2 * Gremlin Digital Video (GDV) decoder
3 * Copyright (c) 2017 Konstantin Shishkov
4 * Copyright (c) 2017 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
23 #include "libavutil/common.h"
25 #include "bytestream.h"
28 typedef struct GDVContext {
29 AVCodecContext *avctx;
38 unsigned scale_h, scale_v;
41 typedef struct Bits8 {
46 typedef struct Bits32 {
51 #define PREAMBLE_SIZE 4096
53 static av_cold int gdv_decode_init(AVCodecContext *avctx)
55 GDVContext *gdv = avctx->priv_data;
58 avctx->pix_fmt = AV_PIX_FMT_PAL8;
59 gdv->frame_size = avctx->width * avctx->height + PREAMBLE_SIZE;
60 gdv->frame = av_calloc(gdv->frame_size, 1);
62 return AVERROR(ENOMEM);
64 for (i = 0; i < 2; i++) {
65 for (j = 0; j < 256; j++) {
66 for (k = 0; k < 8; k++) {
67 gdv->frame[i * 2048 + j * 8 + k] = j;
75 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
79 if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
84 for (j = 0; j < h; j++) {
86 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
87 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
89 for (x = w - 1; x >= 0 && !(x&1); x--) {
90 dst1[x] = src1[(x>>1)];
93 for (x--; x >= 0; x-=2) {
95 dst1[x+1] = src1[(x>>1)];
98 } else if (gdv->scale_h) {
99 for (j = 0; j < h; j++) {
101 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
102 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
103 memcpy(dst1, src1, w);
107 if (scale_h && scale_v) {
108 for (y = 0; y < (h>>1); y++) {
109 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
110 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
111 for (x = 0; x < (w>>1); x++) {
115 } else if (scale_h) {
116 for (y = 0; y < (h>>1); y++) {
117 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
118 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
119 memcpy(dst1, src1, w);
121 } else if (scale_v) {
122 for (y = 0; y < h; y++) {
123 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
124 for (x = 0; x < (w>>1); x++) {
130 gdv->scale_v = scale_v;
131 gdv->scale_h = scale_h;
134 static int read_bits2(Bits8 *bits, GetByteContext *gb)
138 if (bits->fill == 0) {
139 bits->queue |= bytestream2_get_byte(gb);
142 res = bits->queue >> 6;
149 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
151 bits->queue = bytestream2_get_le32(gb);
155 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
157 int res = bits->queue & ((1 << nbits) - 1);
159 bits->queue >>= nbits;
161 if (bits->fill <= 16) {
162 bits->queue |= bytestream2_get_le16(gb) << bits->fill;
169 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
176 bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
177 c = bytestream2_get_byte(g2);
178 for (i = 0; i < len; i++) {
179 bytestream2_put_byte(pb, c);
181 } else if (offset < 0) {
182 int start = bytestream2_tell_p(pb) - (-offset);
184 bytestream2_seek(g2, start, SEEK_SET);
185 for (i = 0; i < len; i++) {
186 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
189 int start = bytestream2_tell_p(pb) + offset;
191 bytestream2_seek(g2, start, SEEK_SET);
192 for (i = 0; i < len; i++) {
193 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
198 static int decompress_2(AVCodecContext *avctx)
200 GDVContext *gdv = avctx->priv_data;
201 GetByteContext *gb = &gdv->gb;
202 GetByteContext *g2 = &gdv->g2;
203 PutByteContext *pb = &gdv->pb;
207 bytestream2_init(g2, gdv->frame, gdv->frame_size);
208 bytestream2_skip_p(pb, PREAMBLE_SIZE);
210 for (c = 0; c < 256; c++) {
211 for (i = 0; i < 16; i++) {
212 gdv->frame[c * 16 + i] = c;
216 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
217 int tag = read_bits2(&bits, gb);
219 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
220 } else if (tag == 1) {
221 int b = bytestream2_get_byte(gb);
222 int len = (b & 0xF) + 3;
223 int top = (b >> 4) & 0xF;
224 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
225 lz_copy(pb, g2, off, len);
226 } else if (tag == 2) {
227 int len = (bytestream2_get_byte(gb)) + 2;
228 bytestream2_skip_p(pb, len);
234 if (bytestream2_get_bytes_left_p(pb) > 0)
235 return AVERROR_INVALIDDATA;
240 static int decompress_5(AVCodecContext *avctx, unsigned skip)
242 GDVContext *gdv = avctx->priv_data;
243 GetByteContext *gb = &gdv->gb;
244 GetByteContext *g2 = &gdv->g2;
245 PutByteContext *pb = &gdv->pb;
248 bytestream2_init(g2, gdv->frame, gdv->frame_size);
249 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
251 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
252 int tag = read_bits2(&bits, gb);
254 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
255 } else if (tag == 1) {
256 int b = bytestream2_get_byte(gb);
257 int len = (b & 0xF) + 3;
259 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
260 lz_copy(pb, g2, off, len);
261 } else if (tag == 2) {
263 int b = bytestream2_get_byte(gb);
270 len = bytestream2_get_le16(gb);
272 bytestream2_skip_p(pb, len + 1);
274 int b = bytestream2_get_byte(gb);
275 int len = (b & 0x3) + 2;
276 int off = -(b >> 2) - 1;
277 lz_copy(pb, g2, off, len);
283 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
285 GDVContext *gdv = avctx->priv_data;
286 GetByteContext *gb = &gdv->gb;
287 GetByteContext *g2 = &gdv->g2;
288 PutByteContext *pb = &gdv->pb;
291 bytestream2_init(g2, gdv->frame, gdv->frame_size);
292 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
293 fill_bits32(&bits, gb);
295 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
296 int tag = read_bits32(&bits, gb, 2);
298 int b = read_bits32(&bits, gb, 1);
300 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
308 val = read_bits32(&bits, gb, lbits);
310 if (val != ((1 << lbits) - 1)) {
315 for (i = 0; i < len; i++) {
316 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
319 } else if (tag == 1) {
320 int b = read_bits32(&bits, gb, 1);
324 len = (read_bits32(&bits, gb, 4)) + 2;
326 int bb = bytestream2_get_byte(gb);
327 if ((bb & 0x80) == 0) {
330 int top = (bb & 0x7F) << 8;
331 len = top + bytestream2_get_byte(gb) + 146;
334 bytestream2_skip_p(pb, len);
335 } else if (tag == 2) {
336 int i, subtag = read_bits32(&bits, gb, 2);
339 int top = (read_bits32(&bits, gb, 4)) << 8;
340 int offs = top + bytestream2_get_byte(gb);
341 if ((subtag != 0) || (offs <= 0xF80)) {
342 int len = (subtag) + 3;
343 lz_copy(pb, g2, (offs) - 4096, len);
345 int real_off, len, c1, c2;
351 real_off = ((offs >> 4) & 0x7) + 1;
352 len = ((offs & 0xF) + 2) * 2;
353 c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
354 c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
355 for (i = 0; i < len/2; i++) {
356 bytestream2_put_byte(pb, c1);
357 bytestream2_put_byte(pb, c2);
361 int b = bytestream2_get_byte(gb);
362 int off = ((b & 0x7F)) + 1;
363 int len = ((b & 0x80) == 0) ? 2 : 3;
365 lz_copy(pb, g2, -off, len);
371 int q, b = bytestream2_get_byte(gb);
372 if ((b & 0xC0) == 0xC0) {
373 len = ((b & 0x3F)) + 8;
374 q = read_bits32(&bits, gb, 4);
375 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
378 if ((b & 0x80) == 0) {
379 len = ((b >> 4)) + 6;
382 len = ((b & 0x3F)) + 14;
383 ofs1 = read_bits32(&bits, gb, 4);
385 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
388 int ofs1, b = bytestream2_get_byte(gb);
390 if ((b >> 4) == 0xF) {
391 len = bytestream2_get_byte(gb) + 21;
396 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
398 lz_copy(pb, g2, off, len);
405 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
406 int *got_frame, AVPacket *avpkt)
408 GDVContext *gdv = avctx->priv_data;
409 GetByteContext *gb = &gdv->gb;
410 PutByteContext *pb = &gdv->pb;
411 AVFrame *frame = data;
412 int ret, i, pal_size;
413 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
418 bytestream2_init(gb, avpkt->data, avpkt->size);
419 bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
421 flags = bytestream2_get_le32(gb);
422 compression = flags & 0xF;
424 if (compression == 4 || compression == 7 || compression > 8)
425 return AVERROR_INVALIDDATA;
427 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
429 if (pal && pal_size == AVPALETTE_SIZE)
430 memcpy(gdv->pal, pal, AVPALETTE_SIZE);
432 rescale(gdv, gdv->frame, avctx->width, avctx->height,
433 !!(flags & 0x10), !!(flags & 0x20));
435 switch (compression) {
437 memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
439 if (bytestream2_get_bytes_left(gb) < 256*3)
440 return AVERROR_INVALIDDATA;
441 for (i = 0; i < 256; i++) {
442 unsigned r = bytestream2_get_byte(gb);
443 unsigned g = bytestream2_get_byte(gb);
444 unsigned b = bytestream2_get_byte(gb);
445 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
449 ret = decompress_2(avctx);
454 ret = decompress_5(avctx, flags >> 8);
457 ret = decompress_68(avctx, flags >> 8, 0);
460 ret = decompress_68(avctx, flags >> 8, 1);
468 memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
469 dst = frame->data[0];
471 if (!gdv->scale_v && !gdv->scale_h) {
472 int sidx = PREAMBLE_SIZE, didx = 0;
475 for (y = 0; y < avctx->height; y++) {
476 for (x = 0; x < avctx->width; x++) {
477 dst[x+didx] = gdv->frame[x+sidx];
479 sidx += avctx->width;
480 didx += frame->linesize[0];
483 int sidx = PREAMBLE_SIZE, didx = 0;
486 for (y = 0; y < avctx->height; y++) {
488 memcpy(dst + didx, gdv->frame + sidx, avctx->width);
490 for (x = 0; x < avctx->width - 1; x+=2) {
492 dst[didx + x + 1] = gdv->frame[sidx + (x>>1)];
494 for (; x < avctx->width; x++) {
495 dst[didx + x] = gdv->frame[sidx + (x>>1)];
498 if (!gdv->scale_h || ((y & 1) == 1)) {
499 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
501 didx += frame->linesize[0];
507 return ret < 0 ? ret : avpkt->size;
510 static av_cold int gdv_decode_close(AVCodecContext *avctx)
512 GDVContext *gdv = avctx->priv_data;
513 av_freep(&gdv->frame);
517 AVCodec ff_gdv_decoder = {
519 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
520 .type = AVMEDIA_TYPE_VIDEO,
521 .id = AV_CODEC_ID_GDV,
522 .priv_data_size = sizeof(GDVContext),
523 .init = gdv_decode_init,
524 .close = gdv_decode_close,
525 .decode = gdv_decode_frame,
526 .capabilities = AV_CODEC_CAP_DR1,
527 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,