~ubuntu-branches/debian/experimental/libav/experimental

« back to all changes in this revision

Viewing changes to libavcodec/fic.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-02-17 22:07:03 UTC
  • mfrom: (1.1.25)
  • Revision ID: package-import@ubuntu.com-20140217220703-51bpzx0ln1d961vs
Tags: 6:10~beta1-2
* New Upstream release 10_beta1. This upstream git snapshot has too many
  changes to list here, cf. to the upstream Changelog:
  http://git.libav.org/?p=libav.git;a=blob;f=Changelog;hb=refs/tags/v10_beta1
  - works with H.264 that has different bit depth between chroma and luma,
    Closes: #738599
* Bump shlibs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Libav.
 
8
 *
 
9
 * Libav 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
 * Libav 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 Libav; 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 "dsputil.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
    AVCodecContext *avctx;
 
41
    AVFrame *frame;
 
42
 
 
43
    DSPContext dsp;
 
44
    ScanTable scantable;
 
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
} FICContext;
 
56
 
 
57
static const uint8_t fic_qmat_hq[64] = {
 
58
    1, 2, 2, 2, 3, 3, 3, 4,
 
59
    2, 2, 2, 3, 3, 3, 4, 4,
 
60
    2, 2, 3, 3, 3, 4, 4, 4,
 
61
    2, 2, 3, 3, 3, 4, 4, 5,
 
62
    2, 3, 3, 3, 4, 4, 5, 6,
 
63
    3, 3, 3, 4, 4, 5, 6, 7,
 
64
    3, 3, 3, 4, 4, 5, 7, 7,
 
65
    3, 3, 4, 4, 5, 7, 7, 7,
 
66
};
 
67
 
 
68
static const uint8_t fic_qmat_lq[64] = {
 
69
    1,  5,  6,  7,  8,  9,  9, 11,
 
70
    5,  5,  7,  8,  9,  9, 11, 12,
 
71
    6,  7,  8,  9,  9, 11, 11, 12,
 
72
    7,  7,  8,  9,  9, 11, 12, 13,
 
73
    7,  8,  9,  9, 10, 11, 13, 16,
 
74
    8,  9,  9, 10, 11, 13, 16, 19,
 
75
    8,  9,  9, 11, 12, 15, 18, 23,
 
76
    9,  9, 11, 12, 15, 18, 23, 27
 
77
};
 
78
 
 
79
static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
 
80
 
 
81
#define FIC_HEADER_SIZE 27
 
82
 
 
83
static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
 
84
                            uint8_t *dst, int stride, int16_t *block)
 
85
{
 
86
    int i, num_coeff;
 
87
 
 
88
    /* Is it a skip block? */
 
89
    if (get_bits1(gb)) {
 
90
        /* This is a P-frame. */
 
91
        ctx->frame->key_frame = 0;
 
92
        ctx->frame->pict_type = AV_PICTURE_TYPE_P;
 
93
 
 
94
        return 0;
 
95
    }
 
96
 
 
97
    ctx->dsp.clear_block(block);
 
98
 
 
99
    num_coeff = get_bits(gb, 7);
 
100
    if (num_coeff > 64)
 
101
        return AVERROR_INVALIDDATA;
 
102
 
 
103
    for (i = 0; i < num_coeff; i++)
 
104
        block[ctx->scantable.permutated[i]] = get_se_golomb(gb) * ctx->qmat[i];
 
105
 
 
106
    ctx->dsp.idct_put(dst, stride, block);
 
107
 
 
108
    return 0;
 
109
}
 
110
 
 
111
static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
 
112
{
 
113
    FICContext *ctx        = avctx->priv_data;
 
114
    FICThreadContext *tctx = tdata;
 
115
    GetBitContext gb;
 
116
    uint8_t *src = tctx->src;
 
117
    int slice_h  = tctx->slice_h;
 
118
    int src_size = tctx->src_size;
 
119
    int y_off    = tctx->y_off;
 
120
    int x, y, p;
 
121
 
 
122
    init_get_bits(&gb, src, src_size * 8);
 
123
 
 
124
    for (p = 0; p < 3; p++) {
 
125
        int stride   = ctx->frame->linesize[p];
 
126
        uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
 
127
 
 
128
        for (y = 0; y < (slice_h >> !!p); y += 8) {
 
129
            for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
 
130
                int ret;
 
131
 
 
132
                if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, tctx->block)) != 0)
 
133
                    return ret;
 
134
            }
 
135
 
 
136
            dst += 8 * stride;
 
137
        }
 
138
    }
 
139
 
 
140
    return 0;
 
141
}
 
142
 
 
143
static int fic_decode_frame(AVCodecContext *avctx, void *data,
 
144
                            int *got_frame, AVPacket *avpkt)
 
145
{
 
146
    FICContext *ctx = avctx->priv_data;
 
147
    uint8_t *src = avpkt->data;
 
148
    int ret;
 
149
    int slice, nslices;
 
150
    int msize;
 
151
    int tsize;
 
152
    uint8_t *sdata;
 
153
 
 
154
    if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0) {
 
155
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
 
156
        return ret;
 
157
    }
 
158
 
 
159
    /* Header + at least one slice (4) */
 
