FFmpeg
Loading...
Searching...
No Matches
opus.c
Go to the documentation of this file.
1/*
2 * Opus parser for Ogg
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 <string.h>
23
25#include "libavutil/mem.h"
30
36
37#define OPUS_SEEK_PREROLL_MS 80
38#define OPUS_HEAD_SIZE 19
39
40static int parse_opus_header(AVFormatContext *avf, AVStream *st, struct ogg_stream *os,
41 struct oggopus_private *priv, uint8_t *packet,
42 size_t psize)
43{
44 int channels;
45 int ret;
46
47 if (psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
49
52
53 channels = AV_RL8(packet + 9);
56 av_log(avf, AV_LOG_ERROR, "Channel change is not supported\n");
58 }
59
61
62 priv->pre_skip = AV_RL16(packet + 10);
64 os->start_trimming = priv->pre_skip;
65 /*orig_sample_rate = AV_RL32(packet + 12);*/
66 /*gain = AV_RL16(packet + 16);*/
67 /*channel_map = AV_RL8 (packet + 18);*/
68
70 if (ret < 0)
71 return ret;
72
73 memcpy(st->codecpar->extradata, packet, os->psize);
74
75 st->codecpar->sample_rate = 48000;
77 st->codecpar->sample_rate, 1000);
78 avpriv_set_pts_info(st, 64, 1, 48000);
79
80 priv->need_comments = 1;
81
82 return 1;
83}
84
85static int opus_header(AVFormatContext *avf, int idx)
86{
87 struct ogg *ogg = avf->priv_data;
88 struct ogg_stream *os = &ogg->streams[idx];
89 AVStream *st = avf->streams[idx];
90 struct oggopus_private *priv = os->private;
91 uint8_t *packet = os->buf + os->pstart;
92
93 if (!priv) {
94 priv = os->private = av_mallocz(sizeof(*priv));
95 if (!priv)
96 return AVERROR(ENOMEM);
97 }
98
99 if (os->flags & OGG_FLAG_BOS)
100 return parse_opus_header(avf, st, os, priv, packet, os->psize);
101
102 if (priv->need_comments) {
103 if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
104 return AVERROR_INVALIDDATA;
105 ff_vorbis_comment_read_stream(avf, st, packet + 8, os->psize - 8);
106 priv->need_comments--;
107 return 1;
108 }
109
110 return 0;
111}
112
113static int opus_duration(uint8_t *src, int size)
114{
115 unsigned nb_frames = 1;
116 unsigned toc = src[0];
117 unsigned toc_config = toc >> 3;
118 unsigned toc_count = toc & 3;
119 unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
120 toc_config < 16 ? 480 << (toc_config & 1) :
121 120 << (toc_config & 3);
122 if (toc_count == 3) {
123 if (size<2)
124 return AVERROR_INVALIDDATA;
125 nb_frames = src[1] & 0x3F;
126 } else if (toc_count) {
127 nb_frames = 2;
128 }
129
130 return frame_size * nb_frames;
131}
132
133static int opus_packet(AVFormatContext *avf, int idx)
134{
135 struct ogg *ogg = avf->priv_data;
136 struct ogg_stream *os = &ogg->streams[idx];
137 AVStream *st = avf->streams[idx];
138 struct oggopus_private *priv = os->private;
139 uint8_t *packet = os->buf + os->pstart;
140 int ret;
141
142 if (!os->psize)
143 return AVERROR_INVALIDDATA;
144 if (os->granule > (1LL << 62)) {
145 av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os->granule);
146 return AVERROR_INVALIDDATA;
147 }
148
149 if (os->psize > 8 && !memcmp(packet, "OpusHead", 8)) {
150 ret = parse_opus_header(avf, st, os, priv, packet, os->psize);
151 if (ret < 0)
152 return ret;
153
154 /* A later link's header: the decoder is still set up for the previous one. */
156 os->new_extradata = av_memdup(packet, os->psize);
157 if (!os->new_extradata)
158 return AVERROR(ENOMEM);
159 os->new_extradata_size = os->psize;
160
161 return 1;
162 }
163
164 if (os->psize > 8 && !memcmp(packet, "OpusTags", 8)) {
165 ret = ff_vorbis_update_metadata(avf, st, os->buf + os->pstart + 8,
166 os->psize - 8);
167 if (ret < 0)
168 return ret;
169
170 priv->need_comments = 0;
171 return 1;
172 }
173
174 if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
175 int seg, d;
176 int duration;
177 uint8_t *last_pkt = os->buf + os->pstart;
178 uint8_t *next_pkt = last_pkt;
179
180 duration = 0;
181 seg = os->segp;
182 d = opus_duration(last_pkt, os->psize);
183 if (d < 0) {
185 return 0;
186 }
187 duration += d;
188 last_pkt = next_pkt = next_pkt + os->psize;
189 for (; seg < os->nsegs; seg++) {
190 next_pkt += os->segments[seg];
191 if (os->segments[seg] < 255 && next_pkt != last_pkt) {
192 d = opus_duration(last_pkt, next_pkt - last_pkt);
193 if (d > 0)
194 duration += d;
195 last_pkt = next_pkt;
196 }
197 }
198 os->lastpts =
199 os->lastdts = os->granule - duration;
200 }
201
202 if ((ret = opus_duration(packet, os->psize)) < 0)
203 return ret;
204
205 os->pduration = ret;
206 if (os->lastpts != AV_NOPTS_VALUE) {
207 if (st->start_time == AV_NOPTS_VALUE)
208 st->start_time = os->lastpts;
209 priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
210 }
211
212 priv->cur_dts += os->pduration;
213 if ((os->flags & OGG_FLAG_EOS)) {
214 int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
215 skip = FFMIN(skip, os->pduration);
216 if (skip > 0) {
217 os->pduration = skip < os->pduration ? os->pduration - skip : 1;
218 os->end_trimming = skip;
219 av_log(avf, AV_LOG_DEBUG,
220 "Last packet was truncated to %d due to end trimming.\n",
221 os->pduration);
222 }
223 if (os->segp == os->nsegs)
224 os->pflags |= AV_PKT_FLAG_EOS;
225 }
226
227 return 0;
228}
229
230const struct ogg_codec ff_opus_codec = {
231 .name = "Opus",
232 .magic = "OpusHead",
233 .magicsize = 8,
234 .header = opus_header,
235 .packet = opus_packet,
236 .nb_header = 1,
237};
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
int ff_vorbis_comment_read_stream(AVFormatContext *as, AVStream *st, const uint8_t *buf, int size)
Parse Vorbis comments and add metadata to an AVStream.
Definition reader.c:73
long long int64_t
Definition coverity.c:34
static int64_t duration
Definition ffplay.c:359
int ret
Definition ffplay.c:391
static const uint8_t frame_size[4]
Definition g723_1.h:222
@ AV_CODEC_ID_OPUS
Definition codec_id.h:519
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:658
#define AV_PKT_FLAG_EOS
The packet ends a sequence: the next packet, if any, does not continue its decoder state.
Definition packet.h:685
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_RL8(x)
#define AV_RL16(p)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define OPUS_HEAD_SIZE
Definition libopusdec.c:51
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
int ff_vorbis_update_metadata(AVFormatContext *s, AVStream *st, const uint8_t *buf, int size)
Parse Vorbis comments, add metadata to an AVStream.
Definition vorbis.c:125
const struct ogg_codec ff_opus_codec
Definition opus.c:230
#define OGG_FLAG_BOS
Definition mapping.h:122
#define OGG_FLAG_EOS
Definition mapping.h:123
Memory handling functions.
static int parse_opus_header(AVFormatContext *avf, AVStream *st, struct ogg_stream *os, struct oggopus_private *priv, uint8_t *packet, size_t psize)
Definition opus.c:40
static int opus_packet(AVFormatContext *avf, int idx)
Definition opus.c:133
static int opus_duration(uint8_t *src, int size)
Definition opus.c:113
#define OPUS_SEEK_PREROLL_MS
Definition opus.c:37
static int opus_header(AVFormatContext *avf, int idx)
Definition opus.c:85
int nb_channels
Number of channels in this layout.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int seek_preroll
Number of audio samples to skip after a discontinuity.
Definition codec_par.h:256
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
int initial_padding
Number of padding audio samples at the start.
Definition codec_par.h:239
Format I/O context.
Definition avformat.h:1401
void * priv_data
Format private data.
Definition avformat.h:1429
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1469
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:817
Copyright (C) 2005 Michael Ahlberg, Måns Rullgård.
Definition mapping.h:30
unsigned int pduration
Definition mapping.h:74
uint8_t * new_extradata
Definition mapping.h:99
size_t new_extradata_size
Definition mapping.h:100
int end_trimming
set the number of packets to drop from the end
Definition mapping.h:95
unsigned int pflags
Definition mapping.h:73
int nsegs
Definition mapping.h:85
int start_trimming
set the number of packets to drop from the start
Definition mapping.h:94
uint8_t segments[255]
Definition mapping.h:86
uint64_t granule
Definition mapping.h:76
int64_t lastdts
Definition mapping.h:79
unsigned int pstart
Definition mapping.h:71
unsigned int psize
Definition mapping.h:72
void * private
Definition mapping.h:101
uint8_t * buf
Definition mapping.h:68
int64_t lastpts
Definition mapping.h:78
int segp
Definition mapping.h:85
int flags
Definition mapping.h:82
Definition mapping.h:112
struct ogg_stream * streams
Definition mapping.h:113
int64_t cur_dts
Definition opus.c:34
unsigned pre_skip
Definition opus.c:33
int need_comments
Definition opus.c:32
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
int size