Statistics
| Branch: | Revision:

ffmpeg / libavcodec / indeo3.c @ 727c236a

History | View | Annotate | Download (38.9 KB)

1 deabd4fd Mike Melanson
/*
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 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 deabd4fd Mike Melanson
 */
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
30
#include "indeo3data.h"
31
32
typedef struct
33
{
34
  unsigned char *Ybuf;
35
  unsigned char *Ubuf;
36
  unsigned char *Vbuf;
37
  unsigned char *the_buf;
38
  unsigned int the_buf_size;
39
  unsigned short y_w, y_h;
40
  unsigned short uv_w, uv_h;
41
} YUVBufs;
42
43
typedef struct Indeo3DecodeContext {
44
    AVCodecContext *avctx;
45
    int width, height;
46
    AVFrame frame;
47
48
    YUVBufs iv_frame[2];
49
    YUVBufs *cur_frame;
50
    YUVBufs *ref_frame;
51
52
    unsigned char *ModPred;
53
    unsigned short *corrector_type;
54
} Indeo3DecodeContext;
55
56
static int corrector_type_0[24] = {
57
  195, 159, 133, 115, 101,  93,  87,  77,
58
  195, 159, 133, 115, 101,  93,  87,  77,
59
  128,  79,  79,  79,  79,  79,  79,  79
60
};
61
62
static int corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
63
64 115329f1 Diego Biurrun
static void build_modpred(Indeo3DecodeContext *s)
65 deabd4fd Mike Melanson
{
66
  int i, j;
67
68
  s->ModPred = (unsigned char *) av_malloc (8 * 128);
69
70
  for (i=0; i < 128; ++i) {
71
    s->ModPred[i+0*128] = (i > 126) ? 254 : 2*((i + 1) - ((i + 1) % 2));
72
    s->ModPred[i+1*128] = (i == 7)  ?  20 : ((i == 119 || i == 120)
73 bb270c08 Diego Biurrun
                                 ? 236 : 2*((i + 2) - ((i + 1) % 3)));
74 deabd4fd Mike Melanson
    s->ModPred[i+2*128] = (i > 125) ? 248 : 2*((i + 2) - ((i + 2) % 4));
75 bb270c08 Diego Biurrun
    s->ModPred[i+3*128] =                        2*((i + 1) - ((i - 3) % 5));
76 deabd4fd Mike Melanson
    s->ModPred[i+4*128] = (i == 8)  ?  20 : 2*((i + 1) - ((i - 3) % 6));
77 bb270c08 Diego Biurrun
    s->ModPred[i+5*128] =                        2*((i + 4) - ((i + 3) % 7));
78 deabd4fd Mike Melanson
    s->ModPred[i+6*128] = (i > 123) ? 240 : 2*((i + 4) - ((i + 4) % 8));
79 bb270c08 Diego Biurrun
    s->ModPred[i+7*128] =                        2*((i + 5) - ((i + 4) % 9));
80 deabd4fd Mike Melanson
  }
81
82
  s->corrector_type = (unsigned short *) av_malloc (24 * 256 * sizeof(unsigned short));
83
84
  for (i=0; i < 24; ++i) {
85
    for (j=0; j < 256; ++j) {
86
      s->corrector_type[i*256+j] = (j < corrector_type_0[i])
87 bb270c08 Diego Biurrun
                                ? 1 : ((j < 248 || (i == 16 && j == 248))
88
                                       ? 0 : corrector_type_2[j - 248]);
89 deabd4fd Mike Melanson
    }
90
  }
91
}
92
93 115329f1 Diego Biurrun
static void iv_Decode_Chunk(Indeo3DecodeContext *s, unsigned char *cur,
94
  unsigned char *ref, int width, int height, unsigned char *buf1,
95 deabd4fd Mike Melanson
  long fflags2, unsigned char *hdr,
96
  unsigned char *buf2, int min_width_160);
