FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libopusdec.c
Go to the documentation of this file.
1 /*
2  * Opus decoder using libopus
3  * Copyright (c) 2012 Nicolas George
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
34  OpusMSDecoder *dec;
36  int pre_skip;
37 #ifndef OPUS_SET_GAIN
38  union { int i; double d; } gain;
39 #endif
40 };
41 
42 #define OPUS_HEAD_SIZE 19
43 
45 {
46  struct libopus_context *opus = avc->priv_data;
47  int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
48  uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
49 
50  avc->sample_rate = 48000;
53  avc->channel_layout = avc->channels > 8 ? 0 :
55 
56  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
57  opus->pre_skip = AV_RL16(avc->extradata + 10);
58  gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
59  channel_map = AV_RL8 (avc->extradata + 18);
60  }
61  if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
62  nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
63  nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
64  if (nb_streams + nb_coupled != avc->channels)
65  av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
66  mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
67  } else {
68  if (avc->channels > 2 || channel_map) {
69  av_log(avc, AV_LOG_ERROR,
70  "No channel mapping for %d channels.\n", avc->channels);
71  return AVERROR(EINVAL);
72  }
73  nb_streams = 1;
74  nb_coupled = avc->channels > 1;
75  mapping = mapping_arr;
76  }
77 
78  if (avc->channels > 2 && avc->channels <= 8) {
79  const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
80  int ch;
81 
82  /* Remap channels from vorbis order to ffmpeg order */
83  for (ch = 0; ch < avc->channels; ch++)
84  mapping_arr[ch] = mapping[vorbis_offset[ch]];
85  mapping = mapping_arr;
86  }
87 
88  opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
89  nb_streams, nb_coupled,
90  mapping, &ret);
91  if (!opus->dec) {
92  av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
93  opus_strerror(ret));
94  return ff_opus_error_to_averror(ret);
95  }
96 
97 #ifdef OPUS_SET_GAIN
98  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
99  if (ret != OPUS_OK)
100  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
101  opus_strerror(ret));
102 #else
103  {
104  double gain_lin = pow(10, gain_db / (20.0 * 256));
105  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
106  opus->gain.d = gain_lin;
107  else
108  opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
109  }
110 #endif
111 
112  avc->internal->skip_samples = opus->pre_skip;
113  avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
115  avc->coded_frame = &opus->frame;
116  return 0;
117 }
118 
120 {
121  struct libopus_context *opus = avc->priv_data;
122 
123  opus_multistream_decoder_destroy(opus->dec);
124  return 0;
125 }
126 
127 #define MAX_FRAME_SIZE (960 * 6)
128 
129 static int libopus_decode(AVCodecContext *avc, void *frame,
130  int *got_frame_ptr, AVPacket *pkt)
131 {
132  struct libopus_context *opus = avc->priv_data;
133  int ret, nb_samples;
134 
136  ret = ff_get_buffer(avc, &opus->frame);
137  if (ret < 0) {
138  av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
139  return ret;
140  }
141 
142  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
143  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
144  (opus_int16 *)opus->frame.data[0],
145  opus->frame.nb_samples, 0);
146  else
147  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
148  (float *)opus->frame.data[0],
149  opus->frame.nb_samples, 0);
150 
151  if (nb_samples < 0) {
152  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
153  opus_strerror(nb_samples));
154  return ff_opus_error_to_averror(nb_samples);
155  }
156 
157 #ifndef OPUS_SET_GAIN
158  {
159  int i = avc->channels * nb_samples;
160  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
161  float *pcm = (float *)opus->frame.data[0];
162  for (; i > 0; i--, pcm++)
163  *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
164  } else {
165  int16_t *pcm = (int16_t *)opus->frame.data[0];
166  for (; i > 0; i--, pcm++)
167  *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
168  }
169  }
170 #endif
171 
172  opus->frame.nb_samples = nb_samples;
173  *(AVFrame *)frame = opus->frame;
174  *got_frame_ptr = 1;
175  return pkt->size;
176 }
177 
178 static void libopus_flush(AVCodecContext *avc)
179 {
180  struct libopus_context *opus = avc->priv_data;
181 
182  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
183  /* The stream can have been extracted by a tool that is not Opus-aware.
184  Therefore, any packet can become the first of the stream. */
185  avc->internal->skip_samples = opus->pre_skip;
186 }
187 
189  .name = "libopus",
190  .type = AVMEDIA_TYPE_AUDIO,
191  .id = AV_CODEC_ID_OPUS,
192  .priv_data_size = sizeof(struct libopus_context),
193  .init = libopus_decode_init,
194  .close = libopus_decode_close,
195  .decode = libopus_decode,
196  .flush = libopus_flush,
197  .capabilities = CODEC_CAP_DR1,
198  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
199  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
202 };