---
libavcodec/kmvc.c | 63 +++++++++++++++++++++++++++++++---------------------
1 files changed, 37 insertions(+), 26 deletions(-)
diff --git a/libavcodec/kmvc.c b/libavcodec/kmvc.c
index 07ca194..a07b07f 100644
--- a/libavcodec/kmvc.c
+++ b/libavcodec/kmvc.c
@@ -57,17 +57,17 @@ typedef struct BitBuf {
#define kmvc_init_getbits(bb, src) bb.bits = 7; bb.bitbuf = *src++;
-#define kmvc_getbit(bb, src, res) {\
+#define kmvc_getbit(bb, src, src_end, res) {\
res = 0; \
if (bb.bitbuf & (1 << bb.bits)) res = 1; \
bb.bits--; \
- if(bb.bits == -1) { \
+ if(bb.bits == -1 && src < src_end) { \
bb.bitbuf = *src++; \
bb.bits = 7; \
} \
}
-static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int
w, int h)
+static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int
src_size, int w, int h)
{
BitBuf bb;
int res, val;
@@ -75,29 +75,33 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx, const
uint8_t * src, int w,
int bx, by;
int l0x, l1x, l0y, l1y;
int mx, my;
+ const uint8_t *src_end = src + src_size;
kmvc_init_getbits(bb, src);
for (by = 0; by < h; by += 8)
for (bx = 0; bx < w; bx += 8) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) { // fill whole 8x8 block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
for (i = 0; i < 64; i++)
BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
} else { // handle four 4x4 subblocks
for (i = 0; i < 4; i++) {
l0x = bx + (i & 1) * 4;
l0y = by + (i & 2) * 2;
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) { // fill whole 4x4 block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
for (j = 0; j < 16; j++)
BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
val;
} else { // copy block from already decoded
place
- val = *src++;
+ if (src < src_end)
+ val = *src++;
mx = val & 0xF;
my = val >> 4;
for (j = 0; j < 16; j++)
@@ -108,17 +112,19 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx,
const uint8_t * src, int w,
for (j = 0; j < 4; j++) {
l1x = l0x + (j & 1) * 2;
l1y = l0y + (j & 2);
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) { // fill whole 2x2 block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
BLK(ctx->cur, l1x, l1y) = val;
BLK(ctx->cur, l1x + 1, l1y) = val;
BLK(ctx->cur, l1x, l1y + 1) = val;
BLK(ctx->cur, l1x + 1, l1y + 1) = val;
} else { // copy block from already
decoded place
- val = *src++;
+ if (src < src_end)
+ val = *src++;
mx = val & 0xF;
my = val >> 4;
BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur,
l1x - mx, l1y - my);
@@ -142,7 +148,7 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx, const
uint8_t * src, int w,
}
}
-static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int
w, int h)
+static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int
src_size, int w, int h)
{
BitBuf bb;
int res, val;
@@ -150,16 +156,18 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx,
const uint8_t * src, int w,
int bx, by;
int l0x, l1x, l0y, l1y;
int mx, my;
+ const uint8_t *src_end = src + src_size;
kmvc_init_getbits(bb, src);
for (by = 0; by < h; by += 8)
for (bx = 0; bx < w; bx += 8) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) { // fill whole 8x8 block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
for (i = 0; i < 64; i++)
BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
} else { // copy block from previous frame
@@ -171,11 +179,12 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx,
const uint8_t * src, int w,
for (i = 0; i < 4; i++) {
l0x = bx + (i & 1) * 4;
l0y = by + (i & 2) * 2;
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) { // fill whole 4x4 block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
for (j = 0; j < 16; j++)
BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
val;
} else { // copy block
@@ -190,17 +199,19 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx,
const uint8_t * src, int w,
for (j = 0; j < 4; j++) {
l1x = l0x + (j & 1) * 2;
l1y = l0y + (j & 2);
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) {
- kmvc_getbit(bb, src, res);
+ kmvc_getbit(bb, src, src_end,res);
if (!res) { // fill whole 2x2 block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
BLK(ctx->cur, l1x, l1y) = val;
BLK(ctx->cur, l1x + 1, l1y) = val;
BLK(ctx->cur, l1x, l1y + 1) = val;
BLK(ctx->cur, l1x + 1, l1y + 1) = val;
} else { // copy block
- val = *src++;
+ if (src < src_end)
+ val = *src++;
mx = (val & 0xF) - 8;
my = (val >> 4) - 8;
BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev,
l1x + mx, l1y + my);
@@ -299,10 +310,10 @@ static int decode_frame(AVCodecContext * avctx, void
*data, int *data_size, AVPa
memcpy(ctx->cur, ctx->prev, 320 * 200);
break;
case 3:
- kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
+ kmvc_decode_intra_8x8(ctx, buf, buf_size, avctx->width, avctx->height);
break;
case 4:
- kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
+ kmvc_decode_inter_8x8(ctx, buf, buf_size, avctx->width, avctx->height);
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header
& KMVC_METHOD);
--
1.7.1
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel