FFmpeg
Loading...
Searching...
No Matches
oggenc.c
Go to the documentation of this file.
1/*
2 * Ogg muxer
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
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 "config_components.h"
23
24#include <stdint.h>
25
26#include "libavutil/crc.h"
28#include "libavutil/mem.h"
29#include "libavutil/opt.h"
31#include "libavcodec/xiph.h"
33#include "libavcodec/flac.h"
34#include "avformat.h"
35#include "avio_internal.h"
36#include "internal.h"
37#include "mux.h"
38#include "version.h"
39#include "vorbiscomment.h"
40
41#define MAX_PAGE_SIZE 65025
42
43typedef struct OGGPage {
47 uint8_t flags;
49 uint8_t segments[255];
51 uint16_t size;
52} OGGPage;
53
54typedef struct OGGStreamContext {
55 unsigned page_counter;
56 uint8_t *header[3];
57 int header_len[3];
58 /** for theora granule */
61 int vrev;
62 /* for VP8 granule */
63 int isvp8;
64 int eos;
65 unsigned page_count; ///< number of page buffered
66 OGGPage page; ///< current page
67 unsigned serial_num; ///< serial number
68 int64_t last_granule; ///< last packet granule
69 int packet_seen; ///< true when packets have been submitted
71
72typedef struct OGGPageList {
76
77typedef struct OGGContext {
78 const AVClass *class;
80 int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
82 int failed; // if true all packet submission will fail.
84
85#define OFFSET(x) offsetof(OGGContext, x)
86#define PARAM AV_OPT_FLAG_ENCODING_PARAM
87
89
90static const AVOption options[] = {
91 { "serial_offset", "serial number offset, -1 to derive it from the stream headers",
92 OFFSET(serial_offset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, PARAM },
93 { "page_duration", "preferred page duration, in microseconds",
94 OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
95 { NULL },
96};
97
98static const AVClass ogg_muxer_class = {
99 .class_name = "Ogg (audio/video/Speex/Opus) muxer",
100 .item_name = av_default_item_name,
101 .option = options,
102 .version = LIBAVUTIL_VERSION_INT,
103};
104
106{
107 OGGStreamContext *oggstream = st->priv_data;
108
109 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
113 av_freep(&oggstream->header[0]);
114 }
115
116 av_freep(&oggstream->header[1]);
117}
118
119static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
120{
121 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
122 uint8_t buf[4 + 1 + 1 + 8 + 4 + 4 + 4 + 1 + 255], *ptr = buf, *crc_pos;
123 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE);
124 uint32_t crc;
125
126 bytestream_put_le32(&ptr, MKTAG('O', 'g', 'g', 'S'));
127 bytestream_put_byte(&ptr, 0);
128 bytestream_put_byte(&ptr, page->flags | extra_flags);
129 bytestream_put_le64(&ptr, page->granule);
130 bytestream_put_le32(&ptr, oggstream->serial_num);
131 bytestream_put_le32(&ptr, oggstream->page_counter++);
132 crc_pos = ptr;
133 bytestream_put_le32(&ptr, 0);
134 bytestream_put_byte(&ptr, page->segments_count);
135 bytestream_put_buffer(&ptr, page->segments, page->segments_count);
136
137 crc = av_crc(crc_table, 0, buf, ptr - buf);
138 crc = av_crc(crc_table, crc, page->data, page->size);
139 bytestream_put_be32(&crc_pos, crc);
140
141 avio_write(s->pb, buf, ptr - buf);
142 avio_write(s->pb, page->data, page->size);
144 oggstream->page_count--;
145}
146
147static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
148{
149 return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
150 (oggstream->isvp8 && !((granule >> 3) & 0x07ffffff));
151}
152
154{
155 if (oggstream->kfgshift)
156 return (granule>>oggstream->kfgshift) +
157 (granule & ((1<<oggstream->kfgshift)-1));
158 else if (oggstream->isvp8)
159 return granule >> 32;
160 else
161 return granule;
162}
163
165{
166 AVStream *st2 = s->streams[next->stream_index];
167 AVStream *st = s->streams[page->stream_index];
168 int64_t next_granule, cur_granule;
169
170 if (next->granule == -1 || page->granule == -1)
171 return 0;
172
173 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
175 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
177 return next_granule > cur_granule;
178}
179
181{
182 oggstream->page.granule = -1;
183 oggstream->page.flags = 0;
184 oggstream->page.segments_count = 0;
185 oggstream->page.size = 0;
186 return 0;
187}
188
190{
191 OGGContext *ogg = s->priv_data;
192 OGGPageList **p = &ogg->page_list;
193 OGGPageList *l = av_mallocz(sizeof(*l));
194
195 if (!l) {
196 ogg_reset_cur_page(oggstream);
197 return AVERROR(ENOMEM);
198 }
199 l->page = oggstream->page;
200
201 oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule);
202 oggstream->page_count++;
203 ogg_reset_cur_page(oggstream);
204
205 while (*p) {
206 if (ogg_compare_granule(s, &(*p)->page, &l->page))
207 break;
208 p = &(*p)->next;
209 }
210 l->next = *p;
211 *p = l;
212
213 return 0;
214}
215
217 const uint8_t *data, unsigned size, int64_t granule,
218 int header)
219{
220 OGGStreamContext *oggstream = st->priv_data;
221 OGGContext *ogg = s->priv_data;
222 int total_segments = size / 255 + 1;
223 const uint8_t *p = data;
224 int i, segments, len, ret, flush = 0;
225
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.
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 (ret = ogg_buffer_page(s, oggstream)) < 0)
237 return ret;
238 flush = 1;
239 }
240
241 // avoid a continued page
242 if (!header && oggstream->page.size > 0 &&
243 MAX_PAGE_SIZE - oggstream->page.size < size) {
244 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
245 return ret;
246 }
247
248 for (i = 0; i < total_segments; ) {
249 OGGPage *page = &oggstream->page;
250
251 segments = FFMIN(total_segments - i, 255 - page->segments_count);
252
253 if (i && !page->segments_count)
254 page->flags |= 1; // continued packet
255
256 memset(page->segments+page->segments_count, 255, segments - 1);
257 page->segments_count += segments - 1;
258
259 len = FFMIN(size, segments*255);
260 page->segments[page->segments_count++] = len - (segments-1)*255;
261 if (len)
262 memcpy(page->data+page->size, p, len);
263 p += len;
264 size -= len;
265 i += segments;
266 page->size += len;
267
268 if (i == total_segments)
269 page->granule = granule;
270
271 {
272 AVRational time_base = s->streams[page->stream_index]->time_base;
273
274 int64_t start = av_rescale_q(page->start_granule, time_base,
276 int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule),
277 time_base, AV_TIME_BASE_Q);
278
279 if (page->segments_count == 255) {
280 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
281 return ret;
282 } else if (!header) {
283 if (ogg->pref_duration > 0 && next - start >= ogg->pref_duration) {
284 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
285 return ret;
286 }
287 }
288 }
289 }
290
291 if (flush && oggstream->page.granule != -1 &&
292 (ret = ogg_buffer_page(s, oggstream)) < 0)
293 return ret;
294
295 return 0;
296}
297
298static uint8_t *ogg_write_vorbiscomment(int64_t offset, int bitexact,
299 int *header_len, AVDictionary **m, int framing_bit,
300 AVChapter **chapters, unsigned int nb_chapters)
301{
302 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
303 FFIOContext pb;
305 uint8_t *p;
306
308
309 size = ff_vorbiscomment_length(*m, vendor, chapters, nb_chapters);
310 if (size < 0)
311 return NULL;
312 size += offset + framing_bit;
313 if (size > INT_MAX)
314 return NULL;
315 p = av_mallocz(size);
316 if (!p)
317 return NULL;
318
320 ff_vorbiscomment_write(&pb.pub, *m, vendor, chapters, nb_chapters);
321 if (framing_bit)
322 avio_w8(&pb.pub, 1);
323
324 *header_len = size;
325 return p;
326}
327
329 OGGStreamContext *oggstream, int bitexact,
330 AVDictionary **m)
331{
332 uint8_t *p;
333
335 return AVERROR(EINVAL);
336
337 // first packet: STREAMINFO
338 oggstream->header_len[0] = 51;
339 oggstream->header[0] = av_mallocz(51); // per ogg flac specs
340 p = oggstream->header[0];
341 if (!p)
342 return AVERROR(ENOMEM);
343 bytestream_put_byte(&p, 0x7F);
344 bytestream_put_buffer(&p, "FLAC", 4);
345 bytestream_put_byte(&p, 1); // major version
346 bytestream_put_byte(&p, 0); // minor version
347 bytestream_put_be16(&p, 1); // headers packets without this one
348 bytestream_put_buffer(&p, "fLaC", 4);
349 bytestream_put_byte(&p, 0x00); // streaminfo
350 bytestream_put_be24(&p, 34);
352
353 // second packet: VorbisComment
354 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
355 if (!p)
356 return AVERROR(ENOMEM);
357 oggstream->header[1] = p;
358 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
359 bytestream_put_be24(&p, oggstream->header_len[1] - 4);
360
361 return 0;
362}
363
364#define SPEEX_HEADER_SIZE 80
365
367 OGGStreamContext *oggstream, int bitexact,
368 AVDictionary **m)
369{
370 uint8_t *p;
371
373 return AVERROR_INVALIDDATA;
374
375 // first packet: Speex header
377 if (!p)
378 return AVERROR(ENOMEM);
379 oggstream->header[0] = p;
380 oggstream->header_len[0] = SPEEX_HEADER_SIZE;
382 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
383
384 // second packet: VorbisComment
385 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
386 if (!p)
387 return AVERROR(ENOMEM);
388 oggstream->header[1] = p;
389
390 return 0;
391}
392
393#define OPUS_HEADER_SIZE 19
394
396 OGGStreamContext *oggstream, int bitexact,
397 AVDictionary **m, AVChapter **chapters,
398 unsigned int nb_chapters)
399{
400 uint8_t *p;
401
403 return AVERROR_INVALIDDATA;
404
405 /* first packet: Opus header */
406 p = av_mallocz(par->extradata_size);
407 if (!p)
408 return AVERROR(ENOMEM);
409 oggstream->header[0] = p;
410 oggstream->header_len[0] = par->extradata_size;
412
413 /* second packet: VorbisComment */
414 p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0, chapters, nb_chapters);
415 if (!p)
416 return AVERROR(ENOMEM);
417 oggstream->header[1] = p;
418 bytestream_put_buffer(&p, "OpusTags", 8);
419
420 return 0;
421}
422
423#define VP8_HEADER_SIZE 26
424
426 OGGStreamContext *oggstream, int bitexact)
427{
428 AVCodecParameters *par = st->codecpar;
429 uint8_t *p;
430
431 /* first packet: VP8 header */
433 if (!p)
434 return AVERROR(ENOMEM);
435 oggstream->header[0] = p;
436 oggstream->header_len[0] = VP8_HEADER_SIZE;
437 bytestream_put_byte(&p, 0x4f); // HDRID
438 bytestream_put_buffer(&p, "VP80", 4); // Identifier
439 bytestream_put_byte(&p, 1); // HDRTYP
440 bytestream_put_byte(&p, 1); // VMAJ
441 bytestream_put_byte(&p, 0); // VMIN
442 bytestream_put_be16(&p, par->width);
443 bytestream_put_be16(&p, par->height);
444 bytestream_put_be24(&p, par->sample_aspect_ratio.num);
445 bytestream_put_be24(&p, par->sample_aspect_ratio.den);
446 if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
447 // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
448 // multiple framerate if available.
449 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
450 st->time_base.num, st->time_base.den,
453 }
454 bytestream_put_be32(&p, st->time_base.den);
455 bytestream_put_be32(&p, st->time_base.num);
456
457 /* optional second packet: VorbisComment */
458 if (av_dict_count(st->metadata)) {
459 p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0, NULL, 0);
460 if (!p)
461 return AVERROR(ENOMEM);
462 oggstream->header[1] = p;
463 bytestream_put_byte(&p, 0x4f); // HDRID
464 bytestream_put_buffer(&p, "VP80", 4); // Identifier
465 bytestream_put_byte(&p, 2); // HDRTYP
466 bytestream_put_byte(&p, 0x20);
467 }
468
469 oggstream->isvp8 = 1;
470
471 return 0;
472}
473
475{
476 OGGContext *ogg = s->priv_data;
477 OGGPageList *next, *p;
478
479 if (!ogg->page_list)
480 return;
481
482 for (p = ogg->page_list; p; ) {
483 OGGStreamContext *oggstream =
484 s->streams[p->page.stream_index]->priv_data;
485 if (oggstream->page_count < 2 && !flush)
486 break;
487 ogg_write_page(s, &p->page,
488 flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
489 next = p->next;
490 av_freep(&p);
491 p = next;
492 }
493 ogg->page_list = p;
494}
495
496/**
497 * Derive a bitexact serial number from the stream's header packets, which
498 * makes links muxed apart unlikely to share one, though not certain not to.
499 */
500static unsigned ogg_bitexact_serial(const OGGStreamContext *oggstream, unsigned seed)
501{
503 uint32_t crc = seed;
504
505 for (int i = 0; i < 3; i++)
506 if (oggstream->header_len[i])
507 crc = av_crc(table, crc, oggstream->header[i], oggstream->header_len[i]);
508
509 return crc;
510}
511
512static int ogg_serial_in_use(AVFormatContext *s, int nb_streams, unsigned serial_num)
513{
514 for (int i = 0; i < nb_streams; i++) {
515 const OGGStreamContext *oggstream = s->streams[i]->priv_data;
516 if (oggstream->serial_num == serial_num)
517 return 1;
518 }
519 return 0;
520}
521
522// This function can be used on an initialized context to reinitialize the
523// streams.
525{
526 OGGContext *ogg = s->priv_data;
527 OGGStreamContext *oggstream = NULL;
528 int i;
529
530 for (i = 0; i < s->nb_streams; i++) {
531 AVStream *st = s->streams[i];
532 unsigned serial_num;
533
536 /* Opus requires a fixed 48kHz clock */
537 avpriv_set_pts_info(st, 64, 1, 48000);
538 else
540 }
541
548 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
549 return AVERROR(EINVAL);
550 }
551
552 if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
554 av_log(s, AV_LOG_ERROR, "No extradata present\n");
555 return AVERROR_INVALIDDATA;
556 }
557 oggstream = st->priv_data;
558
559 if (!oggstream) {
560 oggstream = av_mallocz(sizeof(*oggstream));
561 if (!oggstream)
562 return AVERROR(ENOMEM);
563 st->priv_data = oggstream;
564 } else {
566 memset(oggstream, 0, sizeof(*oggstream));
567 }
568
569 oggstream->page.stream_index = i;
570
572
573 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC) {
574 int err = ogg_build_flac_headers(st->codecpar, oggstream,
575 s->flags & AVFMT_FLAG_BITEXACT,
576 &st->metadata);
577 if (err) {
578 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
579 return err;
580 }
581 } else if (st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
582 int err = ogg_build_speex_headers(st->codecpar, oggstream,
583 s->flags & AVFMT_FLAG_BITEXACT,
584 &st->metadata);
585 if (err) {
586 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
587 return err;
588 }
589 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
590 int err = ogg_build_opus_headers(st->codecpar, oggstream,
591 s->flags & AVFMT_FLAG_BITEXACT,
592 &st->metadata, s->chapters, s->nb_chapters);
593 if (err) {
594 av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
595 return err;
596 }
597 } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
598 int err = ogg_build_vp8_headers(s, st, oggstream,
599 s->flags & AVFMT_FLAG_BITEXACT);
600 if (err) {
601 av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
602 return err;
603 }
604 } else {
605 uint8_t *p;
606 const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
607 int header_type = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
608 int framing_bit = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
609
611 st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
612 (const uint8_t**)oggstream->header, oggstream->header_len) < 0) {
613 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
614 oggstream->header[1] = NULL;
615 return AVERROR_INVALIDDATA;
616 }
617
619 &oggstream->header_len[1], &st->metadata,
620 framing_bit, NULL, 0);
621 oggstream->header[1] = p;
622 if (!p)
623 return AVERROR(ENOMEM);
624
625 bytestream_put_byte(&p, header_type);
626 bytestream_put_buffer(&p, cstr, 6);
627
629 int den = AV_RB32(oggstream->header[0] + 22), num = AV_RB32(oggstream->header[0] + 26);
630 /* Make sure to use time base stored in the Theora stream header to write
631 correct timestamps */
632 if (st->time_base.num != num || st->time_base.den != den) {
633 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
634 st->time_base.num, st->time_base.den, num, den);
635 avpriv_set_pts_info(st, 64, num, den);
636 }
637 /** KFGSHIFT is the width of the less significant section of the granule position
638 The less significant section is the frame count since the last keyframe */
639 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
640 oggstream->vrev = oggstream->header[0][9];
641 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
642 oggstream->kfgshift, oggstream->vrev);
643 }
644 }
645
646 if (s->flags & AVFMT_FLAG_BITEXACT && ogg->serial_offset >= 0)
647 serial_num = i + ogg->serial_offset;
648 else if (s->flags & AVFMT_FLAG_BITEXACT)
649 serial_num = ogg_bitexact_serial(oggstream, i);
650 else
651 serial_num = av_get_random_seed();
652 while (ogg_serial_in_use(s, i, serial_num))
653 serial_num = s->flags & AVFMT_FLAG_BITEXACT ? serial_num + 1
655 oggstream->serial_num = serial_num;
656 }
657
658 return 0;
659}
660
662{
663 OGGStreamContext *oggstream = NULL;
664 int i, j, ret;
665
666 for (j = 0; j < s->nb_streams; j++) {
667 oggstream = s->streams[j]->priv_data;
668 ret = ogg_buffer_data(s, s->streams[j], oggstream->header[0],
669 oggstream->header_len[0], 0, 1);
670 if (ret < 0)
671 return ret;
672 oggstream->page.flags |= 2; // bos
673 ret = ogg_buffer_page(s, oggstream);
674 if (ret < 0)
675 return ret;
676 }
677 for (j = 0; j < s->nb_streams; j++) {
678 AVStream *st = s->streams[j];
679 oggstream = st->priv_data;
680 for (i = 1; i < 3; i++) {
681 if (oggstream->header_len[i]) {
682 ret = ogg_buffer_data(s, st, oggstream->header[i],
683 oggstream->header_len[i], 0, 1);
684 if (ret < 0)
685 return ret;
686 }
687 }
688 ret = ogg_buffer_page(s, oggstream);
689 if (ret < 0)
690 return ret;
691 }
692
693 oggstream->page.start_granule = AV_NOPTS_VALUE;
694
695 ogg_write_pages(s, 2);
696
697 return 0;
698}
699
701{
702 int ret = 0;
703 size_t size;
704 OGGContext *oggcontext = s->priv_data;
705 AVStream *st = s->streams[pkt->stream_index];
706 OGGStreamContext *oggstream = st->priv_data;
707 const uint8_t *side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
708
709 if (!side_metadata)
710 return 0;
711
712 // Don't restart on first packet.
713 if (!oggstream->packet_seen)
714 return 0;
715
716 if (s->nb_streams > 1) {
717 av_log(s, AV_LOG_WARNING, "Multiple streams present: cannot insert new metadata!\n");
718 return 0;
719 }
720
724 av_log(s, AV_LOG_WARNING, "Inserting metadata is only supported for vorbis, flac and opus streams!\n");
725 return 0;
726 }
727
728 ret = ogg_write_trailer(s);
729 if (ret < 0)
730 goto end;
731
733 ret = av_packet_unpack_dictionary(side_metadata, size, &st->metadata);
734 if (ret < 0)
735 goto end;
736
737 ret = ogg_init(s);
738 if (ret < 0)
739 goto end;
740
741 ret = ogg_write_header(s);
742
743end:
744 oggcontext->failed = ret < 0;
745 return ret;
746}
747
749{
750 AVStream *st = s->streams[pkt->stream_index];
751 OGGContext *oggcontext = s->priv_data;
752 OGGStreamContext *oggstream = st->priv_data;
753 int ret;
754 int64_t granule;
755
756 if (oggcontext->failed)
757 return AVERROR_INVALIDDATA;
758
760 if (ret < 0)
761 return ret;
762
764 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
765 int pframe_count;
766 if (pkt->flags & AV_PKT_FLAG_KEY)
767 oggstream->last_kf_pts = pts;
768 pframe_count = pts - oggstream->last_kf_pts;
769 // prevent frame count from overflow if key frame flag is not set
770 if (pframe_count >= (1<<oggstream->kfgshift)) {
771 oggstream->last_kf_pts += pframe_count;
772 pframe_count = 0;
773 }
774 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
775 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
776 granule = pkt->pts + pkt->duration +
778 (AVRational){ 1, st->codecpar->sample_rate },
779 st->time_base);
780 else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
781 int64_t pts, invcnt, dist;
782 int visible;
783
784 visible = (pkt->data[0] >> 4) & 1;
785 pts = pkt->pts + pkt->duration;
786 invcnt = (oggstream->last_granule >> 30) & 3;
787 invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
788 dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
789
790 granule = (pts << 32) | (invcnt << 30) | (dist << 3);
791 } else
792 granule = pkt->pts + pkt->duration;
793
794 if (oggstream->page.start_granule == AV_NOPTS_VALUE)
795 oggstream->page.start_granule = pkt->pts;
796
797 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
798 if (ret < 0)
799 return ret;
800
801 ogg_write_pages(s, 0);
802
803 oggstream->last_granule = granule;
804 oggstream->packet_seen = 1;
805
806 return 0;
807}
808
810{
811 int i, ret;
812
813 if (pkt)
814 return pkt->size || !pkt->side_data_elems ? ogg_write_packet_internal(s, pkt) : 0;
815
816 for (i = 0; i < s->nb_streams; i++) {
817 OGGStreamContext *oggstream = s->streams[i]->priv_data;
818 if (oggstream->page.segments_count &&
819 (ret = ogg_buffer_page(s, oggstream)) < 0)
820 return ret;
821 }
822
823 ogg_write_pages(s, 2);
824 return 1;
825}
826
828{
829 int i, ret;
830
831 /* flush current page if needed */
832 for (i = 0; i < s->nb_streams; i++) {
833 OGGStreamContext *oggstream = s->streams[i]->priv_data;
834
835 if (oggstream->page.segments_count &&
836 (ret = ogg_buffer_page(s, oggstream)) < 0)
837 return ret;
838 }
839
840 ogg_write_pages(s, 1);
841
842 return 0;
843}
844
846{
847 OGGContext *ogg = s->priv_data;
848 OGGPageList *p = ogg->page_list;
849 int i;
850
851 for (i = 0; i < s->nb_streams; i++) {
852 AVStream *st = s->streams[i];
853 if (st->priv_data)
855 }
856
857 while (p) {
858 OGGPageList *next = p->next;
859 av_free(p);
860 p = next;
861 }
862 ogg->page_list = NULL;
863}
864
865#if CONFIG_OGG_MUXER
867 .p.name = "ogg",
868 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg"),
869 .p.mime_type = "application/ogg",
870 .p.extensions = "ogg"
871#if !CONFIG_OGV_MUXER
872 ",ogv"
873#endif
874#if !CONFIG_SPX_MUXER
875 ",spx"
876#endif
877#if !CONFIG_OPUS_MUXER
878 ",opus"
879#endif
880 ,
881 .priv_data_size = sizeof(OGGContext),
882 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
884 .p.video_codec = AV_CODEC_ID_THEORA,
885 .init = ogg_init,
889 .deinit = ogg_free,
891 .p.priv_class = &ogg_muxer_class,
892 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
893};
894#endif
895
896#if CONFIG_OGA_MUXER
898 .p.name = "oga",
899 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
900 .p.mime_type = "audio/ogg",
901 .p.extensions = "oga",
902 .priv_data_size = sizeof(OGGContext),
903 .p.audio_codec = AV_CODEC_ID_FLAC,
904 .init = ogg_init,
905 .write_header = ogg_write_header,
906 .write_packet = ogg_write_packet,
907 .write_trailer = ogg_write_trailer,
908 .deinit = ogg_free,
909 .p.flags = AVFMT_TS_NEGATIVE,
910 .p.priv_class = &ogg_muxer_class,
911 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
912};
913#endif
914
915#if CONFIG_OGV_MUXER
917 .p.name = "ogv",
918 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Video"),
919 .p.mime_type = "video/ogg",
920 .p.extensions = "ogv",
921 .priv_data_size = sizeof(OGGContext),
922 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
924 .p.video_codec = CONFIG_LIBTHEORA_ENCODER ?
926 .init = ogg_init,
930 .deinit = ogg_free,
932 .p.priv_class = &ogg_muxer_class,
933 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
934};
935#endif
936
937#if CONFIG_SPX_MUXER
939 .p.name = "spx",
940 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
941 .p.mime_type = "audio/ogg",
942 .p.extensions = "spx",
943 .priv_data_size = sizeof(OGGContext),
944 .p.audio_codec = AV_CODEC_ID_SPEEX,
945 .init = ogg_init,
946 .write_header = ogg_write_header,
947 .write_packet = ogg_write_packet,
948 .write_trailer = ogg_write_trailer,
949 .deinit = ogg_free,
950 .p.flags = AVFMT_TS_NEGATIVE,
951 .p.priv_class = &ogg_muxer_class,
952 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
953};
954#endif
955
956#if CONFIG_OPUS_MUXER
958 .p.name = "opus",
959 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
960 .p.mime_type = "audio/ogg",
961 .p.extensions = "opus",
962 .priv_data_size = sizeof(OGGContext),
963 .p.audio_codec = AV_CODEC_ID_OPUS,
964 .init = ogg_init,
965 .write_header = ogg_write_header,
966 .write_packet = ogg_write_packet,
967 .write_trailer = ogg_write_trailer,
968 .deinit = ogg_free,
969 .p.flags = AVFMT_TS_NEGATIVE,
970 .p.priv_class = &ogg_muxer_class,
971 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
972};
973#endif
const FFOutputFormat ff_ogv_muxer
const FFOutputFormat ff_ogg_muxer
const FFOutputFormat ff_opus_muxer
const FFOutputFormat ff_spx_muxer
const FFOutputFormat ff_oga_muxer
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:856
Main libavformat public API header.
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition avformat.h:1569
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition avformat.h:512
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:509
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition aviobuf.c:464
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition avio.h:145
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for writing.
Definition aviobuf.c:104
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition bytestream.h:372
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static void deinit(AVFormatContext *s)
Definition chromaprint.c:53
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
#define PARAM
Definition dcadec.c:448
static AVPacket * pkt
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:205
static unsigned int nb_streams
Definition ffprobe.c:390
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
FLAC (Free Lossless Audio Codec) common stuff.
#define FLAC_STREAMINFO_SIZE
Definition flac.h:32
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:462
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_THEORA
Definition codec_id.h:80
@ AV_CODEC_ID_FLAC
Definition codec_id.h:469
@ AV_CODEC_ID_SPEEX
Definition codec_id.h:492
@ AV_CODEC_ID_OPUS
Definition codec_id.h:517
@ AV_PKT_DATA_STRINGS_METADATA
A list of zero terminated key/value strings.
Definition packet.h:169
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
int av_packet_unpack_dictionary(const uint8_t *data, size_t size, AVDictionary **dict)
Unpack a dictionary from side_data.
Definition packet.c:355
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_32_IEEE
Definition crc.h:52
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition dict.h:81
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:38
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_WL32(p, v)
#define AV_RB32(p)
unsigned offset
Definition libaomenc.c:763
Libavformat version macros.
#define LIBAVFORMAT_IDENT
Definition version.h:45
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:26
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition mux.h:38
const char data[16]
Definition mxf.c:149
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
static uint8_t page[OGG_PAGE_MAX_SIZE]
Definition ogg_pages.c:29
#define MAX_PAGE_SIZE
Definition oggdec.c:41
#define OPUS_HEADER_SIZE
Definition oggenc.c:393
static int ogg_write_header(AVFormatContext *s)
Definition oggenc.c:661
static int ogg_build_opus_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m, AVChapter **chapters, unsigned int nb_chapters)
Definition oggenc.c:395
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
Definition oggenc.c:164
static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st, OGGStreamContext *oggstream, int bitexact)
Definition oggenc.c:425
static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition oggenc.c:748
static void ogg_cleanup_stream(AVStream *st)
Definition oggenc.c:105
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
Definition oggenc.c:189
static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
Definition oggenc.c:119
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
Definition oggenc.c:180
static const AVClass ogg_muxer_class
Definition oggenc.c:98
static int ogg_serial_in_use(AVFormatContext *s, int nb_streams, unsigned serial_num)
Definition oggenc.c:512
static int ogg_build_flac_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition oggenc.c:328
#define SPEEX_HEADER_SIZE
Definition oggenc.c:364
static unsigned ogg_bitexact_serial(const OGGStreamContext *oggstream, unsigned seed)
Derive a bitexact serial number from the stream's header packets, which makes links muxed apart unlik...
Definition oggenc.c:500
#define VP8_HEADER_SIZE
Definition oggenc.c:423
#define MAX_PAGE_SIZE
Definition oggenc.c:41
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition oggenc.c:809
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
Definition oggenc.c:153
static int ogg_check_new_metadata(AVFormatContext *s, AVPacket *pkt)
Definition oggenc.c:700
static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
Definition oggenc.c:147
static int ogg_build_speex_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition oggenc.c:366
static void ogg_free(AVFormatContext *s)
Definition oggenc.c:845
static void ogg_write_pages(AVFormatContext *s, int flush)
Definition oggenc.c:474
static int ogg_init(AVFormatContext *s)
Definition oggenc.c:524
static int ogg_write_trailer(AVFormatContext *s)
Definition oggenc.c:827
static uint8_t * ogg_write_vorbiscomment(int64_t offset, int bitexact, int *header_len, AVDictionary **m, int framing_bit, AVChapter **chapters, unsigned int nb_chapters)
Definition oggenc.c:298
#define OFFSET(x)
Definition oggenc.c:85
static int ogg_buffer_data(AVFormatContext *s, AVStream *st, const uint8_t *data, unsigned size, int64_t granule, int header)
Definition oggenc.c:216
AVOptions.
static const uint16_t table[]
Definition prosumer.c:203
static const uint8_t header[24]
Definition sdr2.c:68
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
AVRational sample_aspect_ratio
The aspect ratio (width/height) which a single pixel should have when displayed.
Definition codec_par.h:161
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
int initial_padding
Number of padding audio samples at the start.
Definition codec_par.h:239
Format I/O context.
Definition avformat.h:1400
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVDictionary * metadata
Definition avformat.h:848
void * priv_data
Definition avformat.h:793
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:902
AVIOContext pub
int64_t pref_duration
preferred page duration (0 => fill all segments)
Definition oggenc.c:80
int serial_offset
Definition oggenc.c:81
int failed
Definition oggenc.c:82
OGGPageList * page_list
Definition oggenc.c:79
OGGPage page
Definition oggenc.c:73
struct OGGPageList * next
Definition oggenc.c:74
uint8_t data[MAX_PAGE_SIZE]
Definition oggenc.c:50
uint16_t size
Definition oggenc.c:51
int stream_index
Definition oggenc.c:46
uint8_t segments_count
Definition oggenc.c:48
uint8_t segments[255]
Definition oggenc.c:49
uint8_t flags
Definition oggenc.c:47
int64_t start_granule
Definition oggenc.c:44
int64_t granule
Definition oggenc.c:45
int kfgshift
for theora granule
Definition oggenc.c:59
unsigned serial_num
serial number
Definition oggenc.c:67
unsigned page_count
number of page buffered
Definition oggenc.c:65
int64_t last_kf_pts
Definition oggenc.c:60
uint8_t * header[3]
Definition oggenc.c:56
unsigned page_counter
Definition oggenc.c:55
int packet_seen
true when packets have been submitted
Definition oggenc.c:69
OGGPage page
current page
Definition oggenc.c:66
int64_t last_granule
last packet granule
Definition oggenc.c:68
int header_len[3]
Definition oggenc.c:57
Definition oggdec.h:112
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
int num
Definition error.c:23
static int64_t pts
int size
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
static unsigned int seed
Definition videogen.c:78
int len
int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *dict, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into an AVIOContext.
int ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
const AVMetadataConv ff_vorbiscomment_metadata_conv[]
VorbisComment metadata conversion mapping.
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26