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.

1151 lines
48KB

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