15 #include "config_ffmpeg.h"
16 #include "ffmpegAudioCursor.h"
18 #include "ffmpegAudio.h"
20 #include "libavutil/dict.h"
21 #include "libavutil/opt.h"
22 #include "libavcodec/avcodec.h"
23 #include "libavformat/avformat.h"
26 #ifdef HAVE_SWRESAMPLE
28 #include "libswresample/swresample.h"
34 #if LIBAVFORMAT_VERSION_MAJOR < 53
35 #define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
38 #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
40 #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
51 _filename(src->_filename),
56 #ifdef HAVE_SWRESAMPLE
69 nassertv(_format_ctx != NULL);
71 #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 6, 0)
72 if (avformat_find_stream_info(_format_ctx, NULL) < 0) {
74 if (av_find_stream_info(_format_ctx) < 0) {
81 for (
int i = 0; i < (int)_format_ctx->nb_streams; i++) {
82 if (_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
84 _audio_ctx = _format_ctx->streams[i]->codec;
85 _audio_timebase = av_q2d(_format_ctx->streams[i]->time_base);
86 _audio_rate = _audio_ctx->sample_rate;
87 _audio_channels = _audio_ctx->channels;
91 if (_audio_ctx == 0) {
96 AVCodec *pAudioCodec = avcodec_find_decoder(_audio_ctx->codec_id);
97 if (pAudioCodec == 0) {
102 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 8, 0)
103 AVDictionary *opts = NULL;
104 av_dict_set(&opts,
"request_sample_fmt",
"s16", 0);
105 if (avcodec_open2(_audio_ctx, pAudioCodec, NULL) < 0) {
107 if (avcodec_open(_audio_ctx, pAudioCodec) < 0) {
113 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 8, 0)
118 if (_audio_ctx->sample_fmt != AV_SAMPLE_FMT_S16) {
119 #ifdef HAVE_SWRESAMPLE
120 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 25, 0)
122 <<
"Codec does not use signed 16-bit sample format. Upgrade libavcodec to 53.25.0 or higher.\n";
125 <<
"Codec does not use signed 16-bit sample format. Setting up swresample context.\n";
128 _resample_ctx = swr_alloc();
129 av_opt_set_int(_resample_ctx,
"in_channel_layout", _audio_ctx->channel_layout, 0);
130 av_opt_set_int(_resample_ctx,
"out_channel_layout", _audio_ctx->channel_layout, 0);
131 av_opt_set_int(_resample_ctx,
"in_sample_rate", _audio_ctx->sample_rate, 0);
132 av_opt_set_int(_resample_ctx,
"out_sample_rate", _audio_ctx->sample_rate, 0);
133 av_opt_set_sample_fmt(_resample_ctx,
"in_sample_fmt", _audio_ctx->sample_fmt, 0);
134 av_opt_set_sample_fmt(_resample_ctx,
"out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
136 if (swr_init(_resample_ctx) != 0) {
138 <<
"Failed to set up resample context.\n";
139 _resample_ctx = NULL;
143 <<
"Codec does not use signed 16-bit sample format, but support for libswresample has not been enabled.\n";
147 _length = (_format_ctx->duration * 1.0) / AV_TIME_BASE;
149 _can_seek_fast =
true;
151 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
152 _frame = av_frame_alloc();
154 _frame = avcodec_alloc_frame();
157 _packet =
new AVPacket;
158 _buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE / 2;
159 _buffer_alloc =
new PN_int16[_buffer_size + 64];
162 if ((_packet == 0)||(_buffer_alloc == 0)) {
166 memset(_packet, 0,
sizeof(AVPacket));
170 _buffer = _buffer_alloc;
171 while (((
size_t)_buffer) & 31) {
176 _initial_dts = _packet->dts;
198 void FfmpegAudioCursor::
201 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 45, 101)
202 av_frame_free(&_frame);
203 #elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
204 avcodec_free_frame(&_frame);
213 av_free_packet(_packet);
220 delete[] _buffer_alloc;
225 if ((_audio_ctx)&&(_audio_ctx->codec)) {
226 avcodec_close(_audio_ctx);
235 #ifdef HAVE_SWRESAMPLE
237 swr_free(&_resample_ctx);
238 _resample_ctx = NULL;
251 void FfmpegAudioCursor::
254 av_free_packet(_packet);
256 while (av_read_frame(_format_ctx, _packet) >= 0) {
257 if (_packet->stream_index == _audio_index) {
258 _packet_size = _packet->size;
259 _packet_data = _packet->data;
262 av_free_packet(_packet);
277 bool FfmpegAudioCursor::
280 while (_buffer_head == _buffer_tail) {
282 if (_packet->data == 0) {
284 _buffer_tail = _buffer_size;
285 memset(_buffer, 0, _buffer_size * 2);
287 }
else if (_packet_size > 0) {
288 int bufsize = _buffer_size * 2;
289 #if LIBAVCODEC_VERSION_INT < 3349504
290 int len = avcodec_decode_audio(_audio_ctx, _buffer, &bufsize,
291 _packet_data, _packet_size);
292 movies_debug(
"avcodec_decode_audio returned " << len);
293 #elif LIBAVCODEC_VERSION_INT < 3414272
294 int len = avcodec_decode_audio2(_audio_ctx, _buffer, &bufsize,
295 _packet_data, _packet_size);
296 movies_debug(
"avcodec_decode_audio2 returned " << len);
297 #elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 25, 0)
301 av_init_packet(&pkt);
302 pkt.data = _packet_data;
303 pkt.size = _packet_size;
304 int len = avcodec_decode_audio3(_audio_ctx, _buffer, &bufsize, &pkt);
305 movies_debug(
"avcodec_decode_audio3 returned " << len);
306 av_free_packet(&pkt);
310 av_init_packet(&pkt);
311 pkt.data = _packet_data;
312 pkt.size = _packet_size;
313 int len = avcodec_decode_audio4(_audio_ctx, _frame, &got_frame, &pkt);
314 movies_debug(
"avcodec_decode_audio4 returned " << len);
315 av_free_packet(&pkt);
319 #ifdef HAVE_SWRESAMPLE
322 bufsize = swr_convert(_resample_ctx, (uint8_t **)&_buffer, _buffer_size / 2, (
const uint8_t**)_frame->extended_data, _frame->nb_samples);
323 bufsize *= _audio_channels * 2;
327 bufsize = _frame->linesize[0];
328 memcpy(_buffer, _frame->data[0], bufsize);
331 #if LIBAVUTIL_VERSION_INT > AV_VERSION_INT(52, 19, 100)
332 av_frame_unref(_frame);
338 }
else if (len == 0){
345 _buffer_tail = (bufsize/2);
364 PN_int64 target_ts = (PN_int64)(t / _audio_timebase);
365 if (target_ts < (PN_int64)(_initial_dts)) {
367 target_ts = _initial_dts;
369 if (av_seek_frame(_format_ctx, _audio_index, target_ts, AVSEEK_FLAG_BACKWARD) < 0) {
370 ffmpeg_cat.error() <<
"Seek failure. Shutting down movie.\n";
374 avcodec_close(_audio_ctx);
375 AVCodec *pAudioCodec = avcodec_find_decoder(_audio_ctx->codec_id);
376 if(pAudioCodec == 0) {
380 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 8, 0)
381 if (avcodec_open2(_audio_ctx, pAudioCodec, NULL) < 0) {
383 if (avcodec_open(_audio_ctx, pAudioCodec) < 0) {
391 double ts = _packet->dts * _audio_timebase;
393 int skip = (int)((t-ts) * _audio_rate);
410 int desired = n * _audio_channels;
412 while (desired > 0) {
413 if (_buffer_head == _buffer_tail) {
414 if(!reload_buffer()){
417 movies_debug(
"read_samples() desired samples: " << desired <<
" N:" << n);
419 int available = _buffer_tail - _buffer_head;
420 int ncopy = (desired > available) ? available : desired;
423 memcpy(data, _buffer + _buffer_head, ncopy * 2);
427 _buffer_head += ncopy;
AVFormatContext * get_format_context() const
Returns a pointer to the opened ffmpeg context, or NULL if the file was not successfully opened...
virtual void seek(double offset)
Seeks to a target location.
bool open_vfs(const Filename &filename)
Opens the movie file via Panda's VFS.
A stream that generates a sequence of audio samples.
void close()
Explicitly closes the opened file.
virtual void read_samples(int n, PN_int16 *data)
Read audio samples from the stream.
virtual ~FfmpegAudioCursor()
xxx
A MovieAudio is actually any source that provides a sequence of audio samples.
FfmpegAudioCursor(FfmpegAudio *src)
xxx
TypeHandle is the identifier used to differentiate C++ class types.