2014-02-07 11:35:07 +03:00
|
|
|
/*
|
|
|
|
* Mirillis FIC decoder
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Konstantin Shishkov
|
|
|
|
* Copyright (c) 2014 Derek Buitenhuis
|
|
|
|
*
|
2014-02-08 21:02:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
2014-02-07 11:35:07 +03:00
|
|
|
*
|
2014-02-08 21:02:46 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2014-02-07 11:35:07 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2014-02-08 21:02:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2014-02-07 11:35:07 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-02-08 21:02:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2014-02-07 11:35:07 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/common.h"
|
2015-06-18 12:28:29 +02:00
|
|
|
#include "libavutil/opt.h"
|
2014-02-07 11:35:07 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "get_bits.h"
|
|
|
|
#include "golomb.h"
|
|
|
|
|
|
|
|
typedef struct FICThreadContext {
|
|
|
|
DECLARE_ALIGNED(16, int16_t, block)[64];
|
|
|
|
uint8_t *src;
|
|
|
|
int slice_h;
|
|
|
|
int src_size;
|
|
|
|
int y_off;
|
2017-03-29 01:52:07 +02:00
|
|
|
int p_frame;
|
2014-02-07 11:35:07 +03:00
|
|
|
} FICThreadContext;
|
|
|
|
|
|
|
|
typedef struct FICContext {
|
2015-06-18 12:28:29 +02:00
|
|
|
AVClass *class;
|
2014-02-07 11:35:07 +03:00
|
|
|
AVCodecContext *avctx;
|
|
|
|
AVFrame *frame;
|
2014-04-20 01:46:52 +03:00
|
|
|
AVFrame *final_frame;
|
2014-02-07 11:35:07 +03:00
|
|
|
|
|
|
|
FICThreadContext *slice_data;
|
|
|
|
int slice_data_size;
|
|
|
|
|
|
|
|
const uint8_t *qmat;
|
|
|
|
|
|
|
|
enum AVPictureType cur_frame_type;
|
|
|
|
|
|
|
|
int aligned_width, aligned_height;
|
|
|
|
int num_slices, slice_h;
|
2014-04-20 01:46:52 +03:00
|
|
|
|
|
|
|
uint8_t cursor_buf[4096];
|
2015-06-18 12:28:29 +02:00
|
|
|
int skip_cursor;
|
2014-02-07 11:35:07 +03:00
|
|
|
} FICContext;
|
|
|
|
|
|
|
|
static const uint8_t fic_qmat_hq[64] = {
|
|
|
|
1, 2, 2, 2, 3, 3, 3, 4,
|
|
|
|
2, 2, 2, 3, 3, 3, 4, 4,
|
|
|
|
2, 2, 3, 3, 3, 4, 4, 4,
|
|
|
|
2, 2, 3, 3, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 3, 4, 4, 5, 6,
|
|
|
|
3, 3, 3, 4, 4, 5, 6, 7,
|
|
|
|
3, 3, 3, 4, 4, 5, 7, 7,
|
|
|
|
3, 3, 4, 4, 5, 7, 7, 7,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t fic_qmat_lq[64] = {
|
|
|
|
1, 5, 6, 7, 8, 9, 9, 11,
|
|
|
|
5, 5, 7, 8, 9, 9, 11, 12,
|
|
|
|
6, 7, 8, 9, 9, 11, 11, 12,
|
|
|
|
7, 7, 8, 9, 9, 11, 12, 13,
|
|
|
|
7, 8, 9, 9, 10, 11, 13, 16,
|
|
|
|
8, 9, 9, 10, 11, 13, 16, 19,
|
|
|
|
8, 9, 9, 11, 12, 15, 18, 23,
|
|
|
|
9, 9, 11, 12, 15, 18, 23, 27
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
|
|
|
|
|
|
|
|
#define FIC_HEADER_SIZE 27
|
2018-05-05 23:42:36 +02:00
|
|
|
#define CURSOR_OFFSET 59
|
2014-02-07 11:35:07 +03:00
|
|
|
|
2014-04-23 22:06:50 +03:00
|
|
|
static av_always_inline void fic_idct(int16_t *blk, int step, int shift, int rnd)
|
2014-04-01 22:00:47 +03:00
|
|
|
{
|
2017-08-17 18:24:37 +02:00
|
|
|
const unsigned t0 = 27246 * blk[3 * step] + 18405 * blk[5 * step];
|
|
|
|
const unsigned t1 = 27246 * blk[5 * step] - 18405 * blk[3 * step];
|
|
|
|
const unsigned t2 = 6393 * blk[7 * step] + 32139 * blk[1 * step];
|
|
|
|
const unsigned t3 = 6393 * blk[1 * step] - 32139 * blk[7 * step];
|
|
|
|
const unsigned t4 = 5793U * ((int)(t2 + t0 + 0x800) >> 12);
|
|
|
|
const unsigned t5 = 5793U * ((int)(t3 + t1 + 0x800) >> 12);
|
2017-05-18 17:13:18 +02:00
|
|
|
const unsigned t6 = t2 - t0;
|
|
|
|
const unsigned t7 = t3 - t1;
|
|
|
|
const unsigned t8 = 17734 * blk[2 * step] - 42813 * blk[6 * step];
|
|
|
|
const unsigned t9 = 17734 * blk[6 * step] + 42814 * blk[2 * step];
|
|
|
|
const unsigned tA = (blk[0 * step] - blk[4 * step]) * 32768 + rnd;
|
|
|
|
const unsigned tB = (blk[0 * step] + blk[4 * step]) * 32768 + rnd;
|
|
|
|
blk[0 * step] = (int)( t4 + t9 + tB) >> shift;
|
|
|
|
blk[1 * step] = (int)( t6 + t7 + t8 + tA) >> shift;
|
|
|
|
blk[2 * step] = (int)( t6 - t7 - t8 + tA) >> shift;
|
|
|
|
blk[3 * step] = (int)( t5 - t9 + tB) >> shift;
|
|
|
|
blk[4 * step] = (int)( -t5 - t9 + tB) >> shift;
|
|
|
|
blk[5 * step] = (int)(-(t6 - t7) - t8 + tA) >> shift;
|
|
|
|
blk[6 * step] = (int)(-(t6 + t7) + t8 + tA) >> shift;
|
|
|
|
blk[7 * step] = (int)( -t4 + t9 + tB) >> shift;
|
2014-04-01 22:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fic_idct_put(uint8_t *dst, int stride, int16_t *block)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int16_t *ptr;
|
|
|
|
|
|
|
|
ptr = block;
|
2014-04-23 22:06:50 +03:00
|
|
|
fic_idct(ptr++, 8, 13, (1 << 12) + (1 << 17));
|
|
|
|
for (i = 1; i < 8; i++) {
|
|
|
|
fic_idct(ptr, 8, 13, 1 << 12);
|
2014-04-01 22:00:47 +03:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = block;
|
|
|
|
for (i = 0; i < 8; i++) {
|
2014-04-23 22:06:50 +03:00
|
|
|
fic_idct(ptr, 1, 20, 0);
|
2014-04-01 22:00:47 +03:00
|
|
|
ptr += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = block;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
dst[i] = av_clip_uint8(ptr[i]);
|
|
|
|
dst += stride;
|
|
|
|
ptr += 8;
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 11:35:07 +03:00
|
|
|
static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
|
2017-03-29 01:52:07 +02:00
|
|
|
uint8_t *dst, int stride, int16_t *block, int *is_p)
|
2014-02-07 11:35:07 +03:00
|
|
|
{
|
|
|
|
int i, num_coeff;
|
|
|
|
|
2019-01-23 01:30:53 +02:00
|
|
|
if (get_bits_left(gb) < 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2014-02-07 11:35:07 +03:00
|
|
|
/* Is it a skip block? */
|
|
|
|
if (get_bits1(gb)) {
|
2017-03-29 01:52:07 +02:00
|
|
|
*is_p = 1;
|
2014-02-07 11:35:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-01 22:00:47 +03:00
|
|
|
memset(block, 0, sizeof(*block) * 64);
|
2014-02-07 11:35:07 +03:00
|
|
|
|
|
|
|
num_coeff = get_bits(gb, 7);
|
|
|
|
if (num_coeff > 64)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2017-05-08 21:32:56 +02:00
|
|
|
for (i = 0; i < num_coeff; i++) {
|
|
|
|
int v = get_se_golomb(gb);
|
|
|
|
if (v < -2048 || v > 2048)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
block[ff_zigzag_direct[i]] = v *
|
2014-04-19 22:40:35 +03:00
|
|
|
ctx->qmat[ff_zigzag_direct[i]];
|
2017-05-08 21:32:56 +02:00
|
|
|
}
|
2014-02-07 11:35:07 +03:00
|
|
|
|
2014-04-01 22:00:47 +03:00
|
|
|
fic_idct_put(dst, stride, block);
|
2014-02-07 11:35:07 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
|
|
|
|
{
|
|
|
|
FICContext *ctx = avctx->priv_data;
|
|
|
|
FICThreadContext *tctx = tdata;
|
|
|
|
GetBitContext gb;
|
|
|
|
uint8_t *src = tctx->src;
|
|
|
|
int slice_h = tctx->slice_h;
|
|
|
|
int src_size = tctx->src_size;
|
|
|
|
int y_off = tctx->y_off;
|
2018-12-23 21:49:19 +02:00
|
|
|
int x, y, p, ret;
|
2014-02-07 11:35:07 +03:00
|
|
|
|
2018-12-23 21:49:19 +02:00
|
|
|
ret = init_get_bits8(&gb, src, src_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-02-07 11:35:07 +03:00
|
|
|
|
|
|
|
for (p = 0; p < 3; p++) {
|
|
|
|
int stride = ctx->frame->linesize[p];
|
|
|
|
uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
|
|
|
|
|
|
|
|
for (y = 0; y < (slice_h >> !!p); y += 8) {
|
|
|
|
for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
|
|
|
|
int ret;
|
|
|
|
|
2017-03-29 01:52:07 +02:00
|
|
|
if ((ret = fic_decode_block(ctx, &gb, dst + x, stride,
|
|
|
|
tctx->block, &tctx->p_frame)) != 0)
|
2014-02-07 11:35:07 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst += 8 * stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-20 01:46:52 +03:00
|
|
|
static av_always_inline void fic_alpha_blend(uint8_t *dst, uint8_t *src,
|
|
|
|
int size, uint8_t *alpha)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
dst[i] += ((src[i] - dst[i]) * alpha[i]) >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fic_draw_cursor(AVCodecContext *avctx, int cur_x, int cur_y)
|
|
|
|
{
|
|
|
|
FICContext *ctx = avctx->priv_data;
|
|
|
|
uint8_t *ptr = ctx->cursor_buf;
|
|
|
|
uint8_t *dstptr[3];
|
|
|
|
uint8_t planes[4][1024];
|
|
|
|
uint8_t chroma[3][256];
|
|
|
|
int i, j, p;
|
|
|
|
|
|
|
|
/* Convert to YUVA444. */
|
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
planes[0][i] = (( 25 * ptr[0] + 129 * ptr[1] + 66 * ptr[2]) / 255) + 16;
|
|
|
|
planes[1][i] = ((-38 * ptr[0] + 112 * ptr[1] + -74 * ptr[2]) / 255) + 128;
|
|
|
|
planes[2][i] = ((-18 * ptr[0] + 112 * ptr[1] + -94 * ptr[2]) / 255) + 128;
|
|
|
|
planes[3][i] = ptr[3];
|
|
|
|
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Subsample chroma. */
|
|
|
|
for (i = 0; i < 32; i += 2)
|
|
|
|
for (j = 0; j < 32; j += 2)
|
|
|
|
for (p = 0; p < 3; p++)
|
|
|
|
chroma[p][16 * (i / 2) + j / 2] = (planes[p + 1][32 * i + j ] +
|
|
|
|
planes[p + 1][32 * i + j + 1] +
|
|
|
|
planes[p + 1][32 * (i + 1) + j ] +
|
|
|
|
planes[p + 1][32 * (i + 1) + j + 1]) / 4;
|
|
|
|
|
|
|
|
/* Seek to x/y pos of cursor. */
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
dstptr[i] = ctx->final_frame->data[i] +
|
|
|
|
(ctx->final_frame->linesize[i] * (cur_y >> !!i)) +
|
|
|
|
(cur_x >> !!i) + !!i;
|
|
|
|
|
|
|
|
/* Copy. */
|
|
|
|
for (i = 0; i < FFMIN(32, avctx->height - cur_y) - 1; i += 2) {
|
|
|
|
int lsize = FFMIN(32, avctx->width - cur_x);
|
|
|
|
int csize = lsize / 2;
|
|
|
|
|
|
|
|
fic_alpha_blend(dstptr[0],
|
|
|
|
planes[0] + i * 32, lsize, planes[3] + i * 32);
|
|
|
|
fic_alpha_blend(dstptr[0] + ctx->final_frame->linesize[0],
|
|
|
|
planes[0] + (i + 1) * 32, lsize, planes[3] + (i + 1) * 32);
|
|
|
|
fic_alpha_blend(dstptr[1],
|
|
|
|
chroma[0] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
|
|
|
|
fic_alpha_blend(dstptr[2],
|
|
|
|
chroma[1] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
|
|
|
|
|
|
|
|
dstptr[0] += ctx->final_frame->linesize[0] * 2;
|
|
|
|
dstptr[1] += ctx->final_frame->linesize[1];
|
|
|
|
dstptr[2] += ctx->final_frame->linesize[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 11:35:07 +03:00
|
|
|
static int fic_decode_frame(AVCodecContext *avctx, void *data,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
FICContext *ctx = avctx->priv_data;
|
|
|
|
uint8_t *src = avpkt->data;
|
|
|
|
int ret;
|
|
|
|
int slice, nslices;
|
|
|
|
int msize;
|
|
|
|
int tsize;
|
2014-04-20 01:46:52 +03:00
|
|
|
int cur_x, cur_y;
|
2015-06-18 12:28:29 +02:00
|
|
|
int skip_cursor = ctx->skip_cursor;
|
2014-02-07 11:35:07 +03:00
|
|
|
uint8_t *sdata;
|
|
|
|
|
2015-02-10 16:20:32 +02:00
|
|
|
if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0)
|
2014-02-07 11:35:07 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Header + at least one slice (4) */
|
|
|
|
if (avpkt->size < FIC_HEADER_SIZE + 4) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for header. */
|
|
|
|
if (memcmp(src, fic_header, 7))
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
|
|
|
|
|
2014-03-07 17:31:53 +03:00
|
|
|
/* Is it a skip frame? */
|
2014-08-14 06:32:44 +03:00
|
|
|
if (src[17]) {
|
|
|
|
if (!ctx->final_frame) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Initial frame is skipped\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2014-03-07 17:31:53 +03:00
|
|
|
goto skip;
|
2014-08-14 06:32:44 +03:00
|
|
|
}
|
2014-03-07 17:31:53 +03:00
|
|
|
|
2014-02-07 11:35:07 +03:00
|
|
|
nslices = src[13];
|
|
|
|
if (!nslices) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* High or Low Quality Matrix? */
|
|
|
|
ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
|
|
|
|
|
|
|
|
/* Skip cursor data. */
|
|
|
|
tsize = AV_RB24(src + 24);
|
|
|
|
if (tsize > avpkt->size - FIC_HEADER_SIZE) {
|
2014-04-20 01:46:52 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Packet is too small to contain cursor (%d vs %d bytes).\n",
|
|
|
|
tsize, avpkt->size - FIC_HEADER_SIZE);
|
2014-02-07 11:35:07 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2016-05-01 15:30:22 +02:00
|
|
|
if (!tsize || !AV_RL16(src + 37) || !AV_RL16(src + 39))
|
2014-11-03 01:43:08 +02:00
|
|
|
skip_cursor = 1;
|
|
|
|
|
|
|
|
if (!skip_cursor && tsize < 32) {
|
2014-04-20 01:46:52 +03:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Cursor data too small. Skipping cursor.\n");
|
|
|
|
skip_cursor = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cursor position. */
|
|
|
|
cur_x = AV_RL16(src + 33);
|
|
|
|
cur_y = AV_RL16(src + 35);
|
2014-11-03 01:43:08 +02:00
|
|
|
if (!skip_cursor && (cur_x > avctx->width || cur_y > avctx->height)) {
|
2016-05-01 15:27:51 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG,
|
2016-04-18 00:18:54 +02:00
|
|
|
"Invalid cursor position: (%d,%d). Skipping cursor.\n",
|
2014-04-20 01:46:52 +03:00
|
|
|
cur_x, cur_y);
|
|
|
|
skip_cursor = 1;
|
|
|
|
}
|
|
|
|
|
2014-11-03 01:43:08 +02:00
|
|
|
if (!skip_cursor && (AV_RL16(src + 37) != 32 || AV_RL16(src + 39) != 32)) {
|
2014-04-20 01:46:52 +03:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Invalid cursor size. Skipping cursor.\n");
|
|
|
|
skip_cursor = 1;
|
|
|
|
}
|
|
|
|
|
2018-05-05 22:00:01 +02:00
|
|
|
if (!skip_cursor && avpkt->size < CURSOR_OFFSET + sizeof(ctx->cursor_buf)) {
|
|
|
|
skip_cursor = 1;
|
|
|
|
}
|
|
|
|
|
2014-02-07 11:35:07 +03:00
|
|
|
/* Slice height for all but the last slice. */
|
|
|
|
ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
|
|
|
|
if (ctx->slice_h % 16)
|
|
|
|
ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
|
|
|
|
|
|
|
|
/* First slice offset and remaining data. */
|
|
|
|
sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
|
|
|
|
msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
|
|
|
|
|
2019-05-01 20:12:12 +02:00
|
|
|
if (msize <= ctx->aligned_width/8 * (ctx->aligned_height/8) / 8) {
|
2014-02-07 11:35:07 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate slice data. */
|
|
|
|
av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
|
|
|
|
nslices * sizeof(ctx->slice_data[0]));
|
|
|
|
if (!ctx->slice_data_size) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2014-02-15 19:19:59 +03:00
|
|
|
memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0]));
|
2014-02-07 11:35:07 +03:00
|
|
|
|
|
|
|
for (slice = 0; slice < nslices; slice++) {
|
2014-02-15 19:19:32 +03:00
|
|
|
unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
|
|
|
|
unsigned slice_size;
|
2014-02-07 11:35:07 +03:00
|
|
|
int y_off = ctx->slice_h * slice;
|
|
|
|
int slice_h = ctx->slice_h;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either read the slice size, or consume all data left.
|
|
|
|
* Also, special case the last slight height.
|
|
|
|
*/
|
|
|
|
if (slice == nslices - 1) {
|
|
|
|
slice_size = msize;
|
|
|
|
slice_h = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
|
|
|
|
} else {
|
|
|
|
slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
|
2018-12-16 22:43:07 +02:00
|
|
|
if (slice_size < slice_off)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2014-02-07 11:35:07 +03:00
|
|
|
}
|
|
|
|
|
2014-02-15 19:19:32 +03:00
|
|
|
if (slice_size < slice_off || slice_size > msize)
|
2014-02-07 11:35:07 +03:00
|
|
|
continue;
|
|
|
|
|
2014-02-15 19:19:32 +03:00
|
|
|
slice_size -= slice_off;
|
|
|
|
|
2014-02-07 11:35:07 +03:00
|
|
|
ctx->slice_data[slice].src = sdata + slice_off;
|
|
|
|
ctx->slice_data[slice].src_size = slice_size;
|
|
|
|
ctx->slice_data[slice].slice_h = slice_h;
|
|
|
|
ctx->slice_data[slice].y_off = y_off;
|
|
|
|
}
|
|
|
|
|
2014-04-16 03:06:37 +03:00
|
|
|
if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
|
|
|
|
NULL, nslices, sizeof(ctx->slice_data[0]))) < 0)
|
2014-02-07 11:35:07 +03:00
|
|
|
return ret;
|
|
|
|
|
2017-03-29 01:52:07 +02:00
|
|
|
ctx->frame->key_frame = 1;
|
|
|
|
ctx->frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
for (slice = 0; slice < nslices; slice++) {
|
|
|
|
if (ctx->slice_data[slice].p_frame) {
|
|
|
|
ctx->frame->key_frame = 0;
|
|
|
|
ctx->frame->pict_type = AV_PICTURE_TYPE_P;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 01:46:52 +03:00
|
|
|
av_frame_free(&ctx->final_frame);
|
|
|
|
ctx->final_frame = av_frame_clone(ctx->frame);
|
|
|
|
if (!ctx->final_frame) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not clone frame buffer.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we use a user-supplied buffer. */
|
|
|
|
if ((ret = ff_reget_buffer(avctx, ctx->final_frame)) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not make frame writable.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw cursor. */
|
|
|
|
if (!skip_cursor) {
|
2018-05-05 23:42:36 +02:00
|
|
|
memcpy(ctx->cursor_buf, src + CURSOR_OFFSET, sizeof(ctx->cursor_buf));
|
2014-04-20 01:46:52 +03:00
|
|
|
fic_draw_cursor(avctx, cur_x, cur_y);
|
|
|
|
}
|
|
|
|
|
2014-03-07 17:31:53 +03:00
|
|
|
skip:
|
2014-02-07 11:35:07 +03:00
|
|
|
*got_frame = 1;
|
2014-04-20 01:46:52 +03:00
|
|
|
if ((ret = av_frame_ref(data, ctx->final_frame)) < 0)
|
2014-02-07 11:35:07 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int fic_decode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
FICContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
av_freep(&ctx->slice_data);
|
2014-04-20 01:46:52 +03:00
|
|
|
av_frame_free(&ctx->final_frame);
|
2014-02-07 11:35:07 +03:00
|
|
|
av_frame_free(&ctx->frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int fic_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
FICContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
/* Initialize various context values */
|
|
|
|
ctx->avctx = avctx;
|
|
|
|
ctx->aligned_width = FFALIGN(avctx->width, 16);
|
|
|
|
ctx->aligned_height = FFALIGN(avctx->height, 16);
|
|
|
|
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
|
|
avctx->bits_per_raw_sample = 8;
|
|
|
|
|
|
|
|
ctx->frame = av_frame_alloc();
|
|
|
|
if (!ctx->frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-18 12:28:29 +02:00
|
|
|
static const AVOption options[] = {
|
2015-11-21 23:04:39 +02:00
|
|
|
{ "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 },
|
2015-06-18 12:28:29 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass fic_decoder_class = {
|
2018-09-08 20:25:47 +02:00
|
|
|
.class_name = "FIC decoder",
|
2015-06-18 12:28:29 +02:00
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2014-02-07 11:35:07 +03:00
|
|
|
AVCodec ff_fic_decoder = {
|
|
|
|
.name = "fic",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_FIC,
|
|
|
|
.priv_data_size = sizeof(FICContext),
|
|
|
|
.init = fic_decode_init,
|
|
|
|
.decode = fic_decode_frame,
|
|
|
|
.close = fic_decode_close,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
|
2015-06-18 12:28:29 +02:00
|
|
|
.priv_class = &fic_decoder_class,
|
2014-02-07 11:35:07 +03:00
|
|
|
};
|