]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopusdec.c
mpeg12dec: avoid signed overflow in bitrate calculation
[ffmpeg] / libavcodec / libopusdec.c
1 /*
2  * Opus decoder using libopus
3  * Copyright (c) 2012 Nicolas George
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <opus.h>
23 #include <opus_multistream.h>
24
25 #include "libavutil/intreadwrite.h"
26
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "vorbis.h"
30 #include "mathops.h"
31 #include "libopus.h"
32
33 struct libopus_context {
34     OpusMSDecoder *dec;
35 };
36
37 #define OPUS_HEAD_SIZE 19
38
39 static av_cold int libopus_decode_init(AVCodecContext *avc)
40 {
41     struct libopus_context *opus = avc->priv_data;
42     int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
43     uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
44
45     if (avc->channels <= 0) {
46         av_log(avc, AV_LOG_WARNING,
47                "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
48         avc->channels = 2;
49     }
50
51     avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
52     if (avc->channels <= 0) {
53         av_log(avc, AV_LOG_WARNING,
54                "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
55         avc->channels = 2;
56     }
57
58     avc->sample_rate    = 48000;
59     avc->sample_fmt     = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
60                           AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
61     avc->channel_layout = avc->channels > 8 ? 0 :
62                           ff_vorbis_channel_layouts[avc->channels - 1];
63
64     if (avc->extradata_size >= OPUS_HEAD_SIZE) {
65         gain_db     = sign_extend(AV_RL16(avc->extradata + 16), 16);
66         channel_map = AV_RL8 (avc->extradata + 18);
67     }
68     if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
69         nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
70         nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
71         if (nb_streams + nb_coupled != avc->channels)
72             av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
73         mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
74     } else {
75         if (avc->channels > 2 || channel_map) {
76             av_log(avc, AV_LOG_ERROR,
77                    "No channel mapping for %d channels.\n", avc->channels);
78             return AVERROR(EINVAL);
79         }
80         nb_streams = 1;
81         nb_coupled = avc->channels > 1;
82         mapping    = mapping_arr;
83     }
84
85     if (avc->channels > 2 && avc->channels <= 8) {
86         const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
87         int ch;
88
89         /* Remap channels from Vorbis order to libav order */
90         for (ch = 0; ch < avc->channels; ch++)
91             mapping_arr[ch] = mapping[vorbis_offset[ch]];
92         mapping = mapping_arr;
93     }
94
95     opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
96                                                 nb_streams, nb_coupled,
97                                                 mapping, &ret);
98     if (!opus->dec) {
99         av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
100                opus_strerror(ret));
101         return ff_opus_error_to_averror(ret);
102     }
103
104     ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
105     if (ret != OPUS_OK)
106         av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
107                opus_strerror(ret));
108
109     avc->delay = 3840;  /* Decoder delay (in samples) at 48kHz */
110
111     return 0;
112 }
113
114 static av_cold int libopus_decode_close(AVCodecContext *avc)
115 {
116     struct libopus_context *opus = avc->priv_data;
117
118     opus_multistream_decoder_destroy(opus->dec);
119     return 0;
120 }
121
122 #define MAX_FRAME_SIZE (960 * 6)
123
124 static int libopus_decode(AVCodecContext *avc, void *data,
125                           int *got_frame_ptr, AVPacket *pkt)
126 {
127     struct libopus_context *opus = avc->priv_data;
128     AVFrame *frame               = data;
129     int ret, nb_samples;
130
131     frame->nb_samples = MAX_FRAME_SIZE;
132     ret = ff_get_buffer(avc, frame, 0);
133     if (ret < 0) {
134         av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
135         return ret;
136     }
137
138     if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
139         nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
140                                              (opus_int16 *)frame->data[0],
141                                              frame->nb_samples, 0);
142     else
143         nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
144                                                    (float *)frame->data[0],
145                                                    frame->nb_samples, 0);
146
147     if (nb_samples < 0) {
148         av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
149                opus_strerror(nb_samples));
150         return ff_opus_error_to_averror(nb_samples);
151     }
152
153     frame->nb_samples = nb_samples;
154     *got_frame_ptr    = 1;
155
156     return pkt->size;
157 }
158
159 static void libopus_flush(AVCodecContext *avc)
160 {
161     struct libopus_context *opus = avc->priv_data;
162
163     opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
164 }
165
166 AVCodec ff_libopus_decoder = {
167     .name           = "libopus",
168     .long_name      = NULL_IF_CONFIG_SMALL("libopus Opus"),
169     .type           = AVMEDIA_TYPE_AUDIO,
170     .id             = AV_CODEC_ID_OPUS,
171     .priv_data_size = sizeof(struct libopus_context),
172     .init           = libopus_decode_init,
173     .close          = libopus_decode_close,
174     .decode         = libopus_decode,
175     .flush          = libopus_flush,
176     .capabilities   = AV_CODEC_CAP_DR1,
177     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
178                                                      AV_SAMPLE_FMT_S16,
179                                                      AV_SAMPLE_FMT_NONE },
180 };