indeo3 for bigendian patch by (demo-n <demo-n users.sourceforge net)
[libav.git] / libavcodec / indeo3.c
1 /*
2 * Intel Indeo 3 (IV31, IV32, etc.) video decoder for ffmpeg
3 * written, produced, and directed by Alan Smithee
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "common.h"
26 #include "avcodec.h"
27 #include "dsputil.h"
28 #include "mpegvideo.h"
29
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 static void build_modpred(Indeo3DecodeContext *s)
65 {
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 ? 236 : 2*((i + 2) - ((i + 1) % 3)));
74 s->ModPred[i+2*128] = (i > 125) ? 248 : 2*((i + 2) - ((i + 2) % 4));
75 s->ModPred[i+3*128] = 2*((i + 1) - ((i - 3) % 5));
76 s->ModPred[i+4*128] = (i == 8) ? 20 : 2*((i + 1) - ((i - 3) % 6));
77 s->ModPred[i+5*128] = 2*((i + 4) - ((i + 3) % 7));
78 s->ModPred[i+6*128] = (i > 123) ? 240 : 2*((i + 4) - ((i + 4) % 8));
79 s->ModPred[i+7*128] = 2*((i + 5) - ((i + 4) % 9));
80 }
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 ? 1 : ((j < 248 || (i == 16 && j == 248))
88 ? 0 : corrector_type_2[j - 248]);
89 }
90 }
91 }
92
93 static void iv_Decode_Chunk(Indeo3DecodeContext *s, unsigned char *cur,
94 unsigned char *ref, int width, int height, unsigned char *buf1,
95 long fflags2, unsigned char *hdr,
96 unsigned char *buf2, int min_width_160);
97
98 #ifndef min
99 #define min(a,b) ((a) < (b) ? (a) : (b))
100 #endif
101
102 /* ---------------------------------------------------------------------- */
103 static void iv_alloc_frames(Indeo3DecodeContext *s)
104 {
105 int luma_width, luma_height, luma_pixels, chroma_width, chroma_height,
106 chroma_pixels, i;
107 unsigned int bufsize;
108
109 luma_width = (s->width + 3) & (~3);
110 luma_height = (s->height + 3) & (~3);
111
112 s->iv_frame[0].y_w = s->iv_frame[0].y_h =
113 s->iv_frame[0].the_buf_size = 0;
114 s->iv_frame[1].y_w = s->iv_frame[1].y_h =
115 s->iv_frame[1].the_buf_size = 0;
116 s->iv_frame[1].the_buf = NULL;
117
118 chroma_width = ((luma_width >> 2) + 3) & (~3);
119 chroma_height = ((luma_height>> 2) + 3) & (~3);
120 luma_pixels = luma_width * luma_height;
121 chroma_pixels = chroma_width * chroma_height;
122
123 bufsize = luma_pixels * 2 + luma_width * 3 +
124 (chroma_pixels + chroma_width) * 4;
125
126 if((s->iv_frame[0].the_buf =
127 (s->iv_frame[0].the_buf_size == 0 ? av_malloc(bufsize) :
128 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 s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =
150 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 static void iv_free_func(Indeo3DecodeContext *s)
162 {
163 int i;
164
165 for(i = 0 ; i < 2 ; i++) {
166 if(s->iv_frame[i].the_buf != NULL)
167 av_free(s->iv_frame[i].the_buf);
168 s->iv_frame[i].Ybuf = s->iv_frame[i].Ubuf =
169 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 static unsigned long iv_decode_frame(Indeo3DecodeContext *s,
182 unsigned char *buf, int buf_size)
183 {
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
202 if(avcodec_check_dimensions(NULL, hdr_width, hdr_height))
203 return -1;
204
205 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 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 min(hdr_width, 160));
232
233 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
234 {
235
236 buf_pos = buf + 16 + offs2;
237 offs = le2me_32(*(uint32_t *)buf_pos);
238 buf_pos += 4;
239
240 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 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 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 min(chroma_width, 40));
251
252 }
253
254 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 #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 static void iv_Decode_Chunk(Indeo3DecodeContext *s,
312 unsigned char *cur, unsigned char *ref, int width, int height,
313 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 signed char *ref_vectors;
320 unsigned char *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
321 uint32_t *cur_lp, *ref_lp;
322 const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
323 unsigned short *correction_type_sp[2];
324 ustr_t strip_tbl[20], *strip;
325 int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
326 rle_v1, rle_v2, rle_v3;
327 unsigned short res;
328
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 for(j = -1; j < 8; j++)
335 width_tbl[j] = i * j;
336
337 strip = strip_tbl;
338
339 for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
340
341 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
348 bit_pos = 0;
349
350 rle_v1 = rle_v2 = rle_v3 = 0;
351
352 while(strip >= strip_tbl) {
353 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 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 continue;
368 } else if(cmd == 1) {
369 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 continue;
375 } else if(cmd == 2) {
376 if(strip->usl7 == 0) {
377 strip->usl7 = 1;
378 ref_vectors = NULL;
379 continue;
380 }
381 } else if(cmd == 3) {
382 if(strip->usl7 == 0) {
383 strip->usl7 = 1;
384 ref_vectors = buf2 + (*buf1 * 2);
385 buf1++;
386 continue;
387 }
388 }
389
390 cur_frm_pos = cur + width * strip->ypos + strip->xpos;
391
392 if((blks_width = strip->width) < 0)
393 blks_width += 3;
394 blks_width >>= 2;
395 blks_height = strip->height;
396
397 if(ref_vectors != NULL) {
398 ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
399 ref_vectors[1] + strip->xpos;
400 } else
401 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 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
416 cur_frm_pos += 4;
417 ref_frm_pos += 4;
418 }
419 } else if(cmd != 1)
420 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 for(i = 0; i < blks_width << 2; i++) {
431 int v = *cp >> 1;
432 *(cp++) = cp2[v];
433 }
434 }
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 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
458 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
459
460 switch(correction_type_sp[0][k]) {
461 case 0:
462 *cur_lp = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
463 lp2++;
464 break;
465 case 1:
466 res = ((le2me_16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
467 ((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 buf1++;
471 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 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
490
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 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
497 break;
498 } else {
499 rle_v1 = 1;
500 rle_v2 = *buf1 - 1;
501 }
502 case 5:
503 LP2_CHECK(buf1,rle_v3,lp2)
504 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 if(rle_v3 != 0)
512 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 LV1_CHECK(buf1,rle_v3,lv1,lp2)
534 break;
535 default:
536 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 if(ref_vectors != NULL)
552 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 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
563 switch(correction_type_sp[lp2 & 0x01][k]) {
564 case 0:
565 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
566 if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
567 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
568 else
569 cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
570 lp2++;
571 break;
572
573 case 1:
574 res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
575 ((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 if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
580 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 buf1++;
584 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 if(rle_v3 != 0)
609 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 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
620
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 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
627 break;
628 } else {
629 rle_v2 = (*buf1) - 1;
630 rle_v1 = 1;
631 }
632 case 5:
633 LP2_CHECK(buf1,rle_v3,lp2)
634 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 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
642 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 LV1_CHECK(buf1,rle_v3,lv1,lp2)
651 break;
652
653 default:
654 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 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 lv1 = ref_lp[0];
677 lv2 = ref_lp[1];
678 if(lp2 == 0 && flag1 != 0) {
679 #ifdef WORDS_BIGENDIAN
680 lv1 = lv1 & 0xFF00FF00;
681 lv1 = (lv1 >> 8) | lv1;
682 lv2 = lv2 & 0xFF00FF00;
683 lv2 = (lv2 >> 8) | lv2;
684 #else
685 lv1 = lv1 & 0x00FF00FF;
686 lv1 = (lv1 << 8) | lv1;
687 lv2 = lv2 & 0x00FF00FF;
688 lv2 = (lv2 << 8) | lv2;
689 #endif
690 }
691
692 switch(correction_type_sp[lp2 & 0x01][k]) {
693 case 0:
694 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 if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
697 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 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1);
708 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
709 if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
710 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 buf1++;
717 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 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
761 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 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
777 break;
778 } else {
779 rle_v1 = 1;
780 rle_v2 = (*buf1) - 1;
781 }
782 case 5:
783 LP2_CHECK(buf1,rle_v3,lp2)
784 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 if(rle_v3 != 0)
808 rle_v3 = 0;
809 else {
810 buf1--;
811 rle_v3 = 1;
812 }
813 lp2 = 4;
814 }
815 break;
816
817 case 9:
818 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
819 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 LV1_CHECK(buf1,rle_v3,lv1,lp2)
826 break;
827
828 default:
829 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 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
845 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
846
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 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 lp2++;
856 break;
857
858 case 1:
859 lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
860 lv2 = correctionloworder_lp[lp2 & 0x01][k];
861 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 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 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
891 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
892 ((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 }
895 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
896 break;
897 } else {
898 rle_v1 = 1;
899 rle_v2 = (*buf1) - 1;
900 }
901 case 5:
902 case 7:
903 LP2_CHECK(buf1,rle_v3,lp2)
904 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 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
915 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 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
921 LV1_CHECK(buf1,rle_v3,lv1,lp2)
922 break;
923
924 default:
925 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 if(ref_vectors == NULL)
941 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 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
948 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
949
950 switch(correction_type_sp[lp2 & 0x01][k]) {
951 case 0:
952 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 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 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 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 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
990
991 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
992 cur_lp[j] = ref_lp[j];
993
994 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
995 break;
996 } else {
997 rle_v1 = 1;
998 rle_v2 = (*buf1) - 1;
999 }
1000 case 5:
1001 case 7:
1002 LP2_CHECK(buf1,rle_v3,lp2)
1003 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 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
1012 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 LV1_CHECK(buf1,rle_v3,lv1,lp2)
1019 break;
1020
1021 default:
1022 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 default:
1036 return;
1037 }
1038 }
1039
1040 if(strip < strip_tbl)
1041 return;
1042
1043 for( ; strip >= strip_tbl; strip--) {
1044 if(strip->split_flag != 0) {
1045 strip->split_flag = 0;
1046 strip->usl7 = (strip-1)->usl7;
1047
1048 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 } else {
1054 strip->ypos += strip->height;
1055 strip->height = (strip-1)->height - strip->height;
1056 }
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 if(s->frame.data[0])
1090 avctx->release_buffer(avctx, &s->frame);
1091
1092 s->frame.reference = 0;
1093 if(avctx->get_buffer(avctx, &s->frame) < 0) {
1094 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1095 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 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1107 {
1108 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 }
1124
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 };