ulti.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:12k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*
  2.  * IBM Ultimotion Video Decoder
  3.  * Copyright (C) 2004 Konstantin Shishkov
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  *
  19.  */
  20. /**
  21.  * @file ulti.c 
  22.  * IBM Ultimotion Video Decoder.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "common.h"
  30. #include "avcodec.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. } UltimotionDecodeContext;
  38. static 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->has_b_frames = 0;
  47.     avctx->coded_frame = (AVFrame*) &s->frame;
  48.     s->ulti_codebook = ulti_codebook;
  49.     return 0;
  50. }
  51. static int block_coords[8] = // 4x4 block coords in 8x8 superblock
  52.     { 0, 0, 0, 4, 4, 4, 4, 0};
  53. static int angle_by_index[4] = { 0, 2, 6, 12};
  54. /* Lookup tables for luma and chroma - used by ulti_convert_yuv() */
  55. static uint8_t ulti_lumas[64] =
  56.     { 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28,
  57.       0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44,
  58.       0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60,
  59.       0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C,
  60.       0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98,
  61.       0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3,
  62.       0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF,
  63.       0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB};
  64.       
  65. static uint8_t ulti_chromas[16] =
  66.     { 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D,
  67.       0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0};
  68.       
  69. /* convert Ultimotion YUV block (sixteen 6-bit Y samples and
  70.  two 4-bit chroma samples) into standard YUV and put it into frame */
  71. static void ulti_convert_yuv(AVFrame *frame, int x, int y,
  72.      uint8_t *luma,int chroma)
  73. {
  74.     uint8_t *y_plane, *cr_plane, *cb_plane;
  75.     int i;
  76.     
  77.     y_plane = frame->data[0] + x + y * frame->linesize[0];
  78.     cr_plane = frame->data[1] + (x / 4) + (y / 4) * frame->linesize[1];
  79.     cb_plane = frame->data[2] + (x / 4) + (y / 4) * frame->linesize[2];
  80.     
  81.     cr_plane[0] = ulti_chromas[chroma >> 4];
  82.     
  83.     cb_plane[0] = ulti_chromas[chroma & 0xF];
  84.     
  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.     
  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.     
  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.     
  185.     ulti_convert_yuv(frame, x, y, Luma, chroma);
  186. }
  187. static int ulti_decode_frame(AVCodecContext *avctx, 
  188.                              void *data, int *data_size,
  189.                              uint8_t *buf, int buf_size)
  190. {
  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.     if(s->frame.data[0])
  202.         avctx->release_buffer(avctx, &s->frame);
  203.     s->frame.reference = 1;
  204.     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  205.     if(avctx->get_buffer(avctx, &s->frame) < 0) {
  206.         av_log(avctx, AV_LOG_ERROR, "get_buffer() failedn");
  207.         return -1;
  208.     }
  209.     
  210.     while(!done) {
  211. int idx;
  212. if(blocks >= s->blocks || y >= s->height)
  213.     break;//all blocks decoded
  214. idx = *buf++;
  215. if((idx & 0xF8) == 0x70) {
  216.     switch(idx) {
  217.     case 0x70: //change modifier
  218. modifier = *buf++;
  219. if(modifier>1)
  220.     av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %in", 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 = *buf++;
  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%02Xn", 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 = *buf++;
  260.     }
  261.     for (i = 0; i < 4; i++) { // for every subblock
  262. code = (idx >> (6 - i*2)) & 3; //extract 2 bits
  263. if(!code) //skip subblock
  264.     continue;
  265. if(cf)
  266.     chroma = *buf++;
  267. tx = x + block_coords[i * 2];
  268. ty = y + block_coords[(i * 2) + 1];
  269. switch(code) {
  270. case 1: 
  271.     tmp = *buf++;
  272.     
  273.     angle = angle_by_index[(tmp >> 6) & 0x3];
  274.     
  275.     Y[0] = tmp & 0x3F;
  276.     Y[1] = Y[0];
  277.     
  278.     if (angle) {
  279. Y[2] = Y[0]+1;
  280. if (Y[2] > 0x3F)
  281.     Y[2] = 0x3F;
  282. Y[3] = Y[2];
  283.     } else {
  284. Y[2] = Y[0];
  285. Y[3] = Y[0];
  286.     }
  287.     break;
  288.     
  289. case 2:
  290.     if (modifier) { // unpack four luma samples
  291. tmp = (*buf++) << 16;
  292. tmp += (*buf++) << 8;
  293. tmp += *buf++;
  294. Y[0] = (tmp >> 18) & 0x3F;
  295. Y[1] = (tmp >> 12) & 0x3F;
  296. Y[2] = (tmp >> 6) & 0x3F;
  297. Y[3] = tmp & 0x3F;
  298. angle = 16;
  299.     } else { // retrieve luma samples from codebook
  300. tmp = (*buf++) << 8;
  301. tmp += (*buf++);
  302. angle = (tmp >> 12) & 0xF;
  303. tmp &= 0xFFF;
  304. tmp <<= 2;
  305. Y[0] = s->ulti_codebook[tmp];
  306. Y[1] = s->ulti_codebook[tmp + 1];
  307. Y[2] = s->ulti_codebook[tmp + 2];
  308. Y[3] = s->ulti_codebook[tmp + 3];
  309.     }
  310.     break;
  311.     
  312. case 3:
  313.     if (modifier) { // all 16 luma samples
  314. uint8_t Luma[16];
  315. tmp = (*buf++) << 16;
  316. tmp += (*buf++) << 8;
  317. tmp += *buf++;
  318. Luma[0] = (tmp >> 18) & 0x3F;
  319. Luma[1] = (tmp >> 12) & 0x3F;
  320. Luma[2] = (tmp >> 6) & 0x3F;
  321. Luma[3] = tmp & 0x3F;
  322. tmp = (*buf++) << 16;
  323. tmp += (*buf++) << 8;
  324. tmp += *buf++;
  325. Luma[4] = (tmp >> 18) & 0x3F;
  326. Luma[5] = (tmp >> 12) & 0x3F;
  327. Luma[6] = (tmp >> 6) & 0x3F;
  328. Luma[7] = tmp & 0x3F;
  329. tmp = (*buf++) << 16;
  330. tmp += (*buf++) << 8;
  331. tmp += *buf++;
  332. Luma[8] = (tmp >> 18) & 0x3F;
  333. Luma[9] = (tmp >> 12) & 0x3F;
  334. Luma[10] = (tmp >> 6) & 0x3F;
  335. Luma[11] = tmp & 0x3F;
  336. tmp = (*buf++) << 16;
  337. tmp += (*buf++) << 8;
  338. tmp += *buf++;
  339. Luma[12] = (tmp >> 18) & 0x3F;
  340. Luma[13] = (tmp >> 12) & 0x3F;
  341. Luma[14] = (tmp >> 6) & 0x3F;
  342. Luma[15] = tmp & 0x3F;
  343. ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
  344.     } else {
  345. tmp = *buf++;
  346. if(tmp & 0x80) {
  347.     angle = (tmp >> 4) & 0x7;
  348.     tmp = (tmp << 8) + *buf++;
  349.     Y[0] = (tmp >> 6) & 0x3F;
  350.     Y[1] = tmp & 0x3F;
  351.     Y[2] = (*buf++) & 0x3F;
  352.     Y[3] = (*buf++) & 0x3F;
  353.     ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
  354. } else { // some patterns
  355.     int f0, f1;
  356.     f0 = *buf++;
  357.     f1 = tmp;
  358.     Y[0] = (*buf++) & 0x3F;
  359.     Y[1] = (*buf++) & 0x3F;
  360.     ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
  361. }
  362.     }
  363.     break;
  364. }
  365. if(code != 3)
  366.     ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
  367.     }
  368.     blocks++;
  369.          x += 8;
  370.     if(x >= s->width) {
  371. x = 0;
  372. y += 8;
  373.     }
  374. }
  375.     }
  376.     
  377.     *data_size=sizeof(AVFrame);
  378.     *(AVFrame*)data= s->frame;
  379.     return buf_size;
  380. }
  381. static int ulti_decode_end(AVCodecContext *avctx)
  382. {
  383. /*    UltimotionDecodeContext *s = avctx->priv_data;*/
  384.     return 0;
  385. }
  386. AVCodec ulti_decoder = {
  387.     "ultimotion",
  388.     CODEC_TYPE_VIDEO,
  389.     CODEC_ID_ULTI,
  390.     sizeof(UltimotionDecodeContext),
  391.     ulti_decode_init,
  392.     NULL,
  393.     ulti_decode_end,
  394.     ulti_decode_frame,
  395.     CODEC_CAP_DR1,
  396.     NULL
  397. };