+
+int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s)
+{
+ *s = av_mallocz(sizeof(V4L2m2mContext));
+ if (!*s)
+ return AVERROR(ENOMEM);
+
+ priv->context_ref = av_buffer_create((uint8_t *) *s, sizeof(V4L2m2mContext),
+ &v4l2_m2m_destroy_context, NULL, 0);
+ if (!priv->context_ref) {
+ av_freep(s);
+ return AVERROR(ENOMEM);
+ }
+
+ /* assign the context */
+ priv->context = *s;
+ (*s)->priv = priv;
+
+ /* populate it */
+ priv->context->capture.num_buffers = priv->num_capture_buffers;
+ priv->context->output.num_buffers = priv->num_output_buffers;
+ priv->context->self_ref = priv->context_ref;
+ priv->context->fd = -1;
+
+ priv->context->frame = av_frame_alloc();
+ if (!priv->context->frame) {
+ av_buffer_unref(&priv->context_ref);
+ *s = NULL; /* freed when unreferencing context_ref */
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}