Statistics
| Branch: | Revision:

ffmpeg / libavcodec / indeo3.c @ e20c4069

History | View | Annotate | Download (37.5 KB)

1
/*
2
 * Intel Indeo 3 (IV31, IV32, etc.) video decoder for ffmpeg
3
 * written, produced, and directed by Alan Smithee
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
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
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19

    
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <unistd.h>
24

    
25
#include "common.h"
26
#include "avcodec.h"
27
#include "dsputil.h"
28
#include "mpegvideo.h"
29
#include "bswap.h"
30

    
31
#include "indeo3data.h"
32

    
33
typedef struct
34
{
35
  unsigned char *Ybuf;
36
  unsigned char *Ubuf;
37
  unsigned char *Vbuf;
38
  unsigned char *the_buf;
39
  unsigned int the_buf_size;
40
  unsigned short y_w, y_h;
41
  unsigned short uv_w, uv_h;
42
} YUVBufs;
43

    
44
typedef struct Indeo3DecodeContext {
45
    AVCodecContext *avctx;
46
    int width, height;
47
    AVFrame frame;
48

    
49
    YUVBufs iv_frame[2];
50
    YUVBufs *cur_frame;
51
    YUVBufs *ref_frame;
52

    
53
    unsigned char *ModPred;
54
    unsigned short *corrector_type;
55
} Indeo3DecodeContext;
56

    
57
static int corrector_type_0[24] = {
58
  195, 159, 133, 115, 101,  93,  87,  77,
59
  195, 159, 133, 115, 101,  93,  87,  77,
60
  128,  79,  79,  79,  79,  79,  79,  79
61
};
62

    
63
static int corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
64

    
65
static void build_modpred(Indeo3DecodeContext *s) 
66
{
67
  int i, j;
68

    
69
  s->ModPred = (unsigned char *) av_malloc (8 * 128);
70

    
71
  for (i=0; i < 128; ++i) {
72
    s->ModPred[i+0*128] = (i > 126) ? 254 : 2*((i + 1) - ((i + 1) % 2));
73
    s->ModPred[i+1*128] = (i == 7)  ?  20 : ((i == 119 || i == 120)
74
                                 ? 236 : 2*((i + 2) - ((i + 1) % 3)));
75
    s->ModPred[i+2*128] = (i > 125) ? 248 : 2*((i + 2) - ((i + 2) % 4));
76
    s->ModPred[i+3*128] =                         2*((i + 1) - ((i - 3) % 5));
77
    s->ModPred[i+4*128] = (i == 8)  ?  20 : 2*((i + 1) - ((i - 3) % 6));
78
    s->ModPred[i+5*128] =                         2*((i + 4) - ((i + 3) % 7));
79
    s->ModPred[i+6*128] = (i > 123) ? 240 : 2*((i + 4) - ((i + 4) % 8));
80
    s->ModPred[i+7*128] =                         2*((i + 5) - ((i + 4) % 9));
81
  }
82

    
83
  s->corrector_type = (unsigned short *) av_malloc (24 * 256 * sizeof(unsigned short));
84

    
85
  for (i=0; i < 24; ++i) {
86
    for (j=0; j < 256; ++j) {
87
      s->corrector_type[i*256+j] = (j < corrector_type_0[i])
88
                                ? 1 : ((j < 248 || (i == 16 && j == 248))
89
                                       ? 0 : corrector_type_2[j - 248]);
90
    }
91
  }
92
}
93

    
94
static void iv_Decode_Chunk(Indeo3DecodeContext *s, unsigned char *cur, 
95
  unsigned char *ref, int width, int height, unsigned char *buf1, 
96
  long fflags2, unsigned char *hdr,
97
  unsigned char *buf2, int min_width_160);
98

    
99
#define min(a,b) ((a) < (b) ? (a) : (b))
100

    
101
/* ---------------------------------------------------------------------- */
102
static void iv_alloc_frames(Indeo3DecodeContext *s) 
103
{
104
  int luma_width, luma_height, luma_pixels, chroma_width, chroma_height,
105
    chroma_pixels, bufsize, i;
106

    
107
  luma_width = (s->width + 15) & -0x10;
108
  luma_height = (s->height + 15) & -0x10;
109

    
110
  s->iv_frame[0].y_w = s->iv_frame[0].y_h = 
111
    s->iv_frame[0].the_buf_size = 0;
112
  s->iv_frame[1].y_w = s->iv_frame[1].y_h = 
113
    s->iv_frame[1].the_buf_size = 0;
114
  s->iv_frame[1].the_buf = NULL;
115

    
116
  chroma_width = luma_width >> 2;
117
  chroma_height = luma_height >> 2;
118
  luma_pixels = luma_width * luma_height;
119
  chroma_pixels = chroma_width * chroma_height;
120

    
121
  bufsize = luma_pixels * 2 + luma_width * 3 + 
122
    (chroma_pixels + chroma_width) * 4;
123

    
124
  if((s->iv_frame[0].the_buf = 
125
    (s->iv_frame[0].the_buf_size == 0 ? av_malloc(bufsize) : 
126
      av_realloc(s->iv_frame[0].the_buf, bufsize))) == NULL)
127
    return;
128
  s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
129
  s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
130
  s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
131
  s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;
132
  s->iv_frame[0].the_buf_size = bufsize;
133

    
134
  s->iv_frame[0].Ybuf = s->iv_frame[0].the_buf + luma_width;
135
  i = luma_pixels + luma_width * 2;
136
  s->iv_frame[1].Ybuf = s->iv_frame[0].the_buf + i;
137
  i += (luma_pixels + luma_width);
138
  s->iv_frame[0].Ubuf = s->iv_frame[0].the_buf + i;
139
  i += (chroma_pixels + chroma_width);
140
  s->iv_frame[1].Ubuf = s->iv_frame[0].the_buf + i;
141
  i += (chroma_pixels + chroma_width);
142
  s->iv_frame[0].Vbuf = s->iv_frame[0].the_buf + i;
143
  i += (chroma_pixels + chroma_width);
144
  s->iv_frame[1].Vbuf = s->iv_frame[0].the_buf + i;
145

    
146
  for(i = 1; i <= luma_width; i++)
147
    s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] = 
