asvdec.c 10.2 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2003 Michael Niedermayer
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * License along with FFmpeg; if not, write to the Free Software
18 19 20 21 22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * ASUS V1/V2 decoder.
 */

26
#include "libavutil/attributes.h"
27 28 29 30
#include "libavutil/mem.h"

#include "asv.h"
#include "avcodec.h"
31
#include "blockdsp.h"
32
#include "idctdsp.h"
33
#include "internal.h"
34
#include "mathops.h"
35 36 37 38 39 40 41 42 43 44 45
#include "mpeg12data.h"

#define VLC_BITS 6
#define ASV2_LEVEL_VLC_BITS 10

static VLC ccp_vlc;
static VLC level_vlc;
static VLC dc_ccp_vlc;
static VLC ac_ccp_vlc;
static VLC asv2_level_vlc;

46 47
static av_cold void init_vlcs(ASV1Context *a)
{
48 49 50 51 52 53
    static int done = 0;

    if (!done) {
        done = 1;

        INIT_VLC_STATIC(&ccp_vlc, VLC_BITS, 17,
54 55
                        &ff_asv_ccp_tab[0][1], 2, 1,
                        &ff_asv_ccp_tab[0][0], 2, 1, 64);
56
        INIT_VLC_STATIC(&dc_ccp_vlc, VLC_BITS, 8,
57 58
                        &ff_asv_dc_ccp_tab[0][1], 2, 1,
                        &ff_asv_dc_ccp_tab[0][0], 2, 1, 64);
59
        INIT_VLC_STATIC(&ac_ccp_vlc, VLC_BITS, 16,
60 61
                        &ff_asv_ac_ccp_tab[0][1], 2, 1,
                        &ff_asv_ac_ccp_tab[0][0], 2, 1, 64);
62
        INIT_VLC_STATIC(&level_vlc,  VLC_BITS, 7,
63 64
                        &ff_asv_level_tab[0][1], 2, 1,
                        &ff_asv_level_tab[0][0], 2, 1, 64);
65
        INIT_VLC_STATIC(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63,
66 67
                        &ff_asv2_level_tab[0][1], 2, 1,
                        &ff_asv2_level_tab[0][0], 2, 1, 1024);
68 69 70 71
    }
}

//FIXME write a reversed bitstream reader to avoid the double reverse
72 73 74
static inline int asv2_get_bits(GetBitContext *gb, int n)
{
    return ff_reverse[get_bits(gb, n) << (8-n)];
75 76
}

77 78 79
static inline int asv1_get_level(GetBitContext *gb)
{
    int code = get_vlc2(gb, level_vlc.table, VLC_BITS, 1);
80

81 82 83 84
    if (code == 3)
        return get_sbits(gb, 8);
    else
        return code - 3;
85 86
}

87 88 89
static inline int asv2_get_level(GetBitContext *gb)
{
    int code = get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1);
90

91 92 93 94
    if (code == 31)
        return (int8_t)asv2_get_bits(gb, 8);
    else
        return code - 31;
95 96
}

Diego Biurrun's avatar
Diego Biurrun committed
97
static inline int asv1_decode_block(ASV1Context *a, int16_t block[64])
98
{
99 100
    int i;

101
    block[0] = 8 * get_bits(&a->gb, 8);
102

103 104
    for (i = 0; i < 11; i++) {
        const int ccp = get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
105

106 107 108 109
        if (ccp) {
            if (ccp == 16)
                break;
            if (ccp < 0 || i >= 10) {
110
                av_log(a->avctx, AV_LOG_ERROR, "coded coeff pattern damaged\n");
111
                return AVERROR_INVALIDDATA;
112 113
            }

114 115 116 117 118 119 120 121
            if (ccp & 8)
                block[a->scantable.permutated[4 * i + 0]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 0]) >> 4;
            if (ccp & 4)
                block[a->scantable.permutated[4 * i + 1]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 1]) >> 4;
            if (ccp & 2)
                block[a->scantable.permutated[4 * i + 2]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 2]) >> 4;
            if (ccp & 1)
                block[a->scantable.permutated[4 * i + 3]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 3]) >> 4;
