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