Browse code

avcodec: add YUY2 Lossless Codec decoder

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/06/13 19:17:44
Showing 8 changed files
... ...
@@ -2,6 +2,10 @@ Entries are sorted chronologically from oldest to youngest within each release,
2 2
 releases are sorted from youngest to oldest.
3 3
 
4 4
 version <next>:
5
+- YUY2 Lossless Codec decoder
6
+
7
+
8
+version 3.1:
5 9
 - DXVA2-accelerated HEVC Main10 decoding
6 10
 - fieldhint filter
7 11
 - loop video filter and aloop audio filter
... ...
@@ -633,6 +633,7 @@ OBJS-$(CONFIG_XWD_DECODER)             += xwddec.o
633 633
 OBJS-$(CONFIG_XWD_ENCODER)             += xwdenc.o
634 634
 OBJS-$(CONFIG_Y41P_DECODER)            += y41pdec.o
635 635
 OBJS-$(CONFIG_Y41P_ENCODER)            += y41penc.o
636
+OBJS-$(CONFIG_YLC_DECODER)             += ylc.o
636 637
 OBJS-$(CONFIG_YOP_DECODER)             += yop.o
637 638
 OBJS-$(CONFIG_YUV4_DECODER)            += yuv4dec.o
638 639
 OBJS-$(CONFIG_YUV4_ENCODER)            += yuv4enc.o
... ...
@@ -370,6 +370,7 @@ void avcodec_register_all(void)
370 370
     REGISTER_DECODER(XL,                xl);
371 371
     REGISTER_ENCDEC (XWD,               xwd);
372 372
     REGISTER_ENCDEC (Y41P,              y41p);
373
+    REGISTER_DECODER(YLC,               ylc);
373 374
     REGISTER_DECODER(YOP,               yop);
374 375
     REGISTER_ENCDEC (YUV4,              yuv4);
375 376
     REGISTER_DECODER(ZERO12V,           zero12v);
... ...
@@ -408,6 +408,7 @@ enum AVCodecID {
408 408
     AV_CODEC_ID_M101,
409 409
     AV_CODEC_ID_MAGICYUV,
410 410
     AV_CODEC_ID_SHEERVIDEO,
411
+    AV_CODEC_ID_YLC,
411 412
 
412 413
     /* various PCM "codecs" */
413 414
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1556,6 +1556,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1556 1556
         .long_name = NULL_IF_CONFIG_SMALL("BitJazz SheerVideo"),
1557 1557
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1558 1558
     },
1559
+    {
1560
+        .id        = AV_CODEC_ID_YLC,
1561
+        .type      = AVMEDIA_TYPE_VIDEO,
1562
+        .name      = "ylc",
1563
+        .long_name = NULL_IF_CONFIG_SMALL("YUY2 Lossless Codec"),
1564
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1565
+    },
1559 1566
 
1560 1567
     /* various PCM "codecs" */
