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.

3690 lines
135KB

  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. .name = "snow",
  1693. .type = AVMEDIA_TYPE_VIDEO,
  1694. .id = CODEC_ID_SNOW,
  1695. .priv_data_size = sizeof(SnowContext),
  1696. .init = decode_init,
  1697. .close = decode_end,
  1698. .decode = decode_frame,
  1699. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1700. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  1701. };
  1702. #if CONFIG_SNOW_ENCODER
  1703. static av_cold int encode_init(AVCodecContext *avctx)
  1704. {
  1705. SnowContext *s = avctx->priv_data;
  1706. int plane_index;
  1707. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  1708. av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  1709. "Use vstrict=-2 / -strict -2 to use it anyway.\n");
  1710. return -1;
  1711. }
  1712. if(avctx->prediction_method == DWT_97
  1713. && (avctx->flags & CODEC_FLAG_QSCALE)
  1714. && avctx->global_quality == 0){
  1715. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  1716. return -1;
  1717. }
  1718. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  1719. s->mv_scale = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  1720. s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
  1721. for(plane_index=0; plane_index<3; plane_index++){
  1722. s->plane[plane_index].diag_mc= 1;
  1723. s->plane[plane_index].htaps= 6;
  1724. s->plane[plane_index].hcoeff[0]= 40;
  1725. s->plane[plane_index].hcoeff[1]= -10;
  1726. s->plane[plane_index].hcoeff[2]= 2;
  1727. s->plane[plane_index].fast_mc= 1;
  1728. }
  1729. common_init(avctx);
  1730. alloc_blocks(s);
  1731. s->version=0;
  1732. s->m.avctx = avctx;
  1733. s->m.flags = avctx->flags;
  1734. s->m.bit_rate= avctx->bit_rate;
  1735. s->m.me.temp =
  1736. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  1737. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  1738. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  1739. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  1740. h263_encode_init(&s->m); //mv_penalty
  1741. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  1742. if(avctx->flags&CODEC_FLAG_PASS1){
  1743. if(!avctx->stats_out)
  1744. avctx->stats_out = av_mallocz(256);
  1745. }
  1746. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  1747. if(ff_rate_control_init(&s->m) < 0)
  1748. return -1;
  1749. }
  1750. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  1751. avctx->coded_frame= &s->current_picture;
  1752. switch(avctx->pix_fmt){
  1753. // case PIX_FMT_YUV444P:
  1754. // case PIX_FMT_YUV422P:
  1755. case PIX_FMT_YUV420P:
  1756. case PIX_FMT_GRAY8:
  1757. // case PIX_FMT_YUV411P:
  1758. // case PIX_FMT_YUV410P:
  1759. s->colorspace_type= 0;
  1760. break;
  1761. /* case PIX_FMT_RGB32:
  1762. s->colorspace= 1;
  1763. break;*/
  1764. default:
  1765. av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
  1766. return -1;
  1767. }
  1768. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  1769. s->chroma_h_shift= 1;
  1770. s->chroma_v_shift= 1;
  1771. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  1772. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  1773. s->avctx->get_buffer(s->avctx, &s->input_picture);
  1774. if(s->avctx->me_method == ME_ITER){
  1775. int i;
  1776. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  1777. for(i=0; i<s->max_ref_frames; i++){
  1778. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  1779. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  1780. }
  1781. }
  1782. return 0;
  1783. }
  1784. //near copy & paste from dsputil, FIXME
  1785. static int pix_sum(uint8_t * pix, int line_size, int w)
  1786. {
  1787. int s, i, j;
  1788. s = 0;
  1789. for (i = 0; i < w; i++) {
  1790. for (j = 0; j < w; j++) {
  1791. s += pix[0];
  1792. pix ++;
  1793. }
  1794. pix += line_size - w;
  1795. }
  1796. return s;
  1797. }
  1798. //near copy & paste from dsputil, FIXME
  1799. static int pix_norm1(uint8_t * pix, int line_size, int w)
  1800. {
  1801. int s, i, j;
  1802. uint32_t *sq = ff_squareTbl + 256;
  1803. s = 0;
  1804. for (i = 0; i < w; i++) {
  1805. for (j = 0; j < w; j ++) {
  1806. s += sq[pix[0]];
  1807. pix ++;
  1808. }
  1809. pix += line_size - w;
  1810. }
  1811. return s;
  1812. }
  1813. //FIXME copy&paste
  1814. #define P_LEFT P[1]
  1815. #define P_TOP P[2]
  1816. #define P_TOPRIGHT P[3]
  1817. #define P_MEDIAN P[4]
  1818. #define P_MV1 P[9]
  1819. #define FLAG_QPEL 1 //must be 1
  1820. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  1821. uint8_t p_buffer[1024];
  1822. uint8_t i_buffer[1024];
  1823. uint8_t p_state[sizeof(s->block_state)];
  1824. uint8_t i_state[sizeof(s->block_state)];
  1825. RangeCoder pc, ic;
  1826. uint8_t *pbbak= s->c.bytestream;
  1827. uint8_t *pbbak_start= s->c.bytestream_start;
  1828. int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  1829. const int w= s->b_width << s->block_max_depth;
  1830. const int h= s->b_height << s->block_max_depth;
  1831. const int rem_depth= s->block_max_depth - level;
  1832. const int index= (x + y*w) << rem_depth;
  1833. const int block_w= 1<<(LOG2_MB_SIZE - level);
  1834. int trx= (x+1)<<rem_depth;
  1835. int try= (y+1)<<rem_depth;
  1836. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  1837. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  1838. const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1839. const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1840. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1841. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1842. int pl = left->color[0];
  1843. int pcb= left->color[1];
  1844. int pcr= left->color[2];
  1845. int pmx, pmy;
  1846. int mx=0, my=0;
  1847. int l,cr,cb;
  1848. const int stride= s->current_picture.linesize[0];
  1849. const int uvstride= s->current_picture.linesize[1];
  1850. uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
  1851. s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
  1852. s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
  1853. int P[10][2];
  1854. int16_t last_mv[3][2];
  1855. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  1856. const int shift= 1+qpel;
  1857. MotionEstContext *c= &s->m.me;
  1858. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  1859. int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  1860. int my_context= av_log2(2*FFABS(left->my - top->my));
  1861. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1862. int ref, best_ref, ref_score, ref_mx, ref_my;
  1863. assert(sizeof(s->block_state) >= 256);
  1864. if(s->keyframe){
  1865. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  1866. return 0;
  1867. }
  1868. // clip predictors / edge ?
  1869. P_LEFT[0]= left->mx;
  1870. P_LEFT[1]= left->my;
  1871. P_TOP [0]= top->mx;
  1872. P_TOP [1]= top->my;
  1873. P_TOPRIGHT[0]= tr->mx;
  1874. P_TOPRIGHT[1]= tr->my;
  1875. last_mv[0][0]= s->block[index].mx;
  1876. last_mv[0][1]= s->block[index].my;
  1877. last_mv[1][0]= right->mx;
  1878. last_mv[1][1]= right->my;
  1879. last_mv[2][0]= bottom->mx;
  1880. last_mv[2][1]= bottom->my;
  1881. s->m.mb_stride=2;
  1882. s->m.mb_x=
  1883. s->m.mb_y= 0;
  1884. c->skip= 0;
  1885. assert(c-> stride == stride);
  1886. assert(c->uvstride == uvstride);
  1887. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1888. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1889. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1890. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  1891. c->xmin = - x*block_w - 16+3;
  1892. c->ymin = - y*block_w - 16+3;
  1893. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  1894. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  1895. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1896. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  1897. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  1898. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1899. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1900. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  1901. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1902. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1903. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1904. if (!y) {
  1905. c->pred_x= P_LEFT[0];
  1906. c->pred_y= P_LEFT[1];
  1907. } else {
  1908. c->pred_x = P_MEDIAN[0];
  1909. c->pred_y = P_MEDIAN[1];
  1910. }
  1911. score= INT_MAX;
  1912. best_ref= 0;
  1913. for(ref=0; ref<s->ref_frames; ref++){
  1914. init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
  1915. ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  1916. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  1917. assert(ref_mx >= c->xmin);
  1918. assert(ref_mx <= c->xmax);
  1919. assert(ref_my >= c->ymin);
  1920. assert(ref_my <= c->ymax);
  1921. ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  1922. ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  1923. ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  1924. if(s->ref_mvs[ref]){
  1925. s->ref_mvs[ref][index][0]= ref_mx;
  1926. s->ref_mvs[ref][index][1]= ref_my;
  1927. s->ref_scores[ref][index]= ref_score;
  1928. }
  1929. if(score > ref_score){
  1930. score= ref_score;
  1931. best_ref= ref;
  1932. mx= ref_mx;
  1933. my= ref_my;
  1934. }
  1935. }
  1936. //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  1937. // subpel search
  1938. base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  1939. pc= s->c;
  1940. pc.bytestream_start=
  1941. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  1942. memcpy(p_state, s->block_state, sizeof(s->block_state));
  1943. if(level!=s->block_max_depth)
  1944. put_rac(&pc, &p_state[4 + s_context], 1);
  1945. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  1946. if(s->ref_frames > 1)
  1947. put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  1948. pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  1949. put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  1950. put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  1951. p_len= pc.bytestream - pc.bytestream_start;
  1952. score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  1953. block_s= block_w*block_w;
  1954. sum = pix_sum(current_data[0], stride, block_w);
  1955. l= (sum + block_s/2)/block_s;
  1956. iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  1957. block_s= block_w*block_w>>2;
  1958. sum = pix_sum(current_data[1], uvstride, block_w>>1);
  1959. cb= (sum + block_s/2)/block_s;
  1960. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  1961. sum = pix_sum(current_data[2], uvstride, block_w>>1);
  1962. cr= (sum + block_s/2)/block_s;
  1963. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  1964. ic= s->c;
  1965. ic.bytestream_start=
  1966. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  1967. memcpy(i_state, s->block_state, sizeof(s->block_state));
  1968. if(level!=s->block_max_depth)
  1969. put_rac(&ic, &i_state[4 + s_context], 1);
  1970. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  1971. put_symbol(&ic, &i_state[32], l-pl , 1);
  1972. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  1973. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  1974. i_len= ic.bytestream - ic.bytestream_start;
  1975. iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  1976. // assert(score==256*256*256*64-1);
  1977. assert(iscore < 255*255*256 + s->lambda2*10);
  1978. assert(iscore >= 0);
  1979. assert(l>=0 && l<=255);
  1980. assert(pl>=0 && pl<=255);
  1981. if(level==0){
  1982. int varc= iscore >> 8;
  1983. int vard= score >> 8;
  1984. if (vard <= 64 || vard < varc)
  1985. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1986. else
  1987. c->scene_change_score+= s->m.qscale;
  1988. }
  1989. if(level!=s->block_max_depth){
  1990. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  1991. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1992. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1993. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1994. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1995. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  1996. if(score2 < score && score2 < iscore)
  1997. return score2;
  1998. }
  1999. if(iscore < score){
  2000. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2001. memcpy(pbbak, i_buffer, i_len);
  2002. s->c= ic;
  2003. s->c.bytestream_start= pbbak_start;
  2004. s->c.bytestream= pbbak + i_len;
  2005. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  2006. memcpy(s->block_state, i_state, sizeof(s->block_state));
  2007. return iscore;
  2008. }else{
  2009. memcpy(pbbak, p_buffer, p_len);
  2010. s->c= pc;
  2011. s->c.bytestream_start= pbbak_start;
  2012. s->c.bytestream= pbbak + p_len;
  2013. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  2014. memcpy(s->block_state, p_state, sizeof(s->block_state));
  2015. return score;
  2016. }
  2017. }
  2018. static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  2019. const int w= s->b_width << s->block_max_depth;
  2020. const int rem_depth= s->block_max_depth - level;
  2021. const int index= (x + y*w) << rem_depth;
  2022. int trx= (x+1)<<rem_depth;
  2023. BlockNode *b= &s->block[index];
  2024. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2025. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  2026. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2027. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  2028. int pl = left->color[0];
  2029. int pcb= left->color[1];
  2030. int pcr= left->color[2];
  2031. int pmx, pmy;
  2032. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2033. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  2034. int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  2035. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  2036. if(s->keyframe){
  2037. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  2038. return;
  2039. }
  2040. if(level!=s->block_max_depth){
  2041. if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  2042. put_rac(&s->c, &s->block_state[4 + s_context], 1);
  2043. }else{
  2044. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  2045. encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  2046. encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  2047. encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  2048. encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  2049. return;
  2050. }
  2051. }
  2052. if(b->type & BLOCK_INTRA){
  2053. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2054. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  2055. put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  2056. put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  2057. put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  2058. set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  2059. }else{
  2060. pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  2061. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  2062. if(s->ref_frames > 1)
  2063. put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  2064. put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  2065. put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  2066. set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  2067. }
  2068. }
  2069. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2070. int i, x2, y2;
  2071. Plane *p= &s->plane[plane_index];
  2072. const int block_size = MB_SIZE >> s->block_max_depth;
  2073. const int block_w = plane_index ? block_size/2 : block_size;
  2074. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2075. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2076. const int ref_stride= s->current_picture.linesize[plane_index];
  2077. uint8_t *src= s-> input_picture.data[plane_index];
  2078. IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  2079. const int b_stride = s->b_width << s->block_max_depth;
  2080. const int w= p->width;
  2081. const int h= p->height;
  2082. int index= mb_x + mb_y*b_stride;
  2083. BlockNode *b= &s->block[index];
  2084. BlockNode backup= *b;
  2085. int ab=0;
  2086. int aa=0;
  2087. b->type|= BLOCK_INTRA;
  2088. b->color[plane_index]= 0;
  2089. memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  2090. for(i=0; i<4; i++){
  2091. int mb_x2= mb_x + (i &1) - 1;
  2092. int mb_y2= mb_y + (i>>1) - 1;
  2093. int x= block_w*mb_x2 + block_w/2;
  2094. int y= block_w*mb_y2 + block_w/2;
  2095. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2096. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2097. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2098. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2099. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2100. int obmc_v= obmc[index];
  2101. int d;
  2102. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2103. if(x<0) obmc_v += obmc[index + block_w];
  2104. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2105. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2106. //FIXME precalculate this or simplify it somehow else
  2107. d = -dst[index] + (1<<(FRAC_BITS-1));
  2108. dst[index] = d;
  2109. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2110. aa += obmc_v * obmc_v; //FIXME precalculate this
  2111. }
  2112. }
  2113. }
  2114. *b= backup;
  2115. return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we should not need clipping
  2116. }
  2117. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2118. const int b_stride = s->b_width << s->block_max_depth;
  2119. const int b_height = s->b_height<< s->block_max_depth;
  2120. int index= x + y*b_stride;
  2121. const BlockNode *b = &s->block[index];
  2122. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2123. const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2124. const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2125. const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2126. int dmx, dmy;
  2127. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2128. // int my_context= av_log2(2*FFABS(left->my - top->my));
  2129. if(x<0 || x>=b_stride || y>=b_height)
  2130. return 0;
  2131. /*
  2132. 1 0 0
  2133. 01X 1-2 1
  2134. 001XX 3-6 2-3
  2135. 0001XXX 7-14 4-7
  2136. 00001XXXX 15-30 8-15
  2137. */
  2138. //FIXME try accurate rate
  2139. //FIXME intra and inter predictors if surrounding blocks are not the same type
  2140. if(b->type & BLOCK_INTRA){
  2141. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  2142. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  2143. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  2144. }else{
  2145. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2146. dmx-= b->mx;
  2147. dmy-= b->my;
  2148. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2149. + av_log2(2*FFABS(dmy))
  2150. + av_log2(2*b->ref));
  2151. }
  2152. }
  2153. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2154. Plane *p= &s->plane[plane_index];
  2155. const int block_size = MB_SIZE >> s->block_max_depth;
  2156. const int block_w = plane_index ? block_size/2 : block_size;
  2157. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2158. const int ref_stride= s->current_picture.linesize[plane_index];
  2159. uint8_t *dst= s->current_picture.data[plane_index];
  2160. uint8_t *src= s-> input_picture.data[plane_index];
  2161. IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2162. uint8_t *cur = s->scratchbuf;
  2163. uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
  2164. const int b_stride = s->b_width << s->block_max_depth;
  2165. const int b_height = s->b_height<< s->block_max_depth;
  2166. const int w= p->width;
  2167. const int h= p->height;
  2168. int distortion;
  2169. int rate= 0;
  2170. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2171. int sx= block_w*mb_x - block_w/2;
  2172. int sy= block_w*mb_y - block_w/2;
  2173. int x0= FFMAX(0,-sx);
  2174. int y0= FFMAX(0,-sy);
  2175. int x1= FFMIN(block_w*2, w-sx);
  2176. int y1= FFMIN(block_w*2, h-sy);
  2177. int i,x,y;
  2178. 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);
  2179. for(y=y0; y<y1; y++){
  2180. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2181. const IDWTELEM *pred1 = pred + y*obmc_stride;
  2182. uint8_t *cur1 = cur + y*ref_stride;
  2183. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2184. for(x=x0; x<x1; x++){
  2185. #if FRAC_BITS >= LOG2_OBMC_MAX
  2186. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2187. #else
  2188. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  2189. #endif
  2190. v = (v + pred1[x]) >> FRAC_BITS;
  2191. if(v&(~255)) v= ~(v>>31);
  2192. dst1[x] = v;
  2193. }
  2194. }
  2195. /* copy the regions where obmc[] = (uint8_t)256 */
  2196. if(LOG2_OBMC_MAX == 8
  2197. && (mb_x == 0 || mb_x == b_stride-1)
  2198. && (mb_y == 0 || mb_y == b_height-1)){
  2199. if(mb_x == 0)
  2200. x1 = block_w;
  2201. else
  2202. x0 = block_w;
  2203. if(mb_y == 0)
  2204. y1 = block_w;
  2205. else
  2206. y0 = block_w;
  2207. for(y=y0; y<y1; y++)
  2208. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2209. }
  2210. if(block_w==16){
  2211. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2212. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2213. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  2214. * So improving the score of one block is not strictly guaranteed
  2215. * to improve the score of the whole frame, thus iterative motion
  2216. * estimation does not always converge. */
  2217. if(s->avctx->me_cmp == FF_CMP_W97)
  2218. distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2219. else if(s->avctx->me_cmp == FF_CMP_W53)
  2220. distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2221. else{
  2222. distortion = 0;
  2223. for(i=0; i<4; i++){
  2224. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2225. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2226. }
  2227. }
  2228. }else{
  2229. assert(block_w==8);
  2230. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2231. }
  2232. if(plane_index==0){
  2233. for(i=0; i<4; i++){
  2234. /* ..RRr
  2235. * .RXx.
  2236. * rxx..
  2237. */
  2238. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2239. }
  2240. if(mb_x == b_stride-2)
  2241. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2242. }
  2243. return distortion + rate*penalty_factor;
  2244. }
  2245. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2246. int i, y2;
  2247. Plane *p= &s->plane[plane_index];
  2248. const int block_size = MB_SIZE >> s->block_max_depth;
  2249. const int block_w = plane_index ? block_size/2 : block_size;
  2250. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2251. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2252. const int ref_stride= s->current_picture.linesize[plane_index];
  2253. uint8_t *dst= s->current_picture.data[plane_index];
  2254. uint8_t *src= s-> input_picture.data[plane_index];
  2255. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  2256. // const has only been removed from zero_dst to suppress a warning
  2257. static IDWTELEM zero_dst[4096]; //FIXME
  2258. const int b_stride = s->b_width << s->block_max_depth;
  2259. const int w= p->width;
  2260. const int h= p->height;
  2261. int distortion= 0;
  2262. int rate= 0;
  2263. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2264. for(i=0; i<9; i++){
  2265. int mb_x2= mb_x + (i%3) - 1;
  2266. int mb_y2= mb_y + (i/3) - 1;
  2267. int x= block_w*mb_x2 + block_w/2;
  2268. int y= block_w*mb_y2 + block_w/2;
  2269. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  2270. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2271. //FIXME find a cleaner/simpler way to skip the outside stuff
  2272. for(y2= y; y2<0; y2++)
  2273. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2274. for(y2= h; y2<y+block_w; y2++)
  2275. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2276. if(x<0){
  2277. for(y2= y; y2<y+block_w; y2++)
  2278. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2279. }
  2280. if(x+block_w > w){
  2281. for(y2= y; y2<y+block_w; y2++)
  2282. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2283. }
  2284. assert(block_w== 8 || block_w==16);
  2285. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2286. }
  2287. if(plane_index==0){
  2288. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2289. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2290. /* ..RRRr
  2291. * .RXXx.
  2292. * .RXXx.
  2293. * rxxx.
  2294. */
  2295. if(merged)
  2296. rate = get_block_bits(s, mb_x, mb_y, 2);
  2297. for(i=merged?4:0; i<9; i++){
  2298. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2299. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2300. }
  2301. }
  2302. return distortion + rate*penalty_factor;
  2303. }
  2304. static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2305. const int w= b->width;
  2306. const int h= b->height;
  2307. int x, y;
  2308. if(1){
  2309. int run=0;
  2310. int runs[w*h];
  2311. int run_index=0;
  2312. int max_index;
  2313. for(y=0; y<h; y++){
  2314. for(x=0; x<w; x++){
  2315. int v, p=0;
  2316. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2317. v= src[x + y*stride];
  2318. if(y){
  2319. t= src[x + (y-1)*stride];
  2320. if(x){
  2321. lt= src[x - 1 + (y-1)*stride];
  2322. }
  2323. if(x + 1 < w){
  2324. rt= src[x + 1 + (y-1)*stride];
  2325. }
  2326. }
  2327. if(x){
  2328. l= src[x - 1 + y*stride];
  2329. /*if(x > 1){
  2330. if(orientation==1) ll= src[y + (x-2)*stride];
  2331. else ll= src[x - 2 + y*stride];
  2332. }*/
  2333. }
  2334. if(parent){
  2335. int px= x>>1;
  2336. int py= y>>1;
  2337. if(px<b->parent->width && py<b->parent->height)
  2338. p= parent[px + py*2*stride];
  2339. }
  2340. if(!(/*ll|*/l|lt|t|rt|p)){
  2341. if(v){
  2342. runs[run_index++]= run;
  2343. run=0;
  2344. }else{
  2345. run++;
  2346. }
  2347. }
  2348. }
  2349. }
  2350. max_index= run_index;
  2351. runs[run_index++]= run;
  2352. run_index=0;
  2353. run= runs[run_index++];
  2354. put_symbol2(&s->c, b->state[30], max_index, 0);
  2355. if(run_index <= max_index)
  2356. put_symbol2(&s->c, b->state[1], run, 3);
  2357. for(y=0; y<h; y++){
  2358. if(s->c.bytestream_end - s->c.bytestream < w*40){
  2359. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2360. return -1;
  2361. }
  2362. for(x=0; x<w; x++){
  2363. int v, p=0;
  2364. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2365. v= src[x + y*stride];
  2366. if(y){
  2367. t= src[x + (y-1)*stride];
  2368. if(x){
  2369. lt= src[x - 1 + (y-1)*stride];
  2370. }
  2371. if(x + 1 < w){
  2372. rt= src[x + 1 + (y-1)*stride];
  2373. }
  2374. }
  2375. if(x){
  2376. l= src[x - 1 + y*stride];
  2377. /*if(x > 1){
  2378. if(orientation==1) ll= src[y + (x-2)*stride];
  2379. else ll= src[x - 2 + y*stride];
  2380. }*/
  2381. }
  2382. if(parent){
  2383. int px= x>>1;
  2384. int py= y>>1;
  2385. if(px<b->parent->width && py<b->parent->height)
  2386. p= parent[px + py*2*stride];
  2387. }
  2388. if(/*ll|*/l|lt|t|rt|p){
  2389. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2390. put_rac(&s->c, &b->state[0][context], !!v);
  2391. }else{
  2392. if(!run){
  2393. run= runs[run_index++];
  2394. if(run_index <= max_index)
  2395. put_symbol2(&s->c, b->state[1], run, 3);
  2396. assert(v);
  2397. }else{
  2398. run--;
  2399. assert(!v);
  2400. }
  2401. }
  2402. if(v){
  2403. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2404. int l2= 2*FFABS(l) + (l<0);
  2405. int t2= 2*FFABS(t) + (t<0);
  2406. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  2407. put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
  2408. }
  2409. }
  2410. }
  2411. }
  2412. return 0;
  2413. }
  2414. static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2415. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  2416. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  2417. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  2418. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  2419. }
  2420. 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){
  2421. const int b_stride= s->b_width << s->block_max_depth;
  2422. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2423. BlockNode backup= *block;
  2424. int rd, index, value;
  2425. assert(mb_x>=0 && mb_y>=0);
  2426. assert(mb_x<b_stride);
  2427. if(intra){
  2428. block->color[0] = p[0];
  2429. block->color[1] = p[1];
  2430. block->color[2] = p[2];
  2431. block->type |= BLOCK_INTRA;
  2432. }else{
  2433. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2434. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2435. if(s->me_cache[index] == value)
  2436. return 0;
  2437. s->me_cache[index]= value;
  2438. block->mx= p[0];
  2439. block->my= p[1];
  2440. block->type &= ~BLOCK_INTRA;
  2441. }
  2442. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2443. //FIXME chroma
  2444. if(rd < *best_rd){
  2445. *best_rd= rd;
  2446. return 1;
  2447. }else{
  2448. *block= backup;
  2449. return 0;
  2450. }
  2451. }
  2452. /* special case for int[2] args we discard afterwards,
  2453. * fixes compilation problem with gcc 2.95 */
  2454. 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){
  2455. int p[2] = {p0, p1};
  2456. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2457. }
  2458. 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){
  2459. const int b_stride= s->b_width << s->block_max_depth;
  2460. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2461. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2462. int rd, index, value;
  2463. assert(mb_x>=0 && mb_y>=0);
  2464. assert(mb_x<b_stride);
  2465. assert(((mb_x|mb_y)&1) == 0);
  2466. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2467. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2468. if(s->me_cache[index] == value)
  2469. return 0;
  2470. s->me_cache[index]= value;
  2471. block->mx= p0;
  2472. block->my= p1;
  2473. block->ref= ref;
  2474. block->type &= ~BLOCK_INTRA;
  2475. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2476. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2477. //FIXME chroma
  2478. if(rd < *best_rd){
  2479. *best_rd= rd;
  2480. return 1;
  2481. }else{
  2482. block[0]= backup[0];
  2483. block[1]= backup[1];
  2484. block[b_stride]= backup[2];
  2485. block[b_stride+1]= backup[3];
  2486. return 0;
  2487. }
  2488. }
  2489. static void iterative_me(SnowContext *s){
  2490. int pass, mb_x, mb_y;
  2491. const int b_width = s->b_width << s->block_max_depth;
  2492. const int b_height= s->b_height << s->block_max_depth;
  2493. const int b_stride= b_width;
  2494. int color[3];
  2495. {
  2496. RangeCoder r = s->c;
  2497. uint8_t state[sizeof(s->block_state)];
  2498. memcpy(state, s->block_state, sizeof(s->block_state));
  2499. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2500. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2501. encode_q_branch(s, 0, mb_x, mb_y);
  2502. s->c = r;
  2503. memcpy(s->block_state, state, sizeof(s->block_state));
  2504. }
  2505. for(pass=0; pass<25; pass++){
  2506. int change= 0;
  2507. for(mb_y= 0; mb_y<b_height; mb_y++){
  2508. for(mb_x= 0; mb_x<b_width; mb_x++){
  2509. int dia_change, i, j, ref;
  2510. int best_rd= INT_MAX, ref_rd;
  2511. BlockNode backup, ref_b;
  2512. const int index= mb_x + mb_y * b_stride;
  2513. BlockNode *block= &s->block[index];
  2514. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2515. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2516. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2517. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2518. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2519. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2520. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2521. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2522. const int b_w= (MB_SIZE >> s->block_max_depth);
  2523. uint8_t obmc_edged[b_w*2][b_w*2];
  2524. if(pass && (block->type & BLOCK_OPT))
  2525. continue;
  2526. block->type |= BLOCK_OPT;
  2527. backup= *block;
  2528. if(!s->me_cache_generation)
  2529. memset(s->me_cache, 0, sizeof(s->me_cache));
  2530. s->me_cache_generation += 1<<22;
  2531. //FIXME precalculate
  2532. {
  2533. int x, y;
  2534. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2535. if(mb_x==0)
  2536. for(y=0; y<b_w*2; y++)
  2537. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2538. if(mb_x==b_stride-1)
  2539. for(y=0; y<b_w*2; y++)
  2540. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2541. if(mb_y==0){
  2542. for(x=0; x<b_w*2; x++)
  2543. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  2544. for(y=1; y<b_w; y++)
  2545. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  2546. }
  2547. if(mb_y==b_height-1){
  2548. for(x=0; x<b_w*2; x++)
  2549. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  2550. for(y=b_w; y<b_w*2-1; y++)
  2551. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  2552. }
  2553. }
  2554. //skip stuff outside the picture
  2555. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  2556. uint8_t *src= s-> input_picture.data[0];
  2557. uint8_t *dst= s->current_picture.data[0];
  2558. const int stride= s->current_picture.linesize[0];
  2559. const int block_w= MB_SIZE >> s->block_max_depth;
  2560. const int sx= block_w*mb_x - block_w/2;
  2561. const int sy= block_w*mb_y - block_w/2;
  2562. const int w= s->plane[0].width;
  2563. const int h= s->plane[0].height;
  2564. int y;
  2565. for(y=sy; y<0; y++)
  2566. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2567. for(y=h; y<sy+block_w*2; y++)
  2568. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2569. if(sx<0){
  2570. for(y=sy; y<sy+block_w*2; y++)
  2571. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  2572. }
  2573. if(sx+block_w*2 > w){
  2574. for(y=sy; y<sy+block_w*2; y++)
  2575. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  2576. }
  2577. }
  2578. // intra(black) = neighbors' contribution to the current block
  2579. for(i=0; i<3; i++)
  2580. color[i]= get_dc(s, mb_x, mb_y, i);
  2581. // get previous score (cannot be cached due to OBMC)
  2582. if(pass > 0 && (block->type&BLOCK_INTRA)){
  2583. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  2584. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  2585. }else
  2586. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  2587. ref_b= *block;
  2588. ref_rd= best_rd;
  2589. for(ref=0; ref < s->ref_frames; ref++){
  2590. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  2591. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  2592. continue;
  2593. block->ref= ref;
  2594. best_rd= INT_MAX;
  2595. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  2596. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  2597. if(tb)
  2598. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  2599. if(lb)
  2600. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  2601. if(rb)
  2602. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  2603. if(bb)
  2604. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  2605. /* fullpel ME */
  2606. //FIXME avoid subpel interpolation / round to nearest integer
  2607. do{
  2608. dia_change=0;
  2609. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  2610. for(j=0; j<i; j++){
  2611. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2612. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  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. }
  2616. }
  2617. }while(dia_change);
  2618. /* subpel ME */
  2619. do{
  2620. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  2621. dia_change=0;
  2622. for(i=0; i<8; i++)
  2623. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  2624. }while(dia_change);
  2625. //FIXME or try the standard 2 pass qpel or similar
  2626. mvr[0][0]= block->mx;
  2627. mvr[0][1]= block->my;
  2628. if(ref_rd > best_rd){
  2629. ref_rd= best_rd;
  2630. ref_b= *block;
  2631. }
  2632. }
  2633. best_rd= ref_rd;
  2634. *block= ref_b;
  2635. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  2636. //FIXME RD style color selection
  2637. if(!same_block(block, &backup)){
  2638. if(tb ) tb ->type &= ~BLOCK_OPT;
  2639. if(lb ) lb ->type &= ~BLOCK_OPT;
  2640. if(rb ) rb ->type &= ~BLOCK_OPT;
  2641. if(bb ) bb ->type &= ~BLOCK_OPT;
  2642. if(tlb) tlb->type &= ~BLOCK_OPT;
  2643. if(trb) trb->type &= ~BLOCK_OPT;
  2644. if(blb) blb->type &= ~BLOCK_OPT;
  2645. if(brb) brb->type &= ~BLOCK_OPT;
  2646. change ++;
  2647. }
  2648. }
  2649. }
  2650. av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  2651. if(!change)
  2652. break;
  2653. }
  2654. if(s->block_max_depth == 1){
  2655. int change= 0;
  2656. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  2657. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  2658. int i;
  2659. int best_rd, init_rd;
  2660. const int index= mb_x + mb_y * b_stride;
  2661. BlockNode *b[4];
  2662. b[0]= &s->block[index];
  2663. b[1]= b[0]+1;
  2664. b[2]= b[0]+b_stride;
  2665. b[3]= b[2]+1;
  2666. if(same_block(b[0], b[1]) &&
  2667. same_block(b[0], b[2]) &&
  2668. same_block(b[0], b[3]))
  2669. continue;
  2670. if(!s->me_cache_generation)
  2671. memset(s->me_cache, 0, sizeof(s->me_cache));
  2672. s->me_cache_generation += 1<<22;
  2673. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  2674. //FIXME more multiref search?
  2675. check_4block_inter(s, mb_x, mb_y,
  2676. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  2677. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  2678. for(i=0; i<4; i++)
  2679. if(!(b[i]->type&BLOCK_INTRA))
  2680. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  2681. if(init_rd != best_rd)
  2682. change++;
  2683. }
  2684. }
  2685. av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  2686. }
  2687. }
  2688. static void encode_blocks(SnowContext *s, int search){
  2689. int x, y;
  2690. int w= s->b_width;
  2691. int h= s->b_height;
  2692. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  2693. iterative_me(s);
  2694. for(y=0; y<h; y++){
  2695. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  2696. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2697. return;
  2698. }
  2699. for(x=0; x<w; x++){
  2700. if(s->avctx->me_method == ME_ITER || !search)
  2701. encode_q_branch2(s, 0, x, y);
  2702. else
  2703. encode_q_branch (s, 0, x, y);
  2704. }
  2705. }
  2706. }
  2707. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  2708. const int w= b->width;
  2709. const int h= b->height;
  2710. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2711. const int qmul= qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  2712. int x,y, thres1, thres2;
  2713. if(s->qlog == LOSSLESS_QLOG){
  2714. for(y=0; y<h; y++)
  2715. for(x=0; x<w; x++)
  2716. dst[x + y*stride]= src[x + y*stride];
  2717. return;
  2718. }
  2719. bias= bias ? 0 : (3*qmul)>>3;
  2720. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  2721. thres2= 2*thres1;
  2722. if(!bias){
  2723. for(y=0; y<h; y++){
  2724. for(x=0; x<w; x++){
  2725. int i= src[x + y*stride];
  2726. if((unsigned)(i+thres1) > thres2){
  2727. if(i>=0){
  2728. i<<= QEXPSHIFT;
  2729. i/= qmul; //FIXME optimize
  2730. dst[x + y*stride]= i;
  2731. }else{
  2732. i= -i;
  2733. i<<= QEXPSHIFT;
  2734. i/= qmul; //FIXME optimize
  2735. dst[x + y*stride]= -i;
  2736. }
  2737. }else
  2738. dst[x + y*stride]= 0;
  2739. }
  2740. }
  2741. }else{
  2742. for(y=0; y<h; y++){
  2743. for(x=0; x<w; x++){
  2744. int i= src[x + y*stride];
  2745. if((unsigned)(i+thres1) > thres2){
  2746. if(i>=0){
  2747. i<<= QEXPSHIFT;
  2748. i= (i + bias) / qmul; //FIXME optimize
  2749. dst[x + y*stride]= i;
  2750. }else{
  2751. i= -i;
  2752. i<<= QEXPSHIFT;
  2753. i= (i + bias) / qmul; //FIXME optimize
  2754. dst[x + y*stride]= -i;
  2755. }
  2756. }else
  2757. dst[x + y*stride]= 0;
  2758. }
  2759. }
  2760. }
  2761. }
  2762. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  2763. const int w= b->width;
  2764. const int h= b->height;
  2765. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2766. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  2767. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2768. int x,y;
  2769. if(s->qlog == LOSSLESS_QLOG) return;
  2770. for(y=0; y<h; y++){
  2771. for(x=0; x<w; x++){
  2772. int i= src[x + y*stride];
  2773. if(i<0){
  2774. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2775. }else if(i>0){
  2776. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2777. }
  2778. }
  2779. }
  2780. }
  2781. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  2782. const int w= b->width;
  2783. const int h= b->height;
  2784. int x,y;
  2785. for(y=h-1; y>=0; y--){
  2786. for(x=w-1; x>=0; x--){
  2787. int i= x + y*stride;
  2788. if(x){
  2789. if(use_median){
  2790. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2791. else src[i] -= src[i - 1];
  2792. }else{
  2793. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2794. else src[i] -= src[i - 1];
  2795. }
  2796. }else{
  2797. if(y) src[i] -= src[i - stride];
  2798. }
  2799. }
  2800. }
  2801. }
  2802. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  2803. const int w= b->width;
  2804. const int h= b->height;
  2805. int x,y;
  2806. for(y=0; y<h; y++){
  2807. for(x=0; x<w; x++){
  2808. int i= x + y*stride;
  2809. if(x){
  2810. if(use_median){
  2811. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2812. else src[i] += src[i - 1];
  2813. }else{
  2814. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2815. else src[i] += src[i - 1];
  2816. }
  2817. }else{
  2818. if(y) src[i] += src[i - stride];
  2819. }
  2820. }
  2821. }
  2822. }
  2823. static void encode_qlogs(SnowContext *s){
  2824. int plane_index, level, orientation;
  2825. for(plane_index=0; plane_index<2; plane_index++){
  2826. for(level=0; level<s->spatial_decomposition_count; level++){
  2827. for(orientation=level ? 1:0; orientation<4; orientation++){
  2828. if(orientation==2) continue;
  2829. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  2830. }
  2831. }
  2832. }
  2833. }
  2834. static void encode_header(SnowContext *s){
  2835. int plane_index, i;
  2836. uint8_t kstate[32];
  2837. memset(kstate, MID_STATE, sizeof(kstate));
  2838. put_rac(&s->c, kstate, s->keyframe);
  2839. if(s->keyframe || s->always_reset){
  2840. reset_contexts(s);
  2841. s->last_spatial_decomposition_type=
  2842. s->last_qlog=
  2843. s->last_qbias=
  2844. s->last_mv_scale=
  2845. s->last_block_max_depth= 0;
  2846. for(plane_index=0; plane_index<2; plane_index++){
  2847. Plane *p= &s->plane[plane_index];
  2848. p->last_htaps=0;
  2849. p->last_diag_mc=0;
  2850. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  2851. }
  2852. }
  2853. if(s->keyframe){
  2854. put_symbol(&s->c, s->header_state, s->version, 0);
  2855. put_rac(&s->c, s->header_state, s->always_reset);
  2856. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  2857. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  2858. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2859. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  2860. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  2861. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  2862. put_rac(&s->c, s->header_state, s->spatial_scalability);
  2863. // put_rac(&s->c, s->header_state, s->rate_scalability);
  2864. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  2865. encode_qlogs(s);
  2866. }
  2867. if(!s->keyframe){
  2868. int update_mc=0;
  2869. for(plane_index=0; plane_index<2; plane_index++){
  2870. Plane *p= &s->plane[plane_index];
  2871. update_mc |= p->last_htaps != p->htaps;
  2872. update_mc |= p->last_diag_mc != p->diag_mc;
  2873. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  2874. }
  2875. put_rac(&s->c, s->header_state, update_mc);
  2876. if(update_mc){
  2877. for(plane_index=0; plane_index<2; plane_index++){
  2878. Plane *p= &s->plane[plane_index];
  2879. put_rac(&s->c, s->header_state, p->diag_mc);
  2880. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  2881. for(i= p->htaps/2; i; i--)
  2882. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  2883. }
  2884. }
  2885. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  2886. put_rac(&s->c, s->header_state, 1);
  2887. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2888. encode_qlogs(s);
  2889. }else
  2890. put_rac(&s->c, s->header_state, 0);
  2891. }
  2892. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  2893. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  2894. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  2895. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  2896. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  2897. }
  2898. static void update_last_header_values(SnowContext *s){
  2899. int plane_index;
  2900. if(!s->keyframe){
  2901. for(plane_index=0; plane_index<2; plane_index++){
  2902. Plane *p= &s->plane[plane_index];
  2903. p->last_diag_mc= p->diag_mc;
  2904. p->last_htaps = p->htaps;
  2905. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  2906. }
  2907. }
  2908. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  2909. s->last_qlog = s->qlog;
  2910. s->last_qbias = s->qbias;
  2911. s->last_mv_scale = s->mv_scale;
  2912. s->last_block_max_depth = s->block_max_depth;
  2913. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  2914. }
  2915. static int qscale2qlog(int qscale){
  2916. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  2917. + 61*QROOT/8; ///< 64 > 60
  2918. }
  2919. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  2920. {
  2921. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  2922. * FIXME we know exact mv bits at this point,
  2923. * but ratecontrol isn't set up to include them. */
  2924. uint32_t coef_sum= 0;
  2925. int level, orientation, delta_qlog;
  2926. for(level=0; level<s->spatial_decomposition_count; level++){
  2927. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2928. SubBand *b= &s->plane[0].band[level][orientation];
  2929. IDWTELEM *buf= b->ibuf;
  2930. const int w= b->width;
  2931. const int h= b->height;
  2932. const int stride= b->stride;
  2933. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  2934. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  2935. const int qdiv= (1<<16)/qmul;
  2936. int x, y;
  2937. //FIXME this is ugly
  2938. for(y=0; y<h; y++)
  2939. for(x=0; x<w; x++)
  2940. buf[x+y*stride]= b->buf[x+y*stride];
  2941. if(orientation==0)
  2942. decorrelate(s, b, buf, stride, 1, 0);
  2943. for(y=0; y<h; y++)
  2944. for(x=0; x<w; x++)
  2945. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  2946. }
  2947. }
  2948. /* ugly, ratecontrol just takes a sqrt again */
  2949. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  2950. assert(coef_sum < INT_MAX);
  2951. if(pict->pict_type == AV_PICTURE_TYPE_I){
  2952. s->m.current_picture.mb_var_sum= coef_sum;
  2953. s->m.current_picture.mc_mb_var_sum= 0;
  2954. }else{
  2955. s->m.current_picture.mc_mb_var_sum= coef_sum;
  2956. s->m.current_picture.mb_var_sum= 0;
  2957. }
  2958. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  2959. if (pict->quality < 0)
  2960. return INT_MIN;
  2961. s->lambda= pict->quality * 3/2;
  2962. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  2963. s->qlog+= delta_qlog;
  2964. return delta_qlog;
  2965. }
  2966. static void calculate_visual_weight(SnowContext *s, Plane *p){
  2967. int width = p->width;
  2968. int height= p->height;
  2969. int level, orientation, x, y;
  2970. for(level=0; level<s->spatial_decomposition_count; level++){
  2971. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2972. SubBand *b= &p->band[level][orientation];
  2973. IDWTELEM *ibuf= b->ibuf;
  2974. int64_t error=0;
  2975. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  2976. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  2977. ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2978. for(y=0; y<height; y++){
  2979. for(x=0; x<width; x++){
  2980. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  2981. error += d*d;
  2982. }
  2983. }
  2984. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  2985. }
  2986. }
  2987. }
  2988. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  2989. SnowContext *s = avctx->priv_data;
  2990. RangeCoder * const c= &s->c;
  2991. AVFrame *pict = data;
  2992. const int width= s->avctx->width;
  2993. const int height= s->avctx->height;
  2994. int level, orientation, plane_index, i, y;
  2995. uint8_t rc_header_bak[sizeof(s->header_state)];
  2996. uint8_t rc_block_bak[sizeof(s->block_state)];
  2997. ff_init_range_encoder(c, buf, buf_size);
  2998. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  2999. for(i=0; i<3; i++){
  3000. int shift= !!i;
  3001. for(y=0; y<(height>>shift); y++)
  3002. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3003. &pict->data[i][y * pict->linesize[i]],
  3004. width>>shift);
  3005. }
  3006. s->new_picture = *pict;
  3007. s->m.picture_number= avctx->frame_number;
  3008. if(avctx->flags&CODEC_FLAG_PASS2){
  3009. s->m.pict_type =
  3010. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3011. s->keyframe= pict->pict_type==AV_PICTURE_TYPE_I;
  3012. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  3013. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3014. if (pict->quality < 0)
  3015. return -1;
  3016. }
  3017. }else{
  3018. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3019. s->m.pict_type=
  3020. pict->pict_type= s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  3021. }
  3022. if(s->pass1_rc && avctx->frame_number == 0)
  3023. pict->quality= 2*FF_QP2LAMBDA;
  3024. if(pict->quality){
  3025. s->qlog= qscale2qlog(pict->quality);
  3026. s->lambda = pict->quality * 3/2;
  3027. }
  3028. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3029. s->qlog= LOSSLESS_QLOG;
  3030. s->lambda = 0;
  3031. }//else keep previous frame's qlog until after motion estimation
  3032. frame_start(s);
  3033. s->m.current_picture_ptr= &s->m.current_picture;
  3034. s->m.last_picture.f.pts = s->m.current_picture.f.pts;
  3035. s->m.current_picture.f.pts = pict->pts;
  3036. if(pict->pict_type == AV_PICTURE_TYPE_P){
  3037. int block_width = (width +15)>>4;
  3038. int block_height= (height+15)>>4;
  3039. int stride= s->current_picture.linesize[0];
  3040. assert(s->current_picture.data[0]);
  3041. assert(s->last_picture[0].data[0]);
  3042. s->m.avctx= s->avctx;
  3043. s->m.current_picture.f.data[0] = s->current_picture.data[0];
  3044. s->m. last_picture.f.data[0] = s->last_picture[0].data[0];
  3045. s->m. new_picture.f.data[0] = s-> input_picture.data[0];
  3046. s->m. last_picture_ptr= &s->m. last_picture;
  3047. s->m.linesize=
  3048. s->m. last_picture.f.linesize[0] =
  3049. s->m. new_picture.f.linesize[0] =
  3050. s->m.current_picture.f.linesize[0] = stride;
  3051. s->m.uvlinesize= s->current_picture.linesize[1];
  3052. s->m.width = width;
  3053. s->m.height= height;
  3054. s->m.mb_width = block_width;
  3055. s->m.mb_height= block_height;
  3056. s->m.mb_stride= s->m.mb_width+1;
  3057. s->m.b8_stride= 2*s->m.mb_width+1;
  3058. s->m.f_code=1;
  3059. s->m.pict_type= pict->pict_type;
  3060. s->m.me_method= s->avctx->me_method;
  3061. s->m.me.scene_change_score=0;
  3062. s->m.flags= s->avctx->flags;
  3063. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3064. s->m.out_format= FMT_H263;
  3065. s->m.unrestricted_mv= 1;
  3066. s->m.lambda = s->lambda;
  3067. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3068. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3069. s->m.dsp= s->dsp; //move
  3070. ff_init_me(&s->m);
  3071. s->dsp= s->m.dsp;
  3072. }
  3073. if(s->pass1_rc){
  3074. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  3075. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  3076. }
  3077. redo_frame:
  3078. if(pict->pict_type == AV_PICTURE_TYPE_I)
  3079. s->spatial_decomposition_count= 5;
  3080. else
  3081. s->spatial_decomposition_count= 5;
  3082. s->m.pict_type = pict->pict_type;
  3083. s->qbias= pict->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
  3084. common_init_after_header(avctx);
  3085. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3086. for(plane_index=0; plane_index<3; plane_index++){
  3087. calculate_visual_weight(s, &s->plane[plane_index]);
  3088. }
  3089. }
  3090. encode_header(s);
  3091. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3092. encode_blocks(s, 1);
  3093. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3094. for(plane_index=0; plane_index<3; plane_index++){
  3095. Plane *p= &s->plane[plane_index];
  3096. int w= p->width;
  3097. int h= p->height;
  3098. int x, y;
  3099. // int bits= put_bits_count(&s->c.pb);
  3100. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3101. //FIXME optimize
  3102. if(pict->data[plane_index]) //FIXME gray hack
  3103. for(y=0; y<h; y++){
  3104. for(x=0; x<w; x++){
  3105. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3106. }
  3107. }
  3108. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  3109. if( plane_index==0
  3110. && pict->pict_type == AV_PICTURE_TYPE_P
  3111. && !(avctx->flags&CODEC_FLAG_PASS2)
  3112. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3113. ff_init_range_encoder(c, buf, buf_size);
  3114. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3115. pict->pict_type= AV_PICTURE_TYPE_I;
  3116. s->keyframe=1;
  3117. s->current_picture.key_frame=1;
  3118. goto redo_frame;
  3119. }
  3120. if(s->qlog == LOSSLESS_QLOG){
  3121. for(y=0; y<h; y++){
  3122. for(x=0; x<w; x++){
  3123. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3124. }
  3125. }
  3126. }else{
  3127. for(y=0; y<h; y++){
  3128. for(x=0; x<w; x++){
  3129. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  3130. }
  3131. }
  3132. }
  3133. /* if(QUANTIZE2)
  3134. dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
  3135. else*/
  3136. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3137. if(s->pass1_rc && plane_index==0){
  3138. int delta_qlog = ratecontrol_1pass(s, pict);
  3139. if (delta_qlog <= INT_MIN)
  3140. return -1;
  3141. if(delta_qlog){
  3142. //reordering qlog in the bitstream would eliminate this reset
  3143. ff_init_range_encoder(c, buf, buf_size);
  3144. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  3145. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  3146. encode_header(s);
  3147. encode_blocks(s, 0);
  3148. }
  3149. }
  3150. for(level=0; level<s->spatial_decomposition_count; level++){
  3151. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3152. SubBand *b= &p->band[level][orientation];
  3153. if(!QUANTIZE2)
  3154. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  3155. if(orientation==0)
  3156. decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == AV_PICTURE_TYPE_P, 0);
  3157. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  3158. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3159. if(orientation==0)
  3160. correlate(s, b, b->ibuf, b->stride, 1, 0);
  3161. }
  3162. }
  3163. for(level=0; level<s->spatial_decomposition_count; level++){
  3164. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3165. SubBand *b= &p->band[level][orientation];
  3166. dequantize(s, b, b->ibuf, b->stride);
  3167. }
  3168. }
  3169. ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3170. if(s->qlog == LOSSLESS_QLOG){
  3171. for(y=0; y<h; y++){
  3172. for(x=0; x<w; x++){
  3173. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  3174. }
  3175. }
  3176. }
  3177. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3178. }else{
  3179. //ME/MC only
  3180. if(pict->pict_type == AV_PICTURE_TYPE_I){
  3181. for(y=0; y<h; y++){
  3182. for(x=0; x<w; x++){
  3183. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3184. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3185. }
  3186. }
  3187. }else{
  3188. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  3189. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3190. }
  3191. }
  3192. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3193. int64_t error= 0;
  3194. if(pict->data[plane_index]) //FIXME gray hack
  3195. for(y=0; y<h; y++){
  3196. for(x=0; x<w; x++){
  3197. 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];
  3198. error += d*d;
  3199. }
  3200. }
  3201. s->avctx->error[plane_index] += error;
  3202. s->current_picture.error[plane_index] = error;
  3203. }
  3204. }
  3205. update_last_header_values(s);
  3206. release_buffer(avctx);
  3207. s->current_picture.coded_picture_number = avctx->frame_number;
  3208. s->current_picture.pict_type = pict->pict_type;
  3209. s->current_picture.quality = pict->quality;
  3210. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3211. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3212. s->m.current_picture.f.display_picture_number =
  3213. s->m.current_picture.f.coded_picture_number = avctx->frame_number;
  3214. s->m.current_picture.f.quality = pict->quality;
  3215. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3216. if(s->pass1_rc)
  3217. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  3218. return -1;
  3219. if(avctx->flags&CODEC_FLAG_PASS1)
  3220. ff_write_pass1_stats(&s->m);
  3221. s->m.last_pict_type = s->m.pict_type;
  3222. avctx->frame_bits = s->m.frame_bits;
  3223. avctx->mv_bits = s->m.mv_bits;
  3224. avctx->misc_bits = s->m.misc_bits;
  3225. avctx->p_tex_bits = s->m.p_tex_bits;
  3226. emms_c();
  3227. return ff_rac_terminate(c);
  3228. }
  3229. static av_cold int encode_end(AVCodecContext *avctx)
  3230. {
  3231. SnowContext *s = avctx->priv_data;
  3232. common_end(s);
  3233. if (s->input_picture.data[0])
  3234. avctx->release_buffer(avctx, &s->input_picture);
  3235. av_free(avctx->stats_out);
  3236. return 0;
  3237. }
  3238. AVCodec ff_snow_encoder = {
  3239. .name = "snow",
  3240. .type = AVMEDIA_TYPE_VIDEO,
  3241. .id = CODEC_ID_SNOW,
  3242. .priv_data_size = sizeof(SnowContext),
  3243. .init = encode_init,
  3244. .encode = encode_frame,
  3245. .close = encode_end,
  3246. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  3247. };
  3248. #endif