indeo3.c 47 KB
Newer Older
1 2 3 4
/*
 * Intel Indeo 3 (IV31, IV32, etc.) video decoder for ffmpeg
 * written, produced, and directed by Alan Smithee
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 23 24 25 26 27 28
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "avcodec.h"
#include "dsputil.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
29
#include "bytestream.h"
30 31 32 33 34

#include "indeo3data.h"

typedef struct
{
Benoit Fouet's avatar
Benoit Fouet committed
35 36 37 38 39
    uint8_t *Ybuf;
    uint8_t *Ubuf;
    uint8_t *Vbuf;
    unsigned short y_w, y_h;
    unsigned short uv_w, uv_h;
40 41 42 43 44 45 46
} YUVBufs;

typedef struct Indeo3DecodeContext {
    AVCodecContext *avctx;
    int width, height;
    AVFrame frame;

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

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

56
static const uint8_t corrector_type_0[24] = {
Benoit Fouet's avatar
Benoit Fouet committed
57 58 59
    195, 159, 133, 115, 101,  93,  87,  77,
    195, 159, 133, 115, 101,  93,  87,  77,
    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
{
Benoit Fouet's avatar
Benoit Fouet committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    int i, j;

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

    for (i=0; i < 128; ++i) {
        s->ModPred[i+0*128] = i >  126 ? 254 : 2*(i + 1 - ((i + 1) % 2));
        s->ModPred[i+1*128] = i ==   7 ?  20 :
                              i == 119 ||
                              i == 120 ? 236 : 2*(i + 2 - ((i + 1) % 3));
        s->ModPred[i+2*128] = i >  125 ? 248 : 2*(i + 2 - ((i + 2) % 4));
        s->ModPred[i+3*128] =                  2*(i + 1 - ((i - 3) % 5));
        s->ModPred[i+4*128] = i ==   8 ?  20 : 2*(i + 1 - ((i - 3) % 6));
        s->ModPred[i+5*128] =                  2*(i + 4 - ((i + 3) % 7));
        s->ModPred[i+6*128] = i >  123 ? 240 : 2*(i + 4 - ((i + 4) % 8));
        s->ModPred[i+7*128] =                  2*(i + 5 - ((i + 4) % 9));
    }

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

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

  return 0;
96 97
}

98
static av_cold int iv_alloc_frames(Indeo3DecodeContext *s)
99
{
100 101 102 103 104 105 106 107 108
    int luma_width    = (s->width           + 3) & ~3,
        luma_height   = (s->height          + 3) & ~3,
        chroma_width  = ((luma_width  >> 2) + 3) & ~3,
        chroma_height = ((luma_height >> 2) + 3) & ~3,
        luma_pixels   = luma_width   * luma_height,
        chroma_pixels = chroma_width * chroma_height,
        i;
    unsigned int bufsize = luma_pixels * 2 + luma_width * 3 +
                          (chroma_pixels   + chroma_width) * 4;
Benoit Fouet's avatar
Benoit Fouet committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    if(!(s->buf = av_malloc(bufsize)))
        return AVERROR(ENOMEM);
    s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
    s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
    s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
    s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;

    s->iv_frame[0].Ybuf = s->buf + luma_width;
    i = luma_pixels + luma_width * 2;
    s->iv_frame[1].Ybuf = s->buf + i;
    i += (luma_pixels + luma_width);
    s->iv_frame[0].Ubuf = s->buf + i;
    i += (chroma_pixels + chroma_width);
    s->iv_frame[1].Ubuf = s->buf + i;
    i += (chroma_pixels + chroma_width);
    s->iv_frame[0].Vbuf = s->buf + i;
    i += (chroma_pixels + chroma_width);
    s->iv_frame[1].Vbuf = s->buf + i;

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

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

Benoit Fouet's avatar
Benoit Fouet committed
140
    return 0;
141 142
}

143
static av_cold void iv_free_func(Indeo3DecodeContext *s)
144
{
Benoit Fouet's avatar
Benoit Fouet committed
145 146 147
    av_free(s->buf);
    av_free(s->ModPred);
    av_free(s->corrector_type);
148 149
}

150
struct ustr {
Benoit Fouet's avatar
Benoit Fouet committed
151 152 153 154 155 156 157
    long xpos;
    long ypos;
    long width;
    long height;
    long split_flag;
    long split_direction;
    long usl7;
158
};
159 160


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


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


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


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

203
static void iv_Decode_Chunk(Indeo3DecodeContext *s,
Benoit Fouet's avatar
Benoit Fouet committed
204
        uint8_t *cur, uint8_t *ref, int width, int height,
205
        const uint8_t *buf1, long cb_offset, const uint8_t *hdr,
Benoit Fouet's avatar
Benoit Fouet committed
206
        const uint8_t *buf2, int min_width_160)
207
{
Benoit Fouet's avatar
Benoit Fouet committed
208 209 210 211 212 213 214 215
    uint8_t bit_buf;
    unsigned long bit_pos, lv, lv1, lv2;
    long *width_tbl, width_tbl_arr[10];
    const signed char *ref_vectors;
    uint8_t *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
    uint32_t *cur_lp, *ref_lp;
    const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
    uint8_t *correction_type_sp[2];
216
    struct ustr strip_tbl[20], *strip;
Benoit Fouet's avatar
Benoit Fouet committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
        rle_v1, rle_v2, rle_v3;
    unsigned short res;

    bit_buf = 0;
    ref_vectors = NULL;

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

    strip = strip_tbl;

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

    strip->ypos = strip->xpos = 0;
    for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
    strip->height = height;
    strip->split_direction = 0;
    strip->split_flag = 0;
    strip->usl7 = 0;

    bit_pos = 0;

    rle_v1 = rle_v2 = rle_v3 = 0;

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

Benoit Fouet's avatar
Benoit Fouet committed
250 251 252 253 254
        bit_pos -= 2;
        cmd = (bit_buf >> bit_pos) & 0x03;

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

Benoit Fouet's avatar
Benoit Fouet committed
290
        cur_frm_pos = cur + width * strip->ypos + strip->xpos;
291

Benoit Fouet's avatar
Benoit Fouet committed
292 293 294 295
        if((blks_width = strip->width) < 0)
            blks_width += 3;
        blks_width >>= 2;
        blks_height = strip->height;
296

Benoit Fouet's avatar
Benoit Fouet committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        if(ref_vectors != NULL) {
            ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
                ref_vectors[1] + strip->xpos;
        } else
            ref_frm_pos = cur_frm_pos - width_tbl[4];

        if(cmd == 2) {
            if(bit_pos <= 0) {
                bit_pos = 8;
                bit_buf = *buf1++;
            }

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

            if(cmd == 0 || ref_vectors != NULL) {
                for(lp1 = 0; lp1 < blks_width; lp1++) {
                    for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
                    cur_frm_pos += 4;
                    ref_frm_pos += 4;
                }
            } else if(cmd != 1)
                return;
        } else {
            k = *buf1 >> 4;
            j = *buf1 & 0x0f;
            buf1++;
325
            lv = j + cb_offset;
Benoit Fouet's avatar
Benoit Fouet committed
326 327 328 329 330 331 332

            if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
                cp2 = s->ModPred + ((lv - 8) << 7);
                cp = ref_frm_pos;
                for(i = 0; i < blks_width << 2; i++) {
                    int v = *cp >> 1;
                    *(cp++) = cp2[v];
333
                }
Benoit Fouet's avatar
Benoit Fouet committed
334
            }
335

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

Benoit Fouet's avatar
Benoit Fouet committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
            switch(k) {
            case 1:
            case 0:                    /********** CASE 0 **********/
                for( ; blks_height > 0; blks_height -= 4) {
                    for(lp1 = 0; lp1 < blks_width; lp1++) {
                        for(lp2 = 0; lp2 < 4; ) {
                            k = *buf1++;
                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
                            ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];

                            switch(correction_type_sp[0][k]) {
                            case 0:
                                *cur_lp = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
                                lp2++;
                                break;
                            case 1:
                                res = ((le2me_16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
                                ((unsigned short *)cur_lp)[0] = le2me_16(res);
                                res = ((le2me_16(((unsigned short *)(ref_lp))[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
                                ((unsigned short *)cur_lp)[1] = le2me_16(res);
                                buf1++;
                                lp2++;
                                break;
                            case 2:
                                if(lp2 == 0) {
                                    for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
                                        cur_lp[j] = ref_lp[j];
                                    lp2 += 2;
                                }
                                break;
                            case 3:
                                if(lp2 < 2) {
                                    for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
                                        cur_lp[j] = ref_lp[j];
                                    lp2 = 3;
                                }
                                break;
                            case 8:
                                if(lp2 == 0) {
                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)

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

                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
Benoit Fouet's avatar
Benoit Fouet committed
397
                                    break;
Benoit Fouet's avatar
Benoit Fouet committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
                                } else {
                                    rle_v1 = 1;
                                    rle_v2 = *buf1 - 1;
                                }
                            case 5:
                                LP2_CHECK(buf1,rle_v3,lp2)
                            case 4:
                                for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
                                    cur_lp[j] = ref_lp[j];
                                lp2 = 4;
                                break;

                            case 7:
                                if(rle_v3 != 0)
                                    rle_v3 = 0;
                                else {
                                    buf1--;
                                    rle_v3 = 1;
                                }
                            case 6:
                                if(ref_vectors != NULL) {
                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
                                        cur_lp[j] = ref_lp[j];
                                }
                                lp2 = 4;
                                break;

                            case 9:
                                lv1 = *buf1++;
                                lv = (lv1 & 0x7F) << 1;
                                lv += (lv << 8);
                                lv += (lv << 16);
                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
                                    cur_lp[j] = lv;

                                LV1_CHECK(buf1,rle_v3,lv1,lp2)
Benoit Fouet's avatar
Benoit Fouet committed
434
                                break;
Benoit Fouet's avatar
Benoit Fouet committed
435 436
                            default:
                                return;
437 438 439
                            }
                        }

Benoit Fouet's avatar
Benoit Fouet committed
440 441 442
                        cur_frm_pos += 4;
                        ref_frm_pos += 4;
                    }
443

Benoit Fouet's avatar
Benoit Fouet committed
444 445
                    cur_frm_pos += ((width - blks_width) * 4);
                    ref_frm_pos += ((width - blks_width) * 4);
446
                }
Benoit Fouet's avatar
Benoit Fouet committed
447
                break;
448

Benoit Fouet's avatar
Benoit Fouet committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
            case 4:
            case 3:                    /********** CASE 3 **********/
                if(ref_vectors != NULL)
                    return;
                flag1 = 1;

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

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

                            switch(correction_type_sp[lp2 & 0x01][k]) {
                            case 0:
                                cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
                                if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
                                    cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                else
                                    cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
                                lp2++;
                                break;

                            case 1:
                                res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
                                ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
                                res = ((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
                                ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);

                                if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
                                    cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                else
                                    cur_lp[0] = cur_lp[width_tbl[1]];
                                buf1++;
                                lp2++;
                                break;

                            case 2:
                                if(lp2 == 0) {
                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
                                        cur_lp[j] = *ref_lp;
                                    lp2 += 2;
                                }
                                break;

                            case 3:
                                if(lp2 < 2) {
                                    for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
                                        cur_lp[j] = *ref_lp;
                                    lp2 = 3;
                                }
                                break;

                            case 6:
                                lp2 = 4;
                                break;

                            case 7:
                                if(rle_v3 != 0)
                                    rle_v3 = 0;
                                else {
                                    buf1--;
                                    rle_v3 = 1;
                                }
                                lp2 = 4;
                                break;

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

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

                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
                                    break;
                                } else {
                                    rle_v2 = (*buf1) - 1;
                                    rle_v1 = 1;
                                }
                            case 5:
                                LP2_CHECK(buf1,rle_v3,lp2)
                            case 4:
                                for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
                                    cur_lp[j] = *ref_lp;
                                lp2 = 4;
                                break;

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

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

                                LV1_CHECK(buf1,rle_v3,lv1,lp2)
                                break;

                            default:
                                return;
                            }
                        }
557

Benoit Fouet's avatar
Benoit Fouet committed
558 559
                        cur_frm_pos += 4;
                    }
560

Benoit Fouet's avatar
Benoit Fouet committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
                    cur_frm_pos += (((width * 2) - blks_width) * 4);
                    flag1 = 0;
                }
                break;

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

                    for( ; blks_height > 0; blks_height -= 8) {
                        for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
                            for(lp2 = 0; lp2 < 4; ) {
                                k = *buf1++;
                                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
                                ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
                                lv1 = ref_lp[0];
                                lv2 = ref_lp[1];
                                if(lp2 == 0 && flag1 != 0) {
#ifdef WORDS_BIGENDIAN
                                    lv1 = lv1 & 0xFF00FF00;
                                    lv1 = (lv1 >> 8) | lv1;
                                    lv2 = lv2 & 0xFF00FF00;
                                    lv2 = (lv2 >> 8) | lv2;
#else
                                    lv1 = lv1 & 0x00FF00FF;
                                    lv1 = (lv1 << 8) | lv1;
                                    lv2 = lv2 & 0x00FF00FF;
                                    lv2 = (lv2 << 8) | lv2;
#endif
                                }

                                switch(correction_type_sp[lp2 & 0x01][k]) {
                                case 0:
                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1);
                                    if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
                                    } else {
                                        cur_lp[0] = cur_lp[width_tbl[1]];
                                        cur_lp[1] = cur_lp[width_tbl[1]+1];
                                    }
                                    lp2++;
                                    break;

                                case 1:
                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1);
                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
                                    if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
                                    } else {
                                        cur_lp[0] = cur_lp[width_tbl[1]];
                                        cur_lp[1] = cur_lp[width_tbl[1]+1];
                                    }
                                    buf1++;
                                    lp2++;
                                    break;

                                case 2:
                                    if(lp2 == 0) {
                                        if(flag1 != 0) {
                                            for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
                                                cur_lp[j] = lv1;
                                                cur_lp[j+1] = lv2;
                                            }
                                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
                                        } else {
                                            for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
                                                cur_lp[j] = lv1;
                                                cur_lp[j+1] = lv2;
                                            }
                                        }
                                        lp2 += 2;
                                    }
                                    break;

                                case 3:
                                    if(lp2 < 2) {
                                        if(lp2 == 0 && flag1 != 0) {
                                            for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
                                                cur_lp[j] = lv1;
                                                cur_lp[j+1] = lv2;
                                            }
                                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
                                        } else {
                                            for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
                                                cur_lp[j] = lv1;
                                                cur_lp[j+1] = lv2;
                                            }
                                        }
                                        lp2 = 3;
                                    }
                                    break;

                                case 8:
                                    if(lp2 == 0) {
                                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
                                        if(rle_v1 == 1) {
                                            if(flag1 != 0) {
                                                for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
                                                    cur_lp[j] = lv1;
                                                    cur_lp[j+1] = lv2;
                                                }
                                                cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                                cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
                                            } else {
                                                for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
                                                    cur_lp[j] = lv1;
                                                    cur_lp[j+1] = lv2;
                                                }
                                            }
                                        }
                                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
                                        break;
                                    } else {
                                        rle_v1 = 1;
                                        rle_v2 = (*buf1) - 1;
                                    }
                                case 5:
                                    LP2_CHECK(buf1,rle_v3,lp2)
                                case 4:
                                    if(lp2 == 0 && flag1 != 0) {
                                        for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
                                            cur_lp[j] = lv1;
                                            cur_lp[j+1] = lv2;
                                        }
                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
                                    } else {
                                        for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
                                            cur_lp[j] = lv1;
                                            cur_lp[j+1] = lv2;
                                        }
                                    }
                                    lp2 = 4;
                                    break;

                                case 6:
                                    lp2 = 4;
                                    break;

                                case 7:
                                    if(lp2 == 0) {
                                        if(rle_v3 != 0)
                                            rle_v3 = 0;
                                        else {
                                            buf1--;
                                            rle_v3 = 1;
                                        }
                                        lp2 = 4;
                                    }
                                    break;

                                case 9:
                                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
                                    lv1 = *buf1;
                                    lv = (lv1 & 0x7F) << 1;
                                    lv += (lv << 8);
                                    lv += (lv << 16);
                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
                                        cur_lp[j] = lv;
                                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
                                    break;

                                default:
                                    return;
                                }
                            }
