FFmpeg  4.4
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 <stdint.h>
23 
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"
31 #include "avformat.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include "vorbiscomment.h"
35 
36 #define MAX_PAGE_SIZE 65025
37 
38 typedef struct OGGPage {
39  int64_t start_granule;
40  int64_t granule;
46  uint16_t size;
47 } OGGPage;
48 
49 typedef struct OGGStreamContext {
50  unsigned page_counter;
52  int header_len[3];
53  /** for theora granule */
54  int kfgshift;
55  int64_t last_kf_pts;
56  int vrev;
57  /* for VP8 granule */
58  int isvp8;
59  int eos;
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
65 
66 typedef struct OGGPageList {
68  struct OGGPageList *next;
69 } OGGPageList;
70 
71 typedef struct OGGContext {
72  const AVClass *class;
74  int pref_size; ///< preferred page size (0 => fill all segments)
75  int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
77 } OGGContext;
78 
79 #define OFFSET(x) offsetof(OGGContext, x)
80 #define PARAM AV_OPT_FLAG_ENCODING_PARAM
81 
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 },
91  { NULL },
92 };
93 
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,\
98  .option = options,\
99  .version = LIBAVUTIL_VERSION_INT,\
100 };
101 
102 static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
103 {
104  OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
105  uint8_t buf[4 + 1 + 1 + 8 + 4 + 4 + 4 + 1 + 255], *ptr = buf, *crc_pos;
106  const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE);
107  uint32_t crc;
108 
109  bytestream_put_le32(&ptr, MKTAG('O', 'g', 'g', 'S'));
110  bytestream_put_byte(&ptr, 0);
111  bytestream_put_byte(&ptr, page->flags | extra_flags);
112  bytestream_put_le64(&ptr, page->granule);
113  bytestream_put_le32(&ptr, oggstream->serial_num);
114  bytestream_put_le32(&ptr, oggstream->page_counter++);
115  crc_pos = ptr;
116  bytestream_put_le32(&ptr, 0);
117  bytestream_put_byte(&ptr, page->segments_count);
118  bytestream_put_buffer(&ptr, page->segments, page->segments_count);
119 
120  crc = av_crc(crc_table, 0, buf, ptr - buf);
121  crc = av_crc(crc_table, crc, page->data, page->size);
122  bytestream_put_be32(&crc_pos, crc);
123 
124  avio_write(s->pb, buf, ptr - buf);
125  avio_write(s->pb, page->data, page->size);
127  oggstream->page_count--;
128 }
129 
130 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
131 {
132  return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
133  (oggstream->isvp8 && !((granule >> 3) & 0x07ffffff));
134 }
135 
136 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
137 {
138  if (oggstream->kfgshift)
139  return (granule>>oggstream->kfgshift) +
140  (granule & ((1<<oggstream->kfgshift)-1));
141  else if (oggstream->isvp8)
142  return granule >> 32;
143  else
144  return granule;
145 }
146 
148 {
149  AVStream *st2 = s->streams[next->stream_index];
150  AVStream *st = s->streams[page->stream_index];
151  int64_t next_granule, cur_granule;
152 
153  if (next->granule == -1 || page->granule == -1)
154  return 0;
155 
156  next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
157  st2->time_base, AV_TIME_BASE_Q);
158  cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
159  st ->time_base, AV_TIME_BASE_Q);
160  return next_granule > cur_granule;
161 }
162 
163 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
164 {
165  oggstream->page.granule = -1;
166  oggstream->page.flags = 0;
167  oggstream->page.segments_count = 0;
168  oggstream->page.size = 0;
169  return 0;
170 }
171 
173 {
174  OGGContext *ogg = s->priv_data;
175  OGGPageList **p = &ogg->page_list;
176  OGGPageList *l = av_mallocz(sizeof(*l));
177 
178  if (!l)
179  return AVERROR(ENOMEM);
180  l->page = oggstream->page;
181 
182  oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule);
183  oggstream->page_count++;
184  ogg_reset_cur_page(oggstream);
185 
186  while (*p) {
187  if (ogg_compare_granule(s, &(*p)->page, &l->page))
188  break;
189  p = &(*p)->next;
190  }
191  l->next = *p;
192  *p = l;
193 
194  return 0;
195 }
196 
198  uint8_t *data, unsigned size, int64_t granule,
199  int header)
200 {
201  OGGStreamContext *oggstream = st->priv_data;
202  OGGContext *ogg = s->priv_data;
203  int total_segments = size / 255 + 1;
204  uint8_t *p = data;
205  int i, segments, len, flush = 0;
206 
207  // Handles VFR by flushing page because this frame needs to have a timestamp
208  // For theora and VP8, keyframes also need to have a timestamp to correctly mark
209  // them as such, otherwise seeking will not work correctly at the very
210  // least with old libogg versions.
211  // Do not try to flush header packets though, that will create broken files.
213  (ogg_granule_to_timestamp(oggstream, granule) >
214  ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
215  ogg_key_granule(oggstream, granule))) {
216  if (oggstream->page.granule != -1)
217  ogg_buffer_page(s, oggstream);
218  flush = 1;
219  }
220 
221  // avoid a continued page
222  if (!header && oggstream->page.size > 0 &&
223  MAX_PAGE_SIZE - oggstream->page.size < size) {
224  ogg_buffer_page(s, oggstream);
225  }
226 
227  for (i = 0; i < total_segments; ) {
228  OGGPage *page = &oggstream->page;
229 
230  segments = FFMIN(total_segments - i, 255 - page->segments_count);
231 
232  if (i && !page->segments_count)
233  page->flags |= 1; // continued packet
234 
235  memset(page->segments+page->segments_count, 255, segments - 1);
236  page->segments_count += segments - 1;
237 
238  len = FFMIN(size, segments*255);
239  page->segments[page->segments_count++] = len - (segments-1)*255;
240  memcpy(page->data+page->size, p, len);
241  p += len;
242  size -= len;
243  i += segments;
244  page->size += len;
245 
246  if (i == total_segments)
247  page->granule = granule;
248 
249  {
250  AVStream *st = s->streams[page->stream_index];
251 
252  int64_t start = av_rescale_q(page->start_granule, st->time_base,
254  int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule),
256 
257  if (page->segments_count == 255) {
258  ogg_buffer_page(s, oggstream);
259  } else if (!header) {
260  if ((ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
261  (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
262  ogg_buffer_page(s, oggstream);
263  }
264  }
265  }
266  }
267 
268  if (flush && oggstream->page.granule != -1)
269  ogg_buffer_page(s, oggstream);
270 
271  return 0;
272 }
273 
274 static uint8_t *ogg_write_vorbiscomment(int64_t offset, int bitexact,
275  int *header_len, AVDictionary **m, int framing_bit,
276  AVChapter **chapters, unsigned int nb_chapters)
277 {
278  const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
279  AVIOContext pb;
280  int64_t size;
281  uint8_t *p;
282 
284 
285  size = offset + ff_vorbiscomment_length(*m, vendor, chapters, nb_chapters) + framing_bit;
286  if (size > INT_MAX)
287  return NULL;
288  p = av_mallocz(size);
289  if (!p)
290  return NULL;
291 
292  ffio_init_context(&pb, p + offset, size - offset, 1, NULL, NULL, NULL, NULL);
293  ff_vorbiscomment_write(&pb, *m, vendor, chapters, nb_chapters);
294  if (framing_bit)
295  avio_w8(&pb, 1);
296 
297  *header_len = size;
298  return p;
299 }
300 
302  OGGStreamContext *oggstream, int bitexact,
303  AVDictionary **m)
304 {
305  uint8_t *p;
306 
308  return AVERROR(EINVAL);
309 
310  // first packet: STREAMINFO
311  oggstream->header_len[0] = 51;
312  oggstream->header[0] = av_mallocz(51); // per ogg flac specs
313  p = oggstream->header[0];
314  if (!p)
315  return AVERROR(ENOMEM);
316  bytestream_put_byte(&p, 0x7F);
317  bytestream_put_buffer(&p, "FLAC", 4);
318  bytestream_put_byte(&p, 1); // major version
319  bytestream_put_byte(&p, 0); // minor version
320  bytestream_put_be16(&p, 1); // headers packets without this one
321  bytestream_put_buffer(&p, "fLaC", 4);
322  bytestream_put_byte(&p, 0x00); // streaminfo
323  bytestream_put_be24(&p, 34);
325 
326  // second packet: VorbisComment
327  p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
328  if (!p)
329  return AVERROR(ENOMEM);
330  oggstream->header[1] = p;
331  bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
332  bytestream_put_be24(&p, oggstream->header_len[1] - 4);
333 
334  return 0;
335 }
336 
337 #define SPEEX_HEADER_SIZE 80
338 
340  OGGStreamContext *oggstream, int bitexact,
341  AVDictionary **m)
342 {
343  uint8_t *p;
344 
346  return AVERROR_INVALIDDATA;
347 
348  // first packet: Speex header
350  if (!p)
351  return AVERROR(ENOMEM);
352  oggstream->header[0] = p;
353  oggstream->header_len[0] = SPEEX_HEADER_SIZE;
355  AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
356 
357  // second packet: VorbisComment
358  p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
359  if (!p)
360  return AVERROR(ENOMEM);
361  oggstream->header[1] = p;
362 
363  return 0;
364 }
365 
366 #define OPUS_HEADER_SIZE 19
367 
369  OGGStreamContext *oggstream, int bitexact,
370  AVDictionary **m, AVChapter **chapters,
371  unsigned int nb_chapters)
372 {
373  uint8_t *p;
374 
375  if (par->extradata_size < OPUS_HEADER_SIZE)
376  return AVERROR_INVALIDDATA;
377 
378  /* first packet: Opus header */
379  p = av_mallocz(par->extradata_size);
380  if (!p)
381  return AVERROR(ENOMEM);
382  oggstream->header[0] = p;
383  oggstream->header_len[0] = par->extradata_size;
385 
386  /* second packet: VorbisComment */
387  p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0, chapters, nb_chapters);
388  if (!p)
389  return AVERROR(ENOMEM);
390  oggstream->header[1] = p;
391  bytestream_put_buffer(&p, "OpusTags", 8);
392 
393  return 0;
394 }
395 
396 #define VP8_HEADER_SIZE 26
397 
399  OGGStreamContext *oggstream, int bitexact)
400 {
401  AVCodecParameters *par = st->codecpar;
402  uint8_t *p;
403 
404  /* first packet: VP8 header */
406  if (!p)
407  return AVERROR(ENOMEM);
408  oggstream->header[0] = p;
409  oggstream->header_len[0] = VP8_HEADER_SIZE;
410  bytestream_put_byte(&p, 0x4f); // HDRID
411  bytestream_put_buffer(&p, "VP80", 4); // Identifier
412  bytestream_put_byte(&p, 1); // HDRTYP
413  bytestream_put_byte(&p, 1); // VMAJ
414  bytestream_put_byte(&p, 0); // VMIN
415  bytestream_put_be16(&p, par->width);
416  bytestream_put_be16(&p, par->height);
417  bytestream_put_be24(&p, par->sample_aspect_ratio.num);
418  bytestream_put_be24(&p, par->sample_aspect_ratio.den);
419  if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
420  // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
421  // multiple framerate if available.
422  av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
423  st->time_base.num, st->time_base.den,
424  st->r_frame_rate.den, st->r_frame_rate.num);
426  }
427  bytestream_put_be32(&p, st->time_base.den);
428  bytestream_put_be32(&p, st->time_base.num);
429 
430  /* optional second packet: VorbisComment */
432  p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0, NULL, 0);
433  if (!p)
434  return AVERROR(ENOMEM);
435  oggstream->header[1] = p;
436  bytestream_put_byte(&p, 0x4f); // HDRID
437  bytestream_put_buffer(&p, "VP80", 4); // Identifier
438  bytestream_put_byte(&p, 2); // HDRTYP
439  bytestream_put_byte(&p, 0x20);
440  }
441 
442  oggstream->isvp8 = 1;
443 
444  return 0;
445 }
446 
448 {
449  OGGContext *ogg = s->priv_data;
450  OGGPageList *next, *p;
451 
452  if (!ogg->page_list)
453  return;
454 
455  for (p = ogg->page_list; p; ) {
456  OGGStreamContext *oggstream =
457  s->streams[p->page.stream_index]->priv_data;
458  if (oggstream->page_count < 2 && !flush)
459  break;
460  ogg_write_page(s, &p->page,
461  flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
462  next = p->next;
463  av_freep(&p);
464  p = next;
465  }
466  ogg->page_list = p;
467 }
468 
470 {
471  OGGContext *ogg = s->priv_data;
472  OGGStreamContext *oggstream = NULL;
473  int i, j;
474 
475  if (ogg->pref_size)
476  av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
477 
478  for (i = 0; i < s->nb_streams; i++) {
479  AVStream *st = s->streams[i];
480  unsigned serial_num = i + ogg->serial_offset;
481 
482  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
483  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
484  /* Opus requires a fixed 48kHz clock */
485  avpriv_set_pts_info(st, 64, 1, 48000);
486  else
487  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
488  }
489 
490  if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
496  av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
497  return AVERROR(EINVAL);
498  }
499 
500  if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
502  av_log(s, AV_LOG_ERROR, "No extradata present\n");
503  return AVERROR_INVALIDDATA;
504  }
505  oggstream = av_mallocz(sizeof(*oggstream));
506  if (!oggstream)
507  return AVERROR(ENOMEM);
508 
509  oggstream->page.stream_index = i;
510 
511  if (!(s->flags & AVFMT_FLAG_BITEXACT))
512  do {
513  serial_num = av_get_random_seed();
514  for (j = 0; j < i; j++) {
515  OGGStreamContext *sc = s->streams[j]->priv_data;
516  if (serial_num == sc->serial_num)
517  break;
518  }
519  } while (j < i);
520  oggstream->serial_num = serial_num;
521 
522  av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
523 
524  st->priv_data = oggstream;
525  if (st->codecpar->codec_id == AV_CODEC_ID_FLAC) {
526  int err = ogg_build_flac_headers(st->codecpar, oggstream,
527  s->flags & AVFMT_FLAG_BITEXACT,
528  &st->metadata);
529  if (err) {
530  av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
531  return err;
532  }
533  } else if (st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
534  int err = ogg_build_speex_headers(st->codecpar, oggstream,
535  s->flags & AVFMT_FLAG_BITEXACT,
536  &st->metadata);
537  if (err) {
538  av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
539  return err;
540  }
541  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
542  int err = ogg_build_opus_headers(st->codecpar, oggstream,
543  s->flags & AVFMT_FLAG_BITEXACT,
544  &st->metadata, s->chapters, s->nb_chapters);
545  if (err) {
546  av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
547  return err;
548  }
549  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
550  int err = ogg_build_vp8_headers(s, st, oggstream,
551  s->flags & AVFMT_FLAG_BITEXACT);
552  if (err) {
553  av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
554  return err;
555  }
556  } else {
557  uint8_t *p;
558  const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
559  int header_type = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
560  int framing_bit = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
561 
563  st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
564  (const uint8_t**)oggstream->header, oggstream->header_len) < 0) {
565  av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
566  oggstream->header[1] = NULL;
567  return AVERROR_INVALIDDATA;
568  }
569 
571  &oggstream->header_len[1], &st->metadata,
572  framing_bit, NULL, 0);
573  oggstream->header[1] = p;
574  if (!p)
575  return AVERROR(ENOMEM);
576 
577  bytestream_put_byte(&p, header_type);
578  bytestream_put_buffer(&p, cstr, 6);
579 
580  if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
581  int den = AV_RB32(oggstream->header[0] + 22), num = AV_RB32(oggstream->header[0] + 26);
582  /* Make sure to use time base stored in the Theora stream header to write
583  correct timestamps */
584  if (st->time_base.num != num || st->time_base.den != den) {
585  av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
586  st->time_base.num, st->time_base.den, num, den);
587  avpriv_set_pts_info(st, 64, num, den);
588  }
589  /** KFGSHIFT is the width of the less significant section of the granule position
590  The less significant section is the frame count since the last keyframe */
591  oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
592  oggstream->vrev = oggstream->header[0][9];
593  av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
594  oggstream->kfgshift, oggstream->vrev);
595  }
596  }
597  }
598 
599  return 0;
600 }
601 
603 {
604  OGGStreamContext *oggstream = NULL;
605  int i, j;
606 
607  for (j = 0; j < s->nb_streams; j++) {
608  oggstream = s->streams[j]->priv_data;
609  ogg_buffer_data(s, s->streams[j], oggstream->header[0],
610  oggstream->header_len[0], 0, 1);
611  oggstream->page.flags |= 2; // bos
612  ogg_buffer_page(s, oggstream);
613  }
614  for (j = 0; j < s->nb_streams; j++) {
615  AVStream *st = s->streams[j];
616  oggstream = st->priv_data;
617  for (i = 1; i < 3; i++) {
618  if (oggstream->header_len[i])
619  ogg_buffer_data(s, st, oggstream->header[i],
620  oggstream->header_len[i], 0, 1);
621  }
622  ogg_buffer_page(s, oggstream);
623  }
624 
625  oggstream->page.start_granule = AV_NOPTS_VALUE;
626 
627  ogg_write_pages(s, 2);
628 
629  return 0;
630 }
631 
633 {
634  AVStream *st = s->streams[pkt->stream_index];
635  OGGStreamContext *oggstream = st->priv_data;
636  int ret;
637  int64_t granule;
638 
639  if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
640  int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
641  int pframe_count;
642  if (pkt->flags & AV_PKT_FLAG_KEY)
643  oggstream->last_kf_pts = pts;
644  pframe_count = pts - oggstream->last_kf_pts;
645  // prevent frame count from overflow if key frame flag is not set
646  if (pframe_count >= (1<<oggstream->kfgshift)) {
647  oggstream->last_kf_pts += pframe_count;
648  pframe_count = 0;
649  }
650  granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
651  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
652  granule = pkt->pts + pkt->duration +
654  (AVRational){ 1, st->codecpar->sample_rate },
655  st->time_base);
656  else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
657  int64_t pts, invcnt, dist;
658  int visible;
659 
660  visible = (pkt->data[0] >> 4) & 1;
661  pts = pkt->pts + pkt->duration;
662  invcnt = (oggstream->last_granule >> 30) & 3;
663  invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
664  dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
665 
666  granule = (pts << 32) | (invcnt << 30) | (dist << 3);
667  } else
668  granule = pkt->pts + pkt->duration;
669 
670  if (oggstream->page.start_granule == AV_NOPTS_VALUE)
671  oggstream->page.start_granule = pkt->pts;
672 
673  ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
674  if (ret < 0)
675  return ret;
676 
677  ogg_write_pages(s, 0);
678 
679  oggstream->last_granule = granule;
680 
681  return 0;
682 }
683 
685 {
686  int i;
687 
688  if (pkt)
690 
691  for (i = 0; i < s->nb_streams; i++) {
692  OGGStreamContext *oggstream = s->streams[i]->priv_data;
693  if (oggstream->page.segments_count)
694  ogg_buffer_page(s, oggstream);
695  }
696 
697  ogg_write_pages(s, 2);
698  return 1;
699 }
700 
702 {
703  int i;
704 
705  /* flush current page if needed */
706  for (i = 0; i < s->nb_streams; i++) {
707  OGGStreamContext *oggstream = s->streams[i]->priv_data;
708 
709  if (oggstream->page.size > 0)
710  ogg_buffer_page(s, oggstream);
711  }
712 
713  ogg_write_pages(s, 1);
714 
715  return 0;
716 }
717 
719 {
720  OGGContext *ogg = s->priv_data;
721  OGGPageList *p = ogg->page_list;
722  int i;
723 
724  for (i = 0; i < s->nb_streams; i++) {
725  AVStream *st = s->streams[i];
726  OGGStreamContext *oggstream = st->priv_data;
727  if (!oggstream)
728  continue;
729  if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
733  av_freep(&oggstream->header[0]);
734  }
735  av_freep(&oggstream->header[1]);
736  }
737 
738  while (p) {
739  OGGPageList *next = p->next;
740  av_free(p);
741  p = next;
742  }
743  ogg->page_list = NULL;
744 }
745 
746 #if CONFIG_OGG_MUXER
747 OGG_CLASS(ogg, Ogg)
749  .name = "ogg",
750  .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
751  .mime_type = "application/ogg",
752  .extensions = "ogg"
753 #if !CONFIG_OGV_MUXER
754  ",ogv"
755 #endif
756 #if !CONFIG_SPX_MUXER
757  ",spx"
758 #endif
759 #if !CONFIG_OPUS_MUXER
760  ",opus"
761 #endif
762  ,
763  .priv_data_size = sizeof(OGGContext),
764  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
766  .video_codec = AV_CODEC_ID_THEORA,
767  .init = ogg_init,
771  .deinit = ogg_free,
773  .priv_class = &ogg_muxer_class,
774 };
775 #endif
776 
777 #if CONFIG_OGA_MUXER
778 OGG_CLASS(oga, Ogg audio)
780  .name = "oga",
781  .long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
782  .mime_type = "audio/ogg",
783  .extensions = "oga",
784  .priv_data_size = sizeof(OGGContext),
785  .audio_codec = AV_CODEC_ID_FLAC,
786  .init = ogg_init,
790  .deinit = ogg_free,
792  .priv_class = &oga_muxer_class,
793 };
794 #endif
795 
796 #if CONFIG_OGV_MUXER
797 OGG_CLASS(ogv, Ogg video)
799  .name = "ogv",
800  .long_name = NULL_IF_CONFIG_SMALL("Ogg Video"),
801  .mime_type = "video/ogg",
802  .extensions = "ogv",
803  .priv_data_size = sizeof(OGGContext),
804  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
806  .video_codec = CONFIG_LIBTHEORA_ENCODER ?
808  .init = ogg_init,
812  .deinit = ogg_free,
814  .priv_class = &ogv_muxer_class,
815 };
816 #endif
817 
818 #if CONFIG_SPX_MUXER
819 OGG_CLASS(spx, Ogg Speex)
821  .name = "spx",
822  .long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
823  .mime_type = "audio/ogg",
824  .extensions = "spx",
825  .priv_data_size = sizeof(OGGContext),
826  .audio_codec = AV_CODEC_ID_SPEEX,
827  .init = ogg_init,
831  .deinit = ogg_free,
833  .priv_class = &spx_muxer_class,
834 };
835 #endif
836 
837 #if CONFIG_OPUS_MUXER
838 OGG_CLASS(opus, Ogg Opus)
840  .name = "opus",
841  .long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
842  .mime_type = "audio/ogg",
843  .extensions = "opus",
844  .priv_data_size = sizeof(OGGContext),
845  .audio_codec = AV_CODEC_ID_OPUS,
846  .init = ogg_init,
850  .deinit = ogg_free,
852  .priv_class = &opus_muxer_class,
853 };
854 #endif
static void flush(AVCodecContext *avctx)
AVOutputFormat ff_ogg_muxer
AVOutputFormat ff_opus_muxer
AVOutputFormat ff_oga_muxer
AVOutputFormat ff_spx_muxer
AVOutputFormat ff_ogv_muxer
uint8_t
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:1380
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:471
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:475
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:472
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:479
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:203
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:88
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
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_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define CONFIG_LIBTHEORA_ENCODER
Definition: config.h:1478
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:1481
#define NULL
Definition: coverity.c:32
Public header for CRC hash function implementation.
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:429
@ AV_CODEC_ID_VP8
Definition: codec_id.h:189
@ AV_CODEC_ID_THEORA
Definition: codec_id.h:79
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:436
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:459
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:484
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
uint32_t AVCRC
Definition: crc.h:47
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:392
@ AV_CRC_32_IEEE
Definition: crc.h:53
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:76
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
int i
Definition: input.c:407
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4941
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
#define LIBAVFORMAT_IDENT
Definition: version.h:46
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char data[16]
Definition: mxf.c:142
#define OPUS_HEADER_SIZE
Definition: oggenc.c:366
static int ogg_write_header(AVFormatContext *s)
Definition: oggenc.c:602
static int ogg_build_opus_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m, AVChapter **chapters, unsigned int nb_chapters)
Definition: oggenc.c:368
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
Definition: oggenc.c:147
static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st, OGGStreamContext *oggstream, int bitexact)
Definition: oggenc.c:398
static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: oggenc.c:632
static const AVOption options[]
Definition: oggenc.c:82
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
Definition: oggenc.c:172
static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
Definition: oggenc.c:102
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
Definition: oggenc.c:163
#define OGG_CLASS(flavor, name)
Definition: oggenc.c:94
static int ogg_build_flac_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:301
static int ogg_buffer_data(AVFormatContext *s, AVStream *st, uint8_t *data, unsigned size, int64_t granule, int header)
Definition: oggenc.c:197
#define SPEEX_HEADER_SIZE
Definition: oggenc.c:337
#define VP8_HEADER_SIZE
Definition: oggenc.c:396
#define MAX_PAGE_SIZE
Definition: oggenc.c:36
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: oggenc.c:684
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
Definition: oggenc.c:136
static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
Definition: oggenc.c:130
static int ogg_build_speex_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:339
static void ogg_free(AVFormatContext *s)
Definition: oggenc.c:718
static void ogg_write_pages(AVFormatContext *s, int flush)
Definition: oggenc.c:447
static int ogg_init(AVFormatContext *s)
Definition: oggenc.c:469
static int ogg_write_trailer(AVFormatContext *s)
Definition: oggenc.c:701
#define OFFSET(x)
Definition: oggenc.c:79
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:274
#define PARAM
Definition: oggenc.c:80
AVOptions.
static const uint8_t header[24]
Definition: sdr2.c:67
Describe the class of an AVClass context structure.
Definition: log.h:67
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
int width
Video only.
Definition: codec_par.h:126
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:136
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
int initial_padding
Audio only.
Definition: codec_par.h:189
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
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:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVDictionary * metadata
Definition: avformat.h:937
void * priv_data
Definition: avformat.h:888
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1015
int64_t pref_duration
preferred page duration (0 => fill all segments)
Definition: oggenc.c:75
int serial_offset
Definition: oggenc.c:76
OGGPageList * page_list
Definition: oggenc.c:73
int pref_size
preferred page size (0 => fill all segments)
Definition: oggenc.c:74
OGGPage page
Definition: oggenc.c:67
struct OGGPageList * next
Definition: oggenc.c:68
Definition: oggenc.c:38
uint8_t data[MAX_PAGE_SIZE]
Definition: oggenc.c:45
uint16_t size
Definition: oggenc.c:46
int stream_index
Definition: oggenc.c:41
uint8_t segments_count
Definition: oggenc.c:43
uint8_t segments[255]
Definition: oggenc.c:44
uint8_t flags
Definition: oggenc.c:42
int64_t start_granule
Definition: oggenc.c:39
int64_t granule
Definition: oggenc.c:40
int kfgshift
for theora granule
Definition: oggenc.c:54
unsigned serial_num
serial number
Definition: oggenc.c:62
unsigned page_count
number of page buffered
Definition: oggenc.c:60
int64_t last_kf_pts
Definition: oggenc.c:55
uint8_t * header[3]
Definition: oggenc.c:51
unsigned page_counter
Definition: oggenc.c:50
OGGPage page
current page
Definition: oggenc.c:61
int64_t last_granule
last packet granule
Definition: oggenc.c:63
int header_len[3]
Definition: oggenc.c:52
Definition: oggdec.h:102
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
static int64_t pts
int size
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into an AVIOContext.
Definition: vorbiscomment.c:65
const AVMetadataConv ff_vorbiscomment_metadata_conv[]
VorbisComment metadata conversion mapping.
Definition: vorbiscomment.c:33
int64_t ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
Definition: vorbiscomment.c:41
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:24