3 * Copyright (c) 2012 Nicolas George
5 * This file is part of Libav.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
29 struct oggopus_private {
35 #define OPUS_HEAD_SIZE 19
37 static int opus_header(AVFormatContext *avf, int idx)
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;
47 priv = os->private = av_mallocz(sizeof(*priv));
49 return AVERROR(ENOMEM);
51 if (os->flags & OGG_FLAG_BOS) {
52 if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
53 return AVERROR_INVALIDDATA;
55 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
56 st->codec->codec_id = AV_CODEC_ID_OPUS;
57 st->codec->channels = AV_RL8(packet + 9);
58 priv->pre_skip = AV_RL16(packet + 10);
60 extradata = av_malloc(os->psize + FF_INPUT_BUFFER_PADDING_SIZE);
62 return AVERROR(ENOMEM);
64 memcpy(extradata, packet, os->psize);
65 st->codec->extradata = extradata;
66 st->codec->extradata_size = os->psize;
68 st->codec->sample_rate = 48000;
69 avpriv_set_pts_info(st, 64, 1, 48000);
70 priv->need_comments = 1;
74 if (priv->need_comments) {
75 if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
76 return AVERROR_INVALIDDATA;
77 ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8);
78 priv->need_comments--;
85 static int opus_packet(AVFormatContext *avf, int idx)
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;
95 return AVERROR_INVALIDDATA;
98 toc_config = toc >> 3;
100 frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
101 toc_config < 16 ? 480 << (toc_config & 1) :
102 120 << (toc_config & 3);
103 if (toc_count == 3) {
105 return AVERROR_INVALIDDATA;
106 nb_frames = packet[1] & 0x3F;
107 } else if (toc_count) {
111 os->pduration = frame_size * nb_frames;
112 if (os->lastpts != AV_NOPTS_VALUE) {
113 if (st->start_time == AV_NOPTS_VALUE)
114 st->start_time = os->lastpts;
115 priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
118 priv->cur_dts += os->pduration;
119 if ((os->flags & OGG_FLAG_EOS)) {
120 int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
121 skip = FFMIN(skip, os->pduration);
123 os->pduration = skip < os->pduration ? os->pduration - skip : 1;
124 av_log(avf, AV_LOG_WARNING,
125 "Last packet is truncated to %d (because of unimplemented end trim support).\n",
127 return AVERROR_PATCHWELCOME;
134 const struct ogg_codec ff_opus_codec = {
138 .header = opus_header,
139 .packet = opus_packet,
140 .granule_is_start = 1,