148
      s->iv_frame[0].Ubuf[-i] = 0x80;
149

    
150
  for(i = 1; i <= chroma_width; i++) {
151
    s->iv_frame[1].Ubuf[-i] = 0x80;
152
    s->iv_frame[0].Vbuf[-i] = 0x80;
153
    s->iv_frame[1].Vbuf[-i] = 0x80;
154
    s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;
155
  }
156
}
157

    
158
/* ---------------------------------------------------------------------- */
159
static void iv_free_func(Indeo3DecodeContext *s) 
160
{
161
  int i;
162

    
163
  for(i = 0 ; i < 2 ; i++) {
164
    if(s->iv_frame[i].the_buf != NULL) 
165
      av_free(s->iv_frame[i].the_buf);
166
    s->iv_frame[i].Ybuf = s->iv_frame[i].Ubuf = 
167
      s->iv_frame[i].Vbuf = NULL;
168
    s->iv_frame[i].the_buf = NULL;
169
    s->iv_frame[i].the_buf_size = 0;
170
    s->iv_frame[i].y_w = s->iv_frame[i].y_h = 0;
171
    s->iv_frame[i].uv_w = s->iv_frame[i].uv_h = 0;
172
  }
173

    
174
  av_free(s->ModPred);
175
  av_free(s->corrector_type);
176
}
177

    
178
/* ---------------------------------------------------------------------- */
179
static unsigned long iv_decode_frame(Indeo3DecodeContext *s, 
180
                                     unsigned char *buf, int buf_size) 
181
{
182
  unsigned int hdr_width, hdr_height,
183
    chroma_width, chroma_height;
184
  unsigned long fflags1, fflags2, fflags3, offs1, offs2, offs3, offs;
185
  unsigned char *hdr_pos, *buf_pos;
186

    
187
  buf_pos = buf;
188
  buf_pos += 18;
189

    
190
  fflags1 = le2me_16(*(uint16_t *)buf_pos);
191
  buf_pos += 2;
192
  fflags3 = le2me_32(*(uint32_t *)buf_pos);
193
  buf_pos += 4;
194
  fflags2 = *buf_pos++;
195
  buf_pos += 3;
196
  hdr_height = le2me_16(*(uint16_t *)buf_pos);
197
  buf_pos += 2;
198
  hdr_width = le2me_16(*(uint16_t *)buf_pos);
199
  buf_pos += 2;
200
  chroma_height = ((hdr_height >> 2) + 3) & 0x7ffc;
201
  chroma_width = ((hdr_width >> 2) + 3) & 0x7ffc;
202
  offs1 = le2me_32(*(uint32_t *)buf_pos);
203
  buf_pos += 4;
204
  offs2 = le2me_32(*(uint32_t *)buf_pos);
205
  buf_pos += 4;
206
  offs3 = le2me_32(*(uint32_t *)buf_pos);
207
  buf_pos += 8;
208
  hdr_pos = buf_pos;
209
  if(fflags3 == 0x80) return 4;
210

    
211
  if(fflags1 & 0x200) {
212
    s->cur_frame = s->iv_frame + 1;
213
    s->ref_frame = s->iv_frame;
214
  } else {
215
    s->cur_frame = s->iv_frame;
216
    s->ref_frame = s->iv_frame + 1;
217
  }
218

    
219
  buf_pos = buf + 16 + offs1;
220
  offs = le2me_32(*(uint32_t *)buf_pos);
221
  buf_pos += 4;
222

    
223
  iv_Decode_Chunk(s, s->cur_frame->Ybuf, s->ref_frame->Ybuf, hdr_width, 
224
    hdr_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos, 
225
    min(hdr_width, 160));
226

    
227
  buf_pos = buf + 16 + offs2;
228
  offs = le2me_32(*(uint32_t *)buf_pos);
229
  buf_pos += 4;
230

    
231
  iv_Decode_Chunk(s, s->cur_frame->Vbuf, s->ref_frame->Vbuf, chroma_width, 
232
    chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos, 
233
    min(chroma_width, 40));
234

    
235
  buf_pos = buf + 16 + offs3;
236
  offs = le2me_32(*(uint32_t *)buf_pos);
237
  buf_pos += 4;
238

    
239
  iv_Decode_Chunk(s, s->cur_frame->Ubuf, s->ref_frame->Ubuf, chroma_width, 
240
    chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos, 
241
    min(chroma_width, 40));
242

    
243
  return 8;
244
}
245

    
246
typedef struct {
247
  long xpos;
248
  long ypos;
249
  long width;
250
  long height;
251
  long split_flag;
252
  long split_direction;
253
  long usl7;
254
} ustr_t;
255

    
256
/* ---------------------------------------------------------------------- */
257

    
258
#define LV1_CHECK(buf1,rle_v3,lv1,lp2)  \
259
  if((lv1 & 0x80) != 0) {   \
260
    if(rle_v3 != 0)         \
261
      rle_v3 = 0;           \
262
    else {                  \
263
      rle_v3 = 1;           \
264
      buf1 -= 2;            \
265
    }                       \
266
  }                         \
