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  if (ts < 0)
372  return 0;
373 
374  entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
375  if (entries == NULL)
376  return AVERROR(ENOMEM);
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 == AV_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 == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA)
473  ret = put_xiph_codecpriv(s, dyn_cp, codec);
474  else if (codec->codec_id == AV_CODEC_ID_FLAC)
475  ret = ff_flac_write_header(dyn_cp, codec, 1);
476  else if (codec->codec_id == AV_CODEC_ID_H264)
477  ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
478  else if (codec->codec_id == AV_CODEC_ID_ALAC) {
479  if (codec->extradata_size < 36) {
480  av_log(s, AV_LOG_ERROR,
481  "Invalid extradata found, ALAC expects a 36-byte "
482  "QuickTime atom.");
483  ret = AVERROR_INVALIDDATA;
484  } else
485  avio_write(dyn_cp, codec->extradata + 12,
486  codec->extradata_size - 12);
487  }
488  else if (codec->extradata_size)
489  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
490  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
491  if (qt_id) {
492  if (!codec->codec_tag)
494  if (codec->extradata_size)
495  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
496  } else {
497  if (!codec->codec_tag)
499  if (!codec->codec_tag) {
500  av_log(s, AV_LOG_ERROR, "No bmp codec ID found.\n");
501  ret = -1;
502  }
503 
504  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
505  }
506 
507  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
508  unsigned int tag;
510  if (!tag) {
511  av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
512  ret = -1;
513  }
514  if (!codec->codec_tag)
515  codec->codec_tag = tag;
516 
517  ff_put_wav_header(dyn_cp, codec);
518  }
519 
520  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
521  if (codecpriv_size)
522  put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
523  av_free(codecpriv);
524  return ret;
525 }
526 
528 {
529  MatroskaMuxContext *mkv = s->priv_data;
530  AVIOContext *pb = s->pb;
531  ebml_master tracks;
532  int i, j, ret;
533 
535  if (ret < 0) return ret;
536 
537  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
538  for (i = 0; i < s->nb_streams; i++) {
539  AVStream *st = s->streams[i];
540  AVCodecContext *codec = st->codec;
541  ebml_master subinfo, track;
542  int native_id = 0;
543  int qt_id = 0;
544  int bit_depth = av_get_bits_per_sample(codec->codec_id);
545  int sample_rate = codec->sample_rate;
546  int output_sample_rate = 0;
548 
549  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
550  mkv->have_attachments = 1;
551  continue;
552  }
553 
554  if (!bit_depth)
555  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
556 
557  if (codec->codec_id == AV_CODEC_ID_AAC)
558  get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
559 
562  put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
563  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
564 
565  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
567  tag = av_dict_get(st->metadata, "language", NULL, 0);
568  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
569 
570  if (st->disposition)
572 
573  // look for a codec ID string specific to mkv to use,
574  // if none are found, use AVI codes
575  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
576  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
578  native_id = 1;
579  break;
580  }
581  }
582 
583  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
584  codec->codec_id == AV_CODEC_ID_VORBIS)) {
585  av_log(s, AV_LOG_ERROR,
586  "Only VP8 video and Vorbis audio are supported for WebM.\n");
587  return AVERROR(EINVAL);
588  }
589 
590  switch (codec->codec_type) {
591  case AVMEDIA_TYPE_VIDEO:
594 
595  if (!native_id &&
598  || codec->codec_id == AV_CODEC_ID_SVQ1
599  || codec->codec_id == AV_CODEC_ID_SVQ3
600  || codec->codec_id == AV_CODEC_ID_CINEPAK))
601  qt_id = 1;
602 
603  if (qt_id)
604  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
605  else if (!native_id) {
606  // if there is no mkv-specific codec ID, use VFW mode
607  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
608  mkv->tracks[i].write_dts = 1;
609  }
610 
611  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
612  // XXX: interlace flag?
615  if ((tag = av_dict_get(s->metadata, "stereo_mode", NULL, 0))) {
616  uint8_t stereo_fmt = atoi(tag->value);
617  int valid_fmt = 0;
618 
619  switch (mkv->mode) {
620  case MODE_WEBM:
623  valid_fmt = 1;
624  break;
625  case MODE_MATROSKAv2:
627  valid_fmt = 1;
628  break;
629  }
630 
631  if (valid_fmt)
632  put_ebml_uint (pb, MATROSKA_ID_VIDEOSTEREOMODE, stereo_fmt);
633  }
634  if (st->sample_aspect_ratio.num) {
635  int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
639  }
640  end_ebml_master(pb, subinfo);
641  break;
642 
643  case AVMEDIA_TYPE_AUDIO:
645 
646  if (!native_id)
647  // no mkv-specific ID, use ACM mode
648  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
649 
650  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
653  if (output_sample_rate)
654  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
655  if (bit_depth)
657  end_ebml_master(pb, subinfo);
658  break;
659 
662  if (!native_id) {
663  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
664  return AVERROR(ENOSYS);
665  }
666  break;
667  default:
668  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
669  break;
670  }
671  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
672  if (ret < 0) return ret;
673 
674  end_ebml_master(pb, track);
675 
676  // ms precision is the de-facto standard timescale for mkv files
677  avpriv_set_pts_info(st, 64, 1, 1000);
678  }
679  end_ebml_master(pb, tracks);
680  return 0;
681 }
682 
684 {
685  MatroskaMuxContext *mkv = s->priv_data;
686  AVIOContext *pb = s->pb;
687  ebml_master chapters, editionentry;
688  AVRational scale = {1, 1E9};
689  int i, ret;
690 
691  if (!s->nb_chapters)
692  return 0;
693 
695  if (ret < 0) return ret;
696 
697  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
698  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
701  for (i = 0; i < s->nb_chapters; i++) {
702  ebml_master chapteratom, chapterdisplay;
703  AVChapter *c = s->chapters[i];
705 
706  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
709  av_rescale_q(c->start, c->time_base, scale));
711  av_rescale_q(c->end, c->time_base, scale));
714  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
715  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
718  end_ebml_master(pb, chapterdisplay);
719  }
720  end_ebml_master(pb, chapteratom);
721  }
722  end_ebml_master(pb, editionentry);
723  end_ebml_master(pb, chapters);
724  return 0;
725 }
726 
728 {
729  uint8_t *key = av_strdup(t->key);
730  uint8_t *p = key;
731  const uint8_t *lang = NULL;
733 
734  if ((p = strrchr(p, '-')) &&
735  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
736  *p = 0;
737 
738  p = key;
739  while (*p) {
740  if (*p == ' ')
741  *p = '_';
742  else if (*p >= 'a' && *p <= 'z')
743  *p -= 'a' - 'A';
744  p++;
745  }
746 
749  if (lang)
752  end_ebml_master(pb, tag);
753 
754  av_freep(&key);
755 }
756 
757 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
758  unsigned int uid, ebml_master *tags)
759 {
760  MatroskaMuxContext *mkv = s->priv_data;
761  ebml_master tag, targets;
763  int ret;
764 
765  if (!tags->pos) {
767  if (ret < 0) return ret;
768 
769  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
770  }
771 
772  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
773  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
774  if (elementid)
775  put_ebml_uint(s->pb, elementid, uid);
776  end_ebml_master(s->pb, targets);
777 
778  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
779  if (av_strcasecmp(t->key, "title"))
780  mkv_write_simpletag(s->pb, t);
781 
782  end_ebml_master(s->pb, tag);
783  return 0;
784 }
785 
787 {
788  ebml_master tags = {0};
789  int i, ret;
790 
792 
794  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
795  if (ret < 0) return ret;
796  }
797 
798  for (i = 0; i < s->nb_streams; i++) {
799  AVStream *st = s->streams[i];
800 
801  if (!av_dict_get(st->metadata, "", 0, AV_DICT_IGNORE_SUFFIX))
802  continue;
803 
804  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
805  if (ret < 0) return ret;
806  }
807 
808  for (i = 0; i < s->nb_chapters; i++) {
809  AVChapter *ch = s->chapters[i];
810 
812  continue;
813 
814  ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
815  if (ret < 0) return ret;
816  }
817 
818  if (tags.pos)
819  end_ebml_master(s->pb, tags);
820  return 0;
821 }
822 
824 {
825  MatroskaMuxContext *mkv = s->priv_data;
826  AVIOContext *pb = s->pb;
827  ebml_master attachments;
828  AVLFG c;
829  int i, ret;
830 
831  if (!mkv->have_attachments)
832  return 0;
833 
835 
837  if (ret < 0) return ret;
838 
839  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
840 
841  for (i = 0; i < s->nb_streams; i++) {
842  AVStream *st = s->streams[i];
843  ebml_master attached_file;
845  const char *mimetype = NULL;
846 
848  continue;
849 
850  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
851 
852  if (t = av_dict_get(st->metadata, "title", NULL, 0))
854  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
855  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
856  return AVERROR(EINVAL);
857  }
859  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
860  mimetype = t->value;
861  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
862  int i;
863  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
864  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
865  mimetype = ff_mkv_mime_tags[i].str;
866  break;
867  }
868  }
869  if (!mimetype) {
870  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
871  "it cannot be deduced from the codec id.\n", i);
872  return AVERROR(EINVAL);
873  }
874 
878  end_ebml_master(pb, attached_file);
879  }
880  end_ebml_master(pb, attachments);
881 
882  return 0;
883 }
884 
886 {
887  MatroskaMuxContext *mkv = s->priv_data;
888  AVIOContext *pb = s->pb;
889  ebml_master ebml_header, segment_info;
891  int ret, i;
892 
893  if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
894  else mkv->mode = MODE_MATROSKAv2;
895 
896  mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
897  if (!mkv->tracks)
898  return AVERROR(ENOMEM);
899 
900  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
908  end_ebml_master(pb, ebml_header);
909 
911  mkv->segment_offset = avio_tell(pb);
912 
913  // we write 2 seek heads - one at the end of the file to point to each
914  // cluster, and one at the beginning to point to all other level one
915  // elements (including the seek head at the end of the file), which
916  // isn't more than 10 elements if we only write one of each other
917  // currently defined level 1 element
918  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
919  if (!mkv->main_seekhead)
920  return AVERROR(ENOMEM);
921 
923  if (ret < 0) return ret;
924 
925  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
927  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
929  if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
930  uint32_t segment_uid[4];
931  AVLFG lfg;
932 
934 
935  for (i = 0; i < 4; i++)
936  segment_uid[i] = av_lfg_get(&lfg);
937 
940  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
941  }
942 
943  // reserve space for the duration
944  mkv->duration = 0;
945  mkv->duration_offset = avio_tell(pb);
946  put_ebml_void(pb, 11); // assumes double-precision float to be written
947  end_ebml_master(pb, segment_info);
948 
949  ret = mkv_write_tracks(s);
950  if (ret < 0) return ret;
951 
952  if (mkv->mode != MODE_WEBM) {
953  ret = mkv_write_chapters(s);
954  if (ret < 0) return ret;
955 
956  ret = mkv_write_tags(s);
957  if (ret < 0) return ret;
958 
959  ret = mkv_write_attachments(s);
960  if (ret < 0) return ret;
961  }
962 
963  if (!s->pb->seekable)
965 
966  mkv->cues = mkv_start_cues(mkv->segment_offset);
967  if (mkv->cues == NULL)
968  return AVERROR(ENOMEM);
969 
971  mkv->cur_audio_pkt.size = 0;
972  mkv->audio_buffer_size = 0;
973 
974  avio_flush(pb);
975  return 0;
976 }
977 
978 static int mkv_blockgroup_size(int pkt_size)
979 {
980  int size = pkt_size + 4;
981  size += ebml_num_size(size);
982  size += 2; // EBML ID for block and block duration
983  size += 8; // max size of block duration
984  size += ebml_num_size(size);
985  size += 1; // blockgroup EBML ID
986  return size;
987 }
988 
989 static int ass_get_duration(const uint8_t *p)
990 {
991  int sh, sm, ss, sc, eh, em, es, ec;
992  uint64_t start, end;
993 
994  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
995  &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
996  return 0;
997  start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
998  end = 3600000*eh + 60000*em + 1000*es + 10*ec;
999  return end - start;
1000 }
1001 
1003 {
1004  MatroskaMuxContext *mkv = s->priv_data;
1005  int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
1006  uint8_t *start, *end, *data = pkt->data;
1007  ebml_master blockgroup;
1008  char buffer[2048];
1009 
1010  while (data_size) {
1011  int duration = ass_get_duration(data);
1012  max_duration = FFMAX(duration, max_duration);
1013  end = memchr(data, '\n', data_size);
1014  size = line_size = end ? end-data+1 : data_size;
1015  size -= end ? (end[-1]=='\r')+1 : 0;
1016  start = data;
1017  for (i=0; i<3; i++, start++)
1018  if (!(start = memchr(start, ',', size-(start-data))))
1019  return max_duration;
1020  size -= start - data;
1021  sscanf(data, "Dialogue: %d,", &layer);
1022  i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
1023  s->streams[pkt->stream_index]->nb_frames, layer);
1024  size = FFMIN(i+size, sizeof(buffer));
1025  memcpy(buffer+i, start, size-i);
1026 
1027  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1028  "pts %" PRId64 ", duration %d\n",
1029  avio_tell(pb), size, pkt->pts, duration);
1032  put_ebml_num(pb, size+4, 0);
1033  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1034  avio_wb16(pb, pkt->pts - mkv->cluster_pts);
1035  avio_w8(pb, 0);
1036  avio_write(pb, buffer, size);
1038  end_ebml_master(pb, blockgroup);
1039 
1040  data += line_size;
1041  data_size -= line_size;
1042  }
1043 
1044  return max_duration;
1045 }
1046 
1048  unsigned int blockid, AVPacket *pkt, int flags)
1049 {
1050  MatroskaMuxContext *mkv = s->priv_data;
1051  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1052  uint8_t *data = NULL;
1053  int offset = 0, size = pkt->size;
1054  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1055 
1056  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1057  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1058  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1059  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1060  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1061  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1062  else
1063  data = pkt->data;
1064 
1065  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1066  /* Matroska specification requires to remove the first QuickTime atom
1067  */
1068  size -= 8;
1069  offset = 8;
1070  }
1071 
1072  put_ebml_id(pb, blockid);
1073  put_ebml_num(pb, size+4, 0);
1074  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1075  avio_wb16(pb, ts - mkv->cluster_pts);
1076  avio_w8(pb, flags);
1077  avio_write(pb, data + offset, size);
1078  if (data != pkt->data)
1079  av_free(data);
1080 }
1081 
1082 static int srt_get_duration(uint8_t **buf)
1083 {
1084  int i, duration = 0;
1085 
1086  for (i=0; i<2 && !duration; i++) {
1087  int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1088  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1089  &s_hour, &s_min, &s_sec, &s_hsec,
1090  &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1091  s_min += 60*s_hour; e_min += 60*e_hour;
1092  s_sec += 60*s_min; e_sec += 60*e_min;
1093  s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1094  duration = e_hsec - s_hsec;
1095  }
1096  *buf += strcspn(*buf, "\n") + 1;
1097  }
1098  return duration;
1099 }
1100 
1102 {
1103  ebml_master blockgroup;
1104  AVPacket pkt2 = *pkt;
1105  int64_t duration = srt_get_duration(&pkt2.data);
1106  pkt2.size -= pkt2.data - pkt->data;
1107 
1108  blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1109  mkv_blockgroup_size(pkt2.size));
1110  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1112  end_ebml_master(pb, blockgroup);
1113 
1114  return duration;
1115 }
1116 
1118 {
1119  MatroskaMuxContext *mkv = s->priv_data;
1120  int bufsize;
1121  uint8_t *dyn_buf;
1122 
1123  if (!mkv->dyn_bc)
1124  return;
1125 
1126  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1127  avio_write(s->pb, dyn_buf, bufsize);
1128  av_free(dyn_buf);
1129  mkv->dyn_bc = NULL;
1130 }
1131 
1133 {
1134  MatroskaMuxContext *mkv = s->priv_data;
1135  AVIOContext *pb = s->pb;
1136  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1137  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1138  int duration = pkt->duration;
1139  int ret;
1140  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1141 
1142  if (ts == AV_NOPTS_VALUE) {
1143  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1144  return AVERROR(EINVAL);
1145  }
1146 
1147  if (!s->pb->seekable) {
1148  if (!mkv->dyn_bc)
1149  avio_open_dyn_buf(&mkv->dyn_bc);
1150  pb = mkv->dyn_bc;
1151  }
1152 
1153  if (!mkv->cluster_pos) {
1154  mkv->cluster_pos = avio_tell(s->pb);
1157  mkv->cluster_pts = FFMAX(0, ts);
1158  }
1159 
1160  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1161  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1162  } else if (codec->codec_id == AV_CODEC_ID_SSA) {
1163  duration = mkv_write_ass_blocks(s, pb, pkt);
1164  } else if (codec->codec_id == AV_CODEC_ID_SRT) {
1165  duration = mkv_write_srt_blocks(s, pb, pkt);
1166  } else {
1168  duration = pkt->convergence_duration;
1169  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1171  end_ebml_master(pb, blockgroup);
1172  }
1173 
1174  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
1175  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
1176  if (ret < 0) return ret;
1177  }
1178 
1179  mkv->duration = FFMAX(mkv->duration, ts + duration);
1180  return 0;
1181 }
1182 
1183 static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
1184 {
1185  uint8_t *data = mkv->cur_audio_pkt.data;
1186  mkv->cur_audio_pkt = *pkt;
1187  mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
1188  if (!mkv->cur_audio_pkt.data)
1189  return AVERROR(ENOMEM);
1190 
1191  memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
1192  mkv->cur_audio_pkt.size = pkt->size;
1193  return 0;
1194 }
1195 
1197 {
1198  MatroskaMuxContext *mkv = s->priv_data;
1199  AVIOContext *pb = s->pb->seekable ? s->pb : mkv->dyn_bc;
1200  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1201  int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1202  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1203  int cluster_size = avio_tell(pb) - (s->pb->seekable ? mkv->cluster_pos : 0);
1204 
1205  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1206  // after 4k and on a keyframe
1207  if (mkv->cluster_pos &&
1208  ((!s->pb->seekable && (cluster_size > 32*1024 || ts > mkv->cluster_pts + 1000))
1209  || cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
1210  || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
1211  av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
1212  " bytes, pts %" PRIu64 "\n", avio_tell(pb), ts);
1213  end_ebml_master(pb, mkv->cluster);
1214  mkv->cluster_pos = 0;
1215  if (mkv->dyn_bc)
1216  mkv_flush_dynbuf(s);
1217  }
1218 
1219  // check if we have an audio packet cached
1220  if (mkv->cur_audio_pkt.size > 0) {
1221  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1222  mkv->cur_audio_pkt.size = 0;
1223  if (ret < 0) {
1224  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1225  return ret;
1226  }
1227  }
1228 
1229  // buffer an audio packet to ensure the packet containing the video
1230  // keyframe's timecode is contained in the same cluster for WebM
1231  if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
1232  ret = mkv_copy_packet(mkv, pkt);
1233  else
1234  ret = mkv_write_packet_internal(s, pkt);
1235  return ret;
1236 }
1237 
1239 {
1240  MatroskaMuxContext *mkv = s->priv_data;
1241  AVIOContext *pb = s->pb;
1242  int64_t currentpos, cuespos;
1243  int ret;
1244 
1245  // check if we have an audio packet cached
1246  if (mkv->cur_audio_pkt.size > 0) {
1247  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1248  mkv->cur_audio_pkt.size = 0;
1249  if (ret < 0) {
1250  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1251  return ret;
1252  }
1253  }
1254 
1255  if (mkv->dyn_bc) {
1256  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1257  mkv_flush_dynbuf(s);
1258  } else if (mkv->cluster_pos) {
1259  end_ebml_master(pb, mkv->cluster);
1260  }
1261 
1262  if (pb->seekable) {
1263  if (mkv->cues->num_entries) {
1264  cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
1265 
1267  if (ret < 0) return ret;
1268  }
1269 
1271 
1272  // update the duration
1273  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1274  currentpos = avio_tell(pb);
1275  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1277 
1278  avio_seek(pb, currentpos, SEEK_SET);
1279  }
1280 
1281  end_ebml_master(pb, mkv->segment);
1282  av_free(mkv->tracks);
1283  av_freep(&mkv->cues->entries);
1284  av_freep(&mkv->cues);
1286 
1287  return 0;
1288 }
1289 
1290 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1291 {
1292  int i;
1293  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1294  if (ff_mkv_codec_tags[i].id == codec_id)
1295  return 1;
1296 
1297  if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
1298  enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
1299  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1300  return 1;
1301  }
1302 
1303  return 0;
1304 }
1305 
1306 #if CONFIG_MATROSKA_MUXER
1307 AVOutputFormat ff_matroska_muxer = {
1308  .name = "matroska",
1309  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1310  .mime_type = "video/x-matroska",
1311  .extensions = "mkv",
1312  .priv_data_size = sizeof(MatroskaMuxContext),
1313  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1315  .video_codec = CONFIG_LIBX264_ENCODER ?
1322  .codec_tag = (const AVCodecTag* const []){
1324  },
1325  .subtitle_codec = AV_CODEC_ID_SSA,
1326  .query_codec = mkv_query_codec,
1327 };
1328 #endif
1329 
1330 #if CONFIG_WEBM_MUXER
1331 AVOutputFormat ff_webm_muxer = {
1332  .name = "webm",
1333  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
1334  .mime_type = "video/webm",
1335  .extensions = "webm",
1336  .priv_data_size = sizeof(MatroskaMuxContext),
1337  .audio_codec = AV_CODEC_ID_VORBIS,
1338  .video_codec = AV_CODEC_ID_VP8,
1344 };
1345 #endif
1346 
1347 #if CONFIG_MATROSKA_AUDIO_MUXER
1348 AVOutputFormat ff_matroska_audio_muxer = {
1349  .name = "matroska",
1350  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1351  .mime_type = "audio/x-matroska",
1352  .extensions = "mka",
1353  .priv_data_size = sizeof(MatroskaMuxContext),
1354  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1355  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
1356  .video_codec = AV_CODEC_ID_NONE,
1361  .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
1362 };
1363 #endif
unsigned int nb_chapters
Definition: avformat.h:969
Definition: lfg.h:25
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:97
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:94
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:75
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:106
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:945
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:727
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:139
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:84
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:3283
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:123
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:92
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:823
uint64_t pts
Definition: matroskaenc.c:61
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:697
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:916
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:185
#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:885
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:158
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
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:124
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2116
int64_t segment_offset
Definition: matroskaenc.c:67
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:143
Definition: matroskaenc.c:46
AVDictionary * metadata
Definition: avformat.h:817
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
mkv_track * tracks
Definition: matroskaenc.c:91
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:198
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:169
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:933
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:192
static int64_t duration
Definition: avplay.c:249
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:1465
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:151
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:182
Format I/O context.
Definition: avformat.h:828
char str[32]
Definition: internal.h:41
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:2112
uint8_t
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:195
static int srt_get_duration(uint8_t **buf)
Definition: matroskaenc.c:1082
static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
Definition: matroskaenc.c:757
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:90
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:68
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:112
#define AV_RB32
Definition: intreadwrite.h:130
uint64_t segmentpos
Definition: matroskaenc.c:48
int id
unique ID to identify the chapter
Definition: avformat.h:814
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:173
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:197
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1101
AVStream ** streams
Definition: avformat.h:876
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:121
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:915
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:105
static int flags
Definition: log.c:42
uint32_t tag
Definition: movenc.c:802
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:272
enum AVCodecID id
Definition: internal.h:42
struct MatroskaMuxContext MatroskaMuxContext
unsigned int audio_buffer_size
Definition: matroskaenc.c:93
#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
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:937
#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: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:1183
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:842
#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:82
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:115
AVPacket cur_audio_pkt
Definition: matroskaenc.c:94
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:95
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:786
AVDictionary * metadata
Definition: avformat.h:972
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1811
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
struct mkv_seekhead mkv_seekhead
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:177
#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:106
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:53
static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1002
unsigned int elementid
Definition: matroskaenc.c:47
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:54
#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:419
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:184
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:959
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:978
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
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:970
enum AVCodecID id
Definition: matroska.h:246
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: utils.c:2130
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:683
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:201
enum AVCodecID codec_id
Definition: mov_chan.c:432
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:297
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:921
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags)
Definition: matroskaenc.c:1047
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:87
#define MATROSKA_ID_TAG
Definition: matroska.h:147
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:875
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:68
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:154
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:31
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:140
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:149
int width
picture width / height.
Definition: avcodec.h:1508
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:202
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:148
const char * name
Definition: avformat.h:376
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static char buffer[20]
Definition: seek-test.c:31
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:190
AVDictionary * metadata
Definition: avformat.h:699
Opaque data information usually sparse.
Definition: avutil.h:183
#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:367
#define EBML_ID_VOID
Definition: matroska.h:45
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:120
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:184
Stream structure.
Definition: avformat.h:622
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:816
NULL
Definition: eval.c:52
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
enum AVMediaType codec_type
Definition: avcodec.h:1347
#define CONFIG_LIBX264_ENCODER
Definition: config.h:951
enum AVCodecID codec_id
Definition: avcodec.h:1350
#define MATROSKA_ID_SEEKID
Definition: matroska.h:165
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:166
int sample_rate
samples per second
Definition: avcodec.h:2104
AVIOContext * pb
I/O context.
Definition: avformat.h:861
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:28
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
struct mkv_seekhead_entry mkv_seekhead_entry
main external API structure.
Definition: avcodec.h:1339
#define MATROSKA_ID_BLOCK
Definition: matroska.h:176
#define MATROSKA_ID_INFO
Definition: matroska.h:56
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:157
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:151
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1365
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:57
int extradata_size
Definition: avcodec.h:1455
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:89
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:162
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:189
#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:172
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:108
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:527
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:138
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
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:949
AVIOContext * dyn_bc
Definition: matroskaenc.c:81
AVMediaType
Definition: avutil.h:177
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:87
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1132
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
#define MATROSKA_ID_TRACKVIDEO
Definition: matroska.h:82
static int ass_get_duration(const uint8_t *p)
Definition: matroskaenc.c:989
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:61
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:193
static const uint16_t scale[4]
#define MATROSKA_ID_FILENAME
Definition: matroska.h:183
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:99
#define MATROSKA_ID_CODECID
Definition: matroska.h:83
int64_t start
Definition: avformat.h:816
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:1238
Main libavformat public API header.
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:142
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:166
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:191
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:191
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:116
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
Definition: matroskaenc.c:461
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:688
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:815
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:42
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:686
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
ebml_master cluster
Definition: matroskaenc.c:84
char * value
Definition: dict.h:76
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:135
mkv_seekhead_entry * entries
Definition: matroskaenc.c:56
int channels
number of audio channels
Definition: avcodec.h:2105
#define av_log2
Definition: intmath.h:85
#define MATROSKA_ID_FILEUID
Definition: matroska.h:186
void * priv_data
Format private data.
Definition: avformat.h:848
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:200
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:113
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:914
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1117
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:194
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:150
#define MODE_MATROSKAv2
Definition: matroskaenc.c:76
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:85
#define MODE_WEBM
Definition: matroskaenc.c:77
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1196
#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:102
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:357
int stream_index
Definition: avcodec.h:917
int num_entries
Definition: matroskaenc.c:69
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
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:181
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
This structure stores compressed data.
Definition: avcodec.h:898
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:158
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
Definition: matroskaenc.c:1290
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:908
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:107
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
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:24