Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Mirillis FIC decoder | ||
3 | * | ||
4 | * Copyright (c) 2014 Konstantin Shishkov | ||
5 | * Copyright (c) 2014 Derek Buitenhuis | ||
6 | * | ||
7 | * This file is part of FFmpeg. | ||
8 | * | ||
9 | * FFmpeg is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU Lesser General Public | ||
11 | * License as published by the Free Software Foundation; either | ||
12 | * version 2.1 of the License, or (at your option) any later version. | ||
13 | * | ||
14 | * FFmpeg is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * Lesser General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public | ||
20 | * License along with FFmpeg; if not, write to the Free Software | ||
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
22 | */ | ||
23 | |||
24 | #include "libavutil/common.h" | ||
25 | #include "libavutil/mem.h" | ||
26 | #include "libavutil/mem_internal.h" | ||
27 | #include "libavutil/opt.h" | ||
28 | #include "avcodec.h" | ||
29 | #include "codec_internal.h" | ||
30 | #include "decode.h" | ||
31 | #include "get_bits.h" | ||
32 | #include "golomb.h" | ||
33 | |||
34 | typedef struct FICThreadContext { | ||
35 | DECLARE_ALIGNED(16, int16_t, block)[64]; | ||
36 | const uint8_t *src; | ||
37 | int slice_h; | ||
38 | int src_size; | ||
39 | int y_off; | ||
40 | int p_frame; | ||
41 | } FICThreadContext; | ||
42 | |||
43 | typedef struct FICContext { | ||
44 | AVClass *class; | ||
45 | AVCodecContext *avctx; | ||
46 | AVFrame *frame; | ||
47 | AVFrame *final_frame; | ||
48 | |||
49 | FICThreadContext *slice_data; | ||
50 | int slice_data_size; | ||
51 | |||
52 | const uint8_t *qmat; | ||
53 | |||
54 | enum AVPictureType cur_frame_type; | ||
55 | |||
56 | int aligned_width, aligned_height; | ||
57 | int num_slices, slice_h; | ||
58 | |||
59 | uint8_t cursor_buf[4096]; | ||
60 | int skip_cursor; | ||
61 | } FICContext; | ||
62 | |||
63 | static const uint8_t fic_qmat_hq[64] = { | ||
64 | 1, 2, 2, 2, 3, 3, 3, 4, | ||
65 | 2, 2, 2, 3, 3, 3, 4, 4, | ||
66 | 2, 2, 3, 3, 3, 4, 4, 4, | ||
67 | 2, 2, 3, 3, 3, 4, 4, 5, | ||
68 | 2, 3, 3, 3, 4, 4, 5, 6, | ||
69 | 3, 3, 3, 4, 4, 5, 6, 7, | ||
70 | 3, 3, 3, 4, 4, 5, 7, 7, | ||
71 | 3, 3, 4, 4, 5, 7, 7, 7, | ||
72 | }; | ||
73 | |||
74 | static const uint8_t fic_qmat_lq[64] = { | ||
75 | 1, 5, 6, 7, 8, 9, 9, 11, | ||
76 | 5, 5, 7, 8, 9, 9, 11, 12, | ||
77 | 6, 7, 8, 9, 9, 11, 11, 12, | ||
78 | 7, 7, 8, 9, 9, 11, 12, 13, | ||
79 | 7, 8, 9, 9, 10, 11, 13, 16, | ||
80 | 8, 9, 9, 10, 11, 13, 16, 19, | ||
81 | 8, 9, 9, 11, 12, 15, 18, 23, | ||
82 | 9, 9, 11, 12, 15, 18, 23, 27 | ||
83 | }; | ||
84 | |||
85 | static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' }; | ||
86 | |||
87 | #define FIC_HEADER_SIZE 27 | ||
88 | #define CURSOR_OFFSET 59 | ||
89 | |||
90 | 1030768 | static av_always_inline void fic_idct(int16_t *blk, int step, int shift, int rnd) | |
91 | { | ||
92 | 1030768 | const unsigned t0 = 27246 * blk[3 * step] + 18405 * blk[5 * step]; | |
93 | 1030768 | const unsigned t1 = 27246 * blk[5 * step] - 18405 * blk[3 * step]; | |
94 | 1030768 | const unsigned t2 = 6393 * blk[7 * step] + 32139 * blk[1 * step]; | |
95 | 1030768 | const unsigned t3 = 6393 * blk[1 * step] - 32139 * blk[7 * step]; | |
96 | 1030768 | const unsigned t4 = 5793U * ((int)(t2 + t0 + 0x800) >> 12); | |
97 | 1030768 | const unsigned t5 = 5793U * ((int)(t3 + t1 + 0x800) >> 12); | |
98 | 1030768 | const unsigned t6 = t2 - t0; | |
99 | 1030768 | const unsigned t7 = t3 - t1; | |
100 | 1030768 | const unsigned t8 = 17734 * blk[2 * step] - 42813 * blk[6 * step]; | |
101 | 1030768 | const unsigned t9 = 17734 * blk[6 * step] + 42814 * blk[2 * step]; | |
102 | 1030768 | const unsigned tA = (blk[0 * step] - blk[4 * step]) * 32768 + rnd; | |
103 | 1030768 | const unsigned tB = (blk[0 * step] + blk[4 * step]) * 32768 + rnd; | |
104 | 1030768 | blk[0 * step] = (int)( t4 + t9 + tB) >> shift; | |
105 | 1030768 | blk[1 * step] = (int)( t6 + t7 + t8 + tA) >> shift; | |
106 | 1030768 | blk[2 * step] = (int)( t6 - t7 - t8 + tA) >> shift; | |
107 | 1030768 | blk[3 * step] = (int)( t5 - t9 + tB) >> shift; | |
108 | 1030768 | blk[4 * step] = (int)( -t5 - t9 + tB) >> shift; | |
109 | 1030768 | blk[5 * step] = (int)(-(t6 - t7) - t8 + tA) >> shift; | |
110 | 1030768 | blk[6 * step] = (int)(-(t6 + t7) + t8 + tA) >> shift; | |
111 | 1030768 | blk[7 * step] = (int)( -t4 + t9 + tB) >> shift; | |
112 | 1030768 | } | |
113 | |||
114 | 64423 | static void fic_idct_put(uint8_t *dst, int stride, int16_t *block) | |
115 | { | ||
116 | int i, j; | ||
117 | int16_t *ptr; | ||
118 | |||
119 | 64423 | ptr = block; | |
120 | 64423 | fic_idct(ptr++, 8, 13, (1 << 12) + (1 << 17)); | |
121 |
2/2✓ Branch 0 taken 450961 times.
✓ Branch 1 taken 64423 times.
|
515384 | for (i = 1; i < 8; i++) { |
122 | 450961 | fic_idct(ptr, 8, 13, 1 << 12); | |
123 | 450961 | ptr++; | |
124 | } | ||
125 | |||
126 | 64423 | ptr = block; | |
127 |
2/2✓ Branch 0 taken 515384 times.
✓ Branch 1 taken 64423 times.
|
579807 | for (i = 0; i < 8; i++) { |
128 | 515384 | fic_idct(ptr, 1, 20, 0); | |
129 | 515384 | ptr += 8; | |
130 | } | ||
131 | |||
132 | 64423 | ptr = block; | |
133 |
2/2✓ Branch 0 taken 515384 times.
✓ Branch 1 taken 64423 times.
|
579807 | for (j = 0; j < 8; j++) { |
134 |
2/2✓ Branch 0 taken 4123072 times.
✓ Branch 1 taken 515384 times.
|
4638456 | for (i = 0; i < 8; i++) |
135 | 4123072 | dst[i] = av_clip_uint8(ptr[i]); | |
136 | 515384 | dst += stride; | |
137 | 515384 | ptr += 8; | |
138 | } | ||
139 | 64423 | } | |
140 | 538550 | static int fic_decode_block(FICContext *ctx, GetBitContext *gb, | |
141 | uint8_t *dst, int stride, int16_t *block, int *is_p) | ||
142 | { | ||
143 | int i, num_coeff; | ||
144 | |||
145 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 538547 times.
|
538550 | if (get_bits_left(gb) < 8) |
146 | 3 | return AVERROR_INVALIDDATA; | |
147 | |||
148 | /* Is it a skip block? */ | ||
149 |
2/2✓ Branch 1 taken 474124 times.
✓ Branch 2 taken 64423 times.
|
538547 | if (get_bits1(gb)) { |
150 | 474124 | *is_p = 1; | |
151 | 474124 | return 0; | |
152 | } | ||
153 | |||
154 | 64423 | memset(block, 0, sizeof(*block) * 64); | |
155 | |||
156 | 64423 | num_coeff = get_bits(gb, 7); | |
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64423 times.
|
64423 | if (num_coeff > 64) |
158 | ✗ | return AVERROR_INVALIDDATA; | |
159 | |||
160 |
2/2✓ Branch 0 taken 1124888 times.
✓ Branch 1 taken 64423 times.
|
1189311 | for (i = 0; i < num_coeff; i++) { |
161 | 1124888 | int v = get_se_golomb(gb); | |
162 |
2/4✓ Branch 0 taken 1124888 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1124888 times.
|
1124888 | if (v < -2048 || v > 2048) |
163 | ✗ | return AVERROR_INVALIDDATA; | |
164 | 1124888 | block[ff_zigzag_direct[i]] = v * | |
165 | 1124888 | ctx->qmat[ff_zigzag_direct[i]]; | |
166 | } | ||
167 | |||
168 | 64423 | fic_idct_put(dst, stride, block); | |
169 | |||
170 | 64423 | return 0; | |
171 | } | ||
172 | |||
173 | 92 | static int fic_decode_slice(AVCodecContext *avctx, void *tdata) | |
174 | { | ||
175 | 92 | FICContext *ctx = avctx->priv_data; | |
176 | 92 | FICThreadContext *tctx = tdata; | |
177 | GetBitContext gb; | ||
178 | 92 | const uint8_t *src = tctx->src; | |
179 | 92 | int slice_h = tctx->slice_h; | |
180 | 92 | int src_size = tctx->src_size; | |
181 | 92 | int y_off = tctx->y_off; | |
182 | int x, y, p, ret; | ||
183 | |||
184 | 92 | ret = init_get_bits8(&gb, src, src_size); | |
185 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 88 times.
|
92 | if (ret < 0) |
186 | 4 | return ret; | |
187 | |||
188 |
2/2✓ Branch 0 taken 264 times.
✓ Branch 1 taken 85 times.
|
349 | for (p = 0; p < 3; p++) { |
189 | 264 | int stride = ctx->frame->linesize[p]; | |
190 | 264 | uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride; | |
191 | |||
192 |
2/2✓ Branch 0 taken 4224 times.
✓ Branch 1 taken 261 times.
|
4485 | for (y = 0; y < (slice_h >> !!p); y += 8) { |
193 |
2/2✓ Branch 0 taken 538550 times.
✓ Branch 1 taken 4221 times.
|
542771 | for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) { |
194 | int ret; | ||
195 | |||
196 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 538547 times.
|
538550 | if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, |
197 | 538550 | tctx->block, &tctx->p_frame)) != 0) | |
198 | 3 | return ret; | |
199 | } | ||
200 | |||
201 | 4221 | dst += 8 * stride; | |
202 | } | ||
203 | } | ||
204 | |||
205 | 85 | return 0; | |
206 | } | ||
207 | |||
208 | 1472 | static av_always_inline void fic_alpha_blend(uint8_t *dst, uint8_t *src, | |
209 | int size, uint8_t *alpha) | ||
210 | { | ||
211 | int i; | ||
212 | |||
213 |
2/2✓ Branch 0 taken 35328 times.
✓ Branch 1 taken 1472 times.
|
36800 | for (i = 0; i < size; i++) |
214 | 35328 | dst[i] += ((src[i] - dst[i]) * alpha[i]) >> 8; | |
215 | 1472 | } | |
216 | |||
217 | 23 | static void fic_draw_cursor(AVCodecContext *avctx, int cur_x, int cur_y) | |
218 | { | ||
219 | 23 | FICContext *ctx = avctx->priv_data; | |
220 | 23 | uint8_t *ptr = ctx->cursor_buf; | |
221 | uint8_t *dstptr[3]; | ||
222 | uint8_t planes[4][1024]; | ||
223 | uint8_t chroma[3][256]; | ||
224 | int i, j, p; | ||
225 | |||
226 | /* Convert to YUVA444. */ | ||
227 |
2/2✓ Branch 0 taken 23552 times.
✓ Branch 1 taken 23 times.
|
23575 | for (i = 0; i < 1024; i++) { |
228 | 23552 | planes[0][i] = (( 25 * ptr[0] + 129 * ptr[1] + 66 * ptr[2]) / 255) + 16; | |
229 | 23552 | planes[1][i] = ((-38 * ptr[0] + 112 * ptr[1] + -74 * ptr[2]) / 255) + 128; | |
230 | 23552 | planes[2][i] = ((-18 * ptr[0] + 112 * ptr[1] + -94 * ptr[2]) / 255) + 128; | |
231 | 23552 | planes[3][i] = ptr[3]; | |
232 | |||
233 | 23552 | ptr += 4; | |
234 | } | ||
235 | |||
236 | /* Subsample chroma. */ | ||
237 |
2/2✓ Branch 0 taken 368 times.
✓ Branch 1 taken 23 times.
|
391 | for (i = 0; i < 32; i += 2) |
238 |
2/2✓ Branch 0 taken 5888 times.
✓ Branch 1 taken 368 times.
|
6256 | for (j = 0; j < 32; j += 2) |
239 |
2/2✓ Branch 0 taken 17664 times.
✓ Branch 1 taken 5888 times.
|
23552 | for (p = 0; p < 3; p++) |
240 | 17664 | chroma[p][16 * (i / 2) + j / 2] = (planes[p + 1][32 * i + j ] + | |
241 | 17664 | planes[p + 1][32 * i + j + 1] + | |
242 | 17664 | planes[p + 1][32 * (i + 1) + j ] + | |
243 | 17664 | planes[p + 1][32 * (i + 1) + j + 1]) / 4; | |
244 | |||
245 | /* Seek to x/y pos of cursor. */ | ||
246 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 23 times.
|
92 | for (i = 0; i < 3; i++) |
247 | 69 | dstptr[i] = ctx->final_frame->data[i] + | |
248 | 69 | (ctx->final_frame->linesize[i] * (cur_y >> !!i)) + | |
249 | 69 | (cur_x >> !!i) + !!i; | |
250 | |||
251 | /* Copy. */ | ||
252 |
2/2✓ Branch 0 taken 368 times.
✓ Branch 1 taken 23 times.
|
391 | for (i = 0; i < FFMIN(32, avctx->height - cur_y) - 1; i += 2) { |
253 | 368 | int lsize = FFMIN(32, avctx->width - cur_x); | |
254 | 368 | int csize = lsize / 2; | |
255 | |||
256 | 368 | fic_alpha_blend(dstptr[0], | |
257 | 368 | planes[0] + i * 32, lsize, planes[3] + i * 32); | |
258 | 368 | fic_alpha_blend(dstptr[0] + ctx->final_frame->linesize[0], | |
259 | 368 | planes[0] + (i + 1) * 32, lsize, planes[3] + (i + 1) * 32); | |
260 | 368 | fic_alpha_blend(dstptr[1], | |
261 | 368 | chroma[0] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16); | |
262 | 368 | fic_alpha_blend(dstptr[2], | |
263 | 368 | chroma[1] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16); | |
264 | |||
265 | 368 | dstptr[0] += ctx->final_frame->linesize[0] * 2; | |
266 | 368 | dstptr[1] += ctx->final_frame->linesize[1]; | |
267 | 368 | dstptr[2] += ctx->final_frame->linesize[2]; | |
268 | } | ||
269 | 23 | } | |
270 | |||
271 | 121 | static int fic_decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
272 | int *got_frame, AVPacket *avpkt) | ||
273 | { | ||
274 | 121 | FICContext *ctx = avctx->priv_data; | |
275 | 121 | const uint8_t *src = avpkt->data; | |
276 | int ret; | ||
277 | int slice, nslices; | ||
278 | int msize; | ||
279 | int tsize; | ||
280 | int cur_x, cur_y; | ||
281 | 121 | int skip_cursor = ctx->skip_cursor; | |
282 | const uint8_t *sdata; | ||
283 | |||
284 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 121 times.
|
121 | if ((ret = ff_reget_buffer(avctx, ctx->frame, 0)) < 0) |
285 | ✗ | return ret; | |
286 | |||
287 | /* Header + at least one slice (4) */ | ||
288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
|
121 | if (avpkt->size < FIC_HEADER_SIZE + 4) { |
289 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n"); | |
290 | ✗ | return AVERROR_INVALIDDATA; | |
291 | } | ||
292 | |||
293 | /* Check for header. */ | ||
294 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
|
121 | if (memcmp(src, fic_header, 7)) |
295 | ✗ | av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n"); | |
296 | |||
297 | /* Is it a skip frame? */ | ||
298 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 23 times.
|
121 | if (src[17]) { |
299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
|
98 | if (!ctx->final_frame) { |
300 | ✗ | av_log(avctx, AV_LOG_WARNING, "Initial frame is skipped\n"); | |
301 | ✗ | return AVERROR_INVALIDDATA; | |
302 | } | ||
303 | 98 | goto skip; | |
304 | } | ||
305 | |||
306 | 23 | nslices = src[13]; | |
307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!nslices) { |
308 | ✗ | av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n"); | |
309 | ✗ | return AVERROR_INVALIDDATA; | |
310 | } | ||
311 | |||
312 | /* High or Low Quality Matrix? */ | ||
313 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq; |
314 | |||
315 | /* Skip cursor data. */ | ||
316 | 23 | tsize = AV_RB24(src + 24); | |
317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (tsize > avpkt->size - FIC_HEADER_SIZE) { |
318 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
319 | "Packet is too small to contain cursor (%d vs %d bytes).\n", | ||
320 | ✗ | tsize, avpkt->size - FIC_HEADER_SIZE); | |
321 | ✗ | return AVERROR_INVALIDDATA; | |
322 | } | ||
323 | |||
324 |
3/6✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 23 times.
|
23 | if (!tsize || !AV_RL16(src + 37) || !AV_RL16(src + 39)) |
325 | ✗ | skip_cursor = 1; | |
326 | |||
327 |
2/4✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
|
23 | if (!skip_cursor && tsize < 32) { |
328 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
329 | "Cursor data too small. Skipping cursor.\n"); | ||
330 | ✗ | skip_cursor = 1; | |
331 | } | ||
332 | |||
333 | /* Cursor position. */ | ||
334 | 23 | cur_x = AV_RL16(src + 33); | |
335 | 23 | cur_y = AV_RL16(src + 35); | |
336 |
3/6✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 23 times.
|
23 | if (!skip_cursor && (cur_x > avctx->width || cur_y > avctx->height)) { |
337 | ✗ | av_log(avctx, AV_LOG_DEBUG, | |
338 | "Invalid cursor position: (%d,%d). Skipping cursor.\n", | ||
339 | cur_x, cur_y); | ||
340 | ✗ | skip_cursor = 1; | |
341 | } | ||
342 | |||
343 |
3/6✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 23 times.
|
23 | if (!skip_cursor && (AV_RL16(src + 37) != 32 || AV_RL16(src + 39) != 32)) { |
344 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
345 | "Invalid cursor size. Skipping cursor.\n"); | ||
346 | ✗ | skip_cursor = 1; | |
347 | } | ||
348 | |||
349 |
2/4✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
|
23 | if (!skip_cursor && avpkt->size < CURSOR_OFFSET + sizeof(ctx->cursor_buf)) { |
350 | ✗ | skip_cursor = 1; | |
351 | } | ||
352 | |||
353 | /* Slice height for all but the last slice. */ | ||
354 | 23 | ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices; | |
355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (ctx->slice_h % 16) |
356 | ✗ | ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16); | |
357 | |||
358 | /* First slice offset and remaining data. */ | ||
359 | 23 | sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices; | |
360 | 23 | msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE; | |
361 | |||
362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (msize <= ctx->aligned_width/8 * (ctx->aligned_height/8) / 8) { |
363 | ✗ | av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n"); | |
364 | ✗ | return AVERROR_INVALIDDATA; | |
365 | } | ||
366 | |||
367 | /* Allocate slice data. */ | ||
368 | 23 | av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size, | |
369 | nslices * sizeof(ctx->slice_data[0])); | ||
370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!ctx->slice_data_size) { |
371 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n"); | |
372 | ✗ | return AVERROR(ENOMEM); | |
373 | } | ||
374 | 23 | memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0])); | |
375 | |||
376 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 23 times.
|
115 | for (slice = 0; slice < nslices; slice++) { |
377 | 92 | unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4); | |
378 | unsigned slice_size; | ||
379 | 92 | int y_off = ctx->slice_h * slice; | |
380 | 92 | int slice_h = ctx->slice_h; | |
381 | |||
382 | /* | ||
383 | * Either read the slice size, or consume all data left. | ||
384 | * Also, special case the last slight height. | ||
385 | */ | ||
386 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 69 times.
|
92 | if (slice == nslices - 1) { |
387 | 23 | slice_size = msize; | |
388 | 23 | slice_h = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16); | |
389 | } else { | ||
390 | 69 | slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4); | |
391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | if (slice_size < slice_off) |
392 | ✗ | return AVERROR_INVALIDDATA; | |
393 | } | ||
394 | |||
395 |
4/4✓ Branch 0 taken 91 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 88 times.
|
92 | if (slice_size < slice_off || slice_size > msize) |
396 | 4 | continue; | |
397 | |||
398 | 88 | slice_size -= slice_off; | |
399 | |||
400 | 88 | ctx->slice_data[slice].src = sdata + slice_off; | |
401 | 88 | ctx->slice_data[slice].src_size = slice_size; | |
402 | 88 | ctx->slice_data[slice].slice_h = slice_h; | |
403 | 88 | ctx->slice_data[slice].y_off = y_off; | |
404 | } | ||
405 | |||
406 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data, |
407 | NULL, nslices, sizeof(ctx->slice_data[0]))) < 0) | ||
408 | ✗ | return ret; | |
409 | |||
410 | 23 | ctx->frame->flags |= AV_FRAME_FLAG_KEY; | |
411 | 23 | ctx->frame->pict_type = AV_PICTURE_TYPE_I; | |
412 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 3 times.
|
35 | for (slice = 0; slice < nslices; slice++) { |
413 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 12 times.
|
32 | if (ctx->slice_data[slice].p_frame) { |
414 | 20 | ctx->frame->flags &= ~AV_FRAME_FLAG_KEY; | |
415 | 20 | ctx->frame->pict_type = AV_PICTURE_TYPE_P; | |
416 | 20 | break; | |
417 | } | ||
418 | } | ||
419 | 23 | av_frame_free(&ctx->final_frame); | |
420 | 23 | ctx->final_frame = av_frame_clone(ctx->frame); | |
421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!ctx->final_frame) { |
422 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not clone frame buffer.\n"); | |
423 | ✗ | return AVERROR(ENOMEM); | |
424 | } | ||
425 | |||
426 | /* Make sure we use a user-supplied buffer. */ | ||
427 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = ff_reget_buffer(avctx, ctx->final_frame, 0)) < 0) { |
428 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not make frame writable.\n"); | |
429 | ✗ | return ret; | |
430 | } | ||
431 | |||
432 | /* Draw cursor. */ | ||
433 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!skip_cursor) { |
434 | 23 | memcpy(ctx->cursor_buf, src + CURSOR_OFFSET, sizeof(ctx->cursor_buf)); | |
435 | 23 | fic_draw_cursor(avctx, cur_x, cur_y); | |
436 | } | ||
437 | |||
438 | ✗ | skip: | |
439 | 121 | *got_frame = 1; | |
440 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 121 times.
|
121 | if ((ret = av_frame_ref(rframe, ctx->final_frame)) < 0) |
441 | ✗ | return ret; | |
442 | |||
443 | 121 | return avpkt->size; | |
444 | } | ||
445 | |||
446 | 2 | static av_cold int fic_decode_close(AVCodecContext *avctx) | |
447 | { | ||
448 | 2 | FICContext *ctx = avctx->priv_data; | |
449 | |||
450 | 2 | av_freep(&ctx->slice_data); | |
451 | 2 | av_frame_free(&ctx->final_frame); | |
452 | 2 | av_frame_free(&ctx->frame); | |
453 | |||
454 | 2 | return 0; | |
455 | } | ||
456 | |||
457 | 2 | static av_cold int fic_decode_init(AVCodecContext *avctx) | |
458 | { | ||
459 | 2 | FICContext *ctx = avctx->priv_data; | |
460 | |||
461 | /* Initialize various context values */ | ||
462 | 2 | ctx->avctx = avctx; | |
463 | 2 | ctx->aligned_width = FFALIGN(avctx->width, 16); | |
464 | 2 | ctx->aligned_height = FFALIGN(avctx->height, 16); | |
465 | |||
466 | 2 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; | |
467 | 2 | avctx->bits_per_raw_sample = 8; | |
468 | |||
469 | 2 | ctx->frame = av_frame_alloc(); | |
470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!ctx->frame) |
471 | ✗ | return AVERROR(ENOMEM); | |
472 | |||
473 | 2 | return 0; | |
474 | } | ||
475 | |||
476 | static const AVOption options[] = { | ||
477 | { "skip_cursor", "skip the cursor", offsetof(FICContext, skip_cursor), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM }, | ||
478 | { NULL }, | ||
479 | }; | ||
480 | |||
481 | static const AVClass fic_decoder_class = { | ||
482 | .class_name = "FIC decoder", | ||
483 | .item_name = av_default_item_name, | ||
484 | .option = options, | ||
485 | .version = LIBAVUTIL_VERSION_INT, | ||
486 | }; | ||
487 | |||
488 | const FFCodec ff_fic_decoder = { | ||
489 | .p.name = "fic", | ||
490 | CODEC_LONG_NAME("Mirillis FIC"), | ||
491 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
492 | .p.id = AV_CODEC_ID_FIC, | ||
493 | .priv_data_size = sizeof(FICContext), | ||
494 | .init = fic_decode_init, | ||
495 | FF_CODEC_DECODE_CB(fic_decode_frame), | ||
496 | .close = fic_decode_close, | ||
497 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS, | ||
498 | .p.priv_class = &fic_decoder_class, | ||
499 | }; | ||
500 |