LCOV - code coverage report
Current view: top level - libavcodec - cfhd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 431 562 76.7 %
Date: 2018-05-20 11:54:08 Functions: 12 13 92.3 %

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

Generated by: LCOV version 1.13