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