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
26 #include "libavutil/buffer.h"
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
34 #include "bytestream.h"
38 #define SUBBAND_COUNT 10
40 static av_cold int cfhd_decode_init(AVCodecContext *avctx)
42 CFHDContext *s = avctx->priv_data;
44 avctx->bits_per_raw_sample = 10;
47 return ff_cfhd_init_vlcs(s);
50 static void init_plane_defaults(CFHDContext *s)
54 s->subband_num_actual = 0;
57 static void init_frame_defaults(CFHDContext *s)
65 s->lowpass_precision = 16;
70 init_plane_defaults(s);
73 /* TODO: merge with VLC tables or use LUT */
74 static inline int dequant_and_decompand(int level, int quantisation)
76 int64_t abslevel = abs(level);
77 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) * FFSIGN(level) * quantisation;
80 static inline void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride,
81 int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
86 for (i = 0; i < len; i++) {
88 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
89 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
91 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
93 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
94 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
96 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
97 } else if (i == len-1) {
98 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
99 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
101 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
103 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
104 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
106 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
108 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
109 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
111 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
113 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
114 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
116 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
121 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high, int width)
123 filter(output, 1, low, 1, high, 1, width, 0);
126 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high, int width, uint8_t clip)
128 filter(output, 1, low, 1, high, 1, width, clip);
131 static void vert_filter(int16_t *output, int out_stride, int16_t *low, int low_stride,
132 int16_t *high, int high_stride, int len)
134 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
137 static void free_buffers(AVCodecContext *avctx)
139 CFHDContext *s = avctx->priv_data;
142 for (i = 0; i < 4; i++) {
143 av_freep(&s->plane[i].idwt_buf);
144 av_freep(&s->plane[i].idwt_tmp);
150 static int alloc_buffers(AVCodecContext *avctx)
152 CFHDContext *s = avctx->priv_data;
153 int i, j, k, ret, planes;
155 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
157 avctx->pix_fmt = s->coded_format;
159 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
160 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
162 for (i = 0; i < planes; i++) {
163 int width = i ? avctx->width >> s->chroma_x_shift : avctx->width;
164 int height = i ? avctx->height >> s->chroma_y_shift : avctx->height;
165 int stride = FFALIGN(width / 8, 8) * 8;
166 int w8, h8, w4, h4, w2, h2;
167 height = FFALIGN(height / 8, 2) * 8;
168 s->plane[i].width = width;
169 s->plane[i].height = height;
170 s->plane[i].stride = stride;
172 w8 = FFALIGN(s->plane[i].width / 8, 8);
173 h8 = FFALIGN(s->plane[i].height / 8, 2);
179 s->plane[i].idwt_buf = av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_buf));
180 s->plane[i].idwt_tmp = av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
181 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp) {
182 return AVERROR(ENOMEM);
185 s->plane[i].subband[0] = s->plane[i].idwt_buf;
186 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
187 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
188 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
189 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
190 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
191 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
192 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
193 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
194 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
196 for (j = 0; j < DWT_LEVELS; j++) {
197 for(k = 0; k < 4; k++) {
198 s->plane[i].band[j][k].a_width = w8 << j;
199 s->plane[i].band[j][k].a_height = h8 << j;
203 /* ll2 and ll1 commented out because they are done in-place */
204 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
205 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
206 //s->plane[i].l_h[2] = ll2;
207 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
208 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
209 //s->plane[i].l_h[5] = ll1;
210 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
211 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
214 s->a_height = s->coded_height;
215 s->a_width = s->coded_width;
216 s->a_format = s->coded_format;
221 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
224 CFHDContext *s = avctx->priv_data;
226 ThreadFrame frame = { .f = data };
228 int ret = 0, i, j, planes, plane, got_buffer = 0;
231 s->coded_format = AV_PIX_FMT_YUV422P10;
232 init_frame_defaults(s);
233 planes = av_pix_fmt_count_planes(s->coded_format);
235 bytestream2_init(&gb, avpkt->data, avpkt->size);
237 while (bytestream2_get_bytes_left(&gb) > 4) {
238 /* Bit weird but implement the tag parsing as the spec says */
239 uint16_t tagu = bytestream2_get_be16(&gb);
240 int16_t tag = (int16_t)tagu;
241 int8_t tag8 = (int8_t)(tagu >> 8);
242 uint16_t abstag = abs(tag);
243 int8_t abs_tag8 = abs(tag8);
244 uint16_t data = bytestream2_get_be16(&gb);
245 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
246 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
247 } else if (tag == 20) {
248 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
249 s->coded_width = data;
250 } else if (tag == 21) {
251 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
252 s->coded_height = data;
253 } else if (tag == 101) {
254 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
256 } else if (tag == 12) {
257 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
258 s->channel_cnt = data;
260 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
261 ret = AVERROR_PATCHWELCOME;
264 } else if (tag == 14) {
265 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
266 if (data != SUBBAND_COUNT) {
267 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
268 ret = AVERROR_PATCHWELCOME;
271 } else if (tag == 62) {
272 s->channel_num = data;
273 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
274 if (s->channel_num >= planes) {
275 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
276 ret = AVERROR(EINVAL);
279 init_plane_defaults(s);
280 } else if (tag == 48) {
281 if (s->subband_num != 0 && data == 1) // hack
283 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
284 s->subband_num = data;
285 if (s->level >= DWT_LEVELS) {
286 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
287 ret = AVERROR(EINVAL);
290 if (s->subband_num > 3) {
291 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
292 ret = AVERROR(EINVAL);
295 } else if (tag == 51) {
296 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
297 s->subband_num_actual = data;
298 if (s->subband_num_actual >= 10) {
299 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
300 ret = AVERROR(EINVAL);
303 } else if (tag == 35)
304 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
305 else if (tag == 53) {
306 s->quantisation = data;
307 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
308 } else if (tag == 109) {
309 s->prescale_shift[0] = (data >> 0) & 0x7;
310 s->prescale_shift[1] = (data >> 3) & 0x7;
311 s->prescale_shift[2] = (data >> 6) & 0x7;
312 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
313 } else if (tag == 27) {
314 s->plane[s->channel_num].band[0][0].width = data;
315 s->plane[s->channel_num].band[0][0].stride = data;
316 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
317 if (data < 2 || data > s->plane[s->channel_num].band[0][0].a_width) {
318 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
319 ret = AVERROR(EINVAL);
322 } else if (tag == 28) {
323 s->plane[s->channel_num].band[0][0].height = data;
324 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
325 if (data < 2 || data > s->plane[s->channel_num].band[0][0].height) {
326 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
327 ret = AVERROR(EINVAL);
331 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
332 else if (tag == 10) {
334 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
335 ret = AVERROR_PATCHWELCOME;
338 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
339 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
340 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %"PRIu16" %s\n", data * 4, tag < 0 ? "optional" : "required");
341 bytestream2_skipu(&gb, data * 4);
342 } else if (tag == 23) {
343 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
344 avpriv_report_missing_feature(avctx, "Skip frame");
345 ret = AVERROR_PATCHWELCOME;
347 } else if (tag == 2) {
348 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
349 if (data > bytestream2_get_bytes_left(&gb) / 4) {
350 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
351 ret = AVERROR_INVALIDDATA;
354 for (i = 0; i < data; i++) {
355 uint16_t tag2 = bytestream2_get_be16(&gb);
356 uint16_t val2 = bytestream2_get_be16(&gb);
357 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
359 } else if (tag == 41) {
360 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
361 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
362 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);
364 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
365 ret = AVERROR(EINVAL);
368 } else if (tag == 42) {
369 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
370 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
372 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
373 ret = AVERROR(EINVAL);
376 } else if (tag == 49) {
377 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
378 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
379 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
381 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
382 ret = AVERROR(EINVAL);
385 } else if (tag == 50) {
386 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
387 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
389 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
390 ret = AVERROR(EINVAL);
393 } else if (tag == 71) {
395 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
396 } else if (tag == 72) {
398 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
399 } else if (tag == 70) {
400 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
402 if (!(s->bpc == 10 || s->bpc == 12)) {
403 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
404 ret = AVERROR(EINVAL);
407 } else if (tag == 84) {
408 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
410 s->coded_format = AV_PIX_FMT_YUV422P10;
412 s->coded_format = AV_PIX_FMT_GBRP12;
414 s->coded_format = AV_PIX_FMT_GBRAP12;
416 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
417 ret = AVERROR_PATCHWELCOME;
420 planes = av_pix_fmt_count_planes(s->coded_format);
422 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
424 /* Some kind of end of header tag */
425 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
426 s->coded_format != AV_PIX_FMT_NONE) {
427 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
428 s->a_format != s->coded_format) {
430 if ((ret = alloc_buffers(avctx)) < 0) {
436 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
441 s->coded_format = AV_PIX_FMT_NONE;
444 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
446 /* Lowpass coefficients */
447 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
448 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
449 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
450 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
451 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
453 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
454 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
455 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
456 ret = AVERROR(EINVAL);
460 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %"PRIu16" height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
461 for (i = 0; i < lowpass_height; i++) {
462 for (j = 0; j < lowpass_width; j++)
463 coeff_data[j] = bytestream2_get_be16u(&gb);
465 coeff_data += lowpass_width;
468 /* Align to mod-4 position to continue reading tags */
469 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
471 /* Copy last line of coefficients if odd height */
472 if (lowpass_height & 1) {
473 memcpy(&coeff_data[lowpass_height * lowpass_width],
474 &coeff_data[(lowpass_height - 1) * lowpass_width],
475 lowpass_width * sizeof(*coeff_data));
478 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %"PRIu16"\n", lowpass_width * lowpass_height);
481 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
482 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
483 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
484 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
485 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
486 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
487 int expected = highpass_height * highpass_stride;
488 int a_expected = highpass_a_height * highpass_a_width;
489 int level, run, coeff;
490 int count = 0, bytes;
492 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
493 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficents\n");
494 ret = AVERROR(EINVAL);
498 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);
500 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
502 OPEN_READER(re, &s->gb);
505 UPDATE_CACHE(re, &s->gb);
506 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
515 if (count > expected)
518 coeff = dequant_and_decompand(level, s->quantisation);
519 for (i = 0; i < run; i++)
520 *coeff_data++ = coeff;
524 UPDATE_CACHE(re, &s->gb);
525 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
529 if (level == 255 && run == 2)
534 if (count > expected)
537 coeff = dequant_and_decompand(level, s->quantisation);
538 for (i = 0; i < run; i++)
539 *coeff_data++ = coeff;
542 CLOSE_READER(re, &s->gb);
545 if (count > expected) {
546 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
547 ret = AVERROR(EINVAL);
551 bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
552 if (bytes > bytestream2_get_bytes_left(&gb)) {
553 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
554 ret = AVERROR(EINVAL);
557 bytestream2_seek(&gb, bytes, SEEK_CUR);
559 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
562 /* Copy last line of coefficients if odd height */
563 if (highpass_height & 1) {
564 memcpy(&coeff_data[highpass_height * highpass_stride],
565 &coeff_data[(highpass_height - 1) * highpass_stride],
566 highpass_stride * sizeof(*coeff_data));
571 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
572 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
573 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
574 ret = AVERROR(EINVAL);
579 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
580 ret = AVERROR(EINVAL);
584 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
585 for (plane = 0; plane < planes && !ret; plane++) {
587 int lowpass_height = s->plane[plane].band[0][0].height;
588 int lowpass_width = s->plane[plane].band[0][0].width;
589 int highpass_stride = s->plane[plane].band[0][1].stride;
590 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
591 int16_t *low, *high, *output, *dst;
593 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
594 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
595 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
596 ret = AVERROR(EINVAL);
600 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
602 low = s->plane[plane].subband[0];
603 high = s->plane[plane].subband[2];
604 output = s->plane[plane].l_h[0];
605 for (i = 0; i < lowpass_width; i++) {
606 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
612 low = s->plane[plane].subband[1];
613 high = s->plane[plane].subband[3];
614 output = s->plane[plane].l_h[1];
616 for (i = 0; i < lowpass_width; i++) {
617 // note the stride of "low" is highpass_stride
618 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
624 low = s->plane[plane].l_h[0];
625 high = s->plane[plane].l_h[1];
626 output = s->plane[plane].subband[0];
627 for (i = 0; i < lowpass_height * 2; i++) {
628 horiz_filter(output, low, high, lowpass_width);
629 low += lowpass_width;
630 high += lowpass_width;
631 output += lowpass_width * 2;
634 output = s->plane[plane].subband[0];
635 for (i = 0; i < lowpass_height * 2; i++) {
636 for (j = 0; j < lowpass_width * 2; j++)
639 output += lowpass_width * 2;
644 lowpass_height = s->plane[plane].band[1][1].height;
645 lowpass_width = s->plane[plane].band[1][1].width;
646 highpass_stride = s->plane[plane].band[1][1].stride;
648 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
649 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
650 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
651 ret = AVERROR(EINVAL);
655 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
657 low = s->plane[plane].subband[0];
658 high = s->plane[plane].subband[5];
659 output = s->plane[plane].l_h[3];
660 for (i = 0; i < lowpass_width; i++) {
661 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
667 low = s->plane[plane].subband[4];
668 high = s->plane[plane].subband[6];
669 output = s->plane[plane].l_h[4];
670 for (i = 0; i < lowpass_width; i++) {
671 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
677 low = s->plane[plane].l_h[3];
678 high = s->plane[plane].l_h[4];
679 output = s->plane[plane].subband[0];
680 for (i = 0; i < lowpass_height * 2; i++) {
681 horiz_filter(output, low, high, lowpass_width);
682 low += lowpass_width;
683 high += lowpass_width;
684 output += lowpass_width * 2;
687 output = s->plane[plane].subband[0];
688 for (i = 0; i < lowpass_height * 2; i++) {
689 for (j = 0; j < lowpass_width * 2; j++)
692 output += lowpass_width * 2;
696 lowpass_height = s->plane[plane].band[2][1].height;
697 lowpass_width = s->plane[plane].band[2][1].width;
698 highpass_stride = s->plane[plane].band[2][1].stride;
700 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
701 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
702 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
703 ret = AVERROR(EINVAL);
707 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
709 low = s->plane[plane].subband[0];
710 high = s->plane[plane].subband[8];
711 output = s->plane[plane].l_h[6];
712 for (i = 0; i < lowpass_width; i++) {
713 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
719 low = s->plane[plane].subband[7];
720 high = s->plane[plane].subband[9];
721 output = s->plane[plane].l_h[7];
722 for (i = 0; i < lowpass_width; i++) {
723 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
729 dst = (int16_t *)pic->data[act_plane];
730 low = s->plane[plane].l_h[6];
731 high = s->plane[plane].l_h[7];
732 for (i = 0; i < lowpass_height * 2; i++) {
733 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
734 low += lowpass_width;
735 high += lowpass_width;
736 dst += pic->linesize[act_plane] / 2;
749 static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
751 CFHDContext *s = avctx->priv_data;
755 if (!avctx->internal->is_copy) {
756 ff_free_vlc(&s->vlc_9);
757 ff_free_vlc(&s->vlc_18);
763 AVCodec ff_cfhd_decoder = {
765 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
766 .type = AVMEDIA_TYPE_VIDEO,
767 .id = AV_CODEC_ID_CFHD,
768 .priv_data_size = sizeof(CFHDContext),
769 .init = cfhd_decode_init,
770 .close = cfhd_close_decoder,
771 .decode = cfhd_decode,
772 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
773 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,