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.

933 lines
29KB

  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
  23. * 4XM codec.
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "get_bits.h"
  29. #include "bytestream.h"
  30. //#undef NDEBUG
  31. //#include <assert.h>
  32. #define BLOCK_TYPE_VLC_BITS 5
  33. #define ACDC_VLC_BITS 9
  34. #define CFRAME_BUFFER_COUNT 100
  35. static const uint8_t block_type_tab[2][4][8][2]={
  36. {
  37. { //{8,4,2}x{8,4,2}
  38. { 0,1}, { 2,2}, { 6,3}, {14,4}, {30,5}, {31,5}, { 0,0}
  39. },{ //{8,4}x1
  40. { 0,1}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}, { 0,0}
  41. },{ //1x{8,4}
  42. { 0,1}, { 2,2}, { 0,0}, { 6,3}, {14,4}, {15,4}, { 0,0}
  43. },{ //1x2, 2x1
  44. { 0,1}, { 0,0}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}
  45. }
  46. },{
  47. { //{8,4,2}x{8,4,2}
  48. { 1,2}, { 4,3}, { 5,3}, {0,2}, {6,3}, {7,3}, {0,0}
  49. },{//{8,4}x1
  50. { 1,2}, { 0,0}, { 2,2}, {0,2}, {6,3}, {7,3}, {0,0}
  51. },{//1x{8,4}
  52. { 1,2}, { 2,2}, { 0,0}, {0,2}, {6,3}, {7,3}, {0,0}
  53. },{//1x2, 2x1
  54. { 1,2}, { 0,0}, { 0,0}, {0,2}, {2,2}, {6,3}, {7,3}
  55. }
  56. }
  57. };
  58. static const uint8_t size2index[4][4]={
  59. {-1, 3, 1, 1},
  60. { 3, 0, 0, 0},
  61. { 2, 0, 0, 0},
  62. { 2, 0, 0, 0},
  63. };
  64. static const int8_t mv[256][2]={
  65. { 0, 0},{ 0, -1},{ -1, 0},{ 1, 0},{ 0, 1},{ -1, -1},{ 1, -1},{ -1, 1},
  66. { 1, 1},{ 0, -2},{ -2, 0},{ 2, 0},{ 0, 2},{ -1, -2},{ 1, -2},{ -2, -1},
  67. { 2, -1},{ -2, 1},{ 2, 1},{ -1, 2},{ 1, 2},{ -2, -2},{ 2, -2},{ -2, 2},
  68. { 2, 2},{ 0, -3},{ -3, 0},{ 3, 0},{ 0, 3},{ -1, -3},{ 1, -3},{ -3, -1},
  69. { 3, -1},{ -3, 1},{ 3, 1},{ -1, 3},{ 1, 3},{ -2, -3},{ 2, -3},{ -3, -2},
  70. { 3, -2},{ -3, 2},{ 3, 2},{ -2, 3},{ 2, 3},{ 0, -4},{ -4, 0},{ 4, 0},
  71. { 0, 4},{ -1, -4},{ 1, -4},{ -4, -1},{ 4, -1},{ 4, 1},{ -1, 4},{ 1, 4},
  72. { -3, -3},{ -3, 3},{ 3, 3},{ -2, -4},{ -4, -2},{ 4, -2},{ -4, 2},{ -2, 4},
  73. { 2, 4},{ -3, -4},{ 3, -4},{ 4, -3},{ -5, 0},{ -4, 3},{ -3, 4},{ 3, 4},
  74. { -1, -5},{ -5, -1},{ -5, 1},{ -1, 5},{ -2, -5},{ 2, -5},{ 5, -2},{ 5, 2},
  75. { -4, -4},{ -4, 4},{ -3, -5},{ -5, -3},{ -5, 3},{ 3, 5},{ -6, 0},{ 0, 6},
  76. { -6, -1},{ -6, 1},{ 1, 6},{ 2, -6},{ -6, 2},{ 2, 6},{ -5, -4},{ 5, 4},
  77. { 4, 5},{ -6, -3},{ 6, 3},{ -7, 0},{ -1, -7},{ 5, -5},{ -7, 1},{ -1, 7},
  78. { 4, -6},{ 6, 4},{ -2, -7},{ -7, 2},{ -3, -7},{ 7, -3},{ 3, 7},{ 6, -5},
  79. { 0, -8},{ -1, -8},{ -7, -4},{ -8, 1},{ 4, 7},{ 2, -8},{ -2, 8},{ 6, 6},
  80. { -8, 3},{ 5, -7},{ -5, 7},{ 8, -4},{ 0, -9},{ -9, -1},{ 1, 9},{ 7, -6},
  81. { -7, 6},{ -5, -8},{ -5, 8},{ -9, 3},{ 9, -4},{ 7, -7},{ 8, -6},{ 6, 8},
  82. { 10, 1},{-10, 2},{ 9, -5},{ 10, -3},{ -8, -7},{-10, -4},{ 6, -9},{-11, 0},
  83. { 11, 1},{-11, -2},{ -2, 11},{ 7, -9},{ -7, 9},{ 10, 6},{ -4, 11},{ 8, -9},
  84. { 8, 9},{ 5, 11},{ 7,-10},{ 12, -3},{ 11, 6},{ -9, -9},{ 8, 10},{ 5, 12},
  85. {-11, 7},{ 13, 2},{ 6,-12},{ 10, 9},{-11, 8},{ -7, 12},{ 0, 14},{ 14, -2},
  86. { -9, 11},{ -6, 13},{-14, -4},{ -5,-14},{ 5, 14},{-15, -1},{-14, -6},{ 3,-15},
  87. { 11,-11},{ -7, 14},{ -5, 15},{ 8,-14},{ 15, 6},{ 3, 16},{ 7,-15},{-16, 5},
  88. { 0, 17},{-16, -6},{-10, 14},{-16, 7},{ 12, 13},{-16, 8},{-17, 6},{-18, 3},
  89. { -7, 17},{ 15, 11},{ 16, 10},{ 2,-19},{ 3,-19},{-11,-16},{-18, 8},{-19, -6},
  90. { 2,-20},{-17,-11},{-10,-18},{ 8, 19},{-21, -1},{-20, 7},{ -4, 21},{ 21, 5},
  91. { 15, 16},{ 2,-22},{-10,-20},{-22, 5},{ 20,-11},{ -7,-22},{-12, 20},{ 23, -5},
  92. { 13,-20},{ 24, -2},{-15, 19},{-11, 22},{ 16, 19},{ 23,-10},{-18,-18},{ -9,-24},
  93. { 24,-10},{ -3, 26},{-23, 13},{-18,-20},{ 17, 21},{ -4, 27},{ 27, 6},{ 1,-28},
  94. {-11, 26},{-17,-23},{ 7, 28},{ 11,-27},{ 29, 5},{-23,-19},{-28,-11},{-21, 22},
  95. {-30, 7},{-17, 26},{-27, 16},{ 13, 29},{ 19,-26},{ 10,-31},{-14,-30},{ 20,-27},
  96. {-29, 18},{-16,-31},{-28,-22},{ 21,-30},{-25, 28},{ 26,-29},{ 25,-32},{-32,-32}
  97. };
  98. // this is simply the scaled down elementwise product of the standard jpeg quantizer table and the AAN premul table
  99. static const uint8_t dequant_table[64]={
  100. 16, 15, 13, 19, 24, 31, 28, 17,
  101. 17, 23, 25, 31, 36, 63, 45, 21,
  102. 18, 24, 27, 37, 52, 59, 49, 20,
  103. 16, 28, 34, 40, 60, 80, 51, 20,
  104. 18, 31, 48, 66, 68, 86, 56, 21,
  105. 19, 38, 56, 59, 64, 64, 48, 20,
  106. 27, 48, 55, 55, 56, 51, 35, 15,
  107. 20, 35, 34, 32, 31, 22, 15, 8,
  108. };
  109. static VLC block_type_vlc[2][4];
  110. typedef struct CFrameBuffer{
  111. unsigned int allocated_size;
  112. unsigned int size;
  113. int id;
  114. uint8_t *data;
  115. }CFrameBuffer;
  116. typedef struct FourXContext{
  117. AVCodecContext *avctx;
  118. DSPContext dsp;
  119. AVFrame current_picture, last_picture;
  120. GetBitContext pre_gb; ///< ac/dc prefix
  121. GetBitContext gb;
  122. const uint8_t *bytestream;
  123. const uint8_t *bytestream_end;
  124. const uint16_t *wordstream;
  125. const uint16_t *wordstream_end;
  126. int mv[256];
  127. VLC pre_vlc;
  128. int last_dc;
  129. DECLARE_ALIGNED(16, DCTELEM, block)[6][64];
  130. void *bitstream_buffer;
  131. unsigned int bitstream_buffer_size;
  132. int version;
  133. CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
  134. } FourXContext;
  135. #define FIX_1_082392200 70936
  136. #define FIX_1_414213562 92682
  137. #define FIX_1_847759065 121095
  138. #define FIX_2_613125930 171254
  139. #define MULTIPLY(var,const) (((var)*(const)) >> 16)
  140. static void idct(DCTELEM block[64]){
  141. int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  142. int tmp10, tmp11, tmp12, tmp13;
  143. int z5, z10, z11, z12, z13;
  144. int i;
  145. int temp[64];
  146. for(i=0; i<8; i++){
  147. tmp10 = block[8*0 + i] + block[8*4 + i];
  148. tmp11 = block[8*0 + i] - block[8*4 + i];
  149. tmp13 = block[8*2 + i] + block[8*6 + i];
  150. tmp12 = MULTIPLY(block[8*2 + i] - block[8*6 + i], FIX_1_414213562) - tmp13;
  151. tmp0 = tmp10 + tmp13;
  152. tmp3 = tmp10 - tmp13;
  153. tmp1 = tmp11 + tmp12;
  154. tmp2 = tmp11 - tmp12;
  155. z13 = block[8*5 + i] + block[8*3 + i];
  156. z10 = block[8*5 + i] - block[8*3 + i];
  157. z11 = block[8*1 + i] + block[8*7 + i];
  158. z12 = block[8*1 + i] - block[8*7 + i];
  159. tmp7 = z11 + z13;
  160. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
  161. z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
  162. tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
  163. tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5;
  164. tmp6 = tmp12 - tmp7;
  165. tmp5 = tmp11 - tmp6;
  166. tmp4 = tmp10 + tmp5;
  167. temp[8*0 + i] = tmp0 + tmp7;
  168. temp[8*7 + i] = tmp0 - tmp7;
  169. temp[8*1 + i] = tmp1 + tmp6;
  170. temp[8*6 + i] = tmp1 - tmp6;
  171. temp[8*2 + i] = tmp2 + tmp5;
  172. temp[8*5 + i] = tmp2 - tmp5;
  173. temp[8*4 + i] = tmp3 + tmp4;
  174. temp[8*3 + i] = tmp3 - tmp4;
  175. }
  176. for(i=0; i<8*8; i+=8){
  177. tmp10 = temp[0 + i] + temp[4 + i];
  178. tmp11 = temp[0 + i] - temp[4 + i];
  179. tmp13 = temp[2 + i] + temp[6 + i];
  180. tmp12 = MULTIPLY(temp[2 + i] - temp[6 + i], FIX_1_414213562) - tmp13;
  181. tmp0 = tmp10 + tmp13;
  182. tmp3 = tmp10 - tmp13;
  183. tmp1 = tmp11 + tmp12;
  184. tmp2 = tmp11 - tmp12;
  185. z13 = temp[5 + i] + temp[3 + i];
  186. z10 = temp[5 + i] - temp[3 + i];
  187. z11 = temp[1 + i] + temp[7 + i];
  188. z12 = temp[1 + i] - temp[7 + i];
  189. tmp7 = z11 + z13;
  190. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
  191. z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
  192. tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
  193. tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5;
  194. tmp6 = tmp12 - tmp7;
  195. tmp5 = tmp11 - tmp6;
  196. tmp4 = tmp10 + tmp5;
  197. block[0 + i] = (tmp0 + tmp7)>>6;
  198. block[7 + i] = (tmp0 - tmp7)>>6;
  199. block[1 + i] = (tmp1 + tmp6)>>6;
  200. block[6 + i] = (tmp1 - tmp6)>>6;
  201. block[2 + i] = (tmp2 + tmp5)>>6;
  202. block[5 + i] = (tmp2 - tmp5)>>6;
  203. block[4 + i] = (tmp3 + tmp4)>>6;
  204. block[3 + i] = (tmp3 - tmp4)>>6;
  205. }
  206. }
  207. static av_cold void init_vlcs(FourXContext *f){
  208. static VLC_TYPE table[8][32][2];
  209. int i;
  210. for(i=0; i<8; i++){
  211. block_type_vlc[0][i].table= table[i];
  212. block_type_vlc[0][i].table_allocated= 32;
  213. init_vlc(&block_type_vlc[0][i], BLOCK_TYPE_VLC_BITS, 7,
  214. &block_type_tab[0][i][0][1], 2, 1,
  215. &block_type_tab[0][i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
  216. }
  217. }
  218. static void init_mv(FourXContext *f){
  219. int i;
  220. for(i=0; i<256; i++){
  221. if(f->version>1)
  222. f->mv[i] = mv[i][0] + mv[i][1] *f->current_picture.linesize[0]/2;
  223. else
  224. f->mv[i] = (i&15) - 8 + ((i>>4)-8)*f->current_picture.linesize[0]/2;
  225. }
  226. }
  227. #if HAVE_BIGENDIAN
  228. #define LE_CENTRIC_MUL(dst, src, scale, dc) \
  229. { \
  230. unsigned tmpval = AV_RN32(src); \
  231. tmpval = (tmpval << 16) | (tmpval >> 16); \
  232. tmpval = tmpval * (scale) + (dc); \
  233. tmpval = (tmpval << 16) | (tmpval >> 16); \
  234. AV_WN32A(dst, tmpval); \
  235. }
  236. #else
  237. #define LE_CENTRIC_MUL(dst, src, scale, dc) \
  238. { \
  239. unsigned tmpval = AV_RN32(src) * (scale) + (dc); \
  240. AV_WN32A(dst, tmpval); \
  241. }
  242. #endif
  243. static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, int h, int stride, int scale, unsigned dc){
  244. int i;
  245. dc*= 0x10001;
  246. switch(log2w){
  247. case 0:
  248. for(i=0; i<h; i++){
  249. dst[0] = scale*src[0] + dc;
  250. if(scale) src += stride;
  251. dst += stride;
  252. }
  253. break;
  254. case 1:
  255. for(i=0; i<h; i++){
  256. LE_CENTRIC_MUL(dst, src, scale, dc);
  257. if(scale) src += stride;
  258. dst += stride;
  259. }
  260. break;
  261. case 2:
  262. for(i=0; i<h; i++){
  263. LE_CENTRIC_MUL(dst, src, scale, dc);
  264. LE_CENTRIC_MUL(dst + 2, src + 2, scale, dc);
  265. if(scale) src += stride;
  266. dst += stride;
  267. }
  268. break;
  269. case 3:
  270. for(i=0; i<h; i++){
  271. LE_CENTRIC_MUL(dst, src, scale, dc);
  272. LE_CENTRIC_MUL(dst + 2, src + 2, scale, dc);
  273. LE_CENTRIC_MUL(dst + 4, src + 4, scale, dc);
  274. LE_CENTRIC_MUL(dst + 6, src + 6, scale, dc);
  275. if(scale) src += stride;
  276. dst += stride;
  277. }
  278. break;
  279. default: assert(0);
  280. }
  281. }
  282. static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){
  283. const int index= size2index[log2h][log2w];
  284. const int h= 1<<log2h;
  285. int code= get_vlc2(&f->gb, block_type_vlc[1-(f->version>1)][index].table, BLOCK_TYPE_VLC_BITS, 1);
  286. uint16_t *start= (uint16_t*)f->last_picture.data[0];
  287. uint16_t *end= start + stride*(f->avctx->height-h+1) - (1<<log2w);
  288. assert(code>=0 && code<=6);
  289. if(code == 0){
  290. if (f->bytestream_end - f->bytestream < 1){
  291. av_log(f->avctx, AV_LOG_ERROR, "bytestream overread\n");
  292. return;
  293. }
  294. src += f->mv[ *f->bytestream++ ];
  295. if(start > src || src > end){
  296. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  297. return;
  298. }
  299. mcdc(dst, src, log2w, h, stride, 1, 0);
  300. }else if(code == 1){
  301. log2h--;
  302. decode_p_block(f, dst , src , log2w, log2h, stride);
  303. decode_p_block(f, dst + (stride<<log2h), src + (stride<<log2h), log2w, log2h, stride);
  304. }else if(code == 2){
  305. log2w--;
  306. decode_p_block(f, dst , src , log2w, log2h, stride);
  307. decode_p_block(f, dst + (1<<log2w), src + (1<<log2w), log2w, log2h, stride);
  308. }else if(code == 3 && f->version<2){
  309. mcdc(dst, src, log2w, h, stride, 1, 0);
  310. }else if(code == 4){
  311. if (f->bytestream_end - f->bytestream < 1){
  312. av_log(f->avctx, AV_LOG_ERROR, "bytestream overread\n");
  313. return;
  314. }
  315. src += f->mv[ *f->bytestream++ ];
  316. if(start > src || src > end){
  317. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  318. return;
  319. }
  320. if (f->wordstream_end - f->wordstream < 1){
  321. av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n");
  322. return;
  323. }
  324. mcdc(dst, src, log2w, h, stride, 1, av_le2ne16(*f->wordstream++));
  325. }else if(code == 5){
  326. if (f->wordstream_end - f->wordstream < 1){
  327. av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n");
  328. return;
  329. }
  330. mcdc(dst, src, log2w, h, stride, 0, av_le2ne16(*f->wordstream++));
  331. }else if(code == 6){
  332. if (f->wordstream_end - f->wordstream < 2){
  333. av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n");
  334. return;
  335. }
  336. if(log2w){
  337. dst[0] = av_le2ne16(*f->wordstream++);
  338. dst[1] = av_le2ne16(*f->wordstream++);
  339. }else{
  340. dst[0 ] = av_le2ne16(*f->wordstream++);
  341. dst[stride] = av_le2ne16(*f->wordstream++);
  342. }
  343. }
  344. }
  345. static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length){
  346. int x, y;
  347. const int width= f->avctx->width;
  348. const int height= f->avctx->height;
  349. uint16_t *src= (uint16_t*)f->last_picture.data[0];
  350. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  351. const int stride= f->current_picture.linesize[0]>>1;
  352. unsigned int bitstream_size, bytestream_size, wordstream_size, extra;
  353. if(f->version>1){
  354. extra=20;
  355. if (length < extra)
  356. return -1;
  357. bitstream_size= AV_RL32(buf+8);
  358. wordstream_size= AV_RL32(buf+12);
  359. bytestream_size= AV_RL32(buf+16);
  360. }else{
  361. extra=0;
  362. bitstream_size = AV_RL16(buf-4);
  363. wordstream_size= AV_RL16(buf-2);
  364. bytestream_size= FFMAX(length - bitstream_size - wordstream_size, 0);
  365. }
  366. if (bitstream_size > length ||
  367. bytestream_size > length - bitstream_size ||
  368. wordstream_size > length - bytestream_size - bitstream_size ||
  369. extra > length - bytestream_size - bitstream_size - wordstream_size){
  370. av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
  371. bitstream_size+ bytestream_size+ wordstream_size - length);
  372. return -1;
  373. }
  374. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  375. if (!f->bitstream_buffer)
  376. return AVERROR(ENOMEM);
  377. f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)(buf + extra), bitstream_size/4);
  378. memset((uint8_t*)f->bitstream_buffer + bitstream_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  379. init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
  380. f->wordstream= (const uint16_t*)(buf + extra + bitstream_size);
  381. f->wordstream_end= f->wordstream + wordstream_size/2;
  382. f->bytestream= buf + extra + bitstream_size + wordstream_size;
  383. f->bytestream_end = f->bytestream + bytestream_size;
  384. init_mv(f);
  385. for(y=0; y<height; y+=8){
  386. for(x=0; x<width; x+=8){
  387. decode_p_block(f, dst + x, src + x, 3, 3, stride);
  388. }
  389. src += 8*stride;
  390. dst += 8*stride;
  391. }
  392. return 0;
  393. }
  394. /**
  395. * decode block and dequantize.
  396. * Note this is almost identical to MJPEG.
  397. */
  398. static int decode_i_block(FourXContext *f, DCTELEM *block){
  399. int code, i, j, level, val;
  400. if(get_bits_left(&f->gb) < 2){
  401. av_log(f->avctx, AV_LOG_ERROR, "%d bits left before decode_i_block()\n", get_bits_left(&f->gb));
  402. return -1;
  403. }
  404. /* DC coef */
  405. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  406. if (val>>4){
  407. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  408. }
  409. if(val)
  410. val = get_xbits(&f->gb, val);
  411. val = val * dequant_table[0] + f->last_dc;
  412. f->last_dc =
  413. block[0] = val;
  414. /* AC coefs */
  415. i = 1;
  416. for(;;) {
  417. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  418. /* EOB */
  419. if (code == 0)
  420. break;
  421. if (code == 0xf0) {
  422. i += 16;
  423. } else {
  424. level = get_xbits(&f->gb, code & 0xf);
  425. i += code >> 4;
  426. if (i >= 64) {
  427. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  428. return 0;
  429. }
  430. j= ff_zigzag_direct[i];
  431. block[j] = level * dequant_table[j];
  432. i++;
  433. if (i >= 64)
  434. break;
  435. }
  436. }
  437. return 0;
  438. }
  439. static inline void idct_put(FourXContext *f, int x, int y){
  440. DCTELEM (*block)[64]= f->block;
  441. int stride= f->current_picture.linesize[0]>>1;
  442. int i;
  443. uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
  444. for(i=0; i<4; i++){
  445. block[i][0] += 0x80*8*8;
  446. idct(block[i]);
  447. }
  448. if(!(f->avctx->flags&CODEC_FLAG_GRAY)){
  449. for(i=4; i<6; i++) idct(block[i]);
  450. }
  451. /* Note transform is:
  452. y= ( 1b + 4g + 2r)/14
  453. cb=( 3b - 2g - 1r)/14
  454. cr=(-1b - 4g + 5r)/14
  455. */
  456. for(y=0; y<8; y++){
  457. for(x=0; x<8; x++){
  458. DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3); //FIXME optimize
  459. int cb= block[4][x + 8*y];
  460. int cr= block[5][x + 8*y];
  461. int cg= (cb + cr)>>1;
  462. int y;
  463. cb+=cb;
  464. y = temp[0];
  465. dst[0 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  466. y = temp[1];
  467. dst[1 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  468. y = temp[8];
  469. dst[ stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  470. y = temp[9];
  471. dst[1+stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  472. dst += 2;
  473. }
  474. dst += 2*stride - 2*8;
  475. }
  476. }
  477. static int decode_i_mb(FourXContext *f){
  478. int i;
  479. f->dsp.clear_blocks(f->block[0]);
  480. for(i=0; i<6; i++){
  481. if(decode_i_block(f, f->block[i]) < 0)
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. static const uint8_t *read_huffman_tables(FourXContext *f, const uint8_t * const buf, int buf_size){
  487. int frequency[512];
  488. uint8_t flag[512];
  489. int up[512];
  490. uint8_t len_tab[257];
  491. int bits_tab[257];
  492. int start, end;
  493. const uint8_t *ptr= buf;
  494. const uint8_t *ptr_end = buf + buf_size;
  495. int j;
  496. memset(frequency, 0, sizeof(frequency));
  497. memset(up, -1, sizeof(up));
  498. start= *ptr++;
  499. end= *ptr++;
  500. for(;;){
  501. int i;
  502. if (start <= end && ptr_end - ptr < end - start + 1 + 1)
  503. return NULL;
  504. for(i=start; i<=end; i++){
  505. frequency[i]= *ptr++;
  506. }
  507. start= *ptr++;
  508. if(start==0) break;
  509. end= *ptr++;
  510. }
  511. frequency[256]=1;
  512. while((ptr - buf)&3) ptr++; // 4byte align
  513. for(j=257; j<512; j++){
  514. int min_freq[2]= {256*256, 256*256};
  515. int smallest[2]= {0, 0};
  516. int i;
  517. for(i=0; i<j; i++){
  518. if(frequency[i] == 0) continue;
  519. if(frequency[i] < min_freq[1]){
  520. if(frequency[i] < min_freq[0]){
  521. min_freq[1]= min_freq[0]; smallest[1]= smallest[0];
  522. min_freq[0]= frequency[i];smallest[0]= i;
  523. }else{
  524. min_freq[1]= frequency[i];smallest[1]= i;
  525. }
  526. }
  527. }
  528. if(min_freq[1] == 256*256) break;
  529. frequency[j]= min_freq[0] + min_freq[1];
  530. flag[ smallest[0] ]= 0;
  531. flag[ smallest[1] ]= 1;
  532. up[ smallest[0] ]=
  533. up[ smallest[1] ]= j;
  534. frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
  535. }
  536. for(j=0; j<257; j++){
  537. int node;
  538. int len=0;
  539. int bits=0;
  540. for(node= j; up[node] != -1; node= up[node]){
  541. bits += flag[node]<<len;
  542. len++;
  543. if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
  544. }
  545. bits_tab[j]= bits;
  546. len_tab[j]= len;
  547. }
  548. if (init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
  549. len_tab , 1, 1,
  550. bits_tab, 4, 4, 0))
  551. return NULL;
  552. return ptr;
  553. }
  554. static int mix(int c0, int c1){
  555. int blue = 2*(c0&0x001F) + (c1&0x001F);
  556. int green= (2*(c0&0x03E0) + (c1&0x03E0))>>5;
  557. int red = 2*(c0>>10) + (c1>>10);
  558. return red/3*1024 + green/3*32 + blue/3;
  559. }
  560. static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length){
  561. int x, y, x2, y2;
  562. const int width= f->avctx->width;
  563. const int height= f->avctx->height;
  564. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  565. const int stride= f->current_picture.linesize[0]>>1;
  566. const uint8_t *buf_end = buf + length;
  567. for(y=0; y<height; y+=16){
  568. for(x=0; x<width; x+=16){
  569. unsigned int color[4], bits;
  570. if (buf_end - buf < 8)
  571. return -1;
  572. memset(color, 0, sizeof(color));
  573. //warning following is purely guessed ...
  574. color[0]= bytestream_get_le16(&buf);
  575. color[1]= bytestream_get_le16(&buf);
  576. if(color[0]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
  577. if(color[1]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
  578. color[2]= mix(color[0], color[1]);
  579. color[3]= mix(color[1], color[0]);
  580. bits= bytestream_get_le32(&buf);
  581. for(y2=0; y2<16; y2++){
  582. for(x2=0; x2<16; x2++){
  583. int index= 2*(x2>>2) + 8*(y2>>2);
  584. dst[y2*stride+x2]= color[(bits>>index)&3];
  585. }
  586. }
  587. dst+=16;
  588. }
  589. dst += 16*stride - width;
  590. }
  591. return 0;
  592. }
  593. static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length){
  594. int x, y;
  595. const int width= f->avctx->width;
  596. const int height= f->avctx->height;
  597. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  598. const int stride= f->current_picture.linesize[0]>>1;
  599. const unsigned int bitstream_size= AV_RL32(buf);
  600. unsigned int prestream_size;
  601. const uint8_t *prestream;
  602. if (bitstream_size > (1<<26) || length < bitstream_size + 12)
  603. return -1;
  604. prestream_size = 4*AV_RL32(buf + bitstream_size + 4);
  605. prestream = buf + bitstream_size + 12;
  606. if (prestream_size > (1<<26) ||
  607. prestream_size != length - (bitstream_size + 12)){
  608. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
  609. return -1;
  610. }
  611. prestream= read_huffman_tables(f, prestream, buf + length - prestream);
  612. if (!prestream)
  613. return -1;
  614. init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
  615. prestream_size= length + buf - prestream;
  616. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size, prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  617. if (!f->bitstream_buffer)
  618. return AVERROR(ENOMEM);
  619. f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)prestream, prestream_size/4);
  620. memset((uint8_t*)f->bitstream_buffer + prestream_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  621. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
  622. f->last_dc= 0*128*8*8;
  623. for(y=0; y<height; y+=16){
  624. for(x=0; x<width; x+=16){
  625. if(decode_i_mb(f) < 0)
  626. return -1;
  627. idct_put(f, x, y);
  628. }
  629. dst += 16*stride;
  630. }
  631. if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  632. av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
  633. return 0;
  634. }
  635. static int decode_frame(AVCodecContext *avctx,
  636. void *data, int *data_size,
  637. AVPacket *avpkt)
  638. {
  639. const uint8_t *buf = avpkt->data;
  640. int buf_size = avpkt->size;
  641. FourXContext * const f = avctx->priv_data;
  642. AVFrame *picture = data;
  643. AVFrame *p, temp;
  644. int i, frame_4cc, frame_size;
  645. if (buf_size < 12)
  646. return AVERROR_INVALIDDATA;
  647. frame_4cc= AV_RL32(buf);
  648. if(buf_size != AV_RL32(buf+4)+8 || buf_size < 20){
  649. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, AV_RL32(buf+4));
  650. }
  651. if(frame_4cc == AV_RL32("cfrm")){
  652. int free_index=-1;
  653. const int data_size= buf_size - 20;
  654. const int id= AV_RL32(buf+12);
  655. const int whole_size= AV_RL32(buf+16);
  656. CFrameBuffer *cfrm;
  657. if (data_size < 0 || whole_size < 0){
  658. av_log(f->avctx, AV_LOG_ERROR, "sizes invalid\n");
  659. return AVERROR_INVALIDDATA;
  660. }
  661. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  662. if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  663. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
  664. }
  665. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  666. if(f->cfrm[i].id == id) break;
  667. if(f->cfrm[i].size == 0 ) free_index= i;
  668. }
  669. if(i>=CFRAME_BUFFER_COUNT){
  670. i= free_index;
  671. f->cfrm[i].id= id;
  672. }
  673. cfrm= &f->cfrm[i];
  674. if (data_size > UINT_MAX - cfrm->size - FF_INPUT_BUFFER_PADDING_SIZE)
  675. return AVERROR_INVALIDDATA;
  676. cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
  677. if(!cfrm->data){ //explicit check needed as memcpy below might not catch a NULL
  678. av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
  679. return -1;
  680. }
  681. memcpy(cfrm->data + cfrm->size, buf+20, data_size);
  682. cfrm->size += data_size;
  683. if(cfrm->size >= whole_size){
  684. buf= cfrm->data;
  685. frame_size= cfrm->size;
  686. if(id != avctx->frame_number){
  687. av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
  688. }
  689. cfrm->size= cfrm->id= 0;
  690. frame_4cc= AV_RL32("pfrm");
  691. }else
  692. return buf_size;
  693. }else{
  694. buf= buf + 12;
  695. frame_size= buf_size - 12;
  696. }
  697. temp= f->current_picture;
  698. f->current_picture= f->last_picture;
  699. f->last_picture= temp;
  700. p= &f->current_picture;
  701. avctx->coded_frame= p;
  702. avctx->flags |= CODEC_FLAG_EMU_EDGE; // alternatively we would have to use our own buffer management
  703. p->reference= 3;
  704. if (avctx->reget_buffer(avctx, p) < 0) {
  705. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  706. return -1;
  707. }
  708. if(frame_4cc == AV_RL32("ifr2")){
  709. p->pict_type= AV_PICTURE_TYPE_I;
  710. if(decode_i2_frame(f, buf-4, frame_size+4) < 0){
  711. av_log(f->avctx, AV_LOG_ERROR, "decode i2 frame failed\n");
  712. return -1;
  713. }
  714. }else if(frame_4cc == AV_RL32("ifrm")){
  715. p->pict_type= AV_PICTURE_TYPE_I;
  716. if(decode_i_frame(f, buf, frame_size) < 0){
  717. av_log(f->avctx, AV_LOG_ERROR, "decode i frame failed\n");
  718. return -1;
  719. }
  720. }else if(frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")){
  721. if(!f->last_picture.data[0]){
  722. f->last_picture.reference= 3;
  723. if(avctx->get_buffer(avctx, &f->last_picture) < 0){
  724. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  725. return -1;
  726. }
  727. }
  728. p->pict_type= AV_PICTURE_TYPE_P;
  729. if(decode_p_frame(f, buf, frame_size) < 0){
  730. av_log(f->avctx, AV_LOG_ERROR, "decode p frame failed\n");
  731. return -1;
  732. }
  733. }else if(frame_4cc == AV_RL32("snd_")){
  734. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
  735. }else{
  736. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
  737. }
  738. p->key_frame= p->pict_type == AV_PICTURE_TYPE_I;
  739. *picture= *p;
  740. *data_size = sizeof(AVPicture);
  741. emms_c();
  742. return buf_size;
  743. }
  744. static av_cold void common_init(AVCodecContext *avctx){
  745. FourXContext * const f = avctx->priv_data;
  746. dsputil_init(&f->dsp, avctx);
  747. f->avctx= avctx;
  748. }
  749. static av_cold int decode_init(AVCodecContext *avctx){
  750. FourXContext * const f = avctx->priv_data;
  751. if(avctx->extradata_size != 4 || !avctx->extradata) {
  752. av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
  753. return 1;
  754. }
  755. if((avctx->width % 16) || (avctx->height % 16)) {
  756. av_log(avctx, AV_LOG_ERROR, "unsupported width/height\n");
  757. return AVERROR_INVALIDDATA;
  758. }
  759. avcodec_get_frame_defaults(&f->current_picture);
  760. avcodec_get_frame_defaults(&f->last_picture);
  761. f->version= AV_RL32(avctx->extradata)>>16;
  762. common_init(avctx);
  763. init_vlcs(f);
  764. if(f->version>2) avctx->pix_fmt= PIX_FMT_RGB565;
  765. else avctx->pix_fmt= PIX_FMT_BGR555;
  766. return 0;
  767. }
  768. static av_cold int decode_end(AVCodecContext *avctx){
  769. FourXContext * const f = avctx->priv_data;
  770. int i;
  771. av_freep(&f->bitstream_buffer);
  772. f->bitstream_buffer_size=0;
  773. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  774. av_freep(&f->cfrm[i].data);
  775. f->cfrm[i].allocated_size= 0;
  776. }
  777. free_vlc(&f->pre_vlc);
  778. if(f->current_picture.data[0])
  779. avctx->release_buffer(avctx, &f->current_picture);
  780. if(f->last_picture.data[0])
  781. avctx->release_buffer(avctx, &f->last_picture);
  782. return 0;
  783. }
  784. AVCodec ff_fourxm_decoder = {
  785. .name = "4xm",
  786. .type = AVMEDIA_TYPE_VIDEO,
  787. .id = CODEC_ID_4XM,
  788. .priv_data_size = sizeof(FourXContext),
  789. .init = decode_init,
  790. .close = decode_end,
  791. .decode = decode_frame,
  792. .capabilities = CODEC_CAP_DR1,
  793. .long_name = NULL_IF_CONFIG_SMALL("4X Movie"),
  794. };