Libav
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 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 
34  OpusMSDecoder *dec;
35 };
36 
37 #define OPUS_HEAD_SIZE 19
38 
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) {
47  "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
48  avc->channels = 2;
49  }
50 
51  avc->sample_rate = 48000;
54  avc->channel_layout = avc->channels > 8 ? 0 :
56 
57  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
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 libav 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  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
98  if (ret != OPUS_OK)
99  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
100  opus_strerror(ret));
101 
102  avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
103 
104  return 0;
105 }
106 
108 {
109  struct libopus_context *opus = avc->priv_data;
110 
111  opus_multistream_decoder_destroy(opus->dec);
112  return 0;
113 }
114 
115 #define MAX_FRAME_SIZE (960 * 6)
116 
117 static int libopus_decode(AVCodecContext *avc, void *data,
118  int *got_frame_ptr, AVPacket *pkt)
119 {
120  struct libopus_context *opus = avc->priv_data;
121  AVFrame *frame = data;
122  int ret, nb_samples;
123 
124  frame->nb_samples = MAX_FRAME_SIZE;
125  ret = ff_get_buffer(avc, frame, 0);
126  if (ret < 0) {
127  av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
128  return ret;
129  }
130 
131  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
132  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
133  (opus_int16 *)frame->data[0],
134  frame->nb_samples, 0);
135  else
136  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
137  (float *)frame->data[0],
138  frame->nb_samples, 0);
139 
140  if (nb_samples < 0) {
141  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
142  opus_strerror(nb_samples));
143  return ff_opus_error_to_averror(nb_samples);
144  }
145 
146  frame->nb_samples = nb_samples;
147  *got_frame_ptr = 1;
148 
149  return pkt->size;
150 }
151 
152 static void libopus_flush(AVCodecContext *avc)
153 {
154  struct libopus_context *opus = avc->priv_data;
155 
156  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
157 }
158 
160  .name = "libopus",
161  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
162  .type = AVMEDIA_TYPE_AUDIO,
163  .id = AV_CODEC_ID_OPUS,
164  .priv_data_size = sizeof(struct libopus_context),
165  .init = libopus_decode_init,
166  .close = libopus_decode_close,
167  .decode = libopus_decode,
168  .flush = libopus_flush,
169  .capabilities = AV_CODEC_CAP_DR1,
170  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
173 };
static int libopus_decode(AVCodecContext *avc, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: libopusdec.c:117
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AV_RL16
Definition: intreadwrite.h:42
AVCodec.
Definition: avcodec.h:3120
AVCodec ff_libopus_decoder
Definition: libopusdec.c:159
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
#define MAX_FRAME_SIZE
Definition: libopusdec.c:115
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
#define AV_RL8(x)
Definition: intreadwrite.h:371
OpusMSDecoder * dec
Definition: libopusdec.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
enum AVSampleFormat request_sample_fmt
Used to request a sample format from the decoder.
Definition: avcodec.h:2224
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2203
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition: libopusdec.c:39
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
int sample_rate
samples per second
Definition: avcodec.h:2152
main external API structure.
Definition: avcodec.h:1409
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
static void libopus_flush(AVCodecContext *avc)
Definition: libopusdec.c:152
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
int extradata_size
Definition: avcodec.h:1524
#define OPUS_HEAD_SIZE
Definition: libopusdec.c:37
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:118
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
common internal api header.
signed 16 bits
Definition: samplefmt.h:63
void * priv_data
Definition: avcodec.h:1451
int channels
number of audio channels
Definition: avcodec.h:2153
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition: libopusdec.c:107
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
This structure stores compressed data.
Definition: avcodec.h:1323
int delay
Codec delay.
Definition: avcodec.h:1563
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838