267
  lp2 = 4;
268

    
269

    
270
#define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)  \
271
  if(rle_v3 == 0) {         \
272
    rle_v2 = *buf1;         \
273
    rle_v1 = 1;             \
274
    if(rle_v2 > 32) {       \
275
      rle_v2 -= 32;         \
276
      rle_v1 = 0;           \
277
    }                       \
278
    rle_v3 = 1;             \
279
  }                         \
280
  buf1--;
281

    
282

    
283
#define LP2_CHECK(buf1,rle_v3,lp2)  \
284
  if(lp2 == 0 && rle_v3 != 0)     \
285
    rle_v3 = 0;           \
286
  else {                  \
287
    buf1--;               \
288
    rle_v3 = 1;           \
289
  }
290

    
291

    
292
#define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
293
  rle_v2--;             \
294
  if(rle_v2 == 0) {     \
295
    rle_v3 = 0;         \
296
    buf1 += 2;          \
297
  }                     \
298
  lp2 = 4;
299

    
300
static void iv_Decode_Chunk(Indeo3DecodeContext *s,
301
  unsigned char *cur, unsigned char *ref, int width, int height, 
302
  unsigned char *buf1, long fflags2, unsigned char *hdr,
303
  unsigned char *buf2, int min_width_160)
304
{
305
  unsigned char bit_buf;
306
  unsigned long bit_pos, lv, lv1, lv2;
307
  long *width_tbl, width_tbl_arr[10];
308
  char *ref_vectors;
309
  unsigned char *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
310
  unsigned long *cur_lp, *ref_lp, *correction_lp[2], *correctionloworder_lp[2],
311
    *correctionhighorder_lp[2];
312
  unsigned short *correction_type_sp[2];
313
  ustr_t strip_tbl[20], *strip;
314
  int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
315
    rle_v1, rle_v2, rle_v3;
316

    
317
  bit_buf = 0;
318
  ref_vectors = NULL;
319

    
320
  width_tbl = width_tbl_arr + 1;
321
  i = (width < 0 ? width + 3 : width)/4;
322
  for(j = -1; j < 8; j++) 
323
    width_tbl[j] = i * j;
324

    
325
  strip = strip_tbl;
326

    
327
  for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
328

    
329
  strip->ypos = strip->xpos = 0;
330
  for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
331
  strip->height = height;
332
  strip->split_direction = 0;
333
  strip->split_flag = 0;
334
  strip->usl7 = 0;
335

    
336
  bit_pos = 0;
337

    
338
  rle_v1 = rle_v2 = rle_v3 = 0;
339

    
340
  while(strip >= strip_tbl) {
341
    if(bit_pos <= 0) {
342
      bit_pos = 8;
343
      bit_buf = *buf1++;
344
    }
345

    
346
    bit_pos -= 2;
347
    cmd = (bit_buf >> bit_pos) & 0x03;
348

    
349
    if(cmd == 0) {
350
      strip++;
351
      memcpy(strip, strip-1, sizeof(ustr_t));
352
      strip->split_flag = 1;
353
      strip->split_direction = 0;
354
      strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);
355
      continue;
356
    } else if(cmd == 1) {
357
      strip++;
358
      memcpy(strip, strip-1, sizeof(ustr_t));
359
      strip->split_flag = 1;
360
      strip->split_direction = 1;
361
      strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);
362
      continue;
363
    } else if(cmd == 2) {
364
      if(strip->usl7 == 0) {
365
        strip->usl7 = 1;
366
        ref_vectors = NULL;
367
        continue;
368
      }
369
    } else if(cmd == 3) {
370
      if(strip->usl7 == 0) {
371
        strip->usl7 = 1;
372
        ref_vectors = buf2 + (*buf1 * 2);
373
        buf1++;
374
        continue;
375
      }
376
    }
377

    
378
    cur_frm_pos = cur + width * strip->ypos + strip->xpos;
379

    
380
    if((blks_width = strip->width) < 0) 
381
      blks_width += 3;
