1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * Frame multithreading support functions
22 * @see doc/multithreading.txt
23 */
24
25 #include "config.h"
26
27 #include <stdint.h>
28
29 #if HAVE_PTHREADS
30 #include <pthread.h>
31 #elif HAVE_W32THREADS
33 #elif HAVE_OS2THREADS
35 #endif
36
42
51
52 /**
53 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
54 */
57
63
66
68
69 AVPacket avpkt;
///< Input packet (for decoding) or output (for encoding).
70
71 AVFrame *
frame;
///< Output frame (for decoding) or input (for encoding).
72 int got_frame;
///< The output of got_picture_ptr from the last avcodec_decode_video() call.
73 int result;
///< The result of the last codec decode/encode() call.
74
75 enum {
79 * Set when the codec calls get_buffer().
80 * State is returned to STATE_SETTING_UP afterwards.
81 */
83 * Set when the codec calls get_format().
84 * State is returned to STATE_SETTING_UP afterwards.
85 */
88
89 /**
90 * Array of frames passed to ff_thread_release_buffer().
91 * Frames are released after all threads referencing them are finished.
92 */
96
99
103
104 /**
105 * Context stored in the client AVCodecInternal thread_ctx.
106 */
110
112
115
117 * Set for the first N packets, where N is the number of threads.
118 * While it is set, ff_thread_en/decode_frame won't return any results.
119 */
120
121 int die;
///< Set when threads should exit.
123
124 #if FF_API_GET_BUFFER
125 #define THREAD_SAFE_CALLBACKS(avctx) \
126 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
127 #else
128 #define THREAD_SAFE_CALLBACKS(avctx) \
129 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
130 #endif
131
132 /**
133 * Codec worker thread.
134 *
135 * Automatically calls ff_thread_finish_setup() if the codec does
136 * not provide an update_thread_context method, or if the codec returns
137 * before calling it.
138 */
140 {
145
147 while (1) {
148 while (p->
state == STATE_INPUT_READY && !fctx->
die)
150
151 if (fctx->
die)
break;
152
155
159
163 "free the frame on failure. This is a bug, please report it.\n");
165 }
166
168
170 #if 0 //BUFREF-FIXME
171 for (i = 0; i < MAX_BUFFERS; i++)
173 p->progress[i][0] = INT_MAX;
174 p->progress[i][1] = INT_MAX;
175 }
176 #endif
177 p->
state = STATE_INPUT_READY;
178
182 }
184
186 }
187
188 /**
189 * Update the next thread's AVCodecContext with values from the reference thread's context.
190 *
191 * @param dst The destination context.
192 * @param src The source context.
193 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
194 */
196 {
197 int err = 0;
198
199 if (dst != src) {
205
208
211
214 #if FF_API_AFD
218 #endif /* FF_API_AFD */
219
222
226
231
234
240 }
241
242 if (for_user) {
245 } else {
248 }
249
250 return err;
251 }
252
253 /**
254 * Update the next thread's AVCodecContext with values set by the user.
255 *
256 * @param dst The destination context.
257 * @param src The source context.
258 * @return 0 on success, negative error code on failure
259 */
261 {
262 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
264
267 #if FF_API_GET_BUFFER
272 #endif
273
277
280
282
286
291 if (err < 0)
292 return err;
293 }
296 }
298 return 0;
299 #undef copy_fields
300 }
301
302 /// Releases the buffers that this decoding thread was the last user of.
304 {
306
309
311
312 // fix extended data in case the caller screwed it up
318
320 }
321 }
322
324 {
328
330
332
334
335 if (prev_thread) {
336 int err;
337 if (prev_thread->
state == STATE_SETTING_UP) {
339 while (prev_thread->
state == STATE_SETTING_UP)
342 }
343
345 if (err) {
347 return err;
348 }
349 }
350
353
354 p->
state = STATE_SETTING_UP;
357
358 /*
359 * If the client doesn't have a thread-safe get_buffer(),
360 * then decoding threads call back to the main thread,
361 * and it calls back to the client here.
362 */
363
369 #endif
372 while (p->
state != STATE_SETUP_FINISHED && p->
state != STATE_INPUT_READY) {
373 int call_done = 1;
375 while (p->
state == STATE_SETTING_UP)
377
379 case STATE_GET_BUFFER:
381 break;
382 case STATE_GET_FORMAT:
384 break;
385 default:
386 call_done = 0;
387 break;
388 }
389 if (call_done) {
390 p->
state = STATE_SETTING_UP;
392 }
394 }
395 }
396
399
400 return 0;
401 }
402
404 AVFrame *picture,
int *got_picture_ptr,
406 {
410 int err;
411
412 /*
413 * Submit a packet to the next decoding thread.
414 */
415
418 if (err) return err;
420 if (err) return err;
421
422 /*
423 * If we're still receiving the initial packets, don't return a frame.
424 */
425
428
430 *got_picture_ptr=0;
433 }
434
435 /*
436 * Return the next available frame from the oldest thread.
437 * If we're at the end of the stream, then we have to skip threads that
438 * didn't output a frame, because we don't want to accidentally signal
439 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
440 */
441
442 do {
443 p = &fctx->
threads[finished++];
444
445 if (p->
state != STATE_INPUT_READY) {
447 while (p->
state != STATE_INPUT_READY)
450 }
451
455
456 /*
457 * A later call with avkpt->size == 0 may loop over all threads,
458 * including this one, searching for a frame to return before being
459 * stopped by the "finished != fctx->next_finished" condition.
460 * Make sure we don't mistakenly return the same frame again.
461 */
463
466
468
470
472
473 /* return the size of the consumed packet if no error occurred */
475 }
476
478 {
481
482 if (!progress || progress[field] >= n)
return;
483
485
488
493 }
494
496 {
499
500 if (!progress || progress[field] >= n)
return;
501
503
506
508 while (progress[field] < n)
511 }
512
515
517
518 if(p->
state == STATE_SETUP_FINISHED){
520 }
521
523 p->
state = STATE_SETUP_FINISHED;
526 }
527
528 /// Waits for all threads to finish.
530 {
531 int i;
532
533 for (i = 0; i < thread_count; i++) {
535
536 if (p->
state != STATE_INPUT_READY) {
538 while (p->
state != STATE_INPUT_READY)
541 }
543 }
544 }
545
547 {
550 int i;
551
553
559 }
560
562
563 for (i = 0; i < thread_count; i++) {
565
569
573
576
578
581 }
582
583 for (i = 0; i < thread_count; i++) {
585
593
594 if (i) {
597 }
598
601 }
602
606 }
607
609 {
614 int i, err = 0;
615
616 #if HAVE_W32THREADS
618 #endif
619
620 if (!thread_count) {
623 nb_cpus = 1;
624 // use number of cores + 1 as thread count if there is more than one
625 if (nb_cpus > 1)
627 else
629 }
630
631 if (thread_count <= 1) {
633 return 0;
634 }
635
637
641
642 for (i = 0; i < thread_count; i++) {
645
651
656 goto error;
657 }
658
661
662 if (!copy) {
664 goto error;
665 }
666
668
672 goto error;
673 }
677
678 if (!i) {
680
682 err = codec->
init(copy);
683
685 } else {
689 goto error;
690 }
693
696 }
697
698 if (err) goto error;
699
703 goto error;
704 }
705
706 return 0;
707
708 error:
710
711 return err;
712 }
713
715 {
716 int i;
718
719 if (!fctx) return;
720
725 }
726
732 // Make sure decode flush calls with size=0 won't return old frames
735
737
740 }
741 }
742
744 {
748 return 0;
749 }
750 return 1;
751 }
752
754 {
756 int err;
757
759
761
764
765 if (p->
state != STATE_SETTING_UP &&
767 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
768 return -1;
769 }
770
772 int *progress;
776 }
778
779 progress[0] = progress[1] = -1;
780 }
781
787 #endif
791 } else {
795 p->
state = STATE_GET_BUFFER;
797
798 while (p->
state != STATE_SETTING_UP)
800
802
804
805 }
808
809 if (err)
811
813
814 return err;
815 }
816
818 {
824 if (p->
state != STATE_SETTING_UP) {
825 av_log(avctx,
AV_LOG_ERROR,
"get_format() cannot be called after ff_thread_finish_setup()\n");
826 return -1;
827 }
830 p->
state = STATE_GET_FORMAT;
832
833 while (p->
state != STATE_SETTING_UP)
835
837
839
840 return res;
841 }
842
844 {
846 if (ret < 0)
849 }
850
852 {
859 (
862 #endif
865
866 if (!f->
f || !f->
f->
buf[0])
867 return;
868
871
874
875 if (can_direct_free) {
877 return;
878 }
879
882
884 goto fail;
888 if (!tmp)
889 goto fail;
891
894
896
897 fail:
899 }