FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/kmvc.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 124 210 59.0%
Functions: 4 4 100.0%
Branches: 90 156 57.7%

Line Branch Exec Source
1 /*
2 * KMVC decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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 * Karl Morton's Video Codec decoder
25 */
26
27 #include <stdio.h>
28
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "codec_internal.h"
32 #include "decode.h"
33 #include "libavutil/common.h"
34
35 #define KMVC_KEYFRAME 0x80
36 #define KMVC_PALETTE 0x40
37 #define KMVC_METHOD 0x0F
38 #define MAX_PALSIZE 256
39
40 /*
41 * Decoder context
42 */
43 typedef struct KmvcContext {
44 AVCodecContext *avctx;
45
46 GetByteContext g;
47 uint8_t *cur, *prev;
48 int setpal;
49 int palsize;
50 uint32_t pal[MAX_PALSIZE];
51 uint8_t frm0[320 * 200], frm1[320 * 200];
52 } KmvcContext;
53
54 typedef struct BitBuf {
55 int bits;
56 int bitbuf;
57 } BitBuf;
58
59 #define BLK(data, x, y) data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
60
61 #define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
62
63 #define kmvc_getbit(bb, g, res) {\
64 res = 0; \
65 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
66 bb.bits--; \
67 if(bb.bits == -1) { \
68 bb.bitbuf = bytestream2_get_byte(g); \
69 bb.bits = 7; \
70 } \
71 }
72
73 1 static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
74 {
75 BitBuf bb;
76 int res, val;
77 int i, j;
78 int bx, by;
79 int l0x, l1x, l0y, l1y;
80 int mx, my;
81
82 1 kmvc_init_getbits(bb, &ctx->g);
83
84
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 for (by = 0; by < h; by += 8)
85
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 20 times.
820 for (bx = 0; bx < w; bx += 8) {
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 800 times.
800 if (!bytestream2_get_bytes_left(&ctx->g)) {
87 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
88 return AVERROR_INVALIDDATA;
89 }
90
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 700 times.
800 kmvc_getbit(bb, &ctx->g, res);
91
1/2
✓ Branch 0 taken 800 times.
✗ Branch 1 not taken.
800 if (!res) { // fill whole 8x8 block
92 800 val = bytestream2_get_byte(&ctx->g);
93
2/2
✓ Branch 0 taken 51200 times.
✓ Branch 1 taken 800 times.
52000 for (i = 0; i < 64; i++)
94 51200 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
95 } else { // handle four 4x4 subblocks
96 for (i = 0; i < 4; i++) {
97 l0x = bx + (i & 1) * 4;
98 l0y = by + (i & 2) * 2;
99 kmvc_getbit(bb, &ctx->g, res);
100 if (!res) {
101 kmvc_getbit(bb, &ctx->g, res);
102 if (!res) { // fill whole 4x4 block
103 val = bytestream2_get_byte(&ctx->g);
104 for (j = 0; j < 16; j++)
105 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
106 } else { // copy block from already decoded place
107 val = bytestream2_get_byte(&ctx->g);
108 mx = val & 0xF;
109 my = val >> 4;
110 if ((l0x-mx) + 320*(l0y-my) < 0 || (l0x-mx) + 320*(l0y-my) > 320*197 - 4) {
111 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
112 return AVERROR_INVALIDDATA;
113 }
114 for (j = 0; j < 16; j++)
115 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
116 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
117 }
118 } else { // descend to 2x2 sub-sub-blocks
119 for (j = 0; j < 4; j++) {
120 l1x = l0x + (j & 1) * 2;
121 l1y = l0y + (j & 2);
122 kmvc_getbit(bb, &ctx->g, res);
123 if (!res) {
124 kmvc_getbit(bb, &ctx->g, res);
125 if (!res) { // fill whole 2x2 block
126 val = bytestream2_get_byte(&ctx->g);
127 BLK(ctx->cur, l1x, l1y) = val;
128 BLK(ctx->cur, l1x + 1, l1y) = val;
129 BLK(ctx->cur, l1x, l1y + 1) = val;
130 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
131 } else { // copy block from already decoded place
132 val = bytestream2_get_byte(&ctx->g);
133 mx = val & 0xF;
134 my = val >> 4;
135 if ((l1x-mx) + 320*(l1y-my) < 0 || (l1x-mx) + 320*(l1y-my) > 320*199 - 2) {
136 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
137 return AVERROR_INVALIDDATA;
138 }
139 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
140 BLK(ctx->cur, l1x + 1, l1y) =
141 BLK(ctx->cur, l1x + 1 - mx, l1y - my);
142 BLK(ctx->cur, l1x, l1y + 1) =
143 BLK(ctx->cur, l1x - mx, l1y + 1 - my);
144 BLK(ctx->cur, l1x + 1, l1y + 1) =
145 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
146 }
147 } else { // read values for block
148 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
149 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
150 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
151 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
152 }
153 }
154 }
155 }
156 }
157 }
158
159 1 return 0;
160 }
161
162 76 static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
163 {
164 BitBuf bb;
165 int res, val;
166 int i, j;
167 int bx, by;
168 int l0x, l1x, l0y, l1y;
169 int mx, my;
170
171 76 kmvc_init_getbits(bb, &ctx->g);
172
173
2/2
✓ Branch 0 taken 1520 times.
✓ Branch 1 taken 76 times.
1596 for (by = 0; by < h; by += 8)
174
2/2
✓ Branch 0 taken 60800 times.
✓ Branch 1 taken 1520 times.
62320 for (bx = 0; bx < w; bx += 8) {
175
4/4
✓ Branch 0 taken 20059 times.
✓ Branch 1 taken 40741 times.
✓ Branch 2 taken 5780 times.
✓ Branch 3 taken 55020 times.
60800 kmvc_getbit(bb, &ctx->g, res);
176
2/2
✓ Branch 0 taken 40741 times.
✓ Branch 1 taken 20059 times.
60800 if (!res) {
177
4/4
✓ Branch 0 taken 1236 times.
✓ Branch 1 taken 39505 times.
✓ Branch 2 taken 6938 times.
✓ Branch 3 taken 33803 times.
40741 kmvc_getbit(bb, &ctx->g, res);
178
2/2
✓ Branch 0 taken 39505 times.
✓ Branch 1 taken 1236 times.
40741 if (!res) { // fill whole 8x8 block
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39505 times.
39505 if (!bytestream2_get_bytes_left(&ctx->g)) {
180 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
181 return AVERROR_INVALIDDATA;
182 }
183 39505 val = bytestream2_get_byte(&ctx->g);
184
2/2
✓ Branch 0 taken 2528320 times.
✓ Branch 1 taken 39505 times.
2567825 for (i = 0; i < 64; i++)
185 2528320 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
186 } else { // copy block from previous frame
187
2/2
✓ Branch 0 taken 79104 times.
✓ Branch 1 taken 1236 times.
80340 for (i = 0; i < 64; i++)
188 79104 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
189 79104 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
190 }
191 } else { // handle four 4x4 subblocks
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20059 times.
20059 if (!bytestream2_get_bytes_left(&ctx->g)) {
193 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
194 return AVERROR_INVALIDDATA;
195 }
196
2/2
✓ Branch 0 taken 80236 times.
✓ Branch 1 taken 20059 times.
100295 for (i = 0; i < 4; i++) {
197 80236 l0x = bx + (i & 1) * 4;
198 80236 l0y = by + (i & 2) * 2;
199
4/4
✓ Branch 0 taken 64393 times.
✓ Branch 1 taken 15843 times.
✓ Branch 2 taken 10097 times.
✓ Branch 3 taken 70139 times.
80236 kmvc_getbit(bb, &ctx->g, res);
200
2/2
✓ Branch 0 taken 15843 times.
✓ Branch 1 taken 64393 times.
80236 if (!res) {
201
4/4
✓ Branch 0 taken 9750 times.
✓ Branch 1 taken 6093 times.
✓ Branch 2 taken 1937 times.
✓ Branch 3 taken 13906 times.
15843 kmvc_getbit(bb, &ctx->g, res);
202
2/2
✓ Branch 0 taken 6093 times.
✓ Branch 1 taken 9750 times.
15843 if (!res) { // fill whole 4x4 block
203 6093 val = bytestream2_get_byte(&ctx->g);
204
2/2
✓ Branch 0 taken 97488 times.
✓ Branch 1 taken 6093 times.
103581 for (j = 0; j < 16; j++)
205 97488 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
206 } else { // copy block
207 9750 val = bytestream2_get_byte(&ctx->g);
208 9750 mx = (val & 0xF) - 8;
209 9750 my = (val >> 4) - 8;
210
2/4
✓ Branch 0 taken 9750 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9750 times.
9750 if ((l0x+mx) + 320*(l0y+my) < 0 || (l0x+mx) + 320*(l0y+my) > 320*197 - 4) {
211 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
212 return AVERROR_INVALIDDATA;
213 }
214
2/2
✓ Branch 0 taken 156000 times.
✓ Branch 1 taken 9750 times.
165750 for (j = 0; j < 16; j++)
215 156000 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
216 156000 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
217 }
218 } else { // descend to 2x2 sub-sub-blocks
219
2/2
✓ Branch 0 taken 257572 times.
✓ Branch 1 taken 64393 times.
321965 for (j = 0; j < 4; j++) {
220 257572 l1x = l0x + (j & 1) * 2;
221 257572 l1y = l0y + (j & 2);
222
4/4
✓ Branch 0 taken 129049 times.
✓ Branch 1 taken 128523 times.
✓ Branch 2 taken 32245 times.
✓ Branch 3 taken 225327 times.
257572 kmvc_getbit(bb, &ctx->g, res);
223
2/2
✓ Branch 0 taken 128523 times.
✓ Branch 1 taken 129049 times.
257572 if (!res) {
224
4/4
✓ Branch 0 taken 106955 times.
✓ Branch 1 taken 21568 times.
✓ Branch 2 taken 15937 times.
✓ Branch 3 taken 112586 times.
128523 kmvc_getbit(bb, &ctx->g, res);
225
2/2
✓ Branch 0 taken 21568 times.
✓ Branch 1 taken 106955 times.
128523 if (!res) { // fill whole 2x2 block
226 21568 val = bytestream2_get_byte(&ctx->g);
227 21568 BLK(ctx->cur, l1x, l1y) = val;
228 21568 BLK(ctx->cur, l1x + 1, l1y) = val;
229 21568 BLK(ctx->cur, l1x, l1y + 1) = val;
230 21568 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
231 } else { // copy block
232 106955 val = bytestream2_get_byte(&ctx->g);
233 106955 mx = (val & 0xF) - 8;
234 106955 my = (val >> 4) - 8;
235
2/4
✓ Branch 0 taken 106955 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 106955 times.
106955 if ((l1x+mx) + 320*(l1y+my) < 0 || (l1x+mx) + 320*(l1y+my) > 320*199 - 2) {
236 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
237 return AVERROR_INVALIDDATA;
238 }
239 106955 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
240 106955 BLK(ctx->cur, l1x + 1, l1y) =
241 106955 BLK(ctx->prev, l1x + 1 + mx, l1y + my);
242 106955 BLK(ctx->cur, l1x, l1y + 1) =
243 106955 BLK(ctx->prev, l1x + mx, l1y + 1 + my);
244 106955 BLK(ctx->cur, l1x + 1, l1y + 1) =
245 106955 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
246 }
247 } else { // read values for block
248 129049 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
249 129049 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
250 129049 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
251 129049 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
252 }
253 }
254 }
255 }
256 }
257 }
258
259 76 return 0;
260 }
261
262 77 static int decode_frame(AVCodecContext * avctx, AVFrame *frame,
263 int *got_frame, AVPacket *avpkt)
264 {
265 77 KmvcContext *const ctx = avctx->priv_data;
266 uint8_t *out, *src;
267 int i, ret;
268 int header;
269 int blocksize;
270
271 77 bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
272
273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
274 return ret;
275
276 #if FF_API_PALETTE_HAS_CHANGED
277 FF_DISABLE_DEPRECATION_WARNINGS
278 77 frame->palette_has_changed =
279 #endif
280 77 ff_copy_palette(ctx->pal, avpkt, avctx);
281 #if FF_API_PALETTE_HAS_CHANGED
282 FF_ENABLE_DEPRECATION_WARNINGS
283 #endif
284
285 77 header = bytestream2_get_byte(&ctx->g);
286
287 /* blocksize 127 is really palette change event */
288
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if (bytestream2_peek_byte(&ctx->g) == 127) {
289 bytestream2_skip(&ctx->g, 3);
290 for (i = 0; i < 127; i++) {
291 ctx->pal[i + (header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
292 bytestream2_skip(&ctx->g, 1);
293 }
294 bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
295 }
296
297
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 76 times.
77 if (header & KMVC_KEYFRAME) {
298 1 frame->flags |= AV_FRAME_FLAG_KEY;
299 1 frame->pict_type = AV_PICTURE_TYPE_I;
300 } else {
301 76 frame->flags &= ~AV_FRAME_FLAG_KEY;
302 76 frame->pict_type = AV_PICTURE_TYPE_P;
303 }
304
305
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 76 times.
77 if (header & KMVC_PALETTE) {
306 #if FF_API_PALETTE_HAS_CHANGED
307 FF_DISABLE_DEPRECATION_WARNINGS
308 1 frame->palette_has_changed = 1;
309 FF_ENABLE_DEPRECATION_WARNINGS
310 #endif
311 // palette starts from index 1 and has 127 entries
312
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 1 times.
256 for (i = 1; i <= ctx->palsize; i++) {
313 255 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
314 }
315 }
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (ctx->setpal) {
318 ctx->setpal = 0;
319 #if FF_API_PALETTE_HAS_CHANGED
320 FF_DISABLE_DEPRECATION_WARNINGS
321 frame->palette_has_changed = 1;
322 FF_ENABLE_DEPRECATION_WARNINGS
323 #endif
324 }
325
326 /* make the palette available on the way out */
327 77 memcpy(frame->data[1], ctx->pal, 1024);
328
329 77 blocksize = bytestream2_get_byte(&ctx->g);
330
331
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
77 if (blocksize != 8 && blocksize != 127) {
332 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
333 return AVERROR_INVALIDDATA;
334 }
335 77 memset(ctx->cur, 0, 320 * 200);
336
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
77 switch (header & KMVC_METHOD) {
337 case 0:
338 case 1: // used in palette changed event
339 memcpy(ctx->cur, ctx->prev, 320 * 200);
340 break;
341 1 case 3:
342 1 kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
343 1 break;
344 76 case 4:
345 76 kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
346 76 break;
347 default:
348 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
349 return AVERROR_INVALIDDATA;
350 }
351
352 77 out = frame->data[0];
353 77 src = ctx->cur;
354
2/2
✓ Branch 0 taken 12320 times.
✓ Branch 1 taken 77 times.
12397 for (i = 0; i < avctx->height; i++) {
355 12320 memcpy(out, src, avctx->width);
356 12320 src += 320;
357 12320 out += frame->linesize[0];
358 }
359
360 /* flip buffers */
361 77 FFSWAP(uint8_t *, ctx->cur, ctx->prev);
362
363 77 *got_frame = 1;
364
365 /* always report that the buffer was completely consumed */
366 77 return avpkt->size;
367 }
368
369
370
371 /*
372 * Init kmvc decoder
373 */
374 2 static av_cold int decode_init(AVCodecContext * avctx)
375 {
376 2 KmvcContext *const c = avctx->priv_data;
377 int i;
378
379 2 c->avctx = avctx;
380
381
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (avctx->width > 320 || avctx->height > 200) {
382 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
383 return AVERROR(EINVAL);
384 }
385
386 2 c->cur = c->frm0;
387 2 c->prev = c->frm1;
388
389
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < 256; i++) {
390 512 c->pal[i] = 0xFFU << 24 | i * 0x10101;
391 }
392
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->extradata_size < 12) {
394 av_log(avctx, AV_LOG_WARNING,
395 "Extradata missing, decoding may not work properly...\n");
396 c->palsize = 127;
397 } else {
398 2 c->palsize = AV_RL16(avctx->extradata + 10);
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (c->palsize >= (unsigned)MAX_PALSIZE) {
400 c->palsize = 127;
401 av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
402 return AVERROR_INVALIDDATA;
403 }
404 }
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->extradata_size == 1036) { // palette in extradata
407 uint8_t *src = avctx->extradata + 12;
408 for (i = 0; i < 256; i++) {
409 c->pal[i] = AV_RL32(src);
410 src += 4;
411 }
412 c->setpal = 1;
413 }
414
415 2 avctx->pix_fmt = AV_PIX_FMT_PAL8;
416
417 2 return 0;
418 }
419
420 const FFCodec ff_kmvc_decoder = {
421 .p.name = "kmvc",
422 CODEC_LONG_NAME("Karl Morton's video codec"),
423 .p.type = AVMEDIA_TYPE_VIDEO,
424 .p.id = AV_CODEC_ID_KMVC,
425 .priv_data_size = sizeof(KmvcContext),
426 .init = decode_init,
427 FF_CODEC_DECODE_CB(decode_frame),
428 .p.capabilities = AV_CODEC_CAP_DR1,
429 };
430