732

Benoit Fouet's avatar
Benoit Fouet committed
733 734
                            cur_frm_pos += 8;
                        }
735

Benoit Fouet's avatar
Benoit Fouet committed
736 737
                        cur_frm_pos += (((width * 2) - blks_width) * 4);
                        flag1 = 0;
738
                    }
Benoit Fouet's avatar
Benoit Fouet committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
                } else {
                    for( ; blks_height > 0; blks_height -= 8) {
                        for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
                            for(lp2 = 0; lp2 < 4; ) {
                                k = *buf1++;
                                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
                                ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];

                                switch(correction_type_sp[lp2 & 0x01][k]) {
                                case 0:
                                    lv1 = correctionloworder_lp[lp2 & 0x01][k];
                                    lv2 = correctionhighorder_lp[lp2 & 0x01][k];
                                    cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
                                    cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
                                    lp2++;
                                    break;

                                case 1:
                                    lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
                                    lv2 = correctionloworder_lp[lp2 & 0x01][k];
                                    cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
                                    cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
                                    lp2++;
                                    break;

                                case 2:
                                    if(lp2 == 0) {
                                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
                                            cur_lp[j] = ref_lp[j];
                                            cur_lp[j+1] = ref_lp[j+1];
                                        }
                                        lp2 += 2;
                                    }
                                    break;

                                case 3:
                                    if(lp2 < 2) {
                                        for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
                                            cur_lp[j] = ref_lp[j];
                                            cur_lp[j+1] = ref_lp[j+1];
                                        }
                                        lp2 = 3;
                                    }
                                    break;

                                case 8:
                                    if(lp2 == 0) {
                                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
                                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
                                            ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
                                            ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];
                                        }
                                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
                                        break;
                                    } else {
                                        rle_v1 = 1;
                                        rle_v2 = (*buf1) - 1;
                                    }
                                case 5:
                                case 7:
                                    LP2_CHECK(buf1,rle_v3,lp2)
                                case 6:
                                case 4:
                                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
                                        cur_lp[j] = ref_lp[j];
                                        cur_lp[j+1] = ref_lp[j+1];
                                    }
                                    lp2 = 4;
                                    break;

                                case 9:
                                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
                                    lv1 = *buf1;
                                    lv = (lv1 & 0x7F) << 1;
                                    lv += (lv << 8);
                                    lv += (lv << 16);
                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
                                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
                                    LV1_CHECK(buf1,rle_v3,lv1,lp2)
                                    break;

                                default:
                                    return;
                                }
                            }
