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;
136 s->vqa_version =
s->avctx->extradata[0];
137 switch (
s->vqa_version) {
148 s->width =
AV_RL16(&
s->avctx->extradata[6]);
149 s->height =
AV_RL16(&
s->avctx->extradata[8]);
151 s->width=
s->height= 0;
154 s->vector_width =
s->avctx->extradata[10];
155 s->vector_height =
s->avctx->extradata[11];
156 s->partial_count =
s->partial_countdown =
s->avctx->extradata[13];
159 if ((
s->vector_width != 4) ||
160 ((
s->vector_height != 2) && (
s->vector_height != 4))) {
165 if (
s->width %
s->vector_width ||
s->height %
s->vector_height) {
175 s->next_codebook_buffer =
av_malloc(
s->codebook_size);
176 if (!
s->next_codebook_buffer)
180 s->decode_buffer_size = (
s->width /
s->vector_width) *
181 (
s->height /
s->vector_height) * 2;
183 if (!
s->decode_buffer)
187 if (
s->vector_height == 4) {
188 codebook_index = 0xFF00 * 16;
189 for (
i = 0;
i < 256;
i++)
190 for (j = 0; j < 16; j++)
191 s->codebook[codebook_index++] =
i;
193 codebook_index = 0xF00 * 8;
194 for (
i = 0;
i < 256;
i++)
195 for (j = 0; j < 8; j++)
196 s->codebook[codebook_index++] =
i;
198 s->next_codebook_buffer_index = 0;
208 #define CHECK_COUNT() \
209 if (dest_index + count > dest_size) { \
210 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
211 av_log(s->avctx, AV_LOG_ERROR, "current dest_index = %d, count = %d, dest_size = %d\n", \
212 dest_index, count, dest_size); \
213 return AVERROR_INVALIDDATA; \
216 #define CHECK_COPY(idx) \
217 if (idx < 0 || idx + count > dest_size) { \
218 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
219 av_log(s->avctx, AV_LOG_ERROR, "current src_pos = %d, count = %d, dest_size = %d\n", \
220 src_pos, count, dest_size); \
221 return AVERROR_INVALIDDATA; \
226 unsigned char *dest,
int dest_size,
int check_size) {
242 opcode = bytestream2_get_byte(&
s->gb);
243 ff_tlog(
s->avctx,
"opcode %02X: ", opcode);
249 if (dest_index >= dest_size) {
250 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
251 dest_index, dest_size);
255 if (opcode == 0xFF) {
257 count = bytestream2_get_le16(&
s->gb);
258 src_pos = bytestream2_get_le16(&
s->gb);
259 ff_tlog(
s->avctx,
"(1) copy %X bytes from absolute pos %X\n",
count, src_pos);
263 dest[dest_index +
i] = dest[src_pos +
i];
266 }
else if (opcode == 0xFE) {
268 count = bytestream2_get_le16(&
s->gb);
269 color = bytestream2_get_byte(&
s->gb);
275 }
else if ((opcode & 0xC0) == 0xC0) {
277 count = (opcode & 0x3F) + 3;
278 src_pos = bytestream2_get_le16(&
s->gb);
279 ff_tlog(
s->avctx,
"(3) copy %X bytes from absolute pos %X\n",
count, src_pos);
283 dest[dest_index +
i] = dest[src_pos +
i];
286 }
else if (opcode > 0x80) {
288 count = opcode & 0x3F;
289 ff_tlog(
s->avctx,
"(4) copy %X bytes from source to dest\n",
count);
296 count = ((opcode & 0x70) >> 4) + 3;
297 src_pos = bytestream2_get_byte(&
s->gb) | ((opcode & 0x0F) << 8);
298 ff_tlog(
s->avctx,
"(5) copy %X bytes from relpos %X\n",
count, src_pos);
302 dest[dest_index +
i] = dest[dest_index - src_pos +
i];
312 if (dest_index < dest_size) {
313 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
314 dest_index, dest_size);
315 memset(dest + dest_index, 0, dest_size - dest_index);
323 unsigned int chunk_type;
324 unsigned int chunk_size;
326 unsigned int index = 0;
328 unsigned char r,
g,
b;
343 int vector_index = 0;
347 int hibytes =
s->decode_buffer_size / 2;
352 chunk_type = bytestream2_get_be32u(&
s->gb);
354 chunk_size = bytestream2_get_be32u(&
s->gb);
356 switch (chunk_type) {
392 byte_skip = chunk_size & 0x01;
397 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
405 if (cplz_chunk != -1) {
412 if (cpl0_chunk != -1) {
415 chunk_size = bytestream2_get_be32(&
s->gb);
422 for (
i = 0;
i < chunk_size / 3;
i++) {
424 r = bytestream2_get_byteu(&
s->gb) * 4;
425 g = bytestream2_get_byteu(&
s->gb) * 4;
426 b = bytestream2_get_byteu(&
s->gb) * 4;
427 s->palette[
i] = 0xFF
U << 24 |
r << 16 |
g << 8 |
b;
428 s->palette[
i] |=
s->palette[
i] >> 6 & 0x30303;
433 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
441 if (cbfz_chunk != -1) {
444 chunk_size = bytestream2_get_be32(&
s->gb);
446 s->codebook_size, 0)) < 0)
451 if (cbf0_chunk != -1) {
454 chunk_size = bytestream2_get_be32(&
s->gb);
466 if (vptz_chunk == -1) {
474 chunk_size = bytestream2_get_be32(&
s->gb);
476 s->decode_buffer,
s->decode_buffer_size, 1)) < 0)
480 if (
s->vector_height == 4)
484 for (y = 0; y <
s->height; y +=
s->vector_height) {
485 for (x = 0; x <
s->width; x += 4, lobytes++, hibytes++) {
486 pixel_ptr = y *
frame->linesize[0] + x;
490 switch (
s->vqa_version) {
493 lobyte =
s->decode_buffer[lobytes * 2];
494 hibyte =
s->decode_buffer[(lobytes * 2) + 1];
495 vector_index = ((hibyte << 8) | lobyte) >> 3;
496 vector_index <<= index_shift;
497 lines =
s->vector_height;
499 if (hibyte == 0xFF) {
501 frame->data[0][pixel_ptr + 0] = 255 - lobyte;
502 frame->data[0][pixel_ptr + 1] = 255 - lobyte;
503 frame->data[0][pixel_ptr + 2] = 255 - lobyte;
504 frame->data[0][pixel_ptr + 3] = 255 - lobyte;
505 pixel_ptr +=
frame->linesize[0];
512 lobyte =
s->decode_buffer[lobytes];
513 hibyte =
s->decode_buffer[hibytes];
514 vector_index = (hibyte << 8) | lobyte;
515 vector_index <<= index_shift;
516 lines =
s->vector_height;
526 frame->data[0][pixel_ptr + 0] =
s->codebook[vector_index++];
527 frame->data[0][pixel_ptr + 1] =
s->codebook[vector_index++];
528 frame->data[0][pixel_ptr + 2] =
s->codebook[vector_index++];
529 frame->data[0][pixel_ptr + 3] =
s->codebook[vector_index++];
530 pixel_ptr +=
frame->linesize[0];
536 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
542 if (cbp0_chunk != -1) {
545 chunk_size = bytestream2_get_be32(&
s->gb);
556 s->next_codebook_buffer_index += chunk_size;
558 s->partial_countdown--;
559 if (
s->partial_countdown <= 0) {
562 memcpy(
s->codebook,
s->next_codebook_buffer,
563 s->next_codebook_buffer_index);
566 s->next_codebook_buffer_index = 0;
567 s->partial_countdown =
s->partial_count;
571 if (cbpz_chunk != -1) {
574 chunk_size = bytestream2_get_be32(&
s->gb);
585 s->next_codebook_buffer_index += chunk_size;
587 s->partial_countdown--;
588 if (
s->partial_countdown <= 0) {
592 s->codebook,
s->codebook_size, 0);
595 s->next_codebook_buffer_index = 0;
596 s->partial_countdown =
s->partial_count;
606 void *
data,
int *got_frame,
622 frame->palette_has_changed = 1;