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,
53 BitsPerComponent = 101,
61 static av_cold int cfhd_init(AVCodecContext *avctx)
63 CFHDContext *s = avctx->priv_data;
65 avctx->bits_per_raw_sample = 10;
68 return ff_cfhd_init_vlcs(s);
71 static void init_plane_defaults(CFHDContext *s)
75 s->subband_num_actual = 0;
78 static void init_peak_table_defaults(CFHDContext *s)
82 memset(&s->peak.base, 0, sizeof(s->peak.base));
85 static void init_frame_defaults(CFHDContext *s)
89 s->cropped_height = 0;
92 s->subband_cnt = SUBBAND_COUNT;
94 s->lowpass_precision = 16;
99 s->difference_coding = 0;
101 init_plane_defaults(s);
102 init_peak_table_defaults(s);
105 /* TODO: merge with VLC tables or use LUT */
106 static inline int dequant_and_decompand(int level, int quantisation, int codebook)
108 if (codebook == 0 || codebook == 1) {
109 int64_t abslevel = abs(level);
111 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
112 FFSIGN(level) * quantisation;
114 return level * quantisation;
116 return level * quantisation;
119 static inline void difference_coding(int16_t *band, int width, int height)
123 for (i = 0; i < height; i++) {
124 for (j = 1; j < width; j++) {
125 band[j] += band[j-1];
131 static inline void peak_table(int16_t *band, Peak *peak, int length)
134 for (i = 0; i < length; i++)
135 if (abs(band[i]) > peak->level)
136 band[i] = bytestream2_get_le16(&peak->base);
139 static inline void process_alpha(int16_t *alpha, int width)
142 for (i = 0; i < width; i++) {
144 channel -= ALPHA_COMPAND_DC_OFFSET;
146 channel *= ALPHA_COMPAND_GAIN;
148 channel = av_clip_uintp2(channel, 12);
153 static inline void filter(int16_t *output, ptrdiff_t out_stride,
154 int16_t *low, ptrdiff_t low_stride,
155 int16_t *high, ptrdiff_t high_stride,
161 for (i = 0; i < len; i++) {
163 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
164 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
166 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
168 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
169 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
171 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
172 } else if (i == len-1) {
173 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
174 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
176 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
178 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
179 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
181 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
183 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
184 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
186 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
188 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
189 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
191 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
196 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
197 int width, int linesize, int plane)
201 for (i = 0; i < width; i++) {
202 even = (low[i] - high[i])/2;
203 odd = (low[i] + high[i])/2;
204 output[i] = av_clip_uintp2(even, 10);
205 output[i + linesize] = av_clip_uintp2(odd, 10);
208 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
211 filter(output, 1, low, 1, high, 1, width, 0);
214 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
217 filter(output, 1, low, 1, high, 1, width, clip);
220 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
221 int16_t *low, ptrdiff_t low_stride,
222 int16_t *high, ptrdiff_t high_stride, int len)
224 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
227 static void free_buffers(CFHDContext *s)
231 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
232 av_freep(&s->plane[i].idwt_buf);
233 av_freep(&s->plane[i].idwt_tmp);
235 for (j = 0; j < 9; j++)
236 s->plane[i].subband[j] = NULL;
238 for (j = 0; j < 8; j++)
239 s->plane[i].l_h[j] = NULL;
245 static int alloc_buffers(AVCodecContext *avctx)
247 CFHDContext *s = avctx->priv_data;
248 int i, j, ret, planes;
249 int chroma_x_shift, chroma_y_shift;
252 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
254 avctx->pix_fmt = s->coded_format;
256 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
258 &chroma_y_shift)) < 0)
260 planes = av_pix_fmt_count_planes(s->coded_format);
262 for (i = 0; i < planes; i++) {
263 int w8, h8, w4, h4, w2, h2;
264 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
265 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
266 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
268 height = FFALIGN(height / 8, 2) * 8;
269 s->plane[i].width = width;
270 s->plane[i].height = height;
271 s->plane[i].stride = stride;
273 w8 = FFALIGN(s->plane[i].width / 8, 8);
280 s->plane[i].idwt_buf =
281 av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
282 s->plane[i].idwt_tmp =
283 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
284 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
285 return AVERROR(ENOMEM);
287 s->plane[i].subband[0] = s->plane[i].idwt_buf;
288 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
289 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
290 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
291 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
292 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
293 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
294 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
295 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
296 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
298 for (j = 0; j < DWT_LEVELS; j++) {
299 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
300 s->plane[i].band[j][k].a_width = w8 << j;
301 s->plane[i].band[j][k].a_height = h8 << j;
305 /* ll2 and ll1 commented out because they are done in-place */
306 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
307 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
308 // s->plane[i].l_h[2] = ll2;
309 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
310 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
311 // s->plane[i].l_h[5] = ll1;
312 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
313 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
316 s->a_height = s->coded_height;
317 s->a_width = s->coded_width;
318 s->a_format = s->coded_format;
323 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
326 CFHDContext *s = avctx->priv_data;
328 ThreadFrame frame = { .f = data };
330 int ret = 0, i, j, planes, plane, got_buffer = 0;
333 s->coded_format = AV_PIX_FMT_YUV422P10;
334 init_frame_defaults(s);
335 planes = av_pix_fmt_count_planes(s->coded_format);
337 bytestream2_init(&gb, avpkt->data, avpkt->size);
339 while (bytestream2_get_bytes_left(&gb) > 4) {
340 /* Bit weird but implement the tag parsing as the spec says */
341 uint16_t tagu = bytestream2_get_be16(&gb);
342 int16_t tag = (int16_t)tagu;
343 int8_t tag8 = (int8_t)(tagu >> 8);
344 uint16_t abstag = abs(tag);
345 int8_t abs_tag8 = abs(tag8);
346 uint16_t data = bytestream2_get_be16(&gb);
347 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
348 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
349 } else if (tag == SampleFlags) {
350 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
351 s->progressive = data & 0x0001;
352 } else if (tag == ImageWidth) {
353 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
354 s->coded_width = data;
355 } else if (tag == ImageHeight) {
356 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
357 s->coded_height = data;
358 } else if (tag == 101) {
359 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
360 if (data < 1 || data > 31) {
361 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
362 ret = AVERROR(EINVAL);
366 } else if (tag == ChannelCount) {
367 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
368 s->channel_cnt = data;
370 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
371 ret = AVERROR_PATCHWELCOME;
374 } else if (tag == SubbandCount) {
375 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
376 if (data != SUBBAND_COUNT) {
377 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
378 ret = AVERROR_PATCHWELCOME;
381 } else if (tag == ChannelNumber) {
382 s->channel_num = data;
383 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
384 if (s->channel_num >= planes) {
385 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
386 ret = AVERROR(EINVAL);
389 init_plane_defaults(s);
390 } else if (tag == SubbandNumber) {
391 if (s->subband_num != 0 && data == 1) // hack
393 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
394 s->subband_num = data;
395 if (s->level >= DWT_LEVELS) {
396 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
397 ret = AVERROR(EINVAL);
400 if (s->subband_num > 3) {
401 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
402 ret = AVERROR(EINVAL);
405 } else if (tag == 51) {
406 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
407 s->subband_num_actual = data;
408 if (s->subband_num_actual >= 10) {
409 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
410 ret = AVERROR(EINVAL);
413 } else if (tag == LowpassPrecision)
414 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
415 else if (tag == Quantization) {
416 s->quantisation = data;
417 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
418 } else if (tag == PrescaleShift) {
419 s->prescale_shift[0] = (data >> 0) & 0x7;
420 s->prescale_shift[1] = (data >> 3) & 0x7;
421 s->prescale_shift[2] = (data >> 6) & 0x7;
422 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
423 } else if (tag == 27) {
424 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
425 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
426 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
427 ret = AVERROR(EINVAL);
430 s->plane[s->channel_num].band[0][0].width = data;
431 s->plane[s->channel_num].band[0][0].stride = data;
432 } else if (tag == 28) {
433 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
434 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
435 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
436 ret = AVERROR(EINVAL);
439 s->plane[s->channel_num].band[0][0].height = data;
441 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
442 else if (tag == 10) {
444 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
445 ret = AVERROR_PATCHWELCOME;
448 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
449 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
450 if (abstag == 0x4001)
452 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
453 bytestream2_skipu(&gb, data * 4);
454 } else if (tag == 23) {
455 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
456 avpriv_report_missing_feature(avctx, "Skip frame");
457 ret = AVERROR_PATCHWELCOME;
459 } else if (tag == 2) {
460 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
461 if (data > bytestream2_get_bytes_left(&gb) / 4) {
462 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
463 ret = AVERROR_INVALIDDATA;
466 for (i = 0; i < data; i++) {
467 uint16_t tag2 = bytestream2_get_be16(&gb);
468 uint16_t val2 = bytestream2_get_be16(&gb);
469 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
471 } else if (tag == 41) {
472 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);
474 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
475 ret = AVERROR(EINVAL);
478 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
479 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
480 } else if (tag == 42) {
481 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
483 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
484 ret = AVERROR(EINVAL);
487 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
488 } else if (tag == 49) {
489 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
491 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
492 ret = AVERROR(EINVAL);
495 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
496 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
497 } else if (tag == 50) {
498 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
500 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
501 ret = AVERROR(EINVAL);
504 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
505 } else if (tag == 71) {
507 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
508 } else if (tag == 72) {
509 s->codebook = data & 0xf;
510 s->difference_coding = (data >> 4) & 1;
511 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
512 } else if (tag == 70) {
513 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
514 if (!(data == 10 || data == 12)) {
515 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
516 ret = AVERROR(EINVAL);
520 } else if (tag == 84) {
521 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
523 s->coded_format = AV_PIX_FMT_YUV422P10;
525 s->coded_format = AV_PIX_FMT_GBRP12;
527 s->coded_format = AV_PIX_FMT_GBRAP12;
529 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
530 ret = AVERROR_PATCHWELCOME;
533 planes = av_pix_fmt_count_planes(s->coded_format);
534 } else if (tag == -85) {
535 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
536 s->cropped_height = data;
537 } else if (tag == -75) {
538 s->peak.offset &= ~0xffff;
539 s->peak.offset |= (data & 0xffff);
542 } else if (tag == -76) {
543 s->peak.offset &= 0xffff;
544 s->peak.offset |= (data & 0xffffU)<<16;
547 } else if (tag == -74 && s->peak.offset) {
548 s->peak.level = data;
549 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
551 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
553 /* Some kind of end of header tag */
554 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
555 s->coded_format != AV_PIX_FMT_NONE) {
556 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
557 s->a_format != s->coded_format) {
559 if ((ret = alloc_buffers(avctx)) < 0) {
564 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
567 if (s->cropped_height)
568 avctx->height = s->cropped_height;
572 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
577 s->coded_format = AV_PIX_FMT_NONE;
580 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
582 /* Lowpass coefficients */
583 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
584 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
585 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
586 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
587 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
590 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
591 ret = AVERROR(EINVAL);
595 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
596 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
597 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
598 ret = AVERROR(EINVAL);
602 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
603 for (i = 0; i < lowpass_height; i++) {
604 for (j = 0; j < lowpass_width; j++)
605 coeff_data[j] = bytestream2_get_be16u(&gb);
607 coeff_data += lowpass_width;
610 /* Align to mod-4 position to continue reading tags */
611 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
613 /* Copy last line of coefficients if odd height */
614 if (lowpass_height & 1) {
615 memcpy(&coeff_data[lowpass_height * lowpass_width],
616 &coeff_data[(lowpass_height - 1) * lowpass_width],
617 lowpass_width * sizeof(*coeff_data));
620 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
623 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
624 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
625 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
626 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
627 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
628 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
630 int a_expected = highpass_a_height * highpass_a_width;
631 int level, run, coeff;
632 int count = 0, bytes;
635 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
636 ret = AVERROR(EINVAL);
640 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
641 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
642 ret = AVERROR(EINVAL);
645 expected = highpass_height * highpass_stride;
647 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);
649 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
651 OPEN_READER(re, &s->gb);
654 UPDATE_CACHE(re, &s->gb);
655 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
664 if (count > expected)
667 coeff = dequant_and_decompand(level, s->quantisation, 0);
668 for (i = 0; i < run; i++)
669 *coeff_data++ = coeff;
673 UPDATE_CACHE(re, &s->gb);
674 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
678 if (level == 255 && run == 2)
683 if (count > expected)
686 coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
687 for (i = 0; i < run; i++)
688 *coeff_data++ = coeff;
691 CLOSE_READER(re, &s->gb);
694 if (count > expected) {
695 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
696 ret = AVERROR(EINVAL);
700 peak_table(coeff_data - count, &s->peak, count);
701 if (s->difference_coding)
702 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
704 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
705 if (bytes > bytestream2_get_bytes_left(&gb)) {
706 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
707 ret = AVERROR(EINVAL);
710 bytestream2_seek(&gb, bytes, SEEK_CUR);
712 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
715 /* Copy last line of coefficients if odd height */
716 if (highpass_height & 1) {
717 memcpy(&coeff_data[highpass_height * highpass_stride],
718 &coeff_data[(highpass_height - 1) * highpass_stride],
719 highpass_stride * sizeof(*coeff_data));
724 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
725 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
726 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
727 ret = AVERROR(EINVAL);
732 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
733 ret = AVERROR(EINVAL);
737 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
738 for (plane = 0; plane < planes && !ret; plane++) {
740 int lowpass_height = s->plane[plane].band[0][0].height;
741 int lowpass_width = s->plane[plane].band[0][0].width;
742 int highpass_stride = s->plane[plane].band[0][1].stride;
743 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
744 int16_t *low, *high, *output, *dst;
746 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
747 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
748 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
749 ret = AVERROR(EINVAL);
753 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
755 low = s->plane[plane].subband[0];
756 high = s->plane[plane].subband[2];
757 output = s->plane[plane].l_h[0];
758 for (i = 0; i < lowpass_width; i++) {
759 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
765 low = s->plane[plane].subband[1];
766 high = s->plane[plane].subband[3];
767 output = s->plane[plane].l_h[1];
769 for (i = 0; i < lowpass_width; i++) {
770 // note the stride of "low" is highpass_stride
771 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
777 low = s->plane[plane].l_h[0];
778 high = s->plane[plane].l_h[1];
779 output = s->plane[plane].subband[0];
780 for (i = 0; i < lowpass_height * 2; i++) {
781 horiz_filter(output, low, high, lowpass_width);
782 low += lowpass_width;
783 high += lowpass_width;
784 output += lowpass_width * 2;
787 output = s->plane[plane].subband[0];
788 for (i = 0; i < lowpass_height * 2; i++) {
789 for (j = 0; j < lowpass_width * 2; j++)
792 output += lowpass_width * 2;
797 lowpass_height = s->plane[plane].band[1][1].height;
798 lowpass_width = s->plane[plane].band[1][1].width;
799 highpass_stride = s->plane[plane].band[1][1].stride;
801 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
802 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
803 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
804 ret = AVERROR(EINVAL);
808 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
810 low = s->plane[plane].subband[0];
811 high = s->plane[plane].subband[5];
812 output = s->plane[plane].l_h[3];
813 for (i = 0; i < lowpass_width; i++) {
814 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
820 low = s->plane[plane].subband[4];
821 high = s->plane[plane].subband[6];
822 output = s->plane[plane].l_h[4];
823 for (i = 0; i < lowpass_width; i++) {
824 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
830 low = s->plane[plane].l_h[3];
831 high = s->plane[plane].l_h[4];
832 output = s->plane[plane].subband[0];
833 for (i = 0; i < lowpass_height * 2; i++) {
834 horiz_filter(output, low, high, lowpass_width);
835 low += lowpass_width;
836 high += lowpass_width;
837 output += lowpass_width * 2;
840 output = s->plane[plane].subband[0];
841 for (i = 0; i < lowpass_height * 2; i++) {
842 for (j = 0; j < lowpass_width * 2; j++)
845 output += lowpass_width * 2;
849 lowpass_height = s->plane[plane].band[2][1].height;
850 lowpass_width = s->plane[plane].band[2][1].width;
851 highpass_stride = s->plane[plane].band[2][1].stride;
853 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
854 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
855 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
856 ret = AVERROR(EINVAL);
860 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
861 if (s->progressive) {
862 low = s->plane[plane].subband[0];
863 high = s->plane[plane].subband[8];
864 output = s->plane[plane].l_h[6];
865 for (i = 0; i < lowpass_width; i++) {
866 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
872 low = s->plane[plane].subband[7];
873 high = s->plane[plane].subband[9];
874 output = s->plane[plane].l_h[7];
875 for (i = 0; i < lowpass_width; i++) {
876 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
882 dst = (int16_t *)pic->data[act_plane];
883 low = s->plane[plane].l_h[6];
884 high = s->plane[plane].l_h[7];
885 for (i = 0; i < lowpass_height * 2; i++) {
886 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
887 low += lowpass_width;
888 high += lowpass_width;
889 dst += pic->linesize[act_plane] / 2;
892 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
893 pic->interlaced_frame = 1;
894 low = s->plane[plane].subband[0];
895 high = s->plane[plane].subband[7];
896 output = s->plane[plane].l_h[6];
897 for (i = 0; i < lowpass_height; i++) {
898 horiz_filter(output, low, high, lowpass_width);
899 low += lowpass_width;
900 high += lowpass_width;
901 output += lowpass_width * 2;
904 low = s->plane[plane].subband[8];
905 high = s->plane[plane].subband[9];
906 output = s->plane[plane].l_h[7];
907 for (i = 0; i < lowpass_height; i++) {
908 horiz_filter(output, low, high, lowpass_width);
909 low += lowpass_width;
910 high += lowpass_width;
911 output += lowpass_width * 2;
914 dst = (int16_t *)pic->data[act_plane];
915 low = s->plane[plane].l_h[6];
916 high = s->plane[plane].l_h[7];
917 for (i = 0; i < lowpass_height; i++) {
918 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
919 low += lowpass_width * 2;
920 high += lowpass_width * 2;
921 dst += pic->linesize[act_plane];
935 static av_cold int cfhd_close(AVCodecContext *avctx)
937 CFHDContext *s = avctx->priv_data;
941 if (!avctx->internal->is_copy) {
942 ff_free_vlc(&s->vlc_9);
943 ff_free_vlc(&s->vlc_18);
949 AVCodec ff_cfhd_decoder = {
951 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
952 .type = AVMEDIA_TYPE_VIDEO,
953 .id = AV_CODEC_ID_CFHD,
954 .priv_data_size = sizeof(CFHDContext),
957 .decode = cfhd_decode,
958 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
959 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,