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 "avformat.h"
23 #include "internal.h"
24 #include "riff.h"
25 #include "isom.h"
26 #include "matroska.h"
27 #include "avc.h"
28 #include "flacenc.h"
29 #include "avlanguage.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/intfloat.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/random_seed.h"
35 #include "libavutil/lfg.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avstring.h"
38 #include "libavcodec/xiph.h"
39 #include "libavcodec/mpeg4audio.h"
40 
41 typedef struct ebml_master {
42  int64_t pos;
43  int sizebytes;
44 } ebml_master;
45 
46 typedef struct mkv_seekhead_entry {
47  unsigned int elementid;
48  uint64_t segmentpos;
50 
51 typedef struct mkv_seekhead {
52  int64_t filepos;
53  int64_t segment_offset;
58 } mkv_seekhead;
59 
60 typedef struct {
61  uint64_t pts;
62  int tracknum;
63  int64_t cluster_pos;
64 } mkv_cuepoint;
65 
66 typedef struct {
67  int64_t segment_offset;
70 } mkv_cues;
71 
72 typedef struct {
73  int write_dts;
74 } mkv_track;
75 
76 #define MODE_MATROSKAv2 0x01
77 #define MODE_WEBM 0x02
78 
79 typedef struct MatroskaMuxContext {
80  int mode;
83  int64_t segment_offset;
85  int64_t cluster_pos;
86  int64_t cluster_pts;
87  int64_t duration_offset;
88  int64_t duration;
92 
93  unsigned int audio_buffer_size;
95 
98 
99 
102 #define MAX_SEEKENTRY_SIZE 21
103 
106 #define MAX_CUETRACKPOS_SIZE 22
107 
109 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
110 
111 
112 static int ebml_id_size(unsigned int id)
113 {
114  return (av_log2(id+1)-1)/7+1;
115 }
116 
117 static void put_ebml_id(AVIOContext *pb, unsigned int id)
118 {
119  int i = ebml_id_size(id);
120  while (i--)
121  avio_w8(pb, id >> (i*8));
122 }
123 
129 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
130 {
131  assert(bytes <= 8);
132  avio_w8(pb, 0x1ff >> bytes);
133  while (--bytes)
134  avio_w8(pb, 0xff);
135 }
136 
140 static int ebml_num_size(uint64_t num)
141 {
142  int bytes = 1;
143  while ((num+1) >> bytes*7) bytes++;
144  return bytes;
145 }
146 
153 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
154 {
155  int i, needed_bytes = ebml_num_size(num);
156 
157  // sizes larger than this are currently undefined in EBML
158  assert(num < (1ULL<<56)-1);
159 
160  if (bytes == 0)
161  // don't care how many bytes are used, so use the min
162  bytes = needed_bytes;
163  // the bytes needed to write the given size would exceed the bytes
164  // that we need to use, so write unknown size. This shouldn't happen.
165  assert(bytes >= needed_bytes);
166 
167  num |= 1ULL << bytes*7;
168  for (i = bytes - 1; i >= 0; i--)
169  avio_w8(pb, num >> i*8);
170 }
171 
172 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
173 {
174  int i, bytes = 1;
175  uint64_t tmp = val;
176  while (tmp>>=8) bytes++;
177 
178  put_ebml_id(pb, elementid);
179  put_ebml_num(pb, bytes, 0);
180  for (i = bytes - 1; i >= 0; i--)
181  avio_w8(pb, val >> i*8);
182 }
183 
184 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
185 {
186  put_ebml_id(pb, elementid);
187  put_ebml_num(pb, 8, 0);
188  avio_wb64(pb, av_double2int(val));
189 }
190 
191 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
192  const void *buf, int size)
193 {
194  put_ebml_id(pb, elementid);
195  put_ebml_num(pb, size, 0);
196  avio_write(pb, buf, size);
197 }
198 
199 static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
200 {
201  put_ebml_binary(pb, elementid, str, strlen(str));
202 }
203 
210 static void put_ebml_void(AVIOContext *pb, uint64_t size)
211 {
212  int64_t currentpos = avio_tell(pb);
213 
214  assert(size >= 2);
215 
217  // we need to subtract the length needed to store the size from the
218  // size we need to reserve so 2 cases, we use 8 bytes to store the
219  // size if possible, 1 byte otherwise
220  if (size < 10)
221  put_ebml_num(pb, size-1, 0);
222  else
223  put_ebml_num(pb, size-9, 8);
224  while(avio_tell(pb) < currentpos + size)
225  avio_w8(pb, 0);
226 }
227 
228 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
229 {
230  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
231  put_ebml_id(pb, elementid);
232  put_ebml_size_unknown(pb, bytes);
233  return (ebml_master){ avio_tell(pb), bytes };
234 }
235 
236 static void end_ebml_master(AVIOContext *pb, ebml_master master)
237 {
238  int64_t pos = avio_tell(pb);
239 
240  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
241  return;
242  put_ebml_num(pb, pos - master.pos, master.sizebytes);
243  avio_seek(pb, pos, SEEK_SET);
244 }
245 
246 static void put_xiph_size(AVIOContext *pb, int size)
247 {
248  int i;
249  for (i = 0; i < size / 255; i++)
250  avio_w8(pb, 255);
251  avio_w8(pb, size % 255);
252 }
253 
265 static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
266 {
267  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
268  if (new_seekhead == NULL)
269  return NULL;
270 
271  new_seekhead->segment_offset = segment_offset;
272 
273  if (numelements > 0) {
274  new_seekhead->filepos = avio_tell(pb);
275  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
276  // and size, and 3 bytes to guarantee that an EBML void element
277  // will fit afterwards
278  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
279  new_seekhead->max_entries = numelements;
280  put_ebml_void(pb, new_seekhead->reserved_size);
281  }
282  return new_seekhead;
283 }
284 
285 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
286 {
287  mkv_seekhead_entry *entries = seekhead->entries;
288 
289  // don't store more elements than we reserved space for
290  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
291  return -1;
292 
293  entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry));
294  if (entries == NULL)
295  return AVERROR(ENOMEM);
296 
297  entries[seekhead->num_entries ].elementid = elementid;
298  entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
299 
300  seekhead->entries = entries;
301  return 0;
302 }
303 
313 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
314 {
315  ebml_master metaseek, seekentry;
316  int64_t currentpos;
317  int i;
318 
319  currentpos = avio_tell(pb);
320 
321  if (seekhead->reserved_size > 0) {
322  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
323  currentpos = -1;
324  goto fail;
325  }
326  }
327 
328  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
329  for (i = 0; i < seekhead->num_entries; i++) {
330  mkv_seekhead_entry *entry = &seekhead->entries[i];
331 
333 
335  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
336  put_ebml_id(pb, entry->elementid);
337 
339  end_ebml_master(pb, seekentry);
340  }
341  end_ebml_master(pb, metaseek);
342 
343  if (seekhead->reserved_size > 0) {
344  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
345  put_ebml_void(pb, remaining);
346  avio_seek(pb, currentpos, SEEK_SET);
347 
348  currentpos = seekhead->filepos;
349  }
350 fail:
351  av_free(seekhead->entries);
352  av_free(seekhead);
353 
354  return currentpos;
355 }
356 
357 static mkv_cues * mkv_start_cues(int64_t segment_offset)
358 {
359  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
360  if (cues == NULL)
361  return NULL;
362 
363  cues->segment_offset = segment_offset;
364  return cues;
365 }
366 
367 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
368 {
369  mkv_cuepoint *entries = cues->entries;
370 
371  entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
372  if (entries == NULL)
373  return AVERROR(ENOMEM);
374 
375  if (ts < 0)
376  return 0;
377 
378  entries[cues->num_entries ].pts = ts;
379  entries[cues->num_entries ].tracknum = stream + 1;
380  entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
381 
382  cues->entries = entries;
383  return 0;
384 }
385 
386 static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
387 {
388  ebml_master cues_element;
389  int64_t currentpos;
390  int i, j;
391 
392  currentpos = avio_tell(pb);
393  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
394 
395  for (i = 0; i < cues->num_entries; i++) {
396  ebml_master cuepoint, track_positions;
397  mkv_cuepoint *entry = &cues->entries[i];
398  uint64_t pts = entry->pts;
399 
400  cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
402 
403  // put all the entries from different tracks that have the exact same
404  // timestamp into the same CuePoint
405  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
407  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
408  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
409  end_ebml_master(pb, track_positions);
410  }
411  i += j - 1;
412  end_ebml_master(pb, cuepoint);
413  }
414  end_ebml_master(pb, cues_element);
415 
416  return currentpos;
417 }
418 
420 {
421  uint8_t *header_start[3];
422  int header_len[3];
423  int first_header_size;
424  int j;
425 
426  if (codec->codec_id == CODEC_ID_VORBIS)
427  first_header_size = 30;
428  else
429  first_header_size = 42;
430 
432  first_header_size, header_start, header_len) < 0) {
433  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
434  return -1;
435  }
436 
437  avio_w8(pb, 2); // number packets - 1
438  for (j = 0; j < 2; j++) {
439  put_xiph_size(pb, header_len[j]);
440  }
441  for (j = 0; j < 3; j++)
442  avio_write(pb, header_start[j], header_len[j]);
443 
444  return 0;
445 }
446 
447 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
448 {
449  MPEG4AudioConfig mp4ac;
450 
451  if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
452  codec->extradata_size * 8, 1) < 0) {
453  av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
454  return;
455  }
456 
457  *sample_rate = mp4ac.sample_rate;
458  *output_sample_rate = mp4ac.ext_sample_rate;
459 }
460 
461 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
462 {
463  AVIOContext *dyn_cp;
464  uint8_t *codecpriv;
465  int ret, codecpriv_size;
466 
467  ret = avio_open_dyn_buf(&dyn_cp);
468  if(ret < 0)
469  return ret;
470 
471  if (native_id) {
472  if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA)
473  ret = put_xiph_codecpriv(s, dyn_cp, codec);
474  else if (codec->codec_id == CODEC_ID_FLAC)
475  ret = ff_flac_write_header(dyn_cp, codec, 1);
476  else if (codec->codec_id == CODEC_ID_H264)
477  ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
478  else if (codec->extradata_size)
479  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
480  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
481  if (qt_id) {
482  if (!codec->codec_tag)
484  if (codec->extradata_size)
485  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
486  } else {
487  if (!codec->codec_tag)
489  if (!codec->codec_tag) {
490  av_log(s, AV_LOG_ERROR, "No bmp codec ID found.\n");
491  ret = -1;
492  }
493 
494  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
495  }
496 
497  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
498  unsigned int tag;
500  if (!tag) {
501  av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
502  ret = -1;
503  }
504  if (!codec->codec_tag)
505  codec->codec_tag = tag;
506 
507  ff_put_wav_header(dyn_cp, codec);
508  }
509 
510  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
511  if (codecpriv_size)
512  put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
513  av_free(codecpriv);
514  return ret;
515 }
516 
518 {
519  MatroskaMuxContext *mkv = s->priv_data;
520  AVIOContext *pb = s->pb;
521  ebml_master tracks;
522  int i, j, ret;
523 
525  if (ret < 0) return ret;
526 
527  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
528  for (i = 0; i < s->nb_streams; i++) {
529  AVStream *st = s->streams[i];
530  AVCodecContext *codec = st->codec;
531  ebml_master subinfo, track;
532  int native_id = 0;
533  int qt_id = 0;
534  int bit_depth = av_get_bits_per_sample(codec->codec_id);
535  int sample_rate = codec->sample_rate;
536  int output_sample_rate = 0;
538 
539  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
540  mkv->have_attachments = 1;
541  continue;
542  }
543 
544  if (!bit_depth)
545  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
546 
547  if (codec->codec_id == CODEC_ID_AAC)
548  get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
549 
552  put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
553  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
554 
555  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
557  tag = av_dict_get(st->metadata, "language", NULL, 0);
558  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
559 
560  if (st->disposition)
562 
563  // look for a codec ID string specific to mkv to use,
564  // if none are found, use AVI codes
565  for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
566  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
568  native_id = 1;
569  break;
570  }
571  }
572 
573  if (mkv->mode == MODE_WEBM && !(codec->codec_id == CODEC_ID_VP8 ||
574  codec->codec_id == CODEC_ID_VORBIS)) {
575  av_log(s, AV_LOG_ERROR,
576  "Only VP8 video and Vorbis audio are supported for WebM.\n");
577  return AVERROR(EINVAL);
578  }
579 
580  switch (codec->codec_type) {
581  case AVMEDIA_TYPE_VIDEO:
584 
585  if (!native_id &&
588  || codec->codec_id == CODEC_ID_SVQ1
589  || codec->codec_id == CODEC_ID_SVQ3
590  || codec->codec_id == CODEC_ID_CINEPAK))
591  qt_id = 1;
592 
593  if (qt_id)
594  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
595  else if (!native_id) {
596  // if there is no mkv-specific codec ID, use VFW mode
597  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
598  mkv->tracks[i].write_dts = 1;
599  }
600 
601  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
602  // XXX: interlace flag?
605  if ((tag = av_dict_get(s->metadata, "stereo_mode", NULL, 0))) {
606  uint8_t stereo_fmt = atoi(tag->value);
607  int valid_fmt = 0;
608 
609  switch (mkv->mode) {
610  case MODE_WEBM:
613  valid_fmt = 1;
614  break;
615  case MODE_MATROSKAv2:
617  valid_fmt = 1;
618  break;
619  }
620 
621  if (valid_fmt)
622  put_ebml_uint (pb, MATROSKA_ID_VIDEOSTEREOMODE, stereo_fmt);
623  }
624  if (st->sample_aspect_ratio.num) {
625  int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
629  }
630  end_ebml_master(pb, subinfo);
631  break;
632 
633  case AVMEDIA_TYPE_AUDIO:
635 
636  if (!native_id)
637  // no mkv-specific ID, use ACM mode
638  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
639 
640  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
643  if (output_sample_rate)
644  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
645  if (bit_depth)
647  end_ebml_master(pb, subinfo);
648  break;
649 
652  if (!native_id) {
653  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
654  return AVERROR(ENOSYS);
655  }
656  break;
657  default:
658  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
659  break;
660  }
661  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
662  if (ret < 0) return ret;
663 
664  end_ebml_master(pb, track);
665 
666  // ms precision is the de-facto standard timescale for mkv files
667  avpriv_set_pts_info(st, 64, 1, 1000);
668  }
669  end_ebml_master(pb, tracks);
670  return 0;
671 }
672 
674 {
675  MatroskaMuxContext *mkv = s->priv_data;
676  AVIOContext *pb = s->pb;
677  ebml_master chapters, editionentry;
678  AVRational scale = {1, 1E9};
679  int i, ret;
680 
681  if (!s->nb_chapters)
682  return 0;
683 
685  if (ret < 0) return ret;
686 
687  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
688  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
691  for (i = 0; i < s->nb_chapters; i++) {
692  ebml_master chapteratom, chapterdisplay;
693  AVChapter *c = s->chapters[i];
695 
696  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
699  av_rescale_q(c->start, c->time_base, scale));
701  av_rescale_q(c->end, c->time_base, scale));
704  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
705  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
708  end_ebml_master(pb, chapterdisplay);
709  }
710  end_ebml_master(pb, chapteratom);
711  }
712  end_ebml_master(pb, editionentry);
713  end_ebml_master(pb, chapters);
714  return 0;
715 }
716 
718 {
719  uint8_t *key = av_strdup(t->key);
720  uint8_t *p = key;
721  const uint8_t *lang = NULL;
723 
724  if ((p = strrchr(p, '-')) &&
725  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
726  *p = 0;
727 
728  p = key;
729  while (*p) {
730  if (*p == ' ')
731  *p = '_';
732  else if (*p >= 'a' && *p <= 'z')
733  *p -= 'a' - 'A';
734  p++;
735  }
736 
739  if (lang)
742  end_ebml_master(pb, tag);
743 
744  av_freep(&key);
745 }
746 
747 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
748  unsigned int uid, ebml_master *tags)
749 {
750  MatroskaMuxContext *mkv = s->priv_data;
751  ebml_master tag, targets;
753  int ret;
754 
755  if (!tags->pos) {
757  if (ret < 0) return ret;
758 
759  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
760  }
761 
762  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
763  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
764  if (elementid)
765  put_ebml_uint(s->pb, elementid, uid);
766  end_ebml_master(s->pb, targets);
767 
768  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
769  if (av_strcasecmp(t->key, "title") &&
770  av_strcasecmp(t->key, "encoding_tool"))
771  mkv_write_simpletag(s->pb, t);
772 
773  end_ebml_master(s->pb, tag);
774  return 0;
775 }
776 
778 {
779  ebml_master tags = {0};
780  int i, ret;
781 
783 
785  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
786  if (ret < 0) return ret;
787  }
788 
789  for (i = 0; i < s->nb_streams; i++) {
790  AVStream *st = s->streams[i];
791 
792  if (!av_dict_get(st->metadata, "", 0, AV_DICT_IGNORE_SUFFIX))
793  continue;
794 
795  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
796  if (ret < 0) return ret;
797  }
798 
799  for (i = 0; i < s->nb_chapters; i++) {
800  AVChapter *ch = s->chapters[i];
801 
803  continue;
804 
805  ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
806  if (ret < 0) return ret;
807  }
808 
809  if (tags.pos)
810  end_ebml_master(s->pb, tags);
811  return 0;
812 }
813 
815 {
816  MatroskaMuxContext *mkv = s->priv_data;
817  AVIOContext *pb = s->pb;
818  ebml_master attachments;
819  AVLFG c;
820  int i, ret;
821 
822  if (!mkv->have_attachments)
823  return 0;
824 
826 
828  if (ret < 0) return ret;
829 
830  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
831 
832  for (i = 0; i < s->nb_streams; i++) {
833  AVStream *st = s->streams[i];
834  ebml_master attached_file;
836  const char *mimetype = NULL;
837 
839  continue;
840 
841  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
842 
843  if (t = av_dict_get(st->metadata, "title", NULL, 0))
845  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
846  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
847  return AVERROR(EINVAL);
848  }
850  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
851  mimetype = t->value;
852  else if (st->codec->codec_id != CODEC_ID_NONE ) {
853  int i;
854  for (i = 0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++)
855  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
856  mimetype = ff_mkv_mime_tags[i].str;
857  break;
858  }
859  }
860  if (!mimetype) {
861  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
862  "it cannot be deduced from the codec id.\n", i);
863  return AVERROR(EINVAL);
864  }
865 
869  end_ebml_master(pb, attached_file);
870  }
871  end_ebml_master(pb, attachments);
872 
873  return 0;
874 }
875 
877 {
878  MatroskaMuxContext *mkv = s->priv_data;
879  AVIOContext *pb = s->pb;
880  ebml_master ebml_header, segment_info;
882  int ret, i;
883 
884  if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
885  else mkv->mode = MODE_MATROSKAv2;
886 
887  mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
888  if (!mkv->tracks)
889  return AVERROR(ENOMEM);
890 
891  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
899  end_ebml_master(pb, ebml_header);
900 
902  mkv->segment_offset = avio_tell(pb);
903 
904  // we write 2 seek heads - one at the end of the file to point to each
905  // cluster, and one at the beginning to point to all other level one
906  // elements (including the seek head at the end of the file), which
907  // isn't more than 10 elements if we only write one of each other
908  // currently defined level 1 element
909  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
910  if (!mkv->main_seekhead)
911  return AVERROR(ENOMEM);
912 
914  if (ret < 0) return ret;
915 
916  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
918  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
920  if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
921  uint32_t segment_uid[4];
922  AVLFG lfg;
923 
925 
926  for (i = 0; i < 4; i++)
927  segment_uid[i] = av_lfg_get(&lfg);
928 
930  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
932  else
934  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
935  }
936 
937  // reserve space for the duration
938  mkv->duration = 0;
939  mkv->duration_offset = avio_tell(pb);
940  put_ebml_void(pb, 11); // assumes double-precision float to be written
941  end_ebml_master(pb, segment_info);
942 
943  ret = mkv_write_tracks(s);
944  if (ret < 0) return ret;
945 
946  if (mkv->mode != MODE_WEBM) {
947  ret = mkv_write_chapters(s);
948  if (ret < 0) return ret;
949 
950  ret = mkv_write_tags(s);
951  if (ret < 0) return ret;
952 
953  ret = mkv_write_attachments(s);
954  if (ret < 0) return ret;
955  }
956 
957  if (!s->pb->seekable)
959 
960  mkv->cues = mkv_start_cues(mkv->segment_offset);
961  if (mkv->cues == NULL)
962  return AVERROR(ENOMEM);
963 
965  mkv->cur_audio_pkt.size = 0;
966  mkv->audio_buffer_size = 0;
967 
968  avio_flush(pb);
969  return 0;
970 }
971 
972 static int mkv_blockgroup_size(int pkt_size)
973 {
974  int size = pkt_size + 4;
975  size += ebml_num_size(size);
976  size += 2; // EBML ID for block and block duration
977  size += 8; // max size of block duration
978  size += ebml_num_size(size);
979  size += 1; // blockgroup EBML ID
980  return size;
981 }
982 
983 static int ass_get_duration(const uint8_t *p)
984 {
985  int sh, sm, ss, sc, eh, em, es, ec;
986  uint64_t start, end;
987 
988  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
989  &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
990  return 0;
991  start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
992  end = 3600000*eh + 60000*em + 1000*es + 10*ec;
993  return end - start;
994 }
995 
997 {
998  MatroskaMuxContext *mkv = s->priv_data;
999  int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
1000  uint8_t *start, *end, *data = pkt->data;
1001  ebml_master blockgroup;
1002  char buffer[2048];
1003 
1004  while (data_size) {
1005  int duration = ass_get_duration(data);
1006  max_duration = FFMAX(duration, max_duration);
1007  end = memchr(data, '\n', data_size);
1008  size = line_size = end ? end-data+1 : data_size;
1009  size -= end ? (end[-1]=='\r')+1 : 0;
1010  start = data;
1011  for (i=0; i<3; i++, start++)
1012  if (!(start = memchr(start, ',', size-(start-data))))
1013  return max_duration;
1014  size -= start - data;
1015  sscanf(data, "Dialogue: %d,", &layer);
1016  i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
1017  s->streams[pkt->stream_index]->nb_frames, layer);
1018  size = FFMIN(i+size, sizeof(buffer));
1019  memcpy(buffer+i, start, size-i);
1020 
1021  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1022  "pts %" PRId64 ", duration %d\n",
1023  avio_tell(pb), size, pkt->pts, duration);
1026  put_ebml_num(pb, size+4, 0);
1027  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1028  avio_wb16(pb, pkt->pts - mkv->cluster_pts);
1029  avio_w8(pb, 0);
1030  avio_write(pb, buffer, size);
1032  end_ebml_master(pb, blockgroup);
1033 
1034  data += line_size;
1035  data_size -= line_size;
1036  }
1037 
1038  return max_duration;
1039 }
1040 
1042  unsigned int blockid, AVPacket *pkt, int flags)
1043 {
1044  MatroskaMuxContext *mkv = s->priv_data;
1045  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1046  uint8_t *data = NULL;
1047  int size = pkt->size;
1048  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1049 
1050  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1051  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1052  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1053  if (codec->codec_id == CODEC_ID_H264 && codec->extradata_size > 0 &&
1054  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1055  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1056  else
1057  data = pkt->data;
1058  put_ebml_id(pb, blockid);
1059  put_ebml_num(pb, size+4, 0);
1060  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1061  avio_wb16(pb, ts - mkv->cluster_pts);
1062  avio_w8(pb, flags);
1063  avio_write(pb, data, size);
1064  if (data != pkt->data)
1065  av_free(data);
1066 }
1067 
1068 static int srt_get_duration(uint8_t **buf)
1069 {
1070  int i, duration = 0;
1071 
1072  for (i=0; i<2 && !duration; i++) {
1073  int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1074  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1075  &s_hour, &s_min, &s_sec, &s_hsec,
1076  &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1077  s_min += 60*s_hour; e_min += 60*e_hour;
1078  s_sec += 60*s_min; e_sec += 60*e_min;
1079  s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1080  duration = e_hsec - s_hsec;
1081  }
1082  *buf += strcspn(*buf, "\n") + 1;
1083  }
1084  return duration;
1085 }
1086 
1088 {
1089  ebml_master blockgroup;
1090  AVPacket pkt2 = *pkt;
1091  int64_t duration = srt_get_duration(&pkt2.data);
1092  pkt2.size -= pkt2.data - pkt->data;
1093 
1094  blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1095  mkv_blockgroup_size(pkt2.size));
1096  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1098  end_ebml_master(pb, blockgroup);
1099 
1100  return duration;
1101 }
1102 
1104 {
1105  MatroskaMuxContext *mkv = s->priv_data;
1106  int bufsize;
1107  uint8_t *dyn_buf;
1108 
1109  if (!mkv->dyn_bc)
1110  return;
1111 
1112  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1113  avio_write(s->pb, dyn_buf, bufsize);
1114  av_free(dyn_buf);
1115  mkv->dyn_bc = NULL;
1116 }
1117 
1119 {
1120  MatroskaMuxContext *mkv = s->priv_data;
1121  AVIOContext *pb = s->pb;
1122  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1123  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1124  int duration = pkt->duration;
1125  int ret;
1126  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1127 
1128  if (ts == AV_NOPTS_VALUE) {
1129  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1130  return AVERROR(EINVAL);
1131  }
1132 
1133  if (!s->pb->seekable) {
1134  if (!mkv->dyn_bc)
1135  avio_open_dyn_buf(&mkv->dyn_bc);
1136  pb = mkv->dyn_bc;
1137  }
1138 
1139  if (!mkv->cluster_pos) {
1140  mkv->cluster_pos = avio_tell(s->pb);
1143  mkv->cluster_pts = FFMAX(0, ts);
1144  }
1145 
1146  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1147  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1148  } else if (codec->codec_id == CODEC_ID_SSA) {
1149  duration = mkv_write_ass_blocks(s, pb, pkt);
1150  } else if (codec->codec_id == CODEC_ID_SRT) {
1151  duration = mkv_write_srt_blocks(s, pb, pkt);
1152  } else {
1154  duration = pkt->convergence_duration;
1155  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1157  end_ebml_master(pb, blockgroup);
1158  }
1159 
1160  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
1161  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
1162  if (ret < 0) return ret;
1163  }
1164 
1165  mkv->duration = FFMAX(mkv->duration, ts + duration);
1166  return 0;
1167 }
1168 
1169 static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
1170 {
1171  uint8_t *data = mkv->cur_audio_pkt.data;
1172  mkv->cur_audio_pkt = *pkt;
1173  mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
1174  if (!mkv->cur_audio_pkt.data)
1175  return AVERROR(ENOMEM);
1176 
1177  memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
1178  mkv->cur_audio_pkt.size = pkt->size;
1179  return 0;
1180 }
1181 
1183 {
1184  MatroskaMuxContext *mkv = s->priv_data;
1185  AVIOContext *pb = s->pb->seekable ? s->pb : mkv->dyn_bc;
1186  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1187  int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1188  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1189  int cluster_size = avio_tell(pb) - (s->pb->seekable ? mkv->cluster_pos : 0);
1190 
1191  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1192  // after 4k and on a keyframe
1193  if (mkv->cluster_pos &&
1194  ((!s->pb->seekable && (cluster_size > 32*1024 || ts > mkv->cluster_pts + 1000))
1195  || cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
1196  || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
1197  av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
1198  " bytes, pts %" PRIu64 "\n", avio_tell(pb), ts);
1199  end_ebml_master(pb, mkv->cluster);
1200  mkv->cluster_pos = 0;
1201  if (mkv->dyn_bc)
1202  mkv_flush_dynbuf(s);
1203  }
1204 
1205  // check if we have an audio packet cached
1206  if (mkv->cur_audio_pkt.size > 0) {
1207  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1208  mkv->cur_audio_pkt.size = 0;
1209  if (ret < 0) {
1210  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1211  return ret;
1212  }
1213  }
1214 
1215  // buffer an audio packet to ensure the packet containing the video
1216  // keyframe's timecode is contained in the same cluster for WebM
1217  if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
1218  ret = mkv_copy_packet(mkv, pkt);
1219  else
1220  ret = mkv_write_packet_internal(s, pkt);
1221  return ret;
1222 }
1223 
1225 {
1226  MatroskaMuxContext *mkv = s->priv_data;
1227  AVIOContext *pb = s->pb;
1228  int64_t currentpos, cuespos;
1229  int ret;
1230 
1231  // check if we have an audio packet cached
1232  if (mkv->cur_audio_pkt.size > 0) {
1233  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1234  mkv->cur_audio_pkt.size = 0;
1235  if (ret < 0) {
1236  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1237  return ret;
1238  }
1239  }
1240 
1241  if (mkv->dyn_bc) {
1242  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1243  mkv_flush_dynbuf(s);
1244  } else if (mkv->cluster_pos) {
1245  end_ebml_master(pb, mkv->cluster);
1246  }
1247 
1248  if (pb->seekable) {
1249  if (mkv->cues->num_entries) {
1250  cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
1251 
1253  if (ret < 0) return ret;
1254  }
1255 
1257 
1258  // update the duration
1259  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1260  currentpos = avio_tell(pb);
1261  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1263 
1264  avio_seek(pb, currentpos, SEEK_SET);
1265  }
1266 
1267  end_ebml_master(pb, mkv->segment);
1268  av_free(mkv->tracks);
1269  av_freep(&mkv->cues->entries);
1270  av_freep(&mkv->cues);
1272  avio_flush(pb);
1273  return 0;
1274 }
1275 
1276 static int mkv_query_codec(enum CodecID codec_id, int std_compliance)
1277 {
1278  int i;
1279  for (i = 0; ff_mkv_codec_tags[i].id != CODEC_ID_NONE; i++)
1280  if (ff_mkv_codec_tags[i].id == codec_id)
1281  return 1;
1282 
1283  if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
1284  enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
1285  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1286  return 1;
1287  }
1288 
1289  return 0;
1290 }
1291 
1292 #if CONFIG_MATROSKA_MUXER
1293 AVOutputFormat ff_matroska_muxer = {
1294  .name = "matroska",
1295  .long_name = NULL_IF_CONFIG_SMALL("Matroska file format"),
1296  .mime_type = "video/x-matroska",
1297  .extensions = "mkv",
1298  .priv_data_size = sizeof(MatroskaMuxContext),
1300  .audio_codec = CODEC_ID_VORBIS,
1301 #else
1302  .audio_codec = CODEC_ID_AC3,
1303 #endif
1305  .video_codec = CODEC_ID_H264,
1306 #else
1307  .video_codec = CODEC_ID_MPEG4,
1308 #endif
1313  .codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
1314  .subtitle_codec = CODEC_ID_SSA,
1315  .query_codec = mkv_query_codec,
1316 };
1317 #endif
1318 
1319 #if CONFIG_WEBM_MUXER
1320 AVOutputFormat ff_webm_muxer = {
1321  .name = "webm",
1322  .long_name = NULL_IF_CONFIG_SMALL("WebM file format"),
1323  .mime_type = "video/webm",
1324  .extensions = "webm",
1325  .priv_data_size = sizeof(MatroskaMuxContext),
1326  .audio_codec = CODEC_ID_VORBIS,
1327  .video_codec = CODEC_ID_VP8,
1332 };
1333 #endif
1334 
1335 #if CONFIG_MATROSKA_AUDIO_MUXER
1336 AVOutputFormat ff_matroska_audio_muxer = {
1337  .name = "matroska",
1338  .long_name = NULL_IF_CONFIG_SMALL("Matroska file format"),
1339  .mime_type = "audio/x-matroska",
1340  .extensions = "mka",
1341  .priv_data_size = sizeof(MatroskaMuxContext),
1343  .audio_codec = CODEC_ID_VORBIS,
1344 #else
1345  .audio_codec = CODEC_ID_AC3,
1346 #endif
1347  .video_codec = CODEC_ID_NONE,
1352  .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
1353 };
1354 #endif
unsigned int nb_chapters
Definition: avformat.h:1063
Definition: lfg.h:25
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:521
static int write_header(AVFormatContext *s)
Definition: assenc.c:28
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:154
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:96
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:129
Bytestream IO Context.
Definition: avio.h:68
int size
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:43
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:93
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:74
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:105
AV_WL32 AV_WL24 AV_WL16 AV_RB32
Definition: bytestream.h:89
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1196
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:63
static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:717
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:138
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:83
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:122
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:91
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:814
uint64_t pts
Definition: matroskaenc.c:61
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:716
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:184
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:41
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:876
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:79
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:157
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:67
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:69
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:123
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
int64_t segment_offset
Definition: matroskaenc.c:67
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:142
Definition: matroskaenc.c:46
AVDictionary * metadata
Definition: avformat.h:852
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:27
mkv_track * tracks
Definition: matroskaenc.c:91
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:197
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:168
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1184
#define EBML_ID_DOCTYPE
Definition: matroska.h:39
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:191
static int64_t duration
Definition: avplay.c:241
int64_t pos
absolute offset in the file where the master's elements start
Definition: matroskaenc.c:42
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
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:147
static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
Definition: matroskaenc.c:386
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:181
Format I/O context.
Definition: avformat.h:863
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
char str[32]
Definition: matroska.h:249
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:85
Public dictionary API.
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1464
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:194
static int srt_get_duration(uint8_t **buf)
Definition: matroskaenc.c:1068
static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
Definition: matroskaenc.c:747
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:89
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:112
uint64_t segmentpos
Definition: matroskaenc.c:48
int id
unique ID to identify the chapter
Definition: avformat.h:849
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:65
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:172
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:196
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1087
AVStream ** streams
Definition: avformat.h:908
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:120
const char data[16]
Definition: mxf.c:60
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
uint8_t * data
Definition: avcodec.h:908
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:104
static int flags
Definition: log.c:34
uint32_t tag
Definition: movenc.c:670
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:447
static EbmlSyntax ebml_header[]
Definition: matroskadec.c:264
struct MatroskaMuxContext MatroskaMuxContext
unsigned int audio_buffer_size
Definition: matroskaenc.c:93
#define MATROSKA_ID_CUES
Definition: matroska.h:57
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:33
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:77
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
int av_get_bits_per_sample(enum CodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1634
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:930
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:71
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number in EBML variable length format.
Definition: matroskaenc.c:153
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:67
static float t
static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
Definition: matroskaenc.c:1169
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:285
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:53
struct AVOutputFormat * oformat
Definition: avformat.h:877
#define MATROSKA_ID_TRACKUID
Definition: matroska.h:78
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:132
ebml_master segment
Definition: matroskaenc.c:82
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:114
enum CodecID codec_id
Definition: mov_chan.c:417
AVPacket cur_audio_pkt
Definition: matroskaenc.c:94
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:777
AVDictionary * metadata
Definition: avformat.h:1085
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
struct mkv_seekhead mkv_seekhead
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:176
#define EBML_ID_EBMLREADVERSION
Definition: matroska.h:36
#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:106
static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:996
#define AVERROR(e)
Definition: error.h:43
unsigned int elementid
Definition: matroskaenc.c:47
#define CONFIG_LIBX264_ENCODER
Definition: config.h:824
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:54
#define MATROSKA_ID_CLUSTER
Definition: matroska.h:61
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:419
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:183
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:952
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:68
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:972
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:140
int write_dts
Definition: matroskaenc.c:73
AVChapter ** chapters
Definition: avformat.h:1064
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:673
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:37
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:200
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:289
#define FFMAX(a, b)
Definition: common.h:53
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:265
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:52
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags)
Definition: matroskaenc.c:1041
AVCodecContext * codec
codec context
Definition: avformat.h:623
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:85
#define MATROSKA_ID_TAG
Definition: matroska.h:146
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:907
#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:122
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:205
#define EBML_ID_EBMLVERSION
Definition: matroska.h:35
mkv_cuepoint * entries
Definition: matroskaenc.c:68
#define FFMIN(a, b)
Definition: common.h:55
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:153
enum CodecID id
Definition: matroska.h:245
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:31
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:137
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:148
int width
picture width / height.
Definition: avcodec.h:1408
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:201
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:374
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:147
const char * name
Definition: avformat.h:390
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static char buffer[20]
Definition: seek-test.c:34
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:189
AVDictionary * metadata
Definition: avformat.h:718
Opaque data information usually sparse.
Definition: avutil.h:234
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:62
static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
Definition: matroskaenc.c:367
#define EBML_ID_VOID
Definition: matroska.h:44
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:119
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:75
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:184
Stream structure.
Definition: avformat.h:620
static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
Definition: matroskaenc.c:199
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:851
NULL
Definition: eval.c:50
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:596
static int mkv_query_codec(enum CodecID codec_id, int std_compliance)
Definition: matroskaenc.c:1276
#define MATROSKA_ID_TAGS
Definition: matroska.h:58
enum AVMediaType codec_type
Definition: avcodec.h:1574
#define MATROSKA_ID_SEEKID
Definition: matroska.h:164
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:162
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:169
struct mkv_seekhead_entry mkv_seekhead_entry
main external API structure.
Definition: avcodec.h:1329
#define MATROSKA_ID_BLOCK
Definition: matroska.h:175
#define MATROSKA_ID_INFO
Definition: matroska.h:55
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:156
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:150
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
struct ebml_master ebml_master
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:56
int extradata_size
Definition: avcodec.h:1388
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:88
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:161
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:188
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1645
#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:109
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:171
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:107
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:517
rational number numerator/denominator
Definition: rational.h:43
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
Definition: matroskaenc.c:172
#define MATROSKA_ID_CUETIME
Definition: matroska.h:137
static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
Write the seek head to the file and free it.
Definition: matroskaenc.c:313
AVIOContext * dyn_bc
Definition: matroskaenc.c:81
AVMediaType
Definition: avutil.h:228
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
int64_t duration_offset
Definition: matroskaenc.c:87
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1118
#define MATROSKA_ID_TITLE
Definition: matroska.h:67
#define MATROSKA_ID_TRACKVIDEO
Definition: matroska.h:81
static int ass_get_duration(const uint8_t *p)
Definition: matroskaenc.c:983
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:117
int ff_flac_write_header(AVIOContext *pb, AVCodecContext *codec, int last_block)
mkv_cues * cues
Definition: matroskaenc.c:90
void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:60
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:533
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:192
static const uint16_t scale[4]
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
Definition: utils.c:2196
#define MATROSKA_ID_FILENAME
Definition: matroska.h:182
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:97
#define MATROSKA_ID_CODECID
Definition: matroska.h:82
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_RB24
Definition: bytestream.h:89
int64_t start
Definition: avformat.h:851
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:88
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:1224
Main libavformat public API header.
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:141
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:165
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:190
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:125
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:191
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:822
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
Definition: matroskaenc.c:461
enum AVMediaType avcodec_get_type(enum CodecID codec_id)
Get the type of the given codec.
Definition: utils.c:1848
void av_cold av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:703
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
static void put_ebml_void(AVIOContext *pb, uint64_t size)
Write a void element of a given size.
Definition: matroskaenc.c:210
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:850
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:701
char * key
Definition: dict.h:75
#define MATROSKA_ID_SEGMENT
Definition: matroska.h:52
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
CodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:83
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:59
#define EBML_ID_HEADER
Definition: matroska.h:32
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:378
ebml_master cluster
Definition: matroskaenc.c:84
char * value
Definition: dict.h:76
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:134
mkv_seekhead_entry * entries
Definition: matroskaenc.c:56
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: utils.c:55
int channels
number of audio channels
Definition: avcodec.h:1457
#define MATROSKA_ID_FILEUID
Definition: matroska.h:185
void * priv_data
Format private data.
Definition: avformat.h:883
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:199
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:112
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:907
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1103
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:38
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:193
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:149
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
#define MODE_MATROSKAv2
Definition: matroskaenc.c:76
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:73
#define MODE_WEBM
Definition: matroskaenc.c:77
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1182
#define MATROSKA_ID_DURATION
Definition: matroska.h:66
#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:102
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:357
int stream_index
Definition: avcodec.h:910
int num_entries
Definition: matroskaenc.c:69
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:40
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:649
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:236
int64_t segment_offset
Definition: matroskaenc.c:83
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:180
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:228
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:89
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:246
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:106
const AVCodecTag codec_movvideo_tags[]
Definition: isom.c:69
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:80
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:271
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:24
enum CodecID codec_id
Definition: avcodec.h:1575
enum CodecID id
Definition: matroska.h:250