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.

758 lines
23KB

  1. /*
  2. * 4XM codec
  3. * Copyright (c) 2003 Michael Niedermayer
  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. * @file 4xm.c
  21. * 4XM codec.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. //#undef NDEBUG
  27. //#include <assert.h>
  28. #define BLOCK_TYPE_VLC_BITS 5
  29. #define ACDC_VLC_BITS 9
  30. #define CFRAME_BUFFER_COUNT 100
  31. static const uint8_t block_type_tab[4][8][2]={
  32. { //{8,4,2}x{8,4,2}
  33. { 0,1}, { 2,2}, { 6,3}, {14,4}, {30,5}, {31,5}, { 0,0}
  34. },{ //{8,4}x1
  35. { 0,1}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}, { 0,0}
  36. },{ //1x{8,4}
  37. { 0,1}, { 2,2}, { 0,0}, { 6,3}, {14,4}, {15,4}, { 0,0}
  38. },{ //1x2, 2x1
  39. { 0,1}, { 0,0}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}
  40. }
  41. };
  42. static const uint8_t size2index[4][4]={
  43. {-1, 3, 1, 1},
  44. { 3, 0, 0, 0},
  45. { 2, 0, 0, 0},
  46. { 2, 0, 0, 0},
  47. };
  48. static const int8_t mv[256][2]={
  49. { 0, 0},{ 0, -1},{ -1, 0},{ 1, 0},{ 0, 1},{ -1, -1},{ 1, -1},{ -1, 1},
  50. { 1, 1},{ 0, -2},{ -2, 0},{ 2, 0},{ 0, 2},{ -1, -2},{ 1, -2},{ -2, -1},
  51. { 2, -1},{ -2, 1},{ 2, 1},{ -1, 2},{ 1, 2},{ -2, -2},{ 2, -2},{ -2, 2},
  52. { 2, 2},{ 0, -3},{ -3, 0},{ 3, 0},{ 0, 3},{ -1, -3},{ 1, -3},{ -3, -1},
  53. { 3, -1},{ -3, 1},{ 3, 1},{ -1, 3},{ 1, 3},{ -2, -3},{ 2, -3},{ -3, -2},
  54. { 3, -2},{ -3, 2},{ 3, 2},{ -2, 3},{ 2, 3},{ 0, -4},{ -4, 0},{ 4, 0},
  55. { 0, 4},{ -1, -4},{ 1, -4},{ -4, -1},{ 4, -1},{ 4, 1},{ -1, 4},{ 1, 4},
  56. { -3, -3},{ -3, 3},{ 3, 3},{ -2, -4},{ -4, -2},{ 4, -2},{ -4, 2},{ -2, 4},
  57. { 2, 4},{ -3, -4},{ 3, -4},{ 4, -3},{ -5, 0},{ -4, 3},{ -3, 4},{ 3, 4},
  58. { -1, -5},{ -5, -1},{ -5, 1},{ -1, 5},{ -2, -5},{ 2, -5},{ 5, -2},{ 5, 2},
  59. { -4, -4},{ -4, 4},{ -3, -5},{ -5, -3},{ -5, 3},{ 3, 5},{ -6, 0},{ 0, 6},
  60. { -6, -1},{ -6, 1},{ 1, 6},{ 2, -6},{ -6, 2},{ 2, 6},{ -5, -4},{ 5, 4},
  61. { 4, 5},{ -6, -3},{ 6, 3},{ -7, 0},{ -1, -7},{ 5, -5},{ -7, 1},{ -1, 7},
  62. { 4, -6},{ 6, 4},{ -2, -7},{ -7, 2},{ -3, -7},{ 7, -3},{ 3, 7},{ 6, -5},
  63. { 0, -8},{ -1, -8},{ -7, -4},{ -8, 1},{ 4, 7},{ 2, -8},{ -2, 8},{ 6, 6},
  64. { -8, 3},{ 5, -7},{ -5, 7},{ 8, -4},{ 0, -9},{ -9, -1},{ 1, 9},{ 7, -6},
  65. { -7, 6},{ -5, -8},{ -5, 8},{ -9, 3},{ 9, -4},{ 7, -7},{ 8, -6},{ 6, 8},
  66. { 10, 1},{-10, 2},{ 9, -5},{ 10, -3},{ -8, -7},{-10, -4},{ 6, -9},{-11, 0},
  67. { 11, 1},{-11, -2},{ -2, 11},{ 7, -9},{ -7, 9},{ 10, 6},{ -4, 11},{ 8, -9},
  68. { 8, 9},{ 5, 11},{ 7,-10},{ 12, -3},{ 11, 6},{ -9, -9},{ 8, 10},{ 5, 12},
  69. {-11, 7},{ 13, 2},{ 6,-12},{ 10, 9},{-11, 8},{ -7, 12},{ 0, 14},{ 14, -2},
  70. { -9, 11},{ -6, 13},{-14, -4},{ -5,-14},{ 5, 14},{-15, -1},{-14, -6},{ 3,-15},
  71. { 11,-11},{ -7, 14},{ -5, 15},{ 8,-14},{ 15, 6},{ 3, 16},{ 7,-15},{-16, 5},
  72. { 0, 17},{-16, -6},{-10, 14},{-16, 7},{ 12, 13},{-16, 8},{-17, 6},{-18, 3},
  73. { -7, 17},{ 15, 11},{ 16, 10},{ 2,-19},{ 3,-19},{-11,-16},{-18, 8},{-19, -6},
  74. { 2,-20},{-17,-11},{-10,-18},{ 8, 19},{-21, -1},{-20, 7},{ -4, 21},{ 21, 5},
  75. { 15, 16},{ 2,-22},{-10,-20},{-22, 5},{ 20,-11},{ -7,-22},{-12, 20},{ 23, -5},
  76. { 13,-20},{ 24, -2},{-15, 19},{-11, 22},{ 16, 19},{ 23,-10},{-18,-18},{ -9,-24},
  77. { 24,-10},{ -3, 26},{-23, 13},{-18,-20},{ 17, 21},{ -4, 27},{ 27, 6},{ 1,-28},
  78. {-11, 26},{-17,-23},{ 7, 28},{ 11,-27},{ 29, 5},{-23,-19},{-28,-11},{-21, 22},
  79. {-30, 7},{-17, 26},{-27, 16},{ 13, 29},{ 19,-26},{ 10,-31},{-14,-30},{ 20,-27},
  80. {-29, 18},{-16,-31},{-28,-22},{ 21,-30},{-25, 28},{ 26,-29},{ 25,-32},{-32,-32}
  81. };
  82. // this is simply the scaled down elementwise product of the standard jpeg quantizer table and the AAN premul table
  83. static const uint8_t dequant_table[64]={
  84. 16, 15, 13, 19, 24, 31, 28, 17,
  85. 17, 23, 25, 31, 36, 63, 45, 21,
  86. 18, 24, 27, 37, 52, 59, 49, 20,
  87. 16, 28, 34, 40, 60, 80, 51, 20,
  88. 18, 31, 48, 66, 68, 86, 56, 21,
  89. 19, 38, 56, 59, 64, 64, 48, 20,
  90. 27, 48, 55, 55, 56, 51, 35, 15,
  91. 20, 35, 34, 32, 31, 22, 15, 8,
  92. };
  93. static VLC block_type_vlc[4];
  94. typedef struct CFrameBuffer{
  95. int allocated_size;
  96. int size;
  97. int id;
  98. uint8_t *data;
  99. }CFrameBuffer;
  100. typedef struct FourXContext{
  101. AVCodecContext *avctx;
  102. DSPContext dsp;
  103. AVFrame current_picture, last_picture;
  104. GetBitContext pre_gb; ///< ac/dc prefix
  105. GetBitContext gb;
  106. uint8_t *bytestream;
  107. uint16_t *wordstream;
  108. int mv[256];
  109. VLC pre_vlc;
  110. int last_dc;
  111. DCTELEM __align8 block[6][64];
  112. uint8_t *bitstream_buffer;
  113. int bitstream_buffer_size;
  114. CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
  115. } FourXContext;
  116. #define FIX_1_082392200 70936
  117. #define FIX_1_414213562 92682
  118. #define FIX_1_847759065 121095
  119. #define FIX_2_613125930 171254
  120. #define MULTIPLY(var,const) (((var)*(const)) >> 16)
  121. static void idct(DCTELEM block[64]){
  122. int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  123. int tmp10, tmp11, tmp12, tmp13;
  124. int z5, z10, z11, z12, z13;
  125. int i;
  126. int temp[64];
  127. for(i=0; i<8; i++){
  128. tmp10 = block[8*0 + i] + block[8*4 + i];
  129. tmp11 = block[8*0 + i] - block[8*4 + i];
  130. tmp13 = block[8*2 + i] + block[8*6 + i];
  131. tmp12 = MULTIPLY(block[8*2 + i] - block[8*6 + i], FIX_1_414213562) - tmp13;
  132. tmp0 = tmp10 + tmp13;
  133. tmp3 = tmp10 - tmp13;
  134. tmp1 = tmp11 + tmp12;
  135. tmp2 = tmp11 - tmp12;
  136. z13 = block[8*5 + i] + block[8*3 + i];
  137. z10 = block[8*5 + i] - block[8*3 + i];
  138. z11 = block[8*1 + i] + block[8*7 + i];
  139. z12 = block[8*1 + i] - block[8*7 + i];
  140. tmp7 = z11 + z13;
  141. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
  142. z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
  143. tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
  144. tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5;
  145. tmp6 = tmp12 - tmp7;
  146. tmp5 = tmp11 - tmp6;
  147. tmp4 = tmp10 + tmp5;
  148. temp[8*0 + i] = tmp0 + tmp7;
  149. temp[8*7 + i] = tmp0 - tmp7;
  150. temp[8*1 + i] = tmp1 + tmp6;
  151. temp[8*6 + i] = tmp1 - tmp6;
  152. temp[8*2 + i] = tmp2 + tmp5;
  153. temp[8*5 + i] = tmp2 - tmp5;
  154. temp[8*4 + i] = tmp3 + tmp4;
  155. temp[8*3 + i] = tmp3 - tmp4;
  156. }
  157. for(i=0; i<8*8; i+=8){
  158. tmp10 = temp[0 + i] + temp[4 + i];
  159. tmp11 = temp[0 + i] - temp[4 + i];
  160. tmp13 = temp[2 + i] + temp[6 + i];
  161. tmp12 = MULTIPLY(temp[2 + i] - temp[6 + i], FIX_1_414213562) - tmp13;
  162. tmp0 = tmp10 + tmp13;
  163. tmp3 = tmp10 - tmp13;
  164. tmp1 = tmp11 + tmp12;
  165. tmp2 = tmp11 - tmp12;
  166. z13 = temp[5 + i] + temp[3 + i];
  167. z10 = temp[5 + i] - temp[3 + i];
  168. z11 = temp[1 + i] + temp[7 + i];
  169. z12 = temp[1 + i] - temp[7 + i];
  170. tmp7 = z11 + z13;
  171. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
  172. z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
  173. tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
  174. tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5;
  175. tmp6 = tmp12 - tmp7;
  176. tmp5 = tmp11 - tmp6;
  177. tmp4 = tmp10 + tmp5;
  178. block[0 + i] = (tmp0 + tmp7)>>6;
  179. block[7 + i] = (tmp0 - tmp7)>>6;
  180. block[1 + i] = (tmp1 + tmp6)>>6;
  181. block[6 + i] = (tmp1 - tmp6)>>6;
  182. block[2 + i] = (tmp2 + tmp5)>>6;
  183. block[5 + i] = (tmp2 - tmp5)>>6;
  184. block[4 + i] = (tmp3 + tmp4)>>6;
  185. block[3 + i] = (tmp3 - tmp4)>>6;
  186. }
  187. }
  188. static void init_vlcs(FourXContext *f){
  189. static int done = 0;
  190. int i;
  191. if (!done) {
  192. done = 1;
  193. for(i=0; i<4; i++){
  194. init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7,
  195. &block_type_tab[i][0][1], 2, 1,
  196. &block_type_tab[i][0][0], 2, 1);
  197. }
  198. }
  199. }
  200. static void init_mv(FourXContext *f){
  201. int i;
  202. for(i=0; i<256; i++){
  203. f->mv[i] = mv[i][0] + mv[i][1]*f->current_picture.linesize[0]/2;
  204. }
  205. }
  206. static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, int h, int stride, int scale, int dc){
  207. int i;
  208. dc*= 0x10001;
  209. switch(log2w){
  210. case 0:
  211. for(i=0; i<h; i++){
  212. dst[0] = scale*src[0] + dc;
  213. if(scale) src += stride;
  214. dst += stride;
  215. }
  216. break;
  217. case 1:
  218. for(i=0; i<h; i++){
  219. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  220. if(scale) src += stride;
  221. dst += stride;
  222. }
  223. break;
  224. case 2:
  225. for(i=0; i<h; i++){
  226. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  227. ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
  228. if(scale) src += stride;
  229. dst += stride;
  230. }
  231. break;
  232. case 3:
  233. for(i=0; i<h; i++){
  234. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  235. ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
  236. ((uint32_t*)dst)[2] = scale*((uint32_t*)src)[2] + dc;
  237. ((uint32_t*)dst)[3] = scale*((uint32_t*)src)[3] + dc;
  238. if(scale) src += stride;
  239. dst += stride;
  240. }
  241. break;
  242. default: assert(0);
  243. }
  244. }
  245. static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){
  246. const int index= size2index[log2h][log2w];
  247. const int h= 1<<log2h;
  248. int code= get_vlc2(&f->gb, block_type_vlc[index].table, BLOCK_TYPE_VLC_BITS, 1);
  249. assert(code>=0 && code<=6);
  250. if(code == 0){
  251. src += f->mv[ *f->bytestream++ ];
  252. mcdc(dst, src, log2w, h, stride, 1, 0);
  253. }else if(code == 1){
  254. log2h--;
  255. decode_p_block(f, dst , src , log2w, log2h, stride);
  256. decode_p_block(f, dst + (stride<<log2h), src + (stride<<log2h), log2w, log2h, stride);
  257. }else if(code == 2){
  258. log2w--;
  259. decode_p_block(f, dst , src , log2w, log2h, stride);
  260. decode_p_block(f, dst + (1<<log2w), src + (1<<log2w), log2w, log2h, stride);
  261. }else if(code == 4){
  262. src += f->mv[ *f->bytestream++ ];
  263. mcdc(dst, src, log2w, h, stride, 1, le2me_16(*f->wordstream++));
  264. }else if(code == 5){
  265. mcdc(dst, src, log2w, h, stride, 0, le2me_16(*f->wordstream++));
  266. }else if(code == 6){
  267. if(log2w){
  268. dst[0] = le2me_16(*f->wordstream++);
  269. dst[1] = le2me_16(*f->wordstream++);
  270. }else{
  271. dst[0 ] = le2me_16(*f->wordstream++);
  272. dst[stride] = le2me_16(*f->wordstream++);
  273. }
  274. }
  275. }
  276. static int get32(void *p){
  277. return le2me_32(*(uint32_t*)p);
  278. }
  279. static int decode_p_frame(FourXContext *f, uint8_t *buf, int length){
  280. int x, y;
  281. const int width= f->avctx->width;
  282. const int height= f->avctx->height;
  283. uint16_t *src= (uint16_t*)f->last_picture.data[0];
  284. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  285. const int stride= f->current_picture.linesize[0]>>1;
  286. const int bitstream_size= get32(buf+8);
  287. const int bytestream_size= get32(buf+16);
  288. const int wordstream_size= get32(buf+12);
  289. if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length)
  290. av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
  291. bitstream_size+ bytestream_size+ wordstream_size - length);
  292. f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  293. f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + 20), bitstream_size/4);
  294. init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
  295. f->wordstream= (uint16_t*)(buf + 20 + bitstream_size);
  296. f->bytestream= buf + 20 + bitstream_size + wordstream_size;
  297. init_mv(f);
  298. for(y=0; y<height; y+=8){
  299. for(x=0; x<width; x+=8){
  300. decode_p_block(f, dst + x, src + x, 3, 3, stride);
  301. }
  302. src += 8*stride;
  303. dst += 8*stride;
  304. }
  305. if(bitstream_size != (get_bits_count(&f->gb)+31)/32*4)
  306. av_log(f->avctx, AV_LOG_ERROR, " %d %d %d bytes left\n",
  307. bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
  308. bytestream_size - (f->bytestream - (buf + 20 + bitstream_size + wordstream_size)),
  309. wordstream_size - (((uint8_t*)f->wordstream) - (buf + 20 + bitstream_size))
  310. );
  311. return 0;
  312. }
  313. /**
  314. * decode block and dequantize.
  315. * Note this is allmost identical to mjpeg
  316. */
  317. static int decode_i_block(FourXContext *f, DCTELEM *block){
  318. int code, i, j, level, val;
  319. /* DC coef */
  320. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  321. if (val>>4){
  322. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  323. }
  324. if(val)
  325. val = get_xbits(&f->gb, val);
  326. val = val * dequant_table[0] + f->last_dc;
  327. f->last_dc =
  328. block[0] = val;
  329. /* AC coefs */
  330. i = 1;
  331. for(;;) {
  332. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  333. /* EOB */
  334. if (code == 0)
  335. break;
  336. if (code == 0xf0) {
  337. i += 16;
  338. } else {
  339. level = get_xbits(&f->gb, code & 0xf);
  340. i += code >> 4;
  341. if (i >= 64) {
  342. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  343. return 0;
  344. }
  345. j= ff_zigzag_direct[i];
  346. block[j] = level * dequant_table[j];
  347. i++;
  348. if (i >= 64)
  349. break;
  350. }
  351. }
  352. return 0;
  353. }
  354. static inline void idct_put(FourXContext *f, int x, int y){
  355. DCTELEM (*block)[64]= f->block;
  356. int stride= f->current_picture.linesize[0]>>1;
  357. int i;
  358. uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
  359. for(i=0; i<4; i++){
  360. block[i][0] += 0x80*8*8;
  361. idct(block[i]);
  362. }
  363. if(!(f->avctx->flags&CODEC_FLAG_GRAY)){
  364. for(i=4; i<6; i++) idct(block[i]);
  365. }
  366. /* Note transform is:
  367. y= ( 1b + 4g + 2r)/14
  368. cb=( 3b - 2g - 1r)/14
  369. cr=(-1b - 4g + 5r)/14
  370. */
  371. for(y=0; y<8; y++){
  372. for(x=0; x<8; x++){
  373. DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3); //FIXME optimize
  374. int cb= block[4][x + 8*y];
  375. int cr= block[5][x + 8*y];
  376. int cg= (cb + cr)>>1;
  377. int y;
  378. cb+=cb;
  379. y = temp[0];
  380. dst[0 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  381. y = temp[1];
  382. dst[1 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  383. y = temp[8];
  384. dst[ stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  385. y = temp[9];
  386. dst[1+stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  387. dst += 2;
  388. }
  389. dst += 2*stride - 2*8;
  390. }
  391. }
  392. static int decode_i_mb(FourXContext *f){
  393. int i;
  394. f->dsp.clear_blocks(f->block[0]);
  395. for(i=0; i<6; i++){
  396. if(decode_i_block(f, f->block[i]) < 0)
  397. return -1;
  398. }
  399. return 0;
  400. }
  401. static uint8_t *read_huffman_tables(FourXContext *f, uint8_t * const buf){
  402. int frequency[512];
  403. uint8_t flag[512];
  404. int up[512];
  405. uint8_t len_tab[257];
  406. int bits_tab[257];
  407. int start, end;
  408. uint8_t *ptr= buf;
  409. int j;
  410. memset(frequency, 0, sizeof(frequency));
  411. memset(up, -1, sizeof(up));
  412. start= *ptr++;
  413. end= *ptr++;
  414. for(;;){
  415. int i;
  416. for(i=start; i<=end; i++){
  417. frequency[i]= *ptr++;
  418. // printf("%d %d %d\n", start, end, frequency[i]);
  419. }
  420. start= *ptr++;
  421. if(start==0) break;
  422. end= *ptr++;
  423. }
  424. frequency[256]=1;
  425. while((ptr - buf)&3) ptr++; // 4byte align
  426. // for(j=0; j<16; j++)
  427. // printf("%2X", ptr[j]);
  428. for(j=257; j<512; j++){
  429. int min_freq[2]= {256*256, 256*256};
  430. int smallest[2]= {0, 0};
  431. int i;
  432. for(i=0; i<j; i++){
  433. if(frequency[i] == 0) continue;
  434. if(frequency[i] < min_freq[1]){
  435. if(frequency[i] < min_freq[0]){
  436. min_freq[1]= min_freq[0]; smallest[1]= smallest[0];
  437. min_freq[0]= frequency[i];smallest[0]= i;
  438. }else{
  439. min_freq[1]= frequency[i];smallest[1]= i;
  440. }
  441. }
  442. }
  443. if(min_freq[1] == 256*256) break;
  444. frequency[j]= min_freq[0] + min_freq[1];
  445. flag[ smallest[0] ]= 0;
  446. flag[ smallest[1] ]= 1;
  447. up[ smallest[0] ]=
  448. up[ smallest[1] ]= j;
  449. frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
  450. }
  451. for(j=0; j<257; j++){
  452. int node;
  453. int len=0;
  454. int bits=0;
  455. for(node= j; up[node] != -1; node= up[node]){
  456. bits += flag[node]<<len;
  457. len++;
  458. if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
  459. }
  460. bits_tab[j]= bits;
  461. len_tab[j]= len;
  462. }
  463. init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
  464. len_tab , 1, 1,
  465. bits_tab, 4, 4);
  466. return ptr;
  467. }
  468. static int decode_i_frame(FourXContext *f, uint8_t *buf, int length){
  469. int x, y;
  470. const int width= f->avctx->width;
  471. const int height= f->avctx->height;
  472. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  473. const int stride= f->current_picture.linesize[0]>>1;
  474. const int bitstream_size= get32(buf);
  475. const int token_count __attribute__((unused)) = get32(buf + bitstream_size + 8);
  476. int prestream_size= 4*get32(buf + bitstream_size + 4);
  477. uint8_t *prestream= buf + bitstream_size + 12;
  478. if(prestream_size + bitstream_size + 12 != length)
  479. av_log(f->avctx, AV_LOG_ERROR, "size missmatch %d %d %d\n", prestream_size, bitstream_size, length);
  480. prestream= read_huffman_tables(f, prestream);
  481. init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
  482. prestream_size= length + buf - prestream;
  483. f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  484. f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)prestream, prestream_size/4);
  485. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
  486. f->last_dc= 0*128*8*8;
  487. for(y=0; y<height; y+=16){
  488. for(x=0; x<width; x+=16){
  489. if(decode_i_mb(f) < 0)
  490. return -1;
  491. idct_put(f, x, y);
  492. }
  493. dst += 16*stride;
  494. }
  495. if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  496. av_log(f->avctx, AV_LOG_ERROR, "end missmatch\n");
  497. return 0;
  498. }
  499. static int decode_frame(AVCodecContext *avctx,
  500. void *data, int *data_size,
  501. uint8_t *buf, int buf_size)
  502. {
  503. FourXContext * const f = avctx->priv_data;
  504. AVFrame *picture = data;
  505. AVFrame *p, temp;
  506. int i, frame_4cc, frame_size;
  507. *data_size = 0;
  508. /* special case for last picture */
  509. if (buf_size == 0) {
  510. return 0;
  511. }
  512. frame_4cc= get32(buf);
  513. if(buf_size != get32(buf+4)+8){
  514. av_log(f->avctx, AV_LOG_ERROR, "size missmatch %d %d\n", buf_size, get32(buf+4));
  515. }
  516. if(frame_4cc == ff_get_fourcc("cfrm")){
  517. int free_index=-1;
  518. const int data_size= buf_size - 20;
  519. const int id= get32(buf+12);
  520. const int whole_size= get32(buf+16);
  521. CFrameBuffer *cfrm;
  522. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  523. if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  524. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
  525. }
  526. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  527. if(f->cfrm[i].id == id) break;
  528. if(f->cfrm[i].size == 0 ) free_index= i;
  529. }
  530. if(i>=CFRAME_BUFFER_COUNT){
  531. i= free_index;
  532. f->cfrm[i].id= id;
  533. }
  534. cfrm= &f->cfrm[i];
  535. cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
  536. memcpy(cfrm->data + cfrm->size, buf+20, data_size);
  537. cfrm->size += data_size;
  538. if(cfrm->size >= whole_size){
  539. buf= cfrm->data;
  540. frame_size= cfrm->size;
  541. if(id != avctx->frame_number){
  542. av_log(f->avctx, AV_LOG_ERROR, "cframe id missmatch %d %d\n", id, avctx->frame_number);
  543. }
  544. cfrm->size= cfrm->id= 0;
  545. frame_4cc= ff_get_fourcc("pfrm");
  546. }else
  547. return buf_size;
  548. }else{
  549. buf= buf + 12;
  550. frame_size= buf_size - 12;
  551. }
  552. temp= f->current_picture;
  553. f->current_picture= f->last_picture;
  554. f->last_picture= temp;
  555. p= &f->current_picture;
  556. avctx->coded_frame= p;
  557. avctx->flags |= CODEC_FLAG_EMU_EDGE; // alternatively we would have to use our own buffer management
  558. if(p->data[0])
  559. avctx->release_buffer(avctx, p);
  560. p->reference= 1;
  561. if(avctx->get_buffer(avctx, p) < 0){
  562. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  563. return -1;
  564. }
  565. if(frame_4cc == ff_get_fourcc("ifrm")){
  566. p->pict_type= I_TYPE;
  567. if(decode_i_frame(f, buf, frame_size) < 0)
  568. return -1;
  569. }else if(frame_4cc == ff_get_fourcc("pfrm")){
  570. p->pict_type= P_TYPE;
  571. if(decode_p_frame(f, buf, frame_size) < 0)
  572. return -1;
  573. }else if(frame_4cc == ff_get_fourcc("snd_")){
  574. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
  575. }else{
  576. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
  577. }
  578. #if 0
  579. for(i=0; i<20; i++){
  580. printf("%2X %c ", buf[i], clip(buf[i],16,126));
  581. }
  582. #endif
  583. p->key_frame= p->pict_type == I_TYPE;
  584. *picture= *p;
  585. *data_size = sizeof(AVPicture);
  586. emms_c();
  587. return buf_size;
  588. }
  589. static void common_init(AVCodecContext *avctx){
  590. FourXContext * const f = avctx->priv_data;
  591. dsputil_init(&f->dsp, avctx);
  592. f->avctx= avctx;
  593. }
  594. static int decode_init(AVCodecContext *avctx){
  595. FourXContext * const f = avctx->priv_data;
  596. common_init(avctx);
  597. init_vlcs(f);
  598. avctx->pix_fmt= PIX_FMT_RGB565;
  599. return 0;
  600. }
  601. static int decode_end(AVCodecContext *avctx){
  602. FourXContext * const f = avctx->priv_data;
  603. int i;
  604. av_freep(&f->bitstream_buffer);
  605. f->bitstream_buffer_size=0;
  606. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  607. av_freep(&f->cfrm[i].data);
  608. f->cfrm[i].allocated_size= 0;
  609. }
  610. free_vlc(&f->pre_vlc);
  611. avcodec_default_free_buffers(avctx);
  612. return 0;
  613. }
  614. AVCodec fourxm_decoder = {
  615. "4xm",
  616. CODEC_TYPE_VIDEO,
  617. CODEC_ID_4XM,
  618. sizeof(FourXContext),
  619. decode_init,
  620. NULL,
  621. decode_end,
  622. decode_frame,
  623. /*CODEC_CAP_DR1,*/
  624. };