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, "Sample index table - skipping %i values\n", data);
570 if (data > bytestream2_get_bytes_left(&gb) / 4) {
571 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
572 ret = AVERROR_INVALIDDATA;
575 for (i = 0; i < data; i++) {
576 uint32_t offset = bytestream2_get_be32(&gb);
577 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
579 } else if (tag == HighpassWidth) {
580 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);
582 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
583 ret = AVERROR(EINVAL);
586 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
587 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
588 } else if (tag == HighpassHeight) {
589 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
591 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
592 ret = AVERROR(EINVAL);
595 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
596 } else if (tag == BandWidth) {
597 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
599 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
600 ret = AVERROR(EINVAL);
603 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
604 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
605 } else if (tag == BandHeight) {
606 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
608 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
609 ret = AVERROR(EINVAL);
612 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
613 } else if (tag == InputFormat) {
614 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
615 if (s->coded_format == AV_PIX_FMT_NONE ||
616 s->coded_format == AV_PIX_FMT_YUV422P10) {
617 if (data >= 100 && data <= 105) {
618 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
619 } else if (data >= 122 && data <= 128) {
620 s->coded_format = AV_PIX_FMT_GBRP12;
621 } else if (data == 30) {
622 s->coded_format = AV_PIX_FMT_GBRAP12;
624 s->coded_format = AV_PIX_FMT_YUV422P10;
626 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
628 } else if (tag == BandCodingFlags) {
629 s->codebook = data & 0xf;
630 s->difference_coding = (data >> 4) & 1;
631 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
632 } else if (tag == Precision) {
633 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
634 if (!(data == 10 || data == 12)) {
635 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
636 ret = AVERROR(EINVAL);
639 avctx->bits_per_raw_sample = s->bpc = data;
640 } else if (tag == EncodedFormat) {
641 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
643 s->coded_format = AV_PIX_FMT_YUV422P10;
644 } else if (data == 2) {
645 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
646 } else if (data == 3) {
647 s->coded_format = AV_PIX_FMT_GBRP12;
648 } else if (data == 4) {
649 s->coded_format = AV_PIX_FMT_GBRAP12;
651 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
652 ret = AVERROR_PATCHWELCOME;
655 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
656 } else if (tag == -85) {
657 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
658 s->cropped_height = data;
659 } else if (tag == -75) {
660 s->peak.offset &= ~0xffff;
661 s->peak.offset |= (data & 0xffff);
664 } else if (tag == -76) {
665 s->peak.offset &= 0xffff;
666 s->peak.offset |= (data & 0xffffU)<<16;
669 } else if (tag == -74 && s->peak.offset) {
670 s->peak.level = data;
671 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
673 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
675 if (tag == BitstreamMarker && data == 0xf0f &&
676 s->coded_format != AV_PIX_FMT_NONE) {
677 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
678 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
679 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
681 if (s->coded_width) {
682 s->coded_width *= factor;
685 if (s->coded_height) {
686 s->coded_height *= factor;
689 if (!s->a_width && !s->coded_width) {
690 s->coded_width = lowpass_width * factor * 8;
693 if (!s->a_height && !s->coded_height) {
694 s->coded_height = lowpass_height * factor * 8;
697 if (s->a_width && !s->coded_width)
698 s->coded_width = s->a_width;
699 if (s->a_height && !s->coded_height)
700 s->coded_height = s->a_height;
702 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
703 s->a_format != s->coded_format) {
705 if ((ret = alloc_buffers(avctx)) < 0) {
710 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
713 if (s->cropped_height) {
714 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
715 if (avctx->height < height)
716 return AVERROR_INVALIDDATA;
717 avctx->height = height;
722 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
727 s->coded_format = AV_PIX_FMT_NONE;
729 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
733 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
737 s->coded_format = AV_PIX_FMT_NONE;
741 if (s->subband_num_actual == 255)
743 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
745 /* Lowpass coefficients */
746 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
747 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
748 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
749 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
750 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
752 if (lowpass_width < 3 ||
753 lowpass_width > lowpass_a_width) {
754 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
755 ret = AVERROR(EINVAL);
759 if (lowpass_height < 3 ||
760 lowpass_height > lowpass_a_height) {
761 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
762 ret = AVERROR(EINVAL);
767 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
768 ret = AVERROR(EINVAL);
772 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
773 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
774 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
775 ret = AVERROR(EINVAL);
779 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
780 for (i = 0; i < lowpass_height; i++) {
781 for (j = 0; j < lowpass_width; j++)
782 coeff_data[j] = bytestream2_get_be16u(&gb);
784 coeff_data += lowpass_width;
787 /* Align to mod-4 position to continue reading tags */
788 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
790 /* Copy last line of coefficients if odd height */
791 if (lowpass_height & 1) {
792 memcpy(&coeff_data[lowpass_height * lowpass_width],
793 &coeff_data[(lowpass_height - 1) * lowpass_width],
794 lowpass_width * sizeof(*coeff_data));
797 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
800 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
801 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
802 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
803 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
804 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
805 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
807 int a_expected = highpass_a_height * highpass_a_width;
808 int level, run, coeff;
809 int count = 0, bytes;
812 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
813 ret = AVERROR(EINVAL);
817 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
818 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
819 ret = AVERROR(EINVAL);
822 expected = highpass_height * highpass_stride;
824 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);
826 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
830 OPEN_READER(re, &s->gb);
832 const int lossless = s->band_encoding == 5;
834 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
838 UPDATE_CACHE(re, &s->gb);
839 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
848 if (count > expected)
852 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
855 if (tag == BandSecondPass) {
856 const uint16_t q = s->quantisation;
858 for (i = 0; i < run; i++) {
859 *coeff_data |= coeff << 8;
863 for (i = 0; i < run; i++)
864 *coeff_data++ = coeff;
869 UPDATE_CACHE(re, &s->gb);
870 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
874 if (level == 255 && run == 2)
879 if (count > expected)
883 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
886 if (tag == BandSecondPass) {
887 const uint16_t q = s->quantisation;
889 for (i = 0; i < run; i++) {
890 *coeff_data |= coeff << 8;
894 for (i = 0; i < run; i++)
895 *coeff_data++ = coeff;
899 CLOSE_READER(re, &s->gb);
902 if (count > expected) {
903 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
904 ret = AVERROR(EINVAL);
908 peak_table(coeff_data - count, &s->peak, count);
909 if (s->difference_coding)
910 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
912 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
913 if (bytes > bytestream2_get_bytes_left(&gb)) {
914 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
915 ret = AVERROR(EINVAL);
918 bytestream2_seek(&gb, bytes, SEEK_CUR);
920 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
922 if (s->subband_num_actual != 255)
925 /* Copy last line of coefficients if odd height */
926 if (highpass_height & 1) {
927 memcpy(&coeff_data[highpass_height * highpass_stride],
928 &coeff_data[(highpass_height - 1) * highpass_stride],
929 highpass_stride * sizeof(*coeff_data));
934 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
935 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
940 ff_thread_finish_setup(avctx);
942 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
943 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
944 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
945 ret = AVERROR(EINVAL);
950 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
951 ret = AVERROR(EINVAL);
955 if (s->transform_type == 0 && s->sample_type != 1) {
956 for (plane = 0; plane < s->planes && !ret; plane++) {
958 int lowpass_height = s->plane[plane].band[0][0].height;
959 int lowpass_width = s->plane[plane].band[0][0].width;
960 int highpass_stride = s->plane[plane].band[0][1].stride;
961 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
962 ptrdiff_t dst_linesize;
963 int16_t *low, *high, *output, *dst;
965 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
967 dst_linesize = pic->linesize[act_plane];
969 dst_linesize = pic->linesize[act_plane] / 2;
972 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
973 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
974 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
975 ret = AVERROR(EINVAL);
979 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
981 low = s->plane[plane].subband[0];
982 high = s->plane[plane].subband[2];
983 output = s->plane[plane].l_h[0];
984 for (i = 0; i < lowpass_width; i++) {
985 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
991 low = s->plane[plane].subband[1];
992 high = s->plane[plane].subband[3];
993 output = s->plane[plane].l_h[1];
995 for (i = 0; i < lowpass_width; i++) {
996 // note the stride of "low" is highpass_stride
997 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1003 low = s->plane[plane].l_h[0];
1004 high = s->plane[plane].l_h[1];
1005 output = s->plane[plane].subband[0];
1006 for (i = 0; i < lowpass_height * 2; i++) {
1007 horiz_filter(output, low, high, lowpass_width);
1008 low += lowpass_width;
1009 high += lowpass_width;
1010 output += lowpass_width * 2;
1013 output = s->plane[plane].subband[0];
1014 for (i = 0; i < lowpass_height * 2; i++) {
1015 for (j = 0; j < lowpass_width * 2; j++)
1018 output += lowpass_width * 2;
1023 lowpass_height = s->plane[plane].band[1][1].height;
1024 lowpass_width = s->plane[plane].band[1][1].width;
1025 highpass_stride = s->plane[plane].band[1][1].stride;
1027 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1028 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1029 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1030 ret = AVERROR(EINVAL);
1034 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1036 low = s->plane[plane].subband[0];
1037 high = s->plane[plane].subband[5];
1038 output = s->plane[plane].l_h[3];
1039 for (i = 0; i < lowpass_width; i++) {
1040 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1046 low = s->plane[plane].subband[4];
1047 high = s->plane[plane].subband[6];
1048 output = s->plane[plane].l_h[4];
1049 for (i = 0; i < lowpass_width; i++) {
1050 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1056 low = s->plane[plane].l_h[3];
1057 high = s->plane[plane].l_h[4];
1058 output = s->plane[plane].subband[0];
1059 for (i = 0; i < lowpass_height * 2; i++) {
1060 horiz_filter(output, low, high, lowpass_width);
1061 low += lowpass_width;
1062 high += lowpass_width;
1063 output += lowpass_width * 2;
1066 output = s->plane[plane].subband[0];
1067 for (i = 0; i < lowpass_height * 2; i++) {
1068 for (j = 0; j < lowpass_width * 2; j++)
1071 output += lowpass_width * 2;
1075 lowpass_height = s->plane[plane].band[2][1].height;
1076 lowpass_width = s->plane[plane].band[2][1].width;
1077 highpass_stride = s->plane[plane].band[2][1].stride;
1079 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1080 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
1081 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1082 ret = AVERROR(EINVAL);
1086 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1087 if (s->progressive) {
1088 low = s->plane[plane].subband[0];
1089 high = s->plane[plane].subband[8];
1090 output = s->plane[plane].l_h[6];
1091 for (i = 0; i < lowpass_width; i++) {
1092 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1098 low = s->plane[plane].subband[7];
1099 high = s->plane[plane].subband[9];
1100 output = s->plane[plane].l_h[7];
1101 for (i = 0; i < lowpass_width; i++) {
1102 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1108 dst = (int16_t *)pic->data[act_plane];
1109 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1113 dst += pic->linesize[act_plane] >> 1;
1115 low = s->plane[plane].l_h[6];
1116 high = s->plane[plane].l_h[7];
1118 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1119 (lowpass_height * 2 > avctx->coded_height / 2 ||
1120 lowpass_width * 2 > avctx->coded_width / 2 )
1122 ret = AVERROR_INVALIDDATA;
1126 for (i = 0; i < lowpass_height * 2; i++) {
1127 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1128 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1130 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1131 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1132 process_alpha(dst, lowpass_width * 2);
1133 low += lowpass_width;
1134 high += lowpass_width;
1135 dst += dst_linesize;
1138 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1139 pic->interlaced_frame = 1;
1140 low = s->plane[plane].subband[0];
1141 high = s->plane[plane].subband[7];
1142 output = s->plane[plane].l_h[6];
1143 for (i = 0; i < lowpass_height; i++) {
1144 horiz_filter(output, low, high, lowpass_width);
1145 low += lowpass_width;
1146 high += lowpass_width;
1147 output += lowpass_width * 2;
1150 low = s->plane[plane].subband[8];
1151 high = s->plane[plane].subband[9];
1152 output = s->plane[plane].l_h[7];
1153 for (i = 0; i < lowpass_height; i++) {
1154 horiz_filter(output, low, high, lowpass_width);
1155 low += lowpass_width;
1156 high += lowpass_width;
1157 output += lowpass_width * 2;
1160 dst = (int16_t *)pic->data[act_plane];
1161 low = s->plane[plane].l_h[6];
1162 high = s->plane[plane].l_h[7];
1163 for (i = 0; i < lowpass_height; i++) {
1164 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1165 low += lowpass_width * 2;
1166 high += lowpass_width * 2;
1167 dst += pic->linesize[act_plane];
1171 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1172 for (plane = 0; plane < s->planes && !ret; plane++) {
1173 int lowpass_height = s->plane[plane].band[0][0].height;
1174 int lowpass_width = s->plane[plane].band[0][0].width;
1175 int highpass_stride = s->plane[plane].band[0][1].stride;
1176 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1177 int16_t *low, *high, *output, *dst;
1178 ptrdiff_t dst_linesize;
1180 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1182 dst_linesize = pic->linesize[act_plane];
1184 dst_linesize = pic->linesize[act_plane] / 2;
1187 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1188 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1189 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1190 ret = AVERROR(EINVAL);
1194 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1196 low = s->plane[plane].subband[0];
1197 high = s->plane[plane].subband[2];
1198 output = s->plane[plane].l_h[0];
1199 for (i = 0; i < lowpass_width; i++) {
1200 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1206 low = s->plane[plane].subband[1];
1207 high = s->plane[plane].subband[3];
1208 output = s->plane[plane].l_h[1];
1209 for (i = 0; i < lowpass_width; i++) {
1210 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1216 low = s->plane[plane].l_h[0];
1217 high = s->plane[plane].l_h[1];
1218 output = s->plane[plane].l_h[7];
1219 for (i = 0; i < lowpass_height * 2; i++) {
1220 horiz_filter(output, low, high, lowpass_width);
1221 low += lowpass_width;
1222 high += lowpass_width;
1223 output += lowpass_width * 2;
1226 output = s->plane[plane].l_h[7];
1227 for (i = 0; i < lowpass_height * 2; i++) {
1228 for (j = 0; j < lowpass_width * 2; j++)
1231 output += lowpass_width * 2;
1235 lowpass_height = s->plane[plane].band[1][1].height;
1236 lowpass_width = s->plane[plane].band[1][1].width;
1237 highpass_stride = s->plane[plane].band[1][1].stride;
1239 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1240 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1241 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1242 ret = AVERROR(EINVAL);
1246 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1248 low = s->plane[plane].l_h[7];
1249 high = s->plane[plane].subband[5];
1250 output = s->plane[plane].l_h[3];
1251 for (i = 0; i < lowpass_width; i++) {
1252 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1258 low = s->plane[plane].subband[4];
1259 high = s->plane[plane].subband[6];
1260 output = s->plane[plane].l_h[4];
1261 for (i = 0; i < lowpass_width; i++) {
1262 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1268 low = s->plane[plane].l_h[3];
1269 high = s->plane[plane].l_h[4];
1270 output = s->plane[plane].l_h[7];
1271 for (i = 0; i < lowpass_height * 2; i++) {
1272 horiz_filter(output, low, high, lowpass_width);
1273 low += lowpass_width;
1274 high += lowpass_width;
1275 output += lowpass_width * 2;
1278 output = s->plane[plane].l_h[7];
1279 for (i = 0; i < lowpass_height * 2; i++) {
1280 for (j = 0; j < lowpass_width * 2; j++)
1282 output += lowpass_width * 2;
1285 low = s->plane[plane].subband[7];
1286 high = s->plane[plane].subband[9];
1287 output = s->plane[plane].l_h[3];
1288 for (i = 0; i < lowpass_width; i++) {
1289 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1295 low = s->plane[plane].subband[8];
1296 high = s->plane[plane].subband[10];
1297 output = s->plane[plane].l_h[4];
1298 for (i = 0; i < lowpass_width; i++) {
1299 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1305 low = s->plane[plane].l_h[3];
1306 high = s->plane[plane].l_h[4];
1307 output = s->plane[plane].l_h[9];
1308 for (i = 0; i < lowpass_height * 2; i++) {
1309 horiz_filter(output, low, high, lowpass_width);
1310 low += lowpass_width;
1311 high += lowpass_width;
1312 output += lowpass_width * 2;
1315 lowpass_height = s->plane[plane].band[4][1].height;
1316 lowpass_width = s->plane[plane].band[4][1].width;
1317 highpass_stride = s->plane[plane].band[4][1].stride;
1318 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1320 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1321 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1322 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1323 ret = AVERROR(EINVAL);
1327 low = s->plane[plane].l_h[7];
1328 high = s->plane[plane].l_h[9];
1329 output = s->plane[plane].l_h[7];
1330 for (i = 0; i < lowpass_height; i++) {
1331 inverse_temporal_filter(output, low, high, lowpass_width);
1332 low += lowpass_width;
1333 high += lowpass_width;
1335 if (s->progressive) {
1336 low = s->plane[plane].l_h[7];
1337 high = s->plane[plane].subband[15];
1338 output = s->plane[plane].l_h[6];
1339 for (i = 0; i < lowpass_width; i++) {
1340 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1346 low = s->plane[plane].subband[14];
1347 high = s->plane[plane].subband[16];
1348 output = s->plane[plane].l_h[7];
1349 for (i = 0; i < lowpass_width; i++) {
1350 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1356 low = s->plane[plane].l_h[9];
1357 high = s->plane[plane].subband[12];
1358 output = s->plane[plane].l_h[8];
1359 for (i = 0; i < lowpass_width; i++) {
1360 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1366 low = s->plane[plane].subband[11];
1367 high = s->plane[plane].subband[13];
1368 output = s->plane[plane].l_h[9];
1369 for (i = 0; i < lowpass_width; i++) {
1370 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1376 if (s->sample_type == 1)
1379 dst = (int16_t *)pic->data[act_plane];
1380 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1384 dst += pic->linesize[act_plane] >> 1;
1387 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1388 (lowpass_height * 2 > avctx->coded_height / 2 ||
1389 lowpass_width * 2 > avctx->coded_width / 2 )
1391 ret = AVERROR_INVALIDDATA;
1395 low = s->plane[plane].l_h[6];
1396 high = s->plane[plane].l_h[7];
1397 for (i = 0; i < lowpass_height * 2; i++) {
1398 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1399 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1401 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1402 low += lowpass_width;
1403 high += lowpass_width;
1404 dst += dst_linesize;
1407 pic->interlaced_frame = 1;
1408 low = s->plane[plane].l_h[7];
1409 high = s->plane[plane].subband[14];
1410 output = s->plane[plane].l_h[6];
1411 for (i = 0; i < lowpass_height; i++) {
1412 horiz_filter(output, low, high, lowpass_width);
1413 low += lowpass_width;
1414 high += lowpass_width;
1415 output += lowpass_width * 2;
1418 low = s->plane[plane].subband[15];
1419 high = s->plane[plane].subband[16];
1420 output = s->plane[plane].l_h[7];
1421 for (i = 0; i < lowpass_height; i++) {
1422 horiz_filter(output, low, high, lowpass_width);
1423 low += lowpass_width;
1424 high += lowpass_width;
1425 output += lowpass_width * 2;
1428 low = s->plane[plane].l_h[9];
1429 high = s->plane[plane].subband[11];
1430 output = s->plane[plane].l_h[8];
1431 for (i = 0; i < lowpass_height; i++) {
1432 horiz_filter(output, low, high, lowpass_width);
1433 low += lowpass_width;
1434 high += lowpass_width;
1435 output += lowpass_width * 2;
1438 low = s->plane[plane].subband[12];
1439 high = s->plane[plane].subband[13];
1440 output = s->plane[plane].l_h[9];
1441 for (i = 0; i < lowpass_height; i++) {
1442 horiz_filter(output, low, high, lowpass_width);
1443 low += lowpass_width;
1444 high += lowpass_width;
1445 output += lowpass_width * 2;
1448 if (s->sample_type == 1)
1451 dst = (int16_t *)pic->data[act_plane];
1452 low = s->plane[plane].l_h[6];
1453 high = s->plane[plane].l_h[7];
1454 for (i = 0; i < lowpass_height; i++) {
1455 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1456 low += lowpass_width * 2;
1457 high += lowpass_width * 2;
1458 dst += pic->linesize[act_plane];
1464 if (s->transform_type == 2 && s->sample_type == 1) {
1465 int16_t *low, *high, *dst;
1466 int lowpass_height, lowpass_width, highpass_stride;
1467 ptrdiff_t dst_linesize;
1469 for (plane = 0; plane < s->planes; plane++) {
1470 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1472 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1474 dst_linesize = pic->linesize[act_plane];
1476 dst_linesize = pic->linesize[act_plane] / 2;
1479 lowpass_height = s->plane[plane].band[4][1].height;
1480 lowpass_width = s->plane[plane].band[4][1].width;
1481 highpass_stride = s->plane[plane].band[4][1].stride;
1483 if (s->progressive) {
1484 dst = (int16_t *)pic->data[act_plane];
1485 low = s->plane[plane].l_h[8];
1486 high = s->plane[plane].l_h[9];
1488 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1492 dst += pic->linesize[act_plane] >> 1;
1495 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1496 (lowpass_height * 2 > avctx->coded_height / 2 ||
1497 lowpass_width * 2 > avctx->coded_width / 2 )
1499 ret = AVERROR_INVALIDDATA;
1503 for (i = 0; i < lowpass_height * 2; i++) {
1504 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1505 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1507 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1508 low += lowpass_width;
1509 high += lowpass_width;
1510 dst += dst_linesize;
1513 dst = (int16_t *)pic->data[act_plane];
1514 low = s->plane[plane].l_h[8];
1515 high = s->plane[plane].l_h[9];
1516 for (i = 0; i < lowpass_height; i++) {
1517 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1518 low += lowpass_width * 2;
1519 high += lowpass_width * 2;
1520 dst += pic->linesize[act_plane];
1526 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1527 process_bayer(pic, s->bpc);
1536 static av_cold int cfhd_close(AVCodecContext *avctx)
1538 CFHDContext *s = avctx->priv_data;
1542 ff_free_vlc(&s->vlc_9);
1543 ff_free_vlc(&s->vlc_18);
1549 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1551 CFHDContext *psrc = src->priv_data;
1552 CFHDContext *pdst = dst->priv_data;
1555 if (dst == src || psrc->transform_type == 0)
1558 pdst->a_format = psrc->a_format;
1559 pdst->a_width = psrc->a_width;
1560 pdst->a_height = psrc->a_height;
1561 pdst->transform_type = psrc->transform_type;
1562 pdst->progressive = psrc->progressive;
1563 pdst->planes = psrc->planes;
1565 if (!pdst->plane[0].idwt_buf) {
1566 pdst->coded_width = pdst->a_width;
1567 pdst->coded_height = pdst->a_height;
1568 pdst->coded_format = pdst->a_format;
1569 ret = alloc_buffers(dst);
1574 for (int plane = 0; plane < pdst->planes; plane++) {
1575 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1576 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1577 pdst->plane[plane].idwt_size * sizeof(int16_t));
1584 AVCodec ff_cfhd_decoder = {
1586 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1587 .type = AVMEDIA_TYPE_VIDEO,
1588 .id = AV_CODEC_ID_CFHD,
1589 .priv_data_size = sizeof(CFHDContext),
1591 .close = cfhd_close,
1592 .decode = cfhd_decode,
1593 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1594 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1595 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,