~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/rv40.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
271
271
    return 0;
272
272
}
273
273
 
274
 
#define CLIP_SYMM(a, b) av_clip(a, -(b), b)
275
 
/**
276
 
 * weaker deblocking very similar to the one described in 4.4.2 of JVT-A003r1
277
 
 */
278
 
static inline void rv40_weak_loop_filter(uint8_t *src, const int step,
279
 
                                         const int filter_p1, const int filter_q1,
280
 
                                         const int alpha, const int beta,
281
 
                                         const int lim_p0q0,
282
 
                                         const int lim_q1, const int lim_p1,
283
 
                                         const int diff_p1p0, const int diff_q1q0,
284
 
                                         const int diff_p1p2, const int diff_q1q2)
285
 
{
286
 
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
287
 
    int t, u, diff;
288
 
 
289
 
    t = src[0*step] - src[-1*step];
290
 
    if(!t)
291
 
        return;
292
 
    u = (alpha * FFABS(t)) >> 7;
293
 
    if(u > 3 - (filter_p1 && filter_q1))
294
 
        return;
295
 
 
296
 
    t <<= 2;
297
 
    if(filter_p1 && filter_q1)
298
 
        t += src[-2*step] - src[1*step];
299
 
    diff = CLIP_SYMM((t + 4) >> 3, lim_p0q0);
300
 
    src[-1*step] = cm[src[-1*step] + diff];
301
 
    src[ 0*step] = cm[src[ 0*step] - diff];
302
 
    if(FFABS(diff_p1p2) <= beta && filter_p1){
303
 
        t = (diff_p1p0 + diff_p1p2 - diff) >> 1;
304
 
        src[-2*step] = cm[src[-2*step] - CLIP_SYMM(t, lim_p1)];
305
 
    }
306
 
    if(FFABS(diff_q1q2) <= beta && filter_q1){
307
 
        t = (diff_q1q0 + diff_q1q2 + diff) >> 1;
308
 
        src[ 1*step] = cm[src[ 1*step] - CLIP_SYMM(t, lim_q1)];
309
 
    }
310
 
}
311
 
 
312
 
static av_always_inline void rv40_adaptive_loop_filter(uint8_t *src, const int step,
313
 
                                             const int stride, const int dmode,
314
 
                                             const int lim_q1, const int lim_p1,
315
 
                                             const int alpha,
316
 
                                             const int beta, const int beta2,
317
 
                                             const int chroma, const int edge)
318
 
