]> git.sesse.net Git - ffmpeg/blob - libavcodec/gdv.c
avcodec/gdv: Simplify first inner loop in rescale()
[ffmpeg] / libavcodec / gdv.c
1 /*
2  * Gremlin Digital Video (GDV) decoder
3  * Copyright (c) 2017 Konstantin Shishkov
4  * Copyright (c) 2017 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "libavutil/common.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27
28 typedef struct GDVContext {
29     AVCodecContext *avctx;
30
31     GetByteContext gb;
32     GetByteContext g2;
33     PutByteContext pb;
34
35     uint32_t pal[256];
36     uint8_t *frame;
37     unsigned frame_size;
38     unsigned scale_h, scale_v;
39 } GDVContext;
40
41 typedef struct Bits8 {
42     uint8_t queue;
43     uint8_t fill;
44 } Bits8;
45
46 typedef struct Bits32 {
47     uint32_t queue;
48     uint8_t  fill;
49 } Bits32;
50
51 #define PREAMBLE_SIZE 4096
52
53 static av_cold int gdv_decode_init(AVCodecContext *avctx)
54 {
55     GDVContext *gdv = avctx->priv_data;
56     int i, j, k;
57
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);
61     if (!gdv->frame)
62         return AVERROR(ENOMEM);
63
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;
68             }
69         }
70     }
71
72     return 0;
73 }
74
75 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
76 {
77     int i, j, y, x;
78
79     if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
80         return;
81     }
82
83     if (gdv->scale_v) {
84         for (j = 0; j < h; j++) {
85             int y = h - j - 1;
86             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
87             uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
88             for (x = w - 1; x >= 0; x--) {
89                 dst1[x] = src1[(x>>1)];
90             }
91         }
92     } else if (gdv->scale_h) {
93         for (j = 0; j < h; j++) {
94             int y = h - j - 1;
95             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
96             uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
97             memcpy(dst1, src1, w);
98         }
99     }
100
101     if (scale_h && scale_v) {
102         for (y = 0; y < (h>>1); y++) {
103             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
104             uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
105             for (x = 0; x < (w>>1); x++) {
106                 dst1[x] = src1[x*2];
107             }
108         }
109     } else if (scale_h) {
110         for (y = 0; y < (h>>1); y++) {
111             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
112             uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
113             memcpy(dst1, src1, w);
114         }
115     } else if (scale_v) {
116         for (y = 0; y < h; y++) {
117             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
118             for (x = 0; x < (w>>1); x++) {
119                 dst1[x] = dst1[x*2];
120             }
121         }
122     }
123
124     gdv->scale_v = scale_v;
125     gdv->scale_h = scale_h;
126 }
127
128 static int read_bits2(Bits8 *bits, GetByteContext *gb)
129 {
130     int res;
131
132     if (bits->fill == 0) {
133         bits->queue |= bytestream2_get_byte(gb);
134         bits->fill   = 8;
135     }
136     res = bits->queue >> 6;
137     bits->queue <<= 2;
138     bits->fill   -= 2;
139
140     return res;
141 }
142
143 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
144 {
145     bits->queue = bytestream2_get_le32(gb);
146     bits->fill  = 32;
147 }
148
149 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
150 {
151     int res = bits->queue & ((1 << nbits) - 1);
152
153     bits->queue >>= nbits;
154     bits->fill   -= nbits;
155     if (bits->fill <= 16) {
156         bits->queue |= bytestream2_get_le16(gb) << bits->fill;
157         bits->fill  += 16;
158     }
159
160     return res;
161 }
162
163 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
164 {
165     int i;
166
167     if (offset == -1) {
168         int c;
169
170         bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
171         c = bytestream2_get_byte(g2);
172         for (i = 0; i < len; i++) {
173             bytestream2_put_byte(pb, c);
174         }
175     } else if (offset < 0) {
176         int start = bytestream2_tell_p(pb) - (-offset);
177
178         bytestream2_seek(g2, start, SEEK_SET);
179         for (i = 0; i < len; i++) {
180             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
181         }
182     } else {
183         int start = bytestream2_tell_p(pb) + offset;
184
185         bytestream2_seek(g2, start, SEEK_SET);
186         for (i = 0; i < len; i++) {
187             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
188         }
189     }
190 }
191
192 static int decompress_2(AVCodecContext *avctx)
193 {
194     GDVContext *gdv = avctx->priv_data;
195     GetByteContext *gb = &gdv->gb;
196     GetByteContext *g2 = &gdv->g2;
197     PutByteContext *pb = &gdv->pb;
198     Bits8 bits = { 0 };
199     int c, i;
200
201     bytestream2_init(g2, gdv->frame, gdv->frame_size);
202     bytestream2_skip_p(pb, PREAMBLE_SIZE);
203
204     for (c = 0; c < 256; c++) {
205         for (i = 0; i < 16; i++) {
206             gdv->frame[c * 16 + i] = c;
207         }
208     }
209
210     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
211         int tag = read_bits2(&bits, gb);
212         if (tag == 0) {
213             bytestream2_put_byte(pb, bytestream2_get_byte(gb));
214         } else if (tag == 1) {
215             int b = bytestream2_get_byte(gb);
216             int len = (b & 0xF) + 3;
217             int top = (b >> 4) & 0xF;
218             int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
219             lz_copy(pb, g2, off, len);
220         } else if (tag == 2) {
221             int len = (bytestream2_get_byte(gb)) + 2;
222             bytestream2_skip_p(pb, len);
223         } else {
224             break;
225         }
226     }
227     return 0;
228 }
229
230 static int decompress_5(AVCodecContext *avctx, unsigned skip)
231 {
232     GDVContext *gdv = avctx->priv_data;
233     GetByteContext *gb = &gdv->gb;
234     GetByteContext *g2 = &gdv->g2;
235     PutByteContext *pb = &gdv->pb;
236     Bits8 bits = { 0 };
237
238     bytestream2_init(g2, gdv->frame, gdv->frame_size);
239     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
240
241     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
242         int tag = read_bits2(&bits, gb);
243         if (tag == 0) {
244             bytestream2_put_byte(pb, bytestream2_get_byte(gb));
245         } else if (tag == 1) {
246             int b = bytestream2_get_byte(gb);
247             int len = (b & 0xF) + 3;
248             int top = b >> 4;
249             int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
250             lz_copy(pb, g2, off, len);
251         } else if (tag == 2) {
252             int len;
253             int b = bytestream2_get_byte(gb);
254             if (b == 0) {
255                 break;
256             }
257             if (b != 0xFF) {
258                 len = b;
259             } else {
260                 len = bytestream2_get_le16(gb);
261             }
262             bytestream2_skip_p(pb, len + 1);
263         } else {
264             int b = bytestream2_get_byte(gb);
265             int len = (b & 0x3) + 2;
266             int off = -(b >> 2) - 1;
267             lz_copy(pb, g2, off, len);
268         }
269     }
270     return 0;
271 }
272
273 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
274 {
275     GDVContext *gdv = avctx->priv_data;
276     GetByteContext *gb = &gdv->gb;
277     GetByteContext *g2 = &gdv->g2;
278     PutByteContext *pb = &gdv->pb;
279     Bits32 bits;
280
281     bytestream2_init(g2, gdv->frame, gdv->frame_size);
282     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
283     fill_bits32(&bits, gb);
284
285     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
286         int tag = read_bits32(&bits, gb, 2);
287         if (tag == 0) {
288             int b = read_bits32(&bits, gb, 1);
289             if (b == 0) {
290                 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
291             } else {
292                 int i, len = 2;
293                 int lbits = 0;
294                 while (1) {
295                     int val;
296
297                     lbits += 1;
298                     val = read_bits32(&bits, gb, lbits);
299                     len += val;
300                     if (val != ((1 << lbits) - 1)) {
301                         break;
302                     }
303                     assert(lbits < 16);
304                 }
305                 for (i = 0; i < len; i++) {
306                     bytestream2_put_byte(pb, bytestream2_get_byte(gb));
307                 }
308             }
309         } else if (tag == 1) {
310             int b = read_bits32(&bits, gb, 1);
311             int len;
312
313             if (b == 0) {
314                 len = (read_bits32(&bits, gb, 4)) + 2;
315             } else {
316                 int bb = bytestream2_get_byte(gb);
317                 if ((bb & 0x80) == 0) {
318                     len = bb + 18;
319                 } else {
320                     int top = (bb & 0x7F) << 8;
321                     len = top + bytestream2_get_byte(gb) + 146;
322                 }
323             }
324             bytestream2_skip_p(pb, len);
325         } else if (tag == 2) {
326             int i, subtag = read_bits32(&bits, gb, 2);
327
328             if (subtag != 3) {
329                 int top = (read_bits32(&bits, gb, 4)) << 8;
330                 int offs = top + bytestream2_get_byte(gb);
331                 if ((subtag != 0) || (offs <= 0xF80)) {
332                     int len = (subtag) + 3;
333                     lz_copy(pb, g2, (offs) - 4096, len);
334                 } else {
335                     int real_off, len, c1, c2;
336
337                     if (offs == 0xFFF) {
338                         return 0;
339                     }
340
341                     real_off = ((offs >> 4) & 0x7) + 1;
342                     len = ((offs & 0xF) + 2) * 2;
343                     c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
344                     c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
345                     for (i = 0; i < len/2; i++) {
346                         bytestream2_put_byte(pb, c1);
347                         bytestream2_put_byte(pb, c2);
348                     }
349                 }
350             } else {
351                 int b = bytestream2_get_byte(gb);
352                 int off = ((b & 0x7F)) + 1;
353                 int len = ((b & 0x80) == 0) ? 2 : 3;
354
355                 lz_copy(pb, g2, -off, len);
356             }
357         } else {
358             int len;
359             int off;
360             if (use8) {
361                 int q, b = bytestream2_get_byte(gb);
362                 if ((b & 0xC0) == 0xC0) {
363                     len = ((b & 0x3F)) + 8;
364                     q = read_bits32(&bits, gb, 4);
365                     off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
366                 } else {
367                     int ofs1;
368                     if ((b & 0x80) == 0) {
369                         len = ((b >> 4)) + 6;
370                         ofs1 = (b & 0xF);
371                     } else {
372                         len = ((b & 0x3F)) + 14;
373                         ofs1 = read_bits32(&bits, gb, 4);
374                     }
375                     off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
376                 }
377             } else {
378                 int ofs1, b = bytestream2_get_byte(gb);
379
380                 if ((b >> 4) == 0xF) {
381                     len = bytestream2_get_byte(gb) + 21;
382                 } else {
383                     len = (b >> 4) + 6;
384                 }
385                 ofs1 = (b & 0xF);
386                 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
387             }
388             lz_copy(pb, g2, off, len);
389         }
390     }
391
392     return 0;
393 }
394
395 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
396                             int *got_frame, AVPacket *avpkt)
397 {
398     GDVContext *gdv = avctx->priv_data;
399     GetByteContext *gb = &gdv->gb;
400     PutByteContext *pb = &gdv->pb;
401     AVFrame *frame = data;
402     int ret, i, pal_size;
403     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
404     int compression;
405     unsigned flags;
406     uint8_t *dst;
407
408     bytestream2_init(gb, avpkt->data, avpkt->size);
409     bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
410
411     flags = bytestream2_get_le32(gb);
412     compression = flags & 0xF;
413
414     if (compression == 4 || compression == 7 || compression > 8)
415         return AVERROR_INVALIDDATA;
416
417     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
418         return ret;
419     if (pal && pal_size == AVPALETTE_SIZE)
420         memcpy(gdv->pal, pal, AVPALETTE_SIZE);
421
422     rescale(gdv, gdv->frame, avctx->width, avctx->height,
423             !!(flags & 0x10), !!(flags & 0x20));
424
425     switch (compression) {
426     case 1:
427         memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
428     case 0:
429         if (bytestream2_get_bytes_left(gb) < 256*3)
430             return AVERROR_INVALIDDATA;
431         for (i = 0; i < 256; i++) {
432             unsigned r = bytestream2_get_byte(gb);
433             unsigned g = bytestream2_get_byte(gb);
434             unsigned b = bytestream2_get_byte(gb);
435             gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
436         }
437         break;
438     case 2:
439         ret = decompress_2(avctx);
440         break;
441     case 3:
442         break;
443     case 5:
444         ret = decompress_5(avctx, flags >> 8);
445         break;
446     case 6:
447         ret = decompress_68(avctx, flags >> 8, 0);
448         break;
449     case 8:
450         ret = decompress_68(avctx, flags >> 8, 1);
451         break;
452     default:
453         av_assert0(0);
454     }
455
456     memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
457     dst = frame->data[0];
458
459     if (!gdv->scale_v && !gdv->scale_h) {
460         int sidx = PREAMBLE_SIZE, didx = 0;
461         int y, x;
462
463         for (y = 0; y < avctx->height; y++) {
464             for (x = 0; x < avctx->width; x++) {
465                 dst[x+didx] = gdv->frame[x+sidx];
466             }
467             sidx += avctx->width;
468             didx += frame->linesize[0];
469         }
470     } else {
471         int sidx = PREAMBLE_SIZE, didx = 0;
472         int y, x;
473
474         for (y = 0; y < avctx->height; y++) {
475             if (!gdv->scale_v) {
476                 for (x = 0; x < avctx->width; x++) {
477                     dst[didx + x] = gdv->frame[sidx + x];
478                 }
479             } else {
480                 for (x = 0; x < avctx->width; x++) {
481                     dst[didx + x] = gdv->frame[sidx + x/2];
482                 }
483             }
484             if (!gdv->scale_h || ((y & 1) == 1)) {
485                 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
486             }
487             didx += frame->linesize[0];
488         }
489     }
490
491     *got_frame = 1;
492
493     return ret < 0 ? ret : avpkt->size;
494 }
495
496 static av_cold int gdv_decode_close(AVCodecContext *avctx)
497 {
498     GDVContext *gdv = avctx->priv_data;
499     av_freep(&gdv->frame);
500     return 0;
501 }
502
503 AVCodec ff_gdv_decoder = {
504     .name           = "gdv",
505     .long_name      = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
506     .type           = AVMEDIA_TYPE_VIDEO,
507     .id             = AV_CODEC_ID_GDV,
508     .priv_data_size = sizeof(GDVContext),
509     .init           = gdv_decode_init,
510     .close          = gdv_decode_close,
511     .decode         = gdv_decode_frame,
512     .capabilities   = AV_CODEC_CAP_DR1,
513     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
514 };