summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2023-09-06 02:27:56 +0200
committerPaul B Mahol <onemda@gmail.com>2023-09-06 15:17:12 +0200
commitd33c630b2a84365b465e66053f00d5660a3e22a3 (patch)
tree79b68b58825a862d394f4e9383e81e28dabaf863
parentbfa43447fae18a4ccc1c3c2af88bb676b24d0872 (diff)
avcodec/yuv4enc: do not read past end of input in case of odd height
-rw-r--r--libavcodec/yuv4enc.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/libavcodec/yuv4enc.c b/libavcodec/yuv4enc.c
index 8123260d5d..2a9d3442ca 100644
--- a/libavcodec/yuv4enc.c
+++ b/libavcodec/yuv4enc.c
@@ -29,10 +29,10 @@ static int yuv4_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
{
uint8_t *dst;
const uint8_t *y, *u, *v;
- int i, j, ret;
+ int ret;
- ret = ff_get_encode_buffer(avctx, pkt, 6 * (avctx->width + 1 >> 1)
- * (avctx->height + 1 >> 1), 0);
+ ret = ff_get_encode_buffer(avctx, pkt, 6 * ((avctx->width + 1) / 2)
+ * ((avctx->height + 1) / 2), 0);
if (ret < 0)
return ret;
dst = pkt->data;
@@ -41,8 +41,8 @@ static int yuv4_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
u = pic->data[1];
v = pic->data[2];
- for (i = 0; i < avctx->height + 1 >> 1; i++) {
- for (j = 0; j < avctx->width + 1 >> 1; j++) {
+ for (int i = 0; i < avctx->height / 2; i++) {
+ for (int j = 0; j < (avctx->width + 1) / 2; j++) {
*dst++ = u[j] ^ 0x80;
*dst++ = v[j] ^ 0x80;
*dst++ = y[ 2 * j ];
@@ -55,6 +55,17 @@ static int yuv4_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
v += pic->linesize[2];
}
+ if (avctx->height & 1) {
+ for (int j = 0; j < (avctx->width + 1) / 2; j++) {
+ *dst++ = u[j] ^ 0x80;
+ *dst++ = v[j] ^ 0x80;
+ *dst++ = y[2 * j ];
+ *dst++ = y[2 * j + 1];
+ *dst++ = y[2 * j ];
+ *dst++ = y[2 * j + 1];
+ }
+ }
+
*got_packet = 1;
return 0;
}