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.

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