40 #define ALPHA_COMPAND_DC_OFFSET 256
41 #define ALPHA_COMPAND_GAIN 9400
75 s->subband_num_actual = 0;
82 memset(&
s->peak.base, 0,
sizeof(
s->peak.base));
89 s->cropped_height = 0;
94 s->lowpass_precision = 16;
99 s->difference_coding = 0;
108 if (codebook == 0 || codebook == 1) {
111 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
114 return level * quantisation;
116 return level * quantisation;
124 for (j = 1; j <
width; j++) {
125 band[j] += band[j-1];
134 for (
i = 0;
i < length;
i++)
136 band[
i] = bytestream2_get_le16(&peak->
base);
155 const int linesize =
frame->linesize[0];
156 uint16_t *
r = (uint16_t *)
frame->data[0];
157 uint16_t *g1 = (uint16_t *)(
frame->data[0] + 2);
158 uint16_t *g2 = (uint16_t *)(
frame->data[0] +
frame->linesize[0]);
159 uint16_t *
b = (uint16_t *)(
frame->data[0] +
frame->linesize[0] + 2);
160 const int mid = 2048;
162 for (
int y = 0; y <
frame->height >> 1; y++) {
163 for (
int x = 0; x <
frame->width; x += 2) {
173 R = (rg - mid) * 2 +
g;
176 B = (bg - mid) * 2 +
g;
178 R = av_clip_uintp2(
R * 16, 16);
179 G1 = av_clip_uintp2(G1 * 16, 16);
180 G2 = av_clip_uintp2(G2 * 16, 16);
181 B = av_clip_uintp2(
B * 16, 16);
197 int16_t *low, ptrdiff_t low_stride,
198 int16_t *high, ptrdiff_t high_stride,
204 for (
i = 0;
i <
len;
i++) {
206 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
207 output[(2*
i+0)*out_stride] = (
tmp + high[0*high_stride]) >> 1;
211 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
212 output[(2*
i+1)*out_stride] = (
tmp - high[0*high_stride]) >> 1;
215 }
else if (
i ==
len-1) {
216 tmp = ( 5*low[
i*low_stride] + 4*low[(
i-1)*low_stride] - low[(
i-2)*low_stride] + 4) >> 3;
217 output[(2*
i+0)*out_stride] = (
tmp + high[
i*high_stride]) >> 1;
221 tmp = (11*low[
i*low_stride] - 4*low[(
i-1)*low_stride] + low[(
i-2)*low_stride] + 4) >> 3;
222 output[(2*
i+1)*out_stride] = (
tmp - high[
i*high_stride]) >> 1;
226 tmp = (low[(
i-1)*low_stride] - low[(
i+1)*low_stride] + 4) >> 3;
227 output[(2*
i+0)*out_stride] = (
tmp + low[
i*low_stride] + high[
i*high_stride]) >> 1;
231 tmp = (low[(
i+1)*low_stride] - low[(
i-1)*low_stride] + 4) >> 3;
232 output[(2*
i+1)*out_stride] = (
tmp + low[
i*low_stride] - high[
i*high_stride]) >> 1;
240 int width,
int linesize,
int plane)
245 even = (low[
i] - high[
i])/2;
246 odd = (low[
i] + high[
i])/2;
248 output[
i + linesize] = av_clip_uintp2(odd, 10);
270 int16_t *low, ptrdiff_t low_stride,
271 int16_t *high, ptrdiff_t high_stride,
int len)
273 filter(
output, out_stride, low, low_stride, high, high_stride,
len, 0);
284 for (j = 0; j < 9; j++)
285 s->plane[
i].subband[j] =
NULL;
287 for (j = 0; j < 8; j++)
288 s->plane[
i].l_h[j] =
NULL;
298 int chroma_x_shift, chroma_y_shift;
303 s->coded_height *= 2;
312 &chroma_y_shift)) < 0)
322 int w8, h8, w4, h4, w2, h2;
339 s->plane[
i].idwt_buf =
341 s->plane[
i].idwt_tmp =
343 if (!
s->plane[
i].idwt_buf || !
s->plane[
i].idwt_tmp)
346 s->plane[
i].subband[0] =
s->plane[
i].idwt_buf;
347 s->plane[
i].subband[1] =
s->plane[
i].idwt_buf + 2 * w8 * h8;
348 s->plane[
i].subband[2] =
s->plane[
i].idwt_buf + 1 * w8 * h8;
349 s->plane[
i].subband[3] =
s->plane[
i].idwt_buf + 3 * w8 * h8;
350 s->plane[
i].subband[4] =
s->plane[
i].idwt_buf + 2 * w4 * h4;
351 s->plane[
i].subband[5] =
s->plane[
i].idwt_buf + 1 * w4 * h4;
352 s->plane[
i].subband[6] =
s->plane[
i].idwt_buf + 3 * w4 * h4;
353 s->plane[
i].subband[7] =
s->plane[
i].idwt_buf + 2 * w2 * h2;
354 s->plane[
i].subband[8] =
s->plane[
i].idwt_buf + 1 * w2 * h2;
355 s->plane[
i].subband[9] =
s->plane[
i].idwt_buf + 3 * w2 * h2;
359 s->plane[
i].band[j][k].a_width = w8 << j;
360 s->plane[
i].band[j][k].a_height = h8 << j;
365 s->plane[
i].l_h[0] =
s->plane[
i].idwt_tmp;
366 s->plane[
i].l_h[1] =
s->plane[
i].idwt_tmp + 2 * w8 * h8;
368 s->plane[
i].l_h[3] =
s->plane[
i].idwt_tmp;
369 s->plane[
i].l_h[4] =
s->plane[
i].idwt_tmp + 2 * w4 * h4;
371 s->plane[
i].l_h[6] =
s->plane[
i].idwt_tmp;
372 s->plane[
i].l_h[7] =
s->plane[
i].idwt_tmp + 2 * w2 * h2;
375 s->a_height =
s->coded_height;
376 s->a_width =
s->coded_width;
377 s->a_format =
s->coded_format;
389 int ret = 0,
i, j,
planes, plane, got_buffer = 0;
400 uint16_t tagu = bytestream2_get_be16(&gb);
401 int16_t
tag = (int16_t)tagu;
402 int8_t tag8 = (int8_t)(tagu >> 8);
403 uint16_t abstag =
abs(
tag);
404 int8_t abs_tag8 =
abs(tag8);
405 uint16_t
data = bytestream2_get_be16(&gb);
406 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
410 s->progressive =
data & 0x0001;
413 s->coded_width =
data;
416 s->coded_height =
data;
417 }
else if (
tag == 101) {
419 if (data < 1 || data > 31) {
427 s->channel_cnt =
data;
441 s->channel_num =
data;
443 if (
s->channel_num >=
planes) {
450 if (
s->subband_num != 0 &&
data == 1)
453 s->subband_num =
data;
459 if (
s->subband_num > 3) {
464 }
else if (
tag == 51) {
466 s->subband_num_actual =
data;
467 if (
s->subband_num_actual >= 10) {
475 s->quantisation =
data;
478 s->prescale_shift[0] = (
data >> 0) & 0x7;
479 s->prescale_shift[1] = (
data >> 3) & 0x7;
480 s->prescale_shift[2] = (
data >> 6) & 0x7;
482 }
else if (
tag == 27) {
484 if (data < 3 || data >
s->plane[
s->channel_num].band[0][0].a_width) {
489 s->plane[
s->channel_num].band[0][0].width =
data;
490 s->plane[
s->channel_num].band[0][0].stride =
data;
491 }
else if (
tag == 28) {
493 if (data < 3 || data >
s->plane[
s->channel_num].band[0][0].a_height) {
498 s->plane[
s->channel_num].band[0][0].height =
data;
501 else if (
tag == 10) {
506 }
else if (
data == 1) {
512 }
else if (abstag >= 0x4000 && abstag <= 0x40ff) {
513 if (abstag == 0x4001)
517 }
else if (
tag == 23) {
522 }
else if (
tag == 2) {
530 uint16_t tag2 = bytestream2_get_be16(&gb);
531 uint16_t val2 = bytestream2_get_be16(&gb);
534 }
else if (
tag == 41) {
535 av_log(avctx,
AV_LOG_DEBUG,
"Highpass width %i channel %i level %i subband %i\n",
data,
s->channel_num,
s->level,
s->subband_num);
541 s->plane[
s->channel_num].band[
s->level][
s->subband_num].width =
data;
542 s->plane[
s->channel_num].band[
s->level][
s->subband_num].stride =
FFALIGN(
data, 8);
543 }
else if (
tag == 42) {
550 s->plane[
s->channel_num].band[
s->level][
s->subband_num].height =
data;
551 }
else if (
tag == 49) {
558 s->plane[
s->channel_num].band[
s->level][
s->subband_num].width =
data;
559 s->plane[
s->channel_num].band[
s->level][
s->subband_num].stride =
FFALIGN(
data, 8);
560 }
else if (
tag == 50) {
567 s->plane[
s->channel_num].band[
s->level][
s->subband_num].height =
data;
568 }
else if (
tag == 71) {
571 }
else if (
tag == 72) {
572 s->codebook =
data & 0xf;
573 s->difference_coding = (
data >> 4) & 1;
575 }
else if (
tag == 70) {
583 }
else if (
tag == 84) {
587 }
else if (
data == 2) {
589 }
else if (
data == 3) {
591 }
else if (
data == 4) {
599 }
else if (
tag == -85) {
601 s->cropped_height =
data;
602 }
else if (
tag == -75) {
603 s->peak.offset &= ~0xffff;
604 s->peak.offset |= (
data & 0xffff);
607 }
else if (
tag == -76) {
608 s->peak.offset &= 0xffff;
609 s->peak.offset |= (
data & 0xffff
U)<<16;
612 }
else if (
tag == -74 &&
s->peak.offset) {
613 s->peak.level =
data;
625 if (
tag == 4 &&
data == 0x1a4a &&
s->coded_width &&
s->coded_height &&
627 if (
s->a_width !=
s->coded_width ||
s->a_height !=
s->coded_height ||
628 s->a_format !=
s->coded_format) {
638 if (
s->cropped_height) {
655 coeff_data =
s->plane[
s->channel_num].subband[
s->subband_num_actual];
658 if (
tag == 4 &&
data == 0xf0f &&
s->a_width &&
s->a_height) {
659 int lowpass_height =
s->plane[
s->channel_num].band[0][0].height;
660 int lowpass_width =
s->plane[
s->channel_num].band[0][0].width;
661 int lowpass_a_height =
s->plane[
s->channel_num].band[0][0].a_height;
662 int lowpass_a_width =
s->plane[
s->channel_num].band[0][0].a_width;
670 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
677 av_log(avctx,
AV_LOG_DEBUG,
"Start of lowpass coeffs component %d height:%d, width:%d\n",
s->channel_num, lowpass_height, lowpass_width);
678 for (
i = 0;
i < lowpass_height;
i++) {
679 for (j = 0; j < lowpass_width; j++)
680 coeff_data[j] = bytestream2_get_be16u(&gb);
682 coeff_data += lowpass_width;
689 if (lowpass_height & 1) {
690 memcpy(&coeff_data[lowpass_height * lowpass_width],
691 &coeff_data[(lowpass_height - 1) * lowpass_width],
692 lowpass_width *
sizeof(*coeff_data));
695 av_log(avctx,
AV_LOG_DEBUG,
"Lowpass coefficients %d\n", lowpass_width * lowpass_height);
698 if (
tag == 55 &&
s->subband_num_actual != 255 &&
s->a_width &&
s->a_height) {
699 int highpass_height =
s->plane[
s->channel_num].band[
s->level][
s->subband_num].height;
700 int highpass_width =
s->plane[
s->channel_num].band[
s->level][
s->subband_num].width;
701 int highpass_a_width =
s->plane[
s->channel_num].band[
s->level][
s->subband_num].a_width;
702 int highpass_a_height =
s->plane[
s->channel_num].band[
s->level][
s->subband_num].a_height;
703 int highpass_stride =
s->plane[
s->channel_num].band[
s->level][
s->subband_num].stride;
705 int a_expected = highpass_a_height * highpass_a_width;
707 int count = 0, bytes;
715 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
720 expected = highpass_height * highpass_stride;
722 av_log(avctx,
AV_LOG_DEBUG,
"Start subband coeffs plane %i level %i codebook %i expected %i\n",
s->channel_num,
s->level,
s->codebook, expected);
739 if (count > expected)
744 *coeff_data++ =
coeff;
758 if (count > expected)
763 *coeff_data++ =
coeff;
769 if (count > expected) {
776 if (
s->difference_coding)
777 difference_coding(
s->plane[
s->channel_num].subband[
s->subband_num_actual], highpass_width, highpass_height);
787 av_log(avctx,
AV_LOG_DEBUG,
"End subband coeffs %i extra %i\n", count, count - expected);
791 if (highpass_height & 1) {
792 memcpy(&coeff_data[highpass_height * highpass_stride],
793 &coeff_data[(highpass_height - 1) * highpass_stride],
794 highpass_stride *
sizeof(*coeff_data));
819 for (plane = 0; plane <
planes && !
ret; plane++) {
821 int lowpass_height =
s->plane[plane].band[0][0].height;
822 int lowpass_width =
s->plane[plane].band[0][0].width;
823 int highpass_stride =
s->plane[plane].band[0][1].stride;
824 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
825 ptrdiff_t dst_linesize;
826 int16_t *low, *high, *
output, *dst;
830 dst_linesize = pic->
linesize[act_plane];
832 dst_linesize = pic->
linesize[act_plane] / 2;
835 if (lowpass_height >
s->plane[plane].band[0][0].a_height || lowpass_width >
s->plane[plane].band[0][0].a_width ||
836 !highpass_stride ||
s->plane[plane].band[0][1].width >
s->plane[plane].band[0][1].a_width) {
842 av_log(avctx,
AV_LOG_DEBUG,
"Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
844 low =
s->plane[plane].subband[0];
845 high =
s->plane[plane].subband[2];
846 output =
s->plane[plane].l_h[0];
847 for (
i = 0;
i < lowpass_width;
i++) {
848 vert_filter(
output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
854 low =
s->plane[plane].subband[1];
855 high =
s->plane[plane].subband[3];
856 output =
s->plane[plane].l_h[1];
858 for (
i = 0;
i < lowpass_width;
i++) {
860 vert_filter(
output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
866 low =
s->plane[plane].l_h[0];
867 high =
s->plane[plane].l_h[1];
868 output =
s->plane[plane].subband[0];
869 for (
i = 0;
i < lowpass_height * 2;
i++) {
871 low += lowpass_width;
872 high += lowpass_width;
873 output += lowpass_width * 2;
876 output =
s->plane[plane].subband[0];
877 for (
i = 0;
i < lowpass_height * 2;
i++) {
878 for (j = 0; j < lowpass_width * 2; j++)
881 output += lowpass_width * 2;
886 lowpass_height =
s->plane[plane].band[1][1].height;
887 lowpass_width =
s->plane[plane].band[1][1].width;
888 highpass_stride =
s->plane[plane].band[1][1].stride;
890 if (lowpass_height >
s->plane[plane].band[1][1].a_height || lowpass_width >
s->plane[plane].band[1][1].a_width ||
891 !highpass_stride ||
s->plane[plane].band[1][1].width >
s->plane[plane].band[1][1].a_width) {
897 av_log(avctx,
AV_LOG_DEBUG,
"Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
899 low =
s->plane[plane].subband[0];
900 high =
s->plane[plane].subband[5];
901 output =
s->plane[plane].l_h[3];
902 for (
i = 0;
i < lowpass_width;
i++) {
903 vert_filter(
output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
909 low =
s->plane[plane].subband[4];
910 high =
s->plane[plane].subband[6];
911 output =
s->plane[plane].l_h[4];
912 for (
i = 0;
i < lowpass_width;
i++) {
913 vert_filter(
output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
919 low =
s->plane[plane].l_h[3];
920 high =
s->plane[plane].l_h[4];
921 output =
s->plane[plane].subband[0];
922 for (
i = 0;
i < lowpass_height * 2;
i++) {
924 low += lowpass_width;
925 high += lowpass_width;
926 output += lowpass_width * 2;
929 output =
s->plane[plane].subband[0];
930 for (
i = 0;
i < lowpass_height * 2;
i++) {
931 for (j = 0; j < lowpass_width * 2; j++)
934 output += lowpass_width * 2;
938 lowpass_height =
s->plane[plane].band[2][1].height;
939 lowpass_width =
s->plane[plane].band[2][1].width;
940 highpass_stride =
s->plane[plane].band[2][1].stride;
942 if (lowpass_height >
s->plane[plane].band[2][1].a_height || lowpass_width >
s->plane[plane].band[2][1].a_width ||
943 !highpass_stride ||
s->plane[plane].band[2][1].width >
s->plane[plane].band[2][1].a_width) {
949 av_log(avctx,
AV_LOG_DEBUG,
"Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
950 if (
s->progressive) {
951 low =
s->plane[plane].subband[0];
952 high =
s->plane[plane].subband[8];
953 output =
s->plane[plane].l_h[6];
954 for (
i = 0;
i < lowpass_width;
i++) {
955 vert_filter(
output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
961 low =
s->plane[plane].subband[7];
962 high =
s->plane[plane].subband[9];
963 output =
s->plane[plane].l_h[7];
964 for (
i = 0;
i < lowpass_width;
i++) {
965 vert_filter(
output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
971 dst = (int16_t *)pic->
data[act_plane];
976 dst += pic->
linesize[act_plane] >> 1;
978 low =
s->plane[plane].l_h[6];
979 high =
s->plane[plane].l_h[7];
989 for (
i = 0;
i < lowpass_height * 2;
i++) {
996 low += lowpass_width;
997 high += lowpass_width;
1003 low =
s->plane[plane].subband[0];
1004 high =
s->plane[plane].subband[7];
1005 output =
s->plane[plane].l_h[6];
1006 for (
i = 0;
i < lowpass_height;
i++) {
1008 low += lowpass_width;
1009 high += lowpass_width;
1010 output += lowpass_width * 2;
1013 low =
s->plane[plane].subband[8];
1014 high =
s->plane[plane].subband[9];
1015 output =
s->plane[plane].l_h[7];
1016 for (
i = 0;
i < lowpass_height;
i++) {
1018 low += lowpass_width;
1019 high += lowpass_width;
1020 output += lowpass_width * 2;
1023 dst = (int16_t *)pic->
data[act_plane];
1024 low =
s->plane[plane].l_h[6];
1025 high =
s->plane[plane].l_h[7];
1026 for (
i = 0;
i < lowpass_height;
i++) {
1028 low += lowpass_width * 2;
1029 high += lowpass_width * 2;