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