| 914 | } |
| 915 | |
| 916 | av_cold int ff_frame_thread_init(AVCodecContext *avctx) |
| 917 | { |
| 918 | int thread_count = avctx->thread_count; |
| 919 | const FFCodec *codec = ffcodec(avctx->codec); |
| 920 | FrameThreadContext *fctx; |
| 921 | int err, i = 0; |
| 922 | |
| 923 | if (!thread_count) { |
| 924 | int nb_cpus = av_cpu_count(); |
| 925 | // use number of cores + 1 as thread count if there is more than one |
| 926 | if (nb_cpus > 1) |
| 927 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); |
| 928 | else |
| 929 | thread_count = avctx->thread_count = 1; |
| 930 | } |
| 931 | |
| 932 | if (thread_count <= 1) { |
| 933 | avctx->active_thread_type = 0; |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext)); |
| 938 | if (!fctx) |
| 939 | return AVERROR(ENOMEM); |
| 940 | |
| 941 | err = ff_pthread_init(fctx, thread_ctx_offsets); |
| 942 | if (err < 0) { |
| 943 | ff_pthread_free(fctx, thread_ctx_offsets); |
| 944 | av_freep(&avctx->internal->thread_ctx); |
| 945 | return err; |
| 946 | } |
| 947 | |
| 948 | fctx->next_pkt = av_packet_alloc(); |
| 949 | if (!fctx->next_pkt) |
| 950 | return AVERROR(ENOMEM); |
| 951 | |
| 952 | fctx->async_lock = 1; |
| 953 | |
| 954 | if (codec->p.type == AVMEDIA_TYPE_VIDEO) |
| 955 | avctx->delay = avctx->thread_count - 1; |
| 956 | |
| 957 | fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads)); |
| 958 | if (!fctx->threads) { |
| 959 | err = AVERROR(ENOMEM); |
| 960 | goto error; |
| 961 | } |
| 962 | |
| 963 | for (; i < thread_count; ) { |
| 964 | PerThreadContext *p = &fctx->threads[i]; |
| 965 | int first = !i; |
| 966 | |
| 967 | err = init_thread(p, &i, fctx, avctx, codec, first); |
| 968 | if (err < 0) |
| 969 | goto error; |
| 970 | } |
| 971 | |
| 972 | return 0; |
| 973 |
no test coverage detected