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