/src/ffmpeg/libavcodec/frame_thread_encoder.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2012 Michael Niedermayer <michaelni@gmx.at> |
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 <stdatomic.h> |
22 | | |
23 | | #include "frame_thread_encoder.h" |
24 | | |
25 | | #include "libavutil/avassert.h" |
26 | | #include "libavutil/cpu.h" |
27 | | #include "libavutil/mem.h" |
28 | | #include "libavutil/opt.h" |
29 | | #include "libavutil/thread.h" |
30 | | #include "avcodec.h" |
31 | | #include "avcodec_internal.h" |
32 | | #include "codec_par.h" |
33 | | #include "encode.h" |
34 | | #include "internal.h" |
35 | | #include "pthread_internal.h" |
36 | | |
37 | 0 | #define MAX_THREADS 64 |
38 | | /* There can be as many as MAX_THREADS + 1 outstanding tasks. |
39 | | * An additional + 1 is needed so that one can distinguish |
40 | | * the case of zero and MAX_THREADS + 1 outstanding tasks modulo |
41 | | * the number of buffers. */ |
42 | | #define BUFFER_SIZE (MAX_THREADS + 2) |
43 | | |
44 | | typedef struct{ |
45 | | AVFrame *indata; |
46 | | AVPacket *outdata; |
47 | | int return_code; |
48 | | int finished; |
49 | | int got_packet; |
50 | | } Task; |
51 | | |
52 | | typedef struct{ |
53 | | AVCodecContext *parent_avctx; |
54 | | |
55 | | pthread_mutex_t task_fifo_mutex; /* Used to guard (next_)task_index */ |
56 | | pthread_cond_t task_fifo_cond; |
57 | | |
58 | | unsigned pthread_init_cnt; |
59 | | unsigned max_tasks; |
60 | | Task tasks[BUFFER_SIZE]; |
61 | | pthread_mutex_t finished_task_mutex; /* Guards tasks[i].finished */ |
62 | | pthread_cond_t finished_task_cond; |
63 | | |
64 | | unsigned next_task_index; |
65 | | unsigned task_index; |
66 | | unsigned finished_task_index; |
67 | | |
68 | | pthread_t worker[MAX_THREADS]; |
69 | | atomic_int exit; |
70 | | } ThreadContext; |
71 | | |
72 | | #define OFF(member) offsetof(ThreadContext, member) |
73 | | DEFINE_OFFSET_ARRAY(ThreadContext, thread_ctx, pthread_init_cnt, |
74 | | (OFF(task_fifo_mutex), OFF(finished_task_mutex)), |
75 | | (OFF(task_fifo_cond), OFF(finished_task_cond))); |
76 | | #undef OFF |
77 | | |
78 | 0 | static void * attribute_align_arg worker(void *v){ |
79 | 0 | AVCodecContext *avctx = v; |
80 | 0 | ThreadContext *c = avctx->internal->frame_thread_encoder; |
81 | |
|
82 | 0 | while (!atomic_load(&c->exit)) { |
83 | 0 | int ret; |
84 | 0 | AVPacket *pkt; |
85 | 0 | AVFrame *frame; |
86 | 0 | Task *task; |
87 | 0 | unsigned task_index; |
88 | |
|
89 | 0 | pthread_mutex_lock(&c->task_fifo_mutex); |
90 | 0 | while (c->next_task_index == c->task_index || atomic_load(&c->exit)) { |
91 | 0 | if (atomic_load(&c->exit)) { |
92 | 0 | pthread_mutex_unlock(&c->task_fifo_mutex); |
93 | 0 | goto end; |
94 | 0 | } |
95 | 0 | pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex); |
96 | 0 | } |
97 | 0 | task_index = c->next_task_index; |
98 | 0 | c->next_task_index = (c->next_task_index + 1) % c->max_tasks; |
99 | 0 | pthread_mutex_unlock(&c->task_fifo_mutex); |
100 | | /* The main thread ensures that any two outstanding tasks have |
101 | | * different indices, ergo each worker thread owns its element |
102 | | * of c->tasks with the exception of finished, which is shared |
103 | | * with the main thread and guarded by finished_task_mutex. */ |
104 | 0 | task = &c->tasks[task_index]; |
105 | 0 | frame = task->indata; |
106 | 0 | pkt = task->outdata; |
107 | |
|
108 | 0 | ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet); |
109 | 0 | pthread_mutex_lock(&c->finished_task_mutex); |
110 | 0 | task->return_code = ret; |
111 | 0 | task->finished = 1; |
112 | 0 | pthread_cond_signal(&c->finished_task_cond); |
113 | 0 | pthread_mutex_unlock(&c->finished_task_mutex); |
114 | 0 | } |
115 | 0 | end: |
116 | 0 | avcodec_free_context(&avctx); |
117 | 0 | return NULL; |
118 | 0 | } |
119 | | |
120 | | av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx) |
121 | 65.1k | { |
122 | 65.1k | int i=0; |
123 | 65.1k | ThreadContext *c; |
124 | 65.1k | AVCodecContext *thread_avctx = NULL; |
125 | 65.1k | AVCodecParameters *par = NULL; |
126 | 65.1k | int ret; |
127 | | |
128 | 65.1k | if( !(avctx->thread_type & FF_THREAD_FRAME) |
129 | 65.1k | || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)) |
130 | 52.0k | return 0; |
131 | | |
132 | 13.0k | if( !avctx->thread_count |
133 | 0 | && avctx->codec_id == AV_CODEC_ID_MJPEG |
134 | 0 | && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) { |
135 | 0 | av_log(avctx, AV_LOG_DEBUG, |
136 | 0 | "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice " |
137 | 0 | "or a constant quantizer if you want to use multiple cpu cores\n"); |
138 | 0 | avctx->thread_count = 1; |
139 | 0 | } |
140 | 13.0k | if( avctx->thread_count > 1 |
141 | 0 | && avctx->codec_id == AV_CODEC_ID_MJPEG |
142 | 0 | && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) |
143 | 0 | av_log(avctx, AV_LOG_WARNING, |
144 | 0 | "MJPEG CBR encoding works badly with frame multi-threading, consider " |
145 | 0 | "using -threads 1, -thread_type slice or a constant quantizer.\n"); |
146 | | |
147 | 13.0k | if (avctx->codec_id == AV_CODEC_ID_HUFFYUV || |
148 | 12.6k | avctx->codec_id == AV_CODEC_ID_FFVHUFF) { |
149 | 907 | int warn = 0; |
150 | 907 | int64_t tmp; |
151 | | |
152 | 907 | if (avctx->flags & AV_CODEC_FLAG_PASS1) |
153 | 0 | warn = 1; |
154 | 907 | else if (av_opt_get_int(avctx->priv_data, "context", 0, &tmp) >= 0 && |
155 | 548 | tmp > 0) { |
156 | 0 | warn = av_opt_get_int(avctx->priv_data, "non_deterministic", 0, &tmp) < 0 |
157 | 0 | || !tmp; |
158 | 0 | } |
159 | | // huffyuv does not support these with multiple frame threads currently |
160 | 907 | if (warn) { |
161 | 0 | av_log(avctx, AV_LOG_WARNING, |
162 | 0 | "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n"); |
163 | 0 | avctx->thread_count = 1; |
164 | 0 | } |
165 | 907 | } |
166 | | |
167 | 13.0k | if(!avctx->thread_count) { |
168 | 0 | avctx->thread_count = av_cpu_count(); |
169 | 0 | avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS); |
170 | 0 | } |
171 | | |
172 | 13.0k | if(avctx->thread_count <= 1) |
173 | 13.0k | return 0; |
174 | | |
175 | 0 | if(avctx->thread_count > MAX_THREADS) |
176 | 0 | return AVERROR(EINVAL); |
177 | | |
178 | 0 | av_assert0(!avctx->internal->frame_thread_encoder); |
179 | 0 | c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext)); |
180 | 0 | if(!c) |
181 | 0 | return AVERROR(ENOMEM); |
182 | | |
183 | 0 | c->parent_avctx = avctx; |
184 | |
|
185 | 0 | ret = ff_pthread_init(c, thread_ctx_offsets); |
186 | 0 | if (ret < 0) |
187 | 0 | goto fail; |
188 | 0 | atomic_init(&c->exit, 0); |
189 | |
|
190 | 0 | c->max_tasks = avctx->thread_count + 2; |
191 | 0 | for (unsigned j = 0; j < c->max_tasks; j++) { |
192 | 0 | if (!(c->tasks[j].indata = av_frame_alloc()) || |
193 | 0 | !(c->tasks[j].outdata = av_packet_alloc())) { |
194 | 0 | ret = AVERROR(ENOMEM); |
195 | 0 | goto fail; |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | 0 | par = avcodec_parameters_alloc(); |
200 | 0 | if (!par) { |
201 | 0 | ret = AVERROR(ENOMEM); |
202 | 0 | goto fail; |
203 | 0 | } |
204 | | |
205 | 0 | ret = avcodec_parameters_from_context(par, avctx); |
206 | 0 | if (ret < 0) |
207 | 0 | goto fail; |
208 | | |
209 | 0 | for(i=0; i<avctx->thread_count ; i++){ |
210 | 0 | thread_avctx = avcodec_alloc_context3(avctx->codec); |
211 | 0 | if (!thread_avctx) { |
212 | 0 | ret = AVERROR(ENOMEM); |
213 | 0 | goto fail; |
214 | 0 | } |
215 | | |
216 | 0 | ret = avcodec_parameters_to_context(thread_avctx, par); |
217 | 0 | if (ret < 0) |
218 | 0 | goto fail; |
219 | | |
220 | 0 | ret = av_opt_copy(thread_avctx, avctx); |
221 | 0 | if (ret < 0) |
222 | 0 | goto fail; |
223 | 0 | if (avctx->codec->priv_class) { |
224 | 0 | ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data); |
225 | 0 | if (ret < 0) |
226 | 0 | goto fail; |
227 | 0 | } |
228 | 0 | thread_avctx->thread_count = 1; |
229 | 0 | thread_avctx->active_thread_type &= ~FF_THREAD_FRAME; |
230 | |
|
231 | 0 | #define DUP_MATRIX(m) \ |
232 | 0 | if (avctx->m) { \ |
233 | 0 | thread_avctx->m = av_memdup(avctx->m, 64 * sizeof(*avctx->m)); \ |
234 | 0 | if (!thread_avctx->m) { \ |
235 | 0 | ret = AVERROR(ENOMEM); \ |
236 | 0 | goto fail; \ |
237 | 0 | } \ |
238 | 0 | } |
239 | 0 | DUP_MATRIX(intra_matrix); |
240 | 0 | DUP_MATRIX(chroma_intra_matrix); |
241 | 0 | DUP_MATRIX(inter_matrix); |
242 | |
|
243 | 0 | #undef DUP_MATRIX |
244 | |
|
245 | 0 | thread_avctx->opaque = avctx->opaque; |
246 | 0 | thread_avctx->get_encode_buffer = avctx->get_encode_buffer; |
247 | 0 | thread_avctx->execute = avctx->execute; |
248 | 0 | thread_avctx->execute2 = avctx->execute2; |
249 | 0 | thread_avctx->stats_in = avctx->stats_in; |
250 | |
|
251 | 0 | if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0) |
252 | 0 | goto fail; |
253 | 0 | av_assert0(!thread_avctx->internal->frame_thread_encoder); |
254 | 0 | thread_avctx->internal->frame_thread_encoder = c; |
255 | 0 | if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) { |
256 | 0 | ret = AVERROR(ret); |
257 | 0 | goto fail; |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | 0 | avcodec_parameters_free(&par); |
262 | |
|
263 | 0 | avctx->active_thread_type = FF_THREAD_FRAME; |
264 | |
|
265 | 0 | return 0; |
266 | 0 | fail: |
267 | 0 | avcodec_parameters_free(&par); |
268 | 0 | avcodec_free_context(&thread_avctx); |
269 | 0 | avctx->thread_count = i; |
270 | 0 | av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n"); |
271 | 0 | ff_frame_thread_encoder_free(avctx); |
272 | 0 | return ret; |
273 | 0 | } |
274 | | |
275 | | av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx) |
276 | 0 | { |
277 | 0 | ThreadContext *c= avctx->internal->frame_thread_encoder; |
278 | | |
279 | | /* In case initializing the mutexes/condition variables failed, |
280 | | * they must not be used. In this case the thread_count is zero |
281 | | * as no thread has been initialized yet. */ |
282 | 0 | if (avctx->thread_count > 0) { |
283 | 0 | pthread_mutex_lock(&c->task_fifo_mutex); |
284 | 0 | atomic_store(&c->exit, 1); |
285 | 0 | pthread_cond_broadcast(&c->task_fifo_cond); |
286 | 0 | pthread_mutex_unlock(&c->task_fifo_mutex); |
287 | |
|
288 | 0 | for (int i = 0; i < avctx->thread_count; i++) |
289 | 0 | pthread_join(c->worker[i], NULL); |
290 | 0 | } |
291 | |
|
292 | 0 | for (unsigned i = 0; i < c->max_tasks; i++) { |
293 | 0 | av_frame_free(&c->tasks[i].indata); |
294 | 0 | av_packet_free(&c->tasks[i].outdata); |
295 | 0 | } |
296 | |
|
297 | 0 | ff_pthread_free(c, thread_ctx_offsets); |
298 | 0 | av_freep(&avctx->internal->frame_thread_encoder); |
299 | 0 | } |
300 | | |
301 | | int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
302 | | AVFrame *frame, int *got_packet_ptr) |
303 | 0 | { |
304 | 0 | ThreadContext *c = avctx->internal->frame_thread_encoder; |
305 | 0 | Task *outtask; |
306 | |
|
307 | 0 | av_assert1(!*got_packet_ptr); |
308 | |
|
309 | 0 | if(frame){ |
310 | 0 | av_frame_move_ref(c->tasks[c->task_index].indata, frame); |
311 | |
|
312 | 0 | pthread_mutex_lock(&c->task_fifo_mutex); |
313 | 0 | c->task_index = (c->task_index + 1) % c->max_tasks; |
314 | 0 | pthread_cond_signal(&c->task_fifo_cond); |
315 | 0 | pthread_mutex_unlock(&c->task_fifo_mutex); |
316 | 0 | } |
317 | |
|
318 | 0 | outtask = &c->tasks[c->finished_task_index]; |
319 | 0 | pthread_mutex_lock(&c->finished_task_mutex); |
320 | | /* The access to task_index in the following code is ok, |
321 | | * because it is only ever changed by the main thread. */ |
322 | 0 | if (c->task_index == c->finished_task_index || |
323 | 0 | (frame && !outtask->finished && |
324 | 0 | (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) { |
325 | 0 | pthread_mutex_unlock(&c->finished_task_mutex); |
326 | 0 | return 0; |
327 | 0 | } |
328 | 0 | while (!outtask->finished) { |
329 | 0 | pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex); |
330 | 0 | } |
331 | 0 | pthread_mutex_unlock(&c->finished_task_mutex); |
332 | | /* We now own outtask completely: No worker thread touches it any more, |
333 | | * because there is no outstanding task with this index. */ |
334 | 0 | outtask->finished = 0; |
335 | 0 | av_packet_move_ref(pkt, outtask->outdata); |
336 | 0 | *got_packet_ptr = outtask->got_packet; |
337 | 0 | c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks; |
338 | |
|
339 | 0 | return outtask->return_code; |
340 | 0 | } |