avio_alloc_context() is the right direction.  Here is my implementation of your "read_fun" which uses a QIODevice <https://doc.qt.io/qt-5/qiodevice.html> instead of std::fstream.   libav calls read_fun when it wants data.  opaque will point to your ifs.  You need to read from ifs and fill libav's buffer (*buf) and then return how many bytes you put into the buffer.

static int read_fun(void *opaque, uint8_t *buf, int bufSize)
{
    QIODevice *dataStream = (QIODevice *) opaque;
    bufSize = FFMIN(bufSize, dataStream->bytesAvailable());
    dataStream->read((char*)buf, bufSize);

    return bufSize;
}

On 2020-01-23 1:22 p.m., Ed Rupp wrote:
Hi,

TL;DR; Any direction on how to feed avformat a buffer of bytes (the contents of which are a complete file) instead of a file from disk is all I really need. Thanks for making this lib it's pretty amazing and I'm just getting started...

I'm working within a framework in which I receive complete files in the form of a uint8_t* and length.
At first it seemed like the way to go would be custom IO...
Here's some of the code
//////////////////////
AVFormatContext *fmt = avformat_alloc_context();
std::fstream ifs;
ifs.read(reinterpret_cast<char*>(my_data_from_framework), length);
uint8_t *buf = (unsigned char*)av_malloc(8192); // magic number from web examples
//The read functions below us fstream operations tellg and seekp.
AVIOContext *avio = avio_alloc_context(buf, 8192, 0, (void*)&ifs, read_fun, NULL, seek_fun);
fmt->pb = avio;

//Some of the docs I read made it seem like passing a NULL for filename would work.
int status = avformat_open_input(&fmt, NULL, NULL,NULL)
//avformat_open_input is returning -1 and yes the seg fault happens at the next call below

///////////////

After doing the above I follow up with the following call and get a segfault

avformat_find_stream_info(fmt, NULL);


The docs and the code in libformat utils.c: init_input (see below) make it seem like I should be able to do this i.e. set pb on AVFormatContext but its not doing what I hoped:
325 /* Open input file and probe the format if necessary. */
326 <https://www.ffmpeg.org/doxygen/2.5/libavformat_2utils_8c.html#a18b3e6a0f95cd0b00f35765781f19d36> static int init_input <https://www.ffmpeg.org/doxygen/2.5/libavformat_2utils_8c.html#a18b3e6a0f95cd0b00f35765781f19d36>(AVFormatContext <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html> *s <https://www.ffmpeg.org/doxygen/2.5/avisynth__c_8h.html#ab87f55bd0280d90925050a4188c14ab5>, const char *filename, 327 AVDictionary <https://www.ffmpeg.org/doxygen/2.5/structAVDictionary.html> **options <https://www.ffmpeg.org/doxygen/2.5/ffmpeg_8h.html#abdbaa7127ef32aa4c435f3c609b4c2cd>)
328 {
329 int ret <https://www.ffmpeg.org/doxygen/2.5/avfilter_8c.html#a339672ff94e6199019102f50d317c3d7>; 330 AVProbeData <https://www.ffmpeg.org/doxygen/2.5/structAVProbeData.html> pd = { filename, NULL, 0 }; 331 int score = AVPROBE_SCORE_RETRY <https://www.ffmpeg.org/doxygen/2.5/avformat_8h.html#adeacd8263046043734062588db39bce9>;
332
333 if (s->pb <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87>) { 334  s->flags <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a32379cc371463b235d54235d4af06a15> |= AVFMT_FLAG_CUSTOM_IO <https://www.ffmpeg.org/doxygen/2.5/avformat_8h.html#ac5fdea71141dcc30346a57df75cf408e>; 335 if (!s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374>) 336 return av_probe_input_buffer2 <https://www.ffmpeg.org/doxygen/2.5/group__lavf__decoding.html#ga69e44bd9ade0160a8cd6196aa37505ea>(s->pb <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87>, &s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374>, filename, 337  s, 0, s->format_probesize <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a7d1f665ecabe03e3d18accbb1ebb32b1>); 338 else if (s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374>->flags <https://www.ffmpeg.org/doxygen/2.5/structAVInputFormat.html#a1b30f6647d0c2faf38ba8786d7c3a838> & AVFMT_NOFILE <https://www.ffmpeg.org/doxygen/2.5/avformat_8h.html#a752cce390d480521919aa5d8be24ac0b>) 339 av_log <https://www.ffmpeg.org/doxygen/2.5/log_8c.html#a33c4311b86f031152c5a23b91c4fe862>(s, AV_LOG_WARNING <https://www.ffmpeg.org/doxygen/2.5/group__lavu__log__constants.html#ga85b57516ca703cc47d9bbe5f4658c716>, "Custom AVIOContext makes no sense and "
340 "will be ignored with AVFMT_NOFILE format.\n");
341 return 0;
342  }
343
344 if ((s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374> && s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374>->flags <https://www.ffmpeg.org/doxygen/2.5/structAVInputFormat.html#a1b30f6647d0c2faf38ba8786d7c3a838> & AVFMT_NOFILE <https://www.ffmpeg.org/doxygen/2.5/avformat_8h.html#a752cce390d480521919aa5d8be24ac0b>) || 345  (!s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374> && (s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374> = av_probe_input_format2 <https://www.ffmpeg.org/doxygen/2.5/group__lavf__decoding.html#gae8b938f6e7c3741dd27a6c171e72f33d>(&pd, 0, &score))))
346 return score;
347
348 if ((ret = avio_open2 <https://www.ffmpeg.org/doxygen/2.5/avio_8h.html#ade8a63980569494c99593ebf0d1e891b>(&s->pb <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87>, filename, AVIO_FLAG_READ <https://www.ffmpeg.org/doxygen/2.5/avio_8h.html#a21e61cb486bd1588eb7f775998cf8c77> | s->avio_flags <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a5e6814c9de3c272396f07e2ff18c7b27>, 349  &s->interrupt_callback <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a5b37acfe4024d92ee510064e80920b40>, options)) < 0) 350 return ret <https://www.ffmpeg.org/doxygen/2.5/avfilter_8c.html#a339672ff94e6199019102f50d317c3d7>; 351 if (s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374>)
352 return 0;
353 return av_probe_input_buffer2 <https://www.ffmpeg.org/doxygen/2.5/group__lavf__decoding.html#ga69e44bd9ade0160a8cd6196aa37505ea>(s->pb <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87>, &s->iformat <https://www.ffmpeg.org/doxygen/2.5/structAVFormatContext.html#a78efc5a53c21c8d81197445207ac4374>, filename,
354
Thanks
Ed

_______________________________________________
Libav-user mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".

Reply via email to