FFmpeg  2.1.1
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 "avc.h"
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "avlanguage.h"
26 #include "flacenc.h"
27 #include "internal.h"
28 #include "isom.h"
29 #include "matroska.h"
30 #include "riff.h"
31 #include "subtitles.h"
32 #include "wv.h"
33 
34 #include "libavutil/avstring.h"
35 #include "libavutil/dict.h"
36 #include "libavutil/intfloat.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/lfg.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/random_seed.h"
42 #include "libavutil/samplefmt.h"
43 #include "libavutil/sha.h"
44 
45 #include "libavcodec/xiph.h"
46 #include "libavcodec/mpeg4audio.h"
47 #include "libavcodec/internal.h"
48 
49 typedef struct ebml_master {
50  int64_t pos; ///< absolute offset in the file where the master's elements start
51  int sizebytes; ///< how many bytes were reserved for the size
52 } ebml_master;
53 
54 typedef struct mkv_seekhead_entry {
55  unsigned int elementid;
56  uint64_t segmentpos;
58 
59 typedef struct mkv_seekhead {
60  int64_t filepos;
61  int64_t segment_offset; ///< the file offset to the beginning of the segment
62  int reserved_size; ///< -1 if appending to file
66 } mkv_seekhead;
67 
68 typedef struct {
69  uint64_t pts;
70  int tracknum;
71  int64_t cluster_pos; ///< file offset of the cluster containing the block
72  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
73  int64_t duration; ///< duration of the block according to time base
74 } mkv_cuepoint;
75 
76 typedef struct {
77  int64_t segment_offset;
80 } mkv_cues;
81 
82 typedef struct {
83  int write_dts;
84  int has_cue;
85 } mkv_track;
86 
87 #define MODE_MATROSKAv2 0x01
88 #define MODE_WEBM 0x02
89 
90 typedef struct MatroskaMuxContext {
91  const AVClass *class;
92  int mode;
95  int64_t segment_offset;
97  int64_t cluster_pos; ///< file offset of the current cluster
98  int64_t cluster_pts;
99  int64_t duration_offset;
100  int64_t duration;
104 
106 
108 
111  int64_t cues_pos;
113 
117 
118 
119 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
120  * offset, 4 bytes for target EBML ID */
121 #define MAX_SEEKENTRY_SIZE 21
122 
123 /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4
124  * 8-byte uint max */
125 #define MAX_CUETRACKPOS_SIZE 42
126 
127 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
128 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
129 
130 /** Seek preroll value for opus */
131 #define OPUS_SEEK_PREROLL 80000000
132 
133 
134 static int ebml_id_size(unsigned int id)
135 {
136  return (av_log2(id+1)-1)/7+1;
137 }
138 
139 static void put_ebml_id(AVIOContext *pb, unsigned int id)
140 {
141  int i = ebml_id_size(id);
142  while (i--)
143  avio_w8(pb, (uint8_t)(id >> (i*8)));
144 }
145 
146 /**
147  * Write an EBML size meaning "unknown size".
148  *
149  * @param bytes The number of bytes the size should occupy (maximum: 8).
150  */
151 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
152 {
153  av_assert0(bytes <= 8);
154  avio_w8(pb, 0x1ff >> bytes);
155  ffio_fill(pb, 0xff, bytes - 1);
156 }
157 
158 /**
159  * Calculate how many bytes are needed to represent a given number in EBML.
160  */
161 static int ebml_num_size(uint64_t num)
162 {
163  int bytes = 1;
164  while ((num+1) >> bytes*7) bytes++;
165  return bytes;
166 }
167 
168 /**
169  * Write a number in EBML variable length format.
170  *
171  * @param bytes The number of bytes that need to be used to write the number.
172  * If zero, any number of bytes can be used.
173  */
174 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
175 {
176  int i, needed_bytes = ebml_num_size(num);
177 
178  // sizes larger than this are currently undefined in EBML
179  av_assert0(num < (1ULL<<56)-1);
180 
181  if (bytes == 0)
182  // don't care how many bytes are used, so use the min
183  bytes = needed_bytes;
184  // the bytes needed to write the given size would exceed the bytes
185  // that we need to use, so write unknown size. This shouldn't happen.
186  av_assert0(bytes >= needed_bytes);
187 
188  num |= 1ULL << bytes*7;
189  for (i = bytes - 1; i >= 0; i--)
190  avio_w8(pb, (uint8_t)(num >> i*8));
191 }
192 
193 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
194 {
195  int i, bytes = 1;
196  uint64_t tmp = val;
197  while (tmp>>=8) bytes++;
198 
199  put_ebml_id(pb, elementid);
200  put_ebml_num(pb, bytes, 0);
201  for (i = bytes - 1; i >= 0; i--)
202  avio_w8(pb, (uint8_t)(val >> i*8));
203 }
204 
205 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
206 {
207  put_ebml_id(pb, elementid);
208  put_ebml_num(pb, 8, 0);
209  avio_wb64(pb, av_double2int(val));
210 }
211 
212 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
213  const void *buf, int size)
214 {
215  put_ebml_id(pb, elementid);
216  put_ebml_num(pb, size, 0);
217  avio_write(pb, buf, size);
218 }
219 
220 static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
221 {
222  put_ebml_binary(pb, elementid, str, strlen(str));
223 }
224 
225 /**
226  * Write a void element of a given size. Useful for reserving space in
227  * the file to be written to later.
228  *
229  * @param size The number of bytes to reserve, which must be at least 2.
230  */
231 static void put_ebml_void(AVIOContext *pb, uint64_t size)
232 {
233  int64_t currentpos = avio_tell(pb);
234 
235  av_assert0(size >= 2);
236 
238  // we need to subtract the length needed to store the size from the
239  // size we need to reserve so 2 cases, we use 8 bytes to store the
240  // size if possible, 1 byte otherwise
241  if (size < 10)
242  put_ebml_num(pb, size-1, 0);
243  else
244  put_ebml_num(pb, size-9, 8);
245  ffio_fill(pb, 0, currentpos + size - avio_tell(pb));
246 }
247 
248 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
249 {
250  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
251  put_ebml_id(pb, elementid);
252  put_ebml_size_unknown(pb, bytes);
253  return (ebml_master){ avio_tell(pb), bytes };
254 }
255 
257 {
258  int64_t pos = avio_tell(pb);
259 
260  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
261  return;
262  put_ebml_num(pb, pos - master.pos, master.sizebytes);
263  avio_seek(pb, pos, SEEK_SET);
264 }
265 
266 static void put_xiph_size(AVIOContext *pb, int size)
267 {
268  ffio_fill(pb, 255, size / 255);
269  avio_w8(pb, size % 255);
270 }
271 
272 /**
273  * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
274  * elements. If a maximum number of elements is specified, enough space
275  * will be reserved at the current file location to write a seek head of
276  * that size.
277  *
278  * @param segment_offset The absolute offset to the position in the file
279  * where the segment begins.
280  * @param numelements The maximum number of elements that will be indexed
281  * by this seek head, 0 if unlimited.
282  */
283 static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
284 {
285  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
286  if (new_seekhead == NULL)
287  return NULL;
288 
289  new_seekhead->segment_offset = segment_offset;
290 
291  if (numelements > 0) {
292  new_seekhead->filepos = avio_tell(pb);
293  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
294  // and size, and 3 bytes to guarantee that an EBML void element
295  // will fit afterwards
296  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
297  new_seekhead->max_entries = numelements;
298  put_ebml_void(pb, new_seekhead->reserved_size);
299  }
300  return new_seekhead;
301 }
302 
303 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
304 {
305  mkv_seekhead_entry *entries = seekhead->entries;
306 
307  // don't store more elements than we reserved space for
308  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
309  return -1;
310 
311  entries = av_realloc_array(entries, seekhead->num_entries + 1, sizeof(mkv_seekhead_entry));
312  if (entries == NULL)
313  return AVERROR(ENOMEM);
314  seekhead->entries = entries;
315 
316  seekhead->entries[seekhead->num_entries].elementid = elementid;
317  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
318 
319  return 0;
320 }
321 
322 /**
323  * Write the seek head to the file and free it. If a maximum number of
324  * elements was specified to mkv_start_seekhead(), the seek head will
325  * be written at the location reserved for it. Otherwise, it is written
326  * at the current location in the file.
327  *
328  * @return The file offset where the seekhead was written,
329  * -1 if an error occurred.
330  */
331 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
332 {
333  ebml_master metaseek, seekentry;
334  int64_t currentpos;
335  int i;
336 
337  currentpos = avio_tell(pb);
338 
339  if (seekhead->reserved_size > 0) {
340  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
341  currentpos = -1;
342  goto fail;
343  }
344  }
345 
346  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
347  for (i = 0; i < seekhead->num_entries; i++) {
348  mkv_seekhead_entry *entry = &seekhead->entries[i];
349 
351 
353  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
354  put_ebml_id(pb, entry->elementid);
355 
357  end_ebml_master(pb, seekentry);
358  }
359  end_ebml_master(pb, metaseek);
360 
361  if (seekhead->reserved_size > 0) {
362  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
363  put_ebml_void(pb, remaining);
364  avio_seek(pb, currentpos, SEEK_SET);
365 
366  currentpos = seekhead->filepos;
367  }
368 fail:
369  av_freep(&seekhead->entries);
370  av_free(seekhead);
371 
372  return currentpos;
373 }
374 
375 static mkv_cues * mkv_start_cues(int64_t segment_offset)
376 {
377  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
378  if (cues == NULL)
379  return NULL;
380 
381  cues->segment_offset = segment_offset;
382  return cues;
383 }
384 
385 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos, int64_t relative_pos,
386  int64_t duration)
387 {
388  mkv_cuepoint *entries = cues->entries;
389 
390  if (ts < 0)
391  return 0;
392 
393  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
394  if (entries == NULL)
395  return AVERROR(ENOMEM);
396  cues->entries = entries;
397 
398  cues->entries[cues->num_entries].pts = ts;
399  cues->entries[cues->num_entries].tracknum = stream + 1;
400  cues->entries[cues->num_entries].cluster_pos = cluster_pos - cues->segment_offset;
401  cues->entries[cues->num_entries].relative_pos = relative_pos;
402  cues->entries[cues->num_entries++].duration = duration;
403 
404  return 0;
405 }
406 
407 static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, mkv_track *tracks, int num_tracks)
408 {
409  ebml_master cues_element;
410  int64_t currentpos;
411  int i, j;
412 
413  currentpos = avio_tell(pb);
414  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
415 
416  for (i = 0; i < cues->num_entries; i++) {
417  ebml_master cuepoint, track_positions;
418  mkv_cuepoint *entry = &cues->entries[i];
419  uint64_t pts = entry->pts;
420 
421  cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
423 
424  // put all the entries from different tracks that have the exact same
425  // timestamp into the same CuePoint
426  for (j = 0; j < num_tracks; j++)
427  tracks[j].has_cue = 0;
428  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
429  int tracknum = entry[j].tracknum - 1;
430  av_assert0(tracknum>=0 && tracknum<num_tracks);
431  if (tracks[tracknum].has_cue)
432  continue;
433  tracks[tracknum].has_cue = 1;
435  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
436  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos);
437  put_ebml_uint(pb, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos);
438  if (entry[j].duration != -1)
440  end_ebml_master(pb, track_positions);
441  }
442  i += j - 1;
443  end_ebml_master(pb, cuepoint);
444  }
445  end_ebml_master(pb, cues_element);
446 
447  return currentpos;
448 }
449 
451 {
452  uint8_t *header_start[3];
453  int header_len[3];
454  int first_header_size;
455  int j;
456 
457  if (codec->codec_id == AV_CODEC_ID_VORBIS)
458  first_header_size = 30;
459  else
460  first_header_size = 42;
461 
463  first_header_size, header_start, header_len) < 0) {
464  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
465  return -1;
466  }
467 
468  avio_w8(pb, 2); // number packets - 1
469  for (j = 0; j < 2; j++) {
470  put_xiph_size(pb, header_len[j]);
471  }
472  for (j = 0; j < 3; j++)
473  avio_write(pb, header_start[j], header_len[j]);
474 
475  return 0;
476 }
477 
479 {
480  if (codec->extradata && codec->extradata_size == 2)
481  avio_write(pb, codec->extradata, 2);
482  else
483  avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs
484  return 0;
485 }
486 
487 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
488 {
489  MPEG4AudioConfig mp4ac;
490 
491  if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
492  codec->extradata_size * 8, 1) < 0) {
493  av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
494  return;
495  }
496 
497  *sample_rate = mp4ac.sample_rate;
498  *output_sample_rate = mp4ac.ext_sample_rate;
499 }
500 
501 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
502 {
503  AVIOContext *dyn_cp;
504  uint8_t *codecpriv;
505  int ret, codecpriv_size;
506 
507  ret = avio_open_dyn_buf(&dyn_cp);
508  if(ret < 0)
509  return ret;
510 
511  if (native_id) {
512  if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA)
513  ret = put_xiph_codecpriv(s, dyn_cp, codec);
514  else if (codec->codec_id == AV_CODEC_ID_FLAC)
515  ret = ff_flac_write_header(dyn_cp, codec, 1);
516  else if (codec->codec_id == AV_CODEC_ID_WAVPACK)
517  ret = put_wv_codecpriv(dyn_cp, codec);
518  else if (codec->codec_id == AV_CODEC_ID_H264)
519  ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
520  else if (codec->codec_id == AV_CODEC_ID_ALAC) {
521  if (codec->extradata_size < 36) {
522  av_log(s, AV_LOG_ERROR,
523  "Invalid extradata found, ALAC expects a 36-byte "
524  "QuickTime atom.");
525  ret = AVERROR_INVALIDDATA;
526  } else
527  avio_write(dyn_cp, codec->extradata + 12,
528  codec->extradata_size - 12);
529  }
530  else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA)
531  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
532  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
533  if (qt_id) {
534  if (!codec->codec_tag)
536  if (codec->extradata_size)
537  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
538  } else {
539  if (!codec->codec_tag)
541  if (!codec->codec_tag) {
542  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
543  avcodec_get_name(codec->codec_id));
544  ret = AVERROR(EINVAL);
545  }
546 
547  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
548  }
549 
550  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
551  unsigned int tag;
553  if (!tag) {
554  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
555  avcodec_get_name(codec->codec_id));
556  ret = AVERROR(EINVAL);
557  }
558  if (!codec->codec_tag)
559  codec->codec_tag = tag;
560 
561  ff_put_wav_header(dyn_cp, codec);
562  }
563 
564  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
565  if (codecpriv_size)
566  put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
567  av_free(codecpriv);
568  return ret;
569 }
570 
572 {
573  MatroskaMuxContext *mkv = s->priv_data;
574  AVIOContext *pb = s->pb;
575  ebml_master tracks;
576  int i, j, ret, default_stream_exists = 0;
577 
579  if (ret < 0) return ret;
580 
581  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
582  for (i = 0; i < s->nb_streams; i++) {
583  AVStream *st = s->streams[i];
584  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
585  }
586  for (i = 0; i < s->nb_streams; i++) {
587  AVStream *st = s->streams[i];
588  AVCodecContext *codec = st->codec;
589  ebml_master subinfo, track;
590  int native_id = 0;
591  int qt_id = 0;
592  int bit_depth = av_get_bits_per_sample(codec->codec_id);
593  int sample_rate = codec->sample_rate;
594  int output_sample_rate = 0;
596 
597  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
598  mkv->have_attachments = 1;
599  continue;
600  }
601 
602  if (!bit_depth)
603  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
604  if (!bit_depth)
605  bit_depth = codec->bits_per_coded_sample;
606 
607  if (codec->codec_id == AV_CODEC_ID_AAC)
608  get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
609 
612  put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
613  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
614 
615  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
617  tag = av_dict_get(st->metadata, "language", NULL, 0);
618  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
619  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
620  } else if (tag && tag->value) {
622  }
623 
624  // The default value for TRACKFLAGDEFAULT is 1, so add element
625  // if we need to clear it.
626  if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT))
628 
631 
632  if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) {
633  const char *codec_id;
635  codec_id = "D_WEBVTT/CAPTIONS";
636  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
637  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
638  codec_id = "D_WEBVTT/DESCRIPTIONS";
639  native_id = MATROSKA_TRACK_TYPE_METADATA;
640  } else if (st->disposition & AV_DISPOSITION_METADATA) {
641  codec_id = "D_WEBVTT/METADATA";
642  native_id = MATROSKA_TRACK_TYPE_METADATA;
643  } else {
644  codec_id = "D_WEBVTT/SUBTITLES";
645  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
646  }
647  put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
648  } else {
649  // look for a codec ID string specific to mkv to use,
650  // if none are found, use AVI codes
651  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
652  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
654  native_id = 1;
655  break;
656  }
657  }
658  }
659 
660  if (codec->codec_id == AV_CODEC_ID_OPUS) {
661  uint64_t codec_delay =av_rescale_q(codec->delay,
662  (AVRational){1, codec->sample_rate},
663  (AVRational){1, 1000000000});
664  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codec_delay);
666 
667  }
668 
669  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
670  codec->codec_id == AV_CODEC_ID_VP9 ||
671  ((codec->codec_id == AV_CODEC_ID_OPUS)&&(codec->strict_std_compliance <= FF_COMPLIANCE_EXPERIMENTAL)) ||
672  codec->codec_id == AV_CODEC_ID_VORBIS ||
673  codec->codec_id == AV_CODEC_ID_WEBVTT)) {
674  av_log(s, AV_LOG_ERROR,
675  "Only VP8,VP9 video and Vorbis,Opus(experimental, use -strict -2) audio and WebVTT subtitles are supported for WebM.\n");
676  return AVERROR(EINVAL);
677  }
678 
679  switch (codec->codec_type) {
680  case AVMEDIA_TYPE_VIDEO:
682  if(st->avg_frame_rate.num && st->avg_frame_rate.den && 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base))
683  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9/av_q2d(st->avg_frame_rate));
684  else
685  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
686 
687  if (!native_id &&
688  ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
689  (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id)
690  || codec->codec_id == AV_CODEC_ID_SVQ1
691  || codec->codec_id == AV_CODEC_ID_SVQ3
692  || codec->codec_id == AV_CODEC_ID_CINEPAK))
693  qt_id = 1;
694 
695  if (qt_id)
696  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
697  else if (!native_id) {
698  // if there is no mkv-specific codec ID, use VFW mode
699  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
700  mkv->tracks[i].write_dts = 1;
701  }
702 
703  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
704  // XXX: interlace flag?
705  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
706  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
707 
708  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
709  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
710  // save stereo mode flag
711  uint64_t st_mode = MATROSKA_VIDEO_STEREO_MODE_COUNT;
712 
713  for (j=0; j<MATROSKA_VIDEO_STEREO_MODE_COUNT; j++)
714  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[j])){
715  st_mode = j;
716  break;
717  }
718 
719  if ((mkv->mode == MODE_WEBM && st_mode > 3 && st_mode != 11)
720  || st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) {
721  av_log(s, AV_LOG_ERROR,
722  "The specified stereo mode is not valid.\n");
723  return AVERROR(EINVAL);
724  } else
726  }
727 
728  if ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) ||
729  (tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) ||
730  (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) {
732  }
733 
734  if (st->sample_aspect_ratio.num) {
735  int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
736  if (d_width > INT_MAX) {
737  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
738  return AVERROR(EINVAL);
739  }
741  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height);
742  }
743 
744  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
745  uint32_t color_space = av_le2ne32(codec->codec_tag);
746  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
747  }
748  end_ebml_master(pb, subinfo);
749  break;
750 
751  case AVMEDIA_TYPE_AUDIO:
753 
754  if (!native_id)
755  // no mkv-specific ID, use ACM mode
756  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
757 
758  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
759  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
761  if (output_sample_rate)
762  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
763  if (bit_depth)
765  end_ebml_master(pb, subinfo);
766  break;
767 
769  if (!native_id) {
770  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
771  return AVERROR(ENOSYS);
772  }
773 
774  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT)
775  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
776 
777  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
778  break;
779  default:
780  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
781  return AVERROR(EINVAL);
782  }
783 
784  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
785  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
786  if (ret < 0) return ret;
787  }
788 
789  end_ebml_master(pb, track);
790 
791  // ms precision is the de-facto standard timescale for mkv files
792  avpriv_set_pts_info(st, 64, 1, 1000);
793  }
794  end_ebml_master(pb, tracks);
795  return 0;
796 }
797 
799 {
800  MatroskaMuxContext *mkv = s->priv_data;
801  AVIOContext *pb = s->pb;
802  ebml_master chapters, editionentry;
803  AVRational scale = {1, 1E9};
804  int i, ret;
805 
806  if (!s->nb_chapters || mkv->wrote_chapters)
807  return 0;
808 
810  if (ret < 0) return ret;
811 
812  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
813  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
816  for (i = 0; i < s->nb_chapters; i++) {
817  ebml_master chapteratom, chapterdisplay;
818  AVChapter *c = s->chapters[i];
819  AVDictionaryEntry *t = NULL;
820 
821  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
824  av_rescale_q(c->start, c->time_base, scale));
826  av_rescale_q(c->end, c->time_base, scale));
829  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
830  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
833  end_ebml_master(pb, chapterdisplay);
834  }
835  end_ebml_master(pb, chapteratom);
836  }
837  end_ebml_master(pb, editionentry);
838  end_ebml_master(pb, chapters);
839 
840  mkv->wrote_chapters = 1;
841  return 0;
842 }
843 
845 {
846  uint8_t *key = av_strdup(t->key);
847  uint8_t *p = key;
848  const uint8_t *lang = NULL;
850 
851  if ((p = strrchr(p, '-')) &&
852  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
853  *p = 0;
854 
855  p = key;
856  while (*p) {
857  if (*p == ' ')
858  *p = '_';
859  else if (*p >= 'a' && *p <= 'z')
860  *p -= 'a' - 'A';
861  p++;
862  }
863 
866  if (lang)
869  end_ebml_master(pb, tag);
870 
871  av_freep(&key);
872 }
873 
874 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
875  unsigned int uid, ebml_master *tags)
876 {
877  MatroskaMuxContext *mkv = s->priv_data;
878  ebml_master tag, targets;
879  AVDictionaryEntry *t = NULL;
880  int ret;
881 
882  if (!tags->pos) {
884  if (ret < 0) return ret;
885 
886  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
887  }
888 
889  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
890  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
891  if (elementid)
892  put_ebml_uint(s->pb, elementid, uid);
893  end_ebml_master(s->pb, targets);
894 
895  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
896  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
897  mkv_write_simpletag(s->pb, t);
898 
899  end_ebml_master(s->pb, tag);
900  return 0;
901 }
902 
904 {
905  AVDictionaryEntry *t = NULL;
906 
907  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
908  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
909  return 1;
910 
911  return 0;
912 }
913 
915 {
916  MatroskaMuxContext *mkv = s->priv_data;
917  ebml_master tags = {0};
918  int i, ret;
919 
921 
922  if (mkv_check_tag(s->metadata)) {
923  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
924  if (ret < 0) return ret;
925  }
926 
927  for (i = 0; i < s->nb_streams; i++) {
928  AVStream *st = s->streams[i];
929 
930  if (!mkv_check_tag(st->metadata))
931  continue;
932 
933  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
934  if (ret < 0) return ret;
935  }
936 
937  for (i = 0; i < s->nb_chapters; i++) {
938  AVChapter *ch = s->chapters[i];
939 
940  if (!mkv_check_tag(ch->metadata))
941  continue;
942 
944  if (ret < 0) return ret;
945  }
946 
947  if (tags.pos)
948  end_ebml_master(s->pb, tags);
949  return 0;
950 }
951 
953 {
954  MatroskaMuxContext *mkv = s->priv_data;
955  AVIOContext *pb = s->pb;
956  ebml_master attachments;
957  AVLFG c;
958  int i, ret;
959 
960  if (!mkv->have_attachments)
961  return 0;
962 
964 
966  if (ret < 0) return ret;
967 
968  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
969 
970  for (i = 0; i < s->nb_streams; i++) {
971  AVStream *st = s->streams[i];
972  ebml_master attached_file;
974  const char *mimetype = NULL;
975  uint64_t fileuid;
976 
978  continue;
979 
980  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
981 
982  if (t = av_dict_get(st->metadata, "title", NULL, 0))
984  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
985  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
986  return AVERROR(EINVAL);
987  }
989  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
990  mimetype = t->value;
991  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
992  int i;
993  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
994  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
995  mimetype = ff_mkv_mime_tags[i].str;
996  break;
997  }
998  }
999  if (!mimetype) {
1000  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1001  "it cannot be deduced from the codec id.\n", i);
1002  return AVERROR(EINVAL);
1003  }
1004 
1005  if (st->codec->flags & CODEC_FLAG_BITEXACT) {
1006  struct AVSHA *sha = av_sha_alloc();
1007  uint8_t digest[20];
1008  if (!sha)
1009  return AVERROR(ENOMEM);
1010  av_sha_init(sha, 160);
1012  av_sha_final(sha, digest);
1013  av_free(sha);
1014  fileuid = AV_RL64(digest);
1015  } else {
1016  fileuid = av_lfg_get(&c);
1017  }
1018  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
1019  fileuid, i);
1020 
1023  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
1024  end_ebml_master(pb, attached_file);
1025  }
1026  end_ebml_master(pb, attachments);
1027 
1028  return 0;
1029 }
1030 
1032 {
1033  MatroskaMuxContext *mkv = s->priv_data;
1034  AVIOContext *pb = s->pb;
1035  ebml_master ebml_header, segment_info;
1037  int ret, i;
1038 
1039  if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
1040  else mkv->mode = MODE_MATROSKAv2;
1041 
1042  if (s->avoid_negative_ts < 0)
1043  s->avoid_negative_ts = 1;
1044 
1045  for (i = 0; i < s->nb_streams; i++)
1046  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 ||
1047  s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK ||
1049  s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR ||
1050  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 ||
1051  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) {
1052  av_log(s, AV_LOG_ERROR,
1053  "The Matroska muxer does not yet support muxing %s\n",
1055  return AVERROR_PATCHWELCOME;
1056  }
1057 
1058  mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
1059  if (!mkv->tracks)
1060  return AVERROR(ENOMEM);
1061 
1062  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1070  end_ebml_master(pb, ebml_header);
1071 
1073  mkv->segment_offset = avio_tell(pb);
1074 
1075  // we write 2 seek heads - one at the end of the file to point to each
1076  // cluster, and one at the beginning to point to all other level one
1077  // elements (including the seek head at the end of the file), which
1078  // isn't more than 10 elements if we only write one of each other
1079  // currently defined level 1 element
1080  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1081  if (!mkv->main_seekhead)
1082  return AVERROR(ENOMEM);
1083 
1085  if (ret < 0) return ret;
1086 
1087  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
1089  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1091  if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
1092  uint32_t segment_uid[4];
1093  AVLFG lfg;
1094 
1096 
1097  for (i = 0; i < 4; i++)
1098  segment_uid[i] = av_lfg_get(&lfg);
1099 
1102  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1103  } else {
1104  const char *ident = "Lavf";
1107  }
1108 
1109  if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
1110  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1111  int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000;
1112  uint8_t date_utc_buf[8];
1113  AV_WB64(date_utc_buf, date_utc);
1114  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1115  }
1116 
1117  // reserve space for the duration
1118  mkv->duration = 0;
1119  mkv->duration_offset = avio_tell(pb);
1120  put_ebml_void(pb, 11); // assumes double-precision float to be written
1121  end_ebml_master(pb, segment_info);
1122 
1123  ret = mkv_write_tracks(s);
1124  if (ret < 0) return ret;
1125 
1126  for (i = 0; i < s->nb_chapters; i++)
1127  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1128 
1129  if (mkv->mode != MODE_WEBM) {
1130  ret = mkv_write_chapters(s);
1131  if (ret < 0) return ret;
1132 
1133  ret = mkv_write_tags(s);
1134  if (ret < 0) return ret;
1135 
1136  ret = mkv_write_attachments(s);
1137  if (ret < 0) return ret;
1138  }
1139 
1140  if (!s->pb->seekable)
1142 
1143  mkv->cues = mkv_start_cues(mkv->segment_offset);
1144  if (mkv->cues == NULL)
1145  return AVERROR(ENOMEM);
1146 
1147  if (pb->seekable && mkv->reserve_cues_space) {
1148  mkv->cues_pos = avio_tell(pb);
1150  }
1151 
1153  mkv->cur_audio_pkt.size = 0;
1154  mkv->cluster_pos = -1;
1155 
1156  avio_flush(pb);
1157 
1158  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1159  // after 4k and on a keyframe
1160  if (pb->seekable) {
1161  if (mkv->cluster_time_limit < 0)
1162  mkv->cluster_time_limit = 5000;
1163  if (mkv->cluster_size_limit < 0)
1164  mkv->cluster_size_limit = 5 * 1024 * 1024;
1165  } else {
1166  if (mkv->cluster_time_limit < 0)
1167  mkv->cluster_time_limit = 1000;
1168  if (mkv->cluster_size_limit < 0)
1169  mkv->cluster_size_limit = 32 * 1024;
1170  }
1171 
1172  return 0;
1173 }
1174 
1175 static int mkv_blockgroup_size(int pkt_size)
1176 {
1177  int size = pkt_size + 4;
1178  size += ebml_num_size(size);
1179  size += 2; // EBML ID for block and block duration
1180  size += 8; // max size of block duration
1181  size += ebml_num_size(size);
1182  size += 1; // blockgroup EBML ID
1183  return size;
1184 }
1185 
1186 static int ass_get_duration(const uint8_t *p)
1187 {
1188  int sh, sm, ss, sc, eh, em, es, ec;
1189  uint64_t start, end;
1190 
1191  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
1192  &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
1193  return 0;
1194  start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc;
1195  end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec;
1196  return end - start;
1197 }
1198 
1199 #if FF_API_ASS_SSA
1200 static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1201 {
1202  MatroskaMuxContext *mkv = s->priv_data;
1203  int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
1204  uint8_t *start, *end, *data = pkt->data;
1205  ebml_master blockgroup;
1206  char buffer[2048];
1207 
1208  while (data_size) {
1209  int duration = ass_get_duration(data);
1210  max_duration = FFMAX(duration, max_duration);
1211  end = memchr(data, '\n', data_size);
1212  size = line_size = end ? end-data+1 : data_size;
1213  size -= end ? (end[-1]=='\r')+1 : 0;
1214  start = data;
1215  for (i=0; i<3; i++, start++)
1216  if (!(start = memchr(start, ',', size-(start-data))))
1217  return max_duration;
1218  size -= start - data;
1219  sscanf(data, "Dialogue: %d,", &layer);
1220  i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
1221  s->streams[pkt->stream_index]->nb_frames, layer);
1222  size = FFMIN(i+size, sizeof(buffer));
1223  memcpy(buffer+i, start, size-i);
1224 
1225  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1226  "pts %" PRId64 ", duration %d\n",
1227  avio_tell(pb), size, pkt->pts, duration);
1230  put_ebml_num(pb, size+4, 0);
1231  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1232  avio_wb16(pb, pkt->pts - mkv->cluster_pts);
1233  avio_w8(pb, 0);
1234  avio_write(pb, buffer, size);
1236  end_ebml_master(pb, blockgroup);
1237 
1238  data += line_size;
1239  data_size -= line_size;
1240  }
1241 
1242  return max_duration;
1243 }
1244 #endif
1245 
1246 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1247 {
1248  uint8_t *dst;
1249  int srclen = *size;
1250  int offset = 0;
1251  int ret;
1252 
1253  dst = av_malloc(srclen);
1254  if (!dst)
1255  return AVERROR(ENOMEM);
1256 
1257  while (srclen >= WV_HEADER_SIZE) {
1258  WvHeader header;
1259 
1260  ret = ff_wv_parse_header(&header, src);
1261  if (ret < 0)
1262  goto fail;
1263  src += WV_HEADER_SIZE;
1264  srclen -= WV_HEADER_SIZE;
1265 
1266  if (srclen < header.blocksize) {
1267  ret = AVERROR_INVALIDDATA;
1268  goto fail;
1269  }
1270 
1271  if (header.initial) {
1272  AV_WL32(dst + offset, header.samples);
1273  offset += 4;
1274  }
1275  AV_WL32(dst + offset, header.flags);
1276  AV_WL32(dst + offset + 4, header.crc);
1277  offset += 8;
1278 
1279  if (!(header.initial && header.final)) {
1280  AV_WL32(dst + offset, header.blocksize);
1281  offset += 4;
1282  }
1283 
1284  memcpy(dst + offset, src, header.blocksize);
1285  src += header.blocksize;
1286  srclen -= header.blocksize;
1287  offset += header.blocksize;
1288  }
1289 
1290  *pdst = dst;
1291  *size = offset;
1292 
1293  return 0;
1294 fail:
1295  av_freep(&dst);
1296  return ret;
1297 }
1298 
1300  unsigned int blockid, AVPacket *pkt, int flags)
1301 {
1302  MatroskaMuxContext *mkv = s->priv_data;
1303  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1304  uint8_t *data = NULL, *side_data = NULL;
1305  int offset = 0, size = pkt->size, side_data_size = 0;
1306  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1307  uint64_t additional_id = 0, discard_padding = 0;
1308  ebml_master block_group, block_additions, block_more;
1309 
1310  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1311  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1312  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1313  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1314  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1315  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1316  else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {
1317  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1318  if (ret < 0) {
1319  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1320  return;
1321  }
1322  } else
1323  data = pkt->data;
1324 
1325  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1326  /* Matroska specification requires to remove the first QuickTime atom
1327  */
1328  size -= 8;
1329  offset = 8;
1330  }
1331 
1332  side_data = av_packet_get_side_data(pkt,
1334  &side_data_size);
1335 
1336  if (side_data && side_data_size >= 10) {
1337  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
1338  (AVRational){1, codec->sample_rate},
1339  (AVRational){1, 1000000000});
1340  }
1341 
1342  side_data = av_packet_get_side_data(pkt,
1344  &side_data_size);
1345  if (side_data) {
1346  additional_id = AV_RB64(side_data);
1347  side_data += 8;
1348  side_data_size -= 8;
1349  }
1350 
1351  if ((side_data_size && additional_id == 1) || discard_padding) {
1352  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
1353  blockid = MATROSKA_ID_BLOCK;
1354  }
1355 
1356  put_ebml_id(pb, blockid);
1357  put_ebml_num(pb, size+4, 0);
1358  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1359  avio_wb16(pb, ts - mkv->cluster_pts);
1360  avio_w8(pb, flags);
1361  avio_write(pb, data + offset, size);
1362  if (data != pkt->data)
1363  av_free(data);
1364 
1365  if (discard_padding) {
1366  put_ebml_uint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
1367  }
1368 
1369  if (side_data_size && additional_id == 1) {
1370  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
1371  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
1374  put_ebml_num(pb, side_data_size, 0);
1375  avio_write(pb, side_data, side_data_size);
1376  end_ebml_master(pb, block_more);
1377  end_ebml_master(pb, block_additions);
1378  }
1379  if ((side_data_size && additional_id == 1) || discard_padding) {
1380  end_ebml_master(pb, block_group);
1381  }
1382 }
1383 
1385 {
1386  int i, duration = 0;
1387 
1388  for (i=0; i<2 && !duration; i++) {
1389  int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1390  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1391  &s_hour, &s_min, &s_sec, &s_hsec,
1392  &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1393  s_min += 60*s_hour; e_min += 60*e_hour;
1394  s_sec += 60*s_min; e_sec += 60*e_min;
1395  s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1396  duration = e_hsec - s_hsec;
1397  }
1398  *buf += ff_subtitles_next_line(*buf);
1399  }
1400  return duration;
1401 }
1402 
1404 {
1405  ebml_master blockgroup;
1406  AVPacket pkt2 = *pkt;
1407  int64_t duration = srt_get_duration(&pkt2.data);
1408  pkt2.size -= pkt2.data - pkt->data;
1409 
1410  blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1411  mkv_blockgroup_size(pkt2.size));
1412  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1414  end_ebml_master(pb, blockgroup);
1415 
1416  return duration;
1417 }
1418 
1420 {
1421  MatroskaMuxContext *mkv = s->priv_data;
1422  ebml_master blockgroup;
1423  int id_size, settings_size, size;
1424  uint8_t *id, *settings;
1425  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1426  const int flags = 0;
1427 
1428  id_size = 0;
1430  &id_size);
1431 
1432  settings_size = 0;
1434  &settings_size);
1435 
1436  size = id_size + 1 + settings_size + 1 + pkt->size;
1437 
1438  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1439  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1440  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
1441 
1443 
1445  put_ebml_num(pb, size+4, 0);
1446  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1447  avio_wb16(pb, ts - mkv->cluster_pts);
1448  avio_w8(pb, flags);
1449  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
1450 
1452  end_ebml_master(pb, blockgroup);
1453 
1454  return pkt->duration;
1455 }
1456 
1458 {
1459  MatroskaMuxContext *mkv = s->priv_data;
1460  int bufsize;
1461  uint8_t *dyn_buf;
1462 
1463  if (!mkv->dyn_bc)
1464  return;
1465 
1466  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1467  avio_write(s->pb, dyn_buf, bufsize);
1468  av_free(dyn_buf);
1469  mkv->dyn_bc = NULL;
1470 }
1471 
1473 {
1474  MatroskaMuxContext *mkv = s->priv_data;
1475  AVIOContext *pb = s->pb;
1476  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1477  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1478  int duration = pkt->duration;
1479  int ret;
1480  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1481  int64_t relative_packet_pos;
1482 
1483  if (ts == AV_NOPTS_VALUE) {
1484  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1485  return AVERROR(EINVAL);
1486  }
1487 
1488  if (!s->pb->seekable) {
1489  if (!mkv->dyn_bc) {
1490  if ((ret = avio_open_dyn_buf(&mkv->dyn_bc)) < 0) {
1491  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1492  return ret;
1493  }
1494  }
1495  pb = mkv->dyn_bc;
1496  }
1497 
1498  if (mkv->cluster_pos == -1) {
1499  mkv->cluster_pos = avio_tell(s->pb);
1502  mkv->cluster_pts = FFMAX(0, ts);
1503  }
1504 
1505  relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos;
1506 
1507  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1508  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1509 #if FF_API_ASS_SSA
1510  } else if (codec->codec_id == AV_CODEC_ID_SSA) {
1511  duration = mkv_write_ass_blocks(s, pb, pkt);
1512 #endif
1513  } else if (codec->codec_id == AV_CODEC_ID_SRT) {
1514  duration = mkv_write_srt_blocks(s, pb, pkt);
1515  } else if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
1516  duration = mkv_write_vtt_blocks(s, pb, pkt);
1517  } else {
1519  /* For backward compatibility, prefer convergence_duration. */
1520  if (pkt->convergence_duration > 0) {
1521  duration = pkt->convergence_duration;
1522  }
1523  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1525  end_ebml_master(pb, blockgroup);
1526  }
1527 
1528  if ((codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) || codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1529  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos, relative_packet_pos,
1530  codec->codec_type == AVMEDIA_TYPE_SUBTITLE ? duration : -1);
1531  if (ret < 0) return ret;
1532  }
1533 
1534  mkv->duration = FFMAX(mkv->duration, ts + duration);
1535  return 0;
1536 }
1537 
1539 {
1540  MatroskaMuxContext *mkv = s->priv_data;
1541  int codec_type = s->streams[pkt->stream_index]->codec->codec_type;
1542  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1543  int cluster_size;
1544  int64_t cluster_time;
1545  AVIOContext *pb;
1546  int ret;
1547 
1548  if (mkv->tracks[pkt->stream_index].write_dts)
1549  cluster_time = pkt->dts - mkv->cluster_pts;
1550  else
1551  cluster_time = pkt->pts - mkv->cluster_pts;
1552 
1553  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1554  // after 4k and on a keyframe
1555  if (s->pb->seekable) {
1556  pb = s->pb;
1557  cluster_size = avio_tell(pb) - mkv->cluster_pos;
1558  } else {
1559  pb = mkv->dyn_bc;
1560  cluster_size = avio_tell(pb);
1561  }
1562 
1563  if (mkv->cluster_pos != -1 &&
1564  (cluster_size > mkv->cluster_size_limit ||
1565  cluster_time > mkv->cluster_time_limit ||
1566  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
1567  cluster_size > 4 * 1024))) {
1568  av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
1569  " bytes, pts %" PRIu64 "dts %" PRIu64 "\n",
1570  avio_tell(pb), pkt->pts, pkt->dts);
1571  end_ebml_master(pb, mkv->cluster);
1572  mkv->cluster_pos = -1;
1573  if (mkv->dyn_bc)
1574  mkv_flush_dynbuf(s);
1575  avio_flush(s->pb);
1576  }
1577 
1578  // check if we have an audio packet cached
1579  if (mkv->cur_audio_pkt.size > 0) {
1580  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1582  if (ret < 0) {
1583  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1584  return ret;
1585  }
1586  }
1587 
1588  // buffer an audio packet to ensure the packet containing the video
1589  // keyframe's timecode is contained in the same cluster for WebM
1590  if (codec_type == AVMEDIA_TYPE_AUDIO) {
1591  mkv->cur_audio_pkt = *pkt;
1592  if (pkt->buf) {
1593  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1594  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1595  } else
1596  ret = av_dup_packet(&mkv->cur_audio_pkt);
1597  if (mkv->cur_audio_pkt.side_data_elems > 0) {
1599  }
1600  } else
1601  ret = mkv_write_packet_internal(s, pkt);
1602  return ret;
1603 }
1604 
1606 {
1607  MatroskaMuxContext *mkv = s->priv_data;
1608  AVIOContext *pb;
1609  if (s->pb->seekable)
1610  pb = s->pb;
1611  else
1612  pb = mkv->dyn_bc;
1613  if (!pkt) {
1614  if (mkv->cluster_pos != -1) {
1615  av_log(s, AV_LOG_DEBUG, "Flushing cluster at offset %" PRIu64
1616  " bytes\n", avio_tell(pb));
1617  end_ebml_master(pb, mkv->cluster);
1618  mkv->cluster_pos = -1;
1619  if (mkv->dyn_bc)
1620  mkv_flush_dynbuf(s);
1621  avio_flush(s->pb);
1622  }
1623  return 0;
1624  }
1625  return mkv_write_packet(s, pkt);
1626 }
1627 
1629 {
1630  MatroskaMuxContext *mkv = s->priv_data;
1631  AVIOContext *pb = s->pb;
1632  int64_t currentpos, cuespos;
1633  int ret;
1634 
1635  // check if we have an audio packet cached
1636  if (mkv->cur_audio_pkt.size > 0) {
1637  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1639  if (ret < 0) {
1640  av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1641  return ret;
1642  }
1643  }
1644 
1645  if (mkv->dyn_bc) {
1646  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1647  mkv_flush_dynbuf(s);
1648  } else if (mkv->cluster_pos != -1) {
1649  end_ebml_master(pb, mkv->cluster);
1650  }
1651 
1652  if (mkv->mode != MODE_WEBM) {
1653  ret = mkv_write_chapters(s);
1654  if (ret < 0) return ret;
1655  }
1656 
1657  if (pb->seekable) {
1658  if (mkv->cues->num_entries) {
1659  if (mkv->reserve_cues_space) {
1660  int64_t cues_end;
1661 
1662  currentpos = avio_tell(pb);
1663  avio_seek(pb, mkv->cues_pos, SEEK_SET);
1664 
1665  cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams);
1666  cues_end = avio_tell(pb);
1667  if (cues_end > cuespos + mkv->reserve_cues_space) {
1668  av_log(s, AV_LOG_ERROR, "Insufficient space reserved for cues: %d "
1669  "(needed: %"PRId64").\n", mkv->reserve_cues_space,
1670  cues_end - cuespos);
1671  return AVERROR(EINVAL);
1672  }
1673 
1674  if (cues_end < cuespos + mkv->reserve_cues_space)
1675  put_ebml_void(pb, mkv->reserve_cues_space - (cues_end - cuespos));
1676 
1677  avio_seek(pb, currentpos, SEEK_SET);
1678  } else {
1679  cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams);
1680  }
1681 
1683  if (ret < 0) return ret;
1684  }
1685 
1687 
1688  // update the duration
1689  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1690  currentpos = avio_tell(pb);
1691  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1693 
1694  avio_seek(pb, currentpos, SEEK_SET);
1695  }
1696 
1697  end_ebml_master(pb, mkv->segment);
1698  av_freep(&mkv->tracks);
1699  av_freep(&mkv->cues->entries);
1700  av_freep(&mkv->cues);
1701 
1702  return 0;
1703 }
1704 
1705 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1706 {
1707  int i;
1708  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1709  if (ff_mkv_codec_tags[i].id == codec_id)
1710  return 1;
1711 
1712  if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
1713  enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
1714  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1715  return 1;
1716  }
1717 
1718  return 0;
1719 }
1720 
1722  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
1723  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
1724  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
1725  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
1726  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
1727  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
1728  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
1729  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
1730  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
1731  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
1732  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
1733  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
1734  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1735 };
1736 
1738  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
1739  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
1740  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
1741  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
1742  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
1743  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1744 };
1745 
1746 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1747 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1748 static const AVOption options[] = {
1749  { "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 },
1750  { "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 },
1751  { "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 },
1752  { NULL },
1753 };
1754 
1755 #if CONFIG_MATROSKA_MUXER
1756 static const AVClass matroska_class = {
1757  .class_name = "matroska muxer",
1758  .item_name = av_default_item_name,
1759  .option = options,
1760  .version = LIBAVUTIL_VERSION_INT,
1761 };
1762 
1763 AVOutputFormat ff_matroska_muxer = {
1764  .name = "matroska",
1765  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1766  .mime_type = "video/x-matroska",
1767  .extensions = "mkv",
1768  .priv_data_size = sizeof(MatroskaMuxContext),
1769  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1771  .video_codec = CONFIG_LIBX264_ENCODER ?
1778  .codec_tag = (const AVCodecTag* const []){
1781  },
1782 #if FF_API_ASS_SSA
1783  .subtitle_codec = AV_CODEC_ID_SSA,
1784 #else
1785  .subtitle_codec = AV_CODEC_ID_ASS,
1786 #endif
1787  .query_codec = mkv_query_codec,
1788  .priv_class = &matroska_class,
1789 };
1790 #endif
1791 
1792 #if CONFIG_WEBM_MUXER
1793 static const AVClass webm_class = {
1794  .class_name = "webm muxer",
1795  .item_name = av_default_item_name,
1796  .option = options,
1797  .version = LIBAVUTIL_VERSION_INT,
1798 };
1799 
1800 AVOutputFormat ff_webm_muxer = {
1801  .name = "webm",
1802  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
1803  .mime_type = "video/webm",
1804  .extensions = "webm",
1805  .priv_data_size = sizeof(MatroskaMuxContext),
1806  .audio_codec = AV_CODEC_ID_VORBIS,
1807  .video_codec = AV_CODEC_ID_VP8,
1808  .subtitle_codec = AV_CODEC_ID_WEBVTT,
1814  .priv_class = &webm_class,
1815 };
1816 #endif
1817 
1818 #if CONFIG_MATROSKA_AUDIO_MUXER
1819 static const AVClass mka_class = {
1820  .class_name = "matroska audio muxer",
1821  .item_name = av_default_item_name,
1822  .option = options,
1823  .version = LIBAVUTIL_VERSION_INT,
1824 };
1825 AVOutputFormat ff_matroska_audio_muxer = {
1826  .name = "matroska",
1827  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
1828  .mime_type = "audio/x-matroska",
1829  .extensions = "mka",
1830  .priv_data_size = sizeof(MatroskaMuxContext),
1831  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1832  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
1833  .video_codec = AV_CODEC_ID_NONE,
1839  .codec_tag = (const AVCodecTag* const []){
1841  },
1842  .priv_class = &mka_class,
1843 };
1844 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1125
Definition: lfg.h:25
#define MATROSKA_ID_SEEKPREROLL
Definition: matroska.h:95
void av_sha_final(struct AVSHA *context, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:341
static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1419
const char const char void * val
Definition: avisynth_c.h:671
#define MATROSKA_ID_BLOCKADDID
Definition: matroska.h:194
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:104
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning &quot;unknown size&quot;.
Definition: matroskaenc.c:151
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
static int mkv_check_tag(AVDictionary *m)
Definition: matroskaenc.c:903
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
#define AVERROR_PATCHWELCOME
char * key
Definition: dict.h:81
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:71
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:51
The optional first identifier line of a WebVTT cue.
Definition: avcodec.h:1009
uint32_t samples
Definition: wv.h:39
int initial
Definition: wv.h:43
void av_sha_update(struct AVSHA *context, 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
AVOption.
Definition: opt.h:253
hash context
Definition: sha.c:34
int64_t cluster_pos
file offset of the cluster containing the block
Definition: matroskaenc.c:71
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:365
static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
Definition: matroskaenc.c:844
enum AVCodecID id
Definition: mxfenc.c:90
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:156
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:89
int av_sha_init(struct AVSHA *context, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
#define AV_DISPOSITION_METADATA
Definition: avformat.h:651
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
Definition: mem.c:256
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:3922
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:131
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:99
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:952
uint64_t pts
Definition: matroskaenc.c:69
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int size
Definition: avcodec.h:1064
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:210
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:1031
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
enum AVMediaType codec_type
Definition: rtp.c:37
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:177
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:73
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
int64_t cluster_time_limit
Definition: matroskaenc.c:112
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:132
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2546
int64_t segment_offset
Definition: matroskaenc.c:77
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
const char * master
Definition: vf_curves.c:106
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:160
Definition: matroskaenc.c:54
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:247
mkv_track * tracks
Definition: matroskaenc.c:103
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:223
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:188
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:217
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
int64_t pos
absolute offset in the file where the master&#39;s elements start
Definition: matroskaenc.c:50
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1046
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
#define FLAGS
Definition: matroskaenc.c:1747
AVDictionary * metadata
Definition: avformat.h:735
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:207
Format I/O context.
Definition: avformat.h:968
char str[32]
Definition: internal.h:45
int64_t cluster_pos
file offset of the current cluster
Definition: matroskaenc.c:97
static const AVCodecTag additional_audio_tags[]
Definition: matroskaenc.c:1721
#define AV_DISPOSITION_CAPTIONS
To specify text track kind (different from subtitles default).
Definition: avformat.h:649
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1881
uint8_t
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:220
static int srt_get_duration(uint8_t **buf)
Definition: matroskaenc.c:1384
static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
Definition: matroskaenc.c:874
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:55
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:97
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:66
static int ebml_id_size(unsigned int id)
Definition: matroskaenc.c:134
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:131
uint32_t flags
Definition: wv.h:40
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
uint64_t segmentpos
Definition: matroskaenc.c:56
int id
unique ID to identify the chapter
Definition: avformat.h:943
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:196
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:222
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: matroskaenc.c:1403
#define MATROSKA_ID_BLOCKMORE
Definition: matroska.h:193
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:161
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:129
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
const char data[16]
Definition: mxf.c:68
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:634
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:622
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:112
#define MATROSKA_ID_BLOCKADDITIONS
Definition: matroska.h:192
uint32_t tag
Definition: movenc.c:961
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
Definition: matroskaenc.c:487
static EbmlSyntax ebml_header[]
Definition: matroskadec.c:298
#define WV_HEADER_SIZE
Definition: wavpack.h:30
enum AVCodecID id
Definition: internal.h:46
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:714
planar YUV 4:2:0, 20bpp, (1 Cr &amp; Cb sample per 2x2 Y &amp; A samples)
Definition: avcodec.h:4571
#define MATROSKA_ID_CUES
Definition: matroska.h:58
#define LIBAVFORMAT_IDENT
Definition: version.h:44
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
static int64_t duration
Definition: ffplay.c:306
Definition: wv.h:34
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
const OptionDef options[]
Definition: ffserver.c:4682
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:72
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1030
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number in EBML variable length format.
Definition: matroskaenc.c:174
int has_cue
Definition: matroskaenc.c:84
static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
Definition: matroskaenc.c:303
int64_t segment_offset
the file offset to the beginning of the segment
Definition: matroskaenc.c:61
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
#define MATROSKA_ID_TRACKUID
Definition: matroska.h:79
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
ebml_master segment
Definition: matroskaenc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:353
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:122
uint32_t chapter_id_offset
Definition: matroskaenc.c:114
static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos, int64_t relative_pos, int64_t duration)
Definition: matroskaenc.c:385
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2424
AVPacket cur_audio_pkt
Definition: matroskaenc.c:105
int av_copy_packet_side_data(AVPacket *dst, AVPacket *src)
Copy packet side data.
Definition: avpacket.c:223
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:914
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2928
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
#define AV_DISPOSITION_DESCRIPTIONS
Definition: avformat.h:650
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:200
#define EBML_ID_EBMLREADVERSION
Definition: matroska.h:37
#define MAX_CUETRACKPOS_SIZE
per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4 8-byte uint max
Definition: matroskaenc.c:125
void * priv_data
Format private data.
Definition: avformat.h:988
unsigned m
Definition: audioconvert.c:186
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:363
unsigned int elementid
Definition: matroskaenc.c:55
int reserved_size
-1 if appending to file
Definition: matroskaenc.c:62
#define MATROSKA_ID_CLUSTER
Definition: matroska.h:62
const char * av_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:736
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
Definition: matroskaenc.c:450
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:209
int64_t convergence_duration
Time difference in AVStream-&gt;time_base units from the pts of this packet to the point at which the ou...
Definition: avcodec.h:1111
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
static int mkv_blockgroup_size(int pkt_size)
Definition: matroskaenc.c:1175
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:2626
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
static int ebml_num_size(uint64_t num)
Calculate how many bytes are needed to represent a given number in EBML.
Definition: matroskaenc.c:161
int write_dts
Definition: matroskaenc.c:83
int final
Definition: wv.h:43
enum AVCodecID id
Definition: matroska.h:272
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: utils.c:3346
static int mkv_write_chapters(AVFormatContext *s)
Definition: matroskaenc.c:798
int side_data_elems
Definition: avcodec.h:1079
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:226
enum AVCodecID codec_id
Definition: mov_chan.c:433
uint32_t crc
Definition: wv.h:41
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:358
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:283
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
int64_t filepos
Definition: matroskaenc.c:60
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags)
Definition: matroskaenc.c:1299
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:107
#define MATROSKA_ID_TAG
Definition: matroska.h:166
goto fail
Definition: avfilter.c:963
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:1015
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
#define EBML_ID_EBMLVERSION
Definition: matroska.h:36
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
mkv_cuepoint * entries
Definition: matroskaenc.c:78
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:159
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:173
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:212
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:168
ret
Definition: avfilter.c:961
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:323
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREO_MODE_COUNT]
Definition: matroska.c:126
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:227
AVStream ** streams
Definition: avformat.h:1016
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:93
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:4005
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:167
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:357
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
AVDictionary * metadata
Definition: avformat.h:946
char * value
Definition: dict.h:82
#define FFMIN(a, b)
Definition: avcodec.h:925
const char * name
Definition: avformat.h:395
static char buffer[20]
Definition: seek-test.c:31
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1216
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:215
#define CONFIG_LIBVORBIS_ENCODER
Definition: config.h:1210
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:125
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
#define EBML_ID_VOID
Definition: matroska.h:45
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:203
#define OFFSET(x)
Definition: matroskaenc.c:1746
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2426
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:128
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
Definition: matroskaenc.c:205
Stream structure.
Definition: avformat.h:667
static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
Definition: matroskaenc.c:220
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:945
#define AV_RB64(x)
Definition: intreadwrite.h:292
sample_rate
AVS_Value src
Definition: avisynth_c.h:523
#define MATROSKA_ID_TRACKFLAGFORCED
Definition: matroska.h:100
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
enum AVMediaType codec_type
Definition: avcodec.h:1154
void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
#define FFMAX(a, b)
Definition: avcodec.h:923
static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, mkv_track *tracks, int num_tracks)
Definition: matroskaenc.c:407
enum AVCodecID codec_id
Definition: avcodec.h:1157
#define MATROSKA_ID_SEEKID
Definition: matroska.h:184
int sample_rate
samples per second
Definition: avcodec.h:1873
main external API structure.
Definition: avcodec.h:1146
#define MATROSKA_ID_BLOCK
Definition: matroska.h:199
#define MATROSKA_ID_INFO
Definition: matroska.h:56
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:176
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:170
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:361
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
#define MATROSKA_ID_TRACKS
Definition: matroska.h:57
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
void * buf
Definition: avisynth_c.h:594
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1004
int extradata_size
Definition: avcodec.h:1255
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:96
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1018
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:181
Describe the class of an AVClass context structure.
Definition: log.h:50
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:214
#define MAX_CUEPOINT_SIZE(num_tracks)
per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max
Definition: matroskaenc.c:128
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:191
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:115
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:571
rational number numerator/denominator
Definition: rational.h:43
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
Definition: matroskaenc.c:193
#define MATROSKA_ID_CUEDURATION
Definition: matroska.h:162
uint8_t * data
Definition: avcodec.h:1063
#define MATROSKA_ID_CUETIME
Definition: matroska.h:155
static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
Write the seek head to the file and free it.
Definition: matroskaenc.c:331
Recommmends skipping the specified number of samples.
Definition: avcodec.h:969
AVDictionary * metadata
Definition: avformat.h:1128
AVIOContext * dyn_bc
Definition: matroskaenc.c:93
AVMediaType
Definition: avutil.h:180
int avpriv_split_xiph_headers(uint8_t *extradata, int extradata_size, int first_header_size, uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use. ...
Definition: xiph.c:24
int64_t duration_offset
Definition: matroskaenc.c:99
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1472
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
#define snprintf
Definition: snprintf.h:34
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
#define AV_WB64(p, darg)
Definition: intreadwrite.h:303
#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:1246
#define AV_RB24(x)
Definition: intreadwrite.h:436
static int ass_get_duration(const uint8_t *p)
Definition: matroskaenc.c:1186
static void put_ebml_id(AVIOContext *pb, unsigned int id)
Definition: matroskaenc.c:139
#define type
int ff_flac_write_header(AVIOContext *pb, AVCodecContext *codec, int last_block)
void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
Definition: riffenc.c:203
static int put_wv_codecpriv(AVIOContext *pb, AVCodecContext *codec)
Definition: matroskaenc.c:478
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:61
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:218
static av_always_inline int ff_subtitles_next_line(const char *ptr)
Get the number of characters to increment to jump to the next line, or to the end of the string...
Definition: subtitles.h:111
static int flags
Definition: cpu.c:45
static const uint16_t scale[4]
#define MATROSKA_ID_FILENAME
Definition: matroska.h:208
#define MATROSKA_ID_BLOCKADDITIONAL
Definition: matroska.h:195
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:120
#define MATROSKA_ID_CODECID
Definition: matroska.h:88
static const AVCodecTag additional_video_tags[]
Definition: matroskaenc.c:1737
int64_t start
Definition: avformat.h:945
#define AV_RL64(x)
Definition: intreadwrite.h:317
#define MATROSKA_ID_VIDEOALPHAMODE
Definition: matroska.h:123
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:1628
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:159
static float t
Definition: muxing.c:123
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:185
#define MATROSKA_ID_CODECDELAY
Definition: matroska.h:94
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:216
common internal api header.
static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, const void *buf, int size)
Definition: matroskaenc.c:212
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
Definition: matroskaenc.c:501
static double c[64]
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1605
Main libavformat public API header.
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:724
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
Definition: riffenc.c:54
static void put_ebml_void(AVIOContext *pb, uint64_t size)
Write a void element of a given size.
Definition: matroskaenc.c:231
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:944
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:722
#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:1015
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:60
ASS as defined in Matroska.
Definition: avcodec.h:496
#define EBML_ID_HEADER
Definition: matroska.h:33
#define AV_RB32(x)
Definition: intreadwrite.h:258
struct AVOutputFormat * oformat
Definition: avformat.h:982
ebml_master cluster
Definition: matroskaenc.c:96
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:91
#define AVERROR_INVALIDDATA
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:152
mkv_seekhead_entry * entries
Definition: matroskaenc.c:64
#define AV_RL32(x)
Definition: intreadwrite.h:275
#define av_log2
Definition: intmath.h:89
#define MATROSKA_VIDEO_STEREO_MODE_COUNT
Definition: matroska.h:278
#define MATROSKA_ID_FILEUID
Definition: matroska.h:211
#define av_le2ne32(x)
Definition: bswap.h:96
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:225
Opaque data information usually sparse.
Definition: avcodec.h:2235
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:492
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
#define AVERROR(e)
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:72
int64_t dts
Decompression timestamp in AVStream-&gt;time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1062
static void mkv_flush_dynbuf(AVFormatContext *s)
Definition: matroskaenc.c:1457
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:219
void INT64 start
Definition: avisynth_c.h:594
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:169
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:68
#define MODE_MATROSKAv2
Definition: matroskaenc.c:87
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:106
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define MODE_WEBM
Definition: matroskaenc.c:88
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:1538
static AVPacket pkt
Definition: demuxing.c:52
#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:121
static mkv_cues * mkv_start_cues(int64_t segment_offset)
Definition: matroskaenc.c:375
int stream_index
Definition: avcodec.h:1065
int num_entries
Definition: matroskaenc.c:79
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:256
int64_t segment_offset
Definition: matroskaenc.c:95
#define CONFIG_LIBX264_ENCODER
Definition: config.h:1214
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:206
AVChapter ** chapters
Definition: avformat.h:1126
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
Definition: matroskaenc.c:248
mkv_seekhead * main_seekhead
Definition: matroskaenc.c:101
This structure stores compressed data.
Definition: avcodec.h:1040
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:85
int delay
Codec delay.
Definition: avcodec.h:1302
uint32_t blocksize
Definition: wv.h:35
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
Definition: matroskaenc.c:1705
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:266
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#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: avcodec.h:2278
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:27
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
#define MATROSKA_ID_DISCARDPADDING
Definition: matroska.h:203
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45