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.

438 lines
15KB

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