Revision d375c104

View differences:

libavcodec/avcodec.h
725 725
 * Codec should fill in channel configuration and samplerate instead of container
726 726
 */
727 727
#define CODEC_CAP_CHANNEL_CONF     0x0400
728

  
728 729
/**
729 730
 * Codec is able to deal with negative linesizes
730 731
 */
731 732
#define CODEC_CAP_NEG_LINESIZES    0x0800
733

  
732 734
/**
733 735
 * Codec supports frame-level multithreading.
734 736
 */
......
2983 2985
    const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
2984 2986
    const int64_t *channel_layouts;         ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
2985 2987
    uint8_t max_lowres;                     ///< maximum value for lowres supported by the decoder
2988

  
2986 2989
    AVClass *priv_class;                    ///< AVClass for the private context
2990

  
2987 2991
    const AVProfile *profiles;              ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
2988 2992

  
2989 2993
    /**
libavcodec/dsputil.c
298 298

  
299 299
/* draw the edges of width 'w' of an image of size width, height */
300 300
//FIXME check that this is ok for mpeg4 interlaced
301
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
301
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w, int sides)
302 302
{
303 303
    uint8_t *ptr, *last_line;
304 304
    int i;
......
306 306
    last_line = buf + (height - 1) * wrap;
307 307
    for(i=0;i<w;i++) {
308 308
        /* top and bottom */
309
        memcpy(buf - (i + 1) * wrap, buf, width);
310
        memcpy(last_line + (i + 1) * wrap, last_line, width);
309
        if (sides&EDGE_TOP)    memcpy(buf - (i + 1) * wrap, buf, width);
310
        if (sides&EDGE_BOTTOM) memcpy(last_line + (i + 1) * wrap, last_line, width);
311 311
    }
312 312
    /* left and right */
313 313
    ptr = buf;
......
318 318
    }
319 319
    /* corners */
320 320
    for(i=0;i<w;i++) {
321
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
322
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
323
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
324
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
321
        if (sides&EDGE_TOP) {
322
            memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
323
            memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
324
        }
325

  
326
        if (sides&EDGE_BOTTOM) {
327
            memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
328
            memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
329
        }
325 330
    }
326 331
}
327 332

  
libavcodec/dsputil.h
492 492
#define BASIS_SHIFT 16
493 493
#define RECON_SHIFT 6
494 494

  
495
    void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
495
    void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int sides);
496 496
#define EDGE_WIDTH 16
497
#define EDGE_TOP    1
498
#define EDGE_BOTTOM 2
497 499

  
498 500
    void (*prefetch)(void *mem, int stride, int h);
499 501

  
libavcodec/h263dec.c
35 35
#include "mpeg4video_parser.h"
36 36
#include "msmpeg4.h"
37 37
#include "vdpau_internal.h"
38
#include "thread.h"
38 39
#include "flv.h"
39 40
#include "mpeg4video.h"
40 41

  
......
235 236
                    if(++s->mb_x >= s->mb_width){
236 237
                        s->mb_x=0;
237 238
                        ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
239
                        MPV_report_decode_progress(s);
238 240
                        s->mb_y++;
239 241
                    }
240 242
                    return 0;
......
255 257
        }
256 258

  
257 259
        ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
260
        MPV_report_decode_progress(s);
258 261

  
259 262
        s->mb_x= 0;
260 263
    }
......
639 642
    if(MPV_frame_start(s, avctx) < 0)
640 643
        return -1;
641 644

  
645
    if (!s->divx_packed) ff_thread_finish_setup(avctx);
646

  
642 647
    if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
643 648
        ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
644 649
        goto frame_end;
libavcodec/h264.c
36 36
#include "golomb.h"
37 37
#include "mathops.h"
38 38
#include "rectangle.h"
39
#include "thread.h"
39 40
#include "vdpau_internal.h"
40 41
#include "libavutil/avassert.h"
41 42

  
......
249 250
    return 0;
250 251
}
251 252

  
253
static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, int height,
254
                                 int y_offset, int list){
255
    int raw_my= h->mv_cache[list][ scan8[n] ][1];
256
    int filter_height= (raw_my&3) ? 2 : 0;
257
    int full_my= (raw_my>>2) + y_offset;
258
    int top = full_my - filter_height, bottom = full_my + height + filter_height;
259

  
260
    return FFMAX(abs(top), bottom);
261
}
262

  
263
static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, int height,
264
                               int y_offset, int list0, int list1, int *nrefs){
265
    MpegEncContext * const s = &h->s;
266
    int my;
267

  
268
    y_offset += 16*(s->mb_y >> MB_FIELD);
269

  
270
    if(list0){
271
        int ref_n = h->ref_cache[0][ scan8[n] ];
272
        Picture *ref= &h->ref_list[0][ref_n];
273

  
274
        // Error resilience puts the current picture in the ref list.
275
        // Don't try to wait on these as it will cause a deadlock.
276
        // Fields can wait on each other, though.
277
        if(ref->thread_opaque != s->current_picture.thread_opaque ||
278
           (ref->reference&3) != s->picture_structure) {
279
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
280
            if (refs[0][ref_n] < 0) nrefs[0] += 1;
281
            refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
282
        }
283
    }
284

  
285
    if(list1){
286
        int ref_n = h->ref_cache[1][ scan8[n] ];
287
        Picture *ref= &h->ref_list[1][ref_n];
288

  
289
        if(ref->thread_opaque != s->current_picture.thread_opaque ||
290
           (ref->reference&3) != s->picture_structure) {
291
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
292
            if (refs[1][ref_n] < 0) nrefs[1] += 1;
293
            refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
294
        }
295
    }
296
}
297

  
298
/**
299
 * Wait until all reference frames are available for MC operations.
300
 *
301
 * @param h the H264 context
302
 */