122 123 124 125 126 127
        }
    }

    return 0;
}

Diego Biurrun's avatar
Diego Biurrun committed
128
static inline int asv2_decode_block(ASV1Context *a, int16_t block[64])
129
{
130 131
    int i, count, ccp;

132
    count = asv2_get_bits(&a->gb, 4);
133

134
    block[0] = 8 * asv2_get_bits(&a->gb, 8);
135

136 137 138 139 140 141 142 143
    ccp = get_vlc2(&a->gb, dc_ccp_vlc.table, VLC_BITS, 1);
    if (ccp) {
        if (ccp & 4)
            block[a->scantable.permutated[1]] = (asv2_get_level(&a->gb) * a->intra_matrix[1]) >> 4;
        if (ccp & 2)
            block[a->scantable.permutated[2]] = (asv2_get_level(&a->gb) * a->intra_matrix[2]) >> 4;
        if (ccp & 1)
            block[a->scantable.permutated[3]] = (asv2_get_level(&a->gb) * a->intra_matrix[3]) >> 4;
144 145
    }

146 147 148 149 150 151 152 153 154 155 156 157
    for (i = 1; i < count + 1; i++) {
        const int ccp = get_vlc2(&a->gb, ac_ccp_vlc.table, VLC_BITS, 1);

        if (ccp) {
            if (ccp & 8)
                block[a->scantable.permutated[4*i + 0]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 0]) >> 4;
            if (ccp & 4)
                block[a->scantable.permutated[4*i + 1]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 1]) >> 4;
            if (ccp & 2)
                block[a->scantable.permutated[4*i + 2]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 2]) >> 4;
            if (ccp & 1)
                block[a->scantable.permutated[4*i + 3]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 3]) >> 4;
158 159 160 161 162 163
        }
    }

    return 0;
}

Diego Biurrun's avatar
Diego Biurrun committed
164
static inline int decode_mb(ASV1Context *a, int16_t block[6][64])
165
{
166 167
    int i;

168
    a->bdsp.clear_blocks(block[0]);
169

170 171 172
    if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
        for (i = 0; i < 6; i++) {
            if (asv1_decode_block(a, block[i]) < 0)
173 174
                return -1;
        }
175 176 177
    } else {
        for (i = 0; i < 6; i++) {
            if (asv2_decode_block(a, block[i]) < 0)
178 179 180 181 182 183
                return -1;
        }
    }
    return 0;
}

184
static inline void idct_put(ASV1Context *a, AVFrame *frame, int mb_x, int mb_y)
185
{
Diego Biurrun's avatar
Diego Biurrun committed
186
    int16_t (*block)[64] = a->block;
187
    int linesize         = frame->linesize[0];
188

189 190 191
    uint8_t *dest_y  = frame->data[0] + (mb_y * 16* linesize              ) + mb_x * 16;
    uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
    uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
192

193 194 195 196
    a->idsp.idct_put(dest_y,                    linesize, block[0]);
    a->idsp.idct_put(dest_y + 8,                linesize, block[1]);
    a->idsp.idct_put(dest_y + 8 * linesize,     linesize, block[2]);
    a->idsp.idct_put(dest_y + 8 * linesize + 8, linesize, block[3]);
197

198
    if (!(a->avctx->flags&CODEC_FLAG_GRAY)) {
199 200
        a->idsp.idct_put(dest_cb, frame->linesize[1], block[4]);
        a->idsp.idct_put(dest_cr, frame->linesize[2], block[5]);
201 202 203 204
    }
}

static int decode_frame(AVCodecContext *avctx,
205
                        void *data, int *got_frame,
206 207 208
                        AVPacket *avpkt)
{
    ASV1Context * const a = avctx->priv_data;
209 210
    const uint8_t *buf    = avpkt->data;
    int buf_size          = avpkt->size;
211
    AVFrame * const p     = data;
212
    int mb_x, mb_y, ret;
213

214
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
215
        return ret;
216 217
    p->pict_type = AV_PICTURE_TYPE_I;
    p->key_frame = 1;
218 219 220 221 222 223

    av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size,
                          buf_size);
    if (!a->bitstream_buffer)
        return AVERROR(ENOMEM);

