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 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) 214 av_packet_unref(_packet);
216 av_free_packet(_packet);
224 delete[] _buffer_alloc;
229 if ((_audio_ctx)&&(_audio_ctx->codec)) {
230 avcodec_close(_audio_ctx);
239 #ifdef HAVE_SWRESAMPLE 241 swr_free(&_resample_ctx);
242 _resample_ctx = NULL;
255 void FfmpegAudioCursor::
258 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) 259 av_packet_unref(_packet);
261 av_free_packet(_packet);
264 while (av_read_frame(_format_ctx, _packet) >= 0) {
265 if (_packet->stream_index == _audio_index) {
266 _packet_size = _packet->size;
267 _packet_data = _packet->data;
270 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) 271 av_packet_unref(_packet);
273 av_free_packet(_packet);
289 bool FfmpegAudioCursor::
292 while (_buffer_head == _buffer_tail) {
294 if (_packet->data == 0) {
296 _buffer_tail = _buffer_size;
297 memset(_buffer, 0, _buffer_size * 2);
299 }
else if (_packet_size > 0) {
300 int bufsize = _buffer_size * 2;
301 #if LIBAVCODEC_VERSION_INT < 3349504 302 int len = avcodec_decode_audio(_audio_ctx, _buffer, &bufsize,
303 _packet_data, _packet_size);
304 movies_debug(
"avcodec_decode_audio returned " << len);
305 #elif LIBAVCODEC_VERSION_INT < 3414272 306 int len = avcodec_decode_audio2(_audio_ctx, _buffer, &bufsize,
307 _packet_data, _packet_size);
308 movies_debug(
"avcodec_decode_audio2 returned " << len);
309 #elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 25, 0) 313 av_init_packet(&pkt);
314 pkt.data = _packet_data;
315 pkt.size = _packet_size;
316 int len = avcodec_decode_audio3(_audio_ctx, _buffer, &bufsize, &pkt);
317 movies_debug(
"avcodec_decode_audio3 returned " << len);
318 av_free_packet(&pkt);
322 av_init_packet(&pkt);
323 pkt.data = _packet_data;
324 pkt.size = _packet_size;
325 int len = avcodec_decode_audio4(_audio_ctx, _frame, &got_frame, &pkt);
326 movies_debug(
"avcodec_decode_audio4 returned " << len);
327 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) 328 av_packet_unref(&pkt);
330 av_free_packet(&pkt);
335 #ifdef HAVE_SWRESAMPLE 338 bufsize = swr_convert(_resample_ctx, (uint8_t **)&_buffer, _buffer_size / 2, (
const uint8_t**)_frame->extended_data, _frame->nb_samples);
339 bufsize *= _audio_channels * 2;
343 bufsize = _frame->linesize[0];
344 memcpy(_buffer, _frame->data[0], bufsize);
347 #if LIBAVUTIL_VERSION_INT > AV_VERSION_INT(52, 19, 100) 348 av_frame_unref(_frame);
354 }
else if (len == 0){
361 _buffer_tail = (bufsize/2);
380 PN_int64 target_ts = (PN_int64)(t / _audio_timebase);
381 if (target_ts < (PN_int64)(_initial_dts)) {
383 target_ts = _initial_dts;
385 if (av_seek_frame(_format_ctx, _audio_index, target_ts, AVSEEK_FLAG_BACKWARD) < 0) {
386 ffmpeg_cat.error() <<
"Seek failure. Shutting down movie.\n";
390 avcodec_close(_audio_ctx);
391 AVCodec *pAudioCodec = avcodec_find_decoder(_audio_ctx->codec_id);
392 if(pAudioCodec == 0) {
396 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 8, 0) 397 if (avcodec_open2(_audio_ctx, pAudioCodec, NULL) < 0) {
399 if (avcodec_open(_audio_ctx, pAudioCodec) < 0) {
407 double ts = _packet->dts * _audio_timebase;
409 int skip = (int)((t-ts) * _audio_rate);
426 int desired = n * _audio_channels;
428 while (desired > 0) {
429 if (_buffer_head == _buffer_tail) {
430 if(!reload_buffer()){
433 movies_debug(
"read_samples() desired samples: " << desired <<
" N:" << n);
435 int available = _buffer_tail - _buffer_head;
436 int ncopy = (desired > available) ? available : desired;
439 memcpy(data, _buffer + _buffer_head, ncopy * 2);
443 _buffer_head += ncopy;
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
AVFormatContext * get_format_context() const
Returns a pointer to the opened ffmpeg context, or NULL if the file was not successfully opened...
TypeHandle is the identifier used to differentiate C++ class types.