[PATCH] Simplify decode_format80 function
Adam Iglewski
adam.iglewski
Sat May 9 01:57:21 CEST 2009
---
libavcodec/vqavideo.c | 121 ++++++++++++++++---------------------------------
1 files changed, 40 insertions(+), 81 deletions(-)
diff --git a/libavcodec/vqavideo.c b/libavcodec/vqavideo.c
index 67dd7de..9646d67 100644
--- a/libavcodec/vqavideo.c
+++ b/libavcodec/vqavideo.c
@@ -199,101 +199,61 @@ static av_cold int vqa_decode_init(AVCodecContext *avctx)
return 0;
}
-#define CHECK_COUNT() \
- if (dest_index + count > dest_size) { \
- av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
- av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
- dest_index, count, dest_size); \
+#define CHECK_PTR() \
+ if (dest + count > dest_end) { \
+ av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest buffer\n"); \
return; \
}
static void decode_format80(const unsigned char *src, int src_size,
unsigned char *dest, int dest_size, int check_size) {
- int src_index = 0;
- int dest_index = 0;
+ const unsigned char *from,*src_end = src+src_size;
+ unsigned char *dest_end = dest+dest_size;
int count;
int src_pos;
- unsigned char color;
- int i;
+ unsigned char code;
int new_format = !*src || src_size > 0xffff;
if (new_format)
- src_index++;
+ src++;
- while (src_index < src_size) {
+ while (src < src_end) {
- vqa_debug(" opcode %02X: ", src[src_index]);
+ code = *src++;
/* 0x80 means that frame is finished */
- if (src[src_index] == 0x80)
+ if (code == 0x80)
return;
- if (dest_index >= dest_size) {
- av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
- dest_index, dest_size);
- return;
- }
-
- if (src[src_index] == 0xFF) {
-
- src_index++;
- count = AV_RL16(&src[src_index]);
- src_index += 2;
- src_pos = AV_RL16(&src[src_index]);
- if(new_format)
- src_pos = dest_index-src_pos;
- src_index += 2;
- vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
- CHECK_COUNT();
- for (i = 0; i < count; i++)
- dest[dest_index + i] = dest[src_pos + i];
- dest_index += count;
-
- } else if (src[src_index] == 0xFE) {
-
- src_index++;
- count = AV_RL16(&src[src_index]);
- src_index += 2;
- color = src[src_index++];
- vqa_debug("(2) set %X bytes to %02X\n", count, color);
- CHECK_COUNT();
- memset(&dest[dest_index], color, count);
- dest_index += count;
-
- } else if ((src[src_index] & 0xC0) == 0xC0) {
-
- count = (src[src_index++] & 0x3F) + 3;
- src_pos = AV_RL16(&src[src_index]);
- if(new_format)
- src_pos = dest_index-src_pos;
- src_index += 2;
- vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
- CHECK_COUNT();
- for (i = 0; i < count; i++)
- dest[dest_index + i] = dest[src_pos + i];
- dest_index += count;
-
- } else if (src[src_index] > 0x80) {
-
- count = src[src_index++] & 0x3F;
- vqa_debug("(4) copy %X bytes from source to dest\n", count);
- CHECK_COUNT();
- memcpy(&dest[dest_index], &src[src_index], count);
- src_index += count;
- dest_index += count;
-
- } else {
-
- count = ((src[src_index] & 0x70) >> 4) + 3;
- src_pos = AV_RB16(&src[src_index]) & 0x0FFF;
- src_index += 2;
- vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
- CHECK_COUNT();
- for (i = 0; i < count; i++)
- dest[dest_index + i] = dest[dest_index - src_pos + i];
- dest_index += count;
- }
+ if(code == 0xFE) {
+ count = bytestream_get_le16(&src);
+ CHECK_PTR()
+ memset(dest, *src++, count);
+ dest += count;
+ continue;
+ } else if((code == 0xFF) || ((code & 0xC0) == 0xC0)) {
+ if(code == 0xFF)
+ count = bytestream_get_le16(&src);
+ else
+ count = (code & 0x3F) + 3;
+ src_pos = bytestream_get_le16(&src);
+ if(new_format)
+ from = dest-src_pos;
+ else
+ from = dest_end-dest_size+src_pos;
+ } else if (code > 0x80) {
+ count = (code & 0x3F);
+ from = src;
+ src += count;
+ } else {
+ count = ((code & 0x70) >> 4) + 3;
+ src_pos = ((code & 0x0F) << 8) | *src++;
+ from = dest-src_pos;
+ }
+ CHECK_PTR()
+ while(count--)
+ *dest++ = *from++;
}
/* validate that the entire destination buffer was filled; this is
@@ -301,9 +261,8 @@ static void decode_format80(const unsigned char *src, int src_size,
* codebook entry; it is not important for compressed codebooks because
* not every entry needs to be filled */
if (check_size)
- if (dest_index < dest_size)
- av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
- dest_index, dest_size);
+ if (dest < dest_end)
+ av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest < dest_end\n");
}
static inline void vqa_copy_hc_block(uint16_t *pixels,int stride,const uint16_t *codebook,
--
1.6.0.4
--------------000406070301060204010203
Content-Type: text/x-diff;
name="0006-Simplify-vqa_decode_chunk-part-I.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="0006-Simplify-vqa_decode_chunk-part-I.patch"
More information about the ffmpeg-devel
mailing list