FFmpeg
Loading...
Searching...
No Matches
muxer.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"
32#include "libavcodec/xiph.h"
37#include "libavformat/mux.h"
38#include "libavformat/version.h"
40#include "muxer.h"
41
42typedef struct OGGPageList {
46
47typedef struct OGGContext {
48 const AVClass *class;
50 int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
52 int failed; // if true all packet submission will fail.
53 int ended; ///< a new link could not be started, the packets that follow are dropped
55
56#define OFFSET(x) offsetof(OGGContext, x)
57#define PARAM AV_OPT_FLAG_ENCODING_PARAM
58
60
61static const AVOption options[] = {
62 { "serial_offset", "serial number offset, -1 to derive it from the stream headers",
63 OFFSET(serial_offset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, PARAM },
64 { "page_duration", "preferred page duration, in microseconds",
65 OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
66 { NULL },
67};
68
69static const AVClass ogg_muxer_class = {
70 .class_name = "Ogg (audio/video/Speex/Opus) muxer",
71 .item_name = av_default_item_name,
72 .option = options,
73 .version = LIBAVUTIL_VERSION_INT,
74};
75
77{
78 OGGStreamContext *oggstream = st->priv_data;
79
80 for (int i = 0; i < 3; i++)
81 av_freep(&oggstream->header[i]);
82 av_freep(&oggstream->priv_data);
83}
84
85static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
86{
87 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
88 uint8_t buf[4 + 1 + 1 + 8 + 4 + 4 + 4 + 1 + 255], *ptr = buf, *crc_pos;
89 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE);
90 uint32_t crc;
91
92 bytestream_put_le32(&ptr, MKTAG('O', 'g', 'g', 'S'));
93 bytestream_put_byte(&ptr, 0);
94 bytestream_put_byte(&ptr, page->flags | extra_flags);
95 bytestream_put_le64(&ptr, page->granule);
96 bytestream_put_le32(&ptr, oggstream->serial_num);
97 bytestream_put_le32(&ptr, oggstream->page_counter++);
98 crc_pos = ptr;
99 bytestream_put_le32(&ptr, 0);
100 bytestream_put_byte(&ptr, page->segments_count);
101 bytestream_put_buffer(&ptr, page->segments, page->segments_count);
102
103 crc = av_crc(crc_table, 0, buf, ptr - buf);
104 crc = av_crc(crc_table, crc, page->data, page->size);
105 bytestream_put_be32(&crc_pos, crc);
106
107 avio_write(s->pb, buf, ptr - buf);
108 avio_write(s->pb, page->data, page->size);
110 oggstream->page_count--;
111}
112
113static int ogg_key_granule(const OGGStreamContext *oggstream, int64_t granule)
114{
115 return oggstream->granule_key_mask && !(granule & oggstream->granule_key_mask);
116}
117
119{
120 return (granule >> oggstream->granule_shift) + (granule & oggstream->granule_delta_mask);
121}
122
124{
125 AVStream *st2 = s->streams[next->stream_index];
126 AVStream *st = s->streams[page->stream_index];
127 int64_t next_granule, cur_granule;
128
129 if (next->granule == -1 || page->granule == -1)
130 return 0;
131
132 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
134 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
136 return next_granule > cur_granule;
137}
138
140{
141 oggstream->page.granule = -1;
142 oggstream->page.flags = 0;
143 oggstream->page.segments_count = 0;
144 oggstream->page.size = 0;
145 return 0;
146}
147
149{
150 OGGContext *ogg = s->priv_data;
151 OGGPageList **p = &ogg->page_list;
152 OGGPageList *l = av_mallocz(sizeof(*l));
153
154 if (!l) {
155 ogg_reset_cur_page(oggstream);
156 return AVERROR(ENOMEM);
157 }
158 l->page = oggstream->page;
159
160 oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule);
161 oggstream->page_count++;
162 ogg_reset_cur_page(oggstream);
163
164 while (*p) {
165 if (ogg_compare_granule(s, &(*p)->page, &l->page))
166 break;
167 p = &(*p)->next;
168 }
169 l->next = *p;
170 *p = l;
171
172 return 0;
173}
174
176 const uint8_t *data, unsigned size, int64_t granule,
177 int header)
178{
179 OGGStreamContext *oggstream = st->priv_data;
180 OGGContext *ogg = s->priv_data;
181 int total_segments = size / 255 + 1;
182 const uint8_t *p = data;
183 int i, segments, len, ret, flush = 0;
184
185 // Handles VFR by flushing page because this frame needs to have a timestamp
186 // For theora and VP8, keyframes also need to have a timestamp to correctly mark
187 // them as such, otherwise seeking will not work correctly at the very
188 // least with old libogg versions.
189 // Do not try to flush header packets though, that will create broken files.
191 (ogg_granule_to_timestamp(oggstream, granule) >
192 ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
193 ogg_key_granule(oggstream, granule))) {
194 if (oggstream->page.granule != -1 &&
195 (ret = ogg_buffer_page(s, oggstream)) < 0)
196 return ret;
197 flush = 1;
198 }
199
200 // avoid a continued page
201 if (!header && oggstream->page.size > 0 &&
202 MAX_PAGE_SIZE - oggstream->page.size < size) {
203 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
204 return ret;
205 }
206
207 for (i = 0; i < total_segments; ) {
208 OGGPage *page = &oggstream->page;
209
210 segments = FFMIN(total_segments - i, 255 - page->segments_count);
211
212 if (i && !page->segments_count)
213 page->flags |= 1; // continued packet
214
215 memset(page->segments+page->segments_count, 255, segments - 1);
216 page->segments_count += segments - 1;
217
218 len = FFMIN(size, segments*255);
219 page->segments[page->segments_count++] = len - (segments-1)*255;
220 if (len)
221 memcpy(page->data+page->size, p, len);
222 p += len;
223 size -= len;
224 i += segments;
225 page->size += len;
226
227 if (i == total_segments)
228 page->granule = granule;
229
230 {
231 AVRational time_base = s->streams[page->stream_index]->time_base;
232
233 int64_t start = av_rescale_q(page->start_granule, time_base,
235 int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule),
236 time_base, AV_TIME_BASE_Q);
237
238 if (page->segments_count == 255) {
239 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
240 return ret;
241 } else if (!header) {
242 if (ogg->pref_duration > 0 && next - start >= ogg->pref_duration) {
243 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
244 return ret;
245 }
246 }
247 }
248 }
249
250 if (flush && oggstream->page.granule != -1 &&
251 (ret = ogg_buffer_page(s, oggstream)) < 0)
252 return ret;
253
254 return 0;
255}
256
258 int *header_len, AVDictionary **m, int framing_bit,
259 AVChapter **chapters, unsigned int nb_chapters)
260{
261 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
262 FFIOContext pb;
264 uint8_t *p;
265
267
268 size = ff_vorbis_comment_length(*m, vendor, chapters, nb_chapters);
269 if (size < 0)
270 return NULL;
271 size += offset + framing_bit;
272 if (size > INT_MAX)
273 return NULL;
274 p = av_mallocz(size);
275 if (!p)
276 return NULL;
277
279 ff_vorbis_comment_write(&pb.pub, *m, vendor, chapters, nb_chapters);
280 if (framing_bit)
281 avio_w8(&pb.pub, 1);
282
283 *header_len = size;
284 return p;
285}
286
288 int first_header_size, int comment_type,
289 const char *name, int framing_bit)
290{
291 OGGStreamContext *oggstream = st->priv_data;
292 const uint8_t *header[3];
293 uint8_t *p;
294
296 first_header_size, header, oggstream->header_len) < 0 ||
297 oggstream->header_len[0] < first_header_size) {
298 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
299 return AVERROR_INVALIDDATA;
300 }
301
302 oggstream->header[0] = av_memdup(header[0], oggstream->header_len[0]);
303 oggstream->header[2] = av_memdup(header[2], oggstream->header_len[2]);
304 if (!oggstream->header[0] || !oggstream->header[2])
305 return AVERROR(ENOMEM);
306
308 &oggstream->header_len[1], &st->metadata,
309 framing_bit, NULL, 0);
310 oggstream->header[1] = p;
311 if (!p)
312 return AVERROR(ENOMEM);
313
314 bytestream_put_byte(&p, comment_type);
316
317 return 0;
318}
319
328
330{
331 for (int i = 0; i < FF_ARRAY_ELEMS(ogg_mappings); i++)
333 return ogg_mappings[i];
334 return NULL;
335}
336
338{
339 OGGContext *ogg = s->priv_data;
340 OGGPageList *next, *p;
341
342 if (!ogg->page_list)
343 return;
344
345 for (p = ogg->page_list; p; ) {
346 OGGStreamContext *oggstream =
347 s->streams[p->page.stream_index]->priv_data;
348 if (oggstream->page_count < 2 && !flush)
349 break;
350 ogg_write_page(s, &p->page,
351 flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
352 next = p->next;
353 av_freep(&p);
354 p = next;
355 }
356 ogg->page_list = p;
357}
358
359/**
360 * Derive a bitexact serial number from the stream's header packets, which
361 * makes links muxed apart unlikely to share one, though not certain not to.
362 */
363static unsigned ogg_bitexact_serial(const OGGStreamContext *oggstream, unsigned seed)
364{
366 uint32_t crc = seed;
367
368 for (int i = 0; i < 3; i++)
369 if (oggstream->header_len[i])
370 crc = av_crc(table, crc, oggstream->header[i], oggstream->header_len[i]);
371
372 return crc;
373}
374
375static int ogg_serial_in_use(AVFormatContext *s, int nb_streams, unsigned serial_num)
376{
377 for (int i = 0; i < nb_streams; i++) {
378 const OGGStreamContext *oggstream = s->streams[i]->priv_data;
379 if (oggstream->serial_num == serial_num)
380 return 1;
381 }
382 return 0;
383}
384
385// This function can be used on an initialized context to reinitialize the
386// streams.
388{
389 OGGContext *ogg = s->priv_data;
390 OGGStreamContext *oggstream = NULL;
391 int i;
392
393 for (i = 0; i < s->nb_streams; i++) {
394 AVStream *st = s->streams[i];
395 unsigned serial_num;
396
397 const OGGMuxerMapping *mapping = ogg_find_mapping(st->codecpar->codec_id);
398 int err;
399
402
403 if (!mapping) {
404 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
405 return AVERROR(EINVAL);
406 }
407
408 if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
409 !mapping->optional_extradata) {
410 av_log(s, AV_LOG_ERROR, "No extradata present\n");
411 return AVERROR_INVALIDDATA;
412 }
413 oggstream = st->priv_data;
414
415 if (!oggstream) {
416 oggstream = av_mallocz(sizeof(*oggstream));
417 if (!oggstream)
418 return AVERROR(ENOMEM);
419 st->priv_data = oggstream;
420 } else {
422 memset(oggstream, 0, sizeof(*oggstream));
423 }
424
425 oggstream->page.stream_index = i;
426 oggstream->mapping = mapping;
427 if (mapping->priv_data_size) {
428 oggstream->priv_data = av_mallocz(mapping->priv_data_size);
429 if (!oggstream->priv_data)
430 return AVERROR(ENOMEM);
431 }
432
434
435 err = mapping->init(s, st);
436 if (err < 0)
437 return err;
438
439 if (s->flags & AVFMT_FLAG_BITEXACT && ogg->serial_offset >= 0)
440 serial_num = i + ogg->serial_offset;
441 else if (s->flags & AVFMT_FLAG_BITEXACT)
442 serial_num = ogg_bitexact_serial(oggstream, i);
443 else
444 serial_num = av_get_random_seed();
445 while (ogg_serial_in_use(s, i, serial_num))
446 serial_num = s->flags & AVFMT_FLAG_BITEXACT ? serial_num + 1
448 oggstream->serial_num = serial_num;
449 }
450
451 return 0;
452}
453
455{
456 OGGStreamContext *oggstream = NULL;
457 int i, j, ret;
458
459 for (j = 0; j < s->nb_streams; j++) {
460 oggstream = s->streams[j]->priv_data;
461 ret = ogg_buffer_data(s, s->streams[j], oggstream->header[0],
462 oggstream->header_len[0], 0, 1);
463 if (ret < 0)
464 return ret;
465 oggstream->page.flags |= 2; // bos
466 ret = ogg_buffer_page(s, oggstream);
467 if (ret < 0)
468 return ret;
469 }
470 for (j = 0; j < s->nb_streams; j++) {
471 AVStream *st = s->streams[j];
472 oggstream = st->priv_data;
473 for (i = 1; i < 3; i++) {
474 if (oggstream->header_len[i]) {
475 ret = ogg_buffer_data(s, st, oggstream->header[i],
476 oggstream->header_len[i], 0, 1);
477 if (ret < 0)
478 return ret;
479 }
480 }
481 ret = ogg_buffer_page(s, oggstream);
482 if (ret < 0)
483 return ret;
484 }
485
486 oggstream->page.start_granule = AV_NOPTS_VALUE;
487
488 ogg_write_pages(s, 2);
489
490 return 0;
491}
492
493/**
494 * Keep the current link when a new one cannot be started. New metadata is
495 * then dropped, while new extradata ends the output, as the packets that
496 * follow need its headers.
497 */
498static int ogg_skip_new_link(AVFormatContext *s, const uint8_t *new_extradata,
499 const char *reason)
500{
501 OGGContext *ogg = s->priv_data;
502
503 if (!new_extradata) {
504 av_log(s, AV_LOG_WARNING, "%s: new metadata ignored\n", reason);
505 return 0;
506 }
507
508 av_log(s, AV_LOG_WARNING, "%s: ending the output at the current link\n", reason);
509 ogg->ended = 1;
510 return 0;
511}
512
514{
515 int ret = 0;
516 const char *refused;
517 size_t metadata_size, extradata_size;
518 OGGContext *oggcontext = s->priv_data;
519 AVStream *st = s->streams[pkt->stream_index];
520 AVCodecParameters *par = st->codecpar;
521 OGGStreamContext *oggstream = st->priv_data;
522 const uint8_t *side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &metadata_size);
523 const uint8_t *new_extradata = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size);
524
525 if (new_extradata && extradata_size == par->extradata_size &&
526 !memcmp(new_extradata, par->extradata, extradata_size))
527 new_extradata = NULL;
528
529 if (!side_metadata && !new_extradata)
530 return 0;
531
532 /* The headers of the first link are written from codecpar before any packet. */
533 if (!oggstream->packet_seen) {
534 if (new_extradata)
535 av_log(s, AV_LOG_WARNING, "New extradata on the first packet ignored: "
536 "the stream headers are already written!\n");
537 return 0;
538 }
539
540 if (s->nb_streams > 1)
541 return ogg_skip_new_link(s, new_extradata, "Multiple streams present");
542
543 if (!oggstream->mapping->new_links)
544 return ogg_skip_new_link(s, new_extradata,
545 "This codec does not support starting a new Ogg link");
546
547 if (oggstream->mapping->new_link_refused &&
548 (refused = oggstream->mapping->new_link_refused(st, pkt, new_extradata, extradata_size)))
549 return ogg_skip_new_link(s, new_extradata, refused);
550
552 if (ret < 0)
553 goto end;
554
555 if (side_metadata) {
557 ret = av_packet_unpack_dictionary(side_metadata, metadata_size, &st->metadata);
558 if (ret < 0)
559 goto end;
560 }
561
562 if (new_extradata) {
563 if (extradata_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
565 goto end;
566 }
567 ret = ff_alloc_extradata(par, extradata_size);
568 if (ret < 0)
569 goto end;
570 memcpy(par->extradata, new_extradata, extradata_size);
571 }
572
573 ret = ogg_init(s);
574 if (ret < 0)
575 goto end;
576
578
579end:
580 oggcontext->failed = ret < 0;
581 return ret;
582}
583
585{
586 AVStream *st = s->streams[pkt->stream_index];
587 OGGContext *oggcontext = s->priv_data;
588 OGGStreamContext *oggstream = st->priv_data;
589 int ret;
590 int64_t granule;
591
592 if (oggcontext->failed)
593 return AVERROR_INVALIDDATA;
594 if (oggcontext->ended)
595 return 0;
596
599 return ret;
600
601 if (oggstream->mapping->packet_granule)
602 granule = oggstream->mapping->packet_granule(st, pkt);
603 else
604 granule = pkt->pts + pkt->duration + oggstream->packet_granule_offset;
605
606 if (oggstream->page.start_granule == AV_NOPTS_VALUE)
607 oggstream->page.start_granule = pkt->pts;
608
609 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
610 if (ret < 0)
611 return ret;
612
613 ogg_write_pages(s, 0);
614
615 oggstream->last_granule = granule;
616 oggstream->packet_seen = 1;
617
618 return 0;
619}
620
622{
623 int i, ret;
624
625 if (pkt)
626 return pkt->size || !pkt->side_data_elems ? ogg_write_packet_internal(s, pkt) : 0;
627
628 for (i = 0; i < s->nb_streams; i++) {
629 OGGStreamContext *oggstream = s->streams[i]->priv_data;
630 if (oggstream->page.segments_count &&
631 (ret = ogg_buffer_page(s, oggstream)) < 0)
632 return ret;
633 }
634
635 ogg_write_pages(s, 2);
636 return 1;
637}
638
640{
641 int i, ret;
642
643 /* flush current page if needed */
644 for (i = 0; i < s->nb_streams; i++) {
645 OGGStreamContext *oggstream = s->streams[i]->priv_data;
646
647 if (oggstream->page.segments_count &&
648 (ret = ogg_buffer_page(s, oggstream)) < 0)
649 return ret;
650 }
651
652 ogg_write_pages(s, 1);
653
654 return 0;
655}
656
658{
659 OGGContext *ogg = s->priv_data;
660 OGGPageList *p = ogg->page_list;
661 int i;
662
663 for (i = 0; i < s->nb_streams; i++) {
664 AVStream *st = s->streams[i];
665 if (st->priv_data)
667 }
668
669 while (p) {
670 OGGPageList *next = p->next;
671 av_free(p);
672 p = next;
673 }
674 ogg->page_list = NULL;
675}
676
677#if CONFIG_OGG_MUXER
679 .p.name = "ogg",
680 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg"),
681 .p.mime_type = "application/ogg",
682 .p.extensions = "ogg"
683#if !CONFIG_OGV_MUXER
684 ",ogv"
685#endif
686#if !CONFIG_SPX_MUXER
687 ",spx"
688#endif
689#if !CONFIG_OPUS_MUXER
690 ",opus"
691#endif
692 ,
693 .priv_data_size = sizeof(OGGContext),
694 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
696 .p.video_codec = AV_CODEC_ID_THEORA,
697 .init = ogg_init,
701 .deinit = ogg_free,
703 .p.priv_class = &ogg_muxer_class,
704 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
705};
706#endif
707
708#if CONFIG_OGA_MUXER
710 .p.name = "oga",
711 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
712 .p.mime_type = "audio/ogg",
713 .p.extensions = "oga",
714 .priv_data_size = sizeof(OGGContext),
715 .p.audio_codec = AV_CODEC_ID_FLAC,
716 .init = ogg_init,
717 .write_header = ogg_write_header,
718 .write_packet = ogg_write_packet,
719 .write_trailer = ogg_write_trailer,
720 .deinit = ogg_free,
721 .p.flags = AVFMT_TS_NEGATIVE,
722 .p.priv_class = &ogg_muxer_class,
723 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
724};
725#endif
726
727#if CONFIG_OGV_MUXER
729 .p.name = "ogv",
730 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Video"),
731 .p.mime_type = "video/ogg",
732 .p.extensions = "ogv",
733 .priv_data_size = sizeof(OGGContext),
734 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
736 .p.video_codec = CONFIG_LIBTHEORA_ENCODER ?
738 .init = ogg_init,
742 .deinit = ogg_free,
744 .p.priv_class = &ogg_muxer_class,
745 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
746};
747#endif
748
749#if CONFIG_SPX_MUXER
751 .p.name = "spx",
752 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
753 .p.mime_type = "audio/ogg",
754 .p.extensions = "spx",
755 .priv_data_size = sizeof(OGGContext),
756 .p.audio_codec = AV_CODEC_ID_SPEEX,
757 .init = ogg_init,
758 .write_header = ogg_write_header,
759 .write_packet = ogg_write_packet,
760 .write_trailer = ogg_write_trailer,
761 .deinit = ogg_free,
762 .p.flags = AVFMT_TS_NEGATIVE,
763 .p.priv_class = &ogg_muxer_class,
764 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
765};
766#endif
767
768#if CONFIG_OPUS_MUXER
770 .p.name = "opus",
771 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
772 .p.mime_type = "audio/ogg",
773 .p.extensions = "opus",
774 .priv_data_size = sizeof(OGGContext),
775 .p.audio_codec = AV_CODEC_ID_OPUS,
776 .init = ogg_init,
777 .write_header = ogg_write_header,
778 .write_packet = ogg_write_packet,
779 .write_trailer = ogg_write_trailer,
780 .deinit = ogg_free,
781 .p.flags = AVFMT_TS_NEGATIVE,
782 .p.priv_class = &ogg_muxer_class,
783 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
784};
785#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:865
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:1570
#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
int ff_vorbis_comment_length(const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
Definition writer.c:44
const AVMetadataConv ff_vorbis_comment_metadata_conv[]
VorbisComment metadata conversion mapping.
Definition writer.c:34
int ff_vorbis_comment_write(AVIOContext *pb, const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into an AVIOContext.
Definition writer.c:62
#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:450
static AVPacket * pkt
#define MAX_PAGE_SIZE
Definition demuxer.c:41
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:615
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:203
int ret
Definition ffplay.c:391
static unsigned int nb_streams
Definition ffprobe.c:390
static void write_header(FFV1Context *f)
Definition ffv1enc.c:397
@ 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
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:464
@ 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:471
@ AV_CODEC_ID_SPEEX
Definition codec_id.h:494
@ AV_CODEC_ID_OPUS
Definition codec_id.h:519
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AV_PKT_DATA_STRINGS_METADATA
A list of zero terminated key/value strings.
Definition packet.h:169
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
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
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#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:247
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#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
unsigned offset
Definition libaomenc.c:812
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
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:98
#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
internal metadata API header see avformat.h or the public API!
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition mux.h:38
static int ogg_write_header(AVFormatContext *s)
Definition muxer.c:454
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
Definition muxer.c:123
static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition muxer.c:584
static void ogg_cleanup_stream(AVStream *st)
Definition muxer.c:76
static int ogg_key_granule(const OGGStreamContext *oggstream, int64_t granule)
Definition muxer.c:113
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
Definition muxer.c:148
static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
Definition muxer.c:85
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
Definition muxer.c:139
static const OGGMuxerMapping * ogg_find_mapping(enum AVCodecID codec_id)
Definition muxer.c:329
static const AVClass ogg_muxer_class
Definition muxer.c:69
static int ogg_serial_in_use(AVFormatContext *s, int nb_streams, unsigned serial_num)
Definition muxer.c:375
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 muxer.c:363
static int ogg_skip_new_link(AVFormatContext *s, const uint8_t *new_extradata, const char *reason)
Keep the current link when a new one cannot be started.
Definition muxer.c:498
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition muxer.c:621
static const OGGMuxerMapping *const ogg_mappings[]
Definition muxer.c:320
static int ogg_check_new_link(AVFormatContext *s, AVPacket *pkt)
Definition muxer.c:513
static void ogg_free(AVFormatContext *s)
Definition muxer.c:657
static void ogg_write_pages(AVFormatContext *s, int flush)
Definition muxer.c:337
static int64_t ogg_granule_to_timestamp(const OGGStreamContext *oggstream, int64_t granule)
Definition muxer.c:118
static int ogg_init(AVFormatContext *s)
Definition muxer.c:387
static int ogg_write_trailer(AVFormatContext *s)
Definition muxer.c:639
int ff_ogg_build_xiph_headers(AVFormatContext *s, AVStream *st, int first_header_size, int comment_type, const char *name, int framing_bit)
Build the headers of a codec using the Xiph header layout: identification, comment and setup packets ...
Definition muxer.c:287
#define OFFSET(x)
Definition muxer.c:56
uint8_t * ff_ogg_write_vorbiscomment(int64_t offset, int bitexact, int *header_len, AVDictionary **m, int framing_bit, AVChapter **chapters, unsigned int nb_chapters)
Allocate a comment header packet holding a VorbisComment.
Definition muxer.c:257
static int ogg_buffer_data(AVFormatContext *s, AVStream *st, const uint8_t *data, unsigned size, int64_t granule, int header)
Definition muxer.c:175
const OGGMuxerMapping ff_ogg_speex_muxer_mapping
Definition speex.c:66
const OGGMuxerMapping ff_ogg_theora_muxer_mapping
Definition theora.c:110
const OGGMuxerMapping ff_ogg_vp8_muxer_mapping
Definition vp8.c:110
const OGGMuxerMapping ff_ogg_flac_muxer_mapping
Definition flac.c:73
const OGGMuxerMapping ff_ogg_vorbis_muxer_mapping
Definition vorbis.c:29
const OGGMuxerMapping ff_ogg_opus_muxer_mapping
Definition opus.c:78
const char data[16]
Definition mxf.c:155
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
static uint8_t page[OGG_PAGE_MAX_SIZE]
Definition ogg_pages.c:29
AVOptions.
static const uint16_t table[]
Definition prosumer.c:203
const char * name
Definition qsvenc.c:142
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
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
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
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
Format I/O context.
Definition avformat.h:1401
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
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
AVIOContext pub
int64_t pref_duration
preferred page duration (0 => fill all segments)
Definition muxer.c:50
int serial_offset
Definition muxer.c:51
int failed
Definition muxer.c:52
int ended
a new link could not be started, the packets that follow are dropped
Definition muxer.c:53
OGGPageList * page_list
Definition muxer.c:49
Codec mapping of the muxer: the parts of the Ogg encapsulation that depend on the codec.
Definition muxer.h:71
int new_links
a new link can replace the current one
Definition muxer.h:75
int(* init)(AVFormatContext *s, AVStream *st)
Build the header packets in OGGStreamContext.header, adjusting the stream time base when the codec se...
Definition muxer.h:80
size_t priv_data_size
size of OGGStreamContext.priv_data
Definition muxer.h:73
int optional_extradata
the headers can be built without extradata
Definition muxer.h:74
const char *(* new_link_refused)(const AVStream *st, const AVPacket *pkt, const uint8_t *new_extradata, size_t size)
Definition muxer.h:86
int64_t(* packet_granule)(AVStream *st, const AVPacket *pkt)
Definition muxer.h:92
OGGPage page
Definition muxer.c:43
struct OGGPageList * next
Definition muxer.c:44
uint16_t size
Definition muxer.h:40
int stream_index
Definition muxer.h:35
uint8_t segments_count
Definition muxer.h:37
uint8_t flags
Definition muxer.h:36
int64_t start_granule
Definition muxer.h:33
int64_t granule
Definition muxer.h:34
unsigned granule_shift
Granule layout, set by the mapping.
Definition muxer.h:55
unsigned serial_num
serial number
Definition muxer.h:62
unsigned page_count
number of page buffered
Definition muxer.h:60
const OGGMuxerMapping * mapping
Definition muxer.h:49
uint8_t * header[3]
Definition muxer.h:47
unsigned page_counter
Definition muxer.h:46
void * priv_data
state of the mapping
Definition muxer.h:50
int packet_granule_offset
added to the end timestamp of a packet without packet_granule
Definition muxer.h:58
int64_t granule_delta_mask
Definition muxer.h:56
int64_t granule_key_mask
bits all clear on keyframes, 0 when no granule marks one
Definition muxer.h:57
int packet_seen
true when packets have been submitted
Definition muxer.h:64
OGGPage page
current page
Definition muxer.h:61
int64_t last_granule
last packet granule
Definition muxer.h:63
int header_len[3]
Definition muxer.h:48
Definition demuxer.h:112
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
int size
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
enum AVCodecID codec_id
static unsigned int seed
Definition videogen.c:78
int len
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