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.

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