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.

439 lines
15KB

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