38 if (!
s->mapping || !
s->color)
46#define COPY(t, a, b, last) memcpy(a, b, offsetof(t, last) + sizeof((b)->last))
93 union { uint32_t u32;
float f32; } fpart;
112 union { uint32_t u32;
float f32; } fpart;
140#define VALIDATE(VAR, MIN, MAX) \
142 if (VAR < MIN || VAR > MAX) { \
143 av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: " \
144 #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR); \
145 ff_dovi_ctx_unref(s); \
146 return AVERROR_INVALIDDATA; \
187 for (
int i = 0;
i < 4;
i++)
200 const int denom = 32767;
207 int ext_block_length)
223 if (ext_block_length < 12)
226 if (ext_block_length < 13)
229 if (ext_block_length < 19)
231 for (
int i = 0;
i < 6;
i++)
233 if (ext_block_length < 25)
235 for (
int i = 0;
i < 6;
i++)
240 if (ext_block_length < 17)
252 if (ext_block_length < 21)
279 int compression,
int err_recognition)
281 int num_ext_blocks, ext_block_length, start_pos, parsed_bits, ret;
287 if (num_ext_blocks && !ext) {
293 while (num_ext_blocks--) {
305 "static extension block level %d\n",
level);
320 memset(dm, 0,
sizeof(*dm));
324 case 2: ret =
parse_ext_v2(
s, gb, dm, ext_block_length);
break;
335 if (parsed_bits > ext_block_length * 8)
337 skip_bits(gb, ext_block_length * 8 - parsed_bits);
351 uint8_t vdr_seq_info_present;
352 uint8_t vdr_dm_metadata_present;
353 uint8_t dm_compression = 0;
354 uint8_t use_prev_vdr_rpu;
357 uint8_t compression =
s->cfg.dv_profile ?
s->cfg.dv_md_compression : 0;
363 if (
s->cfg.dv_profile == 10 ) {
378 const unsigned header_magic = 0x01be6841u;
379 unsigned emdf_header, emdf_payload_size, emdf_protection;
383 VALIDATE(emdf_header, header_magic, header_magic);
385 VALIDATE(emdf_payload_size, 6, 512);
395 for (
int i = 0;
i < emdf_payload_size;
i++)
398 rpu_size = emdf_payload_size;
401 emdf_protection =
get_bits(gb, 5 + 12);
402 VALIDATE(emdf_protection, 0x400, 0x400);
417 "%"PRIu8
", ignoring\n", rpu_type);
429 if (vdr_seq_info_present) {
451 int ext_mapping_idc = el_bit_depth_minus8 >> 8;
452 el_bit_depth_minus8 = el_bit_depth_minus8 & 0xFF;
453 VALIDATE(bl_bit_depth_minus8, 0, 8);
454 VALIDATE(el_bit_depth_minus8, 0, 8);
456 VALIDATE(vdr_bit_depth_minus8, 0, 8);
479 if (dm_compression > 1) {
484 }
else if (dm_compression && !vdr_dm_metadata_present) {
486 "but no DM metadata present");
501 if (
profile < 8 && compression) {
503 "metadata compression.",
profile);
507 if (use_prev_vdr_rpu && !compression) {
509 "use_prev_vdr_rpu=1\n");
513 if (dm_compression && !compression) {
515 "dm_compression=%d\n", dm_compression);
520 if (use_prev_vdr_rpu) {
523 if (!
s->vdr[prev_vdr_rpu_id])
525 if (!
s->vdr[prev_vdr_rpu_id]) {
534 s->mapping =
s->vdr[prev_vdr_rpu_id];
539 if (!
s->vdr[vdr_rpu_id]) {
541 if (!
s->vdr[vdr_rpu_id]) {
547 s->mapping = mapping =
s->vdr[vdr_rpu_id];
552 for (
int c = 0;
c < 3;
c++) {
569 for (
int i = 0;
i < 2;
i++) {
589 for (
int c = 0;
c < 3;
c++) {
595 switch (mapping_idc) {
600 if (poly_order_minus1 == 0) {
602 if (linear_interp_flag) {
605 "linear interpolation");
615 int mmr_order_minus1 =
get_bits(gb, 2);
619 for (
int j = 0; j < curve->
mmr_order[
i]; j++) {
620 for (
int k = 0; k < 7; k++)
630 for (
int c = 0;
c < 3;
c++) {
644 if (vdr_dm_metadata_present) {
650 if (affected_dm_id != current_dm_id) {
655 "mismatch? %u != %u\n", affected_dm_id, current_dm_id);
669 color->dm_metadata_id = affected_dm_id;
671 if (!dm_compression) {
672 for (
int i = 0;
i < 9;
i++)
674 for (
int i = 0;
i < 3;
i++) {
675 int denom =
profile == 4 ? (1 << 30) : (1 << 28);
684 for (
int i = 0;
i < 9;
i++)
735 -1, rpu, rpu_size - 1));
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define i(width, name, range_min, range_max)
Public header for CRC hash function implementation.
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
#define AV_EF_EXPLODE
abort decoding on minor error detection
#define AV_EF_AGGRESSIVE
consider things that a sane encoder/muxer should not do as an error
#define AV_EF_CAREFUL
consider things that violate the spec, are fast to calculate and have not been seen in the wild as er...
const AVDOVIColorMetadata ff_dovi_color_default
int ff_dovi_guess_profile_hevc(const AVDOVIRpuDataHeader *hdr)
Internal helper function to guess the correct DV profile for HEVC.
void ff_dovi_ctx_unref(DOVIContext *s)
Completely reset a DOVIContext, preserving only logctx.
static int ff_dovi_rpu_extension_is_static(int level)
int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size, int err_recognition)
Parse the contents of a Dolby Vision RPU and update the parsed values in the DOVIContext struct.
#define VALIDATE(VAR, MIN, MAX)
static int parse_ext_v1(DOVIContext *s, GetBitContext *gb, AVDOVIDmData *dm)
static int parse_ext_v2(DOVIContext *s, GetBitContext *gb, AVDOVIDmData *dm, int ext_block_length)
#define COPY(t, a, b, last)
static unsigned get_variable_bits(GetBitContext *gb, int n)
static uint64_t get_ue_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
static int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
static int parse_ext_blocks(DOVIContext *s, GetBitContext *gb, int ver, int compression, int err_recognition)
static AVCIExy get_cie_xy(GetBitContext *gb)
int ff_dovi_get_metadata(DOVIContext *s, AVDOVIMetadata **out_metadata)
Get the decoded AVDOVIMetadata.
int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
Attach the decoded AVDOVIMetadata as side data to an AVFrame.
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int get_sbits(GetBitContext *s, int n)
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static const uint8_t * align_get_bits(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
static int get_se_golomb_long(GetBitContext *gb)
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AVFrameSideData * av_frame_new_side_data_from_buf(AVFrame *frame, enum AVFrameSideDataType type, AVBufferRef *buf)
Add a new side data to a frame from an existing AVBufferRef.
@ AV_FRAME_DATA_DOVI_METADATA
Parsed Dolby Vision metadata, suitable for passing to a software implementation.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static AVRational av_make_q(int num, int den)
Create an AVRational.
Memory handling functions.
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
AVWhitepointCoefficients wp
AVPrimaryCoefficients prim
Dolby Vision RPU data mapping parameters.
uint32_t num_x_partitions
uint8_t mapping_color_space
uint8_t mapping_chroma_format_idc
AVDOVIReshapingCurve curves[3]
enum AVDOVINLQMethod nlq_method_idc
uint32_t num_y_partitions
Dolby Vision metadata extension block.
uint8_t target_primary_index
AVColorPrimariesDesc target_display_primaries
uint8_t target_display_index
uint8_t reference_mode_flag
uint16_t trim_chroma_weight
uint16_t trim_saturation_gain
uint8_t target_display_index
uint16_t target_mid_contrast
uint8_t saturation_vector_field[6]
uint8_t hue_vector_field[6]
uint16_t trim_saturation_gain
uint16_t trim_chroma_weight
AVColorPrimariesDesc source_display_primaries
uint8_t source_primary_index
Coefficients of the non-linear inverse quantization.
uint64_t linear_deadzone_slope
uint64_t linear_deadzone_threshold
uint8_t poly_order[AV_DOVI_MAX_PIECES]
enum AVDOVIMappingMethod mapping_idc[AV_DOVI_MAX_PIECES]
int64_t mmr_constant[AV_DOVI_MAX_PIECES]
int64_t mmr_coef[AV_DOVI_MAX_PIECES][3][7]
int64_t poly_coef[AV_DOVI_MAX_PIECES][3]
uint16_t pivots[AV_DOVI_MAX_PIECES+1]
uint8_t mmr_order[AV_DOVI_MAX_PIECES]
Structure to hold side data for an AVFrame.
This structure describes decoded (raw) audio or video data.
AVDOVIDmData dm_static[7]
static extension blocks
AVDOVIDmData dm_dynamic[25]
dynamic extension blocks
#define avpriv_request_sample(...)