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_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;
56  st->codec->channels = AV_RL8 (packet + 9);
57  priv->pre_skip = AV_RL16(packet + 10);
58  /*orig_sample_rate = AV_RL32(packet + 12);*/
59  /*gain = AV_RL16(packet + 16);*/
60  /*channel_map = AV_RL8 (packet + 18);*/
61 
62  extradata = av_malloc(os->psize + FF_INPUT_BUFFER_PADDING_SIZE);
63  if (!extradata)
64  return AVERROR(ENOMEM);
65  memcpy(extradata, packet, os->psize);
66  st->codec->extradata = extradata;
67  st->codec->extradata_size = os->psize;
68 
69  st->codec->sample_rate = 48000;
70  avpriv_set_pts_info(st, 64, 1, 48000);
71  priv->need_comments = 1;
72  return 1;
73  }
74 
75  if (priv->need_comments) {
76  if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
77  return AVERROR_INVALIDDATA;
78  ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8);
79  priv->need_comments--;
80  return 1;
81  }
82  return 0;
83 }
84 
85 static int opus_packet(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  unsigned toc, toc_config, toc_count, frame_size, nb_frames = 1;
93 
94  if (!os->psize)
95  return AVERROR_INVALIDDATA;
96  toc = *packet;
97  toc_config = toc >> 3;
98  toc_count = toc & 3;
99  frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
100  toc_config < 16 ? 480 << (toc_config & 1) :
101  120 << (toc_config & 3);
102  if (toc_count == 3) {
103  if (os->psize < 2)
104  return AVERROR_INVALIDDATA;
105  nb_frames = packet[1] & 0x3F;
106  } else if (toc_count) {
107  nb_frames = 2;
108  }
109  os->pduration = frame_size * nb_frames;
110  if (os->lastpts != AV_NOPTS_VALUE) {
111  if (st->start_time == AV_NOPTS_VALUE)
112  st->start_time = os->lastpts;
113  priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
114  }
115  priv->cur_dts += os->pduration;
116  if ((os->flags & OGG_FLAG_EOS)) {
117  int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
118  skip = FFMIN(skip, os->pduration);
119  if (skip > 0) {
120  os->pduration = skip < os->pduration ? os->pduration - skip : 1;
121  av_log(avf, AV_LOG_WARNING,
122  "Last packet must be truncated to %d (unimplemented).\n",
123  os->pduration);
124  }
125  }
126  return 0;
127 }
128 
129 const struct ogg_codec ff_opus_codec = {
130  .name = "Opus",
131  .magic = "OpusHead",
132  .magicsize = 8,
133  .header = opus_header,
134  .packet = opus_packet,
135 };
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:205
#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
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.
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
static int opus_header(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:37
#define AV_RL16
int flags
Definition: oggdec.h:76
int64_t lastpts
Definition: oggdec.h:72
static int opus_packet(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:85
Format I/O context.
Definition: avformat.h:944
unsigned int psize
Definition: oggdec.h:66
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream ** streams
Definition: avformat.h:992
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size)
#define AV_RL8(x)
Definition: intreadwrite.h:390
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
unsigned pre_skip
Definition: oggparseopus.c:31
#define OPUS_HEAD_SIZE
Definition: oggparseopus.c:35
#define OGG_FLAG_EOS
Definition: oggdec.h:109
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define FFMAX(a, b)
Definition: common.h:56
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
#define FFMIN(a, b)
Definition: common.h:58
uint64_t granule
Definition: oggdec.h:70
unsigned int pstart
Definition: oggdec.h:65
struct ogg_stream * streams
Definition: oggdec.h:99
#define OGG_FLAG_BOS
Definition: oggdec.h:108
AVDictionary * metadata
Definition: avformat.h:711
Stream structure.
Definition: avformat.h:643
enum AVMediaType codec_type
unsigned int pduration
Definition: oggdec.h:68
enum AVCodecID codec_id
const int8_t * name
Definition: oggdec.h:34
int sample_rate
samples per second
const struct ogg_codec ff_opus_codec
Definition: oggparseopus.c:129
void * private
Definition: oggdec.h:87
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:73
int64_t lastdts
Definition: oggdec.h:73
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
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:689
Definition: oggdec.h:98
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190