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.

429 lines
14KB

  1. /*
  2. * IBM Ultimotion Video Decoder
  3. * Copyright (C) 2004 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file ulti.c
  24. * IBM Ultimotion Video Decoder.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "common.h"
  31. #include "avcodec.h"
  32. #include "ulti_cb.h"
  33. typedef struct UltimotionDecodeContext {
  34. AVCodecContext *avctx;
  35. int width, height, blocks;
  36. AVFrame frame;
  37. const uint8_t *ulti_codebook;
  38. } UltimotionDecodeContext;
  39. static int ulti_decode_init(AVCodecContext *avctx)
  40. {
  41. UltimotionDecodeContext *s = avctx->priv_data;
  42. s->avctx = avctx;
  43. s->width = avctx->width;
  44. s->height = avctx->height;
  45. s->blocks = (s->width / 8) * (s->height / 8);
  46. avctx->pix_fmt = PIX_FMT_YUV410P;
  47. avctx->has_b_frames = 0;
  48. avctx->coded_frame = (AVFrame*) &s->frame;
  49. s->ulti_codebook = ulti_codebook;
  50. return 0;
  51. }
  52. static int block_coords[8] = // 4x4 block coords in 8x8 superblock
  53. { 0, 0, 0, 4, 4, 4, 4, 0};
  54. static int angle_by_index[4] = { 0, 2, 6, 12};
  55. /* Lookup tables for luma and chroma - used by ulti_convert_yuv() */
  56. static uint8_t ulti_lumas[64] =
  57. { 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28,
  58. 0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44,
  59. 0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60,
  60. 0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C,
  61. 0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98,
  62. 0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3,
  63. 0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF,
  64. 0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB};
  65. static uint8_t ulti_chromas[16] =
  66. { 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D,
  67. 0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0};
  68. /* convert Ultimotion YUV block (sixteen 6-bit Y samples and
  69. two 4-bit chroma samples) into standard YUV and put it into frame */
  70. static void ulti_convert_yuv(AVFrame *frame, int x, int y,
  71. uint8_t *luma,int chroma)
  72. {
  73. uint8_t *y_plane, *cr_plane, *cb_plane;
  74. int i;
  75. y_plane = frame->data[0] + x + y * frame->linesize[0];
  76. cr_plane = frame->data[1] + (x / 4) + (y / 4) * frame->linesize[1];
  77. cb_plane = frame->data[2] + (x / 4) + (y / 4) * frame->linesize[2];
  78. cr_plane[0] = ulti_chromas[chroma >> 4];
  79. cb_plane[0] = ulti_chromas[chroma & 0xF];
  80. for(i = 0; i < 16; i++){
  81. y_plane[i & 3] = ulti_lumas[luma[i]];
  82. if((i & 3) == 3) { //next row
  83. y_plane += frame->linesize[0];
  84. }
  85. }
  86. }
  87. /* generate block like in MS Video1 */
  88. static void ulti_pattern(AVFrame *frame, int x, int y,
  89. int f0, int f1, int Y0, int Y1, int chroma)
  90. {
  91. uint8_t Luma[16];
  92. int mask, i;
  93. for(mask = 0x80, i = 0; mask; mask >>= 1, i++) {
  94. if(f0 & mask)
  95. Luma[i] = Y1;
  96. else
  97. Luma[i] = Y0;
  98. }
  99. for(mask = 0x80, i = 8; mask; mask >>= 1, i++) {
  100. if(f1 & mask)
  101. Luma[i] = Y1;
  102. else
  103. Luma[i] = Y0;
  104. }
  105. ulti_convert_yuv(frame, x, y, Luma, chroma);
  106. }
  107. /* fill block with some gradient */
  108. static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
  109. {
  110. uint8_t Luma[16];
  111. if(angle & 8) { //reverse order
  112. int t;
  113. angle &= 0x7;
  114. t = Y[0];
  115. Y[0] = Y[3];
  116. Y[3] = t;
  117. t = Y[1];
  118. Y[1] = Y[2];
  119. Y[2] = t;
  120. }
  121. switch(angle){
  122. case 0:
  123. Luma[0] = Y[0]; Luma[1] = Y[1]; Luma[2] = Y[2]; Luma[3] = Y[3];
  124. Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3];
  125. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
  126. Luma[12] = Y[0]; Luma[13] = Y[1]; Luma[14] = Y[2]; Luma[15] = Y[3];
  127. break;
  128. case 1:
  129. Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3];
  130. Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3];
  131. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
  132. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
  133. break;
  134. case 2:
  135. Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3];
  136. Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3];
  137. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
  138. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
  139. break;
  140. case 3:
  141. Luma[0] = Y[2]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3];
  142. Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3];
  143. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
  144. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[1];
  145. break;
  146. case 4:
  147. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3];
  148. Luma[4] = Y[2]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[2];
  149. Luma[8] = Y[1]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[1];
  150. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
  151. break;
  152. case 5:
  153. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[2];
  154. Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[1];
  155. Luma[8] = Y[2]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[0];
  156. Luma[12] = Y[1]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
  157. break;
  158. case 6:
  159. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[2];
  160. Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[1];
  161. Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
  162. Luma[12] = Y[1]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
  163. break;
  164. case 7:
  165. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[1];
  166. Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[0];
  167. Luma[8] = Y[3]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
  168. Luma[12] = Y[2]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
  169. break;
  170. default:
  171. Luma[0] = Y[0]; Luma[1] = Y[0]; Luma[2] = Y[1]; Luma[3] = Y[1];
  172. Luma[4] = Y[0]; Luma[5] = Y[0]; Luma[6] = Y[1]; Luma[7] = Y[1];
  173. Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[3]; Luma[11] = Y[3];
  174. Luma[12] = Y[2]; Luma[13] = Y[2]; Luma[14] = Y[3]; Luma[15] = Y[3];
  175. break;
  176. }
  177. ulti_convert_yuv(frame, x, y, Luma, chroma);
  178. }
  179. static int ulti_decode_frame(AVCodecContext *avctx,
  180. void *data, int *data_size,
  181. uint8_t *buf, int buf_size)
  182. {
  183. UltimotionDecodeContext *s=avctx->priv_data;
  184. int modifier = 0;
  185. int uniq = 0;
  186. int mode = 0;
  187. int blocks = 0;
  188. int done = 0;
  189. int x = 0, y = 0;
  190. int i;
  191. int skip;
  192. int tmp;
  193. if(s->frame.data[0])
  194. avctx->release_buffer(avctx, &s->frame);
  195. s->frame.reference = 1;
  196. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  197. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  198. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  199. return -1;
  200. }
  201. while(!done) {
  202. int idx;
  203. if(blocks >= s->blocks || y >= s->height)
  204. break;//all blocks decoded
  205. idx = *buf++;
  206. if((idx & 0xF8) == 0x70) {
  207. switch(idx) {
  208. case 0x70: //change modifier
  209. modifier = *buf++;
  210. if(modifier>1)
  211. av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
  212. break;
  213. case 0x71: // set uniq flag
  214. uniq = 1;
  215. break;
  216. case 0x72: //toggle mode
  217. mode = !mode;
  218. break;
  219. case 0x73: //end-of-frame
  220. done = 1;
  221. break;
  222. case 0x74: //skip some blocks
  223. skip = *buf++;
  224. if ((blocks + skip) >= s->blocks)
  225. break;
  226. blocks += skip;
  227. x += skip * 8;
  228. while(x >= s->width) {
  229. x -= s->width;
  230. y += 8;
  231. }
  232. break;
  233. default:
  234. av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
  235. }
  236. } else { //handle one block
  237. int code;
  238. int cf;
  239. int angle = 0;
  240. uint8_t Y[4]; // luma samples of block
  241. int tx = 0, ty = 0; //coords of subblock
  242. int chroma = 0;
  243. if (mode || uniq) {
  244. uniq = 0;
  245. cf = 1;
  246. chroma = 0;
  247. } else {
  248. cf = 0;
  249. if (idx)
  250. chroma = *buf++;
  251. }
  252. for (i = 0; i < 4; i++) { // for every subblock
  253. code = (idx >> (6 - i*2)) & 3; //extract 2 bits
  254. if(!code) //skip subblock
  255. continue;
  256. if(cf)
  257. chroma = *buf++;
  258. tx = x + block_coords[i * 2];
  259. ty = y + block_coords[(i * 2) + 1];
  260. switch(code) {
  261. case 1:
  262. tmp = *buf++;
  263. angle = angle_by_index[(tmp >> 6) & 0x3];
  264. Y[0] = tmp & 0x3F;
  265. Y[1] = Y[0];
  266. if (angle) {
  267. Y[2] = Y[0]+1;
  268. if (Y[2] > 0x3F)
  269. Y[2] = 0x3F;
  270. Y[3] = Y[2];
  271. } else {
  272. Y[2] = Y[0];
  273. Y[3] = Y[0];
  274. }
  275. break;
  276. case 2:
  277. if (modifier) { // unpack four luma samples
  278. tmp = (*buf++) << 16;
  279. tmp += (*buf++) << 8;
  280. tmp += *buf++;
  281. Y[0] = (tmp >> 18) & 0x3F;
  282. Y[1] = (tmp >> 12) & 0x3F;
  283. Y[2] = (tmp >> 6) & 0x3F;
  284. Y[3] = tmp & 0x3F;
  285. angle = 16;
  286. } else { // retrieve luma samples from codebook
  287. tmp = (*buf++) << 8;
  288. tmp += (*buf++);
  289. angle = (tmp >> 12) & 0xF;
  290. tmp &= 0xFFF;
  291. tmp <<= 2;
  292. Y[0] = s->ulti_codebook[tmp];
  293. Y[1] = s->ulti_codebook[tmp + 1];
  294. Y[2] = s->ulti_codebook[tmp + 2];
  295. Y[3] = s->ulti_codebook[tmp + 3];
  296. }
  297. break;
  298. case 3:
  299. if (modifier) { // all 16 luma samples
  300. uint8_t Luma[16];
  301. tmp = (*buf++) << 16;
  302. tmp += (*buf++) << 8;
  303. tmp += *buf++;
  304. Luma[0] = (tmp >> 18) & 0x3F;
  305. Luma[1] = (tmp >> 12) & 0x3F;
  306. Luma[2] = (tmp >> 6) & 0x3F;
  307. Luma[3] = tmp & 0x3F;
  308. tmp = (*buf++) << 16;
  309. tmp += (*buf++) << 8;
  310. tmp += *buf++;
  311. Luma[4] = (tmp >> 18) & 0x3F;
  312. Luma[5] = (tmp >> 12) & 0x3F;
  313. Luma[6] = (tmp >> 6) & 0x3F;
  314. Luma[7] = tmp & 0x3F;
  315. tmp = (*buf++) << 16;
  316. tmp += (*buf++) << 8;
  317. tmp += *buf++;
  318. Luma[8] = (tmp >> 18) & 0x3F;
  319. Luma[9] = (tmp >> 12) & 0x3F;
  320. Luma[10] = (tmp >> 6) & 0x3F;
  321. Luma[11] = tmp & 0x3F;
  322. tmp = (*buf++) << 16;
  323. tmp += (*buf++) << 8;
  324. tmp += *buf++;
  325. Luma[12] = (tmp >> 18) & 0x3F;
  326. Luma[13] = (tmp >> 12) & 0x3F;
  327. Luma[14] = (tmp >> 6) & 0x3F;
  328. Luma[15] = tmp & 0x3F;
  329. ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
  330. } else {
  331. tmp = *buf++;
  332. if(tmp & 0x80) {
  333. angle = (tmp >> 4) & 0x7;
  334. tmp = (tmp << 8) + *buf++;
  335. Y[0] = (tmp >> 6) & 0x3F;
  336. Y[1] = tmp & 0x3F;
  337. Y[2] = (*buf++) & 0x3F;
  338. Y[3] = (*buf++) & 0x3F;
  339. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
  340. } else { // some patterns
  341. int f0, f1;
  342. f0 = *buf++;
  343. f1 = tmp;
  344. Y[0] = (*buf++) & 0x3F;
  345. Y[1] = (*buf++) & 0x3F;
  346. ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
  347. }
  348. }
  349. break;
  350. }
  351. if(code != 3)
  352. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
  353. }
  354. blocks++;
  355. x += 8;
  356. if(x >= s->width) {
  357. x = 0;
  358. y += 8;
  359. }
  360. }
  361. }
  362. *data_size=sizeof(AVFrame);
  363. *(AVFrame*)data= s->frame;
  364. return buf_size;
  365. }
  366. static int ulti_decode_end(AVCodecContext *avctx)
  367. {
  368. /* UltimotionDecodeContext *s = avctx->priv_data;*/
  369. return 0;
  370. }
  371. AVCodec ulti_decoder = {
  372. "ultimotion",
  373. CODEC_TYPE_VIDEO,
  374. CODEC_ID_ULTI,
  375. sizeof(UltimotionDecodeContext),
  376. ulti_decode_init,
  377. NULL,
  378. ulti_decode_end,
  379. ulti_decode_frame,
  380. CODEC_CAP_DR1,
  381. NULL
  382. };