2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
4 * This file is part of FFmpeg.
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.
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.
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
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 "bytestream.h"
40 #define ALPHA_COMPAND_DC_OFFSET 256
41 #define ALPHA_COMPAND_GAIN 9400
48 LowpassPrecision = 35,
52 BitsPerComponent = 101,
58 static av_cold int cfhd_init(AVCodecContext *avctx)
60 CFHDContext *s = avctx->priv_data;
62 avctx->bits_per_raw_sample = 10;
65 return ff_cfhd_init_vlcs(s);
68 static void init_plane_defaults(CFHDContext *s)
72 s->subband_num_actual = 0;
75 static void init_frame_defaults(CFHDContext *s)
79 s->cropped_height = 0;
82 s->subband_cnt = SUBBAND_COUNT;
84 s->lowpass_precision = 16;
89 init_plane_defaults(s);
92 /* TODO: merge with VLC tables or use LUT */
93 static inline int dequant_and_decompand(int level, int quantisation)
95 int64_t abslevel = abs(level);
96 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
97 FFSIGN(level) * quantisation;
100 static inline void process_alpha(int16_t *alpha, int width)
103 for (i = 0; i < width; i++) {
105 channel -= ALPHA_COMPAND_DC_OFFSET;
107 channel *= ALPHA_COMPAND_GAIN;
109 channel = av_clip_uintp2(channel, 12);
114 static inline void filter(int16_t *output, ptrdiff_t out_stride,
115 int16_t *low, ptrdiff_t low_stride,
116 int16_t *high, ptrdiff_t high_stride,
122 for (i = 0; i < len; i++) {
124 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
125 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
127 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
129 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
130 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
132 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
133 } else if (i == len-1) {
134 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
135 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
137 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
139 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
140 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
142 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
144 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
145 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
147 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
149 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
150 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
152 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
157 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
160 filter(output, 1, low, 1, high, 1, width, 0);
163 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
166 filter(output, 1, low, 1, high, 1, width, clip);
169 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
170 int16_t *low, ptrdiff_t low_stride,
171 int16_t *high, ptrdiff_t high_stride, int len)
173 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
176 static void free_buffers(CFHDContext *s)
180 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
181 av_freep(&s->plane[i].idwt_buf);
182 av_freep(&s->plane[i].idwt_tmp);
184 for (j = 0; j < 9; j++)
185 s->plane[i].subband[j] = NULL;
187 for (j = 0; j < 8; j++)
188 s->plane[i].l_h[j] = NULL;
194 static int alloc_buffers(AVCodecContext *avctx)
196 CFHDContext *s = avctx->priv_data;
197 int i, j, ret, planes;
198 int chroma_x_shift, chroma_y_shift;
201 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
203 avctx->pix_fmt = s->coded_format;
205 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
207 &chroma_y_shift)) < 0)
209 planes = av_pix_fmt_count_planes(s->coded_format);
211 for (i = 0; i < planes; i++) {
212 int w8, h8, w4, h4, w2, h2;
213 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
214 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
215 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
217 height = FFALIGN(height / 8, 2) * 8;
218 s->plane[i].width = width;
219 s->plane[i].height = height;
220 s->plane[i].stride = stride;
222 w8 = FFALIGN(s->plane[i].width / 8, 8);
229 s->plane[i].idwt_buf =
230 av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
231 s->plane[i].idwt_tmp =
232 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
233 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
234 return AVERROR(ENOMEM);
236 s->plane[i].subband[0] = s->plane[i].idwt_buf;
237 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
238 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
239 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
240 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
241 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
242 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
243 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
244 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
245 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
247 for (j = 0; j < DWT_LEVELS; j++) {
248 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
249 s->plane[i].band[j][k].a_width = w8 << j;
250 s->plane[i].band[j][k].a_height = h8 << j;
254 /* ll2 and ll1 commented out because they are done in-place */
255 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
256 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
257 // s->plane[i].l_h[2] = ll2;
258 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
259 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
260 // s->plane[i].l_h[5] = ll1;
261 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
262 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
265 s->a_height = s->coded_height;
266 s->a_width = s->coded_width;
267 s->a_format = s->coded_format;
272 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
275 CFHDContext *s = avctx->priv_data;
277 ThreadFrame frame = { .f = data };
279 int ret = 0, i, j, planes, plane, got_buffer = 0;
282 s->coded_format = AV_PIX_FMT_YUV422P10;
283 init_frame_defaults(s);
284 planes = av_pix_fmt_count_planes(s->coded_format);
286 bytestream2_init(&gb, avpkt->data, avpkt->size);
288 while (bytestream2_get_bytes_left(&gb) > 4) {
289 /* Bit weird but implement the tag parsing as the spec says */
290 uint16_t tagu = bytestream2_get_be16(&gb);
291 int16_t tag = (int16_t)tagu;
292 int8_t tag8 = (int8_t)(tagu >> 8);
293 uint16_t abstag = abs(tag);
294 int8_t abs_tag8 = abs(tag8);
295 uint16_t data = bytestream2_get_be16(&gb);
296 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
297 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
298 } else if (tag == ImageWidth) {
299 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
300 s->coded_width = data;
301 } else if (tag == ImageHeight) {
302 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
303 s->coded_height = data;
304 } else if (tag == 101) {
305 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
306 if (data < 1 || data > 31) {
307 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
308 ret = AVERROR(EINVAL);
312 } else if (tag == ChannelCount) {
313 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
314 s->channel_cnt = data;
316 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
317 ret = AVERROR_PATCHWELCOME;
320 } else if (tag == SubbandCount) {
321 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
322 if (data != SUBBAND_COUNT) {
323 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
324 ret = AVERROR_PATCHWELCOME;
327 } else if (tag == ChannelNumber) {
328 s->channel_num = data;
329 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
330 if (s->channel_num >= planes) {
331 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
332 ret = AVERROR(EINVAL);
335 init_plane_defaults(s);
336 } else if (tag == SubbandNumber) {
337 if (s->subband_num != 0 && data == 1) // hack
339 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
340 s->subband_num = data;
341 if (s->level >= DWT_LEVELS) {
342 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
343 ret = AVERROR(EINVAL);
346 if (s->subband_num > 3) {
347 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
348 ret = AVERROR(EINVAL);
351 } else if (tag == 51) {
352 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
353 s->subband_num_actual = data;
354 if (s->subband_num_actual >= 10) {
355 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
356 ret = AVERROR(EINVAL);
359 } else if (tag == LowpassPrecision)
360 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
361 else if (tag == Quantization) {
362 s->quantisation = data;
363 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
364 } else if (tag == PrescaleShift) {
365 s->prescale_shift[0] = (data >> 0) & 0x7;
366 s->prescale_shift[1] = (data >> 3) & 0x7;
367 s->prescale_shift[2] = (data >> 6) & 0x7;
368 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
369 } else if (tag == 27) {
370 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
371 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
372 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
373 ret = AVERROR(EINVAL);
376 s->plane[s->channel_num].band[0][0].width = data;
377 s->plane[s->channel_num].band[0][0].stride = data;
378 } else if (tag == 28) {
379 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
380 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
381 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
382 ret = AVERROR(EINVAL);
385 s->plane[s->channel_num].band[0][0].height = data;
387 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
388 else if (tag == 10) {
390 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
391 ret = AVERROR_PATCHWELCOME;
394 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
395 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
396 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
397 bytestream2_skipu(&gb, data * 4);
398 } else if (tag == 23) {
399 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
400 avpriv_report_missing_feature(avctx, "Skip frame");
401 ret = AVERROR_PATCHWELCOME;
403 } else if (tag == 2) {
404 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
405 if (data > bytestream2_get_bytes_left(&gb) / 4) {
406 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
407 ret = AVERROR_INVALIDDATA;
410 for (i = 0; i < data; i++) {
411 uint16_t tag2 = bytestream2_get_be16(&gb);
412 uint16_t val2 = bytestream2_get_be16(&gb);
413 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
415 } else if (tag == 41) {
416 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);
418 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
419 ret = AVERROR(EINVAL);
422 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
423 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
424 } else if (tag == 42) {
425 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
427 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
428 ret = AVERROR(EINVAL);
431 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
432 } else if (tag == 49) {
433 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
435 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
436 ret = AVERROR(EINVAL);
439 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
440 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
441 } else if (tag == 50) {
442 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
444 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
445 ret = AVERROR(EINVAL);
448 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
449 } else if (tag == 71) {
451 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
452 } else if (tag == 72) {
454 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
455 } else if (tag == 70) {
456 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
457 if (!(data == 10 || data == 12)) {
458 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
459 ret = AVERROR(EINVAL);
463 } else if (tag == 84) {
464 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
466 s->coded_format = AV_PIX_FMT_YUV422P10;
468 s->coded_format = AV_PIX_FMT_GBRP12;
470 s->coded_format = AV_PIX_FMT_GBRAP12;
472 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
473 ret = AVERROR_PATCHWELCOME;
476 planes = av_pix_fmt_count_planes(s->coded_format);
477 } else if (tag == -85) {
478 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
479 s->cropped_height = data;
481 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
483 /* Some kind of end of header tag */
484 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
485 s->coded_format != AV_PIX_FMT_NONE) {
486 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
487 s->a_format != s->coded_format) {
489 if ((ret = alloc_buffers(avctx)) < 0) {
494 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
497 if (s->cropped_height)
498 avctx->height = s->cropped_height;
502 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
507 s->coded_format = AV_PIX_FMT_NONE;
510 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
512 /* Lowpass coefficients */
513 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
514 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
515 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
516 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
517 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
520 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
521 ret = AVERROR(EINVAL);
525 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
526 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
527 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
528 ret = AVERROR(EINVAL);
532 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
533 for (i = 0; i < lowpass_height; i++) {
534 for (j = 0; j < lowpass_width; j++)
535 coeff_data[j] = bytestream2_get_be16u(&gb);
537 coeff_data += lowpass_width;
540 /* Align to mod-4 position to continue reading tags */
541 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
543 /* Copy last line of coefficients if odd height */
544 if (lowpass_height & 1) {
545 memcpy(&coeff_data[lowpass_height * lowpass_width],
546 &coeff_data[(lowpass_height - 1) * lowpass_width],
547 lowpass_width * sizeof(*coeff_data));
550 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
553 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
554 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
555 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
556 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
557 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
558 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
560 int a_expected = highpass_a_height * highpass_a_width;
561 int level, run, coeff;
562 int count = 0, bytes;
565 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
566 ret = AVERROR(EINVAL);
570 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
571 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
572 ret = AVERROR(EINVAL);
575 expected = highpass_height * highpass_stride;
577 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);
579 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
581 OPEN_READER(re, &s->gb);
584 UPDATE_CACHE(re, &s->gb);
585 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
594 if (count > expected)
597 coeff = dequant_and_decompand(level, s->quantisation);
598 for (i = 0; i < run; i++)
599 *coeff_data++ = coeff;
603 UPDATE_CACHE(re, &s->gb);
604 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
608 if (level == 255 && run == 2)
613 if (count > expected)
616 coeff = dequant_and_decompand(level, s->quantisation);
617 for (i = 0; i < run; i++)
618 *coeff_data++ = coeff;
621 CLOSE_READER(re, &s->gb);
624 if (count > expected) {
625 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
626 ret = AVERROR(EINVAL);
630 bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
631 if (bytes > bytestream2_get_bytes_left(&gb)) {
632 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
633 ret = AVERROR(EINVAL);
636 bytestream2_seek(&gb, bytes, SEEK_CUR);
638 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
641 /* Copy last line of coefficients if odd height */
642 if (highpass_height & 1) {
643 memcpy(&coeff_data[highpass_height * highpass_stride],
644 &coeff_data[(highpass_height - 1) * highpass_stride],
645 highpass_stride * sizeof(*coeff_data));
650 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
651 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
652 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
653 ret = AVERROR(EINVAL);
658 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
659 ret = AVERROR(EINVAL);
663 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
664 for (plane = 0; plane < planes && !ret; plane++) {
666 int lowpass_height = s->plane[plane].band[0][0].height;
667 int lowpass_width = s->plane[plane].band[0][0].width;
668 int highpass_stride = s->plane[plane].band[0][1].stride;
669 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
670 int16_t *low, *high, *output, *dst;
672 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
673 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
674 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
675 ret = AVERROR(EINVAL);
679 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
681 low = s->plane[plane].subband[0];
682 high = s->plane[plane].subband[2];
683 output = s->plane[plane].l_h[0];
684 for (i = 0; i < lowpass_width; i++) {
685 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
691 low = s->plane[plane].subband[1];
692 high = s->plane[plane].subband[3];
693 output = s->plane[plane].l_h[1];
695 for (i = 0; i < lowpass_width; i++) {
696 // note the stride of "low" is highpass_stride
697 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
703 low = s->plane[plane].l_h[0];
704 high = s->plane[plane].l_h[1];
705 output = s->plane[plane].subband[0];
706 for (i = 0; i < lowpass_height * 2; i++) {
707 horiz_filter(output, low, high, lowpass_width);
708 low += lowpass_width;
709 high += lowpass_width;
710 output += lowpass_width * 2;
713 output = s->plane[plane].subband[0];
714 for (i = 0; i < lowpass_height * 2; i++) {
715 for (j = 0; j < lowpass_width * 2; j++)
718 output += lowpass_width * 2;
723 lowpass_height = s->plane[plane].band[1][1].height;
724 lowpass_width = s->plane[plane].band[1][1].width;
725 highpass_stride = s->plane[plane].band[1][1].stride;
727 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
728 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
729 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
730 ret = AVERROR(EINVAL);
734 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
736 low = s->plane[plane].subband[0];
737 high = s->plane[plane].subband[5];
738 output = s->plane[plane].l_h[3];
739 for (i = 0; i < lowpass_width; i++) {
740 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
746 low = s->plane[plane].subband[4];
747 high = s->plane[plane].subband[6];
748 output = s->plane[plane].l_h[4];
749 for (i = 0; i < lowpass_width; i++) {
750 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
756 low = s->plane[plane].l_h[3];
757 high = s->plane[plane].l_h[4];
758 output = s->plane[plane].subband[0];
759 for (i = 0; i < lowpass_height * 2; i++) {
760 horiz_filter(output, low, high, lowpass_width);
761 low += lowpass_width;
762 high += lowpass_width;
763 output += lowpass_width * 2;
766 output = s->plane[plane].subband[0];
767 for (i = 0; i < lowpass_height * 2; i++) {
768 for (j = 0; j < lowpass_width * 2; j++)
771 output += lowpass_width * 2;
775 lowpass_height = s->plane[plane].band[2][1].height;
776 lowpass_width = s->plane[plane].band[2][1].width;
777 highpass_stride = s->plane[plane].band[2][1].stride;
779 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
780 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
781 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
782 ret = AVERROR(EINVAL);
786 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
788 low = s->plane[plane].subband[0];
789 high = s->plane[plane].subband[8];
790 output = s->plane[plane].l_h[6];
791 for (i = 0; i < lowpass_width; i++) {
792 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
798 low = s->plane[plane].subband[7];
799 high = s->plane[plane].subband[9];
800 output = s->plane[plane].l_h[7];
801 for (i = 0; i < lowpass_width; i++) {
802 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
808 dst = (int16_t *)pic->data[act_plane];
809 low = s->plane[plane].l_h[6];
810 high = s->plane[plane].l_h[7];
811 for (i = 0; i < lowpass_height * 2; i++) {
812 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
814 process_alpha(dst, lowpass_width * 2);
815 low += lowpass_width;
816 high += lowpass_width;
817 dst += pic->linesize[act_plane] / 2;
830 static av_cold int cfhd_close(AVCodecContext *avctx)
832 CFHDContext *s = avctx->priv_data;
836 if (!avctx->internal->is_copy) {
837 ff_free_vlc(&s->vlc_9);
838 ff_free_vlc(&s->vlc_18);
844 AVCodec ff_cfhd_decoder = {
846 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
847 .type = AVMEDIA_TYPE_VIDEO,
848 .id = AV_CODEC_ID_CFHD,
849 .priv_data_size = sizeof(CFHDContext),
852 .decode = cfhd_decode,
853 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
854 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,