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.

1160 lines
48KB

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