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 FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "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/sha.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/intfloat.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/random_seed.h"
36 #include "libavutil/lfg.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/avstring.h"
39 #include "libavcodec/xiph.h"
40 #include "libavcodec/mpeg4audio.h"
41 
42 typedef struct ebml_master {
43  int64_t pos; ///< absolute offset in the file where the master's elements start
44  int sizebytes; ///< how many bytes were reserved for the size
45 } ebml_master;
46 
47 typedef struct mkv_seekhead_entry {
48  unsigned int elementid;
49  uint64_t segmentpos;
51 
52 typedef struct mkv_seekhead {
53  int64_t filepos;
54  int64_t segment_offset; ///< the file offset to the beginning of the segment
55  int reserved_size; ///< -1 if appending to file
59 } mkv_seekhead;
60 
61 typedef struct {
62  uint64_t pts;
63  int tracknum;
64  int64_t cluster_pos; ///< file offset of the cluster containing the block
65 } mkv_cuepoint;
66 
67 typedef struct {
68  int64_t segment_offset;
71 } mkv_cues;
72 
73 typedef struct {
74  int write_dts;
75  int has_cue;
76 } mkv_track;
77 
78 #define MODE_MATROSKAv2 0x01
79 #define MODE_WEBM 0x02
80 
81 typedef struct MatroskaMuxContext {
82  int mode;
85  int64_t segment_offset;
87  int64_t cluster_pos; ///< file offset of the current cluster
88  int64_t cluster_pts;
89  int64_t duration_offset;
90  int64_t duration;
94 
96 
99 
100 
101 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
102  * offset, 4 bytes for target EBML ID */
103 #define MAX_SEEKENTRY_SIZE 21
104 
105 /** per-cuepoint-track - 3 1-byte EBML IDs, 3 1-byte EBML sizes, 2
106  * 8-byte uint max */
107 #define MAX_CUETRACKPOS_SIZE 22
108 
109 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
110 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
111 
112 
113 static int ebml_id_size(unsigned int id)
114 {
115  return (av_log2(id+1)-1)/7+1;
116 }
117 
118 static void put_ebml_id(AVIOContext *pb, unsigned int id)
119 {
120  int i = ebml_id_size(id);
121  while (i--)
122  avio_w8(pb, (uint8_t)(id >> (i*8)));
123 }
124 
125 /**
126  * Write an EBML size meaning "unknown size".
127  *
128  * @param bytes The number of bytes the size should occupy (maximum: 8).
129  */
130 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
131 {
132  av_assert0(bytes <= 8);
133  avio_w8(pb, 0x1ff >> bytes);
134  while (--bytes)
135  avio_w8(pb, 0xff);
136 }
137 
138 /**
139  * Calculate how many bytes are needed to represent a given number in EBML.
140  */
141 static int ebml_num_size(uint64_t num)
142 {
143  int bytes = 1;
144  while ((num+1) >> bytes*7) bytes++;
145  return bytes;
146 }
147 
148 /**
149  * Write a number in EBML variable length format.
150  *
151  * @param bytes The number of bytes that need to be used to write the number.
152  * If zero, any number of bytes can be used.
153  */
154 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
155 {
156  int i, needed_bytes = ebml_num_size(num);
157 
158  // sizes larger than this are currently undefined in EBML
159  av_assert0(num < (1ULL<<56)-1);
160 
161  if (bytes == 0)
162  // don't care how many bytes are used, so use the min
163  bytes = needed_bytes;
164  // the bytes needed to write the given size would exceed the bytes
165  // that we need to use, so write unknown size. This shouldn't happen.
166  av_assert0(bytes >= needed_bytes);
167 
168  num |= 1ULL << bytes*7;
169  for (i = bytes - 1; i >= 0; i--)
170  avio_w8(pb, (uint8_t)(num >> i*8));
171 }
172 
173 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
174 {
175  int i, bytes = 1;
176  uint64_t tmp = val;
177  while (tmp>>=8) bytes++;
178 
179  put_ebml_id(pb, elementid);
180  put_ebml_num(pb, bytes, 0);
181  for (i = bytes - 1; i >= 0; i--)
182  avio_w8(pb, (uint8_t)(val >> i*8));
183 }
184 
185 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
186 {
187  put_ebml_id(pb, elementid);
188  put_ebml_num(pb, 8, 0);
189  avio_wb64(pb, av_double2int(val));
190 }
191 
192 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
193  const void *buf, int size)
194 {
195  put_ebml_id(pb, elementid);
196  put_ebml_num(pb, size, 0);
197  avio_write(pb, buf, size);
198 }
199 
200 static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
201 {
202  put_ebml_binary(pb, elementid, str, strlen(str));
203 }
204 
205 /**
206  * Write a void element of a given size. Useful for reserving space in
207  * the file to be written to later.
208  *
209  * @param size The number of bytes to reserve, which must be at least 2.
210  */
211 static void put_ebml_void(AVIOContext *pb, uint64_t size)
212 {
213  int64_t currentpos = avio_tell(pb);
214 
215  av_assert0(size >= 2);
216 
218  // we need to subtract the length needed to store the size from the
219  // size we need to reserve so 2 cases, we use 8 bytes to store the
220  // size if possible, 1 byte otherwise
221  if (size < 10)
222  put_ebml_num(pb, size-1, 0);
223  else
224  put_ebml_num(pb, size-9, 8);
225  while(avio_tell(pb) < currentpos + size)
226  avio_w8(pb, 0);
227 }
228 
229 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
230 {
231  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
232  put_ebml_id(pb, elementid);
233  put_ebml_size_unknown(pb, bytes);
234  return (ebml_master){ avio_tell(pb), bytes };
235 }
236 
238 {
239  int64_t pos = avio_tell(pb);
240 
241  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
242  return;
243  put_ebml_num(pb, pos - master.pos, master.sizebytes);
244  avio_seek(pb, pos, SEEK_SET);
245 }
246 
247 static void put_xiph_size(AVIOContext *pb, int size)
248 {
249  int i;
250  for (i = 0; i < size / 255; i++)
251  avio_w8(pb, 255);
252  avio_w8(pb, size % 255);
253 }
254 
255 /**
256  * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
257  * elements. If a maximum number of elements is specified, enough space
258  * will be reserved at the current file location to write a seek head of
259  * that size.
260  *
261  * @param segment_offset The absolute offset to the position in the file
262  * where the segment begins.
263  * @param numelements The maximum number of elements that will be indexed
264  * by this seek head, 0 if unlimited.
265  */
266 static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
267 {
268  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
269  if (new_seekhead == NULL)
270  return NULL;
271 
272  new_seekhead->segment_offset = segment_offset;
273 
274  if (numelements > 0) {
275  new_seekhead->filepos = avio_tell(pb);
276  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
277  // and size, and 3 bytes to guarantee that an EBML void element
278  // will fit afterwards
279  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
280  new_seekhead->max_entries = numelements;
281  put_ebml_void(pb, new_seekhead->reserved_size);
282  }
283  return new_seekhead;
284 }
285 
286 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
287 {
288  mkv_seekhead_entry *entries = seekhead->entries;
289 
290  // don't store more elements than we reserved space for
291  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
292  return -1;
293 
294  entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry));
295  if (entries == NULL)
296  return AVERROR(ENOMEM);
297 
298  entries[seekhead->num_entries ].elementid = elementid;
299  entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
300 
301  seekhead->entries = entries;
302  return 0;
303 }
304 
305 /**
306  * Write the seek head to the file and free it. If a maximum number of
307  * elements was specified to mkv_start_seekhead(), the seek head will
308  * be written at the location reserved for it. Otherwise, it is written
309  * at the current location in the file.
310  *
311  * @return The file offset where the seekhead was written,
312  * -1 if an error occurred.
313  */
314 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
315 {
316  ebml_master metaseek, seekentry;
317  int64_t currentpos;
318  int i;
319 
320  currentpos = avio_tell(pb);
321 
322  if (seekhead->reserved_size > 0) {
323  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
324  currentpos = -1;
325  goto fail;
326  }
327  }
328 
329  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
330  for (i = 0; i < seekhead->num_entries; i++) {
331  mkv_seekhead_entry *entry = &seekhead->entries[i];
332 
334 
336  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
337  put_ebml_id(pb, entry->elementid);
338 
340  end_ebml_master(pb, seekentry);
341  }
342  end_ebml_master(pb, metaseek);
343 
344  if (seekhead->reserved_size > 0) {
345  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
346  put_ebml_void(pb, remaining);
347  avio_seek(pb, currentpos, SEEK_SET);
348 
349  currentpos = seekhead->filepos;
350  }
351 fail:
352  av_free(seekhead->entries);
353  av_free(seekhead);
354 
355  return currentpos;
356 }
357 
358 static mkv_cues * mkv_start_cues(int64_t segment_offset)
359 {
360  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
361  if (cues == NULL)
362  return NULL;
363 
364  cues->segment_offset = segment_offset;
365  return cues;
366 }
367 
368 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
369 {
370  mkv_cuepoint *entries = cues->entries;
371 
372  if (ts < 0)
373  return 0;
374 
375  entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
376  if (entries == NULL)
377  return AVERROR(ENOMEM);
378 
379  entries[cues->num_entries ].pts = ts;
380  entries[cues->num_entries ].tracknum = stream + 1;
381  entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
382 
383  cues->entries = entries;
384  return 0;
385 }
386 
387 static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, mkv_track *tracks, int num_tracks)
388 {
389  ebml_master cues_element;
390  int64_t currentpos;
391  int i, j;
392 
393  currentpos = avio_tell(pb);
394  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
395 
396  for (i = 0; i < cues->num_entries; i++) {
397  ebml_master cuepoint, track_positions;
398  mkv_cuepoint *entry = &cues->entries[i];
399  uint64_t pts = entry->pts;
400 
401  cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
403 
404  // put all the entries from different tracks that have the exact same
405  // timestamp into the same CuePoint
406  for (j = 0; j < num_tracks; j++)
407  tracks[j].has_cue = 0;
408  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
409  int tracknum = entry[j].tracknum - 1;
410  av_assert0(tracknum>=0 && tracknum<num_tracks);
411  if (tracks[tracknum].has_cue)
412  continue;
413  tracks[tracknum].has_cue = 1;
415  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
416  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
417  end_ebml_master(pb, track_positions);
418  }
419  i += j - 1;
420  end_ebml_master(pb, cuepoint);
421  }
422  end_ebml_master(pb, cues_element);
423 
424  return currentpos;
425 }
426 
428 {
429  uint8_t *header_start[3];
430  int header_len[3];
431  int first_header_size;
432  int j;
433 
434  if (codec->codec_id == AV_CODEC_ID_VORBIS)
435  first_header_size = 30;
436  else
437  first_header_size = 42;
438 
440  first_header_size, header_start, header_len) < 0) {
441  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
442  return -1;
443  }
444 
445  avio_w8(pb, 2); // number packets - 1
446  for (j = 0; j < 2; j++) {
447  put_xiph_size(pb, header_len[j]);
448  }
449  for (j = 0; j < 3; j++)
450  avio_write(pb, header_start[j], header_len[j]);
451 
452  return 0;
453 }
454 
455 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
456 {
457  MPEG4AudioConfig mp4ac;
458 
459  if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
460  codec->extradata_size * 8, 1) < 0) {
461  av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
462  return;
463  }
464 
465  *sample_rate = mp4ac.sample_rate;
466  *output_sample_rate = mp4ac.ext_sample_rate;
467 }
468 
469 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
470 {
471  AVIOContext *dyn_cp;
472  uint8_t *codecpriv;
473  int ret, codecpriv_size;
474 
475  ret = avio_open_dyn_buf(&dyn_cp);
476  if(ret < 0)
477  return ret;
478 
479  if (native_id) {
480  if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA)
481  ret = put_xiph_codecpriv(s, dyn_cp, codec);
482  else if (codec->codec_id == AV_CODEC_ID_FLAC)
483  ret = ff_flac_write_header(dyn_cp, codec, 1);
484  else if (codec->codec_id == AV_CODEC_ID_H264)
485  ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
486  else if (codec->codec_id == AV_CODEC_ID_ALAC) {
487  if (codec->extradata_size < 36) {
488  av_log(s, AV_LOG_ERROR,
489  "Invalid extradata found, ALAC expects a 36-byte "
490  "QuickTime atom.");
491  ret = AVERROR_INVALIDDATA;
492  } else
493  avio_write(dyn_cp, codec->extradata + 12,
494  codec->extradata_size - 12);
495  }
496  else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA)
497  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
498  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
499  if (qt_id) {
500  if (!codec->codec_tag)
502  if (codec->extradata_size)
503  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
504  } else {
505  if (!codec->codec_tag)
507  if (!codec->codec_tag) {
508  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
509  avcodec_get_name(codec->codec_id));
510  ret = AVERROR(EINVAL);
511  }
512 
513  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
514  }
515 
516  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
517  unsigned int tag;
519  if (!tag) {
520  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
521  avcodec_get_name(codec->codec_id));
522  ret = AVERROR(EINVAL);
523  }
524  if (!codec->codec_tag)
525  codec->codec_tag = tag;
526 
527  ff_put_wav_header(dyn_cp, codec);
528  }
529 
530  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
531  if (codecpriv_size)
532  put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
533  av_free(codecpriv);
534  return ret;
535 }
536 
538 {
539  MatroskaMuxContext *mkv = s->priv_data;
540  AVIOContext *pb = s->pb;
541  ebml_master tracks;
542  int i, j, ret, default_stream_exists = 0;
543 
545  if (ret < 0) return ret;
546 
547  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
548  for (i = 0; i < s->nb_streams; i++) {
549  AVStream *st = s->streams[i];
550  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
551  }
552  for (i = 0; i < s->nb_streams; i++) {
553  AVStream *st = s->streams[i];
554  AVCodecContext *codec = st->codec;
555  ebml_master subinfo, track;
556  int native_id = 0;
557  int qt_id = 0;
558  int bit_depth = av_get_bits_per_sample(codec->codec_id);
559  int sample_rate = codec->sample_rate;
560  int output_sample_rate = 0;
562 
563  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
564  mkv->have_attachments = 1;
565  continue;
566  }
567 
568  if (!bit_depth)
569  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
570  if (!bit_depth)
571  bit_depth = codec->bits_per_coded_sample;
572 
573  if (codec->codec_id == AV_CODEC_ID_AAC)
574  get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
575 
578  put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
579  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
580 
581  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
583  tag = av_dict_get(st->metadata, "language", NULL, 0);
584  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
585 
586  if (default_stream_exists) {
588  }
591 
592  // look for a codec ID string specific to mkv to use,
593  // if none are found, use AVI codes
594  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
595  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
597  native_id = 1;
598  break;
599  }
600  }
601 
602  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
603  codec->codec_id == AV_CODEC_ID_VORBIS)) {
604  av_log(s, AV_LOG_ERROR,
605  "Only VP8 video and Vorbis audio are supported for WebM.\n");
606  return AVERROR(EINVAL);
607  }
608 
609  switch (codec->codec_type) {
610  case AVMEDIA_TYPE_VIDEO:
612  if(st->avg_frame_rate.num && st->avg_frame_rate.den && 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base))
614  else
616 
617  if (!native_id &&
620  || codec->codec_id == AV_CODEC_ID_SVQ1
621  || codec->codec_id == AV_CODEC_ID_SVQ3
622  || codec->codec_id == AV_CODEC_ID_CINEPAK))
623  qt_id = 1;
624 
625  if (qt_id)
626  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
627  else if (!native_id) {
628  // if there is no mkv-specific codec ID, use VFW mode
629  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
630  mkv->tracks[i].write_dts = 1;
631  }
632 
633  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
634  // XXX: interlace flag?
637 
638  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
639  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
640  // save stereo mode flag
641  uint64_t st_mode = MATROSKA_VIDEO_STEREO_MODE_COUNT;
642 
643  for (j=0; j<MATROSKA_VIDEO_STEREO_MODE_COUNT; j++)
644  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[j])){
645  st_mode = j;
646  break;
647  }
648 
649  if ((mkv->mode == MODE_WEBM && st_mode > 3 && st_mode != 11)
650  || st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) {
651  av_log(s, AV_LOG_ERROR,
652  "The specified stereo mode is not valid.\n");
653  return AVERROR(EINVAL);
654  } else
656  }
657 
658  if (st->sample_aspect_ratio.num) {
659  int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
660  if (d_width > INT_MAX) {
661  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
662  return AVERROR(EINVAL);
663  }
666  }
667 
668  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
669  uint32_t color_space = av_le2ne32(codec->codec_tag);
670  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
671  }
672  end_ebml_master(pb, subinfo);
673  break;
674 
675  case AVMEDIA_TYPE_AUDIO:
677 
678  if (!native_id)
679  // no mkv-specific ID, use ACM mode
680  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
681 
682  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
685  if (output_sample_rate)
686  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
687  if (bit_depth)
689  end_ebml_master(pb, subinfo);
690  break;
691 
694  if (!native_id) {
695  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
696  return AVERROR(ENOSYS);
697  }
698  break;
699  default:
700  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
701  break;
702  }
703  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
704  if (ret < 0) return ret;
705 
706  end_ebml_master(pb, track);
707 
708  // ms precision is the de-facto standard timescale for mkv files
709  avpriv_set_pts_info(st, 64, 1, 1000);
710  }
711  end_ebml_master(pb, tracks);
712  return 0;
713 }
714 
716 {
717  MatroskaMuxContext *mkv = s->priv_data;
718  AVIOContext *pb = s->pb;
719  ebml_master chapters, editionentry;
720  AVRational scale = {1, 1E9};
721  int i, ret;
722 
723  if (!s->nb_chapters)
724  return 0;
725 
727  if (ret < 0) return ret;
728 
729  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
730  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
733  for (i = 0; i < s->nb_chapters; i++) {
734  ebml_master chapteratom, chapterdisplay;
735  AVChapter *c = s->chapters[i];
737 
738  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
741  av_rescale_q(c->start, c->time_base, scale));
743  av_rescale_q(c->end, c->time_base, scale));
746  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
747  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
750  end_ebml_master(pb, chapterdisplay);
751  }
752  end_ebml_master(pb, chapteratom);
753  }
754  end_ebml_master(pb, editionentry);
755  end_ebml_master(pb, chapters);
756  return 0;
757 }
758 
760 {
761  uint8_t *key = av_strdup(t->key);
762  uint8_t *p = key;
763  const uint8_t *lang = NULL;
765 
766  if ((p = strrchr(p, '-')) &&
767  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
768  *p = 0;
769 
770  p = key;
771  while (*p) {
772  if (*p == ' ')
773  *p = '_';
774  else if (*p >= 'a' && *p <= 'z')
775  *p -= 'a' - 'A';
776  p++;
777  }
778 
781  if (lang)
784  end_ebml_master(pb, tag);
785 
786  av_freep(&key);
787 }
788 
789 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
790  unsigned int uid, ebml_master *tags)
791 {
792  MatroskaMuxContext *mkv = s->priv_data;
795  int ret;
796 
797  if (!tags->pos) {
799  if (ret < 0) return ret;
800 
801  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
802  }
803 
804  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
805  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
806  if (elementid)
807  put_ebml_uint(s->pb, elementid, uid);
808  end_ebml_master(s->pb, targets);
809 
810  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
811  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
812  mkv_write_simpletag(s->pb, t);
813 
814  end_ebml_master(s->pb, tag);
815  return 0;
816 }
817 
819 {
820  ebml_master tags = {0};
821  int i, ret;
822 
824 
826  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
827  if (ret < 0) return ret;
828  }
829 
830  for (i = 0; i < s->nb_streams; i++) {
831  AVStream *st = s->streams[i];
832 
833  if (!av_dict_get(st->metadata, "", 0, AV_DICT_IGNORE_SUFFIX))
834  continue;
835 
836  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
837  if (ret < 0) return ret;
838  }
839 
840  for (i = 0; i < s->nb_chapters; i++) {
841  AVChapter *ch = s->chapters[i];
842 
844  continue;
845 
846  ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
847  if (ret < 0) return ret;
848  }
849 
850  if (tags.pos)
851  end_ebml_master(s->pb, tags);
852  return 0;
853 }
854 
856 {
857  MatroskaMuxContext *mkv = s->priv_data;
858  AVIOContext *pb = s->pb;
859  ebml_master attachments;
860  AVLFG c;
861  int i, ret;
862 
863  if (!mkv->have_attachments)
864  return 0;
865 
867 
869  if (ret < 0) return ret;
870 
871  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
872 
873  for (i = 0; i < s->nb_streams; i++) {
874  AVStream *st = s->streams[i];
875  ebml_master attached_file;
877  const char *mimetype = NULL;
878  uint64_t fileuid;
879 
881  continue;
882 
883  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
884 
885  if (t = av_dict_get(st->metadata, "title", NULL, 0))
887  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
888  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
889  return AVERROR(EINVAL);
890  }
892  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
893  mimetype = t->value;
894  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
895  int i;
896  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
897  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
898  mimetype = ff_mkv_mime_tags[i].str;
899  break;
900  }
901  }
902  if (!mimetype) {
903  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
904  "it cannot be deduced from the codec id.\n", i);
905  return AVERROR(EINVAL);
906  }
907 
908  if (st->codec->flags & CODEC_FLAG_BITEXACT) {
909  struct AVSHA *sha = av_sha_alloc();
910  uint8_t digest[20];
911  if (!sha)
912  return AVERROR(ENOMEM);
913  av_sha_init(sha, 160);
915  av_sha_final(sha, digest);
916  av_free(sha);
917  fileuid = AV_RL64(digest);
918  } else {
919  fileuid = av_lfg_get(&c);
920  }
921  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
922  fileuid, i);
923 
926  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
927  end_ebml_master(pb, attached_file);
928  }
929  end_ebml_master(pb, attachments);
930 
931  return 0;
932 }
933 
935 {
936  MatroskaMuxContext *mkv = s->priv_data;
937  AVIOContext *pb = s->pb;
938  ebml_master ebml_header, segment_info;
940  int ret, i;
941 
942  if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
943  else mkv->mode = MODE_MATROSKAv2;
944 
945  if (s->avoid_negative_ts < 0)
946  s->avoid_negative_ts = 1;
947 
948  for (i = 0; i < s->nb_streams; i++)
949  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 ||
950  s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK ||
952  s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR ||
953  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 ||
954  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) {
955  av_log(s, AV_LOG_ERROR,
956  "The Matroska muxer does not yet support muxing %s\n",
958  return AVERROR_PATCHWELCOME;
959  }
960 
961  mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
962  if (!mkv->tracks)
963  return AVERROR(ENOMEM);
964 
965  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
973  end_ebml_master(pb, ebml_header);
974 
976  mkv->segment_offset = avio_tell(pb);
977 
978  // we write 2 seek heads - one at the end of the file to point to each
979  // cluster, and one at the beginning to point to all other level one
980  // elements (including the seek head at the end of the file), which
981  // isn't more than 10 elements if we only write one of each other
982  // currently defined level 1 element
983  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
984  if (!mkv->main_seekhead)
985  return AVERROR(ENOMEM);
986 
988  if (ret < 0) return ret;
989 
990  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
992  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
994  if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
995  uint32_t segment_uid[4];
996  AVLFG lfg;
997 
999 
1000  for (i = 0; i < 4; i++)
1001  segment_uid[i] = av_lfg_get(&lfg);
1002 
1005  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1006  }
1007 
1008  if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
1009  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1010  int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000;
1011  uint8_t date_utc_buf[8];
1012  AV_WB64(date_utc_buf, date_utc);
1013  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1014  }
1015 
1016  // reserve space for the duration
1017  mkv->duration = 0;
1018  mkv->duration_offset = avio_tell(pb);
1019  put_ebml_void(pb, 11); // assumes double-precision float to be written
1020  end_ebml_master(pb, segment_info);
1021 
1022  ret = mkv_write_tracks(s);
1023  if (ret < 0) return ret;
1024 
1025  if (mkv->mode != MODE_WEBM) {
1026  ret = mkv_write_chapters(s);
1027  if (ret < 0) return ret;
1028 
1029  ret = mkv_write_tags(s);
1030  if (ret < 0) return ret;
1031 
1032  ret = mkv_write_attachments(s);
1033  if (ret < 0) return ret;
1034  }
1035 
1036  if (!s->pb->seekable)
1038 
1039  mkv->cues = mkv_start_cues(mkv->segment_offset);
1040  if (mkv->cues == NULL)
1041  return AVERROR(ENOMEM);
1042 
1044  mkv->cur_audio_pkt.size = 0;
1045  mkv->cluster_pos = -1;
1046 
1047  avio_flush(pb);
1048  return 0;
1049 }
1050 
1051 static int mkv_blockgroup_size(int pkt_size)
1052 {
1053  int size = pkt_size + 4;
1054  size += ebml_num_size(size);
1055  size += 2; // EBML ID for block and block duration
1056  size += 8; // max size of block duration
1057  size += ebml_num_size(size);
1058  size += 1; // blockgroup EBML ID
1059  return size;
1060 }
1061 
1062 static int ass_get_duration(const uint8_t *p)
1063 {
1064  int sh, sm, ss, sc, eh, em, es, ec;
1065  uint64_t start, end;
1066 
1067  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
1068  &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
1069  return 0;
1070  start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc;
1071  end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec;
1072  return end - start;
1073 }
1074 
1075 #if FF_API_ASS_SSA
1076 static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1077 {
1078  MatroskaMuxContext *mkv = s->priv_data;
1079  int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
1080  uint8_t *start, *end, *data = pkt->data;
1081  ebml_master blockgroup;
1082  char buffer[2048];
1083 
1084  while (data_size) {
1085  int duration = ass_get_duration(data);
1086  max_duration = FFMAX(duration, max_duration);
1087  end = memchr(data, '\n', data_size);
1088  size = line_size = end ? end-data+1 : data_size;
1089  size -= end ? (end[-1]=='\r')+1 : 0;
1090  start = data;
1091  for (i=0; i<3; i++, start++)
1092  if (!(start = memchr(start, ',', size-(start-data))))
1093  return max_duration;
1094  size -= start - data;
1095  sscanf(data, "Dialogue: %d,", &layer);
1096  i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
1097  s->streams[pkt->stream_index]->nb_frames, layer);
1098  size = FFMIN(i+size, sizeof(buffer));
1099  memcpy(buffer+i, start, size-i);
1100 
1101  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1102  "pts %" PRId64 ", duration %d\n",
1103  avio_tell(pb), size, pkt->pts, duration);
1106  put_ebml_num(pb, size+4, 0);
1107  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1108  avio_wb16(pb, pkt->pts - mkv->cluster_pts);
1109  avio_w8(pb, 0);
1110  avio_write(pb, buffer, size);
1112  end_ebml_master(pb, blockgroup);
1113 
1114  data += line_size;
1115  data_size -= line_size;
1116  }
1117 
1118  return max_duration;
1119 }
1120 #endif
1121 
1123  unsigned int blockid, AVPacket *pkt, int flags)
1124 {
1125  MatroskaMuxContext *mkv = s->priv_data;
1126  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1127  uint8_t *data = NULL;
1128  int offset = 0, size = pkt->size;
1129  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1130 
1131  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1132  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1133  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1134  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1135  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1136  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1137  else
1138  data = pkt->data;
1139 
1140  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1141  /* Matroska specification requires to remove the first QuickTime atom
1142  */
1143  size -= 8;
1144  offset = 8;
1145  }
1146 
1147  put_ebml_id(pb, blockid);
1148  put_ebml_num(pb, size+4, 0);
1149  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1150  avio_wb16(pb, ts - mkv->cluster_pts);
1151  avio_w8(pb, flags);
1152  avio_write(pb, data + offset, size);
1153  if (data != pkt->data)
1154  av_free(data);
1155 }
1156 
1158 {
1159  int i, duration = 0;
1160 
1161  for (i=0; i<2 && !duration; i++) {
1162  int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1163  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1164  &s_hour, &s_min, &s_sec, &s_hsec,
1165  &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1166  s_min += 60*s_hour; e_min += 60*e_hour;
1167  s_sec += 60*s_min; e_sec += 60*e_min;
1168  s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1169  duration = e_hsec - s_hsec;
1170  }
1171  *buf += strcspn(*buf, "\n") + 1;
1172  }
1173  return duration;
1174 }
1175 
1177 {
1178  ebml_master blockgroup;
1179  AVPacket pkt2 = *pkt;
1180  int64_t duration = srt_get_duration(&pkt2.data);
1181  pkt2.size -= pkt2.data - pkt->data;
1182 
1183  blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1184  mkv_blockgroup_size(pkt2.size));
1185  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1187  end_ebml_master(pb, blockgroup);
1188 
1189  return duration;
1190 }
1191 
1193 {
1194  MatroskaMuxContext *mkv = s->priv_data;
1195  int bufsize;
1196  uint8_t *dyn_buf;
1197 
1198  if (!mkv->dyn_bc)
1199  return;
1200 
1201  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1202  avio_write(s->pb, dyn_buf, bufsize);
1203  av_free(dyn_buf);
1204  mkv->dyn_bc = NULL;
1205 }
1206 
1208 {
1209  MatroskaMuxContext *mkv = s->priv_data;
1210  AVIOContext *pb = s->pb;
1211  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1212  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1213  int duration = pkt->duration;
1214  int ret;
1215  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1216 
1217  if (ts == AV_NOPTS_VALUE) {
1218  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1219  return AVERROR(EINVAL);
1220  }
1221 
1222  if (!s->pb->seekable) {
1223  if (!mkv->dyn_bc) {
1224  if ((ret = avio_open_dyn_buf(&mkv->dyn_bc)) < 0) {
1225  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1226  return ret;
1227  }
1228  }
1229  pb = mkv->dyn_bc;
1230  }
1231 
1232  if (mkv->cluster_pos == -1) {
1233  mkv->cluster_pos = avio_tell(s->pb);
1236  mkv->cluster_pts = FFMAX(0, ts);
1237  }
1238 
1239  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1240  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1241 #if FF_API_ASS_SSA
1242  } else if (codec->codec_id == AV_CODEC_ID_SSA) {
1243  duration = mkv_write_ass_blocks(s, pb, pkt);
1244 #endif
1245  } else if (codec->codec_id == AV_CODEC_ID_SRT) {
1246  duration = mkv_write_srt_blocks(s, pb, pkt);
1247  } else {
1249  /* For backward compatibility, prefer convergence_duration. */
1250  if (pkt->convergence_duration > 0) {
1251  duration = pkt->convergence_duration;
1252  }
1253  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1255  end_ebml_master(pb, blockgroup);
1256  }
1257 
1258  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
1259  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
1260  if (ret < 0) return ret;
1261  }
1262 
1263  mkv->duration = FFMAX(mkv->duration, ts + duration);
1264  return 0;
1265 }
1266 
1268 {
1269  MatroskaMuxContext *mkv = s->priv_data;
1270  AVIOContext *pb = s->pb->seekable ? s->pb : mkv->dyn_bc;
1271  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1272  int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1273  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1274  int cluster_size = avio_tell(pb) - (s->pb->seekable ? mkv->cluster_pos : 0);
1275 
1276  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1277  // after 4k and on a keyframe
1278  if (mkv->cluster_pos != -1 &&
1279  ((!s->pb->seekable && (cluster_size > 32*1024 || ts > mkv->cluster_pts + 1000))
1280  || cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
1281  || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
1282  av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
1283  " bytes, pts %" PRIu64 "\n", avio_tell(pb), ts);
1284  end_ebml_master(pb, mkv->cluster);
1285  mkv->cluster_pos = -1;
1286  if (mkv->dyn_bc)
1287  mkv_flush_dynbuf(s);
1288  }
1289 
1290  // check if we have an audio packet cached
1291  if (mkv->cur_audio_pkt.size > 0) {
1292  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1294  if (ret < 0) {
1295  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1296  return ret;
1297  }
1298  }
1299 
1300  // buffer an audio packet to ensure the packet containing the video
1301  // keyframe's timecode is contained in the same cluster for WebM
1302  if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1303  mkv->cur_audio_pkt = *pkt;
1304  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1305  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1306  } else
1307  ret = mkv_write_packet_internal(s, pkt);
1308  return ret;
1309 }
1310 
1312 {
1313  MatroskaMuxContext *mkv = s->priv_data;
1314  AVIOContext *pb = s->pb;
1315  int64_t currentpos, cuespos;
1316  int ret;
1317 
1318  // check if we have an audio packet cached
1319  if (mkv->cur_audio_pkt.size > 0) {
1320  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1322  if (ret < 0) {
1323  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1324  return ret;
1325  }
1326  }
1327 
1328  if (mkv->dyn_bc) {
1329  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1330  mkv_flush_dynbuf(s);
1331  } else if (mkv->cluster_pos != -1) {
1332  end_ebml_master(pb, mkv->cluster);
1333  }
1334 
1335  if (pb->seekable) {
1336  if (mkv->cues->num_entries) {
1337  cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams);
1338 
1340  if (ret < 0) return ret;
1341  }
1342 
1344 
1345  // update the duration
1346  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1347  currentpos = avio_tell(pb);
1348  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1350 
1351  avio_seek(pb, currentpos, SEEK_SET);
1352  }
1353 
1354  end_ebml_master(pb, mkv->segment);
1355  av_free(mkv->tracks);
1356  av_freep(&mkv->cues->entries);
1357  av_freep(&mkv->cues);
1358 
1359  return 0;
1360 }
1361 
1362 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1363 {
1364  int i;
1365  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1366  if (ff_mkv_codec_tags[i].id == codec_id)
1367  return 1;
1368 
1369  if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
1370  enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
1371  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1372  return 1;
1373  }
1374 
1375  return 0;
1376 }
1377 
1379  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
1380  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
1381  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
1382  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
1383  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
1384  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
1385  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
1386  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
1387  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
1388  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
1389  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
1390  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
1391  { AV_CODEC_ID_WAVPACK, 0xFFFFFFFF },
1392  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1393 };
1394 
1396  { AV_CODEC_ID_PRORES, 0xFFFFFFFF },
1397  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
1398  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
1399  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
1400  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
1401  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
1402  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1403 };
1404 
1405 #if CONFIG_MATROSKA_MUXER
1406 AVOutputFormat ff_matroska_muxer = {
1407  .name = "matroska",
1408  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1409  .mime_type = "video/x-matroska",
1410  .extensions = "mkv",
1411  .priv_data_size = sizeof(MatroskaMuxContext),
1412  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1414  .video_codec = CONFIG_LIBX264_ENCODER ?
1421  .codec_tag = (const AVCodecTag* const []){
1424  },
1425 #if FF_API_ASS_SSA
1426  .subtitle_codec = AV_CODEC_ID_SSA,
1427 #else
1428  .subtitle_codec = AV_CODEC_ID_ASS,
1429 #endif
1430  .query_codec = mkv_query_codec,
1431 };
1432 #endif
1433 
1434 #if CONFIG_WEBM_MUXER
1435 AVOutputFormat ff_webm_muxer = {
1436  .name = "webm",
1437  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
1438  .mime_type = "video/webm",
1439  .extensions = "webm",
1440  .priv_data_size = sizeof(MatroskaMuxContext),
1441  .audio_codec = AV_CODEC_ID_VORBIS,
1442  .video_codec = AV_CODEC_ID_VP8,
1448 };
1449 #endif
1450 
1451 #if CONFIG_MATROSKA_AUDIO_MUXER
1452 AVOutputFormat ff_matroska_audio_muxer = {
1453  .name = "matroska",
1454  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1455  .mime_type = "audio/x-matroska",
1456  .extensions = "mka",
1457  .priv_data_size = sizeof(MatroskaMuxContext),
1458  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1460  .video_codec = AV_CODEC_ID_NONE,
1465  .codec_tag = (const AVCodecTag* const []){
1467  },
1468 };
1469 #endif
unsigned int nb_chapters
Definition: avformat.h:1089
Definition: lfg.h:25
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:320
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:361
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:102
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:130
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:71
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:44
void av_sha_update(AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:293
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:99
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:75
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:111
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:988
hash context
Definition: sha.c:32
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:64
static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:759
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:154
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:89
int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:252
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.
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:129
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:97
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
static int write_packet(AVFormatContext *s, AVPacket *pkt)
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:855
uint64_t pts
Definition: matroskaenc.c:62
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:709
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:204
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
#define AV_RB24
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:934
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:173
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:130
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
int64_t segment_offset
Definition: matroskaenc.c:68
const char * master
Definition: vf_curves.c:97
#define av_le2ne32(x)
Definition: bswap.h:96
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:158
Definition: matroskaenc.c:47
FFmpeg currently uses a custom build this text attempts to document some of its obscure features and options Makefile the full command issued by make and its output will be shown on the screen DESTDIR Destination directory for the install targets
Definition: build_system.txt:1
AVDictionary * metadata
Definition: avformat.h:922
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
mkv_track * tracks
Definition: matroskaenc.c:93
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:217
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:184
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:976
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:363
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:211
int64_t pos
absolute offset in the file where the master&#39;s elements start
Definition: matroskaenc.c:43
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:201
Format I/O context.
Definition: avformat.h:944
#define AV_RL64
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:87
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const AVCodecTag additional_audio_tags[]
Definition: matroskaenc.c:1378
Public dictionary API.
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:214
static int srt_get_duration(uint8_t **buf)
Definition: matroskaenc.c:1157
static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
Definition: matroskaenc.c:789
window constants for m
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:95
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:68
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:113
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
uint64_t segmentpos
Definition: matroskaenc.c:49
int id
unique ID to identify the chapter
Definition: avformat.h:919
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
end end
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:192
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:216
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1176
AVStream ** streams
Definition: avformat.h:992
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:127
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
uint8_t * data
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:110
uint32_t tag
Definition: movenc.c:894
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:455
static EbmlSyntax ebml_header[]
Definition: matroskadec.c:295
enum AVCodecID id
struct MatroskaMuxContext MatroskaMuxContext
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#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:248
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
static int64_t duration
Definition: ffplay.c:294
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#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:154
static int write_trailer(AVFormatContext *s)
int has_cue
Definition: matroskaenc.c:75
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:286
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:54
struct AVOutputFormat * oformat
Definition: avformat.h:958
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#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:130
ebml_master segment
Definition: matroskaenc.c:84
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:120
AVPacket cur_audio_pkt
Definition: matroskaenc.c:95
AVCodecID
Identify the syntax and semantics of the bitstream.
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:818
AVDictionary * metadata
Definition: avformat.h:1092
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
struct mkv_seekhead mkv_seekhead
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:196
#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:107
unsigned int elementid
Definition: matroskaenc.c:48
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:55
#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:427
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:203
Spectrum Plot time data
int64_t convergence_duration
Time difference in AVStream->time_base units from the pts of this packet to the point at which the ou...
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
#define FF_COMPLIANCE_NORMAL
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:1051
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
int flags
CODEC_FLAG_*.
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:141
int write_dts
Definition: matroskaenc.c:74
AVChapter ** chapters
Definition: avformat.h:1090
enum AVCodecID id
Definition: matroska.h:265
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:715
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:220
enum AVCodecID codec_id
Definition: mov_chan.c:433
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:716
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:334
#define FFMAX(a, b)
Definition: common.h:56
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:266
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:53
int size
int flags
A combination of AV_PKT_FLAG values.
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags)
Definition: matroskaenc.c:1122
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:101
#define MATROSKA_ID_TAG
Definition: matroska.h:162
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:617
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
#define AV_LOG_VERBOSE
Definition: log.h:157
#define LIBAVFORMAT_IDENT
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)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
#define EBML_ID_EBMLVERSION
Definition: matroska.h:36
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
mkv_cuepoint * entries
Definition: matroskaenc.c:69
#define FFMIN(a, b)
Definition: common.h:58
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:169
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:35
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:212
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:164
ret
Definition: avfilter.c:821
int width
picture width / height.
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREO_MODE_COUNT]
Definition: matroska.c:120
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:221
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:351
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:163
t
Definition: genspecsines3.m:6
const char * name
Definition: avformat.h:378
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1180
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:209
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:1166
AVDictionary * metadata
Definition: avformat.h:711
Opaque data information usually sparse.
Definition: avutil.h:147
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:123
#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:368
#define EBML_ID_VOID
Definition: matroska.h:45
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:126
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:185
Stream structure.
Definition: avformat.h:643
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
Definition: matroskaenc.c:200
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:921
NULL
Definition: eval.c:55
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:605
sample_rate
#define MATROSKA_ID_TRACKFLAGFORCED
Definition: matroska.h:98
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
enum AVMediaType codec_type
static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, mkv_track *tracks, int num_tracks)
Definition: matroskaenc.c:387
enum AVCodecID codec_id
#define MATROSKA_ID_SEEKID
Definition: matroska.h:180
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:220
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
struct mkv_seekhead_entry mkv_seekhead_entry
main external API structure.
#define MATROSKA_ID_BLOCK
Definition: matroska.h:195
#define MATROSKA_ID_INFO
Definition: matroska.h:56
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:172
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:166
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:43
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
void * buf
Definition: avisynth_c.h:594
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:94
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:177
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:208
#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:110
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:187
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:113
synthesis window for stochastic i
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:537
rational number numerator/denominator
Definition: rational.h:43
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
Definition: matroskaenc.c:173
#define MATROSKA_ID_CUETIME
Definition: matroska.h:153
static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
Write the seek head to the file and free it.
Definition: matroskaenc.c:314
AVIOContext * dyn_bc
Definition: matroskaenc.c:83
AVMediaType
Definition: avutil.h:141
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:89
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1207
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
#define snprintf
Definition: snprintf.h:34
#define MATROSKA_ID_TRACKVIDEO
Definition: matroska.h:81
static int ass_get_duration(const uint8_t *p)
Definition: matroskaenc.c:1062
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:118
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
int ff_flac_write_header(AVIOContext *pb, AVCodecContext *codec, int last_block)
mkv_cues * cues
Definition: matroskaenc.c:92
void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
#define AV_WB64(p, darg)
Definition: intreadwrite.h:303
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:61
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
track
Definition: brsc.py:18
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:212
static int flags
Definition: cpu.c:23
#define MATROSKA_ID_FILENAME
Definition: matroska.h:202
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:114
#define MATROSKA_ID_CODECID
Definition: matroska.h:88
const AVCodecTag additional_video_tags[]
Definition: matroskaenc.c:1395
int64_t start
Definition: avformat.h:921
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:1311
Main libavformat public API header.
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:157
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:181
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:210
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:192
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
Definition: matroskaenc.c:469
static double c[64]
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:700
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
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:211
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:920
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:698
Same thing on a dB scale
char * key
Definition: dict.h:81
int den
denominator
Definition: rational.h:45
#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:81
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:60
ASS as defined in Matroska.
#define EBML_ID_HEADER
Definition: matroska.h:33
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:355
ebml_master cluster
Definition: matroskaenc.c:86
char * value
Definition: dict.h:82
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:150
mkv_seekhead_entry * entries
Definition: matroskaenc.c:57
int channels
number of audio channels
#define av_log2
Definition: intmath.h:89
#define MATROSKA_VIDEO_STEREO_MODE_COUNT
Definition: matroska.h:271
#define MATROSKA_ID_FILEUID
Definition: matroska.h:205
void * priv_data
Format private data.
Definition: avformat.h:964
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:219
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1192
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:213
void INT64 start
Definition: avisynth_c.h:594
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:165
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:68
#define MODE_MATROSKAv2
Definition: matroskaenc.c:78
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:105
#define MODE_WEBM
Definition: matroskaenc.c:79
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1267
#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:103
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:358
int num_entries
Definition: matroskaenc.c:70
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:237
int64_t segment_offset
Definition: matroskaenc.c:85
#define CONFIG_LIBX264_ENCODER
Definition: config.h:1169
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:200
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:229
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:91
This structure stores compressed data.
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
Definition: matroskaenc.c:1362
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:247
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:112
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:82
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:27