]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libopusdec.c
h264dec: Fix mix of lossless and lossy MBs decoding
[ffmpeg] / libavcodec / libopusdec.c
index 1b5b7464961d3cf1300bd1bad106500096fcda8c..781635615c488e0b54a08e1ae528e3f2626b3329 100644 (file)
 #include <opus.h>
 #include <opus_multistream.h>
 
-#include "libavutil/avassert.h"
 #include "libavutil/intreadwrite.h"
+
 #include "avcodec.h"
+#include "internal.h"
 #include "vorbis.h"
 #include "mathops.h"
 #include "libopus.h"
 
 struct libopus_context {
     OpusMSDecoder *dec;
-    AVFrame frame;
 };
 
 #define OPUS_HEAD_SIZE 19
@@ -42,6 +42,19 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
     int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
     uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
 
+    if (avc->channels <= 0) {
+        av_log(avc, AV_LOG_WARNING,
+               "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
+        avc->channels = 2;
+    }
+
+    avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
+    if (avc->channels <= 0) {
+        av_log(avc, AV_LOG_WARNING,
+               "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
+        avc->channels = 2;
+    }
+
     avc->sample_rate    = 48000;
     avc->sample_fmt     = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
                           AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
@@ -73,7 +86,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
         const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
         int ch;
 
-        /* Remap channels from vorbis order to libav order */
+        /* Remap channels from Vorbis order to libav order */
         for (ch = 0; ch < avc->channels; ch++)
             mapping_arr[ch] = mapping[vorbis_offset[ch]];
         mapping = mapping_arr;
@@ -94,8 +107,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
                opus_strerror(ret));
 
     avc->delay = 3840;  /* Decoder delay (in samples) at 48kHz */
-    avcodec_get_frame_defaults(&opus->frame);
-    avc->coded_frame = &opus->frame;
+
     return 0;
 }
 
@@ -109,14 +121,15 @@ static av_cold int libopus_decode_close(AVCodecContext *avc)
 
 #define MAX_FRAME_SIZE (960 * 6)
 
-static int libopus_decode(AVCodecContext *avc, void *frame,
+static int libopus_decode(AVCodecContext *avc, void *data,
                           int *got_frame_ptr, AVPacket *pkt)
 {
     struct libopus_context *opus = avc->priv_data;
+    AVFrame *frame               = data;
     int ret, nb_samples;
 
-    opus->frame.nb_samples = MAX_FRAME_SIZE;
-    ret = avc->get_buffer(avc, &opus->frame);
+    frame->nb_samples = MAX_FRAME_SIZE;
+    ret = ff_get_buffer(avc, frame, 0);
     if (ret < 0) {
         av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
@@ -124,12 +137,12 @@ static int libopus_decode(AVCodecContext *avc, void *frame,
 
     if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
         nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
-                                             (opus_int16 *)opus->frame.data[0],
-                                             opus->frame.nb_samples, 0);
+                                             (opus_int16 *)frame->data[0],
+                                             frame->nb_samples, 0);
     else
         nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
-                                                   (float *)opus->frame.data[0],
-                                                   opus->frame.nb_samples, 0);
+                                                   (float *)frame->data[0],
+                                                   frame->nb_samples, 0);
 
     if (nb_samples < 0) {
         av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
@@ -137,9 +150,9 @@ static int libopus_decode(AVCodecContext *avc, void *frame,
         return ff_opus_error_to_averror(nb_samples);
     }
 
-    opus->frame.nb_samples = nb_samples;
-    *(AVFrame *)frame = opus->frame;
-    *got_frame_ptr = 1;
+    frame->nb_samples = nb_samples;
+    *got_frame_ptr    = 1;
+
     return pkt->size;
 }
 
@@ -152,6 +165,7 @@ static void libopus_flush(AVCodecContext *avc)
 
 AVCodec ff_libopus_decoder = {
     .name           = "libopus",
+    .long_name      = NULL_IF_CONFIG_SMALL("libopus Opus"),
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = AV_CODEC_ID_OPUS,
     .priv_data_size = sizeof(struct libopus_context),
@@ -159,8 +173,7 @@ AVCodec ff_libopus_decoder = {
     .close          = libopus_decode_close,
     .decode         = libopus_decode,
     .flush          = libopus_flush,
-    .capabilities   = CODEC_CAP_DR1,
-    .long_name      = NULL_IF_CONFIG_SMALL("libopus Opus"),
+    .capabilities   = AV_CODEC_CAP_DR1,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
                                                      AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_NONE },