Libav
matroskaenc.c
Go to the documentation of this file.
1 /*
2  * Matroska muxer
3  * Copyright (c) 2007 David Conrad
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 "avc.h"
25 #include "hevc.h"
26 #include "avformat.h"
27 #include "avlanguage.h"
28 #include "flacenc.h"
29 #include "internal.h"
30 #include "isom.h"
31 #include "matroska.h"
32 #include "riff.h"
33 #include "wv.h"
34 
35 #include "libavutil/avstring.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/intfloat.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/lfg.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/random_seed.h"
43 #include "libavutil/samplefmt.h"
44 
45 #include "libavcodec/xiph.h"
46 #include "libavcodec/mpeg4audio.h"
47 
48 typedef struct ebml_master {
49  int64_t pos;
50  int sizebytes;
51 } ebml_master;
52 
53 typedef struct mkv_seekhead_entry {
54  unsigned int elementid;
55  uint64_t segmentpos;
57 
58 typedef struct mkv_seekhead {
59  int64_t filepos;
60  int64_t segment_offset;
65 } mkv_seekhead;
66 
67 typedef struct {
68  uint64_t pts;
69  int tracknum;
70  int64_t cluster_pos;
71 } mkv_cuepoint;
72 
73 typedef struct {
74  int64_t segment_offset;
77 } mkv_cues;
78 
79 typedef struct {
80  int write_dts;
81 } mkv_track;
82 
83 #define MODE_MATROSKAv2 0x01
84 #define MODE_WEBM 0x02
85 
86 typedef struct MatroskaMuxContext {
87  const AVClass *class;
88  int mode;
91  int64_t segment_offset;
93  int64_t cluster_pos;
94  int64_t cluster_pts;
95  int64_t duration_offset;
96  int64_t duration;
100 
102 
104 
107  int64_t cues_pos;
111 
112 
115 #define MAX_SEEKENTRY_SIZE 21
116 
119 #define MAX_CUETRACKPOS_SIZE 22
120 
122 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
123 
124 
125 static int ebml_id_size(unsigned int id)
126 {
127  return (av_log2(id+1)-1)/7+1;
128 }
129 
130 static void put_ebml_id(AVIOContext *pb, unsigned int id)
131 {
132  int i = ebml_id_size(id);
133  while (i--)
134  avio_w8(pb, id >> (i*8));
135 }
136 
142 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
143 {
144  assert(bytes <= 8);
145  avio_w8(pb, 0x1ff >> bytes);
146  while (--bytes)
147  avio_w8(pb, 0xff);
148 }
149 
153 static int ebml_num_size(uint64_t num)
154 {
155  int bytes = 1;
156  while ((num+1) >> bytes*7) bytes++;
157  return bytes;
158 }
159 
166 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
167 {
168  int i, needed_bytes = ebml_num_size(num);
169 
170  // sizes larger than this are currently undefined in EBML
171  assert(num < (1ULL<<56)-1);
172 
173  if (bytes == 0)
174  // don't care how many bytes are used, so use the min
175  bytes = needed_bytes;
176  // the bytes needed to write the given size would exceed the bytes
177  // that we need to use, so write unknown size. This shouldn't happen.
178  assert(bytes >= needed_bytes);
179 
180  num |= 1ULL << bytes*7;
181  for (i = bytes - 1; i >= 0; i--)
182  avio_w8(pb, num >> i*8);
183 }
184 
185 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
186 {
187  int i, bytes = 1;
188  uint64_t tmp = val;
189  while (tmp>>=8) bytes++;
190 
191  put_ebml_id(pb, elementid);
192  put_ebml_num(pb, bytes, 0);
193  for (i = bytes - 1; i >= 0; i--)
194  avio_w8(pb, val >> i*8);
195 }
196 
197 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
198 {
199  put_ebml_id(pb, elementid);
200  put_ebml_num(pb, 8, 0);
201  avio_wb64(pb, av_double2int(val));
202 }
203 
204 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
205  const void *buf, int size)
206 {
207  put_ebml_id(pb, elementid);
208  put_ebml_num(pb, size, 0);
209  avio_write(pb, buf, size);
210 }
211 
212 static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
213 {
214  put_ebml_binary(pb, elementid, str, strlen(str));
215 }
216 
223 static void put_ebml_void(AVIOContext *pb, uint64_t size)
224 {
225  int64_t currentpos = avio_tell(pb);
226 
227  assert(size >= 2);
228 
230  // we need to subtract the length needed to store the size from the
231  // size we need to reserve so 2 cases, we use 8 bytes to store the
232  // size if possible, 1 byte otherwise
233  if (size < 10)
234  put_ebml_num(pb, size-1, 0);
235  else
236  put_ebml_num(pb, size-9, 8);
237  while(avio_tell(pb) < currentpos + size)
238  avio_w8(pb, 0);
239 }
240 
241 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
242 {
243  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
244  put_ebml_id(pb, elementid);
245  put_ebml_size_unknown(pb, bytes);
246  return (ebml_master){ avio_tell(pb), bytes };
247 }
248 
249 static void end_ebml_master(AVIOContext *pb, ebml_master master)
250 {
251  int64_t pos = avio_tell(pb);
252 
253  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
254  return;
255  put_ebml_num(pb, pos - master.pos, master.sizebytes);
256  avio_seek(pb, pos, SEEK_SET);
257 }
258 
259 static void put_xiph_size(AVIOContext *pb, int size)
260 {
261  int i;
262  for (i = 0; i < size / 255; i++)
263  avio_w8(pb, 255);
264  avio_w8(pb, size % 255);
265 }
266 
278 static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
279 {
280  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
281  if (new_seekhead == NULL)
282  return NULL;
283 
284  new_seekhead->segment_offset = segment_offset;
285 
286  if (numelements > 0) {
287  new_seekhead->filepos = avio_tell(pb);
288  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
289  // and size, and 3 bytes to guarantee that an EBML void element
290  // will fit afterwards
291  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
292  new_seekhead->max_entries = numelements;
293  put_ebml_void(pb, new_seekhead->reserved_size);
294  }
295  return new_seekhead;
296 }
297 
298 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
299 {
300  int err;
301 
302  // don't store more elements than we reserved space for
303  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
304  return -1;
305 
306  if ((err = av_reallocp_array(&seekhead->entries, seekhead->num_entries + 1,
307  sizeof(*seekhead->entries))) < 0) {
308  seekhead->num_entries = 0;
309  return err;
310  }
311 
312  seekhead->entries[seekhead->num_entries].elementid = elementid;
313  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
314 
315  return 0;
316 }
317 
327 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
328 {
329  ebml_master metaseek, seekentry;
330  int64_t currentpos;
331  int i;
332 
333  currentpos = avio_tell(pb);
334 
335  if (seekhead->reserved_size > 0) {
336  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
337  currentpos = -1;
338  goto fail;
339  }
340  }
341 
342  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
343  for (i = 0; i < seekhead->num_entries; i++) {
344  mkv_seekhead_entry *entry = &seekhead->entries[i];
345 
347 
349  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
350  put_ebml_id(pb, entry->elementid);
351 
353  end_ebml_master(pb, seekentry);
354  }
355  end_ebml_master(pb, metaseek);
356 
357  if (seekhead->reserved_size > 0) {
358  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
359  put_ebml_void(pb, remaining);
360  avio_seek(pb, currentpos, SEEK_SET);
361 
362  currentpos = seekhead->filepos;
363  }
364 fail:
365  av_free(seekhead->entries);
366  av_free(seekhead);
367 
368  return currentpos;
369 }
370 
371 static mkv_cues * mkv_start_cues(int64_t segment_offset)
372 {
373  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
374  if (cues == NULL)
375  return NULL;
376 
377  cues->segment_offset = segment_offset;
378  return cues;
379 }
380 
381 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
382 {
383  int err;
384 
385  if (ts < 0)
386  return 0;
387 
388  if ((err = av_reallocp_array(&cues->entries, cues->num_entries + 1,
389  sizeof(*cues->entries))) < 0) {
390  cues->num_entries = 0;
391  return err;
392  }
393 
394  cues->entries[cues->num_entries].pts = ts;
395  cues->entries[cues->num_entries].tracknum = stream + 1;
396  cues->entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
397 
398  return 0;
399 }
400 
401 static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
402 {
403  ebml_master cues_element;
404  int64_t currentpos;
405  int i, j;
406 
407  currentpos = avio_tell(pb);
408  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
409 
410  for (i = 0; i < cues->num_entries; i++) {
411  ebml_master cuepoint, track_positions;
412  mkv_cuepoint *entry = &cues->entries[i];
413  uint64_t pts = entry->pts;
414 
415  cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
417 
418  // put all the entries from different tracks that have the exact same
419  // timestamp into the same CuePoint
420  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
422  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
423  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
424  end_ebml_master(pb, track_positions);
425  }
426  i += j - 1;
427  end_ebml_master(pb, cuepoint);
428  }
429  end_ebml_master(pb, cues_element);
430 
431  return currentpos;
432 }
433 
435 {
436  uint8_t *header_start[3];
437  int header_len[3];
438  int first_header_size;
439  int j;
440 
441  if (codec->codec_id == AV_CODEC_ID_VORBIS)
442  first_header_size = 30;
443  else
444  first_header_size = 42;
445 
447  first_header_size, header_start, header_len) < 0) {
448  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
449  return -1;
450  }
451 
452  avio_w8(pb, 2); // number packets - 1
453  for (j = 0; j < 2; j++) {
454  put_xiph_size(pb, header_len[j]);
455  }
456  for (j = 0; j < 3; j++)
457  avio_write(pb, header_start[j], header_len[j]);
458 
459  return 0;
460 }
461 
463 {
464  if (codec->extradata && codec->extradata_size == 2)
465  avio_write(pb, codec->extradata, 2);
466  else
467  avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs
468  return 0;
469 }
470 
471 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
472 {
473  MPEG4AudioConfig mp4ac;
474 
475  if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
476  codec->extradata_size * 8, 1) < 0) {
477  av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
478  return;
479  }
480 
481  *sample_rate = mp4ac.sample_rate;
482  *output_sample_rate = mp4ac.ext_sample_rate;
483 }
484 
485 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
486 {
487  AVIOContext *dyn_cp;
488  uint8_t *codecpriv;
489  int ret, codecpriv_size;
490 
491  ret = avio_open_dyn_buf(&dyn_cp);
492  if(ret < 0)
493  return ret;
494 
495  if (native_id) {
496  if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA)
497  ret = put_xiph_codecpriv(s, dyn_cp, codec);
498  else if (codec->codec_id == AV_CODEC_ID_FLAC)
499  ret = ff_flac_write_header(dyn_cp, codec, 1);
500  else if (codec->codec_id == AV_CODEC_ID_WAVPACK)
501  ret = put_wv_codecpriv(dyn_cp, codec);
502  else if (codec->codec_id == AV_CODEC_ID_H264)
503  ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
504  else if (codec->codec_id == AV_CODEC_ID_HEVC)
505  ret = ff_isom_write_hvcc(dyn_cp, codec->extradata, codec->extradata_size, 0);
506  else if (codec->codec_id == AV_CODEC_ID_ALAC) {
507  if (codec->extradata_size < 36) {
508  av_log(s, AV_LOG_ERROR,
509  "Invalid extradata found, ALAC expects a 36-byte "
510  "QuickTime atom.");
511  ret = AVERROR_INVALIDDATA;
512  } else
513  avio_write(dyn_cp, codec->extradata + 12,
514  codec->extradata_size - 12);
515  }
516  else if (codec->extradata_size)
517  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
518  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
519  if (qt_id) {
520  if (!codec->codec_tag)
522  if (codec->extradata_size)
523  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
524  } else {
525  if (!codec->codec_tag)
527  if (!codec->codec_tag) {
528  av_log(s, AV_LOG_ERROR, "No bmp codec ID found.\n");
529  ret = -1;
530  }
531 
532  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
533  }
534 
535  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
536  unsigned int tag;
538  if (!tag) {
539  av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
540  ret = -1;
541  }
542  if (!codec->codec_tag)
543  codec->codec_tag = tag;
544 
545  ff_put_wav_header(dyn_cp, codec);
546  }
547 
548  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
549  if (codecpriv_size)
550  put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
551  av_free(codecpriv);
552  return ret;
553 }
554 
556 {
557  MatroskaMuxContext *mkv = s->priv_data;
558  AVIOContext *pb = s->pb;
559  ebml_master tracks;
560  int i, j, ret;
561 
563  if (ret < 0) return ret;
564 
565  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
566  for (i = 0; i < s->nb_streams; i++) {
567  AVStream *st = s->streams[i];
568  AVCodecContext *codec = st->codec;
569  ebml_master subinfo, track;
570  int native_id = 0;
571  int qt_id = 0;
572  int bit_depth = av_get_bits_per_sample(codec->codec_id);
573  int sample_rate = codec->sample_rate;
574  int output_sample_rate = 0;
576 
577  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
578  mkv->have_attachments = 1;
579  continue;
580  }
581 
582  if (!bit_depth)
583  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
584 
585  if (codec->codec_id == AV_CODEC_ID_AAC)
586  get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
587 
590  put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
591  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
592 
593  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
595  tag = av_dict_get(st->metadata, "language", NULL, 0);
596  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
597 
598  // The default value for TRACKFLAGDEFAULT is 1, so add element
599  // if we need to clear it.
600  if (!(st->disposition & AV_DISPOSITION_DEFAULT))
602 
603  // look for a codec ID string specific to mkv to use,
604  // if none are found, use AVI codes
605  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
606  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
608  native_id = 1;
609  break;
610  }
611  }
612 
613  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
614  codec->codec_id == AV_CODEC_ID_VORBIS)) {
615  av_log(s, AV_LOG_ERROR,
616  "Only VP8 video and Vorbis audio are supported for WebM.\n");
617  return AVERROR(EINVAL);
618  }
619 
620  switch (codec->codec_type) {
621  case AVMEDIA_TYPE_VIDEO:
624 
625  if (!native_id &&
628  || codec->codec_id == AV_CODEC_ID_SVQ1
629  || codec->codec_id == AV_CODEC_ID_SVQ3
630  || codec->codec_id == AV_CODEC_ID_CINEPAK))
631  qt_id = 1;
632 
633  if (qt_id)
634  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
635  else if (!native_id) {
636  // if there is no mkv-specific codec ID, use VFW mode
637  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
638  mkv->tracks[i].write_dts = 1;
639  }
640 
641  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
642  // XXX: interlace flag?
645  if ((tag = av_dict_get(s->metadata, "stereo_mode", NULL, 0))) {
646  uint8_t stereo_fmt = atoi(tag->value);
647  int valid_fmt = 0;
648 
649  switch (mkv->mode) {
650  case MODE_WEBM:
653  valid_fmt = 1;
654  break;
655  case MODE_MATROSKAv2:
657  valid_fmt = 1;
658  break;
659  }
660 
661  if (valid_fmt)
662  put_ebml_uint (pb, MATROSKA_ID_VIDEOSTEREOMODE, stereo_fmt);
663  }
664  if (st->sample_aspect_ratio.num) {
665  int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
669  }
670  end_ebml_master(pb, subinfo);
671  break;
672 
673  case AVMEDIA_TYPE_AUDIO:
675 
676  if (!native_id)
677  // no mkv-specific ID, use ACM mode
678  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
679 
680  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
683  if (output_sample_rate)
684  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
685  if (bit_depth)
687  end_ebml_master(pb, subinfo);
688  break;
689 
692  if (!native_id) {
693  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
694  return AVERROR(ENOSYS);
695  }
696  break;
697  default:
698  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
699  break;
700  }
701  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
702  if (ret < 0) return ret;
703 
704  end_ebml_master(pb, track);
705 
706  // ms precision is the de-facto standard timescale for mkv files
707  avpriv_set_pts_info(st, 64, 1, 1000);
708  }
709  end_ebml_master(pb, tracks);
710  return 0;
711 }
712 
714 {
715  MatroskaMuxContext *mkv = s->priv_data;
716  AVIOContext *pb = s->pb;
717  ebml_master chapters, editionentry;
718  AVRational scale = {1, 1E9};
719  int i, ret;
720 
721  if (!s->nb_chapters || mkv->wrote_chapters)
722  return 0;
723 
725  if (ret < 0) return ret;
726 
727  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
728  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
731  for (i = 0; i < s->nb_chapters; i++) {
732  ebml_master chapteratom, chapterdisplay;
733  AVChapter *c = s->chapters[i];
735 
736  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
739  av_rescale_q(c->start, c->time_base, scale));
741  av_rescale_q(c->end, c->time_base, scale));
744  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
745  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
748  end_ebml_master(pb, chapterdisplay);
749  }
750  end_ebml_master(pb, chapteratom);
751  }
752  end_ebml_master(pb, editionentry);
753  end_ebml_master(pb, chapters);
754 
755  mkv->wrote_chapters = 1;
756  return 0;
757 }
758 
760 {
761  uint8_t *key = av_strdup(t->key);
762  uint8_t *p = key;
763  const uint8_t *lang = NULL;
765 
766  if ((p = strrchr(p, '-')) &&
767  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
768  *p = 0;
769 
770  p = key;
771  while (*p) {
772  if (*p == ' ')
773  *p = '_';
774  else if (*p >= 'a' && *p <= 'z')
775  *p -= 'a' - 'A';
776  p++;
777  }
778 
781  if (lang)
784  end_ebml_master(pb, tag);
785 
786  av_freep(&key);
787 }
788 
789 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
790  unsigned int uid, ebml_master *tags)
791 {
792  MatroskaMuxContext *mkv = s->priv_data;
793  ebml_master tag, targets;
795  int ret;
796 
797  if (!tags->pos) {
799  if (ret < 0) return ret;
800 
801  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
802  }
803 
804  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
805  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
806  if (elementid)
807  put_ebml_uint(s->pb, elementid, uid);
808  end_ebml_master(s->pb, targets);
809 
810  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
811  if (av_strcasecmp(t->key, "title") &&
812  av_strcasecmp(t->key, "encoding_tool"))
813  mkv_write_simpletag(s->pb, t);
814 
815  end_ebml_master(s->pb, tag);
816  return 0;
817 }
818 
820 {
821  ebml_master tags = {0};
822  int i, ret;
823 
825 
827  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
828  if (ret < 0) return ret;
829  }
830 
831  for (i = 0; i < s->nb_streams; i++) {
832  AVStream *st = s->streams[i];
833 
834  if (!av_dict_get(st->metadata, "", 0, AV_DICT_IGNORE_SUFFIX))
835  continue;
836 
837  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
838  if (ret < 0) return ret;
839  }
840 
841  for (i = 0; i < s->nb_chapters; i++) {
842  AVChapter *ch = s->chapters[i];
843 
845  continue;
846 
847  ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
848  if (ret < 0) return ret;
849  }
850 
851  if (tags.pos)
852  end_ebml_master(s->pb, tags);
853  return 0;
854 }
855 
857 {
858  MatroskaMuxContext *mkv = s->priv_data;
859  AVIOContext *pb = s->pb;
860  ebml_master attachments;
861  AVLFG c;
862  int i, ret;
863 
864  if (!mkv->have_attachments)
865  return 0;
866 
868 
870  if (ret < 0) return ret;
871 
872  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
873 
874  for (i = 0; i < s->nb_streams; i++) {
875  AVStream *st = s->streams[i];
876  ebml_master attached_file;
878  const char *mimetype = NULL;
879 
881  continue;
882 
883  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
884 
885  if (t = av_dict_get(st->metadata, "title", NULL, 0))
887  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
888  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
889  return AVERROR(EINVAL);
890  }
892  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
893  mimetype = t->value;
894  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
895  int i;
896  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
897  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
898  mimetype = ff_mkv_mime_tags[i].str;
899  break;
900  }
901  }
902  if (!mimetype) {
903  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
904  "it cannot be deduced from the codec id.\n", i);
905  return AVERROR(EINVAL);
906  }
907 
911  end_ebml_master(pb, attached_file);
912  }
913  end_ebml_master(pb, attachments);
914 
915  return 0;
916 }
917 
919 {
920  MatroskaMuxContext *mkv = s->priv_data;
921  AVIOContext *pb = s->pb;
922  ebml_master ebml_header, segment_info;
924  int ret, i;
925 
926  if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
927  else mkv->mode = MODE_MATROSKAv2;
928 
929  mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
930  if (!mkv->tracks)
931  return AVERROR(ENOMEM);
932 
933  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
941  end_ebml_master(pb, ebml_header);
942 
944  mkv->segment_offset = avio_tell(pb);
945 
946  // we write 2 seek heads - one at the end of the file to point to each
947  // cluster, and one at the beginning to point to all other level one
948  // elements (including the seek head at the end of the file), which
949  // isn't more than 10 elements if we only write one of each other
950  // currently defined level 1 element
951  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
952  if (!mkv->main_seekhead)
953  return AVERROR(ENOMEM);
954 
956  if (ret < 0) return ret;
957 
958  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
960  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
962  if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
963  uint32_t segment_uid[4];
964  AVLFG lfg;
965 
967 
968  for (i = 0; i < 4; i++)
969  segment_uid[i] = av_lfg_get(&lfg);
970 
972  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
974  else
976  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
977  }
978 
979  // reserve space for the duration
980  mkv->duration = 0;
981  mkv->duration_offset = avio_tell(pb);
982  put_ebml_void(pb, 11); // assumes double-precision float to be written
983  end_ebml_master(pb, segment_info);
984 
985  ret = mkv_write_tracks(s);
986  if (ret < 0) return ret;
987 
988  if (mkv->mode != MODE_WEBM) {
989  ret = mkv_write_chapters(s);
990  if (ret < 0) return ret;
991 
992  ret = mkv_write_tags(s);
993  if (ret < 0) return ret;
994 
995  ret = mkv_write_attachments(s);
996  if (ret < 0) return ret;
997  }
998 
999  if (!s->pb->seekable)
1001 
1002  mkv->cues = mkv_start_cues(mkv->segment_offset);
1003  if (mkv->cues == NULL)
1004  return AVERROR(ENOMEM);
1005 
1006  if (pb->seekable && mkv->reserve_cues_space) {
1007  mkv->cues_pos = avio_tell(pb);
1009  }
1010 
1012  mkv->cur_audio_pkt.size = 0;
1013 
1014  avio_flush(pb);
1015 
1016  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1017  // after 4k and on a keyframe
1018  if (pb->seekable) {
1019  if (mkv->cluster_time_limit < 0)
1020  mkv->cluster_time_limit = 5000;
1021  if (mkv->cluster_size_limit < 0)
1022  mkv->cluster_size_limit = 5 * 1024 * 1024;
1023  } else {
1024  if (mkv->cluster_time_limit < 0)
1025  mkv->cluster_time_limit = 1000;
1026  if (mkv->cluster_size_limit < 0)
1027  mkv->cluster_size_limit = 32 * 1024;
1028  }
1029 
1030  return 0;
1031 }
1032 
1033 static int mkv_blockgroup_size(int pkt_size)
1034 {
1035  int size = pkt_size + 4;
1036  size += ebml_num_size(size);
1037  size += 2; // EBML ID for block and block duration
1038  size += 8; // max size of block duration
1039  size += ebml_num_size(size);
1040  size += 1; // blockgroup EBML ID
1041  return size;
1042 }
1043 
1044 static int ass_get_duration(const uint8_t *p)
1045 {
1046  int sh, sm, ss, sc, eh, em, es, ec;
1047  uint64_t start, end;
1048 
1049  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
1050  &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
1051  return 0;
1052  start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
1053  end = 3600000*eh + 60000*em + 1000*es + 10*ec;
1054  return end - start;
1055 }
1056 
1058 {
1059  MatroskaMuxContext *mkv = s->priv_data;
1060  int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
1061  uint8_t *start, *end, *data = pkt->data;
1062  ebml_master blockgroup;
1063  char buffer[2048];
1064 
1065  while (data_size) {
1066  int duration = ass_get_duration(data);
1067  max_duration = FFMAX(duration, max_duration);
1068  end = memchr(data, '\n', data_size);
1069  size = line_size = end ? end-data+1 : data_size;
1070  size -= end ? (end[-1]=='\r')+1 : 0;
1071  start = data;
1072  for (i=0; i<3; i++, start++)
1073  if (!(start = memchr(start, ',', size-(start-data))))
1074  return max_duration;
1075  size -= start - data;
1076  sscanf(data, "Dialogue: %d,", &layer);
1077  i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
1078  s->streams[pkt->stream_index]->nb_frames, layer);
1079  size = FFMIN(i+size, sizeof(buffer));
1080  memcpy(buffer+i, start, size-i);
1081 
1082  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1083  "pts %" PRId64 ", duration %d\n",
1084  avio_tell(pb), size, pkt->pts, duration);
1087  put_ebml_num(pb, size+4, 0);
1088  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1089  avio_wb16(pb, pkt->pts - mkv->cluster_pts);
1090  avio_w8(pb, 0);
1091  avio_write(pb, buffer, size);
1093  end_ebml_master(pb, blockgroup);
1094 
1095  data += line_size;
1096  data_size -= line_size;
1097  }
1098 
1099  return max_duration;
1100 }
1101 
1102 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1103 {
1104  uint8_t *dst;
1105  int srclen = *size;
1106  int offset = 0;
1107  int ret;
1108 
1109  dst = av_malloc(srclen);
1110  if (!dst)
1111  return AVERROR(ENOMEM);
1112 
1113  while (srclen >= WV_HEADER_SIZE) {
1114  WvHeader header;
1115 
1116  ret = ff_wv_parse_header(&header, src);
1117  if (ret < 0)
1118  goto fail;
1119  src += WV_HEADER_SIZE;
1120  srclen -= WV_HEADER_SIZE;
1121 
1122  if (srclen < header.blocksize) {
1123  ret = AVERROR_INVALIDDATA;
1124  goto fail;
1125  }
1126 
1127  if (header.initial) {
1128  AV_WL32(dst + offset, header.samples);
1129  offset += 4;
1130  }
1131  AV_WL32(dst + offset, header.flags);
1132  AV_WL32(dst + offset + 4, header.crc);
1133  offset += 8;
1134 
1135  if (!(header.initial && header.final)) {
1136  AV_WL32(dst + offset, header.blocksize);
1137  offset += 4;
1138  }
1139 
1140  memcpy(dst + offset, src, header.blocksize);
1141  src += header.blocksize;
1142  srclen -= header.blocksize;
1143  offset += header.blocksize;
1144  }
1145 
1146  *pdst = dst;
1147  *size = offset;
1148 
1149  return 0;
1150 fail:
1151  av_freep(&dst);
1152  return ret;
1153 }
1154 
1156  unsigned int blockid, AVPacket *pkt, int flags)
1157 {
1158  MatroskaMuxContext *mkv = s->priv_data;
1159  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1160  uint8_t *data = NULL;
1161  int offset = 0, size = pkt->size;
1162  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1163 
1164  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1165  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1166  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1167  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1168  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1169  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1170  else if (codec->codec_id == AV_CODEC_ID_HEVC && codec->extradata_size > 6 &&
1171  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1172  /* extradata is Annex B, assume the bitstream is too and convert it */
1173  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
1174  else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {
1175  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1176  if (ret < 0) {
1177  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1178  return;
1179  }
1180  } else
1181  data = pkt->data;
1182 
1183  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1184  /* Matroska specification requires to remove the first QuickTime atom
1185  */
1186  size -= 8;
1187  offset = 8;
1188  }
1189 
1190  put_ebml_id(pb, blockid);
1191  put_ebml_num(pb, size+4, 0);
1192  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1193  avio_wb16(pb, ts - mkv->cluster_pts);
1194  avio_w8(pb, flags);
1195  avio_write(pb, data + offset, size);
1196  if (data != pkt->data)
1197  av_free(data);
1198 }
1199 
1200 static int srt_get_duration(uint8_t **buf)
1201 {
1202  int i, duration = 0;
1203 
1204  for (i=0; i<2 && !duration; i++) {
1205  int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1206  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1207  &s_hour, &s_min, &s_sec, &s_hsec,
1208  &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1209  s_min += 60*s_hour; e_min += 60*e_hour;
1210  s_sec += 60*s_min; e_sec += 60*e_min;
1211  s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1212  duration = e_hsec - s_hsec;
1213  }
1214  *buf += strcspn(*buf, "\n") + 1;
1215  }
1216  return duration;
1217 }
1218 
1220 {
1221  ebml_master blockgroup;
1222  AVPacket pkt2 = *pkt;
1223  int64_t duration = srt_get_duration(&pkt2.data);
1224  pkt2.size -= pkt2.data - pkt->data;
1225 
1226  blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1227  mkv_blockgroup_size(pkt2.size));
1228  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1230  end_ebml_master(pb, blockgroup);
1231 
1232  return duration;
1233 }
1234 
1236 {
1237  MatroskaMuxContext *mkv = s->priv_data;
1238  int bufsize;
1239  uint8_t *dyn_buf;
1240 
1241  if (!mkv->dyn_bc)
1242  return;
1243 
1244  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1245  avio_write(s->pb, dyn_buf, bufsize);
1246  av_free(dyn_buf);
1247  mkv->dyn_bc = NULL;
1248 }
1249 
1251 {
1252  MatroskaMuxContext *mkv = s->priv_data;
1253  AVIOContext *pb = s->pb;
1254  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1255  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1256  int duration = pkt->duration;
1257  int ret;
1258  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1259 
1260  if (ts == AV_NOPTS_VALUE) {
1261  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1262  return AVERROR(EINVAL);
1263  }
1264 
1265  if (!s->pb->seekable) {
1266  if (!mkv->dyn_bc)
1267  avio_open_dyn_buf(&mkv->dyn_bc);
1268  pb = mkv->dyn_bc;
1269  }
1270 
1271  if (!mkv->cluster_pos) {
1272  mkv->cluster_pos = avio_tell(s->pb);
1275  mkv->cluster_pts = FFMAX(0, ts);
1276  }
1277 
1278  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1279  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1280  } else if (codec->codec_id == AV_CODEC_ID_SSA) {
1281  duration = mkv_write_ass_blocks(s, pb, pkt);
1282  } else if (codec->codec_id == AV_CODEC_ID_SRT) {
1283  duration = mkv_write_srt_blocks(s, pb, pkt);
1284  } else {
1286  duration = pkt->convergence_duration;
1287  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1289  end_ebml_master(pb, blockgroup);
1290  }
1291 
1292  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
1293  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
1294  if (ret < 0) return ret;
1295  }
1296 
1297  mkv->duration = FFMAX(mkv->duration, ts + duration);
1298  return 0;
1299 }
1300 
1302 {
1303  MatroskaMuxContext *mkv = s->priv_data;
1304  int codec_type = s->streams[pkt->stream_index]->codec->codec_type;
1305  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1306  int cluster_size;
1307  int64_t cluster_time;
1308  AVIOContext *pb;
1309  int ret;
1310 
1311  if (mkv->tracks[pkt->stream_index].write_dts)
1312  cluster_time = pkt->dts - mkv->cluster_pts;
1313  else
1314  cluster_time = pkt->pts - mkv->cluster_pts;
1315 
1316  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1317  // after 4k and on a keyframe
1318  if (s->pb->seekable) {
1319  pb = s->pb;
1320  cluster_size = avio_tell(pb) - mkv->cluster_pos;
1321  } else {
1322  pb = mkv->dyn_bc;
1323  cluster_size = avio_tell(pb);
1324  }
1325 
1326  if (mkv->cluster_pos &&
1327  (cluster_size > mkv->cluster_size_limit ||
1328  cluster_time > mkv->cluster_time_limit ||
1329  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
1330  cluster_size > 4 * 1024))) {
1331  av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
1332  " bytes, pts %" PRIu64 "dts %" PRIu64 "\n",
1333  avio_tell(pb), pkt->pts, pkt->dts);
1334  end_ebml_master(pb, mkv->cluster);
1335  mkv->cluster_pos = 0;
1336  if (mkv->dyn_bc)
1337  mkv_flush_dynbuf(s);
1338  avio_flush(s->pb);
1339  }
1340 
1341  // check if we have an audio packet cached
1342  if (mkv->cur_audio_pkt.size > 0) {
1343  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1345  if (ret < 0) {
1346  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1347  return ret;
1348  }
1349  }
1350 
1351  // buffer an audio packet to ensure the packet containing the video
1352  // keyframe's timecode is contained in the same cluster for WebM
1353  if (codec_type == AVMEDIA_TYPE_AUDIO) {
1354  mkv->cur_audio_pkt = *pkt;
1355  if (pkt->buf) {
1356  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1357  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1358  } else
1359  ret = av_dup_packet(&mkv->cur_audio_pkt);
1360  } else
1361  ret = mkv_write_packet_internal(s, pkt);
1362  return ret;
1363 }
1364 
1366 {
1367  MatroskaMuxContext *mkv = s->priv_data;
1368  AVIOContext *pb;
1369  if (s->pb->seekable)
1370  pb = s->pb;
1371  else
1372  pb = mkv->dyn_bc;
1373  if (!pkt) {
1374  if (mkv->cluster_pos) {
1375  av_log(s, AV_LOG_DEBUG, "Flushing cluster at offset %" PRIu64
1376  " bytes\n", avio_tell(pb));
1377  end_ebml_master(pb, mkv->cluster);
1378  mkv->cluster_pos = 0;
1379  if (mkv->dyn_bc)
1380  mkv_flush_dynbuf(s);
1381  avio_flush(s->pb);
1382  }
1383  return 0;
1384  }
1385  return mkv_write_packet(s, pkt);
1386 }
1387 
1389 {
1390  MatroskaMuxContext *mkv = s->priv_data;
1391  AVIOContext *pb = s->pb;
1392  int64_t currentpos, cuespos;
1393  int ret;
1394 
1395  // check if we have an audio packet cached
1396  if (mkv->cur_audio_pkt.size > 0) {
1397  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1399  if (ret < 0) {
1400  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1401  return ret;
1402  }
1403  }
1404 
1405  if (mkv->dyn_bc) {
1406  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1407  mkv_flush_dynbuf(s);
1408  } else if (mkv->cluster_pos) {
1409  end_ebml_master(pb, mkv->cluster);
1410  }
1411 
1412  if (mkv->mode != MODE_WEBM) {
1413  ret = mkv_write_chapters(s);
1414  if (ret < 0) return ret;
1415  }
1416 
1417  if (pb->seekable) {
1418  if (mkv->cues->num_entries) {
1419  if (mkv->reserve_cues_space) {
1420  int64_t cues_end;
1421 
1422  currentpos = avio_tell(pb);
1423  avio_seek(pb, mkv->cues_pos, SEEK_SET);
1424 
1425  cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
1426  cues_end = avio_tell(pb);
1427  if (cues_end > cuespos + mkv->reserve_cues_space) {
1428  av_log(s, AV_LOG_ERROR, "Insufficient space reserved for cues: %d "
1429  "(needed: %"PRId64").\n", mkv->reserve_cues_space,
1430  cues_end - cuespos);
1431  return AVERROR(EINVAL);
1432  }
1433 
1434  if (cues_end < cuespos + mkv->reserve_cues_space)
1435  put_ebml_void(pb, mkv->reserve_cues_space - (cues_end - cuespos));
1436 
1437  avio_seek(pb, currentpos, SEEK_SET);
1438  } else {
1439  cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
1440  }
1441 
1443  if (ret < 0) return ret;
1444  }
1445 
1447 
1448  // update the duration
1449  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1450  currentpos = avio_tell(pb);
1451  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1453 
1454  avio_seek(pb, currentpos, SEEK_SET);
1455  }
1456 
1457  end_ebml_master(pb, mkv->segment);
1458  av_free(mkv->tracks);
1459  av_freep(&mkv->cues->entries);
1460  av_freep(&mkv->cues);
1461 
1462  return 0;
1463 }
1464 
1465 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1466 {
1467  int i;
1468  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1469  if (ff_mkv_codec_tags[i].id == codec_id)
1470  return 1;
1471 
1472  if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
1473  enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
1474  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1475  return 1;
1476  }
1477 
1478  return 0;
1479 }
1480 
1481 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1482 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1483 static const AVOption options[] = {
1484  { "reserve_index_space", "Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).", OFFSET(reserve_cues_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
1485  { "cluster_size_limit", "Store at most the provided amount of bytes in a cluster. ", OFFSET(cluster_size_limit), AV_OPT_TYPE_INT , { .i64 = -1 }, -1, INT_MAX, FLAGS },
1486  { "cluster_time_limit", "Store at most the provided number of milliseconds in a cluster.", OFFSET(cluster_time_limit), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
1487  { NULL },
1488 };
1489 
1490 #if CONFIG_MATROSKA_MUXER
1491 static const AVClass matroska_class = {
1492  .class_name = "matroska muxer",
1493  .item_name = av_default_item_name,
1494  .option = options,
1495  .version = LIBAVUTIL_VERSION_INT,
1496 };
1497 
1498 AVOutputFormat ff_matroska_muxer = {
1499  .name = "matroska",
1500  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1501  .mime_type = "video/x-matroska",
1502  .extensions = "mkv",
1503  .priv_data_size = sizeof(MatroskaMuxContext),
1504  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1506  .video_codec = CONFIG_LIBX264_ENCODER ?
1513  .codec_tag = (const AVCodecTag* const []){
1515  },
1516  .subtitle_codec = AV_CODEC_ID_SSA,
1517  .query_codec = mkv_query_codec,
1518  .priv_class = &matroska_class,
1519 };
1520 #endif
1521 
1522 #if CONFIG_WEBM_MUXER
1523 static const AVClass webm_class = {
1524  .class_name = "webm muxer",
1525  .item_name = av_default_item_name,
1526  .option = options,
1527  .version = LIBAVUTIL_VERSION_INT,
1528 };
1529 
1530 AVOutputFormat ff_webm_muxer = {
1531  .name = "webm",
1532  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
1533  .mime_type = "video/webm",
1534  .extensions = "webm",
1535  .priv_data_size = sizeof(MatroskaMuxContext),
1536  .audio_codec = AV_CODEC_ID_VORBIS,
1537  .video_codec = AV_CODEC_ID_VP8,
1543  .priv_class = &webm_class,
1544 };
1545 #endif
1546 
1547 #if CONFIG_MATROSKA_AUDIO_MUXER
1548 static const AVClass mka_class = {
1549  .class_name = "matroska audio muxer",
1550  .item_name = av_default_item_name,
1551  .option = options,
1552  .version = LIBAVUTIL_VERSION_INT,
1553 };
1554 AVOutputFormat ff_matroska_audio_muxer = {
1555  .name = "matroska",
1556  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1557  .mime_type = "audio/x-matroska",
1558  .extensions = "mka",
1559  .priv_data_size = sizeof(MatroskaMuxContext),
1560  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1561  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
1562  .video_codec = AV_CODEC_ID_NONE,
1568  .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
1569  .priv_class = &mka_class,
1570 };
1571 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1053
Definition: lfg.h:25
internal header for HEVC (de)muxer utilities
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:98
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:336
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:142
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:50
uint32_t samples
Definition: wv.h:39
int initial
Definition: wv.h:43
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:95
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:75
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:107
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:960
AVOption.
Definition: opt.h:233
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:70
static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:759
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:140
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:84
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:3208
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:124
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:93
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:856
uint64_t pts
Definition: matroskaenc.c:68
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:747
int num
numerator
Definition: rational.h:44
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
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:187
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
#define AV_RB24
Definition: intreadwrite.h:64
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:918
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
enum AVMediaType codec_type
Definition: rtp.c:36
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:159
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
int64_t cluster_time_limit
Definition: matroskaenc.c:108
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:125
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2064
int64_t segment_offset
Definition: matroskaenc.c:74
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:144
Definition: matroskaenc.c:53
AVDictionary * metadata
Definition: avformat.h:858
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:189
mkv_track * tracks
Definition: matroskaenc.c:99
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:416
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:200
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:170
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:948
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:417
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:194
static int64_t duration
Definition: avplay.c:246
int64_t pos
absolute offset in the file where the master's elements start
Definition: matroskaenc.c:49
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1173
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 int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
Definition: matroskaenc.c:401
#define FLAGS
Definition: matroskaenc.c:1482
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:184
Format I/O context.
Definition: avformat.h:871
char str[32]
Definition: internal.h:41
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:93
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
Public dictionary API.
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:988
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1787
uint8_t
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:197
static int srt_get_duration(uint8_t **buf)
Definition: matroskaenc.c:1200
static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
Definition: matroskaenc.c:789
AVOptions.
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:91
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:67
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:125
uint32_t flags
Definition: wv.h:40
#define AV_RB32
Definition: intreadwrite.h:130
uint64_t segmentpos
Definition: matroskaenc.c:55
int id
unique ID to identify the chapter
Definition: avformat.h:855
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:174
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:199
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1219
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:935
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:122
const char data[16]
Definition: mxf.c:66
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
uint8_t * data
Definition: avcodec.h:973
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:106
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:822
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:471
static EbmlSyntax ebml_header[]
Definition: matroskadec.c:274
enum AVCodecID id
Definition: internal.h:42
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:685
#define MATROSKA_ID_CUES
Definition: matroska.h:58
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
static float t
Definition: output.c:52
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
Definition: wv.h:34
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:995
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:72
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number in EBML variable length format.
Definition: matroskaenc.c:166
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:298
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:60
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:890
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
#define MATROSKA_ID_TRACKUID
Definition: matroska.h:79
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:122
ebml_master segment
Definition: matroskaenc.c:90
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:116
AVPacket cur_audio_pkt
Definition: matroskaenc.c:101
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:819
#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:1064
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1938
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:178
#define EBML_ID_EBMLREADVERSION
Definition: matroska.h:37
#define MAX_CUETRACKPOS_SIZE
per-cuepoint-track - 3 1-byte EBML IDs, 3 1-byte EBML sizes, 2 8-byte uint max
Definition: matroskaenc.c:119
static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1057
#define AVERROR(e)
Definition: error.h:43
unsigned int elementid
Definition: matroskaenc.c:54
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:61
#define MATROSKA_ID_CLUSTER
Definition: matroska.h:62
const char * av_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:736
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
Definition: matroskaenc.c:434
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:186
int64_t convergence_duration
Time difference in AVStream->time_base units from the pts of this packet to the point at which the ou...
Definition: avcodec.h:1021
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:1033
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1142
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:153
int write_dts
Definition: matroskaenc.c:80
int final
Definition: wv.h:43
AVChapter ** chapters
Definition: avformat.h:1054
enum AVCodecID id
Definition: matroska.h:248
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: utils.c:2283
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:713
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:203
enum AVCodecID codec_id
Definition: mov_chan.c:432
uint32_t crc
Definition: wv.h:41
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:323
#define FFMAX(a, b)
Definition: common.h:55
static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
Initialize a mkv_seekhead element to be ready to index level 1 Matroska elements. ...
Definition: matroskaenc.c:278
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
int64_t filepos
Definition: matroskaenc.c:59
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags)
Definition: matroskaenc.c:1155
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:89
#define MATROSKA_ID_TAG
Definition: matroska.h:148
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:923
#define LIBAVFORMAT_IDENT
Definition: version.h:44
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
#define EBML_ID_EBMLVERSION
Definition: matroska.h:36
mkv_cuepoint * entries
Definition: matroskaenc.c:75
#define FFMIN(a, b)
Definition: common.h:57
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:155
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:150
int width
picture width / height.
Definition: avcodec.h:1217
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:204
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:406
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition: hevc.c:1065
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:149
const char * name
Definition: avformat.h:437
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define WV_HEADER_SIZE
Definition: wavpack.c:36
static char buffer[20]
Definition: seek-test.c:31
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:192
AVDictionary * metadata
Definition: avformat.h:749
Opaque data information usually sparse.
Definition: avutil.h:191
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
Definition: matroskaenc.c:381
#define EBML_ID_VOID
Definition: matroska.h:45
#define OFFSET(x)
Definition: matroskaenc.c:1481
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:121
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:95
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:197
Stream structure.
Definition: avformat.h:683
static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
Definition: matroskaenc.c:212
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:857
NULL
Definition: eval.c:55
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:652
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
enum AVMediaType codec_type
Definition: avcodec.h:1062
enum AVCodecID codec_id
Definition: avcodec.h:1065
#define MATROSKA_ID_SEEKID
Definition: matroska.h:166
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
int sample_rate
samples per second
Definition: avcodec.h:1779
AVIOContext * pb
I/O context.
Definition: avformat.h:913
av_default_item_name
Definition: dnxhdenc.c:45
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1054
#define MATROSKA_ID_BLOCK
Definition: matroska.h:177
#define MATROSKA_ID_INFO
Definition: matroska.h:56
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:158
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:152
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1080
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
#define MATROSKA_ID_TRACKS
Definition: matroska.h:57
int extradata_size
Definition: avcodec.h:1163
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:90
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:163
Describe the class of an AVClass context structure.
Definition: log.h:33
#define CONFIG_LIBX264_ENCODER
Definition: config.h:993
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:191
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2330
#define MAX_CUEPOINT_SIZE(num_tracks)
per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max
Definition: matroskaenc.c:122
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:173
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:109
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:555
rational number numerator/denominator
Definition: rational.h:43
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
Definition: matroskaenc.c:185
#define MATROSKA_ID_CUETIME
Definition: matroska.h:139
static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
Write the seek head to the file and free it.
Definition: matroskaenc.c:327
AVIOContext * dyn_bc
Definition: matroskaenc.c:89
AVMediaType
Definition: avutil.h:185
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
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
int64_t duration_offset
Definition: matroskaenc.c:95
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1250
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
#define MATROSKA_ID_TRACKVIDEO
Definition: matroska.h:82
static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
Definition: matroskaenc.c:1102
static int ass_get_duration(const uint8_t *p)
Definition: matroskaenc.c:1044
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:130
int ff_flac_write_header(AVIOContext *pb, AVCodecContext *codec, int last_block)
mkv_cues * cues
Definition: matroskaenc.c:98
void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
Definition: riffenc.c:186
static int put_wv_codecpriv(AVIOContext *pb, AVCodecContext *codec)
Definition: matroskaenc.c:462
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:61
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:195
static const uint16_t scale[4]
#define MATROSKA_ID_FILENAME
Definition: matroska.h:185
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:101
#define MATROSKA_ID_CODECID
Definition: matroska.h:83
int64_t start
Definition: avformat.h:857
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:1388
Main libavformat public API header.
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:143
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:167
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:193
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:204
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
Definition: matroskaenc.c:485
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1365
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:738
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
Definition: riffenc.c:50
static void put_ebml_void(AVIOContext *pb, uint64_t size)
Write a void element of a given size.
Definition: matroskaenc.c:223
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:856
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:736
char * key
Definition: dict.h:75
#define MATROSKA_ID_SEGMENT
Definition: matroska.h:53
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata to retrieve audio configuration.
Definition: mpeg4audio.c:79
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:60
#define EBML_ID_HEADER
Definition: matroska.h:33
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:410
ebml_master cluster
Definition: matroskaenc.c:92
char * value
Definition: dict.h:76
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:136
mkv_seekhead_entry * entries
Definition: matroskaenc.c:63
int channels
number of audio channels
Definition: avcodec.h:1780
#define av_log2
Definition: intmath.h:85
#define MATROSKA_ID_FILEUID
Definition: matroska.h:188
void * priv_data
Format private data.
Definition: avformat.h:899
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:202
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:114
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1235
static const AVOption options[]
Definition: matroskaenc.c:1483
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:196
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:151
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
#define MODE_MATROSKAv2
Definition: matroskaenc.c:83
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:95
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext...
Definition: hevc.c:1081
#define MODE_WEBM
Definition: matroskaenc.c:84
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1301
#define MATROSKA_ID_DURATION
Definition: matroska.h:67
#define MAX_SEEKENTRY_SIZE
2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit offset, 4 bytes for target EBML I...
Definition: matroskaenc.c:115
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:371
int stream_index
Definition: avcodec.h:975
int num_entries
Definition: matroskaenc.c:76
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:249
int64_t segment_offset
Definition: matroskaenc.c:91
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:183
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:241
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:97
This structure stores compressed data.
Definition: avcodec.h:950
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
uint32_t blocksize
Definition: wv.h:35
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
Definition: matroskaenc.c:1465
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:259
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:108
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:81
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:24