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 if (s->transform_type != 2)
104 s->transform_type = -1;
105 init_plane_defaults(s);
106 init_peak_table_defaults(s);
109 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
111 if (codebook == 0 || codebook == 1) {
112 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
114 return level * quantisation;
117 static inline void difference_coding(int16_t *band, int width, int height)
121 for (i = 0; i < height; i++) {
122 for (j = 1; j < width; j++) {
123 band[j] += band[j-1];
129 static inline void peak_table(int16_t *band, Peak *peak, int length)
132 for (i = 0; i < length; i++)
133 if (abs(band[i]) > peak->level)
134 band[i] = bytestream2_get_le16(&peak->base);
137 static inline void process_alpha(int16_t *alpha, int width)
140 for (i = 0; i < width; i++) {
142 channel -= ALPHA_COMPAND_DC_OFFSET;
144 channel *= ALPHA_COMPAND_GAIN;
146 channel = av_clip_uintp2(channel, 12);
151 static inline void process_bayer(AVFrame *frame, int bpc)
153 const int linesize = frame->linesize[0];
154 uint16_t *r = (uint16_t *)frame->data[0];
155 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
156 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
157 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
158 const int mid = 1 << (bpc - 1);
159 const int factor = 1 << (16 - bpc);
161 for (int y = 0; y < frame->height >> 1; y++) {
162 for (int x = 0; x < frame->width; x += 2) {
172 R = (rg - mid) * 2 + g;
175 B = (bg - mid) * 2 + g;
177 R = av_clip_uintp2(R * factor, 16);
178 G1 = av_clip_uintp2(G1 * factor, 16);
179 G2 = av_clip_uintp2(G2 * factor, 16);
180 B = av_clip_uintp2(B * factor, 16);
195 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
196 int width, int linesize, int plane)
200 for (i = 0; i < width; i++) {
201 even = (low[i] - high[i])/2;
202 odd = (low[i] + high[i])/2;
203 output[i] = av_clip_uintp2(even, 10);
204 output[i + linesize] = av_clip_uintp2(odd, 10);
208 static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
210 for (int i = 0; i < width; i++) {
211 int even = (low[i] - high[i]) / 2;
212 int odd = (low[i] + high[i]) / 2;
219 static void free_buffers(CFHDContext *s)
223 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
224 av_freep(&s->plane[i].idwt_buf);
225 av_freep(&s->plane[i].idwt_tmp);
226 s->plane[i].idwt_size = 0;
228 for (j = 0; j < SUBBAND_COUNT_3D; j++)
229 s->plane[i].subband[j] = NULL;
231 for (j = 0; j < 10; j++)
232 s->plane[i].l_h[j] = NULL;
236 s->a_transform_type = INT_MIN;
239 static int alloc_buffers(AVCodecContext *avctx)
241 CFHDContext *s = avctx->priv_data;
242 int i, j, ret, planes, bayer = 0;
243 int chroma_x_shift, chroma_y_shift;
246 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
248 avctx->pix_fmt = s->coded_format;
250 ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
252 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
254 &chroma_y_shift)) < 0)
256 planes = av_pix_fmt_count_planes(s->coded_format);
257 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
264 for (i = 0; i < planes; i++) {
265 int w8, h8, w4, h4, w2, h2;
266 int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
267 int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
268 ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
270 if (chroma_y_shift && !bayer)
271 height = FFALIGN(height / 8, 2) * 8;
272 s->plane[i].width = width;
273 s->plane[i].height = height;
274 s->plane[i].stride = stride;
276 w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
277 h8 = FFALIGN(height, 8) / 8;
283 if (s->transform_type == 0) {
284 s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
285 s->plane[i].idwt_buf =
286 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
287 s->plane[i].idwt_tmp =
288 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
290 s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
291 s->plane[i].idwt_buf =
292 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
293 s->plane[i].idwt_tmp =
294 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
297 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
298 return AVERROR(ENOMEM);
300 s->plane[i].subband[0] = s->plane[i].idwt_buf;
301 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
302 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
303 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
304 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
305 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
306 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
307 if (s->transform_type == 0) {
308 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
309 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
310 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
313 s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
314 s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
315 s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
316 s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
317 s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
318 s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
319 s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
320 s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
321 s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
322 s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
325 if (s->transform_type == 0) {
326 for (j = 0; j < DWT_LEVELS; j++) {
327 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
328 s->plane[i].band[j][k].a_width = w8 << j;
329 s->plane[i].band[j][k].a_height = h8 << j;
333 for (j = 0; j < DWT_LEVELS_3D; j++) {
334 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
336 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
337 s->plane[i].band[j][k].a_width = w8 << t;
338 s->plane[i].band[j][k].a_height = h8 << t;
343 /* ll2 and ll1 commented out because they are done in-place */
344 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
345 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
346 // s->plane[i].l_h[2] = ll2;
347 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
348 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
349 // s->plane[i].l_h[5] = ll1;
350 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
351 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
352 if (s->transform_type != 0) {
353 int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
355 s->plane[i].l_h[8] = frame2;
356 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
360 s->a_transform_type = s->transform_type;
361 s->a_height = s->coded_height;
362 s->a_width = s->coded_width;
363 s->a_format = s->coded_format;
368 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
371 CFHDContext *s = avctx->priv_data;
372 CFHDDSPContext *dsp = &s->dsp;
374 ThreadFrame frame = { .f = data };
376 int ret = 0, i, j, plane, got_buffer = 0;
379 init_frame_defaults(s);
380 s->planes = av_pix_fmt_count_planes(s->coded_format);
382 bytestream2_init(&gb, avpkt->data, avpkt->size);
384 while (bytestream2_get_bytes_left(&gb) >= 4) {
385 /* Bit weird but implement the tag parsing as the spec says */
386 uint16_t tagu = bytestream2_get_be16(&gb);
387 int16_t tag = (int16_t)tagu;
388 int8_t tag8 = (int8_t)(tagu >> 8);
389 uint16_t abstag = abs(tag);
390 int8_t abs_tag8 = abs(tag8);
391 uint16_t data = bytestream2_get_be16(&gb);
392 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
393 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
394 } else if (tag == SampleFlags) {
395 av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
396 s->progressive = data & 0x0001;
397 } else if (tag == FrameType) {
398 s->frame_type = data;
399 av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
400 } else if (abstag == VersionMajor) {
401 av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
402 } else if (abstag == VersionMinor) {
403 av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
404 } else if (abstag == VersionRevision) {
405 av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
406 } else if (abstag == VersionEdit) {
407 av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
408 } else if (abstag == Version) {
409 av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
410 } else if (tag == ImageWidth) {
411 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
412 s->coded_width = data;
413 } else if (tag == ImageHeight) {
414 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
415 s->coded_height = data;
416 } else if (tag == ChannelCount) {
417 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
418 s->channel_cnt = data;
420 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
421 ret = AVERROR_PATCHWELCOME;
424 } else if (tag == SubbandCount) {
425 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
426 if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
427 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
428 ret = AVERROR_PATCHWELCOME;
431 } else if (tag == ChannelNumber) {
432 s->channel_num = data;
433 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
434 if (s->channel_num >= s->planes) {
435 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
436 ret = AVERROR(EINVAL);
439 init_plane_defaults(s);
440 } else if (tag == SubbandNumber) {
441 if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2)) // hack
443 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
444 s->subband_num = data;
445 if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
446 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
447 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
448 ret = AVERROR(EINVAL);
451 if (s->subband_num > 3) {
452 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
453 ret = AVERROR(EINVAL);
456 } else if (tag == SubbandBand) {
457 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
458 if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
459 (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
460 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
461 ret = AVERROR(EINVAL);
464 if (s->transform_type == 0 || s->transform_type == 2)
465 s->subband_num_actual = data;
467 av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
468 } else if (tag == LowpassPrecision)
469 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
470 else if (tag == Quantization) {
471 s->quantisation = data;
472 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
473 } else if (tag == PrescaleTable) {
474 for (i = 0; i < 8; i++)
475 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
476 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
477 } else if (tag == BandEncoding) {
478 if (!data || data > 5) {
479 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
480 ret = AVERROR(EINVAL);
483 s->band_encoding = data;
484 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
485 } else if (tag == LowpassWidth) {
486 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
487 s->plane[s->channel_num].band[0][0].width = data;
488 s->plane[s->channel_num].band[0][0].stride = data;
489 } else if (tag == LowpassHeight) {
490 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
491 s->plane[s->channel_num].band[0][0].height = data;
492 } else if (tag == SampleType) {
493 s->sample_type = data;
494 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
495 } else if (tag == TransformType) {
497 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
498 ret = AVERROR(EINVAL);
500 } else if (data == 1) {
501 av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
502 ret = AVERROR_PATCHWELCOME;
505 if (s->transform_type == -1) {
506 s->transform_type = data;
507 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
509 av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
511 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
512 if (abstag == 0x4001)
514 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
515 bytestream2_skipu(&gb, data * 4);
516 } else if (tag == FrameIndex) {
517 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
518 s->frame_index = data;
519 } else if (tag == SampleIndexTable) {
520 av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
521 if (data > bytestream2_get_bytes_left(&gb) / 4) {
522 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
523 ret = AVERROR_INVALIDDATA;
526 for (i = 0; i < data; i++) {
527 uint32_t offset = bytestream2_get_be32(&gb);
528 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
530 } else if (tag == HighpassWidth) {
531 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);
533 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
534 ret = AVERROR(EINVAL);
537 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
538 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
539 } else if (tag == HighpassHeight) {
540 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
542 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
543 ret = AVERROR(EINVAL);
546 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
547 } else if (tag == BandWidth) {
548 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
550 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
551 ret = AVERROR(EINVAL);
554 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
555 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
556 } else if (tag == BandHeight) {
557 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
559 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
560 ret = AVERROR(EINVAL);
563 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
564 } else if (tag == InputFormat) {
565 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
566 if (s->coded_format == AV_PIX_FMT_NONE ||
567 s->coded_format == AV_PIX_FMT_YUV422P10) {
568 if (data >= 100 && data <= 105) {
569 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
570 } else if (data >= 122 && data <= 128) {
571 s->coded_format = AV_PIX_FMT_GBRP12;
572 } else if (data == 30) {
573 s->coded_format = AV_PIX_FMT_GBRAP12;
575 s->coded_format = AV_PIX_FMT_YUV422P10;
577 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
579 } else if (tag == BandCodingFlags) {
580 s->codebook = data & 0xf;
581 s->difference_coding = (data >> 4) & 1;
582 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
583 } else if (tag == Precision) {
584 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
585 if (!(data == 10 || data == 12)) {
586 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
587 ret = AVERROR(EINVAL);
590 avctx->bits_per_raw_sample = s->bpc = data;
591 } else if (tag == EncodedFormat) {
592 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
594 s->coded_format = AV_PIX_FMT_YUV422P10;
595 } else if (data == 2) {
596 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
597 } else if (data == 3) {
598 s->coded_format = AV_PIX_FMT_GBRP12;
599 } else if (data == 4) {
600 s->coded_format = AV_PIX_FMT_GBRAP12;
602 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
603 ret = AVERROR_PATCHWELCOME;
606 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
607 } else if (tag == -DisplayHeight) {
608 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
609 s->cropped_height = data;
610 } else if (tag == -PeakOffsetLow) {
611 s->peak.offset &= ~0xffff;
612 s->peak.offset |= (data & 0xffff);
615 } else if (tag == -PeakOffsetHigh) {
616 s->peak.offset &= 0xffff;
617 s->peak.offset |= (data & 0xffffU)<<16;
620 } else if (tag == -PeakLevel && s->peak.offset) {
621 s->peak.level = data;
622 if (s->peak.offset < 4 - bytestream2_tell(&s->peak.base) ||
623 s->peak.offset > 4 + bytestream2_get_bytes_left(&s->peak.base)
625 ret = AVERROR_INVALIDDATA;
628 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
630 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
632 if (tag == BitstreamMarker && data == 0xf0f &&
633 s->coded_format != AV_PIX_FMT_NONE) {
634 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
635 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
636 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
638 if (s->coded_width) {
639 s->coded_width *= factor;
642 if (s->coded_height) {
643 s->coded_height *= factor;
646 if (!s->a_width && !s->coded_width) {
647 s->coded_width = lowpass_width * factor * 8;
650 if (!s->a_height && !s->coded_height) {
651 s->coded_height = lowpass_height * factor * 8;
654 if (s->a_width && !s->coded_width)
655 s->coded_width = s->a_width;
656 if (s->a_height && !s->coded_height)
657 s->coded_height = s->a_height;
659 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
660 s->a_format != s->coded_format ||
661 s->transform_type != s->a_transform_type) {
663 if ((ret = alloc_buffers(avctx)) < 0) {
668 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
671 if (s->cropped_height) {
672 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
673 if (avctx->height < height)
674 return AVERROR_INVALIDDATA;
675 avctx->height = height;
680 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
685 s->coded_format = AV_PIX_FMT_NONE;
687 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
691 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
695 s->coded_format = AV_PIX_FMT_NONE;
699 if (s->subband_num_actual == 255)
701 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
703 /* Lowpass coefficients */
704 if (tag == BitstreamMarker && data == 0xf0f) {
705 int lowpass_height, lowpass_width, lowpass_a_height, lowpass_a_width;
707 if (!s->a_width || !s->a_height) {
708 ret = AVERROR_INVALIDDATA;
712 lowpass_height = s->plane[s->channel_num].band[0][0].height;
713 lowpass_width = s->plane[s->channel_num].band[0][0].width;
714 lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
715 lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
717 if (lowpass_width < 3 ||
718 lowpass_width > lowpass_a_width) {
719 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
720 ret = AVERROR(EINVAL);
724 if (lowpass_height < 3 ||
725 lowpass_height > lowpass_a_height) {
726 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
727 ret = AVERROR(EINVAL);
732 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
733 ret = AVERROR(EINVAL);
737 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
738 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
739 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
740 ret = AVERROR(EINVAL);
744 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
745 for (i = 0; i < lowpass_height; i++) {
746 for (j = 0; j < lowpass_width; j++)
747 coeff_data[j] = bytestream2_get_be16u(&gb);
749 coeff_data += lowpass_width;
752 /* Align to mod-4 position to continue reading tags */
753 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
755 /* Copy last line of coefficients if odd height */
756 if (lowpass_height & 1) {
757 memcpy(&coeff_data[lowpass_height * lowpass_width],
758 &coeff_data[(lowpass_height - 1) * lowpass_width],
759 lowpass_width * sizeof(*coeff_data));
762 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
765 av_assert0(s->subband_num_actual != 255);
766 if (tag == BandHeader || tag == BandSecondPass) {
767 int highpass_height, highpass_width, highpass_a_width, highpass_a_height, highpass_stride, a_expected;
769 int level, run, coeff;
770 int count = 0, bytes;
772 if (!s->a_width || !s->a_height) {
773 ret = AVERROR_INVALIDDATA;
777 highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
778 highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
779 highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
780 highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
781 highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
782 a_expected = highpass_a_height * highpass_a_width;
785 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
786 ret = AVERROR(EINVAL);
790 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
791 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
792 ret = AVERROR(EINVAL);
795 expected = highpass_height * highpass_stride;
797 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);
799 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
803 OPEN_READER(re, &s->gb);
805 const int lossless = s->band_encoding == 5;
807 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
811 UPDATE_CACHE(re, &s->gb);
812 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
821 if (count > expected)
825 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
828 if (tag == BandSecondPass) {
829 const uint16_t q = s->quantisation;
831 for (i = 0; i < run; i++) {
832 *coeff_data |= coeff * 256;
836 for (i = 0; i < run; i++)
837 *coeff_data++ = coeff;
842 UPDATE_CACHE(re, &s->gb);
843 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
847 if (level == 255 && run == 2)
852 if (count > expected)
856 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
859 if (tag == BandSecondPass) {
860 const uint16_t q = s->quantisation;
862 for (i = 0; i < run; i++) {
863 *coeff_data |= coeff * 256;
867 for (i = 0; i < run; i++)
868 *coeff_data++ = coeff;
872 CLOSE_READER(re, &s->gb);
875 if (count > expected) {
876 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
877 ret = AVERROR(EINVAL);
881 peak_table(coeff_data - count, &s->peak, count);
882 if (s->difference_coding)
883 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
885 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
886 if (bytes > bytestream2_get_bytes_left(&gb)) {
887 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
888 ret = AVERROR(EINVAL);
891 bytestream2_seek(&gb, bytes, SEEK_CUR);
893 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
895 if (s->subband_num_actual != 255)
900 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
901 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
906 ff_thread_finish_setup(avctx);
908 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
909 s->a_transform_type == INT_MIN ||
910 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
911 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
912 ret = AVERROR(EINVAL);
917 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
918 ret = AVERROR(EINVAL);
922 if (s->transform_type == 0 && s->sample_type != 1) {
923 for (plane = 0; plane < s->planes && !ret; plane++) {
925 int lowpass_height = s->plane[plane].band[0][0].height;
926 int output_stride = s->plane[plane].band[0][0].a_width;
927 int lowpass_width = s->plane[plane].band[0][0].width;
928 int highpass_stride = s->plane[plane].band[0][1].stride;
929 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
930 ptrdiff_t dst_linesize;
931 int16_t *low, *high, *output, *dst;
933 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
935 dst_linesize = pic->linesize[act_plane];
937 dst_linesize = pic->linesize[act_plane] / 2;
940 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
941 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
942 lowpass_width < 3 || lowpass_height < 3) {
943 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
944 ret = AVERROR(EINVAL);
948 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
950 low = s->plane[plane].subband[0];
951 high = s->plane[plane].subband[2];
952 output = s->plane[plane].l_h[0];
953 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
955 low = s->plane[plane].subband[1];
956 high = s->plane[plane].subband[3];
957 output = s->plane[plane].l_h[1];
959 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
961 low = s->plane[plane].l_h[0];
962 high = s->plane[plane].l_h[1];
963 output = s->plane[plane].subband[0];
964 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
966 output = s->plane[plane].subband[0];
967 for (i = 0; i < lowpass_height * 2; i++) {
968 for (j = 0; j < lowpass_width * 2; j++)
971 output += output_stride * 2;
976 lowpass_height = s->plane[plane].band[1][1].height;
977 output_stride = s->plane[plane].band[1][1].a_width;
978 lowpass_width = s->plane[plane].band[1][1].width;
979 highpass_stride = s->plane[plane].band[1][1].stride;
981 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
982 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
983 lowpass_width < 3 || lowpass_height < 3) {
984 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
985 ret = AVERROR(EINVAL);
989 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
991 low = s->plane[plane].subband[0];
992 high = s->plane[plane].subband[5];
993 output = s->plane[plane].l_h[3];
994 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
996 low = s->plane[plane].subband[4];
997 high = s->plane[plane].subband[6];
998 output = s->plane[plane].l_h[4];
999 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1001 low = s->plane[plane].l_h[3];
1002 high = s->plane[plane].l_h[4];
1003 output = s->plane[plane].subband[0];
1004 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1006 output = s->plane[plane].subband[0];
1007 for (i = 0; i < lowpass_height * 2; i++) {
1008 for (j = 0; j < lowpass_width * 2; j++)
1011 output += output_stride * 2;
1015 lowpass_height = s->plane[plane].band[2][1].height;
1016 output_stride = s->plane[plane].band[2][1].a_width;
1017 lowpass_width = s->plane[plane].band[2][1].width;
1018 highpass_stride = s->plane[plane].band[2][1].stride;
1020 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1021 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1022 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1023 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1024 ret = AVERROR(EINVAL);
1028 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1029 if (s->progressive) {
1030 low = s->plane[plane].subband[0];
1031 high = s->plane[plane].subband[8];
1032 output = s->plane[plane].l_h[6];
1033 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1035 low = s->plane[plane].subband[7];
1036 high = s->plane[plane].subband[9];
1037 output = s->plane[plane].l_h[7];
1038 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1040 dst = (int16_t *)pic->data[act_plane];
1041 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1045 dst += pic->linesize[act_plane] >> 1;
1047 low = s->plane[plane].l_h[6];
1048 high = s->plane[plane].l_h[7];
1050 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1051 (lowpass_height * 2 > avctx->coded_height / 2 ||
1052 lowpass_width * 2 > avctx->coded_width / 2 )
1054 ret = AVERROR_INVALIDDATA;
1058 for (i = 0; i < s->plane[act_plane].height; i++) {
1059 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1060 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1061 process_alpha(dst, lowpass_width * 2);
1062 low += output_stride;
1063 high += output_stride;
1064 dst += dst_linesize;
1067 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1068 pic->interlaced_frame = 1;
1069 low = s->plane[plane].subband[0];
1070 high = s->plane[plane].subband[7];
1071 output = s->plane[plane].l_h[6];
1072 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1074 low = s->plane[plane].subband[8];
1075 high = s->plane[plane].subband[9];
1076 output = s->plane[plane].l_h[7];
1077 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1079 dst = (int16_t *)pic->data[act_plane];
1080 low = s->plane[plane].l_h[6];
1081 high = s->plane[plane].l_h[7];
1082 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1083 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1084 low += output_stride * 2;
1085 high += output_stride * 2;
1086 dst += pic->linesize[act_plane];
1090 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1091 for (plane = 0; plane < s->planes && !ret; plane++) {
1092 int lowpass_height = s->plane[plane].band[0][0].height;
1093 int output_stride = s->plane[plane].band[0][0].a_width;
1094 int lowpass_width = s->plane[plane].band[0][0].width;
1095 int highpass_stride = s->plane[plane].band[0][1].stride;
1096 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1097 int16_t *low, *high, *output, *dst;
1098 ptrdiff_t dst_linesize;
1100 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1102 dst_linesize = pic->linesize[act_plane];
1104 dst_linesize = pic->linesize[act_plane] / 2;
1107 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1108 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1109 lowpass_width < 3 || lowpass_height < 3) {
1110 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1111 ret = AVERROR(EINVAL);
1115 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1117 low = s->plane[plane].subband[0];
1118 high = s->plane[plane].subband[2];
1119 output = s->plane[plane].l_h[0];
1120 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1122 low = s->plane[plane].subband[1];
1123 high = s->plane[plane].subband[3];
1124 output = s->plane[plane].l_h[1];
1125 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1127 low = s->plane[plane].l_h[0];
1128 high = s->plane[plane].l_h[1];
1129 output = s->plane[plane].l_h[7];
1130 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1132 output = s->plane[plane].l_h[7];
1133 for (i = 0; i < lowpass_height * 2; i++) {
1134 for (j = 0; j < lowpass_width * 2; j++)
1137 output += output_stride * 2;
1141 lowpass_height = s->plane[plane].band[1][1].height;
1142 output_stride = s->plane[plane].band[1][1].a_width;
1143 lowpass_width = s->plane[plane].band[1][1].width;
1144 highpass_stride = s->plane[plane].band[1][1].stride;
1146 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1147 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1148 lowpass_width < 3 || lowpass_height < 3) {
1149 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1150 ret = AVERROR(EINVAL);
1154 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1156 low = s->plane[plane].l_h[7];
1157 high = s->plane[plane].subband[5];
1158 output = s->plane[plane].l_h[3];
1159 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1161 low = s->plane[plane].subband[4];
1162 high = s->plane[plane].subband[6];
1163 output = s->plane[plane].l_h[4];
1164 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1166 low = s->plane[plane].l_h[3];
1167 high = s->plane[plane].l_h[4];
1168 output = s->plane[plane].l_h[7];
1169 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1171 output = s->plane[plane].l_h[7];
1172 for (i = 0; i < lowpass_height * 2; i++) {
1173 for (j = 0; j < lowpass_width * 2; j++)
1175 output += output_stride * 2;
1178 low = s->plane[plane].subband[7];
1179 high = s->plane[plane].subband[9];
1180 output = s->plane[plane].l_h[3];
1181 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1183 low = s->plane[plane].subband[8];
1184 high = s->plane[plane].subband[10];
1185 output = s->plane[plane].l_h[4];
1186 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1188 low = s->plane[plane].l_h[3];
1189 high = s->plane[plane].l_h[4];
1190 output = s->plane[plane].l_h[9];
1191 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1193 lowpass_height = s->plane[plane].band[4][1].height;
1194 output_stride = s->plane[plane].band[4][1].a_width;
1195 lowpass_width = s->plane[plane].band[4][1].width;
1196 highpass_stride = s->plane[plane].band[4][1].stride;
1197 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1199 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1200 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1201 lowpass_width < 3 || lowpass_height < 3) {
1202 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1203 ret = AVERROR(EINVAL);
1207 low = s->plane[plane].l_h[7];
1208 high = s->plane[plane].l_h[9];
1209 output = s->plane[plane].l_h[7];
1210 for (i = 0; i < lowpass_height; i++) {
1211 inverse_temporal_filter(low, high, lowpass_width);
1212 low += output_stride;
1213 high += output_stride;
1215 if (s->progressive) {
1216 low = s->plane[plane].l_h[7];
1217 high = s->plane[plane].subband[15];
1218 output = s->plane[plane].l_h[6];
1219 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1221 low = s->plane[plane].subband[14];
1222 high = s->plane[plane].subband[16];
1223 output = s->plane[plane].l_h[7];
1224 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1226 low = s->plane[plane].l_h[9];
1227 high = s->plane[plane].subband[12];
1228 output = s->plane[plane].l_h[8];
1229 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1231 low = s->plane[plane].subband[11];
1232 high = s->plane[plane].subband[13];
1233 output = s->plane[plane].l_h[9];
1234 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1236 if (s->sample_type == 1)
1239 dst = (int16_t *)pic->data[act_plane];
1240 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1244 dst += pic->linesize[act_plane] >> 1;
1247 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1248 (lowpass_height * 2 > avctx->coded_height / 2 ||
1249 lowpass_width * 2 > avctx->coded_width / 2 )
1251 ret = AVERROR_INVALIDDATA;
1255 low = s->plane[plane].l_h[6];
1256 high = s->plane[plane].l_h[7];
1257 for (i = 0; i < s->plane[act_plane].height; i++) {
1258 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1259 low += output_stride;
1260 high += output_stride;
1261 dst += dst_linesize;
1264 pic->interlaced_frame = 1;
1265 low = s->plane[plane].l_h[7];
1266 high = s->plane[plane].subband[14];
1267 output = s->plane[plane].l_h[6];
1268 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1270 low = s->plane[plane].subband[15];
1271 high = s->plane[plane].subband[16];
1272 output = s->plane[plane].l_h[7];
1273 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1275 low = s->plane[plane].l_h[9];
1276 high = s->plane[plane].subband[11];
1277 output = s->plane[plane].l_h[8];
1278 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1280 low = s->plane[plane].subband[12];
1281 high = s->plane[plane].subband[13];
1282 output = s->plane[plane].l_h[9];
1283 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1285 if (s->sample_type == 1)
1288 dst = (int16_t *)pic->data[act_plane];
1289 low = s->plane[plane].l_h[6];
1290 high = s->plane[plane].l_h[7];
1291 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1292 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1293 low += output_stride * 2;
1294 high += output_stride * 2;
1295 dst += pic->linesize[act_plane];
1301 if (s->transform_type == 2 && s->sample_type == 1) {
1302 int16_t *low, *high, *dst;
1303 int output_stride, lowpass_height, lowpass_width;
1304 ptrdiff_t dst_linesize;
1306 for (plane = 0; plane < s->planes; plane++) {
1307 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1309 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1311 dst_linesize = pic->linesize[act_plane];
1313 dst_linesize = pic->linesize[act_plane] / 2;
1316 lowpass_height = s->plane[plane].band[4][1].height;
1317 output_stride = s->plane[plane].band[4][1].a_width;
1318 lowpass_width = s->plane[plane].band[4][1].width;
1320 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1321 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1322 lowpass_width < 3 || lowpass_height < 3) {
1323 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1324 ret = AVERROR(EINVAL);
1328 if (s->progressive) {
1329 dst = (int16_t *)pic->data[act_plane];
1330 low = s->plane[plane].l_h[8];
1331 high = s->plane[plane].l_h[9];
1333 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1337 dst += pic->linesize[act_plane] >> 1;
1340 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1341 (lowpass_height * 2 > avctx->coded_height / 2 ||
1342 lowpass_width * 2 > avctx->coded_width / 2 )
1344 ret = AVERROR_INVALIDDATA;
1348 for (i = 0; i < s->plane[act_plane].height; i++) {
1349 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1350 low += output_stride;
1351 high += output_stride;
1352 dst += dst_linesize;
1355 dst = (int16_t *)pic->data[act_plane];
1356 low = s->plane[plane].l_h[8];
1357 high = s->plane[plane].l_h[9];
1358 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1359 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1360 low += output_stride * 2;
1361 high += output_stride * 2;
1362 dst += pic->linesize[act_plane];
1368 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1369 process_bayer(pic, s->bpc);
1378 static av_cold int cfhd_close(AVCodecContext *avctx)
1380 CFHDContext *s = avctx->priv_data;
1384 ff_free_vlc(&s->vlc_9);
1385 ff_free_vlc(&s->vlc_18);
1391 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1393 CFHDContext *psrc = src->priv_data;
1394 CFHDContext *pdst = dst->priv_data;
1397 if (dst == src || psrc->transform_type == 0)
1400 if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1401 pdst->a_format != psrc->a_format ||
1402 pdst->a_width != psrc->a_width ||
1403 pdst->a_height != psrc->a_height ||
1404 pdst->a_transform_type != psrc->a_transform_type)
1407 pdst->a_format = psrc->a_format;
1408 pdst->a_width = psrc->a_width;
1409 pdst->a_height = psrc->a_height;
1410 pdst->a_transform_type = psrc->a_transform_type;
1411 pdst->transform_type = psrc->transform_type;
1412 pdst->progressive = psrc->progressive;
1413 pdst->planes = psrc->planes;
1415 if (!pdst->plane[0].idwt_buf) {
1416 pdst->coded_width = pdst->a_width;
1417 pdst->coded_height = pdst->a_height;
1418 pdst->coded_format = pdst->a_format;
1419 pdst->transform_type = pdst->a_transform_type;
1420 ret = alloc_buffers(dst);
1425 for (int plane = 0; plane < pdst->planes; plane++) {
1426 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1427 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1428 pdst->plane[plane].idwt_size * sizeof(int16_t));
1435 AVCodec ff_cfhd_decoder = {
1437 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1438 .type = AVMEDIA_TYPE_VIDEO,
1439 .id = AV_CODEC_ID_CFHD,
1440 .priv_data_size = sizeof(CFHDContext),
1442 .close = cfhd_close,
1443 .decode = cfhd_decode,
1444 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1445 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1446 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,