FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/smacker.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 266 421 63.2%
Branches: 129 237 54.4%

Line Branch Exec Source
1 /*
2 * Smacker 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 * Smacker decoder
25 */
26
27 /*
28 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
29 */
30
31 #include <stddef.h>
32
33 #include "libavutil/channel_layout.h"
34
35 #include "avcodec.h"
36
37 #define SMKTREE_BITS 9
38 #define SMK_NODE 0x80000000
39
40 #define SMKTREE_DECODE_MAX_RECURSION FFMIN(32, 3 * SMKTREE_BITS)
41 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
42
43 /* The maximum possible unchecked overread happens in decode_header_trees:
44 * Decoding the MMAP tree can overread by 6 * SMKTREE_BITS + 1, followed by
45 * three get_bits1, followed by at most 2 + 3 * 16 read bits when reading
46 * the TYPE tree before the next check. 64 is because of 64 bit reads. */
47 #if (6 * SMKTREE_BITS + 1 + 3 + (2 + 3 * 16) + 64) <= 8 * AV_INPUT_BUFFER_PADDING_SIZE
48 #define UNCHECKED_BITSTREAM_READER 1
49 #endif
50 #define BITSTREAM_READER_LE
51 #include "bytestream.h"
52 #include "codec_internal.h"
53 #include "decode.h"
54 #include "get_bits.h"
55
56 typedef struct SmackVContext {
57 AVCodecContext *avctx;
58 AVFrame *pic;
59
60 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
61 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
62 } SmackVContext;
63
64 typedef struct HuffEntry {
65 uint8_t value;
66 uint8_t length;
67 } HuffEntry;
68
69 /**
70 * Context used for code reconstructing
71 */
72 typedef struct HuffContext {
73 int current;
74 HuffEntry entries[256];
75 } HuffContext;
76
77 /* common parameters used for decode_bigtree */
78 typedef struct DBCtx {
79 int current, length;
80 int *values;
81 VLC *v1, *v2;
82 uint8_t vals[2];
83 int escapes[3];
84 int *last;
85 } DBCtx;
86
87 /* possible runs of blocks */
88 static const int block_runs[64] = {
89 1, 2, 3, 4, 5, 6, 7, 8,
90 9, 10, 11, 12, 13, 14, 15, 16,
91 17, 18, 19, 20, 21, 22, 23, 24,
92 25, 26, 27, 28, 29, 30, 31, 32,
93 33, 34, 35, 36, 37, 38, 39, 40,
94 41, 42, 43, 44, 45, 46, 47, 48,
95 49, 50, 51, 52, 53, 54, 55, 56,
96 57, 58, 59, 128, 256, 512, 1024, 2048 };
97
98 enum SmkBlockTypes {
99 SMK_BLK_MONO = 0,
100 SMK_BLK_FULL = 1,
101 SMK_BLK_SKIP = 2,
102 SMK_BLK_FILL = 3 };
103
104 /**
105 * Decode local frame tree
106 *
107 * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
108 * does not overread gb on success.
109 */
110 9904 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, int length)
111 {
112
2/4
✓ Branch 0 taken 9904 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9904 times.
9904 if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
113 av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
114 return AVERROR_INVALIDDATA;
115 }
116
117
2/2
✓ Branch 1 taken 5007 times.
✓ Branch 2 taken 4897 times.
9904 if(!get_bits1(gb)){ //Leaf
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5007 times.
5007 if (hc->current >= 256) {
119 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
120 return AVERROR_INVALIDDATA;
121 }
122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5007 times.
5007 if (get_bits_left(gb) < 8)
123 return AVERROR_INVALIDDATA;
124 5007 hc->entries[hc->current++] = (HuffEntry){ get_bits(gb, 8), length };
125 5007 return 0;
126 } else { //Node
127 int r;
128 4897 length++;
129 4897 r = smacker_decode_tree(gb, hc, length);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4897 times.
4897 if(r)
131 return r;
132 4897 return smacker_decode_tree(gb, hc, length);
133 }
134 }
135
136 /**
137 * Decode header tree
138 *
139 * Checks before the first read, can overread by 6 * SMKTREE_BITS on success.
140 */
141 81192 static int smacker_decode_bigtree(GetBitContext *gb, DBCtx *ctx, int length)
142 {
143 // Larger length can cause segmentation faults due to too deep recursion.
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81192 times.
81192 if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
145 av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
146 return AVERROR_INVALIDDATA;
147 }
148
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81192 times.
81192 if (ctx->current >= ctx->length) {
150 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
151 return AVERROR_INVALIDDATA;
152 }
153
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81192 times.
81192 if (get_bits_left(gb) <= 0)
154 return AVERROR_INVALIDDATA;
155
2/2
✓ Branch 1 taken 40602 times.
✓ Branch 2 taken 40590 times.
81192 if(!get_bits1(gb)){ //Leaf
156 int val, i1, i2;
157 40602 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3)
158
1/2
✓ Branch 0 taken 40602 times.
✗ Branch 1 not taken.
40602 : ctx->vals[0];
159 40602 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3)
160
1/2
✓ Branch 0 taken 40602 times.
✗ Branch 1 not taken.
40602 : ctx->vals[1];
161 40602 val = i1 | (i2 << 8);
162
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 40593 times.
40602 if(val == ctx->escapes[0]) {
163 9 ctx->last[0] = ctx->current;
164 9 val = 0;
165
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 40581 times.
40593 } else if(val == ctx->escapes[1]) {
166 12 ctx->last[1] = ctx->current;
167 12 val = 0;
168
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 40569 times.
40581 } else if(val == ctx->escapes[2]) {
169 12 ctx->last[2] = ctx->current;
170 12 val = 0;
171 }
172
173 40602 ctx->values[ctx->current++] = val;
174 40602 return 1;
175 } else { //Node
176 40590 int r = 0, r_new, t;
177
178 40590 t = ctx->current++;
179 40590 r = smacker_decode_bigtree(gb, ctx, length + 1);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40590 times.
40590 if(r < 0)
181 return r;
182 40590 ctx->values[t] = SMK_NODE | r;
183 40590 r++;
184 40590 r_new = smacker_decode_bigtree(gb, ctx, length + 1);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40590 times.
40590 if (r_new < 0)
186 return r_new;
187 40590 return r + r_new;
188 }
189 }
190
191 /**
192 * Store large tree as FFmpeg's vlc codes
193 *
194 * Can read FFMAX(1 + SMKTREE_DECODE_MAX_RECURSION, 2 + 3 * 16) bits
195 * before the first check; can overread by 6 * SMKTREE_BITS + 1 on success.
196 */
197 12 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
198 {
199 12 VLC vlc[2] = { { 0 } };
200 int escapes[3];
201 DBCtx ctx;
202 int err;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
205 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
206 return AVERROR_INVALIDDATA;
207 }
208
209
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 for (int i = 0; i < 2; i++) {
210 HuffContext h;
211 24 h.current = 0;
212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (!get_bits1(gb)) {
213 ctx.vals[i] = 0;
214 av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
215 i ? "high" : "low");
216 continue;
217 }
218 24 err = smacker_decode_tree(gb, &h, 0);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (err < 0)
220 goto error;
221 24 skip_bits1(gb);
222
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (h.current > 1) {
223 24 err = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
224 &h.entries[0].length, sizeof(*h.entries),
225 &h.entries[0].value, sizeof(*h.entries), 1,
226 24 0, INIT_VLC_OUTPUT_LE, smk->avctx);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (err < 0) {
228 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
229 goto error;
230 }
231 } else
232 ctx.vals[i] = h.entries[0].value;
233 }
234
235 12 escapes[0] = get_bits(gb, 16);
236 12 escapes[1] = get_bits(gb, 16);
237 12 escapes[2] = get_bits(gb, 16);
238
239 12 last[0] = last[1] = last[2] = -1;
240
241 12 ctx.escapes[0] = escapes[0];
242 12 ctx.escapes[1] = escapes[1];
243 12 ctx.escapes[2] = escapes[2];
244 12 ctx.v1 = &vlc[0];
245 12 ctx.v2 = &vlc[1];
246 12 ctx.last = last;
247 12 ctx.length = (size + 3) >> 2;
248 12 ctx.current = 0;
249 12 ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!ctx.values) {
251 err = AVERROR(ENOMEM);
252 goto error;
253 }
254 12 *recodes = ctx.values;
255
256 12 err = smacker_decode_bigtree(gb, &ctx, 0);
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (err < 0)
258 goto error;
259 12 skip_bits1(gb);
260
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
263
264 12 err = 0;
265 12 error:
266
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 for (int i = 0; i < 2; i++) {
267 24 ff_free_vlc(&vlc[i]);
268 }
269
270 12 return err;
271 }
272
273 3 static int decode_header_trees(SmackVContext *smk) {
274 GetBitContext gb;
275 int mmap_size, mclr_size, full_size, type_size, ret;
276 3 int skip = 0;
277
278 3 mmap_size = AV_RL32(smk->avctx->extradata);
279 3 mclr_size = AV_RL32(smk->avctx->extradata + 4);
280 3 full_size = AV_RL32(smk->avctx->extradata + 8);
281 3 type_size = AV_RL32(smk->avctx->extradata + 12);
282
283 3 ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
285 return ret;
286
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if(!get_bits1(&gb)) {
288 skip ++;
289 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
290 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
291 if (!smk->mmap_tbl)
292 return AVERROR(ENOMEM);
293 smk->mmap_tbl[0] = 0;
294 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
295 } else {
296 3 ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
298 return ret;
299 }
300
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if(!get_bits1(&gb)) {
301 skip ++;
302 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
303 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
304 if (!smk->mclr_tbl)
305 return AVERROR(ENOMEM);
306 smk->mclr_tbl[0] = 0;
307 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
308 } else {
309 3 ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
311 return ret;
312 }
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if(!get_bits1(&gb)) {
314 skip ++;
315 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
316 smk->full_tbl = av_malloc(sizeof(int) * 2);
317 if (!smk->full_tbl)
318 return AVERROR(ENOMEM);
319 smk->full_tbl[0] = 0;
320 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
321 } else {
322 3 ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
324 return ret;
325 }
326
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if(!get_bits1(&gb)) {
327 skip ++;
328 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
329 smk->type_tbl = av_malloc(sizeof(int) * 2);
330 if (!smk->type_tbl)
331 return AVERROR(ENOMEM);
332 smk->type_tbl[0] = 0;
333 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
334 } else {
335 3 ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
337 return ret;
338 }
339
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (skip == 4 || get_bits_left(&gb) < 0)
340 return AVERROR_INVALIDDATA;
341
342 3 return 0;
343 }
344
345 400 static av_always_inline void last_reset(int *recode, int *last) {
346 400 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
347 400 }
348
349 /* Get code and update history.
350 * Checks before reading, does not overread. */
351 721937 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
352 721937 register int *table = recode;
353 int v;
354
355
2/2
✓ Branch 0 taken 5171932 times.
✓ Branch 1 taken 721937 times.
5893869 while(*table & SMK_NODE) {
356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5171932 times.
5171932 if (get_bits_left(gb) < 1)
357 return AVERROR_INVALIDDATA;
358
2/2
✓ Branch 1 taken 2453865 times.
✓ Branch 2 taken 2718067 times.
5171932 if(get_bits1(gb))
359 2453865 table += (*table) & (~SMK_NODE);
360 5171932 table++;
361 }
362 721937 v = *table;
363
364
2/2
✓ Branch 0 taken 603120 times.
✓ Branch 1 taken 118817 times.
721937 if(v != recode[last[0]]) {
365 603120 recode[last[2]] = recode[last[1]];
366 603120 recode[last[1]] = recode[last[0]];
367 603120 recode[last[0]] = v;
368 }
369 721937 return v;
370 }
371
372 100 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
373 int *got_frame, AVPacket *avpkt)
374 {
375 100 SmackVContext * const smk = avctx->priv_data;
376 uint8_t *out;
377 uint32_t *pal;
378 GetByteContext gb2;
379 GetBitContext gb;
380 int blocks, blk, bw, bh;
381 int i, ret;
382 int stride;
383 int flags;
384
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
100 if (avpkt->size <= 769)
386 return AVERROR_INVALIDDATA;
387
388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
100 if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
389 return ret;
390
391 /* make the palette available on the way out */
392 100 pal = (uint32_t*)smk->pic->data[1];
393 100 bytestream2_init(&gb2, avpkt->data, avpkt->size);
394 100 flags = bytestream2_get_byteu(&gb2);
395 100 smk->pic->palette_has_changed = flags & 1;
396 100 smk->pic->key_frame = !!(flags & 2);
397
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 99 times.
100 if (smk->pic->key_frame)
398 1 smk->pic->pict_type = AV_PICTURE_TYPE_I;
399 else
400 99 smk->pic->pict_type = AV_PICTURE_TYPE_P;
401
402
2/2
✓ Branch 0 taken 25600 times.
✓ Branch 1 taken 100 times.
25700 for(i = 0; i < 256; i++)
403 25600 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
404
405 100 last_reset(smk->mmap_tbl, smk->mmap_last);
406 100 last_reset(smk->mclr_tbl, smk->mclr_last);
407 100 last_reset(smk->full_tbl, smk->full_last);
408 100 last_reset(smk->type_tbl, smk->type_last);
409
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
100 if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
410 return ret;
411
412 100 blk = 0;
413 100 bw = avctx->width >> 2;
414 100 bh = avctx->height >> 2;
415 100 blocks = bw * bh;
416 100 stride = smk->pic->linesize[0];
417
2/2
✓ Branch 0 taken 44593 times.
✓ Branch 1 taken 100 times.
44693 while(blk < blocks) {
418 int type, run, mode;
419 uint16_t pix;
420
421 44593 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44593 times.
44593 if (type < 0)
423 return type;
424 44593 run = block_runs[(type >> 2) & 0x3F];
425
4/5
✓ Branch 0 taken 15331 times.
✓ Branch 1 taken 11617 times.
✓ Branch 2 taken 10001 times.
✓ Branch 3 taken 7644 times.
✗ Branch 4 not taken.
44593 switch(type & 3){
426 15331 case SMK_BLK_MONO:
427
3/4
✓ Branch 0 taken 28348 times.
✓ Branch 1 taken 15331 times.
✓ Branch 2 taken 28348 times.
✗ Branch 3 not taken.
43679 while(run-- && blk < blocks){
428 int clr, map;
429 int hi, lo;
430 28348 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
431 28348 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
432 28348 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
433 28348 hi = clr >> 8;
434 28348 lo = clr & 0xFF;
435
2/2
✓ Branch 0 taken 113392 times.
✓ Branch 1 taken 28348 times.
141740 for(i = 0; i < 4; i++) {
436
2/2
✓ Branch 0 taken 53765 times.
✓ Branch 1 taken 59627 times.
113392 if(map & 1) out[0] = hi; else out[0] = lo;
437
2/2
✓ Branch 0 taken 46096 times.
✓ Branch 1 taken 67296 times.
113392 if(map & 2) out[1] = hi; else out[1] = lo;
438
2/2
✓ Branch 0 taken 38914 times.
✓ Branch 1 taken 74478 times.
113392 if(map & 4) out[2] = hi; else out[2] = lo;
439
2/2
✓ Branch 0 taken 32496 times.
✓ Branch 1 taken 80896 times.
113392 if(map & 8) out[3] = hi; else out[3] = lo;
440 113392 map >>= 4;
441 113392 out += stride;
442 }
443 28348 blk++;
444 }
445 15331 break;
446 11617 case SMK_BLK_FULL:
447 11617 mode = 0;
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11617 times.
11617 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
449 if (get_bits_left(&gb) < 1)
450 return AVERROR_INVALIDDATA;
451 if(get_bits1(&gb)) mode = 1;
452 else if(get_bits1(&gb)) mode = 2;
453 }
454
3/4
✓ Branch 0 taken 77581 times.
✓ Branch 1 taken 11617 times.
✓ Branch 2 taken 77581 times.
✗ Branch 3 not taken.
89198 while(run-- && blk < blocks){
455
1/4
✓ Branch 0 taken 77581 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
77581 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
456 switch(mode){
457 77581 case 0:
458
2/2
✓ Branch 0 taken 310324 times.
✓ Branch 1 taken 77581 times.
387905 for(i = 0; i < 4; i++) {
459 310324 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
460 310324 AV_WL16(out+2,pix);
461 310324 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
462 310324 AV_WL16(out,pix);
463 310324 out += stride;
464 }
465 77581 break;
466 case 1:
467 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
468 out[0] = out[1] = pix & 0xFF;
469 out[2] = out[3] = pix >> 8;
470 out += stride;
471 out[0] = out[1] = pix & 0xFF;
472 out[2] = out[3] = pix >> 8;
473 out += stride;
474 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
475 out[0] = out[1] = pix & 0xFF;
476 out[2] = out[3] = pix >> 8;
477 out += stride;
478 out[0] = out[1] = pix & 0xFF;
479 out[2] = out[3] = pix >> 8;
480 break;
481 case 2:
482 for(i = 0; i < 2; i++) {
483 uint16_t pix1, pix2;
484 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
485 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
486 AV_WL16(out,pix1);
487 AV_WL16(out+2,pix2);
488 out += stride;
489 AV_WL16(out,pix1);
490 AV_WL16(out+2,pix2);
491 out += stride;
492 }
493 break;
494 }
495 77581 blk++;
496 }
497 11617 break;
498 10001 case SMK_BLK_SKIP:
499
3/4
✓ Branch 0 taken 278421 times.
✓ Branch 1 taken 10001 times.
✓ Branch 2 taken 278421 times.
✗ Branch 3 not taken.
288422 while(run-- && blk < blocks)
500 278421 blk++;
501 10001 break;
502 7644 case SMK_BLK_FILL:
503 7644 mode = type >> 8;
504
3/4
✓ Branch 0 taken 15650 times.
✓ Branch 1 taken 7644 times.
✓ Branch 2 taken 15650 times.
✗ Branch 3 not taken.
23294 while(run-- && blk < blocks){
505 uint32_t col;
506 15650 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
507 15650 col = mode * 0x01010101U;
508
2/2
✓ Branch 0 taken 62600 times.
✓ Branch 1 taken 15650 times.
78250 for(i = 0; i < 4; i++) {
509 62600 *((uint32_t*)out) = col;
510 62600 out += stride;
511 }
512 15650 blk++;
513 }
514 7644 break;
515 }
516
517 }
518
519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
100 if ((ret = av_frame_ref(rframe, smk->pic)) < 0)
520 return ret;
521
522 100 *got_frame = 1;
523
524 /* always report that the buffer was completely consumed */
525 100 return avpkt->size;
526 }
527
528
529 3 static av_cold int decode_end(AVCodecContext *avctx)
530 {
531 3 SmackVContext * const smk = avctx->priv_data;
532
533 3 av_freep(&smk->mmap_tbl);
534 3 av_freep(&smk->mclr_tbl);
535 3 av_freep(&smk->full_tbl);
536 3 av_freep(&smk->type_tbl);
537
538 3 av_frame_free(&smk->pic);
539
540 3 return 0;
541 }
542
543
544 3 static av_cold int decode_init(AVCodecContext *avctx)
545 {
546 3 SmackVContext * const c = avctx->priv_data;
547 int ret;
548
549 3 c->avctx = avctx;
550
551 3 avctx->pix_fmt = AV_PIX_FMT_PAL8;
552
553 3 c->pic = av_frame_alloc();
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!c->pic)
555 return AVERROR(ENOMEM);
556
557 /* decode huffman trees from extradata */
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->extradata_size <= 16){
559 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
560 return AVERROR(EINVAL);
561 }
562
563 3 ret = decode_header_trees(c);
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
565 return ret;
566 }
567
568 3 return 0;
569 }
570
571
572 3 static av_cold int smka_decode_init(AVCodecContext *avctx)
573 {
574 3 int channels = avctx->ch_layout.nb_channels;
575
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (channels < 1 || channels > 2) {
576 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
577 return AVERROR_INVALIDDATA;
578 }
579 3 av_channel_layout_uninit(&avctx->ch_layout);
580 3 av_channel_layout_default(&avctx->ch_layout, channels);
581 3 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
582
583 3 return 0;
584 }
585
586 /**
587 * Decode Smacker audio data
588 */
589 86 static int smka_decode_frame(AVCodecContext *avctx, AVFrame *frame,
590 int *got_frame_ptr, AVPacket *avpkt)
591 {
592 86 const uint8_t *buf = avpkt->data;
593 86 int buf_size = avpkt->size;
594 GetBitContext gb;
595 86 VLC vlc[4] = { { 0 } };
596 int16_t *samples;
597 uint8_t *samples8;
598 uint8_t values[4];
599 int i, res, ret;
600 int unp_size;
601 int bits, stereo;
602 unsigned pred[2], val, val2;
603
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (buf_size <= 4) {
605 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
606 return AVERROR_INVALIDDATA;
607 }
608
609 86 unp_size = AV_RL32(buf);
610
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (unp_size > (1U<<24)) {
612 av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
613 return AVERROR_INVALIDDATA;
614 }
615
616
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
617 return ret;
618
619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if(!get_bits1(&gb)){
620 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
621 *got_frame_ptr = 0;
622 return 1;
623 }
624 86 stereo = get_bits1(&gb);
625 86 bits = get_bits1(&gb);
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (stereo ^ (avctx->ch_layout.nb_channels != 1)) {
627 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
628 return AVERROR_INVALIDDATA;
629 }
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
631 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
632 return AVERROR_INVALIDDATA;
633 }
634
635 /* get output buffer */
636 86 frame->nb_samples = unp_size / (avctx->ch_layout.nb_channels * (bits + 1));
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (unp_size % (avctx->ch_layout.nb_channels * (bits + 1))) {
638 av_log(avctx, AV_LOG_ERROR,
639 "The buffer does not contain an integer number of samples\n");
640 return AVERROR_INVALIDDATA;
641 }
642
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
643 return ret;
644 86 samples = (int16_t *)frame->data[0];
645 86 samples8 = frame->data[0];
646
647 // Initialize
648
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 86 times.
172 for(i = 0; i < (1 << (bits + stereo)); i++) {
649 HuffContext h;
650 86 h.current = 0;
651 86 skip_bits1(&gb);
652
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if ((ret = smacker_decode_tree(&gb, &h, 0)) < 0)
653 goto error;
654 86 skip_bits1(&gb);
655
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 6 times.
86 if (h.current > 1) {
656 80 ret = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
657 &h.entries[0].length, sizeof(*h.entries),
658 &h.entries[0].value, sizeof(*h.entries), 1,
659 0, INIT_VLC_OUTPUT_LE, avctx);
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (ret < 0) {
661 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
662 goto error;
663 }
664 } else
665 6 values[i] = h.entries[0].value;
666 }
667 /* this codec relies on wraparound instead of clipping audio */
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if(bits) { //decode 16-bit data
669 for(i = stereo; i >= 0; i--)
670 pred[i] = av_bswap16(get_bits(&gb, 16));
671 for(i = 0; i <= stereo; i++)
672 *samples++ = pred[i];
673 unp_size /= 2;
674
675 if (vlc[0 ].table || vlc[ 1].table ||
676 vlc[2*stereo].table || vlc[2*stereo+1].table) {
677 for(; i < unp_size ; i++) {
678 unsigned idx = 2 * (i & stereo);
679 if (get_bits_left(&gb) < 0) {
680 ret = AVERROR_INVALIDDATA;
681 goto error;
682 }
683 if (vlc[idx].table)
684 res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
685 else
686 res = values[idx];
687 val = res;
688 if (vlc[++idx].table)
689 res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
690 else
691 res = values[idx];
692 val |= res << 8;
693 pred[idx / 2] += val;
694 *samples++ = pred[idx / 2];
695 }
696 } else if (stereo) {
697 val = 256*values[1] + values[0];
698 val2 = 256*values[3] + values[2];
699 for(; i < unp_size; i+=2) {
700 pred[0] += val;
701 pred[1] += val2;
702 *samples++ = pred[0];
703 *samples++ = pred[1];
704 }
705 } else {
706 val = 256*values[1] + values[0];
707 for(; i < unp_size; i++) {
708 pred[0] += val;
709 *samples++ = pred[0];
710 }
711 }
712 } else { //8-bit data
713
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 86 times.
172 for(i = stereo; i >= 0; i--)
714 86 pred[i] = get_bits(&gb, 8);
715
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 86 times.
172 for(i = 0; i <= stereo; i++)
716 86 *samples8++ = pred[i];
717
2/2
✓ Branch 0 taken 156466 times.
✓ Branch 1 taken 86 times.
156552 for(; i < unp_size; i++) {
718 156466 unsigned idx = i & stereo;
719
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 156466 times.
156466 if (get_bits_left(&gb) < 0) {
720 ret = AVERROR_INVALIDDATA;
721 goto error;
722 }
723
2/2
✓ Branch 0 taken 147216 times.
✓ Branch 1 taken 9250 times.
156466 if (vlc[idx].table)
724 147216 val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
725 else
726 9250 val = values[idx];
727 156466 pred[idx] += val;
728 156466 *samples8++ = pred[idx];
729 }
730 }
731
732 86 *got_frame_ptr = 1;
733 86 ret = buf_size;
734
735 86 error:
736
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 86 times.
430 for(i = 0; i < 4; i++) {
737 344 ff_free_vlc(&vlc[i]);
738 }
739
740 86 return ret;
741 }
742
743 const FFCodec ff_smacker_decoder = {
744 .p.name = "smackvid",
745 CODEC_LONG_NAME("Smacker video"),
746 .p.type = AVMEDIA_TYPE_VIDEO,
747 .p.id = AV_CODEC_ID_SMACKVIDEO,
748 .priv_data_size = sizeof(SmackVContext),
749 .init = decode_init,
750 .close = decode_end,
751 FF_CODEC_DECODE_CB(decode_frame),
752 .p.capabilities = AV_CODEC_CAP_DR1,
753 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
754 };
755
756 const FFCodec ff_smackaud_decoder = {
757 .p.name = "smackaud",
758 CODEC_LONG_NAME("Smacker audio"),
759 .p.type = AVMEDIA_TYPE_AUDIO,
760 .p.id = AV_CODEC_ID_SMACKAUDIO,
761 .init = smka_decode_init,
762 FF_CODEC_DECODE_CB(smka_decode_frame),
763 .p.capabilities = AV_CODEC_CAP_DR1,
764 };
765