2 * Go2Webinar / Go2Meeting decoder
3 * Copyright (c) 2012 Konstantin Shishkov
4 * Copyright (c) 2013 Maxim Poliakovski
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Go2Webinar / Go2Meeting decoder
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
36 #include "bytestream.h"
41 #include "jpegtables.h"
44 #define EPIC_PIX_STACK_SIZE 1024
45 #define EPIC_PIX_STACK_MAX (EPIC_PIX_STACK_SIZE - 1)
61 static const uint8_t luma_quant[64] = {
62 8, 6, 5, 8, 12, 20, 26, 31,
63 6, 6, 7, 10, 13, 29, 30, 28,
64 7, 7, 8, 12, 20, 29, 35, 28,
65 7, 9, 11, 15, 26, 44, 40, 31,
66 9, 11, 19, 28, 34, 55, 52, 39,
67 12, 18, 28, 32, 41, 52, 57, 46,
68 25, 32, 39, 44, 52, 61, 60, 51,
69 36, 46, 48, 49, 56, 50, 52, 50
72 static const uint8_t chroma_quant[64] = {
73 9, 9, 12, 24, 50, 50, 50, 50,
74 9, 11, 13, 33, 50, 50, 50, 50,
75 12, 13, 28, 50, 50, 50, 50, 50,
76 24, 33, 50, 50, 50, 50, 50, 50,
77 50, 50, 50, 50, 50, 50, 50, 50,
78 50, 50, 50, 50, 50, 50, 50, 50,
79 50, 50, 50, 50, 50, 50, 50, 50,
80 50, 50, 50, 50, 50, 50, 50, 50,
83 typedef struct ePICPixListElem {
84 struct ePICPixListElem *next;
89 typedef struct ePICPixHashElem {
91 struct ePICPixListElem *list;
94 #define EPIC_HASH_SIZE 256
95 typedef struct ePICPixHash {
96 ePICPixHashElem *bucket[EPIC_HASH_SIZE];
97 int bucket_size[EPIC_HASH_SIZE];
98 int bucket_fill[EPIC_HASH_SIZE];
101 typedef struct ePICContext {
104 ElsUnsignedRung unsigned_rung;
107 uint8_t W_ctx_rung[256];
108 uint8_t N_ctx_rung[512];
109 uint8_t nw_pred_rung[256];
110 uint8_t ne_pred_rung[256];
111 uint8_t prev_row_rung[14];
112 uint8_t runlen_zeroes[14];
115 uint32_t stack[EPIC_PIX_STACK_SIZE];
119 typedef struct JPGContext {
120 BlockDSPContext bdsp;
124 VLC dc_vlc[2], ac_vlc[2];
126 DECLARE_ALIGNED(32, int16_t, block)[6][64];
131 typedef struct G2MContext {
138 int width, height, bpp;
139 int orig_width, orig_height;
140 int tile_width, tile_height;
141 int tiles_x, tiles_y, tile_x, tile_y;
146 int framebuf_stride, old_width, old_height;
148 uint8_t *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
149 int tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
152 uint8_t *kempf_buf, *kempf_flags;
157 int cursor_w, cursor_h, cursor_x, cursor_y;
158 int cursor_hot_x, cursor_hot_y;
161 static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
162 const uint8_t *val_table, int nb_codes,
165 uint8_t huff_size[256] = { 0 };
166 uint16_t huff_code[256];
167 uint16_t huff_sym[256];
170 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
172 for (i = 0; i < 256; i++)
173 huff_sym[i] = i + 16 * is_ac;
176 huff_sym[0] = 16 * 256;
178 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
179 huff_code, 2, 2, huff_sym, 2, 2, 0);
182 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
186 ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
187 avpriv_mjpeg_val_dc, 12, 0);
190 ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
191 avpriv_mjpeg_val_dc, 12, 0);
194 ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
195 avpriv_mjpeg_val_ac_luminance, 251, 1);
198 ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
199 avpriv_mjpeg_val_ac_chrominance, 251, 1);
203 ff_blockdsp_init(&c->bdsp, avctx);
204 ff_idctdsp_init(&c->idsp, avctx);
205 ff_init_scantable(c->idsp.idct_permutation, &c->scantable,
211 static av_cold void jpg_free_context(JPGContext *ctx)
215 for (i = 0; i < 2; i++) {
216 ff_free_vlc(&ctx->dc_vlc[i]);
217 ff_free_vlc(&ctx->ac_vlc[i]);
223 static void jpg_unescape(const uint8_t *src, int src_size,
224 uint8_t *dst, int *dst_size)
226 const uint8_t *src_end = src + src_size;
227 uint8_t *dst_start = dst;
229 while (src < src_end) {
234 if (x == 0xFF && !*src)
237 *dst_size = dst - dst_start;
240 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
241 int plane, int16_t *block)
244 const int is_chroma = !!plane;
245 const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
247 if (get_bits_left(gb) < 1)
248 return AVERROR_INVALIDDATA;
250 c->bdsp.clear_block(block);
251 dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
253 return AVERROR_INVALIDDATA;
255 dc = get_xbits(gb, dc);
256 dc = dc * qmat[0] + c->prev_dc[plane];
258 c->prev_dc[plane] = dc;
262 val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
264 return AVERROR_INVALIDDATA;
268 return val ? AVERROR_INVALIDDATA : 0;
272 val = get_xbits(gb, nbits);
273 val *= qmat[ff_zigzag_direct[pos]];
274 block[c->scantable.permutated[pos]] = val;
280 static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
282 out[ridx] = av_clip_uint8(Y + (91881 * V + 32768 >> 16));
283 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
284 out[2 - ridx] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
287 static int jpg_decode_data(JPGContext *c, int width, int height,
288 const uint8_t *src, int src_size,
289 uint8_t *dst, int dst_stride,
290 const uint8_t *mask, int mask_stride, int num_mbs,
294 int mb_w, mb_h, mb_x, mb_y, i, j;
298 const int ridx = swapuv ? 2 : 0;
300 if ((ret = av_reallocp(&c->buf,
301 src_size + AV_INPUT_BUFFER_PADDING_SIZE)) < 0)
303 jpg_unescape(src, src_size, c->buf, &unesc_size);
304 memset(c->buf + unesc_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
305 if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
308 width = FFALIGN(width, 16);
310 mb_h = (height + 15) >> 4;
313 num_mbs = mb_w * mb_h * 4;
315 for (i = 0; i < 3; i++)
316 c->prev_dc[i] = 1024;
319 c->bdsp.clear_blocks(c->block[0]);
320 for (mb_y = 0; mb_y < mb_h; mb_y++) {
321 for (mb_x = 0; mb_x < mb_w; mb_x++) {
322 if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
323 !mask[mb_x * 2 + mask_stride] &&
324 !mask[mb_x * 2 + 1 + mask_stride]) {
328 for (j = 0; j < 2; j++) {
329 for (i = 0; i < 2; i++) {
330 if (mask && !mask[mb_x * 2 + i + j * mask_stride])
333 if ((ret = jpg_decode_block(c, &gb, 0,
334 c->block[i + j * 2])) != 0)
336 c->idsp.idct(c->block[i + j * 2]);
339 for (i = 1; i < 3; i++) {
340 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
342 c->idsp.idct(c->block[i + 3]);
345 for (j = 0; j < 16; j++) {
346 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
347 for (i = 0; i < 16; i++) {
350 Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
351 U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
352 V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
353 yuv2rgb(out + i * 3, ridx, Y, U, V);
364 mask += mask_stride * 2;
370 #define LOAD_NEIGHBOURS(x) \
371 W = curr_row[(x) - 1]; \
372 N = above_row[(x)]; \
373 WW = curr_row[(x) - 2]; \
374 NW = above_row[(x) - 1]; \
375 NE = above_row[(x) + 1]; \
376 NN = above2_row[(x)]; \
377 NNW = above2_row[(x) - 1]; \
378 NWW = above_row[(x) - 2]; \
379 NNE = above2_row[(x) + 1]
381 #define UPDATE_NEIGHBOURS(x) \
387 NE = above_row[(x) + 1]; \
388 NNE = above2_row[(x) + 1]
394 /* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
395 static int djb2_hash(uint32_t key)
399 h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
400 h = (h * 33) ^ ((key >> 16) & 0xFF);
401 h = (h * 33) ^ ((key >> 8) & 0xFF);
402 h = (h * 33) ^ (key & 0xFF);
404 return h & (EPIC_HASH_SIZE - 1);
407 static void epic_hash_init(ePICPixHash *hash)
409 memset(hash, 0, sizeof(*hash));
412 static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
414 int i, idx = djb2_hash(key);
415 ePICPixHashElem *bucket = hash->bucket[idx];
417 for (i = 0; i < hash->bucket_fill[idx]; i++)
418 if (bucket[i].pix_id == key)
424 static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
426 ePICPixHashElem *bucket, *ret;
427 int idx = djb2_hash(key);
429 if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
432 if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
433 int new_size = hash->bucket_size[idx] + 16;
434 bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
437 hash->bucket[idx] = bucket;
438 hash->bucket_size[idx] = new_size;
441 ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
442 memset(ret, 0, sizeof(*ret));
447 static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
449 ePICPixListElem *new_elem;
450 ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
453 if (!(hash_elem = epic_hash_add(hash, key)))
454 return AVERROR(ENOMEM);
457 new_elem = av_mallocz(sizeof(*new_elem));
459 return AVERROR(ENOMEM);
461 new_elem->pixel = pix;
462 new_elem->next = hash_elem->list;
463 hash_elem->list = new_elem;
468 static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
471 ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
473 if (hash_elem != NULL && hash_elem->list != NULL)
479 static void epic_free_pixel_cache(ePICPixHash *hash)
483 for (i = 0; i < EPIC_HASH_SIZE; i++) {
484 for (j = 0; j < hash->bucket_fill[i]; j++) {
485 ePICPixListElem *list_elem = hash->bucket[i][j].list;
487 ePICPixListElem *tmp = list_elem->next;
492 av_freep(&hash->bucket[i]);
493 hash->bucket_size[i] =
494 hash->bucket_fill[i] = 0;
498 static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
502 for (i = 0; i < dc->stack_pos; i++)
503 if (dc->stack[i] == pix)
506 return i != dc->stack_pos;
509 #define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
511 static inline int epic_decode_component_pred(ePICContext *dc,
512 int N, int W, int NW)
514 unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
515 return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
518 static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
519 const uint32_t *curr_row,
520 const uint32_t *above_row)
522 uint32_t N, W, NW, pred;
524 int GN, GW, GNW, R, G, B;
529 NW = above_row[x - 1];
531 GN = (N >> G_shift) & 0xFF;
532 GW = (W >> G_shift) & 0xFF;
533 GNW = (NW >> G_shift) & 0xFF;
535 G = epic_decode_component_pred(dc, GN, GW, GNW);
537 R = G + epic_decode_component_pred(dc,
538 ((N >> R_shift) & 0xFF) - GN,
539 ((W >> R_shift) & 0xFF) - GW,
540 ((NW >> R_shift) & 0xFF) - GNW);
542 B = G + epic_decode_component_pred(dc,
543 ((N >> B_shift) & 0xFF) - GN,
544 ((W >> B_shift) & 0xFF) - GW,
545 ((NW >> B_shift) & 0xFF) - GNW);
548 pred = curr_row[x - 1];
552 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
553 R = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
555 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
556 G = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
558 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
559 B = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
562 if (R<0 || G<0 || B<0 || R > 255 || G > 255 || B > 255) {
563 avpriv_request_sample(NULL, "RGB %d %d %d is out of range\n", R, G, B);
567 return (R << R_shift) | (G << G_shift) | (B << B_shift);
570 static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
571 uint32_t *pPix, uint32_t pix)
573 if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
577 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
581 static int epic_handle_edges(ePICContext *dc, int x, int y,
582 const uint32_t *curr_row,
583 const uint32_t *above_row, uint32_t *pPix)
587 if (!x && !y) { /* special case: top-left pixel */
588 /* the top-left pixel is coded independently with 3 unsigned numbers */
589 *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
590 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
591 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
595 if (x) { /* predict from W first */
596 pix = curr_row[x - 1];
597 if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
601 if (y) { /* then try to predict from N */
603 if (!dc->stack_pos || dc->stack[0] != pix) {
604 if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
612 static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
613 const uint32_t *curr_row,
614 const uint32_t *above_row,
615 const uint32_t *above2_row,
616 uint32_t *pPix, int *pRun)
618 int idx, got_pixel = 0, WWneW, old_WWneW = 0;
619 uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
625 if (dc->next_run_pos == x) {
626 /* can't reuse W for the new pixel in this case */
629 idx = (WW != W) << 7 |
637 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
643 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
651 if (got_pixel) // pixel value already known (derived from either W or N)
653 else { // pixel value is unknown and will be decoded later
654 NWneW = *pRun ? NWneW : NW != W;
656 /* TODO: RFC this mess! */
657 switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
659 break; // do nothing here
664 if (!is_pixel_on_stack(dc, N)) {
666 (*pRun ? old_WWneW : WW != W) << 7 |
674 if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
685 if (!is_pixel_on_stack(dc, N))
686 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
691 if (x + *pRun >= tile_width - 1)
694 UPDATE_NEIGHBOURS(x + *pRun);
696 if (!NWneW && NW == N && N == NE) {
698 int start_pos = x + *pRun;
700 /* scan for a run of pix in the line above */
701 uint32_t pix = above_row[start_pos + 1];
702 for (pos = start_pos + 2; pos < tile_width; pos++)
703 if (!(above_row[pos] == pix))
705 run = pos - start_pos - 1;
706 idx = av_ceil_log2(run);
707 if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
711 /* run-length is coded as plain binary number of idx - 1 bits */
712 for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
713 if ((1 << pos) + rle < run &&
714 ff_els_decode_bit(&dc->els_ctx,
715 flag ? &dc->runlen_one
716 : &dc->runlen_zeroes[pos])) {
722 break; // return immediately
724 if (x + *pRun >= tile_width - 1)
727 LOAD_NEIGHBOURS(x + *pRun);
740 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
743 dc->next_run_pos = x + *pRun;
747 static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
748 uint32_t *pPix, uint32_t pix)
750 if (ff_els_decode_bit(&dc->els_ctx, rung)) {
754 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
758 static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
759 int tile_width, const uint32_t *curr_row,
760 const uint32_t *above_row, uint32_t *pPix)
764 /* try to reuse the NW pixel first */
766 uint32_t NW = above_row[x - 1];
767 if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
768 if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
773 /* try to reuse the NE[x + run, y] pixel */
775 if (pos < tile_width - 1 && y) {
776 uint32_t NE = above_row[pos + 1];
777 if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
778 if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
786 static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
788 ePICPixListElem *list, *prev = NULL;
789 ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
791 if (!hash_elem || !hash_elem->list)
794 list = hash_elem->list;
796 if (!is_pixel_on_stack(dc, list->pixel)) {
797 if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
799 if (list != hash_elem->list) {
800 prev->next = list->next;
801 list->next = hash_elem->list;
802 hash_elem->list = list;
806 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
815 static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
816 int tile_width, int stride)
820 uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
822 for (y = 0; y < tile_height; y++, out += stride) {
823 above2_row = above_row;
824 above_row = curr_row;
825 curr_row = (uint32_t *) out;
827 for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
829 return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
831 pix = curr_row[x - 1]; // get W pixel
833 if (y >= 1 && x >= 2 &&
834 pix != curr_row[x - 2] && pix != above_row[x - 1] &&
835 pix != above_row[x - 2] && pix != above_row[x] &&
836 !epic_cache_entries_for_pixel(&dc->hash, pix)) {
837 curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
841 dc->stack_pos = 0; // empty stack
843 if (y < 2 || x < 2 || x == tile_width - 1) {
845 got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
847 got_pixel = epic_decode_run_length(dc, x, y, tile_width,
849 above2_row, &pix, &run);
854 if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
855 tile_width, curr_row,
857 uint32_t ref_pix = curr_row[x - 1];
858 if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
859 pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
860 if (is_pixel_on_stack(dc, pix))
861 return AVERROR_INVALIDDATA;
864 int ret = epic_add_pixel_to_cache(&dc->hash,
872 for (; run > 0; x++, run--)
881 static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
882 const uint8_t *src, size_t src_size,
883 AVCodecContext *avctx)
885 uint8_t prefix, mask = 0x80;
886 int extrabytes, tile_width, tile_height, awidth, aheight;
893 /* get data size of the ELS partition as unsigned variable-length integer */
896 for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
898 if (extrabytes > 3 || src_size < extrabytes) {
899 av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
900 return AVERROR_INVALIDDATA;
903 els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
904 while (extrabytes-- > 0) {
905 els_dsize = (els_dsize << 8) | *src++;
909 if (src_size < els_dsize) {
910 av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %"SIZE_SPECIFIER", got %"SIZE_SPECIFIER"\n",
911 els_dsize, src_size);
912 return AVERROR_INVALIDDATA;
915 tile_width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
916 tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
917 awidth = FFALIGN(tile_width, 16);
918 aheight = FFALIGN(tile_height, 16);
920 if (tile_width > (1 << FF_ARRAY_ELEMS(c->ec.prev_row_rung))) {
921 avpriv_request_sample(avctx, "large tile width");
922 return AVERROR_INVALIDDATA;
927 uint8_t tr_r, tr_g, tr_b, *buf;
929 /* ELS decoder initializations */
930 memset(&c->ec, 0, sizeof(c->ec));
931 ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
932 epic_hash_init(&c->ec.hash);
934 /* decode transparent pixel value */
935 tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
936 tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
937 tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
938 if (c->ec.els_ctx.err != 0) {
939 av_log(avctx, AV_LOG_ERROR,
940 "ePIC: couldn't decode transparency pixel!\n");
941 ff_els_decoder_uninit(&c->ec.unsigned_rung);
942 return AVERROR_INVALIDDATA;
945 ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
948 epic_free_pixel_cache(&c->ec.hash);
949 ff_els_decoder_uninit(&c->ec.unsigned_rung);
952 av_log(avctx, AV_LOG_ERROR,
953 "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
954 avctx->frame_number, tile_x, tile_y);
955 return AVERROR_INVALIDDATA;
959 dst = c->framebuf + tile_x * c->tile_width * 3 +
960 tile_y * c->tile_height * c->framebuf_stride;
962 for (j = 0; j < tile_height; j++) {
964 in = (uint32_t *) buf;
965 for (i = 0; i < tile_width; i++) {
966 out[0] = (in[i] >> R_shift) & 0xFF;
967 out[1] = (in[i] >> G_shift) & 0xFF;
968 out[2] = (in[i] >> B_shift) & 0xFF;
971 buf += c->epic_buf_stride;
972 dst += c->framebuf_stride;
975 if (src_size > els_dsize) {
978 int bstride = FFALIGN(tile_width, 16) >> 3;
980 int estride = c->epic_buf_stride >> 2;
983 src_size -= els_dsize;
985 in = (uint32_t *) c->epic_buf;
986 tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
988 memset(c->kempf_flags, 0,
989 (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
990 for (j = 0; j < tile_height; j += 8) {
991 for (i = 0; i < tile_width; i += 8) {
992 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
993 for (k = 0; k < 8 * 8; k++) {
994 if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
995 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
1004 memset(c->jpeg_tile, 0, c->tile_stride * aheight);
1005 jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
1006 c->jpeg_tile, c->tile_stride,
1007 c->kempf_flags, bstride, nblocks, c->swapuv);
1009 in = (uint32_t *) c->epic_buf;
1010 dst = c->framebuf + tile_x * c->tile_width * 3 +
1011 tile_y * c->tile_height * c->framebuf_stride;
1013 for (j = 0; j < tile_height; j++) {
1014 for (i = 0; i < tile_width; i++)
1016 memcpy(dst + i * 3, jpg + i * 3, 3);
1017 in += c->epic_buf_stride >> 2;
1018 dst += c->framebuf_stride;
1019 jpg += c->tile_stride;
1023 dst = c->framebuf + tile_x * c->tile_width * 3 +
1024 tile_y * c->tile_height * c->framebuf_stride;
1025 return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
1026 dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
1032 static int kempf_restore_buf(const uint8_t *src, int len,
1033 uint8_t *dst, int stride,
1034 const uint8_t *jpeg_tile, int tile_stride,
1035 int width, int height,
1036 const uint8_t *pal, int npal, int tidx)
1041 int align_width = FFALIGN(width, 16);
1043 if ((ret = init_get_bits8(&gb, src, len)) < 0)
1046 if (npal <= 2) nb = 1;
1047 else if (npal <= 4) nb = 2;
1048 else if (npal <= 16) nb = 4;
1051 for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
1052 if (get_bits(&gb, 8))
1054 for (i = 0; i < width; i++) {
1055 col = get_bits(&gb, nb);
1057 memcpy(dst + i * 3, pal + col * 3, 3);
1059 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
1061 skip_bits_long(&gb, nb * (align_width - width));
1067 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
1068 const uint8_t *src, int src_size)
1071 int hdr, zsize, npal, tidx = -1, ret;
1073 const uint8_t *src_end = src + src_size;
1074 uint8_t pal[768], transp[3];
1075 uLongf dlen = (c->tile_width + 1) * c->tile_height;
1077 int nblocks, cblocks, bstride;
1078 int bits, bitbuf, coded;
1079 uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
1080 tile_y * c->tile_height * c->framebuf_stride;
1083 return AVERROR_INVALIDDATA;
1085 width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
1086 height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
1089 sub_type = hdr >> 5;
1090 if (sub_type == 0) {
1092 memcpy(transp, src, 3);
1094 for (j = 0; j < height; j++, dst += c->framebuf_stride)
1095 for (i = 0; i < width; i++)
1096 memcpy(dst + i * 3, transp, 3);
1098 } else if (sub_type == 1) {
1099 return jpg_decode_data(&c->jc, width, height, src, src_end - src,
1100 dst, c->framebuf_stride, NULL, 0, 0, 0);
1103 if (sub_type != 2) {
1104 memcpy(transp, src, 3);
1108 if (src_end - src < npal * 3)
1109 return AVERROR_INVALIDDATA;
1110 memcpy(pal, src, npal * 3);
1112 if (sub_type != 2) {
1113 for (i = 0; i < npal; i++) {
1114 if (!memcmp(pal + i * 3, transp, 3)) {
1121 if (src_end - src < 2)
1123 zsize = (src[0] << 8) | src[1];
1126 if (src_end - src < zsize + (sub_type != 2))
1127 return AVERROR_INVALIDDATA;
1129 ret = uncompress(c->kempf_buf, &dlen, src, zsize);
1131 return AVERROR_INVALIDDATA;
1134 if (sub_type == 2) {
1135 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1136 NULL, 0, width, height, pal, npal, tidx);
1140 nblocks = *src++ + 1;
1142 bstride = FFALIGN(width, 16) >> 3;
1143 // blocks are coded LSB and we need normal bitreader for JPEG data
1145 for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
1146 for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
1149 return AVERROR_INVALIDDATA;
1157 if (cblocks > nblocks)
1158 return AVERROR_INVALIDDATA;
1159 c->kempf_flags[j * 2 + i * 2 * bstride] =
1160 c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
1161 c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
1162 c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
1166 memset(c->jpeg_tile, 0, c->tile_stride * height);
1167 jpg_decode_data(&c->jc, width, height, src, src_end - src,
1168 c->jpeg_tile, c->tile_stride,
1169 c->kempf_flags, bstride, nblocks * 4, 0);
1171 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1172 c->jpeg_tile, c->tile_stride,
1173 width, height, pal, npal, tidx);
1178 static int g2m_init_buffers(G2MContext *c)
1182 if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
1183 c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
1184 aligned_height = c->height + 15;
1185 av_free(c->framebuf);
1186 c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
1188 return AVERROR(ENOMEM);
1190 if (!c->synth_tile || !c->jpeg_tile ||
1191 (c->compression == 2 && !c->epic_buf_base) ||
1192 c->old_tile_w < c->tile_width ||
1193 c->old_tile_h < c->tile_height) {
1194 c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
1195 c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
1196 aligned_height = FFALIGN(c->tile_height, 16);
1197 av_freep(&c->synth_tile);
1198 av_freep(&c->jpeg_tile);
1199 av_freep(&c->kempf_buf);
1200 av_freep(&c->kempf_flags);
1201 av_freep(&c->epic_buf_base);
1203 c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
1204 c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
1205 c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height +
1206 AV_INPUT_BUFFER_PADDING_SIZE);
1207 c->kempf_flags = av_mallocz(c->tile_width * aligned_height);
1208 if (!c->synth_tile || !c->jpeg_tile ||
1209 !c->kempf_buf || !c->kempf_flags)
1210 return AVERROR(ENOMEM);
1211 if (c->compression == 2) {
1212 c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
1213 if (!c->epic_buf_base)
1214 return AVERROR(ENOMEM);
1215 c->epic_buf = c->epic_buf_base + 4;
1222 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
1228 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
1229 uint32_t cursor_hot_x, cursor_hot_y;
1230 int cursor_fmt, err;
1232 cur_size = bytestream2_get_be32(gb);
1233 cursor_w = bytestream2_get_byte(gb);
1234 cursor_h = bytestream2_get_byte(gb);
1235 cursor_hot_x = bytestream2_get_byte(gb);
1236 cursor_hot_y = bytestream2_get_byte(gb);
1237 cursor_fmt = bytestream2_get_byte(gb);
1239 cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
1241 if (cursor_w < 1 || cursor_w > 256 ||
1242 cursor_h < 1 || cursor_h > 256) {
1243 av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
1244 cursor_w, cursor_h);
1245 return AVERROR_INVALIDDATA;
1247 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
1248 av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
1249 cursor_hot_x, cursor_hot_y);
1250 cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
1251 cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
1253 if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
1254 c->cursor_w * c->cursor_h / 4 > cur_size) {
1255 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
1256 cur_size, bytestream2_get_bytes_left(gb));
1257 return AVERROR_INVALIDDATA;
1259 if (cursor_fmt != 1 && cursor_fmt != 32) {
1260 avpriv_report_missing_feature(avctx, "Cursor format %d",
1262 return AVERROR_PATCHWELCOME;
1265 if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
1266 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
1270 c->cursor_w = cursor_w;
1271 c->cursor_h = cursor_h;
1272 c->cursor_hot_x = cursor_hot_x;
1273 c->cursor_hot_y = cursor_hot_y;
1274 c->cursor_fmt = cursor_fmt;
1275 c->cursor_stride = cursor_stride;
1278 switch (c->cursor_fmt) {
1279 case 1: // old monochrome
1280 for (j = 0; j < c->cursor_h; j++) {
1281 for (i = 0; i < c->cursor_w; i += 32) {
1282 bits = bytestream2_get_be32(gb);
1283 for (k = 0; k < 32; k++) {
1284 dst[0] = !!(bits & 0x80000000);
1292 for (j = 0; j < c->cursor_h; j++) {
1293 for (i = 0; i < c->cursor_w; i += 32) {
1294 bits = bytestream2_get_be32(gb);
1295 for (k = 0; k < 32; k++) {
1296 int mask_bit = !!(bits & 0x80000000);
1297 switch (dst[0] * 2 + mask_bit) {
1322 case 32: // full colour
1323 /* skip monochrome version of the cursor and decode RGBA instead */
1324 bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
1325 for (j = 0; j < c->cursor_h; j++) {
1326 for (i = 0; i < c->cursor_w; i++) {
1327 int val = bytestream2_get_be32(gb);
1336 return AVERROR_PATCHWELCOME;
1341 #define APPLY_ALPHA(src, new, alpha) \
1342 src = (src * (256 - alpha) + new * alpha) >> 8
1344 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
1348 const uint8_t *cursor;
1353 x = c->cursor_x - c->cursor_hot_x;
1354 y = c->cursor_y - c->cursor_hot_y;
1360 if (x + w > c->width)
1362 if (y + h > c->height)
1376 cursor += -y * c->cursor_stride;
1381 for (j = 0; j < h; j++) {
1382 for (i = 0; i < w; i++) {
1383 uint8_t alpha = cursor[i * 4];
1384 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
1385 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
1386 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
1389 cursor += c->cursor_stride;
1393 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
1394 int *got_picture_ptr, AVPacket *avpkt)
1396 const uint8_t *buf = avpkt->data;
1397 int buf_size = avpkt->size;
1398 G2MContext *c = avctx->priv_data;
1399 AVFrame *pic = data;
1400 GetByteContext bc, tbc;
1403 uint32_t chunk_size, r_mask, g_mask, b_mask;
1404 int chunk_type, chunk_start;
1408 if (buf_size < 12) {
1409 av_log(avctx, AV_LOG_ERROR,
1410 "Frame should have at least 12 bytes, got %d instead\n",
1412 return AVERROR_INVALIDDATA;
1415 bytestream2_init(&bc, buf, buf_size);
1417 magic = bytestream2_get_be32(&bc);
1418 if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
1419 (magic & 0xF) < 2 || (magic & 0xF) > 5) {
1420 av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
1421 return AVERROR_INVALIDDATA;
1424 c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
1426 while (bytestream2_get_bytes_left(&bc) > 5) {
1427 chunk_size = bytestream2_get_le32(&bc) - 1;
1428 chunk_type = bytestream2_get_byte(&bc);
1429 chunk_start = bytestream2_tell(&bc);
1430 if (chunk_size > bytestream2_get_bytes_left(&bc)) {
1431 av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
1432 chunk_size, chunk_type);
1435 switch (chunk_type) {
1439 if (chunk_size < 21) {
1440 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
1444 c->width = bytestream2_get_be32(&bc);
1445 c->height = bytestream2_get_be32(&bc);
1446 if (c->width < 16 || c->height < 16) {
1447 av_log(avctx, AV_LOG_ERROR,
1448 "Invalid frame dimensions %dx%d\n",
1449 c->width, c->height);
1450 ret = AVERROR_INVALIDDATA;
1453 if (c->width != avctx->width || c->height != avctx->height) {
1454 ret = ff_set_dimensions(avctx, c->width, c->height);
1458 c->compression = bytestream2_get_be32(&bc);
1459 if (c->compression != 2 && c->compression != 3) {
1460 avpriv_report_missing_feature(avctx, "Compression method %d",
1462 ret = AVERROR_PATCHWELCOME;
1465 c->tile_width = bytestream2_get_be32(&bc);
1466 c->tile_height = bytestream2_get_be32(&bc);
1467 if (c->tile_width <= 0 || c->tile_height <= 0 ||
1468 ((c->tile_width | c->tile_height) & 0xF) ||
1469 c->tile_width * (uint64_t)c->tile_height >= INT_MAX / 4 ||
1470 av_image_check_size2(c->tile_width, c->tile_height, avctx->max_pixels, avctx->pix_fmt, 0, avctx) < 0
1472 av_log(avctx, AV_LOG_ERROR,
1473 "Invalid tile dimensions %dx%d\n",
1474 c->tile_width, c->tile_height);
1475 ret = AVERROR_INVALIDDATA;
1478 c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
1479 c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
1480 c->bpp = bytestream2_get_byte(&bc);
1482 if (bytestream2_get_bytes_left(&bc) < 16 ||
1483 (chunk_size - 21) < 16) {
1484 av_log(avctx, AV_LOG_ERROR,
1485 "Display info: missing bitmasks!\n");
1486 ret = AVERROR_INVALIDDATA;
1489 r_mask = bytestream2_get_be32(&bc);
1490 g_mask = bytestream2_get_be32(&bc);
1491 b_mask = bytestream2_get_be32(&bc);
1492 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
1493 avpriv_report_missing_feature(avctx,
1494 "Bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32,
1495 r_mask, g_mask, b_mask);
1496 ret = AVERROR_PATCHWELCOME;
1500 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
1501 ret = AVERROR_PATCHWELCOME;
1504 if (g2m_init_buffers(c)) {
1505 ret = AVERROR(ENOMEM);
1511 if (!c->tiles_x || !c->tiles_y) {
1512 av_log(avctx, AV_LOG_WARNING,
1513 "No display info - skipping tile\n");
1516 if (chunk_size < 2) {
1517 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
1521 c->tile_x = bytestream2_get_byte(&bc);
1522 c->tile_y = bytestream2_get_byte(&bc);
1523 if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
1524 av_log(avctx, AV_LOG_ERROR,
1525 "Invalid tile pos %d,%d (in %dx%d grid)\n",
1526 c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
1530 switch (c->compression) {
1531 case COMPR_EPIC_J_B:
1532 ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
1533 buf + bytestream2_tell(&bc),
1534 chunk_size - 2, avctx);
1536 case COMPR_KEMPF_J_B:
1537 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
1538 buf + bytestream2_tell(&bc),
1542 if (ret && c->framebuf)
1543 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
1544 c->tile_x, c->tile_y);
1547 if (chunk_size < 5) {
1548 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
1552 c->cursor_x = bytestream2_get_be16(&bc);
1553 c->cursor_y = bytestream2_get_be16(&bc);
1556 if (chunk_size < 8) {
1557 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
1561 bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
1563 g2m_load_cursor(avctx, c, &tbc);
1569 av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
1573 /* navigate to next chunk */
1574 bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
1579 if (c->width && c->height && c->framebuf) {
1580 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
1583 pic->key_frame = got_header;
1584 pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1586 for (i = 0; i < avctx->height; i++)
1587 memcpy(pic->data[0] + i * pic->linesize[0],
1588 c->framebuf + i * c->framebuf_stride,
1590 g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
1592 *got_picture_ptr = 1;
1607 static av_cold int g2m_decode_init(AVCodecContext *avctx)
1609 G2MContext *const c = avctx->priv_data;
1612 if ((ret = jpg_init(avctx, &c->jc)) != 0) {
1613 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
1614 jpg_free_context(&c->jc);
1615 return AVERROR(ENOMEM);
1618 avctx->pix_fmt = AV_PIX_FMT_RGB24;
1620 // store original sizes and check against those if resize happens
1621 c->orig_width = avctx->width;
1622 c->orig_height = avctx->height;
1627 static av_cold int g2m_decode_end(AVCodecContext *avctx)
1629 G2MContext *const c = avctx->priv_data;
1631 jpg_free_context(&c->jc);
1633 av_freep(&c->epic_buf_base);
1635 av_freep(&c->kempf_buf);
1636 av_freep(&c->kempf_flags);
1637 av_freep(&c->synth_tile);
1638 av_freep(&c->jpeg_tile);
1639 av_freep(&c->cursor);
1640 av_freep(&c->framebuf);
1645 AVCodec ff_g2m_decoder = {
1647 .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
1648 .type = AVMEDIA_TYPE_VIDEO,
1649 .id = AV_CODEC_ID_G2M,
1650 .priv_data_size = sizeof(G2MContext),
1651 .init = g2m_decode_init,
1652 .close = g2m_decode_end,
1653 .decode = g2m_decode_frame,
1654 .capabilities = AV_CODEC_CAP_DR1,
1655 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,