Revision d375c104 libavcodec/mpegvideo.c

View differences:

libavcodec/mpegvideo.c
38 38
#include "msmpeg4.h"
39 39
#include "faandct.h"
40 40
#include "xvmc_internal.h"
41
#include "thread.h"
41 42
#include <limits.h>
42 43

  
43 44
//#undef NDEBUG
......
205 206
 */
206 207
static void free_frame_buffer(MpegEncContext *s, Picture *pic)
207 208
{
208
    s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
209
    ff_thread_release_buffer(s->avctx, (AVFrame*)pic);
209 210
    av_freep(&pic->hwaccel_picture_private);
210 211
}
211 212

  
......
227 228
        }
228 229
    }
229 230

  
230
    r = s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
231
    r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic);
231 232

  
232 233
    if (r<0 || !pic->age || !pic->type || !pic->data[0]) {
233 234
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
......
458 459
//STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
459 460
}
460 461

  
462
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
463
{
464
    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
465

  
466
    if(dst == src || !s1->context_initialized) return 0;
467

  
468
    //FIXME can parameters change on I-frames? in that case dst may need a reinit
469
    if(!s->context_initialized){
470
        memcpy(s, s1, sizeof(MpegEncContext));
471

  
472
        s->avctx                 = dst;
473
        s->picture_range_start  += MAX_PICTURE_COUNT;
474
        s->picture_range_end    += MAX_PICTURE_COUNT;
475
        s->bitstream_buffer      = NULL;
476
        s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
477

  
478
        MPV_common_init(s);
479
    }
480

  
481
    s->avctx->coded_height  = s1->avctx->coded_height;
482
    s->avctx->coded_width   = s1->avctx->coded_width;
483
    s->avctx->width         = s1->avctx->width;
484
    s->avctx->height        = s1->avctx->height;
485

  
486
    s->coded_picture_number = s1->coded_picture_number;
487
    s->picture_number       = s1->picture_number;
488
    s->input_picture_number = s1->input_picture_number;
489

  
490
    memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
491
    memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture);
492

  
493
    s->last_picture_ptr     = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
494
    s->current_picture_ptr  = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
495
    s->next_picture_ptr     = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
496

  
497
    memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
498

  
499
    //Error/bug resilience
500
    s->next_p_frame_damaged = s1->next_p_frame_damaged;
501
    s->workaround_bugs      = s1->workaround_bugs;
502

  
503
    //MPEG4 timing info
504
    memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits);
505

  
506
    //B-frame info
507
    s->max_b_frames         = s1->max_b_frames;
508
    s->low_delay            = s1->low_delay;
509
    s->dropable             = s1->dropable;
510

  
511
    //DivX handling (doesn't work)
512
    s->divx_packed          = s1->divx_packed;
513

  
514
    if(s1->bitstream_buffer){
515
        if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
516
            av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size);
517
        s->bitstream_buffer_size  = s1->bitstream_buffer_size;
518
        memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size);
519
        memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
520
    }
521

  
522
    //MPEG2/interlacing info
523
    memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence);
524

  
525
    if(!s1->first_field){
526
        s->last_pict_type= s1->pict_type;
527
        if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->quality;
528

  
529
        if(s1->pict_type!=FF_B_TYPE){
530
            s->last_non_b_pict_type= s1->pict_type;
531
        }
532
    }
533

  
534
    return 0;