828

Benoit Fouet's avatar
Benoit Fouet committed
829 830 831
                            cur_frm_pos += 8;
                            ref_frm_pos += 8;
                        }
832

Benoit Fouet's avatar
Benoit Fouet committed
833 834
                        cur_frm_pos += (((width * 2) - blks_width) * 4);
                        ref_frm_pos += (((width * 2) - blks_width) * 4);
835 836
                    }
                }
Benoit Fouet's avatar
Benoit Fouet committed
837
                break;
838

Benoit Fouet's avatar
Benoit Fouet committed
839 840 841
            case 11:                    /********** CASE 11 **********/
                if(ref_vectors == NULL)
                    return;
842

Benoit Fouet's avatar
Benoit Fouet committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
                for( ; blks_height > 0; blks_height -= 8) {
                    for(lp1 = 0; lp1 < blks_width; lp1++) {
                        for(lp2 = 0; lp2 < 4; ) {
                            k = *buf1++;
                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
                            ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];

                            switch(correction_type_sp[lp2 & 0x01][k]) {
                            case 0:
                                cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
                                cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
                                lp2++;
                                break;

                            case 1:
                                lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
                                lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1);
                                ((unsigned short *)cur_lp)[0] = le2me_16(res);
                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1);
                                ((unsigned short *)cur_lp)[1] = le2me_16(res);
                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1);
                                ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1);
                                ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
                                lp2++;
                                break;

                            case 2:
                                if(lp2 == 0) {
                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
                                        cur_lp[j] = ref_lp[j];
                                    lp2 += 2;
                                }
                                break;

                            case 3:
                                if(lp2 < 2) {
                                    for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
                                        cur_lp[j] = ref_lp[j];
                                    lp2 = 3;
                                }
                                break;

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

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

                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
                                    break;
                                } else {
                                    rle_v1 = 1;
                                    rle_v2 = (*buf1) - 1;
                                }
                            case 5:
                            case 7:
                                LP2_CHECK(buf1,rle_v3,lp2)
                            case 4:
                            case 6:
                                for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
                                    cur_lp[j] = ref_lp[j];
                                lp2 = 4;
                                break;

                            case 9:
                                av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
                                lv1 = *buf1++;
                                lv = (lv1 & 0x7F) << 1;
                                lv += (lv << 8);
                                lv += (lv << 16);
                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
                                    cur_lp[j] = lv;
                                LV1_CHECK(buf1,rle_v3,lv1,lp2)
                                break;

                            default:
                                return;
                            }
                        }
