81 #define PALETTE_COUNT 256
82 #define VQA_HEADER_SIZE 0x2A
86 #define MAX_CODEBOOK_VECTORS 0xFF00
87 #define SOLID_PIXEL_VECTORS 0x100
88 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
89 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4 * sizeof(uint16_t))
91 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
92 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
93 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
94 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
95 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
96 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
97 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
98 #define VPTR_TAG MKBETAG('V', 'P', 'T', 'R')
99 #define VPRZ_TAG MKBETAG('V', 'P', 'R', 'Z')
130 int i, j, codebook_index,
ret;
142 s->vqa_version =
s->avctx->extradata[0];
144 if (
s->vqa_version < 1 ||
s->vqa_version > 3) {
149 s->width =
AV_RL16(&
s->avctx->extradata[6]);
150 s->height =
AV_RL16(&
s->avctx->extradata[8]);
152 s->width=
s->height= 0;
155 s->vector_width =
s->avctx->extradata[10];
156 s->vector_height =
s->avctx->extradata[11];
157 s->partial_count =
s->partial_countdown =
s->avctx->extradata[13];
159 colors = (
s->avctx->extradata[14] << 8) |
s->avctx->extradata[15];
168 if ((
s->vector_width != 4) ||
169 ((
s->vector_height != 2) && (
s->vector_height != 4))) {
174 if (
s->width %
s->vector_width ||
s->height %
s->vector_height) {
188 s->next_codebook_buffer =
av_malloc(
s->codebook_size);
189 if (!
s->next_codebook_buffer)
193 s->decode_buffer_size = (
s->width /
s->vector_width) *
194 (
s->height /
s->vector_height) * 2;
196 if (!
s->decode_buffer)
200 if (
s->vector_height == 4) {
201 codebook_index = 0xFF00 * 16;
202 for (
i = 0;
i < 256;
i++)
203 for (j = 0; j < 16; j++)
204 s->codebook[codebook_index++] =
i;
206 codebook_index = 0xF00 * 8;
207 for (
i = 0;
i < 256;
i++)
208 for (j = 0; j < 8; j++)
209 s->codebook[codebook_index++] =
i;
211 s->next_codebook_buffer_index = 0;
216 #define CHECK_COUNT() \
217 if (dest_index + 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 dest_index = %d, count = %d, dest_size = %d\n", \
220 dest_index, count, dest_size); \
221 return AVERROR_INVALIDDATA; \
224 #define CHECK_COPY(idx) \
225 if (idx < 0 || idx + count > dest_size) { \
226 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
227 av_log(s->avctx, AV_LOG_ERROR, "current src_pos = %d, count = %d, dest_size = %d\n", \
228 src_pos, count, dest_size); \
229 return AVERROR_INVALIDDATA; \
234 unsigned char *dest,
int dest_size,
int check_size) {
237 int count, opcode, start;
250 if (bytestream2_peek_byte(&
s->gb) == 0x00) {
252 bytestream2_get_byte(&
s->gb);
253 ff_tlog(
s->avctx,
"found new format stream ");
258 opcode = bytestream2_get_byte(&
s->gb);
259 ff_tlog(
s->avctx,
"opcode %02X: ", opcode);
265 if (dest_index >= dest_size) {
266 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
267 dest_index, dest_size);
271 if (opcode == 0xFF) {
273 count = bytestream2_get_le16(&
s->gb);
274 src_pos = bytestream2_get_le16(&
s->gb);
276 src_pos = dest_index - src_pos;
277 ff_tlog(
s->avctx,
"(1) copy %X bytes from pos %X\n", count, src_pos);
280 for (
i = 0;
i < count;
i++)
281 dest[dest_index +
i] = dest[src_pos +
i];
284 }
else if (opcode == 0xFE) {
286 count = bytestream2_get_le16(&
s->gb);
287 color = bytestream2_get_byte(&
s->gb);
288 ff_tlog(
s->avctx,
"(2) set %X bytes to %02X\n", count,
color);
290 memset(&dest[dest_index],
color, count);
293 }
else if ((opcode & 0xC0) == 0xC0) {
295 count = (opcode & 0x3F) + 3;
296 src_pos = bytestream2_get_le16(&
s->gb);
298 src_pos = dest_index - src_pos;
299 ff_tlog(
s->avctx,
"(3) copy %X bytes from pos %X\n", count, src_pos);
302 for (
i = 0;
i < count;
i++)
303 dest[dest_index +
i] = dest[src_pos +
i];
306 }
else if (opcode > 0x80) {
308 count = opcode & 0x3F;
309 ff_tlog(
s->avctx,
"(4) copy %X bytes from source to dest\n", count);
316 count = ((opcode & 0x70) >> 4) + 3;
317 src_pos = bytestream2_get_byte(&
s->gb) | ((opcode & 0x0F) << 8);
318 ff_tlog(
s->avctx,
"(5) copy %X bytes from relpos %X\n", count, src_pos);
321 for (
i = 0;
i < count;
i++)
322 dest[dest_index +
i] = dest[dest_index - src_pos +
i];
332 if (dest_index < dest_size) {
333 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
334 dest_index, dest_size);
335 memset(dest + dest_index, 0, dest_size - dest_index);
343 unsigned int chunk_type;
344 unsigned int chunk_size;
346 unsigned int index = 0;
348 unsigned char r,
g,
b;
363 int vector_index = 0;
367 int hibytes =
s->decode_buffer_size / 2;
372 chunk_type = bytestream2_get_be32u(&
s->gb);
374 chunk_size = bytestream2_get_be32u(&
s->gb);
376 switch (chunk_type) {
412 byte_skip = chunk_size & 0x01;
417 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
425 if (cplz_chunk != -1) {
432 if (cpl0_chunk != -1) {
435 chunk_size = bytestream2_get_be32(&
s->gb);
442 for (
i = 0;
i < chunk_size / 3;
i++) {
444 r = bytestream2_get_byteu(&
s->gb) * 4;
445 g = bytestream2_get_byteu(&
s->gb) * 4;
446 b = bytestream2_get_byteu(&
s->gb) * 4;
447 s->palette[
i] = 0xFF
U << 24 |
r << 16 |
g << 8 |
b;
448 s->palette[
i] |=
s->palette[
i] >> 6 & 0x30303;
453 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
461 if (cbfz_chunk != -1) {
464 chunk_size = bytestream2_get_be32(&
s->gb);
466 s->codebook_size, 0)) < 0)
471 if (cbf0_chunk != -1) {
474 chunk_size = bytestream2_get_be32(&
s->gb);
486 if (vptz_chunk == -1) {
494 chunk_size = bytestream2_get_be32(&
s->gb);
496 s->decode_buffer,
s->decode_buffer_size, 1)) < 0)
500 if (
s->vector_height == 4)
504 for (y = 0; y <
s->height; y +=
s->vector_height) {
505 for (x = 0; x <
s->width; x += 4, lobytes++, hibytes++) {
510 switch (
s->vqa_version) {
513 lobyte =
s->decode_buffer[lobytes * 2];
514 hibyte =
s->decode_buffer[(lobytes * 2) + 1];
515 vector_index = ((hibyte << 8) | lobyte) >> 3;
516 vector_index <<= index_shift;
517 lines =
s->vector_height;
519 if (hibyte == 0xFF) {
521 frame->
data[0][pixel_ptr + 0] = 255 - lobyte;
522 frame->
data[0][pixel_ptr + 1] = 255 - lobyte;
523 frame->
data[0][pixel_ptr + 2] = 255 - lobyte;
524 frame->
data[0][pixel_ptr + 3] = 255 - lobyte;
532 lobyte =
s->decode_buffer[lobytes];
533 hibyte =
s->decode_buffer[hibytes];
534 vector_index = (hibyte << 8) | lobyte;
535 vector_index <<= index_shift;
536 lines =
s->vector_height;
545 frame->
data[0][pixel_ptr + 0] =
s->codebook[vector_index++];
546 frame->
data[0][pixel_ptr + 1] =
s->codebook[vector_index++];
547 frame->
data[0][pixel_ptr + 2] =
s->codebook[vector_index++];
548 frame->
data[0][pixel_ptr + 3] =
s->codebook[vector_index++];
555 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
561 if (cbp0_chunk != -1) {
564 chunk_size = bytestream2_get_be32(&
s->gb);
575 s->next_codebook_buffer_index += chunk_size;
577 s->partial_countdown--;
578 if (
s->partial_countdown <= 0) {
581 memcpy(
s->codebook,
s->next_codebook_buffer,
582 s->next_codebook_buffer_index);
585 s->next_codebook_buffer_index = 0;
586 s->partial_countdown =
s->partial_count;
590 if (cbpz_chunk != -1) {
593 chunk_size = bytestream2_get_be32(&
s->gb);
604 s->next_codebook_buffer_index += chunk_size;
606 s->partial_countdown--;
607 if (
s->partial_countdown <= 0) {
611 s->codebook,
s->codebook_size, 0);
614 s->next_codebook_buffer_index = 0;
615 s->partial_countdown =
s->partial_count;
626 unsigned int chunk_type;
627 unsigned int chunk_size;
628 unsigned int index = 0;
639 chunk_type = bytestream2_get_be32u(&
s->gb);
641 chunk_size = bytestream2_get_be32u(&
s->gb);
643 switch (chunk_type) {
666 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
673 if (cbfz_chunk != -1) {
675 chunk_size = bytestream2_get_be32(&
s->gb);
677 s->codebook_size, 0)) < 0)
682 if (cbf0_chunk != -1) {
684 chunk_size = bytestream2_get_be32(&
s->gb);
697 if (vptr_chunk != -1) {
700 chunk_size = bytestream2_get_be32(&
s->gb);
701 if (chunk_size >
s->decode_buffer_size) {
706 }
else if (vprz_chunk != -1) {
710 chunk_size = bytestream2_get_be32(&
s->gb);
711 if ((res =
decode_format80(
s, chunk_size,
s->decode_buffer,
s->decode_buffer_size, 0)) < 0)
722 for (
int y_pos = 0; y_pos <
s->height; y_pos +=
s->vector_height) {
725 while (x_pos < s->
width) {
726 int vector_index = 0;
734 code = bytestream2_get_le16(&gb_stream);
742 }
else if (
type < 3) {
743 vector_index =
code & 0xff;
744 count = ((
code & 0x1f00) >> 7) + 1 +
type;
745 }
else if (
type < 5) {
748 }
else if (
type < 7) {
750 count = bytestream2_get_byte(&gb_stream);
756 if (count < 0 || count > (
s->width - x_pos) /
s->vector_width) {
761 while (count-- && x_pos < s->
width) {
762 const int bytes_per_vector = 4 *
s->vector_height *
sizeof(uint16_t);
763 unsigned char *
src =
s->codebook + vector_index * bytes_per_vector;
764 unsigned char *dst =
s->frame->data[0] + y_pos *
s->frame->linesize[0]
765 +
sizeof(uint16_t) * x_pos;
770 for (
int y = 0; y <
s->vector_height; y++) {
771 int size = 4 *
sizeof(uint16_t);
773 dst +=
s->frame->linesize[0];
778 if ((
type == 2) && count > 0) {
779 vector_index = bytestream2_get_byte(&gb_stream);
812 #if FF_API_PALETTE_HAS_CHANGED
814 s->frame->palette_has_changed = 1;
847 {
"max_pixels",
"640*480" },
852 .
p.
name =
"vqavideo",
853 CODEC_LONG_NAME(
"Westwood Studios VQA (Vector Quantized Animation) video"),