2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
4 * This file is part of Libav.
6 * Libav 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.
11 * Libav 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Cineform HD video decoder
26 #include "libavutil/attributes.h"
27 #include "libavutil/buffer.h"
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
34 #include "bitstream.h"
35 #include "bytestream.h"
45 LowpassPrecision = 35,
49 BitsPerComponent = 101,
55 static av_cold int cfhd_init(AVCodecContext *avctx)
57 CFHDContext *s = avctx->priv_data;
59 memset(s, 0, sizeof(*s));
62 avctx->bits_per_raw_sample = 10;
64 return ff_cfhd_init_vlcs(s);
67 static void init_plane_defaults(CFHDContext *s)
71 s->subband_num_actual = 0;
74 static void init_frame_defaults(CFHDContext *s)
76 s->coded_format = AV_PIX_FMT_YUV422P10;
79 s->cropped_height = 0;
82 s->subband_cnt = SUBBAND_COUNT;
84 s->lowpass_precision = 16;
86 s->prescale_shift[0] = 0;
87 s->prescale_shift[1] = 0;
88 s->prescale_shift[2] = 0;
92 init_plane_defaults(s);
95 /* TODO: merge with VLC tables or use LUT */
96 static inline int dequant_and_decompand(int level, int quantisation)
98 int64_t abslevel = abs(level);
99 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
100 FFSIGN(level) * quantisation;
103 static inline void filter(int16_t *output, ptrdiff_t out_stride,
104 int16_t *low, ptrdiff_t low_stride,
105 int16_t *high, ptrdiff_t high_stride,
111 for (i = 0; i < len; i++) {
113 tmp = (11 * low[0 * low_stride] - 4 * low[1 * low_stride] + low[2 * low_stride] + 4) >> 3;
114 output[(2 * i + 0) * out_stride] = (tmp + high[0 * high_stride]) >> 1;
115 } else if (i == len - 1) {
116 tmp = (5 * low[i * low_stride] + 4 * low[(i - 1) * low_stride] - low[(i - 2) * low_stride] + 4) >> 3;
117 output[(2 * i + 0) * out_stride] = (tmp + high[i * high_stride]) >> 1;
119 tmp = (low[(i - 1) * low_stride] - low[(i + 1) * low_stride] + 4) >> 3;
120 output[(2 * i + 0) * out_stride] = (tmp + low[i * low_stride] + high[i * high_stride]) >> 1;
123 output[(2 * i + 0) * out_stride] = av_clip_uintp2_c(output[(2 * i + 0) * out_stride], clip);
126 tmp = (5 * low[0 * low_stride] + 4 * low[1 * low_stride] - low[2 * low_stride] + 4) >> 3;
127 output[(2 * i + 1) * out_stride] = (tmp - high[0 * high_stride]) >> 1;
128 } else if (i == len - 1) {
129 tmp = (11 * low[i * low_stride] - 4 * low[(i - 1) * low_stride] + low[(i - 2) * low_stride] + 4) >> 3;
130 output[(2 * i + 1) * out_stride] = (tmp - high[i * high_stride]) >> 1;
132 tmp = (low[(i + 1) * low_stride] - low[(i - 1) * low_stride] + 4) >> 3;
133 output[(2 * i + 1) * out_stride] = (tmp + low[i * low_stride] - high[i * high_stride]) >> 1;
136 output[(2 * i + 1) * out_stride] = av_clip_uintp2_c(output[(2 * i + 1) * out_stride], clip);
140 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
143 filter(output, 1, low, 1, high, 1, width, 0);
146 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
149 filter(output, 1, low, 1, high, 1, width, clip);
152 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
153 int16_t *low, ptrdiff_t low_stride,
154 int16_t *high, ptrdiff_t high_stride, int len)
156 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
159 static void free_buffers(CFHDContext *s)
163 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
164 av_freep(&s->plane[i].idwt_buf);
165 av_freep(&s->plane[i].idwt_tmp);
171 static int alloc_buffers(CFHDContext *s)
173 int i, j, ret, planes;
174 int chroma_x_shift, chroma_y_shift;
177 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
179 &chroma_y_shift)) < 0)
181 planes = av_pix_fmt_count_planes(s->coded_format);
183 for (i = 0; i < planes; i++) {
184 int w8, h8, w4, h4, w2, h2;
185 int width = i ? s->coded_width >> chroma_x_shift : s->coded_width;
186 int height = i ? s->coded_height >> chroma_y_shift : s->coded_height;
187 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
188 height = FFALIGN(height / 8, 2) * 8;
189 s->plane[i].width = width;
190 s->plane[i].height = height;
191 s->plane[i].stride = stride;
193 w8 = FFALIGN(s->plane[i].width / 8, 8);
194 h8 = FFALIGN(s->plane[i].height / 8, 2);
200 s->plane[i].idwt_buf =
201 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_buf));
202 s->plane[i].idwt_tmp =
203 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
204 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
205 return AVERROR(ENOMEM);
207 s->plane[i].subband[0] = s->plane[i].idwt_buf;
208 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
209 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
210 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
211 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
212 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
213 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
214 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
215 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
216 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
218 for (j = 0; j < DWT_LEVELS; j++) {
219 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
220 s->plane[i].band[j][k].a_width = w8 << j;
221 s->plane[i].band[j][k].a_height = h8 << j;
225 /* ll2 and ll1 commented out because they are done in-place */
226 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
227 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
228 // s->plane[i].l_h[2] = ll2;
229 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
230 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
231 // s->plane[i].l_h[5] = ll1;
232 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
233 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
236 s->a_height = s->coded_height;
237 s->a_width = s->coded_width;
238 s->a_format = s->coded_format;
243 static int parse_tag(CFHDContext *s, GetByteContext *gb,
244 int16_t *tag_, uint16_t *value, int *planes)
246 /* Bit weird but implement the tag parsing as the spec says */
247 uint16_t tagu = bytestream2_get_be16(gb);
249 int8_t tag8 = tagu >> 8;
250 uint16_t abstag = abs(tag);
251 int8_t abs_tag8 = abs(tag8);
252 uint16_t data = bytestream2_get_be16(gb);
256 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6F) {
257 av_log(s->avctx, AV_LOG_DEBUG, "large len %"PRIX16"\n",
258 ((tagu & 0xFF) << 16) | data);
260 } else if (abstag >= 0x4000 && abstag <= 0x40FF) {
261 av_log(s->avctx, AV_LOG_DEBUG, "Small chunk length %"PRIu16" %s\n",
262 data * 4, tag < 0 ? "optional" : "required");
263 bytestream2_skipu(gb, data * 4);
269 av_log(s->avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
274 av_log(s->avctx, AV_LOG_DEBUG,
275 "tag=2 header - skipping %"PRIu16" tag/value pairs\n", data);
276 if (data > bytestream2_get_bytes_left(gb) / 4) {
277 av_log(s->avctx, AV_LOG_ERROR,
278 "Too many tag/value pairs (%"PRIu16")\n", data);
279 return AVERROR_INVALIDDATA;
281 for (i = 0; i < data; i++) {
282 uint16_t tag2 = bytestream2_get_be16(gb);
283 uint16_t val2 = bytestream2_get_be16(gb);
284 av_log(s->avctx, AV_LOG_DEBUG, "Tag/Value = %"PRIX16" %"PRIX16"\n",
291 avpriv_report_missing_feature(s->avctx, "Transform type %"PRIu16, data);
292 return AVERROR_PATCHWELCOME;
294 av_log(s->avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
297 av_log(s->avctx, AV_LOG_DEBUG, "Channel count: %"PRIu16"\n", data);
299 avpriv_report_missing_feature(s->avctx, "Channel count %"PRIu16, data);
300 return AVERROR_PATCHWELCOME;
302 s->channel_cnt = data;
305 av_log(s->avctx, AV_LOG_DEBUG, "Subband count: %"PRIu16"\n", data);
306 if (data != SUBBAND_COUNT) {
307 avpriv_report_missing_feature(s->avctx, "Subband count %"PRIu16, data);
308 return AVERROR_PATCHWELCOME;
312 av_log(s->avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
313 s->coded_width = data;
316 av_log(s->avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
317 s->coded_height = data;
320 avpriv_report_missing_feature(s->avctx, "Skip frame");
321 return AVERROR_PATCHWELCOME;
323 av_log(s->avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
324 if (data < 2 || data > s->plane[s->channel_num].band[0][0].a_width) {
325 av_log(s->avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
326 return AVERROR_INVALIDDATA;
328 s->plane[s->channel_num].band[0][0].width = data;
329 s->plane[s->channel_num].band[0][0].stride = data;
332 av_log(s->avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
333 if (data < 2 || data > s->plane[s->channel_num].band[0][0].a_height) {
334 av_log(s->avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
335 return AVERROR_INVALIDDATA;
337 s->plane[s->channel_num].band[0][0].height = data;
339 case LowpassPrecision:
340 av_log(s->avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
344 av_log(s->avctx, AV_LOG_DEBUG,
345 "Highpass width%s %"PRIu16" channel %i level %i subband %i\n",
346 tag == 49 ? "2" : "", data,
347 s->channel_num, s->level, s->subband_num);
349 av_log(s->avctx, AV_LOG_ERROR, "Invalid highpass width%s\n", tag == 49 ? "2" : "");
350 return AVERROR_INVALIDDATA;
352 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
353 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
357 av_log(s->avctx, AV_LOG_DEBUG, "Highpass height%s %"PRIu16"\n", tag == 50 ? "2" : "", data);
359 av_log(s->avctx, AV_LOG_ERROR, "Invalid highpass height%s\n", tag == 50 ? "2" : "");
360 return AVERROR_INVALIDDATA;
362 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
365 av_log(s->avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
367 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband number\n");
368 return AVERROR_INVALIDDATA;
370 if (s->subband_num != 0 && data == 1) {
371 if (s->level + 1 >= DWT_LEVELS) {
372 av_log(s->avctx, AV_LOG_ERROR, "Invalid level\n");
373 return AVERROR_INVALIDDATA;
378 s->subband_num = data;
381 av_log(s->avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
382 if (data >= SUBBAND_COUNT) {
383 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
384 return AVERROR_INVALIDDATA;
386 s->subband_num_actual = data;
389 s->quantisation = data;
390 av_log(s->avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
393 av_log(s->avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
394 if (data >= *planes) {
395 av_log(s->avctx, AV_LOG_ERROR, "Invalid channel number\n");
396 return AVERROR_INVALIDDATA;
398 s->channel_num = data;
399 init_plane_defaults(s);
402 av_log(s->avctx, AV_LOG_DEBUG,
403 "Subsampling or bit-depth flag? %"PRIu16"\n", data);
404 if (!(data == 10 || data == 12)) {
405 av_log(s->avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
406 return AVERROR_INVALIDDATA;
412 av_log(s->avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
416 av_log(s->avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
419 av_log(s->avctx, AV_LOG_DEBUG, "Sample format? %"PRIu16"\n", data);
422 s->coded_format = AV_PIX_FMT_YUV422P10;
425 s->coded_format = AV_PIX_FMT_GBRP12;
428 s->coded_format = AV_PIX_FMT_GBRAP12;
431 avpriv_report_missing_feature(s->avctx, "Sample format %"PRIu16, data);
432 return AVERROR_PATCHWELCOME;
434 *planes = av_pix_fmt_count_planes(s->coded_format);
437 av_log(s->avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
438 s->cropped_height = data;
441 av_log(s->avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
445 s->prescale_shift[0] = (data >> 0) & 0x7;
446 s->prescale_shift[1] = (data >> 3) & 0x7;
447 s->prescale_shift[2] = (data >> 6) & 0x7;
448 av_log(s->avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %"PRIX16"\n", data);
451 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tag %"PRIu16" data %"PRIX16"\n",
458 static int read_lowpass_coeffs(CFHDContext *s, GetByteContext *gb,
462 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
463 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
464 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
465 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
467 if (lowpass_height > lowpass_a_height ||
468 lowpass_width > lowpass_a_width ||
469 lowpass_a_width * lowpass_a_height * sizeof(*coeff_data) > bytestream2_get_bytes_left(gb)) {
470 av_log(s->avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
471 return AVERROR_INVALIDDATA;
474 av_log(s->avctx, AV_LOG_DEBUG,
475 "Start of lowpass coeffs component %d height:%d, width:%d\n",
476 s->channel_num, lowpass_height, lowpass_width);
477 for (i = 0; i < lowpass_height; i++) {
478 for (j = 0; j < lowpass_width; j++)
479 coeff_data[j] = bytestream2_get_be16u(gb);
481 coeff_data += lowpass_width;
484 /* Align to mod-4 position to continue reading tags */
485 bytestream2_seek(gb, bytestream2_tell(gb) & 3, SEEK_CUR);
487 /* Copy last coefficient line if height is odd. */
488 if (lowpass_height & 1) {
489 int16_t *last_line = &coeff_data[lowpass_height * lowpass_width];
490 memcpy(last_line, &last_line[-lowpass_width],
491 lowpass_width * sizeof(*coeff_data));
494 av_log(s->avctx, AV_LOG_DEBUG, "Lowpass coefficients %i\n",
495 lowpass_width * lowpass_height);
500 #define DECODE_SUBBAND_COEFFS(TABLE, COND) \
502 int level, run, coeff; \
503 BITSTREAM_RL_VLC(level, run, &s->bc, s->TABLE, VLC_BITS, 3); \
511 if (count > expected) { \
512 av_log(s->avctx, AV_LOG_ERROR, "Escape codeword not found, " \
513 "probably corrupt data\n"); \
514 return AVERROR_INVALIDDATA; \
517 coeff = dequant_and_decompand(level, s->quantisation); \
518 for (i = 0; i < run; i++) \
519 *coeff_data++ = coeff; \
522 static int read_highpass_coeffs(CFHDContext *s, GetByteContext *gb,
526 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
527 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
528 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
529 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
530 ptrdiff_t highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
531 int expected = highpass_height * highpass_stride;
532 int a_expected = highpass_a_height * highpass_a_width;
536 if (highpass_height > highpass_a_height ||
537 highpass_width > highpass_a_width ||
538 a_expected < expected) {
539 av_log(s->avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
540 return AVERROR_INVALIDDATA;
543 av_log(s->avctx, AV_LOG_DEBUG,
544 "Start subband coeffs plane %i level %i codebook %i expected %i\n",
545 s->channel_num, s->level, s->codebook, expected);
547 if ((ret = bitstream_init8(&s->bc, gb->buffer,
548 bytestream2_get_bytes_left(gb))) < 0)
551 DECODE_SUBBAND_COEFFS(table_9_rl_vlc, level == 64)
553 DECODE_SUBBAND_COEFFS(table_18_rl_vlc, level == 255 && run == 2)
556 bytes = FFALIGN(AV_CEIL_RSHIFT(bitstream_tell(&s->bc), 3), 4);
557 if (bytes > bytestream2_get_bytes_left(gb)) {
558 av_log(s->avctx, AV_LOG_ERROR, "Bitstream overread error\n");
559 return AVERROR_INVALIDDATA;
561 bytestream2_seek(gb, bytes, SEEK_CUR);
563 av_log(s->avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n",
564 count, count - expected);
567 /* Copy last coefficient line if height is odd. */
568 if (highpass_height & 1) {
569 int16_t *last_line = &coeff_data[expected];
570 memcpy(last_line, &last_line[-highpass_stride],
571 highpass_stride * sizeof(*coeff_data));
577 static int reconstruct_level(CFHDContext *s, AVFrame *pic, int plane, int level)
579 int i, j, idx = level - 1, idx2 = level > 1 ? 1 : 0;
580 int16_t *low, *high, *output, *dst;
581 int lowpass_height = s->plane[plane].band[idx][idx2].height;
582 int lowpass_width = s->plane[plane].band[idx][idx2].width;
583 ptrdiff_t highpass_stride = s->plane[plane].band[idx][1].stride;
585 if (lowpass_height > s->plane[plane].band[idx][idx2].a_height ||
586 lowpass_width > s->plane[plane].band[idx][idx2].a_width ||
587 s->plane[plane].band[idx][1].width > s->plane[plane].band[idx][1].a_width ||
589 av_log(s->avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
590 return AVERROR_INVALIDDATA;
593 av_log(s->avctx, AV_LOG_DEBUG, "Level %d plane %i %i %i %ti\n",
594 level, plane, lowpass_height, lowpass_width, highpass_stride);
596 low = s->plane[plane].subband[0];
597 high = s->plane[plane].subband[2 + 3 * idx];
598 output = s->plane[plane].l_h[3 * idx];
599 for (i = 0; i < lowpass_width; i++) {
600 vert_filter(output, lowpass_width, low, lowpass_width, high,
601 highpass_stride, lowpass_height);
607 low = s->plane[plane].subband[1 + 3 * idx];
608 high = s->plane[plane].subband[3 + 3 * idx];
609 output = s->plane[plane].l_h[1 + 3 * idx];
610 for (i = 0; i < lowpass_width; i++) {
611 // note the stride of "low" is highpass_stride
612 vert_filter(output, lowpass_width, low, highpass_stride, high,
613 highpass_stride, lowpass_height);
619 low = s->plane[plane].l_h[0 + 3 * idx];
620 high = s->plane[plane].l_h[1 + 3 * idx];
623 output = s->plane[plane].subband[0];
624 for (i = 0; i < lowpass_height * 2; i++) {
625 horiz_filter(output, low, high, lowpass_width);
626 low += lowpass_width;
627 high += lowpass_width;
628 output += lowpass_width * 2;
630 if (s->bpc == 12 || level == 2) {
631 output = s->plane[plane].subband[0];
632 for (i = 0; i < lowpass_height * 2; i++) {
633 for (j = 0; j < lowpass_width * 2; j++)
636 output += lowpass_width * 2;
640 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
641 dst = (int16_t *)pic->data[act_plane];
642 for (i = 0; i < lowpass_height * 2; i++) {
643 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
644 low += lowpass_width;
645 high += lowpass_width;
646 dst += pic->linesize[act_plane] / 2;
653 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
656 CFHDContext *s = avctx->priv_data;
658 ThreadFrame frame = { .f = data };
659 int ret = 0, planes, plane;
663 init_frame_defaults(s);
664 planes = av_pix_fmt_count_planes(s->coded_format);
666 bytestream2_init(&gb, avpkt->data, avpkt->size);
668 while (bytestream2_get_bytes_left(&gb) > 4) {
669 if ((ret = parse_tag(s, &gb, &tag, &value, &planes)) < 0)
672 /* Some kind of end of header tag */
673 if (tag == 4 && value == 0x1A4A)
677 if (s->coded_width <= 0 || s->coded_height <= 0 || s->coded_format == AV_PIX_FMT_NONE) {
678 av_log(avctx, AV_LOG_ERROR, "Video dimensions/format missing or invalid\n");
679 return AVERROR_INVALIDDATA;
682 ret = ff_set_dimensions(s->avctx, s->coded_width, s->coded_height);
685 if (s->cropped_height)
686 s->avctx->height = s->cropped_height;
688 s->avctx->pix_fmt = s->coded_format;
690 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
691 s->a_format != s->coded_format) {
693 if ((ret = alloc_buffers(s)) < 0) {
699 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
704 s->coded_format = AV_PIX_FMT_NONE;
706 while (bytestream2_get_bytes_left(&gb) > 4) {
709 if ((ret = parse_tag(s, &gb, &tag, &value, &planes)) < 0)
712 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
713 if (tag == 4 && value == 0x0F0F) {
714 if ((ret = read_lowpass_coeffs(s, &gb, coeff_data)) < 0)
716 } else if (tag == 55 && s->subband_num_actual != 255) {
717 if ((ret = read_highpass_coeffs(s, &gb, coeff_data)) < 0)
722 if (s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
723 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
724 return AVERROR_INVALIDDATA;
727 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
728 for (plane = 0; plane < planes; plane++) {
730 if ((ret = reconstruct_level(s, data, plane, 1)) < 0)
734 if ((ret = reconstruct_level(s, data, plane, 2)) < 0)
738 if ((ret = reconstruct_level(s, data, plane, 3)) < 0)
746 static av_cold int cfhd_close(AVCodecContext *avctx)
748 CFHDContext *s = avctx->priv_data;
752 ff_free_vlc(&s->vlc_9);
753 ff_free_vlc(&s->vlc_18);
758 AVCodec ff_cfhd_decoder = {
760 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
761 .type = AVMEDIA_TYPE_VIDEO,
762 .id = AV_CODEC_ID_CFHD,
763 .priv_data_size = sizeof(CFHDContext),
765 .init_thread_copy = ONLY_IF_THREADS_ENABLED(cfhd_init),
767 .decode = cfhd_decode,
768 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
769 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,