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);
494 s->transform_type = data;
495 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
496 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
497 if (abstag == 0x4001)
499 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
500 bytestream2_skipu(&gb, data * 4);
501 } else if (tag == FrameIndex) {
502 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
503 s->frame_index = data;
504 } else if (tag == SampleIndexTable) {
505 av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
506 if (data > bytestream2_get_bytes_left(&gb) / 4) {
507 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
508 ret = AVERROR_INVALIDDATA;
511 for (i = 0; i < data; i++) {
512 uint32_t offset = bytestream2_get_be32(&gb);
513 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
515 } else if (tag == HighpassWidth) {
516 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);
518 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
519 ret = AVERROR(EINVAL);
522 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
523 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
524 } else if (tag == HighpassHeight) {
525 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
527 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
528 ret = AVERROR(EINVAL);
531 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
532 } else if (tag == BandWidth) {
533 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
535 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
536 ret = AVERROR(EINVAL);
539 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
540 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
541 } else if (tag == BandHeight) {
542 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
544 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
545 ret = AVERROR(EINVAL);
548 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
549 } else if (tag == InputFormat) {
550 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
551 if (s->coded_format == AV_PIX_FMT_NONE ||
552 s->coded_format == AV_PIX_FMT_YUV422P10) {
553 if (data >= 100 && data <= 105) {
554 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
555 } else if (data >= 122 && data <= 128) {
556 s->coded_format = AV_PIX_FMT_GBRP12;
557 } else if (data == 30) {
558 s->coded_format = AV_PIX_FMT_GBRAP12;
560 s->coded_format = AV_PIX_FMT_YUV422P10;
562 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
564 } else if (tag == BandCodingFlags) {
565 s->codebook = data & 0xf;
566 s->difference_coding = (data >> 4) & 1;
567 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
568 } else if (tag == Precision) {
569 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
570 if (!(data == 10 || data == 12)) {
571 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
572 ret = AVERROR(EINVAL);
575 avctx->bits_per_raw_sample = s->bpc = data;
576 } else if (tag == EncodedFormat) {
577 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
579 s->coded_format = AV_PIX_FMT_YUV422P10;
580 } else if (data == 2) {
581 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
582 } else if (data == 3) {
583 s->coded_format = AV_PIX_FMT_GBRP12;
584 } else if (data == 4) {
585 s->coded_format = AV_PIX_FMT_GBRAP12;
587 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
588 ret = AVERROR_PATCHWELCOME;
591 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
592 } else if (tag == -DisplayHeight) {
593 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
594 s->cropped_height = data;
595 } else if (tag == -PeakOffsetLow) {
596 s->peak.offset &= ~0xffff;
597 s->peak.offset |= (data & 0xffff);
600 } else if (tag == -PeakOffsetHigh) {
601 s->peak.offset &= 0xffff;
602 s->peak.offset |= (data & 0xffffU)<<16;
605 } else if (tag == -PeakLevel && s->peak.offset) {
606 s->peak.level = data;
607 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
609 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
611 if (tag == BitstreamMarker && data == 0xf0f &&
612 s->coded_format != AV_PIX_FMT_NONE) {
613 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
614 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
615 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
617 if (s->coded_width) {
618 s->coded_width *= factor;
621 if (s->coded_height) {
622 s->coded_height *= factor;
625 if (!s->a_width && !s->coded_width) {
626 s->coded_width = lowpass_width * factor * 8;
629 if (!s->a_height && !s->coded_height) {
630 s->coded_height = lowpass_height * factor * 8;
633 if (s->a_width && !s->coded_width)
634 s->coded_width = s->a_width;
635 if (s->a_height && !s->coded_height)
636 s->coded_height = s->a_height;
638 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
639 s->a_format != s->coded_format) {
641 if ((ret = alloc_buffers(avctx)) < 0) {
646 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
649 if (s->cropped_height) {
650 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
651 if (avctx->height < height)
652 return AVERROR_INVALIDDATA;
653 avctx->height = height;
658 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
663 s->coded_format = AV_PIX_FMT_NONE;
665 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
669 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
673 s->coded_format = AV_PIX_FMT_NONE;
677 if (s->subband_num_actual == 255)
679 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
681 /* Lowpass coefficients */
682 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
683 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
684 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
685 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
686 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
688 if (lowpass_width < 3 ||
689 lowpass_width > lowpass_a_width) {
690 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
691 ret = AVERROR(EINVAL);
695 if (lowpass_height < 3 ||
696 lowpass_height > lowpass_a_height) {
697 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
698 ret = AVERROR(EINVAL);
703 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
704 ret = AVERROR(EINVAL);
708 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
709 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
710 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
711 ret = AVERROR(EINVAL);
715 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
716 for (i = 0; i < lowpass_height; i++) {
717 for (j = 0; j < lowpass_width; j++)
718 coeff_data[j] = bytestream2_get_be16u(&gb);
720 coeff_data += lowpass_width;
723 /* Align to mod-4 position to continue reading tags */
724 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
726 /* Copy last line of coefficients if odd height */
727 if (lowpass_height & 1) {
728 memcpy(&coeff_data[lowpass_height * lowpass_width],
729 &coeff_data[(lowpass_height - 1) * lowpass_width],
730 lowpass_width * sizeof(*coeff_data));
733 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
736 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
737 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
738 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
739 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
740 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
741 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
743 int a_expected = highpass_a_height * highpass_a_width;
744 int level, run, coeff;
745 int count = 0, bytes;
748 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
749 ret = AVERROR(EINVAL);
753 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
754 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
755 ret = AVERROR(EINVAL);
758 expected = highpass_height * highpass_stride;
760 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);
762 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
766 OPEN_READER(re, &s->gb);
768 const int lossless = s->band_encoding == 5;
770 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
774 UPDATE_CACHE(re, &s->gb);
775 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
784 if (count > expected)
788 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
791 if (tag == BandSecondPass) {
792 const uint16_t q = s->quantisation;
794 for (i = 0; i < run; i++) {
795 *coeff_data |= coeff << 8;
799 for (i = 0; i < run; i++)
800 *coeff_data++ = coeff;
805 UPDATE_CACHE(re, &s->gb);
806 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
810 if (level == 255 && run == 2)
815 if (count > expected)
819 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
822 if (tag == BandSecondPass) {
823 const uint16_t q = s->quantisation;
825 for (i = 0; i < run; i++) {
826 *coeff_data |= coeff << 8;
830 for (i = 0; i < run; i++)
831 *coeff_data++ = coeff;
835 CLOSE_READER(re, &s->gb);
838 if (count > expected) {
839 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
840 ret = AVERROR(EINVAL);
844 peak_table(coeff_data - count, &s->peak, count);
845 if (s->difference_coding)
846 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
848 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
849 if (bytes > bytestream2_get_bytes_left(&gb)) {
850 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
851 ret = AVERROR(EINVAL);
854 bytestream2_seek(&gb, bytes, SEEK_CUR);
856 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
858 if (s->subband_num_actual != 255)
863 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
864 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
869 ff_thread_finish_setup(avctx);
871 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
872 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
873 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
874 ret = AVERROR(EINVAL);
879 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
880 ret = AVERROR(EINVAL);
884 if (s->transform_type == 0 && s->sample_type != 1) {
885 for (plane = 0; plane < s->planes && !ret; plane++) {
887 int lowpass_height = s->plane[plane].band[0][0].height;
888 int output_stride = s->plane[plane].band[0][0].a_width;
889 int lowpass_width = s->plane[plane].band[0][0].width;
890 int highpass_stride = s->plane[plane].band[0][1].stride;
891 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
892 ptrdiff_t dst_linesize;
893 int16_t *low, *high, *output, *dst;
895 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
897 dst_linesize = pic->linesize[act_plane];
899 dst_linesize = pic->linesize[act_plane] / 2;
902 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
903 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
904 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
905 ret = AVERROR(EINVAL);
909 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
911 low = s->plane[plane].subband[0];
912 high = s->plane[plane].subband[2];
913 output = s->plane[plane].l_h[0];
914 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
916 low = s->plane[plane].subband[1];
917 high = s->plane[plane].subband[3];
918 output = s->plane[plane].l_h[1];
920 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
922 low = s->plane[plane].l_h[0];
923 high = s->plane[plane].l_h[1];
924 output = s->plane[plane].subband[0];
925 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
927 output = s->plane[plane].subband[0];
928 for (i = 0; i < lowpass_height * 2; i++) {
929 for (j = 0; j < lowpass_width * 2; j++)
932 output += output_stride * 2;
937 lowpass_height = s->plane[plane].band[1][1].height;
938 output_stride = s->plane[plane].band[1][1].a_width;
939 lowpass_width = s->plane[plane].band[1][1].width;
940 highpass_stride = s->plane[plane].band[1][1].stride;
942 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
943 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
944 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
945 ret = AVERROR(EINVAL);
949 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
951 low = s->plane[plane].subband[0];
952 high = s->plane[plane].subband[5];
953 output = s->plane[plane].l_h[3];
954 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
956 low = s->plane[plane].subband[4];
957 high = s->plane[plane].subband[6];
958 output = s->plane[plane].l_h[4];
959 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
961 low = s->plane[plane].l_h[3];
962 high = s->plane[plane].l_h[4];
963 output = s->plane[plane].subband[0];
964 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
966 output = s->plane[plane].subband[0];
967 for (i = 0; i < lowpass_height * 2; i++) {
968 for (j = 0; j < lowpass_width * 2; j++)
971 output += output_stride * 2;
975 lowpass_height = s->plane[plane].band[2][1].height;
976 output_stride = s->plane[plane].band[2][1].a_width;
977 lowpass_width = s->plane[plane].band[2][1].width;
978 highpass_stride = s->plane[plane].band[2][1].stride;
980 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
981 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
982 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
983 ret = AVERROR(EINVAL);
987 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
988 if (s->progressive) {
989 low = s->plane[plane].subband[0];
990 high = s->plane[plane].subband[8];
991 output = s->plane[plane].l_h[6];
992 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
994 low = s->plane[plane].subband[7];
995 high = s->plane[plane].subband[9];
996 output = s->plane[plane].l_h[7];
997 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
999 dst = (int16_t *)pic->data[act_plane];
1000 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1004 dst += pic->linesize[act_plane] >> 1;
1006 low = s->plane[plane].l_h[6];
1007 high = s->plane[plane].l_h[7];
1009 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1010 (lowpass_height * 2 > avctx->coded_height / 2 ||
1011 lowpass_width * 2 > avctx->coded_width / 2 )
1013 ret = AVERROR_INVALIDDATA;
1017 for (i = 0; i < lowpass_height * 2; i++) {
1018 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1019 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1020 process_alpha(dst, lowpass_width * 2);
1021 low += output_stride;
1022 high += output_stride;
1023 dst += dst_linesize;
1026 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1027 pic->interlaced_frame = 1;
1028 low = s->plane[plane].subband[0];
1029 high = s->plane[plane].subband[7];
1030 output = s->plane[plane].l_h[6];
1031 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1033 low = s->plane[plane].subband[8];
1034 high = s->plane[plane].subband[9];
1035 output = s->plane[plane].l_h[7];
1036 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1038 dst = (int16_t *)pic->data[act_plane];
1039 low = s->plane[plane].l_h[6];
1040 high = s->plane[plane].l_h[7];
1041 for (i = 0; i < lowpass_height; i++) {
1042 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1043 low += output_stride * 2;
1044 high += output_stride * 2;
1045 dst += pic->linesize[act_plane];
1049 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1050 for (plane = 0; plane < s->planes && !ret; plane++) {
1051 int lowpass_height = s->plane[plane].band[0][0].height;
1052 int output_stride = s->plane[plane].band[0][0].a_width;
1053 int lowpass_width = s->plane[plane].band[0][0].width;
1054 int highpass_stride = s->plane[plane].band[0][1].stride;
1055 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1056 int16_t *low, *high, *output, *dst;
1057 ptrdiff_t dst_linesize;
1059 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1061 dst_linesize = pic->linesize[act_plane];
1063 dst_linesize = pic->linesize[act_plane] / 2;
1066 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1067 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1068 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1069 ret = AVERROR(EINVAL);
1073 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1075 low = s->plane[plane].subband[0];
1076 high = s->plane[plane].subband[2];
1077 output = s->plane[plane].l_h[0];
1078 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1080 low = s->plane[plane].subband[1];
1081 high = s->plane[plane].subband[3];
1082 output = s->plane[plane].l_h[1];
1083 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1085 low = s->plane[plane].l_h[0];
1086 high = s->plane[plane].l_h[1];
1087 output = s->plane[plane].l_h[7];
1088 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1090 output = s->plane[plane].l_h[7];
1091 for (i = 0; i < lowpass_height * 2; i++) {
1092 for (j = 0; j < lowpass_width * 2; j++)
1095 output += output_stride * 2;
1099 lowpass_height = s->plane[plane].band[1][1].height;
1100 output_stride = s->plane[plane].band[1][1].a_width;
1101 lowpass_width = s->plane[plane].band[1][1].width;
1102 highpass_stride = s->plane[plane].band[1][1].stride;
1104 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1105 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1106 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1107 ret = AVERROR(EINVAL);
1111 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1113 low = s->plane[plane].l_h[7];
1114 high = s->plane[plane].subband[5];
1115 output = s->plane[plane].l_h[3];
1116 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1118 low = s->plane[plane].subband[4];
1119 high = s->plane[plane].subband[6];
1120 output = s->plane[plane].l_h[4];
1121 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1123 low = s->plane[plane].l_h[3];
1124 high = s->plane[plane].l_h[4];
1125 output = s->plane[plane].l_h[7];
1126 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1128 output = s->plane[plane].l_h[7];
1129 for (i = 0; i < lowpass_height * 2; i++) {
1130 for (j = 0; j < lowpass_width * 2; j++)
1132 output += output_stride * 2;
1135 low = s->plane[plane].subband[7];
1136 high = s->plane[plane].subband[9];
1137 output = s->plane[plane].l_h[3];
1138 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1140 low = s->plane[plane].subband[8];
1141 high = s->plane[plane].subband[10];
1142 output = s->plane[plane].l_h[4];
1143 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1145 low = s->plane[plane].l_h[3];
1146 high = s->plane[plane].l_h[4];
1147 output = s->plane[plane].l_h[9];
1148 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1150 lowpass_height = s->plane[plane].band[4][1].height;
1151 output_stride = s->plane[plane].band[4][1].a_width;
1152 lowpass_width = s->plane[plane].band[4][1].width;
1153 highpass_stride = s->plane[plane].band[4][1].stride;
1154 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1156 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1157 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1158 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1159 ret = AVERROR(EINVAL);
1163 low = s->plane[plane].l_h[7];
1164 high = s->plane[plane].l_h[9];
1165 output = s->plane[plane].l_h[7];
1166 for (i = 0; i < lowpass_height; i++) {
1167 inverse_temporal_filter(low, high, lowpass_width);
1168 low += output_stride;
1169 high += output_stride;
1171 if (s->progressive) {
1172 low = s->plane[plane].l_h[7];
1173 high = s->plane[plane].subband[15];
1174 output = s->plane[plane].l_h[6];
1175 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1177 low = s->plane[plane].subband[14];
1178 high = s->plane[plane].subband[16];
1179 output = s->plane[plane].l_h[7];
1180 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1182 low = s->plane[plane].l_h[9];
1183 high = s->plane[plane].subband[12];
1184 output = s->plane[plane].l_h[8];
1185 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1187 low = s->plane[plane].subband[11];
1188 high = s->plane[plane].subband[13];
1189 output = s->plane[plane].l_h[9];
1190 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1192 if (s->sample_type == 1)
1195 dst = (int16_t *)pic->data[act_plane];
1196 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1200 dst += pic->linesize[act_plane] >> 1;
1203 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1204 (lowpass_height * 2 > avctx->coded_height / 2 ||
1205 lowpass_width * 2 > avctx->coded_width / 2 )
1207 ret = AVERROR_INVALIDDATA;
1211 low = s->plane[plane].l_h[6];
1212 high = s->plane[plane].l_h[7];
1213 for (i = 0; i < lowpass_height * 2; i++) {
1214 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1215 low += output_stride;
1216 high += output_stride;
1217 dst += dst_linesize;
1220 pic->interlaced_frame = 1;
1221 low = s->plane[plane].l_h[7];
1222 high = s->plane[plane].subband[14];
1223 output = s->plane[plane].l_h[6];
1224 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1226 low = s->plane[plane].subband[15];
1227 high = s->plane[plane].subband[16];
1228 output = s->plane[plane].l_h[7];
1229 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1231 low = s->plane[plane].l_h[9];
1232 high = s->plane[plane].subband[11];
1233 output = s->plane[plane].l_h[8];
1234 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1236 low = s->plane[plane].subband[12];
1237 high = s->plane[plane].subband[13];
1238 output = s->plane[plane].l_h[9];
1239 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1241 if (s->sample_type == 1)
1244 dst = (int16_t *)pic->data[act_plane];
1245 low = s->plane[plane].l_h[6];
1246 high = s->plane[plane].l_h[7];
1247 for (i = 0; i < lowpass_height; i++) {
1248 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1249 low += output_stride * 2;
1250 high += output_stride * 2;
1251 dst += pic->linesize[act_plane];
1257 if (s->transform_type == 2 && s->sample_type == 1) {
1258 int16_t *low, *high, *dst;
1259 int output_stride, lowpass_height, lowpass_width;
1260 ptrdiff_t dst_linesize;
1262 for (plane = 0; plane < s->planes; plane++) {
1263 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1265 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1267 dst_linesize = pic->linesize[act_plane];
1269 dst_linesize = pic->linesize[act_plane] / 2;
1272 lowpass_height = s->plane[plane].band[4][1].height;
1273 output_stride = s->plane[plane].band[4][1].a_width;
1274 lowpass_width = s->plane[plane].band[4][1].width;
1276 if (s->progressive) {
1277 dst = (int16_t *)pic->data[act_plane];
1278 low = s->plane[plane].l_h[8];
1279 high = s->plane[plane].l_h[9];
1281 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1285 dst += pic->linesize[act_plane] >> 1;
1288 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1289 (lowpass_height * 2 > avctx->coded_height / 2 ||
1290 lowpass_width * 2 > avctx->coded_width / 2 )
1292 ret = AVERROR_INVALIDDATA;
1296 for (i = 0; i < lowpass_height * 2; i++) {
1297 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1298 low += output_stride;
1299 high += output_stride;
1300 dst += dst_linesize;
1303 dst = (int16_t *)pic->data[act_plane];
1304 low = s->plane[plane].l_h[8];
1305 high = s->plane[plane].l_h[9];
1306 for (i = 0; i < lowpass_height; i++) {
1307 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1308 low += output_stride * 2;
1309 high += output_stride * 2;
1310 dst += pic->linesize[act_plane];
1316 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1317 process_bayer(pic, s->bpc);
1326 static av_cold int cfhd_close(AVCodecContext *avctx)
1328 CFHDContext *s = avctx->priv_data;
1332 ff_free_vlc(&s->vlc_9);
1333 ff_free_vlc(&s->vlc_18);
1339 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1341 CFHDContext *psrc = src->priv_data;
1342 CFHDContext *pdst = dst->priv_data;
1345 if (dst == src || psrc->transform_type == 0)
1348 pdst->a_format = psrc->a_format;
1349 pdst->a_width = psrc->a_width;
1350 pdst->a_height = psrc->a_height;
1351 pdst->transform_type = psrc->transform_type;
1352 pdst->progressive = psrc->progressive;
1353 pdst->planes = psrc->planes;
1355 if (!pdst->plane[0].idwt_buf) {
1356 pdst->coded_width = pdst->a_width;
1357 pdst->coded_height = pdst->a_height;
1358 pdst->coded_format = pdst->a_format;
1359 ret = alloc_buffers(dst);
1364 for (int plane = 0; plane < pdst->planes; plane++) {
1365 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1366 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1367 pdst->plane[plane].idwt_size * sizeof(int16_t));
1374 AVCodec ff_cfhd_decoder = {
1376 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1377 .type = AVMEDIA_TYPE_VIDEO,
1378 .id = AV_CODEC_ID_CFHD,
1379 .priv_data_size = sizeof(CFHDContext),
1381 .close = cfhd_close,
1382 .decode = cfhd_decode,
1383 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1384 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1385 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,