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 Bits32 {
46 #define PREAMBLE_SIZE 4096
48 static av_cold int gdv_decode_init(AVCodecContext *avctx)
50 GDVContext *gdv = avctx->priv_data;
53 avctx->pix_fmt = AV_PIX_FMT_PAL8;
54 gdv->frame_size = avctx->width * avctx->height + PREAMBLE_SIZE;
55 gdv->frame = av_calloc(gdv->frame_size, 1);
57 return AVERROR(ENOMEM);
59 for (i = 0; i < 2; i++) {
60 for (j = 0; j < 256; j++) {
61 for (k = 0; k < 8; k++) {
62 gdv->frame[i * 2048 + j * 8 + k] = j;
70 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
74 if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
78 if (gdv->scale_h && gdv->scale_v) {
79 for (j = 0; j < h; j++) {
81 for (i = 0; i < w; i++) {
83 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
86 } else if (gdv->scale_h) {
87 for (j = 0; j < h; j++) {
89 for (x = 0; x < w; x++) {
90 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x + (y/2) * w];
93 } else if (gdv->scale_v) {
94 for (j = 0; j < h; j++) {
96 for (i = 0; i < w; i++) {
98 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + y * (w/2)];
103 if (scale_h && scale_v) {
104 for (y = 0; y < h/2; y++) {
105 for (x = 0; x < w/2; x++) {
106 dst[PREAMBLE_SIZE + x + y * (w/2)] = dst[PREAMBLE_SIZE + x*2 + y*2 * w];
109 } else if (scale_h) {
110 for (y = 0; y < h/2; y++) {
111 for (x = 0; x < w; x++) {
112 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x + y*2 * w];
115 } else if (scale_v) {
116 for (y = 0; y < h; y++) {
117 for (x = 0; x < w/2; x++) {
118 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x*2 + y * w];
123 gdv->scale_v = scale_v;
124 gdv->scale_h = scale_h;
127 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
129 bits->queue = bytestream2_get_le32(gb);
133 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
135 int res = bits->queue & ((1 << nbits) - 1);
137 bits->queue >>= nbits;
139 if (bits->fill <= 16) {
140 bits->queue |= bytestream2_get_le16(gb) << bits->fill;
147 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
154 bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
155 c = bytestream2_get_byte(g2);
156 for (i = 0; i < len; i++) {
157 bytestream2_put_byte(pb, c);
159 } else if (offset < 0) {
160 int start = bytestream2_tell_p(pb) - (-offset);
162 bytestream2_seek(g2, start, SEEK_SET);
163 for (i = 0; i < len; i++) {
164 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
167 int start = bytestream2_tell_p(pb) + offset;
169 bytestream2_seek(g2, start, SEEK_SET);
170 for (i = 0; i < len; i++) {
171 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
176 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
178 GDVContext *gdv = avctx->priv_data;
179 GetByteContext *gb = &gdv->gb;
180 GetByteContext *g2 = &gdv->g2;
181 PutByteContext *pb = &gdv->pb;
184 bytestream2_init(g2, gdv->frame, gdv->frame_size);
185 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
186 fill_bits32(&bits, gb);
188 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
189 int tag = read_bits32(&bits, gb, 2);
191 int b = read_bits32(&bits, gb, 1);
193 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
201 val = read_bits32(&bits, gb, lbits);
203 if (val != ((1 << lbits) - 1)) {
208 for (i = 0; i < len; i++) {
209 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
212 } else if (tag == 1) {
213 int b = read_bits32(&bits, gb, 1);
217 len = (read_bits32(&bits, gb, 4)) + 2;
219 int bb = bytestream2_get_byte(gb);
220 if ((bb & 0x80) == 0) {
223 int top = (bb & 0x7F) << 8;
224 len = top + bytestream2_get_byte(gb) + 146;
227 bytestream2_skip_p(pb, len);
228 } else if (tag == 2) {
229 int i, subtag = read_bits32(&bits, gb, 2);
232 int top = (read_bits32(&bits, gb, 4)) << 8;
233 int offs = top + bytestream2_get_byte(gb);
234 if ((subtag != 0) || (offs <= 0xF80)) {
235 int len = (subtag) + 3;
236 lz_copy(pb, g2, (offs) - 4096, len);
238 int real_off, len, c1, c2;
244 real_off = ((offs >> 4) & 0x7) + 1;
245 len = ((offs & 0xF) + 2) * 2;
246 c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
247 c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
248 for (i = 0; i < len/2; i++) {
249 bytestream2_put_byte(pb, c1);
250 bytestream2_put_byte(pb, c2);
254 int b = bytestream2_get_byte(gb);
255 int off = ((b & 0x7F)) + 1;
256 int len = ((b & 0x80) == 0) ? 2 : 3;
258 lz_copy(pb, g2, -off, len);
264 int q, b = bytestream2_get_byte(gb);
265 if ((b & 0xC0) == 0xC0) {
266 len = ((b & 0x3F)) + 8;
267 q = read_bits32(&bits, gb, 4);
268 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
271 if ((b & 0x80) == 0) {
272 len = ((b >> 4)) + 6;
275 len = ((b & 0x3F)) + 14;
276 ofs1 = read_bits32(&bits, gb, 4);
278 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
281 int ofs1, b = bytestream2_get_byte(gb);
283 if ((b >> 4) == 0xF) {
284 len = bytestream2_get_byte(gb) + 21;
289 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
291 lz_copy(pb, g2, off, len);
298 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
299 int *got_frame, AVPacket *avpkt)
301 GDVContext *gdv = avctx->priv_data;
302 GetByteContext *gb = &gdv->gb;
303 PutByteContext *pb = &gdv->pb;
304 AVFrame *frame = data;
305 int ret, i, pal_size;
306 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
311 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
313 if (pal && pal_size == AVPALETTE_SIZE)
314 memcpy(gdv->pal, pal, AVPALETTE_SIZE);
316 bytestream2_init(gb, avpkt->data, avpkt->size);
317 bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
319 flags = bytestream2_get_le32(gb);
320 compression = flags & 0xF;
322 rescale(gdv, gdv->frame, avctx->width, avctx->height,
323 !!(flags & 0x10), !!(flags & 0x20));
325 switch (compression) {
327 memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
329 for (i = 0; i < 256; i++) {
330 unsigned r = bytestream2_get_byte(gb);
331 unsigned g = bytestream2_get_byte(gb);
332 unsigned b = bytestream2_get_byte(gb);
333 gdv->pal[i] = 0xFF << 24 | r << 18 | g << 10 | b << 2;
339 ret = decompress_68(avctx, flags >> 8, 0);
342 ret = decompress_68(avctx, flags >> 8, 1);
345 return AVERROR_INVALIDDATA;
348 memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
349 dst = frame->data[0];
351 if (!gdv->scale_v && !gdv->scale_h) {
352 int sidx = PREAMBLE_SIZE, didx = 0;
355 for (y = 0; y < avctx->height; y++) {
356 for (x = 0; x < avctx->width; x++) {
357 dst[x+didx] = gdv->frame[x+sidx];
359 sidx += avctx->width;
360 didx += frame->linesize[0];
363 int sidx = PREAMBLE_SIZE, didx = 0;
366 for (y = 0; y < avctx->height; y++) {
368 for (x = 0; x < avctx->width; x++) {
369 dst[didx + x] = gdv->frame[sidx + x];
372 for (x = 0; x < avctx->width; x++) {
373 dst[didx + x] = gdv->frame[sidx + x/2];
376 if (!gdv->scale_h || ((y & 1) == 1)) {
377 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
379 didx += frame->linesize[0];
385 return ret < 0 ? ret : avpkt->size;
388 static av_cold int gdv_decode_close(AVCodecContext *avctx)
390 GDVContext *gdv = avctx->priv_data;
391 av_freep(&gdv->frame);
395 AVCodec ff_gdv_decoder = {
397 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
398 .type = AVMEDIA_TYPE_VIDEO,
399 .id = AV_CODEC_ID_GDV,
400 .priv_data_size = sizeof(GDVContext),
401 .init = gdv_decode_init,
402 .close = gdv_decode_close,
403 .decode = gdv_decode_frame,
404 .capabilities = AV_CODEC_CAP_DR1,
405 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,