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