303
static void await_references(H264Context *h){
304
    MpegEncContext * const s = &h->s;
305
    const int mb_xy= h->mb_xy;
306
    const int mb_type= s->current_picture.mb_type[mb_xy];
307
    int refs[2][48];
308
    int nrefs[2] = {0};
309
    int ref, list;
310

  
311
    memset(refs, -1, sizeof(refs));
312

  
313
    if(IS_16X16(mb_type)){
314
        get_lowest_part_y(h, refs, 0, 16, 0,
315
                  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
316
    }else if(IS_16X8(mb_type)){
317
        get_lowest_part_y(h, refs, 0, 8, 0,
318
                  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
319
        get_lowest_part_y(h, refs, 8, 8, 8,
320
                  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
321
    }else if(IS_8X16(mb_type)){
322
        get_lowest_part_y(h, refs, 0, 16, 0,
323
                  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
324
        get_lowest_part_y(h, refs, 4, 16, 0,
325
                  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
326
    }else{
327
        int i;
328

  
329
        assert(IS_8X8(mb_type));
330

  
331
        for(i=0; i<4; i++){
332
            const int sub_mb_type= h->sub_mb_type[i];
333
            const int n= 4*i;
334
            int y_offset= (i&2)<<2;
335

  
336
            if(IS_SUB_8X8(sub_mb_type)){
337
                get_lowest_part_y(h, refs, n  , 8, y_offset,
338
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
339
            }else if(IS_SUB_8X4(sub_mb_type)){
340
                get_lowest_part_y(h, refs, n  , 4, y_offset,
341
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
342
                get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
343
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
344
            }else if(IS_SUB_4X8(sub_mb_type)){
345
                get_lowest_part_y(h, refs, n  , 8, y_offset,
346
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
347
                get_lowest_part_y(h, refs, n+1, 8, y_offset,
348
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
349
            }else{
350
                int j;
351
                assert(IS_SUB_4X4(sub_mb_type));
352
                for(j=0; j<4; j++){
353
                    int sub_y_offset= y_offset + 2*(j&2);
354
                    get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
355
                              IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
356
                }
357
            }
358
        }
359
    }
360

  
361
    for(list=h->list_count-1; list>=0; list--){
362
        for(ref=0; ref<48 && nrefs[list]; ref++){
363
            int row = refs[list][ref];
364
            if(row >= 0){
365
                Picture *ref_pic = &h->ref_list[list][ref];
366
                int ref_field = ref_pic->reference - 1;
367
                int ref_field_picture = ref_pic->field_picture;
368
                int pic_height = 16*s->mb_height >> ref_field_picture;
369

  
370
                row <<= MB_MBAFF;
371
                nrefs[list]--;
372

  
373
                if(!FIELD_PICTURE && ref_field_picture){ // frame referencing two fields
374
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
375
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1)           , pic_height-1), 0);
376
                }else if(FIELD_PICTURE && !ref_field_picture){ // field referencing one field of a frame
377
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field    , pic_height-1), 0);
378
                }else if(FIELD_PICTURE){
379
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
380
                }else{
381
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
382
                }
383
            }
384
        }
385
    }
386
}
387

  
252 388
#if 0
253 389
/**
254 390
 * DCT transforms the 16 dc values.
......
539 675

  
540 676
    assert(IS_INTER(mb_type));
541 677

  
678
    if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME)
679
        await_references(h);
542 680
    prefetch_motion(h, 0);
543 681

  
544 682
    if(IS_16X16(mb_type)){
......
887 1025
    ff_h264_decode_init_vlc();
888 1026

  
889 1027
    h->thread_context[0] = h;
890
    h->outputed_poc = INT_MIN;
1028
    h->outputed_poc = h->next_outputed_poc = INT_MIN;
891 1029
    h->prev_poc_msb= 1<<16;
892 1030
    h->x264_build = -1;
893 1031
    ff_h264_reset_sei(h);
......
910 1048
    return 0;
911 1049
}
912 1050

  
1051
static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
1052
{
1053
    int i;
1054

  
1055
    for (i=0; i<count; i++){
1056
        to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1057
    }
1058
}
1059

  
1060
static void copy_parameter_set(void **to, void **from, int count, int size)
1061
{
1062
    int i;
1063

  
1064
    for (i=0; i<count; i++){
1065
        if (to[i] && !from[i]) av_freep(&to[i]);
1066
        else if (from[i] && !to[i]) to[i] = av_malloc(size);
1067

  
1068
        if (from[i]) memcpy(to[i], from[i], size);
1069
    }
1070
}
1071

  
1072
static int decode_init_thread_copy(AVCodecContext *avctx){
1073
    H264Context *h= avctx->priv_data;
1074

  
1075
    if (!avctx->is_copy) return 0;
1076
    memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1077
    memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1078

  
1079
    return 0;
1080
}
1081

  
1082
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1083
static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
1084
    H264Context *h= dst->priv_data, *h1= src->priv_data;
1085
    MpegEncContext * const s = &h->s, * const s1 = &h1->s;
1086
    int inited = s->context_initialized, err;
1087
    int i;
1088

  
1089
    if(dst == src || !s1->context_initialized) return 0;
1090

  
1091
    err = ff_mpeg_update_thread_context(dst, src);
1092
    if(err) return err;
1093

  
1094
    //FIXME handle width/height changing
1095
    if(!inited){
1096
        for(i = 0; i < MAX_SPS_COUNT; i++)
1097
            av_freep(h->sps_buffers + i);
1098

  
1099
        for(i = 0; i < MAX_PPS_COUNT; i++)
1100
            av_freep(h->pps_buffers + i);
1101

  
1102
        memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
1103
        memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1104
        memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1105
        ff_h264_alloc_tables(h);
1106
        context_init(h);
1107

  
1108
        for(i=0; i<2; i++){
1109
            h->rbsp_buffer[i] = NULL;
1110
            h->rbsp_buffer_size[i] = 0;
1111
        }
1112

  
1113
        h->thread_context[0] = h;
1114

  
1115
        // frame_start may not be called for the next thread (if it's decoding a bottom field)
1116
        // so this has to be allocated here
1117
        h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
1118

  
1119
        s->dsp.clear_blocks(h->mb);
1120
    }
1121

  
1122
    //extradata/NAL handling
1123
    h->is_avc          = h1->is_avc;
1124

  
1125
    //SPS/PPS
1126
    copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
1127
    h->sps             = h1->sps;
1128
    copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
1129
    h->pps             = h1->pps;
1130

  
1131
    //Dequantization matrices
1132
    //FIXME these are big - can they be only copied when PPS changes?
1133
    copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1134

  
1135
    for(i=0; i<6; i++)
1136
        h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1137

  
1138
    for(i=0; i<2; i++)
1139
        h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1140

  
1141
    h->dequant_coeff_pps = h1->dequant_coeff_pps;
1142

  
1143
    //POC timing
1144
    copy_fields(h, h1, poc_lsb, redundant_pic_count);
1145

  
1146
    //reference lists
1147
    copy_fields(h, h1, ref_count, intra_gb);
1148
    copy_fields(h, h1, short_ref, cabac_init_idc);
1149

  
1150
    copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);
1151
    copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1);
1152
    copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
1153

  
1154
    h->last_slice_type = h1->last_slice_type;
1155

  
1156
    if(!s->current_picture_ptr) return 0;
1157

  
1158
    if(!s->dropable) {
1159
        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1160
        h->prev_poc_msb     = h->poc_msb;
1161
        h->prev_poc_lsb     = h->poc_lsb;
1162
    }
1163
    h->prev_frame_num_offset= h->frame_num_offset;
1164
    h->prev_frame_num       = h->frame_num;
1165
    h->outputed_poc         = h->next_outputed_poc;
1166

  
1167
    return 0;
1168
}
1169

  
913 1170
int ff_h264_frame_start(H264Context *h){
914 1171
    MpegEncContext * const s = &h->s;
915 1172
    int i;
......
961 1218

  
962 1219
    s->current_picture_ptr->field_poc[0]=
963 1220
    s->current_picture_ptr->field_poc[1]= INT_MAX;
1221

  
1222
    h->next_output_pic = NULL;
1223

  
964 1224
    assert(s->current_picture_ptr->long_ref==0);
965 1225

  
966 1226
    return 0;
967 1227
}
968 1228

  
1229
/**
1230
  * Run setup operations that must be run after slice header decoding.
1231
  * This includes finding the next displayed frame.
1232
  *
1233
  * @param h h264 master context
1234
  */
1235
static void decode_postinit(H264Context *h){
1236
    MpegEncContext * const s = &h->s;
1237
    Picture *out = s->current_picture_ptr;
1238
    Picture *cur = s->current_picture_ptr;
1239
    int i, pics, out_of_order, out_idx;
1240

  
1241
    s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
1242
    s->current_picture_ptr->pict_type= s->pict_type;
1243

  
1244
    if (h->next_output_pic) return;
1245

  
1246
    if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
1247
        //FIXME this allows the next thread to start once we encounter the first field of a PAFF packet
1248
        //This works if the next packet contains the second field. It does not work if both fields are
1249
        //in the same packet.
1250
        //ff_thread_finish_setup(s->avctx);
1251
        return;
1252
    }
1253

  
1254
    cur->interlaced_frame = 0;
1255
    cur->repeat_pict = 0;
1256

  
1257
    /* Signal interlacing information externally. */
1258
    /* Prioritize picture timing SEI information over used decoding process if it exists. */
1259

  
1260
    if(h->sps.pic_struct_present_flag){
1261
        switch (h->sei_pic_struct)
1262
        {
1263
        case SEI_PIC_STRUCT_FRAME:
1264
            break;
1265
        case SEI_PIC_STRUCT_TOP_FIELD:
1266
        case SEI_PIC_STRUCT_BOTTOM_FIELD:
1267
            cur->interlaced_frame = 1;
1268
            break;
1269
        case SEI_PIC_STRUCT_TOP_BOTTOM:
1270
        case SEI_PIC_STRUCT_BOTTOM_TOP:
1271
            if (FIELD_OR_MBAFF_PICTURE)
1272
                cur->interlaced_frame = 1;
1273
            else
1274
                // try to flag soft telecine progressive
1275
                cur->interlaced_frame = h->prev_interlaced_frame;
1276
            break;
1277
        case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1278
        case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1279
            // Signal the possibility of telecined film externally (pic_struct 5,6)
1280
            // From these hints, let the applications decide if they apply deinterlacing.
1281
            cur->repeat_pict = 1;
1282
            break;
1283
        case SEI_PIC_STRUCT_FRAME_DOUBLING:
1284
            // Force progressive here, as doubling interlaced frame is a bad idea.
1285
            cur->repeat_pict = 2;
1286
            break;
1287
        case SEI_PIC_STRUCT_FRAME_TRIPLING:
1288
            cur->repeat_pict = 4;
1289
            break;
1290
        }
1291

  
1292
        if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
1293
            cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
1294
    }else{
1295
        /* Derive interlacing flag from used decoding process. */
1296
        cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
1297
    }
1298
    h->prev_interlaced_frame = cur->interlaced_frame;
1299

  
1300
    if (cur->field_poc[0] != cur->field_poc[1]){
1301
        /* Derive top_field_first from field pocs. */
1302
        cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
1303
    }else{
1304
        if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
1305
            /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
1306
            if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
1307
              || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1308
                cur->top_field_first = 1;
1309
            else
1310
                cur->top_field_first = 0;
1311
        }else{
1312
            /* Most likely progressive */
1313
            cur->top_field_first = 0;
1314
        }
1315
    }
1316

  
1317
    //FIXME do something with unavailable reference frames
1318

  
1319
    /* Sort B-frames into display order */
1320

  
1321
    if(h->sps.bitstream_restriction_flag
1322
       && s->avctx->has_b_frames < h->sps.num_reorder_frames){
1323
        s->avctx->has_b_frames = h->sps.num_reorder_frames;
1324
        s->low_delay = 0;
1325
    }
1326

  
1327
    if(   s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
1328
       && !h->sps.bitstream_restriction_flag){
1329
        s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
1330
        s->low_delay= 0;
1331
    }
1332

  
1333
    pics = 0;
1334
    while(h->delayed_pic[pics]) pics++;
1335

  
1336
    assert(pics <= MAX_DELAYED_PIC_COUNT);
1337

  
1338
    h->delayed_pic[pics++] = cur;
1339
    if(cur->reference == 0)
1340
        cur->reference = DELAYED_PIC_REF;
1341

  
1342
    out = h->delayed_pic[0];
1343
    out_idx = 0;
1344
    for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
1345
        if(h->delayed_pic[i]->poc < out->poc){
1346
            out = h->delayed_pic[i];
1347
            out_idx = i;
1348
        }
1349
    if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
1350
        h->next_outputed_poc= INT_MIN;
1351
    out_of_order = out->poc < h->next_outputed_poc;
1352

  
1353
    if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
1354
        { }
1355
    else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
1356
       || (s->low_delay &&
1357
        ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
1358
         || cur->pict_type == FF_B_TYPE)))
1359
    {
1360
        s->low_delay = 0;
1361
        s->avctx->has_b_frames++;
1362
    }
1363

  
1364
    if(out_of_order || pics > s->avctx->has_b_frames){
1365
        out->reference &= ~DELAYED_PIC_REF;
1366
        for(i=out_idx; h->delayed_pic[i]; i++)
1367
            h->delayed_pic[i] = h->delayed_pic[i+1];
1368
    }
1369
    if(!out_of_order && pics > s->avctx->has_b_frames){
1370
        h->next_output_pic = out;
1371
        if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
1372
            h->next_outputed_poc = INT_MIN;
1373
        } else
1374
            h->next_outputed_poc = out->poc;
1375
    }else{
1376
        av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
1377
    }
