[FFmpeg-devel,GSOC] lavc/cfhd:IP frame decoding (inverse 3d transform) introduced

Submitted by Gagandeep Singh on May 31, 2018, 11:25 a.m.

Details

Message ID 20180531112522.14212-1-deepgagan231197@gmail.com
State New
Headers show

Commit Message

Gagandeep Singh May 31, 2018, 11:25 a.m.
---
 libavcodec/cfhd.c | 462 ++++++++++++++++++++++++++++++++++++++++++++++--------
 libavcodec/cfhd.h |  15 +-
 2 files changed, 411 insertions(+), 66 deletions(-)

Comments

Gagandeep Singh May 31, 2018, 4:20 p.m.
On Thu, May 31, 2018 at 4:55 PM, Gagandeep Singh <deepgagan231197@gmail.com>
wrote:

> ---
>  libavcodec/cfhd.c | 462 ++++++++++++++++++++++++++++++
> ++++++++++++++++--------
>  libavcodec/cfhd.h |  15 +-
>  2 files changed, 411 insertions(+), 66 deletions(-)
>
> diff --git a/libavcodec/cfhd.c b/libavcodec/cfhd.c
> index 051d210355..c0cd25a95e 100644
> --- a/libavcodec/cfhd.c
> +++ b/libavcodec/cfhd.c
> @@ -41,12 +41,15 @@
>  #define ALPHA_COMPAND_GAIN 9400
>
>  enum CFHDParam {
> +    TransformType    =  10,
>      ChannelCount     =  12,
>      SubbandCount     =  14,
> +    Pframe           =  19,
>      ImageWidth       =  20,
>      ImageHeight      =  21,
>      LowpassPrecision =  35,
>      SubbandNumber    =  48,
> +    EncodingMethod   =  52,
>      Quantization     =  53,
>      ChannelNumber    =  62,
>      SampleFlags      =  68,
> @@ -84,6 +87,11 @@ static void init_peak_table_defaults(CFHDContext *s)
>
>  static void init_frame_defaults(CFHDContext *s)
>  {
> +    s->sample_type       = 0;
> +    s->transform_type    = 0;
> +    s->num_frames        = 0;
> +    s->Pframe            = 0;
> +    s->first_wavelet     = 0;
>      s->coded_width       = 0;
>      s->coded_height      = 0;
>      s->cropped_height    = 0;
> @@ -103,8 +111,10 @@ static void init_frame_defaults(CFHDContext *s)
>  }
>
>  /* TODO: merge with VLC tables or use LUT */
> -static inline int dequant_and_decompand(int level, int quantisation, int
> codebook)
> +static inline int dequant_and_decompand(int level, int quantisation, int
> codebook, int lossless)
>  {
> +    if (lossless)
> +        return level;
>      if (codebook == 0 || codebook == 1) {
>          int64_t abslevel = abs(level);
>          if (level < 264)
> @@ -193,16 +203,21 @@ static inline void filter(int16_t *output, ptrdiff_t
> out_stride,
>      }
>  }
>
> -static inline void interlaced_vertical_filter(int16_t *output, int16_t
> *low, int16_t *high,
> -                         int width, int linesize, int plane)
> +static inline void temporal_inverse_filter(int16_t *output, int16_t
> *low, int16_t *high,
> +                         int width, int linesize, int
> temporal_for_highpass)
>  {
>      int i;
>      int16_t even, odd;
>      for (i = 0; i < width; i++) {
>          even = (low[i] - high[i])/2;
>          odd  = (low[i] + high[i])/2;
> -        output[i]            = av_clip_uintp2(even, 10);
> -        output[i + linesize] = av_clip_uintp2(odd, 10);
> +        if (!temporal_for_highpass) {
> +            output[i]            = av_clip_uintp2(even, 10);
> +            output[i + linesize] = av_clip_uintp2(odd, 10);
> +        } else {
> +            low[i]  = even;
> +            high[i] = odd;
> +        }
>      }
>  }
>  static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
> @@ -261,6 +276,7 @@ static int alloc_buffers(AVCodecContext *avctx)
>
>      for (i = 0; i < planes; i++) {
>          int w8, h8, w4, h4, w2, h2;
> +        int16_t *frame2;
>          int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
>          int height = i ? avctx->height >> chroma_y_shift : avctx->height;
>          ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
> @@ -277,28 +293,85 @@ static int alloc_buffers(AVCodecContext *avctx)
>          w2 = w4 * 2;
>          h2 = h4 * 2;
>
> -        s->plane[i].idwt_buf =
> -            av_mallocz_array(height * stride,
> sizeof(*s->plane[i].idwt_buf));
> -        s->plane[i].idwt_tmp =
> -            av_malloc_array(height * stride,
> sizeof(*s->plane[i].idwt_tmp));
> -        if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
> -            return AVERROR(ENOMEM);
> -
> -        s->plane[i].subband[0] = s->plane[i].idwt_buf;
> -        s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
> -        s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
> -        s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
> -        s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
> -        s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
> -        s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
> -        s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
> -        s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
> -        s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
> -
> -        for (j = 0; j < DWT_LEVELS; j++) {
> -            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
> -                s->plane[i].band[j][k].a_width  = w8 << j;
> -                s->plane[i].band[j][k].a_height = h8 << j;
> +        if (s->transform_type == 0) {
> +            s->plane[i].idwt_buf =
> +                av_mallocz_array(height * stride,
> sizeof(*s->plane[i].idwt_buf));
> +            s->plane[i].idwt_tmp =
> +                av_malloc_array(height * stride,
> sizeof(*s->plane[i].idwt_tmp));
> +            if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
> +                return AVERROR(ENOMEM);
> +        } else if (s->transform_type == 2) {
> +            s->plane[i].idwt_buf =
> +                av_mallocz_array(2 * height * stride,
> sizeof(*s->plane[i].idwt_buf));
> +            s->plane[i].idwt_tmp =
> +                av_malloc_array(height * stride,
> sizeof(*s->plane[i].idwt_tmp));
> +            if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
> +                return AVERROR(ENOMEM);
> +        }
> +
> +        if (s->transform_type == 0) {
> +            s->plane[i].subband[0] = s->plane[i].idwt_buf;
> +            s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
> +            s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
> +            s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
> +            s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
> +            s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
> +            s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
> +            s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
> +            s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
> +            s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
> +        } else if (s->transform_type == 2) {
> +            s->plane[i].subband[0]  = s->plane[i].idwt_buf;
> +            s->plane[i].subband[1]  = s->plane[i].idwt_buf + 2 * w8 * h8;
> +            s->plane[i].subband[2]  = s->plane[i].idwt_buf + 1 * w8 * h8;
> +            s->plane[i].subband[3]  = s->plane[i].idwt_buf + 3 * w8 * h8;
> +            s->plane[i].subband[4]  = s->plane[i].idwt_buf + 2 * w4 * h4;
> +            s->plane[i].subband[5]  = s->plane[i].idwt_buf + 1 * w4 * h4;
> +            s->plane[i].subband[6]  = s->plane[i].idwt_buf + 3 * w4 * h4;
> +            frame2 =
> +            s->plane[i].subband[7]  = s->plane[i].idwt_buf + 4 * w2 * h2;
> +            s->plane[i].subband[8]  = frame2 + 2 * w4 * h4;
> +            s->plane[i].subband[9]  = frame2 + 1 * w4 * h4;
> +            s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
> +            s->plane[i].subband[11] = s->plane[i].idwt_buf + 2 * w2 * h2;
> +            s->plane[i].subband[12] = s->plane[i].idwt_buf + 1 * w2 * h2;
> +            s->plane[i].subband[13] = s->plane[i].idwt_buf + 3 * w2 * h2;
> +            s->plane[i].subband[14] = frame2 + 2 * w2 * h2;
> +            s->plane[i].subband[15] = frame2 + 1 * w2 * h2;
> +            s->plane[i].subband[16] = frame2 + 3 * w2 * h2;
> +        }
> +
> +        if (s->transform_type == 0) {
> +            for (j = 0; j < DWT_LEVELS - 3; j++) {
> +                for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]);
> k++) {
> +                    s->plane[i].band[j][k].a_width  = w8 << j;
> +                    s->plane[i].band[j][k].a_height = h8 << j;
> +                }
> +            }
> +        } else if(s->transform_type == 2) {
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[0]); k++) {
> +                s->plane[i].band[0][k].a_width  = w8;
> +                s->plane[i].band[0][k].a_height = h8;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[1]); k++) {
> +                s->plane[i].band[1][k].a_width  = w8 * 2;
> +                s->plane[i].band[1][k].a_height = h8 * 2;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[2]); k++) {
> +                s->plane[i].band[2][k].a_width  = w8 * 2;
> +                s->plane[i].band[2][k].a_height = h8 * 2;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[3]); k++) {
> +                s->plane[i].band[3][k].a_width  = w8 * 4;
> +                s->plane[i].band[3][k].a_height = h8 * 4;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[4]); k++) {
> +                s->plane[i].band[4][k].a_width  = w8 * 4;
> +                s->plane[i].band[4][k].a_height = h8 * 4;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[5]); k++) {
> +                s->plane[i].band[5][k].a_width  = w8 * 4;
> +                s->plane[i].band[5][k].a_height = h8 * 4;
>              }
>          }
>
> @@ -327,6 +400,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>      GetByteContext gb;
>      ThreadFrame frame = { .f = data };
>      AVFrame *pic = data;
> +    s->next_frame = av_frame_alloc();
>      int ret = 0, i, j, planes, plane, got_buffer = 0;
>      int16_t *coeff_data;
>
> @@ -349,6 +423,9 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>          } else if (tag == SampleFlags) {
>              av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
>              s->progressive = data & 0x0001;
> +        } else if (tag == Pframe) {
> +            s->Pframe = 1;
> +            av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
>          } else if (tag == ImageWidth) {
>              av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
>              s->coded_width = data;
> @@ -373,7 +450,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              }
>          } else if (tag == SubbandCount) {
>              av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n",
> data);
> -            if (data != SUBBAND_COUNT) {
> +            if (data != 10 && data != 17) {
>                  av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16"
> is unsupported\n", data);
>                  ret = AVERROR_PATCHWELCOME;
>                  break;
> @@ -405,7 +482,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>          } else if (tag == 51) {
>              av_log(avctx, AV_LOG_DEBUG, "Subband number actual
> %"PRIu16"\n", data);
>              s->subband_num_actual = data;
> -            if (s->subband_num_actual >= 10) {
> +            if (s->subband_num_actual >= 17 && s->subband_num_actual !=
> 255) {
>                  av_log(avctx, AV_LOG_ERROR, "Invalid subband number
> actual\n");
>                  ret = AVERROR(EINVAL);
>                  break;
> @@ -420,9 +497,15 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              s->prescale_shift[1] = (data >> 3) & 0x7;
>              s->prescale_shift[2] = (data >> 6) & 0x7;
>              av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n",
> data);
> +        } else if (tag == EncodingMethod) {
> +            s->encode_method = data;
> +            av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d :
> %x\n",s->subband_num_actual, data);
>          } else if (tag == 27) {
>              av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n",
> data);
> -            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width)
> {
> +            if (s->coded_width == 0){
> +                s->coded_width = data << 3;
> +              }
> +                if (data < 3) {
>                  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
>                  ret = AVERROR(EINVAL);
>                  break;
> @@ -431,20 +514,21 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              s->plane[s->channel_num].band[0][0].stride = data;
>          } else if (tag == 28) {
>              av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n",
> data);
> -            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height)
> {
> +            if (s->coded_height == 0)
> +                s->coded_height = data << 3;
> +            if (data < 3) {
>                  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
>                  ret = AVERROR(EINVAL);
>                  break;
>              }
>              s->plane[s->channel_num].band[0][0].height = data;
> -        } else if (tag == 1)
> +        } else if (tag == 1) {
> +            s->sample_type = data;
> +            if (data == 2)
> +                s->Pframe = 1;
>              av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
> -        else if (tag == 10) {
> -            if (data != 0) {
> -                avpriv_report_missing_feature(avctx, "Transform type of
> %"PRIu16, data);
> -                ret = AVERROR_PATCHWELCOME;
> -                break;
> -            }
> +        } else if (tag == 10) {
> +            s->transform_type = data;
>              av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n",
> data);
>          } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
>              if (abstag == 0x4001)
> @@ -502,9 +586,8 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                  break;
>              }
>              s->plane[s->channel_num].band[s->level][s->subband_num].height
> = data;
> -        } else if (tag == 71) {
> -            s->codebook = data;
> -            av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
> +        } else if (tag == -71) {
> +            av_log(avctx, AV_LOG_DEBUG, "color format %d\n", data);
>          } else if (tag == 72) {
>              s->codebook = data & 0xf;
>              s->difference_coding = (data >> 4) & 1;
> @@ -547,12 +630,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>          } else if (tag == -74 && s->peak.offset) {
>              s->peak.level = data;
>              s->peak.base += s->peak.offset / 2 - 2;
> -        } else
> +        } else if (tag == 82);
> +        else
>              av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag,
> data);
>
>          /* Some kind of end of header tag */
> -        if (tag == 4 && data == 0x1a4a && s->coded_width &&
> s->coded_height &&
> +        if ((tag == 4 && (data == 0x1a4a || data == 0xf0f) ||
> s->sample_type == 6 || s->sample_type == 1) && s->coded_width &&
> s->coded_height &&
>              s->coded_format != AV_PIX_FMT_NONE) {
> +            av_log(avctx, AV_LOG_DEBUG,  "this summers's gonna hurt\n");
>              if (s->a_width != s->coded_width || s->a_height !=
> s->coded_height ||
>                  s->a_format != s->coded_format) {
>                  free_buffers(s);
> @@ -620,7 +705,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n",
> lowpass_width * lowpass_height);
>          }
>
> -        if (tag == 55 && s->subband_num_actual != 255 && s->a_width &&
> s->a_height) {
> +        if ((tag == 55 || tag == 82) && s->a_width && s->a_height) {
>              int highpass_height = s->plane[s->channel_num].band[
> s->level][s->subband_num].height;
>              int highpass_width  = s->plane[s->channel_num].band[
> s->level][s->subband_num].width;
>              int highpass_a_width = s->plane[s->channel_num].band[
> s->level][s->subband_num].a_width;
> @@ -638,9 +723,11 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              }
>
>              if (highpass_height > highpass_a_height || highpass_width >
> highpass_a_width || a_expected < highpass_height *
> (uint64_t)highpass_stride) {
> -                av_log(avctx, AV_LOG_ERROR, "Too many highpass
> coefficients\n");
> -                ret = AVERROR(EINVAL);
> -                goto end;
> +                  if (s->subband_num_actual != 255) {
> +                      av_log(avctx, AV_LOG_ERROR, "Too many highpass
> coefficients\n");
> +                      ret = AVERROR(EINVAL);
> +                      goto end;
> +                  }
>              }
>              expected = highpass_height * highpass_stride;
>
> @@ -648,8 +735,13 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>
>              init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb)
> * 8);
>              {
> +                if(s->subband_num_actual == 255){
> +                    expected = 0;
> +                    goto finish;
> +                }
> +
>                  OPEN_READER(re, &s->gb);
> -                if (!s->codebook) {
> +                if (!s->codebook && !(s->transform_type == 2 &&
> s->subband_num_actual == 7)) {
>                      while (1) {
>                          UPDATE_CACHE(re, &s->gb);
>                          GET_RL_VLC(level, run, re, &s->gb,
> s->table_9_rl_vlc,
> @@ -664,9 +756,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                          if (count > expected)
>                              break;
>
> -                        coeff = dequant_and_decompand(level,
> s->quantisation, 0);
> +                        coeff = dequant_and_decompand(level,
> s->quantisation, 0, (s->sample_type == 2 || s->sample_type == 3) &&
> s->Pframe && s->subband_num_actual == 7);
>                          for (i = 0; i < run; i++)
> -                            *coeff_data++ = coeff;
> +                            if (tag != 82)
> +                                *coeff_data++  = coeff;
> +                            else {
> +                                *coeff_data   |= coeff << 8;
> +                                *coeff_data++ *= s->quantisation;
> +                            }
>                      }
>                      if (s->peak.level)
>                          peak_table(coeff_data - expected, &s->peak,
> expected);
> @@ -688,9 +785,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                          if (count > expected)
>                              break;
>
> -                        coeff = dequant_and_decompand(level,
> s->quantisation, s->codebook);
> +                        coeff = dequant_and_decompand(level,
> s->quantisation, s->codebook, (s->sample_type == 2 || s->sample_type == 3)
> && s->Pframe && s->subband_num_actual == 7);
>                          for (i = 0; i < run; i++)
> -                            *coeff_data++ = coeff;
> +                            if (tag != 82)
> +                                *coeff_data++  = coeff;
> +                            else {
> +                                *coeff_data   |= coeff << 8;
> +                                *coeff_data++ *= s->quantisation;
> +                            }
>                      }
>                      if (s->peak.level)
>                          peak_table(coeff_data - expected, &s->peak,
> expected);
> @@ -701,12 +803,12 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                  CLOSE_READER(re, &s->gb);
>              }
>
> -            if (count > expected) {
> +            if (count > expected && s->subband_num_actual != 255) {
>                  av_log(avctx, AV_LOG_ERROR, "Escape codeword not found,
> probably corrupt data\n");
>                  ret = AVERROR(EINVAL);
>                  goto end;
>              }
> -
> +            finish:
>              bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3),
> 4);
>              if (bytes > bytestream2_get_bytes_left(&gb)) {
>                  av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
> @@ -726,14 +828,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              }
>          }
>      }
> -
> -    if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
> -        s->coded_width || s->coded_height || s->coded_format !=
> AV_PIX_FMT_NONE) {
> +#if 0
> +    if ((!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
> +        s->coded_width || s->coded_height || s->coded_format !=
> AV_PIX_FMT_NONE) && s->sample_type != 1) {
>          av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
>          ret = AVERROR(EINVAL);
>          goto end;
>      }
> -
> +#endif
>      if (!got_buffer) {
>          av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
>          ret = AVERROR(EINVAL);
> @@ -741,8 +843,9 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>      }
>
>      planes = av_pix_fmt_count_planes(avctx->pix_fmt);
> +    if (s->transform_type == 0 && s->sample_type != 1) {
>      for (plane = 0; plane < planes && !ret; plane++) {
> -        /* level 1 */
> +            /* level 1 */
>          int lowpass_height  = s->plane[plane].band[0][0].height;
>          int lowpass_width   = s->plane[plane].band[0][0].width;
>          int highpass_stride = s->plane[plane].band[0][1].stride;
> @@ -921,14 +1024,249 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              low  = s->plane[plane].l_h[6];
>              high = s->plane[plane].l_h[7];
>              for (i = 0; i < lowpass_height; i++) {
> -                interlaced_vertical_filter(dst, low, high, lowpass_width
> * 2,  pic->linesize[act_plane]/2, act_plane);
> +                temporal_inverse_filter(dst, low, high, lowpass_width *
> 2,  pic->linesize[act_plane]/2, 0);
>                  low  += lowpass_width * 2;
>                  high += lowpass_width * 2;
>                  dst  += pic->linesize[act_plane];
>              }
>          }
>      }
> +    } else if (s->transform_type == 2 && s->sample_type != 1) {
> +          for (plane = 0; plane < planes && !ret; plane++) {
> +              /* level 1 */
> +              int lowpass_height  = s->plane[plane].band[0][0].height;
> +              int lowpass_width   = s->plane[plane].band[0][0].width;
> +              int highpass_stride = s->plane[plane].band[0][1].stride;
> +              int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
> +              int16_t *low, *high, *output, *dst;
> +
> +              if (lowpass_height > s->plane[plane].band[0][0].a_height
> || lowpass_width > s->plane[plane].band[0][0].a_width ||
> +                  !highpass_stride || s->plane[plane].band[0][1].width >
> s->plane[plane].band[0][1].a_width) {
> +                  av_log(avctx, AV_LOG_ERROR, "Invalid plane
> dimensions\n");
> +                  ret = AVERROR(EINVAL);
> +                  goto end;
> +              }
> +
> +              av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i
> %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              low    = s->plane[plane].subband[0];
> +              high   = s->plane[plane].subband[2];
> +              output = s->plane[plane].l_h[0];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low, lowpass_width,
> high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].subband[1];
> +              high   = s->plane[plane].subband[3];
> +              output = s->plane[plane].l_h[1];
> +
> +              for (i = 0; i < lowpass_width; i++) {
> +                  // note the stride of "low" is highpass_stride
> +                  vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].l_h[0];
> +              high   = s->plane[plane].l_h[1];
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height * 2; i++) {
> +                  horiz_filter(output, low, high, lowpass_width);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width * 2;
> +              }
> +              if (s->bpc == 12) {
> +                  output = s->plane[plane].subband[0];
> +                  for (i = 0; i < lowpass_height * 2; i++) {
> +                      for (j = 0; j < lowpass_width * 2; j++)
> +                          output[j] *= 4;
> +
> +                      output += lowpass_width * 2;
> +                  }
> +              }
> +
> +              /* level 2 */
> +              lowpass_height  = s->plane[plane].band[1][1].height;
> +              lowpass_width   = s->plane[plane].band[1][1].width;
> +              highpass_stride = s->plane[plane].band[1][1].stride;
> +
> +              if (lowpass_height > s->plane[plane].band[1][1].a_height
> || lowpass_width > s->plane[plane].band[1][1].a_width ||
> +                  !highpass_stride || s->plane[plane].band[1][1].width >
> s->plane[plane].band[1][1].a_width) {
> +                  av_log(avctx, AV_LOG_ERROR, "Invalid plane
> dimensions\n");
> +                  ret = AVERROR(EINVAL);
> +                  goto end;
> +              }
> +
> +              av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              low    = s->plane[plane].subband[0];
> +              high   = s->plane[plane].subband[5];
> +              output = s->plane[plane].l_h[3];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low, lowpass_width,
> high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].subband[4];
> +              high   = s->plane[plane].subband[6];
> +              output = s->plane[plane].l_h[4];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].l_h[3];
> +              high   = s->plane[plane].l_h[4];
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height * 2; i++) {
> +                  horiz_filter(output, low, high, lowpass_width);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width * 2;
> +              }
> +
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height * 2; i++) {
> +                  for (j = 0; j < lowpass_width * 2; j++)
> +                      output[j] *= 4;
> +
> +                  output += lowpass_width * 2;
> +              }
> +
> +              lowpass_height  = s->plane[plane].band[2][1].height;
> +              lowpass_width   = s->plane[plane].band[2][1].width;
> +              highpass_stride = s->plane[plane].band[2][1].stride;
> +              av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              low    = s->plane[plane].subband[7];
> +              high   = s->plane[plane].subband[9];
> +              output = s->plane[plane].l_h[3];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low, lowpass_width,
> high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].subband[8];
> +              high   = s->plane[plane].subband[10];
> +              output = s->plane[plane].l_h[4];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].l_h[3];
> +              high   = s->plane[plane].l_h[4];
> +              output = s->plane[plane].subband[7];
> +              for (i = 0; i < lowpass_height; i++) {
> +                  horiz_filter(output, low, high, lowpass_width);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width * 2;
> +              }
> +
> +              lowpass_height  = s->plane[plane].band[3][1].height;
> +              lowpass_width   = s->plane[plane].band[3][1].width;
> +              highpass_stride = s->plane[plane].band[3][1].stride;
> +              av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              if (lowpass_height > s->plane[plane].band[3][1].a_height
> || lowpass_width > s->plane[plane].band[3][1].a_width ||
> +                  !highpass_stride || s->plane[plane].band[3][1].width >
> s->plane[plane].band[3][1].a_width) {
> +                  av_log(avctx, AV_LOG_ERROR, "Invalid plane
> dimensions\n");
> +                  ret = AVERROR(EINVAL);
> +                  goto end;
> +              }
> +
> +              low    = s->plane[plane].subband[0];
> +              high   = s->plane[plane].subband[7];
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height; i++) {
> +                  temporal_inverse_filter(output, low, high,
> lowpass_width, 4 * lowpass_width * lowpass_height, 1);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width;
> +              }
> +
> +              av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +              if (s->progressive) {
> +                  low    = s->plane[plane].subband[0];
> +                  high   = s->plane[plane].subband[12];
> +                  output = s->plane[plane].l_h[6];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> lowpass_width, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  low    = s->plane[plane].subband[11];
> +                  high   = s->plane[plane].subband[13];
> +                  output = s->plane[plane].l_h[7];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  dst = (int16_t *)pic->data[act_plane];
> +                  low  = s->plane[plane].l_h[6];
> +                  high = s->plane[plane].l_h[7];
> +                  for (i = 0; i < lowpass_height * 2; i++) {
> +                      horiz_filter_clip(dst, low, high, lowpass_width,
> s->bpc);
> +                      low  += lowpass_width;
> +                      high += lowpass_width;
> +                      dst  += pic->linesize[act_plane] / 2;
> +                  }
> +
> +                  low    = s->plane[plane].subband[7];
> +                  high   = s->plane[plane].subband[15];
> +                  output = s->plane[plane].l_h[6];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> lowpass_width, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  low    = s->plane[plane].subband[14];
> +                  high   = s->plane[plane].subband[16];
> +                  output = s->plane[plane].l_h[7];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  low  = s->plane[plane].l_h[6];
> +                  high = s->plane[plane].l_h[7];
> +                  output = s->plane[plane].subband[7];
> +                  for (i = 0; i < lowpass_height * 2; i++) {
> +                      horiz_filter_clip(output, low, high, lowpass_width,
> s->bpc);
> +                      low  += lowpass_width;
> +                      high += lowpass_width;
> +                      output += lowpass_width;
> +                  }
> +                  s->next_frame->data[act_plane] = output;
>
> +            }
> +        }
> +    } else if (s->transform_type == 2 && s->sample_type == 1) {
> +        av_frame_ref(pic, s->next_frame);
> +    }
>
>  end:
>      if (ret < 0)
> @@ -961,6 +1299,6 @@ AVCodec ff_cfhd_decoder = {
>      .init             = cfhd_init,
>      .close            = cfhd_close,
>      .decode           = cfhd_decode,
> -    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
> -    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
> FF_CODEC_CAP_INIT_CLEANUP,
> +    .capabilities     = AV_CODEC_CAP_DR1,
> +    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
>  };
> diff --git a/libavcodec/cfhd.h b/libavcodec/cfhd.h
> index 7cd251fca7..0126bd4052 100644
> --- a/libavcodec/cfhd.h
> +++ b/libavcodec/cfhd.h
> @@ -30,15 +30,15 @@
>  #include "vlc.h"
>
>  #define VLC_BITS       9
> -#define SUBBAND_COUNT 10
> -
> +#define SUBBAND_COUNT 17
> +#define BAND_END_TRAILER (1 << 15) - 1
>  typedef struct CFHD_RL_VLC_ELEM {
>      int16_t level;
>      int8_t len;
>      uint16_t run;
>  } CFHD_RL_VLC_ELEM;
>
> -#define DWT_LEVELS 3
> +#define DWT_LEVELS 6
>
>  typedef struct SubBand {
>      int level;
> @@ -82,9 +82,16 @@ typedef struct CFHDContext {
>
>      CFHD_RL_VLC_ELEM table_18_rl_vlc[4572];
>      VLC vlc_18;
> -
> +    AVFrame * next_frame;
>      GetBitContext gb;
>
> +    int sample_type;
> +    int transform_type;
> +    int num_spatial;
> +    int num_frames;
> +    int encode_method;
> +    int first_wavelet;
> +    int Pframe;
>      int coded_width;
>      int coded_height;
>      int cropped_height;
Kieran Kunhya May 31, 2018, 8:38 p.m.
Hi

On Thu, 31 May 2018 at 12:25 Gagandeep Singh <deepgagan231197@gmail.com>
wrote:

> ---
>  libavcodec/cfhd.c | 462
> ++++++++++++++++++++++++++++++++++++++++++++++--------
>  libavcodec/cfhd.h |  15 +-
>  2 files changed, 411 insertions(+), 66 deletions(-)
>
> diff --git a/libavcodec/cfhd.c b/libavcodec/cfhd.c
> index 051d210355..c0cd25a95e 100644
> --- a/libavcodec/cfhd.c
> +++ b/libavcodec/cfhd.c
> @@ -41,12 +41,15 @@
>  #define ALPHA_COMPAND_GAIN 9400
>
>  enum CFHDParam {
> +    TransformType    =  10,
>      ChannelCount     =  12,
>      SubbandCount     =  14,
> +    Pframe           =  19,
>

PFrame


>      ImageWidth       =  20,
>      ImageHeight      =  21,
>      LowpassPrecision =  35,
>      SubbandNumber    =  48,
> +    EncodingMethod   =  52,
>      Quantization     =  53,
>      ChannelNumber    =  62,
>      SampleFlags      =  68,
> @@ -84,6 +87,11 @@ static void init_peak_table_defaults(CFHDContext *s)
>
>  static void init_frame_defaults(CFHDContext *s)
>  {
> +    s->sample_type       = 0;
> +    s->transform_type    = 0;
> +    s->num_frames        = 0;
> +    s->Pframe            = 0;
> +    s->first_wavelet     = 0;
>      s->coded_width       = 0;
>      s->coded_height      = 0;
>      s->cropped_height    = 0;
> @@ -103,8 +111,10 @@ static void init_frame_defaults(CFHDContext *s)
>  }
>
>  /* TODO: merge with VLC tables or use LUT */
> -static inline int dequant_and_decompand(int level, int quantisation, int
> codebook)
> +static inline int dequant_and_decompand(int level, int quantisation, int
> codebook, int lossless)
>  {
> +    if (lossless)
> +        return level;
>      if (codebook == 0 || codebook == 1) {
>          int64_t abslevel = abs(level);
>          if (level < 264)
> @@ -193,16 +203,21 @@ static inline void filter(int16_t *output, ptrdiff_t
> out_stride,
>      }
>  }
>
> -static inline void interlaced_vertical_filter(int16_t *output, int16_t
> *low, int16_t *high,
> -                         int width, int linesize, int plane)
> +static inline void temporal_inverse_filter(int16_t *output, int16_t *low,
> int16_t *high,
> +                         int width, int linesize, int
> temporal_for_highpass)
>  {
>      int i;
>      int16_t even, odd;
>      for (i = 0; i < width; i++) {
>          even = (low[i] - high[i])/2;
>          odd  = (low[i] + high[i])/2;
> -        output[i]            = av_clip_uintp2(even, 10);
> -        output[i + linesize] = av_clip_uintp2(odd, 10);
> +        if (!temporal_for_highpass) {
> +            output[i]            = av_clip_uintp2(even, 10);
> +            output[i + linesize] = av_clip_uintp2(odd, 10);
> +        } else {
> +            low[i]  = even;
> +            high[i] = odd;
> +        }
>      }
>  }
>  static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
> @@ -261,6 +276,7 @@ static int alloc_buffers(AVCodecContext *avctx)
>
>      for (i = 0; i < planes; i++) {
>          int w8, h8, w4, h4, w2, h2;
> +        int16_t *frame2;
>          int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
>          int height = i ? avctx->height >> chroma_y_shift : avctx->height;
>          ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
> @@ -277,28 +293,85 @@ static int alloc_buffers(AVCodecContext *avctx)
>          w2 = w4 * 2;
>          h2 = h4 * 2;
>
> -        s->plane[i].idwt_buf =
> -            av_mallocz_array(height * stride,
> sizeof(*s->plane[i].idwt_buf));
> -        s->plane[i].idwt_tmp =
> -            av_malloc_array(height * stride,
> sizeof(*s->plane[i].idwt_tmp));
> -        if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
> -            return AVERROR(ENOMEM);
> -
> -        s->plane[i].subband[0] = s->plane[i].idwt_buf;
> -        s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
> -        s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
> -        s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
> -        s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
> -        s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
> -        s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
> -        s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
> -        s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
> -        s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
> -
> -        for (j = 0; j < DWT_LEVELS; j++) {
> -            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
> -                s->plane[i].band[j][k].a_width  = w8 << j;
> -                s->plane[i].band[j][k].a_height = h8 << j;
> +        if (s->transform_type == 0) {
> +            s->plane[i].idwt_buf =
> +                av_mallocz_array(height * stride,
> sizeof(*s->plane[i].idwt_buf));
> +            s->plane[i].idwt_tmp =
> +                av_malloc_array(height * stride,
> sizeof(*s->plane[i].idwt_tmp));
> +            if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
> +                return AVERROR(ENOMEM);
> +        } else if (s->transform_type == 2) {
> +            s->plane[i].idwt_buf =
> +                av_mallocz_array(2 * height * stride,
> sizeof(*s->plane[i].idwt_buf));
> +            s->plane[i].idwt_tmp =
> +                av_malloc_array(height * stride,
> sizeof(*s->plane[i].idwt_tmp));
> +            if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
> +                return AVERROR(ENOMEM);
> +        }
> +
> +        if (s->transform_type == 0) {
> +            s->plane[i].subband[0] = s->plane[i].idwt_buf;
> +            s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
> +            s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
> +            s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
> +            s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
> +            s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
> +            s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
> +            s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
> +            s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
> +            s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
> +        } else if (s->transform_type == 2) {
> +            s->plane[i].subband[0]  = s->plane[i].idwt_buf;
> +            s->plane[i].subband[1]  = s->plane[i].idwt_buf + 2 * w8 * h8;
> +            s->plane[i].subband[2]  = s->plane[i].idwt_buf + 1 * w8 * h8;
> +            s->plane[i].subband[3]  = s->plane[i].idwt_buf + 3 * w8 * h8;
> +            s->plane[i].subband[4]  = s->plane[i].idwt_buf + 2 * w4 * h4;
> +            s->plane[i].subband[5]  = s->plane[i].idwt_buf + 1 * w4 * h4;
> +            s->plane[i].subband[6]  = s->plane[i].idwt_buf + 3 * w4 * h4;
> +            frame2 =
> +            s->plane[i].subband[7]  = s->plane[i].idwt_buf + 4 * w2 * h2;
> +            s->plane[i].subband[8]  = frame2 + 2 * w4 * h4;
> +            s->plane[i].subband[9]  = frame2 + 1 * w4 * h4;
> +            s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
> +            s->plane[i].subband[11] = s->plane[i].idwt_buf + 2 * w2 * h2;
> +            s->plane[i].subband[12] = s->plane[i].idwt_buf + 1 * w2 * h2;
> +            s->plane[i].subband[13] = s->plane[i].idwt_buf + 3 * w2 * h2;
> +            s->plane[i].subband[14] = frame2 + 2 * w2 * h2;
> +            s->plane[i].subband[15] = frame2 + 1 * w2 * h2;
> +            s->plane[i].subband[16] = frame2 + 3 * w2 * h2;
> +        }
> +
> +        if (s->transform_type == 0) {
> +            for (j = 0; j < DWT_LEVELS - 3; j++) {
> +                for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++)
> {
> +                    s->plane[i].band[j][k].a_width  = w8 << j;
> +                    s->plane[i].band[j][k].a_height = h8 << j;
> +                }
> +            }
> +        } else if(s->transform_type == 2) {
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[0]); k++) {
> +                s->plane[i].band[0][k].a_width  = w8;
> +                s->plane[i].band[0][k].a_height = h8;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[1]); k++) {
> +                s->plane[i].band[1][k].a_width  = w8 * 2;
> +                s->plane[i].band[1][k].a_height = h8 * 2;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[2]); k++) {
> +                s->plane[i].band[2][k].a_width  = w8 * 2;
> +                s->plane[i].band[2][k].a_height = h8 * 2;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[3]); k++) {
> +                s->plane[i].band[3][k].a_width  = w8 * 4;
> +                s->plane[i].band[3][k].a_height = h8 * 4;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[4]); k++) {
> +                s->plane[i].band[4][k].a_width  = w8 * 4;
> +                s->plane[i].band[4][k].a_height = h8 * 4;
> +            }
> +            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[5]); k++) {
> +                s->plane[i].band[5][k].a_width  = w8 * 4;
> +                s->plane[i].band[5][k].a_height = h8 * 4;
>              }
>          }
>
> @@ -327,6 +400,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>      GetByteContext gb;
>      ThreadFrame frame = { .f = data };
>      AVFrame *pic = data;
> +    s->next_frame = av_frame_alloc();
>      int ret = 0, i, j, planes, plane, got_buffer = 0;
>      int16_t *coeff_data;
>
> @@ -349,6 +423,9 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>          } else if (tag == SampleFlags) {
>              av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
>              s->progressive = data & 0x0001;
> +        } else if (tag == Pframe) {
> +            s->Pframe = 1;
> +            av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
>

Remove CamelCase in struct


>          } else if (tag == ImageWidth) {
>              av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
>              s->coded_width = data;
> @@ -373,7 +450,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              }
>          } else if (tag == SubbandCount) {
>              av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n",
> data);
> -            if (data != SUBBAND_COUNT) {
> +            if (data != 10 && data != 17) {
>                  av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16"
> is unsupported\n", data);
>                  ret = AVERROR_PATCHWELCOME;
>                  break;
> @@ -405,7 +482,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>          } else if (tag == 51) {
>              av_log(avctx, AV_LOG_DEBUG, "Subband number actual
> %"PRIu16"\n", data);
>              s->subband_num_actual = data;
> -            if (s->subband_num_actual >= 10) {
> +            if (s->subband_num_actual >= 17 && s->subband_num_actual !=
> 255) {
>                  av_log(avctx, AV_LOG_ERROR, "Invalid subband number
> actual\n");
>                  ret = AVERROR(EINVAL);
>                  break;
> @@ -420,9 +497,15 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              s->prescale_shift[1] = (data >> 3) & 0x7;
>              s->prescale_shift[2] = (data >> 6) & 0x7;
>              av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n",
> data);
> +        } else if (tag == EncodingMethod) {
> +            s->encode_method = data;
> +            av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d :
> %x\n",s->subband_num_actual, data);
>          } else if (tag == 27) {
>              av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n",
> data);
> -            if (data < 3 || data >
> s->plane[s->channel_num].band[0][0].a_width) {
> +            if (s->coded_width == 0){
> +                s->coded_width = data << 3;
> +              }
> +                if (data < 3) {
>                  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
>                  ret = AVERROR(EINVAL);
>                  break;
> @@ -431,20 +514,21 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              s->plane[s->channel_num].band[0][0].stride = data;
>          } else if (tag == 28) {
>              av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n",
> data);
> -            if (data < 3 || data >
> s->plane[s->channel_num].band[0][0].a_height) {
> +            if (s->coded_height == 0)
> +                s->coded_height = data << 3;
> +            if (data < 3) {
>                  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
>                  ret = AVERROR(EINVAL);
>                  break;
>              }
>              s->plane[s->channel_num].band[0][0].height = data;
> -        } else if (tag == 1)
> +        } else if (tag == 1) {
> +            s->sample_type = data;
> +            if (data == 2)
> +                s->Pframe = 1;
>              av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
> -        else if (tag == 10) {
> -            if (data != 0) {
> -                avpriv_report_missing_feature(avctx, "Transform type of
> %"PRIu16, data);
> -                ret = AVERROR_PATCHWELCOME;
> -                break;
> -            }
> +        } else if (tag == 10) {
> +            s->transform_type = data;
>              av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n",
> data);
>          } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
>              if (abstag == 0x4001)
> @@ -502,9 +586,8 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                  break;
>              }
>
>  s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
> -        } else if (tag == 71) {
> -            s->codebook = data;
> -            av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
> +        } else if (tag == -71) {
> +            av_log(avctx, AV_LOG_DEBUG, "color format %d\n", data);
>          } else if (tag == 72) {
>

This hunk should be a separate patch


>              s->codebook = data & 0xf;
>              s->difference_coding = (data >> 4) & 1;
> @@ -547,12 +630,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>          } else if (tag == -74 && s->peak.offset) {
>              s->peak.level = data;
>              s->peak.base += s->peak.offset / 2 - 2;
> -        } else
> +        } else if (tag == 82);
> +        else
>              av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag,
> data);
>
>          /* Some kind of end of header tag */
> -        if (tag == 4 && data == 0x1a4a && s->coded_width &&
> s->coded_height &&
> +        if ((tag == 4 && (data == 0x1a4a || data == 0xf0f) ||
> s->sample_type == 6 || s->sample_type == 1) && s->coded_width &&
> s->coded_height &&
>              s->coded_format != AV_PIX_FMT_NONE) {
> +            av_log(avctx, AV_LOG_DEBUG,  "this summers's gonna hurt\n");
>              if (s->a_width != s->coded_width || s->a_height !=
> s->coded_height ||
>                  s->a_format != s->coded_format) {
>                  free_buffers(s);
> @@ -620,7 +705,7 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n",
> lowpass_width * lowpass_height);
>          }
>
> -        if (tag == 55 && s->subband_num_actual != 255 && s->a_width &&
> s->a_height) {
> +        if ((tag == 55 || tag == 82) && s->a_width && s->a_height) {
>              int highpass_height =
> s->plane[s->channel_num].band[s->level][s->subband_num].height;
>              int highpass_width  =
> s->plane[s->channel_num].band[s->level][s->subband_num].width;
>              int highpass_a_width =
> s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
> @@ -638,9 +723,11 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              }
>
>              if (highpass_height > highpass_a_height || highpass_width >
> highpass_a_width || a_expected < highpass_height *
> (uint64_t)highpass_stride) {
> -                av_log(avctx, AV_LOG_ERROR, "Too many highpass
> coefficients\n");
> -                ret = AVERROR(EINVAL);
> -                goto end;
> +                  if (s->subband_num_actual != 255) {
> +                      av_log(avctx, AV_LOG_ERROR, "Too many highpass
> coefficients\n");
> +                      ret = AVERROR(EINVAL);
> +                      goto end;
> +                  }
>              }
>              expected = highpass_height * highpass_stride;
>
> @@ -648,8 +735,13 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>
>              init_get_bits(&s->gb, gb.buffer,
> bytestream2_get_bytes_left(&gb) * 8);
>              {
> +                if(s->subband_num_actual == 255){
> +                    expected = 0;
> +                    goto finish;
> +                }
> +
>                  OPEN_READER(re, &s->gb);
> -                if (!s->codebook) {
> +                if (!s->codebook && !(s->transform_type == 2 &&
> s->subband_num_actual == 7)) {
>                      while (1) {
>                          UPDATE_CACHE(re, &s->gb);
>                          GET_RL_VLC(level, run, re, &s->gb,
> s->table_9_rl_vlc,
> @@ -664,9 +756,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                          if (count > expected)
>                              break;
>
> -                        coeff = dequant_and_decompand(level,
> s->quantisation, 0);
> +                        coeff = dequant_and_decompand(level,
> s->quantisation, 0, (s->sample_type == 2 || s->sample_type == 3) &&
> s->Pframe && s->subband_num_actual == 7);
>                          for (i = 0; i < run; i++)
> -                            *coeff_data++ = coeff;
> +                            if (tag != 82)
> +                                *coeff_data++  = coeff;
> +                            else {
> +                                *coeff_data   |= coeff << 8;
> +                                *coeff_data++ *= s->quantisation;
> +                            }
>                      }
>                      if (s->peak.level)
>                          peak_table(coeff_data - expected, &s->peak,
> expected);
> @@ -688,9 +785,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                          if (count > expected)
>                              break;
>
> -                        coeff = dequant_and_decompand(level,
> s->quantisation, s->codebook);
> +                        coeff = dequant_and_decompand(level,
> s->quantisation, s->codebook, (s->sample_type == 2 || s->sample_type == 3)
> && s->Pframe && s->subband_num_actual == 7);
>                          for (i = 0; i < run; i++)
> -                            *coeff_data++ = coeff;
> +                            if (tag != 82)
> +                                *coeff_data++  = coeff;
> +                            else {
> +                                *coeff_data   |= coeff << 8;
> +                                *coeff_data++ *= s->quantisation;
> +                            }
>                      }
>                      if (s->peak.level)
>                          peak_table(coeff_data - expected, &s->peak,
> expected);
> @@ -701,12 +803,12 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>                  CLOSE_READER(re, &s->gb);
>              }
>
> -            if (count > expected) {
> +            if (count > expected && s->subband_num_actual != 255) {
>                  av_log(avctx, AV_LOG_ERROR, "Escape codeword not found,
> probably corrupt data\n");
>                  ret = AVERROR(EINVAL);
>                  goto end;
>              }
> -
> +            finish:
>              bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
>              if (bytes > bytestream2_get_bytes_left(&gb)) {
>                  av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
> @@ -726,14 +828,14 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              }
>          }
>      }
> -
> -    if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
> -        s->coded_width || s->coded_height || s->coded_format !=
> AV_PIX_FMT_NONE) {
> +#if 0
> +    if ((!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
> +        s->coded_width || s->coded_height || s->coded_format !=
> AV_PIX_FMT_NONE) && s->sample_type != 1) {
>          av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
>          ret = AVERROR(EINVAL);
>          goto end;
>      }
> -
> +#endif
>      if (!got_buffer) {
>          av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
>          ret = AVERROR(EINVAL);
> @@ -741,8 +843,9 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>      }
>
>      planes = av_pix_fmt_count_planes(avctx->pix_fmt);
> +    if (s->transform_type == 0 && s->sample_type != 1) {
>      for (plane = 0; plane < planes && !ret; plane++) {
> -        /* level 1 */
> +            /* level 1 */
>          int lowpass_height  = s->plane[plane].band[0][0].height;
>          int lowpass_width   = s->plane[plane].band[0][0].width;
>          int highpass_stride = s->plane[plane].band[0][1].stride;
> @@ -921,14 +1024,249 @@ static int cfhd_decode(AVCodecContext *avctx, void
> *data, int *got_frame,
>              low  = s->plane[plane].l_h[6];
>              high = s->plane[plane].l_h[7];
>              for (i = 0; i < lowpass_height; i++) {
> -                interlaced_vertical_filter(dst, low, high, lowpass_width
> * 2,  pic->linesize[act_plane]/2, act_plane);
> +                temporal_inverse_filter(dst, low, high, lowpass_width *
> 2,  pic->linesize[act_plane]/2, 0);
>                  low  += lowpass_width * 2;
>                  high += lowpass_width * 2;
>                  dst  += pic->linesize[act_plane];
>              }
>          }
>      }
> +    } else if (s->transform_type == 2 && s->sample_type != 1) {
> +          for (plane = 0; plane < planes && !ret; plane++) {
> +              /* level 1 */
> +              int lowpass_height  = s->plane[plane].band[0][0].height;
> +              int lowpass_width   = s->plane[plane].band[0][0].width;
> +              int highpass_stride = s->plane[plane].band[0][1].stride;
> +              int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
> +              int16_t *low, *high, *output, *dst;
> +
> +              if (lowpass_height > s->plane[plane].band[0][0].a_height ||
> lowpass_width > s->plane[plane].band[0][0].a_width ||
> +                  !highpass_stride || s->plane[plane].band[0][1].width >
> s->plane[plane].band[0][1].a_width) {
> +                  av_log(avctx, AV_LOG_ERROR, "Invalid plane
> dimensions\n");
> +                  ret = AVERROR(EINVAL);
> +                  goto end;
> +              }
> +
> +              av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i
> %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              low    = s->plane[plane].subband[0];
> +              high   = s->plane[plane].subband[2];
> +              output = s->plane[plane].l_h[0];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low, lowpass_width,
> high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].subband[1];
> +              high   = s->plane[plane].subband[3];
> +              output = s->plane[plane].l_h[1];
> +
> +              for (i = 0; i < lowpass_width; i++) {
> +                  // note the stride of "low" is highpass_stride
> +                  vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].l_h[0];
> +              high   = s->plane[plane].l_h[1];
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height * 2; i++) {
> +                  horiz_filter(output, low, high, lowpass_width);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width * 2;
> +              }
> +              if (s->bpc == 12) {
> +                  output = s->plane[plane].subband[0];
> +                  for (i = 0; i < lowpass_height * 2; i++) {
> +                      for (j = 0; j < lowpass_width * 2; j++)
> +                          output[j] *= 4;
> +
> +                      output += lowpass_width * 2;
> +                  }
> +              }
> +
> +              /* level 2 */
> +              lowpass_height  = s->plane[plane].band[1][1].height;
> +              lowpass_width   = s->plane[plane].band[1][1].width;
> +              highpass_stride = s->plane[plane].band[1][1].stride;
> +
> +              if (lowpass_height > s->plane[plane].band[1][1].a_height ||
> lowpass_width > s->plane[plane].band[1][1].a_width ||
> +                  !highpass_stride || s->plane[plane].band[1][1].width >
> s->plane[plane].band[1][1].a_width) {
> +                  av_log(avctx, AV_LOG_ERROR, "Invalid plane
> dimensions\n");
> +                  ret = AVERROR(EINVAL);
> +                  goto end;
> +              }
> +
> +              av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              low    = s->plane[plane].subband[0];
> +              high   = s->plane[plane].subband[5];
> +              output = s->plane[plane].l_h[3];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low, lowpass_width,
> high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].subband[4];
> +              high   = s->plane[plane].subband[6];
> +              output = s->plane[plane].l_h[4];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].l_h[3];
> +              high   = s->plane[plane].l_h[4];
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height * 2; i++) {
> +                  horiz_filter(output, low, high, lowpass_width);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width * 2;
> +              }
> +
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height * 2; i++) {
> +                  for (j = 0; j < lowpass_width * 2; j++)
> +                      output[j] *= 4;
> +
> +                  output += lowpass_width * 2;
> +              }
> +
> +              lowpass_height  = s->plane[plane].band[2][1].height;
> +              lowpass_width   = s->plane[plane].band[2][1].width;
> +              highpass_stride = s->plane[plane].band[2][1].stride;
> +              av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              low    = s->plane[plane].subband[7];
> +              high   = s->plane[plane].subband[9];
> +              output = s->plane[plane].l_h[3];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low, lowpass_width,
> high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].subband[8];
> +              high   = s->plane[plane].subband[10];
> +              output = s->plane[plane].l_h[4];
> +              for (i = 0; i < lowpass_width; i++) {
> +                  vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                  low++;
> +                  high++;
> +                  output++;
> +              }
> +
> +              low    = s->plane[plane].l_h[3];
> +              high   = s->plane[plane].l_h[4];
> +              output = s->plane[plane].subband[7];
> +              for (i = 0; i < lowpass_height; i++) {
> +                  horiz_filter(output, low, high, lowpass_width);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width * 2;
> +              }
> +
> +              lowpass_height  = s->plane[plane].band[3][1].height;
> +              lowpass_width   = s->plane[plane].band[3][1].width;
> +              highpass_stride = s->plane[plane].band[3][1].stride;
> +              av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +
> +              if (lowpass_height > s->plane[plane].band[3][1].a_height ||
> lowpass_width > s->plane[plane].band[3][1].a_width ||
> +                  !highpass_stride || s->plane[plane].band[3][1].width >
> s->plane[plane].band[3][1].a_width) {
> +                  av_log(avctx, AV_LOG_ERROR, "Invalid plane
> dimensions\n");
> +                  ret = AVERROR(EINVAL);
> +                  goto end;
> +              }
> +
> +              low    = s->plane[plane].subband[0];
> +              high   = s->plane[plane].subband[7];
> +              output = s->plane[plane].subband[0];
> +              for (i = 0; i < lowpass_height; i++) {
> +                  temporal_inverse_filter(output, low, high,
> lowpass_width, 4 * lowpass_width * lowpass_height, 1);
> +                  low    += lowpass_width;
> +                  high   += lowpass_width;
> +                  output += lowpass_width;
> +              }
> +
> +              av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n",
> plane, lowpass_height, lowpass_width, highpass_stride);
> +              if (s->progressive) {
> +                  low    = s->plane[plane].subband[0];
> +                  high   = s->plane[plane].subband[12];
> +                  output = s->plane[plane].l_h[6];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> lowpass_width, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  low    = s->plane[plane].subband[11];
> +                  high   = s->plane[plane].subband[13];
> +                  output = s->plane[plane].l_h[7];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  dst = (int16_t *)pic->data[act_plane];
> +                  low  = s->plane[plane].l_h[6];
> +                  high = s->plane[plane].l_h[7];
> +                  for (i = 0; i < lowpass_height * 2; i++) {
> +                      horiz_filter_clip(dst, low, high, lowpass_width,
> s->bpc);
> +                      low  += lowpass_width;
> +                      high += lowpass_width;
> +                      dst  += pic->linesize[act_plane] / 2;
> +                  }
> +
> +                  low    = s->plane[plane].subband[7];
> +                  high   = s->plane[plane].subband[15];
> +                  output = s->plane[plane].l_h[6];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> lowpass_width, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  low    = s->plane[plane].subband[14];
> +                  high   = s->plane[plane].subband[16];
> +                  output = s->plane[plane].l_h[7];
> +                  for (i = 0; i < lowpass_width; i++) {
> +                      vert_filter(output, lowpass_width, low,
> highpass_stride, high, highpass_stride, lowpass_height);
> +                      low++;
> +                      high++;
> +                      output++;
> +                  }
> +
> +                  low  = s->plane[plane].l_h[6];
> +                  high = s->plane[plane].l_h[7];
> +                  output = s->plane[plane].subband[7];
> +                  for (i = 0; i < lowpass_height * 2; i++) {
> +                      horiz_filter_clip(output, low, high, lowpass_width,
> s->bpc);
> +                      low  += lowpass_width;
> +                      high += lowpass_width;
> +                      output += lowpass_width;
> +                  }
> +                  s->next_frame->data[act_plane] = output;
>
> +            }
> +        }
> +    } else if (s->transform_type == 2 && s->sample_type == 1) {
> +        av_frame_ref(pic, s->next_frame);
> +    }
>

Needs unreffing when done with.
But I don't fully understand the code, you reference the frame but never
use it?


>
>  end:
>      if (ret < 0)
> @@ -961,6 +1299,6 @@ AVCodec ff_cfhd_decoder = {
>      .init             = cfhd_init,
>      .close            = cfhd_close,
>      .decode           = cfhd_decode,
> -    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
> -    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
> FF_CODEC_CAP_INIT_CLEANUP,
> +    .capabilities     = AV_CODEC_CAP_DR1,
> +    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
>  };
> diff --git a/libavcodec/cfhd.h b/libavcodec/cfhd.h
> index 7cd251fca7..0126bd4052 100644
> --- a/libavcodec/cfhd.h
> +++ b/libavcodec/cfhd.h
> @@ -30,15 +30,15 @@
>  #include "vlc.h"
>
>  #define VLC_BITS       9
> -#define SUBBAND_COUNT 10
> -
> +#define SUBBAND_COUNT 17
> +#define BAND_END_TRAILER (1 << 15) - 1
>

Maybe something better for these #defines to explain the old mode vs new.


>  typedef struct CFHD_RL_VLC_ELEM {
>      int16_t level;
>      int8_t len;
>      uint16_t run;
>  } CFHD_RL_VLC_ELEM;
>
> -#define DWT_LEVELS 3
> +#define DWT_LEVELS 6
>
>  typedef struct SubBand {
>      int level;
> @@ -82,9 +82,16 @@ typedef struct CFHDContext {
>
>      CFHD_RL_VLC_ELEM table_18_rl_vlc[4572];
>      VLC vlc_18;
> -
> +    AVFrame * next_frame;
>      GetBitContext gb;
>
> +    int sample_type;
> +    int transform_type;
> +    int num_spatial;
> +    int num_frames;
> +    int encode_method;
> +    int first_wavelet;
> +    int Pframe;
>      int coded_width;
>      int coded_height;
>      int cropped_height;
> --
> 2.14.
>

Kieran
Michael Niedermayer June 1, 2018, 9:34 p.m.
On Thu, May 31, 2018 at 04:55:22PM +0530, Gagandeep Singh wrote:
> ---
>  libavcodec/cfhd.c | 462 ++++++++++++++++++++++++++++++++++++++++++++++--------
>  libavcodec/cfhd.h |  15 +-
>  2 files changed, 411 insertions(+), 66 deletions(-)


this causes some build warnings:

CC	libavcodec/cfhd.o
libavcodec/cfhd.c: In function ‘cfhd_decode’:
libavcodec/cfhd.c:404:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
     int ret = 0, i, j, planes, plane, got_buffer = 0;
     ^
libavcodec/cfhd.c:743:17: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
                 OPEN_READER(re, &s->gb);
                 ^
libavcodec/cfhd.c:1263:50: warning: assignment from incompatible pointer type [enabled by default]
                   s->next_frame->data[act_plane] = output;


[...]

Patch hide | download patch | download mbox

diff --git a/libavcodec/cfhd.c b/libavcodec/cfhd.c
index 051d210355..c0cd25a95e 100644
--- a/libavcodec/cfhd.c
+++ b/libavcodec/cfhd.c
@@ -41,12 +41,15 @@ 
 #define ALPHA_COMPAND_GAIN 9400
 
 enum CFHDParam {
+    TransformType    =  10,
     ChannelCount     =  12,
     SubbandCount     =  14,
+    Pframe           =  19,
     ImageWidth       =  20,
     ImageHeight      =  21,
     LowpassPrecision =  35,
     SubbandNumber    =  48,
+    EncodingMethod   =  52,
     Quantization     =  53,
     ChannelNumber    =  62,
     SampleFlags      =  68,
@@ -84,6 +87,11 @@  static void init_peak_table_defaults(CFHDContext *s)
 
 static void init_frame_defaults(CFHDContext *s)
 {
+    s->sample_type       = 0;
+    s->transform_type    = 0;
+    s->num_frames        = 0;
+    s->Pframe            = 0;
+    s->first_wavelet     = 0;
     s->coded_width       = 0;
     s->coded_height      = 0;
     s->cropped_height    = 0;
@@ -103,8 +111,10 @@  static void init_frame_defaults(CFHDContext *s)
 }
 
 /* TODO: merge with VLC tables or use LUT */
-static inline int dequant_and_decompand(int level, int quantisation, int codebook)
+static inline int dequant_and_decompand(int level, int quantisation, int codebook, int lossless)
 {
+    if (lossless)
+        return level;
     if (codebook == 0 || codebook == 1) {
         int64_t abslevel = abs(level);
         if (level < 264)
@@ -193,16 +203,21 @@  static inline void filter(int16_t *output, ptrdiff_t out_stride,
     }
 }
 
-static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
-                         int width, int linesize, int plane)
+static inline void temporal_inverse_filter(int16_t *output, int16_t *low, int16_t *high,
+                         int width, int linesize, int temporal_for_highpass)
 {
     int i;
     int16_t even, odd;
     for (i = 0; i < width; i++) {
         even = (low[i] - high[i])/2;
         odd  = (low[i] + high[i])/2;
-        output[i]            = av_clip_uintp2(even, 10);
-        output[i + linesize] = av_clip_uintp2(odd, 10);
+        if (!temporal_for_highpass) {
+            output[i]            = av_clip_uintp2(even, 10);
+            output[i + linesize] = av_clip_uintp2(odd, 10);
+        } else {
+            low[i]  = even;
+            high[i] = odd;
+        }
     }
 }
 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
@@ -261,6 +276,7 @@  static int alloc_buffers(AVCodecContext *avctx)
 
     for (i = 0; i < planes; i++) {
         int w8, h8, w4, h4, w2, h2;
+        int16_t *frame2;
         int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
         int height = i ? avctx->height >> chroma_y_shift : avctx->height;
         ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
@@ -277,28 +293,85 @@  static int alloc_buffers(AVCodecContext *avctx)
         w2 = w4 * 2;
         h2 = h4 * 2;
 
-        s->plane[i].idwt_buf =
-            av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
-        s->plane[i].idwt_tmp =
-            av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
-        if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
-            return AVERROR(ENOMEM);
-
-        s->plane[i].subband[0] = s->plane[i].idwt_buf;
-        s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
-        s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
-        s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
-        s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
-        s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
-        s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
-        s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
-        s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
-        s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
-
-        for (j = 0; j < DWT_LEVELS; j++) {
-            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
-                s->plane[i].band[j][k].a_width  = w8 << j;
-                s->plane[i].band[j][k].a_height = h8 << j;
+        if (s->transform_type == 0) {
+            s->plane[i].idwt_buf =
+                av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
+            s->plane[i].idwt_tmp =
+                av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
+            if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
+                return AVERROR(ENOMEM);
+        } else if (s->transform_type == 2) {
+            s->plane[i].idwt_buf =
+                av_mallocz_array(2 * height * stride, sizeof(*s->plane[i].idwt_buf));
+            s->plane[i].idwt_tmp =
+                av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
+            if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
+                return AVERROR(ENOMEM);
+        }
+
+        if (s->transform_type == 0) {
+            s->plane[i].subband[0] = s->plane[i].idwt_buf;
+            s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
+            s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
+            s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
+            s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
+            s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
+            s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
+            s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
+            s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
+            s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
+        } else if (s->transform_type == 2) {
+            s->plane[i].subband[0]  = s->plane[i].idwt_buf;
+            s->plane[i].subband[1]  = s->plane[i].idwt_buf + 2 * w8 * h8;
+            s->plane[i].subband[2]  = s->plane[i].idwt_buf + 1 * w8 * h8;
+            s->plane[i].subband[3]  = s->plane[i].idwt_buf + 3 * w8 * h8;
+            s->plane[i].subband[4]  = s->plane[i].idwt_buf + 2 * w4 * h4;
+            s->plane[i].subband[5]  = s->plane[i].idwt_buf + 1 * w4 * h4;
+            s->plane[i].subband[6]  = s->plane[i].idwt_buf + 3 * w4 * h4;
+            frame2 =
+            s->plane[i].subband[7]  = s->plane[i].idwt_buf + 4 * w2 * h2;
+            s->plane[i].subband[8]  = frame2 + 2 * w4 * h4;
+            s->plane[i].subband[9]  = frame2 + 1 * w4 * h4;
+            s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
+            s->plane[i].subband[11] = s->plane[i].idwt_buf + 2 * w2 * h2;
+            s->plane[i].subband[12] = s->plane[i].idwt_buf + 1 * w2 * h2;
+            s->plane[i].subband[13] = s->plane[i].idwt_buf + 3 * w2 * h2;
+            s->plane[i].subband[14] = frame2 + 2 * w2 * h2;
+            s->plane[i].subband[15] = frame2 + 1 * w2 * h2;
+            s->plane[i].subband[16] = frame2 + 3 * w2 * h2;
+        }
+
+        if (s->transform_type == 0) {
+            for (j = 0; j < DWT_LEVELS - 3; j++) {
+                for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
+                    s->plane[i].band[j][k].a_width  = w8 << j;
+                    s->plane[i].band[j][k].a_height = h8 << j;
+                }
+            }
+        } else if(s->transform_type == 2) {
+            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[0]); k++) {
+                s->plane[i].band[0][k].a_width  = w8;
+                s->plane[i].band[0][k].a_height = h8;
+            }
+            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[1]); k++) {
+                s->plane[i].band[1][k].a_width  = w8 * 2;
+                s->plane[i].band[1][k].a_height = h8 * 2;
+            }
+            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[2]); k++) {
+                s->plane[i].band[2][k].a_width  = w8 * 2;
+                s->plane[i].band[2][k].a_height = h8 * 2;
+            }
+            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[3]); k++) {
+                s->plane[i].band[3][k].a_width  = w8 * 4;
+                s->plane[i].band[3][k].a_height = h8 * 4;
+            }
+            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[4]); k++) {
+                s->plane[i].band[4][k].a_width  = w8 * 4;
+                s->plane[i].band[4][k].a_height = h8 * 4;
+            }
+            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[5]); k++) {
+                s->plane[i].band[5][k].a_width  = w8 * 4;
+                s->plane[i].band[5][k].a_height = h8 * 4;
             }
         }
 
