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 && (s->transform_type == 0 || s->transform_type == 2)) // 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 if (s->peak.offset < 4 - bytestream2_tell(&s->peak.base) ||
621 s->peak.offset > 4 + bytestream2_get_bytes_left(&s->peak.base)
623 ret = AVERROR_INVALIDDATA;
626 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
628 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
630 if (tag == BitstreamMarker && data == 0xf0f &&
631 s->coded_format != AV_PIX_FMT_NONE) {
632 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
633 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
634 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
636 if (s->coded_width) {
637 s->coded_width *= factor;
640 if (s->coded_height) {
641 s->coded_height *= factor;
644 if (!s->a_width && !s->coded_width) {
645 s->coded_width = lowpass_width * factor * 8;
648 if (!s->a_height && !s->coded_height) {
649 s->coded_height = lowpass_height * factor * 8;
652 if (s->a_width && !s->coded_width)
653 s->coded_width = s->a_width;
654 if (s->a_height && !s->coded_height)
655 s->coded_height = s->a_height;
657 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
658 s->a_format != s->coded_format) {
660 if ((ret = alloc_buffers(avctx)) < 0) {
665 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
668 if (s->cropped_height) {
669 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
670 if (avctx->height < height)
671 return AVERROR_INVALIDDATA;
672 avctx->height = height;
677 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
682 s->coded_format = AV_PIX_FMT_NONE;
684 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
688 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
692 s->coded_format = AV_PIX_FMT_NONE;
696 if (s->subband_num_actual == 255)
698 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
700 /* Lowpass coefficients */
701 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
702 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
703 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
704 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
705 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
707 if (lowpass_width < 3 ||
708 lowpass_width > lowpass_a_width) {
709 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
710 ret = AVERROR(EINVAL);
714 if (lowpass_height < 3 ||
715 lowpass_height > lowpass_a_height) {
716 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
717 ret = AVERROR(EINVAL);
722 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
723 ret = AVERROR(EINVAL);
727 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
728 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
729 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
730 ret = AVERROR(EINVAL);
734 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
735 for (i = 0; i < lowpass_height; i++) {
736 for (j = 0; j < lowpass_width; j++)
737 coeff_data[j] = bytestream2_get_be16u(&gb);
739 coeff_data += lowpass_width;
742 /* Align to mod-4 position to continue reading tags */
743 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
745 /* Copy last line of coefficients if odd height */
746 if (lowpass_height & 1) {
747 memcpy(&coeff_data[lowpass_height * lowpass_width],
748 &coeff_data[(lowpass_height - 1) * lowpass_width],
749 lowpass_width * sizeof(*coeff_data));
752 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
755 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
756 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
757 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
758 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
759 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
760 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
762 int a_expected = highpass_a_height * highpass_a_width;
763 int level, run, coeff;
764 int count = 0, bytes;
767 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
768 ret = AVERROR(EINVAL);
772 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
773 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
774 ret = AVERROR(EINVAL);
777 expected = highpass_height * highpass_stride;
779 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);
781 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
785 OPEN_READER(re, &s->gb);
787 const int lossless = s->band_encoding == 5;
789 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
793 UPDATE_CACHE(re, &s->gb);
794 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
803 if (count > expected)
807 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
810 if (tag == BandSecondPass) {
811 const uint16_t q = s->quantisation;
813 for (i = 0; i < run; i++) {
814 *coeff_data |= coeff << 8;
818 for (i = 0; i < run; i++)
819 *coeff_data++ = coeff;
824 UPDATE_CACHE(re, &s->gb);
825 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
829 if (level == 255 && run == 2)
834 if (count > expected)
838 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
841 if (tag == BandSecondPass) {
842 const uint16_t q = s->quantisation;
844 for (i = 0; i < run; i++) {
845 *coeff_data |= coeff << 8;
849 for (i = 0; i < run; i++)
850 *coeff_data++ = coeff;
854 CLOSE_READER(re, &s->gb);
857 if (count > expected) {
858 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
859 ret = AVERROR(EINVAL);
863 peak_table(coeff_data - count, &s->peak, count);
864 if (s->difference_coding)
865 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
867 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
868 if (bytes > bytestream2_get_bytes_left(&gb)) {
869 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
870 ret = AVERROR(EINVAL);
873 bytestream2_seek(&gb, bytes, SEEK_CUR);
875 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
877 if (s->subband_num_actual != 255)
882 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
883 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
888 ff_thread_finish_setup(avctx);
890 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
891 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
892 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
893 ret = AVERROR(EINVAL);
898 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
899 ret = AVERROR(EINVAL);
903 if (s->transform_type == 0 && s->sample_type != 1) {
904 for (plane = 0; plane < s->planes && !ret; plane++) {
906 int lowpass_height = s->plane[plane].band[0][0].height;
907 int output_stride = s->plane[plane].band[0][0].a_width;
908 int lowpass_width = s->plane[plane].band[0][0].width;
909 int highpass_stride = s->plane[plane].band[0][1].stride;
910 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
911 ptrdiff_t dst_linesize;
912 int16_t *low, *high, *output, *dst;
914 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
916 dst_linesize = pic->linesize[act_plane];
918 dst_linesize = pic->linesize[act_plane] / 2;
921 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
922 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
923 lowpass_width < 3 || lowpass_height < 3) {
924 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
925 ret = AVERROR(EINVAL);
929 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
931 low = s->plane[plane].subband[0];
932 high = s->plane[plane].subband[2];
933 output = s->plane[plane].l_h[0];
934 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
936 low = s->plane[plane].subband[1];
937 high = s->plane[plane].subband[3];
938 output = s->plane[plane].l_h[1];
940 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
942 low = s->plane[plane].l_h[0];
943 high = s->plane[plane].l_h[1];
944 output = s->plane[plane].subband[0];
945 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
947 output = s->plane[plane].subband[0];
948 for (i = 0; i < lowpass_height * 2; i++) {
949 for (j = 0; j < lowpass_width * 2; j++)
952 output += output_stride * 2;
957 lowpass_height = s->plane[plane].band[1][1].height;
958 output_stride = s->plane[plane].band[1][1].a_width;
959 lowpass_width = s->plane[plane].band[1][1].width;
960 highpass_stride = s->plane[plane].band[1][1].stride;
962 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
963 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
964 lowpass_width < 3 || lowpass_height < 3) {
965 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
966 ret = AVERROR(EINVAL);
970 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
972 low = s->plane[plane].subband[0];
973 high = s->plane[plane].subband[5];
974 output = s->plane[plane].l_h[3];
975 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
977 low = s->plane[plane].subband[4];
978 high = s->plane[plane].subband[6];
979 output = s->plane[plane].l_h[4];
980 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
982 low = s->plane[plane].l_h[3];
983 high = s->plane[plane].l_h[4];
984 output = s->plane[plane].subband[0];
985 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
987 output = s->plane[plane].subband[0];
988 for (i = 0; i < lowpass_height * 2; i++) {
989 for (j = 0; j < lowpass_width * 2; j++)
992 output += output_stride * 2;
996 lowpass_height = s->plane[plane].band[2][1].height;
997 output_stride = s->plane[plane].band[2][1].a_width;
998 lowpass_width = s->plane[plane].band[2][1].width;
999 highpass_stride = s->plane[plane].band[2][1].stride;
1001 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1002 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1003 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1004 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1005 ret = AVERROR(EINVAL);
1009 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1010 if (s->progressive) {
1011 low = s->plane[plane].subband[0];
1012 high = s->plane[plane].subband[8];
1013 output = s->plane[plane].l_h[6];
1014 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1016 low = s->plane[plane].subband[7];
1017 high = s->plane[plane].subband[9];
1018 output = s->plane[plane].l_h[7];
1019 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1021 dst = (int16_t *)pic->data[act_plane];
1022 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1026 dst += pic->linesize[act_plane] >> 1;
1028 low = s->plane[plane].l_h[6];
1029 high = s->plane[plane].l_h[7];
1031 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1032 (lowpass_height * 2 > avctx->coded_height / 2 ||
1033 lowpass_width * 2 > avctx->coded_width / 2 )
1035 ret = AVERROR_INVALIDDATA;
1039 for (i = 0; i < s->plane[act_plane].height; i++) {
1040 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1041 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1042 process_alpha(dst, lowpass_width * 2);
1043 low += output_stride;
1044 high += output_stride;
1045 dst += dst_linesize;
1048 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1049 pic->interlaced_frame = 1;
1050 low = s->plane[plane].subband[0];
1051 high = s->plane[plane].subband[7];
1052 output = s->plane[plane].l_h[6];
1053 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1055 low = s->plane[plane].subband[8];
1056 high = s->plane[plane].subband[9];
1057 output = s->plane[plane].l_h[7];
1058 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1060 dst = (int16_t *)pic->data[act_plane];
1061 low = s->plane[plane].l_h[6];
1062 high = s->plane[plane].l_h[7];
1063 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1064 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1065 low += output_stride * 2;
1066 high += output_stride * 2;
1067 dst += pic->linesize[act_plane];
1071 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1072 for (plane = 0; plane < s->planes && !ret; plane++) {
1073 int lowpass_height = s->plane[plane].band[0][0].height;
1074 int output_stride = s->plane[plane].band[0][0].a_width;
1075 int lowpass_width = s->plane[plane].band[0][0].width;
1076 int highpass_stride = s->plane[plane].band[0][1].stride;
1077 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1078 int16_t *low, *high, *output, *dst;
1079 ptrdiff_t dst_linesize;
1081 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1083 dst_linesize = pic->linesize[act_plane];
1085 dst_linesize = pic->linesize[act_plane] / 2;
1088 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1089 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1090 lowpass_width < 3 || lowpass_height < 3) {
1091 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1092 ret = AVERROR(EINVAL);
1096 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1098 low = s->plane[plane].subband[0];
1099 high = s->plane[plane].subband[2];
1100 output = s->plane[plane].l_h[0];
1101 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1103 low = s->plane[plane].subband[1];
1104 high = s->plane[plane].subband[3];
1105 output = s->plane[plane].l_h[1];
1106 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1108 low = s->plane[plane].l_h[0];
1109 high = s->plane[plane].l_h[1];
1110 output = s->plane[plane].l_h[7];
1111 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1113 output = s->plane[plane].l_h[7];
1114 for (i = 0; i < lowpass_height * 2; i++) {
1115 for (j = 0; j < lowpass_width * 2; j++)
1118 output += output_stride * 2;
1122 lowpass_height = s->plane[plane].band[1][1].height;
1123 output_stride = s->plane[plane].band[1][1].a_width;
1124 lowpass_width = s->plane[plane].band[1][1].width;
1125 highpass_stride = s->plane[plane].band[1][1].stride;
1127 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1128 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1129 lowpass_width < 3 || lowpass_height < 3) {
1130 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1131 ret = AVERROR(EINVAL);
1135 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1137 low = s->plane[plane].l_h[7];
1138 high = s->plane[plane].subband[5];
1139 output = s->plane[plane].l_h[3];
1140 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1142 low = s->plane[plane].subband[4];
1143 high = s->plane[plane].subband[6];
1144 output = s->plane[plane].l_h[4];
1145 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1147 low = s->plane[plane].l_h[3];
1148 high = s->plane[plane].l_h[4];
1149 output = s->plane[plane].l_h[7];
1150 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1152 output = s->plane[plane].l_h[7];
1153 for (i = 0; i < lowpass_height * 2; i++) {
1154 for (j = 0; j < lowpass_width * 2; j++)
1156 output += output_stride * 2;
1159 low = s->plane[plane].subband[7];
1160 high = s->plane[plane].subband[9];
1161 output = s->plane[plane].l_h[3];
1162 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1164 low = s->plane[plane].subband[8];
1165 high = s->plane[plane].subband[10];
1166 output = s->plane[plane].l_h[4];
1167 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1169 low = s->plane[plane].l_h[3];
1170 high = s->plane[plane].l_h[4];
1171 output = s->plane[plane].l_h[9];
1172 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1174 lowpass_height = s->plane[plane].band[4][1].height;
1175 output_stride = s->plane[plane].band[4][1].a_width;
1176 lowpass_width = s->plane[plane].band[4][1].width;
1177 highpass_stride = s->plane[plane].band[4][1].stride;
1178 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1180 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1181 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1182 lowpass_width < 3 || lowpass_height < 3) {
1183 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1184 ret = AVERROR(EINVAL);
1188 low = s->plane[plane].l_h[7];
1189 high = s->plane[plane].l_h[9];
1190 output = s->plane[plane].l_h[7];
1191 for (i = 0; i < lowpass_height; i++) {
1192 inverse_temporal_filter(low, high, lowpass_width);
1193 low += output_stride;
1194 high += output_stride;
1196 if (s->progressive) {
1197 low = s->plane[plane].l_h[7];
1198 high = s->plane[plane].subband[15];
1199 output = s->plane[plane].l_h[6];
1200 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1202 low = s->plane[plane].subband[14];
1203 high = s->plane[plane].subband[16];
1204 output = s->plane[plane].l_h[7];
1205 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1207 low = s->plane[plane].l_h[9];
1208 high = s->plane[plane].subband[12];
1209 output = s->plane[plane].l_h[8];
1210 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1212 low = s->plane[plane].subband[11];
1213 high = s->plane[plane].subband[13];
1214 output = s->plane[plane].l_h[9];
1215 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1217 if (s->sample_type == 1)
1220 dst = (int16_t *)pic->data[act_plane];
1221 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1225 dst += pic->linesize[act_plane] >> 1;
1228 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1229 (lowpass_height * 2 > avctx->coded_height / 2 ||
1230 lowpass_width * 2 > avctx->coded_width / 2 )
1232 ret = AVERROR_INVALIDDATA;
1236 low = s->plane[plane].l_h[6];
1237 high = s->plane[plane].l_h[7];
1238 for (i = 0; i < s->plane[act_plane].height; i++) {
1239 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1240 low += output_stride;
1241 high += output_stride;
1242 dst += dst_linesize;
1245 pic->interlaced_frame = 1;
1246 low = s->plane[plane].l_h[7];
1247 high = s->plane[plane].subband[14];
1248 output = s->plane[plane].l_h[6];
1249 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1251 low = s->plane[plane].subband[15];
1252 high = s->plane[plane].subband[16];
1253 output = s->plane[plane].l_h[7];
1254 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1256 low = s->plane[plane].l_h[9];
1257 high = s->plane[plane].subband[11];
1258 output = s->plane[plane].l_h[8];
1259 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1261 low = s->plane[plane].subband[12];
1262 high = s->plane[plane].subband[13];
1263 output = s->plane[plane].l_h[9];
1264 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1266 if (s->sample_type == 1)
1269 dst = (int16_t *)pic->data[act_plane];
1270 low = s->plane[plane].l_h[6];
1271 high = s->plane[plane].l_h[7];
1272 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1273 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1274 low += output_stride * 2;
1275 high += output_stride * 2;
1276 dst += pic->linesize[act_plane];
1282 if (s->transform_type == 2 && s->sample_type == 1) {
1283 int16_t *low, *high, *dst;
1284 int output_stride, lowpass_height, lowpass_width;
1285 ptrdiff_t dst_linesize;
1287 for (plane = 0; plane < s->planes; plane++) {
1288 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1290 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1292 dst_linesize = pic->linesize[act_plane];
1294 dst_linesize = pic->linesize[act_plane] / 2;
1297 lowpass_height = s->plane[plane].band[4][1].height;
1298 output_stride = s->plane[plane].band[4][1].a_width;
1299 lowpass_width = s->plane[plane].band[4][1].width;
1301 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1302 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1303 lowpass_width < 3 || lowpass_height < 3) {
1304 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1305 ret = AVERROR(EINVAL);
1309 if (s->progressive) {
1310 dst = (int16_t *)pic->data[act_plane];
1311 low = s->plane[plane].l_h[8];
1312 high = s->plane[plane].l_h[9];
1314 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1318 dst += pic->linesize[act_plane] >> 1;
1321 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1322 (lowpass_height * 2 > avctx->coded_height / 2 ||
1323 lowpass_width * 2 > avctx->coded_width / 2 )
1325 ret = AVERROR_INVALIDDATA;
1329 for (i = 0; i < s->plane[act_plane].height; i++) {
1330 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1331 low += output_stride;
1332 high += output_stride;
1333 dst += dst_linesize;
1336 dst = (int16_t *)pic->data[act_plane];
1337 low = s->plane[plane].l_h[8];
1338 high = s->plane[plane].l_h[9];
1339 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1340 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1341 low += output_stride * 2;
1342 high += output_stride * 2;
1343 dst += pic->linesize[act_plane];
1349 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1350 process_bayer(pic, s->bpc);
1359 static av_cold int cfhd_close(AVCodecContext *avctx)
1361 CFHDContext *s = avctx->priv_data;
1365 ff_free_vlc(&s->vlc_9);
1366 ff_free_vlc(&s->vlc_18);
1372 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1374 CFHDContext *psrc = src->priv_data;
1375 CFHDContext *pdst = dst->priv_data;
1378 if (dst == src || psrc->transform_type == 0)
1381 if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1382 pdst->a_format != psrc->a_format ||
1383 pdst->a_width != psrc->a_width ||
1384 pdst->a_height != psrc->a_height)
1387 pdst->a_format = psrc->a_format;
1388 pdst->a_width = psrc->a_width;
1389 pdst->a_height = psrc->a_height;
1390 pdst->transform_type = psrc->transform_type;
1391 pdst->progressive = psrc->progressive;
1392 pdst->planes = psrc->planes;
1394 if (!pdst->plane[0].idwt_buf) {
1395 pdst->coded_width = pdst->a_width;
1396 pdst->coded_height = pdst->a_height;
1397 pdst->coded_format = pdst->a_format;
1398 ret = alloc_buffers(dst);
1403 for (int plane = 0; plane < pdst->planes; plane++) {
1404 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1405 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1406 pdst->plane[plane].idwt_size * sizeof(int16_t));
1413 AVCodec ff_cfhd_decoder = {
1415 .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1416 .type = AVMEDIA_TYPE_VIDEO,
1417 .id = AV_CODEC_ID_CFHD,
1418 .priv_data_size = sizeof(CFHDContext),
1420 .close = cfhd_close,
1421 .decode = cfhd_decode,
1422 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1423 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1424 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,