382
    blks_width >>= 2;
383
    blks_height = strip->height;
384

    
385
    if(ref_vectors != NULL) {
386
      ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
387
        ref_vectors[1] + strip->xpos;
388
    } else 
389
      ref_frm_pos = cur_frm_pos - width_tbl[4];
390

    
391
    if(cmd == 2) {
392
      if(bit_pos <= 0) {
393
        bit_pos = 8;
394
        bit_buf = *buf1++;
395
      }
396

    
397
      bit_pos -= 2;
398
      cmd = (bit_buf >> bit_pos) & 0x03;
399

    
400
      if(cmd == 0 || ref_vectors != NULL) {
401
        for(lp1 = 0; lp1 < blks_width; lp1++) {
402
          for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
403
            ((unsigned long *)cur_frm_pos)[j] = ((unsigned long *)ref_frm_pos)[j];
404
          cur_frm_pos += 4;
405
          ref_frm_pos += 4;
406
        }
407
      } else if(cmd != 1) 
408
        return;
409
    } else {
410
      k = *buf1 >> 4;
411
      j = *buf1 & 0x0f;
412
      buf1++;
413
      lv = j + fflags2;
414

    
415
      if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
416
        cp2 = s->ModPred + ((lv - 8) << 7);
417
        cp = ref_frm_pos;
418
        for(i = 0; i < blks_width << 2; i++) { *(cp++) = cp2[*cp >> 1]; }
419
      }
420

    
421
      if(k == 1 || k == 4) {
422
        lv = (hdr[j] & 0xf) + fflags2;
423
        correction_type_sp[0] = s->corrector_type + (lv << 8);
424
        correction_lp[0] = correction + (lv << 8);
425
        lv = (hdr[j] >> 4) + fflags2;
426
        correction_lp[1] = correction + (lv << 8);
427
        correction_type_sp[1] = s->corrector_type + (lv << 8);
428
      } else {
429
        correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);
430
        correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);
431
        correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);
432
        correction_lp[0] = correction_lp[1] = correction + (lv << 8);
433
      }
