/src/ffmpeg/libavcodec/clearvideo.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * ClearVideo decoder |
3 | | * Copyright (c) 2012-2018 Konstantin Shishkov |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * ClearVideo decoder |
25 | | */ |
26 | | |
27 | | #include "libavutil/mem.h" |
28 | | #include "libavutil/mem_internal.h" |
29 | | #include "libavutil/thread.h" |
30 | | |
31 | | #include "avcodec.h" |
32 | | #include "bytestream.h" |
33 | | #include "codec_internal.h" |
34 | | #include "decode.h" |
35 | | #include "get_bits.h" |
36 | | #include "idctdsp.h" |
37 | | #include "mathops.h" |
38 | | #include "clearvideodata.h" |
39 | | |
40 | 73.7M | #define CLV_VLC_BITS 9 |
41 | | |
42 | | typedef struct LevelCodes { |
43 | | const VLCElem *flags_cb; |
44 | | const VLCElem *mv_cb; |
45 | | const VLCElem *bias_cb; |
46 | | } LevelCodes; |
47 | | |
48 | | typedef struct MV { |
49 | | int16_t x, y; |
50 | | } MV; |
51 | | |
52 | | static const MV zero_mv = { 0 }; |
53 | | |
54 | | typedef struct MVInfo { |
55 | | int mb_w; |
56 | | int mb_h; |
57 | | int mb_size; |
58 | | int mb_stride; |
59 | | int top; |
60 | | MV *mv; |
61 | | } MVInfo; |
62 | | |
63 | | typedef struct CLVContext { |
64 | | AVCodecContext *avctx; |
65 | | IDCTDSPContext idsp; |
66 | | AVFrame *pic; |
67 | | AVFrame *prev; |
68 | | GetBitContext gb; |
69 | | int mb_width, mb_height; |
70 | | int pmb_width, pmb_height; |
71 | | MVInfo mvi; |
72 | | int tile_size; |
73 | | int tile_shift; |
74 | | int luma_dc_quant, chroma_dc_quant, ac_quant; |
75 | | DECLARE_ALIGNED(16, int16_t, block)[64]; |
76 | | int top_dc[3], left_dc[4]; |
77 | | } CLVContext; |
78 | | |
79 | | static VLCElem dc_vlc[1104], ac_vlc[554]; |
80 | | static LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V |
81 | | |
82 | | static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac, |
83 | | int ac_quant) |
84 | 37.0M | { |
85 | 37.0M | GetBitContext *gb = &ctx->gb; |
86 | 37.0M | int idx = 1, last = 0, val, skip; |
87 | | |
88 | 37.0M | memset(blk, 0, sizeof(*blk) * 64); |
89 | 37.0M | blk[0] = get_vlc2(gb, dc_vlc, CLV_VLC_BITS, 3); |
90 | | |
91 | 37.0M | if (!has_ac) |
92 | 35.9M | return 0; |
93 | | |
94 | 5.52M | while (idx < 64 && !last) { |
95 | 4.68M | val = get_vlc2(gb, ac_vlc, CLV_VLC_BITS, 2); |
96 | 4.68M | if (val < 0) |
97 | 174k | return AVERROR_INVALIDDATA; |
98 | 4.50M | if (val != 0x1BFF) { |
99 | 4.45M | last = val >> 12; |
100 | 4.45M | skip = (val >> 4) & 0xFF; |
101 | 4.45M | val &= 0xF; |
102 | 4.45M | if (get_bits1(gb)) |
103 | 2.50M | val = -val; |
104 | 4.45M | } else { |
105 | 52.7k | last = get_bits1(gb); |
106 | 52.7k | skip = get_bits(gb, 6); |
107 | 52.7k | val = get_sbits(gb, 8); |
108 | 52.7k | } |
109 | 4.50M | if (val) { |
110 | 4.50M | int aval = FFABS(val), sign = val < 0; |
111 | 4.50M | val = ac_quant * (2 * aval + 1); |
112 | 4.50M | if (!(ac_quant & 1)) |
113 | 2.91M | val--; |
114 | 4.50M | if (sign) |
115 | 2.55M | val = -val; |
116 | 4.50M | } |
117 | 4.50M | idx += skip; |
118 | 4.50M | if (idx >= 64) |
119 | 30.9k | return AVERROR_INVALIDDATA; |
120 | 4.47M | blk[ff_zigzag_direct[idx++]] = val; |
121 | 4.47M | } |
122 | | |
123 | 846k | return (idx <= 64 && last) ? 0 : -1; |
124 | 1.05M | } |
125 | | |
126 | | #define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP) \ |
127 | 588M | const int t0 = OP(2841 * blk[1 * step] + 565 * blk[7 * step]); \ |
128 | 588M | const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]); \ |
129 | 588M | const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]); \ |
130 | 588M | const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]); \ |
131 | 588M | const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]); \ |
132 | 588M | const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]); \ |
133 | 588M | const int t6 = ((blk[0 * step] + blk[4 * step]) * (1 << dshift)) + bias; \ |
134 | 588M | const int t7 = ((blk[0 * step] - blk[4 * step]) * (1 << dshift)) + bias; \ |
135 | 588M | const int t8 = t0 + t2; \ |
136 | 588M | const int t9 = t0 - t2; \ |
137 | 588M | const int tA = (int)(181U * (t9 + (t1 - t3)) + 0x80) >> 8; \ |
138 | 588M | const int tB = (int)(181U * (t9 - (t1 - t3)) + 0x80) >> 8; \ |
139 | 588M | const int tC = t1 + t3; \ |
140 | 588M | \ |
141 | 588M | blk[0 * step] = (t6 + t5 + t8) >> shift; \ |
142 | 588M | blk[1 * step] = (t7 + t4 + tA) >> shift; \ |
143 | 588M | blk[2 * step] = (t7 - t4 + tB) >> shift; \ |
144 | 588M | blk[3 * step] = (t6 - t5 + tC) >> shift; \ |
145 | 588M | blk[4 * step] = (t6 - t5 - tC) >> shift; \ |
146 | 588M | blk[5 * step] = (t7 - t4 - tB) >> shift; \ |
147 | 588M | blk[6 * step] = (t7 + t4 - tA) >> shift; \ |
148 | 588M | blk[7 * step] = (t6 + t5 - t8) >> shift; \ |
149 | | |
150 | 1.76G | #define ROP(x) x |
151 | 1.76G | #define COP(x) (((x) + 4) >> 3) |
152 | | |
153 | | static void clv_dct(int16_t *block) |
154 | 36.7M | { |
155 | 36.7M | int i; |
156 | 36.7M | int16_t *ptr; |
157 | | |
158 | 36.7M | ptr = block; |
159 | 331M | for (i = 0; i < 8; i++) { |
160 | 1.76G | DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP); |
161 | 294M | ptr += 8; |
162 | 294M | } |
163 | | |
164 | 36.7M | ptr = block; |
165 | 331M | for (i = 0; i < 8; i++) { |
166 | 1.76G | DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP); |
167 | 294M | ptr++; |
168 | 294M | } |
169 | 36.7M | } |
170 | | |
171 | | static int decode_mb(CLVContext *c, int x, int y) |
172 | 6.28M | { |
173 | 6.28M | int i, has_ac[6], off; |
174 | | |
175 | 44.0M | for (i = 0; i < 6; i++) |
176 | 37.7M | has_ac[i] = get_bits1(&c->gb); |
177 | | |
178 | 6.28M | off = x * 16 + y * 16 * c->pic->linesize[0]; |
179 | 30.9M | for (i = 0; i < 4; i++) { |
180 | 24.7M | if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0) |
181 | 171k | return AVERROR_INVALIDDATA; |
182 | 24.6M | if (!x && !(i & 1)) { |
183 | 1.50M | c->block[0] += c->top_dc[0]; |
184 | 1.50M | c->top_dc[0] = c->block[0]; |
185 | 23.1M | } else { |
186 | 23.1M | c->block[0] += c->left_dc[(i & 2) >> 1]; |
187 | 23.1M | } |
188 | 24.6M | c->left_dc[(i & 2) >> 1] = c->block[0]; |
189 | 24.6M | c->block[0] *= c->luma_dc_quant; |
190 | 24.6M | clv_dct(c->block); |
191 | 24.6M | if (i == 2) |
192 | 6.14M | off += c->pic->linesize[0] * 8; |
193 | 24.6M | c->idsp.put_pixels_clamped(c->block, |
194 | 24.6M | c->pic->data[0] + off + (i & 1) * 8, |
195 | 24.6M | c->pic->linesize[0]); |
196 | 24.6M | } |
197 | | |
198 | 6.11M | off = x * 8 + y * 8 * c->pic->linesize[1]; |
199 | 18.2M | for (i = 1; i < 3; i++) { |
200 | 12.2M | if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0) |
201 | 47.3k | return AVERROR_INVALIDDATA; |
202 | 12.1M | if (!x) { |
203 | 1.48M | c->block[0] += c->top_dc[i]; |
204 | 1.48M | c->top_dc[i] = c->block[0]; |
205 | 10.6M | } else { |
206 | 10.6M | c->block[0] += c->left_dc[i + 1]; |
207 | 10.6M | } |
208 | 12.1M | c->left_dc[i + 1] = c->block[0]; |
209 | 12.1M | c->block[0] *= c->chroma_dc_quant; |
210 | 12.1M | clv_dct(c->block); |
211 | 12.1M | c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off, |
212 | 12.1M | c->pic->linesize[i]); |
213 | 12.1M | } |
214 | | |
215 | 6.06M | return 0; |
216 | 6.11M | } |
217 | | |
218 | | static int copy_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src, |
219 | | int plane, int x, int y, int dx, int dy, int size) |
220 | 15.6M | { |
221 | 15.6M | int shift = plane > 0; |
222 | 15.6M | int sx = x + dx; |
223 | 15.6M | int sy = y + dy; |
224 | 15.6M | int sstride, dstride, soff, doff; |
225 | 15.6M | uint8_t *dbuf; |
226 | 15.6M | const uint8_t *sbuf; |
227 | 15.6M | int i; |
228 | | |
229 | 15.6M | if (x < 0 || sx < 0 || y < 0 || sy < 0 || |
230 | 15.6M | x + size > avctx->coded_width >> shift || |
231 | 15.6M | y + size > avctx->coded_height >> shift || |
232 | 15.6M | sx + size > avctx->coded_width >> shift || |
233 | 15.6M | sy + size > avctx->coded_height >> shift) |
234 | 1.81M | return AVERROR_INVALIDDATA; |
235 | | |
236 | 13.7M | sstride = src->linesize[plane]; |
237 | 13.7M | dstride = dst->linesize[plane]; |
238 | 13.7M | soff = sx + sy * sstride; |
239 | 13.7M | sbuf = src->data[plane]; |
240 | 13.7M | doff = x + y * dstride; |
241 | 13.7M | dbuf = dst->data[plane]; |
242 | | |
243 | 107M | for (i = 0; i < size; i++) { |
244 | 93.7M | uint8_t *dptr = &dbuf[doff]; |
245 | 93.7M | const uint8_t *sptr = &sbuf[soff]; |
246 | | |
247 | 93.7M | memcpy(dptr, sptr, size); |
248 | 93.7M | doff += dstride; |
249 | 93.7M | soff += sstride; |
250 | 93.7M | } |
251 | | |
252 | 13.7M | return 0; |
253 | 15.6M | } |
254 | | |
255 | | static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src, |
256 | | int plane, int x, int y, int dx, int dy, int size, int bias) |
257 | 2.76M | { |
258 | 2.76M | int shift = plane > 0; |
259 | 2.76M | int sx = x + dx; |
260 | 2.76M | int sy = y + dy; |
261 | 2.76M | int sstride = src->linesize[plane]; |
262 | 2.76M | int dstride = dst->linesize[plane]; |
263 | 2.76M | int soff = sx + sy * sstride; |
264 | 2.76M | const uint8_t *sbuf = src->data[plane]; |
265 | 2.76M | int doff = x + y * dstride; |
266 | 2.76M | uint8_t *dbuf = dst->data[plane]; |
267 | 2.76M | int i, j; |
268 | | |
269 | 2.76M | if (x < 0 || sx < 0 || y < 0 || sy < 0 || |
270 | 2.76M | x + size > avctx->coded_width >> shift || |
271 | 2.76M | y + size > avctx->coded_height >> shift || |
272 | 2.76M | sx + size > avctx->coded_width >> shift || |
273 | 2.76M | sy + size > avctx->coded_height >> shift) |
274 | 115k | return AVERROR_INVALIDDATA; |
275 | | |
276 | 10.7M | for (j = 0; j < size; j++) { |
277 | 8.13M | uint8_t *dptr = &dbuf[doff]; |
278 | 8.13M | const uint8_t *sptr = &sbuf[soff]; |
279 | | |
280 | 74.2M | for (i = 0; i < size; i++) { |
281 | 66.1M | int val = sptr[i] + bias; |
282 | | |
283 | 66.1M | dptr[i] = av_clip_uint8(val); |
284 | 66.1M | } |
285 | | |
286 | 8.13M | doff += dstride; |
287 | 8.13M | soff += sstride; |
288 | 8.13M | } |
289 | | |
290 | 2.64M | return 0; |
291 | 2.76M | } |
292 | | |
293 | | static MV *mvi_predict(MVInfo *mvi, int mb_x, int mb_y) |
294 | 2.80M | { |
295 | 2.80M | MV res, pred_mv; |
296 | 2.80M | int left_mv, right_mv, top_mv, bot_mv; |
297 | | |
298 | 2.80M | if (mvi->top) { |
299 | 1.23M | if (mb_x > 0) { |
300 | 1.22M | pred_mv = mvi->mv[mvi->mb_stride + mb_x - 1]; |
301 | 1.22M | } else { |
302 | 4.64k | pred_mv = zero_mv; |
303 | 4.64k | } |
304 | 1.57M | } else if ((mb_x == 0) || (mb_x == mvi->mb_w - 1)) { |
305 | 736k | pred_mv = mvi->mv[mb_x]; |
306 | 835k | } else { |
307 | 835k | MV A = mvi->mv[mvi->mb_stride + mb_x - 1]; |
308 | 835k | MV B = mvi->mv[ mb_x ]; |
309 | 835k | MV C = mvi->mv[ mb_x + 1]; |
310 | 835k | pred_mv.x = mid_pred(A.x, B.x, C.x); |
311 | 835k | pred_mv.y = mid_pred(A.y, B.y, C.y); |
312 | 835k | } |
313 | | |
314 | 2.80M | res = pred_mv; |
315 | | |
316 | 2.80M | left_mv = -((mb_x * mvi->mb_size)); |
317 | 2.80M | right_mv = ((mvi->mb_w - mb_x - 1) * mvi->mb_size); |
318 | 2.80M | if (res.x < left_mv) { |
319 | 10.0k | res.x = left_mv; |
320 | 10.0k | } |
321 | 2.80M | if (res.x > right_mv) { |
322 | 17.9k | res.x = right_mv; |
323 | 17.9k | } |
324 | 2.80M | top_mv = -((mb_y * mvi->mb_size)); |
325 | 2.80M | bot_mv = ((mvi->mb_h - mb_y - 1) * mvi->mb_size); |
326 | 2.80M | if (res.y < top_mv) { |
327 | 17.8k | res.y = top_mv; |
328 | 17.8k | } |
329 | 2.80M | if (res.y > bot_mv) { |
330 | 41.8k | res.y = bot_mv; |
331 | 41.8k | } |
332 | | |
333 | 2.80M | mvi->mv[mvi->mb_stride + mb_x].x = res.x; |
334 | 2.80M | mvi->mv[mvi->mb_stride + mb_x].y = res.y; |
335 | | |
336 | 2.80M | return &mvi->mv[mvi->mb_stride + mb_x]; |
337 | 2.80M | } |
338 | | |
339 | | static void mvi_update_prediction(MV *mv, MV diff) |
340 | 1.45M | { |
341 | 1.45M | mv->x += diff.x; |
342 | 1.45M | mv->y += diff.y; |
343 | 1.45M | } |
344 | | |
345 | | static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size) |
346 | 4.64k | { |
347 | 4.64k | mvi->top = 1; |
348 | 4.64k | mvi->mb_w = mb_w; |
349 | 4.64k | mvi->mb_h = mb_h; |
350 | 4.64k | mvi->mb_size = mb_size; |
351 | 4.64k | mvi->mb_stride = mb_w; |
352 | 4.64k | memset(mvi->mv, 0, sizeof(MV) * mvi->mb_stride * 2); |
353 | 4.64k | } |
354 | | |
355 | | static void mvi_update_row(MVInfo *mvi) |
356 | 508k | { |
357 | 508k | int i; |
358 | | |
359 | 508k | mvi->top = 0; |
360 | 2.76M | for (i = 0 ; i < mvi->mb_stride; i++) { |
361 | 2.25M | mvi->mv[i] = mvi->mv[mvi->mb_stride + i]; |
362 | 2.25M | } |
363 | 508k | } |
364 | | |
365 | | static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src, |
366 | | int plane, int x, int y, int dx, int dy, int size, int bias) |
367 | 14.3M | { |
368 | 14.3M | int ret; |
369 | | |
370 | 14.3M | if (!bias) { |
371 | 11.5M | ret = copy_block(avctx, dst, src, plane, x, y, dx, dy, size); |
372 | 11.5M | } else { |
373 | 2.76M | ret = copyadd_block(avctx, dst, src, plane, x, y, dx, dy, size, bias); |
374 | 2.76M | } |
375 | | |
376 | 14.3M | return ret; |
377 | 14.3M | } |
378 | | |
379 | | static int decode_tile(AVCodecContext *avctx, GetBitContext *gb, |
380 | | const LevelCodes *lc, |
381 | | AVFrame *dst, const AVFrame *src, |
382 | | int plane, int x, int y, int size, |
383 | | MV root_mv, MV *pred) |
384 | 13.5M | { |
385 | 13.5M | int i, flags = 0; |
386 | 13.5M | int16_t bias = 0; |
387 | 13.5M | MV mv = { 0 }; |
388 | 13.5M | int err; |
389 | | |
390 | 13.5M | if (lc->flags_cb) |
391 | 12.1M | flags = get_vlc2(gb, lc->flags_cb, CLV_VLC_BITS, 2); |
392 | | |
393 | 13.5M | if (lc->mv_cb) { |
394 | 10.6M | uint16_t mv_code = get_vlc2(gb, lc->mv_cb, CLV_VLC_BITS, 2); |
395 | | |
396 | 10.6M | if (mv_code != MV_ESC) { |
397 | 10.6M | mv.x = (int8_t)(mv_code & 0xff); |
398 | 10.6M | mv.y = (int8_t)(mv_code >> 8); |
399 | 10.6M | } else { |
400 | 49.0k | mv.x = get_sbits(gb, 8); |
401 | 49.0k | mv.y = get_sbits(gb, 8); |
402 | 49.0k | } |
403 | 10.6M | if (pred) |
404 | 1.45M | mvi_update_prediction(pred, mv); |
405 | 10.6M | } |
406 | 13.5M | mv.x += root_mv.x; |
407 | 13.5M | mv.y += root_mv.y; |
408 | | |
409 | 13.5M | if (lc->bias_cb) { |
410 | 9.23M | uint16_t bias_val = get_vlc2(gb, lc->bias_cb, CLV_VLC_BITS, 2); |
411 | | |
412 | 9.23M | if (bias_val != BIAS_ESC) { |
413 | 9.22M | bias = (int16_t)(bias_val); |
414 | 9.22M | } else { |
415 | 6.07k | bias = get_sbits(gb, 16); |
416 | 6.07k | } |
417 | 9.23M | } |
418 | | |
419 | 13.5M | if (flags) { |
420 | 3.64M | int hsize = size >> 1; |
421 | 16.7M | for (i = 0; i < 4; i++) { |
422 | 13.5M | int xoff = (i & 2) == 0 ? 0 : hsize; |
423 | 13.5M | int yoff = (i & 1) == 0 ? 0 : hsize; |
424 | | |
425 | 13.5M | if (flags & (1 << i)) { |
426 | 9.23M | err = decode_tile(avctx, gb, lc + 1, dst, src, plane, |
427 | 9.23M | x + xoff, y + yoff, hsize, root_mv, NULL); |
428 | 9.23M | } else { |
429 | 4.35M | err = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, |
430 | 4.35M | mv.x, mv.y, hsize, bias); |
431 | 4.35M | } |
432 | 13.5M | if (err < 0) |
433 | 514k | return err; |
434 | 13.5M | } |
435 | 9.95M | } else { |
436 | 9.95M | err = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, bias); |
437 | 9.95M | if (err < 0) |
438 | 207k | return err; |
439 | 9.95M | } |
440 | | |
441 | 12.8M | return 0; |
442 | 13.5M | } |
443 | | |
444 | | static void extend_edges(AVFrame *buf, int tile_size) |
445 | 14.1k | { |
446 | 14.1k | int comp, i, j; |
447 | | |
448 | 55.1k | for (comp = 0; comp < 3; comp++) { |
449 | 41.7k | int shift = comp > 0; |
450 | 41.7k | int w = buf->width >> shift; |
451 | 41.7k | int h = buf->height >> shift; |
452 | 41.7k | int size = comp == 0 ? tile_size : tile_size >> 1; |
453 | 41.7k | int stride = buf->linesize[comp]; |
454 | 41.7k | uint8_t *framebuf = buf->data[comp]; |
455 | | |
456 | 41.7k | int right = size - (w & (size - 1)); |
457 | 41.7k | int bottom = size - (h & (size - 1)); |
458 | | |
459 | 41.7k | if ((right == size) && (bottom == size)) { |
460 | 750 | return; |
461 | 750 | } |
462 | 40.9k | if (right != size) { |
463 | 23.9k | int off = w; |
464 | 28.6M | for (j = 0; j < h; j++) { |
465 | 214M | for (i = 0; i < right; i++) { |
466 | 185M | framebuf[off + i] = 0x80; |
467 | 185M | } |
468 | 28.6M | off += stride; |
469 | 28.6M | } |
470 | 23.9k | } |
471 | 40.9k | if (bottom != size) { |
472 | 29.5k | int off = h * stride; |
473 | 627k | for (j = 0; j < bottom; j++) { |
474 | 2.06G | for (i = 0; i < stride; i++) { |
475 | 2.06G | framebuf[off + i] = 0x80; |
476 | 2.06G | } |
477 | 598k | off += stride; |
478 | 598k | } |
479 | 29.5k | } |
480 | 40.9k | } |
481 | 14.1k | } |
482 | | |
483 | | static int clv_decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
484 | | int *got_frame, AVPacket *avpkt) |
485 | 425k | { |
486 | 425k | const uint8_t *buf = avpkt->data; |
487 | 425k | int buf_size = avpkt->size; |
488 | 425k | CLVContext *c = avctx->priv_data; |
489 | 425k | GetByteContext gb; |
490 | 425k | uint32_t frame_type; |
491 | 425k | int i, j, ret; |
492 | 425k | int mb_ret = 0; |
493 | | |
494 | 425k | bytestream2_init(&gb, buf, buf_size); |
495 | 425k | if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) { |
496 | 463 | int skip = bytestream2_get_byte(&gb); |
497 | 463 | bytestream2_skip(&gb, (skip + 1) * 8); |
498 | 463 | } |
499 | | |
500 | 425k | frame_type = bytestream2_get_byte(&gb); |
501 | | |
502 | 425k | if ((frame_type & 0x7f) == 0x30) { |
503 | 178k | *got_frame = 0; |
504 | 178k | return buf_size; |
505 | 247k | } else if (frame_type & 0x2) { |
506 | 95.1k | if (buf_size < c->mb_width * c->mb_height) { |
507 | 83.7k | av_log(avctx, AV_LOG_ERROR, "Packet too small\n"); |
508 | 83.7k | return AVERROR_INVALIDDATA; |
509 | 83.7k | } |
510 | | |
511 | 11.4k | if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) |
512 | 66 | return ret; |
513 | | |
514 | 11.3k | c->pic->flags |= AV_FRAME_FLAG_KEY; |
515 | 11.3k | c->pic->pict_type = AV_PICTURE_TYPE_I; |
516 | | |
517 | 11.3k | bytestream2_get_be32(&gb); // frame size; |
518 | 11.3k | c->ac_quant = bytestream2_get_byte(&gb); |
519 | 11.3k | c->luma_dc_quant = 32; |
520 | 11.3k | c->chroma_dc_quant = 32; |
521 | | |
522 | 11.3k | if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb), |
523 | 11.3k | buf_size - bytestream2_tell(&gb))) < 0) |
524 | 0 | return ret; |
525 | | |
526 | 45.3k | for (i = 0; i < 3; i++) |
527 | 34.0k | c->top_dc[i] = 32; |
528 | 56.7k | for (i = 0; i < 4; i++) |
529 | 45.3k | c->left_dc[i] = 32; |
530 | | |
531 | 778k | for (j = 0; j < c->mb_height; j++) { |
532 | 7.05M | for (i = 0; i < c->mb_width; i++) { |
533 | 6.28M | ret = decode_mb(c, i, j); |
534 | 6.28M | if (ret < 0) |
535 | 218k | mb_ret = ret; |
536 | 6.28M | } |
537 | 766k | } |
538 | 11.3k | extend_edges(c->pic, c->tile_size); |
539 | 152k | } else { |
540 | 152k | int plane; |
541 | | |
542 | 152k | if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb))) |
543 | 147k | return AVERROR_INVALIDDATA; |
544 | | |
545 | 5.21k | if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) |
546 | 195 | return ret; |
547 | | |
548 | 5.01k | ret = av_frame_copy(c->pic, c->prev); |
549 | 5.01k | if (ret < 0) |
550 | 369 | return ret; |
551 | | |
552 | 4.64k | if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb), |
553 | 4.64k | buf_size - bytestream2_tell(&gb))) < 0) |
554 | 0 | return ret; |
555 | | |
556 | 4.64k | mvi_reset(&c->mvi, c->pmb_width, c->pmb_height, 1 << c->tile_shift); |
557 | | |
558 | 512k | for (j = 0; j < c->pmb_height; j++) { |
559 | 3.31M | for (i = 0; i < c->pmb_width; i++) { |
560 | 2.80M | MV *mvp, mv; |
561 | 2.80M | if (get_bits_left(&c->gb) <= 0) |
562 | 1.79k | return AVERROR_INVALIDDATA; |
563 | | |
564 | 2.80M | mvp = mvi_predict(&c->mvi, i, j); |
565 | 2.80M | mv = *mvp; |
566 | 2.80M | if (get_bits1(&c->gb)) { |
567 | 5.40M | for (plane = 0; plane < 3; plane++) { |
568 | 4.05M | int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1); |
569 | 4.05M | int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1); |
570 | 4.05M | int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1); |
571 | 4.05M | int16_t mx = plane == 0 ? mv.x : mv.x / 2; |
572 | 4.05M | int16_t my = plane == 0 ? mv.y : mv.y / 2; |
573 | | |
574 | 4.05M | ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size); |
575 | 4.05M | if (ret < 0) |
576 | 1.61M | mb_ret = ret; |
577 | 4.05M | } |
578 | 1.45M | } else { |
579 | 1.45M | int x = i << c->tile_shift; |
580 | 1.45M | int y = j << c->tile_shift; |
581 | 1.45M | int size = 1 << c->tile_shift; |
582 | 1.45M | MV cmv; |
583 | | |
584 | 1.45M | ret = decode_tile(avctx, &c->gb, &lev[0], c->pic, c->prev, // Y |
585 | 1.45M | 0, x, y, size, mv, mvp); |
586 | 1.45M | if (ret < 0) |
587 | 146k | mb_ret = ret; |
588 | 1.45M | x = i << (c->tile_shift - 1); |
589 | 1.45M | y = j << (c->tile_shift - 1); |
590 | 1.45M | size = 1 << (c->tile_shift - 1); |
591 | 1.45M | cmv = *mvp; |
592 | 1.45M | cmv.x /= 2; |
593 | 1.45M | cmv.y /= 2; |
594 | 1.45M | ret = decode_tile(avctx, &c->gb, &lev[4], c->pic, c->prev, // U |
595 | 1.45M | 1, x, y, size, cmv, NULL); |
596 | 1.45M | if (ret < 0) |
597 | 84.0k | mb_ret = ret; |
598 | 1.45M | ret = decode_tile(avctx, &c->gb, &lev[7], c->pic, c->prev, // U |
599 | 1.45M | 2, x, y, size, cmv, NULL); |
600 | 1.45M | if (ret < 0) |
601 | 81.3k | mb_ret = ret; |
602 | 1.45M | } |
603 | 2.80M | } |
604 | 508k | mvi_update_row(&c->mvi); |
605 | 508k | } |
606 | 2.85k | extend_edges(c->pic, c->tile_size); |
607 | | |
608 | 2.85k | c->pic->flags &= ~AV_FRAME_FLAG_KEY; |
609 | 2.85k | c->pic->pict_type = AV_PICTURE_TYPE_P; |
610 | 2.85k | } |
611 | | |
612 | 14.1k | if ((ret = av_frame_ref(rframe, c->pic)) < 0) |
613 | 0 | return ret; |
614 | | |
615 | 14.1k | FFSWAP(AVFrame *, c->pic, c->prev); |
616 | | |
617 | 14.1k | *got_frame = 1; |
618 | | |
619 | 14.1k | if (get_bits_left(&c->gb) < 0) |
620 | 11.9k | av_log(c->avctx, AV_LOG_WARNING, "overread %d\n", -get_bits_left(&c->gb)); |
621 | | |
622 | 14.1k | return mb_ret < 0 ? mb_ret : buf_size; |
623 | 14.1k | } |
624 | | |
625 | | static av_cold const VLCElem *build_vlc(VLCInitState *state, |
626 | | const uint8_t counts[16], |
627 | | const uint16_t **syms) |
628 | 15 | { |
629 | 15 | uint8_t lens[MAX_VLC_ENTRIES]; |
630 | 15 | const uint16_t *symbols = *syms; |
631 | 15 | unsigned num = 0; |
632 | | |
633 | 255 | for (int i = 0; i < 16; i++) { |
634 | 240 | unsigned count = counts[i]; |
635 | 240 | if (count == 255) /* Special case for Y_3 table */ |
636 | 1 | count = 303; |
637 | 5.01k | for (count += num; num < count; num++) |
638 | 4.77k | lens[num] = i + 1; |
639 | 240 | } |
640 | 15 | *syms += num; |
641 | 15 | return ff_vlc_init_tables_from_lengths(state, CLV_VLC_BITS, num, lens, 1, |
642 | 15 | symbols, 2, 2, 0, 0); |
643 | 15 | } |
644 | | |
645 | | static av_cold void clv_init_static(void) |
646 | 1 | { |
647 | 1 | static VLCElem vlc_buf[16716]; |
648 | 1 | VLCInitState state = VLC_INIT_STATE(vlc_buf); |
649 | 1 | const uint16_t *mv_syms = clv_mv_syms, *bias_syms = clv_bias_syms; |
650 | | |
651 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(dc_vlc, CLV_VLC_BITS, NUM_DC_CODES, |
652 | 1 | clv_dc_lens, 1, |
653 | 1 | clv_dc_syms, 1, 1, -63, 0); |
654 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(ac_vlc, CLV_VLC_BITS, NUM_AC_CODES, |
655 | 1 | clv_ac_bits, 1, |
656 | 1 | clv_ac_syms, 2, 2, 0, 0); |
657 | 10 | for (unsigned i = 0, j = 0, k = 0;; i++) { |
658 | 10 | if (0x36F & (1 << i)) { |
659 | 8 | lev[i].mv_cb = build_vlc(&state, clv_mv_len_counts[k], &mv_syms); |
660 | 8 | k++; |
661 | 8 | } |
662 | 10 | if (i == FF_ARRAY_ELEMS(lev) - 1) |
663 | 1 | break; |
664 | 9 | if (0x1B7 & (1 << i)) { |
665 | 7 | lev[i].flags_cb = |
666 | 7 | ff_vlc_init_tables_from_lengths(&state, CLV_VLC_BITS, 16, |
667 | 7 | clv_flags_bits[j], 1, |
668 | 7 | clv_flags_syms[j], 1, 1, |
669 | 7 | 0, 0); |
670 | | |
671 | 7 | lev[i + 1].bias_cb = build_vlc(&state, clv_bias_len_counts[j], |
672 | 7 | &bias_syms); |
673 | 7 | j++; |
674 | 7 | } |
675 | 9 | } |
676 | 1 | } |
677 | | |
678 | | static av_cold int clv_decode_init(AVCodecContext *avctx) |
679 | 1.23k | { |
680 | 1.23k | static AVOnce init_static_once = AV_ONCE_INIT; |
681 | 1.23k | CLVContext *const c = avctx->priv_data; |
682 | 1.23k | int ret, w, h; |
683 | | |
684 | 1.23k | if (avctx->extradata_size == 110) { |
685 | 74 | c->tile_size = AV_RL32(&avctx->extradata[94]); |
686 | 1.15k | } else if (avctx->extradata_size == 150) { |
687 | 1 | c->tile_size = AV_RB32(&avctx->extradata[134]); |
688 | 1.15k | } else if (!avctx->extradata_size) { |
689 | 1.11k | c->tile_size = 16; |
690 | 1.11k | } else { |
691 | 42 | av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size); |
692 | 42 | return AVERROR_INVALIDDATA; |
693 | 42 | } |
694 | | |
695 | 1.18k | c->tile_shift = av_log2(c->tile_size); |
696 | 1.18k | if (1U << c->tile_shift != c->tile_size || c->tile_shift < 1 || c->tile_shift > 30) { |
697 | 20 | av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2 > 1 and < 2^31\n", c->tile_size); |
698 | 20 | return AVERROR_INVALIDDATA; |
699 | 20 | } |
700 | | |
701 | 1.16k | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
702 | 1.16k | w = avctx->width; |
703 | 1.16k | h = avctx->height; |
704 | 1.16k | ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift)); |
705 | 1.16k | if (ret < 0) |
706 | 144 | return ret; |
707 | 1.02k | avctx->width = w; |
708 | 1.02k | avctx->height = h; |
709 | | |
710 | 1.02k | c->avctx = avctx; |
711 | 1.02k | c->mb_width = FFALIGN(avctx->width, 16) >> 4; |
712 | 1.02k | c->mb_height = FFALIGN(avctx->height, 16) >> 4; |
713 | 1.02k | c->pmb_width = (w + c->tile_size - 1) >> c->tile_shift; |
714 | 1.02k | c->pmb_height = (h + c->tile_size - 1) >> c->tile_shift; |
715 | 1.02k | c->pic = av_frame_alloc(); |
716 | 1.02k | c->prev = av_frame_alloc(); |
717 | 1.02k | c->mvi.mv = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv)); |
718 | 1.02k | if (!c->pic || !c->prev || !c->mvi.mv) |
719 | 0 | return AVERROR(ENOMEM); |
720 | | |
721 | 1.02k | ff_idctdsp_init(&c->idsp, avctx); |
722 | | |
723 | 1.02k | ff_thread_once(&init_static_once, clv_init_static); |
724 | | |
725 | 1.02k | return 0; |
726 | 1.02k | } |
727 | | |
728 | | static av_cold int clv_decode_end(AVCodecContext *avctx) |
729 | 1.23k | { |
730 | 1.23k | CLVContext *const c = avctx->priv_data; |
731 | | |
732 | 1.23k | av_frame_free(&c->prev); |
733 | 1.23k | av_frame_free(&c->pic); |
734 | | |
735 | 1.23k | av_freep(&c->mvi.mv); |
736 | | |
737 | 1.23k | return 0; |
738 | 1.23k | } |
739 | | |
740 | | const FFCodec ff_clearvideo_decoder = { |
741 | | .p.name = "clearvideo", |
742 | | CODEC_LONG_NAME("Iterated Systems ClearVideo"), |
743 | | .p.type = AVMEDIA_TYPE_VIDEO, |
744 | | .p.id = AV_CODEC_ID_CLEARVIDEO, |
745 | | .priv_data_size = sizeof(CLVContext), |
746 | | .init = clv_decode_init, |
747 | | .close = clv_decode_end, |
748 | | FF_CODEC_DECODE_CB(clv_decode_frame), |
749 | | .p.capabilities = AV_CODEC_CAP_DR1, |
750 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
751 | | }; |