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 #define BITSTREAM_READER_LE
28
33
38
44
47
49
52
53 /**
54 * Initialize the decoder
55 * @param avctx decoder context
56 * @return 0 success, negative on error
57 */
59 {
61
63
64 s->num_superblocks = ((unsigned)avctx->
width / 8) *
65 ((unsigned)avctx->
height / 8);
66
70
71 return 0;
72 }
73
75 {
78
79 for (
i = 0;
i < 3;
i++)
81
83
84 return 0;
85 }
86
89 {
92
96
100 unsigned mask_bits =
get_bits(gb, 4);
104
105 for (j = 0; j < 4; j++)
106 cb.blocks[
i].pixels[j] =
color[(mask_bits>>j) & 1];
107 }
109 }
110
112 {
114 // This function reads a maximum of 23 bits,
115 // which is within the padding space
117 return -1;
121
123 if (
value != (1 + ((1 << 3) - 1)))
125
127 if (
value != (1 + ((1 << 3) - 1)) + ((1 << 7) - 1))
129
131 }
132
134 int* codebook_index, int superblock_index)
135 {
136 // This function reads a maximum of 22 bits; the callers
137 // guard this function appropriately
138 unsigned block_index, depth;
141 static const int8_t transitions[3][2] = { {2, 1}, {0, 2}, {1, 0} };
143 *codebook_index = transitions[*codebook_index][
value];
144 }
145
146 depth =
s->codebooks[*codebook_index].depth;
147
148 // depth = 0 means that this shouldn't read any bits;
149 // in theory, this is the same as get_bits(gb, 0), but
150 // that doesn't actually work.
152
153 if (*codebook_index == 1) {
154 block_index += superblock_index <<
s->codebooks[1].depth;
155 }
156
157 // This condition can occur with invalid bitstreams and
158 // *codebook_index == 2
159 if (block_index >=
s->codebooks[*codebook_index].size || !
s->codebooks[*codebook_index].blocks)
161
162 return s->codebooks[*codebook_index].blocks[block_index];
163 }
164
166 // Formula: ((index / 4) * 16 + (index % 4) * 2) / 2
168
169 // This technically violates C99 aliasing rules, but it should be safe.
170 dst[0] =
mb.pixels32[0];
171 dst[4] =
mb.pixels32[1];
172 }
173
175 uint16_t*
src, ptrdiff_t src_stride)
176 {
177 unsigned y;
179 for (y = 0; y < 8; y++)
180 memcpy(dest + y * dest_stride,
src + y * src_stride,
181 sizeof(uint16_t) * 8);
182 else
183 for (y = 0; y < 8; y++)
184 memset(dest + y * dest_stride, 0, sizeof(uint16_t) * 8);
185 }
186
188 0x4, 0x8, 0x40, 0x80,
189 0x100, 0x200, 0x1000, 0x2000,
190 0x400, 0x800, 0x4000, 0x8000};
191
194 {
195 int buf_size = avpkt->
size;
197
201
202 unsigned superblock_index, cb_index = 1,
203 superblock_col_index = 0,
204 superblocks_per_row = avctx->
width / 8,
skip = -1;
205
206 uint16_t* old_frame_data, *new_frame_data;
207 ptrdiff_t old_stride, new_stride;
208
210
213
214 // This call also guards the potential depth reads for the
215 // codebook unpacking.
216 // Check if the amount we will read minimally is available on input.
217 // The 64 represent the immediately next 2 frame_* elements read, the 23/4320
218 // represent a lower bound of the space needed for skipped superblocks. Non
219 // skipped SBs need more space.
222
225
226 // Leave last frame unchanged
227 // FIXME: Is this necessary? I haven't seen it in any real samples
228 if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) {
229 if (!
s->frame->data[0])
231
233
234 *got_frame = 1;
237
238 return 0;
239 }
240
241 for (
i = 0;
i < 3;
i++) {
242 if (frame_flags & (1 << (17 +
i))) {
243 unsigned cb_depth, cb_size;
245 // This codebook can be cut off at places other than
246 // powers of 2, leaving some of the entries undefined.
248 if (!cb_size) {
251 }
252 cb_depth =
av_log2(cb_size - 1) + 1;
253 } else {
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 if (
s->num_superblocks >= INT_MAX >> cb_depth) {
269 }
270
272 if (cb_size >= INT_MAX / 34 ||
get_bits_left(&gb) < (
int)cb_size * 34)
274
278 if (!
s->codebooks[
i].blocks)
280 }
281 }
282
285
286 new_frame_data = (uint16_t*)
frame->data[0];
287 new_stride =
frame->linesize[0] / 2;
288 old_frame_data = (uint16_t*)
s->frame->data[0];
289 old_stride =
s->frame->linesize[0] / 2;
290
291 for (superblock_index = 0; superblock_index <
s->num_superblocks;
292 superblock_index++) {
295 unsigned multi_mask = 0;
296
298 // Note that this call will make us skip the rest of the blocks
299 // if the frame prematurely ends
301 }
302
305 old_frame_data, old_stride);
306 } else {
308 old_frame_data, old_stride);
309
315 for (
i = 0;
i < 16;
i++) {
318 }
319 }
320 }
321
323 unsigned inv_mask =
get_bits(&gb, 4);
324 for (
i = 0;
i < 4;
i++) {
325 if (inv_mask & (1 <<
i)) {
326 multi_mask ^= 0xF <<
i*4;
327 } else {
329 }
330 }
331
332 for (
i = 0;
i < 16;
i++) {
335 superblock_index);
337 }
338 }
339 } else if (frame_flags & (1 << 16)) {
343 }
344 }
345
347 }
348
349 superblock_col_index++;
350 new_frame_data += 8;
351 if (old_frame_data)
352 old_frame_data += 8;
353 if (superblock_col_index == superblocks_per_row) {
354 new_frame_data += new_stride * 8 - superblocks_per_row * 8;
355 if (old_frame_data)
356 old_frame_data += old_stride * 8 - superblocks_per_row * 8;
357 superblock_col_index = 0;
358 }
360 }
361
363 "Escape sizes: %i, %i, %i\n",
365
368
369 *got_frame = 1;
370
371 return 0;
372 }
373
374
376 .
p.
name =
"escape124",
385 };