]> git.sesse.net Git - ffmpeg/blob - libavcodec/fic.c
Merge commit 'f4d5a2cc35fcdf06ec031fabe8b0710e995fe924'
[ffmpeg] / libavcodec / fic.c
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 "avcodec.h"
26 #include "internal.h"
27 #include "get_bits.h"
28 #include "golomb.h"
29
30 typedef struct FICThreadContext {
31     DECLARE_ALIGNED(16, int16_t, block)[64];
32     uint8_t *src;
33     int slice_h;
34     int src_size;
35     int y_off;
36 } FICThreadContext;
37
38 typedef struct FICContext {
39     AVCodecContext *avctx;
40     AVFrame *frame;
41
42     FICThreadContext *slice_data;
43     int slice_data_size;
44
45     const uint8_t *qmat;
46
47     enum AVPictureType cur_frame_type;
48
49     int aligned_width, aligned_height;
50     int num_slices, slice_h;
51 } FICContext;
52
53 static const uint8_t fic_qmat_hq[64] = {
54     1, 2, 2, 2, 3, 3, 3, 4,
55     2, 2, 2, 3, 3, 3, 4, 4,
56     2, 2, 3, 3, 3, 4, 4, 4,
57     2, 2, 3, 3, 3, 4, 4, 5,
58     2, 3, 3, 3, 4, 4, 5, 6,
59     3, 3, 3, 4, 4, 5, 6, 7,
60     3, 3, 3, 4, 4, 5, 7, 7,
61     3, 3, 4, 4, 5, 7, 7, 7,
62 };
63
64 static const uint8_t fic_qmat_lq[64] = {
65     1,  5,  6,  7,  8,  9,  9, 11,
66     5,  5,  7,  8,  9,  9, 11, 12,
67     6,  7,  8,  9,  9, 11, 11, 12,
68     7,  7,  8,  9,  9, 11, 12, 13,
69     7,  8,  9,  9, 10, 11, 13, 16,
70     8,  9,  9, 10, 11, 13, 16, 19,
71     8,  9,  9, 11, 12, 15, 18, 23,
72     9,  9, 11, 12, 15, 18, 23, 27
73 };
74
75 static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
76
77 #define FIC_HEADER_SIZE 27
78
79 static av_always_inline void fic_idct(int16_t *blk, int step, int shift)
80 {
81     const int t0 =  27246 * blk[3 * step] + 18405 * blk[5 * step];
82     const int t1 =  27246 * blk[5 * step] - 18405 * blk[3 * step];
83     const int t2 =   6393 * blk[7 * step] + 32139 * blk[1 * step];
84     const int t3 =   6393 * blk[1 * step] - 32139 * blk[7 * step];
85     const int t4 = 5793 * (t2 + t0 + 0x800 >> 12);
86     const int t5 = 5793 * (t3 + t1 + 0x800 >> 12);
87     const int t6 = t2 - t0;
88     const int t7 = t3 - t1;
89     const int t8 =  17734 * blk[2 * step] - 42813 * blk[6 * step];
90     const int t9 =  17734 * blk[6 * step] + 42814 * blk[2 * step];
91     const int tA = (blk[0 * step] - blk[4 * step] << 15) + (1 << shift - 1);
92     const int tB = (blk[0 * step] + blk[4 * step] << 15) + (1 << shift - 1);
93     blk[0 * step] = (  t4       + t9 + tB) >> shift;
94     blk[1 * step] = (  t6 + t7  + t8 + tA) >> shift;
95     blk[2 * step] = (  t6 - t7  - t8 + tA) >> shift;
96     blk[3 * step] = (  t5       - t9 + tB) >> shift;
97     blk[4 * step] = ( -t5       - t9 + tB) >> shift;
98     blk[5 * step] = (-(t6 - t7) - t8 + tA) >> shift;
99     blk[6 * step] = (-(t6 + t7) + t8 + tA) >> shift;
100     blk[7 * step] = ( -t4       + t9 + tB) >> shift;
101 }
102
103 static void fic_idct_put(uint8_t *dst, int stride, int16_t *block)
104 {
105     int i, j;
106     int16_t *ptr;
107
108     ptr = block;
109     for (i = 0; i < 8; i++) {
110         fic_idct(ptr, 8, 13);
111         ptr++;
112     }
113
114     ptr = block;
115     for (i = 0; i < 8; i++) {
116         fic_idct(ptr, 1, 20);
117         ptr += 8;
118     }
119
120     ptr = block;
121     for (j = 0; j < 8; j++) {
122         for (i = 0; i < 8; i++)
123             dst[i] = av_clip_uint8(ptr[i]);
124         dst += stride;
125         ptr += 8;
126     }
127 }
128 static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
129                             uint8_t *dst, int stride, int16_t *block)
130 {
131     int i, num_coeff;
132
133     /* Is it a skip block? */
134     if (get_bits1(gb)) {
135         /* This is a P-frame. */
136         ctx->frame->key_frame = 0;
137         ctx->frame->pict_type = AV_PICTURE_TYPE_P;
138
139         return 0;
140     }
141
142     memset(block, 0, sizeof(*block) * 64);
143
144     num_coeff = get_bits(gb, 7);
145     if (num_coeff > 64)
146         return AVERROR_INVALIDDATA;
147
148     for (i = 0; i < num_coeff; i++)
149         block[ff_zigzag_direct[i]] = get_se_golomb(gb) *
150                                      ctx->qmat[ff_zigzag_direct[i]];
151
152     fic_idct_put(dst, stride, block);
153
154     return 0;
155 }
156
157 static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
158 {
159     FICContext *ctx        = avctx->priv_data;
160     FICThreadContext *tctx = tdata;
161     GetBitContext gb;
162     uint8_t *src = tctx->src;
163     int slice_h  = tctx->slice_h;
164     int src_size = tctx->src_size;
165     int y_off    = tctx->y_off;
166     int x, y, p;
167
168     init_get_bits(&gb, src, src_size * 8);
169
170     for (p = 0; p < 3; p++) {
171         int stride   = ctx->frame->linesize[p];
172         uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
173
174         for (y = 0; y < (slice_h >> !!p); y += 8) {
175             for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
176                 int ret;
177
178                 if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, tctx->block)) != 0)
179                     return ret;
180             }
181
182             dst += 8 * stride;
183         }
184     }
185
186     return 0;
187 }
188
189 static int fic_decode_frame(AVCodecContext *avctx, void *data,
190                             int *got_frame, AVPacket *avpkt)
191 {
192     FICContext *ctx = avctx->priv_data;
193     uint8_t *src = avpkt->data;
194     int ret;
195     int slice, nslices;
196     int msize;
197     int tsize;
198     uint8_t *sdata;
199
200     if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0) {
201         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
202         return ret;
203     }
204
205     /* Header + at least one slice (4) */
206     if (avpkt->size < FIC_HEADER_SIZE + 4) {
207         av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
208         return AVERROR_INVALIDDATA;
209     }
210
211     /* Check for header. */
212     if (memcmp(src, fic_header, 7))
213         av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
214
215     /* Is it a skip frame? */
216     if (src[17])
217         goto skip;
218
219     nslices = src[13];
220     if (!nslices) {
221         av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
222         return AVERROR_INVALIDDATA;
223     }
224
225     /* High or Low Quality Matrix? */
226     ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
227
228     /* Skip cursor data. */
229     tsize = AV_RB24(src + 24);
230     if (tsize > avpkt->size - FIC_HEADER_SIZE) {
231         av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size.\n");
232         return AVERROR_INVALIDDATA;
233     }
234
235     /* Slice height for all but the last slice. */
236     ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
237     if (ctx->slice_h % 16)
238         ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
239
240     /* First slice offset and remaining data. */
241     sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
242     msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
243
244     if (msize <= 0) {
245         av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
246         return AVERROR_INVALIDDATA;
247     }
248
249     /*
250      * Set the frametype to I initially. It will be set to P if the frame
251      * has any dependencies (skip blocks). There will be a race condition
252      * inside the slice decode function to set these, but we do not care.
253      * since they will only ever be set to 0/P.
254      */
255     ctx->frame->key_frame = 1;
256     ctx->frame->pict_type = AV_PICTURE_TYPE_I;
257
258     /* Allocate slice data. */
259     av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
260                    nslices * sizeof(ctx->slice_data[0]));
261     if (!ctx->slice_data_size) {
262         av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
263         return AVERROR(ENOMEM);
264     }
265     memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0]));
266
267     for (slice = 0; slice < nslices; slice++) {
268         unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
269         unsigned slice_size;
270         int y_off   = ctx->slice_h * slice;
271         int slice_h = ctx->slice_h;
272
273         /*
274          * Either read the slice size, or consume all data left.
275          * Also, special case the last slight height.
276          */
277         if (slice == nslices - 1) {
278             slice_size   = msize;
279             slice_h      = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
280         } else {
281             slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
282         }
283
284         if (slice_size < slice_off || slice_size > msize)
285             continue;
286
287         slice_size -= slice_off;
288
289         ctx->slice_data[slice].src      = sdata + slice_off;
290         ctx->slice_data[slice].src_size = slice_size;
291         ctx->slice_data[slice].slice_h  = slice_h;
292         ctx->slice_data[slice].y_off    = y_off;
293     }
294
295     if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
296                               NULL, nslices, sizeof(ctx->slice_data[0]))) < 0)
297         return ret;
298
299 skip:
300     *got_frame = 1;
301     if ((ret = av_frame_ref(data, ctx->frame)) < 0)
302         return ret;
303
304     return avpkt->size;
305 }
306
307 static av_cold int fic_decode_close(AVCodecContext *avctx)
308 {
309     FICContext *ctx = avctx->priv_data;
310
311     av_freep(&ctx->slice_data);
312     av_frame_free(&ctx->frame);
313
314     return 0;
315 }
316
317 static av_cold int fic_decode_init(AVCodecContext *avctx)
318 {
319     FICContext *ctx = avctx->priv_data;
320
321     /* Initialize various context values */
322     ctx->avctx            = avctx;
323     ctx->aligned_width    = FFALIGN(avctx->width,  16);
324     ctx->aligned_height   = FFALIGN(avctx->height, 16);
325
326     avctx->pix_fmt             = AV_PIX_FMT_YUV420P;
327     avctx->bits_per_raw_sample = 8;
328
329     ctx->frame = av_frame_alloc();
330     if (!ctx->frame)
331         return AVERROR(ENOMEM);
332
333     return 0;
334 }
335
336 AVCodec ff_fic_decoder = {
337     .name           = "fic",
338     .long_name      = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
339     .type           = AVMEDIA_TYPE_VIDEO,
340     .id             = AV_CODEC_ID_FIC,
341     .priv_data_size = sizeof(FICContext),
342     .init           = fic_decode_init,
343     .decode         = fic_decode_frame,
344     .close          = fic_decode_close,
345     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
346 };