aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/cbs_sei.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2023-09-13 01:01:16 +0200
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2023-10-07 22:35:14 +0200
commit3ba4f9c21e8bd78386738324d8767d74d75eec53 (patch)
treec0c9127edbbeaad333ffb4bcb2e5bc78b7b2c6a7 /libavcodec/cbs_sei.c
parent3e9b8d14e5d0df72765a1c9ca683c9d0f1d51e1a (diff)
downloadffmpeg-3ba4f9c21e8bd78386738324d8767d74d75eec53.tar.gz
avcodec/cbs_sei: Use RefStruct API for SEI messages
The SEI message code uses the AVBuffer API for its SEI messages and contained buffers (like the extension buffer for HEVC or the user data (un)registered payload buffers). Contrary to the ordinary CBS code (where some of these contained buffer references are actually references to the provided AVPacket's data so that one can't replace them with the RefStruct API), the CBS SEI code never uses outside buffers at all and can therefore be switched entirely to the RefStruct API. This avoids the overhead inherent in the AVBuffer API (namely the separate allocations etc.). Notice that the refcounting here is actually currently unused; the refcounts are always one (or zero in case of no refcounting); its only advantage is the flexibility provided by custom free functions. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavcodec/cbs_sei.c')
-rw-r--r--libavcodec/cbs_sei.c61
1 files changed, 25 insertions, 36 deletions
diff --git a/libavcodec/cbs_sei.c b/libavcodec/cbs_sei.c
index bd7f6f4938..e28c2f9093 100644
--- a/libavcodec/cbs_sei.c
+++ b/libavcodec/cbs_sei.c
@@ -22,25 +22,25 @@
#include "cbs_h265.h"
#include "cbs_h266.h"
#include "cbs_sei.h"
+#include "refstruct.h"
-static void cbs_free_user_data_registered(void *opaque, uint8_t *data)
+static void cbs_free_user_data_registered(FFRefStructOpaque unused, void *obj)
{
- SEIRawUserDataRegistered *udr = (SEIRawUserDataRegistered*)data;
- av_buffer_unref(&udr->data_ref);
- av_free(udr);
+ SEIRawUserDataRegistered *udr = obj;
+ ff_refstruct_unref(&udr->data);
}
-static void cbs_free_user_data_unregistered(void *opaque, uint8_t *data)
+static void cbs_free_user_data_unregistered(FFRefStructOpaque unused, void *obj)
{
- SEIRawUserDataUnregistered *udu = (SEIRawUserDataUnregistered*)data;
- av_buffer_unref(&udu->data_ref);
- av_free(udu);
+ SEIRawUserDataUnregistered *udu = obj;
+ ff_refstruct_unref(&udu->data);
}
int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message,
const SEIMessageTypeDescriptor *desc)
{
- void (*free_func)(void*, uint8_t*);
+ void (*free_func)(FFRefStructOpaque, void*);
+ unsigned flags = 0;
av_assert0(message->payload == NULL &&
message->payload_ref == NULL);
@@ -50,24 +50,16 @@ int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message,
free_func = &cbs_free_user_data_registered;
else if (desc->type == SEI_TYPE_USER_DATA_UNREGISTERED)
free_func = &cbs_free_user_data_unregistered;
- else
+ else {
free_func = NULL;
-
- if (free_func) {
- message->payload = av_mallocz(desc->size);
- if (!message->payload)
- return AVERROR(ENOMEM);
- message->payload_ref =
- av_buffer_create(message->payload, desc->size,
- free_func, NULL, 0);
- } else {
- message->payload_ref = av_buffer_alloc(desc->size);
+ flags = FF_REFSTRUCT_FLAG_NO_ZEROING;
}
- if (!message->payload_ref) {
- av_freep(&message->payload);
+
+ message->payload_ref = ff_refstruct_alloc_ext(desc->size, flags,
+ NULL, free_func);
+ if (!message->payload_ref)
return AVERROR(ENOMEM);
- }
- message->payload = message->payload_ref->data;
+ message->payload = message->payload_ref;
return 0;
}
@@ -101,8 +93,8 @@ void ff_cbs_sei_free_message_list(SEIRawMessageList *list)
{
for (int i = 0; i < list->nb_messages; i++) {
SEIRawMessage *message = &list->messages[i];
- av_buffer_unref(&message->payload_ref);
- av_buffer_unref(&message->extension_data_ref);
+ ff_refstruct_unref(&message->payload_ref);
+ ff_refstruct_unref(&message->extension_data);
}
av_free(list->messages);
}
@@ -278,13 +270,12 @@ int ff_cbs_sei_add_message(CodedBitstreamContext *ctx,
int prefix,
uint32_t payload_type,
void *payload_data,
- AVBufferRef *payload_buf)
+ void *payload_ref)
{
const SEIMessageTypeDescriptor *desc;
CodedBitstreamUnit *unit;
SEIRawMessageList *list;
SEIRawMessage *message;
- AVBufferRef *payload_ref;
int err;
desc = ff_cbs_sei_find_type(ctx, payload_type);
@@ -306,12 +297,10 @@ int ff_cbs_sei_add_message(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- if (payload_buf) {
- payload_ref = av_buffer_ref(payload_buf);
- if (!payload_ref)
- return AVERROR(ENOMEM);
- } else {
- payload_ref = NULL;
+ if (payload_ref) {
+ /* The following just increments payload_ref's refcount,
+ * so that payload_ref is now owned by us. */
+ payload_ref = ff_refstruct_ref(payload_ref);
}
message = &list->messages[list->nb_messages - 1];
@@ -364,8 +353,8 @@ static void cbs_sei_delete_message(SEIRawMessageList *list,
av_assert0(0 <= position && position < list->nb_messages);
message = &list->messages[position];
- av_buffer_unref(&message->payload_ref);
- av_buffer_unref(&message->extension_data_ref);
+ ff_refstruct_unref(&message->payload_ref);
+ ff_refstruct_unref(&message->extension_data);
--list->nb_messages;