3 * Copyright (c) 2012 Nicolas George
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
30 struct oggopus_private
{
36 #define OPUS_SEEK_PREROLL_MS 80
37 #define OPUS_HEAD_SIZE 19
39 static int parse_opus_header(AVFormatContext
*avf
, AVStream
*st
, struct ogg_stream
*os
,
40 struct oggopus_private
*priv
, uint8_t *packet
,
46 if (psize
< OPUS_HEAD_SIZE
|| (AV_RL8(packet
+ 8) & 0xF0) != 0)
47 return AVERROR_INVALIDDATA
;
49 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
50 st
->codecpar
->codec_id
= AV_CODEC_ID_OPUS
;
52 channels
= AV_RL8(packet
+ 9);
53 if (st
->codecpar
->ch_layout
.nb_channels
&&
54 channels
!= st
->codecpar
->ch_layout
.nb_channels
) {
55 av_log(avf
, AV_LOG_ERROR
, "Channel change is not supported\n");
56 return AVERROR_PATCHWELCOME
;
59 st
->codecpar
->ch_layout
.nb_channels
= channels
;
61 priv
->pre_skip
= AV_RL16(packet
+ 10);
62 st
->codecpar
->initial_padding
= priv
->pre_skip
;
63 os
->start_trimming
= priv
->pre_skip
;
64 /*orig_sample_rate = AV_RL32(packet + 12);*/
65 /*gain = AV_RL16(packet + 16);*/
66 /*channel_map = AV_RL8 (packet + 18);*/
68 ret
= ff_alloc_extradata(st
->codecpar
, os
->psize
);
72 memcpy(st
->codecpar
->extradata
, packet
, os
->psize
);
74 st
->codecpar
->sample_rate
= 48000;
75 st
->codecpar
->seek_preroll
= av_rescale(OPUS_SEEK_PREROLL_MS
,
76 st
->codecpar
->sample_rate
, 1000);
77 avpriv_set_pts_info(st
, 64, 1, 48000);
79 priv
->need_comments
= 1;
84 static int opus_header(AVFormatContext
*avf
, int idx
)
86 struct ogg
*ogg
= avf
->priv_data
;
87 struct ogg_stream
*os
= &ogg
->streams
[idx
];
88 AVStream
*st
= avf
->streams
[idx
];
89 struct oggopus_private
*priv
= os
->private;
90 uint8_t *packet
= os
->buf
+ os
->pstart
;
93 priv
= os
->private = av_mallocz(sizeof(*priv
));
95 return AVERROR(ENOMEM
);
98 if (os
->flags
& OGG_FLAG_BOS
)
99 return parse_opus_header(avf
, st
, os
, priv
, packet
, os
->psize
);
101 if (priv
->need_comments
) {
102 if (os
->psize
< 8 || memcmp(packet
, "OpusTags", 8))
103 return AVERROR_INVALIDDATA
;
104 ff_vorbis_stream_comment(avf
, st
, packet
+ 8, os
->psize
- 8);
105 priv
->need_comments
--;
112 static int opus_duration(uint8_t *src
, int size
)
114 unsigned nb_frames
= 1;
115 unsigned toc
= src
[0];
116 unsigned toc_config
= toc
>> 3;
117 unsigned toc_count
= toc
& 3;
118 unsigned frame_size
= toc_config
< 12 ? FFMAX(480, 960 * (toc_config
& 3)) :
119 toc_config
< 16 ? 480 << (toc_config
& 1) :
120 120 << (toc_config
& 3);
121 if (toc_count
== 3) {
123 return AVERROR_INVALIDDATA
;
124 nb_frames
= src
[1] & 0x3F;
125 } else if (toc_count
) {
129 return frame_size
* nb_frames
;
132 static int opus_packet(AVFormatContext
*avf
, int idx
)
134 struct ogg
*ogg
= avf
->priv_data
;
135 struct ogg_stream
*os
= &ogg
->streams
[idx
];
136 AVStream
*st
= avf
->streams
[idx
];
137 struct oggopus_private
*priv
= os
->private;
138 uint8_t *packet
= os
->buf
+ os
->pstart
;
142 return AVERROR_INVALIDDATA
;
143 if (os
->granule
> (1LL << 62)) {
144 av_log(avf
, AV_LOG_ERROR
, "Unsupported huge granule pos %"PRId64
"\n", os
->granule
);
145 return AVERROR_INVALIDDATA
;
148 if (os
->psize
> 8 && !memcmp(packet
, "OpusHead", 8)) {
149 ret
= parse_opus_header(avf
, st
, os
, priv
, packet
, os
->psize
);
156 if (os
->psize
> 8 && !memcmp(packet
, "OpusTags", 8)) {
157 ret
= ff_vorbis_update_metadata(avf
, st
, os
->buf
+ os
->pstart
+ 8,
162 priv
->need_comments
= 0;
166 if ((!os
->lastpts
|| os
->lastpts
== AV_NOPTS_VALUE
) && !(os
->flags
& OGG_FLAG_EOS
)) {
169 uint8_t *last_pkt
= os
->buf
+ os
->pstart
;
170 uint8_t *next_pkt
= last_pkt
;
174 d
= opus_duration(last_pkt
, os
->psize
);
176 os
->pflags
|= AV_PKT_FLAG_CORRUPT
;
180 last_pkt
= next_pkt
= next_pkt
+ os
->psize
;
181 for (; seg
< os
->nsegs
; seg
++) {
182 next_pkt
+= os
->segments
[seg
];
183 if (os
->segments
[seg
] < 255 && next_pkt
!= last_pkt
) {
184 int d
= opus_duration(last_pkt
, next_pkt
- last_pkt
);
191 os
->lastdts
= os
->granule
- duration
;
194 if ((ret
= opus_duration(packet
, os
->psize
)) < 0)
198 if (os
->lastpts
!= AV_NOPTS_VALUE
) {
199 if (st
->start_time
== AV_NOPTS_VALUE
)
200 st
->start_time
= os
->lastpts
;
201 priv
->cur_dts
= os
->lastdts
= os
->lastpts
-= priv
->pre_skip
;
204 priv
->cur_dts
+= os
->pduration
;
205 if ((os
->flags
& OGG_FLAG_EOS
)) {
206 int64_t skip
= priv
->cur_dts
- os
->granule
+ priv
->pre_skip
;
207 skip
= FFMIN(skip
, os
->pduration
);
209 os
->pduration
= skip
< os
->pduration
? os
->pduration
- skip
: 1;
210 os
->end_trimming
= skip
;
211 av_log(avf
, AV_LOG_DEBUG
,
212 "Last packet was truncated to %d due to end trimming.\n",
220 const struct ogg_codec ff_opus_codec
= {
224 .header
= opus_header
,
225 .packet
= opus_packet
,