434

    
435
      switch(k) {
436
        case 1:
437
        case 0:                    /********** CASE 0 **********/
438
          for( ; blks_height > 0; blks_height -= 4) {
439
            for(lp1 = 0; lp1 < blks_width; lp1++) {
440
              for(lp2 = 0; lp2 < 4; ) {
441
                k = *buf1++;
442
                cur_lp = ((unsigned long *)cur_frm_pos) + width_tbl[lp2];
443
                ref_lp = ((unsigned long *)ref_frm_pos) + width_tbl[lp2];
444

    
445
                switch(correction_type_sp[0][k]) {
446
                  case 0:
447
                    *cur_lp = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
448
                    lp2++;
449
                    break;
450
                  case 1:
451
                    ((unsigned short *)cur_lp)[0] = ((((unsigned short *)(ref_lp))[0] >> 1)
452
                      + correction_lp[lp2 & 0x01][*buf1++]) << 1;
453
                    ((unsigned short *)cur_lp)[1] = ((((unsigned short *)(ref_lp))[1] >> 1)
454
                      + correction_lp[lp2 & 0x01][k]) << 1;
455
                    lp2++;
456
                    break;
457
                  case 2:
458
                    if(lp2 == 0) {
459
                      for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
460
                        cur_lp[j] = ref_lp[j];
461
                      lp2 += 2;
462
                    }
463
                    break;
464
                  case 3:
465
                    if(lp2 < 2) {
466
                      for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
467
                        cur_lp[j] = ref_lp[j];
468
                      lp2 = 3;
469
                    }
470
                    break;
471
                  case 8:
472
                    if(lp2 == 0) {
473
                      RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
474

    
475
                      if(rle_v1 == 1 || ref_vectors != NULL) {
476
                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
477
                          cur_lp[j] = ref_lp[j];
478
                      }
479

    
480
                      RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
481
                      break;
482
                    } else {
483
                      rle_v1 = 1;
484
                      rle_v2 = *buf1 - 1;
485
                    }
486
                  case 5:
487
                      LP2_CHECK(buf1,rle_v3,lp2)
488
                  case 4:
489
                    for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
490
                      cur_lp[j] = ref_lp[j];
491
                    lp2 = 4;
492
                    break;
493

    
494
                  case 7:
495
                    if(rle_v3 != 0) 
496
                      rle_v3 = 0;
497
                    else {
498
                      buf1--;
499
                      rle_v3 = 1;
500
                    }
501
                  case 6:
502
                    if(ref_vectors != NULL) {
503
                      for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
504
                        cur_lp[j] = ref_lp[j];
505
                    }
506
                    lp2 = 4;
507
                    break;
508

    
509
                  case 9:
510
                    lv1 = *buf1++;
511
                    lv = (lv1 & 0x7F) << 1;
512
                    lv += (lv << 8);
513
                    lv += (lv << 16);
514
                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
515
                      cur_lp[j] = lv;
516

    
517
                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
518
                    break;
519
                  default: 
520
                    return;
521
                }
522
              }
523

    
524
              cur_frm_pos += 4;
525
              ref_frm_pos += 4;
526
            }
527

    
528
            cur_frm_pos += ((width - blks_width) * 4);
529
            ref_frm_pos += ((width - blks_width) * 4);
530
          }
531
          break;
532

    
533
        case 4:
534
        case 3:                    /********** CASE 3 **********/
535
          if(ref_vectors != NULL) 
536
            return;
537
          flag1 = 1;
538

    
539
          for( ; blks_height > 0; blks_height -= 8) {
540
            for(lp1 = 0; lp1 < blks_width; lp1++) {
541
              for(lp2 = 0; lp2 < 4; ) {
542
                k = *buf1++;
543

    
544
                cur_lp = ((unsigned long *)cur_frm_pos) + width_tbl[lp2 * 2];
545
                ref_lp = ((unsigned long *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
546

    
547
                switch(correction_type_sp[lp2 & 0x01][k]) {
548
                  case 0:
549
                    cur_lp[width_tbl[1]] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
550
                    if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
551
                      cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
552
                    else
553
                      cur_lp[0] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
554
                    lp2++;
555
                    break;
556

    
557
                  case 1:
558
                    ((unsigned short *)cur_lp)[width_tbl[2]] =
559
                      ((((unsigned short *)ref_lp)[0] >> 1) + correction_lp[lp2 & 0x01][*buf1++]) << 1;
560
                    ((unsigned short *)cur_lp)[width_tbl[2]+1] =
561
                      ((((unsigned short *)ref_lp)[1] >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
562
                    if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
563
                      cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
564
                    else
565
                      cur_lp[0] = cur_lp[width_tbl[1]];
566
                    lp2++;
567
                    break;
568

    
569
                  case 2:
570
                    if(lp2 == 0) {
571
                      for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
572
                        cur_lp[j] = *ref_lp;
573
                      lp2 += 2;
574
                    }
575
                    break;
576

    
577
                  case 3:
578
                    if(lp2 < 2) {
579
                      for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
580
                        cur_lp[j] = *ref_lp;
581
                      lp2 = 3;
582
                    }
583
                    break;
584

    
585
                  case 6:
586
                    lp2 = 4;
587
                    break;
588

    
589
                  case 7:
590
                    if(rle_v3 != 0) 
591
                      rle_v3 = 0;
592
                    else {
593
                      buf1--;
594
                      rle_v3 = 1;
595
                    }
596
                    lp2 = 4;
597
                    break;
598

    
599
                  case 8:
600
                    if(lp2 == 0) {
601
                      RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
602

    
603
                      if(rle_v1 == 1) {
604
                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
605
                          cur_lp[j] = ref_lp[j];
606
                      }
607

    
608
                      RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
609
                      break;
610
                    } else {
611
                      rle_v2 = (*buf1) - 1;
612
                      rle_v1 = 1;
613
                    }
614
                  case 5:
615
                      LP2_CHECK(buf1,rle_v3,lp2)
616
                  case 4:
617
                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
618
                      cur_lp[j] = *ref_lp;
619
                    lp2 = 4;
620
                    break;
621

    
622
                  case 9:
623
                    fprintf(stderr, "UNTESTED.\n");
624
                    lv1 = *buf1++;
625
                    lv = (lv1 & 0x7F) << 1;
626
                    lv += (lv << 8);
627
                    lv += (lv << 16);
628

    
629
                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
630
                      cur_lp[j] = lv;
631

    
632
                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
633
                    break;
634

    
635
                  default: 
636
                    return;
637
                }
638
              }
639

    
640
              cur_frm_pos += 4;
641
            }
642

    
643
            cur_frm_pos += (((width * 2) - blks_width) * 4);
644
            flag1 = 0;
645
          }
646
          break;
647

    
648
        case 10:                    /********** CASE 10 **********/
649
          if(ref_vectors == NULL) {
650
            flag1 = 1;
651

    
652
            for( ; blks_height > 0; blks_height -= 8) {
653
              for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
654
                for(lp2 = 0; lp2 < 4; ) {
655
                  k = *buf1++;
656
                  cur_lp = ((unsigned long *)cur_frm_pos) + width_tbl[lp2 * 2];
657
                  ref_lp = ((unsigned long *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
658
                  lv1 = ref_lp[0];
659
                  lv2 = ref_lp[1];
660
                  if(lp2 == 0 && flag1 != 0) {
661
                    lv1 = lv1 & 0x00FF00FF;
662
                    lv1 = (lv1 << 8) | lv1;
663
                    lv2 = lv2 & 0x00FF00FF;
664
                    lv2 = (lv2 << 8) | lv2;
665
                  }
666

    
667
                  switch(correction_type_sp[lp2 & 0x01][k]) {
668
                    case 0:
669
                      cur_lp[width_tbl[1]] = ((lv1 >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1;
670
                      cur_lp[width_tbl[1]+1] = ((lv2 >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1;
671
                      if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
672
                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
673
                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
674
                      } else {
675
                        cur_lp[0] = cur_lp[width_tbl[1]];
676
                        cur_lp[1] = cur_lp[width_tbl[1]+1];
677
                      }
678
                      lp2++;
679
                      break;
680

    
681
                    case 1:
682
                      cur_lp[width_tbl[1]] = ((lv1 >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1++]) << 1;
683
                      cur_lp[width_tbl[1]+1] = ((lv2 >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1;
684
                      if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
685
                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
686
                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
687
                      } else {
688
                        cur_lp[0] = cur_lp[width_tbl[1]];
689
                        cur_lp[1] = cur_lp[width_tbl[1]+1];
690
                      }
691
                      lp2++;
692
                      break;
693

    
694
                    case 2:
695
                      if(lp2 == 0) {
696
                        if(flag1 != 0) {
697
                          for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
698
                            cur_lp[j] = lv1;
699
                            cur_lp[j+1] = lv2;
700
                          }
701
                          cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
702
                          cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
703
                        } else {
704
                          for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
705
                            cur_lp[j] = lv1;
706
                            cur_lp[j+1] = lv2;
707
                          }
708
                        }
709
                        lp2 += 2;
710
                      }
711
                      break;
712

    
713
                    case 3:
714
                      if(lp2 < 2) {
715
                        if(lp2 == 0 && flag1 != 0) {
716
                          for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
717
                            cur_lp[j] = lv1;
718
                            cur_lp[j+1] = lv2;
719
                          }
720
                          cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
721
                          cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
722
                        } else {
723
                          for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
724
                            cur_lp[j] = lv1;
725
                            cur_lp[j+1] = lv2;
726
                          }
727
                        }
728
                        lp2 = 3;
729
                      }
730
                      break;
731

    
732
                    case 8:
733
                      if(lp2 == 0) {
734
                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
735
                        if(rle_v1 == 1) {
736
                          if(flag1 != 0) {
737
                            for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
738
                              cur_lp[j] = lv1;
739
                              cur_lp[j+1] = lv2;
740
                            }
741
                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
742
                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
743
                          } else {
744
                            for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
745
                              cur_lp[j] = lv1;
746
                              cur_lp[j+1] = lv2;
747
                            }
748
                          }
749
                        }
750
                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
751
                        break;
752
                      } else {
753
                        rle_v1 = 1;
754
                        rle_v2 = (*buf1) - 1;
755
                      }
756
                    case 5:
757
                        LP2_CHECK(buf1,rle_v3,lp2)
758
                    case 4:
759
                      if(lp2 == 0 && flag1 != 0) {
760
                        for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
761
                          cur_lp[j] = lv1;
762
                          cur_lp[j+1] = lv2;
763
                        }
764
                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
765
                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
766
                      } else {
767
                        for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
768
                          cur_lp[j] = lv1;
769
                          cur_lp[j+1] = lv2;
770
                        }
771
                      }
772
                      lp2 = 4;
773
                      break;
774

    
775
                    case 6:
776
                      lp2 = 4;
777
                      break;
778

    
779
                    case 7:
780
                      if(lp2 == 0) {
781
                        if(rle_v3 != 0) 
782
                          rle_v3 = 0;
783
                        else {
784
                          buf1--;
785
                          rle_v3 = 1;
786
                        }
787
                        lp2 = 4;
788
                      }
789
                      break;
790

    
791
                    case 9:
792
                      fprintf(stderr, "UNTESTED.\n");
793
                      lv1 = *buf1;
794
                      lv = (lv1 & 0x7F) << 1;
795
                      lv += (lv << 8);
796
                      lv += (lv << 16);
797
                      for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
798
                        cur_lp[j] = lv;
799
                      LV1_CHECK(buf1,rle_v3,lv1,lp2)
800
                      break;
801

    
802
                    default: 
803
                      return;
804
                  }
805
                }
806

    
807
                cur_frm_pos += 8;
808
              }
809

    
810
              cur_frm_pos += (((width * 2) - blks_width) * 4);
811
              flag1 = 0;
812
            }
813
          } else {
814
            for( ; blks_height > 0; blks_height -= 8) {
815
              for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
816
                for(lp2 = 0; lp2 < 4; ) {
817
                  k = *buf1++;
818
                  cur_lp = ((unsigned long *)cur_frm_pos) + width_tbl[lp2 * 2];
819
                  ref_lp = ((unsigned long *)ref_frm_pos) + width_tbl[lp2 * 2];
820

    
821
                  switch(correction_type_sp[lp2 & 0x01][k]) {
822
                    case 0:
823
                      lv1 = correctionloworder_lp[lp2 & 0x01][k];
824
                      lv2 = correctionhighorder_lp[lp2 & 0x01][k];
825
                      cur_lp[0] = ((ref_lp[0] >> 1) + lv1) << 1;
826
                      cur_lp[1] = ((ref_lp[1] >> 1) + lv2) << 1;
827
                      cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + lv1) << 1;
828
                      cur_lp[width_tbl[1]+1] = ((ref_lp[width_tbl[1]+1] >> 1) + lv2) << 1;
829
                      lp2++;
830
                      break;
831

    
832
                    case 1:
833
                      lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
834
                      lv2 = correctionloworder_lp[lp2 & 0x01][k];
835
                      cur_lp[0] = ((ref_lp[0] >> 1) + lv1) << 1;
836
                      cur_lp[1] = ((ref_lp[1] >> 1) + lv2) << 1;
837
                      cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + lv1) << 1;
838
                      cur_lp[width_tbl[1]+1] = ((ref_lp[width_tbl[1]+1] >> 1) + lv2) << 1;
839
                      lp2++;
840
                      break;
841

    
842
                    case 2:
843
                      if(lp2 == 0) {
844
                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
845
                          cur_lp[j] = ref_lp[j];
846
                          cur_lp[j+1] = ref_lp[j+1];
847
                        }
848
                        lp2 += 2;
849
                      }
850
                      break;
851

    
852
                    case 3:
853
                      if(lp2 < 2) {
854
                        for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
855
                          cur_lp[j] = ref_lp[j];
856
                          cur_lp[j+1] = ref_lp[j+1];
857
                        }
858
                        lp2 = 3;
859
                      }
860
                      break;
861

    
862
                    case 8:
863
                      if(lp2 == 0) {
864
                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
865
                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
866
                          ((unsigned long *)cur_frm_pos)[j] = ((unsigned long *)ref_frm_pos)[j];
867
                          ((unsigned long *)cur_frm_pos)[j+1] = ((unsigned long *)ref_frm_pos)[j+1];
868
                        }
869
                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
870
                        break;
871
                      } else {
872
                        rle_v1 = 1;
873
                        rle_v2 = (*buf1) - 1;
874
                      }
875
                    case 5:
876
                    case 7:
877
                        LP2_CHECK(buf1,rle_v3,lp2)
878
                    case 6:
879
                    case 4:
880
                      for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
881
                        cur_lp[j] = ref_lp[j];
882
                        cur_lp[j+1] = ref_lp[j+1];
883
                      }
