Browse code

Merge commit '08c2d8f0aa679c2f060721d1d0d4f33d2ae32368'

* commit '08c2d8f0aa679c2f060721d1d0d4f33d2ae32368':
Go2Meeting decoder

Conflicts:
Changelog
libavcodec/g2meet.c
libavcodec/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2015/07/01 10:18:30
Showing 7 changed files
... ...
@@ -11,6 +11,7 @@ version <next>:
11 11
 - rewritten ASF demuxer
12 12
 - showvolume filter
13 13
 - Many improvements to the JPEG 2000 decoder
14
+- Go2Meeting decoding support
14 15
 
15 16
 
16 17
 version 2.7:
... ...
@@ -667,6 +667,8 @@ following image formats are supported:
667 667
     @tab Sorenson H.263 used in Flash
668 668
 @item Forward Uncompressed   @tab     @tab  X
669 669
 @item Fraps                  @tab     @tab  X
670
+@item Go2Meeting             @tab     @tab  X
671
+    @tab fourcc: G2M2, G2M3
670 672
 @item Go2Webinar             @tab     @tab  X
671 673
     @tab fourcc: G2M4
672 674
 @item H.261                  @tab  X  @tab  X
... ...
@@ -257,7 +257,7 @@ OBJS-$(CONFIG_FLIC_DECODER)            += flicvideo.o
257 257
 OBJS-$(CONFIG_FOURXM_DECODER)          += 4xm.o
258 258
 OBJS-$(CONFIG_FRAPS_DECODER)           += fraps.o
259 259
 OBJS-$(CONFIG_FRWU_DECODER)            += frwu.o
260
-OBJS-$(CONFIG_G2M_DECODER)             += g2meet.o
260
+OBJS-$(CONFIG_G2M_DECODER)             += g2meet.o elsdec.o
261 261
 OBJS-$(CONFIG_G723_1_DECODER)          += g723_1.o acelp_vectors.o \
262 262
                                           celp_filters.o celp_math.o
263 263
 OBJS-$(CONFIG_G723_1_ENCODER)          += g723_1.o acelp_vectors.o celp_math.o
