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 FFmpeg.
10 *
11 * FFmpeg 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 * FFmpeg 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 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 * @file
28 * Multithreading support functions
29 * @see doc/multithreading.txt
30 */
31
32 #include "config.h"
33
40
41 #if HAVE_PTHREADS
42 #include <pthread.h>
43 #elif HAVE_W32THREADS
45 #elif HAVE_OS2THREADS
47 #endif
48
51
61
69
70 /**
71 * Context used by codec threads and stored in their AVCodecContext thread_opaque.
72 */
75
81
84
86
87 AVPacket avpkt;
///< Input packet (for decoding) or output (for encoding).
88 uint8_t *
buf;
///< backup storage for packet data when the input packet is not refcounted
90
91 AVFrame frame;
///< Output frame (for decoding) or input (for encoding).
92 int got_frame;
///< The output of got_picture_ptr from the last avcodec_decode_video() call.
93 int result;
///< The result of the last codec decode/encode() call.
94
95 enum {
99 * Set when the codec calls get_buffer().
100 * State is returned to STATE_SETTING_UP afterwards.
101 */
103 * Set when the codec calls get_format().
104 * State is returned to STATE_SETTING_UP afterwards.
105 */
108
109 /**
110 * Array of frames passed to ff_thread_release_buffer().
111 * Frames are released after all threads referencing them are finished.
112 */
116
119
123
124 /**
125 * Context stored in the client AVCodecContext thread_opaque.
126 */
130
132
135
137 * Set for the first N packets, where N is the number of threads.
138 * While it is set, ff_thread_en/decode_frame won't return any results.
139 */
140
141 int die;
///< Set when threads should exit.
143
144
145 /* H264 slice threading seems to be buggy with more than 16 threads,
146 * limit the number of threads to 16 for automatic detection */
147 #define MAX_AUTO_THREADS 16
148
150 {
154 int last_execute = 0;
156 int self_id;
157
160 for (;;){
164
168 our_job = self_id;
169
172 return NULL;
173 }
174 }
176
178 c->
func2(avctx, c->
args, our_job, self_id);
179
182 }
183 }
184
186 {
190 }
191
193 {
195 int i;
196
201
204
210 }
211
213 {
215 int dummy_ret;
216
219
220 if (job_count <= 0)
221 return 0;
222
224
230 if (ret) {
233 } else {
234 c->
rets = &dummy_ret;
236 }
239
241
242 return 0;
243 }
244
246 {
250 }
251
253 {
254 int i;
257
258 if (!thread_count) {
262 // use number of cores + 1 as thread count if there is more than one
263 if (nb_cpus > 1)
265 else
267 }
268
269 if (thread_count <= 1) {
271 return 0;
272 }
273
275 if (!c)
276 return -1;
277
281 return -1;
282 }
283
293 for (i=0; i<thread_count; i++) {
298 return -1;
299 }
300 }
301
303
306 return 0;
307 }
308
309 #define THREAD_SAFE_CALLBACKS(avctx) \
310 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
311
312 /**
313 * Codec worker thread.
314 *
315 * Automatically calls ff_thread_finish_setup() if the codec does
316 * not provide an update_thread_context method, or if the codec returns
317 * before calling it.
318 */
320 {
325
327 while (1) {
328 while (p->
state == STATE_INPUT_READY && !fctx->
die)
330
331 if (fctx->
die)
break;
332
335
339
340 /* many decoders assign whole AVFrames, thus overwriting extended_data;
341 * make sure it's set correctly */
343
345
347 #if 0 //BUFREF-FIXME
348 for (i = 0; i < MAX_BUFFERS; i++)
350 p->progress[i][0] = INT_MAX;
351 p->progress[i][1] = INT_MAX;
352 }
353 #endif
354 p->
state = STATE_INPUT_READY;
355
359 }
361
362 return NULL;
363 }
364
365 /**
366 * Update the next thread's AVCodecContext with values from the reference thread's context.
367 *
368 * @param dst The destination context.
369 * @param src The source context.
370 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
371 */
373 {
374 int err = 0;
375
376 if (dst != src) {
381
384
387
391
394
398
403
406
411 }
412
413 if (for_user) {
416 } else {
419 }
420
421 return err;
422 }
423
424 /**
425 * Update the next thread's AVCodecContext with values set by the user.
426 *
427 * @param dst The destination context.
428 * @param src The source context.
429 * @return 0 on success, negative error code on failure
430 */
432 {
433 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
435
438 #if FF_API_GET_BUFFER
439 dst->get_buffer = src->get_buffer;
440 dst->release_buffer = src->release_buffer;
441 #endif
442
446
449
451
455
460 if (!tmp) {
463 }
465 }
468 }
470 return 0;
471 #undef copy_fields
472 }
473
474 /// Releases the buffers that this decoding thread was the last user of.
476 {
478
481
483
484 // fix extended data in case the caller screwed it up
490
492 }
493 }
494
496 {
500
502
504
506
507 if (prev_thread) {
508 int err;
509 if (prev_thread->
state == STATE_SETTING_UP) {
511 while (prev_thread->
state == STATE_SETTING_UP)
514 }
515
517 if (err) {
519 return err;
520 }
521 }
522
527 else {
532 }
533
534 p->
state = STATE_SETTING_UP;
537
538 /*
539 * If the client doesn't have a thread-safe get_buffer(),
540 * then decoding threads call back to the main thread,
541 * and it calls back to the client here.
542 */
543
547 p->
avctx->get_buffer ||
548 #endif
550 while (p->
state != STATE_SETUP_FINISHED && p->
state != STATE_INPUT_READY) {
551 int call_done = 1;
553 while (p->
state == STATE_SETTING_UP)
555
557 case STATE_GET_BUFFER:
559 break;
560 case STATE_GET_FORMAT:
562 break;
563 default:
564 call_done = 0;
565 break;
566 }
567 if (call_done) {
568 p->
state = STATE_SETTING_UP;
570 }
572 }
573 }
574
577
578 return 0;
579 }
580
582 AVFrame *picture,
int *got_picture_ptr,
584 {
588 int err;
589
590 /*
591 * Submit a packet to the next decoding thread.
592 */
593
596 if (err) return err;
598 if (err) return err;
599
600 /*
601 * If we're still receiving the initial packets, don't return a frame.
602 */
603
606
608 *got_picture_ptr=0;
611 }
612
613 /*
614 * Return the next available frame from the oldest thread.
615 * If we're at the end of the stream, then we have to skip threads that
616 * didn't output a frame, because we don't want to accidentally signal
617 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
618 */
619
620 do {
621 p = &fctx->
threads[finished++];
622
623 if (p->
state != STATE_INPUT_READY) {
625 while (p->
state != STATE_INPUT_READY)
628 }
629
633
634 /*
635 * A later call with avkpt->size == 0 may loop over all threads,
636 * including this one, searching for a frame to return before being
637 * stopped by the "finished != fctx->next_finished" condition.
638 * Make sure we don't mistakenly return the same frame again.
639 */
641
644
646
648
650
651 /* return the size of the consumed packet if no error occurred */
653 }
654
656 {
659
660 if (!progress || progress[field] >= n)
return;
661
663
666
671 }
672
674 {
677
678 if (!progress || progress[field] >= n)
return;
679
681
684
686 while (progress[field] < n)
689 }
690
693
695
696 if(p->
state == STATE_SETUP_FINISHED){
698 }
699
701 p->
state = STATE_SETUP_FINISHED;
704 }
705
706 /// Waits for all threads to finish.
708 {
709 int i;
710
711 for (i = 0; i < thread_count; i++) {
713
714 if (p->
state != STATE_INPUT_READY) {
716 while (p->
state != STATE_INPUT_READY)
719 }
721 }
722 }
723
725 {
728 int i;
729
731
737 }
738
740
741 for (i = 0; i < thread_count; i++) {
743
747
751
754
756
759 }
760
761 for (i = 0; i < thread_count; i++) {
763
772
773 if (i) {
777 }
778
780 }
781
785 }
786
788 {
793 int i, err = 0;
794
795 if (!thread_count) {
798 nb_cpus = 1;
799 // use number of cores + 1 as thread count if there is more than one
800 if (nb_cpus > 1)
802 else
804 }
805
806 if (thread_count <= 1) {
808 return 0;
809 }
810
812
816
817 for (i = 0; i < thread_count; i++) {
820
826
829
830 if (!copy) {
832 goto error;
833 }
834
838
839 if (!i) {
841
843 err = codec->
init(copy);
844
846 } else {
850 goto error;
851 }
856 goto error;
857 }
860
863 }
864
865 if (err) goto error;
866
870 goto error;
871 }
872
873 return 0;
874
875 error:
877
878 return err;
879 }
880
882 {
883 int i;
885
887
894 }
895
901 // Make sure decode flush calls with size=0 won't return old frames
904
906 }
907 }
908
910 {
914 return 0;
915 }
916 return 1;
917 }
918
920 {
922 int err;
923
925
927
930
931 if (p->
state != STATE_SETTING_UP &&
933 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
934 return -1;
935 }
936
938 int *progress;
942 }
944
945 progress[0] = progress[1] = -1;
946 }
947
949
952 !avctx->get_buffer &&
953 #endif
956 } else {
960 p->
state = STATE_GET_BUFFER;
962
963 while (p->
state != STATE_SETTING_UP)
965
967
969
970 }
973
974 if (err)
976
978
979 return err;
980 }
981
983 {
989 if (p->
state != STATE_SETTING_UP) {
990 av_log(avctx,
AV_LOG_ERROR,
"get_format() cannot be called after ff_thread_finish_setup()\n");
991 return -1;
992 }
995 p->
state = STATE_GET_FORMAT;
997
998 while (p->
state != STATE_SETTING_UP)
1000
1002
1004
1006 }
1007
1009 {
1011 if (ret < 0)
1014 }
1015
1017 {
1023 (
1025 !avctx->get_buffer &&
1026 #endif
1028
1030 return;
1031
1034
1037
1038 if (can_direct_free) {
1040 return;
1041 }
1042
1045
1047 goto fail;
1051 if (!tmp)
1052 goto fail;
1054
1057
1059
1060 fail:
1062 }
1063
1064 /**
1065 * Set the threading algorithms used.
1066 *
1067 * Threading requires more than one thread.
1068 * Frame threading requires entire frames to be passed to the codec,
1069 * and introduces extra decoding delay, so is incompatible with low_delay.
1070 *
1071 * @param avctx The context.
1072 */
1074 {
1089 }
1090
1093 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
1095 }
1096
1098 {
1099 #if HAVE_W32THREADS
1101 #endif
1102
1104
1109
1110 return 0;
1111 }
1112
1114 {
1117 else
1119 }