83 #define PALETTE_COUNT 256
84 #define VQA_HEADER_SIZE 0x2A
88 #define MAX_CODEBOOK_VECTORS 0xFF00
89 #define SOLID_PIXEL_VECTORS 0x100
90 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
91 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4 * sizeof(uint16_t))
93 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
94 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
95 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
96 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
97 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
98 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
99 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
100 #define VPTR_TAG MKBETAG('V', 'P', 'T', 'R')
101 #define VPRZ_TAG MKBETAG('V', 'P', 'R', 'Z')
132 int i, j, codebook_index,
ret;
144 s->vqa_version =
s->avctx->extradata[0];
146 if (
s->vqa_version < 1 ||
s->vqa_version > 3) {
151 s->width =
AV_RL16(&
s->avctx->extradata[6]);
152 s->height =
AV_RL16(&
s->avctx->extradata[8]);
154 s->width=
s->height= 0;
157 s->vector_width =
s->avctx->extradata[10];
158 s->vector_height =
s->avctx->extradata[11];
159 s->partial_count =
s->partial_countdown =
s->avctx->extradata[13];
161 colors = (
s->avctx->extradata[14] << 8) |
s->avctx->extradata[15];
170 if ((
s->vector_width != 4) ||
171 ((
s->vector_height != 2) && (
s->vector_height != 4))) {
176 if (
s->width %
s->vector_width ||
s->height %
s->vector_height) {
190 s->next_codebook_buffer =
av_malloc(
s->codebook_size);
191 if (!
s->next_codebook_buffer)
195 s->decode_buffer_size = (
s->width /
s->vector_width) *
196 (
s->height /
s->vector_height) * 2;
198 if (!
s->decode_buffer)
202 if (
s->vector_height == 4) {
203 codebook_index = 0xFF00 * 16;
204 for (
i = 0;
i < 256;
i++)
205 for (j = 0; j < 16; j++)
206 s->codebook[codebook_index++] =
i;
208 codebook_index = 0xF00 * 8;
209 for (
i = 0;
i < 256;
i++)
210 for (j = 0; j < 8; j++)
211 s->codebook[codebook_index++] =
i;
213 s->next_codebook_buffer_index = 0;
218 #define CHECK_COUNT() \
219 if (dest_index + count > dest_size) { \
220 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
221 av_log(s->avctx, AV_LOG_ERROR, "current dest_index = %d, count = %d, dest_size = %d\n", \
222 dest_index, count, dest_size); \
223 return AVERROR_INVALIDDATA; \
226 #define CHECK_COPY(idx) \
227 if (idx < 0 || idx + count > dest_size) { \
228 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
229 av_log(s->avctx, AV_LOG_ERROR, "current src_pos = %d, count = %d, dest_size = %d\n", \
230 src_pos, count, dest_size); \
231 return AVERROR_INVALIDDATA; \
236 unsigned char *dest,
int dest_size,
int check_size) {
239 int count, opcode, start;
252 if (bytestream2_peek_byte(&
s->gb) == 0x00) {
254 bytestream2_get_byte(&
s->gb);
255 ff_tlog(
s->avctx,
"found new format stream ");
260 opcode = bytestream2_get_byte(&
s->gb);
261 ff_tlog(
s->avctx,
"opcode %02X: ", opcode);
267 if (dest_index >= dest_size) {
268 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
269 dest_index, dest_size);
273 if (opcode == 0xFF) {
275 count = bytestream2_get_le16(&
s->gb);
276 src_pos = bytestream2_get_le16(&
s->gb);
278 src_pos = dest_index - src_pos;
279 ff_tlog(
s->avctx,
"(1) copy %X bytes from pos %X\n", count, src_pos);
282 for (
i = 0;
i < count;
i++)
283 dest[dest_index +
i] = dest[src_pos +
i];
286 }
else if (opcode == 0xFE) {
288 count = bytestream2_get_le16(&
s->gb);
289 color = bytestream2_get_byte(&
s->gb);
290 ff_tlog(
s->avctx,
"(2) set %X bytes to %02X\n", count,
color);
292 memset(&dest[dest_index],
color, count);
295 }
else if ((opcode & 0xC0) == 0xC0) {
297 count = (opcode & 0x3F) + 3;
298 src_pos = bytestream2_get_le16(&
s->gb);
300 src_pos = dest_index - src_pos;
301 ff_tlog(
s->avctx,
"(3) copy %X bytes from pos %X\n", count, src_pos);
304 for (
i = 0;
i < count;
i++)
305 dest[dest_index +
i] = dest[src_pos +
i];
308 }
else if (opcode > 0x80) {
310 count = opcode & 0x3F;
311 ff_tlog(
s->avctx,
"(4) copy %X bytes from source to dest\n", count);
318 count = ((opcode & 0x70) >> 4) + 3;
319 src_pos = bytestream2_get_byte(&
s->gb) | ((opcode & 0x0F) << 8);
320 ff_tlog(
s->avctx,
"(5) copy %X bytes from relpos %X\n", count, src_pos);
323 for (
i = 0;
i < count;
i++)
324 dest[dest_index +
i] = dest[dest_index - src_pos +
i];
334 if (dest_index < dest_size) {
335 av_log(
s->avctx,
AV_LOG_ERROR,
"decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
336 dest_index, dest_size);
337 memset(dest + dest_index, 0, dest_size - dest_index);
345 unsigned int chunk_type;
346 unsigned int chunk_size;
348 unsigned int index = 0;
350 unsigned char r,
g,
b;
365 int vector_index = 0;
369 int hibytes =
s->decode_buffer_size / 2;
374 chunk_type = bytestream2_get_be32u(&
s->gb);
376 chunk_size = bytestream2_get_be32u(&
s->gb);
378 switch (chunk_type) {
414 byte_skip = chunk_size & 0x01;
419 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
427 if (cplz_chunk != -1) {
434 if (cpl0_chunk != -1) {
437 chunk_size = bytestream2_get_be32(&
s->gb);
444 for (
i = 0;
i < chunk_size / 3;
i++) {
446 r = bytestream2_get_byteu(&
s->gb) * 4;
447 g = bytestream2_get_byteu(&
s->gb) * 4;
448 b = bytestream2_get_byteu(&
s->gb) * 4;
449 s->palette[
i] = 0xFF
U << 24 |
r << 16 |
g << 8 |
b;
450 s->palette[
i] |=
s->palette[
i] >> 6 & 0x30303;
455 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
463 if (cbfz_chunk != -1) {
466 chunk_size = bytestream2_get_be32(&
s->gb);
468 s->codebook_size, 0)) < 0)
473 if (cbf0_chunk != -1) {
476 chunk_size = bytestream2_get_be32(&
s->gb);
488 if (vptz_chunk == -1) {
496 chunk_size = bytestream2_get_be32(&
s->gb);
498 s->decode_buffer,
s->decode_buffer_size, 1)) < 0)
502 if (
s->vector_height == 4)
506 for (y = 0; y <
s->height; y +=
s->vector_height) {
507 for (x = 0; x <
s->width; x += 4, lobytes++, hibytes++) {
508 pixel_ptr = y *
frame->linesize[0] + x;
512 switch (
s->vqa_version) {
515 lobyte =
s->decode_buffer[lobytes * 2];
516 hibyte =
s->decode_buffer[(lobytes * 2) + 1];
517 vector_index = ((hibyte << 8) | lobyte) >> 3;
518 vector_index <<= index_shift;
519 lines =
s->vector_height;
521 if (hibyte == 0xFF) {
523 frame->data[0][pixel_ptr + 0] = 255 - lobyte;
524 frame->data[0][pixel_ptr + 1] = 255 - lobyte;
525 frame->data[0][pixel_ptr + 2] = 255 - lobyte;
526 frame->data[0][pixel_ptr + 3] = 255 - lobyte;
527 pixel_ptr +=
frame->linesize[0];
534 lobyte =
s->decode_buffer[lobytes];
535 hibyte =
s->decode_buffer[hibytes];
536 vector_index = (hibyte << 8) | lobyte;
537 vector_index <<= index_shift;
538 lines =
s->vector_height;
547 frame->data[0][pixel_ptr + 0] =
s->codebook[vector_index++];
548 frame->data[0][pixel_ptr + 1] =
s->codebook[vector_index++];
549 frame->data[0][pixel_ptr + 2] =
s->codebook[vector_index++];
550 frame->data[0][pixel_ptr + 3] =
s->codebook[vector_index++];
551 pixel_ptr +=
frame->linesize[0];
557 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
563 if (cbp0_chunk != -1) {
566 chunk_size = bytestream2_get_be32(&
s->gb);
577 s->next_codebook_buffer_index += chunk_size;
579 s->partial_countdown--;
580 if (
s->partial_countdown <= 0) {
583 memcpy(
s->codebook,
s->next_codebook_buffer,
584 s->next_codebook_buffer_index);
587 s->next_codebook_buffer_index = 0;
588 s->partial_countdown =
s->partial_count;
592 if (cbpz_chunk != -1) {
595 chunk_size = bytestream2_get_be32(&
s->gb);
606 s->next_codebook_buffer_index += chunk_size;
608 s->partial_countdown--;
609 if (
s->partial_countdown <= 0) {
613 s->codebook,
s->codebook_size, 0);
616 s->next_codebook_buffer_index = 0;
617 s->partial_countdown =
s->partial_count;
628 unsigned int chunk_type;
629 unsigned int chunk_size;
630 unsigned int index = 0;
641 chunk_type = bytestream2_get_be32u(&
s->gb);
643 chunk_size = bytestream2_get_be32u(&
s->gb);
645 switch (chunk_type) {
668 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
675 if (cbfz_chunk != -1) {
677 chunk_size = bytestream2_get_be32(&
s->gb);
679 s->codebook_size, 0)) < 0)
684 if (cbf0_chunk != -1) {
686 chunk_size = bytestream2_get_be32(&
s->gb);
699 if (vptr_chunk != -1) {
702 chunk_size = bytestream2_get_be32(&
s->gb);
703 if (chunk_size >
s->decode_buffer_size) {
708 }
else if (vprz_chunk != -1) {
712 chunk_size = bytestream2_get_be32(&
s->gb);
713 if ((res =
decode_format80(
s, chunk_size,
s->decode_buffer,
s->decode_buffer_size, 0)) < 0)
724 for (
int y_pos = 0; y_pos <
s->height; y_pos +=
s->vector_height) {
727 while (x_pos < s->
width) {
728 int vector_index = 0;
736 code = bytestream2_get_le16(&gb_stream);
744 }
else if (
type < 3) {
745 vector_index =
code & 0xff;
746 count = ((
code & 0x1f00) >> 7) + 1 +
type;
747 }
else if (
type < 5) {
750 }
else if (
type < 7) {
752 count = bytestream2_get_byte(&gb_stream);
758 if (count < 0 || count > (
s->width - x_pos) /
s->vector_width) {
763 while (count-- && x_pos < s->
width) {
764 const int bytes_per_vector = 4 *
s->vector_height *
sizeof(uint16_t);
765 unsigned char *
src =
s->codebook + vector_index * bytes_per_vector;
766 unsigned char *dst =
s->frame->data[0] + y_pos *
s->frame->linesize[0]
767 +
sizeof(uint16_t) * x_pos;
772 for (
int y = 0; y <
s->vector_height; y++) {
773 int size = 4 *
sizeof(uint16_t);
775 dst +=
s->frame->linesize[0];
780 if ((
type == 2) && count > 0) {
781 vector_index = bytestream2_get_byte(&gb_stream);
814 s->frame->palette_has_changed = 1;
845 {
"max_pixels",
"640*480" },
850 .
p.
name =
"vqavideo",