1378

  
1379
    ff_thread_finish_setup(s->avctx);
1380
}
1381

  
969 1382
static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
970 1383
    MpegEncContext * const s = &h->s;
971 1384
    uint8_t *top_border;
......
1479 1892
            h->delayed_pic[i]->reference= 0;
1480 1893
        h->delayed_pic[i]= NULL;
1481 1894
    }
1482
    h->outputed_poc= INT_MIN;
1895
    h->outputed_poc=h->next_outputed_poc= INT_MIN;
1483 1896
    h->prev_interlaced_frame = 1;
1484 1897
    idr(h);
1485 1898
    if(h->s.current_picture_ptr)
......
1603 2016
    }
1604 2017
}
1605 2018

  
1606
static void field_end(H264Context *h){
2019
static void field_end(H264Context *h, int in_setup){
1607 2020
    MpegEncContext * const s = &h->s;
1608 2021
    AVCodecContext * const avctx= s->avctx;
1609 2022
    s->mb_y= 0;
1610 2023

  
1611
    s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
1612
    s->current_picture_ptr->pict_type= s->pict_type;
2024
    if (!in_setup && !s->dropable)
2025
        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
2026
                                 s->picture_structure==PICT_BOTTOM_FIELD);
1613 2027

  
1614 2028
    if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
1615 2029
        ff_vdpau_h264_set_reference_frames(s);
1616 2030

  
1617
    if(!s->dropable) {
1618
        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1619
        h->prev_poc_msb= h->poc_msb;
1620
        h->prev_poc_lsb= h->poc_lsb;
2031
    if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
2032
        if(!s->dropable) {
2033
            ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2034
            h->prev_poc_msb= h->poc_msb;
2035
            h->prev_poc_lsb= h->poc_lsb;
2036
        }
2037
        h->prev_frame_num_offset= h->frame_num_offset;
2038
        h->prev_frame_num= h->frame_num;
2039
        h->outputed_poc = h->next_outputed_poc;
1621 2040
    }
1622
    h->prev_frame_num_offset= h->frame_num_offset;
1623
    h->prev_frame_num= h->frame_num;
1624 2041

  
1625 2042
    if (avctx->hwaccel) {
1626 2043
        if (avctx->hwaccel->end_frame(avctx) < 0)
......
1737 2154

  
1738 2155
    if(first_mb_in_slice == 0){ //FIXME better field boundary detection
1739 2156
        if(h0->current_slice && FIELD_PICTURE){
1740
            field_end(h);
2157
            field_end(h, 1);
1741 2158
        }
1742 2159

  
1743 2160
        h0->current_slice = 0;
......
1806 2223
    if (s->context_initialized
1807 2224
        && (   s->width != s->avctx->width || s->height != s->avctx->height
1808 2225
            || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
1809
        if(h != h0)
2226
        if(h != h0) {
2227
            av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
1810 2228
            return -1;   // width / height changed during parallelized decoding
2229
        }
1811 2230
        free_tables(h, 0);
1812 2231
        flush_dpb(s->avctx);
1813 2232
        MPV_common_end(s);
......
1852 2271
        init_scan_tables(h);
1853 2272
        ff_h264_alloc_tables(h);
1854 2273

  
1855
        for(i = 1; i < s->avctx->thread_count; i++) {
1856
            H264Context *c;
1857
            c = h->thread_context[i] = av_malloc(sizeof(H264Context));
1858
            memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
1859
            memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
1860
            c->h264dsp = h->h264dsp;
1861
            c->sps = h->sps;
1862
            c->pps = h->pps;
1863
            init_scan_tables(c);
1864
            clone_tables(c, h, i);
1865
        }
1866

  
1867
        for(i = 0; i < s->avctx->thread_count; i++)
1868
            if(context_init(h->thread_context[i]) < 0)
2274
        if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
2275
            if (context_init(h) < 0)
1869 2276
                return -1;
2277
        } else {
2278
            for(i = 1; i < s->avctx->thread_count; i++) {
2279
                H264Context *c;
2280
                c = h->thread_context[i] = av_malloc(sizeof(H264Context));
2281
                memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
2282
                memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
2283
                c->h264dsp = h->h264dsp;
2284
                c->sps = h->sps;
2285
                c->pps = h->pps;
2286
                init_scan_tables(c);
2287
                clone_tables(c, h, i);
2288
            }
2289

  
2290
            for(i = 0; i < s->avctx->thread_count; i++)
2291
                if(context_init(h->thread_context[i]) < 0)
2292
                    return -1;
2293
        }
1870 2294
    }
1871 2295

  
1872 2296
    h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
......
1887 2311
    h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
1888 2312

  
1889 2313
    if(h0->current_slice == 0){
2314
        if(h->frame_num != h->prev_frame_num &&
2315
          (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num) < (h->frame_num - h->sps.ref_frame_count))
2316
            h->prev_frame_num = h->frame_num - h->sps.ref_frame_count - 1;
2317

  
1890 2318
        while(h->frame_num !=  h->prev_frame_num &&
1891 2319
              h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
1892 2320
            Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
......
1896 2324
            h->prev_frame_num++;
1897 2325
            h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
1898 2326
            s->current_picture_ptr->frame_num= h->prev_frame_num;
2327
            ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
2328
            ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
1899 2329
            ff_generate_sliding_window_mmcos(h);
1900 2330
            ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1901 2331
            /* Error concealment: if a ref is missing, copy the previous ref in its place.
......
2045 2475
    if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
2046 2476
        return -1;
2047 2477

  
2478
    //FIXME mt gives valgrind warnings and crashes if this is uncommented
2479
    /*
2480

  
2048 2481
    if(h->slice_type_nos!=FF_I_TYPE){
2049 2482
        s->last_picture_ptr= &h->ref_list[0][0];
2050 2483
        ff_copy_picture(&s->last_picture, s->last_picture_ptr);
......
2054 2487
        ff_copy_picture(&s->next_picture, s->next_picture_ptr);
2055 2488
    }
2056 2489

  
2490
    */
2491

  
2057 2492
    if(   (h->pps.weighted_pred          && h->slice_type_nos == FF_P_TYPE )
2058 2493
       ||  (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
2059 2494
        pred_weight_table(h);
......
2200 2635
                          +(h->ref_list[j][i].reference&3);
2201 2636
    }
2202 2637

  
2203
    h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
2638
    //FIXME: fix draw_edges+PAFF+frame threads
2639
    h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type&FF_THREAD_FRAME)) ? 0 : 16;
2204 2640
    h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
2205 2641

  
2206 2642
    if(s->avctx->debug&FF_DEBUG_PICT_INFO){
......
2521 2957
    h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2522 2958
}
2523 2959

  
2960
/**
2961
 * Draw edges and report progress for the last MB row.
2962
 */
2963
static void decode_finish_row(H264Context *h){
2964
    MpegEncContext * const s = &h->s;
2965
    int top = 16*(s->mb_y >> FIELD_PICTURE);
2966
    int height = 16 << FRAME_MBAFF;
2967
    int deblock_border = (16 + 4) << FRAME_MBAFF;
2968
    int pic_height = 16*s->mb_height >> FIELD_PICTURE;
2969

  
2970
    if (h->deblocking_filter) {
2971
        if((top + height) >= pic_height)
2972
            height += deblock_border;
2973

  
2974
        top -= deblock_border;
2975
    }
2976

  
2977
    if (top >= pic_height || (top + height) < h->emu_edge_height)
2978
        return;
2979

  
2980
    height = FFMIN(height, pic_height - top);
2981
    if (top < h->emu_edge_height) {
2982
        height = top+height;
2983
        top = 0;
2984
    }
2985

  
2986
    ff_draw_horiz_band(s, top, height);
2987

  
2988
    if (s->dropable) return;
2989

  
2990
    ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
2991
                             s->picture_structure==PICT_BOTTOM_FIELD);
2992
}
2993

  
2524 2994
static int decode_slice(struct AVCodecContext *avctx, void *arg){
2525 2995
    H264Context *h = *(void**)arg;
2526 2996
    MpegEncContext * const s = &h->s;
......
2574 3044
            if( ++s->mb_x >= s->mb_width ) {
2575 3045
                s->mb_x = 0;
2576 3046
                loop_filter(h);
2577
                ff_draw_horiz_band(s, 16*s->mb_y, 16);
3047
                decode_finish_row(h);
2578 3048
                ++s->mb_y;
2579 3049
                if(FIELD_OR_MBAFF_PICTURE) {
2580 3050
                    ++s->mb_y;
......
2614 3084
            if(++s->mb_x >= s->mb_width){
2615 3085
                s->mb_x=0;
2616 3086
                loop_filter(h);
2617
                ff_draw_horiz_band(s, 16*s->mb_y, 16);
3087
                decode_finish_row(h);
2618 3088
                ++s->mb_y;
2619 3089
                if(FIELD_OR_MBAFF_PICTURE) {
2620 3090
                    ++s->mb_y;
......
2747 3217
    int context_count = 0;
2748 3218
    int next_avc= h->is_avc ? 0 : buf_size;
2749 3219

  
2750
    h->max_contexts = avctx->thread_count;
3220
    h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
2751 3221
#if 0
2752 3222
    int i;
2753 3223
    for(i=0; i<50; i++){
......
2842 3312
            if((err = decode_slice_header(hx, h)))
2843 3313
               break;
2844 3314

  
3315
            s->current_picture_ptr->key_frame |=
3316
                    (hx->nal_unit_type == NAL_IDR_SLICE) ||
3317
                    (h->sei_recovery_frame_cnt >= 0);
3318

  
2845 3319
            if (h->current_slice == 1) {
3320
                if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
3321
                    decode_postinit(h);
3322
                }
3323

  
2846 3324
                if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
2847 3325
                    return -1;
2848 3326
                if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2849 3327
                    ff_vdpau_h264_picture_start(s);
2850 3328
            }
2851 3329

  
2852
            s->current_picture_ptr->key_frame |=
2853
                    (hx->nal_unit_type == NAL_IDR_SLICE) ||
2854
                    (h->sei_recovery_frame_cnt >= 0);
2855 3330
            if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
2856 3331
               && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
2857 3332
               && (avctx->skip_frame < AVDISCARD_BIDIR  || hx->slice_type_nos!=FF_B_TYPE)
......
2981 3456
        Picture *out;
2982 3457
        int i, out_idx;
2983 3458

  
3459
        s->current_picture_ptr = NULL;
3460

  
2984 3461
//FIXME factorize this with the output code below
2985 3462
        out = h->delayed_pic[0];
2986 3463
        out_idx = 0;
......
3017 3494
    }
3018 3495

  
3019 3496
    if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
3020
        Picture *out = s->current_picture_ptr;
3021
        Picture *cur = s->current_picture_ptr;
3022
        int i, pics, out_of_order, out_idx;
3023 3497

  
3024
        field_end(h);
3498
        if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h);
3025 3499

  
3026
        if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
3500
        field_end(h, 0);
3501

  
3502
        if (!h->next_output_pic) {
3027 3503
            /* Wait for second field. */
3028 3504
            *data_size = 0;
3029 3505

  
3030 3506
        } else {
3031
            cur->interlaced_frame = 0;
3032
            cur->repeat_pict = 0;
3033

  
3034
            /* Signal interlacing information externally. */
3035
            /* Prioritize picture timing SEI information over used decoding process if it exists. */
3036

  
3037
            if(h->sps.pic_struct_present_flag){
3038
                switch (h->sei_pic_struct)
3039
                {
3040
                case SEI_PIC_STRUCT_FRAME:
3041
                    break;
3042
                case SEI_PIC_STRUCT_TOP_FIELD:
3043
                case SEI_PIC_STRUCT_BOTTOM_FIELD:
3044
                    cur->interlaced_frame = 1;
3045
                    break;
3046
                case SEI_PIC_STRUCT_TOP_BOTTOM:
3047
                case SEI_PIC_STRUCT_BOTTOM_TOP:
3048
                    if (FIELD_OR_MBAFF_PICTURE)
3049
                        cur->interlaced_frame = 1;
3050
                    else
3051
                        // try to flag soft telecine progressive
3052
                        cur->interlaced_frame = h->prev_interlaced_frame;
3053
                    break;
3054
                case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
3055
                case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
3056
                    // Signal the possibility of telecined film externally (pic_struct 5,6)
3057
                    // From these hints, let the applications decide if they apply deinterlacing.
3058
                    cur->repeat_pict = 1;
3059
                    break;
3060
                case SEI_PIC_STRUCT_FRAME_DOUBLING:
3061
                    // Force progressive here, as doubling interlaced frame is a bad idea.
3062
                    cur->repeat_pict = 2;
3063
                    break;
3064
                case SEI_PIC_STRUCT_FRAME_TRIPLING:
3065
                    cur->repeat_pict = 4;
3066
                    break;
3067
                }
3068

  
3069
                if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
3070
                    cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
3071
            }else{
3072
                /* Derive interlacing flag from used decoding process. */
3073
                cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
3074
            }
3075
            h->prev_interlaced_frame = cur->interlaced_frame;
3076

  
3077
            if (cur->field_poc[0] != cur->field_poc[1]){
3078
                /* Derive top_field_first from field pocs. */
3079
                cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
3080
            }else{
3081
                if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
3082
                    /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
3083
                    if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
3084
                      || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
3085
                        cur->top_field_first = 1;
3086
                    else
3087
                        cur->top_field_first = 0;
3088
                }else{
3089
                    /* Most likely progressive */
3090
                    cur->top_field_first = 0;
3091
                }
3092
            }
3093

  
3094
        //FIXME do something with unavailable reference frames
3095

  
3096
            /* Sort B-frames into display order */
3097

  
3098
            if(h->sps.bitstream_restriction_flag
3099
               && s->avctx->has_b_frames < h->sps.num_reorder_frames){
3100
                s->avctx->has_b_frames = h->sps.num_reorder_frames;
3101
                s->low_delay = 0;
3102
            }
3103

  
3104
            if(   s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
3105
               && !h->sps.bitstream_restriction_flag){
3106
                s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
3107
                s->low_delay= 0;
3108
            }
3109

  
3110
            pics = 0;
3111
            while(h->delayed_pic[pics]) pics++;
3112

  
3113
            assert(pics <= MAX_DELAYED_PIC_COUNT);
3114

  
3115
            h->delayed_pic[pics++] = cur;
3116
            if(cur->reference == 0)
3117
                cur->reference = DELAYED_PIC_REF;
3118

  
3119
            out = h->delayed_pic[0];
3120
            out_idx = 0;
3121
            for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
3122
                if(h->delayed_pic[i]->poc < out->poc){
3123
                    out = h->delayed_pic[i];
3124
                    out_idx = i;
3125
                }
3126
            if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
3127
                h->outputed_poc= INT_MIN;
3128
            out_of_order = out->poc < h->outputed_poc;
3129

  
3130
            if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
3131
                { }
3132
            else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
3133
               || (s->low_delay &&
3134
                ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
3135
                 || cur->pict_type == FF_B_TYPE)))
3136
            {
3137
                s->low_delay = 0;
3138
                s->avctx->has_b_frames++;
3139
            }
3140

  
3141
            if(out_of_order || pics > s->avctx->has_b_frames){
3142
                out->reference &= ~DELAYED_PIC_REF;
3143
                for(i=out_idx; h->delayed_pic[i]; i++)
3144
                    h->delayed_pic[i] = h->delayed_pic[i+1];
3145
            }
3146
            if(!out_of_order && pics > s->avctx->has_b_frames){
3147
                *data_size = sizeof(AVFrame);
3148

  
3149
                if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
3150
                    h->outputed_poc = INT_MIN;
3151
                } else
3152
                    h->outputed_poc = out->poc;
3153
                *pict= *(AVFrame*)out;
3154
            }else{
3155
                av_log(avctx, AV_LOG_DEBUG, "no picture\n");
3156
            }
3507
            *data_size = sizeof(AVFrame);
3508
            *pict = *(AVFrame*)h->next_output_pic;
3157 3509
        }
3158 3510
    }
3159 3511

  
......
3412 3764
    NULL,
3413 3765
    ff_h264_decode_end,
3414 3766
    decode_frame,
3415
    /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3767
    /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS,
3416 3768
    .flush= flush_dpb,
3417 3769
    .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
3770
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
3771
    .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
3418 3772
    .profiles = NULL_IF_CONFIG_SMALL(profiles),
3419 3773
};
3420 3774

  
libavcodec/h264.h
392 392
    /**
393 393
     * num_ref_idx_l0/1_active_minus1 + 1
394 394
     */
