You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1134 lines
38KB

  1. /*
  2. * Intel Indeo 3 (IV31, IV32, etc.) video decoder for ffmpeg
  3. * written, produced, and directed by Alan Smithee
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "common.h"
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "mpegvideo.h"
  27. #include "indeo3data.h"
  28. typedef struct
  29. {
  30. unsigned char *Ybuf;
  31. unsigned char *Ubuf;
  32. unsigned char *Vbuf;
  33. unsigned char *the_buf;
  34. unsigned int the_buf_size;
  35. unsigned short y_w, y_h;
  36. unsigned short uv_w, uv_h;
  37. } YUVBufs;
  38. typedef struct Indeo3DecodeContext {
  39. AVCodecContext *avctx;
  40. int width, height;
  41. AVFrame frame;
  42. YUVBufs iv_frame[2];
  43. YUVBufs *cur_frame;
  44. YUVBufs *ref_frame;
  45. unsigned char *ModPred;
  46. unsigned short *corrector_type;
  47. } Indeo3DecodeContext;
  48. static int corrector_type_0[24] = {
  49. 195, 159, 133, 115, 101, 93, 87, 77,
  50. 195, 159, 133, 115, 101, 93, 87, 77,
  51. 128, 79, 79, 79, 79, 79, 79, 79
  52. };
  53. static int corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
  54. static void build_modpred(Indeo3DecodeContext *s)
  55. {
  56. int i, j;
  57. s->ModPred = (unsigned char *) av_malloc (8 * 128);
  58. for (i=0; i < 128; ++i) {
  59. s->ModPred[i+0*128] = (i > 126) ? 254 : 2*((i + 1) - ((i + 1) % 2));
  60. s->ModPred[i+1*128] = (i == 7) ? 20 : ((i == 119 || i == 120)
  61. ? 236 : 2*((i + 2) - ((i + 1) % 3)));
  62. s->ModPred[i+2*128] = (i > 125) ? 248 : 2*((i + 2) - ((i + 2) % 4));
  63. s->ModPred[i+3*128] = 2*((i + 1) - ((i - 3) % 5));
  64. s->ModPred[i+4*128] = (i == 8) ? 20 : 2*((i + 1) - ((i - 3) % 6));
  65. s->ModPred[i+5*128] = 2*((i + 4) - ((i + 3) % 7));
  66. s->ModPred[i+6*128] = (i > 123) ? 240 : 2*((i + 4) - ((i + 4) % 8));
  67. s->ModPred[i+7*128] = 2*((i + 5) - ((i + 4) % 9));
  68. }
  69. s->corrector_type = (unsigned short *) av_malloc (24 * 256 * sizeof(unsigned short));
  70. for (i=0; i < 24; ++i) {
  71. for (j=0; j < 256; ++j) {
  72. s->corrector_type[i*256+j] = (j < corrector_type_0[i])
  73. ? 1 : ((j < 248 || (i == 16 && j == 248))
  74. ? 0 : corrector_type_2[j - 248]);
  75. }
  76. }
  77. }
  78. static void iv_Decode_Chunk(Indeo3DecodeContext *s, unsigned char *cur,
  79. unsigned char *ref, int width, int height, unsigned char *buf1,
  80. long fflags2, unsigned char *hdr,
  81. unsigned char *buf2, int min_width_160);
  82. #define min(a,b) ((a) < (b) ? (a) : (b))
  83. /* ---------------------------------------------------------------------- */
  84. static void iv_alloc_frames(Indeo3DecodeContext *s)
  85. {
  86. int luma_width, luma_height, luma_pixels, chroma_width, chroma_height,
  87. chroma_pixels, i;
  88. unsigned int bufsize;
  89. luma_width = (s->width + 3) & (~3);
  90. luma_height = (s->height + 3) & (~3);
  91. s->iv_frame[0].y_w = s->iv_frame[0].y_h =
  92. s->iv_frame[0].the_buf_size = 0;
  93. s->iv_frame[1].y_w = s->iv_frame[1].y_h =
  94. s->iv_frame[1].the_buf_size = 0;
  95. s->iv_frame[1].the_buf = NULL;
  96. chroma_width = ((luma_width >> 2) + 3) & (~3);
  97. chroma_height = ((luma_height>> 2) + 3) & (~3);
  98. luma_pixels = luma_width * luma_height;
  99. chroma_pixels = chroma_width * chroma_height;
  100. bufsize = luma_pixels * 2 + luma_width * 3 +
  101. (chroma_pixels + chroma_width) * 4;
  102. if((s->iv_frame[0].the_buf =
  103. (s->iv_frame[0].the_buf_size == 0 ? av_malloc(bufsize) :
  104. av_realloc(s->iv_frame[0].the_buf, bufsize))) == NULL)
  105. return;
  106. s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
  107. s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
  108. s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
  109. s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;
  110. s->iv_frame[0].the_buf_size = bufsize;
  111. s->iv_frame[0].Ybuf = s->iv_frame[0].the_buf + luma_width;
  112. i = luma_pixels + luma_width * 2;
  113. s->iv_frame[1].Ybuf = s->iv_frame[0].the_buf + i;
  114. i += (luma_pixels + luma_width);
  115. s->iv_frame[0].Ubuf = s->iv_frame[0].the_buf + i;
  116. i += (chroma_pixels + chroma_width);
  117. s->iv_frame[1].Ubuf = s->iv_frame[0].the_buf + i;
  118. i += (chroma_pixels + chroma_width);
  119. s->iv_frame[0].Vbuf = s->iv_frame[0].the_buf + i;
  120. i += (chroma_pixels + chroma_width);
  121. s->iv_frame[1].Vbuf = s->iv_frame[0].the_buf + i;
  122. for(i = 1; i <= luma_width; i++)
  123. s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =
  124. s->iv_frame[0].Ubuf[-i] = 0x80;
  125. for(i = 1; i <= chroma_width; i++) {
  126. s->iv_frame[1].Ubuf[-i] = 0x80;
  127. s->iv_frame[0].Vbuf[-i] = 0x80;
  128. s->iv_frame[1].Vbuf[-i] = 0x80;
  129. s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;
  130. }
  131. }
  132. /* ---------------------------------------------------------------------- */
  133. static void iv_free_func(Indeo3DecodeContext *s)
  134. {
  135. int i;
  136. for(i = 0 ; i < 2 ; i++) {
  137. if(s->iv_frame[i].the_buf != NULL)
  138. av_free(s->iv_frame[i].the_buf);
  139. s->iv_frame[i].Ybuf = s->iv_frame[i].Ubuf =
  140. s->iv_frame[i].Vbuf = NULL;
  141. s->iv_frame[i].the_buf = NULL;
  142. s->iv_frame[i].the_buf_size = 0;
  143. s->iv_frame[i].y_w = s->iv_frame[i].y_h = 0;
  144. s->iv_frame[i].uv_w = s->iv_frame[i].uv_h = 0;
  145. }
  146. av_free(s->ModPred);
  147. av_free(s->corrector_type);
  148. }
  149. /* ---------------------------------------------------------------------- */
  150. static unsigned long iv_decode_frame(Indeo3DecodeContext *s,
  151. unsigned char *buf, int buf_size)
  152. {
  153. unsigned int hdr_width, hdr_height,
  154. chroma_width, chroma_height;
  155. unsigned long fflags1, fflags2, fflags3, offs1, offs2, offs3, offs;
  156. unsigned char *hdr_pos, *buf_pos;
  157. buf_pos = buf;
  158. buf_pos += 18;
  159. fflags1 = le2me_16(*(uint16_t *)buf_pos);
  160. buf_pos += 2;
  161. fflags3 = le2me_32(*(uint32_t *)buf_pos);
  162. buf_pos += 4;
  163. fflags2 = *buf_pos++;
  164. buf_pos += 3;
  165. hdr_height = le2me_16(*(uint16_t *)buf_pos);
  166. buf_pos += 2;
  167. hdr_width = le2me_16(*(uint16_t *)buf_pos);
  168. if(avcodec_check_dimensions(NULL, hdr_width, hdr_height))
  169. return -1;
  170. buf_pos += 2;
  171. chroma_height = ((hdr_height >> 2) + 3) & 0x7ffc;
  172. chroma_width = ((hdr_width >> 2) + 3) & 0x7ffc;
  173. offs1 = le2me_32(*(uint32_t *)buf_pos);
  174. buf_pos += 4;
  175. offs2 = le2me_32(*(uint32_t *)buf_pos);
  176. buf_pos += 4;
  177. offs3 = le2me_32(*(uint32_t *)buf_pos);
  178. buf_pos += 8;
  179. hdr_pos = buf_pos;
  180. if(fflags3 == 0x80) return 4;
  181. if(fflags1 & 0x200) {
  182. s->cur_frame = s->iv_frame + 1;
  183. s->ref_frame = s->iv_frame;
  184. } else {
  185. s->cur_frame = s->iv_frame;
  186. s->ref_frame = s->iv_frame + 1;
  187. }
  188. buf_pos = buf + 16 + offs1;
  189. offs = le2me_32(*(uint32_t *)buf_pos);
  190. buf_pos += 4;
  191. iv_Decode_Chunk(s, s->cur_frame->Ybuf, s->ref_frame->Ybuf, hdr_width,
  192. hdr_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos,
  193. min(hdr_width, 160));
  194. if (!(s->avctx->flags & CODEC_FLAG_GRAY))
  195. {
  196. buf_pos = buf + 16 + offs2;
  197. offs = le2me_32(*(uint32_t *)buf_pos);
  198. buf_pos += 4;
  199. iv_Decode_Chunk(s, s->cur_frame->Vbuf, s->ref_frame->Vbuf, chroma_width,
  200. chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos,
  201. min(chroma_width, 40));
  202. buf_pos = buf + 16 + offs3;
  203. offs = le2me_32(*(uint32_t *)buf_pos);
  204. buf_pos += 4;
  205. iv_Decode_Chunk(s, s->cur_frame->Ubuf, s->ref_frame->Ubuf, chroma_width,
  206. chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos,
  207. min(chroma_width, 40));
  208. }
  209. return 8;
  210. }
  211. typedef struct {
  212. long xpos;
  213. long ypos;
  214. long width;
  215. long height;
  216. long split_flag;
  217. long split_direction;
  218. long usl7;
  219. } ustr_t;
  220. /* ---------------------------------------------------------------------- */
  221. #define LV1_CHECK(buf1,rle_v3,lv1,lp2) \
  222. if((lv1 & 0x80) != 0) { \
  223. if(rle_v3 != 0) \
  224. rle_v3 = 0; \
  225. else { \
  226. rle_v3 = 1; \
  227. buf1 -= 2; \
  228. } \
  229. } \
  230. lp2 = 4;
  231. #define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3) \
  232. if(rle_v3 == 0) { \
  233. rle_v2 = *buf1; \
  234. rle_v1 = 1; \
  235. if(rle_v2 > 32) { \
  236. rle_v2 -= 32; \
  237. rle_v1 = 0; \
  238. } \
  239. rle_v3 = 1; \
  240. } \
  241. buf1--;
  242. #define LP2_CHECK(buf1,rle_v3,lp2) \
  243. if(lp2 == 0 && rle_v3 != 0) \
  244. rle_v3 = 0; \
  245. else { \
  246. buf1--; \
  247. rle_v3 = 1; \
  248. }
  249. #define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
  250. rle_v2--; \
  251. if(rle_v2 == 0) { \
  252. rle_v3 = 0; \
  253. buf1 += 2; \
  254. } \
  255. lp2 = 4;
  256. static void iv_Decode_Chunk(Indeo3DecodeContext *s,
  257. unsigned char *cur, unsigned char *ref, int width, int height,
  258. unsigned char *buf1, long fflags2, unsigned char *hdr,
  259. unsigned char *buf2, int min_width_160)
  260. {
  261. unsigned char bit_buf;
  262. unsigned long bit_pos, lv, lv1, lv2;
  263. long *width_tbl, width_tbl_arr[10];
  264. char *ref_vectors;
  265. unsigned char *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
  266. uint32_t *cur_lp, *ref_lp;
  267. const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
  268. unsigned short *correction_type_sp[2];
  269. ustr_t strip_tbl[20], *strip;
  270. int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
  271. rle_v1, rle_v2, rle_v3;
  272. bit_buf = 0;
  273. ref_vectors = NULL;
  274. width_tbl = width_tbl_arr + 1;
  275. i = (width < 0 ? width + 3 : width)/4;
  276. for(j = -1; j < 8; j++)
  277. width_tbl[j] = i * j;
  278. strip = strip_tbl;
  279. for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
  280. strip->ypos = strip->xpos = 0;
  281. for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
  282. strip->height = height;
  283. strip->split_direction = 0;
  284. strip->split_flag = 0;
  285. strip->usl7 = 0;
  286. bit_pos = 0;
  287. rle_v1 = rle_v2 = rle_v3 = 0;
  288. while(strip >= strip_tbl) {
  289. if(bit_pos <= 0) {
  290. bit_pos = 8;
  291. bit_buf = *buf1++;
  292. }
  293. bit_pos -= 2;
  294. cmd = (bit_buf >> bit_pos) & 0x03;
  295. if(cmd == 0) {
  296. strip++;
  297. memcpy(strip, strip-1, sizeof(ustr_t));
  298. strip->split_flag = 1;
  299. strip->split_direction = 0;
  300. strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);
  301. continue;
  302. } else if(cmd == 1) {
  303. strip++;
  304. memcpy(strip, strip-1, sizeof(ustr_t));
  305. strip->split_flag = 1;
  306. strip->split_direction = 1;
  307. strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);
  308. continue;
  309. } else if(cmd == 2) {
  310. if(strip->usl7 == 0) {
  311. strip->usl7 = 1;
  312. ref_vectors = NULL;
  313. continue;
  314. }
  315. } else if(cmd == 3) {
  316. if(strip->usl7 == 0) {
  317. strip->usl7 = 1;
  318. ref_vectors = buf2 + (*buf1 * 2);
  319. buf1++;
  320. continue;
  321. }
  322. }
  323. cur_frm_pos = cur + width * strip->ypos + strip->xpos;
  324. if((blks_width = strip->width) < 0)
  325. blks_width += 3;
  326. blks_width >>= 2;
  327. blks_height = strip->height;
  328. if(ref_vectors != NULL) {
  329. ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
  330. ref_vectors[1] + strip->xpos;
  331. } else
  332. ref_frm_pos = cur_frm_pos - width_tbl[4];
  333. if(cmd == 2) {
  334. if(bit_pos <= 0) {
  335. bit_pos = 8;
  336. bit_buf = *buf1++;
  337. }
  338. bit_pos -= 2;
  339. cmd = (bit_buf >> bit_pos) & 0x03;
  340. if(cmd == 0 || ref_vectors != NULL) {
  341. for(lp1 = 0; lp1 < blks_width; lp1++) {
  342. for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
  343. ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
  344. cur_frm_pos += 4;
  345. ref_frm_pos += 4;
  346. }
  347. } else if(cmd != 1)
  348. return;
  349. } else {
  350. k = *buf1 >> 4;
  351. j = *buf1 & 0x0f;
  352. buf1++;
  353. lv = j + fflags2;
  354. if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
  355. cp2 = s->ModPred + ((lv - 8) << 7);
  356. cp = ref_frm_pos;
  357. for(i = 0; i < blks_width << 2; i++) {
  358. int v = *cp >> 1;
  359. *(cp++) = cp2[v];
  360. }
  361. }
  362. if(k == 1 || k == 4) {
  363. lv = (hdr[j] & 0xf) + fflags2;
  364. correction_type_sp[0] = s->corrector_type + (lv << 8);
  365. correction_lp[0] = correction + (lv << 8);
  366. lv = (hdr[j] >> 4) + fflags2;
  367. correction_lp[1] = correction + (lv << 8);
  368. correction_type_sp[1] = s->corrector_type + (lv << 8);
  369. } else {
  370. correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);
  371. correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);
  372. correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);
  373. correction_lp[0] = correction_lp[1] = correction + (lv << 8);
  374. }
  375. switch(k) {
  376. case 1:
  377. case 0: /********** CASE 0 **********/
  378. for( ; blks_height > 0; blks_height -= 4) {
  379. for(lp1 = 0; lp1 < blks_width; lp1++) {
  380. for(lp2 = 0; lp2 < 4; ) {
  381. k = *buf1++;
  382. cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
  383. ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
  384. switch(correction_type_sp[0][k]) {
  385. case 0:
  386. *cur_lp = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
  387. lp2++;
  388. break;
  389. case 1:
  390. ((unsigned short *)cur_lp)[0] = ((((unsigned short *)(ref_lp))[0] >> 1)
  391. + correction_lp[lp2 & 0x01][*buf1++]) << 1;
  392. ((unsigned short *)cur_lp)[1] = ((((unsigned short *)(ref_lp))[1] >> 1)
  393. + correction_lp[lp2 & 0x01][k]) << 1;
  394. lp2++;
  395. break;
  396. case 2:
  397. if(lp2 == 0) {
  398. for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])
  399. cur_lp[j] = ref_lp[j];
  400. lp2 += 2;
  401. }
  402. break;
  403. case 3:
  404. if(lp2 < 2) {
  405. for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])
  406. cur_lp[j] = ref_lp[j];
  407. lp2 = 3;
  408. }
  409. break;
  410. case 8:
  411. if(lp2 == 0) {
  412. RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
  413. if(rle_v1 == 1 || ref_vectors != NULL) {
  414. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  415. cur_lp[j] = ref_lp[j];
  416. }
  417. RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
  418. break;
  419. } else {
  420. rle_v1 = 1;
  421. rle_v2 = *buf1 - 1;
  422. }
  423. case 5:
  424. LP2_CHECK(buf1,rle_v3,lp2)
  425. case 4:
  426. for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])
  427. cur_lp[j] = ref_lp[j];
  428. lp2 = 4;
  429. break;
  430. case 7:
  431. if(rle_v3 != 0)
  432. rle_v3 = 0;
  433. else {
  434. buf1--;
  435. rle_v3 = 1;
  436. }
  437. case 6:
  438. if(ref_vectors != NULL) {
  439. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  440. cur_lp[j] = ref_lp[j];
  441. }
  442. lp2 = 4;
  443. break;
  444. case 9:
  445. lv1 = *buf1++;
  446. lv = (lv1 & 0x7F) << 1;
  447. lv += (lv << 8);
  448. lv += (lv << 16);
  449. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  450. cur_lp[j] = lv;
  451. LV1_CHECK(buf1,rle_v3,lv1,lp2)
  452. break;
  453. default:
  454. return;
  455. }
  456. }
  457. cur_frm_pos += 4;
  458. ref_frm_pos += 4;
  459. }
  460. cur_frm_pos += ((width - blks_width) * 4);
  461. ref_frm_pos += ((width - blks_width) * 4);
  462. }
  463. break;
  464. case 4:
  465. case 3: /********** CASE 3 **********/
  466. if(ref_vectors != NULL)
  467. return;
  468. flag1 = 1;
  469. for( ; blks_height > 0; blks_height -= 8) {
  470. for(lp1 = 0; lp1 < blks_width; lp1++) {
  471. for(lp2 = 0; lp2 < 4; ) {
  472. k = *buf1++;
  473. cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
  474. ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
  475. switch(correction_type_sp[lp2 & 0x01][k]) {
  476. case 0:
  477. cur_lp[width_tbl[1]] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
  478. if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
  479. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  480. else
  481. cur_lp[0] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
  482. lp2++;
  483. break;
  484. case 1:
  485. ((unsigned short *)cur_lp)[width_tbl[2]] =
  486. ((((unsigned short *)ref_lp)[0] >> 1) + correction_lp[lp2 & 0x01][*buf1++]) << 1;
  487. ((unsigned short *)cur_lp)[width_tbl[2]+1] =
  488. ((((unsigned short *)ref_lp)[1] >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
  489. if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)
  490. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  491. else
  492. cur_lp[0] = cur_lp[width_tbl[1]];
  493. lp2++;
  494. break;
  495. case 2:
  496. if(lp2 == 0) {
  497. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  498. cur_lp[j] = *ref_lp;
  499. lp2 += 2;
  500. }
  501. break;
  502. case 3:
  503. if(lp2 < 2) {
  504. for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
  505. cur_lp[j] = *ref_lp;
  506. lp2 = 3;
  507. }
  508. break;
  509. case 6:
  510. lp2 = 4;
  511. break;
  512. case 7:
  513. if(rle_v3 != 0)
  514. rle_v3 = 0;
  515. else {
  516. buf1--;
  517. rle_v3 = 1;
  518. }
  519. lp2 = 4;
  520. break;
  521. case 8:
  522. if(lp2 == 0) {
  523. RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
  524. if(rle_v1 == 1) {
  525. for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
  526. cur_lp[j] = ref_lp[j];
  527. }
  528. RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
  529. break;
  530. } else {
  531. rle_v2 = (*buf1) - 1;
  532. rle_v1 = 1;
  533. }
  534. case 5:
  535. LP2_CHECK(buf1,rle_v3,lp2)
  536. case 4:
  537. for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
  538. cur_lp[j] = *ref_lp;
  539. lp2 = 4;
  540. break;
  541. case 9:
  542. av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
  543. lv1 = *buf1++;
  544. lv = (lv1 & 0x7F) << 1;
  545. lv += (lv << 8);
  546. lv += (lv << 16);
  547. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  548. cur_lp[j] = lv;
  549. LV1_CHECK(buf1,rle_v3,lv1,lp2)
  550. break;
  551. default:
  552. return;
  553. }
  554. }
  555. cur_frm_pos += 4;
  556. }
  557. cur_frm_pos += (((width * 2) - blks_width) * 4);
  558. flag1 = 0;
  559. }
  560. break;
  561. case 10: /********** CASE 10 **********/
  562. if(ref_vectors == NULL) {
  563. flag1 = 1;
  564. for( ; blks_height > 0; blks_height -= 8) {
  565. for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
  566. for(lp2 = 0; lp2 < 4; ) {
  567. k = *buf1++;
  568. cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
  569. ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];
  570. lv1 = ref_lp[0];
  571. lv2 = ref_lp[1];
  572. if(lp2 == 0 && flag1 != 0) {
  573. lv1 = lv1 & 0x00FF00FF;
  574. lv1 = (lv1 << 8) | lv1;
  575. lv2 = lv2 & 0x00FF00FF;
  576. lv2 = (lv2 << 8) | lv2;
  577. }
  578. switch(correction_type_sp[lp2 & 0x01][k]) {
  579. case 0:
  580. cur_lp[width_tbl[1]] = ((lv1 >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1;
  581. cur_lp[width_tbl[1]+1] = ((lv2 >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1;
  582. if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
  583. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  584. cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
  585. } else {
  586. cur_lp[0] = cur_lp[width_tbl[1]];
  587. cur_lp[1] = cur_lp[width_tbl[1]+1];
  588. }
  589. lp2++;
  590. break;
  591. case 1:
  592. cur_lp[width_tbl[1]] = ((lv1 >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1++]) << 1;
  593. cur_lp[width_tbl[1]+1] = ((lv2 >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1;
  594. if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {
  595. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  596. cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
  597. } else {
  598. cur_lp[0] = cur_lp[width_tbl[1]];
  599. cur_lp[1] = cur_lp[width_tbl[1]+1];
  600. }
  601. lp2++;
  602. break;
  603. case 2:
  604. if(lp2 == 0) {
  605. if(flag1 != 0) {
  606. for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {
  607. cur_lp[j] = lv1;
  608. cur_lp[j+1] = lv2;
  609. }
  610. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  611. cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
  612. } else {
  613. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
  614. cur_lp[j] = lv1;
  615. cur_lp[j+1] = lv2;
  616. }
  617. }
  618. lp2 += 2;
  619. }
  620. break;
  621. case 3:
  622. if(lp2 < 2) {
  623. if(lp2 == 0 && flag1 != 0) {
  624. for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {
  625. cur_lp[j] = lv1;
  626. cur_lp[j+1] = lv2;
  627. }
  628. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  629. cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
  630. } else {
  631. for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
  632. cur_lp[j] = lv1;
  633. cur_lp[j+1] = lv2;
  634. }
  635. }
  636. lp2 = 3;
  637. }
  638. break;
  639. case 8:
  640. if(lp2 == 0) {
  641. RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
  642. if(rle_v1 == 1) {
  643. if(flag1 != 0) {
  644. for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
  645. cur_lp[j] = lv1;
  646. cur_lp[j+1] = lv2;
  647. }
  648. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  649. cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
  650. } else {
  651. for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
  652. cur_lp[j] = lv1;
  653. cur_lp[j+1] = lv2;
  654. }
  655. }
  656. }
  657. RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
  658. break;
  659. } else {
  660. rle_v1 = 1;
  661. rle_v2 = (*buf1) - 1;
  662. }
  663. case 5:
  664. LP2_CHECK(buf1,rle_v3,lp2)
  665. case 4:
  666. if(lp2 == 0 && flag1 != 0) {
  667. for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {
  668. cur_lp[j] = lv1;
  669. cur_lp[j+1] = lv2;
  670. }
  671. cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;
  672. cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;
  673. } else {
  674. for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
  675. cur_lp[j] = lv1;
  676. cur_lp[j+1] = lv2;
  677. }
  678. }
  679. lp2 = 4;
  680. break;
  681. case 6:
  682. lp2 = 4;
  683. break;
  684. case 7:
  685. if(lp2 == 0) {
  686. if(rle_v3 != 0)
  687. rle_v3 = 0;
  688. else {
  689. buf1--;
  690. rle_v3 = 1;
  691. }
  692. lp2 = 4;
  693. }
  694. break;
  695. case 9:
  696. av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
  697. lv1 = *buf1;
  698. lv = (lv1 & 0x7F) << 1;
  699. lv += (lv << 8);
  700. lv += (lv << 16);
  701. for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
  702. cur_lp[j] = lv;
  703. LV1_CHECK(buf1,rle_v3,lv1,lp2)
  704. break;
  705. default:
  706. return;
  707. }
  708. }
  709. cur_frm_pos += 8;
  710. }
  711. cur_frm_pos += (((width * 2) - blks_width) * 4);
  712. flag1 = 0;
  713. }
  714. } else {
  715. for( ; blks_height > 0; blks_height -= 8) {
  716. for(lp1 = 0; lp1 < blks_width; lp1 += 2) {
  717. for(lp2 = 0; lp2 < 4; ) {
  718. k = *buf1++;
  719. cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
  720. ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
  721. switch(correction_type_sp[lp2 & 0x01][k]) {
  722. case 0:
  723. lv1 = correctionloworder_lp[lp2 & 0x01][k];
  724. lv2 = correctionhighorder_lp[lp2 & 0x01][k];
  725. cur_lp[0] = ((ref_lp[0] >> 1) + lv1) << 1;
  726. cur_lp[1] = ((ref_lp[1] >> 1) + lv2) << 1;
  727. cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + lv1) << 1;
  728. cur_lp[width_tbl[1]+1] = ((ref_lp[width_tbl[1]+1] >> 1) + lv2) << 1;
  729. lp2++;
  730. break;
  731. case 1:
  732. lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];
  733. lv2 = correctionloworder_lp[lp2 & 0x01][k];
  734. cur_lp[0] = ((ref_lp[0] >> 1) + lv1) << 1;
  735. cur_lp[1] = ((ref_lp[1] >> 1) + lv2) << 1;
  736. cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + lv1) << 1;
  737. cur_lp[width_tbl[1]+1] = ((ref_lp[width_tbl[1]+1] >> 1) + lv2) << 1;
  738. lp2++;
  739. break;
  740. case 2:
  741. if(lp2 == 0) {
  742. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {
  743. cur_lp[j] = ref_lp[j];
  744. cur_lp[j+1] = ref_lp[j+1];
  745. }
  746. lp2 += 2;
  747. }
  748. break;
  749. case 3:
  750. if(lp2 < 2) {
  751. for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {
  752. cur_lp[j] = ref_lp[j];
  753. cur_lp[j+1] = ref_lp[j+1];
  754. }
  755. lp2 = 3;
  756. }
  757. break;
  758. case 8:
  759. if(lp2 == 0) {
  760. RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
  761. for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {
  762. ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
  763. ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];
  764. }
  765. RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
  766. break;
  767. } else {
  768. rle_v1 = 1;
  769. rle_v2 = (*buf1) - 1;
  770. }
  771. case 5:
  772. case 7:
  773. LP2_CHECK(buf1,rle_v3,lp2)
  774. case 6:
  775. case 4:
  776. for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {
  777. cur_lp[j] = ref_lp[j];
  778. cur_lp[j+1] = ref_lp[j+1];
  779. }
  780. lp2 = 4;
  781. break;
  782. case 9:
  783. av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
  784. lv1 = *buf1;
  785. lv = (lv1 & 0x7F) << 1;
  786. lv += (lv << 8);
  787. lv += (lv << 16);
  788. for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
  789. ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;
  790. LV1_CHECK(buf1,rle_v3,lv1,lp2)
  791. break;
  792. default:
  793. return;
  794. }
  795. }
  796. cur_frm_pos += 8;
  797. ref_frm_pos += 8;
  798. }
  799. cur_frm_pos += (((width * 2) - blks_width) * 4);
  800. ref_frm_pos += (((width * 2) - blks_width) * 4);
  801. }
  802. }
  803. break;
  804. case 11: /********** CASE 11 **********/
  805. if(ref_vectors == NULL)
  806. return;
  807. for( ; blks_height > 0; blks_height -= 8) {
  808. for(lp1 = 0; lp1 < blks_width; lp1++) {
  809. for(lp2 = 0; lp2 < 4; ) {
  810. k = *buf1++;
  811. cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];
  812. ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];
  813. switch(correction_type_sp[lp2 & 0x01][k]) {
  814. case 0:
  815. cur_lp[0] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
  816. cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + correction_lp[lp2 & 0x01][k]) << 1;
  817. lp2++;
  818. break;
  819. case 1:
  820. lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);
  821. lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);
  822. ((unsigned short *)cur_lp)[0] = ((((unsigned short *)ref_lp)[0] >> 1) + lv1) << 1;
  823. ((unsigned short *)cur_lp)[1] = ((((unsigned short *)ref_lp)[1] >> 1) + lv2) << 1;
  824. ((unsigned short *)cur_lp)[width_tbl[2]] = ((((unsigned short *)ref_lp)[width_tbl[2]] >> 1) + lv1) << 1;
  825. ((unsigned short *)cur_lp)[width_tbl[2]+1] = ((((unsigned short *)ref_lp)[width_tbl[2]+1] >> 1) + lv2) << 1;
  826. lp2++;
  827. break;
  828. case 2:
  829. if(lp2 == 0) {
  830. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  831. cur_lp[j] = ref_lp[j];
  832. lp2 += 2;
  833. }
  834. break;
  835. case 3:
  836. if(lp2 < 2) {
  837. for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])
  838. cur_lp[j] = ref_lp[j];
  839. lp2 = 3;
  840. }
  841. break;
  842. case 8:
  843. if(lp2 == 0) {
  844. RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)
  845. for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])
  846. cur_lp[j] = ref_lp[j];
  847. RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)
  848. break;
  849. } else {
  850. rle_v1 = 1;
  851. rle_v2 = (*buf1) - 1;
  852. }
  853. case 5:
  854. case 7:
  855. LP2_CHECK(buf1,rle_v3,lp2)
  856. case 4:
  857. case 6:
  858. for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])
  859. cur_lp[j] = ref_lp[j];
  860. lp2 = 4;
  861. break;
  862. case 9:
  863. av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");
  864. lv1 = *buf1++;
  865. lv = (lv1 & 0x7F) << 1;
  866. lv += (lv << 8);
  867. lv += (lv << 16);
  868. for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])
  869. cur_lp[j] = lv;
  870. LV1_CHECK(buf1,rle_v3,lv1,lp2)
  871. break;
  872. default:
  873. return;
  874. }
  875. }
  876. cur_frm_pos += 4;
  877. ref_frm_pos += 4;
  878. }
  879. cur_frm_pos += (((width * 2) - blks_width) * 4);
  880. ref_frm_pos += (((width * 2) - blks_width) * 4);
  881. }
  882. break;
  883. default:
  884. return;
  885. }
  886. }
  887. if(strip < strip_tbl)
  888. return;
  889. for( ; strip >= strip_tbl; strip--) {
  890. if(strip->split_flag != 0) {
  891. strip->split_flag = 0;
  892. strip->usl7 = (strip-1)->usl7;
  893. if(strip->split_direction) {
  894. strip->xpos += strip->width;
  895. strip->width = (strip-1)->width - strip->width;
  896. if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)
  897. strip->width = width - strip->xpos;
  898. } else {
  899. strip->ypos += strip->height;
  900. strip->height = (strip-1)->height - strip->height;
  901. }
  902. break;
  903. }
  904. }
  905. }
  906. }
  907. static int indeo3_decode_init(AVCodecContext *avctx)
  908. {
  909. Indeo3DecodeContext *s = avctx->priv_data;
  910. s->avctx = avctx;
  911. s->width = avctx->width;
  912. s->height = avctx->height;
  913. avctx->pix_fmt = PIX_FMT_YUV410P;
  914. avctx->has_b_frames = 0;
  915. build_modpred(s);
  916. iv_alloc_frames(s);
  917. return 0;
  918. }
  919. static int indeo3_decode_frame(AVCodecContext *avctx,
  920. void *data, int *data_size,
  921. unsigned char *buf, int buf_size)
  922. {
  923. Indeo3DecodeContext *s=avctx->priv_data;
  924. unsigned char *src, *dest;
  925. int y;
  926. iv_decode_frame(s, buf, buf_size);
  927. if(s->frame.data[0])
  928. avctx->release_buffer(avctx, &s->frame);
  929. s->frame.reference = 0;
  930. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  931. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  932. return -1;
  933. }
  934. src = s->cur_frame->Ybuf;
  935. dest = s->frame.data[0];
  936. for (y = 0; y < s->height; y++) {
  937. memcpy(dest, src, s->cur_frame->y_w);
  938. src += s->cur_frame->y_w;
  939. dest += s->frame.linesize[0];
  940. }
  941. if (!(s->avctx->flags & CODEC_FLAG_GRAY))
  942. {
  943. src = s->cur_frame->Ubuf;
  944. dest = s->frame.data[1];
  945. for (y = 0; y < s->height / 4; y++) {
  946. memcpy(dest, src, s->cur_frame->uv_w);
  947. src += s->cur_frame->uv_w;
  948. dest += s->frame.linesize[1];
  949. }
  950. src = s->cur_frame->Vbuf;
  951. dest = s->frame.data[2];
  952. for (y = 0; y < s->height / 4; y++) {
  953. memcpy(dest, src, s->cur_frame->uv_w);
  954. src += s->cur_frame->uv_w;
  955. dest += s->frame.linesize[2];
  956. }
  957. }
  958. *data_size=sizeof(AVFrame);
  959. *(AVFrame*)data= s->frame;
  960. return buf_size;
  961. }
  962. static int indeo3_decode_end(AVCodecContext *avctx)
  963. {
  964. Indeo3DecodeContext *s = avctx->priv_data;
  965. iv_free_func(s);
  966. return 0;
  967. }
  968. AVCodec indeo3_decoder = {
  969. "indeo3",
  970. CODEC_TYPE_VIDEO,
  971. CODEC_ID_INDEO3,
  972. sizeof(Indeo3DecodeContext),
  973. indeo3_decode_init,
  974. NULL,
  975. indeo3_decode_end,
  976. indeo3_decode_frame,
  977. 0,
  978. NULL
  979. };