4xm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. if( bitstream_size != (get_bits_count(&f->gb)+31)/32*4
  393. || (((const char*)f->wordstream - (const char*)buf + 2)&~2) != extra + bitstream_size + wordstream_size
  394. || (((const char*)f->bytestream - (const char*)buf + 3)&~3) != extra + bitstream_size + wordstream_size + bytestream_size)
  395. av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
  396. bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
  397. -(((const char*)f->bytestream - (const char*)buf + 3)&~3) + (extra + bitstream_size + wordstream_size + bytestream_size),
  398. -(((const char*)f->wordstream - (const char*)buf + 2)&~2) + (extra + bitstream_size + wordstream_size)
  399. );
  400. return 0;
  401. }
  402. /**
  403. * decode block and dequantize.
  404. * Note this is almost identical to MJPEG.
  405. */
  406. static int decode_i_block(FourXContext *f, DCTELEM *block){
  407. int code, i, j, level, val;
  408. /* DC coef */
  409. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  410. if (val>>4){
  411. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  412. }
  413. if(val)
  414. val = get_xbits(&f->gb, val);
  415. val = val * dequant_table[0] + f->last_dc;
  416. f->last_dc =
  417. block[0] = val;
  418. /* AC coefs */
  419. i = 1;
  420. for(;;) {
  421. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  422. /* EOB */
  423. if (code == 0)
  424. break;
  425. if (code == 0xf0) {
  426. i += 16;
  427. } else {
  428. level = get_xbits(&f->gb, code & 0xf);
  429. i += code >> 4;
  430. if (i >= 64) {
  431. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  432. return 0;
  433. }
  434. j= ff_zigzag_direct[i];
  435. block[j] = level * dequant_table[j];
  436. i++;
  437. if (i >= 64)
  438. break;
  439. }
  440. }
  441. return 0;
  442. }
  443. static inline void idct_put(FourXContext *f, int x, int y){
  444. DCTELEM (*block)[64]= f->block;
  445. int stride= f->current_picture.linesize[0]>>1;
  446. int i;
  447. uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
  448. for(i=0; i<4; i++){
  449. block[i][0] += 0x80*8*8;
  450. idct(block[i]);
  451. }
  452. if(!(f->avctx->flags&CODEC_FLAG_GRAY)){
  453. for(i=4; i<6; i++) idct(block[i]);
  454. }
  455. /* Note transform is:
  456. y= ( 1b + 4g + 2r)/14
  457. cb=( 3b - 2g - 1r)/14
  458. cr=(-1b - 4g + 5r)/14
  459. */
  460. for(y=0; y<8; y++){
  461. for(x=0; x<8; x++){
  462. DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3); //FIXME optimize
  463. int cb= block[4][x + 8*y];
  464. int cr= block[5][x + 8*y];
  465. int cg= (cb + cr)>>1;
  466. int y;
  467. cb+=cb;
  468. y = temp[0];
  469. dst[0 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  470. y = temp[1];
  471. dst[1 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  472. y = temp[8];
  473. dst[ stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  474. y = temp[9];
  475. dst[1+stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
  476. dst += 2;
  477. }
  478. dst += 2*stride - 2*8;
  479. }
  480. }
  481. static int decode_i_mb(FourXContext *f){
  482. int i;
  483. f->dsp.clear_blocks(f->block[0]);
  484. for(i=0; i<6; i++){
  485. if(decode_i_block(f, f->block[i]) < 0)
  486. return -1;
  487. }
  488. return 0;
  489. }
  490. static const uint8_t *read_huffman_tables(FourXContext *f, const uint8_t * const buf, int buf_size){
  491. int frequency[512];
  492. uint8_t flag[512];
  493. int up[512];
  494. uint8_t len_tab[257];
  495. int bits_tab[257];
  496. int start, end;
  497. const uint8_t *ptr= buf;
  498. const uint8_t *ptr_end = buf + buf_size;
  499. int j;
  500. memset(frequency, 0, sizeof(frequency));
  501. memset(up, -1, sizeof(up));
  502. start= *ptr++;
  503. end= *ptr++;
  504. for(;;){
  505. int i;
  506. if (start <= end && ptr_end - ptr < end - start + 1 + 1)
  507. return NULL;
  508. for(i=start; i<=end; i++){
  509. frequency[i]= *ptr++;
  510. }
  511. start= *ptr++;
  512. if(start==0) break;
  513. end= *ptr++;
  514. }
  515. frequency[256]=1;
  516. while((ptr - buf)&3) ptr++; // 4byte align
  517. for(j=257; j<512; j++){
  518. int min_freq[2]= {256*256, 256*256};
  519. int smallest[2]= {0, 0};
  520. int i;
  521. for(i=0; i<j; i++){
  522. if(frequency[i] == 0) continue;
  523. if(frequency[i] < min_freq[1]){
  524. if(frequency[i] < min_freq[0]){
  525. min_freq[1]= min_freq[0]; smallest[1]= smallest[0];
  526. min_freq[0]= frequency[i];smallest[0]= i;
  527. }else{
  528. min_freq[1]= frequency[i];smallest[1]= i;
  529. }
  530. }
  531. }
  532. if(min_freq[1] == 256*256) break;
  533. frequency[j]= min_freq[0] + min_freq[1];
  534. flag[ smallest[0] ]= 0;
  535. flag[ smallest[1] ]= 1;
  536. up[ smallest[0] ]=
  537. up[ smallest[1] ]= j;
  538. frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
  539. }
  540. for(j=0; j<257; j++){
  541. int node;
  542. int len=0;
  543. int bits=0;
  544. for(node= j; up[node] != -1; node= up[node]){
  545. bits += flag[node]<<len;
  546. len++;
  547. if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
  548. }
  549. bits_tab[j]= bits;
  550. len_tab[j]= len;
  551. }
  552. if (init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
  553. len_tab , 1, 1,
  554. bits_tab, 4, 4, 0))
  555. return NULL;
  556. return ptr;
  557. }
  558. static int mix(int c0, int c1){
  559. int blue = 2*(c0&0x001F) + (c1&0x001F);
  560. int green= (2*(c0&0x03E0) + (c1&0x03E0))>>5;
  561. int red = 2*(c0>>10) + (c1>>10);
  562. return red/3*1024 + green/3*32 + blue/3;
  563. }
  564. static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length){
  565. int x, y, x2, y2;
  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 uint8_t *buf_end = buf + length;
  571. for(y=0; y<height; y+=16){
  572. for(x=0; x<width; x+=16){
  573. unsigned int color[4], bits;
  574. if (buf_end - buf < 8)
  575. return -1;
  576. memset(color, 0, sizeof(color));
  577. //warning following is purely guessed ...
  578. color[0]= bytestream_get_le16(&buf);
  579. color[1]= bytestream_get_le16(&buf);
  580. if(color[0]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
  581. if(color[1]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
  582. color[2]= mix(color[0], color[1]);
  583. color[3]= mix(color[1], color[0]);
  584. bits= bytestream_get_le32(&buf);
  585. for(y2=0; y2<16; y2++){
  586. for(x2=0; x2<16; x2++){
  587. int index= 2*(x2>>2) + 8*(y2>>2);
  588. dst[y2*stride+x2]= color[(bits>>index)&3];
  589. }
  590. }
  591. dst+=16;
  592. }
  593. dst += 16*stride - width;
  594. }
  595. return 0;
  596. }
  597. static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length){
  598. int x, y;
  599. const int width= f->avctx->width;
  600. const int height= f->avctx->height;
  601. uint16_t *dst= (uint16_t*)f->current_picture.data[0];
  602. const int stride= f->current_picture.linesize[0]>>1;
  603. const unsigned int bitstream_size= AV_RL32(buf);
  604. unsigned int prestream_size;
  605. const uint8_t *prestream;
  606. if (bitstream_size > (1<<26) || length < bitstream_size + 12)
  607. return -1;
  608. prestream_size = 4*AV_RL32(buf + bitstream_size + 4);
  609. prestream = buf + bitstream_size + 12;
  610. if (prestream_size > (1<<26) ||
  611. prestream_size != length - (bitstream_size + 12)){
  612. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
  613. return -1;
  614. }
  615. prestream= read_huffman_tables(f, prestream, buf + length - prestream);
  616. if (!prestream)
  617. return -1;
  618. init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
  619. prestream_size= length + buf - prestream;
  620. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size, prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  621. if (!f->bitstream_buffer)
  622. return AVERROR(ENOMEM);
  623. f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)prestream, prestream_size/4);
  624. memset((uint8_t*)f->bitstream_buffer + prestream_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  625. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
  626. f->last_dc= 0*128*8*8;
  627. for(y=0; y<height; y+=16){
  628. for(x=0; x<width; x+=16){
  629. if(decode_i_mb(f) < 0)
  630. return -1;
  631. idct_put(f, x, y);
  632. }
  633. dst += 16*stride;
  634. }
  635. if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  636. av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
  637. return 0;
  638. }
  639. static int decode_frame(AVCodecContext *avctx,
  640. void *data, int *data_size,
  641. AVPacket *avpkt)
  642. {
  643. const uint8_t *buf = avpkt->data;
  644. int buf_size = avpkt->size;
  645. FourXContext * const f = avctx->priv_data;
  646. AVFrame *picture = data;
  647. AVFrame *p, temp;
  648. int i, frame_4cc, frame_size;
  649. if (buf_size < 12)
  650. return AVERROR_INVALIDDATA;
  651. frame_4cc= AV_RL32(buf);
  652. if(buf_size != AV_RL32(buf+4)+8 || buf_size < 20){
  653. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, AV_RL32(buf+4));
  654. }
  655. if(frame_4cc == AV_RL32("cfrm")){
  656. int free_index=-1;
  657. const int data_size= buf_size - 20;
  658. const int id= AV_RL32(buf+12);
  659. const int whole_size= AV_RL32(buf+16);
  660. CFrameBuffer *cfrm;
  661. if (data_size < 0 || whole_size < 0){
  662. av_log(f->avctx, AV_LOG_ERROR, "sizes invalid\n");
  663. return AVERROR_INVALIDDATA;
  664. }
  665. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  666. if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  667. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
  668. }
  669. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  670. if(f->cfrm[i].id == id) break;
  671. if(f->cfrm[i].size == 0 ) free_index= i;
  672. }
  673. if(i>=CFRAME_BUFFER_COUNT){
  674. i= free_index;
  675. f->cfrm[i].id= id;
  676. }
  677. cfrm= &f->cfrm[i];
  678. if (data_size > UINT_MAX - cfrm->size - FF_INPUT_BUFFER_PADDING_SIZE)
  679. return AVERROR_INVALIDDATA;
  680. cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
  681. if(!cfrm->data){ //explicit check needed as memcpy below might not catch a NULL
  682. av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
  683. return -1;
  684. }
  685. memcpy(cfrm->data + cfrm->size, buf+20, data_size);
  686. cfrm->size += data_size;
  687. if(cfrm->size >= whole_size){
  688. buf= cfrm->data;
  689. frame_size= cfrm->size;
  690. if(id != avctx->frame_number){
  691. av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
  692. }
  693. cfrm->size= cfrm->id= 0;
  694. frame_4cc= AV_RL32("pfrm");
  695. }else
  696. return buf_size;
  697. }else{
  698. buf= buf + 12;
  699. frame_size= buf_size - 12;
  700. }
  701. temp= f->current_picture;
  702. f->current_picture= f->last_picture;
  703. f->last_picture= temp;
  704. p= &f->current_picture;
  705. avctx->coded_frame= p;
  706. avctx->flags |= CODEC_FLAG_EMU_EDGE; // alternatively we would have to use our own buffer management
  707. p->reference= 1;
  708. if (avctx->reget_buffer(avctx, p) < 0) {
  709. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  710. return -1;
  711. }
  712. if(frame_4cc == AV_RL32("ifr2")){
  713. p->pict_type= AV_PICTURE_TYPE_I;
  714. if(decode_i2_frame(f, buf-4, frame_size+4) < 0){
  715. av_log(f->avctx, AV_LOG_ERROR, "decode i2 frame failed\n");
  716. return -1;
  717. }
  718. }else if(frame_4cc == AV_RL32("ifrm")){
  719. p->pict_type= AV_PICTURE_TYPE_I;
  720. if(decode_i_frame(f, buf, frame_size) < 0){
  721. av_log(f->avctx, AV_LOG_ERROR, "decode i frame failed\n");
  722. return -1;
  723. }
  724. }else if(frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")){
  725. if(!f->last_picture.data[0]){
  726. f->last_picture.reference= 1;
  727. if(avctx->get_buffer(avctx, &f->last_picture) < 0){
  728. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  729. return -1;
  730. }
  731. }
  732. p->pict_type= AV_PICTURE_TYPE_P;
  733. if(decode_p_frame(f, buf, frame_size) < 0){
  734. av_log(f->avctx, AV_LOG_ERROR, "decode p frame failed\n");
  735. return -1;
  736. }
  737. }else if(frame_4cc == AV_RL32("snd_")){
  738. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
  739. }else{
  740. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
  741. }
  742. p->key_frame= p->pict_type == AV_PICTURE_TYPE_I;
  743. *picture= *p;
  744. *data_size = sizeof(AVPicture);
  745. emms_c();
  746. return buf_size;
  747. }
  748. static av_cold void common_init(AVCodecContext *avctx){
  749. FourXContext * const f = avctx->priv_data;
  750. dsputil_init(&f->dsp, avctx);
  751. f->avctx= avctx;
  752. }
  753. static av_cold int decode_init(AVCodecContext *avctx){
  754. FourXContext * const f = avctx->priv_data;
  755. if(avctx->extradata_size != 4 || !avctx->extradata) {
  756. av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
  757. return 1;
  758. }
  759. if((avctx->width % 16) || (avctx->height % 16)) {
  760. av_log(avctx, AV_LOG_ERROR, "unsupported width/height\n");
  761. return AVERROR_INVALIDDATA;
  762. }
  763. avcodec_get_frame_defaults(&f->current_picture);
  764. avcodec_get_frame_defaults(&f->last_picture);
  765. f->version= AV_RL32(avctx->extradata)>>16;
  766. common_init(avctx);
  767. init_vlcs(f);
  768. if(f->version>2) avctx->pix_fmt= PIX_FMT_RGB565;
  769. else avctx->pix_fmt= PIX_FMT_BGR555;
  770. return 0;
  771. }
  772. static av_cold int decode_end(AVCodecContext *avctx){
  773. FourXContext * const f = avctx->priv_data;
  774. int i;
  775. av_freep(&f->bitstream_buffer);
  776. f->bitstream_buffer_size=0;
  777. for(i=0; i<CFRAME_BUFFER_COUNT; i++){
  778. av_freep(&f->cfrm[i].data);
  779. f->cfrm[i].allocated_size= 0;
  780. }
  781. free_vlc(&f->pre_vlc);
  782. if(f->current_picture.data[0])
  783. avctx->release_buffer(avctx, &f->current_picture);
  784. if(f->last_picture.data[0])
  785. avctx->release_buffer(avctx, &f->last_picture);
  786. return 0;
  787. }
  788. AVCodec ff_fourxm_decoder = {
  789. "4xm",
  790. AVMEDIA_TYPE_VIDEO,
  791. CODEC_ID_4XM,
  792. sizeof(FourXContext),
  793. decode_init,
  794. NULL,
  795. decode_end,
  796. decode_frame,
  797. CODEC_CAP_DR1,
  798. .long_name = NULL_IF_CONFIG_SMALL("4X Movie"),
  799. };