FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/smacker.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 266 406 65.5%
Branches: 129 223 57.8%

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