884
                      lp2 = 4;
885
                      break;
886

    
887
                    case 9:
888
                      fprintf(stderr, "UNTESTED.\n");
889
                      lv1 = *buf1;
890
                      lv = (lv1 & 0x7F) << 1;
891
                      lv += (lv << 8);
892
                      lv += (lv << 16);
893
                      for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
894
                        ((unsigned long *)cur_frm_pos)[j] = ((unsigned long *)cur_frm_pos)[j+1] = lv;
895
                      LV1_CHECK(buf1,rle_v3,lv1,lp2)
896
                      break;
897

    
898
                    default: 
899
                      return;
900
                  }
901
                }
902

    
903
                cur_frm_pos += 8;
904
                ref_frm_pos += 8;
905
              }
906

    
907
              cur_frm_pos += (((width * 2) - blks_width) * 4);
908
              ref_frm_pos += (((width * 2) - blks_width) * 4);
909
            }
910
          }
911
          break;
912

    
913
        case 11:                    /********** CASE 11 **********/
914
          if(ref_vectors == NULL) 
915
            return;
916

    
917
          for( ; blks_height > 0; blks_height -= 8) {
918
            for(lp1 = 0; lp1 < blks_width; lp1++) {
919
              for(lp2 = 0; lp2 < 4; ) {
920
                k = *buf1++;
921
                cur_lp = ((unsigned long *)cur_frm_pos) + width_tbl[lp2 * 2];
922
                ref_lp = ((unsigned long *)ref_frm_pos) + width_tbl[lp2 * 2];
923

    
924
                switch(correction_type_sp[lp2 & 0x01][k]) {
925
                  case 0:
926
                    cur_lp[0] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
927
                    cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
928
                    lp2++;
929
                    break;
930

    
931
                  case 1:
932
                    lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
933
                    lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
934
                    ((unsigned short *)cur_lp)[0] = ((((unsigned short *)ref_lp)[0] >> 1) + lv1) << 1;
935
                    ((unsigned short *)cur_lp)[1] = ((((unsigned short *)ref_lp)[1] >> 1) + lv2) << 1;
936
                    ((unsigned short *)cur_lp)[width_tbl[2]] = ((((unsigned short *)ref_lp)[width_tbl[2]] >> 1) + lv1) << 1;
937
                    ((unsigned short *)cur_lp)[width_tbl[2]+1] = ((((unsigned short *)ref_lp)[width_tbl[2]+1] >> 1) + lv2) << 1;
938
                    lp2++;
939
                    break;
940

    
941
                  case 2:
942
                    if(lp2 == 0) {
943
                      for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
944
                        cur_lp[j] = ref_lp[j];
945
                      lp2 += 2;
946
                    }
947
                    break;
948

    
949
                  case 3:
950
                    if(lp2 < 2) {
951
                      for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
952
                        cur_lp[j] = ref_lp[j];
953
                      lp2 = 3;
954
                    }
955
                    break;
956

    
957
                  case 8:
958
                    if(lp2 == 0) {
959
                      RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
960

    
961
                      for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
962
                        cur_lp[j] = ref_lp[j];
963

    
964
                      RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
965
                      break;
966
                    } else {
967
                      rle_v1 = 1;
968
                      rle_v2 = (*buf1) - 1;
969
                    }
970
                  case 5:
971
                  case 7:
972
                      LP2_CHECK(buf1,rle_v3,lp2)
973
                  case 4:
974
                  case 6:
975
                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
976
                      cur_lp[j] = ref_lp[j];
977
                    lp2 = 4;
978
                    break;
979

    
980
                case 9:
981
                  fprintf(stderr, "UNTESTED.\n");
982
                  lv1 = *buf1++;
983
                  lv = (lv1 & 0x7F) << 1;
984
                  lv += (lv << 8);
985
                  lv += (lv << 16);
986
                  for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
987
                    cur_lp[j] = lv;
988
                  LV1_CHECK(buf1,rle_v3,lv1,lp2)
989
                  break;
990

    
991
                  default: 
992
                    return;
993
                }
994
              }
995

    
996
              cur_frm_pos += 4;
997
              ref_frm_pos += 4;
998
            }
999

    
1000
            cur_frm_pos += (((width * 2) - blks_width) * 4);
1001
            ref_frm_pos += (((width * 2) - blks_width) * 4);
1002
          }