160
    if (avpkt->size < FIC_HEADER_SIZE + 4) {
 
161
        av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
 
162
        return AVERROR_INVALIDDATA;
 
163
    }
 
164
 
 
165
    /* Check for header. */
 
166
    if (memcmp(src, fic_header, 7))
 
167
        av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
 
168
 
 
169
    nslices = src[13];
 
170
    if (!nslices) {
 
171
        av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
 
172
        return AVERROR_INVALIDDATA;
 
173
    }
 
174
 
 
175
    /* High or Low Quality Matrix? */
 
176
    ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
 
177
 
 
178
    /* Skip cursor data. */
 
179
    tsize = AV_RB24(src + 24);
 
180
    if (tsize > avpkt->size - FIC_HEADER_SIZE) {
 
181
        av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size.\n");
 
182
        return AVERROR_INVALIDDATA;
 
183
    }
 
184
 
 
185
    /* Slice height for all but the last slice. */
 
186
    ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
 
187
    if (ctx->slice_h % 16)
 
188
        ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
 
189
 
 
190
    /* First slice offset and remaining data. */
 
191
    sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
 
192
    msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
 
193
 
 
194
    if (msize <= 0) {
 
195
        av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
 
196
        return AVERROR_INVALIDDATA;
 
197
    }
 
198
 
 
199
    /*
 
200
     * Set the frametype to I initially. It will be set to P if the frame
 
201
     * has any dependencies (skip blocks). There will be a race condition
 
202
     * inside the slice decode function to set these, but we do not care.
 
203
     * since they will only ever be set to 0/P.
 
204
     */
 
205
    ctx->frame->key_frame = 1;
 
206
    ctx->frame->pict_type = AV_PICTURE_TYPE_I;
 
207
 
 
208
    /* Allocate slice data. */
 
209
    av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
 
210
                   nslices * sizeof(ctx->slice_data[0]));
 
211
    if (!ctx->slice_data_size) {
 
212
        av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
 
213
        return AVERROR(ENOMEM);
 
214
    }
 
215
 
 
216
    for (slice = 0; slice < nslices; slice++) {
 
217
        int slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
 
218
        int slice_size;
 
219
        int y_off   = ctx->slice_h * slice;
 
220
        int slice_h = ctx->slice_h;
 
221
 
 
222
        /*
 
223
         * Either read the slice size, or consume all data left.
 
224
         * Also, special case the last slight height.
 
225
         */
 
226
        if (slice == nslices - 1) {
 
227
            slice_size   = msize;
 
228
            slice_h      = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
 
229
        } else {
 
230
            slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
 
231
        }
 
232
 
 
233
        slice_size -= slice_off;
 
234
 
 
235
        if (slice_off > msize || slice_off + slice_size > msize)
 
236
            continue;
 
237
 
 
238
        ctx->slice_data[slice].src      = sdata + slice_off;
 
239
        ctx->slice_data[slice].src_size = slice_size;
 
240
        ctx->slice_data[slice].slice_h  = slice_h;
 
241
        ctx->slice_data[slice].y_off    = y_off;
 
242
    }
 
243
 
 
244
    if (ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
 
245
                             NULL, nslices, sizeof(ctx->slice_data[0])) < 0)
 
246
        return ret;
 
247
 
 
248
    *got_frame = 1;
 
249
    if ((ret = av_frame_ref(data, ctx->frame)) < 0)
 
250
        return ret;
 
251
 
 
252
    return avpkt->size;
 
253
}
 
254
 
 
255
static av_cold int fic_decode_close(AVCodecContext *avctx)
 
256
{
 
257
    FICContext *ctx = avctx->priv_data;
 
258
 
 
259
    av_freep(&ctx->slice_data);
 
260
    av_frame_free(&ctx->frame);
 
261
 
 
262
    return 0;
 
263
}
 
264
 
 
265
static av_cold int fic_decode_init(AVCodecContext *avctx)
 
266
{
 
267
    FICContext *ctx = avctx->priv_data;
 
268
 
 
269
    /* Initialize various context values */
 
270
    ctx->avctx            = avctx;
 
271
    ctx->aligned_width    = FFALIGN(avctx->width,  16);
 
272
    ctx->aligned_height   = FFALIGN(avctx->height, 16);
 
273
 
 
274
    avctx->pix_fmt             = AV_PIX_FMT_YUV420P;
 
275
    avctx->bits_per_raw_sample = 8;
 
276
 
 
277
    ctx->frame = av_frame_alloc();
 
278
    if (!ctx->frame)
 
279
        return AVERROR(ENOMEM);
 
280
 
 
281
    ff_dsputil_init(&ctx->dsp, avctx);
 
282
 
 
283
    ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
 
284
 
 
285
    return 0;
 
286
}
 
287
 
 
288
AVCodec ff_fic_decoder = {
 
289
    .name           = "fic",
 
290
    .long_name      = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
 
291
    .type           = AVMEDIA_TYPE_VIDEO,
 
292
    .id             = AV_CODEC_ID_FIC,
 
293
    .priv_data_size = sizeof(FICContext),
 
294
    .init           = fic_decode_init,
 
295
    .decode         = fic_decode_frame,
 
296
    .close          = fic_decode_close,
 
297
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
 
298
};