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
28 #include <inttypes.h>
29
36
44 FRAME_OLD_ARITH_RGB = 7,
/**< obsolete arithmetic coded RGB (no longer encoded by upstream since version 1.1.0) */
49 };
50
54 int zeros;
/**< number of consecutive zero bytes encountered */
55 int zeros_rem;
/**< number of zero bytes remaining to output */
60
61 /**
62 * Compute the 52bit mantissa of 1/(double)denom.
63 * This crazy format uses floats in an entropy coder and we have to match x86
64 * rounding exactly, thus ordinary floats aren't portable enough.
65 * @param denom denominator
66 * @return 52bit mantissa
67 * @see softfloat_mul
68 */
70 {
72 uint64_t
ret = (1ULL << 52) / denom;
73 uint64_t err = (1ULL << 52) - ret * denom;
76 err += denom / 2;
77 return ret + err / denom;
78 }
79
80 /**
81 * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
82 * Used in combination with softfloat_reciprocal computes x/(double)denom.
83 * @param x 32bit integer factor
84 * @param mantissa mantissa of f with exponent 0
85 * @return 32bit integer value (x*f)
86 * @see softfloat_reciprocal
87 */
89 {
90 uint64_t l = x * (mantissa & 0xffffffff);
91 uint64_t h = x * (mantissa >> 32);
92 h += l >> 32;
93 l &= 0xffffffff;
95 h += l >> 32;
96 return h >> 20;
97 }
98
100 {
101 return (x << 1) ^ (x >> 7);
102 }
103
105 {
106 static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
107 int i;
108 int bit = 0;
110 int prevbit = 0;
112
113 for (i = 0; i < 7; i++) {
114 if (prevbit && bit)
115 break;
116 prevbit = bit;
118 if (bit && !prevbit)
119 bits += series[i];
120 }
121 bits--;
122 if (bits < 0 || bits > 31) {
123 *value = 0;
124 return -1;
125 } else if (bits == 0) {
126 *value = 0;
127 return 0;
128 }
129
132
133 *value = val - 1;
134
135 return 0;
136 }
137
139 {
140 int i, j, scale_factor;
141 unsigned prob, cumulative_target;
142 unsigned cumul_prob = 0;
143 unsigned scaled_cumul_prob = 0;
144
146 rac->
prob[257] = UINT_MAX;
147 /* Read probabilities from bitstream */
148 for (i = 1; i < 257; i++) {
151 return -1;
152 }
153 if ((uint64_t)cumul_prob + rac->
prob[i] > UINT_MAX) {
155 return -1;
156 }
157 cumul_prob += rac->
prob[i];
161 return -1;
162 }
163 if (prob > 256 - i)
164 prob = 256 - i;
165 for (j = 0; j < prob; j++)
167 }
168 }
169
170 if (!cumul_prob) {
172 return -1;
173 }
174
175 /* Scale probabilities so cumulative probability is an even power of 2. */
176 scale_factor =
av_log2(cumul_prob);
177
178 if (cumul_prob & (cumul_prob - 1)) {
180 for (i = 1; i <= 128; i++) {
182 scaled_cumul_prob += rac->
prob[i];
183 }
184 if (scaled_cumul_prob <= 0) {
187 }
188 for (; i < 257; i++) {
190 scaled_cumul_prob += rac->
prob[i];
191 }
192
193 scale_factor++;
194 cumulative_target = 1 << scale_factor;
195
196 if (scaled_cumul_prob > cumulative_target) {
198 "Scaled probabilities are larger than target!\n");
199 return -1;
200 }
201
202 scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
203
204 for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
207 scaled_cumul_prob--;
208 }
209 /* Comment from reference source:
210 * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way
211 * // since the compression change is negligible and fixing it
212 * // breaks backwards compatibility
213 * b =- (signed int)b;
214 * b &= 0xFF;
215 * } else {
216 * b++;
217 * b &= 0x7f;
218 * }
219 */
220 }
221 }
222
223 rac->
scale = scale_factor;
224
225 /* Fill probability array with cumulative probability for each symbol. */
226 for (i = 1; i < 257; i++)
228
229 return 0;
230 }
231
234 int *left_top)
235 {
236 /* This is almost identical to add_hfyu_median_pred in huffyuvdsp.h.
237 * However the &0xFF on the gradient predictor yealds incorrect output
238 * for lagarith.
239 */
240 int i;
242
243 l = *left;
244 lt = *left_top;
245
246 for (i = 0; i < w; i++) {
247 l =
mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
248 lt = src1[i];
249 dst[i] = l;
250 }
251
252 *left = l;
253 *left_top = lt;
254 }
255
258 {
260
261 if (!line) {
262 /* Left prediction only for first line */
264 } else {
265 /* Left pixel is actually prev_row[width] */
266 L = buf[width - stride - 1];
267
268 if (line == 1) {
269 /* Second line, left predict first pixel, the rest of the line is median predicted
270 * NOTE: In the case of RGB this pixel is top predicted */
272 } else {
273 /* Top left is 2 rows back, last pixel */
274 TL = buf[width - (2 * stride) - 1];
275 }
276
278 width, &L, &TL);
279 }
280 }
281
284 int is_luma)
285 {
287
288 if (!line) {
289 L= buf[0];
290 if (is_luma)
291 buf[0] = 0;
293 if (is_luma)
295 return;
296 }
297 if (line == 1) {
298 const int HEAD = is_luma ? 4 : 2;
299 int i;
300
301 L = buf[width - stride - 1];
302 TL = buf[HEAD - stride - 1];
303 for (i = 0; i < HEAD; i++) {
304 L += buf[i];
306 }
307 for (; i <
width; i++) {
308 L =
mid_pred(L & 0xFF, buf[i - stride], (L + buf[i - stride] - TL) & 0xFF) + buf[i];
309 TL = buf[i - stride];
311 }
312 } else {
313 TL = buf[width - (2 * stride) - 1];
314 L = buf[width - stride - 1];
316 }
317 }
318
321 int esc_count)
322 {
323 int i = 0;
325
326 if (!esc_count)
327 esc_count = -1;
328
329 /* Output any zeros remaining from the previous run */
330 handle_zeros:
333 memset(dst + i, 0, count);
336 }
337
338 while (i < width) {
340 ret++;
341
342 if (dst[i])
344 else
346
347 i++;
348 if (l->
zeros == esc_count) {
350 ret++;
351
353
355 goto handle_zeros;
356 }
357 }
359 }
360
363 int width,
int esc_count)
364 {
365 int i = 0;
369 uint8_t mask1 = -(esc_count < 2);
370 uint8_t mask2 = -(esc_count < 3);
372
374
375 memset(dst, 0, width);
376
377 output_zeros:
380 if (end - dst < count) {
383 }
384
385 memset(dst, 0, count);
388 }
389
390 while (dst < end) {
391 i = 0;
392 while (!zero_run && dst + i < end) {
393 i++;
394 if (i+2 >= src_end - src)
396 zero_run =
397 !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
398 }
399 if (zero_run) {
400 zero_run = 0;
401 i += esc_count;
402 memcpy(dst, src, i);
403 dst += i;
405
406 src += i + 1;
407 goto output_zeros;
408 } else {
409 memcpy(dst, src, i);
410 src += i;
411 dst += i;
412 }
413 }
414 return src - src_start;
415 }
416
417
418
422 {
423 int i = 0;
424 int read = 0;
427 int esc_count;
430 const uint8_t *src_end = src + src_size;
432
435
436 if(src_size < 2)
438
439 esc_count = src[0];
440 if (esc_count < 4) {
442 if(src_size < 5)
444 if (esc_count &&
AV_RL32(src + 1) < length) {
446 offset += 4;
447 }
448
449 if ((ret =
init_get_bits8(&gb, src + offset, src_size - offset)) < 0)
451
453 return -1;
454
456
457 for (i = 0; i <
height; i++)
459 stride, esc_count);
460
461 if (read > length)
463 "Output more bytes than length (%d of %"PRIu32")\n", read,
464 length);
465 } else if (esc_count < 8) {
466 esc_count -= 4;
467 src ++;
468 src_size --;
469 if (esc_count > 0) {
470 /* Zero run coding only, no range coding. */
471 for (i = 0; i <
height; i++) {
473 src_end, width, esc_count);
474 if (res < 0)
475 return res;
476 src += res;
477 }
478 } else {
479 if (src_size < width * height)
481 /* Plane is stored uncompressed */
482 for (i = 0; i <
height; i++) {
483 memcpy(dst + (i * stride), src, width);
485 }
486 }
487 } else if (esc_count == 0xff) {
488 /* Plane is a solid run of given value */
489 for (i = 0; i <
height; i++)
490 memset(dst + i * stride, src[1], width);
491 /* Do not apply prediction.
492 Note: memset to 0 above, setting first value to src[1]
493 and applying prediction gives the same result. */
494 return 0;
495 } else {
497 "Invalid zero run escape code! (%#x)\n", esc_count);
498 return -1;
499 }
500
502 for (i = 0; i <
height; i++) {
504 dst += stride;
505 }
506 } else {
507 for (i = 0; i <
height; i++) {
510 dst += stride;
511 }
512 }
513
514 return 0;
515 }
516
517 /**
518 * Decode a frame.
519 * @param avctx codec context
520 * @param data output AVFrame
521 * @param data_size size of output data or 0 if no picture is returned
522 * @param avpkt input packet
523 * @return number of consumed bytes on success or negative if decode fails
524 */
527 {
529 unsigned int buf_size = avpkt->
size;
534 uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
535 uint32_t offs[4];
537 int i, j, planes = 3;
539
541
542 frametype = buf[0];
543
546
547 switch (frametype) {
554 } else {
556 planes = 4;
557 }
558
561
564 for (j = 0; j < avctx->
height; j++) {
565 for (i = 0; i < avctx->
width; i++)
566 AV_WN32(dst + i * 4, offset_gu);
568 }
569 } else {
570 for (j = 0; j < avctx->
height; j++) {
571 memset(dst, buf[1], avctx->
width * planes);
573 }
574 }
575 break;
579 } else {
581 offset_gu |= 0xFF
U << 24;
582 }
583
586
588 for (j = 0; j < avctx->
height; j++) {
589 for (i = 0; i < avctx->
width; i++)
591 AV_WB24(dst + i * 3, offset_gu);
592 } else {
593 AV_WN32(dst + i * 4, offset_gu);
594 }
596 }
597 break;
600 planes = 4;
601 offset_ry += 4;
607
610
611 offs[0] = offset_bv;
612 offs[1] = offset_gu;
613 offs[2] = offset_ry;
614
621 }
622 for (i = 0; i < planes; i++)
624 for (i = 0; i < planes; i++)
625 if (buf_size <= offs[i]) {
627 "Invalid frame offsets\n");
629 }
630
631 for (i = 0; i < planes; i++)
635 buf_size - offs[i]);
637 for (i = 0; i < planes; i++)
640 for (i = 0; i < avctx->
width; i++) {
642 r = srcs[0][i];
643 g = srcs[1][i];
644 b = srcs[2][i];
648 a = srcs[3][i];
650 } else {
654 }
655 }
657 for (i = 0; i < planes; i++)
659 }
660 break;
663
666
667 if (offset_ry >= buf_size ||
668 offset_gu >= buf_size ||
669 offset_bv >= buf_size) {
671 "Invalid frame offsets\n");
673 }
674
677 buf_size - offset_ry);
680 buf + offset_gu, buf_size - offset_gu);
683 buf + offset_bv, buf_size - offset_bv);
684 break;
687
690 if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
692 }
693
694 if (offset_ry >= buf_size ||
695 offset_gu >= buf_size ||
696 offset_bv >= buf_size) {
698 "Invalid frame offsets\n");
700 }
701
704 buf_size - offset_ry);
707 buf + offset_gu, buf_size - offset_gu);
710 buf + offset_bv, buf_size - offset_bv);
711 break;
712 default:
714 "Unsupported Lagarith frame type: %#"PRIx8"\n", frametype);
716 }
717
718 *got_frame = 1;
719
720 return buf_size;
721 }
722
724 {
727
729
730 return 0;
731 }
732
734 {
736
738
739 return 0;
740 }
741
752 };