FFmpeg
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_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
103 {
104  int64_t pos = avio_tell(pb);
105  uint32_t checksum = ffio_get_checksum(pb);
106  avio_seek(pb, crc_offset, SEEK_SET);
107  avio_wb32(pb, checksum);
108  avio_seek(pb, pos, SEEK_SET);
109 }
110 
111 static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
112 {
113  OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
114  AVIOContext *pb;
115  int64_t crc_offset;
116  int ret, size;
117  uint8_t *buf;
118 
119  ret = avio_open_dyn_buf(&pb);
120  if (ret < 0)
121  return ret;
123  ffio_wfourcc(pb, "OggS");
124  avio_w8(pb, 0);
125  avio_w8(pb, page->flags | extra_flags);
126  avio_wl64(pb, page->granule);
127  avio_wl32(pb, oggstream->serial_num);
128  avio_wl32(pb, oggstream->page_counter++);
129  crc_offset = avio_tell(pb);
130  avio_wl32(pb, 0); // crc
131  avio_w8(pb, page->segments_count);
132  avio_write(pb, page->segments, page->segments_count);
133  avio_write(pb, page->data, page->size);
134 
135  ogg_update_checksum(s, pb, crc_offset);
136 
137  size = avio_close_dyn_buf(pb, &buf);
138  if (size < 0)
139  return size;
140 
141  avio_write(s->pb, buf, size);
143  av_free(buf);
144  oggstream->page_count--;
145  return 0;
146 }
147 
148 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
149 {
150  return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
151  (oggstream->isvp8 && !((granule >> 3) & 0x07ffffff));
152 }
153 
154 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
155 {
156  if (oggstream->kfgshift)
157  return (granule>>oggstream->kfgshift) +
158  (granule & ((1<<oggstream->kfgshift)-1));
159  else if (oggstream->isvp8)
160  return granule >> 32;
161  else
162  return granule;
163 }
164 
166 {
167  AVStream *st2 = s->streams[next->stream_index];
168  AVStream *st = s->streams[page->stream_index];
169  int64_t next_granule, cur_granule;
170 
171  if (next->granule == -1 || page->granule == -1)
172  return 0;
173 
174  next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
175  st2->time_base, AV_TIME_BASE_Q);
176  cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
177  st ->time_base, AV_TIME_BASE_Q);
178  return next_granule > cur_granule;
179 }
180 
181 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
182 {
183  oggstream->page.granule = -1;
184  oggstream->page.flags = 0;
185  oggstream->page.segments_count = 0;
186  oggstream->page.size = 0;
187  return 0;
188 }
189 
191 {
192  OGGContext *ogg = s->priv_data;
193  OGGPageList **p = &ogg->page_list;
194  OGGPageList *l = av_mallocz(sizeof(*l));
195 
196  if (!l)
197  return AVERROR(ENOMEM);
198  l->page = oggstream->page;
199 
200  oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule);
201  oggstream->page_count++;
202  ogg_reset_cur_page(oggstream);
203 
204  while (*p) {
205  if (ogg_compare_granule(s, &(*p)->page, &l->page))
206  break;
207  p = &(*p)->next;
208  }
209  l->next = *p;
210  *p = l;
211 
212  return 0;
213 }
214 
216  uint8_t *data, unsigned size, int64_t granule,
217  int header)
218 {
219  OGGStreamContext *oggstream = st->priv_data;
220  OGGContext *ogg = s->priv_data;
221  int total_segments = size / 255 + 1;
222  uint8_t *p = data;
223  int i, segments, len, flush = 0;
224 
225  // Handles VFR by flushing page because this frame needs to have a timestamp
226  // For theora and VP8, keyframes also need to have a timestamp to correctly mark
227  // them as such, otherwise seeking will not work correctly at the very
228  // least with old libogg versions.
229  // Do not try to flush header packets though, that will create broken files.
230  if ((st->codecpar->codec_id == AV_CODEC_ID_THEORA || st->codecpar->codec_id == AV_CODEC_ID_VP8) && !header &&
231  (ogg_granule_to_timestamp(oggstream, granule) >
232  ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
233  ogg_key_granule(oggstream, granule))) {
234  if (oggstream->page.granule != -1)
235  ogg_buffer_page(s, oggstream);
236  flush = 1;
237  }
238 
239  // avoid a continued page
240  if (!header && oggstream->page.size > 0 &&
241  MAX_PAGE_SIZE - oggstream->page.size < size) {
242  ogg_buffer_page(s, oggstream);
243  }
244 
245  for (i = 0; i < total_segments; ) {
246  OGGPage *page = &oggstream->page;
247 
248  segments = FFMIN(total_segments - i, 255 - page->segments_count);
249 
250  if (i && !page->segments_count)
251  page->flags |= 1; // continued packet
252 
253  memset(page->segments+page->segments_count, 255, segments - 1);
254  page->segments_count += segments - 1;
255 
256  len = FFMIN(size, segments*255);
257  page->segments[page->segments_count++] = len - (segments-1)*255;
258  memcpy(page->data+page->size, p, len);
259  p += len;
260  size -= len;
261  i += segments;
262  page->size += len;
263 
264  if (i == total_segments)
265  page->granule = granule;
266 
267  {
268  AVStream *st = s->streams[page->stream_index];
269 
270  int64_t start = av_rescale_q(page->start_granule, st->time_base,
272  int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule),
274 
275  if (page->segments_count == 255) {
276  ogg_buffer_page(s, oggstream);
277  } else if (!header) {
278  if ((ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
279  (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
280  ogg_buffer_page(s, oggstream);
281  }
282  }
283  }
284  }
285 
286  if (flush && oggstream->page.granule != -1)
287  ogg_buffer_page(s, oggstream);
288 
289  return 0;
290 }
291 
292 static uint8_t *ogg_write_vorbiscomment(int64_t offset, int bitexact,
293  int *header_len, AVDictionary **m, int framing_bit,
294  AVChapter **chapters, unsigned int nb_chapters)
295 {
296  const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
297  int64_t size;
298  uint8_t *p, *p0;
299 
301 
302  size = offset + ff_vorbiscomment_length(*m, vendor, chapters, nb_chapters) + framing_bit;
303  if (size > INT_MAX)
304  return NULL;
305  p = av_mallocz(size);
306  if (!p)
307  return NULL;
308  p0 = p;
309 
310  p += offset;
311  ff_vorbiscomment_write(&p, m, vendor, chapters, nb_chapters);
312  if (framing_bit)
313  bytestream_put_byte(&p, 1);
314 
315  *header_len = size;
316  return p0;
317 }
318 
320  OGGStreamContext *oggstream, int bitexact,
321  AVDictionary **m)
322 {
323  uint8_t *p;
324 
326  return AVERROR(EINVAL);
327 
328  // first packet: STREAMINFO
329  oggstream->header_len[0] = 51;
330  oggstream->header[0] = av_mallocz(51); // per ogg flac specs
331  p = oggstream->header[0];
332  if (!p)
333  return AVERROR(ENOMEM);
334  bytestream_put_byte(&p, 0x7F);
335  bytestream_put_buffer(&p, "FLAC", 4);
336  bytestream_put_byte(&p, 1); // major version
337  bytestream_put_byte(&p, 0); // minor version
338  bytestream_put_be16(&p, 1); // headers packets without this one
339  bytestream_put_buffer(&p, "fLaC", 4);
340  bytestream_put_byte(&p, 0x00); // streaminfo
341  bytestream_put_be24(&p, 34);
343 
344  // second packet: VorbisComment
345  p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
346  if (!p)
347  return AVERROR(ENOMEM);
348  oggstream->header[1] = p;
349  bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
350  bytestream_put_be24(&p, oggstream->header_len[1] - 4);
351 
352  return 0;
353 }
354 
355 #define SPEEX_HEADER_SIZE 80
356 
358  OGGStreamContext *oggstream, int bitexact,
359  AVDictionary **m)
360 {
361  uint8_t *p;
362 
364  return AVERROR_INVALIDDATA;
365 
366  // first packet: Speex header
368  if (!p)
369  return AVERROR(ENOMEM);
370  oggstream->header[0] = p;
371  oggstream->header_len[0] = SPEEX_HEADER_SIZE;
373  AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
374 
375  // second packet: VorbisComment
376  p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
377  if (!p)
378  return AVERROR(ENOMEM);
379  oggstream->header[1] = p;
380 
381  return 0;
382 }
383 
384 #define OPUS_HEADER_SIZE 19
385 
387  OGGStreamContext *oggstream, int bitexact,
388  AVDictionary **m, AVChapter **chapters,
389  unsigned int nb_chapters)
390 {
391  uint8_t *p;
392 
393  if (par->extradata_size < OPUS_HEADER_SIZE)
394  return AVERROR_INVALIDDATA;
395 
396  /* first packet: Opus header */
397  p = av_mallocz(par->extradata_size);
398  if (!p)
399  return AVERROR(ENOMEM);
400  oggstream->header[0] = p;
401  oggstream->header_len[0] = par->extradata_size;
403 
404  /* second packet: VorbisComment */
405  p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0, chapters, nb_chapters);
406  if (!p)
407  return AVERROR(ENOMEM);
408  oggstream->header[1] = p;
409  bytestream_put_buffer(&p, "OpusTags", 8);
410 
411  return 0;
412 }
413 
414 #define VP8_HEADER_SIZE 26
415 
417  OGGStreamContext *oggstream, int bitexact)
418 {
419  AVCodecParameters *par = st->codecpar;
420  uint8_t *p;
421 
422  /* first packet: VP8 header */
424  if (!p)
425  return AVERROR(ENOMEM);
426  oggstream->header[0] = p;
427  oggstream->header_len[0] = VP8_HEADER_SIZE;
428  bytestream_put_byte(&p, 0x4f); // HDRID
429  bytestream_put_buffer(&p, "VP80", 4); // Identifier
430  bytestream_put_byte(&p, 1); // HDRTYP
431  bytestream_put_byte(&p, 1); // VMAJ
432  bytestream_put_byte(&p, 0); // VMIN
433  bytestream_put_be16(&p, par->width);
434  bytestream_put_be16(&p, par->height);
435  bytestream_put_be24(&p, par->sample_aspect_ratio.num);
436  bytestream_put_be24(&p, par->sample_aspect_ratio.den);
437  if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
438  // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
439  // multiple framerate if available.
440  av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
441  st->time_base.num, st->time_base.den,
442  st->r_frame_rate.den, st->r_frame_rate.num);
444  }
445  bytestream_put_be32(&p, st->time_base.den);
446  bytestream_put_be32(&p, st->time_base.num);
447 
448  /* optional second packet: VorbisComment */
450  p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0, NULL, 0);
451  if (!p)
452  return AVERROR(ENOMEM);
453  oggstream->header[1] = p;
454  bytestream_put_byte(&p, 0x4f); // HDRID
455  bytestream_put_buffer(&p, "VP80", 4); // Identifier
456  bytestream_put_byte(&p, 2); // HDRTYP
457  bytestream_put_byte(&p, 0x20);
458  }
459 
460  oggstream->isvp8 = 1;
461 
462  return 0;
463 }
464 
466 {
467  OGGContext *ogg = s->priv_data;
468  OGGPageList *next, *p;
469 
470  if (!ogg->page_list)
471  return;
472 
473  for (p = ogg->page_list; p; ) {
474  OGGStreamContext *oggstream =
476  if (oggstream->page_count < 2 && !flush)
477  break;
478  ogg_write_page(s, &p->page,
479  flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
480  next = p->next;
481  av_freep(&p);
482  p = next;
483  }
484  ogg->page_list = p;
485 }
486 
488 {
489  OGGContext *ogg = s->priv_data;
490  OGGStreamContext *oggstream = NULL;
491  int i, j;
492 
493  if (ogg->pref_size)
494  av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
495 
496  for (i = 0; i < s->nb_streams; i++) {
497  AVStream *st = s->streams[i];
498  unsigned serial_num = i + ogg->serial_offset;
499 
500  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
501  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
502  /* Opus requires a fixed 48kHz clock */
503  avpriv_set_pts_info(st, 64, 1, 48000);
504  else
505  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
506  }
507 
508  if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
514  av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
515  return AVERROR(EINVAL);
516  }
517 
518  if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
520  av_log(s, AV_LOG_ERROR, "No extradata present\n");
521  return AVERROR_INVALIDDATA;
522  }
523  oggstream = av_mallocz(sizeof(*oggstream));
524  if (!oggstream)
525  return AVERROR(ENOMEM);
526 
527  oggstream->page.stream_index = i;
528 
529  if (!(s->flags & AVFMT_FLAG_BITEXACT))
530  do {
531  serial_num = av_get_random_seed();
532  for (j = 0; j < i; j++) {
533  OGGStreamContext *sc = s->streams[j]->priv_data;
534  if (serial_num == sc->serial_num)
535  break;
536  }
537  } while (j < i);
538  oggstream->serial_num = serial_num;
539 
541 
542  st->priv_data = oggstream;
543  if (st->codecpar->codec_id == AV_CODEC_ID_FLAC) {
544  int err = ogg_build_flac_headers(st->codecpar, oggstream,
546  &st->metadata);
547  if (err) {
548  av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
549  av_freep(&st->priv_data);
550  return err;
551  }
552  } else if (st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
553  int err = ogg_build_speex_headers(st->codecpar, oggstream,
555  &st->metadata);
556  if (err) {
557  av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
558  av_freep(&st->priv_data);
559  return err;
560  }
561  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
562  int err = ogg_build_opus_headers(st->codecpar, oggstream,
564  &st->metadata, s->chapters, s->nb_chapters);
565  if (err) {
566  av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
567  av_freep(&st->priv_data);
568  return err;
569  }
570  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
571  int err = ogg_build_vp8_headers(s, st, oggstream,
573  if (err) {
574  av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
575  av_freep(&st->priv_data);
576  return err;
577  }
578  } else {
579  uint8_t *p;
580  const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
581  int header_type = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
582  int framing_bit = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
583 
585  st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
586  (const uint8_t**)oggstream->header, oggstream->header_len) < 0) {
587  av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
588  av_freep(&st->priv_data);
589  return AVERROR_INVALIDDATA;
590  }
591 
593  &oggstream->header_len[1], &st->metadata,
594  framing_bit, NULL, 0);
595  oggstream->header[1] = p;
596  if (!p)
597  return AVERROR(ENOMEM);
598 
599  bytestream_put_byte(&p, header_type);
600  bytestream_put_buffer(&p, cstr, 6);
601 
602  if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
603  int den = AV_RB32(oggstream->header[0] + 22), num = AV_RB32(oggstream->header[0] + 26);
604  /* Make sure to use time base stored in the Theora stream header to write
605  correct timestamps */
606  if (st->time_base.num != num || st->time_base.den != den) {
607  av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
608  st->time_base.num, st->time_base.den, num, den);
609  avpriv_set_pts_info(st, 64, num, den);
610  }
611  /** KFGSHIFT is the width of the less significant section of the granule position
612  The less significant section is the frame count since the last keyframe */
613  oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
614  oggstream->vrev = oggstream->header[0][9];
615  av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
616  oggstream->kfgshift, oggstream->vrev);
617  }
618  }
619  }
620 
621  return 0;
622 }
623 
625 {
626  OGGStreamContext *oggstream = NULL;
627  int i, j;
628 
629  for (j = 0; j < s->nb_streams; j++) {
630  oggstream = s->streams[j]->priv_data;
631  ogg_buffer_data(s, s->streams[j], oggstream->header[0],
632  oggstream->header_len[0], 0, 1);
633  oggstream->page.flags |= 2; // bos
634  ogg_buffer_page(s, oggstream);
635  }
636  for (j = 0; j < s->nb_streams; j++) {
637  AVStream *st = s->streams[j];
638  oggstream = st->priv_data;
639  for (i = 1; i < 3; i++) {
640  if (oggstream->header_len[i])
641  ogg_buffer_data(s, st, oggstream->header[i],
642  oggstream->header_len[i], 0, 1);
643  }
644  ogg_buffer_page(s, oggstream);
645  }
646 
647  oggstream->page.start_granule = AV_NOPTS_VALUE;
648 
649  ogg_write_pages(s, 2);
650 
651  return 0;
652 }
653 
655 {
656  AVStream *st = s->streams[pkt->stream_index];
657  OGGStreamContext *oggstream = st->priv_data;
658  int ret;
659  int64_t granule;
660 
661  if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
662  int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
663  int pframe_count;
664  if (pkt->flags & AV_PKT_FLAG_KEY)
665  oggstream->last_kf_pts = pts;
666  pframe_count = pts - oggstream->last_kf_pts;
667  // prevent frame count from overflow if key frame flag is not set
668  if (pframe_count >= (1<<oggstream->kfgshift)) {
669  oggstream->last_kf_pts += pframe_count;
670  pframe_count = 0;
671  }
672  granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
673  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
674  granule = pkt->pts + pkt->duration +
676  (AVRational){ 1, st->codecpar->sample_rate },
677  st->time_base);
678  else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
679  int64_t pts, invcnt, dist;
680  int visible;
681 
682  visible = (pkt->data[0] >> 4) & 1;
683  pts = pkt->pts + pkt->duration;
684  invcnt = (oggstream->last_granule >> 30) & 3;
685  invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
686  dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
687 
688  granule = (pts << 32) | (invcnt << 30) | (dist << 3);
689  } else
690  granule = pkt->pts + pkt->duration;
691 
692  if (oggstream->page.start_granule == AV_NOPTS_VALUE)
693  oggstream->page.start_granule = pkt->pts;
694 
695  ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
696  if (ret < 0)
697  return ret;
698 
699  ogg_write_pages(s, 0);
700 
701  oggstream->last_granule = granule;
702 
703  return 0;
704 }
705 
707 {
708  int i;
709 
710  if (pkt)
711  return ogg_write_packet_internal(s, pkt);
712 
713  for (i = 0; i < s->nb_streams; i++) {
714  OGGStreamContext *oggstream = s->streams[i]->priv_data;
715  if (oggstream->page.segments_count)
716  ogg_buffer_page(s, oggstream);
717  }
718 
719  ogg_write_pages(s, 2);
720  return 1;
721 }
722 
724 {
725  int i;
726 
727  /* flush current page if needed */
728  for (i = 0; i < s->nb_streams; i++) {
729  OGGStreamContext *oggstream = s->streams[i]->priv_data;
730 
731  if (oggstream->page.size > 0)
732  ogg_buffer_page(s, oggstream);
733  }
734 
735  ogg_write_pages(s, 1);
736 
737  return 0;
738 }
739 
741 {
742  OGGContext *ogg = s->priv_data;
743  OGGPageList *p = ogg->page_list;
744  int i;
745 
746  for (i = 0; i < s->nb_streams; i++) {
747  AVStream *st = s->streams[i];
748  OGGStreamContext *oggstream = st->priv_data;
749  if (!oggstream)
750  continue;
751  if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
755  av_freep(&oggstream->header[0]);
756  }
757  av_freep(&oggstream->header[1]);
758  av_freep(&st->priv_data);
759  }
760 
761  while (p) {
762  OGGPageList *next = p->next;
763  av_free(p);
764  p = next;
765  }
766  ogg->page_list = NULL;
767 }
768 
769 #if CONFIG_OGG_MUXER
770 OGG_CLASS(ogg, Ogg)
772  .name = "ogg",
773  .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
774  .mime_type = "application/ogg",
775  .extensions = "ogg"
776 #if !CONFIG_OGV_MUXER
777  ",ogv"
778 #endif
779 #if !CONFIG_SPX_MUXER
780  ",spx"
781 #endif
782 #if !CONFIG_OPUS_MUXER
783  ",opus"
784 #endif
785  ,
786  .priv_data_size = sizeof(OGGContext),
787  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
789  .video_codec = AV_CODEC_ID_THEORA,
790  .init = ogg_init,
794  .deinit = ogg_free,
796  .priv_class = &ogg_muxer_class,
797 };
798 #endif
799 
800 #if CONFIG_OGA_MUXER
801 OGG_CLASS(oga, Ogg audio)
803  .name = "oga",
804  .long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
805  .mime_type = "audio/ogg",
806  .extensions = "oga",
807  .priv_data_size = sizeof(OGGContext),
808  .audio_codec = AV_CODEC_ID_FLAC,
809  .init = ogg_init,
813  .deinit = ogg_free,
815  .priv_class = &oga_muxer_class,
816 };
817 #endif
818 
819 #if CONFIG_OGV_MUXER
820 OGG_CLASS(ogv, Ogg video)
822  .name = "ogv",
823  .long_name = NULL_IF_CONFIG_SMALL("Ogg Video"),
824  .mime_type = "video/ogg",
825  .extensions = "ogv",
826  .priv_data_size = sizeof(OGGContext),
827  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
829  .video_codec = CONFIG_LIBTHEORA_ENCODER ?
831  .init = ogg_init,
835  .deinit = ogg_free,
837  .priv_class = &ogv_muxer_class,
838 };
839 #endif
840 
841 #if CONFIG_SPX_MUXER
842 OGG_CLASS(spx, Ogg Speex)
844  .name = "spx",
845  .long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
846  .mime_type = "audio/ogg",
847  .extensions = "spx",
848  .priv_data_size = sizeof(OGGContext),
849  .audio_codec = AV_CODEC_ID_SPEEX,
850  .init = ogg_init,
854  .deinit = ogg_free,
856  .priv_class = &spx_muxer_class,
857 };
858 #endif
859 
860 #if CONFIG_OPUS_MUXER
861 OGG_CLASS(opus, Ogg Opus)
863  .name = "opus",
864  .long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
865  .mime_type = "audio/ogg",
866  .extensions = "opus",
867  .priv_data_size = sizeof(OGGContext),
868  .audio_codec = AV_CODEC_ID_OPUS,
869  .init = ogg_init,
873  .deinit = ogg_free,
875  .priv_class = &opus_muxer_class,
876 };
877 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1586
int header_len[3]
Definition: oggenc.c:52
static int ogg_write_header(AVFormatContext *s)
Definition: oggenc.c:624
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:690
#define NULL
Definition: coverity.c:32
int64_t granule
Definition: oggenc.c:40
int64_t last_granule
last packet granule
Definition: oggenc.c:63
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1466
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
unsigned page_count
number of page buffered
Definition: oggenc.c:60
#define AV_DICT_DONT_OVERWRITE
Don&#39;t overwrite existing entries.
Definition: dict.h:79
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:4931
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:4063
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3987
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1495
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
uint8_t data[MAX_PAGE_SIZE]
Definition: oggenc.c:45
OGGPage page
Definition: oggenc.c:67
void * priv_data
Definition: avformat.h:895
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
OGGPage page
current page
Definition: oggenc.c:61
static AVPacket pkt
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:476
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1432
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:477
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3979
#define OPUS_HEADER_SIZE
Definition: oggenc.c:384
static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
Definition: oggenc.c:111
Format I/O context.
Definition: avformat.h:1357
static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
Definition: oggenc.c:102
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:372
uint8_t
static void ogg_free(AVFormatContext *s)
Definition: oggenc.c:740
int width
Video only.
Definition: avcodec.h:4053
AVOptions.
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146
uint16_t size
Definition: oggenc.c:46
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1512
#define MAX_PAGE_SIZE
Definition: oggenc.c:36
uint8_t segments_count
Definition: oggenc.c:43
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:496
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1425
static int ogg_build_speex_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:357
int64_t last_kf_pts
Definition: oggenc.c:55
Public header for CRC hash function implementation.
int initial_padding
Audio only.
Definition: avcodec.h:4116
AVOutputFormat ff_spx_muxer
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
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1488
uint8_t * data
Definition: avcodec.h:1494
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static const uint8_t header[24]
Definition: sdr2.c:67
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:58
Definition: oggenc.c:38
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1505
static const AVOption options[]
Definition: oggenc.c:82
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1526
static int ogg_build_flac_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:319
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:460
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
unsigned page_counter
Definition: oggenc.c:50
const AVMetadataConv ff_vorbiscomment_metadata_conv[]
VorbisComment metadata conversion mapping.
Definition: vorbiscomment.c:33
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1597
static int ogg_buffer_data(AVFormatContext *s, AVStream *st, uint8_t *data, unsigned size, int64_t granule, int header)
Definition: oggenc.c:215
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
int64_t start_granule
Definition: oggenc.c:39
struct OGGPageList * next
Definition: oggenc.c:68
int pref_size
preferred page size (0 => fill all segments)
Definition: oggenc.c:74
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3983
AVChapter ** chapters
Definition: avformat.h:1587
AVOutputFormat ff_ogv_muxer
int ff_vorbiscomment_write(uint8_t **p, AVDictionary **m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into a buffer.
Definition: vorbiscomment.c:65
uint8_t flags
Definition: oggenc.c:42
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
Definition: oggenc.c:181
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
Definition: oggenc.c:154
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1500
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4005
static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: oggenc.c:654
#define VP8_HEADER_SIZE
Definition: oggenc.c:414
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1413
static int ogg_write_trailer(AVFormatContext *s)
Definition: oggenc.c:723
#define LIBAVFORMAT_IDENT
Definition: version.h:46
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:629
static int ogg_build_opus_headers(AVCodecParameters *par, OGGStreamContext *oggstream, int bitexact, AVDictionary **m, AVChapter **chapters, unsigned int nb_chapters)
Definition: oggenc.c:386
#define FFMIN(a, b)
Definition: common.h:96
int serial_offset
Definition: oggenc.c:76
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:603
int64_t ff_vorbiscomment_length(AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
Definition: vorbiscomment.c:41
const char * name
Definition: avformat.h:505
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
Definition: oggenc.c:165
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
#define s(width, name)
Definition: cbs_vp9.c:257
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
AVDictionary * metadata
Definition: avformat.h:944
static volatile int checksum
Definition: adler32.c:30
uint8_t * header[3]
Definition: oggenc.c:51
#define SPEEX_HEADER_SIZE
Definition: oggenc.c:355
Stream structure.
Definition: avformat.h:880
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
AVIOContext * pb
I/O context.
Definition: avformat.h:1399
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:196
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
Definition: oggenc.c:190
void * buf
Definition: avisynth_c.h:766
int stream_index
Definition: oggenc.c:41
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: oggenc.c:706
Describe the class of an AVClass context structure.
Definition: log.h:67
#define OGG_CLASS(flavor, name)
Definition: oggenc.c:94
Rational number (pair of numerator and denominator).
Definition: rational.h:58
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:621
unsigned serial_num
serial number
Definition: oggenc.c:62
#define PARAM
Definition: oggenc.c:80
static int64_t pts
OGGPageList * page_list
Definition: oggenc.c:73
static int ogg_init(AVFormatContext *s)
Definition: oggenc.c:487
int sample_rate
Audio only.
Definition: avcodec.h:4097
Main libavformat public API header.
AVOutputFormat ff_oga_muxer
AVOutputFormat ff_ogg_muxer
#define OFFSET(x)
Definition: oggenc.c:79
static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
Definition: oggenc.c:148
int kfgshift
for theora granule
Definition: oggenc.c:54
int den
Denominator.
Definition: rational.h:60
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
A Quick Description Of Rate Distortion Theory We want to encode a video
#define av_free(p)
Definition: oggdec.h:101
int len
void * priv_data
Format private data.
Definition: avformat.h:1385
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:349
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4001
AVOutputFormat ff_opus_muxer
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:380
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:766
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
uint8_t segments[255]
Definition: oggenc.c:44
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1027
int stream_index
Definition: avcodec.h:1496
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:909
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int64_t pref_duration
preferred page duration (0 => fill all segments)
Definition: oggenc.c:75
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:482
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1004
static void ogg_write_pages(AVFormatContext *s, int flush)
Definition: oggenc.c:465
This structure stores compressed data.
Definition: avcodec.h:1471
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:292
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1487
static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st, OGGStreamContext *oggstream, int bitexact)
Definition: oggenc.c:416
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_WL32(p, v)
Definition: intreadwrite.h:426