Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Duck/ON2 TrueMotion 2 Decoder | ||
3 | * Copyright (c) 2005 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 | * Duck TrueMotion2 decoder. | ||
25 | */ | ||
26 | |||
27 | #include <inttypes.h> | ||
28 | |||
29 | #include "libavutil/mem.h" | ||
30 | #include "avcodec.h" | ||
31 | #include "bswapdsp.h" | ||
32 | #include "bytestream.h" | ||
33 | #include "codec_internal.h" | ||
34 | #include "decode.h" | ||
35 | #include "get_bits.h" | ||
36 | |||
37 | #define TM2_ESCAPE 0x80000000 | ||
38 | #define TM2_DELTAS 64 | ||
39 | |||
40 | /* Huffman-coded streams of different types of blocks */ | ||
41 | enum TM2_STREAMS { | ||
42 | TM2_C_HI = 0, | ||
43 | TM2_C_LO, | ||
44 | TM2_L_HI, | ||
45 | TM2_L_LO, | ||
46 | TM2_UPD, | ||
47 | TM2_MOT, | ||
48 | TM2_TYPE, | ||
49 | TM2_NUM_STREAMS | ||
50 | }; | ||
51 | |||
52 | /* Block types */ | ||
53 | enum TM2_BLOCKS { | ||
54 | TM2_HI_RES = 0, | ||
55 | TM2_MED_RES, | ||
56 | TM2_LOW_RES, | ||
57 | TM2_NULL_RES, | ||
58 | TM2_UPDATE, | ||
59 | TM2_STILL, | ||
60 | TM2_MOTION | ||
61 | }; | ||
62 | |||
63 | typedef struct TM2Context { | ||
64 | AVCodecContext *avctx; | ||
65 | AVFrame *pic; | ||
66 | |||
67 | GetBitContext gb; | ||
68 | int error; | ||
69 | BswapDSPContext bdsp; | ||
70 | |||
71 | uint8_t *buffer; | ||
72 | int buffer_size; | ||
73 | |||
74 | /* TM2 streams */ | ||
75 | int *tokens[TM2_NUM_STREAMS]; | ||
76 | int tok_lens[TM2_NUM_STREAMS]; | ||
77 | int tok_ptrs[TM2_NUM_STREAMS]; | ||
78 | int deltas[TM2_NUM_STREAMS][TM2_DELTAS]; | ||
79 | /* for blocks decoding */ | ||
80 | int D[4]; | ||
81 | int CD[4]; | ||
82 | int *last; | ||
83 | int *clast; | ||
84 | |||
85 | /* data for current and previous frame */ | ||
86 | int *Y_base, *UV_base; | ||
87 | int *Y1, *U1, *V1, *Y2, *U2, *V2; | ||
88 | int y_stride, uv_stride; | ||
89 | int cur; | ||
90 | } TM2Context; | ||
91 | |||
92 | /** | ||
93 | * Huffman codes for each of streams | ||
94 | */ | ||
95 | typedef struct TM2Codes { | ||
96 | VLC vlc; ///< table for FFmpeg bitstream reader | ||
97 | int bits; | ||
98 | int *recode; ///< table for converting from code indexes to values | ||
99 | int length; | ||
100 | } TM2Codes; | ||
101 | |||
102 | /** | ||
103 | * structure for gathering Huffman codes information | ||
104 | */ | ||
105 | typedef struct TM2Huff { | ||
106 | int val_bits; ///< length of literal | ||
107 | int max_bits; ///< maximum length of code | ||
108 | int min_bits; ///< minimum length of code | ||
109 | int nodes; ///< total number of nodes in tree | ||
110 | int num; ///< current number filled | ||
111 | int max_num; ///< total number of codes | ||
112 | int *nums; ///< literals | ||
113 | uint8_t *lens; ///< codelengths | ||
114 | } TM2Huff; | ||
115 | |||
116 | /** | ||
117 | * | ||
118 | * @returns the length of the longest code or an AVERROR code | ||
119 | */ | ||
120 | 3148 | static int tm2_read_tree(TM2Context *ctx, int length, TM2Huff *huff) | |
121 | { | ||
122 | int ret, ret2; | ||
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3148 times.
|
3148 | if (length > huff->max_bits) { |
124 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", | |
125 | huff->max_bits); | ||
126 | ✗ | return AVERROR_INVALIDDATA; | |
127 | } | ||
128 | |||
129 |
2/2✓ Branch 1 taken 1670 times.
✓ Branch 2 taken 1478 times.
|
3148 | if (!get_bits1(&ctx->gb)) { /* literal */ |
130 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1667 times.
|
1670 | if (length == 0) { |
131 | 3 | length = 1; | |
132 | } | ||
133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1670 times.
|
1670 | if (huff->num >= huff->max_num) { |
134 | ✗ | av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n"); | |
135 | ✗ | return AVERROR_INVALIDDATA; | |
136 | } | ||
137 | 1670 | huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits); | |
138 | 1670 | huff->lens[huff->num] = length; | |
139 | 1670 | huff->num++; | |
140 | 1670 | return length; | |
141 | } else { /* non-terminal node */ | ||
142 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1478 times.
|
1478 | if ((ret2 = tm2_read_tree(ctx, length + 1, huff)) < 0) |
143 | ✗ | return ret2; | |
144 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1478 times.
|
1478 | if ((ret = tm2_read_tree(ctx, length + 1, huff)) < 0) |
145 | ✗ | return ret; | |
146 | } | ||
147 | 1478 | return FFMAX(ret, ret2); | |
148 | } | ||
149 | |||
150 | 192 | static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) | |
151 | { | ||
152 | TM2Huff huff; | ||
153 | 192 | int res = 0; | |
154 | |||
155 | 192 | huff.val_bits = get_bits(&ctx->gb, 5); | |
156 | 192 | huff.max_bits = get_bits(&ctx->gb, 5); | |
157 | 192 | huff.min_bits = get_bits(&ctx->gb, 5); | |
158 | 192 | huff.nodes = get_bits(&ctx->gb, 17); | |
159 | 192 | huff.num = 0; | |
160 | |||
161 | /* check for correct codes parameters */ | ||
162 |
2/4✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 192 times.
✗ Branch 3 not taken.
|
192 | if ((huff.val_bits < 1) || (huff.val_bits > 32) || |
163 |
2/4✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
|
192 | (huff.max_bits < 0) || (huff.max_bits > 25)) { |
164 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal " | |
165 | "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits); | ||
166 | ✗ | return AVERROR_INVALIDDATA; | |
167 | } | ||
168 |
2/4✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
|
192 | if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) { |
169 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree " | |
170 | "nodes: %i\n", huff.nodes); | ||
171 | ✗ | return AVERROR_INVALIDDATA; | |
172 | } | ||
173 | /* one-node tree */ | ||
174 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 189 times.
|
192 | if (huff.max_bits == 0) |
175 | 3 | huff.max_bits = 1; | |
176 | |||
177 | /* allocate space for codes - it is exactly ceil(nodes / 2) entries */ | ||
178 | 192 | huff.max_num = (huff.nodes + 1) >> 1; | |
179 | 192 | huff.nums = av_calloc(huff.max_num, sizeof(int)); | |
180 | 192 | huff.lens = av_mallocz(huff.max_num); | |
181 | |||
182 |
2/4✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
|
192 | if (!huff.nums || !huff.lens) { |
183 | ✗ | res = AVERROR(ENOMEM); | |
184 | ✗ | goto out; | |
185 | } | ||
186 | |||
187 | 192 | res = tm2_read_tree(ctx, 0, &huff); | |
188 | |||
189 |
2/4✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
|
192 | if (res >= 0 && res != huff.max_bits) { |
190 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Got less bits than expected: %i of %i\n", | |
191 | res, huff.max_bits); | ||
192 | ✗ | res = AVERROR_INVALIDDATA; | |
193 | } | ||
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (huff.num != huff.max_num) { |
195 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n", | |
196 | huff.num, huff.max_num); | ||
197 | ✗ | res = AVERROR_INVALIDDATA; | |
198 | } | ||
199 | |||
200 | /* convert codes to vlc_table */ | ||
201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (res >= 0) { |
202 | 192 | res = ff_vlc_init_from_lengths(&code->vlc, huff.max_bits, huff.max_num, | |
203 | 192 | huff.lens, sizeof(huff.lens[0]), | |
204 | 192 | NULL, 0, 0, 0, 0, ctx->avctx); | |
205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (res < 0) |
206 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); | |
207 | else { | ||
208 | 192 | code->bits = huff.max_bits; | |
209 | 192 | code->length = huff.max_num; | |
210 | 192 | code->recode = huff.nums; | |
211 | 192 | huff.nums = NULL; | |
212 | } | ||
213 | } | ||
214 | |||
215 | ✗ | out: | |
216 | /* free allocated memory */ | ||
217 | 192 | av_free(huff.nums); | |
218 | 192 | av_free(huff.lens); | |
219 | |||
220 | 192 | return res; | |
221 | } | ||
222 | |||
223 | 192 | static void tm2_free_codes(TM2Codes *code) | |
224 | { | ||
225 | 192 | av_free(code->recode); | |
226 | 192 | ff_vlc_free(&code->vlc); | |
227 | 192 | } | |
228 | |||
229 | 2353240 | static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code) | |
230 | { | ||
231 | int val; | ||
232 | 2353240 | val = get_vlc2(gb, code->vlc.table, code->bits, 1); | |
233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2353240 times.
|
2353240 | if(val<0) |
234 | ✗ | return -1; | |
235 | 2353240 | return code->recode[val]; | |
236 | } | ||
237 | |||
238 | #define TM2_OLD_HEADER_MAGIC 0x00000100 | ||
239 | #define TM2_NEW_HEADER_MAGIC 0x00000101 | ||
240 | |||
241 | 30 | static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf) | |
242 | { | ||
243 | 30 | uint32_t magic = AV_RL32(buf); | |
244 | |||
245 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | switch (magic) { |
246 | ✗ | case TM2_OLD_HEADER_MAGIC: | |
247 | ✗ | avpriv_request_sample(ctx->avctx, "Old TM2 header"); | |
248 | ✗ | return 0; | |
249 | 30 | case TM2_NEW_HEADER_MAGIC: | |
250 | 30 | return 0; | |
251 | ✗ | default: | |
252 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n", | |
253 | magic); | ||
254 | ✗ | return AVERROR_INVALIDDATA; | |
255 | } | ||
256 | } | ||
257 | |||
258 | 29 | static int tm2_read_deltas(TM2Context *ctx, int stream_id) | |
259 | { | ||
260 | int d, mb; | ||
261 | int i, v; | ||
262 | |||
263 | 29 | d = get_bits(&ctx->gb, 9); | |
264 | 29 | mb = get_bits(&ctx->gb, 5); | |
265 | |||
266 | av_assert2(mb < 32); | ||
267 |
3/6✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
|
29 | if ((d < 1) || (d > TM2_DELTAS) || (mb < 1)) { |
268 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb); | |
269 | ✗ | return AVERROR_INVALIDDATA; | |
270 | } | ||
271 | |||
272 |
2/2✓ Branch 0 taken 565 times.
✓ Branch 1 taken 29 times.
|
594 | for (i = 0; i < d; i++) { |
273 | 565 | v = get_bits_long(&ctx->gb, mb); | |
274 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 295 times.
|
565 | if (v & (1 << (mb - 1))) |
275 | 270 | ctx->deltas[stream_id][i] = v - (1U << mb); | |
276 | else | ||
277 | 295 | ctx->deltas[stream_id][i] = v; | |
278 | } | ||
279 |
2/2✓ Branch 0 taken 1291 times.
✓ Branch 1 taken 29 times.
|
1320 | for (; i < TM2_DELTAS; i++) |
280 | 1291 | ctx->deltas[stream_id][i] = 0; | |
281 | |||
282 | 29 | return 0; | |
283 | } | ||
284 | |||
285 | 210 | static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size) | |
286 | { | ||
287 | int i, ret; | ||
288 | 210 | int skip = 0; | |
289 | int len, toks, pos; | ||
290 | TM2Codes codes; | ||
291 | GetByteContext gb; | ||
292 | |||
293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if (buf_size < 4) { |
294 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n"); | |
295 | ✗ | return AVERROR_INVALIDDATA; | |
296 | } | ||
297 | |||
298 | /* get stream length in dwords */ | ||
299 | 210 | bytestream2_init(&gb, buf, buf_size); | |
300 | 210 | len = bytestream2_get_be32(&gb); | |
301 | |||
302 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 192 times.
|
210 | if (len == 0) |
303 | 18 | return 4; | |
304 | |||
305 |
3/6✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 192 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 192 times.
|
192 | if (len >= INT_MAX / 4 - 1 || len < 0 || len * 4 + 4 > buf_size) { |
306 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n"); | |
307 | ✗ | return AVERROR_INVALIDDATA; | |
308 | } | ||
309 | 192 | skip = len * 4 + 4; | |
310 | |||
311 | 192 | toks = bytestream2_get_be32(&gb); | |
312 |
2/2✓ Branch 0 taken 162 times.
✓ Branch 1 taken 30 times.
|
192 | if (toks & 1) { |
313 | 162 | len = bytestream2_get_be32(&gb); | |
314 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 157 times.
|
162 | if (len == TM2_ESCAPE) { |
315 | 5 | len = bytestream2_get_be32(&gb); | |
316 | } | ||
317 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 133 times.
|
162 | if (len > 0) { |
318 | 29 | pos = bytestream2_tell(&gb); | |
319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (skip <= pos) |
320 | ✗ | return AVERROR_INVALIDDATA; | |
321 | 29 | init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); | |
322 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
|
29 | if ((ret = tm2_read_deltas(ctx, stream_id)) < 0) |
323 | ✗ | return ret; | |
324 | 29 | bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); | |
325 | } | ||
326 | } | ||
327 | /* skip unused fields */ | ||
328 | 192 | len = bytestream2_get_be32(&gb); | |
329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */ |
330 | ✗ | bytestream2_skip(&gb, 8); /* unused by decoder */ | |
331 | } else { | ||
332 | 192 | bytestream2_skip(&gb, 4); /* unused by decoder */ | |
333 | } | ||
334 | |||
335 | 192 | pos = bytestream2_tell(&gb); | |
336 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (skip <= pos) |
337 | ✗ | return AVERROR_INVALIDDATA; | |
338 | 192 | init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); | |
339 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 192 times.
|
192 | if ((ret = tm2_build_huff_table(ctx, &codes)) < 0) |
340 | ✗ | return ret; | |
341 | 192 | bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); | |
342 | |||
343 | 192 | toks >>= 1; | |
344 | /* check if we have sane number of tokens */ | ||
345 |
2/4✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
|
192 | if ((toks < 0) || (toks > 0xFFFFFF)) { |
346 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); | |
347 | ✗ | ret = AVERROR_INVALIDDATA; | |
348 | ✗ | goto end; | |
349 | } | ||
350 | 192 | ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int)); | |
351 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (ret < 0) { |
352 | ✗ | ctx->tok_lens[stream_id] = 0; | |
353 | ✗ | goto end; | |
354 | } | ||
355 | 192 | ctx->tok_lens[stream_id] = toks; | |
356 | 192 | len = bytestream2_get_be32(&gb); | |
357 |
2/2✓ Branch 0 taken 189 times.
✓ Branch 1 taken 3 times.
|
192 | if (len > 0) { |
358 | 189 | pos = bytestream2_tell(&gb); | |
359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 189 times.
|
189 | if (skip <= pos) { |
360 | ✗ | ret = AVERROR_INVALIDDATA; | |
361 | ✗ | goto end; | |
362 | } | ||
363 | 189 | init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); | |
364 |
2/2✓ Branch 0 taken 2353240 times.
✓ Branch 1 taken 189 times.
|
2353429 | for (i = 0; i < toks; i++) { |
365 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2353240 times.
|
2353240 | if (get_bits_left(&ctx->gb) <= 0) { |
366 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); | |
367 | ✗ | ret = AVERROR_INVALIDDATA; | |
368 | ✗ | goto end; | |
369 | } | ||
370 | 2353240 | ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes); | |
371 |
4/6✓ Branch 0 taken 2209240 times.
✓ Branch 1 taken 144000 times.
✓ Branch 2 taken 2209240 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2353240 times.
|
2353240 | if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) { |
372 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n", | |
373 | ✗ | ctx->tokens[stream_id][i], stream_id, i); | |
374 | ✗ | ret = AVERROR_INVALIDDATA; | |
375 | ✗ | goto end; | |
376 | } | ||
377 | } | ||
378 | } else { | ||
379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (len < 0) { |
380 | ✗ | ret = AVERROR_INVALIDDATA; | |
381 | ✗ | goto end; | |
382 | } | ||
383 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
|
21 | for (i = 0; i < toks; i++) { |
384 | 18 | ctx->tokens[stream_id][i] = codes.recode[0]; | |
385 |
2/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
18 | if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) { |
386 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n", | |
387 | ✗ | ctx->tokens[stream_id][i], stream_id, i); | |
388 | ✗ | ret = AVERROR_INVALIDDATA; | |
389 | ✗ | goto end; | |
390 | } | ||
391 | } | ||
392 | } | ||
393 | |||
394 | 192 | ret = skip; | |
395 | |||
396 | 192 | end: | |
397 | 192 | tm2_free_codes(&codes); | |
398 | 192 | return ret; | |
399 | } | ||
400 | |||
401 | 2353258 | static inline int GET_TOK(TM2Context *ctx,int type) | |
402 | { | ||
403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2353258 times.
|
2353258 | if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) { |
404 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Read token from stream %i out of bounds (%i>=%i)\n", type, ctx->tok_ptrs[type], ctx->tok_lens[type]); | |
405 | ✗ | ctx->error = 1; | |
406 | ✗ | return 0; | |
407 | } | ||
408 |
2/2✓ Branch 0 taken 2209258 times.
✓ Branch 1 taken 144000 times.
|
2353258 | if (type <= TM2_MOT) { |
409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2209258 times.
|
2209258 | if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) { |
410 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]); | |
411 | ✗ | return 0; | |
412 | } | ||
413 | 2209258 | return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]]; | |
414 | } | ||
415 | 144000 | return ctx->tokens[type][ctx->tok_ptrs[type]++]; | |
416 | } | ||
417 | |||
418 | /* blocks decoding routines */ | ||
419 | |||
420 | /* common Y, U, V pointers initialisation */ | ||
421 | #define TM2_INIT_POINTERS() \ | ||
422 | int *last, *clast; \ | ||
423 | int *Y, *U, *V;\ | ||
424 | int Ystride, Ustride, Vstride;\ | ||
425 | \ | ||
426 | Ystride = ctx->y_stride;\ | ||
427 | Vstride = ctx->uv_stride;\ | ||
428 | Ustride = ctx->uv_stride;\ | ||
429 | Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\ | ||
430 | V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\ | ||
431 | U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\ | ||
432 | last = ctx->last + bx * 4;\ | ||
433 | clast = ctx->clast + bx * 4; | ||
434 | |||
435 | #define TM2_INIT_POINTERS_2() \ | ||
436 | unsigned *Yo, *Uo, *Vo;\ | ||
437 | int oYstride, oUstride, oVstride;\ | ||
438 | \ | ||
439 | TM2_INIT_POINTERS();\ | ||
440 | oYstride = Ystride;\ | ||
441 | oVstride = Vstride;\ | ||
442 | oUstride = Ustride;\ | ||
443 | Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\ | ||
444 | Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\ | ||
445 | Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2; | ||
446 | |||
447 | /* recalculate last and delta values for next blocks */ | ||
448 | #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\ | ||
449 | CD[0] = (unsigned)CHR[ 1] - (unsigned)last[1];\ | ||
450 | CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\ | ||
451 | last[0] = (int)CHR[stride + 0];\ | ||
452 | last[1] = (int)CHR[stride + 1];} | ||
453 | |||
454 | /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */ | ||
455 | 116660 | static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last) | |
456 | { | ||
457 | unsigned ct, d; | ||
458 | int i, j; | ||
459 | |||
460 |
2/2✓ Branch 0 taken 466640 times.
✓ Branch 1 taken 116660 times.
|
583300 | for (j = 0; j < 4; j++){ |
461 | 466640 | ct = ctx->D[j]; | |
462 |
2/2✓ Branch 0 taken 1866560 times.
✓ Branch 1 taken 466640 times.
|
2333200 | for (i = 0; i < 4; i++){ |
463 | 1866560 | d = deltas[i + j * 4]; | |
464 | 1866560 | ct += d; | |
465 | 1866560 | last[i] += ct; | |
466 | 1866560 | Y[i] = av_clip_uint8(last[i]); | |
467 | } | ||
468 | 466640 | Y += stride; | |
469 | 466640 | ctx->D[j] = ct; | |
470 | } | ||
471 | 116660 | } | |
472 | |||
473 | 233320 | static inline void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas) | |
474 | { | ||
475 | int i, j; | ||
476 |
2/2✓ Branch 0 taken 466640 times.
✓ Branch 1 taken 233320 times.
|
699960 | for (j = 0; j < 2; j++) { |
477 |
2/2✓ Branch 0 taken 933280 times.
✓ Branch 1 taken 466640 times.
|
1399920 | for (i = 0; i < 2; i++) { |
478 | 933280 | CD[j] += deltas[i + j * 2]; | |
479 | 933280 | last[i] += CD[j]; | |
480 | 933280 | data[i] = last[i]; | |
481 | } | ||
482 | 466640 | data += stride; | |
483 | } | ||
484 | 233320 | } | |
485 | |||
486 | 99888 | static inline void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx) | |
487 | { | ||
488 | int t; | ||
489 | int l; | ||
490 | int prev; | ||
491 | |||
492 |
2/2✓ Branch 0 taken 99060 times.
✓ Branch 1 taken 828 times.
|
99888 | if (bx > 0) |
493 | 99060 | prev = clast[-3]; | |
494 | else | ||
495 | 828 | prev = 0; | |
496 | 99888 | t = (int)(CD[0] + CD[1]) >> 1; | |
497 | 99888 | l = (int)(prev - CD[0] - CD[1] + clast[1]) >> 1; | |
498 | 99888 | CD[1] = CD[0] + CD[1] - t; | |
499 | 99888 | CD[0] = t; | |
500 | 99888 | clast[0] = l; | |
501 | |||
502 | 99888 | tm2_high_chroma(data, stride, clast, CD, deltas); | |
503 | 99888 | } | |
504 | |||
505 | 66716 | static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
506 | { | ||
507 | int i; | ||
508 | int deltas[16]; | ||
509 |
6/6✓ Branch 0 taken 31738 times.
✓ Branch 1 taken 34978 times.
✓ Branch 2 taken 31738 times.
✓ Branch 3 taken 34978 times.
✓ Branch 4 taken 31738 times.
✓ Branch 5 taken 34978 times.
|
66716 | TM2_INIT_POINTERS(); |
510 | |||
511 | /* hi-res chroma */ | ||
512 |
2/2✓ Branch 0 taken 266864 times.
✓ Branch 1 taken 66716 times.
|
333580 | for (i = 0; i < 4; i++) { |
513 | 266864 | deltas[i] = GET_TOK(ctx, TM2_C_HI); | |
514 | 266864 | deltas[i + 4] = GET_TOK(ctx, TM2_C_HI); | |
515 | } | ||
516 | 66716 | tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas); | |
517 | 66716 | tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4); | |
518 | |||
519 | /* hi-res luma */ | ||
520 |
2/2✓ Branch 0 taken 1067456 times.
✓ Branch 1 taken 66716 times.
|
1134172 | for (i = 0; i < 16; i++) |
521 | 1067456 | deltas[i] = GET_TOK(ctx, TM2_L_HI); | |
522 | |||
523 | 66716 | tm2_apply_deltas(ctx, Y, Ystride, deltas, last); | |
524 | 66716 | } | |
525 | |||
526 | 22463 | static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
527 | { | ||
528 | int i; | ||
529 | int deltas[16]; | ||
530 |
6/6✓ Branch 0 taken 10594 times.
✓ Branch 1 taken 11869 times.
✓ Branch 2 taken 10594 times.
✓ Branch 3 taken 11869 times.
✓ Branch 4 taken 10594 times.
✓ Branch 5 taken 11869 times.
|
22463 | TM2_INIT_POINTERS(); |
531 | |||
532 | /* low-res chroma */ | ||
533 | 22463 | deltas[0] = GET_TOK(ctx, TM2_C_LO); | |
534 | 22463 | deltas[1] = deltas[2] = deltas[3] = 0; | |
535 | 22463 | tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx); | |
536 | |||
537 | 22463 | deltas[0] = GET_TOK(ctx, TM2_C_LO); | |
538 | 22463 | deltas[1] = deltas[2] = deltas[3] = 0; | |
539 | 22463 | tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx); | |
540 | |||
541 | /* hi-res luma */ | ||
542 |
2/2✓ Branch 0 taken 359408 times.
✓ Branch 1 taken 22463 times.
|
381871 | for (i = 0; i < 16; i++) |
543 | 359408 | deltas[i] = GET_TOK(ctx, TM2_L_HI); | |
544 | |||
545 | 22463 | tm2_apply_deltas(ctx, Y, Ystride, deltas, last); | |
546 | 22463 | } | |
547 | |||
548 | 12092 | static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
549 | { | ||
550 | int i; | ||
551 | int t1, t2; | ||
552 | int deltas[16]; | ||
553 |
6/6✓ Branch 0 taken 5975 times.
✓ Branch 1 taken 6117 times.
✓ Branch 2 taken 5975 times.
✓ Branch 3 taken 6117 times.
✓ Branch 4 taken 5975 times.
✓ Branch 5 taken 6117 times.
|
12092 | TM2_INIT_POINTERS(); |
554 | |||
555 | /* low-res chroma */ | ||
556 | 12092 | deltas[0] = GET_TOK(ctx, TM2_C_LO); | |
557 | 12092 | deltas[1] = deltas[2] = deltas[3] = 0; | |
558 | 12092 | tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx); | |
559 | |||
560 | 12092 | deltas[0] = GET_TOK(ctx, TM2_C_LO); | |
561 | 12092 | deltas[1] = deltas[2] = deltas[3] = 0; | |
562 | 12092 | tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx); | |
563 | |||
564 | /* low-res luma */ | ||
565 |
2/2✓ Branch 0 taken 193472 times.
✓ Branch 1 taken 12092 times.
|
205564 | for (i = 0; i < 16; i++) |
566 | 193472 | deltas[i] = 0; | |
567 | |||
568 | 12092 | deltas[ 0] = GET_TOK(ctx, TM2_L_LO); | |
569 | 12092 | deltas[ 2] = GET_TOK(ctx, TM2_L_LO); | |
570 | 12092 | deltas[ 8] = GET_TOK(ctx, TM2_L_LO); | |
571 | 12092 | deltas[10] = GET_TOK(ctx, TM2_L_LO); | |
572 | |||
573 |
2/2✓ Branch 0 taken 12025 times.
✓ Branch 1 taken 67 times.
|
12092 | if (bx > 0) |
574 | 12025 | last[0] = (int)((unsigned)last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1; | |
575 | else | ||
576 | 67 | last[0] = (int)((unsigned)last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1; | |
577 | 12092 | last[2] = (int)((unsigned)last[1] + last[3]) >> 1; | |
578 | |||
579 | 12092 | t1 = ctx->D[0] + (unsigned)ctx->D[1]; | |
580 | 12092 | ctx->D[0] = t1 >> 1; | |
581 | 12092 | ctx->D[1] = t1 - (t1 >> 1); | |
582 | 12092 | t2 = ctx->D[2] + (unsigned)ctx->D[3]; | |
583 | 12092 | ctx->D[2] = t2 >> 1; | |
584 | 12092 | ctx->D[3] = t2 - (t2 >> 1); | |
585 | |||
586 | 12092 | tm2_apply_deltas(ctx, Y, Ystride, deltas, last); | |
587 | 12092 | } | |
588 | |||
589 | 15389 | static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
590 | { | ||
591 | int i; | ||
592 | int ct; | ||
593 | unsigned left, right; | ||
594 | int diff; | ||
595 | int deltas[16]; | ||
596 |
6/6✓ Branch 0 taken 7171 times.
✓ Branch 1 taken 8218 times.
✓ Branch 2 taken 7171 times.
✓ Branch 3 taken 8218 times.
✓ Branch 4 taken 7171 times.
✓ Branch 5 taken 8218 times.
|
15389 | TM2_INIT_POINTERS(); |
597 | |||
598 | /* null chroma */ | ||
599 | 15389 | deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0; | |
600 | 15389 | tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx); | |
601 | |||
602 | 15389 | deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0; | |
603 | 15389 | tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx); | |
604 | |||
605 | /* null luma */ | ||
606 |
2/2✓ Branch 0 taken 246224 times.
✓ Branch 1 taken 15389 times.
|
261613 | for (i = 0; i < 16; i++) |
607 | 246224 | deltas[i] = 0; | |
608 | |||
609 | 15389 | ct = (unsigned)ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3]; | |
610 | |||
611 |
2/2✓ Branch 0 taken 15315 times.
✓ Branch 1 taken 74 times.
|
15389 | if (bx > 0) |
612 | 15315 | left = last[-1] - (unsigned)ct; | |
613 | else | ||
614 | 74 | left = 0; | |
615 | |||
616 | 15389 | right = last[3]; | |
617 | 15389 | diff = right - left; | |
618 | 15389 | last[0] = left + (diff >> 2); | |
619 | 15389 | last[1] = left + (diff >> 1); | |
620 | 15389 | last[2] = right - (diff >> 2); | |
621 | 15389 | last[3] = right; | |
622 | { | ||
623 | 15389 | unsigned tp = left; | |
624 | |||
625 | 15389 | ctx->D[0] = (tp + (ct >> 2)) - left; | |
626 | 15389 | left += ctx->D[0]; | |
627 | 15389 | ctx->D[1] = (tp + (ct >> 1)) - left; | |
628 | 15389 | left += ctx->D[1]; | |
629 | 15389 | ctx->D[2] = ((tp + ct) - (ct >> 2)) - left; | |
630 | 15389 | left += ctx->D[2]; | |
631 | 15389 | ctx->D[3] = (tp + ct) - left; | |
632 | } | ||
633 | 15389 | tm2_apply_deltas(ctx, Y, Ystride, deltas, last); | |
634 | 15389 | } | |
635 | |||
636 | 20959 | static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
637 | { | ||
638 | int i, j; | ||
639 |
12/12✓ Branch 0 taken 12586 times.
✓ Branch 1 taken 8373 times.
✓ Branch 2 taken 12586 times.
✓ Branch 3 taken 8373 times.
✓ Branch 4 taken 12586 times.
✓ Branch 5 taken 8373 times.
✓ Branch 6 taken 12586 times.
✓ Branch 7 taken 8373 times.
✓ Branch 8 taken 12586 times.
✓ Branch 9 taken 8373 times.
✓ Branch 10 taken 12586 times.
✓ Branch 11 taken 8373 times.
|
20959 | TM2_INIT_POINTERS_2(); |
640 | |||
641 | /* update chroma */ | ||
642 |
2/2✓ Branch 0 taken 41918 times.
✓ Branch 1 taken 20959 times.
|
62877 | for (j = 0; j < 2; j++) { |
643 |
2/2✓ Branch 0 taken 83836 times.
✓ Branch 1 taken 41918 times.
|
125754 | for (i = 0; i < 2; i++){ |
644 | 83836 | U[i] = Uo[i]; | |
645 | 83836 | V[i] = Vo[i]; | |
646 | } | ||
647 | 41918 | U += Ustride; V += Vstride; | |
648 | 41918 | Uo += oUstride; Vo += oVstride; | |
649 | } | ||
650 | 20959 | U -= Ustride * 2; | |
651 | 20959 | V -= Vstride * 2; | |
652 | 20959 | TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD); | |
653 | 20959 | TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2)); | |
654 | |||
655 | /* update deltas */ | ||
656 | 20959 | ctx->D[0] = Yo[3] - last[3]; | |
657 | 20959 | ctx->D[1] = Yo[3 + oYstride] - Yo[3]; | |
658 | 20959 | ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride]; | |
659 | 20959 | ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2]; | |
660 | |||
661 |
2/2✓ Branch 0 taken 83836 times.
✓ Branch 1 taken 20959 times.
|
104795 | for (j = 0; j < 4; j++) { |
662 |
2/2✓ Branch 0 taken 335344 times.
✓ Branch 1 taken 83836 times.
|
419180 | for (i = 0; i < 4; i++) { |
663 | 335344 | Y[i] = Yo[i]; | |
664 | 335344 | last[i] = Yo[i]; | |
665 | } | ||
666 | 83836 | Y += Ystride; | |
667 | 83836 | Yo += oYstride; | |
668 | } | ||
669 | 20959 | } | |
670 | |||
671 | 5383 | static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
672 | { | ||
673 | int i, j; | ||
674 | unsigned d; | ||
675 |
12/12✓ Branch 0 taken 3308 times.
✓ Branch 1 taken 2075 times.
✓ Branch 2 taken 3308 times.
✓ Branch 3 taken 2075 times.
✓ Branch 4 taken 3308 times.
✓ Branch 5 taken 2075 times.
✓ Branch 6 taken 3308 times.
✓ Branch 7 taken 2075 times.
✓ Branch 8 taken 3308 times.
✓ Branch 9 taken 2075 times.
✓ Branch 10 taken 3308 times.
✓ Branch 11 taken 2075 times.
|
5383 | TM2_INIT_POINTERS_2(); |
676 | |||
677 | /* update chroma */ | ||
678 |
2/2✓ Branch 0 taken 10766 times.
✓ Branch 1 taken 5383 times.
|
16149 | for (j = 0; j < 2; j++) { |
679 |
2/2✓ Branch 0 taken 21532 times.
✓ Branch 1 taken 10766 times.
|
32298 | for (i = 0; i < 2; i++) { |
680 | 21532 | U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD); | |
681 | 21532 | V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD); | |
682 | } | ||
683 | 10766 | U += Ustride; | |
684 | 10766 | V += Vstride; | |
685 | 10766 | Uo += oUstride; | |
686 | 10766 | Vo += oVstride; | |
687 | } | ||
688 | 5383 | U -= Ustride * 2; | |
689 | 5383 | V -= Vstride * 2; | |
690 | 5383 | TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD); | |
691 | 5383 | TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2)); | |
692 | |||
693 | /* update deltas */ | ||
694 | 5383 | ctx->D[0] = Yo[3] - last[3]; | |
695 | 5383 | ctx->D[1] = Yo[3 + oYstride] - Yo[3]; | |
696 | 5383 | ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride]; | |
697 | 5383 | ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2]; | |
698 | |||
699 |
2/2✓ Branch 0 taken 21532 times.
✓ Branch 1 taken 5383 times.
|
26915 | for (j = 0; j < 4; j++) { |
700 | 21532 | d = last[3]; | |
701 |
2/2✓ Branch 0 taken 86128 times.
✓ Branch 1 taken 21532 times.
|
107660 | for (i = 0; i < 4; i++) { |
702 | 86128 | Y[i] = Yo[i] + (unsigned)GET_TOK(ctx, TM2_UPD); | |
703 | 86128 | last[i] = Y[i]; | |
704 | } | ||
705 | 21532 | ctx->D[j] = last[3] - d; | |
706 | 21532 | Y += Ystride; | |
707 | 21532 | Yo += oYstride; | |
708 | } | ||
709 | 5383 | } | |
710 | |||
711 | 998 | static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by) | |
712 | { | ||
713 | int i, j; | ||
714 | int mx, my; | ||
715 |
12/12✓ Branch 0 taken 628 times.
✓ Branch 1 taken 370 times.
✓ Branch 2 taken 628 times.
✓ Branch 3 taken 370 times.
✓ Branch 4 taken 628 times.
✓ Branch 5 taken 370 times.
✓ Branch 6 taken 628 times.
✓ Branch 7 taken 370 times.
✓ Branch 8 taken 628 times.
✓ Branch 9 taken 370 times.
✓ Branch 10 taken 628 times.
✓ Branch 11 taken 370 times.
|
998 | TM2_INIT_POINTERS_2(); |
716 | |||
717 | 998 | mx = GET_TOK(ctx, TM2_MOT); | |
718 | 998 | my = GET_TOK(ctx, TM2_MOT); | |
719 | 998 | mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4); | |
720 | 998 | my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4); | |
721 | |||
722 |
4/8✓ Branch 0 taken 998 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 998 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 998 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 998 times.
|
998 | if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) { |
723 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n"); | |
724 | ✗ | return; | |
725 | } | ||
726 | |||
727 | 998 | Yo += my * oYstride + mx; | |
728 | 998 | Uo += (my >> 1) * oUstride + (mx >> 1); | |
729 | 998 | Vo += (my >> 1) * oVstride + (mx >> 1); | |
730 | |||
731 | /* copy chroma */ | ||
732 |
2/2✓ Branch 0 taken 1996 times.
✓ Branch 1 taken 998 times.
|
2994 | for (j = 0; j < 2; j++) { |
733 |
2/2✓ Branch 0 taken 3992 times.
✓ Branch 1 taken 1996 times.
|
5988 | for (i = 0; i < 2; i++) { |
734 | 3992 | U[i] = Uo[i]; | |
735 | 3992 | V[i] = Vo[i]; | |
736 | } | ||
737 | 1996 | U += Ustride; | |
738 | 1996 | V += Vstride; | |
739 | 1996 | Uo += oUstride; | |
740 | 1996 | Vo += oVstride; | |
741 | } | ||
742 | 998 | U -= Ustride * 2; | |
743 | 998 | V -= Vstride * 2; | |
744 | 998 | TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD); | |
745 | 998 | TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2)); | |
746 | |||
747 | /* copy luma */ | ||
748 |
2/2✓ Branch 0 taken 3992 times.
✓ Branch 1 taken 998 times.
|
4990 | for (j = 0; j < 4; j++) { |
749 |
2/2✓ Branch 0 taken 15968 times.
✓ Branch 1 taken 3992 times.
|
19960 | for (i = 0; i < 4; i++) { |
750 | 15968 | Y[i] = Yo[i]; | |
751 | } | ||
752 | 3992 | Y += Ystride; | |
753 | 3992 | Yo += oYstride; | |
754 | } | ||
755 | /* calculate deltas */ | ||
756 | 998 | Y -= Ystride * 4; | |
757 | 998 | ctx->D[0] = (unsigned)Y[3] - last[3]; | |
758 | 998 | ctx->D[1] = (unsigned)Y[3 + Ystride] - Y[3]; | |
759 | 998 | ctx->D[2] = (unsigned)Y[3 + Ystride * 2] - Y[3 + Ystride]; | |
760 | 998 | ctx->D[3] = (unsigned)Y[3 + Ystride * 3] - Y[3 + Ystride * 2]; | |
761 |
2/2✓ Branch 0 taken 3992 times.
✓ Branch 1 taken 998 times.
|
4990 | for (i = 0; i < 4; i++) |
762 | 3992 | last[i] = Y[i + Ystride * 3]; | |
763 | } | ||
764 | |||
765 | 30 | static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p) | |
766 | { | ||
767 | int i, j; | ||
768 | 30 | int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1; | |
769 | int type; | ||
770 | 30 | int keyframe = 1; | |
771 | int *Y, *U, *V; | ||
772 | uint8_t *dst; | ||
773 | |||
774 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 30 times.
|
240 | for (i = 0; i < TM2_NUM_STREAMS; i++) |
775 | 210 | ctx->tok_ptrs[i] = 0; | |
776 | |||
777 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ctx->tok_lens[TM2_TYPE]<bw*bh) { |
778 | ✗ | av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh); | |
779 | ✗ | return AVERROR_INVALIDDATA; | |
780 | } | ||
781 | |||
782 | 30 | memset(ctx->last, 0, 4 * bw * sizeof(int)); | |
783 | 30 | memset(ctx->clast, 0, 4 * bw * sizeof(int)); | |
784 | |||
785 |
2/2✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 30 times.
|
1830 | for (j = 0; j < bh; j++) { |
786 | 1800 | memset(ctx->D, 0, 4 * sizeof(int)); | |
787 | 1800 | memset(ctx->CD, 0, 4 * sizeof(int)); | |
788 |
2/2✓ Branch 0 taken 144000 times.
✓ Branch 1 taken 1800 times.
|
145800 | for (i = 0; i < bw; i++) { |
789 | 144000 | type = GET_TOK(ctx, TM2_TYPE); | |
790 |
7/8✓ Branch 0 taken 66716 times.
✓ Branch 1 taken 22463 times.
✓ Branch 2 taken 12092 times.
✓ Branch 3 taken 15389 times.
✓ Branch 4 taken 5383 times.
✓ Branch 5 taken 20959 times.
✓ Branch 6 taken 998 times.
✗ Branch 7 not taken.
|
144000 | switch(type) { |
791 | 66716 | case TM2_HI_RES: | |
792 | 66716 | tm2_hi_res_block(ctx, p, i, j); | |
793 | 66716 | break; | |
794 | 22463 | case TM2_MED_RES: | |
795 | 22463 | tm2_med_res_block(ctx, p, i, j); | |
796 | 22463 | break; | |
797 | 12092 | case TM2_LOW_RES: | |
798 | 12092 | tm2_low_res_block(ctx, p, i, j); | |
799 | 12092 | break; | |
800 | 15389 | case TM2_NULL_RES: | |
801 | 15389 | tm2_null_res_block(ctx, p, i, j); | |
802 | 15389 | break; | |
803 | 5383 | case TM2_UPDATE: | |
804 | 5383 | tm2_update_block(ctx, p, i, j); | |
805 | 5383 | keyframe = 0; | |
806 | 5383 | break; | |
807 | 20959 | case TM2_STILL: | |
808 | 20959 | tm2_still_block(ctx, p, i, j); | |
809 | 20959 | keyframe = 0; | |
810 | 20959 | break; | |
811 | 998 | case TM2_MOTION: | |
812 | 998 | tm2_motion_block(ctx, p, i, j); | |
813 | 998 | keyframe = 0; | |
814 | 998 | break; | |
815 | ✗ | default: | |
816 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type); | |
817 | } | ||
818 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144000 times.
|
144000 | if (ctx->error) |
819 | ✗ | return AVERROR_INVALIDDATA; | |
820 | } | ||
821 | } | ||
822 | |||
823 | /* copy data from our buffer to AVFrame */ | ||
824 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | Y = (ctx->cur?ctx->Y2:ctx->Y1); |
825 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | U = (ctx->cur?ctx->U2:ctx->U1); |
826 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | V = (ctx->cur?ctx->V2:ctx->V1); |
827 | 30 | dst = p->data[0]; | |
828 |
2/2✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 30 times.
|
7230 | for (j = 0; j < h; j++) { |
829 |
2/2✓ Branch 0 taken 2304000 times.
✓ Branch 1 taken 7200 times.
|
2311200 | for (i = 0; i < w; i++) { |
830 | 2304000 | unsigned y = Y[i], u = U[i >> 1], v = V[i >> 1]; | |
831 | 2304000 | dst[3*i+0] = av_clip_uint8(y + v); | |
832 | 2304000 | dst[3*i+1] = av_clip_uint8(y); | |
833 | 2304000 | dst[3*i+2] = av_clip_uint8(y + u); | |
834 | } | ||
835 | |||
836 | /* horizontal edge extension */ | ||
837 | 7200 | Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0]; | |
838 | 7200 | Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1]; | |
839 | |||
840 | /* vertical edge extension */ | ||
841 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 7170 times.
|
7200 | if (j == 0) { |
842 | 30 | memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride); | |
843 | 30 | memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride); | |
844 | 30 | memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride); | |
845 | 30 | memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride); | |
846 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 7140 times.
|
7170 | } else if (j == h - 1) { |
847 | 30 | memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride); | |
848 | 30 | memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride); | |
849 | 30 | memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride); | |
850 | 30 | memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride); | |
851 | } | ||
852 | |||
853 | 7200 | Y += ctx->y_stride; | |
854 |
2/2✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 3600 times.
|
7200 | if (j & 1) { |
855 | /* horizontal edge extension */ | ||
856 | 3600 | U[-2] = U[-1] = U[0]; | |
857 | 3600 | V[-2] = V[-1] = V[0]; | |
858 | 3600 | U[cw + 1] = U[cw] = U[cw - 1]; | |
859 | 3600 | V[cw + 1] = V[cw] = V[cw - 1]; | |
860 | |||
861 | /* vertical edge extension */ | ||
862 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3570 times.
|
3600 | if (j == 1) { |
863 | 30 | memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride); | |
864 | 30 | memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride); | |
865 | 30 | memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride); | |
866 | 30 | memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride); | |
867 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3540 times.
|
3570 | } else if (j == h - 1) { |
868 | 30 | memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride); | |
869 | 30 | memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride); | |
870 | 30 | memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride); | |
871 | 30 | memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride); | |
872 | } | ||
873 | |||
874 | 3600 | U += ctx->uv_stride; | |
875 | 3600 | V += ctx->uv_stride; | |
876 | } | ||
877 | 7200 | dst += p->linesize[0]; | |
878 | } | ||
879 | |||
880 | 30 | return keyframe; | |
881 | } | ||
882 | |||
883 | static const int tm2_stream_order[TM2_NUM_STREAMS] = { | ||
884 | TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE | ||
885 | }; | ||
886 | |||
887 | #define TM2_HEADER_SIZE 40 | ||
888 | |||
889 | 30 | static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
890 | int *got_frame, AVPacket *avpkt) | ||
891 | { | ||
892 | 30 | TM2Context * const l = avctx->priv_data; | |
893 | 30 | const uint8_t *buf = avpkt->data; | |
894 | 30 | int buf_size = avpkt->size & ~3; | |
895 | 30 | AVFrame * const p = l->pic; | |
896 | 30 | int offset = TM2_HEADER_SIZE; | |
897 | int i, t, ret; | ||
898 | |||
899 | 30 | l->error = 0; | |
900 | |||
901 | 30 | av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size); | |
902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!l->buffer) { |
903 | ✗ | av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n"); | |
904 | ✗ | return AVERROR(ENOMEM); | |
905 | } | ||
906 | |||
907 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | if ((ret = ff_reget_buffer(avctx, p, 0)) < 0) |
908 | ✗ | return ret; | |
909 | |||
910 | 30 | l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf, | |
911 | buf_size >> 2); | ||
912 | |||
913 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | if ((ret = tm2_read_header(l, l->buffer)) < 0) { |
914 | ✗ | return ret; | |
915 | } | ||
916 | |||
917 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 30 times.
|
240 | for (i = 0; i < TM2_NUM_STREAMS; i++) { |
918 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if (offset >= buf_size) { |
919 | ✗ | av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n"); | |
920 | ✗ | return AVERROR_INVALIDDATA; | |
921 | } | ||
922 | |||
923 | 210 | t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i], | |
924 | buf_size - offset); | ||
925 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if (t < 0) { |
926 | ✗ | int j = tm2_stream_order[i]; | |
927 | ✗ | if (l->tok_lens[j]) | |
928 | ✗ | memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]); | |
929 | ✗ | return t; | |
930 | } | ||
931 | 210 | offset += t; | |
932 | } | ||
933 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 25 times.
|
30 | if (tm2_decode_blocks(l, p)) { |
934 | 5 | p->flags |= AV_FRAME_FLAG_KEY; | |
935 | 5 | p->pict_type = AV_PICTURE_TYPE_I; | |
936 | } else { | ||
937 | 25 | p->flags &= ~AV_FRAME_FLAG_KEY; | |
938 | 25 | p->pict_type = AV_PICTURE_TYPE_P; | |
939 | } | ||
940 | |||
941 | 30 | l->cur = !l->cur; | |
942 | 30 | *got_frame = 1; | |
943 | 30 | ret = av_frame_ref(rframe, l->pic); | |
944 | |||
945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | return (ret < 0) ? ret : buf_size; |
946 | } | ||
947 | |||
948 | 2 | static av_cold int decode_init(AVCodecContext *avctx) | |
949 | { | ||
950 | 2 | TM2Context * const l = avctx->priv_data; | |
951 | 2 | int w = avctx->width, h = avctx->height; | |
952 | |||
953 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if ((avctx->width & 3) || (avctx->height & 3)) { |
954 | ✗ | av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n"); | |
955 | ✗ | return AVERROR(EINVAL); | |
956 | } | ||
957 | |||
958 | 2 | l->avctx = avctx; | |
959 | 2 | avctx->pix_fmt = AV_PIX_FMT_BGR24; | |
960 | |||
961 | 2 | l->pic = av_frame_alloc(); | |
962 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!l->pic) |
963 | ✗ | return AVERROR(ENOMEM); | |
964 | |||
965 | 2 | ff_bswapdsp_init(&l->bdsp); | |
966 | |||
967 | 2 | l->last = av_malloc_array(w, 2 * sizeof(*l->last)); | |
968 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!l->last) |
969 | ✗ | return AVERROR(ENOMEM); | |
970 | 2 | l->clast = l->last + w; | |
971 | |||
972 | 2 | w += 8; | |
973 | 2 | h += 8; | |
974 | 2 | l->Y_base = av_calloc(w * h, 2 * sizeof(*l->Y_base)); | |
975 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!l->Y_base) |
976 | ✗ | return AVERROR(ENOMEM); | |
977 | 2 | l->y_stride = w; | |
978 | 2 | l->Y1 = l->Y_base + l->y_stride * 4 + 4; | |
979 | 2 | l->Y2 = l->Y1 + w * h; | |
980 | 2 | w = (w + 1) >> 1; | |
981 | 2 | h = (h + 1) >> 1; | |
982 | 2 | l->UV_base = av_calloc(w * h, 4 * sizeof(*l->UV_base)); | |
983 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!l->UV_base) |
984 | ✗ | return AVERROR(ENOMEM); | |
985 | 2 | l->uv_stride = w; | |
986 | 2 | l->U1 = l->UV_base + l->uv_stride * 2 + 2; | |
987 | 2 | l->U2 = l->U1 + w * h; | |
988 | 2 | l->V1 = l->U2 + w * h; | |
989 | 2 | l->V2 = l->V1 + w * h; | |
990 | |||
991 | 2 | return 0; | |
992 | } | ||
993 | |||
994 | 2 | static av_cold int decode_end(AVCodecContext *avctx) | |
995 | { | ||
996 | 2 | TM2Context * const l = avctx->priv_data; | |
997 | int i; | ||
998 | |||
999 | 2 | av_freep(&l->last); | |
1000 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
|
16 | for (i = 0; i < TM2_NUM_STREAMS; i++) |
1001 | 14 | av_freep(&l->tokens[i]); | |
1002 | |||
1003 | 2 | av_freep(&l->Y_base); | |
1004 | 2 | av_freep(&l->UV_base); | |
1005 | 2 | av_freep(&l->buffer); | |
1006 | 2 | l->buffer_size = 0; | |
1007 | |||
1008 | 2 | av_frame_free(&l->pic); | |
1009 | |||
1010 | 2 | return 0; | |
1011 | } | ||
1012 | |||
1013 | const FFCodec ff_truemotion2_decoder = { | ||
1014 | .p.name = "truemotion2", | ||
1015 | CODEC_LONG_NAME("Duck TrueMotion 2.0"), | ||
1016 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1017 | .p.id = AV_CODEC_ID_TRUEMOTION2, | ||
1018 | .priv_data_size = sizeof(TM2Context), | ||
1019 | .init = decode_init, | ||
1020 | .close = decode_end, | ||
1021 | FF_CODEC_DECODE_CB(decode_frame), | ||
1022 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
1023 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1024 | }; | ||
1025 |