00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025
00026 #include "avcodec.h"
00027 #include "dsputil.h"
00028 #include "bytestream.h"
00029
00030 #include "indeo3data.h"
00031
00032 typedef struct
00033 {
00034 uint8_t *Ybuf;
00035 uint8_t *Ubuf;
00036 uint8_t *Vbuf;
00037 unsigned short y_w, y_h;
00038 unsigned short uv_w, uv_h;
00039 } YUVBufs;
00040
00041 typedef struct Indeo3DecodeContext {
00042 AVCodecContext *avctx;
00043 int width, height;
00044 AVFrame frame;
00045
00046 uint8_t *buf;
00047 YUVBufs iv_frame[2];
00048 YUVBufs *cur_frame;
00049 YUVBufs *ref_frame;
00050
00051 uint8_t *ModPred;
00052 uint8_t *corrector_type;
00053 } Indeo3DecodeContext;
00054
00055 static const uint8_t corrector_type_0[24] = {
00056 195, 159, 133, 115, 101, 93, 87, 77,
00057 195, 159, 133, 115, 101, 93, 87, 77,
00058 128, 79, 79, 79, 79, 79, 79, 79
00059 };
00060
00061 static const uint8_t corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
00062
00063 static av_cold int build_modpred(Indeo3DecodeContext *s)
00064 {
00065 int i, j;
00066
00067 if (!(s->ModPred = av_malloc(8 * 128)))
00068 return AVERROR(ENOMEM);
00069
00070 for (i=0; i < 128; ++i) {
00071 s->ModPred[i+0*128] = i > 126 ? 254 : 2*(i + 1 - ((i + 1) % 2));
00072 s->ModPred[i+1*128] = i == 7 ? 20 :
00073 i == 119 ||
00074 i == 120 ? 236 : 2*(i + 2 - ((i + 1) % 3));
00075 s->ModPred[i+2*128] = i > 125 ? 248 : 2*(i + 2 - ((i + 2) % 4));
00076 s->ModPred[i+3*128] = 2*(i + 1 - ((i - 3) % 5));
00077 s->ModPred[i+4*128] = i == 8 ? 20 : 2*(i + 1 - ((i - 3) % 6));
00078 s->ModPred[i+5*128] = 2*(i + 4 - ((i + 3) % 7));
00079 s->ModPred[i+6*128] = i > 123 ? 240 : 2*(i + 4 - ((i + 4) % 8));
00080 s->ModPred[i+7*128] = 2*(i + 5 - ((i + 4) % 9));
00081 }
00082
00083 if (!(s->corrector_type = av_malloc(24 * 256)))
00084 return AVERROR(ENOMEM);
00085
00086 for (i=0; i < 24; ++i) {
00087 for (j=0; j < 256; ++j) {
00088 s->corrector_type[i*256+j] = j < corrector_type_0[i] ? 1 :
00089 j < 248 || (i == 16 && j == 248) ? 0 :
00090 corrector_type_2[j - 248];
00091 }
00092 }
00093
00094 return 0;
00095 }
00096
00097 static av_cold int iv_alloc_frames(Indeo3DecodeContext *s)
00098 {
00099 int luma_width = (s->width + 3) & ~3,
00100 luma_height = (s->height + 3) & ~3,
00101 chroma_width = ((luma_width >> 2) + 3) & ~3,
00102 chroma_height = ((luma_height >> 2) + 3) & ~3,
00103 luma_pixels = luma_width * luma_height,
00104 chroma_pixels = chroma_width * chroma_height,
00105 i;
00106 unsigned int bufsize = luma_pixels * 2 + luma_width * 3 +
00107 (chroma_pixels + chroma_width) * 4;
00108
00109 av_freep(&s->buf);
00110 if(!(s->buf = av_malloc(bufsize)))
00111 return AVERROR(ENOMEM);
00112 s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
00113 s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
00114 s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
00115 s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;
00116
00117 s->iv_frame[0].Ybuf = s->buf + luma_width;
00118 i = luma_pixels + luma_width * 2;
00119 s->iv_frame[1].Ybuf = s->buf + i;
00120 i += (luma_pixels + luma_width);
00121 s->iv_frame[0].Ubuf = s->buf + i;
00122 i += (chroma_pixels + chroma_width);
00123 s->iv_frame[1].Ubuf = s->buf + i;
00124 i += (chroma_pixels + chroma_width);
00125 s->iv_frame[0].Vbuf = s->buf + i;
00126 i += (chroma_pixels + chroma_width);
00127 s->iv_frame[1].Vbuf = s->buf + i;
00128
00129 for(i = 1; i <= luma_width; i++)
00130 s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =
00131 s->iv_frame[0].Ubuf[-i] = 0x80;
00132
00133 for(i = 1; i <= chroma_width; i++) {
00134 s->iv_frame[1].Ubuf[-i] = 0x80;
00135 s->iv_frame[0].Vbuf[-i] = 0x80;
00136 s->iv_frame[1].Vbuf[-i] = 0x80;
00137 s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;
00138 }
00139
00140 return 0;
00141 }
00142
00143 static av_cold void iv_free_func(Indeo3DecodeContext *s)
00144 {
00145 av_freep(&s->buf);
00146 av_freep(&s->ModPred);
00147 av_freep(&s->corrector_type);
00148 }
00149
00150 struct ustr {
00151 long xpos;
00152 long ypos;
00153 long width;
00154 long height;
00155 long split_flag;
00156 long split_direction;
00157 long usl7;
00158 };
00159
00160
00161 #define LV1_CHECK(buf1,rle_v3,lv1,lp2) \
00162 if((lv1 & 0x80) != 0) { \
00163 if(rle_v3 != 0) \
00164 rle_v3 = 0; \
00165 else { \
00166 rle_v3 = 1; \
00167 buf1 -= 2; \
00168 } \
00169 } \
00170 lp2 = 4;
00171
00172
00173 #define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3) \
00174 if(rle_v3 == 0) { \
00175 rle_v2 = *buf1; \
00176 rle_v1 = 1; \
00177 if(rle_v2 > 32) { \
00178 rle_v2 -= 32; \
00179 rle_v1 = 0; \
00180 } \
00181 rle_v3 = 1; \
00182 } \
00183 buf1--;
00184
00185
00186 #define LP2_CHECK(buf1,rle_v3,lp2) \
00187 if(lp2 == 0 && rle_v3 != 0) \
00188 rle_v3 = 0; \
00189 else { \
00190 buf1--; \
00191 rle_v3 = 1; \
00192 }
00193
00194
00195 #define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
00196 rle_v2--; \
00197 if(rle_v2 == 0) { \
00198 rle_v3 = 0; \
00199 buf1 += 2; \
00200 } \
00201 lp2 = 4;
00202
00203 static void iv_Decode_Chunk(Indeo3DecodeContext *s,
00204 uint8_t *cur, uint8_t *ref, int width, int height,
00205 const uint8_t *buf1, long cb_offset, const uint8_t *hdr,
00206 const uint8_t *buf2, int min_width_160)
00207 {
00208 uint8_t bit_buf;
00209 unsigned long bit_pos, lv, lv1, lv2;
00210 long *width_tbl, width_tbl_arr[10];
00211 const signed char *ref_vectors;
00212 uint8_t *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
00213 uint32_t *cur_lp, *ref_lp;
00214 const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
00215 uint8_t *correction_type_sp[2];
00216 struct ustr strip_tbl[20], *strip;
00217 int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
00218 rle_v1, rle_v2, rle_v3;
00219 unsigned short res;
00220
00221 bit_buf = 0;
00222 ref_vectors = NULL;
00223
00224 width_tbl = width_tbl_arr + 1;
00225 i = (width < 0 ? width + 3 : width)/4;
00226 for(j = -1; j < 8; j++)
00227 width_tbl[j] = i * j;
00228
00229 strip = strip_tbl;
00230
00231 for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
00232
00233 strip->ypos = strip->xpos = 0;
00234 for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
00235 strip->height = height;
00236 strip->split_direction = 0;
00237 strip->split_flag = 0;
00238 strip->usl7 = 0;
00239
00240 bit_pos = 0;
00241
00242 rle_v1 = rle_v2 = rle_v3 = 0;
00243
00244 while(strip >= strip_tbl) {
00245 if(bit_pos <= 0) {
00246 bit_pos = 8;
00247 bit_buf = *buf1++;
00248 }
00249
00250 bit_pos -= 2;
00251 cmd = (bit_buf >> bit_pos) & 0x03;
00252
00253 if(cmd == 0) {
00254 strip++;
00255 if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
00256 av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
00257 break;
00258 }
00259 memcpy(strip, strip-1, sizeof(*strip));
00260 strip->split_flag = 1;
00261 strip->split_direction = 0;
00262 strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);
00263 continue;
00264 } else if(cmd == 1) {
00265 strip++;
00266 if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
00267 av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
00268 break;
00269 }
00270 memcpy(strip, strip-1, sizeof(*strip));
00271 strip->split_flag = 1;
00272 strip->split_direction = 1;
00273 strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);
00274 continue;
00275 } else if(cmd == 2) {
00276 if(strip->usl7 == 0) {
00277 strip->usl7 = 1;
00278 ref_vectors = NULL;
00279 continue;
00280 }
00281 } else if(cmd == 3) {
00282 if(strip->usl7 == 0) {
00283 strip->usl7 = 1;
00284 ref_vectors = (const signed char*)buf2 + (*buf1 * 2);
00285 buf1++;
00286 continue;
00287 }
00288 }
00289
00290 cur_frm_pos = cur + width * strip->ypos + strip->xpos;
00291
00292 if((blks_width = strip->width) < 0)
00293 blks_width += 3;
00294 blks_width >>= 2;
00295 blks_height = strip->height;
00296
00297 if(ref_vectors != NULL) {
00298 ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
00299 ref_vectors[1] + strip->xpos;
00300 } else
00301 ref_frm_pos = cur_frm_pos - width_tbl[4];
00302
00303 if(cmd == 2) {
00304 if(bit_pos <= 0) {
00305 bit_pos = 8;
00306 bit_buf = *buf1++;
00307 }
00308
00309 bit_pos -= 2;
00310 cmd = (bit_buf >> bit_pos) & 0x03;
00311
00312 if(cmd == 0 || ref_vectors != NULL) {
00313 for(lp1 = 0; lp1 < blks_width; lp1++) {
00314 for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
00315 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
00316 cur_frm_pos += 4;
00317 ref_frm_pos += 4;
00318 }
00319 } else if(cmd != 1)
00320 return;
00321 } else {
00322 k = *buf1 >> 4;
00323 j = *buf1 & 0x0f;
00324 buf1++;
00325 lv = j + cb_offset;
00326
00327 if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
00328 cp2 = s->ModPred + ((lv - 8) << 7);
00329 cp = ref_frm_pos;
00330 for(i = 0; i < blks_width << 2; i++) {
00331 int v = *cp >> 1;
00332 *(cp++) = cp2[v];
00333 }
00334 }
00335
00336 if(k == 1 || k == 4) {
00337 lv = (hdr[j] & 0xf) + cb_offset;
00338 correction_type_sp[0] = s->corrector_type + (lv << 8);
00339 correction_lp[0] = correction + (lv << 8);
00340 lv = (hdr[j] >> 4) + cb_offset;
00341 correction_lp[1] = correction + (lv << 8);
00342 correction_type_sp[1] = s->corrector_type + (lv << 8);
00343 } else {
00344 correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);
00345 correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);
00346 correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);
00347 correction_lp[0] = correction_lp[1] = correction + (lv << 8);
00348 }
00349
00350 switch(k) {
00351 case 1:
00352 case 0:
00353 for( ; blks_height > 0; blks_height -= 4) {
00354 for(lp1 = 0; lp1 < blks_width; lp1++) {
00355 for(lp2 = 0; lp2 < 4; ) {
00356 k = *buf1++;
00357 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
00358 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
00359
00360 switch(correction_type_sp[0][k]) {
00361 case 0:
00362 *cur_lp = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
00363 lp2++;
00364 break;
00365 case 1:
00366 res = ((le2me_16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
00367 ((unsigned short *)cur_lp)[0] = le2me_16(res);
00368 res = ((le2me_16(((unsigned short *)(ref_lp))[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
00369 ((unsigned short *)cur_lp)[1] = le2me_16(res);
00370 buf1++;
00371 lp2++;
00372 break;
00373 case 2:
00374 if(lp2 == 0) {
00375 for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
00376 cur_lp[j] = ref_lp[j];
00377 lp2 += 2;
00378 }
00379 break;
00380 case 3:
00381 if(lp2 < 2) {
00382 for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
00383 cur_lp[j] = ref_lp[j];
00384 lp2 = 3;
00385 }
00386 break;
00387 case 8:
00388 if(lp2 == 0) {
00389 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
00390
00391 if(rle_v1 == 1 || ref_vectors != NULL) {
00392 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00393 cur_lp[j] = ref_lp[j];
00394 }
00395
00396 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
00397 break;
00398 } else {
00399 rle_v1 = 1;
00400 rle_v2 = *buf1 - 1;
00401 }
00402 case 5:
00403 LP2_CHECK(buf1,rle_v3,lp2)
00404 case 4:
00405 for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
00406 cur_lp[j] = ref_lp[j];
00407 lp2 = 4;
00408 break;
00409
00410 case 7:
00411 if(rle_v3 != 0)
00412 rle_v3 = 0;
00413 else {
00414 buf1--;
00415 rle_v3 = 1;
00416 }
00417 case 6:
00418 if(ref_vectors != NULL) {
00419 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00420 cur_lp[j] = ref_lp[j];
00421 }
00422 lp2 = 4;
00423 break;
00424
00425 case 9:
00426 lv1 = *buf1++;
00427 lv = (lv1 & 0x7F) << 1;
00428 lv += (lv << 8);
00429 lv += (lv << 16);
00430 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00431 cur_lp[j] = lv;
00432
00433 LV1_CHECK(buf1,rle_v3,lv1,lp2)
00434 break;
00435 default:
00436 return;
00437 }
00438 }
00439
00440 cur_frm_pos += 4;
00441 ref_frm_pos += 4;
00442 }
00443
00444 cur_frm_pos += ((width - blks_width) * 4);
00445 ref_frm_pos += ((width - blks_width) * 4);
00446 }
00447 break;
00448
00449 case 4:
00450 case 3:
00451 if(ref_vectors != NULL)
00452 return;
00453 flag1 = 1;
00454
00455 for( ; blks_height > 0; blks_height -= 8) {
00456 for(lp1 = 0; lp1 < blks_width; lp1++) {
00457 for(lp2 = 0; lp2 < 4; ) {
00458 k = *buf1++;
00459
00460 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
00461 ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
00462
00463 switch(correction_type_sp[lp2 & 0x01][k]) {
00464 case 0:
00465 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
00466 if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
00467 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00468 else
00469 cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
00470 lp2++;
00471 break;
00472
00473 case 1:
00474 res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;
00475 ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
00476 res = ((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
00477 ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
00478
00479 if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
00480 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00481 else
00482 cur_lp[0] = cur_lp[width_tbl[1]];
00483 buf1++;
00484 lp2++;
00485 break;
00486
00487 case 2:
00488 if(lp2 == 0) {
00489 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00490 cur_lp[j] = *ref_lp;
00491 lp2 += 2;
00492 }
00493 break;
00494
00495 case 3:
00496 if(lp2 < 2) {
00497 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
00498 cur_lp[j] = *ref_lp;
00499 lp2 = 3;
00500 }
00501 break;
00502
00503 case 6:
00504 lp2 = 4;
00505 break;
00506
00507 case 7:
00508 if(rle_v3 != 0)
00509 rle_v3 = 0;
00510 else {
00511 buf1--;
00512 rle_v3 = 1;
00513 }
00514 lp2 = 4;
00515 break;
00516
00517 case 8:
00518 if(lp2 == 0) {
00519 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
00520
00521 if(rle_v1 == 1) {
00522 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
00523 cur_lp[j] = ref_lp[j];
00524 }
00525
00526 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
00527 break;
00528 } else {
00529 rle_v2 = (*buf1) - 1;
00530 rle_v1 = 1;
00531 }
00532 case 5:
00533 LP2_CHECK(buf1,rle_v3,lp2)
00534 case 4:
00535 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
00536 cur_lp[j] = *ref_lp;
00537 lp2 = 4;
00538 break;
00539
00540 case 9:
00541 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
00542 lv1 = *buf1++;
00543 lv = (lv1 & 0x7F) << 1;
00544 lv += (lv << 8);
00545 lv += (lv << 16);
00546
00547 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00548 cur_lp[j] = lv;
00549
00550 LV1_CHECK(buf1,rle_v3,lv1,lp2)
00551 break;
00552
00553 default:
00554 return;
00555 }
00556 }
00557
00558 cur_frm_pos += 4;
00559 }
00560
00561 cur_frm_pos += (((width * 2) - blks_width) * 4);
00562 flag1 = 0;
00563 }
00564 break;
00565
00566 case 10:
00567 if(ref_vectors == NULL) {
00568 flag1 = 1;
00569
00570 for( ; blks_height > 0; blks_height -= 8) {
00571 for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
00572 for(lp2 = 0; lp2 < 4; ) {
00573 k = *buf1++;
00574 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
00575 ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
00576 lv1 = ref_lp[0];
00577 lv2 = ref_lp[1];
00578 if(lp2 == 0 && flag1 != 0) {
00579 #if HAVE_BIGENDIAN
00580 lv1 = lv1 & 0xFF00FF00;
00581 lv1 = (lv1 >> 8) | lv1;
00582 lv2 = lv2 & 0xFF00FF00;
00583 lv2 = (lv2 >> 8) | lv2;
00584 #else
00585 lv1 = lv1 & 0x00FF00FF;
00586 lv1 = (lv1 << 8) | lv1;
00587 lv2 = lv2 & 0x00FF00FF;
00588 lv2 = (lv2 << 8) | lv2;
00589 #endif
00590 }
00591
00592 switch(correction_type_sp[lp2 & 0x01][k]) {
00593 case 0:
00594 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
00595 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1);
00596 if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
00597 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00598 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
00599 } else {
00600 cur_lp[0] = cur_lp[width_tbl[1]];
00601 cur_lp[1] = cur_lp[width_tbl[1]+1];
00602 }
00603 lp2++;
00604 break;
00605
00606 case 1:
00607 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1);
00608 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);
00609 if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
00610 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00611 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
00612 } else {
00613 cur_lp[0] = cur_lp[width_tbl[1]];
00614 cur_lp[1] = cur_lp[width_tbl[1]+1];
00615 }
00616 buf1++;
00617 lp2++;
00618 break;
00619
00620 case 2:
00621 if(lp2 == 0) {
00622 if(flag1 != 0) {
00623 for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
00624 cur_lp[j] = lv1;
00625 cur_lp[j+1] = lv2;
00626 }
00627 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00628 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
00629 } else {
00630 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
00631 cur_lp[j] = lv1;
00632 cur_lp[j+1] = lv2;
00633 }
00634 }
00635 lp2 += 2;
00636 }
00637 break;
00638
00639 case 3:
00640 if(lp2 < 2) {
00641 if(lp2 == 0 && flag1 != 0) {
00642 for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
00643 cur_lp[j] = lv1;
00644 cur_lp[j+1] = lv2;
00645 }
00646 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00647 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
00648 } else {
00649 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
00650 cur_lp[j] = lv1;
00651 cur_lp[j+1] = lv2;
00652 }
00653 }
00654 lp2 = 3;
00655 }
00656 break;
00657
00658 case 8:
00659 if(lp2 == 0) {
00660 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
00661 if(rle_v1 == 1) {
00662 if(flag1 != 0) {
00663 for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
00664 cur_lp[j] = lv1;
00665 cur_lp[j+1] = lv2;
00666 }
00667 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00668 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
00669 } else {
00670 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
00671 cur_lp[j] = lv1;
00672 cur_lp[j+1] = lv2;
00673 }
00674 }
00675 }
00676 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
00677 break;
00678 } else {
00679 rle_v1 = 1;
00680 rle_v2 = (*buf1) - 1;
00681 }
00682 case 5:
00683 LP2_CHECK(buf1,rle_v3,lp2)
00684 case 4:
00685 if(lp2 == 0 && flag1 != 0) {
00686 for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
00687 cur_lp[j] = lv1;
00688 cur_lp[j+1] = lv2;
00689 }
00690 cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
00691 cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
00692 } else {
00693 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
00694 cur_lp[j] = lv1;
00695 cur_lp[j+1] = lv2;
00696 }
00697 }
00698 lp2 = 4;
00699 break;
00700
00701 case 6:
00702 lp2 = 4;
00703 break;
00704
00705 case 7:
00706 if(lp2 == 0) {
00707 if(rle_v3 != 0)
00708 rle_v3 = 0;
00709 else {
00710 buf1--;
00711 rle_v3 = 1;
00712 }
00713 lp2 = 4;
00714 }
00715 break;
00716
00717 case 9:
00718 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
00719 lv1 = *buf1;
00720 lv = (lv1 & 0x7F) << 1;
00721 lv += (lv << 8);
00722 lv += (lv << 16);
00723 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
00724 cur_lp[j] = lv;
00725 LV1_CHECK(buf1,rle_v3,lv1,lp2)
00726 break;
00727
00728 default:
00729 return;
00730 }
00731 }
00732
00733 cur_frm_pos += 8;
00734 }
00735
00736 cur_frm_pos += (((width * 2) - blks_width) * 4);
00737 flag1 = 0;
00738 }
00739 } else {
00740 for( ; blks_height > 0; blks_height -= 8) {
00741 for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
00742 for(lp2 = 0; lp2 < 4; ) {
00743 k = *buf1++;
00744 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
00745 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
00746
00747 switch(correction_type_sp[lp2 & 0x01][k]) {
00748 case 0:
00749 lv1 = correctionloworder_lp[lp2 & 0x01][k];
00750 lv2 = correctionhighorder_lp[lp2 & 0x01][k];
00751 cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
00752 cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
00753 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
00754 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
00755 lp2++;
00756 break;
00757
00758 case 1:
00759 lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
00760 lv2 = correctionloworder_lp[lp2 & 0x01][k];
00761 cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);
00762 cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);
00763 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);
00764 cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);
00765 lp2++;
00766 break;
00767
00768 case 2:
00769 if(lp2 == 0) {
00770 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
00771 cur_lp[j] = ref_lp[j];
00772 cur_lp[j+1] = ref_lp[j+1];
00773 }
00774 lp2 += 2;
00775 }
00776 break;
00777
00778 case 3:
00779 if(lp2 < 2) {
00780 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
00781 cur_lp[j] = ref_lp[j];
00782 cur_lp[j+1] = ref_lp[j+1];
00783 }
00784 lp2 = 3;
00785 }
00786 break;
00787
00788 case 8:
00789 if(lp2 == 0) {
00790 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
00791 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
00792 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
00793 ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];
00794 }
00795 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
00796 break;
00797 } else {
00798 rle_v1 = 1;
00799 rle_v2 = (*buf1) - 1;
00800 }
00801 case 5:
00802 case 7:
00803 LP2_CHECK(buf1,rle_v3,lp2)
00804 case 6:
00805 case 4:
00806 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
00807 cur_lp[j] = ref_lp[j];
00808 cur_lp[j+1] = ref_lp[j+1];
00809 }
00810 lp2 = 4;
00811 break;
00812
00813 case 9:
00814 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
00815 lv1 = *buf1;
00816 lv = (lv1 & 0x7F) << 1;
00817 lv += (lv << 8);
00818 lv += (lv << 16);
00819 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
00820 ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
00821 LV1_CHECK(buf1,rle_v3,lv1,lp2)
00822 break;
00823
00824 default:
00825 return;
00826 }
00827 }
00828
00829 cur_frm_pos += 8;
00830 ref_frm_pos += 8;
00831 }
00832
00833 cur_frm_pos += (((width * 2) - blks_width) * 4);
00834 ref_frm_pos += (((width * 2) - blks_width) * 4);
00835 }
00836 }
00837 break;
00838
00839 case 11:
00840 if(ref_vectors == NULL)
00841 return;
00842
00843 for( ; blks_height > 0; blks_height -= 8) {
00844 for(lp1 = 0; lp1 < blks_width; lp1++) {
00845 for(lp2 = 0; lp2 < 4; ) {
00846 k = *buf1++;
00847 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
00848 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
00849
00850 switch(correction_type_sp[lp2 & 0x01][k]) {
00851 case 0:
00852 cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
00853 cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);
00854 lp2++;
00855 break;
00856
00857 case 1:
00858 lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
00859 lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
00860 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1);
00861 ((unsigned short *)cur_lp)[0] = le2me_16(res);
00862 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1);
00863 ((unsigned short *)cur_lp)[1] = le2me_16(res);
00864 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1);
00865 ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);
00866 res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1);
00867 ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);
00868 lp2++;
00869 break;
00870
00871 case 2:
00872 if(lp2 == 0) {
00873 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00874 cur_lp[j] = ref_lp[j];
00875 lp2 += 2;
00876 }
00877 break;
00878
00879 case 3:
00880 if(lp2 < 2) {
00881 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
00882 cur_lp[j] = ref_lp[j];
00883 lp2 = 3;
00884 }
00885 break;
00886
00887 case 8:
00888 if(lp2 == 0) {
00889 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
00890
00891 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
00892 cur_lp[j] = ref_lp[j];
00893
00894 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
00895 break;
00896 } else {
00897 rle_v1 = 1;
00898 rle_v2 = (*buf1) - 1;
00899 }
00900 case 5:
00901 case 7:
00902 LP2_CHECK(buf1,rle_v3,lp2)
00903 case 4:
00904 case 6:
00905 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
00906 cur_lp[j] = ref_lp[j];
00907 lp2 = 4;
00908 break;
00909
00910 case 9:
00911 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
00912 lv1 = *buf1++;
00913 lv = (lv1 & 0x7F) << 1;
00914 lv += (lv << 8);
00915 lv += (lv << 16);
00916 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
00917 cur_lp[j] = lv;
00918 LV1_CHECK(buf1,rle_v3,lv1,lp2)
00919 break;
00920
00921 default:
00922 return;
00923 }
00924 }
00925
00926 cur_frm_pos += 4;
00927 ref_frm_pos += 4;
00928 }
00929
00930 cur_frm_pos += (((width * 2) - blks_width) * 4);
00931 ref_frm_pos += (((width * 2) - blks_width) * 4);
00932 }
00933 break;
00934
00935 default:
00936 return;
00937 }
00938 }
00939
00940 for( ; strip >= strip_tbl; strip--) {
00941 if(strip->split_flag != 0) {
00942 strip->split_flag = 0;
00943 strip->usl7 = (strip-1)->usl7;
00944
00945 if(strip->split_direction) {
00946 strip->xpos += strip->width;
00947 strip->width = (strip-1)->width - strip->width;
00948 if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
00949 strip->width = width - strip->xpos;
00950 } else {
00951 strip->ypos += strip->height;
00952 strip->height = (strip-1)->height - strip->height;
00953 }
00954 break;
00955 }
00956 }
00957 }
00958 }
00959
00960 static av_cold int indeo3_decode_init(AVCodecContext *avctx)
00961 {
00962 Indeo3DecodeContext *s = avctx->priv_data;
00963 int ret = 0;
00964
00965 s->avctx = avctx;
00966 s->width = avctx->width;
00967 s->height = avctx->height;
00968 avctx->pix_fmt = PIX_FMT_YUV410P;
00969
00970 if (!(ret = build_modpred(s)))
00971 ret = iv_alloc_frames(s);
00972 if (ret)
00973 iv_free_func(s);
00974
00975 return ret;
00976 }
00977
00978 static int iv_decode_frame(AVCodecContext *avctx,
00979 const uint8_t *buf, int buf_size)
00980 {
00981 Indeo3DecodeContext *s = avctx->priv_data;
00982 unsigned int image_width, image_height,
00983 chroma_width, chroma_height;
00984 unsigned long flags, cb_offset, data_size,
00985 y_offset, v_offset, u_offset, mc_vector_count;
00986 const uint8_t *hdr_pos, *buf_pos;
00987
00988 buf_pos = buf;
00989 buf_pos += 18;
00990
00991 flags = bytestream_get_le16(&buf_pos);
00992 data_size = bytestream_get_le32(&buf_pos);
00993 cb_offset = *buf_pos++;
00994 buf_pos += 3;
00995 image_height = bytestream_get_le16(&buf_pos);
00996 image_width = bytestream_get_le16(&buf_pos);
00997
00998 if(avcodec_check_dimensions(avctx, image_width, image_height))
00999 return -1;
01000 if (image_width != avctx->width || image_height != avctx->height) {
01001 int ret;
01002 avcodec_set_dimensions(avctx, image_width, image_height);
01003 s->width = avctx->width;
01004 s->height = avctx->height;
01005 ret = iv_alloc_frames(s);
01006 if (ret < 0) {
01007 s->width = s->height = 0;
01008 return ret;
01009 }
01010 }
01011
01012 chroma_height = ((image_height >> 2) + 3) & 0x7ffc;
01013 chroma_width = ((image_width >> 2) + 3) & 0x7ffc;
01014 y_offset = bytestream_get_le32(&buf_pos);
01015 v_offset = bytestream_get_le32(&buf_pos);
01016 u_offset = bytestream_get_le32(&buf_pos);
01017 buf_pos += 4;
01018 hdr_pos = buf_pos;
01019 if(data_size == 0x80) return 4;
01020
01021 if(FFMAX3(y_offset, v_offset, u_offset) >= buf_size-16) {
01022 av_log(s->avctx, AV_LOG_ERROR, "y/u/v offset outside buffer\n");
01023 return -1;
01024 }
01025
01026 if(flags & 0x200) {
01027 s->cur_frame = s->iv_frame + 1;
01028 s->ref_frame = s->iv_frame;
01029 } else {
01030 s->cur_frame = s->iv_frame;
01031 s->ref_frame = s->iv_frame + 1;
01032 }
01033
01034 buf_pos = buf + 16 + y_offset;
01035 mc_vector_count = bytestream_get_le32(&buf_pos);
01036 if(2LL*mc_vector_count >= buf_size-16-y_offset) {
01037 av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
01038 return -1;
01039 }
01040
01041 iv_Decode_Chunk(s, s->cur_frame->Ybuf, s->ref_frame->Ybuf, image_width,
01042 image_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
01043 FFMIN(image_width, 160));
01044
01045 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
01046 {
01047
01048 buf_pos = buf + 16 + v_offset;
01049 mc_vector_count = bytestream_get_le32(&buf_pos);
01050 if(2LL*mc_vector_count >= buf_size-16-v_offset) {
01051 av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
01052 return -1;
01053 }
01054
01055 iv_Decode_Chunk(s, s->cur_frame->Vbuf, s->ref_frame->Vbuf, chroma_width,
01056 chroma_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
01057 FFMIN(chroma_width, 40));
01058
01059 buf_pos = buf + 16 + u_offset;
01060 mc_vector_count = bytestream_get_le32(&buf_pos);
01061 if(2LL*mc_vector_count >= buf_size-16-u_offset) {
01062 av_log(s->avctx, AV_LOG_ERROR, "mc_vector_count too large\n");
01063 return -1;
01064 }
01065
01066 iv_Decode_Chunk(s, s->cur_frame->Ubuf, s->ref_frame->Ubuf, chroma_width,
01067 chroma_height, buf_pos + mc_vector_count * 2, cb_offset, hdr_pos, buf_pos,
01068 FFMIN(chroma_width, 40));
01069
01070 }
01071
01072 return 8;
01073 }
01074
01075 static int indeo3_decode_frame(AVCodecContext *avctx,
01076 void *data, int *data_size,
01077 AVPacket *avpkt)
01078 {
01079 const uint8_t *buf = avpkt->data;
01080 int buf_size = avpkt->size;
01081 Indeo3DecodeContext *s=avctx->priv_data;
01082 uint8_t *src, *dest;
01083 int y;
01084
01085 if (iv_decode_frame(avctx, buf, buf_size) < 0)
01086 return -1;
01087
01088 if(s->frame.data[0])
01089 avctx->release_buffer(avctx, &s->frame);
01090
01091 s->frame.reference = 0;
01092 if(avctx->get_buffer(avctx, &s->frame) < 0) {
01093 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01094 return -1;
01095 }
01096
01097 src = s->cur_frame->Ybuf;
01098 dest = s->frame.data[0];
01099 for (y = 0; y < s->height; y++) {
01100 memcpy(dest, src, s->cur_frame->y_w);
01101 src += s->cur_frame->y_w;
01102 dest += s->frame.linesize[0];
01103 }
01104
01105 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
01106 {
01107 src = s->cur_frame->Ubuf;
01108 dest = s->frame.data[1];
01109 for (y = 0; y < s->height / 4; y++) {
01110 memcpy(dest, src, s->cur_frame->uv_w);
01111 src += s->cur_frame->uv_w;
01112 dest += s->frame.linesize[1];
01113 }
01114
01115 src = s->cur_frame->Vbuf;
01116 dest = s->frame.data[2];
01117 for (y = 0; y < s->height / 4; y++) {
01118 memcpy(dest, src, s->cur_frame->uv_w);
01119 src += s->cur_frame->uv_w;
01120 dest += s->frame.linesize[2];
01121 }
01122 }
01123
01124 *data_size=sizeof(AVFrame);
01125 *(AVFrame*)data= s->frame;
01126
01127 return buf_size;
01128 }
01129
01130 static av_cold int indeo3_decode_end(AVCodecContext *avctx)
01131 {
01132 Indeo3DecodeContext *s = avctx->priv_data;
01133
01134 iv_free_func(s);
01135
01136 return 0;
01137 }
01138
01139 AVCodec indeo3_decoder = {
01140 "indeo3",
01141 AVMEDIA_TYPE_VIDEO,
01142 CODEC_ID_INDEO3,
01143 sizeof(Indeo3DecodeContext),
01144 indeo3_decode_init,
01145 NULL,
01146 indeo3_decode_end,
01147 indeo3_decode_frame,
01148 CODEC_CAP_DR1,
01149 NULL,
01150 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
01151 };