{
319
 
    int diff_p1p0[4], diff_q1q0[4], diff_p1p2[4], diff_q1q2[4];
320
 
    int sum_p1p0 = 0, sum_q1q0 = 0, sum_p1p2 = 0, sum_q1q2 = 0;
321
 
    uint8_t *ptr;
322
 
    int flag_strong0 = 1, flag_strong1 = 1;
323
 
    int filter_p1, filter_q1;
324
 
    int i;
325
 
    int lims;
326
 
 
327
 
    for(i = 0, ptr = src; i < 4; i++, ptr += stride){
328
 
        diff_p1p0[i] = ptr[-2*step] - ptr[-1*step];
329
 
        diff_q1q0[i] = ptr[ 1*step] - ptr[ 0*step];
330
 
        sum_p1p0 += diff_p1p0[i];
331
 
        sum_q1q0 += diff_q1q0[i];
332
 
    }
333
 
    filter_p1 = FFABS(sum_p1p0) < (beta<<2);
334
 
    filter_q1 = FFABS(sum_q1q0) < (beta<<2);
335
 
    if(!filter_p1 && !filter_q1)
336
 
        return;
337
 
 
338
 
    for(i = 0, ptr = src; i < 4; i++, ptr += stride){
339
 
        diff_p1p2[i] = ptr[-2*step] - ptr[-3*step];
340
 
        diff_q1q2[i] = ptr[ 1*step] - ptr[ 2*step];
341
 
        sum_p1p2 += diff_p1p2[i];
342
 
        sum_q1q2 += diff_q1q2[i];
343
 
    }
344
 
 
345
 
    if(edge){
346
 
        flag_strong0 = filter_p1 && (FFABS(sum_p1p2) < beta2);
347
 
        flag_strong1 = filter_q1 && (FFABS(sum_q1q2) < beta2);
348
 
    }else{
349
 
        flag_strong0 = flag_strong1 = 0;
350
 
    }
351
 
 
352
 
    lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;
353
 
    if(flag_strong0 && flag_strong1){ /* strong filtering */
354
 
        for(i = 0; i < 4; i++, src += stride){
355
 
            int sflag, p0, q0, p1, q1;
356
 
            int t = src[0*step] - src[-1*step];
357
 
 
358
 
            if(!t) continue;
359
 
            sflag = (alpha * FFABS(t)) >> 7;
360
 
            if(sflag > 1) continue;
361
 
 
362
 
            p0 = (25*src[-3*step] + 26*src[-2*step]
363
 
                + 26*src[-1*step]
364
 
                + 26*src[ 0*step] + 25*src[ 1*step] + rv40_dither_l[dmode + i]) >> 7;
365
 
            q0 = (25*src[-2*step] + 26*src[-1*step]
366
 
                + 26*src[ 0*step]
367
 
                + 26*src[ 1*step] + 25*src[ 2*step] + rv40_dither_r[dmode + i]) >> 7;
368
 
            if(sflag){
369
 
                p0 = av_clip(p0, src[-1*step] - lims, src[-1*step] + lims);
370
 
                q0 = av_clip(q0, src[ 0*step] - lims, src[ 0*step] + lims);
371
 
            }
372
 
            p1 = (25*src[-4*step] + 26*src[-3*step]
373
 
                + 26*src[-2*step]
374
 
                + 26*p0           + 25*src[ 0*step] + rv40_dither_l[dmode + i]) >> 7;
375
 
            q1 = (25*src[-1*step] + 26*q0
376
 
                + 26*src[ 1*step]
377
 
                + 26*src[ 2*step] + 25*src[ 3*step] + rv40_dither_r[dmode + i]) >> 7;
378
 
            if(sflag){
379
 
                p1 = av_clip(p1, src[-2*step] - lims, src[-2*step] + lims);
380
 
                q1 = av_clip(q1, src[ 1*step] - lims, src[ 1*step] + lims);
381
 
            }
382
 
            src[-2*step] = p1;
383
 
            src[-1*step] = p0;
384
 
            src[ 0*step] = q0;
385
 
            src[ 1*step] = q1;
386
 
            if(!chroma){
387
 
                src[-3*step] = (25*src[-1*step] + 26*src[-2*step] + 51*src[-3*step] + 26*src[-4*step] + 64) >> 7;
388
 
                src[ 2*step] = (25*src[ 0*step] + 26*src[ 1*step] + 51*src[ 2*step] + 26*src[ 3*step] + 64) >> 7;
389
 
            }
390
 
        }
391
 
    }else if(filter_p1 && filter_q1){
392
 
        for(i = 0; i < 4; i++, src += stride)
393
 
            rv40_weak_loop_filter(src, step, 1, 1, alpha, beta, lims, lim_q1, lim_p1,
394
 
                                  diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]);
395
 
    }else{
396
 
        for(i = 0; i < 4; i++, src += stride)
397
 
            rv40_weak_loop_filter(src, step, filter_p1, filter_q1,
398
 
                                  alpha, beta, lims>>1, lim_q1>>1, lim_p1>>1,
399
 
                                  diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]);
400
 
    }
401
 
}
402
 
 
403
 
static void rv40_v_loop_filter(uint8_t *src, int stride, int dmode,
404
 
                               int lim_q1, int lim_p1,
405
 
                               int alpha, int beta, int beta2, int chroma, int edge){
406
 
    rv40_adaptive_loop_filter(src, 1, stride, dmode, lim_q1, lim_p1,
407
 
                              alpha, beta, beta2, chroma, edge);
408
 
}
409
 
