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/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;
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  avc->sample_rate = 48000;
48  avc->channel_layout = avc->channels > 8 ? 0 :
50 
51  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
52  gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
53  channel_map = AV_RL8 (avc->extradata + 18);
54  }
55  if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
56  nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
57  nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
58  if (nb_streams + nb_coupled != avc->channels)
59  av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
60  mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
61  } else {
62  if (avc->channels > 2 || channel_map) {
63  av_log(avc, AV_LOG_ERROR,
64  "No channel mapping for %d channels.\n", avc->channels);
65  return AVERROR(EINVAL);
66  }
67  nb_streams = 1;
68  nb_coupled = avc->channels > 1;
69  mapping = mapping_arr;
70  }
71 
72  if (avc->channels > 2 && avc->channels <= 8) {
73  const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
74  int ch;
75 
76  /* Remap channels from vorbis order to libav order */
77  for (ch = 0; ch < avc->channels; ch++)
78  mapping_arr[ch] = mapping[vorbis_offset[ch]];
79  mapping = mapping_arr;
80  }
81 
82  opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
83  nb_streams, nb_coupled,
84  mapping, &ret);
85  if (!opus->dec) {
86  av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
87  opus_strerror(ret));
88  return ff_opus_error_to_averror(ret);
89  }
90 
91  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
92  if (ret != OPUS_OK)
93  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
94  opus_strerror(ret));
95 
96  avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
97 
98  return 0;
99 }
100 
102 {
103  struct libopus_context *opus = avc->priv_data;
104 
105  opus_multistream_decoder_destroy(opus->dec);
106  return 0;
107 }
108 
109 #define MAX_FRAME_SIZE (960 * 6)
110 
111 static int libopus_decode(AVCodecContext *avc, void *data,
112  int *got_frame_ptr, AVPacket *pkt)
113 {
114  struct libopus_context *opus = avc->priv_data;
115  AVFrame *frame = data;
116  int ret, nb_samples;
117 
118  frame->nb_samples = MAX_FRAME_SIZE;
119  ret = ff_get_buffer(avc, frame, 0);
120  if (ret < 0) {
121  av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
122  return ret;
123  }
124 
125  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
126  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
127  (opus_int16 *)frame->data[0],
128  frame->nb_samples, 0);
129  else
130  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
131  (float *)frame->data[0],
132  frame->nb_samples, 0);
133 
134  if (nb_samples < 0) {
135  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
136  opus_strerror(nb_samples));
137  return ff_opus_error_to_averror(nb_samples);
138  }
139 
140  frame->nb_samples = nb_samples;
141  *got_frame_ptr = 1;
142 
143  return pkt->size;
144 }
145 
146 static void libopus_flush(AVCodecContext *avc)
147 {
148  struct libopus_context *opus = avc->priv_data;
149 
150  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
151 }
152 
154  .name = "libopus",
155  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
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  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
167 };
static int libopus_decode(AVCodecContext *avc, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: libopusdec.c:111
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int size
Definition: avcodec.h:974
#define AV_RL16
Definition: intreadwrite.h:42
AVCodec.
Definition: avcodec.h:2796
AVCodec ff_libopus_decoder
Definition: libopusdec.c:153
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
#define av_cold
Definition: attributes.h:66
#define MAX_FRAME_SIZE
Definition: libopusdec.c:109
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define AV_RL8(x)
Definition: intreadwrite.h:360
OpusMSDecoder * dec
Definition: libopusdec.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
enum AVSampleFormat request_sample_fmt
Used to request a sample format from the decoder.
Definition: avcodec.h:1873
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition: libopusdec.c:39
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int sample_rate
samples per second
Definition: avcodec.h:1791
main external API structure.
Definition: avcodec.h:1050
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
static void libopus_flush(AVCodecContext *avc)
Definition: libopusdec.c:146
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
int extradata_size
Definition: avcodec.h:1165
#define OPUS_HEAD_SIZE
Definition: libopusdec.c:37
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:127
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
signed 16 bits
Definition: samplefmt.h:64
void * priv_data
Definition: avcodec.h:1092
int channels
number of audio channels
Definition: avcodec.h:1792
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition: libopusdec.c:101
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
This structure stores compressed data.
Definition: avcodec.h:950
int delay
Codec delay.
Definition: avcodec.h:1212
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179