97
98 cdb2c1ca Sebastian Jedruszkiewicz
#ifndef min
99 deabd4fd Mike Melanson
#define min(a,b) ((a) < (b) ? (a) : (b))
100 cdb2c1ca Sebastian Jedruszkiewicz
#endif
101 deabd4fd Mike Melanson
102
/* ---------------------------------------------------------------------- */
103 115329f1 Diego Biurrun
static void iv_alloc_frames(Indeo3DecodeContext *s)
104 deabd4fd Mike Melanson
{
105
  int luma_width, luma_height, luma_pixels, chroma_width, chroma_height,
106 7fea94ce Zdenek Kabelac
      chroma_pixels, i;
107
  unsigned int bufsize;
108 deabd4fd Mike Melanson
109 2ecad8bd Michael Niedermayer
  luma_width   = (s->width  + 3) & (~3);
110
  luma_height  = (s->height + 3) & (~3);
111 deabd4fd Mike Melanson
112 115329f1 Diego Biurrun
  s->iv_frame[0].y_w = s->iv_frame[0].y_h =
113 deabd4fd Mike Melanson
    s->iv_frame[0].the_buf_size = 0;
114 115329f1 Diego Biurrun
  s->iv_frame[1].y_w = s->iv_frame[1].y_h =
115 deabd4fd Mike Melanson
    s->iv_frame[1].the_buf_size = 0;
116
  s->iv_frame[1].the_buf = NULL;
117
118 2ecad8bd Michael Niedermayer
  chroma_width  = ((luma_width >> 2) + 3) & (~3);
119
  chroma_height = ((luma_height>> 2) + 3) & (~3);
120 deabd4fd Mike Melanson
  luma_pixels = luma_width * luma_height;
121
  chroma_pixels = chroma_width * chroma_height;
122
123 115329f1 Diego Biurrun
  bufsize = luma_pixels * 2 + luma_width * 3 +
124 deabd4fd Mike Melanson
    (chroma_pixels + chroma_width) * 4;
125
126 115329f1 Diego Biurrun
  if((s->iv_frame[0].the_buf =
127
    (s->iv_frame[0].the_buf_size == 0 ? av_malloc(bufsize) :
128 deabd4fd Mike Melanson
      av_realloc(s->iv_frame[0].the_buf, bufsize))) == NULL)
129
    return;
130
  s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
131
  s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
132
  s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
133
  s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;
134
  s->iv_frame[0].the_buf_size = bufsize;
135
136
  s->iv_frame[0].Ybuf = s->iv_frame[0].the_buf + luma_width;
137
  i = luma_pixels + luma_width * 2;
138
  s->iv_frame[1].Ybuf = s->iv_frame[0].the_buf + i;
139
  i += (luma_pixels + luma_width);
140
  s->iv_frame[0].Ubuf = s->iv_frame[0].the_buf + i;
141
  i += (chroma_pixels + chroma_width);
142
  s->iv_frame[1].Ubuf = s->iv_frame[0].the_buf + i;
143
  i += (chroma_pixels + chroma_width);
144
  s->iv_frame[0].Vbuf = s->iv_frame[0].the_buf + i;
145
  i += (chroma_pixels + chroma_width);
146
  s->iv_frame[1].Vbuf = s->iv_frame[0].the_buf + i;
147
148
  for(i = 1; i <= luma_width; i++)
149 115329f1 Diego Biurrun
    s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =
150 deabd4fd Mike Melanson
      s->iv_frame[0].Ubuf[-i] = 0x80;
151
152
  for(i = 1; i <= chroma_width; i++) {
153
    s->iv_frame[1].Ubuf[-i] = 0x80;
154
    s->iv_frame[0].Vbuf[-i] = 0x80;
155
    s->iv_frame[1].Vbuf[-i] = 0x80;
156
    s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;
157
  }
158
}
159
160
/* ---------------------------------------------------------------------- */
161 115329f1 Diego Biurrun
static void iv_free_func(Indeo3DecodeContext *s)
162 deabd4fd Mike Melanson
{
163
  int i;
164
165
  for(i = 0 ; i < 2 ; i++) {
166 115329f1 Diego Biurrun
    if(s->iv_frame[i].the_buf != NULL)
167 deabd4fd Mike Melanson
      av_free(s->iv_frame[i].the_buf);
168 115329f1 Diego Biurrun
    s->iv_frame[i].Ybuf = s->iv_frame[i].Ubuf =
169 deabd4fd Mike Melanson
      s->iv_frame[i].Vbuf = NULL;
170
    s->iv_frame[i].the_buf = NULL;
171
    s->iv_frame[i].the_buf_size = 0;
172
    s->iv_frame[i].y_w = s->iv_frame[i].y_h = 0;
173
    s->iv_frame[i].uv_w = s->iv_frame[i].uv_h = 0;
174
  }
175
176
  av_free(s->ModPred);
177
  av_free(s->corrector_type);
178
}
179
180
/* ---------------------------------------------------------------------- */
181 115329f1 Diego Biurrun
static unsigned long iv_decode_frame(Indeo3DecodeContext *s,
182
                                     unsigned char *buf, int buf_size)
183 deabd4fd Mike Melanson
{
184
  unsigned int hdr_width, hdr_height,
185
    chroma_width, chroma_height;
186
  unsigned long fflags1, fflags2, fflags3, offs1, offs2, offs3, offs;
187
  unsigned char *hdr_pos, *buf_pos;
188
189
  buf_pos = buf;
190
  buf_pos += 18;
191
192
  fflags1 = le2me_16(*(uint16_t *)buf_pos);
193
  buf_pos += 2;
194
  fflags3 = le2me_32(*(uint32_t *)buf_pos);
195
  buf_pos += 4;
196
  fflags2 = *buf_pos++;
197
  buf_pos += 3;
198
  hdr_height = le2me_16(*(uint16_t *)buf_pos);
199
  buf_pos += 2;
200
  hdr_width = le2me_16(*(uint16_t *)buf_pos);
201 115329f1 Diego Biurrun
202 0ecca7a4 Michael Niedermayer
  if(avcodec_check_dimensions(NULL, hdr_width, hdr_height))
203
      return -1;
204 115329f1 Diego Biurrun
205 deabd4fd Mike Melanson
  buf_pos += 2;
206
  chroma_height = ((hdr_height >> 2) + 3) & 0x7ffc;
207
  chroma_width = ((hdr_width >> 2) + 3) & 0x7ffc;
208
  offs1 = le2me_32(*(uint32_t *)buf_pos);
209
  buf_pos += 4;
210
  offs2 = le2me_32(*(uint32_t *)buf_pos);
211
  buf_pos += 4;
212
  offs3 = le2me_32(*(uint32_t *)buf_pos);
213
  buf_pos += 8;
214
  hdr_pos = buf_pos;
215
  if(fflags3 == 0x80) return 4;
216
217
  if(fflags1 & 0x200) {
218
    s->cur_frame = s->iv_frame + 1;
219
    s->ref_frame = s->iv_frame;
220
  } else {
221
    s->cur_frame = s->iv_frame;
222
    s->ref_frame = s->iv_frame + 1;
223
  }
224
225
  buf_pos = buf + 16 + offs1;
226
  offs = le2me_32(*(uint32_t *)buf_pos);
227
  buf_pos += 4;
228
229 115329f1 Diego Biurrun
  iv_Decode_Chunk(s, s->cur_frame->Ybuf, s->ref_frame->Ybuf, hdr_width,
230
    hdr_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos,
231 deabd4fd Mike Melanson
    min(hdr_width, 160));
232
233 f970a8e6 Alex Beregszaszi
  if (!(s->avctx->flags & CODEC_FLAG_GRAY))
234
  {
235
236 deabd4fd Mike Melanson
  buf_pos = buf + 16 + offs2;
237
  offs = le2me_32(*(uint32_t *)buf_pos);
238
  buf_pos += 4;
239
240 115329f1 Diego Biurrun
  iv_Decode_Chunk(s, s->cur_frame->Vbuf, s->ref_frame->Vbuf, chroma_width,
241
    chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos,
242 deabd4fd Mike Melanson
    min(chroma_width, 40));
243
244
  buf_pos = buf + 16 + offs3;
245
  offs = le2me_32(*(uint32_t *)buf_pos);
246
  buf_pos += 4;
247
248 115329f1 Diego Biurrun
  iv_Decode_Chunk(s, s->cur_frame->Ubuf, s->ref_frame->Ubuf, chroma_width,
249
    chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos,
250 deabd4fd Mike Melanson
    min(chroma_width, 40));
251
252 f970a8e6 Alex Beregszaszi
  }
253
254 deabd4fd Mike Melanson
  return 8;
255
}
256
257
typedef struct {
258
  long xpos;
259
  long ypos;
260
  long width;
261
  long height;
262
  long split_flag;
263
  long split_direction;
264
  long usl7;
265
} ustr_t;
266
267
/* ---------------------------------------------------------------------- */
268
269 a44f8ccb Mike Melanson
#define LV1_CHECK(buf1,rle_v3,lv1,lp2)  \
270
  if((lv1 & 0x80) != 0) {   \
271
    if(rle_v3 != 0)         \
272
      rle_v3 = 0;           \
273
    else {                  \
274
      rle_v3 = 1;           \
275
      buf1 -= 2;            \
276
    }                       \
277
  }                         \