static void rv40_h_loop_filter(uint8_t *src, int stride, int dmode,
410
 
                               int lim_q1, int lim_p1,
411
 
                               int alpha, int beta, int beta2, int chroma, int edge){
412
 
    rv40_adaptive_loop_filter(src, stride, 1, dmode, lim_q1, lim_p1,
413
 
                              alpha, beta, beta2, chroma, edge);
414
 
}
415
 
 
416
274
enum RV40BlockPos{
417
275
    POS_CUR,
418
276
    POS_TOP,
436
294
static const int neighbour_offs_x[4] = { 0,  0, -1, 0 };
437
295
static const int neighbour_offs_y[4] = { 0, -1,  0, 1 };
438
296
 
 
297
static void rv40_adaptive_loop_filter(RV34DSPContext *rdsp,
 
298
                                      uint8_t *src, int stride, int dmode,
 
299
                                      int lim_q1, int lim_p1,
 
300
                                      int alpha, int beta, int beta2,
 
301
                                      int chroma, int edge, int dir)
 
302
{
 
303
    int filter_p1, filter_q1;
 
304
    int strong;
 
305
    int lims;
 
306
 
 
307
    strong = rdsp->rv40_loop_filter_strength[dir](src, stride, beta, beta2,
 
308
                                                  edge, &filter_p1, &filter_q1);
 
309
 
 
310
    lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;
 
311
 
 
312
    if (strong) {
 
313
        rdsp->rv40_strong_loop_filter[dir](src, stride, alpha,
 
314
                                           lims, dmode, chroma);
 
315
    } else if (filter_p1 & filter_q1) {
 
316
        rdsp->rv40_weak_loop_filter[dir](src, stride, 1, 1, alpha, beta,
 
317
                                         lims, lim_q1, lim_p1);
 
318
    } else if (filter_p1 | filter_q1) {
 
319
        rdsp->rv40_weak_loop_filter[dir](src, stride, filter_p1, filter_q1,
 
320
                                         alpha, beta, lims >> 1, lim_q1 >> 1,
 
321
                                         lim_p1 >> 1);
 
322
    }
 
323
}
 
324
 
439
325
/**
440
326
 * RV40 loop filtering function
441
327
 */
475
361
 
476
362
    mb_pos = row * s->mb_stride;
477
363
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
478
 
        int mbtype = s->current_picture_ptr->mb_type[mb_pos];
 
364
        int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];
479
365
        if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
480
366
            r->cbp_luma  [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF;
481
367
        if(IS_INTRA(mbtype))
489
375
        int avail[4];
490
376
        int y_to_deblock, c_to_deblock[2];
491
377
 
492
 
        q = s->current_picture_ptr->qscale_table[mb_pos];
 
378
        q = s->current_picture_ptr->f.qscale_table[mb_pos];
493
379
        alpha = rv40_alpha_tab[q];
494
380
        beta  = rv40_beta_tab [q];
495
381
        betaY = betaC = beta * 3;
504
390
            if(avail[i]){
505
391
                int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride;
506
392
                mvmasks[i] = r->deblock_coefs[pos];
507
 
                mbtype [i] = s->current_picture_ptr->mb_type[pos];
 
393
                mbtype [i] = s->current_picture_ptr->f.mb_type[pos];
508
394
                cbp    [i] = r->cbp_luma[pos];
509
395
                uvcbp[i][0] = r->cbp_chroma[pos] & 0xF;
510
396
                uvcbp[i][1] = r->cbp_chroma[pos] >> 4;
563
449
        }
564
450
 
565
451
        for(j = 0; j < 16; j += 4){
566
 
            Y = s->current_picture_ptr->data[0] + mb_x*16 + (row*16 + j) * s->linesize;
 
452
            Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
567
453
            for(i = 0; i < 4; i++, Y += 4){
568
454
                int ij = i + j;
569
455
                int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0;
572
458
                // if bottom block is coded then we can filter its top edge
573
459
                // (or bottom edge of this block, which is the same)
574
460
                if(y_h_deblock & (MASK_BOTTOM << ij)){
575
 
                    rv40_h_loop_filter(Y+4*s->linesize, s->linesize, dither,
576
 
                                       y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0,
577
 
                                       clip_cur,
578
 
                                       alpha, beta, betaY, 0, 0);
 
461
                    rv40_adaptive_loop_filter(&r->rdsp, Y+4*s->linesize,
 
462
                                              s->linesize, dither,
 
463
                                              y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0,
 
464
                                              clip_cur, alpha, beta, betaY,
 
465
                                              0, 0, 0);
579
466
                }
580
467
                // filter left block edge in ordinary mode (with low filtering strength)
581
468
                if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){
583
470
                        clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;
584
471
                    else
585
472
                        clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;
586
 
                    rv40_v_loop_filter(Y, s->linesize, dither,
587
 
                                       clip_cur,
588
 
                                       clip_left,
589
 
                                       alpha, beta, betaY, 0, 0);
 
473
                    rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
 
474
                                              clip_cur,
 
475
                                              clip_left,
 
476
                                              alpha, beta, betaY, 0, 0, 1);
590
477
                }
591
478
                // filter top edge of the current macroblock when filtering strength is high
592
479
                if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){
593
 
                    rv40_h_loop_filter(Y, s->linesize, dither,
 
480
                    rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
594
481
                                       clip_cur,
595
482
                                       mvmasks[POS_TOP] & (MASK_TOP << i) ? clip[POS_TOP] : 0,
596
 
                                       alpha, beta, betaY, 0, 1);
 
483
                                       alpha, beta, betaY, 0, 1, 0);
