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;
238 static int alloc_buffers(AVCodecContext *avctx)
240 CFHDContext *s = avctx->priv_data;
241 int i, j, ret, planes, bayer = 0;
242 int chroma_x_shift, chroma_y_shift;
245 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
247 avctx->pix_fmt = s->coded_format;
249 ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
251 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
253 &chroma_y_shift)) < 0)
255 planes = av_pix_fmt_count_planes(s->coded_format);
256 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
263 for (i = 0; i < planes; i++) {
264 int w8, h8, w4, h4, w2, h2;
265 int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
266 int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
267 ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
269 if (chroma_y_shift && !bayer)
270 height = FFALIGN(height / 8, 2) * 8;
271 s->plane[i].width = width;
272 s->plane[i].height = height;
273 s->plane[i].stride = stride;
275 w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
276 h8 = FFALIGN(height, 8) / 8;
282 if (s->transform_type == 0) {
283 s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
284 s->plane[i].idwt_buf =
285 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
286 s->plane[i].idwt_tmp =
287 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
289 s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
290 s->plane[i].idwt_buf =
291 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
292 s->plane[i].idwt_tmp =
293 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
296 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
297 return AVERROR(ENOMEM);
299 s->plane[i].subband[0] = s->plane[i].idwt_buf;
300 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
301 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
302 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
303 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
304 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
305 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
306 if (s->transform_type == 0) {
307 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
308 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
309 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
312 s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
313 s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
314 s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
315 s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
316 s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
317 s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
318 s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
319 s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
320 s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
321 s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
324 if (s->transform_type == 0) {
325 for (j = 0; j < DWT_LEVELS; j++) {
326 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
327 s->plane[i].band[j][k].a_width = w8 << j;
328 s->plane[i].band[j][k].a_height = h8 << j;
332 for (j = 0; j < DWT_LEVELS_3D; j++) {
333 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
335 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
336 s->plane[i].band[j][k].a_width = w8 << t;
337 s->plane[i].band[j][k].a_height = h8 << t;
342 /* ll2 and ll1 commented out because they are done in-place */
343 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
344 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
345 // s->plane[i].l_h[2] = ll2;
346 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
347 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
348 // s->plane[i].l_h[5] = ll1;
349 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
350 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
351 if (s->transform_type != 0) {
352 int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
354 s->plane[i].l_h[8] = frame2;
355 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
359 s->a_height = s->coded_height;
360 s->a_width = s->coded_width;
361 s->a_format = s->coded_format;
366 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
369 CFHDContext *s = avctx->priv_data;
370 CFHDDSPContext *dsp = &s->dsp;
372 ThreadFrame frame = { .f = data };
374 int ret = 0, i, j, plane, got_buffer = 0;
377 init_frame_defaults(s);
378 s->planes = av_pix_fmt_count_planes(s->coded_format);
380 bytestream2_init(&gb, avpkt->data, avpkt->size);
382 while (bytestream2_get_bytes_left(&gb) >= 4) {
383 /* Bit weird but implement the tag parsing as the spec says */
384 uint16_t tagu = bytestream2_get_be16(&gb);
385 int16_t tag = (int16_t)tagu;
386 int8_t tag8 = (int8_t)(tagu >> 8);
387 uint16_t abstag = abs(tag);
388 int8_t abs_tag8 = abs(tag8);
389 uint16_t data = bytestream2_get_be16(&gb);
390 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
391 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
392 } else if (tag == SampleFlags) {
393 av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
394 s->progressive = data & 0x0001;
395 } else if (tag == FrameType) {
396 s->frame_type = data;
397 av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
398 } else if (abstag == VersionMajor) {
399 av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
400 } else if (abstag == VersionMinor) {
401 av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
402 } else if (abstag == VersionRevision) {
403 av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
404 } else if (abstag == VersionEdit) {
405 av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
406 } else if (abstag == Version) {
407 av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
408 } else if (tag == ImageWidth) {
409 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
410 s->coded_width = data;
411 } else if (tag == ImageHeight) {
412 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
413 s->coded_height = data;
414 } else if (tag == ChannelCount) {
415 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
416 s->channel_cnt = data;
418 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
419 ret = AVERROR_PATCHWELCOME;
422 } else if (tag == SubbandCount) {
423 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
424 if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
425 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
426 ret = AVERROR_PATCHWELCOME;
429 } else if (tag == ChannelNumber) {
430 s->channel_num = data;
431 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
432 if (s->channel_num >= s->planes) {
433 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
434 ret = AVERROR(EINVAL);
437 init_plane_defaults(s);
438 } else if (tag == SubbandNumber) {
439 if (s->subband_num != 0 && data == 1) // hack
441 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
442 s->subband_num = data;
443 if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
444 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
445 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
446 ret = AVERROR(EINVAL);
449 if (s->subband_num > 3) {
450 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
451 ret = AVERROR(EINVAL);
454 } else if (tag == SubbandBand) {
455 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
456 if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
457 (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
458 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
459 ret = AVERROR(EINVAL);
462 if (s->transform_type == 0 || s->transform_type == 2)
463 s->subband_num_actual = data;
465 av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
466 } else if (tag == LowpassPrecision)
467 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
468 else if (tag == Quantization) {
469 s->quantisation = data;
470 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
471 } else if (tag == PrescaleTable) {
472 for (i = 0; i < 8; i++)
473 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
474 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
475 } else if (tag == BandEncoding) {
476 if (!data || data > 5) {
477 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
478 ret = AVERROR(EINVAL);
481 s->band_encoding = data;
482 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
483 } else if (tag == LowpassWidth) {
484 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
485 s->plane[s->channel_num].band[0][0].width = data;
486 s->plane[s->channel_num].band[0][0].stride = data;
487 } else if (tag == LowpassHeight) {
488 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
489 s->plane[s->channel_num].band[0][0].height = data;
490 } else if (tag == SampleType) {
491 s->sample_type = data;
492 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
493 } else if (tag == TransformType) {
495 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
496 ret = AVERROR(EINVAL);
498 } else if (data == 1) {
499 av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
500 ret = AVERROR_PATCHWELCOME;
503 if (s->transform_type == -1) {
504 s->transform_type = data;
505 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
507 av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
509 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
510 if (abstag == 0x4001)
512 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
513 bytestream2_skipu(&gb, data * 4);
514 } else if (tag == FrameIndex) {
515 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
516 s->frame_index = data;
517 } else if (tag == SampleIndexTable) {
518 av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
519 if (data > bytestream2_get_bytes_left(&gb) / 4) {
520 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
521 ret = AVERROR_INVALIDDATA;
524 for (i = 0; i < data; i++) {
525 uint32_t offset = bytestream2_get_be32(&gb);
526 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
528 } else if (tag == HighpassWidth) {
529 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);
531 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
532 ret = AVERROR(EINVAL);
535 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
536 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
537 } else if (tag == HighpassHeight) {
538 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
540 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
541 ret = AVERROR(EINVAL);
544 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
545 } else if (tag == BandWidth) {
546 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
548 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
549 ret = AVERROR(EINVAL);
552 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
553 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
554 } else if (tag == BandHeight) {
555 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
557 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
558 ret = AVERROR(EINVAL);
561 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
562 } else if (tag == InputFormat) {
563 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
564 if (s->coded_format == AV_PIX_FMT_NONE ||
565 s->coded_format == AV_PIX_FMT_YUV422P10) {
566 if (data >= 100 && data <= 105) {
567 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
568 } else if (data >= 122 && data <= 128) {
569 s->coded_format = AV_PIX_FMT_GBRP12;
570 } else if (data == 30) {
571 s->coded_format = AV_PIX_FMT_GBRAP12;
573 s->coded_format = AV_PIX_FMT_YUV422P10;
575 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
577 } else if (tag == BandCodingFlags) {
578 s->codebook = data & 0xf;
579 s->difference_coding = (data >> 4) & 1;
580 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
581 } else if (tag == Precision) {
582 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
583 if (!(data == 10 || data == 12)) {
584 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
585 ret = AVERROR(EINVAL);
588 avctx->bits_per_raw_sample = s->bpc = data;
589 } else if (tag == EncodedFormat) {
590 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
592 s->coded_format = AV_PIX_FMT_YUV422P10;
593 } else if (data == 2) {
594 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
595 } else if (data == 3) {
596 s->coded_format = AV_PIX_FMT_GBRP12;
597 } else if (data == 4) {
598 s->coded_format = AV_PIX_FMT_GBRAP12;
600 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
601 ret = AVERROR_PATCHWELCOME;
604 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
605 } else if (tag == -DisplayHeight) {
606 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
607 s->cropped_height = data;
608 } else if (tag == -PeakOffsetLow) {
609 s->peak.offset &= ~0xffff;
610 s->peak.offset |= (data & 0xffff);
613 } else if (tag == -PeakOffsetHigh) {
614 s->peak.offset &= 0xffff;
615 s->peak.offset |= (data & 0xffffU)<<16;
618 } else if (tag == -PeakLevel && s->peak.offset) {
619 s->peak.level = data;
620 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
622 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
624 if (tag == BitstreamMarker && data == 0xf0f &&
625 s->coded_format != AV_PIX_FMT_NONE) {
626 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
627 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
628 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
630 if (s->coded_width) {
631 s->coded_width *= factor;
634 if (s->coded_height) {
635 s->coded_height *= factor;
638 if (!s->a_width && !s->coded_width) {
639 s->coded_width = lowpass_width * factor * 8;
642 if (!s->a_height && !s->coded_height) {
643 s->coded_height = lowpass_height * factor * 8;
646 if (s->a_width && !s->coded_width)
647 s->coded_width = s->a_width;
648 if (s->a_height && !s->coded_height)
649 s->coded_height = s->a_height;
651 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
652 s->a_format != s->coded_format) {
654 if ((ret = alloc_buffers(avctx)) < 0) {
659 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
662 if (s->cropped_height) {
663 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
664 if (avctx->height < height)
665 return AVERROR_INVALIDDATA;
666 avctx->height = height;
671 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
676 s->coded_format = AV_PIX_FMT_NONE;
678 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
682 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
686 s->coded_format = AV_PIX_FMT_NONE;
690 if (s->subband_num_actual == 255)
692 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
694 /* Lowpass coefficients */
695 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
696 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
697 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
698 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
699 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
701 if (lowpass_width < 3 ||
702 lowpass_width > lowpass_a_width) {
703 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
704 ret = AVERROR(EINVAL);
708 if (lowpass_height < 3 ||
709 lowpass_height > lowpass_a_height) {
710 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
711 ret = AVERROR(EINVAL);
716 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
717 ret = AVERROR(EINVAL);
721 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
722 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
723 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
724 ret = AVERROR(EINVAL);
728 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
729 for (i = 0; i < lowpass_height; i++) {
730 for (j = 0; j < lowpass_width; j++)
731 coeff_data[j] = bytestream2_get_be16u(&gb);
733 coeff_data += lowpass_width;
736 /* Align to mod-4 position to continue reading tags */
737 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
739 /* Copy last line of coefficients if odd height */
740 if (lowpass_height & 1) {
741 memcpy(&coeff_data[lowpass_height * lowpass_width],
742 &coeff_data[(lowpass_height - 1) * lowpass_width],
743 lowpass_width * sizeof(*coeff_data));
746 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
749 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
750 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
751 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
752 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
753 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
754 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
756 int a_expected = highpass_a_height * highpass_a_width;
757 int level, run, coeff;
758 int count = 0, bytes;
761 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
762 ret = AVERROR(EINVAL);
766 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
767 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
768 ret = AVERROR(EINVAL);
771 expected = highpass_height * highpass_stride;
773 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);
775 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
779 OPEN_READER(re, &s->gb);
781 const int lossless = s->band_encoding == 5;
783 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
787 UPDATE_CACHE(re, &s->gb);
788 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
797 if (count > expected)
801 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
804 if (tag == BandSecondPass) {
805 const uint16_t q = s->quantisation;
807 for (i = 0; i < run; i++) {
808 *coeff_data |= coeff << 8;
812 for (i = 0; i < run; i++)
813 *coeff_data++ = coeff;
818 UPDATE_CACHE(re, &s->gb);
819 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
823 if (level == 255 && run == 2)
828 if (count > expected)
832 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
835 if (tag == BandSecondPass) {
836 const uint16_t q = s->quantisation;
838 for (i = 0; i < run; i++) {
839 *coeff_data |= coeff << 8;
843 for (i = 0; i < run; i++)
844 *coeff_data++ = coeff;
848 CLOSE_READER(re, &s->gb);
851 if (count > expected) {
852 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
853 ret = AVERROR(EINVAL);
857 peak_table(coeff_data - count, &s->peak, count);
858 if (s->difference_coding)
859 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
861 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
862 if (bytes > bytestream2_get_bytes_left(&gb)) {
863 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
864 ret = AVERROR(EINVAL);
867 bytestream2_seek(&gb, bytes, SEEK_CUR);
869 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
871 if (s->subband_num_actual != 255)
876 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
877 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
882 ff_thread_finish_setup(avctx);
884 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
885 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
886 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
887 ret = AVERROR(EINVAL);
892 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
893 ret = AVERROR(EINVAL);
897 if (s->transform_type == 0 && s->sample_type != 1) {
898 for (plane = 0; plane < s->planes && !ret; plane++) {
900 int lowpass_height = s->plane[plane].band[0][0].height;
901 int output_stride = s->plane[plane].band[0][0].a_width;
902 int lowpass_width = s->plane[plane].band[0][0].width;
903 int highpass_stride = s->plane[plane].band[0][1].stride;
904 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
905 ptrdiff_t dst_linesize;
906 int16_t *low, *high, *output, *dst;
908 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
910 dst_linesize = pic->linesize[act_plane];
912 dst_linesize = pic->linesize[act_plane] / 2;
915 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
916 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
917 lowpass_width < 3 || lowpass_height < 3) {
918 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
919 ret = AVERROR(EINVAL);
923 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
925 low = s->plane[plane].subband[0];
926 high = s->plane[plane].subband[2];
927 output = s->plane[plane].l_h[0];
928 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
930 low = s->plane[plane].subband[1];
931 high = s->plane[plane].subband[3];
932 output = s->plane[plane].l_h[1];
934 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
936 low = s->plane[plane].l_h[0];
937 high = s->plane[plane].l_h[1];
938 output = s->plane[plane].subband[0];
939 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
941 output = s->plane[plane].subband[0];
942 for (i = 0; i < lowpass_height * 2; i++) {
943 for (j = 0; j < lowpass_width * 2; j++)
946 output += output_stride * 2;
951 lowpass_height = s->plane[plane].band[1][1].height;
952 output_stride = s->plane[plane].band[1][1].a_width;
953 lowpass_width = s->plane[plane].band[1][1].width;
954 highpass_stride = s->plane[plane].band[1][1].stride;
956 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
957 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
958 lowpass_width < 3 || lowpass_height < 3) {
959 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
960 ret = AVERROR(EINVAL);
964 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
966 low = s->plane[plane].subband[0];
967 high = s->plane[plane].subband[5];
968 output = s->plane[plane].l_h[3];
969 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
971 low = s->plane[plane].subband[4];
972 high = s->plane[plane].subband[6];
973 output = s->plane[plane].l_h[4];
974 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
976 low = s->plane[plane].l_h[3];
977 high = s->plane[plane].l_h[4];
978 output = s->plane[plane].subband[0];
979 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
981 output = s->plane[plane].subband[0];
982 for (i = 0; i < lowpass_height * 2; i++) {
983 for (j = 0; j < lowpass_width * 2; j++)
986 output += output_stride * 2;
990 lowpass_height = s->plane[plane].band[2][1].height;
991 output_stride = s->plane[plane].band[2][1].a_width;
992 lowpass_width = s->plane[plane].band[2][1].width;
993 highpass_stride = s->plane[plane].band[2][1].stride;
995 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
996 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
997 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
998 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
999 ret = AVERROR(EINVAL);
1003 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1004 if (s->progressive) {
1005 low = s->plane[plane].subband[0];
1006 high = s->plane[plane].subband[8];
1007 output = s->plane[plane].l_h[6];
1008 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1010 low = s->plane[plane].subband[7];
1011 high = s->plane[plane].subband[9];
1012 output = s->plane[plane].l_h[7];
1013 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1015 dst = (int16_t *)pic->data[act_plane];
1016 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1020 dst += pic->linesize[act_plane] >> 1;
1022 low = s->plane[plane].l_h[6];
1023 high = s->plane[plane].l_h[7];
1025 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1026 (lowpass_height * 2 > avctx->coded_height / 2 ||
1027 lowpass_width * 2 > avctx->coded_width / 2 )
1029 ret = AVERROR_INVALIDDATA;
1033 for (i = 0; i < s->plane[act_plane].height; i++) {
1034 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1035 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1036 process_alpha(dst, lowpass_width * 2);
1037 low += output_stride;
1038 high += output_stride;
1039 dst += dst_linesize;
1042 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1043 pic->interlaced_frame = 1;
1044 low = s->plane[plane].subband[0];
1045 high = s->plane[plane].subband[7];
1046 output = s->plane[plane].l_h[6];
1047 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1049 low = s->plane[plane].subband[8];
1050 high = s->plane[plane].subband[9];
1051 output = s->plane[plane].l_h[7];
1052 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1054 dst = (int16_t *)pic->data[act_plane];
1055 low = s->plane[plane].l_h[6];
1056 high = s->plane[plane].l_h[7];
1057 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1058 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1059 low += output_stride * 2;
1060 high += output_stride * 2;
1061 dst += pic->linesize[act_plane];
1065 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1066 for (plane = 0; plane < s->planes && !ret; plane++) {
1067 int lowpass_height = s->plane[plane].band[0][0].height;
1068 int output_stride = s->plane[plane].band[0][0].a_width;
1069 int lowpass_width = s->plane[plane].band[0][0].width;
1070 int highpass_stride = s->plane[plane].band[0][1].stride;
1071 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1072 int16_t *low, *high, *output, *dst;
1073 ptrdiff_t dst_linesize;
1075 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1077 dst_linesize = pic->linesize[act_plane];
1079 dst_linesize = pic->linesize[act_plane] / 2;
1082 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1083 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1084 lowpass_width < 3 || lowpass_height < 3) {
1085 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1086 ret = AVERROR(EINVAL);
1090 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1092 low = s->plane[plane].subband[0];
1093 high = s->plane[plane].subband[2];
1094 output = s->plane[plane].l_h[0];
1095 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1097 low = s->plane[plane].subband[1];
1098 high = s->plane[plane].subband[3];
1099 output = s->plane[plane].l_h[1];
1100 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1102 low = s->plane[plane].l_h[0];
1103 high = s->plane[plane].l_h[1];
1104 output = s->plane[plane].l_h[7];
1105 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1107 output = s->plane[plane].l_h[7];
1108 for (i = 0; i < lowpass_height * 2; i++) {
1109 for (j = 0; j < lowpass_width * 2; j++)
1112 output += output_stride * 2;
1116 lowpass_height = s->plane[plane].band[1][1].height;
1117 output_stride = s->plane[plane].band[1][1].a_width;
1118 lowpass_width = s->plane[plane].band[1][1].width;
1119 highpass_stride = s->plane[plane].band[1][1].stride;
1121 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1122 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1123 lowpass_width < 3 || lowpass_height < 3) {
1124 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1125 ret = AVERROR(EINVAL);
1129 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1131 low = s->plane[plane].l_h[7];
1132 high = s->plane[plane].subband[5];
1133 output = s->plane[plane].l_h[3];
1134 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1136 low = s->plane[plane].subband[4];
1137 high = s->plane[plane].subband[6];
1138 output = s->plane[plane].l_h[4];
1139 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1141 low = s->plane[plane].l_h[3];
1142 high = s->plane[plane].l_h[4];
1143 output = s->plane[plane].l_h[7];
1144 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1146 output = s->plane[plane].l_h[7];
1147 for (i = 0; i < lowpass_height * 2; i++) {
1148 for (j = 0; j < lowpass_width * 2; j++)
1150 output += output_stride * 2;
1153 low = s->plane[plane].subband[7];
1154 high = s->plane[plane].subband[9];
1155 output = s->plane[plane].l_h[3];
1156 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1158 low = s->plane[plane].subband[8];
1159 high = s->plane[plane].subband[10];
1160 output = s->plane[plane].l_h[4];
1161 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1163 low = s->plane[plane].l_h[3];
1164 high = s->plane[plane].l_h[4];
1165 output = s->plane[plane].l_h[9];
1166 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1168 lowpass_height = s->plane[plane].band[4][1].height;
1169 output_stride = s->plane[plane].band[4][1].a_width;
1170 lowpass_width = s->plane[plane].band[4][1].width;
1171 highpass_stride = s->plane[plane].band[4][1].stride;
1172 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1174 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1175 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1176 lowpass_width < 3 || lowpass_height < 3) {
1177 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1178 ret = AVERROR(EINVAL);
1182 low = s->plane[plane].l_h[7];
1183 high = s->plane[plane].l_h[9];
1184 output = s->plane[plane].l_h[7];
1185 for (i = 0; i < lowpass_height; i++) {
1186 inverse_temporal_filter(low, high, lowpass_width);
1187 low += output_stride;
1188 high += output_stride;
1190 if (s->progressive) {
1191 low = s->plane[plane].l_h[7];
1192 high = s->plane[plane].subband[15];
1193 output = s->plane[plane].l_h[6];
1194 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1196 low = s->plane[plane].subband[14];
1197 high = s->plane[plane].subband[16];
1198 output = s->plane[plane].l_h[7];
1199 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1201 low = s->plane[plane].l_h[9];
1202 high = s->plane[plane].subband[12];
1203 output = s->plane[plane].l_h[8];
1204 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1206 low = s->plane[plane].subband[11];
1207 high = s->plane[plane].subband[13];
1208 output = s->plane[plane].l_h[9];
1209 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1211 if (s->sample_type == 1)
1214 dst = (int16_t *)pic->data[act_plane];
1215 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1219 dst += pic->linesize[act_plane] >> 1;
1222 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1223 (lowpass_height * 2 > avctx->coded_height / 2 ||
1224 lowpass_width * 2 > avctx->coded_width / 2 )
1226 ret = AVERROR_INVALIDDATA;
1230 low = s->plane[plane].l_h[6];
1231 high = s->plane[plane].l_h[7];
1232 for (i = 0; i < s->plane[act_plane].height; i++) {
1233 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1234 low += output_stride;
1235 high += output_stride;
1236 dst += dst_linesize;
1239 pic->interlaced_frame = 1;
1240 low = s->plane[plane].l_h[7];
1241 high = s->plane[plane].subband[14];
1242 output = s->plane[plane].l_h[6];
1243 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1245 low = s->plane[plane].subband[15];
1246 high = s->plane[plane].subband[16];
1247 output = s->plane[plane].l_h[7];
1248 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1250 low = s->plane[plane].l_h[9];
1251 high = s->plane[plane].subband[11];
1252 output = s->plane[plane].l_h[8];
1253 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1255 low = s->plane[plane].subband[12];
1256 high = s->plane[plane].subband[13];
1257 output = s->plane[plane].l_h[9];
1258 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1260 if (s->sample_type == 1)
1263 dst = (int16_t *)pic->data[act_plane];
1264 low = s->plane[plane].l_h[6];
1265 high = s->plane[plane].l_h[7];
1266 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1267 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1268 low += output_stride * 2;
1269 high += output_stride * 2;
1270 dst += pic->linesize[act_plane];
1276 if (s->transform_type == 2 && s->sample_type == 1) {
1277 int16_t *low, *high, *dst;
1278 int output_stride, lowpass_height, lowpass_width;
1279 ptrdiff_t dst_linesize;
1281 for (plane = 0; plane < s->planes; plane++) {
1282 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1284 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1286 dst_linesize = pic->linesize[act_plane];
1288 dst_linesize = pic->linesize[act_plane] / 2;
1291 lowpass_height = s->plane[plane].band[4][1].height;
1292 output_stride = s->plane[plane].band[4][1].a_width;
1293 lowpass_width = s->plane[plane].band[4][1].width;
1295 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1296 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1297 lowpass_width < 3 || lowpass_height < 3) {
1298 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1299 ret = AVERROR(EINVAL);
1303 if (s->progressive) {
1304 dst = (int16_t *)pic->data[act_plane];
1305 low = s->plane[plane].l_h[8];
1306 high = s->plane[plane].l_h[9];
1308 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1312 dst += pic->linesize[act_plane] >> 1;
1315 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1316 (lowpass_height * 2 > avctx->coded_height / 2 ||
1317 lowpass_width * 2 > avctx->coded_width / 2 )
1319 ret = AVERROR_INVALIDDATA;
1323 for (i = 0; i < s->plane[act_plane].height; i++) {
1324 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1325 low += output_stride;
1326 high += output_stride;
1327 dst += dst_linesize;
1330 dst = (int16_t *)pic->data[act_plane];
1331 low = s->plane[plane].l_h[8];
1332 high = s->plane[plane].l_h[9];
1333 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1334 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1335 low += output_stride * 2;
1336 high += output_stride * 2;
1337 dst += pic->linesize[act_plane];
1343 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1344 process_bayer(pic, s->bpc);
1353 static av_cold int cfhd_close(AVCodecContext *avctx)
1355 CFHDContext *s = avctx->priv_data;
1359 ff_free_vlc(&s->vlc_9);
1360 ff_free_vlc(&s->vlc_18);
1366 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1368 CFHDContext *psrc = src->priv_data;
1369 CFHDContext *pdst = dst->priv_data;
1372 if (dst == src || psrc->transform_type == 0)
1375 pdst->a_format = psrc->a_format;
1376 pdst->a_width = psrc->a_width;
1377 pdst->a_height = psrc->a_height;
1378 pdst->transform_type = psrc->transform_type;
1379 pdst->progressive = psrc->progressive;
1380 pdst->planes = psrc->planes;
1382 if (!pdst->plane[0].idwt_buf) {
1383 pdst->coded_width = pdst->a_width;
1384 pdst->coded_height = pdst->a_height;
1385 pdst->coded_format = pdst->a_format;
1386 ret = alloc_buffers(dst);
1391 for (int plane = 0; plane < pdst->planes; plane++) {
1392 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1393 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1394 pdst->plane[plane].idwt_size * sizeof(int16_t));
1401 AVCodec ff_cfhd_decoder = {
1403 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1404 .type = AVMEDIA_TYPE_VIDEO,
1405 .id = AV_CODEC_ID_CFHD,
1406 .priv_data_size = sizeof(CFHDContext),
1408 .close = cfhd_close,
1409 .decode = cfhd_decode,
1410 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1411 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1412 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,