82 #define PALETTE_COUNT 256
83 #define VQA_HEADER_SIZE 0x2A
87 #define MAX_CODEBOOK_VECTORS 0xFF00
88 #define SOLID_PIXEL_VECTORS 0x100
89 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
90 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4 * sizeof(uint16_t))
92 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
93 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
94 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
95 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
96 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
97 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
98 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
99 #define VPTR_TAG MKBETAG('V', 'P', 'T', 'R')
100 #define VPRZ_TAG MKBETAG('V', 'P', 'R', 'Z')
131 int i, j, codebook_index,
ret;
143 s->vqa_version =
s->avctx->extradata[0];
145 if (
s->vqa_version < 1 ||
s->vqa_version > 3) {
150 s->width =
AV_RL16(&
s->avctx->extradata[6]);
151 s->height =
AV_RL16(&
s->avctx->extradata[8]);
153 s->width=
s->height= 0;
156 s->vector_width =
s->avctx->extradata[10];
157 s->vector_height =
s->avctx->extradata[11];
158 s->partial_count =
s->partial_countdown =
s->avctx->extradata[13];
160 colors = (
s->avctx->extradata[14] << 8) |
s->avctx->extradata[15];
169 if ((
s->vector_width != 4) ||
170 ((
s->vector_height != 2) && (
s->vector_height != 4))) {
175 if (
s->width %
s->vector_width ||
s->height %
s->vector_height) {
189 s->next_codebook_buffer =
av_malloc(
s->codebook_size);
190 if (!
s->next_codebook_buffer)
194 s->decode_buffer_size = (
s->width /
s->vector_width) *
195 (
s->height /
s->vector_height) * 2;
197 if (!
s->decode_buffer)
201 if (
s->vector_height == 4) {
202 codebook_index = 0xFF00 * 16;
203 for (
i = 0;
i < 256;
i++)
204 for (j = 0; j < 16; j++)
205 s->codebook[codebook_index++] =
i;
207 codebook_index = 0xF00 * 8;
208 for (
i = 0;
i < 256;
i++)
209 for (j = 0; j < 8; j++)
210 s->codebook[codebook_index++] =
i;
212 s->next_codebook_buffer_index = 0;
217 #define CHECK_COUNT() \
218 if (dest_index + count > dest_size) { \
219 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
220 av_log(s->avctx, AV_LOG_ERROR, "current dest_index = %d, count = %d, dest_size = %d\n", \
221 dest_index, count, dest_size); \
222 return AVERROR_INVALIDDATA; \
225 #define CHECK_COPY(idx) \
226 if (idx < 0 || idx + count > dest_size) { \
227 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
228 av_log(s->avctx, AV_LOG_ERROR, "current src_pos = %d, count = %d, dest_size = %d\n", \
229 src_pos, count, dest_size); \
230 return AVERROR_INVALIDDATA; \
235 unsigned char *dest,
int dest_size,
int check_size) {
238 int count, opcode, start;
251 if (bytestream2_peek_byte(&
s->gb) == 0x00) {
253 bytestream2_get_byte(&
s->gb);
254 ff_tlog(
s->avctx,
"found new format stream ");
259 opcode = bytestream2_get_byte(&
s->gb);
260 ff_tlog(
s->avctx,
"opcode %02X: ", opcode);
266 if (dest_index >= dest_size) {
267 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
268 dest_index, dest_size);
272 if (opcode == 0xFF) {
274 count = bytestream2_get_le16(&
s->gb);
275 src_pos = bytestream2_get_le16(&
s->gb);
277 src_pos = dest_index - src_pos;
278 ff_tlog(
s->avctx,
"(1) copy %X bytes from pos %X\n", count, src_pos);
281 for (
i = 0;
i < count;
i++)
282 dest[dest_index +
i] = dest[src_pos +
i];
285 }
else if (opcode == 0xFE) {
287 count = bytestream2_get_le16(&
s->gb);
288 color = bytestream2_get_byte(&
s->gb);
289 ff_tlog(
s->avctx,
"(2) set %X bytes to %02X\n", count,
color);
291 memset(&dest[dest_index],
color, count);
294 }
else if ((opcode & 0xC0) == 0xC0) {
296 count = (opcode & 0x3F) + 3;
297 src_pos = bytestream2_get_le16(&
s->gb);
299 src_pos = dest_index - src_pos;
300 ff_tlog(
s->avctx,
"(3) copy %X bytes from pos %X\n", count, src_pos);
303 for (
i = 0;
i < count;
i++)
304 dest[dest_index +
i] = dest[src_pos +
i];
307 }
else if (opcode > 0x80) {
309 count = opcode & 0x3F;
310 ff_tlog(
s->avctx,
"(4) copy %X bytes from source to dest\n", count);
317 count = ((opcode & 0x70) >> 4) + 3;
318 src_pos = bytestream2_get_byte(&
s->gb) | ((opcode & 0x0F) << 8);
319 ff_tlog(
s->avctx,
"(5) copy %X bytes from relpos %X\n", count, src_pos);
322 for (
i = 0;
i < count;
i++)
323 dest[dest_index +
i] = dest[dest_index - src_pos +
i];
333 if (dest_index < dest_size) {
334 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
335 dest_index, dest_size);
336 memset(dest + dest_index, 0, dest_size - dest_index);
344 unsigned int chunk_type;
345 unsigned int chunk_size;
347 unsigned int index = 0;
349 unsigned char r,
g,
b;
364 int vector_index = 0;
368 int hibytes =
s->decode_buffer_size / 2;
373 chunk_type = bytestream2_get_be32u(&
s->gb);
375 chunk_size = bytestream2_get_be32u(&
s->gb);
377 switch (chunk_type) {
413 byte_skip = chunk_size & 0x01;
418 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
426 if (cplz_chunk != -1) {
433 if (cpl0_chunk != -1) {
436 chunk_size = bytestream2_get_be32(&
s->gb);
443 for (
i = 0;
i < chunk_size / 3;
i++) {
445 r = bytestream2_get_byteu(&
s->gb) * 4;
446 g = bytestream2_get_byteu(&
s->gb) * 4;
447 b = bytestream2_get_byteu(&
s->gb) * 4;
448 s->palette[
i] = 0xFF
U << 24 |
r << 16 |
g << 8 |
b;
449 s->palette[
i] |=
s->palette[
i] >> 6 & 0x30303;
454 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
462 if (cbfz_chunk != -1) {
465 chunk_size = bytestream2_get_be32(&
s->gb);
467 s->codebook_size, 0)) < 0)
472 if (cbf0_chunk != -1) {
475 chunk_size = bytestream2_get_be32(&
s->gb);
487 if (vptz_chunk == -1) {
495 chunk_size = bytestream2_get_be32(&
s->gb);
497 s->decode_buffer,
s->decode_buffer_size, 1)) < 0)
501 if (
s->vector_height == 4)
505 for (y = 0; y <
s->height; y +=
s->vector_height) {
506 for (x = 0; x <
s->width; x += 4, lobytes++, hibytes++) {
507 pixel_ptr = y *
frame->linesize[0] + x;
511 switch (
s->vqa_version) {
514 lobyte =
s->decode_buffer[lobytes * 2];
515 hibyte =
s->decode_buffer[(lobytes * 2) + 1];
516 vector_index = ((hibyte << 8) | lobyte) >> 3;
517 vector_index <<= index_shift;
518 lines =
s->vector_height;
520 if (hibyte == 0xFF) {
522 frame->data[0][pixel_ptr + 0] = 255 - lobyte;
523 frame->data[0][pixel_ptr + 1] = 255 - lobyte;
524 frame->data[0][pixel_ptr + 2] = 255 - lobyte;
525 frame->data[0][pixel_ptr + 3] = 255 - lobyte;
526 pixel_ptr +=
frame->linesize[0];
533 lobyte =
s->decode_buffer[lobytes];
534 hibyte =
s->decode_buffer[hibytes];
535 vector_index = (hibyte << 8) | lobyte;
536 vector_index <<= index_shift;
537 lines =
s->vector_height;
546 frame->data[0][pixel_ptr + 0] =
s->codebook[vector_index++];
547 frame->data[0][pixel_ptr + 1] =
s->codebook[vector_index++];
548 frame->data[0][pixel_ptr + 2] =
s->codebook[vector_index++];
549 frame->data[0][pixel_ptr + 3] =
s->codebook[vector_index++];
550 pixel_ptr +=
frame->linesize[0];
556 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
562 if (cbp0_chunk != -1) {
565 chunk_size = bytestream2_get_be32(&
s->gb);
576 s->next_codebook_buffer_index += chunk_size;
578 s->partial_countdown--;
579 if (
s->partial_countdown <= 0) {
582 memcpy(
s->codebook,
s->next_codebook_buffer,
583 s->next_codebook_buffer_index);
586 s->next_codebook_buffer_index = 0;
587 s->partial_countdown =
s->partial_count;
591 if (cbpz_chunk != -1) {
594 chunk_size = bytestream2_get_be32(&
s->gb);
605 s->next_codebook_buffer_index += chunk_size;
607 s->partial_countdown--;
608 if (
s->partial_countdown <= 0) {
612 s->codebook,
s->codebook_size, 0);
615 s->next_codebook_buffer_index = 0;
616 s->partial_countdown =
s->partial_count;
627 unsigned int chunk_type;
628 unsigned int chunk_size;
629 unsigned int index = 0;
640 chunk_type = bytestream2_get_be32u(&
s->gb);
642 chunk_size = bytestream2_get_be32u(&
s->gb);
644 switch (chunk_type) {
667 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
674 if (cbfz_chunk != -1) {
676 chunk_size = bytestream2_get_be32(&
s->gb);
678 s->codebook_size, 0)) < 0)
683 if (cbf0_chunk != -1) {
685 chunk_size = bytestream2_get_be32(&
s->gb);
698 if (vptr_chunk != -1) {
701 chunk_size = bytestream2_get_be32(&
s->gb);
702 if (chunk_size >
s->decode_buffer_size) {
707 }
else if (vprz_chunk != -1) {
711 chunk_size = bytestream2_get_be32(&
s->gb);
712 if ((res =
decode_format80(
s, chunk_size,
s->decode_buffer,
s->decode_buffer_size, 0)) < 0)
723 for (
int y_pos = 0; y_pos <
s->height; y_pos +=
s->vector_height) {
726 while (x_pos < s->
width) {
727 int vector_index = 0;
735 code = bytestream2_get_le16(&gb_stream);
743 }
else if (
type < 3) {
744 vector_index =
code & 0xff;
745 count = ((
code & 0x1f00) >> 7) + 1 +
type;
746 }
else if (
type < 5) {
749 }
else if (
type < 7) {
751 count = bytestream2_get_byte(&gb_stream);
757 if (count < 0 || count > (
s->width - x_pos) /
s->vector_width) {
762 while (count-- && x_pos < s->
width) {
763 const int bytes_per_vector = 4 *
s->vector_height *
sizeof(uint16_t);
764 unsigned char *
src =
s->codebook + vector_index * bytes_per_vector;
765 unsigned char *dst =
s->frame->data[0] + y_pos *
s->frame->linesize[0]
766 +
sizeof(uint16_t) * x_pos;
771 for (
int y = 0; y <
s->vector_height; y++) {
772 int size = 4 *
sizeof(uint16_t);
774 dst +=
s->frame->linesize[0];
779 if ((
type == 2) && count > 0) {
780 vector_index = bytestream2_get_byte(&gb_stream);
797 void *
data,
int *got_frame,
814 s->frame->palette_has_changed = 1;
845 {
"max_pixels",
"640*480" },