/*
* Quicktime Video (RPZA) Video Decoder
- * Copyright (C) 2003 the ffmpeg project
+ * Copyright (C) 2003 The FFmpeg project
*
* This file is part of Libav.
*
* pixels shall be stored in native CPU endianness.
*/
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
GetByteContext gb;
} RpzaContext;
-#define ADVANCE_BLOCK() \
-{ \
- pixel_ptr += 4; \
- if (pixel_ptr >= width) \
- { \
- pixel_ptr = 0; \
- row_ptr += stride * 4; \
- } \
- total_blocks--; \
- if (total_blocks < 0) \
- { \
- av_log(s->avctx, AV_LOG_ERROR, "warning: block counter just went negative (this should not happen)\n"); \
- return; \
- } \
-}
+#define CHECK_BLOCK() \
+ if (total_blocks < 1) { \
+ av_log(s->avctx, AV_LOG_ERROR, \
+ "Block counter just went negative (this should not happen)\n"); \
+ return AVERROR_INVALIDDATA; \
+ } \
+
+#define ADVANCE_BLOCK() \
+ { \
+ pixel_ptr += 4; \
+ if (pixel_ptr >= width) \
+ { \
+ pixel_ptr = 0; \
+ row_ptr += stride * 4; \
+ } \
+ total_blocks--; \
+ }
-static void rpza_decode_stream(RpzaContext *s)
+static int rpza_decode_stream(RpzaContext *s)
{
int width = s->avctx->width;
int stride = s->frame->linesize[0] / 2;
int row_inc = stride - 4;
int chunk_size;
- unsigned char opcode;
- int n_blocks;
- unsigned short colorA = 0, colorB;
- unsigned short color4[4];
- unsigned char index, idx;
- unsigned short ta, tb;
- unsigned short *pixels = (unsigned short *)s->frame->data[0];
+ uint16_t colorA = 0, colorB;
+ uint16_t color4[4];
+ uint16_t ta, tb;
+ uint16_t *pixels = (uint16_t *)s->frame->data[0];
int row_ptr = 0;
int pixel_ptr = 0;
av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0xe1\n",
bytestream2_peek_byte(&s->gb));
- /* Get chunk size, ingnoring first byte */
+ /* Get chunk size, ignoring first byte */
chunk_size = bytestream2_get_be32(&s->gb) & 0x00FFFFFF;
/* If length mismatch use size from MOV file and try to decode anyway */
/* Process chunk data */
while (bytestream2_get_bytes_left(&s->gb)) {
- opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
+ uint8_t opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
- n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
+ int n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
/* If opcode MSbit is 0, we need more data to decide what to do */
if ((opcode & 0x80) == 0) {
}
}
+ n_blocks = FFMIN(n_blocks, total_blocks);
+
switch (opcode & 0xe0) {
/* Skip blocks */
case 0x80:
while (n_blocks--) {
- ADVANCE_BLOCK();
+ CHECK_BLOCK();
+ ADVANCE_BLOCK();
}
break;
case 0xa0:
colorA = bytestream2_get_be16(&s->gb);
while (n_blocks--) {
+ CHECK_BLOCK();
block_ptr = row_ptr + pixel_ptr;
for (pixel_y = 0; pixel_y < 4; pixel_y++) {
for (pixel_x = 0; pixel_x < 4; pixel_x++){
color4[2] |= ((21 * ta + 11 * tb) >> 5);
if (bytestream2_get_bytes_left(&s->gb) < n_blocks * 4)
- return;
+ return AVERROR_INVALIDDATA;
while (n_blocks--) {
+ CHECK_BLOCK();
block_ptr = row_ptr + pixel_ptr;
for (pixel_y = 0; pixel_y < 4; pixel_y++) {
- index = bytestream2_get_byteu(&s->gb);
+ uint8_t index = bytestream2_get_byteu(&s->gb);
for (pixel_x = 0; pixel_x < 4; pixel_x++){
- idx = (index >> (2 * (3 - pixel_x))) & 0x03;
+ uint8_t idx = (index >> (2 * (3 - pixel_x))) & 0x03;
pixels[block_ptr] = color4[idx];
block_ptr++;
}
/* Fill block with 16 colors */
case 0x00:
if (bytestream2_get_bytes_left(&s->gb) < 30)
- return;
+ return AVERROR_INVALIDDATA;
+ CHECK_BLOCK();
block_ptr = row_ptr + pixel_ptr;
for (pixel_y = 0; pixel_y < 4; pixel_y++) {
for (pixel_x = 0; pixel_x < 4; pixel_x++){
av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
" Skip remaining %d bytes of chunk data.\n", opcode,
bytestream2_get_bytes_left(&s->gb));
- return;
+ return AVERROR_INVALIDDATA;
} /* Opcode switch */
}
+
+ return 0;
}
static av_cold int rpza_decode_init(AVCodecContext *avctx)
return ret;
}
- rpza_decode_stream(s);
+ ret = rpza_decode_stream(s);
+ if (ret < 0)
+ return ret;
if ((ret = av_frame_ref(data, s->frame)) < 0)
return ret;
.init = rpza_decode_init,
.close = rpza_decode_end,
.decode = rpza_decode_frame,
- .capabilities = CODEC_CAP_DR1,
+ .capabilities = AV_CODEC_CAP_DR1,
};