]> git.sesse.net Git - ffmpeg/blob - libavcodec/jvdec.c
Merge commit 'be7c323176e2e5fcf30e3d2ff20975b2f936811b'
[ffmpeg] / libavcodec / jvdec.c
1 /*
2  * Bitmap Brothers JV video decoder
3  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Bitmap Brothers JV video decoder
25  * @author Peter Ross <pross@xvid.org>
26  */
27
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "get_bits.h"
31 #include "internal.h"
32 #include "libavutil/intreadwrite.h"
33
34 typedef struct JvContext {
35     DSPContext dsp;
36     AVFrame   *frame;
37     uint32_t   palette[AVPALETTE_COUNT];
38     int        palette_has_changed;
39 } JvContext;
40
41 static av_cold int decode_init(AVCodecContext *avctx)
42 {
43     JvContext *s = avctx->priv_data;
44
45     s->frame = av_frame_alloc();
46     if (!s->frame)
47         return AVERROR(ENOMEM);
48
49     avctx->pix_fmt = AV_PIX_FMT_PAL8;
50     ff_dsputil_init(&s->dsp, avctx);
51     return 0;
52 }
53
54 /**
55  * Decode 2x2 block
56  */
57 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
58 {
59     int i, j, v[2];
60
61     switch (get_bits(gb, 2)) {
62     case 1:
63         v[0] = get_bits(gb, 8);
64         for (j = 0; j < 2; j++)
65             memset(dst + j*linesize, v[0], 2);
66         break;
67     case 2:
68         v[0] = get_bits(gb, 8);
69         v[1] = get_bits(gb, 8);
70         for (j = 0; j < 2; j++)
71             for (i = 0; i < 2; i++)
72                 dst[j*linesize + i] = v[get_bits1(gb)];
73         break;
74     case 3:
75         for (j = 0; j < 2; j++)
76             for (i = 0; i < 2; i++)
77                 dst[j*linesize + i] = get_bits(gb, 8);
78     }
79 }
80
81 /**
82  * Decode 4x4 block
83  */
84 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
85 {
86     int i, j, v[2];
87
88     switch (get_bits(gb, 2)) {
89     case 1:
90         v[0] = get_bits(gb, 8);
91         for (j = 0; j < 4; j++)
92             memset(dst + j*linesize, v[0], 4);
93         break;
94     case 2:
95         v[0] = get_bits(gb, 8);
96         v[1] = get_bits(gb, 8);
97         for (j = 2; j >= 0; j -= 2) {
98             for (i = 0; i < 4; i++)
99                 dst[j*linesize + i]     = v[get_bits1(gb)];
100             for (i = 0; i < 4; i++)
101                 dst[(j+1)*linesize + i] = v[get_bits1(gb)];
102         }
103         break;
104     case 3:
105         for (j = 0; j < 4; j += 2)
106             for (i = 0; i < 4; i += 2)
107                 decode2x2(gb, dst + j*linesize + i, linesize);
108     }
109 }
110
111 /**
112  * Decode 8x8 block
113  */
114 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
115 {
116     int i, j, v[2];
117
118     switch (get_bits(gb, 2)) {
119     case 1:
120         v[0] = get_bits(gb, 8);
121         dsp->fill_block_tab[1](dst, v[0], linesize, 8);
122         break;
123     case 2:
124         v[0] = get_bits(gb, 8);
125         v[1] = get_bits(gb, 8);
126         for (j = 7; j >= 0; j--)
127             for (i = 0; i <  8; i++)
128                 dst[j*linesize + i] = v[get_bits1(gb)];
129         break;
130     case 3:
131         for (j = 0; j < 8; j += 4)
132             for (i = 0; i < 8; i += 4)
133                 decode4x4(gb, dst + j*linesize + i, linesize);
134     }
135 }
136
137 static int decode_frame(AVCodecContext *avctx,
138                         void *data, int *got_frame,
139                         AVPacket *avpkt)
140 {
141     JvContext *s           = avctx->priv_data;
142     const uint8_t *buf     = avpkt->data;
143     const uint8_t *buf_end = buf + avpkt->size;
144     int video_size, video_type, i, j, ret;
145
146     if (avpkt->size < 6)
147         return AVERROR_INVALIDDATA;
148
149     video_size = AV_RL32(buf);
150     video_type = buf[4];
151     buf += 5;
152
153     if (video_size) {
154         if (video_size < 0 || video_size > avpkt->size - 5) {
155             av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
156             return AVERROR_INVALIDDATA;
157         }
158         if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
159             return ret;
160
161         if (video_type == 0 || video_type == 1) {
162             GetBitContext gb;
163             init_get_bits(&gb, buf, 8 * video_size);
164
165             for (j = 0; j < avctx->height; j += 8)
166                 for (i = 0; i < avctx->width; i += 8)
167                     decode8x8(&gb, s->frame->data[0] + j * s->frame->linesize[0] + i,
168                               s->frame->linesize[0], &s->dsp);
169
170             buf += video_size;
171         } else if (video_type == 2) {
172             int v = *buf++;
173             for (j = 0; j < avctx->height; j++)
174                 memset(s->frame->data[0] + j * s->frame->linesize[0], v, avctx->width);
175         } else {
176             av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
177             return AVERROR_INVALIDDATA;
178         }
179     }
180
181     if (buf_end - buf >= AVPALETTE_COUNT * 3) {
182         for (i = 0; i < AVPALETTE_COUNT; i++) {
183             uint32_t pal = AV_RB24(buf);
184             s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
185             buf += 3;
186         }
187         s->palette_has_changed = 1;
188     }
189
190     if (video_size) {
191         s->frame->key_frame           = 1;
192         s->frame->pict_type           = AV_PICTURE_TYPE_I;
193         s->frame->palette_has_changed = s->palette_has_changed;
194         s->palette_has_changed       = 0;
195         memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
196
197         if ((ret = av_frame_ref(data, s->frame)) < 0)
198             return ret;
199         *got_frame = 1;
200     }
201
202     return avpkt->size;
203 }
204
205 static av_cold int decode_close(AVCodecContext *avctx)
206 {
207     JvContext *s = avctx->priv_data;
208
209     av_frame_free(&s->frame);
210
211     return 0;
212 }
213
214 AVCodec ff_jv_decoder = {
215     .name           = "jv",
216     .long_name      = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
217     .type           = AVMEDIA_TYPE_VIDEO,
218     .id             = AV_CODEC_ID_JV,
219     .priv_data_size = sizeof(JvContext),
220     .init           = decode_init,
221     .close          = decode_close,
222     .decode         = decode_frame,
223     .capabilities   = CODEC_CAP_DR1,
224 };