2 * Infinity IMM4 decoder
4 * Copyright (c) 2018 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/thread.h"
31 #include "copy_block.h"
36 #define CBPLO_VLC_BITS 6
37 #define CBPHI_VLC_BITS 6
38 #define BLKTYPE_VLC_BITS 9
39 #define BLOCK_VLC_BITS 12
41 typedef struct IMM4Context {
53 ScanTable intra_scantable;
54 DECLARE_ALIGNED(32, int16_t, block)[6][64];
58 static const uint8_t intra_cb[] = {
62 static const uint8_t inter_cb[] = {
66 static const uint8_t cbplo[][2] = {
67 { 0,-6 }, { 0x01, 6 }, { 0x02, 6 }, { 0x03, 6 }, { 0x00, 4 },
68 { 0x01, 3 }, { 0x02, 3 }, { 0x03, 3 }, { 0x00, 1 },
71 static const uint8_t cbphi_bits[] = {
72 4, 5, 5, 4, 5, 4, 6, 4, 5, 6, 4, 4, 4, 4, 4, 2
75 static const uint8_t cbphi_codes[] = {
76 3, 5, 4, 9, 3, 7, 2, 11, 2, 3, 5, 10, 4, 8, 6, 3
79 static const uint8_t blktype[][2] = {
80 { 0,-8 }, { 0x34, 9 }, { 0,-9 }, { 0x14, 9 }, { 0,-9 },
81 { 0x23, 8 }, { 0x13, 8 }, { 0x32, 8 }, { 0x33, 7 }, { 0x22, 7 },
82 { 0x12, 7 }, { 0x21, 7 }, { 0x11, 7 }, { 0x04, 6 }, { 0x30, 6 },
83 { 0x03, 5 }, { 0x20, 4 }, { 0x10, 4 }, { 0x02, 3 }, { 0x01, 3 },
87 static const uint16_t block_symbols[] = {
88 0, 0x4082, 0x4003, 0x000B, 0x000A, 0x4E01, 0x4D81, 0x4D01, 0x4C81,
89 0x0482, 0x0402, 0x0382, 0x0302, 0x0282, 0x0183, 0x0103, 0x0084, 0x000C,
90 0x0085, 0x0B81, 0x0C01, 0x4E81, 0x4F01, 0x4F81, 0x5001, 0x0086, 0x0104,
91 0x0203, 0x0283, 0x0303, 0x0502, 0x0C81, 0x0D01, 0x5081, 0x5101, 0x5181,
92 0x5201, 0x5281, 0x5301, 0x5381, 0x5401, 0x0000, 0x0009, 0x0008, 0x4C01,
93 0x4B81, 0x4B01, 0x4A81, 0x4A01, 0x4981, 0x4901, 0x4881, 0x4002, 0x0B01,
94 0x0A81, 0x0A01, 0x0981, 0x0901, 0x0881, 0x0801, 0x0781, 0x0202, 0x0182,
95 0x0007, 0x0006, 0x4801, 0x4781, 0x4701, 0x4681, 0x4601, 0x4581, 0x4501,
96 0x4481, 0x0701, 0x0681, 0x0102, 0x0083, 0x0005, 0x4401, 0x4381, 0x4301,
97 0x4281, 0x0601, 0x0581, 0x0501, 0x0004, 0x4201, 0x4181, 0x4101, 0x4081,
98 0x0481, 0x0401, 0x0381, 0x0301, 0x0082, 0x0003, 0x0281, 0x0201, 0x0181,
99 0x4001, 0x0001, 0x0081, 0x0101, 0x0002,
102 static const uint8_t block_bits[] = {
103 -9, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11,
104 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
105 12, 12, 12, 7, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
106 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
107 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
108 6, 5, 5, 5, 4, 2, 3, 4, 4,
111 static VLC cbplo_tab;
112 static VLC cbphi_tab;
113 static VLC blktype_tab;
114 static VLC block_tab;
116 static int get_cbphi(GetBitContext *gb, int x)
120 value = get_vlc2(gb, cbphi_tab.table, CBPHI_VLC_BITS, 1);
122 return AVERROR_INVALIDDATA;
124 return x ? value : 15 - value;
127 static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
128 int block, int factor, int flag, int offset, int flag2)
130 IMM4Context *s = avctx->priv_data;
131 const uint8_t *scantable = s->intra_scantable.permutated;
132 int i, last, len, factor2;
134 for (i = !flag; i < 64; i++) {
137 value = get_vlc2(gb, block_tab.table, BLOCK_VLC_BITS, 1);
139 return AVERROR_INVALIDDATA;
141 last = get_bits1(gb);
142 len = get_bits(gb, 6);
143 factor2 = get_sbits(gb, 8);
145 factor2 = value & 0x7F;
146 last = (value >> 14) & 1;
147 len = (value >> 7) & 0x3F;
154 s->block[block][scantable[i]] = offset * (factor2 < 0 ? -1 : 1) + factor * factor2;
159 if (s->hi == 2 && flag2 && block < 4) {
161 s->block[block][scantable[0]] *= 2;
162 s->block[block][scantable[1]] *= 2;
163 s->block[block][scantable[8]] *= 2;
164 s->block[block][scantable[16]] *= 2;
170 static int decode_blocks(AVCodecContext *avctx, GetBitContext *gb,
171 unsigned cbp, int flag, int offset, unsigned flag2)
173 IMM4Context *s = avctx->priv_data;
174 const uint8_t *scantable = s->intra_scantable.permutated;
177 memset(s->block, 0, sizeof(s->block));
179 for (i = 0; i < 6; i++) {
181 int x = get_bits(gb, 8);
187 s->block[i][scantable[0]] = x;
190 if (cbp & (1 << (5 - i))) {
191 ret = decode_block(avctx, gb, i, s->factor, flag, offset, flag2);
200 static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
202 IMM4Context *s = avctx->priv_data;
203 int ret, x, y, offset = 0;
207 return AVERROR_INVALIDDATA;
208 s->factor = intra_cb[s->lo];
210 s->factor = s->lo * 2;
220 for (y = 0; y < avctx->height; y += 16) {
221 for (x = 0; x < avctx->width; x += 16) {
222 unsigned flag, cbphi, cbplo;
224 cbplo = get_vlc2(gb, cbplo_tab.table, CBPLO_VLC_BITS, 1);
225 flag = get_bits1(gb);
227 cbphi = get_cbphi(gb, 1);
229 ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0, offset, flag);
233 s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
234 frame->linesize[0], s->block[0]);
235 s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
236 frame->linesize[0], s->block[1]);
237 s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
238 frame->linesize[0], s->block[2]);
239 s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
240 frame->linesize[0], s->block[3]);
241 s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
242 frame->linesize[1], s->block[4]);
243 s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
244 frame->linesize[2], s->block[5]);
251 static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
252 AVFrame *frame, AVFrame *prev)
254 IMM4Context *s = avctx->priv_data;
255 int ret, x, y, offset = 0;
259 return AVERROR_INVALIDDATA;
260 s->factor = inter_cb[s->lo];
262 s->factor = s->lo * 2;
272 for (y = 0; y < avctx->height; y += 16) {
273 for (x = 0; x < avctx->width; x += 16) {
274 int reverse, intra_block, value;
275 unsigned cbphi, cbplo, flag2 = 0;
278 copy_block16(frame->data[0] + y * frame->linesize[0] + x,
279 prev->data[0] + y * prev->linesize[0] + x,
280 frame->linesize[0], prev->linesize[0], 16);
281 copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
282 prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1),
283 frame->linesize[1], prev->linesize[1], 8);
284 copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
285 prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1),
286 frame->linesize[2], prev->linesize[2], 8);
290 value = get_vlc2(gb, blktype_tab.table, BLKTYPE_VLC_BITS, 1);
292 return AVERROR_INVALIDDATA;
294 intra_block = value & 0x07;
295 reverse = intra_block == 3;
297 flag2 = get_bits1(gb);
300 cbphi = get_cbphi(gb, reverse);
302 ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0, offset, flag2);
306 s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
307 frame->linesize[0], s->block[0]);
308 s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
309 frame->linesize[0], s->block[1]);
310 s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
311 frame->linesize[0], s->block[2]);
312 s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
313 frame->linesize[0], s->block[3]);
314 s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
315 frame->linesize[1], s->block[4]);
316 s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
317 frame->linesize[2], s->block[5]);
319 flag2 = get_bits1(gb);
321 ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 1, offset, flag2);
325 copy_block16(frame->data[0] + y * frame->linesize[0] + x,
326 prev->data[0] + y * prev->linesize[0] + x,
327 frame->linesize[0], prev->linesize[0], 16);
328 copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
329 prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1),
330 frame->linesize[1], prev->linesize[1], 8);
331 copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
332 prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1),
333 frame->linesize[2], prev->linesize[2], 8);
335 s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x,
336 frame->linesize[0], s->block[0]);
337 s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x + 8,
338 frame->linesize[0], s->block[1]);
339 s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x,
340 frame->linesize[0], s->block[2]);
341 s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
342 frame->linesize[0], s->block[3]);
343 s->idsp.idct_add(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
344 frame->linesize[1], s->block[4]);
345 s->idsp.idct_add(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
346 frame->linesize[2], s->block[5]);
354 static int decode_frame(AVCodecContext *avctx, void *data,
355 int *got_frame, AVPacket *avpkt)
357 IMM4Context *s = avctx->priv_data;
358 GetBitContext *gb = &s->gb;
359 AVFrame *frame = data;
364 if (avpkt->size <= 32)
365 return AVERROR_INVALIDDATA;
367 av_fast_padded_malloc(&s->bitstream, &s->bitstream_size,
368 FFALIGN(avpkt->size, 4));
370 return AVERROR(ENOMEM);
372 s->bdsp.bswap_buf((uint32_t *)s->bitstream,
373 (uint32_t *)avpkt->data,
374 (avpkt->size + 3) >> 2);
376 if ((ret = init_get_bits8(gb, s->bitstream, FFALIGN(avpkt->size, 4))) < 0)
379 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
380 avctx->color_range = AVCOL_RANGE_JPEG;
382 width = avctx->width;
383 height = avctx->height;
385 scaled = avpkt->data[8];
387 int mode = avpkt->data[10];
417 skip_bits_long(gb, 24 * 8);
418 type = get_bits_long(gb, 32);
419 s->hi = get_bits(gb, 16);
420 s->lo = get_bits(gb, 16);
424 frame->key_frame = 1;
425 frame->pict_type = AV_PICTURE_TYPE_I;
428 frame->key_frame = 0;
429 frame->pict_type = AV_PICTURE_TYPE_P;
432 avpriv_request_sample(avctx, "type %X", type);
433 return AVERROR_PATCHWELCOME;
436 if (avctx->width != width ||
437 avctx->height != height) {
438 if (!frame->key_frame) {
439 av_log(avctx, AV_LOG_ERROR, "Frame size change is unsupported.\n");
440 return AVERROR_INVALIDDATA;
442 av_frame_unref(s->prev_frame);
445 ret = ff_set_dimensions(avctx, width, height);
449 if ((ret = ff_get_buffer(avctx, frame, frame->key_frame ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
452 if (frame->key_frame) {
453 ret = decode_intra(avctx, gb, frame);
457 av_frame_unref(s->prev_frame);
458 if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
461 if (!s->prev_frame->data[0]) {
462 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
463 return AVERROR_INVALIDDATA;
466 ret = decode_inter(avctx, gb, frame, s->prev_frame);
476 static av_cold void imm4_init_static_data(void)
478 INIT_VLC_STATIC_FROM_LENGTHS(&cbplo_tab, CBPLO_VLC_BITS, FF_ARRAY_ELEMS(cbplo),
479 &cbplo[0][1], 2, &cbplo[0][0], 2, 1,
480 0, 0, 1 << CBPLO_VLC_BITS);
482 INIT_VLC_SPARSE_STATIC(&cbphi_tab, CBPHI_VLC_BITS, FF_ARRAY_ELEMS(cbphi_bits),
483 cbphi_bits, 1, 1, cbphi_codes, 1, 1, NULL, 0, 0, 64);
485 INIT_VLC_STATIC_FROM_LENGTHS(&blktype_tab, BLKTYPE_VLC_BITS, FF_ARRAY_ELEMS(blktype),
486 &blktype[0][1], 2, &blktype[0][0], 2, 1,
487 0, 0, 1 << BLKTYPE_VLC_BITS);
489 INIT_VLC_STATIC_FROM_LENGTHS(&block_tab, BLOCK_VLC_BITS, FF_ARRAY_ELEMS(block_bits),
490 block_bits, 1, block_symbols, 2, 2,
491 0, 0, 1 << BLOCK_VLC_BITS);
494 static av_cold int decode_init(AVCodecContext *avctx)
496 static AVOnce init_static_once = AV_ONCE_INIT;
497 IMM4Context *s = avctx->priv_data;
500 for (int i = 0; i < 64; i++)
503 ff_bswapdsp_init(&s->bdsp);
504 ff_idctdsp_init(&s->idsp, avctx);
505 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, table);
507 s->prev_frame = av_frame_alloc();
509 return AVERROR(ENOMEM);
511 ff_thread_once(&init_static_once, imm4_init_static_data);
516 static void decode_flush(AVCodecContext *avctx)
518 IMM4Context *s = avctx->priv_data;
520 av_frame_unref(s->prev_frame);
523 static av_cold int decode_close(AVCodecContext *avctx)
525 IMM4Context *s = avctx->priv_data;
527 av_frame_free(&s->prev_frame);
528 av_freep(&s->bitstream);
529 s->bitstream_size = 0;
534 AVCodec ff_imm4_decoder = {
536 .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM4"),
537 .type = AVMEDIA_TYPE_VIDEO,
538 .id = AV_CODEC_ID_IMM4,
539 .priv_data_size = sizeof(IMM4Context),
541 .close = decode_close,
542 .decode = decode_frame,
543 .flush = decode_flush,
544 .capabilities = AV_CODEC_CAP_DR1,
545 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
546 FF_CODEC_CAP_INIT_CLEANUP,