FFmpeg
dovi_rpu.h
Go to the documentation of this file.
1 /*
2  * Dolby Vision RPU decoder
3  *
4  * Copyright (C) 2021 Jan Ekström
5  * Copyright (C) 2021 Niklas Haas
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #ifndef AVCODEC_DOVI_RPU_H
25 #define AVCODEC_DOVI_RPU_H
26 
27 #include "libavutil/dovi_meta.h"
28 #include "libavutil/frame.h"
29 
30 #include "avcodec.h"
31 #include "codec_par.h"
32 
33 #define DOVI_MAX_DM_ID 15
34 
35 typedef struct DOVIExt {
36  AVDOVIDmData dm_static[7]; ///< static extension blocks
37  AVDOVIDmData dm_dynamic[25]; ///< dynamic extension blocks
40 } DOVIExt;
41 
42 typedef struct DOVIContext {
43  void *logctx;
44 
45  /**
46  * Enable tri-state. For encoding only. FF_DOVI_AUTOMATIC enables Dolby
47  * Vision only if avctx->decoded_side_data contains an AVDOVIMetadata.
48  */
49 #define FF_DOVI_AUTOMATIC -1
50  int enable;
51 
52  /**
53  * Currently active dolby vision configuration, or {0} for none.
54  * Set by the user when decoding. Generated by ff_dovi_configure()
55  * when encoding.
56  *
57  * Note: sizeof(cfg) is not part of the libavutil ABI, so users should
58  * never pass &cfg to any other library calls. This is included merely as
59  * a way to look up the values of fields known at compile time.
60  */
62 
63  /**
64  * Currently active RPU data header, updates on every ff_dovi_rpu_parse()
65  * or ff_dovi_rpu_generate().
66  */
68 
69  /**
70  * Currently active data mappings, or NULL. Points into memory owned by the
71  * corresponding rpu/vdr_ref, which becomes invalid on the next call to
72  * ff_dovi_rpu_parse() or ff_dovi_rpu_generate().
73  */
76 
77  /**
78  * Currently active extension blocks, updates on every ff_dovi_rpu_parse()
79  * or ff_dovi_rpu_generate().
80  */
81  DOVIExt *ext_blocks; ///< RefStruct, or NULL if no extension blocks
82 
83  /**
84  * Private fields internal to dovi_rpu.c
85  */
86  AVDOVIColorMetadata *dm; ///< RefStruct
87  AVDOVIDataMapping *vdr[DOVI_MAX_DM_ID+1]; ///< RefStruct references
88  uint8_t *rpu_buf; ///< temporary buffer
89  unsigned rpu_buf_sz;
90 
91 } DOVIContext;
92 
94 
95 /**
96  * Completely reset a DOVIContext, preserving only logctx.
97  */
99 
100 /**
101  * Partially reset the internal state. Resets per-frame state, but preserves
102  * the stream-wide configuration record.
103  */
105 
106 /**
107  * Parse the contents of a Dolby Vision RPU and update the parsed values in the
108  * DOVIContext struct. This function should receive the decoded unit payload,
109  * without any T.35 or NAL unit headers.
110  *
111  * Returns 0 or an error code.
112  *
113  * Note: `DOVIContext.cfg` should be initialized before calling into this
114  * function. If not done, the profile will be guessed according to HEVC
115  * semantics.
116  */
117 int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size,
118  int err_recognition);
119 
120 /**
121  * Get the decoded AVDOVIMetadata. Ownership passes to the caller.
122  *
123  * Returns the size of *out_metadata, a negative error code, or 0 if no
124  * metadata is available to return.
125  */
126 int ff_dovi_get_metadata(DOVIContext *s, AVDOVIMetadata **out_metadata);
127 
128 /**
129  * Attach the decoded AVDOVIMetadata as side data to an AVFrame.
130  * Returns 0 or a negative error code.
131  */
133 
134 /**
135  * Configure the encoder for Dolby Vision encoding. Generates a configuration
136  * record in s->cfg, and attaches it to avctx->coded_side_data. Sets the correct
137  * profile and compatibility ID based on the tagged AVCodecParameters colorspace
138  * metadata, and the correct level based on the resolution and tagged framerate.
139  *
140  * `metadata` should point to the first frame's RPU, if available. If absent,
141  * auto-detection will be performed, but this can sometimes lead to inaccurate
142  * results (in particular for HEVC streams with enhancement layers).
143  *
144  * Returns 0 or a negative error code.
145  */
147  const AVDOVIMetadata *metadata,
148  enum AVDOVICompression compression,
149  int strict_std_compliance);
150 
151 /**
152  * Helper wrapper around `ff_dovi_configure_ext` which infers the codec
153  * parameters from an AVCodecContext.
154  */
156 
157 enum {
158  FF_DOVI_WRAP_NAL = 1 << 0, ///< wrap inside NAL RBSP
159  FF_DOVI_WRAP_T35 = 1 << 1, ///< wrap inside T.35+EMDF
160  FF_DOVI_COMPRESS_RPU = 1 << 2, ///< enable compression for this RPU
161 };
162 
163 /**
164  * Synthesize a Dolby Vision RPU reflecting the current state. By default, the
165  * RPU is not encapsulated (see `flags` for more options). Note that this
166  * assumes all previous calls to `ff_dovi_rpu_generate` have been
167  * appropriately signalled, i.e. it will not re-send already transmitted
168  * redundant data.
169  *
170  * Mutates the internal state of DOVIContext to reflect the change.
171  * Returns 0 or a negative error code.
172  */
173 int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
174  int flags, uint8_t **out_rpu, int *out_size);
175 
176 
177 /***************************************************
178  * The following section is for internal use only. *
179  ***************************************************/
180 
181 enum {
182  RPU_COEFF_FIXED = 0,
183  RPU_COEFF_FLOAT = 1,
184 };
185 
186 /**
187  * Internal helper function to guess the correct DV profile for HEVC.
188  *
189  * Returns the profile number or 0 if unknown.
190  */
192 
193 /* Default values for AVDOVIColorMetadata */
195 
197 {
198  switch (level) {
199  case 6:
200  case 10:
201  case 32: /* reserved as static by spec */
202  case 254:
203  case 255:
204  return 1;
205  default:
206  return 0;
207  }
208 }
209 
210 #endif /* AVCODEC_DOVI_RPU_H */
DOVIContext::cfg
AVDOVIDecoderConfigurationRecord cfg
Currently active dolby vision configuration, or {0} for none.
Definition: dovi_rpu.h:61
level
uint8_t level
Definition: svq3.c:205
ff_dovi_ctx_replace
void ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
Definition: dovi_rpu.c:58
DOVI_MAX_DM_ID
#define DOVI_MAX_DM_ID
Definition: dovi_rpu.h:33
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
ff_dovi_rpu_parse
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.
Definition: dovi_rpudec.c:346
out_size
int out_size
Definition: movenc.c:56
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
ff_dovi_color_default
const AVDOVIColorMetadata ff_dovi_color_default
Definition: dovi_rpu.c:93
AVDOVIRpuDataHeader
Dolby Vision RPU data header.
Definition: dovi_meta.h:87
DOVIExt::num_dynamic
int num_dynamic
Definition: dovi_rpu.h:39
DOVIContext
Definition: dovi_rpu.h:42
FF_DOVI_WRAP_T35
@ FF_DOVI_WRAP_T35
wrap inside T.35+EMDF
Definition: dovi_rpu.h:159
ff_dovi_configure
int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx)
Helper wrapper around ff_dovi_configure_ext which infers the codec parameters from an AVCodecContext.
Definition: dovi_rpuenc.c:241
FF_DOVI_COMPRESS_RPU
@ FF_DOVI_COMPRESS_RPU
enable compression for this RPU
Definition: dovi_rpu.h:160
AVDOVIDmData
Dolby Vision metadata extension block.
Definition: dovi_meta.h:310
RPU_COEFF_FIXED
@ RPU_COEFF_FIXED
Definition: dovi_rpu.h:184
AVDOVIMetadata
Combined struct representing a combination of header, mapping and color metadata, for attaching to fr...
Definition: dovi_meta.h:337
s
#define s(width, name)
Definition: cbs_vp9.c:198
DOVIContext::mapping
const AVDOVIDataMapping * mapping
Currently active data mappings, or NULL.
Definition: dovi_rpu.h:74
DOVIExt::dm_static
AVDOVIDmData dm_static[7]
static extension blocks
Definition: dovi_rpu.h:36
RPU_COEFF_FLOAT
@ RPU_COEFF_FLOAT
Definition: dovi_rpu.h:185
ff_dovi_rpu_generate
int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, int flags, uint8_t **out_rpu, int *out_size)
Synthesize a Dolby Vision RPU reflecting the current state.
Definition: dovi_rpuenc.c:562
ff_dovi_configure_ext
int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar, const AVDOVIMetadata *metadata, enum AVDOVICompression compression, int strict_std_compliance)
Configure the encoder for Dolby Vision encoding.
Definition: dovi_rpuenc.c:55
DOVIContext::vdr
AVDOVIDataMapping * vdr[DOVI_MAX_DM_ID+1]
RefStruct references.
Definition: dovi_rpu.h:87
DOVIContext::color
const AVDOVIColorMetadata * color
Definition: dovi_rpu.h:75
DOVIContext::rpu_buf_sz
unsigned rpu_buf_sz
Definition: dovi_rpu.h:89
ff_dovi_attach_side_data
int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
Attach the decoded AVDOVIMetadata as side data to an AVFrame.
Definition: dovi_rpudec.c:64
DOVIExt
Definition: dovi_rpu.h:35
DOVIExt::dm_dynamic
AVDOVIDmData dm_dynamic[25]
dynamic extension blocks
Definition: dovi_rpu.h:37
ff_dovi_ctx_flush
void ff_dovi_ctx_flush(DOVIContext *s)
Partially reset the internal state.
Definition: dovi_rpu.c:42
frame.h
ff_dovi_get_metadata
int ff_dovi_get_metadata(DOVIContext *s, AVDOVIMetadata **out_metadata)
Get the decoded AVDOVIMetadata.
Definition: dovi_rpudec.c:33
DOVIContext::header
AVDOVIRpuDataHeader header
Currently active RPU data header, updates on every ff_dovi_rpu_parse() or ff_dovi_rpu_generate().
Definition: dovi_rpu.h:67
avcodec.h
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
dovi_meta.h
DOVIContext::ext_blocks
DOVIExt * ext_blocks
Currently active extension blocks, updates on every ff_dovi_rpu_parse() or ff_dovi_rpu_generate().
Definition: dovi_rpu.h:81
AVCodecContext
main external API structure.
Definition: avcodec.h:451
DOVIContext::dm
AVDOVIColorMetadata * dm
Private fields internal to dovi_rpu.c.
Definition: dovi_rpu.h:86
FF_DOVI_WRAP_NAL
@ FF_DOVI_WRAP_NAL
wrap inside NAL RBSP
Definition: dovi_rpu.h:158
DOVIContext::enable
int enable
Definition: dovi_rpu.h:50
DOVIContext::rpu_buf
uint8_t * rpu_buf
temporary buffer
Definition: dovi_rpu.h:88
ff_dovi_rpu_extension_is_static
static int ff_dovi_rpu_extension_is_static(int level)
Definition: dovi_rpu.h:196
AVDOVIColorMetadata
Dolby Vision RPU colorspace metadata parameters.
Definition: dovi_meta.h:171
DOVIContext::logctx
void * logctx
Definition: dovi_rpu.h:43
AVDOVICompression
AVDOVICompression
Definition: dovi_meta.h:67
codec_par.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
DOVIExt::num_static
int num_static
Definition: dovi_rpu.h:38
ff_dovi_guess_profile_hevc
int ff_dovi_guess_profile_hevc(const AVDOVIRpuDataHeader *hdr)
Internal helper function to guess the correct DV profile for HEVC.
Definition: dovi_rpu.c:71
AVDOVIDataMapping
Dolby Vision RPU data mapping parameters.
Definition: dovi_meta.h:152
AVDOVIDecoderConfigurationRecord
Definition: dovi_meta.h:55
ff_dovi_ctx_unref
void ff_dovi_ctx_unref(DOVIContext *s)
Completely reset a DOVIContext, preserving only logctx.
Definition: dovi_rpu.c:29