Statistics
| Branch: | Revision:

ffmpeg / libavcodec / indeo3.c @ 2912e87a

History | View | Annotate | Download (47.5 KB)

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 Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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

    
26
#include "libavutil/imgutils.h"
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 short y_w, y_h;
39
    unsigned short uv_w, uv_h;
40
} YUVBufs;
41

    
42
typedef struct Indeo3DecodeContext {
43
    AVCodecContext *avctx;
44
    int width, height;
45
    AVFrame frame;
46

    
47
    uint8_t *buf;
48
    YUVBufs iv_frame[2];
49
    YUVBufs *cur_frame;
50
    YUVBufs *ref_frame;
51

    
52
    uint8_t *ModPred;
53
    uint8_t *corrector_type;
54
} Indeo3DecodeContext;
55

    
56
static const uint8_t corrector_type_0[24] = {
57
    195, 159, 133, 115, 101,  93,  87,  77,
58
    195, 159, 133, 115, 101,  93,  87,  77,
59
    128,  79,  79,  79,  79,  79,  79,  79
60
};
61

    
62
static const uint8_t corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
63

    
64
static av_cold int build_modpred(Indeo3DecodeContext *s)
65
{
66
    int i, j;
67

    
68
    if (!(s->ModPred = av_malloc(8 * 128)))
69
        return AVERROR(ENOMEM);
70

    
71
    for (i=0; i < 128; ++i) {
72
        s->ModPred[i+0*128] = i >  126 ? 254 : 2*(i + 1 - ((i + 1) % 2));
73
        s->ModPred[i+1*128] = i ==   7 ?  20 :
74
                              i == 119 ||
75
                              i == 120 ? 236 : 2*(i + 2 - ((i + 1) % 3));
76
        s->ModPred[i+2*128] = i >  125 ? 248 : 2*(i + 2 - ((i + 2) % 4));
77
        s->ModPred[i+3*128] =                  2*(i + 1 - ((i - 3) % 5));
78
        s->ModPred[i+4*128] = i ==   8 ?  20 : 2*(i + 1 - ((i - 3) % 6));
79
        s->ModPred[i+5*128] =                  2*(i + 4 - ((i + 3) % 7));
80
        s->ModPred[i+6*128] = i >  123 ? 240 : 2*(i + 4 - ((i + 4) % 8));
81
        s->ModPred[i+7*128] =                  2*(i + 5 - ((i + 4) % 9));
82
    }
83

    
84
    if (!(s->corrector_type = av_malloc(24 * 256)))
85
        return AVERROR(ENOMEM);
86

    
87
    for (i=0; i < 24; ++i) {
88
        for (j=0; j < 256; ++j) {
89
            s->corrector_type[i*256+j] = j < corrector_type_0[i]          ? 1 :
90
                                         j < 248 || (i == 16 && j == 248) ? 0 :
91
                                         corrector_type_2[j - 248];
92
        }
93
    }
94

    
95
  return 0;
96
}
97

    
98
static av_cold int iv_alloc_frames(Indeo3DecodeContext *s)
99
{
100
    int luma_width    = (s->width           + 3) & ~3,
101
        luma_height   = (s->height          + 3) & ~3,
102
        chroma_width  = ((luma_width  >> 2) + 3) & ~3,
103
        chroma_height = ((luma_height >> 2) + 3) & ~3,
104
        luma_pixels   = luma_width   * luma_height,
105
        chroma_pixels = chroma_width * chroma_height,
106
        i;
107
    unsigned int bufsize = luma_pixels * 2 + luma_width * 3 +
108
                          (chroma_pixels   + chroma_width) * 4;
109

    
110
    av_freep(&s->buf);
111
    if(!(s->buf = av_malloc(bufsize)))
112
        return AVERROR(ENOMEM);
113
    s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
114
    s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
115
    s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
116
    s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;
117

    
118
    s->iv_frame[0].Ybuf = s->buf + luma_width;
119
    i = luma_pixels + luma_width * 2;
120
    s->iv_frame[1].Ybuf = s->buf + i;
121
    i += (luma_pixels + luma_width);
122
    s->iv_frame[0].Ubuf = s->buf + i;
123
    i += (chroma_pixels + chroma_width);
124
    s->iv_frame[1].Ubuf = s->buf + i;
125
    i += (chroma_pixels + chroma_width);
126
    s->iv_frame[0].Vbuf = s->buf + i;
127
    i += (chroma_pixels + chroma_width);
128
    s->iv_frame[1].Vbuf = s->buf + i;
129

    
130
    for(i = 1; i <= luma_width; i++)
131
        s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =
132
            s->iv_frame[0].Ubuf[-i] = 0x80;
133

    
134
    for(i = 1; i <= chroma_width; i++) {
135
        s->iv_frame[1].Ubuf[-i] = 0x80;
136
        s->iv_frame[0].Vbuf[-i] = 0x80;
137
        s->iv_frame[1].Vbuf[-i] = 0x80;
138
        s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;
139
    }
140

    
141
    return 0;
142
}
143

    
144
static av_cold void iv_free_func(Indeo3DecodeContext *s)
145
{
146
    av_freep(&s->buf);
147
    av_freep(&s->ModPred);
148
    av_freep(&s->corrector_type);
149
}
150

    
151
struct ustr {
152
    long xpos;
153
    long ypos;
154
    long width;
155
    long height;
156
    long split_flag;
157
    long split_direction;
158
    long usl7;
159
};
160

    
161

    
162
#define LV1_CHECK(buf1,rle_v3,lv1,lp2)  \
163
    if((lv1 & 0x80) != 0) {             \
164
        if(rle_v3 != 0)                 \
165
            rle_v3 = 0;                 \
166
        else {                          \
167
            rle_v3 = 1;                 \
168
            buf1 -= 2;                  \
169
        }                               \
170
    }                                   \
