Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been referencing this article about how to create a custom IO-Context with FFmpeg, but he invoked the use of an IStream interface. I am simply just trying to use an std::ifstream object in order to create the Context and proceed with opening it in the

What I have tried:

Essentially what I have tried so far is close to what the user has done but I still find myself struggling with a lot of issues. I created the following typedef struct and functions below in order to run it but I have experienced a lot of errors and issues that I'm just unsure how to resolve. If anyone could take a look at this and show me what I need to correct or if there's an easier way to do this I'd truly appreciate it. Thanks.

typedef struct struct_stream
    std::ifstream audio_stream;


Stream * audio_file = NULL;
(audio_file)->audio_stream(filename, std::ifstream::in);

unsigned char *buffer = (unsigned char *)av_malloc(3000);
AVIOContext *avio_ctx = NULL;

avio_ctx = avio_alloc_context(buffer, 3000, 0, audio_file, ReadFunc, NULL, SeekFunc);

// Set the IOContext:
AVFormatContext* pCtx = avformat_alloc_context();
pCtx->pb = pIOCtx;
int ret = avformat_open_input(&pCtx, "", NULL, NULL);

int ReadFunc(void* ptr, uint8_t* buf, int buf_size)
    Stream* pStream = reinterpret_cast<Stream*>(ptr);

    (pStream->audio_stream).read(buf, sizeof buf);
     std::streamsize bytes = (pStream->audio_stream).gcount();

  return (int) bytes;
// whence: SEEK_SET, SEEK_CUR, SEEK_END (like fseek) and AVSEEK_SIZE
int64_t SeekFunc(void* ptr, int64_t pos, int whence)
  // honestly not sure how to write this function but I've been using this as a resource
<a href="">resource</a>


If anyone happens to know how to perform this with an std::ifstream object or know what I can do to fix my program I'd really appreciate it. Thank you!
Updated 26-Jun-21 22:23pm
Richard MacCutchan 27-Jun-21 3:24am
"a lot of errors and issues"
Why do so many people who post questions here think that we can magically guess what errors they encounter?

1 solution

It looks like you missed some steps to create working code with not initializing your audio_file. It needs to be created before used.

Maybe you read some ffmpeg tutorial to start. The ffmpeg is tricky so stick the documentation and working code tutorials.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900