2 * Intel Indeo 3 (IV31, IV32, etc.) video decoder for ffmpeg
3 * written, produced, and directed by Alan Smithee
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavcore/imgutils.h"
29 #include "bytestream.h"
31 #include "indeo3data.h"
38 unsigned short y_w, y_h;
39 unsigned short uv_w, uv_h;
42 typedef struct Indeo3DecodeContext {
43 AVCodecContext *avctx;
53 uint8_t *corrector_type;
54 } Indeo3DecodeContext;
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
62 static const uint8_t corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
64 static av_cold int build_modpred(Indeo3DecodeContext *s)
68 if (!(s->ModPred = av_malloc(8 * 128)))
69 return AVERROR(ENOMEM);
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 :
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));
84 if (!(s->corrector_type = av_malloc(24 * 256)))
85 return AVERROR(ENOMEM);
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];
98 static av_cold int iv_alloc_frames(Indeo3DecodeContext *s)
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,
107 unsigned int bufsize = luma_pixels * 2 + luma_width * 3 +
108 (chroma_pixels + chroma_width) * 4;
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;
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;
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;
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;
144 static av_cold void iv_free_func(Indeo3DecodeContext *s)
147 av_freep(&s->ModPred);
148 av_freep(&s->corrector_type);
157 long split_direction;
162 #define LV1_CHECK(buf1,rle_v3,lv1,lp2) \
163 if((lv1 & 0x80) != 0) { \
174 #define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3) \
187 #define LP2_CHECK(buf1,rle_v3,lp2) \
188 if(lp2 == 0 && rle_v3 != 0) \
196 #define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
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)
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;
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;
232 for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
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;
243 rle_v1 = rle_v2 = rle_v3 = 0;
245 while(strip >= strip_tbl) {
252 cmd = (bit_buf >> bit_pos) & 0x03;
256 if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
257 av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
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);
265 } else if(cmd == 1) {
267 if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
268 av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
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);
276 } else if(cmd == 2) {
277 if(strip->usl7 == 0) {
282 } else if(cmd == 3) {
283 if(strip->usl7 == 0) {
285 ref_vectors = (const signed char*)buf2 + (*buf1 * 2);
291 cur_frm_pos = cur + width * strip->ypos + strip->xpos;
293 if((blks_width = strip->width) < 0)
296 blks_height = strip->height;
298 if(ref_vectors != NULL) {
299 ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
300 ref_vectors[1] + strip->xpos;
302 ref_frm_pos = cur_frm_pos - width_tbl[4];
311 cmd = (bit_buf >> bit_pos) & 0x03;
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];
328 if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
329 cp2 = s->ModPred + ((lv - 8) << 7);
331 for(i = 0; i < blks_width << 2; i++) {
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);
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);
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; ) {
358 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
359 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
361 switch(correction_type_sp[0][k]) {
363 *cur_lp = av_le2ne32(((av_le2ne32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 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);
376 for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
377 cur_lp[j] = ref_lp[j];
383 for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
384 cur_lp[j] = ref_lp[j];
390 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
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];
397 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
404 LP2_CHECK(buf1,rle_v3,lp2)
406 for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
407 cur_lp[j] = ref_lp[j];
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];
428 lv = (lv1 & 0x7F) << 1;
431 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
434 LV1_CHECK(buf1,rle_v3,lv1,lp2)
445 cur_frm_pos += ((width - blks_width) * 4);
446 ref_frm_pos += ((width - blks_width) * 4);
451 case 3: /********** CASE 3 **********/
452 if(ref_vectors != NULL)
456 for( ; blks_height > 0; blks_height -= 8) {
457 for(lp1 = 0; lp1 < blks_width; lp1++) {
458 for(lp2 = 0; lp2 < 4; ) {
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];
464 switch(correction_type_sp[lp2 & 0x01][k]) {
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;
470 cur_lp[0] = av_le2ne32(((av_le2ne32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 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);
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;
483 cur_lp[0] = cur_lp[width_tbl[1]];
490 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
498 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
520 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
523 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
524 cur_lp[j] = ref_lp[j];
527 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
530 rle_v2 = (*buf1) - 1;
534 LP2_CHECK(buf1,rle_v3,lp2)
536 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
542 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
544 lv = (lv1 & 0x7F) << 1;
548 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
551 LV1_CHECK(buf1,rle_v3,lv1,lp2)
562 cur_frm_pos += (((width * 2) - blks_width) * 4);
567 case 10: /********** CASE 10 **********/
568 if(ref_vectors == NULL) {
571 for( ; blks_height > 0; blks_height -= 8) {
572 for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
573 for(lp2 = 0; lp2 < 4; ) {
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];
579 if(lp2 == 0 && flag1 != 0) {
581 lv1 = lv1 & 0xFF00FF00;
582 lv1 = (lv1 >> 8) | lv1;
583 lv2 = lv2 & 0xFF00FF00;
584 lv2 = (lv2 >> 8) | lv2;
586 lv1 = lv1 & 0x00FF00FF;
587 lv1 = (lv1 << 8) | lv1;
588 lv2 = lv2 & 0x00FF00FF;
589 lv2 = (lv2 << 8) | lv2;
593 switch(correction_type_sp[lp2 & 0x01][k]) {
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;
601 cur_lp[0] = cur_lp[width_tbl[1]];
602 cur_lp[1] = cur_lp[width_tbl[1]+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;
614 cur_lp[0] = cur_lp[width_tbl[1]];
615 cur_lp[1] = cur_lp[width_tbl[1]+1];
624 for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
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;
631 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
642 if(lp2 == 0 && flag1 != 0) {
643 for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
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;
650 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
661 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
664 for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
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;
671 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
677 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
681 rle_v2 = (*buf1) - 1;
684 LP2_CHECK(buf1,rle_v3,lp2)
686 if(lp2 == 0 && flag1 != 0) {
687 for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
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;
694 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
719 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
721 lv = (lv1 & 0x7F) << 1;
724 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
726 LV1_CHECK(buf1,rle_v3,lv1,lp2)
737 cur_frm_pos += (((width * 2) - blks_width) * 4);
741 for( ; blks_height > 0; blks_height -= 8) {
742 for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
743 for(lp2 = 0; lp2 < 4; ) {
745 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
746 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
748 switch(correction_type_sp[lp2 & 0x01][k]) {
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);
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);
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];
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];
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];
796 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
800 rle_v2 = (*buf1) - 1;
804 LP2_CHECK(buf1,rle_v3,lp2)
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];
815 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
817 lv = (lv1 & 0x7F) << 1;
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)
834 cur_frm_pos += (((width * 2) - blks_width) * 4);
835 ref_frm_pos += (((width * 2) - blks_width) * 4);
840 case 11: /********** CASE 11 **********/
841 if(ref_vectors == NULL)
844 for( ; blks_height > 0; blks_height -= 8) {
845 for(lp1 = 0; lp1 < blks_width; lp1++) {
846 for(lp2 = 0; lp2 < 4; ) {
848 cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
849 ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
851 switch(correction_type_sp[lp2 & 0x01][k]) {
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);
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);
874 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
875 cur_lp[j] = ref_lp[j];
882 for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
883 cur_lp[j] = ref_lp[j];
890 RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
892 for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
893 cur_lp[j] = ref_lp[j];
895 RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
899 rle_v2 = (*buf1) - 1;
903 LP2_CHECK(buf1,rle_v3,lp2)
906 for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
907 cur_lp[j] = ref_lp[j];
912 av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
914 lv = (lv1 & 0x7F) << 1;
917 for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
919 LV1_CHECK(buf1,rle_v3,lv1,lp2)
931 cur_frm_pos += (((width * 2) - blks_width) * 4);
932 ref_frm_pos += (((width * 2) - blks_width) * 4);
941 for( ; strip >= strip_tbl; strip--) {
942 if(strip->split_flag != 0) {
943 strip->split_flag = 0;
944 strip->usl7 = (strip-1)->usl7;
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;
952 strip->ypos += strip->height;
953 strip->height = (strip-1)->height - strip->height;
961 static av_cold int indeo3_decode_init(AVCodecContext *avctx)
963 Indeo3DecodeContext *s = avctx->priv_data;
967 s->width = avctx->width;
968 s->height = avctx->height;
969 avctx->pix_fmt = PIX_FMT_YUV410P;
971 if (!(ret = build_modpred(s)))
972 ret = iv_alloc_frames(s);
979 static int iv_decode_frame(AVCodecContext *avctx,
980 const uint8_t *buf, int buf_size)
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;
990 buf_pos += 18; /* skip OS header (16 bytes) and version number */
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);
999 if(av_image_check_size(image_width, image_height, 0, avctx))
1001 if (image_width != avctx->width || image_height != avctx->height) {
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);
1008 s->width = s->height = 0;
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 */
1020 if(data_size == 0x80) return 4;
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");
1028 s->cur_frame = s->iv_frame + 1;
1029 s->ref_frame = s->iv_frame;
1031 s->cur_frame = s->iv_frame;
1032 s->ref_frame = s->iv_frame + 1;
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");
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));
1046 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
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");
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));
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");
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));
1076 static int indeo3_decode_frame(AVCodecContext *avctx,
1077 void *data, int *data_size,
1080 const uint8_t *buf = avpkt->data;
1081 int buf_size = avpkt->size;
1082 Indeo3DecodeContext *s=avctx->priv_data;
1083 uint8_t *src, *dest;
1086 if (iv_decode_frame(avctx, buf, buf_size) < 0)
1089 if(s->frame.data[0])
1090 avctx->release_buffer(avctx, &s->frame);
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");
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];
1106 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
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];
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];
1125 *data_size=sizeof(AVFrame);
1126 *(AVFrame*)data= s->frame;
1131 static av_cold int indeo3_decode_end(AVCodecContext *avctx)
1133 Indeo3DecodeContext *s = avctx->priv_data;
1140 AVCodec indeo3_decoder = {
1144 sizeof(Indeo3DecodeContext),
1148 indeo3_decode_frame,
1151 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),