171
    lp2 = 4;
172

    
173

    
174
#define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)  \
175
    if(rle_v3 == 0) {                            \
176
        rle_v2 = *buf1;                          \
177
        rle_v1 = 1;                              \
178
        if(rle_v2 > 32) {                        \
179
            rle_v2 -= 32;                        \
180
            rle_v1 = 0;                          \
181
        }                                        \
182
        rle_v3 = 1;                              \
183
    }                                            \
184
    buf1--;
185

    
186

    
187
#define LP2_CHECK(buf1,rle_v3,lp2)  \
188
    if(lp2 == 0 && rle_v3 != 0)     \
189
        rle_v3 = 0;                 \
190
    else {                          \
191
        buf1--;                     \
192
        rle_v3 = 1;                 \
193
    }
194

    
195

    
196
#define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
197
    rle_v2--;                                 \
198
    if(rle_v2 == 0) {                         \
199
        rle_v3 = 0;                           \
200
        buf1 += 2;                            \
201
    }                                         \
202
    lp2 = 4;
203

    
204
static void iv_Decode_Chunk(Indeo3DecodeContext *s,
205
        uint8_t *cur, uint8_t *ref, int width, int height,
206
        const uint8_t *buf1, long cb_offset, const uint8_t *hdr,
207
        const uint8_t *buf2, int min_width_160)
208
{
209
    uint8_t bit_buf;
210
    unsigned long bit_pos, lv, lv1, lv2;
211
    long *width_tbl, width_tbl_arr[10];
212
    const signed char *ref_vectors;
213
    uint8_t *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
214
    uint32_t *cur_lp, *ref_lp;
215
    const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
216
    uint8_t *correction_type_sp[2];
217
    struct ustr strip_tbl[20], *strip;
218
    int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
219
        rle_v1, rle_v2, rle_v3;
220
    unsigned short res;
221

    
222
    bit_buf = 0;
223
    ref_vectors = NULL;
224

    
225
    width_tbl = width_tbl_arr + 1;
226
    i = (width < 0 ? width + 3 : width)/4;
227
    for(j = -1; j < 8; j++)
228
        width_tbl[j] = i * j;
229

    
230
    strip = strip_tbl;
231

    
232
    for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
233

    
234
    strip->ypos = strip->xpos = 0;
235
    for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
236
    strip->height = height;
237
    strip->split_direction = 0;
238
    strip->split_flag = 0;
239
    strip->usl7 = 0;
240

    
241
    bit_pos = 0;
242

    
243
    rle_v1 = rle_v2 = rle_v3 = 0;
244

    
245
    while(strip >= strip_tbl) {
246
        if(bit_pos <= 0) {
247
            bit_pos = 8;
248
            bit_buf = *buf1++;
249
        }
250

    
251
        bit_pos -= 2;
252
        cmd = (bit_buf >> bit_pos) & 0x03;
253

    
254
        if(cmd == 0) {
255
            strip++;
256
            if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
257
                av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
258
                break;
259
            }
260
            memcpy(strip, strip-1, sizeof(*strip));
261
            strip->split_flag = 1;
262
            strip->split_direction = 0;
263
            strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);
264
            continue;
265
        } else if(cmd == 1) {
266
            strip++;
267
            if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
268
                av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
269
                break;
270
            }
271
            memcpy(strip, strip-1, sizeof(*strip));
272
            strip->split_flag = 1;
273
            strip->split_direction = 1;
274
            strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);
275
            continue;
276
        } else if(cmd == 2) {
277
            if(strip->usl7 == 0) {
278
                strip->usl7 = 1;
279
                ref_vectors = NULL;
280
                continue;
281
            }
282
        } else if(cmd == 3) {
283
            if(strip->usl7 == 0) {
284
                strip->usl7 = 1;
285
                ref_vectors = (const signed char*)buf2 + (*buf1 * 2);
286
                buf1++;
287
                continue;
288
            }
289
        }
290

    
291
        cur_frm_pos = cur + width * strip->ypos + strip->xpos;
292

    
293
        if((blks_width = strip->width) < 0)
294
            blks_width += 3;
