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;
49 return ff_cfhd_init_vlcs(s);
52 static void init_plane_defaults(CFHDContext *s)
56 s->subband_num_actual = 0;
59 static void init_frame_defaults(CFHDContext *s)
67 s->lowpass_precision = 16;
72 init_plane_defaults(s);
75 /* TODO: merge with VLC tables or use LUT */
76 static inline int dequant_and_decompand(int level, int quantisation)
78 int64_t abslevel = abs(level);
79 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) * FFSIGN(level) * quantisation;
82 static inline void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride,
83 int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
88 for (i = 0; i < len; i++) {
90 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
91 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
93 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
95 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
96 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
98 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
99 } else if (i == len-1) {
100 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
101 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
103 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
105 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
106 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
108 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
110 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
111 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
113 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
115 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
116 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
118 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
123 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high, int width)
125 filter(output, 1, low, 1, high, 1, width, 0);
128 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high, int width, uint8_t clip)
130 filter(output, 1, low, 1, high, 1, width, clip);
133 static void vert_filter(int16_t *output, int out_stride, int16_t *low, int low_stride,
134 int16_t *high, int high_stride, int len)
136 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
139 static void free_buffers(AVCodecContext *avctx)
141 CFHDContext *s = avctx->priv_data;
144 for (i = 0; i < 4; i++) {
145 av_freep(&s->plane[i].idwt_buf);
146 av_freep(&s->plane[i].idwt_tmp);
152 static int alloc_buffers(AVCodecContext *avctx)
154 CFHDContext *s = avctx->priv_data;
155 int i, j, k, ret, planes;
157 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
159 avctx->pix_fmt = s->coded_format;
161 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
162 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
164 for (i = 0; i < planes; i++) {
165 int width = i ? avctx->width >> s->chroma_x_shift : avctx->width;
166 int height = i ? avctx->height >> s->chroma_y_shift : avctx->height;
167 int stride = FFALIGN(width / 8, 8) * 8;
168 int w8, h8, w4, h4, w2, h2;
169 height = FFALIGN(height / 8, 2) * 8;
170 s->plane[i].width = width;
171 s->plane[i].height = height;
172 s->plane[i].stride = stride;
174 w8 = FFALIGN(s->plane[i].width / 8, 8);
175 h8 = FFALIGN(s->plane[i].height / 8, 2);
181 s->plane[i].idwt_buf = av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_buf));
182 s->plane[i].idwt_tmp = av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
183 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp) {
184 return AVERROR(ENOMEM);
187 s->plane[i].subband[0] = s->plane[i].idwt_buf;
188 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
189 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
190 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
191 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
192 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
193 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
194 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
195 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
196 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
198 for (j = 0; j < DWT_LEVELS; j++) {
199 for(k = 0; k < 4; k++) {
200 s->plane[i].band[j][k].a_width = w8 << j;
201 s->plane[i].band[j][k].a_height = h8 << j;
205 /* ll2 and ll1 commented out because they are done in-place */
206 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
207 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
208 //s->plane[i].l_h[2] = ll2;
209 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
210 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
211 //s->plane[i].l_h[5] = ll1;
212 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
213 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
216 s->a_height = s->coded_height;
217 s->a_width = s->coded_width;
218 s->a_format = s->coded_format;
223 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
226 CFHDContext *s = avctx->priv_data;
228 ThreadFrame frame = { .f = data };
230 int ret = 0, i, j, planes, plane, got_buffer = 0;
233 s->coded_format = AV_PIX_FMT_YUV422P10;
234 init_frame_defaults(s);
235 planes = av_pix_fmt_count_planes(s->coded_format);
237 bytestream2_init(&gb, avpkt->data, avpkt->size);
239 while (bytestream2_get_bytes_left(&gb) > 4) {
240 /* Bit weird but implement the tag parsing as the spec says */
241 uint16_t tagu = bytestream2_get_be16(&gb);
242 int16_t tag = (int16_t)tagu;
243 int8_t tag8 = (int8_t)(tagu >> 8);
244 uint16_t abstag = abs(tag);
245 int8_t abs_tag8 = abs(tag8);
246 uint16_t data = bytestream2_get_be16(&gb);
247 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
248 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
249 } else if (tag == 20) {
250 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
251 s->coded_width = data;
252 } else if (tag == 21) {
253 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
254 s->coded_height = data;
255 } else if (tag == 101) {
256 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
258 } else if (tag == 12) {
259 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
260 s->channel_cnt = data;
262 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
263 ret = AVERROR_PATCHWELCOME;
266 } else if (tag == 14) {
267 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
268 if (data != SUBBAND_COUNT) {
269 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
270 ret = AVERROR_PATCHWELCOME;
273 } else if (tag == 62) {
274 s->channel_num = data;
275 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
276 if (s->channel_num >= planes) {
277 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
278 ret = AVERROR(EINVAL);
281 init_plane_defaults(s);
282 } else if (tag == 48) {
283 if (s->subband_num != 0 && data == 1) // hack
285 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
286 s->subband_num = data;
287 if (s->level >= DWT_LEVELS) {
288 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
289 ret = AVERROR(EINVAL);
292 if (s->subband_num > 3) {
293 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
294 ret = AVERROR(EINVAL);
297 } else if (tag == 51) {
298 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
299 s->subband_num_actual = data;
300 if (s->subband_num_actual >= 10) {
301 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
302 ret = AVERROR(EINVAL);
305 } else if (tag == 35)
306 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
307 else if (tag == 53) {
308 s->quantisation = data;
309 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
310 } else if (tag == 109) {
311 s->prescale_shift[0] = (data >> 0) & 0x7;
312 s->prescale_shift[1] = (data >> 3) & 0x7;
313 s->prescale_shift[2] = (data >> 6) & 0x7;
314 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
315 } else if (tag == 27) {
316 s->plane[s->channel_num].band[0][0].width = data;
317 s->plane[s->channel_num].band[0][0].stride = data;
318 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
319 if (data < 2 || data > s->plane[s->channel_num].band[0][0].a_width) {
320 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
321 ret = AVERROR(EINVAL);
324 } else if (tag == 28) {
325 s->plane[s->channel_num].band[0][0].height = data;
326 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
327 if (data < 2 || data > s->plane[s->channel_num].band[0][0].height) {
328 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
329 ret = AVERROR(EINVAL);
333 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
334 else if (tag == 10) {
336 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
337 ret = AVERROR_PATCHWELCOME;
340 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
341 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
342 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %"PRIu16" %s\n", data * 4, tag < 0 ? "optional" : "required");
343 bytestream2_skipu(&gb, data * 4);
344 } else if (tag == 23) {
345 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
346 avpriv_report_missing_feature(avctx, "Skip frame");
347 ret = AVERROR_PATCHWELCOME;
349 } else if (tag == 2) {
350 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
351 if (data > bytestream2_get_bytes_left(&gb) / 4) {
352 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
353 ret = AVERROR_INVALIDDATA;
356 for (i = 0; i < data; i++) {
357 uint16_t tag2 = bytestream2_get_be16(&gb);
358 uint16_t val2 = bytestream2_get_be16(&gb);
359 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
361 } else if (tag == 41) {
362 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
363 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
364 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);
366 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
367 ret = AVERROR(EINVAL);
370 } else if (tag == 42) {
371 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
372 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
374 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
375 ret = AVERROR(EINVAL);
378 } else if (tag == 49) {
379 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
380 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
381 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
383 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
384 ret = AVERROR(EINVAL);
387 } else if (tag == 50) {
388 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
389 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
391 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
392 ret = AVERROR(EINVAL);
395 } else if (tag == 71) {
397 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
398 } else if (tag == 72) {
400 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
401 } else if (tag == 70) {
402 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
404 if (!(s->bpc == 10 || s->bpc == 12)) {
405 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
406 ret = AVERROR(EINVAL);
409 } else if (tag == 84) {
410 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
412 s->coded_format = AV_PIX_FMT_YUV422P10;
414 s->coded_format = AV_PIX_FMT_GBRP12;
416 s->coded_format = AV_PIX_FMT_GBRAP12;
418 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
419 ret = AVERROR_PATCHWELCOME;
422 planes = av_pix_fmt_count_planes(s->coded_format);
424 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
426 /* Some kind of end of header tag */
427 if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
428 s->coded_format != AV_PIX_FMT_NONE) {
429 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
430 s->a_format != s->coded_format) {
432 if ((ret = alloc_buffers(avctx)) < 0) {
438 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
443 s->coded_format = AV_PIX_FMT_NONE;
446 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
448 /* Lowpass coefficients */
449 if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
450 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
451 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
452 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
453 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
455 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
456 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
457 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
458 ret = AVERROR(EINVAL);
462 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %"PRIu16" height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
463 for (i = 0; i < lowpass_height; i++) {
464 for (j = 0; j < lowpass_width; j++)
465 coeff_data[j] = bytestream2_get_be16u(&gb);
467 coeff_data += lowpass_width;
470 /* Align to mod-4 position to continue reading tags */
471 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
473 /* Copy last line of coefficients if odd height */
474 if (lowpass_height & 1) {
475 memcpy(&coeff_data[lowpass_height * lowpass_width],
476 &coeff_data[(lowpass_height - 1) * lowpass_width],
477 lowpass_width * sizeof(*coeff_data));
480 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %"PRIu16"\n", lowpass_width * lowpass_height);
483 if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
484 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
485 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
486 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
487 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
488 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
489 int expected = highpass_height * highpass_stride;
490 int a_expected = highpass_a_height * highpass_a_width;
491 int level, run, coeff;
492 int count = 0, bytes;
494 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
495 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficents\n");
496 ret = AVERROR(EINVAL);
500 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);
502 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
504 OPEN_READER(re, &s->gb);
507 UPDATE_CACHE(re, &s->gb);
508 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
517 if (count > expected)
520 coeff = dequant_and_decompand(level, s->quantisation);
521 for (i = 0; i < run; i++)
522 *coeff_data++ = coeff;
526 UPDATE_CACHE(re, &s->gb);
527 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
531 if (level == 255 && run == 2)
536 if (count > expected)
539 coeff = dequant_and_decompand(level, s->quantisation);
540 for (i = 0; i < run; i++)
541 *coeff_data++ = coeff;
544 CLOSE_READER(re, &s->gb);
547 if (count > expected) {
548 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
549 ret = AVERROR(EINVAL);
553 bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
554 if (bytes > bytestream2_get_bytes_left(&gb)) {
555 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
556 ret = AVERROR(EINVAL);
559 bytestream2_seek(&gb, bytes, SEEK_CUR);
561 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
564 /* Copy last line of coefficients if odd height */
565 if (highpass_height & 1) {
566 memcpy(&coeff_data[highpass_height * highpass_stride],
567 &coeff_data[(highpass_height - 1) * highpass_stride],
568 highpass_stride * sizeof(*coeff_data));
573 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
574 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
575 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
576 ret = AVERROR(EINVAL);
581 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
582 ret = AVERROR(EINVAL);
586 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
587 for (plane = 0; plane < planes && !ret; plane++) {
589 int lowpass_height = s->plane[plane].band[0][0].height;
590 int lowpass_width = s->plane[plane].band[0][0].width;
591 int highpass_stride = s->plane[plane].band[0][1].stride;
592 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
593 int16_t *low, *high, *output, *dst;
595 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
596 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
597 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
598 ret = AVERROR(EINVAL);
602 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
604 low = s->plane[plane].subband[0];
605 high = s->plane[plane].subband[2];
606 output = s->plane[plane].l_h[0];
607 for (i = 0; i < lowpass_width; i++) {
608 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
614 low = s->plane[plane].subband[1];
615 high = s->plane[plane].subband[3];
616 output = s->plane[plane].l_h[1];
618 for (i = 0; i < lowpass_width; i++) {
619 // note the stride of "low" is highpass_stride
620 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
626 low = s->plane[plane].l_h[0];
627 high = s->plane[plane].l_h[1];
628 output = s->plane[plane].subband[0];
629 for (i = 0; i < lowpass_height * 2; i++) {
630 horiz_filter(output, low, high, lowpass_width);
631 low += lowpass_width;
632 high += lowpass_width;
633 output += lowpass_width * 2;
636 output = s->plane[plane].subband[0];
637 for (i = 0; i < lowpass_height * 2; i++) {
638 for (j = 0; j < lowpass_width * 2; j++)
641 output += lowpass_width * 2;
646 lowpass_height = s->plane[plane].band[1][1].height;
647 lowpass_width = s->plane[plane].band[1][1].width;
648 highpass_stride = s->plane[plane].band[1][1].stride;
650 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
651 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
652 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
653 ret = AVERROR(EINVAL);
657 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
659 low = s->plane[plane].subband[0];
660 high = s->plane[plane].subband[5];
661 output = s->plane[plane].l_h[3];
662 for (i = 0; i < lowpass_width; i++) {
663 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
669 low = s->plane[plane].subband[4];
670 high = s->plane[plane].subband[6];
671 output = s->plane[plane].l_h[4];
672 for (i = 0; i < lowpass_width; i++) {
673 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
679 low = s->plane[plane].l_h[3];
680 high = s->plane[plane].l_h[4];
681 output = s->plane[plane].subband[0];
682 for (i = 0; i < lowpass_height * 2; i++) {
683 horiz_filter(output, low, high, lowpass_width);
684 low += lowpass_width;
685 high += lowpass_width;
686 output += lowpass_width * 2;
689 output = s->plane[plane].subband[0];
690 for (i = 0; i < lowpass_height * 2; i++) {
691 for (j = 0; j < lowpass_width * 2; j++)
694 output += lowpass_width * 2;
698 lowpass_height = s->plane[plane].band[2][1].height;
699 lowpass_width = s->plane[plane].band[2][1].width;
700 highpass_stride = s->plane[plane].band[2][1].stride;
702 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
703 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
704 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
705 ret = AVERROR(EINVAL);
709 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
711 low = s->plane[plane].subband[0];
712 high = s->plane[plane].subband[8];
713 output = s->plane[plane].l_h[6];
714 for (i = 0; i < lowpass_width; i++) {
715 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
721 low = s->plane[plane].subband[7];
722 high = s->plane[plane].subband[9];
723 output = s->plane[plane].l_h[7];
724 for (i = 0; i < lowpass_width; i++) {
725 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
731 dst = (int16_t *)pic->data[act_plane];
732 low = s->plane[plane].l_h[6];
733 high = s->plane[plane].l_h[7];
734 for (i = 0; i < lowpass_height * 2; i++) {
735 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
736 low += lowpass_width;
737 high += lowpass_width;
738 dst += pic->linesize[act_plane] / 2;
751 static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
753 CFHDContext *s = avctx->priv_data;
757 if (!avctx->internal->is_copy) {
758 ff_free_vlc(&s->vlc_9);
759 ff_free_vlc(&s->vlc_18);
765 AVCodec ff_cfhd_decoder = {
767 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
768 .type = AVMEDIA_TYPE_VIDEO,
769 .id = AV_CODEC_ID_CFHD,
770 .priv_data_size = sizeof(CFHDContext),
771 .init = cfhd_decode_init,
772 .close = cfhd_close_decoder,
773 .decode = cfhd_decode,
774 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
775 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,