]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopusdec.c
Remove put_no_rnd_pixels_l2 function pointer for w=16 from dsputil.
[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/avassert.h"
26 #include "libavutil/intreadwrite.h"
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     AVFrame frame;
36 };
37
38 #define OPUS_HEAD_SIZE 19
39
40 static av_cold int libopus_decode_init(AVCodecContext *avc)
41 {
42     struct libopus_context *opus = avc->priv_data;
43     int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
44     uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
45
46     avc->sample_rate    = 48000;
47     avc->sample_fmt     = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
48                           AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
49     avc->channel_layout = avc->channels > 8 ? 0 :
50                           ff_vorbis_channel_layouts[avc->channels - 1];
51
52     if (avc->extradata_size >= OPUS_HEAD_SIZE) {
53         gain_db     = sign_extend(AV_RL16(avc->extradata + 16), 16);
54         channel_map = AV_RL8 (avc->extradata + 18);
55     }
56     if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
57         nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
58         nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
59         if (nb_streams + nb_coupled != avc->channels)
60             av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
61         mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
62     } else {
63         if (avc->channels > 2 || channel_map) {
64             av_log(avc, AV_LOG_ERROR,
65                    "No channel mapping for %d channels.\n", avc->channels);
66             return AVERROR(EINVAL);
67         }
68         nb_streams = 1;
69         nb_coupled = avc->channels > 1;
70         mapping    = mapping_arr;
71     }
72
73     if (avc->channels > 2 && avc->channels <= 8) {
74         const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
75         int ch;
76
77         /* Remap channels from vorbis order to libav order */
78         for (ch = 0; ch < avc->channels; ch++)
79             mapping_arr[ch] = mapping[vorbis_offset[ch]];
80         mapping = mapping_arr;
81     }
82
83     opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
84                                                 nb_streams, nb_coupled,
85                                                 mapping, &ret);
86     if (!opus->dec) {
87         av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
88                opus_strerror(ret));
89         return ff_opus_error_to_averror(ret);
90     }
91
92     ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
93     if (ret != OPUS_OK)
94         av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
95                opus_strerror(ret));
96
97     avc->delay = 3840;  /* Decoder delay (in samples) at 48kHz */
98     avcodec_get_frame_defaults(&opus->frame);
99     avc->coded_frame = &opus->frame;
100     return 0;
101 }
102
103 static av_cold int libopus_decode_close(AVCodecContext *avc)
104 {
105     struct libopus_context *opus = avc->priv_data;
106
107     opus_multistream_decoder_destroy(opus->dec);
108     return 0;
109 }
110
111 #define MAX_FRAME_SIZE (960 * 6)
112
113 static int libopus_decode(AVCodecContext *avc, void *frame,
114                           int *got_frame_ptr, AVPacket *pkt)
115 {
116     struct libopus_context *opus = avc->priv_data;
117     int ret, nb_samples;
118
119     opus->frame.nb_samples = MAX_FRAME_SIZE;
120     ret = ff_get_buffer(avc, &opus->frame);
121     if (ret < 0) {
122         av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
123         return ret;
124     }
125
126     if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
127         nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
128                                              (opus_int16 *)opus->frame.data[0],
129                                              opus->frame.nb_samples, 0);
130     else
131         nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
132                                                    (float *)opus->frame.data[0],
133                                                    opus->frame.nb_samples, 0);
134
135     if (nb_samples < 0) {
136         av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
137                opus_strerror(nb_samples));
138         return ff_opus_error_to_averror(nb_samples);
139     }
140
141     opus->frame.nb_samples = nb_samples;
142     *(AVFrame *)frame = opus->frame;
143     *got_frame_ptr = 1;
144     return pkt->size;
145 }
146
147 static void libopus_flush(AVCodecContext *avc)
148 {
149     struct libopus_context *opus = avc->priv_data;
150
151     opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
152 }
153
154 AVCodec ff_libopus_decoder = {
155     .name           = "libopus",
156     .type           = AVMEDIA_TYPE_AUDIO,
157     .id             = AV_CODEC_ID_OPUS,
158     .priv_data_size = sizeof(struct libopus_context),
159     .init           = libopus_decode_init,
160     .close          = libopus_decode_close,
161     .decode         = libopus_decode,
162     .flush          = libopus_flush,
163     .capabilities   = CODEC_CAP_DR1,
164     .long_name      = NULL_IF_CONFIG_SMALL("libopus Opus"),
165     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
166                                                      AV_SAMPLE_FMT_S16,
167                                                      AV_SAMPLE_FMT_NONE },
168 };