]> git.sesse.net Git - ffmpeg/blob - libavcodec/gdv.c
avcodec/jpeg2000dec: Check nonzerobits more completely
[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 Bits32 {
42     uint32_t queue;
43     uint8_t  fill;
44 } Bits32;
45
46 #define PREAMBLE_SIZE 4096
47
48 static av_cold int gdv_decode_init(AVCodecContext *avctx)
49 {
50     GDVContext *gdv = avctx->priv_data;
51     int i, j, k;
52
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);
56     if (!gdv->frame)
57         return AVERROR(ENOMEM);
58
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;
63             }
64         }
65     }
66
67     return 0;
68 }
69
70 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
71 {
72     int i, j, y, x;
73
74     if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
75         return;
76     }
77
78     if (gdv->scale_h && gdv->scale_v) {
79         for (j = 0; j < h; j++) {
80             int y = h - j - 1;
81             for (i = 0; i < w; i++) {
82                 int x = w - i - 1;
83                 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
84             }
85         }
86     } else if (gdv->scale_h) {
87         for (j = 0; j < h; j++) {
88             int y = h - j - 1;
89             for (x = 0; x < w; x++) {
90                 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x + (y/2) * w];
91             }
92         }
93     } else if (gdv->scale_v) {
94         for (j = 0; j < h; j++) {
95             int y = h - j - 1;
96             for (i = 0; i < w; i++) {
97                 int x = w - i - 1;
98                 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + y * (w/2)];
99             }
100         }
101     }
102
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];
107             }
108         }
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];
113             }
114         }
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];
119             }
120         }
121     }
122
123     gdv->scale_v = scale_v;
124     gdv->scale_h = scale_h;
125 }
126
127 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
128 {
129     bits->queue = bytestream2_get_le32(gb);
130     bits->fill  = 32;
131 }
132
133 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
134 {
135     int res = bits->queue & ((1 << nbits) - 1);
136
137     bits->queue >>= nbits;
138     bits->fill   -= nbits;
139     if (bits->fill <= 16) {
140         bits->queue |= bytestream2_get_le16(gb) << bits->fill;
141         bits->fill  += 16;
142     }
143
144     return res;
145 }
146
147 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
148 {
149     int i;
150
151     if (offset == -1) {
152         int c;
153
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);
158         }
159     } else if (offset < 0) {
160         int start = bytestream2_tell_p(pb) - (-offset);
161
162         bytestream2_seek(g2, start, SEEK_SET);
163         for (i = 0; i < len; i++) {
164             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
165         }
166     } else {
167         int start = bytestream2_tell_p(pb) + offset;
168
169         bytestream2_seek(g2, start, SEEK_SET);
170         for (i = 0; i < len; i++) {
171             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
172         }
173     }
174 }
175
176 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
177 {
178     GDVContext *gdv = avctx->priv_data;
179     GetByteContext *gb = &gdv->gb;
180     GetByteContext *g2 = &gdv->g2;
181     PutByteContext *pb = &gdv->pb;
182     Bits32 bits;
183
184     bytestream2_init(g2, gdv->frame, gdv->frame_size);
185     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
186     fill_bits32(&bits, gb);
187
188     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
189         int tag = read_bits32(&bits, gb, 2);
190         if (tag == 0) {
191             int b = read_bits32(&bits, gb, 1);
192             if (b == 0) {
193                 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
194             } else {
195                 int i, len = 2;
196                 int lbits = 0;
197                 while (1) {
198                     int val;
199
200                     lbits += 1;
201                     val = read_bits32(&bits, gb, lbits);
202                     len += val;
203                     if (val != ((1 << lbits) - 1)) {
204                         break;
205                     }
206                     assert(lbits < 16);
207                 }
208                 for (i = 0; i < len; i++) {
209                     bytestream2_put_byte(pb, bytestream2_get_byte(gb));
210                 }
211             }
212         } else if (tag == 1) {
213             int b = read_bits32(&bits, gb, 1);
214             int len;
215
216             if (b == 0) {
217                 len = (read_bits32(&bits, gb, 4)) + 2;
218             } else {
219                 int bb = bytestream2_get_byte(gb);
220                 if ((bb & 0x80) == 0) {
221                     len = bb + 18;
222                 } else {
223                     int top = (bb & 0x7F) << 8;
224                     len = top + bytestream2_get_byte(gb) + 146;
225                 }
226             }
227             bytestream2_skip_p(pb, len);
228         } else if (tag == 2) {
229             int i, subtag = read_bits32(&bits, gb, 2);
230
231             if (subtag != 3) {
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);
237                 } else {
238                     int real_off, len, c1, c2;
239
240                     if (offs == 0xFFF) {
241                         return 0;
242                     }
243
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);
251                     }
252                 }
253             } else {
254                 int b = bytestream2_get_byte(gb);
255                 int off = ((b & 0x7F)) + 1;
256                 int len = ((b & 0x80) == 0) ? 2 : 3;
257
258                 lz_copy(pb, g2, -off, len);
259             }
260         } else {
261             int len;
262             int off;
263             if (use8) {
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;
269                 } else {
270                     int ofs1;
271                     if ((b & 0x80) == 0) {
272                         len = ((b >> 4)) + 6;
273                         ofs1 = (b & 0xF);
274                     } else {
275                         len = ((b & 0x3F)) + 14;
276                         ofs1 = read_bits32(&bits, gb, 4);
277                     }
278                     off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
279                 }
280             } else {
281                 int ofs1, b = bytestream2_get_byte(gb);
282
283                 if ((b >> 4) == 0xF) {
284                     len = bytestream2_get_byte(gb) + 21;
285                 } else {
286                     len = (b >> 4) + 6;
287                 }
288                 ofs1 = (b & 0xF);
289                 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
290             }
291             lz_copy(pb, g2, off, len);
292         }
293     }
294
295     return 0;
296 }
297
298 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
299                             int *got_frame, AVPacket *avpkt)
300 {
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);
307     int compression;
308     unsigned flags;
309     uint8_t *dst;
310
311     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
312         return ret;
313     if (pal && pal_size == AVPALETTE_SIZE)
314         memcpy(gdv->pal, pal, AVPALETTE_SIZE);
315
316     bytestream2_init(gb, avpkt->data, avpkt->size);
317     bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
318
319     flags = bytestream2_get_le32(gb);
320     compression = flags & 0xF;
321
322     rescale(gdv, gdv->frame, avctx->width, avctx->height,
323             !!(flags & 0x10), !!(flags & 0x20));
324
325     switch (compression) {
326     case 1:
327         memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
328     case 0:
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;
334         }
335         break;
336     case 3:
337         break;
338     case 6:
339         ret = decompress_68(avctx, flags >> 8, 0);
340         break;
341     case 8:
342         ret = decompress_68(avctx, flags >> 8, 1);
343         break;
344     default:
345         return AVERROR_INVALIDDATA;
346     }
347
348     memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
349     dst = frame->data[0];
350
351     if (!gdv->scale_v && !gdv->scale_h) {
352         int sidx = PREAMBLE_SIZE, didx = 0;
353         int y, x;
354
355         for (y = 0; y < avctx->height; y++) {
356             for (x = 0; x < avctx->width; x++) {
357                 dst[x+didx] = gdv->frame[x+sidx];
358             }
359             sidx += avctx->width;
360             didx += frame->linesize[0];
361         }
362     } else {
363         int sidx = PREAMBLE_SIZE, didx = 0;
364         int y, x;
365
366         for (y = 0; y < avctx->height; y++) {
367             if (!gdv->scale_v) {
368                 for (x = 0; x < avctx->width; x++) {
369                     dst[didx + x] = gdv->frame[sidx + x];
370                 }
371             } else {
372                 for (x = 0; x < avctx->width; x++) {
373                     dst[didx + x] = gdv->frame[sidx + x/2];
374                 }
375             }
376             if (!gdv->scale_h || ((y & 1) == 1)) {
377                 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
378             }
379             didx += frame->linesize[0];
380         }
381     }
382
383     *got_frame = 1;
384
385     return ret < 0 ? ret : avpkt->size;
386 }
387
388 static av_cold int gdv_decode_close(AVCodecContext *avctx)
389 {
390     GDVContext *gdv = avctx->priv_data;
391     av_freep(&gdv->frame);
392     return 0;
393 }
394
395 AVCodec ff_gdv_decoder = {
396     .name           = "gdv",
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,
406 };