1003
          break;
1004

    
1005
        default: 
1006
          return;
1007
      }
1008
    }
1009

    
1010
    if(strip < strip_tbl) 
1011
      return;
1012

    
1013
    for( ; strip >= strip_tbl; strip--) {
1014
      if(strip->split_flag != 0) {
1015
        strip->split_flag = 0;
1016
        strip->usl7 = (strip-1)->usl7;
1017

    
1018
        if(strip->split_direction) {
1019
          strip->xpos += strip->width;
1020
          strip->width = (strip-1)->width - strip->width;
1021
          if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
1022
            strip->width = width - strip->xpos;
1023
        } else {
1024
          strip->ypos += strip->height;
1025
          strip->height = (strip-1)->height - strip->height;
1026
        }
1027
        break;
1028
      }
1029
    }
1030
  }
1031
}
1032

    
1033
static int indeo3_decode_init(AVCodecContext *avctx)
1034
{
1035
    Indeo3DecodeContext *s = avctx->priv_data;
1036

    
1037
    s->avctx = avctx;
1038
    s->width = avctx->width;
1039
    s->height = avctx->height;
1040
    avctx->pix_fmt = PIX_FMT_YUV410P;
1041
    avctx->has_b_frames = 0;
1042

    
1043
    build_modpred(s);
1044
    iv_alloc_frames(s);
1045

    
1046
    return 0;
1047
}
1048

    
1049
static int indeo3_decode_frame(AVCodecContext *avctx,
1050
                               void *data, int *data_size,
1051
                               unsigned char *buf, int buf_size)
