/src/ffmpeg/libavcodec/hqx.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Canopus HQX decoder |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include <inttypes.h> |
22 | | |
23 | | #include "libavutil/imgutils.h" |
24 | | #include "libavutil/intreadwrite.h" |
25 | | |
26 | | #include "avcodec.h" |
27 | | #include "canopus.h" |
28 | | #include "codec_internal.h" |
29 | | #include "get_bits.h" |
30 | | #include "thread.h" |
31 | | |
32 | | #include "hqx.h" |
33 | | #include "hqxdsp.h" |
34 | | |
35 | | /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */ |
36 | | enum HQXFormat { |
37 | | HQX_422 = 0, |
38 | | HQX_444, |
39 | | HQX_422A, |
40 | | HQX_444A, |
41 | | }; |
42 | | |
43 | 402k | #define HQX_HEADER_SIZE 59 |
44 | | |
45 | | /* macroblock selects a group of 4 possible quants and |
46 | | * a block can use any of those four quantisers |
47 | | * one column is powers of 2, the other one is powers of 2 * 3, |
48 | | * then there is the special one, powers of 2 * 5 */ |
49 | | static const int hqx_quants[16][4] = { |
50 | | { 0x1, 0x2, 0x4, 0x8 }, { 0x1, 0x3, 0x6, 0xC }, |
51 | | { 0x2, 0x4, 0x8, 0x10 }, { 0x3, 0x6, 0xC, 0x18 }, |
52 | | { 0x4, 0x8, 0x10, 0x20 }, { 0x6, 0xC, 0x18, 0x30 }, |
53 | | { 0x8, 0x10, 0x20, 0x40 }, |
54 | | { 0xA, 0x14, 0x28, 0x50 }, |
55 | | { 0xC, 0x18, 0x30, 0x60 }, |
56 | | { 0x10, 0x20, 0x40, 0x80 }, { 0x18, 0x30, 0x60, 0xC0 }, |
57 | | { 0x20, 0x40, 0x80, 0x100 }, { 0x30, 0x60, 0xC0, 0x180 }, |
58 | | { 0x40, 0x80, 0x100, 0x200 }, { 0x60, 0xC0, 0x180, 0x300 }, |
59 | | { 0x80, 0x100, 0x200, 0x400 } |
60 | | }; |
61 | | |
62 | | static const uint8_t hqx_quant_luma[64] = { |
63 | | 16, 16, 16, 19, 19, 19, 42, 44, |
64 | | 16, 16, 19, 19, 19, 38, 43, 45, |
65 | | 16, 19, 19, 19, 40, 41, 45, 48, |
66 | | 19, 19, 19, 40, 41, 42, 46, 49, |
67 | | 19, 19, 40, 41, 42, 43, 48, 101, |
68 | | 19, 38, 41, 42, 43, 44, 98, 104, |
69 | | 42, 43, 45, 46, 48, 98, 109, 116, |
70 | | 44, 45, 48, 49, 101, 104, 116, 123, |
71 | | }; |
72 | | |
73 | | static const uint8_t hqx_quant_chroma[64] = { |
74 | | 16, 16, 19, 25, 26, 26, 42, 44, |
75 | | 16, 19, 25, 25, 26, 38, 43, 91, |
76 | | 19, 25, 26, 27, 40, 41, 91, 96, |
77 | | 25, 25, 27, 40, 41, 84, 93, 197, |
78 | | 26, 26, 40, 41, 84, 86, 191, 203, |
79 | | 26, 38, 41, 84, 86, 177, 197, 209, |
80 | | 42, 43, 91, 93, 191, 197, 219, 232, |
81 | | 44, 91, 96, 197, 203, 209, 232, 246, |
82 | | }; |
83 | | |
84 | | static inline void put_blocks(HQXContext *ctx, int plane, |
85 | | int x, int y, int ilace, |
86 | | int16_t *block0, int16_t *block1, |
87 | | const uint8_t *quant) |
88 | 3.45M | { |
89 | 3.45M | int fields = ilace ? 2 : 1; |
90 | 3.45M | int lsize = ctx->pic->linesize[plane]; |
91 | 3.45M | uint8_t *p = ctx->pic->data[plane] + x * 2; |
92 | | |
93 | 3.45M | ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize), |
94 | 3.45M | lsize * fields, block0, quant); |
95 | 3.45M | ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize), |
96 | 3.45M | lsize * fields, block1, quant); |
97 | 3.45M | } |
98 | | |
99 | | static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac, |
100 | | int *run, int *lev) |
101 | 358M | { |
102 | 358M | int val; |
103 | | |
104 | 358M | val = show_bits(gb, ac->lut_bits); |
105 | 358M | if (ac->lut[val].bits == -1) { |
106 | 8.81M | GetBitContext gb2 = *gb; |
107 | 8.81M | skip_bits(&gb2, ac->lut_bits); |
108 | 8.81M | val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits); |
109 | 8.81M | } |
110 | 358M | *run = ac->lut[val].run; |
111 | 358M | *lev = ac->lut[val].lev; |
112 | 358M | skip_bits(gb, ac->lut[val].bits); |
113 | 358M | } |
114 | | |
115 | | static int decode_block(GetBitContext *gb, VLC *vlc, |
116 | | const int *quants, int dcb, |
117 | | int16_t block[64], int *last_dc) |
118 | 6.22M | { |
119 | 6.22M | int q, dc; |
120 | 6.22M | int ac_idx; |
121 | 6.22M | int run, lev, pos = 1; |
122 | | |
123 | 6.22M | memset(block, 0, 64 * sizeof(*block)); |
124 | 6.22M | dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2); |
125 | 6.22M | *last_dc += dc; |
126 | | |
127 | 6.22M | block[0] = sign_extend(*last_dc << (12 - dcb), 12); |
128 | | |
129 | 6.22M | q = quants[get_bits(gb, 2)]; |
130 | 6.22M | if (q >= 128) |
131 | 835k | ac_idx = HQX_AC_Q128; |
132 | 5.38M | else if (q >= 64) |
133 | 96.2k | ac_idx = HQX_AC_Q64; |
134 | 5.28M | else if (q >= 32) |
135 | 124k | ac_idx = HQX_AC_Q32; |
136 | 5.16M | else if (q >= 16) |
137 | 156k | ac_idx = HQX_AC_Q16; |
138 | 5.00M | else if (q >= 8) |
139 | 106k | ac_idx = HQX_AC_Q8; |
140 | 4.90M | else |
141 | 4.90M | ac_idx = HQX_AC_Q0; |
142 | | |
143 | 358M | do { |
144 | 358M | hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev); |
145 | 358M | pos += run; |
146 | 358M | if (pos >= 64) |
147 | 327k | break; |
148 | 358M | block[ff_zigzag_direct[pos++]] = lev * q; |
149 | 358M | } while (pos < 64); |
150 | | |
151 | 0 | return 0; |
152 | 6.22M | } |
153 | | |
154 | | static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y) |
155 | 98.0k | { |
156 | 98.0k | HQXSlice *slice = &ctx->slice[slice_no]; |
157 | 98.0k | GetBitContext *gb = &slice->gb; |
158 | 98.0k | const int *quants; |
159 | 98.0k | int flag; |
160 | 98.0k | int last_dc; |
161 | 98.0k | int i, ret; |
162 | | |
163 | 98.0k | if (ctx->interlaced) |
164 | 81.0k | flag = get_bits1(gb); |
165 | 16.9k | else |
166 | 16.9k | flag = 0; |
167 | | |
168 | 98.0k | quants = hqx_quants[get_bits(gb, 4)]; |
169 | | |
170 | 882k | for (i = 0; i < 8; i++) { |
171 | 784k | int vlc_index = ctx->dcb - 9; |
172 | 784k | if (i == 0 || i == 4 || i == 6) |
173 | 294k | last_dc = 0; |
174 | 784k | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, |
175 | 784k | ctx->dcb, slice->block[i], &last_dc); |
176 | 784k | if (ret < 0) |
177 | 0 | return ret; |
178 | 784k | } |
179 | | |
180 | 98.0k | put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[2], hqx_quant_luma); |
181 | 98.0k | put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[3], hqx_quant_luma); |
182 | 98.0k | put_blocks(ctx, 2, x >> 1, y, flag, slice->block[4], slice->block[5], hqx_quant_chroma); |
183 | 98.0k | put_blocks(ctx, 1, x >> 1, y, flag, slice->block[6], slice->block[7], hqx_quant_chroma); |
184 | | |
185 | 98.0k | return 0; |
186 | 98.0k | } |
187 | | |
188 | | static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y) |
189 | 234k | { |
190 | 234k | HQXSlice *slice = &ctx->slice[slice_no]; |
191 | 234k | GetBitContext *gb = &slice->gb; |
192 | 234k | const int *quants; |
193 | 234k | int flag = 0; |
194 | 234k | int last_dc; |
195 | 234k | int i, ret; |
196 | 234k | int cbp; |
197 | | |
198 | 234k | cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1); |
199 | | |
200 | 3.05M | for (i = 0; i < 12; i++) |
201 | 2.81M | memset(slice->block[i], 0, sizeof(**slice->block) * 64); |
202 | 3.05M | for (i = 0; i < 12; i++) |
203 | 2.81M | slice->block[i][0] = -0x800; |
204 | 234k | if (cbp) { |
205 | 233k | if (ctx->interlaced) |
206 | 176k | flag = get_bits1(gb); |
207 | | |
208 | 233k | quants = hqx_quants[get_bits(gb, 4)]; |
209 | | |
210 | 233k | cbp |= cbp << 4; // alpha CBP |
211 | 233k | if (cbp & 0x3) // chroma CBP - top |
212 | 223k | cbp |= 0x500; |
213 | 233k | if (cbp & 0xC) // chroma CBP - bottom |
214 | 211k | cbp |= 0xA00; |
215 | 3.03M | for (i = 0; i < 12; i++) { |
216 | 2.80M | if (i == 0 || i == 4 || i == 8 || i == 10) |
217 | 934k | last_dc = 0; |
218 | 2.80M | if (cbp & (1 << i)) { |
219 | 2.51M | int vlc_index = ctx->dcb - 9; |
220 | 2.51M | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, |
221 | 2.51M | ctx->dcb, slice->block[i], &last_dc); |
222 | 2.51M | if (ret < 0) |
223 | 0 | return ret; |
224 | 2.51M | } |
225 | 2.80M | } |
226 | 233k | } |
227 | | |
228 | 234k | put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma); |
229 | 234k | put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma); |
230 | 234k | put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma); |
231 | 234k | put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma); |
232 | 234k | put_blocks(ctx, 2, x >> 1, y, flag, slice->block[ 8], slice->block[ 9], hqx_quant_chroma); |
233 | 234k | put_blocks(ctx, 1, x >> 1, y, flag, slice->block[10], slice->block[11], hqx_quant_chroma); |
234 | | |
235 | 234k | return 0; |
236 | 234k | } |
237 | | |
238 | | static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y) |
239 | 178k | { |
240 | 178k | HQXSlice *slice = &ctx->slice[slice_no]; |
241 | 178k | GetBitContext *gb = &slice->gb; |
242 | 178k | const int *quants; |
243 | 178k | int flag; |
244 | 178k | int last_dc; |
245 | 178k | int i, ret; |
246 | | |
247 | 178k | if (ctx->interlaced) |
248 | 173k | flag = get_bits1(gb); |
249 | 5.14k | else |
250 | 5.14k | flag = 0; |
251 | | |
252 | 178k | quants = hqx_quants[get_bits(gb, 4)]; |
253 | | |
254 | 2.32M | for (i = 0; i < 12; i++) { |
255 | 2.14M | int vlc_index = ctx->dcb - 9; |
256 | 2.14M | if (i == 0 || i == 4 || i == 8) |
257 | 536k | last_dc = 0; |
258 | 2.14M | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, |
259 | 2.14M | ctx->dcb, slice->block[i], &last_dc); |
260 | 2.14M | if (ret < 0) |
261 | 0 | return ret; |
262 | 2.14M | } |
263 | | |
264 | 178k | put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[ 2], hqx_quant_luma); |
265 | 178k | put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[ 3], hqx_quant_luma); |
266 | 178k | put_blocks(ctx, 2, x, y, flag, slice->block[4], slice->block[ 6], hqx_quant_chroma); |
267 | 178k | put_blocks(ctx, 2, x + 8, y, flag, slice->block[5], slice->block[ 7], hqx_quant_chroma); |
268 | 178k | put_blocks(ctx, 1, x, y, flag, slice->block[8], slice->block[10], hqx_quant_chroma); |
269 | 178k | put_blocks(ctx, 1, x + 8, y, flag, slice->block[9], slice->block[11], hqx_quant_chroma); |
270 | | |
271 | 178k | return 0; |
272 | 178k | } |
273 | | |
274 | | static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y) |
275 | 72.6k | { |
276 | 72.6k | HQXSlice *slice = &ctx->slice[slice_no]; |
277 | 72.6k | GetBitContext *gb = &slice->gb; |
278 | 72.6k | const int *quants; |
279 | 72.6k | int flag = 0; |
280 | 72.6k | int last_dc; |
281 | 72.6k | int i, ret; |
282 | 72.6k | int cbp; |
283 | | |
284 | 72.6k | cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1); |
285 | | |
286 | 1.23M | for (i = 0; i < 16; i++) |
287 | 1.16M | memset(slice->block[i], 0, sizeof(**slice->block) * 64); |
288 | 1.23M | for (i = 0; i < 16; i++) |
289 | 1.16M | slice->block[i][0] = -0x800; |
290 | 72.6k | if (cbp) { |
291 | 64.3k | if (ctx->interlaced) |
292 | 53.7k | flag = get_bits1(gb); |
293 | | |
294 | 64.3k | quants = hqx_quants[get_bits(gb, 4)]; |
295 | | |
296 | 64.3k | cbp |= cbp << 4; // alpha CBP |
297 | 64.3k | cbp |= cbp << 8; // chroma CBP |
298 | 1.09M | for (i = 0; i < 16; i++) { |
299 | 1.02M | if (i == 0 || i == 4 || i == 8 || i == 12) |
300 | 257k | last_dc = 0; |
301 | 1.02M | if (cbp & (1 << i)) { |
302 | 778k | int vlc_index = ctx->dcb - 9; |
303 | 778k | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, |
304 | 778k | ctx->dcb, slice->block[i], &last_dc); |
305 | 778k | if (ret < 0) |
306 | 0 | return ret; |
307 | 778k | } |
308 | 1.02M | } |
309 | 64.3k | } |
310 | | |
311 | 72.6k | put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma); |
312 | 72.6k | put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma); |
313 | 72.6k | put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma); |
314 | 72.6k | put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma); |
315 | 72.6k | put_blocks(ctx, 2, x, y, flag, slice->block[ 8], slice->block[10], hqx_quant_chroma); |
316 | 72.6k | put_blocks(ctx, 2, x + 8, y, flag, slice->block[ 9], slice->block[11], hqx_quant_chroma); |
317 | 72.6k | put_blocks(ctx, 1, x, y, flag, slice->block[12], slice->block[14], hqx_quant_chroma); |
318 | 72.6k | put_blocks(ctx, 1, x + 8, y, flag, slice->block[13], slice->block[15], hqx_quant_chroma); |
319 | | |
320 | 72.6k | return 0; |
321 | 72.6k | } |
322 | | |
323 | | static const int shuffle_16[16] = { |
324 | | 0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12 |
325 | | }; |
326 | | |
327 | | static int decode_slice(HQXContext *ctx, int slice_no) |
328 | 1.49k | { |
329 | 1.49k | int mb_w = (ctx->width + 15) >> 4; |
330 | 1.49k | int mb_h = (ctx->height + 15) >> 4; |
331 | 1.49k | int grp_w = (mb_w + 4) / 5; |
332 | 1.49k | int grp_h = (mb_h + 4) / 5; |
333 | 1.49k | int grp_h_edge = grp_w * (mb_w / grp_w); |
334 | 1.49k | int grp_v_edge = grp_h * (mb_h / grp_h); |
335 | 1.49k | int grp_v_rest = mb_w - grp_h_edge; |
336 | 1.49k | int grp_h_rest = mb_h - grp_v_edge; |
337 | 1.49k | int num_mbs = mb_w * mb_h; |
338 | 1.49k | int num_tiles = (num_mbs + 479) / 480; |
339 | 1.49k | int std_tile_blocks = num_mbs / (16 * num_tiles); |
340 | 1.49k | int g_tile = slice_no * num_tiles; |
341 | 1.49k | int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i; |
342 | 1.49k | int tile_blocks, tile_limit, tile_no; |
343 | | |
344 | 21.8k | for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) { |
345 | 20.3k | tile_blocks = std_tile_blocks; |
346 | 20.3k | tile_limit = -1; |
347 | 20.3k | if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) { |
348 | 14.3k | tile_limit = num_mbs / (16 * num_tiles); |
349 | 14.3k | tile_blocks++; |
350 | 14.3k | } |
351 | 604k | for (i = 0; i < tile_blocks; i++) { |
352 | 584k | if (i == tile_limit) |
353 | 14.3k | blk_addr = g_tile + 16 * num_tiles * i; |
354 | 569k | else |
355 | 569k | blk_addr = tile_no + 16 * num_tiles * i + |
356 | 569k | num_tiles * shuffle_16[(i + slice_no) & 0xF]; |
357 | 584k | loc_row = grp_h * (blk_addr / (grp_h * mb_w)); |
358 | 584k | loc_addr = blk_addr % (grp_h * mb_w); |
359 | 584k | if (loc_row >= grp_v_edge) { |
360 | 34.3k | mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w)); |
361 | 34.3k | pos = loc_addr % (grp_h_rest * grp_w); |
362 | 549k | } else { |
363 | 549k | mb_x = grp_w * (loc_addr / (grp_h * grp_w)); |
364 | 549k | pos = loc_addr % (grp_h * grp_w); |
365 | 549k | } |
366 | 584k | if (mb_x >= grp_h_edge) { |
367 | 81.6k | mb_x += pos % grp_v_rest; |
368 | 81.6k | mb_y = loc_row + (pos / grp_v_rest); |
369 | 502k | } else { |
370 | 502k | mb_x += pos % grp_w; |
371 | 502k | mb_y = loc_row + (pos / grp_w); |
372 | 502k | } |
373 | 584k | ctx->decode_func(ctx, slice_no, mb_x * 16, mb_y * 16); |
374 | 584k | } |
375 | 20.3k | } |
376 | | |
377 | 1.49k | return 0; |
378 | 1.49k | } |
379 | | |
380 | | static int decode_slice_thread(AVCodecContext *avctx, void *arg, |
381 | | int slice_no, int threadnr) |
382 | 187k | { |
383 | 187k | HQXContext *ctx = avctx->priv_data; |
384 | 187k | uint32_t *slice_off = ctx->slice_off; |
385 | 187k | int ret; |
386 | | |
387 | 187k | if (slice_off[slice_no] < HQX_HEADER_SIZE || |
388 | 187k | slice_off[slice_no] >= slice_off[slice_no + 1] || |
389 | 187k | slice_off[slice_no + 1] > ctx->data_size) { |
390 | 186k | av_log(avctx, AV_LOG_ERROR, "Invalid slice size %d.\n", ctx->data_size); |
391 | 186k | return AVERROR_INVALIDDATA; |
392 | 186k | } |
393 | | |
394 | 1.49k | ret = init_get_bits8(&ctx->slice[slice_no].gb, |
395 | 1.49k | ctx->src + slice_off[slice_no], |
396 | 1.49k | slice_off[slice_no + 1] - slice_off[slice_no]); |
397 | 1.49k | if (ret < 0) |
398 | 0 | return ret; |
399 | | |
400 | 1.49k | return decode_slice(ctx, slice_no); |
401 | 1.49k | } |
402 | | |
403 | | static int hqx_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
404 | | int *got_picture_ptr, AVPacket *avpkt) |
405 | 53.2k | { |
406 | 53.2k | HQXContext *ctx = avctx->priv_data; |
407 | 53.2k | const uint8_t *src = avpkt->data; |
408 | 53.2k | uint32_t info_tag; |
409 | 53.2k | int data_start; |
410 | 53.2k | int i, ret; |
411 | | |
412 | 53.2k | if (avpkt->size < 4 + 4) { |
413 | 25.7k | av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size); |
414 | 25.7k | return AVERROR_INVALIDDATA; |
415 | 25.7k | } |
416 | | |
417 | 27.5k | info_tag = AV_RL32(src); |
418 | 27.5k | if (info_tag == MKTAG('I', 'N', 'F', 'O')) { |
419 | 9.66k | uint32_t info_offset = AV_RL32(src + 4); |
420 | 9.66k | if (info_offset > INT_MAX || info_offset + 8 > avpkt->size) { |
421 | 840 | av_log(avctx, AV_LOG_ERROR, |
422 | 840 | "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n", |
423 | 840 | info_offset); |
424 | 840 | return AVERROR_INVALIDDATA; |
425 | 840 | } |
426 | 8.82k | ff_canopus_parse_info_tag(avctx, src + 8, info_offset); |
427 | | |
428 | 8.82k | info_offset += 8; |
429 | 8.82k | src += info_offset; |
430 | 8.82k | } |
431 | | |
432 | 26.6k | data_start = src - avpkt->data; |
433 | 26.6k | ctx->data_size = avpkt->size - data_start; |
434 | 26.6k | ctx->src = src; |
435 | 26.6k | ctx->pic = frame; |
436 | | |
437 | 26.6k | if (ctx->data_size < HQX_HEADER_SIZE) { |
438 | 11.9k | av_log(avctx, AV_LOG_ERROR, "Frame too small.\n"); |
439 | 11.9k | return AVERROR_INVALIDDATA; |
440 | 11.9k | } |
441 | | |
442 | 14.7k | if (src[0] != 'H' || src[1] != 'Q') { |
443 | 1.26k | av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n"); |
444 | 1.26k | return AVERROR_INVALIDDATA; |
445 | 1.26k | } |
446 | 13.4k | ctx->interlaced = !(src[2] & 0x80); |
447 | 13.4k | ctx->format = src[2] & 7; |
448 | 13.4k | ctx->dcb = (src[3] & 3) + 8; |
449 | 13.4k | ctx->width = AV_RB16(src + 4); |
450 | 13.4k | ctx->height = AV_RB16(src + 6); |
451 | 242k | for (i = 0; i < 17; i++) |
452 | 228k | ctx->slice_off[i] = AV_RB24(src + 8 + i * 3); |
453 | | |
454 | 13.4k | if (ctx->dcb == 8) { |
455 | 250 | av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb); |
456 | 250 | return AVERROR_INVALIDDATA; |
457 | 250 | } |
458 | 13.2k | ret = av_image_check_size(ctx->width, ctx->height, 0, avctx); |
459 | 13.2k | if (ret < 0) { |
460 | 650 | av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n", |
461 | 650 | ctx->width, ctx->height); |
462 | 650 | return AVERROR_INVALIDDATA; |
463 | 650 | } |
464 | | |
465 | 12.5k | avctx->coded_width = FFALIGN(ctx->width, 16); |
466 | 12.5k | avctx->coded_height = FFALIGN(ctx->height, 16); |
467 | 12.5k | avctx->width = ctx->width; |
468 | 12.5k | avctx->height = ctx->height; |
469 | 12.5k | avctx->bits_per_raw_sample = 10; |
470 | | |
471 | | //The minimum size is 2bit per macroblock |
472 | | // hqx_decode_422 & hqx_decode_444 have a unconditionally stored 4bits hqx_quants index |
473 | | // hqx_decode_422a & hqx_decode_444a use cbp_vlc which has a minimum length of 2 bits for its VLCs |
474 | | // The code rejects slices overlapping in their input data |
475 | 12.5k | if (avctx->coded_width / 16 * (avctx->coded_height / 16) * |
476 | 12.5k | (100 - avctx->discard_damaged_percentage) / 100 > 4LL * avpkt->size) |
477 | 412 | return AVERROR_INVALIDDATA; |
478 | | |
479 | 12.1k | switch (ctx->format) { |
480 | 5.72k | case HQX_422: |
481 | 5.72k | avctx->pix_fmt = AV_PIX_FMT_YUV422P16; |
482 | 5.72k | ctx->decode_func = hqx_decode_422; |
483 | 5.72k | break; |
484 | 676 | case HQX_444: |
485 | 676 | avctx->pix_fmt = AV_PIX_FMT_YUV444P16; |
486 | 676 | ctx->decode_func = hqx_decode_444; |
487 | 676 | break; |
488 | 1.04k | case HQX_422A: |
489 | 1.04k | avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; |
490 | 1.04k | ctx->decode_func = hqx_decode_422a; |
491 | 1.04k | break; |
492 | 4.50k | case HQX_444A: |
493 | 4.50k | avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; |
494 | 4.50k | ctx->decode_func = hqx_decode_444a; |
495 | 4.50k | break; |
496 | 199 | default: |
497 | 199 | av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format); |
498 | 199 | return AVERROR_INVALIDDATA; |
499 | 12.1k | } |
500 | | |
501 | 11.9k | ret = ff_thread_get_buffer(avctx, frame, 0); |
502 | 11.9k | if (ret < 0) |
503 | 197 | return ret; |
504 | | |
505 | 11.7k | avctx->execute2(avctx, decode_slice_thread, NULL, NULL, 16); |
506 | | |
507 | 11.7k | *got_picture_ptr = 1; |
508 | | |
509 | 11.7k | return avpkt->size; |
510 | 11.9k | } |
511 | | |
512 | | static av_cold int hqx_decode_close(AVCodecContext *avctx) |
513 | 1.84k | { |
514 | 1.84k | int i; |
515 | 1.84k | HQXContext *ctx = avctx->priv_data; |
516 | | |
517 | 1.84k | ff_vlc_free(&ctx->cbp_vlc); |
518 | 7.37k | for (i = 0; i < 3; i++) { |
519 | 5.53k | ff_vlc_free(&ctx->dc_vlc[i]); |
520 | 5.53k | } |
521 | | |
522 | 1.84k | return 0; |
523 | 1.84k | } |
524 | | |
525 | | static av_cold int hqx_decode_init(AVCodecContext *avctx) |
526 | 1.84k | { |
527 | 1.84k | HQXContext *ctx = avctx->priv_data; |
528 | | |
529 | 1.84k | ff_hqxdsp_init(&ctx->hqxdsp); |
530 | | |
531 | 1.84k | return ff_hqx_init_vlcs(ctx); |
532 | 1.84k | } |
533 | | |
534 | | const FFCodec ff_hqx_decoder = { |
535 | | .p.name = "hqx", |
536 | | CODEC_LONG_NAME("Canopus HQX"), |
537 | | .p.type = AVMEDIA_TYPE_VIDEO, |
538 | | .p.id = AV_CODEC_ID_HQX, |
539 | | .priv_data_size = sizeof(HQXContext), |
540 | | .init = hqx_decode_init, |
541 | | FF_CODEC_DECODE_CB(hqx_decode_frame), |
542 | | .close = hqx_decode_close, |
543 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS | |
544 | | AV_CODEC_CAP_FRAME_THREADS, |
545 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
546 | | }; |