Add undefine macro.
[plumiferos.git] / extern / ffmpeg / libavcodec / indeo3.c
blob90eb37150a04d63145905ae16d34051539704ea9
1 /*
2 * Intel Indeo 3 (IV31, IV32, etc.) video decoder for ffmpeg
3 * written, produced, and directed by Alan Smithee
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "common.h"
26 #include "avcodec.h"
27 #include "dsputil.h"
28 #include "mpegvideo.h"
30 #include "indeo3data.h"
32 typedef struct
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;
43 typedef struct Indeo3DecodeContext {
44 AVCodecContext *avctx;
45 int width, height;
46 AVFrame frame;
48 YUVBufs iv_frame[2];
49 YUVBufs *cur_frame;
50 YUVBufs *ref_frame;
52 unsigned char *ModPred;
53 unsigned short *corrector_type;
54 } Indeo3DecodeContext;
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
62 static int corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
64 static void build_modpred(Indeo3DecodeContext *s)
66 int i, j;
68 s->ModPred = (unsigned char *) av_malloc (8 * 128);
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));
82 s->corrector_type = (unsigned short *) av_malloc (24 * 256 * sizeof(unsigned short));
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]);
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);
98 #ifndef min
99 #define min(a,b) ((a) < (b) ? (a) : (b))
100 #endif
102 /* ---------------------------------------------------------------------- */
103 static void iv_alloc_frames(Indeo3DecodeContext *s)
105 int luma_width, luma_height, luma_pixels, chroma_width, chroma_height,
106 chroma_pixels, i;
107 unsigned int bufsize;
109 luma_width = (s->width + 3) & (~3);
110 luma_height = (s->height + 3) & (~3);
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;
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;
123 bufsize = luma_pixels * 2 + luma_width * 3 +
124 (chroma_pixels + chroma_width) * 4;
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;
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;
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;
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;
160 /* ---------------------------------------------------------------------- */
161 static void iv_free_func(Indeo3DecodeContext *s)
163 int i;
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;
176 av_free(s->ModPred);
177 av_free(s->corrector_type);
180 /* ---------------------------------------------------------------------- */
181 static unsigned long iv_decode_frame(Indeo3DecodeContext *s,
182 unsigned char *buf, int buf_size)
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;
189 buf_pos = buf;
190 buf_pos += 18;
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);
202 if(avcodec_check_dimensions(NULL, hdr_width, hdr_height))
203 return -1;
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;
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;
225 buf_pos = buf + 16 + offs1;
226 offs = le2me_32(*(uint32_t *)buf_pos);
227 buf_pos += 4;
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));
233 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
236 buf_pos = buf + 16 + offs2;
237 offs = le2me_32(*(uint32_t *)buf_pos);
238 buf_pos += 4;
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));
244 buf_pos = buf + 16 + offs3;
245 offs = le2me_32(*(uint32_t *)buf_pos);
246 buf_pos += 4;
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));
254 return 8;
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;
267 /* ---------------------------------------------------------------------- */
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; \
278 lp2 = 4;
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; \
289 rle_v3 = 1; \
291 buf1--;
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; \
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; \
309 lp2 = 4;
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)
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;
329 bit_buf = 0;
330 ref_vectors = NULL;
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;
337 strip = strip_tbl;
339 for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
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;
348 bit_pos = 0;
350 rle_v1 = rle_v2 = rle_v3 = 0;
352 while(strip >= strip_tbl) {
353 if(bit_pos <= 0) {
354 bit_pos = 8;
355 bit_buf = *buf1++;
358 bit_pos -= 2;
359 cmd = (bit_buf >> bit_pos) & 0x03;
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;
381 } else if(cmd == 3) {
382 if(strip->usl7 == 0) {
383 strip->usl7 = 1;
384 ref_vectors = buf2 + (*buf1 * 2);
385 buf1++;
386 continue;
390 cur_frm_pos = cur + width * strip->ypos + strip->xpos;
392 if((blks_width = strip->width) < 0)
393 blks_width += 3;
394 blks_width >>= 2;
395 blks_height = strip->height;
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];
403 if(cmd == 2) {
404 if(bit_pos <= 0) {
405 bit_pos = 8;
406 bit_buf = *buf1++;
409 bit_pos -= 2;
410 cmd = (bit_buf >> bit_pos) & 0x03;
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;
419 } else if(cmd != 1)
420 return;
421 } else {
422 k = *buf1 >> 4;
423 j = *buf1 & 0x0f;
424 buf1++;
425 lv = j + fflags2;
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];
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);
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];
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;
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;
486 break;
487 case 8:
488 if(lp2 == 0) {
489 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
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];
496 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
497 break;
498 } else {
499 rle_v1 = 1;
500 rle_v2 = *buf1 - 1;
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;
510 case 7:
511 if(rle_v3 != 0)
512 rle_v3 = 0;
513 else {
514 buf1--;
515 rle_v3 = 1;
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];
522 lp2 = 4;
523 break;
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;
533 LV1_CHECK(buf1,rle_v3,lv1,lp2)
534 break;
535 default:
536 return;
540 cur_frm_pos += 4;
541 ref_frm_pos += 4;
544 cur_frm_pos += ((width - blks_width) * 4);
545 ref_frm_pos += ((width - blks_width) * 4);
547 break;
549 case 4:
550 case 3: /********** CASE 3 **********/
551 if(ref_vectors != NULL)
552 return;
553 flag1 = 1;
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++;
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];
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;
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);
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;
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;
593 break;
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;
601 break;
603 case 6:
604 lp2 = 4;
605 break;
607 case 7:
608 if(rle_v3 != 0)
609 rle_v3 = 0;
610 else {
611 buf1--;
612 rle_v3 = 1;
614 lp2 = 4;
615 break;
617 case 8:
618 if(lp2 == 0) {
619 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
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];
626 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
627 break;
628 } else {
629 rle_v2 = (*buf1) - 1;
630 rle_v1 = 1;
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;
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);
647 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
648 cur_lp[j] = lv;
650 LV1_CHECK(buf1,rle_v3,lv1,lp2)
651 break;
653 default:
654 return;
658 cur_frm_pos += 4;
661 cur_frm_pos += (((width * 2) - blks_width) * 4);
662 flag1 = 0;
664 break;
666 case 10: /********** CASE 10 **********/
667 if(ref_vectors == NULL) {
668 flag1 = 1;
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
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];
703 lp2++;
704 break;
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];
716 buf1++;
717 lp2++;
718 break;
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;
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;
735 lp2 += 2;
737 break;
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;
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;
754 lp2 = 3;
756 break;
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;
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;
776 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
777 break;
778 } else {
779 rle_v1 = 1;
780 rle_v2 = (*buf1) - 1;
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;
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;
798 lp2 = 4;
799 break;
801 case 6:
802 lp2 = 4;
803 break;
805 case 7:
806 if(lp2 == 0) {
807 if(rle_v3 != 0)
808 rle_v3 = 0;
809 else {
810 buf1--;
811 rle_v3 = 1;
813 lp2 = 4;
815 break;
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;
828 default:
829 return;
833 cur_frm_pos += 8;
836 cur_frm_pos += (((width * 2) - blks_width) * 4);
837 flag1 = 0;
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];
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;
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;
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];
874 lp2 += 2;
876 break;
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];
884 lp2 = 3;
886 break;
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];
895 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
896 break;
897 } else {
898 rle_v1 = 1;
899 rle_v2 = (*buf1) - 1;
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];
910 lp2 = 4;
911 break;
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;
924 default:
925 return;
929 cur_frm_pos += 8;
930 ref_frm_pos += 8;
933 cur_frm_pos += (((width * 2) - blks_width) * 4);
934 ref_frm_pos += (((width * 2) - blks_width) * 4);
937 break;
939 case 11: /********** CASE 11 **********/
940 if(ref_vectors == NULL)
941 return;
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];
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;
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;
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;
977 break;
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;
985 break;
987 case 8:
988 if(lp2 == 0) {
989 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
991 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
992 cur_lp[j] = ref_lp[j];
994 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
995 break;
996 } else {
997 rle_v1 = 1;
998 rle_v2 = (*buf1) - 1;
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;
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;
1021 default:
1022 return;
1026 cur_frm_pos += 4;
1027 ref_frm_pos += 4;
1030 cur_frm_pos += (((width * 2) - blks_width) * 4);
1031 ref_frm_pos += (((width * 2) - blks_width) * 4);
1033 break;
1035 default:
1036 return;
1040 if(strip < strip_tbl)
1041 return;
1043 for( ; strip >= strip_tbl; strip--) {
1044 if(strip->split_flag != 0) {
1045 strip->split_flag = 0;
1046 strip->usl7 = (strip-1)->usl7;
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;
1057 break;
1063 static int indeo3_decode_init(AVCodecContext *avctx)
1065 Indeo3DecodeContext *s = avctx->priv_data;
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;
1073 build_modpred(s);
1074 iv_alloc_frames(s);
1076 return 0;
1079 static int indeo3_decode_frame(AVCodecContext *avctx,
1080 void *data, int *data_size,
1081 unsigned char *buf, int buf_size)
1083 Indeo3DecodeContext *s=avctx->priv_data;
1084 unsigned char *src, *dest;
1085 int y;
1087 iv_decode_frame(s, buf, buf_size);
1089 if(s->frame.data[0])
1090 avctx->release_buffer(avctx, &s->frame);
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;
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];
1106 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
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];
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];
1125 *data_size=sizeof(AVFrame);
1126 *(AVFrame*)data= s->frame;
1128 return buf_size;
1131 static int indeo3_decode_end(AVCodecContext *avctx)
1133 Indeo3DecodeContext *s = avctx->priv_data;
1135 iv_free_func(s);
1137 return 0;
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,
1150 NULL