Avoids allocations and therefore error checks; also avoids indirections and allows to remove the boilerplate code for creating an object with a dedicated free function.
Showing the changes to libavcodec (4 of 4 files). Show all
Unified · Split
| @@ -29,6 +29,7 @@ · 28 unmodified lines | ||
| 29 | 29 | #include <poll.h> |
| 30 | 30 | #include "libavcodec/avcodec.h" |
| 31 | 31 | #include "libavutil/pixdesc.h" |
| 32 | +#include "refstruct.h" | |
| 32 | 33 | #include "v4l2_context.h" |
| 33 | 34 | #include "v4l2_buffers.h" |
| 34 | 35 | #include "v4l2_m2m.h" |
| @@ -229,7 +230,7 @@ static void v4l2_free_buffer(void *opaque, uint8_t *unused) · 194 unmodified lines | ||
| 229 | 230 | ff_v4l2_buffer_enqueue(avbuf); |
| 230 | 231 | } |
| 231 | 232 | |
| 232 | − av_buffer_unref(&avbuf->context_ref); | |
| 233 | + ff_refstruct_unref(&avbuf->context_ref); | |
| 233 | 234 | } |
| 234 | 235 | } |
| 235 | 236 | |
| @@ -240,9 +241,7 @@ static int v4l2_buf_increase_ref(V4L2Buffer *in) · 4 unmodified lines | ||
| 240 | 241 | if (in->context_ref) |
| 241 | 242 | atomic_fetch_add(&in->context_refcount, 1); |
| 242 | 243 | else { |
| 243 | − in->context_ref = av_buffer_ref(s->self_ref); | |
| 244 | − if (!in->context_ref) | |
| 245 | − return AVERROR(ENOMEM); | |
| 244 | + in->context_ref = ff_refstruct_ref(s->self_ref); | |
| 246 | 245 | |
| 247 | 246 | in->context_refcount = 1; |
| 248 | 247 | } |
| @@ -28,7 +28,6 @@ · 27 unmodified lines | ||
| 28 | 28 | #include <stddef.h> |
| 29 | 29 | #include <linux/videodev2.h> |
| 30 | 30 | |
| 31 | −#include "libavutil/buffer.h" | |
| 32 | 31 | #include "libavutil/frame.h" |
| 33 | 32 | #include "packet.h" |
| 34 | 33 | |
| @@ -46,8 +45,9 @@ typedef struct V4L2Buffer { · 11 unmodified lines | ||
| 46 | 45 | struct V4L2Context *context; |
| 47 | 46 | |
| 48 | 47 | /* This object is refcounted per-plane, so we need to keep track |
| 49 | − * of how many context-refs we are holding. */ | |
| 50 | − AVBufferRef *context_ref; | |
| 48 | + * of how many context-refs we are holding. | |
| 49 | + * This pointer is a RefStruct reference. */ | |
| 50 | + const struct V4L2m2mContext *context_ref; | |
| 51 | 51 | atomic_uint context_refcount; |
| 52 | 52 | |
| 53 | 53 | /* keep track of the mmap address and mmap length */ |
| @@ -32,6 +32,7 @@ · 31 unmodified lines | ||
| 32 | 32 | #include "libavutil/pixdesc.h" |
| 33 | 33 | #include "libavutil/imgutils.h" |
| 34 | 34 | #include "libavutil/pixfmt.h" |
| 35 | +#include "refstruct.h" | |
| 35 | 36 | #include "v4l2_context.h" |
| 36 | 37 | #include "v4l2_fmt.h" |
| 37 | 38 | #include "v4l2_m2m.h" |
| @@ -247,9 +248,9 @@ int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *s) · 209 unmodified lines | ||
| 247 | 248 | return 0; |
| 248 | 249 | } |
| 249 | 250 | |
| 250 | −static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) | |
| 251 | +static void v4l2_m2m_destroy_context(FFRefStructOpaque unused, void *context) | |
| 251 | 252 | { |
| 252 | − V4L2m2mContext *s = (V4L2m2mContext*)context; | |
| 253 | + V4L2m2mContext *s = context; | |
| 253 | 254 | |
| 254 | 255 | ff_v4l2_context_release(&s->capture); |
| 255 | 256 | sem_destroy(&s->refsync); |
| @@ -258,8 +259,6 @@ static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) · 2 unmodified lines | ||
| 258 | 259 | close(s->fd); |
| 259 | 260 | av_frame_free(&s->frame); |
| 260 | 261 | av_packet_unref(&s->buf_pkt); |
| 261 | − | |
| 262 | − av_free(s); | |
| 263 | 262 | } |
| 264 | 263 | |
| 265 | 264 | int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv) |
| @@ -283,7 +282,7 @@ int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv) · 17 unmodified lines | ||
| 283 | 282 | ff_v4l2_context_release(&s->output); |
| 284 | 283 | |
| 285 | 284 | s->self_ref = NULL; |
| 286 | − av_buffer_unref(&priv->context_ref); | |
| 285 | + ff_refstruct_unref(&priv->context); | |
| 287 | 286 | |
| 288 | 287 | return 0; |
| 289 | 288 | } |
| @@ -328,17 +327,11 @@ int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv) · 38 unmodified lines | ||
| 328 | 327 | |
| 329 | 328 | int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) |
| 330 | 329 | { |
| 331 | − *s = av_mallocz(sizeof(V4L2m2mContext)); | |
| 330 | + *s = ff_refstruct_alloc_ext(sizeof(**s), 0, NULL, | |
| 331 | + &v4l2_m2m_destroy_context); | |
| 332 | 332 | if (!*s) |
| 333 | 333 | return AVERROR(ENOMEM); |
| 334 | 334 | |
| 335 | − priv->context_ref = av_buffer_create((uint8_t *) *s, sizeof(V4L2m2mContext), | |
| 336 | − &v4l2_m2m_destroy_context, NULL, 0); | |
| 337 | − if (!priv->context_ref) { | |
| 338 | − av_freep(s); | |
| 339 | − return AVERROR(ENOMEM); | |
| 340 | − } | |
| 341 | − | |
| 342 | 335 | /* assign the context */ |
| 343 | 336 | priv->context = *s; |
| 344 | 337 | (*s)->priv = priv; |
| @@ -346,13 +339,13 @@ int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) · 1 unmodified line | ||
| 346 | 339 | /* populate it */ |
| 347 | 340 | priv->context->capture.num_buffers = priv->num_capture_buffers; |
| 348 | 341 | priv->context->output.num_buffers = priv->num_output_buffers; |
| 349 | − priv->context->self_ref = priv->context_ref; | |
| 342 | + priv->context->self_ref = priv->context; | |
| 350 | 343 | priv->context->fd = -1; |
| 351 | 344 | |
| 352 | 345 | priv->context->frame = av_frame_alloc(); |
| 353 | 346 | if (!priv->context->frame) { |
| 354 | − av_buffer_unref(&priv->context_ref); | |
| 355 | − *s = NULL; /* freed when unreferencing context_ref */ | |
| 347 | + ff_refstruct_unref(&priv->context); | |
| 348 | + *s = NULL; /* freed when unreferencing context */ | |
| 356 | 349 | return AVERROR(ENOMEM); |
| 357 | 350 | } |
| 358 | 351 | |
| @@ -62,7 +62,7 @@ typedef struct V4L2m2mContext { · 61 unmodified lines | ||
| 62 | 62 | AVFrame *frame; |
| 63 | 63 | |
| 64 | 64 | /* Reference to self; only valid while codec is active. */ |
| 65 | − AVBufferRef *self_ref; | |
| 65 | + struct V4L2m2mContext *self_ref; | |
| 66 | 66 | |
| 67 | 67 | /* reference back to V4L2m2mPriv */ |
| 68 | 68 | void *priv; |
| @@ -71,8 +71,7 @@ typedef struct V4L2m2mContext { · 2 unmodified lines | ||
| 71 | 71 | typedef struct V4L2m2mPriv { |
| 72 | 72 | AVClass *class; |
| 73 | 73 | |
| 74 | − V4L2m2mContext *context; | |
| 75 | − AVBufferRef *context_ref; | |
| 74 | + V4L2m2mContext *context; ///< RefStruct reference | |
| 76 | 75 | |
| 77 | 76 | int num_output_buffers; |
| 78 | 77 | int num_capture_buffers; |