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);
146 for (j = 0; j < 9; j++)
147 s->plane[i].subband[j] = NULL;
149 for (j = 0; j < 8; j++)
150 s->plane[i].l_h[j] = NULL;
156 static int alloc_buffers(AVCodecContext *avctx)
158 CFHDContext *s = avctx->priv_data;
159 int i, j, k, ret, planes;
161 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
163 avctx->pix_fmt = s->coded_format;
165 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
166 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
168 for (i = 0; i < planes; i++) {
169 int width = i ? avctx->width >> s->chroma_x_shift : avctx->width;
170 int height = i ? avctx->height >> s->chroma_y_shift : avctx->height;
171 int stride = FFALIGN(width / 8, 8) * 8;
172 int w8, h8, w4, h4, w2, h2;
173 height = FFALIGN(height / 8, 2) * 8;
174 s->plane[i].width = width;
175 s->plane[i].height = height;
176 s->plane[i].stride = stride;
178 w8 = FFALIGN(s->plane[i].width / 8, 8);
179 h8 = FFALIGN(s->plane[i].height / 8, 2);
185 s->plane[i].idwt_buf = av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
186 s->plane[i].idwt_tmp = av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
187 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp) {
188 return AVERROR(ENOMEM);
191 s->plane[i].subband[0] = s->plane[i].idwt_buf;
192 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
193 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
194 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
195 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
196 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
197 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
198 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
199 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
200 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
202 for (j = 0; j < DWT_LEVELS; j++) {
203 for(k = 0; k < 4; k++) {
204 s->plane[i].band[j][k].a_width = w8 << j;
205 s->plane[i].band[j][k].a_height = h8 << j;
209 /* ll2 and ll1 commented out because they are done in-place */
210 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
211 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
212 //s->plane[i].l_h[2] = ll2;
213 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
214 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
215 //s->plane[i].l_h[5] = ll1;
216 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
217 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
220 s->a_height = s->coded_height;
221 s->a_width = s->coded_width;
222 s->a_format = s->coded_format;
227 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
230 CFHDContext *s = avctx->priv_data;
232 ThreadFrame frame = { .f = data };
234 int ret = 0, i, j, planes, plane, got_buffer = 0;
237 s->coded_format = AV_PIX_FMT_YUV422P10;
238 init_frame_defaults(s);
239 planes = av_pix_fmt_count_planes(s->coded_format);
241 bytestream2_init(&gb, avpkt->data, avpkt->size);
243 while (bytestream2_get_bytes_left(&gb) > 4) {
244 /* Bit weird but implement the tag parsing as the spec says */
245 uint16_t tagu = bytestream2_get_be16(&gb);
246 int16_t tag = (int16_t)tagu;
247 int8_t tag8 = (int8_t)(tagu >> 8);
248 uint16_t abstag = abs(tag);
249 int8_t abs_tag8 = abs(tag8);
250 uint16_t data = bytestream2_get_be16(&gb);
251 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
252 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
253 } else if (tag == 20) {
254 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
255 s->coded_width = data;
256 } else if (tag == 21) {
257 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
258 s->coded_height = data;
259 } else if (tag == 101) {
260 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
262 } else if (tag == 12) {
263 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
264 s->channel_cnt = data;
266 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
267 ret = AVERROR_PATCHWELCOME;
270 } else if (tag == 14) {
271 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
272 if (data != SUBBAND_COUNT) {
273 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
274 ret = AVERROR_PATCHWELCOME;
277 } else if (tag == 62) {
278 s->channel_num = data;
279 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
280 if (s->channel_num >= planes) {
281 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
282 ret = AVERROR(EINVAL);
285 init_plane_defaults(s);
286 } else if (tag == 48) {
287 if (s->subband_num != 0 && data == 1) // hack
289 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
290 s->subband_num = data;
291 if (s->level >= DWT_LEVELS) {
292 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
293 ret = AVERROR(EINVAL);
296 if (s->subband_num > 3) {
297 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
298 ret = AVERROR(EINVAL);
301 } else if (tag == 51) {
302 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
303 s->subband_num_actual = data;
304 if (s->subband_num_actual >= 10) {
305 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
306 ret = AVERROR(EINVAL);
309 } else if (tag == 35)
310 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
311 else if (tag == 53) {
312 s->quantisation = data;
313 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
314 } else if (tag == 109) {
315 s->prescale_shift[0] = (data >> 0) & 0x7;
316 s->prescale_shift[1] = (data >> 3) & 0x7;
317 s->prescale_shift[2] = (data >> 6) & 0x7;
318 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
319 } else if (tag == 27) {
320 s->plane[s->channel_num].band[0][0].width = data;
321 s->plane[s->channel_num].band[0][0].stride = data;
322 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
323 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
324 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
325 ret = AVERROR(EINVAL);
328 } else if (tag == 28) {
329 s->plane[s->channel_num].band[0][0].height = data;
330 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
331 if (data < 3 || data > s->plane[s->channel_num].band[0][0].height) {
332 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
333 ret = AVERROR(EINVAL);
337 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
338 else if (tag == 10) {
340 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
341 ret = AVERROR_PATCHWELCOME;
344 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
345 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
346 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
347 bytestream2_skipu(&gb, data * 4);
348 } else if (tag == 23) {
349 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
350 avpriv_report_missing_feature(avctx, "Skip frame");
351 ret = AVERROR_PATCHWELCOME;
353 } else if (tag == 2) {
354 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
355 if (data > bytestream2_get_bytes_left(&gb) / 4) {
356 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
357 ret = AVERROR_INVALIDDATA;
360 for (i = 0; i < data; i++) {
361 uint16_t tag2 = bytestream2_get_be16(&gb);
362 uint16_t val2 = bytestream2_get_be16(&gb);
363 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
365 } else if (tag == 41) {
366 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
367 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
368 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);
370 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
371 ret = AVERROR(EINVAL);
374 } else if (tag == 42) {
375 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
376 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
378 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
379 ret = AVERROR(EINVAL);
382 } else if (tag == 49) {
383 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
384 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
385 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
387 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
388 ret = AVERROR(EINVAL);
391 } else if (tag == 50) {
392 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
393 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
395 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
396 ret = AVERROR(EINVAL);
399 } else if (tag == 71) {
401 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
402 } else if (tag == 72) {
404 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
405 } else if (tag == 70) {
406 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
408 if (!(s->bpc == 10 || s->bpc == 12)) {
409 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
410 ret = AVERROR(EINVAL);
413 } else if (tag == 84) {
414 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
416 s->coded_format = AV_PIX_FMT_YUV422P10;
418 s->coded_format = AV_PIX_FMT_GBRP12;
420 s->coded_format = AV_PIX_FMT_GBRAP12;
422 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
423 ret = AVERROR_PATCHWELCOME;
426 planes = av_pix_fmt_count_planes(s->coded_format);
428 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
430 /* Some kind of end of header tag */
431 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
432 s->coded_format != AV_PIX_FMT_NONE) {
433 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
434 s->a_format != s->coded_format) {
436 if ((ret = alloc_buffers(avctx)) < 0) {
441 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
447 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
452 s->coded_format = AV_PIX_FMT_NONE;
455 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
457 /* Lowpass coefficients */
458 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
459 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
460 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
461 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
462 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
465 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
466 ret = AVERROR(EINVAL);
470 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
471 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
472 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
473 ret = AVERROR(EINVAL);
477 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
478 for (i = 0; i < lowpass_height; i++) {
479 for (j = 0; j < lowpass_width; j++)
480 coeff_data[j] = bytestream2_get_be16u(&gb);
482 coeff_data += lowpass_width;
485 /* Align to mod-4 position to continue reading tags */
486 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
488 /* Copy last line of coefficients if odd height */
489 if (lowpass_height & 1) {
490 memcpy(&coeff_data[lowpass_height * lowpass_width],
491 &coeff_data[(lowpass_height - 1) * lowpass_width],
492 lowpass_width * sizeof(*coeff_data));
495 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
498 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
499 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
500 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
501 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
502 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
503 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
505 int a_expected = highpass_a_height * highpass_a_width;
506 int level, run, coeff;
507 int count = 0, bytes;
510 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
511 ret = AVERROR(EINVAL);
515 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
516 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
517 ret = AVERROR(EINVAL);
520 expected = highpass_height * highpass_stride;
522 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);
524 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
526 OPEN_READER(re, &s->gb);
529 UPDATE_CACHE(re, &s->gb);
530 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
539 if (count > expected)
542 coeff = dequant_and_decompand(level, s->quantisation);
543 for (i = 0; i < run; i++)
544 *coeff_data++ = coeff;
548 UPDATE_CACHE(re, &s->gb);
549 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
553 if (level == 255 && run == 2)
558 if (count > expected)
561 coeff = dequant_and_decompand(level, s->quantisation);
562 for (i = 0; i < run; i++)
563 *coeff_data++ = coeff;
566 CLOSE_READER(re, &s->gb);
569 if (count > expected) {
570 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
571 ret = AVERROR(EINVAL);
575 bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
576 if (bytes > bytestream2_get_bytes_left(&gb)) {
577 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
578 ret = AVERROR(EINVAL);
581 bytestream2_seek(&gb, bytes, SEEK_CUR);
583 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
586 /* Copy last line of coefficients if odd height */
587 if (highpass_height & 1) {
588 memcpy(&coeff_data[highpass_height * highpass_stride],
589 &coeff_data[(highpass_height - 1) * highpass_stride],
590 highpass_stride * sizeof(*coeff_data));
595 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
596 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
597 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
598 ret = AVERROR(EINVAL);
603 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
604 ret = AVERROR(EINVAL);
608 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
609 for (plane = 0; plane < planes && !ret; plane++) {
611 int lowpass_height = s->plane[plane].band[0][0].height;
612 int lowpass_width = s->plane[plane].band[0][0].width;
613 int highpass_stride = s->plane[plane].band[0][1].stride;
614 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
615 int16_t *low, *high, *output, *dst;
617 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
618 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
619 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
620 ret = AVERROR(EINVAL);
624 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
626 low = s->plane[plane].subband[0];
627 high = s->plane[plane].subband[2];
628 output = s->plane[plane].l_h[0];
629 for (i = 0; i < lowpass_width; i++) {
630 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
636 low = s->plane[plane].subband[1];
637 high = s->plane[plane].subband[3];
638 output = s->plane[plane].l_h[1];
640 for (i = 0; i < lowpass_width; i++) {
641 // note the stride of "low" is highpass_stride
642 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
648 low = s->plane[plane].l_h[0];
649 high = s->plane[plane].l_h[1];
650 output = s->plane[plane].subband[0];
651 for (i = 0; i < lowpass_height * 2; i++) {
652 horiz_filter(output, low, high, lowpass_width);
653 low += lowpass_width;
654 high += lowpass_width;
655 output += lowpass_width * 2;
658 output = s->plane[plane].subband[0];
659 for (i = 0; i < lowpass_height * 2; i++) {
660 for (j = 0; j < lowpass_width * 2; j++)
663 output += lowpass_width * 2;
668 lowpass_height = s->plane[plane].band[1][1].height;
669 lowpass_width = s->plane[plane].band[1][1].width;
670 highpass_stride = s->plane[plane].band[1][1].stride;
672 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
673 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
674 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
675 ret = AVERROR(EINVAL);
679 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
681 low = s->plane[plane].subband[0];
682 high = s->plane[plane].subband[5];
683 output = s->plane[plane].l_h[3];
684 for (i = 0; i < lowpass_width; i++) {
685 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
691 low = s->plane[plane].subband[4];
692 high = s->plane[plane].subband[6];
693 output = s->plane[plane].l_h[4];
694 for (i = 0; i < lowpass_width; i++) {
695 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
701 low = s->plane[plane].l_h[3];
702 high = s->plane[plane].l_h[4];
703 output = s->plane[plane].subband[0];
704 for (i = 0; i < lowpass_height * 2; i++) {
705 horiz_filter(output, low, high, lowpass_width);
706 low += lowpass_width;
707 high += lowpass_width;
708 output += lowpass_width * 2;
711 output = s->plane[plane].subband[0];
712 for (i = 0; i < lowpass_height * 2; i++) {
713 for (j = 0; j < lowpass_width * 2; j++)
716 output += lowpass_width * 2;
720 lowpass_height = s->plane[plane].band[2][1].height;
721 lowpass_width = s->plane[plane].band[2][1].width;
722 highpass_stride = s->plane[plane].band[2][1].stride;
724 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
725 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
726 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
727 ret = AVERROR(EINVAL);
731 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
733 low = s->plane[plane].subband[0];
734 high = s->plane[plane].subband[8];
735 output = s->plane[plane].l_h[6];
736 for (i = 0; i < lowpass_width; i++) {
737 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
743 low = s->plane[plane].subband[7];
744 high = s->plane[plane].subband[9];
745 output = s->plane[plane].l_h[7];
746 for (i = 0; i < lowpass_width; i++) {
747 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
753 dst = (int16_t *)pic->data[act_plane];
754 low = s->plane[plane].l_h[6];
755 high = s->plane[plane].l_h[7];
756 for (i = 0; i < lowpass_height * 2; i++) {
757 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
758 low += lowpass_width;
759 high += lowpass_width;
760 dst += pic->linesize[act_plane] / 2;
773 static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
775 CFHDContext *s = avctx->priv_data;
779 if (!avctx->internal->is_copy) {
780 ff_free_vlc(&s->vlc_9);
781 ff_free_vlc(&s->vlc_18);
787 AVCodec ff_cfhd_decoder = {
789 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
790 .type = AVMEDIA_TYPE_VIDEO,
791 .id = AV_CODEC_ID_CFHD,
792 .priv_data_size = sizeof(CFHDContext),
793 .init = cfhd_decode_init,
794 .close = cfhd_close_decoder,
795 .decode = cfhd_decode,
796 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
797 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,