00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00066 #include <stdio.h>
00067 #include <stdlib.h>
00068 #include <string.h>
00069 #include <unistd.h>
00070
00071 #include "avcodec.h"
00072 #include "dsputil.h"
00073
00074 #define PALETTE_COUNT 256
00075 #define VQA_HEADER_SIZE 0x2A
00076 #define CHUNK_PREAMBLE_SIZE 8
00077
00078
00079
00080 #define MAX_CODEBOOK_VECTORS 0xFF00
00081 #define SOLID_PIXEL_VECTORS 0x100
00082 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
00083 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
00084
00085 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
00086 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
00087 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
00088 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
00089 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
00090 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
00091 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
00092
00093 #define VQA_DEBUG 0
00094
00095 #if VQA_DEBUG
00096 #define vqa_debug printf
00097 #else
00098 static inline void vqa_debug(const char *format, ...) { }
00099 #endif
00100
00101 typedef struct VqaContext {
00102
00103 AVCodecContext *avctx;
00104 DSPContext dsp;
00105 AVFrame frame;
00106
00107 const unsigned char *buf;
00108 int size;
00109
00110 uint32_t palette[PALETTE_COUNT];
00111
00112 int width;
00113 int height;
00114 int vector_width;
00115 int vector_height;
00116 int vqa_version;
00117
00118 unsigned char *codebook;
00119 int codebook_size;
00120 unsigned char *next_codebook_buffer;
00121 int next_codebook_buffer_index;
00122
00123 unsigned char *decode_buffer;
00124 int decode_buffer_size;
00125
00126
00127 int partial_countdown;
00128 int partial_count;
00129
00130 } VqaContext;
00131
00132 static int vqa_decode_init(AVCodecContext *avctx)
00133 {
00134 VqaContext *s = avctx->priv_data;
00135 unsigned char *vqa_header;
00136 int i, j, codebook_index;;
00137
00138 s->avctx = avctx;
00139 avctx->pix_fmt = PIX_FMT_PAL8;
00140 dsputil_init(&s->dsp, avctx);
00141
00142
00143 if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
00144 av_log(s->avctx, AV_LOG_ERROR, " VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
00145 return -1;
00146 }
00147
00148
00149 vqa_header = (unsigned char *)s->avctx->extradata;
00150 s->vqa_version = vqa_header[0];
00151 s->width = AV_RL16(&vqa_header[6]);
00152 s->height = AV_RL16(&vqa_header[8]);
00153 if(avcodec_check_dimensions(avctx, s->width, s->height)){
00154 s->width= s->height= 0;
00155 return -1;
00156 }
00157 s->vector_width = vqa_header[10];
00158 s->vector_height = vqa_header[11];
00159 s->partial_count = s->partial_countdown = vqa_header[13];
00160
00161
00162 if ((s->vector_width != 4) ||
00163 ((s->vector_height != 2) && (s->vector_height != 4))) {
00164
00165 return -1;
00166 }
00167
00168
00169 s->codebook_size = MAX_CODEBOOK_SIZE;
00170 s->codebook = av_malloc(s->codebook_size);
00171 s->next_codebook_buffer = av_malloc(s->codebook_size);
00172
00173
00174 if (s->vector_height == 4) {
00175 codebook_index = 0xFF00 * 16;
00176 for (i = 0; i < 256; i++)
00177 for (j = 0; j < 16; j++)
00178 s->codebook[codebook_index++] = i;
00179 } else {
00180 codebook_index = 0xF00 * 8;
00181 for (i = 0; i < 256; i++)
00182 for (j = 0; j < 8; j++)
00183 s->codebook[codebook_index++] = i;
00184 }
00185 s->next_codebook_buffer_index = 0;
00186
00187
00188 s->decode_buffer_size = (s->width / s->vector_width) *
00189 (s->height / s->vector_height) * 2;
00190 s->decode_buffer = av_malloc(s->decode_buffer_size);
00191
00192 s->frame.data[0] = NULL;
00193
00194 return 0;
00195 }
00196
00197 #define CHECK_COUNT() \
00198 if (dest_index + count > dest_size) { \
00199 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
00200 av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
00201 dest_index, count, dest_size); \
00202 return; \
00203 }
00204
00205 static void decode_format80(const unsigned char *src, int src_size,
00206 unsigned char *dest, int dest_size, int check_size) {
00207
00208 int src_index = 0;
00209 int dest_index = 0;
00210 int count;
00211 int src_pos;
00212 unsigned char color;
00213 int i;
00214
00215 while (src_index < src_size) {
00216
00217 vqa_debug(" opcode %02X: ", src[src_index]);
00218
00219
00220 if (src[src_index] == 0x80)
00221 return;
00222
00223 if (dest_index >= dest_size) {
00224 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
00225 dest_index, dest_size);
00226 return;
00227 }
00228
00229 if (src[src_index] == 0xFF) {
00230
00231 src_index++;
00232 count = AV_RL16(&src[src_index]);
00233 src_index += 2;
00234 src_pos = AV_RL16(&src[src_index]);
00235 src_index += 2;
00236 vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
00237 CHECK_COUNT();
00238 for (i = 0; i < count; i++)
00239 dest[dest_index + i] = dest[src_pos + i];
00240 dest_index += count;
00241
00242 } else if (src[src_index] == 0xFE) {
00243
00244 src_index++;
00245 count = AV_RL16(&src[src_index]);
00246 src_index += 2;
00247 color = src[src_index++];
00248 vqa_debug("(2) set %X bytes to %02X\n", count, color);
00249 CHECK_COUNT();
00250 memset(&dest[dest_index], color, count);
00251 dest_index += count;
00252
00253 } else if ((src[src_index] & 0xC0) == 0xC0) {
00254
00255 count = (src[src_index++] & 0x3F) + 3;
00256 src_pos = AV_RL16(&src[src_index]);
00257 src_index += 2;
00258 vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
00259 CHECK_COUNT();
00260 for (i = 0; i < count; i++)
00261 dest[dest_index + i] = dest[src_pos + i];
00262 dest_index += count;
00263
00264 } else if (src[src_index] > 0x80) {
00265
00266 count = src[src_index++] & 0x3F;
00267 vqa_debug("(4) copy %X bytes from source to dest\n", count);
00268 CHECK_COUNT();
00269 memcpy(&dest[dest_index], &src[src_index], count);
00270 src_index += count;
00271 dest_index += count;
00272
00273 } else {
00274
00275 count = ((src[src_index] & 0x70) >> 4) + 3;
00276 src_pos = AV_RB16(&src[src_index]) & 0x0FFF;
00277 src_index += 2;
00278 vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
00279 CHECK_COUNT();
00280 for (i = 0; i < count; i++)
00281 dest[dest_index + i] = dest[dest_index - src_pos + i];
00282 dest_index += count;
00283 }
00284 }
00285
00286
00287
00288
00289
00290 if (check_size)
00291 if (dest_index < dest_size)
00292 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
00293 dest_index, dest_size);
00294 }
00295
00296 static void vqa_decode_chunk(VqaContext *s)
00297 {
00298 unsigned int chunk_type;
00299 unsigned int chunk_size;
00300 int byte_skip;
00301 unsigned int index = 0;
00302 int i;
00303 unsigned char r, g, b;
00304 int index_shift;
00305
00306 int cbf0_chunk = -1;
00307 int cbfz_chunk = -1;
00308 int cbp0_chunk = -1;
00309 int cbpz_chunk = -1;
00310 int cpl0_chunk = -1;
00311 int cplz_chunk = -1;
00312 int vptz_chunk = -1;
00313
00314 int x, y;
00315 int lines = 0;
00316 int pixel_ptr;
00317 int vector_index = 0;
00318 int lobyte = 0;
00319 int hibyte = 0;
00320 int lobytes = 0;
00321 int hibytes = s->decode_buffer_size / 2;
00322
00323
00324 while (index < s->size) {
00325
00326 chunk_type = AV_RB32(&s->buf[index]);
00327 chunk_size = AV_RB32(&s->buf[index + 4]);
00328
00329 switch (chunk_type) {
00330
00331 case CBF0_TAG:
00332 cbf0_chunk = index;
00333 break;
00334
00335 case CBFZ_TAG:
00336 cbfz_chunk = index;
00337 break;
00338
00339 case CBP0_TAG:
00340 cbp0_chunk = index;
00341 break;
00342
00343 case CBPZ_TAG:
00344 cbpz_chunk = index;
00345 break;
00346
00347 case CPL0_TAG:
00348 cpl0_chunk = index;
00349 break;
00350
00351 case CPLZ_TAG:
00352 cplz_chunk = index;
00353 break;
00354
00355 case VPTZ_TAG:
00356 vptz_chunk = index;
00357 break;
00358
00359 default:
00360 av_log(s->avctx, AV_LOG_ERROR, " VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
00361 (chunk_type >> 24) & 0xFF,
00362 (chunk_type >> 16) & 0xFF,
00363 (chunk_type >> 8) & 0xFF,
00364 (chunk_type >> 0) & 0xFF,
00365 chunk_type);
00366 break;
00367 }
00368
00369 byte_skip = chunk_size & 0x01;
00370 index += (CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip);
00371 }
00372
00373
00374 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
00375
00376
00377 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n");
00378 return;
00379 }
00380
00381
00382 if (cplz_chunk != -1) {
00383
00384
00385
00386 }
00387
00388
00389 if (cpl0_chunk != -1) {
00390
00391 chunk_size = AV_RB32(&s->buf[cpl0_chunk + 4]);
00392
00393 if (chunk_size / 3 > 256) {
00394 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n",
00395 chunk_size / 3);
00396 return;
00397 }
00398 cpl0_chunk += CHUNK_PREAMBLE_SIZE;
00399 for (i = 0; i < chunk_size / 3; i++) {
00400
00401 r = s->buf[cpl0_chunk++] * 4;
00402 g = s->buf[cpl0_chunk++] * 4;
00403 b = s->buf[cpl0_chunk++] * 4;
00404 s->palette[i] = (r << 16) | (g << 8) | (b);
00405 }
00406 }
00407
00408
00409 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
00410
00411
00412 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n");
00413 return;
00414 }
00415
00416
00417 if (cbfz_chunk != -1) {
00418
00419 chunk_size = AV_RB32(&s->buf[cbfz_chunk + 4]);
00420 cbfz_chunk += CHUNK_PREAMBLE_SIZE;
00421 decode_format80(&s->buf[cbfz_chunk], chunk_size,
00422 s->codebook, s->codebook_size, 0);
00423 }
00424
00425
00426 if (cbf0_chunk != -1) {
00427
00428 chunk_size = AV_RB32(&s->buf[cbf0_chunk + 4]);
00429
00430 if (chunk_size > MAX_CODEBOOK_SIZE) {
00431 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
00432 chunk_size);
00433 return;
00434 }
00435 cbf0_chunk += CHUNK_PREAMBLE_SIZE;
00436
00437 memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size);
00438 }
00439
00440
00441 if (vptz_chunk == -1) {
00442
00443
00444 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n");
00445 return;
00446 }
00447
00448 chunk_size = AV_RB32(&s->buf[vptz_chunk + 4]);
00449 vptz_chunk += CHUNK_PREAMBLE_SIZE;
00450 decode_format80(&s->buf[vptz_chunk], chunk_size,
00451 s->decode_buffer, s->decode_buffer_size, 1);
00452
00453
00454 if (s->vector_height == 4)
00455 index_shift = 4;
00456 else
00457 index_shift = 3;
00458 for (y = 0; y < s->frame.linesize[0] * s->height;
00459 y += s->frame.linesize[0] * s->vector_height) {
00460
00461 for (x = y; x < y + s->width; x += 4, lobytes++, hibytes++) {
00462 pixel_ptr = x;
00463
00464
00465
00466 switch (s->vqa_version) {
00467
00468 case 1:
00469
00470
00471 lobyte = s->decode_buffer[lobytes * 2];
00472 hibyte = s->decode_buffer[(lobytes * 2) + 1];
00473 vector_index = ((hibyte << 8) | lobyte) >> 3;
00474 vector_index <<= index_shift;
00475 lines = s->vector_height;
00476
00477 if (hibyte == 0xFF) {
00478 while (lines--) {
00479 s->frame.data[0][pixel_ptr + 0] = 255 - lobyte;
00480 s->frame.data[0][pixel_ptr + 1] = 255 - lobyte;
00481 s->frame.data[0][pixel_ptr + 2] = 255 - lobyte;
00482 s->frame.data[0][pixel_ptr + 3] = 255 - lobyte;
00483 pixel_ptr += s->frame.linesize[0];
00484 }
00485 lines=0;
00486 }
00487 break;
00488
00489 case 2:
00490 lobyte = s->decode_buffer[lobytes];
00491 hibyte = s->decode_buffer[hibytes];
00492 vector_index = (hibyte << 8) | lobyte;
00493 vector_index <<= index_shift;
00494 lines = s->vector_height;
00495 break;
00496
00497 case 3:
00498
00499 lines = 0;
00500 break;
00501 }
00502
00503 while (lines--) {
00504 s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
00505 s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
00506 s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
00507 s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
00508 pixel_ptr += s->frame.linesize[0];
00509 }
00510 }
00511 }
00512
00513
00514 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
00515
00516 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n");
00517 return;
00518 }
00519
00520 if (cbp0_chunk != -1) {
00521
00522 chunk_size = AV_RB32(&s->buf[cbp0_chunk + 4]);
00523 cbp0_chunk += CHUNK_PREAMBLE_SIZE;
00524
00525
00526 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
00527 &s->buf[cbp0_chunk], chunk_size);
00528 s->next_codebook_buffer_index += chunk_size;
00529
00530 s->partial_countdown--;
00531 if (s->partial_countdown == 0) {
00532
00533
00534 memcpy(s->codebook, s->next_codebook_buffer,
00535 s->next_codebook_buffer_index);
00536
00537
00538 s->next_codebook_buffer_index = 0;
00539 s->partial_countdown = s->partial_count;
00540 }
00541 }
00542
00543 if (cbpz_chunk != -1) {
00544
00545 chunk_size = AV_RB32(&s->buf[cbpz_chunk + 4]);
00546 cbpz_chunk += CHUNK_PREAMBLE_SIZE;
00547
00548
00549 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
00550 &s->buf[cbpz_chunk], chunk_size);
00551 s->next_codebook_buffer_index += chunk_size;
00552
00553 s->partial_countdown--;
00554 if (s->partial_countdown == 0) {
00555
00556
00557 decode_format80(s->next_codebook_buffer,
00558 s->next_codebook_buffer_index,
00559 s->codebook, s->codebook_size, 0);
00560
00561
00562 s->next_codebook_buffer_index = 0;
00563 s->partial_countdown = s->partial_count;
00564 }
00565 }
00566 }
00567
00568 static int vqa_decode_frame(AVCodecContext *avctx,
00569 void *data, int *data_size,
00570 const uint8_t *buf, int buf_size)
00571 {
00572 VqaContext *s = avctx->priv_data;
00573
00574 s->buf = buf;
00575 s->size = buf_size;
00576
00577 if (s->frame.data[0])
00578 avctx->release_buffer(avctx, &s->frame);
00579
00580 if (avctx->get_buffer(avctx, &s->frame)) {
00581 av_log(s->avctx, AV_LOG_ERROR, " VQA Video: get_buffer() failed\n");
00582 return -1;
00583 }
00584
00585 vqa_decode_chunk(s);
00586
00587
00588 memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
00589 s->frame.palette_has_changed = 1;
00590
00591 *data_size = sizeof(AVFrame);
00592 *(AVFrame*)data = s->frame;
00593
00594
00595 return buf_size;
00596 }
00597
00598 static int vqa_decode_end(AVCodecContext *avctx)
00599 {
00600 VqaContext *s = avctx->priv_data;
00601
00602 av_free(s->codebook);
00603 av_free(s->next_codebook_buffer);
00604 av_free(s->decode_buffer);
00605
00606 if (s->frame.data[0])
00607 avctx->release_buffer(avctx, &s->frame);
00608
00609 return 0;
00610 }
00611
00612 AVCodec vqa_decoder = {
00613 "vqavideo",
00614 CODEC_TYPE_VIDEO,
00615 CODEC_ID_WS_VQA,
00616 sizeof(VqaContext),
00617 vqa_decode_init,
00618 NULL,
00619 vqa_decode_end,
00620 vqa_decode_frame,
00621 CODEC_CAP_DR1,
00622 };