1.m_pIOContext = avio_alloc_context(m_pDecodeBuff,
BUF_SIZE, // internal Buffer and its size
0, // bWriteable (1=true,0=false)
&m_CodeQueue, // user data ; will be passed to our callback functions
ReadData,
NULL, // Write callback function (not used in this example)
NULL);
2.
AVInputFormat *pInFmt = NULL;
if (av_probe_input_buffer(m_pIOContext, &pInFmt, "", NULL, 0, 32*1024) < 0)
{
TRACE("probe failed!\n");
return FALSE;
}
3
if(m_pFormatContex == NULL)
{
// Allocate the AVFormatContext:
m_pFormatContex = avformat_alloc_context();
if(m_pFormatContex == NULL)
{
TRACE("avformat alloc failed!\n");
return FALSE;
}
// Set the IOContext:
m_pFormatContex->pb = m_pIOContext;
}
4. if(avformat_open_input(&m_pFormatContex, "", pInFmt, NULL) != 0)
{
TRACE("avformat open failed.\n");
return FALSE;
}
5.
if(avformat_open_input(&m_pFormatContex, "", pInFmt, NULL) != 0)
{
TRACE("avformat open failed.\n");
return FALSE;
}
if(av_find_stream_info(m_pFormatContex) < 0)
{
TRACE("could not fine stream.\n");
return FALSE;
}
6
if(av_find_stream_info(m_pFormatContex) < 0)
{
TRACE("could not fine stream.\n");
return FALSE;
}
av_dump_format(m_pFormatContex, 0, "", 0);
7
for (int i = 0; i < m_pFormatContex->nb_streams; i++)
{
if ( (m_pFormatContex->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
&& (m_iVideoIndex < 0) )
{
m_iVideoIndex = i;
}
}
AVStream *pAVStream;
pAVStream = m_pFormatContex->streams[m_iVideoIndex];
8.
m_pCodecContextDecode = pAVStream->codec;
std::cout<<"***codec_id = "<<m_pCodecContextDecode->codec_id<<endl;
m_pCodecDecode = avcodec_find_decoder(m_pCodecContextDecode->codec_id);
if (m_pCodecDecode == NULL)
{
TRACE("could not find video decoder!\n");
return FALSE;
}
9.
if(m_pFrameDecode == NULL)
{
m_pFrameDecode = avcodec_alloc_frame();
if(m_pFrameDecode == NULL)
{
TRACE("avcodec alloc frame failed!\n");
return FALSE;
}
}
10.
av_init_packet(&m_PacketDecode);
11.
if (av_read_frame(m_pFormatContex, &m_PacketDecode) >= 0 )
{
int iSize = m_PacketDecode.size;
if (m_PacketDecode.stream_index == m_iVideoIndex )
{
int got_picture = 0;
int iRet = 0;
iRet = avcodec_decode_video2(m_pCodecContextDecode, m_pFrameDecode, &got_picture, &m_PacketDecode);
12.if ( got_picture )
{
for ( j = 0; j != m_iHeight; j++)
{
memcpy(m_pCurrYuvBuff + iSize, m_pFrameDecode->data[0] + j * m_pFrameDecode->linesize[0], m_iWidth);
iSize += m_iWidth;
}
for (j = 0; j != m_iHeight / 2; j++)
{
memcpy(m_pCurrYuvBuff + iSize, m_pFrameDecode->data[2] + j * m_pFrameDecode->linesize[2], m_iWidth / 2);
iSize += m_iWidth / 2;
}
for (j = 0; j != m_iHeight / 2; j++)
{
memcpy(m_pCurrYuvBuff + iSize, m_pFrameDecode->data[1] + j * m_pFrameDecode->linesize[1], m_iWidth / 2);
iSize += m_iWidth / 2;
}
if(m_pDecodeCallBack != NULL)
{
m_pDecodeCallBack(m_pCurrYuvBuff, iSize, m_iWidth, m_iHeight, m_pContext);
}
}