3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
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/crc.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/random_seed.h"
28 #include "libavcodec/xiph.h"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/flac.h"
32 #include "avio_internal.h"
34 #include "vorbiscomment.h"
36 #define MAX_PAGE_SIZE 65025
38 typedef struct OGGPage {
39 int64_t start_granule;
43 uint8_t segments_count;
44 uint8_t segments[255];
45 uint8_t data[MAX_PAGE_SIZE];
49 typedef struct OGGStreamContext {
50 unsigned page_counter;
53 /** for theora granule */
60 unsigned page_count; ///< number of page buffered
61 OGGPage page; ///< current page
62 unsigned serial_num; ///< serial number
63 int64_t last_granule; ///< last packet granule
66 typedef struct OGGPageList {
68 struct OGGPageList *next;
71 typedef struct OGGContext {
73 OGGPageList *page_list;
74 int pref_size; ///< preferred page size (0 => fill all segments)
75 int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
79 #define OFFSET(x) offsetof(OGGContext, x)
80 #define PARAM AV_OPT_FLAG_ENCODING_PARAM
82 static const AVOption options[] = {
83 { "serial_offset", "serial number offset",
84 OFFSET(serial_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, PARAM },
85 { "oggpagesize", "Set preferred Ogg page size.",
86 OFFSET(pref_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, MAX_PAGE_SIZE, PARAM},
87 { "pagesize", "preferred page size in bytes (deprecated)",
88 OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
89 { "page_duration", "preferred page duration, in microseconds",
90 OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
94 #define OGG_CLASS(flavor, name)\
95 static const AVClass flavor ## _muxer_class = {\
96 .class_name = #name " muxer",\
97 .item_name = av_default_item_name,\
99 .version = LIBAVUTIL_VERSION_INT,\
102 static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
104 int64_t pos = avio_tell(pb);
105 uint32_t checksum = ffio_get_checksum(pb);
106 avio_seek(pb, crc_offset, SEEK_SET);
107 avio_wb32(pb, checksum);
108 avio_seek(pb, pos, SEEK_SET);
111 static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
113 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
119 ret = avio_open_dyn_buf(&pb);
122 ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
123 ffio_wfourcc(pb, "OggS");
125 avio_w8(pb, page->flags | extra_flags);
126 avio_wl64(pb, page->granule);
127 avio_wl32(pb, oggstream->serial_num);
128 avio_wl32(pb, oggstream->page_counter++);
129 crc_offset = avio_tell(pb);
130 avio_wl32(pb, 0); // crc
131 avio_w8(pb, page->segments_count);
132 avio_write(pb, page->segments, page->segments_count);
133 avio_write(pb, page->data, page->size);
135 ogg_update_checksum(s, pb, crc_offset);
138 size = avio_close_dyn_buf(pb, &buf);
142 avio_write(s->pb, buf, size);
145 oggstream->page_count--;
149 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
151 return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
152 (oggstream->isvp8 && !((granule >> 3) & 0x07ffffff));
155 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
157 if (oggstream->kfgshift)
158 return (granule>>oggstream->kfgshift) +
159 (granule & ((1<<oggstream->kfgshift)-1));
160 else if (oggstream->isvp8)
161 return granule >> 32;
166 static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
168 AVStream *st2 = s->streams[next->stream_index];
169 AVStream *st = s->streams[page->stream_index];
170 int64_t next_granule, cur_granule;
172 if (next->granule == -1 || page->granule == -1)
175 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
176 st2->time_base, AV_TIME_BASE_Q);
177 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
178 st ->time_base, AV_TIME_BASE_Q);
179 return next_granule > cur_granule;
182 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
184 oggstream->page.granule = -1;
185 oggstream->page.flags = 0;
186 oggstream->page.segments_count = 0;
187 oggstream->page.size = 0;
191 static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
193 OGGContext *ogg = s->priv_data;
194 OGGPageList **p = &ogg->page_list;
195 OGGPageList *l = av_mallocz(sizeof(*l));
198 return AVERROR(ENOMEM);
199 l->page = oggstream->page;
201 oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule);
202 oggstream->page_count++;
203 ogg_reset_cur_page(oggstream);
206 if (ogg_compare_granule(s, &(*p)->page, &l->page))
216 static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
217 uint8_t *data, unsigned size, int64_t granule,
220 OGGStreamContext *oggstream = st->priv_data;
221 OGGContext *ogg = s->priv_data;
222 int total_segments = size / 255 + 1;
224 int i, segments, len, flush = 0;
226 // Handles VFR by flushing page because this frame needs to have a timestamp
227 // For theora and VP8, keyframes also need to have a timestamp to correctly mark
228 // them as such, otherwise seeking will not work correctly at the very
229 // least with old libogg versions.
230 // Do not try to flush header packets though, that will create broken files.
231 if ((st->codecpar->codec_id == AV_CODEC_ID_THEORA || st->codecpar->codec_id == AV_CODEC_ID_VP8) && !header &&
232 (ogg_granule_to_timestamp(oggstream, granule) >
233 ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
234 ogg_key_granule(oggstream, granule))) {
235 if (oggstream->page.granule != -1)
236 ogg_buffer_page(s, oggstream);
240 // avoid a continued page
241 if (!header && oggstream->page.size > 0 &&
242 MAX_PAGE_SIZE - oggstream->page.size < size) {
243 ogg_buffer_page(s, oggstream);
246 for (i = 0; i < total_segments; ) {
247 OGGPage *page = &oggstream->page;
249 segments = FFMIN(total_segments - i, 255 - page->segments_count);
251 if (i && !page->segments_count)
252 page->flags |= 1; // continued packet
254 memset(page->segments+page->segments_count, 255, segments - 1);
255 page->segments_count += segments - 1;
257 len = FFMIN(size, segments*255);
258 page->segments[page->segments_count++] = len - (segments-1)*255;
259 memcpy(page->data+page->size, p, len);
265 if (i == total_segments)
266 page->granule = granule;
269 AVStream *st = s->streams[page->stream_index];
271 int64_t start = av_rescale_q(page->start_granule, st->time_base,
273 int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule),
274 st->time_base, AV_TIME_BASE_Q);
276 if (page->segments_count == 255) {
277 ogg_buffer_page(s, oggstream);
278 } else if (!header) {
279 if ((ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
280 (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
281 ogg_buffer_page(s, oggstream);
287 if (flush && oggstream->page.granule != -1)
288 ogg_buffer_page(s, oggstream);
293 static uint8_t *ogg_write_vorbiscomment(int64_t offset, int bitexact,
294 int *header_len, AVDictionary **m, int framing_bit)
296 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
300 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
302 size = offset + ff_vorbiscomment_length(*m, vendor) + framing_bit;
305 p = av_mallocz(size);
311 ff_vorbiscomment_write(&p, m, vendor);
313 bytestream_put_byte(&p, 1);
319 static int ogg_build_flac_headers(AVCodecParameters *par,
320 OGGStreamContext *oggstream, int bitexact,
325 if (par->extradata_size < FLAC_STREAMINFO_SIZE)
326 return AVERROR(EINVAL);
328 // first packet: STREAMINFO
329 oggstream->header_len[0] = 51;
330 oggstream->header[0] = av_mallocz(51); // per ogg flac specs
331 p = oggstream->header[0];
333 return AVERROR(ENOMEM);
334 bytestream_put_byte(&p, 0x7F);
335 bytestream_put_buffer(&p, "FLAC", 4);
336 bytestream_put_byte(&p, 1); // major version
337 bytestream_put_byte(&p, 0); // minor version
338 bytestream_put_be16(&p, 1); // headers packets without this one
339 bytestream_put_buffer(&p, "fLaC", 4);
340 bytestream_put_byte(&p, 0x00); // streaminfo
341 bytestream_put_be24(&p, 34);
342 bytestream_put_buffer(&p, par->extradata, FLAC_STREAMINFO_SIZE);
344 // second packet: VorbisComment
345 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
347 return AVERROR(ENOMEM);
348 oggstream->header[1] = p;
349 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
350 bytestream_put_be24(&p, oggstream->header_len[1] - 4);
355 #define SPEEX_HEADER_SIZE 80
357 static int ogg_build_speex_headers(AVCodecParameters *par,
358 OGGStreamContext *oggstream, int bitexact,
363 if (par->extradata_size < SPEEX_HEADER_SIZE)
364 return AVERROR_INVALIDDATA;
366 // first packet: Speex header
367 p = av_mallocz(SPEEX_HEADER_SIZE);
369 return AVERROR(ENOMEM);
370 oggstream->header[0] = p;
371 oggstream->header_len[0] = SPEEX_HEADER_SIZE;
372 bytestream_put_buffer(&p, par->extradata, SPEEX_HEADER_SIZE);
373 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
375 // second packet: VorbisComment
376 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
378 return AVERROR(ENOMEM);
379 oggstream->header[1] = p;
384 #define OPUS_HEADER_SIZE 19
386 static int ogg_build_opus_headers(AVCodecParameters *par,
387 OGGStreamContext *oggstream, int bitexact,
392 if (par->extradata_size < OPUS_HEADER_SIZE)
393 return AVERROR_INVALIDDATA;
395 /* first packet: Opus header */
396 p = av_mallocz(par->extradata_size);
398 return AVERROR(ENOMEM);
399 oggstream->header[0] = p;
400 oggstream->header_len[0] = par->extradata_size;
401 bytestream_put_buffer(&p, par->extradata, par->extradata_size);
403 /* second packet: VorbisComment */
404 p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
406 return AVERROR(ENOMEM);
407 oggstream->header[1] = p;
408 bytestream_put_buffer(&p, "OpusTags", 8);
413 #define VP8_HEADER_SIZE 26
415 static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st,
416 OGGStreamContext *oggstream, int bitexact)
418 AVCodecParameters *par = st->codecpar;
421 /* first packet: VP8 header */
422 p = av_mallocz(VP8_HEADER_SIZE);
424 return AVERROR(ENOMEM);
425 oggstream->header[0] = p;
426 oggstream->header_len[0] = VP8_HEADER_SIZE;
427 bytestream_put_byte(&p, 0x4f); // HDRID
428 bytestream_put_buffer(&p, "VP80", 4); // Identifier
429 bytestream_put_byte(&p, 1); // HDRTYP
430 bytestream_put_byte(&p, 1); // VMAJ
431 bytestream_put_byte(&p, 0); // VMIN
432 bytestream_put_be16(&p, par->width);
433 bytestream_put_be16(&p, par->height);
434 bytestream_put_be24(&p, par->sample_aspect_ratio.num);
435 bytestream_put_be24(&p, par->sample_aspect_ratio.den);
436 if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
437 // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
438 // multiple framerate if available.
439 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
440 st->time_base.num, st->time_base.den,
441 st->r_frame_rate.den, st->r_frame_rate.num);
442 avpriv_set_pts_info(st, 64, st->r_frame_rate.den, st->r_frame_rate.num);
444 bytestream_put_be32(&p, st->time_base.den);
445 bytestream_put_be32(&p, st->time_base.num);
447 /* optional second packet: VorbisComment */
448 if (av_dict_get(st->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
449 p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0);
451 return AVERROR(ENOMEM);
452 oggstream->header[1] = p;
453 bytestream_put_byte(&p, 0x4f); // HDRID
454 bytestream_put_buffer(&p, "VP80", 4); // Identifier
455 bytestream_put_byte(&p, 2); // HDRTYP
456 bytestream_put_byte(&p, 0x20);
459 oggstream->isvp8 = 1;
464 static void ogg_write_pages(AVFormatContext *s, int flush)
466 OGGContext *ogg = s->priv_data;
467 OGGPageList *next, *p;
472 for (p = ogg->page_list; p; ) {
473 OGGStreamContext *oggstream =
474 s->streams[p->page.stream_index]->priv_data;
475 if (oggstream->page_count < 2 && !flush)
477 ogg_write_page(s, &p->page,
478 flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
486 static int ogg_init(AVFormatContext *s)
488 OGGContext *ogg = s->priv_data;
489 OGGStreamContext *oggstream = NULL;
493 av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
495 for (i = 0; i < s->nb_streams; i++) {
496 AVStream *st = s->streams[i];
497 unsigned serial_num = i + ogg->serial_offset;
499 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
500 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
501 /* Opus requires a fixed 48kHz clock */
502 avpriv_set_pts_info(st, 64, 1, 48000);
504 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
507 if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
508 st->codecpar->codec_id != AV_CODEC_ID_THEORA &&
509 st->codecpar->codec_id != AV_CODEC_ID_SPEEX &&
510 st->codecpar->codec_id != AV_CODEC_ID_FLAC &&
511 st->codecpar->codec_id != AV_CODEC_ID_OPUS &&
512 st->codecpar->codec_id != AV_CODEC_ID_VP8) {
513 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
514 return AVERROR(EINVAL);
517 if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
518 st->codecpar->codec_id != AV_CODEC_ID_VP8) {
519 av_log(s, AV_LOG_ERROR, "No extradata present\n");
520 return AVERROR_INVALIDDATA;
522 oggstream = av_mallocz(sizeof(*oggstream));
524 return AVERROR(ENOMEM);
526 oggstream->page.stream_index = i;
528 if (!(s->flags & AVFMT_FLAG_BITEXACT))
530 serial_num = av_get_random_seed();
531 for (j = 0; j < i; j++) {
532 OGGStreamContext *sc = s->streams[j]->priv_data;
533 if (serial_num == sc->serial_num)
537 oggstream->serial_num = serial_num;
539 av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
541 st->priv_data = oggstream;
542 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC) {
543 int err = ogg_build_flac_headers(st->codecpar, oggstream,
544 s->flags & AVFMT_FLAG_BITEXACT,
547 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
548 av_freep(&st->priv_data);
551 } else if (st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
552 int err = ogg_build_speex_headers(st->codecpar, oggstream,
553 s->flags & AVFMT_FLAG_BITEXACT,
556 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
557 av_freep(&st->priv_data);
560 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
561 int err = ogg_build_opus_headers(st->codecpar, oggstream,
562 s->flags & AVFMT_FLAG_BITEXACT,
565 av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
566 av_freep(&st->priv_data);
569 } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
570 int err = ogg_build_vp8_headers(s, st, oggstream,
571 s->flags & AVFMT_FLAG_BITEXACT);
573 av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
574 av_freep(&st->priv_data);
579 const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
580 int header_type = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
581 int framing_bit = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
583 if (avpriv_split_xiph_headers(st->codecpar->extradata, st->codecpar->extradata_size,
584 st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
585 (const uint8_t**)oggstream->header, oggstream->header_len) < 0) {
586 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
587 av_freep(&st->priv_data);
588 return AVERROR_INVALIDDATA;
591 p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
592 &oggstream->header_len[1], &st->metadata,
594 oggstream->header[1] = p;
596 return AVERROR(ENOMEM);
598 bytestream_put_byte(&p, header_type);
599 bytestream_put_buffer(&p, cstr, 6);
601 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
602 int den = AV_RB32(oggstream->header[0] + 22), num = AV_RB32(oggstream->header[0] + 26);
603 /* Make sure to use time base stored in the Theora stream header to write
604 correct timestamps */
605 if (st->time_base.num != num || st->time_base.den != den) {
606 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
607 st->time_base.num, st->time_base.den, num, den);
608 avpriv_set_pts_info(st, 64, num, den);
610 /** KFGSHIFT is the width of the less significant section of the granule position
611 The less significant section is the frame count since the last keyframe */
612 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
613 oggstream->vrev = oggstream->header[0][9];
614 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
615 oggstream->kfgshift, oggstream->vrev);
623 static int ogg_write_header(AVFormatContext *s)
625 OGGStreamContext *oggstream = NULL;
628 for (j = 0; j < s->nb_streams; j++) {
629 oggstream = s->streams[j]->priv_data;
630 ogg_buffer_data(s, s->streams[j], oggstream->header[0],
631 oggstream->header_len[0], 0, 1);
632 oggstream->page.flags |= 2; // bos
633 ogg_buffer_page(s, oggstream);
635 for (j = 0; j < s->nb_streams; j++) {
636 AVStream *st = s->streams[j];
637 oggstream = st->priv_data;
638 for (i = 1; i < 3; i++) {
639 if (oggstream->header_len[i])
640 ogg_buffer_data(s, st, oggstream->header[i],
641 oggstream->header_len[i], 0, 1);
643 ogg_buffer_page(s, oggstream);
646 oggstream->page.start_granule = AV_NOPTS_VALUE;
648 ogg_write_pages(s, 2);
653 static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
655 AVStream *st = s->streams[pkt->stream_index];
656 OGGStreamContext *oggstream = st->priv_data;
660 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
661 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
663 if (pkt->flags & AV_PKT_FLAG_KEY)
664 oggstream->last_kf_pts = pts;
665 pframe_count = pts - oggstream->last_kf_pts;
666 // prevent frame count from overflow if key frame flag is not set
667 if (pframe_count >= (1<<oggstream->kfgshift)) {
668 oggstream->last_kf_pts += pframe_count;
671 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
672 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
673 granule = pkt->pts + pkt->duration +
674 av_rescale_q(st->codecpar->initial_padding,
675 (AVRational){ 1, st->codecpar->sample_rate },
677 else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
678 int64_t pts, invcnt, dist;
681 visible = (pkt->data[0] >> 4) & 1;
682 pts = pkt->pts + pkt->duration;
683 invcnt = (oggstream->last_granule >> 30) & 3;
684 invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
685 dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
687 granule = (pts << 32) | (invcnt << 30) | (dist << 3);
689 granule = pkt->pts + pkt->duration;
691 if (oggstream->page.start_granule == AV_NOPTS_VALUE)
692 oggstream->page.start_granule = pkt->pts;
694 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
698 ogg_write_pages(s, 0);
700 oggstream->last_granule = granule;
705 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
710 return ogg_write_packet_internal(s, pkt);
712 for (i = 0; i < s->nb_streams; i++) {
713 OGGStreamContext *oggstream = s->streams[i]->priv_data;
714 if (oggstream->page.segments_count)
715 ogg_buffer_page(s, oggstream);
718 ogg_write_pages(s, 2);
722 static int ogg_write_trailer(AVFormatContext *s)
726 /* flush current page if needed */
727 for (i = 0; i < s->nb_streams; i++) {
728 OGGStreamContext *oggstream = s->streams[i]->priv_data;
730 if (oggstream->page.size > 0)
731 ogg_buffer_page(s, oggstream);
734 ogg_write_pages(s, 1);
739 static void ogg_free(AVFormatContext *s)
743 for (i = 0; i < s->nb_streams; i++) {
744 AVStream *st = s->streams[i];
745 OGGStreamContext *oggstream = st->priv_data;
748 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
749 st->codecpar->codec_id == AV_CODEC_ID_SPEEX ||
750 st->codecpar->codec_id == AV_CODEC_ID_OPUS ||
751 st->codecpar->codec_id == AV_CODEC_ID_VP8) {
752 av_freep(&oggstream->header[0]);
754 av_freep(&oggstream->header[1]);
755 av_freep(&st->priv_data);
761 AVOutputFormat ff_ogg_muxer = {
763 .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
764 .mime_type = "application/ogg",
766 #if !CONFIG_OGV_MUXER
769 #if !CONFIG_SPX_MUXER
772 #if !CONFIG_OPUS_MUXER
776 .priv_data_size = sizeof(OGGContext),
777 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
778 AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
779 .video_codec = AV_CODEC_ID_THEORA,
781 .write_header = ogg_write_header,
782 .write_packet = ogg_write_packet,
783 .write_trailer = ogg_write_trailer,
785 .flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
786 .priv_class = &ogg_muxer_class,
791 OGG_CLASS(oga, Ogg audio)
792 AVOutputFormat ff_oga_muxer = {
794 .long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
795 .mime_type = "audio/ogg",
797 .priv_data_size = sizeof(OGGContext),
798 .audio_codec = AV_CODEC_ID_FLAC,
800 .write_header = ogg_write_header,
801 .write_packet = ogg_write_packet,
802 .write_trailer = ogg_write_trailer,
804 .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
805 .priv_class = &oga_muxer_class,
810 OGG_CLASS(ogv, Ogg video)
811 AVOutputFormat ff_ogv_muxer = {
813 .long_name = NULL_IF_CONFIG_SMALL("Ogg Video"),
814 .mime_type = "video/ogg",
816 .priv_data_size = sizeof(OGGContext),
817 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
818 AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
819 .video_codec = CONFIG_LIBTHEORA_ENCODER ?
820 AV_CODEC_ID_THEORA : AV_CODEC_ID_VP8,
822 .write_header = ogg_write_header,
823 .write_packet = ogg_write_packet,
824 .write_trailer = ogg_write_trailer,
826 .flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
827 .priv_class = &ogv_muxer_class,
832 OGG_CLASS(spx, Ogg Speex)
833 AVOutputFormat ff_spx_muxer = {
835 .long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
836 .mime_type = "audio/ogg",
838 .priv_data_size = sizeof(OGGContext),
839 .audio_codec = AV_CODEC_ID_SPEEX,
841 .write_header = ogg_write_header,
842 .write_packet = ogg_write_packet,
843 .write_trailer = ogg_write_trailer,
845 .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
846 .priv_class = &spx_muxer_class,
850 #if CONFIG_OPUS_MUXER
851 OGG_CLASS(opus, Ogg Opus)
852 AVOutputFormat ff_opus_muxer = {
854 .long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
855 .mime_type = "audio/ogg",
856 .extensions = "opus",
857 .priv_data_size = sizeof(OGGContext),
858 .audio_codec = AV_CODEC_ID_OPUS,
860 .write_header = ogg_write_header,
861 .write_packet = ogg_write_packet,
862 .write_trailer = ogg_write_trailer,
864 .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
865 .priv_class = &opus_muxer_class,