Line | Branch | Exec | Source |
---|---|---|---|
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 | ||
38 | int num_static; | ||
39 | int num_dynamic; | ||
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 | */ | ||
61 | AVDOVIDecoderConfigurationRecord cfg; | ||
62 | |||
63 | /** | ||
64 | * Currently active RPU data header, updates on every ff_dovi_rpu_parse() | ||
65 | * or ff_dovi_rpu_generate(). | ||
66 | */ | ||
67 | AVDOVIRpuDataHeader header; | ||
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 | */ | ||
74 | const AVDOVIDataMapping *mapping; | ||
75 | const AVDOVIColorMetadata *color; | ||
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 | |||
93 | void ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0); | ||
94 | |||
95 | /** | ||
96 | * Completely reset a DOVIContext, preserving only logctx. | ||
97 | */ | ||
98 | void ff_dovi_ctx_unref(DOVIContext *s); | ||
99 | |||
100 | /** | ||
101 | * Partially reset the internal state. Resets per-frame state, but preserves | ||
102 | * the stream-wide configuration record. | ||
103 | */ | ||
104 | void ff_dovi_ctx_flush(DOVIContext *s); | ||
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 | */ | ||
132 | int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame); | ||
133 | |||
134 | /** | ||
135 | * Configure the encoder for Dolby Vision encoding. Generates a configuration | ||
136 | * record in s->cfg, and attaches it to codecpar->coded_side_data. Sets the | ||
137 | * correct profile and compatibility ID based on the tagged AVCodecParameters | ||
138 | * colorspace metadata, and the correct level based on the resolution and | ||
139 | * tagged framerate. | ||
140 | * | ||
141 | * `metadata` should point to the first frame's RPU, if available. If absent, | ||
142 | * auto-detection will be performed, but this can sometimes lead to inaccurate | ||
143 | * results (in particular for HEVC streams with enhancement layers). | ||
144 | * | ||
145 | * Returns 0 or a negative error code. | ||
146 | */ | ||
147 | int ff_dovi_configure_from_codedpar(DOVIContext *s, AVCodecParameters *codecpar, | ||
148 | const AVDOVIMetadata *metadata, | ||
149 | enum AVDOVICompression compression, | ||
150 | int strict_std_compliance); | ||
151 | |||
152 | /** | ||
153 | * Variant of `ff_dovi_configure_from_codedpar` which infers the codec | ||
154 | * parameters from an AVCodecContext. | ||
155 | */ | ||
156 | int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx); | ||
157 | |||
158 | enum { | ||
159 | FF_DOVI_WRAP_NAL = 1 << 0, ///< wrap inside NAL RBSP | ||
160 | FF_DOVI_WRAP_T35 = 1 << 1, ///< wrap inside T.35+EMDF | ||
161 | FF_DOVI_COMPRESS_RPU = 1 << 2, ///< enable compression for this RPU | ||
162 | }; | ||
163 | |||
164 | /** | ||
165 | * Synthesize a Dolby Vision RPU reflecting the current state. By default, the | ||
166 | * RPU is not encapsulated (see `flags` for more options). Note that this | ||
167 | * assumes all previous calls to `ff_dovi_rpu_generate` have been | ||
168 | * appropriately signalled, i.e. it will not re-send already transmitted | ||
169 | * redundant data. | ||
170 | * | ||
171 | * Mutates the internal state of DOVIContext to reflect the change. | ||
172 | * Returns 0 or a negative error code. | ||
173 | */ | ||
174 | int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, | ||
175 | int flags, uint8_t **out_rpu, int *out_size); | ||
176 | |||
177 | |||
178 | /*************************************************** | ||
179 | * The following section is for internal use only. * | ||
180 | ***************************************************/ | ||
181 | |||
182 | enum { | ||
183 | RPU_COEFF_FIXED = 0, | ||
184 | RPU_COEFF_FLOAT = 1, | ||
185 | }; | ||
186 | |||
187 | /** | ||
188 | * Internal helper function to guess the correct DV profile for HEVC. | ||
189 | * | ||
190 | * Returns the profile number or 0 if unknown. | ||
191 | */ | ||
192 | int ff_dovi_guess_profile_hevc(const AVDOVIRpuDataHeader *hdr); | ||
193 | |||
194 | /* Default values for AVDOVIColorMetadata */ | ||
195 | extern const AVDOVIColorMetadata ff_dovi_color_default; | ||
196 | |||
197 | 2 | static inline int ff_dovi_rpu_extension_is_static(int level) | |
198 | { | ||
199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | switch (level) { |
200 | ✗ | case 6: | |
201 | case 10: | ||
202 | case 32: /* reserved as static by spec */ | ||
203 | case 254: | ||
204 | case 255: | ||
205 | ✗ | return 1; | |
206 | 2 | default: | |
207 | 2 | return 0; | |
208 | } | ||
209 | } | ||
210 | |||
211 | #endif /* AVCODEC_DOVI_RPU_H */ | ||
212 |