4xm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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 libavcodec/4xm.c
  23. * 4XM codec.
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "bitstream.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_8(DCTELEM, block[6][64]);
  130. uint8_t *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. int i;
  209. for(i=0; i<8; i++){
  210. init_vlc(&block_type_vlc[0][i], BLOCK_TYPE_VLC_BITS, 7,
  211. &block_type_tab[0][i][0][1], 2, 1,
  212. &block_type_tab[0][i][0][0], 2, 1, 1);
  213. }
  214. }
  215. static void init_mv(FourXContext *f){
  216. int i;
  217. for(i=0; i<256; i++){
  218. if(f->version>1)
  219. f->mv[i] = mv[i][0] + mv[i][1] *f->current_picture.linesize[0]/2;
  220. else
  221. f->mv[i] = (i&15) - 8 + ((i>>4)-8)*f->current_picture.linesize[0]/2;
  222. }
  223. }
  224. static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, int h, int stride, int scale, int dc){
  225. int i;
  226. dc*= 0x10001;
  227. switch(log2w){
  228. case 0:
  229. for(i=0; i<h; i++){
  230. dst[0] = scale*src[0] + dc;
  231. if(scale) src += stride;
  232. dst += stride;
  233. }
  234. break;
  235. case 1:
  236. for(i=0; i<h; i++){
  237. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  238. if(scale) src += stride;
  239. dst += stride;
  240. }
  241. break;
  242. case 2:
  243. for(i=0; i<h; i++){
  244. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  245. ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
  246. if(scale) src += stride;
  247. dst += stride;
  248. }
  249. break;
  250. case 3:
  251. for(i=0; i<h; i++){
  252. ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
  253. ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
  254. ((uint32_t*)dst)[2] = scale*((uint32_t*)src)[2] + dc;
  255. ((uint32_t*)dst)[3] = scale*((uint32_t*)src)[3] + dc;
  256. if(scale) src += stride;
  257. dst += stride;
  258. }
  259. break;
  260. default: assert(0);
  261. }
  262. }
  263. static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){
  264. const int index= size2index[log2h][log2w];
  265. const int h= 1<<log2h;
  266. int code= get_vlc2(&f->gb, block_type_vlc[1-(f->version>1)][index].table, BLOCK_TYPE_VLC_BITS, 1);
  267. uint16_t *start= (uint16_t*)f->last_picture.data[0];
  268. uint16_t *end= start + stride*(f->avctx->height-h+1) - (1<<log2w);
  269. assert(code>=0 && code<=6);
  270. if(code == 0){
  271. if (f->bytestream_end - f->bytestream < 1)
  272. return;
  273. src += f->mv[ *f->bytestream++ ];
  274. if(start > src || src > end){
  275. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  276. return;
  277. }
  278. mcdc(dst, src, log2w, h, stride, 1, 0);
  279. }else if(code == 1){
  280. log2h--;
  281. decode_p_block(f, dst , src , log2w, log2h, stride);
  282. decode_p_block(f, dst + (stride<<log2h), src + (stride<<log2h), log2w, log2h, stride);
  283. }else if(code == 2){
  284. log2w--;
  285. decode_p_block(f, dst , src , log2w, log2h, stride);
  286. decode_p_block(f, dst + (1<<log2w), src + (1<<log2w), log2w, log2h, stride);
  287. }else if(code == 3 && f->version<2){
  288. mcdc(dst, src, log2w, h, stride, 1, 0);
  289. }else if(code == 4){
  290. if (f->bytestream_end - f->bytestream < 1)
  291. return;
  292. src += f->mv[ *f->bytestream++ ];
  293. if(start > src || src > end){
  294. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  295. return;
  296. }
  297. if (f->wordstream_end - f->wordstream < 1)
  298. return;
  299. mcdc(dst, src, log2w, h, stride, 1, le2me_16(*f->wordstream++));
  300. }else if(code == 5){
  301. if (f->wordstream_end - f->wordstream < 1)
  302. return;
  303. mcdc(dst, src, log2w, h, stride, 0, le2me_16(*f->wordstream++));
  304. }else if(code == 6){
  305. if (f->wordstream_end - f->wordstream < 2)
  306. return;
  307. if(log2w){
  308. dst[0] = le2me_16(*f->wordstream++);
  309. dst[1] = le2me_16(*f->wordstream++);
  310. }else{
  311. dst[0 ] = le2me_16(*f->wordstream++);
  312. dst[stride] = le2me_16(*f->wordstream++);
  313. }
  314. }
  315. }
  316. static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length){
  317. int x, y;
  318. const int width= f->avctx->width;
  319. const int height= f->avctx->height;
  320. uint16_t *src= (uint16_t*)f->last_picture.data[0];
  321. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  322. const int stride= f->current_picture.linesize[0]>>1;
  323. unsigned int bitstream_size, bytestream_size, wordstream_size, extra;
  324. if(f->version>1){
  325. extra=20;
  326. if (length < extra)
  327. return -1;
  328. bitstream_size= AV_RL32(buf+8);
  329. wordstream_size= AV_RL32(buf+12);
  330. bytestream_size= AV_RL32(buf+16);
  331. }else{
  332. extra=0;
  333. bitstream_size = AV_RL16(buf-4);
  334. wordstream_size= AV_RL16(buf-2);
  335. bytestream_size= FFMAX(length - bitstream_size - wordstream_size, 0);
  336. }
  337. if (bitstream_size > length ||
  338. bytestream_size > length - bitstream_size ||
  339. wordstream_size > length - bytestream_size - bitstream_size ||
  340. extra > length - bytestream_size - bitstream_size - wordstream_size){
  341. av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
  342. bitstream_size+ bytestream_size+ wordstream_size - length);
  343. return -1;
  344. }
  345. f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  346. f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (const uint32_t*)(buf + extra), bitstream_size/4);
  347. init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
  348. f->wordstream= (const uint16_t*)(buf + extra + bitstream_size);
  349. f->wordstream_end= f->wordstream + wordstream_size/2;
  350. f->bytestream= buf + extra + bitstream_size + wordstream_size;
  351. f->bytestream_end = f->bytestream + bytestream_size;
  352. init_mv(f);
  353. for(y=0; y<height; y+=8){
  354. for(x=0; x<width; x+=8){
  355. decode_p_block(f, dst + x, src + x, 3, 3, stride);
  356. }
  357. src += 8*stride;
  358. dst += 8*stride;
  359. }
  360. if( bitstream_size != (get_bits_count(&f->gb)+31)/32*4
  361. || (((const char*)f->wordstream - (const char*)buf + 2)&~2) != extra + bitstream_size + wordstream_size
  362. || (((const char*)f->bytestream - (const char*)buf + 3)&~3) != extra + bitstream_size + wordstream_size + bytestream_size)
  363. av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
  364. bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
  365. -(((const char*)f->bytestream - (const char*)buf + 3)&~3) + (extra + bitstream_size + wordstream_size + bytestream_size),
  366. -(((const char*)f->wordstream - (const char*)buf + 2)&~2) + (extra + bitstream_size + wordstream_size)
  367. );
  368. return 0;
  369. }
  370. /**
  371. * decode block and dequantize.
  372. * Note this is almost identical to MJPEG.
  373. */
  374. static int decode_i_block(FourXContext *f, DCTELEM *block){
  375. int code, i, j, level, val;
  376. /* DC coef */
  377. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  378. if (val>>4){
  379. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  380. }
  381. if(val)
  382. val = get_xbits(&f->gb, val);
  383. val = val * dequant_table[0] + f->last_dc;
  384. f->last_dc =
  385. block[0] = val;
  386. /* AC coefs */
  387. i = 1;
  388. for(;;) {
  389. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  390. /* EOB */
  391. if (code == 0)
  392. break;
  393. if (code == 0xf0) {
  394. i += 16;
  395. } else {
  396. level = get_xbits(&f->gb, code & 0xf);
  397. i += code >> 4;
  398. if (i >= 64) {
  399. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  400. return 0;
  401. }
  402. j= ff_zigzag_direct[i];
  403. block[j] = level * dequant_table[j];
  404. i++;
  405. if (i >= 64)
  406. break;
  407. }
  408. }
  409. return 0;
  410. }
  411. static inline void idct_put(FourXContext *f, int x, int y){
  412. DCTELEM (*block)[64]= f->block;
  413. int stride= f->current_picture.linesize[0]>>1;
  414. int i;
  415. uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
  416. for(i=0; i<4; i++){
  417. block[i][0] += 0x80*8*8;
  418. idct(block[i]);
  419. }
  420. if(!(f->avctx->flags&CODEC_FLAG_GRAY)){
  421. for(i=4; i<6; i++) idct(block[i]);
  422. }
  423. /* Note transform is:
  424. y= ( 1b + 4g + 2r)/14
  425. cb=( 3b - 2g - 1r)/14
  426. cr=(-1b - 4g + 5r)/14
  427. */
  428. for(y=0; y<8; y++){
  429. for(x=0; x<8; x++){
  430. DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3); //FIXME optimize
  431. int cb= block[4][x + 8*y];
  432. int cr= block[5][x + 8*y];
  433. int cg= (cb + cr)>>1;
  434. int y;
  435. cb+=cb;
  436. y = temp[0];
  437. dst[0 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  438. y = temp[1];
  439. dst[1 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  440. y = temp[8];
  441. dst[ stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  442. y = temp[9];
  443. dst[1+stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  444. dst += 2;
  445. }
  446. dst += 2*stride - 2*8;
  447. }
  448. }
  449. static int decode_i_mb(FourXContext *f){
  450. int i;
  451. f->dsp.clear_blocks(f->block[0]);
  452. for(i=0; i<6; i++){
  453. if(decode_i_block(f, f->block[i]) < 0)
  454. return -1;
  455. }
  456. return 0;
  457. }
  458. static const uint8_t *read_huffman_tables(FourXContext *f, const uint8_t * const buf, int buf_size){
  459. int frequency[512];
  460. uint8_t flag[512];
  461. int up[512];
  462. uint8_t len_tab[257];
  463. int bits_tab[257];
  464. int start, end;
  465. const uint8_t *ptr= buf;
  466. const uint8_t *ptr_end = buf + buf_size;
  467. int j;
  468. memset(frequency, 0, sizeof(frequency));
  469. memset(up, -1, sizeof(up));
  470. start= *ptr++;
  471. end= *ptr++;
  472. for(;;){
  473. int i;
  474. if (start <= end && ptr_end - ptr < end - start + 1 + 1)
  475. return NULL;
  476. for(i=start; i<=end; i++){
  477. frequency[i]= *ptr++;
  478. }
  479. start= *ptr++;
  480. if(start==0) break;
  481. end= *ptr++;
  482. }
  483. frequency[256]=1;
  484. while((ptr - buf)&3) ptr++; // 4byte align
  485. for(j=257; j<512; j++){
  486. int min_freq[2]= {256*256, 256*256};
  487. int smallest[2]= {0, 0};
  488. int i;
  489. for(i=0; i<j; i++){
  490. if(frequency[i] == 0) continue;
  491. if(frequency[i] < min_freq[1]){
  492. if(frequency[i] < min_freq[0]){
  493. min_freq[1]= min_freq[0]; smallest[1]= smallest[0];
  494. min_freq[0]= frequency[i];smallest[0]= i;
  495. }else{
  496. min_freq[1]= frequency[i];smallest[1]= i;
  497. }
  498. }
  499. }
  500. if(min_freq[1] == 256*256) break;
  501. frequency[j]= min_freq[0] + min_freq[1];
  502. flag[ smallest[0] ]= 0;
  503. flag[ smallest[1] ]= 1;
  504. up[ smallest[0] ]=
  505. up[ smallest[1] ]= j;
  506. frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
  507. }
  508. for(j=0; j<257; j++){
  509. int node;
  510. int len=0;
  511. int bits=0;
  512. for(node= j; up[node] != -1; node= up[node]){
  513. bits += flag[node]<<len;
  514. len++;
  515. if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
  516. }
  517. bits_tab[j]= bits;
  518. len_tab[j]= len;
  519. }
  520. init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
  521. len_tab , 1, 1,
  522. bits_tab, 4, 4, 0);
  523. return ptr;
  524. }
  525. static int mix(int c0, int c1){
  526. int blue = 2*(c0&0x001F) + (c1&0x001F);
  527. int green= (2*(c0&0x03E0) + (c1&0x03E0))>>5;
  528. int red = 2*(c0>>10) + (c1>>10);
  529. return red/3*1024 + green/3*32 + blue/3;
  530. }
  531. static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length){
  532. int x, y, x2, y2;
  533. const int width= f->avctx->width;
  534. const int height= f->avctx->height;
  535. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  536. const int stride= f->current_picture.linesize[0]>>1;
  537. const uint8_t *buf_end = buf + length;
  538. for(y=0; y<height; y+=16){
  539. for(x=0; x<width; x+=16){
  540. unsigned int color[4], bits;
  541. if (buf_end - buf < 8)
  542. return -1;
  543. memset(color, 0, sizeof(color));
  544. //warning following is purely guessed ...
  545. color[0]= bytestream_get_le16(&buf);
  546. color[1]= bytestream_get_le16(&buf);
  547. if(color[0]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
  548. if(color[1]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
  549. color[2]= mix(color[0], color[1]);
  550. color[3]= mix(color[1], color[0]);
  551. bits= bytestream_get_le32(&buf);
  552. for(y2=0; y2<16; y2++){
  553. for(x2=0; x2<16; x2++){
  554. int index= 2*(x2>>2) + 8*(y2>>2);
  555. dst[y2*stride+x2]= color[(bits>>index)&3];
  556. }
  557. }
  558. dst+=16;
  559. }
  560. dst += 16*stride - width;
  561. }
  562. return 0;
  563. }
  564. static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length){
  565. int x, y;
  566. const int width= f->avctx->width;
  567. const int height= f->avctx->height;
  568. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  569. const int stride= f->current_picture.linesize[0]>>1;
  570. const unsigned int bitstream_size= AV_RL32(buf);
  571. unsigned int prestream_size;
  572. const uint8_t *prestream;
  573. if (bitstream_size > (1<<26) || length < bitstream_size + 12)
  574. return -1;
  575. prestream_size = 4*AV_RL32(buf + bitstream_size + 4);
  576. prestream = buf + bitstream_size + 12;
  577. if (prestream_size > (1<<26) ||
  578. prestream_size != length - (bitstream_size + 12)){
  579. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
  580. return -1;
  581. }
  582. prestream= read_huffman_tables(f, prestream, buf + length - prestream);
  583. if (!prestream)
  584. return -1;
  585. init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
  586. prestream_size= length + buf - prestream;
  587. f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  588. f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (const uint32_t*)prestream, prestream_size/4);
  589. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
  590. f->last_dc= 0*128*8*8;
  591. for(y=0; y<height; y+=16){
  592. for(x=0; x<width; x+=16){
  593. if(decode_i_mb(f) < 0)
  594. return -1;
  595. idct_put(f, x, y);
  596. }
  597. dst += 16*stride;
  598. }
  599. if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  600. av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
  601. return 0;
  602. }
  603. static int decode_frame(AVCodecContext *avctx,
  604. void *data, int *data_size,
  605. const uint8_t *buf, int buf_size)
  606. {
  607. FourXContext * const f = avctx->priv_data;
  608. AVFrame *picture = data;
  609. AVFrame *p, temp;
  610. int i, frame_4cc, frame_size;
  611. if (buf_size < 12)
  612. return AVERROR_INVALIDDATA;
  613. frame_4cc= AV_RL32(buf);
  614. if(buf_size != AV_RL32(buf+4)+8 || buf_size < 20){
  615. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, AV_RL32(buf+4));
  616. }
  617. if(frame_4cc == AV_RL32("cfrm")){
  618. int free_index=-1;
  619. const int data_size= buf_size - 20;
  620. const int id= AV_RL32(buf+12);
  621. const int whole_size= AV_RL32(buf+16);
  622. CFrameBuffer *cfrm;
  623. if (data_size < 0 || whole_size < 0)
  624. return AVERROR_INVALIDDATA;
  625. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  626. if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  627. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
  628. }
  629. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  630. if(f->cfrm[i].id == id) break;
  631. if(f->cfrm[i].size == 0 ) free_index= i;
  632. }
  633. if(i>=CFRAME_BUFFER_COUNT){
  634. i= free_index;
  635. f->cfrm[i].id= id;
  636. }
  637. cfrm= &f->cfrm[i];
  638. if (data_size > UINT_MAX - cfrm->size - FF_INPUT_BUFFER_PADDING_SIZE)
  639. return AVERROR_INVALIDDATA;
  640. cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
  641. if(!cfrm->data){ //explicit check needed as memcpy below might not catch a NULL
  642. av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
  643. return -1;
  644. }
  645. memcpy(cfrm->data + cfrm->size, buf+20, data_size);
  646. cfrm->size += data_size;
  647. if(cfrm->size >= whole_size){
  648. buf= cfrm->data;
  649. frame_size= cfrm->size;
  650. if(id != avctx->frame_number){
  651. av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
  652. }
  653. cfrm->size= cfrm->id= 0;
  654. frame_4cc= AV_RL32("pfrm");
  655. }else
  656. return buf_size;
  657. }else{
  658. buf= buf + 12;
  659. frame_size= buf_size - 12;
  660. }
  661. temp= f->current_picture;
  662. f->current_picture= f->last_picture;
  663. f->last_picture= temp;
  664. p= &f->current_picture;
  665. avctx->coded_frame= p;
  666. avctx->flags |= CODEC_FLAG_EMU_EDGE; // alternatively we would have to use our own buffer management
  667. if(p->data[0])
  668. avctx->release_buffer(avctx, p);
  669. p->reference= 1;
  670. if(avctx->get_buffer(avctx, p) < 0){
  671. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  672. return -1;
  673. }
  674. if(frame_4cc == AV_RL32("ifr2")){
  675. p->pict_type= FF_I_TYPE;
  676. if(decode_i2_frame(f, buf-4, frame_size) < 0)
  677. return -1;
  678. }else if(frame_4cc == AV_RL32("ifrm")){
  679. p->pict_type= FF_I_TYPE;
  680. if(decode_i_frame(f, buf, frame_size) < 0)
  681. return -1;
  682. }else if(frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")){
  683. p->pict_type= FF_P_TYPE;
  684. if(decode_p_frame(f, buf, frame_size) < 0)
  685. return -1;
  686. }else if(frame_4cc == AV_RL32("snd_")){
  687. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
  688. }else{
  689. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
  690. }
  691. p->key_frame= p->pict_type == FF_I_TYPE;
  692. *picture= *p;
  693. *data_size = sizeof(AVPicture);
  694. emms_c();
  695. return buf_size;
  696. }
  697. static av_cold void common_init(AVCodecContext *avctx){
  698. FourXContext * const f = avctx->priv_data;
  699. dsputil_init(&f->dsp, avctx);
  700. f->avctx= avctx;
  701. }
  702. static av_cold int decode_init(AVCodecContext *avctx){
  703. FourXContext * const f = avctx->priv_data;
  704. if(avctx->extradata_size != 4 || !avctx->extradata) {
  705. av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
  706. return 1;
  707. }
  708. f->version= AV_RL32(avctx->extradata)>>16;
  709. common_init(avctx);
  710. init_vlcs(f);
  711. if(f->version>2) avctx->pix_fmt= PIX_FMT_RGB565;
  712. else avctx->pix_fmt= PIX_FMT_RGB555;
  713. return 0;
  714. }
  715. static av_cold int decode_end(AVCodecContext *avctx){
  716. FourXContext * const f = avctx->priv_data;
  717. int i;
  718. av_freep(&f->bitstream_buffer);
  719. f->bitstream_buffer_size=0;
  720. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  721. av_freep(&f->cfrm[i].data);
  722. f->cfrm[i].allocated_size= 0;
  723. }
  724. free_vlc(&f->pre_vlc);
  725. return 0;
  726. }
  727. AVCodec fourxm_decoder = {
  728. "4xm",
  729. CODEC_TYPE_VIDEO,
  730. CODEC_ID_4XM,
  731. sizeof(FourXContext),
  732. decode_init,
  733. NULL,
  734. decode_end,
  735. decode_frame,
  736. /*CODEC_CAP_DR1,*/
  737. .long_name = NULL_IF_CONFIG_SMALL("4X Movie"),
  738. };