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