annotate ffmpeg/libavcodec/frame_thread_encoder.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) 2012 Michael Niedermayer <michaelni@gmx.at>
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 #include "frame_thread_encoder.h"
yading@10 22
yading@10 23 #include "libavutil/fifo.h"
yading@10 24 #include "libavutil/avassert.h"
yading@10 25 #include "libavutil/imgutils.h"
yading@10 26 #include "avcodec.h"
yading@10 27 #include "internal.h"
yading@10 28 #include "thread.h"
yading@10 29
yading@10 30 #if HAVE_PTHREADS
yading@10 31 #include <pthread.h>
yading@10 32 #elif HAVE_W32THREADS
yading@10 33 #include "w32pthreads.h"
yading@10 34 #elif HAVE_OS2THREADS
yading@10 35 #include "os2threads.h"
yading@10 36 #endif
yading@10 37
yading@10 38 #define MAX_THREADS 64
yading@10 39 #define BUFFER_SIZE (2*MAX_THREADS)
yading@10 40
yading@10 41 typedef struct{
yading@10 42 void *indata;
yading@10 43 void *outdata;
yading@10 44 int64_t return_code;
yading@10 45 unsigned index;
yading@10 46 } Task;
yading@10 47
yading@10 48 typedef struct{
yading@10 49 AVCodecContext *parent_avctx;
yading@10 50 pthread_mutex_t buffer_mutex;
yading@10 51
yading@10 52 AVFifoBuffer *task_fifo;
yading@10 53 pthread_mutex_t task_fifo_mutex;
yading@10 54 pthread_cond_t task_fifo_cond;
yading@10 55
yading@10 56 Task finished_tasks[BUFFER_SIZE];
yading@10 57 pthread_mutex_t finished_task_mutex;
yading@10 58 pthread_cond_t finished_task_cond;
yading@10 59
yading@10 60 unsigned task_index;
yading@10 61 unsigned finished_task_index;
yading@10 62
yading@10 63 pthread_t worker[MAX_THREADS];
yading@10 64 int exit;
yading@10 65 } ThreadContext;
yading@10 66
yading@10 67 static void * attribute_align_arg worker(void *v){
yading@10 68 AVCodecContext *avctx = v;
yading@10 69 ThreadContext *c = avctx->internal->frame_thread_encoder;
yading@10 70 AVPacket *pkt = NULL;
yading@10 71
yading@10 72 while(!c->exit){
yading@10 73 int got_packet, ret;
yading@10 74 AVFrame *frame;
yading@10 75 Task task;
yading@10 76
yading@10 77 if(!pkt) pkt= av_mallocz(sizeof(*pkt));
yading@10 78 if(!pkt) continue;
yading@10 79 av_init_packet(pkt);
yading@10 80
yading@10 81 pthread_mutex_lock(&c->task_fifo_mutex);
yading@10 82 while (av_fifo_size(c->task_fifo) <= 0 || c->exit) {
yading@10 83 if(c->exit){
yading@10 84 pthread_mutex_unlock(&c->task_fifo_mutex);
yading@10 85 goto end;
yading@10 86 }
yading@10 87 pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
yading@10 88 }
yading@10 89 av_fifo_generic_read(c->task_fifo, &task, sizeof(task), NULL);
yading@10 90 pthread_mutex_unlock(&c->task_fifo_mutex);
yading@10 91 frame = task.indata;
yading@10 92
yading@10 93 ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
yading@10 94 pthread_mutex_lock(&c->buffer_mutex);
yading@10 95 av_frame_unref(frame);
yading@10 96 pthread_mutex_unlock(&c->buffer_mutex);
yading@10 97 av_frame_free(&frame);
yading@10 98 if(got_packet) {
yading@10 99 av_dup_packet(pkt);
yading@10 100 } else {
yading@10 101 pkt->data = NULL;
yading@10 102 pkt->size = 0;
yading@10 103 }
yading@10 104 pthread_mutex_lock(&c->finished_task_mutex);
yading@10 105 c->finished_tasks[task.index].outdata = pkt; pkt = NULL;
yading@10 106 c->finished_tasks[task.index].return_code = ret;
yading@10 107 pthread_cond_signal(&c->finished_task_cond);
yading@10 108 pthread_mutex_unlock(&c->finished_task_mutex);
yading@10 109 }
yading@10 110 end:
yading@10 111 av_free(pkt);
yading@10 112 pthread_mutex_lock(&c->buffer_mutex);
yading@10 113 avcodec_close(avctx);
yading@10 114 pthread_mutex_unlock(&c->buffer_mutex);
yading@10 115 av_freep(&avctx);
yading@10 116 return NULL;
yading@10 117 }
yading@10 118
yading@10 119 int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
yading@10 120 int i=0;
yading@10 121 ThreadContext *c;
yading@10 122
yading@10 123
yading@10 124 if( !(avctx->thread_type & FF_THREAD_FRAME)
yading@10 125 || !(avctx->codec->capabilities & CODEC_CAP_INTRA_ONLY))
yading@10 126 return 0;
yading@10 127
yading@10 128 if(!avctx->thread_count) {
yading@10 129 avctx->thread_count = ff_get_logical_cpus(avctx);
yading@10 130 avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
yading@10 131 }
yading@10 132
yading@10 133 if(avctx->thread_count <= 1)
yading@10 134 return 0;
yading@10 135
yading@10 136 if(avctx->thread_count > MAX_THREADS)
yading@10 137 return AVERROR(EINVAL);
yading@10 138
yading@10 139 av_assert0(!avctx->internal->frame_thread_encoder);
yading@10 140 c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
yading@10 141 if(!c)
yading@10 142 return AVERROR(ENOMEM);
yading@10 143
yading@10 144 c->parent_avctx = avctx;
yading@10 145
yading@10 146 c->task_fifo = av_fifo_alloc(sizeof(Task) * BUFFER_SIZE);
yading@10 147 if(!c->task_fifo)
yading@10 148 goto fail;
yading@10 149
yading@10 150 pthread_mutex_init(&c->task_fifo_mutex, NULL);
yading@10 151 pthread_mutex_init(&c->finished_task_mutex, NULL);
yading@10 152 pthread_mutex_init(&c->buffer_mutex, NULL);
yading@10 153 pthread_cond_init(&c->task_fifo_cond, NULL);
yading@10 154 pthread_cond_init(&c->finished_task_cond, NULL);
yading@10 155
yading@10 156 for(i=0; i<avctx->thread_count ; i++){
yading@10 157 AVDictionary *tmp = NULL;
yading@10 158 void *tmpv;
yading@10 159 AVCodecContext *thread_avctx = avcodec_alloc_context3(avctx->codec);
yading@10 160 if(!thread_avctx)
yading@10 161 goto fail;
yading@10 162 tmpv = thread_avctx->priv_data;
yading@10 163 *thread_avctx = *avctx;
yading@10 164 thread_avctx->priv_data = tmpv;
yading@10 165 thread_avctx->internal = NULL;
yading@10 166 memcpy(thread_avctx->priv_data, avctx->priv_data, avctx->codec->priv_data_size);
yading@10 167 thread_avctx->thread_count = 1;
yading@10 168 thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
yading@10 169
yading@10 170 av_dict_copy(&tmp, options, 0);
yading@10 171 av_dict_set(&tmp, "threads", "1", 0);
yading@10 172 if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) {
yading@10 173 av_dict_free(&tmp);
yading@10 174 goto fail;
yading@10 175 }
yading@10 176 av_dict_free(&tmp);
yading@10 177 av_assert0(!thread_avctx->internal->frame_thread_encoder);
yading@10 178 thread_avctx->internal->frame_thread_encoder = c;
yading@10 179 if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) {
yading@10 180 goto fail;
yading@10 181 }
yading@10 182 }
yading@10 183
yading@10 184 avctx->active_thread_type = FF_THREAD_FRAME;
yading@10 185
yading@10 186 return 0;
yading@10 187 fail:
yading@10 188 avctx->thread_count = i;
yading@10 189 av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
yading@10 190 ff_frame_thread_encoder_free(avctx);
yading@10 191 return -1;
yading@10 192 }
yading@10 193
yading@10 194 void ff_frame_thread_encoder_free(AVCodecContext *avctx){
yading@10 195 int i;
yading@10 196 ThreadContext *c= avctx->internal->frame_thread_encoder;
yading@10 197
yading@10 198 pthread_mutex_lock(&c->task_fifo_mutex);
yading@10 199 c->exit = 1;
yading@10 200 pthread_cond_broadcast(&c->task_fifo_cond);
yading@10 201 pthread_mutex_unlock(&c->task_fifo_mutex);
yading@10 202
yading@10 203 for (i=0; i<avctx->thread_count; i++) {
yading@10 204 pthread_join(c->worker[i], NULL);
yading@10 205 }
yading@10 206
yading@10 207 pthread_mutex_destroy(&c->task_fifo_mutex);
yading@10 208 pthread_mutex_destroy(&c->finished_task_mutex);
yading@10 209 pthread_mutex_destroy(&c->buffer_mutex);
yading@10 210 pthread_cond_destroy(&c->task_fifo_cond);
yading@10 211 pthread_cond_destroy(&c->finished_task_cond);
yading@10 212 av_fifo_free(c->task_fifo); c->task_fifo = NULL;
yading@10 213 av_freep(&avctx->internal->frame_thread_encoder);
yading@10 214 }
yading@10 215
yading@10 216 int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr){
yading@10 217 ThreadContext *c = avctx->internal->frame_thread_encoder;
yading@10 218 Task task;
yading@10 219 int ret;
yading@10 220
yading@10 221 av_assert1(!*got_packet_ptr);
yading@10 222
yading@10 223 if(frame){
yading@10 224 if(!(avctx->flags & CODEC_FLAG_INPUT_PRESERVED)){
yading@10 225 AVFrame *new = av_frame_alloc();
yading@10 226 if(!new)
yading@10 227 return AVERROR(ENOMEM);
yading@10 228 pthread_mutex_lock(&c->buffer_mutex);
yading@10 229 ret = ff_get_buffer(c->parent_avctx, new, 0);
yading@10 230 pthread_mutex_unlock(&c->buffer_mutex);
yading@10 231 if(ret<0)
yading@10 232 return ret;
yading@10 233 new->pts = frame->pts;
yading@10 234 new->quality = frame->quality;
yading@10 235 new->pict_type = frame->pict_type;
yading@10 236 av_image_copy(new->data, new->linesize, (const uint8_t **)frame->data, frame->linesize,
yading@10 237 avctx->pix_fmt, avctx->width, avctx->height);
yading@10 238 frame = new;
yading@10 239 }
yading@10 240
yading@10 241 task.index = c->task_index;
yading@10 242 task.indata = (void*)frame;
yading@10 243 pthread_mutex_lock(&c->task_fifo_mutex);
yading@10 244 av_fifo_generic_write(c->task_fifo, &task, sizeof(task), NULL);
yading@10 245 pthread_cond_signal(&c->task_fifo_cond);
yading@10 246 pthread_mutex_unlock(&c->task_fifo_mutex);
yading@10 247
yading@10 248 c->task_index = (c->task_index+1) % BUFFER_SIZE;
yading@10 249
yading@10 250 if(!c->finished_tasks[c->finished_task_index].outdata && (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)
yading@10 251 return 0;
yading@10 252 }
yading@10 253
yading@10 254 if(c->task_index == c->finished_task_index)
yading@10 255 return 0;
yading@10 256
yading@10 257 pthread_mutex_lock(&c->finished_task_mutex);
yading@10 258 while (!c->finished_tasks[c->finished_task_index].outdata) {
yading@10 259 pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
yading@10 260 }
yading@10 261 task = c->finished_tasks[c->finished_task_index];
yading@10 262 *pkt = *(AVPacket*)(task.outdata);
yading@10 263 if(pkt->data)
yading@10 264 *got_packet_ptr = 1;
yading@10 265 av_freep(&c->finished_tasks[c->finished_task_index].outdata);
yading@10 266 c->finished_task_index = (c->finished_task_index+1) % BUFFER_SIZE;
yading@10 267 pthread_mutex_unlock(&c->finished_task_mutex);
yading@10 268
yading@10 269 return task.return_code;
yading@10 270 }