# HG changeset patch # User conrad # Date 1188951871 0 # Node ID 158f2c1999564d4338577d8edfcba5f5949034c4 # Parent 13940660643778b76c98d2ea401b5903aafe5d40 Correct handling of smaller unknown sizes diff -r 139406606437 -r 158f2c199956 matroskaenc.c --- a/matroskaenc.c Wed Sep 05 00:24:29 2007 +0000 +++ b/matroskaenc.c Wed Sep 05 00:24:31 2007 +0000 @@ -83,6 +83,23 @@ return (av_log2(id+1)-1)/7+1; } +/** + * Write an EBML size meaning "unknown size" + * + * @param bytes The number of bytes the size should occupy. Maximum of 8. + */ +static void put_ebml_size_unknown(ByteIOContext *pb, int bytes) +{ + uint64_t value = 0; + int i; + + bytes = FFMIN(bytes, 8); + for (i = 0; i < bytes*7 + 1; i++) + value |= 1ULL << i; + for (i = bytes-1; i >= 0; i--) + put_byte(pb, value >> i*8); +} + // XXX: test this thoroughly and get rid of minbytes hack (currently needed to // use up all of the space reserved in start_ebml_master) static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes) @@ -91,9 +108,12 @@ // sizes larger than this are currently undefined in EBML // so write "unknown" size - size = FFMIN(size, (1ULL<<56)-1); + if (size >= (1ULL<<56)-1) { + put_ebml_size_unknown(pb, 1); + return; + } - while (size >> (bytes*7 + 7)) bytes++; + while ((size+1) >> (bytes*7 + 7)) bytes++; put_byte(pb, (0x80 >> bytes) | (size >> bytes*8)); for (bytes -= 1; bytes >= 0; bytes--) @@ -159,7 +179,7 @@ put_ebml_id(pb, elementid); // XXX: this always reserves the maximum needed space to store any size value // we should be smarter (additional parameter for expected size?) - put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size + put_ebml_size_unknown(pb, 8); return url_ftell(pb); }