395
    uint8_t *list_counts;            ///< Array of list_count per MB specifying the slice type
395 396
    unsigned int ref_count[2];   ///< counts frames or fields, depending on current mb mode
396 397
    unsigned int list_count;
397
    uint8_t *list_counts;            ///< Array of list_count per MB specifying the slice type
398 398
    Picture ref_list[2][48];         /**< 0..15: frame refs, 16..47: mbaff field refs.
399 399
                                          Reordered version of default_ref_list
400 400
                                          according to picture reordering in slice header */
......
504 504
    Picture *long_ref[32];
505 505
    Picture default_ref_list[2][32]; ///< base reference list for all slices of a coded picture
506 506
    Picture *delayed_pic[MAX_DELAYED_PIC_COUNT+2]; //FIXME size?
507
    Picture *next_output_pic;
507 508
    int outputed_poc;
509
    int next_outputed_poc;
508 510

  
509 511
    /**
510 512
     * memory management control operations buffer.
libavcodec/h264_direct.c
31 31
#include "mpegvideo.h"
32 32
#include "h264.h"
33 33
#include "rectangle.h"
34
#include "thread.h"
34 35

  
35 36
//#undef NDEBUG
36 37
#include <assert.h>
......
126 127
        h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
127 128
        ref1sidx=sidx= h->col_parity;
128 129
    }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
129
        h->col_fieldoff= s->mb_stride*(2*(h->ref_list[1][0].reference) - 3);
130
        h->col_fieldoff= 2*(h->ref_list[1][0].reference) - 3;
130 131
    }
131 132

  
132 133
    if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
......
140 141
    }
141 142
}
142 143

  
144
static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
145
{
146
    int ref_field = ref->reference - 1;
147
    int ref_field_picture = ref->field_picture;
148
    int ref_height = 16*h->s.mb_height >> ref_field_picture;
149

  
150
    if(!HAVE_PTHREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
151
        return;
152

  
153
    //FIXME it can be safe to access mb stuff
154
    //even if pixels aren't deblocked yet
155

  
156
    ff_thread_await_progress((AVFrame*)ref, FFMIN(16*mb_y >> ref_field_picture, ref_height-1),
157
                             ref_field_picture && ref_field);
158
}
159

  
143 160
static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
144 161
    MpegEncContext * const s = &h->s;
145 162
    int b8_stride = 2;
146 163
    int b4_stride = h->b_stride;
147
    int mb_xy = h->mb_xy;
164
    int mb_xy = h->mb_xy, mb_y = s->mb_y;
148 165
    int mb_type_col[2];
149 166
    const int16_t (*l1mv0)[2], (*l1mv1)[2];
150 167
    const int8_t *l1ref0, *l1ref1;
......
157 174

  
158 175
    assert(h->ref_list[1][0].reference&3);
159 176

  
177
    await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
178

  
160 179
#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
161 180

  
162 181

  
......
217 236

  
218 237
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
219 238
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
239
            mb_y = (s->mb_y&~1) + h->col_parity;
220 240
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
221 241
            b8_stride = 0;
222 242
        }else{
223
            mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
243
            mb_y  += h->col_fieldoff;
244
            mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
224 245
        }
225 246
        goto single_col;
226 247
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
227 248
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
249
            mb_y = s->mb_y&~1;
228 250
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
229 251
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
230 252
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
......
260 282
        }
261 283
    }
262 284

  
285
    await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
286

  
263 287
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
264 288
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
265 289
    l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
......
384 408
    MpegEncContext * const s = &h->s;
385 409
    int b8_stride = 2;
386 410
    int b4_stride = h->b_stride;
387
    int mb_xy = h->mb_xy;
411
    int mb_xy = h->mb_xy, mb_y = s->mb_y;
388 412
    int mb_type_col[2];
389 413
    const int16_t (*l1mv0)[2], (*l1mv1)[2];
390 414
    const int8_t *l1ref0, *l1ref1;
......
394 418

  
395 419
    assert(h->ref_list[1][0].reference&3);
396 420

  
421
    await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
422

  
397 423
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
398 424
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
425
            mb_y = (s->mb_y&~1) + h->col_parity;
399 426
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
400 427
            b8_stride = 0;
401 428
        }else{
402
            mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
429
            mb_y  += h->col_fieldoff;
430
            mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
403 431
        }
404 432
        goto single_col;
405 433
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
406 434
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
435
            mb_y = s->mb_y&~1;
407 436
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
408 437
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
409 438
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
......
440 469
        }
441 470
    }
442 471

  
472
    await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
473

  
443 474
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
444 475
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
445 476
    l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
libavcodec/mdec.c
31 31
#include "dsputil.h"
32 32
#include "mpegvideo.h"
33 33
#include "mpeg12.h"
34
#include "thread.h"
34 35

  
35 36
typedef struct MDECContext{
36 37
    AVCodecContext *avctx;
......
162 163
    int i;
163 164

  
164 165
    if(p->data[0])
165
        avctx->release_buffer(avctx, p);
166
        ff_thread_release_buffer(avctx, p);
166 167

  
167 168
    p->reference= 0;
168
    if(avctx->get_buffer(avctx, p) < 0){
169
    if(ff_thread_get_buffer(avctx, p) < 0){
169 170
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
170 171
        return -1;
171 172
    }
......
238 239
    return 0;
239 240
}
240 241

  
242
static av_cold int decode_init_thread_copy(AVCodecContext *avctx){
243
    MDECContext * const a = avctx->priv_data;
244
    AVFrame *p = (AVFrame*)&a->picture;
245

  
246
    avctx->coded_frame= p;
247
    a->avctx= avctx;
248

  
249
    p->qscale_table= av_mallocz( p->qstride * a->mb_height);
250

  
251
    return 0;
252
}
253

  
241 254
static av_cold int decode_end(AVCodecContext *avctx){
242 255
    MDECContext * const a = avctx->priv_data;
243 256

  
......
259 272
    NULL,
260 273
    decode_end,
261 274
    decode_frame,
262
    CODEC_CAP_DR1,
275
    CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
263 276
    .long_name= NULL_IF_CONFIG_SMALL("Sony PlayStation MDEC (Motion DECoder)"),
277
    .init_thread_copy= ONLY_IF_THREADS_ENABLED(decode_init_thread_copy)
264 278
};
265 279

  
libavcodec/mimic.c
27 27
#include "get_bits.h"
28 28
#include "bytestream.h"
29 29
#include "dsputil.h"
30
#include "thread.h"
30 31

  
31 32
#define MIMIC_HEADER_SIZE   20
32 33

  
......
51 52
    ScanTable       scantable;
52 53
    DSPContext      dsp;
53 54
    VLC             vlc;
55

  
56
    /* Kept in the context so multithreading can have a constant to read from */
