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.

417 lines
14KB

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