264 264
new file mode 100644
... ...
@@ -0,0 +1,422 @@
0
+/*
1
+ * ELS (Entropy Logarithmic-Scale) decoder
2
+ *
3
+ * Copyright (c) 2013 Maxim Poliakovski
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
+ * Entropy Logarithmic-Scale binary arithmetic decoder
25
+ */
26
+
27
+#include <math.h>
28
+#include <stdint.h>
29
+
30
+#include "libavutil/common.h"
31
+#include "libavutil/intreadwrite.h"
32
+
33
+#include "avcodec.h"
34
+#include "elsdec.h"
35
+
36
+/* ELS coder constants and structures. */
37
+#define ELS_JOTS_PER_BYTE   36
38
+#define ELS_MAX             (1 << 24)
39
+#define RUNG_SPACE          (64 * sizeof(ElsRungNode))
40
+
41
+/* ELS coder tables. */
42
+static const struct Ladder {
43
+    int8_t  AMps;
44
+    int8_t  ALps;
45
+    uint8_t next0;
46
+    uint8_t next1;
47
+} Ladder[174] = {
48
+    { -6,   -5,   2,   1 },
49
+    { -2,  -12,   3,   6 },
50
+    { -2,  -12,   4,   6 },
51
+    { -1,  -16,   7,   5 },
52
+    { -1,  -16,   8,  10 },
53
+    { -5,   -6,  11,   9 },
54
+    { -6,   -5,  10,   5 },
55
+    { -1,  -18,  13,  11 },
56
+    { -1,  -18,  12,  14 },
57
+    { -6,   -5,  15,  18 },
58
+    { -5,   -6,  14,   9 },
59
+    { -3,   -8,  17,  15 },
60
+    { -1,  -20,  20,  16 },
61
+    { -1,  -20,  23,  17 },
62
+    { -3,   -8,  16,  18 },
63
+    { -5,   -6,  19,  26 },
64
+    { -3,   -9,  22,  24 },
65
+    { -3,   -9,  21,  19 },
66
+    { -5,   -6,  24,  26 },
67
+    { -4,   -7,  27,  25 },
68
+    { -1,  -22,  34,  28 },
69
+    { -2,  -11,  29,  27 },
70
+    { -2,  -11,  28,  30 },
71
+    { -1,  -22,  39,  29 },
72
+    { -4,   -7,  30,  32 },
73
+    { -6,   -5,  33,  31 },
74
+    { -6,   -5,  32,  25 },
75
+    { -3,   -8,  35,  33 },
76
+    { -2,  -12,  36,  38 },
77
+    { -2,  -12,  37,  35 },
78
+    { -3,   -8,  38,  40 },
79
+    { -6,   -5,  41,  48 },
80
+    { -6,   -5,  40,  31 },
81
+    { -5,   -6,  43,  41 },
82
+    { -1,  -24,  94,  42 },
83
+    { -3,   -8,  45,  43 },
84
+    { -2,  -12,  42,  44 },
85
+    { -2,  -12,  47,  45 },
86
+    { -3,   -8,  44,  46 },
87
+    { -1,  -24, 125,  47 },
88
+    { -5,   -6,  46,  48 },
89
+    { -6,   -5,  49,  49 },
90
+    { -2,  -13, 152, 164 },
91
+    { -4,   -7,  51,  49 },
92
+    { -3,   -9, 164, 168 },
93
+    { -3,   -9,  55,  51 },
94
+    { -4,   -7, 168, 170 },
95
+    { -2,  -13,  67,  55 },
96
+    { -6,   -5, 170,  49 },
97
+    { -6,   -5,  51, 170 },
98
+    { -1,  -72,  50,  74 },
99
+    { -4,   -7,  53,  49 },
100
+    { -1,  -61,  50,  74 },
101
+    { -3,   -8,  55,  49 },
102
+    { -1,  -51,  52,  76 },
103
+    { -3,   -9,  57,  51 },
104
+    { -1,  -46,  54,  76 },
105
+    { -2,  -10,  59,  53 },
106
+    { -1,  -43,  56,  78 },
107
+    { -2,  -11,  61,  53 },
108
+    { -1,  -41,  58,  80 },
109
+    { -2,  -12,  63,  55 },
110
+    { -1,  -39,  60,  82 },
111
+    { -2,  -12,  65,  55 },
112
+    { -1,  -37,  62,  84 },
113
+    { -2,  -13,  67,  57 },
114
+    { -1,  -36,  64,  86 },
115
+    { -1,  -14,  69,  59 },
116
+    { -1,  -35,  66,  88 },
117
+    { -1,  -14,  71,  59 },
118
+    { -1,  -34,  68,  90 },
119
+    { -1,  -15,  73,  61 },
120
+    { -1,  -33,  70,  92 },
121
+    { -1,  -15,  75,  61 },
122
+    { -1,  -32,  72,  94 },
123
+    { -1,  -15,  77,  63 },
124
+    { -1,  -31,  74,  96 },
125
+    { -1,  -16,  79,  65 },
126
+    { -1,  -31,  76,  98 },
127
+    { -1,  -16,  81,  67 },
128
+    { -1,  -30,  78, 100 },
129
+    { -1,  -17,  83,  67 },
130
+    { -1,  -29,  80, 102 },
131
+    { -1,  -17,  85,  69 },
132
+    { -1,  -29,  82, 104 },
133
+    { -1,  -18,  87,  71 },
134
+    { -1,  -28,  84, 104 },
135
+    { -1,  -18,  89,  73 },
136
+    { -1,  -28,  86, 108 },
137
+    { -1,  -18,  91,  73 },
138
+    { -1,  -27,  88, 108 },
139
+    { -1,  -19,  93,  75 },
140
+    { -1,  -27,  90, 112 },
141
+    { -1,  -19,  95,  77 },
142
+    { -1,  -26,  92, 112 },
143
+    { -1,  -20,  97,  79 },
144
+    { -1,  -26,  94, 114 },
145
+    { -1,  -20,  99,  81 },
146
+    { -1,  -25,  96, 116 },
147
+    { -1,  -20, 101,  83 },
148
+    { -1,  -25,  98, 118 },
149
+    { -1,  -21, 103,  83 },
150
+    { -1,  -24, 100, 120 },
151
+    { -1,  -21, 105,  85 },
152
+    { -1,  -24, 102, 122 },
153
+    { -1,  -22, 107,  87 },
154
+    { -1,  -23, 104, 124 },
155
+    { -1,  -22, 109,  89 },
156
+    { -1,  -23, 106, 126 },
157
+    { -1,  -22, 111,  91 },
158
+    { -1,  -22, 108, 128 },
159
+    { -1,  -23, 113,  93 },
160
+    { -1,  -22, 110, 130 },
161
+    { -1,  -23, 115,  95 },
162
+    { -1,  -22, 112, 132 },
163
+    { -1,  -24, 117,  97 },
164
+    { -1,  -21, 114, 134 },
165
+    { -1,  -24, 119,  99 },
166
+    { -1,  -21, 116, 136 },
167
+    { -1,  -25, 121, 101 },
168
+    { -1,  -20, 118, 136 },
169
+    { -1,  -25, 123, 103 },
170
+    { -1,  -20, 120, 138 },
171
+    { -1,  -26, 125, 105 },
172
+    { -1,  -20, 122, 140 },
173
+    { -1,  -26, 127, 107 },
174
+    { -1,  -19, 124, 142 },
175
+    { -1,  -27, 129, 107 },
176
+    { -1,  -19, 126, 144 },
177
+    { -1,  -27, 131, 111 },
178
+    { -1,  -18, 128, 146 },
179
+    { -1,  -28, 133, 111 },
180
+    { -1,  -18, 130, 146 },
181
+    { -1,  -28, 135, 115 },
182
+    { -1,  -18, 132, 148 },
183
+    { -1,  -29, 137, 115 },
184
+    { -1,  -17, 134, 150 },
185
+    { -1,  -29, 139, 117 },
186
+    { -1,  -17, 136, 152 },
187
+    { -1,  -30, 141, 119 },
188
+    { -1,  -16, 138, 152 },
189
+    { -1,  -31, 143, 121 },
190
+    { -1,  -16, 140, 154 },
191
+    { -1,  -31, 145, 123 },
192
+    { -1,  -15, 142, 156 },
193
+    { -1,  -32, 147, 125 },
194
+    { -1,  -15, 144, 158 },
195
+    { -1,  -33, 149, 127 },
196
+    { -1,  -15, 146, 158 },
197
+    { -1,  -34, 151, 129 },
198
+    { -1,  -14, 148, 160 },
199
+    { -1,  -35, 153, 131 },
200
+    { -1,  -14, 150, 160 },
201
+    { -1,  -36, 155, 133 },
202
+    { -2,  -13, 152, 162 },
203
+    { -1,  -37, 157, 135 },
204
+    { -2,  -12, 154, 164 },
205
+    { -1,  -39, 159, 137 },
206
+    { -2,  -12, 156, 164 },
207
+    { -1,  -41, 161, 139 },
208
+    { -2,  -11, 158, 166 },
209
+    { -1,  -43, 163, 141 },
210
+    { -2,  -10, 160, 166 },
211
+    { -1,  -46, 165, 143 },
212
+    { -3,   -9, 162, 168 },
213
+    { -1,  -51, 167, 143 },
214
+    { -3,   -8, 164, 170 },
215
+    { -1,  -61, 169, 145 },
216
+    { -4,   -7, 166, 170 },
217
+    { -1,  -72, 169, 145 },
218
+    { -6,   -5, 168,  49 },
219
+    {  0, -108, 171, 171 },
220
+    {  0, -108, 172, 172 },
221
+    { -6,   -5, 173, 173 },
222
+};
223
+
224
+static const uint32_t els_exp_tab[ELS_JOTS_PER_BYTE * 4 + 1] = {
225
+           0,        0,       0,       0,       0,       0,         0,        0,
226
+           0,        0,       0,       0,       0,       0,         0,        0,
227
+           0,        0,       0,       0,       0,       0,         0,        0,
228
+           0,        0,       0,       0,       0,       0,         0,        0,
229
+           0,        0,       0,       0,       1,       1,         1,        1,
230
+           1,        2,       2,       2,       3,       4,         4,        5,
231
+           6,        7,       8,      10,      11,      13,        16,       18,
232
+          21,       25,      29,      34,      40,      47,        54,       64,
233
+          74,       87,     101,     118,     138,      161,      188,      219,
234
+         256,      298,     348,     406,     474,      552,      645,      752,
235
+         877,     1024,    1194,    1393,    1625,     1896,     2211,     2580,
236
+        3010,     3511,    4096,    4778,    5573,     6501,     7584,     8847,
237
+       10321,    12040,   14045,   16384,   19112,    22295,    26007,    30339,
238
+       35391,    41285,   48160,   56180,   65536,    76288,    89088,   103936,
239
+      121344,   141312,  165120,  192512,  224512,   262144,   305664,   356608,
240
+      416000,   485376,  566016,  660480,  770560,   898816,  1048576,  1223168,
241
+     1426688,  1664256, 1941504, 2264832, 2642176,  3082240,  3595520,  4194304,
242
+     4892672,  5707520, 6657792, 7766784, 9060096, 10568960, 12328960, 14382080,
243
+    16777216,
244
+};
245
+
246
+void ff_els_decoder_init(ElsDecCtx *ctx, const uint8_t *in, size_t data_size)
247
+{
248
+    int nbytes;
249
+
250
+    /* consume up to 3 bytes from the input data */
251
+    if (data_size >= 3) {
252
+        ctx->x = AV_RB24(in);
253
+        nbytes = 3;
254
+    } else if (data_size == 2) {
255
+        ctx->x = AV_RB16(in);
256
+        nbytes = 2;
257
+    } else {
258
+        ctx->x = *in;
259
+        nbytes = 1;
260
+    }
261
+
262
+    ctx->in_buf    = in + nbytes;
263
+    ctx->data_size = data_size - nbytes;
264
+    ctx->err       = 0;
265
+    ctx->j         = ELS_JOTS_PER_BYTE;
266
+    ctx->t         = ELS_MAX;
267
+    ctx->diff      = FFMIN(ELS_MAX - ctx->x,
268
+                           ELS_MAX - els_exp_tab[ELS_JOTS_PER_BYTE * 4 - 1]);
269
+}
270
+
271
+void ff_els_decoder_uninit(ElsUnsignedRung *rung)
272
+{
273
+    av_free(rung->rem_rung_list);
274
+}
275
+
276
+static int els_import_byte(ElsDecCtx *ctx)
277
+{
278
+    if (!ctx->data_size) {
279
+        ctx->err = AVERROR_EOF;
280
+        return AVERROR_EOF;
281
+    }
282
+    ctx->x   = (ctx->x << 8) | *ctx->in_buf++;
283
+    ctx->data_size--;
284
+    ctx->j  += ELS_JOTS_PER_BYTE;
285
+    ctx->t <<= 8;
286
+
287
+    return 0;
288
+}
289
+
290
+int ff_els_decode_bit(ElsDecCtx *ctx, uint8_t *rung)
291
+{
292
+    int z, bit, ret;
293
+    const uint32_t *pAllowable = &els_exp_tab[ELS_JOTS_PER_BYTE * 3];
294
+
295
+    if (ctx->err)
296
+        return 0;
297
+
298
+    z          = pAllowable[ctx->j + Ladder[*rung].ALps];
299
+    ctx->t    -= z;
300
+    ctx->diff -= z;
301
+    if (ctx->diff > 0)
302
+        return *rung & 1;   /* shortcut for x < t > pAllowable[j - 1] */
303
+
304
+    if (ctx->t > ctx->x) {  /* decode most probable symbol (MPS) */
305
+        ctx->j += Ladder[*rung].AMps;
306
+        while (ctx->t > pAllowable[ctx->j])
307
+            ctx->j++;
308
+
309
+        if (ctx->j <= 0) { /* MPS: import one byte from bytestream. */
310
+            ret = els_import_byte(ctx);
311
+            if (ret < 0)
312
+                return ret;
313
+        }
314
+
315
+        z     = ctx->t;
316
+        bit   = *rung & 1;
317
+        *rung = Ladder[*rung].next0;
318
+    } else { /* decode less probable symbol (LPS) */
319
+        ctx->x -= ctx->t;
320
+        ctx->t  = z;
321
+
322
+        ctx->j += Ladder[*rung].ALps;
323
+        if (ctx->j <= 0) {
324
+            /* LPS: import one byte from bytestream. */
325
+            z <<= 8;
326
+            ret = els_import_byte(ctx);
327
+            if (ret < 0)
328
+                return ret;
329
+            if (ctx->j <= 0) {
330
+                /* LPS: import second byte from bytestream. */
331
+                z <<= 8;
332
+                ret = els_import_byte(ctx);
333
+                if (ret < 0)
334
+                    return ret;
335
+                while (pAllowable[ctx->j - 1] >= z)
336
+                    ctx->j--;
337
+            }
338
+        }
339
+
340
+        bit   = !(*rung & 1);
341
+        *rung = Ladder[*rung].next1;
342
+    }
343
+
344
+    ctx->diff = FFMIN(z - ctx->x, z - pAllowable[ctx->j - 1]);
345
+
346
+    return bit;
347
+}
348
+
349
+unsigned ff_els_decode_unsigned(ElsDecCtx *ctx, ElsUnsignedRung *ur)
350
+{
351
+    int i, n, r, bit;
352
+    ElsRungNode *rung_node;
353
+
354
+    if (ctx->err)
355
+        return 0;
356
+
357
+    /* decode unary prefix */
358
+    for (n = 0; n < ELS_EXPGOLOMB_LEN + 1; n++)
359
+        if (ff_els_decode_bit(ctx, &ur->prefix_rung[n]))
360
+            break;
361
+
362
+    /* handle the error/overflow case */
363
+    if (ctx->err || n >= ELS_EXPGOLOMB_LEN) {
364
+        ctx->err = AVERROR(EOVERFLOW);
365
+        return 0;
366
+    }
367
+
368
+    /* handle the zero case */
369
+    if (!n)
370
+        return 0;
371
+
372
+    /* initialize probability tree */
373
+    if (!ur->rem_rung_list) {
374
+        ur->rem_rung_list = av_realloc(NULL, RUNG_SPACE);
375
+        if (!ur->rem_rung_list) {
376
+            ctx->err = AVERROR(ENOMEM);
377
+            return 0;
378
+        }
379
+        memset(ur->rem_rung_list, 0, RUNG_SPACE);
380
+        ur->rung_list_size = RUNG_SPACE;
381
+        ur->avail_index    = ELS_EXPGOLOMB_LEN;
382
+    }
383
+
384
+    /* decode the remainder */
385
+    for (i = 0, r = 0, bit = 0; i < n; i++) {
386
+        if (!i)
387
+            rung_node = &ur->rem_rung_list[n];
388
+        else {
389
+            if (!rung_node->next_index) {
390
+                if (ur->rung_list_size <= (ur->avail_index + 2) * sizeof(ElsRungNode)) {
391
+                    // remember rung_node position
392
+                    ptrdiff_t pos     = rung_node - ur->rem_rung_list;
393
+                    ur->rem_rung_list = av_realloc(ur->rem_rung_list,
394
+                                                   ur->rung_list_size +
395
+                                                   RUNG_SPACE);
396
+                    if (!ur->rem_rung_list) {
397
+                        av_free(ur->rem_rung_list);
398
+                        ctx->err = AVERROR(ENOMEM);
399
+                        return 0;
400
+                    }
401
+                    memset((uint8_t *) ur->rem_rung_list + ur->rung_list_size, 0,
402
+                           RUNG_SPACE);
403
+                    ur->rung_list_size += RUNG_SPACE;
404
+                    // restore rung_node position in the new list
405
+                    rung_node = &ur->rem_rung_list[pos];
406
+                }
407
+                rung_node->next_index = ur->avail_index;
408
+                ur->avail_index      += 2;
409
+            }
410
+            rung_node = &ur->rem_rung_list[rung_node->next_index + bit];
411
+        }
412
+
413
+        bit = ff_els_decode_bit(ctx, &rung_node->rung);
414
+        if (ctx->err)
415
+            return bit;
416
+
417
+        r = (r << 1) + bit;
418
+    }
419
+
420
+    return (1 << n) - 1 + r; /* make value from exp golomb code */
421
+}
0 422
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+/*
1
+ * ELS (Entropy Logarithmic-Scale) decoder
2
+ *
3
+ * Copyright (c) 2013 Maxim Poliakovski
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
+ * Entropy Logarithmic-Scale binary arithmetic coder
25
+ */
26
+
27
+#ifndef AVCODEC_ELSDEC_H
28
+#define AVCODEC_ELSDEC_H
29
+
30
+#include <stdint.h>
31
+#include <sys/types.h>
32
+
33
+#define ELS_EXPGOLOMB_LEN   10
34
+
35
+typedef struct ElsDecCtx {
36
+    const uint8_t *in_buf;
37
+    unsigned x;
38
+    size_t data_size;
39
+    int j, t, diff, err;
40
+} ElsDecCtx;
41
+
42
+typedef struct ElsRungNode {
43
+    uint8_t  rung;
44
+    uint16_t next_index;
45
+} ElsRungNode;
46
+
47
+typedef struct ElsUnsignedRung {
48
+    uint8_t      prefix_rung[ELS_EXPGOLOMB_LEN + 1];
49
+    ElsRungNode  *rem_rung_list;
50
+    size_t       rung_list_size;
51
+    uint16_t     avail_index;
52
+} ElsUnsignedRung;
53
+
54
+void ff_els_decoder_init(ElsDecCtx *ctx, const uint8_t *in, size_t data_size);
55
+void ff_els_decoder_uninit(ElsUnsignedRung *rung);
56
+int  ff_els_decode_bit(ElsDecCtx *ctx, unsigned char *rung);
57
+unsigned ff_els_decode_unsigned(ElsDecCtx *ctx, ElsUnsignedRung *ur);
58
+
59
+#endif /* AVCODEC_ELSDEC_H */
... ...
@@ -1,6 +1,7 @@
1 1
 /*
2
- * Go2Webinar decoder
2
+ * Go2Webinar / Go2Meeting decoder
3 3
  * Copyright (c) 2012 Konstantin Shishkov
4
+ * Copyright (c) 2013 Maxim Poliakovski
4 5
  *
5 6
  * This file is part of FFmpeg.
6 7
  *
... ...
@@ -21,7 +22,7 @@
21 21
 
22 22
 /**
23 23
  * @file
24
- * Go2Webinar decoder
24
+ * Go2Webinar / Go2Meeting decoder
25 25
  */
