//ffmpeg.cpp
#include "ffmpeg.h"
int nRet = 0;
AVFormatContext* icodec = NULL;
AVInputFormat* ifmt = NULL;
char szError[256];
AVFormatContext* oc = NULL;
AVOutputFormat* ofmt = NULL;
AVStream * video_st = NULL;
AVStream * audio_st = NULL;
AVCodec *audio_codec;
AVCodec *video_codec;
double audio_pts = 0.0;
double video_pts = 0.0;
int video_stream_idx = -1;
int audio_stream_idx = -1;
AVPacket pkt;
AVBitStreamFilterContext * vbsf_aac_adtstoasc = NULL;
//video param
int m_dwWidth = 0;
int m_dwHeight = 0;
double m_dbFrameRate = 25.0; //帧率
AVCodecID video_codecID = AV_CODEC_ID_H264;
AVPixelFormat video_pixelfromat = AV_PIX_FMT_YUV420P;
char spspps[100];
int spspps_size = 0;
//audio param
int m_dwChannelCount = 2; //声道
int m_dwBitsPerSample = 16; //样本
int m_dwFrequency = 44100; //采样率
AVCodecID audio_codecID = AV_CODEC_ID_AAC;
int audio_frame_size = 1024;
int InitInput(char * Filename,AVFormatContext ** iframe_c,AVInputFormat** iinputframe)
{
int i = 0;
nRet = avformat_open_input(iframe_c, Filename,(*iinputframe), NULL);
if (nRet != 0)
{
av_strerror(nRet, szError, 256);
printf(szError);
printf("\n");
printf("Call avformat_open_input function failed!\n");
return 0;
}
if (av_find_stream_info(*iframe_c)
{
printf("Call av_find_stream_info function failed!\n");
return 0;
}
//输出视频信息
av_dump_format(*iframe_c, -1, Filename, 0);
//添加音频信息到输出context
for (i = 0; i nb_streams; i++)
{
if ((*iframe_c)->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
video_stream_idx = i;
m_dwWidth = (*iframe_c)->streams[i]->codec->width;
m_dwHeight = (*iframe_c)->streams[i]->codec->height;
m_dbFrameRate = av_q2d((*iframe_c)->streams[i]->r_frame_rate);
video_codecID = (*iframe_c)->streams[i]->codec->codec_id;
video_pixelfromat = (*iframe_c)->streams[i]->codec->pix_fmt;
spspps_size = (*iframe_c)->streams[i]->codec->extradata_size;
memcpy(spspps,(*iframe_c)->streams[i]->codec->extradata,spspps_size);
}
else if ((*iframe_c)->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audio_stream_idx = i;
m_dwChannelCount = (*iframe_c)->streams[i]->codec->channels;
switch ((*iframe_c)->streams[i]->codec->sample_fmt)
{