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.

418 lines
14KB

  1. /*
  2. * IBM Ultimotion Video Decoder
  3. * Copyright (C) 2004 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. s->frame.reference = 1;
  200. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  201. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  202. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  203. return -1;
  204. }
  205. while(!done) {
  206. int idx;
  207. if(blocks >= s->blocks || y >= s->height)
  208. break;//all blocks decoded
  209. idx = *buf++;
  210. if((idx & 0xF8) == 0x70) {
  211. switch(idx) {
  212. case 0x70: //change modifier
  213. modifier = *buf++;
  214. if(modifier>1)
  215. av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
  216. break;
  217. case 0x71: // set uniq flag
  218. uniq = 1;
  219. break;
  220. case 0x72: //toggle mode
  221. mode = !mode;
  222. break;
  223. case 0x73: //end-of-frame
  224. done = 1;
  225. break;
  226. case 0x74: //skip some blocks
  227. skip = *buf++;
  228. if ((blocks + skip) >= s->blocks)
  229. break;
  230. blocks += skip;
  231. x += skip * 8;
  232. while(x >= s->width) {
  233. x -= s->width;
  234. y += 8;
  235. }
  236. break;
  237. default:
  238. av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
  239. }
  240. } else { //handle one block
  241. int code;
  242. int cf;
  243. int angle = 0;
  244. uint8_t Y[4]; // luma samples of block
  245. int tx = 0, ty = 0; //coords of subblock
  246. int chroma = 0;
  247. if (mode || uniq) {
  248. uniq = 0;
  249. cf = 1;
  250. chroma = 0;
  251. } else {
  252. cf = 0;
  253. if (idx)
  254. chroma = *buf++;
  255. }
  256. for (i = 0; i < 4; i++) { // for every subblock
  257. code = (idx >> (6 - i*2)) & 3; //extract 2 bits
  258. if(!code) //skip subblock
  259. continue;
  260. if(cf)
  261. chroma = *buf++;
  262. tx = x + block_coords[i * 2];
  263. ty = y + block_coords[(i * 2) + 1];
  264. switch(code) {
  265. case 1:
  266. tmp = *buf++;
  267. angle = angle_by_index[(tmp >> 6) & 0x3];
  268. Y[0] = tmp & 0x3F;
  269. Y[1] = Y[0];
  270. if (angle) {
  271. Y[2] = Y[0]+1;
  272. if (Y[2] > 0x3F)
  273. Y[2] = 0x3F;
  274. Y[3] = Y[2];
  275. } else {
  276. Y[2] = Y[0];
  277. Y[3] = Y[0];
  278. }
  279. break;
  280. case 2:
  281. if (modifier) { // unpack four luma samples
  282. tmp = bytestream_get_be24(&buf);
  283. Y[0] = (tmp >> 18) & 0x3F;
  284. Y[1] = (tmp >> 12) & 0x3F;
  285. Y[2] = (tmp >> 6) & 0x3F;
  286. Y[3] = tmp & 0x3F;
  287. angle = 16;
  288. } else { // retrieve luma samples from codebook
  289. tmp = bytestream_get_be16(&buf);
  290. angle = (tmp >> 12) & 0xF;
  291. tmp &= 0xFFF;
  292. tmp <<= 2;
  293. Y[0] = s->ulti_codebook[tmp];
  294. Y[1] = s->ulti_codebook[tmp + 1];
  295. Y[2] = s->ulti_codebook[tmp + 2];
  296. Y[3] = s->ulti_codebook[tmp + 3];
  297. }
  298. break;
  299. case 3:
  300. if (modifier) { // all 16 luma samples
  301. uint8_t Luma[16];
  302. tmp = bytestream_get_be24(&buf);
  303. Luma[0] = (tmp >> 18) & 0x3F;
  304. Luma[1] = (tmp >> 12) & 0x3F;
  305. Luma[2] = (tmp >> 6) & 0x3F;
  306. Luma[3] = tmp & 0x3F;
  307. tmp = bytestream_get_be24(&buf);
  308. Luma[4] = (tmp >> 18) & 0x3F;
  309. Luma[5] = (tmp >> 12) & 0x3F;
  310. Luma[6] = (tmp >> 6) & 0x3F;
  311. Luma[7] = tmp & 0x3F;
  312. tmp = bytestream_get_be24(&buf);
  313. Luma[8] = (tmp >> 18) & 0x3F;
  314. Luma[9] = (tmp >> 12) & 0x3F;
  315. Luma[10] = (tmp >> 6) & 0x3F;
  316. Luma[11] = tmp & 0x3F;
  317. tmp = bytestream_get_be24(&buf);
  318. Luma[12] = (tmp >> 18) & 0x3F;
  319. Luma[13] = (tmp >> 12) & 0x3F;
  320. Luma[14] = (tmp >> 6) & 0x3F;
  321. Luma[15] = tmp & 0x3F;
  322. ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
  323. } else {
  324. tmp = *buf++;
  325. if(tmp & 0x80) {
  326. angle = (tmp >> 4) & 0x7;
  327. tmp = (tmp << 8) + *buf++;
  328. Y[0] = (tmp >> 6) & 0x3F;
  329. Y[1] = tmp & 0x3F;
  330. Y[2] = (*buf++) & 0x3F;
  331. Y[3] = (*buf++) & 0x3F;
  332. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
  333. } else { // some patterns
  334. int f0, f1;
  335. f0 = *buf++;
  336. f1 = tmp;
  337. Y[0] = (*buf++) & 0x3F;
  338. Y[1] = (*buf++) & 0x3F;
  339. ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
  340. }
  341. }
  342. break;
  343. }
  344. if(code != 3)
  345. ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
  346. }
  347. blocks++;
  348. x += 8;
  349. if(x >= s->width) {
  350. x = 0;
  351. y += 8;
  352. }
  353. }
  354. }
  355. *data_size=sizeof(AVFrame);
  356. *(AVFrame*)data= s->frame;
  357. return buf_size;
  358. }
  359. AVCodec ff_ulti_decoder = {
  360. "ultimotion",
  361. AVMEDIA_TYPE_VIDEO,
  362. CODEC_ID_ULTI,
  363. sizeof(UltimotionDecodeContext),
  364. ulti_decode_init,
  365. NULL,
  366. ulti_decode_end,
  367. ulti_decode_frame,
  368. CODEC_CAP_DR1,
  369. NULL,
  370. .long_name = NULL_IF_CONFIG_SMALL("IBM UltiMotion"),
  371. };