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.

3813 lines
140KB

  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intmath.h"
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "dwt.h"
  24. #include "snow.h"
  25. #include "rangecoder.h"
  26. #include "mathops.h"
  27. #include "mpegvideo.h"
  28. #include "h263.h"
  29. #undef NDEBUG
  30. #include <assert.h>
  31. static const int8_t quant3bA[256]={
  32. 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  33. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  34. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  35. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  36. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  37. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  38. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  39. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  40. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  41. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  42. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  43. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  44. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  45. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  46. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  47. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  48. };
  49. static const uint8_t obmc32[1024]={
  50. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  51. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  52. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  53. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  54. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  55. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  56. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  57. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  58. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  59. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  60. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  61. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  62. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  63. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  64. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  65. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  66. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  67. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  68. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  69. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  70. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  71. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  72. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  73. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  74. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  75. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  76. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  77. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  78. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  79. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  80. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  81. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  82. //error:0.000020
  83. };
  84. static const uint8_t obmc16[256]={
  85. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  86. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  87. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  88. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  89. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  90. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  91. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  92. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  93. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  94. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  95. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  96. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  97. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  98. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  99. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  100. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  101. //error:0.000015
  102. };
  103. //linear *64
  104. static const uint8_t obmc8[64]={
  105. 4, 12, 20, 28, 28, 20, 12, 4,
  106. 12, 36, 60, 84, 84, 60, 36, 12,
  107. 20, 60,100,140,140,100, 60, 20,
  108. 28, 84,140,196,196,140, 84, 28,
  109. 28, 84,140,196,196,140, 84, 28,
  110. 20, 60,100,140,140,100, 60, 20,
  111. 12, 36, 60, 84, 84, 60, 36, 12,
  112. 4, 12, 20, 28, 28, 20, 12, 4,
  113. //error:0.000000
  114. };
  115. //linear *64
  116. static const uint8_t obmc4[16]={
  117. 16, 48, 48, 16,
  118. 48,144,144, 48,
  119. 48,144,144, 48,
  120. 16, 48, 48, 16,
  121. //error:0.000000
  122. };
  123. static const uint8_t * const obmc_tab[4]={
  124. obmc32, obmc16, obmc8, obmc4
  125. };
  126. static int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  127. typedef struct BlockNode{
  128. int16_t mx;
  129. int16_t my;
  130. uint8_t ref;
  131. uint8_t color[3];
  132. uint8_t type;
  133. //#define TYPE_SPLIT 1
  134. #define BLOCK_INTRA 1
  135. #define BLOCK_OPT 2
  136. //#define TYPE_NOCOLOR 4
  137. uint8_t level; //FIXME merge into type?
  138. }BlockNode;
  139. static const BlockNode null_block= { //FIXME add border maybe
  140. .color= {128,128,128},
  141. .mx= 0,
  142. .my= 0,
  143. .ref= 0,
  144. .type= 0,
  145. .level= 0,
  146. };
  147. #define LOG2_MB_SIZE 4
  148. #define MB_SIZE (1<<LOG2_MB_SIZE)
  149. #define ENCODER_EXTRA_BITS 4
  150. #define HTAPS_MAX 8
  151. typedef struct x_and_coeff{
  152. int16_t x;
  153. uint16_t coeff;
  154. } x_and_coeff;
  155. typedef struct SubBand{
  156. int level;
  157. int stride;
  158. int width;
  159. int height;
  160. int qlog; ///< log(qscale)/log[2^(1/6)]
  161. DWTELEM *buf;
  162. IDWTELEM *ibuf;
  163. int buf_x_offset;
  164. int buf_y_offset;
  165. int stride_line; ///< Stride measured in lines, not pixels.
  166. x_and_coeff * x_coeff;
  167. struct SubBand *parent;
  168. uint8_t state[/*7*2*/ 7 + 512][32];
  169. }SubBand;
  170. typedef struct Plane{
  171. int width;
  172. int height;
  173. SubBand band[MAX_DECOMPOSITIONS][4];
  174. int htaps;
  175. int8_t hcoeff[HTAPS_MAX/2];
  176. int diag_mc;
  177. int fast_mc;
  178. int last_htaps;
  179. int8_t last_hcoeff[HTAPS_MAX/2];
  180. int last_diag_mc;
  181. }Plane;
  182. typedef struct SnowContext{
  183. AVCodecContext *avctx;
  184. RangeCoder c;
  185. DSPContext dsp;
  186. DWTContext dwt;
  187. AVFrame new_picture;
  188. AVFrame input_picture; ///< new_picture with the internal linesizes
  189. AVFrame current_picture;
  190. AVFrame last_picture[MAX_REF_FRAMES];
  191. uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
  192. AVFrame mconly_picture;
  193. // uint8_t q_context[16];
  194. uint8_t header_state[32];
  195. uint8_t block_state[128 + 32*128];
  196. int keyframe;
  197. int always_reset;
  198. int version;
  199. int spatial_decomposition_type;
  200. int last_spatial_decomposition_type;
  201. int temporal_decomposition_type;
  202. int spatial_decomposition_count;
  203. int last_spatial_decomposition_count;
  204. int temporal_decomposition_count;
  205. int max_ref_frames;
  206. int ref_frames;
  207. int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
  208. uint32_t *ref_scores[MAX_REF_FRAMES];
  209. DWTELEM *spatial_dwt_buffer;
  210. IDWTELEM *spatial_idwt_buffer;
  211. int colorspace_type;
  212. int chroma_h_shift;
  213. int chroma_v_shift;
  214. int spatial_scalability;
  215. int qlog;
  216. int last_qlog;
  217. int lambda;
  218. int lambda2;
  219. int pass1_rc;
  220. int mv_scale;
  221. int last_mv_scale;
  222. int qbias;
  223. int last_qbias;
  224. #define QBIAS_SHIFT 3
  225. int b_width;
  226. int b_height;
  227. int block_max_depth;
  228. int last_block_max_depth;
  229. Plane plane[MAX_PLANES];
  230. BlockNode *block;
  231. #define ME_CACHE_SIZE 1024
  232. int me_cache[ME_CACHE_SIZE];
  233. int me_cache_generation;
  234. slice_buffer sb;
  235. MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
  236. uint8_t *scratchbuf;
  237. }SnowContext;
  238. #ifdef __sgi
  239. // Avoid a name clash on SGI IRIX
  240. #undef qexp
  241. #endif
  242. #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
  243. static uint8_t qexp[QROOT];
  244. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  245. int i;
  246. if(v){
  247. const int a= FFABS(v);
  248. const int e= av_log2(a);
  249. const int el= FFMIN(e, 10);
  250. put_rac(c, state+0, 0);
  251. for(i=0; i<el; i++){
  252. put_rac(c, state+1+i, 1); //1..10
  253. }
  254. for(; i<e; i++){
  255. put_rac(c, state+1+9, 1); //1..10
  256. }
  257. put_rac(c, state+1+FFMIN(i,9), 0);
  258. for(i=e-1; i>=el; i--){
  259. put_rac(c, state+22+9, (a>>i)&1); //22..31
  260. }
  261. for(; i>=0; i--){
  262. put_rac(c, state+22+i, (a>>i)&1); //22..31
  263. }
  264. if(is_signed)
  265. put_rac(c, state+11 + el, v < 0); //11..21
  266. }else{
  267. put_rac(c, state+0, 1);
  268. }
  269. }
  270. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  271. if(get_rac(c, state+0))
  272. return 0;
  273. else{
  274. int i, e, a;
  275. e= 0;
  276. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  277. e++;
  278. }
  279. a= 1;
  280. for(i=e-1; i>=0; i--){
  281. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  282. }
  283. e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21
  284. return (a^e)-e;
  285. }
  286. }
  287. static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
  288. int i;
  289. int r= log2>=0 ? 1<<log2 : 1;
  290. assert(v>=0);
  291. assert(log2>=-4);
  292. while(v >= r){
  293. put_rac(c, state+4+log2, 1);
  294. v -= r;
  295. log2++;
  296. if(log2>0) r+=r;
  297. }
  298. put_rac(c, state+4+log2, 0);
  299. for(i=log2-1; i>=0; i--){
  300. put_rac(c, state+31-i, (v>>i)&1);
  301. }
  302. }
  303. static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
  304. int i;
  305. int r= log2>=0 ? 1<<log2 : 1;
  306. int v=0;
  307. assert(log2>=-4);
  308. while(get_rac(c, state+4+log2)){
  309. v+= r;
  310. log2++;
  311. if(log2>0) r+=r;
  312. }
  313. for(i=log2-1; i>=0; i--){
  314. v+= get_rac(c, state+31-i)<<i;
  315. }
  316. return v;
  317. }
  318. static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
  319. const int w= b->width;
  320. const int h= b->height;
  321. int x,y;
  322. int run, runs;
  323. x_and_coeff *xc= b->x_coeff;
  324. x_and_coeff *prev_xc= NULL;
  325. x_and_coeff *prev2_xc= xc;
  326. x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
  327. x_and_coeff *prev_parent_xc= parent_xc;
  328. runs= get_symbol2(&s->c, b->state[30], 0);
  329. if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
  330. else run= INT_MAX;
  331. for(y=0; y<h; y++){
  332. int v=0;
  333. int lt=0, t=0, rt=0;
  334. if(y && prev_xc->x == 0){
  335. rt= prev_xc->coeff;
  336. }
  337. for(x=0; x<w; x++){
  338. int p=0;
  339. const int l= v;
  340. lt= t; t= rt;
  341. if(y){
  342. if(prev_xc->x <= x)
  343. prev_xc++;
  344. if(prev_xc->x == x + 1)
  345. rt= prev_xc->coeff;
  346. else
  347. rt=0;
  348. }
  349. if(parent_xc){
  350. if(x>>1 > parent_xc->x){
  351. parent_xc++;
  352. }
  353. if(x>>1 == parent_xc->x){
  354. p= parent_xc->coeff;
  355. }
  356. }
  357. if(/*ll|*/l|lt|t|rt|p){
  358. int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
  359. v=get_rac(&s->c, &b->state[0][context]);
  360. if(v){
  361. v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
  362. v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
  363. xc->x=x;
  364. (xc++)->coeff= v;
  365. }
  366. }else{
  367. if(!run){
  368. if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
  369. else run= INT_MAX;
  370. v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
  371. v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
  372. xc->x=x;
  373. (xc++)->coeff= v;
  374. }else{
  375. int max_run;
  376. run--;
  377. v=0;
  378. if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
  379. else max_run= FFMIN(run, w-x-1);
  380. if(parent_xc)
  381. max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
  382. x+= max_run;
  383. run-= max_run;
  384. }
  385. }
  386. }
  387. (xc++)->x= w+1; //end marker
  388. prev_xc= prev2_xc;
  389. prev2_xc= xc;
  390. if(parent_xc){
  391. if(y&1){
  392. while(parent_xc->x != parent->width+1)
  393. parent_xc++;
  394. parent_xc++;
  395. prev_parent_xc= parent_xc;
  396. }else{
  397. parent_xc= prev_parent_xc;
  398. }
  399. }
  400. }
  401. (xc++)->x= w+1; //end marker
  402. }
  403. static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
  404. const int w= b->width;
  405. int y;
  406. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  407. int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  408. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  409. int new_index = 0;
  410. if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
  411. qadd= 0;
  412. qmul= 1<<QEXPSHIFT;
  413. }
  414. /* If we are on the second or later slice, restore our index. */
  415. if (start_y != 0)
  416. new_index = save_state[0];
  417. for(y=start_y; y<h; y++){
  418. int x = 0;
  419. int v;
  420. IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
  421. memset(line, 0, b->width*sizeof(IDWTELEM));
  422. v = b->x_coeff[new_index].coeff;
  423. x = b->x_coeff[new_index++].x;
  424. while(x < w){
  425. register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
  426. register int u= -(v&1);
  427. line[x] = (t^u) - u;
  428. v = b->x_coeff[new_index].coeff;
  429. x = b->x_coeff[new_index++].x;
  430. }
  431. }
  432. /* Save our variables for the next slice. */
  433. save_state[0] = new_index;
  434. return;
  435. }
  436. static void reset_contexts(SnowContext *s){ //FIXME better initial contexts
  437. int plane_index, level, orientation;
  438. for(plane_index=0; plane_index<3; plane_index++){
  439. for(level=0; level<MAX_DECOMPOSITIONS; level++){
  440. for(orientation=level ? 1:0; orientation<4; orientation++){
  441. memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
  442. }
  443. }
  444. }
  445. memset(s->header_state, MID_STATE, sizeof(s->header_state));
  446. memset(s->block_state, MID_STATE, sizeof(s->block_state));
  447. }
  448. static int alloc_blocks(SnowContext *s){
  449. int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
  450. int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
  451. s->b_width = w;
  452. s->b_height= h;
  453. av_free(s->block);
  454. s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
  455. return 0;
  456. }
  457. static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
  458. const int w= s->b_width << s->block_max_depth;
  459. const int rem_depth= s->block_max_depth - level;
  460. const int index= (x + y*w) << rem_depth;
  461. const int block_w= 1<<rem_depth;
  462. BlockNode block;
  463. int i,j;
  464. block.color[0]= l;
  465. block.color[1]= cb;
  466. block.color[2]= cr;
  467. block.mx= mx;
  468. block.my= my;
  469. block.ref= ref;
  470. block.type= type;
  471. block.level= level;
  472. for(j=0; j<block_w; j++){
  473. for(i=0; i<block_w; i++){
  474. s->block[index + i + j*w]= block;
  475. }
  476. }
  477. }
  478. static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  479. const int offset[3]= {
  480. y*c-> stride + x,
  481. ((y*c->uvstride + x)>>1),
  482. ((y*c->uvstride + x)>>1),
  483. };
  484. int i;
  485. for(i=0; i<3; i++){
  486. c->src[0][i]= src [i];
  487. c->ref[0][i]= ref [i] + offset[i];
  488. }
  489. assert(!ref_index);
  490. }
  491. static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
  492. const BlockNode *left, const BlockNode *top, const BlockNode *tr){
  493. if(s->ref_frames == 1){
  494. *mx = mid_pred(left->mx, top->mx, tr->mx);
  495. *my = mid_pred(left->my, top->my, tr->my);
  496. }else{
  497. const int *scale = scale_mv_ref[ref];
  498. *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
  499. (top ->mx * scale[top ->ref] + 128) >>8,
  500. (tr ->mx * scale[tr ->ref] + 128) >>8);
  501. *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
  502. (top ->my * scale[top ->ref] + 128) >>8,
  503. (tr ->my * scale[tr ->ref] + 128) >>8);
  504. }
  505. }
  506. static av_always_inline int same_block(BlockNode *a, BlockNode *b){
  507. if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
  508. return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
  509. }else{
  510. return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
  511. }
  512. }
  513. static void decode_q_branch(SnowContext *s, int level, int x, int y){
  514. const int w= s->b_width << s->block_max_depth;
  515. const int rem_depth= s->block_max_depth - level;
  516. const int index= (x + y*w) << rem_depth;
  517. int trx= (x+1)<<rem_depth;
  518. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  519. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  520. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  521. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  522. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  523. if(s->keyframe){
  524. set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
  525. return;
  526. }
  527. if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
  528. int type, mx, my;
  529. int l = left->color[0];
  530. int cb= left->color[1];
  531. int cr= left->color[2];
  532. int ref = 0;
  533. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  534. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
  535. int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
  536. type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  537. if(type){
  538. pred_mv(s, &mx, &my, 0, left, top, tr);
  539. l += get_symbol(&s->c, &s->block_state[32], 1);
  540. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  541. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  542. }else{
  543. if(s->ref_frames > 1)
  544. ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  545. pred_mv(s, &mx, &my, ref, left, top, tr);
  546. mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  547. my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  548. }
  549. set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
  550. }else{
  551. decode_q_branch(s, level+1, 2*x+0, 2*y+0);
  552. decode_q_branch(s, level+1, 2*x+1, 2*y+0);
  553. decode_q_branch(s, level+1, 2*x+0, 2*y+1);
  554. decode_q_branch(s, level+1, 2*x+1, 2*y+1);
  555. }
  556. }
  557. static void decode_blocks(SnowContext *s){
  558. int x, y;
  559. int w= s->b_width;
  560. int h= s->b_height;
  561. for(y=0; y<h; y++){
  562. for(x=0; x<w; x++){
  563. decode_q_branch(s, 0, x, y);
  564. }
  565. }
  566. }
  567. static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, int stride, int b_w, int b_h, int dx, int dy){
  568. static const uint8_t weight[64]={
  569. 8,7,6,5,4,3,2,1,
  570. 7,7,0,0,0,0,0,1,
  571. 6,0,6,0,0,0,2,0,
  572. 5,0,0,5,0,3,0,0,
  573. 4,0,0,0,4,0,0,0,
  574. 3,0,0,5,0,3,0,0,
  575. 2,0,6,0,0,0,2,0,
  576. 1,7,0,0,0,0,0,1,
  577. };
  578. static const uint8_t brane[256]={
  579. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
  580. 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
  581. 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
  582. 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
  583. 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
  584. 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
  585. 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
  586. 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
  587. 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
  588. 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
  589. 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
  590. 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
  591. 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
  592. 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
  593. 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
  594. 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
  595. };
  596. static const uint8_t needs[16]={
  597. 0,1,0,0,
  598. 2,4,2,0,
  599. 0,1,0,0,
  600. 15
  601. };
  602. int x, y, b, r, l;
  603. int16_t tmpIt [64*(32+HTAPS_MAX)];
  604. uint8_t tmp2t[3][stride*(32+HTAPS_MAX)];
  605. int16_t *tmpI= tmpIt;
  606. uint8_t *tmp2= tmp2t[0];
  607. const uint8_t *hpel[11];
  608. assert(dx<16 && dy<16);
  609. r= brane[dx + 16*dy]&15;
  610. l= brane[dx + 16*dy]>>4;
  611. b= needs[l] | needs[r];
  612. if(p && !p->diag_mc)
  613. b= 15;
  614. if(b&5){
  615. for(y=0; y < b_h+HTAPS_MAX-1; y++){
  616. for(x=0; x < b_w; x++){
  617. int a_1=src[x + HTAPS_MAX/2-4];
  618. int a0= src[x + HTAPS_MAX/2-3];
  619. int a1= src[x + HTAPS_MAX/2-2];
  620. int a2= src[x + HTAPS_MAX/2-1];
  621. int a3= src[x + HTAPS_MAX/2+0];
  622. int a4= src[x + HTAPS_MAX/2+1];
  623. int a5= src[x + HTAPS_MAX/2+2];
  624. int a6= src[x + HTAPS_MAX/2+3];
  625. int am=0;
  626. if(!p || p->fast_mc){
  627. am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  628. tmpI[x]= am;
  629. am= (am+16)>>5;
  630. }else{
  631. am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
  632. tmpI[x]= am;
  633. am= (am+32)>>6;
  634. }
  635. if(am&(~255)) am= ~(am>>31);
  636. tmp2[x]= am;
  637. }
  638. tmpI+= 64;
  639. tmp2+= stride;
  640. src += stride;
  641. }
  642. src -= stride*y;
  643. }
  644. src += HTAPS_MAX/2 - 1;
  645. tmp2= tmp2t[1];
  646. if(b&2){
  647. for(y=0; y < b_h; y++){
  648. for(x=0; x < b_w+1; x++){
  649. int a_1=src[x + (HTAPS_MAX/2-4)*stride];
  650. int a0= src[x + (HTAPS_MAX/2-3)*stride];
  651. int a1= src[x + (HTAPS_MAX/2-2)*stride];
  652. int a2= src[x + (HTAPS_MAX/2-1)*stride];
  653. int a3= src[x + (HTAPS_MAX/2+0)*stride];
  654. int a4= src[x + (HTAPS_MAX/2+1)*stride];
  655. int a5= src[x + (HTAPS_MAX/2+2)*stride];
  656. int a6= src[x + (HTAPS_MAX/2+3)*stride];
  657. int am=0;
  658. if(!p || p->fast_mc)
  659. am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
  660. else
  661. am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
  662. if(am&(~255)) am= ~(am>>31);
  663. tmp2[x]= am;
  664. }
  665. src += stride;
  666. tmp2+= stride;
  667. }
  668. src -= stride*y;
  669. }
  670. src += stride*(HTAPS_MAX/2 - 1);
  671. tmp2= tmp2t[2];
  672. tmpI= tmpIt;
  673. if(b&4){
  674. for(y=0; y < b_h; y++){
  675. for(x=0; x < b_w; x++){
  676. int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
  677. int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
  678. int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
  679. int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
  680. int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
  681. int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
  682. int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
  683. int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
  684. int am=0;
  685. if(!p || p->fast_mc)
  686. am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
  687. else
  688. am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
  689. if(am&(~255)) am= ~(am>>31);
  690. tmp2[x]= am;
  691. }
  692. tmpI+= 64;
  693. tmp2+= stride;
  694. }
  695. }
  696. hpel[ 0]= src;
  697. hpel[ 1]= tmp2t[0] + stride*(HTAPS_MAX/2-1);
  698. hpel[ 2]= src + 1;
  699. hpel[ 4]= tmp2t[1];
  700. hpel[ 5]= tmp2t[2];
  701. hpel[ 6]= tmp2t[1] + 1;
  702. hpel[ 8]= src + stride;
  703. hpel[ 9]= hpel[1] + stride;
  704. hpel[10]= hpel[8] + 1;
  705. if(b==15){
  706. const uint8_t *src1= hpel[dx/8 + dy/8*4 ];
  707. const uint8_t *src2= hpel[dx/8 + dy/8*4+1];
  708. const uint8_t *src3= hpel[dx/8 + dy/8*4+4];
  709. const uint8_t *src4= hpel[dx/8 + dy/8*4+5];
  710. dx&=7;
  711. dy&=7;
  712. for(y=0; y < b_h; y++){
  713. for(x=0; x < b_w; x++){
  714. dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
  715. (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
  716. }
  717. src1+=stride;
  718. src2+=stride;
  719. src3+=stride;
  720. src4+=stride;
  721. dst +=stride;
  722. }
  723. }else{
  724. const uint8_t *src1= hpel[l];
  725. const uint8_t *src2= hpel[r];
  726. int a= weight[((dx&7) + (8*(dy&7)))];
  727. int b= 8-a;
  728. for(y=0; y < b_h; y++){
  729. for(x=0; x < b_w; x++){
  730. dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
  731. }
  732. src1+=stride;
  733. src2+=stride;
  734. dst +=stride;
  735. }
  736. }
  737. }
  738. #define mca(dx,dy,b_w)\
  739. static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, int stride, int h){\
  740. assert(h==b_w);\
  741. mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
  742. }
  743. mca( 0, 0,16)
  744. mca( 8, 0,16)
  745. mca( 0, 8,16)
  746. mca( 8, 8,16)
  747. mca( 0, 0,8)
  748. mca( 8, 0,8)
  749. mca( 0, 8,8)
  750. mca( 8, 8,8)
  751. static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
  752. if(block->type & BLOCK_INTRA){
  753. int x, y;
  754. const int color = block->color[plane_index];
  755. const int color4= color*0x01010101;
  756. if(b_w==32){
  757. for(y=0; y < b_h; y++){
  758. *(uint32_t*)&dst[0 + y*stride]= color4;
  759. *(uint32_t*)&dst[4 + y*stride]= color4;
  760. *(uint32_t*)&dst[8 + y*stride]= color4;
  761. *(uint32_t*)&dst[12+ y*stride]= color4;
  762. *(uint32_t*)&dst[16+ y*stride]= color4;
  763. *(uint32_t*)&dst[20+ y*stride]= color4;
  764. *(uint32_t*)&dst[24+ y*stride]= color4;
  765. *(uint32_t*)&dst[28+ y*stride]= color4;
  766. }
  767. }else if(b_w==16){
  768. for(y=0; y < b_h; y++){
  769. *(uint32_t*)&dst[0 + y*stride]= color4;
  770. *(uint32_t*)&dst[4 + y*stride]= color4;
  771. *(uint32_t*)&dst[8 + y*stride]= color4;
  772. *(uint32_t*)&dst[12+ y*stride]= color4;
  773. }
  774. }else if(b_w==8){
  775. for(y=0; y < b_h; y++){
  776. *(uint32_t*)&dst[0 + y*stride]= color4;
  777. *(uint32_t*)&dst[4 + y*stride]= color4;
  778. }
  779. }else if(b_w==4){
  780. for(y=0; y < b_h; y++){
  781. *(uint32_t*)&dst[0 + y*stride]= color4;
  782. }
  783. }else{
  784. for(y=0; y < b_h; y++){
  785. for(x=0; x < b_w; x++){
  786. dst[x + y*stride]= color;
  787. }
  788. }
  789. }
  790. }else{
  791. uint8_t *src= s->last_picture[block->ref].data[plane_index];
  792. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  793. int mx= block->mx*scale;
  794. int my= block->my*scale;
  795. const int dx= mx&15;
  796. const int dy= my&15;
  797. const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  798. sx += (mx>>4) - (HTAPS_MAX/2-1);
  799. sy += (my>>4) - (HTAPS_MAX/2-1);
  800. src += sx + sy*stride;
  801. if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
  802. || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
  803. s->dsp.emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
  804. src= tmp + MB_SIZE;
  805. }
  806. // assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
  807. // assert(!(b_w&(b_w-1)));
  808. assert(b_w>1 && b_h>1);
  809. assert((tab_index>=0 && tab_index<4) || b_w==32);
  810. if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
  811. mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
  812. else if(b_w==32){
  813. int y;
  814. for(y=0; y<b_h; y+=16){
  815. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
  816. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
  817. }
  818. }else if(b_w==b_h)
  819. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
  820. else if(b_w==2*b_h){
  821. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
  822. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
  823. }else{
  824. assert(2*b_w==b_h);
  825. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
  826. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
  827. }
  828. }
  829. }
  830. void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
  831. int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
  832. int y, x;
  833. IDWTELEM * dst;
  834. for(y=0; y<b_h; y++){
  835. //FIXME ugly misuse of obmc_stride
  836. const uint8_t *obmc1= obmc + y*obmc_stride;
  837. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  838. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  839. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  840. dst = slice_buffer_get_line(sb, src_y + y);
  841. for(x=0; x<b_w; x++){
  842. int v= obmc1[x] * block[3][x + y*src_stride]
  843. +obmc2[x] * block[2][x + y*src_stride]
  844. +obmc3[x] * block[1][x + y*src_stride]
  845. +obmc4[x] * block[0][x + y*src_stride];
  846. v <<= 8 - LOG2_OBMC_MAX;
  847. if(FRAC_BITS != 8){
  848. v >>= 8 - FRAC_BITS;
  849. }
  850. if(add){
  851. v += dst[x + src_x];
  852. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  853. if(v&(~255)) v= ~(v>>31);
  854. dst8[x + y*src_stride] = v;
  855. }else{
  856. dst[x + src_x] -= v;
  857. }
  858. }
  859. }
  860. }
  861. //FIXME name cleanup (b_w, block_w, b_width stuff)
  862. static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){
  863. const int b_width = s->b_width << s->block_max_depth;
  864. const int b_height= s->b_height << s->block_max_depth;
  865. const int b_stride= b_width;
  866. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  867. BlockNode *rt= lt+1;
  868. BlockNode *lb= lt+b_stride;
  869. BlockNode *rb= lb+1;
  870. uint8_t *block[4];
  871. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  872. uint8_t *tmp = s->scratchbuf;
  873. uint8_t *ptmp;
  874. int x,y;
  875. if(b_x<0){
  876. lt= rt;
  877. lb= rb;
  878. }else if(b_x + 1 >= b_width){
  879. rt= lt;
  880. rb= lb;
  881. }
  882. if(b_y<0){
  883. lt= lb;
  884. rt= rb;
  885. }else if(b_y + 1 >= b_height){
  886. lb= lt;
  887. rb= rt;
  888. }
  889. if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  890. obmc -= src_x;
  891. b_w += src_x;
  892. if(!sliced && !offset_dst)
  893. dst -= src_x;
  894. src_x=0;
  895. }else if(src_x + b_w > w){
  896. b_w = w - src_x;
  897. }
  898. if(src_y<0){
  899. obmc -= src_y*obmc_stride;
  900. b_h += src_y;
  901. if(!sliced && !offset_dst)
  902. dst -= src_y*dst_stride;
  903. src_y=0;
  904. }else if(src_y + b_h> h){
  905. b_h = h - src_y;
  906. }
  907. if(b_w<=0 || b_h<=0) return;
  908. assert(src_stride > 2*MB_SIZE + 5);
  909. if(!sliced && offset_dst)
  910. dst += src_x + src_y*dst_stride;
  911. dst8+= src_x + src_y*src_stride;
  912. // src += src_x + src_y*src_stride;
  913. ptmp= tmp + 3*tmp_step;
  914. block[0]= ptmp;
  915. ptmp+=tmp_step;
  916. pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  917. if(same_block(lt, rt)){
  918. block[1]= block[0];
  919. }else{
  920. block[1]= ptmp;
  921. ptmp+=tmp_step;
  922. pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  923. }
  924. if(same_block(lt, lb)){
  925. block[2]= block[0];
  926. }else if(same_block(rt, lb)){
  927. block[2]= block[1];
  928. }else{
  929. block[2]= ptmp;
  930. ptmp+=tmp_step;
  931. pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  932. }
  933. if(same_block(lt, rb) ){
  934. block[3]= block[0];
  935. }else if(same_block(rt, rb)){
  936. block[3]= block[1];
  937. }else if(same_block(lb, rb)){
  938. block[3]= block[2];
  939. }else{
  940. block[3]= ptmp;
  941. pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  942. }
  943. if(sliced){
  944. s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  945. }else{
  946. for(y=0; y<b_h; y++){
  947. //FIXME ugly misuse of obmc_stride
  948. const uint8_t *obmc1= obmc + y*obmc_stride;
  949. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  950. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  951. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  952. for(x=0; x<b_w; x++){
  953. int v= obmc1[x] * block[3][x + y*src_stride]
  954. +obmc2[x] * block[2][x + y*src_stride]
  955. +obmc3[x] * block[1][x + y*src_stride]
  956. +obmc4[x] * block[0][x + y*src_stride];
  957. v <<= 8 - LOG2_OBMC_MAX;
  958. if(FRAC_BITS != 8){
  959. v >>= 8 - FRAC_BITS;
  960. }
  961. if(add){
  962. v += dst[x + y*dst_stride];
  963. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  964. if(v&(~255)) v= ~(v>>31);
  965. dst8[x + y*src_stride] = v;
  966. }else{
  967. dst[x + y*dst_stride] -= v;
  968. }
  969. }
  970. }
  971. }
  972. }
  973. static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  974. Plane *p= &s->plane[plane_index];
  975. const int mb_w= s->b_width << s->block_max_depth;
  976. const int mb_h= s->b_height << s->block_max_depth;
  977. int x, y, mb_x;
  978. int block_size = MB_SIZE >> s->block_max_depth;
  979. int block_w = plane_index ? block_size/2 : block_size;
  980. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  981. int obmc_stride= plane_index ? block_size : 2*block_size;
  982. int ref_stride= s->current_picture.linesize[plane_index];
  983. uint8_t *dst8= s->current_picture.data[plane_index];
  984. int w= p->width;
  985. int h= p->height;
  986. if(s->keyframe || (s->avctx->debug&512)){
  987. if(mb_y==mb_h)
  988. return;
  989. if(add){
  990. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  991. // DWTELEM * line = slice_buffer_get_line(sb, y);
  992. IDWTELEM * line = sb->line[y];
  993. for(x=0; x<w; x++){
  994. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  995. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  996. v >>= FRAC_BITS;
  997. if(v&(~255)) v= ~(v>>31);
  998. dst8[x + y*ref_stride]= v;
  999. }
  1000. }
  1001. }else{
  1002. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1003. // DWTELEM * line = slice_buffer_get_line(sb, y);
  1004. IDWTELEM * line = sb->line[y];
  1005. for(x=0; x<w; x++){
  1006. line[x] -= 128 << FRAC_BITS;
  1007. // buf[x + y*w]-= 128<<FRAC_BITS;
  1008. }
  1009. }
  1010. }
  1011. return;
  1012. }
  1013. for(mb_x=0; mb_x<=mb_w; mb_x++){
  1014. add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  1015. block_w*mb_x - block_w/2,
  1016. block_w*mb_y - block_w/2,
  1017. block_w, block_w,
  1018. w, h,
  1019. w, ref_stride, obmc_stride,
  1020. mb_x - 1, mb_y - 1,
  1021. add, 0, plane_index);
  1022. }
  1023. }
  1024. static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  1025. Plane *p= &s->plane[plane_index];
  1026. const int mb_w= s->b_width << s->block_max_depth;
  1027. const int mb_h= s->b_height << s->block_max_depth;
  1028. int x, y, mb_x;
  1029. int block_size = MB_SIZE >> s->block_max_depth;
  1030. int block_w = plane_index ? block_size/2 : block_size;
  1031. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  1032. const int obmc_stride= plane_index ? block_size : 2*block_size;
  1033. int ref_stride= s->current_picture.linesize[plane_index];
  1034. uint8_t *dst8= s->current_picture.data[plane_index];
  1035. int w= p->width;
  1036. int h= p->height;
  1037. if(s->keyframe || (s->avctx->debug&512)){
  1038. if(mb_y==mb_h)
  1039. return;
  1040. if(add){
  1041. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1042. for(x=0; x<w; x++){
  1043. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  1044. v >>= FRAC_BITS;
  1045. if(v&(~255)) v= ~(v>>31);
  1046. dst8[x + y*ref_stride]= v;
  1047. }
  1048. }
  1049. }else{
  1050. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1051. for(x=0; x<w; x++){
  1052. buf[x + y*w]-= 128<<FRAC_BITS;
  1053. }
  1054. }
  1055. }
  1056. return;
  1057. }
  1058. for(mb_x=0; mb_x<=mb_w; mb_x++){
  1059. add_yblock(s, 0, NULL, buf, dst8, obmc,
  1060. block_w*mb_x - block_w/2,
  1061. block_w*mb_y - block_w/2,
  1062. block_w, block_w,
  1063. w, h,
  1064. w, ref_stride, obmc_stride,
  1065. mb_x - 1, mb_y - 1,
  1066. add, 1, plane_index);
  1067. }
  1068. }
  1069. static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
  1070. const int mb_h= s->b_height << s->block_max_depth;
  1071. int mb_y;
  1072. for(mb_y=0; mb_y<=mb_h; mb_y++)
  1073. predict_slice(s, buf, plane_index, add, mb_y);
  1074. }
  1075. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
  1076. const int w= b->width;
  1077. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1078. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1079. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1080. int x,y;
  1081. if(s->qlog == LOSSLESS_QLOG) return;
  1082. for(y=start_y; y<end_y; y++){
  1083. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  1084. IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  1085. for(x=0; x<w; x++){
  1086. int i= line[x];
  1087. if(i<0){
  1088. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  1089. }else if(i>0){
  1090. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  1091. }
  1092. }
  1093. }
  1094. }
  1095. static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){
  1096. const int w= b->width;
  1097. int x,y;
  1098. IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
  1099. IDWTELEM * prev;
  1100. if (start_y != 0)
  1101. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  1102. for(y=start_y; y<end_y; y++){
  1103. prev = line;
  1104. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  1105. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  1106. for(x=0; x<w; x++){
  1107. if(x){
  1108. if(use_median){
  1109. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  1110. else line[x] += line[x - 1];
  1111. }else{
  1112. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  1113. else line[x] += line[x - 1];
  1114. }
  1115. }else{
  1116. if(y) line[x] += prev[x];
  1117. }
  1118. }
  1119. }
  1120. }
  1121. static void decode_qlogs(SnowContext *s){
  1122. int plane_index, level, orientation;
  1123. for(plane_index=0; plane_index<3; plane_index++){
  1124. for(level=0; level<s->spatial_decomposition_count; level++){
  1125. for(orientation=level ? 1:0; orientation<4; orientation++){
  1126. int q;
  1127. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  1128. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  1129. else q= get_symbol(&s->c, s->header_state, 1);
  1130. s->plane[plane_index].band[level][orientation].qlog= q;
  1131. }
  1132. }
  1133. }
  1134. }
  1135. #define GET_S(dst, check) \
  1136. tmp= get_symbol(&s->c, s->header_state, 0);\
  1137. if(!(check)){\
  1138. av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
  1139. return -1;\
  1140. }\
  1141. dst= tmp;
  1142. static int decode_header(SnowContext *s){
  1143. int plane_index, tmp;
  1144. uint8_t kstate[32];
  1145. memset(kstate, MID_STATE, sizeof(kstate));
  1146. s->keyframe= get_rac(&s->c, kstate);
  1147. if(s->keyframe || s->always_reset){
  1148. reset_contexts(s);
  1149. s->spatial_decomposition_type=
  1150. s->qlog=
  1151. s->qbias=
  1152. s->mv_scale=
  1153. s->block_max_depth= 0;
  1154. }
  1155. if(s->keyframe){
  1156. GET_S(s->version, tmp <= 0U)
  1157. s->always_reset= get_rac(&s->c, s->header_state);
  1158. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  1159. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  1160. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  1161. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  1162. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  1163. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  1164. s->spatial_scalability= get_rac(&s->c, s->header_state);
  1165. // s->rate_scalability= get_rac(&s->c, s->header_state);
  1166. GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
  1167. s->max_ref_frames++;
  1168. decode_qlogs(s);
  1169. }
  1170. if(!s->keyframe){
  1171. if(get_rac(&s->c, s->header_state)){
  1172. for(plane_index=0; plane_index<2; plane_index++){
  1173. int htaps, i, sum=0;
  1174. Plane *p= &s->plane[plane_index];
  1175. p->diag_mc= get_rac(&s->c, s->header_state);
  1176. htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
  1177. if((unsigned)htaps > HTAPS_MAX || htaps==0)
  1178. return -1;
  1179. p->htaps= htaps;
  1180. for(i= htaps/2; i; i--){
  1181. p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
  1182. sum += p->hcoeff[i];
  1183. }
  1184. p->hcoeff[0]= 32-sum;
  1185. }
  1186. s->plane[2].diag_mc= s->plane[1].diag_mc;
  1187. s->plane[2].htaps = s->plane[1].htaps;
  1188. memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
  1189. }
  1190. if(get_rac(&s->c, s->header_state)){
  1191. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  1192. decode_qlogs(s);
  1193. }
  1194. }
  1195. s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
  1196. if(s->spatial_decomposition_type > 1U){
  1197. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  1198. return -1;
  1199. }
  1200. if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
  1201. s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){
  1202. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size", s->spatial_decomposition_count);
  1203. return -1;
  1204. }
  1205. s->qlog += get_symbol(&s->c, s->header_state, 1);
  1206. s->mv_scale += get_symbol(&s->c, s->header_state, 1);
  1207. s->qbias += get_symbol(&s->c, s->header_state, 1);
  1208. s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
  1209. if(s->block_max_depth > 1 || s->block_max_depth < 0){
  1210. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
  1211. s->block_max_depth= 0;
  1212. return -1;
  1213. }
  1214. return 0;
  1215. }
  1216. static void init_qexp(void){
  1217. int i;
  1218. double v=128;
  1219. for(i=0; i<QROOT; i++){
  1220. qexp[i]= lrintf(v);
  1221. v *= pow(2, 1.0 / QROOT);
  1222. }
  1223. }
  1224. static av_cold int common_init(AVCodecContext *avctx){
  1225. SnowContext *s = avctx->priv_data;
  1226. int width, height;
  1227. int i, j;
  1228. s->avctx= avctx;
  1229. s->max_ref_frames=1; //just make sure its not an invalid value in case of no initial keyframe
  1230. dsputil_init(&s->dsp, avctx);
  1231. ff_dwt_init(&s->dwt);
  1232. #define mcf(dx,dy)\
  1233. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  1234. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  1235. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  1236. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  1237. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  1238. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  1239. mcf( 0, 0)
  1240. mcf( 4, 0)
  1241. mcf( 8, 0)
  1242. mcf(12, 0)
  1243. mcf( 0, 4)
  1244. mcf( 4, 4)
  1245. mcf( 8, 4)
  1246. mcf(12, 4)
  1247. mcf( 0, 8)
  1248. mcf( 4, 8)
  1249. mcf( 8, 8)
  1250. mcf(12, 8)
  1251. mcf( 0,12)
  1252. mcf( 4,12)
  1253. mcf( 8,12)
  1254. mcf(12,12)
  1255. #define mcfh(dx,dy)\
  1256. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  1257. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  1258. mc_block_hpel ## dx ## dy ## 16;\
  1259. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  1260. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  1261. mc_block_hpel ## dx ## dy ## 8;
  1262. mcfh(0, 0)
  1263. mcfh(8, 0)
  1264. mcfh(0, 8)
  1265. mcfh(8, 8)
  1266. if(!qexp[0])
  1267. init_qexp();
  1268. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  1269. width= s->avctx->width;
  1270. height= s->avctx->height;
  1271. s->spatial_idwt_buffer= av_mallocz(width*height*sizeof(IDWTELEM));
  1272. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); //FIXME this does not belong here
  1273. for(i=0; i<MAX_REF_FRAMES; i++)
  1274. for(j=0; j<MAX_REF_FRAMES; j++)
  1275. scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  1276. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  1277. s->scratchbuf = av_malloc(s->mconly_picture.linesize[0]*7*MB_SIZE);
  1278. return 0;
  1279. }
  1280. static int common_init_after_header(AVCodecContext *avctx){
  1281. SnowContext *s = avctx->priv_data;
  1282. int plane_index, level, orientation;
  1283. for(plane_index=0; plane_index<3; plane_index++){
  1284. int w= s->avctx->width;
  1285. int h= s->avctx->height;
  1286. if(plane_index){
  1287. w>>= s->chroma_h_shift;
  1288. h>>= s->chroma_v_shift;
  1289. }
  1290. s->plane[plane_index].width = w;
  1291. s->plane[plane_index].height= h;
  1292. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  1293. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1294. SubBand *b= &s->plane[plane_index].band[level][orientation];
  1295. b->buf= s->spatial_dwt_buffer;
  1296. b->level= level;
  1297. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  1298. b->width = (w + !(orientation&1))>>1;
  1299. b->height= (h + !(orientation>1))>>1;
  1300. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  1301. b->buf_x_offset = 0;
  1302. b->buf_y_offset = 0;
  1303. if(orientation&1){
  1304. b->buf += (w+1)>>1;
  1305. b->buf_x_offset = (w+1)>>1;
  1306. }
  1307. if(orientation>1){
  1308. b->buf += b->stride>>1;
  1309. b->buf_y_offset = b->stride_line >> 1;
  1310. }
  1311. b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
  1312. if(level)
  1313. b->parent= &s->plane[plane_index].band[level-1][orientation];
  1314. //FIXME avoid this realloc
  1315. av_freep(&b->x_coeff);
  1316. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  1317. }
  1318. w= (w+1)>>1;
  1319. h= (h+1)>>1;
  1320. }
  1321. }
  1322. return 0;
  1323. }
  1324. #define QUANTIZE2 0
  1325. #if QUANTIZE2==1
  1326. #define Q2_STEP 8
  1327. static void find_sse(SnowContext *s, Plane *p, int *score, int score_stride, IDWTELEM *r0, IDWTELEM *r1, int level, int orientation){
  1328. SubBand *b= &p->band[level][orientation];
  1329. int x, y;
  1330. int xo=0;
  1331. int yo=0;
  1332. int step= 1 << (s->spatial_decomposition_count - level);
  1333. if(orientation&1)
  1334. xo= step>>1;
  1335. if(orientation&2)
  1336. yo= step>>1;
  1337. //FIXME bias for nonzero ?
  1338. //FIXME optimize
  1339. memset(score, 0, sizeof(*score)*score_stride*((p->height + Q2_STEP-1)/Q2_STEP));
  1340. for(y=0; y<p->height; y++){
  1341. for(x=0; x<p->width; x++){
  1342. int sx= (x-xo + step/2) / step / Q2_STEP;
  1343. int sy= (y-yo + step/2) / step / Q2_STEP;
  1344. int v= r0[x + y*p->width] - r1[x + y*p->width];
  1345. assert(sx>=0 && sy>=0 && sx < score_stride);
  1346. v= ((v+8)>>4)<<4;
  1347. score[sx + sy*score_stride] += v*v;
  1348. assert(score[sx + sy*score_stride] >= 0);
  1349. }
  1350. }
  1351. }
  1352. static void dequantize_all(SnowContext *s, Plane *p, IDWTELEM *buffer, int width, int height){
  1353. int level, orientation;
  1354. for(level=0; level<s->spatial_decomposition_count; level++){
  1355. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1356. SubBand *b= &p->band[level][orientation];
  1357. IDWTELEM *dst= buffer + (b->ibuf - s->spatial_idwt_buffer);
  1358. dequantize(s, b, dst, b->stride);
  1359. }
  1360. }
  1361. }
  1362. static void dwt_quantize(SnowContext *s, Plane *p, DWTELEM *buffer, int width, int height, int stride, int type){
  1363. int level, orientation, ys, xs, x, y, pass;
  1364. IDWTELEM best_dequant[height * stride];
  1365. IDWTELEM idwt2_buffer[height * stride];
  1366. const int score_stride= (width + 10)/Q2_STEP;
  1367. int best_score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
  1368. int score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
  1369. int threshold= (s->m.lambda * s->m.lambda) >> 6;
  1370. //FIXME pass the copy cleanly ?
  1371. // memcpy(dwt_buffer, buffer, height * stride * sizeof(DWTELEM));
  1372. ff_spatial_dwt(buffer, width, height, stride, type, s->spatial_decomposition_count);
  1373. for(level=0; level<s->spatial_decomposition_count; level++){
  1374. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1375. SubBand *b= &p->band[level][orientation];
  1376. IDWTELEM *dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
  1377. DWTELEM *src= buffer + (b-> buf - s->spatial_dwt_buffer);
  1378. assert(src == b->buf); // code does not depend on this but it is true currently
  1379. quantize(s, b, dst, src, b->stride, s->qbias);
  1380. }
  1381. }
  1382. for(pass=0; pass<1; pass++){
  1383. if(s->qbias == 0) //keyframe
  1384. continue;
  1385. for(level=0; level<s->spatial_decomposition_count; level++){
  1386. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1387. SubBand *b= &p->band[level][orientation];
  1388. IDWTELEM *dst= idwt2_buffer + (b->ibuf - s->spatial_idwt_buffer);
  1389. IDWTELEM *best_dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
  1390. for(ys= 0; ys<Q2_STEP; ys++){
  1391. for(xs= 0; xs<Q2_STEP; xs++){
  1392. memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
  1393. dequantize_all(s, p, idwt2_buffer, width, height);
  1394. ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
  1395. find_sse(s, p, best_score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
  1396. memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
  1397. for(y=ys; y<b->height; y+= Q2_STEP){
  1398. for(x=xs; x<b->width; x+= Q2_STEP){
  1399. if(dst[x + y*b->stride]<0) dst[x + y*b->stride]++;
  1400. if(dst[x + y*b->stride]>0) dst[x + y*b->stride]--;
  1401. //FIXME try more than just --
  1402. }
  1403. }
  1404. dequantize_all(s, p, idwt2_buffer, width, height);
  1405. ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
  1406. find_sse(s, p, score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
  1407. for(y=ys; y<b->height; y+= Q2_STEP){
  1408. for(x=xs; x<b->width; x+= Q2_STEP){
  1409. int score_idx= x/Q2_STEP + (y/Q2_STEP)*score_stride;
  1410. if(score[score_idx] <= best_score[score_idx] + threshold){
  1411. best_score[score_idx]= score[score_idx];
  1412. if(best_dst[x + y*b->stride]<0) best_dst[x + y*b->stride]++;
  1413. if(best_dst[x + y*b->stride]>0) best_dst[x + y*b->stride]--;
  1414. //FIXME copy instead
  1415. }
  1416. }
  1417. }
  1418. }
  1419. }
  1420. }
  1421. }
  1422. }
  1423. memcpy(s->spatial_idwt_buffer, best_dequant, height * stride * sizeof(IDWTELEM)); //FIXME work with that directly instead of copy at the end
  1424. }
  1425. #endif /* QUANTIZE2==1 */
  1426. #define USE_HALFPEL_PLANE 0
  1427. static void halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
  1428. int p,x,y;
  1429. assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
  1430. for(p=0; p<3; p++){
  1431. int is_chroma= !!p;
  1432. int w= s->avctx->width >>is_chroma;
  1433. int h= s->avctx->height >>is_chroma;
  1434. int ls= frame->linesize[p];
  1435. uint8_t *src= frame->data[p];
  1436. halfpel[1][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  1437. halfpel[2][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  1438. halfpel[3][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  1439. halfpel[0][p]= src;
  1440. for(y=0; y<h; y++){
  1441. for(x=0; x<w; x++){
  1442. int i= y*ls + x;
  1443. halfpel[1][p][i]= (20*(src[i] + src[i+1]) - 5*(src[i-1] + src[i+2]) + (src[i-2] + src[i+3]) + 16 )>>5;
  1444. }
  1445. }
  1446. for(y=0; y<h; y++){
  1447. for(x=0; x<w; x++){
  1448. int i= y*ls + x;
  1449. halfpel[2][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  1450. }
  1451. }
  1452. src= halfpel[1][p];
  1453. for(y=0; y<h; y++){
  1454. for(x=0; x<w; x++){
  1455. int i= y*ls + x;
  1456. halfpel[3][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  1457. }
  1458. }
  1459. //FIXME border!
  1460. }
  1461. }
  1462. static void release_buffer(AVCodecContext *avctx){
  1463. SnowContext *s = avctx->priv_data;
  1464. int i;
  1465. if(s->last_picture[s->max_ref_frames-1].data[0]){
  1466. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  1467. for(i=0; i<9; i++)
  1468. if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
  1469. av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
  1470. }
  1471. }
  1472. static int frame_start(SnowContext *s){
  1473. AVFrame tmp;
  1474. int w= s->avctx->width; //FIXME round up to x16 ?
  1475. int h= s->avctx->height;
  1476. if(s->current_picture.data[0]){
  1477. s->dsp.draw_edges(s->current_picture.data[0],
  1478. s->current_picture.linesize[0], w , h ,
  1479. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1480. s->dsp.draw_edges(s->current_picture.data[1],
  1481. s->current_picture.linesize[1], w>>1, h>>1,
  1482. EDGE_WIDTH/2, EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
  1483. s->dsp.draw_edges(s->current_picture.data[2],
  1484. s->current_picture.linesize[2], w>>1, h>>1,
  1485. EDGE_WIDTH/2, EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
  1486. }
  1487. release_buffer(s->avctx);
  1488. tmp= s->last_picture[s->max_ref_frames-1];
  1489. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  1490. memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  1491. if(USE_HALFPEL_PLANE && s->current_picture.data[0])
  1492. halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
  1493. s->last_picture[0]= s->current_picture;
  1494. s->current_picture= tmp;
  1495. if(s->keyframe){
  1496. s->ref_frames= 0;
  1497. }else{
  1498. int i;
  1499. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  1500. if(i && s->last_picture[i-1].key_frame)
  1501. break;
  1502. s->ref_frames= i;
  1503. if(s->ref_frames==0){
  1504. av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
  1505. return -1;
  1506. }
  1507. }
  1508. s->current_picture.reference= 1;
  1509. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  1510. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1511. return -1;
  1512. }
  1513. s->current_picture.key_frame= s->keyframe;
  1514. return 0;
  1515. }
  1516. static av_cold void common_end(SnowContext *s){
  1517. int plane_index, level, orientation, i;
  1518. av_freep(&s->spatial_dwt_buffer);
  1519. av_freep(&s->spatial_idwt_buffer);
  1520. s->m.me.temp= NULL;
  1521. av_freep(&s->m.me.scratchpad);
  1522. av_freep(&s->m.me.map);
  1523. av_freep(&s->m.me.score_map);
  1524. av_freep(&s->m.obmc_scratchpad);
  1525. av_freep(&s->block);
  1526. av_freep(&s->scratchbuf);
  1527. for(i=0; i<MAX_REF_FRAMES; i++){
  1528. av_freep(&s->ref_mvs[i]);
  1529. av_freep(&s->ref_scores[i]);
  1530. if(s->last_picture[i].data[0])
  1531. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  1532. }
  1533. for(plane_index=0; plane_index<3; plane_index++){
  1534. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  1535. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1536. SubBand *b= &s->plane[plane_index].band[level][orientation];
  1537. av_freep(&b->x_coeff);
  1538. }
  1539. }
  1540. }
  1541. if (s->mconly_picture.data[0])
  1542. s->avctx->release_buffer(s->avctx, &s->mconly_picture);
  1543. if (s->current_picture.data[0])
  1544. s->avctx->release_buffer(s->avctx, &s->current_picture);
  1545. }
  1546. static av_cold int decode_init(AVCodecContext *avctx)
  1547. {
  1548. avctx->pix_fmt= PIX_FMT_YUV420P;
  1549. common_init(avctx);
  1550. return 0;
  1551. }
  1552. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1553. const uint8_t *buf = avpkt->data;
  1554. int buf_size = avpkt->size;
  1555. SnowContext *s = avctx->priv_data;
  1556. RangeCoder * const c= &s->c;
  1557. int bytes_read;
  1558. AVFrame *picture = data;
  1559. int level, orientation, plane_index;
  1560. ff_init_range_decoder(c, buf, buf_size);
  1561. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1562. s->current_picture.pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  1563. if(decode_header(s)<0)
  1564. return -1;
  1565. common_init_after_header(avctx);
  1566. // realloc slice buffer for the case that spatial_decomposition_count changed
  1567. ff_slice_buffer_destroy(&s->sb);
  1568. ff_slice_buffer_init(&s->sb, s->plane[0].height, (MB_SIZE >> s->block_max_depth) + s->spatial_decomposition_count * 8 + 1, s->plane[0].width, s->spatial_idwt_buffer);
  1569. for(plane_index=0; plane_index<3; plane_index++){
  1570. Plane *p= &s->plane[plane_index];
  1571. p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  1572. && p->hcoeff[1]==-10
  1573. && p->hcoeff[2]==2;
  1574. }
  1575. alloc_blocks(s);
  1576. if(frame_start(s) < 0)
  1577. return -1;
  1578. //keyframe flag duplication mess FIXME
  1579. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1580. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  1581. decode_blocks(s);
  1582. for(plane_index=0; plane_index<3; plane_index++){
  1583. Plane *p= &s->plane[plane_index];
  1584. int w= p->width;
  1585. int h= p->height;
  1586. int x, y;
  1587. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  1588. if(s->avctx->debug&2048){
  1589. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  1590. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1591. for(y=0; y<h; y++){
  1592. for(x=0; x<w; x++){
  1593. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  1594. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  1595. }
  1596. }
  1597. }
  1598. {
  1599. for(level=0; level<s->spatial_decomposition_count; level++){
  1600. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1601. SubBand *b= &p->band[level][orientation];
  1602. unpack_coeffs(s, b, b->parent, orientation);
  1603. }
  1604. }
  1605. }
  1606. {
  1607. const int mb_h= s->b_height << s->block_max_depth;
  1608. const int block_size = MB_SIZE >> s->block_max_depth;
  1609. const int block_w = plane_index ? block_size/2 : block_size;
  1610. int mb_y;
  1611. DWTCompose cs[MAX_DECOMPOSITIONS];
  1612. int yd=0, yq=0;
  1613. int y;
  1614. int end_y;
  1615. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1616. for(mb_y=0; mb_y<=mb_h; mb_y++){
  1617. int slice_starty = block_w*mb_y;
  1618. int slice_h = block_w*(mb_y+1);
  1619. if (!(s->keyframe || s->avctx->debug&512)){
  1620. slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
  1621. slice_h -= (block_w >> 1);
  1622. }
  1623. for(level=0; level<s->spatial_decomposition_count; level++){
  1624. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1625. SubBand *b= &p->band[level][orientation];
  1626. int start_y;
  1627. int end_y;
  1628. int our_mb_start = mb_y;
  1629. int our_mb_end = (mb_y + 1);
  1630. const int extra= 3;
  1631. start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  1632. end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  1633. if (!(s->keyframe || s->avctx->debug&512)){
  1634. start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  1635. end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  1636. }
  1637. start_y = FFMIN(b->height, start_y);
  1638. end_y = FFMIN(b->height, end_y);
  1639. if (start_y != end_y){
  1640. if (orientation == 0){
  1641. SubBand * correlate_band = &p->band[0][0];
  1642. int correlate_end_y = FFMIN(b->height, end_y + 1);
  1643. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  1644. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  1645. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  1646. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
  1647. }
  1648. else
  1649. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  1650. }
  1651. }
  1652. }
  1653. for(; yd<slice_h; yd+=4){
  1654. ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  1655. }
  1656. if(s->qlog == LOSSLESS_QLOG){
  1657. for(; yq<slice_h && yq<h; yq++){
  1658. IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  1659. for(x=0; x<w; x++){
  1660. line[x] <<= FRAC_BITS;
  1661. }
  1662. }
  1663. }
  1664. predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  1665. y = FFMIN(p->height, slice_starty);
  1666. end_y = FFMIN(p->height, slice_h);
  1667. while(y < end_y)
  1668. ff_slice_buffer_release(&s->sb, y++);
  1669. }
  1670. ff_slice_buffer_flush(&s->sb);
  1671. }
  1672. }
  1673. emms_c();
  1674. release_buffer(avctx);
  1675. if(!(s->avctx->debug&2048))
  1676. *picture= s->current_picture;
  1677. else
  1678. *picture= s->mconly_picture;
  1679. *data_size = sizeof(AVFrame);
  1680. bytes_read= c->bytestream - c->bytestream_start;
  1681. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  1682. return bytes_read;
  1683. }
  1684. static av_cold int decode_end(AVCodecContext *avctx)
  1685. {
  1686. SnowContext *s = avctx->priv_data;
  1687. ff_slice_buffer_destroy(&s->sb);
  1688. common_end(s);
  1689. return 0;
  1690. }
  1691. AVCodec ff_snow_decoder = {
  1692. "snow",
  1693. AVMEDIA_TYPE_VIDEO,
  1694. CODEC_ID_SNOW,
  1695. sizeof(SnowContext),
  1696. decode_init,
  1697. NULL,
  1698. decode_end,
  1699. decode_frame,
  1700. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1701. NULL,
  1702. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  1703. };
  1704. #if CONFIG_SNOW_ENCODER
  1705. static av_cold int encode_init(AVCodecContext *avctx)
  1706. {
  1707. SnowContext *s = avctx->priv_data;
  1708. int plane_index;
  1709. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  1710. av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  1711. "Use vstrict=-2 / -strict -2 to use it anyway.\n");
  1712. return -1;
  1713. }
  1714. if(avctx->prediction_method == DWT_97
  1715. && (avctx->flags & CODEC_FLAG_QSCALE)
  1716. && avctx->global_quality == 0){
  1717. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  1718. return -1;
  1719. }
  1720. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  1721. s->mv_scale = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  1722. s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
  1723. for(plane_index=0; plane_index<3; plane_index++){
  1724. s->plane[plane_index].diag_mc= 1;
  1725. s->plane[plane_index].htaps= 6;
  1726. s->plane[plane_index].hcoeff[0]= 40;
  1727. s->plane[plane_index].hcoeff[1]= -10;
  1728. s->plane[plane_index].hcoeff[2]= 2;
  1729. s->plane[plane_index].fast_mc= 1;
  1730. }
  1731. common_init(avctx);
  1732. alloc_blocks(s);
  1733. s->version=0;
  1734. s->m.avctx = avctx;
  1735. s->m.flags = avctx->flags;
  1736. s->m.bit_rate= avctx->bit_rate;
  1737. s->m.me.temp =
  1738. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  1739. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  1740. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  1741. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  1742. h263_encode_init(&s->m); //mv_penalty
  1743. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  1744. if(avctx->flags&CODEC_FLAG_PASS1){
  1745. if(!avctx->stats_out)
  1746. avctx->stats_out = av_mallocz(256);
  1747. }
  1748. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  1749. if(ff_rate_control_init(&s->m) < 0)
  1750. return -1;
  1751. }
  1752. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  1753. avctx->coded_frame= &s->current_picture;
  1754. switch(avctx->pix_fmt){
  1755. // case PIX_FMT_YUV444P:
  1756. // case PIX_FMT_YUV422P:
  1757. case PIX_FMT_YUV420P:
  1758. case PIX_FMT_GRAY8:
  1759. // case PIX_FMT_YUV411P:
  1760. // case PIX_FMT_YUV410P:
  1761. s->colorspace_type= 0;
  1762. break;
  1763. /* case PIX_FMT_RGB32:
  1764. s->colorspace= 1;
  1765. break;*/
  1766. default:
  1767. av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
  1768. return -1;
  1769. }
  1770. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  1771. s->chroma_h_shift= 1;
  1772. s->chroma_v_shift= 1;
  1773. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  1774. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  1775. s->avctx->get_buffer(s->avctx, &s->input_picture);
  1776. if(s->avctx->me_method == ME_ITER){
  1777. int i;
  1778. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  1779. for(i=0; i<s->max_ref_frames; i++){
  1780. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  1781. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  1782. }
  1783. }
  1784. return 0;
  1785. }
  1786. //near copy & paste from dsputil, FIXME
  1787. static int pix_sum(uint8_t * pix, int line_size, int w)
  1788. {
  1789. int s, i, j;
  1790. s = 0;
  1791. for (i = 0; i < w; i++) {
  1792. for (j = 0; j < w; j++) {
  1793. s += pix[0];
  1794. pix ++;
  1795. }
  1796. pix += line_size - w;
  1797. }
  1798. return s;
  1799. }
  1800. //near copy & paste from dsputil, FIXME
  1801. static int pix_norm1(uint8_t * pix, int line_size, int w)
  1802. {
  1803. int s, i, j;
  1804. uint32_t *sq = ff_squareTbl + 256;
  1805. s = 0;
  1806. for (i = 0; i < w; i++) {
  1807. for (j = 0; j < w; j ++) {
  1808. s += sq[pix[0]];
  1809. pix ++;
  1810. }
  1811. pix += line_size - w;
  1812. }
  1813. return s;
  1814. }
  1815. //FIXME copy&paste
  1816. #define P_LEFT P[1]
  1817. #define P_TOP P[2]
  1818. #define P_TOPRIGHT P[3]
  1819. #define P_MEDIAN P[4]
  1820. #define P_MV1 P[9]
  1821. #define FLAG_QPEL 1 //must be 1
  1822. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  1823. uint8_t p_buffer[1024];
  1824. uint8_t i_buffer[1024];
  1825. uint8_t p_state[sizeof(s->block_state)];
  1826. uint8_t i_state[sizeof(s->block_state)];
  1827. RangeCoder pc, ic;
  1828. uint8_t *pbbak= s->c.bytestream;
  1829. uint8_t *pbbak_start= s->c.bytestream_start;
  1830. int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  1831. const int w= s->b_width << s->block_max_depth;
  1832. const int h= s->b_height << s->block_max_depth;
  1833. const int rem_depth= s->block_max_depth - level;
  1834. const int index= (x + y*w) << rem_depth;
  1835. const int block_w= 1<<(LOG2_MB_SIZE - level);
  1836. int trx= (x+1)<<rem_depth;
  1837. int try= (y+1)<<rem_depth;
  1838. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  1839. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  1840. const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1841. const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1842. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1843. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1844. int pl = left->color[0];
  1845. int pcb= left->color[1];
  1846. int pcr= left->color[2];
  1847. int pmx, pmy;
  1848. int mx=0, my=0;
  1849. int l,cr,cb;
  1850. const int stride= s->current_picture.linesize[0];
  1851. const int uvstride= s->current_picture.linesize[1];
  1852. uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
  1853. s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
  1854. s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
  1855. int P[10][2];
  1856. int16_t last_mv[3][2];
  1857. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  1858. const int shift= 1+qpel;
  1859. MotionEstContext *c= &s->m.me;
  1860. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  1861. int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  1862. int my_context= av_log2(2*FFABS(left->my - top->my));
  1863. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1864. int ref, best_ref, ref_score, ref_mx, ref_my;
  1865. assert(sizeof(s->block_state) >= 256);
  1866. if(s->keyframe){
  1867. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  1868. return 0;
  1869. }
  1870. // clip predictors / edge ?
  1871. P_LEFT[0]= left->mx;
  1872. P_LEFT[1]= left->my;
  1873. P_TOP [0]= top->mx;
  1874. P_TOP [1]= top->my;
  1875. P_TOPRIGHT[0]= tr->mx;
  1876. P_TOPRIGHT[1]= tr->my;
  1877. last_mv[0][0]= s->block[index].mx;
  1878. last_mv[0][1]= s->block[index].my;
  1879. last_mv[1][0]= right->mx;
  1880. last_mv[1][1]= right->my;
  1881. last_mv[2][0]= bottom->mx;
  1882. last_mv[2][1]= bottom->my;
  1883. s->m.mb_stride=2;
  1884. s->m.mb_x=
  1885. s->m.mb_y= 0;
  1886. c->skip= 0;
  1887. assert(c-> stride == stride);
  1888. assert(c->uvstride == uvstride);
  1889. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1890. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1891. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1892. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  1893. c->xmin = - x*block_w - 16+3;
  1894. c->ymin = - y*block_w - 16+3;
  1895. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  1896. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  1897. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1898. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  1899. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  1900. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1901. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1902. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  1903. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1904. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1905. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1906. if (!y) {
  1907. c->pred_x= P_LEFT[0];
  1908. c->pred_y= P_LEFT[1];
  1909. } else {
  1910. c->pred_x = P_MEDIAN[0];
  1911. c->pred_y = P_MEDIAN[1];
  1912. }
  1913. score= INT_MAX;
  1914. best_ref= 0;
  1915. for(ref=0; ref<s->ref_frames; ref++){
  1916. init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
  1917. ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  1918. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  1919. assert(ref_mx >= c->xmin);
  1920. assert(ref_mx <= c->xmax);
  1921. assert(ref_my >= c->ymin);
  1922. assert(ref_my <= c->ymax);
  1923. ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  1924. ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  1925. ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  1926. if(s->ref_mvs[ref]){
  1927. s->ref_mvs[ref][index][0]= ref_mx;
  1928. s->ref_mvs[ref][index][1]= ref_my;
  1929. s->ref_scores[ref][index]= ref_score;
  1930. }
  1931. if(score > ref_score){
  1932. score= ref_score;
  1933. best_ref= ref;
  1934. mx= ref_mx;
  1935. my= ref_my;
  1936. }
  1937. }
  1938. //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  1939. // subpel search
  1940. base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  1941. pc= s->c;
  1942. pc.bytestream_start=
  1943. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  1944. memcpy(p_state, s->block_state, sizeof(s->block_state));
  1945. if(level!=s->block_max_depth)
  1946. put_rac(&pc, &p_state[4 + s_context], 1);
  1947. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  1948. if(s->ref_frames > 1)
  1949. put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  1950. pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  1951. put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  1952. put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  1953. p_len= pc.bytestream - pc.bytestream_start;
  1954. score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  1955. block_s= block_w*block_w;
  1956. sum = pix_sum(current_data[0], stride, block_w);
  1957. l= (sum + block_s/2)/block_s;
  1958. iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  1959. block_s= block_w*block_w>>2;
  1960. sum = pix_sum(current_data[1], uvstride, block_w>>1);
  1961. cb= (sum + block_s/2)/block_s;
  1962. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  1963. sum = pix_sum(current_data[2], uvstride, block_w>>1);
  1964. cr= (sum + block_s/2)/block_s;
  1965. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  1966. ic= s->c;
  1967. ic.bytestream_start=
  1968. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  1969. memcpy(i_state, s->block_state, sizeof(s->block_state));
  1970. if(level!=s->block_max_depth)
  1971. put_rac(&ic, &i_state[4 + s_context], 1);
  1972. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  1973. put_symbol(&ic, &i_state[32], l-pl , 1);
  1974. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  1975. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  1976. i_len= ic.bytestream - ic.bytestream_start;
  1977. iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  1978. // assert(score==256*256*256*64-1);
  1979. assert(iscore < 255*255*256 + s->lambda2*10);
  1980. assert(iscore >= 0);
  1981. assert(l>=0 && l<=255);
  1982. assert(pl>=0 && pl<=255);
  1983. if(level==0){
  1984. int varc= iscore >> 8;
  1985. int vard= score >> 8;
  1986. if (vard <= 64 || vard < varc)
  1987. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1988. else
  1989. c->scene_change_score+= s->m.qscale;
  1990. }
  1991. if(level!=s->block_max_depth){
  1992. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  1993. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1994. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1995. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1996. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1997. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  1998. if(score2 < score && score2 < iscore)
  1999. return score2;
  2000. }
  2001. if(iscore < score){
  2002. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2003. memcpy(pbbak, i_buffer, i_len);
  2004. s->c= ic;
  2005. s->c.bytestream_start= pbbak_start;
  2006. s->c.bytestream= pbbak + i_len;
  2007. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  2008. memcpy(s->block_state, i_state, sizeof(s->block_state));
  2009. return iscore;
  2010. }else{
  2011. memcpy(pbbak, p_buffer, p_len);
  2012. s->c= pc;
  2013. s->c.bytestream_start= pbbak_start;
  2014. s->c.bytestream= pbbak + p_len;
  2015. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  2016. memcpy(s->block_state, p_state, sizeof(s->block_state));
  2017. return score;
  2018. }
  2019. }
  2020. static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  2021. const int w= s->b_width << s->block_max_depth;
  2022. const int rem_depth= s->block_max_depth - level;
  2023. const int index= (x + y*w) << rem_depth;
  2024. int trx= (x+1)<<rem_depth;
  2025. BlockNode *b= &s->block[index];
  2026. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2027. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  2028. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2029. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  2030. int pl = left->color[0];
  2031. int pcb= left->color[1];
  2032. int pcr= left->color[2];
  2033. int pmx, pmy;
  2034. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2035. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  2036. int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  2037. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  2038. if(s->keyframe){
  2039. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  2040. return;
  2041. }
  2042. if(level!=s->block_max_depth){
  2043. if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  2044. put_rac(&s->c, &s->block_state[4 + s_context], 1);
  2045. }else{
  2046. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  2047. encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  2048. encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  2049. encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  2050. encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  2051. return;
  2052. }
  2053. }
  2054. if(b->type & BLOCK_INTRA){
  2055. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2056. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  2057. put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  2058. put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  2059. put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  2060. set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  2061. }else{
  2062. pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  2063. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  2064. if(s->ref_frames > 1)
  2065. put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  2066. put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  2067. put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  2068. set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  2069. }
  2070. }
  2071. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2072. int i, x2, y2;
  2073. Plane *p= &s->plane[plane_index];
  2074. const int block_size = MB_SIZE >> s->block_max_depth;
  2075. const int block_w = plane_index ? block_size/2 : block_size;
  2076. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2077. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2078. const int ref_stride= s->current_picture.linesize[plane_index];
  2079. uint8_t *src= s-> input_picture.data[plane_index];
  2080. IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  2081. const int b_stride = s->b_width << s->block_max_depth;
  2082. const int w= p->width;
  2083. const int h= p->height;
  2084. int index= mb_x + mb_y*b_stride;
  2085. BlockNode *b= &s->block[index];
  2086. BlockNode backup= *b;
  2087. int ab=0;
  2088. int aa=0;
  2089. b->type|= BLOCK_INTRA;
  2090. b->color[plane_index]= 0;
  2091. memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  2092. for(i=0; i<4; i++){
  2093. int mb_x2= mb_x + (i &1) - 1;
  2094. int mb_y2= mb_y + (i>>1) - 1;
  2095. int x= block_w*mb_x2 + block_w/2;
  2096. int y= block_w*mb_y2 + block_w/2;
  2097. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2098. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2099. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2100. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2101. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2102. int obmc_v= obmc[index];
  2103. int d;
  2104. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2105. if(x<0) obmc_v += obmc[index + block_w];
  2106. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2107. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2108. //FIXME precalculate this or simplify it somehow else
  2109. d = -dst[index] + (1<<(FRAC_BITS-1));
  2110. dst[index] = d;
  2111. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2112. aa += obmc_v * obmc_v; //FIXME precalculate this
  2113. }
  2114. }
  2115. }
  2116. *b= backup;
  2117. return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we should not need clipping
  2118. }
  2119. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2120. const int b_stride = s->b_width << s->block_max_depth;
  2121. const int b_height = s->b_height<< s->block_max_depth;
  2122. int index= x + y*b_stride;
  2123. const BlockNode *b = &s->block[index];
  2124. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2125. const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2126. const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2127. const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2128. int dmx, dmy;
  2129. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2130. // int my_context= av_log2(2*FFABS(left->my - top->my));
  2131. if(x<0 || x>=b_stride || y>=b_height)
  2132. return 0;
  2133. /*
  2134. 1 0 0
  2135. 01X 1-2 1
  2136. 001XX 3-6 2-3
  2137. 0001XXX 7-14 4-7
  2138. 00001XXXX 15-30 8-15
  2139. */
  2140. //FIXME try accurate rate
  2141. //FIXME intra and inter predictors if surrounding blocks are not the same type
  2142. if(b->type & BLOCK_INTRA){
  2143. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  2144. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  2145. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  2146. }else{
  2147. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2148. dmx-= b->mx;
  2149. dmy-= b->my;
  2150. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2151. + av_log2(2*FFABS(dmy))
  2152. + av_log2(2*b->ref));
  2153. }
  2154. }
  2155. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2156. Plane *p= &s->plane[plane_index];
  2157. const int block_size = MB_SIZE >> s->block_max_depth;
  2158. const int block_w = plane_index ? block_size/2 : block_size;
  2159. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2160. const int ref_stride= s->current_picture.linesize[plane_index];
  2161. uint8_t *dst= s->current_picture.data[plane_index];
  2162. uint8_t *src= s-> input_picture.data[plane_index];
  2163. IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2164. uint8_t *cur = s->scratchbuf;
  2165. uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
  2166. const int b_stride = s->b_width << s->block_max_depth;
  2167. const int b_height = s->b_height<< s->block_max_depth;
  2168. const int w= p->width;
  2169. const int h= p->height;
  2170. int distortion;
  2171. int rate= 0;
  2172. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2173. int sx= block_w*mb_x - block_w/2;
  2174. int sy= block_w*mb_y - block_w/2;
  2175. int x0= FFMAX(0,-sx);
  2176. int y0= FFMAX(0,-sy);
  2177. int x1= FFMIN(block_w*2, w-sx);
  2178. int y1= FFMIN(block_w*2, h-sy);
  2179. int i,x,y;
  2180. pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
  2181. for(y=y0; y<y1; y++){
  2182. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2183. const IDWTELEM *pred1 = pred + y*obmc_stride;
  2184. uint8_t *cur1 = cur + y*ref_stride;
  2185. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2186. for(x=x0; x<x1; x++){
  2187. #if FRAC_BITS >= LOG2_OBMC_MAX
  2188. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2189. #else
  2190. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  2191. #endif
  2192. v = (v + pred1[x]) >> FRAC_BITS;
  2193. if(v&(~255)) v= ~(v>>31);
  2194. dst1[x] = v;
  2195. }
  2196. }
  2197. /* copy the regions where obmc[] = (uint8_t)256 */
  2198. if(LOG2_OBMC_MAX == 8
  2199. && (mb_x == 0 || mb_x == b_stride-1)
  2200. && (mb_y == 0 || mb_y == b_height-1)){
  2201. if(mb_x == 0)
  2202. x1 = block_w;
  2203. else
  2204. x0 = block_w;
  2205. if(mb_y == 0)
  2206. y1 = block_w;
  2207. else
  2208. y0 = block_w;
  2209. for(y=y0; y<y1; y++)
  2210. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2211. }
  2212. if(block_w==16){
  2213. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2214. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2215. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  2216. * So improving the score of one block is not strictly guaranteed
  2217. * to improve the score of the whole frame, thus iterative motion
  2218. * estimation does not always converge. */
  2219. if(s->avctx->me_cmp == FF_CMP_W97)
  2220. distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2221. else if(s->avctx->me_cmp == FF_CMP_W53)
  2222. distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2223. else{
  2224. distortion = 0;
  2225. for(i=0; i<4; i++){
  2226. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2227. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2228. }
  2229. }
  2230. }else{
  2231. assert(block_w==8);
  2232. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2233. }
  2234. if(plane_index==0){
  2235. for(i=0; i<4; i++){
  2236. /* ..RRr
  2237. * .RXx.
  2238. * rxx..
  2239. */
  2240. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2241. }
  2242. if(mb_x == b_stride-2)
  2243. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2244. }
  2245. return distortion + rate*penalty_factor;
  2246. }
  2247. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2248. int i, y2;
  2249. Plane *p= &s->plane[plane_index];
  2250. const int block_size = MB_SIZE >> s->block_max_depth;
  2251. const int block_w = plane_index ? block_size/2 : block_size;
  2252. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2253. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2254. const int ref_stride= s->current_picture.linesize[plane_index];
  2255. uint8_t *dst= s->current_picture.data[plane_index];
  2256. uint8_t *src= s-> input_picture.data[plane_index];
  2257. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  2258. // const has only been removed from zero_dst to suppress a warning
  2259. static IDWTELEM zero_dst[4096]; //FIXME
  2260. const int b_stride = s->b_width << s->block_max_depth;
  2261. const int w= p->width;
  2262. const int h= p->height;
  2263. int distortion= 0;
  2264. int rate= 0;
  2265. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2266. for(i=0; i<9; i++){
  2267. int mb_x2= mb_x + (i%3) - 1;
  2268. int mb_y2= mb_y + (i/3) - 1;
  2269. int x= block_w*mb_x2 + block_w/2;
  2270. int y= block_w*mb_y2 + block_w/2;
  2271. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  2272. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2273. //FIXME find a cleaner/simpler way to skip the outside stuff
  2274. for(y2= y; y2<0; y2++)
  2275. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2276. for(y2= h; y2<y+block_w; y2++)
  2277. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2278. if(x<0){
  2279. for(y2= y; y2<y+block_w; y2++)
  2280. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2281. }
  2282. if(x+block_w > w){
  2283. for(y2= y; y2<y+block_w; y2++)
  2284. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2285. }
  2286. assert(block_w== 8 || block_w==16);
  2287. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2288. }
  2289. if(plane_index==0){
  2290. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2291. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2292. /* ..RRRr
  2293. * .RXXx.
  2294. * .RXXx.
  2295. * rxxx.
  2296. */
  2297. if(merged)
  2298. rate = get_block_bits(s, mb_x, mb_y, 2);
  2299. for(i=merged?4:0; i<9; i++){
  2300. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2301. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2302. }
  2303. }
  2304. return distortion + rate*penalty_factor;
  2305. }
  2306. static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2307. const int w= b->width;
  2308. const int h= b->height;
  2309. int x, y;
  2310. if(1){
  2311. int run=0;
  2312. int runs[w*h];
  2313. int run_index=0;
  2314. int max_index;
  2315. for(y=0; y<h; y++){
  2316. for(x=0; x<w; x++){
  2317. int v, p=0;
  2318. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2319. v= src[x + y*stride];
  2320. if(y){
  2321. t= src[x + (y-1)*stride];
  2322. if(x){
  2323. lt= src[x - 1 + (y-1)*stride];
  2324. }
  2325. if(x + 1 < w){
  2326. rt= src[x + 1 + (y-1)*stride];
  2327. }
  2328. }
  2329. if(x){
  2330. l= src[x - 1 + y*stride];
  2331. /*if(x > 1){
  2332. if(orientation==1) ll= src[y + (x-2)*stride];
  2333. else ll= src[x - 2 + y*stride];
  2334. }*/
  2335. }
  2336. if(parent){
  2337. int px= x>>1;
  2338. int py= y>>1;
  2339. if(px<b->parent->width && py<b->parent->height)
  2340. p= parent[px + py*2*stride];
  2341. }
  2342. if(!(/*ll|*/l|lt|t|rt|p)){
  2343. if(v){
  2344. runs[run_index++]= run;
  2345. run=0;
  2346. }else{
  2347. run++;
  2348. }
  2349. }
  2350. }
  2351. }
  2352. max_index= run_index;
  2353. runs[run_index++]= run;
  2354. run_index=0;
  2355. run= runs[run_index++];
  2356. put_symbol2(&s->c, b->state[30], max_index, 0);
  2357. if(run_index <= max_index)
  2358. put_symbol2(&s->c, b->state[1], run, 3);
  2359. for(y=0; y<h; y++){
  2360. if(s->c.bytestream_end - s->c.bytestream < w*40){
  2361. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2362. return -1;
  2363. }
  2364. for(x=0; x<w; x++){
  2365. int v, p=0;
  2366. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2367. v= src[x + y*stride];
  2368. if(y){
  2369. t= src[x + (y-1)*stride];
  2370. if(x){
  2371. lt= src[x - 1 + (y-1)*stride];
  2372. }
  2373. if(x + 1 < w){
  2374. rt= src[x + 1 + (y-1)*stride];
  2375. }
  2376. }
  2377. if(x){
  2378. l= src[x - 1 + y*stride];
  2379. /*if(x > 1){
  2380. if(orientation==1) ll= src[y + (x-2)*stride];
  2381. else ll= src[x - 2 + y*stride];
  2382. }*/
  2383. }
  2384. if(parent){
  2385. int px= x>>1;
  2386. int py= y>>1;
  2387. if(px<b->parent->width && py<b->parent->height)
  2388. p= parent[px + py*2*stride];
  2389. }
  2390. if(/*ll|*/l|lt|t|rt|p){
  2391. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2392. put_rac(&s->c, &b->state[0][context], !!v);
  2393. }else{
  2394. if(!run){
  2395. run= runs[run_index++];
  2396. if(run_index <= max_index)
  2397. put_symbol2(&s->c, b->state[1], run, 3);
  2398. assert(v);
  2399. }else{
  2400. run--;
  2401. assert(!v);
  2402. }
  2403. }
  2404. if(v){
  2405. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2406. int l2= 2*FFABS(l) + (l<0);
  2407. int t2= 2*FFABS(t) + (t<0);
  2408. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  2409. put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
  2410. }
  2411. }
  2412. }
  2413. }
  2414. return 0;
  2415. }
  2416. static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2417. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  2418. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  2419. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  2420. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  2421. }
  2422. static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, const uint8_t *obmc_edged, int *best_rd){
  2423. const int b_stride= s->b_width << s->block_max_depth;
  2424. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2425. BlockNode backup= *block;
  2426. int rd, index, value;
  2427. assert(mb_x>=0 && mb_y>=0);
  2428. assert(mb_x<b_stride);
  2429. if(intra){
  2430. block->color[0] = p[0];
  2431. block->color[1] = p[1];
  2432. block->color[2] = p[2];
  2433. block->type |= BLOCK_INTRA;
  2434. }else{
  2435. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2436. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2437. if(s->me_cache[index] == value)
  2438. return 0;
  2439. s->me_cache[index]= value;
  2440. block->mx= p[0];
  2441. block->my= p[1];
  2442. block->type &= ~BLOCK_INTRA;
  2443. }
  2444. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2445. //FIXME chroma
  2446. if(rd < *best_rd){
  2447. *best_rd= rd;
  2448. return 1;
  2449. }else{
  2450. *block= backup;
  2451. return 0;
  2452. }
  2453. }
  2454. /* special case for int[2] args we discard afterwards,
  2455. * fixes compilation problem with gcc 2.95 */
  2456. static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, const uint8_t *obmc_edged, int *best_rd){
  2457. int p[2] = {p0, p1};
  2458. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2459. }
  2460. static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
  2461. const int b_stride= s->b_width << s->block_max_depth;
  2462. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2463. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2464. int rd, index, value;
  2465. assert(mb_x>=0 && mb_y>=0);
  2466. assert(mb_x<b_stride);
  2467. assert(((mb_x|mb_y)&1) == 0);
  2468. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2469. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2470. if(s->me_cache[index] == value)
  2471. return 0;
  2472. s->me_cache[index]= value;
  2473. block->mx= p0;
  2474. block->my= p1;
  2475. block->ref= ref;
  2476. block->type &= ~BLOCK_INTRA;
  2477. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2478. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2479. //FIXME chroma
  2480. if(rd < *best_rd){
  2481. *best_rd= rd;
  2482. return 1;
  2483. }else{
  2484. block[0]= backup[0];
  2485. block[1]= backup[1];
  2486. block[b_stride]= backup[2];
  2487. block[b_stride+1]= backup[3];
  2488. return 0;
  2489. }
  2490. }
  2491. static void iterative_me(SnowContext *s){
  2492. int pass, mb_x, mb_y;
  2493. const int b_width = s->b_width << s->block_max_depth;
  2494. const int b_height= s->b_height << s->block_max_depth;
  2495. const int b_stride= b_width;
  2496. int color[3];
  2497. {
  2498. RangeCoder r = s->c;
  2499. uint8_t state[sizeof(s->block_state)];
  2500. memcpy(state, s->block_state, sizeof(s->block_state));
  2501. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2502. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2503. encode_q_branch(s, 0, mb_x, mb_y);
  2504. s->c = r;
  2505. memcpy(s->block_state, state, sizeof(s->block_state));
  2506. }
  2507. for(pass=0; pass<25; pass++){
  2508. int change= 0;
  2509. for(mb_y= 0; mb_y<b_height; mb_y++){
  2510. for(mb_x= 0; mb_x<b_width; mb_x++){
  2511. int dia_change, i, j, ref;
  2512. int best_rd= INT_MAX, ref_rd;
  2513. BlockNode backup, ref_b;
  2514. const int index= mb_x + mb_y * b_stride;
  2515. BlockNode *block= &s->block[index];
  2516. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2517. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2518. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2519. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2520. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2521. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2522. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2523. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2524. const int b_w= (MB_SIZE >> s->block_max_depth);
  2525. uint8_t obmc_edged[b_w*2][b_w*2];
  2526. if(pass && (block->type & BLOCK_OPT))
  2527. continue;
  2528. block->type |= BLOCK_OPT;
  2529. backup= *block;
  2530. if(!s->me_cache_generation)
  2531. memset(s->me_cache, 0, sizeof(s->me_cache));
  2532. s->me_cache_generation += 1<<22;
  2533. //FIXME precalculate
  2534. {
  2535. int x, y;
  2536. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2537. if(mb_x==0)
  2538. for(y=0; y<b_w*2; y++)
  2539. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2540. if(mb_x==b_stride-1)
  2541. for(y=0; y<b_w*2; y++)
  2542. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2543. if(mb_y==0){
  2544. for(x=0; x<b_w*2; x++)
  2545. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  2546. for(y=1; y<b_w; y++)
  2547. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  2548. }
  2549. if(mb_y==b_height-1){
  2550. for(x=0; x<b_w*2; x++)
  2551. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  2552. for(y=b_w; y<b_w*2-1; y++)
  2553. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  2554. }
  2555. }
  2556. //skip stuff outside the picture
  2557. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  2558. uint8_t *src= s-> input_picture.data[0];
  2559. uint8_t *dst= s->current_picture.data[0];
  2560. const int stride= s->current_picture.linesize[0];
  2561. const int block_w= MB_SIZE >> s->block_max_depth;
  2562. const int sx= block_w*mb_x - block_w/2;
  2563. const int sy= block_w*mb_y - block_w/2;
  2564. const int w= s->plane[0].width;
  2565. const int h= s->plane[0].height;
  2566. int y;
  2567. for(y=sy; y<0; y++)
  2568. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2569. for(y=h; y<sy+block_w*2; y++)
  2570. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2571. if(sx<0){
  2572. for(y=sy; y<sy+block_w*2; y++)
  2573. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  2574. }
  2575. if(sx+block_w*2 > w){
  2576. for(y=sy; y<sy+block_w*2; y++)
  2577. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  2578. }
  2579. }
  2580. // intra(black) = neighbors' contribution to the current block
  2581. for(i=0; i<3; i++)
  2582. color[i]= get_dc(s, mb_x, mb_y, i);
  2583. // get previous score (cannot be cached due to OBMC)
  2584. if(pass > 0 && (block->type&BLOCK_INTRA)){
  2585. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  2586. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  2587. }else
  2588. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  2589. ref_b= *block;
  2590. ref_rd= best_rd;
  2591. for(ref=0; ref < s->ref_frames; ref++){
  2592. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  2593. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  2594. continue;
  2595. block->ref= ref;
  2596. best_rd= INT_MAX;
  2597. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  2598. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  2599. if(tb)
  2600. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  2601. if(lb)
  2602. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  2603. if(rb)
  2604. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  2605. if(bb)
  2606. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  2607. /* fullpel ME */
  2608. //FIXME avoid subpel interpolation / round to nearest integer
  2609. do{
  2610. dia_change=0;
  2611. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  2612. for(j=0; j<i; j++){
  2613. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2614. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2615. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2616. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2617. }
  2618. }
  2619. }while(dia_change);
  2620. /* subpel ME */
  2621. do{
  2622. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  2623. dia_change=0;
  2624. for(i=0; i<8; i++)
  2625. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  2626. }while(dia_change);
  2627. //FIXME or try the standard 2 pass qpel or similar
  2628. mvr[0][0]= block->mx;
  2629. mvr[0][1]= block->my;
  2630. if(ref_rd > best_rd){
  2631. ref_rd= best_rd;
  2632. ref_b= *block;
  2633. }
  2634. }
  2635. best_rd= ref_rd;
  2636. *block= ref_b;
  2637. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  2638. //FIXME RD style color selection
  2639. if(!same_block(block, &backup)){
  2640. if(tb ) tb ->type &= ~BLOCK_OPT;
  2641. if(lb ) lb ->type &= ~BLOCK_OPT;
  2642. if(rb ) rb ->type &= ~BLOCK_OPT;
  2643. if(bb ) bb ->type &= ~BLOCK_OPT;
  2644. if(tlb) tlb->type &= ~BLOCK_OPT;
  2645. if(trb) trb->type &= ~BLOCK_OPT;
  2646. if(blb) blb->type &= ~BLOCK_OPT;
  2647. if(brb) brb->type &= ~BLOCK_OPT;
  2648. change ++;
  2649. }
  2650. }
  2651. }
  2652. av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  2653. if(!change)
  2654. break;
  2655. }
  2656. if(s->block_max_depth == 1){
  2657. int change= 0;
  2658. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  2659. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  2660. int i;
  2661. int best_rd, init_rd;
  2662. const int index= mb_x + mb_y * b_stride;
  2663. BlockNode *b[4];
  2664. b[0]= &s->block[index];
  2665. b[1]= b[0]+1;
  2666. b[2]= b[0]+b_stride;
  2667. b[3]= b[2]+1;
  2668. if(same_block(b[0], b[1]) &&
  2669. same_block(b[0], b[2]) &&
  2670. same_block(b[0], b[3]))
  2671. continue;
  2672. if(!s->me_cache_generation)
  2673. memset(s->me_cache, 0, sizeof(s->me_cache));
  2674. s->me_cache_generation += 1<<22;
  2675. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  2676. //FIXME more multiref search?
  2677. check_4block_inter(s, mb_x, mb_y,
  2678. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  2679. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  2680. for(i=0; i<4; i++)
  2681. if(!(b[i]->type&BLOCK_INTRA))
  2682. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  2683. if(init_rd != best_rd)
  2684. change++;
  2685. }
  2686. }
  2687. av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  2688. }
  2689. }
  2690. static void encode_blocks(SnowContext *s, int search){
  2691. int x, y;
  2692. int w= s->b_width;
  2693. int h= s->b_height;
  2694. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  2695. iterative_me(s);
  2696. for(y=0; y<h; y++){
  2697. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  2698. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2699. return;
  2700. }
  2701. for(x=0; x<w; x++){
  2702. if(s->avctx->me_method == ME_ITER || !search)
  2703. encode_q_branch2(s, 0, x, y);
  2704. else
  2705. encode_q_branch (s, 0, x, y);
  2706. }
  2707. }
  2708. }
  2709. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  2710. const int w= b->width;
  2711. const int h= b->height;
  2712. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2713. const int qmul= qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  2714. int x,y, thres1, thres2;
  2715. if(s->qlog == LOSSLESS_QLOG){
  2716. for(y=0; y<h; y++)
  2717. for(x=0; x<w; x++)
  2718. dst[x + y*stride]= src[x + y*stride];
  2719. return;
  2720. }
  2721. bias= bias ? 0 : (3*qmul)>>3;
  2722. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  2723. thres2= 2*thres1;
  2724. if(!bias){
  2725. for(y=0; y<h; y++){
  2726. for(x=0; x<w; x++){
  2727. int i= src[x + y*stride];
  2728. if((unsigned)(i+thres1) > thres2){
  2729. if(i>=0){
  2730. i<<= QEXPSHIFT;
  2731. i/= qmul; //FIXME optimize
  2732. dst[x + y*stride]= i;
  2733. }else{
  2734. i= -i;
  2735. i<<= QEXPSHIFT;
  2736. i/= qmul; //FIXME optimize
  2737. dst[x + y*stride]= -i;
  2738. }
  2739. }else
  2740. dst[x + y*stride]= 0;
  2741. }
  2742. }
  2743. }else{
  2744. for(y=0; y<h; y++){
  2745. for(x=0; x<w; x++){
  2746. int i= src[x + y*stride];
  2747. if((unsigned)(i+thres1) > thres2){
  2748. if(i>=0){
  2749. i<<= QEXPSHIFT;
  2750. i= (i + bias) / qmul; //FIXME optimize
  2751. dst[x + y*stride]= i;
  2752. }else{
  2753. i= -i;
  2754. i<<= QEXPSHIFT;
  2755. i= (i + bias) / qmul; //FIXME optimize
  2756. dst[x + y*stride]= -i;
  2757. }
  2758. }else
  2759. dst[x + y*stride]= 0;
  2760. }
  2761. }
  2762. }
  2763. }
  2764. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  2765. const int w= b->width;
  2766. const int h= b->height;
  2767. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2768. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  2769. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2770. int x,y;
  2771. if(s->qlog == LOSSLESS_QLOG) return;
  2772. for(y=0; y<h; y++){
  2773. for(x=0; x<w; x++){
  2774. int i= src[x + y*stride];
  2775. if(i<0){
  2776. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2777. }else if(i>0){
  2778. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2779. }
  2780. }
  2781. }
  2782. }
  2783. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  2784. const int w= b->width;
  2785. const int h= b->height;
  2786. int x,y;
  2787. for(y=h-1; y>=0; y--){
  2788. for(x=w-1; x>=0; x--){
  2789. int i= x + y*stride;
  2790. if(x){
  2791. if(use_median){
  2792. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2793. else src[i] -= src[i - 1];
  2794. }else{
  2795. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2796. else src[i] -= src[i - 1];
  2797. }
  2798. }else{
  2799. if(y) src[i] -= src[i - stride];
  2800. }
  2801. }
  2802. }
  2803. }
  2804. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  2805. const int w= b->width;
  2806. const int h= b->height;
  2807. int x,y;
  2808. for(y=0; y<h; y++){
  2809. for(x=0; x<w; x++){
  2810. int i= x + y*stride;
  2811. if(x){
  2812. if(use_median){
  2813. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2814. else src[i] += src[i - 1];
  2815. }else{
  2816. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2817. else src[i] += src[i - 1];
  2818. }
  2819. }else{
  2820. if(y) src[i] += src[i - stride];
  2821. }
  2822. }
  2823. }
  2824. }
  2825. static void encode_qlogs(SnowContext *s){
  2826. int plane_index, level, orientation;
  2827. for(plane_index=0; plane_index<2; plane_index++){
  2828. for(level=0; level<s->spatial_decomposition_count; level++){
  2829. for(orientation=level ? 1:0; orientation<4; orientation++){
  2830. if(orientation==2) continue;
  2831. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  2832. }
  2833. }
  2834. }
  2835. }
  2836. static void encode_header(SnowContext *s){
  2837. int plane_index, i;
  2838. uint8_t kstate[32];
  2839. memset(kstate, MID_STATE, sizeof(kstate));
  2840. put_rac(&s->c, kstate, s->keyframe);
  2841. if(s->keyframe || s->always_reset){
  2842. reset_contexts(s);
  2843. s->last_spatial_decomposition_type=
  2844. s->last_qlog=
  2845. s->last_qbias=
  2846. s->last_mv_scale=
  2847. s->last_block_max_depth= 0;
  2848. for(plane_index=0; plane_index<2; plane_index++){
  2849. Plane *p= &s->plane[plane_index];
  2850. p->last_htaps=0;
  2851. p->last_diag_mc=0;
  2852. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  2853. }
  2854. }
  2855. if(s->keyframe){
  2856. put_symbol(&s->c, s->header_state, s->version, 0);
  2857. put_rac(&s->c, s->header_state, s->always_reset);
  2858. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  2859. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  2860. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2861. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  2862. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  2863. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  2864. put_rac(&s->c, s->header_state, s->spatial_scalability);
  2865. // put_rac(&s->c, s->header_state, s->rate_scalability);
  2866. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  2867. encode_qlogs(s);
  2868. }
  2869. if(!s->keyframe){
  2870. int update_mc=0;
  2871. for(plane_index=0; plane_index<2; plane_index++){
  2872. Plane *p= &s->plane[plane_index];
  2873. update_mc |= p->last_htaps != p->htaps;
  2874. update_mc |= p->last_diag_mc != p->diag_mc;
  2875. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  2876. }
  2877. put_rac(&s->c, s->header_state, update_mc);
  2878. if(update_mc){
  2879. for(plane_index=0; plane_index<2; plane_index++){
  2880. Plane *p= &s->plane[plane_index];
  2881. put_rac(&s->c, s->header_state, p->diag_mc);
  2882. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  2883. for(i= p->htaps/2; i; i--)
  2884. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  2885. }
  2886. }
  2887. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  2888. put_rac(&s->c, s->header_state, 1);
  2889. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2890. encode_qlogs(s);
  2891. }else
  2892. put_rac(&s->c, s->header_state, 0);
  2893. }
  2894. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  2895. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  2896. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  2897. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  2898. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  2899. }
  2900. static void update_last_header_values(SnowContext *s){
  2901. int plane_index;
  2902. if(!s->keyframe){
  2903. for(plane_index=0; plane_index<2; plane_index++){
  2904. Plane *p= &s->plane[plane_index];
  2905. p->last_diag_mc= p->diag_mc;
  2906. p->last_htaps = p->htaps;
  2907. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  2908. }
  2909. }
  2910. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  2911. s->last_qlog = s->qlog;
  2912. s->last_qbias = s->qbias;
  2913. s->last_mv_scale = s->mv_scale;
  2914. s->last_block_max_depth = s->block_max_depth;
  2915. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  2916. }
  2917. static int qscale2qlog(int qscale){
  2918. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  2919. + 61*QROOT/8; //<64 >60
  2920. }
  2921. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  2922. {
  2923. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  2924. * FIXME we know exact mv bits at this point,
  2925. * but ratecontrol isn't set up to include them. */
  2926. uint32_t coef_sum= 0;
  2927. int level, orientation, delta_qlog;
  2928. for(level=0; level<s->spatial_decomposition_count; level++){
  2929. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2930. SubBand *b= &s->plane[0].band[level][orientation];
  2931. IDWTELEM *buf= b->ibuf;
  2932. const int w= b->width;
  2933. const int h= b->height;
  2934. const int stride= b->stride;
  2935. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  2936. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  2937. const int qdiv= (1<<16)/qmul;
  2938. int x, y;
  2939. //FIXME this is ugly
  2940. for(y=0; y<h; y++)
  2941. for(x=0; x<w; x++)
  2942. buf[x+y*stride]= b->buf[x+y*stride];
  2943. if(orientation==0)
  2944. decorrelate(s, b, buf, stride, 1, 0);
  2945. for(y=0; y<h; y++)
  2946. for(x=0; x<w; x++)
  2947. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  2948. }
  2949. }
  2950. /* ugly, ratecontrol just takes a sqrt again */
  2951. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  2952. assert(coef_sum < INT_MAX);
  2953. if(pict->pict_type == AV_PICTURE_TYPE_I){
  2954. s->m.current_picture.mb_var_sum= coef_sum;
  2955. s->m.current_picture.mc_mb_var_sum= 0;
  2956. }else{
  2957. s->m.current_picture.mc_mb_var_sum= coef_sum;
  2958. s->m.current_picture.mb_var_sum= 0;
  2959. }
  2960. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  2961. if (pict->quality < 0)
  2962. return INT_MIN;
  2963. s->lambda= pict->quality * 3/2;
  2964. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  2965. s->qlog+= delta_qlog;
  2966. return delta_qlog;
  2967. }
  2968. static void calculate_visual_weight(SnowContext *s, Plane *p){
  2969. int width = p->width;
  2970. int height= p->height;
  2971. int level, orientation, x, y;
  2972. for(level=0; level<s->spatial_decomposition_count; level++){
  2973. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2974. SubBand *b= &p->band[level][orientation];
  2975. IDWTELEM *ibuf= b->ibuf;
  2976. int64_t error=0;
  2977. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  2978. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  2979. ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2980. for(y=0; y<height; y++){
  2981. for(x=0; x<width; x++){
  2982. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  2983. error += d*d;
  2984. }
  2985. }
  2986. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  2987. }
  2988. }
  2989. }
  2990. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  2991. SnowContext *s = avctx->priv_data;
  2992. RangeCoder * const c= &s->c;
  2993. AVFrame *pict = data;
  2994. const int width= s->avctx->width;
  2995. const int height= s->avctx->height;
  2996. int level, orientation, plane_index, i, y;
  2997. uint8_t rc_header_bak[sizeof(s->header_state)];
  2998. uint8_t rc_block_bak[sizeof(s->block_state)];
  2999. ff_init_range_encoder(c, buf, buf_size);
  3000. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3001. for(i=0; i<3; i++){
  3002. int shift= !!i;
  3003. for(y=0; y<(height>>shift); y++)
  3004. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3005. &pict->data[i][y * pict->linesize[i]],
  3006. width>>shift);
  3007. }
  3008. s->new_picture = *pict;
  3009. s->m.picture_number= avctx->frame_number;
  3010. if(avctx->flags&CODEC_FLAG_PASS2){
  3011. s->m.pict_type =
  3012. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3013. s->keyframe= pict->pict_type==AV_PICTURE_TYPE_I;
  3014. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  3015. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3016. if (pict->quality < 0)
  3017. return -1;
  3018. }
  3019. }else{
  3020. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3021. s->m.pict_type=
  3022. pict->pict_type= s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  3023. }
  3024. if(s->pass1_rc && avctx->frame_number == 0)
  3025. pict->quality= 2*FF_QP2LAMBDA;
  3026. if(pict->quality){
  3027. s->qlog= qscale2qlog(pict->quality);
  3028. s->lambda = pict->quality * 3/2;
  3029. }
  3030. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3031. s->qlog= LOSSLESS_QLOG;
  3032. s->lambda = 0;
  3033. }//else keep previous frame's qlog until after motion estimation
  3034. frame_start(s);
  3035. s->m.current_picture_ptr= &s->m.current_picture;
  3036. s->m.last_picture.pts= s->m.current_picture.pts;
  3037. s->m.current_picture.pts= pict->pts;
  3038. if(pict->pict_type == AV_PICTURE_TYPE_P){
  3039. int block_width = (width +15)>>4;
  3040. int block_height= (height+15)>>4;
  3041. int stride= s->current_picture.linesize[0];
  3042. assert(s->current_picture.data[0]);
  3043. assert(s->last_picture[0].data[0]);
  3044. s->m.avctx= s->avctx;
  3045. s->m.current_picture.data[0]= s->current_picture.data[0];
  3046. s->m. last_picture.data[0]= s->last_picture[0].data[0];
  3047. s->m. new_picture.data[0]= s-> input_picture.data[0];
  3048. s->m. last_picture_ptr= &s->m. last_picture;
  3049. s->m.linesize=
  3050. s->m. last_picture.linesize[0]=
  3051. s->m. new_picture.linesize[0]=
  3052. s->m.current_picture.linesize[0]= stride;
  3053. s->m.uvlinesize= s->current_picture.linesize[1];
  3054. s->m.width = width;
  3055. s->m.height= height;
  3056. s->m.mb_width = block_width;
  3057. s->m.mb_height= block_height;
  3058. s->m.mb_stride= s->m.mb_width+1;
  3059. s->m.b8_stride= 2*s->m.mb_width+1;
  3060. s->m.f_code=1;
  3061. s->m.pict_type= pict->pict_type;
  3062. s->m.me_method= s->avctx->me_method;
  3063. s->m.me.scene_change_score=0;
  3064. s->m.flags= s->avctx->flags;
  3065. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3066. s->m.out_format= FMT_H263;
  3067. s->m.unrestricted_mv= 1;
  3068. s->m.lambda = s->lambda;
  3069. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3070. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3071. s->m.dsp= s->dsp; //move
  3072. ff_init_me(&s->m);
  3073. s->dsp= s->m.dsp;
  3074. }
  3075. if(s->pass1_rc){
  3076. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  3077. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  3078. }
  3079. redo_frame:
  3080. if(pict->pict_type == AV_PICTURE_TYPE_I)
  3081. s->spatial_decomposition_count= 5;
  3082. else
  3083. s->spatial_decomposition_count= 5;
  3084. s->m.pict_type = pict->pict_type;
  3085. s->qbias= pict->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
  3086. common_init_after_header(avctx);
  3087. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3088. for(plane_index=0; plane_index<3; plane_index++){
  3089. calculate_visual_weight(s, &s->plane[plane_index]);
  3090. }
  3091. }
  3092. encode_header(s);
  3093. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3094. encode_blocks(s, 1);
  3095. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3096. for(plane_index=0; plane_index<3; plane_index++){
  3097. Plane *p= &s->plane[plane_index];
  3098. int w= p->width;
  3099. int h= p->height;
  3100. int x, y;
  3101. // int bits= put_bits_count(&s->c.pb);
  3102. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3103. //FIXME optimize
  3104. if(pict->data[plane_index]) //FIXME gray hack
  3105. for(y=0; y<h; y++){
  3106. for(x=0; x<w; x++){
  3107. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3108. }
  3109. }
  3110. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  3111. if( plane_index==0
  3112. && pict->pict_type == AV_PICTURE_TYPE_P
  3113. && !(avctx->flags&CODEC_FLAG_PASS2)
  3114. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3115. ff_init_range_encoder(c, buf, buf_size);
  3116. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3117. pict->pict_type= AV_PICTURE_TYPE_I;
  3118. s->keyframe=1;
  3119. s->current_picture.key_frame=1;
  3120. goto redo_frame;
  3121. }
  3122. if(s->qlog == LOSSLESS_QLOG){
  3123. for(y=0; y<h; y++){
  3124. for(x=0; x<w; x++){
  3125. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3126. }
  3127. }
  3128. }else{
  3129. for(y=0; y<h; y++){
  3130. for(x=0; x<w; x++){
  3131. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  3132. }
  3133. }
  3134. }
  3135. /* if(QUANTIZE2)
  3136. dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
  3137. else*/
  3138. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3139. if(s->pass1_rc && plane_index==0){
  3140. int delta_qlog = ratecontrol_1pass(s, pict);
  3141. if (delta_qlog <= INT_MIN)
  3142. return -1;
  3143. if(delta_qlog){
  3144. //reordering qlog in the bitstream would eliminate this reset
  3145. ff_init_range_encoder(c, buf, buf_size);
  3146. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  3147. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  3148. encode_header(s);
  3149. encode_blocks(s, 0);
  3150. }
  3151. }
  3152. for(level=0; level<s->spatial_decomposition_count; level++){
  3153. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3154. SubBand *b= &p->band[level][orientation];
  3155. if(!QUANTIZE2)
  3156. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  3157. if(orientation==0)
  3158. decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == AV_PICTURE_TYPE_P, 0);
  3159. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  3160. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3161. if(orientation==0)
  3162. correlate(s, b, b->ibuf, b->stride, 1, 0);
  3163. }
  3164. }
  3165. for(level=0; level<s->spatial_decomposition_count; level++){
  3166. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3167. SubBand *b= &p->band[level][orientation];
  3168. dequantize(s, b, b->ibuf, b->stride);
  3169. }
  3170. }
  3171. ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3172. if(s->qlog == LOSSLESS_QLOG){
  3173. for(y=0; y<h; y++){
  3174. for(x=0; x<w; x++){
  3175. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  3176. }
  3177. }
  3178. }
  3179. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3180. }else{
  3181. //ME/MC only
  3182. if(pict->pict_type == AV_PICTURE_TYPE_I){
  3183. for(y=0; y<h; y++){
  3184. for(x=0; x<w; x++){
  3185. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3186. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3187. }
  3188. }
  3189. }else{
  3190. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  3191. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3192. }
  3193. }
  3194. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3195. int64_t error= 0;
  3196. if(pict->data[plane_index]) //FIXME gray hack
  3197. for(y=0; y<h; y++){
  3198. for(x=0; x<w; x++){
  3199. int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3200. error += d*d;
  3201. }
  3202. }
  3203. s->avctx->error[plane_index] += error;
  3204. s->current_picture.error[plane_index] = error;
  3205. }
  3206. }
  3207. update_last_header_values(s);
  3208. release_buffer(avctx);
  3209. s->current_picture.coded_picture_number = avctx->frame_number;
  3210. s->current_picture.pict_type = pict->pict_type;
  3211. s->current_picture.quality = pict->quality;
  3212. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3213. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3214. s->m.current_picture.display_picture_number =
  3215. s->m.current_picture.coded_picture_number = avctx->frame_number;
  3216. s->m.current_picture.quality = pict->quality;
  3217. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3218. if(s->pass1_rc)
  3219. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  3220. return -1;
  3221. if(avctx->flags&CODEC_FLAG_PASS1)
  3222. ff_write_pass1_stats(&s->m);
  3223. s->m.last_pict_type = s->m.pict_type;
  3224. avctx->frame_bits = s->m.frame_bits;
  3225. avctx->mv_bits = s->m.mv_bits;
  3226. avctx->misc_bits = s->m.misc_bits;
  3227. avctx->p_tex_bits = s->m.p_tex_bits;
  3228. emms_c();
  3229. return ff_rac_terminate(c);
  3230. }
  3231. static av_cold int encode_end(AVCodecContext *avctx)
  3232. {
  3233. SnowContext *s = avctx->priv_data;
  3234. common_end(s);
  3235. if (s->input_picture.data[0])
  3236. avctx->release_buffer(avctx, &s->input_picture);
  3237. av_free(avctx->stats_out);
  3238. return 0;
  3239. }
  3240. AVCodec ff_snow_encoder = {
  3241. "snow",
  3242. AVMEDIA_TYPE_VIDEO,
  3243. CODEC_ID_SNOW,
  3244. sizeof(SnowContext),
  3245. encode_init,
  3246. encode_frame,
  3247. encode_end,
  3248. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  3249. };
  3250. #endif
  3251. #ifdef TEST
  3252. #undef malloc
  3253. #undef free
  3254. #undef printf
  3255. #include "libavutil/lfg.h"
  3256. #include "libavutil/mathematics.h"
  3257. int main(void){
  3258. int width=256;
  3259. int height=256;
  3260. int buffer[2][width*height];
  3261. SnowContext s;
  3262. int i;
  3263. AVLFG prng;
  3264. s.spatial_decomposition_count=6;
  3265. s.spatial_decomposition_type=1;
  3266. av_lfg_init(&prng, 1);
  3267. printf("testing 5/3 DWT\n");
  3268. for(i=0; i<width*height; i++)
  3269. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  3270. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3271. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3272. for(i=0; i<width*height; i++)
  3273. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  3274. printf("testing 9/7 DWT\n");
  3275. s.spatial_decomposition_type=0;
  3276. for(i=0; i<width*height; i++)
  3277. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  3278. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3279. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3280. for(i=0; i<width*height; i++)
  3281. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  3282. {
  3283. int level, orientation, x, y;
  3284. int64_t errors[8][4];
  3285. int64_t g=0;
  3286. memset(errors, 0, sizeof(errors));
  3287. s.spatial_decomposition_count=3;
  3288. s.spatial_decomposition_type=0;
  3289. for(level=0; level<s.spatial_decomposition_count; level++){
  3290. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3291. int w= width >> (s.spatial_decomposition_count-level);
  3292. int h= height >> (s.spatial_decomposition_count-level);
  3293. int stride= width << (s.spatial_decomposition_count-level);
  3294. DWTELEM *buf= buffer[0];
  3295. int64_t error=0;
  3296. if(orientation&1) buf+=w;
  3297. if(orientation>1) buf+=stride>>1;
  3298. memset(buffer[0], 0, sizeof(int)*width*height);
  3299. buf[w/2 + h/2*stride]= 256*256;
  3300. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3301. for(y=0; y<height; y++){
  3302. for(x=0; x<width; x++){
  3303. int64_t d= buffer[0][x + y*width];
  3304. error += d*d;
  3305. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  3306. }
  3307. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  3308. }
  3309. error= (int)(sqrt(error)+0.5);
  3310. errors[level][orientation]= error;
  3311. if(g) g=av_gcd(g, error);
  3312. else g= error;
  3313. }
  3314. }
  3315. printf("static int const visual_weight[][4]={\n");
  3316. for(level=0; level<s.spatial_decomposition_count; level++){
  3317. printf(" {");
  3318. for(orientation=0; orientation<4; orientation++){
  3319. printf("%8"PRId64",", errors[level][orientation]/g);
  3320. }
  3321. printf("},\n");
  3322. }
  3323. printf("};\n");
  3324. {
  3325. int level=2;
  3326. int w= width >> (s.spatial_decomposition_count-level);
  3327. //int h= height >> (s.spatial_decomposition_count-level);
  3328. int stride= width << (s.spatial_decomposition_count-level);
  3329. DWTELEM *buf= buffer[0];
  3330. int64_t error=0;
  3331. buf+=w;
  3332. buf+=stride>>1;
  3333. memset(buffer[0], 0, sizeof(int)*width*height);
  3334. for(y=0; y<height; y++){
  3335. for(x=0; x<width; x++){
  3336. int tab[4]={0,2,3,1};
  3337. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  3338. }
  3339. }
  3340. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3341. for(y=0; y<height; y++){
  3342. for(x=0; x<width; x++){
  3343. int64_t d= buffer[0][x + y*width];
  3344. error += d*d;
  3345. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  3346. }
  3347. if(FFABS(height/2-y)<9) printf("\n");
  3348. }
  3349. }
  3350. }
  3351. return 0;
  3352. }
  3353. #endif /* TEST */