FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mss12.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 360 405 88.9%
Branches: 218 289 75.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Konstantin Shishkov
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Common functions for Microsoft Screen 1 and 2
24 */
25
26 #include <inttypes.h>
27
28 #include "libavutil/intfloat.h"
29 #include "libavutil/intreadwrite.h"
30 #include "avcodec.h"
31 #include "mss12.h"
32
33 enum SplitMode {
34 SPLIT_VERT = 0,
35 SPLIT_HOR,
36 SPLIT_NONE
37 };
38
39 static const int sec_order_sizes[4] = { 1, 7, 6, 1 };
40
41 enum ContextDirection {
42 TOP_LEFT = 0,
43 TOP,
44 TOP_RIGHT,
45 LEFT
46 };
47
48 3009272 static int model_calc_threshold(Model *m)
49 {
50 int thr;
51
52 3009272 thr = 2 * m->weights[m->num_syms] - 1;
53 3009272 thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
54
55 3009272 return FFMIN(thr, 0x3FFF);
56 }
57
58 1419 static void model_reset(Model *m)
59 {
60 int i;
61
62
2/2
✓ Branch 0 taken 11900 times.
✓ Branch 1 taken 1419 times.
13319 for (i = 0; i <= m->num_syms; i++) {
63 11900 m->weights[i] = 1;
64 11900 m->cum_prob[i] = m->num_syms - i;
65 }
66 1419 m->weights[0] = 0;
67
2/2
✓ Branch 0 taken 10481 times.
✓ Branch 1 taken 1419 times.
11900 for (i = 0; i < m->num_syms; i++)
68 10481 m->idx2sym[i + 1] = i;
69 1419 }
70
71 2580 static av_cold void model_init(Model *m, int num_syms, int thr_weight)
72 {
73 2580 m->num_syms = num_syms;
74 2580 m->thr_weight = thr_weight;
75 2580 m->threshold = num_syms * thr_weight;
76 2580 }
77
78 3281165 static void model_rescale_weights(Model *m)
79 {
80 int i;
81 int cum_prob;
82
83
2/2
✓ Branch 0 taken 3009272 times.
✓ Branch 1 taken 271893 times.
3281165 if (m->thr_weight == THRESH_ADAPTIVE)
84 3009272 m->threshold = model_calc_threshold(m);
85
2/2
✓ Branch 0 taken 29957 times.
✓ Branch 1 taken 3281165 times.
3311122 while (m->cum_prob[0] > m->threshold) {
86 29957 cum_prob = 0;
87
2/2
✓ Branch 0 taken 103487 times.
✓ Branch 1 taken 29957 times.
133444 for (i = m->num_syms; i >= 0; i--) {
88 103487 m->cum_prob[i] = cum_prob;
89 103487 m->weights[i] = (m->weights[i] + 1) >> 1;
90 103487 cum_prob += m->weights[i];
91 }
92 }
93 3281165 }
94
95 3281165 void ff_mss12_model_update(Model *m, int val)
96 {
97 int i;
98
99
2/2
✓ Branch 0 taken 6609 times.
✓ Branch 1 taken 3274556 times.
3281165 if (m->weights[val] == m->weights[val - 1]) {
100
2/2
✓ Branch 0 taken 40546 times.
✓ Branch 1 taken 6609 times.
47155 for (i = val; m->weights[i - 1] == m->weights[val]; i--);
101
1/2
✓ Branch 0 taken 6609 times.
✗ Branch 1 not taken.
6609 if (i != val) {
102 int sym1, sym2;
103
104 6609 sym1 = m->idx2sym[val];
105 6609 sym2 = m->idx2sym[i];
106
107 6609 m->idx2sym[val] = sym2;
108 6609 m->idx2sym[i] = sym1;
109
110 6609 val = i;
111 }
112 }
113 3281165 m->weights[val]++;
114
2/2
✓ Branch 0 taken 3412656 times.
✓ Branch 1 taken 3281165 times.
6693821 for (i = val - 1; i >= 0; i--)
115 3412656 m->cum_prob[i]++;
116 3281165 model_rescale_weights(m);
117 3281165 }
118
119 22 static void pixctx_reset(PixContext *ctx)
120 {
121 int i, j;
122
123
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 9 times.
22 if (!ctx->special_initial_cache)
124
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 13 times.
157 for (i = 0; i < ctx->cache_size; i++)
125 144 ctx->cache[i] = i;
126 else {
127 9 ctx->cache[0] = 1;
128 9 ctx->cache[1] = 2;
129 9 ctx->cache[2] = 4;
130 }
131
132 22 model_reset(&ctx->cache_model);
133 22 model_reset(&ctx->full_model);
134
135
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 22 times.
352 for (i = 0; i < 15; i++)
136
2/2
✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 330 times.
1650 for (j = 0; j < 4; j++)
137 1320 model_reset(&ctx->sec_models[i][j]);
138 22 }
139
140 40 static av_cold void pixctx_init(PixContext *ctx, int cache_size,
141 int full_model_syms, int special_initial_cache)
142 {
143 int i, j, k, idx;
144
145 40 ctx->cache_size = cache_size + 4;
146 40 ctx->num_syms = cache_size;
147 40 ctx->special_initial_cache = special_initial_cache;
148
149 40 model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
150 40 model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
151
152
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 40 times.
200 for (i = 0, idx = 0; i < 4; i++)
153
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 160 times.
760 for (j = 0; j < sec_order_sizes[i]; j++, idx++)
154
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 600 times.
3000 for (k = 0; k < 4; k++)
155
2/2
✓ Branch 0 taken 2240 times.
✓ Branch 1 taken 160 times.
2400 model_init(&ctx->sec_models[idx][k], 2 + i,
156 i ? THRESH_LOW : THRESH_ADAPTIVE);
157 40 }
158
159 27962 static av_always_inline int decode_pixel(ArithCoder *acoder, PixContext *pctx,
160 uint8_t *ngb, int num_ngb, int any_ngb)
161 {
162 int i, val, pix;
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27962 times.
27962 if (acoder->overread > MAX_OVERREAD)
165 return AVERROR_INVALIDDATA;
166 27962 val = acoder->get_model_sym(acoder, &pctx->cache_model);
167
2/2
✓ Branch 0 taken 26419 times.
✓ Branch 1 taken 1543 times.
27962 if (val < pctx->num_syms) {
168
2/2
✓ Branch 0 taken 23783 times.
✓ Branch 1 taken 2636 times.
26419 if (any_ngb) {
169 int idx, j;
170
171 23783 idx = 0;
172
1/2
✓ Branch 0 taken 44518 times.
✗ Branch 1 not taken.
44518 for (i = 0; i < pctx->cache_size; i++) {
173
2/2
✓ Branch 0 taken 56284 times.
✓ Branch 1 taken 33967 times.
90251 for (j = 0; j < num_ngb; j++)
174
2/2
✓ Branch 0 taken 10551 times.
✓ Branch 1 taken 45733 times.
56284 if (pctx->cache[i] == ngb[j])
175 10551 break;
176
2/2
✓ Branch 0 taken 33967 times.
✓ Branch 1 taken 10551 times.
44518 if (j == num_ngb) {
177
2/2
✓ Branch 0 taken 23783 times.
✓ Branch 1 taken 10184 times.
33967 if (idx == val)
178 23783 break;
179 10184 idx++;
180 }
181 }
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23783 times.
23783 val = FFMIN(i, pctx->cache_size - 1);
183 }
184 26419 pix = pctx->cache[val];
185 } else {
186 1543 pix = acoder->get_model_sym(acoder, &pctx->full_model);
187
2/2
✓ Branch 0 taken 16874 times.
✓ Branch 1 taken 1445 times.
18319 for (i = 0; i < pctx->cache_size - 1; i++)
188
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 16776 times.
16874 if (pctx->cache[i] == pix)
189 98 break;
190 1543 val = i;
191 }
192
2/2
✓ Branch 0 taken 14489 times.
✓ Branch 1 taken 13473 times.
27962 if (val) {
193
2/2
✓ Branch 0 taken 39699 times.
✓ Branch 1 taken 14489 times.
54188 for (i = val; i > 0; i--)
194 39699 pctx->cache[i] = pctx->cache[i - 1];
195 14489 pctx->cache[0] = pix;
196 }
197
198 27962 return pix;
199 }
200
201 3241997 static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
202 uint8_t *src, ptrdiff_t stride, int x, int y,
203 int has_right)
204 {
205 uint8_t neighbours[4];
206 uint8_t ref_pix[4];
207 int nlen;
208 3241997 int layer = 0, sub;
209 int pix;
210 int i, j;
211
212
2/2
✓ Branch 0 taken 49376 times.
✓ Branch 1 taken 3192621 times.
3241997 if (!y) {
213 49376 memset(neighbours, src[-1], 4);
214 } else {
215 3192621 neighbours[TOP] = src[-stride];
216
2/2
✓ Branch 0 taken 27425 times.
✓ Branch 1 taken 3165196 times.
3192621 if (!x) {
217 27425 neighbours[TOP_LEFT] = neighbours[LEFT] = neighbours[TOP];
218 } else {
219 3165196 neighbours[TOP_LEFT] = src[-stride - 1];
220 3165196 neighbours[ LEFT] = src[-1];
221 }
222
2/2
✓ Branch 0 taken 3165249 times.
✓ Branch 1 taken 27372 times.
3192621 if (has_right)
223 3165249 neighbours[TOP_RIGHT] = src[-stride + 1];
224 else
225 27372 neighbours[TOP_RIGHT] = neighbours[TOP];
226 }
227
228 3241997 sub = 0;
229
4/4
✓ Branch 0 taken 3190523 times.
✓ Branch 1 taken 51474 times.
✓ Branch 2 taken 3072982 times.
✓ Branch 3 taken 117541 times.
3241997 if (x >= 2 && src[-2] == neighbours[LEFT])
230 3072982 sub = 1;
231
4/4
✓ Branch 0 taken 3156165 times.
✓ Branch 1 taken 85832 times.
✓ Branch 2 taken 3029273 times.
✓ Branch 3 taken 126892 times.
3241997 if (y >= 2 && src[-2 * stride] == neighbours[TOP])
232 3029273 sub |= 2;
233
234 3241997 nlen = 1;
235 3241997 ref_pix[0] = neighbours[0];
236
2/2
✓ Branch 0 taken 9725991 times.
✓ Branch 1 taken 3241997 times.
12967988 for (i = 1; i < 4; i++) {
237
2/2
✓ Branch 0 taken 9870008 times.
✓ Branch 1 taken 247439 times.
10117447 for (j = 0; j < nlen; j++)
238
2/2
✓ Branch 0 taken 9478552 times.
✓ Branch 1 taken 391456 times.
9870008 if (ref_pix[j] == neighbours[i])
239 9478552 break;
240
2/2
✓ Branch 0 taken 247439 times.
✓ Branch 1 taken 9478552 times.
9725991 if (j == nlen)
241 247439 ref_pix[nlen++] = neighbours[i];
242 }
243
244
4/5
✓ Branch 0 taken 3006583 times.
✓ Branch 1 taken 224458 times.
✓ Branch 2 taken 9887 times.
✓ Branch 3 taken 1069 times.
✗ Branch 4 not taken.
3241997 switch (nlen) {
245 3006583 case 1:
246 3006583 layer = 0;
247 3006583 break;
248 224458 case 2:
249
2/2
✓ Branch 0 taken 118825 times.
✓ Branch 1 taken 105633 times.
224458 if (neighbours[TOP] == neighbours[TOP_LEFT]) {
250
2/2
✓ Branch 0 taken 55657 times.
✓ Branch 1 taken 63168 times.
118825 if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
251 55657 layer = 1;
252
2/2
✓ Branch 0 taken 48995 times.
✓ Branch 1 taken 14173 times.
63168 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
253 48995 layer = 2;
254 else
255 14173 layer = 3;
256
2/2
✓ Branch 0 taken 43077 times.
✓ Branch 1 taken 62556 times.
105633 } else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT]) {
257
2/2
✓ Branch 0 taken 15328 times.
✓ Branch 1 taken 27749 times.
43077 if (neighbours[LEFT] == neighbours[TOP_LEFT])
258 15328 layer = 4;
259 else
260 27749 layer = 5;
261
2/2
✓ Branch 0 taken 40511 times.
✓ Branch 1 taken 22045 times.
62556 } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
262 40511 layer = 6;
263 } else {
264 22045 layer = 7;
265 }
266 224458 break;
267 9887 case 3:
268
2/2
✓ Branch 0 taken 694 times.
✓ Branch 1 taken 9193 times.
9887 if (neighbours[TOP] == neighbours[TOP_LEFT])
269 694 layer = 8;
270
2/2
✓ Branch 0 taken 3171 times.
✓ Branch 1 taken 6022 times.
9193 else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
271 3171 layer = 9;
272
2/2
✓ Branch 0 taken 1932 times.
✓ Branch 1 taken 4090 times.
6022 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
273 1932 layer = 10;
274
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 2810 times.
4090 else if (neighbours[TOP_RIGHT] == neighbours[TOP])
275 1280 layer = 11;
276
2/2
✓ Branch 0 taken 2500 times.
✓ Branch 1 taken 310 times.
2810 else if (neighbours[TOP] == neighbours[LEFT])
277 2500 layer = 12;
278 else
279 310 layer = 13;
280 9887 break;
281 1069 case 4:
282 1069 layer = 14;
283 1069 break;
284 }
285
286 3241997 pix = acoder->get_model_sym(acoder,
287 &pctx->sec_models[layer][sub]);
288
2/2
✓ Branch 0 taken 3216725 times.
✓ Branch 1 taken 25272 times.
3241997 if (pix < nlen)
289 3216725 return ref_pix[pix];
290 else
291 25272 return decode_pixel(acoder, pctx, ref_pix, nlen, 1);
292 }
293
294 603 static int decode_region(ArithCoder *acoder, uint8_t *dst, uint8_t *rgb_dst,
295 int x, int y, int width, int height, ptrdiff_t stride,
296 ptrdiff_t rgb_stride, PixContext *pctx,
297 const uint32_t *pal)
298 {
299 int i, j, p;
300
301
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 344 times.
603 rgb_stride = rgb_dst ? rgb_stride : 0;
302
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 344 times.
603 rgb_dst = rgb_dst ? rgb_dst + x * 3 + y * rgb_stride : NULL;
303 603 dst += x + y * stride;
304
305
2/2
✓ Branch 0 taken 27922 times.
✓ Branch 1 taken 603 times.
28525 for (j = 0; j < height; j++) {
306
2/2
✓ Branch 0 taken 3242171 times.
✓ Branch 1 taken 27922 times.
3270093 for (i = 0; i < width; i++) {
307
4/4
✓ Branch 0 taken 27922 times.
✓ Branch 1 taken 3214249 times.
✓ Branch 2 taken 603 times.
✓ Branch 3 taken 27319 times.
3242171 if (!i && !j)
308 603 p = decode_pixel(acoder, pctx, NULL, 0, 0);
309 else
310 3241568 p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
311 3241568 i, j, width - i - 1);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3242171 times.
3242171 if (p < 0)
313 return p;
314 3242171 dst[i] = p;
315
316
2/2
✓ Branch 0 taken 3054291 times.
✓ Branch 1 taken 187880 times.
3242171 if (rgb_dst)
317 3054291 AV_WB24(rgb_dst + i * 3, pal[p]);
318 }
319 27922 dst += stride;
320
2/2
✓ Branch 0 taken 21362 times.
✓ Branch 1 taken 6560 times.
27922 rgb_dst = FF_PTR_ADD(rgb_dst, rgb_stride);
321 }
322
323 603 return 0;
324 }
325
326 2806 static void copy_rectangles(MSS12Context const *c,
327 int x, int y, int width, int height)
328 {
329 int j;
330
331
2/2
✓ Branch 0 taken 1345 times.
✓ Branch 1 taken 1461 times.
2806 if (c->last_rgb_pic)
332
2/2
✓ Branch 0 taken 1345 times.
✓ Branch 1 taken 1345 times.
2690 for (j = y; j < y + height; j++) {
333 1345 memcpy(c->rgb_pic + j * c->rgb_stride + x * 3,
334 1345 c->last_rgb_pic + j * c->rgb_stride + x * 3,
335 1345 width * 3);
336 1345 memcpy(c->pal_pic + j * c->pal_stride + x,
337 1345 c->last_pal_pic + j * c->pal_stride + x,
338 width);
339 }
340 2806 }
341
342 132 static int motion_compensation(MSS12Context const *c,
343 int x, int y, int width, int height)
344 {
345
2/4
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
132 if (x + c->mvX < 0 || x + c->mvX + width > c->avctx->width ||
346
2/4
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
132 y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 !c->rgb_pic)
348 return -1;
349 else {
350 132 uint8_t *dst = c->pal_pic + x + y * c->pal_stride;
351 132 uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
352 uint8_t *src;
353 uint8_t *rgb_src;
354 int j;
355 132 x += c->mvX;
356 132 y += c->mvY;
357
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 57 times.
132 if (c->last_rgb_pic) {
358 75 src = c->last_pal_pic + x + y * c->pal_stride;
359 75 rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
360 } else {
361 57 src = c->pal_pic + x + y * c->pal_stride;
362 57 rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
363 }
364
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 132 times.
264 for (j = 0; j < height; j++) {
365 132 memmove(dst, src, width);
366 132 memmove(rgb_dst, rgb_src, width * 3);
367 132 dst += c->pal_stride;
368 132 src += c->pal_stride;
369 132 rgb_dst += c->rgb_stride;
370 132 rgb_src += c->rgb_stride;
371 }
372 }
373 132 return 0;
374 }
375
376 5 static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
377 uint8_t *dst, ptrdiff_t stride, uint8_t *mask,
378 ptrdiff_t mask_stride, int x, int y,
379 int width, int height,
380 PixContext *pctx)
381 {
382 int i, j, p;
383 5 uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
384
385 5 dst += x + y * stride;
386 5 mask += x + y * mask_stride;
387
388
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 5 times.
619 for (j = 0; j < height; j++) {
389
2/2
✓ Branch 0 taken 3334 times.
✓ Branch 1 taken 614 times.
3948 for (i = 0; i < width; i++) {
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3334 times.
3334 if (c->avctx->err_recognition & AV_EF_EXPLODE &&
391 ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
392 !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
393 return -1;
394
395
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 562 times.
3334 if (mask[i] == 0x02) {
396 2772 copy_rectangles(c, x + i, y + j, 1, 1);
397
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 430 times.
562 } else if (mask[i] == 0x04) {
398
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
132 if (motion_compensation(c, x + i, y + j, 1, 1))
399 return -1;
400
1/2
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
430 } else if (mask[i] != 0x80) {
401
4/4
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 323 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 106 times.
430 if (!i && !j)
402 1 p = decode_pixel(acoder, pctx, NULL, 0, 0);
403 else
404 429 p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
405 429 i, j, width - i - 1);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
430 if (p < 0)
407 return p;
408 430 dst[i] = p;
409
1/2
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
430 if (c->rgb_pic)
410 430 AV_WB24(rgb_dst + i * 3, c->pal[p]);
411 }
412 }
413 614 dst += stride;
414 614 mask += mask_stride;
415 614 rgb_dst += c->rgb_stride;
416 }
417
418 5 return 0;
419 }
420
421 20 static av_cold void slicecontext_init(SliceContext *sc,
422 int version, int full_model_syms)
423 {
424 20 model_init(&sc->intra_region, 2, THRESH_ADAPTIVE);
425 20 model_init(&sc->inter_region, 2, THRESH_ADAPTIVE);
426 20 model_init(&sc->split_mode, 3, THRESH_HIGH);
427 20 model_init(&sc->edge_mode, 2, THRESH_HIGH);
428 20 model_init(&sc->pivot, 3, THRESH_LOW);
429
430 20 pixctx_init(&sc->intra_pix_ctx, 8, full_model_syms, 0);
431
432
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 pixctx_init(&sc->inter_pix_ctx, version ? 3 : 2,
433 full_model_syms, version ? 1 : 0);
434 20 }
435
436 11 void ff_mss12_slicecontext_reset(SliceContext *sc)
437 {
438 11 model_reset(&sc->intra_region);
439 11 model_reset(&sc->inter_region);
440 11 model_reset(&sc->split_mode);
441 11 model_reset(&sc->edge_mode);
442 11 model_reset(&sc->pivot);
443 11 pixctx_reset(&sc->intra_pix_ctx);
444 11 pixctx_reset(&sc->inter_pix_ctx);
445 11 }
446
447 1713 static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
448 {
449 int val, inv;
450
451 1713 inv = acoder->get_model_sym(acoder, &sc->edge_mode);
452 1713 val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
453
454
2/2
✓ Branch 0 taken 1043 times.
✓ Branch 1 taken 670 times.
1713 if (val > 2) {
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1043 times.
1043 if ((base + 1) / 2 - 2 <= 0)
456 return -1;
457
458 1043 val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
459 }
460
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1713 times.
1713 if ((unsigned)val >= base)
462 return -1;
463
464
2/2
✓ Branch 0 taken 832 times.
✓ Branch 1 taken 881 times.
1713 return inv ? base - val : val;
465 }
466
467 1647 static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
468 int x, int y, int width, int height)
469 {
470 1647 MSS12Context const *c = sc->c;
471 int mode;
472
473 1647 mode = acoder->get_model_sym(acoder, &sc->intra_region);
474
475
2/2
✓ Branch 0 taken 1049 times.
✓ Branch 1 taken 598 times.
1647 if (!mode) {
476 int i, j, pix, rgb_pix;
477 1049 ptrdiff_t stride = c->pal_stride;
478 1049 ptrdiff_t rgb_stride = c->rgb_stride;
479 1049 uint8_t *dst = c->pal_pic + x + y * stride;
480
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 589 times.
1049 uint8_t *rgb_dst = c->rgb_pic ? c->rgb_pic + x * 3 + y * rgb_stride : NULL;
481
482 1049 pix = decode_pixel(acoder, &sc->intra_pix_ctx, NULL, 0, 0);
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1049 times.
1049 if (pix < 0)
484 return pix;
485 1049 rgb_pix = c->pal[pix];
486
2/2
✓ Branch 0 taken 52895 times.
✓ Branch 1 taken 1049 times.
53944 for (i = 0; i < height; i++, dst += stride) {
487 52895 memset(dst, pix, width);
488
2/2
✓ Branch 0 taken 41628 times.
✓ Branch 1 taken 11267 times.
52895 if (rgb_dst) {
489
2/2
✓ Branch 0 taken 3564623 times.
✓ Branch 1 taken 41628 times.
3606251 for (j = 0; j < width * 3; j += 3)
490 3564623 AV_WB24(rgb_dst + j, rgb_pix);
491 41628 rgb_dst += rgb_stride;
492 }
493 }
494 } else {
495 598 return decode_region(acoder, c->pal_pic, c->rgb_pic,
496 598 x, y, width, height, c->pal_stride, c->rgb_stride,
497 &sc->intra_pix_ctx, &c->pal[0]);
498 }
499
500 1049 return 0;
501 }
502
503 1042 static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
504 int x, int y, int width, int height)
505 {
506 1042 MSS12Context const *c = sc->c;
507 int mode;
508
509 1042 mode = acoder->get_model_sym(acoder, &sc->inter_region);
510
511
2/2
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 5 times.
1042 if (!mode) {
512 1037 mode = decode_pixel(acoder, &sc->inter_pix_ctx, NULL, 0, 0);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1037 times.
1037 if (mode < 0)
514 return mode;
515
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1037 times.
1037 if (c->avctx->err_recognition & AV_EF_EXPLODE &&
517 ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
518 !c->rgb_pic && mode != 0x80 && mode != 0xFF))
519 return -1;
520
521
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 1003 times.
1037 if (mode == 0x02)
522 34 copy_rectangles(c, x, y, width, height);
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1003 times.
1003 else if (mode == 0x04)
524 return motion_compensation(c, x, y, width, height);
525
2/2
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 149 times.
1003 else if (mode != 0x80)
526 854 return decode_region_intra(sc, acoder, x, y, width, height);
527 } else {
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (decode_region(acoder, c->mask, NULL,
529 5 x, y, width, height, c->mask_stride, 0,
530 &sc->inter_pix_ctx, &c->pal[0]) < 0)
531 return -1;
532 5 return decode_region_masked(c, acoder, c->pal_pic,
533 5 c->pal_stride, c->mask,
534 5 c->mask_stride,
535 x, y, width, height,
536 &sc->intra_pix_ctx);
537 }
538
539 183 return 0;
540 }
541
542 3548 int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
543 int x, int y, int width, int height)
544 {
545 int mode, pivot;
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3548 times.
3548 if (acoder->overread > MAX_OVERREAD)
547 return AVERROR_INVALIDDATA;
548
549 3548 mode = acoder->get_model_sym(acoder, &sc->split_mode);
550
551
3/4
✓ Branch 0 taken 847 times.
✓ Branch 1 taken 866 times.
✓ Branch 2 taken 1835 times.
✗ Branch 3 not taken.
3548 switch (mode) {
552 847 case SPLIT_VERT:
553
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
847 if ((pivot = decode_pivot(sc, acoder, height)) < 1)
554 return -1;
555
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
847 if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
556 return -1;
557
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
847 if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
558 return -1;
559 847 break;
560 866 case SPLIT_HOR:
561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 866 times.
866 if ((pivot = decode_pivot(sc, acoder, width)) < 1)
562 return -1;
563
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 866 times.
866 if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
564 return -1;
565
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 866 times.
866 if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
566 return -1;
567 866 break;
568 1835 case SPLIT_NONE:
569
2/2
✓ Branch 0 taken 793 times.
✓ Branch 1 taken 1042 times.
1835 if (sc->c->keyframe)
570 793 return decode_region_intra(sc, acoder, x, y, width, height);
571 else
572 1042 return decode_region_inter(sc, acoder, x, y, width, height);
573 default:
574 return -1;
575 }
576
577 1713 return 0;
578 }
579
580 14 av_cold int ff_mss12_decode_init(MSS12Context *c, int version,
581 SliceContext* sc1, SliceContext *sc2)
582 {
583 14 AVCodecContext *avctx = c->avctx;
584 int i;
585
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (avctx->extradata_size < 52 + 256 * 3) {
587 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
588 avctx->extradata_size);
589 return AVERROR_INVALIDDATA;
590 }
591
592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
593 av_log(avctx, AV_LOG_ERROR,
594 "Insufficient extradata size: expected %"PRIu32" got %d\n",
595 AV_RB32(avctx->extradata),
596 avctx->extradata_size);
597 return AVERROR_INVALIDDATA;
598 }
599
600 14 avctx->coded_width = FFMAX(AV_RB32(avctx->extradata + 20), avctx->width);
601 14 avctx->coded_height = FFMAX(AV_RB32(avctx->extradata + 24), avctx->height);
602
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
603 av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
604 avctx->coded_width, avctx->coded_height);
605 return AVERROR_INVALIDDATA;
606 }
607
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (avctx->coded_width < 1 || avctx->coded_height < 1) {
608 av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too small",
609 avctx->coded_width, avctx->coded_height);
610 return AVERROR_INVALIDDATA;
611 }
612
613 14 av_log(avctx, AV_LOG_DEBUG, "Encoder version %"PRIu32".%"PRIu32"\n",
614 14 AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (version != AV_RB32(avctx->extradata + 4) > 1) {
616 av_log(avctx, AV_LOG_ERROR,
617 "Header version doesn't match codec tag\n");
618 return -1;
619 }
620
621 14 c->free_colours = AV_RB32(avctx->extradata + 48);
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if ((unsigned)c->free_colours > 256) {
623 av_log(avctx, AV_LOG_ERROR,
624 "Incorrect number of changeable palette entries: %d\n",
625 c->free_colours);
626 return AVERROR_INVALIDDATA;
627 }
628 14 av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
629
630 14 av_log(avctx, AV_LOG_DEBUG, "Display dimensions %"PRIu32"x%"PRIu32"\n",
631 14 AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
632 14 av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
633 avctx->coded_width, avctx->coded_height);
634 14 av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
635 14 av_int2float(AV_RB32(avctx->extradata + 28)));
636 14 av_log(avctx, AV_LOG_DEBUG, "Bitrate %"PRIu32" bps\n",
637 14 AV_RB32(avctx->extradata + 32));
638 14 av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
639 14 av_int2float(AV_RB32(avctx->extradata + 36)));
640 14 av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
641 14 av_int2float(AV_RB32(avctx->extradata + 40)));
642 14 av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
643 14 av_int2float(AV_RB32(avctx->extradata + 44)));
644
645
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 if (version) {
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (avctx->extradata_size < 60 + 256 * 3) {
647 av_log(avctx, AV_LOG_ERROR,
648 "Insufficient extradata size %d for v2\n",
649 avctx->extradata_size);
650 return AVERROR_INVALIDDATA;
651 }
652
653 12 c->slice_split = AV_RB32(avctx->extradata + 52);
654 12 av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
655
656 12 c->full_model_syms = AV_RB32(avctx->extradata + 56);
657
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 if (c->full_model_syms < 2 || c->full_model_syms > 256) {
658 av_log(avctx, AV_LOG_ERROR,
659 "Incorrect number of used colours %d\n",
660 c->full_model_syms);
661 return AVERROR_INVALIDDATA;
662 }
663 12 av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
664 c->full_model_syms);
665 } else {
666 2 c->slice_split = 0;
667 2 c->full_model_syms = 256;
668 }
669
670
2/2
✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 14 times.
3598 for (i = 0; i < 256; i++)
671
6/6
✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 512 times.
✓ Branch 2 taken 3072 times.
✓ Branch 3 taken 512 times.
✓ Branch 4 taken 3072 times.
✓ Branch 5 taken 512 times.
3584 c->pal[i] = 0xFFU << 24 | AV_RB24(avctx->extradata + 52 +
672 (version ? 8 : 0) + i * 3);
673
674 14 c->mask_stride = FFALIGN(avctx->width, 16);
675 14 c->mask = av_malloc_array(c->mask_stride, avctx->height);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!c->mask) {
677 av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
678 return AVERROR(ENOMEM);
679 }
680
681 14 sc1->c = c;
682 14 slicecontext_init(sc1, version, c->full_model_syms);
683
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 if (c->slice_split) {
684 6 sc2->c = c;
685 6 slicecontext_init(sc2, version, c->full_model_syms);
686 }
687 14 c->corrupted = 1;
688
689 14 return 0;
690 }
691
692 14 av_cold int ff_mss12_decode_end(MSS12Context *c)
693 {
694 14 av_freep(&c->mask);
695
696 14 return 0;
697 }
698