2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Cineform HD video decoder
26 #include "libavutil/attributes.h"
27 #include "libavutil/buffer.h"
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
34 #include "bytestream.h"
40 #define ALPHA_COMPAND_DC_OFFSET 256
41 #define ALPHA_COMPAND_GAIN 9400
43 static av_cold int cfhd_init(AVCodecContext *avctx)
45 CFHDContext *s = avctx->priv_data;
49 for (int i = 0; i < 64; i++) {
67 for (int i = 0; i < 256; i++)
68 s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
70 return ff_cfhd_init_vlcs(s);
73 static void init_plane_defaults(CFHDContext *s)
77 s->subband_num_actual = 0;
80 static void init_peak_table_defaults(CFHDContext *s)
84 memset(&s->peak.base, 0, sizeof(s->peak.base));
87 static void init_frame_defaults(CFHDContext *s)
91 s->coded_format = AV_PIX_FMT_YUV422P10;
92 s->cropped_height = 0;
95 s->subband_cnt = SUBBAND_COUNT;
97 s->lowpass_precision = 16;
100 s->difference_coding = 0;
103 init_plane_defaults(s);
104 init_peak_table_defaults(s);
107 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
109 if (codebook == 0 || codebook == 1) {
110 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
112 return level * quantisation;
115 static inline void difference_coding(int16_t *band, int width, int height)
119 for (i = 0; i < height; i++) {
120 for (j = 1; j < width; j++) {
121 band[j] += band[j-1];
127 static inline void peak_table(int16_t *band, Peak *peak, int length)
130 for (i = 0; i < length; i++)
131 if (abs(band[i]) > peak->level)
132 band[i] = bytestream2_get_le16(&peak->base);
135 static inline void process_alpha(int16_t *alpha, int width)
138 for (i = 0; i < width; i++) {
140 channel -= ALPHA_COMPAND_DC_OFFSET;
142 channel *= ALPHA_COMPAND_GAIN;
144 channel = av_clip_uintp2(channel, 12);
149 static inline void process_bayer(AVFrame *frame, int bpc)
151 const int linesize = frame->linesize[0];
152 uint16_t *r = (uint16_t *)frame->data[0];
153 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
154 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
155 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
156 const int mid = 1 << (bpc - 1);
157 const int factor = 1 << (16 - bpc);
159 for (int y = 0; y < frame->height >> 1; y++) {
160 for (int x = 0; x < frame->width; x += 2) {
170 R = (rg - mid) * 2 + g;
173 B = (bg - mid) * 2 + g;
175 R = av_clip_uintp2(R * factor, 16);
176 G1 = av_clip_uintp2(G1 * factor, 16);
177 G2 = av_clip_uintp2(G2 * factor, 16);
178 B = av_clip_uintp2(B * factor, 16);
193 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
194 int width, int linesize, int plane)
198 for (i = 0; i < width; i++) {
199 even = (low[i] - high[i])/2;
200 odd = (low[i] + high[i])/2;
201 output[i] = av_clip_uintp2(even, 10);
202 output[i + linesize] = av_clip_uintp2(odd, 10);
206 static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
208 for (int i = 0; i < width; i++) {
209 int even = (low[i] - high[i]) / 2;
210 int odd = (low[i] + high[i]) / 2;
217 static void free_buffers(CFHDContext *s)
221 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
222 av_freep(&s->plane[i].idwt_buf);
223 av_freep(&s->plane[i].idwt_tmp);
224 s->plane[i].idwt_size = 0;
226 for (j = 0; j < SUBBAND_COUNT_3D; j++)
227 s->plane[i].subband[j] = NULL;
229 for (j = 0; j < 10; j++)
230 s->plane[i].l_h[j] = NULL;
236 static int alloc_buffers(AVCodecContext *avctx)
238 CFHDContext *s = avctx->priv_data;
239 int i, j, ret, planes, bayer = 0;
240 int chroma_x_shift, chroma_y_shift;
243 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
245 avctx->pix_fmt = s->coded_format;
247 ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
249 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
251 &chroma_y_shift)) < 0)
253 planes = av_pix_fmt_count_planes(s->coded_format);
254 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
261 for (i = 0; i < planes; i++) {
262 int w8, h8, w4, h4, w2, h2;
263 int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
264 int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
265 ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
267 if (chroma_y_shift && !bayer)
268 height = FFALIGN(height / 8, 2) * 8;
269 s->plane[i].width = width;
270 s->plane[i].height = height;
271 s->plane[i].stride = stride;
273 w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
274 h8 = FFALIGN(height, 8) / 8;
280 if (s->transform_type == 0) {
281 s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
282 s->plane[i].idwt_buf =
283 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
284 s->plane[i].idwt_tmp =
285 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
287 s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
288 s->plane[i].idwt_buf =
289 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
290 s->plane[i].idwt_tmp =
291 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
294 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
295 return AVERROR(ENOMEM);
297 s->plane[i].subband[0] = s->plane[i].idwt_buf;
298 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
299 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
300 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
301 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
302 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
303 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
304 if (s->transform_type == 0) {
305 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
306 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
307 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
310 s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
311 s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
312 s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
313 s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
314 s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
315 s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
316 s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
317 s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
318 s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
319 s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
322 if (s->transform_type == 0) {
323 for (j = 0; j < DWT_LEVELS; j++) {
324 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
325 s->plane[i].band[j][k].a_width = w8 << j;
326 s->plane[i].band[j][k].a_height = h8 << j;
330 for (j = 0; j < DWT_LEVELS_3D; j++) {
331 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
333 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
334 s->plane[i].band[j][k].a_width = w8 << t;
335 s->plane[i].band[j][k].a_height = h8 << t;
340 /* ll2 and ll1 commented out because they are done in-place */
341 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
342 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
343 // s->plane[i].l_h[2] = ll2;
344 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
345 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
346 // s->plane[i].l_h[5] = ll1;
347 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
348 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
349 if (s->transform_type != 0) {
350 int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
352 s->plane[i].l_h[8] = frame2;
353 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
357 s->a_height = s->coded_height;
358 s->a_width = s->coded_width;
359 s->a_format = s->coded_format;
364 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
367 CFHDContext *s = avctx->priv_data;
368 CFHDDSPContext *dsp = &s->dsp;
370 ThreadFrame frame = { .f = data };
372 int ret = 0, i, j, plane, got_buffer = 0;
375 init_frame_defaults(s);
376 s->planes = av_pix_fmt_count_planes(s->coded_format);
378 bytestream2_init(&gb, avpkt->data, avpkt->size);
380 while (bytestream2_get_bytes_left(&gb) >= 4) {
381 /* Bit weird but implement the tag parsing as the spec says */
382 uint16_t tagu = bytestream2_get_be16(&gb);
383 int16_t tag = (int16_t)tagu;
384 int8_t tag8 = (int8_t)(tagu >> 8);
385 uint16_t abstag = abs(tag);
386 int8_t abs_tag8 = abs(tag8);
387 uint16_t data = bytestream2_get_be16(&gb);
388 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
389 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
390 } else if (tag == SampleFlags) {
391 av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
392 s->progressive = data & 0x0001;
393 } else if (tag == FrameType) {
394 s->frame_type = data;
395 av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
396 } else if (abstag == VersionMajor) {
397 av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
398 } else if (abstag == VersionMinor) {
399 av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
400 } else if (abstag == VersionRevision) {
401 av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
402 } else if (abstag == VersionEdit) {
403 av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
404 } else if (abstag == Version) {
405 av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
406 } else if (tag == ImageWidth) {
407 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
408 s->coded_width = data;
409 } else if (tag == ImageHeight) {
410 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
411 s->coded_height = data;
412 } else if (tag == ChannelCount) {
413 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
414 s->channel_cnt = data;
416 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
417 ret = AVERROR_PATCHWELCOME;
420 } else if (tag == SubbandCount) {
421 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
422 if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
423 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
424 ret = AVERROR_PATCHWELCOME;
427 } else if (tag == ChannelNumber) {
428 s->channel_num = data;
429 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
430 if (s->channel_num >= s->planes) {
431 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
432 ret = AVERROR(EINVAL);
435 init_plane_defaults(s);
436 } else if (tag == SubbandNumber) {
437 if (s->subband_num != 0 && data == 1) // hack
439 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
440 s->subband_num = data;
441 if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
442 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
443 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
444 ret = AVERROR(EINVAL);
447 if (s->subband_num > 3) {
448 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
449 ret = AVERROR(EINVAL);
452 } else if (tag == SubbandBand) {
453 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
454 s->subband_num_actual = data;
455 if ((s->transform_type == 0 && s->subband_num_actual >= SUBBAND_COUNT) ||
456 (s->transform_type == 2 && s->subband_num_actual >= SUBBAND_COUNT_3D && s->subband_num_actual != 255)) {
457 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
458 ret = AVERROR(EINVAL);
461 } else if (tag == LowpassPrecision)
462 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
463 else if (tag == Quantization) {
464 s->quantisation = data;
465 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
466 } else if (tag == PrescaleTable) {
467 for (i = 0; i < 8; i++)
468 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
469 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
470 } else if (tag == BandEncoding) {
471 if (!data || data > 5) {
472 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
473 ret = AVERROR(EINVAL);
476 s->band_encoding = data;
477 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
478 } else if (tag == LowpassWidth) {
479 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
480 s->plane[s->channel_num].band[0][0].width = data;
481 s->plane[s->channel_num].band[0][0].stride = data;
482 } else if (tag == LowpassHeight) {
483 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
484 s->plane[s->channel_num].band[0][0].height = data;
485 } else if (tag == SampleType) {
486 s->sample_type = data;
487 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
488 } else if (tag == TransformType) {
490 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
491 ret = AVERROR(EINVAL);
493 } else if (data == 1) {
494 av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
495 ret = AVERROR_PATCHWELCOME;
498 s->transform_type = data;
499 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
500 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
501 if (abstag == 0x4001)
503 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
504 bytestream2_skipu(&gb, data * 4);
505 } else if (tag == FrameIndex) {
506 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
507 s->frame_index = data;
508 } else if (tag == SampleIndexTable) {
509 av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
510 if (data > bytestream2_get_bytes_left(&gb) / 4) {
511 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
512 ret = AVERROR_INVALIDDATA;
515 for (i = 0; i < data; i++) {
516 uint32_t offset = bytestream2_get_be32(&gb);
517 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
519 } else if (tag == HighpassWidth) {
520 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);
522 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
523 ret = AVERROR(EINVAL);
526 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
527 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
528 } else if (tag == HighpassHeight) {
529 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
531 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
532 ret = AVERROR(EINVAL);
535 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
536 } else if (tag == BandWidth) {
537 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
539 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
540 ret = AVERROR(EINVAL);
543 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
544 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
545 } else if (tag == BandHeight) {
546 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
548 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
549 ret = AVERROR(EINVAL);
552 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
553 } else if (tag == InputFormat) {
554 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
555 if (s->coded_format == AV_PIX_FMT_NONE ||
556 s->coded_format == AV_PIX_FMT_YUV422P10) {
557 if (data >= 100 && data <= 105) {
558 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
559 } else if (data >= 122 && data <= 128) {
560 s->coded_format = AV_PIX_FMT_GBRP12;
561 } else if (data == 30) {
562 s->coded_format = AV_PIX_FMT_GBRAP12;
564 s->coded_format = AV_PIX_FMT_YUV422P10;
566 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
568 } else if (tag == BandCodingFlags) {
569 s->codebook = data & 0xf;
570 s->difference_coding = (data >> 4) & 1;
571 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
572 } else if (tag == Precision) {
573 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
574 if (!(data == 10 || data == 12)) {
575 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
576 ret = AVERROR(EINVAL);
579 avctx->bits_per_raw_sample = s->bpc = data;
580 } else if (tag == EncodedFormat) {
581 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
583 s->coded_format = AV_PIX_FMT_YUV422P10;
584 } else if (data == 2) {
585 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
586 } else if (data == 3) {
587 s->coded_format = AV_PIX_FMT_GBRP12;
588 } else if (data == 4) {
589 s->coded_format = AV_PIX_FMT_GBRAP12;
591 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
592 ret = AVERROR_PATCHWELCOME;
595 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
596 } else if (tag == -DisplayHeight) {
597 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
598 s->cropped_height = data;
599 } else if (tag == -PeakOffsetLow) {
600 s->peak.offset &= ~0xffff;
601 s->peak.offset |= (data & 0xffff);
604 } else if (tag == -PeakOffsetHigh) {
605 s->peak.offset &= 0xffff;
606 s->peak.offset |= (data & 0xffffU)<<16;
609 } else if (tag == -PeakLevel && s->peak.offset) {
610 s->peak.level = data;
611 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
613 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
615 if (tag == BitstreamMarker && data == 0xf0f &&
616 s->coded_format != AV_PIX_FMT_NONE) {
617 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
618 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
619 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
621 if (s->coded_width) {
622 s->coded_width *= factor;
625 if (s->coded_height) {
626 s->coded_height *= factor;
629 if (!s->a_width && !s->coded_width) {
630 s->coded_width = lowpass_width * factor * 8;
633 if (!s->a_height && !s->coded_height) {
634 s->coded_height = lowpass_height * factor * 8;
637 if (s->a_width && !s->coded_width)
638 s->coded_width = s->a_width;
639 if (s->a_height && !s->coded_height)
640 s->coded_height = s->a_height;
642 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
643 s->a_format != s->coded_format) {
645 if ((ret = alloc_buffers(avctx)) < 0) {
650 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
653 if (s->cropped_height) {
654 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
655 if (avctx->height < height)
656 return AVERROR_INVALIDDATA;
657 avctx->height = height;
662 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
667 s->coded_format = AV_PIX_FMT_NONE;
669 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
673 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
677 s->coded_format = AV_PIX_FMT_NONE;
681 if (s->subband_num_actual == 255)
683 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
685 /* Lowpass coefficients */
686 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
687 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
688 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
689 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
690 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
692 if (lowpass_width < 3 ||
693 lowpass_width > lowpass_a_width) {
694 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
695 ret = AVERROR(EINVAL);
699 if (lowpass_height < 3 ||
700 lowpass_height > lowpass_a_height) {
701 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
702 ret = AVERROR(EINVAL);
707 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
708 ret = AVERROR(EINVAL);
712 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
713 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
714 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
715 ret = AVERROR(EINVAL);
719 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
720 for (i = 0; i < lowpass_height; i++) {
721 for (j = 0; j < lowpass_width; j++)
722 coeff_data[j] = bytestream2_get_be16u(&gb);
724 coeff_data += lowpass_width;
727 /* Align to mod-4 position to continue reading tags */
728 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
730 /* Copy last line of coefficients if odd height */
731 if (lowpass_height & 1) {
732 memcpy(&coeff_data[lowpass_height * lowpass_width],
733 &coeff_data[(lowpass_height - 1) * lowpass_width],
734 lowpass_width * sizeof(*coeff_data));
737 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
740 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
741 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
742 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
743 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
744 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
745 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
747 int a_expected = highpass_a_height * highpass_a_width;
748 int level, run, coeff;
749 int count = 0, bytes;
752 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
753 ret = AVERROR(EINVAL);
757 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
758 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
759 ret = AVERROR(EINVAL);
762 expected = highpass_height * highpass_stride;
764 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);
766 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
770 OPEN_READER(re, &s->gb);
772 const int lossless = s->band_encoding == 5;
774 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
778 UPDATE_CACHE(re, &s->gb);
779 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
788 if (count > expected)
792 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
795 if (tag == BandSecondPass) {
796 const uint16_t q = s->quantisation;
798 for (i = 0; i < run; i++) {
799 *coeff_data |= coeff << 8;
803 for (i = 0; i < run; i++)
804 *coeff_data++ = coeff;
809 UPDATE_CACHE(re, &s->gb);
810 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
814 if (level == 255 && run == 2)
819 if (count > expected)
823 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
826 if (tag == BandSecondPass) {
827 const uint16_t q = s->quantisation;
829 for (i = 0; i < run; i++) {
830 *coeff_data |= coeff << 8;
834 for (i = 0; i < run; i++)
835 *coeff_data++ = coeff;
839 CLOSE_READER(re, &s->gb);
842 if (count > expected) {
843 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
844 ret = AVERROR(EINVAL);
848 peak_table(coeff_data - count, &s->peak, count);
849 if (s->difference_coding)
850 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
852 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
853 if (bytes > bytestream2_get_bytes_left(&gb)) {
854 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
855 ret = AVERROR(EINVAL);
858 bytestream2_seek(&gb, bytes, SEEK_CUR);
860 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
862 if (s->subband_num_actual != 255)
867 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
868 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
873 ff_thread_finish_setup(avctx);
875 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
876 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
877 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
878 ret = AVERROR(EINVAL);
883 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
884 ret = AVERROR(EINVAL);
888 if (s->transform_type == 0 && s->sample_type != 1) {
889 for (plane = 0; plane < s->planes && !ret; plane++) {
891 int lowpass_height = s->plane[plane].band[0][0].height;
892 int output_stride = s->plane[plane].band[0][0].a_width;
893 int lowpass_width = s->plane[plane].band[0][0].width;
894 int highpass_stride = s->plane[plane].band[0][1].stride;
895 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
896 ptrdiff_t dst_linesize;
897 int16_t *low, *high, *output, *dst;
899 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
901 dst_linesize = pic->linesize[act_plane];
903 dst_linesize = pic->linesize[act_plane] / 2;
906 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
907 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
908 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
909 ret = AVERROR(EINVAL);
913 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
915 low = s->plane[plane].subband[0];
916 high = s->plane[plane].subband[2];
917 output = s->plane[plane].l_h[0];
918 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
920 low = s->plane[plane].subband[1];
921 high = s->plane[plane].subband[3];
922 output = s->plane[plane].l_h[1];
924 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
926 low = s->plane[plane].l_h[0];
927 high = s->plane[plane].l_h[1];
928 output = s->plane[plane].subband[0];
929 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
931 output = s->plane[plane].subband[0];
932 for (i = 0; i < lowpass_height * 2; i++) {
933 for (j = 0; j < lowpass_width * 2; j++)
936 output += output_stride * 2;
941 lowpass_height = s->plane[plane].band[1][1].height;
942 output_stride = s->plane[plane].band[1][1].a_width;
943 lowpass_width = s->plane[plane].band[1][1].width;
944 highpass_stride = s->plane[plane].band[1][1].stride;
946 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
947 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
948 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
949 ret = AVERROR(EINVAL);
953 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
955 low = s->plane[plane].subband[0];
956 high = s->plane[plane].subband[5];
957 output = s->plane[plane].l_h[3];
958 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
960 low = s->plane[plane].subband[4];
961 high = s->plane[plane].subband[6];
962 output = s->plane[plane].l_h[4];
963 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
965 low = s->plane[plane].l_h[3];
966 high = s->plane[plane].l_h[4];
967 output = s->plane[plane].subband[0];
968 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
970 output = s->plane[plane].subband[0];
971 for (i = 0; i < lowpass_height * 2; i++) {
972 for (j = 0; j < lowpass_width * 2; j++)
975 output += output_stride * 2;
979 lowpass_height = s->plane[plane].band[2][1].height;
980 output_stride = s->plane[plane].band[2][1].a_width;
981 lowpass_width = s->plane[plane].band[2][1].width;
982 highpass_stride = s->plane[plane].band[2][1].stride;
984 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
985 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
986 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
987 ret = AVERROR(EINVAL);
991 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
992 if (s->progressive) {
993 low = s->plane[plane].subband[0];
994 high = s->plane[plane].subband[8];
995 output = s->plane[plane].l_h[6];
996 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
998 low = s->plane[plane].subband[7];
999 high = s->plane[plane].subband[9];
1000 output = s->plane[plane].l_h[7];
1001 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1003 dst = (int16_t *)pic->data[act_plane];
1004 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1008 dst += pic->linesize[act_plane] >> 1;
1010 low = s->plane[plane].l_h[6];
1011 high = s->plane[plane].l_h[7];
1013 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1014 (lowpass_height * 2 > avctx->coded_height / 2 ||
1015 lowpass_width * 2 > avctx->coded_width / 2 )
1017 ret = AVERROR_INVALIDDATA;
1021 for (i = 0; i < lowpass_height * 2; i++) {
1022 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1023 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1024 process_alpha(dst, lowpass_width * 2);
1025 low += output_stride;
1026 high += output_stride;
1027 dst += dst_linesize;
1030 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1031 pic->interlaced_frame = 1;
1032 low = s->plane[plane].subband[0];
1033 high = s->plane[plane].subband[7];
1034 output = s->plane[plane].l_h[6];
1035 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1037 low = s->plane[plane].subband[8];
1038 high = s->plane[plane].subband[9];
1039 output = s->plane[plane].l_h[7];
1040 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1042 dst = (int16_t *)pic->data[act_plane];
1043 low = s->plane[plane].l_h[6];
1044 high = s->plane[plane].l_h[7];
1045 for (i = 0; i < lowpass_height; i++) {
1046 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1047 low += output_stride * 2;
1048 high += output_stride * 2;
1049 dst += pic->linesize[act_plane];
1053 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1054 for (plane = 0; plane < s->planes && !ret; plane++) {
1055 int lowpass_height = s->plane[plane].band[0][0].height;
1056 int output_stride = s->plane[plane].band[0][0].a_width;
1057 int lowpass_width = s->plane[plane].band[0][0].width;
1058 int highpass_stride = s->plane[plane].band[0][1].stride;
1059 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1060 int16_t *low, *high, *output, *dst;
1061 ptrdiff_t dst_linesize;
1063 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1065 dst_linesize = pic->linesize[act_plane];
1067 dst_linesize = pic->linesize[act_plane] / 2;
1070 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1071 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1072 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1073 ret = AVERROR(EINVAL);
1077 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1079 low = s->plane[plane].subband[0];
1080 high = s->plane[plane].subband[2];
1081 output = s->plane[plane].l_h[0];
1082 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1084 low = s->plane[plane].subband[1];
1085 high = s->plane[plane].subband[3];
1086 output = s->plane[plane].l_h[1];
1087 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1089 low = s->plane[plane].l_h[0];
1090 high = s->plane[plane].l_h[1];
1091 output = s->plane[plane].l_h[7];
1092 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1094 output = s->plane[plane].l_h[7];
1095 for (i = 0; i < lowpass_height * 2; i++) {
1096 for (j = 0; j < lowpass_width * 2; j++)
1099 output += output_stride * 2;
1103 lowpass_height = s->plane[plane].band[1][1].height;
1104 output_stride = s->plane[plane].band[1][1].a_width;
1105 lowpass_width = s->plane[plane].band[1][1].width;
1106 highpass_stride = s->plane[plane].band[1][1].stride;
1108 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1109 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1110 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1111 ret = AVERROR(EINVAL);
1115 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1117 low = s->plane[plane].l_h[7];
1118 high = s->plane[plane].subband[5];
1119 output = s->plane[plane].l_h[3];
1120 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1122 low = s->plane[plane].subband[4];
1123 high = s->plane[plane].subband[6];
1124 output = s->plane[plane].l_h[4];
1125 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1127 low = s->plane[plane].l_h[3];
1128 high = s->plane[plane].l_h[4];
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++)
1136 output += output_stride * 2;
1139 low = s->plane[plane].subband[7];
1140 high = s->plane[plane].subband[9];
1141 output = s->plane[plane].l_h[3];
1142 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1144 low = s->plane[plane].subband[8];
1145 high = s->plane[plane].subband[10];
1146 output = s->plane[plane].l_h[4];
1147 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1149 low = s->plane[plane].l_h[3];
1150 high = s->plane[plane].l_h[4];
1151 output = s->plane[plane].l_h[9];
1152 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1154 lowpass_height = s->plane[plane].band[4][1].height;
1155 output_stride = s->plane[plane].band[4][1].a_width;
1156 lowpass_width = s->plane[plane].band[4][1].width;
1157 highpass_stride = s->plane[plane].band[4][1].stride;
1158 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1160 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1161 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1162 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1163 ret = AVERROR(EINVAL);
1167 low = s->plane[plane].l_h[7];
1168 high = s->plane[plane].l_h[9];
1169 output = s->plane[plane].l_h[7];
1170 for (i = 0; i < lowpass_height; i++) {
1171 inverse_temporal_filter(low, high, lowpass_width);
1172 low += output_stride;
1173 high += output_stride;
1175 if (s->progressive) {
1176 low = s->plane[plane].l_h[7];
1177 high = s->plane[plane].subband[15];
1178 output = s->plane[plane].l_h[6];
1179 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1181 low = s->plane[plane].subband[14];
1182 high = s->plane[plane].subband[16];
1183 output = s->plane[plane].l_h[7];
1184 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1186 low = s->plane[plane].l_h[9];
1187 high = s->plane[plane].subband[12];
1188 output = s->plane[plane].l_h[8];
1189 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1191 low = s->plane[plane].subband[11];
1192 high = s->plane[plane].subband[13];
1193 output = s->plane[plane].l_h[9];
1194 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1196 if (s->sample_type == 1)
1199 dst = (int16_t *)pic->data[act_plane];
1200 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1204 dst += pic->linesize[act_plane] >> 1;
1207 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1208 (lowpass_height * 2 > avctx->coded_height / 2 ||
1209 lowpass_width * 2 > avctx->coded_width / 2 )
1211 ret = AVERROR_INVALIDDATA;
1215 low = s->plane[plane].l_h[6];
1216 high = s->plane[plane].l_h[7];
1217 for (i = 0; i < lowpass_height * 2; i++) {
1218 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1219 low += output_stride;
1220 high += output_stride;
1221 dst += dst_linesize;
1224 pic->interlaced_frame = 1;
1225 low = s->plane[plane].l_h[7];
1226 high = s->plane[plane].subband[14];
1227 output = s->plane[plane].l_h[6];
1228 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1230 low = s->plane[plane].subband[15];
1231 high = s->plane[plane].subband[16];
1232 output = s->plane[plane].l_h[7];
1233 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1235 low = s->plane[plane].l_h[9];
1236 high = s->plane[plane].subband[11];
1237 output = s->plane[plane].l_h[8];
1238 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1240 low = s->plane[plane].subband[12];
1241 high = s->plane[plane].subband[13];
1242 output = s->plane[plane].l_h[9];
1243 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1245 if (s->sample_type == 1)
1248 dst = (int16_t *)pic->data[act_plane];
1249 low = s->plane[plane].l_h[6];
1250 high = s->plane[plane].l_h[7];
1251 for (i = 0; i < lowpass_height; i++) {
1252 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1253 low += output_stride * 2;
1254 high += output_stride * 2;
1255 dst += pic->linesize[act_plane];
1261 if (s->transform_type == 2 && s->sample_type == 1) {
1262 int16_t *low, *high, *dst;
1263 int output_stride, lowpass_height, lowpass_width;
1264 ptrdiff_t dst_linesize;
1266 for (plane = 0; plane < s->planes; plane++) {
1267 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1269 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1271 dst_linesize = pic->linesize[act_plane];
1273 dst_linesize = pic->linesize[act_plane] / 2;
1276 lowpass_height = s->plane[plane].band[4][1].height;
1277 output_stride = s->plane[plane].band[4][1].a_width;
1278 lowpass_width = s->plane[plane].band[4][1].width;
1280 if (s->progressive) {
1281 dst = (int16_t *)pic->data[act_plane];
1282 low = s->plane[plane].l_h[8];
1283 high = s->plane[plane].l_h[9];
1285 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1289 dst += pic->linesize[act_plane] >> 1;
1292 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1293 (lowpass_height * 2 > avctx->coded_height / 2 ||
1294 lowpass_width * 2 > avctx->coded_width / 2 )
1296 ret = AVERROR_INVALIDDATA;
1300 for (i = 0; i < lowpass_height * 2; i++) {
1301 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1302 low += output_stride;
1303 high += output_stride;
1304 dst += dst_linesize;
1307 dst = (int16_t *)pic->data[act_plane];
1308 low = s->plane[plane].l_h[8];
1309 high = s->plane[plane].l_h[9];
1310 for (i = 0; i < lowpass_height; i++) {
1311 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1312 low += output_stride * 2;
1313 high += output_stride * 2;
1314 dst += pic->linesize[act_plane];
1320 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1321 process_bayer(pic, s->bpc);
1330 static av_cold int cfhd_close(AVCodecContext *avctx)
1332 CFHDContext *s = avctx->priv_data;
1336 ff_free_vlc(&s->vlc_9);
1337 ff_free_vlc(&s->vlc_18);
1343 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1345 CFHDContext *psrc = src->priv_data;
1346 CFHDContext *pdst = dst->priv_data;
1349 if (dst == src || psrc->transform_type == 0)
1352 pdst->a_format = psrc->a_format;
1353 pdst->a_width = psrc->a_width;
1354 pdst->a_height = psrc->a_height;
1355 pdst->transform_type = psrc->transform_type;
1356 pdst->progressive = psrc->progressive;
1357 pdst->planes = psrc->planes;
1359 if (!pdst->plane[0].idwt_buf) {
1360 pdst->coded_width = pdst->a_width;
1361 pdst->coded_height = pdst->a_height;
1362 pdst->coded_format = pdst->a_format;
1363 ret = alloc_buffers(dst);
1368 for (int plane = 0; plane < pdst->planes; plane++) {
1369 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1370 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1371 pdst->plane[plane].idwt_size * sizeof(int16_t));
1378 AVCodec ff_cfhd_decoder = {
1380 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1381 .type = AVMEDIA_TYPE_VIDEO,
1382 .id = AV_CODEC_ID_CFHD,
1383 .priv_data_size = sizeof(CFHDContext),
1385 .close = cfhd_close,
1386 .decode = cfhd_decode,
1387 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1388 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1389 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,