1 /*
2 * Escape 130 Video Decoder
3 * Copyright (C) 2008 Eli Friedman (eli.friedman <at> 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
23
24 #define BITSTREAM_READER_LE
27
28
33
34 /**
35 * Initialize the decoder
36 * @param avctx decoder context
37 * @return 0 success, negative on error
38 */
40 {
43
47 }
48
50
51 return 0;
52 }
53
55 {
57
60
62
63 return 0;
64 }
65
68 // This function reads a maximum of 27 bits,
69 // which is within the padding space
71 return -1;
72
74 if (value)
75 return 0;
76
78 if (value)
80
82 if (value)
83 return value + 7;
84
86 if (value)
87 return value + 262;
88
89 return -1;
90 }
91
92 /**
93 * Decode a single frame
94 * @param avctx decoder context
95 * @param data decoded frame
96 * @param got_frame have decoded frame
97 * @param buf input buffer
98 * @param buf_size input buffer size
99 * @return 0 success, -1 on error
100 */
102 void *
data,
int *got_frame,
104 {
106 int buf_size = avpkt->
size;
108
110 unsigned i;
111
112 uint8_t *old_y, *old_cb, *old_cr,
113 *new_y, *new_cb, *new_cr;
114 unsigned old_y_stride, old_cb_stride, old_cr_stride,
115 new_y_stride, new_cb_stride, new_cr_stride;
116 unsigned total_blocks = avctx->
width * avctx->
height / 4,
117 block_index, row_index = 0;
118 unsigned y[4] = {0},
cb = 16,
cr = 16;
119 unsigned skip = -1;
120 unsigned y_base = 0;
122
124
126
128 return -1;
129
130 // Header; no useful information in here
132
136 return -1;
137 }
138
139 new_y = new_frame.
data[0];
140 new_cb = new_frame.
data[1];
141 new_cr = new_frame.
data[2];
142 new_y_stride = new_frame.
linesize[0];
143 new_cb_stride = new_frame.
linesize[1];
144 new_cr_stride = new_frame.
linesize[2];
151
153 "Strides: %i, %i\n",
154 new_y_stride, new_cb_stride);
155
156 for (block_index = 0; block_index < total_blocks; block_index++) {
157 // Note that this call will make us skip the rest of the blocks
158 // if the frame prematurely ends
159 if (skip == -1)
161
162 if (skip) {
163 if (old_y) {
164 y[0] = old_y[0] / 4;
165 y[1] = old_y[1] / 4;
166 y[2] = old_y[old_y_stride] / 4;
167 y[3] = old_y[old_y_stride+1] / 4;
168 y_base= yb[0];
171 } else {
172 y_base=y[0] = y[1] = y[2] = y[3] = 0;
174 }
175 } else {
178 static const int8_t sign_table[64][4] =
179 { {0, 0, 0, 0},
180 {-1, 1, 0, 0},
181 {1, -1, 0, 0},
182 {-1, 0, 1, 0},
183 {-1, 1, 1, 0},
184 {0, -1, 1, 0},
185 {1, -1, 1, 0},
186 {-1, -1, 1, 0},
187 {1, 0, -1, 0},
188 {0, 1, -1, 0},
189 {1, 1, -1, 0},
190 {-1, 1, -1, 0},
191 {1, -1, -1, 0},
192 {-1, 0, 0, 1},
193 {-1, 1, 0, 1},
194 {0, -1, 0, 1},
195
196 {0, 0, 0, 0},
197 {1, -1, 0, 1},
198 {-1, -1, 0, 1},
199 {-1, 0, 1, 1},
200 {-1, 1, 1, 1},
201 {0, -1, 1, 1},
202 {1, -1, 1, 1},
203 {-1, -1, 1, 1},
204 {0, 0, -1, 1},
205 {1, 0, -1, 1},
206 {-1, 0, -1, 1},
207 {0, 1, -1, 1},
208 {1, 1, -1, 1},
209 {-1, 1, -1, 1},
210 {0, -1, -1, 1},
211 {1, -1, -1, 1},
212
213 {0, 0, 0, 0},
214 {-1, -1, -1, 1},
215 {1, 0, 0, -1},
216 {0, 1, 0, -1},
217 {1, 1, 0, -1},
218 {-1, 1, 0, -1},
219 {1, -1, 0, -1},
220 {0, 0, 1, -1},
221 {1, 0, 1, -1},
222 {-1, 0, 1, -1},
223 {0, 1, 1, -1},
224 {1, 1, 1, -1},
225 {-1, 1, 1, -1},
226 {0, -1, 1, -1},
227 {1, -1, 1, -1},
228 {-1, -1, 1, -1},
229
230 {0, 0, 0, 0},
231 {1, 0, -1, -1},
232 {0, 1, -1, -1},
233 {1, 1, -1, -1},
234 {-1, 1, -1, -1},
235 {1, -1, -1, -1} };
236 unsigned sign_selector =
get_bits(&gb, 6);
237 unsigned difference_selector =
get_bits(&gb, 2);
239 for (i = 0; i < 4; i++) {
240 y[i] = av_clip((int)y_base + offset_table[difference_selector] *
241 sign_table[sign_selector][i], 0, 63);
242 }
246 } else {
247 unsigned adjust_index =
get_bits(&gb, 3);
248 static const int8_t adjust[] = {-4, -3, -2, -1, 1, 2, 3, 4};
249 y_base = (y_base + adjust[adjust_index]) & 63;
250 }
251 for (i = 0; i < 4; i++)
252 y[i] = y_base;
253 }
254
259 } else {
260 unsigned adjust_index =
get_bits(&gb, 3);
261 static const int8_t adjust[2][8] =
262 { { 1, 1, 0, -1, -1, -1, 0, 1 },
263 { 0, 1, 1, 1, 0, -1, -1, -1 } };
264 cb = (
cb + adjust[0][adjust_index]) & 31;
265 cr = (
cr + adjust[1][adjust_index]) & 31;
266 }
267 }
268 }
269 *yb++= y_base;
270
271 new_y[0] = y[0] * 4;
272 new_y[1] = y[1] * 4;
273 new_y[new_y_stride] = y[2] * 4;
274 new_y[new_y_stride + 1] = y[3] * 4;
277
278 if (old_y)
279 old_y += 2, old_cb++, old_cr++;
280 new_y += 2, new_cb++, new_cr++;
281 row_index++;
282 if (avctx->
width / 2 == row_index) {
283 row_index = 0;
284 if (old_y) {
285 old_y += old_y_stride * 2 - avctx->
width;
286 old_cb += old_cb_stride - avctx->
width / 2;
287 old_cr += old_cr_stride - avctx->
width / 2;
288 }
289 new_y += new_y_stride * 2 - avctx->
width;
290 new_cb += new_cb_stride - avctx->
width / 2;
291 new_cr += new_cr_stride - avctx->
width / 2;
292 }
293
294 skip--;
295 }
296
298 "Escape sizes: %i, %i\n",
300
303
305 *got_frame = 1;
306
307 return buf_size;
308 }
309
310
321 };