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
43 static av_cold int cfhd_init(AVCodecContext *avctx)
45 CFHDContext *s = avctx->priv_data;
49 for (int i = 0; i < 64; i++) {
67 for (int i = 0; i < 256; i++)
68 s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
70 return ff_cfhd_init_vlcs(s);
73 static void init_plane_defaults(CFHDContext *s)
77 s->subband_num_actual = 0;
80 static void init_peak_table_defaults(CFHDContext *s)
84 memset(&s->peak.base, 0, sizeof(s->peak.base));
87 static void init_frame_defaults(CFHDContext *s)
91 s->coded_format = AV_PIX_FMT_YUV422P10;
92 s->cropped_height = 0;
95 s->subband_cnt = SUBBAND_COUNT;
97 s->lowpass_precision = 16;
100 s->difference_coding = 0;
103 init_plane_defaults(s);
104 init_peak_table_defaults(s);
107 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
109 if (codebook == 0 || codebook == 1) {
110 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
112 return level * quantisation;
115 static inline void difference_coding(int16_t *band, int width, int height)
119 for (i = 0; i < height; i++) {
120 for (j = 1; j < width; j++) {
121 band[j] += band[j-1];
127 static inline void peak_table(int16_t *band, Peak *peak, int length)
130 for (i = 0; i < length; i++)
131 if (abs(band[i]) > peak->level)
132 band[i] = bytestream2_get_le16(&peak->base);
135 static inline void process_alpha(int16_t *alpha, int width)
138 for (i = 0; i < width; i++) {
140 channel -= ALPHA_COMPAND_DC_OFFSET;
142 channel *= ALPHA_COMPAND_GAIN;
144 channel = av_clip_uintp2(channel, 12);
149 static inline void process_bayer(AVFrame *frame, int bpc)
151 const int linesize = frame->linesize[0];
152 uint16_t *r = (uint16_t *)frame->data[0];
153 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
154 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
155 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
156 const int mid = 1 << (bpc - 1);
157 const int factor = 1 << (16 - bpc);
159 for (int y = 0; y < frame->height >> 1; y++) {
160 for (int x = 0; x < frame->width; x += 2) {
170 R = (rg - mid) * 2 + g;
173 B = (bg - mid) * 2 + g;
175 R = av_clip_uintp2(R * factor, 16);
176 G1 = av_clip_uintp2(G1 * factor, 16);
177 G2 = av_clip_uintp2(G2 * factor, 16);
178 B = av_clip_uintp2(B * factor, 16);
193 static inline void filter(int16_t *output, ptrdiff_t out_stride,
194 int16_t *low, ptrdiff_t low_stride,
195 int16_t *high, ptrdiff_t high_stride,
201 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
202 output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
204 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
206 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
207 output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
209 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
211 for (i = 1; i < len - 1; i++) {
212 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
213 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
215 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
217 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
218 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
220 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
223 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
224 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
226 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
228 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
229 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
231 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
234 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
235 int width, int linesize, int plane)
239 for (i = 0; i < width; i++) {
240 even = (low[i] - high[i])/2;
241 odd = (low[i] + high[i])/2;
242 output[i] = av_clip_uintp2(even, 10);
243 output[i + linesize] = av_clip_uintp2(odd, 10);
247 static inline void inverse_temporal_filter(int16_t *output, int16_t *low, int16_t *high,
250 for (int i = 0; i < width; i++) {
251 int even = (low[i] - high[i]) / 2;
252 int odd = (low[i] + high[i]) / 2;
259 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
262 filter(output, 1, low, 1, high, 1, width, 0);
265 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
268 filter(output, 1, low, 1, high, 1, width, clip);
271 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
274 filter(output, 2, low, 1, high, 1, width, clip);
277 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
278 int16_t *low, ptrdiff_t low_stride,
279 int16_t *high, ptrdiff_t high_stride, int len)
281 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
284 static void free_buffers(CFHDContext *s)
288 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
289 av_freep(&s->plane[i].idwt_buf);
290 av_freep(&s->plane[i].idwt_tmp);
291 s->plane[i].idwt_size = 0;
293 for (j = 0; j < SUBBAND_COUNT_3D; j++)
294 s->plane[i].subband[j] = NULL;
296 for (j = 0; j < 10; j++)
297 s->plane[i].l_h[j] = NULL;
303 static int alloc_buffers(AVCodecContext *avctx)
305 CFHDContext *s = avctx->priv_data;
306 int i, j, ret, planes, bayer = 0;
307 int chroma_x_shift, chroma_y_shift;
310 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
312 avctx->pix_fmt = s->coded_format;
314 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
316 &chroma_y_shift)) < 0)
318 planes = av_pix_fmt_count_planes(s->coded_format);
319 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
326 for (i = 0; i < planes; i++) {
327 int w8, h8, w4, h4, w2, h2;
328 int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
329 int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
330 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
332 if (chroma_y_shift && !bayer)
333 height = FFALIGN(height / 8, 2) * 8;
334 s->plane[i].width = width;
335 s->plane[i].height = height;
336 s->plane[i].stride = stride;
338 w8 = FFALIGN(s->plane[i].width / 8, 8);
339 h8 = FFALIGN(height, 8) / 8;
345 if (s->transform_type == 0) {
346 s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
347 s->plane[i].idwt_buf =
348 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
349 s->plane[i].idwt_tmp =
350 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
352 s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
353 s->plane[i].idwt_buf =
354 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
355 s->plane[i].idwt_tmp =
356 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
359 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
360 return AVERROR(ENOMEM);
362 s->plane[i].subband[0] = s->plane[i].idwt_buf;
363 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
364 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
365 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
366 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
367 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
368 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
369 if (s->transform_type == 0) {
370 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
371 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
372 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
375 s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
376 s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
377 s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
378 s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
379 s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
380 s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
381 s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
382 s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
383 s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
384 s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
387 if (s->transform_type == 0) {
388 for (j = 0; j < DWT_LEVELS; j++) {
389 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
390 s->plane[i].band[j][k].a_width = w8 << j;
391 s->plane[i].band[j][k].a_height = h8 << j;
395 for (j = 0; j < DWT_LEVELS_3D; j++) {
396 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
398 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
399 s->plane[i].band[j][k].a_width = w8 << t;
400 s->plane[i].band[j][k].a_height = h8 << t;
405 /* ll2 and ll1 commented out because they are done in-place */
406 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
407 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
408 // s->plane[i].l_h[2] = ll2;
409 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
410 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
411 // s->plane[i].l_h[5] = ll1;
412 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
413 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
414 if (s->transform_type != 0) {
415 int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
417 s->plane[i].l_h[8] = frame2;
418 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
422 s->a_height = s->coded_height;
423 s->a_width = s->coded_width;
424 s->a_format = s->coded_format;
429 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
432 CFHDContext *s = avctx->priv_data;
434 ThreadFrame frame = { .f = data };
436 int ret = 0, i, j, plane, got_buffer = 0;
439 init_frame_defaults(s);
440 s->planes = av_pix_fmt_count_planes(s->coded_format);
442 bytestream2_init(&gb, avpkt->data, avpkt->size);
444 while (bytestream2_get_bytes_left(&gb) >= 4) {
445 /* Bit weird but implement the tag parsing as the spec says */
446 uint16_t tagu = bytestream2_get_be16(&gb);
447 int16_t tag = (int16_t)tagu;
448 int8_t tag8 = (int8_t)(tagu >> 8);
449 uint16_t abstag = abs(tag);
450 int8_t abs_tag8 = abs(tag8);
451 uint16_t data = bytestream2_get_be16(&gb);
452 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
453 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
454 } else if (tag == SampleFlags) {
455 av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
456 s->progressive = data & 0x0001;
457 } else if (tag == FrameType) {
458 s->frame_type = data;
459 av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
460 } else if (abstag == VersionMajor) {
461 av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
462 } else if (abstag == VersionMinor) {
463 av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
464 } else if (abstag == VersionRevision) {
465 av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
466 } else if (abstag == VersionEdit) {
467 av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
468 } else if (abstag == Version) {
469 av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
470 } else if (tag == ImageWidth) {
471 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
472 s->coded_width = data;
473 } else if (tag == ImageHeight) {
474 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
475 s->coded_height = data;
476 } else if (tag == ChannelCount) {
477 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
478 s->channel_cnt = data;
480 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
481 ret = AVERROR_PATCHWELCOME;
484 } else if (tag == SubbandCount) {
485 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
486 if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
487 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
488 ret = AVERROR_PATCHWELCOME;
491 } else if (tag == ChannelNumber) {
492 s->channel_num = data;
493 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
494 if (s->channel_num >= s->planes) {
495 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
496 ret = AVERROR(EINVAL);
499 init_plane_defaults(s);
500 } else if (tag == SubbandNumber) {
501 if (s->subband_num != 0 && data == 1) // hack
503 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
504 s->subband_num = data;
505 if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
506 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
507 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
508 ret = AVERROR(EINVAL);
511 if (s->subband_num > 3) {
512 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
513 ret = AVERROR(EINVAL);
516 } else if (tag == SubbandBand) {
517 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
518 s->subband_num_actual = data;
519 if ((s->transform_type == 0 && s->subband_num_actual >= SUBBAND_COUNT) ||
520 (s->transform_type == 2 && s->subband_num_actual >= SUBBAND_COUNT_3D && s->subband_num_actual != 255)) {
521 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
522 ret = AVERROR(EINVAL);
525 } else if (tag == LowpassPrecision)
526 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
527 else if (tag == Quantization) {
528 s->quantisation = data;
529 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
530 } else if (tag == PrescaleTable) {
531 for (i = 0; i < 8; i++)
532 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
533 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
534 } else if (tag == BandEncoding) {
535 if (!data || data > 5) {
536 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
537 ret = AVERROR(EINVAL);
540 s->band_encoding = data;
541 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
542 } else if (tag == LowpassWidth) {
543 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
544 s->plane[s->channel_num].band[0][0].width = data;
545 s->plane[s->channel_num].band[0][0].stride = data;
546 } else if (tag == LowpassHeight) {
547 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
548 s->plane[s->channel_num].band[0][0].height = data;
549 } else if (tag == SampleType) {
550 s->sample_type = data;
551 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
552 } else if (tag == TransformType) {
554 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
555 ret = AVERROR(EINVAL);
558 s->transform_type = data;
559 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
560 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
561 if (abstag == 0x4001)
563 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
564 bytestream2_skipu(&gb, data * 4);
565 } else if (tag == FrameIndex) {
566 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
567 s->frame_index = data;
568 } else if (tag == SampleIndexTable) {
569 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
570 if (data > bytestream2_get_bytes_left(&gb) / 4) {
571 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
572 ret = AVERROR_INVALIDDATA;
575 for (i = 0; i < data; i++) {
576 uint16_t tag2 = bytestream2_get_be16(&gb);
577 uint16_t val2 = bytestream2_get_be16(&gb);
578 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
580 } else if (tag == HighpassWidth) {
581 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);
583 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
584 ret = AVERROR(EINVAL);
587 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
588 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
589 } else if (tag == HighpassHeight) {
590 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
592 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
593 ret = AVERROR(EINVAL);
596 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
597 } else if (tag == BandWidth) {
598 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
600 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
601 ret = AVERROR(EINVAL);
604 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
605 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
606 } else if (tag == BandHeight) {
607 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
609 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
610 ret = AVERROR(EINVAL);
613 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
614 } else if (tag == InputFormat) {
615 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
616 if (s->coded_format == AV_PIX_FMT_NONE ||
617 s->coded_format == AV_PIX_FMT_YUV422P10) {
618 if (data >= 100 && data <= 105) {
619 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
620 } else if (data >= 122 && data <= 128) {
621 s->coded_format = AV_PIX_FMT_GBRP12;
622 } else if (data == 30) {
623 s->coded_format = AV_PIX_FMT_GBRAP12;
625 s->coded_format = AV_PIX_FMT_YUV422P10;
627 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
629 } else if (tag == BandCodingFlags) {
630 s->codebook = data & 0xf;
631 s->difference_coding = (data >> 4) & 1;
632 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
633 } else if (tag == Precision) {
634 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
635 if (!(data == 10 || data == 12)) {
636 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
637 ret = AVERROR(EINVAL);
640 avctx->bits_per_raw_sample = s->bpc = data;
641 } else if (tag == EncodedFormat) {
642 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
644 s->coded_format = AV_PIX_FMT_YUV422P10;
645 } else if (data == 2) {
646 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
647 } else if (data == 3) {
648 s->coded_format = AV_PIX_FMT_GBRP12;
649 } else if (data == 4) {
650 s->coded_format = AV_PIX_FMT_GBRAP12;
652 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
653 ret = AVERROR_PATCHWELCOME;
656 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
657 } else if (tag == -85) {
658 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
659 s->cropped_height = data;
660 } else if (tag == -75) {
661 s->peak.offset &= ~0xffff;
662 s->peak.offset |= (data & 0xffff);
665 } else if (tag == -76) {
666 s->peak.offset &= 0xffff;
667 s->peak.offset |= (data & 0xffffU)<<16;
670 } else if (tag == -74 && s->peak.offset) {
671 s->peak.level = data;
672 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
674 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
676 if (tag == BitstreamMarker && data == 0xf0f &&
677 s->coded_format != AV_PIX_FMT_NONE) {
678 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
679 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
680 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
682 if (s->coded_width) {
683 s->coded_width *= factor;
686 if (s->coded_height) {
687 s->coded_height *= factor;
690 if (!s->a_width && !s->coded_width) {
691 s->coded_width = lowpass_width * factor * 8;
694 if (!s->a_height && !s->coded_height) {
695 s->coded_height = lowpass_height * factor * 8;
698 if (s->a_width && !s->coded_width)
699 s->coded_width = s->a_width;
700 if (s->a_height && !s->coded_height)
701 s->coded_height = s->a_height;
703 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
704 s->a_format != s->coded_format) {
706 if ((ret = alloc_buffers(avctx)) < 0) {
711 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
714 if (s->cropped_height) {
715 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
716 if (avctx->height < height)
717 return AVERROR_INVALIDDATA;
718 avctx->height = height;
723 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
728 s->coded_format = AV_PIX_FMT_NONE;
730 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
734 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
738 s->coded_format = AV_PIX_FMT_NONE;
742 if (s->subband_num_actual == 255)
744 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
746 /* Lowpass coefficients */
747 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
748 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
749 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
750 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
751 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
753 if (lowpass_width < 3 ||
754 lowpass_width > lowpass_a_width) {
755 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
756 ret = AVERROR(EINVAL);
760 if (lowpass_height < 3 ||
761 lowpass_height > lowpass_a_height) {
762 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
763 ret = AVERROR(EINVAL);
768 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
769 ret = AVERROR(EINVAL);
773 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
774 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
775 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
776 ret = AVERROR(EINVAL);
780 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
781 for (i = 0; i < lowpass_height; i++) {
782 for (j = 0; j < lowpass_width; j++)
783 coeff_data[j] = bytestream2_get_be16u(&gb);
785 coeff_data += lowpass_width;
788 /* Align to mod-4 position to continue reading tags */
789 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
791 /* Copy last line of coefficients if odd height */
792 if (lowpass_height & 1) {
793 memcpy(&coeff_data[lowpass_height * lowpass_width],
794 &coeff_data[(lowpass_height - 1) * lowpass_width],
795 lowpass_width * sizeof(*coeff_data));
798 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
801 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
802 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
803 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
804 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
805 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
806 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
808 int a_expected = highpass_a_height * highpass_a_width;
809 int level, run, coeff;
810 int count = 0, bytes;
813 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
814 ret = AVERROR(EINVAL);
818 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
819 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
820 ret = AVERROR(EINVAL);
823 expected = highpass_height * highpass_stride;
825 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);
827 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
831 OPEN_READER(re, &s->gb);
833 const int lossless = s->band_encoding == 5;
835 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
839 UPDATE_CACHE(re, &s->gb);
840 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
849 if (count > expected)
853 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
856 if (tag == BandSecondPass) {
857 const uint16_t q = s->quantisation;
859 for (i = 0; i < run; i++) {
860 *coeff_data |= coeff << 8;
864 for (i = 0; i < run; i++)
865 *coeff_data++ = coeff;
870 UPDATE_CACHE(re, &s->gb);
871 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
875 if (level == 255 && run == 2)
880 if (count > expected)
884 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
887 if (tag == BandSecondPass) {
888 const uint16_t q = s->quantisation;
890 for (i = 0; i < run; i++) {
891 *coeff_data |= coeff << 8;
895 for (i = 0; i < run; i++)
896 *coeff_data++ = coeff;
900 CLOSE_READER(re, &s->gb);
903 if (count > expected) {
904 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
905 ret = AVERROR(EINVAL);
909 peak_table(coeff_data - count, &s->peak, count);
910 if (s->difference_coding)
911 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
913 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
914 if (bytes > bytestream2_get_bytes_left(&gb)) {
915 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
916 ret = AVERROR(EINVAL);
919 bytestream2_seek(&gb, bytes, SEEK_CUR);
921 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
923 if (s->subband_num_actual != 255)
926 /* Copy last line of coefficients if odd height */
927 if (highpass_height & 1) {
928 memcpy(&coeff_data[highpass_height * highpass_stride],
929 &coeff_data[(highpass_height - 1) * highpass_stride],
930 highpass_stride * sizeof(*coeff_data));
935 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
936 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
941 ff_thread_finish_setup(avctx);
943 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
944 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
945 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
946 ret = AVERROR(EINVAL);
951 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
952 ret = AVERROR(EINVAL);
956 if (s->transform_type == 0 && s->sample_type != 1) {
957 for (plane = 0; plane < s->planes && !ret; plane++) {
959 int lowpass_height = s->plane[plane].band[0][0].height;
960 int lowpass_width = s->plane[plane].band[0][0].width;
961 int highpass_stride = s->plane[plane].band[0][1].stride;
962 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
963 ptrdiff_t dst_linesize;
964 int16_t *low, *high, *output, *dst;
966 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
968 dst_linesize = pic->linesize[act_plane];
970 dst_linesize = pic->linesize[act_plane] / 2;
973 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
974 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
975 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
976 ret = AVERROR(EINVAL);
980 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
982 low = s->plane[plane].subband[0];
983 high = s->plane[plane].subband[2];
984 output = s->plane[plane].l_h[0];
985 for (i = 0; i < lowpass_width; i++) {
986 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
992 low = s->plane[plane].subband[1];
993 high = s->plane[plane].subband[3];
994 output = s->plane[plane].l_h[1];
996 for (i = 0; i < lowpass_width; i++) {
997 // note the stride of "low" is highpass_stride
998 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1004 low = s->plane[plane].l_h[0];
1005 high = s->plane[plane].l_h[1];
1006 output = s->plane[plane].subband[0];
1007 for (i = 0; i < lowpass_height * 2; i++) {
1008 horiz_filter(output, low, high, lowpass_width);
1009 low += lowpass_width;
1010 high += lowpass_width;
1011 output += lowpass_width * 2;
1014 output = s->plane[plane].subband[0];
1015 for (i = 0; i < lowpass_height * 2; i++) {
1016 for (j = 0; j < lowpass_width * 2; j++)
1019 output += lowpass_width * 2;
1024 lowpass_height = s->plane[plane].band[1][1].height;
1025 lowpass_width = s->plane[plane].band[1][1].width;
1026 highpass_stride = s->plane[plane].band[1][1].stride;
1028 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1029 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1030 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1031 ret = AVERROR(EINVAL);
1035 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1037 low = s->plane[plane].subband[0];
1038 high = s->plane[plane].subband[5];
1039 output = s->plane[plane].l_h[3];
1040 for (i = 0; i < lowpass_width; i++) {
1041 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1047 low = s->plane[plane].subband[4];
1048 high = s->plane[plane].subband[6];
1049 output = s->plane[plane].l_h[4];
1050 for (i = 0; i < lowpass_width; i++) {
1051 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1057 low = s->plane[plane].l_h[3];
1058 high = s->plane[plane].l_h[4];
1059 output = s->plane[plane].subband[0];
1060 for (i = 0; i < lowpass_height * 2; i++) {
1061 horiz_filter(output, low, high, lowpass_width);
1062 low += lowpass_width;
1063 high += lowpass_width;
1064 output += lowpass_width * 2;
1067 output = s->plane[plane].subband[0];
1068 for (i = 0; i < lowpass_height * 2; i++) {
1069 for (j = 0; j < lowpass_width * 2; j++)
1072 output += lowpass_width * 2;
1076 lowpass_height = s->plane[plane].band[2][1].height;
1077 lowpass_width = s->plane[plane].band[2][1].width;
1078 highpass_stride = s->plane[plane].band[2][1].stride;
1080 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1081 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
1082 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1083 ret = AVERROR(EINVAL);
1087 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1088 if (s->progressive) {
1089 low = s->plane[plane].subband[0];
1090 high = s->plane[plane].subband[8];
1091 output = s->plane[plane].l_h[6];
1092 for (i = 0; i < lowpass_width; i++) {
1093 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1099 low = s->plane[plane].subband[7];
1100 high = s->plane[plane].subband[9];
1101 output = s->plane[plane].l_h[7];
1102 for (i = 0; i < lowpass_width; i++) {
1103 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1109 dst = (int16_t *)pic->data[act_plane];
1110 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1114 dst += pic->linesize[act_plane] >> 1;
1116 low = s->plane[plane].l_h[6];
1117 high = s->plane[plane].l_h[7];
1119 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1120 (lowpass_height * 2 > avctx->coded_height / 2 ||
1121 lowpass_width * 2 > avctx->coded_width / 2 )
1123 ret = AVERROR_INVALIDDATA;
1127 for (i = 0; i < lowpass_height * 2; i++) {
1128 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1129 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1131 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1132 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1133 process_alpha(dst, lowpass_width * 2);
1134 low += lowpass_width;
1135 high += lowpass_width;
1136 dst += dst_linesize;
1139 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1140 pic->interlaced_frame = 1;
1141 low = s->plane[plane].subband[0];
1142 high = s->plane[plane].subband[7];
1143 output = s->plane[plane].l_h[6];
1144 for (i = 0; i < lowpass_height; i++) {
1145 horiz_filter(output, low, high, lowpass_width);
1146 low += lowpass_width;
1147 high += lowpass_width;
1148 output += lowpass_width * 2;
1151 low = s->plane[plane].subband[8];
1152 high = s->plane[plane].subband[9];
1153 output = s->plane[plane].l_h[7];
1154 for (i = 0; i < lowpass_height; i++) {
1155 horiz_filter(output, low, high, lowpass_width);
1156 low += lowpass_width;
1157 high += lowpass_width;
1158 output += lowpass_width * 2;
1161 dst = (int16_t *)pic->data[act_plane];
1162 low = s->plane[plane].l_h[6];
1163 high = s->plane[plane].l_h[7];
1164 for (i = 0; i < lowpass_height; i++) {
1165 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1166 low += lowpass_width * 2;
1167 high += lowpass_width * 2;
1168 dst += pic->linesize[act_plane];
1172 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1173 for (plane = 0; plane < s->planes && !ret; plane++) {
1174 int lowpass_height = s->plane[plane].band[0][0].height;
1175 int lowpass_width = s->plane[plane].band[0][0].width;
1176 int highpass_stride = s->plane[plane].band[0][1].stride;
1177 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1178 int16_t *low, *high, *output, *dst;
1179 ptrdiff_t dst_linesize;
1181 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1183 dst_linesize = pic->linesize[act_plane];
1185 dst_linesize = pic->linesize[act_plane] / 2;
1188 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1189 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1190 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1191 ret = AVERROR(EINVAL);
1195 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1197 low = s->plane[plane].subband[0];
1198 high = s->plane[plane].subband[2];
1199 output = s->plane[plane].l_h[0];
1200 for (i = 0; i < lowpass_width; i++) {
1201 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1207 low = s->plane[plane].subband[1];
1208 high = s->plane[plane].subband[3];
1209 output = s->plane[plane].l_h[1];
1210 for (i = 0; i < lowpass_width; i++) {
1211 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1217 low = s->plane[plane].l_h[0];
1218 high = s->plane[plane].l_h[1];
1219 output = s->plane[plane].l_h[7];
1220 for (i = 0; i < lowpass_height * 2; i++) {
1221 horiz_filter(output, low, high, lowpass_width);
1222 low += lowpass_width;
1223 high += lowpass_width;
1224 output += lowpass_width * 2;
1227 output = s->plane[plane].l_h[7];
1228 for (i = 0; i < lowpass_height * 2; i++) {
1229 for (j = 0; j < lowpass_width * 2; j++)
1232 output += lowpass_width * 2;
1236 lowpass_height = s->plane[plane].band[1][1].height;
1237 lowpass_width = s->plane[plane].band[1][1].width;
1238 highpass_stride = s->plane[plane].band[1][1].stride;
1240 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1241 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1242 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1243 ret = AVERROR(EINVAL);
1247 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1249 low = s->plane[plane].l_h[7];
1250 high = s->plane[plane].subband[5];
1251 output = s->plane[plane].l_h[3];
1252 for (i = 0; i < lowpass_width; i++) {
1253 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1259 low = s->plane[plane].subband[4];
1260 high = s->plane[plane].subband[6];
1261 output = s->plane[plane].l_h[4];
1262 for (i = 0; i < lowpass_width; i++) {
1263 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1269 low = s->plane[plane].l_h[3];
1270 high = s->plane[plane].l_h[4];
1271 output = s->plane[plane].l_h[7];
1272 for (i = 0; i < lowpass_height * 2; i++) {
1273 horiz_filter(output, low, high, lowpass_width);
1274 low += lowpass_width;
1275 high += lowpass_width;
1276 output += lowpass_width * 2;
1279 output = s->plane[plane].l_h[7];
1280 for (i = 0; i < lowpass_height * 2; i++) {
1281 for (j = 0; j < lowpass_width * 2; j++)
1283 output += lowpass_width * 2;
1286 low = s->plane[plane].subband[7];
1287 high = s->plane[plane].subband[9];
1288 output = s->plane[plane].l_h[3];
1289 for (i = 0; i < lowpass_width; i++) {
1290 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1296 low = s->plane[plane].subband[8];
1297 high = s->plane[plane].subband[10];
1298 output = s->plane[plane].l_h[4];
1299 for (i = 0; i < lowpass_width; i++) {
1300 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1306 low = s->plane[plane].l_h[3];
1307 high = s->plane[plane].l_h[4];
1308 output = s->plane[plane].l_h[9];
1309 for (i = 0; i < lowpass_height * 2; i++) {
1310 horiz_filter(output, low, high, lowpass_width);
1311 low += lowpass_width;
1312 high += lowpass_width;
1313 output += lowpass_width * 2;
1316 lowpass_height = s->plane[plane].band[4][1].height;
1317 lowpass_width = s->plane[plane].band[4][1].width;
1318 highpass_stride = s->plane[plane].band[4][1].stride;
1319 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1321 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1322 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1323 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1324 ret = AVERROR(EINVAL);
1328 low = s->plane[plane].l_h[7];
1329 high = s->plane[plane].l_h[9];
1330 output = s->plane[plane].l_h[7];
1331 for (i = 0; i < lowpass_height; i++) {
1332 inverse_temporal_filter(output, low, high, lowpass_width);
1333 low += lowpass_width;
1334 high += lowpass_width;
1336 if (s->progressive) {
1337 low = s->plane[plane].l_h[7];
1338 high = s->plane[plane].subband[15];
1339 output = s->plane[plane].l_h[6];
1340 for (i = 0; i < lowpass_width; i++) {
1341 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1347 low = s->plane[plane].subband[14];
1348 high = s->plane[plane].subband[16];
1349 output = s->plane[plane].l_h[7];
1350 for (i = 0; i < lowpass_width; i++) {
1351 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1357 low = s->plane[plane].l_h[9];
1358 high = s->plane[plane].subband[12];
1359 output = s->plane[plane].l_h[8];
1360 for (i = 0; i < lowpass_width; i++) {
1361 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1367 low = s->plane[plane].subband[11];
1368 high = s->plane[plane].subband[13];
1369 output = s->plane[plane].l_h[9];
1370 for (i = 0; i < lowpass_width; i++) {
1371 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1377 if (s->sample_type == 1)
1380 dst = (int16_t *)pic->data[act_plane];
1381 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1385 dst += pic->linesize[act_plane] >> 1;
1388 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1389 (lowpass_height * 2 > avctx->coded_height / 2 ||
1390 lowpass_width * 2 > avctx->coded_width / 2 )
1392 ret = AVERROR_INVALIDDATA;
1396 low = s->plane[plane].l_h[6];
1397 high = s->plane[plane].l_h[7];
1398 for (i = 0; i < lowpass_height * 2; i++) {
1399 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1400 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1402 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1403 low += lowpass_width;
1404 high += lowpass_width;
1405 dst += dst_linesize;
1408 pic->interlaced_frame = 1;
1409 low = s->plane[plane].l_h[7];
1410 high = s->plane[plane].subband[14];
1411 output = s->plane[plane].l_h[6];
1412 for (i = 0; i < lowpass_height; i++) {
1413 horiz_filter(output, low, high, lowpass_width);
1414 low += lowpass_width;
1415 high += lowpass_width;
1416 output += lowpass_width * 2;
1419 low = s->plane[plane].subband[15];
1420 high = s->plane[plane].subband[16];
1421 output = s->plane[plane].l_h[7];
1422 for (i = 0; i < lowpass_height; i++) {
1423 horiz_filter(output, low, high, lowpass_width);
1424 low += lowpass_width;
1425 high += lowpass_width;
1426 output += lowpass_width * 2;
1429 low = s->plane[plane].l_h[9];
1430 high = s->plane[plane].subband[11];
1431 output = s->plane[plane].l_h[8];
1432 for (i = 0; i < lowpass_height; i++) {
1433 horiz_filter(output, low, high, lowpass_width);
1434 low += lowpass_width;
1435 high += lowpass_width;
1436 output += lowpass_width * 2;
1439 low = s->plane[plane].subband[12];
1440 high = s->plane[plane].subband[13];
1441 output = s->plane[plane].l_h[9];
1442 for (i = 0; i < lowpass_height; i++) {
1443 horiz_filter(output, low, high, lowpass_width);
1444 low += lowpass_width;
1445 high += lowpass_width;
1446 output += lowpass_width * 2;
1449 if (s->sample_type == 1)
1452 dst = (int16_t *)pic->data[act_plane];
1453 low = s->plane[plane].l_h[6];
1454 high = s->plane[plane].l_h[7];
1455 for (i = 0; i < lowpass_height; i++) {
1456 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1457 low += lowpass_width * 2;
1458 high += lowpass_width * 2;
1459 dst += pic->linesize[act_plane];
1465 if (s->transform_type == 2 && s->sample_type == 1) {
1466 int16_t *low, *high, *dst;
1467 int lowpass_height, lowpass_width, highpass_stride;
1468 ptrdiff_t dst_linesize;
1470 for (plane = 0; plane < s->planes; plane++) {
1471 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1473 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1475 dst_linesize = pic->linesize[act_plane];
1477 dst_linesize = pic->linesize[act_plane] / 2;
1480 lowpass_height = s->plane[plane].band[4][1].height;
1481 lowpass_width = s->plane[plane].band[4][1].width;
1482 highpass_stride = s->plane[plane].band[4][1].stride;
1484 if (s->progressive) {
1485 dst = (int16_t *)pic->data[act_plane];
1486 low = s->plane[plane].l_h[8];
1487 high = s->plane[plane].l_h[9];
1489 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1493 dst += pic->linesize[act_plane] >> 1;
1496 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1497 (lowpass_height * 2 > avctx->coded_height / 2 ||
1498 lowpass_width * 2 > avctx->coded_width / 2 )
1500 ret = AVERROR_INVALIDDATA;
1504 for (i = 0; i < lowpass_height * 2; i++) {
1505 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1506 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1508 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1509 low += lowpass_width;
1510 high += lowpass_width;
1511 dst += dst_linesize;
1514 dst = (int16_t *)pic->data[act_plane];
1515 low = s->plane[plane].l_h[8];
1516 high = s->plane[plane].l_h[9];
1517 for (i = 0; i < lowpass_height; i++) {
1518 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1519 low += lowpass_width * 2;
1520 high += lowpass_width * 2;
1521 dst += pic->linesize[act_plane];
1527 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1528 process_bayer(pic, s->bpc);
1537 static av_cold int cfhd_close(AVCodecContext *avctx)
1539 CFHDContext *s = avctx->priv_data;
1543 ff_free_vlc(&s->vlc_9);
1544 ff_free_vlc(&s->vlc_18);
1550 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1552 CFHDContext *psrc = src->priv_data;
1553 CFHDContext *pdst = dst->priv_data;
1556 if (dst == src || psrc->transform_type == 0)
1559 pdst->a_format = psrc->a_format;
1560 pdst->a_width = psrc->a_width;
1561 pdst->a_height = psrc->a_height;
1562 pdst->transform_type = psrc->transform_type;
1563 pdst->progressive = psrc->progressive;
1564 pdst->planes = psrc->planes;
1566 if (!pdst->plane[0].idwt_buf) {
1567 pdst->coded_width = pdst->a_width;
1568 pdst->coded_height = pdst->a_height;
1569 pdst->coded_format = pdst->a_format;
1570 ret = alloc_buffers(dst);
1575 for (int plane = 0; plane < pdst->planes; plane++) {
1576 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1577 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1578 pdst->plane[plane].idwt_size * sizeof(int16_t));
1585 AVCodec ff_cfhd_decoder = {
1587 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1588 .type = AVMEDIA_TYPE_VIDEO,
1589 .id = AV_CODEC_ID_CFHD,
1590 .priv_data_size = sizeof(CFHDContext),
1592 .close = cfhd_close,
1593 .decode = cfhd_decode,
1594 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1595 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1596 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,