~siretart/libav/merge.raring.libav-0.8.6

« back to all changes in this revision

Viewing changes to libavcodec/error_resilience.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:30:00 UTC
  • mfrom: (1.4.1)
  • mto: (1.3.11 sid) (26.1.1 quantal-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20120112223000-s1reiy1e28hnix42
Tags: upstream-0.8~beta2
ImportĀ upstreamĀ versionĀ 0.8~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#undef mb_intra
42
42
 
43
43
static void decode_mb(MpegEncContext *s, int ref){
44
 
    s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize  ) + s->mb_x * 16;
45
 
    s->dest[1] = s->current_picture.data[1] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);
46
 
    s->dest[2] = s->current_picture.data[2] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);
 
44
    s->dest[0] = s->current_picture.f.data[0] + (s->mb_y *  16 * s->linesize)                         + s->mb_x *  16;
 
45
    s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
 
46
    s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
47
47
 
48
48
    if(CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264){
49
49
        H264Context *h= (void*)s;
50
50
        h->mb_xy= s->mb_x + s->mb_y*s->mb_stride;
51
51
        memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
52
52
        assert(ref>=0);
53
 
        if(ref >= h->ref_count[0]) //FIXME it is posible albeit uncommon that slice references differ between slices, we take the easy approuch and ignore it for now. If this turns out to have any relevance in practice then correct remapping should be added
 
53
        /* FIXME: It is posible albeit uncommon that slice references
 
54
         * differ between slices. We take the easy approach and ignore
 
55
         * it for now. If this turns out to have any relevance in
 
56
         * practice then correct remapping should be added. */
 
57
        if (ref >= h->ref_count[0])
54
58
            ref=0;
55
 
        fill_rectangle(&s->current_picture.ref_index[0][4*h->mb_xy], 2, 2, 2, ref, 1);
 
59
        fill_rectangle(&s->current_picture.f.ref_index[0][4*h->mb_xy], 2, 2, 2, ref, 1);
56
60
        fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
57
61
        fill_rectangle(h->mv_cache[0][ scan8[0] ], 4, 4, 8, pack16to32(s->mv[0][0][0],s->mv[0][0][1]), 4);
58
62
        assert(!FRAME_MBAFF);
80
84
}
81
85
 
82
86
/**
83
 
 * replaces the current MB with a flat dc only version.
 
87
 * Replace the current MB with a flat dc-only version.
84
88
 */
85
89
static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
86
90
{
105
109
    for(y=0; y<8; y++){
106
110
        int x;
107
111
        for(x=0; x<8; x++){
108
 
            dest_cb[x + y*(s->uvlinesize)]= dcu/8;
109
 
            dest_cr[x + y*(s->uvlinesize)]= dcv/8;
 
112
            dest_cb[x + y * s->uvlinesize] = dcu / 8;
 
113
            dest_cr[x + y * s->uvlinesize] = dcv / 8;
110
114
        }
111
115
    }
112
116
}
166
170
 
167
171
            error= s->error_status_table[mb_index];
168
172
 
169
 
            if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
170
 
            if(!(error&DC_ERROR)) continue;           //dc-ok
 
173
            if(IS_INTER(s->current_picture.f.mb_type[mb_index])) continue; //inter
 
174
            if(!(error&ER_DC_ERROR)) continue;           //dc-ok
171
175
 
172
176
            /* right block */