26 26
 
27 27
 #include <inttypes.h>
... ...
@@ -32,12 +33,16 @@
32 32
 #include "avcodec.h"
33 33
 #include "blockdsp.h"
34 34
 #include "bytestream.h"
35
+#include "elsdec.h"
35 36
 #include "get_bits.h"
36 37
 #include "idctdsp.h"
37 38
 #include "internal.h"
38 39
 #include "jpegtables.h"
39 40
 #include "mjpeg.h"
40 41
 
42
+#define EPIC_PIX_STACK_SIZE 1024
43
+#define EPIC_PIX_STACK_MAX  (EPIC_PIX_STACK_SIZE - 1)
44
+
41 45
 enum ChunkType {
42 46
     DISPLAY_INFO = 0xC8,
43 47
     TILE_DATA,
... ...
@@ -74,6 +79,42 @@ static const uint8_t chroma_quant[64] = {
74 74
     50, 50, 50, 50, 50, 50, 50, 50,
75 75
 };
76 76
 
77
+typedef struct ePICPixListElem {
78
+    struct ePICPixListElem *next;
79
+    uint32_t               pixel;
80
+    uint8_t                rung;
81
+} ePICPixListElem;
82
+
83
+typedef struct ePICPixHashElem {
84
+    uint32_t                pix_id;
85
+    struct ePICPixListElem  *list;
86
+} ePICPixHashElem;
87
+
88
+#define EPIC_HASH_SIZE 256
89
+typedef struct ePICPixHash {
90
+    ePICPixHashElem *bucket[EPIC_HASH_SIZE];
91
+    int              bucket_size[EPIC_HASH_SIZE];
92
+    int              bucket_fill[EPIC_HASH_SIZE];
93
+} ePICPixHash;
94
+
95
+typedef struct ePICContext {
96
+    ElsDecCtx        els_ctx;
97
+    int              next_run_pos;
98
+    ElsUnsignedRung  unsigned_rung;
99
+    uint8_t          W_flag_rung;
100
+    uint8_t          N_flag_rung;
101
+    uint8_t          W_ctx_rung[256];
102
+    uint8_t          N_ctx_rung[512];
103
+    uint8_t          nw_pred_rung[256];
104
+    uint8_t          ne_pred_rung[256];
105
+    uint8_t          prev_row_rung[14];
106
+    uint8_t          runlen_zeroes[14];
107
+    uint8_t          runlen_one;
108
+    int              stack_pos;
109
+    uint32_t         stack[EPIC_PIX_STACK_SIZE];
110
+    ePICPixHash      hash;
111
+} ePICContext;
112
+
77 113
 typedef struct JPGContext {
78 114
     BlockDSPContext bdsp;
79 115
     IDCTDSPContext idsp;
... ...
@@ -87,7 +128,9 @@ typedef struct JPGContext {
87 87
 } JPGContext;
88 88
 
89 89
 typedef struct G2MContext {
90
+    ePICContext ec;
90 91
     JPGContext jc;
92
+
91 93
     int        version;
92 94
 
93 95
     int        compression;
... ...
@@ -101,8 +144,9 @@ typedef struct G2MContext {
101 101
     uint8_t    *framebuf;
102 102
     int        framebuf_stride, old_width, old_height;
103 103
 
104
-    uint8_t    *synth_tile, *jpeg_tile;
105
-    int        tile_stride, old_tile_w, old_tile_h;
104
+    uint8_t    *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
105
+    int        tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
106
+    int        swapuv;
106 107
 
107 108
     uint8_t    *kempf_buf, *kempf_flags;
108 109
 
... ...
@@ -229,11 +273,11 @@ static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
229 229
     return 0;
230 230
 }
231 231
 
232
-static inline void yuv2rgb(uint8_t *out, int Y, int U, int V)
232
+static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
233 233
 {
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));
234
+    out[ridx]     = av_clip_uint8(Y +              (91881 * V + 32768 >> 16));
235
+    out[1]        = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
236
+    out[2 - ridx] = av_clip_uint8(Y + (116130 * U             + 32768 >> 16));
237 237
 }
238 238
 
239 239
 static int jpg_decode_data(JPGContext *c, int width, int height,
... ...
@@ -247,6 +291,7 @@ static int jpg_decode_data(JPGContext *c, int width, int height,
247 247
     int bx, by;
248 248
     int unesc_size;
249 249
     int ret;
250
+    const int ridx = swapuv ? 2 : 0;
250 251
 
251 252
     if ((ret = av_reallocp(&c->buf,
252 253
                            src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
... ...
@@ -299,9 +344,9 @@ static int jpg_decode_data(JPGContext *c, int width, int height,
299 299
                     int Y, U, V;
300 300
 
301 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);
302
+                    U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
303
+                    V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
304
+                    yuv2rgb(out + i * 3, ridx, Y, U, V);
305 305
                 }
306 306
             }
307 307
 
... ...
@@ -318,6 +363,659 @@ static int jpg_decode_data(JPGContext *c, int width, int height,
318 318
     return 0;
319 319
 }
320 320
 
321
+#define LOAD_NEIGHBOURS(x)      \
322
+    W   = curr_row[(x)   - 1];  \
323
+    N   = above_row[(x)];       \
324
+    WW  = curr_row[(x)   - 2];  \
325
+    NW  = above_row[(x)  - 1];  \
326
+    NE  = above_row[(x)  + 1];  \
327
+    NN  = above2_row[(x)];      \
328
+    NNW = above2_row[(x) - 1];  \
329
+    NWW = above_row[(x)  - 2];  \
330
+    NNE = above2_row[(x) + 1]
331
+
332
+#define UPDATE_NEIGHBOURS(x)    \
333
+    NNW = NN;                   \
334
+    NN  = NNE;                  \
335
+    NWW = NW;                   \
336
+    NW  = N;                    \
337
+    N   = NE;                   \
338
+    NE  = above_row[(x)  + 1];  \
339
+    NNE = above2_row[(x) + 1]
340
+
341
+#define R_shift 16
342
+#define G_shift  8
343
+#define B_shift  0
344
+
345
+static inline int log2_ceil(uint32_t x)
346
+{
347
+    int c = 0;
348
+
349
+    for (--x; x > 0; x >>= 1)
350
+        c++;
351
+
352
+    return c;
353
+}
354
+
355
+/* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
356
+static int djb2_hash(uint32_t key)
357
+{
358
+    int h = 5381;
359
+
360
+    h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
361
+    h = (h * 33) ^ ((key >> 16) & 0xFF);
362
+    h = (h * 33) ^ ((key >>  8) & 0xFF);
363
+    h = (h * 33) ^  (key        & 0xFF);
364
+
365
+    return h & (EPIC_HASH_SIZE - 1);
366
+}
367
+
368
+static void epic_hash_init(ePICPixHash *hash)
369
+{
370
+    memset(hash, 0, sizeof(*hash));
371
+}
372
+
373
+static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
374
+{
375
+    int i, idx = djb2_hash(key);
376
+    ePICPixHashElem *bucket = hash->bucket[idx];
377
+
378
+    for (i = 0; i < hash->bucket_fill[idx]; i++)
379
+        if (bucket[i].pix_id == key)
380
+            return &bucket[i];
381
+
382
+    return NULL;
383
+}
384
+
385
+static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
386
+{
387
+    ePICPixHashElem *bucket, *ret;
388
+    int idx = djb2_hash(key);
389
+
390
+    if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
391
+        return NULL;
392
+
393
+    if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
394
+        int new_size = hash->bucket_size[idx] + 16;
395
+        bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
396
+        if (!bucket)
397
+            return NULL;
398
+        hash->bucket[idx]      = bucket;
399
+        hash->bucket_size[idx] = new_size;
400
+    }
401
+
402
+    ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
403
+    memset(ret, 0, sizeof(*ret));
404
+    ret->pix_id = key;
405
+    return ret;
406
+}
407
+
408
+static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
409
+{
410
+    ePICPixListElem *new_elem;
411
+    ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
412
+
413
+    if (!hash_elem) {
414
+        if (!(hash_elem = epic_hash_add(hash, key)))
415
+            return AVERROR(ENOMEM);
416
+    }
417
+
418
+    new_elem = av_mallocz(sizeof(*new_elem));
419
+    if (!new_elem)
420
+        return AVERROR(ENOMEM);
421
+
422
+    new_elem->pixel = pix;
423
+    new_elem->next  = hash_elem->list;
424
+    hash_elem->list = new_elem;
425
+
426
+    return 0;
427
+}
428
+
429
+static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
430
+                                               uint32_t pix)
431
+{
432
+    ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
433
+
434
+    if (hash_elem != NULL && hash_elem->list != NULL)
435
+        return 1;
436
+
437
+    return 0;
438
+}
439
+
440
+static void epic_free_pixel_cache(ePICPixHash *hash)
441
+{
442
+    int i, j;
443
+
444
+    for (i = 0; i < EPIC_HASH_SIZE; i++) {
445
+        for (j = 0; j < hash->bucket_fill[i]; j++) {
446
+            ePICPixListElem *list_elem = hash->bucket[i][j].list;
447
+            while (list_elem) {
448
+                ePICPixListElem *tmp = list_elem->next;
449
+                av_free(list_elem);
450
+                list_elem = tmp;
451
+            }
452
+        }
453
+        av_freep(&hash->bucket[i]);
454
+        hash->bucket_size[i] =
455
+        hash->bucket_fill[i] = 0;
456
+    }
457
+}
458
+
459
+static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
460
+{
461
+    int i;
462
+
463
+    for (i = 0; i < dc->stack_pos; i++)
464
+        if (dc->stack[i] == pix)
465
+            break;
466
+
467
+    return i != dc->stack_pos;
468
+}
469
+
470
+#define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
471
+
472
+static inline int epic_decode_component_pred(ePICContext *dc,
473
+                                             int N, int W, int NW)
474
+{
475
+    unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
476
+    return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
477
+}
478
+
479
+static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
480
+                                       const uint32_t *curr_row,
481
+                                       const uint32_t *above_row)
482
+{
483
+    uint32_t N, W, NW, pred;
484
+    unsigned delta;
485
+    int GN, GW, GNW, R, G, B;
486
+
487
+    if (x && y) {
488
+        W  = curr_row[x  - 1];
489
+        N  = above_row[x];
490
+        NW = above_row[x - 1];
491
+
492
+        GN  = (N  >> G_shift) & 0xFF;
493
+        GW  = (W  >> G_shift) & 0xFF;
494
+        GNW = (NW >> G_shift) & 0xFF;
495
+
496
+        G = epic_decode_component_pred(dc, GN, GW, GNW);
497
+
498
+        R = G + epic_decode_component_pred(dc,
499
+                                           ((N  >> R_shift) & 0xFF) - GN,
500
+                                           ((W  >> R_shift) & 0xFF) - GW,
501
+                                           ((NW >> R_shift) & 0xFF) - GNW);
502
+
503
+        B = G + epic_decode_component_pred(dc,
504
+                                           ((N  >> B_shift) & 0xFF) - GN,
505
+                                           ((W  >> B_shift) & 0xFF) - GW,
506
+                                           ((NW >> B_shift) & 0xFF) - GNW);
507
+    } else {
508
+        if (x)
509
+            pred = curr_row[x - 1];
510
+        else
511
+            pred = above_row[x];
512
+
513
+        delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
514
+        R     = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
515
+
516
+        delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
517
+        G     = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
518
+
519
+        delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
520
+        B     = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
521
+    }
522
+
523
+    return (R << R_shift) | (G << G_shift) | (B << B_shift);
524
+}
525
+
526
+static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
527
+                              uint32_t *pPix, uint32_t pix)
528
+{
529
+    if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
530
+        *pPix = pix;
531
+        return 1;
532
+    }
533
+    dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
534
+    return 0;
535
+}
536
+
537
+static int epic_handle_edges(ePICContext *dc, int x, int y,
538
+                             const uint32_t *curr_row,
539
+                             const uint32_t *above_row, uint32_t *pPix)
540
+{
541
+    uint32_t pix;
542
+
543
+    if (!x && !y) { /* special case: top-left pixel */
544
+        /* the top-left pixel is coded independently with 3 unsigned numbers */
545
+        *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
546
+                (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
547
+                (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
548
+        return 1;
549
+    }
550
+
551
+    if (x) { /* predict from W first */
552
+        pix = curr_row[x - 1];
553
+        if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
554
+            return 1;
555
+    }
556
+
557
+    if (y) { /* then try to predict from N */
558
+        pix = above_row[x];
559
+        if (!dc->stack_pos || dc->stack[0] != pix) {
560
+            if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
561
+                return 1;
562
+        }
563
+    }
564
+
565
+    return 0;
566
+}
567
+
568
+static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
569
+                                  const uint32_t *curr_row,
570
+                                  const uint32_t *above_row,
571
+                                  const uint32_t *above2_row,
572
+                                  uint32_t *pPix, int *pRun)
573
+{
574
+    int idx, got_pixel = 0, WWneW, old_WWneW = 0;
575
+    uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
576
+
577
+    *pRun = 0;
578
+
579
+    LOAD_NEIGHBOURS(x);
580
+
581
+    if (dc->next_run_pos == x) {
582
+        /* can't reuse W for the new pixel in this case */
583
+        WWneW = 1;
584
+    } else {
585
+        idx = (WW  != W)  << 7 |
586
+              (NW  != W)  << 6 |
587
+              (N   != NE) << 5 |
588
+              (NW  != N)  << 4 |
589
+              (NWW != NW) << 3 |
590
+              (NNE != NE) << 2 |
591
+              (NN  != N)  << 1 |
592
+              (NNW != NW);
593
+        WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
594
+    }
595
+
596
+    if (WWneW)
597
+        dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
598
+    else {
599
+        *pPix     = W;
600
+        got_pixel = 1;
601
+    }
602
+
603
+    do {
604
+        int NWneW = 1;
605
+        if (got_pixel) // pixel value already known (derived from either W or N)
606
+            NWneW = *pPix != N;
607
+        else { // pixel value is unknown and will be decoded later
608
+            NWneW = *pRun ? NWneW : NW != W;
609
+
610
+            /* TODO: RFC this mess! */
611
+            switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
612
+            case 0:
613
+                break; // do nothing here
614
+            case 3:
615
+            case 5:
616
+            case 6:
617
+            case 7:
618
+                if (!is_pixel_on_stack(dc, N)) {
619
+                    idx = WWneW       << 8 |
620
+                          (*pRun ? old_WWneW : WW != W) << 7 |
621
+                          NWneW       << 6 |
622
+                          (N   != NE) << 5 |
623
+                          (NW  != N)  << 4 |
624
+                          (NWW != NW) << 3 |
625
+                          (NNE != NE) << 2 |
626
+                          (NN  != N)  << 1 |
627
+                          (NNW != NW);
628
+                    if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
629
+                        NWneW = 0;
630
+                        *pPix = N;
631
+                        got_pixel = 1;
632
+                        break;
633
+                    }
634
+                }
635
+                /* fall through */
636
+            default:
637
+                NWneW = 1;
638
+                old_WWneW = WWneW;
639
+                if (!is_pixel_on_stack(dc, N))
640
+                    dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
641
+            }
642
+        }
643
+
644
+        (*pRun)++;
645
+        if (x + *pRun >= tile_width - 1)
646
+            break;
647
+
648
+        UPDATE_NEIGHBOURS(x + *pRun);
649
+
650
+        if (!NWneW && NW == N && N == NE) {
651
+            int pos, run, rle;
652
+            int start_pos = x + *pRun;
653
+
654
+            /* scan for a run of pix in the line above */
655
+            uint32_t pix = above_row[start_pos + 1];
656
+            for (pos = start_pos + 2; pos < tile_width; pos++)
657
+                if (!(above_row[pos] == pix))
658
+                    break;
659
+            run = pos - start_pos - 1;
660
+            idx = log2_ceil(run);
661
+            if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
662
+                *pRun += run;
663
+            else {
664
+                int flag;
665
+                /* run-length is coded as plain binary number of idx - 1 bits */
666
+                for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
667
+                    if ((1 << pos) + rle < run &&
668
+                        ff_els_decode_bit(&dc->els_ctx,
669
+                                          flag ? &dc->runlen_one
670
+                                               : &dc->runlen_zeroes[pos])) {
671
+                        flag = 1;
672
+                        rle |= 1 << pos;
673
+                    }
674
+                }
675
+                *pRun += rle;
676
+                break; // return immediately
677
+            }
678
+            if (x + *pRun >= tile_width - 1)
679
+                break;
680
+
681
+            LOAD_NEIGHBOURS(x + *pRun);
682
+            WWneW = 0;
683
+            NWneW = 0;
684
+        }
685
+
686
+        idx = WWneW       << 7 |
687
+              NWneW       << 6 |
688
+              (N   != NE) << 5 |
689
+              (NW  != N)  << 4 |
690
+              (NWW != NW) << 3 |
691
+              (NNE != NE) << 2 |
692
+              (NN  != N)  << 1 |
693
+              (NNW != NW);
694
+        WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
695
+    } while (!WWneW);
696
+
697
+    dc->next_run_pos = x + *pRun;
698
+    return got_pixel;
699
+}
700
+
701
+static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
702
+                               uint32_t *pPix, uint32_t pix)
703
+{
704
+    if (ff_els_decode_bit(&dc->els_ctx, rung)) {
705
+        *pPix = pix;
706
+        return 1;
707
+    }
708
+    dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
709
+    return 0;
710
+}
711
+
712
+static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
713
+                                   int tile_width, const uint32_t *curr_row,
714
+                                   const uint32_t *above_row, uint32_t *pPix)
715
+{
716
+    int pos;
717
+
718
+    /* try to reuse the NW pixel first */
719
+    if (x && y) {
720
+        uint32_t NW = above_row[x - 1];
721
+        if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
722
+            if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
723
+                return 1;
724
+        }
725
+    }
726
+
727
+    /* try to reuse the NE[x + run, y] pixel */
728
+    pos = x + run - 1;
729
+    if (pos < tile_width - 1 && y) {
730
+        uint32_t NE = above_row[pos + 1];
731
+        if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
732
+            if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
733
+                return 1;
734
+        }
735
+    }
736
+
737
+    return 0;
738
+}
739
+
740
+static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
741
+{
742
+    ePICPixListElem *list, *prev = NULL;
743
+    ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
744
+
745
+    if (!hash_elem || !hash_elem->list)
746
+        return 0;
747
+
748
+    list = hash_elem->list;
749
+    while (list) {
750
+        if (!is_pixel_on_stack(dc, list->pixel)) {
751
+            if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
752
+                *pPix = list->pixel;
753
+                if (list != hash_elem->list) {
754
+                    prev->next      = list->next;
755
+                    list->next      = hash_elem->list;
756
+                    hash_elem->list = list;
757
+                }
758
+                return 1;
759
+            }
760
+            dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
761
+        }
762
+        prev = list;
763
+        list = list->next;
764
+    }
765
+
766
+    return 0;
767
+}
768
+
769
+static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
770
+                            int tile_width, int stride)
771
+{
772
+    int x, y;
773
+    uint32_t pix;
774
+    uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
775
+
776
+    for (y = 0; y < tile_height; y++, out += stride) {
777
+        above2_row = above_row;
778
+        above_row  = curr_row;
779
+        curr_row   = (uint32_t *) out;
780
+
781
+        for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
782
+            if (dc->els_ctx.err)
783
+                return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
784
+
785
+            pix = curr_row[x - 1]; // get W pixel
786
+
787
+            if (y >= 1 && x >= 2 &&
788
+                pix != curr_row[x - 2]  && pix != above_row[x - 1] &&
789
+                pix != above_row[x - 2] && pix != above_row[x] &&
790
+                !epic_cache_entries_for_pixel(&dc->hash, pix)) {
791
+                curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
792
+                x++;
793
+            } else {
794
+                int got_pixel, run;
795
+                dc->stack_pos = 0; // empty stack
796
+
797
+                if (y < 2 || x < 2 || x == tile_width - 1) {
798
+                    run       = 1;
799
+                    got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
800
+                } else
801
+                    got_pixel = epic_decode_run_length(dc, x, y, tile_width,
802
+                                                       curr_row, above_row,
803
+                                                       above2_row, &pix, &run);
804
+
805
+                if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
806
+                                                           tile_width, curr_row,
807
+                                                           above_row, &pix)) {
808
+                    uint32_t ref_pix = curr_row[x - 1];
809
+                    if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
810
+                        pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
811
+                        if (x) {
812
+                            int ret = epic_add_pixel_to_cache(&dc->hash,
813
+                                                              ref_pix,
814
+                                                              pix);
815
+                            if (ret)
816
+                                return ret;
817
+                        }
818
+                    }
819
+                }
820
+                for (; run > 0; x++, run--)
821
+                    curr_row[x] = pix;
822
+            }
823
+        }
824
+    }
825
+
826
+    return 0;
827
+}
828
+
829
+static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
830
+                               const uint8_t *src, size_t src_size,
831
+                               AVCodecContext *avctx)
832
+{
833
+    uint8_t prefix, mask = 0x80;
834
+    int extrabytes, tile_width, tile_height, awidth, aheight;
835
+    size_t els_dsize;
836
+    uint8_t *dst;
837
+
838
+    if (!src_size)
839
+        return 0;
840
+
841
+    /* get data size of the ELS partition as unsigned variable-length integer */
842
+    prefix = *src++;
843
+    src_size--;
844
+    for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
845
+        mask >>= 1;
846
+    if (extrabytes > 3 || src_size < extrabytes) {
847
+        av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
848
+        return AVERROR_INVALIDDATA;
849
+    }
850
+
851
+    els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
852
+    while (extrabytes-- > 0) {
853
+        els_dsize = (els_dsize << 8) | *src++;
854
+        src_size--;
855
+    }
856
+
857
+    if (src_size < els_dsize) {
858
+        av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %zu, got %zu\n",
859
+               els_dsize, src_size);
860
+        return AVERROR_INVALIDDATA;
861
+    }
862
+
863
+    tile_width  = FFMIN(c->width  - tile_x * c->tile_width,  c->tile_width);
864
+    tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
865
+    awidth      = FFALIGN(tile_width,  16);
866
+    aheight     = FFALIGN(tile_height, 16);
867
+
868
+    if (els_dsize) {
869
+        int ret, i, j, k;
870
+        uint8_t tr_r, tr_g, tr_b, *buf;
871
+        uint32_t *in;
872
+        /* ELS decoder initializations */
873
+        memset(&c->ec, 0, sizeof(c->ec));
874
+        ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
875
+        epic_hash_init(&c->ec.hash);
876
+
877
+        /* decode transparent pixel value */
878
+        tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
879
+        tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
880
+        tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
881
+        if (c->ec.els_ctx.err != 0) {
882
+            av_log(avctx, AV_LOG_ERROR,
883
+                   "ePIC: couldn't decode transparency pixel!\n");
884
+            return AVERROR_INVALIDDATA;
885
+        }
886
+
887
+        ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
888
+                               c->epic_buf_stride);
889
+
890
+        epic_free_pixel_cache(&c->ec.hash);
891
+        ff_els_decoder_uninit(&c->ec.unsigned_rung);
892
+
893
+        if (ret) {
894
+            av_log(avctx, AV_LOG_ERROR,
895
+                   "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
896
+                   avctx->frame_number, tile_x, tile_y);
897
+            return AVERROR_INVALIDDATA;
898
+        }
899
+
900
+        buf = c->epic_buf;
901
+        dst = c->framebuf + tile_x * c->tile_width * 3 +
902
+              tile_y * c->tile_height * c->framebuf_stride;
903
+
904
+        for (j = 0; j < tile_height; j++) {
905
+            uint8_t *out = dst;
906
+            in  = (uint32_t *) buf;
907
+            for (i = 0; i < tile_width; i++) {
908
+                out[0] = (in[i] >> R_shift) & 0xFF;
909
+                out[1] = (in[i] >> G_shift) & 0xFF;
910
+                out[2] = (in[i] >> B_shift) & 0xFF;
911
+                out   += 3;
912
+            }
913
+            buf += c->epic_buf_stride;
914
+            dst += c->framebuf_stride;
915
+        }
916
+
917
+        if (src_size > els_dsize) {
918
+            uint8_t *jpg;
919
+            uint32_t tr;
920
+            int bstride = FFALIGN(tile_width, 16) >> 3;
921
+            int nblocks = 0;
922
+            int estride = c->epic_buf_stride >> 2;
923
+
924
+            src      += els_dsize;
925
+            src_size -= els_dsize;
926
+
927
+            in = (uint32_t *) c->epic_buf;
928
+            tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
929
+
930
+            memset(c->kempf_flags, 0,
931
+                   (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
932
+            for (j = 0; j < tile_height; j += 8) {
933
+                for (i = 0; i < tile_width; i += 8) {
934
+                    c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
935
+                    for (k = 0; k < 8 * 8; k++) {
936
+                        if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
937
+                            c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
938
+                            nblocks++;
939
+                            break;
940
+                        }
941
+                    }
942
+                }
943
+                in += 8 * estride;
944
+            }
945
+
946
+            memset(c->jpeg_tile, 0, c->tile_stride * aheight);
947
+            jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
948
+                            c->jpeg_tile, c->tile_stride,
949
+                            c->kempf_flags, bstride, nblocks, c->swapuv);
950
+
951
+            in  = (uint32_t *) c->epic_buf;
952
+            dst = c->framebuf + tile_x * c->tile_width * 3 +
953
+                  tile_y * c->tile_height * c->framebuf_stride;
954
+            jpg = c->jpeg_tile;
955
+            for (j = 0; j < tile_height; j++) {
956
+                for (i = 0; i < tile_width; i++)
957
+                    if (in[i] == tr)
958
+                        memcpy(dst + i * 3, jpg + i * 3, 3);
959
+                in  += c->epic_buf_stride >> 2;
960
+                dst += c->framebuf_stride;
961
+                jpg += c->tile_stride;
962
+            }
963
+        }
964
+    } else {
965
+        dst = c->framebuf + tile_x * c->tile_width * 3 +
966
+              tile_y * c->tile_height * c->framebuf_stride;
967
+        return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
968
+                               dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
969
+    }
970
+
971
+    return 0;
972
+}
973
+
321 974
 static int kempf_restore_buf(const uint8_t *src, int len,
322 975
                               uint8_t *dst, int stride,
323 976
                               const uint8_t *jpeg_tile, int tile_stride,
... ...
@@ -327,6 +1025,7 @@ static int kempf_restore_buf(const uint8_t *src, int len,
327 327
     GetBitContext gb;
328 328
     int i, j, nb, col;
329 329
     int ret;
330
+    int align_width = FFALIGN(width, 16);
330 331
 
331 332
     if ((ret = init_get_bits8(&gb, src, len)) < 0)
332 333
         return ret;
... ...
@@ -346,6 +1045,7 @@ static int kempf_restore_buf(const uint8_t *src, int len,
346 346
             else
347 347
                 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
348 348
         }
349
+        skip_bits_long(&gb, nb * (align_width - width));
349 350
     }
350 351
 
351 352
     return 0;
... ...
@@ -475,22 +1175,31 @@ static int g2m_init_buffers(G2MContext *c)
475 475
             return AVERROR(ENOMEM);
476 476
     }
