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  * 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  s->bpc = data;
262  } else if (tag == 12) {
263  av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
264  s->channel_cnt = data;
265  if (data > 4) {
266  av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
267  ret = AVERROR_PATCHWELCOME;
268  break;
269  }
270  } else if (tag == 14) {
271  av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
272  if (data != SUBBAND_COUNT) {
273  av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
274  ret = AVERROR_PATCHWELCOME;
275  break;
276  }
277  } else if (tag == 62) {
278  s->channel_num = data;
279  av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
280  if (s->channel_num >= planes) {
281  av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
282  ret = AVERROR(EINVAL);
283  break;
284  }
286  } else if (tag == 48) {
287  if (s->subband_num != 0 && data == 1) // hack
288  s->level++;
289  av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
290  s->subband_num = data;
291  if (s->level >= DWT_LEVELS) {
292  av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
293  ret = AVERROR(EINVAL);
294  break;
295  }
296  if (s->subband_num > 3) {
297  av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
298  ret = AVERROR(EINVAL);
299  break;
300  }
301  } else if (tag == 51) {
302  av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
304  if (s->subband_num_actual >= 10) {
305  av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
306  ret = AVERROR(EINVAL);
307  break;
308  }
309  } else if (tag == 35)
310  av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
311  else if (tag == 53) {
312  s->quantisation = data;
313  av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
314  } else if (tag == 109) {
315  s->prescale_shift[0] = (data >> 0) & 0x7;
316  s->prescale_shift[1] = (data >> 3) & 0x7;
317  s->prescale_shift[2] = (data >> 6) & 0x7;
318  av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
319  } else if (tag == 27) {
320  s->plane[s->channel_num].band[0][0].width = data;
321  s->plane[s->channel_num].band[0][0].stride = data;
322  av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
323  if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
324  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
325  ret = AVERROR(EINVAL);
326  break;
327  }
328  } else if (tag == 28) {
329  s->plane[s->channel_num].band[0][0].height = data;
330  av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
331  if (data < 3 || data > s->plane[s->channel_num].band[0][0].height) {
332  av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
333  ret = AVERROR(EINVAL);
334  break;
335  }
336  } else if (tag == 1)
337  av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
338  else if (tag == 10) {
339  if (data != 0) {
340  avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
341  ret = AVERROR_PATCHWELCOME;
342  break;
343  }
344  av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
345  } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
346  av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
347  bytestream2_skipu(&gb, data * 4);
348  } else if (tag == 23) {
349  av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
350  avpriv_report_missing_feature(avctx, "Skip frame");
351  ret = AVERROR_PATCHWELCOME;
352  break;
353  } else if (tag == 2) {
354  av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
355  if (data > bytestream2_get_bytes_left(&gb) / 4) {
356  av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
357  ret = AVERROR_INVALIDDATA;
358  break;
359  }
360  for (i = 0; i < data; i++) {
361  uint16_t tag2 = bytestream2_get_be16(&gb);
362  uint16_t val2 = bytestream2_get_be16(&gb);
363  av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
364  }
365  } else if (tag == 41) {
366  s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
367  s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
368  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);
369  if (data < 3) {
370  av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
371  ret = AVERROR(EINVAL);
372  break;
373  }
374  } else if (tag == 42) {
375  s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
376  av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
377  if (data < 3) {
378  av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
379  ret = AVERROR(EINVAL);
380  break;
381  }
382  } else if (tag == 49) {
383  s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
384  s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
385  av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
386  if (data < 3) {
387  av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
388  ret = AVERROR(EINVAL);
389  break;
390  }
391  } else if (tag == 50) {
392  s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
393  av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
394  if (data < 3) {
395  av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
396  ret = AVERROR(EINVAL);
397  break;
398  }
399  } else if (tag == 71) {
400  s->codebook = data;
401  av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
402  } else if (tag == 72) {
403  s->codebook = data;
404  av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
405  } else if (tag == 70) {
406  av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
407  s->bpc = data;
408  if (!(s->bpc == 10 || s->bpc == 12)) {
409  av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
410  ret = AVERROR(EINVAL);
411  break;
412  }
413  } else if (tag == 84) {
414  av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
415  if (data == 1)
417  else if (data == 3)
419  else if (data == 4)
421  else {
422  avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
423  ret = AVERROR_PATCHWELCOME;
424  break;
425  }
427  } else
428  av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
429 
430  /* Some kind of end of header tag */
431  if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
433  if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
434  s->a_format != s->coded_format) {
435  free_buffers(avctx);
436  if ((ret = alloc_buffers(avctx)) < 0) {
437  free_buffers(avctx);
438  return ret;
439  }
440  }
441  ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
442  if (ret < 0)
443  return ret;
444  frame.f->width =
445  frame.f->height = 0;
446 
447  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
448  return ret;
449 
450  s->coded_width = 0;
451  s->coded_height = 0;
453  got_buffer = 1;
454  }
455  coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
456 
457  /* Lowpass coefficients */
458  if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
459  int lowpass_height = s->plane[s->channel_num].band[0][0].height;
460  int lowpass_width = s->plane[s->channel_num].band[0][0].width;
461  int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
462  int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
463 
464  if (!got_buffer) {
465  av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
466  ret = AVERROR(EINVAL);
467  goto end;
468  }
469 
470  if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
471  lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
472  av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
473  ret = AVERROR(EINVAL);
474  goto end;
475  }
476 
477  av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
478  for (i = 0; i < lowpass_height; i++) {
479  for (j = 0; j < lowpass_width; j++)
480  coeff_data[j] = bytestream2_get_be16u(&gb);
481 
482  coeff_data += lowpass_width;
483  }
484 
485  /* Align to mod-4 position to continue reading tags */
486  bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
487 
488  /* Copy last line of coefficients if odd height */
489  if (lowpass_height & 1) {
490  memcpy(&coeff_data[lowpass_height * lowpass_width],
491  &coeff_data[(lowpass_height - 1) * lowpass_width],
492  lowpass_width * sizeof(*coeff_data));
493  }
494 
495  av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
496  }
497 
498  if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
499  int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
500  int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
501  int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
502  int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
503  int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
504  int expected = highpass_height * highpass_stride;
505  int a_expected = highpass_a_height * highpass_a_width;
506  int level, run, coeff;
507  int count = 0, bytes;
508 
509  if (!got_buffer) {
510  av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
511  ret = AVERROR(EINVAL);
512  goto end;
513  }
514 
515  if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
516  av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
517  ret = AVERROR(EINVAL);
518  goto end;
519  }
520 
521  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);
522 
524  {
525  OPEN_READER(re, &s->gb);
526  if (!s->codebook) {
527  while (1) {
528  UPDATE_CACHE(re, &s->gb);
529  GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
530  VLC_BITS, 3, 1);
531 
532  /* escape */
533  if (level == 64)
534  break;
535 
536  count += run;
537 
538  if (count > expected)
539  break;
540 
541  coeff = dequant_and_decompand(level, s->quantisation);
542  for (i = 0; i < run; i++)
543  *coeff_data++ = coeff;
544  }
545  } else {
546  while (1) {
547  UPDATE_CACHE(re, &s->gb);
548  GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
549  VLC_BITS, 3, 1);
550 
551  /* escape */
552  if (level == 255 && run == 2)
553  break;
554 
555  count += run;
556 
557  if (count > expected)
558  break;
559 
560  coeff = dequant_and_decompand(level, s->quantisation);
561  for (i = 0; i < run; i++)
562  *coeff_data++ = coeff;
563  }
564  }
565  CLOSE_READER(re, &s->gb);
566  }
567 
568  if (count > expected) {
569  av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
570  ret = AVERROR(EINVAL);
571  goto end;
572  }
573 
574  bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
575  if (bytes > bytestream2_get_bytes_left(&gb)) {
576  av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
577  ret = AVERROR(EINVAL);
578  goto end;
579  } else
580  bytestream2_seek(&gb, bytes, SEEK_CUR);
581 
582  av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
583  s->codebook = 0;
584 
585  /* Copy last line of coefficients if odd height */
586  if (highpass_height & 1) {
587  memcpy(&coeff_data[highpass_height * highpass_stride],
588  &coeff_data[(highpass_height - 1) * highpass_stride],
589  highpass_stride * sizeof(*coeff_data));
590  }
591  }
592  }
593 
594  if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
596  av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
597  ret = AVERROR(EINVAL);
598  goto end;
599  }
600 
601  if (!got_buffer) {
602  av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
603  ret = AVERROR(EINVAL);
604  goto end;
605  }
606 
607  planes = av_pix_fmt_count_planes(avctx->pix_fmt);
608  for (plane = 0; plane < planes && !ret; plane++) {
609  /* level 1 */
610  int lowpass_height = s->plane[plane].band[0][0].height;
611  int lowpass_width = s->plane[plane].band[0][0].width;
612  int highpass_stride = s->plane[plane].band[0][1].stride;
613  int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
614  int16_t *low, *high, *output, *dst;
615 
616  if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
617  !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
618  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
619  ret = AVERROR(EINVAL);
620  goto end;
621  }
622 
623  av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
624 
625  low = s->plane[plane].subband[0];
626  high = s->plane[plane].subband[2];
627  output = s->plane[plane].l_h[0];
628  for (i = 0; i < lowpass_width; i++) {
629  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
630  low++;
631  high++;
632  output++;
633  }
634 
635  low = s->plane[plane].subband[1];
636  high = s->plane[plane].subband[3];
637  output = s->plane[plane].l_h[1];
638 
639  for (i = 0; i < lowpass_width; i++) {
640  // note the stride of "low" is highpass_stride
641  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
642  low++;
643  high++;
644  output++;
645  }
646 
647  low = s->plane[plane].l_h[0];
648  high = s->plane[plane].l_h[1];
649  output = s->plane[plane].subband[0];
650  for (i = 0; i < lowpass_height * 2; i++) {
651  horiz_filter(output, low, high, lowpass_width);
652  low += lowpass_width;
653  high += lowpass_width;
654  output += lowpass_width * 2;
655  }
656  if (s->bpc == 12) {
657  output = s->plane[plane].subband[0];
658  for (i = 0; i < lowpass_height * 2; i++) {
659  for (j = 0; j < lowpass_width * 2; j++)
660  output[j] <<= 2;
661 
662  output += lowpass_width * 2;
663  }
664  }
665 
666  /* level 2 */
667  lowpass_height = s->plane[plane].band[1][1].height;
668  lowpass_width = s->plane[plane].band[1][1].width;
669  highpass_stride = s->plane[plane].band[1][1].stride;
670 
671  if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
672  !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
673  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
674  ret = AVERROR(EINVAL);
675  goto end;
676  }
677 
678  av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
679 
680  low = s->plane[plane].subband[0];
681  high = s->plane[plane].subband[5];
682  output = s->plane[plane].l_h[3];
683  for (i = 0; i < lowpass_width; i++) {
684  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
685  low++;
686  high++;
687  output++;
688  }
689 
690  low = s->plane[plane].subband[4];
691  high = s->plane[plane].subband[6];
692  output = s->plane[plane].l_h[4];
693  for (i = 0; i < lowpass_width; i++) {
694  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
695  low++;
696  high++;
697  output++;
698  }
699 
700  low = s->plane[plane].l_h[3];
701  high = s->plane[plane].l_h[4];
702  output = s->plane[plane].subband[0];
703  for (i = 0; i < lowpass_height * 2; i++) {
704  horiz_filter(output, low, high, lowpass_width);
705  low += lowpass_width;
706  high += lowpass_width;
707  output += lowpass_width * 2;
708  }
709 
710  output = s->plane[plane].subband[0];
711  for (i = 0; i < lowpass_height * 2; i++) {
712  for (j = 0; j < lowpass_width * 2; j++)
713  output[j] <<= 2;
714 
715  output += lowpass_width * 2;
716  }
717 
718  /* level 3 */
719  lowpass_height = s->plane[plane].band[2][1].height;
720  lowpass_width = s->plane[plane].band[2][1].width;
721  highpass_stride = s->plane[plane].band[2][1].stride;
722 
723  if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
724  !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
725  av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
726  ret = AVERROR(EINVAL);
727  goto end;
728  }
729 
730  av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
731 
732  low = s->plane[plane].subband[0];
733  high = s->plane[plane].subband[8];
734  output = s->plane[plane].l_h[6];
735  for (i = 0; i < lowpass_width; i++) {
736  vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
737  low++;
738  high++;
739  output++;
740  }
741 
742  low = s->plane[plane].subband[7];
743  high = s->plane[plane].subband[9];
744  output = s->plane[plane].l_h[7];
745  for (i = 0; i < lowpass_width; i++) {
746  vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
747  low++;
748  high++;
749  output++;
750  }
751 
752  dst = (int16_t *)pic->data[act_plane];
753  low = s->plane[plane].l_h[6];
754  high = s->plane[plane].l_h[7];
755  for (i = 0; i < lowpass_height * 2; i++) {
756  horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
757  low += lowpass_width;
758  high += lowpass_width;
759  dst += pic->linesize[act_plane] / 2;
760  }
761  }
762 
763 
764 end:
765  if (ret < 0)
766  return ret;
767 
768  *got_frame = 1;
769  return avpkt->size;
770 }
771 
773 {
774  CFHDContext *s = avctx->priv_data;
775 
776  free_buffers(avctx);
777 
778  if (!avctx->internal->is_copy) {
779  ff_free_vlc(&s->vlc_9);
780  ff_free_vlc(&s->vlc_18);
781  }
782 
783  return 0;
784 }
785 
787  .name = "cfhd",
788  .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
789  .type = AVMEDIA_TYPE_VIDEO,
790  .id = AV_CODEC_ID_CFHD,
791  .priv_data_size = sizeof(CFHDContext),
793  .close = cfhd_close_decoder,
794  .decode = cfhd_decode,
797 };
#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:184
AVCodecContext * avctx
Definition: cfhd.h:72
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:2306
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:210
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:1602
#define VLC_BITS
Definition: asvdec.c:37
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
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
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:3077
AVCodec.
Definition: avcodec.h:3600
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
static int alloc_buffers(AVCodecContext *avctx)
Definition: cfhd.c:156
#define height
uint8_t * data
Definition: avcodec.h:1601
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:1382
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:160
int width
width and height of the video frame
Definition: frame.h:236
#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:111
#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:176
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
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:362
#define DWT_LEVELS
Definition: cfhd.h:41
int stride
Definition: cfhd.h:46
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
GLsizei count
Definition: opengl_enc.c:109
#define CLOSE_READER(name, gb)
Definition: get_bits.h:131
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1022
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
Definition: get_bits.h:490
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 width
#define FFSIGN(a)
Definition: common.h:73
int width
picture width / height.
Definition: avcodec.h:1863
static void init_plane_defaults(CFHDContext *s)
Definition: cfhd.c:50
uint16_t quantisation
Definition: cfhd.h:98
void avcodec_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: imgconvert.c:38
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:215
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:1676
int codebook
Definition: cfhd.h:102
#define OPEN_READER(name, gb)
Definition: get_bits.h:120
int subband_num
Definition: cfhd.h:103
int pshift
Definition: cfhd.h:100
AVCodec ff_cfhd_decoder
Definition: cfhd.c:786
static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
Definition: cfhd.c:772
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
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:358
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:342
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
uint8_t level
Definition: svq3.c:207
static void free_buffers(AVCodecContext *avctx)
Definition: cfhd.c:137
int coded_width
Definition: cfhd.h:85
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:157
ptrdiff_t stride
Definition: cfhd.h:59
int bpc
Definition: cfhd.h:93
void * priv_data
Definition: avcodec.h:1718
int len
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1726
int a_height
Definition: cfhd.h:90
static uint8_t tmp[8]
Definition: des.c:38
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:236
int ff_cfhd_init_vlcs(CFHDContext *s)
Definition: cfhddata.c:228
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
#define av_malloc_array(a, b)
#define stride
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:1578
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:360
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
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