FFmpeg
sei.c
Go to the documentation of this file.
1 /*
2  * VVC Supplementary Enhancement Information messages
3  *
4  * copyright (c) 2024 Wu Jianhua <toqsxw@outlook.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "sei.h"
24 #include "dec.h"
25 #include "libavutil/refstruct.h"
26 
28 {
29  const VVCSPS *sps = fc->ps.sps;
30 
31  h->present = !s->fg_characteristics_cancel_flag;
32  if (h->present) {
33  h->model_id = s->fg_model_id;
34  h->separate_colour_description_present_flag = s->fg_separate_colour_description_present_flag;
35  if (h->separate_colour_description_present_flag) {
36  h->bit_depth_luma = s->fg_bit_depth_luma_minus8 + 8;
37  h->bit_depth_chroma = s->fg_bit_depth_chroma_minus8 + 8;
38  h->full_range = s->fg_full_range_flag;
39  h->color_primaries = s->fg_colour_primaries;
40  h->transfer_characteristics = s->fg_transfer_characteristics;
41  h->matrix_coeffs = s->fg_matrix_coeffs;
42  } else {
43  if (!sps) {
44  av_log(fc->log_ctx, AV_LOG_ERROR,
45  "No active SPS for film_grain_characteristics.\n");
46  return AVERROR_INVALIDDATA;
47  }
48  h->bit_depth_luma = sps->bit_depth;
49  h->bit_depth_chroma = sps->bit_depth;
50  h->full_range = sps->r->vui.vui_full_range_flag;
51  h->color_primaries = sps->r->vui.vui_colour_primaries;
52  h->transfer_characteristics = sps->r->vui.vui_transfer_characteristics;
53  h->matrix_coeffs = sps->r->vui.vui_matrix_coeffs ;
54  }
55 
56  h->blending_mode_id = s->fg_blending_mode_id;
57  h->log2_scale_factor = s->fg_log2_scale_factor;
58 
59  for (int c = 0; c < 3; c++) {
60  h->comp_model_present_flag[c] = s->fg_comp_model_present_flag[c];
61  if (h->comp_model_present_flag[c]) {
62  h->num_intensity_intervals[c] = s->fg_num_intensity_intervals_minus1[c] + 1;
63  h->num_model_values[c] = s->fg_num_model_values_minus1[c] + 1;
64 
65  if (h->num_model_values[c] > 6)
66  return AVERROR_INVALIDDATA;
67 
68  for (int i = 0; i < h->num_intensity_intervals[c]; i++) {
69  h->intensity_interval_lower_bound[c][i] = s->fg_intensity_interval_lower_bound[c][i];
70  h->intensity_interval_upper_bound[c][i] = s->fg_intensity_interval_upper_bound[c][i];
71  for (int j = 0; j < h->num_model_values[c]; j++)
72  h->comp_model_value[c][i][j] = s->fg_comp_model_value[c][i][j];
73  }
74  }
75  }
76 
77  h->persistence_flag = s->fg_characteristics_persistence_flag;
78  }
79 
80  return 0;
81 }
82 
84 {
85  h->present = 1;
86  h->hash_type = s->dph_sei_hash_type;
87  if (h->hash_type == 0)
88  memcpy(h->md5, s->dph_sei_picture_md5, sizeof(h->md5));
89  else if (h->hash_type == 1)
90  memcpy(h->crc, s->dph_sei_picture_crc, sizeof(h->crc));
91  else if (h->hash_type == 2)
92  memcpy(h->checksum, s->dph_sei_picture_checksum, sizeof(h->checksum));
93 
94  return 0;
95 }
96 
98 {
99  int degrees[] = { 0, 0x8000, 0x4000, 0xC000 };
100 
101  h->present = !s->display_orientation_cancel_flag;
102  if (h->present) {
103  if (s->display_orientation_transform_type > 7)
104  return AVERROR_INVALIDDATA;
105 
106  h->vflip = 0;
107  if (s->display_orientation_transform_type == 1 ||
108  s->display_orientation_transform_type == 3 ||
109  s->display_orientation_transform_type == 4 ||
110  s->display_orientation_transform_type == 6) {
111  h->hflip = 1;
112  } else {
113  h->hflip = 0;
114  }
115  h->anticlockwise_rotation = degrees[s->display_orientation_transform_type >> 1];
116  }
117 
118  return 0;
119 }
120 
122 {
123  h->present = 1;
124  h->max_content_light_level = s->max_content_light_level;
125  h->max_pic_average_light_level = s->max_pic_average_light_level;
126 
127  return 0;
128 }
129 
131 {
132  if (s->ffi_source_scan_type > 3)
133  return AVERROR_INVALIDDATA;
134 
135  h->present = 1;
136  if (s->ffi_field_pic_flag) {
137  if (s->ffi_bottom_field_flag)
138  h->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
139  else
140  h->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
141  } else {
142  h->display_elemental_periods = s->ffi_display_elemental_periods_minus1 + 1;
143  }
144 
145  h->source_scan_type = s->ffi_source_scan_type;
146  h->duplicate_flag = s->ffi_duplicate_flag;
147 
148  return 0;
149 }
150 
152 {
153  h->present = 1;
154  h->ambient_illuminance = s->ambient_illuminance;
155  h->ambient_light_x = s->ambient_light_x;
156  h->ambient_light_y = s->ambient_light_y;
157 
158  return 0;
159 }
160 
162 {
163  h->present = 1;
164 
165  for (int c = 0; c < 3; c++) {
166  h->display_primaries[c][0] = s->display_primaries_x[c];
167  h->display_primaries[c][1] = s->display_primaries_y[c];
168  }
169 
170  h->white_point[0] = s->white_point_x;
171  h->white_point[1] = s->white_point_y;
172 
173  h->max_luminance = s->max_display_mastering_luminance;
174  h->min_luminance = s->min_display_mastering_luminance;
175 
176  return 0;
177 }
178 
180 {
181  H2645SEI *c = &s->common;
182 
183  if (!sei)
184  return AVERROR_INVALIDDATA;
185 
186  for (int i = 0; i < sei->message_list.nb_messages; i++) {
187  int ret = 0;
188  SEIRawMessage *message = &sei->message_list.messages[i];
189  void *payload = message->payload;
190 
191  switch (message->payload_type) {
193  av_refstruct_unref(&c->film_grain_characteristics);
194  c->film_grain_characteristics = av_refstruct_allocz(sizeof(*c->film_grain_characteristics));
195  if (!c->film_grain_characteristics)
196  return AVERROR(ENOMEM);
197  ret = decode_film_grain_characteristics(c->film_grain_characteristics, payload, fc);
198  break;
199 
201  ret = decode_decoded_picture_hash(&s->picture_hash, payload);
202  break;
203 
205  ret = decode_display_orientation(&s->common.display_orientation, payload);
206  break;
207 
209  ret = decode_content_light_level_info(&s->common.content_light, payload);
210  break;
211 
213  ret = decode_frame_field_info(&s->frame_field_info, payload);
214  break;
215 
217  ret = decode_ambient_viewing_environment(&s->common.ambient_viewing_environment, payload);
218  break;
219 
221  ret = decode_mastering_display_colour_volume(&s->common.mastering_display, payload);
222  break;
223 
224  default:
225  av_log(fc->log_ctx, AV_LOG_DEBUG, "Skipped %s SEI %d\n",
226  sei->nal_unit_header.nal_unit_type == VVC_PREFIX_SEI_NUT ?
227  "PREFIX" : "SUFFIX", message->payload_type);
229  }
230 
231  if (ret == AVERROR(ENOMEM))
232  return ret;
233  if (ret < 0)
234  av_log(fc->log_ctx, AV_LOG_WARNING, "Failure to parse %s SEI %d: %s\n",
235  sei->nal_unit_header.nal_unit_type == VVC_PREFIX_SEI_NUT ?
236  "PREFIX" : "SUFFIX", message->payload_type, av_err2str(ret));
237  }
238 
239  return 0;
240 }
241 
243 {
244  dst->picture_hash.present = 0; // drop hash
245  dst->frame_field_info.present = 0; // drop field info
246  return ff_h2645_sei_ctx_replace(&dst->common, &src->common);
247 }
248 
250 {
251  ff_h2645_sei_reset(&s->common);
252  s->picture_hash.present = 0;
253  s->frame_field_info.present = 0;
254 }
VVCSPS
Definition: ps.h:58
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
H274SEIPictureHash
Definition: h274.h:69
SEIRawAmbientViewingEnvironment
Definition: cbs_sei.h:94
ff_vvc_sei_reset
void ff_vvc_sei_reset(VVCSEI *s)
Definition: sei.c:249
message
Definition: api-threadmessage-test.c:47
SEIRawFrameFieldInformation
Definition: cbs_sei.h:128
sei.h
SEIRawMessage
Definition: cbs_sei.h:140
H2645SEIMasteringDisplay
Definition: h2645_sei.h:114
decode_decoded_picture_hash
static int decode_decoded_picture_hash(H274SEIPictureHash *h, const SEIRawDecodedPictureHash *s)
Definition: sei.c:83
H2645SEIDisplayOrientation
Definition: h2645_sei.h:74
SEIRawContentLightLevelInfo
Definition: cbs_sei.h:85
SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT
@ SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT
Definition: sei.h:107
decode_film_grain_characteristics
static int decode_film_grain_characteristics(H2645SEIFilmGrainCharacteristics *h, const SEIRawFilmGrainCharacteristics *s, const VVCFrameContext *fc)
Definition: sei.c:27
refstruct.h
av_refstruct_allocz
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition: refstruct.h:105
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
H266RawSEI
Definition: cbs_h266.h:853
s
#define s(width, name)
Definition: cbs_vp9.c:198
fc
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:493
H2645SEIAmbientViewingEnvironment
Definition: h2645_sei.h:85
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
decode_ambient_viewing_environment
static int decode_ambient_viewing_environment(H2645SEIAmbientViewingEnvironment *h, const SEIRawAmbientViewingEnvironment *s)
Definition: sei.c:151
AV_PICTURE_STRUCTURE_BOTTOM_FIELD
@ AV_PICTURE_STRUCTURE_BOTTOM_FIELD
coded as bottom field
Definition: avcodec.h:2563
AV_PICTURE_STRUCTURE_TOP_FIELD
@ AV_PICTURE_STRUCTURE_TOP_FIELD
coded as top field
Definition: avcodec.h:2562
SEIRawMasteringDisplayColourVolume
Definition: cbs_sei.h:76
VVC_PREFIX_SEI_NUT
@ VVC_PREFIX_SEI_NUT
Definition: vvc.h:52
H2645SEI
Definition: h2645_sei.h:128
decode_content_light_level_info
static int decode_content_light_level_info(H2645SEIContentLight *h, const SEIRawContentLightLevelInfo *s)
Definition: sei.c:121
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
Definition: cbs_h264_syntax_template.c:858
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
VVCSEI
Definition: sei.h:36
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME
@ SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME
Definition: sei.h:96
ff_h2645_sei_reset
void ff_h2645_sei_reset(H2645SEI *s)
Definition: h2645_sei.c:919
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
ff_vvc_sei_decode
int ff_vvc_sei_decode(VVCSEI *s, const H266RawSEI *sei, const struct VVCFrameContext *fc)
Definition: sei.c:179
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
decode_mastering_display_colour_volume
static int decode_mastering_display_colour_volume(H2645SEIMasteringDisplay *h, const SEIRawMasteringDisplayColourVolume *s)
Definition: sei.c:161
FF_H2645_SEI_MESSAGE_UNHANDLED
@ FF_H2645_SEI_MESSAGE_UNHANDLED
Definition: h2645_sei.h:149
SEI_TYPE_DISPLAY_ORIENTATION
@ SEI_TYPE_DISPLAY_ORIENTATION
Definition: sei.h:77
ret
ret
Definition: filter_design.txt:187
ff_vvc_sei_replace
int ff_vvc_sei_replace(VVCSEI *dst, const VVCSEI *src)
Definition: sei.c:242
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
H274SEIFrameFieldInfo
Definition: h274.h:84
SEIRawDisplayOrientation
Definition: cbs_sei.h:121
SEIRawDecodedPictureHash
Definition: cbs_sei.h:66
decode_display_orientation
static int decode_display_orientation(H2645SEIDisplayOrientation *h, const SEIRawDisplayOrientation *s)
Definition: sei.c:97
SEI_TYPE_DECODED_PICTURE_HASH
@ SEI_TYPE_DECODED_PICTURE_HASH
Definition: sei.h:91
ff_h2645_sei_ctx_replace
int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
Definition: h2645_sei.c:522
decode_frame_field_info
static int decode_frame_field_info(H274SEIFrameFieldInfo *h, const SEIRawFrameFieldInformation *s)
Definition: sei.c:130
VVCFrameContext
Definition: dec.h:122
H2645SEIContentLight
Definition: h2645_sei.h:122
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
SEI_TYPE_FRAME_FIELD_INFO
@ SEI_TYPE_FRAME_FIELD_INFO
Definition: sei.h:126
SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
@ SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
Definition: sei.h:103
h
h
Definition: vp9dsp_template.c:2070
SEIRawFilmGrainCharacteristics
Definition: cbs_sei.h:100
SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
@ SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
Definition: sei.h:49
src
#define src
Definition: vp8dsp.c:248
dec.h
H2645SEIFilmGrainCharacteristics
Definition: h2645_sei.h:92