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>
31 #include "libavutil/intreadwrite.h"
33 typedef struct JvContext {
36 uint32_t palette[AVPALETTE_COUNT];
37 int palette_has_changed;
40 static av_cold int decode_init(AVCodecContext *avctx)
42 JvContext *s = avctx->priv_data;
43 avctx->pix_fmt = PIX_FMT_PAL8;
44 dsputil_init(&s->dsp, avctx);
51 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
55 switch (get_bits(gb, 2)) {
57 v[0] = get_bits(gb, 8);
58 for (j = 0; j < 2; j++)
59 memset(dst + j*linesize, v[0], 2);
62 v[0] = get_bits(gb, 8);
63 v[1] = get_bits(gb, 8);
64 for (j = 0; j < 2; j++)
65 for (i = 0; i < 2; i++)
66 dst[j*linesize + i] = v[get_bits1(gb)];
69 for (j = 0; j < 2; j++)
70 for (i = 0; i < 2; i++)
71 dst[j*linesize + i] = get_bits(gb, 8);
78 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
82 switch (get_bits(gb, 2)) {
84 v[0] = get_bits(gb, 8);
85 for (j = 0; j < 4; j++)
86 memset(dst + j*linesize, v[0], 4);
89 v[0] = get_bits(gb, 8);
90 v[1] = get_bits(gb, 8);
91 for (j = 2; j >= 0; j -= 2) {
92 for (i = 0; i < 4; i++)
93 dst[j*linesize + i] = v[get_bits1(gb)];
94 for (i = 0; i < 4; i++)
95 dst[(j+1)*linesize + i] = v[get_bits1(gb)];
99 for (j = 0; j < 4; j += 2)
100 for (i = 0; i < 4; i += 2)
101 decode2x2(gb, dst + j*linesize + i, linesize);
108 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
112 switch (get_bits(gb, 2)) {
114 v[0] = get_bits(gb, 8);
115 dsp->fill_block_tab[1](dst, v[0], linesize, 8);
118 v[0] = get_bits(gb, 8);
119 v[1] = get_bits(gb, 8);
120 for (j = 7; j >= 0; j--)
121 for (i = 0; i < 8; i++)
122 dst[j*linesize + i] = v[get_bits1(gb)];
125 for (j = 0; j < 8; j += 4)
126 for (i = 0; i < 8; i += 4)
127 decode4x4(gb, dst + j*linesize + i, linesize);
131 static int decode_frame(AVCodecContext *avctx,
132 void *data, int *data_size,
135 JvContext *s = avctx->priv_data;
136 int buf_size = avpkt->size;
137 const uint8_t *buf = avpkt->data;
138 const uint8_t *buf_end = buf + buf_size;
139 int video_size, video_type, i, j;
141 video_size = AV_RL32(buf);
146 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
147 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
151 if (video_type == 0 || video_type == 1) {
153 init_get_bits(&gb, buf, FFMIN(video_size, (buf_end - buf) * 8));
155 for (j = 0; j < avctx->height; j += 8)
156 for (i = 0; i < avctx->width; i += 8)
157 decode8x8(&gb, s->frame.data[0] + j*s->frame.linesize[0] + i,
158 s->frame.linesize[0], &s->dsp);
161 } else if (video_type == 2) {
162 if (buf + 1 <= buf_end) {
164 for (j = 0; j < avctx->height; j++)
165 memset(s->frame.data[0] + j*s->frame.linesize[0], v, avctx->width);
168 av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
169 return AVERROR_INVALIDDATA;
174 for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
175 uint32_t pal = AV_RB24(buf);
176 s->palette[i] = 0xFF << 24 | pal << 2 | ((pal >> 4) & 0x30303);
179 s->palette_has_changed = 1;
183 s->frame.key_frame = 1;
184 s->frame.pict_type = AV_PICTURE_TYPE_I;
185 s->frame.palette_has_changed = s->palette_has_changed;
186 s->palette_has_changed = 0;
187 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
189 *data_size = sizeof(AVFrame);
190 *(AVFrame*)data = s->frame;
196 static av_cold int decode_close(AVCodecContext *avctx)
198 JvContext *s = avctx->priv_data;
201 avctx->release_buffer(avctx, &s->frame);
206 AVCodec ff_jv_decoder = {
208 .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
209 .type = AVMEDIA_TYPE_VIDEO,
211 .priv_data_size = sizeof(JvContext),
213 .close = decode_close,
214 .decode = decode_frame,
215 .capabilities = CODEC_CAP_DR1,