FFmpeg  4.4.9
cbs_sei.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "cbs.h"
20 #include "cbs_internal.h"
21 #include "cbs_h264.h"
22 #include "cbs_h265.h"
23 #include "cbs_sei.h"
24 
25 static void cbs_free_user_data_registered(void *opaque, uint8_t *data)
26 {
29  av_free(udr);
30 }
31 
32 static void cbs_free_user_data_unregistered(void *opaque, uint8_t *data)
33 {
36  av_free(udu);
37 }
38 
41 {
42  void (*free_func)(void*, uint8_t*);
43 
44  av_assert0(message->payload == NULL &&
45  message->payload_ref == NULL);
46  message->payload_type = desc->type;
47 
49  free_func = &cbs_free_user_data_registered;
50  else if (desc->type == SEI_TYPE_USER_DATA_UNREGISTERED)
52  else
53  free_func = NULL;
54 
55  if (free_func) {
56  message->payload = av_mallocz(desc->size);
57  if (!message->payload)
58  return AVERROR(ENOMEM);
59  message->payload_ref =
60  av_buffer_create(message->payload, desc->size,
61  free_func, NULL, 0);
62  } else {
63  message->payload_ref = av_buffer_allocz(desc->size);
64  }
65  if (!message->payload_ref) {
66  av_freep(&message->payload);
67  return AVERROR(ENOMEM);
68  }
69  message->payload = message->payload_ref->data;
70 
71  return 0;
72 }
73 
75 {
76  void *ptr;
77  int old_count = list->nb_messages_allocated;
78 
79  av_assert0(list->nb_messages <= old_count);
80  if (list->nb_messages + 1 > old_count) {
81  int new_count = 2 * old_count + 1;
82 
83  ptr = av_realloc_array(list->messages,
84  new_count, sizeof(*list->messages));
85  if (!ptr)
86  return AVERROR(ENOMEM);
87 
88  list->messages = ptr;
89  list->nb_messages_allocated = new_count;
90  }
91  memset(&list->messages[list->nb_messages], 0, sizeof(*list->messages));
92  ++list->nb_messages;
93  return 0;
94 }
95 
97 {
98  for (int i = 0; i < list->nb_messages; i++) {
99  SEIRawMessage *message = &list->messages[i];
100  av_buffer_unref(&message->payload_ref);
101  av_buffer_unref(&message->extension_data_ref);
102  }
103  av_free(list->messages);
104 }
105 
108  int prefix,
109  CodedBitstreamUnit **sei_unit)
110 {
111  CodedBitstreamUnit *unit;
112  int sei_type, highest_vcl_type, err, i, position;
113 
114  switch (ctx->codec->codec_id) {
115  case AV_CODEC_ID_H264:
116  // (We can ignore auxiliary slices because we only have prefix
117  // SEI in H.264 and an auxiliary picture must always follow a
118  // primary picture.)
119  highest_vcl_type = H264_NAL_IDR_SLICE;
120  if (prefix)
121  sei_type = H264_NAL_SEI;
122  else
123  return AVERROR(EINVAL);
124  break;
125  case AV_CODEC_ID_H265:
126  highest_vcl_type = HEVC_NAL_RSV_VCL31;
127  if (prefix)
128  sei_type = HEVC_NAL_SEI_PREFIX;
129  else
130  sei_type = HEVC_NAL_SEI_SUFFIX;
131  break;
132  default:
133  return AVERROR(EINVAL);
134  }
135 
136  // Find an existing SEI NAL unit of the right type.
137  unit = NULL;
138  for (i = 0; i < au->nb_units; i++) {
139  if (au->units[i].type == sei_type) {
140  unit = &au->units[i];
141  break;
142  }
143  }
144 
145  if (unit) {
146  *sei_unit = unit;
147  return 0;
148  }
149 
150  // Need to add a new SEI NAL unit ...
151  if (prefix) {
152  // ... before the first VCL NAL unit.
153  for (i = 0; i < au->nb_units; i++) {
154  if (au->units[i].type < highest_vcl_type)
155  break;
156  }
157  position = i;
158  } else {
159  // ... after the last VCL NAL unit.
160  for (i = au->nb_units - 1; i >= 0; i--) {
161  if (au->units[i].type < highest_vcl_type)
162  break;
163  }
164  if (i < 0) {
165  // No VCL units; just put it at the end.
166  position = au->nb_units;
167  } else {
168  position = i + 1;
169  }
170  }
171 
172  err = ff_cbs_insert_unit_content(au, position, sei_type,
173  NULL, NULL);
174  if (err < 0)
175  return err;
176  unit = &au->units[position];
177  unit->type = sei_type;
178 
179  err = ff_cbs_alloc_unit_content2(ctx, unit);
180  if (err < 0)
181  return err;
182 
183  switch (ctx->codec->codec_id) {
184  case AV_CODEC_ID_H264:
185  {
186  H264RawSEI sei = {
187  .nal_unit_header = {
188  .nal_ref_idc = 0,
189  .nal_unit_type = sei_type,
190  },
191  };
192  memcpy(unit->content, &sei, sizeof(sei));
193  }
194  break;
195  case AV_CODEC_ID_H265:
196  {
197  H265RawSEI sei = {
198  .nal_unit_header = {
199  .nal_unit_type = sei_type,
200  .nuh_layer_id = 0,
201  .nuh_temporal_id_plus1 = 1,
202  },
203  };
204  memcpy(unit->content, &sei, sizeof(sei));
205  }
206  break;
207  default:
208  av_assert0(0);
209  }
210 
211  *sei_unit = unit;
212  return 0;
213 }
214 
216  CodedBitstreamUnit *unit,
217  SEIRawMessageList **list)
218 {
219  switch (ctx->codec->codec_id) {
220  case AV_CODEC_ID_H264:
221  {
222  H264RawSEI *sei = unit->content;
223  if (unit->type != H264_NAL_SEI)
224  return AVERROR(EINVAL);
225  *list = &sei->message_list;
226  }
227  break;
228  case AV_CODEC_ID_H265:
229  {
230  H265RawSEI *sei = unit->content;
231  if (unit->type != HEVC_NAL_SEI_PREFIX &&
232  unit->type != HEVC_NAL_SEI_SUFFIX)
233  return AVERROR(EINVAL);
234  *list = &sei->message_list;
235  }
236  break;
237  default:
238  return AVERROR(EINVAL);
239  }
240 
241  return 0;
242 }
243 
246  int prefix,
247  uint32_t payload_type,
248  void *payload_data,
249  AVBufferRef *payload_buf)
250 {
252  CodedBitstreamUnit *unit;
253  SEIRawMessageList *list;
255  AVBufferRef *payload_ref;
256  int err;
257 
258  desc = ff_cbs_sei_find_type(ctx, payload_type);
259  if (!desc)
260  return AVERROR(EINVAL);
261 
262  // Find an existing SEI unit or make a new one to add to.
263  err = cbs_sei_get_unit(ctx, au, prefix, &unit);
264  if (err < 0)
265  return err;
266 
267  // Find the message list inside the codec-dependent unit.
268  err = cbs_sei_get_message_list(ctx, unit, &list);
269  if (err < 0)
270  return err;
271 
272  // Add a new message to the message list.
273  err = ff_cbs_sei_list_add(list);
274  if (err < 0)
275  return err;
276 
277  if (payload_buf) {
278  payload_ref = av_buffer_ref(payload_buf);
279  if (!payload_ref)
280  return AVERROR(ENOMEM);
281  } else {
282  payload_ref = NULL;
283  }
284 
285  message = &list->messages[list->nb_messages - 1];
286 
287  message->payload_type = payload_type;
288  message->payload = payload_data;
289  message->payload_ref = payload_ref;
290 
291  return 0;
292 }
293 
296  uint32_t payload_type,
297  SEIRawMessage **iter)
298 {
299  int err, i, j, found;
300 
301  found = 0;
302  for (i = 0; i < au->nb_units; i++) {
303  CodedBitstreamUnit *unit = &au->units[i];
304  SEIRawMessageList *list;
305 
306  err = cbs_sei_get_message_list(ctx, unit, &list);
307  if (err < 0)
308  continue;
309 
310  for (j = 0; j < list->nb_messages; j++) {
311  SEIRawMessage *message = &list->messages[j];
312 
313  if (message->payload_type == payload_type) {
314  if (!*iter || found) {
315  *iter = message;
316  return 0;
317  }
318  if (message == *iter)
319  found = 1;
320  }
321  }
322  }
323 
324  return AVERROR(ENOENT);
325 }
326 
328  int position)
329 {
331 
332  av_assert0(0 <= position && position < list->nb_messages);
333 
334  message = &list->messages[position];
335  av_buffer_unref(&message->payload_ref);
336  av_buffer_unref(&message->extension_data_ref);
337 
338  --list->nb_messages;
339 
340  if (list->nb_messages > 0) {
341  memmove(list->messages + position,
342  list->messages + position + 1,
343  (list->nb_messages - position) * sizeof(*list->messages));
344  }
345 }
346 
349  uint32_t payload_type)
350 {
351  int err, i, j;
352 
353  for (i = 0; i < au->nb_units; i++) {
354  CodedBitstreamUnit *unit = &au->units[i];
355  SEIRawMessageList *list;
356 
357  err = cbs_sei_get_message_list(ctx, unit, &list);
358  if (err < 0)
359  continue;
360 
361  for (j = list->nb_messages - 1; j >= 0; j--) {
362  if (list->messages[j].payload_type == payload_type)
363  cbs_sei_delete_message(list, j);
364  }
365  }
366 }
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:737
int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:869
const SEIMessageTypeDescriptor * ff_cbs_sei_find_type(CodedBitstreamContext *ctx, int payload_type)
Find the type descriptor for the given payload type.
Definition: cbs_h2645.c:1653
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static void cbs_sei_delete_message(SEIRawMessageList *list, int position)
Definition: cbs_sei.c:327
static void cbs_free_user_data_registered(void *opaque, uint8_t *data)
Definition: cbs_sei.c:25
void ff_cbs_sei_free_message_list(SEIRawMessageList *list)
Free all SEI messages in a message list.
Definition: cbs_sei.c:96
int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message, const SEIMessageTypeDescriptor *desc)
Allocate a new payload for the given SEI message.
Definition: cbs_sei.c:39
static void cbs_free_user_data_unregistered(void *opaque, uint8_t *data)
Definition: cbs_sei.c:32
void ff_cbs_sei_delete_message_type(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, uint32_t payload_type)
Delete all messages with the given payload type from an access unit.
Definition: cbs_sei.c:347
static int cbs_sei_get_message_list(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, SEIRawMessageList **list)
Definition: cbs_sei.c:215
int ff_cbs_sei_list_add(SEIRawMessageList *list)
Allocate a new empty SEI message in a message list.
Definition: cbs_sei.c:74
int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, int prefix, uint32_t payload_type, void *payload_data, AVBufferRef *payload_buf)
Add an SEI message to an access unit.
Definition: cbs_sei.c:244
static int cbs_sei_get_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, int prefix, CodedBitstreamUnit **sei_unit)
Definition: cbs_sei.c:106
int ff_cbs_sei_find_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, uint32_t payload_type, SEIRawMessage **iter)
Iterate over messages with the given payload type in an access unit.
Definition: cbs_sei.c:294
static int FUNC() message(CodedBitstreamContext *ctx, RWContext *rw, SEIRawMessage *current)
#define AV_CODEC_ID_H265
Definition: codec_id.h:224
#define NULL
Definition: coverity.c:32
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:125
AVBufferRef * av_buffer_allocz(buffer_size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:29
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
#define AVERROR(e)
Definition: error.h:43
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
@ H264_NAL_SEI
Definition: h264.h:40
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
int i
Definition: input.c:407
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
@ HEVC_NAL_RSV_VCL31
Definition: hevc.h:60
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
const char * desc
Definition: libsvtav1.c:83
const char data[16]
Definition: mxf.c:142
typedef void(RENAME(mix_any_func_type))
@ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
Definition: sei.h:34
@ SEI_TYPE_USER_DATA_UNREGISTERED
Definition: sei.h:35
A reference to a data buffer.
Definition: buffer.h:84
Context structure for coded bitstream operations.
Definition: cbs.h:170
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:118
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:164
int nb_units
Number of units in this fragment.
Definition: cbs.h:149
Coded bitstream unit structure.
Definition: cbs.h:66
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:103
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:70
int nb_messages_allocated
Definition: cbs_sei.h:81
SEIRawMessage * messages
Definition: cbs_sei.h:79
uint32_t payload_type
Definition: cbs_sei.h:69
AVBufferRef * data_ref
Definition: cbs_sei.h:39
AVBufferRef * data_ref
Definition: cbs_sei.h:46
#define av_free(p)
#define av_freep(p)
AVFormatContext * ctx
Definition: movenc.c:48