comparison snow.c @ 3314:aea2230e6033 libavcodec

Snow multiple reference frames (bitstream is not backwards compatible, even if refs aren't used)
author lorenm
date Sun, 28 May 2006 21:44:47 +0000
parents be941215e8e6
children 0b4f548dfb44
comparison
equal deleted inserted replaced
3313:be941215e8e6 3314:aea2230e6033
367 }; 367 };
368 368
369 typedef struct BlockNode{ 369 typedef struct BlockNode{
370 int16_t mx; 370 int16_t mx;
371 int16_t my; 371 int16_t my;
372 uint8_t ref;
372 uint8_t color[3]; 373 uint8_t color[3];
373 uint8_t type; 374 uint8_t type;
374 //#define TYPE_SPLIT 1 375 //#define TYPE_SPLIT 1
375 #define BLOCK_INTRA 1 376 #define BLOCK_INTRA 1
376 #define BLOCK_OPT 2 377 #define BLOCK_OPT 2
380 381
381 static const BlockNode null_block= { //FIXME add border maybe 382 static const BlockNode null_block= { //FIXME add border maybe
382 .color= {128,128,128}, 383 .color= {128,128,128},
383 .mx= 0, 384 .mx= 0,
384 .my= 0, 385 .my= 0,
386 .ref= 0,
385 .type= 0, 387 .type= 0,
386 .level= 0, 388 .level= 0,
387 }; 389 };
388 390
389 #define LOG2_MB_SIZE 4 391 #define LOG2_MB_SIZE 4
422 RangeCoder c; 424 RangeCoder c;
423 DSPContext dsp; 425 DSPContext dsp;
424 AVFrame new_picture; 426 AVFrame new_picture;
425 AVFrame input_picture; ///< new_picture with the internal linesizes 427 AVFrame input_picture; ///< new_picture with the internal linesizes
426 AVFrame current_picture; 428 AVFrame current_picture;
427 AVFrame last_picture; 429 AVFrame last_picture[MAX_REF_FRAMES];
428 AVFrame mconly_picture; 430 AVFrame mconly_picture;
429 // uint8_t q_context[16]; 431 // uint8_t q_context[16];
430 uint8_t header_state[32]; 432 uint8_t header_state[32];
431 uint8_t block_state[128 + 32*128]; 433 uint8_t block_state[128 + 32*128];
432 int keyframe; 434 int keyframe;
434 int version; 436 int version;
435 int spatial_decomposition_type; 437 int spatial_decomposition_type;
436 int temporal_decomposition_type; 438 int temporal_decomposition_type;
437 int spatial_decomposition_count; 439 int spatial_decomposition_count;
438 int temporal_decomposition_count; 440 int temporal_decomposition_count;
441 int max_ref_frames;
442 int ref_frames;
443 int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
444 uint32_t *ref_scores[MAX_REF_FRAMES];
439 DWTELEM *spatial_dwt_buffer; 445 DWTELEM *spatial_dwt_buffer;
440 int colorspace_type; 446 int colorspace_type;
441 int chroma_h_shift; 447 int chroma_h_shift;
442 int chroma_v_shift; 448 int chroma_v_shift;
443 int spatial_scalability; 449 int spatial_scalability;
1903 pix += line_size - w; 1909 pix += line_size - w;
1904 } 1910 }
1905 return s; 1911 return s;
1906 } 1912 }
1907 1913
1908 static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int type){ 1914 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){
1909 const int w= s->b_width << s->block_max_depth; 1915 const int w= s->b_width << s->block_max_depth;
1910 const int rem_depth= s->block_max_depth - level; 1916 const int rem_depth= s->block_max_depth - level;
1911 const int index= (x + y*w) << rem_depth; 1917 const int index= (x + y*w) << rem_depth;
1912 const int block_w= 1<<rem_depth; 1918 const int block_w= 1<<rem_depth;
1913 BlockNode block; 1919 BlockNode block;
1916 block.color[0]= l; 1922 block.color[0]= l;
1917 block.color[1]= cb; 1923 block.color[1]= cb;
1918 block.color[2]= cr; 1924 block.color[2]= cr;
1919 block.mx= mx; 1925 block.mx= mx;
1920 block.my= my; 1926 block.my= my;
1927 block.ref= ref;
1921 block.type= type; 1928 block.type= type;
1922 block.level= level; 1929 block.level= level;
1923 1930
1924 for(j=0; j<block_w; j++){ 1931 for(j=0; j<block_w; j++){
1925 for(i=0; i<block_w; i++){ 1932 for(i=0; i<block_w; i++){
1987 int P[10][2]; 1994 int P[10][2];
1988 int16_t last_mv[3][2]; 1995 int16_t last_mv[3][2];
1989 int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused 1996 int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
1990 const int shift= 1+qpel; 1997 const int shift= 1+qpel;
1991 MotionEstContext *c= &s->m.me; 1998 MotionEstContext *c= &s->m.me;
1999 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
1992 int mx_context= av_log2(2*ABS(left->mx - top->mx)); 2000 int mx_context= av_log2(2*ABS(left->mx - top->mx));
1993 int my_context= av_log2(2*ABS(left->my - top->my)); 2001 int my_context= av_log2(2*ABS(left->my - top->my));
1994 int s_context= 2*left->level + 2*top->level + tl->level + tr->level; 2002 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2003 int ref, best_ref, ref_score, ref_mx, ref_my;
1995 2004
1996 assert(sizeof(s->block_state) >= 256); 2005 assert(sizeof(s->block_state) >= 256);
1997 if(s->keyframe){ 2006 if(s->keyframe){
1998 set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA); 2007 set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, 0, BLOCK_INTRA);
1999 return 0; 2008 return 0;
2000 } 2009 }
2001 2010
2002 // clip predictors / edge ? 2011 // clip predictors / edge ?
2003 2012
2017 2026
2018 s->m.mb_stride=2; 2027 s->m.mb_stride=2;
2019 s->m.mb_x= 2028 s->m.mb_x=
2020 s->m.mb_y= 0; 2029 s->m.mb_y= 0;
2021 s->m.me.skip= 0; 2030 s->m.me.skip= 0;
2022
2023 init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
2024 2031
2025 assert(s->m.me. stride == stride); 2032 assert(s->m.me. stride == stride);
2026 assert(s->m.me.uvstride == uvstride); 2033 assert(s->m.me.uvstride == uvstride);
2027 2034
2028 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp); 2035 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
2052 } else { 2059 } else {
2053 c->pred_x = P_MEDIAN[0]; 2060 c->pred_x = P_MEDIAN[0];
2054 c->pred_y = P_MEDIAN[1]; 2061 c->pred_y = P_MEDIAN[1];
2055 } 2062 }
2056 2063
2057 score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv, 2064 score= INT_MAX;
2058 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w); 2065 best_ref= 0;
2059 2066 for(ref=0; ref<s->ref_frames; ref++){
2060 assert(mx >= c->xmin); 2067 init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
2061 assert(mx <= c->xmax); 2068
2062 assert(my >= c->ymin); 2069 ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
2063 assert(my <= c->ymax); 2070 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
2064 2071
2065 score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w); 2072 assert(ref_mx >= c->xmin);
2066 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0); 2073 assert(ref_mx <= c->xmax);
2074 assert(ref_my >= c->ymin);
2075 assert(ref_my <= c->ymax);
2076
2077 ref_score= s->m.me.sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
2078 ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
2079 ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
2080 if(s->ref_mvs[ref]){
2081 s->ref_mvs[ref][index][0]= ref_mx;
2082 s->ref_mvs[ref][index][1]= ref_my;
2083 s->ref_scores[ref][index]= ref_score;
2084 }
2085 if(score > ref_score){
2086 score= ref_score;
2087 best_ref= ref;
2088 mx= ref_mx;
2089 my= ref_my;
2090 }
2091 }
2067 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2 2092 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2
2068 2093
2069 // subpel search 2094 // subpel search
2070 pc= s->c; 2095 pc= s->c;
2071 pc.bytestream_start= 2096 pc.bytestream_start=
2073 memcpy(p_state, s->block_state, sizeof(s->block_state)); 2098 memcpy(p_state, s->block_state, sizeof(s->block_state));
2074 2099
2075 if(level!=s->block_max_depth) 2100 if(level!=s->block_max_depth)
2076 put_rac(&pc, &p_state[4 + s_context], 1); 2101 put_rac(&pc, &p_state[4 + s_context], 1);
2077 put_rac(&pc, &p_state[1 + left->type + top->type], 0); 2102 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
2078 put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1); 2103 if(s->ref_frames > 1)
2079 put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1); 2104 put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
2105 put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
2106 put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
2080 p_len= pc.bytestream - pc.bytestream_start; 2107 p_len= pc.bytestream - pc.bytestream_start;
2081 score += (s->lambda2*(p_len*8 2108 score += (s->lambda2*(p_len*8
2082 + (pc.outstanding_count - s->c.outstanding_count)*8 2109 + (pc.outstanding_count - s->c.outstanding_count)*8
2083 + (-av_log2(pc.range) + av_log2(s->c.range)) 2110 + (-av_log2(pc.range) + av_log2(s->c.range))
2084 ))>>FF_LAMBDA_SHIFT; 2111 ))>>FF_LAMBDA_SHIFT;
2142 if(iscore < score){ 2169 if(iscore < score){
2143 memcpy(pbbak, i_buffer, i_len); 2170 memcpy(pbbak, i_buffer, i_len);
2144 s->c= ic; 2171 s->c= ic;
2145 s->c.bytestream_start= pbbak_start; 2172 s->c.bytestream_start= pbbak_start;
2146 s->c.bytestream= pbbak + i_len; 2173 s->c.bytestream= pbbak + i_len;
2147 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA); 2174 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
2148 memcpy(s->block_state, i_state, sizeof(s->block_state)); 2175 memcpy(s->block_state, i_state, sizeof(s->block_state));
2149 return iscore; 2176 return iscore;
2150 }else{ 2177 }else{
2151 memcpy(pbbak, p_buffer, p_len); 2178 memcpy(pbbak, p_buffer, p_len);
2152 s->c= pc; 2179 s->c= pc;
2153 s->c.bytestream_start= pbbak_start; 2180 s->c.bytestream_start= pbbak_start;
2154 s->c.bytestream= pbbak + p_len; 2181 s->c.bytestream= pbbak + p_len;
2155 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0); 2182 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
2156 memcpy(s->block_state, p_state, sizeof(s->block_state)); 2183 memcpy(s->block_state, p_state, sizeof(s->block_state));
2157 return score; 2184 return score;
2158 } 2185 }
2159 } 2186 }
2160 2187
2161 static always_inline int same_block(BlockNode *a, BlockNode *b){ 2188 static always_inline int same_block(BlockNode *a, BlockNode *b){
2162 if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){ 2189 if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
2163 return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2])); 2190 return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
2164 }else{ 2191 }else{
2165 return !((a->mx - b->mx) | (a->my - b->my) | ((a->type ^ b->type)&BLOCK_INTRA)); 2192 return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
2166 } 2193 }
2167 } 2194 }
2168 2195
2169 static void encode_q_branch2(SnowContext *s, int level, int x, int y){ 2196 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
2170 const int w= s->b_width << s->block_max_depth; 2197 const int w= s->b_width << s->block_max_depth;
2179 int pl = left->color[0]; 2206 int pl = left->color[0];
2180 int pcb= left->color[1]; 2207 int pcb= left->color[1];
2181 int pcr= left->color[2]; 2208 int pcr= left->color[2];
2182 int pmx= mid_pred(left->mx, top->mx, tr->mx); 2209 int pmx= mid_pred(left->mx, top->mx, tr->mx);
2183 int pmy= mid_pred(left->my, top->my, tr->my); 2210 int pmy= mid_pred(left->my, top->my, tr->my);
2184 int mx_context= av_log2(2*ABS(left->mx - top->mx)); 2211 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
2185 int my_context= av_log2(2*ABS(left->my - top->my)); 2212 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 16*!!b->ref;
2213 int my_context= av_log2(2*ABS(left->my - top->my)) + 16*!!b->ref;
2186 int s_context= 2*left->level + 2*top->level + tl->level + tr->level; 2214 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2187 2215
2188 if(s->keyframe){ 2216 if(s->keyframe){
2189 set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA); 2217 set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, 0, BLOCK_INTRA);
2190 return; 2218 return;
2191 } 2219 }
2192 2220
2193 if(level!=s->block_max_depth){ 2221 if(level!=s->block_max_depth){
2194 if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){ 2222 if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
2205 if(b->type & BLOCK_INTRA){ 2233 if(b->type & BLOCK_INTRA){
2206 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1); 2234 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
2207 put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1); 2235 put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
2208 put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1); 2236 put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
2209 put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1); 2237 put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
2210 set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, BLOCK_INTRA); 2238 set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
2211 }else{ 2239 }else{
2212 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0); 2240 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
2241 if(s->ref_frames > 1)
2242 put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
2213 put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1); 2243 put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
2214 put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1); 2244 put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
2215 set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, 0); 2245 set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
2216 } 2246 }
2217 } 2247 }
2218 2248
2219 static void decode_q_branch(SnowContext *s, int level, int x, int y){ 2249 static void decode_q_branch(SnowContext *s, int level, int x, int y){
2220 const int w= s->b_width << s->block_max_depth; 2250 const int w= s->b_width << s->block_max_depth;
2226 BlockNode *tl = y && x ? &s->block[index-w-1] : left; 2256 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
2227 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt 2257 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
2228 int s_context= 2*left->level + 2*top->level + tl->level + tr->level; 2258 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2229 2259
2230 if(s->keyframe){ 2260 if(s->keyframe){
2231 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA); 2261 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);
2232 return; 2262 return;
2233 } 2263 }
2234 2264
2235 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){ 2265 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
2236 int type; 2266 int type;
2237 int l = left->color[0]; 2267 int l = left->color[0];
2238 int cb= left->color[1]; 2268 int cb= left->color[1];
2239 int cr= left->color[2]; 2269 int cr= left->color[2];
2240 int mx= mid_pred(left->mx, top->mx, tr->mx); 2270 int mx= mid_pred(left->mx, top->mx, tr->mx);
2241 int my= mid_pred(left->my, top->my, tr->my); 2271 int my= mid_pred(left->my, top->my, tr->my);
2272 int ref = 0;
2273 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
2242 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx)); 2274 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
2243 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my)); 2275 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
2244 2276
2245 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0; 2277 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
2246 2278
2247 if(type){ 2279 if(type){
2248 l += get_symbol(&s->c, &s->block_state[32], 1); 2280 l += get_symbol(&s->c, &s->block_state[32], 1);
2249 cb+= get_symbol(&s->c, &s->block_state[64], 1); 2281 cb+= get_symbol(&s->c, &s->block_state[64], 1);
2250 cr+= get_symbol(&s->c, &s->block_state[96], 1); 2282 cr+= get_symbol(&s->c, &s->block_state[96], 1);
2251 }else{ 2283 }else{
2252 mx+= get_symbol(&s->c, &s->block_state[128 + 32*mx_context], 1); 2284 if(s->ref_frames > 1)
2253 my+= get_symbol(&s->c, &s->block_state[128 + 32*my_context], 1); 2285 ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
2254 } 2286 mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
2255 set_blocks(s, level, x, y, l, cb, cr, mx, my, type); 2287 my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
2288 }
2289 set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
2256 }else{ 2290 }else{
2257 decode_q_branch(s, level+1, 2*x+0, 2*y+0); 2291 decode_q_branch(s, level+1, 2*x+0, 2*y+0);
2258 decode_q_branch(s, level+1, 2*x+1, 2*y+0); 2292 decode_q_branch(s, level+1, 2*x+1, 2*y+0);
2259 decode_q_branch(s, level+1, 2*x+0, 2*y+1); 2293 decode_q_branch(s, level+1, 2*x+0, 2*y+1);
2260 decode_q_branch(s, level+1, 2*x+1, 2*y+1); 2294 decode_q_branch(s, level+1, 2*x+1, 2*y+1);
2378 mca( 0, 0,8) 2412 mca( 0, 0,8)
2379 mca( 8, 0,8) 2413 mca( 8, 0,8)
2380 mca( 0, 8,8) 2414 mca( 0, 8,8)
2381 mca( 8, 8,8) 2415 mca( 8, 8,8)
2382 2416
2383 static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){ 2417 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){
2384 if(block->type & BLOCK_INTRA){ 2418 if(block->type & BLOCK_INTRA){
2385 int x, y; 2419 int x, y;
2386 const int color = block->color[plane_index]; 2420 const int color = block->color[plane_index];
2387 const int color4= color*0x01010101; 2421 const int color4= color*0x01010101;
2388 if(b_w==32){ 2422 if(b_w==32){
2418 dst[x + y*stride]= color; 2452 dst[x + y*stride]= color;
2419 } 2453 }
2420 } 2454 }
2421 } 2455 }
2422 }else{ 2456 }else{
2457 uint8_t *src= s->last_picture[block->ref].data[plane_index];
2423 const int scale= plane_index ? s->mv_scale : 2*s->mv_scale; 2458 const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
2424 int mx= block->mx*scale; 2459 int mx= block->mx*scale;
2425 int my= block->my*scale; 2460 int my= block->my*scale;
2426 const int dx= mx&15; 2461 const int dx= mx&15;
2427 const int dy= my&15; 2462 const int dy= my&15;
2492 } 2527 }
2493 } 2528 }
2494 } 2529 }
2495 2530
2496 //FIXME name clenup (b_w, block_w, b_width stuff) 2531 //FIXME name clenup (b_w, block_w, b_width stuff)
2497 static always_inline void add_yblock_buffered(SnowContext *s, slice_buffer * sb, DWTELEM *old_dst, uint8_t *dst8, uint8_t *src, 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 plane_index){ 2532 static always_inline void add_yblock_buffered(SnowContext *s, slice_buffer * sb, DWTELEM *old_dst, uint8_t *dst8, 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 plane_index){
2498 DWTELEM * dst = NULL; 2533 DWTELEM * dst = NULL;
2499 const int b_width = s->b_width << s->block_max_depth; 2534 const int b_width = s->b_width << s->block_max_depth;
2500 const int b_height= s->b_height << s->block_max_depth; 2535 const int b_height= s->b_height << s->block_max_depth;
2501 const int b_stride= b_width; 2536 const int b_stride= b_width;
2502 BlockNode *lt= &s->block[b_x + b_y*b_stride]; 2537 BlockNode *lt= &s->block[b_x + b_y*b_stride];
2547 // src += src_x + src_y*src_stride; 2582 // src += src_x + src_y*src_stride;
2548 2583
2549 ptmp= tmp + 3*tmp_step; 2584 ptmp= tmp + 3*tmp_step;
2550 block[0]= ptmp; 2585 block[0]= ptmp;
2551 ptmp+=tmp_step; 2586 ptmp+=tmp_step;
2552 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h); 2587 pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
2553 2588
2554 if(same_block(lt, rt)){ 2589 if(same_block(lt, rt)){
2555 block[1]= block[0]; 2590 block[1]= block[0];
2556 }else{ 2591 }else{
2557 block[1]= ptmp; 2592 block[1]= ptmp;
2558 ptmp+=tmp_step; 2593 ptmp+=tmp_step;
2559 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h); 2594 pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
2560 } 2595 }
2561 2596
2562 if(same_block(lt, lb)){ 2597 if(same_block(lt, lb)){
2563 block[2]= block[0]; 2598 block[2]= block[0];
2564 }else if(same_block(rt, lb)){ 2599 }else if(same_block(rt, lb)){
2565 block[2]= block[1]; 2600 block[2]= block[1];
2566 }else{ 2601 }else{
2567 block[2]= ptmp; 2602 block[2]= ptmp;
2568 ptmp+=tmp_step; 2603 ptmp+=tmp_step;
2569 pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h); 2604 pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
2570 } 2605 }
2571 2606
2572 if(same_block(lt, rb) ){ 2607 if(same_block(lt, rb) ){
2573 block[3]= block[0]; 2608 block[3]= block[0];
2574 }else if(same_block(rt, rb)){ 2609 }else if(same_block(rt, rb)){
2575 block[3]= block[1]; 2610 block[3]= block[1];
2576 }else if(same_block(lb, rb)){ 2611 }else if(same_block(lb, rb)){
2577 block[3]= block[2]; 2612 block[3]= block[2];
2578 }else{ 2613 }else{
2579 block[3]= ptmp; 2614 block[3]= ptmp;
2580 pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h); 2615 pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
2581 } 2616 }
2582 #if 0 2617 #if 0
2583 for(y=0; y<b_h; y++){ 2618 for(y=0; y<b_h; y++){
2584 for(x=0; x<b_w; x++){ 2619 for(x=0; x<b_w; x++){
2585 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX); 2620 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
2622 } 2657 }
2623 #endif 2658 #endif
2624 } 2659 }
2625 2660
2626 //FIXME name clenup (b_w, block_w, b_width stuff) 2661 //FIXME name clenup (b_w, block_w, b_width stuff)
2627 static always_inline void add_yblock(SnowContext *s, DWTELEM *dst, uint8_t *dst8, uint8_t *src, 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){ 2662 static always_inline void add_yblock(SnowContext *s, DWTELEM *dst, uint8_t *dst8, 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){
2628 const int b_width = s->b_width << s->block_max_depth; 2663 const int b_width = s->b_width << s->block_max_depth;
2629 const int b_height= s->b_height << s->block_max_depth; 2664 const int b_height= s->b_height << s->block_max_depth;
2630 const int b_stride= b_width; 2665 const int b_stride= b_width;
2631 BlockNode *lt= &s->block[b_x + b_y*b_stride]; 2666 BlockNode *lt= &s->block[b_x + b_y*b_stride];
2632 BlockNode *rt= lt+1; 2667 BlockNode *rt= lt+1;
2681 // src += src_x + src_y*src_stride; 2716 // src += src_x + src_y*src_stride;
2682 2717
2683 ptmp= tmp + 3*tmp_step; 2718 ptmp= tmp + 3*tmp_step;
2684 block[0]= ptmp; 2719 block[0]= ptmp;
2685 ptmp+=tmp_step; 2720 ptmp+=tmp_step;
2686 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h); 2721 pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
2687 2722
2688 if(same_block(lt, rt)){ 2723 if(same_block(lt, rt)){
2689 block[1]= block[0]; 2724 block[1]= block[0];
2690 }else{ 2725 }else{
2691 block[1]= ptmp; 2726 block[1]= ptmp;
2692 ptmp+=tmp_step; 2727 ptmp+=tmp_step;
2693 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h); 2728 pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
2694 } 2729 }
2695 2730
2696 if(same_block(lt, lb)){ 2731 if(same_block(lt, lb)){
2697 block[2]= block[0]; 2732 block[2]= block[0];
2698 }else if(same_block(rt, lb)){ 2733 }else if(same_block(rt, lb)){
2699 block[2]= block[1]; 2734 block[2]= block[1];
2700 }else{ 2735 }else{
2701 block[2]= ptmp; 2736 block[2]= ptmp;
2702 ptmp+=tmp_step; 2737 ptmp+=tmp_step;
2703 pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h); 2738 pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
2704 } 2739 }
2705 2740
2706 if(same_block(lt, rb) ){ 2741 if(same_block(lt, rb) ){
2707 block[3]= block[0]; 2742 block[3]= block[0];
2708 }else if(same_block(rt, rb)){ 2743 }else if(same_block(rt, rb)){
2709 block[3]= block[1]; 2744 block[3]= block[1];
2710 }else if(same_block(lb, rb)){ 2745 }else if(same_block(lb, rb)){
2711 block[3]= block[2]; 2746 block[3]= block[2];
2712 }else{ 2747 }else{
2713 block[3]= ptmp; 2748 block[3]= ptmp;
2714 pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h); 2749 pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
2715 } 2750 }
2716 #if 0 2751 #if 0
2717 for(y=0; y<b_h; y++){ 2752 for(y=0; y<b_h; y++){
2718 for(x=0; x<b_w; x++){ 2753 for(x=0; x<b_w; x++){
2719 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX); 2754 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
2785 int block_size = MB_SIZE >> s->block_max_depth; 2820 int block_size = MB_SIZE >> s->block_max_depth;
2786 int block_w = plane_index ? block_size/2 : block_size; 2821 int block_w = plane_index ? block_size/2 : block_size;
2787 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth]; 2822 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
2788 int obmc_stride= plane_index ? block_size : 2*block_size; 2823 int obmc_stride= plane_index ? block_size : 2*block_size;
2789 int ref_stride= s->current_picture.linesize[plane_index]; 2824 int ref_stride= s->current_picture.linesize[plane_index];
2790 uint8_t *ref = s->last_picture.data[plane_index];
2791 uint8_t *dst8= s->current_picture.data[plane_index]; 2825 uint8_t *dst8= s->current_picture.data[plane_index];
2792 int w= p->width; 2826 int w= p->width;
2793 int h= p->height; 2827 int h= p->height;
2794 START_TIMER 2828 START_TIMER
2795 2829
2828 } 2862 }
2829 2863
2830 for(mb_x=0; mb_x<=mb_w; mb_x++){ 2864 for(mb_x=0; mb_x<=mb_w; mb_x++){
2831 START_TIMER 2865 START_TIMER
2832 2866
2833 add_yblock_buffered(s, sb, old_buffer, dst8, ref, obmc, 2867 add_yblock_buffered(s, sb, old_buffer, dst8, obmc,
2834 block_w*mb_x - block_w/2, 2868 block_w*mb_x - block_w/2,
2835 block_w*mb_y - block_w/2, 2869 block_w*mb_y - block_w/2,
2836 block_w, block_w, 2870 block_w, block_w,
2837 w, h, 2871 w, h,
2838 w, ref_stride, obmc_stride, 2872 w, ref_stride, obmc_stride,
2853 int block_size = MB_SIZE >> s->block_max_depth; 2887 int block_size = MB_SIZE >> s->block_max_depth;
2854 int block_w = plane_index ? block_size/2 : block_size; 2888 int block_w = plane_index ? block_size/2 : block_size;
2855 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth]; 2889 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
2856 const int obmc_stride= plane_index ? block_size : 2*block_size; 2890 const int obmc_stride= plane_index ? block_size : 2*block_size;
2857 int ref_stride= s->current_picture.linesize[plane_index]; 2891 int ref_stride= s->current_picture.linesize[plane_index];
2858 uint8_t *ref = s->last_picture.data[plane_index];
2859 uint8_t *dst8= s->current_picture.data[plane_index]; 2892 uint8_t *dst8= s->current_picture.data[plane_index];
2860 int w= p->width; 2893 int w= p->width;
2861 int h= p->height; 2894 int h= p->height;
2862 START_TIMER 2895 START_TIMER
2863 2896
2886 } 2919 }
2887 2920
2888 for(mb_x=0; mb_x<=mb_w; mb_x++){ 2921 for(mb_x=0; mb_x<=mb_w; mb_x++){
2889 START_TIMER 2922 START_TIMER
2890 2923
2891 add_yblock(s, buf, dst8, ref, obmc, 2924 add_yblock(s, buf, dst8, obmc,
2892 block_w*mb_x - block_w/2, 2925 block_w*mb_x - block_w/2,
2893 block_w*mb_y - block_w/2, 2926 block_w*mb_y - block_w/2,
2894 block_w, block_w, 2927 block_w, block_w,
2895 w, h, 2928 w, h,
2896 w, ref_stride, obmc_stride, 2929 w, ref_stride, obmc_stride,
2916 const int block_size = MB_SIZE >> s->block_max_depth; 2949 const int block_size = MB_SIZE >> s->block_max_depth;
2917 const int block_w = plane_index ? block_size/2 : block_size; 2950 const int block_w = plane_index ? block_size/2 : block_size;
2918 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth]; 2951 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
2919 const int obmc_stride= plane_index ? block_size : 2*block_size; 2952 const int obmc_stride= plane_index ? block_size : 2*block_size;
2920 const int ref_stride= s->current_picture.linesize[plane_index]; 2953 const int ref_stride= s->current_picture.linesize[plane_index];
2921 uint8_t *ref= s-> last_picture.data[plane_index];
2922 uint8_t *src= s-> input_picture.data[plane_index]; 2954 uint8_t *src= s-> input_picture.data[plane_index];
2923 DWTELEM *dst= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; 2955 DWTELEM *dst= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
2924 const int b_stride = s->b_width << s->block_max_depth; 2956 const int b_stride = s->b_width << s->block_max_depth;
2925 const int w= p->width; 2957 const int w= p->width;
2926 const int h= p->height; 2958 const int h= p->height;
2938 int mb_x2= mb_x + (i &1) - 1; 2970 int mb_x2= mb_x + (i &1) - 1;
2939 int mb_y2= mb_y + (i>>1) - 1; 2971 int mb_y2= mb_y + (i>>1) - 1;
2940 int x= block_w*mb_x2 + block_w/2; 2972 int x= block_w*mb_x2 + block_w/2;
2941 int y= block_w*mb_y2 + block_w/2; 2973 int y= block_w*mb_y2 + block_w/2;
2942 2974
2943 add_yblock(s, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, ref, obmc, 2975 add_yblock(s, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
2944 x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index); 2976 x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
2945 2977
2946 for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){ 2978 for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
2947 for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){ 2979 for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
2948 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride; 2980 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
2995 if(b->type & BLOCK_INTRA){ 3027 if(b->type & BLOCK_INTRA){
2996 return 3+2*( av_log2(2*ABS(left->color[0] - b->color[0])) 3028 return 3+2*( av_log2(2*ABS(left->color[0] - b->color[0]))
2997 + av_log2(2*ABS(left->color[1] - b->color[1])) 3029 + av_log2(2*ABS(left->color[1] - b->color[1]))
2998 + av_log2(2*ABS(left->color[2] - b->color[2]))); 3030 + av_log2(2*ABS(left->color[2] - b->color[2])));
2999 }else 3031 }else
3000 return 2*(1 + av_log2(2*ABS(dmx)) 3032 return 2*(1 + av_log2(2*ABS(dmx)) //FIXME kill the 2* can be merged in lambda
3001 + av_log2(2*ABS(dmy))); //FIXME kill the 2* can be merged in lambda 3033 + av_log2(2*ABS(dmy))
3034 + av_log2(2*b->ref));
3002 } 3035 }
3003 3036
3004 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){ 3037 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
3005 Plane *p= &s->plane[plane_index]; 3038 Plane *p= &s->plane[plane_index];
3006 const int block_size = MB_SIZE >> s->block_max_depth; 3039 const int block_size = MB_SIZE >> s->block_max_depth;
3007 const int block_w = plane_index ? block_size/2 : block_size; 3040 const int block_w = plane_index ? block_size/2 : block_size;
3008 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth]; 3041 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
3009 const int obmc_stride= plane_index ? block_size : 2*block_size; 3042 const int obmc_stride= plane_index ? block_size : 2*block_size;
3010 const int ref_stride= s->current_picture.linesize[plane_index]; 3043 const int ref_stride= s->current_picture.linesize[plane_index];
3011 uint8_t *ref= s-> last_picture.data[plane_index];
3012 uint8_t *dst= s->current_picture.data[plane_index]; 3044 uint8_t *dst= s->current_picture.data[plane_index];
3013 uint8_t *src= s-> input_picture.data[plane_index]; 3045 uint8_t *src= s-> input_picture.data[plane_index];
3014 DWTELEM *pred= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; 3046 DWTELEM *pred= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
3015 uint8_t cur[ref_stride*2*MB_SIZE]; //FIXME alignment 3047 uint8_t cur[ref_stride*2*MB_SIZE]; //FIXME alignment
3016 uint8_t tmp[ref_stride*(2*MB_SIZE+5)]; 3048 uint8_t tmp[ref_stride*(2*MB_SIZE+5)];
3027 int y0= FFMAX(0,-sy); 3059 int y0= FFMAX(0,-sy);
3028 int x1= FFMIN(block_w*2, w-sx); 3060 int x1= FFMIN(block_w*2, w-sx);
3029 int y1= FFMIN(block_w*2, h-sy); 3061 int y1= FFMIN(block_w*2, h-sy);
3030 int i,x,y; 3062 int i,x,y;
3031 3063
3032 pred_block(s, cur, ref, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h); 3064 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);
3033 3065
3034 for(y=y0; y<y1; y++){ 3066 for(y=y0; y<y1; y++){
3035 const uint8_t *obmc1= obmc_edged + y*obmc_stride; 3067 const uint8_t *obmc1= obmc_edged + y*obmc_stride;
3036 const DWTELEM *pred1 = pred + y*obmc_stride; 3068 const DWTELEM *pred1 = pred + y*obmc_stride;
3037 uint8_t *cur1 = cur + y*ref_stride; 3069 uint8_t *cur1 = cur + y*ref_stride;
3092 const int block_size = MB_SIZE >> s->block_max_depth; 3124 const int block_size = MB_SIZE >> s->block_max_depth;
3093 const int block_w = plane_index ? block_size/2 : block_size; 3125 const int block_w = plane_index ? block_size/2 : block_size;
3094 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth]; 3126 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
3095 const int obmc_stride= plane_index ? block_size : 2*block_size; 3127 const int obmc_stride= plane_index ? block_size : 2*block_size;
3096 const int ref_stride= s->current_picture.linesize[plane_index]; 3128 const int ref_stride= s->current_picture.linesize[plane_index];
3097 uint8_t *ref= s-> last_picture.data[plane_index];
3098 uint8_t *dst= s->current_picture.data[plane_index]; 3129 uint8_t *dst= s->current_picture.data[plane_index];
3099 uint8_t *src= s-> input_picture.data[plane_index]; 3130 uint8_t *src= s-> input_picture.data[plane_index];
3100 const static DWTELEM zero_dst[4096]; //FIXME 3131 const static DWTELEM zero_dst[4096]; //FIXME
3101 const int b_stride = s->b_width << s->block_max_depth; 3132 const int b_stride = s->b_width << s->block_max_depth;
3102 const int b_height = s->b_height<< s->block_max_depth; 3133 const int b_height = s->b_height<< s->block_max_depth;
3110 int mb_x2= mb_x + (i%3) - 1; 3141 int mb_x2= mb_x + (i%3) - 1;
3111 int mb_y2= mb_y + (i/3) - 1; 3142 int mb_y2= mb_y + (i/3) - 1;
3112 int x= block_w*mb_x2 + block_w/2; 3143 int x= block_w*mb_x2 + block_w/2;
3113 int y= block_w*mb_y2 + block_w/2; 3144 int y= block_w*mb_y2 + block_w/2;
3114 3145
3115 add_yblock(s, zero_dst, dst, ref, obmc, 3146 add_yblock(s, zero_dst, dst, obmc,
3116 x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index); 3147 x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
3117 3148
3118 //FIXME find a cleaner/simpler way to skip the outside stuff 3149 //FIXME find a cleaner/simpler way to skip the outside stuff
3119 for(y2= y; y2<0; y2++) 3150 for(y2= y; y2<0; y2++)
3120 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w); 3151 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
3166 block->color[1] = p[1]; 3197 block->color[1] = p[1];
3167 block->color[2] = p[2]; 3198 block->color[2] = p[2];
3168 block->type |= BLOCK_INTRA; 3199 block->type |= BLOCK_INTRA;
3169 }else{ 3200 }else{
3170 index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1); 3201 index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
3171 value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6); 3202 value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
3172 if(s->me_cache[index] == value) 3203 if(s->me_cache[index] == value)
3173 return 0; 3204 return 0;
3174 s->me_cache[index]= value; 3205 s->me_cache[index]= value;
3175 3206
3176 block->mx= p[0]; 3207 block->mx= p[0];
3194 static 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){ 3225 static 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){
3195 int p[2] = {p0, p1}; 3226 int p[2] = {p0, p1};
3196 return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd); 3227 return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
3197 } 3228 }
3198 3229
3199 static always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int *best_rd){ 3230 static always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
3200 const int b_stride= s->b_width << s->block_max_depth; 3231 const int b_stride= s->b_width << s->block_max_depth;
3201 BlockNode *block= &s->block[mb_x + mb_y * b_stride]; 3232 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
3202 BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]}; 3233 BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
3203 int rd, index, value; 3234 int rd, index, value;
3204 3235
3205 assert(mb_x>=0 && mb_y>=0); 3236 assert(mb_x>=0 && mb_y>=0);
3206 assert(mb_x<b_stride); 3237 assert(mb_x<b_stride);
3207 assert(((mb_x|mb_y)&1) == 0); 3238 assert(((mb_x|mb_y)&1) == 0);
3208 3239
3209 index= (p0 + 31*p1) & (ME_CACHE_SIZE-1); 3240 index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
3210 value= s->me_cache_generation + (p0>>10) + (p1<<6); 3241 value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
3211 if(s->me_cache[index] == value) 3242 if(s->me_cache[index] == value)
3212 return 0; 3243 return 0;
3213 s->me_cache[index]= value; 3244 s->me_cache[index]= value;
3214 3245
3215 block->mx= p0; 3246 block->mx= p0;
3216 block->my= p1; 3247 block->my= p1;
3248 block->ref= ref;
3217 block->type &= ~BLOCK_INTRA; 3249 block->type &= ~BLOCK_INTRA;
3218 block[1]= block[b_stride]= block[b_stride+1]= *block; 3250 block[1]= block[b_stride]= block[b_stride+1]= *block;
3219 3251
3220 rd= get_4block_rd(s, mb_x, mb_y, 0); 3252 rd= get_4block_rd(s, mb_x, mb_y, 0);
3221 3253
3253 for(pass=0; pass<50; pass++){ 3285 for(pass=0; pass<50; pass++){
3254 int change= 0; 3286 int change= 0;
3255 3287
3256 for(mb_y= 0; mb_y<b_height; mb_y++){ 3288 for(mb_y= 0; mb_y<b_height; mb_y++){
3257 for(mb_x= 0; mb_x<b_width; mb_x++){ 3289 for(mb_x= 0; mb_x<b_width; mb_x++){
3258 int dia_change, i, j; 3290 int dia_change, i, j, ref;
3259 int best_rd= INT_MAX; 3291 int best_rd= INT_MAX, ref_rd;
3260 BlockNode backup; 3292 BlockNode backup, ref_b;
3261 const int index= mb_x + mb_y * b_stride; 3293 const int index= mb_x + mb_y * b_stride;
3262 BlockNode *block= &s->block[index]; 3294 BlockNode *block= &s->block[index];
3263 BlockNode *tb = mb_y ? &s->block[index-b_stride ] : &null_block; 3295 BlockNode *tb = mb_y ? &s->block[index-b_stride ] : &null_block;
3264 BlockNode *lb = mb_x ? &s->block[index -1] : &null_block; 3296 BlockNode *lb = mb_x ? &s->block[index -1] : &null_block;
3265 BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : &null_block; 3297 BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : &null_block;
3341 int color0[3]= {block->color[0], block->color[1], block->color[2]}; 3373 int color0[3]= {block->color[0], block->color[1], block->color[2]};
3342 check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd); 3374 check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
3343 }else 3375 }else
3344 check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd); 3376 check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
3345 3377
3346 check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd); 3378 ref_b= *block;
3347 check_block_inter(s, mb_x, mb_y, tb->mx, tb->my, *obmc_edged, &best_rd); 3379 ref_rd= best_rd;
3348 check_block_inter(s, mb_x, mb_y, lb->mx, lb->my, *obmc_edged, &best_rd); 3380 for(ref=0; ref < s->ref_frames; ref++){
3349 check_block_inter(s, mb_x, mb_y, rb->mx, rb->my, *obmc_edged, &best_rd); 3381 int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
3350 check_block_inter(s, mb_x, mb_y, bb->mx, bb->my, *obmc_edged, &best_rd); 3382 if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
3351 3383 continue;
3352 /* fullpel ME */ 3384 block->ref= ref;
3353 //FIXME avoid subpel interpol / round to nearest integer 3385 best_rd= INT_MAX;
3354 do{ 3386
3355 dia_change=0; 3387 check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
3356 for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){ 3388 check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
3357 for(j=0; j<i; j++){ 3389 if(tb!=&null_block)
3358 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd); 3390 check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
3359 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd); 3391 if(lb!=&null_block)
3360 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd); 3392 check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
3361 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd); 3393 if(rb!=&null_block)
3394 check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
3395 if(bb!=&null_block)
3396 check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
3397
3398 /* fullpel ME */
3399 //FIXME avoid subpel interpol / round to nearest integer
3400 do{
3401 dia_change=0;
3402 for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
3403 for(j=0; j<i; j++){
3404 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
3405 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
3406 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
3407 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
3408 }
3362 } 3409 }
3410 }while(dia_change);
3411 /* subpel ME */
3412 do{
3413 static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
3414 dia_change=0;
3415 for(i=0; i<8; i++)
3416 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
3417 }while(dia_change);
3418 //FIXME or try the standard 2 pass qpel or similar
3419
3420 mvr[0][0]= block->mx;
3421 mvr[0][1]= block->my;
3422 if(ref_rd > best_rd){
3423 ref_rd= best_rd;
3424 ref_b= *block;
3363 } 3425 }
3364 }while(dia_change); 3426 }
3365 /* subpel ME */ 3427 best_rd= ref_rd;
3366 do{ 3428 *block= ref_b;
3367 static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
3368 dia_change=0;
3369 for(i=0; i<8; i++)
3370 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
3371 }while(dia_change);
3372 //FIXME or try the standard 2 pass qpel or similar
3373 #if 1 3429 #if 1
3374 check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd); 3430 check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
3375 //FIXME RD style color selection 3431 //FIXME RD style color selection
3376 #endif 3432 #endif
3377 if(!same_block(block, &backup)){ 3433 if(!same_block(block, &backup)){
3414 memset(s->me_cache, 0, sizeof(s->me_cache)); 3470 memset(s->me_cache, 0, sizeof(s->me_cache));
3415 s->me_cache_generation += 1<<22; 3471 s->me_cache_generation += 1<<22;
3416 3472
3417 init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0); 3473 init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
3418 3474
3475 //FIXME more multiref search?
3419 check_4block_inter(s, mb_x, mb_y, 3476 check_4block_inter(s, mb_x, mb_y,
3420 (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2, 3477 (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
3421 (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, &best_rd); 3478 (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
3422 3479
3423 for(i=0; i<4; i++) 3480 for(i=0; i<4; i++)
3424 if(!(b[i]->type&BLOCK_INTRA)) 3481 if(!(b[i]->type&BLOCK_INTRA))
3425 check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, &best_rd); 3482 check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
3426 3483
3427 if(init_rd != best_rd) 3484 if(init_rd != best_rd)
3428 change++; 3485 change++;
3429 } 3486 }
3430 } 3487 }
3646 put_symbol(&s->c, s->header_state, s->colorspace_type, 0); 3703 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
3647 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0); 3704 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
3648 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0); 3705 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
3649 put_rac(&s->c, s->header_state, s->spatial_scalability); 3706 put_rac(&s->c, s->header_state, s->spatial_scalability);
3650 // put_rac(&s->c, s->header_state, s->rate_scalability); 3707 // put_rac(&s->c, s->header_state, s->rate_scalability);
3708 put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
3651 3709
3652 for(plane_index=0; plane_index<2; plane_index++){ 3710 for(plane_index=0; plane_index<2; plane_index++){
3653 for(level=0; level<s->spatial_decomposition_count; level++){ 3711 for(level=0; level<s->spatial_decomposition_count; level++){
3654 for(orientation=level ? 1:0; orientation<4; orientation++){ 3712 for(orientation=level ? 1:0; orientation<4; orientation++){
3655 if(orientation==2) continue; 3713 if(orientation==2) continue;
3687 s->colorspace_type= get_symbol(&s->c, s->header_state, 0); 3745 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
3688 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0); 3746 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
3689 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0); 3747 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
3690 s->spatial_scalability= get_rac(&s->c, s->header_state); 3748 s->spatial_scalability= get_rac(&s->c, s->header_state);
3691 // s->rate_scalability= get_rac(&s->c, s->header_state); 3749 // s->rate_scalability= get_rac(&s->c, s->header_state);
3750 s->max_ref_frames= get_symbol(&s->c, s->header_state, 0)+1;
3692 3751
3693 for(plane_index=0; plane_index<3; plane_index++){ 3752 for(plane_index=0; plane_index<3; plane_index++){
3694 for(level=0; level<s->spatial_decomposition_count; level++){ 3753 for(level=0; level<s->spatial_decomposition_count; level++){
3695 for(orientation=level ? 1:0; orientation<4; orientation++){ 3754 for(orientation=level ? 1:0; orientation<4; orientation++){
3696 int q; 3755 int q;
3949 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t)); 4008 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
3950 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t)); 4009 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
3951 s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t)); 4010 s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
3952 h263_encode_init(&s->m); //mv_penalty 4011 h263_encode_init(&s->m); //mv_penalty
3953 4012
4013 s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
4014
3954 if(avctx->flags&CODEC_FLAG_PASS1){ 4015 if(avctx->flags&CODEC_FLAG_PASS1){
3955 if(!avctx->stats_out) 4016 if(!avctx->stats_out)
3956 avctx->stats_out = av_mallocz(256); 4017 avctx->stats_out = av_mallocz(256);
3957 } 4018 }
3958 if(!(avctx->flags&CODEC_FLAG_QSCALE)){ 4019 if(!(avctx->flags&CODEC_FLAG_QSCALE)){
3990 ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp); 4051 ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
3991 ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp); 4052 ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
3992 4053
3993 s->avctx->get_buffer(s->avctx, &s->input_picture); 4054 s->avctx->get_buffer(s->avctx, &s->input_picture);
3994 4055
4056 if(s->avctx->me_method == ME_ITER){
4057 int i;
4058 int size= s->b_width * s->b_height << 2*s->block_max_depth;
4059 for(i=0; i<s->max_ref_frames; i++){
4060 s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
4061 s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
4062 }
4063 }
4064
3995 return 0; 4065 return 0;
3996 } 4066 }
3997 4067
3998 static int frame_start(SnowContext *s){ 4068 static int frame_start(SnowContext *s){
3999 AVFrame tmp; 4069 AVFrame tmp;
4004 draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH ); 4074 draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
4005 draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2); 4075 draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
4006 draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2); 4076 draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
4007 } 4077 }
4008 4078
4009 tmp= s->last_picture; 4079 tmp= s->last_picture[s->max_ref_frames-1];
4010 s->last_picture= s->current_picture; 4080 memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
4081 s->last_picture[0]= s->current_picture;
4011 s->current_picture= tmp; 4082 s->current_picture= tmp;
4083
4084 if(s->keyframe){
4085 s->ref_frames= 0;
4086 }else{
4087 int i;
4088 for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
4089 if(i && s->last_picture[i-1].key_frame)
4090 break;
4091 s->ref_frames= i;
4092 }
4012 4093
4013 s->current_picture.reference= 1; 4094 s->current_picture.reference= 1;
4014 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){ 4095 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
4015 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 4096 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
4016 return -1; 4097 return -1;
4017 } 4098 }
4099
4100 s->current_picture.key_frame= s->keyframe;
4018 4101
4019 return 0; 4102 return 0;
4020 } 4103 }
4021 4104
4022 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 4105 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
4064 s->qlog= LOSSLESS_QLOG; 4147 s->qlog= LOSSLESS_QLOG;
4065 s->lambda = 0; 4148 s->lambda = 0;
4066 }//else keep previous frame's qlog until after motion est 4149 }//else keep previous frame's qlog until after motion est
4067 4150
4068 frame_start(s); 4151 frame_start(s);
4069 s->current_picture.key_frame= s->keyframe;
4070 4152
4071 s->m.current_picture_ptr= &s->m.current_picture; 4153 s->m.current_picture_ptr= &s->m.current_picture;
4072 if(pict->pict_type == P_TYPE){ 4154 if(pict->pict_type == P_TYPE){
4073 int block_width = (width +15)>>4; 4155 int block_width = (width +15)>>4;
4074 int block_height= (height+15)>>4; 4156 int block_height= (height+15)>>4;
4075 int stride= s->current_picture.linesize[0]; 4157 int stride= s->current_picture.linesize[0];
4076 4158
4077 assert(s->current_picture.data[0]); 4159 assert(s->current_picture.data[0]);
4078 assert(s->last_picture.data[0]); 4160 assert(s->last_picture[0].data[0]);
4079 4161
4080 s->m.avctx= s->avctx; 4162 s->m.avctx= s->avctx;
4081 s->m.current_picture.data[0]= s->current_picture.data[0]; 4163 s->m.current_picture.data[0]= s->current_picture.data[0];
4082 s->m. last_picture.data[0]= s-> last_picture.data[0]; 4164 s->m. last_picture.data[0]= s->last_picture[0].data[0];
4083 s->m. new_picture.data[0]= s-> input_picture.data[0]; 4165 s->m. new_picture.data[0]= s-> input_picture.data[0];
4084 s->m. last_picture_ptr= &s->m. last_picture; 4166 s->m. last_picture_ptr= &s->m. last_picture;
4085 s->m.linesize= 4167 s->m.linesize=
4086 s->m. last_picture.linesize[0]= 4168 s->m. last_picture.linesize[0]=
4087 s->m. new_picture.linesize[0]= 4169 s->m. new_picture.linesize[0]=
4143 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){ 4225 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
4144 ff_init_range_encoder(c, buf, buf_size); 4226 ff_init_range_encoder(c, buf, buf_size);
4145 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 4227 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
4146 pict->pict_type= FF_I_TYPE; 4228 pict->pict_type= FF_I_TYPE;
4147 s->keyframe=1; 4229 s->keyframe=1;
4230 s->current_picture.key_frame=1;
4148 reset_contexts(s); 4231 reset_contexts(s);
4149 goto redo_frame; 4232 goto redo_frame;
4150 } 4233 }
4151 4234
4152 if(s->qlog == LOSSLESS_QLOG){ 4235 if(s->qlog == LOSSLESS_QLOG){
4209 s->avctx->error[plane_index] += error; 4292 s->avctx->error[plane_index] += error;
4210 s->current_picture.error[plane_index] = error; 4293 s->current_picture.error[plane_index] = error;
4211 } 4294 }
4212 } 4295 }
4213 4296
4214 if(s->last_picture.data[0]) 4297 if(s->last_picture[s->max_ref_frames-1].data[0])
4215 avctx->release_buffer(avctx, &s->last_picture); 4298 avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
4216 4299
4217 s->current_picture.coded_picture_number = avctx->frame_number; 4300 s->current_picture.coded_picture_number = avctx->frame_number;
4218 s->current_picture.pict_type = pict->pict_type; 4301 s->current_picture.pict_type = pict->pict_type;
4219 s->current_picture.quality = pict->quality; 4302 s->current_picture.quality = pict->quality;
4220 s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start); 4303 s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
4233 4316
4234 return ff_rac_terminate(c); 4317 return ff_rac_terminate(c);
4235 } 4318 }
4236 4319
4237 static void common_end(SnowContext *s){ 4320 static void common_end(SnowContext *s){
4238 int plane_index, level, orientation; 4321 int plane_index, level, orientation, i;
4239 4322
4240 av_freep(&s->spatial_dwt_buffer); 4323 av_freep(&s->spatial_dwt_buffer);
4241 4324
4242 av_freep(&s->m.me.scratchpad); 4325 av_freep(&s->m.me.scratchpad);
4243 av_freep(&s->m.me.map); 4326 av_freep(&s->m.me.map);
4244 av_freep(&s->m.me.score_map); 4327 av_freep(&s->m.me.score_map);
4245 av_freep(&s->m.obmc_scratchpad); 4328 av_freep(&s->m.obmc_scratchpad);
4246 4329
4247 av_freep(&s->block); 4330 av_freep(&s->block);
4331
4332 for(i=0; i<MAX_REF_FRAMES; i++){
4333 av_freep(&s->ref_mvs[i]);
4334 av_freep(&s->ref_scores[i]);
4335 if(s->last_picture[i].data[0])
4336 s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
4337 }
4248 4338
4249 for(plane_index=0; plane_index<3; plane_index++){ 4339 for(plane_index=0; plane_index<3; plane_index++){
4250 for(level=s->spatial_decomposition_count-1; level>=0; level--){ 4340 for(level=s->spatial_decomposition_count-1; level>=0; level--){
4251 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 4341 for(orientation=level ? 1 : 0; orientation<4; orientation++){
4252 SubBand *b= &s->plane[plane_index].band[level][orientation]; 4342 SubBand *b= &s->plane[plane_index].band[level][orientation];
4417 STOP_TIMER("idwt + predict_slices")} 4507 STOP_TIMER("idwt + predict_slices")}
4418 } 4508 }
4419 4509
4420 emms_c(); 4510 emms_c();
4421 4511
4422 if(s->last_picture.data[0]) 4512 if(s->last_picture[s->max_ref_frames-1].data[0])
4423 avctx->release_buffer(avctx, &s->last_picture); 4513 avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
4424 4514
4425 if(!(s->avctx->debug&2048)) 4515 if(!(s->avctx->debug&2048))
4426 *picture= s->current_picture; 4516 *picture= s->current_picture;
4427 else 4517 else
4428 *picture= s->mconly_picture; 4518 *picture= s->mconly_picture;