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];
 
  136             band[
i] = bytestream2_get_le16(&peak->
base);
 
  154                           int16_t *low, ptrdiff_t low_stride,
 
  155                           int16_t *high, ptrdiff_t high_stride,
 
  161     for (
i = 0; 
i < 
len; 
i++) {
 
  163             tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
 
  164             output[(2*
i+0)*out_stride] = (
tmp + high[0*high_stride]) >> 1;
 
  168             tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
 
  169             output[(2*
i+1)*out_stride] = (
tmp - high[0*high_stride]) >> 1;
 
  172         } 
else if (
i == 
len-1) {
 
  173             tmp = ( 5*low[
i*low_stride] + 4*low[(
i-1)*low_stride] - low[(
i-2)*low_stride] + 4) >> 3;
 
  174             output[(2*
i+0)*out_stride] = (
tmp + high[
i*high_stride]) >> 1;
 
  178             tmp = (11*low[
i*low_stride] - 4*low[(
i-1)*low_stride] + low[(
i-2)*low_stride] + 4) >> 3;
 
  179             output[(2*
i+1)*out_stride] = (
tmp - high[
i*high_stride]) >> 1;
 
  183             tmp = (low[(
i-1)*low_stride] - low[(
i+1)*low_stride] + 4) >> 3;
 
  184             output[(2*
i+0)*out_stride] = (
tmp + low[
i*low_stride] + high[
i*high_stride]) >> 1;
 
  188             tmp = (low[(
i+1)*low_stride] - low[(
i-1)*low_stride] + 4) >> 3;
 
  189             output[(2*
i+1)*out_stride] = (
tmp + low[
i*low_stride] - high[
i*high_stride]) >> 1;
 
  202         even = (low[
i] - high[
i])/2;
 
  203         odd  = (low[
i] + high[
i])/2;
 
  205         output[
i + linesize] = av_clip_uintp2(odd, 10);
 
  221                         int16_t *low, ptrdiff_t low_stride,
 
  222                         int16_t *high, ptrdiff_t high_stride, 
int len)
 
  224     filter(
output, out_stride, low, low_stride, high, high_stride, 
len, 0);
 
  235         for (j = 0; j < 9; j++)
 
  236             s->plane[
i].subband[j] = 
NULL;
 
  238         for (j = 0; j < 8; j++)
 
  239             s->plane[
i].l_h[j] = 
NULL;
 
  249     int chroma_x_shift, chroma_y_shift;
 
  258                                                 &chroma_y_shift)) < 0)
 
  263         int w8, h8, w4, h4, w2, h2;
 
  280         s->plane[
i].idwt_buf =
 
  282         s->plane[
i].idwt_tmp =
 
  284         if (!
s->plane[
i].idwt_buf || !
s->plane[
i].idwt_tmp)
 
  287         s->plane[
i].subband[0] = 
s->plane[
i].idwt_buf;
 
  288         s->plane[
i].subband[1] = 
s->plane[
i].idwt_buf + 2 * w8 * h8;
 
  289         s->plane[
i].subband[2] = 
s->plane[
i].idwt_buf + 1 * w8 * h8;
 
  290         s->plane[
i].subband[3] = 
s->plane[
i].idwt_buf + 3 * w8 * h8;
 
  291         s->plane[
i].subband[4] = 
s->plane[
i].idwt_buf + 2 * w4 * h4;
 
  292         s->plane[
i].subband[5] = 
s->plane[
i].idwt_buf + 1 * w4 * h4;
 
  293         s->plane[
i].subband[6] = 
s->plane[
i].idwt_buf + 3 * w4 * h4;
 
  294         s->plane[
i].subband[7] = 
s->plane[
i].idwt_buf + 2 * w2 * h2;
 
  295         s->plane[
i].subband[8] = 
s->plane[
i].idwt_buf + 1 * w2 * h2;
 
  296         s->plane[
i].subband[9] = 
s->plane[
i].idwt_buf + 3 * w2 * h2;
 
  300                 s->plane[
i].band[j][k].a_width  = w8 << j;
 
  301                 s->plane[
i].band[j][k].a_height = h8 << j;
 
  306         s->plane[
i].l_h[0] = 
s->plane[
i].idwt_tmp;
 
  307         s->plane[
i].l_h[1] = 
s->plane[
i].idwt_tmp + 2 * w8 * h8;
 
  309         s->plane[
i].l_h[3] = 
s->plane[
i].idwt_tmp;
 
  310         s->plane[
i].l_h[4] = 
s->plane[
i].idwt_tmp + 2 * w4 * h4;
 
  312         s->plane[
i].l_h[6] = 
s->plane[
i].idwt_tmp;
 
  313         s->plane[
i].l_h[7] = 
s->plane[
i].idwt_tmp + 2 * w2 * h2;
 
  316     s->a_height = 
