Guest User

Untitled

a guest
Aug 11th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 62.50 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
  3.  *
  4.  * This file is part of FFmpeg.
  5.  *
  6.  * FFmpeg is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * FFmpeg is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with FFmpeg; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19.  */
  20.  
  21. /**
  22.  * @file
  23.  * Cineform HD video decoder
  24.  */
  25.  
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/buffer.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32.  
  33. #include "avcodec.h"
  34. #include "bytestream.h"
  35. #include "get_bits.h"
  36. #include "internal.h"
  37. #include "thread.h"
  38. #include "cfhd.h"
  39.  
  40. #define ALPHA_COMPAND_DC_OFFSET 256
  41. #define ALPHA_COMPAND_GAIN 9400
  42.  
  43. enum CFHDParam {
  44.     TransformType    =  10,
  45.     ChannelCount     =  12,
  46.     SubbandCount     =  14,
  47.     Pframe           =  19,
  48.     ImageWidth       =  20,
  49.     ImageHeight      =  21,
  50.     LowpassPrecision =  35,
  51.     SubbandNumber    =  48,
  52.     EncodingMethod   =  52,
  53.     Quantization     =  53,
  54.     ChannelNumber    =  62,
  55.     SampleFlags      =  68,
  56.     BitsPerComponent = 101,
  57.     ChannelWidth     = 104,
  58.     ChannelHeight    = 105,
  59.     PrescaleShift    = 109,
  60. };
  61.  
  62.  
  63.  
  64. static av_cold int cfhd_init(AVCodecContext *avctx)
  65. {
  66.     CFHDContext *s = avctx->priv_data;
  67.  
  68.     if (!avctx->internal->is_copy)
  69.         avctx->internal->allocate_progress = 1;
  70.     avctx->bits_per_raw_sample = 10;
  71.     s->avctx                   = avctx;
  72.     s->i_frame.f = av_frame_alloc();
  73.     s->p_frame.f = av_frame_alloc();
  74.  
  75.     return ff_cfhd_init_vlcs(s);
  76. }
  77.  
  78. static void init_plane_defaults(CFHDContext *s)
  79. {
  80.     s->subband_num        = 0;
  81.     s->level              = 0;
  82.     s->subband_num_actual = 0;
  83. }
  84.  
  85. static void init_peak_table_defaults(CFHDContext *s)
  86. {
  87.     s->peak.level  = 0;
  88.     s->peak.offset = 0;
  89.     s->peak.base   = NULL;
  90. }
  91.  
  92. static void init_frame_defaults(CFHDContext *s)
  93. {
  94.     s->sample_type       = 0;
  95.     s->transform_type    = 0;
  96.     s->num_frames        = 0;
  97.     s->pframe            = 0;
  98.     s->first_wavelet     = 0;
  99.     s->coded_width       = 0;
  100.     s->coded_height      = 0;
  101.     s->cropped_height    = 0;
  102.     s->bpc               = 10;
  103.     s->channel_cnt       = 4;
  104.     s->subband_cnt       = SUBBAND_COUNT;
  105.     s->channel_num       = 0;
  106.     s->lowpass_precision = 16;
  107.     s->quantisation      = 1;
  108.     s->wavelet_depth     = 3;
  109.     s->pshift            = 1;
  110.     s->codebook          = 0;
  111.     s->difference_coding = 0;
  112.     s->progressive       = 0;
  113.     init_plane_defaults(s);
  114.     init_peak_table_defaults(s);
  115. }
  116.  
  117. /* TODO: merge with VLC tables or use LUT */
  118. static inline int dequant_and_decompand(int level, int quantisation, int codebook, int lossless)
  119. {
  120.     if (lossless)
  121.         return level;
  122.     if (codebook == 0) {
  123.         if (level >= 40 && level < 264) {
  124.             if (level >= 54) {
  125.                 level  -= 54;
  126.                 level <<= 2;
  127.                 level  += 54;
  128.             }
  129.             level  -= 40;
  130.             level <<= 2;
  131.             level  += 40;
  132.         } else if (level <= -40) {
  133.             level = -level;
  134.             if (level >= 54) {
  135.                 level  -= 54;
  136.                 level <<= 2;
  137.                 level  += 54;
  138.             }
  139.             level  -= 40;
  140.             level <<= 2;
  141.             level  += 40;
  142.             level   = -level;
  143.         }
  144.         return level * quantisation;
  145.     } else if (codebook == 1) {
  146.         int64_t abslevel = abs(level);
  147.         if (level < 264)
  148.             return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
  149.                FFSIGN(level) * quantisation;
  150.         else
  151.             return level * quantisation;
  152.     } else
  153.         return level * quantisation;
  154. }
  155.  
  156. static inline void difference_coding(int16_t *band, int width, int height)
  157. {
  158.  
  159.     int i,j;
  160.     for (i = 0; i < height; i++) {
  161.         for (j = 1; j < width; j++) {
  162.           band[j] += band[j-1];
  163.         }
  164.         band += width;
  165.     }
  166. }
  167.  
  168. static inline void peak_table(int16_t *band, Peak *peak, int length)
  169. {
  170.     int i;
  171.     for (i = 0; i < length; i++)
  172.         if (abs(band[i]) > peak->level)
  173.             band[i] = *(peak->base++);
  174. }
  175.  
  176. static inline void process_alpha(int16_t *alpha, int width)
  177. {
  178.     int i, channel;
  179.     for (i = 0; i < width; i++) {
  180.         channel   = alpha[i];
  181.         channel  -= ALPHA_COMPAND_DC_OFFSET;
  182.         channel <<= 3;
  183.         channel  *= ALPHA_COMPAND_GAIN;
  184.         channel >>= 16;
  185.         channel   = av_clip_uintp2(channel, 12);
  186.         alpha[i]  = channel;
  187.     }
  188. }
  189.  
  190. static inline void filter(int16_t *output, ptrdiff_t out_stride,
  191.                           int16_t *low, ptrdiff_t low_stride,
  192.                           int16_t *high, ptrdiff_t high_stride,
  193.                           int len, int clip)
  194. {
  195.     int16_t tmp;
  196.     int i;
  197.  
  198.     for (i = 0; i < len; i++) {
  199.         if (i == 0) {
  200.             tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
  201.             output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
  202.             if (clip)
  203.                 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  204.  
  205.             tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
  206.             output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
  207.             if (clip)
  208.                 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  209.         } else if (i == len-1) {
  210.             tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
  211.             output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
  212.             if (clip)
  213.                 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  214.  
  215.             tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
  216.             output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
  217.             if (clip)
  218.                 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  219.         } else {
  220.             tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
  221.             output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
  222.             if (clip)
  223.                 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  224.  
  225.             tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
  226.             output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
  227.             if (clip)
  228.                 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  229.         }
  230.     }
  231. }
  232.  
  233. static inline void temporal_inverse_filter(int16_t *output, int16_t *low, int16_t *high,
  234.                          int width, int linesize, int temporal_for_highpass)
  235. {
  236.     int i;
  237.     int16_t even, odd;
  238.     for (i = 0; i < width; i++) {
  239.         even = (low[i] - high[i])/2;
  240.         odd  = (low[i] + high[i])/2;
  241.         if (!temporal_for_highpass) {
  242.             output[i]            = av_clip_uintp2(even, 10);
  243.             output[i + linesize] = av_clip_uintp2(odd, 10);
  244.         } else {
  245.             low[i]  = even;
  246.             high[i] = odd;
  247.         }
  248.     }
  249. }
  250. static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
  251.                          int width)
  252. {
  253.     filter(output, 1, low, 1, high, 1, width, 0);
  254. }
  255.  
  256. static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
  257.                               int width, int clip)
  258. {
  259.     filter(output, 1, low, 1, high, 1, width, clip);
  260. }
  261.  
  262. static void vert_filter(int16_t *output, ptrdiff_t out_stride,
  263.                         int16_t *low, ptrdiff_t low_stride,
  264.                         int16_t *high, ptrdiff_t high_stride, int len)
  265. {
  266.     filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
  267. }
  268.  
  269. static void free_buffers(CFHDContext *s)
  270. {
  271.     int i, j;
  272.  
  273.     for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  274.         av_freep(&s->plane[i].idwt_buf);
  275.         av_freep(&s->plane[i].idwt_tmp);
  276.         if (s->transform_type == 0)
  277.             for (j = 0; j < 9; j++)
  278.                 s->plane[i].subband[j] = NULL;
  279.         else
  280.             for (j = 0; j < 17; j++)
  281.                 s->plane[i].subband[j] = NULL;
  282.  
  283.         for (j = 0; j < 8; j++)
  284.             s->plane[i].l_h[j] = NULL;
  285.     }
  286.     s->a_height = 0;
  287.     s->a_width  = 0;
  288. }
  289.  
  290. static int alloc_buffers(AVCodecContext *avctx)
  291. {
  292.     CFHDContext *s = avctx->priv_data;
  293.     int i, j, ret, planes;
  294.     int chroma_x_shift, chroma_y_shift;
  295.     unsigned k;
  296.  
  297.     if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  298.         return ret;
  299.     avctx->pix_fmt = s->coded_format;
  300.  
  301.     if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  302.                                                 &chroma_x_shift,
  303.                                                 &chroma_y_shift)) < 0)
  304.         return ret;
  305.     planes = av_pix_fmt_count_planes(s->coded_format);
  306.  
  307.     for (i = 0; i < planes; i++) {
  308.         int w8, h8, w4, h4, w2, h2;
  309.         int16_t *frame2;
  310.         int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
  311.         int height = i ? avctx->height >> chroma_y_shift : avctx->height;
  312.         ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
  313.         if (chroma_y_shift)
  314.             height = FFALIGN(height / 8, 2) * 8;
  315.         s->plane[i].width  = width;
  316.         s->plane[i].height = height;
  317.         s->plane[i].stride = stride;
  318.  
  319.         w8 = FFALIGN(s->plane[i].width  / 8, 8);
  320.         h8 = height / 8;
  321.         w4 = w8 * 2;
  322.         h4 = h8 * 2;
  323.         w2 = w4 * 2;
  324.         h2 = h4 * 2;
  325.  
  326.         if (s->transform_type == 0) {
  327.             s->plane[i].idwt_buf =
  328.                 av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
  329.             s->plane[i].idwt_tmp =
  330.                 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
  331.             if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  332.                 return AVERROR(ENOMEM);
  333.         } else if (s->transform_type == 2) {
  334.             s->plane[i].idwt_buf =
  335.                 av_mallocz_array(2 * height * stride, sizeof(*s->plane[i].idwt_buf));
  336.             s->plane[i].idwt_tmp =
  337.                 av_malloc_array(2 * height * stride, sizeof(*s->plane[i].idwt_tmp));
  338.             if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  339.                 return AVERROR(ENOMEM);
  340.         }
  341.  
  342.         if (s->transform_type == 0) {
  343.             s->plane[i].subband[0] = s->plane[i].idwt_buf;
  344.             s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  345.             s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  346.             s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  347.             s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  348.             s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  349.             s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  350.             s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  351.             s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  352.             s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  353.         } else if (s->transform_type == 2) {
  354.             s->plane[i].subband[0]  = s->plane[i].idwt_buf;
  355.             s->plane[i].subband[1]  = s->plane[i].idwt_buf + 2 * w8 * h8;
  356.             s->plane[i].subband[2]  = s->plane[i].idwt_buf + 1 * w8 * h8;
  357.             s->plane[i].subband[3]  = s->plane[i].idwt_buf + 3 * w8 * h8;
  358.             s->plane[i].subband[4]  = s->plane[i].idwt_buf + 2 * w4 * h4;
  359.             s->plane[i].subband[5]  = s->plane[i].idwt_buf + 1 * w4 * h4;
  360.             s->plane[i].subband[6]  = s->plane[i].idwt_buf + 3 * w4 * h4;
  361.             frame2 =
  362.             s->plane[i].subband[7]  = s->plane[i].idwt_buf + 4 * w2 * h2;
  363.             s->plane[i].subband[8]  = frame2 + 2 * w4 * h4;
  364.             s->plane[i].subband[9]  = frame2 + 1 * w4 * h4;
  365.             s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
  366.             s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
  367.             s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
  368.             s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
  369.             s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
  370.             s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
  371.             s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
  372.         }
  373.  
  374.         if (s->transform_type == 0) {
  375.             for (j = 0; j < DWT_LEVELS - 3; j++) {
  376.                 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  377.                     s->plane[i].band[j][k].a_width  = w8 << j;
  378.                     s->plane[i].band[j][k].a_height = h8 << j;
  379.                 }
  380.             }
  381.         } else if (s->transform_type == 2) {
  382.             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[0]); k++) {
  383.                 s->plane[i].band[0][k].a_width  = w8;
  384.                 s->plane[i].band[0][k].a_height = h8;
  385.             }
  386.             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[1]); k++) {
  387.                 s->plane[i].band[1][k].a_width  = w8 * 2;
  388.                 s->plane[i].band[1][k].a_height = h8 * 2;
  389.             }
  390.             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[2]); k++) {
  391.                 s->plane[i].band[2][k].a_width  = w8 * 2;
  392.                 s->plane[i].band[2][k].a_height = h8 * 2;
  393.             }
  394.             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[3]); k++) {
  395.                 s->plane[i].band[3][k].a_width  = w8 * 4;
  396.                 s->plane[i].band[3][k].a_height = h8 * 4;
  397.             }
  398.             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[4]); k++) {
  399.                 s->plane[i].band[4][k].a_width  = w8 * 4;
  400.                 s->plane[i].band[4][k].a_height = h8 * 4;
  401.             }
  402.             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[5]); k++) {
  403.                 s->plane[i].band[5][k].a_width  = w8 * 4;
  404.                 s->plane[i].band[5][k].a_height = h8 * 4;
  405.             }
  406.         }
  407.  
  408.         /* ll2 and ll1 commented out because they are done in-place */
  409.         s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  410.         s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  411.         // s->plane[i].l_h[2] = ll2;
  412.         s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  413.         s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  414.         // s->plane[i].l_h[5] = ll1;
  415.         s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  416.         s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  417.         if (s->transform_type == 2) {
  418.             s->plane[i].l_h[8] = s->plane[i].idwt_tmp + 4 * w2 * h2;
  419.             s->plane[i].l_h[9] = s->plane[i].idwt_tmp + 6 * w2 * h2;
  420.             }
  421.     }
  422.  
  423.     s->a_height = s->coded_height;
  424.     s->a_width  = s->coded_width;
  425.     s->a_format = s->coded_format;
  426.  
  427.     return 0;
  428. }
  429.  
  430. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  431. {
  432.     CFHDContext *csrc = src->priv_data;
  433.     CFHDContext *cdst = dst->priv_data;
  434.     cdst->transform_type = csrc->transform_type;
  435.     if (csrc->sample_type != 1 && csrc->transform_type != 0) {
  436.         cdst->progressive = csrc->progressive;
  437.         cdst->picture = &csrc->p_frame;
  438.         cdst->connection = &csrc->i_frame;
  439.         cdst->buffers = csrc->plane;
  440.     }
  441.  
  442.     return 0;
  443. }
  444.  
  445. static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
  446.                        AVPacket *avpkt)
  447. {
  448.     CFHDContext *s = avctx->priv_data;
  449.     GetByteContext gb;
  450.     ThreadFrame frame = { .f = data };
  451.     int ret = 0, i, j, planes, plane, got_buffer = 0, progress1 = 1, progress2 = 1;
  452.     int16_t *coeff_data;
  453.  
  454.     s->coded_format = AV_PIX_FMT_YUV422P10;
  455.     init_frame_defaults(s);
  456.     planes = av_pix_fmt_count_planes(s->coded_format);
  457.  
  458.     bytestream2_init(&gb, avpkt->data, avpkt->size);
  459.  
  460.     while (bytestream2_get_bytes_left(&gb) > 4) {
  461.         /* Bit weird but implement the tag parsing as the spec says */
  462.         uint16_t tagu   = bytestream2_get_be16(&gb);
  463.         int16_t tag     = (int16_t)tagu;
  464.         int8_t tag8     = (int8_t)(tagu >> 8);
  465.         uint16_t abstag = abs(tag);
  466.         int8_t abs_tag8 = abs(tag8);
  467.         uint16_t data   = bytestream2_get_be16(&gb);
  468.         if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  469.             av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  470.         } else if (tag == SampleFlags) {
  471.             av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
  472.             s->progressive = data & 0x0001;
  473.         } else if (tag == Pframe) {
  474.             s->pframe = 1;
  475.             av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
  476.         } else if (tag == ImageWidth) {
  477.             av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  478.             s->coded_width = data;
  479.         } else if (tag == ImageHeight) {
  480.             av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  481.             s->coded_height = data;
  482.         } else if (tag == 101) {
  483.             av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
  484.             if (data < 1 || data > 31) {
  485.                 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
  486.                 ret = AVERROR(EINVAL);
  487.                 break;
  488.             }
  489.             s->bpc = data;
  490.         } else if (tag == ChannelCount) {
  491.             av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  492.             s->channel_cnt = data;
  493.             if (data > 4) {
  494.                 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  495.                 ret = AVERROR_PATCHWELCOME;
  496.                 break;
  497.             }
  498.         } else if (tag == SubbandCount) {
  499.             av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  500.             if (data != 10 && data != 17) {
  501.                 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  502.                 ret = AVERROR_PATCHWELCOME;
  503.                 break;
  504.             }
  505.         } else if (tag == ChannelNumber) {
  506.             s->channel_num = data;
  507.             av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  508.             if (s->channel_num >= planes) {
  509.                 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  510.                 ret = AVERROR(EINVAL);
  511.                 break;
  512.             }
  513.             init_plane_defaults(s);
  514.         } else if (tag == SubbandNumber) {
  515.             if (s->subband_num != 0 && data == 1)  // hack
  516.                 s->level++;
  517.             av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  518.             s->subband_num = data;
  519.             if (s->level >= DWT_LEVELS) {
  520.                 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  521.                 ret = AVERROR(EINVAL);
  522.                 break;
  523.             }
  524.             if (s->subband_num > 3) {
  525.                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  526.                 ret = AVERROR(EINVAL);
  527.                 break;
  528.             }
  529.         } else if (tag == 51) {
  530.             av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  531.             s->subband_num_actual = data;
  532.             if (s->subband_num_actual >= 17 && s->subband_num_actual != 255) {
  533.                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  534.                 ret = AVERROR(EINVAL);
  535.                 break;
  536.             }
  537.         } else if (tag == LowpassPrecision)
  538.             av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  539.         else if (tag == Quantization) {
  540.             s->quantisation = data;
  541.             av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  542.         } else if (tag == PrescaleShift) {
  543.             s->prescale_shift[0] = (data >> 0) & 0x7;
  544.             s->prescale_shift[1] = (data >> 3) & 0x7;
  545.             s->prescale_shift[2] = (data >> 6) & 0x7;
  546.             av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
  547.         } else if (tag == EncodingMethod) {
  548.             s->encode_method = data;
  549.             av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n",s->subband_num_actual, data);
  550.         } else if (tag == 27) {
  551.             av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  552.             if (s->coded_width == 0){
  553.                 s->coded_width = data << 3;
  554.               }
  555.                 if (data < 3) {
  556.                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  557.                 ret = AVERROR(EINVAL);
  558.                 break;
  559.             }
  560.             s->plane[s->channel_num].band[0][0].width  = data;
  561.             s->plane[s->channel_num].band[0][0].stride = data;
  562.         } else if (tag == 28) {
  563.             av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  564.             if (s->coded_height == 0)
  565.                 s->coded_height = data << 3;
  566.             if (data < 3) {
  567.                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  568.                 ret = AVERROR(EINVAL);
  569.                 break;
  570.             }
  571.             s->plane[s->channel_num].band[0][0].height = data;
  572.         } else if (tag == 1) {
  573.             s->sample_type = data;
  574.             if (data == 2)
  575.                 s->pframe  = 1;
  576.             else if (data == 1)
  577.                 s->transform_type = 2;
  578.             av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  579.         } else if (tag == 10) {
  580.             s->transform_type = data;
  581.             av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
  582.         } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  583.             if (abstag == 0x4001)
  584.                 s->peak.level = 0;
  585.             av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  586.             bytestream2_skipu(&gb, data * 4);
  587.         } else if (tag == 23) {
  588.             av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
  589.             avpriv_report_missing_feature(avctx, "Skip frame");
  590.             ret = AVERROR_PATCHWELCOME;
  591.             break;
  592.         } else if (tag == 2) {
  593.             av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
  594.             if (data > bytestream2_get_bytes_left(&gb) / 4) {
  595.                 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
  596.                 ret = AVERROR_INVALIDDATA;
  597.                 break;
  598.             }
  599.             for (i = 0; i < data; i++) {
  600.                 uint16_t tag2 = bytestream2_get_be16(&gb);
  601.                 uint16_t val2 = bytestream2_get_be16(&gb);
  602.                 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
  603.             }
  604.         } else if (tag == 41) {
  605.             av_log(avctx, AV_LOG_DEBUG, "Highpass width %i channel %i level %i subband %i\n", data, s->channel_num, s->level, s->subband_num);
  606.             if (data < 3) {
  607.                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  608.                 ret = AVERROR(EINVAL);
  609.                 break;
  610.             }
  611.             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
  612.             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  613.         } else if (tag == 42) {
  614.             av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  615.             if (data < 3) {
  616.                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  617.                 ret = AVERROR(EINVAL);
  618.                 break;
  619.             }
  620.             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  621.         } else if (tag == 49) {
  622.             av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  623.             if (data < 3) {
  624.                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  625.                 ret = AVERROR(EINVAL);
  626.                 break;
  627.             }
  628.             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
  629.             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  630.         } else if (tag == 50) {
  631.             av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  632.             if (data < 3) {
  633.                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  634.                 ret = AVERROR(EINVAL);
  635.                 break;
  636.             }
  637.             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  638.         } else if (tag == 71) {
  639.             s->codebook = data;
  640.             av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
  641.         } else if (tag == 72) {
  642.             s->codebook = data & 0xf;
  643.             s->difference_coding = (data >> 4) & 1;
  644.             av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  645.         } else if (tag == 70) {
  646.             av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
  647.             if (!(data == 10 || data == 12)) {
  648.                 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  649.                 ret = AVERROR(EINVAL);
  650.                 break;
  651.             }
  652.             s->bpc = data;
  653.         } else if (tag == 84) {
  654.             av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  655.             if (data == 1)
  656.                 s->coded_format = AV_PIX_FMT_YUV422P10;
  657.             else if (data == 3)
  658.                 s->coded_format = AV_PIX_FMT_GBRP12;
  659.             else if (data == 4)
  660.                 s->coded_format = AV_PIX_FMT_GBRAP12;
  661.             else {
  662.                 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  663.                 ret = AVERROR_PATCHWELCOME;
  664.                 break;
  665.             }
  666.             planes = av_pix_fmt_count_planes(s->coded_format);
  667.         } else if (tag == -85) {
  668.             av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  669.             s->cropped_height = data;
  670.         } else if (tag == -75) {
  671.             s->peak.offset &= ~0xffff;
  672.             s->peak.offset |= (data & 0xffff);
  673.             s->peak.base    = (int16_t *) gb.buffer;
  674.             s->peak.level   = 0;
  675.         } else if (tag == -76) {
  676.             s->peak.offset &= 0xffff;
  677.             s->peak.offset |= (data & 0xffff)<<16;
  678.             s->peak.base    = (int16_t *) gb.buffer;
  679.             s->peak.level   = 0;
  680.         } else if (tag == -74 && s->peak.offset) {
  681.             s->peak.level = data;
  682.             s->peak.base += s->peak.offset / 2 - 2;
  683.         } else if (tag == 82);
  684.         else
  685.             av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
  686.  
  687.         /* Some kind of end of header tag */
  688.         if (((tag == 4 && data == 0x1a4a) || s->sample_type == 1) && s->coded_width && s->coded_height &&
  689.             s->coded_format != AV_PIX_FMT_NONE && s->sample_type != 3 && s->sample_type != 6) {
  690.             if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  691.                 s->a_format != s->coded_format) {
  692.                 free_buffers(s);
  693.                 if ((ret = alloc_buffers(avctx)) < 0) {
  694.                     free_buffers(s);
  695.                     return ret;
  696.                 }
  697.             }
  698.             if (s->transform_type == 2) {
  699.                 //IP samples
  700.                 if (s->sample_type != 1) {
  701.                     s->picture = &s->i_frame;
  702.                     s->connection = &s->p_frame;
  703.                     s->buffers = s->plane;
  704.                 }
  705.                 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  706.                 if (ret < 0)
  707.                     return ret;
  708.                 if (s->sample_type != 1) {
  709.                     if (s->i_frame.f->data[0])
  710.                         ff_thread_release_buffer(avctx, &s->i_frame);
  711.                     if (s->p_frame.f->data[0])
  712.                         ff_thread_release_buffer(avctx, &s->p_frame);
  713.                     av_frame_copy_props(s->i_frame.f, frame.f);
  714.                     av_frame_copy_props(s->p_frame.f, frame.f);
  715.                     if (s->cropped_height)
  716.                         avctx->height = s->cropped_height;
  717.                     s->picture->f->width =
  718.                     s->picture->f->height = 0;
  719.                     s->connection->f->width =
  720.                     s->connection->f->height = 0;
  721.                     if ((ret = ff_thread_get_buffer(avctx, s->picture, 0)) < 0)
  722.                         return ret;
  723.                     if ((ret = ff_thread_get_buffer(avctx, s->connection, 0)) < 0)
  724.                         return ret;
  725.                 }
  726.             } else {
  727.                 //only intra frame samples
  728.                 s->picture = &s->i_frame;
  729.                 s->buffers = s->plane;
  730.                 if (s->picture->f->data[0])
  731.                     ff_thread_release_buffer(avctx, s->picture);
  732.                 av_frame_copy_props(s->i_frame.f, frame.f);
  733.                 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  734.                 if (ret < 0)
  735.                     return ret;
  736.                 if (s->cropped_height)
  737.                     avctx->height = s->cropped_height;
  738.                 s->picture->f->width =
  739.                 s->picture->f->height = 0;
  740.                 if ((ret = ff_thread_get_buffer(avctx, s->picture, 0)) < 0)
  741.                     return ret;
  742.             }
  743.             s->coded_width = 0;
  744.             s->coded_height = 0;
  745.             s->coded_format = AV_PIX_FMT_NONE;
  746.             got_buffer = 1;
  747.             ff_thread_finish_setup(avctx);
  748.         }
  749.         coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  750.  
  751.         /* Lowpass coefficients */
  752.         if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
  753.             int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  754.             int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
  755.             int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  756.             int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
  757.  
  758.             if (!got_buffer) {
  759.                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  760.                 ret = AVERROR(EINVAL);
  761.                 goto end;
  762.             }
  763.  
  764.             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  765.                 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  766.                 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  767.                 ret = AVERROR(EINVAL);
  768.                 goto end;
  769.             }
  770.  
  771.             av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  772.             for (i = 0; i < lowpass_height; i++) {
  773.                 for (j = 0; j < lowpass_width; j++)
  774.                     coeff_data[j] = bytestream2_get_be16u(&gb);
  775.  
  776.                 coeff_data += lowpass_width;
  777.             }
  778.  
  779.             /* Align to mod-4 position to continue reading tags */
  780.             bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  781.  
  782.             /* Copy last line of coefficients if odd height */
  783.             if (lowpass_height & 1) {
  784.                 memcpy(&coeff_data[lowpass_height * lowpass_width],
  785.                        &coeff_data[(lowpass_height - 1) * lowpass_width],
  786.                        lowpass_width * sizeof(*coeff_data));
  787.             }
  788.  
  789.             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  790.         }
  791.  
  792.         if ((tag == 55 || tag == 82) && s->a_width && s->a_height) {
  793.             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  794.             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  795.             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  796.             int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  797.             int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  798.             int expected;
  799.             int a_expected = highpass_a_height * highpass_a_width;
  800.             int level, run, coeff;
  801.             int count = 0, bytes;
  802.  
  803.             if (!got_buffer) {
  804.                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  805.                 ret = AVERROR(EINVAL);
  806.                 goto end;
  807.             }
  808.  
  809.             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  810.                   if (s->subband_num_actual != 255) {
  811.                       av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  812.                       ret = AVERROR(EINVAL);
  813.                       goto end;
  814.                   }
  815.             }
  816.             expected = highpass_height * highpass_stride;
  817.  
  818.             av_log(avctx, AV_LOG_DEBUG, "Start subband coeffs plane %i level %i codebook %i expected %i\n", s->channel_num, s->level, s->codebook, expected);
  819.  
  820.             init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  821.             if (s->subband_num_actual == 255) {
  822.                 expected = 0;
  823.                 goto finish;
  824.             }
  825.             {
  826.                 OPEN_READER(re, &s->gb);
  827.                 if (!s->codebook && !(s->transform_type == 2 && s->subband_num_actual == 7)) {
  828.                     while (1) {
  829.                         UPDATE_CACHE(re, &s->gb);
  830.                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  831.                                    VLC_BITS, 3, 1);
  832.  
  833.                         /* escape */
  834.                         if (level == 64)
  835.                             break;
  836.  
  837.                         count += run;
  838.  
  839.                         if (count > expected)
  840.                             break;
  841.  
  842.                         coeff = dequant_and_decompand(level, s->quantisation, 0, (s->sample_type == 2 || s->sample_type == 3) && s->pframe && s->subband_num_actual == 7 && s->encode_method == 5);
  843.                         for (i = 0; i < run; i++)
  844.                             if (tag != 82)
  845.                                 *coeff_data++  = coeff;
  846.                             else {
  847.                                 *coeff_data   |= coeff << 8;
  848.                                 *coeff_data++ *= s->quantisation;
  849.                             }
  850.                     }
  851.                     if (s->peak.level)
  852.                         peak_table(coeff_data - expected, &s->peak, expected);
  853.                     if (s->difference_coding)
  854.                         difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  855.  
  856.                 } else {
  857.                     while (1) {
  858.                         UPDATE_CACHE(re, &s->gb);
  859.                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  860.                                    VLC_BITS, 3, 1);
  861.  
  862.                         /* escape */
  863.                         if (level == 255 && run == 2)
  864.                             break;
  865.  
  866.                         count += run;
  867.  
  868.                         if (count > expected)
  869.                             break;
  870.  
  871.                         coeff = dequant_and_decompand(level, s->quantisation, s->codebook, (s->sample_type == 2 || s->sample_type == 3) && s->pframe && s->subband_num_actual == 7 && s->encode_method == 5);
  872.                         for (i = 0; i < run; i++)
  873.                             if (tag != 82)
  874.                                 *coeff_data++  = coeff;
  875.                             else {
  876.                                 *coeff_data   |= coeff << 8;
  877.                                 *coeff_data++ *= s->quantisation;
  878.                             }
  879.                     }
  880.                     if (s->peak.level)
  881.                         peak_table(coeff_data - expected, &s->peak, expected);
  882.                     if (s->difference_coding)
  883.                         difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  884.  
  885.                 }
  886.                 CLOSE_READER(re, &s->gb);
  887.             }
  888.  
  889.             if (count > expected && s->subband_num_actual != 255) {
  890.                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  891.                 ret = AVERROR(EINVAL);
  892.                 goto end;
  893.             }
  894.             finish:
  895.             bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  896.             if (bytes > bytestream2_get_bytes_left(&gb)) {
  897.                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  898.                 ret = AVERROR(EINVAL);
  899.                 goto end;
  900.             } else
  901.                 bytestream2_seek(&gb, bytes, SEEK_CUR);
  902.  
  903.             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  904.             s->codebook = 0;
  905.  
  906.             /* Copy last line of coefficients if odd height */
  907.             if (highpass_height & 1) {
  908.                 memcpy(&coeff_data[highpass_height * highpass_stride],
  909.                        &coeff_data[(highpass_height - 1) * highpass_stride],
  910.                        highpass_stride * sizeof(*coeff_data));
  911.             }
  912.             if (s->transform_type == 2 && s->subband_num_actual == 10)
  913.                 ff_thread_report_progress(s->picture, progress1 <<= 1, 0);
  914.         }
  915.     }
  916. #if 0
  917.     if ((!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  918.         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) && s->sample_type != 1) {
  919.         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  920.         ret = AVERROR(EINVAL);
  921.         goto end;
  922.     }
  923. #endif
  924.     if (!got_buffer) {
  925.         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  926.         ret = AVERROR(EINVAL);
  927.         goto end;
  928.     }
  929.  
  930.     planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  931.     if (s->transform_type == 0 && s->sample_type != 1) {
  932.     for (plane = 0; plane < planes && !ret; plane++) {
  933.             /* level 1 */
  934.         int lowpass_height  = s->plane[plane].band[0][0].height;
  935.         int lowpass_width   = s->plane[plane].band[0][0].width;
  936.         int highpass_stride = s->plane[plane].band[0][1].stride;
  937.         int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  938.         int16_t *low, *high, *output, *dst;
  939.  
  940.         if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  941.             !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  942.             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  943.             ret = AVERROR(EINVAL);
  944.             goto end;
  945.         }
  946.  
  947.         av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  948.  
  949.         low    = s->plane[plane].subband[0];
  950.         high   = s->plane[plane].subband[2];
  951.         output = s->plane[plane].l_h[0];
  952.         for (i = 0; i < lowpass_width; i++) {
  953.             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  954.             low++;
  955.             high++;
  956.             output++;
  957.         }
  958.  
  959.         low    = s->plane[plane].subband[1];
  960.         high   = s->plane[plane].subband[3];
  961.         output = s->plane[plane].l_h[1];
  962.  
  963.         for (i = 0; i < lowpass_width; i++) {
  964.             // note the stride of "low" is highpass_stride
  965.             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  966.             low++;
  967.             high++;
  968.             output++;
  969.         }
  970.  
  971.         low    = s->plane[plane].l_h[0];
  972.         high   = s->plane[plane].l_h[1];
  973.         output = s->plane[plane].subband[0];
  974.         for (i = 0; i < lowpass_height * 2; i++) {
  975.             horiz_filter(output, low, high, lowpass_width);
  976.             low    += lowpass_width;
  977.             high   += lowpass_width;
  978.             output += lowpass_width * 2;
  979.         }
  980.         if (s->bpc == 12) {
  981.             output = s->plane[plane].subband[0];
  982.             for (i = 0; i < lowpass_height * 2; i++) {
  983.                 for (j = 0; j < lowpass_width * 2; j++)
  984.                     output[j] *= 4;
  985.  
  986.                 output += lowpass_width * 2;
  987.             }
  988.         }
  989.  
  990.         /* level 2 */
  991.         lowpass_height  = s->plane[plane].band[1][1].height;
  992.         lowpass_width   = s->plane[plane].band[1][1].width;
  993.         highpass_stride = s->plane[plane].band[1][1].stride;
  994.  
  995.         if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  996.             !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  997.             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  998.             ret = AVERROR(EINVAL);
  999.             goto end;
  1000.         }
  1001.  
  1002.         av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1003.  
  1004.         low    = s->plane[plane].subband[0];
  1005.         high   = s->plane[plane].subband[5];
  1006.         output = s->plane[plane].l_h[3];
  1007.         for (i = 0; i < lowpass_width; i++) {
  1008.             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1009.             low++;
  1010.             high++;
  1011.             output++;
  1012.         }
  1013.  
  1014.         low    = s->plane[plane].subband[4];
  1015.         high   = s->plane[plane].subband[6];
  1016.         output = s->plane[plane].l_h[4];
  1017.         for (i = 0; i < lowpass_width; i++) {
  1018.             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1019.             low++;
  1020.             high++;
  1021.             output++;
  1022.         }
  1023.  
  1024.         low    = s->plane[plane].l_h[3];
  1025.         high   = s->plane[plane].l_h[4];
  1026.         output = s->plane[plane].subband[0];
  1027.         for (i = 0; i < lowpass_height * 2; i++) {
  1028.             horiz_filter(output, low, high, lowpass_width);
  1029.             low    += lowpass_width;
  1030.             high   += lowpass_width;
  1031.             output += lowpass_width * 2;
  1032.         }
  1033.  
  1034.         output = s->plane[plane].subband[0];
  1035.         for (i = 0; i < lowpass_height * 2; i++) {
  1036.             for (j = 0; j < lowpass_width * 2; j++)
  1037.                 output[j] *= 4;
  1038.  
  1039.             output += lowpass_width * 2;
  1040.         }
  1041.  
  1042.         /* level 3 */
  1043.         lowpass_height  = s->plane[plane].band[2][1].height;
  1044.         lowpass_width   = s->plane[plane].band[2][1].width;
  1045.         highpass_stride = s->plane[plane].band[2][1].stride;
  1046.  
  1047.         if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  1048.             !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  1049.             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1050.             ret = AVERROR(EINVAL);
  1051.             goto end;
  1052.         }
  1053.         av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1054.         if (s->progressive) {
  1055.             low    = s->plane[plane].subband[0];
  1056.             high   = s->plane[plane].subband[8];
  1057.             output = s->plane[plane].l_h[6];
  1058.             for (i = 0; i < lowpass_width; i++) {
  1059.                 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1060.                 low++;
  1061.                 high++;
  1062.                 output++;
  1063.             }
  1064.  
  1065.             low    = s->plane[plane].subband[7];
  1066.             high   = s->plane[plane].subband[9];
  1067.             output = s->plane[plane].l_h[7];
  1068.             for (i = 0; i < lowpass_width; i++) {
  1069.                 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1070.                 low++;
  1071.                 high++;
  1072.                 output++;
  1073.             }
  1074.  
  1075.             dst = (int16_t *)s->picture->f->data[act_plane];
  1076.             low  = s->plane[plane].l_h[6];
  1077.             high = s->plane[plane].l_h[7];
  1078.             for (i = 0; i < lowpass_height * 2; i++) {
  1079.                 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1080.                 low  += lowpass_width;
  1081.                 high += lowpass_width;
  1082.                 dst  += s->picture->f->linesize[act_plane] / 2;
  1083.             }
  1084.         } else {
  1085.             av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", s->picture->f->interlaced_frame);
  1086.             s->picture->f->interlaced_frame = 1;
  1087.             low    = s->plane[plane].subband[0];
  1088.             high   = s->plane[plane].subband[7];
  1089.             output = s->plane[plane].l_h[6];
  1090.             for (i = 0; i < lowpass_height; i++) {
  1091.                 horiz_filter(output, low, high, lowpass_width);
  1092.                 low    += lowpass_width;
  1093.                 high   += lowpass_width;
  1094.                 output += lowpass_width * 2;
  1095.             }
  1096.  
  1097.             low    = s->plane[plane].subband[8];
  1098.             high   = s->plane[plane].subband[9];
  1099.             output = s->plane[plane].l_h[7];
  1100.             for (i = 0; i < lowpass_height; i++) {
  1101.                 horiz_filter(output, low, high, lowpass_width);
  1102.                 low    += lowpass_width;
  1103.                 high   += lowpass_width;
  1104.                 output += lowpass_width * 2;
  1105.             }
  1106.  
  1107.             dst  = (int16_t *)s->picture->f->data[act_plane];
  1108.             low  = s->plane[plane].l_h[6];
  1109.             high = s->plane[plane].l_h[7];
  1110.             for (i = 0; i < lowpass_height; i++) {
  1111.                 temporal_inverse_filter(dst, low, high, lowpass_width * 2,  s->picture->f->linesize[act_plane]/2, 0);
  1112.                 low  += lowpass_width * 2;
  1113.                 high += lowpass_width * 2;
  1114.                 dst  += s->picture->f->linesize[act_plane];
  1115.             }
  1116.         }
  1117.     }
  1118.     av_frame_ref(frame.f, s->picture->f);
  1119.     ff_thread_report_progress(s->picture, INT_MAX, 0);
  1120.     } else if (s->transform_type == 2 && s->sample_type != 1) {
  1121.           for (plane = 0; plane < planes && !ret; plane++) {
  1122.               /* level 1 */
  1123.               int lowpass_height  = s->plane[plane].band[0][0].height;
  1124.               int lowpass_width   = s->plane[plane].band[0][0].width;
  1125.               int highpass_stride = s->plane[plane].band[0][1].stride;
  1126.               int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  1127.               int16_t *low, *high, *output, *dst;
  1128.  
  1129.               if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  1130.                   !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  1131.                   av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1132.                   ret = AVERROR(EINVAL);
  1133.                   goto end;
  1134.               }
  1135.  
  1136.               av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1137.  
  1138.               low    = s->plane[plane].subband[0];
  1139.               high   = s->plane[plane].subband[2];
  1140.               output = s->plane[plane].l_h[0];
  1141.               for (i = 0; i < lowpass_width; i++) {
  1142.                   vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1143.                   low++;
  1144.                   high++;
  1145.                   output++;
  1146.               }
  1147.  
  1148.               low    = s->plane[plane].subband[1];
  1149.               high   = s->plane[plane].subband[3];
  1150.               output = s->plane[plane].l_h[1];
  1151.               for (i = 0; i < lowpass_width; i++) {
  1152.                   vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1153.                   low++;
  1154.                   high++;
  1155.                   output++;
  1156.               }
  1157.  
  1158.               low    = s->plane[plane].l_h[0];
  1159.               high   = s->plane[plane].l_h[1];
  1160.               output = s->plane[plane].subband[0];
  1161.               for (i = 0; i < lowpass_height * 2; i++) {
  1162.                   horiz_filter(output, low, high, lowpass_width);
  1163.                   low    += lowpass_width;
  1164.                   high   += lowpass_width;
  1165.                   output += lowpass_width * 2;
  1166.               }
  1167.               if (s->bpc == 12) {
  1168.                   output = s->plane[plane].subband[0];
  1169.                   for (i = 0; i < lowpass_height * 2; i++) {
  1170.                       for (j = 0; j < lowpass_width * 2; j++)
  1171.                           output[j] *= 4;
  1172.  
  1173.                       output += lowpass_width * 2;
  1174.                   }
  1175.               }
  1176.  
  1177.               /* level 2 */
  1178.               lowpass_height  = s->plane[plane].band[1][1].height;
  1179.               lowpass_width   = s->plane[plane].band[1][1].width;
  1180.               highpass_stride = s->plane[plane].band[1][1].stride;
  1181.  
  1182.               if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  1183.                   !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  1184.                   av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1185.                   ret = AVERROR(EINVAL);
  1186.                   goto end;
  1187.               }
  1188.  
  1189.               av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1190.  
  1191.               low    = s->plane[plane].subband[0];
  1192.               high   = s->plane[plane].subband[5];
  1193.               output = s->plane[plane].l_h[3];
  1194.               for (i = 0; i < lowpass_width; i++) {
  1195.                   vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1196.                   low++;
  1197.                   high++;
  1198.                   output++;
  1199.               }
  1200.  
  1201.               low    = s->plane[plane].subband[4];
  1202.               high   = s->plane[plane].subband[6];
  1203.               output = s->plane[plane].l_h[4];
  1204.               for (i = 0; i < lowpass_width; i++) {
  1205.                   vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1206.                   low++;
  1207.                   high++;
  1208.                   output++;
  1209.               }
  1210.  
  1211.               low    = s->plane[plane].l_h[3];
  1212.               high   = s->plane[plane].l_h[4];
  1213.               output = s->plane[plane].subband[0];
  1214.               for (i = 0; i < lowpass_height * 2; i++) {
  1215.                   horiz_filter(output, low, high, lowpass_width);
  1216.                   low    += lowpass_width;
  1217.                   high   += lowpass_width;
  1218.                   output += lowpass_width * 2;
  1219.               }
  1220.  
  1221.               output = s->plane[plane].subband[0];
  1222.               for (i = 0; i < lowpass_height * 2; i++) {
  1223.                   for (j = 0; j < lowpass_width * 2; j++)
  1224.                       output[j] *= 4;
  1225.  
  1226.                   output += lowpass_width * 2;
  1227.               }
  1228.  
  1229.               lowpass_height  = s->plane[plane].band[3][1].height;
  1230.               lowpass_width   = s->plane[plane].band[4][1].width;
  1231.               highpass_stride = s->plane[plane].band[4][1].stride;
  1232.               av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1233.  
  1234.               if (lowpass_height > s->plane[plane].band[3][1].a_height || lowpass_width > s->plane[plane].band[3][1].a_width ||
  1235.                   !highpass_stride || s->plane[plane].band[3][1].width > s->plane[plane].band[3][1].a_width) {
  1236.                   av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1237.                   ret = AVERROR(EINVAL);
  1238.                   goto end;
  1239.               }
  1240.               ff_thread_await_progress(s->connection, progress2 <<= 1, 0);
  1241.               low    = s->plane[plane].subband[0];
  1242.               high   = s->plane[plane].subband[7];
  1243.               output = s->plane[plane].subband[0];
  1244.               for (i = 0; i < lowpass_height; i++) {
  1245.                   temporal_inverse_filter(output, low, high, lowpass_width, 4 * lowpass_width * lowpass_height, 1);
  1246.                   low    += lowpass_width;
  1247.                   high   += lowpass_width;
  1248.               }
  1249.               ff_thread_report_progress(s->picture, progress1 <<= 1, 0);
  1250.               av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1251.               if (s->progressive) {
  1252.                   low    = s->plane[plane].subband[0];
  1253.                   high   = s->plane[plane].subband[15];
  1254.                   output = s->plane[plane].l_h[6];
  1255.                   for (i = 0; i < lowpass_width; i++) {
  1256.                       vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1257.                       low++;
  1258.                       high++;
  1259.                       output++;
  1260.                   }
  1261.  
  1262.                   low    = s->plane[plane].subband[14];
  1263.                   high   = s->plane[plane].subband[16];
  1264.                   output = s->plane[plane].l_h[7];
  1265.                   for (i = 0; i < lowpass_width; i++) {
  1266.                       vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1267.                       low++;
  1268.                       high++;
  1269.                       output++;
  1270.                   }
  1271.  
  1272.                   dst = (int16_t *)s->picture->f->data[act_plane];
  1273.                   low  = s->plane[plane].l_h[6];
  1274.                   high = s->plane[plane].l_h[7];
  1275.                   for (i = 0; i < lowpass_height * 2; i++) {
  1276.                       horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1277.                       low  += lowpass_width;
  1278.                       high += lowpass_width;
  1279.                       dst  += s->picture->f->linesize[act_plane] / 2;
  1280.                   }
  1281.               } else {
  1282.                   av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", s->picture->f->interlaced_frame);
  1283.                   s->picture->f->interlaced_frame = 1;
  1284.                   low    = s->plane[plane].subband[0];
  1285.                   high   = s->plane[plane].subband[14];
  1286.                   output = s->plane[plane].l_h[6];
  1287.                   for (i = 0; i < lowpass_height; i++) {
  1288.                       horiz_filter(output, low, high, lowpass_width);
  1289.                       low    += lowpass_width;
  1290.                       high   += lowpass_width;
  1291.                       output += lowpass_width * 2;
  1292.                   }
  1293.  
  1294.                   low    = s->plane[plane].subband[15];
  1295.                   high   = s->plane[plane].subband[16];
  1296.                   output = s->plane[plane].l_h[7];
  1297.                   for (i = 0; i < lowpass_height; i++) {
  1298.                       horiz_filter(output, low, high, lowpass_width);
  1299.                       low    += lowpass_width;
  1300.                       high   += lowpass_width;
  1301.                       output += lowpass_width * 2;
  1302.                   }
  1303.  
  1304.                   dst  = (int16_t *)s->picture->f->data[act_plane];
  1305.                   low  = s->plane[plane].l_h[6];
  1306.                   high = s->plane[plane].l_h[7];
  1307.                   for (i = 0; i < lowpass_height; i++) {
  1308.                       temporal_inverse_filter(dst, low, high, lowpass_width * 2,  s->picture->f->linesize[act_plane]/2, 0);
  1309.                       low  += lowpass_width * 2;
  1310.                       high += lowpass_width * 2;
  1311.                       dst  += s->picture->f->linesize[act_plane];
  1312.                   }
  1313.               }
  1314.             }
  1315.             ff_thread_report_progress(s->picture, INT_MAX, 0);
  1316.             ff_thread_await_progress(s->connection, INT_MAX, 0);
  1317.             av_frame_ref(frame.f, s->picture->f);
  1318.     } else if (s->sample_type == 1) {
  1319.         int16_t *low, *high, *dst, *output;
  1320.         int lowpass_height, lowpass_width, highpass_stride, act_plane;
  1321.         progress1 = 1, progress2 = 1;
  1322.         for (plane = 0; plane < planes && !ret; plane++) {
  1323.             ff_thread_await_progress(s->connection, progress1 <<= 1, 0);
  1324.             // highpass inverse for temporal
  1325.             lowpass_height  = s->buffers[plane].band[1][1].a_height;
  1326.             lowpass_width   = s->buffers[plane].band[1][1].a_width;
  1327.             highpass_stride = s->buffers[plane].band[1][1].a_width;
  1328.             av_log(avctx, AV_LOG_DEBUG, "Level 2 plane for highpass sample 1 %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1329.             low    = s->buffers[plane].subband[7];
  1330.             high   = s->buffers[plane].subband[9];
  1331.             output = s->buffers[plane].l_h[3];
  1332.             for (i = 0; i < lowpass_width; i++) {
  1333.                 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1334.                 low++;
  1335.                 high++;
  1336.                 output++;
  1337.             }
  1338.  
  1339.             low    = s->buffers[plane].subband[8];
  1340.             high   = s->buffers[plane].subband[10];
  1341.             output = s->buffers[plane].l_h[4];
  1342.             for (i = 0; i < lowpass_width; i++) {
  1343.                 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1344.                 low++;
  1345.                 high++;
  1346.                 output++;
  1347.             }
  1348.  
  1349.             low    = s->buffers[plane].l_h[3];
  1350.             high   = s->buffers[plane].l_h[4];
  1351.             output = s->buffers[plane].subband[7];
  1352.             for (i = 0; i < lowpass_height * 2; i++) {
  1353.                 horiz_filter(output, low, high, lowpass_width);
  1354.                 low    += lowpass_width;
  1355.                 high   += lowpass_width;
  1356.                 output += lowpass_width * 2;
  1357.             }
  1358.             ff_thread_report_progress(s->picture, progress2 <<= 1, 0);
  1359.         }
  1360.         for (plane = 0; plane < planes && !ret; plane++) {
  1361.             ff_thread_await_progress(s->connection, progress1 <<= 1, 0);
  1362.  
  1363.             act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  1364.             lowpass_height  = s->buffers[plane].band[4][1].a_height;
  1365.             lowpass_width   = s->buffers[plane].band[4][1].a_width;
  1366.             highpass_stride = s->buffers[plane].band[4][1].a_width;
  1367.             av_log(avctx, AV_LOG_DEBUG, "Level 3 plane sample 1 %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1368.  
  1369.             if (s->progressive) {
  1370.                 low    = s->buffers[plane].subband[7];
  1371.                 high   = s->buffers[plane].subband[12];
  1372.                 output = s->buffers[plane].l_h[8];
  1373.                 for (i = 0; i < lowpass_width; i++) {
  1374.                     vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1375.                     low++;
  1376.                     high++;
  1377.                     output++;
  1378.                 }
  1379.  
  1380.                 low    = s->buffers[plane].subband[11];
  1381.                 high   = s->buffers[plane].subband[13];
  1382.                 output = s->buffers[plane].l_h[9];
  1383.                 for (i = 0; i < lowpass_width; i++) {
  1384.                     vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1385.                     low++;
  1386.                     high++;
  1387.                     output++;
  1388.                 }
  1389.  
  1390.                 dst = (int16_t *)s->picture->f->data[act_plane];
  1391.                 low  = s->buffers[plane].l_h[8];
  1392.                 high = s->buffers[plane].l_h[9];
  1393.                 for (i = 0; i < lowpass_height * 2; i++) {
  1394.                     horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1395.                     low  += lowpass_width;
  1396.                     high += lowpass_width;
  1397.                     dst  += s->picture->f->linesize[act_plane] / 2;
  1398.                 }
  1399.             } else {
  1400.                 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", s->picture->f->interlaced_frame);
  1401.                 s->picture->f->interlaced_frame = 1;
  1402.                 low    = s->buffers[plane].subband[7];
  1403.                 high   = s->buffers[plane].subband[11];
  1404.                 output = s->buffers[plane].l_h[8];
  1405.                 for (i = 0; i < lowpass_height; i++) {
  1406.                     horiz_filter(output, low, high, lowpass_width);
  1407.                     low    += lowpass_width;
  1408.                     high   += lowpass_width;
  1409.                     output += lowpass_width * 2;
  1410.                 }
  1411.  
  1412.                 low    = s->buffers[plane].subband[12];
  1413.                 high   = s->buffers[plane].subband[13];
  1414.                 output = s->buffers[plane].l_h[9];
  1415.                 for (i = 0; i < lowpass_height; i++) {
  1416.                     horiz_filter(output, low, high, lowpass_width);
  1417.                     low    += lowpass_width;
  1418.                     high   += lowpass_width;
  1419.                     output += lowpass_width * 2;
  1420.                 }
  1421.  
  1422.                 dst  = (int16_t *)s->picture->f->data[act_plane];
  1423.                 low  = s->buffers[plane].l_h[8];
  1424.                 high = s->buffers[plane].l_h[9];
  1425.                 for (i = 0; i < lowpass_height; i++) {
  1426.                     temporal_inverse_filter(dst, low, high, lowpass_width * 2,  s->picture->f->linesize[act_plane]/2, 0);
  1427.                     low  += lowpass_width * 2;
  1428.                     high += lowpass_width * 2;
  1429.                     dst  += s->picture->f->linesize[act_plane];
  1430.                 }
  1431.             }
  1432.         }
  1433.         ff_thread_report_progress(s->picture, INT_MAX, 0);
  1434.         ff_thread_await_progress(s->connection, INT_MAX, 0);
  1435.         av_frame_ref(frame.f, s->picture->f);
  1436.     }
  1437.  
  1438. end:
  1439.     if (ret < 0)
  1440.         return ret;
  1441.  
  1442.     *got_frame = 1;
  1443.     return avpkt->size;
  1444. }
  1445.  
  1446. static av_cold int cfhd_close(AVCodecContext *avctx)
  1447. {
  1448.     CFHDContext *s = avctx->priv_data;
  1449.  
  1450.     free_buffers(s);
  1451.  
  1452.     if (!avctx->internal->is_copy) {
  1453.         ff_free_vlc(&s->vlc_9);
  1454.         ff_free_vlc(&s->vlc_18);
  1455.     }
  1456.     if (s->i_frame.f && s->i_frame.f->data[0])
  1457.         ff_thread_release_buffer(avctx, &s->i_frame);
  1458.     if (s->p_frame.f && s->p_frame.f->data[0])
  1459.         ff_thread_release_buffer(avctx, &s->p_frame);
  1460.  
  1461.     if (s->i_frame.f)
  1462.         av_frame_free(&s->i_frame.f);
  1463.     if (s->p_frame.f)
  1464.         av_frame_free(&s->p_frame.f);
  1465.  
  1466.     return 0;
  1467. }
  1468.  
  1469. AVCodec ff_cfhd_decoder = {
  1470.     .name                  = "cfhd",
  1471.     .long_name             = NULL_IF_CONFIG_SMALL("Cineform HD"),
  1472.     .type                  = AVMEDIA_TYPE_VIDEO,
  1473.     .id                    = AV_CODEC_ID_CFHD,
  1474.     .priv_data_size        = sizeof(CFHDContext),
  1475.     .init                  = cfhd_init,
  1476.     .close                 = cfhd_close,
  1477.     .decode                = cfhd_decode,
  1478.     .update_thread_context = update_thread_context,
  1479.     .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1480.     .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  1481. };
Add Comment
Please, Sign In to add comment