2 * Matroska file demuxer
3 * Copyright (c) 2003-2008 The FFmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavformat/matroskadec.c
24 * Matroska file demuxer
25 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
26 * with a little help from Moritz Bunkus <moritz@bunkus.org>
27 * totally reworked by Aurelien Jacobs <aurel@gnuage.org>
28 * Specs available on the Matroska project page: http://www.matroska.org/.
33 /* For codec_get_id(). */
37 #include "libavcodec/mpeg4audio.h"
38 #include "libavutil/intfloat_readwrite.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/lzo.h"
61 typedef const struct EbmlSyntax
{
70 const struct EbmlSyntax
*n
;
90 uint64_t doctype_version
;
96 } MatroskaTrackCompression
;
101 MatroskaTrackCompression compression
;
102 } MatroskaTrackEncoding
;
106 uint64_t display_width
;
107 uint64_t display_height
;
108 uint64_t pixel_width
;
109 uint64_t pixel_height
;
111 } MatroskaTrackVideo
;
115 double out_samplerate
;
119 /* real audio header (extracted from extradata) */
127 } MatroskaTrackAudio
;
138 uint64_t default_duration
;
139 uint64_t flag_default
;
140 MatroskaTrackVideo video
;
141 MatroskaTrackAudio audio
;
145 int64_t end_timecode
;
155 } MatroskaAttachement
;
193 MatroskaTagTarget target
;
208 AVFormatContext
*ctx
;
212 MatroskaLevel levels
[EBML_MAX_DEPTH
];
219 EbmlList attachments
;
225 /* byte position of the segment inside the stream */
226 int64_t segment_start
;
228 /* the packet queue */
236 /* What to skip before effectively reading a packet. */
237 int skip_to_keyframe
;
238 uint64_t skip_to_timecode
;
239 } MatroskaDemuxContext
;
252 static EbmlSyntax ebml_header
[] = {
253 { EBML_ID_EBMLREADVERSION
, EBML_UINT
, 0, offsetof(Ebml
,version
), {.u
=EBML_VERSION
} },
254 { EBML_ID_EBMLMAXSIZELENGTH
, EBML_UINT
, 0, offsetof(Ebml
,max_size
), {.u
=8} },
255 { EBML_ID_EBMLMAXIDLENGTH
, EBML_UINT
, 0, offsetof(Ebml
,id_length
), {.u
=4} },
256 { EBML_ID_DOCTYPE
, EBML_STR
, 0, offsetof(Ebml
,doctype
), {.s
="(none)"} },
257 { EBML_ID_DOCTYPEREADVERSION
, EBML_UINT
, 0, offsetof(Ebml
,doctype_version
), {.u
=1} },
258 { EBML_ID_EBMLVERSION
, EBML_NONE
},
259 { EBML_ID_DOCTYPEVERSION
, EBML_NONE
},
263 static EbmlSyntax ebml_syntax
[] = {
264 { EBML_ID_HEADER
, EBML_NEST
, 0, 0, {.n
=ebml_header
} },
268 static EbmlSyntax matroska_info
[] = {
269 { MATROSKA_ID_TIMECODESCALE
, EBML_UINT
, 0, offsetof(MatroskaDemuxContext
,time_scale
), {.u
=1000000} },
270 { MATROSKA_ID_DURATION
, EBML_FLOAT
, 0, offsetof(MatroskaDemuxContext
,duration
) },
271 { MATROSKA_ID_TITLE
, EBML_UTF8
, 0, offsetof(MatroskaDemuxContext
,title
) },
272 { MATROSKA_ID_WRITINGAPP
, EBML_NONE
},
273 { MATROSKA_ID_MUXINGAPP
, EBML_NONE
},
274 { MATROSKA_ID_DATEUTC
, EBML_NONE
},
275 { MATROSKA_ID_SEGMENTUID
, EBML_NONE
},
279 static EbmlSyntax matroska_track_video
[] = {
280 { MATROSKA_ID_VIDEOFRAMERATE
, EBML_FLOAT
,0, offsetof(MatroskaTrackVideo
,frame_rate
) },
281 { MATROSKA_ID_VIDEODISPLAYWIDTH
, EBML_UINT
, 0, offsetof(MatroskaTrackVideo
,display_width
) },
282 { MATROSKA_ID_VIDEODISPLAYHEIGHT
, EBML_UINT
, 0, offsetof(MatroskaTrackVideo
,display_height
) },
283 { MATROSKA_ID_VIDEOPIXELWIDTH
, EBML_UINT
, 0, offsetof(MatroskaTrackVideo
,pixel_width
) },
284 { MATROSKA_ID_VIDEOPIXELHEIGHT
, EBML_UINT
, 0, offsetof(MatroskaTrackVideo
,pixel_height
) },
285 { MATROSKA_ID_VIDEOCOLORSPACE
, EBML_UINT
, 0, offsetof(MatroskaTrackVideo
,fourcc
) },
286 { MATROSKA_ID_VIDEOPIXELCROPB
, EBML_NONE
},
287 { MATROSKA_ID_VIDEOPIXELCROPT
, EBML_NONE
},
288 { MATROSKA_ID_VIDEOPIXELCROPL
, EBML_NONE
},
289 { MATROSKA_ID_VIDEOPIXELCROPR
, EBML_NONE
},
290 { MATROSKA_ID_VIDEODISPLAYUNIT
, EBML_NONE
},
291 { MATROSKA_ID_VIDEOFLAGINTERLACED
,EBML_NONE
},
292 { MATROSKA_ID_VIDEOSTEREOMODE
, EBML_NONE
},
293 { MATROSKA_ID_VIDEOASPECTRATIO
, EBML_NONE
},
297 static EbmlSyntax matroska_track_audio
[] = {
298 { MATROSKA_ID_AUDIOSAMPLINGFREQ
, EBML_FLOAT
,0, offsetof(MatroskaTrackAudio
,samplerate
), {.f
=8000.0} },
299 { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
,EBML_FLOAT
,0,offsetof(MatroskaTrackAudio
,out_samplerate
) },
300 { MATROSKA_ID_AUDIOBITDEPTH
, EBML_UINT
, 0, offsetof(MatroskaTrackAudio
,bitdepth
) },
301 { MATROSKA_ID_AUDIOCHANNELS
, EBML_UINT
, 0, offsetof(MatroskaTrackAudio
,channels
), {.u
=1} },
305 static EbmlSyntax matroska_track_encoding_compression
[] = {
306 { MATROSKA_ID_ENCODINGCOMPALGO
, EBML_UINT
, 0, offsetof(MatroskaTrackCompression
,algo
), {.u
=0} },
307 { MATROSKA_ID_ENCODINGCOMPSETTINGS
,EBML_BIN
, 0, offsetof(MatroskaTrackCompression
,settings
) },
311 static EbmlSyntax matroska_track_encoding
[] = {
312 { MATROSKA_ID_ENCODINGSCOPE
, EBML_UINT
, 0, offsetof(MatroskaTrackEncoding
,scope
), {.u
=1} },
313 { MATROSKA_ID_ENCODINGTYPE
, EBML_UINT
, 0, offsetof(MatroskaTrackEncoding
,type
), {.u
=0} },
314 { MATROSKA_ID_ENCODINGCOMPRESSION
,EBML_NEST
, 0, offsetof(MatroskaTrackEncoding
,compression
), {.n
=matroska_track_encoding_compression
} },
315 { MATROSKA_ID_ENCODINGORDER
, EBML_NONE
},
319 static EbmlSyntax matroska_track_encodings
[] = {
320 { MATROSKA_ID_TRACKCONTENTENCODING
, EBML_NEST
, sizeof(MatroskaTrackEncoding
), offsetof(MatroskaTrack
,encodings
), {.n
=matroska_track_encoding
} },
324 static EbmlSyntax matroska_track
[] = {
325 { MATROSKA_ID_TRACKNUMBER
, EBML_UINT
, 0, offsetof(MatroskaTrack
,num
) },
326 { MATROSKA_ID_TRACKNAME
, EBML_UTF8
, 0, offsetof(MatroskaTrack
,name
) },
327 { MATROSKA_ID_TRACKUID
, EBML_UINT
, 0, offsetof(MatroskaTrack
,uid
) },
328 { MATROSKA_ID_TRACKTYPE
, EBML_UINT
, 0, offsetof(MatroskaTrack
,type
) },
329 { MATROSKA_ID_CODECID
, EBML_STR
, 0, offsetof(MatroskaTrack
,codec_id
) },
330 { MATROSKA_ID_CODECPRIVATE
, EBML_BIN
, 0, offsetof(MatroskaTrack
,codec_priv
) },
331 { MATROSKA_ID_TRACKLANGUAGE
, EBML_UTF8
, 0, offsetof(MatroskaTrack
,language
), {.s
="eng"} },
332 { MATROSKA_ID_TRACKDEFAULTDURATION
, EBML_UINT
, 0, offsetof(MatroskaTrack
,default_duration
) },
333 { MATROSKA_ID_TRACKTIMECODESCALE
, EBML_FLOAT
,0, offsetof(MatroskaTrack
,time_scale
), {.f
=1.0} },
334 { MATROSKA_ID_TRACKFLAGDEFAULT
, EBML_UINT
, 0, offsetof(MatroskaTrack
,flag_default
), {.u
=1} },
335 { MATROSKA_ID_TRACKVIDEO
, EBML_NEST
, 0, offsetof(MatroskaTrack
,video
), {.n
=matroska_track_video
} },
336 { MATROSKA_ID_TRACKAUDIO
, EBML_NEST
, 0, offsetof(MatroskaTrack
,audio
), {.n
=matroska_track_audio
} },
337 { MATROSKA_ID_TRACKCONTENTENCODINGS
,EBML_NEST
, 0, 0, {.n
=matroska_track_encodings
} },
338 { MATROSKA_ID_TRACKFLAGENABLED
, EBML_NONE
},
339 { MATROSKA_ID_TRACKFLAGFORCED
, EBML_NONE
},
340 { MATROSKA_ID_TRACKFLAGLACING
, EBML_NONE
},
341 { MATROSKA_ID_CODECNAME
, EBML_NONE
},
342 { MATROSKA_ID_CODECDECODEALL
, EBML_NONE
},
343 { MATROSKA_ID_CODECINFOURL
, EBML_NONE
},
344 { MATROSKA_ID_CODECDOWNLOADURL
, EBML_NONE
},
345 { MATROSKA_ID_TRACKMINCACHE
, EBML_NONE
},
346 { MATROSKA_ID_TRACKMAXCACHE
, EBML_NONE
},
347 { MATROSKA_ID_TRACKMAXBLKADDID
, EBML_NONE
},
351 static EbmlSyntax matroska_tracks
[] = {
352 { MATROSKA_ID_TRACKENTRY
, EBML_NEST
, sizeof(MatroskaTrack
), offsetof(MatroskaDemuxContext
,tracks
), {.n
=matroska_track
} },
356 static EbmlSyntax matroska_attachment
[] = {
357 { MATROSKA_ID_FILEUID
, EBML_UINT
, 0, offsetof(MatroskaAttachement
,uid
) },
358 { MATROSKA_ID_FILENAME
, EBML_UTF8
, 0, offsetof(MatroskaAttachement
,filename
) },
359 { MATROSKA_ID_FILEMIMETYPE
, EBML_STR
, 0, offsetof(MatroskaAttachement
,mime
) },
360 { MATROSKA_ID_FILEDATA
, EBML_BIN
, 0, offsetof(MatroskaAttachement
,bin
) },
361 { MATROSKA_ID_FILEDESC
, EBML_NONE
},
365 static EbmlSyntax matroska_attachments
[] = {
366 { MATROSKA_ID_ATTACHEDFILE
, EBML_NEST
, sizeof(MatroskaAttachement
), offsetof(MatroskaDemuxContext
,attachments
), {.n
=matroska_attachment
} },
370 static EbmlSyntax matroska_chapter_display
[] = {
371 { MATROSKA_ID_CHAPSTRING
, EBML_UTF8
, 0, offsetof(MatroskaChapter
,title
) },
372 { MATROSKA_ID_CHAPLANG
, EBML_NONE
},
376 static EbmlSyntax matroska_chapter_entry
[] = {
377 { MATROSKA_ID_CHAPTERTIMESTART
, EBML_UINT
, 0, offsetof(MatroskaChapter
,start
), {.u
=AV_NOPTS_VALUE
} },
378 { MATROSKA_ID_CHAPTERTIMEEND
, EBML_UINT
, 0, offsetof(MatroskaChapter
,end
), {.u
=AV_NOPTS_VALUE
} },
379 { MATROSKA_ID_CHAPTERUID
, EBML_UINT
, 0, offsetof(MatroskaChapter
,uid
) },
380 { MATROSKA_ID_CHAPTERDISPLAY
, EBML_NEST
, 0, 0, {.n
=matroska_chapter_display
} },
381 { MATROSKA_ID_CHAPTERFLAGHIDDEN
, EBML_NONE
},
382 { MATROSKA_ID_CHAPTERFLAGENABLED
, EBML_NONE
},
383 { MATROSKA_ID_CHAPTERPHYSEQUIV
, EBML_NONE
},
384 { MATROSKA_ID_CHAPTERATOM
, EBML_NONE
},
388 static EbmlSyntax matroska_chapter
[] = {
389 { MATROSKA_ID_CHAPTERATOM
, EBML_NEST
, sizeof(MatroskaChapter
), offsetof(MatroskaDemuxContext
,chapters
), {.n
=matroska_chapter_entry
} },
390 { MATROSKA_ID_EDITIONUID
, EBML_NONE
},
391 { MATROSKA_ID_EDITIONFLAGHIDDEN
, EBML_NONE
},
392 { MATROSKA_ID_EDITIONFLAGDEFAULT
, EBML_NONE
},
393 { MATROSKA_ID_EDITIONFLAGORDERED
, EBML_NONE
},
397 static EbmlSyntax matroska_chapters
[] = {
398 { MATROSKA_ID_EDITIONENTRY
, EBML_NEST
, 0, 0, {.n
=matroska_chapter
} },
402 static EbmlSyntax matroska_index_pos
[] = {
403 { MATROSKA_ID_CUETRACK
, EBML_UINT
, 0, offsetof(MatroskaIndexPos
,track
) },
404 { MATROSKA_ID_CUECLUSTERPOSITION
, EBML_UINT
, 0, offsetof(MatroskaIndexPos
,pos
) },
405 { MATROSKA_ID_CUEBLOCKNUMBER
, EBML_NONE
},
409 static EbmlSyntax matroska_index_entry
[] = {
410 { MATROSKA_ID_CUETIME
, EBML_UINT
, 0, offsetof(MatroskaIndex
,time
) },
411 { MATROSKA_ID_CUETRACKPOSITION
, EBML_NEST
, sizeof(MatroskaIndexPos
), offsetof(MatroskaIndex
,pos
), {.n
=matroska_index_pos
} },
415 static EbmlSyntax matroska_index
[] = {
416 { MATROSKA_ID_POINTENTRY
, EBML_NEST
, sizeof(MatroskaIndex
), offsetof(MatroskaDemuxContext
,index
), {.n
=matroska_index_entry
} },
420 static EbmlSyntax matroska_simpletag
[] = {
421 { MATROSKA_ID_TAGNAME
, EBML_UTF8
, 0, offsetof(MatroskaTag
,name
) },
422 { MATROSKA_ID_TAGSTRING
, EBML_UTF8
, 0, offsetof(MatroskaTag
,string
) },
423 { MATROSKA_ID_TAGLANG
, EBML_STR
, 0, offsetof(MatroskaTag
,lang
), {.s
="und"} },
424 { MATROSKA_ID_TAGDEFAULT
, EBML_UINT
, 0, offsetof(MatroskaTag
,def
) },
425 { MATROSKA_ID_SIMPLETAG
, EBML_NEST
, sizeof(MatroskaTag
), offsetof(MatroskaTag
,sub
), {.n
=matroska_simpletag
} },
429 static EbmlSyntax matroska_tagtargets
[] = {
430 { MATROSKA_ID_TAGTARGETS_TYPE
, EBML_STR
, 0, offsetof(MatroskaTagTarget
,type
) },
431 { MATROSKA_ID_TAGTARGETS_TYPEVALUE
, EBML_UINT
, 0, offsetof(MatroskaTagTarget
,typevalue
), {.u
=50} },
432 { MATROSKA_ID_TAGTARGETS_TRACKUID
, EBML_UINT
, 0, offsetof(MatroskaTagTarget
,trackuid
) },
433 { MATROSKA_ID_TAGTARGETS_CHAPTERUID
,EBML_UINT
, 0, offsetof(MatroskaTagTarget
,chapteruid
) },
434 { MATROSKA_ID_TAGTARGETS_ATTACHUID
, EBML_UINT
, 0, offsetof(MatroskaTagTarget
,attachuid
) },
438 static EbmlSyntax matroska_tag
[] = {
439 { MATROSKA_ID_SIMPLETAG
, EBML_NEST
, sizeof(MatroskaTag
), offsetof(MatroskaTags
,tag
), {.n
=matroska_simpletag
} },
440 { MATROSKA_ID_TAGTARGETS
, EBML_NEST
, 0, offsetof(MatroskaTags
,target
), {.n
=matroska_tagtargets
} },
444 static EbmlSyntax matroska_tags
[] = {
445 { MATROSKA_ID_TAG
, EBML_NEST
, sizeof(MatroskaTags
), offsetof(MatroskaDemuxContext
,tags
), {.n
=matroska_tag
} },
449 static EbmlSyntax matroska_seekhead_entry
[] = {
450 { MATROSKA_ID_SEEKID
, EBML_UINT
, 0, offsetof(MatroskaSeekhead
,id
) },
451 { MATROSKA_ID_SEEKPOSITION
, EBML_UINT
, 0, offsetof(MatroskaSeekhead
,pos
), {.u
=-1} },
455 static EbmlSyntax matroska_seekhead
[] = {
456 { MATROSKA_ID_SEEKENTRY
, EBML_NEST
, sizeof(MatroskaSeekhead
), offsetof(MatroskaDemuxContext
,seekhead
), {.n
=matroska_seekhead_entry
} },
460 static EbmlSyntax matroska_segment
[] = {
461 { MATROSKA_ID_INFO
, EBML_NEST
, 0, 0, {.n
=matroska_info
} },
462 { MATROSKA_ID_TRACKS
, EBML_NEST
, 0, 0, {.n
=matroska_tracks
} },
463 { MATROSKA_ID_ATTACHMENTS
, EBML_NEST
, 0, 0, {.n
=matroska_attachments
} },
464 { MATROSKA_ID_CHAPTERS
, EBML_NEST
, 0, 0, {.n
=matroska_chapters
} },
465 { MATROSKA_ID_CUES
, EBML_NEST
, 0, 0, {.n
=matroska_index
} },
466 { MATROSKA_ID_TAGS
, EBML_NEST
, 0, 0, {.n
=matroska_tags
} },
467 { MATROSKA_ID_SEEKHEAD
, EBML_NEST
, 0, 0, {.n
=matroska_seekhead
} },
468 { MATROSKA_ID_CLUSTER
, EBML_STOP
, 0, offsetof(MatroskaDemuxContext
,has_cluster_id
) },
472 static EbmlSyntax matroska_segments
[] = {
473 { MATROSKA_ID_SEGMENT
, EBML_NEST
, 0, 0, {.n
=matroska_segment
} },
477 static EbmlSyntax matroska_blockgroup
[] = {
478 { MATROSKA_ID_BLOCK
, EBML_BIN
, 0, offsetof(MatroskaBlock
,bin
) },
479 { MATROSKA_ID_SIMPLEBLOCK
, EBML_BIN
, 0, offsetof(MatroskaBlock
,bin
) },
480 { MATROSKA_ID_BLOCKDURATION
, EBML_UINT
, 0, offsetof(MatroskaBlock
,duration
), {.u
=AV_NOPTS_VALUE
} },
481 { MATROSKA_ID_BLOCKREFERENCE
, EBML_UINT
, 0, offsetof(MatroskaBlock
,reference
) },
485 static EbmlSyntax matroska_cluster
[] = {
486 { MATROSKA_ID_CLUSTERTIMECODE
,EBML_UINT
,0, offsetof(MatroskaCluster
,timecode
) },
487 { MATROSKA_ID_BLOCKGROUP
, EBML_NEST
, sizeof(MatroskaBlock
), offsetof(MatroskaCluster
,blocks
), {.n
=matroska_blockgroup
} },
488 { MATROSKA_ID_SIMPLEBLOCK
, EBML_PASS
, sizeof(MatroskaBlock
), offsetof(MatroskaCluster
,blocks
), {.n
=matroska_blockgroup
} },
489 { MATROSKA_ID_CLUSTERPOSITION
,EBML_NONE
},
490 { MATROSKA_ID_CLUSTERPREVSIZE
,EBML_NONE
},
494 static EbmlSyntax matroska_clusters
[] = {
495 { MATROSKA_ID_CLUSTER
, EBML_NEST
, 0, 0, {.n
=matroska_cluster
} },
496 { MATROSKA_ID_INFO
, EBML_NONE
},
497 { MATROSKA_ID_CUES
, EBML_NONE
},
498 { MATROSKA_ID_TAGS
, EBML_NONE
},
499 { MATROSKA_ID_SEEKHEAD
, EBML_NONE
},
504 * Return: Whether we reached the end of a level in the hierarchy or not.
506 static int ebml_level_end(MatroskaDemuxContext
*matroska
)
508 ByteIOContext
*pb
= matroska
->ctx
->pb
;
509 int64_t pos
= url_ftell(pb
);
511 if (matroska
->num_levels
> 0) {
512 MatroskaLevel
*level
= &matroska
->levels
[matroska
->num_levels
- 1];
513 if (pos
- level
->start
>= level
->length
) {
514 matroska
->num_levels
--;
522 * Read: an "EBML number", which is defined as a variable-length
523 * array of bytes. The first byte indicates the length by giving a
524 * number of 0-bits followed by a one. The position of the first
525 * "one" bit inside the first byte indicates the length of this
527 * Returns: number of bytes read, < 0 on error
529 static int ebml_read_num(MatroskaDemuxContext
*matroska
, ByteIOContext
*pb
,
530 int max_size
, uint64_t *number
)
532 int len_mask
= 0x80, read
= 1, n
= 1;
535 /* The first byte tells us the length in bytes - get_byte() can normally
536 * return 0, but since that's not a valid first ebmlID byte, we can
537 * use it safely here to catch EOS. */
538 if (!(total
= get_byte(pb
))) {
539 /* we might encounter EOS here */
541 int64_t pos
= url_ftell(pb
);
542 av_log(matroska
->ctx
, AV_LOG_ERROR
,
543 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
546 return AVERROR(EIO
); /* EOS or actual I/O error */
549 /* get the length of the EBML number */
550 while (read
<= max_size
&& !(total
& len_mask
)) {
554 if (read
> max_size
) {
555 int64_t pos
= url_ftell(pb
) - 1;
556 av_log(matroska
->ctx
, AV_LOG_ERROR
,
557 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
558 (uint8_t) total
, pos
, pos
);
559 return AVERROR_INVALIDDATA
;
562 /* read out length */
565 total
= (total
<< 8) | get_byte(pb
);
573 * Read the next element as an unsigned int.
574 * 0 is success, < 0 is failure.
576 static int ebml_read_uint(ByteIOContext
*pb
, int size
, uint64_t *num
)
580 if (size
< 1 || size
> 8)
581 return AVERROR_INVALIDDATA
;
583 /* big-endian ordering; build up number */
586 *num
= (*num
<< 8) | get_byte(pb
);
592 * Read the next element as a float.
593 * 0 is success, < 0 is failure.
595 static int ebml_read_float(ByteIOContext
*pb
, int size
, double *num
)
598 *num
= av_int2flt(get_be32(pb
));
600 *num
= av_int2dbl(get_be64(pb
));
602 return AVERROR_INVALIDDATA
;
608 * Read the next element as an ASCII string.
609 * 0 is success, < 0 is failure.
611 static int ebml_read_ascii(ByteIOContext
*pb
, int size
, char **str
)
614 /* EBML strings are usually not 0-terminated, so we allocate one
615 * byte more, read the string and NULL-terminate it ourselves. */
616 if (!(*str
= av_malloc(size
+ 1)))
617 return AVERROR(ENOMEM
);
618 if (get_buffer(pb
, (uint8_t *) *str
, size
) != size
) {
628 * Read the next element as binary data.
629 * 0 is success, < 0 is failure.
631 static int ebml_read_binary(ByteIOContext
*pb
, int length
, EbmlBin
*bin
)
634 if (!(bin
->data
= av_malloc(length
)))
635 return AVERROR(ENOMEM
);
638 bin
->pos
= url_ftell(pb
);
639 if (get_buffer(pb
, bin
->data
, length
) != length
)
646 * Read the next element, but only the header. The contents
647 * are supposed to be sub-elements which can be read separately.
648 * 0 is success, < 0 is failure.
650 static int ebml_read_master(MatroskaDemuxContext
*matroska
, int length
)
652 ByteIOContext
*pb
= matroska
->ctx
->pb
;
653 MatroskaLevel
*level
;
655 if (matroska
->num_levels
>= EBML_MAX_DEPTH
) {
656 av_log(matroska
->ctx
, AV_LOG_ERROR
,
657 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH
);
658 return AVERROR(ENOSYS
);
661 level
= &matroska
->levels
[matroska
->num_levels
++];
662 level
->start
= url_ftell(pb
);
663 level
->length
= length
;
669 * Read signed/unsigned "EBML" numbers.
670 * Return: number of bytes processed, < 0 on error
672 static int matroska_ebmlnum_uint(MatroskaDemuxContext
*matroska
,
673 uint8_t *data
, uint32_t size
, uint64_t *num
)
676 init_put_byte(&pb
, data
, size
, 0, NULL
, NULL
, NULL
, NULL
);
677 return ebml_read_num(matroska
, &pb
, 8, num
);
681 * Same as above, but signed.
683 static int matroska_ebmlnum_sint(MatroskaDemuxContext
*matroska
,
684 uint8_t *data
, uint32_t size
, int64_t *num
)
689 /* read as unsigned number first */
690 if ((res
= matroska_ebmlnum_uint(matroska
, data
, size
, &unum
)) < 0)
693 /* make signed (weird way) */
694 *num
= unum
- ((1LL << (7*res
- 1)) - 1);
699 static int ebml_parse_elem(MatroskaDemuxContext
*matroska
,
700 EbmlSyntax
*syntax
, void *data
);
702 static int ebml_parse_id(MatroskaDemuxContext
*matroska
, EbmlSyntax
*syntax
,
703 uint32_t id
, void *data
)
706 for (i
=0; syntax
[i
].id
; i
++)
707 if (id
== syntax
[i
].id
)
709 if (!syntax
[i
].id
&& id
!= EBML_ID_VOID
&& id
!= EBML_ID_CRC32
)
710 av_log(matroska
->ctx
, AV_LOG_INFO
, "Unknown entry 0x%X\n", id
);
711 return ebml_parse_elem(matroska
, &syntax
[i
], data
);
714 static int ebml_parse(MatroskaDemuxContext
*matroska
, EbmlSyntax
*syntax
,
718 int res
= ebml_read_num(matroska
, matroska
->ctx
->pb
, 4, &id
);
720 return res
< 0 ? res
: ebml_parse_id(matroska
, syntax
, id
, data
);
723 static int ebml_parse_nest(MatroskaDemuxContext
*matroska
, EbmlSyntax
*syntax
,
728 for (i
=0; syntax
[i
].id
; i
++)
729 switch (syntax
[i
].type
) {
731 *(uint64_t *)((char *)data
+syntax
[i
].data_offset
) = syntax
[i
].def
.u
;
734 *(double *)((char *)data
+syntax
[i
].data_offset
) = syntax
[i
].def
.f
;
738 *(char **)((char *)data
+syntax
[i
].data_offset
) = av_strdup(syntax
[i
].def
.s
);
742 while (!res
&& !ebml_level_end(matroska
))
743 res
= ebml_parse(matroska
, syntax
, data
);
748 static int ebml_parse_elem(MatroskaDemuxContext
*matroska
,
749 EbmlSyntax
*syntax
, void *data
)
751 ByteIOContext
*pb
= matroska
->ctx
->pb
;
752 uint32_t id
= syntax
->id
;
756 data
= (char *)data
+ syntax
->data_offset
;
757 if (syntax
->list_elem_size
) {
758 EbmlList
*list
= data
;
759 list
->elem
= av_realloc(list
->elem
, (list
->nb_elem
+1)*syntax
->list_elem_size
);
760 data
= (char*)list
->elem
+ list
->nb_elem
*syntax
->list_elem_size
;
761 memset(data
, 0, syntax
->list_elem_size
);
765 if (syntax
->type
!= EBML_PASS
&& syntax
->type
!= EBML_STOP
)
766 if ((res
= ebml_read_num(matroska
, pb
, 8, &length
)) < 0)
769 switch (syntax
->type
) {
770 case EBML_UINT
: res
= ebml_read_uint (pb
, length
, data
); break;
771 case EBML_FLOAT
: res
= ebml_read_float (pb
, length
, data
); break;
773 case EBML_UTF8
: res
= ebml_read_ascii (pb
, length
, data
); break;
774 case EBML_BIN
: res
= ebml_read_binary(pb
, length
, data
); break;
775 case EBML_NEST
: if ((res
=ebml_read_master(matroska
, length
)) < 0)
777 if (id
== MATROSKA_ID_SEGMENT
)
778 matroska
->segment_start
= url_ftell(matroska
->ctx
->pb
);
779 return ebml_parse_nest(matroska
, syntax
->def
.n
, data
);
780 case EBML_PASS
: return ebml_parse_id(matroska
, syntax
->def
.n
, id
, data
);
781 case EBML_STOP
: *(int *)data
= 1; return 1;
782 default: return url_fseek(pb
,length
,SEEK_CUR
)<0 ? AVERROR(EIO
) : 0;
784 if (res
== AVERROR_INVALIDDATA
)
785 av_log(matroska
->ctx
, AV_LOG_ERROR
, "Invalid element\n");
786 else if (res
== AVERROR(EIO
))
787 av_log(matroska
->ctx
, AV_LOG_ERROR
, "Read error\n");
791 static void ebml_free(EbmlSyntax
*syntax
, void *data
)
794 for (i
=0; syntax
[i
].id
; i
++) {
795 void *data_off
= (char *)data
+ syntax
[i
].data_offset
;
796 switch (syntax
[i
].type
) {
798 case EBML_UTF8
: av_freep(data_off
); break;
799 case EBML_BIN
: av_freep(&((EbmlBin
*)data_off
)->data
); break;
801 if (syntax
[i
].list_elem_size
) {
802 EbmlList
*list
= data_off
;
803 char *ptr
= list
->elem
;
804 for (j
=0; j
<list
->nb_elem
; j
++, ptr
+=syntax
[i
].list_elem_size
)
805 ebml_free(syntax
[i
].def
.n
, ptr
);
808 ebml_free(syntax
[i
].def
.n
, data_off
);
818 static int matroska_probe(AVProbeData
*p
)
821 int len_mask
= 0x80, size
= 1, n
= 1;
822 static const char probe_data
[] = "matroska";
825 if (AV_RB32(p
->buf
) != EBML_ID_HEADER
)
828 /* length of header */
830 while (size
<= 8 && !(total
& len_mask
)) {
836 total
&= (len_mask
- 1);
838 total
= (total
<< 8) | p
->buf
[4 + n
++];
840 /* Does the probe data contain the whole header? */
841 if (p
->buf_size
< 4 + size
+ total
)
844 /* The header must contain the document type 'matroska'. For now,
845 * we don't parse the whole header but simply check for the
846 * availability of that array of characters inside the header.
847 * Not fully fool-proof, but good enough. */
848 for (n
= 4+size
; n
<= 4+size
+total
-(sizeof(probe_data
)-1); n
++)
849 if (!memcmp(p
->buf
+n
, probe_data
, sizeof(probe_data
)-1))
850 return AVPROBE_SCORE_MAX
;
855 static MatroskaTrack
*matroska_find_track_by_num(MatroskaDemuxContext
*matroska
,
858 MatroskaTrack
*tracks
= matroska
->tracks
.elem
;
861 for (i
=0; i
< matroska
->tracks
.nb_elem
; i
++)
862 if (tracks
[i
].num
== num
)
865 av_log(matroska
->ctx
, AV_LOG_ERROR
, "Invalid track number %d\n", num
);
869 static int matroska_decode_buffer(uint8_t** buf
, int* buf_size
,
870 MatroskaTrack
*track
)
872 MatroskaTrackEncoding
*encodings
= track
->encodings
.elem
;
873 uint8_t* data
= *buf
;
874 int isize
= *buf_size
;
875 uint8_t* pkt_data
= NULL
;
876 int pkt_size
= isize
;
880 switch (encodings
[0].compression
.algo
) {
881 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP
:
882 return encodings
[0].compression
.settings
.size
;
883 case MATROSKA_TRACK_ENCODING_COMP_LZO
:
885 olen
= pkt_size
*= 3;
886 pkt_data
= av_realloc(pkt_data
, pkt_size
+AV_LZO_OUTPUT_PADDING
);
887 result
= av_lzo1x_decode(pkt_data
, &olen
, data
, &isize
);
888 } while (result
==AV_LZO_OUTPUT_FULL
&& pkt_size
<10000000);
894 case MATROSKA_TRACK_ENCODING_COMP_ZLIB
: {
895 z_stream zstream
= {0};
896 if (inflateInit(&zstream
) != Z_OK
)
898 zstream
.next_in
= data
;
899 zstream
.avail_in
= isize
;
902 pkt_data
= av_realloc(pkt_data
, pkt_size
);
903 zstream
.avail_out
= pkt_size
- zstream
.total_out
;
904 zstream
.next_out
= pkt_data
+ zstream
.total_out
;
905 result
= inflate(&zstream
, Z_NO_FLUSH
);
906 } while (result
==Z_OK
&& pkt_size
<10000000);
907 pkt_size
= zstream
.total_out
;
908 inflateEnd(&zstream
);
909 if (result
!= Z_STREAM_END
)
915 case MATROSKA_TRACK_ENCODING_COMP_BZLIB
: {
916 bz_stream bzstream
= {0};
917 if (BZ2_bzDecompressInit(&bzstream
, 0, 0) != BZ_OK
)
919 bzstream
.next_in
= data
;
920 bzstream
.avail_in
= isize
;
923 pkt_data
= av_realloc(pkt_data
, pkt_size
);
924 bzstream
.avail_out
= pkt_size
- bzstream
.total_out_lo32
;
925 bzstream
.next_out
= pkt_data
+ bzstream
.total_out_lo32
;
926 result
= BZ2_bzDecompress(&bzstream
);
927 } while (result
==BZ_OK
&& pkt_size
<10000000);
928 pkt_size
= bzstream
.total_out_lo32
;
929 BZ2_bzDecompressEnd(&bzstream
);
930 if (result
!= BZ_STREAM_END
)
940 *buf_size
= pkt_size
;
947 static void matroska_fix_ass_packet(MatroskaDemuxContext
*matroska
,
948 AVPacket
*pkt
, uint64_t display_duration
)
950 char *line
, *layer
, *ptr
= pkt
->data
, *end
= ptr
+pkt
->size
;
951 for (; *ptr
!=',' && ptr
<end
-1; ptr
++);
954 for (; *ptr
!=',' && ptr
<end
-1; ptr
++);
956 int64_t end_pts
= pkt
->pts
+ display_duration
;
957 int sc
= matroska
->time_scale
* pkt
->pts
/ 10000000;
958 int ec
= matroska
->time_scale
* end_pts
/ 10000000;
959 int sh
, sm
, ss
, eh
, em
, es
, len
;
960 sh
= sc
/360000; sc
-= 360000*sh
;
961 sm
= sc
/ 6000; sc
-= 6000*sm
;
962 ss
= sc
/ 100; sc
-= 100*ss
;
963 eh
= ec
/360000; ec
-= 360000*eh
;
964 em
= ec
/ 6000; ec
-= 6000*em
;
965 es
= ec
/ 100; ec
-= 100*es
;
967 len
= 50 + end
-ptr
+ FF_INPUT_BUFFER_PADDING_SIZE
;
968 if (!(line
= av_malloc(len
)))
970 snprintf(line
,len
,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
971 layer
, sh
, sm
, ss
, sc
, eh
, em
, es
, ec
, ptr
);
974 pkt
->size
= strlen(line
);
978 static void matroska_merge_packets(AVPacket
*out
, AVPacket
*in
)
980 out
->data
= av_realloc(out
->data
, out
->size
+in
->size
);
981 memcpy(out
->data
+out
->size
, in
->data
, in
->size
);
982 out
->size
+= in
->size
;
983 av_destruct_packet(in
);
987 static void matroska_convert_tag(AVFormatContext
*s
, EbmlList
*list
,
988 AVMetadata
**metadata
, char *prefix
)
990 MatroskaTag
*tags
= list
->elem
;
994 for (i
=0; i
< list
->nb_elem
; i
++) {
995 const char *lang
= strcmp(tags
[i
].lang
, "und") ? tags
[i
].lang
: NULL
;
996 if (prefix
) snprintf(key
, sizeof(key
), "%s/%s", prefix
, tags
[i
].name
);
997 else av_strlcpy(key
, tags
[i
].name
, sizeof(key
));
998 if (tags
[i
].def
|| !lang
) {
999 av_metadata_set(metadata
, key
, tags
[i
].string
);
1000 if (tags
[i
].sub
.nb_elem
)
1001 matroska_convert_tag(s
, &tags
[i
].sub
, metadata
, key
);
1004 av_strlcat(key
, "-", sizeof(key
));
1005 av_strlcat(key
, lang
, sizeof(key
));
1006 av_metadata_set(metadata
, key
, tags
[i
].string
);
1007 if (tags
[i
].sub
.nb_elem
)
1008 matroska_convert_tag(s
, &tags
[i
].sub
, metadata
, key
);
1013 static void matroska_convert_tags(AVFormatContext
*s
)
1015 MatroskaDemuxContext
*matroska
= s
->priv_data
;
1016 MatroskaTags
*tags
= matroska
->tags
.elem
;
1019 for (i
=0; i
< matroska
->tags
.nb_elem
; i
++) {
1020 if (tags
[i
].target
.attachuid
) {
1021 MatroskaAttachement
*attachment
= matroska
->attachments
.elem
;
1022 for (j
=0; j
<matroska
->attachments
.nb_elem
; j
++)
1023 if (attachment
[j
].uid
== tags
[i
].target
.attachuid
)
1024 matroska_convert_tag(s
, &tags
[i
].tag
,
1025 &attachment
[j
].stream
->metadata
, NULL
);
1026 } else if (tags
[i
].target
.chapteruid
) {
1027 MatroskaChapter
*chapter
= matroska
->chapters
.elem
;
1028 for (j
=0; j
<matroska
->chapters
.nb_elem
; j
++)
1029 if (chapter
[j
].uid
== tags
[i
].target
.chapteruid
)
1030 matroska_convert_tag(s
, &tags
[i
].tag
,
1031 &chapter
[j
].chapter
->metadata
, NULL
);
1032 } else if (tags
[i
].target
.trackuid
) {
1033 MatroskaTrack
*track
= matroska
->tracks
.elem
;
1034 for (j
=0; j
<matroska
->tracks
.nb_elem
; j
++)
1035 if (track
[j
].uid
== tags
[i
].target
.trackuid
)
1036 matroska_convert_tag(s
, &tags
[i
].tag
,
1037 &track
[j
].stream
->metadata
, NULL
);
1039 matroska_convert_tag(s
, &tags
[i
].tag
, &s
->metadata
,
1040 tags
[i
].target
.type
);
1045 static void matroska_execute_seekhead(MatroskaDemuxContext
*matroska
)
1047 EbmlList
*seekhead_list
= &matroska
->seekhead
;
1048 MatroskaSeekhead
*seekhead
= seekhead_list
->elem
;
1049 uint32_t level_up
= matroska
->level_up
;
1050 int64_t before_pos
= url_ftell(matroska
->ctx
->pb
);
1051 MatroskaLevel level
;
1054 for (i
=0; i
<seekhead_list
->nb_elem
; i
++) {
1055 int64_t offset
= seekhead
[i
].pos
+ matroska
->segment_start
;
1057 if (seekhead
[i
].pos
<= before_pos
1058 || seekhead
[i
].id
== MATROSKA_ID_SEEKHEAD
1059 || seekhead
[i
].id
== MATROSKA_ID_CLUSTER
)
1063 if (url_fseek(matroska
->ctx
->pb
, offset
, SEEK_SET
) != offset
)
1066 /* We don't want to lose our seekhead level, so we add
1067 * a dummy. This is a crude hack. */
1068 if (matroska
->num_levels
== EBML_MAX_DEPTH
) {
1069 av_log(matroska
->ctx
, AV_LOG_INFO
,
1070 "Max EBML element depth (%d) reached, "
1071 "cannot parse further.\n", EBML_MAX_DEPTH
);
1076 level
.length
= (uint64_t)-1;
1077 matroska
->levels
[matroska
->num_levels
] = level
;
1078 matroska
->num_levels
++;
1080 ebml_parse(matroska
, matroska_segment
, matroska
);
1082 /* remove dummy level */
1083 while (matroska
->num_levels
) {
1084 uint64_t length
= matroska
->levels
[--matroska
->num_levels
].length
;
1085 if (length
== (uint64_t)-1)
1091 url_fseek(matroska
->ctx
->pb
, before_pos
, SEEK_SET
);
1092 matroska
->level_up
= level_up
;
1095 static int matroska_aac_profile(char *codec_id
)
1097 static const char * const aac_profiles
[] = { "MAIN", "LC", "SSR" };
1100 for (profile
=0; profile
<FF_ARRAY_ELEMS(aac_profiles
); profile
++)
1101 if (strstr(codec_id
, aac_profiles
[profile
]))
1106 static int matroska_aac_sri(int samplerate
)
1110 for (sri
=0; sri
<FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates
); sri
++)
1111 if (ff_mpeg4audio_sample_rates
[sri
] == samplerate
)
1116 static int matroska_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
1118 MatroskaDemuxContext
*matroska
= s
->priv_data
;
1119 EbmlList
*attachements_list
= &matroska
->attachments
;
1120 MatroskaAttachement
*attachements
;
1121 EbmlList
*chapters_list
= &matroska
->chapters
;
1122 MatroskaChapter
*chapters
;
1123 MatroskaTrack
*tracks
;
1124 EbmlList
*index_list
;
1125 MatroskaIndex
*index
;
1126 int index_scale
= 1;
1127 uint64_t max_start
= 0;
1134 /* First read the EBML header. */
1135 if (ebml_parse(matroska
, ebml_syntax
, &ebml
)
1136 || ebml
.version
> EBML_VERSION
|| ebml
.max_size
> sizeof(uint64_t)
1137 || ebml
.id_length
> sizeof(uint32_t) || strcmp(ebml
.doctype
, "matroska")
1138 || ebml
.doctype_version
> 2) {
1139 av_log(matroska
->ctx
, AV_LOG_ERROR
,
1140 "EBML header using unsupported features\n"
1141 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1142 ebml
.version
, ebml
.doctype
, ebml
.doctype_version
);
1143 return AVERROR_NOFMT
;
1145 ebml_free(ebml_syntax
, &ebml
);
1147 /* The next thing is a segment. */
1148 if (ebml_parse(matroska
, matroska_segments
, matroska
) < 0)
1150 matroska_execute_seekhead(matroska
);
1152 if (matroska
->duration
)
1153 matroska
->ctx
->duration
= matroska
->duration
* matroska
->time_scale
1154 * 1000 / AV_TIME_BASE
;
1155 av_metadata_set(&s
->metadata
, "title", matroska
->title
);
1157 tracks
= matroska
->tracks
.elem
;
1158 for (i
=0; i
< matroska
->tracks
.nb_elem
; i
++) {
1159 MatroskaTrack
*track
= &tracks
[i
];
1160 enum CodecID codec_id
= CODEC_ID_NONE
;
1161 EbmlList
*encodings_list
= &tracks
->encodings
;
1162 MatroskaTrackEncoding
*encodings
= encodings_list
->elem
;
1163 uint8_t *extradata
= NULL
;
1164 int extradata_size
= 0;
1165 int extradata_offset
= 0;
1168 /* Apply some sanity checks. */
1169 if (track
->type
!= MATROSKA_TRACK_TYPE_VIDEO
&&
1170 track
->type
!= MATROSKA_TRACK_TYPE_AUDIO
&&
1171 track
->type
!= MATROSKA_TRACK_TYPE_SUBTITLE
) {
1172 av_log(matroska
->ctx
, AV_LOG_INFO
,
1173 "Unknown or unsupported track type %"PRIu64
"\n",
1177 if (track
->codec_id
== NULL
)
1180 if (track
->type
== MATROSKA_TRACK_TYPE_VIDEO
) {
1181 if (!track
->default_duration
)
1182 track
->default_duration
= 1000000000/track
->video
.frame_rate
;
1183 if (!track
->video
.display_width
)
1184 track
->video
.display_width
= track
->video
.pixel_width
;
1185 if (!track
->video
.display_height
)
1186 track
->video
.display_height
= track
->video
.pixel_height
;
1187 } else if (track
->type
== MATROSKA_TRACK_TYPE_AUDIO
) {
1188 if (!track
->audio
.out_samplerate
)
1189 track
->audio
.out_samplerate
= track
->audio
.samplerate
;
1191 if (encodings_list
->nb_elem
> 1) {
1192 av_log(matroska
->ctx
, AV_LOG_ERROR
,
1193 "Multiple combined encodings no supported");
1194 } else if (encodings_list
->nb_elem
== 1) {
1195 if (encodings
[0].type
||
1196 (encodings
[0].compression
.algo
!= MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP
&&
1198 encodings
[0].compression
.algo
!= MATROSKA_TRACK_ENCODING_COMP_ZLIB
&&
1201 encodings
[0].compression
.algo
!= MATROSKA_TRACK_ENCODING_COMP_BZLIB
&&
1203 encodings
[0].compression
.algo
!= MATROSKA_TRACK_ENCODING_COMP_LZO
)) {
1204 encodings
[0].scope
= 0;
1205 av_log(matroska
->ctx
, AV_LOG_ERROR
,
1206 "Unsupported encoding type");
1207 } else if (track
->codec_priv
.size
&& encodings
[0].scope
&2) {
1208 uint8_t *codec_priv
= track
->codec_priv
.data
;
1209 int offset
= matroska_decode_buffer(&track
->codec_priv
.data
,
1210 &track
->codec_priv
.size
,
1213 track
->codec_priv
.data
= NULL
;
1214 track
->codec_priv
.size
= 0;
1215 av_log(matroska
->ctx
, AV_LOG_ERROR
,
1216 "Failed to decode codec private data\n");
1217 } else if (offset
> 0) {
1218 track
->codec_priv
.data
= av_malloc(track
->codec_priv
.size
+ offset
);
1219 memcpy(track
->codec_priv
.data
,
1220 encodings
[0].compression
.settings
.data
, offset
);
1221 memcpy(track
->codec_priv
.data
+offset
, codec_priv
,
1222 track
->codec_priv
.size
);
1223 track
->codec_priv
.size
+= offset
;
1225 if (codec_priv
!= track
->codec_priv
.data
)
1226 av_free(codec_priv
);
1230 for(j
=0; ff_mkv_codec_tags
[j
].id
!= CODEC_ID_NONE
; j
++){
1231 if(!strncmp(ff_mkv_codec_tags
[j
].str
, track
->codec_id
,
1232 strlen(ff_mkv_codec_tags
[j
].str
))){
1233 codec_id
= ff_mkv_codec_tags
[j
].id
;
1238 st
= track
->stream
= av_new_stream(s
, 0);
1240 return AVERROR(ENOMEM
);
1242 if (!strcmp(track
->codec_id
, "V_MS/VFW/FOURCC")
1243 && track
->codec_priv
.size
>= 40
1244 && track
->codec_priv
.data
!= NULL
) {
1245 track
->video
.fourcc
= AV_RL32(track
->codec_priv
.data
+ 16);
1246 codec_id
= codec_get_id(codec_bmp_tags
, track
->video
.fourcc
);
1247 } else if (!strcmp(track
->codec_id
, "A_MS/ACM")
1248 && track
->codec_priv
.size
>= 18
1249 && track
->codec_priv
.data
!= NULL
) {
1250 init_put_byte(&b
, track
->codec_priv
.data
, track
->codec_priv
.size
,
1251 URL_RDONLY
, NULL
, NULL
, NULL
, NULL
);
1252 get_wav_header(&b
, st
->codec
, track
->codec_priv
.size
);
1253 codec_id
= st
->codec
->codec_id
;
1254 extradata_offset
= 18;
1255 track
->codec_priv
.size
-= extradata_offset
;
1256 } else if (!strcmp(track
->codec_id
, "V_QUICKTIME")
1257 && (track
->codec_priv
.size
>= 86)
1258 && (track
->codec_priv
.data
!= NULL
)) {
1259 track
->video
.fourcc
= AV_RL32(track
->codec_priv
.data
);
1260 codec_id
=codec_get_id(codec_movvideo_tags
, track
->video
.fourcc
);
1261 } else if (codec_id
== CODEC_ID_PCM_S16BE
) {
1262 switch (track
->audio
.bitdepth
) {
1263 case 8: codec_id
= CODEC_ID_PCM_U8
; break;
1264 case 24: codec_id
= CODEC_ID_PCM_S24BE
; break;
1265 case 32: codec_id
= CODEC_ID_PCM_S32BE
; break;
1267 } else if (codec_id
== CODEC_ID_PCM_S16LE
) {
1268 switch (track
->audio
.bitdepth
) {
1269 case 8: codec_id
= CODEC_ID_PCM_U8
; break;
1270 case 24: codec_id
= CODEC_ID_PCM_S24LE
; break;
1271 case 32: codec_id
= CODEC_ID_PCM_S32LE
; break;
1273 } else if (codec_id
==CODEC_ID_PCM_F32LE
&& track
->audio
.bitdepth
==64) {
1274 codec_id
= CODEC_ID_PCM_F64LE
;
1275 } else if (codec_id
== CODEC_ID_AAC
&& !track
->codec_priv
.size
) {
1276 int profile
= matroska_aac_profile(track
->codec_id
);
1277 int sri
= matroska_aac_sri(track
->audio
.samplerate
);
1278 extradata
= av_malloc(5);
1279 if (extradata
== NULL
)
1280 return AVERROR(ENOMEM
);
1281 extradata
[0] = (profile
<< 3) | ((sri
&0x0E) >> 1);
1282 extradata
[1] = ((sri
&0x01) << 7) | (track
->audio
.channels
<<3);
1283 if (strstr(track
->codec_id
, "SBR")) {
1284 sri
= matroska_aac_sri(track
->audio
.out_samplerate
);
1285 extradata
[2] = 0x56;
1286 extradata
[3] = 0xE5;
1287 extradata
[4] = 0x80 | (sri
<<3);
1291 } else if (codec_id
== CODEC_ID_TTA
) {
1292 extradata_size
= 30;
1293 extradata
= av_mallocz(extradata_size
);
1294 if (extradata
== NULL
)
1295 return AVERROR(ENOMEM
);
1296 init_put_byte(&b
, extradata
, extradata_size
, 1,
1297 NULL
, NULL
, NULL
, NULL
);
1298 put_buffer(&b
, "TTA1", 4);
1300 put_le16(&b
, track
->audio
.channels
);
1301 put_le16(&b
, track
->audio
.bitdepth
);
1302 put_le32(&b
, track
->audio
.out_samplerate
);
1303 put_le32(&b
, matroska
->ctx
->duration
* track
->audio
.out_samplerate
);
1304 } else if (codec_id
== CODEC_ID_RV10
|| codec_id
== CODEC_ID_RV20
||
1305 codec_id
== CODEC_ID_RV30
|| codec_id
== CODEC_ID_RV40
) {
1306 extradata_offset
= 26;
1307 track
->codec_priv
.size
-= extradata_offset
;
1308 } else if (codec_id
== CODEC_ID_RA_144
) {
1309 track
->audio
.out_samplerate
= 8000;
1310 track
->audio
.channels
= 1;
1311 } else if (codec_id
== CODEC_ID_RA_288
|| codec_id
== CODEC_ID_COOK
||
1312 codec_id
== CODEC_ID_ATRAC3
) {
1313 init_put_byte(&b
, track
->codec_priv
.data
,track
->codec_priv
.size
,
1314 0, NULL
, NULL
, NULL
, NULL
);
1316 track
->audio
.coded_framesize
= get_be32(&b
);
1318 track
->audio
.sub_packet_h
= get_be16(&b
);
1319 track
->audio
.frame_size
= get_be16(&b
);
1320 track
->audio
.sub_packet_size
= get_be16(&b
);
1321 track
->audio
.buf
= av_malloc(track
->audio
.frame_size
* track
->audio
.sub_packet_h
);
1322 if (codec_id
== CODEC_ID_RA_288
) {
1323 st
->codec
->block_align
= track
->audio
.coded_framesize
;
1324 track
->codec_priv
.size
= 0;
1326 st
->codec
->block_align
= track
->audio
.sub_packet_size
;
1327 extradata_offset
= 78;
1328 track
->codec_priv
.size
-= extradata_offset
;
1332 if (codec_id
== CODEC_ID_NONE
)
1333 av_log(matroska
->ctx
, AV_LOG_INFO
,
1334 "Unknown/unsupported CodecID %s.\n", track
->codec_id
);
1336 if (track
->time_scale
< 0.01)
1337 track
->time_scale
= 1.0;
1338 av_set_pts_info(st
, 64, matroska
->time_scale
*track
->time_scale
, 1000*1000*1000); /* 64 bit pts in ns */
1340 st
->codec
->codec_id
= codec_id
;
1342 if (strcmp(track
->language
, "und"))
1343 av_metadata_set(&st
->metadata
, "language", track
->language
);
1344 av_metadata_set(&st
->metadata
, "description", track
->name
);
1346 if (track
->flag_default
)
1347 st
->disposition
|= AV_DISPOSITION_DEFAULT
;
1349 if (track
->default_duration
)
1350 av_reduce(&st
->codec
->time_base
.num
, &st
->codec
->time_base
.den
,
1351 track
->default_duration
, 1000000000, 30000);
1354 st
->codec
->extradata
= extradata
;
1355 st
->codec
->extradata_size
= extradata_size
;
1356 } else if(track
->codec_priv
.data
&& track
->codec_priv
.size
> 0){
1357 st
->codec
->extradata
= av_mallocz(track
->codec_priv
.size
+
1358 FF_INPUT_BUFFER_PADDING_SIZE
);
1359 if(st
->codec
->extradata
== NULL
)
1360 return AVERROR(ENOMEM
);
1361 st
->codec
->extradata_size
= track
->codec_priv
.size
;
1362 memcpy(st
->codec
->extradata
,
1363 track
->codec_priv
.data
+ extradata_offset
,
1364 track
->codec_priv
.size
);
1367 if (track
->type
== MATROSKA_TRACK_TYPE_VIDEO
) {
1368 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
1369 st
->codec
->codec_tag
= track
->video
.fourcc
;
1370 st
->codec
->width
= track
->video
.pixel_width
;
1371 st
->codec
->height
= track
->video
.pixel_height
;
1372 av_reduce(&st
->sample_aspect_ratio
.num
,
1373 &st
->sample_aspect_ratio
.den
,
1374 st
->codec
->height
* track
->video
.display_width
,
1375 st
->codec
-> width
* track
->video
.display_height
,
1377 st
->need_parsing
= AVSTREAM_PARSE_HEADERS
;
1378 } else if (track
->type
== MATROSKA_TRACK_TYPE_AUDIO
) {
1379 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
1380 st
->codec
->sample_rate
= track
->audio
.out_samplerate
;
1381 st
->codec
->channels
= track
->audio
.channels
;
1382 } else if (track
->type
== MATROSKA_TRACK_TYPE_SUBTITLE
) {
1383 st
->codec
->codec_type
= CODEC_TYPE_SUBTITLE
;
1387 attachements
= attachements_list
->elem
;
1388 for (j
=0; j
<attachements_list
->nb_elem
; j
++) {
1389 if (!(attachements
[j
].filename
&& attachements
[j
].mime
&&
1390 attachements
[j
].bin
.data
&& attachements
[j
].bin
.size
> 0)) {
1391 av_log(matroska
->ctx
, AV_LOG_ERROR
, "incomplete attachment\n");
1393 AVStream
*st
= av_new_stream(s
, 0);
1396 av_metadata_set(&st
->metadata
, "filename",attachements
[j
].filename
);
1397 st
->codec
->codec_id
= CODEC_ID_NONE
;
1398 st
->codec
->codec_type
= CODEC_TYPE_ATTACHMENT
;
1399 st
->codec
->extradata
= av_malloc(attachements
[j
].bin
.size
);
1400 if(st
->codec
->extradata
== NULL
)
1402 st
->codec
->extradata_size
= attachements
[j
].bin
.size
;
1403 memcpy(st
->codec
->extradata
, attachements
[j
].bin
.data
, attachements
[j
].bin
.size
);
1405 for (i
=0; ff_mkv_mime_tags
[i
].id
!= CODEC_ID_NONE
; i
++) {
1406 if (!strncmp(ff_mkv_mime_tags
[i
].str
, attachements
[j
].mime
,
1407 strlen(ff_mkv_mime_tags
[i
].str
))) {
1408 st
->codec
->codec_id
= ff_mkv_mime_tags
[i
].id
;
1412 attachements
[j
].stream
= st
;
1416 chapters
= chapters_list
->elem
;
1417 for (i
=0; i
<chapters_list
->nb_elem
; i
++)
1418 if (chapters
[i
].start
!= AV_NOPTS_VALUE
&& chapters
[i
].uid
1419 && (max_start
==0 || chapters
[i
].start
> max_start
)) {
1420 chapters
[i
].chapter
=
1421 ff_new_chapter(s
, chapters
[i
].uid
, (AVRational
){1, 1000000000},
1422 chapters
[i
].start
, chapters
[i
].end
,
1424 av_metadata_set(&chapters
[i
].chapter
->metadata
,
1425 "title", chapters
[i
].title
);
1426 max_start
= chapters
[i
].start
;
1429 index_list
= &matroska
->index
;
1430 index
= index_list
->elem
;
1431 if (index_list
->nb_elem
1432 && index
[0].time
> 100000000000000/matroska
->time_scale
) {
1433 av_log(matroska
->ctx
, AV_LOG_WARNING
, "Working around broken index.\n");
1434 index_scale
= matroska
->time_scale
;
1436 for (i
=0; i
<index_list
->nb_elem
; i
++) {
1437 EbmlList
*pos_list
= &index
[i
].pos
;
1438 MatroskaIndexPos
*pos
= pos_list
->elem
;
1439 for (j
=0; j
<pos_list
->nb_elem
; j
++) {
1440 MatroskaTrack
*track
= matroska_find_track_by_num(matroska
,
1442 if (track
&& track
->stream
)
1443 av_add_index_entry(track
->stream
,
1444 pos
[j
].pos
+ matroska
->segment_start
,
1445 index
[i
].time
/index_scale
, 0, 0,
1450 matroska_convert_tags(s
);
1456 * Put one packet in an application-supplied AVPacket struct.
1457 * Returns 0 on success or -1 on failure.
1459 static int matroska_deliver_packet(MatroskaDemuxContext
*matroska
,
1462 if (matroska
->num_packets
> 0) {
1463 memcpy(pkt
, matroska
->packets
[0], sizeof(AVPacket
));
1464 av_free(matroska
->packets
[0]);
1465 if (matroska
->num_packets
> 1) {
1466 memmove(&matroska
->packets
[0], &matroska
->packets
[1],
1467 (matroska
->num_packets
- 1) * sizeof(AVPacket
*));
1469 av_realloc(matroska
->packets
, (matroska
->num_packets
- 1) *
1470 sizeof(AVPacket
*));
1472 av_freep(&matroska
->packets
);
1474 matroska
->num_packets
--;
1482 * Free all packets in our internal queue.
1484 static void matroska_clear_queue(MatroskaDemuxContext
*matroska
)
1486 if (matroska
->packets
) {
1488 for (n
= 0; n
< matroska
->num_packets
; n
++) {
1489 av_free_packet(matroska
->packets
[n
]);
1490 av_free(matroska
->packets
[n
]);
1492 av_freep(&matroska
->packets
);
1493 matroska
->num_packets
= 0;
1497 static int matroska_parse_block(MatroskaDemuxContext
*matroska
, uint8_t *data
,
1498 int size
, int64_t pos
, uint64_t cluster_time
,
1499 uint64_t duration
, int is_keyframe
,
1500 int64_t cluster_pos
)
1502 uint64_t timecode
= AV_NOPTS_VALUE
;
1503 MatroskaTrack
*track
;
1508 uint32_t *lace_size
= NULL
;
1509 int n
, flags
, laces
= 0;
1512 if ((n
= matroska_ebmlnum_uint(matroska
, data
, size
, &num
)) < 0) {
1513 av_log(matroska
->ctx
, AV_LOG_ERROR
, "EBML block data error\n");
1519 track
= matroska_find_track_by_num(matroska
, num
);
1520 if (size
<= 3 || !track
|| !track
->stream
) {
1521 av_log(matroska
->ctx
, AV_LOG_INFO
,
1522 "Invalid stream %"PRIu64
" or size %u\n", num
, size
);
1526 if (st
->discard
>= AVDISCARD_ALL
)
1528 if (duration
== AV_NOPTS_VALUE
)
1529 duration
= track
->default_duration
/ matroska
->time_scale
;
1531 block_time
= AV_RB16(data
);
1535 if (is_keyframe
== -1)
1536 is_keyframe
= flags
& 0x80 ? PKT_FLAG_KEY
: 0;
1538 if (cluster_time
!= (uint64_t)-1
1539 && (block_time
>= 0 || cluster_time
>= -block_time
)) {
1540 timecode
= cluster_time
+ block_time
;
1541 if (track
->type
== MATROSKA_TRACK_TYPE_SUBTITLE
1542 && timecode
< track
->end_timecode
)
1543 is_keyframe
= 0; /* overlapping subtitles are not key frame */
1545 av_add_index_entry(st
, cluster_pos
, timecode
, 0,0,AVINDEX_KEYFRAME
);
1546 track
->end_timecode
= FFMAX(track
->end_timecode
, timecode
+duration
);
1549 if (matroska
->skip_to_keyframe
&& track
->type
!= MATROSKA_TRACK_TYPE_SUBTITLE
) {
1550 if (!is_keyframe
|| timecode
< matroska
->skip_to_timecode
)
1552 matroska
->skip_to_keyframe
= 0;
1555 switch ((flags
& 0x06) >> 1) {
1556 case 0x0: /* no lacing */
1558 lace_size
= av_mallocz(sizeof(int));
1559 lace_size
[0] = size
;
1562 case 0x1: /* Xiph lacing */
1563 case 0x2: /* fixed-size lacing */
1564 case 0x3: /* EBML lacing */
1565 assert(size
>0); // size <=3 is checked before size-=3 above
1566 laces
= (*data
) + 1;
1569 lace_size
= av_mallocz(laces
* sizeof(int));
1571 switch ((flags
& 0x06) >> 1) {
1572 case 0x1: /* Xiph lacing */ {
1575 for (n
= 0; res
== 0 && n
< laces
- 1; n
++) {
1582 lace_size
[n
] += temp
;
1588 total
+= lace_size
[n
];
1590 lace_size
[n
] = size
- total
;
1594 case 0x2: /* fixed-size lacing */
1595 for (n
= 0; n
< laces
; n
++)
1596 lace_size
[n
] = size
/ laces
;
1599 case 0x3: /* EBML lacing */ {
1601 n
= matroska_ebmlnum_uint(matroska
, data
, size
, &num
);
1603 av_log(matroska
->ctx
, AV_LOG_INFO
,
1604 "EBML block data error\n");
1609 total
= lace_size
[0] = num
;
1610 for (n
= 1; res
== 0 && n
< laces
- 1; n
++) {
1613 r
= matroska_ebmlnum_sint(matroska
, data
, size
, &snum
);
1615 av_log(matroska
->ctx
, AV_LOG_INFO
,
1616 "EBML block data error\n");
1621 lace_size
[n
] = lace_size
[n
- 1] + snum
;
1622 total
+= lace_size
[n
];
1624 lace_size
[n
] = size
- total
;
1632 for (n
= 0; n
< laces
; n
++) {
1633 if (st
->codec
->codec_id
== CODEC_ID_RA_288
||
1634 st
->codec
->codec_id
== CODEC_ID_COOK
||
1635 st
->codec
->codec_id
== CODEC_ID_ATRAC3
) {
1636 int a
= st
->codec
->block_align
;
1637 int sps
= track
->audio
.sub_packet_size
;
1638 int cfs
= track
->audio
.coded_framesize
;
1639 int h
= track
->audio
.sub_packet_h
;
1640 int y
= track
->audio
.sub_packet_cnt
;
1641 int w
= track
->audio
.frame_size
;
1644 if (!track
->audio
.pkt_cnt
) {
1645 if (st
->codec
->codec_id
== CODEC_ID_RA_288
)
1646 for (x
=0; x
<h
/2; x
++)
1647 memcpy(track
->audio
.buf
+x
*2*w
+y
*cfs
,
1650 for (x
=0; x
<w
/sps
; x
++)
1651 memcpy(track
->audio
.buf
+sps
*(h
*x
+((h
+1)/2)*(y
&1)+(y
>>1)), data
+x
*sps
, sps
);
1653 if (++track
->audio
.sub_packet_cnt
>= h
) {
1654 track
->audio
.sub_packet_cnt
= 0;
1655 track
->audio
.pkt_cnt
= h
*w
/ a
;
1658 while (track
->audio
.pkt_cnt
) {
1659 pkt
= av_mallocz(sizeof(AVPacket
));
1660 av_new_packet(pkt
, a
);
1661 memcpy(pkt
->data
, track
->audio
.buf
1662 + a
* (h
*w
/ a
- track
->audio
.pkt_cnt
--), a
);
1664 pkt
->stream_index
= st
->index
;
1665 dynarray_add(&matroska
->packets
,&matroska
->num_packets
,pkt
);
1668 MatroskaTrackEncoding
*encodings
= track
->encodings
.elem
;
1669 int offset
= 0, pkt_size
= lace_size
[n
];
1670 uint8_t *pkt_data
= data
;
1672 if (encodings
&& encodings
->scope
& 1) {
1673 offset
= matroska_decode_buffer(&pkt_data
,&pkt_size
, track
);
1678 pkt
= av_mallocz(sizeof(AVPacket
));
1679 /* XXX: prevent data copy... */
1680 if (av_new_packet(pkt
, pkt_size
+offset
) < 0) {
1682 res
= AVERROR(ENOMEM
);
1686 memcpy (pkt
->data
, encodings
->compression
.settings
.data
, offset
);
1687 memcpy (pkt
->data
+offset
, pkt_data
, pkt_size
);
1689 if (pkt_data
!= data
)
1693 pkt
->flags
= is_keyframe
;
1694 pkt
->stream_index
= st
->index
;
1696 pkt
->pts
= timecode
;
1698 if (st
->codec
->codec_id
== CODEC_ID_TEXT
)
1699 pkt
->convergence_duration
= duration
;
1700 else if (track
->type
!= MATROSKA_TRACK_TYPE_SUBTITLE
)
1701 pkt
->duration
= duration
;
1703 if (st
->codec
->codec_id
== CODEC_ID_SSA
)
1704 matroska_fix_ass_packet(matroska
, pkt
, duration
);
1706 if (matroska
->prev_pkt
&&
1707 timecode
!= AV_NOPTS_VALUE
&&
1708 matroska
->prev_pkt
->pts
== timecode
&&
1709 matroska
->prev_pkt
->stream_index
== st
->index
)
1710 matroska_merge_packets(matroska
->prev_pkt
, pkt
);
1712 dynarray_add(&matroska
->packets
,&matroska
->num_packets
,pkt
);
1713 matroska
->prev_pkt
= pkt
;
1717 if (timecode
!= AV_NOPTS_VALUE
)
1718 timecode
= duration
? timecode
+ duration
: AV_NOPTS_VALUE
;
1719 data
+= lace_size
[n
];
1727 static int matroska_parse_cluster(MatroskaDemuxContext
*matroska
)
1729 MatroskaCluster cluster
= { 0 };
1730 EbmlList
*blocks_list
;
1731 MatroskaBlock
*blocks
;
1733 int64_t pos
= url_ftell(matroska
->ctx
->pb
);
1734 matroska
->prev_pkt
= NULL
;
1735 if (matroska
->has_cluster_id
){
1736 /* For the first cluster we parse, its ID was already read as
1737 part of matroska_read_header(), so don't read it again */
1738 res
= ebml_parse_id(matroska
, matroska_clusters
,
1739 MATROSKA_ID_CLUSTER
, &cluster
);
1740 pos
-= 4; /* sizeof the ID which was already read */
1741 matroska
->has_cluster_id
= 0;
1743 res
= ebml_parse(matroska
, matroska_clusters
, &cluster
);
1744 blocks_list
= &cluster
.blocks
;
1745 blocks
= blocks_list
->elem
;
1746 for (i
=0; i
<blocks_list
->nb_elem
; i
++)
1747 if (blocks
[i
].bin
.size
> 0)
1748 res
=matroska_parse_block(matroska
,
1749 blocks
[i
].bin
.data
, blocks
[i
].bin
.size
,
1750 blocks
[i
].bin
.pos
, cluster
.timecode
,
1751 blocks
[i
].duration
, !blocks
[i
].reference
,
1753 ebml_free(matroska_cluster
, &cluster
);
1754 if (res
< 0) matroska
->done
= 1;
1758 static int matroska_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
1760 MatroskaDemuxContext
*matroska
= s
->priv_data
;
1762 while (matroska_deliver_packet(matroska
, pkt
)) {
1765 matroska_parse_cluster(matroska
);
1771 static int matroska_read_seek(AVFormatContext
*s
, int stream_index
,
1772 int64_t timestamp
, int flags
)
1774 MatroskaDemuxContext
*matroska
= s
->priv_data
;
1775 MatroskaTrack
*tracks
= matroska
->tracks
.elem
;
1776 AVStream
*st
= s
->streams
[stream_index
];
1777 int i
, index
, index_sub
, index_min
;
1779 if (!st
->nb_index_entries
)
1781 timestamp
= FFMAX(timestamp
, st
->index_entries
[0].timestamp
);
1783 if ((index
= av_index_search_timestamp(st
, timestamp
, flags
)) < 0) {
1784 url_fseek(s
->pb
, st
->index_entries
[st
->nb_index_entries
-1].pos
, SEEK_SET
);
1785 while ((index
= av_index_search_timestamp(st
, timestamp
, flags
)) < 0) {
1786 matroska_clear_queue(matroska
);
1787 if (matroska_parse_cluster(matroska
) < 0)
1792 matroska_clear_queue(matroska
);
1797 for (i
=0; i
< matroska
->tracks
.nb_elem
; i
++) {
1798 tracks
[i
].end_timecode
= 0;
1799 if (tracks
[i
].type
== MATROSKA_TRACK_TYPE_SUBTITLE
1800 && !tracks
[i
].stream
->discard
!= AVDISCARD_ALL
) {
1801 index_sub
= av_index_search_timestamp(tracks
[i
].stream
, st
->index_entries
[index
].timestamp
, AVSEEK_FLAG_BACKWARD
);
1803 && st
->index_entries
[index_sub
].pos
< st
->index_entries
[index_min
].pos
1804 && st
->index_entries
[index
].timestamp
- st
->index_entries
[index_sub
].timestamp
< 30000000000/matroska
->time_scale
)
1805 index_min
= index_sub
;
1809 url_fseek(s
->pb
, st
->index_entries
[index_min
].pos
, SEEK_SET
);
1810 matroska
->skip_to_keyframe
= !(flags
& AVSEEK_FLAG_ANY
);
1811 matroska
->skip_to_timecode
= st
->index_entries
[index
].timestamp
;
1813 av_update_cur_dts(s
, st
, st
->index_entries
[index
].timestamp
);
1817 static int matroska_read_close(AVFormatContext
*s
)
1819 MatroskaDemuxContext
*matroska
= s
->priv_data
;
1820 MatroskaTrack
*tracks
= matroska
->tracks
.elem
;
1823 matroska_clear_queue(matroska
);
1825 for (n
=0; n
< matroska
->tracks
.nb_elem
; n
++)
1826 if (tracks
[n
].type
== MATROSKA_TRACK_TYPE_AUDIO
)
1827 av_free(tracks
[n
].audio
.buf
);
1828 ebml_free(matroska_segment
, matroska
);
1833 AVInputFormat matroska_demuxer
= {
1835 NULL_IF_CONFIG_SMALL("Matroska file format"),
1836 sizeof(MatroskaDemuxContext
),
1838 matroska_read_header
,
1839 matroska_read_packet
,
1840 matroska_read_close
,
1842 .metadata_conv
= ff_mkv_metadata_conv
,