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;
216 height = FFALIGN(height / 8, 2) * 8;
217 s->plane[i].width = width;
218 s->plane[i].height = height;
219 s->plane[i].stride = stride;
221 w8 = FFALIGN(s->plane[i].width / 8, 8);
222 h8 = FFALIGN(s->plane[i].height / 8, 2);
228 s->plane[i].idwt_buf =
229 av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
230 s->plane[i].idwt_tmp =
231 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
232 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
233 return AVERROR(ENOMEM);
235 s->plane[i].subband[0] = s->plane[i].idwt_buf;
236 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
237 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
238 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
239 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
240 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
241 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
242 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
243 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
244 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
246 for (j = 0; j < DWT_LEVELS; j++) {
247 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
248 s->plane[i].band[j][k].a_width = w8 << j;
249 s->plane[i].band[j][k].a_height = h8 << j;
253 /* ll2 and ll1 commented out because they are done in-place */
254 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
255 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
256 // s->plane[i].l_h[2] = ll2;
257 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
258 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
259 // s->plane[i].l_h[5] = ll1;
260 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
261 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
264 s->a_height = s->coded_height;
265 s->a_width = s->coded_width;
266 s->a_format = s->coded_format;
271 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
274 CFHDContext *s = avctx->priv_data;
276 ThreadFrame frame = { .f = data };
278 int ret = 0, i, j, planes, plane, got_buffer = 0;
281 s->coded_format = AV_PIX_FMT_YUV422P10;
282 init_frame_defaults(s);
283 planes = av_pix_fmt_count_planes(s->coded_format);
285 bytestream2_init(&gb, avpkt->data, avpkt->size);
287 while (bytestream2_get_bytes_left(&gb) > 4) {
288 /* Bit weird but implement the tag parsing as the spec says */
289 uint16_t tagu = bytestream2_get_be16(&gb);
290 int16_t tag = (int16_t)tagu;
291 int8_t tag8 = (int8_t)(tagu >> 8);
292 uint16_t abstag = abs(tag);
293 int8_t abs_tag8 = abs(tag8);
294 uint16_t data = bytestream2_get_be16(&gb);
295 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
296 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
297 } else if (tag == ImageWidth) {
298 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
299 s->coded_width = data;
300 } else if (tag == ImageHeight) {
301 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
302 s->coded_height = data;
303 } else if (tag == 101) {
304 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
305 if (data < 1 || data > 31) {
306 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
307 ret = AVERROR(EINVAL);
311 } else if (tag == ChannelCount) {
312 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
313 s->channel_cnt = data;
315 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
316 ret = AVERROR_PATCHWELCOME;
319 } else if (tag == SubbandCount) {
320 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
321 if (data != SUBBAND_COUNT) {
322 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
323 ret = AVERROR_PATCHWELCOME;
326 } else if (tag == ChannelNumber) {
327 s->channel_num = data;
328 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
329 if (s->channel_num >= planes) {
330 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
331 ret = AVERROR(EINVAL);
334 init_plane_defaults(s);
335 } else if (tag == SubbandNumber) {
336 if (s->subband_num != 0 && data == 1) // hack
338 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
339 s->subband_num = data;
340 if (s->level >= DWT_LEVELS) {
341 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
342 ret = AVERROR(EINVAL);
345 if (s->subband_num > 3) {
346 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
347 ret = AVERROR(EINVAL);
350 } else if (tag == 51) {
351 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
352 s->subband_num_actual = data;
353 if (s->subband_num_actual >= 10) {
354 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
355 ret = AVERROR(EINVAL);
358 } else if (tag == LowpassPrecision)
359 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
360 else if (tag == Quantization) {
361 s->quantisation = data;
362 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
363 } else if (tag == PrescaleShift) {
364 s->prescale_shift[0] = (data >> 0) & 0x7;
365 s->prescale_shift[1] = (data >> 3) & 0x7;
366 s->prescale_shift[2] = (data >> 6) & 0x7;
367 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
368 } else if (tag == 27) {
369 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
370 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
371 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
372 ret = AVERROR(EINVAL);
375 s->plane[s->channel_num].band[0][0].width = data;
376 s->plane[s->channel_num].band[0][0].stride = data;
377 } else if (tag == 28) {
378 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
379 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
380 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
381 ret = AVERROR(EINVAL);
384 s->plane[s->channel_num].band[0][0].height = data;
386 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
387 else if (tag == 10) {
389 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
390 ret = AVERROR_PATCHWELCOME;
393 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
394 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
395 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
396 bytestream2_skipu(&gb, data * 4);
397 } else if (tag == 23) {
398 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
399 avpriv_report_missing_feature(avctx, "Skip frame");
400 ret = AVERROR_PATCHWELCOME;
402 } else if (tag == 2) {
403 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
404 if (data > bytestream2_get_bytes_left(&gb) / 4) {
405 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
406 ret = AVERROR_INVALIDDATA;
409 for (i = 0; i < data; i++) {
410 uint16_t tag2 = bytestream2_get_be16(&gb);
411 uint16_t val2 = bytestream2_get_be16(&gb);
412 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
414 } else if (tag == 41) {
415 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);
417 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\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 == 42) {
424 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
426 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
427 ret = AVERROR(EINVAL);
430 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
431 } else if (tag == 49) {
432 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
434 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
435 ret = AVERROR(EINVAL);
438 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
439 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
440 } else if (tag == 50) {
441 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
443 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
444 ret = AVERROR(EINVAL);
447 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
448 } else if (tag == 71) {
450 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
451 } else if (tag == 72) {
453 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
454 } else if (tag == 70) {
455 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
456 if (!(data == 10 || data == 12)) {
457 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
458 ret = AVERROR(EINVAL);
462 } else if (tag == 84) {
463 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
465 s->coded_format = AV_PIX_FMT_YUV422P10;
467 s->coded_format = AV_PIX_FMT_GBRP12;
469 s->coded_format = AV_PIX_FMT_GBRAP12;
471 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
472 ret = AVERROR_PATCHWELCOME;
475 planes = av_pix_fmt_count_planes(s->coded_format);
476 } else if (tag == -85) {
477 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
478 s->cropped_height = data;
480 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
482 /* Some kind of end of header tag */
483 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
484 s->coded_format != AV_PIX_FMT_NONE) {
485 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
486 s->a_format != s->coded_format) {
488 if ((ret = alloc_buffers(avctx)) < 0) {
493 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
496 if (s->cropped_height)
497 avctx->height = s->cropped_height;
501 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
506 s->coded_format = AV_PIX_FMT_NONE;
509 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
511 /* Lowpass coefficients */
512 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
513 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
514 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
515 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
516 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
519 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
520 ret = AVERROR(EINVAL);
524 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
525 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
526 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
527 ret = AVERROR(EINVAL);
531 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
532 for (i = 0; i < lowpass_height; i++) {
533 for (j = 0; j < lowpass_width; j++)
534 coeff_data[j] = bytestream2_get_be16u(&gb);
536 coeff_data += lowpass_width;
539 /* Align to mod-4 position to continue reading tags */
540 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
542 /* Copy last line of coefficients if odd height */
543 if (lowpass_height & 1) {
544 memcpy(&coeff_data[lowpass_height * lowpass_width],
545 &coeff_data[(lowpass_height - 1) * lowpass_width],
546 lowpass_width * sizeof(*coeff_data));
549 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
552 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
553 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
554 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
555 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
556 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
557 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
559 int a_expected = highpass_a_height * highpass_a_width;
560 int level, run, coeff;
561 int count = 0, bytes;
564 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
565 ret = AVERROR(EINVAL);
569 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
570 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
571 ret = AVERROR(EINVAL);
574 expected = highpass_height * highpass_stride;
576 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);
578 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
580 OPEN_READER(re, &s->gb);
583 UPDATE_CACHE(re, &s->gb);
584 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
593 if (count > expected)
596 coeff = dequant_and_decompand(level, s->quantisation);
597 for (i = 0; i < run; i++)
598 *coeff_data++ = coeff;
602 UPDATE_CACHE(re, &s->gb);
603 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
607 if (level == 255 && run == 2)
612 if (count > expected)
615 coeff = dequant_and_decompand(level, s->quantisation);
616 for (i = 0; i < run; i++)
617 *coeff_data++ = coeff;
620 CLOSE_READER(re, &s->gb);
623 if (count > expected) {
624 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
625 ret = AVERROR(EINVAL);
629 bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
630 if (bytes > bytestream2_get_bytes_left(&gb)) {
631 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
632 ret = AVERROR(EINVAL);
635 bytestream2_seek(&gb, bytes, SEEK_CUR);
637 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
640 /* Copy last line of coefficients if odd height */
641 if (highpass_height & 1) {
642 memcpy(&coeff_data[highpass_height * highpass_stride],
643 &coeff_data[(highpass_height - 1) * highpass_stride],
644 highpass_stride * sizeof(*coeff_data));
649 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
650 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
651 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
652 ret = AVERROR(EINVAL);
657 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
658 ret = AVERROR(EINVAL);
662 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
663 for (plane = 0; plane < planes && !ret; plane++) {
665 int lowpass_height = s->plane[plane].band[0][0].height;
666 int lowpass_width = s->plane[plane].band[0][0].width;
667 int highpass_stride = s->plane[plane].band[0][1].stride;
668 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
669 int16_t *low, *high, *output, *dst;
671 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
672 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
673 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
674 ret = AVERROR(EINVAL);
678 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
680 low = s->plane[plane].subband[0];
681 high = s->plane[plane].subband[2];
682 output = s->plane[plane].l_h[0];
683 for (i = 0; i < lowpass_width; i++) {
684 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
690 low = s->plane[plane].subband[1];
691 high = s->plane[plane].subband[3];
692 output = s->plane[plane].l_h[1];
694 for (i = 0; i < lowpass_width; i++) {
695 // note the stride of "low" is highpass_stride
696 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
702 low = s->plane[plane].l_h[0];
703 high = s->plane[plane].l_h[1];
704 output = s->plane[plane].subband[0];
705 for (i = 0; i < lowpass_height * 2; i++) {
706 horiz_filter(output, low, high, lowpass_width);
707 low += lowpass_width;
708 high += lowpass_width;
709 output += lowpass_width * 2;
712 output = s->plane[plane].subband[0];
713 for (i = 0; i < lowpass_height * 2; i++) {
714 for (j = 0; j < lowpass_width * 2; j++)
717 output += lowpass_width * 2;
722 lowpass_height = s->plane[plane].band[1][1].height;
723 lowpass_width = s->plane[plane].band[1][1].width;
724 highpass_stride = s->plane[plane].band[1][1].stride;
726 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
727 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
728 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
729 ret = AVERROR(EINVAL);
733 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
735 low = s->plane[plane].subband[0];
736 high = s->plane[plane].subband[5];
737 output = s->plane[plane].l_h[3];
738 for (i = 0; i < lowpass_width; i++) {
739 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
745 low = s->plane[plane].subband[4];
746 high = s->plane[plane].subband[6];
747 output = s->plane[plane].l_h[4];
748 for (i = 0; i < lowpass_width; i++) {
749 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
755 low = s->plane[plane].l_h[3];
756 high = s->plane[plane].l_h[4];
757 output = s->plane[plane].subband[0];
758 for (i = 0; i < lowpass_height * 2; i++) {
759 horiz_filter(output, low, high, lowpass_width);
760 low += lowpass_width;
761 high += lowpass_width;
762 output += lowpass_width * 2;
765 output = s->plane[plane].subband[0];
766 for (i = 0; i < lowpass_height * 2; i++) {
767 for (j = 0; j < lowpass_width * 2; j++)
770 output += lowpass_width * 2;
774 lowpass_height = s->plane[plane].band[2][1].height;
775 lowpass_width = s->plane[plane].band[2][1].width;
776 highpass_stride = s->plane[plane].band[2][1].stride;
778 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
779 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
780 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
781 ret = AVERROR(EINVAL);
785 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
787 low = s->plane[plane].subband[0];
788 high = s->plane[plane].subband[8];
789 output = s->plane[plane].l_h[6];
790 for (i = 0; i < lowpass_width; i++) {
791 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
797 low = s->plane[plane].subband[7];
798 high = s->plane[plane].subband[9];
799 output = s->plane[plane].l_h[7];
800 for (i = 0; i < lowpass_width; i++) {
801 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
807 dst = (int16_t *)pic->data[act_plane];
808 low = s->plane[plane].l_h[6];
809 high = s->plane[plane].l_h[7];
810 for (i = 0; i < lowpass_height * 2; i++) {
811 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
813 process_alpha(dst, lowpass_width * 2);
814 low += lowpass_width;
815 high += lowpass_width;
816 dst += pic->linesize[act_plane] / 2;
829 static av_cold int cfhd_close(AVCodecContext *avctx)
831 CFHDContext *s = avctx->priv_data;
835 if (!avctx->internal->is_copy) {
836 ff_free_vlc(&s->vlc_9);
837 ff_free_vlc(&s->vlc_18);
843 AVCodec ff_cfhd_decoder = {
845 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
846 .type = AVMEDIA_TYPE_VIDEO,
847 .id = AV_CODEC_ID_CFHD,
848 .priv_data_size = sizeof(CFHDContext),
851 .decode = cfhd_decode,
852 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
853 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,