@@ -327,6 +400,7 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
     GetByteContext gb;
     ThreadFrame frame = { .f = data };
     AVFrame *pic = data;
+    s->next_frame = av_frame_alloc();
     int ret = 0, i, j, planes, plane, got_buffer = 0;
     int16_t *coeff_data;
 
@@ -349,6 +423,9 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
         } else if (tag == SampleFlags) {
             av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
             s->progressive = data & 0x0001;
+        } else if (tag == Pframe) {
+            s->Pframe = 1;
+            av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
         } else if (tag == ImageWidth) {
             av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
             s->coded_width = data;
@@ -373,7 +450,7 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             }
         } else if (tag == SubbandCount) {
             av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
-            if (data != SUBBAND_COUNT) {
+            if (data != 10 && data != 17) {
                 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
                 ret = AVERROR_PATCHWELCOME;
                 break;
@@ -405,7 +482,7 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
         } else if (tag == 51) {
             av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
             s->subband_num_actual = data;
-            if (s->subband_num_actual >= 10) {
+            if (s->subband_num_actual >= 17 && s->subband_num_actual != 255) {
                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
                 ret = AVERROR(EINVAL);
                 break;
@@ -420,9 +497,15 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             s->prescale_shift[1] = (data >> 3) & 0x7;
             s->prescale_shift[2] = (data >> 6) & 0x7;
             av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
+        } else if (tag == EncodingMethod) {
+            s->encode_method = data;
+            av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n",s->subband_num_actual, data);
         } else if (tag == 27) {
             av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
-            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
+            if (s->coded_width == 0){
+                s->coded_width = data << 3;
+              }
+                if (data < 3) {
                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
                 ret = AVERROR(EINVAL);
                 break;
@@ -431,20 +514,21 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             s->plane[s->channel_num].band[0][0].stride = data;
         } else if (tag == 28) {
             av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
-            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
+            if (s->coded_height == 0)
+                s->coded_height = data << 3;
+            if (data < 3) {
                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
                 ret = AVERROR(EINVAL);
                 break;
             }
             s->plane[s->channel_num].band[0][0].height = data;
-        } else if (tag == 1)
+        } else if (tag == 1) {
+            s->sample_type = data;
+            if (data == 2)
+                s->Pframe = 1;
             av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
-        else if (tag == 10) {
-            if (data != 0) {
-                avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
-                ret = AVERROR_PATCHWELCOME;
-                break;
-            }
+        } else if (tag == 10) {
+            s->transform_type = data;
             av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
         } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
             if (abstag == 0x4001)
@@ -502,9 +586,8 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
                 break;
             }
             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
-        } else if (tag == 71) {
-            s->codebook = data;
-            av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
+        } else if (tag == -71) {
+            av_log(avctx, AV_LOG_DEBUG, "color format %d\n", data);
         } else if (tag == 72) {
             s->codebook = data & 0xf;
             s->difference_coding = (data >> 4) & 1;
@@ -547,12 +630,14 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
         } else if (tag == -74 && s->peak.offset) {
             s->peak.level = data;
             s->peak.base += s->peak.offset / 2 - 2;
-        } else
+        } else if (tag == 82);
+        else
             av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
 
         /* Some kind of end of header tag */
-        if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
+        if ((tag == 4 && (data == 0x1a4a || data == 0xf0f) || s->sample_type == 6 || s->sample_type == 1) && s->coded_width && s->coded_height &&
             s->coded_format != AV_PIX_FMT_NONE) {
+            av_log(avctx, AV_LOG_DEBUG,  "this summers's gonna hurt\n");
             if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
                 s->a_format != s->coded_format) {
                 free_buffers(s);
@@ -620,7 +705,7 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
         }
 
-        if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
+        if ((tag == 55 || tag == 82) && s->a_width && s->a_height) {
             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
@@ -638,9 +723,11 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             }
 
             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
-                av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
-                ret = AVERROR(EINVAL);
-                goto end;
+                  if (s->subband_num_actual != 255) {
+                      av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
+                      ret = AVERROR(EINVAL);
+                      goto end;
+                  }
             }
             expected = highpass_height * highpass_stride;
 
@@ -648,8 +735,13 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
 
             init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
             {
+                if(s->subband_num_actual == 255){
+                    expected = 0;
+                    goto finish;
+                }
+
                 OPEN_READER(re, &s->gb);
-                if (!s->codebook) {
+                if (!s->codebook && !(s->transform_type == 2 && s->subband_num_actual == 7)) {
                     while (1) {
                         UPDATE_CACHE(re, &s->gb);
                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
@@ -664,9 +756,14 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
                         if (count > expected)
                             break;
 
-                        coeff = dequant_and_decompand(level, s->quantisation, 0);
+                        coeff = dequant_and_decompand(level, s->quantisation, 0, (s->sample_type == 2 || s->sample_type == 3) && s->Pframe && s->subband_num_actual == 7);
                         for (i = 0; i < run; i++)
-                            *coeff_data++ = coeff;
+                            if (tag != 82)
+                                *coeff_data++  = coeff;
+                            else {
+                                *coeff_data   |= coeff << 8;
+                                *coeff_data++ *= s->quantisation;
+                            }
                     }
                     if (s->peak.level)
                         peak_table(coeff_data - expected, &s->peak, expected);
@@ -688,9 +785,14 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
                         if (count > expected)
                             break;
 
-                        coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
+                        coeff = dequant_and_decompand(level, s->quantisation, s->codebook, (s->sample_type == 2 || s->sample_type == 3) && s->Pframe && s->subband_num_actual == 7);
                         for (i = 0; i < run; i++)
-                            *coeff_data++ = coeff;
+                            if (tag != 82)
+                                *coeff_data++  = coeff;
+                            else {
+                                *coeff_data   |= coeff << 8;
+                                *coeff_data++ *= s->quantisation;
+                            }
                     }
                     if (s->peak.level)
                         peak_table(coeff_data - expected, &s->peak, expected);
@@ -701,12 +803,12 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
                 CLOSE_READER(re, &s->gb);
             }
 
-            if (count > expected) {
+            if (count > expected && s->subband_num_actual != 255) {
                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
                 ret = AVERROR(EINVAL);
                 goto end;
             }
-
+            finish:
             bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
             if (bytes > bytestream2_get_bytes_left(&gb)) {
                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
@@ -726,14 +828,14 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             }
         }
     }
-
-    if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
-        s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
+#if 0
+    if ((!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
+        s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) && s->sample_type != 1) {
         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
         ret = AVERROR(EINVAL);
         goto end;
     }
-
+#endif
     if (!got_buffer) {
         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
         ret = AVERROR(EINVAL);
@@ -741,8 +843,9 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
     }
 
     planes = av_pix_fmt_count_planes(avctx->pix_fmt);
+    if (s->transform_type == 0 && s->sample_type != 1) {
     for (plane = 0; plane < planes && !ret; plane++) {
-        /* level 1 */
+            /* level 1 */
         int lowpass_height  = s->plane[plane].band[0][0].height;
         int lowpass_width   = s->plane[plane].band[0][0].width;
         int highpass_stride = s->plane[plane].band[0][1].stride;
@@ -921,14 +1024,249 @@  static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
             low  = s->plane[plane].l_h[6];
             high = s->plane[plane].l_h[7];
             for (i = 0; i < lowpass_height; i++) {
-                interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
+                temporal_inverse_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, 0);
                 low  += lowpass_width * 2;
                 high += lowpass_width * 2;
                 dst  += pic->linesize[act_plane];
             }
         }
     }
