FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <stdint.h>
23 
24 #include "avc.h"
25 #include "hevc.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "avlanguage.h"
29 #include "flacenc.h"
30 #include "internal.h"
31 #include "isom.h"
32 #include "matroska.h"
33 #include "riff.h"
34 #include "subtitles.h"
35 #include "vorbiscomment.h"
36 #include "wv.h"
37 
38 #include "libavutil/avstring.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/intfloat.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/lfg.h"
44 #include "libavutil/mathematics.h"
45 #include "libavutil/opt.h"
46 #include "libavutil/random_seed.h"
47 #include "libavutil/rational.h"
48 #include "libavutil/samplefmt.h"
49 #include "libavutil/sha.h"
50 #include "libavutil/stereo3d.h"
51 
52 #include "libavcodec/xiph.h"
53 #include "libavcodec/mpeg4audio.h"
54 #include "libavcodec/internal.h"
55 
56 typedef struct ebml_master {
57  int64_t pos; ///< absolute offset in the file where the master's elements start
58  int sizebytes; ///< how many bytes were reserved for the size
59 } ebml_master;
60 
61 typedef struct mkv_seekhead_entry {
62  unsigned int elementid;
63  uint64_t segmentpos;
65 
66 typedef struct mkv_seekhead {
67  int64_t filepos;
68  int64_t segment_offset; ///< the file offset to the beginning of the segment
69  int reserved_size; ///< -1 if appending to file
73 } mkv_seekhead;
74 
75 typedef struct mkv_cuepoint {
76  uint64_t pts;
78  int tracknum;
79  int64_t cluster_pos; ///< file offset of the cluster containing the block
80  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
81  int64_t duration; ///< duration of the block according to time base
82 } mkv_cuepoint;
83 
84 typedef struct mkv_cues {
85  int64_t segment_offset;
88 } mkv_cues;
89 
90 typedef struct mkv_track {
91  int write_dts;
92  int has_cue;
93  int64_t ts_offset;
94 } mkv_track;
95 
96 #define MODE_MATROSKAv2 0x01
97 #define MODE_WEBM 0x02
98 
99 /** Maximum number of tracks allowed in a Matroska file (with track numbers in
100  * range 1 to 126 (inclusive) */
101 #define MAX_TRACKS 126
102 
103 typedef struct MatroskaMuxContext {
104  const AVClass *class;
105  int mode;
108  int64_t segment_offset;
110  int64_t cluster_pos; ///< file offset of the current cluster
111  int64_t cluster_pts;
113  int64_t duration;
117 
119 
121 
124  int64_t cues_pos;
126  int is_dash;
128  int is_live;
129 
132 
134 
137 
140 
141 
142 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
143  * offset, 4 bytes for target EBML ID */
144 #define MAX_SEEKENTRY_SIZE 21
145 
146 /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4
147  * 8-byte uint max */
148 #define MAX_CUETRACKPOS_SIZE 42
149 
150 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
151 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
152 
153 /** Seek preroll value for opus */
154 #define OPUS_SEEK_PREROLL 80000000
155 
156 static int ebml_id_size(unsigned int id)
157 {
158  return (av_log2(id + 1) - 1) / 7 + 1;
159 }
160 
161 static void put_ebml_id(AVIOContext *pb, unsigned int id)
162 {
163  int i = ebml_id_size(id);
164  while (i--)
165  avio_w8(pb, (uint8_t)(id >> (i * 8)));
166 }
167 
168 /**
169  * Write an EBML size meaning "unknown size".
170  *
171  * @param bytes The number of bytes the size should occupy (maximum: 8).
172  */
173 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
174 {
175  av_assert0(bytes <= 8);
176  avio_w8(pb, 0x1ff >> bytes);
177  ffio_fill(pb, 0xff, bytes - 1);
178 }
179 
180 /**
181  * Calculate how many bytes are needed to represent a given number in EBML.
182  */
183 static int ebml_num_size(uint64_t num)
184 {
185  int bytes = 1;
186  while ((num + 1) >> bytes * 7)
187  bytes++;
188  return bytes;
189 }
190 
191 /**
192  * Write a number in EBML variable length format.
193  *
194  * @param bytes The number of bytes that need to be used to write the number.
195  * If zero, any number of bytes can be used.
196  */
197 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
198 {
199  int i, needed_bytes = ebml_num_size(num);
200 
201  // sizes larger than this are currently undefined in EBML
202  av_assert0(num < (1ULL << 56) - 1);
203 
204  if (bytes == 0)
205  // don't care how many bytes are used, so use the min
206  bytes = needed_bytes;
207  // the bytes needed to write the given size would exceed the bytes
208  // that we need to use, so write unknown size. This shouldn't happen.
209  av_assert0(bytes >= needed_bytes);
210 
211  num |= 1ULL << bytes * 7;
212  for (i = bytes - 1; i >= 0; i--)
213  avio_w8(pb, (uint8_t)(num >> i * 8));
214 }
215 
216 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
217 {
218  int i, bytes = 1;
219  uint64_t tmp = val;
220  while (tmp >>= 8)
221  bytes++;
222 
223  put_ebml_id(pb, elementid);
224  put_ebml_num(pb, bytes, 0);
225  for (i = bytes - 1; i >= 0; i--)
226  avio_w8(pb, (uint8_t)(val >> i * 8));
227 }
228 
229 static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
230 {
231  int i, bytes = 1;
232  uint64_t tmp = 2*(val < 0 ? val^-1 : val);
233 
234  while (tmp>>=8) bytes++;
235 
236  put_ebml_id(pb, elementid);
237  put_ebml_num(pb, bytes, 0);
238  for (i = bytes - 1; i >= 0; i--)
239  avio_w8(pb, (uint8_t)(val >> i * 8));
240 }
241 
242 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
243 {
244  put_ebml_id(pb, elementid);
245  put_ebml_num(pb, 8, 0);
246  avio_wb64(pb, av_double2int(val));
247 }
248 
249 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
250  const void *buf, int size)
251 {
252  put_ebml_id(pb, elementid);
253  put_ebml_num(pb, size, 0);
254  avio_write(pb, buf, size);
255 }
256 
257 static void put_ebml_string(AVIOContext *pb, unsigned int elementid,
258  const char *str)
259 {
260  put_ebml_binary(pb, elementid, str, strlen(str));
261 }
262 
263 /**
264  * Write a void element of a given size. Useful for reserving space in
265  * the file to be written to later.
266  *
267  * @param size The number of bytes to reserve, which must be at least 2.
268  */
269 static void put_ebml_void(AVIOContext *pb, uint64_t size)
270 {
271  int64_t currentpos = avio_tell(pb);
272 
273  av_assert0(size >= 2);
274 
276  // we need to subtract the length needed to store the size from the
277  // size we need to reserve so 2 cases, we use 8 bytes to store the
278  // size if possible, 1 byte otherwise
279  if (size < 10)
280  put_ebml_num(pb, size - 1, 0);
281  else
282  put_ebml_num(pb, size - 9, 8);
283  ffio_fill(pb, 0, currentpos + size - avio_tell(pb));
284 }
285 
286 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid,
287  uint64_t expectedsize)
288 {
289  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
290  put_ebml_id(pb, elementid);
291  put_ebml_size_unknown(pb, bytes);
292  return (ebml_master) {avio_tell(pb), bytes };
293 }
294 
296 {
297  int64_t pos = avio_tell(pb);
298 
299  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
300  return;
301  put_ebml_num(pb, pos - master.pos, master.sizebytes);
302  avio_seek(pb, pos, SEEK_SET);
303 }
304 
305 static void put_xiph_size(AVIOContext *pb, int size)
306 {
307  ffio_fill(pb, 255, size / 255);
308  avio_w8(pb, size % 255);
309 }
310 
311 /**
312  * Free the members allocated in the mux context.
313  */
314 static void mkv_free(MatroskaMuxContext *mkv) {
315  if (mkv->main_seekhead) {
317  av_freep(&mkv->main_seekhead);
318  }
319  if (mkv->cues) {
320  av_freep(&mkv->cues->entries);
321  av_freep(&mkv->cues);
322  }
323  av_freep(&mkv->tracks);
324  av_freep(&mkv->stream_durations);
326 }
327 
328 /**
329  * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
330  * elements. If a maximum number of elements is specified, enough space
331  * will be reserved at the current file location to write a seek head of
332  * that size.
333  *
334  * @param segment_offset The absolute offset to the position in the file
335  * where the segment begins.
336  * @param numelements The maximum number of elements that will be indexed
337  * by this seek head, 0 if unlimited.
338  */
339 static mkv_seekhead *mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset,
340  int numelements)
341 {
342  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
343  if (!new_seekhead)
344  return NULL;
345 
346  new_seekhead->segment_offset = segment_offset;
347 
348  if (numelements > 0) {
349  new_seekhead->filepos = avio_tell(pb);
350  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
351  // and size, and 3 bytes to guarantee that an EBML void element
352  // will fit afterwards
353  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
354  new_seekhead->max_entries = numelements;
355  put_ebml_void(pb, new_seekhead->reserved_size);
356  }
357  return new_seekhead;
358 }
359 
360 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
361 {
362  mkv_seekhead_entry *entries = seekhead->entries;
363 
364  // don't store more elements than we reserved space for
365  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
366  return -1;
367 
368  entries = av_realloc_array(entries, seekhead->num_entries + 1, sizeof(mkv_seekhead_entry));
369  if (!entries)
370  return AVERROR(ENOMEM);
371  seekhead->entries = entries;
372 
373  seekhead->entries[seekhead->num_entries].elementid = elementid;
374  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
375 
376  return 0;
377 }
378 
379 /**
380  * Write the seek head to the file and free it. If a maximum number of
381  * elements was specified to mkv_start_seekhead(), the seek head will
382  * be written at the location reserved for it. Otherwise, it is written
383  * at the current location in the file.
384  *
385  * @return The file offset where the seekhead was written,
386  * -1 if an error occurred.
387  */
389 {
390  mkv_seekhead *seekhead = mkv->main_seekhead;
391  ebml_master metaseek, seekentry;
392  int64_t currentpos;
393  int i;
394 
395  currentpos = avio_tell(pb);
396 
397  if (seekhead->reserved_size > 0) {
398  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
399  currentpos = -1;
400  goto fail;
401  }
402  }
403 
404  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
405  for (i = 0; i < seekhead->num_entries; i++) {
406  mkv_seekhead_entry *entry = &seekhead->entries[i];
407 
409 
411  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
412  put_ebml_id(pb, entry->elementid);
413 
415  end_ebml_master(pb, seekentry);
416  }
417  end_ebml_master(pb, metaseek);
418 
419  if (seekhead->reserved_size > 0) {
420  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
421  put_ebml_void(pb, remaining);
422  avio_seek(pb, currentpos, SEEK_SET);
423 
424  currentpos = seekhead->filepos;
425  }
426 fail:
428  av_freep(&mkv->main_seekhead);
429 
430  return currentpos;
431 }
432 
433 static mkv_cues *mkv_start_cues(int64_t segment_offset)
434 {
435  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
436  if (!cues)
437  return NULL;
438 
439  cues->segment_offset = segment_offset;
440  return cues;
441 }
442 
443 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts,
444  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
445 {
446  mkv_cuepoint *entries = cues->entries;
447 
448  if (ts < 0)
449  return 0;
450 
451  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
452  if (!entries)
453  return AVERROR(ENOMEM);
454  cues->entries = entries;
455 
456  cues->entries[cues->num_entries].pts = ts;
457  cues->entries[cues->num_entries].stream_idx = stream;
458  cues->entries[cues->num_entries].tracknum = tracknum;
459  cues->entries[cues->num_entries].cluster_pos = cluster_pos - cues->segment_offset;
460  cues->entries[cues->num_entries].relative_pos = relative_pos;
461  cues->entries[cues->num_entries++].duration = duration;
462 
463  return 0;
464 }
465 
466 static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
467 {
468  AVIOContext *pb = s->pb;
469  ebml_master cues_element;
470  int64_t currentpos;
471  int i, j;
472 
473  currentpos = avio_tell(pb);
474  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
475 
476  for (i = 0; i < cues->num_entries; i++) {
477  ebml_master cuepoint, track_positions;
478  mkv_cuepoint *entry = &cues->entries[i];
479  uint64_t pts = entry->pts;
480  int ctp_nb = 0;
481 
482  // Calculate the number of entries, so we know the element size
483  for (j = 0; j < num_tracks; j++)
484  tracks[j].has_cue = 0;
485  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
486  int tracknum = entry[j].stream_idx;
487  av_assert0(tracknum>=0 && tracknum<num_tracks);
488  if (tracks[tracknum].has_cue && s->streams[tracknum]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
489  continue;
490  tracks[tracknum].has_cue = 1;
491  ctp_nb ++;
492  }
493 
496 
497  // put all the entries from different tracks that have the exact same
498  // timestamp into the same CuePoint
499  for (j = 0; j < num_tracks; j++)
500  tracks[j].has_cue = 0;
501  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
502  int tracknum = entry[j].stream_idx;
503  av_assert0(tracknum>=0 && tracknum<num_tracks);
504  if (tracks[tracknum].has_cue && s->streams[tracknum]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
505  continue;
506  tracks[tracknum].has_cue = 1;
508  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
509  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos);
510  put_ebml_uint(pb, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos);
511  if (entry[j].duration != -1)
513  end_ebml_master(pb, track_positions);
514  }
515  i += j - 1;
516  end_ebml_master(pb, cuepoint);
517  }
518  end_ebml_master(pb, cues_element);
519 
520  return currentpos;
521 }
522 
524 {
525  const uint8_t *header_start[3];
526  int header_len[3];
527  int first_header_size;
528  int j;
529 
530  if (codec->codec_id == AV_CODEC_ID_VORBIS)
531  first_header_size = 30;
532  else
533  first_header_size = 42;
534 
536  first_header_size, header_start, header_len) < 0) {
537  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
538  return -1;
539  }
540 
541  avio_w8(pb, 2); // number packets - 1
542  for (j = 0; j < 2; j++) {
543  put_xiph_size(pb, header_len[j]);
544  }
545  for (j = 0; j < 3; j++)
546  avio_write(pb, header_start[j], header_len[j]);
547 
548  return 0;
549 }
550 
552 {
553  if (codec->extradata && codec->extradata_size == 2)
554  avio_write(pb, codec->extradata, 2);
555  else
556  avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs
557  return 0;
558 }
559 
561  AVIOContext *pb, AVCodecContext *codec)
562 {
563  int write_comment = (codec->channel_layout &&
564  !(codec->channel_layout & ~0x3ffffULL) &&
566  int ret = ff_flac_write_header(pb, codec->extradata, codec->extradata_size,
567  !write_comment);
568 
569  if (ret < 0)
570  return ret;
571 
572  if (write_comment) {
573  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
574  "Lavf" : LIBAVFORMAT_IDENT;
575  AVDictionary *dict = NULL;
576  uint8_t buf[32], *data, *p;
577  int64_t len;
578 
579  snprintf(buf, sizeof(buf), "0x%"PRIx64, codec->channel_layout);
580  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
581 
582  len = ff_vorbiscomment_length(dict, vendor);
583  if (len >= ((1<<24) - 4))
584  return AVERROR(EINVAL);
585 
586  data = av_malloc(len + 4);
587  if (!data) {
588  av_dict_free(&dict);
589  return AVERROR(ENOMEM);
590  }
591 
592  data[0] = 0x84;
593  AV_WB24(data + 1, len);
594 
595  p = data + 4;
596  ff_vorbiscomment_write(&p, &dict, vendor);
597 
598  avio_write(pb, data, len + 4);
599 
600  av_freep(&data);
601  av_dict_free(&dict);
602  }
603 
604  return 0;
605 }
606 
608  int *sample_rate, int *output_sample_rate)
609 {
610  MPEG4AudioConfig mp4ac;
611 
612  if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
613  codec->extradata_size * 8, 1) < 0) {
614  av_log(s, AV_LOG_ERROR,
615  "Error parsing AAC extradata, unable to determine samplerate.\n");
616  return AVERROR(EINVAL);
617  }
618 
619  *sample_rate = mp4ac.sample_rate;
620  *output_sample_rate = mp4ac.ext_sample_rate;
621  return 0;
622 }
623 
625  AVCodecContext *codec,
626  AVIOContext *dyn_cp)
627 {
628  switch (codec->codec_id) {
629  case AV_CODEC_ID_VORBIS:
630  case AV_CODEC_ID_THEORA:
631  return put_xiph_codecpriv(s, dyn_cp, codec);
632  case AV_CODEC_ID_FLAC:
633  return put_flac_codecpriv(s, dyn_cp, codec);
634  case AV_CODEC_ID_WAVPACK:
635  return put_wv_codecpriv(dyn_cp, codec);
636  case AV_CODEC_ID_H264:
637  return ff_isom_write_avcc(dyn_cp, codec->extradata,
638  codec->extradata_size);
639  case AV_CODEC_ID_HEVC:
640  ff_isom_write_hvcc(dyn_cp, codec->extradata,
641  codec->extradata_size, 0);
642  return 0;
643  case AV_CODEC_ID_ALAC:
644  if (codec->extradata_size < 36) {
645  av_log(s, AV_LOG_ERROR,
646  "Invalid extradata found, ALAC expects a 36-byte "
647  "QuickTime atom.");
648  return AVERROR_INVALIDDATA;
649  } else
650  avio_write(dyn_cp, codec->extradata + 12,
651  codec->extradata_size - 12);
652  break;
653  default:
654  if (codec->codec_id == AV_CODEC_ID_PRORES &&
656  avio_wl32(dyn_cp, codec->codec_tag);
657  } else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA)
658  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
659  }
660 
661  return 0;
662 }
663 
665  AVCodecContext *codec, int native_id,
666  int qt_id)
667 {
668  AVIOContext *dyn_cp;
669  uint8_t *codecpriv;
670  int ret, codecpriv_size;
671 
672  ret = avio_open_dyn_buf(&dyn_cp);
673  if (ret < 0)
674  return ret;
675 
676  if (native_id) {
677  ret = mkv_write_native_codecprivate(s, codec, dyn_cp);
678  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
679  if (qt_id) {
680  if (!codec->codec_tag)
682  codec->codec_id);
683  if (codec->extradata_size) {
686  ) {
687  int i;
688  avio_wb32(dyn_cp, 0x5a + codec->extradata_size);
689  avio_wl32(dyn_cp, codec->codec_tag);
690  for(i = 0; i < 0x5a - 8; i++)
691  avio_w8(dyn_cp, 0);
692  }
693  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
694  }
695  } else {
697  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
698  avcodec_get_name(codec->codec_id));
699 
700  if (!codec->codec_tag)
702  codec->codec_id);
703  if (!codec->codec_tag && codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
704  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
705  avcodec_get_name(codec->codec_id));
706  ret = AVERROR(EINVAL);
707  }
708 
709  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0, 0);
710  }
711  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
712  unsigned int tag;
714  if (!tag) {
715  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
716  avcodec_get_name(codec->codec_id));
717  ret = AVERROR(EINVAL);
718  }
719  if (!codec->codec_tag)
720  codec->codec_tag = tag;
721 
723  }
724 
725  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
726  if (codecpriv_size)
728  codecpriv_size);
729  av_free(codecpriv);
730  return ret;
731 }
732 
733 
735  AVStream *st, int mode, int *h_width, int *h_height)
736 {
737  int i;
738  int ret = 0;
741 
742  *h_width = 1;
743  *h_height = 1;
744  // convert metadata into proper side data and add it to the stream
745  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
746  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
747  int stereo_mode = atoi(tag->value);
748 
749  for (i=0; i<MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
750  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
751  stereo_mode = i;
752  break;
753  }
754 
755  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
756  stereo_mode != 10 && stereo_mode != 12) {
757  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
758  if (ret < 0)
759  return ret;
760  }
761  }
762 
763  // iterate to find the stereo3d side data
764  for (i = 0; i < st->nb_side_data; i++) {
765  AVPacketSideData sd = st->side_data[i];
766  if (sd.type == AV_PKT_DATA_STEREO3D) {
767  AVStereo3D *stereo = (AVStereo3D *)sd.data;
768 
769  switch (stereo->type) {
770  case AV_STEREO3D_2D:
772  break;
774  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
777  *h_width = 2;
778  break;
781  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
782  format--;
783  *h_height = 2;
784  break;
787  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
788  format--;
789  break;
790  case AV_STEREO3D_LINES:
792  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
793  format--;
794  *h_height = 2;
795  break;
796  case AV_STEREO3D_COLUMNS:
798  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
799  format--;
800  *h_width = 2;
801  break;
804  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
805  format++;
806  break;
807  }
808  break;
809  }
810  }
811 
812  if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
813  return ret;
814 
815  // if webm, do not write unsupported modes
816  if ((mode == MODE_WEBM &&
819  || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
820  av_log(s, AV_LOG_ERROR,
821  "The specified stereo mode is not valid.\n");
823  return AVERROR(EINVAL);
824  }
825 
826  // write StereoMode if format is valid
828 
829  return ret;
830 }
831 
833  int i, AVIOContext *pb, int default_stream_exists)
834 {
835  AVStream *st = s->streams[i];
836  AVCodecContext *codec = st->codec;
837  ebml_master subinfo, track;
838  int native_id = 0;
839  int qt_id = 0;
841  int sample_rate = codec->sample_rate;
842  int output_sample_rate = 0;
843  int display_width_div = 1;
844  int display_height_div = 1;
845  int j, ret;
847 
848  // ms precision is the de-facto standard timescale for mkv files
849  avpriv_set_pts_info(st, 64, 1, 1000);
850 
851  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
852  mkv->have_attachments = 1;
853  return 0;
854  }
855 
856  if (!bit_depth && codec->codec_id != AV_CODEC_ID_ADPCM_G726)
857  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
858  if (!bit_depth)
859  bit_depth = codec->bits_per_coded_sample;
860 
861  if (codec->codec_id == AV_CODEC_ID_AAC) {
862  ret = get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
863  if (ret < 0)
864  return ret;
865  }
866 
869  mkv->is_dash ? mkv->dash_track_number : i + 1);
871  mkv->is_dash ? mkv->dash_track_number : i + 1);
872  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
873 
874  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
876  tag = av_dict_get(st->metadata, "language", NULL, 0);
877  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
878  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag && tag->value ? tag->value:"und");
879  } else if (tag && tag->value) {
881  }
882 
883  // The default value for TRACKFLAGDEFAULT is 1, so add element
884  // if we need to clear it.
885  if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT))
887 
890 
891  if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) {
892  const char *codec_id;
894  codec_id = "D_WEBVTT/CAPTIONS";
895  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
896  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
897  codec_id = "D_WEBVTT/DESCRIPTIONS";
898  native_id = MATROSKA_TRACK_TYPE_METADATA;
899  } else if (st->disposition & AV_DISPOSITION_METADATA) {
900  codec_id = "D_WEBVTT/METADATA";
901  native_id = MATROSKA_TRACK_TYPE_METADATA;
902  } else {
903  codec_id = "D_WEBVTT/SUBTITLES";
904  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
905  }
906  put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
907  } else {
908  // look for a codec ID string specific to mkv to use,
909  // if none are found, use AVI codes
910  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
911  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
913  native_id = 1;
914  break;
915  }
916  }
917  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->codec_tag) {
918  if (mkv->allow_raw_vfw) {
919  native_id = 0;
920  } else {
921  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
922  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
923  return AVERROR(EINVAL);
924  }
925  }
926  }
927 
928  if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->initial_padding && codec->codec_id == AV_CODEC_ID_OPUS) {
929 // mkv->tracks[i].ts_offset = av_rescale_q(codec->initial_padding,
930 // (AVRational){ 1, codec->sample_rate },
931 // st->time_base);
932 
935  (AVRational){ 1, codec->sample_rate },
936  (AVRational){ 1, 1000000000 }));
937  }
938  if (codec->codec_id == AV_CODEC_ID_OPUS) {
940  }
941 
942  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
943  codec->codec_id == AV_CODEC_ID_VP9 ||
944  codec->codec_id == AV_CODEC_ID_OPUS ||
945  codec->codec_id == AV_CODEC_ID_VORBIS ||
946  codec->codec_id == AV_CODEC_ID_WEBVTT)) {
948  "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
949  return AVERROR(EINVAL);
950  }
951 
952  switch (codec->codec_type) {
953  case AVMEDIA_TYPE_VIDEO:
955 
956  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
957  && av_cmp_q(av_inv_q(st->avg_frame_rate), codec->time_base) > 0)
958  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->avg_frame_rate.den / st->avg_frame_rate.num);
959  else
960  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * codec->time_base.num / codec->time_base.den);
961 
962  if (!native_id &&
963  ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
964  ((!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) && codec->codec_id != AV_CODEC_ID_RAWVIDEO) ||
965  codec->codec_id == AV_CODEC_ID_SVQ1 ||
966  codec->codec_id == AV_CODEC_ID_SVQ3 ||
967  codec->codec_id == AV_CODEC_ID_CINEPAK))
968  qt_id = 1;
969 
970  if (qt_id)
971  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
972  else if (!native_id) {
973  // if there is no mkv-specific codec ID, use VFW mode
974  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
975  mkv->tracks[i].write_dts = 1;
976  s->internal->avoid_negative_ts_use_pts = 0;
977  }
978 
979  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
980  // XXX: interlace flag?
981  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
982  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
983 
984  // check both side data and metadata for stereo information,
985  // write the result to the bitstream if any is found
986  ret = mkv_write_stereo_mode(s, pb, st, mkv->mode,
987  &display_width_div,
988  &display_height_div);
989  if (ret < 0)
990  return ret;
991 
992  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
993  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
994  (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) {
996  }
997 
998  // write DisplayWidth and DisplayHeight, they contain the size of
999  // a single source view and/or the display aspect ratio
1000  if (st->sample_aspect_ratio.num) {
1001  int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1002  if (d_width > INT_MAX) {
1003  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1004  return AVERROR(EINVAL);
1005  }
1006  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
1007  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
1008  } else if (display_width_div != 1 || display_height_div != 1) {
1009  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , codec->width / display_width_div);
1010  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
1011  }
1012 
1013  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
1014  uint32_t color_space = av_le2ne32(codec->codec_tag);
1015  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
1016  }
1017 
1018  end_ebml_master(pb, subinfo);
1019  break;
1020 
1021  case AVMEDIA_TYPE_AUDIO:
1023 
1024  if (!native_id)
1025  // no mkv-specific ID, use ACM mode
1026  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
1027 
1028  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
1029  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
1031  if (output_sample_rate)
1032  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
1033  if (bit_depth)
1035  end_ebml_master(pb, subinfo);
1036  break;
1037 
1038  case AVMEDIA_TYPE_SUBTITLE:
1039  if (!native_id) {
1040  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
1041  return AVERROR(ENOSYS);
1042  }
1043 
1044  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT)
1045  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1046 
1047  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1048  break;
1049  default:
1050  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1051  return AVERROR(EINVAL);
1052  }
1053 
1054  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
1055  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
1056  if (ret < 0)
1057  return ret;
1058  }
1059 
1060  end_ebml_master(pb, track);
1061 
1062  return 0;
1063 }
1064 
1066 {
1067  MatroskaMuxContext *mkv = s->priv_data;
1068  AVIOContext *pb = s->pb;
1069  ebml_master tracks;
1070  int i, ret, default_stream_exists = 0;
1071 
1073  if (ret < 0)
1074  return ret;
1075 
1076  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
1077  for (i = 0; i < s->nb_streams; i++) {
1078  AVStream *st = s->streams[i];
1079  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
1080  }
1081  for (i = 0; i < s->nb_streams; i++) {
1082  ret = mkv_write_track(s, mkv, i, pb, default_stream_exists);
1083  if (ret < 0)
1084  return ret;
1085  }
1086  end_ebml_master(pb, tracks);
1087  return 0;
1088 }
1089 
1091 {
1092  MatroskaMuxContext *mkv = s->priv_data;
1093  AVIOContext *pb = s->pb;
1094  ebml_master chapters, editionentry;
1095  AVRational scale = {1, 1E9};
1096  int i, ret;
1097 
1098  if (!s->nb_chapters || mkv->wrote_chapters)
1099  return 0;
1100 
1102  if (ret < 0) return ret;
1103 
1104  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
1105  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
1108  for (i = 0; i < s->nb_chapters; i++) {
1109  ebml_master chapteratom, chapterdisplay;
1110  AVChapter *c = s->chapters[i];
1111  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
1112  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
1113  AVDictionaryEntry *t = NULL;
1114  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
1115  av_log(s, AV_LOG_ERROR,
1116  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
1117  chapterstart, chapterend);
1118  return AVERROR_INVALIDDATA;
1119  }
1120 
1121  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
1123  put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
1124  put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
1127  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1128  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
1131  end_ebml_master(pb, chapterdisplay);
1132  }
1133  end_ebml_master(pb, chapteratom);
1134  }
1135  end_ebml_master(pb, editionentry);
1136  end_ebml_master(pb, chapters);
1137 
1138  mkv->wrote_chapters = 1;
1139  return 0;
1140 }
1141 
1143 {
1144  uint8_t *key = av_strdup(t->key);
1145  uint8_t *p = key;
1146  const uint8_t *lang = NULL;
1147  ebml_master tag;
1148 
1149  if (!key)
1150  return AVERROR(ENOMEM);
1151 
1152  if ((p = strrchr(p, '-')) &&
1153  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1154  *p = 0;
1155 
1156  p = key;
1157  while (*p) {
1158  if (*p == ' ')
1159  *p = '_';
1160  else if (*p >= 'a' && *p <= 'z')
1161  *p -= 'a' - 'A';
1162  p++;
1163  }
1164 
1167  if (lang)
1170  end_ebml_master(pb, tag);
1171 
1172  av_freep(&key);
1173  return 0;
1174 }
1175 
1177  unsigned int elementid, unsigned int uid,
1178  ebml_master *tags, ebml_master* tag)
1179 {
1180  MatroskaMuxContext *mkv = s->priv_data;
1181  ebml_master targets;
1182  int ret;
1183 
1184  if (!tags->pos) {
1186  if (ret < 0) return ret;
1187 
1188  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
1189  }
1190 
1191  *tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
1192  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
1193  if (elementid)
1194  put_ebml_uint(s->pb, elementid, uid);
1195  end_ebml_master(s->pb, targets);
1196  return 0;
1197 }
1198 
1199 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
1200  unsigned int uid, ebml_master *tags)
1201 {
1202  ebml_master tag;
1203  int ret;
1204  AVDictionaryEntry *t = NULL;
1205 
1206  ret = mkv_write_tag_targets(s, elementid, uid, tags, &tag);
1207  if (ret < 0)
1208  return ret;
1209 
1210  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
1211  if (av_strcasecmp(t->key, "title") &&
1212  av_strcasecmp(t->key, "stereo_mode") &&
1213  av_strcasecmp(t->key, "encoding_tool")) {
1214  ret = mkv_write_simpletag(s->pb, t);
1215  if (ret < 0)
1216  return ret;
1217  }
1218  }
1219 
1220  end_ebml_master(s->pb, tag);
1221  return 0;
1222 }
1223 
1225 {
1226  AVDictionaryEntry *t = NULL;
1227 
1228  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1229  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
1230  return 1;
1231 
1232  return 0;
1233 }
1234 
1236 {
1237  MatroskaMuxContext *mkv = s->priv_data;
1238  ebml_master tags = {0};
1239  int i, ret;
1240 
1242 
1243  if (mkv_check_tag(s->metadata)) {
1244  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
1245  if (ret < 0) return ret;
1246  }
1247 
1248  for (i = 0; i < s->nb_streams; i++) {
1249  AVStream *st = s->streams[i];
1250 
1251  if (!mkv_check_tag(st->metadata))
1252  continue;
1253 
1254  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
1255  if (ret < 0) return ret;
1256  }
1257 
1258  if (!mkv->is_live) {
1259  for (i = 0; i < s->nb_streams; i++) {
1260  ebml_master tag_target;
1261  ebml_master tag;
1262 
1263  mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags, &tag_target);
1264 
1266  put_ebml_string(s->pb, MATROSKA_ID_TAGNAME, "DURATION");
1267  mkv->stream_duration_offsets[i] = avio_tell(s->pb);
1268 
1269  // Reserve space to write duration as a 20-byte string.
1270  // 2 (ebml id) + 1 (data size) + 20 (data)
1271  put_ebml_void(s->pb, 23);
1272  end_ebml_master(s->pb, tag);
1273  end_ebml_master(s->pb, tag_target);
1274  }
1275  }
1276 
1277  for (i = 0; i < s->nb_chapters; i++) {
1278  AVChapter *ch = s->chapters[i];
1279 
1280  if (!mkv_check_tag(ch->metadata))
1281  continue;
1282 
1284  if (ret < 0) return ret;
1285  }
1286 
1287  if (tags.pos)
1288  end_ebml_master(s->pb, tags);
1289  return 0;
1290 }
1291 
1293 {
1294  MatroskaMuxContext *mkv = s->priv_data;
1295  AVIOContext *pb = s->pb;
1296  ebml_master attachments;
1297  AVLFG c;
1298  int i, ret;
1299 
1300  if (!mkv->have_attachments)
1301  return 0;
1302 
1304 
1306  if (ret < 0) return ret;
1307 
1308  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
1309 
1310  for (i = 0; i < s->nb_streams; i++) {
1311  AVStream *st = s->streams[i];
1312  ebml_master attached_file;
1313  AVDictionaryEntry *t;
1314  const char *mimetype = NULL;
1315  uint64_t fileuid;
1316 
1318  continue;
1319 
1320  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
1321 
1322  if (t = av_dict_get(st->metadata, "title", NULL, 0))
1324  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
1325  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
1326  return AVERROR(EINVAL);
1327  }
1329  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
1330  mimetype = t->value;
1331  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
1332  int i;
1333  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1334  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
1335  mimetype = ff_mkv_mime_tags[i].str;
1336  break;
1337  }
1338  for (i = 0; ff_mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1339  if (ff_mkv_image_mime_tags[i].id == st->codec->codec_id) {
1340  mimetype = ff_mkv_image_mime_tags[i].str;
1341  break;
1342  }
1343  }
1344  if (!mimetype) {
1345  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1346  "it cannot be deduced from the codec id.\n", i);
1347  return AVERROR(EINVAL);
1348  }
1349 
1350  if (s->flags & AVFMT_FLAG_BITEXACT) {
1351  struct AVSHA *sha = av_sha_alloc();
1352  uint8_t digest[20];
1353  if (!sha)
1354  return AVERROR(ENOMEM);
1355  av_sha_init(sha, 160);
1357  av_sha_final(sha, digest);
1358  av_free(sha);
1359  fileuid = AV_RL64(digest);
1360  } else {
1361  fileuid = av_lfg_get(&c);
1362  }
1363  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
1364  fileuid, i);
1365 
1368  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
1369  end_ebml_master(pb, attached_file);
1370  }
1371  end_ebml_master(pb, attachments);
1372 
1373  return 0;
1374 }
1375 
1377 {
1378  MatroskaMuxContext *mkv = s->priv_data;
1379  AVIOContext *pb = s->pb;
1380  ebml_master ebml_header, segment_info;
1382  int ret, i, version = 2;
1383 
1384  if (!strcmp(s->oformat->name, "webm"))
1385  mkv->mode = MODE_WEBM;
1386  else
1387  mkv->mode = MODE_MATROSKAv2;
1388 
1389  if (s->avoid_negative_ts < 0) {
1390  s->avoid_negative_ts = 1;
1392  }
1393 
1394  if (mkv->mode != MODE_WEBM ||
1395  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
1396  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
1397  version = 4;
1398 
1399  for (i = 0; i < s->nb_streams; i++) {
1400  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 ||
1401  s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK ||
1403  s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR ||
1404  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 ||
1405  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) {
1406  av_log(s, AV_LOG_ERROR,
1407  "The Matroska muxer does not yet support muxing %s\n",
1409  return AVERROR_PATCHWELCOME;
1410  }
1411  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_OPUS ||
1412  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
1413  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
1414  version = 4;
1415  }
1416 
1417  mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks));
1418  if (!mkv->tracks) {
1419  ret = AVERROR(ENOMEM);
1420  goto fail;
1421  }
1422  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1428  put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , version);
1430  end_ebml_master(pb, ebml_header);
1431 
1433  mkv->segment_offset = avio_tell(pb);
1434 
1435  // we write 2 seek heads - one at the end of the file to point to each
1436  // cluster, and one at the beginning to point to all other level one
1437  // elements (including the seek head at the end of the file), which
1438  // isn't more than 10 elements if we only write one of each other
1439  // currently defined level 1 element
1440  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1441  if (!mkv->main_seekhead) {
1442  ret = AVERROR(ENOMEM);
1443  goto fail;
1444  }
1445 
1447  if (ret < 0) goto fail;
1448 
1449  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
1451  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1453  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
1454  uint32_t segment_uid[4];
1455  AVLFG lfg;
1456 
1458 
1459  for (i = 0; i < 4; i++)
1460  segment_uid[i] = av_lfg_get(&lfg);
1461 
1463  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
1465  else
1467  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1468  } else {
1469  const char *ident = "Lavf";
1472  }
1473 
1474  if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
1475  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1476  int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000;
1477  uint8_t date_utc_buf[8];
1478  AV_WB64(date_utc_buf, date_utc);
1479  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1480  }
1481 
1482  // reserve space for the duration
1483  mkv->duration = 0;
1484  mkv->duration_offset = avio_tell(pb);
1485  if (!mkv->is_live) {
1486  put_ebml_void(pb, 11); // assumes double-precision float to be written
1487  }
1488  end_ebml_master(pb, segment_info);
1489 
1490  // initialize stream_duration fields
1491  mkv->stream_durations = av_mallocz(s->nb_streams * sizeof(int64_t));
1492  mkv->stream_duration_offsets = av_mallocz(s->nb_streams * sizeof(int64_t));
1493 
1494  ret = mkv_write_tracks(s);
1495  if (ret < 0)
1496  goto fail;
1497 
1498  for (i = 0; i < s->nb_chapters; i++)
1499  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1500 
1501  if (mkv->mode != MODE_WEBM) {
1502  ret = mkv_write_chapters(s);
1503  if (ret < 0)
1504  goto fail;
1505 
1506  ret = mkv_write_tags(s);
1507  if (ret < 0)
1508  goto fail;
1509 
1510  ret = mkv_write_attachments(s);
1511  if (ret < 0)
1512  goto fail;
1513  }
1514 
1515  if (!s->pb->seekable && !mkv->is_live)
1516  mkv_write_seekhead(pb, mkv);
1517 
1518  mkv->cues = mkv_start_cues(mkv->segment_offset);
1519  if (!mkv->cues) {
1520  ret = AVERROR(ENOMEM);
1521  goto fail;
1522  }
1523  if (pb->seekable && mkv->reserve_cues_space) {
1524  mkv->cues_pos = avio_tell(pb);
1526  }
1527 
1529  mkv->cur_audio_pkt.size = 0;
1530  mkv->cluster_pos = -1;
1531 
1532  avio_flush(pb);
1533 
1534  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1535  // after 4k and on a keyframe
1536  if (pb->seekable) {
1537  if (mkv->cluster_time_limit < 0)
1538  mkv->cluster_time_limit = 5000;
1539  if (mkv->cluster_size_limit < 0)
1540  mkv->cluster_size_limit = 5 * 1024 * 1024;
1541  } else {
1542  if (mkv->cluster_time_limit < 0)
1543  mkv->cluster_time_limit = 1000;
1544  if (mkv->cluster_size_limit < 0)
1545  mkv->cluster_size_limit = 32 * 1024;
1546  }
1547 
1548  return 0;
1549 fail:
1550  mkv_free(mkv);
1551  return ret;
1552 }
1553 
1554 static int mkv_blockgroup_size(int pkt_size)
1555 {
1556  int size = pkt_size + 4;
1557  size += ebml_num_size(size);
1558  size += 2; // EBML ID for block and block duration
1559  size += 8; // max size of block duration
1560  size += ebml_num_size(size);
1561  size += 1; // blockgroup EBML ID
1562  return size;
1563 }
1564 
1565 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1566 {
1567  uint8_t *dst;
1568  int srclen = *size;
1569  int offset = 0;
1570  int ret;
1571 
1572  dst = av_malloc(srclen);
1573  if (!dst)
1574  return AVERROR(ENOMEM);
1575 
1576  while (srclen >= WV_HEADER_SIZE) {
1577  WvHeader header;
1578 
1579  ret = ff_wv_parse_header(&header, src);
1580  if (ret < 0)
1581  goto fail;
1582  src += WV_HEADER_SIZE;
1583  srclen -= WV_HEADER_SIZE;
1584 
1585  if (srclen < header.blocksize) {
1586  ret = AVERROR_INVALIDDATA;
1587  goto fail;
1588  }
1589 
1590  if (header.initial) {
1591  AV_WL32(dst + offset, header.samples);
1592  offset += 4;
1593  }
1594  AV_WL32(dst + offset, header.flags);
1595  AV_WL32(dst + offset + 4, header.crc);
1596  offset += 8;
1597 
1598  if (!(header.initial && header.final)) {
1599  AV_WL32(dst + offset, header.blocksize);
1600  offset += 4;
1601  }
1602 
1603  memcpy(dst + offset, src, header.blocksize);
1604  src += header.blocksize;
1605  srclen -= header.blocksize;
1606  offset += header.blocksize;
1607  }
1608 
1609  *pdst = dst;
1610  *size = offset;
1611 
1612  return 0;
1613 fail:
1614  av_freep(&dst);
1615  return ret;
1616 }
1617 
1619  unsigned int blockid, AVPacket *pkt, int keyframe)
1620 {
1621  MatroskaMuxContext *mkv = s->priv_data;
1622  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1623  uint8_t *data = NULL, *side_data = NULL;
1624  int offset = 0, size = pkt->size, side_data_size = 0;
1625  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1626  uint64_t additional_id = 0;
1627  int64_t discard_padding = 0;
1628  uint8_t track_number = (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1));
1629  ebml_master block_group, block_additions, block_more;
1630 
1631  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1632  "pts %" PRId64 ", dts %" PRId64 ", duration %d, keyframe %d\n",
1633  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration,
1634  keyframe != 0);
1635  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1636  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1637  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1638  else if (codec->codec_id == AV_CODEC_ID_HEVC && codec->extradata_size > 6 &&
1639  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1640  /* extradata is Annex B, assume the bitstream is too and convert it */
1641  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
1642  else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {
1643  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1644  if (ret < 0) {
1645  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1646  return;
1647  }
1648  } else
1649  data = pkt->data;
1650 
1651  if (codec->codec_id == AV_CODEC_ID_PRORES && size >= 8) {
1652  /* Matroska specification requires to remove the first QuickTime atom
1653  */
1654  size -= 8;
1655  offset = 8;
1656  }
1657 
1658  side_data = av_packet_get_side_data(pkt,
1660  &side_data_size);
1661 
1662  if (side_data && side_data_size >= 10) {
1663  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
1664  (AVRational){1, codec->sample_rate},
1665  (AVRational){1, 1000000000});
1666  }
1667 
1668  side_data = av_packet_get_side_data(pkt,
1670  &side_data_size);
1671  if (side_data) {
1672  additional_id = AV_RB64(side_data);
1673  side_data += 8;
1674  side_data_size -= 8;
1675  }
1676 
1677  if ((side_data_size && additional_id == 1) || discard_padding) {
1678  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
1679  blockid = MATROSKA_ID_BLOCK;
1680  }
1681 
1682  put_ebml_id(pb, blockid);
1683  put_ebml_num(pb, size + 4, 0);
1684  // this assumes stream_index is less than 126
1685  avio_w8(pb, 0x80 | track_number);
1686  avio_wb16(pb, ts - mkv->cluster_pts);
1687  avio_w8(pb, (blockid == MATROSKA_ID_SIMPLEBLOCK && keyframe) ? (1 << 7) : 0);
1688  avio_write(pb, data + offset, size);
1689  if (data != pkt->data)
1690  av_free(data);
1691 
1692  if (blockid == MATROSKA_ID_BLOCK && !keyframe) {
1694  mkv->last_track_timestamp[track_number - 1]);
1695  }
1696  mkv->last_track_timestamp[track_number - 1] = ts - mkv->cluster_pts;
1697 
1698  if (discard_padding) {
1699  put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
1700  }
1701 
1702  if (side_data_size && additional_id == 1) {
1703  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
1704  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
1707  put_ebml_num(pb, side_data_size, 0);
1708  avio_write(pb, side_data, side_data_size);
1709  end_ebml_master(pb, block_more);
1710  end_ebml_master(pb, block_additions);
1711  }
1712  if ((side_data_size && additional_id == 1) || discard_padding) {
1713  end_ebml_master(pb, block_group);
1714  }
1715 }
1716 
1718 {
1719  MatroskaMuxContext *mkv = s->priv_data;
1720  ebml_master blockgroup;
1721  int id_size, settings_size, size;
1722  uint8_t *id, *settings;
1723  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1724  const int flags = 0;
1725 
1726  id_size = 0;
1728  &id_size);
1729 
1730  settings_size = 0;
1732  &settings_size);
1733 
1734  size = id_size + 1 + settings_size + 1 + pkt->size;
1735 
1736  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1737  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1738  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
1739 
1741 
1743  put_ebml_num(pb, size + 4, 0);
1744  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1745  avio_wb16(pb, ts - mkv->cluster_pts);
1746  avio_w8(pb, flags);
1747  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
1748 
1750  end_ebml_master(pb, blockgroup);
1751 
1752  return pkt->duration;
1753 }
1754 
1756 {
1757  MatroskaMuxContext *mkv = s->priv_data;
1758  int bufsize;
1759  uint8_t *dyn_buf;
1760 
1761  if (!mkv->dyn_bc)
1762  return;
1763 
1764  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1765  avio_write(s->pb, dyn_buf, bufsize);
1766  av_free(dyn_buf);
1767  mkv->dyn_bc = NULL;
1768 }
1769 
1771 {
1772  MatroskaMuxContext *mkv = s->priv_data;
1773  AVIOContext *pb;
1774 
1775  if (s->pb->seekable) {
1776  pb = s->pb;
1777  } else {
1778  pb = mkv->dyn_bc;
1779  }
1780 
1781  av_log(s, AV_LOG_DEBUG,
1782  "Starting new cluster at offset %" PRIu64 " bytes, "
1783  "pts %" PRIu64 "dts %" PRIu64 "\n",
1784  avio_tell(pb), pkt->pts, pkt->dts);
1785  end_ebml_master(pb, mkv->cluster);
1786  mkv->cluster_pos = -1;
1787  if (mkv->dyn_bc)
1788  mkv_flush_dynbuf(s);
1789  avio_flush(s->pb);
1790 }
1791 
1793 {
1794  MatroskaMuxContext *mkv = s->priv_data;
1795  AVIOContext *pb = s->pb;
1796  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1797  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1798  int duration = pkt->duration;
1799  int ret;
1800  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1801  int64_t relative_packet_pos;
1802  int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;
1803 
1804  if (ts == AV_NOPTS_VALUE) {
1805  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1806  return AVERROR(EINVAL);
1807  }
1808  ts += mkv->tracks[pkt->stream_index].ts_offset;
1809 
1810  if (mkv->cluster_pos != -1) {
1811  int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;
1812  if ((int16_t)cluster_time != cluster_time) {
1813  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
1814  mkv_start_new_cluster(s, pkt);
1815  }
1816  }
1817 
1818  if (!s->pb->seekable) {
1819  if (!mkv->dyn_bc) {
1820  ret = avio_open_dyn_buf(&mkv->dyn_bc);
1821  if (ret < 0) {
1822  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1823  return ret;
1824  }
1825  }
1826  pb = mkv->dyn_bc;
1827  }
1828 
1829  if (mkv->cluster_pos == -1) {
1830  mkv->cluster_pos = avio_tell(s->pb);
1833  mkv->cluster_pts = FFMAX(0, ts);
1834  }
1835 
1836  relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos;
1837 
1838  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1839  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);
1840  if (s->pb->seekable && (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {
1841  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);
1842  if (ret < 0) return ret;
1843  }
1844  } else {
1845  if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
1846  duration = mkv_write_vtt_blocks(s, pb, pkt);
1847  } else {
1849  mkv_blockgroup_size(pkt->size));
1850  /* For backward compatibility, prefer convergence_duration. */
1851  if (pkt->convergence_duration > 0) {
1852  duration = pkt->convergence_duration;
1853  }
1854  /* All subtitle blocks are considered to be keyframes. */
1855  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 1);
1857  end_ebml_master(pb, blockgroup);
1858  }
1859 
1860  if (s->pb->seekable) {
1861  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
1862  mkv->cluster_pos, relative_packet_pos, duration);
1863  if (ret < 0)
1864  return ret;
1865  }
1866  }
1867 
1868  mkv->duration = FFMAX(mkv->duration, ts + duration);
1869 
1870  if (mkv->stream_durations)
1871  mkv->stream_durations[pkt->stream_index] =
1872  FFMAX(mkv->stream_durations[pkt->stream_index], ts + duration);
1873 
1874  return 0;
1875 }
1876 
1878 {
1879  MatroskaMuxContext *mkv = s->priv_data;
1880  int codec_type = s->streams[pkt->stream_index]->codec->codec_type;
1881  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1882  int cluster_size;
1883  int64_t cluster_time;
1884  int ret;
1885  int start_new_cluster;
1886 
1887  if (mkv->tracks[pkt->stream_index].write_dts)
1888  cluster_time = pkt->dts - mkv->cluster_pts;
1889  else
1890  cluster_time = pkt->pts - mkv->cluster_pts;
1891  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
1892 
1893  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1894  // after 4k and on a keyframe
1895  if (s->pb->seekable) {
1896  cluster_size = avio_tell(s->pb) - mkv->cluster_pos;
1897  } else {
1898  cluster_size = avio_tell(mkv->dyn_bc);
1899  }
1900 
1901  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
1902  // WebM DASH specification states that the first block of every cluster
1903  // has to be a key frame. So for DASH video, we only create a cluster
1904  // on seeing key frames.
1905  start_new_cluster = keyframe;
1906  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
1907  (mkv->cluster_pos == -1 ||
1908  cluster_time > mkv->cluster_time_limit)) {
1909  // For DASH audio, we create a Cluster based on cluster_time_limit
1910  start_new_cluster = 1;
1911  } else if (!mkv->is_dash &&
1912  (cluster_size > mkv->cluster_size_limit ||
1913  cluster_time > mkv->cluster_time_limit ||
1914  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
1915  cluster_size > 4 * 1024))) {
1916  start_new_cluster = 1;
1917  } else {
1918  start_new_cluster = 0;
1919  }
1920 
1921  if (mkv->cluster_pos != -1 && start_new_cluster) {
1922  mkv_start_new_cluster(s, pkt);
1923  }
1924 
1925  // check if we have an audio packet cached
1926  if (mkv->cur_audio_pkt.size > 0) {
1927  // for DASH audio, a CuePoint has to be added when there is a new cluster.
1929  mkv->is_dash ? start_new_cluster : 0);
1931  if (ret < 0) {
1932  av_log(s, AV_LOG_ERROR,
1933  "Could not write cached audio packet ret:%d\n", ret);
1934  return ret;
1935  }
1936  }
1937 
1938  // buffer an audio packet to ensure the packet containing the video
1939  // keyframe's timecode is contained in the same cluster for WebM
1940  if (codec_type == AVMEDIA_TYPE_AUDIO) {
1941  mkv->cur_audio_pkt = *pkt;
1942  if (pkt->buf) {
1943  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1944  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1945  } else
1946  ret = av_dup_packet(&mkv->cur_audio_pkt);
1947  if (mkv->cur_audio_pkt.side_data_elems > 0) {
1949  }
1950  } else
1951  ret = mkv_write_packet_internal(s, pkt, 0);
1952  return ret;
1953 }
1954 
1956 {
1957  MatroskaMuxContext *mkv = s->priv_data;
1958  AVIOContext *pb;
1959  if (s->pb->seekable)
1960  pb = s->pb;
1961  else
1962  pb = mkv->dyn_bc;
1963  if (!pkt) {
1964  if (mkv->cluster_pos != -1) {
1965  av_log(s, AV_LOG_DEBUG,
1966  "Flushing cluster at offset %" PRIu64 " bytes\n",
1967  avio_tell(pb));
1968  end_ebml_master(pb, mkv->cluster);
1969  mkv->cluster_pos = -1;
1970  if (mkv->dyn_bc)
1971  mkv_flush_dynbuf(s);
1972  avio_flush(s->pb);
1973  }
1974  return 1;
1975  }
1976  return mkv_write_packet(s, pkt);
1977 }
1978 
1980 {
1981  MatroskaMuxContext *mkv = s->priv_data;
1982  AVIOContext *pb = s->pb;
1983  int64_t currentpos, cuespos;
1984  int ret;
1985 
1986  // check if we have an audio packet cached
1987  if (mkv->cur_audio_pkt.size > 0) {
1988  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
1990  if (ret < 0) {
1991  av_log(s, AV_LOG_ERROR,
1992  "Could not write cached audio packet ret:%d\n", ret);
1993  return ret;
1994  }
1995  }
1996 
1997  if (mkv->dyn_bc) {
1998  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1999  mkv_flush_dynbuf(s);
2000  } else if (mkv->cluster_pos != -1) {
2001  end_ebml_master(pb, mkv->cluster);
2002  }
2003 
2004  if (mkv->mode != MODE_WEBM) {
2005  ret = mkv_write_chapters(s);
2006  if (ret < 0)
2007  return ret;
2008  }
2009 
2010  if (pb->seekable) {
2011  if (mkv->cues->num_entries) {
2012  if (mkv->reserve_cues_space) {
2013  int64_t cues_end;
2014 
2015  currentpos = avio_tell(pb);
2016  avio_seek(pb, mkv->cues_pos, SEEK_SET);
2017 
2018  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
2019  cues_end = avio_tell(pb);
2020  if (cues_end > cuespos + mkv->reserve_cues_space) {
2021  av_log(s, AV_LOG_ERROR,
2022  "Insufficient space reserved for cues: %d "
2023  "(needed: %" PRId64 ").\n",
2024  mkv->reserve_cues_space, cues_end - cuespos);
2025  return AVERROR(EINVAL);
2026  }
2027 
2028  if (cues_end < cuespos + mkv->reserve_cues_space)
2030  (cues_end - cuespos));
2031 
2032  avio_seek(pb, currentpos, SEEK_SET);
2033  } else {
2034  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
2035  }
2036 
2038  cuespos);
2039  if (ret < 0)
2040  return ret;
2041  }
2042 
2043  mkv_write_seekhead(pb, mkv);
2044 
2045  // update the duration
2046  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
2047  currentpos = avio_tell(pb);
2048  avio_seek(pb, mkv->duration_offset, SEEK_SET);
2050 
2051  // update stream durations
2052  if (mkv->stream_durations) {
2053  int i;
2054  for (i = 0; i < s->nb_streams; ++i) {
2055  AVStream *st = s->streams[i];
2056  double duration_sec = mkv->stream_durations[i] * av_q2d(st->time_base);
2057  char duration_string[20] = "";
2058 
2059  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
2060  mkv->stream_durations[i]);
2061 
2062  if (!mkv->is_live && mkv->stream_duration_offsets[i] > 0) {
2063  avio_seek(pb, mkv->stream_duration_offsets[i], SEEK_SET);
2064 
2065  snprintf(duration_string, 20, "%02d:%02d:%012.9f",
2066  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
2067  fmod(duration_sec, 60));
2068 
2069  put_ebml_binary(pb, MATROSKA_ID_TAGSTRING, duration_string, 20);
2070  }
2071  }
2072  }
2073 
2074  avio_seek(pb, currentpos, SEEK_SET);
2075  }
2076 
2077  if (!mkv->is_live) {
2078  end_ebml_master(pb, mkv->segment);
2079  }
2080 
2081  mkv_free(mkv);
2082  return 0;
2083 }
2084 
2085 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
2086 {
2087  int i;
2088  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
2089  if (ff_mkv_codec_tags[i].id == codec_id)
2090  return 1;
2091 
2092  if (std_compliance < FF_COMPLIANCE_NORMAL) {
2093  enum AVMediaType type = avcodec_get_type(codec_id);
2094  // mkv theoretically supports any video/audio through VFW/ACM
2095  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
2096  return 1;
2097  }
2098 
2099  return 0;
2100 }
2101 
2103  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
2104  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
2105  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
2106  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
2107  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
2108  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
2109  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
2110  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
2111  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
2112  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
2113  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
2114  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
2115  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2116 };
2117 
2119  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
2120  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
2121  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
2122  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
2123  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
2124  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2125 };
2126 
2128  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
2129  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
2130  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2131 };
2132 
2133 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
2134 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
2135 static const AVOption options[] = {
2136  { "reserve_index_space", "Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).", OFFSET(reserve_cues_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
2137  { "cluster_size_limit", "Store at most the provided amount of bytes in a cluster. ", OFFSET(cluster_size_limit), AV_OPT_TYPE_INT , { .i64 = -1 }, -1, INT_MAX, FLAGS },
2138  { "cluster_time_limit", "Store at most the provided number of milliseconds in a cluster.", OFFSET(cluster_time_limit), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
2139  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
2140  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
2141  { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
2142  { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
2143  { NULL },
2144 };
2145 
2146 #if CONFIG_MATROSKA_MUXER
2147 static const AVClass matroska_class = {
2148  .class_name = "matroska muxer",
2149  .item_name = av_default_item_name,
2150  .option = options,
2151  .version = LIBAVUTIL_VERSION_INT,
2152 };
2153 
2154 AVOutputFormat ff_matroska_muxer = {
2155  .name = "matroska",
2156  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
2157  .mime_type = "video/x-matroska",
2158  .extensions = "mkv",
2159  .priv_data_size = sizeof(MatroskaMuxContext),
2160  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2162  .video_codec = CONFIG_LIBX264_ENCODER ?
2169  .codec_tag = (const AVCodecTag* const []){
2172  },
2173  .subtitle_codec = AV_CODEC_ID_ASS,
2174  .query_codec = mkv_query_codec,
2175  .priv_class = &matroska_class,
2176 };
2177 #endif
2178 
2179 #if CONFIG_WEBM_MUXER
2180 static const AVClass webm_class = {
2181  .class_name = "webm muxer",
2182  .item_name = av_default_item_name,
2183  .option = options,
2184  .version = LIBAVUTIL_VERSION_INT,
2185 };
2186 
2187 AVOutputFormat ff_webm_muxer = {
2188  .name = "webm",
2189  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
2190  .mime_type = "video/webm",
2191  .extensions = "webm",
2192  .priv_data_size = sizeof(MatroskaMuxContext),
2193  .audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
2194  .video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
2195  .subtitle_codec = AV_CODEC_ID_WEBVTT,
2201  .priv_class = &webm_class,
2202 };
2203 #endif
2204 
2205 #if CONFIG_MATROSKA_AUDIO_MUXER
2206 static const AVClass mka_class = {
2207  .class_name = "matroska audio muxer",
2208  .item_name = av_default_item_name,
2209  .option = options,
2210  .version = LIBAVUTIL_VERSION_INT,
2211 };
2212 AVOutputFormat ff_matroska_audio_muxer = {
2213  .name = "matroska",
2214  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
2215  .mime_type = "audio/x-matroska",
2216  .extensions = "mka",
2217  .priv_data_size = sizeof(MatroskaMuxContext),
2218  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2219  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
2220  .video_codec = AV_CODEC_ID_NONE,
2226  .codec_tag = (const AVCodecTag* const []){
2228  },
2229  .priv_class = &mka_class,
2230 };
2231 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1474
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)
Definition: matroskaenc.c:1792
Definition: lfg.h:25
#define MATROSKA_ID_SEEKPREROLL
Definition: matroska.h:95
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:341
internal header for HEVC (de)muxer utilities
#define AV_DISPOSITION_METADATA
Definition: avformat.h:826
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:411
#define NULL
Definition: coverity.c:32
static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1717
const char const char void * val
Definition: avisynth_c.h:634
#define MATROSKA_ID_BLOCKADDID
Definition: matroska.h:194
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:104
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:417
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:173
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int mkv_check_tag(AVDictionary *m)
Definition: matroskaenc.c:1224
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
Views are packed per line, as if interlaced.
Definition: stereo3d.h:97
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:71
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:58
#define MAX_TRACKS
Maximum number of tracks allowed in a Matroska file (with track numbers in range 1 to 126 (inclusive)...
Definition: matroskaenc.c:101
The optional first identifier line of a WebVTT cue.
Definition: avcodec.h:1354
uint32_t samples
Definition: wv.h:39
int initial
Definition: wv.h:43
void av_sha_update(AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:314
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:101
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:75
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:113
static void mkv_start_new_cluster(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1770
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1146
AVOption.
Definition: opt.h:255
hash context
Definition: sha.c:34
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:79
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
Views are alternated temporally.
Definition: stereo3d.h:66
enum AVCodecID id
Definition: mxfenc.c:101
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:156
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2773
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:89
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
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:4083
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:131
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:99
static const AVCodecTag additional_subtitle_tags[]
Definition: matroskaenc.c:2127
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: avcodec.h:1285
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:1292
uint64_t pts
Definition: matroskaenc.c:76
int size
Definition: avcodec.h:1424
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:210
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1698
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
#define MATROSKA_ID_BLOCKREFERENCE
Definition: matroska.h:201
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:1376
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
enum AVMediaType codec_type
Definition: rtp.c:37
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:177
int ff_flac_is_native_layout(uint64_t channel_layout)
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:81
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
int64_t cluster_time_limit
Definition: matroskaenc.c:125
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:132
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2763
int64_t segment_offset
Definition: matroskaenc.c:85
int version
Definition: avisynth_c.h:629
uint64_t_TMPL AV_RL64
Definition: bytestream.h:87
int avoid_negative_ts_use_pts
Definition: internal.h:115
const char * master
Definition: vf_curves.c:110
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:954
static AVPacket pkt
#define av_le2ne32(x)
Definition: bswap.h:96
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:160
Definition: matroskaenc.c:61
AVDictionary * metadata
Definition: avformat.h:1240
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:248
mkv_track * tracks
Definition: matroskaenc.c:116
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:481
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:223
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:188
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1134
#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:483
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:217
int64_t pos
absolute offset in the file where the master's elements start
Definition: matroskaenc.c:57
MatroskaVideoStereoModeType
Definition: matroska.h:248
int ff_vorbiscomment_write(uint8_t **p, AVDictionary **m, const char *vendor_string)
Write a VorbisComment into a buffer.
Definition: vorbiscomment.c:54
#define FLAGS
Definition: matroskaenc.c:2134
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:207
Format I/O context.
Definition: avformat.h:1273
UID uid
Definition: mxfenc.c:1804
char str[32]
Definition: internal.h:48
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:110
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static const AVCodecTag additional_audio_tags[]
Definition: matroskaenc.c:2102
Public dictionary API.
static int mkv_write_native_codecprivate(AVFormatContext *s, AVCodecContext *codec, AVIOContext *dyn_cp)
Definition: matroskaenc.c:624
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:103
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:319
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2270
uint8_t
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:220
#define av_malloc(s)
static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
Definition: matroskaenc.c:1199
mode
Definition: f_perms.c:27
AVOptions.
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:97
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:123
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:71
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:156
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:154
uint32_t flags
Definition: wv.h:40
int ff_mkv_stereo3d_conv(AVStream *st, MatroskaVideoStereoModeType stereo_mode)
Definition: matroska.c:151
uint64_t segmentpos
Definition: matroskaenc.c:63
int id
unique ID to identify the chapter
Definition: avformat.h:1237
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:196
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:222
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:958
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
#define MATROSKA_ID_BLOCKMORE
Definition: matroska.h:193
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:161
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:129
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1384
uint8_t * data
Definition: avcodec.h:1423
static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
Definition: matroskaenc.c:466
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:112
#define MATROSKA_ID_BLOCKADDITIONS
Definition: matroska.h:192
uint32_t tag
Definition: movenc.c:1334
#define WV_HEADER_SIZE
Definition: wavpack.h:30
enum AVCodecID id
Definition: internal.h:49
static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
Definition: matroskaenc.c:560
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
uint8_t * data
Definition: avcodec.h:1373
#define MATROSKA_ID_CUES
Definition: matroska.h:58
int64_t ts_offset
Definition: matroskaenc.c:93
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
static const uint8_t header[24]
Definition: sdr2.c:67
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2996
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
static int64_t duration
Definition: ffplay.c:326
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
static int get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:607
Definition: wv.h:34
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1401
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
#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:197
#define av_log(a,...)
int has_cue
Definition: matroskaenc.c:92
#define AV_DISPOSITION_CAPTIONS
To specify text track kind (different from subtitles default).
Definition: avformat.h:824
unsigned m
Definition: audioconvert.c:187
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:360
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:68
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1292
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
#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:140
ebml_master segment
Definition: matroskaenc.c:107
static int mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:1142
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:122
uint32_t chapter_id_offset
Definition: matroskaenc.c:130
AVPacket cur_audio_pkt
Definition: matroskaenc.c:118
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:1235
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3392
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1485
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:200
#define EBML_ID_EBMLREADVERSION
Definition: matroska.h:37
#define MAX_CUETRACKPOS_SIZE
per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4 8-byte uint max
Definition: matroskaenc.c:148
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
unsigned int elementid
Definition: matroskaenc.c:62
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:69
#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:523
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:209
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:1467
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
int initial_padding
Audio only.
Definition: avcodec.h:3294
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2823
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREOMODE_TYPE_NB]
Definition: matroska.c:127
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:1554
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1406
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:183
int write_dts
Definition: matroskaenc.c:91
int final
Definition: wv.h:43
AVChapter ** chapters
Definition: avformat.h:1475
enum AVCodecID id
Definition: matroska.h:273
enum AVPacketSideDataType type
Definition: avcodec.h:1375
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: utils.c:3838
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:1090
int side_data_elems
Definition: avcodec.h:1435
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
int64_t ff_vorbiscomment_length(AVDictionary *m, const char *vendor_string)
Calculate the length in bytes of a VorbisComment.
Definition: vorbiscomment.c:41
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:226
enum AVCodecID codec_id
Definition: mov_chan.c:433
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:35
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
uint32_t crc
Definition: wv.h:41
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:378
#define FFMAX(a, b)
Definition: common.h:79
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:339
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:67
#define fail()
Definition: checkasm.h:57
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2323
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:111
#define MATROSKA_ID_TAG
Definition: matroska.h:166
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:809
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
#define LIBAVFORMAT_IDENT
Definition: version.h:44
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:198
audio channel layout utility functions
#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:127
mkv_cuepoint * entries
Definition: matroskaenc.c:86
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:164
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:173
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:168
static void mkv_free(MatroskaMuxContext *mkv)
Free the members allocated in the mux context.
Definition: matroskaenc.c:314
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:227
static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv)
Write the seek head to the file and free it.
Definition: matroskaenc.c:388
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:4171
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:471
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition: hevc.c:1077
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:167
const char * name
Definition: avformat.h:513
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
int ff_flac_write_header(AVIOContext *pb, uint8_t *extradata, int extradata_size, int last_block)
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1576
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:215
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const 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
AVDictionary * metadata
Definition: avformat.h:916
Opaque data information usually sparse.
Definition: avutil.h:197
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:125
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
#define EBML_ID_VOID
Definition: matroska.h:45
#define OFFSET(x)
Definition: matroskaenc.c:2133
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:128
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:3035
#define av_log2
Definition: intmath.h:100
static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts, int64_t cluster_pos, int64_t relative_pos, int64_t duration)
Definition: matroskaenc.c:443
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:242
Stream structure.
Definition: avformat.h:842
#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:257
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1239
static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, int i, AVIOContext *pb, int default_stream_exists)
Definition: matroskaenc.c:832
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:797
sample_rate
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
#define AV_DISPOSITION_DESCRIPTIONS
Definition: avformat.h:825
AVS_Value src
Definition: avisynth_c.h:482
#define MATROSKA_ID_TRACKFLAGFORCED
Definition: matroska.h:100
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
enum AVMediaType codec_type
Definition: avcodec.h:1510
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
enum AVCodecID codec_id
Definition: avcodec.h:1519
#define MATROSKA_ID_SEEKID
Definition: matroska.h:184
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int sample_rate
samples per second
Definition: avcodec.h:2262
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:156
main external API structure.
Definition: avcodec.h:1502
#define MATROSKA_ID_BLOCK
Definition: matroska.h:199
#define MATROSKA_ID_INFO
Definition: matroska.h:56
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:176
static const EbmlSyntax ebml_header[]
Definition: matroskadec.c:327
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:170
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
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:553
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1349
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1618
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:96
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:181
Describe the class of an AVClass context structure.
Definition: log.h:67
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:214
#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:151
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:191
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:115
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:1065
rational number numerator/denominator
Definition: rational.h:43
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
Definition: matroskaenc.c:216
#define MATROSKA_ID_CUEDURATION
Definition: matroska.h:162
void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf, int ignore_extradata)
Definition: riffenc.c:205
#define MATROSKA_ID_CUETIME
Definition: matroska.h:155
Recommmends skipping the specified number of samples.
Definition: avcodec.h:1314
AVIOContext * dyn_bc
Definition: matroskaenc.c:106
AVMediaType
Definition: avutil.h:191
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
int64_t duration_offset
Definition: matroskaenc.c:112
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
#define snprintf
Definition: snprintf.h:34
#define MATROSKA_ID_TRACKVIDEO
Definition: matroska.h:81
static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
Definition: matroskaenc.c:1565
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:161
static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
Definition: matroskaenc.c:229
static int put_wv_codecpriv(AVIOContext *pb, AVCodecContext *codec)
Definition: matroskaenc.c:551
Views are on top of each other.
Definition: stereo3d.h:55
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:61
static int64_t pts
Global timestamp for the audio frames.
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:423
int64_t * stream_duration_offsets
Definition: matroskaenc.c:136
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:218
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
static int flags
Definition: cpu.c:47
#define MATROSKA_ID_FILENAME
Definition: matroska.h:208
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
#define MATROSKA_ID_BLOCKADDITIONAL
Definition: matroska.h:195
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:121
#define MATROSKA_ID_CODECID
Definition: matroska.h:88
static const AVCodecTag additional_video_tags[]
Definition: matroskaenc.c:2118
int64_t start
Definition: avformat.h:1239
Views are next to each other.
Definition: stereo3d.h:45
#define MATROSKA_ID_VIDEOALPHAMODE
Definition: matroska.h:123
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:1979
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
Main libavformat public API header.
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:159
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:185
int64_t last_track_timestamp[MAX_TRACKS]
Definition: matroskaenc.c:133
#define MATROSKA_ID_CODECDELAY
Definition: matroska.h:94
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:216
common internal api header.
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:249
rational numbers
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
Definition: matroskaenc.c:664
static double c[64]
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int keyframe)
Definition: matroskaenc.c:1618
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1955
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:905
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
static void put_ebml_void(AVIOContext *pb, uint64_t size)
Write a void element of a given size.
Definition: matroskaenc.c:269
static unsigned bit_depth(uint64_t mask)
Definition: af_astats.c:128
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1238
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:208
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
char * key
Definition: dict.h:87
#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
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition: avcodec.h:1360
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:60
ASS as defined in Matroska.
Definition: avcodec.h:532
#define EBML_ID_HEADER
Definition: matroska.h:33
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
ebml_master cluster
Definition: matroskaenc.c:109
#define av_free(p)
char * value
Definition: dict.h:88
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:152
mkv_seekhead_entry * entries
Definition: matroskaenc.c:71
int len
static int mkv_write_stereo_mode(AVFormatContext *s, AVIOContext *pb, AVStream *st, int mode, int *h_width, int *h_height)
Definition: matroskaenc.c:734
#define MATROSKA_ID_FILEUID
Definition: matroska.h:211
static int mkv_write_tag_targets(AVFormatContext *s, unsigned int elementid, unsigned int uid, ebml_master *tags, ebml_master *tag)
Definition: matroskaenc.c:1176
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:76
void * priv_data
Format private data.
Definition: avformat.h:1301
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:225
Views are packed per column.
Definition: stereo3d.h:107
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:80
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1755
static const AVOption options[]
Definition: matroskaenc.c:2135
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:327
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:219
#define av_freep(p)
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:169
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:72
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:324
#define MODE_MATROSKAv2
Definition: matroskaenc.c:96
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:109
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext...
Definition: hevc.c:1093
const CodecMime ff_mkv_image_mime_tags[]
Definition: matroska.c:102
#define MODE_WEBM
Definition: matroskaenc.c:97
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1877
#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:144
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:433
int stream_index
Definition: avcodec.h:1425
int num_entries
Definition: matroskaenc.c:87
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:884
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:295
int64_t segment_offset
Definition: matroskaenc.c:108
int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src)
Copy packet side data.
Definition: avpacket.c:223
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:206
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:286
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:114
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1400
int64_t * stream_durations
Definition: matroskaenc.c:135
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
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:252
uint32_t blocksize
Definition: wv.h:35
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
Definition: matroskaenc.c:2085
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:305
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:114
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:240
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:29
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define MATROSKA_ID_DISCARDPADDING
Definition: matroska.h:203
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:53