History

avcodec/threadprogress: Add new API for frame-threaded progress

The API is similar to the ThreadFrame API, with the exception that it no longer has an included AVFrame and that it has its own mutexes and condition variables which makes it more independent of pthread_frame.c. One can wait on anything via a ThreadProgress. One just has to ensure that the lifetime of the object containing the ThreadProgress is long enough. This will typically be solved by putting a ThreadProgress in a refcounted structure that is shared between threads.

Andreas Rheinhardt committed 4 years ago

Commit 89828411 parent 0fb09462 files · +171 −0Browse files

Showing the changes to libavcodec (2 of 2 files). Show all

Unified · Split

2 files
  1. libavcodec/threadprogress.c +79 −0
  2. libavcodec/threadprogress.h +92 −0
libavcodec/threadprogress.c+79−0
@@ -0,0 +1,79 @@
1/*
2 * Copyright (c) 2022 Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <limits.h>
22#include <stdatomic.h>
23
24#include "pthread_internal.h"
25#include "threadprogress.h"
26#include "libavutil/attributes.h"
27#include "libavutil/thread.h"
28
29DEFINE_OFFSET_ARRAY(ThreadProgress, thread_progress, init,
30 (offsetof(ThreadProgress, progress_mutex)),
31 (offsetof(ThreadProgress, progress_cond)));
32
33av_cold int ff_thread_progress_init(ThreadProgress *pro, int init_mode)
34{
35 atomic_init(&pro->progress, init_mode ? -1 : INT_MAX);
36#if HAVE_THREADS
37 if (init_mode)
38 return ff_pthread_init(pro, thread_progress_offsets);
39#endif
40 pro->init = init_mode;
41 return 0;
42}
43
44av_cold void ff_thread_progress_destroy(ThreadProgress *pro)
45{
46#if HAVE_THREADS
47 ff_pthread_free(pro, thread_progress_offsets);
48#else
49 pro->init = 0;
50#endif
51}
52
53void ff_thread_progress_report(ThreadProgress *pro, int n)
54{
55 if (atomic_load_explicit(&pro->progress, memory_order_relaxed) >= n)
56 return;
57
58 atomic_store_explicit(&pro->progress, n, memory_order_release);
59
60 ff_mutex_lock(&pro->progress_mutex);
61 ff_cond_broadcast(&pro->progress_cond);
62 ff_mutex_unlock(&pro->progress_mutex);
63}
64
65void ff_thread_progress_await(const ThreadProgress *pro_c, int n)
66{
67 /* Casting const away here is safe, because we only read from progress
68 * and will leave pro_c in the same state upon leaving the function
69 * as it had at the beginning. */
70 ThreadProgress *pro = (ThreadProgress*)pro_c;
71
72 if (atomic_load_explicit(&pro->progress, memory_order_acquire) >= n)
73 return;
74
75 ff_mutex_lock(&pro->progress_mutex);
76 while (atomic_load_explicit(&pro->progress, memory_order_relaxed) < n)
77 ff_cond_wait(&pro->progress_cond, &pro->progress_mutex);
78 ff_mutex_unlock(&pro->progress_mutex);
79}
libavcodec/threadprogress.h+92−0
@@ -0,0 +1,92 @@
1/*
2 * Copyright (c) 2022 Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef AVCODEC_THREADPROGRESS_H
22#define AVCODEC_THREADPROGRESS_H
23
24/**
25 * ThreadProgress is an API to easily notify other threads about progress
26 * of any kind as long as it can be packaged into an int and is consistent
27 * with the natural ordering of integers.
28 *
29 * Each initialized ThreadProgress can be in one of two modes: No-op mode
30 * or ordinary mode. In the former mode, ff_thread_report_progress() and
31 * ff_thread_await_progress() are no-ops to simply support usecases like
32 * non-frame-threading. Only in the latter case perform these functions
33 * what their name already implies.
34 */
35
36#include <limits.h>
37#include <stdatomic.h>
38#include "libavutil/thread.h"
39
40/**
41 * This struct should be treated as opaque by users.
42 */
43typedef struct ThreadProgress {
44 atomic_int progress;
45 unsigned init;
46 AVMutex progress_mutex;
47 AVCond progress_cond;
48} ThreadProgress;
49
50/**
51 * Initialize a ThreadProgress.
52 *
53 * @param init_mode If zero, the ThreadProgress will be initialized
54 * to be in no-op mode as described above. Otherwise
55 * it is initialized to be in ordinary mode.
56 */
57int ff_thread_progress_init(ThreadProgress *pro, int init_mode);
58
59/**
60 * Destroy a ThreadProgress. Can be called on a ThreadProgress that
61 * has never been initialized provided that the ThreadProgress struct
62 * has been initially zeroed. Must be called even if ff_thread_progress_init()
63 * failed.
64 */
65void ff_thread_progress_destroy(ThreadProgress *pro);
66
67/**
68 * Reset the ::ThreadProgress.progress counter; must only be called
69 * if the ThreadProgress is not in use in any way (e.g. no thread
70 * may wait on it via ff_thread_progress_await()).
71 */
72static inline void ff_thread_progress_reset(ThreadProgress *pro)
73{
74 atomic_init(&pro->progress, pro->init ? -1 : INT_MAX);
75}
76
77/**
78 * This function is a no-op in no-op mode; otherwise it notifies
79 * other threads that a certain level of progress has been reached.
80 * Later calls with lower values of progress have no effect.
81 */
82void ff_thread_progress_report(ThreadProgress *pro, int progress);
83
84/**
85 * This function is a no-op in no-op mode; otherwise it waits
86 * until other threads have reached a certain level of progress:
87 * This function will return after another thread has called
88 * ff_thread_progress_report() with the same or higher value for progress.
89 */
90void ff_thread_progress_await(const ThreadProgress *pro, int progress);
91
92#endif /* AVCODEC_THREADPROGRESS_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t