Revision 9f2d1b4f libavcodec/h264.c

View differences:

libavcodec/h264.c
255 255
    int max_pic_num;
256 256

  
257 257
    //Weighted pred stuff
258
    int use_weight;
259
    int use_weight_chroma;
258 260
    int luma_log2_weight_denom;
259 261
    int chroma_log2_weight_denom;
260 262
    int luma_weight[2][16];
261 263
    int luma_offset[2][16];
262 264
    int chroma_weight[2][16][2];
263 265
    int chroma_offset[2][16][2];
266
    int implicit_weight[16][16];
264 267
   
265 268
    //deblock
266 269
    int deblocking_filter;         ///< disable_deblocking_filter_idc with 1<->0 
......
2203 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 2210
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2208 2211
                           int x_offset, int y_offset,
2209 2212
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
......
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 2328
static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2241 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 2332
    MpegEncContext * const s = &h->s;
2244 2333
    const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
2245 2334
    const int mb_type= s->current_picture.mb_type[mb_xy];
......
2249 2338
    if(IS_16X16(mb_type)){
2250 2339
        mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
2251 2340
                qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
2341
                &weight_op[0], &weight_avg[0],
2252 2342
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2253 2343
    }else if(IS_16X8(mb_type)){
2254 2344
        mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
2255 2345
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
2346
                &weight_op[1], &weight_avg[1],
2256 2347
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2257 2348
        mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
2258 2349
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
2350
                &weight_op[1], &weight_avg[1],
2259 2351
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2260 2352
    }else if(IS_8X16(mb_type)){
2261 2353
        mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
2262 2354
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2355
                &weight_op[2], &weight_avg[2],
2263 2356
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2264 2357
        mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
2265 2358
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2359
                &weight_op[2], &weight_avg[2],
2266 2360
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2267 2361
    }else{
2268 2362
        int i;
......
2278 2372
            if(IS_SUB_8X8(sub_mb_type)){
2279 2373
                mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2280 2374
                    qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2375
                    &weight_op[3], &weight_avg[3],
2281 2376
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2282 2377
            }else if(IS_SUB_8X4(sub_mb_type)){
2283 2378
                mc_part(h, n  , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2284 2379
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
2380
                    &weight_op[4], &weight_avg[4],
2285 2381
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2286 2382
                mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
2287 2383
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
2384
                    &weight_op[4], &weight_avg[4],
2288 2385
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2289 2386
            }else if(IS_SUB_4X8(sub_mb_type)){
2290 2387
                mc_part(h, n  , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2291 2388
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2389
                    &weight_op[5], &weight_avg[5],
2292 2390
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2293 2391
                mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
2294 2392
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2393
                    &weight_op[5], &weight_avg[5],
2295 2394
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2296 2395
            }else{
2297 2396
                int j;
......
2301 2400
                    int sub_y_offset= y_offset +   (j&2);
2302 2401
                    mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
2303 2402
                        qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2403
                        &weight_op[6], &weight_avg[6],
2304 2404
                        IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2305 2405
                }
2306 2406
            }
......
2397 2497

  
2398 2498
    av_freep(&h->mb2b_xy);
2399 2499
    av_freep(&h->mb2b8_xy);
2500

  
2501
    av_freep(&h->s.obmc_scratchpad);
2400 2502
}
2401 2503

  
2402 2504
/**
......
2437 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 2545
    return 0;
2442 2546
fail:
......
2668 2772
    }else if(s->codec_id == CODEC_ID_H264){
2669 2773
        hl_motion(h, dest_y, dest_cb, dest_cr,
2670 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

  
......
2898 3003
static int pred_weight_table(H264Context *h){
2899 3004
    MpegEncContext * const s = &h->s;
2900 3005
    int list, i;
3006
    int luma_def, chroma_def;
2901 3007
    
3008
    h->use_weight= 0;
3009
    h->use_weight_chroma= 0;
2902 3010
    h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
2903 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 3015
    for(list=0; list<2; list++){
2906 3016
        for(i=0; i<h->ref_count[list]; i++){
......
2910 3020
            if(luma_weight_flag){
2911 3021
                h->luma_weight[list][i]= get_se_golomb(&s->gb);
2912 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 3031
            chroma_weight_flag= get_bits1(&s->gb);
......
2918 3034
                for(j=0; j<2; j++){
2919 3035
                    h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
2920 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 3049
        if(h->slice_type != B_TYPE) break;
2925 3050
    }
3051
    h->use_weight= h->use_weight || h->use_weight_chroma;
2926 3052
    return 0;
2927 3053
}
2928 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
    }
3091
}
3092

  
2929 3093
/**
2930 3094
 * instantaneous decoder refresh.
2931 3095
 */
......
3387 3551
    if(   (h->pps.weighted_pred          && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE )) 
3388 3552
       || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
3389 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 3559
    if(s->current_picture.reference)
3392 3560
        decode_ref_pic_marking(h);
......
3431 3599
    h->slice_num++;
3432 3600

  
3433 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 3603
               h->slice_num, first_mb_in_slice, 
3436 3604
               av_get_pict_type_char(h->slice_type),
3437 3605
               pps_id, h->frame_num,
3438 3606
               s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
3439 3607
               h->ref_count[0], h->ref_count[1],
3440 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

  

Also available in: Unified diff