Statistics
| Branch: | Revision:

ffmpeg / libavcodec / indeo3.c @ ad1ea1f1

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