Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 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 |
||
28 | |||
29 | #include "libavutil/intreadwrite.h" |
||
30 | #include "avcodec.h" |
||
31 | #include "bytestream.h" |
||
32 | #include "dsputil.h" |
||
33 | #include "get_bits.h" |
||
34 | #include "internal.h" |
||
35 | #include "mjpeg.h" |
||
36 | |||
37 | enum ChunkType { |
||
38 | FRAME_INFO = 0xC8, |
||
39 | TILE_DATA, |
||
40 | CURSOR_POS, |
||
41 | CURSOR_SHAPE, |
||
42 | CHUNK_CC, |
||
43 | CHUNK_CD |
||
44 | }; |
||
45 | |||
46 | enum Compression { |
||
47 | COMPR_EPIC_J_B = 2, |
||
48 | COMPR_KEMPF_J_B, |
||
49 | }; |
||
50 | |||
51 | static const uint8_t luma_quant[64] = { |
||
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 | |||
62 | static const uint8_t chroma_quant[64] = { |
||
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 | |||
73 | typedef struct JPGContext { |
||
74 | DSPContext dsp; |
||
75 | ScanTable scantable; |
||
76 | |||
77 | VLC dc_vlc[2], ac_vlc[2]; |
||
78 | int prev_dc[3]; |
||
79 | DECLARE_ALIGNED(16, int16_t, block)[6][64]; |
||
80 | |||
81 | uint8_t *buf; |
||
82 | } JPGContext; |
||
83 | |||
84 | typedef struct G2MContext { |
||
85 | JPGContext jc; |
||
86 | int version; |
||
87 | |||
88 | int compression; |
||
89 | int width, height, bpp; |
||
90 | int tile_width, tile_height; |
||
91 | int tiles_x, tiles_y, tile_x, tile_y; |
||
92 | |||
93 | int got_header; |
||
94 | |||
95 | uint8_t *framebuf; |
||
96 | int framebuf_stride, old_width, old_height; |
||
97 | |||
98 | uint8_t *synth_tile, *jpeg_tile; |
||
99 | int tile_stride, old_tile_w, old_tile_h; |
||
100 | |||
101 | uint8_t *kempf_buf, *kempf_flags; |
||
102 | |||
103 | uint8_t *cursor; |
||
104 | int cursor_stride; |
||
105 | int cursor_fmt; |
||
106 | int cursor_w, cursor_h, cursor_x, cursor_y; |
||
107 | int cursor_hot_x, cursor_hot_y; |
||
108 | } G2MContext; |
||
109 | |||
110 | static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table, |
||
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 | |||
119 | ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); |
||
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 | |||
127 | return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, |
||
128 | huff_code, 2, 2, huff_sym, 2, 2, 0); |
||
129 | } |
||
130 | |||
131 | static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c) |
||
132 | { |
||
133 | int ret; |
||
134 | |||
135 | ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance, |
||
136 | avpriv_mjpeg_val_dc, 12, 0); |
||
137 | if (ret) |
||
138 | return ret; |
||
139 | ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance, |
||
140 | avpriv_mjpeg_val_dc, 12, 0); |
||
141 | if (ret) |
||
142 | return ret; |
||
143 | ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance, |
||
144 | avpriv_mjpeg_val_ac_luminance, 251, 1); |
||
145 | if (ret) |
||
146 | return ret; |
||
147 | ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance, |
||
148 | avpriv_mjpeg_val_ac_chrominance, 251, 1); |
||
149 | if (ret) |
||
150 | return ret; |
||
151 | |||
152 | ff_dsputil_init(&c->dsp, avctx); |
||
153 | ff_init_scantable(c->dsp.idct_permutation, &c->scantable, |
||
154 | ff_zigzag_direct); |
||
155 | |||
156 | return 0; |
||
157 | } |
||
158 | |||
159 | static av_cold void jpg_free_context(JPGContext *ctx) |
||
160 | { |
||
161 | int i; |
||
162 | |||
163 | for (i = 0; i < 2; i++) { |
||
164 | ff_free_vlc(&ctx->dc_vlc[i]); |
||
165 | ff_free_vlc(&ctx->ac_vlc[i]); |
||
166 | } |
||
167 | |||
168 | av_freep(&ctx->buf); |
||
169 | } |
||
170 | |||
171 | static void jpg_unescape(const uint8_t *src, int src_size, |
||
172 | uint8_t *dst, int *dst_size) |
||
173 | { |
||
174 | const uint8_t *src_end = src + src_size; |
||
175 | uint8_t *dst_start = dst; |
||
176 | |||
177 | while (src < src_end) { |
||
178 | uint8_t x = *src++; |
||
179 | |||
180 | *dst++ = x; |
||
181 | |||
182 | if (x == 0xFF && !*src) |
||
183 | src++; |
||
184 | } |
||
185 | *dst_size = dst - dst_start; |
||
186 | } |
||
187 | |||
188 | static int jpg_decode_block(JPGContext *c, GetBitContext *gb, |
||
189 | int plane, int16_t *block) |
||
190 | { |
||
191 | int dc, val, pos; |
||
192 | const int is_chroma = !!plane; |
||
193 | const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant; |
||
194 | |||
195 | c->dsp.clear_block(block); |
||
196 | dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3); |
||
197 | if (dc < 0) |
||
198 | return AVERROR_INVALIDDATA; |
||
199 | if (dc) |
||
200 | dc = get_xbits(gb, dc); |
||
201 | dc = dc * qmat[0] + c->prev_dc[plane]; |
||
202 | block[0] = dc; |
||
203 | c->prev_dc[plane] = dc; |
||
204 | |||
205 | pos = 0; |
||
206 | while (pos < 63) { |
||
207 | val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3); |
||
208 | if (val < 0) |
||
209 | return AVERROR_INVALIDDATA; |
||
210 | pos += val >> 4; |
||
211 | val &= 0xF; |
||
212 | if (pos > 63) |
||
213 | return val ? AVERROR_INVALIDDATA : 0; |
||
214 | if (val) { |
||
215 | int nbits = val; |
||
216 | |||
217 | val = get_xbits(gb, nbits); |
||
218 | val *= qmat[ff_zigzag_direct[pos]]; |
||
219 | block[c->scantable.permutated[pos]] = val; |
||
220 | } |
||
221 | } |
||
222 | return 0; |
||
223 | } |
||
224 | |||
225 | static inline void yuv2rgb(uint8_t *out, int Y, int U, int V) |
||
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 | |||
232 | static int jpg_decode_data(JPGContext *c, int width, int height, |
||
233 | const uint8_t *src, int src_size, |
||
234 | uint8_t *dst, int dst_stride, |
||
235 | const uint8_t *mask, int mask_stride, int num_mbs, |
||
236 | int swapuv) |
||
237 | { |
||
238 | GetBitContext gb; |
||
239 | uint8_t *tmp; |
||
240 | int mb_w, mb_h, mb_x, mb_y, i, j; |
||
241 | int bx, by; |
||
242 | int unesc_size; |
||
243 | int ret; |
||
244 | |||
245 | tmp = av_realloc(c->buf, src_size + FF_INPUT_BUFFER_PADDING_SIZE); |
||
246 | if (!tmp) |
||
247 | return AVERROR(ENOMEM); |
||
248 | c->buf = tmp; |
||
249 | jpg_unescape(src, src_size, c->buf, &unesc_size); |
||
250 | memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
||
251 | init_get_bits(&gb, c->buf, unesc_size * 8); |
||
252 | |||
253 | width = FFALIGN(width, 16); |
||
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++) |
||
261 | c->prev_dc[i] = 1024; |
||
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++) { |
||
271 | if ((ret = jpg_decode_block(c, &gb, 0, |
||
272 | c->block[i + j * 2])) != 0) |
||
273 | return ret; |
||
274 | c->dsp.idct(c->block[i + j * 2]); |
||
275 | } |
||
276 | } |
||
277 | for (i = 1; i < 3; i++) { |
||
278 | if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0) |
||
279 | return ret; |
||
280 | c->dsp.idct(c->block[i + 3]); |
||
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++) { |
||
286 | int Y, U, V; |
||
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; |
||
291 | yuv2rgb(out + i * 3, Y, U, V); |
||
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 | |||
308 | static void kempf_restore_buf(const uint8_t *src, int len, |
||
309 | uint8_t *dst, int stride, |
||
310 | const uint8_t *jpeg_tile, int tile_stride, |
||
311 | int width, int height, |
||
312 | const uint8_t *pal, int npal, int tidx) |
||
313 | { |
||
314 | GetBitContext gb; |
||
315 | int i, j, nb, col; |
||
316 | |||
317 | init_get_bits(&gb, src, len * 8); |
||
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) { |
||
325 | if (get_bits(&gb, 8)) |
||
326 | continue; |
||
327 | for (i = 0; i < width; i++) { |
||
328 | col = get_bits(&gb, nb); |
||
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 | |||
337 | static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y, |
||
338 | const uint8_t *src, int src_size) |
||
339 | { |
||
340 | int width, height; |
||
341 | int hdr, zsize, npal, tidx = -1, ret; |
||
342 | int i, j; |
||
343 | const uint8_t *src_end = src + src_size; |
||
344 | uint8_t pal[768], transp[3]; |
||
345 | uLongf dlen = (c->tile_width + 1) * c->tile_height; |
||
346 | int sub_type; |
||
347 | int nblocks, cblocks, bstride; |
||
348 | int bits, bitbuf, coded; |
||
349 | uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 + |
||
350 | tile_y * c->tile_height * c->framebuf_stride; |
||
351 | |||
352 | if (src_size < 2) |
||
353 | return AVERROR_INVALIDDATA; |
||
354 | |||
355 | width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width); |
||
356 | height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height); |
||
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; |
||
364 | for (j = 0; j < height; j++, dst += c->framebuf_stride) |
||
365 | for (i = 0; i < width; i++) |
||
366 | memcpy(dst + i * 3, transp, 3); |
||
367 | return 0; |
||
368 | } else if (sub_type == 1) { |
||
369 | return jpg_decode_data(&c->jc, width, height, src, src_end - src, |
||
370 | dst, c->framebuf_stride, NULL, 0, 0, 0); |
||
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 + (sub_type != 2)) |
||
393 | return AVERROR_INVALIDDATA; |
||
394 | |||
395 | ret = uncompress(c->kempf_buf, &dlen, src, zsize); |
||
396 | if (ret) |
||
397 | return AVERROR_INVALIDDATA; |
||
398 | src += zsize; |
||
399 | |||
400 | if (sub_type == 2) { |
||
401 | kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride, |
||
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 | if (src >= src_end) |
||
415 | return AVERROR_INVALIDDATA; |
||
416 | bitbuf = *src++; |
||
417 | bits = 8; |
||
418 | } |
||
419 | coded = bitbuf & 1; |
||
420 | bits--; |
||
421 | bitbuf >>= 1; |
||
422 | cblocks += coded; |
||
423 | if (cblocks > nblocks) |
||
424 | return AVERROR_INVALIDDATA; |
||
425 | c->kempf_flags[j + i * bstride] = coded; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | memset(c->jpeg_tile, 0, c->tile_stride * height); |
||
430 | jpg_decode_data(&c->jc, width, height, src, src_end - src, |
||
431 | c->jpeg_tile, c->tile_stride, |
||
432 | c->kempf_flags, bstride, nblocks, 0); |
||
433 | |||
434 | kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride, |
||
435 | c->jpeg_tile, c->tile_stride, |
||
436 | width, height, pal, npal, tidx); |
||
437 | |||
438 | return 0; |
||
439 | } |
||
440 | |||
441 | static int g2m_init_buffers(G2MContext *c) |
||
442 | { |
||
443 | int aligned_height; |
||
444 | |||
445 | if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) { |
||
446 | c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3; |
||
447 | aligned_height = c->height + 15; |
||
448 | av_free(c->framebuf); |
||
449 | c->framebuf = av_mallocz(c->framebuf_stride * aligned_height); |
||
450 | if (!c->framebuf) |
||
451 | return AVERROR(ENOMEM); |
||
452 | } |
||
453 | if (!c->synth_tile || !c->jpeg_tile || |
||
454 | c->old_tile_w < c->tile_width || |
||
455 | c->old_tile_h < c->tile_height) { |
||
456 | c->tile_stride = FFALIGN(c->tile_width, 16) * 3; |
||
457 | aligned_height = FFALIGN(c->tile_height, 16); |
||
458 | av_free(c->synth_tile); |
||
459 | av_free(c->jpeg_tile); |
||
460 | av_free(c->kempf_buf); |
||
461 | av_free(c->kempf_flags); |
||
462 | c->synth_tile = av_mallocz(c->tile_stride * aligned_height); |
||
463 | c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height); |
||
464 | c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height |
||
465 | + FF_INPUT_BUFFER_PADDING_SIZE); |
||
466 | c->kempf_flags = av_mallocz( c->tile_width * aligned_height); |
||
467 | if (!c->synth_tile || !c->jpeg_tile || |
||
468 | !c->kempf_buf || !c->kempf_flags) |
||
469 | return AVERROR(ENOMEM); |
||
470 | } |
||
471 | |||
472 | return 0; |
||
473 | } |
||
474 | |||
475 | static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c, |
||
476 | GetByteContext *gb) |
||
477 | { |
||
478 | int i, j, k; |
||
479 | uint8_t *dst; |
||
480 | uint32_t bits; |
||
481 | uint32_t cur_size, cursor_w, cursor_h, cursor_stride; |
||
482 | uint32_t cursor_hot_x, cursor_hot_y; |
||
483 | int cursor_fmt; |
||
484 | uint8_t *tmp; |
||
485 | |||
486 | cur_size = bytestream2_get_be32(gb); |
||
487 | cursor_w = bytestream2_get_byte(gb); |
||
488 | cursor_h = bytestream2_get_byte(gb); |
||
489 | cursor_hot_x = bytestream2_get_byte(gb); |
||
490 | cursor_hot_y = bytestream2_get_byte(gb); |
||
491 | cursor_fmt = bytestream2_get_byte(gb); |
||
492 | |||
493 | cursor_stride = FFALIGN(cursor_w, c->cursor_fmt==1 ? 32 : 1) * 4; |
||
494 | |||
495 | if (cursor_w < 1 || cursor_w > 256 || |
||
496 | cursor_h < 1 || cursor_h > 256) { |
||
497 | av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %dx%d\n", |
||
498 | cursor_w, cursor_h); |
||
499 | return AVERROR_INVALIDDATA; |
||
500 | } |
||
501 | if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) { |
||
502 | av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d,%d\n", |
||
503 | cursor_hot_x, cursor_hot_y); |
||
504 | cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1); |
||
505 | cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1); |
||
506 | } |
||
507 | if (cur_size - 9 > bytestream2_get_bytes_left(gb) || |
||
508 | c->cursor_w * c->cursor_h / 4 > cur_size) { |
||
509 | av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d/%d\n", |
||
510 | cur_size, bytestream2_get_bytes_left(gb)); |
||
511 | return AVERROR_INVALIDDATA; |
||
512 | } |
||
513 | if (cursor_fmt != 1 && cursor_fmt != 32) { |
||
514 | avpriv_report_missing_feature(avctx, "Cursor format %d", |
||
515 | cursor_fmt); |
||
516 | return AVERROR_PATCHWELCOME; |
||
517 | } |
||
518 | |||
519 | tmp = av_realloc(c->cursor, cursor_stride * cursor_h); |
||
520 | if (!tmp) { |
||
521 | av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n"); |
||
522 | return AVERROR(ENOMEM); |
||
523 | } |
||
524 | |||
525 | c->cursor = tmp; |
||
526 | c->cursor_w = cursor_w; |
||
527 | c->cursor_h = cursor_h; |
||
528 | c->cursor_hot_x = cursor_hot_x; |
||
529 | c->cursor_hot_y = cursor_hot_y; |
||
530 | c->cursor_fmt = cursor_fmt; |
||
531 | c->cursor_stride = cursor_stride; |
||
532 | |||
533 | dst = c->cursor; |
||
534 | switch (c->cursor_fmt) { |
||
535 | case 1: // old monochrome |
||
536 | for (j = 0; j < c->cursor_h; j++) { |
||
537 | for (i = 0; i < c->cursor_w; i += 32) { |
||
538 | bits = bytestream2_get_be32(gb); |
||
539 | for (k = 0; k < 32; k++) { |
||
540 | dst[0] = !!(bits & 0x80000000); |
||
541 | dst += 4; |
||
542 | bits <<= 1; |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 | |||
547 | dst = c->cursor; |
||
548 | for (j = 0; j < c->cursor_h; j++) { |
||
549 | for (i = 0; i < c->cursor_w; i += 32) { |
||
550 | bits = bytestream2_get_be32(gb); |
||
551 | for (k = 0; k < 32; k++) { |
||
552 | int mask_bit = !!(bits & 0x80000000); |
||
553 | switch (dst[0] * 2 + mask_bit) { |
||
554 | case 0: |
||
555 | dst[0] = 0xFF; dst[1] = 0x00; |
||
556 | dst[2] = 0x00; dst[3] = 0x00; |
||
557 | break; |
||
558 | case 1: |
||
559 | dst[0] = 0xFF; dst[1] = 0xFF; |
||
560 | dst[2] = 0xFF; dst[3] = 0xFF; |
||
561 | break; |
||
562 | default: |
||
563 | dst[0] = 0x00; dst[1] = 0x00; |
||
564 | dst[2] = 0x00; dst[3] = 0x00; |
||
565 | } |
||
566 | dst += 4; |
||
567 | bits <<= 1; |
||
568 | } |
||
569 | } |
||
570 | } |
||
571 | break; |
||
572 | case 32: // full colour |
||
573 | /* skip monochrome version of the cursor and decode RGBA instead */ |
||
574 | bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3)); |
||
575 | for (j = 0; j < c->cursor_h; j++) { |
||
576 | for (i = 0; i < c->cursor_w; i++) { |
||
577 | int val = bytestream2_get_be32(gb); |
||
578 | *dst++ = val >> 0; |
||
579 | *dst++ = val >> 8; |
||
580 | *dst++ = val >> 16; |
||
581 | *dst++ = val >> 24; |
||
582 | } |
||
583 | } |
||
584 | break; |
||
585 | default: |
||
586 | return AVERROR_PATCHWELCOME; |
||
587 | } |
||
588 | return 0; |
||
589 | } |
||
590 | |||
591 | #define APPLY_ALPHA(src, new, alpha) \ |
||
592 | src = (src * (256 - alpha) + new * alpha) >> 8 |
||
593 | |||
594 | static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride) |
||
595 | { |
||
596 | int i, j; |
||
597 | int x, y, w, h; |
||
598 | const uint8_t *cursor; |
||
599 | |||
600 | if (!c->cursor) |
||
601 | return; |
||
602 | |||
603 | x = c->cursor_x - c->cursor_hot_x; |
||
604 | y = c->cursor_y - c->cursor_hot_y; |
||
605 | |||
606 | cursor = c->cursor; |
||
607 | w = c->cursor_w; |
||
608 | h = c->cursor_h; |
||
609 | |||
610 | if (x + w > c->width) |
||
611 | w = c->width - x; |
||
612 | if (y + h > c->height) |
||
613 | h = c->height - y; |
||
614 | if (x < 0) { |
||
615 | w += x; |
||
616 | cursor += -x * 4; |
||
617 | } else { |
||
618 | dst += x * 3; |
||
619 | } |
||
620 | if (y < 0) { |
||
621 | h += y; |
||
622 | cursor += -y * c->cursor_stride; |
||
623 | } else { |
||
624 | dst += y * stride; |
||
625 | } |
||
626 | if (w < 0 || h < 0) |
||
627 | return; |
||
628 | |||
629 | for (j = 0; j < h; j++) { |
||
630 | for (i = 0; i < w; i++) { |
||
631 | uint8_t alpha = cursor[i * 4]; |
||
632 | APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha); |
||
633 | APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha); |
||
634 | APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha); |
||
635 | } |
||
636 | dst += stride; |
||
637 | cursor += c->cursor_stride; |
||
638 | } |
||
639 | } |
||
640 | |||
641 | static int g2m_decode_frame(AVCodecContext *avctx, void *data, |
||
642 | int *got_picture_ptr, AVPacket *avpkt) |
||
643 | { |
||
644 | const uint8_t *buf = avpkt->data; |
||
645 | int buf_size = avpkt->size; |
||
646 | G2MContext *c = avctx->priv_data; |
||
647 | AVFrame *pic = data; |
||
648 | GetByteContext bc, tbc; |
||
649 | int magic; |
||
650 | int got_header = 0; |
||
651 | uint32_t chunk_size; |
||
652 | int chunk_type; |
||
653 | int i; |
||
654 | int ret; |
||
655 | |||
656 | if (buf_size < 12) { |
||
657 | av_log(avctx, AV_LOG_ERROR, |
||
658 | "Frame should have at least 12 bytes, got %d instead\n", |
||
659 | buf_size); |
||
660 | return AVERROR_INVALIDDATA; |
||
661 | } |
||
662 | |||
663 | bytestream2_init(&bc, buf, buf_size); |
||
664 | |||
665 | magic = bytestream2_get_be32(&bc); |
||
666 | if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') || |
||
667 | (magic & 0xF) < 2 || (magic & 0xF) > 4) { |
||
668 | av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic); |
||
669 | return AVERROR_INVALIDDATA; |
||
670 | } |
||
671 | |||
672 | if ((magic & 0xF) != 4) { |
||
673 | av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n"); |
||
674 | return AVERROR(ENOSYS); |
||
675 | } |
||
676 | |||
677 | while (bytestream2_get_bytes_left(&bc) > 5) { |
||
678 | chunk_size = bytestream2_get_le32(&bc) - 1; |
||
679 | chunk_type = bytestream2_get_byte(&bc); |
||
680 | if (chunk_size > bytestream2_get_bytes_left(&bc)) { |
||
681 | av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %d type %02X\n", |
||
682 | chunk_size, chunk_type); |
||
683 | break; |
||
684 | } |
||
685 | switch (chunk_type) { |
||
686 | case FRAME_INFO: |
||
687 | c->got_header = 0; |
||
688 | if (chunk_size < 21) { |
||
689 | av_log(avctx, AV_LOG_ERROR, "Invalid frame info size %d\n", |
||
690 | chunk_size); |
||
691 | break; |
||
692 | } |
||
693 | c->width = bytestream2_get_be32(&bc); |
||
694 | c->height = bytestream2_get_be32(&bc); |
||
695 | if (c->width < 16 || c->width > avctx->width || |
||
696 | c->height < 16 || c->height > avctx->height) { |
||
697 | av_log(avctx, AV_LOG_ERROR, |
||
698 | "Invalid frame dimensions %dx%d\n", |
||
699 | c->width, c->height); |
||
700 | ret = AVERROR_INVALIDDATA; |
||
701 | goto header_fail; |
||
702 | } |
||
703 | if (c->width != avctx->width || c->height != avctx->height) |
||
704 | avcodec_set_dimensions(avctx, c->width, c->height); |
||
705 | c->compression = bytestream2_get_be32(&bc); |
||
706 | if (c->compression != 2 && c->compression != 3) { |
||
707 | av_log(avctx, AV_LOG_ERROR, |
||
708 | "Unknown compression method %d\n", |
||
709 | c->compression); |
||
710 | return AVERROR_PATCHWELCOME; |
||
711 | } |
||
712 | c->tile_width = bytestream2_get_be32(&bc); |
||
713 | c->tile_height = bytestream2_get_be32(&bc); |
||
714 | if (!c->tile_width || !c->tile_height) { |
||
715 | av_log(avctx, AV_LOG_ERROR, |
||
716 | "Invalid tile dimensions %dx%d\n", |
||
717 | c->tile_width, c->tile_height); |
||
718 | ret = AVERROR_INVALIDDATA; |
||
719 | goto header_fail; |
||
720 | } |
||
721 | c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width; |
||
722 | c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height; |
||
723 | c->bpp = bytestream2_get_byte(&bc); |
||
724 | chunk_size -= 21; |
||
725 | bytestream2_skip(&bc, chunk_size); |
||
726 | if (g2m_init_buffers(c)) { |
||
727 | ret = AVERROR(ENOMEM); |
||
728 | goto header_fail; |
||
729 | } |
||
730 | got_header = 1; |
||
731 | break; |
||
732 | case TILE_DATA: |
||
733 | if (!c->tiles_x || !c->tiles_y) { |
||
734 | av_log(avctx, AV_LOG_WARNING, |
||
735 | "No frame header - skipping tile\n"); |
||
736 | bytestream2_skip(&bc, bytestream2_get_bytes_left(&bc)); |
||
737 | break; |
||
738 | } |
||
739 | if (chunk_size < 2) { |
||
740 | av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %d\n", |
||
741 | chunk_size); |
||
742 | break; |
||
743 | } |
||
744 | c->tile_x = bytestream2_get_byte(&bc); |
||
745 | c->tile_y = bytestream2_get_byte(&bc); |
||
746 | if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) { |
||
747 | av_log(avctx, AV_LOG_ERROR, |
||
748 | "Invalid tile pos %d,%d (in %dx%d grid)\n", |
||
749 | c->tile_x, c->tile_y, c->tiles_x, c->tiles_y); |
||
750 | break; |
||
751 | } |
||
752 | chunk_size -= 2; |
||
753 | ret = 0; |
||
754 | switch (c->compression) { |
||
755 | case COMPR_EPIC_J_B: |
||
756 | av_log(avctx, AV_LOG_ERROR, |
||
757 | "ePIC j-b compression is not implemented yet\n"); |
||
758 | return AVERROR(ENOSYS); |
||
759 | case COMPR_KEMPF_J_B: |
||
760 | ret = kempf_decode_tile(c, c->tile_x, c->tile_y, |
||
761 | buf + bytestream2_tell(&bc), |
||
762 | chunk_size); |
||
763 | break; |
||
764 | } |
||
765 | if (ret && c->framebuf) |
||
766 | av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n", |
||
767 | c->tile_x, c->tile_y); |
||
768 | bytestream2_skip(&bc, chunk_size); |
||
769 | break; |
||
770 | case CURSOR_POS: |
||
771 | if (chunk_size < 5) { |
||
772 | av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %d\n", |
||
773 | chunk_size); |
||
774 | break; |
||
775 | } |
||
776 | c->cursor_x = bytestream2_get_be16(&bc); |
||
777 | c->cursor_y = bytestream2_get_be16(&bc); |
||
778 | bytestream2_skip(&bc, chunk_size - 4); |
||
779 | break; |
||
780 | case CURSOR_SHAPE: |
||
781 | if (chunk_size < 8) { |
||
782 | av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d\n", |
||
783 | chunk_size); |
||
784 | break; |
||
785 | } |
||
786 | bytestream2_init(&tbc, buf + bytestream2_tell(&bc), |
||
787 | chunk_size - 4); |
||
788 | g2m_load_cursor(avctx, c, &tbc); |
||
789 | bytestream2_skip(&bc, chunk_size); |
||
790 | break; |
||
791 | case CHUNK_CC: |
||
792 | case CHUNK_CD: |
||
793 | bytestream2_skip(&bc, chunk_size); |
||
794 | break; |
||
795 | default: |
||
796 | av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02X\n", |
||
797 | chunk_type); |
||
798 | bytestream2_skip(&bc, chunk_size); |
||
799 | } |
||
800 | } |
||
801 | if (got_header) |
||
802 | c->got_header = 1; |
||
803 | |||
804 | if (c->width && c->height && c->framebuf) { |
||
805 | if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) |
||
806 | return ret; |
||
807 | |||
808 | pic->key_frame = got_header; |
||
809 | pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; |
||
810 | |||
811 | for (i = 0; i < avctx->height; i++) |
||
812 | memcpy(pic->data[0] + i * pic->linesize[0], |
||
813 | c->framebuf + i * c->framebuf_stride, |
||
814 | c->width * 3); |
||
815 | g2m_paint_cursor(c, pic->data[0], pic->linesize[0]); |
||
816 | |||
817 | *got_picture_ptr = 1; |
||
818 | } |
||
819 | |||
820 | return buf_size; |
||
821 | header_fail: |
||
822 | c->width = c->height = 0; |
||
823 | c->tiles_x = c->tiles_y = 0; |
||
824 | return ret; |
||
825 | } |
||
826 | |||
827 | static av_cold int g2m_decode_init(AVCodecContext *avctx) |
||
828 | { |
||
829 | G2MContext * const c = avctx->priv_data; |
||
830 | int ret; |
||
831 | |||
832 | if ((ret = jpg_init(avctx, &c->jc)) != 0) { |
||
833 | av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n"); |
||
834 | jpg_free_context(&c->jc); |
||
835 | return AVERROR(ENOMEM); |
||
836 | } |
||
837 | |||
838 | avctx->pix_fmt = AV_PIX_FMT_RGB24; |
||
839 | |||
840 | return 0; |
||
841 | } |
||
842 | |||
843 | static av_cold int g2m_decode_end(AVCodecContext *avctx) |
||
844 | { |
||
845 | G2MContext * const c = avctx->priv_data; |
||
846 | |||
847 | jpg_free_context(&c->jc); |
||
848 | |||
849 | av_freep(&c->kempf_buf); |
||
850 | av_freep(&c->kempf_flags); |
||
851 | av_freep(&c->synth_tile); |
||
852 | av_freep(&c->jpeg_tile); |
||
853 | av_freep(&c->cursor); |
||
854 | av_freep(&c->framebuf); |
||
855 | |||
856 | return 0; |
||
857 | } |
||
858 | |||
859 | AVCodec ff_g2m_decoder = { |
||
860 | .name = "g2m", |
||
861 | .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"), |
||
862 | .type = AVMEDIA_TYPE_VIDEO, |
||
863 | .id = AV_CODEC_ID_G2M, |
||
864 | .priv_data_size = sizeof(G2MContext), |
||
865 | .init = g2m_decode_init, |
||
866 | .close = g2m_decode_end, |
||
867 | .decode = g2m_decode_frame, |
||
868 | .capabilities = CODEC_CAP_DR1, |
||
869 | };>>>>>>>>>>>>>>>>>>=><=>>>>=><=>>>>>>>>>>>>>><>>>>>>>>=>=>=>>>>>>>>>>>>>>> |