13ca6e11f4dc7aa768d0592f6ba9bc89f7d45c7a
[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 lp2++;
471 break;
472 case 2:
473 if(lp2 == 0) {
474 for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
475 cur_lp[j] = ref_lp[j];
476 lp2 += 2;
477 }
478 break;
479 case 3:
480 if(lp2 < 2) {
481 for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
482 cur_lp[j] = ref_lp[j];
483 lp2 = 3;
484 }
485 break;
486 case 8:
487 if(lp2 == 0) {
488 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
489
490 if(rle_v1 == 1 || ref_vectors != NULL) {
491 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
492 cur_lp[j] = ref_lp[j];
493 }
494
495 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
496 break;
497 } else {
498 rle_v1 = 1;
499 rle_v2 = *buf1 - 1;
500 }
501 case 5:
502 LP2_CHECK(buf1,rle_v3,lp2)
503 case 4:
504 for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
505 cur_lp[j] = ref_lp[j];
506 lp2 = 4;
507 break;
508
509 case 7:
510 if(rle_v3 != 0)
511 rle_v3 = 0;
512 else {
513 buf1--;
514 rle_v3 = 1;
515 }
516 case 6:
517 if(ref_vectors != NULL) {
518 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
519 cur_lp[j] = ref_lp[j];
520 }
521 lp2 = 4;
522 break;
523
524 case 9:
525 lv1 = *buf1++;
526 lv = (lv1 & 0x7F) << 1;
527 lv += (lv << 8);
528 lv += (lv << 16);
529 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
530 cur_lp[j] = lv;
531
532 LV1_CHECK(buf1,rle_v3,lv1,lp2)
533 break;
534 default:
535 return;
536 }
537 }
538
539 cur_frm_pos += 4;
540 ref_frm_pos += 4;
541 }
542
543 cur_frm_pos += ((width - blks_width) * 4);
544 ref_frm_pos += ((width - blks_width) * 4);
545 }
546 break;
547
548 case 4:
549 case 3: /********** CASE 3 **********/
550 if(ref_vectors != NULL)
551 return;
552 flag1 = 1;
553
554 for( ; blks_height > 0; blks_height -= 8) {
555 for(lp1 = 0; lp1 < blks_width; lp1++) {
556 for(lp2 = 0; lp2 < 4; ) {
557 k = *buf1++;
558
559 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
560 ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
561
562 switch(correction_type_sp[lp2 & 0x01][k]) {
563 case 0:
564 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
565 if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
566 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
567 else
568 cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
569 lp2++;
570 break;
571
572 case 1:
573 res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1++]) << 1;
574 ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
575 res = ((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
576 ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
577
578 if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
579 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
580 else
581 cur_lp[0] = cur_lp[width_tbl[1]];
582 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 if(rle_v3 != 0)
607 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 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
618
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 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
625 break;
626 } else {
627 rle_v2 = (*buf1) - 1;
628 rle_v1 = 1;
629 }
630 case 5:
631 LP2_CHECK(buf1,rle_v3,lp2)
632 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 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
640 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 LV1_CHECK(buf1,rle_v3,lv1,lp2)
649 break;
650
651 default:
652 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 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 lv1 = ref_lp[0];
675 lv2 = ref_lp[1];
676 if(lp2 == 0 && flag1 != 0) {
677 #ifdef WORDS_BIGENDIAN
678 lv1 = lv1 & 0xFF00FF00;
679 lv1 = (lv1 >> 8) | lv1;
680 lv2 = lv2 & 0xFF00FF00;
681 lv2 = (lv2 >> 8) | lv2;
682 #else
683 lv1 = lv1 & 0x00FF00FF;
684 lv1 = (lv1 << 8) | lv1;
685 lv2 = lv2 & 0x00FF00FF;
686 lv2 = (lv2 << 8) | lv2;
687 #endif
688 }
689
690 switch(correction_type_sp[lp2 & 0x01][k]) {
691 case 0:
692 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 if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
695 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 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1++]) << 1);
706 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
707 if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
708 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 lp2++;
715 break;
716
717 case 2:
718 if(lp2 == 0) {
719 if(flag1 != 0) {
720 for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
721 cur_lp[j] = lv1;
722 cur_lp[j+1] = lv2;
723 }
724 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
725 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
726 } else {
727 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
728 cur_lp[j] = lv1;
729 cur_lp[j+1] = lv2;
730 }
731 }
732 lp2 += 2;
733 }
734 break;
735
736 case 3:
737 if(lp2 < 2) {
738 if(lp2 == 0 && flag1 != 0) {
739 for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
740 cur_lp[j] = lv1;
741 cur_lp[j+1] = lv2;
742 }
743 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
744 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
745 } else {
746 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
747 cur_lp[j] = lv1;
748 cur_lp[j+1] = lv2;
749 }
750 }
751 lp2 = 3;
752 }
753 break;
754
755 case 8:
756 if(lp2 == 0) {
757 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
758 if(rle_v1 == 1) {
759 if(flag1 != 0) {
760 for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
761 cur_lp[j] = lv1;
762 cur_lp[j+1] = lv2;
763 }
764 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
765 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
766 } else {
767 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
768 cur_lp[j] = lv1;
769 cur_lp[j+1] = lv2;
770 }
771 }
772 }
773 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
774 break;
775 } else {
776 rle_v1 = 1;
777 rle_v2 = (*buf1) - 1;
778 }
779 case 5:
780 LP2_CHECK(buf1,rle_v3,lp2)
781 case 4:
782 if(lp2 == 0 && flag1 != 0) {
783 for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
784 cur_lp[j] = lv1;
785 cur_lp[j+1] = lv2;
786 }
787 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
788 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
789 } else {
790 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
791 cur_lp[j] = lv1;
792 cur_lp[j+1] = lv2;
793 }
794 }
795 lp2 = 4;
796 break;
797
798 case 6:
799 lp2 = 4;
800 break;
801
802 case 7:
803 if(lp2 == 0) {
804 if(rle_v3 != 0)
805 rle_v3 = 0;
806 else {
807 buf1--;
808 rle_v3 = 1;
809 }
810 lp2 = 4;
811 }
812 break;
813
814 case 9:
815 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
816 lv1 = *buf1;
817 lv = (lv1 & 0x7F) << 1;
818 lv += (lv << 8);
819 lv += (lv << 16);
820 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
821 cur_lp[j] = lv;
822 LV1_CHECK(buf1,rle_v3,lv1,lp2)
823 break;
824
825 default:
826 return;
827 }
828 }
829
830 cur_frm_pos += 8;
831 }
832
833 cur_frm_pos += (((width * 2) - blks_width) * 4);
834 flag1 = 0;
835 }
836 } else {
837 for( ; blks_height > 0; blks_height -= 8) {
838 for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
839 for(lp2 = 0; lp2 < 4; ) {
840 k = *buf1++;
841 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
842 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
843
844 switch(correction_type_sp[lp2 & 0x01][k]) {
845 case 0:
846 lv1 = correctionloworder_lp[lp2 & 0x01][k];
847 lv2 = correctionhighorder_lp[lp2 & 0x01][k];
848 cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
849 cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
850 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
851 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
852 lp2++;
853 break;
854
855 case 1:
856 lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
857 lv2 = correctionloworder_lp[lp2 & 0x01][k];
858 cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
859 cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
860 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
861 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
862 lp2++;
863 break;
864
865 case 2:
866 if(lp2 == 0) {
867 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
868 cur_lp[j] = ref_lp[j];
869 cur_lp[j+1] = ref_lp[j+1];
870 }
871 lp2 += 2;
872 }
873 break;
874
875 case 3:
876 if(lp2 < 2) {
877 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
878 cur_lp[j] = ref_lp[j];
879 cur_lp[j+1] = ref_lp[j+1];
880 }
881 lp2 = 3;
882 }
883 break;
884
885 case 8:
886 if(lp2 == 0) {
887 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
888 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
889 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
890 ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];
891 }
892 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
893 break;
894 } else {
895 rle_v1 = 1;
896 rle_v2 = (*buf1) - 1;
897 }
898 case 5:
899 case 7:
900 LP2_CHECK(buf1,rle_v3,lp2)
901 case 6:
902 case 4:
903 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
904 cur_lp[j] = ref_lp[j];
905 cur_lp[j+1] = ref_lp[j+1];
906 }
907 lp2 = 4;
908 break;
909
910 case 9:
911 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
912 lv1 = *buf1;
913 lv = (lv1 & 0x7F) << 1;
914 lv += (lv << 8);
915 lv += (lv << 16);
916 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
917 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
918 LV1_CHECK(buf1,rle_v3,lv1,lp2)
919 break;
920
921 default:
922 return;
923 }
924 }
925
926 cur_frm_pos += 8;
927 ref_frm_pos += 8;
928 }
929
930 cur_frm_pos += (((width * 2) - blks_width) * 4);
931 ref_frm_pos += (((width * 2) - blks_width) * 4);
932 }
933 }
934 break;
935
936 case 11: /********** CASE 11 **********/
937 if(ref_vectors == NULL)
938 return;
939
940 for( ; blks_height > 0; blks_height -= 8) {
941 for(lp1 = 0; lp1 < blks_width; lp1++) {
942 for(lp2 = 0; lp2 < 4; ) {
943 k = *buf1++;
944 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
945 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
946
947 switch(correction_type_sp[lp2 & 0x01][k]) {
948 case 0:
949 cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
950 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
951 lp2++;
952 break;
953
954 case 1:
955 lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
956 lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
957 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1);
958 ((unsigned short *)cur_lp)[0] = le2me_16(res);
959 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1);
960 ((unsigned short *)cur_lp)[1] = le2me_16(res);
961 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1);
962 ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
963 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1);
964 ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
965 lp2++;
966 break;
967
968 case 2:
969 if(lp2 == 0) {
970 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
971 cur_lp[j] = ref_lp[j];
972 lp2 += 2;
973 }
974 break;
975
976 case 3:
977 if(lp2 < 2) {
978 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
979 cur_lp[j] = ref_lp[j];
980 lp2 = 3;
981 }
982 break;
983
984 case 8:
985 if(lp2 == 0) {
986 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
987
988 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
989 cur_lp[j] = ref_lp[j];
990
991 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
992 break;
993 } else {
994 rle_v1 = 1;
995 rle_v2 = (*buf1) - 1;
996 }
997 case 5:
998 case 7:
999 LP2_CHECK(buf1,rle_v3,lp2)
1000 case 4:
1001 case 6:
1002 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
1003 cur_lp[j] = ref_lp[j];
1004 lp2 = 4;
1005 break;
1006
1007 case 9:
1008 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
1009 lv1 = *buf1++;
1010 lv = (lv1 & 0x7F) << 1;
1011 lv += (lv << 8);
1012 lv += (lv << 16);
1013 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
1014 cur_lp[j] = lv;
1015 LV1_CHECK(buf1,rle_v3,lv1,lp2)
1016 break;
1017
1018 default:
1019 return;
1020 }
1021 }
1022
1023 cur_frm_pos += 4;
1024 ref_frm_pos += 4;
1025 }
1026
1027 cur_frm_pos += (((width * 2) - blks_width) * 4);
1028 ref_frm_pos += (((width * 2) - blks_width) * 4);
1029 }
1030 break;
1031
1032 default:
1033 return;
1034 }
1035 }
1036
1037 if(strip < strip_tbl)
1038 return;
1039
1040 for( ; strip >= strip_tbl; strip--) {
1041 if(strip->split_flag != 0) {
1042 strip->split_flag = 0;
1043 strip->usl7 = (strip-1)->usl7;
1044
1045 if(strip->split_direction) {
1046 strip->xpos += strip->width;
1047 strip->width = (strip-1)->width - strip->width;
1048 if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
1049 strip->width = width - strip->xpos;
1050 } else {
1051 strip->ypos += strip->height;
1052 strip->height = (strip-1)->height - strip->height;
1053 }
1054 break;
1055 }
1056 }
1057 }
1058 }
1059
1060 static int indeo3_decode_init(AVCodecContext *avctx)
1061 {
1062 Indeo3DecodeContext *s = avctx->priv_data;
1063
1064 s->avctx = avctx;
1065 s->width = avctx->width;
1066 s->height = avctx->height;
1067 avctx->pix_fmt = PIX_FMT_YUV410P;
1068 avctx->has_b_frames = 0;
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 if(s->frame.data[0])
1087 avctx->release_buffer(avctx, &s->frame);
1088
1089 s->frame.reference = 0;
1090 if(avctx->get_buffer(avctx, &s->frame) < 0) {
1091 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1092 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 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1104 {
1105 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 }
1121
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 };