Libav
matroskadec.c
Go to the documentation of this file.
1 /*
2  * Matroska file demuxer
3  * Copyright (c) 2003-2008 The Libav Project
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
31 #include "config.h"
32 
33 #include <inttypes.h>
34 #include <stdio.h>
35 #if CONFIG_BZLIB
36 #include <bzlib.h>
37 #endif
38 #if CONFIG_ZLIB
39 #include <zlib.h>
40 #endif
41 
42 #include "libavutil/avstring.h"
43 #include "libavutil/dict.h"
44 #include "libavutil/intfloat.h"
45 #include "libavutil/intreadwrite.h"
46 #include "libavutil/lzo.h"
47 #include "libavutil/mathematics.h"
48 
49 #include "libavcodec/bytestream.h"
50 #include "libavcodec/flac.h"
51 #include "libavcodec/mpeg4audio.h"
52 
53 #include "avformat.h"
54 #include "avio_internal.h"
55 #include "internal.h"
56 #include "isom.h"
57 #include "matroska.h"
58 #include "oggdec.h"
59 /* For ff_codec_get_id(). */
60 #include "riff.h"
61 #include "rmsipr.h"
62 
63 typedef enum {
74 } EbmlType;
75 
76 typedef const struct EbmlSyntax {
77  uint32_t id;
81  union {
82  uint64_t u;
83  double f;
84  const char *s;
85  const struct EbmlSyntax *n;
86  } def;
87 } EbmlSyntax;
88 
89 typedef struct EbmlList {
90  int nb_elem;
91  void *elem;
92 } EbmlList;
93 
94 typedef struct EbmlBin {
95  int size;
97  int64_t pos;
98 } EbmlBin;
99 
100 typedef struct Ebml {
101  uint64_t version;
102  uint64_t max_size;
103  uint64_t id_length;
104  char *doctype;
105  uint64_t doctype_version;
106 } Ebml;
107 
108 typedef struct MatroskaTrackCompression {
109  uint64_t algo;
112 
113 typedef struct MatroskaTrackEncoding {
114  uint64_t scope;
115  uint64_t type;
118 
119 typedef struct MatroskaTrackVideo {
120  double frame_rate;
121  uint64_t display_width;
122  uint64_t display_height;
123  uint64_t pixel_width;
124  uint64_t pixel_height;
125  uint64_t fourcc;
126  uint64_t interlaced;
127  uint64_t field_order;
128  uint64_t stereo_mode;
130 
131 typedef struct MatroskaTrackAudio {
132  double samplerate;
134  uint64_t bitdepth;
135  uint64_t channels;
136 
137  /* real audio header (extracted from extradata) */
143  int pkt_cnt;
144  uint64_t buf_timecode;
147 
148 typedef struct MatroskaTrack {
149  uint64_t num;
150  uint64_t uid;
151  uint64_t type;
152  char *name;
153  char *codec_id;
155  char *language;
156  double time_scale;
158  uint64_t flag_default;
159  uint64_t flag_forced;
163  uint64_t codec_delay;
164 
166  int64_t end_timecode;
168 } MatroskaTrack;
169 
170 typedef struct MatroskaAttachment {
171  uint64_t uid;
172  char *filename;
173  char *mime;
175 
178 
179 typedef struct MatroskaChapter {
180  uint64_t start;
181  uint64_t end;
182  uint64_t uid;
183  char *title;
184 
187 
188 typedef struct MatroskaIndexPos {
189  uint64_t track;
190  uint64_t pos;
192 
193 typedef struct MatroskaIndex {
194  uint64_t time;
196 } MatroskaIndex;
197 
198 typedef struct MatroskaTag {
199  char *name;
200  char *string;
201  char *lang;
202  uint64_t def;
204 } MatroskaTag;
205 
206 typedef struct MatroskaTagTarget {
207  char *type;
208  uint64_t typevalue;
209  uint64_t trackuid;
210  uint64_t chapteruid;
211  uint64_t attachuid;
213 
214 typedef struct MatroskaTags {
217 } MatroskaTags;
218 
219 typedef struct MatroskaSeekhead {
220  uint64_t id;
221  uint64_t pos;
223 
224 typedef struct MatroskaLevel {
225  uint64_t start;
226  uint64_t length;
227 } MatroskaLevel;
228 
229 typedef struct MatroskaCluster {
230  uint64_t timecode;
233 
234 typedef struct MatroskaDemuxContext {
236 
237  /* EBML stuff */
240  int level_up;
241  uint32_t current_id;
242 
243  uint64_t time_scale;
244  double duration;
245  char *title;
252 
253  /* byte position of the segment inside the stream */
254  int64_t segment_start;
255 
256  /* the packet queue */
260 
261  int done;
262 
263  /* What to skip before effectively reading a packet. */
266 
267  /* File has a CUES element, but we defer parsing until it is needed. */
269 
273 
274  /* File has SSA subtitles which prevent incremental cluster parsing. */
277 
278 typedef struct MatroskaBlock {
279  uint64_t duration;
280  int64_t reference;
281  uint64_t non_simple;
283 } MatroskaBlock;
284 
286  { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml, version), { .u = EBML_VERSION } },
287  { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml, max_size), { .u = 8 } },
288  { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml, id_length), { .u = 4 } },
289  { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml, doctype), { .s = "(none)" } },
290  { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml, doctype_version), { .u = 1 } },
292  { EBML_ID_DOCTYPEVERSION, EBML_NONE },
293  { 0 }
294 };
295 
297  { EBML_ID_HEADER, EBML_NEST, 0, 0, { .n = ebml_header } },
298  { 0 }
299 };
300 
302  { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext, time_scale), { .u = 1000000 } },
304  { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext, title) },
306  { MATROSKA_ID_MUXINGAPP, EBML_NONE },
307  { MATROSKA_ID_DATEUTC, EBML_NONE },
308  { MATROSKA_ID_SEGMENTUID, EBML_NONE },
309  { 0 }
310 };
311 
313  { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT, 0, offsetof(MatroskaTrackVideo, frame_rate) },
314  { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo, display_width) },
315  { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo, display_height) },
316  { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo, pixel_width) },
317  { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo, pixel_height) },
320  { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
321  { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
322  { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
323  { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
327  { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
328  { 0 }
329 };
330 
332  { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT, 0, offsetof(MatroskaTrackAudio, samplerate), { .f = 8000.0 } },
333  { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, EBML_FLOAT, 0, offsetof(MatroskaTrackAudio, out_samplerate) },
334  { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio, bitdepth) },
335  { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio, channels), { .u = 1 } },
336  { 0 }
337 };
338 
342  { 0 }
343 };
344 
346  { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding, scope), { .u = 1 } },
347  { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding, type), { .u = 0 } },
348  { MATROSKA_ID_ENCODINGCOMPRESSION, EBML_NEST, 0, offsetof(MatroskaTrackEncoding, compression), { .n = matroska_track_encoding_compression } },
350  { 0 }
351 };
352 
354  { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack, encodings), { .n = matroska_track_encoding } },
355  { 0 }
356 };
357 
359  { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack, num) },
361  { MATROSKA_ID_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTrack, uid) },
364  { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack, codec_priv) },
365  { MATROSKA_ID_CODECDELAY, EBML_UINT, 0, offsetof(MatroskaTrack, codec_delay) },
366  { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack, language), { .s = "eng" } },
367  { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack, default_duration) },
368  { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT, 0, offsetof(MatroskaTrack, time_scale), { .f = 1.0 } },
369  { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack, flag_default), { .u = 1 } },
370  { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, offsetof(MatroskaTrack, flag_forced), { .u = 0 } },
371  { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack, video), { .n = matroska_track_video } },
372  { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack, audio), { .n = matroska_track_audio } },
373  { MATROSKA_ID_TRACKCONTENTENCODINGS, EBML_NEST, 0, 0, { .n = matroska_track_encodings } },
375  { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
376  { MATROSKA_ID_CODECNAME, EBML_NONE },
377  { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
378  { MATROSKA_ID_CODECINFOURL, EBML_NONE },
379  { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
380  { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
381  { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
382  { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
383  { 0 }
384 };
385 
387  { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext, tracks), { .n = matroska_track } },
388  { 0 }
389 };
390 
392  { MATROSKA_ID_FILEUID, EBML_UINT, 0, offsetof(MatroskaAttachment, uid) },
393  { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachment, filename) },
394  { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachment, mime) },
395  { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachment, bin) },
397  { 0 }
398 };
399 
401  { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachment), offsetof(MatroskaDemuxContext, attachments), { .n = matroska_attachment } },
402  { 0 }
403 };
404 
406  { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter, title) },
408  { 0 }
409 };
410 
412  { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter, start), { .u = AV_NOPTS_VALUE } },
413  { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter, end), { .u = AV_NOPTS_VALUE } },
414  { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter, uid) },
415  { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, { .n = matroska_chapter_display } },
417  { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
418  { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
419  { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
420  { 0 }
421 };
422 
424  { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext, chapters), { .n = matroska_chapter_entry } },
426  { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
427  { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
428  { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
429  { 0 }
430 };
431 
433  { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, { .n = matroska_chapter } },
434  { 0 }
435 };
436 
438  { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos, track) },
441  { 0 }
442 };
443 
445  { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex, time) },
446  { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex, pos), { .n = matroska_index_pos } },
447  { 0 }
448 };
449 
451  { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext, index), { .n = matroska_index_entry } },
452  { 0 }
453 };
454 
456  { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag, name) },
457  { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag, string) },
458  { MATROSKA_ID_TAGLANG, EBML_STR, 0, offsetof(MatroskaTag, lang), { .s = "und" } },
459  { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTag, def) },
461  { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag, sub), { .n = matroska_simpletag } },
462  { 0 }
463 };
464 
467  { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, offsetof(MatroskaTagTarget, typevalue), { .u = 50 } },
468  { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTagTarget, trackuid) },
469  { MATROSKA_ID_TAGTARGETS_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaTagTarget, chapteruid) },
470  { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, offsetof(MatroskaTagTarget, attachuid) },
471  { 0 }
472 };
473 
475  { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags, tag), { .n = matroska_simpletag } },
476  { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, offsetof(MatroskaTags, target), { .n = matroska_tagtargets } },
477  { 0 }
478 };
479 
481  { MATROSKA_ID_TAG, EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext, tags), { .n = matroska_tag } },
482  { 0 }
483 };
484 
486  { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead, id) },
487  { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead, pos), { .u = -1 } },
488  { 0 }
489 };
490 
492  { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext, seekhead), { .n = matroska_seekhead_entry } },
493  { 0 }
494 };
495 
497  { MATROSKA_ID_INFO, EBML_NEST, 0, 0, { .n = matroska_info } },
498  { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, { .n = matroska_tracks } },
499  { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, { .n = matroska_attachments } },
500  { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, { .n = matroska_chapters } },
501  { MATROSKA_ID_CUES, EBML_NEST, 0, 0, { .n = matroska_index } },
502  { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, { .n = matroska_tags } },
503  { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, { .n = matroska_seekhead } },
505  { 0 }
506 };
507 
509  { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, { .n = matroska_segment } },
510  { 0 }
511 };
512 
514  { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock, bin) },
515  { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock, bin) },
517  { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock, reference) },
519  { 1, EBML_UINT, 0, offsetof(MatroskaBlock, non_simple), { .u = 1 } },
520  { 0 }
521 };
522 
524  { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, offsetof(MatroskaCluster, timecode) },
525  { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
526  { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
528  { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
529  { 0 }
530 };
531 
533  { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, { .n = matroska_cluster } },
535  { MATROSKA_ID_CUES, EBML_NONE },
536  { MATROSKA_ID_TAGS, EBML_NONE },
537  { MATROSKA_ID_SEEKHEAD, EBML_NONE },
538  { 0 }
539 };
540 
542  { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, offsetof(MatroskaCluster, timecode) },
543  { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
544  { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
546  { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
547  { MATROSKA_ID_INFO, EBML_NONE },
548  { MATROSKA_ID_CUES, EBML_NONE },
549  { MATROSKA_ID_TAGS, EBML_NONE },
550  { MATROSKA_ID_SEEKHEAD, EBML_NONE },
552  { 0 }
553 };
554 
556  { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, offsetof(MatroskaCluster, timecode) },
558  { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP },
560  { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
561  { 0 }
562 };
563 
565  { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, { .n = matroska_cluster_incremental } },
567  { MATROSKA_ID_CUES, EBML_NONE },
568  { MATROSKA_ID_TAGS, EBML_NONE },
569  { MATROSKA_ID_SEEKHEAD, EBML_NONE },
570  { 0 }
571 };
572 
573 static const char *const matroska_doctypes[] = { "matroska", "webm" };
574 
575 static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos)
576 {
577  AVIOContext *pb = matroska->ctx->pb;
578  uint32_t id;
579  matroska->current_id = 0;
580  matroska->num_levels = 0;
581 
582  /* seek to next position to resync from */
583  if (avio_seek(pb, last_pos + 1, SEEK_SET) < 0)
584  goto eof;
585 
586  id = avio_rb32(pb);
587 
588  // try to find a toplevel element
589  while (!pb->eof_reached) {
590  if (id == MATROSKA_ID_INFO || id == MATROSKA_ID_TRACKS ||
591  id == MATROSKA_ID_CUES || id == MATROSKA_ID_TAGS ||
593  id == MATROSKA_ID_CLUSTER || id == MATROSKA_ID_CHAPTERS) {
594  matroska->current_id = id;
595  return 0;
596  }
597  id = (id << 8) | avio_r8(pb);
598  }
599 
600 eof:
601  matroska->done = 1;
602  return AVERROR_EOF;
603 }
604 
605 /*
606  * Return: Whether we reached the end of a level in the hierarchy or not.
607  */
609 {
610  AVIOContext *pb = matroska->ctx->pb;
611  int64_t pos = avio_tell(pb);
612 
613  if (matroska->num_levels > 0) {
614  MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
615  if (pos - level->start >= level->length || matroska->current_id) {
616  matroska->num_levels--;
617  return 1;
618  }
619  }
620  return 0;
621 }
622 
623 /*
624  * Read: an "EBML number", which is defined as a variable-length
625  * array of bytes. The first byte indicates the length by giving a
626  * number of 0-bits followed by a one. The position of the first
627  * "one" bit inside the first byte indicates the length of this
628  * number.
629  * Returns: number of bytes read, < 0 on error
630  */
632  int max_size, uint64_t *number)
633 {
634  int read = 1, n = 1;
635  uint64_t total = 0;
636 
637  /* The first byte tells us the length in bytes - avio_r8() can normally
638  * return 0, but since that's not a valid first ebmlID byte, we can
639  * use it safely here to catch EOS. */
640  if (!(total = avio_r8(pb))) {
641  /* we might encounter EOS here */
642  if (!pb->eof_reached) {
643  int64_t pos = avio_tell(pb);
644  av_log(matroska->ctx, AV_LOG_ERROR,
645  "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
646  pos, pos);
647  return pb->error ? pb->error : AVERROR(EIO);
648  }
649  return AVERROR_EOF;
650  }
651 
652  /* get the length of the EBML number */
653  read = 8 - ff_log2_tab[total];
654  if (read > max_size) {
655  int64_t pos = avio_tell(pb) - 1;
656  av_log(matroska->ctx, AV_LOG_ERROR,
657  "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
658  (uint8_t) total, pos, pos);
659  return AVERROR_INVALIDDATA;
660  }
661 
662  /* read out length */
663  total ^= 1 << ff_log2_tab[total];
664  while (n++ < read)
665  total = (total << 8) | avio_r8(pb);
666 
667  *number = total;
668 
669  return read;
670 }
671 
678  uint64_t *number)
679 {
680  int res = ebml_read_num(matroska, pb, 8, number);
681  if (res > 0 && *number + 1 == 1ULL << (7 * res))
682  *number = 0xffffffffffffffULL;
683  return res;
684 }
685 
686 /*
687  * Read the next element as an unsigned int.
688  * 0 is success, < 0 is failure.
689  */
690 static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
691 {
692  int n = 0;
693 
694  if (size > 8)
695  return AVERROR_INVALIDDATA;
696 
697  /* big-endian ordering; build up number */
698  *num = 0;
699  while (n++ < size)
700  *num = (*num << 8) | avio_r8(pb);
701 
702  return 0;
703 }
704 
705 /*
706  * Read the next element as a float.
707  * 0 is success, < 0 is failure.
708  */
709 static int ebml_read_float(AVIOContext *pb, int size, double *num)
710 {
711  if (size == 0)
712  *num = 0;
713  else if (size == 4)
714  *num = av_int2float(avio_rb32(pb));
715  else if (size == 8)
716  *num = av_int2double(avio_rb64(pb));
717  else
718  return AVERROR_INVALIDDATA;
719 
720  return 0;
721 }
722 
723 /*
724  * Read the next element as an ASCII string.
725  * 0 is success, < 0 is failure.
726  */
727 static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
728 {
729  char *res;
730 
731  /* EBML strings are usually not 0-terminated, so we allocate one
732  * byte more, read the string and NULL-terminate it ourselves. */
733  if (!(res = av_malloc(size + 1)))
734  return AVERROR(ENOMEM);
735  if (avio_read(pb, (uint8_t *) res, size) != size) {
736  av_free(res);
737  return AVERROR(EIO);
738  }
739  (res)[size] = '\0';
740  av_free(*str);
741  *str = res;
742 
743  return 0;
744 }
745 
746 /*
747  * Read the next element as binary data.
748  * 0 is success, < 0 is failure.
749  */
750 static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
751 {
752  av_free(bin->data);
753  bin->size = 0;
754 
755  if (!(bin->data = av_mallocz(length + AV_INPUT_BUFFER_PADDING_SIZE)))
756  return AVERROR(ENOMEM);
757 
758  bin->pos = avio_tell(pb);
759  if (avio_read(pb, bin->data, length) != length) {
760  av_freep(&bin->data);
761  return AVERROR(EIO);
762  }
763 
764  bin->size = length;
765 
766  return 0;
767 }
768 
769 /*
770  * Read the next element, but only the header. The contents
771  * are supposed to be sub-elements which can be read separately.
772  * 0 is success, < 0 is failure.
773  */
774 static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
775 {
776  AVIOContext *pb = matroska->ctx->pb;
778 
779  if (matroska->num_levels >= EBML_MAX_DEPTH) {
780  av_log(matroska->ctx, AV_LOG_ERROR,
781  "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
782  return AVERROR(ENOSYS);
783  }
784 
785  level = &matroska->levels[matroska->num_levels++];
786  level->start = avio_tell(pb);
787  level->length = length;
788 
789  return 0;
790 }
791 
792 /*
793  * Read signed/unsigned "EBML" numbers.
794  * Return: number of bytes processed, < 0 on error
795  */
797  uint8_t *data, uint32_t size, uint64_t *num)
798 {
799  AVIOContext pb;
800  ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
801  return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
802 }
803 
804 /*
805  * Same as above, but signed.
806  */
808  uint8_t *data, uint32_t size, int64_t *num)
809 {
810  uint64_t unum;
811  int res;
812 
813  /* read as unsigned number first */
814  if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
815  return res;
816 
817  /* make signed (weird way) */
818  *num = unum - ((1LL << (7 * res - 1)) - 1);
819 
820  return res;
821 }
822 
823 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
824  EbmlSyntax *syntax, void *data);
825 
826 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
827  uint32_t id, void *data)
828 {
829  int i;
830  for (i = 0; syntax[i].id; i++)
831  if (id == syntax[i].id)
832  break;
833  if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
834  matroska->num_levels > 0 &&
835  matroska->levels[matroska->num_levels - 1].length == 0xffffffffffffff)
836  return 0; // we reached the end of an unknown size cluster
837  if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
838  av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%"PRIX32"\n", id);
839  if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
840  return AVERROR_INVALIDDATA;
841  }
842  return ebml_parse_elem(matroska, &syntax[i], data);
843 }
844 
845 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
846  void *data)
847 {
848  if (!matroska->current_id) {
849  uint64_t id;
850  int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
851  if (res < 0)
852  return res;
853  matroska->current_id = id | 1 << 7 * res;
854  }
855  return ebml_parse_id(matroska, syntax, matroska->current_id, data);
856 }
857 
858 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
859  void *data)
860 {
861  int i, res = 0;
862 
863  for (i = 0; syntax[i].id; i++)
864  switch (syntax[i].type) {
865  case EBML_UINT:
866  *(uint64_t *) ((char *) data + syntax[i].data_offset) = syntax[i].def.u;
867  break;
868  case EBML_FLOAT:
869  *(double *) ((char *) data + syntax[i].data_offset) = syntax[i].def.f;
870  break;
871  case EBML_STR:
872  case EBML_UTF8:
873  // the default may be NULL
874  if (syntax[i].def.s) {
875  uint8_t **dst = (uint8_t **) ((uint8_t *) data + syntax[i].data_offset);
876  *dst = av_strdup(syntax[i].def.s);
877  if (!*dst)
878  return AVERROR(ENOMEM);
879  }
880  break;
881  }
882 
883  while (!res && !ebml_level_end(matroska))
884  res = ebml_parse(matroska, syntax, data);
885 
886  return res;
887 }
888 
890  EbmlSyntax *syntax, void *data)
891 {
892  static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
893  [EBML_UINT] = 8,
894  [EBML_FLOAT] = 8,
895  // max. 16 MB for strings
896  [EBML_STR] = 0x1000000,
897  [EBML_UTF8] = 0x1000000,
898  // max. 256 MB for binary data
899  [EBML_BIN] = 0x10000000,
900  // no limits for anything else
901  };
902  AVIOContext *pb = matroska->ctx->pb;
903  uint32_t id = syntax->id;
904  uint64_t length;
905  int res;
906 
907  data = (char *) data + syntax->data_offset;
908  if (syntax->list_elem_size) {
909  EbmlList *list = data;
910  if ((res = av_reallocp_array(&list->elem,
911  list->nb_elem + 1,
912  syntax->list_elem_size)) < 0) {
913  list->nb_elem = 0;
914  return res;
915  }
916  data = (char *) list->elem + list->nb_elem * syntax->list_elem_size;
917  memset(data, 0, syntax->list_elem_size);
918  list->nb_elem++;
919  }
920 
921  if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
922  matroska->current_id = 0;
923  if ((res = ebml_read_length(matroska, pb, &length)) < 0)
924  return res;
925  if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
926  av_log(matroska->ctx, AV_LOG_ERROR,
927  "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
928  length, max_lengths[syntax->type], syntax->type);
929  return AVERROR_INVALIDDATA;
930  }
931  }
932 
933  switch (syntax->type) {
934  case EBML_UINT:
935  res = ebml_read_uint(pb, length, data);
936  break;
937  case EBML_FLOAT:
938  res = ebml_read_float(pb, length, data);
939  break;
940  case EBML_STR:
941  case EBML_UTF8:
942  res = ebml_read_ascii(pb, length, data);
943  break;
944  case EBML_BIN:
945  res = ebml_read_binary(pb, length, data);
946  break;
947  case EBML_NEST:
948  if ((res = ebml_read_master(matroska, length)) < 0)
949  return res;
950  if (id == MATROSKA_ID_SEGMENT)
951  matroska->segment_start = avio_tell(matroska->ctx->pb);
952  return ebml_parse_nest(matroska, syntax->def.n, data);
953  case EBML_PASS:
954  return ebml_parse_id(matroska, syntax->def.n, id, data);
955  case EBML_STOP:
956  return 1;
957  default:
958  return avio_skip(pb, length) < 0 ? AVERROR(EIO) : 0;
959  }
960  if (res == AVERROR_INVALIDDATA)
961  av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
962  else if (res == AVERROR(EIO))
963  av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
964  return res;
965 }
966 
967 static void ebml_free(EbmlSyntax *syntax, void *data)
968 {
969  int i, j;
970  for (i = 0; syntax[i].id; i++) {
971  void *data_off = (char *) data + syntax[i].data_offset;
972  switch (syntax[i].type) {
973  case EBML_STR:
974  case EBML_UTF8:
975  av_freep(data_off);
976  break;
977  case EBML_BIN:
978  av_freep(&((EbmlBin *) data_off)->data);
979  break;
980  case EBML_NEST:
981  if (syntax[i].list_elem_size) {
982  EbmlList *list = data_off;
983  char *ptr = list->elem;
984  for (j = 0; j < list->nb_elem;
985  j++, ptr += syntax[i].list_elem_size)
986  ebml_free(syntax[i].def.n, ptr);
987  av_free(list->elem);
988  } else
989  ebml_free(syntax[i].def.n, data_off);
990  default:
991  break;
992  }
993  }
994 }
995 
996 /*
997  * Autodetecting...
998  */
1000 {
1001  uint64_t total = 0;
1002  int len_mask = 0x80, size = 1, n = 1, i;
1003 
1004  /* EBML header? */
1005  if (AV_RB32(p->buf) != EBML_ID_HEADER)
1006  return 0;
1007 
1008  /* length of header */
1009  total = p->buf[4];
1010  while (size <= 8 && !(total & len_mask)) {
1011  size++;
1012  len_mask >>= 1;
1013  }
1014  if (size > 8)
1015  return 0;
1016  total &= (len_mask - 1);
1017  while (n < size)
1018  total = (total << 8) | p->buf[4 + n++];
1019 
1020  /* Does the probe data contain the whole header? */
1021  if (p->buf_size < 4 + size + total)
1022  return 0;
1023 
1024  /* The header should contain a known document type. For now,
1025  * we don't parse the whole header but simply check for the
1026  * availability of that array of characters inside the header.
1027  * Not fully fool-proof, but good enough. */
1028  for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
1029  int probelen = strlen(matroska_doctypes[i]);
1030  if (total < probelen)
1031  continue;
1032  for (n = 4 + size; n <= 4 + size + total - probelen; n++)
1033  if (!memcmp(p->buf + n, matroska_doctypes[i], probelen))
1034  return AVPROBE_SCORE_MAX;
1035  }
1036 
1037  // probably valid EBML header but no recognized doctype
1038  return AVPROBE_SCORE_EXTENSION;
1039 }
1040 
1042  int num)
1043 {
1044  MatroskaTrack *tracks = matroska->tracks.elem;
1045  int i;
1046 
1047  for (i = 0; i < matroska->tracks.nb_elem; i++)
1048  if (tracks[i].num == num)
1049  return &tracks[i];
1050 
1051  av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
1052  return NULL;
1053 }
1054 
1055 static int matroska_decode_buffer(uint8_t **buf, int *buf_size,
1056  MatroskaTrack *track)
1057 {
1058  MatroskaTrackEncoding *encodings = track->encodings.elem;
1059  uint8_t *data = *buf;
1060  int isize = *buf_size;
1061  uint8_t *pkt_data = NULL;
1062  uint8_t av_unused *newpktdata;
1063  int pkt_size = isize;
1064  int result = 0;
1065  int olen;
1066 
1067  if (pkt_size >= 10000000)
1068  return AVERROR_INVALIDDATA;
1069 
1070  switch (encodings[0].compression.algo) {
1072  {
1073  int header_size = encodings[0].compression.settings.size;
1074  uint8_t *header = encodings[0].compression.settings.data;
1075 
1076  if (!header_size)
1077  return 0;
1078 
1079  pkt_size = isize + header_size;
1080  pkt_data = av_malloc(pkt_size);
1081  if (!pkt_data)
1082  return AVERROR(ENOMEM);
1083 
1084  memcpy(pkt_data, header, header_size);
1085  memcpy(pkt_data + header_size, data, isize);
1086  break;
1087  }
1088 #if CONFIG_LZO
1090  do {
1091  olen = pkt_size *= 3;
1092  newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING);
1093  if (!newpktdata) {
1094  result = AVERROR(ENOMEM);
1095  goto failed;
1096  }
1097  pkt_data = newpktdata;
1098  result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
1099  } while (result == AV_LZO_OUTPUT_FULL && pkt_size < 10000000);
1100  if (result) {
1101  result = AVERROR_INVALIDDATA;
1102  goto failed;
1103  }
1104  pkt_size -= olen;
1105  break;
1106 #endif
1107 #if CONFIG_ZLIB
1109  {
1110  z_stream zstream = { 0 };
1111  if (inflateInit(&zstream) != Z_OK)
1112  return -1;
1113  zstream.next_in = data;
1114  zstream.avail_in = isize;
1115  do {
1116  pkt_size *= 3;
1117  newpktdata = av_realloc(pkt_data, pkt_size);
1118  if (!newpktdata) {
1119  inflateEnd(&zstream);
1120  goto failed;
1121  }
1122  pkt_data = newpktdata;
1123  zstream.avail_out = pkt_size - zstream.total_out;
1124  zstream.next_out = pkt_data + zstream.total_out;
1125  result = inflate(&zstream, Z_NO_FLUSH);
1126  } while (result == Z_OK && pkt_size < 10000000);
1127  pkt_size = zstream.total_out;
1128  inflateEnd(&zstream);
1129  if (result != Z_STREAM_END) {
1130  if (result == Z_MEM_ERROR)
1131  result = AVERROR(ENOMEM);
1132  else
1133  result = AVERROR_INVALIDDATA;
1134  goto failed;
1135  }
1136  break;
1137  }
1138 #endif
1139 #if CONFIG_BZLIB
1141  {
1142  bz_stream bzstream = { 0 };
1143  if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1144  return -1;
1145  bzstream.next_in = data;
1146  bzstream.avail_in = isize;
1147  do {
1148  pkt_size *= 3;
1149  newpktdata = av_realloc(pkt_data, pkt_size);
1150  if (!newpktdata) {
1151  BZ2_bzDecompressEnd(&bzstream);
1152  goto failed;
1153  }
1154  pkt_data = newpktdata;
1155  bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1156  bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1157  result = BZ2_bzDecompress(&bzstream);
1158  } while (result == BZ_OK && pkt_size < 10000000);
1159  pkt_size = bzstream.total_out_lo32;
1160  BZ2_bzDecompressEnd(&bzstream);
1161  if (result != BZ_STREAM_END) {
1162  if (result == BZ_MEM_ERROR)
1163  result = AVERROR(ENOMEM);
1164  else
1165  result = AVERROR_INVALIDDATA;
1166  goto failed;
1167  }
1168  break;
1169  }
1170 #endif
1171  default:
1172  return AVERROR_INVALIDDATA;
1173  }
1174 
1175  *buf = pkt_data;
1176  *buf_size = pkt_size;
1177  return 0;
1178 
1179 failed:
1180  av_free(pkt_data);
1181  return result;
1182 }
1183 
1185  AVPacket *pkt, uint64_t display_duration)
1186 {
1187  AVBufferRef *line;
1188  char *layer, *ptr = pkt->data, *end = ptr + pkt->size;
1189 
1190  for (; *ptr != ',' && ptr < end - 1; ptr++)
1191  ;
1192  if (*ptr == ',')
1193  layer = ++ptr;
1194  for (; *ptr != ',' && ptr < end - 1; ptr++)
1195  ;
1196  if (*ptr == ',') {
1197  int64_t end_pts = pkt->pts + display_duration;
1198  int sc = matroska->time_scale * pkt->pts / 10000000;
1199  int ec = matroska->time_scale * end_pts / 10000000;
1200  int sh, sm, ss, eh, em, es, len;
1201  sh = sc / 360000;
1202  sc -= 360000 * sh;
1203  sm = sc / 6000;
1204  sc -= 6000 * sm;
1205  ss = sc / 100;
1206  sc -= 100 * ss;
1207  eh = ec / 360000;
1208  ec -= 360000 * eh;
1209  em = ec / 6000;
1210  ec -= 6000 * em;
1211  es = ec / 100;
1212  ec -= 100 * es;
1213  *ptr++ = '\0';
1214  len = 50 + end - ptr + AV_INPUT_BUFFER_PADDING_SIZE;
1215  if (!(line = av_buffer_alloc(len)))
1216  return;
1217  snprintf(line->data, len,
1218  "Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1219  layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1220  av_buffer_unref(&pkt->buf);
1221  pkt->buf = line;
1222  pkt->data = line->data;
1223  pkt->size = strlen(line->data);
1224  }
1225 }
1226 
1228 {
1229  int old_size = out->size;
1230  int ret = av_grow_packet(out, in->size);
1231  if (ret < 0)
1232  return ret;
1233 
1234  memcpy(out->data + old_size, in->data, in->size);
1235 
1236  av_packet_unref(in);
1237  av_free(in);
1238  return 0;
1239 }
1240 
1242  AVDictionary **metadata, char *prefix)
1243 {
1244  MatroskaTag *tags = list->elem;
1245  char key[1024];
1246  int i;
1247 
1248  for (i = 0; i < list->nb_elem; i++) {
1249  const char *lang = tags[i].lang &&
1250  strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
1251 
1252  if (!tags[i].name) {
1253  av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
1254  continue;
1255  }
1256  if (prefix)
1257  snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
1258  else
1259  av_strlcpy(key, tags[i].name, sizeof(key));
1260  if (tags[i].def || !lang) {
1261  av_dict_set(metadata, key, tags[i].string, 0);
1262  if (tags[i].sub.nb_elem)
1263  matroska_convert_tag(s, &tags[i].sub, metadata, key);
1264  }
1265  if (lang) {
1266  av_strlcat(key, "-", sizeof(key));
1267  av_strlcat(key, lang, sizeof(key));
1268  av_dict_set(metadata, key, tags[i].string, 0);
1269  if (tags[i].sub.nb_elem)
1270  matroska_convert_tag(s, &tags[i].sub, metadata, key);
1271  }
1272  }
1274 }
1275 
1277 {
1278  MatroskaDemuxContext *matroska = s->priv_data;
1279  MatroskaTags *tags = matroska->tags.elem;
1280  int i, j;
1281 
1282  for (i = 0; i < matroska->tags.nb_elem; i++) {
1283  if (tags[i].target.attachuid) {
1284  MatroskaAttachment *attachment = matroska->attachments.elem;
1285  int found = 0;
1286  for (j = 0; j < matroska->attachments.nb_elem; j++) {
1287  if (attachment[j].uid == tags[i].target.attachuid &&
1288  attachment[j].stream) {
1289  matroska_convert_tag(s, &tags[i].tag,
1290  &attachment[j].stream->metadata, NULL);
1291  found = 1;
1292  }
1293  }
1294  if (!found) {
1296  "The tags at index %d refer to a "
1297  "non-existent attachment %"PRId64".\n",
1298  i, tags[i].target.attachuid);
1299  }
1300  } else if (tags[i].target.chapteruid) {
1301  MatroskaChapter *chapter = matroska->chapters.elem;
1302  int found = 0;
1303  for (j = 0; j < matroska->chapters.nb_elem; j++) {
1304  if (chapter[j].uid == tags[i].target.chapteruid &&
1305  chapter[j].chapter) {
1306  matroska_convert_tag(s, &tags[i].tag,
1307  &chapter[j].chapter->metadata, NULL);
1308  found = 1;
1309  }
1310  }
1311  if (!found) {
1313  "The tags at index %d refer to a non-existent chapter "
1314  "%"PRId64".\n",
1315  i, tags[i].target.chapteruid);
1316  }
1317  } else if (tags[i].target.trackuid) {
1318  MatroskaTrack *track = matroska->tracks.elem;
1319  int found = 0;
1320  for (j = 0; j < matroska->tracks.nb_elem; j++) {
1321  if (track[j].uid == tags[i].target.trackuid &&
1322  track[j].stream) {
1323  matroska_convert_tag(s, &tags[i].tag,
1324  &track[j].stream->metadata, NULL);
1325  found = 1;
1326  }
1327  }
1328  if (!found) {
1330  "The tags at index %d refer to a non-existent track "
1331  "%"PRId64".\n",
1332  i, tags[i].target.trackuid);
1333  }
1334  } else {
1335  matroska_convert_tag(s, &tags[i].tag, &s->metadata,
1336  tags[i].target.type);
1337  }
1338  }
1339 }
1340 
1342  int idx)
1343 {
1344  EbmlList *seekhead_list = &matroska->seekhead;
1345  uint32_t level_up = matroska->level_up;
1346  uint32_t saved_id = matroska->current_id;
1347  MatroskaSeekhead *seekhead = seekhead_list->elem;
1348  int64_t before_pos = avio_tell(matroska->ctx->pb);
1350  int64_t offset;
1351  int ret = 0;
1352 
1353  if (idx >= seekhead_list->nb_elem ||
1354  seekhead[idx].id == MATROSKA_ID_SEEKHEAD ||
1355  seekhead[idx].id == MATROSKA_ID_CLUSTER)
1356  return 0;
1357 
1358  /* seek */
1359  offset = seekhead[idx].pos + matroska->segment_start;
1360  if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
1361  /* We don't want to lose our seekhead level, so we add
1362  * a dummy. This is a crude hack. */
1363  if (matroska->num_levels == EBML_MAX_DEPTH) {
1364  av_log(matroska->ctx, AV_LOG_INFO,
1365  "Max EBML element depth (%d) reached, "
1366  "cannot parse further.\n", EBML_MAX_DEPTH);
1367  ret = AVERROR_INVALIDDATA;
1368  } else {
1369  level.start = 0;
1370  level.length = (uint64_t) -1;
1371  matroska->levels[matroska->num_levels] = level;
1372  matroska->num_levels++;
1373  matroska->current_id = 0;
1374 
1375  ret = ebml_parse(matroska, matroska_segment, matroska);
1376 
1377  /* remove dummy level */
1378  while (matroska->num_levels) {
1379  uint64_t length = matroska->levels[--matroska->num_levels].length;
1380  if (length == (uint64_t) -1)
1381  break;
1382  }
1383  }
1384  }
1385  /* seek back */
1386  avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
1387  matroska->level_up = level_up;
1388  matroska->current_id = saved_id;
1389 
1390  return ret;
1391 }
1392 
1394 {
1395  EbmlList *seekhead_list = &matroska->seekhead;
1396  int64_t before_pos = avio_tell(matroska->ctx->pb);
1397  int i;
1398 
1399  // we should not do any seeking in the streaming case
1400  if (!matroska->ctx->pb->seekable ||
1401  (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
1402  return;
1403 
1404  for (i = 0; i < seekhead_list->nb_elem; i++) {
1405  MatroskaSeekhead *seekhead = seekhead_list->elem;
1406  if (seekhead[i].pos <= before_pos)
1407  continue;
1408 
1409  // defer cues parsing until we actually need cue data.
1410  if (seekhead[i].id == MATROSKA_ID_CUES) {
1411  matroska->cues_parsing_deferred = 1;
1412  continue;
1413  }
1414 
1415  if (matroska_parse_seekhead_entry(matroska, i) < 0)
1416  break;
1417  }
1418 }
1419 
1421 {
1422  EbmlList *seekhead_list = &matroska->seekhead;
1423  MatroskaSeekhead *seekhead = seekhead_list->elem;
1424  EbmlList *index_list;
1426  int index_scale = 1;
1427  int i, j;
1428 
1429  for (i = 0; i < seekhead_list->nb_elem; i++)
1430  if (seekhead[i].id == MATROSKA_ID_CUES)
1431  break;
1432  assert(i <= seekhead_list->nb_elem);
1433 
1434  matroska_parse_seekhead_entry(matroska, i);
1435 
1436  index_list = &matroska->index;
1437  index = index_list->elem;
1438  if (index_list->nb_elem &&
1439  index[0].time > 1E14 / matroska->time_scale) {
1440  av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
1441  index_scale = matroska->time_scale;
1442  }
1443  for (i = 0; i < index_list->nb_elem; i++) {
1444  EbmlList *pos_list = &index[i].pos;
1445  MatroskaIndexPos *pos = pos_list->elem;
1446  for (j = 0; j < pos_list->nb_elem; j++) {
1447  MatroskaTrack *track = matroska_find_track_by_num(matroska,
1448  pos[j].track);
1449  if (track && track->stream)
1450  av_add_index_entry(track->stream,
1451  pos[j].pos + matroska->segment_start,
1452  index[i].time / index_scale, 0, 0,
1454  }
1455  }
1456 }
1457 
1459 {
1460  static const char *const aac_profiles[] = { "MAIN", "LC", "SSR" };
1461  int profile;
1462 
1463  for (profile = 0; profile < FF_ARRAY_ELEMS(aac_profiles); profile++)
1464  if (strstr(codec_id, aac_profiles[profile]))
1465  break;
1466  return profile + 1;
1467 }
1468 
1469 static int matroska_aac_sri(int samplerate)
1470 {
1471  int sri;
1472 
1473  for (sri = 0; sri < FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
1474  if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
1475  break;
1476  return sri;
1477 }
1478 
1480  MatroskaTrack *track,
1481  int *offset)
1482 {
1483  AVStream *st = track->stream;
1484  uint8_t *p = track->codec_priv.data;
1485  int size = track->codec_priv.size;
1486 
1487  if (size < 8 + FLAC_STREAMINFO_SIZE || p[4] & 0x7f) {
1488  av_log(s, AV_LOG_WARNING, "Invalid FLAC private data\n");
1489  track->codec_priv.size = 0;
1490  return 0;
1491  }
1492  *offset = 8;
1493  track->codec_priv.size = 8 + FLAC_STREAMINFO_SIZE;
1494 
1495  p += track->codec_priv.size;
1496  size -= track->codec_priv.size;
1497 
1498  /* parse the remaining metadata blocks if present */
1499  while (size >= 4) {
1500  int block_last, block_type, block_size;
1501 
1502  flac_parse_block_header(p, &block_last, &block_type, &block_size);
1503 
1504  p += 4;
1505  size -= 4;
1506  if (block_size > size)
1507  return 0;
1508 
1509  /* check for the channel mask */
1510  if (block_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
1511  AVDictionary *dict = NULL;
1512  AVDictionaryEntry *chmask;
1513 
1514  ff_vorbis_comment(s, &dict, p, block_size, 0);
1515  chmask = av_dict_get(dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
1516  if (chmask) {
1517  uint64_t mask = strtol(chmask->value, NULL, 0);
1518  if (!mask || mask & ~0x3ffffULL) {
1520  "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
1521  } else
1522  st->codecpar->channel_layout = mask;
1523  }
1524  av_dict_free(&dict);
1525  }
1526 
1527  p += block_size;
1528  size -= block_size;
1529  }
1530 
1531  return 0;
1532 }
1533 
1534 static int mkv_field_order(int64_t field_order)
1535 {
1536  switch (field_order) {
1538  return AV_FIELD_PROGRESSIVE;
1540  return AV_FIELD_UNKNOWN;
1542  return AV_FIELD_TT;
1544  return AV_FIELD_BB;
1546  return AV_FIELD_BT;
1548  return AV_FIELD_TB;
1549  default:
1550  return AV_FIELD_UNKNOWN;
1551  }
1552 }
1553 
1554 static void mkv_stereo_mode_display_mul(int stereo_mode,
1555  int *h_width, int *h_height)
1556 {
1557  switch (stereo_mode) {
1563  break;
1568  *h_width = 2;
1569  break;
1574  *h_height = 2;
1575  break;
1576  }
1577 }
1578 
1580 {
1581  MatroskaDemuxContext *matroska = s->priv_data;
1582  MatroskaTrack *tracks = matroska->tracks.elem;
1583  AVStream *st;
1584  int i, j, ret;
1585 
1586  for (i = 0; i < matroska->tracks.nb_elem; i++) {
1587  MatroskaTrack *track = &tracks[i];
1589  EbmlList *encodings_list = &track->encodings;
1590  MatroskaTrackEncoding *encodings = encodings_list->elem;
1591  uint8_t *extradata = NULL;
1592  int extradata_size = 0;
1593  int extradata_offset = 0;
1594  AVIOContext b;
1595 
1596  /* Apply some sanity checks. */
1597  if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
1598  track->type != MATROSKA_TRACK_TYPE_AUDIO &&
1599  track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
1600  av_log(matroska->ctx, AV_LOG_INFO,
1601  "Unknown or unsupported track type %"PRIu64"\n",
1602  track->type);
1603  continue;
1604  }
1605  if (!track->codec_id)
1606  continue;
1607 
1608  if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1609  if (!track->default_duration && track->video.frame_rate > 0)
1610  track->default_duration = 1000000000 / track->video.frame_rate;
1611  if (!track->video.display_width)
1612  track->video.display_width = track->video.pixel_width;
1613  if (!track->video.display_height)
1614  track->video.display_height = track->video.pixel_height;
1615  } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1616  if (!track->audio.out_samplerate)
1617  track->audio.out_samplerate = track->audio.samplerate;
1618  }
1619  if (encodings_list->nb_elem > 1) {
1620  av_log(matroska->ctx, AV_LOG_ERROR,
1621  "Multiple combined encodings not supported");
1622  } else if (encodings_list->nb_elem == 1) {
1623  if (encodings[0].type ||
1624  (
1625 #if CONFIG_ZLIB
1626  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1627 #endif
1628 #if CONFIG_BZLIB
1630 #endif
1631 #if CONFIG_LZO
1633 #endif
1635  encodings[0].scope = 0;
1636  av_log(matroska->ctx, AV_LOG_ERROR,
1637  "Unsupported encoding type");
1638  } else if (track->codec_priv.size && encodings[0].scope & 2) {
1639  uint8_t *codec_priv = track->codec_priv.data;
1640  int ret = matroska_decode_buffer(&track->codec_priv.data,
1641  &track->codec_priv.size,
1642  track);
1643  if (ret < 0) {
1644  track->codec_priv.data = NULL;
1645  track->codec_priv.size = 0;
1646  av_log(matroska->ctx, AV_LOG_ERROR,
1647  "Failed to decode codec private data\n");
1648  }
1649 
1650  if (codec_priv != track->codec_priv.data)
1651  av_free(codec_priv);
1652  }
1653  }
1654 
1655  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1656  if (!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
1657  strlen(ff_mkv_codec_tags[j].str))) {
1658  codec_id = ff_mkv_codec_tags[j].id;
1659  break;
1660  }
1661  }
1662 
1663  st = track->stream = avformat_new_stream(s, NULL);
1664  if (!st)
1665  return AVERROR(ENOMEM);
1666 
1667  if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") &&
1668  track->codec_priv.size >= 40 &&
1669  track->codec_priv.data) {
1670  track->ms_compat = 1;
1671  track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
1673  track->video.fourcc);
1674  extradata_offset = 40;
1675  } else if (!strcmp(track->codec_id, "A_MS/ACM") &&
1676  track->codec_priv.size >= 14 &&
1677  track->codec_priv.data) {
1678  int ret;
1679  ffio_init_context(&b, track->codec_priv.data,
1680  track->codec_priv.size,
1681  0, NULL, NULL, NULL, NULL);
1682  ret = ff_get_wav_header(s, &b, st->codecpar, track->codec_priv.size);
1683  if (ret < 0)
1684  return ret;
1685  codec_id = st->codecpar->codec_id;
1686  extradata_offset = FFMIN(track->codec_priv.size, 18);
1687  } else if (!strcmp(track->codec_id, "V_QUICKTIME") &&
1688  (track->codec_priv.size >= 86) &&
1689  (track->codec_priv.data)) {
1690  if (track->codec_priv.size == AV_RB32(track->codec_priv.data)) {
1691  track->video.fourcc = AV_RL32(track->codec_priv.data + 4);
1693  track->video.fourcc);
1694  }
1695  if (codec_id == AV_CODEC_ID_NONE) {
1696  track->video.fourcc = AV_RL32(track->codec_priv.data);
1698  track->video.fourcc);
1699  }
1700  if (codec_id == AV_CODEC_ID_NONE) {
1701  char buf[32];
1702  av_get_codec_tag_string(buf, sizeof(buf), track->video.fourcc);
1703  av_log(matroska->ctx, AV_LOG_ERROR,
1704  "mov FourCC not found %s.\n", buf);
1705  }
1706  } else if (codec_id == AV_CODEC_ID_PCM_S16BE) {
1707  switch (track->audio.bitdepth) {
1708  case 8:
1709  codec_id = AV_CODEC_ID_PCM_U8;
1710  break;
1711  case 24:
1712  codec_id = AV_CODEC_ID_PCM_S24BE;
1713  break;
1714  case 32:
1715  codec_id = AV_CODEC_ID_PCM_S32BE;
1716  break;
1717  }
1718  } else if (codec_id == AV_CODEC_ID_PCM_S16LE) {
1719  switch (track->audio.bitdepth) {
1720  case 8:
1721  codec_id = AV_CODEC_ID_PCM_U8;
1722  break;
1723  case 24:
1724  codec_id = AV_CODEC_ID_PCM_S24LE;
1725  break;
1726  case 32:
1727  codec_id = AV_CODEC_ID_PCM_S32LE;
1728  break;
1729  }
1730  } else if (codec_id == AV_CODEC_ID_PCM_F32LE &&
1731  track->audio.bitdepth == 64) {
1732  codec_id = AV_CODEC_ID_PCM_F64LE;
1733  } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) {
1734  int profile = matroska_aac_profile(track->codec_id);
1735  int sri = matroska_aac_sri(track->audio.samplerate);
1736  extradata = av_mallocz(5 + AV_INPUT_BUFFER_PADDING_SIZE);
1737  if (!extradata)
1738  return AVERROR(ENOMEM);
1739  extradata[0] = (profile << 3) | ((sri & 0x0E) >> 1);
1740  extradata[1] = ((sri & 0x01) << 7) | (track->audio.channels << 3);
1741  if (strstr(track->codec_id, "SBR")) {
1742  sri = matroska_aac_sri(track->audio.out_samplerate);
1743  extradata[2] = 0x56;
1744  extradata[3] = 0xE5;
1745  extradata[4] = 0x80 | (sri << 3);
1746  extradata_size = 5;
1747  } else
1748  extradata_size = 2;
1749  } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size) {
1750  /* Only ALAC's magic cookie is stored in Matroska's track headers.
1751  * Create the "atom size", "tag", and "tag version" fields the
1752  * decoder expects manually. */
1753  extradata_size = 12 + track->codec_priv.size;
1754  extradata = av_mallocz(extradata_size +
1756  if (!extradata)
1757  return AVERROR(ENOMEM);
1758  AV_WB32(extradata, extradata_size);
1759  memcpy(&extradata[4], "alac", 4);
1760  AV_WB32(&extradata[8], 0);
1761  memcpy(&extradata[12], track->codec_priv.data,
1762  track->codec_priv.size);
1763  } else if (codec_id == AV_CODEC_ID_TTA) {
1764  extradata_size = 30;
1765  extradata = av_mallocz(extradata_size);
1766  if (!extradata)
1767  return AVERROR(ENOMEM);
1768  ffio_init_context(&b, extradata, extradata_size, 1,
1769  NULL, NULL, NULL, NULL);
1770  avio_write(&b, "TTA1", 4);
1771  avio_wl16(&b, 1);
1772  avio_wl16(&b, track->audio.channels);
1773  avio_wl16(&b, track->audio.bitdepth);
1774  avio_wl32(&b, track->audio.out_samplerate);
1775  avio_wl32(&b, matroska->ctx->duration *
1776  track->audio.out_samplerate);
1777  } else if (codec_id == AV_CODEC_ID_RV10 ||
1778  codec_id == AV_CODEC_ID_RV20 ||
1779  codec_id == AV_CODEC_ID_RV30 ||
1780  codec_id == AV_CODEC_ID_RV40) {
1781  extradata_offset = 26;
1782  } else if (codec_id == AV_CODEC_ID_RA_144) {
1783  track->audio.out_samplerate = 8000;
1784  track->audio.channels = 1;
1785  } else if (codec_id == AV_CODEC_ID_RA_288 ||
1786  codec_id == AV_CODEC_ID_COOK ||
1787  codec_id == AV_CODEC_ID_ATRAC3 ||
1788  codec_id == AV_CODEC_ID_SIPR) {
1789  int flavor;
1790  ffio_init_context(&b, track->codec_priv.data,
1791  track->codec_priv.size,
1792  0, NULL, NULL, NULL, NULL);
1793  avio_skip(&b, 22);
1794  flavor = avio_rb16(&b);
1795  track->audio.coded_framesize = avio_rb32(&b);
1796  avio_skip(&b, 12);
1797  track->audio.sub_packet_h = avio_rb16(&b);
1798  track->audio.frame_size = avio_rb16(&b);
1799  track->audio.sub_packet_size = avio_rb16(&b);
1800  if (flavor <= 0 ||
1801  track->audio.coded_framesize <= 0 ||
1802  track->audio.sub_packet_h <= 0 ||
1803  track->audio.frame_size <= 0 ||
1804  track->audio.sub_packet_size <= 0)
1805  return AVERROR_INVALIDDATA;
1806  track->audio.buf = av_malloc(track->audio.frame_size *
1807  track->audio.sub_packet_h);
1808  if (!track->audio.buf)
1809  return AVERROR(ENOMEM);
1810  if (codec_id == AV_CODEC_ID_RA_288) {
1811  st->codecpar->block_align = track->audio.coded_framesize;
1812  track->codec_priv.size = 0;
1813  } else {
1814  if (codec_id == AV_CODEC_ID_SIPR && flavor < 4) {
1815  static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1816  track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
1817  st->codecpar->bit_rate = sipr_bit_rate[flavor];
1818  }
1819  st->codecpar->block_align = track->audio.sub_packet_size;
1820  extradata_offset = 78;
1821  }
1822  } else if (codec_id == AV_CODEC_ID_FLAC && track->codec_priv.size) {
1823  ret = matroska_parse_flac(s, track, &extradata_offset);
1824  if (ret < 0)
1825  return ret;
1826  }
1827  track->codec_priv.size -= extradata_offset;
1828 
1829  if (codec_id == AV_CODEC_ID_NONE)
1830  av_log(matroska->ctx, AV_LOG_INFO,
1831  "Unknown/unsupported AVCodecID %s.\n", track->codec_id);
1832 
1833  if (track->time_scale < 0.01)
1834  track->time_scale = 1.0;
1835  avpriv_set_pts_info(st, 64, matroska->time_scale * track->time_scale,
1836  1000 * 1000 * 1000); /* 64 bit pts in ns */
1837 
1838  if (track->type == MATROSKA_TRACK_TYPE_AUDIO &&
1839  track->audio.out_samplerate) {
1841  (AVRational){ 1, 1000000000 },
1842  (AVRational){ 1, track->audio.out_samplerate });
1843  }
1844 
1845  /* convert the delay from ns to the track timebase */
1846  track->codec_delay = av_rescale_q(track->codec_delay,
1847  (AVRational){ 1, 1000000000 },
1848  st->time_base);
1849 
1850  st->codecpar->codec_id = codec_id;
1851  st->start_time = 0;
1852  if (strcmp(track->language, "und"))
1853  av_dict_set(&st->metadata, "language", track->language, 0);
1854  av_dict_set(&st->metadata, "title", track->name, 0);
1855 
1856  if (track->flag_default)
1858  if (track->flag_forced)
1860 
1861  if (!st->codecpar->extradata) {
1862  if (extradata) {
1863  st->codecpar->extradata = extradata;
1864  st->codecpar->extradata_size = extradata_size;
1865  } else if (track->codec_priv.data && track->codec_priv.size > 0) {
1866  st->codecpar->extradata = av_mallocz(track->codec_priv.size +
1868  if (!st->codecpar->extradata)
1869  return AVERROR(ENOMEM);
1870  st->codecpar->extradata_size = track->codec_priv.size;
1871  memcpy(st->codecpar->extradata,
1872  track->codec_priv.data + extradata_offset,
1873  track->codec_priv.size);
1874  }
1875  }
1876 
1877  if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1878  int display_width_mul = 1;
1879  int display_height_mul = 1;
1880 
1882  st->codecpar->codec_tag = track->video.fourcc;
1883  st->codecpar->width = track->video.pixel_width;
1884  st->codecpar->height = track->video.pixel_height;
1885 
1888 
1890  mkv_stereo_mode_display_mul(track->video.stereo_mode, &display_width_mul, &display_height_mul);
1891 
1893  &st->sample_aspect_ratio.den,
1894  st->codecpar->height * track->video.display_width * display_width_mul,
1895  st->codecpar->width * track->video.display_height * display_height_mul,
1896  255);
1897  if (st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1900  if (track->default_duration) {
1902  1000000000, track->default_duration, 30000);
1903  }
1904  // add stream level stereo3d side data if it is a supported format
1906  track->video.stereo_mode != 10 && track->video.stereo_mode != 12) {
1907  int ret = ff_mkv_stereo3d_conv(st, track->video.stereo_mode);
1908  if (ret < 0)
1909  return ret;
1910  }
1911  } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1913  st->codecpar->sample_rate = track->audio.out_samplerate;
1914  st->codecpar->channels = track->audio.channels;
1915  if (st->codecpar->codec_id != AV_CODEC_ID_AAC)
1917  if (st->codecpar->codec_id == AV_CODEC_ID_MP3)
1919  } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
1921  if (st->codecpar->codec_id == AV_CODEC_ID_SSA)
1922  matroska->contains_ssa = 1;
1923  }
1924  }
1925 
1926  return 0;
1927 }
1928 
1930 {
1931  MatroskaDemuxContext *matroska = s->priv_data;
1932  EbmlList *attachments_list = &matroska->attachments;
1933  EbmlList *chapters_list = &matroska->chapters;
1934  MatroskaAttachment *attachments;
1935  MatroskaChapter *chapters;
1936  uint64_t max_start = 0;
1937  int64_t pos;
1938  Ebml ebml = { 0 };
1939  int i, j, res;
1940 
1941  matroska->ctx = s;
1942 
1943  /* First read the EBML header. */
1944  if (ebml_parse(matroska, ebml_syntax, &ebml) || !ebml.doctype) {
1945  av_log(matroska->ctx, AV_LOG_ERROR, "EBML header parsing failed\n");
1946  ebml_free(ebml_syntax, &ebml);
1947  return AVERROR_INVALIDDATA;
1948  }
1949  if (ebml.version > EBML_VERSION ||
1950  ebml.max_size > sizeof(uint64_t) ||
1951  ebml.id_length > sizeof(uint32_t) ||
1952  ebml.doctype_version > 3) {
1953  av_log(matroska->ctx, AV_LOG_ERROR,
1954  "EBML header using unsupported features\n"
1955  "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
1956  ebml.version, ebml.doctype, ebml.doctype_version);
1957  ebml_free(ebml_syntax, &ebml);
1958  return AVERROR_PATCHWELCOME;
1959  }
1960  for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
1961  if (!strcmp(ebml.doctype, matroska_doctypes[i]))
1962  break;
1963  if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
1964  av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
1965  if (matroska->ctx->error_recognition & AV_EF_EXPLODE) {
1966  ebml_free(ebml_syntax, &ebml);
1967  return AVERROR_INVALIDDATA;
1968  }
1969  }
1970  ebml_free(ebml_syntax, &ebml);
1971 
1972  /* The next thing is a segment. */
1973  pos = avio_tell(matroska->ctx->pb);
1974  res = ebml_parse(matroska, matroska_segments, matroska);
1975  // try resyncing until we find a EBML_STOP type element.
1976  while (res != 1) {
1977  res = matroska_resync(matroska, pos);
1978  if (res < 0)
1979  return res;
1980  pos = avio_tell(matroska->ctx->pb);
1981  res = ebml_parse(matroska, matroska_segment, matroska);
1982  }
1983  matroska_execute_seekhead(matroska);
1984 
1985  if (!matroska->time_scale)
1986  matroska->time_scale = 1000000;
1987  if (matroska->duration)
1988  matroska->ctx->duration = matroska->duration * matroska->time_scale *
1989  1000 / AV_TIME_BASE;
1990  av_dict_set(&s->metadata, "title", matroska->title, 0);
1991 
1992  res = matroska_parse_tracks(s);
1993  if (res < 0)
1994  return res;
1995 
1996  attachments = attachments_list->elem;
1997  for (j = 0; j < attachments_list->nb_elem; j++) {
1998  if (!(attachments[j].filename && attachments[j].mime &&
1999  attachments[j].bin.data && attachments[j].bin.size > 0)) {
2000  av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2001  } else {
2002  AVStream *st = avformat_new_stream(s, NULL);
2003  if (!st)
2004  break;
2005  av_dict_set(&st->metadata, "filename", attachments[j].filename, 0);
2006  av_dict_set(&st->metadata, "mimetype", attachments[j].mime, 0);
2008 
2009  for (i = 0; ff_mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
2010  if (!strncmp(ff_mkv_image_mime_tags[i].str, attachments[j].mime,
2011  strlen(ff_mkv_image_mime_tags[i].str))) {
2013  break;
2014  }
2015  }
2016 
2017  attachments[j].stream = st;
2018 
2019  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
2022 
2024  if ((res = av_new_packet(&st->attached_pic, attachments[j].bin.size)) < 0)
2025  return res;
2026  memcpy(st->attached_pic.data, attachments[j].bin.data, attachments[j].bin.size);
2027  st->attached_pic.stream_index = st->index;
2029  } else {
2031  st->codecpar->extradata = av_malloc(attachments[j].bin.size);
2032  if (!st->codecpar->extradata)
2033  break;
2034 
2035  st->codecpar->extradata_size = attachments[j].bin.size;
2036  memcpy(st->codecpar->extradata, attachments[j].bin.data,
2037  attachments[j].bin.size);
2038 
2039  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
2040  if (!strncmp(ff_mkv_mime_tags[i].str, attachments[j].mime,
2041  strlen(ff_mkv_mime_tags[i].str))) {
2043  break;
2044  }
2045  }
2046  }
2047  }
2048  }
2049 
2050  chapters = chapters_list->elem;
2051  for (i = 0; i < chapters_list->nb_elem; i++)
2052  if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid &&
2053  (max_start == 0 || chapters[i].start > max_start)) {
2054  chapters[i].chapter =
2055  avpriv_new_chapter(s, chapters[i].uid,
2056  (AVRational) { 1, 1000000000 },
2057  chapters[i].start, chapters[i].end,
2058  chapters[i].title);
2059  av_dict_set(&chapters[i].chapter->metadata,
2060  "title", chapters[i].title, 0);
2061  max_start = chapters[i].start;
2062  }
2063 
2065 
2066  return 0;
2067 }
2068 
2069 /*
2070  * Put one packet in an application-supplied AVPacket struct.
2071  * Returns 0 on success or -1 on failure.
2072  */
2074  AVPacket *pkt)
2075 {
2076  if (matroska->num_packets > 0) {
2077  memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
2078  av_free(matroska->packets[0]);
2079  if (matroska->num_packets > 1) {
2080  void *newpackets;
2081  memmove(&matroska->packets[0], &matroska->packets[1],
2082  (matroska->num_packets - 1) * sizeof(AVPacket *));
2083  newpackets = av_realloc(matroska->packets,
2084  (matroska->num_packets - 1) *
2085  sizeof(AVPacket *));
2086  if (newpackets)
2087  matroska->packets = newpackets;
2088  } else {
2089  av_freep(&matroska->packets);
2090  matroska->prev_pkt = NULL;
2091  }
2092  matroska->num_packets--;
2093  return 0;
2094  }
2095 
2096  return -1;
2097 }
2098 
2099 /*
2100  * Free all packets in our internal queue.
2101  */
2103 {
2104  matroska->prev_pkt = NULL;
2105  if (matroska->packets) {
2106  int n;
2107  for (n = 0; n < matroska->num_packets; n++) {
2108  av_packet_unref(matroska->packets[n]);
2109  av_free(matroska->packets[n]);
2110  }
2111  av_freep(&matroska->packets);
2112  matroska->num_packets = 0;
2113  }
2114 }
2115 
2117  int *buf_size, int type,
2118  uint32_t **lace_buf, int *laces)
2119 {
2120  int res = 0, n, size = *buf_size;
2121  uint8_t *data = *buf;
2122  uint32_t *lace_size;
2123 
2124  if (!type) {
2125  *laces = 1;
2126  *lace_buf = av_mallocz(sizeof(int));
2127  if (!*lace_buf)
2128  return AVERROR(ENOMEM);
2129 
2130  *lace_buf[0] = size;
2131  return 0;
2132  }
2133 
2134  assert(size > 0);
2135  *laces = *data + 1;
2136  data += 1;
2137  size -= 1;
2138  lace_size = av_mallocz(*laces * sizeof(int));
2139  if (!lace_size)
2140  return AVERROR(ENOMEM);
2141 
2142  switch (type) {
2143  case 0x1: /* Xiph lacing */
2144  {
2145  uint8_t temp;
2146  uint32_t total = 0;
2147  for (n = 0; res == 0 && n < *laces - 1; n++) {
2148  while (1) {
2149  if (size == 0) {
2150  res = AVERROR_EOF;
2151  break;
2152  }
2153  temp = *data;
2154  lace_size[n] += temp;
2155  data += 1;
2156  size -= 1;
2157  if (temp != 0xff)
2158  break;
2159  }
2160  total += lace_size[n];
2161  }
2162  if (size <= total) {
2163  res = AVERROR_INVALIDDATA;
2164  break;
2165  }
2166 
2167  lace_size[n] = size - total;
2168  break;
2169  }
2170 
2171  case 0x2: /* fixed-size lacing */
2172  if (size % (*laces)) {
2173  res = AVERROR_INVALIDDATA;
2174  break;
2175  }
2176  for (n = 0; n < *laces; n++)
2177  lace_size[n] = size / *laces;
2178  break;
2179 
2180  case 0x3: /* EBML lacing */
2181  {
2182  uint64_t num;
2183  uint64_t total;
2184  n = matroska_ebmlnum_uint(matroska, data, size, &num);
2185  if (n < 0) {
2186  av_log(matroska->ctx, AV_LOG_INFO,
2187  "EBML block data error\n");
2188  res = n;
2189  break;
2190  }
2191  data += n;
2192  size -= n;
2193  total = lace_size[0] = num;
2194  for (n = 1; res == 0 && n < *laces - 1; n++) {
2195  int64_t snum;
2196  int r;
2197  r = matroska_ebmlnum_sint(matroska, data, size, &snum);
2198  if (r < 0) {
2199  av_log(matroska->ctx, AV_LOG_INFO,
2200  "EBML block data error\n");
2201  res = r;
2202  break;
2203  }
2204  data += r;
2205  size -= r;
2206  lace_size[n] = lace_size[n - 1] + snum;
2207  total += lace_size[n];
2208  }
2209  if (size <= total) {
2210  res = AVERROR_INVALIDDATA;
2211  break;
2212  }
2213  lace_size[*laces - 1] = size - total;
2214  break;
2215  }
2216  }
2217 
2218  *buf = data;
2219  *lace_buf = lace_size;
2220  *buf_size = size;
2221 
2222  return res;
2223 }
2224 
2226  MatroskaTrack *track, AVStream *st,
2227  uint8_t *data, int size, uint64_t timecode,
2228  uint64_t duration, int64_t pos)
2229 {
2230  int a = st->codecpar->block_align;
2231  int sps = track->audio.sub_packet_size;
2232  int cfs = track->audio.coded_framesize;
2233  int h = track->audio.sub_packet_h;
2234  int y = track->audio.sub_packet_cnt;
2235  int w = track->audio.frame_size;
2236  int x;
2237 
2238  if (!track->audio.pkt_cnt) {
2239  if (track->audio.sub_packet_cnt == 0)
2240  track->audio.buf_timecode = timecode;
2241  if (st->codecpar->codec_id == AV_CODEC_ID_RA_288) {
2242  if (size < cfs * h / 2) {
2243  av_log(matroska->ctx, AV_LOG_ERROR,
2244  "Corrupt int4 RM-style audio packet size\n");
2245  return AVERROR_INVALIDDATA;
2246  }
2247  for (x = 0; x < h / 2; x++)
2248  memcpy(track->audio.buf + x * 2 * w + y * cfs,
2249  data + x * cfs, cfs);
2250  } else if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) {
2251  if (size < w) {
2252  av_log(matroska->ctx, AV_LOG_ERROR,
2253  "Corrupt sipr RM-style audio packet size\n");
2254  return AVERROR_INVALIDDATA;
2255  }
2256  memcpy(track->audio.buf + y * w, data, w);
2257  } else {
2258  if (size < sps * w / sps) {
2259  av_log(matroska->ctx, AV_LOG_ERROR,
2260  "Corrupt generic RM-style audio packet size\n");
2261  return AVERROR_INVALIDDATA;
2262  }
2263  for (x = 0; x < w / sps; x++)
2264  memcpy(track->audio.buf +
2265  sps * (h * x + ((h + 1) / 2) * (y & 1) + (y >> 1)),
2266  data + x * sps, sps);
2267  }
2268 
2269  if (++track->audio.sub_packet_cnt >= h) {
2270  if (st->codecpar->codec_id == AV_CODEC_ID_SIPR)
2271  ff_rm_reorder_sipr_data(track->audio.buf, h, w);
2272  track->audio.sub_packet_cnt = 0;
2273  track->audio.pkt_cnt = h * w / a;
2274  }
2275  }
2276 
2277  while (track->audio.pkt_cnt) {
2278  int ret;
2279  AVPacket *pkt = av_mallocz(sizeof(AVPacket));
2280  if (!pkt)
2281  return AVERROR(ENOMEM);
2282 
2283  ret = av_new_packet(pkt, a);
2284  if (ret < 0) {
2285  av_free(pkt);
2286  return ret;
2287  }
2288  memcpy(pkt->data,
2289  track->audio.buf + a * (h * w / a - track->audio.pkt_cnt--),
2290  a);
2291  pkt->pts = track->audio.buf_timecode;
2293  pkt->pos = pos;
2294  pkt->stream_index = st->index;
2295  dynarray_add(&matroska->packets, &matroska->num_packets, pkt);
2296  }
2297 
2298  return 0;
2299 }
2300 
2301 /* reconstruct full wavpack blocks from mangled matroska ones */
2303  uint8_t **pdst, int *size)
2304 {
2305  uint8_t *dst = NULL;
2306  int dstlen = 0;
2307  int srclen = *size;
2308  uint32_t samples;
2309  uint16_t ver;
2310  int ret, offset = 0;
2311 
2312  if (srclen < 12 || track->stream->codecpar->extradata_size < 2)
2313  return AVERROR_INVALIDDATA;
2314 
2315  ver = AV_RL16(track->stream->codecpar->extradata);
2316 
2317  samples = AV_RL32(src);
2318  src += 4;
2319  srclen -= 4;
2320 
2321  while (srclen >= 8) {
2322  int multiblock;
2323  uint32_t blocksize;
2324  uint8_t *tmp;
2325 
2326  uint32_t flags = AV_RL32(src);
2327  uint32_t crc = AV_RL32(src + 4);
2328  src += 8;
2329  srclen -= 8;
2330 
2331  multiblock = (flags & 0x1800) != 0x1800;
2332  if (multiblock) {
2333  if (srclen < 4) {
2334  ret = AVERROR_INVALIDDATA;
2335  goto fail;
2336  }
2337  blocksize = AV_RL32(src);
2338  src += 4;
2339  srclen -= 4;
2340  } else
2341  blocksize = srclen;
2342 
2343  if (blocksize > srclen) {
2344  ret = AVERROR_INVALIDDATA;
2345  goto fail;
2346  }
2347 
2348  tmp = av_realloc(dst, dstlen + blocksize + 32);
2349  if (!tmp) {
2350  ret = AVERROR(ENOMEM);
2351  goto fail;
2352  }
2353  dst = tmp;
2354  dstlen += blocksize + 32;
2355 
2356  AV_WL32(dst + offset, MKTAG('w', 'v', 'p', 'k')); // tag
2357  AV_WL32(dst + offset + 4, blocksize + 24); // blocksize - 8
2358  AV_WL16(dst + offset + 8, ver); // version
2359  AV_WL16(dst + offset + 10, 0); // track/index_no
2360  AV_WL32(dst + offset + 12, 0); // total samples
2361  AV_WL32(dst + offset + 16, 0); // block index
2362  AV_WL32(dst + offset + 20, samples); // number of samples
2363  AV_WL32(dst + offset + 24, flags); // flags
2364  AV_WL32(dst + offset + 28, crc); // crc
2365  memcpy(dst + offset + 32, src, blocksize); // block data
2366 
2367  src += blocksize;
2368  srclen -= blocksize;
2369  offset += blocksize + 32;
2370  }
2371 
2372  *pdst = dst;
2373  *size = dstlen;
2374 
2375  return 0;
2376 
2377 fail:
2378  av_freep(&dst);
2379  return ret;
2380 }
2381 
2383  MatroskaTrack *track, AVStream *st,
2384  uint8_t *data, int pkt_size,
2385  uint64_t timecode, uint64_t duration,
2386  int64_t pos, int is_keyframe)
2387 {
2388  MatroskaTrackEncoding *encodings = track->encodings.elem;
2389  uint8_t *pkt_data = data;
2390  int offset = 0, res;
2391  AVPacket *pkt;
2392 
2393  if (encodings && encodings->scope & 1) {
2394  res = matroska_decode_buffer(&pkt_data, &pkt_size, track);
2395  if (res < 0)
2396  return res;
2397  }
2398 
2399  if (st->codecpar->codec_id == AV_CODEC_ID_WAVPACK) {
2400  uint8_t *wv_data;
2401  res = matroska_parse_wavpack(track, pkt_data, &wv_data, &pkt_size);
2402  if (res < 0) {
2403  av_log(matroska->ctx, AV_LOG_ERROR,
2404  "Error parsing a wavpack block.\n");
2405  goto fail;
2406  }
2407  if (pkt_data != data)
2408  av_freep(&pkt_data);
2409  pkt_data = wv_data;
2410  }
2411 
2412  if (st->codecpar->codec_id == AV_CODEC_ID_PRORES)
2413  offset = 8;
2414 
2415  pkt = av_mallocz(sizeof(AVPacket));
2416  if (!pkt) {
2417  av_freep(&pkt_data);
2418  return AVERROR(ENOMEM);
2419  }
2420  /* XXX: prevent data copy... */
2421  if (av_new_packet(pkt, pkt_size + offset) < 0) {
2422  av_free(pkt);
2423  av_freep(&pkt_data);
2424  return AVERROR(ENOMEM);
2425  }
2426 
2427  if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
2428  uint8_t *buf = pkt->data;
2429  bytestream_put_be32(&buf, pkt_size);
2430  bytestream_put_be32(&buf, MKBETAG('i', 'c', 'p', 'f'));
2431  }
2432 
2433  memcpy(pkt->data + offset, pkt_data, pkt_size);
2434 
2435  if (pkt_data != data)
2436  av_free(pkt_data);
2437 
2438  pkt->flags = is_keyframe;
2439  pkt->stream_index = st->index;
2440 
2441  if (track->ms_compat)
2442  pkt->dts = timecode;
2443  else
2444  pkt->pts = timecode;
2445  pkt->pos = pos;
2447  pkt->duration = duration;
2448 #if FF_API_CONVERGENCE_DURATION
2450  if (st->codecpar->codec_id == AV_CODEC_ID_SRT)
2453 #endif
2454 
2455  if (st->codecpar->codec_id == AV_CODEC_ID_SSA)
2456  matroska_fix_ass_packet(matroska, pkt, duration);
2457 
2458  if (matroska->prev_pkt &&
2459  timecode != AV_NOPTS_VALUE &&
2460  matroska->prev_pkt->pts == timecode &&
2461  matroska->prev_pkt->stream_index == st->index &&
2463  matroska_merge_packets(matroska->prev_pkt, pkt);
2464  else {
2465  dynarray_add(&matroska->packets, &matroska->num_packets, pkt);
2466  matroska->prev_pkt = pkt;
2467  }
2468 
2469  return 0;
2470 
2471 fail:
2472  if (pkt_data != data)
2473  av_freep(&pkt_data);
2474  return res;
2475 }
2476 
2478  int size, int64_t pos, uint64_t cluster_time,
2479  uint64_t block_duration, int is_keyframe,
2480  int64_t cluster_pos)
2481 {
2482  uint64_t timecode = AV_NOPTS_VALUE;
2483  MatroskaTrack *track;
2484  int res = 0;
2485  AVStream *st;
2486  int16_t block_time;
2487  uint32_t *lace_size = NULL;
2488  int n, flags, laces = 0;
2489  uint64_t num, duration;
2490 
2491  if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
2492  av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2493  return n;
2494  }
2495  data += n;
2496  size -= n;
2497 
2498  track = matroska_find_track_by_num(matroska, num);
2499  if (!track || !track->stream) {
2500  av_log(matroska->ctx, AV_LOG_INFO,
2501  "Invalid stream %"PRIu64" or size %u\n", num, size);
2502  return AVERROR_INVALIDDATA;
2503  } else if (size <= 3)
2504  return 0;
2505  st = track->stream;
2506  if (st->discard >= AVDISCARD_ALL)
2507  return res;
2508 
2509  block_time = AV_RB16(data);
2510  data += 2;
2511  flags = *data++;
2512  size -= 3;
2513  if (is_keyframe == -1)
2514  is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
2515 
2516  if (cluster_time != (uint64_t) -1 &&
2517  (block_time >= 0 || cluster_time >= -block_time)) {
2518  timecode = cluster_time + block_time - track->codec_delay;
2519  if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE &&
2520  timecode < track->end_timecode)
2521  is_keyframe = 0; /* overlapping subtitles are not key frame */
2522  if (is_keyframe)
2523  av_add_index_entry(st, cluster_pos, timecode, 0, 0,
2525  }
2526 
2527  if (matroska->skip_to_keyframe &&
2528  track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
2529  if (!is_keyframe || timecode < matroska->skip_to_timecode)
2530  return res;
2531  matroska->skip_to_keyframe = 0;
2532  }
2533 
2534  res = matroska_parse_laces(matroska, &data, &size, (flags & 0x06) >> 1,
2535  &lace_size, &laces);
2536 
2537  if (res)
2538  goto end;
2539 
2540  if (block_duration != AV_NOPTS_VALUE) {
2541  duration = block_duration / laces;
2542  if (block_duration != duration * laces) {
2543  av_log(matroska->ctx, AV_LOG_WARNING,
2544  "Incorrect block_duration, possibly corrupted container");
2545  }
2546  } else {
2547  duration = track->default_duration / matroska->time_scale;
2548  block_duration = duration * laces;
2549  }
2550 
2551  if (timecode != AV_NOPTS_VALUE)
2552  track->end_timecode =
2553  FFMAX(track->end_timecode, timecode + block_duration);
2554 
2555  for (n = 0; n < laces; n++) {
2556  if ((st->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
2560  st->codecpar->block_align && track->audio.sub_packet_size) {
2561  res = matroska_parse_rm_audio(matroska, track, st, data,
2562  lace_size[n],
2563  timecode, duration, pos);
2564  if (res)
2565  goto end;
2566  } else {
2567  res = matroska_parse_frame(matroska, track, st, data, lace_size[n],
2568  timecode, duration, pos,
2569  !n ? is_keyframe : 0);
2570  if (res)
2571  goto end;
2572  }
2573 
2574  if (timecode != AV_NOPTS_VALUE)
2575  timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2576  data += lace_size[n];
2577  }
2578 
2579 end:
2580  av_free(lace_size);
2581  return res;
2582 }
2583 
2585 {
2586  EbmlList *blocks_list;
2587  MatroskaBlock *blocks;
2588  int i, res;
2589  res = ebml_parse(matroska,
2590  matroska_cluster_incremental_parsing,
2591  &matroska->current_cluster);
2592  if (res == 1) {
2593  /* New Cluster */
2594  if (matroska->current_cluster_pos)
2595  ebml_level_end(matroska);
2596  ebml_free(matroska_cluster, &matroska->current_cluster);
2597  memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster));
2598  matroska->current_cluster_num_blocks = 0;
2599  matroska->current_cluster_pos = avio_tell(matroska->ctx->pb);
2600  matroska->prev_pkt = NULL;
2601  /* sizeof the ID which was already read */
2602  if (matroska->current_id)
2603  matroska->current_cluster_pos -= 4;
2604  res = ebml_parse(matroska,
2605  matroska_clusters_incremental,
2606  &matroska->current_cluster);
2607  /* Try parsing the block again. */
2608  if (res == 1)
2609  res = ebml_parse(matroska,
2610  matroska_cluster_incremental_parsing,
2611  &matroska->current_cluster);
2612  }
2613 
2614  if (!res &&
2615  matroska->current_cluster_num_blocks <
2616  matroska->current_cluster.blocks.nb_elem) {
2617  blocks_list = &matroska->current_cluster.blocks;
2618  blocks = blocks_list->elem;
2619 
2620  matroska->current_cluster_num_blocks = blocks_list->nb_elem;
2621  i = blocks_list->nb_elem - 1;
2622  if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
2623  int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
2624  if (!blocks[i].non_simple)
2625  blocks[i].duration = AV_NOPTS_VALUE;
2626  res = matroska_parse_block(matroska, blocks[i].bin.data,
2627  blocks[i].bin.size, blocks[i].bin.pos,
2628  matroska->current_cluster.timecode,
2629  blocks[i].duration, is_keyframe,
2630  matroska->current_cluster_pos);
2631  }
2632  }
2633 
2634  if (res < 0)
2635  matroska->done = 1;
2636  return res;
2637 }
2638 
2640 {
2641  MatroskaCluster cluster = { 0 };
2642  EbmlList *blocks_list;
2643  MatroskaBlock *blocks;
2644  int i, res;
2645  int64_t pos;
2646 
2647  if (!matroska->contains_ssa)
2648  return matroska_parse_cluster_incremental(matroska);
2649  pos = avio_tell(matroska->ctx->pb);
2650  matroska->prev_pkt = NULL;
2651  if (matroska->current_id)
2652  pos -= 4; /* sizeof the ID which was already read */
2653  res = ebml_parse(matroska, matroska_clusters, &cluster);
2654  blocks_list = &cluster.blocks;
2655  blocks = blocks_list->elem;
2656  for (i = 0; i < blocks_list->nb_elem && !res; i++)
2657  if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
2658  int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
2659  if (!blocks[i].non_simple)
2660  blocks[i].duration = AV_NOPTS_VALUE;
2661  res = matroska_parse_block(matroska, blocks[i].bin.data,
2662  blocks[i].bin.size, blocks[i].bin.pos,
2663  cluster.timecode, blocks[i].duration,
2664  is_keyframe, pos);
2665  }
2666  ebml_free(matroska_cluster, &cluster);
2667  return res;
2668 }
2669 
2671 {
2672  MatroskaDemuxContext *matroska = s->priv_data;
2673  int ret = 0;
2674 
2675  while (!ret && matroska_deliver_packet(matroska, pkt)) {
2676  int64_t pos = avio_tell(matroska->ctx->pb);
2677  if (matroska->done)
2678  return AVERROR_EOF;
2679  if (matroska_parse_cluster(matroska) < 0)
2680  ret = matroska_resync(matroska, pos);
2681  }
2682 
2683  if (ret == AVERROR_INVALIDDATA && pkt->data) {
2684  pkt->flags |= AV_PKT_FLAG_CORRUPT;
2685  return 0;
2686  }
2687 
2688  return ret;
2689 }
2690 
2691 static int matroska_read_seek(AVFormatContext *s, int stream_index,
2692  int64_t timestamp, int flags)
2693 {
2694  MatroskaDemuxContext *matroska = s->priv_data;
2695  MatroskaTrack *tracks = NULL;
2696  AVStream *st = s->streams[stream_index];
2697  int i, index, index_sub, index_min;
2698 
2699  /* Parse the CUES now since we need the index data to seek. */
2700  if (matroska->cues_parsing_deferred) {
2701  matroska_parse_cues(matroska);
2702  matroska->cues_parsing_deferred = 0;
2703  }
2704 
2705  if (!st->nb_index_entries)
2706  return 0;
2707  timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
2708 
2709  if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
2710  avio_seek(s->pb, st->index_entries[st->nb_index_entries - 1].pos,
2711  SEEK_SET);
2712  matroska->current_id = 0;
2713  while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
2714  matroska_clear_queue(matroska);
2715  if (matroska_parse_cluster(matroska) < 0)
2716  break;
2717  }
2718  }
2719 
2720  matroska_clear_queue(matroska);
2721  if (index < 0)
2722  return 0;
2723 
2724  index_min = index;
2725  tracks = matroska->tracks.elem;
2726  for (i = 0; i < matroska->tracks.nb_elem; i++) {
2727  tracks[i].audio.pkt_cnt = 0;
2728  tracks[i].audio.sub_packet_cnt = 0;
2729  tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
2730  tracks[i].end_timecode = 0;
2731  if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE &&
2732  tracks[i].stream->discard != AVDISCARD_ALL) {
2733  index_sub = av_index_search_timestamp(
2734  tracks[i].stream, st->index_entries[index].timestamp,
2736  if (index_sub >= 0 &&
2737  st->index_entries[index_sub].pos < st->index_entries[index_min].pos &&
2738  st->index_entries[index].timestamp -
2739  st->index_entries[index_sub].timestamp < 30000000000 / matroska->time_scale)
2740  index_min = index_sub;
2741  }
2742  }
2743 
2744  avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
2745  matroska->current_id = 0;
2746  matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
2747  matroska->skip_to_timecode = st->index_entries[index].timestamp;
2748  matroska->done = 0;
2749  ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
2750  return 0;
2751 }
2752 
2754 {
2755  MatroskaDemuxContext *matroska = s->priv_data;
2756  MatroskaTrack *tracks = matroska->tracks.elem;
2757  int n;
2758 
2759  matroska_clear_queue(matroska);
2760 
2761  for (n = 0; n < matroska->tracks.nb_elem; n++)
2762  if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
2763  av_free(tracks[n].audio.buf);
2764  ebml_free(matroska_cluster, &matroska->current_cluster);
2765  ebml_free(matroska_segment, matroska);
2766 
2767  return 0;
2768 }
2769 
2771  .name = "matroska,webm",
2772  .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
2773  .extensions = "mkv,mk3d,mka,mks",
2774  .priv_data_size = sizeof(MatroskaDemuxContext),
2780  .mime_type = "audio/webm,audio/x-matroska,video/webm,video/x-matroska"
2781 };
const char * s
Definition: matroskadec.c:84
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1657
static EbmlSyntax matroska_simpletag[]
Definition: matroskadec.c:455
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:98
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3540
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:398
uint32_t fourcc
Definition: hwcontext_qsv.c:90
static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx)
Definition: matroskadec.c:1341
Bytestream IO Context.
Definition: avio.h:104
#define MATROSKA_ID_VIDEOFLAGINTERLACED
Definition: matroska.h:115
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int mkv_field_order(int64_t field_order)
Definition: matroskadec.c:1534
static void matroska_convert_tags(AVFormatContext *s)
Definition: matroskadec.c:1276
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:71
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
int size
uint64_t type
Definition: matroskadec.c:151
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:95
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:75
static int matroska_deliver_packet(MatroskaDemuxContext *matroska, AVPacket *pkt)
Definition: matroskadec.c:2073
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:107
uint64_t version
Definition: matroskadec.c:101
AVInputFormat ff_matroska_demuxer
Definition: matroskadec.c:2770
static int is_keyframe(NalUnitType naltype)
Definition: libx265.c:50
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1214
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:141
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1983
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:84
const unsigned char ff_sipr_subpk_size[4]
Definition: rmsipr.c:25
#define MATROSKA_ID_TAGTARGETS_TYPE
Definition: matroska.h:157
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
static int ebml_level_end(MatroskaDemuxContext *matroska)
Definition: matroskadec.c:608
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, uint8_t *data, uint32_t size, int64_t *num)
Definition: matroskadec.c:807
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:2986
int64_t pos
Definition: avformat.h:664
#define MATROSKA_ID_ENCODINGTYPE
Definition: matroska.h:131
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:125
uint64_t chapteruid
Definition: matroskadec.c:210
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:1659
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:93
EbmlList tag
Definition: matroskadec.c:216
uint64_t uid
Definition: matroskadec.c:150
static EbmlSyntax matroska_segments[]
Definition: matroskadec.c:508
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
MatroskaCluster current_cluster
Definition: matroskadec.c:272
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
#define MATROSKA_ID_TAGTARGETS_ATTACHUID
Definition: matroska.h:161
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
#define MATROSKA_ID_CLUSTERPOSITION
Definition: matroska.h:172
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:188
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:890
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
#define MATROSKA_ID_BLOCKREFERENCE
Definition: matroska.h:180
uint64_t flag_forced
Definition: matroskadec.c:159
uint64_t max_size
Definition: matroskadec.c:102
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition: matroska.h:160
#define AV_RL16
Definition: intreadwrite.h:42
uint64_t flag_default
Definition: matroskadec.c:158
#define MATROSKA_ID_VIDEOASPECTRATIO
Definition: matroska.h:118
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:126
char * name
Definition: matroskadec.c:199
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
Definition: utils.c:2071
#define FF_ARRAY_ELEMS(a)
discard all
Definition: avcodec.h:689
MatroskaLevel levels[EBML_MAX_DEPTH]
Definition: matroskadec.c:239
static EbmlSyntax matroska_track_audio[]
Definition: matroskadec.c:331
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:145
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:681
MatroskaTrackAudio audio
Definition: matroskadec.c:161
uint64_t duration
Definition: matroskadec.c:279
const struct EbmlSyntax * n
Definition: matroskadec.c:85
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:201
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:171
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
static EbmlSyntax matroska_tag[]
Definition: matroskadec.c:474
static EbmlSyntax matroska_attachment[]
Definition: matroskadec.c:391
#define MATROSKA_ID_CHAPTERTIMEEND
Definition: matroska.h:195
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
static EbmlSyntax matroska_track[]
Definition: matroskadec.c:358
#define MATROSKA_ID_TRACKCONTENTENCODINGS
Definition: matroska.h:99
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
#define AV_LZO_OUTPUT_FULL
decoded data did not fit into output buffer
Definition: lzo.h:39
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:2758
#define EBML_VERSION
Definition: matroska.h:30
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:185
Format I/O context.
Definition: avformat.h:940
#define EBML_ID_CRC32
Definition: matroska.h:46
uint64_t def
Definition: matroskadec.c:202
void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition: utils.c:1137
#define MATROSKA_ID_TRACKCONTENTENCODING
Definition: matroska.h:100
#define MATROSKA_ID_CODECDOWNLOADURL
Definition: matroska.h:87
int64_t end_timecode
Definition: matroskadec.c:166
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1053
Public dictionary API.
uint64_t pixel_height
Definition: matroskadec.c:124
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
uint8_t
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:198
uint64_t stereo_mode
Definition: matroskadec.c:128
MatroskaTrackVideo video
Definition: matroskadec.c:160
#define MATROSKA_ID_EDITIONFLAGORDERED
Definition: matroska.h:202
int width
Video only.
Definition: avcodec.h:3525
void * elem
Definition: matroskadec.c:91
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:91
MatroskaTrackCompression compression
Definition: matroskadec.c:116
uint8_t * data
Definition: matroskadec.c:96
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:70
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:696
uint64_t time
Definition: matroskadec.c:194
#define AV_RB32
Definition: intreadwrite.h:130
int ff_mkv_stereo3d_conv(AVStream *st, MatroskaVideoStereoModeType stereo_mode)
Definition: matroska.c:116
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
#define MATROSKA_ID_VIDEOPIXELCROPT
Definition: matroska.h:111
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
static int matroska_aac_sri(int samplerate)
Definition: matroskadec.c:1469
enum AVStreamParseType need_parsing
Definition: avformat.h:879
#define b
Definition: input.c:52
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:175
#define MATROSKA_ID_TAGTARGETS_TYPEVALUE
Definition: matroska.h:158
#define MATROSKA_ID_EDITIONFLAGHIDDEN
Definition: matroska.h:200
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
static EbmlSyntax matroska_cluster[]
Definition: matroskadec.c:523
AVStream * stream
Definition: matroskadec.c:176
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
#define MATROSKA_ID_CODECNAME
Definition: matroska.h:85
char * language
Definition: matroskadec.c:155
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size, int64_t pos, uint64_t cluster_time, uint64_t block_duration, int is_keyframe, int64_t cluster_pos)
Definition: matroskadec.c:2477
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:123
int initial_padding
Audio only.
Definition: avcodec.h:3579
const char data[16]
Definition: mxf.c:70
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:38
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1051
uint8_t * data
Definition: avcodec.h:1346
uint64_t typevalue
Definition: matroskadec.c:208
uint64_t codec_delay
Definition: matroskadec.c:163
static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska)
Definition: matroskadec.c:2584
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:106
#define MATROSKA_ID_EDITIONUID
Definition: matroska.h:199
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size, int parse_picture)
static int flags
Definition: log.c:50
uint32_t tag
Definition: movenc.c:854
static EbmlSyntax ebml_header[]
Definition: matroskadec.c:285
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define MATROSKA_ID_CODECDECODEALL
Definition: matroska.h:88
enum AVCodecID id
Definition: internal.h:43
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:21
static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska, AVPacket *pkt, uint64_t display_duration)
Definition: matroskadec.c:1184
#define MATROSKA_ID_CUES
Definition: matroska.h:58
#define EBML_MAX_DEPTH
Definition: matroska.h:269
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:761
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:72
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_WL16(p, val)
Definition: intreadwrite.h:231
static EbmlSyntax matroska_tracks[]
Definition: matroskadec.c:386
EbmlList sub
Definition: matroskadec.c:203
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
Definition: matroskadec.c:2639
static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, void *data)
Definition: matroskadec.c:858
#define MATROSKA_ID_CUEBLOCKNUMBER
Definition: matroska.h:146
#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:99
uint64_t display_height
Definition: matroskadec.c:122
#define src
Definition: vp8dsp.c:254
static EbmlSyntax matroska_info[]
Definition: matroskadec.c:301
#define MATROSKA_ID_ENCODINGORDER
Definition: matroska.h:129
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:117
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
#define AVINDEX_KEYFRAME
Definition: avformat.h:666
EbmlType type
Definition: matroskadec.c:78
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:168
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:179
#define EBML_ID_EBMLREADVERSION
Definition: matroska.h:37
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1255
static const uint16_t mask[17]
Definition: lzw.c:38
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb, uint64_t *number)
Read a EBML length value.
Definition: matroskadec.c:677
#define AV_RB16
Definition: intreadwrite.h:53
AVChapter * chapter
Definition: matroskadec.c:185
#define AVERROR(e)
Definition: error.h:43
static EbmlSyntax matroska_index[]
Definition: matroskadec.c:450
int64_t timestamp
Definition: avformat.h:665
#define MATROSKA_ID_CLUSTER
Definition: matroska.h:62
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:187
static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska, uint8_t *data, uint32_t size, uint64_t *num)
Definition: matroskadec.c:796
uint64_t display_width
Definition: matroskadec.c:121
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, uint8_t *data, int size, uint64_t timecode, uint64_t duration, int64_t pos)
Definition: matroskadec.c:2225
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
#define MATROSKA_ID_TAGDEFAULT_BUG
Definition: matroska.h:155
Definition: graph2dot.c:48
static EbmlSyntax matroska_clusters[]
Definition: matroskadec.c:532
static EbmlSyntax matroska_chapter[]
Definition: matroskadec.c:423
enum AVCodecID id
Definition: matroska.h:265
#define MATROSKA_ID_VIDEOPIXELCROPR
Definition: matroska.h:113
#define MATROSKA_ID_ENCODINGCOMPSETTINGS
Definition: matroska.h:134
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
#define MATROSKA_ID_CHAPTERFLAGHIDDEN
Definition: matroska.h:204
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
uint64_t timecode
Definition: matroskadec.c:230
#define CONFIG_ZLIB
Definition: config.h:387
#define FFMAX(a, b)
Definition: common.h:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define fail()
Definition: checkasm.h:80
static EbmlSyntax matroska_attachments[]
Definition: matroskadec.c:400
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
static EbmlSyntax matroska_cluster_incremental_parsing[]
Definition: matroskadec.c:541
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
Only parse headers, do not repack.
Definition: avformat.h:658
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
static void ebml_free(EbmlSyntax *syntax, void *data)
Definition: matroskadec.c:967
const CodecMime ff_mkv_mime_tags[]
Definition: matroska.c:102
int nb_elem
Definition: matroskadec.c:90
#define MATROSKA_ID_TAG
Definition: matroska.h:149
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:401
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
char * lang
Definition: matroskadec.c:201
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:684
static EbmlSyntax matroska_chapters[]
Definition: matroskadec.c:432
uint64_t skip_to_timecode
Definition: matroskadec.c:265
Definition: dct.c:52
int block_align
Audio only.
Definition: avcodec.h:3571
static int matroska_read_header(AVFormatContext *s)
Definition: matroskadec.c:1929
static void matroska_parse_cues(MatroskaDemuxContext *matroska)
Definition: matroskadec.c:1420
const char * name
Definition: qsvenc.c:44
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
Definition: matroskadec.c:1393
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:134
uint64_t start
Definition: matroskadec.c:180
#define EBML_ID_EBMLVERSION
Definition: matroska.h:36
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
#define FFMIN(a, b)
Definition: common.h:66
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:156
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
AVPacket ** packets
Definition: matroskadec.c:257
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:151
#define MATROSKA_ID_TRACKTIMECODESCALE
Definition: matroska.h:101
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int ebml_parse_elem(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, void *data)
Definition: matroskadec.c:889
static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb, int max_size, uint64_t *number)
Definition: matroskadec.c:631
enum AVCodecID codec_id
Definition: avconv_vaapi.c:149
#define MATROSKA_ID_CHAPTERFLAGENABLED
Definition: matroska.h:205
uint64_t id_length
Definition: matroskadec.c:103
static EbmlSyntax matroska_track_encodings[]
Definition: matroskadec.c:353
static MatroskaTrack * matroska_find_track_by_num(MatroskaDemuxContext *matroska, int num)
Definition: matroskadec.c:1041
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:150
uint64_t doctype_version
Definition: matroskadec.c:105
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static EbmlSyntax matroska_track_encoding[]
Definition: matroskadec.c:345
#define MATROSKA_ID_TRACKMAXCACHE
Definition: matroska.h:97
int data_offset
Definition: matroskadec.c:80
#define MATROSKA_ID_CHAPTERPHYSEQUIV
Definition: matroska.h:206
static int matroska_read_close(AVFormatContext *s)
Definition: matroskadec.c:2753
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
EbmlBin codec_priv
Definition: matroskadec.c:154
static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
Definition: matroskadec.c:727
static int matroska_decode_buffer(uint8_t **buf, int *buf_size, MatroskaTrack *track)
Definition: matroskadec.c:1055
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:193
#define AV_RL32
Definition: intreadwrite.h:146
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:134
#define AV_EF_EXPLODE
Definition: avcodec.h:2681
AVDictionary * metadata
Definition: avformat.h:772
Opaque data information usually sparse.
Definition: avutil.h:198
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:119
static EbmlSyntax matroska_segment[]
Definition: matroskadec.c:496
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
static int matroska_probe(AVProbeData *p)
Definition: matroskadec.c:999
#define EBML_ID_VOID
Definition: matroska.h:45
static void mkv_stereo_mode_display_mul(int stereo_mode, int *h_width, int *h_height)
Definition: matroskadec.c:1554
static void matroska_convert_tag(AVFormatContext *s, EbmlList *list, AVDictionary **metadata, char *prefix)
Definition: matroskadec.c:1241
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:122
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:694
double f
Definition: matroskadec.c:83
#define MATROSKA_ID_TRACKMINCACHE
Definition: matroska.h:96
static int matroska_parse_flac(AVFormatContext *s, MatroskaTrack *track, int *offset)
Definition: matroskadec.c:1479
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
if(ac->has_optimized_func)
static int matroska_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: matroskadec.c:2691
static EbmlSyntax matroska_seekhead[]
Definition: matroskadec.c:491
Stream structure.
Definition: avformat.h:705
static int matroska_parse_wavpack(MatroskaTrack *track, uint8_t *src, uint8_t **pdst, int *size)
Definition: matroskadec.c:2302
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
EbmlList encodings
Definition: matroskadec.c:162
char * codec_id
Definition: matroskadec.c:153
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:672
int64_t current_cluster_pos
Definition: matroskadec.c:271
#define MATROSKA_ID_VIDEOPIXELCROPB
Definition: matroska.h:110
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
static int matroska_parse_tracks(AVFormatContext *s)
Definition: matroskadec.c:1579
#define MATROSKA_ID_TRACKFLAGFORCED
Definition: matroska.h:94
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
version
Definition: ffv1enc.c:1091
static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
Definition: matroskadec.c:774
#define MATROSKA_ID_TAGDEFAULT
Definition: matroska.h:154
#define MATROSKA_ID_SEEKID
Definition: matroska.h:167
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
AVIOContext * pb
I/O context.
Definition: avformat.h:982
#define MATROSKA_ID_ENCODINGCOMPALGO
Definition: matroska.h:133
#define MATROSKA_ID_BLOCK
Definition: matroska.h:178
#define MATROSKA_ID_INFO
Definition: matroska.h:56
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition: matroska.h:159
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:153
static EbmlSyntax matroska_tags[]
Definition: matroskadec.c:480
uint64_t pixel_width
Definition: matroskadec.c:123
#define MATROSKA_ID_TRACKFLAGENABLED
Definition: matroska.h:92
#define MATROSKA_ID_TRACKS
Definition: matroska.h:57
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:90
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
uint64_t start
Definition: matroskadec.c:225
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:68
static EbmlSyntax matroska_cluster_incremental[]
Definition: matroskadec.c:555
int nb_index_entries
Definition: avformat.h:892
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:164
EbmlList pos
Definition: matroskadec.c:195
uint64_t u
Definition: matroskadec.c:82
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:192
int index
Definition: gxfenc.c:72
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:174
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:109
rational number numerator/denominator
Definition: rational.h:43
AVStream * stream
Definition: matroskadec.c:165
static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, int *buf_size, int type, uint32_t **lace_buf, int *laces)
Definition: matroskadec.c:2116
#define MATROSKA_ID_CUETIME
Definition: matroska.h:140
static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskadec.c:2670
static EbmlSyntax matroska_blockgroup[]
Definition: matroskadec.c:513
static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, void *data)
Definition: matroskadec.c:845
static av_always_inline void flac_parse_block_header(const uint8_t *block_header, int *last, int *type, int *size)
Parse the metadata block parameters from the header.
Definition: flac.h:151
union EbmlSyntax::@140 def
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:405
AVFormatContext * ctx
Definition: matroskadec.c:235
#define MATROSKA_ID_TRACKVIDEO
Definition: matroska.h:82
int size
Definition: matroskadec.c:95
int error
contains the error code or 0 if no error happened
Definition: avio.h:138
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, uint32_t id, void *data)
Definition: matroskadec.c:826
int list_elem_size
Definition: matroskadec.c:79
static EbmlSyntax ebml_syntax[]
Definition: matroskadec.c:296
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:91
mfxU16 profile
Definition: qsvenc.c:43
uint64_t field_order
Definition: matroskadec.c:127
#define MATROSKA_ID_VIDEOFRAMERATE
Definition: matroska.h:105
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:61
int64_t pos
Definition: matroskadec.c:97
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:196
uint8_t level
Definition: svq3.c:204
#define MATROSKA_ID_FILENAME
Definition: matroska.h:186
const int avpriv_mpeg4audio_sample_rates[16]
Definition: mpeg4audio.c:55
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:110
#define CONFIG_LZO
Definition: config.h:428
#define MATROSKA_ID_CODECID
Definition: matroska.h:83
static EbmlSyntax matroska_clusters_incremental[]
Definition: matroskadec.c:564
#define MATROSKA_ID_VIDEOFIELDORDER
Definition: matroska.h:116
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
A reference to a data buffer.
Definition: buffer.h:81
full parsing and repack
Definition: avformat.h:657
int64_t reference
Definition: matroskadec.c:280
void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
Perform 4-bit block reordering for SIPR data.
Definition: rmsipr.c:41
Main libavformat public API header.
uint64_t buf_timecode
Definition: matroskadec.c:144
uint64_t num
Definition: matroskadec.c:149
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:1375
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:144
static EbmlSyntax matroska_track_encoding_compression[]
Definition: matroskadec.c:339
EbmlType
Definition: matroskadec.c:63
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:168
static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos)
Definition: matroskadec.c:575
#define MATROSKA_ID_CODECDELAY
Definition: matroska.h:89
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:194
double time_scale
Definition: matroskadec.c:156
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:109
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
char * string
Definition: matroskadec.c:200
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:750
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1170
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
char * doctype
Definition: matroskadec.c:104
static EbmlSyntax matroska_seekhead_entry[]
Definition: matroskadec.c:485
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:107
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:31
int den
denominator
Definition: rational.h:45
#define MATROSKA_ID_SEGMENT
Definition: matroska.h:53
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1379
uint32_t id
Definition: matroskadec.c:77
static EbmlSyntax matroska_tagtargets[]
Definition: matroskadec.c:465
MatroskaTagTarget target
Definition: matroskadec.c:215
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
#define MKBETAG(a, b, c, d)
Definition: common.h:257
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
#define MATROSKA_ID_SEEKHEAD
Definition: matroska.h:60
#define EBML_ID_HEADER
Definition: matroska.h:33
#define MATROSKA_ID_ENCODINGCOMPRESSION
Definition: matroska.h:132
#define MATROSKA_ID_CLUSTERPREVSIZE
Definition: matroska.h:173
char * value
Definition: dict.h:74
int eof_reached
true if eof reached
Definition: avio.h:132
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:137
int len
#define MATROSKA_ID_FILEUID
Definition: matroska.h:189
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size)
Definition: riffdec.c:82
static uint8_t tmp[8]
Definition: des.c:38
#define MATROSKA_ID_VIDEOPIXELCROPL
Definition: matroska.h:112
void * priv_data
Format private data.
Definition: avformat.h:968
uint64_t non_simple
Definition: matroskadec.c:281
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:203
static EbmlSyntax matroska_index_entry[]
Definition: matroskadec.c:444
static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
Definition: matroskadec.c:750
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:114
int channels
Audio only.
Definition: avcodec.h:3560
static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
Definition: matroskadec.c:690
#define MATROSKA_ID_TRACKMAXBLKADDID
Definition: matroska.h:102
#define AV_WL32(p, val)
Definition: intreadwrite.h:263
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1035
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:197
FILE * out
Definition: movenc.c:54
static int matroska_merge_packets(AVPacket *out, AVPacket *in)
Definition: matroskadec.c:1227
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:152
const CodecMime ff_mkv_image_mime_tags[]
Definition: matroska.c:93
AVCodecParameters * codecpar
Definition: avformat.h:831
static EbmlSyntax matroska_index_pos[]
Definition: matroskadec.c:437
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3487
static int ebml_read_float(AVIOContext *pb, int size, double *num)
Definition: matroskadec.c:709
#define MATROSKA_ID_DURATION
Definition: matroska.h:67
uint64_t length
Definition: matroskadec.c:226
static int matroska_aac_profile(char *codec_id)
Definition: matroskadec.c:1458
int stream_index
Definition: avcodec.h:1348
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
static void matroska_clear_queue(MatroskaDemuxContext *matroska)
Definition: matroskadec.c:2102
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
static EbmlSyntax matroska_chapter_entry[]
Definition: matroskadec.c:411
#define CONFIG_BZLIB
Definition: config.h:356
#define MKTAG(a, b, c, d)
Definition: common.h:256
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:763
#define MATROSKA_ID_ATTACHEDFILE
Definition: matroska.h:184
EbmlList blocks
Definition: matroskadec.c:231
This structure stores compressed data.
Definition: avcodec.h:1323
static EbmlSyntax matroska_chapter_display[]
Definition: matroskadec.c:405
#define MATROSKA_ID_CODECSTATE
Definition: matroska.h:181
uint64_t default_duration
Definition: matroskadec.c:157
static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, uint8_t *data, int pkt_size, uint64_t timecode, uint64_t duration, int64_t pos, int is_keyframe)
Definition: matroskadec.c:2382
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
#define MATROSKA_ID_CODECINFOURL
Definition: matroska.h:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:108
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:790
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:81
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
#define av_unused
Definition: attributes.h:86
#define MATROSKA_ID_ENCODINGSCOPE
Definition: matroska.h:130
const CodecTags ff_mkv_codec_tags[]
Definition: matroska.c:26
static const char *const matroska_doctypes[]
Definition: matroskadec.c:573
static EbmlSyntax matroska_track_video[]
Definition: matroskadec.c:312
uint64_t attachuid
Definition: matroskadec.c:211