ulti.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. const uint8_t *buf, int buf_size)
  180. {
  181. UltimotionDecodeContext *s=avctx->priv_data;
  182. int modifier = 0;
  183. int uniq = 0;
  184. int mode = 0;
  185. int blocks = 0;
  186. int done = 0;
  187. int x = 0, y = 0;
  188. int i;
  189. int skip;
  190. int tmp;
  191. if(s->frame.data[0])
  192. avctx->release_buffer(avctx, &s->frame);
  193. s->frame.reference = 1;
  194. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  195. if(avctx->get_buffer(avctx, &s->frame) < 0) {
  196. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  197. return -1;
  198. }
  199. while(!done) {
  200. int idx;
  201. if(blocks >= s->blocks || y >= s->height)
  202. break;//all blocks decoded
  203. idx = *buf++;
  204. if((idx & 0xF8) == 0x70) {
  205. switch(idx) {
  206. case 0x70: //change modifier
  207. modifier = *buf++;
  208. if(modifier>1)
  209. av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
  210. break;
  211. case 0x71: // set uniq flag
  212. uniq = 1;
  213. break;
  214. case 0x72: //toggle mode
  215. mode = !mode;
  216. break;
  217. case 0x73: //end-of-frame
  218. done = 1;
  219. break;
  220. case 0x74: //skip some blocks
  221. skip = *buf++;
  222. if ((blocks + skip) >= s->blocks)
  223. break;
  224. blocks += skip;
  225. x += skip * 8;
  226. while(x >= s->width) {
  227. x -= s->width;
  228. y += 8;
  229. }
  230. break;
  231. default:
  232. av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
  233. }
  234. } else { //handle one block
  235. int code;
  236. int cf;
  237. int angle = 0;
  238. uint8_t Y[4]; // luma samples of block
  239. int tx = 0, ty = 0; //coords of subblock
  240. int chroma = 0;
  241. if (mode || uniq) {
  242. uniq = 0;
  243. cf = 1;
  244. chroma = 0;
  245. } else {
  246. cf = 0;
  247. if (idx)
  248. chroma = *buf++;
  249. }
  250. for (i = 0; i < 4; i++) { // for every subblock
  251. code = (idx >> (6 - i*2)) & 3; //extract 2 bits
  252. if(!code) //skip subblock
  253. continue;
  254. if(cf)
  255. chroma = *buf++;
  256. tx = x + block_coords[i * 2];
  257. ty = y + block_coords[(i * 2) + 1];
  258. switch(code) {
  259. case 1:
  260. tmp = *buf++;
  261. angle = angle_by_index[(tmp >> 6) & 0x3];
  262. Y[0] = tmp & 0x3F;
  263. Y[1] = Y[0];
  264. if (angle) {
  265. Y[2] = Y[0]+1;
  266. if (Y[2] > 0x3F)
  267. Y[2] = 0x3F;
  268. Y[3] = Y[2];
  269. } else {
  270. Y[2] = Y[0];
  271. Y[3] = Y[0];
  272. }
  273. break;
  274. case 2:
  275. if (modifier) { // unpack four luma samples
  276. tmp = bytestream_get_be24(&buf);
  277. Y[0] = (tmp >> 18) & 0x3F;
  278. Y[1] = (tmp >> 12) & 0x3F;
  279. Y[2] = (tmp >> 6) & 0x3F;
  280. Y[3] = tmp & 0x3F;
  281. angle = 16;
  282. } else { // retrieve luma samples from codebook
  283. tmp = bytestream_get_be16(&buf);
  284. angle = (tmp >> 12) & 0xF;
  285. tmp &= 0xFFF;
  286. tmp <<= 2;
  287. Y[0] = s->ulti_codebook[tmp];
  288. Y[1] = s->ulti_codebook[tmp + 1];
  289. Y[2] = s->ulti_codebook[tmp + 2];
  290. Y[3] = s->ulti_codebook[tmp + 3];
  291. }
  292. break;
  293. case 3:
  294. if (modifier) { // all 16 luma samples
  295. uint8_t Luma[16];
  296. tmp = bytestream_get_be24(&buf);
  297. Luma[0] = (tmp >> 18) & 0x3F;
  298. Luma[1] = (tmp >> 12) & 0x3F;
  299. Luma[2] = (tmp >> 6) & 0x3F;
  300. Luma[3] = tmp & 0x3F;
  301. tmp = bytestream_get_be24(&buf);
  302. Luma[4] = (tmp >> 18) & 0x3F;
  303. Luma[5] = (tmp >> 12) & 0x3F;
  304. Luma[6] = (tmp >> 6) & 0x3F;
  305. Luma[7] = tmp & 0x3F;
  306. tmp = bytestream_get_be24(&buf);
  307. Luma[8] = (tmp >> 18) & 0x3F;
  308. Luma[9] = (tmp >> 12) & 0x3F;
  309. Luma[10] = (tmp >> 6) & 0x3F;
  310. Luma[11] = tmp & 0x3F;
  311. tmp = bytestream_get_be24(&buf);
  312. Luma[12] = (tmp >> 18) & 0x3F;
  313. Luma[13] = (tmp >> 12) & 0x3F;
  314. Luma[14] = (tmp >> 6) & 0x3F;
  315. Luma[15] = tmp & 0x3F;
  316. ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
  317. } else {
  318. tmp = *buf++;
  319. if(tmp & 0x80) {
  320. angle = (tmp >> 4) & 0x7;
  321. tmp = (tmp << 8) + *buf++;
  322. Y[0] = (tmp >> 6) & 0x3F;
  323. Y[1] = tmp & 0x3F;
  324. Y[2] = (*buf++) & 0x3F;
  325. Y[3] = (*buf++) & 0x3F;
  326. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
  327. } else { // some patterns
  328. int f0, f1;
  329. f0 = *buf++;
  330. f1 = tmp;
  331. Y[0] = (*buf++) & 0x3F;
  332. Y[1] = (*buf++) & 0x3F;
  333. ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
  334. }
  335. }
  336. break;
  337. }
  338. if(code != 3)
  339. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
  340. }
  341. blocks++;
  342. x += 8;
  343. if(x >= s->width) {
  344. x = 0;
  345. y += 8;
  346. }
  347. }
  348. }
  349. *data_size=sizeof(AVFrame);
  350. *(AVFrame*)data= s->frame;
  351. return buf_size;
  352. }
  353. static av_cold int ulti_decode_end(AVCodecContext *avctx)
  354. {
  355. /* UltimotionDecodeContext *s = avctx->priv_data;*/
  356. return 0;
  357. }
  358. AVCodec ulti_decoder = {
  359. "ultimotion",
  360. CODEC_TYPE_VIDEO,
  361. CODEC_ID_ULTI,
  362. sizeof(UltimotionDecodeContext),
  363. ulti_decode_init,
  364. NULL,
  365. ulti_decode_end,
  366. ulti_decode_frame,
  367. CODEC_CAP_DR1,
  368. NULL,
  369. .long_name = NULL_IF_CONFIG_SMALL("IBM UltiMotion"),
  370. };