597
484
                }
598
485
                // filter left block edge in edge mode (with high filtering strength)
599
486
                if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){
600
487
                    clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;
601
 
                    rv40_v_loop_filter(Y, s->linesize, dither,
 
488
                    rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
602
489
                                       clip_cur,
603
490
                                       clip_left,
604
 
                                       alpha, beta, betaY, 0, 1);
 
491
                                       alpha, beta, betaY, 0, 1, 1);
605
492
                }
606
493
            }
607
494
        }
608
495
        for(k = 0; k < 2; k++){
609
496
            for(j = 0; j < 2; j++){
610
 
                C = s->current_picture_ptr->data[k+1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize;
 
497
                C = s->current_picture_ptr->f.data[k + 1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize;
611
498
                for(i = 0; i < 2; i++, C += 4){
612
499
                    int ij = i + j*2;
613
500
                    int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0;
614
501
                    if(c_h_deblock[k] & (MASK_CUR << (ij+2))){
615
502
                        int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0;
616
 
                        rv40_h_loop_filter(C+4*s->uvlinesize, s->uvlinesize, i*8,
 
503
                        rv40_adaptive_loop_filter(&r->rdsp, C+4*s->uvlinesize, s->uvlinesize, i*8,
617
504
                                           clip_bot,
618
505
                                           clip_cur,
619
 
                                           alpha, beta, betaC, 1, 0);
 
506
                                           alpha, beta, betaC, 1, 0, 0);
620
507
                    }
621
508
                    if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){
622
509
                        if(!i)
623
510
                            clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;
624
511
                        else
625
512
                            clip_left = c_to_deblock[k]    & (MASK_CUR << (ij-1))  ? clip[POS_CUR]  : 0;
626
 
                        rv40_v_loop_filter(C, s->uvlinesize, j*8,
 
513
                        rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,
627
514
                                           clip_cur,
628
515
                                           clip_left,
629
 
                                           alpha, beta, betaC, 1, 0);
 
516
                                           alpha, beta, betaC, 1, 0, 1);
630
517
                    }
631
518
                    if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){
632
519
                        int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0;
633
 
                        rv40_h_loop_filter(C, s->uvlinesize, i*8,
 
520
                        rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, i*8,
634
521
                                           clip_cur,
635
522
                                           clip_top,
636
 
                                           alpha, beta, betaC, 1, 1);
 
523
                                           alpha, beta, betaC, 1, 1, 0);
637
524
                    }
638
525
                    if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){
639
526
                        clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;
640
 
                        rv40_v_loop_filter(C, s->uvlinesize, j*8,
 
527
                        rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,
641
528
                                           clip_cur,
642
529
                                           clip_left,
643
 
                                           alpha, beta, betaC, 1, 1);
 
530
                                           alpha, beta, betaC, 1, 1, 1);
644
531
                    }
645
532
                }
646
533
            }
669
556
}
670
557
 
671
558
AVCodec ff_rv40_decoder = {
672
 
    "rv40",
673
 
    AVMEDIA_TYPE_VIDEO,
674
 
    CODEC_ID_RV40,
675
 
    sizeof(RV34DecContext),
676
 
    rv40_decode_init,
677
 
    NULL,
678
 
    ff_rv34_decode_end,
679
 
    ff_rv34_decode_frame,
680
 
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
681
 
    .flush = ff_mpeg_flush,
682
 
    .long_name = NULL_IF_CONFIG_SMALL("RealVideo 4.0"),
683
 
    .pix_fmts= ff_pixfmt_list_420,
 
559
    .name           = "rv40",
 
560
    .type           = AVMEDIA_TYPE_VIDEO,
 
561
    .id             = CODEC_ID_RV40,
 
562
    .priv_data_size = sizeof(RV34DecContext),
 
563
    .init           = rv40_decode_init,
 
564
    .close          = ff_rv34_decode_end,
 
565
    .decode         = ff_rv34_decode_frame,
 
566
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS,
 
567
    .flush          = ff_mpeg_flush,
 
568
    .long_name      = NULL_IF_CONFIG_SMALL("RealVideo 4.0"),
 
569
    .pix_fmts       = ff_pixfmt_list_420,
 
570
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_init_thread_copy),
 
571
    .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_update_thread_context),
684
572
};