Make video buffer more generic

Video buffer is a tool between a frame producer and a frame consumer.

For now, it is used between a decoder and a renderer, but in the future
another instance might be used to swscale decoded frames.
This commit is contained in:
Romain Vimont 2021-02-19 21:16:57 +01:00
parent cb197ee3a2
commit 441d3fb119
4 changed files with 54 additions and 55 deletions

View file

@ -15,8 +15,8 @@
static void static void
push_frame(struct decoder *decoder) { push_frame(struct decoder *decoder) {
bool previous_frame_skipped; bool previous_frame_skipped;
video_buffer_offer_decoded_frame(decoder->video_buffer, video_buffer_producer_offer_frame(decoder->video_buffer,
&previous_frame_skipped); &previous_frame_skipped);
if (previous_frame_skipped) { if (previous_frame_skipped) {
fps_counter_add_skipped_frame(decoder->fps_counter); fps_counter_add_skipped_frame(decoder->fps_counter);
// the previous EVENT_NEW_FRAME will consume this frame // the previous EVENT_NEW_FRAME will consume this frame
@ -69,7 +69,7 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
return false; return false;
} }
ret = avcodec_receive_frame(decoder->codec_ctx, ret = avcodec_receive_frame(decoder->codec_ctx,
decoder->video_buffer->decoding_frame); decoder->video_buffer->producer_frame);
if (!ret) { if (!ret) {
// a frame was received // a frame was received
push_frame(decoder); push_frame(decoder);

View file

@ -451,7 +451,7 @@ update_texture(struct screen *screen, const AVFrame *frame) {
static bool static bool
screen_update_frame(struct screen *screen) { screen_update_frame(struct screen *screen) {
const AVFrame *frame = video_buffer_take_rendering_frame(screen->vb); const AVFrame *frame = video_buffer_consumer_take_frame(screen->vb);
fps_counter_add_rendered_frame(screen->fps_counter); fps_counter_add_rendered_frame(screen->fps_counter);

View file

@ -7,9 +7,9 @@
#include "util/log.h" #include "util/log.h"
bool bool
video_buffer_init(struct video_buffer *vb, bool render_expired_frames) { video_buffer_init(struct video_buffer *vb, bool wait_consumer) {
vb->decoding_frame = av_frame_alloc(); vb->producer_frame = av_frame_alloc();
if (!vb->decoding_frame) { if (!vb->producer_frame) {
goto error_0; goto error_0;
} }
@ -18,8 +18,8 @@ video_buffer_init(struct video_buffer *vb, bool render_expired_frames) {
goto error_1; goto error_1;
} }
vb->rendering_frame = av_frame_alloc(); vb->consumer_frame = av_frame_alloc();
if (!vb->rendering_frame) { if (!vb->consumer_frame) {
goto error_2; goto error_2;
} }
@ -28,73 +28,72 @@ video_buffer_init(struct video_buffer *vb, bool render_expired_frames) {
goto error_3; goto error_3;
} }
vb->render_expired_frames = render_expired_frames; vb->wait_consumer = wait_consumer;
if (render_expired_frames) { if (wait_consumer) {
ok = sc_cond_init(&vb->pending_frame_consumed_cond); ok = sc_cond_init(&vb->pending_frame_consumed_cond);
if (!ok) { if (!ok) {
sc_mutex_destroy(&vb->mutex); sc_mutex_destroy(&vb->mutex);
goto error_2; goto error_2;
} }
// interrupted is not used if expired frames are not rendered // interrupted is not used if wait_consumer is disabled since offering
// since offering a frame will never block // a frame will never block
vb->interrupted = false; vb->interrupted = false;
} }
// there is initially no rendering frame, so consider it has already been // there is initially no frame, so consider it has already been consumed
// consumed
vb->pending_frame_consumed = true; vb->pending_frame_consumed = true;
return true; return true;
error_3: error_3:
av_frame_free(&vb->rendering_frame); av_frame_free(&vb->consumer_frame);
error_2: error_2:
av_frame_free(&vb->pending_frame); av_frame_free(&vb->pending_frame);
error_1: error_1:
av_frame_free(&vb->decoding_frame); av_frame_free(&vb->producer_frame);
error_0: error_0:
return false; return false;
} }
void void
video_buffer_destroy(struct video_buffer *vb) { video_buffer_destroy(struct video_buffer *vb) {
if (vb->render_expired_frames) { if (vb->wait_consumer) {
sc_cond_destroy(&vb->pending_frame_consumed_cond); sc_cond_destroy(&vb->pending_frame_consumed_cond);
} }
sc_mutex_destroy(&vb->mutex); sc_mutex_destroy(&vb->mutex);
av_frame_free(&vb->rendering_frame); av_frame_free(&vb->consumer_frame);
av_frame_free(&vb->pending_frame); av_frame_free(&vb->pending_frame);
av_frame_free(&vb->decoding_frame); av_frame_free(&vb->producer_frame);
} }
static void static void
video_buffer_swap_decoding_frame(struct video_buffer *vb) { video_buffer_swap_producer_frame(struct video_buffer *vb) {
sc_mutex_assert(&vb->mutex); sc_mutex_assert(&vb->mutex);
AVFrame *tmp = vb->decoding_frame; AVFrame *tmp = vb->producer_frame;
vb->decoding_frame = vb->pending_frame; vb->producer_frame = vb->pending_frame;
vb->pending_frame = tmp; vb->pending_frame = tmp;
} }
static void static void
video_buffer_swap_rendering_frame(struct video_buffer *vb) { video_buffer_swap_consumer_frame(struct video_buffer *vb) {
sc_mutex_assert(&vb->mutex); sc_mutex_assert(&vb->mutex);
AVFrame *tmp = vb->rendering_frame; AVFrame *tmp = vb->consumer_frame;
vb->rendering_frame = vb->pending_frame; vb->consumer_frame = vb->pending_frame;
vb->pending_frame = tmp; vb->pending_frame = tmp;
} }
void void
video_buffer_offer_decoded_frame(struct video_buffer *vb, video_buffer_producer_offer_frame(struct video_buffer *vb,
bool *previous_frame_skipped) { bool *previous_frame_skipped) {
sc_mutex_lock(&vb->mutex); sc_mutex_lock(&vb->mutex);
if (vb->render_expired_frames) { if (vb->wait_consumer) {
// wait for the current (expired) frame to be consumed // wait for the current (expired) frame to be consumed
while (!vb->pending_frame_consumed && !vb->interrupted) { while (!vb->pending_frame_consumed && !vb->interrupted) {
sc_cond_wait(&vb->pending_frame_consumed_cond, &vb->mutex); sc_cond_wait(&vb->pending_frame_consumed_cond, &vb->mutex);
} }
} }
video_buffer_swap_decoding_frame(vb); video_buffer_swap_producer_frame(vb);
*previous_frame_skipped = !vb->pending_frame_consumed; *previous_frame_skipped = !vb->pending_frame_consumed;
vb->pending_frame_consumed = false; vb->pending_frame_consumed = false;
@ -103,26 +102,26 @@ video_buffer_offer_decoded_frame(struct video_buffer *vb,
} }
const AVFrame * const AVFrame *
video_buffer_take_rendering_frame(struct video_buffer *vb) { video_buffer_consumer_take_frame(struct video_buffer *vb) {
sc_mutex_lock(&vb->mutex); sc_mutex_lock(&vb->mutex);
assert(!vb->pending_frame_consumed); assert(!vb->pending_frame_consumed);
vb->pending_frame_consumed = true; vb->pending_frame_consumed = true;
video_buffer_swap_rendering_frame(vb); video_buffer_swap_consumer_frame(vb);
if (vb->render_expired_frames) { if (vb->wait_consumer) {
// unblock video_buffer_offer_decoded_frame() // unblock video_buffer_offer_decoded_frame()
sc_cond_signal(&vb->pending_frame_consumed_cond); sc_cond_signal(&vb->pending_frame_consumed_cond);
} }
sc_mutex_unlock(&vb->mutex); sc_mutex_unlock(&vb->mutex);
// rendering_frame is only written from this thread, no need to lock // consumer_frame is only written from this thread, no need to lock
return vb->rendering_frame; return vb->consumer_frame;
} }
void void
video_buffer_interrupt(struct video_buffer *vb) { video_buffer_interrupt(struct video_buffer *vb) {
if (vb->render_expired_frames) { if (vb->wait_consumer) {
sc_mutex_lock(&vb->mutex); sc_mutex_lock(&vb->mutex);
vb->interrupted = true; vb->interrupted = true;
sc_mutex_unlock(&vb->mutex); sc_mutex_unlock(&vb->mutex);

View file

@ -13,28 +13,28 @@ typedef struct AVFrame AVFrame;
/** /**
* There are 3 frames in memory: * There are 3 frames in memory:
* - one frame is held by the decoder (decoding_frame) * - one frame is held by the producer (producer_frame)
* - one frame is held by the renderer (rendering_frame) * - one frame is held by the consumer (consumer_frame)
* - one frame is shared between the decoder and the renderer (pending_frame) * - one frame is shared between the producer and the consumer (pending_frame)
* *
* The decoder decodes a packet into the decoding_frame (it may takes time). * The producer generates a frame into the producer_frame (it may takes time).
* *
* Once the frame is decoded, it calls video_buffer_offer_decoded_frame(), * Once the frame is produced, it calls video_buffer_producer_offer_frame(),
* which swaps the decoding and pending frames. * which swaps the producer and pending frames.
* *
* When the renderer is notified that a new frame is available, it calls * When the consumer is notified that a new frame is available, it calls
* video_buffer_take_rendering_frame() to retrieve it, which swaps the pending * video_buffer_consumer_take_frame() to retrieve it, which swaps the pending
* and rendering frames. The frame is valid until the next call, without * and consumer frames. The frame is valid until the next call, without
* blocking the decoder. * blocking the producer.
*/ */
struct video_buffer { struct video_buffer {
AVFrame *decoding_frame; AVFrame *producer_frame;
AVFrame *pending_frame; AVFrame *pending_frame;
AVFrame *rendering_frame; AVFrame *consumer_frame;
sc_mutex mutex; sc_mutex mutex;
bool render_expired_frames; bool wait_consumer; // never overwrite a pending frame if it is not consumed
bool interrupted; bool interrupted;
sc_cond pending_frame_consumed_cond; sc_cond pending_frame_consumed_cond;
@ -42,21 +42,21 @@ struct video_buffer {
}; };
bool bool
video_buffer_init(struct video_buffer *vb, bool render_expired_frames); video_buffer_init(struct video_buffer *vb, bool wait_consumer);
void void
video_buffer_destroy(struct video_buffer *vb); video_buffer_destroy(struct video_buffer *vb);
// set the decoded frame as ready for rendering // set the producer frame as ready for consuming
// the output flag is set to report whether the previous frame has been skipped // the output flag is set to report whether the previous frame has been skipped
void void
video_buffer_offer_decoded_frame(struct video_buffer *vb, video_buffer_producer_offer_frame(struct video_buffer *vb,
bool *previous_frame_skipped); bool *previous_frame_skipped);
// mark the rendering frame as consumed and return it // mark the consumer frame as consumed and return it
// the frame is valid until the next call to this function // the frame is valid until the next call to this function
const AVFrame * const AVFrame *
video_buffer_take_rendering_frame(struct video_buffer *vb); video_buffer_consumer_take_frame(struct video_buffer *vb);
// wake up and avoid any blocking call // wake up and avoid any blocking call
void void