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 && s->a_width && s->a_height) {
705 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
706 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
707 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
708 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
710 if (lowpass_width < 3 ||
711 lowpass_width > lowpass_a_width) {
712 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
713 ret = AVERROR(EINVAL);
717 if (lowpass_height < 3 ||
718 lowpass_height > lowpass_a_height) {
719 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
720 ret = AVERROR(EINVAL);
725 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
726 ret = AVERROR(EINVAL);
730 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
731 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
732 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
733 ret = AVERROR(EINVAL);
737 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
738 for (i = 0; i < lowpass_height; i++) {
739 for (j = 0; j < lowpass_width; j++)
740 coeff_data[j] = bytestream2_get_be16u(&gb);
742 coeff_data += lowpass_width;
745 /* Align to mod-4 position to continue reading tags */
746 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
748 /* Copy last line of coefficients if odd height */
749 if (lowpass_height & 1) {
750 memcpy(&coeff_data[lowpass_height * lowpass_width],
751 &coeff_data[(lowpass_height - 1) * lowpass_width],
752 lowpass_width * sizeof(*coeff_data));
755 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
758 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
759 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
760 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
761 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
762 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
763 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
765 int a_expected = highpass_a_height * highpass_a_width;
766 int level, run, coeff;
767 int count = 0, bytes;
770 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
771 ret = AVERROR(EINVAL);
775 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
776 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
777 ret = AVERROR(EINVAL);
780 expected = highpass_height * highpass_stride;
782 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);
784 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
788 OPEN_READER(re, &s->gb);
790 const int lossless = s->band_encoding == 5;
792 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
796 UPDATE_CACHE(re, &s->gb);
797 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
806 if (count > expected)
810 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
813 if (tag == BandSecondPass) {
814 const uint16_t q = s->quantisation;
816 for (i = 0; i < run; i++) {
817 *coeff_data |= coeff * 256;
821 for (i = 0; i < run; i++)
822 *coeff_data++ = coeff;
827 UPDATE_CACHE(re, &s->gb);
828 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
832 if (level == 255 && run == 2)
837 if (count > expected)
841 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
844 if (tag == BandSecondPass) {
845 const uint16_t q = s->quantisation;
847 for (i = 0; i < run; i++) {
848 *coeff_data |= coeff * 256;
852 for (i = 0; i < run; i++)
853 *coeff_data++ = coeff;
857 CLOSE_READER(re, &s->gb);
860 if (count > expected) {
861 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
862 ret = AVERROR(EINVAL);
866 peak_table(coeff_data - count, &s->peak, count);
867 if (s->difference_coding)
868 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
870 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
871 if (bytes > bytestream2_get_bytes_left(&gb)) {
872 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
873 ret = AVERROR(EINVAL);
876 bytestream2_seek(&gb, bytes, SEEK_CUR);
878 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
880 if (s->subband_num_actual != 255)
885 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
886 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
891 ff_thread_finish_setup(avctx);
893 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
894 s->a_transform_type == INT_MIN ||
895 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
896 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
897 ret = AVERROR(EINVAL);
902 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
903 ret = AVERROR(EINVAL);
907 if (s->transform_type == 0 && s->sample_type != 1) {
908 for (plane = 0; plane < s->planes && !ret; plane++) {
910 int lowpass_height = s->plane[plane].band[0][0].height;
911 int output_stride = s->plane[plane].band[0][0].a_width;
912 int lowpass_width = s->plane[plane].band[0][0].width;
913 int highpass_stride = s->plane[plane].band[0][1].stride;
914 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
915 ptrdiff_t dst_linesize;
916 int16_t *low, *high, *output, *dst;
918 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
920 dst_linesize = pic->linesize[act_plane];
922 dst_linesize = pic->linesize[act_plane] / 2;
925 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
926 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
927 lowpass_width < 3 || lowpass_height < 3) {
928 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
929 ret = AVERROR(EINVAL);
933 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
935 low = s->plane[plane].subband[0];
936 high = s->plane[plane].subband[2];
937 output = s->plane[plane].l_h[0];
938 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
940 low = s->plane[plane].subband[1];
941 high = s->plane[plane].subband[3];
942 output = s->plane[plane].l_h[1];
944 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
946 low = s->plane[plane].l_h[0];
947 high = s->plane[plane].l_h[1];
948 output = s->plane[plane].subband[0];
949 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
951 output = s->plane[plane].subband[0];
952 for (i = 0; i < lowpass_height * 2; i++) {
953 for (j = 0; j < lowpass_width * 2; j++)
956 output += output_stride * 2;
961 lowpass_height = s->plane[plane].band[1][1].height;
962 output_stride = s->plane[plane].band[1][1].a_width;
963 lowpass_width = s->plane[plane].band[1][1].width;
964 highpass_stride = s->plane[plane].band[1][1].stride;
966 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
967 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
968 lowpass_width < 3 || lowpass_height < 3) {
969 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
970 ret = AVERROR(EINVAL);
974 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
976 low = s->plane[plane].subband[0];
977 high = s->plane[plane].subband[5];
978 output = s->plane[plane].l_h[3];
979 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
981 low = s->plane[plane].subband[4];
982 high = s->plane[plane].subband[6];
983 output = s->plane[plane].l_h[4];
984 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
986 low = s->plane[plane].l_h[3];
987 high = s->plane[plane].l_h[4];
988 output = s->plane[plane].subband[0];
989 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
991 output = s->plane[plane].subband[0];
992 for (i = 0; i < lowpass_height * 2; i++) {
993 for (j = 0; j < lowpass_width * 2; j++)
996 output += output_stride * 2;
1000 lowpass_height = s->plane[plane].band[2][1].height;
1001 output_stride = s->plane[plane].band[2][1].a_width;
1002 lowpass_width = s->plane[plane].band[2][1].width;
1003 highpass_stride = s->plane[plane].band[2][1].stride;
1005 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1006 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1007 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1008 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1009 ret = AVERROR(EINVAL);
1013 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1014 if (s->progressive) {
1015 low = s->plane[plane].subband[0];
1016 high = s->plane[plane].subband[8];
1017 output = s->plane[plane].l_h[6];
1018 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1020 low = s->plane[plane].subband[7];
1021 high = s->plane[plane].subband[9];
1022 output = s->plane[plane].l_h[7];
1023 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1025 dst = (int16_t *)pic->data[act_plane];
1026 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1030 dst += pic->linesize[act_plane] >> 1;
1032 low = s->plane[plane].l_h[6];
1033 high = s->plane[plane].l_h[7];
1035 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1036 (lowpass_height * 2 > avctx->coded_height / 2 ||
1037 lowpass_width * 2 > avctx->coded_width / 2 )
1039 ret = AVERROR_INVALIDDATA;
1043 for (i = 0; i < s->plane[act_plane].height; i++) {
1044 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1045 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1046 process_alpha(dst, lowpass_width * 2);
1047 low += output_stride;
1048 high += output_stride;
1049 dst += dst_linesize;
1052 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1053 pic->interlaced_frame = 1;
1054 low = s->plane[plane].subband[0];
1055 high = s->plane[plane].subband[7];
1056 output = s->plane[plane].l_h[6];
1057 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1059 low = s->plane[plane].subband[8];
1060 high = s->plane[plane].subband[9];
1061 output = s->plane[plane].l_h[7];
1062 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1064 dst = (int16_t *)pic->data[act_plane];
1065 low = s->plane[plane].l_h[6];
1066 high = s->plane[plane].l_h[7];
1067 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1068 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1069 low += output_stride * 2;
1070 high += output_stride * 2;
1071 dst += pic->linesize[act_plane];
1075 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1076 for (plane = 0; plane < s->planes && !ret; plane++) {
1077 int lowpass_height = s->plane[plane].band[0][0].height;
1078 int output_stride = s->plane[plane].band[0][0].a_width;
1079 int lowpass_width = s->plane[plane].band[0][0].width;
1080 int highpass_stride = s->plane[plane].band[0][1].stride;
1081 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1082 int16_t *low, *high, *output, *dst;
1083 ptrdiff_t dst_linesize;
1085 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1087 dst_linesize = pic->linesize[act_plane];
1089 dst_linesize = pic->linesize[act_plane] / 2;
1092 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1093 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1094 lowpass_width < 3 || lowpass_height < 3) {
1095 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1096 ret = AVERROR(EINVAL);
1100 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1102 low = s->plane[plane].subband[0];
1103 high = s->plane[plane].subband[2];
1104 output = s->plane[plane].l_h[0];
1105 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1107 low = s->plane[plane].subband[1];
1108 high = s->plane[plane].subband[3];
1109 output = s->plane[plane].l_h[1];
1110 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1112 low = s->plane[plane].l_h[0];
1113 high = s->plane[plane].l_h[1];
1114 output = s->plane[plane].l_h[7];
1115 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1117 output = s->plane[plane].l_h[7];
1118 for (i = 0; i < lowpass_height * 2; i++) {
1119 for (j = 0; j < lowpass_width * 2; j++)
1122 output += output_stride * 2;
1126 lowpass_height = s->plane[plane].band[1][1].height;
1127 output_stride = s->plane[plane].band[1][1].a_width;
1128 lowpass_width = s->plane[plane].band[1][1].width;
1129 highpass_stride = s->plane[plane].band[1][1].stride;
1131 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1132 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1133 lowpass_width < 3 || lowpass_height < 3) {
1134 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1135 ret = AVERROR(EINVAL);
1139 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1141 low = s->plane[plane].l_h[7];
1142 high = s->plane[plane].subband[5];
1143 output = s->plane[plane].l_h[3];
1144 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1146 low = s->plane[plane].subband[4];
1147 high = s->plane[plane].subband[6];
1148 output = s->plane[plane].l_h[4];
1149 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1151 low = s->plane[plane].l_h[3];
1152 high = s->plane[plane].l_h[4];
1153 output = s->plane[plane].l_h[7];
1154 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1156 output = s->plane[plane].l_h[7];
1157 for (i = 0; i < lowpass_height * 2; i++) {
1158 for (j = 0; j < lowpass_width * 2; j++)
1160 output += output_stride * 2;
1163 low = s->plane[plane].subband[7];
1164 high = s->plane[plane].subband[9];
1165 output = s->plane[plane].l_h[3];
1166 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1168 low = s->plane[plane].subband[8];
1169 high = s->plane[plane].subband[10];
1170 output = s->plane[plane].l_h[4];
1171 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1173 low = s->plane[plane].l_h[3];
1174 high = s->plane[plane].l_h[4];
1175 output = s->plane[plane].l_h[9];
1176 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1178 lowpass_height = s->plane[plane].band[4][1].height;
1179 output_stride = s->plane[plane].band[4][1].a_width;
1180 lowpass_width = s->plane[plane].band[4][1].width;
1181 highpass_stride = s->plane[plane].band[4][1].stride;
1182 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1184 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1185 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1186 lowpass_width < 3 || lowpass_height < 3) {
1187 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1188 ret = AVERROR(EINVAL);
1192 low = s->plane[plane].l_h[7];
1193 high = s->plane[plane].l_h[9];
1194 output = s->plane[plane].l_h[7];
1195 for (i = 0; i < lowpass_height; i++) {
1196 inverse_temporal_filter(low, high, lowpass_width);
1197 low += output_stride;
1198 high += output_stride;
1200 if (s->progressive) {
1201 low = s->plane[plane].l_h[7];
1202 high = s->plane[plane].subband[15];
1203 output = s->plane[plane].l_h[6];
1204 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1206 low = s->plane[plane].subband[14];
1207 high = s->plane[plane].subband[16];
1208 output = s->plane[plane].l_h[7];
1209 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1211 low = s->plane[plane].l_h[9];
1212 high = s->plane[plane].subband[12];
1213 output = s->plane[plane].l_h[8];
1214 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1216 low = s->plane[plane].subband[11];
1217 high = s->plane[plane].subband[13];
1218 output = s->plane[plane].l_h[9];
1219 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1221 if (s->sample_type == 1)
1224 dst = (int16_t *)pic->data[act_plane];
1225 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1229 dst += pic->linesize[act_plane] >> 1;
1232 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1233 (lowpass_height * 2 > avctx->coded_height / 2 ||
1234 lowpass_width * 2 > avctx->coded_width / 2 )
1236 ret = AVERROR_INVALIDDATA;
1240 low = s->plane[plane].l_h[6];
1241 high = s->plane[plane].l_h[7];
1242 for (i = 0; i < s->plane[act_plane].height; i++) {
1243 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1244 low += output_stride;
1245 high += output_stride;
1246 dst += dst_linesize;
1249 pic->interlaced_frame = 1;
1250 low = s->plane[plane].l_h[7];
1251 high = s->plane[plane].subband[14];
1252 output = s->plane[plane].l_h[6];
1253 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1255 low = s->plane[plane].subband[15];
1256 high = s->plane[plane].subband[16];
1257 output = s->plane[plane].l_h[7];
1258 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1260 low = s->plane[plane].l_h[9];
1261 high = s->plane[plane].subband[11];
1262 output = s->plane[plane].l_h[8];
1263 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1265 low = s->plane[plane].subband[12];
1266 high = s->plane[plane].subband[13];
1267 output = s->plane[plane].l_h[9];
1268 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1270 if (s->sample_type == 1)
1273 dst = (int16_t *)pic->data[act_plane];
1274 low = s->plane[plane].l_h[6];
1275 high = s->plane[plane].l_h[7];
1276 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1277 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1278 low += output_stride * 2;
1279 high += output_stride * 2;
1280 dst += pic->linesize[act_plane];
1286 if (s->transform_type == 2 && s->sample_type == 1) {
1287 int16_t *low, *high, *dst;
1288 int output_stride, lowpass_height, lowpass_width;
1289 ptrdiff_t dst_linesize;
1291 for (plane = 0; plane < s->planes; plane++) {
1292 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1294 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1296 dst_linesize = pic->linesize[act_plane];
1298 dst_linesize = pic->linesize[act_plane] / 2;
1301 lowpass_height = s->plane[plane].band[4][1].height;
1302 output_stride = s->plane[plane].band[4][1].a_width;
1303 lowpass_width = s->plane[plane].band[4][1].width;
1305 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1306 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1307 lowpass_width < 3 || lowpass_height < 3) {
1308 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1309 ret = AVERROR(EINVAL);
1313 if (s->progressive) {
1314 dst = (int16_t *)pic->data[act_plane];
1315 low = s->plane[plane].l_h[8];
1316 high = s->plane[plane].l_h[9];
1318 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1322 dst += pic->linesize[act_plane] >> 1;
1325 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1326 (lowpass_height * 2 > avctx->coded_height / 2 ||
1327 lowpass_width * 2 > avctx->coded_width / 2 )
1329 ret = AVERROR_INVALIDDATA;
1333 for (i = 0; i < s->plane[act_plane].height; i++) {
1334 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1335 low += output_stride;
1336 high += output_stride;
1337 dst += dst_linesize;
1340 dst = (int16_t *)pic->data[act_plane];
1341 low = s->plane[plane].l_h[8];
1342 high = s->plane[plane].l_h[9];
1343 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1344 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1345 low += output_stride * 2;
1346 high += output_stride * 2;
1347 dst += pic->linesize[act_plane];
1353 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1354 process_bayer(pic, s->bpc);
1363 static av_cold int cfhd_close(AVCodecContext *avctx)
1365 CFHDContext *s = avctx->priv_data;
1369 ff_free_vlc(&s->vlc_9);
1370 ff_free_vlc(&s->vlc_18);
1376 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1378 CFHDContext *psrc = src->priv_data;
1379 CFHDContext *pdst = dst->priv_data;
1382 if (dst == src || psrc->transform_type == 0)
1385 if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1386 pdst->a_format != psrc->a_format ||
1387 pdst->a_width != psrc->a_width ||
1388 pdst->a_height != psrc->a_height ||
1389 pdst->a_transform_type != psrc->a_transform_type)
1392 pdst->a_format = psrc->a_format;
1393 pdst->a_width = psrc->a_width;
1394 pdst->a_height = psrc->a_height;
1395 pdst->a_transform_type = psrc->a_transform_type;
1396 pdst->transform_type = psrc->transform_type;
1397 pdst->progressive = psrc->progressive;
1398 pdst->planes = psrc->planes;
1400 if (!pdst->plane[0].idwt_buf) {
1401 pdst->coded_width = pdst->a_width;
1402 pdst->coded_height = pdst->a_height;
1403 pdst->coded_format = pdst->a_format;
1404 pdst->transform_type = pdst->a_transform_type;
1405 ret = alloc_buffers(dst);
1410 for (int plane = 0; plane < pdst->planes; plane++) {
1411 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1412 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1413 pdst->plane[plane].idwt_size * sizeof(int16_t));
1420 AVCodec ff_cfhd_decoder = {
1422 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1423 .type = AVMEDIA_TYPE_VIDEO,
1424 .id = AV_CODEC_ID_CFHD,
1425 .priv_data_size = sizeof(CFHDContext),
1427 .close = cfhd_close,
1428 .decode = cfhd_decode,
1429 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1430 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1431 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,