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