278
  lp2 = 4;
279
280
281
#define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)  \
282
  if(rle_v3 == 0) {         \
283
    rle_v2 = *buf1;         \
284
    rle_v1 = 1;             \
285
    if(rle_v2 > 32) {       \
286
      rle_v2 -= 32;         \
287
      rle_v1 = 0;           \
288
    }                       \
289
    rle_v3 = 1;             \
290
  }                         \
291
  buf1--;
292
293
294
#define LP2_CHECK(buf1,rle_v3,lp2)  \
295
  if(lp2 == 0 && rle_v3 != 0)     \
296
    rle_v3 = 0;           \
297
  else {                  \
298
    buf1--;               \
299
    rle_v3 = 1;           \
300
  }
301
302
303
#define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
304
  rle_v2--;             \
305
  if(rle_v2 == 0) {     \
306
    rle_v3 = 0;         \
307
    buf1 += 2;          \
308
  }                     \
309
  lp2 = 4;
310
311 deabd4fd Mike Melanson
static void iv_Decode_Chunk(Indeo3DecodeContext *s,
312 115329f1 Diego Biurrun
  unsigned char *cur, unsigned char *ref, int width, int height,
313 deabd4fd Mike Melanson
  unsigned char *buf1, long fflags2, unsigned char *hdr,
314
  unsigned char *buf2, int min_width_160)
315
{
316
  unsigned char bit_buf;
317
  unsigned long bit_pos, lv, lv1, lv2;
318
  long *width_tbl, width_tbl_arr[10];
319 cdb2c1ca Sebastian Jedruszkiewicz
  signed char *ref_vectors;
320 deabd4fd Mike Melanson
  unsigned char *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
321 09b85c13 Fabrice Bellard
  uint32_t *cur_lp, *ref_lp;
322
  const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
323 deabd4fd Mike Melanson
  unsigned short *correction_type_sp[2];
324 a44f8ccb Mike Melanson
  ustr_t strip_tbl[20], *strip;
325 deabd4fd Mike Melanson
  int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
326
    rle_v1, rle_v2, rle_v3;
327 cdb2c1ca Sebastian Jedruszkiewicz
  unsigned short res;
328 deabd4fd Mike Melanson
329
  bit_buf = 0;
330
  ref_vectors = NULL;
331
332
  width_tbl = width_tbl_arr + 1;
333
  i = (width < 0 ? width + 3 : width)/4;
334 115329f1 Diego Biurrun
  for(j = -1; j < 8; j++)
335 deabd4fd Mike Melanson
    width_tbl[j] = i * j;
336
337 a44f8ccb Mike Melanson
  strip = strip_tbl;
338 deabd4fd Mike Melanson
339
  for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
340
341 a44f8ccb Mike Melanson
  strip->ypos = strip->xpos = 0;
342
  for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
343
  strip->height = height;
344
  strip->split_direction = 0;
345
  strip->split_flag = 0;
346
  strip->usl7 = 0;
347 deabd4fd Mike Melanson
348
  bit_pos = 0;
349
350
  rle_v1 = rle_v2 = rle_v3 = 0;
351
352 a44f8ccb Mike Melanson
  while(strip >= strip_tbl) {
353 deabd4fd Mike Melanson
    if(bit_pos <= 0) {
354
      bit_pos = 8;
355
      bit_buf = *buf1++;
356
    }
357
358
    bit_pos -= 2;
359
    cmd = (bit_buf >> bit_pos) & 0x03;
360
361
    if(cmd == 0) {
362 a44f8ccb Mike Melanson
      strip++;
363
      memcpy(strip, strip-1, sizeof(ustr_t));
364
      strip->split_flag = 1;
365
      strip->split_direction = 0;
366
      strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);
367 deabd4fd Mike Melanson
      continue;
368
    } else if(cmd == 1) {
369 a44f8ccb Mike Melanson
      strip++;
370
      memcpy(strip, strip-1, sizeof(ustr_t));
371
      strip->split_flag = 1;
372
      strip->split_direction = 1;
373
      strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);
374 deabd4fd Mike Melanson
      continue;
375
    } else if(cmd == 2) {
376 a44f8ccb Mike Melanson
      if(strip->usl7 == 0) {
377
        strip->usl7 = 1;
378 deabd4fd Mike Melanson
        ref_vectors = NULL;
379
        continue;
380
      }
381
    } else if(cmd == 3) {
382 a44f8ccb Mike Melanson
      if(strip->usl7 == 0) {
383
        strip->usl7 = 1;
384 deabd4fd Mike Melanson
        ref_vectors = buf2 + (*buf1 * 2);
385
        buf1++;
386
        continue;
387
      }
388
    }