57
    int             next_cur_index;
58
    int             next_prev_index;
54 59
} MimicContext;
55 60

  
56 61
static const uint32_t huffcodes[] = {
......
121 126
    return 0;
122 127
}
123 128

  
129
static int mimic_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
130
{
131
    MimicContext *dst = avctx->priv_data, *src = avctx_from->priv_data;
132

  
133
    if (avctx == avctx_from) return 0;
134

  
135
    dst->cur_index  = src->next_cur_index;
136
    dst->prev_index = src->next_prev_index;
137

  
138
    memcpy(dst->buf_ptrs, src->buf_ptrs, sizeof(src->buf_ptrs));
139
    memcpy(dst->flipped_ptrs, src->flipped_ptrs, sizeof(src->flipped_ptrs));
140

  
141
    memset(&dst->buf_ptrs[dst->cur_index], 0, sizeof(AVFrame));
142

  
143
    return 0;
144
}
145

  
124 146
static const int8_t vlcdec_lookup[9][64] = {
125 147
    {    0, },
126 148
    {   -1,   1, },
......
205 227
static int decode(MimicContext *ctx, int quality, int num_coeffs,
206 228
                  int is_iframe)
207 229
{
208
    int y, x, plane;
230
    int y, x, plane, cur_row = 0;
209 231

  
210 232
    for(plane = 0; plane < 3; plane++) {
211 233
        const int is_chroma = !!plane;
......
236 258
                        int index = (ctx->cur_index+backref)&15;
237 259
                        uint8_t *p = ctx->flipped_ptrs[index].data[0];
238 260

  
261
                        ff_thread_await_progress(&ctx->buf_ptrs[index], cur_row, 0);
239 262
                        if(p) {
240 263
                            p += src -
241 264
                                ctx->flipped_ptrs[ctx->prev_index].data[plane];
......
246 269
                        }
247 270
                    }
248 271
                } else {
272
                    ff_thread_await_progress(&ctx->buf_ptrs[ctx->prev_index], cur_row, 0);
249 273
                    ctx->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
250 274
                }
251 275
                src += 8;
......
253 277
            }
