Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions packaging/frame/ffmpeg/libavcodec/v4l2_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,52 @@ static int stuff_all_buffers(AVCodecContext * avctx, V4L2Context* ctx)
return rv;
}

// Adds the capture buffers create_buffers() left for after STREAMON, until
// the pool's total or, past the driver's minimum, until memory runs out.
static int add_late_buffers(AVCodecContext * const avctx, V4L2Context * const ctx)
{
V4L2m2mContext * const s = ctx_to_m2mctx(ctx);
int tries = 0;
int ret = 0;

while (ctx->num_buffers < ctx->late_target) {
struct v4l2_create_buffers create = {.count = 1, .memory = ctx->buf_mem, .format = ctx->format};
if (ioctl(s->fd, VIDIOC_CREATE_BUFS, &create) < 0) {
const int err = errno;
if (err == EINTR)
continue;
// The old working memory goes when the firmware answers its
// release, shortly after STREAMON.
if (err == ENOMEM && ++tries < (ctx->num_buffers < ctx->late_minimum ? 50 : 5)) {
usleep(4000);
continue;
}
if (err == ENOMEM && ctx->num_buffers >= ctx->late_minimum)
break;
ret = AVERROR(err);
av_log(avctx, AV_LOG_ERROR, "%s VIDIOC_CREATE_BUFS failed with %d of %d buffers: %s\n",
ctx->name, ctx->num_buffers, ctx->late_minimum, av_err2str(ret));
return ret;
}
if (create.count != 1 || create.index != (unsigned int)ctx->num_buffers)
return AVERROR(EINVAL);
ff_mutex_lock(&ctx->lock);
ret = ff_v4l2_buffer_initialize(&ctx->bufrefs[ctx->num_buffers], ctx->num_buffers, ctx, ctx->buf_mem);
if (!ret) {
V4L2Buffer * const buf = (V4L2Buffer *)ctx->bufrefs[ctx->num_buffers]->data;
++ctx->num_buffers;
ret = ff_v4l2_buffer_enqueue(buf);
}
ff_mutex_unlock(&ctx->lock);
if (ret < 0)
return ret;
}
av_log(avctx, AV_LOG_INFO, "%s: driver minimum %d buffers, %d in all, made after streaming started\n",
ctx->name, ctx->late_minimum, ctx->num_buffers);
ctx->late_target = 0;
return 0;
}

static int set_streamon(AVCodecContext * const avctx, V4L2Context*const ctx)
{
int type = ctx->type;
Expand All @@ -1055,6 +1101,17 @@ static int set_streamon(AVCodecContext * const avctx, V4L2Context*const ctx)
ret = AVERROR(errno);
av_log(avctx, AV_LOG_ERROR, "%s set status ON failed: err=%s\n", ctx->name,
av_err2str(ret));

// The upstream Iris driver allocates its reference frames and working
// memory here, after the pool, all within the decoder's address
// range. The pool only holds frames for display there, so the caller
// can retry with the driver's minimum and a frame to spare.
if (ret == AVERROR(ENOMEM) && !V4L2_TYPE_IS_OUTPUT(ctx->type) &&
ctx_to_m2mctx(ctx)->iris_reports_minimum) {
av_log(avctx, AV_LOG_WARNING, "%s: the driver refused a pool of %d buffers\n",
ctx->name, ctx->num_buffers);
((V4L2m2mPriv *)ctx_to_m2mctx(ctx)->priv)->capture_start_refused = 1;
}
return ret;
}

Expand All @@ -1063,6 +1120,8 @@ static int set_streamon(AVCodecContext * const avctx, V4L2Context*const ctx)
ctx->flag_last = 0;
ctx->actual_last = 0;
av_log(avctx, AV_LOG_DEBUG, "%s set status ON OK\n", ctx->name);
if (!V4L2_TYPE_IS_OUTPUT(ctx->type) && ctx->late_target > ctx->num_buffers)
ret = add_late_buffers(avctx, ctx);
return ret;
}