389
390 a44f8ccb Mike Melanson
    cur_frm_pos = cur + width * strip->ypos + strip->xpos;
391 deabd4fd Mike Melanson
392 115329f1 Diego Biurrun
    if((blks_width = strip->width) < 0)
393 deabd4fd Mike Melanson
      blks_width += 3;
394
    blks_width >>= 2;
395 a44f8ccb Mike Melanson
    blks_height = strip->height;
396 deabd4fd Mike Melanson
397
    if(ref_vectors != NULL) {
398 a44f8ccb Mike Melanson
      ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
399
        ref_vectors[1] + strip->xpos;
400 115329f1 Diego Biurrun
    } else
401 deabd4fd Mike Melanson
      ref_frm_pos = cur_frm_pos - width_tbl[4];
402
403
    if(cmd == 2) {
404
      if(bit_pos <= 0) {
405
        bit_pos = 8;
406
        bit_buf = *buf1++;
407
      }
408
409
      bit_pos -= 2;
410
      cmd = (bit_buf >> bit_pos) & 0x03;
411
412
      if(cmd == 0 || ref_vectors != NULL) {
413
        for(lp1 = 0; lp1 < blks_width; lp1++) {
414
          for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
415 09b85c13 Fabrice Bellard
            ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
416 deabd4fd Mike Melanson
          cur_frm_pos += 4;
417
          ref_frm_pos += 4;
418
        }
419 115329f1 Diego Biurrun
      } else if(cmd != 1)
420 deabd4fd Mike Melanson
        return;
421
    } else {
422
      k = *buf1 >> 4;
423
      j = *buf1 & 0x0f;
424
      buf1++;
425
      lv = j + fflags2;
426
427
      if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
428
        cp2 = s->ModPred + ((lv - 8) << 7);
429
        cp = ref_frm_pos;
430 115329f1 Diego Biurrun
        for(i = 0; i < blks_width << 2; i++) {
431 09b85c13 Fabrice Bellard
            int v = *cp >> 1;
432 115329f1 Diego Biurrun
            *(cp++) = cp2[v];
433 09b85c13 Fabrice Bellard
        }
434 deabd4fd Mike Melanson
      }
435
436
      if(k == 1 || k == 4) {
437
        lv = (hdr[j] & 0xf) + fflags2;
438
        correction_type_sp[0] = s->corrector_type + (lv << 8);
439
        correction_lp[0] = correction + (lv << 8);
440
        lv = (hdr[j] >> 4) + fflags2;
441
        correction_lp[1] = correction + (lv << 8);
442
        correction_type_sp[1] = s->corrector_type + (lv << 8);
443
      } else {
444
        correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);
445
        correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);
446
        correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);
447
        correction_lp[0] = correction_lp[1] = correction + (lv << 8);
448
      }
