Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 {
39  int64_t start_granule;
40  int64_t granule;
46  uint16_t size;
47 } OGGPage;
48 
49 typedef struct {
50  unsigned page_counter;
51  uint8_t *header[3];
52  int header_len[3];
54  int kfgshift;
55  int64_t last_kf_pts;
56  int vrev;
57  int eos;
58  unsigned page_count;
60  unsigned serial_num;
61  int64_t last_granule;
63 
64 typedef struct OGGPageList {
66  struct OGGPageList *next;
67 } OGGPageList;
68 
69 typedef struct {
70  const AVClass *class;
72  int pref_size;
73  int64_t pref_duration;
74 } OGGContext;
75 
76 #define OFFSET(x) offsetof(OGGContext, x)
77 #define PARAM AV_OPT_FLAG_ENCODING_PARAM
78 
79 static const AVOption options[] = {
80  { "pagesize", "preferred page size in bytes (deprecated)",
81  OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
82  { "page_duration", "preferred page duration, in microseconds",
83  OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
84  { NULL },
85 };
86 
87 static const AVClass ogg_muxer_class = {
88  .class_name = "Ogg muxer",
89  .item_name = av_default_item_name,
90  .option = options,
91  .version = LIBAVUTIL_VERSION_INT,
92 };
93 
94 
95 static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
96 {
97  int64_t pos = avio_tell(pb);
98  uint32_t checksum = ffio_get_checksum(pb);
99  avio_seek(pb, crc_offset, SEEK_SET);
100  avio_wb32(pb, checksum);
101  avio_seek(pb, pos, SEEK_SET);
102 }
103 
104 static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
105 {
106  OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
107  AVIOContext *pb;
108  int64_t crc_offset;
109  int ret, size;
110  uint8_t *buf;
111 
112  ret = avio_open_dyn_buf(&pb);
113  if (ret < 0)
114  return ret;
116  ffio_wfourcc(pb, "OggS");
117  avio_w8(pb, 0);
118  avio_w8(pb, page->flags | extra_flags);
119  avio_wl64(pb, page->granule);
120  avio_wl32(pb, oggstream->serial_num);
121  avio_wl32(pb, oggstream->page_counter++);
122  crc_offset = avio_tell(pb);
123  avio_wl32(pb, 0); // crc
124  avio_w8(pb, page->segments_count);
125  avio_write(pb, page->segments, page->segments_count);
126  avio_write(pb, page->data, page->size);
127 
128  ogg_update_checksum(s, pb, crc_offset);
129  avio_flush(pb);
130 
131  size = avio_close_dyn_buf(pb, &buf);
132  if (size < 0)
133  return size;
134 
135  avio_write(s->pb, buf, size);
136  avio_flush(s->pb);
137  av_free(buf);
138  oggstream->page_count--;
139  return 0;
140 }
141 
142 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
143 {
144  if (oggstream->kfgshift)
145  return (granule>>oggstream->kfgshift) +
146  (granule & ((1<<oggstream->kfgshift)-1));
147  else
148  return granule;
149 }
150 
152 {
153  AVStream *st2 = s->streams[next->stream_index];
154  AVStream *st = s->streams[page->stream_index];
155  int64_t next_granule, cur_granule;
156 
157  if (next->granule == -1 || page->granule == -1)
158  return 0;
159 
160  next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
161  st2->time_base, AV_TIME_BASE_Q);
162  cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
163  st ->time_base, AV_TIME_BASE_Q);
164  return next_granule > cur_granule;
165 }
166 
167 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
168 {
169  oggstream->page.granule = -1;
170  oggstream->page.flags = 0;
171  oggstream->page.segments_count = 0;
172  oggstream->page.size = 0;
173  return 0;
174 }
175 
177 {
178  OGGContext *ogg = s->priv_data;
179  OGGPageList **p = &ogg->page_list;
180  OGGPageList *l = av_mallocz(sizeof(*l));
181 
182  if (!l)
183  return AVERROR(ENOMEM);
184  l->page = oggstream->page;
185 
186  oggstream->page.start_granule = oggstream->page.granule;
187  oggstream->page_count++;
188  ogg_reset_cur_page(oggstream);
189 
190  while (*p) {
191  if (ogg_compare_granule(s, &(*p)->page, &l->page))
192  break;
193  p = &(*p)->next;
194  }
195  l->next = *p;
196  *p = l;
197 
198  return 0;
199 }
200 
202  uint8_t *data, unsigned size, int64_t granule,
203  int header)
204 {
205  OGGStreamContext *oggstream = st->priv_data;
206  OGGContext *ogg = s->priv_data;
207  int total_segments = size / 255 + 1;
208  uint8_t *p = data;
209  int i, segments, len, flush = 0;
210 
211  // Handles VFR by flushing page because this frame needs to have a timestamp
212  if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
213  ogg_granule_to_timestamp(oggstream, granule) >
214  ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
215  if (oggstream->page.granule != -1)
216  ogg_buffer_page(s, oggstream);
217  flush = 1;
218  }
219 
220  // avoid a continued page
221  if (!header && oggstream->page.size > 0 &&
222  MAX_PAGE_SIZE - oggstream->page.size < size) {
223  ogg_buffer_page(s, oggstream);
224  }
225 
226  for (i = 0; i < total_segments; ) {
227  OGGPage *page = &oggstream->page;
228 
229  segments = FFMIN(total_segments - i, 255 - page->segments_count);
230 
231  if (i && !page->segments_count)
232  page->flags |= 1; // continued packet
233 
234  memset(page->segments+page->segments_count, 255, segments - 1);
235  page->segments_count += segments - 1;
236 
237  len = FFMIN(size, segments*255);
238  page->segments[page->segments_count++] = len - (segments-1)*255;
239  memcpy(page->data+page->size, p, len);
240  p += len;
241  size -= len;
242  i += segments;
243  page->size += len;
244 
245  if (i == total_segments)
246  page->granule = granule;
247 
248  if (!header) {
249  AVStream *st = s->streams[page->stream_index];
250 
251  int64_t start = av_rescale_q(page->start_granule, st->time_base,
253  int64_t next = av_rescale_q(page->granule, st->time_base,
255 
256  if (page->segments_count == 255 ||
257  (ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
258  (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
259  ogg_buffer_page(s, oggstream);
260  }
261  }
262  }
263 
264  if (flush && oggstream->page.granule != -1)
265  ogg_buffer_page(s, oggstream);
266 
267  return 0;
268 }
269 
270 static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
271  int *header_len, AVDictionary **m, int framing_bit)
272 {
273  const char *vendor = bitexact ? "Libav" : LIBAVFORMAT_IDENT;
274  int size;
275  uint8_t *p, *p0;
276 
278 
279  size = offset + ff_vorbiscomment_length(*m, vendor) + framing_bit;
280  p = av_mallocz(size);
281  if (!p)
282  return NULL;
283  p0 = p;
284 
285  p += offset;
286  ff_vorbiscomment_write(&p, m, vendor);
287  if (framing_bit)
288  bytestream_put_byte(&p, 1);
289 
290  *header_len = size;
291  return p0;
292 }
293 
295  OGGStreamContext *oggstream, int bitexact,
296  AVDictionary **m)
297 {
298  enum FLACExtradataFormat format;
299  uint8_t *streaminfo;
300  uint8_t *p;
301 
302  if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
303  return -1;
304 
305  // first packet: STREAMINFO
306  oggstream->header_len[0] = 51;
307  oggstream->header[0] = av_mallocz(51); // per ogg flac specs
308  p = oggstream->header[0];
309  if (!p)
310  return AVERROR(ENOMEM);
311  bytestream_put_byte(&p, 0x7F);
312  bytestream_put_buffer(&p, "FLAC", 4);
313  bytestream_put_byte(&p, 1); // major version
314  bytestream_put_byte(&p, 0); // minor version
315  bytestream_put_be16(&p, 1); // headers packets without this one
316  bytestream_put_buffer(&p, "fLaC", 4);
317  bytestream_put_byte(&p, 0x00); // streaminfo
318  bytestream_put_be24(&p, 34);
320 
321  // second packet: VorbisComment
322  p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
323  if (!p)
324  return AVERROR(ENOMEM);
325  oggstream->header[1] = p;
326  bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
327  bytestream_put_be24(&p, oggstream->header_len[1] - 4);
328 
329  return 0;
330 }
331 
332 #define SPEEX_HEADER_SIZE 80
333 
335  OGGStreamContext *oggstream, int bitexact,
336  AVDictionary **m)
337 {
338  uint8_t *p;
339 
340  if (avctx->extradata_size < SPEEX_HEADER_SIZE)
341  return -1;
342 
343  // first packet: Speex header
345  if (!p)
346  return AVERROR(ENOMEM);
347  oggstream->header[0] = p;
348  oggstream->header_len[0] = SPEEX_HEADER_SIZE;
350  AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
351 
352  // second packet: VorbisComment
353  p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
354  if (!p)
355  return AVERROR(ENOMEM);
356  oggstream->header[1] = p;
357 
358  return 0;
359 }
360 
361 #define OPUS_HEADER_SIZE 19
362 
364  OGGStreamContext *oggstream, int bitexact,
365  AVDictionary **m)
366 {
367  uint8_t *p;
368 
369  if (avctx->extradata_size < OPUS_HEADER_SIZE)
370  return -1;
371 
372  /* first packet: Opus header */
373  p = av_mallocz(avctx->extradata_size);
374  if (!p)
375  return AVERROR(ENOMEM);
376  oggstream->header[0] = p;
377  oggstream->header_len[0] = avctx->extradata_size;
378  bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
379 
380  /* second packet: VorbisComment */
381  p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
382  if (!p)
383  return AVERROR(ENOMEM);
384  oggstream->header[1] = p;
385  bytestream_put_buffer(&p, "OpusTags", 8);
386 
387  return 0;
388 }
389 
391 {
392  OGGContext *ogg = s->priv_data;
393  OGGPageList *next, *p;
394 
395  if (!ogg->page_list)
396  return;
397 
398  for (p = ogg->page_list; p; ) {
399  OGGStreamContext *oggstream =
401  if (oggstream->page_count < 2 && !flush)
402  break;
403  ogg_write_page(s, &p->page,
404  flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
405  next = p->next;
406  av_freep(&p);
407  p = next;
408  }
409  ogg->page_list = p;
410 }
411 
413 {
414  OGGContext *ogg = s->priv_data;
415  OGGStreamContext *oggstream;
416  int i, j;
417 
418  if (ogg->pref_size)
419  av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
420 
421  for (i = 0; i < s->nb_streams; i++) {
422  AVStream *st = s->streams[i];
423  unsigned serial_num = i;
424 
425  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
426  if (st->codec->codec_id == AV_CODEC_ID_OPUS)
427  /* Opus requires a fixed 48kHz clock */
428  avpriv_set_pts_info(st, 64, 1, 48000);
429  else
430  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
431 
432  if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
434  st->codec->codec_id != AV_CODEC_ID_SPEEX &&
435  st->codec->codec_id != AV_CODEC_ID_FLAC &&
436  st->codec->codec_id != AV_CODEC_ID_OPUS) {
437  av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
438  return -1;
439  }
440 
441  if (!st->codec->extradata || !st->codec->extradata_size) {
442  av_log(s, AV_LOG_ERROR, "No extradata present\n");
443  return -1;
444  }
445  oggstream = av_mallocz(sizeof(*oggstream));
446  oggstream->page.stream_index = i;
447 
448  if (!(s->flags & AVFMT_FLAG_BITEXACT))
449  do {
450  serial_num = av_get_random_seed();
451  for (j = 0; j < i; j++) {
452  OGGStreamContext *sc = s->streams[j]->priv_data;
453  if (serial_num == sc->serial_num)
454  break;
455  }
456  } while (j < i);
457  oggstream->serial_num = serial_num;
458 
459  st->priv_data = oggstream;
460  if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
461  int err = ogg_build_flac_headers(st->codec, oggstream,
463  &s->metadata);
464  if (err) {
465  av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
466  av_freep(&st->priv_data);
467  return err;
468  }
469  } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
470  int err = ogg_build_speex_headers(st->codec, oggstream,
472  &s->metadata);
473  if (err) {
474  av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
475  av_freep(&st->priv_data);
476  return err;
477  }
478  } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
479  int err = ogg_build_opus_headers(st->codec, oggstream,
481  &s->metadata);
482  if (err) {
483  av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
484  av_freep(&st->priv_data);
485  return err;
486  }
487  } else {
488  uint8_t *p;
489  const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
490  int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
491  int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
492 
494  st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
495  oggstream->header, oggstream->header_len) < 0) {
496  av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
497  av_freep(&st->priv_data);
498  return -1;
499  }
500 
502  &oggstream->header_len[1], &s->metadata,
503  framing_bit);
504  oggstream->header[1] = p;
505  if (!p)
506  return AVERROR(ENOMEM);
507 
508  bytestream_put_byte(&p, header_type);
509  bytestream_put_buffer(&p, cstr, 6);
510 
511  if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
514  oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
515  oggstream->vrev = oggstream->header[0][9];
516  av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
517  oggstream->kfgshift, oggstream->vrev);
518  }
519  }
520  }
521 
522  for (j = 0; j < s->nb_streams; j++) {
523  OGGStreamContext *oggstream = s->streams[j]->priv_data;
524  ogg_buffer_data(s, s->streams[j], oggstream->header[0],
525  oggstream->header_len[0], 0, 1);
526  oggstream->page.flags |= 2; // bos
527  ogg_buffer_page(s, oggstream);
528  }
529  for (j = 0; j < s->nb_streams; j++) {
530  AVStream *st = s->streams[j];
531  OGGStreamContext *oggstream = st->priv_data;
532  for (i = 1; i < 3; i++) {
533  if (oggstream && oggstream->header_len[i])
534  ogg_buffer_data(s, st, oggstream->header[i],
535  oggstream->header_len[i], 0, 1);
536  }
537  ogg_buffer_page(s, oggstream);
538  }
539 
540  oggstream->page.start_granule = AV_NOPTS_VALUE;
541 
542  ogg_write_pages(s, 2);
543 
544  return 0;
545 }
546 
548 {
549  AVStream *st = s->streams[pkt->stream_index];
550  OGGStreamContext *oggstream = st->priv_data;
551  int ret;
552  int64_t granule;
553 
554  if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
555  int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
556  int pframe_count;
557  if (pkt->flags & AV_PKT_FLAG_KEY)
558  oggstream->last_kf_pts = pts;
559  pframe_count = pts - oggstream->last_kf_pts;
560  // prevent frame count from overflow if key frame flag is not set
561  if (pframe_count >= (1<<oggstream->kfgshift)) {
562  oggstream->last_kf_pts += pframe_count;
563  pframe_count = 0;
564  }
565  granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
566  } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
567  granule = pkt->pts + pkt->duration + av_rescale_q(st->codec->delay, (AVRational){ 1, st->codec->sample_rate }, st->time_base);
568  else
569  granule = pkt->pts + pkt->duration;
570 
571  if (oggstream->page.start_granule == AV_NOPTS_VALUE)
572  oggstream->page.start_granule = pkt->pts;
573 
574  ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
575  if (ret < 0)
576  return ret;
577 
578  ogg_write_pages(s, 0);
579 
580  oggstream->last_granule = granule;
581 
582  return 0;
583 }
584 
586 {
587  int i;
588 
589  if (pkt)
590  return ogg_write_packet_internal(s, pkt);
591 
592  for (i = 0; i < s->nb_streams; i++) {
593  OGGStreamContext *oggstream = s->streams[i]->priv_data;
594  if (oggstream->page.segments_count)
595  ogg_buffer_page(s, oggstream);
596  }
597 
598  ogg_write_pages(s, 2);
599  return 0;
600 }
601 
603 {
604  int i;
605 
606  /* flush current page if needed */
607  for (i = 0; i < s->nb_streams; i++) {
608  OGGStreamContext *oggstream = s->streams[i]->priv_data;
609 
610  if (oggstream->page.size > 0)
611  ogg_buffer_page(s, oggstream);
612  }
613 
614  ogg_write_pages(s, 1);
615 
616  for (i = 0; i < s->nb_streams; i++) {
617  AVStream *st = s->streams[i];
618  OGGStreamContext *oggstream = st->priv_data;
619  if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
620  st->codec->codec_id == AV_CODEC_ID_SPEEX ||
621  st->codec->codec_id == AV_CODEC_ID_OPUS) {
622  av_free(oggstream->header[0]);
623  }
624  av_freep(&oggstream->header[1]);
625  av_freep(&st->priv_data);
626  }
627  return 0;
628 }
629 
631  .name = "ogg",
632  .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
633  .mime_type = "application/ogg",
634  .extensions = "ogg,ogv,spx,opus",
635  .priv_data_size = sizeof(OGGContext),
636  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
638  .video_codec = AV_CODEC_ID_THEORA,
643  .priv_class = &ogg_muxer_class,
644 };
int header_len[3]
Definition: oggenc.c:52
static int ogg_write_header(AVFormatContext *s)
Definition: oggenc.c:412
int64_t granule
Definition: oggenc.c:40
int64_t last_granule
last packet granule
Definition: oggenc.c:61
Bytestream IO Context.
Definition: avio.h:68
int size
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:966
AVOption.
Definition: opt.h:234
static int ogg_build_speex_headers(AVCodecContext *avctx, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:334
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
unsigned page_count
number of page buffered
Definition: oggenc.c:58
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:2821
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
uint8_t data[MAX_PAGE_SIZE]
Definition: oggenc.c:45
int avpriv_flac_is_extradata_valid(AVCodecContext *avctx, enum FLACExtradataFormat *format, uint8_t **streaminfo_start)
Validate the FLAC extradata.
Definition: flac.c:169
OGGPage page
Definition: oggenc.c:65
void * priv_data
Definition: avformat.h:719
OGGPage page
current page
Definition: oggenc.c:59
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:425
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:954
#define OPUS_HEADER_SIZE
Definition: oggenc.c:361
int ff_vorbiscomment_write(uint8_t **p, AVDictionary **m, const char *vendor_string)
Write a VorbisComment into a buffer.
Definition: vorbiscomment.c:53
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
Definition: oggenc.c:104
Format I/O context.
Definition: avformat.h:922
static int ogg_build_flac_headers(AVCodecContext *avctx, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:294
static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
Definition: oggenc.c:95
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
uint8_t
AVOptions.
uint16_t size
Definition: oggenc.c:46
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
#define MAX_PAGE_SIZE
Definition: oggenc.c:36
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
uint8_t segments_count
Definition: oggenc.c:43
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
int64_t last_kf_pts
Definition: oggenc.c:55
const char data[16]
Definition: mxf.c:70
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1033
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
int ff_vorbiscomment_length(AVDictionary *m, const char *vendor_string)
Calculate the length in bytes of a VorbisComment.
Definition: vorbiscomment.c:40
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
Definition: oggenc.c:38
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1050
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
static const AVOption options[]
Definition: oggenc.c:79
static const AVClass ogg_muxer_class
Definition: oggenc.c:87
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:324
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
FLACExtradataFormat
Definition: flac.h:58
unsigned page_counter
Definition: oggenc.c:50
const AVMetadataConv ff_vorbiscomment_metadata_conv[]
VorbisComment metadata conversion mapping.
Definition: vorbiscomment.c:33
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int ogg_buffer_data(AVFormatContext *s, AVStream *st, uint8_t *data, unsigned size, int64_t granule, int header)
Definition: oggenc.c:201
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
int64_t start_granule
Definition: oggenc.c:39
#define AVERROR(e)
Definition: error.h:43
struct OGGPageList * next
Definition: oggenc.c:66
int pref_size
preferred page size (0 => fill all segments)
Definition: oggenc.c:72
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
uint8_t flags
Definition: oggenc.c:42
uint8_t segments[255]
Definition: oggdec.h:80
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
Definition: oggenc.c:167
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
Definition: oggenc.c:142
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: oggenc.c:547
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
static int ogg_write_trailer(AVFormatContext *s)
Definition: oggenc.c:602
#define LIBAVFORMAT_IDENT
Definition: version.h:44
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:431
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
#define FFMIN(a, b)
Definition: common.h:57
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:411
const char * name
Definition: avformat.h:446
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
Definition: oggenc.c:151
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
uint8_t * header[3]
Definition: oggenc.c:51
#define SPEEX_HEADER_SIZE
Definition: oggenc.c:332
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static uint8_t * ogg_write_vorbiscomment(int offset, int bitexact, int *header_len, AVDictionary **m, int framing_bit)
Definition: oggenc.c:270
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
av_default_item_name
Definition: dnxhdenc.c:52
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1050
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
Definition: oggenc.c:176
int stream_index
Definition: oggenc.c:41
int extradata_size
Definition: avcodec.h:1165
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: oggenc.c:585
Describe the class of an AVClass context structure.
Definition: log.h:33
AVOutputFormat ff_ogg_muxer
Definition: oggenc.c:630
rational number numerator/denominator
Definition: rational.h:43
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:1049
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:423
int avpriv_split_xiph_headers(uint8_t *extradata, int extradata_size, int first_header_size, 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
unsigned serial_num
serial number
Definition: oggenc.c:60
#define PARAM
Definition: oggenc.c:77
OGGPageList * page_list
Definition: oggenc.c:71
static int ogg_build_opus_headers(AVCodecContext *avctx, OGGStreamContext *oggstream, int bitexact, AVDictionary **m)
Definition: oggenc.c:363
Main libavformat public API header.
#define OFFSET(x)
Definition: oggenc.c:76
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1771
int kfgshift
for theora granule
Definition: oggenc.c:54
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:363
Definition: oggdec.h:96
int len
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
uint8_t segments[255]
Definition: oggenc.c:44
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:95
int stream_index
Definition: avcodec.h:975
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
int64_t pref_duration
preferred page duration (0 => fill all segments)
Definition: oggenc.c:73
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:431
static void ogg_write_pages(AVFormatContext *s, int flush)
Definition: oggenc.c:390
This structure stores compressed data.
Definition: avcodec.h:950
int delay
Codec delay.
Definition: avcodec.h:1212
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228