comparison h264.c @ 2415:db2cf6005d19 libavcodec

H.264 weighted prediction. Bidirectional weighting has not been tested for bitwise accuracy, but looks correct.
author lorenm
date Fri, 07 Jan 2005 05:56:07 +0000
parents 9d780478f8a5
children 06b7d678b582
comparison
equal deleted inserted replaced
2414:18d77621fbc7 2415:db2cf6005d19
253 * max_frame_num or 2*max_frame_num for field pics. 253 * max_frame_num or 2*max_frame_num for field pics.
254 */ 254 */
255 int max_pic_num; 255 int max_pic_num;
256 256
257 //Weighted pred stuff 257 //Weighted pred stuff
258 int use_weight;
259 int use_weight_chroma;
258 int luma_log2_weight_denom; 260 int luma_log2_weight_denom;
259 int chroma_log2_weight_denom; 261 int chroma_log2_weight_denom;
260 int luma_weight[2][16]; 262 int luma_weight[2][16];
261 int luma_offset[2][16]; 263 int luma_offset[2][16];
262 int chroma_weight[2][16][2]; 264 int chroma_weight[2][16][2];
263 int chroma_offset[2][16][2]; 265 int chroma_offset[2][16][2];
266 int implicit_weight[16][16];
264 267
265 //deblock 268 //deblock
266 int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0 269 int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
267 int slice_alpha_c0_offset; 270 int slice_alpha_c0_offset;
268 int slice_beta_offset; 271 int slice_beta_offset;
2201 src_cr= s->edge_emu_buffer; 2204 src_cr= s->edge_emu_buffer;
2202 } 2205 }
2203 chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7); 2206 chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
2204 } 2207 }
2205 2208
2206 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta, 2209 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
2207 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 2210 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2208 int x_offset, int y_offset, 2211 int x_offset, int y_offset,
2209 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put, 2212 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2210 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg, 2213 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
2211 int list0, int list1){ 2214 int list0, int list1){
2235 dest_y, dest_cb, dest_cr, x_offset, y_offset, 2238 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2236 qpix_op, chroma_op); 2239 qpix_op, chroma_op);
2237 } 2240 }
2238 } 2241 }
2239 2242
2243 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
2244 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2245 int x_offset, int y_offset,
2246 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2247 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
2248 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
2249 int list0, int list1){
2250 MpegEncContext * const s = &h->s;
2251
2252 dest_y += 2*x_offset + 2*y_offset*s-> linesize;
2253 dest_cb += x_offset + y_offset*s->uvlinesize;
2254 dest_cr += x_offset + y_offset*s->uvlinesize;
2255 x_offset += 8*s->mb_x;
2256 y_offset += 8*s->mb_y;
2257
2258 if(list0 && list1){
2259 /* don't optimize for luma-only case, since B-frames usually
2260 * use implicit weights => chroma too. */
2261 uint8_t *tmp_cb = s->obmc_scratchpad;
2262 uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize;
2263 uint8_t *tmp_y = tmp_cr + 8*s->uvlinesize;
2264 int refn0 = h->ref_cache[0][ scan8[n] ];
2265 int refn1 = h->ref_cache[1][ scan8[n] ];
2266
2267 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
2268 dest_y, dest_cb, dest_cr,
2269 x_offset, y_offset, qpix_put, chroma_put);
2270 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
2271 tmp_y, tmp_cb, tmp_cr,
2272 x_offset, y_offset, qpix_put, chroma_put);
2273
2274 if(h->use_weight == 2){
2275 int weight0 = h->implicit_weight[refn0][refn1];
2276 int weight1 = 64 - weight0;
2277 luma_weight_avg( dest_y, tmp_y, s-> linesize, 5, weight0, weight1, 0, 0);
2278 chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0, 0);
2279 chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0, 0);
2280 }else{
2281 luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom,
2282 h->luma_weight[0][refn0], h->luma_weight[1][refn1],
2283 h->luma_offset[0][refn0], h->luma_offset[1][refn1]);
2284 chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom,
2285 h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
2286 h->chroma_offset[0][refn0][0], h->chroma_offset[1][refn1][0]);
2287 chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom,
2288 h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
2289 h->chroma_offset[0][refn0][1], h->chroma_offset[1][refn1][1]);
2290 }
2291 }else{
2292 int list = list1 ? 1 : 0;
2293 int refn = h->ref_cache[list][ scan8[n] ];
2294 Picture *ref= &h->ref_list[list][refn];
2295 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
2296 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2297 qpix_put, chroma_put);
2298
2299 luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom,
2300 h->luma_weight[list][refn], h->luma_offset[list][refn]);
2301 if(h->use_weight_chroma){
2302 chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom,
2303 h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
2304 chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom,
2305 h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
2306 }
2307 }
2308 }
2309
2310 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
2311 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2312 int x_offset, int y_offset,
2313 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2314 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
2315 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
2316 int list0, int list1){
2317 if((h->use_weight==2 && list0 && list1
2318 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
2319 || h->use_weight==1)
2320 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
2321 x_offset, y_offset, qpix_put, chroma_put,
2322 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
2323 else
2324 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
2325 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
2326 }
2327
2240 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 2328 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2241 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put), 2329 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
2242 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg)){ 2330 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
2331 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
2243 MpegEncContext * const s = &h->s; 2332 MpegEncContext * const s = &h->s;
2244 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 2333 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
2245 const int mb_type= s->current_picture.mb_type[mb_xy]; 2334 const int mb_type= s->current_picture.mb_type[mb_xy];
2246 2335
2247 assert(IS_INTER(mb_type)); 2336 assert(IS_INTER(mb_type));
2248 2337
2249 if(IS_16X16(mb_type)){ 2338 if(IS_16X16(mb_type)){
2250 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0, 2339 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
2251 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0], 2340 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
2341 &weight_op[0], &weight_avg[0],
2252 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); 2342 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2253 }else if(IS_16X8(mb_type)){ 2343 }else if(IS_16X8(mb_type)){
2254 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0, 2344 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
2255 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0], 2345 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
2346 &weight_op[1], &weight_avg[1],
2256 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); 2347 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2257 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4, 2348 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
2258 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0], 2349 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
2350 &weight_op[1], &weight_avg[1],
2259 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1)); 2351 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2260 }else if(IS_8X16(mb_type)){ 2352 }else if(IS_8X16(mb_type)){
2261 mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0, 2353 mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
2262 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], 2354 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2355 &weight_op[2], &weight_avg[2],
2263 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); 2356 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2264 mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0, 2357 mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
2265 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], 2358 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2359 &weight_op[2], &weight_avg[2],
2266 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1)); 2360 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2267 }else{ 2361 }else{
2268 int i; 2362 int i;
2269 2363
2270 assert(IS_8X8(mb_type)); 2364 assert(IS_8X8(mb_type));
2276 int y_offset= (i&2)<<1; 2370 int y_offset= (i&2)<<1;
2277 2371
2278 if(IS_SUB_8X8(sub_mb_type)){ 2372 if(IS_SUB_8X8(sub_mb_type)){
2279 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset, 2373 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2280 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], 2374 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2375 &weight_op[3], &weight_avg[3],
2281 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); 2376 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2282 }else if(IS_SUB_8X4(sub_mb_type)){ 2377 }else if(IS_SUB_8X4(sub_mb_type)){
2283 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset, 2378 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2284 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1], 2379 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
2380 &weight_op[4], &weight_avg[4],
2285 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); 2381 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2286 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2, 2382 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
2287 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1], 2383 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
2384 &weight_op[4], &weight_avg[4],
2288 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); 2385 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2289 }else if(IS_SUB_4X8(sub_mb_type)){ 2386 }else if(IS_SUB_4X8(sub_mb_type)){
2290 mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset, 2387 mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2291 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], 2388 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2389 &weight_op[5], &weight_avg[5],
2292 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); 2390 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2293 mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset, 2391 mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
2294 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], 2392 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2393 &weight_op[5], &weight_avg[5],
2295 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); 2394 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2296 }else{ 2395 }else{
2297 int j; 2396 int j;
2298 assert(IS_SUB_4X4(sub_mb_type)); 2397 assert(IS_SUB_4X4(sub_mb_type));
2299 for(j=0; j<4; j++){ 2398 for(j=0; j<4; j++){
2300 int sub_x_offset= x_offset + 2*(j&1); 2399 int sub_x_offset= x_offset + 2*(j&1);
2301 int sub_y_offset= y_offset + (j&2); 2400 int sub_y_offset= y_offset + (j&2);
2302 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset, 2401 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
2303 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], 2402 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2403 &weight_op[6], &weight_avg[6],
2304 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); 2404 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2305 } 2405 }
2306 } 2406 }
2307 } 2407 }
2308 } 2408 }
2395 av_freep(&h->top_border); 2495 av_freep(&h->top_border);
2396 h->slice_table= NULL; 2496 h->slice_table= NULL;
2397 2497
2398 av_freep(&h->mb2b_xy); 2498 av_freep(&h->mb2b_xy);
2399 av_freep(&h->mb2b8_xy); 2499 av_freep(&h->mb2b8_xy);
2500
2501 av_freep(&h->s.obmc_scratchpad);
2400 } 2502 }
2401 2503
2402 /** 2504 /**
2403 * allocates tables. 2505 * allocates tables.
2404 * needs widzh/height 2506 * needs widzh/height
2435 2537
2436 h->mb2b_xy [mb_xy]= b_xy; 2538 h->mb2b_xy [mb_xy]= b_xy;
2437 h->mb2b8_xy[mb_xy]= b8_xy; 2539 h->mb2b8_xy[mb_xy]= b8_xy;
2438 } 2540 }
2439 } 2541 }
2542
2543 CHECKED_ALLOCZ(s->obmc_scratchpad, 16*s->linesize + 2*8*s->uvlinesize);
2440 2544
2441 return 0; 2545 return 0;
2442 fail: 2546 fail:
2443 free_tables(h); 2547 free_tables(h);
2444 return -1; 2548 return -1;
2666 if(h->deblocking_filter) 2770 if(h->deblocking_filter)
2667 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0); 2771 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
2668 }else if(s->codec_id == CODEC_ID_H264){ 2772 }else if(s->codec_id == CODEC_ID_H264){
2669 hl_motion(h, dest_y, dest_cb, dest_cr, 2773 hl_motion(h, dest_y, dest_cb, dest_cr,
2670 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab, 2774 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
2671 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab); 2775 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
2776 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
2672 } 2777 }
2673 2778
2674 2779
2675 if(!IS_INTRA4x4(mb_type)){ 2780 if(!IS_INTRA4x4(mb_type)){
2676 if(s->codec_id == CODEC_ID_H264){ 2781 if(s->codec_id == CODEC_ID_H264){
2896 } 3001 }
2897 3002
2898 static int pred_weight_table(H264Context *h){ 3003 static int pred_weight_table(H264Context *h){
2899 MpegEncContext * const s = &h->s; 3004 MpegEncContext * const s = &h->s;
2900 int list, i; 3005 int list, i;
2901 3006 int luma_def, chroma_def;
3007
3008 h->use_weight= 0;
3009 h->use_weight_chroma= 0;
2902 h->luma_log2_weight_denom= get_ue_golomb(&s->gb); 3010 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
2903 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb); 3011 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
3012 luma_def = 1<<h->luma_log2_weight_denom;
3013 chroma_def = 1<<h->chroma_log2_weight_denom;
2904 3014
2905 for(list=0; list<2; list++){ 3015 for(list=0; list<2; list++){
2906 for(i=0; i<h->ref_count[list]; i++){ 3016 for(i=0; i<h->ref_count[list]; i++){
2907 int luma_weight_flag, chroma_weight_flag; 3017 int luma_weight_flag, chroma_weight_flag;
2908 3018
2909 luma_weight_flag= get_bits1(&s->gb); 3019 luma_weight_flag= get_bits1(&s->gb);
2910 if(luma_weight_flag){ 3020 if(luma_weight_flag){
2911 h->luma_weight[list][i]= get_se_golomb(&s->gb); 3021 h->luma_weight[list][i]= get_se_golomb(&s->gb);
2912 h->luma_offset[list][i]= get_se_golomb(&s->gb); 3022 h->luma_offset[list][i]= get_se_golomb(&s->gb);
3023 if( h->luma_weight[list][i] != luma_def
3024 || h->luma_offset[list][i] != 0)
3025 h->use_weight= 1;
3026 }else{
3027 h->luma_weight[list][i]= luma_def;
3028 h->luma_offset[list][i]= 0;
2913 } 3029 }
2914 3030
2915 chroma_weight_flag= get_bits1(&s->gb); 3031 chroma_weight_flag= get_bits1(&s->gb);
2916 if(chroma_weight_flag){ 3032 if(chroma_weight_flag){
2917 int j; 3033 int j;
2918 for(j=0; j<2; j++){ 3034 for(j=0; j<2; j++){
2919 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb); 3035 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
2920 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb); 3036 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
3037 if( h->chroma_weight[list][i][j] != chroma_def
3038 || h->chroma_offset[list][i][j] != 0)
3039 h->use_weight_chroma= 1;
3040 }
3041 }else{
3042 int j;
3043 for(j=0; j<2; j++){
3044 h->chroma_weight[list][i][j]= chroma_def;
3045 h->chroma_offset[list][i][j]= 0;
2921 } 3046 }
2922 } 3047 }
2923 } 3048 }
2924 if(h->slice_type != B_TYPE) break; 3049 if(h->slice_type != B_TYPE) break;
2925 } 3050 }
3051 h->use_weight= h->use_weight || h->use_weight_chroma;
2926 return 0; 3052 return 0;
3053 }
3054
3055 static void implicit_weight_table(H264Context *h){
3056 MpegEncContext * const s = &h->s;
3057 int list, i;
3058 int ref0, ref1;
3059 int cur_poc = s->current_picture_ptr->poc;
3060
3061 if( h->ref_count[0] == 1 && h->ref_count[1] == 1
3062 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
3063 h->use_weight= 0;
3064 h->use_weight_chroma= 0;
3065 return;
3066 }
3067
3068 h->use_weight= 2;
3069 h->use_weight_chroma= 2;
3070 h->luma_log2_weight_denom= 5;
3071 h->chroma_log2_weight_denom= 5;
3072
3073 /* FIXME: MBAFF */
3074 for(ref0=0; ref0 < h->ref_count[0]; ref0++){
3075 int poc0 = h->ref_list[0][ref0].poc;
3076 for(ref1=0; ref1 < h->ref_count[1]; ref1++){
3077 int poc1 = h->ref_list[0][ref1].poc;
3078 int td = clip(poc1 - poc0, -128, 127);
3079 if(td){
3080 int tb = clip(cur_poc - poc0, -128, 127);
3081 int tx = (16384 + (ABS(td) >> 1)) / td;
3082 int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
3083 if(dist_scale_factor < -64 || dist_scale_factor > 128)
3084 h->implicit_weight[ref0][ref1] = 32;
3085 else
3086 h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
3087 }else
3088 h->implicit_weight[ref0][ref1] = 32;
3089 }
3090 }
2927 } 3091 }
2928 3092
2929 /** 3093 /**
2930 * instantaneous decoder refresh. 3094 * instantaneous decoder refresh.
2931 */ 3095 */
3385 decode_ref_pic_list_reordering(h); 3549 decode_ref_pic_list_reordering(h);
3386 3550
3387 if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE )) 3551 if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
3388 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) ) 3552 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
3389 pred_weight_table(h); 3553 pred_weight_table(h);
3554 else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
3555 implicit_weight_table(h);
3556 else
3557 h->use_weight = 0;
3390 3558
3391 if(s->current_picture.reference) 3559 if(s->current_picture.reference)
3392 decode_ref_pic_marking(h); 3560 decode_ref_pic_marking(h);
3393 3561
3394 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ) 3562 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
3429 #endif 3597 #endif
3430 3598
3431 h->slice_num++; 3599 h->slice_num++;
3432 3600
3433 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ 3601 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
3434 av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d\n", 3602 av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d weight:%d%s\n",
3435 h->slice_num, first_mb_in_slice, 3603 h->slice_num, first_mb_in_slice,
3436 av_get_pict_type_char(h->slice_type), 3604 av_get_pict_type_char(h->slice_type),
3437 pps_id, h->frame_num, 3605 pps_id, h->frame_num,
3438 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1], 3606 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
3439 h->ref_count[0], h->ref_count[1], 3607 h->ref_count[0], h->ref_count[1],
3440 s->qscale, 3608 s->qscale,
3441 h->deblocking_filter 3609 h->deblocking_filter,
3610 h->use_weight,
3611 h->use_weight==1 && h->use_weight_chroma ? "c" : ""
3442 ); 3612 );
3443 } 3613 }
3444 3614
3445 return 0; 3615 return 0;
3446 } 3616 }