Revision 9128ae08 libavformat/utils.c

View differences:

libavformat/utils.c
2458 2458
    return ret;
2459 2459
}
2460 2460

  
2461
static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
2462
{
2463
    int i, j;
2464

  
2465
    for (i = 0; i < ic->nb_programs; i++)
2466
        for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2467
            if (ic->programs[i]->stream_index[j] == s)
2468
                return ic->programs[i];
2469
    return NULL;
2470
}
2471

  
2472
int av_find_best_stream(AVFormatContext *ic,
2473
                        enum AVMediaType type,
2474
                        int wanted_stream_nb,
2475
                        int related_stream,
2476
                        AVCodec **decoder_ret,
2477
                        int flags)
2478
{
2479
    int i, nb_streams = ic->nb_streams, stream_number = 0;
2480
    int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2481
    unsigned *program = NULL;
2482
    AVCodec *decoder = NULL, *best_decoder = NULL;
2483

  
2484
    if (related_stream >= 0 && wanted_stream_nb < 0) {
2485
        AVProgram *p = find_program_from_stream(ic, related_stream);
2486
        if (p) {
2487
            program = p->stream_index;
2488
            nb_streams = p->nb_stream_indexes;
2489
        }
2490
    }
2491
    for (i = 0; i < nb_streams; i++) {
2492
        AVStream *st = ic->streams[program ? program[i] : i];
2493
        AVCodecContext *avctx = st->codec;
2494
        if (avctx->codec_type != type)
2495
            continue;
2496
        if (wanted_stream_nb >= 0 && stream_number++ != wanted_stream_nb)
2497
            continue;
2498
        if (decoder_ret) {
2499
            decoder = avcodec_find_decoder(ic->streams[i]->codec->codec_id);
2500
            if (!decoder) {
2501
                if (ret < 0)
2502
                    ret = AVERROR_DECODER_NOT_FOUND;
2503
                continue;
2504
            }
2505
        }
2506
        if (best_count >= st->codec_info_nb_frames)
2507
            continue;
2508
        best_count = st->codec_info_nb_frames;
2509
        ret = i;
2510
        best_decoder = decoder;
2511
        if (program && i == nb_streams - 1 && ret < 0) {
2512
            program = NULL;
2513
            nb_streams = ic->nb_streams;
2514
            i = 0; /* no related stream found, try again with everything */
2515
        }
2516
    }
2517
    if (decoder_ret)
2518
        *decoder_ret = best_decoder;
2519
    return ret;
2520
}
2521

  
2461 2522
/*******************************************************/
2462 2523

  
2463 2524
int av_read_play(AVFormatContext *s)

Also available in: Unified diff