535
}
536

  
461 537
/**
462 538
 * sets the given MpegEncContext to common defaults (same for encoding and decoding).
463 539
 * the changed fields will not depend upon the prior state of the MpegEncContext.
......
478 554

  
479 555
    s->f_code = 1;
480 556
    s->b_code = 1;
557

  
558
    s->picture_range_start = 0;
559
    s->picture_range_end = MAX_PICTURE_COUNT;
481 560
}
482 561

  
483 562
/**
......
506 585
        return -1;
507 586
    }
508 587

  
509
    if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
588
    if(s->avctx->active_thread_type&FF_THREAD_SLICE &&
589
       (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){
510 590
        av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
511 591
        return -1;
512 592
    }
......
599 679
            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
600 680
        }
601 681
    }
602
    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, MAX_PICTURE_COUNT * sizeof(Picture), fail)
603
    for(i = 0; i < MAX_PICTURE_COUNT; i++) {
682
    s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
683
    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail)
684
    for(i = 0; i < s->picture_count; i++) {
604 685
        avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
605 686
    }
606 687

  
......
660 741
    }
661 742

  
662 743
    s->context_initialized = 1;
663

  
664 744
    s->thread_context[0]= s;
665
    threads = s->avctx->thread_count;
666 745

  
667
    for(i=1; i<threads; i++){
668
        s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
669
        memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
670
    }
746
    if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
747
        threads = s->avctx->thread_count;
671 748

  
672
    for(i=0; i<threads; i++){
673
        if(init_duplicate_context(s->thread_context[i], s) < 0)
674
           goto fail;
675
        s->thread_context[i]->start_mb_y= (s->mb_height*(i  ) + s->avctx->thread_count/2) / s->avctx->thread_count;
676
        s->thread_context[i]->end_mb_y  = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
749
        for(i=1; i<threads; i++){
750
            s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
751
            memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
752
        }
753

  
754
        for(i=0; i<threads; i++){
755
            if(init_duplicate_context(s->thread_context[i], s) < 0)
756
               goto fail;
757
            s->thread_context[i]->start_mb_y= (s->mb_height*(i  ) + s->avctx->thread_count/2) / s->avctx->thread_count;
758
            s->thread_context[i]->end_mb_y  = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
759
        }
760
    } else {
761
        if(init_duplicate_context(s, s) < 0) goto fail;
762
        s->start_mb_y = 0;
763
        s->end_mb_y   = s->mb_height;
677 764
    }
678 765

  
679 766
    return 0;
......
687 774
{
688 775
    int i, j, k;
689 776

  
690
    for(i=0; i<s->avctx->thread_count; i++){
691
        free_duplicate_context(s->thread_context[i]);
692
    }
693
    for(i=1; i<s->avctx->thread_count; i++){
694
        av_freep(&s->thread_context[i]);
695
    }
777
    if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
778
        for(i=0; i<s->avctx->thread_count; i++){
779
            free_duplicate_context(s->thread_context[i]);
780
        }
781
        for(i=1; i<s->avctx->thread_count; i++){
782
            av_freep(&s->thread_context[i]);
783
        }
784
    } else free_duplicate_context(s);
696 785

  
697 786
    av_freep(&s->parse_context.buffer);
698 787
    s->parse_context.buffer_size=0;
......
747 836
    av_freep(&s->reordered_input_picture);
748 837
    av_freep(&s->dct_offset);
749 838

  
750
    if(s->picture){
751
        for(i=0; i<MAX_PICTURE_COUNT; i++){
839
    if(s->picture && !s->avctx->is_copy){
840
        for(i=0; i<s->picture_count; i++){
752 841
            free_picture(s, &s->picture[i]);
753 842
        }
754 843
    }
......
762 851
    for(i=0; i<3; i++)
763 852
        av_freep(&s->visualization_buffer[i]);
764 853

  
765
    avcodec_default_free_buffers(s->avctx);
854
    if(!(s->avctx->active_thread_type&FF_THREAD_FRAME))
855
        avcodec_default_free_buffers(s->avctx);
766 856
}
767 857

  
768 858
void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
......
860 950
    int i;
861 951

  
862 952
    if(shared){
863
        for(i=0; i<MAX_PICTURE_COUNT; i++){
953
        for(i=s->picture_range_start; i<s->picture_range_end; i++){
864 954
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
865 955
        }
866 956
    }else{
867
        for(i=0; i<MAX_PICTURE_COUNT; i++){
957
        for(i=s->picture_range_start; i<s->picture_range_end; i++){
868 958
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
869 959
        }
870
        for(i=0; i<MAX_PICTURE_COUNT; i++){
960
        for(i=s->picture_range_start; i<s->picture_range_end; i++){
871 961
            if(s->picture[i].data[0]==NULL) return i;
872 962
        }
873 963
    }
......
924 1014
        /* release forgotten pictures */
