Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
Mirillis FIC video decoder

Conflicts:
Changelog
configure
libavcodec/Makefile
libavcodec/avcodec.h
libavcodec/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2014/02/09 03:02:46
Showing 10 changed files
... ...
@@ -24,6 +24,7 @@ version <next>
24 24
 - OpenGL device
25 25
 - Use metadata_header_padding to control padding in ID3 tags (currently used in
26 26
   MP3, AIFF, and OMA files), FLAC header, and the AVI "junk" block.
27
+- Mirillis FIC video decoder
27 28
 
28 29
 
29 30
 version 2.1:
... ...
@@ -1915,6 +1915,7 @@ ffv1_decoder_select="dsputil golomb rangecoder"
1915 1915
 ffv1_encoder_select="dsputil rangecoder"
1916 1916
 ffvhuff_decoder_select="dsputil llviddsp"
1917 1917
 ffvhuff_encoder_select="dsputil huffman llviddsp"
1918
+fic_decoder_select="dsputil golomb"
1918 1919
 flac_decoder_select="golomb"
1919 1920
 flac_encoder_select="dsputil golomb lpc"
1920 1921
 flashsv_decoder_select="zlib"
... ...
@@ -301,6 +301,8 @@ library:
301 301
     @tab also known as DVB Transport Stream
302 302
 @item MPEG-4                    @tab X @tab X
303 303
     @tab MPEG-4 is a variant of QuickTime.
304
+@item Mirillis FIC video        @tab   @tab X
305
+    @tab No cursor rendering.
304 306
 @item MIME multipart JPEG       @tab X @tab
305 307
 @item MSN TCP webcam            @tab   @tab X
306 308
     @tab Used by MSN Messenger webcam streams.
... ...
@@ -206,6 +206,7 @@ OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1enc.o ffv1.o
206 206
 OBJS-$(CONFIG_FFVHUFF_DECODER)         += huffyuv.o huffyuvdec.o
207 207
 OBJS-$(CONFIG_FFVHUFF_ENCODER)         += huffyuv.o huffyuvenc.o
208 208
 OBJS-$(CONFIG_FFWAVESYNTH_DECODER)     += ffwavesynth.o
209
+OBJS-$(CONFIG_FIC_DECODER)             += fic.o
209 210
 OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o flacdata.o flac.o flacdsp.o
210 211
 OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o flacdata.o flac.o flacdsp.o vorbis_data.o
211 212
 OBJS-$(CONFIG_FLASHSV_DECODER)         += flashsv.o
... ...
@@ -150,6 +150,7 @@ void avcodec_register_all(void)
150 150
     REGISTER_DECODER(EXR,               exr);
151 151
     REGISTER_ENCDEC (FFV1,              ffv1);
152 152
     REGISTER_ENCDEC (FFVHUFF,           ffvhuff);
153
+    REGISTER_DECODER(FIC,               fic);
153 154
     REGISTER_ENCDEC (FLASHSV,           flashsv);
154 155
     REGISTER_ENCDEC (FLASHSV2,          flashsv2);
155 156
     REGISTER_DECODER(FLIC,              flic);
... ...
@@ -285,6 +285,7 @@ enum AVCodecID {
285 285
     AV_CODEC_ID_WEBP_DEPRECATED,
286 286
     AV_CODEC_ID_HNM4_VIDEO,
287 287
     AV_CODEC_ID_HEVC_DEPRECATED,
288
+    AV_CODEC_ID_FIC,
288 289
 
289 290
     AV_CODEC_ID_BRENDER_PIX= MKBETAG('B','P','I','X'),
290 291
     AV_CODEC_ID_Y41P       = MKBETAG('Y','4','1','P'),
... ...
@@ -1403,6 +1403,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1403 1403
         .long_name = NULL_IF_CONFIG_SMALL("H.265 / HEVC (High Efficiency Video Coding)"),
1404 1404
         .props     = AV_CODEC_PROP_LOSSY,
1405 1405
     },
1406
+    {
1407
+        .id        = AV_CODEC_ID_FIC,
1408
+        .type      = AVMEDIA_TYPE_VIDEO,
1409
+        .name      = "fic",
1410
+        .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
1411
+        .props     = AV_CODEC_PROP_LOSSY,
1412
+    },
1406 1413
 
1407 1414
     /* various PCM "codecs" */
