comparison truemotion2.c @ 2967:ef2149182f1c libavcodec

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents 4b3626936f09
children 0b546eab515d
comparison
equal deleted inserted replaced
2966:564788471dd4 2967:ef2149182f1c
15 * You should have received a copy of the GNU Lesser General Public 15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software 16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * 18 *
19 */ 19 */
20 20
21 /** 21 /**
22 * @file truemotion2.c 22 * @file truemotion2.c
23 * Duck TrueMotion2 decoder. 23 * Duck TrueMotion2 decoder.
24 */ 24 */
25 25
26 #include "avcodec.h" 26 #include "avcodec.h"
27 #include "common.h" 27 #include "common.h"
28 #include "bitstream.h" 28 #include "bitstream.h"
29 #include "dsputil.h" 29 #include "dsputil.h"
30 30
41 AVCodecContext *avctx; 41 AVCodecContext *avctx;
42 AVFrame pic; 42 AVFrame pic;
43 43
44 GetBitContext gb; 44 GetBitContext gb;
45 DSPContext dsp; 45 DSPContext dsp;
46 46
47 /* TM2 streams */ 47 /* TM2 streams */
48 int *tokens[TM2_NUM_STREAMS]; 48 int *tokens[TM2_NUM_STREAMS];
49 int tok_lens[TM2_NUM_STREAMS]; 49 int tok_lens[TM2_NUM_STREAMS];
50 int tok_ptrs[TM2_NUM_STREAMS]; 50 int tok_ptrs[TM2_NUM_STREAMS];
51 int deltas[TM2_NUM_STREAMS][TM2_DELTAS]; 51 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
52 /* for blocks decoding */ 52 /* for blocks decoding */
53 int D[4]; 53 int D[4];
54 int CD[4]; 54 int CD[4];
55 int *last; 55 int *last;
56 int *clast; 56 int *clast;
57 57
58 /* data for current and previous frame */ 58 /* data for current and previous frame */
59 int *Y1, *U1, *V1, *Y2, *U2, *V2; 59 int *Y1, *U1, *V1, *Y2, *U2, *V2;
60 int cur; 60 int cur;
61 } TM2Context; 61 } TM2Context;
62 62
116 116
117 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) 117 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
118 { 118 {
119 TM2Huff huff; 119 TM2Huff huff;
120 int res = 0; 120 int res = 0;
121 121
122 huff.val_bits = get_bits(&ctx->gb, 5); 122 huff.val_bits = get_bits(&ctx->gb, 5);
123 huff.max_bits = get_bits(&ctx->gb, 5); 123 huff.max_bits = get_bits(&ctx->gb, 5);
124 huff.min_bits = get_bits(&ctx->gb, 5); 124 huff.min_bits = get_bits(&ctx->gb, 5);
125 huff.nodes = get_bits_long(&ctx->gb, 17); 125 huff.nodes = get_bits_long(&ctx->gb, 17);
126 huff.num = 0; 126 huff.num = 0;
127 127
128 /* check for correct codes parameters */ 128 /* check for correct codes parameters */
129 if((huff.val_bits < 1) || (huff.val_bits > 32) || 129 if((huff.val_bits < 1) || (huff.val_bits > 32) ||
130 (huff.max_bits < 0) || (huff.max_bits > 32)) { 130 (huff.max_bits < 0) || (huff.max_bits > 32)) {
131 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n", 131 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
132 huff.val_bits, huff.max_bits); 132 huff.val_bits, huff.max_bits);
137 return -1; 137 return -1;
138 } 138 }
139 /* one-node tree */ 139 /* one-node tree */
140 if(huff.max_bits == 0) 140 if(huff.max_bits == 0)
141 huff.max_bits = 1; 141 huff.max_bits = 1;
142 142
143 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */ 143 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
144 huff.max_num = (huff.nodes + 1) >> 1; 144 huff.max_num = (huff.nodes + 1) >> 1;
145 huff.nums = av_mallocz(huff.max_num * sizeof(int)); 145 huff.nums = av_mallocz(huff.max_num * sizeof(int));
146 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t)); 146 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
147 huff.lens = av_mallocz(huff.max_num * sizeof(int)); 147 huff.lens = av_mallocz(huff.max_num * sizeof(int));
148 148
149 if(tm2_read_tree(ctx, 0, 0, &huff) == -1) 149 if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
150 res = -1; 150 res = -1;
151 151
152 if(huff.num != huff.max_num) { 152 if(huff.num != huff.max_num) {
153 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n", 153 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
154 huff.num, huff.max_num); 154 huff.num, huff.max_num);
155 res = -1; 155 res = -1;
156 } 156 }
157 157
158 /* convert codes to vlc_table */ 158 /* convert codes to vlc_table */
159 if(res != -1) { 159 if(res != -1) {
160 int i; 160 int i;
161 161
162 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num, 162 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
163 huff.lens, sizeof(int), sizeof(int), 163 huff.lens, sizeof(int), sizeof(int),
164 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0); 164 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
165 if(res < 0) { 165 if(res < 0) {
166 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); 166 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
167 res = -1; 167 res = -1;
168 } else 168 } else
169 res = 0; 169 res = 0;
170 if(res != -1) { 170 if(res != -1) {
171 code->bits = huff.max_bits; 171 code->bits = huff.max_bits;
172 code->length = huff.max_num; 172 code->length = huff.max_num;
173 code->recode = av_malloc(code->length * sizeof(int)); 173 code->recode = av_malloc(code->length * sizeof(int));
177 } 177 }
178 /* free allocated memory */ 178 /* free allocated memory */
179 av_free(huff.nums); 179 av_free(huff.nums);
180 av_free(huff.bits); 180 av_free(huff.bits);
181 av_free(huff.lens); 181 av_free(huff.lens);
182 182
183 return res; 183 return res;
184 } 184 }
185 185
186 static void tm2_free_codes(TM2Codes *code) 186 static void tm2_free_codes(TM2Codes *code)
187 { 187 {
201 static inline int tm2_read_header(TM2Context *ctx, uint8_t *buf) 201 static inline int tm2_read_header(TM2Context *ctx, uint8_t *buf)
202 { 202 {
203 uint32_t magic; 203 uint32_t magic;
204 uint8_t *obuf; 204 uint8_t *obuf;
205 int length; 205 int length;
206 206
207 obuf = buf; 207 obuf = buf;
208 208
209 magic = LE_32(buf); 209 magic = LE_32(buf);
210 buf += 4; 210 buf += 4;
211 211
212 if(magic == 0x00000100) { /* old header */ 212 if(magic == 0x00000100) { /* old header */
213 /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */ 213 /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
214 return 40; 214 return 40;
215 } else if(magic == 0x00000101) { /* new header */ 215 } else if(magic == 0x00000101) { /* new header */
216 int w, h, size, flags, xr, yr; 216 int w, h, size, flags, xr, yr;
217 217
218 length = LE_32(buf); 218 length = LE_32(buf);
219 buf += 4; 219 buf += 4;
220 220
221 init_get_bits(&ctx->gb, buf, 32 * 8); 221 init_get_bits(&ctx->gb, buf, 32 * 8);
222 size = get_bits_long(&ctx->gb, 31); 222 size = get_bits_long(&ctx->gb, 31);
223 h = get_bits(&ctx->gb, 15); 223 h = get_bits(&ctx->gb, 15);
224 w = get_bits(&ctx->gb, 15); 224 w = get_bits(&ctx->gb, 15);
225 flags = get_bits_long(&ctx->gb, 31); 225 flags = get_bits_long(&ctx->gb, 31);
226 yr = get_bits(&ctx->gb, 9); 226 yr = get_bits(&ctx->gb, 9);
227 xr = get_bits(&ctx->gb, 9); 227 xr = get_bits(&ctx->gb, 9);
228 228
229 return 40; 229 return 40;
230 } else { 230 } else {
231 av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic); 231 av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
232 return -1; 232 return -1;
233 } 233 }
234 234
235 return (buf - obuf); 235 return (buf - obuf);
236 } 236 }
237 237
238 static int tm2_read_deltas(TM2Context *ctx, int stream_id) { 238 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
239 int d, mb; 239 int d, mb;
240 int i, v; 240 int i, v;
241 241
242 d = get_bits(&ctx->gb, 9); 242 d = get_bits(&ctx->gb, 9);
243 mb = get_bits(&ctx->gb, 5); 243 mb = get_bits(&ctx->gb, 5);
244 244
245 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) { 245 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
246 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb); 246 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
247 return -1; 247 return -1;
248 } 248 }
249 249
250 for(i = 0; i < d; i++) { 250 for(i = 0; i < d; i++) {
251 v = get_bits_long(&ctx->gb, mb); 251 v = get_bits_long(&ctx->gb, mb);
252 if(v & (1 << (mb - 1))) 252 if(v & (1 << (mb - 1)))
253 ctx->deltas[stream_id][i] = v - (1 << mb); 253 ctx->deltas[stream_id][i] = v - (1 << mb);
254 else 254 else
255 ctx->deltas[stream_id][i] = v; 255 ctx->deltas[stream_id][i] = v;
256 } 256 }
257 for(; i < TM2_DELTAS; i++) 257 for(; i < TM2_DELTAS; i++)
258 ctx->deltas[stream_id][i] = 0; 258 ctx->deltas[stream_id][i] = 0;
259 259
260 return 0; 260 return 0;
261 } 261 }
262 262
263 static int tm2_read_stream(TM2Context *ctx, uint8_t *buf, int stream_id) { 263 static int tm2_read_stream(TM2Context *ctx, uint8_t *buf, int stream_id) {
264 int i; 264 int i;
265 int cur = 0; 265 int cur = 0;
266 int skip = 0; 266 int skip = 0;
267 int len, toks; 267 int len, toks;
268 TM2Codes codes; 268 TM2Codes codes;
269 269
270 /* get stream length in dwords */ 270 /* get stream length in dwords */
271 len = BE_32(buf); buf += 4; cur += 4; 271 len = BE_32(buf); buf += 4; cur += 4;
272 skip = len * 4 + 4; 272 skip = len * 4 + 4;
273 273
274 if(len == 0) 274 if(len == 0)
275 return 4; 275 return 4;
276 276
277 toks = BE_32(buf); buf += 4; cur += 4; 277 toks = BE_32(buf); buf += 4; cur += 4;
278 if(toks & 1) { 278 if(toks & 1) {
279 len = BE_32(buf); buf += 4; cur += 4; 279 len = BE_32(buf); buf += 4; cur += 4;
280 if(len == TM2_ESCAPE) { 280 if(len == TM2_ESCAPE) {
281 len = BE_32(buf); buf += 4; cur += 4; 281 len = BE_32(buf); buf += 4; cur += 4;
292 if(BE_32(buf) == TM2_ESCAPE) { 292 if(BE_32(buf) == TM2_ESCAPE) {
293 buf += 4; cur += 4; /* some unknown length - could be escaped too */ 293 buf += 4; cur += 4; /* some unknown length - could be escaped too */
294 } 294 }
295 buf += 4; cur += 4; 295 buf += 4; cur += 4;
296 buf += 4; cur += 4; /* unused by decoder */ 296 buf += 4; cur += 4; /* unused by decoder */
297 297
298 init_get_bits(&ctx->gb, buf, (skip - cur) * 8); 298 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
299 if(tm2_build_huff_table(ctx, &codes) == -1) 299 if(tm2_build_huff_table(ctx, &codes) == -1)
300 return -1; 300 return -1;
301 buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2; 301 buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
302 cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2; 302 cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
303 303
304 toks >>= 1; 304 toks >>= 1;
305 /* check if we have sane number of tokens */ 305 /* check if we have sane number of tokens */
306 if((toks < 0) || (toks > 0xFFFFFF)){ 306 if((toks < 0) || (toks > 0xFFFFFF)){
307 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); 307 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
308 tm2_free_codes(&codes); 308 tm2_free_codes(&codes);
318 } else { 318 } else {
319 for(i = 0; i < toks; i++) 319 for(i = 0; i < toks; i++)
320 ctx->tokens[stream_id][i] = codes.recode[0]; 320 ctx->tokens[stream_id][i] = codes.recode[0];
321 } 321 }
322 tm2_free_codes(&codes); 322 tm2_free_codes(&codes);
323 323
324 return skip; 324 return skip;
325 } 325 }
326 326
327 static inline int GET_TOK(TM2Context *ctx,int type) { 327 static inline int GET_TOK(TM2Context *ctx,int type) {
328 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) { 328 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
373 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */ 373 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
374 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last) 374 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
375 { 375 {
376 int ct, d; 376 int ct, d;
377 int i, j; 377 int i, j;
378 378
379 for(j = 0; j < 4; j++){ 379 for(j = 0; j < 4; j++){
380 ct = ctx->D[j]; 380 ct = ctx->D[j];
381 for(i = 0; i < 4; i++){ 381 for(i = 0; i < 4; i++){
382 d = deltas[i + j * 4]; 382 d = deltas[i + j * 4];
383 ct += d; 383 ct += d;
415 t = (CD[0] + CD[1]) >> 1; 415 t = (CD[0] + CD[1]) >> 1;
416 l = (prev - CD[0] - CD[1] + clast[1]) >> 1; 416 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
417 CD[1] = CD[0] + CD[1] - t; 417 CD[1] = CD[0] + CD[1] - t;
418 CD[0] = t; 418 CD[0] = t;
419 clast[0] = l; 419 clast[0] = l;
420 420
421 tm2_high_chroma(data, stride, clast, CD, deltas); 421 tm2_high_chroma(data, stride, clast, CD, deltas);
422 } 422 }
423 423
424 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) 424 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
425 { 425 {
432 deltas[i] = GET_TOK(ctx, TM2_C_HI); 432 deltas[i] = GET_TOK(ctx, TM2_C_HI);
433 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI); 433 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
434 } 434 }
435 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas); 435 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
436 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4); 436 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
437 437
438 /* hi-res luma */ 438 /* hi-res luma */
439 for(i = 0; i < 16; i++) 439 for(i = 0; i < 16; i++)
440 deltas[i] = GET_TOK(ctx, TM2_L_HI); 440 deltas[i] = GET_TOK(ctx, TM2_L_HI);
441 441
442 tm2_apply_deltas(ctx, Y, Ystride, deltas, last); 442 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
443 } 443 }
444 444
445 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) 445 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
446 { 446 {
447 int i; 447 int i;
448 int deltas[16]; 448 int deltas[16];
449 TM2_INIT_POINTERS(); 449 TM2_INIT_POINTERS();
450 450
451 /* low-res chroma */ 451 /* low-res chroma */
452 deltas[0] = GET_TOK(ctx, TM2_C_LO); 452 deltas[0] = GET_TOK(ctx, TM2_C_LO);
453 deltas[1] = deltas[2] = deltas[3] = 0; 453 deltas[1] = deltas[2] = deltas[3] = 0;
454 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx); 454 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
455 455
458 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx); 458 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
459 459
460 /* hi-res luma */ 460 /* hi-res luma */
461 for(i = 0; i < 16; i++) 461 for(i = 0; i < 16; i++)
462 deltas[i] = GET_TOK(ctx, TM2_L_HI); 462 deltas[i] = GET_TOK(ctx, TM2_L_HI);
463 463
464 tm2_apply_deltas(ctx, Y, Ystride, deltas, last); 464 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
465 } 465 }
466 466
467 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) 467 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
468 { 468 {
481 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx); 481 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
482 482
483 /* low-res luma */ 483 /* low-res luma */
484 for(i = 0; i < 16; i++) 484 for(i = 0; i < 16; i++)
485 deltas[i] = 0; 485 deltas[i] = 0;
486 486
487 deltas[ 0] = GET_TOK(ctx, TM2_L_LO); 487 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
488 deltas[ 2] = GET_TOK(ctx, TM2_L_LO); 488 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
489 deltas[ 8] = GET_TOK(ctx, TM2_L_LO); 489 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
490 deltas[10] = GET_TOK(ctx, TM2_L_LO); 490 deltas[10] = GET_TOK(ctx, TM2_L_LO);
491 491
492 if(bx > 0) 492 if(bx > 0)
493 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1; 493 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
494 else 494 else
495 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1; 495 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
496 last[2] = (last[1] + last[3]) >> 1; 496 last[2] = (last[1] + last[3]) >> 1;
499 ctx->D[0] = t1 >> 1; 499 ctx->D[0] = t1 >> 1;
500 ctx->D[1] = t1 - (t1 >> 1); 500 ctx->D[1] = t1 - (t1 >> 1);
501 t2 = ctx->D[2] + ctx->D[3]; 501 t2 = ctx->D[2] + ctx->D[3];
502 ctx->D[2] = t2 >> 1; 502 ctx->D[2] = t2 >> 1;
503 ctx->D[3] = t2 - (t2 >> 1); 503 ctx->D[3] = t2 - (t2 >> 1);
504 504
505 tm2_apply_deltas(ctx, Y, Ystride, deltas, last); 505 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
506 } 506 }
507 507
508 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by) 508 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
509 { 509 {
510 int i; 510 int i;
511 int ct; 511 int ct;
512 int left, right, diff; 512 int left, right, diff;
513 int deltas[16]; 513 int deltas[16];
514 TM2_INIT_POINTERS(); 514 TM2_INIT_POINTERS();
515 515
516 /* null chroma */ 516 /* null chroma */
517 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0; 517 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
518 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx); 518 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
519 519
520 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0; 520 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
521 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx); 521 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
522 522
523 /* null luma */ 523 /* null luma */
524 for(i = 0; i < 16; i++) 524 for(i = 0; i < 16; i++)
525 deltas[i] = 0; 525 deltas[i] = 0;
526 526
527 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3]; 527 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
528 528
529 if(bx > 0) 529 if(bx > 0)
530 left = last[-1] - ct; 530 left = last[-1] - ct;
531 else 531 else
532 left = 0; 532 left = 0;
533 533
534 right = last[3]; 534 right = last[3];
535 diff = right - left; 535 diff = right - left;
536 last[0] = left + (diff >> 2); 536 last[0] = left + (diff >> 2);
537 last[1] = left + (diff >> 1); 537 last[1] = left + (diff >> 1);
538 last[2] = right - (diff >> 2); 538 last[2] = right - (diff >> 2);
539 last[3] = right; 539 last[3] = right;
540 { 540 {
541 int tp = left; 541 int tp = left;
542 542
543 ctx->D[0] = (tp + (ct >> 2)) - left; 543 ctx->D[0] = (tp + (ct >> 2)) - left;
544 left += ctx->D[0]; 544 left += ctx->D[0];
545 ctx->D[1] = (tp + (ct >> 1)) - left; 545 ctx->D[1] = (tp + (ct >> 1)) - left;
546 left += ctx->D[1]; 546 left += ctx->D[1];
547 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left; 547 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
589 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by) 589 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
590 { 590 {
591 int i, j; 591 int i, j;
592 int d; 592 int d;
593 TM2_INIT_POINTERS_2(); 593 TM2_INIT_POINTERS_2();
594 594
595 /* update chroma */ 595 /* update chroma */
596 for(j = 0; j < 2; j++){ 596 for(j = 0; j < 2; j++){
597 for(i = 0; i < 2; i++){ 597 for(i = 0; i < 2; i++){
598 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD); 598 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
599 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD); 599 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
630 int mx, my; 630 int mx, my;
631 TM2_INIT_POINTERS_2(); 631 TM2_INIT_POINTERS_2();
632 632
633 mx = GET_TOK(ctx, TM2_MOT); 633 mx = GET_TOK(ctx, TM2_MOT);
634 my = GET_TOK(ctx, TM2_MOT); 634 my = GET_TOK(ctx, TM2_MOT);
635 635
636 Yo += my * oYstride + mx; 636 Yo += my * oYstride + mx;
637 Uo += (my >> 1) * oUstride + (mx >> 1); 637 Uo += (my >> 1) * oUstride + (mx >> 1);
638 Vo += (my >> 1) * oVstride + (mx >> 1); 638 Vo += (my >> 1) * oVstride + (mx >> 1);
639 639
640 /* copy chroma */ 640 /* copy chroma */
641 for(j = 0; j < 2; j++){ 641 for(j = 0; j < 2; j++){
642 for(i = 0; i < 2; i++){ 642 for(i = 0; i < 2; i++){
643 U[i] = Uo[i]; 643 U[i] = Uo[i];
644 V[i] = Vo[i]; 644 V[i] = Vo[i];
675 int bw, bh; 675 int bw, bh;
676 int type; 676 int type;
677 int keyframe = 1; 677 int keyframe = 1;
678 uint8_t *Y, *U, *V; 678 uint8_t *Y, *U, *V;
679 int *src; 679 int *src;
680 680
681 bw = ctx->avctx->width >> 2; 681 bw = ctx->avctx->width >> 2;
682 bh = ctx->avctx->height >> 2; 682 bh = ctx->avctx->height >> 2;
683 683
684 for(i = 0; i < TM2_NUM_STREAMS; i++) 684 for(i = 0; i < TM2_NUM_STREAMS; i++)
685 ctx->tok_ptrs[i] = 0; 685 ctx->tok_ptrs[i] = 0;
686 686
687 if (ctx->tok_lens[TM2_TYPE]<bw*bh){ 687 if (ctx->tok_lens[TM2_TYPE]<bw*bh){
688 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh); 688 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
689 return -1; 689 return -1;
690 } 690 }
691 691
692 memset(ctx->last, 0, 4 * bw * sizeof(int)); 692 memset(ctx->last, 0, 4 * bw * sizeof(int));
693 memset(ctx->clast, 0, 4 * bw * sizeof(int)); 693 memset(ctx->clast, 0, 4 * bw * sizeof(int));
694 694
695 for(j = 0; j < bh; j++) { 695 for(j = 0; j < bh; j++) {
696 memset(ctx->D, 0, 4 * sizeof(int)); 696 memset(ctx->D, 0, 4 * sizeof(int));
725 default: 725 default:
726 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type); 726 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
727 } 727 }
728 } 728 }
729 } 729 }
730 730
731 /* copy data from our buffer to AVFrame */ 731 /* copy data from our buffer to AVFrame */
732 Y = p->data[0]; 732 Y = p->data[0];
733 src = (ctx->cur?ctx->Y2:ctx->Y1); 733 src = (ctx->cur?ctx->Y2:ctx->Y1);
734 for(j = 0; j < ctx->avctx->height; j++){ 734 for(j = 0; j < ctx->avctx->height; j++){
735 for(i = 0; i < ctx->avctx->width; i++){ 735 for(i = 0; i < ctx->avctx->width; i++){
736 Y[i] = clip_uint8(*src++); 736 Y[i] = clip_uint8(*src++);
751 for(i = 0; i < (ctx->avctx->width + 1) >> 1; i++){ 751 for(i = 0; i < (ctx->avctx->width + 1) >> 1; i++){
752 V[i] = clip_uint8(*src++); 752 V[i] = clip_uint8(*src++);
753 } 753 }
754 V += p->linesize[1]; 754 V += p->linesize[1];
755 } 755 }
756 756
757 return keyframe; 757 return keyframe;
758 } 758 }
759 759
760 static int decode_frame(AVCodecContext *avctx, 760 static int decode_frame(AVCodecContext *avctx,
761 void *data, int *data_size, 761 void *data, int *data_size,
762 uint8_t *buf, int buf_size) 762 uint8_t *buf, int buf_size)
763 { 763 {
764 TM2Context * const l = avctx->priv_data; 764 TM2Context * const l = avctx->priv_data;
765 AVFrame * const p= (AVFrame*)&l->pic; 765 AVFrame * const p= (AVFrame*)&l->pic;
772 return -1; 772 return -1;
773 } 773 }
774 774
775 l->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, buf_size >> 2); 775 l->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, buf_size >> 2);
776 skip = tm2_read_header(l, buf); 776 skip = tm2_read_header(l, buf);
777 777
778 if(skip == -1) 778 if(skip == -1)
779 return -1; 779 return -1;
780 780
781 t = tm2_read_stream(l, buf + skip, TM2_C_HI); 781 t = tm2_read_stream(l, buf + skip, TM2_C_HI);
782 if(t == -1) 782 if(t == -1)
783 return -1; 783 return -1;
784 skip += t; 784 skip += t;
785 t = tm2_read_stream(l, buf + skip, TM2_C_LO); 785 t = tm2_read_stream(l, buf + skip, TM2_C_LO);
808 p->key_frame = tm2_decode_blocks(l, p); 808 p->key_frame = tm2_decode_blocks(l, p);
809 if(p->key_frame) 809 if(p->key_frame)
810 p->pict_type = FF_I_TYPE; 810 p->pict_type = FF_I_TYPE;
811 else 811 else
812 p->pict_type = FF_P_TYPE; 812 p->pict_type = FF_P_TYPE;
813 813
814 l->cur = !l->cur; 814 l->cur = !l->cur;
815 *data_size = sizeof(AVFrame); 815 *data_size = sizeof(AVFrame);
816 *(AVFrame*)data = l->pic; 816 *(AVFrame*)data = l->pic;
817 817
818 return buf_size; 818 return buf_size;
819 } 819 }
820 820
821 static int decode_init(AVCodecContext *avctx){ 821 static int decode_init(AVCodecContext *avctx){
822 TM2Context * const l = avctx->priv_data; 822 TM2Context * const l = avctx->priv_data;
827 } 827 }
828 if((avctx->width & 3) || (avctx->height & 3)){ 828 if((avctx->width & 3) || (avctx->height & 3)){
829 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n"); 829 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
830 return -1; 830 return -1;
831 } 831 }
832 832
833 l->avctx = avctx; 833 l->avctx = avctx;
834 l->pic.data[0]=NULL; 834 l->pic.data[0]=NULL;
835 avctx->has_b_frames = 0; 835 avctx->has_b_frames = 0;
836 avctx->pix_fmt = PIX_FMT_YUV420P; 836 avctx->pix_fmt = PIX_FMT_YUV420P;
837 837
838 dsputil_init(&l->dsp, avctx); 838 dsputil_init(&l->dsp, avctx);
839 839
840 l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2)); 840 l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
841 l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2)); 841 l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
842 842
843 for(i = 0; i < TM2_NUM_STREAMS; i++) { 843 for(i = 0; i < TM2_NUM_STREAMS; i++) {
844 l->tokens[i] = NULL; 844 l->tokens[i] = NULL;
845 l->tok_lens[i] = 0; 845 l->tok_lens[i] = 0;
846 } 846 }
847 847
848 l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height); 848 l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
849 l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1)); 849 l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
850 l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1)); 850 l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
851 l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height); 851 l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
852 l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1)); 852 l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
853 l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1)); 853 l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
854 l->cur = 0; 854 l->cur = 0;
855 855
856 return 0; 856 return 0;
857 } 857 }
858 858
859 static int decode_end(AVCodecContext *avctx){ 859 static int decode_end(AVCodecContext *avctx){
860 TM2Context * const l = avctx->priv_data; 860 TM2Context * const l = avctx->priv_data;