LCOV - code coverage report
Current view: top level - libavcodec - cfhd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 430 549 78.3 %
Date: 2017-12-18 06:23:41 Functions: 12 12 100.0 %

          Line data    Source code
       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             : enum CFHDParam {
      41             :     ChannelCount     =  12,
      42             :     SubbandCount     =  14,
      43             :     ImageWidth       =  20,
      44             :     ImageHeight      =  21,
      45             :     LowpassPrecision =  35,
      46             :     SubbandNumber    =  48,
      47             :     Quantization     =  53,
      48             :     ChannelNumber    =  62,
      49             :     BitsPerComponent = 101,
      50             :     ChannelWidth     = 104,
      51             :     ChannelHeight    = 105,
      52             :     PrescaleShift    = 109,
      53             : };
      54             : 
      55           6 : static av_cold int cfhd_init(AVCodecContext *avctx)
      56             : {
      57           6 :     CFHDContext *s = avctx->priv_data;
      58             : 
      59           6 :     avctx->bits_per_raw_sample = 10;
      60           6 :     s->avctx                   = avctx;
      61             : 
      62           6 :     return ff_cfhd_init_vlcs(s);
      63             : }
      64             : 
      65          99 : static void init_plane_defaults(CFHDContext *s)
      66             : {
      67          99 :     s->subband_num        = 0;
      68          99 :     s->level              = 0;
      69          99 :     s->subband_num_actual = 0;
      70          99 : }
      71             : 
      72          33 : static void init_frame_defaults(CFHDContext *s)
      73             : {
      74          33 :     s->coded_width       = 0;
      75          33 :     s->coded_height      = 0;
      76          33 :     s->cropped_height    = 0;
      77          33 :     s->bpc               = 10;
      78          33 :     s->channel_cnt       = 4;
      79          33 :     s->subband_cnt       = SUBBAND_COUNT;
      80          33 :     s->channel_num       = 0;
      81          33 :     s->lowpass_precision = 16;
      82          33 :     s->quantisation      = 1;
      83          33 :     s->wavelet_depth     = 3;
      84          33 :     s->pshift            = 1;
      85          33 :     s->codebook          = 0;
      86          33 :     init_plane_defaults(s);
      87          33 : }
      88             : 
      89             : /* TODO: merge with VLC tables or use LUT */
      90     3522977 : static inline int dequant_and_decompand(int level, int quantisation)
      91             : {
      92     3522977 :     int64_t abslevel = abs(level);
      93     3522977 :     return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
      94     3522977 :            FFSIGN(level) * quantisation;
      95             : }
      96             : 
      97      158158 : static inline void filter(int16_t *output, ptrdiff_t out_stride,
      98             :                           int16_t *low, ptrdiff_t low_stride,
      99             :                           int16_t *high, ptrdiff_t high_stride,
     100             :                           int len, int clip)
     101             : {
     102             :     int16_t tmp;
     103             :     int i;
     104             : 
     105    28658014 :     for (i = 0; i < len; i++) {
     106    28499856 :         if (i == 0) {
     107      158158 :             tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
     108      158158 :             output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
     109      158158 :             if (clip)
     110       39864 :                 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
     111             : 
     112      158158 :             tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
     113      158158 :             output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
     114      158158 :             if (clip)
     115       39864 :                 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
     116    28341698 :         } else if (i == len-1) {
     117      158158 :             tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
     118      158158 :             output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
     119      158158 :             if (clip)
     120       39864 :                 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
     121             : 
     122      158158 :             tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
     123      158158 :             output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
     124      158158 :             if (clip)
     125       39864 :                 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
     126             :         } else {
     127    28183540 :             tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
     128    28183540 :             output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
     129    28183540 :             if (clip)
     130    10777360 :                 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
     131             : 
     132    28183540 :             tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
     133    28183540 :             output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
     134    28183540 :             if (clip)
     135    10777360 :                 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
     136             :         }
     137             :     }
     138      158158 : }
     139             : 
     140       29898 : static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
     141             :                          int width)
     142             : {
     143       29898 :     filter(output, 1, low, 1, high, 1, width, 0);
     144       29898 : }
     145             : 
     146       39864 : static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
     147             :                               int width, int clip)
     148             : {
     149       39864 :     filter(output, 1, low, 1, high, 1, width, clip);
     150       39864 : }
     151             : 
     152       88396 : static void vert_filter(int16_t *output, ptrdiff_t out_stride,
     153             :                         int16_t *low, ptrdiff_t low_stride,
     154             :                         int16_t *high, ptrdiff_t high_stride, int len)
     155             : {
     156       88396 :     filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
     157       88396 : }
     158             : 
     159          12 : static void free_buffers(CFHDContext *s)
     160             : {
     161             :     int i, j;
     162             : 
     163          60 :     for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
     164          48 :         av_freep(&s->plane[i].idwt_buf);
     165          48 :         av_freep(&s->plane[i].idwt_tmp);
     166             : 
     167         480 :         for (j = 0; j < 9; j++)
     168         432 :             s->plane[i].subband[j] = NULL;
     169             : 
     170         432 :         for (j = 0; j < 8; j++)
     171         384 :             s->plane[i].l_h[j] = NULL;
     172             :     }
     173          12 :     s->a_height = 0;
     174          12 :     s->a_width  = 0;
     175          12 : }
     176             : 
     177           6 : static int alloc_buffers(AVCodecContext *avctx)
     178             : {
     179           6 :     CFHDContext *s = avctx->priv_data;
     180             :     int i, j, ret, planes;
     181             :     int chroma_x_shift, chroma_y_shift;
     182             :     unsigned k;
     183             : 
     184           6 :     if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
     185           0 :         return ret;
     186           6 :     avctx->pix_fmt = s->coded_format;
     187             : 
     188           6 :     if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
     189             :                                                 &chroma_x_shift,
     190             :                                                 &chroma_y_shift)) < 0)
     191           0 :         return ret;
     192           6 :     planes = av_pix_fmt_count_planes(s->coded_format);
     193             : 
     194          24 :     for (i = 0; i < planes; i++) {
     195             :         int w8, h8, w4, h4, w2, h2;
     196          18 :         int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
     197          18 :         int height = i ? avctx->height >> chroma_y_shift : avctx->height;
     198          18 :         ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
     199          18 :         height           = FFALIGN(height / 8, 2) * 8;
     200          18 :         s->plane[i].width  = width;
     201          18 :         s->plane[i].height = height;
     202          18 :         s->plane[i].stride = stride;
     203             : 
     204          18 :         w8 = FFALIGN(s->plane[i].width  / 8, 8);
     205          18 :         h8 = FFALIGN(s->plane[i].height / 8, 2);
     206          18 :         w4 = w8 * 2;
     207          18 :         h4 = h8 * 2;
     208          18 :         w2 = w4 * 2;
     209          18 :         h2 = h4 * 2;
     210             : 
     211          18 :         s->plane[i].idwt_buf =
     212          18 :             av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
     213          18 :         s->plane[i].idwt_tmp =
     214          18 :             av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
     215          18 :         if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
     216           0 :             return AVERROR(ENOMEM);
     217             : 
     218          18 :         s->plane[i].subband[0] = s->plane[i].idwt_buf;
     219          18 :         s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
     220          18 :         s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
     221          18 :         s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
     222          18 :         s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
     223          18 :         s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
     224          18 :         s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
     225          18 :         s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
     226          18 :         s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
     227          18 :         s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
     228             : 
     229          72 :         for (j = 0; j < DWT_LEVELS; j++) {
     230         270 :             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
     231         216 :                 s->plane[i].band[j][k].a_width  = w8 << j;
     232         216 :                 s->plane[i].band[j][k].a_height = h8 << j;
     233             :             }
     234             :         }
     235             : 
     236             :         /* ll2 and ll1 commented out because they are done in-place */
     237          18 :         s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
     238          18 :         s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
     239             :         // s->plane[i].l_h[2] = ll2;
     240          18 :         s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
     241          18 :         s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
     242             :         // s->plane[i].l_h[5] = ll1;
     243          18 :         s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
     244          18 :         s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
     245             :     }
     246             : 
     247           6 :     s->a_height = s->coded_height;
     248           6 :     s->a_width  = s->coded_width;
     249           6 :     s->a_format = s->coded_format;
     250             : 
     251           6 :     return 0;
     252             : }
     253             : 
     254          33 : static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
     255             :                        AVPacket *avpkt)
     256             : {
     257          33 :     CFHDContext *s = avctx->priv_data;
     258             :     GetByteContext gb;
     259          33 :     ThreadFrame frame = { .f = data };
     260          33 :     AVFrame *pic = data;
     261          33 :     int ret = 0, i, j, planes, plane, got_buffer = 0;
     262             :     int16_t *coeff_data;
     263             : 
     264          33 :     s->coded_format = AV_PIX_FMT_YUV422P10;
     265          33 :     init_frame_defaults(s);
     266          33 :     planes = av_pix_fmt_count_planes(s->coded_format);
     267             : 
     268          33 :     bytestream2_init(&gb, avpkt->data, avpkt->size);
     269             : 
     270       17226 :     while (bytestream2_get_bytes_left(&gb) > 4) {
     271             :         /* Bit weird but implement the tag parsing as the spec says */
     272       17160 :         uint16_t tagu   = bytestream2_get_be16(&gb);
     273       17160 :         int16_t tag     = (int16_t)tagu;
     274       17160 :         int8_t tag8     = (int8_t)(tagu >> 8);
     275       17160 :         uint16_t abstag = abs(tag);
     276       17160 :         int8_t abs_tag8 = abs(tag8);
     277       17160 :         uint16_t data   = bytestream2_get_be16(&gb);
     278       17160 :         if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
     279           0 :             av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
     280       17160 :         } else if (tag == ImageWidth) {
     281          33 :             av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
     282          33 :             s->coded_width = data;
     283       17127 :         } else if (tag == ImageHeight) {
     284          33 :             av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
     285          33 :             s->coded_height = data;
     286       17094 :         } else if (tag == 101) {
     287           0 :             av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
     288           0 :             if (data < 1 || data > 31) {
     289           0 :                 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
     290           0 :                 ret = AVERROR(EINVAL);
     291           0 :                 break;
     292             :             }
     293           0 :             s->bpc = data;
     294       17094 :         } else if (tag == ChannelCount) {
     295          33 :             av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
     296          33 :             s->channel_cnt = data;
     297          33 :             if (data > 4) {
     298           0 :                 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
     299           0 :                 ret = AVERROR_PATCHWELCOME;
     300           0 :                 break;
     301             :             }
     302       17061 :         } else if (tag == SubbandCount) {
     303          33 :             av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
     304          33 :             if (data != SUBBAND_COUNT) {
     305           0 :                 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
     306           0 :                 ret = AVERROR_PATCHWELCOME;
     307           0 :                 break;
     308             :             }
     309       17028 :         } else if (tag == ChannelNumber) {
     310          66 :             s->channel_num = data;
     311          66 :             av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
     312          66 :             if (s->channel_num >= planes) {
     313           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
     314           0 :                 ret = AVERROR(EINVAL);
     315           0 :                 break;
     316             :             }
     317          66 :             init_plane_defaults(s);
     318       16962 :         } else if (tag == SubbandNumber) {
     319         891 :             if (s->subband_num != 0 && data == 1)  // hack
     320         198 :                 s->level++;
     321         891 :             av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
     322         891 :             s->subband_num = data;
     323         891 :             if (s->level >= DWT_LEVELS) {
     324           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
     325           0 :                 ret = AVERROR(EINVAL);
     326           0 :                 break;
     327             :             }
     328         891 :             if (s->subband_num > 3) {
     329           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
     330           0 :                 ret = AVERROR(EINVAL);
     331           0 :                 break;
     332             :             }
     333       16071 :         } else if (tag == 51) {
     334         891 :             av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
     335         891 :             s->subband_num_actual = data;
     336         891 :             if (s->subband_num_actual >= 10) {
     337           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
     338           0 :                 ret = AVERROR(EINVAL);
     339           0 :                 break;
     340             :             }
     341       15180 :         } else if (tag == LowpassPrecision)
     342          99 :             av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
     343       15081 :         else if (tag == Quantization) {
     344         891 :             s->quantisation = data;
     345         891 :             av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
     346       14190 :         } else if (tag == PrescaleShift) {
     347           0 :             s->prescale_shift[0] = (data >> 0) & 0x7;
     348           0 :             s->prescale_shift[1] = (data >> 3) & 0x7;
     349           0 :             s->prescale_shift[2] = (data >> 6) & 0x7;
     350           0 :             av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
     351       14190 :         } else if (tag == 27) {
     352          99 :             av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
     353          99 :             if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
     354           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
     355           0 :                 ret = AVERROR(EINVAL);
     356           0 :                 break;
     357             :             }
     358          99 :             s->plane[s->channel_num].band[0][0].width  = data;
     359          99 :             s->plane[s->channel_num].band[0][0].stride = data;
     360       14091 :         } else if (tag == 28) {
     361          99 :             av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
     362          99 :             if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
     363           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
     364           0 :                 ret = AVERROR(EINVAL);
     365           0 :                 break;
     366             :             }
     367          99 :             s->plane[s->channel_num].band[0][0].height = data;
     368       13992 :         } else if (tag == 1)
     369          99 :             av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
     370       13893 :         else if (tag == 10) {
     371          33 :             if (data != 0) {
     372           0 :                 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
     373           0 :                 ret = AVERROR_PATCHWELCOME;
     374           0 :                 break;
     375             :             }
     376          33 :             av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
     377       13860 :         } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
     378          66 :             av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
     379          66 :             bytestream2_skipu(&gb, data * 4);
     380       13794 :         } else if (tag == 23) {
     381           0 :             av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
     382           0 :             avpriv_report_missing_feature(avctx, "Skip frame");
     383           0 :             ret = AVERROR_PATCHWELCOME;
     384           0 :             break;
     385       13794 :         } else if (tag == 2) {
     386          33 :             av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
     387          33 :             if (data > bytestream2_get_bytes_left(&gb) / 4) {
     388           0 :                 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
     389           0 :                 ret = AVERROR_INVALIDDATA;
     390           0 :                 break;
     391             :             }
     392         132 :             for (i = 0; i < data; i++) {
     393          99 :                 uint16_t tag2 = bytestream2_get_be16(&gb);
     394          99 :                 uint16_t val2 = bytestream2_get_be16(&gb);
     395          99 :                 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
     396             :             }
     397       13761 :         } else if (tag == 41) {
     398         297 :             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);
     399         297 :             if (data < 3) {
     400           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
     401           0 :                 ret = AVERROR(EINVAL);
     402           0 :                 break;
     403             :             }
     404         297 :             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
     405         297 :             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
     406       13464 :         } else if (tag == 42) {
     407         297 :             av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
     408         297 :             if (data < 3) {
     409           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
     410           0 :                 ret = AVERROR(EINVAL);
     411           0 :                 break;
     412             :             }
     413         297 :             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
     414       13167 :         } else if (tag == 49) {
     415         891 :             av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
     416         891 :             if (data < 3) {
     417           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
     418           0 :                 ret = AVERROR(EINVAL);
     419           0 :                 break;
     420             :             }
     421         891 :             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
     422         891 :             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
     423       12276 :         } else if (tag == 50) {
     424         891 :             av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
     425         891 :             if (data < 3) {
     426           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
     427           0 :                 ret = AVERROR(EINVAL);
     428           0 :                 break;
     429             :             }
     430         891 :             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
     431       11385 :         } else if (tag == 71) {
     432           0 :             s->codebook = data;
     433           0 :             av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
     434       11385 :         } else if (tag == 72) {
     435         891 :             s->codebook = data;
     436         891 :             av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
     437       10494 :         } else if (tag == 70) {
     438          33 :             av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
     439          33 :             if (!(data == 10 || data == 12)) {
     440           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
     441           0 :                 ret = AVERROR(EINVAL);
     442           0 :                 break;
     443             :             }
     444          33 :             s->bpc = data;
     445       10461 :         } else if (tag == 84) {
     446          33 :             av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
     447          33 :             if (data == 1)
     448          22 :                 s->coded_format = AV_PIX_FMT_YUV422P10;
     449          11 :             else if (data == 3)
     450          11 :                 s->coded_format = AV_PIX_FMT_GBRP12;
     451           0 :             else if (data == 4)
     452           0 :                 s->coded_format = AV_PIX_FMT_GBRAP12;
     453             :             else {
     454           0 :                 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
     455           0 :                 ret = AVERROR_PATCHWELCOME;
     456           0 :                 break;
     457             :             }
     458          33 :             planes = av_pix_fmt_count_planes(s->coded_format);
     459       10428 :         } else if (tag == -85) {
     460          33 :             av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
     461          33 :             s->cropped_height = data;
     462             :         } else
     463       10395 :             av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
     464             : 
     465             :         /* Some kind of end of header tag */
     466       17193 :         if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
     467          33 :             s->coded_format != AV_PIX_FMT_NONE) {
     468          60 :             if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
     469          27 :                 s->a_format != s->coded_format) {
     470           6 :                 free_buffers(s);
     471           6 :                 if ((ret = alloc_buffers(avctx)) < 0) {
     472           0 :                     free_buffers(s);
     473           0 :                     return ret;
     474             :                 }
     475             :             }
     476          33 :             ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
     477          33 :             if (ret < 0)
     478           0 :                 return ret;
     479          33 :             if (s->cropped_height)
     480          33 :                 avctx->height = s->cropped_height;
     481          66 :             frame.f->width =
     482          66 :             frame.f->height = 0;
     483             : 
     484          33 :             if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
     485           0 :                 return ret;
     486             : 
     487          33 :             s->coded_width = 0;
     488          33 :             s->coded_height = 0;
     489          33 :             s->coded_format = AV_PIX_FMT_NONE;
     490          33 :             got_buffer = 1;
     491             :         }
     492       17160 :         coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
     493             : 
     494             :         /* Lowpass coefficients */
     495       17160 :         if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
     496          99 :             int lowpass_height = s->plane[s->channel_num].band[0][0].height;
     497          99 :             int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
     498          99 :             int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
     499          99 :             int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
     500             : 
     501          99 :             if (!got_buffer) {
     502           0 :                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
     503           0 :                 ret = AVERROR(EINVAL);
     504           0 :                 goto end;
     505             :             }
     506             : 
     507         198 :             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
     508          99 :                 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
     509           0 :                 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
     510           0 :                 ret = AVERROR(EINVAL);
     511           0 :                 goto end;
     512             :             }
     513             : 
     514          99 :             av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
     515        5082 :             for (i = 0; i < lowpass_height; i++) {
     516      344267 :                 for (j = 0; j < lowpass_width; j++)
     517      339284 :                     coeff_data[j] = bytestream2_get_be16u(&gb);
     518             : 
     519        4983 :                 coeff_data += lowpass_width;
     520             :             }
     521             : 
     522             :             /* Align to mod-4 position to continue reading tags */
     523          99 :             bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
     524             : 
     525             :             /* Copy last line of coefficients if odd height */
     526          99 :             if (lowpass_height & 1) {
     527          66 :                 memcpy(&coeff_data[lowpass_height * lowpass_width],
     528          33 :                        &coeff_data[(lowpass_height - 1) * lowpass_width],
     529             :                        lowpass_width * sizeof(*coeff_data));
     530             :             }
     531             : 
     532          99 :             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
     533             :         }
     534             : 
     535       17160 :         if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
     536         891 :             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
     537         891 :             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
     538         891 :             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
     539         891 :             int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
     540         891 :             int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
     541             :             int expected;
     542         891 :             int a_expected = highpass_a_height * highpass_a_width;
     543             :             int level, run, coeff;
     544         891 :             int count = 0, bytes;
     545             : 
     546         891 :             if (!got_buffer) {
     547           0 :                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
     548           0 :                 ret = AVERROR(EINVAL);
     549           0 :                 goto end;
     550             :             }
     551             : 
     552         891 :             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
     553           0 :                 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
     554           0 :                 ret = AVERROR(EINVAL);
     555           0 :                 goto end;
     556             :             }
     557         891 :             expected = highpass_height * highpass_stride;
     558             : 
     559         891 :             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);
     560             : 
     561         891 :             init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
     562             :             {
     563         891 :                 OPEN_READER(re, &s->gb);
     564         891 :                 if (!s->codebook) {
     565             :                     while (1) {
     566           0 :                         UPDATE_CACHE(re, &s->gb);
     567           0 :                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
     568             :                                    VLC_BITS, 3, 1);
     569             : 
     570             :                         /* escape */
     571           0 :                         if (level == 64)
     572           0 :                             break;
     573             : 
     574           0 :                         count += run;
     575             : 
     576           0 :                         if (count > expected)
     577           0 :                             break;
     578             : 
     579           0 :                         coeff = dequant_and_decompand(level, s->quantisation);
     580           0 :                         for (i = 0; i < run; i++)
     581           0 :                             *coeff_data++ = coeff;
     582             :                     }
     583             :                 } else {
     584             :                     while (1) {
     585     7046845 :                         UPDATE_CACHE(re, &s->gb);
     586     3523868 :                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
     587             :                                    VLC_BITS, 3, 1);
     588             : 
     589             :                         /* escape */
     590     3523868 :                         if (level == 255 && run == 2)
     591         891 :                             break;
     592             : 
     593     3522977 :                         count += run;
     594             : 
     595     3522977 :                         if (count > expected)
     596           0 :                             break;
     597             : 
     598     3522977 :                         coeff = dequant_and_decompand(level, s->quantisation);
     599    25184705 :                         for (i = 0; i < run; i++)
     600    21661728 :                             *coeff_data++ = coeff;
     601             :                     }
     602             :                 }
     603         891 :                 CLOSE_READER(re, &s->gb);
     604             :             }
     605             : 
     606         891 :             if (count > expected) {
     607           0 :                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
     608           0 :                 ret = AVERROR(EINVAL);
     609           0 :                 goto end;
     610             :             }
     611             : 
     612         891 :             bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
     613         891 :             if (bytes > bytestream2_get_bytes_left(&gb)) {
     614           0 :                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
     615           0 :                 ret = AVERROR(EINVAL);
     616           0 :                 goto end;
     617             :             } else
     618         891 :                 bytestream2_seek(&gb, bytes, SEEK_CUR);
     619             : 
     620         891 :             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
     621         891 :             s->codebook = 0;
     622             : 
     623             :             /* Copy last line of coefficients if odd height */
     624         891 :             if (highpass_height & 1) {
     625         198 :                 memcpy(&coeff_data[highpass_height * highpass_stride],
     626          99 :                        &coeff_data[(highpass_height - 1) * highpass_stride],
     627             :                        highpass_stride * sizeof(*coeff_data));
     628             :             }
     629             :         }
     630             :     }
     631             : 
     632          66 :     if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
     633          66 :         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
     634           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
     635           0 :         ret = AVERROR(EINVAL);
     636           0 :         goto end;
     637             :     }
     638             : 
     639          33 :     if (!got_buffer) {
     640           0 :         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
     641           0 :         ret = AVERROR(EINVAL);
     642           0 :         goto end;
     643             :     }
     644             : 
     645          33 :     planes = av_pix_fmt_count_planes(avctx->pix_fmt);
     646         231 :     for (plane = 0; plane < planes && !ret; plane++) {
     647             :         /* level 1 */
     648          99 :         int lowpass_height  = s->plane[plane].band[0][0].height;
     649          99 :         int lowpass_width   = s->plane[plane].band[0][0].width;
     650          99 :         int highpass_stride = s->plane[plane].band[0][1].stride;
     651          99 :         int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
     652             :         int16_t *low, *high, *output, *dst;
     653             : 
     654          99 :         if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
     655          99 :             !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
     656           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
     657           0 :             ret = AVERROR(EINVAL);
     658           0 :             goto end;
     659             :         }
     660             : 
     661          99 :         av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
     662             : 
     663          99 :         low    = s->plane[plane].subband[0];
     664          99 :         high   = s->plane[plane].subband[2];
     665          99 :         output = s->plane[plane].l_h[0];
     666        6413 :         for (i = 0; i < lowpass_width; i++) {
     667        6314 :             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
     668        6314 :             low++;
     669        6314 :             high++;
     670        6314 :             output++;
     671             :         }
     672             : 
     673          99 :         low    = s->plane[plane].subband[1];
     674          99 :         high   = s->plane[plane].subband[3];
     675          99 :         output = s->plane[plane].l_h[1];
     676             : 
     677        6413 :         for (i = 0; i < lowpass_width; i++) {
     678             :             // note the stride of "low" is highpass_stride
     679        6314 :             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
     680        6314 :             low++;
     681        6314 :             high++;
     682        6314 :             output++;
     683             :         }
     684             : 
     685          99 :         low    = s->plane[plane].l_h[0];
     686          99 :         high   = s->plane[plane].l_h[1];
     687          99 :         output = s->plane[plane].subband[0];
     688       10065 :         for (i = 0; i < lowpass_height * 2; i++) {
     689        9966 :             horiz_filter(output, low, high, lowpass_width);
     690        9966 :             low    += lowpass_width;
     691        9966 :             high   += lowpass_width;
     692        9966 :             output += lowpass_width * 2;
     693             :         }
     694          99 :         if (s->bpc == 12) {
     695          33 :             output = s->plane[plane].subband[0];
     696        3993 :             for (i = 0; i < lowpass_height * 2; i++) {
     697      716760 :                 for (j = 0; j < lowpass_width * 2; j++)
     698      712800 :                     output[j] *= 4;
     699             : 
     700        3960 :                 output += lowpass_width * 2;
     701             :             }
     702             :         }
     703             : 
     704             :         /* level 2 */
     705          99 :         lowpass_height  = s->plane[plane].band[1][1].height;
     706          99 :         lowpass_width   = s->plane[plane].band[1][1].width;
     707          99 :         highpass_stride = s->plane[plane].band[1][1].stride;
     708             : 
     709          99 :         if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
     710          99 :             !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
     711           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
     712           0 :             ret = AVERROR(EINVAL);
     713           0 :             goto end;
     714             :         }
     715             : 
     716          99 :         av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
     717             : 
     718          99 :         low    = s->plane[plane].subband[0];
     719          99 :         high   = s->plane[plane].subband[5];
     720          99 :         output = s->plane[plane].l_h[3];
     721       12727 :         for (i = 0; i < lowpass_width; i++) {
     722       12628 :             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
     723       12628 :             low++;
     724       12628 :             high++;
     725       12628 :             output++;
     726             :         }
     727             : 
     728          99 :         low    = s->plane[plane].subband[4];
     729          99 :         high   = s->plane[plane].subband[6];
     730          99 :         output = s->plane[plane].l_h[4];
     731       12727 :         for (i = 0; i < lowpass_width; i++) {
     732       12628 :             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
     733       12628 :             low++;
     734       12628 :             high++;
     735       12628 :             output++;
     736             :         }
     737             : 
     738          99 :         low    = s->plane[plane].l_h[3];
     739          99 :         high   = s->plane[plane].l_h[4];
     740          99 :         output = s->plane[plane].subband[0];
     741       20031 :         for (i = 0; i < lowpass_height * 2; i++) {
     742       19932 :             horiz_filter(output, low, high, lowpass_width);
     743       19932 :             low    += lowpass_width;
     744       19932 :             high   += lowpass_width;
     745       19932 :             output += lowpass_width * 2;
     746             :         }
     747             : 
     748          99 :         output = s->plane[plane].subband[0];
     749       20031 :         for (i = 0; i < lowpass_height * 2; i++) {
     750     5448476 :             for (j = 0; j < lowpass_width * 2; j++)
     751     5428544 :                 output[j] *= 4;
     752             : 
     753       19932 :             output += lowpass_width * 2;
     754             :         }
     755             : 
     756             :         /* level 3 */
     757          99 :         lowpass_height  = s->plane[plane].band[2][1].height;
     758          99 :         lowpass_width   = s->plane[plane].band[2][1].width;
     759          99 :         highpass_stride = s->plane[plane].band[2][1].stride;
     760             : 
     761          99 :         if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
     762          99 :             !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
     763           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
     764           0 :             ret = AVERROR(EINVAL);
     765           0 :             goto end;
     766             :         }
     767             : 
     768          99 :         av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
     769             : 
     770          99 :         low    = s->plane[plane].subband[0];
     771          99 :         high   = s->plane[plane].subband[8];
     772          99 :         output = s->plane[plane].l_h[6];
     773       25355 :         for (i = 0; i < lowpass_width; i++) {
     774       25256 :             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
     775       25256 :             low++;
     776       25256 :             high++;
     777       25256 :             output++;
     778             :         }
     779             : 
     780          99 :         low    = s->plane[plane].subband[7];
     781          99 :         high   = s->plane[plane].subband[9];
     782          99 :         output = s->plane[plane].l_h[7];
     783       25355 :         for (i = 0; i < lowpass_width; i++) {
     784       25256 :             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
     785       25256 :             low++;
     786       25256 :             high++;
     787       25256 :             output++;
     788             :         }
     789             : 
     790          99 :         dst = (int16_t *)pic->data[act_plane];
     791          99 :         low  = s->plane[plane].l_h[6];
     792          99 :         high = s->plane[plane].l_h[7];
     793       39963 :         for (i = 0; i < lowpass_height * 2; i++) {
     794       39864 :             horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
     795       39864 :             low  += lowpass_width;
     796       39864 :             high += lowpass_width;
     797       39864 :             dst  += pic->linesize[act_plane] / 2;
     798             :         }
     799             :     }
     800             : 
     801             : 
     802          33 : end:
     803          33 :     if (ret < 0)
     804           0 :         return ret;
     805             : 
     806          33 :     *got_frame = 1;
     807          33 :     return avpkt->size;
     808             : }
     809             : 
     810           6 : static av_cold int cfhd_close(AVCodecContext *avctx)
     811             : {
     812           6 :     CFHDContext *s = avctx->priv_data;
     813             : 
     814           6 :     free_buffers(s);
     815             : 
     816           6 :     if (!avctx->internal->is_copy) {
     817           6 :         ff_free_vlc(&s->vlc_9);
     818           6 :         ff_free_vlc(&s->vlc_18);
     819             :     }
     820             : 
     821           6 :     return 0;
     822             : }
     823             : 
     824             : AVCodec ff_cfhd_decoder = {
     825             :     .name             = "cfhd",
     826             :     .long_name        = NULL_IF_CONFIG_SMALL("Cineform HD"),
     827             :     .type             = AVMEDIA_TYPE_VIDEO,
     828             :     .id               = AV_CODEC_ID_CFHD,
     829             :     .priv_data_size   = sizeof(CFHDContext),
     830             :     .init             = cfhd_init,
     831             :     .close            = cfhd_close,
     832             :     .decode           = cfhd_decode,
     833             :     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
     834             :     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     835             : };

Generated by: LCOV version 1.13