449
450
      switch(k) {
451
        case 1:
452
        case 0:                    /********** CASE 0 **********/
453
          for( ; blks_height > 0; blks_height -= 4) {
454
            for(lp1 = 0; lp1 < blks_width; lp1++) {
455
              for(lp2 = 0; lp2 < 4; ) {
456
                k = *buf1++;
457 09b85c13 Fabrice Bellard
                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
458
                ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
459 deabd4fd Mike Melanson
460
                switch(correction_type_sp[0][k]) {
461
                  case 0:
462 cdb2c1ca Sebastian Jedruszkiewicz
                    *cur_lp = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
463 deabd4fd Mike Melanson
                    lp2++;
464
                    break;
465
                  case 1:
466 9e42e70c Michael Niedermayer
                    res = ((le2me_16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
467 cdb2c1ca Sebastian Jedruszkiewicz
                    ((unsigned short *)cur_lp)[0] = le2me_16(res);
468
                    res = ((le2me_16(((unsigned short *)(ref_lp))[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
469
                    ((unsigned short *)cur_lp)[1] = le2me_16(res);
470 9e42e70c Michael Niedermayer
                    buf1++;
471 deabd4fd Mike Melanson
                    lp2++;
472
                    break;
473
                  case 2:
474
                    if(lp2 == 0) {
475
                      for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
476
                        cur_lp[j] = ref_lp[j];
477
                      lp2 += 2;
478
                    }
479
                    break;
480
                  case 3:
481
                    if(lp2 < 2) {
482
                      for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
483
                        cur_lp[j] = ref_lp[j];
484
                      lp2 = 3;
485
                    }
486
                    break;
487
                  case 8:
488
                    if(lp2 == 0) {
489 a44f8ccb Mike Melanson
                      RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
490 deabd4fd Mike Melanson
491
                      if(rle_v1 == 1 || ref_vectors != NULL) {
492
                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
493
                          cur_lp[j] = ref_lp[j];
494
                      }
495
496 a44f8ccb Mike Melanson
                      RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
497 deabd4fd Mike Melanson
                      break;
498
                    } else {
499
                      rle_v1 = 1;
500
                      rle_v2 = *buf1 - 1;
501
                    }
502
                  case 5:
503 a44f8ccb Mike Melanson
                      LP2_CHECK(buf1,rle_v3,lp2)
504 deabd4fd Mike Melanson
                  case 4:
505
                    for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
506
                      cur_lp[j] = ref_lp[j];
507
                    lp2 = 4;
508
                    break;
509
510
                  case 7:
511 115329f1 Diego Biurrun
                    if(rle_v3 != 0)
512 deabd4fd Mike Melanson
                      rle_v3 = 0;
513
                    else {
514
                      buf1--;
515
                      rle_v3 = 1;
516
                    }
517
                  case 6:
518
                    if(ref_vectors != NULL) {
519
                      for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
520
                        cur_lp[j] = ref_lp[j];
521
                    }
522
                    lp2 = 4;
523
                    break;
524
525
                  case 9:
526
                    lv1 = *buf1++;
527
                    lv = (lv1 & 0x7F) << 1;
528
                    lv += (lv << 8);
529
                    lv += (lv << 16);
530
                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
531
                      cur_lp[j] = lv;
532
533 a44f8ccb Mike Melanson
                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
534 deabd4fd Mike Melanson
                    break;
535 115329f1 Diego Biurrun
                  default:
536 deabd4fd Mike Melanson
                    return;
537
                }
538
              }
539
540
              cur_frm_pos += 4;
541
              ref_frm_pos += 4;
542
            }
543
544
            cur_frm_pos += ((width - blks_width) * 4);
545
            ref_frm_pos += ((width - blks_width) * 4);
546
          }
547
          break;
548
549
        case 4:
550
        case 3:                    /********** CASE 3 **********/
551 115329f1 Diego Biurrun
          if(ref_vectors != NULL)
552 deabd4fd Mike Melanson
            return;
553
          flag1 = 1;
554
555
          for( ; blks_height > 0; blks_height -= 8) {
556
            for(lp1 = 0; lp1 < blks_width; lp1++) {
557
              for(lp2 = 0; lp2 < 4; ) {
558
                k = *buf1++;
559
560 09b85c13 Fabrice Bellard
                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
561
                ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
562 deabd4fd Mike Melanson
563
                switch(correction_type_sp[lp2 & 0x01][k]) {
564
                  case 0:
565 cdb2c1ca Sebastian Jedruszkiewicz
                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
566 a44f8ccb Mike Melanson
                    if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
567 deabd4fd Mike Melanson
                      cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
568
                    else
569 cdb2c1ca Sebastian Jedruszkiewicz
                      cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
570 deabd4fd Mike Melanson
                    lp2++;
571
                    break;
572
573
                  case 1:
574 9e42e70c Michael Niedermayer
                    res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
575 cdb2c1ca Sebastian Jedruszkiewicz
                    ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
576
                    res = ((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
577
                    ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
578
579 a44f8ccb Mike Melanson
                    if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
580 deabd4fd Mike Melanson
                      cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
581
                    else
582
                      cur_lp[0] = cur_lp[width_tbl[1]];
583 9e42e70c Michael Niedermayer
                    buf1++;
584 deabd4fd Mike Melanson
                    lp2++;
585
                    break;
586
587
                  case 2:
588
                    if(lp2 == 0) {
589
                      for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
590
                        cur_lp[j] = *ref_lp;
591
                      lp2 += 2;
592
                    }
593
                    break;
594
595
                  case 3:
596
                    if(lp2 < 2) {
597
                      for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
598
                        cur_lp[j] = *ref_lp;
599
                      lp2 = 3;
600
                    }
601
                    break;
602
603
                  case 6:
604
                    lp2 = 4;
605
                    break;
606
607
                  case 7:
608 115329f1 Diego Biurrun
                    if(rle_v3 != 0)
609 deabd4fd Mike Melanson
                      rle_v3 = 0;
610
                    else {
611
                      buf1--;
612
                      rle_v3 = 1;
613
                    }
614
                    lp2 = 4;
615
                    break;
616
617
                  case 8:
618
                    if(lp2 == 0) {
619 a44f8ccb Mike Melanson
                      RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
620 deabd4fd Mike Melanson
621
                      if(rle_v1 == 1) {
622
                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
623
                          cur_lp[j] = ref_lp[j];
624
                      }
625
626 a44f8ccb Mike Melanson
                      RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
627 deabd4fd Mike Melanson
                      break;
628
                    } else {
629
                      rle_v2 = (*buf1) - 1;
630
                      rle_v1 = 1;
631
                    }
632
                  case 5:
633 a44f8ccb Mike Melanson
                      LP2_CHECK(buf1,rle_v3,lp2)
634 deabd4fd Mike Melanson
                  case 4:
635
                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
636
                      cur_lp[j] = *ref_lp;
637
                    lp2 = 4;
638
                    break;
639
640
                  case 9:
641 9b879566 Michel Bardiaux
                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
642 deabd4fd Mike Melanson
                    lv1 = *buf1++;
643
                    lv = (lv1 & 0x7F) << 1;
644
                    lv += (lv << 8);
645
                    lv += (lv << 16);
646
647
                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
648
                      cur_lp[j] = lv;
649
650 a44f8ccb Mike Melanson
                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
651 deabd4fd Mike Melanson
                    break;
652
653 115329f1 Diego Biurrun
                  default:
654 deabd4fd Mike Melanson
                    return;
655
                }
656
              }
657
658
              cur_frm_pos += 4;
659
            }
660
661
            cur_frm_pos += (((width * 2) - blks_width) * 4);
662
            flag1 = 0;
663
          }
664
          break;
665
666
        case 10:                    /********** CASE 10 **********/
667
          if(ref_vectors == NULL) {
668
            flag1 = 1;
669
670
            for( ; blks_height > 0; blks_height -= 8) {
671
              for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
672
                for(lp2 = 0; lp2 < 4; ) {
673
                  k = *buf1++;
674 09b85c13 Fabrice Bellard
                  cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
675
                  ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
676 deabd4fd Mike Melanson
                  lv1 = ref_lp[0];
677
                  lv2 = ref_lp[1];
678
                  if(lp2 == 0 && flag1 != 0) {
679 cdb2c1ca Sebastian Jedruszkiewicz
#ifdef WORDS_BIGENDIAN
680
                    lv1 = lv1 & 0xFF00FF00;
681
                    lv1 = (lv1 >> 8) | lv1;
682
                    lv2 = lv2 & 0xFF00FF00;
683
                    lv2 = (lv2 >> 8) | lv2;
684
#else
685 deabd4fd Mike Melanson
                    lv1 = lv1 & 0x00FF00FF;
686
                    lv1 = (lv1 << 8) | lv1;
687
                    lv2 = lv2 & 0x00FF00FF;
688
                    lv2 = (lv2 << 8) | lv2;
689 cdb2c1ca Sebastian Jedruszkiewicz
#endif
690 deabd4fd Mike Melanson
                  }
691
692
                  switch(correction_type_sp[lp2 & 0x01][k]) {
693
                    case 0:
694 cdb2c1ca Sebastian Jedruszkiewicz
                      cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
695
                      cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1);
696 a44f8ccb Mike Melanson
                      if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
697 deabd4fd Mike Melanson
                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
698
                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
699
                      } else {
700
                        cur_lp[0] = cur_lp[width_tbl[1]];
701
                        cur_lp[1] = cur_lp[width_tbl[1]+1];
702
                      }
703
                      lp2++;
704
                      break;
705
706
                    case 1:
707 9e42e70c Michael Niedermayer
                      cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1);
708 cdb2c1ca Sebastian Jedruszkiewicz
                      cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
709 a44f8ccb Mike Melanson
                      if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
710 deabd4fd Mike Melanson
                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
711
                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
712
                      } else {
713
                        cur_lp[0] = cur_lp[width_tbl[1]];
714
                        cur_lp[1] = cur_lp[width_tbl[1]+1];
715
                      }
716 9e42e70c Michael Niedermayer
                      buf1++;
717 deabd4fd Mike Melanson
                      lp2++;
718
                      break;
719
720
                    case 2:
721
                      if(lp2 == 0) {
722
                        if(flag1 != 0) {
723
                          for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
724
                            cur_lp[j] = lv1;
725
                            cur_lp[j+1] = lv2;
726
                          }
727
                          cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
728
                          cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
729
                        } else {
730
                          for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
731
                            cur_lp[j] = lv1;
732
                            cur_lp[j+1] = lv2;
733
                          }
734
                        }
735
                        lp2 += 2;
736
                      }
737
                      break;
738
739
                    case 3:
740
                      if(lp2 < 2) {
741
                        if(lp2 == 0 && flag1 != 0) {
742
                          for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
743
                            cur_lp[j] = lv1;
744
                            cur_lp[j+1] = lv2;
745
                          }
746
                          cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
747
                          cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
748
                        } else {
749
                          for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
750
                            cur_lp[j] = lv1;
751
                            cur_lp[j+1] = lv2;
752
                          }
753
                        }
754
                        lp2 = 3;
755
                      }
756
                      break;
757
758
                    case 8:
759
                      if(lp2 == 0) {
760 a44f8ccb Mike Melanson
                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
761 deabd4fd Mike Melanson
                        if(rle_v1 == 1) {
762
                          if(flag1 != 0) {
763
                            for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
764
                              cur_lp[j] = lv1;
765
                              cur_lp[j+1] = lv2;
766
                            }
767
                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
768
                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
769
                          } else {
770
                            for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
771
                              cur_lp[j] = lv1;
772
                              cur_lp[j+1] = lv2;
773
                            }
774
                          }
775
                        }
776 a44f8ccb Mike Melanson
                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
777 deabd4fd Mike Melanson
                        break;
778
                      } else {
779
                        rle_v1 = 1;
780
                        rle_v2 = (*buf1) - 1;
781
                      }
782
                    case 5:
783 a44f8ccb Mike Melanson
                        LP2_CHECK(buf1,rle_v3,lp2)
784 deabd4fd Mike Melanson
                    case 4:
785
                      if(lp2 == 0 && flag1 != 0) {
786
                        for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
787
                          cur_lp[j] = lv1;
788
                          cur_lp[j+1] = lv2;
789
                        }
790
                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
791
                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
792
                      } else {
793
                        for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
794
                          cur_lp[j] = lv1;
795
                          cur_lp[j+1] = lv2;
796
                        }
797
                      }
798
                      lp2 = 4;
799
                      break;
800
801
                    case 6:
802
                      lp2 = 4;
803
                      break;
804
805
                    case 7:
806
                      if(lp2 == 0) {
807 115329f1 Diego Biurrun
                        if(rle_v3 != 0)
808 deabd4fd Mike Melanson
                          rle_v3 = 0;
809
                        else {
810
                          buf1--;
811
                          rle_v3 = 1;
812
                        }
813
                        lp2 = 4;
814
                      }
815
                      break;
816
817
                    case 9:
818 9b879566 Michel Bardiaux
                      av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
819 deabd4fd Mike Melanson
                      lv1 = *buf1;
820
                      lv = (lv1 & 0x7F) << 1;
821
                      lv += (lv << 8);
822
                      lv += (lv << 16);
823
                      for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
824
                        cur_lp[j] = lv;
825 a44f8ccb Mike Melanson
                      LV1_CHECK(buf1,rle_v3,lv1,lp2)
826 deabd4fd Mike Melanson
                      break;
827
828 115329f1 Diego Biurrun
                    default:
829 deabd4fd Mike Melanson
                      return;
830
                  }