1052
{
1053
    Indeo3DecodeContext *s=avctx->priv_data;
1054
    unsigned char *src, *dest;
1055
    int y;
1056

    
1057
    iv_decode_frame(s, buf, buf_size);
1058

    
1059
    if(s->frame.data[0])
1060
        avctx->release_buffer(avctx, &s->frame);
1061

    
1062
    s->frame.reference = 0;
1063
    if(avctx->get_buffer(avctx, &s->frame) < 0) {
1064
        fprintf(stderr, "get_buffer() failed\n");
1065
        return -1;
1066
    }
1067

    
1068
    src = s->cur_frame->Ybuf;
1069
    dest = s->frame.data[0];
1070
    for (y = 0; y < s->height; y++) {
1071
      memcpy(dest, src, s->cur_frame->y_w);
1072
      src += s->cur_frame->y_w;
1073
      dest += s->frame.linesize[0];
1074
    }
1075

    
1076
    src = s->cur_frame->Ubuf;
1077
    dest = s->frame.data[1];
1078
    for (y = 0; y < s->height / 4; y++) {
1079
      memcpy(dest, src, s->cur_frame->uv_w);
1080
      src += s->cur_frame->uv_w;
1081
      dest += s->frame.linesize[1];
1082
    }
1083

    
1084
    src = s->cur_frame->Vbuf;
1085
    dest = s->frame.data[2];
1086
    for (y = 0; y < s->height / 4; y++) {
1087
      memcpy(dest, src, s->cur_frame->uv_w);
1088
      src += s->cur_frame->uv_w;
1089
      dest += s->frame.linesize[2];
1090
    }
1091

    
1092
    *data_size=sizeof(AVFrame);
1093
    *(AVFrame*)data= s->frame;
1094

    
1095
    return buf_size;
1096
}
1097

    
1098
static int indeo3_decode_end(AVCodecContext *avctx)
1099
{
1100
    Indeo3DecodeContext *s = avctx->priv_data;
1101

    
1102
    iv_free_func(s);
1103

    
1104
    return 0;
1105
}
1106

    
1107
AVCodec indeo3_decoder = {
1108
    "indeo3",
1109
    CODEC_TYPE_VIDEO,
1110
    CODEC_ID_INDEO3,
1111
    sizeof(Indeo3DecodeContext),
1112
    indeo3_decode_init,
1113
    NULL,
1114
    indeo3_decode_end,
1115
    indeo3_decode_frame,
1116
    0,
1117
    NULL
1118
};