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

Showing the changes to libavcodec (4 of 4 files). Show all

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
2929 #include <poll.h>
3030 #include "libavcodec/avcodec.h"
3131 #include "libavutil/pixdesc.h"
32+#include "refstruct.h"
3233 #include "v4l2_context.h"
3334 #include "v4l2_buffers.h"
3435 #include "v4l2_m2m.h"
@@ -229,7 +230,7 @@ static void v4l2_free_buffer(void *opaque, uint8_t *unused) · 194 unmodified lines
229230 ff_v4l2_buffer_enqueue(avbuf);
230231 }
231232
232− av_buffer_unref(&avbuf->context_ref);
233+ ff_refstruct_unref(&avbuf->context_ref);
233234 }
234235 }
235236
@@ -240,9 +241,7 @@ static int v4l2_buf_increase_ref(V4L2Buffer *in) · 4 unmodified lines
240241 if (in->context_ref)
241242 atomic_fetch_add(&in->context_refcount, 1);
242243 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);
246245
247246 in->context_refcount = 1;
248247 }
libavcodec/v4l2_buffers.h+3−3
@@ -28,7 +28,6 @@ · 27 unmodified lines
2828 #include <stddef.h>
2929 #include <linux/videodev2.h>
3030
31−#include "libavutil/buffer.h"
3231 #include "libavutil/frame.h"
3332 #include "packet.h"
3433
@@ -46,8 +45,9 @@ typedef struct V4L2Buffer { · 11 unmodified lines
4645 struct V4L2Context *context;
4746
4847 /* 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;
5151 atomic_uint context_refcount;
5252
5353 /* keep track of the mmap address and mmap length */
libavcodec/v4l2_m2m.c+9−16
@@ -32,6 +32,7 @@ · 31 unmodified lines
3232 #include "libavutil/pixdesc.h"
3333 #include "libavutil/imgutils.h"
3434 #include "libavutil/pixfmt.h"
35+#include "refstruct.h"
3536 #include "v4l2_context.h"
3637 #include "v4l2_fmt.h"
3738 #include "v4l2_m2m.h"
@@ -247,9 +248,9 @@ int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *s) · 209 unmodified lines
247248 return 0;
248249 }
249250
250−static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context)
251+static void v4l2_m2m_destroy_context(FFRefStructOpaque unused, void *context)
251252 {
252− V4L2m2mContext *s = (V4L2m2mContext*)context;
253+ V4L2m2mContext *s = context;
253254
254255 ff_v4l2_context_release(&s->capture);
255256 sem_destroy(&s->refsync);
@@ -258,8 +259,6 @@ static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) · 2 unmodified lines
258259 close(s->fd);
259260 av_frame_free(&s->frame);
260261 av_packet_unref(&s->buf_pkt);
261−
262− av_free(s);
263262 }
264263
265264 int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv)
@@ -283,7 +282,7 @@ int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv) · 17 unmodified lines
283282 ff_v4l2_context_release(&s->output);
284283
285284 s->self_ref = NULL;
286− av_buffer_unref(&priv->context_ref);
285+ ff_refstruct_unref(&priv->context);
287286
288287 return 0;
289288 }
@@ -328,17 +327,11 @@ int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv) · 38 unmodified lines
328327
329328 int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s)
330329 {
331− *s = av_mallocz(sizeof(V4L2m2mContext));
330+ *s = ff_refstruct_alloc_ext(sizeof(**s), 0, NULL,
331+ &v4l2_m2m_destroy_context);
332332 if (!*s)
333333 return AVERROR(ENOMEM);
334334
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−
342335 /* assign the context */
343336 priv->context = *s;
344337 (*s)->priv = priv;
@@ -346,13 +339,13 @@ int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) · 1 unmodified line
346339 /* populate it */
347340 priv->context->capture.num_buffers = priv->num_capture_buffers;
348341 priv->context->output.num_buffers = priv->num_output_buffers;
349− priv->context->self_ref = priv->context_ref;
342+ priv->context->self_ref = priv->context;
350343 priv->context->fd = -1;
351344
352345 priv->context->frame = av_frame_alloc();
353346 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 */
356349 return AVERROR(ENOMEM);
357350 }
358351
libavcodec/v4l2_m2m.h+2−3
@@ -62,7 +62,7 @@ typedef struct V4L2m2mContext { · 61 unmodified lines
6262 AVFrame *frame;
6363
6464 /* Reference to self; only valid while codec is active. */
65− AVBufferRef *self_ref;
65+ struct V4L2m2mContext *self_ref;
6666
6767 /* reference back to V4L2m2mPriv */
6868 void *priv;
@@ -71,8 +71,7 @@ typedef struct V4L2m2mContext { · 2 unmodified lines
7171 typedef struct V4L2m2mPriv {
7272 AVClass *class;
7373
74− V4L2m2mContext *context;
75− AVBufferRef *context_ref;
74+ V4L2m2mContext *context; ///< RefStruct reference
7675
7776 int num_output_buffers;
7877 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