925

Benoit Fouet's avatar
Benoit Fouet committed
926 927 928
                        cur_frm_pos += 4;
                        ref_frm_pos += 4;
                    }
929

Benoit Fouet's avatar
Benoit Fouet committed
930 931 932 933 934 935 936 937 938
                    cur_frm_pos += (((width * 2) - blks_width) * 4);
                    ref_frm_pos += (((width * 2) - blks_width) * 4);
                }
                break;

            default:
                return;
            }
        }
939

Benoit Fouet's avatar
Benoit Fouet committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
        for( ; strip >= strip_tbl; strip--) {
            if(strip->split_flag != 0) {
                strip->split_flag = 0;
                strip->usl7 = (strip-1)->usl7;

                if(strip->split_direction) {
                    strip->xpos += strip->width;
                    strip->width = (strip-1)->width - strip->width;
                    if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
                        strip->width = width - strip->xpos;
                } else {
                    strip->ypos += strip->height;
                    strip->height = (strip-1)->height - strip->height;
                }
                break;
            }
956 957 958 959
        }
    }
}

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

    s->avctx = avctx;
    s->width = avctx->width;
    s->height = avctx->height;
    avctx->pix_fmt = PIX_FMT_YUV410P;

970
    if (!(ret = build_modpred(s)))
