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
32
37 };
38
40
46 };
47
49 {
50 int thr;
51
53 thr = ((thr >> 1) + 4 * m->
cum_prob[0]) / thr;
54
55 return FFMIN(thr, 0x3FFF);
56 }
57
59 {
60 int i;
61
65 }
69 }
70
72 {
76 }
77
79 {
80 int i;
81 int cum_prob;
82
86 cum_prob = 0;
91 }
92 }
93 }
94
96 {
97 int i;
98
101 if (i != val) {
102 int sym1, sym2;
103
106
109
110 val = i;
111 }
112 }
114 for (i = val - 1; i >= 0; i--)
117 }
118
120 {
121 int i, j;
122
126 else {
130 }
131
134
135 for (i = 0; i < 15; i++)
136 for (j = 0; j < 4; j++)
138 }
139
141 int full_model_syms, int special_initial_cache)
142 {
143 int i, j, k, idx;
144
148
151
152 for (i = 0, idx = 0; i < 4; i++)
154 for (k = 0; k < 4; k++)
157 }
158
160 uint8_t *ngb,
int num_ngb,
int any_ngb)
161 {
163
165 if (val < pctx->num_syms) {
166 if (any_ngb) {
167 int idx, j;
168
169 idx = 0;
171 for (j = 0; j < num_ngb; j++)
172 if (pctx->
cache[i] == ngb[j])
173 break;
174 if (j == num_ngb) {
175 if (idx == val)
176 break;
177 idx++;
178 }
179 }
181 }
183 } else {
186 if (pctx->
cache[i] == pix)
187 break;
188 val = i;
189 }
190 if (val) {
191 for (i = val; i > 0; i--)
193 pctx->
cache[0] = pix;
194 }
195
196 return pix;
197 }
198
201 int has_right)
202 {
205 int nlen;
206 int layer = 0, sub;
207 int pix;
208 int i, j;
209
210 if (!y) {
211 memset(neighbours, src[-1], 4);
212 } else {
213 neighbours[
TOP] = src[-stride];
214 if (!x) {
216 } else {
217 neighbours[
TOP_LEFT] = src[-stride - 1];
218 neighbours[
LEFT] = src[-1];
219 }
220 if (has_right)
221 neighbours[
TOP_RIGHT] = src[-stride + 1];
222 else
224 }
225
226 sub = 0;
227 if (x >= 2 && src[-2] == neighbours[
LEFT])
228 sub = 1;
229 if (y >= 2 && src[-2 * stride] == neighbours[
TOP])
230 sub |= 2;
231
232 nlen = 1;
233 ref_pix[0] = neighbours[0];
234 for (i = 1; i < 4; i++) {
235 for (j = 0; j < nlen; j++)
236 if (ref_pix[j] == neighbours[i])
237 break;
238 if (j == nlen)
239 ref_pix[nlen++] = neighbours[i];
240 }
241
242 switch (nlen) {
243 case 1:
244 layer = 0;
245 break;
246 case 2:
247 if (neighbours[TOP] == neighbours[
TOP_LEFT]) {
248 if (neighbours[
TOP_RIGHT] == neighbours[TOP_LEFT])
249 layer = 1;
250 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
251 layer = 2;
252 else
253 layer = 3;
254 }
else if (neighbours[
TOP_RIGHT] == neighbours[TOP_LEFT]) {
255 if (neighbours[LEFT] == neighbours[TOP_LEFT])
256 layer = 4;
257 else
258 layer = 5;
259 } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
260 layer = 6;
261 } else {
262 layer = 7;
263 }
264 break;
265 case 3:
266 if (neighbours[TOP] == neighbours[TOP_LEFT])
267 layer = 8;
268 else if (neighbours[
TOP_RIGHT] == neighbours[TOP_LEFT])
269 layer = 9;
270 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
271 layer = 10;
272 else if (neighbours[
TOP_RIGHT] == neighbours[TOP])
273 layer = 11;
274 else if (neighbours[TOP] == neighbours[LEFT])
275 layer = 12;
276 else
277 layer = 13;
278 break;
279 case 4:
280 layer = 14;
281 break;
282 }
283
286 if (pix < nlen)
287 return ref_pix[pix];
288 else
290 }
291
294 int rgb_stride,
PixContext *pctx,
const uint32_t *pal)
295 {
296 int i, j, p;
297 uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
298
299 dst += x + y * stride;
300
301 for (j = 0; j <
height; j++) {
302 for (i = 0; i <
width; i++) {
303 if (!i && !j)
305 else
307 i, j, width - i - 1);
308 dst[i] = p;
309
310 if (rgb_pic)
311 AV_WB24(rgb_dst + i * 3, pal[p]);
312 }
313 dst += stride;
314 rgb_dst += rgb_stride;
315 }
316
317 return 0;
318 }
319
322 {
323 int j;
324
326 for (j = y; j < y +
height; j++) {
329 width * 3);
332 width);
333 }
334 }
335
338 {
342 return -1;
343 else {
348 int j;
354 } else {
357 }
358 for (j = 0; j <
height; j++) {
359 memmove(dst, src, width);
360 memmove(rgb_dst, rgb_src, width * 3);
365 }
366 }
367 return 0;
368 }
369
372 int mask_stride,
int x,
int y,
375 {
376 int i, j, p;
378
379 dst += x + y * stride;
380 mask += x + y * mask_stride;
381
382 for (j = 0; j <
height; j++) {
383 for (i = 0; i <
width; i++) {
385 ( c->
rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
386 !c->
rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
387 return -1;
388
389 if (mask[i] == 0x02) {
391 } else if (mask[i] == 0x04) {
393 return -1;
394 } else if (mask[i] != 0x80) {
395 if (!i && !j)
397 else
399 i, j, width - i - 1);
400 dst[i] = p;
403 }
404 }
405 dst += stride;
406 mask += mask_stride;
408 }
409
410 return 0;
411 }
412
414 int version,
int full_model_syms)
415 {
421
423
425 full_model_syms, version ? 1 : 0);
426 }
427
429 {
437 }
438
440 {
442
445
446 if (val > 2) {
447 if ((base + 1) / 2 - 2 <= 0)
448 return -1;
449
450 val = acoder->
get_number(acoder, (base + 1) / 2 - 2) + 3;
451 }
452
453 if ((unsigned)val >= base)
454 return -1;
455
456 return inv ? base - val :
val;
457 }
458
461 {
464
466
467 if (!mode) {
468 int i, j, pix, rgb_pix;
473
475 rgb_pix = c->
pal[pix];
476 for (i = 0; i <
height; i++, dst += stride, rgb_dst += rgb_stride) {
477 memset(dst, pix, width);
479 for (j = 0; j < width * 3; j += 3)
481 }
482 } else {
486 }
487
488 return 0;
489 }
490
493 {
496
498
499 if (!mode) {
501
503 ( c->
rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
504 !c->
rgb_pic && mode != 0x80 && mode != 0xFF))
505 return -1;
506
507 if (mode == 0x02)
509 else if (mode == 0x04)
511 else if (mode != 0x80)
513 } else {
517 return -1;
521 x, y, width, height,
523 }
524
525 return 0;
526 }
527
530 {
532
534
535 switch (mode) {
538 return -1;
540 return -1;
542 return -1;
543 break;
546 return -1;
548 return -1;
550 return -1;
551 break;
555 else
557 default:
558 return -1;
559 }
560
561 return 0;
562 }
563
566 {
568 int i;
569
574 }
575
578 "Insufficient extradata size: expected %"PRIu32" got %d\n",
582 }
583
590 }
595 }
596
601 "Header version doesn't match codec tag\n");
602 return -1;
603 }
604
608 "Incorrect number of changeable palette entries: %d\n",
611 }
613
628
629 if (version) {
632 "Insufficient extradata size %d for v2\n",
635 }
636
639
643 "Incorrect number of used colours %d\n",
646 }
649 } else {
652 }
653
654 for (i = 0; i < 256; i++)
656 (version ? 8 : 0) + i * 3);
657
663 }
664
670 }
672
673 return 0;
674 }
675
677 {
679
680 return 0;
681 }