254 278
            src += (stride - ctx->num_hblocks[plane])<<3;
255 279
            dst += (stride - ctx->num_hblocks[plane])<<3;
280

  
281
            ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], cur_row++, 0);
256 282
        }
257 283
    }
258 284

  
......
326 352
    }
327 353

  
328 354
    ctx->buf_ptrs[ctx->cur_index].reference = 1;
329
    if(avctx->get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
355
    ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE;
356
    if(ff_thread_get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
330 357
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
331 358
        return -1;
332 359
    }
333 360

  
361
    ctx->next_prev_index = ctx->cur_index;
362
    ctx->next_cur_index  = (ctx->cur_index - 1) & 15;
363

  
334 364
    prepare_avpic(ctx, &ctx->flipped_ptrs[ctx->cur_index],
335 365
                  (AVPicture*) &ctx->buf_ptrs[ctx->cur_index]);
336 366

  
367
    ff_thread_finish_setup(avctx);
368

  
337 369
    av_fast_malloc(&ctx->swap_buf, &ctx->swap_buf_size,
338 370
                                 swap_buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
339 371
    if(!ctx->swap_buf)
......
345 377
    init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3);
346 378

  
347 379
    if(!decode(ctx, quality, num_coeffs, !is_pframe)) {
348
        avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
349
        return -1;
380
        if (avctx->active_thread_type&FF_THREAD_FRAME)
381
            ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], INT_MAX, 0);
