pthread.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 Roman Shaposhnik
3  * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
4  *
5  * Many thanks to Steven M. Schultz for providing clever ideas and
6  * to Michael Niedermayer <michaelni@gmx.at> for writing initial
7  * implementation.
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * 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  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
32 #include "config.h"
33 
34 #if HAVE_SCHED_GETAFFINITY
35 #define _GNU_SOURCE
36 #include <sched.h>
37 #endif
38 #if HAVE_GETPROCESSAFFINITYMASK
39 #include <windows.h>
40 #endif
41 #if HAVE_SYSCTL
42 #if HAVE_SYS_PARAM_H
43 #include <sys/param.h>
44 #endif
45 #include <sys/types.h>
46 #include <sys/sysctl.h>
47 #endif
48 #if HAVE_SYSCONF
49 #include <unistd.h>
50 #endif
51 
52 #include "avcodec.h"
53 #include "internal.h"
54 #include "thread.h"
55 
56 #if HAVE_PTHREADS
57 #include <pthread.h>
58 #elif HAVE_W32THREADS
59 #include "w32pthreads.h"
60 #endif
61 
62 typedef int (action_func)(AVCodecContext *c, void *arg);
63 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
64 
65 typedef struct ThreadContext {
69  void *args;
70  int *rets;
72  int job_count;
73  int job_size;
74 
79  int done;
81 
83 #define MAX_BUFFERS (32+1)
84 
88 typedef struct PerThreadContext {
90 
96 
99 
101 
104 
106  int got_frame;
107  int result;
108 
109  enum {
117  } state;
118 
125 
131 
134 
138 typedef struct FrameThreadContext {
141 
143 
146 
147  int delaying;
152  int die;
154 
155 
156 /* H264 slice threading seems to be buggy with more than 16 threads,
157  * limit the number of threads to 16 for automatic detection */
158 #define MAX_AUTO_THREADS 16
159 
161 {
162  int ret, nb_cpus = 1;
163 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
164  cpu_set_t cpuset;
165 
166  CPU_ZERO(&cpuset);
167 
168  ret = sched_getaffinity(0, sizeof(cpuset), &cpuset);
169  if (!ret) {
170  nb_cpus = CPU_COUNT(&cpuset);
171  }
172 #elif HAVE_GETPROCESSAFFINITYMASK
173  DWORD_PTR proc_aff, sys_aff;
174  ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff);
175  if (ret)
176  nb_cpus = av_popcount64(proc_aff);
177 #elif HAVE_SYSCTL && defined(HW_NCPU)
178  int mib[2] = { CTL_HW, HW_NCPU };
179  size_t len = sizeof(nb_cpus);
180 
181  ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0);
182  if (ret == -1)
183  nb_cpus = 0;
184 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
185  nb_cpus = sysconf(_SC_NPROC_ONLN);
186 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
187  nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
188 #endif
189  av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
190  return nb_cpus;
191 }
192 
193 
194 static void* attribute_align_arg worker(void *v)
195 {
196  AVCodecContext *avctx = v;
197  ThreadContext *c = avctx->thread_opaque;
198  int our_job = c->job_count;
199  int thread_count = avctx->thread_count;
200  int self_id;
201 
203  self_id = c->current_job++;
204  for (;;){
205  while (our_job >= c->job_count) {
206  if (c->current_job == thread_count + c->job_count)
208 
210  our_job = self_id;
211 
212  if (c->done) {
214  return NULL;
215  }
216  }
218 
219  c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
220  c->func2(avctx, c->args, our_job, self_id);
221 
223  our_job = c->current_job++;
224  }
225 }
226 
228 {
231 }
232 
233 static void thread_free(AVCodecContext *avctx)
234 {
235  ThreadContext *c = avctx->thread_opaque;
236  int i;
237 
239  c->done = 1;
242 
243  for (i=0; i<avctx->thread_count; i++)
244  pthread_join(c->workers[i], NULL);
245 
249  av_free(c->workers);
250  av_freep(&avctx->thread_opaque);
251 }
252 
253 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
254 {
255  ThreadContext *c= avctx->thread_opaque;
256  int dummy_ret;
257 
258  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
259  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
260 
261  if (job_count <= 0)
262  return 0;
263 
265 
266  c->current_job = avctx->thread_count;
267  c->job_count = job_count;
268  c->job_size = job_size;
269  c->args = arg;
270  c->func = func;
271  if (ret) {
272  c->rets = ret;
273  c->rets_count = job_count;
274  } else {
275  c->rets = &dummy_ret;
276  c->rets_count = 1;
277  }
279 
281 
282  return 0;
283 }
284 
285 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
286 {
287  ThreadContext *c= avctx->thread_opaque;
288  c->func2 = func2;
289  return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
290 }
291 
292 static int thread_init(AVCodecContext *avctx)
293 {
294  int i;
295  ThreadContext *c;
296  int thread_count = avctx->thread_count;
297 
298  if (!thread_count) {
299  int nb_cpus = get_logical_cpus(avctx);
300  // use number of cores + 1 as thread count if there is more than one
301  if (nb_cpus > 1)
302  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
303  else
304  thread_count = avctx->thread_count = 1;
305  }
306 
307  if (thread_count <= 1) {
308  avctx->active_thread_type = 0;
309  return 0;
310  }
311 
312  c = av_mallocz(sizeof(ThreadContext));
313  if (!c)
314  return -1;
315 
316  c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
317  if (!c->workers) {
318  av_free(c);
319  return -1;
320  }
321 
322  avctx->thread_opaque = c;
323  c->current_job = 0;
324  c->job_count = 0;
325  c->job_size = 0;
326  c->done = 0;
331  for (i=0; i<thread_count; i++) {
332  if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
333  avctx->thread_count = i;
335  ff_thread_free(avctx);
336  return -1;
337  }
338  }
339 
340  avcodec_thread_park_workers(c, thread_count);
341 
344  return 0;
345 }
346 
355 {
356  PerThreadContext *p = arg;
357  FrameThreadContext *fctx = p->parent;
358  AVCodecContext *avctx = p->avctx;
359  AVCodec *codec = avctx->codec;
360 
361  while (1) {
362  if (p->state == STATE_INPUT_READY && !fctx->die) {
364  while (p->state == STATE_INPUT_READY && !fctx->die)
367  }
368 
369  if (fctx->die) break;
370 
371  if (!codec->update_thread_context && avctx->thread_safe_callbacks)
372  ff_thread_finish_setup(avctx);
373 
376  p->got_frame = 0;
377  p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
378 
379  if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
380 
381  p->state = STATE_INPUT_READY;
382 
386 
388  }
389 
390  return NULL;
391 }
392 
400 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
401 {
402  int err = 0;
403 
404  if (dst != src) {
405  dst->sub_id = src->sub_id;
406  dst->time_base = src->time_base;
407  dst->width = src->width;
408  dst->height = src->height;
409  dst->pix_fmt = src->pix_fmt;
410 
411  dst->coded_width = src->coded_width;
412  dst->coded_height = src->coded_height;
413 
414  dst->has_b_frames = src->has_b_frames;
415  dst->idct_algo = src->idct_algo;
416 
420 
421  dst->profile = src->profile;
422  dst->level = src->level;
423 
425  dst->ticks_per_frame = src->ticks_per_frame;
426  dst->color_primaries = src->color_primaries;
427 
428  dst->color_trc = src->color_trc;
429  dst->colorspace = src->colorspace;
430  dst->color_range = src->color_range;
432  }
433 
434  if (for_user) {
435  dst->coded_frame = src->coded_frame;
436  } else {
437  if (dst->codec->update_thread_context)
438  err = dst->codec->update_thread_context(dst, src);
439  }
440 
441  return err;
442 }
443 
452 {
453 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
454  dst->flags = src->flags;
455 
456  dst->draw_horiz_band= src->draw_horiz_band;
457  dst->get_buffer = src->get_buffer;
458  dst->release_buffer = src->release_buffer;
459 
460  dst->opaque = src->opaque;
461  dst->dsp_mask = src->dsp_mask;
462  dst->debug = src->debug;
463  dst->debug_mv = src->debug_mv;
464 
465  dst->slice_flags = src->slice_flags;
466  dst->flags2 = src->flags2;
467 
469 
470  dst->frame_number = src->frame_number;
472 
473  if (src->slice_count && src->slice_offset) {
474  if (dst->slice_count < src->slice_count) {
475  int *tmp = av_realloc(dst->slice_offset, src->slice_count *
476  sizeof(*dst->slice_offset));
477  if (!tmp) {
478  av_free(dst->slice_offset);
479  return AVERROR(ENOMEM);
480  }
481  dst->slice_offset = tmp;
482  }
483  memcpy(dst->slice_offset, src->slice_offset,
484  src->slice_count * sizeof(*dst->slice_offset));
485  }
486  dst->slice_count = src->slice_count;
487  return 0;
488 #undef copy_fields
489 }
490 
491 static void free_progress(AVFrame *f)
492 {
494  int *progress = f->thread_opaque;
495 
496  p->progress_used[(progress - p->progress[0]) / 2] = 0;
497 }
498 
501 {
502  FrameThreadContext *fctx = p->parent;
503 
504  while (p->num_released_buffers > 0) {
505  AVFrame *f;
506 
509  free_progress(f);
510  f->thread_opaque = NULL;
511 
512  f->owner->release_buffer(f->owner, f);
514  }
515 }
516 
518 {
519  FrameThreadContext *fctx = p->parent;
520  PerThreadContext *prev_thread = fctx->prev_thread;
521  AVCodec *codec = p->avctx->codec;
522  uint8_t *buf = p->avpkt.data;
523 
524  if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
525 
527 
529 
530  if (prev_thread) {
531  int err;
532  if (prev_thread->state == STATE_SETTING_UP) {
533  pthread_mutex_lock(&prev_thread->progress_mutex);
534  while (prev_thread->state == STATE_SETTING_UP)
535  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
536  pthread_mutex_unlock(&prev_thread->progress_mutex);
537  }
538 
539  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
540  if (err) {
542  return err;
543  }
544  }
545 
547  p->avpkt = *avpkt;
548  p->avpkt.data = buf;
549  memcpy(buf, avpkt->data, avpkt->size);
550  memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
551 
552  p->state = STATE_SETTING_UP;
555 
556  /*
557  * If the client doesn't have a thread-safe get_buffer(),
558  * then decoding threads call back to the main thread,
559  * and it calls back to the client here.
560  */
561 
562  if (!p->avctx->thread_safe_callbacks &&
564  while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
566  while (p->state == STATE_SETTING_UP)
568 
569  if (p->state == STATE_GET_BUFFER) {
570  p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
571  p->state = STATE_SETTING_UP;
573  }
575  }
576  }
577 
578  fctx->prev_thread = p;
579  fctx->next_decoding++;
580 
581  return 0;
582 }
583 
585  AVFrame *picture, int *got_picture_ptr,
586  AVPacket *avpkt)
587 {
588  FrameThreadContext *fctx = avctx->thread_opaque;
589  int finished = fctx->next_finished;
590  PerThreadContext *p;
591  int err;
592 
593  /*
594  * Submit a packet to the next decoding thread.
595  */
596 
597  p = &fctx->threads[fctx->next_decoding];
598  err = update_context_from_user(p->avctx, avctx);
599  if (err) return err;
600  err = submit_packet(p, avpkt);
601  if (err) return err;
602 
603  /*
604  * If we're still receiving the initial packets, don't return a frame.
605  */
606 
607  if (fctx->delaying && avpkt->size) {
608  if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
609 
610  *got_picture_ptr=0;
611  return avpkt->size;
612  }
613 
614  /*
615  * Return the next available frame from the oldest thread.
616  * If we're at the end of the stream, then we have to skip threads that
617  * didn't output a frame, because we don't want to accidentally signal
618  * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
619  */
620 
621  do {
622  p = &fctx->threads[finished++];
623 
624  if (p->state != STATE_INPUT_READY) {
626  while (p->state != STATE_INPUT_READY)
629  }
630 
631  *picture = p->frame;
632  *got_picture_ptr = p->got_frame;
633  picture->pkt_dts = p->avpkt.dts;
634  picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
635  picture->width = avctx->width;
636  picture->height = avctx->height;
637  picture->format = avctx->pix_fmt;
638 
639  /*
640  * A later call with avkpt->size == 0 may loop over all threads,
641  * including this one, searching for a frame to return before being
642  * stopped by the "finished != fctx->next_finished" condition.
643  * Make sure we don't mistakenly return the same frame again.
644  */
645  p->got_frame = 0;
646 
647  if (finished >= avctx->thread_count) finished = 0;
648  } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
649 
650  update_context_from_thread(avctx, p->avctx, 1);
651 
652  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
653 
654  fctx->next_finished = finished;
655 
656  /* return the size of the consumed packet if no error occurred */
657  return (p->result >= 0) ? avpkt->size : p->result;
658 }
659 
660 void ff_thread_report_progress(AVFrame *f, int n, int field)
661 {
662  PerThreadContext *p;
663  int *progress = f->thread_opaque;
664 
665  if (!progress || progress[field] >= n) return;
666 
667  p = f->owner->thread_opaque;
668 
669  if (f->owner->debug&FF_DEBUG_THREADS)
670  av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
671 
673  progress[field] = n;
676 }
677 
678 void ff_thread_await_progress(AVFrame *f, int n, int field)
679 {
680  PerThreadContext *p;
681  int *progress = f->thread_opaque;
682 
683  if (!progress || progress[field] >= n) return;
684 
685  p = f->owner->thread_opaque;
686 
687  if (f->owner->debug&FF_DEBUG_THREADS)
688  av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
689 
691  while (progress[field] < n)
694 }
695 
697  PerThreadContext *p = avctx->thread_opaque;
698 
699  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
700 
702  p->state = STATE_SETUP_FINISHED;
705 }
706 
709 {
710  int i;
711 
712  for (i = 0; i < thread_count; i++) {
713  PerThreadContext *p = &fctx->threads[i];
714 
715  if (p->state != STATE_INPUT_READY) {
717  while (p->state != STATE_INPUT_READY)
720  }
721  }
722 }
723 
725 {
726  FrameThreadContext *fctx = avctx->thread_opaque;
727  AVCodec *codec = avctx->codec;
728  int i;
729 
730  park_frame_worker_threads(fctx, thread_count);
731 
732  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
734 
735  fctx->die = 1;
736 
737  for (i = 0; i < thread_count; i++) {
738  PerThreadContext *p = &fctx->threads[i];
739 
743 
744  if (p->thread_init)
745  pthread_join(p->thread, NULL);
746 
747  if (codec->close)
748  codec->close(p->avctx);
749 
750  avctx->codec = NULL;
751 
753  }
754 
755  for (i = 0; i < thread_count; i++) {
756  PerThreadContext *p = &fctx->threads[i];
757 
759 
765  av_freep(&p->avpkt.data);
766 
767  if (i) {
768  av_freep(&p->avctx->priv_data);
769  av_freep(&p->avctx->internal);
771  }
772 
773  av_freep(&p->avctx);
774  }
775 
776  av_freep(&fctx->threads);
778  av_freep(&avctx->thread_opaque);
779 }
780 
782 {
783  int thread_count = avctx->thread_count;
784  AVCodec *codec = avctx->codec;
785  AVCodecContext *src = avctx;
786  FrameThreadContext *fctx;
787  int i, err = 0;
788 
789  if (!thread_count) {
790  int nb_cpus = get_logical_cpus(avctx);
791  // use number of cores + 1 as thread count if there is more than one
792  if (nb_cpus > 1)
793  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
794  else
795  thread_count = avctx->thread_count = 1;
796  }
797 
798  if (thread_count <= 1) {
799  avctx->active_thread_type = 0;
800  return 0;
801  }
802 
803  avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
804 
805  fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
807  fctx->delaying = 1;
808 
809  for (i = 0; i < thread_count; i++) {
811  PerThreadContext *p = &fctx->threads[i];
812 
818 
819  p->parent = fctx;
820  p->avctx = copy;
821 
822  if (!copy) {
823  err = AVERROR(ENOMEM);
824  goto error;
825  }
826 
827  *copy = *src;
828  copy->thread_opaque = p;
829  copy->pkt = &p->avpkt;
830 
831  if (!i) {
832  src = copy;
833 
834  if (codec->init)
835  err = codec->init(copy);
836 
837  update_context_from_thread(avctx, copy, 1);
838  } else {
839  copy->priv_data = av_malloc(codec->priv_data_size);
840  if (!copy->priv_data) {
841  err = AVERROR(ENOMEM);
842  goto error;
843  }
844  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
845  copy->internal = av_malloc(sizeof(AVCodecInternal));
846  if (!copy->internal) {
847  err = AVERROR(ENOMEM);
848  goto error;
849  }
850  *copy->internal = *src->internal;
851  copy->internal->is_copy = 1;
852 
853  if (codec->init_thread_copy)
854  err = codec->init_thread_copy(copy);
855  }
856 
857  if (err) goto error;
858 
860  p->thread_init = 1;
861  }
862 
863  return 0;
864 
865 error:
866  frame_thread_free(avctx, i+1);
867 
868  return err;
869 }
870 
872 {
873  FrameThreadContext *fctx = avctx->thread_opaque;
874 
875  if (!avctx->thread_opaque) return;
876 
878  if (fctx->prev_thread) {
879  if (fctx->prev_thread != &fctx->threads[0])
881  if (avctx->codec->flush)
882  avctx->codec->flush(fctx->threads[0].avctx);
883  }
884 
885  fctx->next_decoding = fctx->next_finished = 0;
886  fctx->delaying = 1;
887  fctx->prev_thread = NULL;
888 }
889 
891 {
892  int i;
893 
894  for (i = 0; i < MAX_BUFFERS; i++)
895  if (!p->progress_used[i]) break;
896 
897  if (i == MAX_BUFFERS) {
898  av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
899  return NULL;
900  }
901 
902  p->progress_used[i] = 1;
903 
904  return p->progress[i];
905 }
906 
908 {
909  PerThreadContext *p = avctx->thread_opaque;
910  int *progress, err;
911 
912  f->owner = avctx;
913 
914  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
915  f->thread_opaque = NULL;
916  return avctx->get_buffer(avctx, f);
917  }
918 
919  if (p->state != STATE_SETTING_UP &&
920  (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
921  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
922  return -1;
923  }
924 
926  f->thread_opaque = progress = allocate_progress(p);
927 
928  if (!progress) {
930  return -1;
931  }
932 
933  progress[0] =
934  progress[1] = -1;
935 
936  if (avctx->thread_safe_callbacks ||
938  err = avctx->get_buffer(avctx, f);
939  } else {
940  p->requested_frame = f;
941  p->state = STATE_GET_BUFFER;
944 
945  while (p->state != STATE_SETTING_UP)
947 
948  err = p->result;
949 
951 
952  if (!avctx->codec->update_thread_context)
953  ff_thread_finish_setup(avctx);
954  }
955 
957 
958  return err;
959 }
960 
962 {
963  PerThreadContext *p = avctx->thread_opaque;
964  FrameThreadContext *fctx;
965 
966  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
967  avctx->release_buffer(avctx, f);
968  return;
969  }
970 
971  if (p->num_released_buffers >= MAX_BUFFERS) {
972  av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
973  return;
974  }
975 
976  if(avctx->debug & FF_DEBUG_BUFFERS)
977  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
978 
979  fctx = p->parent;
983  memset(f->data, 0, sizeof(f->data));
984 }
985 
996 {
997  int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
998  && !(avctx->flags & CODEC_FLAG_TRUNCATED)
999  && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
1000  && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
1001  if (avctx->thread_count == 1) {
1002  avctx->active_thread_type = 0;
1003  } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
1005  } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
1006  avctx->thread_type & FF_THREAD_SLICE) {
1008  } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) {
1009  avctx->thread_count = 1;
1010  avctx->active_thread_type = 0;
1011  }
1012 }
1013 
1015 {
1016  if (avctx->thread_opaque) {
1017  av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
1018  return -1;
1019  }
1020 
1021 #if HAVE_W32THREADS
1022  w32thread_init();
1023 #endif
1024 
1025  if (avctx->codec) {
1027 
1029  return thread_init(avctx);
1030  else if (avctx->active_thread_type&FF_THREAD_FRAME)
1031  return frame_thread_init(avctx);
1032  }
1033 
1034  return 0;
1035 }
1036 
1038 {
1040  frame_thread_free(avctx, avctx->thread_count);
1041  else
1042  thread_free(avctx);
1043 }