Benoit Fouet's avatar
Benoit Fouet committed
971
        ret = iv_alloc_frames(s);
972
    if (ret)
Benoit Fouet's avatar
Benoit Fouet committed
973
        iv_free_func(s);
974

975
    return ret;
976 977
}

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

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

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

997
    if(avcodec_check_dimensions(NULL, image_width, image_height))
998 999
        return -1;

1000 1001 1002 1003 1004 1005
    chroma_height = ((image_height >> 2) + 3) & 0x7ffc;
    chroma_width = ((image_width >> 2) + 3) & 0x7ffc;
    y_offset = bytestream_get_le32(&buf_pos);
    v_offset = bytestream_get_le32(&buf_pos);
    u_offset = bytestream_get_le32(&buf_pos);
    buf_pos += 4; /* reserved */
1006
    hdr_pos = buf_pos;
1007
    if(data_size == 0x80) return 4;
1008

1009 1010 1011 1012 1013
    if(FFMAX3(y_offset, v_offset, u_offset) >= buf_size-16) {
        av_log(s->avctx, AV_LOG_ERROR, "y/u/v offset outside buffer\n");
        return -1;
    }

1014
    if(flags & 0x200) {
1015 1016 1017 1018 1019 1020 1021
        s->cur_frame = s->iv_frame + 1;
        s->ref_frame = s->iv_frame;
    } else {
        s->cur_frame = s->iv_frame;
        s->ref_frame = s->iv_frame + 1;
    }

