1 /*
2 * Gremlin Digital Video (GDV) decoder
3 * Copyright (c) 2017 Konstantin Shishkov
4 * Copyright (c) 2017 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
28
31
35
41
46
51
52 #define PREAMBLE_SIZE 4096
53
55 {
58
64
65 for (
i = 0;
i < 2;
i++) {
66 for (j = 0; j < 256; j++) {
67 for (k = 0; k < 8; k++) {
68 gdv->
frame[
i * 2048 + j * 8 + k] = j;
69 }
70 }
71 }
72
73 return 0;
74 }
75
77 {
78 int x;
79 for (x = 0; x <
w - 7; x+=8) {
80 dst[x + 0] =
81 dst[x + 1] =
src[(x>>1) + 0];
82 dst[x + 2] =
83 dst[x + 3] =
src[(x>>1) + 1];
84 dst[x + 4] =
85 dst[x + 5] =
src[(x>>1) + 2];
86 dst[x + 6] =
87 dst[x + 7] =
src[(x>>1) + 3];
88 }
91 }
92 }
93
95 {
96 int x;
97
98 for (x =
w - 1; (x+1) & 7; x--) {
100 }
101 for (x -= 7; x >= 0; x -= 8) {
102 dst[x + 6] =
103 dst[x + 7] =
src[(x>>1) + 3];
104 dst[x + 4] =
105 dst[x + 5] =
src[(x>>1) + 2];
106 dst[x + 2] =
107 dst[x + 3] =
src[(x>>1) + 1];
108 dst[x + 0] =
109 dst[x + 1] =
src[(x>>1) + 0];
110 }
111 }
112
114 {
115 int x;
116 for (x = 0; x <
w - 7; x+=8) {
117 dst[x + 0] =
src[2*x + 0];
118 dst[x + 1] =
src[2*x + 2];
119 dst[x + 2] =
src[2*x + 4];
120 dst[x + 3] =
src[2*x + 6];
121 dst[x + 4] =
src[2*x + 8];
122 dst[x + 5] =
src[2*x +10];
123 dst[x + 6] =
src[2*x +12];
124 dst[x + 7] =
src[2*x +14];
125 }
128 }
129 }
130
132 {
133 int j, y;
134
136 return;
137 }
138
140 for (j = 0; j <
h; j++) {
144
146 }
148 for (j = 0; j <
h; j++) {
152 memcpy(dst1,
src1,
w);
153 }
154 }
155
156 if (scale_h && scale_v) {
157 for (y = 0; y < (
h>>1); y++) {
161 }
162 } else if (scale_h) {
163 for (y = 0; y < (
h>>1); y++) {
166 memcpy(dst1,
src1,
w);
167 }
168 } else if (scale_v) {
169 for (y = 0; y <
h; y++) {
172 }
173 }
174
177 }
178
180 {
181 int res;
182
183 if (
bits->fill == 0) {
184 bits->queue |= bytestream2_get_byte(gb);
186 }
187 res =
bits->queue >> 6;
190
191 return res;
192 }
193
195 {
196 bits->queue = bytestream2_get_le32(gb);
198 }
199
201 {
202 int res =
bits->queue & ((1 << nbits) - 1);
203
204 bits->queue >>= nbits;
206 if (
bits->fill <= 16) {
207 bits->queue |= bytestream2_get_le16(gb) <<
bits->fill;
209 }
210
211 return res;
212 }
213
215 {
217
220
222 c = bytestream2_get_byte(g2);
223 for (
i = 0;
i <
len;
i++) {
224 bytestream2_put_byte(pb,
c);
225 }
228
230 for (
i = 0;
i <
len;
i++) {
231 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
232 }
233 } else {
235
237 for (
i = 0;
i <
len;
i++) {
238 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
239 }
240 }
241 }
242
244 {
251
254
255 for (
c = 0;
c < 256;
c++) {
256 for (
i = 0;
i < 16;
i++) {
258 }
259 }
260
264 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
265 }
else if (
tag == 1) {
266 int b = bytestream2_get_byte(gb);
267 int len = (
b & 0xF) + 3;
268 int top = (
b >> 4) & 0xF;
269 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
271 }
else if (
tag == 2) {
272 int len = (bytestream2_get_byte(gb)) + 2;
274 } else {
275 break;
276 }
277 }
278
281
282 return 0;
283 }
284
286 {
292
295
301 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
302 }
else if (
tag == 1) {
303 int b = bytestream2_get_byte(gb);
304 int len = (
b & 0xF) + 3;
306 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
308 }
else if (
tag == 2) {
310 int b = bytestream2_get_byte(gb);
312 return 0;
313 }
316 } else {
317 len = bytestream2_get_le16(gb);
318 }
320 } else {
321 int b = bytestream2_get_byte(gb);
322 int len = (
b & 0x3) + 2;
323 int off = -(
b >> 2) - 1;
325 }
326 }
329 return 0;
330 }
331
333 {
339
343
349 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
350 } else {
352 int lbits = 0;
353 while (1) {
355
356 lbits += 1;
359 if (
val != ((1 << lbits) - 1)) {
360 break;
361 }
362 if (lbits >= 16)
364 }
365 for (
i = 0;
i <
len;
i++) {
366 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
367 }
368 }
369 }
else if (
tag == 1) {
372
375 } else {
376 int bb = bytestream2_get_byte(gb);
377 if ((bb & 0x80) == 0) {
379 } else {
380 int top = (bb & 0x7F) << 8;
381 len = top + bytestream2_get_byte(gb) + 146;
382 }
383 }
385 }
else if (
tag == 2) {
387
388 if (subtag != 3) {
390 int offs = top + bytestream2_get_byte(gb);
391 if ((subtag != 0) || (offs <= 0xF80)) {
392 int len = (subtag) + 3;
394 } else {
396
397 if (offs == 0xFFF) {
398 return 0;
399 }
400
401 real_off = ((offs >> 4) & 0x7) + 1;
402 len = ((offs & 0xF) + 2) * 2;
405 for (
i = 0;
i <
len/2;
i++) {
406 bytestream2_put_byte(pb,
c1);
407 bytestream2_put_byte(pb,
c2);
408 }
409 }
410 } else {
411 int b = bytestream2_get_byte(gb);
412 int off = ((
b & 0x7F)) + 1;
413 int len = ((
b & 0x80) == 0) ? 2 : 3;
414
416 }
417 } else {
419 int off;
420 if (use8) {
421 int q,
b = bytestream2_get_byte(gb);
422 if ((
b & 0xC0) == 0xC0) {
423 len = ((
b & 0x3F)) + 8;
425 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
426 } else {
427 int ofs1;
428 if ((
b & 0x80) == 0) {
429 len = ((
b >> 4)) + 6;
431 } else {
432 len = ((
b & 0x3F)) + 14;
434 }
435 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
436 }
437 } else {
438 int ofs1,
b = bytestream2_get_byte(gb);
439
440 if ((
b >> 4) == 0xF) {
441 len = bytestream2_get_byte(gb) + 21;
442 } else {
444 }
446 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
447 }
449 }
450 }
451
454
455 return 0;
456 }
457
460 {
466 int compression;
468 uint8_t *dst;
469
472
473 flags = bytestream2_get_le32(gb);
474 compression =
flags & 0xF;
475
476 if (compression == 4 || compression == 7 || compression > 8)
478
482
487
488 switch (compression) {
489 case 1:
491 case 0:
492 for (
i = 0;
i < 256;
i++) {
493 unsigned r = bytestream2_get_byte(gb);
494 unsigned g = bytestream2_get_byte(gb);
495 unsigned b = bytestream2_get_byte(gb);
496 gdv->
pal[
i] = 0xFF
U << 24 |
r << 18 |
g << 10 |
b << 2;
497 }
498 break;
499 case 2:
501 break;
502 case 3:
503 break;
504 case 5:
506 break;
507 case 6:
509 break;
510 case 8:
512 break;
513 default:
515 }
518
520 dst =
frame->data[0];
521
524 int y;
525
526 for (y = 0; y < avctx->
height; y++) {
527 memcpy(dst + didx, gdv->
frame + sidx, avctx->
width);
528 sidx += avctx->
width;
529 didx +=
frame->linesize[0];
530 }
531 } else {
533 int y;
534
535 for (y = 0; y < avctx->
height; y++) {
537 memcpy(dst + didx, gdv->
frame + sidx, avctx->
width);
538 } else {
539 uint8_t *dst2 = dst + didx;
540 uint8_t *src2 = gdv->
frame + sidx;
541
543 }
544 if (!gdv->
scale_h || ((y & 1) == 1)) {
546 }
547 didx +=
frame->linesize[0];
548 }
549 }
550
551 *got_frame = 1;
552
554 }
555
557 {
560 return 0;
561 }
562
574 };