3 * Copyright (c) 2012 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/intreadwrite.h"
34 #include "bytestream.h"
38 #include "jpegtables.h"
55 static const uint8_t luma_quant[64] = {
56 8, 6, 5, 8, 12, 20, 26, 31,
57 6, 6, 7, 10, 13, 29, 30, 28,
58 7, 7, 8, 12, 20, 29, 35, 28,
59 7, 9, 11, 15, 26, 44, 40, 31,
60 9, 11, 19, 28, 34, 55, 52, 39,
61 12, 18, 28, 32, 41, 52, 57, 46,
62 25, 32, 39, 44, 52, 61, 60, 51,
63 36, 46, 48, 49, 56, 50, 52, 50
66 static const uint8_t chroma_quant[64] = {
67 9, 9, 12, 24, 50, 50, 50, 50,
68 9, 11, 13, 33, 50, 50, 50, 50,
69 12, 13, 28, 50, 50, 50, 50, 50,
70 24, 33, 50, 50, 50, 50, 50, 50,
71 50, 50, 50, 50, 50, 50, 50, 50,
72 50, 50, 50, 50, 50, 50, 50, 50,
73 50, 50, 50, 50, 50, 50, 50, 50,
74 50, 50, 50, 50, 50, 50, 50, 50,
77 typedef struct JPGContext {
82 VLC dc_vlc[2], ac_vlc[2];
84 DECLARE_ALIGNED(16, int16_t, block)[6][64];
89 typedef struct G2MContext {
94 int width, height, bpp;
95 int orig_width, orig_height;
96 int tile_width, tile_height;
97 int tiles_x, tiles_y, tile_x, tile_y;
102 int framebuf_stride, old_width, old_height;
104 uint8_t *synth_tile, *jpeg_tile;
105 int tile_stride, old_tile_w, old_tile_h;
107 uint8_t *kempf_buf, *kempf_flags;
112 int cursor_w, cursor_h, cursor_x, cursor_y;
113 int cursor_hot_x, cursor_hot_y;
116 static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
117 const uint8_t *val_table, int nb_codes,
120 uint8_t huff_size[256] = { 0 };
121 uint16_t huff_code[256];
122 uint16_t huff_sym[256];
125 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
127 for (i = 0; i < 256; i++)
128 huff_sym[i] = i + 16 * is_ac;
131 huff_sym[0] = 16 * 256;
133 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
134 huff_code, 2, 2, huff_sym, 2, 2, 0);
137 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
141 ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
142 avpriv_mjpeg_val_dc, 12, 0);
145 ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
146 avpriv_mjpeg_val_dc, 12, 0);
149 ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
150 avpriv_mjpeg_val_ac_luminance, 251, 1);
153 ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
154 avpriv_mjpeg_val_ac_chrominance, 251, 1);
158 ff_blockdsp_init(&c->bdsp, avctx);
159 ff_idctdsp_init(&c->idsp, avctx);
160 ff_init_scantable(c->idsp.idct_permutation, &c->scantable,
166 static av_cold void jpg_free_context(JPGContext *ctx)
170 for (i = 0; i < 2; i++) {
171 ff_free_vlc(&ctx->dc_vlc[i]);
172 ff_free_vlc(&ctx->ac_vlc[i]);
178 static void jpg_unescape(const uint8_t *src, int src_size,
179 uint8_t *dst, int *dst_size)
181 const uint8_t *src_end = src + src_size;
182 uint8_t *dst_start = dst;
184 while (src < src_end) {
189 if (x == 0xFF && !*src)
192 *dst_size = dst - dst_start;
195 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
196 int plane, int16_t *block)
199 const int is_chroma = !!plane;
200 const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
202 c->bdsp.clear_block(block);
203 dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
205 return AVERROR_INVALIDDATA;
207 dc = get_xbits(gb, dc);
208 dc = dc * qmat[0] + c->prev_dc[plane];
210 c->prev_dc[plane] = dc;
214 val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
216 return AVERROR_INVALIDDATA;
220 return val ? AVERROR_INVALIDDATA : 0;
224 val = get_xbits(gb, nbits);
225 val *= qmat[ff_zigzag_direct[pos]];
226 block[c->scantable.permutated[pos]] = val;
232 static inline void yuv2rgb(uint8_t *out, int Y, int U, int V)
234 out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
235 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
236 out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
239 static int jpg_decode_data(JPGContext *c, int width, int height,
240 const uint8_t *src, int src_size,
241 uint8_t *dst, int dst_stride,
242 const uint8_t *mask, int mask_stride, int num_mbs,
246 int mb_w, mb_h, mb_x, mb_y, i, j;
251 if ((ret = av_reallocp(&c->buf,
252 src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
254 jpg_unescape(src, src_size, c->buf, &unesc_size);
255 memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
256 if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
259 width = FFALIGN(width, 16);
261 mb_h = (height + 15) >> 4;
264 num_mbs = mb_w * mb_h * 4;
266 for (i = 0; i < 3; i++)
267 c->prev_dc[i] = 1024;
270 c->bdsp.clear_blocks(c->block[0]);
271 for (mb_y = 0; mb_y < mb_h; mb_y++) {
272 for (mb_x = 0; mb_x < mb_w; mb_x++) {
273 if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
274 !mask[mb_x * 2 + mask_stride] &&
275 !mask[mb_x * 2 + 1 + mask_stride]) {
279 for (j = 0; j < 2; j++) {
280 for (i = 0; i < 2; i++) {
281 if (mask && !mask[mb_x * 2 + i + j * mask_stride])
284 if ((ret = jpg_decode_block(c, &gb, 0,
285 c->block[i + j * 2])) != 0)
287 c->idsp.idct(c->block[i + j * 2]);
290 for (i = 1; i < 3; i++) {
291 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
293 c->idsp.idct(c->block[i + 3]);
296 for (j = 0; j < 16; j++) {
297 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
298 for (i = 0; i < 16; i++) {
301 Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
302 U = c->block[4 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
303 V = c->block[5 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
304 yuv2rgb(out + i * 3, Y, U, V);
315 mask += mask_stride * 2;
321 static int kempf_restore_buf(const uint8_t *src, int len,
322 uint8_t *dst, int stride,
323 const uint8_t *jpeg_tile, int tile_stride,
324 int width, int height,
325 const uint8_t *pal, int npal, int tidx)
331 if ((ret = init_get_bits8(&gb, src, len)) < 0)
334 if (npal <= 2) nb = 1;
335 else if (npal <= 4) nb = 2;
336 else if (npal <= 16) nb = 4;
339 for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
340 if (get_bits(&gb, 8))
342 for (i = 0; i < width; i++) {
343 col = get_bits(&gb, nb);
345 memcpy(dst + i * 3, pal + col * 3, 3);
347 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
354 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
355 const uint8_t *src, int src_size)
358 int hdr, zsize, npal, tidx = -1, ret;
360 const uint8_t *src_end = src + src_size;
361 uint8_t pal[768], transp[3];
362 uLongf dlen = (c->tile_width + 1) * c->tile_height;
364 int nblocks, cblocks, bstride;
365 int bits, bitbuf, coded;
366 uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
367 tile_y * c->tile_height * c->framebuf_stride;
370 return AVERROR_INVALIDDATA;
372 width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
373 height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
379 memcpy(transp, src, 3);
381 for (j = 0; j < height; j++, dst += c->framebuf_stride)
382 for (i = 0; i < width; i++)
383 memcpy(dst + i * 3, transp, 3);
385 } else if (sub_type == 1) {
386 return jpg_decode_data(&c->jc, width, height, src, src_end - src,
387 dst, c->framebuf_stride, NULL, 0, 0, 0);
391 memcpy(transp, src, 3);
395 if (src_end - src < npal * 3)
396 return AVERROR_INVALIDDATA;
397 memcpy(pal, src, npal * 3);
400 for (i = 0; i < npal; i++) {
401 if (!memcmp(pal + i * 3, transp, 3)) {
408 if (src_end - src < 2)
410 zsize = (src[0] << 8) | src[1];
413 if (src_end - src < zsize + (sub_type != 2))
414 return AVERROR_INVALIDDATA;
416 ret = uncompress(c->kempf_buf, &dlen, src, zsize);
418 return AVERROR_INVALIDDATA;
422 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
423 NULL, 0, width, height, pal, npal, tidx);
427 nblocks = *src++ + 1;
429 bstride = FFALIGN(width, 16) >> 3;
430 // blocks are coded LSB and we need normal bitreader for JPEG data
432 for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
433 for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
436 return AVERROR_INVALIDDATA;
444 if (cblocks > nblocks)
445 return AVERROR_INVALIDDATA;
446 c->kempf_flags[j * 2 + i * 2 * bstride] =
447 c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
448 c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
449 c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
453 memset(c->jpeg_tile, 0, c->tile_stride * height);
454 jpg_decode_data(&c->jc, width, height, src, src_end - src,
455 c->jpeg_tile, c->tile_stride,
456 c->kempf_flags, bstride, nblocks * 4, 0);
458 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
459 c->jpeg_tile, c->tile_stride,
460 width, height, pal, npal, tidx);
465 static int g2m_init_buffers(G2MContext *c)
469 if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
470 c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
471 aligned_height = c->height + 15;
472 av_free(c->framebuf);
473 c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
475 return AVERROR(ENOMEM);
477 if (!c->synth_tile || !c->jpeg_tile ||
478 c->old_tile_w < c->tile_width ||
479 c->old_tile_h < c->tile_height) {
480 c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
481 aligned_height = FFALIGN(c->tile_height, 16);
482 av_free(c->synth_tile);
483 av_free(c->jpeg_tile);
484 av_free(c->kempf_buf);
485 av_free(c->kempf_flags);
486 c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
487 c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
488 c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height
489 + FF_INPUT_BUFFER_PADDING_SIZE);
490 c->kempf_flags = av_mallocz( c->tile_width * aligned_height);
491 if (!c->synth_tile || !c->jpeg_tile ||
492 !c->kempf_buf || !c->kempf_flags)
493 return AVERROR(ENOMEM);
499 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
505 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
506 uint32_t cursor_hot_x, cursor_hot_y;
509 cur_size = bytestream2_get_be32(gb);
510 cursor_w = bytestream2_get_byte(gb);
511 cursor_h = bytestream2_get_byte(gb);
512 cursor_hot_x = bytestream2_get_byte(gb);
513 cursor_hot_y = bytestream2_get_byte(gb);
514 cursor_fmt = bytestream2_get_byte(gb);
516 cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
518 if (cursor_w < 1 || cursor_w > 256 ||
519 cursor_h < 1 || cursor_h > 256) {
520 av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
522 return AVERROR_INVALIDDATA;
524 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
525 av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
526 cursor_hot_x, cursor_hot_y);
527 cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
528 cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
530 if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
531 c->cursor_w * c->cursor_h / 4 > cur_size) {
532 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
533 cur_size, bytestream2_get_bytes_left(gb));
534 return AVERROR_INVALIDDATA;
536 if (cursor_fmt != 1 && cursor_fmt != 32) {
537 avpriv_report_missing_feature(avctx, "Cursor format %d",
539 return AVERROR_PATCHWELCOME;
542 if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
543 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
547 c->cursor_w = cursor_w;
548 c->cursor_h = cursor_h;
549 c->cursor_hot_x = cursor_hot_x;
550 c->cursor_hot_y = cursor_hot_y;
551 c->cursor_fmt = cursor_fmt;
552 c->cursor_stride = cursor_stride;
555 switch (c->cursor_fmt) {
556 case 1: // old monochrome
557 for (j = 0; j < c->cursor_h; j++) {
558 for (i = 0; i < c->cursor_w; i += 32) {
559 bits = bytestream2_get_be32(gb);
560 for (k = 0; k < 32; k++) {
561 dst[0] = !!(bits & 0x80000000);
569 for (j = 0; j < c->cursor_h; j++) {
570 for (i = 0; i < c->cursor_w; i += 32) {
571 bits = bytestream2_get_be32(gb);
572 for (k = 0; k < 32; k++) {
573 int mask_bit = !!(bits & 0x80000000);
574 switch (dst[0] * 2 + mask_bit) {
599 case 32: // full colour
600 /* skip monochrome version of the cursor and decode RGBA instead */
601 bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
602 for (j = 0; j < c->cursor_h; j++) {
603 for (i = 0; i < c->cursor_w; i++) {
604 int val = bytestream2_get_be32(gb);
613 return AVERROR_PATCHWELCOME;
618 #define APPLY_ALPHA(src, new, alpha) \
619 src = (src * (256 - alpha) + new * alpha) >> 8
621 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
625 const uint8_t *cursor;
630 x = c->cursor_x - c->cursor_hot_x;
631 y = c->cursor_y - c->cursor_hot_y;
637 if (x + w > c->width)
639 if (y + h > c->height)
649 cursor += -y * c->cursor_stride;
656 for (j = 0; j < h; j++) {
657 for (i = 0; i < w; i++) {
658 uint8_t alpha = cursor[i * 4];
659 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
660 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
661 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
664 cursor += c->cursor_stride;
668 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
669 int *got_picture_ptr, AVPacket *avpkt)
671 const uint8_t *buf = avpkt->data;
672 int buf_size = avpkt->size;
673 G2MContext *c = avctx->priv_data;
675 GetByteContext bc, tbc;
678 uint32_t chunk_size, r_mask, g_mask, b_mask;
679 int chunk_type, chunk_start;
684 av_log(avctx, AV_LOG_ERROR,
685 "Frame should have at least 12 bytes, got %d instead\n",
687 return AVERROR_INVALIDDATA;
690 bytestream2_init(&bc, buf, buf_size);
692 magic = bytestream2_get_be32(&bc);
693 if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
694 (magic & 0xF) < 2 || (magic & 0xF) > 5) {
695 av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
696 return AVERROR_INVALIDDATA;
699 if ((magic & 0xF) < 4) {
700 av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
701 return AVERROR(ENOSYS);
704 while (bytestream2_get_bytes_left(&bc) > 5) {
705 chunk_size = bytestream2_get_le32(&bc) - 1;
706 chunk_type = bytestream2_get_byte(&bc);
707 chunk_start = bytestream2_tell(&bc);
708 if (chunk_size > bytestream2_get_bytes_left(&bc)) {
709 av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
710 chunk_size, chunk_type);
713 switch (chunk_type) {
717 if (chunk_size < 21) {
718 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
722 c->width = bytestream2_get_be32(&bc);
723 c->height = bytestream2_get_be32(&bc);
724 if (c->width < 16 || c->width > c->orig_width ||
725 c->height < 16 || c->height > c->orig_height) {
726 av_log(avctx, AV_LOG_ERROR,
727 "Invalid frame dimensions %dx%d\n",
728 c->width, c->height);
729 ret = AVERROR_INVALIDDATA;
732 if (c->width != avctx->width || c->height != avctx->height) {
733 ret = ff_set_dimensions(avctx, c->width, c->height);
737 c->compression = bytestream2_get_be32(&bc);
738 if (c->compression != 2 && c->compression != 3) {
739 av_log(avctx, AV_LOG_ERROR,
740 "Unknown compression method %d\n",
742 ret = AVERROR_PATCHWELCOME;
745 c->tile_width = bytestream2_get_be32(&bc);
746 c->tile_height = bytestream2_get_be32(&bc);
747 if (c->tile_width <= 0 || c->tile_height <= 0 ||
748 ((c->tile_width | c->tile_height) & 0xF) ||
749 c->tile_width * 4LL * c->tile_height >= INT_MAX
751 av_log(avctx, AV_LOG_ERROR,
752 "Invalid tile dimensions %dx%d\n",
753 c->tile_width, c->tile_height);
754 ret = AVERROR_INVALIDDATA;
757 c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
758 c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
759 c->bpp = bytestream2_get_byte(&bc);
761 if (bytestream2_get_bytes_left(&bc) < 16 ||
762 (chunk_size - 21) < 16) {
763 av_log(avctx, AV_LOG_ERROR,
764 "Display info: missing bitmasks!\n");
765 ret = AVERROR_INVALIDDATA;
768 r_mask = bytestream2_get_be32(&bc);
769 g_mask = bytestream2_get_be32(&bc);
770 b_mask = bytestream2_get_be32(&bc);
771 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
772 av_log(avctx, AV_LOG_ERROR,
773 "Invalid or unsupported bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32"\n",
774 r_mask, g_mask, b_mask);
775 ret = AVERROR_PATCHWELCOME;
779 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
780 ret = AVERROR_PATCHWELCOME;
783 if (g2m_init_buffers(c)) {
784 ret = AVERROR(ENOMEM);
790 if (!c->tiles_x || !c->tiles_y) {
791 av_log(avctx, AV_LOG_WARNING,
792 "No display info - skipping tile\n");
795 if (chunk_size < 2) {
796 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
800 c->tile_x = bytestream2_get_byte(&bc);
801 c->tile_y = bytestream2_get_byte(&bc);
802 if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
803 av_log(avctx, AV_LOG_ERROR,
804 "Invalid tile pos %d,%d (in %dx%d grid)\n",
805 c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
809 switch (c->compression) {
811 av_log(avctx, AV_LOG_ERROR,
812 "ePIC j-b compression is not implemented yet\n");
813 return AVERROR(ENOSYS);
814 case COMPR_KEMPF_J_B:
815 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
816 buf + bytestream2_tell(&bc),
820 if (ret && c->framebuf)
821 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
822 c->tile_x, c->tile_y);
825 if (chunk_size < 5) {
826 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
830 c->cursor_x = bytestream2_get_be16(&bc);
831 c->cursor_y = bytestream2_get_be16(&bc);
834 if (chunk_size < 8) {
835 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
839 bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
841 g2m_load_cursor(avctx, c, &tbc);
847 av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
851 /* navigate to next chunk */
852 bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
857 if (c->width && c->height && c->framebuf) {
858 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
861 pic->key_frame = got_header;
862 pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
864 for (i = 0; i < avctx->height; i++)
865 memcpy(pic->data[0] + i * pic->linesize[0],
866 c->framebuf + i * c->framebuf_stride,
868 g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
870 *got_picture_ptr = 1;
883 static av_cold int g2m_decode_init(AVCodecContext *avctx)
885 G2MContext *const c = avctx->priv_data;
888 if ((ret = jpg_init(avctx, &c->jc)) != 0) {
889 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
890 jpg_free_context(&c->jc);
891 return AVERROR(ENOMEM);
894 avctx->pix_fmt = AV_PIX_FMT_RGB24;
896 // store original sizes and check against those if resize happens
897 c->orig_width = avctx->width;
898 c->orig_height = avctx->height;
903 static av_cold int g2m_decode_end(AVCodecContext *avctx)
905 G2MContext *const c = avctx->priv_data;
907 jpg_free_context(&c->jc);
909 av_freep(&c->kempf_buf);
910 av_freep(&c->kempf_flags);
911 av_freep(&c->synth_tile);
912 av_freep(&c->jpeg_tile);
913 av_freep(&c->cursor);
914 av_freep(&c->framebuf);
919 AVCodec ff_g2m_decoder = {
921 .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
922 .type = AVMEDIA_TYPE_VIDEO,
923 .id = AV_CODEC_ID_G2M,
924 .priv_data_size = sizeof(G2MContext),
925 .init = g2m_decode_init,
926 .close = g2m_decode_end,
927 .decode = g2m_decode_frame,
928 .capabilities = CODEC_CAP_DR1,