2 * native ebml reader for the Matroska demuxer
3 * new parser copyright (c) 2010 Uoti Urpala
4 * copyright (c) 2004 Aurelien Jacobs <aurel@gnuage.org>
5 * based on the one written by Ronald Bultje for gstreamer
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <libavutil/intfloat_readwrite.h>
33 #include <libavutil/common.h>
36 #include "stream/stream.h"
41 #define SIZE_MAX ((size_t)-1)
45 * Read: the element content data ID.
48 uint32_t ebml_read_id(stream_t
*s
, int *length
)
50 int i
, len_mask
= 0x80;
53 for (i
= 0, id
= stream_read_char(s
); i
< 4 && !(id
& len_mask
); i
++)
56 return EBML_ID_INVALID
;
60 id
= (id
<< 8) | stream_read_char(s
);
65 * Read a variable length unsigned int.
67 uint64_t ebml_read_vlen_uint(uint8_t *buffer
, int *length
)
69 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
72 for (i
= 0, num
= *buffer
++; i
< 8 && !(num
& len_mask
); i
++)
75 return EBML_UINT_INVALID
;
79 if ((int) (num
&= (len_mask
- 1)) == len_mask
- 1)
82 num
= (num
<< 8) | *buffer
++;
83 if ((num
& 0xFF) == 0xFF)
87 return EBML_UINT_INVALID
;
92 * Read a variable length signed int.
94 int64_t ebml_read_vlen_int(uint8_t *buffer
, int *length
)
99 /* read as unsigned number first */
100 unum
= ebml_read_vlen_uint(buffer
, &l
);
101 if (unum
== EBML_UINT_INVALID
)
102 return EBML_INT_INVALID
;
106 return unum
- ((1 << ((7 * l
) - 1)) - 1);
110 * Read: element content length.
112 uint64_t ebml_read_length(stream_t
*s
, int *length
)
114 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
117 for (i
= 0, len
= stream_read_char(s
); i
< 8 && !(len
& len_mask
); i
++)
120 return EBML_UINT_INVALID
;
124 if ((int) (len
&= (len_mask
- 1)) == len_mask
- 1)
127 len
= (len
<< 8) | stream_read_char(s
);
128 if ((len
& 0xFF) == 0xFF)
132 return EBML_UINT_INVALID
;
137 * Read the next element as an unsigned int.
139 uint64_t ebml_read_uint(stream_t
*s
, uint64_t *length
)
141 uint64_t len
, value
= 0;
144 len
= ebml_read_length(s
, &l
);
145 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
146 return EBML_UINT_INVALID
;
151 value
= (value
<< 8) | stream_read_char(s
);
157 * Read the next element as a signed int.
159 int64_t ebml_read_int(stream_t
*s
, uint64_t *length
)
165 len
= ebml_read_length(s
, &l
);
166 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
167 return EBML_INT_INVALID
;
172 l
= stream_read_char(s
);
175 value
= (value
<< 8) | l
;
177 value
= (value
<< 8) | stream_read_char(s
);
183 * Read the next element as a float.
185 double ebml_read_float(stream_t
*s
, uint64_t *length
)
191 len
= ebml_read_length(s
, &l
);
194 value
= av_int2flt(stream_read_dword(s
));
198 value
= av_int2dbl(stream_read_qword(s
));
202 return EBML_FLOAT_INVALID
;
212 * Read the next element as an ASCII string.
214 char *ebml_read_ascii(stream_t
*s
, uint64_t *length
)
220 len
= ebml_read_length(s
, &l
);
221 if (len
== EBML_UINT_INVALID
)
223 if (len
> SIZE_MAX
- 1)
228 str
= malloc(len
+ 1);
229 if (stream_read(s
, str
, len
) != (int) len
) {
239 * Read the next element as a UTF-8 string.
241 char *ebml_read_utf8(stream_t
*s
, uint64_t *length
)
243 return ebml_read_ascii(s
, length
);
247 * Skip the next element.
249 int ebml_read_skip(stream_t
*s
, uint64_t *length
)
254 len
= ebml_read_length(s
, &l
);
255 if (len
== EBML_UINT_INVALID
)
266 * Read the next element, but only the header. The contents
267 * are supposed to be sub-elements which can be read separately.
269 uint32_t ebml_read_master(stream_t
*s
, uint64_t *length
)
274 id
= ebml_read_id(s
, NULL
);
275 if (id
== EBML_ID_INVALID
)
278 len
= ebml_read_length(s
, NULL
);
279 if (len
== EBML_UINT_INVALID
)
280 return EBML_ID_INVALID
;
289 #define EVALARGS(F, ...) F(__VA_ARGS__)
290 #define E(str, N, type) const struct ebml_elem_desc ebml_ ## N ## _desc = { str, type };
291 #define E_SN(str, count, N) const struct ebml_elem_desc ebml_ ## N ## _desc = { str, EBML_TYPE_SUBELEMENTS, sizeof(struct ebml_ ## N), count, (const struct ebml_field_desc[]){
292 #define E_S(str, count) EVALARGS(E_SN, str, count, N)
293 #define FN(id, name, multiple, N) { id, multiple, offsetof(struct ebml_ ## N, name), offsetof(struct ebml_ ## N, n_ ## name), &ebml_##name##_desc},
294 #define F(id, name, multiple) EVALARGS(FN, id, name, multiple, N)
295 #include "ebml_defs.c"
302 // Used to read/write pointers to different struct types
304 #define generic_struct struct generic
306 static uint32_t ebml_parse_id(uint8_t *data
, int *length
)
309 uint32_t id
= *data
++;
310 for (int len_mask
= 0x80; !(id
& len_mask
); len_mask
>>= 1) {
314 return EBML_ID_INVALID
;
319 id
= (id
<< 8) | *data
++;
323 static uint64_t parse_vlen(uint8_t *data
, int *length
, bool is_length
)
325 uint64_t r
= *data
++;
328 for (len_mask
= 0x80; !(r
& len_mask
); len_mask
>>= 1) {
338 if (r
== len_mask
- 1)
340 for (int i
= 1; i
< len
; i
++) {
343 r
= (r
<< 8) | *data
++;
345 if (is_length
&& num_allones
== len
) {
346 // According to Matroska specs this means "unknown length"
347 // Could be supported if there are any actual files using it
355 static uint64_t ebml_parse_length(uint8_t *data
, int *length
)
357 return parse_vlen(data
, length
, true);
360 static uint64_t ebml_parse_uint(uint8_t *data
, int length
)
362 assert(length
>= 1 && length
<= 8);
365 r
= (r
<< 8) + *data
++;
369 static int64_t ebml_parse_sint(uint8_t *data
, int length
)
371 assert(length
>=1 && length
<= 8);
376 r
= (r
<< 8) | *data
++;
380 static double ebml_parse_float(uint8_t *data
, int length
)
382 assert(length
== 4 || length
== 8);
383 uint64_t i
= ebml_parse_uint(data
, length
);
385 return av_int2flt(i
);
387 return av_int2dbl(i
);
391 // target must be initialized to zero
392 static void ebml_parse_element(struct ebml_parse_ctx
*ctx
, void *target
,
393 uint8_t *data
, int size
,
394 const struct ebml_elem_desc
*type
, int level
)
396 assert(type
->type
== EBML_TYPE_SUBELEMENTS
);
398 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%.*s[mkv] Parsing element %s\n",
399 level
, " ", type
->name
);
403 uint8_t *end
= data
+ size
;
405 int num_elems
[MAX_EBML_SUBELEMENTS
] = {};
408 uint32_t id
= ebml_parse_id(p
, &len
);
412 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Error parsing subelement "
417 uint64_t length
= ebml_parse_length(p
, &len
);
421 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Error parsing subelement "
428 for (int i
= 0; i
< type
->field_count
; i
++)
429 if (type
->fields
[i
].id
== id
) {
435 if (length
> end
- p
) {
436 if (field_idx
>= 0 && type
->fields
[field_idx
].desc
->type
437 != EBML_TYPE_SUBELEMENTS
) {
438 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Subelement content goes "
439 "past end of containing element\n");
442 // Try to parse what is possible from inside this partial element
443 ctx
->has_errors
= true;
451 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Subelement headers go "
452 "past end of containing element\n");
454 ctx
->has_errors
= true;
459 for (int i
= 0; i
< type
->field_count
; i
++)
460 if (num_elems
[i
] && type
->fields
[i
].multiple
) {
461 char *ptr
= s
+ type
->fields
[i
].offset
;
462 switch (type
->fields
[i
].desc
->type
) {
463 case EBML_TYPE_SUBELEMENTS
:
464 num_elems
[i
] = FFMIN(num_elems
[i
],
465 1000000000 / type
->fields
[i
].desc
->size
);
466 int size
= num_elems
[i
] * type
->fields
[i
].desc
->size
;
467 *(generic_struct
**) ptr
= talloc_zero_size(ctx
->talloc_ctx
,
471 *(uint64_t **) ptr
= talloc_zero_array(ctx
->talloc_ctx
,
472 uint64_t, num_elems
[i
]);
475 *(int64_t **) ptr
= talloc_zero_array(ctx
->talloc_ctx
,
476 int64_t, num_elems
[i
]);
478 case EBML_TYPE_FLOAT
:
479 *(double **) ptr
= talloc_zero_array(ctx
->talloc_ctx
,
480 double, num_elems
[i
]);
483 case EBML_TYPE_BINARY
:
484 *(struct bstr
**) ptr
= talloc_zero_array(ctx
->talloc_ctx
,
488 case EBML_TYPE_EBML_ID
:
489 *(int32_t **) ptr
= talloc_zero_array(ctx
->talloc_ctx
,
490 uint32_t, num_elems
[i
]);
499 uint32_t id
= ebml_parse_id(data
, &len
);
500 assert(len
>= 0 && len
<= end
- data
);
502 uint64_t length
= ebml_parse_length(data
, &len
);
503 assert(len
>= 0 && len
<= end
- data
);
505 if (length
> end
- data
) {
506 // Try to parse what is possible from inside this partial element
508 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Next subelement content goes "
509 "past end of containing element, will be truncated\n");
512 for (int i
= 0; i
< type
->field_count
; i
++)
513 if (type
->fields
[i
].id
== id
) {
519 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%.*s[mkv] Ignoring Void element "
520 "size: %"PRIu64
"\n", level
+1, " ", length
);
522 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%.*s[mkv] Ignoring CRC-32 "
523 "element size: %"PRIu64
"\n", level
+1, " ",
526 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Ignoring unrecognized "
527 "subelement. ID: %x size: %"PRIu64
"\n", id
, length
);
531 const struct ebml_field_desc
*fd
= &type
->fields
[field_idx
];
532 const struct ebml_elem_desc
*ed
= fd
->desc
;
533 bool multiple
= fd
->multiple
;
534 int *countptr
= (int *) (s
+ fd
->count_offset
);
535 if (*countptr
>= num_elems
[field_idx
]) {
536 // Shouldn't happen with on any sane file without bugs
537 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Too many subelems?\n");
538 ctx
->has_errors
= true;
542 if (*countptr
> 0 && !multiple
) {
543 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] Another subelement of type "
544 "%x %s (size: %"PRIu64
"). Only one allowed. Ignoring.\n",
545 id
, ed
->name
, length
);
546 ctx
->has_errors
= true;
550 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "%.*s[mkv] Parsing %x %s size: %"PRIu64
551 " value: ", level
+1, " ", id
, ed
->name
, length
);
553 char *fieldptr
= s
+ fd
->offset
;
555 case EBML_TYPE_SUBELEMENTS
:
556 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "subelements\n");
559 char *array_start
= (char *) *(generic_struct
**) fieldptr
;
560 subelptr
= array_start
+ *countptr
* ed
->size
;
563 ebml_parse_element(ctx
, subelptr
, data
, length
, ed
, level
+ 1);
566 case EBML_TYPE_UINT
:;
568 #define GETPTR(subelptr, fieldtype) \
570 subelptr = *(fieldtype **) fieldptr + *countptr; \
572 subelptr = (fieldtype *) fieldptr
573 GETPTR(uintptr
, uint64_t);
574 if (length
< 1 || length
> 8) {
575 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "uint invalid length %"PRIu64
579 *uintptr
= ebml_parse_uint(data
, length
);
580 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "uint %"PRIu64
"\n", *uintptr
);
583 case EBML_TYPE_SINT
:;
585 GETPTR(sintptr
, int64_t);
586 if (length
< 1 || length
> 8) {
587 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "sint invalid length %"PRIu64
591 *sintptr
= ebml_parse_sint(data
, length
);
592 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "sint %"PRId64
"\n", *sintptr
);
595 case EBML_TYPE_FLOAT
:;
597 GETPTR(floatptr
, double);
598 if (length
!= 4 && length
!= 8) {
599 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "float invalid length %"PRIu64
603 *floatptr
= ebml_parse_float(data
, length
);
604 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "float %f\n", *floatptr
);
608 case EBML_TYPE_BINARY
:;
610 GETPTR(strptr
, struct bstr
);
611 strptr
->start
= data
;
612 strptr
->len
= length
;
613 if (ed
->type
== EBML_TYPE_STR
)
614 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "string \"%.*s\"\n",
617 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "binary %zd bytes\n",
621 case EBML_TYPE_EBML_ID
:;
623 GETPTR(idptr
, uint32_t);
624 *idptr
= ebml_parse_id(data
, &len
);
626 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "ebml_id broken value\n");
629 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "ebml_id %x\n", (unsigned)*idptr
);
640 // target must be initialized to zero
641 int ebml_read_element(struct stream
*s
, struct ebml_parse_ctx
*ctx
,
642 void *target
, const struct ebml_elem_desc
*desc
)
644 ctx
->has_errors
= false;
645 int msglevel
= ctx
->no_error_messages
? MSGL_DBG2
: MSGL_WARN
;
646 uint64_t length
= ebml_read_length(s
, &ctx
->bytes_read
);
648 mp_msg(MSGT_DEMUX
, msglevel
, "[mkv] Unexpected end of file "
649 "- partial or corrupt file?\n");
652 if (length
> 1000000000) {
653 mp_msg(MSGT_DEMUX
, msglevel
, "[mkv] Refusing to read element over "
657 ctx
->talloc_ctx
= talloc_size(NULL
, length
+ 8);
658 int read_len
= stream_read(s
, ctx
->talloc_ctx
, length
);
659 ctx
->bytes_read
+= read_len
;
660 if (read_len
< length
)
661 mp_msg(MSGT_DEMUX
, msglevel
, "[mkv] Unexpected end of file "
662 "- partial or corrupt file?\n");
663 ebml_parse_element(ctx
, target
, ctx
->talloc_ctx
, read_len
, desc
, 0);
665 mp_msg(MSGT_DEMUX
, msglevel
, "[mkv] Error parsing element %s\n",