pthread.c 3.08 KB
Newer Older
1
/*
2
 * Copyright (c) 2004 Roman Shaposhnik
3
 * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
4
 *
5 6 7
 * Many thanks to Steven M. Schultz for providing clever ideas and
 * to Michael Niedermayer <michaelni@gmx.at> for writing initial
 * implementation.
8
 *
9 10 11
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
12 13
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * FFmpeg is distributed in the hope that it will be useful,
17 18 19 20 21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with FFmpeg; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
 */
25 26 27 28 29 30 31

/**
 * @file
 * Multithreading support functions
 * @see doc/multithreading.txt
 */

32
#include "avcodec.h"
33
#include "internal.h"
34
#include "pthread_internal.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "thread.h"

/**
 * Set the threading algorithms used.
 *
 * Threading requires more than one thread.
 * Frame threading requires entire frames to be passed to the codec,
 * and introduces extra decoding delay, so is incompatible with low_delay.
 *
 * @param avctx The context.
 */
static void validate_thread_parameters(AVCodecContext *avctx)
{
    int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
                                && !(avctx->flags & CODEC_FLAG_TRUNCATED)
                                && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
                                && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
    if (avctx->thread_count == 1) {
        avctx->active_thread_type = 0;
    } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
        avctx->active_thread_type = FF_THREAD_FRAME;
56 57
    } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
               avctx->thread_type & FF_THREAD_SLICE) {
58
        avctx->active_thread_type = FF_THREAD_SLICE;
59 60 61
    } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) {
        avctx->thread_count       = 1;
        avctx->active_thread_type = 0;
62
    }
63 64 65 66 67

    if (avctx->thread_count > MAX_AUTO_THREADS)
        av_log(avctx, AV_LOG_WARNING,
               "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
               avctx->thread_count, MAX_AUTO_THREADS);
68 69
}

70
int ff_thread_init(AVCodecContext *avctx)
71
{
Anton Khirnov's avatar
Anton Khirnov committed
72
    validate_thread_parameters(avctx);
73

Anton Khirnov's avatar
Anton Khirnov committed
74
    if (avctx->active_thread_type&FF_THREAD_SLICE)
75
        return ff_slice_thread_init(avctx);
Anton Khirnov's avatar
Anton Khirnov committed
76
    else if (avctx->active_thread_type&FF_THREAD_FRAME)
77
        return ff_frame_thread_init(avctx);
78 79 80 81

    return 0;
}

82
void ff_thread_free(AVCodecContext *avctx)
83 84
{
    if (avctx->active_thread_type&FF_THREAD_FRAME)
85
        ff_frame_thread_free(avctx, avctx->thread_count);
86
    else
87
        ff_slice_thread_free(avctx);
88
}