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.

3829 lines
140KB

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