831
                }
832
833
                cur_frm_pos += 8;
834
              }
835
836
              cur_frm_pos += (((width * 2) - blks_width) * 4);
837
              flag1 = 0;
838
            }
839
          } else {
840
            for( ; blks_height > 0; blks_height -= 8) {
841
              for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
842
                for(lp2 = 0; lp2 < 4; ) {
843
                  k = *buf1++;
844 09b85c13 Fabrice Bellard
                  cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
845
                  ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
846 deabd4fd Mike Melanson
847
                  switch(correction_type_sp[lp2 & 0x01][k]) {
848
                    case 0:
849
                      lv1 = correctionloworder_lp[lp2 & 0x01][k];
850
                      lv2 = correctionhighorder_lp[lp2 & 0x01][k];
851 cdb2c1ca Sebastian Jedruszkiewicz
                      cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
852
                      cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
853
                      cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
854
                      cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
855 deabd4fd Mike Melanson
                      lp2++;
856
                      break;
857
858
                    case 1:
859
                      lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
860
                      lv2 = correctionloworder_lp[lp2 & 0x01][k];
861 cdb2c1ca Sebastian Jedruszkiewicz
                      cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
862
                      cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
863
                      cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
864
                      cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
865 deabd4fd Mike Melanson
                      lp2++;
866
                      break;
867
868
                    case 2:
869
                      if(lp2 == 0) {
870
                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
871
                          cur_lp[j] = ref_lp[j];
872
                          cur_lp[j+1] = ref_lp[j+1];
873
                        }
874
                        lp2 += 2;
875
                      }
876
                      break;
877
878
                    case 3:
879
                      if(lp2 < 2) {
880
                        for(i = 0, j = 0; i < 6 - (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 = 3;
885
                      }
886
                      break;
887
888
                    case 8:
889
                      if(lp2 == 0) {
890 a44f8ccb Mike Melanson
                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
891 deabd4fd Mike Melanson
                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
892 09b85c13 Fabrice Bellard
                          ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
893
                          ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];
894 deabd4fd Mike Melanson
                        }
895 a44f8ccb Mike Melanson
                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
896 deabd4fd Mike Melanson
                        break;
897
                      } else {
898
                        rle_v1 = 1;
899
                        rle_v2 = (*buf1) - 1;
900
                      }
901
                    case 5:
902
                    case 7:
903 a44f8ccb Mike Melanson
                        LP2_CHECK(buf1,rle_v3,lp2)
904 deabd4fd Mike Melanson
                    case 6:
905
                    case 4:
906
                      for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
907
                        cur_lp[j] = ref_lp[j];
908
                        cur_lp[j+1] = ref_lp[j+1];
909
                      }
910
                      lp2 = 4;
911
                      break;
912
913
                    case 9:
914 9b879566 Michel Bardiaux
                      av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
915 deabd4fd Mike Melanson
                      lv1 = *buf1;
916
                      lv = (lv1 & 0x7F) << 1;
917
                      lv += (lv << 8);
918
                      lv += (lv << 16);
919
                      for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
920 09b85c13 Fabrice Bellard
                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
921 a44f8ccb Mike Melanson
                      LV1_CHECK(buf1,rle_v3,lv1,lp2)
922 deabd4fd Mike Melanson
                      break;
923
924 115329f1 Diego Biurrun
                    default:
925 deabd4fd Mike Melanson
                      return;
926
                  }
927
                }
