diff options
Diffstat (limited to 'src/plugins/previewopus_extractor.c')
-rw-r--r-- | src/plugins/previewopus_extractor.c | 108 |
1 files changed, 67 insertions, 41 deletions
diff --git a/src/plugins/previewopus_extractor.c b/src/plugins/previewopus_extractor.c index 45ea420..f137f38 100644 --- a/src/plugins/previewopus_extractor.c +++ b/src/plugins/previewopus_extractor.c | |||
@@ -44,7 +44,7 @@ | |||
44 | #include <libavformat/avformat.h> | 44 | #include <libavformat/avformat.h> |
45 | #include <libavcodec/avcodec.h> | 45 | #include <libavcodec/avcodec.h> |
46 | #include <libswscale/swscale.h> | 46 | #include <libswscale/swscale.h> |
47 | #include <libavresample/avresample.h> | 47 | #include <libswresample/swresample.h> |
48 | 48 | ||
49 | 49 | ||
50 | /** | 50 | /** |
@@ -300,7 +300,7 @@ init_input_frame (AVFrame **frame) | |||
300 | static int | 300 | static int |
301 | init_resampler (AVCodecContext *input_codec_context, | 301 | init_resampler (AVCodecContext *input_codec_context, |
302 | AVCodecContext *output_codec_context, | 302 | AVCodecContext *output_codec_context, |
303 | AVAudioResampleContext **resample_context) | 303 | SwrContext **resample_context) |
304 | { | 304 | { |
305 | /** | 305 | /** |
306 | * Only initialize the resampler if it is necessary, i.e., | 306 | * Only initialize the resampler if it is necessary, i.e., |
@@ -312,7 +312,7 @@ init_resampler (AVCodecContext *input_codec_context, | |||
312 | int error; | 312 | int error; |
313 | 313 | ||
314 | /** Create a resampler context for the conversion. */ | 314 | /** Create a resampler context for the conversion. */ |
315 | if (! (*resample_context = avresample_alloc_context ())) | 315 | if (! (*resample_context = swr_alloc ())) |
316 | { | 316 | { |
317 | #if DEBUG | 317 | #if DEBUG |
318 | fprintf (stderr, "Could not allocate resample context\n"); | 318 | fprintf (stderr, "Could not allocate resample context\n"); |
@@ -326,28 +326,35 @@ init_resampler (AVCodecContext *input_codec_context, | |||
326 | * Default channel layouts based on the number of channels | 326 | * Default channel layouts based on the number of channels |
327 | * are assumed for simplicity (they are sometimes not detected | 327 | * are assumed for simplicity (they are sometimes not detected |
328 | * properly by the demuxer and/or decoder). | 328 | * properly by the demuxer and/or decoder). |
329 | */av_opt_set_int (*resample_context, "in_channel_layout", | 329 | */ |
330 | av_opt_set_int (*resample_context, | ||
331 | "in_channel_layout", | ||
330 | av_get_default_channel_layout ( | 332 | av_get_default_channel_layout ( |
331 | input_codec_context->channels), 0); | 333 | input_codec_context->channels), 0); |
332 | av_opt_set_int (*resample_context, "out_channel_layout", | 334 | av_opt_set_int (*resample_context, |
335 | "out_channel_layout", | ||
333 | av_get_default_channel_layout ( | 336 | av_get_default_channel_layout ( |
334 | output_codec_context->channels), 0); | 337 | output_codec_context->channels), 0); |
335 | av_opt_set_int (*resample_context, "in_sample_rate", | 338 | av_opt_set_int (*resample_context, |
339 | "in_sample_rate", | ||
336 | input_codec_context->sample_rate, 0); | 340 | input_codec_context->sample_rate, 0); |
337 | av_opt_set_int (*resample_context, "out_sample_rate", | 341 | av_opt_set_int (*resample_context, |
342 | "out_sample_rate", | ||
338 | output_codec_context->sample_rate, 0); | 343 | output_codec_context->sample_rate, 0); |
339 | av_opt_set_int (*resample_context, "in_sample_fmt", | 344 | av_opt_set_int (*resample_context, |
345 | "in_sample_fmt", | ||
340 | input_codec_context->sample_fmt, 0); | 346 | input_codec_context->sample_fmt, 0); |
341 | av_opt_set_int (*resample_context, "out_sample_fmt", | 347 | av_opt_set_int (*resample_context, |
348 | "out_sample_fmt", | ||
342 | output_codec_context->sample_fmt, 0); | 349 | output_codec_context->sample_fmt, 0); |
343 | 350 | ||
344 | /** Open the resampler with the specified parameters. */ | 351 | /** Open the resampler with the specified parameters. */ |
345 | if ((error = avresample_open (*resample_context)) < 0) | 352 | if ((error = swr_init (*resample_context)) < 0) |
346 | { | 353 | { |
347 | #if DEBUG | 354 | #if DEBUG |
348 | fprintf (stderr, "Could not open resample context\n"); | 355 | fprintf (stderr, "Could not open resample context\n"); |
349 | #endif | 356 | #endif |
350 | avresample_free (resample_context); | 357 | swr_free (resample_context); |
351 | return error; | 358 | return error; |
352 | } | 359 | } |
353 | } | 360 | } |
@@ -461,7 +468,8 @@ decode_audio_frame (AVFrame *frame, | |||
461 | * The number of audio samples to be allocated is specified in frame_size. | 468 | * The number of audio samples to be allocated is specified in frame_size. |
462 | */ | 469 | */ |
463 | static int | 470 | static int |
464 | init_converted_samples (uint8_t ***converted_input_samples, int*out_linesize, | 471 | init_converted_samples (uint8_t ***converted_input_samples, |
472 | int*out_linesize, | ||
465 | AVCodecContext *output_codec_context, | 473 | AVCodecContext *output_codec_context, |
466 | int frame_size) | 474 | int frame_size) |
467 | { | 475 | { |
@@ -484,7 +492,8 @@ init_converted_samples (uint8_t ***converted_input_samples, int*out_linesize, | |||
484 | * Allocate memory for the samples of all channels in one consecutive | 492 | * Allocate memory for the samples of all channels in one consecutive |
485 | * block for convenience. | 493 | * block for convenience. |
486 | */ | 494 | */ |
487 | if ((error = av_samples_alloc (*converted_input_samples, out_linesize, | 495 | if ((error = av_samples_alloc (*converted_input_samples, |
496 | out_linesize, | ||
488 | output_codec_context->channels, | 497 | output_codec_context->channels, |
489 | frame_size, | 498 | frame_size, |
490 | output_codec_context->sample_fmt, 0)) < 0) | 499 | output_codec_context->sample_fmt, 0)) < 0) |
@@ -509,19 +518,26 @@ init_converted_samples (uint8_t ***converted_input_samples, int*out_linesize, | |||
509 | */ | 518 | */ |
510 | static int | 519 | static int |
511 | convert_samples (uint8_t **input_data, | 520 | convert_samples (uint8_t **input_data, |
512 | uint8_t **converted_data, const int in_sample, const int | 521 | uint8_t **converted_data, |
513 | out_sample, const int out_linesize, | 522 | int in_sample, |
514 | AVAudioResampleContext *resample_context) | 523 | int out_sample, |
524 | int out_linesize, | ||
525 | SwrContext *resample_context) | ||
515 | { | 526 | { |
516 | int error; | 527 | int error; |
517 | 528 | ||
518 | /** Convert the samples using the resampler. */ | 529 | /** Convert the samples using the resampler. */ |
519 | if ((error = avresample_convert (resample_context, converted_data, | 530 | if ((error = swr_convert (resample_context, |
520 | out_linesize, | 531 | converted_data, |
521 | out_sample, input_data, 0, in_sample)) < 0) | 532 | out_linesize, |
533 | out_sample, | ||
534 | input_data, | ||
535 | 0, | ||
536 | in_sample)) < 0) | ||
522 | { | 537 | { |
523 | #if DEBUG | 538 | #if DEBUG |
524 | fprintf (stderr, "Could not convert input samples (error '%s')\n", | 539 | fprintf (stderr, |
540 | "Could not convert input samples (error '%s')\n", | ||
525 | get_error_text (error)); | 541 | get_error_text (error)); |
526 | #endif | 542 | #endif |
527 | return error; | 543 | return error; |
@@ -532,11 +548,13 @@ convert_samples (uint8_t **input_data, | |||
532 | * Perform a sanity check so that the number of converted samples is | 548 | * Perform a sanity check so that the number of converted samples is |
533 | * not greater than the number of samples to be converted. | 549 | * not greater than the number of samples to be converted. |
534 | * If the sample rates differ, this case has to be handled differently | 550 | * If the sample rates differ, this case has to be handled differently |
535 | */if (avresample_available (resample_context)) | 551 | */ |
552 | if (avresample_available (resample_context)) | ||
536 | { | 553 | { |
537 | #if DEBUG | 554 | #if DEBUG |
538 | fprintf (stderr, "%i Converted samples left over\n",avresample_available ( | 555 | fprintf (stderr, |
539 | resample_context)); | 556 | "%i Converted samples left over\n", |
557 | avresample_available (resample_context)); | ||
540 | #endif | 558 | #endif |
541 | } | 559 | } |
542 | 560 | ||
@@ -588,7 +606,7 @@ read_decode_convert_and_store (AVAudioFifo *fifo, | |||
588 | AVFormatContext *input_format_context, | 606 | AVFormatContext *input_format_context, |
589 | AVCodecContext *input_codec_context, | 607 | AVCodecContext *input_codec_context, |
590 | AVCodecContext *output_codec_context, | 608 | AVCodecContext *output_codec_context, |
591 | AVAudioResampleContext *resampler_context, int | 609 | SwrContext *resampler_context, int |
592 | audio_stream_index, | 610 | audio_stream_index, |
593 | int *finished) | 611 | int *finished) |
594 | { | 612 | { |
@@ -609,22 +627,25 @@ read_decode_convert_and_store (AVAudioFifo *fifo, | |||
609 | 627 | ||
610 | } | 628 | } |
611 | /** Decode one frame worth of audio samples. */ | 629 | /** Decode one frame worth of audio samples. */ |
612 | if (decode_audio_frame (input_frame, input_format_context, | 630 | if (decode_audio_frame (input_frame, |
613 | input_codec_context, audio_stream_index, | 631 | input_format_context, |
614 | &data_present, finished)) | 632 | input_codec_context, |
633 | audio_stream_index, | ||
634 | &data_present, | ||
635 | finished)) | ||
615 | { | 636 | { |
616 | #if DEBUG | 637 | #if DEBUG |
617 | fprintf (stderr, "Failed at decode audio\n"); | 638 | fprintf (stderr, "Failed at decode audio\n"); |
618 | #endif | 639 | #endif |
619 | 640 | ||
620 | goto cleanup; | 641 | goto cleanup; |
621 | |||
622 | } | 642 | } |
623 | /** | 643 | /** |
624 | * If we are at the end of the file and there are no more samples | 644 | * If we are at the end of the file and there are no more samples |
625 | * in the decoder which are delayed, we are actually finished. | 645 | * in the decoder which are delayed, we are actually finished. |
626 | * This must not be treated as an error. | 646 | * This must not be treated as an error. |
627 | */if (*finished && ! data_present) | 647 | */ |
648 | if (*finished && ! data_present) | ||
628 | { | 649 | { |
629 | ret = 0; | 650 | ret = 0; |
630 | #if DEBUG | 651 | #if DEBUG |
@@ -636,7 +657,7 @@ read_decode_convert_and_store (AVAudioFifo *fifo, | |||
636 | if (data_present) | 657 | if (data_present) |
637 | { | 658 | { |
638 | int out_linesize; | 659 | int out_linesize; |
639 | // FIX ME: I'm losing samples, but can't get it to work. | 660 | // FIXME: I'm losing samples, but can't get it to work. |
640 | int out_samples = avresample_available (resampler_context) | 661 | int out_samples = avresample_available (resampler_context) |
641 | + avresample_get_delay (resampler_context) | 662 | + avresample_get_delay (resampler_context) |
642 | + input_frame->nb_samples; | 663 | + input_frame->nb_samples; |
@@ -645,7 +666,8 @@ read_decode_convert_and_store (AVAudioFifo *fifo, | |||
645 | // fprintf(stderr, "Input nbsamples %i out_samples: %i \n",input_frame->nb_samples,out_samples); | 666 | // fprintf(stderr, "Input nbsamples %i out_samples: %i \n",input_frame->nb_samples,out_samples); |
646 | 667 | ||
647 | /** Initialize the temporary storage for the converted input samples. */ | 668 | /** Initialize the temporary storage for the converted input samples. */ |
648 | if (init_converted_samples (&converted_input_samples, &out_linesize, | 669 | if (init_converted_samples (&converted_input_samples, |
670 | &out_linesize, | ||
649 | output_codec_context, | 671 | output_codec_context, |
650 | out_samples)) | 672 | out_samples)) |
651 | { | 673 | { |
@@ -659,20 +681,23 @@ read_decode_convert_and_store (AVAudioFifo *fifo, | |||
659 | * Convert the input samples to the desired output sample format. | 681 | * Convert the input samples to the desired output sample format. |
660 | * This requires a temporary storage provided by converted_input_samples. | 682 | * This requires a temporary storage provided by converted_input_samples. |
661 | */ | 683 | */ |
662 | if (convert_samples (input_frame->extended_data, converted_input_samples, | 684 | if (convert_samples (input_frame->extended_data, |
663 | input_frame->nb_samples, out_samples, out_linesize, | 685 | converted_input_samples, |
686 | input_frame->nb_samples, | ||
687 | out_samples, | ||
688 | out_linesize, | ||
664 | resampler_context)) | 689 | resampler_context)) |
665 | { | 690 | { |
666 | |||
667 | |||
668 | #if DEBUG | 691 | #if DEBUG |
669 | fprintf (stderr, "Failed at convert_samples, input frame %i \n", | 692 | fprintf (stderr, |
693 | "Failed at convert_samples, input frame %i \n", | ||
670 | input_frame->nb_samples); | 694 | input_frame->nb_samples); |
671 | #endif | 695 | #endif |
672 | goto cleanup; | 696 | goto cleanup; |
673 | } | 697 | } |
674 | /** Add the converted input samples to the FIFO buffer for later processing. */ | 698 | /** Add the converted input samples to the FIFO buffer for later processing. */ |
675 | if (add_samples_to_fifo (fifo, converted_input_samples, | 699 | if (add_samples_to_fifo (fifo, |
700 | converted_input_samples, | ||
676 | out_samples)) | 701 | out_samples)) |
677 | { | 702 | { |
678 | #if DEBUG | 703 | #if DEBUG |
@@ -881,7 +906,7 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) | |||
881 | AVDictionary *options; | 906 | AVDictionary *options; |
882 | AVFrame *frame; | 907 | AVFrame *frame; |
883 | AVCodecContext*output_codec_context = NULL; | 908 | AVCodecContext*output_codec_context = NULL; |
884 | AVAudioResampleContext *resample_context = NULL; | 909 | SwrContext *resample_context = NULL; |
885 | AVAudioFifo *fifo = NULL; | 910 | AVAudioFifo *fifo = NULL; |
886 | 911 | ||
887 | int audio_stream_index; | 912 | int audio_stream_index; |
@@ -1045,7 +1070,8 @@ extract_audio (struct EXTRACTOR_ExtractContext *ec) | |||
1045 | * Since the decoder's and the encoder's frame size may differ, we | 1070 | * Since the decoder's and the encoder's frame size may differ, we |
1046 | * need to FIFO buffer to store as many frames worth of input samples | 1071 | * need to FIFO buffer to store as many frames worth of input samples |
1047 | * that they make up at least one frame worth of output samples. | 1072 | * that they make up at least one frame worth of output samples. |
1048 | */while ((av_audio_fifo_size (fifo) < output_frame_size)) | 1073 | */ |
1074 | while ((av_audio_fifo_size (fifo) < output_frame_size)) | ||
1049 | { | 1075 | { |
1050 | /** | 1076 | /** |
1051 | * Decode one frame worth of audio samples, convert it to the | 1077 | * Decode one frame worth of audio samples, convert it to the |
@@ -1143,8 +1169,8 @@ cleanup: | |||
1143 | av_audio_fifo_free (fifo); | 1169 | av_audio_fifo_free (fifo); |
1144 | if (resample_context) | 1170 | if (resample_context) |
1145 | { | 1171 | { |
1146 | avresample_close (resample_context); | 1172 | swr_close (resample_context); |
1147 | avresample_free (&resample_context); | 1173 | swr_free (&resample_context); |
1148 | } | 1174 | } |
1149 | if (output_codec_context) | 1175 | if (output_codec_context) |
1150 | avcodec_close (output_codec_context); | 1176 | avcodec_close (output_codec_context); |