Libav
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 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 <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_HEAD_SIZE 19
36 
37 static int opus_header(AVFormatContext *avf, int idx)
38 {
39  struct ogg *ogg = avf->priv_data;
40  struct ogg_stream *os = &ogg->streams[idx];
41  AVStream *st = avf->streams[idx];
42  struct oggopus_private *priv = os->private;
43  uint8_t *packet = os->buf + os->pstart;
44  uint8_t *extradata;
45 
46  if (!priv) {
47  priv = os->private = av_mallocz(sizeof(*priv));
48  if (!priv)
49  return AVERROR(ENOMEM);
50  }
51  if (os->flags & OGG_FLAG_BOS) {
52  if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
53  return AVERROR_INVALIDDATA;
54 
57  st->codecpar->channels = AV_RL8(packet + 9);
58  priv->pre_skip = AV_RL16(packet + 10);
59 
60  st->codecpar->initial_padding = priv->pre_skip;
61 
62  extradata = av_malloc(os->psize + AV_INPUT_BUFFER_PADDING_SIZE);
63  if (!extradata)
64  return AVERROR(ENOMEM);
65 
66  memcpy(extradata, packet, os->psize);
67  st->codecpar->extradata = extradata;
68  st->codecpar->extradata_size = os->psize;
69 
70  st->codecpar->sample_rate = 48000;
71  avpriv_set_pts_info(st, 64, 1, 48000);
72  priv->need_comments = 1;
73  return 1;
74  }
75 
76  if (priv->need_comments) {
77  if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
78  return AVERROR_INVALIDDATA;
79  ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8);
80  priv->need_comments--;
81  return 1;
82  }
83 
84  return 0;
85 }
86 
87 static int opus_packet(AVFormatContext *avf, int idx)
88 {
89  struct ogg *ogg = avf->priv_data;
90  struct ogg_stream *os = &ogg->streams[idx];
91  AVStream *st = avf->streams[idx];
92  struct oggopus_private *priv = os->private;
93  uint8_t *packet = os->buf + os->pstart;
94  unsigned toc, toc_config, toc_count, frame_size, nb_frames = 1;
95 
96  if (!os->psize)
97  return AVERROR_INVALIDDATA;
98 
99  toc = *packet;
100  toc_config = toc >> 3;
101  toc_count = toc & 3;
102  frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
103  toc_config < 16 ? 480 << (toc_config & 1) :
104  120 << (toc_config & 3);
105  if (toc_count == 3) {
106  if (os->psize < 2)
107  return AVERROR_INVALIDDATA;
108  nb_frames = packet[1] & 0x3F;
109  } else if (toc_count) {
110  nb_frames = 2;
111  }
112 
113  os->pduration = frame_size * nb_frames;
114  if (os->lastpts != AV_NOPTS_VALUE) {
115  if (st->start_time == AV_NOPTS_VALUE)
116  st->start_time = os->lastpts;
117  priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
118  }
119 
120  priv->cur_dts += os->pduration;
121  if ((os->flags & OGG_FLAG_EOS)) {
122  int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
123  skip = FFMIN(skip, os->pduration);
124  if (skip > 0) {
125  os->pduration = skip < os->pduration ? os->pduration - skip : 1;
126  av_log(avf, AV_LOG_WARNING,
127  "Last packet is truncated to %d (because of unimplemented end trim support).\n",
128  os->pduration);
129  return AVERROR_PATCHWELCOME;
130  }
131  }
132 
133  return 0;
134 }
135 
136 const struct ogg_codec ff_opus_codec = {
137  .name = "Opus",
138  .magic = "OpusHead",
139  .magicsize = 8,
140  .header = opus_header,
141  .packet = opus_packet,
142  .granule_is_start = 1,
143  .nb_header = 1,
144 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Copyright (C) 2005 Michael Ahlberg, Måns Rullgård.
Definition: oggdec.h:31
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
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:2986
static const uint8_t frame_size[4]
Definition: g723_1.h:219
static int opus_header(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:37
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
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
int flags
Definition: oggdec.h:76
int64_t lastpts
Definition: oggdec.h:72
static int opus_packet(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:87
Format I/O context.
Definition: avformat.h:940
unsigned int psize
Definition: oggdec.h:66
uint8_t
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int initial_padding
Audio only.
Definition: avcodec.h:3579
#define AV_RL8(x)
Definition: intreadwrite.h:371
unsigned pre_skip
Definition: oggparseopus.c:31
#define OPUS_HEAD_SIZE
Definition: oggparseopus.c:35
#define AVERROR(e)
Definition: error.h:43
int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st, const uint8_t *buf, int size)
#define OGG_FLAG_EOS
Definition: oggdec.h:106
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
#define FFMAX(a, b)
Definition: common.h:64
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
#define FFMIN(a, b)
Definition: common.h:66
uint64_t granule
Definition: oggdec.h:70
unsigned int pstart
Definition: oggdec.h:65
struct ogg_stream * streams
Definition: oggdec.h:97
#define OGG_FLAG_BOS
Definition: oggdec.h:105
Stream structure.
Definition: avformat.h:705
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
unsigned int pduration
Definition: oggdec.h:68
const int8_t * name
Definition: oggdec.h:34
const struct ogg_codec ff_opus_codec
Definition: oggparseopus.c:136
void * private
Definition: oggdec.h:85
int64_t lastdts
Definition: oggdec.h:73
int sample_rate
Audio only.
Definition: avcodec.h:3564
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 stream time base.
Definition: avformat.h:750
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
Definition: oggdec.h:96
void * priv_data
Format private data.
Definition: avformat.h:968
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int channels
Audio only.
Definition: avcodec.h:3560
AVCodecParameters * codecpar
Definition: avformat.h:831
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235