925 1015
        /* if(mpeg124/h263) */
926 1016
        if(!s->encoding){
927
            for(i=0; i<MAX_PICTURE_COUNT; i++){
1017
            for(i=0; i<s->picture_count; i++){
928 1018
                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
929 1019
                    av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
930 1020
                    free_frame_buffer(s, &s->picture[i]);
......
936 1026

  
937 1027
    if(!s->encoding){
938 1028
        /* release non reference frames */
939
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1029
        for(i=0; i<s->picture_count; i++){
940 1030
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
941 1031
                free_frame_buffer(s, &s->picture[i]);
942 1032
            }
......
970 1060
                s->current_picture_ptr->top_field_first= (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
971 1061
        }
972 1062
        s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
1063
        s->current_picture_ptr->field_picture= s->picture_structure != PICT_FRAME;
973 1064
    }
974 1065

  
975 1066
    s->current_picture_ptr->pict_type= s->pict_type;
......
998 1089
            s->last_picture_ptr= &s->picture[i];
999 1090
            if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
1000 1091
                return -1;
1092
            ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
1093
            ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
1001 1094
        }
1002 1095
        if((s->next_picture_ptr==NULL || s->next_picture_ptr->data[0]==NULL) && s->pict_type==FF_B_TYPE){
1003 1096
            /* Allocate a dummy frame */
......
1005 1098
            s->next_picture_ptr= &s->picture[i];
1006 1099
            if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
1007 1100
                return -1;
1101
            ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
1102
            ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
1008 1103
        }
1009 1104
    }
1010 1105

  
......
1057 1152
void MPV_frame_end(MpegEncContext *s)
1058 1153
{
1059 1154
    int i;
1060
    /* draw edge for correct motion prediction if outside */
1155
    /* redraw edges for the frame if decoding didn't complete */
1061 1156
    //just to make sure that all data is rendered.
1062 1157
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
1063 1158
        ff_xvmc_field_end(s);
......
1067 1162
       && s->current_picture.reference
1068 1163
       && !s->intra_only
1069 1164
       && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1070
            s->dsp.draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
1071
            s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1072
            s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1165
        int edges = EDGE_BOTTOM | EDGE_TOP, h = s->v_edge_pos;
1166

  
1167
            s->dsp.draw_edges(s->current_picture_ptr->data[0], s->linesize  , s->h_edge_pos   , h   , EDGE_WIDTH  , edges);
1168
            s->dsp.draw_edges(s->current_picture_ptr->data[1], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
1169
            s->dsp.draw_edges(s->current_picture_ptr->data[2], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
1170

  
1073 1171
    }
1172

  
1074 1173
    emms_c();
1075 1174

  
1076 1175
    s->last_pict_type    = s->pict_type;
......
1091 1190

  
1092 1191
    if(s->encoding){
1093 1192
        /* release non-reference frames */
1094
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1193
        for(i=0; i<s->picture_count; i++){
1095 1194
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1096 1195
                free_frame_buffer(s, &s->picture[i]);
1097 1196
            }
......
1104 1203
    memset(&s->current_picture, 0, sizeof(Picture));
1105 1204
#endif
1106 1205
    s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
1206

  
1207
    if (s->codec_id != CODEC_ID_H264 && s->current_picture.reference) {
1208
        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
1209
    }
1107 1210
}
1108 1211

  
1109 1212
/**
......
1768 1871
    }
1769 1872
}
1770 1873

  
1874
/**
1875
 * find the lowest MB row referenced in the MVs
1876
 */
1877
int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
1878
{
1879
    int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1880
    int my, off, i, mvs;
1881

  
1882
    if (s->picture_structure != PICT_FRAME) goto unhandled;
1883

  
1884
    switch (s->mv_type) {
1885
        case MV_TYPE_16X16:
1886
            mvs = 1;
1887
            break;
1888
        case MV_TYPE_16X8:
1889
            mvs = 2;
1890
            break;
1891
        case MV_TYPE_8X8:
1892
            mvs = 4;
1893
            break;
1894
        default:
1895
            goto unhandled;
1896
    }
1897

  
1898
    for (i = 0; i < mvs; i++) {
1899
        my = s->mv[dir][i][1]<<qpel_shift;
1900
        my_max = FFMAX(my_max, my);
1901
        my_min = FFMIN(my_min, my);
1902
    }
1903

  
1904
    off = (FFMAX(-my_min, my_max) + 63) >> 6;
1905

  
1906
    return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
1907
unhandled:
1908
    return s->mb_height-1;
1909
}
1910

  
1771 1911
/* put block[] to dest[] */
1772 1912
static inline void put_dct(MpegEncContext *s,
1773 1913
                           DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
......
1932 2072
            /* motion handling */
1933 2073
            /* decoding or more than one mb_type (MC was already done otherwise) */
1934 2074
            if(!s->encoding){
2075

  
2076
                if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2077
                    if (s->mv_dir & MV_DIR_FORWARD) {
2078
                        ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
2079
                    }
2080
                    if (s->mv_dir & MV_DIR_BACKWARD) {
2081
                        ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
2082
                    }
2083
                }
2084

  
1935 2085
                if(lowres_flag){
1936 2086
                    h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
1937 2087

  
......
2096 2246
 * @param h is the normal height, this will be reduced automatically if needed for the last row
2097 2247
 */
2098 2248
void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2249
	const int field_pic= s->picture_structure != PICT_FRAME;
2250
    if(field_pic){
2251
        h <<= 1;
2252
        y <<= 1;
2253
    }
2254

  
2255
    if (!s->avctx->hwaccel
2256
       && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2257
       && s->unrestricted_mv
2258
       && s->current_picture.reference
2259
       && !s->intra_only
2260
       && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
2261
        int sides = 0, edge_h;
2262
        if (y==0) sides |= EDGE_TOP;
2263
        if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
2264

  
2265
        edge_h= FFMIN(h, s->v_edge_pos - y);
2266

  
2267
        s->dsp.draw_edges(s->current_picture_ptr->data[0] +  y    *s->linesize  , s->linesize  , s->h_edge_pos   , edge_h   , EDGE_WIDTH  , sides);
2268
        s->dsp.draw_edges(s->current_picture_ptr->data[1] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides);
2269
        s->dsp.draw_edges(s->current_picture_ptr->data[2] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides);
2270
    }
2271

  
2272
    h= FFMIN(h, s->avctx->height - y);
2273

  
2274
    if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2275

  
2099 2276
    if (s->avctx->draw_horiz_band) {
2100 2277
        AVFrame *src;
2101
        const int field_pic= s->picture_structure != PICT_FRAME;
2102 2278
        int offset[4];
2103 2279

  
2104
        h= FFMIN(h, (s->avctx->height>>field_pic) - y);
2105

  
2106
        if(field_pic && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)){
2107
            h <<= 1;
2108
            y <<= 1;
2109
            if(s->first_field) return;
2110
        }
2111

  
2112 2280
        if(s->pict_type==FF_B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
2113 2281
            src= (AVFrame*)s->current_picture_ptr;
2114 2282
        else if(s->last_picture_ptr)
......
2174 2342
    if(s==NULL || s->picture==NULL)
2175 2343
        return;
2176 2344

  
2177
    for(i=0; i<MAX_PICTURE_COUNT; i++){
2345
    for(i=0; i<s->picture_count; i++){
2178 2346
       if(s->picture[i].data[0] && (   s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
2179 2347
                                    || s->picture[i].type == FF_BUFFER_TYPE_USER))
2180 2348
        free_frame_buffer(s, &s->picture[i]);
......
2428 2596
    s->y_dc_scale= s->y_dc_scale_table[ qscale ];
2429 2597
    s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
2430 2598
}
2599

  
2600
void MPV_report_decode_progress(MpegEncContext *s)
2601
{
2602
    if (s->pict_type != FF_B_TYPE && !s->partitioned_frame)
2603
        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
2604
}

Also available in: Unified diff