diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 6bb75457cb..685f7837ed 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -773,11 +773,7 @@ static int ebml_read_sint(AVIOContext *pb, int size, int64_t *num) if (size == 0) { *num = 0; } else { - *num = avio_r8(pb); - /* negative value */ - if (*num & 0x80) { - *num = (-1 << 8) | *num; - } + *num = sign_extend(avio_r8(pb), 8); /* big-endian ordering; build up number */ while (n++ < size) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 8db4795d8c..3f466d0eaa 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -205,22 +205,14 @@ static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val) static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val) { int i, bytes = 1; - uint64_t uval = (val < 0 ? (-val - 1) << 1 : val << 1); - while (uval>>=8) bytes++; + uint64_t tmp = 2*(val < 0 ? val^-1 : val); - /* make unsigned */ - if (val >= 0) { - uval = val; - } else { - uval = 0x80 << (bytes - 1); - uval += val; - uval |= 0x80 << (bytes - 1); - } + while (tmp>>=8) bytes++; put_ebml_id(pb, elementid); put_ebml_num(pb, bytes, 0); for (i = bytes - 1; i >= 0; i--) - avio_w8(pb, (uint8_t)(uval >> i*8)); + avio_w8(pb, (uint8_t)(val >> i*8)); } static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)