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.

419 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. * @file libavcodec/ulti.c
  23. * IBM Ultimotion Video Decoder.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "ulti_cb.h"
  32. typedef struct UltimotionDecodeContext {
  33. AVCodecContext *avctx;
  34. int width, height, blocks;
  35. AVFrame frame;
  36. const uint8_t *ulti_codebook;
  37. } UltimotionDecodeContext;
  38. static av_cold int ulti_decode_init(AVCodecContext *avctx)
  39. {
  40. UltimotionDecodeContext *s = avctx->priv_data;
  41. s->avctx = avctx;
  42. s->width = avctx->width;
  43. s->height = avctx->height;
  44. s->blocks = (s->width / 8) * (s->height / 8);
  45. avctx->pix_fmt = PIX_FMT_YUV410P;
  46. avctx->coded_frame = (AVFrame*) &s->frame;
  47. s->ulti_codebook = ulti_codebook;
  48. return 0;
  49. }
  50. static const int block_coords[8] = // 4x4 block coords in 8x8 superblock
  51. { 0, 0, 0, 4, 4, 4, 4, 0};
  52. static const int angle_by_index[4] = { 0, 2, 6, 12};
  53. /* Lookup tables for luma and chroma - used by ulti_convert_yuv() */
  54. static const uint8_t ulti_lumas[64] =
  55. { 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28,
  56. 0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44,
  57. 0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60,
  58. 0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C,
  59. 0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98,
  60. 0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3,
  61. 0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF,
  62. 0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB};
  63. static const uint8_t ulti_chromas[16] =
  64. { 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D,
  65. 0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0};
  66. /* convert Ultimotion YUV block (sixteen 6-bit Y samples and
  67. two 4-bit chroma samples) into standard YUV and put it into frame */
  68. static void ulti_convert_yuv(AVFrame *frame, int x, int y,
  69. uint8_t *luma,int chroma)
  70. {
  71. uint8_t *y_plane, *cr_plane, *cb_plane;
  72. int i;
  73. y_plane = frame->data[0] + x + y * frame->linesize[0];
  74. cr_plane = frame->data[1] + (x / 4) + (y / 4) * frame->linesize[1];
  75. cb_plane = frame->data[2] + (x / 4) + (y / 4) * frame->linesize[2];
  76. cr_plane[0] = ulti_chromas[chroma >> 4];
  77. cb_plane[0] = ulti_chromas[chroma & 0xF];
  78. for(i = 0; i < 16; i++){
  79. y_plane[i & 3] = ulti_lumas[luma[i]];
  80. if((i & 3) == 3) { //next row
  81. y_plane += frame->linesize[0];
  82. }
  83. }
  84. }
  85. /* generate block like in MS Video1 */
  86. static void ulti_pattern(AVFrame *frame, int x, int y,
  87. int f0, int f1, int Y0, int Y1, int chroma)
  88. {
  89. uint8_t Luma[16];
  90. int mask, i;
  91. for(mask = 0x80, i = 0; mask; mask >>= 1, i++) {
  92. if(f0 & mask)
  93. Luma[i] = Y1;
  94. else
  95. Luma[i] = Y0;
  96. }
  97. for(mask = 0x80, i = 8; mask; mask >>= 1, i++) {
  98. if(f1 & mask)
  99. Luma[i] = Y1;
  100. else
  101. Luma[i] = Y0;
  102. }
  103. ulti_convert_yuv(frame, x, y, Luma, chroma);
  104. }
  105. /* fill block with some gradient */
  106. static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
  107. {
  108. uint8_t Luma[16];
  109. if(angle & 8) { //reverse order
  110. int t;
  111. angle &= 0x7;
  112. t = Y[0];
  113. Y[0] = Y[3];
  114. Y[3] = t;
  115. t = Y[1];
  116. Y[1] = Y[2];
  117. Y[2] = t;
  118. }
  119. switch(angle){
  120. case 0:
  121. Luma[0] = Y[0]; Luma[1] = Y[1]; Luma[2] = Y[2]; Luma[3] = Y[3];
  122. Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3];
  123. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
  124. Luma[12] = Y[0]; Luma[13] = Y[1]; Luma[14] = Y[2]; Luma[15] = Y[3];
  125. break;
  126. case 1:
  127. Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3];
  128. Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3];
  129. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
  130. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
  131. break;
  132. case 2:
  133. Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3];
  134. Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3];
  135. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
  136. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
  137. break;
  138. case 3:
  139. Luma[0] = Y[2]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3];
  140. Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3];
  141. Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
  142. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[1];
  143. break;
  144. case 4:
  145. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3];
  146. Luma[4] = Y[2]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[2];
  147. Luma[8] = Y[1]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[1];
  148. Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
  149. break;
  150. case 5:
  151. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[2];
  152. Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[1];
  153. Luma[8] = Y[2]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[0];
  154. Luma[12] = Y[1]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
  155. break;
  156. case 6:
  157. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[2];
  158. Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[1];
  159. Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
  160. Luma[12] = Y[1]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
  161. break;
  162. case 7:
  163. Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[1];
  164. Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[0];
  165. Luma[8] = Y[3]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
  166. Luma[12] = Y[2]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
  167. break;
  168. default:
  169. Luma[0] = Y[0]; Luma[1] = Y[0]; Luma[2] = Y[1]; Luma[3] = Y[1];
  170. Luma[4] = Y[0]; Luma[5] = Y[0]; Luma[6] = Y[1]; Luma[7] = Y[1];
  171. Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[3]; Luma[11] = Y[3];
  172. Luma[12] = Y[2]; Luma[13] = Y[2]; Luma[14] = Y[3]; Luma[15] = Y[3];
  173. break;
  174. }
  175. ulti_convert_yuv(frame, x, y, Luma, chroma);
  176. }
  177. static int ulti_decode_frame(AVCodecContext *avctx,
  178. void *data, int *data_size,
  179. AVPacket *avpkt)
  180. {
  181. const uint8_t *buf = avpkt->data;
  182. int buf_size = avpkt->size;
  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 = bytestream_get_be24(&buf);
  279. Y[0] = (tmp >> 18) & 0x3F;
  280. Y[1] = (tmp >> 12) & 0x3F;
  281. Y[2] = (tmp >> 6) & 0x3F;
  282. Y[3] = tmp & 0x3F;
  283. angle = 16;
  284. } else { // retrieve luma samples from codebook
  285. tmp = bytestream_get_be16(&buf);
  286. angle = (tmp >> 12) & 0xF;
  287. tmp &= 0xFFF;
  288. tmp <<= 2;
  289. Y[0] = s->ulti_codebook[tmp];
  290. Y[1] = s->ulti_codebook[tmp + 1];
  291. Y[2] = s->ulti_codebook[tmp + 2];
  292. Y[3] = s->ulti_codebook[tmp + 3];
  293. }
  294. break;
  295. case 3:
  296. if (modifier) { // all 16 luma samples
  297. uint8_t Luma[16];
  298. tmp = bytestream_get_be24(&buf);
  299. Luma[0] = (tmp >> 18) & 0x3F;
  300. Luma[1] = (tmp >> 12) & 0x3F;
  301. Luma[2] = (tmp >> 6) & 0x3F;
  302. Luma[3] = tmp & 0x3F;
  303. tmp = bytestream_get_be24(&buf);
  304. Luma[4] = (tmp >> 18) & 0x3F;
  305. Luma[5] = (tmp >> 12) & 0x3F;
  306. Luma[6] = (tmp >> 6) & 0x3F;
  307. Luma[7] = tmp & 0x3F;
  308. tmp = bytestream_get_be24(&buf);
  309. Luma[8] = (tmp >> 18) & 0x3F;
  310. Luma[9] = (tmp >> 12) & 0x3F;
  311. Luma[10] = (tmp >> 6) & 0x3F;
  312. Luma[11] = tmp & 0x3F;
  313. tmp = bytestream_get_be24(&buf);
  314. Luma[12] = (tmp >> 18) & 0x3F;
  315. Luma[13] = (tmp >> 12) & 0x3F;
  316. Luma[14] = (tmp >> 6) & 0x3F;
  317. Luma[15] = tmp & 0x3F;
  318. ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
  319. } else {
  320. tmp = *buf++;
  321. if(tmp & 0x80) {
  322. angle = (tmp >> 4) & 0x7;
  323. tmp = (tmp << 8) + *buf++;
  324. Y[0] = (tmp >> 6) & 0x3F;
  325. Y[1] = tmp & 0x3F;
  326. Y[2] = (*buf++) & 0x3F;
  327. Y[3] = (*buf++) & 0x3F;
  328. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
  329. } else { // some patterns
  330. int f0, f1;
  331. f0 = *buf++;
  332. f1 = tmp;
  333. Y[0] = (*buf++) & 0x3F;
  334. Y[1] = (*buf++) & 0x3F;
  335. ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
  336. }
  337. }
  338. break;
  339. }
  340. if(code != 3)
  341. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
  342. }
  343. blocks++;
  344. x += 8;
  345. if(x >= s->width) {
  346. x = 0;
  347. y += 8;
  348. }
  349. }
  350. }
  351. *data_size=sizeof(AVFrame);
  352. *(AVFrame*)data= s->frame;
  353. return buf_size;
  354. }
  355. static av_cold int ulti_decode_end(AVCodecContext *avctx)
  356. {
  357. /* UltimotionDecodeContext *s = avctx->priv_data;*/
  358. return 0;
  359. }
  360. AVCodec ulti_decoder = {
  361. "ultimotion",
  362. CODEC_TYPE_VIDEO,
  363. CODEC_ID_ULTI,
  364. sizeof(UltimotionDecodeContext),
  365. ulti_decode_init,
  366. NULL,
  367. ulti_decode_end,
  368. ulti_decode_frame,
  369. CODEC_CAP_DR1,
  370. NULL,
  371. .long_name = NULL_IF_CONFIG_SMALL("IBM UltiMotion"),
  372. };