4xm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. int i;
  190. for(i=0; i<4; i++){
  191. init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7,
  192. &block_type_tab[i][0][1], 2, 1,
  193. &block_type_tab[i][0][0], 2, 1, 1);
  194. }
  195. }
  196. static void init_mv(FourXContext *f){
  197. int i;
  198. for(i=0; i<256; i++){
  199. f->mv[i] = mv[i][0] + mv[i][1]*f->current_picture.linesize[0]/2;
  200. }
  201. }
  202. static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, int h, int stride, int scale, int dc){
  203. int i;
  204. dc*= 0x10001;
  205. switch(log2w){
  206. case 0:
  207. for(i=0; i<h; i++){
  208. dst[0] = scale*src[0] + dc;
  209. if(scale) src += stride;
  210. dst += stride;
  211. }
  212. break;
  213. case 1:
  214. for(i=0; i<h; i++){
  215. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  216. if(scale) src += stride;
  217. dst += stride;
  218. }
  219. break;
  220. case 2:
  221. for(i=0; i<h; i++){
  222. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  223. ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
  224. if(scale) src += stride;
  225. dst += stride;
  226. }
  227. break;
  228. case 3:
  229. for(i=0; i<h; i++){
  230. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  231. ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
  232. ((uint32_t*)dst)[2] = scale*((uint32_t*)src)[2] + dc;
  233. ((uint32_t*)dst)[3] = scale*((uint32_t*)src)[3] + dc;
  234. if(scale) src += stride;
  235. dst += stride;
  236. }
  237. break;
  238. default: assert(0);
  239. }
  240. }
  241. static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){
  242. const int index= size2index[log2h][log2w];
  243. const int h= 1<<log2h;
  244. int code= get_vlc2(&f->gb, block_type_vlc[index].table, BLOCK_TYPE_VLC_BITS, 1);
  245. assert(code>=0 && code<=6);
  246. if(code == 0){
  247. src += f->mv[ *f->bytestream++ ];
  248. mcdc(dst, src, log2w, h, stride, 1, 0);
  249. }else if(code == 1){
  250. log2h--;
  251. decode_p_block(f, dst , src , log2w, log2h, stride);
  252. decode_p_block(f, dst + (stride<<log2h), src + (stride<<log2h), log2w, log2h, stride);
  253. }else if(code == 2){
  254. log2w--;
  255. decode_p_block(f, dst , src , log2w, log2h, stride);
  256. decode_p_block(f, dst + (1<<log2w), src + (1<<log2w), log2w, log2h, stride);
  257. }else if(code == 4){
  258. src += f->mv[ *f->bytestream++ ];
  259. mcdc(dst, src, log2w, h, stride, 1, le2me_16(*f->wordstream++));
  260. }else if(code == 5){
  261. mcdc(dst, src, log2w, h, stride, 0, le2me_16(*f->wordstream++));
  262. }else if(code == 6){
  263. if(log2w){
  264. dst[0] = le2me_16(*f->wordstream++);
  265. dst[1] = le2me_16(*f->wordstream++);
  266. }else{
  267. dst[0 ] = le2me_16(*f->wordstream++);
  268. dst[stride] = le2me_16(*f->wordstream++);
  269. }
  270. }
  271. }
  272. static int get32(void *p){
  273. return le2me_32(*(uint32_t*)p);
  274. }
  275. static int decode_p_frame(FourXContext *f, uint8_t *buf, int length){
  276. int x, y;
  277. const int width= f->avctx->width;
  278. const int height= f->avctx->height;
  279. uint16_t *src= (uint16_t*)f->last_picture.data[0];
  280. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  281. const int stride= f->current_picture.linesize[0]>>1;
  282. const unsigned int bitstream_size= get32(buf+8);
  283. const unsigned int bytestream_size= get32(buf+16);
  284. const unsigned int wordstream_size= get32(buf+12);
  285. if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length
  286. || bitstream_size > (1<<26)
  287. || bytestream_size > (1<<26)
  288. || wordstream_size > (1<<26)
  289. ){
  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. return -1;
  293. }
  294. f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  295. f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + 20), bitstream_size/4);
  296. init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
  297. f->wordstream= (uint16_t*)(buf + 20 + bitstream_size);
  298. f->bytestream= buf + 20 + bitstream_size + wordstream_size;
  299. init_mv(f);
  300. for(y=0; y<height; y+=8){
  301. for(x=0; x<width; x+=8){
  302. decode_p_block(f, dst + x, src + x, 3, 3, stride);
  303. }
  304. src += 8*stride;
  305. dst += 8*stride;
  306. }
  307. if(bitstream_size != (get_bits_count(&f->gb)+31)/32*4)
  308. av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
  309. bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
  310. bytestream_size - (f->bytestream - (buf + 20 + bitstream_size + wordstream_size)),
  311. wordstream_size - (((uint8_t*)f->wordstream) - (buf + 20 + bitstream_size))
  312. );
  313. return 0;
  314. }
  315. /**
  316. * decode block and dequantize.
  317. * Note this is allmost identical to mjpeg
  318. */
  319. static int decode_i_block(FourXContext *f, DCTELEM *block){
  320. int code, i, j, level, val;
  321. /* DC coef */
  322. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  323. if (val>>4){
  324. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  325. }
  326. if(val)
  327. val = get_xbits(&f->gb, val);
  328. val = val * dequant_table[0] + f->last_dc;
  329. f->last_dc =
  330. block[0] = val;
  331. /* AC coefs */
  332. i = 1;
  333. for(;;) {
  334. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  335. /* EOB */
  336. if (code == 0)
  337. break;
  338. if (code == 0xf0) {
  339. i += 16;
  340. } else {
  341. level = get_xbits(&f->gb, code & 0xf);
  342. i += code >> 4;
  343. if (i >= 64) {
  344. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  345. return 0;
  346. }
  347. j= ff_zigzag_direct[i];
  348. block[j] = level * dequant_table[j];
  349. i++;
  350. if (i >= 64)
  351. break;
  352. }
  353. }
  354. return 0;
  355. }
  356. static inline void idct_put(FourXContext *f, int x, int y){
  357. DCTELEM (*block)[64]= f->block;
  358. int stride= f->current_picture.linesize[0]>>1;
  359. int i;
  360. uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
  361. for(i=0; i<4; i++){
  362. block[i][0] += 0x80*8*8;
  363. idct(block[i]);
  364. }
  365. if(!(f->avctx->flags&CODEC_FLAG_GRAY)){
  366. for(i=4; i<6; i++) idct(block[i]);
  367. }
  368. /* Note transform is:
  369. y= ( 1b + 4g + 2r)/14
  370. cb=( 3b - 2g - 1r)/14
  371. cr=(-1b - 4g + 5r)/14
  372. */
  373. for(y=0; y<8; y++){
  374. for(x=0; x<8; x++){
  375. DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3); //FIXME optimize
  376. int cb= block[4][x + 8*y];
  377. int cr= block[5][x + 8*y];
  378. int cg= (cb + cr)>>1;
  379. int y;
  380. cb+=cb;
  381. y = temp[0];
  382. dst[0 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  383. y = temp[1];
  384. dst[1 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  385. y = temp[8];
  386. dst[ stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  387. y = temp[9];
  388. dst[1+stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  389. dst += 2;
  390. }
  391. dst += 2*stride - 2*8;
  392. }
  393. }
  394. static int decode_i_mb(FourXContext *f){
  395. int i;
  396. f->dsp.clear_blocks(f->block[0]);
  397. for(i=0; i<6; i++){
  398. if(decode_i_block(f, f->block[i]) < 0)
  399. return -1;
  400. }
  401. return 0;
  402. }
  403. static uint8_t *read_huffman_tables(FourXContext *f, uint8_t * const buf){
  404. int frequency[512];
  405. uint8_t flag[512];
  406. int up[512];
  407. uint8_t len_tab[257];
  408. int bits_tab[257];
  409. int start, end;
  410. uint8_t *ptr= buf;
  411. int j;
  412. memset(frequency, 0, sizeof(frequency));
  413. memset(up, -1, sizeof(up));
  414. start= *ptr++;
  415. end= *ptr++;
  416. for(;;){
  417. int i;
  418. for(i=start; i<=end; i++){
  419. frequency[i]= *ptr++;
  420. // printf("%d %d %d\n", start, end, frequency[i]);
  421. }
  422. start= *ptr++;
  423. if(start==0) break;
  424. end= *ptr++;
  425. }
  426. frequency[256]=1;
  427. while((ptr - buf)&3) ptr++; // 4byte align
  428. // for(j=0; j<16; j++)
  429. // printf("%2X", ptr[j]);
  430. for(j=257; j<512; j++){
  431. int min_freq[2]= {256*256, 256*256};
  432. int smallest[2]= {0, 0};
  433. int i;
  434. for(i=0; i<j; i++){
  435. if(frequency[i] == 0) continue;
  436. if(frequency[i] < min_freq[1]){
  437. if(frequency[i] < min_freq[0]){
  438. min_freq[1]= min_freq[0]; smallest[1]= smallest[0];
  439. min_freq[0]= frequency[i];smallest[0]= i;
  440. }else{
  441. min_freq[1]= frequency[i];smallest[1]= i;
  442. }
  443. }
  444. }
  445. if(min_freq[1] == 256*256) break;
  446. frequency[j]= min_freq[0] + min_freq[1];
  447. flag[ smallest[0] ]= 0;
  448. flag[ smallest[1] ]= 1;
  449. up[ smallest[0] ]=
  450. up[ smallest[1] ]= j;
  451. frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
  452. }
  453. for(j=0; j<257; j++){
  454. int node;
  455. int len=0;
  456. int bits=0;
  457. for(node= j; up[node] != -1; node= up[node]){
  458. bits += flag[node]<<len;
  459. len++;
  460. if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
  461. }
  462. bits_tab[j]= bits;
  463. len_tab[j]= len;
  464. }
  465. init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
  466. len_tab , 1, 1,
  467. bits_tab, 4, 4, 0);
  468. return ptr;
  469. }
  470. static int decode_i_frame(FourXContext *f, uint8_t *buf, int length){
  471. int x, y;
  472. const int width= f->avctx->width;
  473. const int height= f->avctx->height;
  474. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  475. const int stride= f->current_picture.linesize[0]>>1;
  476. const unsigned int bitstream_size= get32(buf);
  477. const int token_count __attribute__((unused)) = get32(buf + bitstream_size + 8);
  478. unsigned int prestream_size= 4*get32(buf + bitstream_size + 4);
  479. uint8_t *prestream= buf + bitstream_size + 12;
  480. if(prestream_size + bitstream_size + 12 != length
  481. || bitstream_size > (1<<26)
  482. || prestream_size > (1<<26)){
  483. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
  484. return -1;
  485. }
  486. prestream= read_huffman_tables(f, prestream);
  487. init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
  488. prestream_size= length + buf - prestream;
  489. f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  490. f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)prestream, prestream_size/4);
  491. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
  492. f->last_dc= 0*128*8*8;
  493. for(y=0; y<height; y+=16){
  494. for(x=0; x<width; x+=16){
  495. if(decode_i_mb(f) < 0)
  496. return -1;
  497. idct_put(f, x, y);
  498. }
  499. dst += 16*stride;
  500. }
  501. if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  502. av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
  503. return 0;
  504. }
  505. static int decode_frame(AVCodecContext *avctx,
  506. void *data, int *data_size,
  507. uint8_t *buf, int buf_size)
  508. {
  509. FourXContext * const f = avctx->priv_data;
  510. AVFrame *picture = data;
  511. AVFrame *p, temp;
  512. int i, frame_4cc, frame_size;
  513. frame_4cc= get32(buf);
  514. if(buf_size != get32(buf+4)+8){
  515. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, get32(buf+4));
  516. }
  517. if(frame_4cc == ff_get_fourcc("cfrm")){
  518. int free_index=-1;
  519. const int data_size= buf_size - 20;
  520. const int id= get32(buf+12);
  521. const int whole_size= get32(buf+16);
  522. CFrameBuffer *cfrm;
  523. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  524. if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  525. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
  526. }
  527. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  528. if(f->cfrm[i].id == id) break;
  529. if(f->cfrm[i].size == 0 ) free_index= i;
  530. }
  531. if(i>=CFRAME_BUFFER_COUNT){
  532. i= free_index;
  533. f->cfrm[i].id= id;
  534. }
  535. cfrm= &f->cfrm[i];
  536. cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
  537. memcpy(cfrm->data + cfrm->size, buf+20, data_size);
  538. cfrm->size += data_size;
  539. if(cfrm->size >= whole_size){
  540. buf= cfrm->data;
  541. frame_size= cfrm->size;
  542. if(id != avctx->frame_number){
  543. av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
  544. }
  545. cfrm->size= cfrm->id= 0;
  546. frame_4cc= ff_get_fourcc("pfrm");
  547. }else
  548. return buf_size;
  549. }else{
  550. buf= buf + 12;
  551. frame_size= buf_size - 12;
  552. }
  553. temp= f->current_picture;
  554. f->current_picture= f->last_picture;
  555. f->last_picture= temp;
  556. p= &f->current_picture;
  557. avctx->coded_frame= p;
  558. avctx->flags |= CODEC_FLAG_EMU_EDGE; // alternatively we would have to use our own buffer management
  559. if(p->data[0])
  560. avctx->release_buffer(avctx, p);
  561. p->reference= 1;
  562. if(avctx->get_buffer(avctx, p) < 0){
  563. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  564. return -1;
  565. }
  566. if(frame_4cc == ff_get_fourcc("ifrm")){
  567. p->pict_type= I_TYPE;
  568. if(decode_i_frame(f, buf, frame_size) < 0)
  569. return -1;
  570. }else if(frame_4cc == ff_get_fourcc("pfrm")){
  571. p->pict_type= P_TYPE;
  572. if(decode_p_frame(f, buf, frame_size) < 0)
  573. return -1;
  574. }else if(frame_4cc == ff_get_fourcc("snd_")){
  575. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
  576. }else{
  577. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
  578. }
  579. #if 0
  580. for(i=0; i<20; i++){
  581. printf("%2X %c ", buf[i], clip(buf[i],16,126));
  582. }
  583. #endif
  584. p->key_frame= p->pict_type == I_TYPE;
  585. *picture= *p;
  586. *data_size = sizeof(AVPicture);
  587. emms_c();
  588. return buf_size;
  589. }
  590. static void common_init(AVCodecContext *avctx){
  591. FourXContext * const f = avctx->priv_data;
  592. dsputil_init(&f->dsp, avctx);
  593. f->avctx= avctx;
  594. }
  595. static int decode_init(AVCodecContext *avctx){
  596. FourXContext * const f = avctx->priv_data;
  597. common_init(avctx);
  598. init_vlcs(f);
  599. avctx->pix_fmt= PIX_FMT_RGB565;
  600. return 0;
  601. }
  602. static int decode_end(AVCodecContext *avctx){
  603. FourXContext * const f = avctx->priv_data;
  604. int i;
  605. av_freep(&f->bitstream_buffer);
  606. f->bitstream_buffer_size=0;
  607. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  608. av_freep(&f->cfrm[i].data);
  609. f->cfrm[i].allocated_size= 0;
  610. }
  611. free_vlc(&f->pre_vlc);
  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. };