s->coded_height;
 
  317     s->a_width  = 
s->coded_width;
 
  318     s->a_format = 
s->coded_format;
 
  341         uint16_t tagu   = bytestream2_get_be16(&gb);
 
  342         int16_t 
tag     = (int16_t)tagu;
 
  343         int8_t tag8     = (int8_t)(tagu >> 8);
 
  344         uint16_t abstag = 
abs(
tag);
 
  345         int8_t abs_tag8 = 
abs(tag8);
 
  346         uint16_t 
data   = bytestream2_get_be16(&gb);
 
  347         if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
 
  351             s->progressive = 
data & 0x0001;
 
  354             s->coded_width = 
data;
 
  357             s->coded_height = 
data;
 
  358         } 
else if (
tag == 101) {
 
  360             if (data < 1 || data > 31) {
 
  368             s->channel_cnt = 
data;
 
  382             s->channel_num = 
data;
 
  384             if (
s->channel_num >= 
planes) {
 
  391             if (
s->subband_num != 0 && 
data == 1)  
 
  394             s->subband_num = 
data;
 
  400             if (
s->subband_num > 3) {
 
  405         } 
else if (
tag == 51) {
 
  407             s->subband_num_actual = 
data;
 
  408             if (
s->subband_num_actual >= 10) {
 
  416             s->quantisation = 
data;
 
  419             s->prescale_shift[0] = (
data >> 0) & 0x7;
 
  420             s->prescale_shift[1] = (
data >> 3) & 0x7;
 
  421             s->prescale_shift[2] = (
data >> 6) & 0x7;
 
  423         } 
else if (
tag == 27) {
 
  425             if (data < 3 || data > 
s->plane[
s->channel_num].band[0][0].a_width) {
 
  430             s->plane[
s->channel_num].band[0][0].width  = 
data;
 
  431             s->plane[
s->channel_num].band[0][0].stride = 
data;
 
  432         } 
else if (
tag == 28) {
 
  434             if (data < 3 || data > 
s->plane[
s->channel_num].band[0][0].a_height) {
 
  439             s->plane[
s->channel_num].band[0][0].height = 
data;
 
  442         else if (
tag == 10) {
 
  447             } 
else if (
data == 1) {
 
  453         } 
else if (abstag >= 0x4000 && abstag <= 0x40ff) {
 
  454             if (abstag == 0x4001)
 
  458         } 
else if (
tag == 23) {
 
  463         } 
else if (
tag == 2) {
 
  471                 uint16_t tag2 = bytestream2_get_be16(&gb);
 
  472                 uint16_t val2 = bytestream2_get_be16(&gb);
 
  475         } 
else if (
tag == 41) {
 
  476             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);
 
  482             s->plane[
s->channel_num].band[
s->level][
s->subband_num].width  = 
data;
 
  483             s->plane[
s->channel_num].band[
s->level][
s->subband_num].stride = 
FFALIGN(
data, 8);
 
  484         } 
else if (
tag == 42) {
 
  491             s->plane[
s->channel_num].band[
s->level][
s->subband_num].height = 
data;
 
  492         } 
else if (
tag == 49) {
 
  499             s->plane[
s->channel_num].band[
s->level][
s->subband_num].width  = 
data;
 
  500             s->plane[
s->channel_num].band[
s->level][
s->subband_num].stride = 
FFALIGN(
data, 8);
 
  501         } 
else if (
tag == 50) {
 
  508             s->plane[
s->channel_num].band[
s->level][
s->subband_num].height = 
data;
 
  509         } 
else if (
tag == 71) {
 
  512         } 
else if (
tag == 72) {
 
  513             s->codebook = 
data & 0xf;
 
  514             s->difference_coding = (
data >> 4) & 1;
 
  516         } 
else if (
tag == 70) {
 
  524         } 
else if (
tag == 84) {
 
  538         } 
else if (
tag == -85) {
 
  540             s->cropped_height = 
data;
 
  541         } 
else if (
tag == -75) {
 
  542             s->peak.offset &= ~0xffff;
 
  543             s->peak.offset |= (
data & 0xffff);
 
  546         } 
else if (
tag == -76) {
 
  547             s->peak.offset &= 0xffff;
 
  548             s->peak.offset |= (
data & 0xffff
U)<<16;
 
  551         } 
else if (
tag == -74 && 
s->peak.offset) {
 
  552             s->peak.level = 
data;
 
  564         if (
tag == 4 && 
data == 0x1a4a && 
s->coded_width && 
s->coded_height &&
 
  566             if (
s->a_width != 
s->coded_width || 
s->a_height != 
s->coded_height ||
 
  567                 s->a_format != 
s->coded_format) {
 
  577             if (
s->cropped_height)
 
  578                 avctx->
height = 
s->cropped_height;
 
  590         coeff_data = 
s->plane[
s->channel_num].subband[
s->subband_num_actual];
 
  593         if (
tag == 4 && 
data == 0xf0f && 
s->a_width && 
s->a_height) {
 
  594             int lowpass_height = 
s->plane[
s->channel_num].band[0][0].height;
 
  595             int lowpass_width  = 
s->plane[
s->channel_num].band[0][0].width;
 
  596             int lowpass_a_height = 
s->plane[
s->channel_num].band[0][0].a_height;
 
  597             int lowpass_a_width  = 
s->plane[
s->channel_num].band[0][0].a_width;
 
  605             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
 
  612             av_log(avctx, 
AV_LOG_DEBUG, 
"Start of lowpass coeffs component %d height:%d, width:%d\n", 
s->channel_num, lowpass_height, lowpass_width);
 
  613             for (
i = 0; 
i < lowpass_height; 
i++) {
 
  614                 for (j = 0; j < lowpass_width; j++)
 
  615                     coeff_data[j] = bytestream2_get_be16u(&gb);
 
  617                 coeff_data += lowpass_width;
 
  624             if (lowpass_height & 1) {
 
  625                 memcpy(&coeff_data[lowpass_height * lowpass_width],
 
  626                        &coeff_data[(lowpass_height - 1) * lowpass_width],
 
  627                        lowpass_width * 
sizeof(*coeff_data));
 
  630             av_log(avctx, 
AV_LOG_DEBUG, 
"Lowpass coefficients %d\n", lowpass_width * lowpass_height);
 
  633         if (
tag == 55 && 
s->subband_num_actual != 255 && 
s->a_width && 
s->a_height) {
 
  634             int highpass_height = 
s->plane[
s->channel_num].band[
s->level][
s->subband_num].height;
 
  635             int highpass_width  = 
s->plane[
s->channel_num].band[
s->level][
s->subband_num].width;
 
  636             int highpass_a_width = 
s->plane[
s->channel_num].band[
s->level][
s->subband_num].a_width;
 
  637             int highpass_a_height = 
s->plane[
s->channel_num].band[
s->level][
s->subband_num].a_height;
 
  638             int highpass_stride = 
s->plane[
s->channel_num].band[
s->level][
s->subband_num].stride;
 
  640             int a_expected = highpass_a_height * highpass_a_width;
 
  642             int count = 0, bytes;
 
  650             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
 
  655             expected = highpass_height * highpass_stride;
 
  657             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);
 
  674                         if (
count > expected)
 
  679                             *coeff_data++ = 
coeff;
 
  693                         if (
count > expected)
 
  698                             *coeff_data++ = 
coeff;
 
  704             if (
count > expected) {
 
  711             if (
s->difference_coding)
 
  712                 difference_coding(
s->plane[
s->channel_num].subband[
s->subband_num_actual], highpass_width, highpass_height);
 
  726             if (highpass_height & 1) {
 
  727                 memcpy(&coeff_data[highpass_height * highpass_stride],
 
  728                        &coeff_data[(highpass_height - 1) * highpass_stride],
 
  729                        highpass_stride * 
sizeof(*coeff_data));
 
  750         int lowpass_height  = 
s->plane[
plane].band[0][0].height;
 
  751         int lowpass_width   = 
s->plane[
plane].band[0][0].width;
 
  752         int highpass_stride = 
s->plane[
plane].band[0][1].stride;
 
  754         int16_t *low, *high, *
output, *dst;
 
  756         if (lowpass_height > 
s->plane[
plane].band[0][0].a_height || lowpass_width > 
s->plane[
plane].band[0][0].a_width ||
 
  757             !highpass_stride || 
s->plane[
plane].band[0][1].width > 
s->plane[
plane].band[0][1].a_width) {
 
  763         av_log(avctx, 
AV_LOG_DEBUG, 
"Decoding level 1 plane %i %i %i %i\n", 
plane, lowpass_height, lowpass_width, highpass_stride);
 
  765         low    = 
s->plane[
plane].subband[0];
 
  766         high   = 
s->plane[
plane].subband[2];
 
  768         for (
i = 0; 
i < lowpass_width; 
i++) {
 
  769             vert_filter(
output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
 
  775         low    = 
s->plane[
plane].subband[1];
 
  776         high   = 
s->plane[
plane].subband[3];
 
  779         for (
i = 0; 
i < lowpass_width; 
i++) {
 
  781             vert_filter(
output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
 
  787         low    = 
s->plane[
plane].l_h[0];
 
  788         high   = 
s->plane[
plane].l_h[1];
 
  790         for (
i = 0; 
i < lowpass_height * 2; 
i++) {
 
  792             low    += lowpass_width;
 
  793             high   += lowpass_width;
 
  794             output += lowpass_width * 2;
 
  798             for (
i = 0; 
i < lowpass_height * 2; 
i++) {
 
  799                 for (j = 0; j < lowpass_width * 2; j++)
 
  802                 output += lowpass_width * 2;
 
  807         lowpass_height  = 
s->plane[
plane].band[1][1].height;
 
  808         lowpass_width   = 
s->plane[
plane].band[1][1].width;
 
  809         highpass_stride = 
s->plane[
plane].band[1][1].stride;
 
  811         if (lowpass_height > 
s->plane[
plane].band[1][1].a_height || lowpass_width > 
s->plane[
plane].band[1][1].a_width ||
 
  812             !highpass_stride || 
s->plane[
plane].band[1][1].width > 
s->plane[
plane].band[1][1].a_width) {
 
  818         av_log(avctx, 
AV_LOG_DEBUG, 
"Level 2 plane %i %i %i %i\n", 
plane, lowpass_height, lowpass_width, highpass_stride);
 
  820         low    = 
s->plane[
plane].subband[0];
 
  821         high   = 
s->plane[
plane].subband[5];
 
  823         for (
i = 0; 
i < lowpass_width; 
i++) {
 
  824             vert_filter(
output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
 
  830         low    = 
s->plane[
plane].subband[4];
 
  831         high   = 
s->plane[
plane].subband[6];
 
  833         for (
i = 0; 
i < lowpass_width; 
i++) {
 
  834             vert_filter(
output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
 
  840         low    = 
s->plane[
plane].l_h[3];
 
  841         high   = 
s->plane[
plane].l_h[4];
 
  843         for (
i = 0; 
i < lowpass_height * 2; 
i++) {
 
  845             low    += lowpass_width;
 
  846             high   += lowpass_width;
 
  847             output += lowpass_width * 2;
 
  851         for (
i = 0; 
i < lowpass_height * 2; 
i++) {
 
  852             for (j = 0; j < lowpass_width * 2; j++)
 
  855             output += lowpass_width * 2;
 
  859         lowpass_height  = 
s->plane[
plane].band[2][1].height;
 
  860         lowpass_width   = 
s->plane[
plane].band[2][1].width;
 
  861         highpass_stride = 
s->plane[
plane].band[2][1].stride;
 
  863         if (lowpass_height > 
s->plane[
plane].band[2][1].a_height || lowpass_width > 
s->plane[
plane].band[2][1].a_width ||
 
  864             !highpass_stride || 
s->plane[
plane].band[2][1].width > 
s->plane[
plane].band[2][1].a_width) {
 
  870         av_log(avctx, 
AV_LOG_DEBUG, 
"Level 3 plane %i %i %i %i\n", 
plane, lowpass_height, lowpass_width, highpass_stride);
 
  871         if (
s->progressive) {
 
  872             low    = 
s->plane[
plane].subband[0];
 
  873             high   = 
s->plane[
plane].subband[8];
 
  875             for (
i = 0; 
i < lowpass_width; 
i++) {
 
  876                 vert_filter(
output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
 
  882             low    = 
s->plane[
plane].subband[7];
 
  883             high   = 
s->plane[
plane].subband[9];
 
  885             for (
i = 0; 
i < lowpass_width; 
i++) {
 
  886                 vert_filter(
output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
 
  892             dst = (int16_t *)pic->
data[act_plane];
 
  893             low  = 
s->plane[
plane].l_h[6];
 
  894             high = 
s->plane[
plane].l_h[7];
 
  895             for (
i = 0; 
i < lowpass_height * 2; 
i++) {
 
  899                 low  += lowpass_width;
 
  900                 high += lowpass_width;
 
  901                 dst  += pic->
linesize[act_plane] / 2;
 
  906             low    = 
s->plane[
plane].subband[0];
 
  907             high   = 
s->plane[
plane].subband[7];
 
  909             for (
i = 0; 
i < lowpass_height; 
i++) {
 
  911                 low    += lowpass_width;
 
  912                 high   += lowpass_width;
 
  913                 output += lowpass_width * 2;
 
  916             low    = 
s->plane[
plane].subband[8];
 
  917             high   = 
s->plane[
plane].subband[9];
 
  919             for (
i = 0; 
i < lowpass_height; 
i++) {
 
  921                 low    += lowpass_width;
 
  922                 high   += lowpass_width;
 
  923                 output += lowpass_width * 2;
 
  926             dst  = (int16_t *)pic->
data[act_plane];
 
  927             low  = 
s->plane[
plane].l_h[6];
 
  928             high = 
s->plane[
plane].l_h[7];
 
  929             for (
i = 0; 
i < lowpass_height; 
i++) {
 
  931                 low  += lowpass_width * 2;
 
  932                 high += lowpass_width * 2;