Reputation: 105
I am wondering how to get the extradata from an ALAC media file using the FFmpeg library without having to manually parse the file?
I setup initially with:
avformat_open_input(&formatContext, pszFileName, 0, 0);
avformat_find_stream_info(formatContext, NULL);
av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
codecContext = avcodec_alloc_context3(codec);
Currently I am able to detect and find the ALAC codec but it fails to open the codec returning AVERROR_INVALIDDATA, which comes from the extradata and extradata_size not being set.
avcodec_open2(codecContext, codec, NULL);
The FFmpeg documentation states that some codecs require extradata and extradata_size to be set to the codec's specifications. But shouldn't this data be set by avformat_find_stream_info?
Upvotes: 0
Views: 2467
Reputation: 11184
Yes, extradata
is populated during either avformat_open_input()
or avformat_find_stream_info()
. However, it populates it in a field you didn't use. You need the following code:
avformat_open_input(&formatContext, pszFileName, 0, 0);
avformat_find_stream_info(formatContext, NULL);
int audioStreamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
codecContext = avcodec_alloc_context3(codec);
avcodec_copy_context(codecContext, formatContext->streams[audioStreamIndex]->codec);
avcodec_open2(codecContext, codec, NULL);
The relevant extra line is avcodec_copy_context()
, which copies the data from the libavformat
demuxer (in formatContext->streams[]
) to the copy of that context (codecContext
) that you will use for decoding using the decoder in libavcodec
.
Upvotes: 2