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.

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