76 #define PALETTE_COUNT 256
77 #define VQA_HEADER_SIZE 0x2A
81 #define MAX_CODEBOOK_VECTORS 0xFF00
82 #define SOLID_PIXEL_VECTORS 0x100
83 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
84 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
86 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
87 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
88 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
89 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
90 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
91 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
92 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
124 int i, j, codebook_index, ret;
189 codebook_index = 0xFF00 * 16;
190 for (i = 0; i < 256; i++)
191 for (j = 0; j < 16; j++)
194 codebook_index = 0xF00 * 8;
195 for (i = 0; i < 256; i++)
196 for (j = 0; j < 8; j++)
209 #define CHECK_COUNT() \
210 if (dest_index + count > dest_size) { \
211 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
212 av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
213 dest_index, count, dest_size); \
214 return AVERROR_INVALIDDATA; \
217 #define CHECK_COPY(idx) \
218 if (idx < 0 || idx + count > dest_size) { \
219 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
220 av_log(NULL, AV_LOG_ERROR, " VQA video: current src_pos = %d, count = %d, dest_size = %d\n", \
221 src_pos, count, dest_size); \
222 return AVERROR_INVALIDDATA; \
227 unsigned char *dest,
int dest_size,
int check_size) {
230 int count, opcode, start;
237 opcode = bytestream2_get_byte(gb);
244 if (dest_index >= dest_size) {
245 av_log(
NULL,
AV_LOG_ERROR,
" VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
246 dest_index, dest_size);
250 if (opcode == 0xFF) {
252 count = bytestream2_get_le16(gb);
253 src_pos = bytestream2_get_le16(gb);
254 av_dlog(
NULL,
"(1) copy %X bytes from absolute pos %X\n", count, src_pos);
257 for (i = 0; i < count; i++)
258 dest[dest_index + i] = dest[src_pos + i];
261 }
else if (opcode == 0xFE) {
263 count = bytestream2_get_le16(gb);
264 color = bytestream2_get_byte(gb);
265 av_dlog(
NULL,
"(2) set %X bytes to %02X\n", count, color);
267 memset(&dest[dest_index], color, count);
270 }
else if ((opcode & 0xC0) == 0xC0) {
272 count = (opcode & 0x3F) + 3;
273 src_pos = bytestream2_get_le16(gb);
274 av_dlog(
NULL,
"(3) copy %X bytes from absolute pos %X\n", count, src_pos);
277 for (i = 0; i < count; i++)
278 dest[dest_index + i] = dest[src_pos + i];
281 }
else if (opcode > 0x80) {
283 count = opcode & 0x3F;
284 av_dlog(
NULL,
"(4) copy %X bytes from source to dest\n", count);
291 count = ((opcode & 0x70) >> 4) + 3;
292 src_pos = bytestream2_get_byte(gb) | ((opcode & 0x0F) << 8);
293 av_dlog(
NULL,
"(5) copy %X bytes from relpos %X\n", count, src_pos);
296 for (i = 0; i < count; i++)
297 dest[dest_index + i] = dest[dest_index - src_pos + i];
307 if (dest_index < dest_size)
308 av_log(
NULL,
AV_LOG_ERROR,
" VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
309 dest_index, dest_size);
316 unsigned int chunk_type;
317 unsigned int chunk_size;
319 unsigned int index = 0;
321 unsigned char r,
g,
b;
336 int vector_index = 0;
345 chunk_type = bytestream2_get_be32u(&s->
gb);
347 chunk_size = bytestream2_get_be32u(&s->
gb);
349 switch (chunk_type) {
381 (chunk_type >> 24) & 0xFF,
382 (chunk_type >> 16) & 0xFF,
383 (chunk_type >> 8) & 0xFF,
384 (chunk_type >> 0) & 0xFF,
389 byte_skip = chunk_size & 0x01;
394 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
402 if (cplz_chunk != -1) {
409 if (cpl0_chunk != -1) {
412 chunk_size = bytestream2_get_be32(&s->
gb);
419 for (i = 0; i < chunk_size / 3; i++) {
421 r = bytestream2_get_byteu(&s->
gb) * 4;
422 g = bytestream2_get_byteu(&s->
gb) * 4;
423 b = bytestream2_get_byteu(&s->
gb) * 4;
424 s->
palette[i] = (r << 16) | (g << 8) | (
b);
429 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
437 if (cbfz_chunk != -1) {
440 chunk_size = bytestream2_get_be32(&s->
gb);
447 if (cbf0_chunk != -1) {
450 chunk_size = bytestream2_get_be32(&s->
gb);
462 if (vptz_chunk == -1) {
470 chunk_size = bytestream2_get_be32(&s->
gb);
481 for (x = 0; x < s->
width; x += 4, lobytes++, hibytes++) {
482 pixel_ptr = y * frame->
linesize[0] + x;
491 vector_index = ((hibyte << 8) | lobyte) >> 3;
492 vector_index <<= index_shift;
495 if (hibyte == 0xFF) {
497 frame->
data[0][pixel_ptr + 0] = 255 - lobyte;
498 frame->
data[0][pixel_ptr + 1] = 255 - lobyte;
499 frame->
data[0][pixel_ptr + 2] = 255 - lobyte;
500 frame->
data[0][pixel_ptr + 3] = 255 - lobyte;
510 vector_index = (hibyte << 8) | lobyte;
511 vector_index <<= index_shift;
522 frame->
data[0][pixel_ptr + 0] = s->
codebook[vector_index++];
523 frame->
data[0][pixel_ptr + 1] = s->
codebook[vector_index++];
524 frame->
data[0][pixel_ptr + 2] = s->
codebook[vector_index++];
525 frame->
data[0][pixel_ptr + 3] = s->
codebook[vector_index++];
532 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
538 if (cbp0_chunk != -1) {
541 chunk_size = bytestream2_get_be32(&s->
gb);
567 if (cbpz_chunk != -1) {
570 chunk_size = bytestream2_get_be32(&s->
gb);
603 void *
data,
int *got_frame,