75 #define PALETTE_COUNT 256
76 #define VQA_HEADER_SIZE 0x2A
80 #define MAX_CODEBOOK_VECTORS 0xFF00
81 #define SOLID_PIXEL_VECTORS 0x100
82 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
83 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
85 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
86 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
87 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
88 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
89 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
90 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
91 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
124 int i, j, codebook_index;
178 codebook_index = 0xFF00 * 16;
179 for (i = 0; i < 256; i++)
180 for (j = 0; j < 16; j++)
183 codebook_index = 0xF00 * 8;
184 for (i = 0; i < 256; i++)
185 for (j = 0; j < 8; j++)
200 #define CHECK_COUNT() \
201 if (dest_index + count > dest_size) { \
202 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
203 av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
204 dest_index, count, dest_size); \
205 return AVERROR_INVALIDDATA; \
208 #define CHECK_COPY(idx) \
209 if (idx < 0 || idx + count > dest_size) { \
210 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
211 av_log(NULL, AV_LOG_ERROR, " VQA video: current src_pos = %d, count = %d, dest_size = %d\n", \
212 src_pos, count, dest_size); \
213 return AVERROR_INVALIDDATA; \
218 unsigned char *dest,
int dest_size,
int check_size) {
221 int count, opcode, start;
228 opcode = bytestream2_get_byte(gb);
235 if (dest_index >= dest_size) {
236 av_log(
NULL,
AV_LOG_ERROR,
" VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
237 dest_index, dest_size);
241 if (opcode == 0xFF) {
243 count = bytestream2_get_le16(gb);
244 src_pos = bytestream2_get_le16(gb);
245 av_dlog(
NULL,
"(1) copy %X bytes from absolute pos %X\n", count, src_pos);
248 for (i = 0; i < count; i++)
249 dest[dest_index + i] = dest[src_pos + i];
252 }
else if (opcode == 0xFE) {
254 count = bytestream2_get_le16(gb);
255 color = bytestream2_get_byte(gb);
256 av_dlog(
NULL,
"(2) set %X bytes to %02X\n", count, color);
258 memset(&dest[dest_index], color, count);
261 }
else if ((opcode & 0xC0) == 0xC0) {
263 count = (opcode & 0x3F) + 3;
264 src_pos = bytestream2_get_le16(gb);
265 av_dlog(
NULL,
"(3) copy %X bytes from absolute pos %X\n", count, src_pos);
268 for (i = 0; i < count; i++)
269 dest[dest_index + i] = dest[src_pos + i];
272 }
else if (opcode > 0x80) {
274 count = opcode & 0x3F;
275 av_dlog(
NULL,
"(4) copy %X bytes from source to dest\n", count);
282 count = ((opcode & 0x70) >> 4) + 3;
283 src_pos = bytestream2_get_byte(gb) | ((opcode & 0x0F) << 8);
284 av_dlog(
NULL,
"(5) copy %X bytes from relpos %X\n", count, src_pos);
287 for (i = 0; i < count; i++)
288 dest[dest_index + i] = dest[dest_index - src_pos + i];
298 if (dest_index < dest_size)
299 av_log(
NULL,
AV_LOG_ERROR,
" VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
300 dest_index, dest_size);
307 unsigned int chunk_type;
308 unsigned int chunk_size;
310 unsigned int index = 0;
312 unsigned char r,
g,
b;
327 int vector_index = 0;
336 chunk_type = bytestream2_get_be32u(&s->
gb);
338 chunk_size = bytestream2_get_be32u(&s->
gb);
340 switch (chunk_type) {
372 (chunk_type >> 24) & 0xFF,
373 (chunk_type >> 16) & 0xFF,
374 (chunk_type >> 8) & 0xFF,
375 (chunk_type >> 0) & 0xFF,
380 byte_skip = chunk_size & 0x01;
385 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
393 if (cplz_chunk != -1) {
400 if (cpl0_chunk != -1) {
403 chunk_size = bytestream2_get_be32(&s->
gb);
410 for (i = 0; i < chunk_size / 3; i++) {
412 r = bytestream2_get_byteu(&s->
gb) * 4;
413 g = bytestream2_get_byteu(&s->
gb) * 4;
414 b = bytestream2_get_byteu(&s->
gb) * 4;
415 s->
palette[i] = (r << 16) | (g << 8) | (
b);
420 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
428 if (cbfz_chunk != -1) {
431 chunk_size = bytestream2_get_be32(&s->
gb);
438 if (cbf0_chunk != -1) {
441 chunk_size = bytestream2_get_be32(&s->
gb);
453 if (vptz_chunk == -1) {
461 chunk_size = bytestream2_get_be32(&s->
gb);
474 for (x = y; x < y + s->
width; x += 4, lobytes++, hibytes++) {
484 vector_index = ((hibyte << 8) | lobyte) >> 3;
485 vector_index <<= index_shift;
488 if (hibyte == 0xFF) {
490 s->
frame.
data[0][pixel_ptr + 0] = 255 - lobyte;
491 s->
frame.
data[0][pixel_ptr + 1] = 255 - lobyte;
492 s->
frame.
data[0][pixel_ptr + 2] = 255 - lobyte;
493 s->
frame.
data[0][pixel_ptr + 3] = 255 - lobyte;
503 vector_index = (hibyte << 8) | lobyte;
504 vector_index <<= index_shift;
525 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
531 if (cbp0_chunk != -1) {
534 chunk_size = bytestream2_get_be32(&s->
gb);
560 if (cbpz_chunk != -1) {
563 chunk_size = bytestream2_get_be32(&s->
gb);
596 void *
data,
int *data_size,
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int check_size(TiffEncoderContext *s, uint64_t need)
Check free space in buffer.
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static int vqa_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
int next_codebook_buffer_index
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
unsigned char * decode_buffer
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static int init(AVCodecParserContext *s)
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static int decode_format80(GetByteContext *gb, int src_size, unsigned char *dest, int dest_size, int check_size)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static int vqa_decode_chunk(VqaContext *s)
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
struct AVFrame AVFrame
Audio Video Frame.
#define av_dlog(pctx,...)
av_dlog macros Useful to print debug messages that shouldn't get compiled in normally.
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
static av_always_inline int bytestream2_tell(GetByteContext *g)
uint32_t palette[PALETTE_COUNT]
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
static void close(AVCodecParserContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int palette_has_changed
Tell user application that palette has changed from previous frame.
#define MAX_CODEBOOK_SIZE
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static av_cold int vqa_decode_end(AVCodecContext *avctx)
8 bit with PIX_FMT_RGB32 palette
struct VqaContext VqaContext
static const uint8_t color[]
static av_cold int vqa_decode_init(AVCodecContext *avctx)
unsigned char * next_codebook_buffer
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
Log a generic warning message about a missing feature.