/src/ffmpeg/libavcodec/bsf/dovi_rpu.c
Line | Count | Source |
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 "libavutil/common.h" |
20 | | #include "libavutil/mem.h" |
21 | | #include "libavutil/opt.h" |
22 | | |
23 | | #include "bsf.h" |
24 | | #include "bsf_internal.h" |
25 | | #include "cbs.h" |
26 | | #include "cbs_bsf.h" |
27 | | #include "cbs_av1.h" |
28 | | #include "cbs_h265.h" |
29 | | #include "dovi_rpu.h" |
30 | | #include "h2645data.h" |
31 | | #include "h265_profile_level.h" |
32 | | #include "itut35.h" |
33 | | |
34 | | #include "hevc/hevc.h" |
35 | | |
36 | | typedef struct DoviRpuContext { |
37 | | CBSBSFContext common; |
38 | | DOVIContext dec; |
39 | | DOVIContext enc; |
40 | | |
41 | | int strip; |
42 | | int compression; |
43 | | } DoviRpuContext; |
44 | | |
45 | | static int update_rpu(AVBSFContext *bsf, const AVPacket *pkt, int flags, |
46 | | const uint8_t *rpu, size_t rpu_size, |
47 | | uint8_t **out_rpu, int *out_size) |
48 | 0 | { |
49 | 0 | DoviRpuContext *s = bsf->priv_data; |
50 | 0 | AVDOVIMetadata *metadata = NULL; |
51 | 0 | int ret; |
52 | |
|
53 | 0 | ret = ff_dovi_rpu_parse(&s->dec, rpu, rpu_size, 0); |
54 | 0 | if (ret < 0) { |
55 | 0 | ff_dovi_ctx_flush(&s->dec); |
56 | 0 | return ret; |
57 | 0 | } |
58 | | |
59 | 0 | ret = ff_dovi_get_metadata(&s->dec, &metadata); |
60 | 0 | if (ret == 0 /* no metadata */) { |
61 | 0 | *out_rpu = NULL; |
62 | 0 | *out_size = 0; |
63 | 0 | return 0; |
64 | 0 | } else if (ret < 0) { |
65 | 0 | ff_dovi_ctx_flush(&s->dec); |
66 | 0 | return ret; |
67 | 0 | } |
68 | | |
69 | 0 | if (pkt && !(pkt->flags & AV_PKT_FLAG_KEY)) |
70 | 0 | flags |= FF_DOVI_COMPRESS_RPU; |
71 | 0 | ret = ff_dovi_rpu_generate(&s->enc, metadata, flags, out_rpu, out_size); |
72 | 0 | av_free(metadata); |
73 | 0 | if (ret < 0) |
74 | 0 | ff_dovi_ctx_flush(&s->enc); |
75 | |
|
76 | 0 | return ret; |
77 | 0 | } |
78 | | |
79 | | static int dovi_rpu_update_fragment_hevc(AVBSFContext *bsf, AVPacket *pkt, |
80 | | CodedBitstreamFragment *au) |
81 | 0 | { |
82 | 0 | DoviRpuContext *s = bsf->priv_data; |
83 | 0 | CodedBitstreamUnit *nal = au->nb_units ? &au->units[au->nb_units - 1] : NULL; |
84 | 0 | uint8_t *rpu = NULL; |
85 | 0 | int rpu_size, ret; |
86 | | |
87 | | // HEVC_NAL_UNSPEC62 is Dolby Vision PRU and HEVC_NAL_UNSPEC63 is Dolby Vision EL |
88 | 0 | if (!nal || (nal->type != HEVC_NAL_UNSPEC62 && nal->type != HEVC_NAL_UNSPEC63)) |
89 | 0 | return 0; |
90 | | |
91 | 0 | if (s->strip) { |
92 | 0 | ff_cbs_delete_unit(au, au->nb_units - 1); |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | 0 | if (nal->type == HEVC_NAL_UNSPEC63) |
97 | 0 | return 0; |
98 | | |
99 | 0 | ret = update_rpu(bsf, pkt, 0, nal->data + 2, nal->data_size - 2, &rpu, &rpu_size); |
100 | 0 | if (ret < 0) |
101 | 0 | return ret; |
102 | | |
103 | | /* NAL unit header + NAL prefix */ |
104 | 0 | if (rpu_size + 3 <= nal->data_size && av_buffer_is_writable(nal->data_ref)) { |
105 | 0 | memcpy(nal->data + 3, rpu, rpu_size); |
106 | 0 | av_free(rpu); |
107 | 0 | nal->data_size = rpu_size + 3; |
108 | 0 | } else { |
109 | 0 | AVBufferRef *ref = av_buffer_alloc(rpu_size + 3); |
110 | 0 | if (!ref) { |
111 | 0 | av_free(rpu); |
112 | 0 | return AVERROR(ENOMEM); |
113 | 0 | } |
114 | | |
115 | 0 | memcpy(ref->data, nal->data, 3); |
116 | 0 | memcpy(ref->data + 3, rpu, rpu_size); |
117 | 0 | av_buffer_unref(&nal->data_ref); |
118 | 0 | av_free(rpu); |
119 | 0 | nal->data = ref->data; |
120 | 0 | nal->data_size = rpu_size + 3; |
121 | 0 | nal->data_ref = ref; |
122 | 0 | nal->data_bit_padding = 0; |
123 | 0 | } |
124 | | |
125 | 0 | return 0; |
126 | 0 | } |
127 | | |
128 | | static int dovi_rpu_update_fragment_av1(AVBSFContext *bsf, AVPacket *pkt, |
129 | | CodedBitstreamFragment *frag) |
130 | 0 | { |
131 | 0 | DoviRpuContext *s = bsf->priv_data; |
132 | 0 | int provider_code, provider_oriented_code, rpu_size, ret; |
133 | 0 | AVBufferRef *ref; |
134 | 0 | uint8_t *rpu; |
135 | |
|
136 | 0 | for (int i = 0; i < frag->nb_units; i++) { |
137 | 0 | AV1RawOBU *obu = frag->units[i].content; |
138 | 0 | AV1RawMetadataITUTT35 *t35 = &obu->obu.metadata.metadata.itut_t35; |
139 | 0 | if (frag->units[i].type != AV1_OBU_METADATA || |
140 | 0 | obu->obu.metadata.metadata_type != AV1_METADATA_TYPE_ITUT_T35 || |
141 | 0 | t35->itu_t_t35_country_code != ITU_T_T35_COUNTRY_CODE_US || |
142 | 0 | t35->payload_size < 6) |
143 | 0 | continue; |
144 | | |
145 | 0 | provider_code = AV_RB16(t35->payload); |
146 | 0 | provider_oriented_code = AV_RB32(t35->payload + 2); |
147 | 0 | if (provider_code != ITU_T_T35_PROVIDER_CODE_DOLBY || |
148 | 0 | provider_oriented_code != 0x800) |
149 | 0 | continue; |
150 | | |
151 | 0 | if (s->strip) { |
152 | 0 | ff_cbs_delete_unit(frag, i); |
153 | 0 | return 0; |
154 | 0 | } |
155 | | |
156 | 0 | ret = update_rpu(bsf, pkt, FF_DOVI_WRAP_T35, |
157 | 0 | t35->payload + 6, t35->payload_size - 6, |
158 | 0 | &rpu, &rpu_size); |
159 | 0 | if (ret < 0) |
160 | 0 | return ret; |
161 | | |
162 | 0 | ref = av_buffer_create(rpu, rpu_size, av_buffer_default_free, NULL, 0); |
163 | 0 | if (!ref) { |
164 | 0 | av_free(rpu); |
165 | 0 | return AVERROR(ENOMEM); |
166 | 0 | } |
167 | | |
168 | 0 | av_buffer_unref(&t35->payload_ref); |
169 | 0 | t35->payload_ref = ref; |
170 | 0 | t35->payload = rpu + 1; /* skip country code */ |
171 | 0 | t35->payload_size = rpu_size - 1; |
172 | 0 | break; /* should be only one RPU per packet */ |
173 | 0 | } |
174 | | |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | | static const CBSBSFType dovi_rpu_hevc_type = { |
179 | | .codec_id = AV_CODEC_ID_HEVC, |
180 | | .fragment_name = "access unit", |
181 | | .unit_name = "NAL unit", |
182 | | .update_fragment = &dovi_rpu_update_fragment_hevc, |
183 | | }; |
184 | | |
185 | | static const CBSBSFType dovi_rpu_av1_type = { |
186 | | .codec_id = AV_CODEC_ID_AV1, |
187 | | .fragment_name = "temporal unit", |
188 | | .unit_name = "OBU", |
189 | | .update_fragment = &dovi_rpu_update_fragment_av1, |
190 | | }; |
191 | | |
192 | | static int dovi_rpu_init(AVBSFContext *bsf) |
193 | 171 | { |
194 | 171 | int ret; |
195 | 171 | DoviRpuContext *s = bsf->priv_data; |
196 | 171 | s->dec.logctx = s->enc.logctx = bsf; |
197 | 171 | s->enc.enable = 1; |
198 | | |
199 | 171 | if (s->compression == AV_DOVI_COMPRESSION_RESERVED) { |
200 | 0 | av_log(bsf, AV_LOG_ERROR, "Invalid compression level: %d\n", s->compression); |
201 | 0 | return AVERROR(EINVAL); |
202 | 0 | } |
203 | | |
204 | 171 | if (s->strip) { |
205 | 0 | av_packet_side_data_remove(bsf->par_out->coded_side_data, |
206 | 0 | &bsf->par_out->nb_coded_side_data, |
207 | 0 | AV_PKT_DATA_DOVI_CONF); |
208 | 171 | } else { |
209 | 171 | const AVPacketSideData *sd; |
210 | 171 | sd = av_packet_side_data_get(bsf->par_out->coded_side_data, |
211 | 171 | bsf->par_out->nb_coded_side_data, |
212 | 171 | AV_PKT_DATA_DOVI_CONF); |
213 | | |
214 | 171 | if (sd) { |
215 | 0 | AVDOVIDecoderConfigurationRecord *cfg; |
216 | 0 | cfg = (AVDOVIDecoderConfigurationRecord *) sd->data; |
217 | 0 | s->dec.cfg = *cfg; |
218 | | |
219 | | /* Update configuration record before setting to enc ctx */ |
220 | 0 | cfg->dv_md_compression = s->compression; |
221 | 0 | if (s->compression && s->dec.cfg.dv_profile < 8) { |
222 | 0 | av_log(bsf, AV_LOG_ERROR, "Invalid compression level %d for " |
223 | 0 | "Dolby Vision profile %d.\n", s->compression, s->dec.cfg.dv_profile); |
224 | 0 | return AVERROR(EINVAL); |
225 | 0 | } |
226 | | |
227 | 0 | s->enc.cfg = *cfg; |
228 | 171 | } else { |
229 | 171 | av_log(bsf, AV_LOG_WARNING, "No Dolby Vision configuration record " |
230 | 171 | "found? Generating one, but results may be invalid.\n"); |
231 | 171 | ret = ff_dovi_configure_from_codedpar(&s->enc, bsf->par_out, NULL, s->compression, |
232 | 171 | FF_COMPLIANCE_NORMAL); |
233 | 171 | if (ret < 0) |
234 | 171 | return ret; |
235 | | /* Be conservative in accepting all compressed RPUs */ |
236 | 0 | s->dec.cfg = s->enc.cfg; |
237 | 0 | s->dec.cfg.dv_md_compression = AV_DOVI_COMPRESSION_EXTENDED; |
238 | 0 | } |
239 | 171 | } |
240 | | |
241 | 0 | switch (bsf->par_in->codec_id) { |
242 | 0 | case AV_CODEC_ID_HEVC: |
243 | 0 | return ff_cbs_bsf_generic_init(bsf, &dovi_rpu_hevc_type); |
244 | 0 | case AV_CODEC_ID_AV1: |
245 | 0 | return ff_cbs_bsf_generic_init(bsf, &dovi_rpu_av1_type); |
246 | 0 | default: |
247 | 0 | return AVERROR_BUG; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | | static void dovi_rpu_close(AVBSFContext *bsf) |
252 | 191 | { |
253 | 191 | DoviRpuContext *s = bsf->priv_data; |
254 | 191 | ff_dovi_ctx_unref(&s->dec); |
255 | 191 | ff_dovi_ctx_unref(&s->enc); |
256 | 191 | ff_cbs_bsf_generic_close(bsf); |
257 | 191 | } |
258 | | |
259 | | #define OFFSET(x) offsetof(DoviRpuContext, x) |
260 | | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) |
261 | | static const AVOption dovi_rpu_options[] = { |
262 | | { "strip", "Strip Dolby Vision metadata", OFFSET(strip), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, |
263 | | { "compression", "DV metadata compression mode", OFFSET(compression), AV_OPT_TYPE_INT, { .i64 = AV_DOVI_COMPRESSION_LIMITED }, 0, AV_DOVI_COMPRESSION_EXTENDED, FLAGS, .unit = "compression" }, |
264 | | { "none", "Don't compress metadata", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, .flags = FLAGS, .unit = "compression" }, |
265 | | { "limited", "Limited metadata compression", 0, AV_OPT_TYPE_CONST, {.i64 = AV_DOVI_COMPRESSION_LIMITED}, .flags = FLAGS, .unit = "compression" }, |
266 | | { "extended", "Extended metadata compression",0, AV_OPT_TYPE_CONST, {.i64 = AV_DOVI_COMPRESSION_EXTENDED}, .flags = FLAGS, .unit = "compression" }, |
267 | | { NULL } |
268 | | }; |
269 | | |
270 | | static const AVClass dovi_rpu_class = { |
271 | | .class_name = "dovi_rpu_bsf", |
272 | | .item_name = av_default_item_name, |
273 | | .option = dovi_rpu_options, |
274 | | .version = LIBAVUTIL_VERSION_INT, |
275 | | }; |
276 | | |
277 | | static const enum AVCodecID dovi_rpu_codec_ids[] = { |
278 | | AV_CODEC_ID_HEVC, AV_CODEC_ID_AV1, AV_CODEC_ID_NONE, |
279 | | }; |
280 | | |
281 | | const FFBitStreamFilter ff_dovi_rpu_bsf = { |
282 | | .p.name = "dovi_rpu", |
283 | | .p.codec_ids = dovi_rpu_codec_ids, |
284 | | .p.priv_class = &dovi_rpu_class, |
285 | | .priv_data_size = sizeof(DoviRpuContext), |
286 | | .init = &dovi_rpu_init, |
287 | | .close = &dovi_rpu_close, |
288 | | .filter = &ff_cbs_bsf_generic_filter, |
289 | | }; |