382
        else {
383
            ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
384
            return -1;
385
        }
350 386
    }
351 387

  
352
    ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE;
353 388
    *(AVFrame*)data = ctx->buf_ptrs[ctx->cur_index];
354 389
    *data_size = sizeof(AVFrame);
355 390

  
356
    ctx->prev_index = ctx->cur_index;
357
    ctx->cur_index--;
358
    ctx->cur_index &= 15;
391
    ctx->prev_index = ctx->next_prev_index;
392
    ctx->cur_index  = ctx->next_cur_index;
359 393

  
360 394
    /* Only release frames that aren't used for backreferences anymore */
361 395
    if(ctx->buf_ptrs[ctx->cur_index].data[0])
362
        avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
396
        ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
363 397

  
364 398
    return buf_size;
365 399
}
......
370 404
    int i;
371 405

  
372 406
    av_free(ctx->swap_buf);
407

  
408
    if(avctx->is_copy) return 0;
409

  
373 410
    for(i = 0; i < 16; i++)
374 411
        if(ctx->buf_ptrs[i].data[0])
375
            avctx->release_buffer(avctx, &ctx->buf_ptrs[i]);
412
            ff_thread_release_buffer(avctx, &ctx->buf_ptrs[i]);
376 413
    free_vlc(&ctx->vlc);