477 477
     if (!c->synth_tile || !c->jpeg_tile ||
478
+        (c->compression == 2 && !c->epic_buf_base) ||
478 479
         c->old_tile_w < c->tile_width ||
479 480
         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);
481
+        c->tile_stride     = FFALIGN(c->tile_width, 16) * 3;
482
+        c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
483
+        aligned_height     = FFALIGN(c->tile_height,    16);
482 484
         av_free(c->synth_tile);
483 485
         av_free(c->jpeg_tile);
484 486
         av_free(c->kempf_buf);
485 487
         av_free(c->kempf_flags);
488
+        av_free(c->epic_buf_base);
486 489
         c->synth_tile  = av_mallocz(c->tile_stride      * aligned_height);
487 490
         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
+        c->kempf_buf   = av_mallocz((c->tile_width + 1) * aligned_height +
492
+                                    FF_INPUT_BUFFER_PADDING_SIZE);
493
+        c->kempf_flags = av_mallocz(c->tile_width       * aligned_height);
491 494
         if (!c->synth_tile || !c->jpeg_tile ||
492 495
             !c->kempf_buf || !c->kempf_flags)
493 496
             return AVERROR(ENOMEM);
497
+        if (c->compression == 2) {
498
+            c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
499
+            if (!c->epic_buf_base)
500
+                return AVERROR(ENOMEM);
501
+            c->epic_buf = c->epic_buf_base + 4;
502
+        }
494 503
     }