295
        blks_width >>= 2;
296
        blks_height = strip->height;
297

    
298
        if(ref_vectors != NULL) {
299
            ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
300
                ref_vectors[1] + strip->xpos;
301
        } else
302
            ref_frm_pos = cur_frm_pos - width_tbl[4];
303

    
304
        if(cmd == 2) {
305
            if(bit_pos <= 0) {
306
                bit_pos = 8;
307
                bit_buf = *buf1++;
308
            }
309

    
310
            bit_pos -= 2;
311
            cmd = (bit_buf >> bit_pos) & 0x03;
312

    
313
            if(cmd == 0 || ref_vectors != NULL) {
314
                for(lp1 = 0; lp1 < blks_width; lp1++) {
315
                    for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
316
                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
317
                    cur_frm_pos += 4;
318
                    ref_frm_pos += 4;
319
                }
320
            } else if(cmd != 1)
321
                return;
322
        } else {
323
            k = *buf1 >> 4;
324
            j = *buf1 & 0x0f;
325
            buf1++;
326
            lv = j + cb_offset;
327

    
328
            if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
329
                cp2 = s->ModPred + ((lv - 8) << 7);
330
                cp = ref_frm_pos;
331
                for(i = 0; i < blks_width << 2; i++) {
332
                    int v = *cp >> 1;
333
                    *(cp++) = cp2[v];
334
                }
335
            }
336

    
337
            if(k == 1 || k == 4) {
338
                lv = (hdr[j] & 0xf) + cb_offset;
339
                correction_type_sp[0] = s->corrector_type + (lv << 8);
340
                correction_lp[0] = correction + (lv << 8);
341
                lv = (hdr[j] >> 4) + cb_offset;
342
                correction_lp[1] = correction + (lv << 8);
343
                correction_type_sp[1] = s->corrector_type + (lv << 8);
344
            } else {
345
                correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);
346
                correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);
347
                correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);
348
                correction_lp[0] = correction_lp[1] = correction + (lv << 8);
349
            }
