<div dir="ltr"><div>I am trying to set up a custom I/O read callback to read from a std::list of frames I transmitted via TCP.  In each std::list<item> contains a frame buffer that was written using a custom I/O write_packet, with an associated frame size and some other information about each frame that I need.  I have verified I am getting all of the information by writing out my frame buffers to disk and opening them up in mplayer or vlc.  The below appears to work, but after avformat_open_input(&m_FormatCtx,"fake.h264",NULL,NULL);  None of the necessary AVContextCodec information appears to be there.  It does find the codec based on the fake filename, fake.h264, but does not read anything in from my header, like height or width... Which makes me think it's just reading the fake filename to determine codec info and not reading in my I/O buffer...<br>

<br></div><div>In my Read_Packet callback I write out the first 32K (all that is read when avformat_open_input is called) and write that out to disk.  mplayer cannot play anything (obviously, it's only 32K), but it is able to parse the header and get height and width:<br>

</div><div><br>VIDEO:  [H264]  696x556  0bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s)<br><br></div><div>I know the buffer has the correct information in it!<br><br></div><div>It should be known that the frame size is larger than 32K, so I manage the position in the buffer with my opaque pointer.  Once finished with the frame, I pop it off.<br>

</div><div><br>static int Read_Packet(void *opaque, uint8_t *buf, int size)<br>{<br>    FrameList *l = (FrameList *)opaque;<br><br>    if(l->empty())<br>
        return 0;<br><br>    Frame_ptr p = l->front();<br>    int ret;<br><br>    if(p->H264_frameSize - p->H264_framePos < size)<br>    {<br>        memcpy(buf,p->H264_frameData + p->H264_framePos,p->H264_frameSize - p->H264_framePos);<br>


        l->pop_front();<br>        ret=p->H264_frameSize - p->H264_framePos;<br>    }else<br>    {<br>        memcpy(buf,p->H264_frameData + p->H264_framePos,size);<br>        p->H264_framePos+=size;<br>

        ret=size;<br>
    }<br><br>    fwrite(buf, ret, 1, fo); //for debugging... Save out 32K, in which mplayer can read the header<br><br>    return ret;<br>}<br><br></div>int main()<br><div>..... snip<br>    AVIOContext *avioctx;<br>    m_BufferCallBack= (unsigned char*)av_malloc(BUFFERSIZE * sizeof(uint8_t));<br>


    avioctx = avio_alloc_context( m_BufferCallBack, //IOBuffer<br>                                  BUFFERSIZE,       //Buffer Size<br>                                  0,                //Write flag, only reading, so 0<br>


                                  m_H264FrameList,  //H264 Frame pointer (opaque)<br>                                  Read_Packet,      //Read callback<br>                                  NULL,             //Write callback<br>


                                  NULL);            //file_seek... not using buffer again so 0 is ok<br><br><br>    m_FormatCtx->pb=avioctx;<br><br>    int ret = avformat_open_input(&m_FormatCtx,"fake.h264",NULL,NULL);<br>


    if (ret < 0)<br>        printf("Cannot open buffer in libavformat\n");<br><br>    videoStream=-1;<br>    videoStream = av_find_best_stream(m_FormatCtx,AVMEDIA_TYPE_VIDEO,-1,-1,&m_avCodec,0);<br><br>    // Get a pointer to the codec context for the video stream<br>

    m_CodecCtx=m_FormatCtx->streams[videoStream]->codec;<br><br>    m_avCodec=avcodec_find_decoder(m_CodecCtx->codec_id);<br>    if(m_avCodec==NULL)<br>        return false; // Codec not found<br><br>    // Open codec<br>

    if(avcodec_open2(m_CodecCtx, m_avCodec,NULL)<0)<br>        return false; // Could not open codec<br><br>    // Allocate video frame<br>    m_avFrame=avcodec_alloc_frame();<br><br>    // Allocate an AVFrame structure<br>

    m_avFrameRGB=avcodec_alloc_frame();<br>    if(m_avFrameRGB==NULL)<br>        return false;<br><br>    av_log(NULL, AV_LOG_INFO, "Width %i | Height %i \n",m_CodecCtx->width,m_CodecCtx->height);<br><br>
    // Determine required buffer size and allocate buffer<br>
    //m_numBytes=avpicture_get_size(PIX_FMT_RGB24, m_CodecCtx->width,m_CodecCtx->height);<br>    m_numBytes=avpicture_get_size(PIX_FMT_YUYV422, m_CodecCtx->width,m_CodecCtx->height);<br>    m_buffer=new uint8_t[m_numBytes];<br>

<br>    // Assign appropriate parts of buffer to image planes in m_avFrameRGB<br>    avpicture_fill((AVPicture *)m_avFrameRGB, m_buffer, PIX_FMT_YUYV422,<br>                   m_CodecCtx->width, m_CodecCtx->height);<br>

</div><div>..... snip<br></div><div><br></div></div>