vqavideo.c
Go to the documentation of this file.
1 /*
2  * Westwood Studios VQA Video Decoder
3  * Copyright (C) 2003 the ffmpeg project
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 
70 #include "libavutil/intreadwrite.h"
71 #include "libavutil/imgutils.h"
72 #include "avcodec.h"
73 #include "bytestream.h"
74 #include "internal.h"
75 
76 #define PALETTE_COUNT 256
77 #define VQA_HEADER_SIZE 0x2A
78 
79 /* allocate the maximum vector space, regardless of the file version:
80  * (0xFF00 codebook vectors + 0x100 solid pixel vectors) * (4x4 pixels/block) */
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)
85 
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')
93 
94 typedef struct VqaContext {
95 
99 
101 
102  int width; /* width of a frame */
103  int height; /* height of a frame */
104  int vector_width; /* width of individual vector */
105  int vector_height; /* height of individual vector */
106  int vqa_version; /* this should be either 1, 2 or 3 */
107 
108  unsigned char *codebook; /* the current codebook */
110  unsigned char *next_codebook_buffer; /* accumulator for next codebook */
112 
113  unsigned char *decode_buffer;
115 
116  /* number of frames to go before replacing codebook */
119 
120 } VqaContext;
121 
123 {
124  VqaContext *s = avctx->priv_data;
125  int i, j, codebook_index;
126 
127  s->avctx = avctx;
128  avctx->pix_fmt = AV_PIX_FMT_PAL8;
129 
130  /* make sure the extradata made it */
131  if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
132  av_log(s->avctx, AV_LOG_ERROR, " VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
133  return -1;
134  }
135 
136  /* load up the VQA parameters from the header */
137  s->vqa_version = s->avctx->extradata[0];
138  switch (s->vqa_version) {
139  case 1:
140  case 2:
141  break;
142  case 3:
143  av_log_missing_feature(avctx, "VQA Version 3", 0);
144  return AVERROR_PATCHWELCOME;
145  default:
146  av_log_missing_feature(avctx, "VQA Version", 1);
147  return AVERROR_PATCHWELCOME;
148  }
149  s->width = AV_RL16(&s->avctx->extradata[6]);
150  s->height = AV_RL16(&s->avctx->extradata[8]);
151  if(av_image_check_size(s->width, s->height, 0, avctx)){
152  s->width= s->height= 0;
153  return -1;
154  }
155  s->vector_width = s->avctx->extradata[10];
156  s->vector_height = s->avctx->extradata[11];
158 
159  /* the vector dimensions have to meet very stringent requirements */
160  if ((s->vector_width != 4) ||
161  ((s->vector_height != 2) && (s->vector_height != 4))) {
162  /* return without further initialization */
163  return -1;
164  }
165 
166  if (s->width & (s->vector_width - 1) ||
167  s->height & (s->vector_height - 1)) {
168  av_log(avctx, AV_LOG_ERROR, "Image size not multiple of block size\n");
169  return AVERROR_INVALIDDATA;
170  }
171 
172  /* allocate codebooks */
175  if (!s->codebook)
176  goto fail;
178  if (!s->next_codebook_buffer)
179  goto fail;
180 
181  /* allocate decode buffer */
182  s->decode_buffer_size = (s->width / s->vector_width) *
183  (s->height / s->vector_height) * 2;
185  if (!s->decode_buffer)
186  goto fail;
187 
188  /* initialize the solid-color vectors */
189  if (s->vector_height == 4) {
190  codebook_index = 0xFF00 * 16;
191  for (i = 0; i < 256; i++)
192  for (j = 0; j < 16; j++)
193  s->codebook[codebook_index++] = i;
194  } else {
195  codebook_index = 0xF00 * 8;
196  for (i = 0; i < 256; i++)
197  for (j = 0; j < 8; j++)
198  s->codebook[codebook_index++] = i;
199  }
201 
202  s->frame.data[0] = NULL;
203 
204  return 0;
205 fail:
206  av_freep(&s->codebook);
208  av_freep(&s->decode_buffer);
209  return AVERROR(ENOMEM);
210 }
211 
212 #define CHECK_COUNT() \
213  if (dest_index + count > dest_size) { \
214  av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
215  av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
216  dest_index, count, dest_size); \
217  return AVERROR_INVALIDDATA; \
218  }
219 
220 #define CHECK_COPY(idx) \
221  if (idx < 0 || idx + count > dest_size) { \
222  av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
223  av_log(NULL, AV_LOG_ERROR, " VQA video: current src_pos = %d, count = %d, dest_size = %d\n", \
224  src_pos, count, dest_size); \
225  return AVERROR_INVALIDDATA; \
226  }
227 
228 
229 static int decode_format80(GetByteContext *gb, int src_size,
230  unsigned char *dest, int dest_size, int check_size) {
231 
232  int dest_index = 0;
233  int count, opcode, start;
234  int src_pos;
235  unsigned char color;
236  int i;
237 
238  start = bytestream2_tell(gb);
239  while (bytestream2_tell(gb) - start < src_size) {
240  opcode = bytestream2_get_byte(gb);
241  av_dlog(NULL, " opcode %02X: ", opcode);
242 
243  /* 0x80 means that frame is finished */
244  if (opcode == 0x80)
245  return 0;
246 
247  if (dest_index >= dest_size) {
248  av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
249  dest_index, dest_size);
250  return AVERROR_INVALIDDATA;
251  }
252 
253  if (opcode == 0xFF) {
254 
255  count = bytestream2_get_le16(gb);
256  src_pos = bytestream2_get_le16(gb);
257  av_dlog(NULL, "(1) copy %X bytes from absolute pos %X\n", count, src_pos);
258  CHECK_COUNT();
259  CHECK_COPY(src_pos);
260  for (i = 0; i < count; i++)
261  dest[dest_index + i] = dest[src_pos + i];
262  dest_index += count;
263 
264  } else if (opcode == 0xFE) {
265 
266  count = bytestream2_get_le16(gb);
267  color = bytestream2_get_byte(gb);
268  av_dlog(NULL, "(2) set %X bytes to %02X\n", count, color);
269  CHECK_COUNT();
270  memset(&dest[dest_index], color, count);
271  dest_index += count;
272 
273  } else if ((opcode & 0xC0) == 0xC0) {
274 
275  count = (opcode & 0x3F) + 3;
276  src_pos = bytestream2_get_le16(gb);
277  av_dlog(NULL, "(3) copy %X bytes from absolute pos %X\n", count, src_pos);
278  CHECK_COUNT();
279  CHECK_COPY(src_pos);
280  for (i = 0; i < count; i++)
281  dest[dest_index + i] = dest[src_pos + i];
282  dest_index += count;
283 
284  } else if (opcode > 0x80) {
285 
286  count = opcode & 0x3F;
287  av_dlog(NULL, "(4) copy %X bytes from source to dest\n", count);
288  CHECK_COUNT();
289  bytestream2_get_buffer(gb, &dest[dest_index], count);
290  dest_index += count;
291 
292  } else {
293 
294  count = ((opcode & 0x70) >> 4) + 3;
295  src_pos = bytestream2_get_byte(gb) | ((opcode & 0x0F) << 8);
296  av_dlog(NULL, "(5) copy %X bytes from relpos %X\n", count, src_pos);
297  CHECK_COUNT();
298  CHECK_COPY(dest_index - src_pos);
299  for (i = 0; i < count; i++)
300  dest[dest_index + i] = dest[dest_index - src_pos + i];
301  dest_index += count;
302  }
303  }
304 
305  /* validate that the entire destination buffer was filled; this is
306  * important for decoding frame maps since each vector needs to have a
307  * codebook entry; it is not important for compressed codebooks because
308  * not every entry needs to be filled */
309  if (check_size)
310  if (dest_index < dest_size)
311  av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
312  dest_index, dest_size);
313 
314  return 0; // let's display what we decoded anyway
315 }
316 
318 {
319  unsigned int chunk_type;
320  unsigned int chunk_size;
321  int byte_skip;
322  unsigned int index = 0;
323  int i;
324  unsigned char r, g, b;
325  int index_shift;
326  int res;
327 
328  int cbf0_chunk = -1;
329  int cbfz_chunk = -1;
330  int cbp0_chunk = -1;
331  int cbpz_chunk = -1;
332  int cpl0_chunk = -1;
333  int cplz_chunk = -1;
334  int vptz_chunk = -1;
335 
336  int x, y;
337  int lines = 0;
338  int pixel_ptr;
339  int vector_index = 0;
340  int lobyte = 0;
341  int hibyte = 0;
342  int lobytes = 0;
343  int hibytes = s->decode_buffer_size / 2;
344 
345  /* first, traverse through the frame and find the subchunks */
346  while (bytestream2_get_bytes_left(&s->gb) >= 8) {
347 
348  chunk_type = bytestream2_get_be32u(&s->gb);
349  index = bytestream2_tell(&s->gb);
350  chunk_size = bytestream2_get_be32u(&s->gb);
351 
352  switch (chunk_type) {
353 
354  case CBF0_TAG:
355  cbf0_chunk = index;
356  break;
357 
358  case CBFZ_TAG:
359  cbfz_chunk = index;
360  break;
361 
362  case CBP0_TAG:
363  cbp0_chunk = index;
364  break;
365 
366  case CBPZ_TAG:
367  cbpz_chunk = index;
368  break;
369 
370  case CPL0_TAG:
371  cpl0_chunk = index;
372  break;
373 
374  case CPLZ_TAG:
375  cplz_chunk = index;
376  break;
377 
378  case VPTZ_TAG:
379  vptz_chunk = index;
380  break;
381 
382  default:
383  av_log(s->avctx, AV_LOG_ERROR, " VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
384  (chunk_type >> 24) & 0xFF,
385  (chunk_type >> 16) & 0xFF,
386  (chunk_type >> 8) & 0xFF,
387  (chunk_type >> 0) & 0xFF,
388  chunk_type);
389  break;
390  }
391 
392  byte_skip = chunk_size & 0x01;
393  bytestream2_skip(&s->gb, chunk_size + byte_skip);
394  }
395 
396  /* next, deal with the palette */
397  if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
398 
399  /* a chunk should not have both chunk types */
400  av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n");
401  return AVERROR_INVALIDDATA;
402  }
403 
404  /* decompress the palette chunk */
405  if (cplz_chunk != -1) {
406 
407 /* yet to be handled */
408 
409  }
410 
411  /* convert the RGB palette into the machine's endian format */
412  if (cpl0_chunk != -1) {
413 
414  bytestream2_seek(&s->gb, cpl0_chunk, SEEK_SET);
415  chunk_size = bytestream2_get_be32(&s->gb);
416  /* sanity check the palette size */
417  if (chunk_size / 3 > 256 || chunk_size > bytestream2_get_bytes_left(&s->gb)) {
418  av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n",
419  chunk_size / 3);
420  return AVERROR_INVALIDDATA;
421  }
422  for (i = 0; i < chunk_size / 3; i++) {
423  /* scale by 4 to transform 6-bit palette -> 8-bit */
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] = (r << 16) | (g << 8) | (b);
428  }
429  }
430 
431  /* next, look for a full codebook */
432  if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
433 
434  /* a chunk should not have both chunk types */
435  av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n");
436  return AVERROR_INVALIDDATA;
437  }
438 
439  /* decompress the full codebook chunk */
440  if (cbfz_chunk != -1) {
441 
442  bytestream2_seek(&s->gb, cbfz_chunk, SEEK_SET);
443  chunk_size = bytestream2_get_be32(&s->gb);
444  if ((res = decode_format80(&s->gb, chunk_size, s->codebook,
445  s->codebook_size, 0)) < 0)
446  return res;
447  }
448 
449  /* copy a full codebook */
450  if (cbf0_chunk != -1) {
451 
452  bytestream2_seek(&s->gb, cbf0_chunk, SEEK_SET);
453  chunk_size = bytestream2_get_be32(&s->gb);
454  /* sanity check the full codebook size */
455  if (chunk_size > MAX_CODEBOOK_SIZE) {
456  av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
457  chunk_size);
458  return AVERROR_INVALIDDATA;
459  }
460 
461  bytestream2_get_buffer(&s->gb, s->codebook, chunk_size);
462  }
463 
464  /* decode the frame */
465  if (vptz_chunk == -1) {
466 
467  /* something is wrong if there is no VPTZ chunk */
468  av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n");
469  return AVERROR_INVALIDDATA;
470  }
471 
472  bytestream2_seek(&s->gb, vptz_chunk, SEEK_SET);
473  chunk_size = bytestream2_get_be32(&s->gb);
474  if ((res = decode_format80(&s->gb, chunk_size,
475  s->decode_buffer, s->decode_buffer_size, 1)) < 0)
476  return res;
477 
478  /* render the final PAL8 frame */
479  if (s->vector_height == 4)
480  index_shift = 4;
481  else
482  index_shift = 3;
483  for (y = 0; y < s->height; y += s->vector_height) {
484  for (x = 0; x < s->width; x += 4, lobytes++, hibytes++) {
485  pixel_ptr = y * s->frame.linesize[0] + x;
486 
487  /* get the vector index, the method for which varies according to
488  * VQA file version */
489  switch (s->vqa_version) {
490 
491  case 1:
492  lobyte = s->decode_buffer[lobytes * 2];
493  hibyte = s->decode_buffer[(lobytes * 2) + 1];
494  vector_index = ((hibyte << 8) | lobyte) >> 3;
495  vector_index <<= index_shift;
496  lines = s->vector_height;
497  /* uniform color fill - a quick hack */
498  if (hibyte == 0xFF) {
499  while (lines--) {
500  s->frame.data[0][pixel_ptr + 0] = 255 - lobyte;
501  s->frame.data[0][pixel_ptr + 1] = 255 - lobyte;
502  s->frame.data[0][pixel_ptr + 2] = 255 - lobyte;
503  s->frame.data[0][pixel_ptr + 3] = 255 - lobyte;
504  pixel_ptr += s->frame.linesize[0];
505  }
506  lines=0;
507  }
508  break;
509 
510  case 2:
511  lobyte = s->decode_buffer[lobytes];
512  hibyte = s->decode_buffer[hibytes];
513  vector_index = (hibyte << 8) | lobyte;
514  vector_index <<= index_shift;
515  lines = s->vector_height;
516  break;
517 
518  case 3:
519 /* not implemented yet */
520  lines = 0;
521  break;
522  }
523 
524  while (lines--) {
525  s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
526  s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
527  s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
528  s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
529  pixel_ptr += s->frame.linesize[0];
530  }
531  }
532  }
533 
534  /* handle partial codebook */
535  if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
536  /* a chunk should not have both chunk types */
537  av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n");
538  return AVERROR_INVALIDDATA;
539  }
540 
541  if (cbp0_chunk != -1) {
542 
543  bytestream2_seek(&s->gb, cbp0_chunk, SEEK_SET);
544  chunk_size = bytestream2_get_be32(&s->gb);
545 
546  if (chunk_size > MAX_CODEBOOK_SIZE - s->next_codebook_buffer_index) {
547  av_log(s->avctx, AV_LOG_ERROR, "cbp0 chunk too large (%u bytes)\n",
548  chunk_size);
549  return AVERROR_INVALIDDATA;
550  }
551 
552  /* accumulate partial codebook */
554  chunk_size);
555  s->next_codebook_buffer_index += chunk_size;
556 
557  s->partial_countdown--;
558  if (s->partial_countdown == 0) {
559 
560  /* time to replace codebook */
561  memcpy(s->codebook, s->next_codebook_buffer,
563 
564  /* reset accounting */
567  }
568  }
569 
570  if (cbpz_chunk != -1) {
571 
572  bytestream2_seek(&s->gb, cbpz_chunk, SEEK_SET);
573  chunk_size = bytestream2_get_be32(&s->gb);
574 
575  if (chunk_size > MAX_CODEBOOK_SIZE - s->next_codebook_buffer_index) {
576  av_log(s->avctx, AV_LOG_ERROR, "cbpz chunk too large (%u bytes)\n",
577  chunk_size);
578  return AVERROR_INVALIDDATA;
579  }
580 
581  /* accumulate partial codebook */
583  chunk_size);
584  s->next_codebook_buffer_index += chunk_size;
585 
586  s->partial_countdown--;
587  if (s->partial_countdown == 0) {
588  GetByteContext gb;
589 
591  /* decompress codebook */
592  if ((res = decode_format80(&gb, s->next_codebook_buffer_index,
593  s->codebook, s->codebook_size, 0)) < 0)
594  return res;
595 
596  /* reset accounting */
599  }
600  }
601 
602  return 0;
603 }
604 
606  void *data, int *got_frame,
607  AVPacket *avpkt)
608 {
609  VqaContext *s = avctx->priv_data;
610  int res;
611 
612  if (s->frame.data[0])
613  avctx->release_buffer(avctx, &s->frame);
614 
615  if (ff_get_buffer(avctx, &s->frame)) {
616  av_log(s->avctx, AV_LOG_ERROR, " VQA Video: get_buffer() failed\n");
617  return -1;
618  }
619 
620  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
621  if ((res = vqa_decode_chunk(s)) < 0)
622  return res;
623 
624  /* make the palette available on the way out */
625  memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
626  s->frame.palette_has_changed = 1;
627 
628  *got_frame = 1;
629  *(AVFrame*)data = s->frame;
630 
631  /* report that the buffer was completely consumed */
632  return avpkt->size;
633 }
634 
636 {
637  VqaContext *s = avctx->priv_data;
638 
639  av_freep(&s->codebook);
641  av_freep(&s->decode_buffer);
642 
643  if (s->frame.data[0])
644  avctx->release_buffer(avctx, &s->frame);
645 
646  return 0;
647 }
648 
650  .name = "vqavideo",
651  .type = AVMEDIA_TYPE_VIDEO,
652  .id = AV_CODEC_ID_WS_VQA,
653  .priv_data_size = sizeof(VqaContext),
657  .capabilities = CODEC_CAP_DR1,
658  .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA (Vector Quantized Animation) video"),
659 };