1 /*
2 * Lagarith lossless decoder
3 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
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 * Lagarith lossless decoder
25 * @author Nathan Caldwell
26 */
27
34
42 FRAME_OLD_ARITH_RGB = 7,
/**< obsolete arithmetic coded RGB (no longer encoded by upstream since version 1.1.0) */
47 };
48
52 int zeros;
/**< number of consecutive zero bytes encountered */
53 int zeros_rem;
/**< number of zero bytes remaining to output */
57
58 /**
59 * Compute the 52bit mantissa of 1/(double)denom.
60 * This crazy format uses floats in an entropy coder and we have to match x86
61 * rounding exactly, thus ordinary floats aren't portable enough.
62 * @param denom denominator
63 * @return 52bit mantissa
64 * @see softfloat_mul
65 */
67 {
69 uint64_t
ret = (1ULL << 52) / denom;
70 uint64_t err = (1ULL << 52) - ret * denom;
73 err += denom / 2;
74 return ret + err / denom;
75 }
76
77 /**
78 * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
79 * Used in combination with softfloat_reciprocal computes x/(double)denom.
80 * @param x 32bit integer factor
81 * @param mantissa mantissa of f with exponent 0
82 * @return 32bit integer value (x*f)
83 * @see softfloat_reciprocal
84 */
86 {
87 uint64_t l = x * (mantissa & 0xffffffff);
88 uint64_t h = x * (mantissa >> 32);
89 h += l >> 32;
90 l &= 0xffffffff;
92 h += l >> 32;
93 return h >> 20;
94 }
95
97 {
98 return (x << 1) ^ (x >> 7);
99 }
100
102 {
103 static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
104 int i;
105 int bit = 0;
107 int prevbit = 0;
109
110 for (i = 0; i < 7; i++) {
111 if (prevbit && bit)
112 break;
113 prevbit = bit;
115 if (bit && !prevbit)
116 bits += series[i];
117 }
118 bits--;
119 if (bits < 0 || bits > 31) {
120 *value = 0;
121 return -1;
122 } else if (bits == 0) {
123 *value = 0;
124 return 0;
125 }
126
129
130 *value = val - 1;
131
132 return 0;
133 }
134
136 {
137 int i, j, scale_factor;
138 unsigned prob, cumulative_target;
139 unsigned cumul_prob = 0;
140 unsigned scaled_cumul_prob = 0;
141
143 rac->
prob[257] = UINT_MAX;
144 /* Read probabilities from bitstream */
145 for (i = 1; i < 257; i++) {
148 return -1;
149 }
150 if ((uint64_t)cumul_prob + rac->
prob[i] > UINT_MAX) {
152 return -1;
153 }
154 cumul_prob += rac->
prob[i];
158 return -1;
159 }
160 if (prob > 256 - i)
161 prob = 256 - i;
162 for (j = 0; j < prob; j++)
164 }
165 }
166
167 if (!cumul_prob) {
169 return -1;
170 }
171
172 /* Scale probabilities so cumulative probability is an even power of 2. */
173 scale_factor =
av_log2(cumul_prob);
174
175 if (cumul_prob & (cumul_prob - 1)) {
177 for (i = 1; i < 257; i++) {
179 scaled_cumul_prob += rac->
prob[i];
180 }
181
182 scale_factor++;
183 cumulative_target = 1 << scale_factor;
184
185 if (scaled_cumul_prob > cumulative_target) {
187 "Scaled probabilities are larger than target!\n");
188 return -1;
189 }
190
191 scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
192
193 for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
196 scaled_cumul_prob--;
197 }
198 /* Comment from reference source:
199 * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way
200 * // since the compression change is negligible and fixing it
201 * // breaks backwards compatibility
202 * b =- (signed int)b;
203 * b &= 0xFF;
204 * } else {
205 * b++;
206 * b &= 0x7f;
207 * }
208 */
209 }
210 }
211
212 rac->
scale = scale_factor;
213
214 /* Fill probability array with cumulative probability for each symbol. */
215 for (i = 1; i < 257; i++)
217
218 return 0;
219 }
220
223 int *left_top)
224 {
225 /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
226 * However the &0xFF on the gradient predictor yealds incorrect output
227 * for lagarith.
228 */
229 int i;
231
232 l = *left;
233 lt = *left_top;
234
235 for (i = 0; i < w; i++) {
236 l =
mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
237 lt = src1[i];
238 dst[i] = l;
239 }
240
241 *left = l;
242 *left_top = lt;
243 }
244
247 {
249
250 if (!line) {
251 /* Left prediction only for first line */
253 width, 0);
254 } else {
255 /* Left pixel is actually prev_row[width] */
256 L = buf[width - stride - 1];
257
258 if (line == 1) {
259 /* Second line, left predict first pixel, the rest of the line is median predicted
260 * NOTE: In the case of RGB this pixel is top predicted */
262 } else {
263 /* Top left is 2 rows back, last pixel */
264 TL = buf[width - (2 *
stride) - 1];
265 }
266
268 width, &L, &TL);
269 }
270 }
271
274 int is_luma)
275 {
277
278 if (!line) {
279 L= buf[0];
280 if (is_luma)
281 buf[0] = 0;
283 if (is_luma)
285 return;
286 }
287 if (line == 1) {
288 const int HEAD = is_luma ? 4 : 2;
289 int i;
290
291 L = buf[width - stride - 1];
292 TL = buf[HEAD - stride - 1];
293 for (i = 0; i < HEAD; i++) {
294 L += buf[i];
296 }
297 for (; i<
width; i++) {
298 L =
mid_pred(L&0xFF, buf[i-stride], (L + buf[i-stride] - TL)&0xFF) + buf[i];
301 }
302 } else {
303 TL = buf[width - (2 *
stride) - 1];
304 L = buf[width - stride - 1];
306 &L, &TL);
307 }
308 }
309
312 int esc_count)
313 {
314 int i = 0;
316
317 if (!esc_count)
318 esc_count = -1;
319
320 /* Output any zeros remaining from the previous run */
321 handle_zeros:
324 memset(dst + i, 0, count);
327 }
328
329 while (i < width) {
331 ret++;
332
333 if (dst[i])
335 else
337
338 i++;
339 if (l->
zeros == esc_count) {
341 ret++;
342
344
346 goto handle_zeros;
347 }
348 }
350 }
351
354 int width,
int esc_count)
355 {
356 int i = 0;
360 uint8_t mask1 = -(esc_count < 2);
361 uint8_t mask2 = -(esc_count < 3);
363
364 output_zeros:
367 if (end - dst < count) {
370 }
371
372 memset(dst, 0, count);
375 }
376
377 while (dst < end) {
378 i = 0;
379 while (!zero_run && dst + i < end) {
380 i++;
381 if (i+2 >= src_end - src)
383 zero_run =
384 !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
385 }
386 if (zero_run) {
387 zero_run = 0;
388 i += esc_count;
389 memcpy(dst, src, i);
390 dst += i;
392
393 src += i + 1;
394 goto output_zeros;
395 } else {
396 memcpy(dst, src, i);
397 src += i;
398 dst += i;
399 }
400 }
401 return src - src_start;
402 }
403
404
405
409 {
410 int i = 0;
411 int read = 0;
414 int esc_count;
417 const uint8_t *src_end = src + src_size;
418
421
422 if(src_size < 2)
424
425 esc_count = src[0];
426 if (esc_count < 4) {
428 if(src_size < 5)
430 if (esc_count &&
AV_RL32(src + 1) < length) {
432 offset += 4;
433 }
434
436
438 return -1;
439
441
442 for (i = 0; i <
height; i++)
444 stride, esc_count);
445
446 if (read > length)
448 "Output more bytes than length (%d of %d)\n", read,
449 length);
450 } else if (esc_count < 8) {
451 esc_count -= 4;
452 if (esc_count > 0) {
453 /* Zero run coding only, no range coding. */
454 for (i = 0; i <
height; i++) {
456 src_end, width, esc_count);
457 if (res < 0)
460 }
461 } else {
462 if (src_size < width * height)
464 /* Plane is stored uncompressed */
465 for (i = 0; i <
height; i++) {
466 memcpy(dst + (i * stride), src, width);
468 }
469 }
470 } else if (esc_count == 0xff) {
471 /* Plane is a solid run of given value */
472 for (i = 0; i <
height; i++)
473 memset(dst + i * stride, src[1], width);
474 /* Do not apply prediction.
475 Note: memset to 0 above, setting first value to src[1]
476 and applying prediction gives the same result. */
477 return 0;
478 } else {
480 "Invalid zero run escape code! (%#x)\n", esc_count);
481 return -1;
482 }
483
485 for (i = 0; i <
height; i++) {
488 }
489 } else {
490 for (i = 0; i <
height; i++) {
494 }
495 }
496
497 return 0;
498 }
499
500 /**
501 * Decode a frame.
502 * @param avctx codec context
503 * @param data output AVFrame
504 * @param data_size size of output data or 0 if no picture is returned
505 * @param avpkt input packet
506 * @return number of consumed bytes on success or negative if decode fails
507 */
510 {
512 unsigned int buf_size = avpkt->
size;
517 uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
518 uint32_t offs[4];
520 int i, j, planes = 3;
522
524
525 frametype = buf[0];
526
529
530 switch (frametype) {
537 } else {
539 planes = 4;
540 }
541
544
547 for (j = 0; j < avctx->
height; j++) {
548 for (i = 0; i < avctx->
width; i++)
549 AV_WN32(dst + i * 4, offset_gu);
551 }
552 } else {
553 for (j = 0; j < avctx->
height; j++) {
554 memset(dst, buf[1], avctx->
width * planes);
556 }
557 }
558 break;
562 } else {
564 offset_gu |= 0xFF
U << 24;
565 }
566
569
571 for (j = 0; j < avctx->
height; j++) {
572 for (i = 0; i < avctx->
width; i++)
574 AV_WB24(dst + i * 3, offset_gu);
575 } else {
576 AV_WN32(dst + i * 4, offset_gu);
577 }
579 }
580 break;
583 planes = 4;
584 offset_ry += 4;
590
593
594 offs[0] = offset_bv;
595 offs[1] = offset_gu;
596 offs[2] = offset_ry;
597
604 }
605 }
606 for (i = 0; i < planes; i++)
608 for (i = 0; i < planes; i++)
609 if (buf_size <= offs[i]) {
611 "Invalid frame offsets\n");
613 }
614
615 for (i = 0; i < planes; i++)
619 buf_size - offs[i]);
621 for (i = 0; i < planes; i++)
624 for (i = 0; i < avctx->
width; i++) {
626 r = srcs[0][i];
627 g = srcs[1][i];
628 b = srcs[2][i];
632 a = srcs[3][i];
634 } else {
638 }
639 }
641 for (i = 0; i < planes; i++)
643 }
644 break;
647
650
651 if (offset_ry >= buf_size ||
652 offset_gu >= buf_size ||
653 offset_bv >= buf_size) {
655 "Invalid frame offsets\n");
657 }
658
661 buf_size - offset_ry);
664 buf + offset_gu, buf_size - offset_gu);
667 buf + offset_bv, buf_size - offset_bv);
668 break;
671
674 if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
676 }
677
678 if (offset_ry >= buf_size ||
679 offset_gu >= buf_size ||
680 offset_bv >= buf_size) {
682 "Invalid frame offsets\n");
684 }
685
688 buf_size - offset_ry);
691 buf + offset_gu, buf_size - offset_gu);
694 buf + offset_bv, buf_size - offset_bv);
695 break;
696 default:
698 "Unsupported Lagarith frame type: %#x\n", frametype);
700 }
701
702 *got_frame = 1;
703
704 return buf_size;
705 }
706
708 {
711
713
714 return 0;
715 }
716
718 {
720
722
723 return 0;
724 }
725
736 };