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"
45 LowpassPrecision = 35,
49 BitsPerComponent = 101,
55 static av_cold int cfhd_init(AVCodecContext *avctx)
57 CFHDContext *s = avctx->priv_data;
59 avctx->bits_per_raw_sample = 10;
62 return ff_cfhd_init_vlcs(s);
65 static void init_plane_defaults(CFHDContext *s)
69 s->subband_num_actual = 0;
72 static void init_frame_defaults(CFHDContext *s)
76 s->cropped_height = 0;
79 s->subband_cnt = SUBBAND_COUNT;
81 s->lowpass_precision = 16;
86 init_plane_defaults(s);
89 /* TODO: merge with VLC tables or use LUT */
90 static inline int dequant_and_decompand(int level, int quantisation)
92 int64_t abslevel = abs(level);
93 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
94 FFSIGN(level) * quantisation;
97 static inline void filter(int16_t *output, ptrdiff_t out_stride,
98 int16_t *low, ptrdiff_t low_stride,
99 int16_t *high, ptrdiff_t high_stride,
105 for (i = 0; i < len; i++) {
107 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
108 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
110 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
112 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
113 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
115 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
116 } else if (i == len-1) {
117 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
118 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
120 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
122 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
123 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
125 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
127 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
128 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
130 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
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;
135 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);
167 for (j = 0; j < 9; j++)
168 s->plane[i].subband[j] = NULL;
170 for (j = 0; j < 8; j++)
171 s->plane[i].l_h[j] = NULL;
177 static int alloc_buffers(AVCodecContext *avctx)
179 CFHDContext *s = avctx->priv_data;
180 int i, j, ret, planes;
181 int chroma_x_shift, chroma_y_shift;
184 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
186 avctx->pix_fmt = s->coded_format;
188 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
190 &chroma_y_shift)) < 0)
192 planes = av_pix_fmt_count_planes(s->coded_format);
194 for (i = 0; i < planes; i++) {
195 int w8, h8, w4, h4, w2, h2;
196 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
197 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
198 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
199 height = FFALIGN(height / 8, 2) * 8;
200 s->plane[i].width = width;
201 s->plane[i].height = height;
202 s->plane[i].stride = stride;
204 w8 = FFALIGN(s->plane[i].width / 8, 8);
205 h8 = FFALIGN(s->plane[i].height / 8, 2);
211 s->plane[i].idwt_buf =
212 av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
213 s->plane[i].idwt_tmp =
214 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
215 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
216 return AVERROR(ENOMEM);
218 s->plane[i].subband[0] = s->plane[i].idwt_buf;
219 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
220 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
221 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
222 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
223 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
224 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
225 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
226 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
227 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
229 for (j = 0; j < DWT_LEVELS; j++) {
230 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
231 s->plane[i].band[j][k].a_width = w8 << j;
232 s->plane[i].band[j][k].a_height = h8 << j;
236 /* ll2 and ll1 commented out because they are done in-place */
237 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
238 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
239 // s->plane[i].l_h[2] = ll2;
240 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
241 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
242 // s->plane[i].l_h[5] = ll1;
243 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
244 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
247 s->a_height = s->coded_height;
248 s->a_width = s->coded_width;
249 s->a_format = s->coded_format;
254 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
257 CFHDContext *s = avctx->priv_data;
259 ThreadFrame frame = { .f = data };
261 int ret = 0, i, j, planes, plane, got_buffer = 0;
264 s->coded_format = AV_PIX_FMT_YUV422P10;
265 init_frame_defaults(s);
266 planes = av_pix_fmt_count_planes(s->coded_format);
268 bytestream2_init(&gb, avpkt->data, avpkt->size);
270 while (bytestream2_get_bytes_left(&gb) > 4) {
271 /* Bit weird but implement the tag parsing as the spec says */
272 uint16_t tagu = bytestream2_get_be16(&gb);
273 int16_t tag = (int16_t)tagu;
274 int8_t tag8 = (int8_t)(tagu >> 8);
275 uint16_t abstag = abs(tag);
276 int8_t abs_tag8 = abs(tag8);
277 uint16_t data = bytestream2_get_be16(&gb);
278 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
279 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
280 } else if (tag == ImageWidth) {
281 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
282 s->coded_width = data;
283 } else if (tag == ImageHeight) {
284 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
285 s->coded_height = data;
286 } else if (tag == 101) {
287 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
288 if (data < 1 || data > 31) {
289 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
290 ret = AVERROR(EINVAL);
294 } else if (tag == ChannelCount) {
295 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
296 s->channel_cnt = data;
298 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
299 ret = AVERROR_PATCHWELCOME;
302 } else if (tag == SubbandCount) {
303 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
304 if (data != SUBBAND_COUNT) {
305 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
306 ret = AVERROR_PATCHWELCOME;
309 } else if (tag == ChannelNumber) {
310 s->channel_num = data;
311 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
312 if (s->channel_num >= planes) {
313 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
314 ret = AVERROR(EINVAL);
317 init_plane_defaults(s);
318 } else if (tag == SubbandNumber) {
319 if (s->subband_num != 0 && data == 1) // hack
321 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
322 s->subband_num = data;
323 if (s->level >= DWT_LEVELS) {
324 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
325 ret = AVERROR(EINVAL);
328 if (s->subband_num > 3) {
329 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
330 ret = AVERROR(EINVAL);
333 } else if (tag == 51) {
334 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
335 s->subband_num_actual = data;
336 if (s->subband_num_actual >= 10) {
337 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
338 ret = AVERROR(EINVAL);
341 } else if (tag == LowpassPrecision)
342 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
343 else if (tag == Quantization) {
344 s->quantisation = data;
345 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
346 } else if (tag == PrescaleShift) {
347 s->prescale_shift[0] = (data >> 0) & 0x7;
348 s->prescale_shift[1] = (data >> 3) & 0x7;
349 s->prescale_shift[2] = (data >> 6) & 0x7;
350 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
351 } else if (tag == 27) {
352 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
353 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
354 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
355 ret = AVERROR(EINVAL);
358 s->plane[s->channel_num].band[0][0].width = data;
359 s->plane[s->channel_num].band[0][0].stride = data;
360 } else if (tag == 28) {
361 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
362 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
363 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
364 ret = AVERROR(EINVAL);
367 s->plane[s->channel_num].band[0][0].height = data;
369 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
370 else if (tag == 10) {
372 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
373 ret = AVERROR_PATCHWELCOME;
376 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
377 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
378 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
379 bytestream2_skipu(&gb, data * 4);
380 } else if (tag == 23) {
381 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
382 avpriv_report_missing_feature(avctx, "Skip frame");
383 ret = AVERROR_PATCHWELCOME;
385 } else if (tag == 2) {
386 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
387 if (data > bytestream2_get_bytes_left(&gb) / 4) {
388 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
389 ret = AVERROR_INVALIDDATA;
392 for (i = 0; i < data; i++) {
393 uint16_t tag2 = bytestream2_get_be16(&gb);
394 uint16_t val2 = bytestream2_get_be16(&gb);
395 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
397 } else if (tag == 41) {
398 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);
400 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
401 ret = AVERROR(EINVAL);
404 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
405 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
406 } else if (tag == 42) {
407 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
409 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
410 ret = AVERROR(EINVAL);
413 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
414 } else if (tag == 49) {
415 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
417 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
418 ret = AVERROR(EINVAL);
421 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
422 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
423 } else if (tag == 50) {
424 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
426 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
427 ret = AVERROR(EINVAL);
430 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
431 } else if (tag == 71) {
433 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
434 } else if (tag == 72) {
436 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
437 } else if (tag == 70) {
438 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
439 if (!(data == 10 || data == 12)) {
440 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
441 ret = AVERROR(EINVAL);
445 } else if (tag == 84) {
446 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
448 s->coded_format = AV_PIX_FMT_YUV422P10;
450 s->coded_format = AV_PIX_FMT_GBRP12;
452 s->coded_format = AV_PIX_FMT_GBRAP12;
454 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
455 ret = AVERROR_PATCHWELCOME;
458 planes = av_pix_fmt_count_planes(s->coded_format);
459 } else if (tag == -85) {
460 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
461 s->cropped_height = data;
463 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
465 /* Some kind of end of header tag */
466 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
467 s->coded_format != AV_PIX_FMT_NONE) {
468 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
469 s->a_format != s->coded_format) {
471 if ((ret = alloc_buffers(avctx)) < 0) {
476 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
479 if (s->cropped_height)
480 avctx->height = s->cropped_height;
484 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
489 s->coded_format = AV_PIX_FMT_NONE;
492 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
494 /* Lowpass coefficients */
495 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
496 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
497 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
498 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
499 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
502 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
503 ret = AVERROR(EINVAL);
507 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
508 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
509 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
510 ret = AVERROR(EINVAL);
514 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
515 for (i = 0; i < lowpass_height; i++) {
516 for (j = 0; j < lowpass_width; j++)
517 coeff_data[j] = bytestream2_get_be16u(&gb);
519 coeff_data += lowpass_width;
522 /* Align to mod-4 position to continue reading tags */
523 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
525 /* Copy last line of coefficients if odd height */
526 if (lowpass_height & 1) {
527 memcpy(&coeff_data[lowpass_height * lowpass_width],
528 &coeff_data[(lowpass_height - 1) * lowpass_width],
529 lowpass_width * sizeof(*coeff_data));
532 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
535 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
536 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
537 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
538 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
539 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
540 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
542 int a_expected = highpass_a_height * highpass_a_width;
543 int level, run, coeff;
544 int count = 0, bytes;
547 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
548 ret = AVERROR(EINVAL);
552 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
553 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
554 ret = AVERROR(EINVAL);
557 expected = highpass_height * highpass_stride;
559 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);
561 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
563 OPEN_READER(re, &s->gb);
566 UPDATE_CACHE(re, &s->gb);
567 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
576 if (count > expected)
579 coeff = dequant_and_decompand(level, s->quantisation);
580 for (i = 0; i < run; i++)
581 *coeff_data++ = coeff;
585 UPDATE_CACHE(re, &s->gb);
586 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
590 if (level == 255 && run == 2)
595 if (count > expected)
598 coeff = dequant_and_decompand(level, s->quantisation);
599 for (i = 0; i < run; i++)
600 *coeff_data++ = coeff;
603 CLOSE_READER(re, &s->gb);
606 if (count > expected) {
607 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
608 ret = AVERROR(EINVAL);
612 bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
613 if (bytes > bytestream2_get_bytes_left(&gb)) {
614 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
615 ret = AVERROR(EINVAL);
618 bytestream2_seek(&gb, bytes, SEEK_CUR);
620 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
623 /* Copy last line of coefficients if odd height */
624 if (highpass_height & 1) {
625 memcpy(&coeff_data[highpass_height * highpass_stride],
626 &coeff_data[(highpass_height - 1) * highpass_stride],
627 highpass_stride * sizeof(*coeff_data));
632 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
633 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
634 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
635 ret = AVERROR(EINVAL);
640 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
641 ret = AVERROR(EINVAL);
645 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
646 for (plane = 0; plane < planes && !ret; plane++) {
648 int lowpass_height = s->plane[plane].band[0][0].height;
649 int lowpass_width = s->plane[plane].band[0][0].width;
650 int highpass_stride = s->plane[plane].band[0][1].stride;
651 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
652 int16_t *low, *high, *output, *dst;
654 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
655 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
656 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
657 ret = AVERROR(EINVAL);
661 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
663 low = s->plane[plane].subband[0];
664 high = s->plane[plane].subband[2];
665 output = s->plane[plane].l_h[0];
666 for (i = 0; i < lowpass_width; i++) {
667 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
673 low = s->plane[plane].subband[1];
674 high = s->plane[plane].subband[3];
675 output = s->plane[plane].l_h[1];
677 for (i = 0; i < lowpass_width; i++) {
678 // note the stride of "low" is highpass_stride
679 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
685 low = s->plane[plane].l_h[0];
686 high = s->plane[plane].l_h[1];
687 output = s->plane[plane].subband[0];
688 for (i = 0; i < lowpass_height * 2; i++) {
689 horiz_filter(output, low, high, lowpass_width);
690 low += lowpass_width;
691 high += lowpass_width;
692 output += lowpass_width * 2;
695 output = s->plane[plane].subband[0];
696 for (i = 0; i < lowpass_height * 2; i++) {
697 for (j = 0; j < lowpass_width * 2; j++)
700 output += lowpass_width * 2;
705 lowpass_height = s->plane[plane].band[1][1].height;
706 lowpass_width = s->plane[plane].band[1][1].width;
707 highpass_stride = s->plane[plane].band[1][1].stride;
709 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
710 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
711 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
712 ret = AVERROR(EINVAL);
716 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
718 low = s->plane[plane].subband[0];
719 high = s->plane[plane].subband[5];
720 output = s->plane[plane].l_h[3];
721 for (i = 0; i < lowpass_width; i++) {
722 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
728 low = s->plane[plane].subband[4];
729 high = s->plane[plane].subband[6];
730 output = s->plane[plane].l_h[4];
731 for (i = 0; i < lowpass_width; i++) {
732 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
738 low = s->plane[plane].l_h[3];
739 high = s->plane[plane].l_h[4];
740 output = s->plane[plane].subband[0];
741 for (i = 0; i < lowpass_height * 2; i++) {
742 horiz_filter(output, low, high, lowpass_width);
743 low += lowpass_width;
744 high += lowpass_width;
745 output += lowpass_width * 2;
748 output = s->plane[plane].subband[0];
749 for (i = 0; i < lowpass_height * 2; i++) {
750 for (j = 0; j < lowpass_width * 2; j++)
753 output += lowpass_width * 2;
757 lowpass_height = s->plane[plane].band[2][1].height;
758 lowpass_width = s->plane[plane].band[2][1].width;
759 highpass_stride = s->plane[plane].band[2][1].stride;
761 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
762 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
763 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
764 ret = AVERROR(EINVAL);
768 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
770 low = s->plane[plane].subband[0];
771 high = s->plane[plane].subband[8];
772 output = s->plane[plane].l_h[6];
773 for (i = 0; i < lowpass_width; i++) {
774 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
780 low = s->plane[plane].subband[7];
781 high = s->plane[plane].subband[9];
782 output = s->plane[plane].l_h[7];
783 for (i = 0; i < lowpass_width; i++) {
784 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
790 dst = (int16_t *)pic->data[act_plane];
791 low = s->plane[plane].l_h[6];
792 high = s->plane[plane].l_h[7];
793 for (i = 0; i < lowpass_height * 2; i++) {
794 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
795 low += lowpass_width;
796 high += lowpass_width;
797 dst += pic->linesize[act_plane] / 2;
810 static av_cold int cfhd_close(AVCodecContext *avctx)
812 CFHDContext *s = avctx->priv_data;
816 if (!avctx->internal->is_copy) {
817 ff_free_vlc(&s->vlc_9);
818 ff_free_vlc(&s->vlc_18);
824 AVCodec ff_cfhd_decoder = {
826 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
827 .type = AVMEDIA_TYPE_VIDEO,
828 .id = AV_CODEC_ID_CFHD,
829 .priv_data_size = sizeof(CFHDContext),
832 .decode = cfhd_decode,
833 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
834 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,