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"
32 #include "bytestream.h"
52 static const uint8_t luma_quant[64] = {
53 8, 6, 5, 8, 12, 20, 26, 31,
54 6, 6, 7, 10, 13, 29, 30, 28,
55 7, 7, 8, 12, 20, 29, 35, 28,
56 7, 9, 11, 15, 26, 44, 40, 31,
57 9, 11, 19, 28, 34, 55, 52, 39,
58 12, 18, 28, 32, 41, 52, 57, 46,
59 25, 32, 39, 44, 52, 61, 60, 51,
60 36, 46, 48, 49, 56, 50, 52, 50
63 static const uint8_t chroma_quant[64] = {
64 9, 9, 12, 24, 50, 50, 50, 50,
65 9, 11, 13, 33, 50, 50, 50, 50,
66 12, 13, 28, 50, 50, 50, 50, 50,
67 24, 33, 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 50, 50, 50, 50, 50, 50, 50, 50,
74 typedef struct JPGContext {
78 VLC dc_vlc[2], ac_vlc[2];
80 DECLARE_ALIGNED(16, int16_t, block)[6][64];
85 typedef struct G2MContext {
90 int width, height, bpp;
91 int tile_width, tile_height;
92 int tiles_x, tiles_y, tile_x, tile_y;
97 int framebuf_stride, old_width, old_height;
99 uint8_t *synth_tile, *jpeg_tile;
100 int tile_stride, old_tile_w, old_tile_h;
102 uint8_t *kempf_buf, *kempf_flags;
107 int cursor_w, cursor_h, cursor_x, cursor_y;
108 int cursor_hot_x, cursor_hot_y;
111 static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
112 const uint8_t *val_table, int nb_codes,
115 uint8_t huff_size[256] = { 0 };
116 uint16_t huff_code[256];
117 uint16_t huff_sym[256];
120 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
122 for (i = 0; i < 256; i++)
123 huff_sym[i] = i + 16 * is_ac;
126 huff_sym[0] = 16 * 256;
128 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
129 huff_code, 2, 2, huff_sym, 2, 2, 0);
132 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
136 ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
137 avpriv_mjpeg_val_dc, 12, 0);
140 ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
141 avpriv_mjpeg_val_dc, 12, 0);
144 ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
145 avpriv_mjpeg_val_ac_luminance, 251, 1);
148 ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
149 avpriv_mjpeg_val_ac_chrominance, 251, 1);
153 ff_dsputil_init(&c->dsp, avctx);
154 ff_init_scantable(c->dsp.idct_permutation, &c->scantable,
160 static av_cold void jpg_free_context(JPGContext *ctx)
164 for (i = 0; i < 2; i++) {
165 ff_free_vlc(&ctx->dc_vlc[i]);
166 ff_free_vlc(&ctx->ac_vlc[i]);
172 static void jpg_unescape(const uint8_t *src, int src_size,
173 uint8_t *dst, int *dst_size)
175 const uint8_t *src_end = src + src_size;
176 uint8_t *dst_start = dst;
178 while (src < src_end) {
183 if (x == 0xFF && !*src)
186 *dst_size = dst - dst_start;
189 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
190 int plane, int16_t *block)
193 const int is_chroma = !!plane;
194 const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
196 c->dsp.clear_block(block);
197 dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
199 return AVERROR_INVALIDDATA;
201 dc = get_xbits(gb, dc);
202 dc = dc * qmat[0] + c->prev_dc[plane];
204 c->prev_dc[plane] = dc;
208 val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
210 return AVERROR_INVALIDDATA;
214 return val ? AVERROR_INVALIDDATA : 0;
218 val = get_xbits(gb, nbits);
219 val *= qmat[ff_zigzag_direct[pos]];
220 block[c->scantable.permutated[pos]] = val;
226 static inline void yuv2rgb(uint8_t *out, int Y, int U, int V)
228 out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
229 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
230 out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
233 static int jpg_decode_data(JPGContext *c, int width, int height,
234 const uint8_t *src, int src_size,
235 uint8_t *dst, int dst_stride,
236 const uint8_t *mask, int mask_stride, int num_mbs,
240 int mb_w, mb_h, mb_x, mb_y, i, j;
245 if ((ret = av_reallocp(&c->buf,
246 src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
248 jpg_unescape(src, src_size, c->buf, &unesc_size);
249 memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
250 init_get_bits(&gb, c->buf, unesc_size * 8);
252 width = FFALIGN(width, 16);
254 mb_h = (height + 15) >> 4;
257 num_mbs = mb_w * mb_h * 4;
259 for (i = 0; i < 3; i++)
260 c->prev_dc[i] = 1024;
262 c->dsp.clear_blocks(c->block[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 * 2] && !mask[mb_x * 2 + 1] &&
266 !mask[mb_x * 2 + mask_stride] &&
267 !mask[mb_x * 2 + 1 + mask_stride]) {
271 for (j = 0; j < 2; j++) {
272 for (i = 0; i < 2; i++) {
273 if (mask && !mask[mb_x * 2 + i + j * mask_stride])
276 if ((ret = jpg_decode_block(c, &gb, 0,
277 c->block[i + j * 2])) != 0)
279 c->dsp.idct(c->block[i + j * 2]);
282 for (i = 1; i < 3; i++) {
283 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
285 c->dsp.idct(c->block[i + 3]);
288 for (j = 0; j < 16; j++) {
289 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
290 for (i = 0; i < 16; i++) {
293 Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
294 U = c->block[4 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
295 V = c->block[5 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
296 yuv2rgb(out + i * 3, Y, U, V);
307 mask += mask_stride * 2;
313 static void kempf_restore_buf(const uint8_t *src, int len,
314 uint8_t *dst, int stride,
315 const uint8_t *jpeg_tile, int tile_stride,
316 int width, int height,
317 const uint8_t *pal, int npal, int tidx)
322 init_get_bits(&gb, src, len * 8);
324 if (npal <= 2) nb = 1;
325 else if (npal <= 4) nb = 2;
326 else if (npal <= 16) nb = 4;
329 for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
330 if (get_bits(&gb, 8))
332 for (i = 0; i < width; i++) {
333 col = get_bits(&gb, nb);
335 memcpy(dst + i * 3, pal + col * 3, 3);
337 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
342 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
343 const uint8_t *src, int src_size)
346 int hdr, zsize, npal, tidx = -1, ret;
348 const uint8_t *src_end = src + src_size;
349 uint8_t pal[768], transp[3];
350 uLongf dlen = (c->tile_width + 1) * c->tile_height;
352 int nblocks, cblocks, bstride;
353 int bits, bitbuf, coded;
354 uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
355 tile_y * c->tile_height * c->framebuf_stride;
358 return AVERROR_INVALIDDATA;
360 width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
361 height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
367 memcpy(transp, src, 3);
369 for (j = 0; j < height; j++, dst += c->framebuf_stride)
370 for (i = 0; i < width; i++)
371 memcpy(dst + i * 3, transp, 3);
373 } else if (sub_type == 1) {
374 return jpg_decode_data(&c->jc, width, height, src, src_end - src,
375 dst, c->framebuf_stride, NULL, 0, 0, 0);
379 memcpy(transp, src, 3);
383 if (src_end - src < npal * 3)
384 return AVERROR_INVALIDDATA;
385 memcpy(pal, src, npal * 3);
388 for (i = 0; i < npal; i++) {
389 if (!memcmp(pal + i * 3, transp, 3)) {
396 if (src_end - src < 2)
398 zsize = (src[0] << 8) | src[1];
401 if (src_end - src < zsize + (sub_type != 2))
402 return AVERROR_INVALIDDATA;
404 ret = uncompress(c->kempf_buf, &dlen, src, zsize);
406 return AVERROR_INVALIDDATA;
410 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
411 NULL, 0, width, height, pal, npal, tidx);
415 nblocks = *src++ + 1;
417 bstride = FFALIGN(width, 16) >> 3;
418 // blocks are coded LSB and we need normal bitreader for JPEG data
420 for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
421 for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
424 return AVERROR_INVALIDDATA;
432 if (cblocks > nblocks)
433 return AVERROR_INVALIDDATA;
434 c->kempf_flags[j * 2 + i * 2 * bstride] =
435 c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
436 c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
437 c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
441 memset(c->jpeg_tile, 0, c->tile_stride * height);
442 jpg_decode_data(&c->jc, width, height, src, src_end - src,
443 c->jpeg_tile, c->tile_stride,
444 c->kempf_flags, bstride, nblocks * 4, 0);
446 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
447 c->jpeg_tile, c->tile_stride,
448 width, height, pal, npal, tidx);
453 static int g2m_init_buffers(G2MContext *c)
457 if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
458 c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
459 aligned_height = c->height + 15;
460 av_free(c->framebuf);
461 c->framebuf = av_mallocz(c->framebuf_stride * aligned_height);
463 return AVERROR(ENOMEM);
465 if (!c->synth_tile || !c->jpeg_tile ||
466 c->old_tile_w < c->tile_width ||
467 c->old_tile_h < c->tile_height) {
468 c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
469 aligned_height = FFALIGN(c->tile_height, 16);
470 av_free(c->synth_tile);
471 av_free(c->jpeg_tile);
472 av_free(c->kempf_buf);
473 av_free(c->kempf_flags);
474 c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
475 c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
476 c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height
477 + FF_INPUT_BUFFER_PADDING_SIZE);
478 c->kempf_flags = av_mallocz( c->tile_width * aligned_height);
479 if (!c->synth_tile || !c->jpeg_tile ||
480 !c->kempf_buf || !c->kempf_flags)
481 return AVERROR(ENOMEM);
487 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
493 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
494 uint32_t cursor_hot_x, cursor_hot_y;
497 cur_size = bytestream2_get_be32(gb);
498 cursor_w = bytestream2_get_byte(gb);
499 cursor_h = bytestream2_get_byte(gb);
500 cursor_hot_x = bytestream2_get_byte(gb);
501 cursor_hot_y = bytestream2_get_byte(gb);
502 cursor_fmt = bytestream2_get_byte(gb);
504 cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
506 if (cursor_w < 1 || cursor_w > 256 ||
507 cursor_h < 1 || cursor_h > 256) {
508 av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
510 return AVERROR_INVALIDDATA;
512 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
513 av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
514 cursor_hot_x, cursor_hot_y);
515 cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
516 cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
518 if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
519 c->cursor_w * c->cursor_h / 4 > cur_size) {
520 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
521 cur_size, bytestream2_get_bytes_left(gb));
522 return AVERROR_INVALIDDATA;
524 if (cursor_fmt != 1 && cursor_fmt != 32) {
525 avpriv_report_missing_feature(avctx, "Cursor format %d",
527 return AVERROR_PATCHWELCOME;
530 if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
531 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
535 c->cursor_w = cursor_w;
536 c->cursor_h = cursor_h;
537 c->cursor_hot_x = cursor_hot_x;
538 c->cursor_hot_y = cursor_hot_y;
539 c->cursor_fmt = cursor_fmt;
540 c->cursor_stride = cursor_stride;
543 switch (c->cursor_fmt) {
544 case 1: // old monochrome
545 for (j = 0; j < c->cursor_h; j++) {
546 for (i = 0; i < c->cursor_w; i += 32) {
547 bits = bytestream2_get_be32(gb);
548 for (k = 0; k < 32; k++) {
549 dst[0] = !!(bits & 0x80000000);
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 int mask_bit = !!(bits & 0x80000000);
562 switch (dst[0] * 2 + mask_bit) {
587 case 32: // full colour
588 /* skip monochrome version of the cursor and decode RGBA instead */
589 bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
590 for (j = 0; j < c->cursor_h; j++) {
591 for (i = 0; i < c->cursor_w; i++) {
592 int val = bytestream2_get_be32(gb);
601 return AVERROR_PATCHWELCOME;
606 #define APPLY_ALPHA(src, new, alpha) \
607 src = (src * (256 - alpha) + new * alpha) >> 8
609 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
613 const uint8_t *cursor;
618 x = c->cursor_x - c->cursor_hot_x;
619 y = c->cursor_y - c->cursor_hot_y;
625 if (x + w > c->width)
627 if (y + h > c->height)
637 cursor += -y * c->cursor_stride;
644 for (j = 0; j < h; j++) {
645 for (i = 0; i < w; i++) {
646 uint8_t alpha = cursor[i * 4];
647 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
648 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
649 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
652 cursor += c->cursor_stride;
656 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
657 int *got_picture_ptr, AVPacket *avpkt)
659 const uint8_t *buf = avpkt->data;
660 int buf_size = avpkt->size;
661 G2MContext *c = avctx->priv_data;
663 GetByteContext bc, tbc;
666 uint32_t chunk_size, r_mask, g_mask, b_mask;
667 int chunk_type, chunk_start;
672 av_log(avctx, AV_LOG_ERROR,
673 "Frame should have at least 12 bytes, got %d instead\n",
675 return AVERROR_INVALIDDATA;
678 bytestream2_init(&bc, buf, buf_size);
680 magic = bytestream2_get_be32(&bc);
681 if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
682 (magic & 0xF) < 2 || (magic & 0xF) > 4) {
683 av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
684 return AVERROR_INVALIDDATA;
687 if ((magic & 0xF) != 4) {
688 av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
689 return AVERROR(ENOSYS);
692 while (bytestream2_get_bytes_left(&bc) > 5) {
693 chunk_size = bytestream2_get_le32(&bc) - 1;
694 chunk_type = bytestream2_get_byte(&bc);
695 chunk_start = bytestream2_tell(&bc);
696 if (chunk_size > bytestream2_get_bytes_left(&bc)) {
697 av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
698 chunk_size, chunk_type);
701 switch (chunk_type) {
705 if (chunk_size < 21) {
706 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
710 c->width = bytestream2_get_be32(&bc);
711 c->height = bytestream2_get_be32(&bc);
712 if (c->width < 16 || c->width > avctx->width ||
713 c->height < 16 || c->height > avctx->height) {
714 av_log(avctx, AV_LOG_ERROR,
715 "Invalid frame dimensions %dx%d\n",
716 c->width, c->height);
717 ret = AVERROR_INVALIDDATA;
720 if (c->width != avctx->width || c->height != avctx->height) {
721 ret = ff_set_dimensions(avctx, c->width, c->height);
725 c->compression = bytestream2_get_be32(&bc);
726 if (c->compression != 2 && c->compression != 3) {
727 av_log(avctx, AV_LOG_ERROR,
728 "Unknown compression method %d\n",
730 ret = AVERROR_PATCHWELCOME;
733 c->tile_width = bytestream2_get_be32(&bc);
734 c->tile_height = bytestream2_get_be32(&bc);
735 if (!c->tile_width || !c->tile_height ||
736 ((c->tile_width | c->tile_height) & 0xF)) {
737 av_log(avctx, AV_LOG_ERROR,
738 "Invalid tile dimensions %dx%d\n",
739 c->tile_width, c->tile_height);
740 ret = AVERROR_INVALIDDATA;
743 c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
744 c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
745 c->bpp = bytestream2_get_byte(&bc);
747 if (bytestream2_get_bytes_left(&bc) < 16 ||
748 (chunk_size - 21) < 16) {
749 av_log(avctx, AV_LOG_ERROR,
750 "Display info: missing bitmasks!\n");
751 ret = AVERROR_INVALIDDATA;
754 r_mask = bytestream2_get_be32(&bc);
755 g_mask = bytestream2_get_be32(&bc);
756 b_mask = bytestream2_get_be32(&bc);
757 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
758 av_log(avctx, AV_LOG_ERROR,
759 "Invalid or unsupported bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32"\n",
760 r_mask, g_mask, b_mask);
761 ret = AVERROR_PATCHWELCOME;
765 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
766 ret = AVERROR_PATCHWELCOME;
769 if (g2m_init_buffers(c)) {
770 ret = AVERROR(ENOMEM);
776 if (!c->tiles_x || !c->tiles_y) {
777 av_log(avctx, AV_LOG_WARNING,
778 "No display info - skipping tile\n");
781 if (chunk_size < 2) {
782 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
786 c->tile_x = bytestream2_get_byte(&bc);
787 c->tile_y = bytestream2_get_byte(&bc);
788 if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
789 av_log(avctx, AV_LOG_ERROR,
790 "Invalid tile pos %d,%d (in %dx%d grid)\n",
791 c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
795 switch (c->compression) {
797 av_log(avctx, AV_LOG_ERROR,
798 "ePIC j-b compression is not implemented yet\n");
799 return AVERROR(ENOSYS);
800 case COMPR_KEMPF_J_B:
801 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
802 buf + bytestream2_tell(&bc),
806 if (ret && c->framebuf)
807 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
808 c->tile_x, c->tile_y);
811 if (chunk_size < 5) {
812 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
816 c->cursor_x = bytestream2_get_be16(&bc);
817 c->cursor_y = bytestream2_get_be16(&bc);
820 if (chunk_size < 8) {
821 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
825 bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
827 g2m_load_cursor(avctx, c, &tbc);
833 av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
837 /* navigate to next chunk */
838 bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
843 if (c->width && c->height && c->framebuf) {
844 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
847 pic->key_frame = got_header;
848 pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
850 for (i = 0; i < avctx->height; i++)
851 memcpy(pic->data[0] + i * pic->linesize[0],
852 c->framebuf + i * c->framebuf_stride,
854 g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
856 *got_picture_ptr = 1;
869 static av_cold int g2m_decode_init(AVCodecContext *avctx)
871 G2MContext *const c = avctx->priv_data;
874 if ((ret = jpg_init(avctx, &c->jc)) != 0) {
875 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
876 jpg_free_context(&c->jc);
877 return AVERROR(ENOMEM);
880 avctx->pix_fmt = AV_PIX_FMT_RGB24;
885 static av_cold int g2m_decode_end(AVCodecContext *avctx)
887 G2MContext *const c = avctx->priv_data;
889 jpg_free_context(&c->jc);
891 av_freep(&c->kempf_buf);
892 av_freep(&c->kempf_flags);
893 av_freep(&c->synth_tile);
894 av_freep(&c->jpeg_tile);
895 av_freep(&c->cursor);
896 av_freep(&c->framebuf);
901 AVCodec ff_g2m_decoder = {
903 .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
904 .type = AVMEDIA_TYPE_VIDEO,
905 .id = AV_CODEC_ID_G2M,
906 .priv_data_size = sizeof(G2MContext),
907 .init = g2m_decode_init,
908 .close = g2m_decode_end,
909 .decode = g2m_decode_frame,
910 .capabilities = CODEC_CAP_DR1,