Line | Branch | Exec | Source |
---|---|---|---|
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 | #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 | 110880 | 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 | { | ||
89 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 110744 times.
|
110880 | int fields = ilace ? 2 : 1; |
90 | 110880 | int lsize = ctx->pic->linesize[plane]; | |
91 | 110880 | uint8_t *p = ctx->pic->data[plane] + x * 2; | |
92 | |||
93 | 110880 | ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize), | |
94 | 110880 | lsize * fields, block0, quant); | |
95 | 110880 | ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize), | |
96 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 110744 times.
|
110880 | lsize * fields, block1, quant); |
97 | 110880 | } | |
98 | |||
99 | 2674300 | static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac, | |
100 | int *run, int *lev) | ||
101 | { | ||
102 | int val; | ||
103 | |||
104 | 2674300 | val = show_bits(gb, ac->lut_bits); | |
105 |
2/2✓ Branch 0 taken 108610 times.
✓ Branch 1 taken 2565690 times.
|
2674300 | if (ac->lut[val].bits == -1) { |
106 | 108610 | GetBitContext gb2 = *gb; | |
107 | 108610 | skip_bits(&gb2, ac->lut_bits); | |
108 | 108610 | val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits); | |
109 | } | ||
110 | 2674300 | *run = ac->lut[val].run; | |
111 | 2674300 | *lev = ac->lut[val].lev; | |
112 | 2674300 | skip_bits(gb, ac->lut[val].bits); | |
113 | 2674300 | } | |
114 | |||
115 | 144104 | static int decode_block(GetBitContext *gb, VLC *vlc, | |
116 | const int *quants, int dcb, | ||
117 | int16_t block[64], int *last_dc) | ||
118 | { | ||
119 | int q, dc; | ||
120 | int ac_idx; | ||
121 | 144104 | int run, lev, pos = 1; | |
122 | |||
123 | 144104 | memset(block, 0, 64 * sizeof(*block)); | |
124 | 144104 | dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2); | |
125 | 144104 | *last_dc += dc; | |
126 | |||
127 | 144104 | block[0] = sign_extend(*last_dc << (12 - dcb), 12); | |
128 | |||
129 | 144104 | q = quants[get_bits(gb, 2)]; | |
130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144104 times.
|
144104 | if (q >= 128) |
131 | ✗ | ac_idx = HQX_AC_Q128; | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144104 times.
|
144104 | else if (q >= 64) |
133 | ✗ | ac_idx = HQX_AC_Q64; | |
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144104 times.
|
144104 | else if (q >= 32) |
135 | ✗ | ac_idx = HQX_AC_Q32; | |
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144104 times.
|
144104 | else if (q >= 16) |
137 | ✗ | ac_idx = HQX_AC_Q16; | |
138 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 144090 times.
|
144104 | else if (q >= 8) |
139 | 14 | ac_idx = HQX_AC_Q8; | |
140 | else | ||
141 | 144090 | ac_idx = HQX_AC_Q0; | |
142 | |||
143 | do { | ||
144 | 2674300 | hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev); | |
145 | 2674300 | pos += run; | |
146 |
2/2✓ Branch 0 taken 140780 times.
✓ Branch 1 taken 2533520 times.
|
2674300 | if (pos >= 64) |
147 | 140780 | break; | |
148 | 2533520 | block[ff_zigzag_direct[pos++]] = lev * q; | |
149 |
2/2✓ Branch 0 taken 2530196 times.
✓ Branch 1 taken 3324 times.
|
2533520 | } while (pos < 64); |
150 | |||
151 | 144104 | return 0; | |
152 | } | ||
153 | |||
154 | 3240 | static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y) | |
155 | { | ||
156 | 3240 | HQXSlice *slice = &ctx->slice[slice_no]; | |
157 | 3240 | GetBitContext *gb = &slice->gb; | |
158 | const int *quants; | ||
159 | int flag; | ||
160 | int last_dc; | ||
161 | int i, ret; | ||
162 | |||
163 |
1/2✓ Branch 0 taken 3240 times.
✗ Branch 1 not taken.
|
3240 | if (ctx->interlaced) |
164 | 3240 | flag = get_bits1(gb); | |
165 | else | ||
166 | ✗ | flag = 0; | |
167 | |||
168 | 3240 | quants = hqx_quants[get_bits(gb, 4)]; | |
169 | |||
170 |
2/2✓ Branch 0 taken 25920 times.
✓ Branch 1 taken 3240 times.
|
29160 | for (i = 0; i < 8; i++) { |
171 | 25920 | int vlc_index = ctx->dcb - 9; | |
172 |
6/6✓ Branch 0 taken 22680 times.
✓ Branch 1 taken 3240 times.
✓ Branch 2 taken 19440 times.
✓ Branch 3 taken 3240 times.
✓ Branch 4 taken 3240 times.
✓ Branch 5 taken 16200 times.
|
25920 | if (i == 0 || i == 4 || i == 6) |
173 | 9720 | last_dc = 0; | |
174 | 25920 | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, | |
175 | 25920 | ctx->dcb, slice->block[i], &last_dc); | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25920 times.
|
25920 | if (ret < 0) |
177 | ✗ | return ret; | |
178 | } | ||
179 | |||
180 | 3240 | put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[2], hqx_quant_luma); | |
181 | 3240 | put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[3], hqx_quant_luma); | |
182 | 3240 | put_blocks(ctx, 2, x >> 1, y, flag, slice->block[4], slice->block[5], hqx_quant_chroma); | |
183 | 3240 | put_blocks(ctx, 1, x >> 1, y, flag, slice->block[6], slice->block[7], hqx_quant_chroma); | |
184 | |||
185 | 3240 | return 0; | |
186 | } | ||
187 | |||
188 | 16320 | static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y) | |
189 | { | ||
190 | 16320 | HQXSlice *slice = &ctx->slice[slice_no]; | |
191 | 16320 | GetBitContext *gb = &slice->gb; | |
192 | const int *quants; | ||
193 | 16320 | int flag = 0; | |
194 | int last_dc; | ||
195 | int i, ret; | ||
196 | int cbp; | ||
197 | |||
198 | 16320 | cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1); | |
199 | |||
200 |
2/2✓ Branch 0 taken 195840 times.
✓ Branch 1 taken 16320 times.
|
212160 | for (i = 0; i < 12; i++) |
201 | 195840 | memset(slice->block[i], 0, sizeof(**slice->block) * 64); | |
202 |
2/2✓ Branch 0 taken 195840 times.
✓ Branch 1 taken 16320 times.
|
212160 | for (i = 0; i < 12; i++) |
203 | 195840 | slice->block[i][0] = -0x800; | |
204 |
2/2✓ Branch 0 taken 9986 times.
✓ Branch 1 taken 6334 times.
|
16320 | if (cbp) { |
205 |
1/2✓ Branch 0 taken 9986 times.
✗ Branch 1 not taken.
|
9986 | if (ctx->interlaced) |
206 | 9986 | flag = get_bits1(gb); | |
207 | |||
208 | 9986 | quants = hqx_quants[get_bits(gb, 4)]; | |
209 | |||
210 | 9986 | cbp |= cbp << 4; // alpha CBP | |
211 |
2/2✓ Branch 0 taken 9896 times.
✓ Branch 1 taken 90 times.
|
9986 | if (cbp & 0x3) // chroma CBP - top |
212 | 9896 | cbp |= 0x500; | |
213 |
2/2✓ Branch 0 taken 9878 times.
✓ Branch 1 taken 108 times.
|
9986 | if (cbp & 0xC) // chroma CBP - bottom |
214 | 9878 | cbp |= 0xA00; | |
215 |
2/2✓ Branch 0 taken 119832 times.
✓ Branch 1 taken 9986 times.
|
129818 | for (i = 0; i < 12; i++) { |
216 |
8/8✓ Branch 0 taken 109846 times.
✓ Branch 1 taken 9986 times.
✓ Branch 2 taken 99860 times.
✓ Branch 3 taken 9986 times.
✓ Branch 4 taken 89874 times.
✓ Branch 5 taken 9986 times.
✓ Branch 6 taken 9986 times.
✓ Branch 7 taken 79888 times.
|
119832 | if (i == 0 || i == 4 || i == 8 || i == 10) |
217 | 39944 | last_dc = 0; | |
218 |
2/2✓ Branch 0 taken 118184 times.
✓ Branch 1 taken 1648 times.
|
119832 | if (cbp & (1 << i)) { |
219 | 118184 | int vlc_index = ctx->dcb - 9; | |
220 | 118184 | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, | |
221 | 118184 | ctx->dcb, slice->block[i], &last_dc); | |
222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118184 times.
|
118184 | if (ret < 0) |
223 | ✗ | return ret; | |
224 | } | ||
225 | } | ||
226 | } | ||
227 | |||
228 | 16320 | put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma); | |
229 | 16320 | put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma); | |
230 | 16320 | put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma); | |
231 | 16320 | put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma); | |
232 | 16320 | put_blocks(ctx, 2, x >> 1, y, flag, slice->block[ 8], slice->block[ 9], hqx_quant_chroma); | |
233 | 16320 | put_blocks(ctx, 1, x >> 1, y, flag, slice->block[10], slice->block[11], hqx_quant_chroma); | |
234 | |||
235 | 16320 | return 0; | |
236 | } | ||
237 | |||
238 | ✗ | static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y) | |
239 | { | ||
240 | ✗ | HQXSlice *slice = &ctx->slice[slice_no]; | |
241 | ✗ | GetBitContext *gb = &slice->gb; | |
242 | const int *quants; | ||
243 | int flag; | ||
244 | int last_dc; | ||
245 | int i, ret; | ||
246 | |||
247 | ✗ | if (ctx->interlaced) | |
248 | ✗ | flag = get_bits1(gb); | |
249 | else | ||
250 | ✗ | flag = 0; | |
251 | |||
252 | ✗ | quants = hqx_quants[get_bits(gb, 4)]; | |
253 | |||
254 | ✗ | for (i = 0; i < 12; i++) { | |
255 | ✗ | int vlc_index = ctx->dcb - 9; | |
256 | ✗ | if (i == 0 || i == 4 || i == 8) | |
257 | ✗ | last_dc = 0; | |
258 | ✗ | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, | |
259 | ✗ | ctx->dcb, slice->block[i], &last_dc); | |
260 | ✗ | if (ret < 0) | |
261 | ✗ | return ret; | |
262 | } | ||
263 | |||
264 | ✗ | put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[ 2], hqx_quant_luma); | |
265 | ✗ | put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[ 3], hqx_quant_luma); | |
266 | ✗ | put_blocks(ctx, 2, x, y, flag, slice->block[4], slice->block[ 6], hqx_quant_chroma); | |
267 | ✗ | put_blocks(ctx, 2, x + 8, y, flag, slice->block[5], slice->block[ 7], hqx_quant_chroma); | |
268 | ✗ | put_blocks(ctx, 1, x, y, flag, slice->block[8], slice->block[10], hqx_quant_chroma); | |
269 | ✗ | put_blocks(ctx, 1, x + 8, y, flag, slice->block[9], slice->block[11], hqx_quant_chroma); | |
270 | |||
271 | ✗ | return 0; | |
272 | } | ||
273 | |||
274 | ✗ | static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y) | |
275 | { | ||
276 | ✗ | HQXSlice *slice = &ctx->slice[slice_no]; | |
277 | ✗ | GetBitContext *gb = &slice->gb; | |
278 | const int *quants; | ||
279 | ✗ | int flag = 0; | |
280 | int last_dc; | ||
281 | int i, ret; | ||
282 | int cbp; | ||
283 | |||
284 | ✗ | cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1); | |
285 | |||
286 | ✗ | for (i = 0; i < 16; i++) | |
287 | ✗ | memset(slice->block[i], 0, sizeof(**slice->block) * 64); | |
288 | ✗ | for (i = 0; i < 16; i++) | |
289 | ✗ | slice->block[i][0] = -0x800; | |
290 | ✗ | if (cbp) { | |
291 | ✗ | if (ctx->interlaced) | |
292 | ✗ | flag = get_bits1(gb); | |
293 | |||
294 | ✗ | quants = hqx_quants[get_bits(gb, 4)]; | |
295 | |||
296 | ✗ | cbp |= cbp << 4; // alpha CBP | |
297 | ✗ | cbp |= cbp << 8; // chroma CBP | |
298 | ✗ | for (i = 0; i < 16; i++) { | |
299 | ✗ | if (i == 0 || i == 4 || i == 8 || i == 12) | |
300 | ✗ | last_dc = 0; | |
301 | ✗ | if (cbp & (1 << i)) { | |
302 | ✗ | int vlc_index = ctx->dcb - 9; | |
303 | ✗ | ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, | |
304 | ✗ | ctx->dcb, slice->block[i], &last_dc); | |
305 | ✗ | if (ret < 0) | |
306 | ✗ | return ret; | |
307 | } | ||
308 | } | ||
309 | } | ||
310 | |||
311 | ✗ | put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma); | |
312 | ✗ | put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma); | |
313 | ✗ | put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma); | |
314 | ✗ | put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma); | |
315 | ✗ | put_blocks(ctx, 2, x, y, flag, slice->block[ 8], slice->block[10], hqx_quant_chroma); | |
316 | ✗ | put_blocks(ctx, 2, x + 8, y, flag, slice->block[ 9], slice->block[11], hqx_quant_chroma); | |
317 | ✗ | put_blocks(ctx, 1, x, y, flag, slice->block[12], slice->block[14], hqx_quant_chroma); | |
318 | ✗ | put_blocks(ctx, 1, x + 8, y, flag, slice->block[13], slice->block[15], hqx_quant_chroma); | |
319 | |||
320 | ✗ | return 0; | |
321 | } | ||
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 | 64 | static int decode_slice(HQXContext *ctx, int slice_no) | |
328 | { | ||
329 | 64 | int mb_w = (ctx->width + 15) >> 4; | |
330 | 64 | int mb_h = (ctx->height + 15) >> 4; | |
331 | 64 | int grp_w = (mb_w + 4) / 5; | |
332 | 64 | int grp_h = (mb_h + 4) / 5; | |
333 | 64 | int grp_h_edge = grp_w * (mb_w / grp_w); | |
334 | 64 | int grp_v_edge = grp_h * (mb_h / grp_h); | |
335 | 64 | int grp_v_rest = mb_w - grp_h_edge; | |
336 | 64 | int grp_h_rest = mb_h - grp_v_edge; | |
337 | 64 | int num_mbs = mb_w * mb_h; | |
338 | 64 | int num_tiles = (num_mbs + 479) / 480; | |
339 | 64 | int std_tile_blocks = num_mbs / (16 * num_tiles); | |
340 | 64 | int g_tile = slice_no * num_tiles; | |
341 | int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i; | ||
342 | int tile_blocks, tile_limit, tile_no; | ||
343 | |||
344 |
2/2✓ Branch 0 taken 672 times.
✓ Branch 1 taken 64 times.
|
736 | for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) { |
345 | 672 | tile_blocks = std_tile_blocks; | |
346 | 672 | tile_limit = -1; | |
347 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 632 times.
|
672 | if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) { |
348 | 40 | tile_limit = num_mbs / (16 * num_tiles); | |
349 | 40 | tile_blocks++; | |
350 | } | ||
351 |
2/2✓ Branch 0 taken 19560 times.
✓ Branch 1 taken 672 times.
|
20232 | for (i = 0; i < tile_blocks; i++) { |
352 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 19520 times.
|
19560 | if (i == tile_limit) |
353 | 40 | blk_addr = g_tile + 16 * num_tiles * i; | |
354 | else | ||
355 | 19520 | blk_addr = tile_no + 16 * num_tiles * i + | |
356 | 19520 | num_tiles * shuffle_16[(i + slice_no) & 0xF]; | |
357 | 19560 | loc_row = grp_h * (blk_addr / (grp_h * mb_w)); | |
358 | 19560 | loc_addr = blk_addr % (grp_h * mb_w); | |
359 |
2/2✓ Branch 0 taken 3240 times.
✓ Branch 1 taken 16320 times.
|
19560 | if (loc_row >= grp_v_edge) { |
360 | 3240 | mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w)); | |
361 | 3240 | pos = loc_addr % (grp_h_rest * grp_w); | |
362 | } else { | ||
363 | 16320 | mb_x = grp_w * (loc_addr / (grp_h * grp_w)); | |
364 | 16320 | pos = loc_addr % (grp_h * grp_w); | |
365 | } | ||
366 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19560 times.
|
19560 | if (mb_x >= grp_h_edge) { |
367 | ✗ | mb_x += pos % grp_v_rest; | |
368 | ✗ | mb_y = loc_row + (pos / grp_v_rest); | |
369 | } else { | ||
370 | 19560 | mb_x += pos % grp_w; | |
371 | 19560 | mb_y = loc_row + (pos / grp_w); | |
372 | } | ||
373 | 19560 | ctx->decode_func(ctx, slice_no, mb_x * 16, mb_y * 16); | |
374 | } | ||
375 | } | ||
376 | |||
377 | 64 | return 0; | |
378 | } | ||
379 | |||
380 | 64 | static int decode_slice_thread(AVCodecContext *avctx, void *arg, | |
381 | int slice_no, int threadnr) | ||
382 | { | ||
383 | 64 | HQXContext *ctx = avctx->priv_data; | |
384 | 64 | uint32_t *slice_off = ctx->slice_off; | |
385 | int ret; | ||
386 | |||
387 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | if (slice_off[slice_no] < HQX_HEADER_SIZE || |
388 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | slice_off[slice_no] >= slice_off[slice_no + 1] || |
389 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | slice_off[slice_no + 1] > ctx->data_size) { |
390 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid slice size %d.\n", ctx->data_size); | |
391 | ✗ | return AVERROR_INVALIDDATA; | |
392 | } | ||
393 | |||
394 | 64 | ret = init_get_bits8(&ctx->slice[slice_no].gb, | |
395 | 64 | ctx->src + slice_off[slice_no], | |
396 | 64 | slice_off[slice_no + 1] - slice_off[slice_no]); | |
397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (ret < 0) |
398 | ✗ | return ret; | |
399 | |||
400 | 64 | return decode_slice(ctx, slice_no); | |
401 | } | ||
402 | |||
403 | 4 | static int hqx_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
404 | int *got_picture_ptr, AVPacket *avpkt) | ||
405 | { | ||
406 | 4 | HQXContext *ctx = avctx->priv_data; | |
407 | 4 | const uint8_t *src = avpkt->data; | |
408 | uint32_t info_tag; | ||
409 | int data_start; | ||
410 | int i, ret; | ||
411 | |||
412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (avpkt->size < 4 + 4) { |
413 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size); | |
414 | ✗ | return AVERROR_INVALIDDATA; | |
415 | } | ||
416 | |||
417 | 4 | info_tag = AV_RL32(src); | |
418 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (info_tag == MKTAG('I', 'N', 'F', 'O')) { |
419 | 4 | uint32_t info_offset = AV_RL32(src + 4); | |
420 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (info_offset > INT_MAX || info_offset + 8 > avpkt->size) { |
421 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
422 | "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n", | ||
423 | info_offset); | ||
424 | ✗ | return AVERROR_INVALIDDATA; | |
425 | } | ||
426 | 4 | ff_canopus_parse_info_tag(avctx, src + 8, info_offset); | |
427 | |||
428 | 4 | info_offset += 8; | |
429 | 4 | src += info_offset; | |
430 | } | ||
431 | |||
432 | 4 | data_start = src - avpkt->data; | |
433 | 4 | ctx->data_size = avpkt->size - data_start; | |
434 | 4 | ctx->src = src; | |
435 | 4 | ctx->pic = frame; | |
436 | |||
437 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ctx->data_size < HQX_HEADER_SIZE) { |
438 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame too small.\n"); | |
439 | ✗ | return AVERROR_INVALIDDATA; | |
440 | } | ||
441 | |||
442 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (src[0] != 'H' || src[1] != 'Q') { |
443 | ✗ | av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n"); | |
444 | ✗ | return AVERROR_INVALIDDATA; | |
445 | } | ||
446 | 4 | ctx->interlaced = !(src[2] & 0x80); | |
447 | 4 | ctx->format = src[2] & 7; | |
448 | 4 | ctx->dcb = (src[3] & 3) + 8; | |
449 | 4 | ctx->width = AV_RB16(src + 4); | |
450 | 4 | ctx->height = AV_RB16(src + 6); | |
451 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 4 times.
|
72 | for (i = 0; i < 17; i++) |
452 | 68 | ctx->slice_off[i] = AV_RB24(src + 8 + i * 3); | |
453 | |||
454 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ctx->dcb == 8) { |
455 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb); | |
456 | ✗ | return AVERROR_INVALIDDATA; | |
457 | } | ||
458 | 4 | ret = av_image_check_size(ctx->width, ctx->height, 0, avctx); | |
459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) { |
460 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n", | |
461 | ctx->width, ctx->height); | ||
462 | ✗ | return AVERROR_INVALIDDATA; | |
463 | } | ||
464 | |||
465 | 4 | avctx->coded_width = FFALIGN(ctx->width, 16); | |
466 | 4 | avctx->coded_height = FFALIGN(ctx->height, 16); | |
467 | 4 | avctx->width = ctx->width; | |
468 | 4 | avctx->height = ctx->height; | |
469 | 4 | 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 | 4 | if (avctx->coded_width / 16 * (avctx->coded_height / 16) * | |
476 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | (100 - avctx->discard_damaged_percentage) / 100 > 4LL * avpkt->size) |
477 | ✗ | return AVERROR_INVALIDDATA; | |
478 | |||
479 |
2/5✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | switch (ctx->format) { |
480 | 2 | case HQX_422: | |
481 | 2 | avctx->pix_fmt = AV_PIX_FMT_YUV422P16; | |
482 | 2 | ctx->decode_func = hqx_decode_422; | |
483 | 2 | break; | |
484 | ✗ | case HQX_444: | |
485 | ✗ | avctx->pix_fmt = AV_PIX_FMT_YUV444P16; | |
486 | ✗ | ctx->decode_func = hqx_decode_444; | |
487 | ✗ | break; | |
488 | 2 | case HQX_422A: | |
489 | 2 | avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; | |
490 | 2 | ctx->decode_func = hqx_decode_422a; | |
491 | 2 | break; | |
492 | ✗ | case HQX_444A: | |
493 | ✗ | avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; | |
494 | ✗ | ctx->decode_func = hqx_decode_444a; | |
495 | ✗ | break; | |
496 | ✗ | default: | |
497 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format); | |
498 | ✗ | return AVERROR_INVALIDDATA; | |
499 | } | ||
500 | |||
501 | 4 | ret = ff_thread_get_buffer(avctx, frame, 0); | |
502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
503 | ✗ | return ret; | |
504 | |||
505 | 4 | avctx->execute2(avctx, decode_slice_thread, NULL, NULL, 16); | |
506 | |||
507 | 4 | *got_picture_ptr = 1; | |
508 | |||
509 | 4 | return avpkt->size; | |
510 | } | ||
511 | |||
512 | 4 | static av_cold int hqx_decode_close(AVCodecContext *avctx) | |
513 | { | ||
514 | int i; | ||
515 | 4 | HQXContext *ctx = avctx->priv_data; | |
516 | |||
517 | 4 | ff_vlc_free(&ctx->cbp_vlc); | |
518 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | for (i = 0; i < 3; i++) { |
519 | 12 | ff_vlc_free(&ctx->dc_vlc[i]); | |
520 | } | ||
521 | |||
522 | 4 | return 0; | |
523 | } | ||
524 | |||
525 | 4 | static av_cold int hqx_decode_init(AVCodecContext *avctx) | |
526 | { | ||
527 | 4 | HQXContext *ctx = avctx->priv_data; | |
528 | |||
529 | 4 | ff_hqxdsp_init(&ctx->hqxdsp); | |
530 | |||
531 | 4 | return ff_hqx_init_vlcs(ctx); | |
532 | } | ||
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 | }; | ||
547 |