1408 1415
     {
1409 1416
new file mode 100644
... ...
@@ -0,0 +1,298 @@
0
+/*
1
+ * Mirillis FIC decoder
2
+ *
3
+ * Copyright (c) 2014 Konstantin Shishkov
4
+ * Copyright (c) 2014 Derek Buitenhuis
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+
23
+#include "libavutil/common.h"
24
+#include "avcodec.h"
25
+#include "internal.h"
26
+#include "dsputil.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
+    DSPContext dsp;
43
+    ScanTable scantable;
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
+} FICContext;
55
+
56
+static const uint8_t fic_qmat_hq[64] = {
57
+    1, 2, 2, 2, 3, 3, 3, 4,
58
+    2, 2, 2, 3, 3, 3, 4, 4,
59
+    2, 2, 3, 3, 3, 4, 4, 4,
60
+    2, 2, 3, 3, 3, 4, 4, 5,
61
+    2, 3, 3, 3, 4, 4, 5, 6,
62
+    3, 3, 3, 4, 4, 5, 6, 7,
63
+    3, 3, 3, 4, 4, 5, 7, 7,
64
+    3, 3, 4, 4, 5, 7, 7, 7,
65
+};
66
+
67
+static const uint8_t fic_qmat_lq[64] = {
68
+    1,  5,  6,  7,  8,  9,  9, 11,
69
+    5,  5,  7,  8,  9,  9, 11, 12,
70
+    6,  7,  8,  9,  9, 11, 11, 12,
71
+    7,  7,  8,  9,  9, 11, 12, 13,
72
+    7,  8,  9,  9, 10, 11, 13, 16,
73
+    8,  9,  9, 10, 11, 13, 16, 19,
74
+    8,  9,  9, 11, 12, 15, 18, 23,
75
+    9,  9, 11, 12, 15, 18, 23, 27
76
+};
77
+
78
+static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
79
+
80
+#define FIC_HEADER_SIZE 27
81
+
82
+static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
83
+                            uint8_t *dst, int stride, int16_t *block)
84
+{
85
+    int i, num_coeff;
86
+
87
+    /* Is it a skip block? */
88
+    if (get_bits1(gb)) {
89
+        /* This is a P-frame. */
90
+        ctx->frame->key_frame = 0;
91
+        ctx->frame->pict_type = AV_PICTURE_TYPE_P;
92
+
93
+        return 0;
94
+    }
95
+
96
+    ctx->dsp.clear_block(block);
97
+
98
+    num_coeff = get_bits(gb, 7);
99
+    if (num_coeff > 64)
100
+        return AVERROR_INVALIDDATA;
101
+
102
+    for (i = 0; i < num_coeff; i++)
103
+        block[ctx->scantable.permutated[i]] = get_se_golomb(gb) * ctx->qmat[i];
104
+
105
+    ctx->dsp.idct_put(dst, stride, block);
106
+
107
+    return 0;
108
+}
109
+
110
+static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
111
+{
112
+    FICContext *ctx        = avctx->priv_data;
113
+    FICThreadContext *tctx = tdata;
114
+    GetBitContext gb;
115
+    uint8_t *src = tctx->src;
116
+    int slice_h  = tctx->slice_h;
117
+    int src_size = tctx->src_size;
118
+    int y_off    = tctx->y_off;
119
+    int x, y, p;
120
+
121
+    init_get_bits(&gb, src, src_size * 8);
122
+
123
+    for (p = 0; p < 3; p++) {
124
+        int stride   = ctx->frame->linesize[p];
125
+        uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
126
+
127
+        for (y = 0; y < (slice_h >> !!p); y += 8) {
128
+            for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
129
+                int ret;
130
+
131
+                if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, tctx->block)) != 0)
132
+                    return ret;
133
+            }
134
+
135
+            dst += 8 * stride;
136
+        }
137
+    }
138
+
139
+    return 0;
140
+}
141
+
142
+static int fic_decode_frame(AVCodecContext *avctx, void *data,
143
+                            int *got_frame, AVPacket *avpkt)
144
+{
145
+    FICContext *ctx = avctx->priv_data;
146
+    uint8_t *src = avpkt->data;
147
+    int ret;
148
+    int slice, nslices;
149
+    int msize;
150
+    int tsize;
151
+    uint8_t *sdata;
152
+
153
+    if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0) {
154
+        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
155
+        return ret;
156
+    }
157
+
158
+    /* Header + at least one slice (4) */
159
+    if (avpkt->size < FIC_HEADER_SIZE + 4) {
160
+        av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
161
+        return AVERROR_INVALIDDATA;
162
+    }
163
+
164
+    /* Check for header. */
165
+    if (memcmp(src, fic_header, 7))
166
+        av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
167
+
168
+    nslices = src[13];
169
+    if (!nslices) {
170
+        av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
171
+        return AVERROR_INVALIDDATA;
172
+    }
173
+
174
+    /* High or Low Quality Matrix? */
175
+    ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
176
+
177
+    /* Skip cursor data. */
178
+    tsize = AV_RB24(src + 24);
179
+    if (tsize > avpkt->size - FIC_HEADER_SIZE) {
180
+        av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size.\n");
181
+        return AVERROR_INVALIDDATA;
182
+    }
183
+
184
+    /* Slice height for all but the last slice. */
185
+    ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
186
+    if (ctx->slice_h % 16)
187
+        ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
188
+
189
+    /* First slice offset and remaining data. */
190
+    sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
191
+    msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
192
+
193
+    if (msize <= 0) {
194
+        av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
195
+        return AVERROR_INVALIDDATA;
196
+    }
197
+
198
+    /*
199
+     * Set the frametype to I initially. It will be set to P if the frame
200
+     * has any dependencies (skip blocks). There will be a race condition
201
+     * inside the slice decode function to set these, but we do not care.
202
+     * since they will only ever be set to 0/P.
203
+     */
204
+    ctx->frame->key_frame = 1;
205
+    ctx->frame->pict_type = AV_PICTURE_TYPE_I;
206
+
207
+    /* Allocate slice data. */
208
+    av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
209
+                   nslices * sizeof(ctx->slice_data[0]));
210
+    if (!ctx->slice_data_size) {
211
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
212
+        return AVERROR(ENOMEM);
213
+    }
214
+
215
+    for (slice = 0; slice < nslices; slice++) {
216
+        int slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
217
+        int slice_size;
218
+        int y_off   = ctx->slice_h * slice;
219
+        int slice_h = ctx->slice_h;
220
+
221
+        /*
222
+         * Either read the slice size, or consume all data left.
223
+         * Also, special case the last slight height.
224
+         */
225
+        if (slice == nslices - 1) {
226
+            slice_size   = msize;
227
+            slice_h      = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
228
+        } else {
229
+            slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
230
+        }
231
+
232
+        slice_size -= slice_off;
233
+
234
+        if (slice_off > msize || slice_off + slice_size > msize)
235
+            continue;
236
+
237
+        ctx->slice_data[slice].src      = sdata + slice_off;
238
+        ctx->slice_data[slice].src_size = slice_size;
239
+        ctx->slice_data[slice].slice_h  = slice_h;
240
+        ctx->slice_data[slice].y_off    = y_off;
241
+    }
242
+
243
+    if (ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
244
+                             NULL, nslices, sizeof(ctx->slice_data[0])) < 0)
245
+        return ret;
246
+
247
+    *got_frame = 1;
248
+    if ((ret = av_frame_ref(data, ctx->frame)) < 0)
249
+        return ret;
250
+
251
+    return avpkt->size;
252
+}
253
+
254
+static av_cold int fic_decode_close(AVCodecContext *avctx)
255
+{
256
+    FICContext *ctx = avctx->priv_data;
257
+
258
+    av_freep(&ctx->slice_data);
259
+    av_frame_free(&ctx->frame);
260
+
261
+    return 0;
262
+}
263
+
264
+static av_cold int fic_decode_init(AVCodecContext *avctx)
265
+{
266
+    FICContext *ctx = avctx->priv_data;
267
+
268
+    /* Initialize various context values */
269
+    ctx->avctx            = avctx;
270
+    ctx->aligned_width    = FFALIGN(avctx->width,  16);
271
+    ctx->aligned_height   = FFALIGN(avctx->height, 16);
272
+
273
+    avctx->pix_fmt             = AV_PIX_FMT_YUV420P;
274
+    avctx->bits_per_raw_sample = 8;
275
+
276
+    ctx->frame = av_frame_alloc();
277
+    if (!ctx->frame)
278
+        return AVERROR(ENOMEM);
279
+
280
+    ff_dsputil_init(&ctx->dsp, avctx);
281
+
282
+    ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
283
+
284
+    return 0;
285
+}
286
+
287
+AVCodec ff_fic_decoder = {
288
+    .name           = "fic",
289
+    .long_name      = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
290
+    .type           = AVMEDIA_TYPE_VIDEO,
291
+    .id             = AV_CODEC_ID_FIC,
292
+    .priv_data_size = sizeof(FICContext),
293
+    .init           = fic_decode_init,
294
+    .decode         = fic_decode_frame,
295
+    .close          = fic_decode_close,
296
+    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
297
+};
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR  49
33
-#define LIBAVCODEC_VERSION_MICRO 101
32
+#define LIBAVCODEC_VERSION_MINOR  50
33
+#define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 36
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -356,6 +356,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
356 356
     { AV_CODEC_ID_G2M,          MKTAG('G', '2', 'M', '2') },
357 357
     { AV_CODEC_ID_G2M,          MKTAG('G', '2', 'M', '3') },
358 358
     { AV_CODEC_ID_G2M,          MKTAG('G', '2', 'M', '4') },
359
+    { AV_CODEC_ID_FIC,          MKTAG('F', 'I', 'C', 'V') },
359 360
     { AV_CODEC_ID_NONE,         0 }
360 361
 };
361 362