Revision d375c104 libavcodec/h264.c

View differences:

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

  

Also available in: Unified diff