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