FFmpeg coverage


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