1022 1023
    buf_pos = buf + 16 + y_offset;
    mc_vector_count = bytestream_get_le32(&buf_pos);
1024 1025 1026 1027
    if(2LL*mc_vector_count >= buf_size-16-y_offset) {
        av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
        return -1;
    }
1028

1029 1030 1031
    iv_Decode_Chunk(s, s->cur_frame->Ybuf, s->ref_frame->Ybuf, image_width,
                    image_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
                    FFMIN(image_width, 160));
1032 1033 1034 1035

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

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

        iv_Decode_Chunk(s, s->cur_frame->Vbuf, s->ref_frame->Vbuf, chroma_width,
1044
                chroma_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
1045 1046
                FFMIN(chroma_width, 40));

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

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

    }

    return 8;
}

1063 1064
static int indeo3_decode_frame(AVCodecContext *avctx,
                               void *data, int *data_size,
1065
                               const uint8_t *buf, int buf_size)
1066 1067
{
    Indeo3DecodeContext *s=avctx->priv_data;
1068
    uint8_t *src, *dest;
1069 1070
    int y;

1071 1072
    if (iv_decode_frame(s, buf, buf_size) < 0)
        return -1;
1073

1074 1075 1076
    if(s->frame.data[0])
        avctx->release_buffer(avctx, &s->frame);

1077 1078
    s->frame.reference = 0;
    if(avctx->get_buffer(avctx, &s->frame) < 0) {
1079
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1080 1081 1082 1083 1084 1085
        return -1;
    }

    src = s->cur_frame->Ybuf;
    dest = s->frame.data[0];
    for (y = 0; y < s->height; y++) {
Benoit Fouet's avatar
Benoit Fouet committed
1086 1087 1088
        memcpy(dest, src, s->cur_frame->y_w);
        src += s->cur_frame->y_w;
        dest += s->frame.linesize[0];
1089 1090
    }

1091 1092
    if (!(s->avctx->flags & CODEC_FLAG_GRAY))
    {
Benoit Fouet's avatar
Benoit Fouet committed
1093 1094 1095 1096 1097 1098 1099
        src = s->cur_frame->Ubuf;
        dest = s->frame.data[1];
        for (y = 0; y < s->height / 4; y++) {
            memcpy(dest, src, s->cur_frame->uv_w);
            src += s->cur_frame->uv_w;
            dest += s->frame.linesize[1];
        }
1100

Benoit Fouet's avatar
Benoit Fouet committed
1101 1102 1103 1104 1105 1106 1107
        src = s->cur_frame->Vbuf;
        dest = s->frame.data[2];
        for (y = 0; y < s->height / 4; y++) {
            memcpy(dest, src, s->cur_frame->uv_w);
            src += s->cur_frame->uv_w;
            dest += s->frame.linesize[2];
        }
1108
    }
1109 1110 1111 1112 1113 1114 1115

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

    return buf_size;
}

1116
static av_cold int indeo3_decode_end(AVCodecContext *avctx)
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
{
    Indeo3DecodeContext *s = avctx->priv_data;

    iv_free_func(s);

    return 0;
}

AVCodec indeo3_decoder = {
    "indeo3",
    CODEC_TYPE_VIDEO,
    CODEC_ID_INDEO3,
    sizeof(Indeo3DecodeContext),
    indeo3_decode_init,
    NULL,
    indeo3_decode_end,
    indeo3_decode_frame,
    0,
1135
    NULL,
1136
    .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
1137
};