History

avcodec/v4l2_(m2m|buffers): Use RefStruct API for context references

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.

Andreas Rheinhardt committed 3 years ago

Commit f18de5b1 parent fbd1b904 files · +17 −26Browse files

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t