Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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