350

    
351
            switch(k) {
352
            case 1:
353
            case 0:                    /********** CASE 0 **********/
354
                for( ; blks_height > 0; blks_height -= 4) {
355
                    for(lp1 = 0; lp1 < blks_width; lp1++) {
356
                        for(lp2 = 0; lp2 < 4; ) {
357
                            k = *buf1++;
358
                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
359
                            ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
360

    
361
                            switch(correction_type_sp[0][k]) {
362
                            case 0:
363
                                *cur_lp = av_le2ne32(((av_le2ne32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
364
                                lp2++;
365
                                break;
366
                            case 1:
367
                                res = ((av_le2ne16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
368
                                ((unsigned short *)cur_lp)[0] = av_le2ne16(res);
369
                                res = ((av_le2ne16(((unsigned short *)(ref_lp))[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
370
                                ((unsigned short *)cur_lp)[1] = av_le2ne16(res);
371
                                buf1++;
372
                                lp2++;
373
                                break;
374
                            case 2:
375
                                if(lp2 == 0) {
376
                                    for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
377
                                        cur_lp[j] = ref_lp[j];
378
                                    lp2 += 2;
379
                                }
380
                                break;
381
                            case 3:
382
                                if(lp2 < 2) {
383
                                    for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
384
                                        cur_lp[j] = ref_lp[j];
385
                                    lp2 = 3;
386
                                }
387
                                break;
388
                            case 8:
389
                                if(lp2 == 0) {
390
                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
391

    
392
                                    if(rle_v1 == 1 || ref_vectors != NULL) {
393
                                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
394
                                            cur_lp[j] = ref_lp[j];
395
                                    }
396

    
397
                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
398
                                    break;
399
                                } else {
400
                                    rle_v1 = 1;
401
                                    rle_v2 = *buf1 - 1;
402
                                }
403
                            case 5:
404
                                LP2_CHECK(buf1,rle_v3,lp2)
405
                            case 4:
406
                                for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
407
                                    cur_lp[j] = ref_lp[j];
408
                                lp2 = 4;
409
                                break;
410

    
411
                            case 7:
412
                                if(rle_v3 != 0)
413
                                    rle_v3 = 0;
414
                                else {
415
                                    buf1--;
416
                                    rle_v3 = 1;
417
                                }
418
                            case 6:
419
                                if(ref_vectors != NULL) {
420
                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
421
                                        cur_lp[j] = ref_lp[j];
422
                                }
423
                                lp2 = 4;
424
                                break;
425

    
426
                            case 9:
427
                                lv1 = *buf1++;
428
                                lv = (lv1 & 0x7F) << 1;
429
                                lv += (lv << 8);
430
                                lv += (lv << 16);
431
                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
432
                                    cur_lp[j] = lv;
433

    
434
                                LV1_CHECK(buf1,rle_v3,lv1,lp2)
435
                                break;
436
                            default:
437
                                return;
438
                            }
439
                        }
440

    
441
                        cur_frm_pos += 4;
442
                        ref_frm_pos += 4;
443
                    }
444

    
445
                    cur_frm_pos += ((width - blks_width) * 4);
446
                    ref_frm_pos += ((width - blks_width) * 4);
447
                }
448
                break;
449

    
450
            case 4:
451
            case 3:                    /********** CASE 3 **********/
452
                if(ref_vectors != NULL)
453
                    return;
454
                flag1 = 1;
455

    
456
                for( ; blks_height > 0; blks_height -= 8) {
457
                    for(lp1 = 0; lp1 < blks_width; lp1++) {
458
                        for(lp2 = 0; lp2 < 4; ) {
459
                            k = *buf1++;
460

    
461
                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
462
                            ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
463

    
464
                            switch(correction_type_sp[lp2 & 0x01][k]) {
465
                            case 0:
466
                                cur_lp[width_tbl[1]] = av_le2ne32(((av_le2ne32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
467
                                if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
468
                                    cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
469
                                else
470
                                    cur_lp[0] = av_le2ne32(((av_le2ne32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
471
                                lp2++;
472
                                break;
473

    
474
                            case 1:
475
                                res = ((av_le2ne16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
476
                                ((unsigned short *)cur_lp)[width_tbl[2]] = av_le2ne16(res);
477
                                res = ((av_le2ne16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
478
                                ((unsigned short *)cur_lp)[width_tbl[2]+1] = av_le2ne16(res);
479

    
480
                                if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
481
                                    cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
482
                                else
483
                                    cur_lp[0] = cur_lp[width_tbl[1]];
484
                                buf1++;
485
                                lp2++;
486
                                break;
487

    
488
                            case 2:
489
                                if(lp2 == 0) {
490
                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
491
                                        cur_lp[j] = *ref_lp;
492
                                    lp2 += 2;
493
                                }
494
                                break;
495

    
496
                            case 3:
497
                                if(lp2 < 2) {
498
                                    for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
499
                                        cur_lp[j] = *ref_lp;
500
                                    lp2 = 3;
501
                                }
502
                                break;
503

    
504
                            case 6:
505
                                lp2 = 4;
506
                                break;
507

    
508
                            case 7:
509
                                if(rle_v3 != 0)
510
                                    rle_v3 = 0;
511
                                else {
512
                                    buf1--;
513
                                    rle_v3 = 1;
514
                                }
515
                                lp2 = 4;
516
                                break;
517

    
518
                            case 8:
519
                                if(lp2 == 0) {
520
                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
521

    
522
                                    if(rle_v1 == 1) {
523
                                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
524
                                            cur_lp[j] = ref_lp[j];
525
                                    }
526

    
527
                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
528
                                    break;
529
                                } else {
530
                                    rle_v2 = (*buf1) - 1;
531
                                    rle_v1 = 1;
532
                                }
533
                            case 5:
534
                                LP2_CHECK(buf1,rle_v3,lp2)
535
                            case 4:
536
                                for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
537
                                    cur_lp[j] = *ref_lp;
538
                                lp2 = 4;
539
                                break;
540

    
541
                            case 9:
542
                                av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
543
                                lv1 = *buf1++;
544
                                lv = (lv1 & 0x7F) << 1;
545
                                lv += (lv << 8);
546
                                lv += (lv << 16);
547

    
548
                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
549
                                    cur_lp[j] = lv;
550

    
551
                                LV1_CHECK(buf1,rle_v3,lv1,lp2)
552
                                break;
553

    
554
                            default:
555
                                return;
556
                            }
557
                        }
558

    
559
                        cur_frm_pos += 4;
560
                    }
561

    
562
                    cur_frm_pos += (((width * 2) - blks_width) * 4);
563
                    flag1 = 0;
564
                }
565
                break;
566

    
567
            case 10:                    /********** CASE 10 **********/
568
                if(ref_vectors == NULL) {
569
                    flag1 = 1;
570

    
571
                    for( ; blks_height > 0; blks_height -= 8) {
572
                        for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
573
                            for(lp2 = 0; lp2 < 4; ) {
574
                                k = *buf1++;
575
                                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
576
                                ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
577
                                lv1 = ref_lp[0];
578
                                lv2 = ref_lp[1];
579
                                if(lp2 == 0 && flag1 != 0) {
580
#if HAVE_BIGENDIAN
581
                                    lv1 = lv1 & 0xFF00FF00;
582
                                    lv1 = (lv1 >> 8) | lv1;
583
                                    lv2 = lv2 & 0xFF00FF00;
584
                                    lv2 = (lv2 >> 8) | lv2;
585
#else
586
                                    lv1 = lv1 & 0x00FF00FF;
587
                                    lv1 = (lv1 << 8) | lv1;
588
                                    lv2 = lv2 & 0x00FF00FF;
589
                                    lv2 = (lv2 << 8) | lv2;
590
#endif
591
                                }
592

    
593
                                switch(correction_type_sp[lp2 & 0x01][k]) {
594
                                case 0:
595
                                    cur_lp[width_tbl[1]] = av_le2ne32(((av_le2ne32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
596
                                    cur_lp[width_tbl[1]+1] = av_le2ne32(((av_le2ne32(lv2) >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1);
597
                                    if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
598
                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
599
                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
600
                                    } else {
601
                                        cur_lp[0] = cur_lp[width_tbl[1]];
602
                                        cur_lp[1] = cur_lp[width_tbl[1]+1];
603
                                    }
604
                                    lp2++;
605
                                    break;
606

    
607
                                case 1:
608
                                    cur_lp[width_tbl[1]] = av_le2ne32(((av_le2ne32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1);
609
                                    cur_lp[width_tbl[1]+1] = av_le2ne32(((av_le2ne32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
610
                                    if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
611
                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
612
                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
613
                                    } else {
614
                                        cur_lp[0] = cur_lp[width_tbl[1]];
615
                                        cur_lp[1] = cur_lp[width_tbl[1]+1];
616
                                    }
617
                                    buf1++;
618
                                    lp2++;
619
                                    break;
620

    
621
                                case 2:
622
                                    if(lp2 == 0) {
623
                                        if(flag1 != 0) {
624
                                            for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
625
                                                cur_lp[j] = lv1;
626
                                                cur_lp[j+1] = lv2;
627
                                            }
628
                                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
629
                                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
630
                                        } else {
631
                                            for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
632
                                                cur_lp[j] = lv1;
633
                                                cur_lp[j+1] = lv2;
634
                                            }
635
                                        }
636
                                        lp2 += 2;
637
                                    }
638
                                    break;
639

    
640
                                case 3:
641
                                    if(lp2 < 2) {
642
                                        if(lp2 == 0 && flag1 != 0) {
643
                                            for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
644
                                                cur_lp[j] = lv1;
645
                                                cur_lp[j+1] = lv2;
646
                                            }
647
                                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
648
                                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
649
                                        } else {
650
                                            for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
651
                                                cur_lp[j] = lv1;
652
                                                cur_lp[j+1] = lv2;
653
                                            }
654
                                        }
655
                                        lp2 = 3;
656
                                    }
657
                                    break;
658

    
659
                                case 8:
660
                                    if(lp2 == 0) {
661
                                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
662
                                        if(rle_v1 == 1) {
663
                                            if(flag1 != 0) {
664
                                                for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
665
                                                    cur_lp[j] = lv1;
666
                                                    cur_lp[j+1] = lv2;
667
                                                }
668
                                                cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
669
                                                cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
670
                                            } else {
671
                                                for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
672
                                                    cur_lp[j] = lv1;
673
                                                    cur_lp[j+1] = lv2;
674
                                                }
675
                                            }
676
                                        }
677
                                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
678
                                        break;
679
                                    } else {
680
                                        rle_v1 = 1;
681
                                        rle_v2 = (*buf1) - 1;
682
                                    }
683
                                case 5:
684
                                    LP2_CHECK(buf1,rle_v3,lp2)
685
                                case 4:
686
                                    if(lp2 == 0 && flag1 != 0) {
687
                                        for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
688
                                            cur_lp[j] = lv1;
689
                                            cur_lp[j+1] = lv2;
690
                                        }
691
                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
692
                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
693
                                    } else {
694
                                        for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
695
                                            cur_lp[j] = lv1;
696
                                            cur_lp[j+1] = lv2;
697
                                        }
698
                                    }
699
                                    lp2 = 4;
700
                                    break;
701

    
702
                                case 6:
703
                                    lp2 = 4;
704
                                    break;
705

    
706
                                case 7:
707
                                    if(lp2 == 0) {
708
                                        if(rle_v3 != 0)
709
                                            rle_v3 = 0;
710
                                        else {
711
                                            buf1--;
712
                                            rle_v3 = 1;
713
                                        }
714
                                        lp2 = 4;
715
                                    }
716
                                    break;
717

    
718
                                case 9:
719
                                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
720
                                    lv1 = *buf1;
721
                                    lv = (lv1 & 0x7F) << 1;
722
                                    lv += (lv << 8);
723
                                    lv += (lv << 16);
724
                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
725
                                        cur_lp[j] = lv;
726
                                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
727
                                    break;
728

    
729
                                default:
730
                                    return;
731
                                }
732
                            }
733

    
734
                            cur_frm_pos += 8;
735
                        }
736

    
737
                        cur_frm_pos += (((width * 2) - blks_width) * 4);
738
                        flag1 = 0;
739
                    }
740
                } else {
741
                    for( ; blks_height > 0; blks_height -= 8) {
742
                        for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
743
                            for(lp2 = 0; lp2 < 4; ) {
744
                                k = *buf1++;
745
                                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
746
                                ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
747

    
748
                                switch(correction_type_sp[lp2 & 0x01][k]) {
749
                                case 0:
750
                                    lv1 = correctionloworder_lp[lp2 & 0x01][k];
751
                                    lv2 = correctionhighorder_lp[lp2 & 0x01][k];
752
                                    cur_lp[0] = av_le2ne32(((av_le2ne32(ref_lp[0]) >> 1) + lv1) << 1);
753
                                    cur_lp[1] = av_le2ne32(((av_le2ne32(ref_lp[1]) >> 1) + lv2) << 1);
754
                                    cur_lp[width_tbl[1]] = av_le2ne32(((av_le2ne32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
755
                                    cur_lp[width_tbl[1]+1] = av_le2ne32(((av_le2ne32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
756
                                    lp2++;
757
                                    break;
758

    
759
                                case 1:
760
                                    lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
761
                                    lv2 = correctionloworder_lp[lp2 & 0x01][k];
762
                                    cur_lp[0] = av_le2ne32(((av_le2ne32(ref_lp[0]) >> 1) + lv1) << 1);
763
                                    cur_lp[1] = av_le2ne32(((av_le2ne32(ref_lp[1]) >> 1) + lv2) << 1);
764
                                    cur_lp[width_tbl[1]] = av_le2ne32(((av_le2ne32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
765
                                    cur_lp[width_tbl[1]+1] = av_le2ne32(((av_le2ne32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
766
                                    lp2++;
767
                                    break;
768

    
769
                                case 2:
770
                                    if(lp2 == 0) {
771
                                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
772
                                            cur_lp[j] = ref_lp[j];
773
                                            cur_lp[j+1] = ref_lp[j+1];
774
                                        }
775
                                        lp2 += 2;
776
                                    }
777
                                    break;
778

    
779
                                case 3:
780
                                    if(lp2 < 2) {
781
                                        for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
782
                                            cur_lp[j] = ref_lp[j];
783
                                            cur_lp[j+1] = ref_lp[j+1];
784
                                        }
785
                                        lp2 = 3;
786
                                    }
787
                                    break;
788

    
789
                                case 8:
790
                                    if(lp2 == 0) {
791
                                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
792
                                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
793
                                            ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
794
                                            ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];
795
                                        }
796
                                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
797
                                        break;
798
                                    } else {
799
                                        rle_v1 = 1;
800
                                        rle_v2 = (*buf1) - 1;
801
                                    }
802
                                case 5:
803
                                case 7:
804
                                    LP2_CHECK(buf1,rle_v3,lp2)
805
                                case 6:
806
                                case 4:
807
                                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
808
                                        cur_lp[j] = ref_lp[j];
809
                                        cur_lp[j+1] = ref_lp[j+1];
810
                                    }
811
                                    lp2 = 4;
812
                                    break;
813

    
814
                                case 9:
815
                                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
816
                                    lv1 = *buf1;
817
                                    lv = (lv1 & 0x7F) << 1;
818
                                    lv += (lv << 8);
819
                                    lv += (lv << 16);
820
                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
821
                                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
822
                                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
823
                                    break;
824

    
825
                                default:
826
                                    return;
827
                                }
828
                            }
829

    
830
                            cur_frm_pos += 8;
831
                            ref_frm_pos += 8;
832
                        }
833

    
834
                        cur_frm_pos += (((width * 2) - blks_width) * 4);
835
                        ref_frm_pos += (((width * 2) - blks_width) * 4);
836
                    }
837
                }
838
                break;
839

    
840
            case 11:                    /********** CASE 11 **********/
841
                if(ref_vectors == NULL)
842
                    return;
843

    
844
                for( ; blks_height > 0; blks_height -= 8) {
845
                    for(lp1 = 0; lp1 < blks_width; lp1++) {
846
                        for(lp2 = 0; lp2 < 4; ) {
847
                            k = *buf1++;
848
                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
849
                            ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
850

    
851
                            switch(correction_type_sp[lp2 & 0x01][k]) {
852
                            case 0:
853
                                cur_lp[0] = av_le2ne32(((av_le2ne32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
854
                                cur_lp[width_tbl[1]] = av_le2ne32(((av_le2ne32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
855
                                lp2++;
856
                                break;
857

    
858
                            case 1:
859
                                lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
860
                                lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
861
                                res = (unsigned short)(((av_le2ne16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1);
862
                                ((unsigned short *)cur_lp)[0] = av_le2ne16(res);
863
                                res = (unsigned short)(((av_le2ne16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1);
864
                                ((unsigned short *)cur_lp)[1] = av_le2ne16(res);
865
                                res = (unsigned short)(((av_le2ne16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1);
866
                                ((unsigned short *)cur_lp)[width_tbl[2]] = av_le2ne16(res);
867
                                res = (unsigned short)(((av_le2ne16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1);
868
                                ((unsigned short *)cur_lp)[width_tbl[2]+1] = av_le2ne16(res);
869
                                lp2++;
870
                                break;
871

    
872
                            case 2:
873
                                if(lp2 == 0) {
874
                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
875
                                        cur_lp[j] = ref_lp[j];
876
                                    lp2 += 2;
877
                                }
878
                                break;
879

    
880
                            case 3:
881
                                if(lp2 < 2) {
882
                                    for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
883
                                        cur_lp[j] = ref_lp[j];
884
                                    lp2 = 3;
885
                                }
886
                                break;
887

    
888
                            case 8:
889
                                if(lp2 == 0) {
890
                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
891

    
892
                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
893
                                        cur_lp[j] = ref_lp[j];
894

    
895
                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
896
                                    break;
897
                                } else {
898
                                    rle_v1 = 1;
899
                                    rle_v2 = (*buf1) - 1;
900
                                }
901
                            case 5:
902
                            case 7:
903
                                LP2_CHECK(buf1,rle_v3,lp2)
904
                            case 4:
905
                            case 6:
906
                                for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
907
                                    cur_lp[j] = ref_lp[j];
908
                                lp2 = 4;
909
                                break;
910

    
911
                            case 9:
912
                                av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
913
                                lv1 = *buf1++;
914
                                lv = (lv1 & 0x7F) << 1;
915
                                lv += (lv << 8);
916
                                lv += (lv << 16);
917
                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
918
                                    cur_lp[j] = lv;
919
                                LV1_CHECK(buf1,rle_v3,lv1,lp2)
920
                                break;
921

    
922
                            default:
923
                                return;
924
                            }
925
                        }
926

    
927
                        cur_frm_pos += 4;
928
                        ref_frm_pos += 4;
929
                    }
930

    
931
                    cur_frm_pos += (((width * 2) - blks_width) * 4);
932
                    ref_frm_pos += (((width * 2) - blks_width) * 4);
933
                }
934
                break;
935

    
936
            default:
937
                return;
938
            }
939
        }
940

    
941
        for( ; strip >= strip_tbl; strip--) {
942
            if(strip->split_flag != 0) {
943
                strip->split_flag = 0;
944
                strip->usl7 = (strip-1)->usl7;
945

    
946
                if(strip->split_direction) {
947
                    strip->xpos += strip->width;
948
                    strip->width = (strip-1)->width - strip->width;
949
                    if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
950
                        strip->width = width - strip->xpos;
951
                } else {
952
                    strip->ypos += strip->height;
953
                    strip->height = (strip-1)->height - strip->height;
954
                }
955
                break;
956
            }
957
        }
958
    }
959
}
960

    
961
static av_cold int indeo3_decode_init(AVCodecContext *avctx)
962
{
963
    Indeo3DecodeContext *s = avctx->priv_data;
964
    int ret = 0;
965

    
966
    s->avctx = avctx;
967
    s->width = avctx->width;
968
    s->height = avctx->height;
969
    avctx->pix_fmt = PIX_FMT_YUV410P;
970

    
971
    if (!(ret = build_modpred(s)))
972
        ret = iv_alloc_frames(s);
973
    if (ret)
974
        iv_free_func(s);
975

    
976
    return ret;
977
}
978

    
979
static int iv_decode_frame(AVCodecContext *avctx,
980
                           const uint8_t *buf, int buf_size)
981
{
982
    Indeo3DecodeContext *s = avctx->priv_data;
983
    unsigned int image_width, image_height,
984
                 chroma_width, chroma_height;
985
    unsigned long flags, cb_offset, data_size,
986
                  y_offset, v_offset, u_offset, mc_vector_count;
987
    const uint8_t *hdr_pos, *buf_pos;
988

    
989
    buf_pos = buf;
990
    buf_pos += 18; /* skip OS header (16 bytes) and version number */
991

    
992
    flags = bytestream_get_le16(&buf_pos);
993
    data_size = bytestream_get_le32(&buf_pos);
994
    cb_offset = *buf_pos++;
995
    buf_pos += 3; /* skip reserved byte and checksum */
996
    image_height = bytestream_get_le16(&buf_pos);
997
    image_width  = bytestream_get_le16(&buf_pos);
998

    
999
    if(av_image_check_size(image_width, image_height, 0, avctx))
1000
        return -1;
1001
    if (image_width != avctx->width || image_height != avctx->height) {
1002
        int ret;
1003
        avcodec_set_dimensions(avctx, image_width, image_height);
1004
        s->width  = avctx->width;
1005
        s->height = avctx->height;
1006
        ret = iv_alloc_frames(s);
1007
        if (ret < 0) {
1008
            s->width = s->height = 0;
1009
            return ret;
1010
        }
1011
    }
1012

    
1013
    chroma_height = ((image_height >> 2) + 3) & 0x7ffc;
1014
    chroma_width = ((image_width >> 2) + 3) & 0x7ffc;
1015
    y_offset = bytestream_get_le32(&buf_pos);
1016
    v_offset = bytestream_get_le32(&buf_pos);
1017
    u_offset = bytestream_get_le32(&buf_pos);
1018
    buf_pos += 4; /* reserved */
1019
    hdr_pos = buf_pos;
1020
    if(data_size == 0x80) return 4;
1021

    
1022
    if(FFMAX3(y_offset, v_offset, u_offset) >= buf_size-16) {
1023
        av_log(s->avctx, AV_LOG_ERROR, "y/u/v offset outside buffer\n");
1024
        return -1;
1025
    }
1026

    
1027
    if(flags & 0x200) {
1028
        s->cur_frame = s->iv_frame + 1;
1029
        s->ref_frame = s->iv_frame;
1030
    } else {
1031
        s->cur_frame = s->iv_frame;
1032
        s->ref_frame = s->iv_frame + 1;
1033
    }
1034

    
1035
    buf_pos = buf + 16 + y_offset;
1036
    mc_vector_count = bytestream_get_le32(&buf_pos);
1037
    if(2LL*mc_vector_count >= buf_size-16-y_offset) {
1038
        av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
1039
        return -1;
1040
    }
1041

    
1042
    iv_Decode_Chunk(s, s->cur_frame->Ybuf, s->ref_frame->Ybuf, image_width,
1043
                    image_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
1044
                    FFMIN(image_width, 160));
1045

    
1046
    if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1047
    {
1048

    
1049
        buf_pos = buf + 16 + v_offset;
1050
        mc_vector_count = bytestream_get_le32(&buf_pos);
1051
        if(2LL*mc_vector_count >= buf_size-16-v_offset) {
1052
            av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
1053
            return -1;
1054
        }
1055

    
1056
        iv_Decode_Chunk(s, s->cur_frame->Vbuf, s->ref_frame->Vbuf, chroma_width,
1057
                chroma_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
1058
                FFMIN(chroma_width, 40));
1059

    
1060
        buf_pos = buf + 16 + u_offset;
1061
        mc_vector_count = bytestream_get_le32(&buf_pos);
1062
        if(2LL*mc_vector_count >= buf_size-16-u_offset) {
1063
            av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
1064
            return -1;
1065
        }
1066

    
1067
        iv_Decode_Chunk(s, s->cur_frame->Ubuf, s->ref_frame->Ubuf, chroma_width,
1068
                chroma_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
1069
                FFMIN(chroma_width, 40));
1070

    
1071
    }
1072

    
1073
    return 8;
1074
}
1075

    
1076
static int indeo3_decode_frame(AVCodecContext *avctx,
1077
                               void *data, int *data_size,
1078
                               AVPacket *avpkt)
1079
{
1080
    const uint8_t *buf = avpkt->data;
1081
    int buf_size = avpkt->size;
1082
    Indeo3DecodeContext *s=avctx->priv_data;
1083
    uint8_t *src, *dest;
1084
    int y;
1085

    
1086
    if (iv_decode_frame(avctx, buf, buf_size) < 0)
1087
        return -1;
1088

    
1089
    if(s->frame.data[0])
1090
        avctx->release_buffer(avctx, &s->frame);
1091

    
1092
    s->frame.reference = 0;
1093
    if(avctx->get_buffer(avctx, &s->frame) < 0) {
1094
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1095
        return -1;
1096
    }
1097

    
1098
    src = s->cur_frame->Ybuf;
1099
    dest = s->frame.data[0];
1100
    for (y = 0; y < s->height; y++) {
1101
        memcpy(dest, src, s->cur_frame->y_w);
1102
        src += s->cur_frame->y_w;
1103
        dest += s->frame.linesize[0];
1104
    }
1105

    
1106
    if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1107
    {
1108
        src = s->cur_frame->Ubuf;
1109
        dest = s->frame.data[1];
1110
        for (y = 0; y < s->height / 4; y++) {
1111
            memcpy(dest, src, s->cur_frame->uv_w);
1112
            src += s->cur_frame->uv_w;
1113
            dest += s->frame.linesize[1];
1114
        }
1115

    
1116
        src = s->cur_frame->Vbuf;
1117
        dest = s->frame.data[2];
1118
        for (y = 0; y < s->height / 4; y++) {
1119
            memcpy(dest, src, s->cur_frame->uv_w);
1120
            src += s->cur_frame->uv_w;
1121
            dest += s->frame.linesize[2];
1122
        }
1123
    }
1124

    
1125
    *data_size=sizeof(AVFrame);
1126
    *(AVFrame*)data= s->frame;
1127

    
1128
    return buf_size;
1129
}
1130

    
1131
static av_cold int indeo3_decode_end(AVCodecContext *avctx)
1132
{
1133
    Indeo3DecodeContext *s = avctx->priv_data;
1134

    
1135
    iv_free_func(s);
1136

    
1137
    return 0;
1138
}
1139

    
1140
AVCodec ff_indeo3_decoder = {
1141
    "indeo3",
1142
    AVMEDIA_TYPE_VIDEO,
1143
    CODEC_ID_INDEO3,
1144
    sizeof(Indeo3DecodeContext),
1145
    indeo3_decode_init,
1146
    NULL,
1147
    indeo3_decode_end,
1148
    indeo3_decode_frame,
1149
    CODEC_CAP_DR1,
1150
    NULL,
1151
    .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
1152
};