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 <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "bytestream.h"
  33. #include "indeo3data.h"
  34. typedef struct
  35. {
  36. uint8_t *Ybuf;
  37. uint8_t *Ubuf;
  38. uint8_t *Vbuf;
  39. unsigned short y_w, y_h;
  40. unsigned short uv_w, uv_h;
  41. } YUVBufs;
  42. typedef struct Indeo3DecodeContext {
  43. AVCodecContext *avctx;
  44. int width, height;
  45. AVFrame frame;
  46. uint8_t *buf;
  47. YUVBufs iv_frame[2];
  48. YUVBufs *cur_frame;
  49. YUVBufs *ref_frame;
  50. uint8_t *ModPred;
  51. uint8_t *corrector_type;
  52. } Indeo3DecodeContext;
  53. static const uint8_t corrector_type_0[24] = {
  54. 195, 159, 133, 115, 101, 93, 87, 77,
  55. 195, 159, 133, 115, 101, 93, 87, 77,
  56. 128, 79, 79, 79, 79, 79, 79, 79
  57. };
  58. static const uint8_t corrector_type_2[8] = { 9, 7, 6, 8, 5, 4, 3, 2 };
  59. static av_cold int build_modpred(Indeo3DecodeContext *s)
  60. {
  61. int i, j;
  62. if (!(s->ModPred = av_malloc(8 * 128)))
  63. return AVERROR(ENOMEM);
  64. for (i=0; i < 128; ++i) {
  65. s->ModPred[i+0*128] = i > 126 ? 254 : 2*(i + 1 - ((i + 1) % 2));
  66. s->ModPred[i+1*128] = i == 7 ? 20 :
  67. i == 119 ||
  68. i == 120 ? 236 : 2*(i + 2 - ((i + 1) % 3));
  69. s->ModPred[i+2*128] = i > 125 ? 248 : 2*(i + 2 - ((i + 2) % 4));
  70. s->ModPred[i+3*128] = 2*(i + 1 - ((i - 3) % 5));
  71. s->ModPred[i+4*128] = i == 8 ? 20 : 2*(i + 1 - ((i - 3) % 6));
  72. s->ModPred[i+5*128] = 2*(i + 4 - ((i + 3) % 7));
  73. s->ModPred[i+6*128] = i > 123 ? 240 : 2*(i + 4 - ((i + 4) % 8));
  74. s->ModPred[i+7*128] = 2*(i + 5 - ((i + 4) % 9));
  75. }
  76. if (!(s->corrector_type = av_malloc(24 * 256)))
  77. return AVERROR(ENOMEM);
  78. for (i=0; i < 24; ++i) {
  79. for (j=0; j < 256; ++j) {
  80. s->corrector_type[i*256+j] = j < corrector_type_0[i] ? 1 :
  81. j < 248 || (i == 16 && j == 248) ? 0 :
  82. corrector_type_2[j - 248];
  83. }
  84. }
  85. return 0;
  86. }
  87. static av_cold int iv_alloc_frames(Indeo3DecodeContext *s)
  88. {
  89. int luma_width = (s->width + 3) & ~3,
  90. luma_height = (s->height + 3) & ~3,
  91. chroma_width = ((luma_width >> 2) + 3) & ~3,
  92. chroma_height = ((luma_height >> 2) + 3) & ~3,
  93. luma_pixels = luma_width * luma_height,
  94. chroma_pixels = chroma_width * chroma_height,
  95. i;
  96. unsigned int bufsize = luma_pixels * 2 + luma_width * 3 +
  97. (chroma_pixels + chroma_width) * 4;
  98. av_freep(&s->buf);
  99. if(!(s->buf = av_malloc(bufsize)))
  100. return AVERROR(ENOMEM);
  101. s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;
  102. s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;
  103. s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;
  104. s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;
  105. s->iv_frame[0].Ybuf = s->buf + luma_width;
  106. i = luma_pixels + luma_width * 2;
  107. s->iv_frame[1].Ybuf = s->buf + i;
  108. i += (luma_pixels + luma_width);
  109. s->iv_frame[0].Ubuf = s->buf + i;
  110. i += (chroma_pixels + chroma_width);
  111. s->iv_frame[1].Ubuf = s->buf + i;
  112. i += (chroma_pixels + chroma_width);
  113. s->iv_frame[0].Vbuf = s->buf + i;
  114. i += (chroma_pixels + chroma_width);
  115. s->iv_frame[1].Vbuf = s->buf + i;
  116. for(i = 1; i <= luma_width; i++)
  117. s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =
  118. s->iv_frame[0].Ubuf[-i] = 0x80;
  119. for(i = 1; i <= chroma_width; i++) {
  120. s->iv_frame[1].Ubuf[-i] = 0x80;
  121. s->iv_frame[0].Vbuf[-i] = 0x80;
  122. s->iv_frame[1].Vbuf[-i] = 0x80;
  123. s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;
  124. }
  125. return 0;
  126. }
  127. static av_cold void iv_free_func(Indeo3DecodeContext *s)
  128. {
  129. av_freep(&s->buf);
  130. av_freep(&s->ModPred);
  131. av_freep(&s->corrector_type);
  132. }
  133. struct ustr {
  134. int xpos;
  135. int ypos;
  136. int width;
  137. int height;
  138. int split_flag;
  139. int split_direction;
  140. int usl7;
  141. };
  142. #define LV1_CHECK(buf1,rle_v3,lv1,lp2) \
  143. if((lv1 & 0x80) != 0) { \
  144. if(rle_v3 != 0) \
  145. rle_v3 = 0; \
  146. else { \
  147. rle_v3 = 1; \
  148. buf1 -= 2; \
  149. } \
  150. } \
  151. lp2 = 4;
  152. #define RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3) \
  153. if(rle_v3 == 0) { \
  154. rle_v2 = *buf1; \
  155. rle_v1 = 1; \
  156. if(rle_v2 > 32) { \
  157. rle_v2 -= 32; \
  158. rle_v1 = 0; \
  159. } \
  160. rle_v3 = 1; \
  161. } \
  162. buf1--;
  163. #define LP2_CHECK(buf1,rle_v3,lp2) \
  164. if(lp2 == 0 && rle_v3 != 0) \
  165. rle_v3 = 0; \
  166. else { \
  167. buf1--; \
  168. rle_v3 = 1; \
  169. }
  170. #define RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2) \
  171. rle_v2--; \
  172. if(rle_v2 == 0) { \
  173. rle_v3 = 0; \
  174. buf1 += 2; \
  175. } \
  176. lp2 = 4;
  177. static void iv_Decode_Chunk(Indeo3DecodeContext *s,
  178. uint8_t *cur, uint8_t *ref, int width, int height,
  179. const uint8_t *buf1, int cb_offset, const uint8_t *hdr,
  180. const uint8_t *buf2, int min_width_160)
  181. {
  182. uint8_t bit_buf;
  183. unsigned int bit_pos, lv, lv1, lv2;
  184. int *width_tbl, width_tbl_arr[10];
  185. const signed char *ref_vectors;
  186. uint8_t *cur_frm_pos, *ref_frm_pos, *cp, *cp2;
  187. uint32_t *cur_lp, *ref_lp;
  188. const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];
  189. uint8_t *correction_type_sp[2];
  190. struct ustr strip_tbl[20], *strip;
  191. int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,
  192. rle_v1, rle_v2, rle_v3;
  193. unsigned short res;
  194. bit_buf = 0;
  195. ref_vectors = NULL;
  196. width_tbl = width_tbl_arr + 1;
  197. i = (width < 0 ? width + 3 : width)/4;
  198. for(j = -1; j < 8; j++)
  199. width_tbl[j] = i * j;
  200. strip = strip_tbl;
  201. for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);
  202. strip->ypos = strip->xpos = 0;
  203. for(strip->width = min_width_160; width > strip->width; strip->width *= 2);
  204. strip->height = height;
  205. strip->split_direction = 0;
  206. strip->split_flag = 0;
  207. strip->usl7 = 0;
  208. bit_pos = 0;
  209. rle_v1 = rle_v2 = rle_v3 = 0;
  210. while(strip >= strip_tbl) {
  211. if(bit_pos <= 0) {
  212. bit_pos = 8;
  213. bit_buf = *buf1++;
  214. }
  215. bit_pos -= 2;
  216. cmd = (bit_buf >> bit_pos) & 0x03;
  217. if(cmd == 0) {
  218. strip++;
  219. if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
  220. av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
  221. break;
  222. }
  223. memcpy(strip, strip-1, sizeof(*strip));
  224. strip->split_flag = 1;
  225. strip->split_direction = 0;
  226. strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);
  227. continue;
  228. } else if(cmd == 1) {
  229. strip++;
  230. if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {
  231. av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");
  232. break;
  233. }
  234. memcpy(strip, strip-1, sizeof(*strip));
  235. strip->split_flag = 1;
  236. strip->split_direction = 1;
  237. strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);
  238. continue;
  239. } else if(cmd == 2) {
  240. if(strip->usl7 == 0) {
  241. strip->usl7 = 1;
  242. ref_vectors = NULL;
  243. continue;
  244. }
  245. } else if(cmd == 3) {
  246. if(strip->usl7 == 0) {
  247. strip->usl7 = 1;
  248. ref_vectors = (const signed char*)buf2 + (*buf1 * 2);
  249. buf1++;
  250. continue;
  251. }
  252. }
  253. cur_frm_pos = cur + width * strip->ypos + strip->xpos;
  254. if((blks_width = strip->width) < 0)
  255. blks_width += 3;
  256. blks_width >>= 2;
  257. blks_height = strip->height;
  258. if(ref_vectors != NULL) {
  259. ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +
  260. ref_vectors[1] + strip->xpos;
  261. } else
  262. ref_frm_pos = cur_frm_pos - width_tbl[4];
  263. if(cmd == 2) {
  264. if(bit_pos <= 0) {
  265. bit_pos = 8;
  266. bit_buf = *buf1++;
  267. }
  268. bit_pos -= 2;
  269. cmd = (bit_buf >> bit_pos) & 0x03;
  270. if(cmd == 0 || ref_vectors != NULL) {
  271. for(lp1 = 0; lp1 < blks_width; lp1++) {
  272. for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])
  273. ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];
  274. cur_frm_pos += 4;
  275. ref_frm_pos += 4;
  276. }
  277. } else if(cmd != 1)
  278. return;
  279. } else {
  280. k = *buf1 >> 4;
  281. j = *buf1 & 0x0f;
  282. buf1++;
  283. lv = j + cb_offset;
  284. if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {
  285. cp2 = s->ModPred + ((lv - 8) << 7);
  286. cp = ref_frm_pos;
  287. for(i = 0; i < blks_width << 2; i++) {
  288. int v = *cp >> 1;
  289. *(cp++) = cp2[v];
  290. }
  291. }
  292. if(k == 1 || k == 4) {
  293. lv = (hdr[j] & 0xf) + cb_offset;
  294. correction_type_sp[0] = s->corrector_type + (lv << 8);
  295. correction_lp[0] = correction + (lv << 8);
  296. lv = (hdr[j] >> 4) + cb_offset;
  297. correction_lp[1] = correction + (lv << 8);
  298. correction_type_sp[1] = s->corrector_type + (lv << 8);
  299. } else {
  300. correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);
  301. correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);
  302. correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);
  303. correction_lp[0] = correction_lp[1] = correction + (lv << 8);
  304. }
  305. switch(k) {
  306. case 1:
  307. case 0: /********** CASE 0 **********/
  308. for( ; blks_height > 0; blks_height -= 4) {
  309. for(lp1 = 0; lp1 < blks_width; lp1++) {
  310. for(lp2 = 0; lp2 < 4; ) {
  311. k = *buf1++;
  312. cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];
  313. ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];
  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. return 0;
  996. }
  997. AVCodec ff_indeo3_decoder = {
  998. "indeo3",
  999. AVMEDIA_TYPE_VIDEO,
  1000. CODEC_ID_INDEO3,
  1001. sizeof(Indeo3DecodeContext),
  1002. indeo3_decode_init,
  1003. NULL,
  1004. indeo3_decode_end,
  1005. indeo3_decode_frame,
  1006. CODEC_CAP_DR1,
  1007. NULL,
  1008. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
  1009. };