1 /*
2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * Cineform HD video decoder
24 */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/buffer.h"
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/opt.h"
33
34 #include "avcodec.h"
35 #include "bytestream.h"
36 #include "codec_internal.h"
37 #include "get_bits.h"
38 #include "internal.h"
39 #include "thread.h"
40 #include "cfhd.h"
41
42 #define ALPHA_COMPAND_DC_OFFSET 256
43 #define ALPHA_COMPAND_GAIN 9400
44
cfhd_init(AVCodecContext * avctx)45 static av_cold int cfhd_init(AVCodecContext *avctx)
46 {
47 CFHDContext *s = avctx->priv_data;
48
49 s->avctx = avctx;
50
51 for (int i = 0; i < 64; i++) {
52 int val = i;
53
54 if (val >= 40) {
55 if (val >= 54) {
56 val -= 54;
57 val <<= 2;
58 val += 54;
59 }
60
61 val -= 40;
62 val <<= 2;
63 val += 40;
64 }
65
66 s->lut[0][i] = val;
67 }
68
69 for (int i = 0; i < 256; i++)
70 s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
71
72 return ff_cfhd_init_vlcs(s);
73 }
74
init_plane_defaults(CFHDContext * s)75 static void init_plane_defaults(CFHDContext *s)
76 {
77 s->subband_num = 0;
78 s->level = 0;
79 s->subband_num_actual = 0;
80 }
81
init_peak_table_defaults(CFHDContext * s)82 static void init_peak_table_defaults(CFHDContext *s)
83 {
84 s->peak.level = 0;
85 s->peak.offset = 0;
86 memset(&s->peak.base, 0, sizeof(s->peak.base));
87 }
88
init_frame_defaults(CFHDContext * s)89 static void init_frame_defaults(CFHDContext *s)
90 {
91 s->coded_width = 0;
92 s->coded_height = 0;
93 s->coded_format = AV_PIX_FMT_YUV422P10;
94 s->cropped_height = 0;
95 s->bpc = 10;
96 s->channel_cnt = 3;
97 s->subband_cnt = SUBBAND_COUNT;
98 s->channel_num = 0;
99 s->lowpass_precision = 16;
100 s->quantisation = 1;
101 s->codebook = 0;
102 s->difference_coding = 0;
103 s->frame_type = 0;
104 s->sample_type = 0;
105 if (s->transform_type != 2)
106 s->transform_type = -1;
107 init_plane_defaults(s);
108 init_peak_table_defaults(s);
109 }
110
dequant_and_decompand(CFHDContext * s,int level,int quantisation,int codebook)111 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
112 {
113 if (codebook == 0 || codebook == 1) {
114 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
115 } else
116 return level * quantisation;
117 }
118
difference_coding(int16_t * band,int width,int height)119 static inline void difference_coding(int16_t *band, int width, int height)
120 {
121
122 int i,j;
123 for (i = 0; i < height; i++) {
124 for (j = 1; j < width; j++) {
125 band[j] += band[j-1];
126 }
127 band += width;
128 }
129 }
130
peak_table(int16_t * band,Peak * peak,int length)131 static inline void peak_table(int16_t *band, Peak *peak, int length)
132 {
133 int i;
134 for (i = 0; i < length; i++)
135 if (abs(band[i]) > peak->level)
136 band[i] = bytestream2_get_le16(&peak->base);
137 }
138
process_alpha(int16_t * alpha,int width)139 static inline void process_alpha(int16_t *alpha, int width)
140 {
141 int i, channel;
142 for (i = 0; i < width; i++) {
143 channel = alpha[i];
144 channel -= ALPHA_COMPAND_DC_OFFSET;
145 channel <<= 3;
146 channel *= ALPHA_COMPAND_GAIN;
147 channel >>= 16;
148 channel = av_clip_uintp2(channel, 12);
149 alpha[i] = channel;
150 }
151 }
152
process_bayer(AVFrame * frame,int bpc)153 static inline void process_bayer(AVFrame *frame, int bpc)
154 {
155 const int linesize = frame->linesize[0];
156 uint16_t *r = (uint16_t *)frame->data[0];
157 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
158 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
159 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
160 const int mid = 1 << (bpc - 1);
161 const int factor = 1 << (16 - bpc);
162
163 for (int y = 0; y < frame->height >> 1; y++) {
164 for (int x = 0; x < frame->width; x += 2) {
165 int R, G1, G2, B;
166 int g, rg, bg, gd;
167
168 g = r[x];
169 rg = g1[x];
170 bg = g2[x];
171 gd = b[x];
172 gd -= mid;
173
174 R = (rg - mid) * 2 + g;
175 G1 = g + gd;
176 G2 = g - gd;
177 B = (bg - mid) * 2 + g;
178
179 R = av_clip_uintp2(R * factor, 16);
180 G1 = av_clip_uintp2(G1 * factor, 16);
181 G2 = av_clip_uintp2(G2 * factor, 16);
182 B = av_clip_uintp2(B * factor, 16);
183
184 r[x] = R;
185 g1[x] = G1;
186 g2[x] = G2;
187 b[x] = B;
188 }
189
190 r += linesize;
191 g1 += linesize;
192 g2 += linesize;
193 b += linesize;
194 }
195 }
196
interlaced_vertical_filter(int16_t * output,int16_t * low,int16_t * high,int width,int linesize,int plane)197 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
198 int width, int linesize, int plane)
199 {
200 int i;
201 int16_t even, odd;
202 for (i = 0; i < width; i++) {
203 even = (low[i] - high[i])/2;
204 odd = (low[i] + high[i])/2;
205 output[i] = av_clip_uintp2(even, 10);
206 output[i + linesize] = av_clip_uintp2(odd, 10);
207 }
208 }
209
inverse_temporal_filter(int16_t * low,int16_t * high,int width)210 static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
211 {
212 for (int i = 0; i < width; i++) {
213 int even = (low[i] - high[i]) / 2;
214 int odd = (low[i] + high[i]) / 2;
215
216 low[i] = even;
217 high[i] = odd;
218 }
219 }
220
free_buffers(CFHDContext * s)221 static void free_buffers(CFHDContext *s)
222 {
223 int i, j;
224
225 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
226 Plane *p = &s->plane[i];
227 av_freep(&s->plane[i].idwt_buf);
228 av_freep(&s->plane[i].idwt_tmp);
229 s->plane[i].idwt_size = 0;
230
231 for (j = 0; j < SUBBAND_COUNT_3D; j++)
232 s->plane[i].subband[j] = NULL;
233
234 for (j = 0; j < 10; j++)
235 s->plane[i].l_h[j] = NULL;
236
237 for (j = 0; j < DWT_LEVELS_3D; j++)
238 p->band[j][0].read_ok =
239 p->band[j][1].read_ok =
240 p->band[j][2].read_ok =
241 p->band[j][3].read_ok = 0;
242 }
243 s->a_height = 0;
244 s->a_width = 0;
245 s->a_transform_type = INT_MIN;
246 }
247
alloc_buffers(AVCodecContext * avctx)248 static int alloc_buffers(AVCodecContext *avctx)
249 {
250 CFHDContext *s = avctx->priv_data;
251 int i, j, ret, planes, bayer = 0;
252 int chroma_x_shift, chroma_y_shift;
253 unsigned k;
254
255 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
256 return ret;
257 avctx->pix_fmt = s->coded_format;
258
259 ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
260
261 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
262 &chroma_x_shift,
263 &chroma_y_shift)) < 0)
264 return ret;
265 planes = av_pix_fmt_count_planes(s->coded_format);
266 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
267 planes = 4;
268 chroma_x_shift = 1;
269 chroma_y_shift = 1;
270 bayer = 1;
271 }
272
273 for (i = 0; i < planes; i++) {
274 int w8, h8, w4, h4, w2, h2;
275 int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
276 int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
277 ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
278
279 if ((ret = av_image_check_size2(stride, height, avctx->max_pixels, s->coded_format, 0, avctx)) < 0)
280 return ret;
281
282 if (chroma_y_shift && !bayer)
283 height = FFALIGN(height / 8, 2) * 8;
284 s->plane[i].width = width;
285 s->plane[i].height = height;
286 s->plane[i].stride = stride;
287
288 w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
289 h8 = FFALIGN(height, 8) / 8;
290 w4 = w8 * 2;
291 h4 = h8 * 2;
292 w2 = w4 * 2;
293 h2 = h4 * 2;
294
295 if (s->transform_type == 0) {
296 s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
297 s->plane[i].idwt_buf =
298 av_calloc(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
299 s->plane[i].idwt_tmp =
300 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
301 } else {
302 s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
303 s->plane[i].idwt_buf =
304 av_calloc(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
305 s->plane[i].idwt_tmp =
306 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
307 }
308
309 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
310 return AVERROR(ENOMEM);
311
312 s->plane[i].subband[0] = s->plane[i].idwt_buf;
313 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
314 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
315 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
316 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
317 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
318 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
319 if (s->transform_type == 0) {
320 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
321 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
322 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
323 } else {
324 int16_t *frame2 =
325 s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
326 s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
327 s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
328 s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
329 s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
330 s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
331 s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
332 s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
333 s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
334 s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
335 }
336
337 if (s->transform_type == 0) {
338 for (j = 0; j < DWT_LEVELS; j++) {
339 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
340 s->plane[i].band[j][k].a_width = w8 << j;
341 s->plane[i].band[j][k].a_height = h8 << j;
342 }
343 }
344 } else {
345 for (j = 0; j < DWT_LEVELS_3D; j++) {
346 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
347
348 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
349 s->plane[i].band[j][k].a_width = w8 << t;
350 s->plane[i].band[j][k].a_height = h8 << t;
351 }
352 }
353 }
354
355 /* ll2 and ll1 commented out because they are done in-place */
356 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
357 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
358 // s->plane[i].l_h[2] = ll2;
359 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
360 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
361 // s->plane[i].l_h[5] = ll1;
362 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
363 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
364 if (s->transform_type != 0) {
365 int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
366
367 s->plane[i].l_h[8] = frame2;
368 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
369 }
370 }
371
372 s->a_transform_type = s->transform_type;
373 s->a_height = s->coded_height;
374 s->a_width = s->coded_width;
375 s->a_format = s->coded_format;
376
377 return 0;
378 }
379
cfhd_decode(AVCodecContext * avctx,AVFrame * pic,int * got_frame,AVPacket * avpkt)380 static int cfhd_decode(AVCodecContext *avctx, AVFrame *pic,
381 int *got_frame, AVPacket *avpkt)
382 {
383 CFHDContext *s = avctx->priv_data;
384 CFHDDSPContext *dsp = &s->dsp;
385 GetByteContext gb;
386 int ret = 0, i, j, plane, got_buffer = 0;
387 int16_t *coeff_data;
388
389 init_frame_defaults(s);
390 s->planes = av_pix_fmt_count_planes(s->coded_format);
391
392 bytestream2_init(&gb, avpkt->data, avpkt->size);
393
394 while (bytestream2_get_bytes_left(&gb) >= 4) {
395 /* Bit weird but implement the tag parsing as the spec says */
396 uint16_t tagu = bytestream2_get_be16(&gb);
397 int16_t tag = (int16_t)tagu;
398 int8_t tag8 = (int8_t)(tagu >> 8);
399 uint16_t abstag = abs(tag);
400 int8_t abs_tag8 = abs(tag8);
401 uint16_t data = bytestream2_get_be16(&gb);
402 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
403 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
404 } else if (tag == SampleFlags) {
405 av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
406 s->progressive = data & 0x0001;
407 } else if (tag == FrameType) {
408 s->frame_type = data;
409 av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
410 } else if (abstag == VersionMajor) {
411 av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
412 } else if (abstag == VersionMinor) {
413 av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
414 } else if (abstag == VersionRevision) {
415 av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
416 } else if (abstag == VersionEdit) {
417 av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
418 } else if (abstag == Version) {
419 av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
420 } else if (tag == ImageWidth) {
421 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
422 s->coded_width = data;
423 } else if (tag == ImageHeight) {
424 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
425 s->coded_height = data;
426 } else if (tag == ChannelCount) {
427 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
428 s->channel_cnt = data;
429 if (data > 4) {
430 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
431 ret = AVERROR_PATCHWELCOME;
432 goto end;
433 }
434 } else if (tag == SubbandCount) {
435 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
436 if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
437 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
438 ret = AVERROR_PATCHWELCOME;
439 goto end;
440 }
441 } else if (tag == ChannelNumber) {
442 s->channel_num = data;
443 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
444 if (s->channel_num >= s->planes) {
445 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
446 ret = AVERROR(EINVAL);
447 goto end;
448 }
449 init_plane_defaults(s);
450 } else if (tag == SubbandNumber) {
451 if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2)) // hack
452 s->level++;
453 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
454 s->subband_num = data;
455 if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
456 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
457 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
458 ret = AVERROR(EINVAL);
459 goto end;
460 }
461 if (s->subband_num > 3) {
462 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
463 ret = AVERROR(EINVAL);
464 goto end;
465 }
466 } else if (tag == SubbandBand) {
467 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
468 if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
469 (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
470 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
471 ret = AVERROR(EINVAL);
472 goto end;
473 }
474 if (s->transform_type == 0 || s->transform_type == 2)
475 s->subband_num_actual = data;
476 else
477 av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
478 } else if (tag == LowpassPrecision)
479 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
480 else if (tag == Quantization) {
481 s->quantisation = data;
482 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
483 } else if (tag == PrescaleTable) {
484 for (i = 0; i < 8; i++)
485 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
486 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
487 } else if (tag == BandEncoding) {
488 if (!data || data > 5) {
489 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
490 ret = AVERROR(EINVAL);
491 goto end;
492 }
493 s->band_encoding = data;
494 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
495 } else if (tag == LowpassWidth) {
496 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
497 s->plane[s->channel_num].band[0][0].width = data;
498 s->plane[s->channel_num].band[0][0].stride = data;
499 } else if (tag == LowpassHeight) {
500 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
501 s->plane[s->channel_num].band[0][0].height = data;
502 } else if (tag == SampleType) {
503 s->sample_type = data;
504 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
505 } else if (tag == TransformType) {
506 if (data > 2) {
507 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
508 ret = AVERROR(EINVAL);
509 goto end;
510 } else if (data == 1) {
511 av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
512 ret = AVERROR_PATCHWELCOME;
513 goto end;
514 }
515 if (s->transform_type == -1) {
516 s->transform_type = data;
517 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
518 } else {
519 av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
520 }
521 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
522 if (abstag == 0x4001)
523 s->peak.level = 0;
524 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
525 bytestream2_skipu(&gb, data * 4);
526 } else if (tag == FrameIndex) {
527 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
528 s->frame_index = data;
529 } else if (tag == SampleIndexTable) {
530 av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
531 if (data > bytestream2_get_bytes_left(&gb) / 4) {
532 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
533 ret = AVERROR_INVALIDDATA;
534 goto end;
535 }
536 for (i = 0; i < data; i++) {
537 uint32_t offset = bytestream2_get_be32(&gb);
538 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
539 }
540 } else if (tag == HighpassWidth) {
541 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);
542 if (data < 3) {
543 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
544 ret = AVERROR(EINVAL);
545 goto end;
546 }
547 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
548 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
549 } else if (tag == HighpassHeight) {
550 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
551 if (data < 3) {
552 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
553 ret = AVERROR(EINVAL);
554 goto end;
555 }
556 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
557 } else if (tag == BandWidth) {
558 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
559 if (data < 3) {
560 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
561 ret = AVERROR(EINVAL);
562 goto end;
563 }
564 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
565 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
566 } else if (tag == BandHeight) {
567 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
568 if (data < 3) {
569 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
570 ret = AVERROR(EINVAL);
571 goto end;
572 }
573 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
574 } else if (tag == InputFormat) {
575 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
576 if (s->coded_format == AV_PIX_FMT_NONE ||
577 s->coded_format == AV_PIX_FMT_YUV422P10) {
578 if (data >= 100 && data <= 105) {
579 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
580 } else if (data >= 122 && data <= 128) {
581 s->coded_format = AV_PIX_FMT_GBRP12;
582 } else if (data == 30) {
583 s->coded_format = AV_PIX_FMT_GBRAP12;
584 } else {
585 s->coded_format = AV_PIX_FMT_YUV422P10;
586 }
587 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
588 }
589 } else if (tag == BandCodingFlags) {
590 s->codebook = data & 0xf;
591 s->difference_coding = (data >> 4) & 1;
592 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
593 } else if (tag == Precision) {
594 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
595 if (!(data == 10 || data == 12)) {
596 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
597 ret = AVERROR(EINVAL);
598 goto end;
599 }
600 avctx->bits_per_raw_sample = s->bpc = data;
601 } else if (tag == EncodedFormat) {
602 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
603 if (data == 1) {
604 s->coded_format = AV_PIX_FMT_YUV422P10;
605 } else if (data == 2) {
606 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
607 } else if (data == 3) {
608 s->coded_format = AV_PIX_FMT_GBRP12;
609 } else if (data == 4) {
610 s->coded_format = AV_PIX_FMT_GBRAP12;
611 } else {
612 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
613 ret = AVERROR_PATCHWELCOME;
614 goto end;
615 }
616 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
617 } else if (tag == -DisplayHeight) {
618 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
619 s->cropped_height = data;
620 } else if (tag == -PeakOffsetLow) {
621 s->peak.offset &= ~0xffff;
622 s->peak.offset |= (data & 0xffff);
623 s->peak.base = gb;
624 s->peak.level = 0;
625 } else if (tag == -PeakOffsetHigh) {
626 s->peak.offset &= 0xffff;
627 s->peak.offset |= (data & 0xffffU)<<16;
628 s->peak.base = gb;
629 s->peak.level = 0;
630 } else if (tag == -PeakLevel && s->peak.offset) {
631 s->peak.level = data;
632 if (s->peak.offset < 4 - bytestream2_tell(&s->peak.base) ||
633 s->peak.offset > 4 + bytestream2_get_bytes_left(&s->peak.base)
634 ) {
635 ret = AVERROR_INVALIDDATA;
636 goto end;
637 }
638 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
639 } else
640 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
641
642 if (tag == BitstreamMarker && data == 0xf0f &&
643 s->coded_format != AV_PIX_FMT_NONE) {
644 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
645 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
646 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
647
648 if (s->coded_width) {
649 s->coded_width *= factor;
650 }
651
652 if (s->coded_height) {
653 s->coded_height *= factor;
654 }
655
656 if (!s->a_width && !s->coded_width) {
657 s->coded_width = lowpass_width * factor * 8;
658 }
659
660 if (!s->a_height && !s->coded_height) {
661 s->coded_height = lowpass_height * factor * 8;
662 }
663
664 if (s->a_width && !s->coded_width)
665 s->coded_width = s->a_width;
666 if (s->a_height && !s->coded_height)
667 s->coded_height = s->a_height;
668
669 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
670 s->a_format != s->coded_format ||
671 s->transform_type != s->a_transform_type) {
672 free_buffers(s);
673 if ((ret = alloc_buffers(avctx)) < 0) {
674 free_buffers(s);
675 return ret;
676 }
677 }
678 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
679 if (ret < 0)
680 return ret;
681 if (s->cropped_height) {
682 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
683 if (avctx->height < height)
684 return AVERROR_INVALIDDATA;
685 avctx->height = height;
686 }
687 pic->width = pic->height = 0;
688
689 if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0)
690 return ret;
691
692 s->coded_width = 0;
693 s->coded_height = 0;
694 s->coded_format = AV_PIX_FMT_NONE;
695 got_buffer = 1;
696 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
697 pic->width = pic->height = 0;
698
699 if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0)
700 return ret;
701 s->coded_width = 0;
702 s->coded_height = 0;
703 s->coded_format = AV_PIX_FMT_NONE;
704 got_buffer = 1;
705 }
706
707 if (s->subband_num_actual == 255)
708 goto finish;
709 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
710
711 /* Lowpass coefficients */
712 if (tag == BitstreamMarker && data == 0xf0f) {
713 int lowpass_height, lowpass_width, lowpass_a_height, lowpass_a_width;
714
715 if (!s->a_width || !s->a_height) {
716 ret = AVERROR_INVALIDDATA;
717 goto end;
718 }
719
720 lowpass_height = s->plane[s->channel_num].band[0][0].height;
721 lowpass_width = s->plane[s->channel_num].band[0][0].width;
722 lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
723 lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
724
725 if (lowpass_width < 3 ||
726 lowpass_width > lowpass_a_width) {
727 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
728 ret = AVERROR(EINVAL);
729 goto end;
730 }
731
732 if (lowpass_height < 3 ||
733 lowpass_height > lowpass_a_height) {
734 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
735 ret = AVERROR(EINVAL);
736 goto end;
737 }
738
739 if (!got_buffer) {
740 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
741 ret = AVERROR(EINVAL);
742 goto end;
743 }
744
745 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
746 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
747 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
748 ret = AVERROR(EINVAL);
749 goto end;
750 }
751
752 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
753 for (i = 0; i < lowpass_height; i++) {
754 for (j = 0; j < lowpass_width; j++)
755 coeff_data[j] = bytestream2_get_be16u(&gb);
756
757 coeff_data += lowpass_width;
758 }
759
760 /* Align to mod-4 position to continue reading tags */
761 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
762
763 /* Copy last line of coefficients if odd height */
764 if (lowpass_height & 1) {
765 memcpy(&coeff_data[lowpass_height * lowpass_width],
766 &coeff_data[(lowpass_height - 1) * lowpass_width],
767 lowpass_width * sizeof(*coeff_data));
768 }
769
770 s->plane[s->channel_num].band[0][0].read_ok = 1;
771
772 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
773 }
774
775 av_assert0(s->subband_num_actual != 255);
776 if (tag == BandHeader || tag == BandSecondPass) {
777 int highpass_height, highpass_width, highpass_a_width, highpass_a_height, highpass_stride, a_expected;
778 int expected;
779 int level, run, coeff;
780 int count = 0, bytes;
781
782 if (!s->a_width || !s->a_height) {
783 ret = AVERROR_INVALIDDATA;
784 goto end;
785 }
786
787 highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
788 highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
789 highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
790 highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
791 highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
792 a_expected = highpass_a_height * highpass_a_width;
793
794 if (!got_buffer) {
795 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
796 ret = AVERROR(EINVAL);
797 goto end;
798 }
799
800 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
801 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
802 ret = AVERROR(EINVAL);
803 goto end;
804 }
805 expected = highpass_height * highpass_stride;
806
807 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);
808
809 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
810 if (ret < 0)
811 goto end;
812 {
813 OPEN_READER(re, &s->gb);
814
815 const int lossless = s->band_encoding == 5;
816
817 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
818 s->codebook = 1;
819 if (!s->codebook) {
820 while (1) {
821 UPDATE_CACHE(re, &s->gb);
822 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
823 VLC_BITS, 3, 1);
824
825 /* escape */
826 if (level == 64)
827 break;
828
829 count += run;
830
831 if (count > expected)
832 break;
833
834 if (!lossless)
835 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
836 else
837 coeff = level;
838 if (tag == BandSecondPass) {
839 const uint16_t q = s->quantisation;
840
841 for (i = 0; i < run; i++) {
842 *coeff_data |= coeff * 256U;
843 *coeff_data++ *= q;
844 }
845 } else {
846 for (i = 0; i < run; i++)
847 *coeff_data++ = coeff;
848 }
849 }
850 } else {
851 while (1) {
852 UPDATE_CACHE(re, &s->gb);
853 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
854 VLC_BITS, 3, 1);
855
856 /* escape */
857 if (level == 255 && run == 2)
858 break;
859
860 count += run;
861
862 if (count > expected)
863 break;
864
865 if (!lossless)
866 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
867 else
868 coeff = level;
869 if (tag == BandSecondPass) {
870 const uint16_t q = s->quantisation;
871
872 for (i = 0; i < run; i++) {
873 *coeff_data |= coeff * 256U;
874 *coeff_data++ *= q;
875 }
876 } else {
877 for (i = 0; i < run; i++)
878 *coeff_data++ = coeff;
879 }
880 }
881 }
882 CLOSE_READER(re, &s->gb);
883 }
884
885 if (count > expected) {
886 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
887 ret = AVERROR(EINVAL);
888 goto end;
889 }
890 if (s->peak.level)
891 peak_table(coeff_data - count, &s->peak, count);
892 if (s->difference_coding)
893 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
894
895 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
896 if (bytes > bytestream2_get_bytes_left(&gb)) {
897 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
898 ret = AVERROR(EINVAL);
899 goto end;
900 } else
901 bytestream2_seek(&gb, bytes, SEEK_CUR);
902
903 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
904 s->plane[s->channel_num].band[s->level][s->subband_num].read_ok = 1;
905 finish:
906 if (s->subband_num_actual != 255)
907 s->codebook = 0;
908 }
909 }
910
911 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
912 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
913 s->progressive = 1;
914 s->planes = 4;
915 }
916
917 ff_thread_finish_setup(avctx);
918
919 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
920 s->a_transform_type == INT_MIN ||
921 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
922 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
923 ret = AVERROR(EINVAL);
924 goto end;
925 }
926
927 if (!got_buffer) {
928 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
929 ret = AVERROR(EINVAL);
930 goto end;
931 }
932
933 for (plane = 0; plane < s->planes; plane++) {
934 int o, level;
935
936 for (level = 0; level < (s->transform_type == 0 ? DWT_LEVELS : DWT_LEVELS_3D) ; level++) {
937 if (s->transform_type == 2)
938 if (level == 2 || level == 5)
939 continue;
940 for (o = !!level; o < 4 ; o++) {
941 if (!s->plane[plane].band[level][o].read_ok) {
942 ret = AVERROR_INVALIDDATA;
943 goto end;
944 }
945 }
946 }
947 }
948
949 if (s->transform_type == 0 && s->sample_type != 1) {
950 for (plane = 0; plane < s->planes && !ret; plane++) {
951 /* level 1 */
952 int lowpass_height = s->plane[plane].band[0][0].height;
953 int output_stride = s->plane[plane].band[0][0].a_width;
954 int lowpass_width = s->plane[plane].band[0][0].width;
955 int highpass_stride = s->plane[plane].band[0][1].stride;
956 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
957 ptrdiff_t dst_linesize;
958 int16_t *low, *high, *output, *dst;
959
960 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
961 act_plane = 0;
962 dst_linesize = pic->linesize[act_plane];
963 } else {
964 dst_linesize = pic->linesize[act_plane] / 2;
965 }
966
967 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
968 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
969 lowpass_width < 3 || lowpass_height < 3) {
970 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
971 ret = AVERROR(EINVAL);
972 goto end;
973 }
974
975 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
976
977 low = s->plane[plane].subband[0];
978 high = s->plane[plane].subband[2];
979 output = s->plane[plane].l_h[0];
980 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
981
982 low = s->plane[plane].subband[1];
983 high = s->plane[plane].subband[3];
984 output = s->plane[plane].l_h[1];
985
986 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
987
988 low = s->plane[plane].l_h[0];
989 high = s->plane[plane].l_h[1];
990 output = s->plane[plane].subband[0];
991 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
992 if (s->bpc == 12) {
993 output = s->plane[plane].subband[0];
994 for (i = 0; i < lowpass_height * 2; i++) {
995 for (j = 0; j < lowpass_width * 2; j++)
996 output[j] *= 4;
997
998 output += output_stride * 2;
999 }
1000 }
1001
1002 /* level 2 */
1003 lowpass_height = s->plane[plane].band[1][1].height;
1004 output_stride = s->plane[plane].band[1][1].a_width;
1005 lowpass_width = s->plane[plane].band[1][1].width;
1006 highpass_stride = s->plane[plane].band[1][1].stride;
1007
1008 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1009 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1010 lowpass_width < 3 || lowpass_height < 3) {
1011 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1012 ret = AVERROR(EINVAL);
1013 goto end;
1014 }
1015
1016 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1017
1018 low = s->plane[plane].subband[0];
1019 high = s->plane[plane].subband[5];
1020 output = s->plane[plane].l_h[3];
1021 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1022
1023 low = s->plane[plane].subband[4];
1024 high = s->plane[plane].subband[6];
1025 output = s->plane[plane].l_h[4];
1026 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1027
1028 low = s->plane[plane].l_h[3];
1029 high = s->plane[plane].l_h[4];
1030 output = s->plane[plane].subband[0];
1031 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1032
1033 output = s->plane[plane].subband[0];
1034 for (i = 0; i < lowpass_height * 2; i++) {
1035 for (j = 0; j < lowpass_width * 2; j++)
1036 output[j] *= 4;
1037
1038 output += output_stride * 2;
1039 }
1040
1041 /* level 3 */
1042 lowpass_height = s->plane[plane].band[2][1].height;
1043 output_stride = s->plane[plane].band[2][1].a_width;
1044 lowpass_width = s->plane[plane].band[2][1].width;
1045 highpass_stride = s->plane[plane].band[2][1].stride;
1046
1047 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1048 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1049 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1050 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1051 ret = AVERROR(EINVAL);
1052 goto end;
1053 }
1054
1055 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1056 if (s->progressive) {
1057 low = s->plane[plane].subband[0];
1058 high = s->plane[plane].subband[8];
1059 output = s->plane[plane].l_h[6];
1060 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1061
1062 low = s->plane[plane].subband[7];
1063 high = s->plane[plane].subband[9];
1064 output = s->plane[plane].l_h[7];
1065 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1066
1067 dst = (int16_t *)pic->data[act_plane];
1068 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1069 if (plane & 1)
1070 dst++;
1071 if (plane > 1)
1072 dst += pic->linesize[act_plane] >> 1;
1073 }
1074 low = s->plane[plane].l_h[6];
1075 high = s->plane[plane].l_h[7];
1076
1077 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1078 (lowpass_height * 2 > avctx->coded_height / 2 ||
1079 lowpass_width * 2 > avctx->coded_width / 2 )
1080 ) {
1081 ret = AVERROR_INVALIDDATA;
1082 goto end;
1083 }
1084
1085 for (i = 0; i < s->plane[act_plane].height; i++) {
1086 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1087 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1088 process_alpha(dst, lowpass_width * 2);
1089 low += output_stride;
1090 high += output_stride;
1091 dst += dst_linesize;
1092 }
1093 } else {
1094 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1095 pic->interlaced_frame = 1;
1096 low = s->plane[plane].subband[0];
1097 high = s->plane[plane].subband[7];
1098 output = s->plane[plane].l_h[6];
1099 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1100
1101 low = s->plane[plane].subband[8];
1102 high = s->plane[plane].subband[9];
1103 output = s->plane[plane].l_h[7];
1104 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1105
1106 dst = (int16_t *)pic->data[act_plane];
1107 low = s->plane[plane].l_h[6];
1108 high = s->plane[plane].l_h[7];
1109 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1110 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1111 low += output_stride * 2;
1112 high += output_stride * 2;
1113 dst += pic->linesize[act_plane];
1114 }
1115 }
1116 }
1117 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1118 for (plane = 0; plane < s->planes && !ret; plane++) {
1119 int lowpass_height = s->plane[plane].band[0][0].height;
1120 int output_stride = s->plane[plane].band[0][0].a_width;
1121 int lowpass_width = s->plane[plane].band[0][0].width;
1122 int highpass_stride = s->plane[plane].band[0][1].stride;
1123 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1124 int16_t *low, *high, *output, *dst;
1125 ptrdiff_t dst_linesize;
1126
1127 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1128 act_plane = 0;
1129 dst_linesize = pic->linesize[act_plane];
1130 } else {
1131 dst_linesize = pic->linesize[act_plane] / 2;
1132 }
1133
1134 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1135 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1136 lowpass_width < 3 || lowpass_height < 3) {
1137 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1138 ret = AVERROR(EINVAL);
1139 goto end;
1140 }
1141
1142 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1143
1144 low = s->plane[plane].subband[0];
1145 high = s->plane[plane].subband[2];
1146 output = s->plane[plane].l_h[0];
1147 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1148
1149 low = s->plane[plane].subband[1];
1150 high = s->plane[plane].subband[3];
1151 output = s->plane[plane].l_h[1];
1152 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1153
1154 low = s->plane[plane].l_h[0];
1155 high = s->plane[plane].l_h[1];
1156 output = s->plane[plane].l_h[7];
1157 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1158 if (s->bpc == 12) {
1159 output = s->plane[plane].l_h[7];
1160 for (i = 0; i < lowpass_height * 2; i++) {
1161 for (j = 0; j < lowpass_width * 2; j++)
1162 output[j] *= 4;
1163
1164 output += output_stride * 2;
1165 }
1166 }
1167
1168 lowpass_height = s->plane[plane].band[1][1].height;
1169 output_stride = s->plane[plane].band[1][1].a_width;
1170 lowpass_width = s->plane[plane].band[1][1].width;
1171 highpass_stride = s->plane[plane].band[1][1].stride;
1172
1173 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1174 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1175 lowpass_width < 3 || lowpass_height < 3) {
1176 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1177 ret = AVERROR(EINVAL);
1178 goto end;
1179 }
1180
1181 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1182
1183 low = s->plane[plane].l_h[7];
1184 high = s->plane[plane].subband[5];
1185 output = s->plane[plane].l_h[3];
1186 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1187
1188 low = s->plane[plane].subband[4];
1189 high = s->plane[plane].subband[6];
1190 output = s->plane[plane].l_h[4];
1191 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1192
1193 low = s->plane[plane].l_h[3];
1194 high = s->plane[plane].l_h[4];
1195 output = s->plane[plane].l_h[7];
1196 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1197
1198 output = s->plane[plane].l_h[7];
1199 for (i = 0; i < lowpass_height * 2; i++) {
1200 for (j = 0; j < lowpass_width * 2; j++)
1201 output[j] *= 4;
1202 output += output_stride * 2;
1203 }
1204
1205 low = s->plane[plane].subband[7];
1206 high = s->plane[plane].subband[9];
1207 output = s->plane[plane].l_h[3];
1208 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1209
1210 low = s->plane[plane].subband[8];
1211 high = s->plane[plane].subband[10];
1212 output = s->plane[plane].l_h[4];
1213 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1214
1215 low = s->plane[plane].l_h[3];
1216 high = s->plane[plane].l_h[4];
1217 output = s->plane[plane].l_h[9];
1218 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1219
1220 lowpass_height = s->plane[plane].band[4][1].height;
1221 output_stride = s->plane[plane].band[4][1].a_width;
1222 lowpass_width = s->plane[plane].band[4][1].width;
1223 highpass_stride = s->plane[plane].band[4][1].stride;
1224 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1225
1226 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1227 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1228 lowpass_width < 3 || lowpass_height < 3) {
1229 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1230 ret = AVERROR(EINVAL);
1231 goto end;
1232 }
1233
1234 low = s->plane[plane].l_h[7];
1235 high = s->plane[plane].l_h[9];
1236 output = s->plane[plane].l_h[7];
1237 for (i = 0; i < lowpass_height; i++) {
1238 inverse_temporal_filter(low, high, lowpass_width);
1239 low += output_stride;
1240 high += output_stride;
1241 }
1242 if (s->progressive) {
1243 low = s->plane[plane].l_h[7];
1244 high = s->plane[plane].subband[15];
1245 output = s->plane[plane].l_h[6];
1246 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1247
1248 low = s->plane[plane].subband[14];
1249 high = s->plane[plane].subband[16];
1250 output = s->plane[plane].l_h[7];
1251 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1252
1253 low = s->plane[plane].l_h[9];
1254 high = s->plane[plane].subband[12];
1255 output = s->plane[plane].l_h[8];
1256 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1257
1258 low = s->plane[plane].subband[11];
1259 high = s->plane[plane].subband[13];
1260 output = s->plane[plane].l_h[9];
1261 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1262
1263 if (s->sample_type == 1)
1264 continue;
1265
1266 dst = (int16_t *)pic->data[act_plane];
1267 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1268 if (plane & 1)
1269 dst++;
1270 if (plane > 1)
1271 dst += pic->linesize[act_plane] >> 1;
1272 }
1273
1274 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1275 (lowpass_height * 2 > avctx->coded_height / 2 ||
1276 lowpass_width * 2 > avctx->coded_width / 2 )
1277 ) {
1278 ret = AVERROR_INVALIDDATA;
1279 goto end;
1280 }
1281
1282 low = s->plane[plane].l_h[6];
1283 high = s->plane[plane].l_h[7];
1284 for (i = 0; i < s->plane[act_plane].height; i++) {
1285 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1286 low += output_stride;
1287 high += output_stride;
1288 dst += dst_linesize;
1289 }
1290 } else {
1291 pic->interlaced_frame = 1;
1292 low = s->plane[plane].l_h[7];
1293 high = s->plane[plane].subband[14];
1294 output = s->plane[plane].l_h[6];
1295 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1296
1297 low = s->plane[plane].subband[15];
1298 high = s->plane[plane].subband[16];
1299 output = s->plane[plane].l_h[7];
1300 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1301
1302 low = s->plane[plane].l_h[9];
1303 high = s->plane[plane].subband[11];
1304 output = s->plane[plane].l_h[8];
1305 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1306
1307 low = s->plane[plane].subband[12];
1308 high = s->plane[plane].subband[13];
1309 output = s->plane[plane].l_h[9];
1310 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1311
1312 if (s->sample_type == 1)
1313 continue;
1314
1315 dst = (int16_t *)pic->data[act_plane];
1316 low = s->plane[plane].l_h[6];
1317 high = s->plane[plane].l_h[7];
1318 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1319 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1320 low += output_stride * 2;
1321 high += output_stride * 2;
1322 dst += pic->linesize[act_plane];
1323 }
1324 }
1325 }
1326 }
1327
1328 if (s->transform_type == 2 && s->sample_type == 1) {
1329 int16_t *low, *high, *dst;
1330 int output_stride, lowpass_height, lowpass_width;
1331 ptrdiff_t dst_linesize;
1332
1333 for (plane = 0; plane < s->planes; plane++) {
1334 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1335
1336 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1337 act_plane = 0;
1338 dst_linesize = pic->linesize[act_plane];
1339 } else {
1340 dst_linesize = pic->linesize[act_plane] / 2;
1341 }
1342
1343 lowpass_height = s->plane[plane].band[4][1].height;
1344 output_stride = s->plane[plane].band[4][1].a_width;
1345 lowpass_width = s->plane[plane].band[4][1].width;
1346
1347 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1348 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1349 lowpass_width < 3 || lowpass_height < 3) {
1350 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1351 ret = AVERROR(EINVAL);
1352 goto end;
1353 }
1354
1355 if (s->progressive) {
1356 dst = (int16_t *)pic->data[act_plane];
1357 low = s->plane[plane].l_h[8];
1358 high = s->plane[plane].l_h[9];
1359
1360 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1361 if (plane & 1)
1362 dst++;
1363 if (plane > 1)
1364 dst += pic->linesize[act_plane] >> 1;
1365 }
1366
1367 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1368 (lowpass_height * 2 > avctx->coded_height / 2 ||
1369 lowpass_width * 2 > avctx->coded_width / 2 )
1370 ) {
1371 ret = AVERROR_INVALIDDATA;
1372 goto end;
1373 }
1374
1375 for (i = 0; i < s->plane[act_plane].height; i++) {
1376 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1377 low += output_stride;
1378 high += output_stride;
1379 dst += dst_linesize;
1380 }
1381 } else {
1382 dst = (int16_t *)pic->data[act_plane];
1383 low = s->plane[plane].l_h[8];
1384 high = s->plane[plane].l_h[9];
1385 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1386 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1387 low += output_stride * 2;
1388 high += output_stride * 2;
1389 dst += pic->linesize[act_plane];
1390 }
1391 }
1392 }
1393 }
1394
1395 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1396 process_bayer(pic, s->bpc);
1397 end:
1398 if (ret < 0)
1399 return ret;
1400
1401 *got_frame = 1;
1402 return avpkt->size;
1403 }
1404
cfhd_close(AVCodecContext * avctx)1405 static av_cold int cfhd_close(AVCodecContext *avctx)
1406 {
1407 CFHDContext *s = avctx->priv_data;
1408
1409 free_buffers(s);
1410
1411 ff_free_vlc(&s->vlc_9);
1412 ff_free_vlc(&s->vlc_18);
1413
1414 return 0;
1415 }
1416
1417 #if HAVE_THREADS
update_thread_context(AVCodecContext * dst,const AVCodecContext * src)1418 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1419 {
1420 CFHDContext *psrc = src->priv_data;
1421 CFHDContext *pdst = dst->priv_data;
1422 int ret;
1423
1424 if (dst == src || psrc->transform_type == 0)
1425 return 0;
1426
1427 if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1428 pdst->a_format != psrc->a_format ||
1429 pdst->a_width != psrc->a_width ||
1430 pdst->a_height != psrc->a_height ||
1431 pdst->a_transform_type != psrc->a_transform_type)
1432 free_buffers(pdst);
1433
1434 pdst->a_format = psrc->a_format;
1435 pdst->a_width = psrc->a_width;
1436 pdst->a_height = psrc->a_height;
1437 pdst->a_transform_type = psrc->a_transform_type;
1438 pdst->transform_type = psrc->transform_type;
1439 pdst->progressive = psrc->progressive;
1440 pdst->planes = psrc->planes;
1441
1442 if (!pdst->plane[0].idwt_buf) {
1443 pdst->coded_width = pdst->a_width;
1444 pdst->coded_height = pdst->a_height;
1445 pdst->coded_format = pdst->a_format;
1446 pdst->transform_type = pdst->a_transform_type;
1447 ret = alloc_buffers(dst);
1448 if (ret < 0)
1449 return ret;
1450 }
1451
1452 for (int plane = 0; plane < pdst->planes; plane++) {
1453 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1454 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1455 pdst->plane[plane].idwt_size * sizeof(int16_t));
1456 }
1457
1458 return 0;
1459 }
1460 #endif
1461
1462 const FFCodec ff_cfhd_decoder = {
1463 .p.name = "cfhd",
1464 .p.long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1465 .p.type = AVMEDIA_TYPE_VIDEO,
1466 .p.id = AV_CODEC_ID_CFHD,
1467 .priv_data_size = sizeof(CFHDContext),
1468 .init = cfhd_init,
1469 .close = cfhd_close,
1470 FF_CODEC_DECODE_CB(cfhd_decode),
1471 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1472 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1473 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1474 };
1475