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
30
35 };
36
38
44 };
45
47 {
48 int thr;
49
51 thr = ((thr >> 1) + 4 * m->
cum_prob[0]) / thr;
52
53 return FFMIN(thr, 0x3FFF);
54 }
55
57 {
58 int i;
59
63 }
67 }
68
70 {
74 }
75
77 {
78 int i;
79 int cum_prob;
80
84 cum_prob = 0;
89 }
90 }
91 }
92
94 {
95 int i;
96
99 if (i != val) {
100 int sym1, sym2;
101
104
107
108 val = i;
109 }
110 }
112 for (i = val - 1; i >= 0; i--)
115 }
116
118 {
119 int i, j;
120
124 else {
128 }
129
132
133 for (i = 0; i < 15; i++)
134 for (j = 0; j < 4; j++)
136 }
137
139 int full_model_syms, int special_initial_cache)
140 {
141 int i, j, k, idx;
142
146
149
150 for (i = 0, idx = 0; i < 4; i++)
152 for (k = 0; k < 4; k++)
155 }
156
158 uint8_t *ngb,
int num_ngb,
int any_ngb)
159 {
161
163 if (val < pctx->num_syms) {
164 if (any_ngb) {
165 int idx, j;
166
167 idx = 0;
169 for (j = 0; j < num_ngb; j++)
170 if (pctx->
cache[i] == ngb[j])
171 break;
172 if (j == num_ngb) {
173 if (idx == val)
174 break;
175 idx++;
176 }
177 }
179 }
181 } else {
184 if (pctx->
cache[i] == pix)
185 break;
186 val = i;
187 }
188 if (val) {
189 for (i = val; i > 0; i--)
191 pctx->
cache[0] = pix;
192 }
193
194 return pix;
195 }
196
199 int has_right)
200 {
203 int nlen;
204 int layer = 0, sub;
205 int pix;
206 int i, j;
207
208 if (!y) {
209 memset(neighbours, src[-1], 4);
210 } else {
212 if (!x) {
214 } else {
215 neighbours[
TOP_LEFT] = src[-stride - 1];
216 neighbours[
LEFT] = src[-1];
217 }
218 if (has_right)
219 neighbours[
TOP_RIGHT] = src[-stride + 1];
220 else
222 }
223
224 sub = 0;
225 if (x >= 2 && src[-2] == neighbours[
LEFT])
226 sub = 1;
227 if (y >= 2 && src[-2 * stride] == neighbours[
TOP])
228 sub |= 2;
229
230 nlen = 1;
231 ref_pix[0] = neighbours[0];
232 for (i = 1; i < 4; i++) {
233 for (j = 0; j < nlen; j++)
234 if (ref_pix[j] == neighbours[i])
235 break;
236 if (j == nlen)
237 ref_pix[nlen++] = neighbours[i];
238 }
239
240 switch (nlen) {
241 case 1:
242 layer = 0;
243 break;
244 case 2:
245 if (neighbours[TOP] == neighbours[
TOP_LEFT]) {
246 if (neighbours[
TOP_RIGHT] == neighbours[TOP_LEFT])
247 layer = 1;
248 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
249 layer = 2;
250 else
251 layer = 3;
252 }
else if (neighbours[
TOP_RIGHT] == neighbours[TOP_LEFT]) {
253 if (neighbours[LEFT] == neighbours[TOP_LEFT])
254 layer = 4;
255 else
256 layer = 5;
257 } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
258 layer = 6;
259 } else {
260 layer = 7;
261 }
262 break;
263 case 3:
264 if (neighbours[TOP] == neighbours[TOP_LEFT])
265 layer = 8;
266 else if (neighbours[
TOP_RIGHT] == neighbours[TOP_LEFT])
267 layer = 9;
268 else if (neighbours[LEFT] == neighbours[TOP_LEFT])
269 layer = 10;
270 else if (neighbours[
TOP_RIGHT] == neighbours[TOP])
271 layer = 11;
272 else if (neighbours[TOP] == neighbours[LEFT])
273 layer = 12;
274 else
275 layer = 13;
276 break;
277 case 4:
278 layer = 14;
279 break;
280 }
281
284 if (pix < nlen)
285 return ref_pix[pix];
286 else
288 }
289
292 int rgb_stride,
PixContext *pctx,
const uint32_t *pal)
293 {
294 int i, j, p;
295 uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
296
298
299 for (j = 0; j <
height; j++) {
300 for (i = 0; i <
width; i++) {
301 if (!i && !j)
303 else
305 i, j, width - i - 1);
306 dst[i] = p;
307
308 if (rgb_pic)
309 AV_WB24(rgb_dst + i * 3, pal[p]);
310 }
312 rgb_dst += rgb_stride;
313 }
314
315 return 0;
316 }
317
320 {
321 int j;
322
324 for (j = y; j < y +
height; j++) {
327 width * 3);
330 width);
331 }
332 }
333
336 {
340 return -1;
341 else {
346 int j;
352 } else {
355 }
356 for (j = 0; j <
height; j++) {
357 memmove(dst, src, width);
358 memmove(rgb_dst, rgb_src, width * 3);
363 }
364 }
365 return 0;
366 }
367
370 int mask_stride,
int x,
int y,
373 {
374 int i, j, p;
376
378 mask += x + y * mask_stride;
379
380 for (j = 0; j <
height; j++) {
381 for (i = 0; i <
width; i++) {
383 ( c->
rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
384 !c->
rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
385 return -1;
386
387 if (mask[i] == 0x02) {
389 } else if (mask[i] == 0x04) {
391 return -1;
392 } else if (mask[i] != 0x80) {
393 if (!i && !j)
395 else
397 i, j, width - i - 1);
398 dst[i] = p;
401 }
402 }
404 mask += mask_stride;
406 }
407
408 return 0;
409 }
410
412 int version,
int full_model_syms)
413 {
419
421
423 full_model_syms, version ? 1 : 0);
424 }
425
427 {
435 }
436
438 {
440
443
444 if (val > 2) {
445 if ((base + 1) / 2 - 2 <= 0)
446 return -1;
447
448 val = acoder->
get_number(acoder, (base + 1) / 2 - 2) + 3;
449 }
450
451 if ((unsigned)val >= base)
452 return -1;
453
454 return inv ? base - val :
val;
455 }
456
459 {
462
464
465 if (!mode) {
466 int i, j, pix, rgb_pix;
471
473 rgb_pix = c->
pal[pix];
474 for (i = 0; i <
height; i++, dst +=
stride, rgb_dst += rgb_stride) {
475 memset(dst, pix, width);
477 for (j = 0; j < width * 3; j += 3)
479 }
480 } else {
484 }
485
486 return 0;
487 }
488
491 {
494
496
497 if (!mode) {
499
501 ( c->
rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
502 !c->
rgb_pic && mode != 0x80 && mode != 0xFF))
503 return -1;
504
505 if (mode == 0x02)
507 else if (mode == 0x04)
509 else if (mode != 0x80)
511 } else {
515 return -1;
519 x, y, width, height,
521 }
522
523 return 0;
524 }
525
528 {
530
532
533 switch (mode) {
536 return -1;
538 return -1;
540 return -1;
541 break;
544 return -1;
546 return -1;
548 return -1;
549 break;
553 else
555 default:
556 return -1;
557 }
558
559 return 0;
560 }
561
564 {
566 int i;
567
572 }
573
576 "Insufficient extradata size: expected %d got %d\n",
580 }
581
588 }
593 }
594
599 "Header version doesn't match codec tag\n");
600 return -1;
601 }
602
606 "Incorrect number of changeable palette entries: %d\n",
609 }
611
626
627 if (version) {
630 "Insufficient extradata size %d for v2\n",
633 }
634
637
641 "Incorrect number of used colours %d\n",
644 }
647 } else {
650 }
651
652 for (i = 0; i < 256; i++)
654 (version ? 8 : 0) + i * 3);
655
661 }
662
668 }
670
671 return 0;
672 }
673
675 {
677
678 return 0;
679 }