1 /*
2 * FFV1 decoder
3 *
4 * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
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
23 /**
24 * @file
25 * FF Video Codec 1 (a lossless codec) decoder
26 */
27
41
43 int is_signed)
44 {
46 return 0;
47 else {
49 e = 0;
51 e++;
52
53 a = 1;
54 for (i = e - 1; i >= 0; i--)
56
57 e = -(is_signed &&
get_rac(c, state + 11 +
FFMIN(e, 10)));
// 11..21
58 return (a ^ e) - e;
59 }
60 }
61
63 {
65 }
66
69 {
71
73 k = 0;
74 while (i < state->error_sum) { // FIXME: optimize
75 k++;
76 i += i;
77 }
78
80 av_dlog(
NULL,
"v:%d bias:%d error:%d drift:%d count:%d k:%d",
82
83 #if 0 // JPEG LS
84 if (k == 0 && 2 * state->
drift <= -state->
count)
85 v ^= (-1);
86 #else
87 v ^= ((2 * state->
drift + state->
count) >> 31);
88 #endif
89
91
93
95 }
96
99 int plane_index,
int bits)
100 {
103 int x;
104 int run_count = 0;
105 int run_mode = 0;
107
109 int i;
110 for (x = 0; x < w; x++) {
112 for (i=0; i<
bits; i++) {
115 }
117 }
118 return;
119 }
120
121 for (x = 0; x < w; x++) {
122 int diff, context, sign;
123
124 context =
get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
125 if (context < 0) {
126 context = -context;
127 sign = 1;
128 } else
129 sign = 0;
130
132
135 } else {
136 if (context == 0 && run_mode == 0)
137 run_mode = 1;
138
139 if (run_mode) {
140 if (run_count == 0 && run_mode == 1) {
143 if (x + run_count <= w)
144 run_index++;
145 } else {
148 else
149 run_count = 0;
150 if (run_index)
151 run_index--;
152 run_mode = 2;
153 }
154 }
155 run_count--;
156 if (run_count < 0) {
157 run_mode = 0;
158 run_count = 0;
160 bits);
161 if (diff >= 0)
162 diff++;
163 } else
164 diff = 0;
165 } else
167
168 av_dlog(s->
avctx,
"count:%d index:%d, mode:%d, x:%d pos:%d\n",
170 }
171
172 if (sign)
174
175 sample[1][x] = (
predict(sample[1] + x, sample[0] + x) +
diff) &
176 ((1 << bits) - 1);
177 }
179 }
180
182 int w,
int h,
int stride,
int plane_index)
183 {
188
190
192
193 for (y = 0; y < h; y++) {
194 int16_t *
temp = sample[0];
// FIXME: try a normal buffer
195
196 sample[0] = sample[1];
198
199 sample[1][-1] = sample[0][0];
200 sample[0][w] = sample[0][w - 1];
201
202 // { START_TIMER
205 for (x = 0; x < w; x++)
206 src[x + stride * y] = sample[1][x];
207 } else {
210 for (x = 0; x < w; x++) {
211 ((uint16_t*)(src + stride*y))[x] = sample[1][x];
212 }
213 } else {
214 for (x = 0; x < w; x++) {
216 }
217 }
218 }
219 // STOP_TIMER("decode-line") }
220 }
221 }
222
224 {
230
231 for (x = 0; x < 4; x++) {
234 }
235
237
239
240 for (y = 0; y < h; y++) {
242 int16_t *
temp = sample[p][0];
// FIXME: try a normal buffer
243
244 sample[p][0] = sample[p][1];
246
247 sample[p][1][-1]= sample[p][0][0 ];
248 sample[p][0][ w]= sample[p][0][w-1];
251 else
253 }
254 for (x = 0; x < w; x++) {
255 int g = sample[0][1][x];
256 int b = sample[1][1][x];
257 int r = sample[2][1][x];
258 int a = sample[3][1][x];
259
266 }
267
268 if (lbd)
269 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
270 else {
271 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
272 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
273 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
274 }
275 }
276 }
277 }
278
280 {
284 memset(state, 128, sizeof(state));
285
287
292
298 return -1;
301 return -1;
302
308 return -1;
309 }
313
317 }
319 }
320
322 if (ps == 1) {
325 } else if (ps == 2) {
328 } else if (ps == 3) {
330 }
333
340 }
341
351 }
352 }
353 }
354
355 return 0;
356 }
357
359 {
365 int i, si;
366
368 ;
369
372
378
381
385
388 memcpy(pdst, psrc, sizeof(*pdst));
391
395 } else {
398 }
399 }
400 }
401
404
411 }
412 }
417
422
430 }
431
433 if (f->colorspace == 0) {
436 const int cx = x >> f->chroma_h_shift;
437 const int cy =
y >> f->chroma_v_shift;
439
440 if (f->chroma_planes) {
441 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
442 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
443 }
444 if (fs->transparency)
446 } else {
447 uint8_t *planes[3] = { p->data[0] + ps * x +
y * p->linesize[0],
448 p->data[1] + ps * x +
y * p->linesize[1],
449 p->data[2] + ps * x +
y * p->linesize[2] };
451 }
452 if (fs->ac && f->version > 2) {
455 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
456 if (v) {
458 fs->slice_damaged = 1;
459 }
460 }
461
462 emms_c();
463
465
466 return 0;
467 }
468
470 {
472 int i = 0;
474
475 memset(state, 128, sizeof(state));
476
477 for (v = 0; i < 128; v++) {
479
480 if (len > 128 - i)
482
483 while (len--) {
484 quant_table[i] = scale *
v;
485 i++;
486 }
487 }
488
489 for (i = 1; i < 128; i++)
490 quant_table[256 - i] = -quant_table[i];
491 quant_table[128] = -quant_table[127];
492
493 return 2 * v - 1;
494 }
495
498 {
499 int i;
501
502 for (i = 0; i < 5; i++) {
504 if (context_count > 32768
U) {
506 }
507 }
508 return (context_count + 1) / 2;
509 }
510
512 {
517
518 memset(state2, 128, sizeof(state2));
519 memset(state, 128, sizeof(state));
520
523
528 }
532 }
535 for (i = 1; i < 256; i++)
537 }
538
548
551 ) {
554 }
555
559
565 }
566 }
569
577 }
578 }
579
584 }
585
590 if (v) {
593 }
594 }
595
598 "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d\n",
609 );
610 return 0;
611 }
612
614 {
618
619 memset(state, 128, sizeof(state));
620
624 if (v >= 2) {
627 }
631 for (i = 1; i < 256; i++)
633 }
634
635 colorspace =
get_symbol(c, state, 0);
//YUV cs type
637 chroma_planes =
get_rac(c, state);
640 transparency =
get_rac(c, state);
641
651 }
652 }
653
660
662 }
663
669 else
679 }
685 }
692 }
699 }
706 }
713 }
719 }
725 }
726 }
730 "chroma subsampling not supported in this colorspace\n");
732 }
741 else
744 } else {
747 }
751 }
752
757 if (context_count < 0) {
760 }
763 } else {
768 int trailer = 3 + 5*!!f->
ec;
771 break;
772 p -= size + trailer;
773 }
774 }
778 }
779
784
786
792
803 }
804
807
812 "quant_table_index out of range\n");
814 }
819 } else {
821 }
822
828 }
830 }
831 }
832 }
833 return 0;
834 }
835
837 {
840
843
846
849
851
852 return 0;
853 }
854
856 {
858 int buf_size = avpkt->
size;
865
869
871
873 /* we have interlaced material flagged in container */
877 }
878
882
890 } else {
893 "Cannot decode non-keyframe without valid keyframe\n");
895 }
897 }
898
901
905
907
908 buf_p = buf + buf_size;
911 int trailer = 3 + 5*!!f->
ec;
913
919 }
921
924 if (crc) {
931 } else {
933 }
935 }
936 }
937
938 if (i) {
940 } else
942
945 }
946
952 sizeof(void*));
953
956 int j;
961 for (j = 0; j < 4; j++) {
968 }
974 }
975 }
977
979
985
986 *got_frame = 1;
987
988 return buf_size;
989 }
990
992 {
995
1000
1005 }
1006
1009
1012
1013 return 0;
1014 }
1015
1017 {
1025 fsdst->
ac = fsrc->
ac;
1027
1028 fsdst->
ec = fsrc->
ec;
1032
1041 }
1042 }
1043
1045 {
1049
1050 if (dst == src)
1051 return 0;
1052
1053 {
1059
1060 memcpy(fdst, fsrc, sizeof(*fdst));
1069 }
1072 }
1073
1075
1076
1081 }
1082
1084
1085 return 0;
1086 }
1087
1099 .capabilities =
CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
1101 };