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/crc.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/intfloat.h"
43 #include "libavutil/intreadwrite.h"
44 #include "libavutil/lfg.h"
46 #include "libavutil/mathematics.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/random_seed.h"
50 #include "libavutil/rational.h"
51 #include "libavutil/samplefmt.h"
52 #include "libavutil/sha.h"
53 #include "libavutil/stereo3d.h"
54 
55 #include "libavcodec/xiph.h"
56 #include "libavcodec/mpeg4audio.h"
57 #include "libavcodec/internal.h"
58 
59 typedef struct ebml_master {
60  int64_t pos; ///< absolute offset in the file where the master's elements start
61  int sizebytes; ///< how many bytes were reserved for the size
62 } ebml_master;
63 
64 typedef struct mkv_seekhead_entry {
65  unsigned int elementid;
66  uint64_t segmentpos;
68 
69 typedef struct mkv_seekhead {
70  int64_t filepos;
71  int64_t segment_offset; ///< the file offset to the beginning of the segment
72  int reserved_size; ///< -1 if appending to file
76 } mkv_seekhead;
77 
78 typedef struct mkv_cuepoint {
79  uint64_t pts;
81  int tracknum;
82  int64_t cluster_pos; ///< file offset of the cluster containing the block
83  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
84  int64_t duration; ///< duration of the block according to time base
85 } mkv_cuepoint;
86 
87 typedef struct mkv_cues {
88  int64_t segment_offset;
91 } mkv_cues;
92 
93 typedef struct mkv_track {
94  int write_dts;
95  int has_cue;
99  int64_t ts_offset;
100 } mkv_track;
101 
102 typedef struct mkv_attachment {
104  uint32_t fileuid;
106 
107 typedef struct mkv_attachments {
111 
112 #define MODE_MATROSKAv2 0x01
113 #define MODE_WEBM 0x02
114 
115 /** Maximum number of tracks allowed in a Matroska file (with track numbers in
116  * range 1 to 126 (inclusive) */
117 #define MAX_TRACKS 126
118 
119 typedef struct MatroskaMuxContext {
120  const AVClass *class;
121  int mode;
130  int64_t segment_offset;
132  int64_t cluster_pos; ///< file offset of the current cluster
133  int64_t cluster_pts;
135  int64_t duration;
140 
142 
145 
148  int64_t cues_pos;
150  int is_dash;
152  int is_live;
154 
157 
159 
162 
165 
166 
167 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
168  * offset, 4 bytes for target EBML ID */
169 #define MAX_SEEKENTRY_SIZE 21
170 
171 /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4
172  * 8-byte uint max */
173 #define MAX_CUETRACKPOS_SIZE 42
174 
175 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
176 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
177 
178 /** Seek preroll value for opus */
179 #define OPUS_SEEK_PREROLL 80000000
180 
181 static int ebml_id_size(unsigned int id)
182 {
183  return (av_log2(id + 1) - 1) / 7 + 1;
184 }
185 
186 static void put_ebml_id(AVIOContext *pb, unsigned int id)
187 {
188  int i = ebml_id_size(id);
189  while (i--)
190  avio_w8(pb, (uint8_t)(id >> (i * 8)));
191 }
192 
193 /**
194  * Write an EBML size meaning "unknown size".
195  *
196  * @param bytes The number of bytes the size should occupy (maximum: 8).
197  */
198 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
199 {
200  av_assert0(bytes <= 8);
201  avio_w8(pb, 0x1ff >> bytes);
202  ffio_fill(pb, 0xff, bytes - 1);
203 }
204 
205 /**
206  * Calculate how many bytes are needed to represent a given number in EBML.
207  */
208 static int ebml_num_size(uint64_t num)
209 {
210  int bytes = 1;
211  while ((num + 1) >> bytes * 7)
212  bytes++;
213  return bytes;
214 }
215 
216 /**
217  * Write a number in EBML variable length format.
218  *
219  * @param bytes The number of bytes that need to be used to write the number.
220  * If zero, any number of bytes can be used.
221  */
222 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
223 {
224  int i, needed_bytes = ebml_num_size(num);
225 
226  // sizes larger than this are currently undefined in EBML
227  av_assert0(num < (1ULL << 56) - 1);
228 
229  if (bytes == 0)
230  // don't care how many bytes are used, so use the min
231  bytes = needed_bytes;
232  // the bytes needed to write the given size would exceed the bytes
233  // that we need to use, so write unknown size. This shouldn't happen.
234  av_assert0(bytes >= needed_bytes);
235 
236  num |= 1ULL << bytes * 7;
237  for (i = bytes - 1; i >= 0; i--)
238  avio_w8(pb, (uint8_t)(num >> i * 8));
239 }
240 
241 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
242 {
243  int i, bytes = 1;
244  uint64_t tmp = val;
245  while (tmp >>= 8)
246  bytes++;
247 
248  put_ebml_id(pb, elementid);
249  put_ebml_num(pb, bytes, 0);
250  for (i = bytes - 1; i >= 0; i--)
251  avio_w8(pb, (uint8_t)(val >> i * 8));
252 }
253 
254 static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
255 {
256  int i, bytes = 1;
257  uint64_t tmp = 2*(val < 0 ? val^-1 : val);
258 
259  while (tmp>>=8) bytes++;
260 
261  put_ebml_id(pb, elementid);
262  put_ebml_num(pb, bytes, 0);
263  for (i = bytes - 1; i >= 0; i--)
264  avio_w8(pb, (uint8_t)(val >> i * 8));
265 }
266 
267 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
268 {
269  put_ebml_id(pb, elementid);
270  put_ebml_num(pb, 8, 0);
271  avio_wb64(pb, av_double2int(val));
272 }
273 
274 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
275  const void *buf, int size)
276 {
277  put_ebml_id(pb, elementid);
278  put_ebml_num(pb, size, 0);
279  avio_write(pb, buf, size);
280 }
281 
282 static void put_ebml_string(AVIOContext *pb, unsigned int elementid,
283  const char *str)
284 {
285  put_ebml_binary(pb, elementid, str, strlen(str));
286 }
287 
288 /**
289  * Write a void element of a given size. Useful for reserving space in
290  * the file to be written to later.
291  *
292  * @param size The number of bytes to reserve, which must be at least 2.
293  */
294 static void put_ebml_void(AVIOContext *pb, uint64_t size)
295 {
296  int64_t currentpos = avio_tell(pb);
297 
298  av_assert0(size >= 2);
299 
301  // we need to subtract the length needed to store the size from the
302  // size we need to reserve so 2 cases, we use 8 bytes to store the
303  // size if possible, 1 byte otherwise
304  if (size < 10)
305  put_ebml_num(pb, size - 2, 0);
306  else
307  put_ebml_num(pb, size - 9, 8);
308  ffio_fill(pb, 0, currentpos + size - avio_tell(pb));
309 }
310 
311 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid,
312  uint64_t expectedsize)
313 {
314  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
315  put_ebml_id(pb, elementid);
316  put_ebml_size_unknown(pb, bytes);
317  return (ebml_master) {avio_tell(pb), bytes };
318 }
319 
321 {
322  int64_t pos = avio_tell(pb);
323 
324  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
325  return;
326  put_ebml_num(pb, pos - master.pos, master.sizebytes);
327  avio_seek(pb, pos, SEEK_SET);
328 }
329 
331  ebml_master *master, unsigned int elementid, uint64_t expectedsize)
332 {
333  int ret;
334 
335  if ((ret = avio_open_dyn_buf(dyn_cp)) < 0)
336  return ret;
337 
338  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
339  *master = start_ebml_master(pb, elementid, expectedsize);
340  if (mkv->write_crc && mkv->mode != MODE_WEBM)
341  put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
342  } else
343  *master = start_ebml_master(*dyn_cp, elementid, expectedsize);
344 
345  return 0;
346 }
347 
350 {
351  uint8_t *buf, crc[4];
352  int size, skip = 0;
353 
354  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
355  size = avio_close_dyn_buf(*dyn_cp, &buf);
356  if (mkv->write_crc && mkv->mode != MODE_WEBM) {
357  skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
358  AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX);
359  put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc));
360  }
361  avio_write(pb, buf + skip, size - skip);
362  end_ebml_master(pb, master);
363  } else {
364  end_ebml_master(*dyn_cp, master);
365  size = avio_close_dyn_buf(*dyn_cp, &buf);
366  avio_write(pb, buf, size);
367  }
368  av_free(buf);
369  *dyn_cp = NULL;
370 }
371 
372 /**
373 * Complete ebml master whithout destroying the buffer, allowing for later updates
374 */
377 {
378  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
379 
380  uint8_t *buf;
381  int size = avio_get_dyn_buf(*dyn_cp, &buf);
382 
383  avio_write(pb, buf, size);
384  end_ebml_master(pb, master);
385  }
386 }
387 
388 static void put_xiph_size(AVIOContext *pb, int size)
389 {
390  ffio_fill(pb, 255, size / 255);
391  avio_w8(pb, size % 255);
392 }
393 
394 /**
395  * Free the members allocated in the mux context.
396  */
397 static void mkv_free(MatroskaMuxContext *mkv) {
398  uint8_t* buf;
399  if (mkv->dyn_bc) {
400  avio_close_dyn_buf(mkv->dyn_bc, &buf);
401  av_free(buf);
402  }
403  if (mkv->info_bc) {
404  avio_close_dyn_buf(mkv->info_bc, &buf);
405  av_free(buf);
406  }
407  if (mkv->tracks_bc) {
408  avio_close_dyn_buf(mkv->tracks_bc, &buf);
409  av_free(buf);
410  }
411  if (mkv->tags_bc) {
412  avio_close_dyn_buf(mkv->tags_bc, &buf);
413  av_free(buf);
414  }
415  if (mkv->main_seekhead) {
417  av_freep(&mkv->main_seekhead);
418  }
419  if (mkv->cues) {
420  av_freep(&mkv->cues->entries);
421  av_freep(&mkv->cues);
422  }
423  if (mkv->attachments) {
424  av_freep(&mkv->attachments->entries);
425  av_freep(&mkv->attachments);
426  }
427  av_freep(&mkv->tracks);
428  av_freep(&mkv->stream_durations);
430 }
431 
432 /**
433  * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
434  * elements. If a maximum number of elements is specified, enough space
435  * will be reserved at the current file location to write a seek head of
436  * that size.
437  *
438  * @param segment_offset The absolute offset to the position in the file
439  * where the segment begins.
440  * @param numelements The maximum number of elements that will be indexed
441  * by this seek head, 0 if unlimited.
442  */
443 static mkv_seekhead *mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset,
444  int numelements)
445 {
446  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
447  if (!new_seekhead)
448  return NULL;
449 
450  new_seekhead->segment_offset = segment_offset;
451 
452  if (numelements > 0) {
453  new_seekhead->filepos = avio_tell(pb);
454  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
455  // and size, 6 bytes for a CRC32 element, and 3 bytes to guarantee
456  // that an EBML void element will fit afterwards
457  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 19;
458  new_seekhead->max_entries = numelements;
459  put_ebml_void(pb, new_seekhead->reserved_size);
460  }
461  return new_seekhead;
462 }
463 
464 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
465 {
466  mkv_seekhead_entry *entries = seekhead->entries;
467 
468  // don't store more elements than we reserved space for
469  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
470  return -1;
471 
472  entries = av_realloc_array(entries, seekhead->num_entries + 1, sizeof(mkv_seekhead_entry));
473  if (!entries)
474  return AVERROR(ENOMEM);
475  seekhead->entries = entries;
476 
477  seekhead->entries[seekhead->num_entries].elementid = elementid;
478  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
479 
480  return 0;
481 }
482 
483 /**
484  * Write the seek head to the file and free it. If a maximum number of
485  * elements was specified to mkv_start_seekhead(), the seek head will
486  * be written at the location reserved for it. Otherwise, it is written
487  * at the current location in the file.
488  *
489  * @return The file offset where the seekhead was written,
490  * -1 if an error occurred.
491  */
493 {
494  AVIOContext *dyn_cp;
495  mkv_seekhead *seekhead = mkv->main_seekhead;
496  ebml_master metaseek, seekentry;
497  int64_t currentpos;
498  int i;
499 
500  currentpos = avio_tell(pb);
501 
502  if (seekhead->reserved_size > 0) {
503  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
504  currentpos = -1;
505  goto fail;
506  }
507  }
508 
509  if (start_ebml_master_crc32(pb, &dyn_cp, mkv, &metaseek, MATROSKA_ID_SEEKHEAD,
510  seekhead->reserved_size) < 0) {
511  currentpos = -1;
512  goto fail;
513  }
514 
515  for (i = 0; i < seekhead->num_entries; i++) {
516  mkv_seekhead_entry *entry = &seekhead->entries[i];
517 
519 
521  put_ebml_num(dyn_cp, ebml_id_size(entry->elementid), 0);
522  put_ebml_id(dyn_cp, entry->elementid);
523 
525  end_ebml_master(dyn_cp, seekentry);
526  }
527  end_ebml_master_crc32(pb, &dyn_cp, mkv, metaseek);
528 
529  if (seekhead->reserved_size > 0) {
530  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
531  put_ebml_void(pb, remaining);
532  avio_seek(pb, currentpos, SEEK_SET);
533 
534  currentpos = seekhead->filepos;
535  }
536 fail:
538  av_freep(&mkv->main_seekhead);
539 
540  return currentpos;
541 }
542 
543 static mkv_cues *mkv_start_cues(int64_t segment_offset)
544 {
545  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
546  if (!cues)
547  return NULL;
548 
549  cues->segment_offset = segment_offset;
550  return cues;
551 }
552 
553 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts,
554  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
555 {
556  mkv_cuepoint *entries = cues->entries;
557 
558  if (ts < 0)
559  return 0;
560 
561  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
562  if (!entries)
563  return AVERROR(ENOMEM);
564  cues->entries = entries;
565 
566  cues->entries[cues->num_entries].pts = ts;
567  cues->entries[cues->num_entries].stream_idx = stream;
568  cues->entries[cues->num_entries].tracknum = tracknum;
569  cues->entries[cues->num_entries].cluster_pos = cluster_pos - cues->segment_offset;
570  cues->entries[cues->num_entries].relative_pos = relative_pos;
571  cues->entries[cues->num_entries++].duration = duration;
572 
573  return 0;
574 }
575 
576 static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
577 {
578  MatroskaMuxContext *mkv = s->priv_data;
579  AVIOContext *dyn_cp, *pb = s->pb;
580  ebml_master cues_element;
581  int64_t currentpos;
582  int i, j, ret;
583 
584  currentpos = avio_tell(pb);
585  ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, &cues_element, MATROSKA_ID_CUES, 0);
586  if (ret < 0)
587  return ret;
588 
589  for (i = 0; i < cues->num_entries; i++) {
590  ebml_master cuepoint, track_positions;
591  mkv_cuepoint *entry = &cues->entries[i];
592  uint64_t pts = entry->pts;
593  int ctp_nb = 0;
594 
595  // Calculate the number of entries, so we know the element size
596  for (j = 0; j < num_tracks; j++)
597  tracks[j].has_cue = 0;
598  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
599  int tracknum = entry[j].stream_idx;
600  av_assert0(tracknum>=0 && tracknum<num_tracks);
601  if (tracks[tracknum].has_cue && s->streams[tracknum]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
602  continue;
603  tracks[tracknum].has_cue = 1;
604  ctp_nb ++;
605  }
606 
607  cuepoint = start_ebml_master(dyn_cp, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(ctp_nb));
608  put_ebml_uint(dyn_cp, MATROSKA_ID_CUETIME, pts);
609 
610  // put all the entries from different tracks that have the exact same
611  // timestamp into the same CuePoint
612  for (j = 0; j < num_tracks; j++)
613  tracks[j].has_cue = 0;
614  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
615  int tracknum = entry[j].stream_idx;
616  av_assert0(tracknum>=0 && tracknum<num_tracks);
617  if (tracks[tracknum].has_cue && s->streams[tracknum]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
618  continue;
619  tracks[tracknum].has_cue = 1;
621  put_ebml_uint(dyn_cp, MATROSKA_ID_CUETRACK , entry[j].tracknum );
622  put_ebml_uint(dyn_cp, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos);
623  put_ebml_uint(dyn_cp, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos);
624  if (entry[j].duration != -1)
625  put_ebml_uint(dyn_cp, MATROSKA_ID_CUEDURATION , entry[j].duration);
626  end_ebml_master(dyn_cp, track_positions);
627  }
628  i += j - 1;
629  end_ebml_master(dyn_cp, cuepoint);
630  }
631  end_ebml_master_crc32(pb, &dyn_cp, mkv, cues_element);
632 
633  return currentpos;
634 }
635 
637 {
638  const uint8_t *header_start[3];
639  int header_len[3];
640  int first_header_size;
641  int j;
642 
643  if (par->codec_id == AV_CODEC_ID_VORBIS)
644  first_header_size = 30;
645  else
646  first_header_size = 42;
647 
649  first_header_size, header_start, header_len) < 0) {
650  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
651  return -1;
652  }
653 
654  avio_w8(pb, 2); // number packets - 1
655  for (j = 0; j < 2; j++) {
656  put_xiph_size(pb, header_len[j]);
657  }
658  for (j = 0; j < 3; j++)
659  avio_write(pb, header_start[j], header_len[j]);
660 
661  return 0;
662 }
663 
665 {
666  if (par->extradata && par->extradata_size == 2)
667  avio_write(pb, par->extradata, 2);
668  else
669  avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs
670  return 0;
671 }
672 
674  AVIOContext *pb, AVCodecParameters *par)
675 {
676  int write_comment = (par->channel_layout &&
677  !(par->channel_layout & ~0x3ffffULL) &&
679  int ret = ff_flac_write_header(pb, par->extradata, par->extradata_size,
680  !write_comment);
681 
682  if (ret < 0)
683  return ret;
684 
685  if (write_comment) {
686  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
687  "Lavf" : LIBAVFORMAT_IDENT;
688  AVDictionary *dict = NULL;
689  uint8_t buf[32], *data, *p;
690  int64_t len;
691 
692  snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
693  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
694 
695  len = ff_vorbiscomment_length(dict, vendor);
696  if (len >= ((1<<24) - 4))
697  return AVERROR(EINVAL);
698 
699  data = av_malloc(len + 4);
700  if (!data) {
701  av_dict_free(&dict);
702  return AVERROR(ENOMEM);
703  }
704 
705  data[0] = 0x84;
706  AV_WB24(data + 1, len);
707 
708  p = data + 4;
709  ff_vorbiscomment_write(&p, &dict, vendor);
710 
711  avio_write(pb, data, len + 4);
712 
713  av_freep(&data);
714  av_dict_free(&dict);
715  }
716 
717  return 0;
718 }
719 
720 static int get_aac_sample_rates(AVFormatContext *s, uint8_t *extradata, int extradata_size,
721  int *sample_rate, int *output_sample_rate)
722 {
723  MPEG4AudioConfig mp4ac;
724  int ret;
725 
726  ret = avpriv_mpeg4audio_get_config(&mp4ac, extradata,
727  extradata_size * 8, 1);
728  /* Don't abort if the failure is because of missing extradata. Assume in that
729  * case a bitstream filter will provide the muxer with the extradata in the
730  * first packet.
731  * Abort however if s->pb is not seekable, as we would not be able to seek back
732  * to write the sample rate elements once the extradata shows up, anyway. */
733  if (ret < 0 && (extradata_size || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL))) {
734  av_log(s, AV_LOG_ERROR,
735  "Error parsing AAC extradata, unable to determine samplerate.\n");
736  return AVERROR(EINVAL);
737  }
738 
739  if (ret < 0) {
740  /* This will only happen when this function is called while writing the
741  * header and no extradata is available. The space for this element has
742  * to be reserved for when this function is called again after the
743  * extradata shows up in the first packet, as there's no way to know if
744  * output_sample_rate will be different than sample_rate or not. */
745  *output_sample_rate = *sample_rate;
746  } else {
747  *sample_rate = mp4ac.sample_rate;
748  *output_sample_rate = mp4ac.ext_sample_rate;
749  }
750  return 0;
751 }
752 
754  AVCodecParameters *par,
755  AVIOContext *dyn_cp)
756 {
757  switch (par->codec_id) {
758  case AV_CODEC_ID_VORBIS:
759  case AV_CODEC_ID_THEORA:
760  return put_xiph_codecpriv(s, dyn_cp, par);
761  case AV_CODEC_ID_FLAC:
762  return put_flac_codecpriv(s, dyn_cp, par);
763  case AV_CODEC_ID_WAVPACK:
764  return put_wv_codecpriv(dyn_cp, par);
765  case AV_CODEC_ID_H264:
766  return ff_isom_write_avcc(dyn_cp, par->extradata,
767  par->extradata_size);
768  case AV_CODEC_ID_HEVC:
769  ff_isom_write_hvcc(dyn_cp, par->extradata,
770  par->extradata_size, 0);
771  return 0;
772  case AV_CODEC_ID_ALAC:
773  if (par->extradata_size < 36) {
774  av_log(s, AV_LOG_ERROR,
775  "Invalid extradata found, ALAC expects a 36-byte "
776  "QuickTime atom.");
777  return AVERROR_INVALIDDATA;
778  } else
779  avio_write(dyn_cp, par->extradata + 12,
780  par->extradata_size - 12);
781  break;
782  case AV_CODEC_ID_AAC:
783  if (par->extradata_size)
784  avio_write(dyn_cp, par->extradata, par->extradata_size);
785  else
786  put_ebml_void(pb, MAX_PCE_SIZE + 2 + 4);
787  break;
788  default:
789  if (par->codec_id == AV_CODEC_ID_PRORES &&
791  avio_wl32(dyn_cp, par->codec_tag);
792  } else if (par->extradata_size && par->codec_id != AV_CODEC_ID_TTA)
793  avio_write(dyn_cp, par->extradata, par->extradata_size);
794  }
795 
796  return 0;
797 }
798 
800  AVCodecParameters *par,
801  int native_id, int qt_id)
802 {
803  AVIOContext *dyn_cp;
804  uint8_t *codecpriv;
805  int ret, codecpriv_size;
806 
807  ret = avio_open_dyn_buf(&dyn_cp);
808  if (ret < 0)
809  return ret;
810 
811  if (native_id) {
812  ret = mkv_write_native_codecprivate(s, pb, par, dyn_cp);
813  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
814  if (qt_id) {
815  if (!par->codec_tag)
817  par->codec_id);
820  ) {
821  int i;
822  avio_wb32(dyn_cp, 0x5a + par->extradata_size);
823  avio_wl32(dyn_cp, par->codec_tag);
824  for(i = 0; i < 0x5a - 8; i++)
825  avio_w8(dyn_cp, 0);
826  }
827  avio_write(dyn_cp, par->extradata, par->extradata_size);
828  } else {
830  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
831  avcodec_get_name(par->codec_id));
832 
833  if (!par->codec_tag)
835  par->codec_id);
836  if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {
837  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
838  avcodec_get_name(par->codec_id));
839  ret = AVERROR(EINVAL);
840  }
841 
842  ff_put_bmp_header(dyn_cp, par, 0, 0);
843  }
844  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
845  unsigned int tag;
847  if (!tag) {
848  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
849  avcodec_get_name(par->codec_id));
850  ret = AVERROR(EINVAL);
851  }
852  if (!par->codec_tag)
853  par->codec_tag = tag;
854 
856  }
857 
858  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
859  if (codecpriv_size)
861  codecpriv_size);
862  av_free(codecpriv);
863  return ret;
864 }
865 
867  AVIOContext *dyn_cp;
868  uint8_t *colorinfo_ptr;
869  int side_data_size = 0;
870  int ret, colorinfo_size;
871  const uint8_t *side_data;
872 
873  ret = avio_open_dyn_buf(&dyn_cp);
874  if (ret < 0)
875  return ret;
876 
877  if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
878  par->color_trc < AVCOL_TRC_NB) {
880  par->color_trc);
881  }
882  if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
883  par->color_space < AVCOL_SPC_NB) {
885  }
887  par->color_primaries < AVCOL_PRI_NB) {
889  }
890  if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
891  par->color_range < AVCOL_RANGE_NB) {
893  }
896  int xpos, ypos;
897 
898  avcodec_enum_to_chroma_pos(&xpos, &ypos, par->chroma_location);
899  put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ, (xpos >> 7) + 1);
900  put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT, (ypos >> 7) + 1);
901  }
902 
904  &side_data_size);
905  if (side_data_size) {
906  const AVContentLightMetadata *metadata =
907  (const AVContentLightMetadata*)side_data;
910  }
911 
913  &side_data_size);
914  if (side_data_size == sizeof(AVMasteringDisplayMetadata)) {
915  ebml_master meta_element = start_ebml_master(
917  const AVMasteringDisplayMetadata *metadata =
918  (const AVMasteringDisplayMetadata*)side_data;
919  if (metadata->has_primaries) {
921  av_q2d(metadata->display_primaries[0][0]));
923  av_q2d(metadata->display_primaries[0][1]));
925  av_q2d(metadata->display_primaries[1][0]));
927  av_q2d(metadata->display_primaries[1][1]));
929  av_q2d(metadata->display_primaries[2][0]));
931  av_q2d(metadata->display_primaries[2][1]));
933  av_q2d(metadata->white_point[0]));
935  av_q2d(metadata->white_point[1]));
936  }
937  if (metadata->has_luminance) {
939  av_q2d(metadata->max_luminance));
941  av_q2d(metadata->min_luminance));
942  }
943  end_ebml_master(dyn_cp, meta_element);
944  }
945 
946  colorinfo_size = avio_close_dyn_buf(dyn_cp, &colorinfo_ptr);
947  if (colorinfo_size) {
948  ebml_master colorinfo = start_ebml_master(pb, MATROSKA_ID_VIDEOCOLOR, colorinfo_size);
949  avio_write(pb, colorinfo_ptr, colorinfo_size);
950  end_ebml_master(pb, colorinfo);
951  }
952  av_free(colorinfo_ptr);
953  return 0;
954 }
955 
957 {
958  int side_data_size = 0;
959  const AVSphericalMapping *spherical =
961  &side_data_size);
962 
963  if (side_data_size) {
964  AVIOContext *dyn_cp;
965  uint8_t *projection_ptr;
966  int ret, projection_size;
967 
968  ret = avio_open_dyn_buf(&dyn_cp);
969  if (ret < 0)
970  return ret;
971 
972  switch (spherical->projection) {
976  break;
978  {
979  AVIOContext b;
980  uint8_t private[20];
981  ffio_init_context(&b, private, sizeof(private),
982  1, NULL, NULL, NULL, NULL);
985  avio_wb32(&b, 0); // version + flags
986  avio_wb32(&b, spherical->bound_top);
987  avio_wb32(&b, spherical->bound_bottom);
988  avio_wb32(&b, spherical->bound_left);
989  avio_wb32(&b, spherical->bound_right);
990  put_ebml_binary(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPRIVATE, private, sizeof(private));
991  break;
992  }
994  {
995  AVIOContext b;
996  uint8_t private[12];
997  ffio_init_context(&b, private, sizeof(private),
998  1, NULL, NULL, NULL, NULL);
1001  avio_wb32(&b, 0); // version + flags
1002  avio_wb32(&b, 0); // layout
1003  avio_wb32(&b, spherical->padding);
1004  put_ebml_binary(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPRIVATE, private, sizeof(private));
1005  break;
1006  }
1007  default:
1008  av_log(s, AV_LOG_WARNING, "Unknown projection type\n");
1009  goto end;
1010  }
1011 
1012  if (spherical->yaw)
1013  put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW, (double)spherical->yaw / (1 << 16));
1014  if (spherical->pitch)
1015  put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH, (double)spherical->pitch / (1 << 16));
1016  if (spherical->roll)
1017  put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL, (double)spherical->roll / (1 << 16));
1018 
1019 end:
1020  projection_size = avio_close_dyn_buf(dyn_cp, &projection_ptr);
1021  if (projection_size) {
1022  ebml_master projection = start_ebml_master(pb, MATROSKA_ID_VIDEOPROJECTION, projection_size);
1023  avio_write(pb, projection_ptr, projection_size);
1024  end_ebml_master(pb, projection);
1025  }
1026  av_freep(&projection_ptr);
1027  }
1028 
1029  return 0;
1030 }
1031 
1033  enum AVFieldOrder field_order)
1034 {
1035  switch (field_order) {
1036  case AV_FIELD_UNKNOWN:
1037  break;
1038  case AV_FIELD_PROGRESSIVE:
1041  break;
1042  case AV_FIELD_TT:
1043  case AV_FIELD_BB:
1044  case AV_FIELD_TB:
1045  case AV_FIELD_BT:
1048  if (mode != MODE_WEBM) {
1049  switch (field_order) {
1050  case AV_FIELD_TT:
1053  break;
1054  case AV_FIELD_BB:
1057  break;
1058  case AV_FIELD_TB:
1061  break;
1062  case AV_FIELD_BT:
1065  break;
1066  }
1067  }
1068  }
1069 }
1070 
1072  AVStream *st, int mode, int *h_width, int *h_height)
1073 {
1074  int i;
1075  int ret = 0;
1078 
1079  *h_width = 1;
1080  *h_height = 1;
1081  // convert metadata into proper side data and add it to the stream
1082  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1083  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1084  int stereo_mode = atoi(tag->value);
1085 
1086  for (i=0; i<MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1087  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1088  stereo_mode = i;
1089  break;
1090  }
1091 
1092  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
1093  stereo_mode != 10 && stereo_mode != 12) {
1094  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
1095  if (ret < 0)
1096  return ret;
1097  }
1098  }
1099 
1100  // iterate to find the stereo3d side data
1101  for (i = 0; i < st->nb_side_data; i++) {
1102  AVPacketSideData sd = st->side_data[i];
1103  if (sd.type == AV_PKT_DATA_STEREO3D) {
1104  AVStereo3D *stereo = (AVStereo3D *)sd.data;
1105 
1106  switch (stereo->type) {
1107  case AV_STEREO3D_2D:
1109  break;
1111  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1114  *h_width = 2;
1115  break;
1116  case AV_STEREO3D_TOPBOTTOM:
1118  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1119  format--;
1120  *h_height = 2;
1121  break;
1124  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1125  format--;
1126  break;
1127  case AV_STEREO3D_LINES:
1129  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1130  format--;
1131  *h_height = 2;
1132  break;
1133  case AV_STEREO3D_COLUMNS:
1135  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1136  format--;
1137  *h_width = 2;
1138  break;
1141  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1142  format++;
1143  break;
1144  }
1145  break;
1146  }
1147  }
1148 
1149  if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
1150  return ret;
1151 
1152  // if webm, do not write unsupported modes
1153  if ((mode == MODE_WEBM &&
1156  || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
1157  av_log(s, AV_LOG_ERROR,
1158  "The specified stereo mode is not valid.\n");
1160  return AVERROR(EINVAL);
1161  }
1162 
1163  // write StereoMode if format is valid
1165 
1166  return ret;
1167 }
1168 
1170  int i, AVIOContext *pb, int default_stream_exists)
1171 {
1172  AVStream *st = s->streams[i];
1173  AVCodecParameters *par = st->codecpar;
1174  ebml_master subinfo, track;
1175  int native_id = 0;
1176  int qt_id = 0;
1178  int sample_rate = par->sample_rate;
1179  int output_sample_rate = 0;
1180  int display_width_div = 1;
1181  int display_height_div = 1;
1182  int j, ret;
1184 
1185  if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
1186  mkv->have_attachments = 1;
1187  return 0;
1188  }
1189 
1190  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1191  if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
1192  if (par->bits_per_raw_sample)
1193  bit_depth = par->bits_per_raw_sample;
1194  else
1195  bit_depth = av_get_bytes_per_sample(par->format) << 3;
1196  }
1197  if (!bit_depth)
1198  bit_depth = par->bits_per_coded_sample;
1199  }
1200 
1201  if (par->codec_id == AV_CODEC_ID_AAC) {
1202  ret = get_aac_sample_rates(s, par->extradata, par->extradata_size, &sample_rate,
1203  &output_sample_rate);
1204  if (ret < 0)
1205  return ret;
1206  }
1207 
1208  track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1210  mkv->is_dash ? mkv->dash_track_number : i + 1);
1212  mkv->is_dash ? mkv->dash_track_number : i + 1);
1213  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
1214 
1215  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1217  tag = av_dict_get(st->metadata, "language", NULL, 0);
1218  if (mkv->mode != MODE_WEBM || par->codec_id != AV_CODEC_ID_WEBVTT) {
1219  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag && tag->value ? tag->value:"und");
1220  } else if (tag && tag->value) {
1222  }
1223 
1224  // The default value for TRACKFLAGDEFAULT is 1, so add element
1225  // if we need to clear it.
1226  if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT))
1228 
1231 
1232  if (mkv->mode == MODE_WEBM && par->codec_id == AV_CODEC_ID_WEBVTT) {
1233  const char *codec_id;
1235  codec_id = "D_WEBVTT/CAPTIONS";
1236  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1237  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1238  codec_id = "D_WEBVTT/DESCRIPTIONS";
1239  native_id = MATROSKA_TRACK_TYPE_METADATA;
1240  } else if (st->disposition & AV_DISPOSITION_METADATA) {
1241  codec_id = "D_WEBVTT/METADATA";
1242  native_id = MATROSKA_TRACK_TYPE_METADATA;
1243  } else {
1244  codec_id = "D_WEBVTT/SUBTITLES";
1245  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1246  }
1247  put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
1248  } else {
1249  // look for a codec ID string specific to mkv to use,
1250  // if none are found, use AVI codes
1251  if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
1252  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1253  if (ff_mkv_codec_tags[j].id == par->codec_id && par->codec_id != AV_CODEC_ID_FFV1) {
1255  native_id = 1;
1256  break;
1257  }
1258  }
1259  } else {
1260  if (mkv->allow_raw_vfw) {
1261  native_id = 0;
1262  } else {
1263  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1264  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1265  return AVERROR(EINVAL);
1266  }
1267  }
1268  }
1269 
1270  if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->initial_padding && par->codec_id == AV_CODEC_ID_OPUS) {
1271  int64_t codecdelay = av_rescale_q(par->initial_padding,
1272  (AVRational){ 1, 48000 },
1273  (AVRational){ 1, 1000000000 });
1274  if (codecdelay < 0) {
1275  av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1276  return AVERROR(EINVAL);
1277  }
1278 // mkv->tracks[i].ts_offset = av_rescale_q(par->initial_padding,
1279 // (AVRational){ 1, par->sample_rate },
1280 // st->time_base);
1281 
1282  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1283  }
1284  if (par->codec_id == AV_CODEC_ID_OPUS) {
1286  }
1287 
1288  if (mkv->mode == MODE_WEBM && !(par->codec_id == AV_CODEC_ID_VP8 ||
1289  par->codec_id == AV_CODEC_ID_VP9 ||
1290  par->codec_id == AV_CODEC_ID_OPUS ||
1291  par->codec_id == AV_CODEC_ID_VORBIS ||
1292  par->codec_id == AV_CODEC_ID_WEBVTT)) {
1294  "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1295  return AVERROR(EINVAL);
1296  }
1297 
1298  switch (par->codec_type) {
1299  case AVMEDIA_TYPE_VIDEO:
1300  mkv->have_video = 1;
1302 
1303  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
1304  && av_cmp_q(av_inv_q(st->avg_frame_rate), st->time_base) > 0)
1305  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->avg_frame_rate.den / st->avg_frame_rate.num);
1306  else
1307  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->time_base.num / st->time_base.den);
1308 
1309  if (!native_id &&
1310  ff_codec_get_tag(ff_codec_movvideo_tags, par->codec_id) &&
1311  ((!ff_codec_get_tag(ff_codec_bmp_tags, par->codec_id) && par->codec_id != AV_CODEC_ID_RAWVIDEO) ||
1312  par->codec_id == AV_CODEC_ID_SVQ1 ||
1313  par->codec_id == AV_CODEC_ID_SVQ3 ||
1314  par->codec_id == AV_CODEC_ID_CINEPAK))
1315  qt_id = 1;
1316 
1317  if (qt_id)
1318  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1319  else if (!native_id) {
1320  // if there is no mkv-specific codec ID, use VFW mode
1321  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1322  mkv->tracks[i].write_dts = 1;
1323  s->internal->avoid_negative_ts_use_pts = 0;
1324  }
1325 
1326  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
1327 
1328  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , par->width);
1329  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height);
1330 
1331  mkv_write_field_order(pb, mkv->mode, par->field_order);
1332 
1333  // check both side data and metadata for stereo information,
1334  // write the result to the bitstream if any is found
1335  ret = mkv_write_stereo_mode(s, pb, st, mkv->mode,
1336  &display_width_div,
1337  &display_height_div);
1338  if (ret < 0)
1339  return ret;
1340 
1341  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1342  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1343  (par->format == AV_PIX_FMT_YUVA420P)) {
1345  }
1346 
1347  // write DisplayWidth and DisplayHeight, they contain the size of
1348  // a single source view and/or the display aspect ratio
1349  if (st->sample_aspect_ratio.num) {
1350  int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1351  if (d_width > INT_MAX) {
1352  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1353  return AVERROR(EINVAL);
1354  }
1355  if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
1356  if (mkv->mode == MODE_WEBM || display_width_div != 1 || display_height_div != 1) {
1357  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
1358  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div);
1359  } else {
1360  AVRational display_aspect_ratio;
1361  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1362  par->width * (int64_t)st->sample_aspect_ratio.num,
1363  par->height * (int64_t)st->sample_aspect_ratio.den,
1364  1024 * 1024);
1365  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH, display_aspect_ratio.num);
1366  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, display_aspect_ratio.den);
1368  }
1369  }
1370  } else if (display_width_div != 1 || display_height_div != 1) {
1371  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , par->width / display_width_div);
1372  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div);
1373  } else if (mkv->mode != MODE_WEBM)
1375 
1376  if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1377  uint32_t color_space = av_le2ne32(par->codec_tag);
1378  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
1379  }
1380  ret = mkv_write_video_color(pb, par, st);
1381  if (ret < 0)
1382  return ret;
1383  ret = mkv_write_video_projection(s, pb, st);
1384  if (ret < 0)
1385  return ret;
1386  end_ebml_master(pb, subinfo);
1387  break;
1388 
1389  case AVMEDIA_TYPE_AUDIO:
1391 
1392  if (!native_id)
1393  // no mkv-specific ID, use ACM mode
1394  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
1395 
1396  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
1397  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , par->channels);
1398 
1399  mkv->tracks[i].sample_rate_offset = avio_tell(pb);
1401  if (output_sample_rate)
1402  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
1403  if (bit_depth)
1405  end_ebml_master(pb, subinfo);
1406  break;
1407 
1408  case AVMEDIA_TYPE_SUBTITLE:
1409  if (!native_id) {
1410  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
1411  return AVERROR(ENOSYS);
1412  }
1413 
1414  if (mkv->mode != MODE_WEBM || par->codec_id != AV_CODEC_ID_WEBVTT)
1415  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1416 
1417  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1418  break;
1419  default:
1420  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1421  return AVERROR(EINVAL);
1422  }
1423 
1424  if (mkv->mode != MODE_WEBM || par->codec_id != AV_CODEC_ID_WEBVTT) {
1425  mkv->tracks[i].codecpriv_offset = avio_tell(pb);
1426  ret = mkv_write_codecprivate(s, pb, par, native_id, qt_id);
1427  if (ret < 0)
1428  return ret;
1429  }
1430 
1431  end_ebml_master(pb, track);
1432 
1433  return 0;
1434 }
1435 
1437 {
1438  MatroskaMuxContext *mkv = s->priv_data;
1439  AVIOContext *pb = s->pb;
1440  int i, ret, default_stream_exists = 0;
1441 
1443  if (ret < 0)
1444  return ret;
1445 
1446  ret = start_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, &mkv->tracks_master, MATROSKA_ID_TRACKS, 0);
1447  if (ret < 0)
1448  return ret;
1449 
1450  for (i = 0; i < s->nb_streams; i++) {
1451  AVStream *st = s->streams[i];
1452  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
1453  }
1454  for (i = 0; i < s->nb_streams; i++) {
1455  ret = mkv_write_track(s, mkv, i, mkv->tracks_bc, default_stream_exists);
1456  if (ret < 0)
1457  return ret;
1458  }
1459 
1460  if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live)
1462  else
1463  end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, mkv->tracks_master);
1464 
1465  return 0;
1466 }
1467 
1469 {
1470  MatroskaMuxContext *mkv = s->priv_data;
1471  AVIOContext *dyn_cp, *pb = s->pb;
1472  ebml_master chapters, editionentry;
1473  AVRational scale = {1, 1E9};
1474  int i, ret;
1475 
1476  if (!s->nb_chapters || mkv->wrote_chapters)
1477  return 0;
1478 
1480  if (ret < 0) return ret;
1481 
1482  ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, &chapters, MATROSKA_ID_CHAPTERS, 0);
1483  if (ret < 0) return ret;
1484 
1485  editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0);
1486  if (mkv->mode != MODE_WEBM) {
1489  }
1490  for (i = 0; i < s->nb_chapters; i++) {
1491  ebml_master chapteratom, chapterdisplay;
1492  AVChapter *c = s->chapters[i];
1493  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
1494  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
1495  AVDictionaryEntry *t = NULL;
1496  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
1497  av_log(s, AV_LOG_ERROR,
1498  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
1499  chapterstart, chapterend);
1500  return AVERROR_INVALIDDATA;
1501  }
1502 
1503  chapteratom = start_ebml_master(dyn_cp, MATROSKA_ID_CHAPTERATOM, 0);
1505  put_ebml_uint(dyn_cp, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
1506  put_ebml_uint(dyn_cp, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
1507  if (mkv->mode != MODE_WEBM) {
1510  }
1511  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1512  chapterdisplay = start_ebml_master(dyn_cp, MATROSKA_ID_CHAPTERDISPLAY, 0);
1514  put_ebml_string(dyn_cp, MATROSKA_ID_CHAPLANG , "und");
1515  end_ebml_master(dyn_cp, chapterdisplay);
1516  }
1517  end_ebml_master(dyn_cp, chapteratom);
1518  }
1519  end_ebml_master(dyn_cp, editionentry);
1520  end_ebml_master_crc32(pb, &dyn_cp, mkv, chapters);
1521 
1522  mkv->wrote_chapters = 1;
1523  return 0;
1524 }
1525 
1527 {
1528  uint8_t *key = av_strdup(t->key);
1529  uint8_t *p = key;
1530  const uint8_t *lang = NULL;
1531  ebml_master tag;
1532 
1533  if (!key)
1534  return AVERROR(ENOMEM);
1535 
1536  if ((p = strrchr(p, '-')) &&
1537  (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1538  *p = 0;
1539 
1540  p = key;
1541  while (*p) {
1542  if (*p == ' ')
1543  *p = '_';
1544  else if (*p >= 'a' && *p <= 'z')
1545  *p -= 'a' - 'A';
1546  p++;
1547  }
1548 
1551  if (lang)
1554  end_ebml_master(pb, tag);
1555 
1556  av_freep(&key);
1557  return 0;
1558 }
1559 
1561  unsigned int elementid, unsigned int uid,
1562  ebml_master *tags, ebml_master* tag)
1563 {
1564  AVIOContext *pb;
1565  MatroskaMuxContext *mkv = s->priv_data;
1566  ebml_master targets;
1567  int ret;
1568 
1569  if (!tags->pos) {
1571  if (ret < 0) return ret;
1572 
1573  start_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv, tags, MATROSKA_ID_TAGS, 0);
1574  }
1575  pb = mkv->tags_bc;
1576 
1577  *tag = start_ebml_master(pb, MATROSKA_ID_TAG, 0);
1578  targets = start_ebml_master(pb, MATROSKA_ID_TAGTARGETS, 0);
1579  if (elementid)
1580  put_ebml_uint(pb, elementid, uid);
1581  end_ebml_master(pb, targets);
1582  return 0;
1583 }
1584 
1585 static int mkv_check_tag_name(const char *name, unsigned int elementid)
1586 {
1587  return av_strcasecmp(name, "title") &&
1588  av_strcasecmp(name, "stereo_mode") &&
1589  av_strcasecmp(name, "creation_time") &&
1590  av_strcasecmp(name, "encoding_tool") &&
1591  av_strcasecmp(name, "duration") &&
1592  (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
1593  av_strcasecmp(name, "language")) &&
1594  (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
1595  (av_strcasecmp(name, "filename") &&
1596  av_strcasecmp(name, "mimetype")));
1597 }
1598 
1599 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
1600  unsigned int uid, ebml_master *tags)
1601 {
1602  MatroskaMuxContext *mkv = s->priv_data;
1603  ebml_master tag;
1604  int ret;
1605  AVDictionaryEntry *t = NULL;
1606 
1607  ret = mkv_write_tag_targets(s, elementid, uid, tags, &tag);
1608  if (ret < 0)
1609  return ret;
1610 
1611  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
1612  if (mkv_check_tag_name(t->key, elementid)) {
1613  ret = mkv_write_simpletag(mkv->tags_bc, t);
1614  if (ret < 0)
1615  return ret;
1616  }
1617  }
1618 
1619  end_ebml_master(mkv->tags_bc, tag);
1620  return 0;
1621 }
1622 
1623 static int mkv_check_tag(AVDictionary *m, unsigned int elementid)
1624 {
1625  AVDictionaryEntry *t = NULL;
1626 
1627  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1628  if (mkv_check_tag_name(t->key, elementid))
1629  return 1;
1630 
1631  return 0;
1632 }
1633 
1635 {
1636  MatroskaMuxContext *mkv = s->priv_data;
1637  int i, ret;
1638 
1640 
1641  if (mkv_check_tag(s->metadata, 0)) {
1642  ret = mkv_write_tag(s, s->metadata, 0, 0, &mkv->tags);
1643  if (ret < 0) return ret;
1644  }
1645 
1646  for (i = 0; i < s->nb_streams; i++) {
1647  AVStream *st = s->streams[i];
1648 
1650  continue;
1651 
1653  continue;
1654 
1655  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &mkv->tags);
1656  if (ret < 0) return ret;
1657  }
1658 
1659  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) {
1660  for (i = 0; i < s->nb_streams; i++) {
1661  AVIOContext *pb;
1662  AVStream *st = s->streams[i];
1663  ebml_master tag_target;
1664  ebml_master tag;
1665 
1667  continue;
1668 
1669  mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &mkv->tags, &tag_target);
1670  pb = mkv->tags_bc;
1671 
1673  put_ebml_string(pb, MATROSKA_ID_TAGNAME, "DURATION");
1674  mkv->stream_duration_offsets[i] = avio_tell(pb);
1675 
1676  // Reserve space to write duration as a 20-byte string.
1677  // 2 (ebml id) + 1 (data size) + 20 (data)
1678  put_ebml_void(pb, 23);
1679  end_ebml_master(pb, tag);
1680  end_ebml_master(pb, tag_target);
1681  }
1682  }
1683 
1684  if (mkv->mode != MODE_WEBM) {
1685  for (i = 0; i < s->nb_chapters; i++) {
1686  AVChapter *ch = s->chapters[i];
1687 
1689  continue;
1690 
1692  if (ret < 0)
1693  return ret;
1694  }
1695  }
1696 
1697  if (mkv->have_attachments && mkv->mode != MODE_WEBM) {
1698  for (i = 0; i < mkv->attachments->num_entries; i++) {
1699  mkv_attachment *attachment = &mkv->attachments->entries[i];
1700  AVStream *st = s->streams[attachment->stream_idx];
1701 
1703  continue;
1704 
1705  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID, attachment->fileuid, &mkv->tags);
1706  if (ret < 0)
1707  return ret;
1708  }
1709  }
1710 
1711  if (mkv->tags.pos) {
1712  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live)
1713  end_ebml_master_crc32_preliminary(s->pb, &mkv->tags_bc, mkv, mkv->tags);
1714  else
1715  end_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv, mkv->tags);
1716  }
1717  return 0;
1718 }
1719 
1721 {
1722  MatroskaMuxContext *mkv = s->priv_data;
1723  AVIOContext *dyn_cp, *pb = s->pb;
1724  ebml_master attachments;
1725  AVLFG c;
1726  int i, ret;
1727 
1728  if (!mkv->have_attachments)
1729  return 0;
1730 
1731  mkv->attachments = av_mallocz(sizeof(*mkv->attachments));
1732  if (!mkv->attachments)
1733  return AVERROR(ENOMEM);
1734 
1736 
1738  if (ret < 0) return ret;
1739 
1740  ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, &attachments, MATROSKA_ID_ATTACHMENTS, 0);
1741  if (ret < 0) return ret;
1742 
1743  for (i = 0; i < s->nb_streams; i++) {
1744  AVStream *st = s->streams[i];
1745  ebml_master attached_file;
1746  mkv_attachment *attachment = mkv->attachments->entries;
1747  AVDictionaryEntry *t;
1748  const char *mimetype = NULL;
1749  uint32_t fileuid;
1750 
1752  continue;
1753 
1754  attachment = av_realloc_array(attachment, mkv->attachments->num_entries + 1, sizeof(mkv_attachment));
1755  if (!attachment)
1756  return AVERROR(ENOMEM);
1757  mkv->attachments->entries = attachment;
1758 
1759  attached_file = start_ebml_master(dyn_cp, MATROSKA_ID_ATTACHEDFILE, 0);
1760 
1761  if (t = av_dict_get(st->metadata, "title", NULL, 0))
1763  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
1764  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
1765  return AVERROR(EINVAL);
1766  }
1768  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
1769  mimetype = t->value;
1770  else if (st->codecpar->codec_id != AV_CODEC_ID_NONE ) {
1771  int i;
1772  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1773  if (ff_mkv_mime_tags[i].id == st->codecpar->codec_id) {
1774  mimetype = ff_mkv_mime_tags[i].str;
1775  break;
1776  }
1777  for (i = 0; ff_mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1778  if (ff_mkv_image_mime_tags[i].id == st->codecpar->codec_id) {
1779  mimetype = ff_mkv_image_mime_tags[i].str;
1780  break;
1781  }
1782  }
1783  if (!mimetype) {
1784  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1785  "it cannot be deduced from the codec id.\n", i);
1786  return AVERROR(EINVAL);
1787  }
1788 
1789  if (s->flags & AVFMT_FLAG_BITEXACT) {
1790  struct AVSHA *sha = av_sha_alloc();
1791  uint8_t digest[20];
1792  if (!sha)
1793  return AVERROR(ENOMEM);
1794  av_sha_init(sha, 160);
1796  av_sha_final(sha, digest);
1797  av_free(sha);
1798  fileuid = AV_RL32(digest);
1799  } else {
1800  fileuid = av_lfg_get(&c);
1801  }
1802  av_log(s, AV_LOG_VERBOSE, "Using %.8"PRIx32" for attachment %d\n",
1803  fileuid, mkv->attachments->num_entries);
1804 
1805  put_ebml_string(dyn_cp, MATROSKA_ID_FILEMIMETYPE, mimetype);
1807  put_ebml_uint(dyn_cp, MATROSKA_ID_FILEUID, fileuid);
1808  end_ebml_master(dyn_cp, attached_file);
1809 
1811  mkv->attachments->entries[mkv->attachments->num_entries++].fileuid = fileuid;
1812  }
1813  end_ebml_master_crc32(pb, &dyn_cp, mkv, attachments);
1814 
1815  return 0;
1816 }
1817 
1819 {
1820  int i = 0;
1821  int64_t max = 0;
1822  int64_t us;
1823 
1824  AVDictionaryEntry *explicitDuration = av_dict_get(s->metadata, "DURATION", NULL, 0);
1825  if (explicitDuration && (av_parse_time(&us, explicitDuration->value, 1) == 0) && us > 0) {
1826  av_log(s, AV_LOG_DEBUG, "get_metadata_duration found duration in context metadata: %" PRId64 "\n", us);
1827  return us;
1828  }
1829 
1830  for (i = 0; i < s->nb_streams; i++) {
1831  int64_t us;
1832  AVDictionaryEntry *duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
1833 
1834  if (duration && (av_parse_time(&us, duration->value, 1) == 0))
1835  max = FFMAX(max, us);
1836  }
1837 
1838  av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
1839  return max;
1840 }
1841 
1843 {
1844  MatroskaMuxContext *mkv = s->priv_data;
1845  AVIOContext *pb = s->pb;
1848  int ret, i, version = 2;
1849  int64_t creation_time;
1850 
1851  if (!strcmp(s->oformat->name, "webm"))
1852  mkv->mode = MODE_WEBM;
1853  else
1854  mkv->mode = MODE_MATROSKAv2;
1855 
1856  if (mkv->mode != MODE_WEBM ||
1857  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
1858  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
1859  version = 4;
1860 
1861  if (s->nb_streams > MAX_TRACKS) {
1862  av_log(s, AV_LOG_ERROR,
1863  "At most %d streams are supported for muxing in Matroska\n",
1864  MAX_TRACKS);
1865  return AVERROR(EINVAL);
1866  }
1867 
1868  for (i = 0; i < s->nb_streams; i++) {
1869  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
1875  av_log(s, AV_LOG_ERROR,
1876  "The Matroska muxer does not yet support muxing %s\n",
1878  return AVERROR_PATCHWELCOME;
1879  }
1880  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
1881  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
1882  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
1883  version = 4;
1884  }
1885 
1886  mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks));
1887  if (!mkv->tracks) {
1888  ret = AVERROR(ENOMEM);
1889  goto fail;
1890  }
1891  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1897  put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , version);
1899  end_ebml_master(pb, ebml_header);
1900 
1902  mkv->segment_offset = avio_tell(pb);
1903 
1904  // we write 2 seek heads - one at the end of the file to point to each
1905  // cluster, and one at the beginning to point to all other level one
1906  // elements (including the seek head at the end of the file), which
1907  // isn't more than 10 elements if we only write one of each other
1908  // currently defined level 1 element
1909  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1910  if (!mkv->main_seekhead) {
1911  ret = AVERROR(ENOMEM);
1912  goto fail;
1913  }
1914 
1916  if (ret < 0) goto fail;
1917 
1918  ret = start_ebml_master_crc32(pb, &mkv->info_bc, mkv, &mkv->info, MATROSKA_ID_INFO, 0);
1919  if (ret < 0)
1920  return ret;
1921  pb = mkv->info_bc;
1922 
1924  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1926  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
1928  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
1930  else
1932 
1933  if (mkv->mode != MODE_WEBM) {
1934  uint32_t segment_uid[4];
1935  AVLFG lfg;
1936 
1938 
1939  for (i = 0; i < 4; i++)
1940  segment_uid[i] = av_lfg_get(&lfg);
1941 
1942  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1943  }
1944  } else {
1945  const char *ident = "Lavf";
1948  }
1949 
1950  if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
1951  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1952  int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
1953  uint8_t date_utc_buf[8];
1954  AV_WB64(date_utc_buf, date_utc);
1955  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1956  }
1957 
1958  // reserve space for the duration
1959  mkv->duration = 0;
1960  mkv->duration_offset = avio_tell(pb);
1961  if (!mkv->is_live) {
1962  int64_t metadata_duration = get_metadata_duration(s);
1963 
1964  if (s->duration > 0) {
1965  int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE);
1966  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
1967  av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration);
1968  } else if (metadata_duration > 0) {
1969  int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE);
1970  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
1971  av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration);
1972  } else {
1973  put_ebml_void(pb, 11); // assumes double-precision float to be written
1974  }
1975  }
1976  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live)
1977  end_ebml_master_crc32_preliminary(s->pb, &mkv->info_bc, mkv, mkv->info);
1978  else
1979  end_ebml_master_crc32(s->pb, &mkv->info_bc, mkv, mkv->info);
1980  pb = s->pb;
1981 
1982  // initialize stream_duration fields
1983  mkv->stream_durations = av_mallocz(s->nb_streams * sizeof(int64_t));
1984  mkv->stream_duration_offsets = av_mallocz(s->nb_streams * sizeof(int64_t));
1985  if (!mkv->stream_durations || !mkv->stream_duration_offsets) {
1986  ret = AVERROR(ENOMEM);
1987  goto fail;
1988  }
1989 
1990  ret = mkv_write_tracks(s);
1991  if (ret < 0)
1992  goto fail;
1993 
1994  for (i = 0; i < s->nb_chapters; i++)
1995  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1996 
1997  ret = mkv_write_chapters(s);
1998  if (ret < 0)
1999  goto fail;
2000 
2001  if (mkv->mode != MODE_WEBM) {
2002  ret = mkv_write_attachments(s);
2003  if (ret < 0)
2004  goto fail;
2005  }
2006 
2007  ret = mkv_write_tags(s);
2008  if (ret < 0)
2009  goto fail;
2010 
2011  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live)
2012  mkv_write_seekhead(pb, mkv);
2013 
2014  mkv->cues = mkv_start_cues(mkv->segment_offset);
2015  if (!mkv->cues) {
2016  ret = AVERROR(ENOMEM);
2017  goto fail;
2018  }
2019  if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && mkv->reserve_cues_space) {
2020  mkv->cues_pos = avio_tell(pb);
2022  }
2023 
2025  mkv->cur_audio_pkt.size = 0;
2026  mkv->cluster_pos = -1;
2027 
2028  avio_flush(pb);
2029 
2030  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2031  // after 4k and on a keyframe
2032  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
2033  if (mkv->cluster_time_limit < 0)
2034  mkv->cluster_time_limit = 5000;
2035  if (mkv->cluster_size_limit < 0)
2036  mkv->cluster_size_limit = 5 * 1024 * 1024;
2037  } else {
2038  if (mkv->cluster_time_limit < 0)
2039  mkv->cluster_time_limit = 1000;
2040  if (mkv->cluster_size_limit < 0)
2041  mkv->cluster_size_limit = 32 * 1024;
2042  }
2043 
2044  return 0;
2045 fail:
2046  mkv_free(mkv);
2047  return ret;
2048 }
2049 
2050 static int mkv_blockgroup_size(int pkt_size)
2051 {
2052  int size = pkt_size + 4;
2053  size += ebml_num_size(size);
2054  size += 2; // EBML ID for block and block duration
2055  size += 8; // max size of block duration
2056  size += ebml_num_size(size);
2057  size += 1; // blockgroup EBML ID
2058  return size;
2059 }
2060 
2061 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
2062 {
2063  uint8_t *dst;
2064  int srclen = *size;
2065  int offset = 0;
2066  int ret;
2067 
2068  dst = av_malloc(srclen);
2069  if (!dst)
2070  return AVERROR(ENOMEM);
2071 
2072  while (srclen >= WV_HEADER_SIZE) {
2073  WvHeader header;
2074 
2075  ret = ff_wv_parse_header(&header, src);
2076  if (ret < 0)
2077  goto fail;
2078  src += WV_HEADER_SIZE;
2079  srclen -= WV_HEADER_SIZE;
2080 
2081  if (srclen < header.blocksize) {
2082  ret = AVERROR_INVALIDDATA;
2083  goto fail;
2084  }
2085 
2086  if (header.initial) {
2087  AV_WL32(dst + offset, header.samples);
2088  offset += 4;
2089  }
2090  AV_WL32(dst + offset, header.flags);
2091  AV_WL32(dst + offset + 4, header.crc);
2092  offset += 8;
2093 
2094  if (!(header.initial && header.final)) {
2095  AV_WL32(dst + offset, header.blocksize);
2096  offset += 4;
2097  }
2098 
2099  memcpy(dst + offset, src, header.blocksize);
2100  src += header.blocksize;
2101  srclen -= header.blocksize;
2102  offset += header.blocksize;
2103  }
2104 
2105  *pdst = dst;
2106  *size = offset;
2107 
2108  return 0;
2109 fail:
2110  av_freep(&dst);
2111  return ret;
2112 }
2113 
2115  unsigned int blockid, AVPacket *pkt, int keyframe)
2116 {
2117  MatroskaMuxContext *mkv = s->priv_data;
2119  uint8_t *data = NULL, *side_data = NULL;
2120  int offset = 0, size = pkt->size, side_data_size = 0;
2121  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
2122  uint64_t additional_id = 0;
2123  int64_t discard_padding = 0;
2124  uint8_t track_number = (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1));
2125  ebml_master block_group, block_additions, block_more;
2126 
2127  ts += mkv->tracks[pkt->stream_index].ts_offset;
2128 
2129  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
2130  "pts %" PRId64 ", dts %" PRId64 ", duration %" PRId64 ", keyframe %d\n",
2131  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration,
2132  keyframe != 0);
2133  if (par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 &&
2134  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
2135  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
2136  else if (par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6 &&
2137  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
2138  /* extradata is Annex B, assume the bitstream is too and convert it */
2139  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
2140  else if (par->codec_id == AV_CODEC_ID_WAVPACK) {
2141  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
2142  if (ret < 0) {
2143  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
2144  return;
2145  }
2146  } else
2147  data = pkt->data;
2148 
2149  if (par->codec_id == AV_CODEC_ID_PRORES && size >= 8) {
2150  /* Matroska specification requires to remove the first QuickTime atom
2151  */
2152  size -= 8;
2153  offset = 8;
2154  }
2155 
2156  side_data = av_packet_get_side_data(pkt,
2158  &side_data_size);
2159 
2160  if (side_data && side_data_size >= 10) {
2161  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
2162  (AVRational){1, par->sample_rate},
2163  (AVRational){1, 1000000000});
2164  }
2165 
2166  side_data = av_packet_get_side_data(pkt,
2168  &side_data_size);
2169  if (side_data) {
2170  additional_id = AV_RB64(side_data);
2171  side_data += 8;
2172  side_data_size -= 8;
2173  }
2174 
2175  if ((side_data_size && additional_id == 1) || discard_padding) {
2176  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
2177  blockid = MATROSKA_ID_BLOCK;
2178  }
2179 
2180  put_ebml_id(pb, blockid);
2181  put_ebml_num(pb, size + 4, 0);
2182  // this assumes stream_index is less than 126
2183  avio_w8(pb, 0x80 | track_number);
2184  avio_wb16(pb, ts - mkv->cluster_pts);
2185  avio_w8(pb, (blockid == MATROSKA_ID_SIMPLEBLOCK && keyframe) ? (1 << 7) : 0);
2186  avio_write(pb, data + offset, size);
2187  if (data != pkt->data)
2188  av_free(data);
2189 
2190  if (blockid == MATROSKA_ID_BLOCK && !keyframe) {
2192  mkv->last_track_timestamp[track_number - 1]);
2193  }
2194  mkv->last_track_timestamp[track_number - 1] = ts - mkv->cluster_pts;
2195 
2196  if (discard_padding) {
2197  put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
2198  }
2199 
2200  if (side_data_size && additional_id == 1) {
2201  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
2202  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
2205  put_ebml_num(pb, side_data_size, 0);
2206  avio_write(pb, side_data, side_data_size);
2207  end_ebml_master(pb, block_more);
2208  end_ebml_master(pb, block_additions);
2209  }
2210  if ((side_data_size && additional_id == 1) || discard_padding) {
2211  end_ebml_master(pb, block_group);
2212  }
2213 }
2214 
2216 {
2217  MatroskaMuxContext *mkv = s->priv_data;
2218  ebml_master blockgroup;
2219  int id_size, settings_size, size;
2220  uint8_t *id, *settings;
2221  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
2222  const int flags = 0;
2223 
2224  id_size = 0;
2226  &id_size);
2227 
2228  settings_size = 0;
2230  &settings_size);
2231 
2232  size = id_size + 1 + settings_size + 1 + pkt->size;
2233 
2234  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
2235  "pts %" PRId64 ", dts %" PRId64 ", duration %" PRId64 ", flags %d\n",
2236  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
2237 
2239 
2241  put_ebml_num(pb, size + 4, 0);
2242  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
2243  avio_wb16(pb, ts - mkv->cluster_pts);
2244  avio_w8(pb, flags);
2245  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
2246 
2248  end_ebml_master(pb, blockgroup);
2249 
2250  return pkt->duration;
2251 }
2252 
2254 {
2255  MatroskaMuxContext *mkv = s->priv_data;
2256 
2257  end_ebml_master_crc32(s->pb, &mkv->dyn_bc, mkv, mkv->cluster);
2258  mkv->cluster_pos = -1;
2259  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
2260  av_log(s, AV_LOG_DEBUG,
2261  "Starting new cluster at offset %" PRIu64 " bytes, "
2262  "pts %" PRIu64 "dts %" PRIu64 "\n",
2263  avio_tell(s->pb), pkt->pts, pkt->dts);
2264  else
2265  av_log(s, AV_LOG_DEBUG, "Starting new cluster, "
2266  "pts %" PRIu64 "dts %" PRIu64 "\n",
2267  pkt->pts, pkt->dts);
2268  avio_flush(s->pb);
2269 }
2270 
2272 {
2273  MatroskaMuxContext *mkv = s->priv_data;
2274  mkv_track *track = &mkv->tracks[pkt->stream_index];
2276  uint8_t *side_data;
2277  int side_data_size = 0, ret;
2278 
2280  &side_data_size);
2281 
2282  switch (par->codec_id) {
2283  case AV_CODEC_ID_AAC:
2284  if (side_data_size && (s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) {
2285  int filler, output_sample_rate = 0;
2286  int64_t curpos;
2287  ret = get_aac_sample_rates(s, side_data, side_data_size, &track->sample_rate,
2288  &output_sample_rate);
2289  if (ret < 0)
2290  return ret;
2291  if (!output_sample_rate)
2292  output_sample_rate = track->sample_rate; // Space is already reserved, so it's this or a void element.
2293  av_freep(&par->extradata);
2294  ret = ff_alloc_extradata(par, side_data_size);
2295  if (ret < 0)
2296  return ret;
2297  memcpy(par->extradata, side_data, side_data_size);
2298  curpos = avio_tell(mkv->tracks_bc);
2299  avio_seek(mkv->tracks_bc, track->codecpriv_offset, SEEK_SET);
2300  mkv_write_codecprivate(s, mkv->tracks_bc, par, 1, 0);
2301  filler = MAX_PCE_SIZE + 2 + 4 - (avio_tell(mkv->tracks_bc) - track->codecpriv_offset);
2302  if (filler)
2303  put_ebml_void(mkv->tracks_bc, filler);
2304  avio_seek(mkv->tracks_bc, track->sample_rate_offset, SEEK_SET);
2306  put_ebml_float(mkv->tracks_bc, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2307  avio_seek(mkv->tracks_bc, curpos, SEEK_SET);
2308  } else if (!par->extradata_size && !track->sample_rate) {
2309  // No extradata (codecpar or packet side data).
2310  av_log(s, AV_LOG_ERROR, "Error parsing AAC extradata, unable to determine samplerate.\n");
2311  return AVERROR(EINVAL);
2312  }
2313  break;
2314  case AV_CODEC_ID_FLAC:
2315  if (side_data_size && (s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) {
2316  AVCodecParameters *codecpriv_par;
2317  int64_t curpos;
2318  if (side_data_size != par->extradata_size) {
2319  av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
2320  pkt->stream_index);
2321  return AVERROR(EINVAL);
2322  }
2323  codecpriv_par = avcodec_parameters_alloc();
2324  if (!codecpriv_par)
2325  return AVERROR(ENOMEM);
2326  ret = avcodec_parameters_copy(codecpriv_par, par);
2327  if (ret < 0) {
2328  avcodec_parameters_free(&codecpriv_par);
2329  return ret;
2330  }
2331  memcpy(codecpriv_par->extradata, side_data, side_data_size);
2332  curpos = avio_tell(mkv->tracks_bc);
2333  avio_seek(mkv->tracks_bc, track->codecpriv_offset, SEEK_SET);
2334  mkv_write_codecprivate(s, mkv->tracks_bc, codecpriv_par, 1, 0);
2335  avio_seek(mkv->tracks_bc, curpos, SEEK_SET);
2336  avcodec_parameters_free(&codecpriv_par);
2337  }
2338  break;
2339  default:
2340  if (side_data_size)
2341  av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
2342  break;
2343  }
2344 
2345  return 0;
2346 }
2347 
2349 {
2350  MatroskaMuxContext *mkv = s->priv_data;
2351  AVIOContext *pb = s->pb;
2353  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
2354  int duration = pkt->duration;
2355  int ret;
2356  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
2357  int64_t relative_packet_pos;
2358  int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;
2359 
2360  if (ts == AV_NOPTS_VALUE) {
2361  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
2362  return AVERROR(EINVAL);
2363  }
2364  ts += mkv->tracks[pkt->stream_index].ts_offset;
2365 
2366  if (mkv->cluster_pos != -1) {
2367  int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;
2368  if ((int16_t)cluster_time != cluster_time) {
2369  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
2370  mkv_start_new_cluster(s, pkt);
2371  }
2372  }
2373 
2374  if (mkv->cluster_pos == -1) {
2375  mkv->cluster_pos = avio_tell(s->pb);
2376  ret = start_ebml_master_crc32(s->pb, &mkv->dyn_bc, mkv, &mkv->cluster, MATROSKA_ID_CLUSTER, 0);
2377  if (ret < 0)
2378  return ret;
2380  mkv->cluster_pts = FFMAX(0, ts);
2381  }
2382  pb = mkv->dyn_bc;
2383 
2384  relative_packet_pos = avio_tell(pb);
2385 
2386  if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {
2387  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);
2388  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {
2389  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);
2390  if (ret < 0) return ret;
2391  }
2392  } else {
2393  if (par->codec_id == AV_CODEC_ID_WEBVTT) {
2394  duration = mkv_write_vtt_blocks(s, pb, pkt);
2395  } else {
2397  mkv_blockgroup_size(pkt->size));
2398 
2399 #if FF_API_CONVERGENCE_DURATION
2401  /* For backward compatibility, prefer convergence_duration. */
2402  if (pkt->convergence_duration > 0) {
2403  duration = pkt->convergence_duration;
2404  }
2406 #endif
2407  /* All subtitle blocks are considered to be keyframes. */
2408  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 1);
2410  end_ebml_master(pb, blockgroup);
2411  }
2412 
2413  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2414  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
2415  mkv->cluster_pos, relative_packet_pos, duration);
2416  if (ret < 0)
2417  return ret;
2418  }
2419  }
2420 
2421  mkv->duration = FFMAX(mkv->duration, ts + duration);
2422 
2423  if (mkv->stream_durations)
2424  mkv->stream_durations[pkt->stream_index] =
2425  FFMAX(mkv->stream_durations[pkt->stream_index], ts + duration);
2426 
2427  return 0;
2428 }
2429 
2431 {
2432  MatroskaMuxContext *mkv = s->priv_data;
2434  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
2435  int cluster_size;
2436  int64_t cluster_time;
2437  int ret;
2438  int start_new_cluster;
2439 
2440  ret = mkv_check_new_extra_data(s, pkt);
2441  if (ret < 0)
2442  return ret;
2443 
2444  if (mkv->tracks[pkt->stream_index].write_dts)
2445  cluster_time = pkt->dts - mkv->cluster_pts;
2446  else
2447  cluster_time = pkt->pts - mkv->cluster_pts;
2448  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
2449 
2450  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2451  // after 4k and on a keyframe
2452  cluster_size = avio_tell(mkv->dyn_bc);
2453 
2454  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
2455  // WebM DASH specification states that the first block of every cluster
2456  // has to be a key frame. So for DASH video, we only create a cluster
2457  // on seeing key frames.
2458  start_new_cluster = keyframe;
2459  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
2460  (mkv->cluster_pos == -1 ||
2461  cluster_time > mkv->cluster_time_limit)) {
2462  // For DASH audio, we create a Cluster based on cluster_time_limit
2463  start_new_cluster = 1;
2464  } else if (!mkv->is_dash &&
2465  (cluster_size > mkv->cluster_size_limit ||
2466  cluster_time > mkv->cluster_time_limit ||
2467  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
2468  cluster_size > 4 * 1024))) {
2469  start_new_cluster = 1;
2470  } else {
2471  start_new_cluster = 0;
2472  }
2473 
2474  if (mkv->cluster_pos != -1 && start_new_cluster) {
2475  mkv_start_new_cluster(s, pkt);
2476  }
2477 
2478  if (!mkv->cluster_pos)
2479  avio_write_marker(s->pb,
2482 
2483  // check if we have an audio packet cached
2484  if (mkv->cur_audio_pkt.size > 0) {
2485  // for DASH audio, a CuePoint has to be added when there is a new cluster.
2487  mkv->is_dash ? start_new_cluster : 0);
2489  if (ret < 0) {
2490  av_log(s, AV_LOG_ERROR,
2491  "Could not write cached audio packet ret:%d\n", ret);
2492  return ret;
2493  }
2494  }
2495 
2496  // buffer an audio packet to ensure the packet containing the video
2497  // keyframe's timecode is contained in the same cluster for WebM
2498  if (codec_type == AVMEDIA_TYPE_AUDIO) {
2499  ret = av_packet_ref(&mkv->cur_audio_pkt, pkt);
2500  } else
2501  ret = mkv_write_packet_internal(s, pkt, 0);
2502  return ret;
2503 }
2504 
2506 {
2507  MatroskaMuxContext *mkv = s->priv_data;
2508 
2509  if (!pkt) {
2510  if (mkv->cluster_pos != -1) {
2511  end_ebml_master_crc32(s->pb, &mkv->dyn_bc, mkv, mkv->cluster);
2512  mkv->cluster_pos = -1;
2513  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
2514  av_log(s, AV_LOG_DEBUG,
2515  "Flushing cluster at offset %" PRIu64 " bytes\n",
2516  avio_tell(s->pb));
2517  else
2518  av_log(s, AV_LOG_DEBUG, "Flushing cluster\n");
2519  avio_flush(s->pb);
2520  }
2521  return 1;
2522  }
2523  return mkv_write_packet(s, pkt);
2524 }
2525 
2527 {
2528  MatroskaMuxContext *mkv = s->priv_data;
2529  AVIOContext *pb = s->pb;
2530  int64_t currentpos, cuespos;
2531  int ret;
2532 
2533  // check if we have an audio packet cached
2534  if (mkv->cur_audio_pkt.size > 0) {
2535  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
2537  if (ret < 0) {
2538  av_log(s, AV_LOG_ERROR,
2539  "Could not write cached audio packet ret:%d\n", ret);
2540  return ret;
2541  }
2542  }
2543 
2544  if (mkv->dyn_bc) {
2545  end_ebml_master_crc32(pb, &mkv->dyn_bc, mkv, mkv->cluster);
2546  }
2547 
2548  ret = mkv_write_chapters(s);
2549  if (ret < 0)
2550  return ret;
2551 
2552 
2553  if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) {
2554  if (mkv->cues->num_entries) {
2555  if (mkv->reserve_cues_space) {
2556  int64_t cues_end;
2557 
2558  currentpos = avio_tell(pb);
2559  avio_seek(pb, mkv->cues_pos, SEEK_SET);
2560 
2561  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
2562  cues_end = avio_tell(pb);
2563  if (cues_end > cuespos + mkv->reserve_cues_space) {
2564  av_log(s, AV_LOG_ERROR,
2565  "Insufficient space reserved for cues: %d "
2566  "(needed: %" PRId64 ").\n",
2567  mkv->reserve_cues_space, cues_end - cuespos);
2568  return AVERROR(EINVAL);
2569  }
2570 
2571  if (cues_end < cuespos + mkv->reserve_cues_space)
2573  (cues_end - cuespos));
2574 
2575  avio_seek(pb, currentpos, SEEK_SET);
2576  } else {
2577  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
2578  }
2579 
2581  cuespos);
2582  if (ret < 0)
2583  return ret;
2584  }
2585 
2586  mkv_write_seekhead(pb, mkv);
2587 
2588  // update the duration
2589  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
2590  currentpos = avio_tell(pb);
2591  avio_seek(mkv->info_bc, mkv->duration_offset, SEEK_SET);
2593  avio_seek(pb, mkv->info.pos, SEEK_SET);
2594  end_ebml_master_crc32(pb, &mkv->info_bc, mkv, mkv->info);
2595 
2596  // write tracks master
2597  avio_seek(pb, mkv->tracks_master.pos, SEEK_SET);
2598  end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, mkv->tracks_master);
2599 
2600  // update stream durations
2601  if (!mkv->is_live && mkv->stream_durations) {
2602  int i;
2603  int64_t curr = avio_tell(mkv->tags_bc);
2604  for (i = 0; i < s->nb_streams; ++i) {
2605  AVStream *st = s->streams[i];
2606 
2607  if (mkv->stream_duration_offsets[i] > 0) {
2608  double duration_sec = mkv->stream_durations[i] * av_q2d(st->time_base);
2609  char duration_string[20] = "";
2610 
2611  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
2612  mkv->stream_durations[i]);
2613 
2614  avio_seek(mkv->tags_bc, mkv->stream_duration_offsets[i], SEEK_SET);
2615 
2616  snprintf(duration_string, 20, "%02d:%02d:%012.9f",
2617  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
2618  fmod(duration_sec, 60));
2619 
2620  put_ebml_binary(mkv->tags_bc, MATROSKA_ID_TAGSTRING, duration_string, 20);
2621  }
2622  }
2623  avio_seek(mkv->tags_bc, curr, SEEK_SET);
2624  }
2625  if (mkv->tags.pos && !mkv->is_live) {
2626  avio_seek(pb, mkv->tags.pos, SEEK_SET);
2627  end_ebml_master_crc32(pb, &mkv->tags_bc, mkv, mkv->tags);
2628  }
2629 
2630  avio_seek(pb, currentpos, SEEK_SET);
2631  }
2632 
2633  if (!mkv->is_live) {
2634  end_ebml_master(pb, mkv->segment);
2635  }
2636 
2637  mkv_free(mkv);
2638  return 0;
2639 }
2640 
2641 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
2642 {
2643  int i;
2644  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
2645  if (ff_mkv_codec_tags[i].id == codec_id)
2646  return 1;
2647 
2648  if (std_compliance < FF_COMPLIANCE_NORMAL) {
2649  enum AVMediaType type = avcodec_get_type(codec_id);
2650  // mkv theoretically supports any video/audio through VFW/ACM
2651  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
2652  return 1;
2653  }
2654 
2655  return 0;
2656 }
2657 
2658 static int mkv_init(struct AVFormatContext *s)
2659 {
2660  int i;
2661 
2662  if (s->avoid_negative_ts < 0) {
2663  s->avoid_negative_ts = 1;
2665  }
2666 
2667  for (i = 0; i < s->nb_streams; i++) {
2668  // ms precision is the de-facto standard timescale for mkv files
2669  avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
2670  }
2671 
2672  return 0;
2673 }
2674 
2675 static int mkv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
2676 {
2677  int ret = 1;
2678  AVStream *st = s->streams[pkt->stream_index];
2679 
2680  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
2681  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
2682  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
2683  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
2684  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
2685  }
2686 
2687  return ret;
2688 }
2689 
2691  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
2692  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
2693  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
2694  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
2695  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
2696  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
2697  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
2698  { AV_CODEC_ID_QDMC, 0xFFFFFFFF },
2699  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
2700  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
2701  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
2702  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
2703  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
2704  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2705 };
2706 
2708  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
2709  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
2710  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
2711  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
2712  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
2713  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2714 };
2715 
2717  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
2718  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
2719  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2720 };
2721 
2722 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
2723 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
2724 static const AVOption options[] = {
2725  { "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 },
2726  { "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 },
2727  { "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 },
2728  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
2729  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
2730  { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
2731  { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
2732  { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
2733  { NULL },
2734 };
2735 
2736 #if CONFIG_MATROSKA_MUXER
2737 static const AVClass matroska_class = {
2738  .class_name = "matroska muxer",
2739  .item_name = av_default_item_name,
2740  .option = options,
2741  .version = LIBAVUTIL_VERSION_INT,
2742 };
2743 
2744 AVOutputFormat ff_matroska_muxer = {
2745  .name = "matroska",
2746  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
2747  .mime_type = "video/x-matroska",
2748  .extensions = "mkv",
2749  .priv_data_size = sizeof(MatroskaMuxContext),
2750  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2752  .video_codec = CONFIG_LIBX264_ENCODER ?
2754  .init = mkv_init,
2760  .codec_tag = (const AVCodecTag* const []){
2763  },
2764  .subtitle_codec = AV_CODEC_ID_ASS,
2765  .query_codec = mkv_query_codec,
2766  .check_bitstream = mkv_check_bitstream,
2767  .priv_class = &matroska_class,
2768 };
2769 #endif
2770 
2771 #if CONFIG_WEBM_MUXER
2772 static const AVClass webm_class = {
2773  .class_name = "webm muxer",
2774  .item_name = av_default_item_name,
2775  .option = options,
2776  .version = LIBAVUTIL_VERSION_INT,
2777 };
2778 
2779 AVOutputFormat ff_webm_muxer = {
2780  .name = "webm",
2781  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
2782  .mime_type = "video/webm",
2783  .extensions = "webm",
2784  .priv_data_size = sizeof(MatroskaMuxContext),
2785  .audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
2786  .video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
2787  .subtitle_codec = AV_CODEC_ID_WEBVTT,
2788  .init = mkv_init,
2792  .check_bitstream = mkv_check_bitstream,
2795  .priv_class = &webm_class,
2796 };
2797 #endif
2798 
2799 #if CONFIG_MATROSKA_AUDIO_MUXER
2800 static const AVClass mka_class = {
2801  .class_name = "matroska audio muxer",
2802  .item_name = av_default_item_name,
2803  .option = options,
2804  .version = LIBAVUTIL_VERSION_INT,
2805 };
2806 AVOutputFormat ff_matroska_audio_muxer = {
2807  .name = "matroska",
2808  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
2809  .mime_type = "audio/x-matroska",
2810  .extensions = "mka",
2811  .priv_data_size = sizeof(MatroskaMuxContext),
2812  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2813  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
2814  .video_codec = AV_CODEC_ID_NONE,
2815  .init = mkv_init,
2819  .check_bitstream = mkv_check_bitstream,
2822  .codec_tag = (const AVCodecTag* const []){
2824  },
2825  .priv_class = &mka_class,
2826 };
2827 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1556
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)
Definition: matroskaenc.c:2348
int32_t pitch
Rotation around the right vector [-90, 90].
Definition: spherical.h:127
Definition: lfg.h:27
#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:345
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:148
#define MATROSKA_ID_VIDEOPROJECTIONPOSEYAW
Definition: matroska.h:159
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:671
enum AVChromaLocation chroma_location
Definition: avcodec.h:4242
internal header for HEVC (de)muxer utilities
#define AV_DISPOSITION_METADATA
Definition: avformat.h:873
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:468
#define NULL
Definition: coverity.c:32
static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:2215
const char const char void * val
Definition: avisynth_c.h:771
#define MATROSKA_ID_BLOCKADDID
Definition: matroska.h:230
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:104
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:474
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:198
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
enum AVColorTransferCharacteristic color_trc
Definition: avcodec.h:4240
#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
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:61
#define MAX_TRACKS
Maximum number of tracks allowed in a Matroska file (with track numbers in range 1 to 126 (inclusive)...
Definition: matroskaenc.c:117
The optional first identifier line of a WebVTT cue.
Definition: avcodec.h:1561
uint32_t samples
Definition: wv.h:39
int initial
Definition: wv.h:43
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:101
unsigned MaxCLL
Max content light level (cd/m^2).
#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:2253
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1342
AVOption.
Definition: opt.h:246
hash context
Definition: sha.c:34
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:82
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, int for_asf, int ignore_extradata)
Definition: riffenc.c:209
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:2658
ebml_master tracks_master
Definition: matroskaenc.c:128
#define MATROSKA_ID_VIDEOPROJECTIONPOSEROLL
Definition: matroska.h:161
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:192
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3053
#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:86
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:4737
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:587
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:167
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:99
static const AVCodecTag additional_subtitle_tags[]
Definition: matroskaenc.c:2716
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: avcodec.h:1479
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:1720
uint64_t pts
Definition: matroskaenc.c:79
Video represents a portion of a sphere mapped on a flat surface using equirectangular projection...
Definition: spherical.h:72
static int mkv_write_video_color(AVIOContext *pb, AVCodecParameters *par, AVStream *st)
Definition: matroskaenc.c:866
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:4152
#define MATROSKA_ID_TAGTARGETS_ATTACHUID
Definition: matroska.h:214
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1680
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:1078
const char * b
Definition: vf_curves.c:113
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:246
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1780
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
#define MATROSKA_ID_BLOCKREFERENCE
Definition: matroska.h:237
int av_log2(unsigned v)
Definition: intmath.c:26
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:1842
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
enum AVMediaType codec_type
Definition: rtp.c:37
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:213
int ff_flac_is_native_layout(uint64_t channel_layout)
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:84
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
#define MATROSKA_ID_VIDEOCOLOR_RX
Definition: matroska.h:145
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:56
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
int64_t cluster_time_limit
Definition: matroskaenc.c:149
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:168
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:3043
int64_t segment_offset
Definition: matroskaenc.c:88
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
int avoid_negative_ts_use_pts
Definition: internal.h:121
const char * master
Definition: vf_curves.c:114
#define MATROSKA_ID_VIDEOPROJECTIONTYPE
Definition: matroska.h:157
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:999
Views are next to each other.
Definition: stereo3d.h:67
static AVPacket pkt
#define av_le2ne32(x)
Definition: bswap.h:96
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:196
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX
Definition: matroska.h:153
Definition: matroskaenc.c:64
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition: utils.c:453
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:1310
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ
Definition: matroska.h:135
mkv_track * tracks
Definition: matroskaenc.c:138
#define src
Definition: vp8dsp.c:254
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:495
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:260
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:224
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1313
#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:496
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:253
enum AVColorSpace color_space
Definition: avcodec.h:4241
int64_t pos
absolute offset in the file where the master's elements start
Definition: matroskaenc.c:60
MatroskaVideoStereoModeType
Definition: matroska.h:300
Mastering display metadata (based on SMPTE-2086:2014).
Definition: avcodec.h:1587
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:2723
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:243
Format I/O context.
Definition: avformat.h:1349
#define EBML_ID_CRC32
Definition: matroska.h:46
UID uid
Definition: mxfenc.c:1895
char str[32]
Definition: internal.h:50
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:132
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:438
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static const AVCodecTag additional_audio_tags[]
Definition: matroskaenc.c:2690
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:374
uint8_t
#define MATROSKA_ID_VIDEOCOLOR_BX
Definition: matroska.h:149
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:256
#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:1599
AVOptions.
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:97
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:2258
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:157
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:75
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:181
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:179
uint32_t flags
Definition: wv.h:40
int ff_mkv_stereo3d_conv(AVStream *st, MatroskaVideoStereoModeType stereo_mode)
Definition: matroska.c:155
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint64_t segmentpos
Definition: matroskaenc.c:66
int id
unique ID to identify the chapter
Definition: avformat.h:1307
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int native_id, int qt_id)
Definition: matroskaenc.c:799
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:232
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:259
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:1003
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:498
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:5254
#define MATROSKA_ID_BLOCKMORE
Definition: matroska.h:229
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:1417
int64_t duration
Definition: movenc.c:63
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:197
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:128
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:165
#define MATROSKA_ID_VIDEOCOLOR
Definition: matroska.h:127
Public header for CRC hash function implementation.
int initial_padding
Audio only.
Definition: avcodec.h:4281
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:40
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1460
uint8_t * data
Definition: avcodec.h:1679
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
Definition: matroskaenc.c:576
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:112
static int flags
Definition: log.c:57
#define MATROSKA_ID_BLOCKADDITIONS
Definition: matroska.h:228
uint32_t tag
Definition: movenc.c:1409
Not part of ABI.
Definition: pixfmt.h:511
#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:1623
#define MATROSKA_ID_CUES
Definition: matroska.h:58
static int start_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, ebml_master *master, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:330
int64_t ts_offset
Definition: matroskaenc.c:99
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
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:556
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
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:216
Definition: wv.h:34
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1477
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:184
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:72
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4254
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number in EBML variable length format.
Definition: matroskaenc.c:222
#define av_log(a,...)
int has_cue
Definition: matroskaenc.c:95
#define AV_DISPOSITION_CAPTIONS
To specify text track kind (different from subtitles default).
Definition: avformat.h:871
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:464
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:71
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1368
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:627
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1711
#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:129
static int mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:1526
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:123
uint32_t chapter_id_offset
Definition: matroskaenc.c:155
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:5456
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
AVPacket cur_audio_pkt
Definition: matroskaenc.c:141
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:214
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:1634
#define MATROSKA_ID_VIDEOCOLOR_BY
Definition: matroska.h:150
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2279
#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:1662
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
int flags
Additional information about the frame packing.
Definition: stereo3d.h:166
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:236
#define EBML_ID_EBMLREADVERSION
Definition: matroska.h:37
#define MATROSKA_ID_VIDEOCOLORMAXCLL
Definition: matroska.h:141
#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:173
#define MATROSKA_ID_VIDEOCOLOR_WHITEX
Definition: matroska.h:151
static int get_aac_sample_rates(AVFormatContext *s, uint8_t *extradata, int extradata_size, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:720
static void end_ebml_master_crc32_preliminary(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, ebml_master master)
Complete ebml master whithout destroying the buffer, allowing for later updates.
Definition: matroskaenc.c:375
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
unsigned int elementid
Definition: matroskaenc.c:65
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:72
#define MATROSKA_ID_CLUSTER
Definition: matroska.h:62
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:736
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
Definition: matroska.h:136
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
Views are packed per line, as if interlaced.
Definition: stereo3d.h:129
enum AVColorPrimaries color_primaries
Definition: avcodec.h:4239
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:245
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
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:1094
Not part of ABI.
Definition: pixfmt.h:448
AVIOContext * tracks_bc
Definition: matroskaenc.c:127
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREOMODE_TYPE_NB]
Definition: matroska.c:131
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:2050
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:208
int write_dts
Definition: matroskaenc.c:94
int final
Definition: wv.h:43
AVChapter ** chapters
Definition: avformat.h:1557
enum AVCodecID id
Definition: matroska.h:354
enum AVPacketSideDataType type
Definition: avcodec.h:1625
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:3150
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:1468
static int mkv_check_tag(AVDictionary *m, unsigned int elementid)
Definition: matroskaenc.c:1623
#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:263
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
uint32_t crc
Definition: wv.h:41
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:469
#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:443
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).
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: utils.c:2268
int64_t filepos
Definition: matroskaenc.c:70
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define fail()
Definition: checkasm.h:109
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4170
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:115
#define MATROSKA_ID_TAG
Definition: matroska.h:202
Views are alternated temporally.
Definition: stereo3d.h:92
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:848
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:170
#define LIBAVFORMAT_IDENT
Definition: version.h:46
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
Video frame is split into 6 faces of a cube, and arranged on a 3x2 layout.
Definition: spherical.h:65
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:236
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
Definition: matroska.h:154
audio channel layout utility functions
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:3216
#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:89
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:202
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:209
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:204
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
static int put_wv_codecpriv(AVIOContext *pb, AVCodecParameters *par)
Definition: matroskaenc.c:664
static void mkv_free(MatroskaMuxContext *mkv)
Free the members allocated in the mux context.
Definition: matroskaenc.c:397
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:264
static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv)
Write the seek head to the file and free it.
Definition: matroskaenc.c:492
static int mkv_write_native_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, AVIOContext *dyn_cp)
Definition: matroskaenc.c:753
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:315
static int64_t get_metadata_duration(AVFormatContext *s)
Definition: matroskaenc.c:1818
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:152
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:485
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition: avcodec.h:1593
uint32_t bound_right
Distance from the right edge.
Definition: spherical.h:169
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:203
const char * name
Definition: avformat.h:524
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:357
#define AV_WB24(p, d)
Definition: intreadwrite.h:455
int(* filler)(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:407
static int mkv_check_new_extra_data(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:2271
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:1658
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:251
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
int32_t yaw
Rotation around the up vector [-180, 180].
Definition: spherical.h:126
AVDictionary * metadata
Definition: avformat.h:961
enum AVCodecID codec_id
Definition: vaapi_decode.c:235
enum AVColorRange color_range
Video only.
Definition: avcodec.h:4238
AVIOContext * info_bc
Definition: matroskaenc.c:125
Opaque data information usually sparse.
Definition: avutil.h:205
#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 OFFSET(x)
Definition: matroskaenc.c:2722
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:164
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:1294
static void end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, ebml_master master)
Definition: matroskaenc.c:348
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition: spherical.h:182
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:553
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:267
Stream structure.
Definition: avformat.h:889
#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:282
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1309
Content light level (based on CTA-861.3).
Definition: avcodec.h:1600
static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, int i, AVIOContext *pb, int default_stream_exists)
Definition: matroskaenc.c:1169
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1420
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:836
sample_rate
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:161
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:104
#define MATROSKA_ID_VIDEOCOLORMATRIXCOEFF
Definition: matroska.h:129
#define AV_DISPOSITION_DESCRIPTIONS
Definition: avformat.h:872
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
#define MATROSKA_ID_TRACKFLAGFORCED
Definition: matroska.h:100
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
Views are on top of each other.
Definition: stereo3d.h:79
#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
int64_t sample_rate_offset
Definition: matroskaenc.c:97
#define MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
Definition: matroska.h:160
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
#define MATROSKA_ID_SEEKID
Definition: matroska.h:220
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
int64_t codecpriv_offset
Definition: matroskaenc.c:98
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:194
Public header for SHA-1 & SHA-256 hash function implementations.
#define MATROSKA_ID_BLOCK
Definition: matroska.h:235
#define MATROSKA_ID_INFO
Definition: matroska.h:56
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:618
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:212
static const EbmlSyntax ebml_header[]
Definition: matroskadec.c:382
mkv_attachment * entries
Definition: matroskaenc.c:108
uint32_t fileuid
Definition: matroskaenc.c:104
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:206
static int mkv_check_tag_name(const char *name, unsigned int elementid)
Definition: matroskaenc.c:1585
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:47
#define MATROSKA_ID_TRACKS
Definition: matroska.h:57
void * buf
Definition: avisynth_c.h:690
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1556
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:70
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:217
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:250
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2984
#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:176
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:227
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:115
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:128
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:1436
Rational number (pair of numerator and denominator).
Definition: rational.h:58
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:241
#define MATROSKA_ID_CUEDURATION
Definition: matroska.h:198
#define MATROSKA_ID_CUETIME
Definition: matroska.h:191
Not part of ABI.
Definition: pixfmt.h:477
AVFieldOrder
Definition: avcodec.h:1739
Recommmends skipping the specified number of samples.
Definition: avcodec.h:1521
AVIOContext * dyn_bc
Definition: matroskaenc.c:122
AVMediaType
Definition: avutil.h:199
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
int64_t duration_offset
Definition: matroskaenc.c:134
#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:2061
This structure describes how to handle spherical videos, outlining information about projection...
Definition: spherical.h:82
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:186
#define MATROSKA_ID_VIDEOPROJECTION
Definition: matroska.h:156
#define MATROSKA_ID_VIDEOCOLORMASTERINGMETA
Definition: matroska.h:144
misc parsing utilities
#define MATROSKA_ID_VIDEOPROJECTIONPRIVATE
Definition: matroska.h:158
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:254
#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:480
int64_t * stream_duration_offsets
Definition: matroskaenc.c:161
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:254
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Definition: matroskaenc.c:636
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: avcodec.h:4207
#define MATROSKA_ID_FILENAME
Definition: matroska.h:244
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
#define MATROSKA_ID_BLOCKADDITIONAL
Definition: matroska.h:231
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:168
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:125
#define MATROSKA_ID_CODECID
Definition: matroska.h:88
static const AVCodecTag additional_video_tags[]
Definition: matroskaenc.c:2707
static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Definition: matroskaenc.c:673
#define MATROSKA_ID_VIDEOFIELDORDER
Definition: matroska.h:122
int64_t start
Definition: avformat.h:1309
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:343
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define MATROSKA_ID_VIDEOALPHAMODE
Definition: matroska.h:124
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:2526
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:2675
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:1708
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:195
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:221
int64_t last_track_timestamp[MAX_TRACKS]
Definition: matroskaenc.c:158
#define MATROSKA_ID_CODECDELAY
Definition: matroska.h:94
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:252
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:274
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:86
Utilties for rational number calculation.
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
static double c[64]
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int keyframe)
Definition: matroskaenc.c:2114
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:2505
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:950
static void put_ebml_void(AVIOContext *pb, uint64_t size)
Write a void element of a given size.
Definition: matroskaenc.c:294
Stereoscopic video.
Views are packed per column.
Definition: stereo3d.h:141
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1308
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
int den
Denominator.
Definition: rational.h:60
#define MATROSKA_ID_SEGMENT
Definition: matroska.h:53
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:115
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata from a raw buffer to retrieve audio configuration. ...
Definition: mpeg4audio.c:155
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition: avcodec.h:1567
#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:122
mkv_attachments * attachments
Definition: matroskaenc.c:139
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:489
ebml_master cluster
Definition: matroskaenc.c:131
#define av_free(p)
char * value
Definition: dict.h:87
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:188
mkv_seekhead_entry * entries
Definition: matroskaenc.c:74
int len
static int mkv_write_stereo_mode(AVFormatContext *s, AVIOContext *pb, AVStream *st, int mode, int *h_width, int *h_height)
Definition: matroskaenc.c:1071
#define MATROSKA_ID_FILEUID
Definition: matroska.h:247
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5324
static int mkv_write_tag_targets(AVFormatContext *s, unsigned int elementid, unsigned int uid, ebml_master *tags, ebml_master *tag)
Definition: matroskaenc.c:1560
void * priv_data
Format private data.
Definition: avformat.h:1377
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:262
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
uint32_t bound_left
Distance from the left edge.
Definition: spherical.h:167
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:83
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4166
#define MATROSKA_ID_VIDEOCOLORMAXFALL
Definition: matroska.h:142
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:120
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1678
static const AVOption options[]
Definition: matroskaenc.c:2724
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:382
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1444
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:255
int sample_rate
Definition: matroskaenc.c:96
#define av_freep(p)
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:205
#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
#define MODE_MATROSKAv2
Definition: matroskaenc.c:112
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
const CodecMime ff_mkv_image_mime_tags[]
Definition: matroska.c:106
AVCodecParameters * codecpar
Definition: avformat.h:1252
ebml_master info
Definition: matroskaenc.c:126
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
#define MODE_WEBM
Definition: matroskaenc.c:113
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:2430
ebml_master tags
Definition: matroskaenc.c:124
#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:169
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:543
int stream_index
Definition: avcodec.h:1681
int num_entries
Definition: matroskaenc.c:90
#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:926
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:320
int64_t segment_offset
Definition: matroskaenc.c:130
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:242
#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:311
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:136
enum AVCodecID id
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
unsigned MaxFALL
Max average light level per frame (cd/m^2).
This structure stores compressed data.
Definition: avcodec.h:1656
int64_t * stream_durations
Definition: matroskaenc.c:160
static void mkv_write_field_order(AVIOContext *pb, int mode, enum AVFieldOrder field_order)
Definition: matroskaenc.c:1032
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
#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:2641
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:388
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
Not part of ABI.
Definition: pixfmt.h:501
#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:248
AVIOContext * tags_bc
Definition: matroskaenc.c:123
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:431
#define MATROSKA_ID_DISCARDPADDING
Definition: matroska.h:239
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:54
static int mkv_write_video_projection(AVFormatContext *s, AVIOContext *pb, AVStream *st)
Definition: matroskaenc.c:956
const char * name
Definition: opengl_enc.c:103
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1325
static uint8_t tmp[11]
Definition: aes_ctr.c:26