Reading a file located in memory with libavformat

前端 未结 3 799
独厮守ぢ
独厮守ぢ 2020-12-12 18:30

I\'m currently trying to read small video files sent from a server

In order to read a file using libavformat, you are supposed to call

av_open_input_         


        
3条回答
  •  时光取名叫无心
    2020-12-12 18:59

    Tomaka17's excellent answer gave me a good start toward solving an analogous problem using Qt QIODevice rather than std::istream. I found I needed to blend aspects of Tomaka17's solution, with aspects of the related experience at http://cdry.wordpress.com/2009/09/09/using-custom-io-callbacks-with-ffmpeg/

    My custom Read function looks like this:

    int readFunction(void* opaque, uint8_t* buf, int buf_size)
    {
        QIODevice* stream = (QIODevice*)opaque;
        int numBytes = stream->read((char*)buf, buf_size);
        return numBytes;
    }
    

    ...but I also needed to create a custom Seek function:

    int64_t seekFunction(void* opaque, int64_t offset, int whence)
    {
        if (whence == AVSEEK_SIZE)
            return -1; // I don't know "size of my handle in bytes"
        QIODevice* stream = (QIODevice*)opaque;
        if (stream->isSequential())
            return -1; // cannot seek a sequential stream
        if (! stream->seek(offset) )
            return -1;
        return stream->pos();
    }
    

    ...and I tied it together like this:

    ...
    const int ioBufferSize = 32768;
    unsigned char * ioBuffer = (unsigned char *)av_malloc(ioBufferSize + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
    AVIOContext * avioContext = avio_alloc_context(ioBuffer, ioBufferSize, 0, (void*)(&fileStream), &readFunction, NULL, &seekFunction);
    AVFormatContext * container = avformat_alloc_context();
    container->pb = avioContext;
    avformat_open_input(&container, "dummyFileName", NULL, NULL);
    ...
    

    Note I have not yet worked out the memory management issues.

提交回复
热议问题