FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cfhd.c
Go to the documentation of this file.
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 {
43  ImageWidth = 20,
50  ChannelWidth = 104,
53 };
54 
55 static av_cold int cfhd_init(AVCodecContext *avctx)
56 {
57  CFHDContext *s = avctx->priv_data;
58 
59  avctx->bits_per_raw_sample = 10;
60  s->avctx = avctx;
61 
62  return ff_cfhd_init_vlcs(s);
63 }
64 
66 {
67  s->subband_num = 0;
68  s->level = 0;
69  s->subband_num_actual = 0;
70 }
71 
73 {
74  s->coded_width = 0;
75  s->coded_height = 0;
76  s->cropped_height = 0;
77  s->bpc = 10;
78  s->channel_cnt = 4;
80  s->channel_num = 0;
81  s->lowpass_precision = 16;
82  s->quantisation = 1;
83  s->wavelet_depth = 3;
84  s->pshift = 1;
85  s->codebook = 0;
87 }
88 
89 /* TODO: merge with VLC tables or use LUT */
90 static inline int dequant_and_decompand(int level, int quantisation)
91 {
92  int64_t abslevel = abs(level);
93  return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
94  FFSIGN(level) * quantisation;
95 }
96 
97 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  for (i = 0; i < len; i++) {
106  if (i == 0) {
107  tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
108  output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
109  if (clip)
110  output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
111 
112  tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
113  output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
114  if (clip)
115  output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
116  } else if (i == len-1) {
117  tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
118  output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
119  if (clip)
120  output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
121 
122  tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
123  output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
124  if (clip)
125  output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
126  } else {
127  tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
128  output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
129  if (clip)
130  output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
131 
132  tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
133  output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
134  if (clip)
135  output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
136  }
137  }
138 }
139 
140 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
141  int width)
142 {
143  filter(output, 1, low, 1, high, 1, width, 0);
144 }
145 
146 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
147  int width, int clip)
148 {
149  filter(output, 1, low, 1, high, 1, width, clip);
150 }
151 
152 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  filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
157 }
158 
160 {
161  int i, j;
162 
163  for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
164  av_freep(&s->plane[i].idwt_buf);
165  av_freep(&s->plane[i].idwt_tmp);
166 
167  for (j = 0; j < 9; j++)
168  s->plane[i].subband[j] = NULL;
169 
170  for (j = 0; j < 8; j++)
171  s->plane[i].l_h[j] = NULL;
172  }
173  s->a_height = 0;
174  s->a_width = 0;
175 }
176 
177 static int alloc_buffers(AVCodecContext *avctx)
178 {
179  CFHDContext *s = avctx->priv_data;
180  int i, j, ret, planes;
181  int chroma_x_shift, chroma_y_shift;
182  unsigned k;
183 
184  if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
185  return ret;
186  avctx->pix_fmt = s->coded_format;
187 
189  &chroma_x_shift,
190  &chroma_y_shift)) < 0)
191  return ret;
193 
194  for (i = 0; i < planes; i++) {
195  int w8, h8, w4, h4, w2, h2;
196  int width = i ? avctx->width >> chroma_x_shift : avctx->width;
197  int height = i ? avctx->height >> chroma_y_shift : avctx->height;
198  ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
199  height = FFALIGN(height / 8, 2) * 8;
200  s->plane[i].width = width;
201  s->plane[i].height = height;
202  s->plane[i].stride = stride;
203 
204  w8 = FFALIGN(s->plane[i].width / 8, 8);
205  h8 = FFALIGN(s->plane[i].height / 8, 2);
206  w4 = w8 * 2;
207  h4 = h8 * 2;
208  w2 = w4 * 2;
209  h2 = h4 * 2;
210 
211  s->plane[i].idwt_buf =
212  av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
213  s->plane[i].idwt_tmp =
214  av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
215  if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
216  return AVERROR(ENOMEM);
217 
218  s->plane[i].subband[0] = s->plane[i].idwt_buf;
219  s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
220  s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
221  s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
222  s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
223  s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
224  s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
225  s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
226  s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
227  s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
228 
229  for (j = 0; j < DWT_LEVELS; j++) {
230  for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
231  s->plane[i].band[j][k].a_width = w8 << j;
232  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  s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
238  s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
239  // s->plane[i].l_h[2] = ll2;
240  s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
241  s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
242  // s->plane[i].l_h[5] = ll1;
243  s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
244  s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
245  }
246 
247  s->a_height = s->coded_height;
248  s->a_width = s->coded_width;
249  s->a_format = s->coded_format;
250 
251  return 0;
252 }
253 
254 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
255  AVPacket *avpkt)
256 {
257  CFHDContext *s = avctx->priv_data;
258  GetByteContext gb;
259  ThreadFrame frame = { .f = data };
260  AVFrame *pic = data;
261  int ret = 0, i, j, planes, plane, got_buffer = 0;
262  int16_t *coeff_data;
263 
267 
268  bytestream2_init(&gb, avpkt->data, avpkt->size);
269 
270  while (bytestream2_get_bytes_left(&gb) > 4) {
271  /* Bit weird but implement the tag parsing as the spec says */
272  uint16_t tagu = bytestream2_get_be16(&gb);
273  int16_t tag = (int16_t)tagu;
274  int8_t tag8 = (int8_t)(tagu >> 8);
275  uint16_t abstag = abs(tag);
276  int8_t abs_tag8 = abs(tag8);
277  uint16_t data = bytestream2_get_be16(&gb);
278  if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
279  av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
280  } else if (tag == ImageWidth) {
281  av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
282  s->coded_width = data;
283  } else if (tag == ImageHeight) {
284  av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
285  s->coded_height = data;
286  } else if (tag == 101) {
287  av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
288  if (data < 1 || data > 31) {
289  av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
290  ret = AVERROR(EINVAL);
291  break;
292  }
293  s->bpc = data;
294  } else if (tag == ChannelCount) {
295  av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
296  s->channel_cnt = data;
297  if (data > 4) {
298  av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
299  ret = AVERROR_PATCHWELCOME;
300  break;
301  }
302  } else if (tag == SubbandCount) {
303  av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
304  if (data != SUBBAND_COUNT) {
305  av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
306  ret = AVERROR_PATCHWELCOME;
307  break;
308  }
309  } else if (tag == ChannelNumber) {
310  s->channel_num = data;
311  av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
312  if (s->channel_num >= planes) {
313  av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
314  ret = AVERROR(EINVAL);
315  break;
316  }
318  } else if (tag == SubbandNumber) {
319  if (s->subband_num != 0 && data == 1) // hack
320  s->level++;
321  av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
322  s->subband_num = data;
323  if (s->level >= DWT_LEVELS) {
324  av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
325  ret = AVERROR(EINVAL);
326  break;
327  }
328  if (s->subband_num > 3) {
329  av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
330  ret = AVERROR(EINVAL);
331  break;
332  }
333  } else if (tag == 51) {
334  av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
336  if (s->subband_num_actual >= 10) {
337  av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
338  ret = AVERROR(EINVAL);
339  break;
340  }
341  } else if (tag == LowpassPrecision)
342  av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
343  else if (tag == Quantization) {
344  s->quantisation = data;
345  av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
346  } else if (tag == PrescaleShift) {
347  s->prescale_shift[0] = (data >> 0) & 0x7;
348  s->prescale_shift[1] = (data >> 3) & 0x7;
349  s->prescale_shift[2] = (data >> 6) & 0x7;
350  av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
351  } else if (tag == 27) {
352  av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
353  if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
354  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
355  ret = AVERROR(EINVAL);
356  break;
357  }
358  s->plane[s->channel_num].band[0][0].width = data;
359  s->plane[s->channel_num].band[0][0].stride = data;
360  } else if (tag == 28) {
361  av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
362  if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
363  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
364  ret = AVERROR(EINVAL);
365  break;
366  }
367  s->plane[s->channel_num].band[0][0].height = data;
368  } else if (tag == 1)
369  av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
370  else if (tag == 10) {
371  if (data != 0) {
372  avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
373  ret = AVERROR_PATCHWELCOME;
374  break;
375  }
376  av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
377  } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
378  av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
379  bytestream2_skipu(&gb, data * 4);
380  } else if (tag == 23) {
381  av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
382  avpriv_report_missing_feature(avctx, "Skip frame");
383  ret = AVERROR_PATCHWELCOME;
384  break;
385  } else if (tag == 2) {
386  av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
387  if (data > bytestream2_get_bytes_left(&gb) / 4) {
388  av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
389  ret = AVERROR_INVALIDDATA;
390  break;
391  }
392  for (i = 0; i < data; i++) {
393  uint16_t tag2 = bytestream2_get_be16(&gb);
394  uint16_t val2 = bytestream2_get_be16(&gb);
395  av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
396  }
397  } else if (tag == 41) {
398  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  if (data < 3) {
400  av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
401  ret = AVERROR(EINVAL);
402  break;
403  }
404  s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
405  s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
406  } else if (tag == 42) {
407  av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
408  if (data < 3) {
409  av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
410  ret = AVERROR(EINVAL);
411  break;
412  }
413  s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
414  } else if (tag == 49) {
415  av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
416  if (data < 3) {
417  av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
418  ret = AVERROR(EINVAL);
419  break;
420  }
421  s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
422  s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
423  } else if (tag == 50) {
424  av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
425  if (data < 3) {
426  av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
427  ret = AVERROR(EINVAL);
428  break;
429  }
430  s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
431  } else if (tag == 71) {
432  s->codebook = data;
433  av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
434  } else if (tag == 72) {
435  s->codebook = data;
436  av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
437  } else if (tag == 70) {
438  av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
439  if (!(data == 10 || data == 12)) {
440  av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
441  ret = AVERROR(EINVAL);
442  break;
443  }
444  s->bpc = data;
445  } else if (tag == 84) {
446  av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
447  if (data == 1)
449  else if (data == 3)
451  else if (data == 4)
453  else {
454  avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
455  ret = AVERROR_PATCHWELCOME;
456  break;
457  }
459  } else if (tag == -85) {
460  av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
461  s->cropped_height = data;
462  } else
463  av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
464 
465  /* Some kind of end of header tag */
466  if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
468  if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
469  s->a_format != s->coded_format) {
470  free_buffers(s);
471  if ((ret = alloc_buffers(avctx)) < 0) {
472  free_buffers(s);
473  return ret;
474  }
475  }
476  ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
477  if (ret < 0)
478  return ret;
479  if (s->cropped_height)
480  avctx->height = s->cropped_height;
481  frame.f->width =
482  frame.f->height = 0;
483 
484  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
485  return ret;
486 
487  s->coded_width = 0;
488  s->coded_height = 0;
490  got_buffer = 1;
491  }
492  coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
493 
494  /* Lowpass coefficients */
495  if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
496  int lowpass_height = s->plane[s->channel_num].band[0][0].height;
497  int lowpass_width = s->plane[s->channel_num].band[0][0].width;
498  int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
499  int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
500 
501  if (!got_buffer) {
502  av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
503  ret = AVERROR(EINVAL);
504  goto end;
505  }
506 
507  if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
508  lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
509  av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
510  ret = AVERROR(EINVAL);
511  goto end;
512  }
513 
514  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  for (i = 0; i < lowpass_height; i++) {
516  for (j = 0; j < lowpass_width; j++)
517  coeff_data[j] = bytestream2_get_be16u(&gb);
518 
519  coeff_data += lowpass_width;
520  }
521 
522  /* Align to mod-4 position to continue reading tags */
523  bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
524 
525  /* Copy last line of coefficients if odd height */
526  if (lowpass_height & 1) {
527  memcpy(&coeff_data[lowpass_height * lowpass_width],
528  &coeff_data[(lowpass_height - 1) * lowpass_width],
529  lowpass_width * sizeof(*coeff_data));
530  }
531 
532  av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
533  }
534 
535  if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
536  int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
537  int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
538  int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
539  int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
540  int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
541  int expected;
542  int a_expected = highpass_a_height * highpass_a_width;
543  int level, run, coeff;
544  int count = 0, bytes;
545 
546  if (!got_buffer) {
547  av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
548  ret = AVERROR(EINVAL);
549  goto end;
550  }
551 
552  if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
553  av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
554  ret = AVERROR(EINVAL);
555  goto end;
556  }
557  expected = highpass_height * highpass_stride;
558 
559  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 
562  {
563  OPEN_READER(re, &s->gb);
564  if (!s->codebook) {
565  while (1) {
566  UPDATE_CACHE(re, &s->gb);
567  GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
568  VLC_BITS, 3, 1);
569 
570  /* escape */
571  if (level == 64)
572  break;
573 
574  count += run;
575 
576  if (count > expected)
577  break;
578 
579  coeff = dequant_and_decompand(level, s->quantisation);
580  for (i = 0; i < run; i++)
581  *coeff_data++ = coeff;
582  }
583  } else {
584  while (1) {
585  UPDATE_CACHE(re, &s->gb);
586  GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
587  VLC_BITS, 3, 1);
588 
589  /* escape */
590  if (level == 255 && run == 2)
591  break;
592 
593  count += run;
594 
595  if (count > expected)
596  break;
597 
598  coeff = dequant_and_decompand(level, s->quantisation);
599  for (i = 0; i < run; i++)
600  *coeff_data++ = coeff;
601  }
602  }
603  CLOSE_READER(re, &s->gb);
604  }
605 
606  if (count > expected) {
607  av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
608  ret = AVERROR(EINVAL);
609  goto end;
610  }
611 
612  bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
613  if (bytes > bytestream2_get_bytes_left(&gb)) {
614  av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
615  ret = AVERROR(EINVAL);
616  goto end;
617  } else
618  bytestream2_seek(&gb, bytes, SEEK_CUR);
619 
620  av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
621  s->codebook = 0;
622 
623  /* Copy last line of coefficients if odd height */
624  if (highpass_height & 1) {
625  memcpy(&coeff_data[highpass_height * highpass_stride],
626  &coeff_data[(highpass_height - 1) * highpass_stride],
627  highpass_stride * sizeof(*coeff_data));
628  }
629  }
630  }
631 
632  if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
634  av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
635  ret = AVERROR(EINVAL);
636  goto end;
637  }
638 
639  if (!got_buffer) {
640  av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
641  ret = AVERROR(EINVAL);
642  goto end;
643  }
644 
645  planes = av_pix_fmt_count_planes(avctx->pix_fmt);
646  for (plane = 0; plane < planes && !ret; plane++) {
647  /* level 1 */
648  int lowpass_height = s->plane[plane].band[0][0].height;
649  int lowpass_width = s->plane[plane].band[0][0].width;
650  int highpass_stride = s->plane[plane].band[0][1].stride;
651  int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
652  int16_t *low, *high, *output, *dst;
653 
654  if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
655  !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
656  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
657  ret = AVERROR(EINVAL);
658  goto end;
659  }
660 
661  av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
662 
663  low = s->plane[plane].subband[0];
664  high = s->plane[plane].subband[2];
665  output = s->plane[plane].l_h[0];
666  for (i = 0; i < lowpass_width; i++) {
667  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
668  low++;
669  high++;
670  output++;
671  }
672 
673  low = s->plane[plane].subband[1];
674  high = s->plane[plane].subband[3];
675  output = s->plane[plane].l_h[1];
676 
677  for (i = 0; i < lowpass_width; i++) {
678  // note the stride of "low" is highpass_stride
679  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
680  low++;
681  high++;
682  output++;
683  }
684 
685  low = s->plane[plane].l_h[0];
686  high = s->plane[plane].l_h[1];
687  output = s->plane[plane].subband[0];
688  for (i = 0; i < lowpass_height * 2; i++) {
689  horiz_filter(output, low, high, lowpass_width);
690  low += lowpass_width;
691  high += lowpass_width;
692  output += lowpass_width * 2;
693  }
694  if (s->bpc == 12) {
695  output = s->plane[plane].subband[0];
696  for (i = 0; i < lowpass_height * 2; i++) {
697  for (j = 0; j < lowpass_width * 2; j++)
698  output[j] *= 4;
699 
700  output += lowpass_width * 2;
701  }
702  }
703 
704  /* level 2 */
705  lowpass_height = s->plane[plane].band[1][1].height;
706  lowpass_width = s->plane[plane].band[1][1].width;
707  highpass_stride = s->plane[plane].band[1][1].stride;
708 
709  if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
710  !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
711  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
712  ret = AVERROR(EINVAL);
713  goto end;
714  }
715 
716  av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
717 
718  low = s->plane[plane].subband[0];
719  high = s->plane[plane].subband[5];
720  output = s->plane[plane].l_h[3];
721  for (i = 0; i < lowpass_width; i++) {
722  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
723  low++;
724  high++;
725  output++;
726  }
727 
728  low = s->plane[plane].subband[4];
729  high = s->plane[plane].subband[6];
730  output = s->plane[plane].l_h[4];
731  for (i = 0; i < lowpass_width; i++) {
732  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
733  low++;
734  high++;
735  output++;
736  }
737 
738  low = s->plane[plane].l_h[3];
739  high = s->plane[plane].l_h[4];
740  output = s->plane[plane].subband[0];
741  for (i = 0; i < lowpass_height * 2; i++) {
742  horiz_filter(output, low, high, lowpass_width);
743  low += lowpass_width;
744  high += lowpass_width;
745  output += lowpass_width * 2;
746  }
747 
748  output = s->plane[plane].subband[0];
749  for (i = 0; i < lowpass_height * 2; i++) {
750  for (j = 0; j < lowpass_width * 2; j++)
751  output[j] *= 4;
752 
753  output += lowpass_width * 2;
754  }
755 
756  /* level 3 */
757  lowpass_height = s->plane[plane].band[2][1].height;
758  lowpass_width = s->plane[plane].band[2][1].width;
759  highpass_stride = s->plane[plane].band[2][1].stride;
760 
761  if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
762  !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
763  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
764  ret = AVERROR(EINVAL);
765  goto end;
766  }
767 
768  av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
769 
770  low = s->plane[plane].subband[0];
771  high = s->plane[plane].subband[8];
772  output = s->plane[plane].l_h[6];
773  for (i = 0; i < lowpass_width; i++) {
774  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
775  low++;
776  high++;
777  output++;
778  }
779 
780  low = s->plane[plane].subband[7];
781  high = s->plane[plane].subband[9];
782  output = s->plane[plane].l_h[7];
783  for (i = 0; i < lowpass_width; i++) {
784  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
785  low++;
786  high++;
787  output++;
788  }
789 
790  dst = (int16_t *)pic->data[act_plane];
791  low = s->plane[plane].l_h[6];
792  high = s->plane[plane].l_h[7];
793  for (i = 0; i < lowpass_height * 2; i++) {
794  horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
795  low += lowpass_width;
796  high += lowpass_width;
797  dst += pic->linesize[act_plane] / 2;
798  }
799  }
800 
801 
802 end:
803  if (ret < 0)
804  return ret;
805 
806  *got_frame = 1;
807  return avpkt->size;
808 }
809 
811 {
812  CFHDContext *s = avctx->priv_data;
813 
814  free_buffers(s);
815 
816  if (!avctx->internal->is_copy) {
817  ff_free_vlc(&s->vlc_9);
818  ff_free_vlc(&s->vlc_18);
819  }
820 
821  return 0;
822 }
823 
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,
835 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
int plane
Definition: avisynth_c.h:422
int channel_cnt
Definition: cfhd.h:92
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cfhd.c:254
VLC vlc_18
Definition: cfhd.h:78
CFHDParam
Definition: cfhd.c:40
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVCodecContext * avctx
Definition: cfhd.h:72
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
float re
Definition: fft.c:82
misc image utilities
AVFrame * f
Definition: thread.h:36
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2403
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:196
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int a_height
Definition: cfhd.h:49
static int dequant_and_decompand(int level, int quantisation)
Definition: cfhd.c:90
int size
Definition: avcodec.h:1401
#define VLC_BITS
Definition: asvdec.c:37
int cropped_height
Definition: cfhd.h:84
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1697
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int16_t * idwt_tmp
Definition: cfhd.h:62
int a_width
Definition: cfhd.h:87
ptrdiff_t stride
Definition: cfhd.h:46
uint8_t run
Definition: svq3.c:206
int subband_num_actual
Definition: cfhd.h:103
static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high, int width, int clip)
Definition: cfhd.c:146
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2711
AVCodec.
Definition: avcodec.h:3351
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
Macro definitions for various function/variable attributes.
int width
Definition: cfhd.h:48
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:97
static void horiz_filter(int16_t *output, int16_t *low, int16_t *high, int width)
Definition: cfhd.c:140
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
#define av_cold
Definition: attributes.h:82
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
Multithreading support functions.
int16_t * idwt_buf
Definition: cfhd.h:61
int a_format
Definition: cfhd.h:89
static AVFrame * frame
static int alloc_buffers(AVCodecContext *avctx)
Definition: cfhd.c:177
#define height
uint8_t * data
Definition: avcodec.h:1400
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
const uint8_t * buffer
Definition: bytestream.h:34
uint32_t tag
Definition: movenc.c:1448
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
bitstream reader API header.
#define FFALIGN(x, a)
Definition: macros.h:48
#define SUBBAND_COUNT
Definition: cfhd.h:33
#define av_log(a,...)
CFHD_RL_VLC_ELEM table_18_rl_vlc[4572]
Definition: cfhd.h:77
CFHD_RL_VLC_ELEM table_9_rl_vlc[2088]
Definition: cfhd.h:74
uint8_t prescale_shift[3]
Definition: cfhd.h:105
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
int width
Definition: frame.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:134
#define AVERROR(e)
Definition: error.h:43
VLC vlc_9
Definition: cfhd.h:75
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2391
#define FF_CEIL_RSHIFT
Definition: common.h:61
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int16_t * l_h[8]
Definition: cfhd.h:66
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
uint16_t width
Definition: gdv.c:47
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:389
#define DWT_LEVELS
Definition: cfhd.h:41
const char * name
Name of the codec implementation.
Definition: avcodec.h:3358
GLsizei count
Definition: opengl_enc.c:109
#define CLOSE_READER(name, gb)
Definition: get_bits.h:131
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1012
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
Definition: get_bits.h:509
int a_width
Definition: cfhd.h:47
static av_cold int cfhd_close(AVCodecContext *avctx)
Definition: cfhd.c:810
static void init_frame_defaults(CFHDContext *s)
Definition: cfhd.c:72
SubBand band[DWT_LEVELS][4]
Definition: cfhd.h:68
int subband_cnt
Definition: cfhd.h:93
#define FFSIGN(a)
Definition: common.h:73
int width
picture width / height.
Definition: avcodec.h:1660
static void init_plane_defaults(CFHDContext *s)
Definition: cfhd.c:65
uint16_t quantisation
Definition: cfhd.h:96
static av_cold int cfhd_init(AVCodecContext *avctx)
Definition: cfhd.c:55
#define FF_ARRAY_ELEMS(a)
static void vert_filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len)
Definition: cfhd.c:152
int channel_num
Definition: cfhd.h:94
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
GetBitContext gb
Definition: cfhd.h:80
int wavelet_depth
Definition: cfhd.h:97
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1488
int codebook
Definition: cfhd.h:100
#define OPEN_READER(name, gb)
Definition: get_bits.h:120
int subband_num
Definition: cfhd.h:101
int pshift
Definition: cfhd.h:98
enum AVPixelFormat coded_format
Definition: cfhd.h:85
AVCodec ff_cfhd_decoder
Definition: cfhd.c:824
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
refcounted data buffer API
int level
Definition: cfhd.h:102
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:385
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
uint8_t level
Definition: svq3.c:207
int coded_width
Definition: cfhd.h:82
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal api header.
common internal and external API header
static double clip(void *opaque, double val)
Clip value val in the minval - maxval range.
Definition: vf_lut.c:158
ptrdiff_t stride
Definition: cfhd.h:59
int bpc
Definition: cfhd.h:91
void * priv_data
Definition: avcodec.h:1515
int len
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1523
int a_height
Definition: cfhd.h:88
int height
Definition: cfhd.h:50
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
int width
Definition: cfhd.h:57
static void free_buffers(CFHDContext *s)
Definition: cfhd.c:159
int height
Definition: frame.h:259
int ff_cfhd_init_vlcs(CFHDContext *s)
Definition: cfhddata.c:276
#define av_freep(p)
#define av_malloc_array(a, b)
#define stride
Plane plane[4]
Definition: cfhd.h:106
int height
Definition: cfhd.h:58
uint8_t lowpass_precision
Definition: cfhd.h:95
int16_t * subband[SUBBAND_COUNT]
Definition: cfhd.h:65
This structure stores compressed data.
Definition: avcodec.h:1377
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
for(j=16;j >0;--j)
int coded_height
Definition: cfhd.h:83
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
Definition: common.h:229
static uint8_t tmp[11]
Definition: aes_ctr.c:26