2 * Bitmap Brothers JV video decoder
3 * Copyright (c) 2011 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Bitmap Brothers JV video decoder
25 * @author Peter Ross <pross@xvid.org>
32 #include "libavutil/intreadwrite.h"
34 typedef struct JvContext {
37 uint32_t palette[AVPALETTE_COUNT];
38 int palette_has_changed;
41 static av_cold int decode_init(AVCodecContext *avctx)
43 JvContext *s = avctx->priv_data;
44 avctx->pix_fmt = AV_PIX_FMT_PAL8;
45 ff_dsputil_init(&s->dsp, avctx);
46 s->frame = av_frame_alloc();
48 return AVERROR(ENOMEM);
55 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
59 switch (get_bits(gb, 2)) {
61 v[0] = get_bits(gb, 8);
62 for (j = 0; j < 2; j++)
63 memset(dst + j*linesize, v[0], 2);
66 v[0] = get_bits(gb, 8);
67 v[1] = get_bits(gb, 8);
68 for (j = 0; j < 2; j++)
69 for (i = 0; i < 2; i++)
70 dst[j*linesize + i] = v[get_bits1(gb)];
73 for (j = 0; j < 2; j++)
74 for (i = 0; i < 2; i++)
75 dst[j*linesize + i] = get_bits(gb, 8);
82 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
86 switch (get_bits(gb, 2)) {
88 v[0] = get_bits(gb, 8);
89 for (j = 0; j < 4; j++)
90 memset(dst + j*linesize, v[0], 4);
93 v[0] = get_bits(gb, 8);
94 v[1] = get_bits(gb, 8);
95 for (j = 2; j >= 0; j -= 2) {
96 for (i = 0; i < 4; i++)
97 dst[j*linesize + i] = v[get_bits1(gb)];
98 for (i = 0; i < 4; i++)
99 dst[(j+1)*linesize + i] = v[get_bits1(gb)];
103 for (j = 0; j < 4; j += 2)
104 for (i = 0; i < 4; i += 2)
105 decode2x2(gb, dst + j*linesize + i, linesize);
112 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
116 switch (get_bits(gb, 2)) {
118 v[0] = get_bits(gb, 8);
119 dsp->fill_block_tab[1](dst, v[0], linesize, 8);
122 v[0] = get_bits(gb, 8);
123 v[1] = get_bits(gb, 8);
124 for (j = 7; j >= 0; j--)
125 for (i = 0; i < 8; i++)
126 dst[j*linesize + i] = v[get_bits1(gb)];
129 for (j = 0; j < 8; j += 4)
130 for (i = 0; i < 8; i += 4)
131 decode4x4(gb, dst + j*linesize + i, linesize);
135 static int decode_frame(AVCodecContext *avctx,
136 void *data, int *got_frame,
139 JvContext *s = avctx->priv_data;
140 const uint8_t *buf = avpkt->data;
141 const uint8_t *buf_end = buf + avpkt->size;
142 int video_size, video_type, i, j, ret;
145 return AVERROR_INVALIDDATA;
147 video_size = AV_RL32(buf);
152 if (video_size < 0 || video_size > avpkt->size - 5) {
153 av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
154 return AVERROR_INVALIDDATA;
156 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
159 if (video_type == 0 || video_type == 1) {
161 init_get_bits(&gb, buf, 8 * video_size);
163 for (j = 0; j < avctx->height; j += 8)
164 for (i = 0; i < avctx->width; i += 8)
165 decode8x8(&gb, s->frame->data[0] + j * s->frame->linesize[0] + i,
166 s->frame->linesize[0], &s->dsp);
169 } else if (video_type == 2) {
171 for (j = 0; j < avctx->height; j++)
172 memset(s->frame->data[0] + j * s->frame->linesize[0], v, avctx->width);
174 av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
175 return AVERROR_INVALIDDATA;
179 if (buf_end - buf >= AVPALETTE_COUNT * 3) {
180 for (i = 0; i < AVPALETTE_COUNT; i++) {
181 uint32_t pal = AV_RB24(buf);
182 s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
185 s->palette_has_changed = 1;
189 s->frame->key_frame = 1;
190 s->frame->pict_type = AV_PICTURE_TYPE_I;
191 s->frame->palette_has_changed = s->palette_has_changed;
192 s->palette_has_changed = 0;
193 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
195 if ((ret = av_frame_ref(data, s->frame)) < 0)
203 static av_cold int decode_close(AVCodecContext *avctx)
205 JvContext *s = avctx->priv_data;
207 av_frame_free(&s->frame);
212 AVCodec ff_jv_decoder = {
214 .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = AV_CODEC_ID_JV,
217 .priv_data_size = sizeof(JvContext),
219 .close = decode_close,
220 .decode = decode_frame,
221 .capabilities = CODEC_CAP_DR1,