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

/**
 * @file
 * Bitmap Brothers JV video decoder
 * @author Peter Ross <pross@xvid.org>
 */

28 29
#include "libavutil/intreadwrite.h"

30
#include "avcodec.h"
31
#include "blockdsp.h"
32
#include "get_bits.h"
33
#include "internal.h"
34 35

typedef struct JvContext {
36
    BlockDSPContext bdsp;
37
    AVFrame   *frame;
38 39 40 41 42 43 44
    uint32_t   palette[AVPALETTE_COUNT];
    int        palette_has_changed;
} JvContext;

static av_cold int decode_init(AVCodecContext *avctx)
{
    JvContext *s = avctx->priv_data;
45

46 47 48 49 50 51 52
    if (!avctx->width || !avctx->height ||
        (avctx->width & 7) || (avctx->height & 7)) {
        av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
               avctx->width, avctx->height);
        return AVERROR(EINVAL);
    }

53 54 55
    s->frame = av_frame_alloc();
    if (!s->frame)
        return AVERROR(ENOMEM);
56

57
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
58
    ff_blockdsp_init(&s->bdsp, avctx);
59 60 61 62 63 64 65 66 67 68 69 70 71 72
    return 0;
}

/**
 * Decode 2x2 block
 */
static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
{
    int i, j, v[2];

    switch (get_bits(gb, 2)) {
    case 1:
        v[0] = get_bits(gb, 8);
        for (j = 0; j < 2; j++)
73
            memset(dst + j * linesize, v[0], 2);
74 75 76 77 78 79
        break;
    case 2:
        v[0] = get_bits(gb, 8);
        v[1] = get_bits(gb, 8);
        for (j = 0; j < 2; j++)
            for (i = 0; i < 2; i++)
80
                dst[j * linesize + i] = v[get_bits1(gb)];
81 82 83 84
        break;
    case 3:
        for (j = 0; j < 2; j++)
            for (i = 0; i < 2; i++)
85
                dst[j * linesize + i] = get_bits(gb, 8);
86 87 88 89 90 91 92 93 94 95 96 97 98 99
    }
}

/**
 * Decode 4x4 block
 */
static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
{
    int i, j, v[2];

    switch (get_bits(gb, 2)) {
    case 1:
        v[0] = get_bits(gb, 8);
        for (j = 0; j < 4; j++)
100
            memset(dst + j * linesize, v[0], 4);
101 102 103 104 105 106
        break;
    case 2:
        v[0] = get_bits(gb, 8);
        v[1] = get_bits(gb, 8);
        for (j = 2; j >= 0; j -= 2) {
            for (i = 0; i < 4; i++)
107
                dst[j * linesize + i] = v[get_bits1(gb)];
108
            for (i = 0; i < 4; i++)
109
                dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
110 111 112 113 114
        }
        break;
    case 3:
        for (j = 0; j < 4; j += 2)
            for (i = 0; i < 4; i += 2)
115
                decode2x2(gb, dst + j * linesize + i, linesize);
116 117 118 119 120 121
    }
}

/**
 * Decode 8x8 block
 */
122
static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
123
                             BlockDSPContext *bdsp)
124 125 126 127 128 129
{
    int i, j, v[2];

    switch (get_bits(gb, 2)) {
    case 1:
        v[0] = get_bits(gb, 8);
130
        bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
131 132 133 134 135
        break;
    case 2:
        v[0] = get_bits(gb, 8);
        v[1] = get_bits(gb, 8);
        for (j = 7; j >= 0; j--)
136 137
            for (i = 0; i < 8; i++)
                dst[j * linesize + i] = v[get_bits1(gb)];
138 139 140 141
        break;
    case 3:
        for (j = 0; j < 8; j += 4)
            for (i = 0; i < 8; i += 4)
142
                decode4x4(gb, dst + j * linesize + i, linesize);
143 144 145
    }
}

146
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
147 148
                        AVPacket *avpkt)
{
149 150
    JvContext *s = avctx->priv_data;
    const uint8_t *buf = avpkt->data;
Paul B Mahol's avatar
Paul B Mahol committed
151
    const uint8_t *buf_end = buf + avpkt->size;
152
    int video_size, video_type, i, j, ret;
153

154 155 156
    if (avpkt->size < 6)
        return AVERROR_INVALIDDATA;

157 158 159 160 161
    video_size = AV_RL32(buf);
    video_type = buf[4];
    buf += 5;

    if (video_size) {
162
        if (video_size < 0 || video_size > avpkt->size - 5) {
163 164 165
            av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
            return AVERROR_INVALIDDATA;
        }
166
        if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
167
            return ret;
168 169 170

        if (video_type == 0 || video_type == 1) {
            GetBitContext gb;
171
            init_get_bits(&gb, buf, 8 * video_size);
172 173 174

            for (j = 0; j < avctx->height; j += 8)
                for (i = 0; i < avctx->width; i += 8)
175 176
                    decode8x8(&gb,
                              s->frame->data[0] + j * s->frame->linesize[0] + i,
177
                              s->frame->linesize[0], &s->bdsp);
178 179 180

            buf += video_size;
        } else if (video_type == 2) {
181 182
            int v = *buf++;
            for (j = 0; j < avctx->height; j++)
183 184
                memset(s->frame->data[0] + j * s->frame->linesize[0],
                       v, avctx->width);
185
        } else {
186 187
            av_log(avctx, AV_LOG_WARNING,
                   "unsupported frame type %i\n", video_type);
188 189 190 191
            return AVERROR_INVALIDDATA;
        }
    }

192 193
    if (buf_end - buf >= AVPALETTE_COUNT * 3) {
        for (i = 0; i < AVPALETTE_COUNT; i++) {
194
            uint32_t pal = AV_RB24(buf);
195
            s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
196 197 198 199 200 201
            buf += 3;
        }
        s->palette_has_changed = 1;
    }

    if (video_size) {
202 203 204
        s->frame->key_frame           = 1;
        s->frame->pict_type           = AV_PICTURE_TYPE_I;
        s->frame->palette_has_changed = s->palette_has_changed;
205
        s->palette_has_changed        = 0;
206
        memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
207

208
        if ((ret = av_frame_ref(data, s->frame)) < 0)
209
            return ret;
210
        *got_frame = 1;
211 212
    }

Paul B Mahol's avatar
Paul B Mahol committed
213
    return avpkt->size;
214 215 216 217 218 219
}

static av_cold int decode_close(AVCodecContext *avctx)
{
    JvContext *s = avctx->priv_data;

220
    av_frame_free(&s->frame);
221 222 223 224 225 226 227

    return 0;
}

AVCodec ff_jv_decoder = {
    .name           = "jv",
    .long_name      = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
228
    .type           = AVMEDIA_TYPE_VIDEO,
229
    .id             = AV_CODEC_ID_JV,
230 231 232 233
    .priv_data_size = sizeof(JvContext),
    .init           = decode_init,
    .close          = decode_close,
    .decode         = decode_frame,
234
    .capabilities   = AV_CODEC_CAP_DR1,
235
};