1 /*
2 * Go2Webinar decoder
3 * Copyright (c) 2012 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 * Go2Webinar decoder
25 */
26
27 #include <zlib.h>
28
36
44 };
45
49 };
50
52 8, 6, 5, 8, 12, 20, 26, 31,
53 6, 6, 7, 10, 13, 29, 30, 28,
54 7, 7, 8, 12, 20, 29, 35, 28,
55 7, 9, 11, 15, 26, 44, 40, 31,
56 9, 11, 19, 28, 34, 55, 52, 39,
57 12, 18, 28, 32, 41, 52, 57, 46,
58 25, 32, 39, 44, 52, 61, 60, 51,
59 36, 46, 48, 49, 56, 50, 52, 50
60 };
61
63 9, 9, 12, 24, 50, 50, 50, 50,
64 9, 11, 13, 33, 50, 50, 50, 50,
65 12, 13, 28, 50, 50, 50, 50, 50,
66 24, 33, 50, 50, 50, 50, 50, 50,
67 50, 50, 50, 50, 50, 50, 50, 50,
68 50, 50, 50, 50, 50, 50, 50, 50,
69 50, 50, 50, 50, 50, 50, 50, 50,
70 50, 50, 50, 50, 50, 50, 50, 50,
71 };
72
76
80
83
87
92
94
97
100
102
109
111 const uint8_t *val_table,
int nb_codes,
112 int is_ac)
113 {
114 uint8_t huff_size[256] = { 0 };
115 uint16_t huff_code[256];
116 uint16_t huff_sym[256];
117 int i;
118
120
121 for (i = 0; i < 256; i++)
122 huff_sym[i] = i + 16 * is_ac;
123
124 if (is_ac)
125 huff_sym[0] = 16 * 256;
126
128 huff_code, 2, 2, huff_sym, 2, 2, 0);
129 }
130
132 {
134
137 if (ret)
141 if (ret)
145 if (ret)
149 if (ret)
151
155
156 return 0;
157 }
158
160 {
161 int i;
162
163 for (i = 0; i < 2; i++) {
166 }
167
169 }
170
173 {
174 const uint8_t *src_end = src + src_size;
176
177 while (src < src_end) {
179
180 *dst++ = x;
181
182 if (x == 0xFF && !*src)
183 src++;
184 }
185 *dst_size = dst - dst_start;
186 }
187
189 int plane, int16_t *
block)
190 {
192 const int is_chroma = !!plane;
194
197 if (dc < 0)
199 if (dc)
201 dc = dc * qmat[0] + c->
prev_dc[plane];
204
205 pos = 0;
206 while (pos < 63) {
208 if (val < 0)
210 pos += val >> 4;
211 val &= 0xF;
212 if (pos > 63)
214 if (val) {
216
220 }
221 }
222 return 0;
223 }
224
226 {
227 out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
228 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
229 out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
230 }
231
236 int swapuv)
237 {
240 int mb_w, mb_h, mb_x, mb_y, i, j;
241 int bx, by;
242 int unesc_size;
244
246 if (!tmp)
252
254 mb_w = width >> 4;
255 mb_h = (height + 15) >> 4;
256
257 if (!num_mbs)
258 num_mbs = mb_w * mb_h;
259
260 for (i = 0; i < 3; i++)
262 bx = by = 0;
263 for (mb_y = 0; mb_y < mb_h; mb_y++) {
264 for (mb_x = 0; mb_x < mb_w; mb_x++) {
265 if (mask && !mask[mb_x]) {
266 bx += 16;
267 continue;
268 }
269 for (j = 0; j < 2; j++) {
270 for (i = 0; i < 2; i++) {
272 c->
block[i + j * 2])) != 0)
273 return ret;
275 }
276 }
277 for (i = 1; i < 3; i++) {
281 }
282
283 for (j = 0; j < 16; j++) {
284 uint8_t *
out = dst + bx * 3 + (by + j) * dst_stride;
285 for (i = 0; i < 16; i++) {
287
288 Y = c->
block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
289 U = c->
block[4 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
290 V = c->
block[5 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
292 }
293 }
294
295 if (!--num_mbs)
296 return 0;
297 bx += 16;
298 }
299 bx = 0;
300 by += 16;
301 if (mask)
302 mask += mask_stride;
303 }
304
305 return 0;
306 }
307
310 const uint8_t *jpeg_tile,
int tile_stride,
312 const uint8_t *pal,
int npal,
int tidx)
313 {
315 int i, j, nb, col;
316
318
319 if (npal <= 2) nb = 1;
320 else if (npal <= 4) nb = 2;
321 else if (npal <= 16) nb = 4;
322 else nb = 8;
323
324 for (j = 0; j <
height; j++, dst +=
stride, jpeg_tile += tile_stride) {
326 continue;
327 for (i = 0; i <
width; i++) {
329 if (col != tidx)
330 memcpy(dst + i * 3, pal + col * 3, 3);
331 else
332 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
333 }
334 }
335 }
336
339 {
341 int hdr, zsize, npal, tidx = -1,
ret;
342 int i, j;
343 const uint8_t *src_end = src + src_size;
346 int sub_type;
347 int nblocks, cblocks, bstride;
348 int bits, bitbuf, coded;
351
354
357
358 hdr = *src++;
359 sub_type = hdr >> 5;
360 if (sub_type == 0) {
361 int j;
362 memcpy(transp, src, 3);
363 src += 3;
365 for (i = 0; i <
width; i++)
366 memcpy(dst + i * 3, transp, 3);
367 return 0;
368 } else if (sub_type == 1) {
371 }
372
373 if (sub_type != 2) {
374 memcpy(transp, src, 3);
375 src += 3;
376 }
377 npal = *src++ + 1;
378 memcpy(pal, src, npal * 3); src += npal * 3;
379 if (sub_type != 2) {
380 for (i = 0; i < npal; i++) {
381 if (!memcmp(pal + i * 3, transp, 3)) {
382 tidx = i;
383 break;
384 }
385 }
386 }
387
388 if (src_end - src < 2)
389 return 0;
390 zsize = (src[0] << 8) | src[1]; src += 2;
391
392 if (src_end - src < zsize)
394
398 src += zsize;
399
400 if (sub_type == 2) {
402 NULL, 0, width, height, pal, npal, tidx);
403 return 0;
404 }
405
406 nblocks = *src++ + 1;
407 cblocks = 0;
408 bstride =
FFALIGN(width, 16) >> 4;
409 // blocks are coded LSB and we need normal bitreader for JPEG data
410 bits = 0;
411 for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
412 for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
413 if (!bits) {
414 bitbuf = *src++;
415 bits = 8;
416 }
417 coded = bitbuf & 1;
418 bits--;
419 bitbuf >>= 1;
420 cblocks += coded;
421 if (cblocks > nblocks)
424 }
425 }
426
431
434 width, height, pal, npal, tidx);
435
436 return 0;
437 }
438
440 {
441 int aligned_height;
442
450 }
468 }
469
470 return 0;
471 }
472
475 {
476 int i, j, k;
479 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
480 uint32_t cursor_hot_x, cursor_hot_y;
481 int cursor_fmt;
483
484 cur_size = bytestream2_get_be32(gb);
485 cursor_w = bytestream2_get_byte(gb);
486 cursor_h = bytestream2_get_byte(gb);
487 cursor_hot_x = bytestream2_get_byte(gb);
488 cursor_hot_y = bytestream2_get_byte(gb);
489 cursor_fmt = bytestream2_get_byte(gb);
490
491 cursor_stride = cursor_w * 4;
492
493 if (cursor_w < 1 || cursor_w > 256 ||
494 cursor_h < 1 || cursor_h > 256) {
496 cursor_w, cursor_h);
498 }
499 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
501 cursor_hot_x, cursor_hot_y);
502 cursor_hot_x =
FFMIN(cursor_hot_x, cursor_w - 1);
503 cursor_hot_y =
FFMIN(cursor_hot_y, cursor_h - 1);
504 }
510 }
511 if (cursor_fmt != 1 && cursor_fmt != 32) {
513 cursor_fmt);
515 }
516
517 if (cursor_fmt == 1 && cursor_w % 32) {
520 }
521
523 if (!tmp) {
526 }
527
535
538 case 1: // old monochrome
540 for (i = 0; i < c->
cursor_w; i += 32) {
541 bits = bytestream2_get_be32(gb);
542 for (k = 0; k < 32; k++) {
543 dst[0] = !!(bits & 0x80000000);
544 dst += 4;
545 bits <<= 1;
546 }
547 }
548 }
549
552 for (i = 0; i < c->
cursor_w; i += 32) {
553 bits = bytestream2_get_be32(gb);
554 for (k = 0; k < 32; k++) {
555 int mask_bit = !!(bits & 0x80000000);
556 switch (dst[0] * 2 + mask_bit) {
557 case 0:
558 dst[0] = 0xFF; dst[1] = 0x00;
559 dst[2] = 0x00; dst[3] = 0x00;
560 break;
561 case 1:
562 dst[0] = 0xFF; dst[1] = 0xFF;
563 dst[2] = 0xFF; dst[3] = 0xFF;
564 break;
565 default:
566 dst[0] = 0x00; dst[1] = 0x00;
567 dst[2] = 0x00; dst[3] = 0x00;
568 }
569 dst += 4;
570 bits <<= 1;
571 }
572 }
573 }
574 break;
575 case 32: // full colour
576 /* skip monochrome version of the cursor and decode RGBA instead */
580 int val = bytestream2_get_be32(gb);
581 *dst++ = val >> 0;
582 *dst++ = val >> 8;
583 *dst++ = val >> 16;
584 *dst++ = val >> 24;
585 }
586 }
587 break;
588 default:
590 }
591 return 0;
592 }
593
594 #define APPLY_ALPHA(src, new, alpha) \
595 src = (src * (256 - alpha) + new * alpha) >> 8
596
598 {
599 int i, j;
602
604 return;
605
608
612
613 if (x + w > c->
width)
617 if (x < 0) {
618 w += x;
619 cursor += -x * 4;
620 } else {
621 dst += x * 3;
622 }
623 if (y < 0) {
626 } else {
628 }
629 if (w < 0 || h < 0)
630 return;
631
632 for (j = 0; j < h; j++) {
633 for (i = 0; i < w; i++) {
635 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
636 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
637 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
638 }
641 }
642 }
643
645 int *got_picture_ptr,
AVPacket *avpkt)
646 {
648 int buf_size = avpkt->
size;
652 int magic;
653 int got_header = 0;
654 uint32_t chunk_size;
655 int chunk_type;
656 int i;
658
659 if (buf_size < 12) {
661 "Frame should have at least 12 bytes, got %d instead\n",
662 buf_size);
664 }
665
667
668 magic = bytestream2_get_be32(&bc);
669 if ((magic & ~0xF) !=
MKBETAG(
'G',
'2',
'M',
'0') ||
670 (magic & 0xF) < 2 || (magic & 0xF) > 4) {
673 }
674
675 if ((magic & 0xF) != 4) {
678 }
679
681 chunk_size = bytestream2_get_le32(&bc) - 1;
682 chunk_type = bytestream2_get_byte(&bc);
685 chunk_size, chunk_type);
686 break;
687 }
688 switch (chunk_type) {
691 if (chunk_size < 21) {
693 chunk_size);
694 break;
695 }
696 c->
width = bytestream2_get_be32(&bc);
697 c->
height = bytestream2_get_be32(&bc);
701 "Invalid frame dimensions %dx%d\n",
704 goto header_fail;
705 }
711 "Unknown compression method %d\n",
714 }
719 "Invalid tile dimensions %dx%d\n",
722 goto header_fail;
723 }
726 c->
bpp = bytestream2_get_byte(&bc);
727 chunk_size -= 21;
731 goto header_fail;
732 }
733 got_header = 1;
734 break;
738 "No frame header - skipping tile\n");
740 break;
741 }
742 if (chunk_size < 2) {
744 chunk_size);
745 break;
746 }
747 c->
tile_x = bytestream2_get_byte(&bc);
748 c->
tile_y = bytestream2_get_byte(&bc);
751 "Invalid tile pos %d,%d (in %dx%d grid)\n",
753 break;
754 }
755 chunk_size -= 2;
756 ret = 0;
760 "ePIC j-b compression is not implemented yet\n");
765 chunk_size);
766 break;
767 }
772 break;
774 if (chunk_size < 5) {
776 chunk_size);
777 break;
778 }
779 c->
cursor_x = bytestream2_get_be16(&bc);
780 c->
cursor_y = bytestream2_get_be16(&bc);
782 break;
784 if (chunk_size < 8) {
786 chunk_size);
787 break;
788 }
790 chunk_size - 4);
793 break;
797 break;
798 default:
800 chunk_type);
802 }
803 }
804 if (got_header)
806
811 }
812
815
816 for (i = 0; i < avctx->
height; i++)
821
822 *got_picture_ptr = 1;
823 }
824
825 return buf_size;
826 header_fail:
830 }
831
833 {
836
841 }
842
844
845 return 0;
846 }
847
849 {
851
853
860
861 return 0;
862 }
863
874 };