495 504
 
496 505
     return 0;
... ...
@@ -696,10 +1405,7 @@ static int g2m_decode_frame(AVCodecContext *avctx, void *data,
696 696
         return AVERROR_INVALIDDATA;
697 697
     }
698 698
 
699
-    if ((magic & 0xF) < 4) {
700
-        av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
701
-        return AVERROR(ENOSYS);
702
-    }
699
+    c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
703 700
 
704 701
     while (bytestream2_get_bytes_left(&bc) > 5) {
705 702
         chunk_size  = bytestream2_get_le32(&bc) - 1;
... ...
@@ -808,9 +1514,10 @@ static int g2m_decode_frame(AVCodecContext *avctx, void *data,
808 808
             ret = 0;
809 809
             switch (c->compression) {
810 810
             case COMPR_EPIC_J_B:
811
-                av_log(avctx, AV_LOG_ERROR,
812
-                       "ePIC j-b compression is not implemented yet\n");
813
-                return AVERROR(ENOSYS);
811
+                ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
812
+                                          buf + bytestream2_tell(&bc),
813
+                                          chunk_size - 2, avctx);
814
+                break;
814 815
             case COMPR_KEMPF_J_B:
815 816
                 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
816 817
                                         buf + bytestream2_tell(&bc),
... ...
@@ -906,6 +1613,7 @@ static av_cold int g2m_decode_end(AVCodecContext *avctx)
906 906
 
907 907
     jpg_free_context(&c->jc);
908 908
 
909
+    av_freep(&c->epic_buf_base);
909 910
     av_freep(&c->kempf_buf);
910 911
     av_freep(&c->kempf_flags);
911 912
     av_freep(&c->synth_tile);
... ...
@@ -926,4 +1634,5 @@ AVCodec ff_g2m_decoder = {
926 926
     .close          = g2m_decode_end,
927 927
     .decode         = g2m_decode_frame,
928 928
     .capabilities   = CODEC_CAP_DR1,
929
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
929 930
 };
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 56
32
-#define LIBAVCODEC_VERSION_MINOR  45
33
-#define LIBAVCODEC_VERSION_MICRO 102
32
+#define LIBAVCODEC_VERSION_MINOR  46
33
+#define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 36
                                                LIBAVCODEC_VERSION_MINOR, \