1/*
2 * Escape 124 Video Decoder
3 * Copyright (C) 2008 Eli Friedman (eli.friedman@gmail.com)
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "internal.h"
24
25#define BITSTREAM_READER_LE
26#include "get_bits.h"
27
28typedef union MacroBlock {
29    uint16_t pixels[4];
30    uint32_t pixels32[2];
31} MacroBlock;
32
33typedef union SuperBlock {
34    uint16_t pixels[64];
35    uint32_t pixels32[32];
36} SuperBlock;
37
38typedef struct CodeBook {
39    unsigned depth;
40    unsigned size;
41    MacroBlock* blocks;
42} CodeBook;
43
44typedef struct Escape124Context {
45    AVFrame *frame;
46
47    unsigned num_superblocks;
48
49    CodeBook codebooks[3];
50} Escape124Context;
51
52/**
53 * Initialize the decoder
54 * @param avctx decoder context
55 * @return 0 success, negative on error
56 */
57static av_cold int escape124_decode_init(AVCodecContext *avctx)
58{
59    Escape124Context *s = avctx->priv_data;
60
61    avctx->pix_fmt = AV_PIX_FMT_RGB555;
62
63    s->num_superblocks = ((unsigned)avctx->width / 8) *
64                         ((unsigned)avctx->height / 8);
65
66    s->frame = av_frame_alloc();
67    if (!s->frame)
68        return AVERROR(ENOMEM);
69
70    return 0;
71}
72
73static av_cold int escape124_decode_close(AVCodecContext *avctx)
74{
75    unsigned i;
76    Escape124Context *s = avctx->priv_data;
77
78    for (i = 0; i < 3; i++)
79        av_free(s->codebooks[i].blocks);
80
81    av_frame_free(&s->frame);
82
83    return 0;
84}
85
86static CodeBook unpack_codebook(GetBitContext* gb, unsigned depth,
87                                 unsigned size)
88{
89    unsigned i, j;
90    CodeBook cb = { 0 };
91
92    if (size >= INT_MAX / 34 || get_bits_left(gb) < size * 34)
93        return cb;
94
95    if (size >= INT_MAX / sizeof(MacroBlock))
96        return cb;
97    cb.blocks = av_malloc(size ? size * sizeof(MacroBlock) : 1);
98    if (!cb.blocks)
99        return cb;
100
101    cb.depth = depth;
102    cb.size = size;
103    for (i = 0; i < size; i++) {
104        unsigned mask_bits = get_bits(gb, 4);
105        unsigned color0 = get_bits(gb, 15);
106        unsigned color1 = get_bits(gb, 15);
107
108        for (j = 0; j < 4; j++) {
109            if (mask_bits & (1 << j))
110                cb.blocks[i].pixels[j] = color1;
111            else
112                cb.blocks[i].pixels[j] = color0;
113        }
114    }
115    return cb;
116}
117
118static unsigned decode_skip_count(GetBitContext* gb)
119{
120    unsigned value;
121    // This function reads a maximum of 23 bits,
122    // which is within the padding space
123    if (get_bits_left(gb) < 1)
124        return -1;
125    value = get_bits1(gb);
126    if (!value)
127        return value;
128
129    value += get_bits(gb, 3);
130    if (value != (1 + ((1 << 3) - 1)))
131        return value;
132
133    value += get_bits(gb, 7);
134    if (value != (1 + ((1 << 3) - 1)) + ((1 << 7) - 1))
135        return value;
136
137    return value + get_bits(gb, 12);
138}
139
140static MacroBlock decode_macroblock(Escape124Context* s, GetBitContext* gb,
141                                    int* codebook_index, int superblock_index)
142{
143    // This function reads a maximum of 22 bits; the callers
144    // guard this function appropriately
145    unsigned block_index, depth;
146
147    if (get_bits1(gb)) {
148        static const char transitions[3][2] = { {2, 1}, {0, 2}, {1, 0} };
149        *codebook_index = transitions[*codebook_index][get_bits1(gb)];
150    }
151
152    depth = s->codebooks[*codebook_index].depth;
153
154    // depth = 0 means that this shouldn't read any bits;
155    // in theory, this is the same as get_bits(gb, 0), but
156    // that doesn't actually work.
157    block_index = depth ? get_bits(gb, depth) : 0;
158
159    if (*codebook_index == 1) {
160        block_index += superblock_index << s->codebooks[1].depth;
161    }
162
163    // This condition can occur with invalid bitstreams and
164    // *codebook_index == 2
165    if (block_index >= s->codebooks[*codebook_index].size)
166        return (MacroBlock) { { 0 } };
167
168    return s->codebooks[*codebook_index].blocks[block_index];
169}
170
171static void insert_mb_into_sb(SuperBlock* sb, MacroBlock mb, unsigned index) {
172   // Formula: ((index / 4) * 16 + (index % 4) * 2) / 2
173   uint32_t *dst = sb->pixels32 + index + (index & -4);
174
175   // This technically violates C99 aliasing rules, but it should be safe.
176   dst[0] = mb.pixels32[0];
177   dst[4] = mb.pixels32[1];
178}
179
180static void copy_superblock(uint16_t* dest, unsigned dest_stride,
181                            uint16_t* src, unsigned src_stride)
182{
183    unsigned y;
184    if (src)
185        for (y = 0; y < 8; y++)
186            memcpy(dest + y * dest_stride, src + y * src_stride,
187                   sizeof(uint16_t) * 8);
188    else
189        for (y = 0; y < 8; y++)
190            memset(dest + y * dest_stride, 0, sizeof(uint16_t) * 8);
191}
192
193static const uint16_t mask_matrix[] = {0x1,   0x2,   0x10,   0x20,
194                                       0x4,   0x8,   0x40,   0x80,
195                                       0x100, 0x200, 0x1000, 0x2000,
196                                       0x400, 0x800, 0x4000, 0x8000};
197
198static int escape124_decode_frame(AVCodecContext *avctx,
199                                  void *data, int *got_frame,
200                                  AVPacket *avpkt)
201{
202    int buf_size = avpkt->size;
203    Escape124Context *s = avctx->priv_data;
204    AVFrame *frame = data;
205
206    GetBitContext gb;
207    unsigned frame_flags, frame_size;
208    unsigned i;
209
210    unsigned superblock_index, cb_index = 1,
211             superblock_col_index = 0,
212             superblocks_per_row = avctx->width / 8, skip = -1;
213
214    uint16_t* old_frame_data, *new_frame_data;
215    unsigned old_stride, new_stride;
216
217    int ret;
218
219    if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
220        return ret;
221
222    // This call also guards the potential depth reads for the
223    // codebook unpacking.
224    if (get_bits_left(&gb) < 64)
225        return -1;
226
227    frame_flags = get_bits_long(&gb, 32);
228    frame_size  = get_bits_long(&gb, 32);
229
230    // Leave last frame unchanged
231    // FIXME: Is this necessary?  I haven't seen it in any real samples
232    if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) {
233        if (!s->frame->data[0])
234            return AVERROR_INVALIDDATA;
235
236        av_log(avctx, AV_LOG_DEBUG, "Skipping frame\n");
237
238        *got_frame = 1;
239        if ((ret = av_frame_ref(frame, s->frame)) < 0)
240            return ret;
241
242        return frame_size;
243    }
244
245    for (i = 0; i < 3; i++) {
246        if (frame_flags & (1 << (17 + i))) {
247            unsigned cb_depth, cb_size;
248            if (i == 2) {
249                // This codebook can be cut off at places other than
250                // powers of 2, leaving some of the entries undefined.
251                cb_size = get_bits_long(&gb, 20);
252                cb_depth = av_log2(cb_size - 1) + 1;
253            } else {
254                cb_depth = get_bits(&gb, 4);
255                if (i == 0) {
256                    // This is the most basic codebook: pow(2,depth) entries
257                    // for a depth-length key
258                    cb_size = 1 << cb_depth;
259                } else {
260                    // This codebook varies per superblock
261                    // FIXME: I don't think this handles integer overflow
262                    // properly
263                    cb_size = s->num_superblocks << cb_depth;
264                }
265            }
266            av_free(s->codebooks[i].blocks);
267            s->codebooks[i] = unpack_codebook(&gb, cb_depth, cb_size);
268            if (!s->codebooks[i].blocks)
269                return -1;
270        }
271    }
272
273    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
274        return ret;
275
276    new_frame_data = (uint16_t*)frame->data[0];
277    new_stride = frame->linesize[0] / 2;
278    old_frame_data = (uint16_t*)s->frame->data[0];
279    old_stride = s->frame->linesize[0] / 2;
280
281    for (superblock_index = 0; superblock_index < s->num_superblocks;
282         superblock_index++) {
283        MacroBlock mb;
284        SuperBlock sb;
285        unsigned multi_mask = 0;
286
287        if (skip == -1) {
288            // Note that this call will make us skip the rest of the blocks
289            // if the frame prematurely ends
290            skip = decode_skip_count(&gb);
291        }
292
293        if (skip) {
294            copy_superblock(new_frame_data, new_stride,
295                            old_frame_data, old_stride);
296        } else {
297            copy_superblock(sb.pixels, 8,
298                            old_frame_data, old_stride);
299
300            while (get_bits_left(&gb) >= 1 && !get_bits1(&gb)) {
301                unsigned mask;
302                mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
303                mask = get_bits(&gb, 16);
304                multi_mask |= mask;
305                for (i = 0; i < 16; i++) {
306                    if (mask & mask_matrix[i]) {
307                        insert_mb_into_sb(&sb, mb, i);
308                    }
309                }
310            }
311
312            if (!get_bits1(&gb)) {
313                unsigned inv_mask = get_bits(&gb, 4);
314                for (i = 0; i < 4; i++) {
315                    if (inv_mask & (1 << i)) {
316                        multi_mask ^= 0xF << i*4;
317                    } else {
318                        multi_mask ^= get_bits(&gb, 4) << i*4;
319                    }
320                }
321
322                for (i = 0; i < 16; i++) {
323                    if (multi_mask & mask_matrix[i]) {
324                        mb = decode_macroblock(s, &gb, &cb_index,
325                                               superblock_index);
326                        insert_mb_into_sb(&sb, mb, i);
327                    }
328                }
329            } else if (frame_flags & (1 << 16)) {
330                while (get_bits_left(&gb) >= 1 && !get_bits1(&gb)) {
331                    mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
332                    insert_mb_into_sb(&sb, mb, get_bits(&gb, 4));
333                }
334            }
335
336            copy_superblock(new_frame_data, new_stride, sb.pixels, 8);
337        }
338
339        superblock_col_index++;
340        new_frame_data += 8;
341        if (old_frame_data)
342            old_frame_data += 8;
343        if (superblock_col_index == superblocks_per_row) {
344            new_frame_data += new_stride * 8 - superblocks_per_row * 8;
345            if (old_frame_data)
346                old_frame_data += old_stride * 8 - superblocks_per_row * 8;
347            superblock_col_index = 0;
348        }
349        skip--;
350    }
351
352    av_log(avctx, AV_LOG_DEBUG,
353           "Escape sizes: %i, %i, %i\n",
354           frame_size, buf_size, get_bits_count(&gb) / 8);
355
356    av_frame_unref(s->frame);
357    if ((ret = av_frame_ref(s->frame, frame)) < 0)
358        return ret;
359
360    *got_frame = 1;
361
362    return frame_size;
363}
364
365
366AVCodec ff_escape124_decoder = {
367    .name           = "escape124",
368    .long_name      = NULL_IF_CONFIG_SMALL("Escape 124"),
369    .type           = AVMEDIA_TYPE_VIDEO,
370    .id             = AV_CODEC_ID_ESCAPE124,
371    .priv_data_size = sizeof(Escape124Context),
372    .init           = escape124_decode_init,
373    .close          = escape124_decode_close,
374    .decode         = escape124_decode_frame,
375    .capabilities   = CODEC_CAP_DR1,
376};
377