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"
45 #include "libavutil/mathematics.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/random_seed.h"
48 #include "libavutil/rational.h"
49 #include "libavutil/samplefmt.h"
50 #include "libavutil/sha.h"
51 #include "libavutil/stereo3d.h"
52 
53 #include "libavcodec/xiph.h"
54 #include "libavcodec/mpeg4audio.h"
55 #include "libavcodec/internal.h"
56 
57 typedef struct ebml_master {
58  int64_t pos; ///< absolute offset in the file where the master's elements start
59  int sizebytes; ///< how many bytes were reserved for the size
60 } ebml_master;
61 
62 typedef struct mkv_seekhead_entry {
63  unsigned int elementid;
64  uint64_t segmentpos;
66 
67 typedef struct mkv_seekhead {
68  int64_t filepos;
69  int64_t segment_offset; ///< the file offset to the beginning of the segment
70  int reserved_size; ///< -1 if appending to file
74 } mkv_seekhead;
75 
76 typedef struct mkv_cuepoint {
77  uint64_t pts;
79  int tracknum;
80  int64_t cluster_pos; ///< file offset of the cluster containing the block
81  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
82  int64_t duration; ///< duration of the block according to time base
83 } mkv_cuepoint;
84 
85 typedef struct mkv_cues {
86  int64_t segment_offset;
89 } mkv_cues;
90 
91 typedef struct mkv_track {
92  int write_dts;
93  int has_cue;
94  int64_t ts_offset;
95 } mkv_track;
96 
97 #define MODE_MATROSKAv2 0x01
98 #define MODE_WEBM 0x02
99 
100 /** Maximum number of tracks allowed in a Matroska file (with track numbers in
101  * range 1 to 126 (inclusive) */
102 #define MAX_TRACKS 126
103 
104 typedef struct MatroskaMuxContext {
105  const AVClass *class;
106  int mode;
109  int64_t segment_offset;
111  int64_t cluster_pos; ///< file offset of the current cluster
112  int64_t cluster_pts;
114  int64_t duration;
118 
120 
123 
126  int64_t cues_pos;
128  int is_dash;
130  int is_live;
131 
134 
136 
139 
142 
143 
144 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
145  * offset, 4 bytes for target EBML ID */
146 #define MAX_SEEKENTRY_SIZE 21
147 
148 /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4
149  * 8-byte uint max */
150 #define MAX_CUETRACKPOS_SIZE 42
151 
152 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
153 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
154 
155 /** Seek preroll value for opus */
156 #define OPUS_SEEK_PREROLL 80000000
157 
158 static int ebml_id_size(unsigned int id)
159 {
160  return (av_log2(id + 1) - 1) / 7 + 1;
161 }
162 
163 static void put_ebml_id(AVIOContext *pb, unsigned int id)
164 {
165  int i = ebml_id_size(id);
166  while (i--)
167  avio_w8(pb, (uint8_t)(id >> (i * 8)));
168 }
169 
170 /**
171  * Write an EBML size meaning "unknown size".
172  *
173  * @param bytes The number of bytes the size should occupy (maximum: 8).
174  */
175 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
176 {
177  av_assert0(bytes <= 8);
178  avio_w8(pb, 0x1ff >> bytes);
179  ffio_fill(pb, 0xff, bytes - 1);
180 }
181 
182 /**
183  * Calculate how many bytes are needed to represent a given number in EBML.
184  */
185 static int ebml_num_size(uint64_t num)
186 {
187  int bytes = 1;
188  while ((num + 1) >> bytes * 7)
189  bytes++;
190  return bytes;
191 }
192 
193 /**
194  * Write a number in EBML variable length format.
195  *
196  * @param bytes The number of bytes that need to be used to write the number.
197  * If zero, any number of bytes can be used.
198  */
199 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
200 {
201  int i, needed_bytes = ebml_num_size(num);
202 
203  // sizes larger than this are currently undefined in EBML
204  av_assert0(num < (1ULL << 56) - 1);
205 
206  if (bytes == 0)
207  // don't care how many bytes are used, so use the min
208  bytes = needed_bytes;
209  // the bytes needed to write the given size would exceed the bytes
210  // that we need to use, so write unknown size. This shouldn't happen.
211  av_assert0(bytes >= needed_bytes);
212 
213  num |= 1ULL << bytes * 7;
214  for (i = bytes - 1; i >= 0; i--)
215  avio_w8(pb, (uint8_t)(num >> i * 8));
216 }
217 
218 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
219 {
220  int i, bytes = 1;
221  uint64_t tmp = val;
222  while (tmp >>= 8)
223  bytes++;
224 
225  put_ebml_id(pb, elementid);
226  put_ebml_num(pb, bytes, 0);
227  for (i = bytes - 1; i >= 0; i--)
228  avio_w8(pb, (uint8_t)(val >> i * 8));
229 }
230 
231 static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
232 {
233  int i, bytes = 1;
234  uint64_t tmp = 2*(val < 0 ? val^-1 : val);
235 
236  while (tmp>>=8) bytes++;
237 
238  put_ebml_id(pb, elementid);
239  put_ebml_num(pb, bytes, 0);
240  for (i = bytes - 1; i >= 0; i--)
241  avio_w8(pb, (uint8_t)(val >> i * 8));
242 }
243 
244 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
245 {
246  put_ebml_id(pb, elementid);
247  put_ebml_num(pb, 8, 0);
248  avio_wb64(pb, av_double2int(val));
249 }
250 
251 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
252  const void *buf, int size)
253 {
254  put_ebml_id(pb, elementid);
255  put_ebml_num(pb, size, 0);
256  avio_write(pb, buf, size);
257 }
258 
259 static void put_ebml_string(AVIOContext *pb, unsigned int elementid,
260  const char *str)
261 {
262  put_ebml_binary(pb, elementid, str, strlen(str));
263 }
264 
265 /**
266  * Write a void element of a given size. Useful for reserving space in
267  * the file to be written to later.
268  *
269  * @param size The number of bytes to reserve, which must be at least 2.
270  */
271 static void put_ebml_void(AVIOContext *pb, uint64_t size)
272 {
273  int64_t currentpos = avio_tell(pb);
274 
275  av_assert0(size >= 2);
276 
278  // we need to subtract the length needed to store the size from the
279  // size we need to reserve so 2 cases, we use 8 bytes to store the
280  // size if possible, 1 byte otherwise
281  if (size < 10)
282  put_ebml_num(pb, size - 1, 0);
283  else
284  put_ebml_num(pb, size - 9, 8);
285  ffio_fill(pb, 0, currentpos + size - avio_tell(pb));
286 }
287 
288 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid,
289  uint64_t expectedsize)
290 {
291  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
292  put_ebml_id(pb, elementid);
293  put_ebml_size_unknown(pb, bytes);
294  return (ebml_master) {avio_tell(pb), bytes };
295 }
296 
298 {
299  int64_t pos = avio_tell(pb);
300 
301  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
302  return;
303  put_ebml_num(pb, pos - master.pos, master.sizebytes);
304  avio_seek(pb, pos, SEEK_SET);
305 }
306 
307 static void put_xiph_size(AVIOContext *pb, int size)
308 {
309  ffio_fill(pb, 255, size / 255);
310  avio_w8(pb, size % 255);
311 }
312 
313 /**
314  * Free the members allocated in the mux context.
315  */
316 static void mkv_free(MatroskaMuxContext *mkv) {
317  if (mkv->main_seekhead) {
319  av_freep(&mkv->main_seekhead);
320  }
321  if (mkv->cues) {
322  av_freep(&mkv->cues->entries);
323  av_freep(&mkv->cues);
324  }
325  av_freep(&mkv->tracks);
326  av_freep(&mkv->stream_durations);
328 }
329 
330 /**
331  * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
332  * elements. If a maximum number of elements is specified, enough space
333  * will be reserved at the current file location to write a seek head of
334  * that size.
335  *
336  * @param segment_offset The absolute offset to the position in the file
337  * where the segment begins.
338  * @param numelements The maximum number of elements that will be indexed
339  * by this seek head, 0 if unlimited.
340  */
341 static mkv_seekhead *mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset,
342  int numelements)
343 {
344  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
345  if (!new_seekhead)
346  return NULL;
347 
348  new_seekhead->segment_offset = segment_offset;
349 
350  if (numelements > 0) {
351  new_seekhead->filepos = avio_tell(pb);
352  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
353  // and size, and 3 bytes to guarantee that an EBML void element
354  // will fit afterwards
355  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
356  new_seekhead->max_entries = numelements;
357  put_ebml_void(pb, new_seekhead->reserved_size);
358  }
359  return new_seekhead;
360 }
361 
362 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
363 {
364  mkv_seekhead_entry *entries = seekhead->entries;
365 
366  // don't store more elements than we reserved space for
367  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
368  return -1;
369 
370  entries = av_realloc_array(entries, seekhead->num_entries + 1, sizeof(mkv_seekhead_entry));
371  if (!entries)
372  return AVERROR(ENOMEM);
373  seekhead->entries = entries;
374 
375  seekhead->entries[seekhead->num_entries].elementid = elementid;
376  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
377 
378  return 0;
379 }
380 
381 /**
382  * Write the seek head to the file and free it. If a maximum number of
383  * elements was specified to mkv_start_seekhead(), the seek head will
384  * be written at the location reserved for it. Otherwise, it is written
385  * at the current location in the file.
386  *
387  * @return The file offset where the seekhead was written,
388  * -1 if an error occurred.
389  */
391 {
392  mkv_seekhead *seekhead = mkv->main_seekhead;
393  ebml_master metaseek, seekentry;
394  int64_t currentpos;
395  int i;
396 
397  currentpos = avio_tell(pb);
398 
399  if (seekhead->reserved_size > 0) {
400  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
401  currentpos = -1;
402  goto fail;
403  }
404  }
405 
406  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
407  for (i = 0; i < seekhead->num_entries; i++) {
408  mkv_seekhead_entry *entry = &seekhead->entries[i];
409 
411 
413  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
414  put_ebml_id(pb, entry->elementid);
415 
417  end_ebml_master(pb, seekentry);
418  }
419  end_ebml_master(pb, metaseek);
420 
421  if (seekhead->reserved_size > 0) {
422  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
423  put_ebml_void(pb, remaining);
424  avio_seek(pb, currentpos, SEEK_SET);
425 
426  currentpos = seekhead->filepos;
427  }
428 fail:
430  av_freep(&mkv->main_seekhead);
431 
432  return currentpos;
433 }
434 
435 static mkv_cues *mkv_start_cues(int64_t segment_offset)
436 {
437  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
438  if (!cues)
439  return NULL;
440 
441  cues->segment_offset = segment_offset;
442  return cues;
443 }
444 
445 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts,
446  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
447 {
448  mkv_cuepoint *entries = cues->entries;
449 
450  if (ts < 0)
451  return 0;
452 
453  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
454  if (!entries)
455  return AVERROR(ENOMEM);
456  cues->entries = entries;
457 
458  cues->entries[cues->num_entries].pts = ts;
459  cues->entries[cues->num_entries].stream_idx = stream;
460  cues->entries[cues->num_entries].tracknum = tracknum;
461  cues->entries[cues->num_entries].cluster_pos = cluster_pos - cues->segment_offset;
462  cues->entries[cues->num_entries].relative_pos = relative_pos;
463  cues->entries[cues->num_entries++].duration = duration;
464 
465  return 0;
466 }
467 
468 static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
469 {
470  AVIOContext *pb = s->pb;
471  ebml_master cues_element;
472  int64_t currentpos;
473  int i, j;
474 
475  currentpos = avio_tell(pb);
476  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
477 
478  for (i = 0; i < cues->num_entries; i++) {
479  ebml_master cuepoint, track_positions;
480  mkv_cuepoint *entry = &cues->entries[i];
481  uint64_t pts = entry->pts;
482  int ctp_nb = 0;
483 
484  // Calculate the number of entries, so we know the element size
485  for (j = 0; j < num_tracks; j++)
486  tracks[j].has_cue = 0;
487  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
488  int tracknum = entry[j].stream_idx;
489  av_assert0(tracknum>=0 && tracknum<num_tracks);
490  if (tracks[tracknum].has_cue && s->streams[tracknum]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
491  continue;
492  tracks[tracknum].has_cue = 1;
493  ctp_nb ++;
494  }
495 
498 
499  // put all the entries from different tracks that have the exact same
500  // timestamp into the same CuePoint
501  for (j = 0; j < num_tracks; j++)
502  tracks[j].has_cue = 0;
503  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
504  int tracknum = entry[j].stream_idx;
505  av_assert0(tracknum>=0 && tracknum<num_tracks);
506  if (tracks[tracknum].has_cue && s->streams[tracknum]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
507  continue;
508  tracks[tracknum].has_cue = 1;
510  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
511  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos);
512  put_ebml_uint(pb, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos);
513  if (entry[j].duration != -1)
515  end_ebml_master(pb, track_positions);
516  }
517  i += j - 1;
518  end_ebml_master(pb, cuepoint);
519  }
520  end_ebml_master(pb, cues_element);
521 
522  return currentpos;
523 }
524 
526 {
527  const uint8_t *header_start[3];
528  int header_len[3];
529  int first_header_size;
530  int j;
531 
532  if (par->codec_id == AV_CODEC_ID_VORBIS)
533  first_header_size = 30;
534  else
535  first_header_size = 42;
536 
538  first_header_size, header_start, header_len) < 0) {
539  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
540  return -1;
541  }
542 
543  avio_w8(pb, 2); // number packets - 1
544  for (j = 0; j < 2; j++) {
545  put_xiph_size(pb, header_len[j]);
546  }
547  for (j = 0; j < 3; j++)
548  avio_write(pb, header_start[j], header_len[j]);
549 
550  return 0;
551 }
552 
554 {
555  if (par->extradata && par->extradata_size == 2)
556  avio_write(pb, par->extradata, 2);
557  else
558  avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs
559  return 0;
560 }
561 
563  AVIOContext *pb, AVCodecParameters *par)
564 {
565  int write_comment = (par->channel_layout &&
566  !(par->channel_layout & ~0x3ffffULL) &&
568  int ret = ff_flac_write_header(pb, par->extradata, par->extradata_size,
569  !write_comment);
570 
571  if (ret < 0)
572  return ret;
573 
574  if (write_comment) {
575  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
576  "Lavf" : LIBAVFORMAT_IDENT;
577  AVDictionary *dict = NULL;
578  uint8_t buf[32], *data, *p;
579  int64_t len;
580 
581  snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
582  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
583 
584  len = ff_vorbiscomment_length(dict, vendor);
585  if (len >= ((1<<24) - 4))
586  return AVERROR(EINVAL);
587 
588  data = av_malloc(len + 4);
589  if (!data) {
590  av_dict_free(&dict);
591  return AVERROR(ENOMEM);
592  }
593 
594  data[0] = 0x84;
595  AV_WB24(data + 1, len);
596 
597  p = data + 4;
598  ff_vorbiscomment_write(&p, &dict, vendor);
599 
600  avio_write(pb, data, len + 4);
601 
602  av_freep(&data);
603  av_dict_free(&dict);
604  }
605 
606  return 0;
607 }
608 
610  int *sample_rate, int *output_sample_rate)
611 {
612  MPEG4AudioConfig mp4ac;
613 
614  if (avpriv_mpeg4audio_get_config(&mp4ac, par->extradata,
615  par->extradata_size * 8, 1) < 0) {
616  av_log(s, AV_LOG_ERROR,
617  "Error parsing AAC extradata, unable to determine samplerate.\n");
618  return AVERROR(EINVAL);
619  }
620 
621  *sample_rate = mp4ac.sample_rate;
622  *output_sample_rate = mp4ac.ext_sample_rate;
623  return 0;
624 }
625 
627  AVCodecParameters *par,
628  AVIOContext *dyn_cp)
629 {
630  switch (par->codec_id) {
631  case AV_CODEC_ID_VORBIS:
632  case AV_CODEC_ID_THEORA:
633  return put_xiph_codecpriv(s, dyn_cp, par);
634  case AV_CODEC_ID_FLAC:
635  return put_flac_codecpriv(s, dyn_cp, par);
636  case AV_CODEC_ID_WAVPACK:
637  return put_wv_codecpriv(dyn_cp, par);
638  case AV_CODEC_ID_H264:
639  return ff_isom_write_avcc(dyn_cp, par->extradata,
640  par->extradata_size);
641  case AV_CODEC_ID_HEVC:
642  ff_isom_write_hvcc(dyn_cp, par->extradata,
643  par->extradata_size, 0);
644  return 0;
645  case AV_CODEC_ID_ALAC:
646  if (par->extradata_size < 36) {
647  av_log(s, AV_LOG_ERROR,
648  "Invalid extradata found, ALAC expects a 36-byte "
649  "QuickTime atom.");
650  return AVERROR_INVALIDDATA;
651  } else
652  avio_write(dyn_cp, par->extradata + 12,
653  par->extradata_size - 12);
654  break;
655  default:
656  if (par->codec_id == AV_CODEC_ID_PRORES &&
658  avio_wl32(dyn_cp, par->codec_tag);
659  } else if (par->extradata_size && par->codec_id != AV_CODEC_ID_TTA)
660  avio_write(dyn_cp, par->extradata, par->extradata_size);
661  }
662 
663  return 0;
664 }
665 
667  AVCodecParameters *par,
668  int native_id, int qt_id)
669 {
670  AVIOContext *dyn_cp;
671  uint8_t *codecpriv;
672  int ret, codecpriv_size;
673 
674  ret = avio_open_dyn_buf(&dyn_cp);
675  if (ret < 0)
676  return ret;
677 
678  if (native_id) {
679  ret = mkv_write_native_codecprivate(s, par, dyn_cp);
680  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
681  if (qt_id) {
682  if (!par->codec_tag)
684  par->codec_id);
685  if (par->extradata_size) {
688  ) {
689  int i;
690  avio_wb32(dyn_cp, 0x5a + par->extradata_size);
691  avio_wl32(dyn_cp, par->codec_tag);
692  for(i = 0; i < 0x5a - 8; i++)
693  avio_w8(dyn_cp, 0);
694  }
695  avio_write(dyn_cp, par->extradata, par->extradata_size);
696  }
697  } else {
699  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
700  avcodec_get_name(par->codec_id));
701 
702  if (!par->codec_tag)
704  par->codec_id);
705  if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {
706  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
707  avcodec_get_name(par->codec_id));
708  ret = AVERROR(EINVAL);
709  }
710 
711  ff_put_bmp_header(dyn_cp, par, ff_codec_bmp_tags, 0, 0);
712  }
713  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
714  unsigned int tag;
716  if (!tag) {
717  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
718  avcodec_get_name(par->codec_id));
719  ret = AVERROR(EINVAL);
720  }
721  if (!par->codec_tag)
722  par->codec_tag = tag;
723 
725  }
726 
727  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
728  if (codecpriv_size)
730  codecpriv_size);
731  av_free(codecpriv);
732  return ret;
733 }
734 
736  int side_data_size = 0;
737  const uint8_t *side_data = av_stream_get_side_data(
738  st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, &side_data_size);
740 
741  if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
742  par->color_trc < AVCOL_TRC_NB) {
744  par->color_trc);
745  }
746  if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
747  par->color_space < AVCOL_SPC_NB) {
749  }
751  par->color_primaries < AVCOL_PRI_NB) {
753  }
754  if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
755  par->color_range < AVCOL_RANGE_NB) {
757  }
758  if (side_data_size == sizeof(AVMasteringDisplayMetadata)) {
759  ebml_master meta_element = start_ebml_master(
761  const AVMasteringDisplayMetadata *metadata =
762  (const AVMasteringDisplayMetadata*)side_data;
763  if (metadata->has_primaries) {
765  av_q2d(metadata->display_primaries[0][0]));
767  av_q2d(metadata->display_primaries[0][1]));
769  av_q2d(metadata->display_primaries[1][0]));
771  av_q2d(metadata->display_primaries[1][1]));
773  av_q2d(metadata->display_primaries[2][0]));
775  av_q2d(metadata->display_primaries[2][1]));
777  av_q2d(metadata->white_point[0]));
779  av_q2d(metadata->white_point[1]));
780  }
781  if (metadata->has_luminance) {
783  av_q2d(metadata->max_luminance));
785  av_q2d(metadata->min_luminance));
786  }
787  end_ebml_master(pb, meta_element);
788  }
789  end_ebml_master(pb, colorinfo);
790  return 0;
791 }
792 
794  enum AVFieldOrder field_order)
795 {
796  switch (field_order) {
797  case AV_FIELD_UNKNOWN:
800  break;
804  break;
805  case AV_FIELD_TT:
806  case AV_FIELD_BB:
807  case AV_FIELD_TB:
808  case AV_FIELD_BT:
811  switch (field_order) {
812  case AV_FIELD_TT:
815  break;
816  case AV_FIELD_BB:
819  break;
820  case AV_FIELD_TB:
823  break;
824  case AV_FIELD_BT:
827  break;
828  }
829  }
830 }
831 
833  AVStream *st, int mode, int *h_width, int *h_height)
834 {
835  int i;
836  int ret = 0;
839 
840  *h_width = 1;
841  *h_height = 1;
842  // convert metadata into proper side data and add it to the stream
843  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
844  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
845  int stereo_mode = atoi(tag->value);
846 
847  for (i=0; i<MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
848  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
849  stereo_mode = i;
850  break;
851  }
852 
853  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
854  stereo_mode != 10 && stereo_mode != 12) {
855  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
856  if (ret < 0)
857  return ret;
858  }
859  }
860 
861  // iterate to find the stereo3d side data
862  for (i = 0; i < st->nb_side_data; i++) {
863  AVPacketSideData sd = st->side_data[i];
864  if (sd.type == AV_PKT_DATA_STEREO3D) {
865  AVStereo3D *stereo = (AVStereo3D *)sd.data;
866 
867  switch (stereo->type) {
868  case AV_STEREO3D_2D:
870  break;
872  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
875  *h_width = 2;
876  break;
879  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
880  format--;
881  *h_height = 2;
882  break;
885  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
886  format--;
887  break;
888  case AV_STEREO3D_LINES:
890  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
891  format--;
892  *h_height = 2;
893  break;
894  case AV_STEREO3D_COLUMNS:
896  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
897  format--;
898  *h_width = 2;
899  break;
902  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
903  format++;
904  break;
905  }
906  break;
907  }
908  }
909 
910  if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
911  return ret;
912 
913  // if webm, do not write unsupported modes
914  if ((mode == MODE_WEBM &&
917  || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
918  av_log(s, AV_LOG_ERROR,
919  "The specified stereo mode is not valid.\n");
921  return AVERROR(EINVAL);
922  }
923 
924  // write StereoMode if format is valid
926 
927  return ret;
928 }
929 
931  int i, AVIOContext *pb, int default_stream_exists)
932 {
933  AVStream *st = s->streams[i];
934  AVCodecParameters *par = st->codecpar;
935  ebml_master subinfo, track;
936  int native_id = 0;
937  int qt_id = 0;
939  int sample_rate = par->sample_rate;
940  int output_sample_rate = 0;
941  int display_width_div = 1;
942  int display_height_div = 1;
943  int j, ret;
945 
946  if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
947  mkv->have_attachments = 1;
948  return 0;
949  }
950 
951  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
952  if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
953  if (par->bits_per_raw_sample)
954  bit_depth = par->bits_per_raw_sample;
955  else
956  bit_depth = av_get_bytes_per_sample(par->format) << 3;
957  }
958  if (!bit_depth)
959  bit_depth = par->bits_per_coded_sample;
960  }
961 
962  if (par->codec_id == AV_CODEC_ID_AAC) {
963  ret = get_aac_sample_rates(s, par, &sample_rate, &output_sample_rate);
964  if (ret < 0)
965  return ret;
966  }
967 
970  mkv->is_dash ? mkv->dash_track_number : i + 1);
972  mkv->is_dash ? mkv->dash_track_number : i + 1);
973  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
974 
975  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
977  tag = av_dict_get(st->metadata, "language", NULL, 0);
978  if (mkv->mode != MODE_WEBM || par->codec_id != AV_CODEC_ID_WEBVTT) {
979  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag && tag->value ? tag->value:"und");
980  } else if (tag && tag->value) {
982  }
983 
984  // The default value for TRACKFLAGDEFAULT is 1, so add element
985  // if we need to clear it.
986  if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT))
988 
991 
992  if (mkv->mode == MODE_WEBM && par->codec_id == AV_CODEC_ID_WEBVTT) {
993  const char *codec_id;
995  codec_id = "D_WEBVTT/CAPTIONS";
996  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
997  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
998  codec_id = "D_WEBVTT/DESCRIPTIONS";
999  native_id = MATROSKA_TRACK_TYPE_METADATA;
1000  } else if (st->disposition & AV_DISPOSITION_METADATA) {
1001  codec_id = "D_WEBVTT/METADATA";
1002  native_id = MATROSKA_TRACK_TYPE_METADATA;
1003  } else {
1004  codec_id = "D_WEBVTT/SUBTITLES";
1005  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1006  }
1007  put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
1008  } else {
1009  // look for a codec ID string specific to mkv to use,
1010  // if none are found, use AVI codes
1011  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1012  if (ff_mkv_codec_tags[j].id == par->codec_id) {
1014  native_id = 1;
1015  break;
1016  }
1017  }
1018  if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->codec_tag) {
1019  if (mkv->allow_raw_vfw) {
1020  native_id = 0;
1021  } else {
1022  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1023  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1024  return AVERROR(EINVAL);
1025  }
1026  }
1027  }
1028 
1029  if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->initial_padding && par->codec_id == AV_CODEC_ID_OPUS) {
1030  int64_t codecdelay = av_rescale_q(par->initial_padding,
1031  (AVRational){ 1, 48000 },
1032  (AVRational){ 1, 1000000000 });
1033  if (codecdelay < 0) {
1034  av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1035  return AVERROR(EINVAL);
1036  }
1037 // mkv->tracks[i].ts_offset = av_rescale_q(par->initial_padding,
1038 // (AVRational){ 1, par->sample_rate },
1039 // st->time_base);
1040 
1041  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1042  }
1043  if (par->codec_id == AV_CODEC_ID_OPUS) {
1045  }
1046 
1047  if (mkv->mode == MODE_WEBM && !(par->codec_id == AV_CODEC_ID_VP8 ||
1048  par->codec_id == AV_CODEC_ID_VP9 ||
1049  par->codec_id == AV_CODEC_ID_OPUS ||
1050  par->codec_id == AV_CODEC_ID_VORBIS ||
1051  par->codec_id == AV_CODEC_ID_WEBVTT)) {
1053  "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1054  return AVERROR(EINVAL);
1055  }
1056 
1057  switch (par->codec_type) {
1058  case AVMEDIA_TYPE_VIDEO:
1059  mkv->have_video = 1;
1061 
1062  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
1063  && av_cmp_q(av_inv_q(st->avg_frame_rate), st->time_base) > 0)
1064  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->avg_frame_rate.den / st->avg_frame_rate.num);
1065  else
1066  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->time_base.num / st->time_base.den);
1067 
1068  if (!native_id &&
1069  ff_codec_get_tag(ff_codec_movvideo_tags, par->codec_id) &&
1070  ((!ff_codec_get_tag(ff_codec_bmp_tags, par->codec_id) && par->codec_id != AV_CODEC_ID_RAWVIDEO) ||
1071  par->codec_id == AV_CODEC_ID_SVQ1 ||
1072  par->codec_id == AV_CODEC_ID_SVQ3 ||
1073  par->codec_id == AV_CODEC_ID_CINEPAK))
1074  qt_id = 1;
1075 
1076  if (qt_id)
1077  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1078  else if (!native_id) {
1079  // if there is no mkv-specific codec ID, use VFW mode
1080  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1081  mkv->tracks[i].write_dts = 1;
1082  s->internal->avoid_negative_ts_use_pts = 0;
1083  }
1084 
1085  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
1086 
1087  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , par->width);
1088  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height);
1089 
1090  if (mkv->mode != MODE_WEBM)
1091  mkv_write_field_order(pb, par->field_order);
1092 
1093  // check both side data and metadata for stereo information,
1094  // write the result to the bitstream if any is found
1095  ret = mkv_write_stereo_mode(s, pb, st, mkv->mode,
1096  &display_width_div,
1097  &display_height_div);
1098  if (ret < 0)
1099  return ret;
1100 
1101  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1102  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1103  (par->format == AV_PIX_FMT_YUVA420P)) {
1105  }
1106 
1107  // write DisplayWidth and DisplayHeight, they contain the size of
1108  // a single source view and/or the display aspect ratio
1109  if (st->sample_aspect_ratio.num) {
1110  int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1111  if (d_width > INT_MAX) {
1112  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1113  return AVERROR(EINVAL);
1114  }
1115  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
1116  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div);
1117  } else if (display_width_div != 1 || display_height_div != 1) {
1118  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , par->width / display_width_div);
1119  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div);
1120  }
1121 
1122  if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1123  uint32_t color_space = av_le2ne32(par->codec_tag);
1124  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
1125  }
1126  if (s->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
1127  mkv_write_video_color(pb, par, st);
1128  }
1129  end_ebml_master(pb, subinfo);
1130  break;
1131 
1132  case AVMEDIA_TYPE_AUDIO:
1134 
1135  if (!native_id)
1136  // no mkv-specific ID, use ACM mode
1137  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
1138 
1139  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
1140  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , par->channels);
1142  if (output_sample_rate)
1143  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
1144  if (bit_depth)
1146  end_ebml_master(pb, subinfo);
1147  break;
1148 
1149  case AVMEDIA_TYPE_SUBTITLE:
1150  if (!native_id) {
1151  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
1152  return AVERROR(ENOSYS);
1153  }
1154 
1155  if (mkv->mode != MODE_WEBM || par->codec_id != AV_CODEC_ID_WEBVTT)
1156  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1157 
1158  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1159  break;
1160  default:
1161  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1162  return AVERROR(EINVAL);
1163  }
1164 
1165  if (mkv->mode != MODE_WEBM || par->codec_id != AV_CODEC_ID_WEBVTT) {
1166  ret = mkv_write_codecprivate(s, pb, par, native_id, qt_id);
1167  if (ret < 0)
1168  return ret;
1169  }
1170 
1171  end_ebml_master(pb, track);
1172 
1173  return 0;
1174 }
1175 
1177 {
1178  MatroskaMuxContext *mkv = s->priv_data;
1179  AVIOContext *pb = s->pb;
1180  ebml_master tracks;
1181  int i, ret, default_stream_exists = 0;
1182 
1184  if (ret < 0)
1185  return ret;
1186 
1187  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
1188  for (i = 0; i < s->nb_streams; i++) {
1189  AVStream *st = s->streams[i];
1190  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
1191  }
1192  for (i = 0; i < s->nb_streams; i++) {
1193  ret = mkv_write_track(s, mkv, i, pb, default_stream_exists);
1194  if (ret < 0)
1195  return ret;
1196  }
1197  end_ebml_master(pb, tracks);
1198  return 0;
1199 }
1200 
1202 {
1203  MatroskaMuxContext *mkv = s->priv_data;
1204  AVIOContext *pb = s->pb;
1205  ebml_master chapters, editionentry;
1206  AVRational scale = {1, 1E9};
1207  int i, ret;
1208 
1209  if (!s->nb_chapters || mkv->wrote_chapters)
1210  return 0;
1211 
1213  if (ret < 0) return ret;
1214 
1215  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
1216  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
1219  for (i = 0; i < s->nb_chapters; i++) {
1220  ebml_master chapteratom, chapterdisplay;
1221  AVChapter *c = s->chapters[i];
1222  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
1223  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
1224  AVDictionaryEntry *t = NULL;
1225  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
1226  av_log(s, AV_LOG_ERROR,
1227  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
1228  chapterstart, chapterend);
1229  return AVERROR_INVALIDDATA;
1230  }
1231 
1232  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
1234  put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
1235  put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
1238  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1239  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
1242  end_ebml_master(pb, chapterdisplay);
1243  }
1244  end_ebml_master(pb, chapteratom);
1245  }
1246  end_ebml_master(pb, editionentry);
1247  end_ebml_master(pb, chapters);
1248 
1249  mkv->wrote_chapters = 1;
1250  return 0;
1251 }
1252 
1254 {
1255  uint8_t *key = av_strdup(t->key);
1256  uint8_t *p = key;
1257  const uint8_t *lang = NULL;
1258  ebml_master tag;
1259 
1260  if (!key)
1261  return AVERROR(ENOMEM);
1262 
1263  if ((p = strrchr(p, '-')) &&
1264  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1265  *p = 0;
1266 
1267  p = key;
1268  while (*p) {
1269  if (*p == ' ')
1270  *p = '_';
1271  else if (*p >= 'a' && *p <= 'z')
1272  *p -= 'a' - 'A';
1273  p++;
1274  }
1275 
1278  if (lang)
1281  end_ebml_master(pb, tag);
1282 
1283  av_freep(&key);
1284  return 0;
1285 }
1286 
1288  unsigned int elementid, unsigned int uid,
1289  ebml_master *tags, ebml_master* tag)
1290 {
1291  MatroskaMuxContext *mkv = s->priv_data;
1292  ebml_master targets;
1293  int ret;
1294 
1295  if (!tags->pos) {
1297  if (ret < 0) return ret;
1298 
1299  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
1300  }
1301 
1302  *tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
1303  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
1304  if (elementid)
1305  put_ebml_uint(s->pb, elementid, uid);
1306  end_ebml_master(s->pb, targets);
1307  return 0;
1308 }
1309 
1310 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
1311  unsigned int uid, ebml_master *tags)
1312 {
1313  ebml_master tag;
1314  int ret;
1315  AVDictionaryEntry *t = NULL;
1316 
1317  ret = mkv_write_tag_targets(s, elementid, uid, tags, &tag);
1318  if (ret < 0)
1319  return ret;
1320 
1321  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
1322  if (av_strcasecmp(t->key, "title") &&
1323  av_strcasecmp(t->key, "stereo_mode") &&
1324  av_strcasecmp(t->key, "creation_time") &&
1325  av_strcasecmp(t->key, "encoding_tool") &&
1326  (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
1327  av_strcasecmp(t->key, "language"))) {
1328  ret = mkv_write_simpletag(s->pb, t);
1329  if (ret < 0)
1330  return ret;
1331  }
1332  }
1333 
1334  end_ebml_master(s->pb, tag);
1335  return 0;
1336 }
1337 
1339 {
1340  AVDictionaryEntry *t = NULL;
1341 
1342  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1343  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
1344  return 1;
1345 
1346  return 0;
1347 }
1348 
1350 {
1351  MatroskaMuxContext *mkv = s->priv_data;
1352  ebml_master tags = {0};
1353  int i, ret;
1354 
1356 
1357  if (mkv_check_tag(s->metadata)) {
1358  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
1359  if (ret < 0) return ret;
1360  }
1361 
1362  for (i = 0; i < s->nb_streams; i++) {
1363  AVStream *st = s->streams[i];
1364 
1365  if (!mkv_check_tag(st->metadata))
1366  continue;
1367 
1368  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
1369  if (ret < 0) return ret;
1370  }
1371 
1372  if (!mkv->is_live) {
1373  for (i = 0; i < s->nb_streams; i++) {
1374  ebml_master tag_target;
1375  ebml_master tag;
1376 
1377  mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags, &tag_target);
1378 
1380  put_ebml_string(s->pb, MATROSKA_ID_TAGNAME, "DURATION");
1381  mkv->stream_duration_offsets[i] = avio_tell(s->pb);
1382 
1383  // Reserve space to write duration as a 20-byte string.
1384  // 2 (ebml id) + 1 (data size) + 20 (data)
1385  put_ebml_void(s->pb, 23);
1386  end_ebml_master(s->pb, tag);
1387  end_ebml_master(s->pb, tag_target);
1388  }
1389  }
1390 
1391  for (i = 0; i < s->nb_chapters; i++) {
1392  AVChapter *ch = s->chapters[i];
1393 
1394  if (!mkv_check_tag(ch->metadata))
1395  continue;
1396 
1398  if (ret < 0) return ret;
1399  }
1400 
1401  if (tags.pos)
1402  end_ebml_master(s->pb, tags);
1403  return 0;
1404 }
1405 
1407 {
1408  MatroskaMuxContext *mkv = s->priv_data;
1409  AVIOContext *pb = s->pb;
1410  ebml_master attachments;
1411  AVLFG c;
1412  int i, ret;
1413 
1414  if (!mkv->have_attachments)
1415  return 0;
1416 
1418 
1420  if (ret < 0) return ret;
1421 
1422  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
1423 
1424  for (i = 0; i < s->nb_streams; i++) {
1425  AVStream *st = s->streams[i];
1426  ebml_master attached_file;
1427  AVDictionaryEntry *t;
1428  const char *mimetype = NULL;
1429  uint64_t fileuid;
1430 
1432  continue;
1433 
1434  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
1435 
1436  if (t = av_dict_get(st->metadata, "title", NULL, 0))
1438  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
1439  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
1440  return AVERROR(EINVAL);
1441  }
1443  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
1444  mimetype = t->value;
1445  else if (st->codecpar->codec_id != AV_CODEC_ID_NONE ) {
1446  int i;
1447  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1448  if (ff_mkv_mime_tags[i].id == st->codecpar->codec_id) {
1449  mimetype = ff_mkv_mime_tags[i].str;
1450  break;
1451  }
1452  for (i = 0; ff_mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1453  if (ff_mkv_image_mime_tags[i].id == st->codecpar->codec_id) {
1454  mimetype = ff_mkv_image_mime_tags[i].str;
1455  break;
1456  }
1457  }
1458  if (!mimetype) {
1459  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1460  "it cannot be deduced from the codec id.\n", i);
1461  return AVERROR(EINVAL);
1462  }
1463 
1464  if (s->flags & AVFMT_FLAG_BITEXACT) {
1465  struct AVSHA *sha = av_sha_alloc();
1466  uint8_t digest[20];
1467  if (!sha)
1468  return AVERROR(ENOMEM);
1469  av_sha_init(sha, 160);
1471  av_sha_final(sha, digest);
1472  av_free(sha);
1473  fileuid = AV_RL64(digest);
1474  } else {
1475  fileuid = av_lfg_get(&c);
1476  }
1477  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
1478  fileuid, i);
1479 
1482  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
1483  end_ebml_master(pb, attached_file);
1484  }
1485  end_ebml_master(pb, attachments);
1486 
1487  return 0;
1488 }
1489 
1491 {
1492  MatroskaMuxContext *mkv = s->priv_data;
1493  AVIOContext *pb = s->pb;
1494  ebml_master ebml_header, segment_info;
1496  int ret, i, version = 2;
1497  int64_t creation_time;
1498 
1499  if (!strcmp(s->oformat->name, "webm"))
1500  mkv->mode = MODE_WEBM;
1501  else
1502  mkv->mode = MODE_MATROSKAv2;
1503 
1504  if (mkv->mode != MODE_WEBM ||
1505  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
1506  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
1507  version = 4;
1508 
1509  for (i = 0; i < s->nb_streams; i++) {
1510  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
1516  av_log(s, AV_LOG_ERROR,
1517  "The Matroska muxer does not yet support muxing %s\n",
1519  return AVERROR_PATCHWELCOME;
1520  }
1521  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
1522  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
1523  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
1524  version = 4;
1525  }
1526 
1527  mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks));
1528  if (!mkv->tracks) {
1529  ret = AVERROR(ENOMEM);
1530  goto fail;
1531  }
1532  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1538  put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , version);
1540  end_ebml_master(pb, ebml_header);
1541 
1543  mkv->segment_offset = avio_tell(pb);
1544 
1545  // we write 2 seek heads - one at the end of the file to point to each
1546  // cluster, and one at the beginning to point to all other level one
1547  // elements (including the seek head at the end of the file), which
1548  // isn't more than 10 elements if we only write one of each other
1549  // currently defined level 1 element
1550  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1551  if (!mkv->main_seekhead) {
1552  ret = AVERROR(ENOMEM);
1553  goto fail;
1554  }
1555 
1557  if (ret < 0) goto fail;
1558 
1559  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
1561  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1563  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
1564  uint32_t segment_uid[4];
1565  AVLFG lfg;
1566 
1568 
1569  for (i = 0; i < 4; i++)
1570  segment_uid[i] = av_lfg_get(&lfg);
1571 
1573  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
1575  else
1577  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1578  } else {
1579  const char *ident = "Lavf";
1582  }
1583 
1584  if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
1585  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1586  int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
1587  uint8_t date_utc_buf[8];
1588  AV_WB64(date_utc_buf, date_utc);
1589  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1590  }
1591 
1592  // reserve space for the duration
1593  mkv->duration = 0;
1594  mkv->duration_offset = avio_tell(pb);
1595  if (!mkv->is_live) {
1596  put_ebml_void(pb, 11); // assumes double-precision float to be written
1597  }
1598  end_ebml_master(pb, segment_info);
1599 
1600  // initialize stream_duration fields
1601  mkv->stream_durations = av_mallocz(s->nb_streams * sizeof(int64_t));
1602  mkv->stream_duration_offsets = av_mallocz(s->nb_streams * sizeof(int64_t));
1603 
1604  ret = mkv_write_tracks(s);
1605  if (ret < 0)
1606  goto fail;
1607 
1608  for (i = 0; i < s->nb_chapters; i++)
1609  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1610 
1611  if (mkv->mode != MODE_WEBM) {
1612  ret = mkv_write_chapters(s);
1613  if (ret < 0)
1614  goto fail;
1615 
1616  ret = mkv_write_tags(s);
1617  if (ret < 0)
1618  goto fail;
1619 
1620  ret = mkv_write_attachments(s);
1621  if (ret < 0)
1622  goto fail;
1623  }
1624 
1625  if (!s->pb->seekable && !mkv->is_live)
1626  mkv_write_seekhead(pb, mkv);
1627 
1628  mkv->cues = mkv_start_cues(mkv->segment_offset);
1629  if (!mkv->cues) {
1630  ret = AVERROR(ENOMEM);
1631  goto fail;
1632  }
1633  if (pb->seekable && mkv->reserve_cues_space) {
1634  mkv->cues_pos = avio_tell(pb);
1636  }
1637 
1639  mkv->cur_audio_pkt.size = 0;
1640  mkv->cluster_pos = -1;
1641 
1642  avio_flush(pb);
1643 
1644  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1645  // after 4k and on a keyframe
1646  if (pb->seekable) {
1647  if (mkv->cluster_time_limit < 0)
1648  mkv->cluster_time_limit = 5000;
1649  if (mkv->cluster_size_limit < 0)
1650  mkv->cluster_size_limit = 5 * 1024 * 1024;
1651  } else {
1652  if (mkv->cluster_time_limit < 0)
1653  mkv->cluster_time_limit = 1000;
1654  if (mkv->cluster_size_limit < 0)
1655  mkv->cluster_size_limit = 32 * 1024;
1656  }
1657 
1658  return 0;
1659 fail:
1660  mkv_free(mkv);
1661  return ret;
1662 }
1663 
1664 static int mkv_blockgroup_size(int pkt_size)
1665 {
1666  int size = pkt_size + 4;
1667  size += ebml_num_size(size);
1668  size += 2; // EBML ID for block and block duration
1669  size += 8; // max size of block duration
1670  size += ebml_num_size(size);
1671  size += 1; // blockgroup EBML ID
1672  return size;
1673 }
1674 
1675 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1676 {
1677  uint8_t *dst;
1678  int srclen = *size;
1679  int offset = 0;
1680  int ret;
1681 
1682  dst = av_malloc(srclen);
1683  if (!dst)
1684  return AVERROR(ENOMEM);
1685 
1686  while (srclen >= WV_HEADER_SIZE) {
1687  WvHeader header;
1688 
1689  ret = ff_wv_parse_header(&header, src);
1690  if (ret < 0)
1691  goto fail;
1692  src += WV_HEADER_SIZE;
1693  srclen -= WV_HEADER_SIZE;
1694 
1695  if (srclen < header.blocksize) {
1696  ret = AVERROR_INVALIDDATA;
1697  goto fail;
1698  }
1699 
1700  if (header.initial) {
1701  AV_WL32(dst + offset, header.samples);
1702  offset += 4;
1703  }
1704  AV_WL32(dst + offset, header.flags);
1705  AV_WL32(dst + offset + 4, header.crc);
1706  offset += 8;
1707 
1708  if (!(header.initial && header.final)) {
1709  AV_WL32(dst + offset, header.blocksize);
1710  offset += 4;
1711  }
1712 
1713  memcpy(dst + offset, src, header.blocksize);
1714  src += header.blocksize;
1715  srclen -= header.blocksize;
1716  offset += header.blocksize;
1717  }
1718 
1719  *pdst = dst;
1720  *size = offset;
1721 
1722  return 0;
1723 fail:
1724  av_freep(&dst);
1725  return ret;
1726 }
1727 
1729  unsigned int blockid, AVPacket *pkt, int keyframe)
1730 {
1731  MatroskaMuxContext *mkv = s->priv_data;
1733  uint8_t *data = NULL, *side_data = NULL;
1734  int offset = 0, size = pkt->size, side_data_size = 0;
1735  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1736  uint64_t additional_id = 0;
1737  int64_t discard_padding = 0;
1738  uint8_t track_number = (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1));
1739  ebml_master block_group, block_additions, block_more;
1740 
1741  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1742  "pts %" PRId64 ", dts %" PRId64 ", duration %" PRId64 ", keyframe %d\n",
1743  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration,
1744  keyframe != 0);
1745  if (par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 &&
1746  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
1747  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1748  else if (par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6 &&
1749  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
1750  /* extradata is Annex B, assume the bitstream is too and convert it */
1751  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
1752  else if (par->codec_id == AV_CODEC_ID_WAVPACK) {
1753  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1754  if (ret < 0) {
1755  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1756  return;
1757  }
1758  } else
1759  data = pkt->data;
1760 
1761  if (par->codec_id == AV_CODEC_ID_PRORES && size >= 8) {
1762  /* Matroska specification requires to remove the first QuickTime atom
1763  */
1764  size -= 8;
1765  offset = 8;
1766  }
1767 
1768  side_data = av_packet_get_side_data(pkt,
1770  &side_data_size);
1771 
1772  if (side_data && side_data_size >= 10) {
1773  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
1774  (AVRational){1, par->sample_rate},
1775  (AVRational){1, 1000000000});
1776  }
1777 
1778  side_data = av_packet_get_side_data(pkt,
1780  &side_data_size);
1781  if (side_data) {
1782  additional_id = AV_RB64(side_data);
1783  side_data += 8;
1784  side_data_size -= 8;
1785  }
1786 
1787  if ((side_data_size && additional_id == 1) || discard_padding) {
1788  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
1789  blockid = MATROSKA_ID_BLOCK;
1790  }
1791 
1792  put_ebml_id(pb, blockid);
1793  put_ebml_num(pb, size + 4, 0);
1794  // this assumes stream_index is less than 126
1795  avio_w8(pb, 0x80 | track_number);
1796  avio_wb16(pb, ts - mkv->cluster_pts);
1797  avio_w8(pb, (blockid == MATROSKA_ID_SIMPLEBLOCK && keyframe) ? (1 << 7) : 0);
1798  avio_write(pb, data + offset, size);
1799  if (data != pkt->data)
1800  av_free(data);
1801 
1802  if (blockid == MATROSKA_ID_BLOCK && !keyframe) {
1804  mkv->last_track_timestamp[track_number - 1]);
1805  }
1806  mkv->last_track_timestamp[track_number - 1] = ts - mkv->cluster_pts;
1807 
1808  if (discard_padding) {
1809  put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
1810  }
1811 
1812  if (side_data_size && additional_id == 1) {
1813  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
1814  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
1817  put_ebml_num(pb, side_data_size, 0);
1818  avio_write(pb, side_data, side_data_size);
1819  end_ebml_master(pb, block_more);
1820  end_ebml_master(pb, block_additions);
1821  }
1822  if ((side_data_size && additional_id == 1) || discard_padding) {
1823  end_ebml_master(pb, block_group);
1824  }
1825 }
1826 
1828 {
1829  MatroskaMuxContext *mkv = s->priv_data;
1830  ebml_master blockgroup;
1831  int id_size, settings_size, size;
1832  uint8_t *id, *settings;
1833  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1834  const int flags = 0;
1835 
1836  id_size = 0;
1838  &id_size);
1839 
1840  settings_size = 0;
1842  &settings_size);
1843 
1844  size = id_size + 1 + settings_size + 1 + pkt->size;
1845 
1846  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1847  "pts %" PRId64 ", dts %" PRId64 ", duration %" PRId64 ", flags %d\n",
1848  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
1849 
1851 
1853  put_ebml_num(pb, size + 4, 0);
1854  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1855  avio_wb16(pb, ts - mkv->cluster_pts);
1856  avio_w8(pb, flags);
1857  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
1858 
1860  end_ebml_master(pb, blockgroup);
1861 
1862  return pkt->duration;
1863 }
1864 
1866 {
1867  MatroskaMuxContext *mkv = s->priv_data;
1868  int bufsize;
1869  uint8_t *dyn_buf;
1870 
1871  if (!mkv->dyn_bc)
1872  return;
1873 
1874  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1875  avio_write(s->pb, dyn_buf, bufsize);
1876  av_free(dyn_buf);
1877  mkv->dyn_bc = NULL;
1878 }
1879 
1881 {
1882  MatroskaMuxContext *mkv = s->priv_data;
1883  AVIOContext *pb;
1884 
1885  if (s->pb->seekable) {
1886  pb = s->pb;
1887  } else {
1888  pb = mkv->dyn_bc;
1889  }
1890 
1891  av_log(s, AV_LOG_DEBUG,
1892  "Starting new cluster at offset %" PRIu64 " bytes, "
1893  "pts %" PRIu64 "dts %" PRIu64 "\n",
1894  avio_tell(pb), pkt->pts, pkt->dts);
1895  end_ebml_master(pb, mkv->cluster);
1896  mkv->cluster_pos = -1;
1897  if (mkv->dyn_bc)
1898  mkv_flush_dynbuf(s);
1899  avio_flush(s->pb);
1900 }
1901 
1903 {
1904  MatroskaMuxContext *mkv = s->priv_data;
1905  AVIOContext *pb = s->pb;
1907  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1908  int duration = pkt->duration;
1909  int ret;
1910  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1911  int64_t relative_packet_pos;
1912  int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;
1913 
1914  if (ts == AV_NOPTS_VALUE) {
1915  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1916  return AVERROR(EINVAL);
1917  }
1918  ts += mkv->tracks[pkt->stream_index].ts_offset;
1919 
1920  if (mkv->cluster_pos != -1) {
1921  int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;
1922  if ((int16_t)cluster_time != cluster_time) {
1923  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
1924  mkv_start_new_cluster(s, pkt);
1925  }
1926  }
1927 
1928  if (!s->pb->seekable) {
1929  if (!mkv->dyn_bc) {
1930  ret = avio_open_dyn_buf(&mkv->dyn_bc);
1931  if (ret < 0) {
1932  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1933  return ret;
1934  }
1935  }
1936  pb = mkv->dyn_bc;
1937  }
1938 
1939  if (mkv->cluster_pos == -1) {
1940  mkv->cluster_pos = avio_tell(s->pb);
1943  mkv->cluster_pts = FFMAX(0, ts);
1944  }
1945 
1946  relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos;
1947 
1948  if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1949  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);
1950  if (s->pb->seekable && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {
1951  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);
1952  if (ret < 0) return ret;
1953  }
1954  } else {
1955  if (par->codec_id == AV_CODEC_ID_WEBVTT) {
1956  duration = mkv_write_vtt_blocks(s, pb, pkt);
1957  } else {
1959  mkv_blockgroup_size(pkt->size));
1960 
1961 #if FF_API_CONVERGENCE_DURATION
1963  /* For backward compatibility, prefer convergence_duration. */
1964  if (pkt->convergence_duration > 0) {
1965  duration = pkt->convergence_duration;
1966  }
1968 #endif
1969  /* All subtitle blocks are considered to be keyframes. */
1970  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 1);
1972  end_ebml_master(pb, blockgroup);
1973  }
1974 
1975  if (s->pb->seekable) {
1976  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
1977  mkv->cluster_pos, relative_packet_pos, duration);
1978  if (ret < 0)
1979  return ret;
1980  }
1981  }
1982 
1983  mkv->duration = FFMAX(mkv->duration, ts + duration);
1984 
1985  if (mkv->stream_durations)
1986  mkv->stream_durations[pkt->stream_index] =
1987  FFMAX(mkv->stream_durations[pkt->stream_index], ts + duration);
1988 
1989  return 0;
1990 }
1991 
1993 {
1994  MatroskaMuxContext *mkv = s->priv_data;
1996  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1997  int cluster_size;
1998  int64_t cluster_time;
1999  int ret;
2000  int start_new_cluster;
2001 
2002  if (mkv->tracks[pkt->stream_index].write_dts)
2003  cluster_time = pkt->dts - mkv->cluster_pts;
2004  else
2005  cluster_time = pkt->pts - mkv->cluster_pts;
2006  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
2007 
2008  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2009  // after 4k and on a keyframe
2010  if (s->pb->seekable) {
2011  cluster_size = avio_tell(s->pb) - mkv->cluster_pos;
2012  } else {
2013  cluster_size = avio_tell(mkv->dyn_bc);
2014  }
2015 
2016  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
2017  // WebM DASH specification states that the first block of every cluster
2018  // has to be a key frame. So for DASH video, we only create a cluster
2019  // on seeing key frames.
2020  start_new_cluster = keyframe;
2021  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
2022  (mkv->cluster_pos == -1 ||
2023  cluster_time > mkv->cluster_time_limit)) {
2024  // For DASH audio, we create a Cluster based on cluster_time_limit
2025  start_new_cluster = 1;
2026  } else if (!mkv->is_dash &&
2027  (cluster_size > mkv->cluster_size_limit ||
2028  cluster_time > mkv->cluster_time_limit ||
2029  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
2030  cluster_size > 4 * 1024))) {
2031  start_new_cluster = 1;
2032  } else {
2033  start_new_cluster = 0;
2034  }
2035 
2036  if (mkv->cluster_pos != -1 && start_new_cluster) {
2037  mkv_start_new_cluster(s, pkt);
2038  }
2039 
2040  if (!mkv->cluster_pos)
2041  avio_write_marker(s->pb,
2044 
2045  // check if we have an audio packet cached
2046  if (mkv->cur_audio_pkt.size > 0) {
2047  // for DASH audio, a CuePoint has to be added when there is a new cluster.
2049  mkv->is_dash ? start_new_cluster : 0);
2051  if (ret < 0) {
2052  av_log(s, AV_LOG_ERROR,
2053  "Could not write cached audio packet ret:%d\n", ret);
2054  return ret;
2055  }
2056  }
2057 
2058  // buffer an audio packet to ensure the packet containing the video
2059  // keyframe's timecode is contained in the same cluster for WebM
2060  if (codec_type == AVMEDIA_TYPE_AUDIO) {
2061  ret = av_packet_ref(&mkv->cur_audio_pkt, pkt);
2062  } else
2063  ret = mkv_write_packet_internal(s, pkt, 0);
2064  return ret;
2065 }
2066 
2068 {
2069  MatroskaMuxContext *mkv = s->priv_data;
2070  AVIOContext *pb;
2071  if (s->pb->seekable)
2072  pb = s->pb;
2073  else
2074  pb = mkv->dyn_bc;
2075  if (!pkt) {
2076  if (mkv->cluster_pos != -1) {
2077  av_log(s, AV_LOG_DEBUG,
2078  "Flushing cluster at offset %" PRIu64 " bytes\n",
2079  avio_tell(pb));
2080  end_ebml_master(pb, mkv->cluster);
2081  mkv->cluster_pos = -1;
2082  if (mkv->dyn_bc)
2083  mkv_flush_dynbuf(s);
2084  avio_flush(s->pb);
2085  }
2086  return 1;
2087  }
2088  return mkv_write_packet(s, pkt);
2089 }
2090 
2092 {
2093  MatroskaMuxContext *mkv = s->priv_data;
2094  AVIOContext *pb = s->pb;
2095  int64_t currentpos, cuespos;
2096  int ret;
2097 
2098  // check if we have an audio packet cached
2099  if (mkv->cur_audio_pkt.size > 0) {
2100  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
2102  if (ret < 0) {
2103  av_log(s, AV_LOG_ERROR,
2104  "Could not write cached audio packet ret:%d\n", ret);
2105  return ret;
2106  }
2107  }
2108 
2109  if (mkv->dyn_bc) {
2110  end_ebml_master(mkv->dyn_bc, mkv->cluster);
2111  mkv_flush_dynbuf(s);
2112  } else if (mkv->cluster_pos != -1) {
2113  end_ebml_master(pb, mkv->cluster);
2114  }
2115 
2116  if (mkv->mode != MODE_WEBM) {
2117  ret = mkv_write_chapters(s);
2118  if (ret < 0)
2119  return ret;
2120  }
2121 
2122  if (pb->seekable) {
2123  if (mkv->cues->num_entries) {
2124  if (mkv->reserve_cues_space) {
2125  int64_t cues_end;
2126 
2127  currentpos = avio_tell(pb);
2128  avio_seek(pb, mkv->cues_pos, SEEK_SET);
2129 
2130  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
2131  cues_end = avio_tell(pb);
2132  if (cues_end > cuespos + mkv->reserve_cues_space) {
2133  av_log(s, AV_LOG_ERROR,
2134  "Insufficient space reserved for cues: %d "
2135  "(needed: %" PRId64 ").\n",
2136  mkv->reserve_cues_space, cues_end - cuespos);
2137  return AVERROR(EINVAL);
2138  }
2139 
2140  if (cues_end < cuespos + mkv->reserve_cues_space)
2142  (cues_end - cuespos));
2143 
2144  avio_seek(pb, currentpos, SEEK_SET);
2145  } else {
2146  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
2147  }
2148 
2150  cuespos);
2151  if (ret < 0)
2152  return ret;
2153  }
2154 
2155  mkv_write_seekhead(pb, mkv);
2156 
2157  // update the duration
2158  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
2159  currentpos = avio_tell(pb);
2160  avio_seek(pb, mkv->duration_offset, SEEK_SET);
2162 
2163  // update stream durations
2164  if (mkv->stream_durations) {
2165  int i;
2166  for (i = 0; i < s->nb_streams; ++i) {
2167  AVStream *st = s->streams[i];
2168  double duration_sec = mkv->stream_durations[i] * av_q2d(st->time_base);
2169  char duration_string[20] = "";
2170 
2171  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
2172  mkv->stream_durations[i]);
2173 
2174  if (!mkv->is_live && mkv->stream_duration_offsets[i] > 0) {
2175  avio_seek(pb, mkv->stream_duration_offsets[i], SEEK_SET);
2176 
2177  snprintf(duration_string, 20, "%02d:%02d:%012.9f",
2178  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
2179  fmod(duration_sec, 60));
2180 
2181  put_ebml_binary(pb, MATROSKA_ID_TAGSTRING, duration_string, 20);
2182  }
2183  }
2184  }
2185 
2186  avio_seek(pb, currentpos, SEEK_SET);
2187  }
2188 
2189  if (!mkv->is_live) {
2190  end_ebml_master(pb, mkv->segment);
2191  }
2192 
2193  mkv_free(mkv);
2194  return 0;
2195 }
2196 
2197 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
2198 {
2199  int i;
2200  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
2201  if (ff_mkv_codec_tags[i].id == codec_id)
2202  return 1;
2203 
2204  if (std_compliance < FF_COMPLIANCE_NORMAL) {
2205  enum AVMediaType type = avcodec_get_type(codec_id);
2206  // mkv theoretically supports any video/audio through VFW/ACM
2207  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
2208  return 1;
2209  }
2210 
2211  return 0;
2212 }
2213 
2214 static int mkv_init(struct AVFormatContext *s)
2215 {
2216  int i;
2217 
2218  if (s->avoid_negative_ts < 0) {
2219  s->avoid_negative_ts = 1;
2221  }
2222 
2223  for (i = 0; i < s->nb_streams; i++) {
2224  // ms precision is the de-facto standard timescale for mkv files
2225  avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
2226  }
2227 
2228  return 0;
2229 }
2230 
2231 static int mkv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
2232 {
2233  int ret = 1;
2234  AVStream *st = s->streams[pkt->stream_index];
2235 
2236  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
2237  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
2238  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
2239  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
2240  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
2241  }
2242 
2243  return ret;
2244 }
2245 
2247  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
2248  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
2249  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
2250  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
2251  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
2252  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
2253  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
2254  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
2255  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
2256  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
2257  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
2258  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
2259  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2260 };
2261 
2263  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
2264  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
2265  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
2266  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
2267  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
2268  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2269 };
2270 
2272  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
2273  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
2274  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2275 };
2276 
2277 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
2278 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
2279 static const AVOption options[] = {
2280  { "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 },
2281  { "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 },
2282  { "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 },
2283  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
2284  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
2285  { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
2286  { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
2287  { NULL },
2288 };
2289 
2290 #if CONFIG_MATROSKA_MUXER
2291 static const AVClass matroska_class = {
2292  .class_name = "matroska muxer",
2293  .item_name = av_default_item_name,
2294  .option = options,
2295  .version = LIBAVUTIL_VERSION_INT,
2296 };
2297 
2298 AVOutputFormat ff_matroska_muxer = {
2299  .name = "matroska",
2300  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
2301  .mime_type = "video/x-matroska",
2302  .extensions = "mkv",
2303  .priv_data_size = sizeof(MatroskaMuxContext),
2304  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2306  .video_codec = CONFIG_LIBX264_ENCODER ?
2308  .init = mkv_init,
2314  .codec_tag = (const AVCodecTag* const []){
2317  },
2318  .subtitle_codec = AV_CODEC_ID_ASS,
2319  .query_codec = mkv_query_codec,
2320  .check_bitstream = mkv_check_bitstream,
2321  .priv_class = &matroska_class,
2322 };
2323 #endif
2324 
2325 #if CONFIG_WEBM_MUXER
2326 static const AVClass webm_class = {
2327  .class_name = "webm muxer",
2328  .item_name = av_default_item_name,
2329  .option = options,
2330  .version = LIBAVUTIL_VERSION_INT,
2331 };
2332 
2333 AVOutputFormat ff_webm_muxer = {
2334  .name = "webm",
2335  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
2336  .mime_type = "video/webm",
2337  .extensions = "webm",
2338  .priv_data_size = sizeof(MatroskaMuxContext),
2339  .audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
2340  .video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
2341  .subtitle_codec = AV_CODEC_ID_WEBVTT,
2342  .init = mkv_init,
2346  .check_bitstream = mkv_check_bitstream,
2349  .priv_class = &webm_class,
2350 };
2351 #endif
2352 
2353 #if CONFIG_MATROSKA_AUDIO_MUXER
2354 static const AVClass mka_class = {
2355  .class_name = "matroska audio muxer",
2356  .item_name = av_default_item_name,
2357  .option = options,
2358  .version = LIBAVUTIL_VERSION_INT,
2359 };
2360 AVOutputFormat ff_matroska_audio_muxer = {
2361  .name = "matroska",
2362  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
2363  .mime_type = "audio/x-matroska",
2364  .extensions = "mka",
2365  .priv_data_size = sizeof(MatroskaMuxContext),
2366  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2367  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
2368  .video_codec = AV_CODEC_ID_NONE,
2369  .init = mkv_init,
2373  .check_bitstream = mkv_check_bitstream,
2376  .codec_tag = (const AVCodecTag* const []){
2378  },
2379  .priv_class = &mka_class,
2380 };
2381 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1528
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)
Definition: matroskaenc.c:1902
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:860
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:440
#define NULL
Definition: coverity.c:32
static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1827
const char const char void * val
Definition: avisynth_c.h:634
#define MATROSKA_ID_BLOCKADDID
Definition: matroska.h:223
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:104
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:446
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:175
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
enum AVColorTransferCharacteristic color_trc
Definition: avcodec.h:4010
#define MATROSKA_ID_VIDEOFLAGINTERLACED
Definition: matroska.h:121
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define MATROSKA_ID_VIDEOCOLOR_GX
Definition: matroska.h:147
enum AVCodecID codec_id
Definition: ffmpeg_vaapi.c:149
static int mkv_check_tag(AVDictionary *m)
Definition: matroskaenc.c:1338
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:71
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:59
#define MAX_TRACKS
Maximum number of tracks allowed in a Matroska file (with track numbers in range 1 to 126 (inclusive)...
Definition: matroskaenc.c:102
The optional first identifier line of a WebVTT cue.
Definition: avcodec.h:1492
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:1880
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1272
AVOption.
Definition: opt.h:245
hash context
Definition: sha.c:34
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:80
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
static int mkv_init(struct AVFormatContext *s)
Definition: matroskaenc.c:2214
enum AVCodecID id
Definition: mxfenc.c:104
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:185
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2946
#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:70
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:4427
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:160
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:99
static const AVCodecTag additional_subtitle_tags[]
Definition: matroskaenc.c:2271
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: avcodec.h:1410
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:1406
uint64_t pts
Definition: matroskaenc.c:77
static int mkv_write_video_color(AVIOContext *pb, AVCodecParameters *par, AVStream *st)
Definition: matroskaenc.c:735
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3922
int size
Definition: avcodec.h:1581
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:239
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1752
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
#define MATROSKA_ID_BLOCKREFERENCE
Definition: matroska.h:230
int av_log2(unsigned v)
Definition: intmath.c:26
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:1490
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
enum AVMediaType codec_type
Definition: rtp.c:37
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:206
int ff_flac_is_native_layout(uint64_t channel_layout)
uint8_t * av_stream_get_side_data(AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:4939
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:82
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_VIDEOCOLOR_RX
Definition: matroska.h:145
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
int64_t cluster_time_limit
Definition: matroskaenc.c:127
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:161
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2936
int64_t segment_offset
Definition: matroskaenc.c:86
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:121
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:983
static AVPacket pkt
#define av_le2ne32(x)
Definition: bswap.h:96
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:189
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX
Definition: matroska.h:153
Definition: matroskaenc.c:62
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_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
AVDictionary * metadata
Definition: avformat.h:1286
mkv_track * tracks
Definition: matroskaenc.c:117
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:493
Views are next to each other.
Definition: stereo3d.h:45
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:253
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:217
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1260
#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:494
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3914
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:246
enum AVColorSpace color_space
Definition: avcodec.h:4011
int64_t pos
absolute offset in the file where the master's elements start
Definition: matroskaenc.c:58
MatroskaVideoStereoModeType
Definition: matroska.h:293
Mastering display metadata (based on SMPTE-2086:2014).
Definition: avcodec.h:1518
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:2278
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:236
Format I/O context.
Definition: avformat.h:1325
UID uid
Definition: mxfenc.c:1820
char str[32]
Definition: internal.h:50
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:111
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:2246
Public dictionary API.
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:346
uint8_t
#define MATROSKA_ID_VIDEOCOLOR_BX
Definition: matroska.h:149
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:249
#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:1310
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:72
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:158
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:156
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:64
int id
unique ID to identify the chapter
Definition: avformat.h:1283
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1598
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int native_id, int qt_id)
Definition: matroskaenc.c:666
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:225
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:252
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:987
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:470
#define MATROSKA_ID_BLOCKMORE
Definition: matroska.h:222
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:1393
int64_t duration
Definition: movenc.c:63
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:190
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:120
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:158
#define MATROSKA_ID_VIDEOCOLOR
Definition: matroska.h:127
int initial_padding
Audio only.
Definition: avcodec.h:4051
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:1436
uint8_t * data
Definition: avcodec.h:1580
static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
Definition: matroskaenc.c:468
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:112
#define MATROSKA_ID_BLOCKADDITIONS
Definition: matroska.h:221
uint32_t tag
Definition: movenc.c:1367
Not part of ABI.
Definition: pixfmt.h:458
#define WV_HEADER_SIZE
Definition: wavpack.h:30
enum AVCodecID id
Definition: internal.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
uint8_t * data
Definition: avcodec.h:1524
#define MATROSKA_ID_CUES
Definition: matroska.h:58
int64_t ts_offset
Definition: matroskaenc.c:94
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:511
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
static const uint8_t header[24]
Definition: sdr2.c:67
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
#define MATROSKA_ID_VIDEOCOLOR_WHITEY
Definition: matroska.h:152
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
Definition: wv.h:34
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1453
Views are alternated temporally.
Definition: stereo3d.h:66
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:72
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4024
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number in EBML variable length format.
Definition: matroskaenc.c:199
#define av_log(a,...)
int has_cue
Definition: matroskaenc.c:93
#define AV_DISPOSITION_CAPTIONS
To specify text track kind (different from subtitles default).
Definition: avformat.h:858
unsigned m
Definition: audioconvert.c:187
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:362
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:69
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1344
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:572
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2870
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1612
static int get_aac_sample_rates(AVFormatContext *s, AVCodecParameters *par, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:609
#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:142
ebml_master segment
Definition: matroskaenc.c:108
static int mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:1253
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:123
uint32_t chapter_id_offset
Definition: matroskaenc.c:132
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails...
Definition: utils.c:5113
AVPacket cur_audio_pkt
Definition: matroskaenc.c:119
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:189
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:1349
#define MATROSKA_ID_VIDEOCOLOR_BY
Definition: matroska.h:150
#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:3446
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1539
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:229
#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:150
#define MATROSKA_ID_VIDEOCOLOR_WHITEX
Definition: matroska.h:151
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
unsigned int elementid
Definition: matroskaenc.c:63
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:70
#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
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
enum AVColorPrimaries color_primaries
Definition: avcodec.h:4009
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:238
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
Not part of ABI.
Definition: pixfmt.h:402
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:202
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:1664
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3918
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:185
int write_dts
Definition: matroskaenc.c:92
int final
Definition: wv.h:43
AVChapter ** chapters
Definition: avformat.h:1529
enum AVCodecID id
Definition: matroska.h:318
enum AVPacketSideDataType type
Definition: avcodec.h:1526
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:2985
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:1201
#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:256
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
uint32_t crc
Definition: wv.h:41
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, const AVCodecTag *tags, int for_asf, int ignore_extradata)
Definition: riffenc.c:207
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:432
#define FFMAX(a, b)
Definition: common.h:94
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:341
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
int64_t filepos
Definition: matroskaenc.c:68
#define fail()
Definition: checkasm.h:81
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1586
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3940
static void mkv_write_field_order(AVIOContext *pb, enum AVFieldOrder field_order)
Definition: matroskaenc.c:793
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:111
#define MATROSKA_ID_TAG
Definition: matroska.h:195
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:841
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
#define LIBAVFORMAT_IDENT
Definition: version.h:46
Views are packed per line, as if interlaced.
Definition: stereo3d.h:97
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:243
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:224
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
Definition: matroska.h:154
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:129
mkv_cuepoint * entries
Definition: matroskaenc.c:87
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:190
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:202
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:197
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
static int put_wv_codecpriv(AVIOContext *pb, AVCodecParameters *par)
Definition: matroskaenc.c:553
static void mkv_free(MatroskaMuxContext *mkv)
Free the members allocated in the mux context.
Definition: matroskaenc.c:316
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:257
static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv)
Write the seek head to the file and free it.
Definition: matroskaenc.c:390
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:483
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:196
const char * name
Definition: avformat.h:522
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#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:1630
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:244
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:945
enum AVColorRange color_range
Video only.
Definition: avcodec.h:4008
Opaque data information usually sparse.
Definition: avutil.h:197
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:126
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
#define EBML_ID_VOID
Definition: matroska.h:45
#define src
Definition: vp9dsp.c:530
#define OFFSET(x)
Definition: matroskaenc.c:2277
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:157
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:3082
Views are packed per column.
Definition: stereo3d.h:107
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:445
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:244
Stream structure.
Definition: avformat.h:876
#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:259
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1285
static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, int i, AVIOContext *pb, int default_stream_exists)
Definition: matroskaenc.c:930
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:829
sample_rate
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
#define MATROSKA_ID_VIDEOCOLORMATRIXCOEFF
Definition: matroska.h:129
#define AV_DISPOSITION_DESCRIPTIONS
Definition: avformat.h:859
#define MATROSKA_ID_TRACKFLAGFORCED
Definition: matroska.h:100
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
#define MATROSKA_ID_VIDEOCOLORPRIMARIES
Definition: matroska.h:140
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
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
#define MATROSKA_ID_SEEKID
Definition: matroska.h:213
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
#define MATROSKA_ID_BLOCK
Definition: matroska.h:228
#define MATROSKA_ID_INFO
Definition: matroska.h:56
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:563
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:205
static const EbmlSyntax ebml_header[]
Definition: matroskadec.c:367
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:199
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:1487
GLint GLenum type
Definition: opengl_enc.c:105
#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:210
static const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:67
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:243
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2869
#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:153
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:220
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:115
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:1176
rational number numerator/denominator
Definition: rational.h:43
Copyright (c) 2016 Neil Birkbeck neil.birkbeck@gmail.com
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
Definition: matroskaenc.c:218
#define MATROSKA_ID_CUEDURATION
Definition: matroska.h:191
#define MATROSKA_ID_CUETIME
Definition: matroska.h:184
Not part of ABI.
Definition: pixfmt.h:428
AVFieldOrder
Definition: avcodec.h:1627
Recommmends skipping the specified number of samples.
Definition: avcodec.h:1452
AVIOContext * dyn_bc
Definition: matroskaenc.c:107
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:113
#define MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS
Definition: matroska.h:138
#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:1675
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:163
#define MATROSKA_ID_VIDEOCOLORMASTERINGMETA
Definition: matroska.h:144
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
Definition: matroskaenc.c:231
#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:452
int64_t * stream_duration_offsets
Definition: matroskaenc.c:138
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:247
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Definition: matroskaenc.c:525
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
static int flags
Definition: cpu.c:47
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: avcodec.h:3977
#define MATROSKA_ID_FILENAME
Definition: matroska.h:237
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:224
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:2262
static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Definition: matroskaenc.c:562
#define MATROSKA_ID_VIDEOFIELDORDER
Definition: matroska.h:122
int64_t start
Definition: avformat.h:1285
int sample_rate
Audio only.
Definition: avcodec.h:4032
#define MATROSKA_ID_VIDEOALPHAMODE
Definition: matroska.h:124
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:2091
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.
static int mkv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:2231
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:1609
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:188
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:214
int64_t last_track_timestamp[MAX_TRACKS]
Definition: matroskaenc.c:135
#define MATROSKA_ID_CODECDELAY
Definition: matroska.h:94
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:245
common internal api header.
#define MATROSKA_ID_VIDEOCOLORRANGE
Definition: matroska.h:137
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:251
rational numbers
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:35
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:76
static double c[64]
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int keyframe)
Definition: matroskaenc.c:1728
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:2067
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:934
static void put_ebml_void(AVIOContext *pb, uint64_t size)
Write a void element of a given size.
Definition: matroskaenc.c:271
static unsigned bit_depth(uint64_t mask)
Definition: af_astats.c:142
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1284
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:33
char * key
Definition: dict.h:86
#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:1498
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:60
#define EBML_ID_HEADER
Definition: matroska.h:33
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:114
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:487
ebml_master cluster
Definition: matroskaenc.c:110
#define av_free(p)
char * value
Definition: dict.h:87
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:181
mkv_seekhead_entry * entries
Definition: matroskaenc.c:72
int len
static int mkv_write_native_codecprivate(AVFormatContext *s, AVCodecParameters *par, AVIOContext *dyn_cp)
Definition: matroskaenc.c:626
static int mkv_write_stereo_mode(AVFormatContext *s, AVIOContext *pb, AVStream *st, int mode, int *h_width, int *h_height)
Definition: matroskaenc.c:832
#define MATROSKA_ID_FILEUID
Definition: matroska.h:240
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:4991
static int mkv_write_tag_targets(AVFormatContext *s, unsigned int elementid, unsigned int uid, ebml_master *tags, ebml_master *tag)
Definition: matroskaenc.c:1287
static uint8_t tmp[8]
Definition: des.c:38
void * priv_data
Format private data.
Definition: avformat.h:1353
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:255
Views are on top of each other.
Definition: stereo3d.h:55
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:498
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3964
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:81
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3936
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1579
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1865
static const AVOption options[]
Definition: matroskaenc.c:2279
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:248
#define av_freep(p)
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:198
#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:70
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:334
#define MODE_MATROSKAv2
Definition: matroskaenc.c:97
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:114
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
AVCodecParameters * codecpar
Definition: avformat.h:1006
#define MODE_WEBM
Definition: matroskaenc.c:98
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3926
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1992
#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:146
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:435
int stream_index
Definition: avcodec.h:1582
int num_entries
Definition: matroskaenc.c:88
#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:913
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:297
int64_t segment_offset
Definition: matroskaenc.c:109
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:235
#define MATROSKA_ID_VIDEOCOLOR_GY
Definition: matroska.h:148
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:288
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:115
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1557
int64_t * stream_durations
Definition: matroskaenc.c:137
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
#define MATROSKA_ID_VIDEOCOLOR_RY
Definition: matroska.h:146
uint32_t blocksize
Definition: wv.h:35
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
Definition: matroskaenc.c:2197
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:307
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1573
Not part of ABI.
Definition: pixfmt.h:446
#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:232
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:53