FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
oggparseopus.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 
24 #include "libavutil/intreadwrite.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "oggdec.h"
28 
31  unsigned pre_skip;
32  int64_t cur_dts;
33 };
34 
35 #define OPUS_SEEK_PREROLL_MS 80
36 #define OPUS_HEAD_SIZE 19
37 
38 static int opus_header(AVFormatContext *avf, int idx)
39 {
40  struct ogg *ogg = avf->priv_data;
41  struct ogg_stream *os = &ogg->streams[idx];
42  AVStream *st = avf->streams[idx];
43  struct oggopus_private *priv = os->private;
44  uint8_t *packet = os->buf + os->pstart;
45 
46  if (!priv) {
47  priv = os->private = av_mallocz(sizeof(*priv));
48  if (!priv)
49  return AVERROR(ENOMEM);
50  }
51 
52  if (os->flags & OGG_FLAG_BOS) {
53  if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
54  return AVERROR_INVALIDDATA;
57  st->codecpar->channels = AV_RL8(packet + 9);
58 
59  priv->pre_skip = AV_RL16(packet + 10);
60  st->codecpar->initial_padding = priv->pre_skip;
61  /*orig_sample_rate = AV_RL32(packet + 12);*/
62  /*gain = AV_RL16(packet + 16);*/
63  /*channel_map = AV_RL8 (packet + 18);*/
64 
65  if (ff_alloc_extradata(st->codecpar, os->psize))
66  return AVERROR(ENOMEM);
67 
68  memcpy(st->codecpar->extradata, packet, os->psize);
69 
70  st->codecpar->sample_rate = 48000;
72  st->codecpar->sample_rate, 1000);
73  avpriv_set_pts_info(st, 64, 1, 48000);
74  priv->need_comments = 1;
75  return 1;
76  }
77 
78  if (priv->need_comments) {
79  if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
80  return AVERROR_INVALIDDATA;
81  ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8);
82  priv->need_comments--;
83  return 1;
84  }
85 
86  return 0;
87 }
88 
89 static int opus_duration(uint8_t *src, int size)
90 {
91  unsigned nb_frames = 1;
92  unsigned toc = src[0];
93  unsigned toc_config = toc >> 3;
94  unsigned toc_count = toc & 3;
95  unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
96  toc_config < 16 ? 480 << (toc_config & 1) :
97  120 << (toc_config & 3);
98  if (toc_count == 3) {
99  if (size<2)
100  return AVERROR_INVALIDDATA;
101  nb_frames = src[1] & 0x3F;
102  } else if (toc_count) {
103  nb_frames = 2;
104  }
105 
106  return frame_size * nb_frames;
107 }
108 
109 static int opus_packet(AVFormatContext *avf, int idx)
110 {
111  struct ogg *ogg = avf->priv_data;
112  struct ogg_stream *os = &ogg->streams[idx];
113  AVStream *st = avf->streams[idx];
114  struct oggopus_private *priv = os->private;
115  uint8_t *packet = os->buf + os->pstart;
116  int ret;
117 
118  if (!os->psize)
119  return AVERROR_INVALIDDATA;
120  if (os->granule > (1LL << 62)) {
121  av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os->granule);
122  return AVERROR_INVALIDDATA;
123  }
124 
125  if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
126  int seg, d;
127  int duration;
128  uint8_t *last_pkt = os->buf + os->pstart;
129  uint8_t *next_pkt = last_pkt;
130 
131  duration = 0;
132  seg = os->segp;
133  d = opus_duration(last_pkt, os->psize);
134  if (d < 0) {
136  return 0;
137  }
138  duration += d;
139  last_pkt = next_pkt = next_pkt + os->psize;
140  for (; seg < os->nsegs; seg++) {
141  next_pkt += os->segments[seg];
142  if (os->segments[seg] < 255 && next_pkt != last_pkt) {
143  int d = opus_duration(last_pkt, next_pkt - last_pkt);
144  if (d > 0)
145  duration += d;
146  last_pkt = next_pkt;
147  }
148  }
149  os->lastpts =
150  os->lastdts = os->granule - duration;
151  }
152 
153  if ((ret = opus_duration(packet, os->psize)) < 0)
154  return ret;
155 
156  os->pduration = ret;
157  if (os->lastpts != AV_NOPTS_VALUE) {
158  if (st->start_time == AV_NOPTS_VALUE)
159  st->start_time = os->lastpts;
160  priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
161  }
162 
163  priv->cur_dts += os->pduration;
164  if ((os->flags & OGG_FLAG_EOS)) {
165  int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
166  skip = FFMIN(skip, os->pduration);
167  if (skip > 0) {
168  os->pduration = skip < os->pduration ? os->pduration - skip : 1;
169  os->end_trimming = skip;
170  av_log(avf, AV_LOG_DEBUG,
171  "Last packet was truncated to %d due to end trimming.\n",
172  os->pduration);
173  }
174  }
175 
176  return 0;
177 }
178 
179 const struct ogg_codec ff_opus_codec = {
180  .name = "Opus",
181  .magic = "OpusHead",
182  .magicsize = 8,
183  .header = opus_header,
184  .packet = opus_packet,
185  .nb_header = 1,
186 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Copyright (C) 2005 Michael Ahlberg, Måns Rullgård.
Definition: oggdec.h:31
#define OPUS_SEEK_PREROLL_MS
Definition: oggparseopus.c:35
unsigned int pflags
Definition: oggdec.h:67
static int opus_duration(uint8_t *src, int size)
Definition: oggparseopus.c:89
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4504
static int opus_header(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:38
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3953
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
int flags
Definition: oggdec.h:76
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int64_t lastpts
Definition: oggdec.h:72
static int opus_packet(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:109
Format I/O context.
Definition: avformat.h:1327
unsigned int psize
Definition: oggdec.h:66
uint8_t
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1395
int64_t duration
Definition: movenc.c:63
int initial_padding
Audio only.
Definition: avcodec.h:4082
int end_trimming
set the number of packets to drop from the end
Definition: oggdec.h:87
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_RL8(x)
Definition: intreadwrite.h:398
unsigned pre_skip
Definition: oggparseopus.c:31
#define OPUS_HEAD_SIZE
Definition: oggparseopus.c:36
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st, const uint8_t *buf, int size)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define OGG_FLAG_EOS
Definition: oggdec.h:112
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3949
#define FFMAX(a, b)
Definition: common.h:94
uint8_t segments[255]
Definition: oggdec.h:80
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:3122
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define FFMIN(a, b)
Definition: common.h:96
uint64_t granule
Definition: oggdec.h:70
unsigned int pstart
Definition: oggdec.h:65
struct ogg_stream * streams
Definition: oggdec.h:102
int segp
Definition: oggdec.h:79
#define OGG_FLAG_BOS
Definition: oggdec.h:111
#define src
Definition: vp9dsp.c:530
Stream structure.
Definition: avformat.h:878
int frame_size
Definition: mxfenc.c:1822
unsigned int pduration
Definition: oggdec.h:68
int nsegs
Definition: oggdec.h:79
const int8_t * name
Definition: oggdec.h:34
const struct ogg_codec ff_opus_codec
Definition: oggparseopus.c:179
void * private
Definition: oggdec.h:90
int64_t lastdts
Definition: oggdec.h:73
int seek_preroll
Audio only.
Definition: avcodec.h:4093
int sample_rate
Audio only.
Definition: avcodec.h:4063
uint8_t * buf
Definition: oggdec.h:62
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:925
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1623
Definition: oggdec.h:101
void * priv_data
Format private data.
Definition: avformat.h:1355
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3967
int channels
Audio only.
Definition: avcodec.h:4059
AVCodecParameters * codecpar
Definition: avformat.h:1230
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242