Expand Down Expand Up @@ -1374,7 +1433,37 @@ static int create_buffers(V4L2Context* const ctx, const unsigned int req_buffers
req.count = spare >= 0 ? 1 : req_buffers;
req.memory = mem;
req.type = ctx->type;
while ((ret = ioctl(s->fd, VIDIOC_REQBUFS, &req)) == -1) {
ctx->late_target = ctx->late_minimum = 0;
// The upstream Iris driver allocates its reference frames and working
// memory at capture STREAMON, the new set before it frees the old, all in
// the decoder's address range. A pool allocated first can leave them no
// room (8K HEVC Main 10 with a minimum of 8 had none even at 8), so start
// with one buffer and add the rest once streaming (VIDIOC_CREATE_BUFS
// takes an exact count, VIDIOC_REQBUFS the driver's minimum).
if (spare >= 0 && s->iris_reports_minimum && !s->iris_startup) {
struct v4l2_control minimum = {.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE};
struct v4l2_create_buffers create = {.count = 1, .memory = mem, .format = ctx->format};
if (ioctl(s->fd, VIDIOC_G_CTRL, &minimum) < 0 || minimum.value < 1) {
av_log(logger(ctx), AV_LOG_ERROR, "%s: no capture minimum from the driver\n", ctx->name);
return AVERROR(EINVAL);
}
while ((ret = ioctl(s->fd, VIDIOC_CREATE_BUFS, &create)) == -1) {
if (errno != EINTR) {
ret = AVERROR(errno);
av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_CREATE_BUFS failed: %s\n", ctx->name, av_err2str(ret));
return ret;
}
}
if (create.count != 1 || create.index != 0) {
ret = AVERROR(EINVAL);
goto fail_release;
}
ctx->late_minimum = minimum.value;
ctx->late_target = minimum.value + spare;
req.count = 1;
spare = 0;
}
else while ((ret = ioctl(s->fd, VIDIOC_REQBUFS, &req)) == -1) {
if (errno != EINTR) {
ret = AVERROR(errno);
av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_REQBUFS failed: %s\n", ctx->name, av_err2str(ret));
Expand Down Expand Up @@ -1404,7 +1493,7 @@ static int create_buffers(V4L2Context* const ctx, const unsigned int req_buffers
}

ctx->num_buffers = req.count;
ctx->bufrefs = av_mallocz(ctx->num_buffers * sizeof(*ctx->bufrefs));
ctx->bufrefs = av_mallocz(FFMAX(ctx->num_buffers, ctx->late_target) * sizeof(*ctx->bufrefs));
if (!ctx->bufrefs) {
av_log(logger(ctx), AV_LOG_ERROR, "%s malloc enomem\n", ctx->name);
ret = AVERROR(ENOMEM);
Expand Down
8 changes: 7 additions & 1 deletion packaging/frame/ffmpeg/libavcodec/v4l2_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ typedef struct V4L2Context {
AVBufferRef **bufrefs;

/**
* Readonly after init.
* Readonly after init, except while late_target adds capture buffers.
*/
int num_buffers;

/**
* Capture buffers made after STREAMON on the upstream Iris driver: the
* pool's total (bufrefs has room for it) and the driver's minimum.
*/
int late_target, late_minimum;

/**
* Buffer memory type V4L2_MEMORY_MMAP or V4L2_MEMORY_DMABUF
*/
Expand Down
28 changes: 27 additions & 1 deletion packaging/frame/ffmpeg/libavcodec/v4l2_m2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ static int v4l2_prepare_contexts(V4L2m2mContext *s, int probe)
v4l2_mplane_video(&cap) ? "mplane" :
v4l2_splane_video(&cap) ? "splane" : "unknown");

s->iris_reports_minimum = 0;
if (!probe && !strcmp((const char *)cap.driver, "iris_driver")) {
struct v4l2_queryctrl ctrl = {.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE};
s->iris_reports_minimum = !ioctl(s->fd, VIDIOC_QUERYCTRL, &ctrl);
av_log(log_ctx, AV_LOG_INFO, "Iris %s its capture minimum\n",
s->iris_reports_minimum ? "reports" : "doesn't report");
}

if (v4l2_mplane_video(&cap)) {
s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
Expand Down Expand Up @@ -255,6 +263,21 @@ static int v4l2_configure_contexts(V4L2m2mContext *s)
cfmt.fmt.pix.pixelformat),
av_get_pix_fmt_name(s->capture.av_pix_fmt) ?: "none");

// The stateful decoder's input size is provisional: the stream's headers
// give the real one at the source change. Iris sizes the working memory
// it allocates before then from it, and at capture start allocates the
// real size's before freeing that: an 8K input format left 8K HEVC Main 10
// no room to start. 4096x2560 still gets input buffers for 8K frames.
if (s->iris_reports_minimum && s->avctx && av_codec_is_decoder(s->avctx->codec) &&
V4L2_TYPE_IS_MULTIPLANAR(s->output.format.type) &&
(uint64_t)s->output.format.fmt.pix_mp.width * s->output.format.fmt.pix_mp.height >
4096 * 2560) {
av_log(log_ctx, AV_LOG_INFO, "Iris: provisional input size 4096x2560 for %ux%u\n",
s->output.format.fmt.pix_mp.width, s->output.format.fmt.pix_mp.height);
s->output.format.fmt.pix_mp.width = 4096;
s->output.format.fmt.pix_mp.height = 2560;
}

ret = ff_v4l2_context_set_format(&s->output);
if (ret) {
av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n");
Expand All @@ -268,7 +291,10 @@ static int v4l2_configure_contexts(V4L2m2mContext *s)
}

ret = ff_v4l2_context_init(&s->output);
if (ret == AVERROR(ENOMEM) && s->avctx && av_codec_is_decoder(s->avctx->codec)) {
// The staged start relies on the old driver keeping the capture format
// through input format changes; the upstream driver replaces it.
if (ret == AVERROR(ENOMEM) && s->avctx && av_codec_is_decoder(s->avctx->codec) &&
!s->iris_reports_minimum) {
V4L2m2mPriv *priv = s->priv;
struct v4l2_capability cap = {0};
const int iris = !ioctl(s->fd, VIDIOC_QUERYCAP, &cap) && !strcmp(cap.driver, "iris_driver");
Expand Down
6 changes: 6 additions & 0 deletions packaging/frame/ffmpeg/libavcodec/v4l2_m2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ typedef struct V4L2m2mContext {
enum IrisStartup iris_startup;
int64_t iris_startup_deadline;
int64_t iris_seek_deadline;
// Iris that reports its capture minimum (V4L2_CID_MIN_BUFFERS_FOR_CAPTURE),
// as the upstream driver in SteamOS 0.5.10 does: it decodes into private
// reference buffers and follows the stateful decoder sequence, so large
// streams start from a provisional input size, never the staged start.
int iris_reports_minimum;
// No usable SPS in the codec header: the first packet's decides.
int iris_sps_in_band;
// H.264's DPB size for the staged pool; its reorder_size stays the
Expand Down Expand Up @@ -201,6 +206,7 @@ typedef struct V4L2m2mPriv {
int num_output_buffers;
int num_capture_buffers;
int capture_spare;
int capture_start_refused;
int drain_seek_status;
int capture_ubwc;
int capture_10bit;
Expand Down
3 changes: 3 additions & 0 deletions packaging/frame/ffmpeg/libavcodec/v4l2_m2m_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,9 @@ static const AVOption options[] = {
OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 2, INT_MAX, FLAGS },
{ "capture_spare", "Capture buffers beyond the driver's minimum; replaces num_capture_buffers (-1: off)",
OFFSET(capture_spare), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 32, FLAGS },
{ "capture_start_refused", "The driver refused to start capture with the fixed num_capture_buffers pool",
OFFSET(capture_start_refused), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
FLAGS | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
{ "drain_seek_status", "Result of the last same-session seek (1: kept, negative: reopen needed)",
OFFSET(drain_seek_status), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, 1,
FLAGS | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
Expand Down
Loading
Loading