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