173
177
            for(j=b_x+1; j<w; j++){
174
178
                int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
175
179
                int error_j= s->error_status_table[mb_index_j];
176
 
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
177
 
                if(intra_j==0 || !(error_j&DC_ERROR)){
 
180
                int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
 
181
                if(intra_j==0 || !(error_j&ER_DC_ERROR)){
178
182
                    color[0]= dc[j + b_y*stride];
179
183
                    distance[0]= j-b_x;
180
184
                    break;
185
189
            for(j=b_x-1; j>=0; j--){
186
190
                int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
187
191
                int error_j= s->error_status_table[mb_index_j];
188
 
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
189
 
                if(intra_j==0 || !(error_j&DC_ERROR)){
 
192
                int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
 
193
                if(intra_j==0 || !(error_j&ER_DC_ERROR)){
190
194
                    color[1]= dc[j + b_y*stride];
191
195
                    distance[1]= b_x-j;
192
196
                    break;
197
201
            for(j=b_y+1; j<h; j++){
198
202
                int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
199
203
                int error_j= s->error_status_table[mb_index_j];
200
 
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
201
 
                if(intra_j==0 || !(error_j&DC_ERROR)){
 
204
                int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
 
205
                if(intra_j==0 || !(error_j&ER_DC_ERROR)){
202
206
                    color[2]= dc[b_x + j*stride];
203
207
                    distance[2]= j-b_y;
204
208
                    break;
209
213
            for(j=b_y-1; j>=0; j--){
210
214
                int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
211
215
                int error_j= s->error_status_table[mb_index_j];
212
 
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
213
 
                if(intra_j==0 || !(error_j&DC_ERROR)){
 
216
                int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
 
217
                if(intra_j==0 || !(error_j&ER_DC_ERROR)){
214
218
                    color[3]= dc[b_x + j*stride];
215
219
                    distance[3]= b_y-j;
216
220
                    break;
248
252
            int y;
249
253
            int left_status = s->error_status_table[( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride];
250
254
            int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
251
 
            int left_intra=   IS_INTRA(s->current_picture.mb_type      [( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
252
 
            int right_intra=  IS_INTRA(s->current_picture.mb_type      [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
253
 
            int left_damage =  left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
254
 
            int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
 
255
            int left_intra  = IS_INTRA(s->current_picture.f.mb_type[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
 
256
            int right_intra = IS_INTRA(s->current_picture.f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
 
257
            int left_damage =  left_status&ER_MB_ERROR;
 
258
            int right_damage= right_status&ER_MB_ERROR;
255
259
            int offset= b_x*8 + b_y*stride*8;
256
 
            int16_t *left_mv=  s->current_picture.motion_val[0][mvy_stride*b_y + mvx_stride* b_x   ];
257
 
            int16_t *right_mv= s->current_picture.motion_val[0][mvy_stride*b_y + mvx_stride*(b_x+1)];
 
260
            int16_t *left_mv=  s->current_picture.f.motion_val[0][mvy_stride*b_y + mvx_stride* b_x   ];
 
261
            int16_t *right_mv= s->current_picture.f.motion_val[0][mvy_stride*b_y + mvx_stride*(b_x+1)];
258
262
 
259
263
            if(!(left_damage||right_damage)) continue; // both undamaged
260
264
 
311
315
            int x;
312
316
            int top_status   = s->error_status_table[(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride];
313
317
            int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
314
 
            int top_intra=     IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride]);
315
 
            int bottom_intra=  IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
316
 
            int top_damage =      top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
317
 
            int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
 
318
            int top_intra    = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ( b_y      >> is_luma) * s->mb_stride]);
 
319
            int bottom_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
 
320
            int top_damage =      top_status&ER_MB_ERROR;
 
321
            int bottom_damage= bottom_status&ER_MB_ERROR;
318
322
            int offset= b_x*8 + b_y*stride*8;
319
 
            int16_t *top_mv=    s->current_picture.motion_val[0][mvy_stride* b_y    + mvx_stride*b_x];
320
 
            int16_t *bottom_mv= s->current_picture.motion_val[0][mvy_stride*(b_y+1) + mvx_stride*b_x];
 
323
            int16_t *top_mv    = s->current_picture.f.motion_val[0][mvy_stride *  b_y      + mvx_stride * b_x];
 
324
            int16_t *bottom_mv = s->current_picture.f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
321
325
 
322
326
            if(!(top_damage||bottom_damage)) continue; // both undamaged
323
327
 
376
380
        int f=0;
377
381
        int error= s->error_status_table[mb_xy];
378
382
 
379
 
        if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
380
 
        if(!(error&MV_ERROR)) f=MV_FROZEN;           //inter with undamaged MV
 
383
        if(IS_INTRA(s->current_picture.f.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
 
384
        if(!(error&ER_MV_ERROR)) f=MV_FROZEN;           //inter with undamaged MV
381
385
 
382
386
        fixed[mb_xy]= f;
383
387
        if(f==MV_FROZEN)
389
393
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
390
394
                const int mb_xy= mb_x + mb_y*s->mb_stride;
391
395
 
392
 
                if(IS_INTRA(s->current_picture.mb_type[mb_xy]))  continue;
393
 
                if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
 
396
                if(IS_INTRA(s->current_picture.f.mb_type[mb_xy]))  continue;
 
397
                if(!(s->error_status_table[mb_xy]&ER_MV_ERROR)) continue;
394
398
 
395
 
                s->mv_dir = s->last_picture.data[0] ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
 
399
                s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
396
400
                s->mb_intra=0;
397
401
                s->mv_type = MV_TYPE_16X16;
398
402
                s->mb_skipped=0;
434
438
                    if((mb_x^mb_y^pass)&1) continue;
435
439
 
436
440
                    if(fixed[mb_xy]==MV_FROZEN) continue;
437
 
                    assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
438
 
                    assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
 
441
                    assert(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
 
442
                    assert(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
439
443
 
440
444
                    j=0;
441
445
                    if(mb_x>0           && fixed[mb_xy-1        ]==MV_FROZEN) j=1;
454
458
                    none_left=0;
455
459
 
456
460
                    if(mb_x>0 && fixed[mb_xy-1]){
457
 
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_step][0];
458
 
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_step][1];
459
 
                        ref         [pred_count]   = s->current_picture.ref_index[0][4*(mb_xy-1)];
 
461
                        mv_predictor[pred_count][0]= s->current_picture.f.motion_val[0][mot_index - mot_step][0];
 
462
                        mv_predictor[pred_count][1]= s->current_picture.f.motion_val[0][mot_index - mot_step][1];
 
463
                        ref         [pred_count]   = s->current_picture.f.ref_index[0][4*(mb_xy-1)];
460
464
                        pred_count++;
461
465
                    }
462
466
                    if(mb_x+1<mb_width && fixed[mb_xy+1]){
463
 
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_step][0];
464
 
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_step][1];
465
 
                        ref         [pred_count]   = s->current_picture.ref_index[0][4*(mb_xy+1)];
 
467
                        mv_predictor[pred_count][0]= s->current_picture.f.motion_val[0][mot_index + mot_step][0];
 
468
                        mv_predictor[pred_count][1]= s->current_picture.f.motion_val[0][mot_index + mot_step][1];
 
469
                        ref         [pred_count]   = s->current_picture.f.ref_index[0][4*(mb_xy+1)];
466
470
                        pred_count++;
467
471
                    }
468
472
                    if(mb_y>0 && fixed[mb_xy-mb_stride]){
469
 
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*mot_step][0];
470
 
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*mot_step][1];
471
 
                        ref         [pred_count]   = s->current_picture.ref_index[0][4*(mb_xy-s->mb_stride)];
 
473
                        mv_predictor[pred_count][0]= s->current_picture.f.motion_val[0][mot_index - mot_stride*mot_step][0];
 
474
                        mv_predictor[pred_count][1]= s->current_picture.f.motion_val[0][mot_index - mot_stride*mot_step][1];
 
475
                        ref         [pred_count]   = s->current_picture.f.ref_index[0][4*(mb_xy-s->mb_stride)];
472
476
                        pred_count++;
473
477
                    }
474
478
                    if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
475
 
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*mot_step][0];
476
 
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*mot_step][1];
477
 
                        ref         [pred_count]   = s->current_picture.ref_index[0][4*(mb_xy+s->mb_stride)];
 
479
                        mv_predictor[pred_count][0]= s->current_picture.f.motion_val[0][mot_index + mot_stride*mot_step][0];
 
480
                        mv_predictor[pred_count][1]= s->current_picture.f.motion_val[0][mot_index + mot_stride*mot_step][1];
 
481
                        ref         [pred_count]   = s->current_picture.f.ref_index[0][4*(mb_xy+s->mb_stride)];
478
482
                        pred_count++;
479
483
                    }
480
484
                    if(pred_count==0) continue;
534
538
                            ff_thread_await_progress((AVFrame *) s->last_picture_ptr,
535
539
                                                     mb_y, 0);
536
540
                        }
537
 
                        if (!s->last_picture.motion_val[0] ||
538
 
                            !s->last_picture.ref_index[0])
 
541
                        if (!s->last_picture.f.motion_val[0] ||
 
542
                            !s->last_picture.f.ref_index[0])
539
543
                            goto skip_last_mv;
540
 
                        prev_x = s->last_picture.motion_val[0][mot_index][0];
541
 
                        prev_y = s->last_picture.motion_val[0][mot_index][1];
542
 
                        prev_ref = s->last_picture.ref_index[0][4*mb_xy];
 
544
                        prev_x = s->last_picture.f.motion_val[0][mot_index][0];
 
545
                        prev_y = s->last_picture.f.motion_val[0][mot_index][1];
 
546
                        prev_ref = s->last_picture.f.ref_index[0][4*mb_xy];
543
547
                    } else {
544
 
                        prev_x = s->current_picture.motion_val[0][mot_index][0];
545
 
                        prev_y = s->current_picture.motion_val[0][mot_index][1];
546
 
                        prev_ref = s->current_picture.ref_index[0][4*mb_xy];
 
548
                        prev_x = s->current_picture.f.motion_val[0][mot_index][0];
 
549
                        prev_y = s->current_picture.f.motion_val[0][mot_index][1];
 
550
                        prev_ref = s->current_picture.f.ref_index[0][4*mb_xy];
547
551
                    }
548
552
 
549
553
                    /* last MV */
565
569
 
566
570
                    for(j=0; j<pred_count; j++){
567
571
                        int score=0;
568
 
                        uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
 
572
                        uint8_t *src = s->current_picture.f.data[0] + mb_x*16 + mb_y*16*s->linesize;
569
573
 
570
 
                        s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
571
 
                        s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
 
574
                        s->current_picture.f.motion_val[0][mot_index][0] = s->mv[0][0][0] = mv_predictor[j][0];
 
575
                        s->current_picture.f.motion_val[0][mot_index][1] = s->mv[0][0][1] = mv_predictor[j][1];
572
576
 
573
577
                        if(ref[j]<0) //predictor intra or otherwise not available
574
578
                            continue;
607
611
 
608
612
                    for(i=0; i<mot_step; i++)
609
613
                        for(j=0; j<mot_step; j++){
610
 
                            s->current_picture.motion_val[0][mot_index+i+j*mot_stride][0]= s->mv[0][0][0];
611
 
                            s->current_picture.motion_val[0][mot_index+i+j*mot_stride][1]= s->mv[0][0][1];
 
614
                            s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
 
615
                            s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
612
616
                        }
613
617
 
614
618
                    decode_mb(s, ref[best_pred]);
640
644
static int is_intra_more_likely(MpegEncContext *s){
641
645
    int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
642
646
 
643
 
    if(!s->last_picture_ptr || !s->last_picture_ptr->data[0]) return 1; //no previous frame available -> use spatial prediction
 
647
    if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) return 1; //no previous frame available -> use spatial prediction
644
648
 
645
649
    undamaged_count=0;
646
650
    for(i=0; i<s->mb_num; i++){
647
651
        const int mb_xy= s->mb_index2xy[i];
648
652
        const int error= s->error_status_table[mb_xy];
649
 
        if(!((error&DC_ERROR) && (error&MV_ERROR)))
 
653
        if(!((error&ER_DC_ERROR) && (error&ER_MV_ERROR)))
650
654
            undamaged_count++;
651
655
    }
652
656
 
653
657
    if(s->codec_id == CODEC_ID_H264){
654
658
        H264Context *h= (void*)s;
655
 
        if(h->ref_count[0] <= 0 || !h->ref_list[0][0].data[0])
 
659
        if (h->list_count <= 0 || h->ref_count[0] <= 0 || !h->ref_list[0][0].f.data[0])
656
660
            return 1;
657
661
    }
658
662
 
662
666
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration && s->pict_type == AV_PICTURE_TYPE_I)
663
667
        return 1;
664
668
 
665
 
    skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
 
669
    skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
666
670
    is_intra_likely=0;
667
671
 
668
672
    j=0;
672
676
            const int mb_xy= mb_x + mb_y*s->mb_stride;
673
677
 
674
678
            error= s->error_status_table[mb_xy];
675
 
            if((error&DC_ERROR) && (error&MV_ERROR))
 
679
            if((error&ER_DC_ERROR) && (error&ER_MV_ERROR))
676
680
                continue; //skip damaged
677
681
 
678
682
            j++;
679
683
            if((j%skip_amount) != 0) continue; //skip a few to speed things up
680
684
 
681
685
            if(s->pict_type==AV_PICTURE_TYPE_I){
682
 
                uint8_t *mb_ptr     = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
683
 
                uint8_t *last_mb_ptr= s->last_picture.data   [0] + mb_x*16 + mb_y*16*s->linesize;
 
686
                uint8_t *mb_ptr     = s->current_picture.f.data[0] + mb_x*16 + mb_y*16*s->linesize;
 
687
                uint8_t *last_mb_ptr= s->last_picture.f.data   [0] + mb_x*16 + mb_y*16*s->linesize;
684
688
 
685
689
                if (s->avctx->codec_id == CODEC_ID_H264) {
686
690
                    // FIXME
691
695
                is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr                    , s->linesize, 16);
692
696
                is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
693
697
            }else{
694
 
                if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
 
698
                if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
695
699
                   is_intra_likely++;
696
700
                else
697
701
                   is_intra_likely--;
703
707
}
704
708
 
705
709
void ff_er_frame_start(MpegEncContext *s){
706
 
    if(!s->error_recognition) return;
 
710
    if(!s->err_recognition) return;
707
711
 
708
 
    memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_stride*s->mb_height*sizeof(uint8_t));
 
712
    memset(s->error_status_table, ER_MB_ERROR|VP_START|ER_MB_END, s->mb_stride*s->mb_height*sizeof(uint8_t));
709
713
    s->error_count= 3*s->mb_num;
710
714
    s->error_occurred = 0;
711
715
}
712
716
 
713
717
/**
714
 
 * adds a slice.
 
718
 * Add a slice.
715
719
 * @param endx x component of the last macroblock, can be -1 for the last of the previous line
716
 
 * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
 
720
 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is assumed that no earlier end or
717
721
 *               error of the same type occurred
718
722
 */
719
723
void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
731
735
        return;
732
736
    }
733
737
 
734
 
    if(!s->error_recognition) return;
 
738
    if(!s->err_recognition) return;
735
739
 
736
740
    mask &= ~VP_START;
737
 
    if(status & (AC_ERROR|AC_END)){
738
 
        mask &= ~(AC_ERROR|AC_END);
739
 
        s->error_count -= end_i - start_i + 1;
740
 
    }
741
 
    if(status & (DC_ERROR|DC_END)){
742
 
        mask &= ~(DC_ERROR|DC_END);
743
 
        s->error_count -= end_i - start_i + 1;
744
 
    }
745
 
    if(status & (MV_ERROR|MV_END)){
746
 
        mask &= ~(MV_ERROR|MV_END);
 
741
    if(status & (ER_AC_ERROR|ER_AC_END)){
 
742
        mask &= ~(ER_AC_ERROR|ER_AC_END);
 
743
        s->error_count -= end_i - start_i + 1;
 
744
    }
 
745
    if(status & (ER_DC_ERROR|ER_DC_END)){
 
746
        mask &= ~(ER_DC_ERROR|ER_DC_END);
 
747
        s->error_count -= end_i - start_i + 1;
 
748
    }
 
749
    if(status & (ER_MV_ERROR|ER_MV_END)){
 
750
        mask &= ~(ER_MV_ERROR|ER_MV_END);
747
751
        s->error_count -= end_i - start_i + 1;
748
752
    }
749
753
 
750
 
    if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) {
 
754
    if(status & ER_MB_ERROR) {
751
755
        s->error_occurred = 1;
752
756
        s->error_count= INT_MAX;
753
757
    }
774
778
        int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
775
779
 
776
780
        prev_status &= ~ VP_START;
777
 
        if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
 
781
        if(prev_status != (ER_MV_END|ER_DC_END|ER_AC_END)) s->error_count= INT_MAX;
778
782
    }
779
783
}
780
784
 
787
791
    int size = s->b8_stride * 2 * s->mb_height;
788
792
    Picture *pic= s->current_picture_ptr;
789
793
 
790
 
    if(!s->error_recognition || s->error_count==0 || s->avctx->lowres ||
 
794
    if(!s->err_recognition || s->error_count==0 || s->avctx->lowres ||
791
795
       s->avctx->hwaccel ||
792
796
       s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
793
 
       s->picture_structure != PICT_FRAME || // we dont support ER of field pictures yet, though it should not crash if enabled
 
797
       s->picture_structure != PICT_FRAME || // we do not support ER of field pictures yet, though it should not crash if enabled
794
798
       s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
795
799
 
796
 
    if(s->current_picture.motion_val[0] == NULL){
 
800
    if (s->current_picture.f.motion_val[0] == NULL) {
797
801
        av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
798
802
 
799
803
        for(i=0; i<2; i++){
800
 
            pic->ref_index[i]= av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
 
804
            pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
801
805
            pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
802
 
            pic->motion_val[i]= pic->motion_val_base[i]+4;
 
806
            pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
803
807
        }
804
 
        pic->motion_subsample_log2= 3;
 
808
        pic->f.motion_subsample_log2 = 3;
805
809
        s->current_picture= *s->current_picture_ptr;
806
810
    }
807
811
 
845
849
            const int mb_xy= s->mb_index2xy[i];
846
850
            int error= s->error_status_table[mb_xy];
847
851
 
848
 
            if(error&AC_END)
 
852
            if(error&ER_AC_END)
849
853
                end_ok=0;
850
 
            if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
 
854
            if((error&ER_MV_END) || (error&ER_DC_END) || (error&ER_AC_ERROR))
851
855
                end_ok=1;
852
856
 
853
857
            if(!end_ok)
854
 
                s->error_status_table[mb_xy]|= AC_ERROR;
 
858
                s->error_status_table[mb_xy]|= ER_AC_ERROR;
855
859
 
856
860
            if(error&VP_START)
857
861
                end_ok=0;
859
863
    }
860
864
 
861
865
    /* handle missing slices */
862
 
    if(s->error_recognition>=4){
 
866
    if(s->err_recognition&AV_EF_EXPLODE){
863
867
        int end_ok=1;
864
868
 
865
869
        for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
870
874
            if(error1&VP_START)
871
875
                end_ok=1;
872
876
 
873
 
            if(   error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
874
 
               && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
875
 
               && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
 
877
            if(   error2==(VP_START|ER_MB_ERROR|ER_MB_END)
 
878
               && error1!=(VP_START|ER_MB_ERROR|ER_MB_END)
 
879
               && ((error1&ER_AC_END) || (error1&ER_DC_END) || (error1&ER_MV_END))){ //end & uninit
876
880
                end_ok=0;
877
881
            }
878
882
 
879
883
            if(!end_ok)
880
 
                s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
 
884
                s->error_status_table[mb_xy]|= ER_MB_ERROR;
881
885
        }
882
886
    }
883
887
 
913
917
        int old_error= s->error_status_table[mb_xy];
914
918
 
915
919
        if(old_error&VP_START)
916
 
            error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
 
920
            error= old_error& ER_MB_ERROR;
917
921
        else{
918
 
            error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
 
922
            error|= old_error& ER_MB_ERROR;
919
923
            s->error_status_table[mb_xy]|= error;
920
924
        }
921
925
    }
925
929
        for(i=0; i<s->mb_num; i++){
926
930
            const int mb_xy= s->mb_index2xy[i];
927
931
            error= s->error_status_table[mb_xy];
928
 
            if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
929
 
                error|= AC_ERROR|DC_ERROR|MV_ERROR;
 
932
            if(error&ER_MB_ERROR)
 
933
                error|= ER_MB_ERROR;
930
934
            s->error_status_table[mb_xy]= error;
931
935
        }
932
936
    }
935
939
    for(i=0; i<s->mb_num; i++){
936
940
        const int mb_xy= s->mb_index2xy[i];
937
941
        error= s->error_status_table[mb_xy];
938
 
        if(error&DC_ERROR) dc_error ++;
939
 
        if(error&AC_ERROR) ac_error ++;
940
 
        if(error&MV_ERROR) mv_error ++;
 
942
        if(error&ER_DC_ERROR) dc_error ++;
 
943
        if(error&ER_AC_ERROR) ac_error ++;
 
944
        if(error&ER_MV_ERROR) mv_error ++;
941
945
    }
942
946
    av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
943
947
 
947
951
    for(i=0; i<s->mb_num; i++){
948
952
        const int mb_xy= s->mb_index2xy[i];
949
953
        error= s->error_status_table[mb_xy];
950
 
        if(!((error&DC_ERROR) && (error&MV_ERROR)))
 
954
        if(!((error&ER_DC_ERROR) && (error&ER_MV_ERROR)))
951
955
            continue;
952
956
 
953
957
        if(is_intra_likely)
954
 
            s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
 
958
            s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
955
959
        else
956
 
            s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
 
960
            s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
957
961
    }
958
962
 
959
963
    // change inter to intra blocks if no reference frames are available
960
 
    if (!s->last_picture.data[0] && !s->next_picture.data[0])
 
964
    if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
961
965
        for(i=0; i<s->mb_num; i++){
962
966
            const int mb_xy= s->mb_index2xy[i];
963
 
            if(!IS_INTRA(s->current_picture.mb_type[mb_xy]))
964
 
                s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
 
967
            if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
 
968
                s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
965
969
        }
966
970
 
967
971
    /* handle inter blocks with damaged AC */
968
972
    for(mb_y=0; mb_y<s->mb_height; mb_y++){
969
973
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
970
974
            const int mb_xy= mb_x + mb_y * s->mb_stride;
971
 
            const int mb_type= s->current_picture.mb_type[mb_xy];
972
 
            int dir = !s->last_picture.data[0];
 
975
            const int mb_type= s->current_picture.f.mb_type[mb_xy];
 
976
            int dir = !s->last_picture.f.data[0];
973
977
            error= s->error_status_table[mb_xy];
974
978
 
975
979
            if(IS_INTRA(mb_type)) continue; //intra
976
 
            if(error&MV_ERROR) continue;              //inter with damaged MV
977
 
            if(!(error&AC_ERROR)) continue;           //undamaged inter
 
980
            if(error&ER_MV_ERROR) continue;              //inter with damaged MV
 
981
            if(!(error&ER_AC_ERROR)) continue;           //undamaged inter
978
982
 
979
983
            s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
980
984
            s->mb_intra=0;
984
988
                int j;
985
989
                s->mv_type = MV_TYPE_8X8;
986
990
                for(j=0; j<4; j++){
987
 
                    s->mv[0][j][0] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
988
 
                    s->mv[0][j][1] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
 
991
                    s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
 
992
                    s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
989
993
                }
990
994
            }else{
991
995
                s->mv_type = MV_TYPE_16X16;
992
 
                s->mv[0][0][0] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][0];
993
 
                s->mv[0][0][1] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][1];
 
996
                s->mv[0][0][0] = s->current_picture.f.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][0];
 
997
                s->mv[0][0][1] = s->current_picture.f.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][1];
994
998
            }
995
999
 
996
1000
            s->dsp.clear_blocks(s->block[0]);
1007
1011
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
1008
1012
                int xy= mb_x*2 + mb_y*2*s->b8_stride;
1009
1013
                const int mb_xy= mb_x + mb_y * s->mb_stride;
1010
 
                const int mb_type= s->current_picture.mb_type[mb_xy];
 
1014
                const int mb_type= s->current_picture.f.mb_type[mb_xy];
1011
1015
                error= s->error_status_table[mb_xy];
1012
1016
 
1013
1017
                if(IS_INTRA(mb_type)) continue;
1014
 
                if(!(error&MV_ERROR)) continue;           //inter with undamaged MV
1015
 
                if(!(error&AC_ERROR)) continue;           //undamaged inter
 
1018
                if(!(error&ER_MV_ERROR)) continue;           //inter with undamaged MV
 
1019
                if(!(error&ER_AC_ERROR)) continue;           //undamaged inter
1016
1020
 
1017
1021
                s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
1018
 
                if(!s->last_picture.data[0]) s->mv_dir &= ~MV_DIR_FORWARD;
1019
 
                if(!s->next_picture.data[0]) s->mv_dir &= ~MV_DIR_BACKWARD;
 
1022
                if(!s->last_picture.f.data[0]) s->mv_dir &= ~MV_DIR_FORWARD;
 
1023
                if(!s->next_picture.f.data[0]) s->mv_dir &= ~MV_DIR_BACKWARD;
1020
1024
                s->mb_intra=0;
1021
1025
                s->mv_type = MV_TYPE_16X16;
1022
1026
                s->mb_skipped=0;
1031
1035
                        ff_thread_await_progress((AVFrame *) s->next_picture_ptr,
1032
1036
                                                 mb_y, 0);
1033
1037
                    }
1034
 
                    s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
1035
 
                    s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
1036
 
                    s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
1037
 
                    s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
 
1038
                    s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] *            time_pb  / time_pp;
 
1039
                    s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] *            time_pb  / time_pp;
 
1040
                    s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
 
1041
                    s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1038
1042
                }else{
1039
1043
                    s->mv[0][0][0]= 0;
1040
1044
                    s->mv[0][0][1]= 0;
1061
1065
            int16_t *dc_ptr;
1062
1066
            uint8_t *dest_y, *dest_cb, *dest_cr;
1063
1067
            const int mb_xy= mb_x + mb_y * s->mb_stride;
1064
 
            const int mb_type= s->current_picture.mb_type[mb_xy];
 
1068
            const int mb_type = s->current_picture.f.mb_type[mb_xy];
1065
1069
 
1066
1070
            error= s->error_status_table[mb_xy];
1067
1071
 
1068
1072
            if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
1069
 
//            if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
 
1073
//            if(error&ER_MV_ERROR) continue; //inter data damaged FIXME is this good?
1070
1074
 
1071
 
            dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
1072
 
            dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
1073
 
            dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
 
1075
            dest_y  = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
 
1076
            dest_cb = s->current_picture.f.data[1] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
 
1077
            dest_cr = s->current_picture.f.data[2] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
1074
1078
 
1075
1079
            dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
1076
1080
            for(n=0; n<4; n++){
1088
1092
            for(y=0; y<8; y++){
1089
1093
                int x;
1090
1094
                for(x=0; x<8; x++){
1091
 
                    dcu+=dest_cb[x + y*(s->uvlinesize)];
1092
 
                    dcv+=dest_cr[x + y*(s->uvlinesize)];
 
1095
                    dcu += dest_cb[x + y * s->uvlinesize];
 
1096
                    dcv += dest_cr[x + y * s->uvlinesize];
1093
1097
                }
1094
1098
            }
1095
1099
            s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
1110
1114
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
1111
1115
            uint8_t *dest_y, *dest_cb, *dest_cr;
1112
1116
            const int mb_xy= mb_x + mb_y * s->mb_stride;
1113
 
            const int mb_type= s->current_picture.mb_type[mb_xy];
 
1117
            const int mb_type = s->current_picture.f.mb_type[mb_xy];
1114
1118
 
1115
1119
            error= s->error_status_table[mb_xy];
1116
1120
 
1117
1121
            if(IS_INTER(mb_type)) continue;
1118
 
            if(!(error&AC_ERROR)) continue;              //undamaged
 
1122
            if(!(error&ER_AC_ERROR)) continue;              //undamaged
1119
1123
 
1120
 
            dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
1121
 
            dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
1122
 
            dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
 
1124
            dest_y  = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
 
1125
            dest_cb = s->current_picture.f.data[1] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
 
1126
            dest_cr = s->current_picture.f.data[2] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
1123
1127
 
1124
1128
            put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1125
1129
        }
1127
1131
 
1128
1132
    if(s->avctx->error_concealment&FF_EC_DEBLOCK){
1129
1133
        /* filter horizontal block boundaries */
1130
 
        h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
1131
 
        h_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1132
 
        h_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
 
1134
        h_block_filter(s, s->current_picture.f.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
 
1135
        h_block_filter(s, s->current_picture.f.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
 
1136
        h_block_filter(s, s->current_picture.f.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1133
1137
 
1134
1138
        /* filter vertical block boundaries */
1135
 
        v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
1136
 
        v_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1137
 
        v_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
 
1139
        v_block_filter(s, s->current_picture.f.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
 
1140
        v_block_filter(s, s->current_picture.f.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
 
1141
        v_block_filter(s, s->current_picture.f.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1138
1142
    }
1139
1143
 
1140
1144
ec_clean:
1143
1147
        const int mb_xy= s->mb_index2xy[i];
1144
1148
        int error= s->error_status_table[mb_xy];
1145
1149
 
1146
 
        if(s->pict_type!=AV_PICTURE_TYPE_B && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
 
1150
        if(s->pict_type!=AV_PICTURE_TYPE_B && (error&(ER_DC_ERROR|ER_MV_ERROR|ER_AC_ERROR))){
1147
1151
            s->mbskip_table[mb_xy]=0;
1148
1152
        }
1149
1153
        s->mbintra_table[mb_xy]=1;