+    } else if (s->transform_type == 2 && s->sample_type != 1) {
+          for (plane = 0; plane < planes && !ret; plane++) {
+              /* level 1 */
+              int lowpass_height  = s->plane[plane].band[0][0].height;
+              int lowpass_width   = s->plane[plane].band[0][0].width;
+              int highpass_stride = s->plane[plane].band[0][1].stride;
+              int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
+              int16_t *low, *high, *output, *dst;
+
+              if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
+                  !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
+                  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
+                  ret = AVERROR(EINVAL);
+                  goto end;
+              }
+
+              av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
+
+              low    = s->plane[plane].subband[0];
+              high   = s->plane[plane].subband[2];
+              output = s->plane[plane].l_h[0];
+              for (i = 0; i < lowpass_width; i++) {
+                  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
+                  low++;
+                  high++;
+                  output++;
+              }
+
+              low    = s->plane[plane].subband[1];
+              high   = s->plane[plane].subband[3];
+              output = s->plane[plane].l_h[1];
+
+              for (i = 0; i < lowpass_width; i++) {
+                  // note the stride of "low" is highpass_stride
+                  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
+                  low++;
+                  high++;
+                  output++;
+              }
+
+              low    = s->plane[plane].l_h[0];
+              high   = s->plane[plane].l_h[1];
+              output = s->plane[plane].subband[0];
+              for (i = 0; i < lowpass_height * 2; i++) {
+                  horiz_filter(output, low, high, lowpass_width);
+                  low    += lowpass_width;
+                  high   += lowpass_width;
+                  output += lowpass_width * 2;
+              }
+              if (s->bpc == 12) {
+                  output = s->plane[plane].subband[0];
+                  for (i = 0; i < lowpass_height * 2; i++) {
+                      for (j = 0; j < lowpass_width * 2; j++)
+                          output[j] *= 4;
+
+                      output += lowpass_width * 2;
+                  }
+              }
+
+              /* level 2 */
+              lowpass_height  = s->plane[plane].band[1][1].height;
+              lowpass_width   = s->plane[plane].band[1][1].width;
+              highpass_stride = s->plane[plane].band[1][1].stride;
+
+              if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
+                  !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
+                  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
+                  ret = AVERROR(EINVAL);
+                  goto end;
+              }
+
+              av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
+
+              low    = s->plane[plane].subband[0];
+              high   = s->plane[plane].subband[5];
+              output = s->plane[plane].l_h[3];
+              for (i = 0; i < lowpass_width; i++) {
+                  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
+                  low++;
+                  high++;
+                  output++;
+              }
+
+              low    = s->plane[plane].subband[4];
+              high   = s->plane[plane].subband[6];
+              output = s->plane[plane].l_h[4];
+              for (i = 0; i < lowpass_width; i++) {
+                  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
+                  low++;
+                  high++;
+                  output++;
+              }
+
+              low    = s->plane[plane].l_h[3];
+              high   = s->plane[plane].l_h[4];
+              output = s->plane[plane].subband[0];
+              for (i = 0; i < lowpass_height * 2; i++) {
+                  horiz_filter(output, low, high, lowpass_width);
+                  low    += lowpass_width;
+                  high   += lowpass_width;
+                  output += lowpass_width * 2;
+              }
+
+              output = s->plane[plane].subband[0];
+              for (i = 0; i < lowpass_height * 2; i++) {
+                  for (j = 0; j < lowpass_width * 2; j++)
+                      output[j] *= 4;
+
+                  output += lowpass_width * 2;
+              }
+
+              lowpass_height  = s->plane[plane].band[2][1].height;
+              lowpass_width   = s->plane[plane].band[2][1].width;
+              highpass_stride = s->plane[plane].band[2][1].stride;
+              av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
+
+              low    = s->plane[plane].subband[7];
+              high   = s->plane[plane].subband[9];
+              output = s->plane[plane].l_h[3];
+              for (i = 0; i < lowpass_width; i++) {
+                  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
+                  low++;
+                  high++;
+                  output++;
+              }
+
+              low    = s->plane[plane].subband[8];
+              high   = s->plane[plane].subband[10];
+              output = s->plane[plane].l_h[4];
+              for (i = 0; i < lowpass_width; i++) {
+                  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
+                  low++;
+                  high++;
+                  output++;
+              }
+
+              low    = s->plane[plane].l_h[3];
+              high   = s->plane[plane].l_h[4];
+              output = s->plane[plane].subband[7];
+              for (i = 0; i < lowpass_height; i++) {
+                  horiz_filter(output, low, high, lowpass_width);
+                  low    += lowpass_width;
+                  high   += lowpass_width;
+                  output += lowpass_width * 2;
+              }
+
+              lowpass_height  = s->plane[plane].band[3][1].height;
+              lowpass_width   = s->plane[plane].band[3][1].width;
+              highpass_stride = s->plane[plane].band[3][1].stride;
+              av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
+
+              if (lowpass_height > s->plane[plane].band[3][1].a_height || lowpass_width > s->plane[plane].band[3][1].a_width ||
+                  !highpass_stride || s->plane[plane].band[3][1].width > s->plane[plane].band[3][1].a_width) {
+                  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
+                  ret = AVERROR(EINVAL);
+                  goto end;
+              }
+
+              low    = s->plane[plane].subband[0];
+              high   = s->plane[plane].subband[7];
+              output = s->plane[plane].subband[0];
+              for (i = 0; i < lowpass_height; i++) {
+                  temporal_inverse_filter(output, low, high, lowpass_width, 4 * lowpass_width * lowpass_height, 1);
+                  low    += lowpass_width;
+                  high   += lowpass_width;
+                  output += lowpass_width;
+              }
+
+              av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
+              if (s->progressive) {
+                  low    = s->plane[plane].subband[0];
+                  high   = s->plane[plane].subband[12];
+                  output = s->plane[plane].l_h[6];
+                  for (i = 0; i < lowpass_width; i++) {
+                      vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
+                      low++;
+                      high++;
+                      output++;
+                  }
+
+                  low    = s->plane[plane].subband[11];
+                  high   = s->plane[plane].subband[13];
+                  output = s->plane[plane].l_h[7];
+                  for (i = 0; i < lowpass_width; i++) {
+                      vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
+                      low++;
+                      high++;
+                      output++;
+                  }
+
+                  dst = (int16_t *)pic->data[act_plane];
+                  low  = s->plane[plane].l_h[6];
+                  high = s->plane[plane].l_h[7];
+                  for (i = 0; i < lowpass_height * 2; i++) {
+                      horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
+                      low  += lowpass_width;
+                      high += lowpass_width;
+                      dst  += pic->linesize[act_plane] / 2;
+                  }
+
+                  low    = s->plane[plane].subband[7];
+                  high   = s->plane[plane].subband[15];
+                  output = s->plane[plane].l_h[6];
+                  for (i = 0; i < lowpass_width; i++) {
+                      vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
+                      low++;
+                      high++;
+                      output++;
+                  }
+
+                  low    = s->plane[plane].subband[14];
+                  high   = s->plane[plane].subband[16];
+                  output = s->plane[plane].l_h[7];
+                  for (i = 0; i < lowpass_width; i++) {
+                      vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
+                      low++;
+                      high++;
+                      output++;
+                  }
+
+                  low  = s->plane[plane].l_h[6];
+                  high = s->plane[plane].l_h[7];
+                  output = s->plane[plane].subband[7];
+                  for (i = 0; i < lowpass_height * 2; i++) {
+                      horiz_filter_clip(output, low, high, lowpass_width, s->bpc);
+                      low  += lowpass_width;
+                      high += lowpass_width;
+                      output += lowpass_width;
+                  }
+                  s->next_frame->data[act_plane] = output;
 
+            }
+        }
+    } else if (s->transform_type == 2 && s->sample_type == 1) {
+        av_frame_ref(pic, s->next_frame);
+    }
 
 end:
     if (ret < 0)
