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