4xm.c 23 KB

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