2 * Bitmap Brothers JV video decoder
3 * Copyright (c) 2011 Peter Ross <pross@xvid.org>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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>
28 #include "libavutil/intreadwrite.h"
31 #include "bitstream.h"
35 typedef struct JvContext
{
38 uint32_t palette
[AVPALETTE_COUNT
];
39 int palette_has_changed
;
42 static av_cold
int decode_init(AVCodecContext
*avctx
)
44 JvContext
*s
= avctx
->priv_data
;
46 if (!avctx
->width
|| !avctx
->height
||
47 (avctx
->width
& 7) || (avctx
->height
& 7)) {
48 av_log(avctx
, AV_LOG_ERROR
, "Invalid video dimensions: %dx%d\n",
49 avctx
->width
, avctx
->height
);
50 return AVERROR(EINVAL
);
53 s
->frame
= av_frame_alloc();
55 return AVERROR(ENOMEM
);
57 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
58 ff_blockdsp_init(&s
->bdsp
);
65 static inline void decode2x2(BitstreamContext
*bc
, uint8_t *dst
, int linesize
)
69 switch (bitstream_read(bc
, 2)) {
71 v
[0] = bitstream_read(bc
, 8);
72 for (j
= 0; j
< 2; j
++)
73 memset(dst
+ j
* linesize
, v
[0], 2);
76 v
[0] = bitstream_read(bc
, 8);
77 v
[1] = bitstream_read(bc
, 8);
78 for (j
= 0; j
< 2; j
++)
79 for (i
= 0; i
< 2; i
++)
80 dst
[j
* linesize
+ i
] = v
[bitstream_read_bit(bc
)];
83 for (j
= 0; j
< 2; j
++)
84 for (i
= 0; i
< 2; i
++)
85 dst
[j
* linesize
+ i
] = bitstream_read(bc
, 8);
92 static inline void decode4x4(BitstreamContext
*bc
, uint8_t *dst
, int linesize
)
96 switch (bitstream_read(bc
, 2)) {
98 v
[0] = bitstream_read(bc
, 8);
99 for (j
= 0; j
< 4; j
++)
100 memset(dst
+ j
* linesize
, v
[0], 4);
103 v
[0] = bitstream_read(bc
, 8);
104 v
[1] = bitstream_read(bc
, 8);
105 for (j
= 2; j
>= 0; j
-= 2) {
106 for (i
= 0; i
< 4; i
++)
107 dst
[j
* linesize
+ i
] = v
[bitstream_read_bit(bc
)];
108 for (i
= 0; i
< 4; i
++)
109 dst
[(j
+ 1) * linesize
+ i
] = v
[bitstream_read_bit(bc
)];
113 for (j
= 0; j
< 4; j
+= 2)
114 for (i
= 0; i
< 4; i
+= 2)
115 decode2x2(bc
, dst
+ j
* linesize
+ i
, linesize
);
122 static inline void decode8x8(BitstreamContext
*bc
, uint8_t *dst
, int linesize
,
123 BlockDSPContext
*bdsp
)
127 switch (bitstream_read(bc
, 2)) {
129 v
[0] = bitstream_read(bc
, 8);
130 bdsp
->fill_block_tab
[1](dst
, v
[0], linesize
, 8);
133 v
[0] = bitstream_read(bc
, 8);
134 v
[1] = bitstream_read(bc
, 8);
135 for (j
= 7; j
>= 0; j
--)
136 for (i
= 0; i
< 8; i
++)
137 dst
[j
* linesize
+ i
] = v
[bitstream_read_bit(bc
)];
140 for (j
= 0; j
< 8; j
+= 4)
141 for (i
= 0; i
< 8; i
+= 4)
142 decode4x4(bc
, dst
+ j
* linesize
+ i
, linesize
);
146 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *got_frame
,
149 JvContext
*s
= avctx
->priv_data
;
150 int buf_size
= avpkt
->size
;
151 const uint8_t *buf
= avpkt
->data
;
152 const uint8_t *buf_end
= buf
+ buf_size
;
153 int video_size
, video_type
, i
, j
, ret
;
155 video_size
= AV_RL32(buf
);
160 if ((ret
= ff_reget_buffer(avctx
, s
->frame
)) < 0) {
161 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
165 if (video_type
== 0 || video_type
== 1) {
167 bitstream_init8(&bc
, buf
, FFMIN(video_size
, buf_end
- buf
));
169 for (j
= 0; j
< avctx
->height
; j
+= 8)
170 for (i
= 0; i
< avctx
->width
; i
+= 8)
172 s
->frame
->data
[0] + j
* s
->frame
->linesize
[0] + i
,
173 s
->frame
->linesize
[0], &s
->bdsp
);
176 } else if (video_type
== 2) {
177 if (buf
+ 1 <= buf_end
) {
179 for (j
= 0; j
< avctx
->height
; j
++)
180 memset(s
->frame
->data
[0] + j
* s
->frame
->linesize
[0],
184 av_log(avctx
, AV_LOG_WARNING
,
185 "unsupported frame type %i\n", video_type
);
186 return AVERROR_INVALIDDATA
;
191 for (i
= 0; i
< AVPALETTE_COUNT
&& buf
+ 3 <= buf_end
; i
++) {
192 s
->palette
[i
] = AV_RB24(buf
) << 2;
195 s
->palette_has_changed
= 1;
199 s
->frame
->key_frame
= 1;
200 s
->frame
->pict_type
= AV_PICTURE_TYPE_I
;
201 s
->frame
->palette_has_changed
= s
->palette_has_changed
;
202 s
->palette_has_changed
= 0;
203 memcpy(s
->frame
->data
[1], s
->palette
, AVPALETTE_SIZE
);
205 if ((ret
= av_frame_ref(data
, s
->frame
)) < 0)
213 static av_cold
int decode_close(AVCodecContext
*avctx
)
215 JvContext
*s
= avctx
->priv_data
;
217 av_frame_free(&s
->frame
);
222 AVCodec ff_jv_decoder
= {
224 .long_name
= NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
225 .type
= AVMEDIA_TYPE_VIDEO
,
226 .id
= AV_CODEC_ID_JV
,
227 .priv_data_size
= sizeof(JvContext
),
229 .close
= decode_close
,
230 .decode
= decode_frame
,
231 .capabilities
= AV_CODEC_CAP_DR1
,