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.

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