lavf: default .ogg audio to libvorbis if we can
[libav.git] / libavformat / oggenc.c
CommitLineData
5118cae6
BC
1/*
2 * Ogg muxer
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
4 *
2912e87a 5 * This file is part of Libav.
5118cae6 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
5118cae6
BC
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
5118cae6
BC
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
5118cae6
BC
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
8f8bc923
DB
22#include <stdint.h>
23
245976da 24#include "libavutil/crc.h"
0ebcdf5c 25#include "libavutil/mathematics.h"
ed3e1b48 26#include "libavutil/opt.h"
ce2a9bc3 27#include "libavutil/random_seed.h"
245976da
DB
28#include "libavcodec/xiph.h"
29#include "libavcodec/bytestream.h"
59c6178a 30#include "libavcodec/flac.h"
5118cae6 31#include "avformat.h"
0abdb293 32#include "avio_internal.h"
1cc65cec 33#include "internal.h"
aeef3ec6 34#include "vorbiscomment.h"
5118cae6 35
95ca3b1e
BC
36#define MAX_PAGE_SIZE 65025
37
38typedef struct {
59220d55 39 int64_t start_granule;
95ca3b1e
BC
40 int64_t granule;
41 int stream_index;
42 uint8_t flags;
43 uint8_t segments_count;
44 uint8_t segments[255];
45 uint8_t data[MAX_PAGE_SIZE];
46 uint16_t size;
47} OGGPage;
48
5118cae6 49typedef struct {
5118cae6
BC
50 unsigned page_counter;
51 uint8_t *header[3];
52 int header_len[3];
53 /** for theora granule */
54 int kfgshift;
55 int64_t last_kf_pts;
56 int vrev;
37b74c86 57 int eos;
95ca3b1e
BC
58 unsigned page_count; ///< number of page buffered
59 OGGPage page; ///< current page
ce2a9bc3 60 unsigned serial_num; ///< serial number
ab043374 61 int64_t last_granule; ///< last packet granule
5118cae6
BC
62} OGGStreamContext;
63
95ca3b1e
BC
64typedef struct OGGPageList {
65 OGGPage page;
66 struct OGGPageList *next;
67} OGGPageList;
68
69typedef struct {
ed3e1b48 70 const AVClass *class;
95ca3b1e 71 OGGPageList *page_list;
ed3e1b48 72 int pref_size; ///< preferred page size (0 => fill all segments)
59220d55 73 int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
95ca3b1e
BC
74} OGGContext;
75
ed3e1b48
AG
76#define OFFSET(x) offsetof(OGGContext, x)
77#define PARAM AV_OPT_FLAG_ENCODING_PARAM
78
79static const AVOption options[] = {
59220d55 80 { "pagesize", "preferred page size in bytes (deprecated)",
e6153f17 81 OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
59220d55
JR
82 { "page_duration", "preferred page duration, in microseconds",
83 OFFSET(pref_duration), AV_OPT_TYPE_INT, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
ed3e1b48
AG
84 { NULL },
85};
86
87static const AVClass ogg_muxer_class = {
88 .class_name = "Ogg muxer",
89 .item_name = av_default_item_name,
90 .option = options,
91 .version = LIBAVUTIL_VERSION_INT,
92};
93
94
ae628ec1 95static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
5118cae6 96{
a2704c97 97 int64_t pos = avio_tell(pb);
b64030f2 98 uint32_t checksum = ffio_get_checksum(pb);
6b4aa5da 99 avio_seek(pb, crc_offset, SEEK_SET);
77eb5504 100 avio_wb32(pb, checksum);
6b4aa5da 101 avio_seek(pb, pos, SEEK_SET);
5118cae6
BC
102}
103
22fe18c1 104static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
5118cae6 105{
95ca3b1e 106 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
ae628ec1 107 AVIOContext *pb;
bc5c918e 108 int64_t crc_offset;
22fe18c1
BC
109 int ret, size;
110 uint8_t *buf;
5118cae6 111
b92c5452 112 ret = avio_open_dyn_buf(&pb);
22fe18c1
BC
113 if (ret < 0)
114 return ret;
4c4427a7 115 ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
0abdb293 116 ffio_wfourcc(pb, "OggS");
77eb5504
AK
117 avio_w8(pb, 0);
118 avio_w8(pb, page->flags | extra_flags);
119 avio_wl64(pb, page->granule);
120 avio_wl32(pb, oggstream->serial_num);
121 avio_wl32(pb, oggstream->page_counter++);
a2704c97 122 crc_offset = avio_tell(pb);
77eb5504
AK
123 avio_wl32(pb, 0); // crc
124 avio_w8(pb, page->segments_count);
125 avio_write(pb, page->segments, page->segments_count);
126 avio_write(pb, page->data, page->size);
22fe18c1
BC
127
128 ogg_update_checksum(s, pb, crc_offset);
b7f2fdde 129 avio_flush(pb);
22fe18c1 130
6dc7d80d 131 size = avio_close_dyn_buf(pb, &buf);
22fe18c1
BC
132 if (size < 0)
133 return size;
134
77eb5504 135 avio_write(s->pb, buf, size);
b7f2fdde 136 avio_flush(s->pb);
5c327555 137 av_free(buf);
95ca3b1e 138 oggstream->page_count--;
22fe18c1 139 return 0;
95ca3b1e
BC
140}
141
ab043374 142static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
95ca3b1e
BC
143{
144 if (oggstream->kfgshift)
ab043374
BC
145 return (granule>>oggstream->kfgshift) +
146 (granule & ((1<<oggstream->kfgshift)-1));
95ca3b1e 147 else
ab043374 148 return granule;
95ca3b1e
BC
149}
150
151static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
152{
153 AVStream *st2 = s->streams[next->stream_index];
154 AVStream *st = s->streams[page->stream_index];
155 int64_t next_granule, cur_granule;
156
157 if (next->granule == -1 || page->granule == -1)
158 return 0;
159
ab043374 160 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
95ca3b1e 161 st2->time_base, AV_TIME_BASE_Q);
ab043374 162 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
95ca3b1e
BC
163 st ->time_base, AV_TIME_BASE_Q);
164 return next_granule > cur_granule;
165}
166
167static int ogg_reset_cur_page(OGGStreamContext *oggstream)
168{
169 oggstream->page.granule = -1;
170 oggstream->page.flags = 0;
171 oggstream->page.segments_count = 0;
172 oggstream->page.size = 0;
173 return 0;
174}
175
176static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
177{
178 OGGContext *ogg = s->priv_data;
179 OGGPageList **p = &ogg->page_list;
180 OGGPageList *l = av_mallocz(sizeof(*l));
181
182 if (!l)
183 return AVERROR(ENOMEM);
184 l->page = oggstream->page;
185
59220d55 186 oggstream->page.start_granule = oggstream->page.granule;
95ca3b1e
BC
187 oggstream->page_count++;
188 ogg_reset_cur_page(oggstream);
189
190 while (*p) {
191 if (ogg_compare_granule(s, &(*p)->page, &l->page))
192 break;
193 p = &(*p)->next;
194 }
195 l->next = *p;
196 *p = l;
197
198 return 0;
199}
200
201static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
10b1c060
RD
202 uint8_t *data, unsigned size, int64_t granule,
203 int header)
95ca3b1e
BC
204{
205 OGGStreamContext *oggstream = st->priv_data;
ed3e1b48 206 OGGContext *ogg = s->priv_data;
95ca3b1e
BC
207 int total_segments = size / 255 + 1;
208 uint8_t *p = data;
ab043374
BC
209 int i, segments, len, flush = 0;
210
211 // Handles VFR by flushing page because this frame needs to have a timestamp
36ef5369 212 if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
ab043374
BC
213 ogg_granule_to_timestamp(oggstream, granule) >
214 ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
215 if (oggstream->page.granule != -1)
216 ogg_buffer_page(s, oggstream);
217 flush = 1;
218 }
95ca3b1e 219
59220d55
JR
220 // avoid a continued page
221 if (!header && oggstream->page.size > 0 &&
222 MAX_PAGE_SIZE - oggstream->page.size < size) {
223 ogg_buffer_page(s, oggstream);
224 }
225
95ca3b1e
BC
226 for (i = 0; i < total_segments; ) {
227 OGGPage *page = &oggstream->page;
228
229 segments = FFMIN(total_segments - i, 255 - page->segments_count);
230
231 if (i && !page->segments_count)
232 page->flags |= 1; // continued packet
233
234 memset(page->segments+page->segments_count, 255, segments - 1);
235 page->segments_count += segments - 1;
236
237 len = FFMIN(size, segments*255);
238 page->segments[page->segments_count++] = len - (segments-1)*255;
239 memcpy(page->data+page->size, p, len);
240 p += len;
241 size -= len;
242 i += segments;
243 page->size += len;
244
245 if (i == total_segments)
246 page->granule = granule;
247
59220d55
JR
248 if (!header) {
249 AVStream *st = s->streams[page->stream_index];
250
251 int64_t start = av_rescale_q(page->start_granule, st->time_base,
252 AV_TIME_BASE_Q);
253 int64_t next = av_rescale_q(page->granule, st->time_base,
254 AV_TIME_BASE_Q);
255
256 if (page->segments_count == 255 ||
257 (ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
258 (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
259 ogg_buffer_page(s, oggstream);
260 }
95ca3b1e
BC
261 }
262 }
ab043374
BC
263
264 if (flush && oggstream->page.granule != -1)
265 ogg_buffer_page(s, oggstream);
266
95ca3b1e 267 return 0;
5118cae6
BC
268}
269
87462968 270static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
d2d67e42 271 int *header_len, AVDictionary **m, int framing_bit)
87462968 272{
c6cd0e17 273 const char *vendor = bitexact ? "Libav" : LIBAVFORMAT_IDENT;
87462968
JR
274 int size;
275 uint8_t *p, *p0;
aeef3ec6 276 unsigned int count;
87462968 277
042ca05f
AK
278 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
279
81e5ff7a 280 size = offset + ff_vorbiscomment_length(*m, vendor, &count) + framing_bit;
87462968
JR
281 p = av_mallocz(size);
282 if (!p)
283 return NULL;
284 p0 = p;
285
286 p += offset;
aeef3ec6 287 ff_vorbiscomment_write(&p, m, vendor, count);
521d434f
JD
288 if (framing_bit)
289 bytestream_put_byte(&p, 1);
87462968
JR
290
291 *header_len = size;
292 return p0;
293}
294
59c6178a 295static int ogg_build_flac_headers(AVCodecContext *avctx,
aeef3ec6 296 OGGStreamContext *oggstream, int bitexact,
d2d67e42 297 AVDictionary **m)
5118cae6 298{
59c6178a
JR
299 enum FLACExtradataFormat format;
300 uint8_t *streaminfo;
5118cae6 301 uint8_t *p;
4315aa8b 302
d9cca9fc 303 if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
5118cae6 304 return -1;
4315aa8b
JR
305
306 // first packet: STREAMINFO
6e330816
JR
307 oggstream->header_len[0] = 51;
308 oggstream->header[0] = av_mallocz(51); // per ogg flac specs
5118cae6 309 p = oggstream->header[0];
e9a32230 310 if (!p)
2874c81c 311 return AVERROR(ENOMEM);
5118cae6
BC
312 bytestream_put_byte(&p, 0x7F);
313 bytestream_put_buffer(&p, "FLAC", 4);
314 bytestream_put_byte(&p, 1); // major version
315 bytestream_put_byte(&p, 0); // minor version
316 bytestream_put_be16(&p, 1); // headers packets without this one
317 bytestream_put_buffer(&p, "fLaC", 4);
318 bytestream_put_byte(&p, 0x00); // streaminfo
319 bytestream_put_be24(&p, 34);
59c6178a 320 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
4315aa8b
JR
321
322 // second packet: VorbisComment
521d434f 323 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
e9a32230 324 if (!p)
2874c81c 325 return AVERROR(ENOMEM);
87462968 326 oggstream->header[1] = p;
5118cae6
BC
327 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
328 bytestream_put_be24(&p, oggstream->header_len[1] - 4);
87462968
JR
329
330 return 0;
331}
332
333#define SPEEX_HEADER_SIZE 80
334
335static int ogg_build_speex_headers(AVCodecContext *avctx,
aeef3ec6 336 OGGStreamContext *oggstream, int bitexact,
d2d67e42 337 AVDictionary **m)
87462968
JR
338{
339 uint8_t *p;
340
341 if (avctx->extradata_size < SPEEX_HEADER_SIZE)
342 return -1;
343
344 // first packet: Speex header
345 p = av_mallocz(SPEEX_HEADER_SIZE);
346 if (!p)
2874c81c 347 return AVERROR(ENOMEM);
87462968
JR
348 oggstream->header[0] = p;
349 oggstream->header_len[0] = SPEEX_HEADER_SIZE;
350 bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
351 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
352
353 // second packet: VorbisComment
521d434f 354 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
87462968 355 if (!p)
2874c81c 356 return AVERROR(ENOMEM);
87462968 357 oggstream->header[1] = p;
4315aa8b 358
5118cae6
BC
359 return 0;
360}
361
bcc1f7ca
NC
362#define OPUS_HEADER_SIZE 19
363
364static int ogg_build_opus_headers(AVCodecContext *avctx,
365 OGGStreamContext *oggstream, int bitexact,
366 AVDictionary **m)
367{
368 uint8_t *p;
369
370 if (avctx->extradata_size < OPUS_HEADER_SIZE)
371 return -1;
372
373 /* first packet: Opus header */
374 p = av_mallocz(avctx->extradata_size);
375 if (!p)
376 return AVERROR(ENOMEM);
377 oggstream->header[0] = p;
378 oggstream->header_len[0] = avctx->extradata_size;
379 bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
380
381 /* second packet: VorbisComment */
382 p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
383 if (!p)
384 return AVERROR(ENOMEM);
385 oggstream->header[1] = p;
386 bytestream_put_buffer(&p, "OpusTags", 8);
387
388 return 0;
389}
390
5118cae6
BC
391static int ogg_write_header(AVFormatContext *s)
392{
59220d55 393 OGGContext *ogg = s->priv_data;
5118cae6
BC
394 OGGStreamContext *oggstream;
395 int i, j;
ce2a9bc3 396
59220d55
JR
397 if (ogg->pref_size)
398 av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
399
5118cae6
BC
400 for (i = 0; i < s->nb_streams; i++) {
401 AVStream *st = s->streams[i];
ce2a9bc3
BC
402 unsigned serial_num = i;
403
72415b2a 404 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
bcc1f7ca
NC
405 if (st->codec->codec_id == AV_CODEC_ID_OPUS)
406 /* Opus requires a fixed 48kHz clock */
407 avpriv_set_pts_info(st, 64, 1, 48000);
408 else
409 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
72415b2a 410 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
c3f9ebf7 411 avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
36ef5369
AK
412 if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
413 st->codec->codec_id != AV_CODEC_ID_THEORA &&
414 st->codec->codec_id != AV_CODEC_ID_SPEEX &&
bcc1f7ca
NC
415 st->codec->codec_id != AV_CODEC_ID_FLAC &&
416 st->codec->codec_id != AV_CODEC_ID_OPUS) {
5118cae6
BC
417 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
418 return -1;
419 }
420
421 if (!st->codec->extradata || !st->codec->extradata_size) {
422 av_log(s, AV_LOG_ERROR, "No extradata present\n");
423 return -1;
424 }
425 oggstream = av_mallocz(sizeof(*oggstream));
95ca3b1e 426 oggstream->page.stream_index = i;
ce2a9bc3
BC
427
428 if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
429 do {
430 serial_num = av_get_random_seed();
431 for (j = 0; j < i; j++) {
432 OGGStreamContext *sc = s->streams[j]->priv_data;
433 if (serial_num == sc->serial_num)
434 break;
435 }
436 } while (j < i);
437 oggstream->serial_num = serial_num;
438
5118cae6 439 st->priv_data = oggstream;
36ef5369 440 if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
3f2eadf4 441 int err = ogg_build_flac_headers(st->codec, oggstream,
aeef3ec6 442 st->codec->flags & CODEC_FLAG_BITEXACT,
81e5ff7a 443 &s->metadata);
3f2eadf4 444 if (err) {
87b4dd3b 445 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
5118cae6 446 av_freep(&st->priv_data);
3f2eadf4 447 return err;
5118cae6 448 }
36ef5369 449 } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
87462968 450 int err = ogg_build_speex_headers(st->codec, oggstream,
aeef3ec6 451 st->codec->flags & CODEC_FLAG_BITEXACT,
81e5ff7a 452 &s->metadata);
87462968
JR
453 if (err) {
454 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
455 av_freep(&st->priv_data);
456 return err;
457 }
bcc1f7ca
NC
458 } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
459 int err = ogg_build_opus_headers(st->codec, oggstream,
460 st->codec->flags & CODEC_FLAG_BITEXACT,
461 &s->metadata);
462 if (err) {
463 av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
464 av_freep(&st->priv_data);
465 return err;
466 }
5118cae6 467 } else {
521d434f 468 uint8_t *p;
36ef5369
AK
469 const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
470 int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
471 int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
521d434f 472
357db4c2 473 if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
36ef5369 474 st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
5118cae6
BC
475 oggstream->header, oggstream->header_len) < 0) {
476 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
477 av_freep(&st->priv_data);
478 return -1;
479 }
521d434f
JD
480
481 p = ogg_write_vorbiscomment(7, st->codec->flags & CODEC_FLAG_BITEXACT,
81e5ff7a 482 &oggstream->header_len[1], &s->metadata,
521d434f 483 framing_bit);
9677247b 484 oggstream->header[1] = p;
521d434f
JD
485 if (!p)
486 return AVERROR(ENOMEM);
487
521d434f
JD
488 bytestream_put_byte(&p, header_type);
489 bytestream_put_buffer(&p, cstr, 6);
490
36ef5369 491 if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
5118cae6
BC
492 /** KFGSHIFT is the width of the less significant section of the granule position
493 The less significant section is the frame count since the last keyframe */
494 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
495 oggstream->vrev = oggstream->header[0][9];
496 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
497 oggstream->kfgshift, oggstream->vrev);
498 }
499 }
500 }
95ca3b1e
BC
501
502 for (j = 0; j < s->nb_streams; j++) {
503 OGGStreamContext *oggstream = s->streams[j]->priv_data;
504 ogg_buffer_data(s, s->streams[j], oggstream->header[0],
10b1c060 505 oggstream->header_len[0], 0, 1);
95ca3b1e
BC
506 oggstream->page.flags |= 2; // bos
507 ogg_buffer_page(s, oggstream);
508 }
509 for (j = 0; j < s->nb_streams; j++) {
510 AVStream *st = s->streams[j];
511 OGGStreamContext *oggstream = st->priv_data;
512 for (i = 1; i < 3; i++) {
513 if (oggstream && oggstream->header_len[i])
514 ogg_buffer_data(s, st, oggstream->header[i],
10b1c060 515 oggstream->header_len[i], 0, 1);
5118cae6 516 }
95ca3b1e 517 ogg_buffer_page(s, oggstream);
5118cae6 518 }
59220d55
JR
519
520 oggstream->page.start_granule = AV_NOPTS_VALUE;
521
5118cae6
BC
522 return 0;
523}
524
95ca3b1e
BC
525static void ogg_write_pages(AVFormatContext *s, int flush)
526{
527 OGGContext *ogg = s->priv_data;
528 OGGPageList *next, *p;
529
530 if (!ogg->page_list)
531 return;
532
533 for (p = ogg->page_list; p; ) {
534 OGGStreamContext *oggstream =
535 s->streams[p->page.stream_index]->priv_data;
536 if (oggstream->page_count < 2 && !flush)
537 break;
538 ogg_write_page(s, &p->page,
539 flush && oggstream->page_count == 1 ? 4 : 0); // eos
540 next = p->next;
541 av_freep(&p);
542 p = next;
543 }
544 ogg->page_list = p;
545}
546
5118cae6
BC
547static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
548{
549 AVStream *st = s->streams[pkt->stream_index];
550 OGGStreamContext *oggstream = st->priv_data;
95ca3b1e 551 int ret;
5118cae6
BC
552 int64_t granule;
553
36ef5369 554 if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
5118cae6
BC
555 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
556 int pframe_count;
cc947f04 557 if (pkt->flags & AV_PKT_FLAG_KEY)
5118cae6
BC
558 oggstream->last_kf_pts = pts;
559 pframe_count = pts - oggstream->last_kf_pts;
560 // prevent frame count from overflow if key frame flag is not set
561 if (pframe_count >= (1<<oggstream->kfgshift)) {
562 oggstream->last_kf_pts += pframe_count;
563 pframe_count = 0;
564 }
565 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
bcc1f7ca
NC
566 } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
567 granule = pkt->pts + pkt->duration + av_rescale_q(st->codec->delay, (AVRational){ 1, st->codec->sample_rate }, st->time_base);
568 else
5118cae6 569 granule = pkt->pts + pkt->duration;
5118cae6 570
59220d55
JR
571 if (oggstream->page.start_granule == AV_NOPTS_VALUE)
572 oggstream->page.start_granule = pkt->pts;
573
10b1c060 574 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
95ca3b1e
BC
575 if (ret < 0)
576 return ret;
5118cae6 577
95ca3b1e 578 ogg_write_pages(s, 0);
fd5583bc 579
ab043374
BC
580 oggstream->last_granule = granule;
581
95ca3b1e 582 return 0;
fd5583bc
BC
583}
584
95ca3b1e 585static int ogg_write_trailer(AVFormatContext *s)
37b74c86 586{
95ca3b1e 587 int i;
012dc2c4 588
59220d55
JR
589 /* flush current page if needed */
590 for (i = 0; i < s->nb_streams; i++) {
591 OGGStreamContext *oggstream = s->streams[i]->priv_data;
592
593 if (oggstream->page.size > 0)
594 ogg_buffer_page(s, oggstream);
595 }
e07b882b 596
95ca3b1e 597 ogg_write_pages(s, 1);
e07b882b 598
5118cae6
BC
599 for (i = 0; i < s->nb_streams; i++) {
600 AVStream *st = s->streams[i];
601 OGGStreamContext *oggstream = st->priv_data;
36ef5369 602 if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
bcc1f7ca
NC
603 st->codec->codec_id == AV_CODEC_ID_SPEEX ||
604 st->codec->codec_id == AV_CODEC_ID_OPUS) {
5118cae6 605 av_free(oggstream->header[0]);
5118cae6 606 }
9677247b 607 av_freep(&oggstream->header[1]);
5118cae6
BC
608 av_freep(&st->priv_data);
609 }
610 return 0;
611}
612
c6610a21 613AVOutputFormat ff_ogg_muxer = {
dfc2c4d9
AK
614 .name = "ogg",
615 .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
616 .mime_type = "application/ogg",
bcc1f7ca 617 .extensions = "ogg,ogv,spx,opus",
dfc2c4d9 618 .priv_data_size = sizeof(OGGContext),
b0c2c097
AK
619 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
620 AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
36ef5369 621 .video_codec = AV_CODEC_ID_THEORA,
dfc2c4d9
AK
622 .write_header = ogg_write_header,
623 .write_packet = ogg_write_packet,
624 .write_trailer = ogg_write_trailer,
c2cb01d4 625 .flags = AVFMT_TS_NEGATIVE,
ed3e1b48 626 .priv_class = &ogg_muxer_class,
5118cae6 627};