@@ -961,6 +1299,6 @@  AVCodec ff_cfhd_decoder = {
     .init             = cfhd_init,
     .close            = cfhd_close,
     .decode           = cfhd_decode,
-    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
-    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
+    .capabilities     = AV_CODEC_CAP_DR1,
+    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
 };
diff --git a/libavcodec/cfhd.h b/libavcodec/cfhd.h
index 7cd251fca7..0126bd4052 100644
--- a/libavcodec/cfhd.h
+++ b/libavcodec/cfhd.h
@@ -30,15 +30,15 @@ 
 #include "vlc.h"
 
 #define VLC_BITS       9
-#define SUBBAND_COUNT 10
-
+#define SUBBAND_COUNT 17
+#define BAND_END_TRAILER (1 << 15) - 1
 typedef struct CFHD_RL_VLC_ELEM {
     int16_t level;
     int8_t len;
     uint16_t run;
 } CFHD_RL_VLC_ELEM;
 
-#define DWT_LEVELS 3
+#define DWT_LEVELS 6
 
 typedef struct SubBand {
     int level;
@@ -82,9 +82,16 @@  typedef struct CFHDContext {
 
     CFHD_RL_VLC_ELEM table_18_rl_vlc[4572];
     VLC vlc_18;
-
+    AVFrame * next_frame;
     GetBitContext gb;
 
+    int sample_type;
+    int transform_type;
+    int num_spatial;
+    int num_frames;
+    int encode_method;
+    int first_wavelet;
+    int Pframe;
     int coded_width;
     int coded_height;
     int cropped_height;