224
    if (avctx->codec_id == AV_CODEC_ID_ASV1)
225 226
        a->bbdsp.bswap_buf((uint32_t *) a->bitstream_buffer,
                           (const uint32_t *) buf, buf_size / 4);
227
    else {
228
        int i;
229 230
        for (i = 0; i < buf_size; i++)
            a->bitstream_buffer[i] = ff_reverse[buf[i]];
231 232 233 234
    }

    init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);

235 236
    for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
        for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
237 238
            if ((ret = decode_mb(a, a->block)) < 0)
                return ret;
239

240
            idct_put(a, p, mb_x, mb_y);
241 242 243
        }
    }

244 245 246
    if (a->mb_width2 != a->mb_width) {
        mb_x = a->mb_width2;
        for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
247 248
            if ((ret = decode_mb(a, a->block)) < 0)
                return ret;
249

250
            idct_put(a, p, mb_x, mb_y);
251 252 253
        }
    }

254 255 256
    if (a->mb_height2 != a->mb_height) {
        mb_y = a->mb_height2;
        for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
257 258
            if ((ret = decode_mb(a, a->block)) < 0)
                return ret;
259

260
            idct_put(a, p, mb_x, mb_y);
261 262 263
        }
    }

264
    *got_frame = 1;
265 266 267

    emms_c();

268
    return (get_bits_count(&a->gb) + 31) / 32 * 4;
269 270
}

271 272
static av_cold int decode_init(AVCodecContext *avctx)
{
273
    ASV1Context * const a = avctx->priv_data;
274
    const int scale       = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
275 276
    int i;

277
    if (avctx->extradata_size < 1) {
278
        av_log(avctx, AV_LOG_WARNING, "No extradata provided\n");
279 280
    }

281
    ff_asv_common_init(avctx);
282
    ff_blockdsp_init(&a->bdsp, avctx);
283
    ff_idctdsp_init(&a->idsp, avctx);
284
    init_vlcs(a);
285
    ff_init_scantable(a->idsp.idct_permutation, &a->scantable, ff_asv_scantab);
286
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
287

288
    if (avctx->extradata_size < 1 || (a->inv_qscale = avctx->extradata[0]) == 0) {
289
        av_log(avctx, AV_LOG_ERROR, "illegal qscale 0\n");
290 291
        if (avctx->codec_id == AV_CODEC_ID_ASV1)
            a->inv_qscale = 6;
292
        else
293
            a->inv_qscale = 10;
294 295
    }

296
    for (i = 0; i < 64; i++) {
297 298
        int index = ff_asv_scantab[i];

299
        a->intra_matrix[i] = 64 * scale * ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
300 301 302 303 304
    }

    return 0;
}

305 306
static av_cold int decode_end(AVCodecContext *avctx)
{
307 308 309
    ASV1Context * const a = avctx->priv_data;

    av_freep(&a->bitstream_buffer);
310
    a->bitstream_buffer_size = 0;
311 312 313 314

    return 0;
}

315
#if CONFIG_ASV1_DECODER
316 317
AVCodec ff_asv1_decoder = {
    .name           = "asv1",
318
    .long_name      = NULL_IF_CONFIG_SMALL("ASUS V1"),
319 320 321 322 323 324 325 326
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_ASV1,
    .priv_data_size = sizeof(ASV1Context),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
};
327
#endif
328

329
#if CONFIG_ASV2_DECODER
330 331
AVCodec ff_asv2_decoder = {
    .name           = "asv2",
332
    .long_name      = NULL_IF_CONFIG_SMALL("ASUS V2"),
333 334 335 336 337 338 339 340
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_ASV2,
    .priv_data_size = sizeof(ASV1Context),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
};
341
#endif
342