928
929
                cur_frm_pos += 8;
930
                ref_frm_pos += 8;
931
              }
932
933
              cur_frm_pos += (((width * 2) - blks_width) * 4);
934
              ref_frm_pos += (((width * 2) - blks_width) * 4);
935
            }
936
          }
937
          break;
938
939
        case 11:                    /********** CASE 11 **********/
940 115329f1 Diego Biurrun
          if(ref_vectors == NULL)
941 deabd4fd Mike Melanson
            return;
942
943
          for( ; blks_height > 0; blks_height -= 8) {
944
            for(lp1 = 0; lp1 < blks_width; lp1++) {
945
              for(lp2 = 0; lp2 < 4; ) {
946
                k = *buf1++;
947 09b85c13 Fabrice Bellard
                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
948
                ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
949 deabd4fd Mike Melanson
950
                switch(correction_type_sp[lp2 & 0x01][k]) {
951
                  case 0:
952 cdb2c1ca Sebastian Jedruszkiewicz
                    cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
953
                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
954 deabd4fd Mike Melanson
                    lp2++;
955
                    break;
956
957
                  case 1:
958
                    lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
959
                    lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
960 cdb2c1ca Sebastian Jedruszkiewicz
                    res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1);
961
                    ((unsigned short *)cur_lp)[0] = le2me_16(res);
962
                    res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1);
963
                    ((unsigned short *)cur_lp)[1] = le2me_16(res);
964
                    res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1);
965
                    ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
966
                    res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1);
967
                    ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
968 deabd4fd Mike Melanson
                    lp2++;
969
                    break;
970
971
                  case 2:
972
                    if(lp2 == 0) {
973
                      for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
974
                        cur_lp[j] = ref_lp[j];
975
                      lp2 += 2;
976
                    }
977
                    break;
978
979
                  case 3:
980
                    if(lp2 < 2) {
981
                      for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
982
                        cur_lp[j] = ref_lp[j];
983
                      lp2 = 3;
984
                    }
985
                    break;
986
987
                  case 8:
988
                    if(lp2 == 0) {
989 a44f8ccb Mike Melanson
                      RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
990 deabd4fd Mike Melanson
991
                      for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
992
                        cur_lp[j] = ref_lp[j];
993
994 a44f8ccb Mike Melanson
                      RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
995 deabd4fd Mike Melanson
                      break;
996
                    } else {
997
                      rle_v1 = 1;
998
                      rle_v2 = (*buf1) - 1;
999
                    }
1000
                  case 5:
1001
                  case 7:
1002 a44f8ccb Mike Melanson
                      LP2_CHECK(buf1,rle_v3,lp2)
