#include "libavutil/mem.h"
/**
- * Context used by codec threads and stored in their AVCodecContext thread_opaque.
+ * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
*/
typedef struct PerThreadContext {
struct FrameThreadContext *parent;
AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
- uint8_t *buf; ///< backup storage for packet data when the input packet is not refcounted
- int allocated_buf_size; ///< Size allocated for buf
- AVFrame frame; ///< Output frame (for decoding) or input (for encoding).
+ AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
int result; ///< The result of the last codec decode/encode() call.
} PerThreadContext;
/**
- * Context stored in the client AVCodecContext thread_opaque.
+ * Context stored in the client AVCodecInternal thread_ctx.
*/
typedef struct FrameThreadContext {
PerThreadContext *threads; ///< The contexts for each thread.
ff_thread_finish_setup(avctx);
pthread_mutex_lock(&p->mutex);
- avcodec_get_frame_defaults(&p->frame);
+ av_frame_unref(p->frame);
p->got_frame = 0;
- p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
+ p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
- /* many decoders assign whole AVFrames, thus overwriting extended_data;
- * make sure it's set correctly */
- p->frame.extended_data = p->frame.data;
+ if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
+ if (avctx->internal->allocate_progress)
+ av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
+ "free the frame on failure. This is a bug, please report it.\n");
+ av_frame_unref(p->frame);
+ }
if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
dst->hwaccel = src->hwaccel;
dst->hwaccel_context = src->hwaccel_context;
+ dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
}
if (for_user) {
}
}
- av_buffer_unref(&p->avpkt.buf);
- p->avpkt = *avpkt;
- if (avpkt->buf)
- p->avpkt.buf = av_buffer_ref(avpkt->buf);
- else {
- av_fast_malloc(&p->buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
- p->avpkt.data = p->buf;
- memcpy(p->buf, avpkt->data, avpkt->size);
- memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
- }
+ av_packet_unref(&p->avpkt);
+ av_packet_ref(&p->avpkt, avpkt);
p->state = STATE_SETTING_UP;
pthread_cond_signal(&p->input_cond);
AVFrame *picture, int *got_picture_ptr,
AVPacket *avpkt)
{
- FrameThreadContext *fctx = avctx->thread_opaque;
+ FrameThreadContext *fctx = avctx->internal->thread_ctx;
int finished = fctx->next_finished;
PerThreadContext *p;
int err;
pthread_mutex_unlock(&p->progress_mutex);
}
- av_frame_move_ref(picture, &p->frame);
+ av_frame_move_ref(picture, p->frame);
*got_picture_ptr = p->got_frame;
picture->pkt_dts = p->avpkt.dts;
if (!progress || progress[field] >= n) return;
- p = f->owner->thread_opaque;
+ p = f->owner->internal->thread_ctx;
if (f->owner->debug&FF_DEBUG_THREADS)
av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
if (!progress || progress[field] >= n) return;
- p = f->owner->thread_opaque;
+ p = f->owner->internal->thread_ctx;
if (f->owner->debug&FF_DEBUG_THREADS)
av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
}
void ff_thread_finish_setup(AVCodecContext *avctx) {
- PerThreadContext *p = avctx->thread_opaque;
+ PerThreadContext *p = avctx->internal->thread_ctx;
if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
{
- FrameThreadContext *fctx = avctx->thread_opaque;
+ FrameThreadContext *fctx = avctx->internal->thread_ctx;
const AVCodec *codec = avctx->codec;
int i;
avctx->codec = NULL;
release_delayed_buffers(p);
- av_frame_unref(&p->frame);
+ av_frame_free(&p->frame);
}
for (i = 0; i < thread_count; i++) {
pthread_cond_destroy(&p->input_cond);
pthread_cond_destroy(&p->progress_cond);
pthread_cond_destroy(&p->output_cond);
- av_buffer_unref(&p->avpkt.buf);
- av_freep(&p->buf);
+ av_packet_unref(&p->avpkt);
av_freep(&p->released_buffers);
if (i) {
av_freep(&p->avctx->priv_data);
- av_freep(&p->avctx->internal);
av_freep(&p->avctx->slice_offset);
}
+ av_freep(&p->avctx->internal);
av_freep(&p->avctx);
}
av_freep(&fctx->threads);
pthread_mutex_destroy(&fctx->buffer_mutex);
- av_freep(&avctx->thread_opaque);
+ av_freep(&avctx->internal->thread_ctx);
}
int ff_frame_thread_init(AVCodecContext *avctx)
return 0;
}
- avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
+ avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
pthread_mutex_init(&fctx->buffer_mutex, NULL);
pthread_cond_init(&p->progress_cond, NULL);
pthread_cond_init(&p->output_cond, NULL);
+ p->frame = av_frame_alloc();
+ if (!p->frame) {
+ err = AVERROR(ENOMEM);
+ goto error;
+ }
+
p->parent = fctx;
p->avctx = copy;
}
*copy = *src;
- copy->thread_opaque = p;
- copy->pkt = &p->avpkt;
+
+ copy->internal = av_malloc(sizeof(AVCodecInternal));
+ if (!copy->internal) {
+ err = AVERROR(ENOMEM);
+ goto error;
+ }
+ *copy->internal = *src->internal;
+ copy->internal->thread_ctx = p;
+ copy->internal->pkt = &p->avpkt;
if (!i) {
src = copy;
goto error;
}
memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
- copy->internal = av_malloc(sizeof(AVCodecInternal));
- if (!copy->internal) {
- err = AVERROR(ENOMEM);
- goto error;
- }
- *copy->internal = *src->internal;
copy->internal->is_copy = 1;
if (codec->init_thread_copy)
void ff_thread_flush(AVCodecContext *avctx)
{
int i;
- FrameThreadContext *fctx = avctx->thread_opaque;
+ FrameThreadContext *fctx = avctx->internal->thread_ctx;
- if (!avctx->thread_opaque) return;
+ if (!fctx) return;
park_frame_worker_threads(fctx, avctx->thread_count);
if (fctx->prev_thread) {
if (fctx->prev_thread != &fctx->threads[0])
update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
- if (avctx->codec->flush)
- avctx->codec->flush(fctx->threads[0].avctx);
}
fctx->next_decoding = fctx->next_finished = 0;
PerThreadContext *p = &fctx->threads[i];
// Make sure decode flush calls with size=0 won't return old frames
p->got_frame = 0;
- av_frame_unref(&p->frame);
+ av_frame_unref(p->frame);
release_delayed_buffers(p);
+
+ if (avctx->codec->flush)
+ avctx->codec->flush(p->avctx);
}
}
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
{
- PerThreadContext *p = avctx->thread_opaque;
+ PerThreadContext *p = avctx->internal->thread_ctx;
int err;
f->owner = avctx;
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
{
- PerThreadContext *p = avctx->thread_opaque;
+ PerThreadContext *p = avctx->internal->thread_ctx;
FrameThreadContext *fctx;
AVFrame *dst, *tmp;
FF_DISABLE_DEPRECATION_WARNINGS
avctx->get_buffer2 == avcodec_default_get_buffer2);
FF_ENABLE_DEPRECATION_WARNINGS
- if (!f->f->data[0])
+ if (!f->f || !f->f->buf[0])
return;
if (avctx->debug & FF_DEBUG_BUFFERS)