Pass AVCodecContext to frame sinks

Frame consumers may need details about the frame format.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
This commit is contained in:
Romain Vimont 2023-02-25 16:19:58 +01:00
parent e22660d698
commit 619730edaf
4 changed files with 21 additions and 8 deletions

View file

@ -25,10 +25,10 @@ sc_decoder_close_sinks(struct sc_decoder *decoder) {
} }
static bool static bool
sc_decoder_open_sinks(struct sc_decoder *decoder) { sc_decoder_open_sinks(struct sc_decoder *decoder, const AVCodecContext *ctx) {
for (unsigned i = 0; i < decoder->sink_count; ++i) { for (unsigned i = 0; i < decoder->sink_count; ++i) {
struct sc_frame_sink *sink = decoder->sinks[i]; struct sc_frame_sink *sink = decoder->sinks[i];
if (!sink->ops->open(sink)) { if (!sink->ops->open(sink, ctx)) {
sc_decoder_close_first_sinks(decoder, i); sc_decoder_close_first_sinks(decoder, i);
return false; return false;
} }
@ -47,6 +47,11 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY; decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
if (codec->type == AVMEDIA_TYPE_VIDEO) {
// Hardcoded video properties
decoder->codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
}
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) { if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
LOGE("Decoder '%s': could not open codec", decoder->name); LOGE("Decoder '%s': could not open codec", decoder->name);
avcodec_free_context(&decoder->codec_ctx); avcodec_free_context(&decoder->codec_ctx);
@ -61,7 +66,7 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
return false; return false;
} }
if (!sc_decoder_open_sinks(decoder)) { if (!sc_decoder_open_sinks(decoder, decoder->codec_ctx)) {
av_frame_free(&decoder->frame); av_frame_free(&decoder->frame);
avcodec_close(decoder->codec_ctx); avcodec_close(decoder->codec_ctx);
avcodec_free_context(&decoder->codec_ctx); avcodec_free_context(&decoder->codec_ctx);

View file

@ -330,7 +330,11 @@ event_watcher(void *data, SDL_Event *event) {
#endif #endif
static bool static bool
sc_screen_frame_sink_open(struct sc_frame_sink *sink) { sc_screen_frame_sink_open(struct sc_frame_sink *sink,
const AVCodecContext *ctx) {
assert(ctx->pix_fmt == AV_PIX_FMT_YUV420P);
(void) ctx;
struct sc_screen *screen = DOWNCAST(sink); struct sc_screen *screen = DOWNCAST(sink);
(void) screen; (void) screen;
#ifndef NDEBUG #ifndef NDEBUG

View file

@ -5,6 +5,7 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <libavcodec/avcodec.h>
typedef struct AVFrame AVFrame; typedef struct AVFrame AVFrame;
@ -18,7 +19,7 @@ struct sc_frame_sink {
}; };
struct sc_frame_sink_ops { struct sc_frame_sink_ops {
bool (*open)(struct sc_frame_sink *sink); bool (*open)(struct sc_frame_sink *sink, const AVCodecContext *ctx);
void (*close)(struct sc_frame_sink *sink); void (*close)(struct sc_frame_sink *sink);
bool (*push)(struct sc_frame_sink *sink, const AVFrame *frame); bool (*push)(struct sc_frame_sink *sink, const AVFrame *frame);
}; };

View file

@ -156,7 +156,10 @@ sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
} }
static bool static bool
sc_v4l2_sink_open(struct sc_v4l2_sink *vs) { sc_v4l2_sink_open(struct sc_v4l2_sink *vs, const AVCodecContext *ctx) {
assert(ctx->pix_fmt == AV_PIX_FMT_YUV420P);
(void) ctx;
static const struct sc_video_buffer_callbacks cbs = { static const struct sc_video_buffer_callbacks cbs = {
.on_new_frame = sc_video_buffer_on_new_frame, .on_new_frame = sc_video_buffer_on_new_frame,
}; };
@ -336,9 +339,9 @@ sc_v4l2_sink_push(struct sc_v4l2_sink *vs, const AVFrame *frame) {
} }
static bool static bool
sc_v4l2_frame_sink_open(struct sc_frame_sink *sink) { sc_v4l2_frame_sink_open(struct sc_frame_sink *sink, const AVCodecContext *ctx) {
struct sc_v4l2_sink *vs = DOWNCAST(sink); struct sc_v4l2_sink *vs = DOWNCAST(sink);
return sc_v4l2_sink_open(vs); return sc_v4l2_sink_open(vs, ctx);
} }
static void static void