Line | Branch | Exec | Source |
---|---|---|---|
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 "libavutil/intreadwrite.h" | ||
29 | |||
30 | #include "avcodec.h" | ||
31 | #include "blockdsp.h" | ||
32 | #include "codec_internal.h" | ||
33 | #include "decode.h" | ||
34 | #include "get_bits.h" | ||
35 | |||
36 | typedef struct JvContext { | ||
37 | BlockDSPContext bdsp; | ||
38 | AVFrame *frame; | ||
39 | uint32_t palette[AVPALETTE_COUNT]; | ||
40 | #if FF_API_PALETTE_HAS_CHANGED | ||
41 | int palette_has_changed; | ||
42 | #endif | ||
43 | } JvContext; | ||
44 | |||
45 | 3 | static av_cold int decode_init(AVCodecContext *avctx) | |
46 | { | ||
47 | 3 | JvContext *s = avctx->priv_data; | |
48 | |||
49 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (!avctx->width || !avctx->height || |
50 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | (avctx->width & 7) || (avctx->height & 7)) { |
51 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n", | |
52 | avctx->width, avctx->height); | ||
53 | ✗ | return AVERROR(EINVAL); | |
54 | } | ||
55 | |||
56 | 3 | s->frame = av_frame_alloc(); | |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!s->frame) |
58 | ✗ | return AVERROR(ENOMEM); | |
59 | |||
60 | 3 | avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
61 | 3 | ff_blockdsp_init(&s->bdsp); | |
62 | 3 | return 0; | |
63 | } | ||
64 | |||
65 | /** | ||
66 | * Decode 2x2 block | ||
67 | */ | ||
68 | 14056 | static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize) | |
69 | { | ||
70 | int i, j, v[2]; | ||
71 | |||
72 |
4/4✓ Branch 1 taken 348 times.
✓ Branch 2 taken 9984 times.
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 3673 times.
|
14056 | switch (get_bits(gb, 2)) { |
73 | 348 | case 1: | |
74 | 348 | v[0] = get_bits(gb, 8); | |
75 |
2/2✓ Branch 0 taken 696 times.
✓ Branch 1 taken 348 times.
|
1044 | for (j = 0; j < 2; j++) |
76 | 696 | memset(dst + j * linesize, v[0], 2); | |
77 | 348 | break; | |
78 | 9984 | case 2: | |
79 | 9984 | v[0] = get_bits(gb, 8); | |
80 | 9984 | v[1] = get_bits(gb, 8); | |
81 |
2/2✓ Branch 0 taken 19968 times.
✓ Branch 1 taken 9984 times.
|
29952 | for (j = 0; j < 2; j++) |
82 |
2/2✓ Branch 0 taken 39936 times.
✓ Branch 1 taken 19968 times.
|
59904 | for (i = 0; i < 2; i++) |
83 | 39936 | dst[j * linesize + i] = v[get_bits1(gb)]; | |
84 | 9984 | break; | |
85 | 51 | case 3: | |
86 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 51 times.
|
153 | for (j = 0; j < 2; j++) |
87 |
2/2✓ Branch 0 taken 204 times.
✓ Branch 1 taken 102 times.
|
306 | for (i = 0; i < 2; i++) |
88 | 204 | dst[j * linesize + i] = get_bits(gb, 8); | |
89 | } | ||
90 | 14056 | } | |
91 | |||
92 | /** | ||
93 | * Decode 4x4 block | ||
94 | */ | ||
95 | 17028 | static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize) | |
96 | { | ||
97 | int i, j, v[2]; | ||
98 | |||
99 |
4/4✓ Branch 1 taken 139 times.
✓ Branch 2 taken 11062 times.
✓ Branch 3 taken 3514 times.
✓ Branch 4 taken 2313 times.
|
17028 | switch (get_bits(gb, 2)) { |
100 | 139 | case 1: | |
101 | 139 | v[0] = get_bits(gb, 8); | |
102 |
2/2✓ Branch 0 taken 556 times.
✓ Branch 1 taken 139 times.
|
695 | for (j = 0; j < 4; j++) |
103 | 556 | memset(dst + j * linesize, v[0], 4); | |
104 | 139 | break; | |
105 | 11062 | case 2: | |
106 | 11062 | v[0] = get_bits(gb, 8); | |
107 | 11062 | v[1] = get_bits(gb, 8); | |
108 |
2/2✓ Branch 0 taken 22124 times.
✓ Branch 1 taken 11062 times.
|
33186 | for (j = 2; j >= 0; j -= 2) { |
109 |
2/2✓ Branch 0 taken 88496 times.
✓ Branch 1 taken 22124 times.
|
110620 | for (i = 0; i < 4; i++) |
110 | 88496 | dst[j * linesize + i] = v[get_bits1(gb)]; | |
111 |
2/2✓ Branch 0 taken 88496 times.
✓ Branch 1 taken 22124 times.
|
110620 | for (i = 0; i < 4; i++) |
112 | 88496 | dst[(j + 1) * linesize + i] = v[get_bits1(gb)]; | |
113 | } | ||
114 | 11062 | break; | |
115 | 3514 | case 3: | |
116 |
2/2✓ Branch 0 taken 7028 times.
✓ Branch 1 taken 3514 times.
|
10542 | for (j = 0; j < 4; j += 2) |
117 |
2/2✓ Branch 0 taken 14056 times.
✓ Branch 1 taken 7028 times.
|
21084 | for (i = 0; i < 4; i += 2) |
118 | 14056 | decode2x2(gb, dst + j * linesize + i, linesize); | |
119 | } | ||
120 | 17028 | } | |
121 | |||
122 | /** | ||
123 | * Decode 8x8 block | ||
124 | */ | ||
125 | 6000 | static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, | |
126 | BlockDSPContext *bdsp) | ||
127 | { | ||
128 | int i, j, v[2]; | ||
129 | |||
130 |
4/4✓ Branch 1 taken 29 times.
✓ Branch 2 taken 218 times.
✓ Branch 3 taken 4257 times.
✓ Branch 4 taken 1496 times.
|
6000 | switch (get_bits(gb, 2)) { |
131 | 29 | case 1: | |
132 | 29 | v[0] = get_bits(gb, 8); | |
133 | 29 | bdsp->fill_block_tab[1](dst, v[0], linesize, 8); | |
134 | 29 | break; | |
135 | 218 | case 2: | |
136 | 218 | v[0] = get_bits(gb, 8); | |
137 | 218 | v[1] = get_bits(gb, 8); | |
138 |
2/2✓ Branch 0 taken 1744 times.
✓ Branch 1 taken 218 times.
|
1962 | for (j = 7; j >= 0; j--) |
139 |
2/2✓ Branch 0 taken 13952 times.
✓ Branch 1 taken 1744 times.
|
15696 | for (i = 0; i < 8; i++) |
140 | 13952 | dst[j * linesize + i] = v[get_bits1(gb)]; | |
141 | 218 | break; | |
142 | 4257 | case 3: | |
143 |
2/2✓ Branch 0 taken 8514 times.
✓ Branch 1 taken 4257 times.
|
12771 | for (j = 0; j < 8; j += 4) |
144 |
2/2✓ Branch 0 taken 17028 times.
✓ Branch 1 taken 8514 times.
|
25542 | for (i = 0; i < 8; i += 4) |
145 | 17028 | decode4x4(gb, dst + j * linesize + i, linesize); | |
146 | } | ||
147 | 6000 | } | |
148 | |||
149 | 9 | static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
150 | int *got_frame, AVPacket *avpkt) | ||
151 | { | ||
152 | 9 | JvContext *s = avctx->priv_data; | |
153 | 9 | const uint8_t *buf = avpkt->data; | |
154 | 9 | const uint8_t *buf_end = buf + avpkt->size; | |
155 | int video_size, video_type, i, j, ret; | ||
156 | |||
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (avpkt->size < 6) |
158 | ✗ | return AVERROR_INVALIDDATA; | |
159 | |||
160 | 9 | video_size = AV_RL32(buf); | |
161 | 9 | video_type = buf[4]; | |
162 | 9 | buf += 5; | |
163 | |||
164 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | if (video_size) { |
165 |
3/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7 times.
|
8 | if (video_size < 0 || video_size > avpkt->size - 5) { |
166 | 1 | av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size); | |
167 | 1 | return AVERROR_INVALIDDATA; | |
168 | } | ||
169 | |||
170 |
3/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
|
7 | if (video_type == 0 || video_type == 1) { |
171 | GetBitContext gb; | ||
172 | 6 | init_get_bits(&gb, buf, 8 * video_size); | |
173 | |||
174 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0) |
175 | ✗ | return ret; | |
176 | |||
177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (avctx->height/8 * (avctx->width/8) > 4 * video_size) { |
178 | ✗ | av_log(avctx, AV_LOG_ERROR, "Insufficient input data for dimensions\n"); | |
179 | ✗ | return AVERROR_INVALIDDATA; | |
180 | } | ||
181 | |||
182 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 6 times.
|
156 | for (j = 0; j < avctx->height; j += 8) |
183 |
2/2✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 150 times.
|
6150 | for (i = 0; i < avctx->width; i += 8) |
184 | 6000 | decode8x8(&gb, | |
185 | 6000 | s->frame->data[0] + j * s->frame->linesize[0] + i, | |
186 | 6000 | s->frame->linesize[0], &s->bdsp); | |
187 | |||
188 | 6 | buf += video_size; | |
189 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (video_type == 2) { |
190 | 1 | int v = *buf++; | |
191 | |||
192 | 1 | av_frame_unref(s->frame); | |
193 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF)) < 0) |
194 | ✗ | return ret; | |
195 | |||
196 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1 times.
|
201 | for (j = 0; j < avctx->height; j++) |
197 | 200 | memset(s->frame->data[0] + j * s->frame->linesize[0], | |
198 | 200 | v, avctx->width); | |
199 | } else { | ||
200 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
201 | "unsupported frame type %i\n", video_type); | ||
202 | ✗ | return AVERROR_INVALIDDATA; | |
203 | } | ||
204 | } | ||
205 | |||
206 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (buf_end - buf >= AVPALETTE_COUNT * 3) { |
207 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 1 times.
|
257 | for (i = 0; i < AVPALETTE_COUNT; i++) { |
208 | 256 | uint32_t pal = AV_RB24(buf); | |
209 | 256 | s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303); | |
210 | 256 | buf += 3; | |
211 | } | ||
212 | #if FF_API_PALETTE_HAS_CHANGED | ||
213 | 1 | s->palette_has_changed = 1; | |
214 | #endif | ||
215 | } | ||
216 | |||
217 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | if (video_size) { |
218 | #if FF_API_PALETTE_HAS_CHANGED | ||
219 | FF_DISABLE_DEPRECATION_WARNINGS | ||
220 | 7 | s->frame->palette_has_changed = s->palette_has_changed; | |
221 | 7 | s->palette_has_changed = 0; | |
222 | FF_ENABLE_DEPRECATION_WARNINGS | ||
223 | #endif | ||
224 | 7 | memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE); | |
225 | |||
226 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((ret = av_frame_ref(rframe, s->frame)) < 0) |
227 | ✗ | return ret; | |
228 | 7 | *got_frame = 1; | |
229 | } | ||
230 | |||
231 | 8 | return avpkt->size; | |
232 | } | ||
233 | |||
234 | 3 | static av_cold int decode_close(AVCodecContext *avctx) | |
235 | { | ||
236 | 3 | JvContext *s = avctx->priv_data; | |
237 | |||
238 | 3 | av_frame_free(&s->frame); | |
239 | |||
240 | 3 | return 0; | |
241 | } | ||
242 | |||
243 | const FFCodec ff_jv_decoder = { | ||
244 | .p.name = "jv", | ||
245 | CODEC_LONG_NAME("Bitmap Brothers JV video"), | ||
246 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
247 | .p.id = AV_CODEC_ID_JV, | ||
248 | .priv_data_size = sizeof(JvContext), | ||
249 | .init = decode_init, | ||
250 | .close = decode_close, | ||
251 | FF_CODEC_DECODE_CB(decode_frame), | ||
252 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
253 | }; | ||
254 |