2 * Matroska file demuxer (no muxer yet)
3 * Copyright (c) 2003-2004 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
24 * Matroska file demuxer
25 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
26 * with a little help from Moritz Bunkus <moritz@bunkus.org>
27 * Specs available on the matroska project page:
28 * http://www.matroska.org/.
32 /* For codec_get_id(). */
35 #include "libavcodec/mpeg4audio.h"
36 #include "libavutil/intfloat_readwrite.h"
37 #include "libavutil/lzo.h"
45 typedef struct Track
{
46 MatroskaTrackType type
;
48 /* Unique track number and track ID. stream_index is the index that
49 * the calling app uses for this track. */
60 unsigned char *codec_priv
;
63 uint64_t default_duration
;
64 MatroskaTrackFlags flags
;
67 MatroskaTrackEncodingCompAlgo encoding_algo
;
68 uint8_t *encoding_settings
;
69 int encoding_settings_len
;
72 typedef struct MatroskaVideoTrack
{
82 MatroskaAspectRatioMode ar_mode
;
83 MatroskaEyeMode eye_mode
;
88 typedef struct MatroskaAudioTrack
{
93 int internal_samplerate
;
97 /* real audio header */
106 } MatroskaAudioTrack
;
108 typedef struct MatroskaSubtitleTrack
{
111 } MatroskaSubtitleTrack
;
113 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
114 sizeof(MatroskaAudioTrack), \
115 sizeof(MatroskaSubtitleTrack)))
117 typedef struct MatroskaLevel
{
122 typedef struct MatroskaDemuxIndex
{
123 uint64_t pos
; /* of the corresponding *cluster*! */
124 uint16_t track
; /* reference to 'num' */
125 uint64_t time
; /* in nanoseconds */
126 } MatroskaDemuxIndex
;
128 typedef struct MatroskaDemuxContext
{
129 AVFormatContext
*ctx
;
133 MatroskaLevel levels
[EBML_MAX_DEPTH
];
141 /* timescale in the file */
144 /* num_streams is the number of streams that av_new_stream() was called
145 * for ( = that are available to the calling program). */
148 MatroskaTrack
*tracks
[MAX_STREAMS
];
150 /* cache for ID peeking */
153 /* byte position of the segment inside the stream */
154 offset_t segment_start
;
156 /* The packet queue. */
160 /* have we already parse metadata/cues/clusters? */
165 /* The index for seeking. */
167 MatroskaDemuxIndex
*index
;
169 /* What to skip before effectively reading a packet. */
170 int skip_to_keyframe
;
171 AVStream
*skip_to_stream
;
172 } MatroskaDemuxContext
;
175 * The first few functions handle EBML file parsing. The rest
176 * is the document interpretation. Matroska really just is a
181 * Return: the amount of levels in the hierarchy that the
182 * current element lies higher than the previous one.
183 * The opposite isn't done - that's auto-done using master
188 ebml_read_element_level_up (MatroskaDemuxContext
*matroska
)
190 ByteIOContext
*pb
= matroska
->ctx
->pb
;
191 offset_t pos
= url_ftell(pb
);
194 while (matroska
->num_levels
> 0) {
195 MatroskaLevel
*level
= &matroska
->levels
[matroska
->num_levels
- 1];
197 if (pos
>= level
->start
+ level
->length
) {
198 matroska
->num_levels
--;
209 * Read: an "EBML number", which is defined as a variable-length
210 * array of bytes. The first byte indicates the length by giving a
211 * number of 0-bits followed by a one. The position of the first
212 * "one" bit inside the first byte indicates the length of this
214 * Returns: num. of bytes read. < 0 on error.
218 ebml_read_num (MatroskaDemuxContext
*matroska
,
222 ByteIOContext
*pb
= matroska
->ctx
->pb
;
223 int len_mask
= 0x80, read
= 1, n
= 1;
226 /* the first byte tells us the length in bytes - get_byte() can normally
227 * return 0, but since that's not a valid first ebmlID byte, we can
228 * use it safely here to catch EOS. */
229 if (!(total
= get_byte(pb
))) {
230 /* we might encounter EOS here */
232 offset_t pos
= url_ftell(pb
);
233 av_log(matroska
->ctx
, AV_LOG_ERROR
,
234 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
237 return AVERROR(EIO
); /* EOS or actual I/O error */
240 /* get the length of the EBML number */
241 while (read
<= max_size
&& !(total
& len_mask
)) {
245 if (read
> max_size
) {
246 offset_t pos
= url_ftell(pb
) - 1;
247 av_log(matroska
->ctx
, AV_LOG_ERROR
,
248 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
249 (uint8_t) total
, pos
, pos
);
250 return AVERROR_INVALIDDATA
;
253 /* read out length */
256 total
= (total
<< 8) | get_byte(pb
);
264 * Read: the element content data ID.
265 * Return: the number of bytes read or < 0 on error.
269 ebml_read_element_id (MatroskaDemuxContext
*matroska
,
276 /* if we re-call this, use our cached ID */
277 if (matroska
->peek_id
!= 0) {
280 *id
= matroska
->peek_id
;
284 /* read out the "EBML number", include tag in ID */
285 if ((read
= ebml_read_num(matroska
, 4, &total
)) < 0)
287 *id
= matroska
->peek_id
= total
| (1 << (read
* 7));
291 *level_up
= ebml_read_element_level_up(matroska
);
297 * Read: element content length.
298 * Return: the number of bytes read or < 0 on error.
302 ebml_read_element_length (MatroskaDemuxContext
*matroska
,
305 /* clear cache since we're now beyond that data point */
306 matroska
->peek_id
= 0;
308 /* read out the "EBML number", include tag in ID */
309 return ebml_read_num(matroska
, 8, length
);
313 * Return: the ID of the next element, or 0 on error.
314 * Level_up contains the amount of levels that this
315 * next element lies higher than the previous one.
319 ebml_peek_id (MatroskaDemuxContext
*matroska
,
324 if (ebml_read_element_id(matroska
, &id
, level_up
) < 0)
331 * Seek to a given offset.
332 * 0 is success, -1 is failure.
336 ebml_read_seek (MatroskaDemuxContext
*matroska
,
339 ByteIOContext
*pb
= matroska
->ctx
->pb
;
341 /* clear ID cache, if any */
342 matroska
->peek_id
= 0;
344 return (url_fseek(pb
, offset
, SEEK_SET
) == offset
) ? 0 : -1;
348 * Skip the next element.
349 * 0 is success, -1 is failure.
353 ebml_read_skip (MatroskaDemuxContext
*matroska
)
355 ByteIOContext
*pb
= matroska
->ctx
->pb
;
360 if ((res
= ebml_read_element_id(matroska
, &id
, NULL
)) < 0 ||
361 (res
= ebml_read_element_length(matroska
, &length
)) < 0)
364 url_fskip(pb
, length
);
370 * Read the next element as an unsigned int.
371 * 0 is success, < 0 is failure.
375 ebml_read_uint (MatroskaDemuxContext
*matroska
,
379 ByteIOContext
*pb
= matroska
->ctx
->pb
;
380 int n
= 0, size
, res
;
383 if ((res
= ebml_read_element_id(matroska
, id
, NULL
)) < 0 ||
384 (res
= ebml_read_element_length(matroska
, &rlength
)) < 0)
387 if (size
< 1 || size
> 8) {
388 offset_t pos
= url_ftell(pb
);
389 av_log(matroska
->ctx
, AV_LOG_ERROR
,
390 "Invalid uint element size %d at position %"PRId64
" (0x%"PRIx64
")\n",
392 return AVERROR_INVALIDDATA
;
395 /* big-endian ordening; build up number */
398 *num
= (*num
<< 8) | get_byte(pb
);
404 * Read the next element as a signed int.
405 * 0 is success, < 0 is failure.
409 ebml_read_sint (MatroskaDemuxContext
*matroska
,
413 ByteIOContext
*pb
= matroska
->ctx
->pb
;
414 int size
, n
= 1, negative
= 0, res
;
417 if ((res
= ebml_read_element_id(matroska
, id
, NULL
)) < 0 ||
418 (res
= ebml_read_element_length(matroska
, &rlength
)) < 0)
421 if (size
< 1 || size
> 8) {
422 offset_t pos
= url_ftell(pb
);
423 av_log(matroska
->ctx
, AV_LOG_ERROR
,
424 "Invalid sint element size %d at position %"PRId64
" (0x%"PRIx64
")\n",
426 return AVERROR_INVALIDDATA
;
428 if ((*num
= get_byte(pb
)) & 0x80) {
433 *num
= (*num
<< 8) | get_byte(pb
);
437 *num
= *num
- (1LL << ((8 * size
) - 1));
443 * Read the next element as a float.
444 * 0 is success, < 0 is failure.
448 ebml_read_float (MatroskaDemuxContext
*matroska
,
452 ByteIOContext
*pb
= matroska
->ctx
->pb
;
456 if ((res
= ebml_read_element_id(matroska
, id
, NULL
)) < 0 ||
457 (res
= ebml_read_element_length(matroska
, &rlength
)) < 0)
462 *num
= av_int2flt(get_be32(pb
));
464 *num
= av_int2dbl(get_be64(pb
));
466 offset_t pos
= url_ftell(pb
);
467 av_log(matroska
->ctx
, AV_LOG_ERROR
,
468 "Invalid float element size %d at position %"PRIu64
" (0x%"PRIx64
")\n",
470 return AVERROR_INVALIDDATA
;
477 * Read the next element as an ASCII string.
478 * 0 is success, < 0 is failure.
482 ebml_read_ascii (MatroskaDemuxContext
*matroska
,
486 ByteIOContext
*pb
= matroska
->ctx
->pb
;
490 if ((res
= ebml_read_element_id(matroska
, id
, NULL
)) < 0 ||
491 (res
= ebml_read_element_length(matroska
, &rlength
)) < 0)
495 /* ebml strings are usually not 0-terminated, so we allocate one
496 * byte more, read the string and NULL-terminate it ourselves. */
497 if (size
< 0 || !(*str
= av_malloc(size
+ 1))) {
498 av_log(matroska
->ctx
, AV_LOG_ERROR
, "Memory allocation failed\n");
499 return AVERROR(ENOMEM
);
501 if (get_buffer(pb
, (uint8_t *) *str
, size
) != size
) {
502 offset_t pos
= url_ftell(pb
);
503 av_log(matroska
->ctx
, AV_LOG_ERROR
,
504 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n", pos
, pos
);
513 * Read the next element as a UTF-8 string.
514 * 0 is success, < 0 is failure.
518 ebml_read_utf8 (MatroskaDemuxContext
*matroska
,
522 return ebml_read_ascii(matroska
, id
, str
);
526 * Read the next element as a date (nanoseconds since 1/1/2000).
527 * 0 is success, < 0 is failure.
531 ebml_read_date (MatroskaDemuxContext
*matroska
,
535 return ebml_read_sint(matroska
, id
, date
);
539 * Read the next element, but only the header. The contents
540 * are supposed to be sub-elements which can be read separately.
541 * 0 is success, < 0 is failure.
545 ebml_read_master (MatroskaDemuxContext
*matroska
,
548 ByteIOContext
*pb
= matroska
->ctx
->pb
;
550 MatroskaLevel
*level
;
553 if ((res
= ebml_read_element_id(matroska
, id
, NULL
)) < 0 ||
554 (res
= ebml_read_element_length(matroska
, &length
)) < 0)
557 /* protect... (Heaven forbids that the '>' is true) */
558 if (matroska
->num_levels
>= EBML_MAX_DEPTH
) {
559 av_log(matroska
->ctx
, AV_LOG_ERROR
,
560 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH
);
561 return AVERROR(ENOSYS
);
565 level
= &matroska
->levels
[matroska
->num_levels
++];
566 level
->start
= url_ftell(pb
);
567 level
->length
= length
;
573 * Read the next element as binary data.
574 * 0 is success, < 0 is failure.
578 ebml_read_binary (MatroskaDemuxContext
*matroska
,
583 ByteIOContext
*pb
= matroska
->ctx
->pb
;
587 if ((res
= ebml_read_element_id(matroska
, id
, NULL
)) < 0 ||
588 (res
= ebml_read_element_length(matroska
, &rlength
)) < 0)
592 if (!(*binary
= av_malloc(*size
))) {
593 av_log(matroska
->ctx
, AV_LOG_ERROR
,
594 "Memory allocation error\n");
595 return AVERROR(ENOMEM
);
598 if (get_buffer(pb
, *binary
, *size
) != *size
) {
599 offset_t pos
= url_ftell(pb
);
600 av_log(matroska
->ctx
, AV_LOG_ERROR
,
601 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n", pos
, pos
);
609 * Read signed/unsigned "EBML" numbers.
610 * Return: number of bytes processed, < 0 on error.
611 * XXX: use ebml_read_num().
615 matroska_ebmlnum_uint (uint8_t *data
,
619 int len_mask
= 0x80, read
= 1, n
= 1, num_ffs
= 0;
623 return AVERROR_INVALIDDATA
;
626 while (read
<= 8 && !(total
& len_mask
)) {
631 return AVERROR_INVALIDDATA
;
633 if ((total
&= (len_mask
- 1)) == len_mask
- 1)
636 return AVERROR_INVALIDDATA
;
640 total
= (total
<< 8) | data
[n
];
653 * Same as above, but signed.
657 matroska_ebmlnum_sint (uint8_t *data
,
664 /* read as unsigned number first */
665 if ((res
= matroska_ebmlnum_uint(data
, size
, &unum
)) < 0)
668 /* make signed (weird way) */
669 if (unum
== (uint64_t)-1)
672 *num
= unum
- ((1LL << ((7 * res
) - 1)) - 1);
678 * Read an EBML header.
679 * 0 is success, < 0 is failure.
683 ebml_read_header (MatroskaDemuxContext
*matroska
,
688 int level_up
, res
= 0;
696 if (!(id
= ebml_peek_id(matroska
, &level_up
)) ||
697 level_up
!= 0 || id
!= EBML_ID_HEADER
) {
698 av_log(matroska
->ctx
, AV_LOG_ERROR
,
699 "This is not an EBML file (id=0x%x/0x%x)\n", id
, EBML_ID_HEADER
);
700 return AVERROR_INVALIDDATA
;
702 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
706 if (!(id
= ebml_peek_id(matroska
, &level_up
)))
714 /* is our read version uptodate? */
715 case EBML_ID_EBMLREADVERSION
: {
718 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
720 if (num
> EBML_VERSION
) {
721 av_log(matroska
->ctx
, AV_LOG_ERROR
,
722 "EBML version %"PRIu64
" (> %d) is not supported\n",
724 return AVERROR_INVALIDDATA
;
729 /* we only handle 8 byte lengths at max */
730 case EBML_ID_EBMLMAXSIZELENGTH
: {
733 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
735 if (num
> sizeof(uint64_t)) {
736 av_log(matroska
->ctx
, AV_LOG_ERROR
,
737 "Integers of size %"PRIu64
" (> %zd) not supported\n",
738 num
, sizeof(uint64_t));
739 return AVERROR_INVALIDDATA
;
744 /* we handle 4 byte IDs at max */
745 case EBML_ID_EBMLMAXIDLENGTH
: {
748 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
750 if (num
> sizeof(uint32_t)) {
751 av_log(matroska
->ctx
, AV_LOG_ERROR
,
752 "IDs of size %"PRIu64
" (> %zu) not supported\n",
753 num
, sizeof(uint32_t));
754 return AVERROR_INVALIDDATA
;
759 case EBML_ID_DOCTYPE
: {
762 if ((res
= ebml_read_ascii(matroska
, &id
, &text
)) < 0)
773 case EBML_ID_DOCTYPEREADVERSION
: {
776 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
784 av_log(matroska
->ctx
, AV_LOG_INFO
,
785 "Unknown data type 0x%x in EBML header", id
);
789 /* we ignore these two, as they don't tell us anything we
791 case EBML_ID_EBMLVERSION
:
792 case EBML_ID_DOCTYPEVERSION
:
793 res
= ebml_read_skip (matroska
);
803 matroska_find_track_by_num (MatroskaDemuxContext
*matroska
,
808 for (i
= 0; i
< matroska
->num_tracks
; i
++)
809 if (matroska
->tracks
[i
]->num
== num
)
817 * Put one packet in an application-supplied AVPacket struct.
818 * Returns 0 on success or -1 on failure.
822 matroska_deliver_packet (MatroskaDemuxContext
*matroska
,
825 if (matroska
->num_packets
> 0) {
826 memcpy(pkt
, matroska
->packets
[0], sizeof(AVPacket
));
827 av_free(matroska
->packets
[0]);
828 if (matroska
->num_packets
> 1) {
829 memmove(&matroska
->packets
[0], &matroska
->packets
[1],
830 (matroska
->num_packets
- 1) * sizeof(AVPacket
*));
832 av_realloc(matroska
->packets
, (matroska
->num_packets
- 1) *
835 av_freep(&matroska
->packets
);
837 matroska
->num_packets
--;
845 * Put a packet into our internal queue. Will be delivered to the
846 * user/application during the next get_packet() call.
850 matroska_queue_packet (MatroskaDemuxContext
*matroska
,
854 av_realloc(matroska
->packets
, (matroska
->num_packets
+ 1) *
856 matroska
->packets
[matroska
->num_packets
] = pkt
;
857 matroska
->num_packets
++;
861 * Free all packets in our internal queue.
864 matroska_clear_queue (MatroskaDemuxContext
*matroska
)
866 if (matroska
->packets
) {
868 for (n
= 0; n
< matroska
->num_packets
; n
++) {
869 av_free_packet(matroska
->packets
[n
]);
870 av_free(matroska
->packets
[n
]);
872 av_free(matroska
->packets
);
873 matroska
->packets
= NULL
;
874 matroska
->num_packets
= 0;
884 matroska_probe (AVProbeData
*p
)
887 int len_mask
= 0x80, size
= 1, n
= 1;
888 uint8_t probe_data
[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
891 if (AV_RB32(p
->buf
) != EBML_ID_HEADER
)
894 /* length of header */
896 while (size
<= 8 && !(total
& len_mask
)) {
902 total
&= (len_mask
- 1);
904 total
= (total
<< 8) | p
->buf
[4 + n
++];
906 /* does the probe data contain the whole header? */
907 if (p
->buf_size
< 4 + size
+ total
)
910 /* the header must contain the document type 'matroska'. For now,
911 * we don't parse the whole header but simply check for the
912 * availability of that array of characters inside the header.
913 * Not fully fool-proof, but good enough. */
914 for (n
= 4 + size
; n
<= 4 + size
+ total
- sizeof(probe_data
); n
++)
915 if (!memcmp (&p
->buf
[n
], probe_data
, sizeof(probe_data
)))
916 return AVPROBE_SCORE_MAX
;
922 * From here on, it's all XML-style DTD stuff... Needs no comments.
926 matroska_parse_info (MatroskaDemuxContext
*matroska
)
931 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "Parsing info...\n");
934 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
937 } else if (matroska
->level_up
) {
938 matroska
->level_up
--;
943 /* cluster timecode */
944 case MATROSKA_ID_TIMECODESCALE
: {
946 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
948 matroska
->time_scale
= num
;
952 case MATROSKA_ID_DURATION
: {
954 if ((res
= ebml_read_float(matroska
, &id
, &num
)) < 0)
956 matroska
->ctx
->duration
= num
* matroska
->time_scale
* 1000 / AV_TIME_BASE
;
960 case MATROSKA_ID_TITLE
: {
962 if ((res
= ebml_read_utf8(matroska
, &id
, &text
)) < 0)
964 strncpy(matroska
->ctx
->title
, text
,
965 sizeof(matroska
->ctx
->title
)-1);
970 case MATROSKA_ID_WRITINGAPP
: {
972 if ((res
= ebml_read_utf8(matroska
, &id
, &text
)) < 0)
974 matroska
->writing_app
= text
;
978 case MATROSKA_ID_MUXINGAPP
: {
980 if ((res
= ebml_read_utf8(matroska
, &id
, &text
)) < 0)
982 matroska
->muxing_app
= text
;
986 case MATROSKA_ID_DATEUTC
: {
988 if ((res
= ebml_read_date(matroska
, &id
, &time
)) < 0)
990 matroska
->created
= time
;
995 av_log(matroska
->ctx
, AV_LOG_INFO
,
996 "Unknown entry 0x%x in info header\n", id
);
1000 res
= ebml_read_skip(matroska
);
1004 if (matroska
->level_up
) {
1005 matroska
->level_up
--;
1014 matroska_add_stream (MatroskaDemuxContext
*matroska
)
1018 MatroskaTrack
*track
;
1020 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "parsing track, adding stream..,\n");
1022 /* Allocate a generic track. As soon as we know its type we'll realloc. */
1023 track
= av_mallocz(MAX_TRACK_SIZE
);
1024 matroska
->num_tracks
++;
1025 strcpy(track
->language
, "eng");
1027 /* start with the master */
1028 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1031 /* try reading the trackentry headers */
1033 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1036 } else if (matroska
->level_up
> 0) {
1037 matroska
->level_up
--;
1042 /* track number (unique stream ID) */
1043 case MATROSKA_ID_TRACKNUMBER
: {
1045 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1051 /* track UID (unique identifier) */
1052 case MATROSKA_ID_TRACKUID
: {
1054 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1060 /* track type (video, audio, combined, subtitle, etc.) */
1061 case MATROSKA_ID_TRACKTYPE
: {
1063 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1065 if (track
->type
&& track
->type
!= num
) {
1066 av_log(matroska
->ctx
, AV_LOG_INFO
,
1067 "More than one tracktype in an entry - skip\n");
1072 switch (track
->type
) {
1073 case MATROSKA_TRACK_TYPE_VIDEO
:
1074 case MATROSKA_TRACK_TYPE_AUDIO
:
1075 case MATROSKA_TRACK_TYPE_SUBTITLE
:
1077 case MATROSKA_TRACK_TYPE_COMPLEX
:
1078 case MATROSKA_TRACK_TYPE_LOGO
:
1079 case MATROSKA_TRACK_TYPE_CONTROL
:
1081 av_log(matroska
->ctx
, AV_LOG_INFO
,
1082 "Unknown or unsupported track type 0x%x\n",
1084 track
->type
= MATROSKA_TRACK_TYPE_NONE
;
1087 matroska
->tracks
[matroska
->num_tracks
- 1] = track
;
1091 /* tracktype specific stuff for video */
1092 case MATROSKA_ID_TRACKVIDEO
: {
1093 MatroskaVideoTrack
*videotrack
;
1095 track
->type
= MATROSKA_TRACK_TYPE_VIDEO
;
1096 if (track
->type
!= MATROSKA_TRACK_TYPE_VIDEO
) {
1097 av_log(matroska
->ctx
, AV_LOG_INFO
,
1098 "video data in non-video track - ignoring\n");
1099 res
= AVERROR_INVALIDDATA
;
1101 } else if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1103 videotrack
= (MatroskaVideoTrack
*)track
;
1106 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1109 } else if (matroska
->level_up
> 0) {
1110 matroska
->level_up
--;
1115 /* fixme, this should be one-up, but I get it here */
1116 case MATROSKA_ID_TRACKDEFAULTDURATION
: {
1118 if ((res
= ebml_read_uint (matroska
, &id
,
1121 track
->default_duration
= num
;
1125 /* video framerate */
1126 case MATROSKA_ID_VIDEOFRAMERATE
: {
1128 if ((res
= ebml_read_float(matroska
, &id
,
1131 if (!track
->default_duration
)
1132 track
->default_duration
= 1000000000/num
;
1136 /* width of the size to display the video at */
1137 case MATROSKA_ID_VIDEODISPLAYWIDTH
: {
1139 if ((res
= ebml_read_uint(matroska
, &id
,
1142 videotrack
->display_width
= num
;
1146 /* height of the size to display the video at */
1147 case MATROSKA_ID_VIDEODISPLAYHEIGHT
: {
1149 if ((res
= ebml_read_uint(matroska
, &id
,
1152 videotrack
->display_height
= num
;
1156 /* width of the video in the file */
1157 case MATROSKA_ID_VIDEOPIXELWIDTH
: {
1159 if ((res
= ebml_read_uint(matroska
, &id
,
1162 videotrack
->pixel_width
= num
;
1166 /* height of the video in the file */
1167 case MATROSKA_ID_VIDEOPIXELHEIGHT
: {
1169 if ((res
= ebml_read_uint(matroska
, &id
,
1172 videotrack
->pixel_height
= num
;
1176 /* whether the video is interlaced */
1177 case MATROSKA_ID_VIDEOFLAGINTERLACED
: {
1179 if ((res
= ebml_read_uint(matroska
, &id
,
1184 MATROSKA_VIDEOTRACK_INTERLACED
;
1187 ~MATROSKA_VIDEOTRACK_INTERLACED
;
1191 /* stereo mode (whether the video has two streams,
1192 * where one is for the left eye and the other for
1193 * the right eye, which creates a 3D-like
1195 case MATROSKA_ID_VIDEOSTEREOMODE
: {
1197 if ((res
= ebml_read_uint(matroska
, &id
,
1200 if (num
!= MATROSKA_EYE_MODE_MONO
&&
1201 num
!= MATROSKA_EYE_MODE_LEFT
&&
1202 num
!= MATROSKA_EYE_MODE_RIGHT
&&
1203 num
!= MATROSKA_EYE_MODE_BOTH
) {
1204 av_log(matroska
->ctx
, AV_LOG_INFO
,
1205 "Ignoring unknown eye mode 0x%x\n",
1209 videotrack
->eye_mode
= num
;
1213 /* aspect ratio behaviour */
1214 case MATROSKA_ID_VIDEOASPECTRATIO
: {
1216 if ((res
= ebml_read_uint(matroska
, &id
,
1219 if (num
!= MATROSKA_ASPECT_RATIO_MODE_FREE
&&
1220 num
!= MATROSKA_ASPECT_RATIO_MODE_KEEP
&&
1221 num
!= MATROSKA_ASPECT_RATIO_MODE_FIXED
) {
1222 av_log(matroska
->ctx
, AV_LOG_INFO
,
1223 "Ignoring unknown aspect ratio 0x%x\n",
1227 videotrack
->ar_mode
= num
;
1231 /* colorspace (only matters for raw video)
1233 case MATROSKA_ID_VIDEOCOLORSPACE
: {
1235 if ((res
= ebml_read_uint(matroska
, &id
,
1238 videotrack
->fourcc
= num
;
1243 av_log(matroska
->ctx
, AV_LOG_INFO
,
1244 "Unknown video track header entry "
1245 "0x%x - ignoring\n", id
);
1249 res
= ebml_read_skip(matroska
);
1253 if (matroska
->level_up
) {
1254 matroska
->level_up
--;
1261 /* tracktype specific stuff for audio */
1262 case MATROSKA_ID_TRACKAUDIO
: {
1263 MatroskaAudioTrack
*audiotrack
;
1265 track
->type
= MATROSKA_TRACK_TYPE_AUDIO
;
1266 if (track
->type
!= MATROSKA_TRACK_TYPE_AUDIO
) {
1267 av_log(matroska
->ctx
, AV_LOG_INFO
,
1268 "audio data in non-audio track - ignoring\n");
1269 res
= AVERROR_INVALIDDATA
;
1271 } else if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1273 audiotrack
= (MatroskaAudioTrack
*)track
;
1274 audiotrack
->channels
= 1;
1275 audiotrack
->samplerate
= 8000;
1278 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1281 } else if (matroska
->level_up
> 0) {
1282 matroska
->level_up
--;
1288 case MATROSKA_ID_AUDIOSAMPLINGFREQ
: {
1290 if ((res
= ebml_read_float(matroska
, &id
,
1293 audiotrack
->internal_samplerate
=
1294 audiotrack
->samplerate
= num
;
1298 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
: {
1300 if ((res
= ebml_read_float(matroska
, &id
,
1303 audiotrack
->samplerate
= num
;
1308 case MATROSKA_ID_AUDIOBITDEPTH
: {
1310 if ((res
= ebml_read_uint(matroska
, &id
,
1313 audiotrack
->bitdepth
= num
;
1318 case MATROSKA_ID_AUDIOCHANNELS
: {
1320 if ((res
= ebml_read_uint(matroska
, &id
,
1323 audiotrack
->channels
= num
;
1328 av_log(matroska
->ctx
, AV_LOG_INFO
,
1329 "Unknown audio track header entry "
1330 "0x%x - ignoring\n", id
);
1334 res
= ebml_read_skip(matroska
);
1338 if (matroska
->level_up
) {
1339 matroska
->level_up
--;
1346 /* codec identifier */
1347 case MATROSKA_ID_CODECID
: {
1349 if ((res
= ebml_read_ascii(matroska
, &id
, &text
)) < 0)
1351 track
->codec_id
= text
;
1355 /* codec private data */
1356 case MATROSKA_ID_CODECPRIVATE
: {
1359 if ((res
= ebml_read_binary(matroska
, &id
, &data
, &size
) < 0))
1361 track
->codec_priv
= data
;
1362 track
->codec_priv_size
= size
;
1366 /* name of the codec */
1367 case MATROSKA_ID_CODECNAME
: {
1369 if ((res
= ebml_read_utf8(matroska
, &id
, &text
)) < 0)
1371 track
->codec_name
= text
;
1375 /* name of this track */
1376 case MATROSKA_ID_TRACKNAME
: {
1378 if ((res
= ebml_read_utf8(matroska
, &id
, &text
)) < 0)
1384 /* language (matters for audio/subtitles, mostly) */
1385 case MATROSKA_ID_TRACKLANGUAGE
: {
1387 if ((res
= ebml_read_utf8(matroska
, &id
, &text
)) < 0)
1389 if ((end
= strchr(text
, '-')))
1391 if (strlen(text
) == 3)
1392 strcpy(track
->language
, text
);
1397 /* whether this is actually used */
1398 case MATROSKA_ID_TRACKFLAGENABLED
: {
1400 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1403 track
->flags
|= MATROSKA_TRACK_ENABLED
;
1405 track
->flags
&= ~MATROSKA_TRACK_ENABLED
;
1409 /* whether it's the default for this track type */
1410 case MATROSKA_ID_TRACKFLAGDEFAULT
: {
1412 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1415 track
->flags
|= MATROSKA_TRACK_DEFAULT
;
1417 track
->flags
&= ~MATROSKA_TRACK_DEFAULT
;
1421 /* lacing (like MPEG, where blocks don't end/start on frame
1423 case MATROSKA_ID_TRACKFLAGLACING
: {
1425 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1428 track
->flags
|= MATROSKA_TRACK_LACING
;
1430 track
->flags
&= ~MATROSKA_TRACK_LACING
;
1434 /* default length (in time) of one data block in this track */
1435 case MATROSKA_ID_TRACKDEFAULTDURATION
: {
1437 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1439 track
->default_duration
= num
;
1443 case MATROSKA_ID_TRACKCONTENTENCODINGS
: {
1444 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1448 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1451 } else if (matroska
->level_up
> 0) {
1452 matroska
->level_up
--;
1457 case MATROSKA_ID_TRACKCONTENTENCODING
: {
1458 int encoding_scope
= 1;
1459 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1463 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1466 } else if (matroska
->level_up
> 0) {
1467 matroska
->level_up
--;
1472 case MATROSKA_ID_ENCODINGSCOPE
: {
1474 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1476 encoding_scope
= num
;
1480 case MATROSKA_ID_ENCODINGTYPE
: {
1482 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1485 av_log(matroska
->ctx
, AV_LOG_ERROR
,
1486 "Unsupported encoding type");
1490 case MATROSKA_ID_ENCODINGCOMPRESSION
: {
1491 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1495 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1498 } else if (matroska
->level_up
> 0) {
1499 matroska
->level_up
--;
1504 case MATROSKA_ID_ENCODINGCOMPALGO
: {
1506 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
1508 if (num
!= MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP
&&
1510 num
!= MATROSKA_TRACK_ENCODING_COMP_ZLIB
&&
1513 num
!= MATROSKA_TRACK_ENCODING_COMP_BZLIB
&&
1515 num
!= MATROSKA_TRACK_ENCODING_COMP_LZO
)
1516 av_log(matroska
->ctx
, AV_LOG_ERROR
,
1517 "Unsupported compression algo\n");
1518 track
->encoding_algo
= num
;
1522 case MATROSKA_ID_ENCODINGCOMPSETTINGS
: {
1525 if ((res
= ebml_read_binary(matroska
, &id
, &data
, &size
) < 0))
1527 track
->encoding_settings
= data
;
1528 track
->encoding_settings_len
= size
;
1533 av_log(matroska
->ctx
, AV_LOG_INFO
,
1534 "Unknown compression header entry "
1535 "0x%x - ignoring\n", id
);
1539 res
= ebml_read_skip(matroska
);
1543 if (matroska
->level_up
) {
1544 matroska
->level_up
--;
1552 av_log(matroska
->ctx
, AV_LOG_INFO
,
1553 "Unknown content encoding header entry "
1554 "0x%x - ignoring\n", id
);
1558 res
= ebml_read_skip(matroska
);
1562 if (matroska
->level_up
) {
1563 matroska
->level_up
--;
1568 track
->encoding_scope
= encoding_scope
;
1573 av_log(matroska
->ctx
, AV_LOG_INFO
,
1574 "Unknown content encodings header entry "
1575 "0x%x - ignoring\n", id
);
1579 res
= ebml_read_skip(matroska
);
1583 if (matroska
->level_up
) {
1584 matroska
->level_up
--;
1592 av_log(matroska
->ctx
, AV_LOG_INFO
,
1593 "Unknown track header entry 0x%x - ignoring\n", id
);
1597 /* we ignore these because they're nothing useful. */
1598 case MATROSKA_ID_CODECINFOURL
:
1599 case MATROSKA_ID_CODECDOWNLOADURL
:
1600 case MATROSKA_ID_TRACKMINCACHE
:
1601 case MATROSKA_ID_TRACKMAXCACHE
:
1602 res
= ebml_read_skip(matroska
);
1606 if (matroska
->level_up
) {
1607 matroska
->level_up
--;
1616 matroska_parse_tracks (MatroskaDemuxContext
*matroska
)
1621 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "parsing tracks...\n");
1624 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1627 } else if (matroska
->level_up
) {
1628 matroska
->level_up
--;
1633 /* one track within the "all-tracks" header */
1634 case MATROSKA_ID_TRACKENTRY
:
1635 res
= matroska_add_stream(matroska
);
1639 av_log(matroska
->ctx
, AV_LOG_INFO
,
1640 "Unknown entry 0x%x in track header\n", id
);
1644 res
= ebml_read_skip(matroska
);
1648 if (matroska
->level_up
) {
1649 matroska
->level_up
--;
1658 matroska_parse_index (MatroskaDemuxContext
*matroska
)
1662 MatroskaDemuxIndex idx
;
1664 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "parsing index...\n");
1667 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1670 } else if (matroska
->level_up
) {
1671 matroska
->level_up
--;
1676 /* one single index entry ('point') */
1677 case MATROSKA_ID_POINTENTRY
:
1678 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1681 /* in the end, we hope to fill one entry with a
1682 * timestamp, a file position and a tracknum */
1683 idx
.pos
= (uint64_t) -1;
1684 idx
.time
= (uint64_t) -1;
1685 idx
.track
= (uint16_t) -1;
1688 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1691 } else if (matroska
->level_up
) {
1692 matroska
->level_up
--;
1697 /* one single index entry ('point') */
1698 case MATROSKA_ID_CUETIME
: {
1700 if ((res
= ebml_read_uint(matroska
, &id
,
1703 idx
.time
= time
* matroska
->time_scale
;
1707 /* position in the file + track to which it
1709 case MATROSKA_ID_CUETRACKPOSITION
:
1710 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1714 if (!(id
= ebml_peek_id (matroska
,
1715 &matroska
->level_up
))) {
1718 } else if (matroska
->level_up
) {
1719 matroska
->level_up
--;
1725 case MATROSKA_ID_CUETRACK
: {
1727 if ((res
= ebml_read_uint(matroska
,
1734 /* position in file */
1735 case MATROSKA_ID_CUECLUSTERPOSITION
: {
1737 if ((res
= ebml_read_uint(matroska
,
1740 idx
.pos
= num
+matroska
->segment_start
;
1745 av_log(matroska
->ctx
, AV_LOG_INFO
,
1746 "Unknown entry 0x%x in "
1747 "CuesTrackPositions\n", id
);
1751 res
= ebml_read_skip(matroska
);
1755 if (matroska
->level_up
) {
1756 matroska
->level_up
--;
1764 av_log(matroska
->ctx
, AV_LOG_INFO
,
1765 "Unknown entry 0x%x in cuespoint "
1770 res
= ebml_read_skip(matroska
);
1774 if (matroska
->level_up
) {
1775 matroska
->level_up
--;
1780 /* so let's see if we got what we wanted */
1781 if (idx
.pos
!= (uint64_t) -1 &&
1782 idx
.time
!= (uint64_t) -1 &&
1783 idx
.track
!= (uint16_t) -1) {
1784 if (matroska
->num_indexes
% 32 == 0) {
1785 /* re-allocate bigger index */
1787 av_realloc(matroska
->index
,
1788 (matroska
->num_indexes
+ 32) *
1789 sizeof(MatroskaDemuxIndex
));
1791 matroska
->index
[matroska
->num_indexes
] = idx
;
1792 matroska
->num_indexes
++;
1797 av_log(matroska
->ctx
, AV_LOG_INFO
,
1798 "Unknown entry 0x%x in cues header\n", id
);
1802 res
= ebml_read_skip(matroska
);
1806 if (matroska
->level_up
) {
1807 matroska
->level_up
--;
1816 matroska_parse_metadata (MatroskaDemuxContext
*matroska
)
1822 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1825 } else if (matroska
->level_up
) {
1826 matroska
->level_up
--;
1831 /* Hm, this is unsupported... */
1833 av_log(matroska
->ctx
, AV_LOG_INFO
,
1834 "Unknown entry 0x%x in metadata header\n", id
);
1838 res
= ebml_read_skip(matroska
);
1842 if (matroska
->level_up
) {
1843 matroska
->level_up
--;
1852 matroska_parse_seekhead (MatroskaDemuxContext
*matroska
)
1857 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "parsing seekhead...\n");
1860 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1863 } else if (matroska
->level_up
) {
1864 matroska
->level_up
--;
1869 case MATROSKA_ID_SEEKENTRY
: {
1870 uint32_t seek_id
= 0, peek_id_cache
= 0;
1871 uint64_t seek_pos
= (uint64_t) -1, t
;
1873 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1877 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
1880 } else if (matroska
->level_up
) {
1881 matroska
->level_up
--;
1886 case MATROSKA_ID_SEEKID
:
1887 res
= ebml_read_uint(matroska
, &id
, &t
);
1891 case MATROSKA_ID_SEEKPOSITION
:
1892 res
= ebml_read_uint(matroska
, &id
, &seek_pos
);
1896 av_log(matroska
->ctx
, AV_LOG_INFO
,
1897 "Unknown seekhead ID 0x%x\n", id
);
1901 res
= ebml_read_skip(matroska
);
1905 if (matroska
->level_up
) {
1906 matroska
->level_up
--;
1911 if (!seek_id
|| seek_pos
== (uint64_t) -1) {
1912 av_log(matroska
->ctx
, AV_LOG_INFO
,
1913 "Incomplete seekhead entry (0x%x/%"PRIu64
")\n",
1919 case MATROSKA_ID_CUES
:
1920 case MATROSKA_ID_TAGS
: {
1921 uint32_t level_up
= matroska
->level_up
;
1922 offset_t before_pos
;
1924 MatroskaLevel level
;
1926 /* remember the peeked ID and the current position */
1927 peek_id_cache
= matroska
->peek_id
;
1928 before_pos
= url_ftell(matroska
->ctx
->pb
);
1931 if ((res
= ebml_read_seek(matroska
, seek_pos
+
1932 matroska
->segment_start
)) < 0)
1935 /* we don't want to lose our seekhead level, so we add
1936 * a dummy. This is a crude hack. */
1937 if (matroska
->num_levels
== EBML_MAX_DEPTH
) {
1938 av_log(matroska
->ctx
, AV_LOG_INFO
,
1939 "Max EBML element depth (%d) reached, "
1940 "cannot parse further.\n", EBML_MAX_DEPTH
);
1941 return AVERROR_UNKNOWN
;
1945 level
.length
= (uint64_t)-1;
1946 matroska
->levels
[matroska
->num_levels
] = level
;
1947 matroska
->num_levels
++;
1950 if (!(id
= ebml_peek_id (matroska
,
1951 &matroska
->level_up
)))
1953 if (id
!= seek_id
) {
1954 av_log(matroska
->ctx
, AV_LOG_INFO
,
1955 "We looked for ID=0x%x but got "
1956 "ID=0x%x (pos=%"PRIu64
")",
1957 seek_id
, id
, seek_pos
+
1958 matroska
->segment_start
);
1962 /* read master + parse */
1963 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
1966 case MATROSKA_ID_CUES
:
1967 if (!(res
= matroska_parse_index(matroska
)) ||
1968 url_feof(matroska
->ctx
->pb
)) {
1969 matroska
->index_parsed
= 1;
1973 case MATROSKA_ID_TAGS
:
1974 if (!(res
= matroska_parse_metadata(matroska
)) ||
1975 url_feof(matroska
->ctx
->pb
)) {
1976 matroska
->metadata_parsed
= 1;
1983 /* remove dummy level */
1984 while (matroska
->num_levels
) {
1985 matroska
->num_levels
--;
1987 matroska
->levels
[matroska
->num_levels
].length
;
1988 if (length
== (uint64_t)-1)
1993 if ((res
= ebml_read_seek(matroska
, before_pos
)) < 0)
1995 matroska
->peek_id
= peek_id_cache
;
1996 matroska
->level_up
= level_up
;
2001 av_log(matroska
->ctx
, AV_LOG_INFO
,
2002 "Ignoring seekhead entry for ID=0x%x\n",
2011 av_log(matroska
->ctx
, AV_LOG_INFO
,
2012 "Unknown seekhead ID 0x%x\n", id
);
2016 res
= ebml_read_skip(matroska
);
2020 if (matroska
->level_up
) {
2021 matroska
->level_up
--;
2030 matroska_parse_attachments(AVFormatContext
*s
)
2032 MatroskaDemuxContext
*matroska
= s
->priv_data
;
2036 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "parsing attachments...\n");
2039 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2042 } else if (matroska
->level_up
) {
2043 matroska
->level_up
--;
2048 case MATROSKA_ID_ATTACHEDFILE
: {
2051 uint8_t* data
= NULL
;
2052 int i
, data_size
= 0;
2055 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2059 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2062 } else if (matroska
->level_up
) {
2063 matroska
->level_up
--;
2068 case MATROSKA_ID_FILENAME
:
2069 res
= ebml_read_utf8 (matroska
, &id
, &name
);
2072 case MATROSKA_ID_FILEMIMETYPE
:
2073 res
= ebml_read_ascii (matroska
, &id
, &mime
);
2076 case MATROSKA_ID_FILEDATA
:
2077 res
= ebml_read_binary(matroska
, &id
, &data
, &data_size
);
2081 av_log(matroska
->ctx
, AV_LOG_INFO
,
2082 "Unknown attachedfile ID 0x%x\n", id
);
2084 res
= ebml_read_skip(matroska
);
2088 if (matroska
->level_up
) {
2089 matroska
->level_up
--;
2094 if (!(name
&& mime
&& data
&& data_size
> 0)) {
2095 av_log(matroska
->ctx
, AV_LOG_ERROR
, "incomplete attachment\n");
2099 st
= av_new_stream(s
, matroska
->num_streams
++);
2101 return AVERROR(ENOMEM
);
2102 st
->filename
= av_strdup(name
);
2103 st
->codec
->codec_id
= CODEC_ID_NONE
;
2104 st
->codec
->codec_type
= CODEC_TYPE_ATTACHMENT
;
2105 st
->codec
->extradata
= av_malloc(data_size
);
2106 if(st
->codec
->extradata
== NULL
)
2107 return AVERROR(ENOMEM
);
2108 st
->codec
->extradata_size
= data_size
;
2109 memcpy(st
->codec
->extradata
, data
, data_size
);
2111 for (i
=0; ff_mkv_mime_tags
[i
].id
!= CODEC_ID_NONE
; i
++) {
2112 if (!strncmp(ff_mkv_mime_tags
[i
].str
, mime
,
2113 strlen(ff_mkv_mime_tags
[i
].str
))) {
2114 st
->codec
->codec_id
= ff_mkv_mime_tags
[i
].id
;
2119 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "new attachment: %s, %s, size %d \n", name
, mime
, data_size
);
2124 av_log(matroska
->ctx
, AV_LOG_INFO
,
2125 "Unknown attachments ID 0x%x\n", id
);
2129 res
= ebml_read_skip(matroska
);
2133 if (matroska
->level_up
) {
2134 matroska
->level_up
--;
2143 matroska_parse_chapters(AVFormatContext
*s
)
2145 MatroskaDemuxContext
*matroska
= s
->priv_data
;
2149 av_log(s
, AV_LOG_DEBUG
, "parsing chapters...\n");
2152 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2155 } else if (matroska
->level_up
) {
2156 matroska
->level_up
--;
2161 case MATROSKA_ID_EDITIONENTRY
: {
2162 uint64_t end
= AV_NOPTS_VALUE
, start
= AV_NOPTS_VALUE
;
2165 /* if there is more than one chapter edition
2166 we take only the first one */
2168 ebml_read_skip(matroska
);
2172 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2176 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2179 } else if (matroska
->level_up
) {
2180 matroska
->level_up
--;
2185 case MATROSKA_ID_CHAPTERATOM
:
2186 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2190 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2193 } else if (matroska
->level_up
) {
2194 matroska
->level_up
--;
2199 case MATROSKA_ID_CHAPTERTIMEEND
:
2200 res
= ebml_read_uint(matroska
, &id
, &end
);
2203 case MATROSKA_ID_CHAPTERTIMESTART
:
2204 res
= ebml_read_uint(matroska
, &id
, &start
);
2207 case MATROSKA_ID_CHAPTERDISPLAY
:
2208 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2212 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2215 } else if (matroska
->level_up
) {
2216 matroska
->level_up
--;
2221 case MATROSKA_ID_CHAPSTRING
:
2222 res
= ebml_read_utf8(matroska
, &id
, &title
);
2226 av_log(s
, AV_LOG_INFO
, "Ignoring unknown Chapter display ID 0x%x\n", id
);
2228 res
= ebml_read_skip(matroska
);
2232 if (matroska
->level_up
) {
2233 matroska
->level_up
--;
2239 case MATROSKA_ID_CHAPTERUID
:
2240 res
= ebml_read_uint(matroska
, &id
, &uid
);
2243 av_log(s
, AV_LOG_INFO
, "Ignoring unknown Chapter atom ID 0x%x\n", id
);
2244 case MATROSKA_ID_CHAPTERFLAGHIDDEN
:
2246 res
= ebml_read_skip(matroska
);
2250 if (matroska
->level_up
) {
2251 matroska
->level_up
--;
2256 if (start
!= AV_NOPTS_VALUE
&& uid
!= -1) {
2257 if(!ff_new_chapter(s
, uid
, (AVRational
){1, 1000000000}, start
, end
, title
))
2258 res
= AVERROR(ENOMEM
);
2264 av_log(s
, AV_LOG_INFO
, "Ignoring unknown Edition entry ID 0x%x\n", id
);
2265 case MATROSKA_ID_EDITIONUID
:
2266 case MATROSKA_ID_EDITIONFLAGHIDDEN
:
2268 res
= ebml_read_skip(matroska
);
2273 if (matroska
->level_up
) {
2274 matroska
->level_up
--;
2282 av_log(s
, AV_LOG_INFO
, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY
, id
);
2284 res
= ebml_read_skip(matroska
);
2288 if (matroska
->level_up
) {
2289 matroska
->level_up
--;
2297 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
2300 matroska_aac_profile (char *codec_id
)
2302 static const char *aac_profiles
[] = {
2307 for (profile
=0; profile
<ARRAY_SIZE(aac_profiles
); profile
++)
2308 if (strstr(codec_id
, aac_profiles
[profile
]))
2314 matroska_aac_sri (int samplerate
)
2318 for (sri
=0; sri
<ARRAY_SIZE(ff_mpeg4audio_sample_rates
); sri
++)
2319 if (ff_mpeg4audio_sample_rates
[sri
] == samplerate
)
2325 matroska_read_header (AVFormatContext
*s
,
2326 AVFormatParameters
*ap
)
2328 MatroskaDemuxContext
*matroska
= s
->priv_data
;
2330 int version
, last_level
, res
= 0;
2335 /* First read the EBML header. */
2337 if ((res
= ebml_read_header(matroska
, &doctype
, &version
)) < 0)
2339 if ((doctype
== NULL
) || strcmp(doctype
, "matroska")) {
2340 av_log(matroska
->ctx
, AV_LOG_ERROR
,
2341 "Wrong EBML doctype ('%s' != 'matroska').\n",
2342 doctype
? doctype
: "(none)");
2345 return AVERROR_NOFMT
;
2349 av_log(matroska
->ctx
, AV_LOG_ERROR
,
2350 "Matroska demuxer version 2 too old for file version %d\n",
2352 return AVERROR_NOFMT
;
2355 /* The next thing is a segment. */
2357 if (!(id
= ebml_peek_id(matroska
, &last_level
)))
2358 return AVERROR(EIO
);
2359 if (id
== MATROSKA_ID_SEGMENT
)
2363 av_log(matroska
->ctx
, AV_LOG_INFO
,
2364 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2365 MATROSKA_ID_SEGMENT
, id
);
2366 if ((res
= ebml_read_skip(matroska
)) < 0)
2370 /* We now have a Matroska segment.
2371 * Seeks are from the beginning of the segment,
2372 * after the segment ID/length. */
2373 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2375 matroska
->segment_start
= url_ftell(s
->pb
);
2377 matroska
->time_scale
= 1000000;
2378 /* we've found our segment, start reading the different contents in here */
2380 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
2383 } else if (matroska
->level_up
) {
2384 matroska
->level_up
--;
2390 case MATROSKA_ID_INFO
: {
2391 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2393 res
= matroska_parse_info(matroska
);
2397 /* track info headers */
2398 case MATROSKA_ID_TRACKS
: {
2399 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2401 res
= matroska_parse_tracks(matroska
);
2406 case MATROSKA_ID_CUES
: {
2407 if (!matroska
->index_parsed
) {
2408 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2410 res
= matroska_parse_index(matroska
);
2412 res
= ebml_read_skip(matroska
);
2417 case MATROSKA_ID_TAGS
: {
2418 if (!matroska
->metadata_parsed
) {
2419 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2421 res
= matroska_parse_metadata(matroska
);
2423 res
= ebml_read_skip(matroska
);
2427 /* file index (if seekable, seek to Cues/Tags to parse it) */
2428 case MATROSKA_ID_SEEKHEAD
: {
2429 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2431 res
= matroska_parse_seekhead(matroska
);
2435 case MATROSKA_ID_ATTACHMENTS
: {
2436 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2438 res
= matroska_parse_attachments(s
);
2442 case MATROSKA_ID_CLUSTER
: {
2443 /* Do not read the master - this will be done in the next
2444 * call to matroska_read_packet. */
2449 case MATROSKA_ID_CHAPTERS
: {
2450 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
2452 res
= matroska_parse_chapters(s
);
2457 av_log(matroska
->ctx
, AV_LOG_INFO
,
2458 "Unknown matroska file header ID 0x%x\n", id
);
2462 res
= ebml_read_skip(matroska
);
2466 if (matroska
->level_up
) {
2467 matroska
->level_up
--;
2472 /* Have we found a cluster? */
2473 if (ebml_peek_id(matroska
, NULL
) == MATROSKA_ID_CLUSTER
) {
2475 MatroskaTrack
*track
;
2478 for (i
= 0; i
< matroska
->num_tracks
; i
++) {
2479 enum CodecID codec_id
= CODEC_ID_NONE
;
2480 uint8_t *extradata
= NULL
;
2481 int extradata_size
= 0;
2482 int extradata_offset
= 0;
2483 track
= matroska
->tracks
[i
];
2484 track
->stream_index
= -1;
2486 /* Apply some sanity checks. */
2487 if (track
->codec_id
== NULL
)
2490 for(j
=0; ff_mkv_codec_tags
[j
].id
!= CODEC_ID_NONE
; j
++){
2491 if(!strncmp(ff_mkv_codec_tags
[j
].str
, track
->codec_id
,
2492 strlen(ff_mkv_codec_tags
[j
].str
))){
2493 codec_id
= ff_mkv_codec_tags
[j
].id
;
2498 /* Set the FourCC from the CodecID. */
2499 /* This is the MS compatibility mode which stores a
2500 * BITMAPINFOHEADER in the CodecPrivate. */
2501 if (!strcmp(track
->codec_id
,
2502 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC
) &&
2503 (track
->codec_priv_size
>= 40) &&
2504 (track
->codec_priv
!= NULL
)) {
2505 MatroskaVideoTrack
*vtrack
= (MatroskaVideoTrack
*) track
;
2507 /* Offset of biCompression. Stored in LE. */
2508 vtrack
->fourcc
= AV_RL32(track
->codec_priv
+ 16);
2509 codec_id
= codec_get_id(codec_bmp_tags
, vtrack
->fourcc
);
2513 /* This is the MS compatibility mode which stores a
2514 * WAVEFORMATEX in the CodecPrivate. */
2515 else if (!strcmp(track
->codec_id
,
2516 MATROSKA_CODEC_ID_AUDIO_ACM
) &&
2517 (track
->codec_priv_size
>= 18) &&
2518 (track
->codec_priv
!= NULL
)) {
2521 /* Offset of wFormatTag. Stored in LE. */
2522 tag
= AV_RL16(track
->codec_priv
);
2523 codec_id
= codec_get_id(codec_wav_tags
, tag
);
2527 else if (codec_id
== CODEC_ID_AAC
&& !track
->codec_priv_size
) {
2528 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*) track
;
2529 int profile
= matroska_aac_profile(track
->codec_id
);
2530 int sri
= matroska_aac_sri(audiotrack
->internal_samplerate
);
2531 extradata
= av_malloc(5);
2532 if (extradata
== NULL
)
2533 return AVERROR(ENOMEM
);
2534 extradata
[0] = (profile
<< 3) | ((sri
&0x0E) >> 1);
2535 extradata
[1] = ((sri
&0x01) << 7) | (audiotrack
->channels
<<3);
2536 if (strstr(track
->codec_id
, "SBR")) {
2537 sri
= matroska_aac_sri(audiotrack
->samplerate
);
2538 extradata
[2] = 0x56;
2539 extradata
[3] = 0xE5;
2540 extradata
[4] = 0x80 | (sri
<<3);
2547 else if (codec_id
== CODEC_ID_TTA
) {
2548 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*) track
;
2550 extradata_size
= 30;
2551 extradata
= av_mallocz(extradata_size
);
2552 if (extradata
== NULL
)
2553 return AVERROR(ENOMEM
);
2554 init_put_byte(&b
, extradata
, extradata_size
, 1,
2555 NULL
, NULL
, NULL
, NULL
);
2556 put_buffer(&b
, "TTA1", 4);
2558 put_le16(&b
, audiotrack
->channels
);
2559 put_le16(&b
, audiotrack
->bitdepth
);
2560 put_le32(&b
, audiotrack
->samplerate
);
2561 put_le32(&b
, matroska
->ctx
->duration
* audiotrack
->samplerate
);
2564 else if (codec_id
== CODEC_ID_RV10
|| codec_id
== CODEC_ID_RV20
||
2565 codec_id
== CODEC_ID_RV30
|| codec_id
== CODEC_ID_RV40
) {
2566 extradata_offset
= 26;
2567 track
->codec_priv_size
-= extradata_offset
;
2570 else if (codec_id
== CODEC_ID_RA_144
) {
2571 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*)track
;
2572 audiotrack
->samplerate
= 8000;
2573 audiotrack
->channels
= 1;
2576 else if (codec_id
== CODEC_ID_RA_288
||
2577 codec_id
== CODEC_ID_COOK
||
2578 codec_id
== CODEC_ID_ATRAC3
) {
2579 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*)track
;
2582 init_put_byte(&b
, track
->codec_priv
, track
->codec_priv_size
, 0,
2583 NULL
, NULL
, NULL
, NULL
);
2585 audiotrack
->coded_framesize
= get_be32(&b
);
2587 audiotrack
->sub_packet_h
= get_be16(&b
);
2588 audiotrack
->frame_size
= get_be16(&b
);
2589 audiotrack
->sub_packet_size
= get_be16(&b
);
2590 audiotrack
->buf
= av_malloc(audiotrack
->frame_size
* audiotrack
->sub_packet_h
);
2591 if (codec_id
== CODEC_ID_RA_288
) {
2592 audiotrack
->block_align
= audiotrack
->coded_framesize
;
2593 track
->codec_priv_size
= 0;
2595 audiotrack
->block_align
= audiotrack
->sub_packet_size
;
2596 extradata_offset
= 78;
2597 track
->codec_priv_size
-= extradata_offset
;
2601 if (codec_id
== CODEC_ID_NONE
) {
2602 av_log(matroska
->ctx
, AV_LOG_INFO
,
2603 "Unknown/unsupported CodecID %s.\n",
2607 track
->stream_index
= matroska
->num_streams
;
2609 matroska
->num_streams
++;
2610 st
= av_new_stream(s
, track
->stream_index
);
2612 return AVERROR(ENOMEM
);
2613 av_set_pts_info(st
, 64, matroska
->time_scale
, 1000*1000*1000); /* 64 bit pts in ns */
2615 st
->codec
->codec_id
= codec_id
;
2617 if (strcmp(track
->language
, "und"))
2618 strcpy(st
->language
, track
->language
);
2620 if (track
->flags
& MATROSKA_TRACK_DEFAULT
)
2621 st
->disposition
|= AV_DISPOSITION_DEFAULT
;
2623 if (track
->default_duration
)
2624 av_reduce(&st
->codec
->time_base
.num
, &st
->codec
->time_base
.den
,
2625 track
->default_duration
, 1000000000, 30000);
2628 st
->codec
->extradata
= extradata
;
2629 st
->codec
->extradata_size
= extradata_size
;
2630 } else if(track
->codec_priv
&& track
->codec_priv_size
> 0){
2631 st
->codec
->extradata
= av_malloc(track
->codec_priv_size
);
2632 if(st
->codec
->extradata
== NULL
)
2633 return AVERROR(ENOMEM
);
2634 st
->codec
->extradata_size
= track
->codec_priv_size
;
2635 memcpy(st
->codec
->extradata
,track
->codec_priv
+extradata_offset
,
2636 track
->codec_priv_size
);
2639 if (track
->type
== MATROSKA_TRACK_TYPE_VIDEO
) {
2640 MatroskaVideoTrack
*videotrack
= (MatroskaVideoTrack
*)track
;
2642 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
2643 st
->codec
->codec_tag
= videotrack
->fourcc
;
2644 st
->codec
->width
= videotrack
->pixel_width
;
2645 st
->codec
->height
= videotrack
->pixel_height
;
2646 if (videotrack
->display_width
== 0)
2647 videotrack
->display_width
= videotrack
->pixel_width
;
2648 if (videotrack
->display_height
== 0)
2649 videotrack
->display_height
= videotrack
->pixel_height
;
2650 av_reduce(&st
->codec
->sample_aspect_ratio
.num
,
2651 &st
->codec
->sample_aspect_ratio
.den
,
2652 st
->codec
->height
* videotrack
->display_width
,
2653 st
->codec
-> width
* videotrack
->display_height
,
2655 st
->need_parsing
= AVSTREAM_PARSE_HEADERS
;
2656 } else if (track
->type
== MATROSKA_TRACK_TYPE_AUDIO
) {
2657 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*)track
;
2659 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
2660 st
->codec
->sample_rate
= audiotrack
->samplerate
;
2661 st
->codec
->channels
= audiotrack
->channels
;
2662 st
->codec
->block_align
= audiotrack
->block_align
;
2663 } else if (track
->type
== MATROSKA_TRACK_TYPE_SUBTITLE
) {
2664 st
->codec
->codec_type
= CODEC_TYPE_SUBTITLE
;
2667 /* What do we do with private data? E.g. for Vorbis. */
2672 if (matroska
->index_parsed
) {
2673 int i
, track
, stream
;
2674 for (i
=0; i
<matroska
->num_indexes
; i
++) {
2675 MatroskaDemuxIndex
*idx
= &matroska
->index
[i
];
2676 track
= matroska_find_track_by_num(matroska
, idx
->track
);
2677 if (track
< 0) continue;
2678 stream
= matroska
->tracks
[track
]->stream_index
;
2679 if (stream
>= 0 && stream
< matroska
->ctx
->nb_streams
)
2680 av_add_index_entry(matroska
->ctx
->streams
[stream
],
2681 idx
->pos
, idx
->time
/matroska
->time_scale
,
2682 0, 0, AVINDEX_KEYFRAME
);
2690 matroska_parse_block(MatroskaDemuxContext
*matroska
, uint8_t *data
, int size
,
2691 int64_t pos
, uint64_t cluster_time
, uint64_t duration
,
2692 int is_keyframe
, int is_bframe
)
2698 uint8_t *origdata
= data
;
2700 uint32_t *lace_size
= NULL
;
2701 int n
, flags
, laces
= 0;
2705 /* first byte(s): tracknum */
2706 if ((n
= matroska_ebmlnum_uint(data
, size
, &num
)) < 0) {
2707 av_log(matroska
->ctx
, AV_LOG_ERROR
, "EBML block data error\n");
2714 /* fetch track from num */
2715 track
= matroska_find_track_by_num(matroska
, num
);
2716 if (size
<= 3 || track
< 0 || track
>= matroska
->num_tracks
) {
2717 av_log(matroska
->ctx
, AV_LOG_INFO
,
2718 "Invalid stream %d or size %u\n", track
, size
);
2722 stream_index
= matroska
->tracks
[track
]->stream_index
;
2723 if (stream_index
< 0 || stream_index
>= matroska
->ctx
->nb_streams
) {
2727 st
= matroska
->ctx
->streams
[stream_index
];
2728 if (st
->discard
>= AVDISCARD_ALL
) {
2732 if (duration
== AV_NOPTS_VALUE
)
2733 duration
= matroska
->tracks
[track
]->default_duration
/ matroska
->time_scale
;
2735 /* block_time (relative to cluster time) */
2736 block_time
= AV_RB16(data
);
2740 if (is_keyframe
== -1)
2741 is_keyframe
= flags
& 0x80 ? PKT_FLAG_KEY
: 0;
2743 if (matroska
->skip_to_keyframe
) {
2744 if (!is_keyframe
|| st
!= matroska
->skip_to_stream
) {
2748 matroska
->skip_to_keyframe
= 0;
2751 switch ((flags
& 0x06) >> 1) {
2752 case 0x0: /* no lacing */
2754 lace_size
= av_mallocz(sizeof(int));
2755 lace_size
[0] = size
;
2758 case 0x1: /* xiph lacing */
2759 case 0x2: /* fixed-size lacing */
2760 case 0x3: /* EBML lacing */
2765 laces
= (*data
) + 1;
2768 lace_size
= av_mallocz(laces
* sizeof(int));
2770 switch ((flags
& 0x06) >> 1) {
2771 case 0x1: /* xiph lacing */ {
2774 for (n
= 0; res
== 0 && n
< laces
- 1; n
++) {
2781 lace_size
[n
] += temp
;
2787 total
+= lace_size
[n
];
2789 lace_size
[n
] = size
- total
;
2793 case 0x2: /* fixed-size lacing */
2794 for (n
= 0; n
< laces
; n
++)
2795 lace_size
[n
] = size
/ laces
;
2798 case 0x3: /* EBML lacing */ {
2800 n
= matroska_ebmlnum_uint(data
, size
, &num
);
2802 av_log(matroska
->ctx
, AV_LOG_INFO
,
2803 "EBML block data error\n");
2808 total
= lace_size
[0] = num
;
2809 for (n
= 1; res
== 0 && n
< laces
- 1; n
++) {
2812 r
= matroska_ebmlnum_sint (data
, size
, &snum
);
2814 av_log(matroska
->ctx
, AV_LOG_INFO
,
2815 "EBML block data error\n");
2820 lace_size
[n
] = lace_size
[n
- 1] + snum
;
2821 total
+= lace_size
[n
];
2823 lace_size
[n
] = size
- total
;
2831 uint64_t timecode
= AV_NOPTS_VALUE
;
2833 if (cluster_time
!= (uint64_t)-1
2834 && (block_time
>= 0 || cluster_time
>= -block_time
))
2835 timecode
= cluster_time
+ block_time
;
2837 for (n
= 0; n
< laces
; n
++) {
2838 if (st
->codec
->codec_id
== CODEC_ID_RA_288
||
2839 st
->codec
->codec_id
== CODEC_ID_COOK
||
2840 st
->codec
->codec_id
== CODEC_ID_ATRAC3
) {
2841 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*)matroska
->tracks
[track
];
2842 int a
= st
->codec
->block_align
;
2843 int sps
= audiotrack
->sub_packet_size
;
2844 int cfs
= audiotrack
->coded_framesize
;
2845 int h
= audiotrack
->sub_packet_h
;
2846 int y
= audiotrack
->sub_packet_cnt
;
2847 int w
= audiotrack
->frame_size
;
2850 if (!audiotrack
->pkt_cnt
) {
2851 if (st
->codec
->codec_id
== CODEC_ID_RA_288
)
2852 for (x
=0; x
<h
/2; x
++)
2853 memcpy(audiotrack
->buf
+x
*2*w
+y
*cfs
,
2856 for (x
=0; x
<w
/sps
; x
++)
2857 memcpy(audiotrack
->buf
+sps
*(h
*x
+((h
+1)/2)*(y
&1)+(y
>>1)), data
+x
*sps
, sps
);
2859 if (++audiotrack
->sub_packet_cnt
>= h
) {
2860 audiotrack
->sub_packet_cnt
= 0;
2861 audiotrack
->pkt_cnt
= h
*w
/ a
;
2864 while (audiotrack
->pkt_cnt
) {
2865 pkt
= av_mallocz(sizeof(AVPacket
));
2866 av_new_packet(pkt
, a
);
2867 memcpy(pkt
->data
, audiotrack
->buf
2868 + a
* (h
*w
/ a
- audiotrack
->pkt_cnt
--), a
);
2870 pkt
->stream_index
= stream_index
;
2871 matroska_queue_packet(matroska
, pkt
);
2874 int result
, offset
= 0, ilen
, olen
, pkt_size
= lace_size
[n
];
2875 uint8_t *pkt_data
= data
;
2877 if (matroska
->tracks
[track
]->encoding_scope
& 1) {
2878 switch (matroska
->tracks
[track
]->encoding_algo
) {
2879 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP
:
2880 offset
= matroska
->tracks
[track
]->encoding_settings_len
;
2882 case MATROSKA_TRACK_ENCODING_COMP_LZO
:
2885 ilen
= lace_size
[n
];
2886 olen
= pkt_size
*= 3;
2887 pkt_data
= av_realloc(pkt_data
,
2888 pkt_size
+LZO_OUTPUT_PADDING
);
2889 result
= lzo1x_decode(pkt_data
, &olen
, data
, &ilen
);
2890 } while (result
==LZO_OUTPUT_FULL
&& pkt_size
<10000000);
2898 case MATROSKA_TRACK_ENCODING_COMP_ZLIB
: {
2899 z_stream zstream
= {0};
2901 if (inflateInit(&zstream
) != Z_OK
)
2903 zstream
.next_in
= data
;
2904 zstream
.avail_in
= lace_size
[n
];
2907 pkt_data
= av_realloc(pkt_data
, pkt_size
);
2908 zstream
.avail_out
= pkt_size
- zstream
.total_out
;
2909 zstream
.next_out
= pkt_data
+ zstream
.total_out
;
2910 result
= inflate(&zstream
, Z_NO_FLUSH
);
2911 } while (result
==Z_OK
&& pkt_size
<10000000);
2912 pkt_size
= zstream
.total_out
;
2913 inflateEnd(&zstream
);
2914 if (result
!= Z_STREAM_END
) {
2922 case MATROSKA_TRACK_ENCODING_COMP_BZLIB
: {
2923 bz_stream bzstream
= {0};
2925 if (BZ2_bzDecompressInit(&bzstream
, 0, 0) != BZ_OK
)
2927 bzstream
.next_in
= data
;
2928 bzstream
.avail_in
= lace_size
[n
];
2931 pkt_data
= av_realloc(pkt_data
, pkt_size
);
2932 bzstream
.avail_out
= pkt_size
- bzstream
.total_out_lo32
;
2933 bzstream
.next_out
= pkt_data
+ bzstream
.total_out_lo32
;
2934 result
= BZ2_bzDecompress(&bzstream
);
2935 } while (result
==BZ_OK
&& pkt_size
<10000000);
2936 pkt_size
= bzstream
.total_out_lo32
;
2937 BZ2_bzDecompressEnd(&bzstream
);
2938 if (result
!= BZ_STREAM_END
) {
2948 pkt
= av_mallocz(sizeof(AVPacket
));
2949 /* XXX: prevent data copy... */
2950 if (av_new_packet(pkt
, pkt_size
+offset
) < 0) {
2951 res
= AVERROR(ENOMEM
);
2956 memcpy (pkt
->data
, matroska
->tracks
[track
]->encoding_settings
, offset
);
2957 memcpy (pkt
->data
+offset
, pkt_data
, pkt_size
);
2960 pkt
->flags
= is_keyframe
;
2961 pkt
->stream_index
= stream_index
;
2963 pkt
->pts
= timecode
;
2965 pkt
->duration
= duration
;
2967 matroska_queue_packet(matroska
, pkt
);
2970 if (timecode
!= AV_NOPTS_VALUE
)
2971 timecode
= duration
? timecode
+ duration
: AV_NOPTS_VALUE
;
2972 data
+= lace_size
[n
];
2982 matroska_parse_blockgroup (MatroskaDemuxContext
*matroska
,
2983 uint64_t cluster_time
)
2988 int is_keyframe
= PKT_FLAG_KEY
, last_num_packets
= matroska
->num_packets
;
2989 uint64_t duration
= AV_NOPTS_VALUE
;
2994 av_log(matroska
->ctx
, AV_LOG_DEBUG
, "parsing blockgroup...\n");
2997 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
3000 } else if (matroska
->level_up
) {
3001 matroska
->level_up
--;
3006 /* one block inside the group. Note, block parsing is one
3007 * of the harder things, so this code is a bit complicated.
3008 * See http://www.matroska.org/ for documentation. */
3009 case MATROSKA_ID_BLOCK
: {
3010 pos
= url_ftell(matroska
->ctx
->pb
);
3011 res
= ebml_read_binary(matroska
, &id
, &data
, &size
);
3015 case MATROSKA_ID_BLOCKDURATION
: {
3016 if ((res
= ebml_read_uint(matroska
, &id
, &duration
)) < 0)
3021 case MATROSKA_ID_BLOCKREFERENCE
: {
3023 /* We've found a reference, so not even the first frame in
3024 * the lace is a key frame. */
3026 if (last_num_packets
!= matroska
->num_packets
)
3027 matroska
->packets
[last_num_packets
]->flags
= 0;
3028 if ((res
= ebml_read_sint(matroska
, &id
, &num
)) < 0)
3036 av_log(matroska
->ctx
, AV_LOG_INFO
,
3037 "Unknown entry 0x%x in blockgroup data\n", id
);
3041 res
= ebml_read_skip(matroska
);
3045 if (matroska
->level_up
) {
3046 matroska
->level_up
--;
3055 res
= matroska_parse_block(matroska
, data
, size
, pos
, cluster_time
,
3056 duration
, is_keyframe
, is_bframe
);
3062 matroska_parse_cluster (MatroskaDemuxContext
*matroska
)
3066 uint64_t cluster_time
= 0;
3071 av_log(matroska
->ctx
, AV_LOG_DEBUG
,
3072 "parsing cluster at %"PRId64
"\n", url_ftell(matroska
->ctx
->pb
));
3075 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
3078 } else if (matroska
->level_up
) {
3079 matroska
->level_up
--;
3084 /* cluster timecode */
3085 case MATROSKA_ID_CLUSTERTIMECODE
: {
3087 if ((res
= ebml_read_uint(matroska
, &id
, &num
)) < 0)
3093 /* a group of blocks inside a cluster */
3094 case MATROSKA_ID_BLOCKGROUP
:
3095 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
3097 res
= matroska_parse_blockgroup(matroska
, cluster_time
);
3100 case MATROSKA_ID_SIMPLEBLOCK
:
3101 pos
= url_ftell(matroska
->ctx
->pb
);
3102 res
= ebml_read_binary(matroska
, &id
, &data
, &size
);
3104 res
= matroska_parse_block(matroska
, data
, size
, pos
,
3105 cluster_time
, AV_NOPTS_VALUE
,
3110 av_log(matroska
->ctx
, AV_LOG_INFO
,
3111 "Unknown entry 0x%x in cluster data\n", id
);
3115 res
= ebml_read_skip(matroska
);
3119 if (matroska
->level_up
) {
3120 matroska
->level_up
--;
3129 matroska_read_packet (AVFormatContext
*s
,
3132 MatroskaDemuxContext
*matroska
= s
->priv_data
;
3136 /* Read stream until we have a packet queued. */
3137 while (matroska_deliver_packet(matroska
, pkt
)) {
3139 /* Have we already reached the end? */
3141 return AVERROR(EIO
);
3145 if (!(id
= ebml_peek_id(matroska
, &matroska
->level_up
))) {
3146 return AVERROR(EIO
);
3147 } else if (matroska
->level_up
) {
3148 matroska
->level_up
--;
3153 case MATROSKA_ID_CLUSTER
:
3154 if ((res
= ebml_read_master(matroska
, &id
)) < 0)
3156 if ((res
= matroska_parse_cluster(matroska
)) == 0)
3157 res
= 1; /* Parsed one cluster, let's get out. */
3162 res
= ebml_read_skip(matroska
);
3166 if (matroska
->level_up
) {
3167 matroska
->level_up
--;
3180 matroska_read_seek (AVFormatContext
*s
, int stream_index
, int64_t timestamp
,
3183 MatroskaDemuxContext
*matroska
= s
->priv_data
;
3184 AVStream
*st
= s
->streams
[stream_index
];
3187 /* find index entry */
3188 index
= av_index_search_timestamp(st
, timestamp
, flags
);
3192 matroska_clear_queue(matroska
);
3195 url_fseek(s
->pb
, st
->index_entries
[index
].pos
, SEEK_SET
);
3196 matroska
->skip_to_keyframe
= !(flags
& AVSEEK_FLAG_ANY
);
3197 matroska
->skip_to_stream
= st
;
3198 matroska
->peek_id
= 0;
3203 matroska_read_close (AVFormatContext
*s
)
3205 MatroskaDemuxContext
*matroska
= s
->priv_data
;
3208 av_free(matroska
->writing_app
);
3209 av_free(matroska
->muxing_app
);
3210 av_free(matroska
->index
);
3212 matroska_clear_queue(matroska
);
3214 for (n
= 0; n
< matroska
->num_tracks
; n
++) {
3215 MatroskaTrack
*track
= matroska
->tracks
[n
];
3216 av_free(track
->codec_id
);
3217 av_free(track
->codec_name
);
3218 av_free(track
->codec_priv
);
3219 av_free(track
->name
);
3221 if (track
->type
== MATROSKA_TRACK_TYPE_AUDIO
) {
3222 MatroskaAudioTrack
*audiotrack
= (MatroskaAudioTrack
*)track
;
3223 av_free(audiotrack
->buf
);
3232 AVInputFormat matroska_demuxer
= {
3234 "Matroska file format",
3235 sizeof(MatroskaDemuxContext
),
3237 matroska_read_header
,
3238 matroska_read_packet
,
3239 matroska_read_close
,