1561 1568
     {
... ...
@@ -28,7 +28,7 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31
-#define LIBAVCODEC_VERSION_MINOR  47
31
+#define LIBAVCODEC_VERSION_MINOR  48
32 32
 #define LIBAVCODEC_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
35 35
new file mode 100644
... ...
@@ -0,0 +1,472 @@
0
+/*
1
+ * YUY2 Lossless Codec
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <stdio.h>
21
+#include <stdlib.h>
22
+#include <string.h>
23
+
24
+#include "libavutil/imgutils.h"
25
+#include "libavutil/internal.h"
26
+#include "libavutil/intreadwrite.h"
27
+#include "libavutil/mem.h"
28
+#include "avcodec.h"
29
+#include "bswapdsp.h"
30
+#include "get_bits.h"
31
+#include "huffyuvdsp.h"
32
+#include "internal.h"
33
+#include "unary.h"
34
+
35
+typedef struct YLCContext {
36
+    VLC vlc[4];
37
+    uint32_t table[1024];
38
+    uint8_t *table_bits;
39
+    uint8_t *bitstream_bits;
40
+    int table_bits_size;
41
+    int bitstream_bits_size;
42
+    BswapDSPContext bdsp;
43
+} YLCContext;
44
+
45
+static av_cold int decode_init(AVCodecContext *avctx)
46
+{
47
+    YLCContext *s = avctx->priv_data;
48
+
49
+    avctx->pix_fmt = AV_PIX_FMT_YUYV422;
50
+    ff_bswapdsp_init(&s->bdsp);
51
+
52
+    return 0;
53
+}
54
+
55
+typedef struct Node {
56
+    int16_t  sym;
57
+    int16_t  n0;
58
+    uint32_t count;
59
+    int16_t  l, r;
60
+} Node;
61
+
62
+static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
63
+                           Node *nodes, int node,
64
+                           uint32_t pfx, int pl, int *pos)
65
+{
66
+    int s;
67
+
68
+    s = nodes[node].sym;
69
+    if (s != -1) {
70
+        bits[*pos] = (~pfx) & ((1 << FFMAX(pl, 1)) - 1);
71
+        lens[*pos] = FFMAX(pl, 1);
72
+        xlat[*pos] = s + (pl == 0);
73
+        (*pos)++;
74
+    } else {
75
+        pfx <<= 1;
76
+        pl++;
77
+        get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
78
+                       pos);
79
+        pfx |= 1;
80
+        get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
81
+                       pos);
82
+    }
83
+}
84
+
85
+static int build_vlc(AVCodecContext *avctx, VLC *vlc, const uint32_t *table)
86
+{
87
+    Node nodes[512];
88
+    uint32_t bits[256];
89
+    int16_t lens[256];
90
+    uint8_t xlat[256];
91
+    int cur_node, i, j, pos = 0;
92
+
93
+    ff_free_vlc(vlc);
94
+
95
+    for (i = 0; i < 256; i++) {
96
+        nodes[i].count = table[i];
97
+        nodes[i].sym   = i;
98
+        nodes[i].n0    = -2;
99
+        nodes[i].l     = i;
100
+        nodes[i].r     = i;
101
+    }
102
+
103
+    cur_node = 256;
104
+    j = 0;
105
+    do {
106
+        for (i = 0; ; i++) {
107
+            int new_node = j;
108
+            int first_node = cur_node;
109
+            int second_node = cur_node;
110
+            int nd, st;
111
+
112
+            nodes[cur_node].count = -1;
113
+
114
+            do {
115
+                int val = nodes[new_node].count;
116
+                if (val && (val < nodes[first_node].count)) {
117
+                    if (val >= nodes[second_node].count) {
118
+                        first_node = new_node;
119
+                    } else {
120
+                        first_node = second_node;
121
+                        second_node = new_node;
122
+                    }
123
+                }
124
+                new_node += 1;
125
+            } while (new_node != cur_node);
126
+
127
+            if (first_node == cur_node)
128
+                break;
129
+
130
+            nd = nodes[second_node].count;
131
+            st = nodes[first_node].count;
132
+            nodes[second_node].count = 0;
133
+            nodes[first_node].count  = 0;
134
+            nodes[cur_node].count = nd + st;
135
+            nodes[cur_node].sym = -1;
136
+            nodes[cur_node].n0 = cur_node;
137
+            nodes[cur_node].l = first_node;
138
+            nodes[cur_node].r = second_node;
139
+            cur_node++;
140
+        }
141
+        j++;
142
+    } while (cur_node - 256 == j);
143
+
144
+    get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);
145
+
146
+    return ff_init_vlc_sparse(vlc, 10, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
147
+}
148
+
149
+static const uint8_t table_y1[] = {
150
+    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
151
+    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
152
+    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
153
+    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
154
+    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
155
+    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
156
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
157
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
158
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
159
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
160
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
161
+    0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
167
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
168
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
169
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
170
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
171
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
172
+    0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
173
+    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
174
+    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
175
+    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
176
+    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
177
+    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
178
+    0x02, 0x00,
179
+};
180
+
181
+static const uint8_t table_u[] = {
182
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
183
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
184
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
186
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
187
+    0x01, 0x01, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0xFF,
188
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
189
+    0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
190
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191
+    0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
192
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
193
+    0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
194
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
195
+    0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
196
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
198
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFF,
199
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
200
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
201
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
203
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
204
+    0x01, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
205
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
206
+    0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
207
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208
+    0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
209
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
210
+    0x01, 0x00,
211
+};
212
+
213
+static const uint8_t table_y2[] = {
214
+    0xFC, 0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE,
215
+    0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFC,
216
+    0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE,
217
+    0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFC, 0xFC,
218
+    0xFC, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF,
219
+    0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFD, 0xFD, 0xFD,
220
+    0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
221
+    0x00, 0x01, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0xFE,
222
+    0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
223
+    0x01, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE,
224
+    0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
225
+    0x01, 0x01, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
226
+    0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02,
227
+    0x02, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00,
228
+    0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
229
+    0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
230
+    0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0xFF,
231
+    0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
232
+    0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0xFF, 0xFF,
233
+    0xFF, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02,
234
+    0x02, 0x02, 0x03, 0x03, 0x03, 0xFF, 0xFF, 0xFF,
235
+    0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02,
236
+    0x02, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x01,
237
+    0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03,
238
+    0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01,
239
+    0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x04,
240
+    0x04, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
241
+    0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x04, 0x04,
242
+    0x04, 0x00,
243
+};
244
+
245
+static const uint8_t table_v[] = {
246
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
247
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
248
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
249
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
250
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
251
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
252
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
253
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
254
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
255
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
256
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
257
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
258
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
259
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
260
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
261
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
262
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
263
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
264
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
265
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
266
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
267
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
268
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
269
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
270
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
271
+    0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
272
+    0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
273
+    0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
274
+    0x01, 0x00,
275
+};
276
+
277
+static int decode_frame(AVCodecContext *avctx,
278
+                        void *data, int *got_frame,
279
+                        AVPacket *avpkt)
280
+{
281
+    int TL[4] = { 128, 128, 128, 128 };
282
+    int L[4]  = { 128, 128, 128, 128 };
283
+    YLCContext *s = avctx->priv_data;
284
+    const uint8_t *buf = avpkt->data;
285
+    int ret, x, y, toffset, boffset;
286
+    AVFrame * const p = data;
287
+    GetBitContext gb;
288
+    uint8_t *dst;
289
+
290
+    if (avpkt->size <= 16)
291
+        return AVERROR_INVALIDDATA;
292
+
293
+    if (AV_RL32(buf) != MKTAG('Y', 'L', 'C', '0') ||
294
+        AV_RL32(buf + 4) != 0)
295
+        return AVERROR_INVALIDDATA;
296
+
297
+    toffset = AV_RL32(buf + 8);
298
+    if (toffset < 16 || toffset >= avpkt->size)
299
+        return AVERROR_INVALIDDATA;
300
+
301
+    boffset = AV_RL32(buf + 12);
302
+    if (toffset >= boffset || boffset >= avpkt->size)
303
+        return AVERROR_INVALIDDATA;
304
+
305
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
306
+        return ret;
307
+
308
+    av_fast_malloc(&s->table_bits, &s->table_bits_size,
309
+                   boffset - toffset + AV_INPUT_BUFFER_PADDING_SIZE);
310
+    if (!s->table_bits)
311
+        return AVERROR(ENOMEM);
312
+
313
+    memcpy(s->table_bits, avpkt->data + toffset, boffset - toffset);
314
+    memset(s->table_bits + boffset - toffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
315
+    s->bdsp.bswap_buf((uint32_t *) s->table_bits,
316
+                      (uint32_t *) s->table_bits,
317
+                      (boffset - toffset + 3) >> 2);
318
+    if ((ret = init_get_bits8(&gb, s->table_bits, boffset - toffset)) < 0)
319
+        return ret;
320
+
321
+    for (x = 0; x < 1024; x++) {
322
+        unsigned len = get_unary(&gb, 1, 31);
323
+        uint32_t val = ((1U << len) - 1) + get_bits_long(&gb, len);
324
+
325
+        s->table[x] = val;
326
+    }
327
+
328
+    ret = build_vlc(avctx, &s->vlc[0], &s->table[0  ]);
329
+    if (ret < 0)
330
+        return ret;
331
+    ret = build_vlc(avctx, &s->vlc[1], &s->table[256]);
332
+    if (ret < 0)
333
+        return ret;
334
+    ret = build_vlc(avctx, &s->vlc[2], &s->table[512]);
335
+    if (ret < 0)
336
+        return ret;
337
+    ret = build_vlc(avctx, &s->vlc[3], &s->table[768]);
338
+    if (ret < 0)
339
+        return ret;
340
+
341
+    av_fast_malloc(&s->bitstream_bits, &s->bitstream_bits_size,
342
+                   avpkt->size - boffset + AV_INPUT_BUFFER_PADDING_SIZE);
343
+    if (!s->bitstream_bits)
344
+        return AVERROR(ENOMEM);
345
+
346
+    memcpy(s->bitstream_bits, avpkt->data + boffset, avpkt->size - boffset);
347
+    memset(s->bitstream_bits + avpkt->size - boffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
348
+    s->bdsp.bswap_buf((uint32_t *) s->bitstream_bits,
349
+                      (uint32_t *) s->bitstream_bits,
350
+                      (avpkt->size - boffset) >> 2);
351
+    if ((ret = init_get_bits8(&gb, s->bitstream_bits, avpkt->size - boffset)) < 0)
352
+        return ret;
353
+
354
+    dst = p->data[0];
355
+    for (y = 0; y < avctx->height; y++) {
356
+        memset(dst, 0, avctx->width * 2);
357
+        dst += p->linesize[0];
358
+    }
359
+
360
+    dst = p->data[0];
361
+    for (y = 0; y < avctx->height; y++) {
362
+        for (x = 0; x < avctx->width * 2 && y < avctx->height;) {
363
+            if (get_bits_left(&gb) <= 0)
364
+                return AVERROR_INVALIDDATA;
365
+
366
+            if (get_bits1(&gb)) {
367
+                int val = get_vlc2(&gb, s->vlc[0].table, s->vlc[0].bits, 3);
368
+                if (val < 0) {
369
+                    return AVERROR_INVALIDDATA;
370
+                } else if (val < 0xE1) {
371
+                    dst[x    ] = table_y1[val];
372
+                    dst[x + 1] = table_u[val];
373
+                    dst[x + 2] = table_y2[val];
374
+                    dst[x + 3] = table_v[val];
375
+                    x += 4;
376
+                } else {
377
+                    int incr = (val - 0xDF) * 4;
378
+                    if (x + incr >= avctx->width * 2) {
379
+                        int iy = ((x + incr) / (avctx->width * 2));
380
+                        x  = (x + incr) % (avctx->width * 2);
381
+                        y += iy;
382
+                        dst += iy * p->linesize[0];
383
+                    } else {
384
+                        x += incr;
385
+                    }
386
+                }
387
+            } else {
388
+                int y1, y2, u, v;
389
+
390
+                y1 = get_vlc2(&gb, s->vlc[1].table, s->vlc[1].bits, 3);
391
+                u  = get_vlc2(&gb, s->vlc[2].table, s->vlc[2].bits, 3);
392
+                y2 = get_vlc2(&gb, s->vlc[1].table, s->vlc[1].bits, 3);
393
+                v  = get_vlc2(&gb, s->vlc[3].table, s->vlc[3].bits, 3);
394
+                if (y1 < 0 || y2 < 0 || u < 0 || v < 0)
395
+                    return AVERROR_INVALIDDATA;
396
+                dst[x    ] = y1;
397
+                dst[x + 1] = u;
398
+                dst[x + 2] = y1 + y2;
399
+                dst[x + 3] = v;
400
+                x += 4;
401
+            }
402
+        }
403
+        dst += p->linesize[0];
404
+    }
405
+
406
+    dst = p->data[0];
407
+    for (x = 0; x < avctx->width * 2; x += 4) {
408
+        dst[x    ] =        dst[x    ] + L[0];
409
+        dst[x + 2] = L[0] = dst[x + 2] + L[0];
410
+        L[1] = dst[x + 1] + L[1];
411
+        dst[x + 1] = L[1];
412
+        L[2] = dst[x + 3] + L[2];
413
+        dst[x + 3] = L[2];
414
+    }
415
+    dst += p->linesize[0];
416
+
417
+    for (y = 1; y < avctx->height; y++) {
418
+        x = 0;
419
+        dst[x    ] =        dst[x    ] + L[0] + dst[x + 0 - p->linesize[0]] - TL[0];
420
+        dst[x + 2] = L[0] = dst[x + 2] + L[0] + dst[x + 2 - p->linesize[0]] - TL[0];
421
+        TL[0] = dst[x + 2 - p->linesize[0]];
422
+        L[1] = dst[x + 1] + L[1] + dst[x + 1 - p->linesize[0]] - TL[1];
423
+        dst[x + 1] = L[1];
424
+        TL[1] = dst[x + 1 - p->linesize[0]];
425
+        L[2] = dst[x + 3] + L[2] + dst[x + 3 - p->linesize[0]] - TL[2];
426
+        dst[x + 3] = L[2];
427
+        TL[2] = dst[x + 3 - p->linesize[0]];
428
+        for (x = 4; x < avctx->width * 2; x += 4) {
429
+            dst[x    ] =        dst[x    ] + L[0] + dst[x + 0 - p->linesize[0]] - TL[0];
430
+            dst[x + 2] = L[0] = dst[x + 2] + L[0] + dst[x + 2 - p->linesize[0]] - TL[0];
431
+            TL[0] = dst[x + 2 - p->linesize[0]];
432
+            L[1] = dst[x + 1] + L[1] + dst[x + 1 - p->linesize[0]] - TL[1];
433
+            dst[x + 1] = L[1];
434
+            TL[1] = dst[x + 1 - p->linesize[0]];
435
+            L[2] = dst[x + 3] + L[2] + dst[x + 3 - p->linesize[0]] - TL[2];
436
+            dst[x + 3] = L[2];
437
+            TL[2] = dst[x + 3 - p->linesize[0]];
438
+        }
439
+        dst += p->linesize[0];
440
+    }
441
+
442
+    p->pict_type = AV_PICTURE_TYPE_I;
443
+    p->key_frame = 1;
444
+    *got_frame   = 1;
445
+
446
+    return avpkt->size;
447
+}
448
+
449
+static av_cold int decode_end(AVCodecContext *avctx)
450
+{
451
+    YLCContext *s = avctx->priv_data;
452
+
453
+    ff_free_vlc(&s->vlc[0]);
454
+    ff_free_vlc(&s->vlc[1]);
455
+    ff_free_vlc(&s->vlc[2]);
456
+    ff_free_vlc(&s->vlc[3]);
457
+
458
+    return 0;
459
+}
460
+
461
+AVCodec ff_ylc_decoder = {
462
+    .name           = "ylc",
463
+    .long_name      = NULL_IF_CONFIG_SMALL("YUY2 Lossless Codec"),
464
+    .type           = AVMEDIA_TYPE_VIDEO,
465
+    .id             = AV_CODEC_ID_YLC,
466
+    .priv_data_size = sizeof(YLCContext),
467
+    .init           = decode_init,
468
+    .close          = decode_end,
469
+    .decode         = decode_frame,
470
+    .capabilities   = AV_CODEC_CAP_DR1,
471
+};
... ...
@@ -424,6 +424,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
424 424
     { AV_CODEC_ID_M101,         MKTAG('M', '1', '0', '1') },
425 425
     { AV_CODEC_ID_M101,         MKTAG('M', '1', '0', '2') },
426 426
     { AV_CODEC_ID_MAGICYUV,     MKTAG('M', 'A', 'G', 'Y') },
427
+    { AV_CODEC_ID_YLC,          MKTAG('Y', 'L', 'C', '0') },
427 428
 
428 429
     { AV_CODEC_ID_NONE,         0 }
429 430
 };