1003 deabd4fd Mike Melanson
                  case 4:
1004
                  case 6:
1005
                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
1006
                      cur_lp[j] = ref_lp[j];
1007
                    lp2 = 4;
1008
                    break;
1009
1010
                case 9:
1011 9b879566 Michel Bardiaux
                  av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
1012 deabd4fd Mike Melanson
                  lv1 = *buf1++;
1013
                  lv = (lv1 & 0x7F) << 1;
1014
                  lv += (lv << 8);
1015
                  lv += (lv << 16);
1016
                  for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
1017
                    cur_lp[j] = lv;
1018 a44f8ccb Mike Melanson
                  LV1_CHECK(buf1,rle_v3,lv1,lp2)
1019 deabd4fd Mike Melanson
                  break;
1020
1021 115329f1 Diego Biurrun
                  default:
1022 deabd4fd Mike Melanson
                    return;
1023
                }
1024
              }
1025
1026
              cur_frm_pos += 4;
1027
              ref_frm_pos += 4;
1028
            }
1029
1030
            cur_frm_pos += (((width * 2) - blks_width) * 4);
1031
            ref_frm_pos += (((width * 2) - blks_width) * 4);
1032
          }
1033
          break;
1034
1035 115329f1 Diego Biurrun
        default:
1036 deabd4fd Mike Melanson
          return;
1037
      }
1038
    }
1039
1040 115329f1 Diego Biurrun
    if(strip < strip_tbl)
1041 deabd4fd Mike Melanson
      return;
1042
1043 a44f8ccb Mike Melanson
    for( ; strip >= strip_tbl; strip--) {
1044
      if(strip->split_flag != 0) {
1045
        strip->split_flag = 0;
1046
        strip->usl7 = (strip-1)->usl7;
1047 deabd4fd Mike Melanson
1048 a44f8ccb Mike Melanson
        if(strip->split_direction) {
1049
          strip->xpos += strip->width;
1050
          strip->width = (strip-1)->width - strip->width;
1051
          if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
1052
            strip->width = width - strip->xpos;
1053 deabd4fd Mike Melanson
        } else {
1054 a44f8ccb Mike Melanson
          strip->ypos += strip->height;
1055
          strip->height = (strip-1)->height - strip->height;
1056 deabd4fd Mike Melanson
        }
1057
        break;
1058
      }
1059
    }
1060
  }
1061
}
1062
1063
static int indeo3_decode_init(AVCodecContext *avctx)
1064
{
1065
    Indeo3DecodeContext *s = avctx->priv_data;
1066
1067
    s->avctx = avctx;
1068
    s->width = avctx->width;
1069
    s->height = avctx->height;
1070
    avctx->pix_fmt = PIX_FMT_YUV410P;
1071
    avctx->has_b_frames = 0;
1072
1073
    build_modpred(s);
1074
    iv_alloc_frames(s);
1075
1076
    return 0;
1077
}
1078
1079
static int indeo3_decode_frame(AVCodecContext *avctx,
1080
                               void *data, int *data_size,
1081
                               unsigned char *buf, int buf_size)
1082
{
1083
    Indeo3DecodeContext *s=avctx->priv_data;
1084
    unsigned char *src, *dest;
1085
    int y;
1086
1087
    iv_decode_frame(s, buf, buf_size);
1088
1089 e20c4069 Michael Niedermayer
    if(s->frame.data[0])
1090
        avctx->release_buffer(avctx, &s->frame);
1091
1092 deabd4fd Mike Melanson
    s->frame.reference = 0;
1093
    if(avctx->get_buffer(avctx, &s->frame) < 0) {
1094 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1095 deabd4fd Mike Melanson
        return -1;
1096
    }
1097
1098
    src = s->cur_frame->Ybuf;
1099
    dest = s->frame.data[0];
1100
    for (y = 0; y < s->height; y++) {
1101
      memcpy(dest, src, s->cur_frame->y_w);
1102
      src += s->cur_frame->y_w;
1103
      dest += s->frame.linesize[0];
1104
    }
1105
1106 f970a8e6 Alex Beregszaszi
    if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1107
    {
1108 deabd4fd Mike Melanson
    src = s->cur_frame->Ubuf;
1109
    dest = s->frame.data[1];
1110
    for (y = 0; y < s->height / 4; y++) {
1111
      memcpy(dest, src, s->cur_frame->uv_w);
1112
      src += s->cur_frame->uv_w;
1113
      dest += s->frame.linesize[1];
1114
    }
1115
1116
    src = s->cur_frame->Vbuf;
1117
    dest = s->frame.data[2];
1118
    for (y = 0; y < s->height / 4; y++) {
1119
      memcpy(dest, src, s->cur_frame->uv_w);
1120
      src += s->cur_frame->uv_w;
1121
      dest += s->frame.linesize[2];
1122
    }
1123 f970a8e6 Alex Beregszaszi
    }
1124 deabd4fd Mike Melanson
1125
    *data_size=sizeof(AVFrame);
1126
    *(AVFrame*)data= s->frame;
1127
1128
    return buf_size;
1129
}
1130
1131
static int indeo3_decode_end(AVCodecContext *avctx)
1132
{
1133
    Indeo3DecodeContext *s = avctx->priv_data;
1134
1135
    iv_free_func(s);
1136
1137
    return 0;
1138
}
1139
1140
AVCodec indeo3_decoder = {
1141
    "indeo3",
1142
    CODEC_TYPE_VIDEO,
1143
    CODEC_ID_INDEO3,
1144
    sizeof(Indeo3DecodeContext),
1145
    indeo3_decode_init,
1146
    NULL,
1147
    indeo3_decode_end,
1148
    indeo3_decode_frame,
1149
    0,
1150
    NULL
1151
};