48 _filename(src->_filename),
50 _packet_data(nullptr),
53 _resample_ctx(nullptr),
55 _buffer_alloc(nullptr),
64 nassertv(_format_ctx !=
nullptr);
66 if (avformat_find_stream_info(_format_ctx,
nullptr) < 0) {
74#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 41, 100)
75 AVCodecParameters *codecpar;
77 AVCodecContext *codecpar;
81 AVStream *stream =
nullptr;
82 for (
int i = 0; i < (int)_format_ctx->nb_streams; i++) {
83#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 41, 100)
84 codecpar = _format_ctx->streams[i]->codecpar;
86 codecpar = _format_ctx->streams[i]->codec;
88 if (codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
90 stream = _format_ctx->streams[i];
95 if (stream ==
nullptr) {
100 _audio_timebase = av_q2d(stream->time_base);
101 _audio_rate = codecpar->sample_rate;
106#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(59, 18, 101)
107 _audio_channels = codecpar->ch_layout.nb_channels;
109 _audio_channels = codecpar->channels;
112 const AVCodec *pAudioCodec = avcodec_find_decoder(codecpar->codec_id);
113 if (pAudioCodec ==
nullptr) {
118 _audio_ctx = avcodec_alloc_context3(pAudioCodec);
120 if (_audio_ctx ==
nullptr) {
125#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 41, 100)
126 avcodec_parameters_to_context(_audio_ctx, codecpar);
128 avcodec_copy_context(_audio_ctx, codecpar);
131 AVDictionary *opts =
nullptr;
132 av_dict_set(&opts,
"request_sample_fmt",
"s16", 0);
133 if (avcodec_open2(_audio_ctx, pAudioCodec,
nullptr) < 0) {
141 if (_audio_ctx->sample_fmt != AV_SAMPLE_FMT_S16) {
142#ifdef HAVE_SWRESAMPLE
143 if (ffmpeg_cat.is_debug()) {
145 <<
"Codec does not use signed 16-bit sample format. Setting up swresample context.\n";
148 _resample_ctx = swr_alloc();
153#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(59, 18, 101)
154 av_opt_set_chlayout(_resample_ctx,
"in_chlayout", &_audio_ctx->ch_layout, 0);
155 av_opt_set_chlayout(_resample_ctx,
"out_chlayout", &_audio_ctx->ch_layout, 0);
157 av_opt_set_int(_resample_ctx,
"in_channel_count", _audio_channels, 0);
158 av_opt_set_int(_resample_ctx,
"out_channel_count", _audio_channels, 0);
159 av_opt_set_int(_resample_ctx,
"in_channel_layout", _audio_ctx->channel_layout, 0);
160 av_opt_set_int(_resample_ctx,
"out_channel_layout", _audio_ctx->channel_layout, 0);
163 av_opt_set_int(_resample_ctx,
"in_sample_rate", _audio_ctx->sample_rate, 0);
164 av_opt_set_int(_resample_ctx,
"out_sample_rate", _audio_ctx->sample_rate, 0);
165 av_opt_set_sample_fmt(_resample_ctx,
"in_sample_fmt", _audio_ctx->sample_fmt, 0);
166 av_opt_set_sample_fmt(_resample_ctx,
"out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
168 if (swr_init(_resample_ctx) != 0) {
170 <<
"Failed to set up resample context.\n";
171 _resample_ctx =
nullptr;
175 <<
"Codec does not use signed 16-bit sample format, but support for libswresample has not been enabled.\n";
179 _length = (_format_ctx->duration * 1.0) / AV_TIME_BASE;
181 _can_seek_fast =
true;
183#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 45, 101)
184 _frame = av_frame_alloc();
186 _frame = avcodec_alloc_frame();
189#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
190 _packet = av_packet_alloc();
192 _packet =
new AVPacket;
195 _buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE / 2;
196 _buffer_alloc =
new int16_t[_buffer_size + 64];
199 if ((_packet ==
nullptr)||(_buffer_alloc ==
nullptr)) {
203 memset(_packet, 0,
sizeof(AVPacket));
207 _buffer = _buffer_alloc;
208 while (((
size_t)_buffer) & 31) {
213 _initial_dts = _packet->dts;