377 414

  
378 415
    return 0;
......
387 424
    NULL,
388 425
    mimic_decode_end,
389 426
    mimic_decode_frame,
390
    CODEC_CAP_DR1,
427
    CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
391 428
    .long_name = NULL_IF_CONFIG_SMALL("Mimic"),
429
    .update_thread_context = ONLY_IF_THREADS_ENABLED(mimic_decode_update_thread_context)
392 430
};
libavcodec/mpeg12.c
37 37
#include "bytestream.h"
38 38
#include "vdpau_internal.h"
39 39
#include "xvmc_internal.h"
40
#include "thread.h"
40 41

  
41 42
//#undef NDEBUG
42 43
//#include <assert.h>
......
1179 1180
    return 0;
1180 1181
}
1181 1182

  
1183
static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
1184
{
1185
    Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1186
    MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1187
    int err;
1188

  
1189
    if(avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
1190
        return 0;
1191

  
1192
    err = ff_mpeg_update_thread_context(avctx, avctx_from);
1193
    if(err) return err;
1194

  
1195
    if(!ctx->mpeg_enc_ctx_allocated)
1196
        memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1197

  
1198
    if(!(s->pict_type == FF_B_TYPE || s->low_delay))
1199
        s->picture_number++;
1200

  
1201
    return 0;
1202
}
1203

  
1182 1204
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1183 1205
                                     const uint8_t *new_perm){
1184 1206
    uint16_t temp_matrix[64];
......
1595 1617
        }
1596 1618

  
1597 1619
        *s->current_picture_ptr->pan_scan= s1->pan_scan;
1620

  
1621
        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1622
            ff_thread_finish_setup(avctx);
1598 1623
    }else{ //second field
1599 1624
            int i;
1600 1625

  
......
1769 1794
            const int mb_size= 16>>s->avctx->lowres;
1770 1795

  
1771 1796
            ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
1797
            MPV_report_decode_progress(s);
1772 1798

  
1773 1799
            s->mb_x = 0;
1774 1800
            s->mb_y += 1<<field_pic;
......
1924 1950
            *pict= *(AVFrame*)s->current_picture_ptr;
1925 1951
            ff_print_debug_info(s, pict);
1926 1952
        } else {
1927
            s->picture_number++;
1953
            if (avctx->active_thread_type&FF_THREAD_FRAME)
1954
                s->picture_number++;
1928 1955
            /* latency of 1 frame for I- and P-frames */
1929 1956
            /* XXX: use another variable than picture_number */
1930 1957
            if (s->last_picture_ptr != NULL) {
......
2260 2287
        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
2261 2288
        if (start_code > 0x1ff){
2262 2289
            if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
2263
                if(avctx->thread_count > 1){
2290
                if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_SLICE){
2264 2291
                    int i;
2265 2292

  
2266 2293
                    avctx->execute(avctx, slice_decode_thread,  &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
......
2422 2449
                    break;
2423 2450
                }
2424 2451

  
2425
                if(avctx->thread_count > 1){
2452
                if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_SLICE){
2426 2453
                    int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
2427 2454
                    if(threshold <= mb_y){
2428 2455
                        MpegEncContext *thread_context= s2->thread_context[s->slice_count];
......
2496 2523
    .flush= flush,
2497 2524
    .max_lowres= 3,
2498 2525
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2526
    .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2499 2527
};
2500 2528

  
2501 2529
AVCodec ff_mpeg2video_decoder = {
......
2532 2560

  
2533 2561
#if CONFIG_MPEG_XVMC_DECODER
2534 2562
static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
2535
    if( avctx->thread_count > 1)
2563
    if( avctx->active_thread_type & FF_THREAD_SLICE )
2536 2564
        return -1;
2537 2565
    if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2538 2566
        return -1;
libavcodec/mpeg4videodec.c
23 23
#include "mpegvideo.h"
24 24
#include "mpeg4video.h"
25 25
#include "h263.h"
26
#include "thread.h"
26 27

  
27 28
// The defines below define the number of bits that are read at once for
28 29
// reading vlc values. Changing these may improve speed and data cache needs
......
373 374
        return -1;
374 375
    }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff