]> git.sesse.net Git - ffmpeg/blob - libavcodec/av1_metadata_bsf.c
Merge commit '90adbf4abf336f8042aecdf1e18fdf76a96304b1'
[ffmpeg] / libavcodec / av1_metadata_bsf.c
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/opt.h"
21
22 #include "bsf.h"
23 #include "cbs.h"
24 #include "cbs_av1.h"
25
26 enum {
27     PASS,
28     INSERT,
29     REMOVE,
30 };
31
32 typedef struct AV1MetadataContext {
33     const AVClass *class;
34
35     CodedBitstreamContext *cbc;
36     CodedBitstreamFragment access_unit;
37
38     int td;
39
40     int color_primaries;
41     int transfer_characteristics;
42     int matrix_coefficients;
43
44     int color_range;
45     int chroma_sample_position;
46
47     AVRational tick_rate;
48     int num_ticks_per_picture;
49 } AV1MetadataContext;
50
51
52 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
53                                                AV1RawSequenceHeader *seq)
54 {
55     AV1MetadataContext *ctx = bsf->priv_data;
56     AV1RawColorConfig  *clc = &seq->color_config;
57     AV1RawTimingInfo   *tim = &seq->timing_info;
58
59     if (ctx->color_primaries >= 0          ||
60         ctx->transfer_characteristics >= 0 ||
61         ctx->matrix_coefficients >= 0) {
62         if (!clc->color_description_present_flag) {
63             clc->color_description_present_flag = 1;
64             clc->color_primaries          = AVCOL_PRI_UNSPECIFIED;
65             clc->transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
66             clc->matrix_coefficients      = AVCOL_SPC_UNSPECIFIED;
67         }
68
69         if (ctx->color_primaries >= 0)
70             clc->color_primaries = ctx->color_primaries;
71         if (ctx->transfer_characteristics >= 0)
72             clc->transfer_characteristics = ctx->transfer_characteristics;
73         if (ctx->matrix_coefficients >= 0)
74             clc->matrix_coefficients = ctx->matrix_coefficients;
75     }
76
77     if (ctx->color_range >= 0) {
78         if (clc->color_primaries          == AVCOL_PRI_BT709        &&
79             clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
80             clc->matrix_coefficients      == AVCOL_SPC_RGB) {
81             av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
82                    "on RGB streams encoded in BT.709 sRGB.\n");
83         } else {
84             clc->color_range = ctx->color_range;
85         }
86     }
87
88     if (ctx->chroma_sample_position >= 0) {
89         if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
90             av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
91                    "can only be set for 4:2:0 streams.\n");
92         } else {
93             clc->chroma_sample_position = ctx->chroma_sample_position;
94         }
95     }
96
97     if (ctx->tick_rate.num && ctx->tick_rate.den) {
98         int num, den;
99
100         av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
101                   UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
102
103         tim->time_scale                = num;
104         tim->num_units_in_display_tick = den;
105         seq->timing_info_present_flag  = 1;
106
107         if (ctx->num_ticks_per_picture > 0) {
108             tim->equal_picture_interval = 1;
109             tim->num_ticks_per_picture_minus_1 =
110                 ctx->num_ticks_per_picture - 1;
111         }
112     }
113
114     return 0;
115 }
116
117 static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *out)
118 {
119     AV1MetadataContext *ctx = bsf->priv_data;
120     AVPacket *in = NULL;
121     CodedBitstreamFragment *frag = &ctx->access_unit;
122     AV1RawOBU td, *obu;
123     int err, i;
124
125     err = ff_bsf_get_packet(bsf, &in);
126     if (err < 0)
127         return err;
128
129     err = ff_cbs_read_packet(ctx->cbc, frag, in);
130     if (err < 0) {
131         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
132         goto fail;
133     }
134
135     for (i = 0; i < frag->nb_units; i++) {
136         if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
137             obu = frag->units[i].content;
138             err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
139             if (err < 0)
140                 goto fail;
141         }
142     }
143
144     // If a Temporal Delimiter is present, it must be the first OBU.
145     if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
146         if (ctx->td == REMOVE)
147             ff_cbs_delete_unit(ctx->cbc, frag, 0);
148     } else if (ctx->td == INSERT) {
149         td = (AV1RawOBU) {
150             .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
151         };
152
153         err = ff_cbs_insert_unit_content(ctx->cbc, frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
154                                          &td, NULL);
155         if (err < 0) {
156             av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
157             goto fail;
158         }
159     }
160
161     err = ff_cbs_write_packet(ctx->cbc, out, frag);
162     if (err < 0) {
163         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
164         goto fail;
165     }
166
167     err = av_packet_copy_props(out, in);
168     if (err < 0)
169         goto fail;
170
171     err = 0;
172 fail:
173     ff_cbs_fragment_uninit(ctx->cbc, frag);
174
175     if (err < 0)
176         av_packet_unref(out);
177     av_packet_free(&in);
178
179     return err;
180 }
181
182 static int av1_metadata_init(AVBSFContext *bsf)
183 {
184     AV1MetadataContext *ctx = bsf->priv_data;
185     CodedBitstreamFragment *frag = &ctx->access_unit;
186     AV1RawOBU *obu;
187     int err, i;
188
189     err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
190     if (err < 0)
191         return err;
192
193     if (bsf->par_in->extradata) {
194         err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
195         if (err < 0) {
196             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
197             goto fail;
198         }
199
200         for (i = 0; i < frag->nb_units; i++) {
201             if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
202                 obu = frag->units[i].content;
203                 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
204                 if (err < 0)
205                     goto fail;
206             }
207         }
208
209         err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
210         if (err < 0) {
211             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
212             goto fail;
213         }
214     }
215
216     err = 0;
217 fail:
218     ff_cbs_fragment_uninit(ctx->cbc, frag);
219     return err;
220 }
221
222 static void av1_metadata_close(AVBSFContext *bsf)
223 {
224     AV1MetadataContext *ctx = bsf->priv_data;
225     ff_cbs_close(&ctx->cbc);
226 }
227
228 #define OFFSET(x) offsetof(AV1MetadataContext, x)
229 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
230 static const AVOption av1_metadata_options[] = {
231     { "td", "Temporal Delimiter OBU",
232         OFFSET(td), AV_OPT_TYPE_INT,
233         { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
234     { "pass",   NULL, 0, AV_OPT_TYPE_CONST,
235         { .i64 = PASS   }, .flags = FLAGS, .unit = "td" },
236     { "insert", NULL, 0, AV_OPT_TYPE_CONST,
237         { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
238     { "remove", NULL, 0, AV_OPT_TYPE_CONST,
239         { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
240
241     { "color_primaries", "Set color primaries (section 6.4.2)",
242         OFFSET(color_primaries), AV_OPT_TYPE_INT,
243         { .i64 = -1 }, -1, 255, FLAGS },
244     { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
245         OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
246         { .i64 = -1 }, -1, 255, FLAGS },
247     { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
248         OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
249         { .i64 = -1 }, -1, 255, FLAGS },
250
251     { "color_range", "Set color range flag (section 6.4.2)",
252         OFFSET(color_range), AV_OPT_TYPE_INT,
253         { .i64 = -1 }, -1, 1, FLAGS, "cr" },
254     { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
255         { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
256     { "pc", "PC (full) range",    0, AV_OPT_TYPE_CONST,
257         { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
258
259     { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
260         OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
261         { .i64 = -1 }, -1, 3, FLAGS, "csp" },
262     { "unknown",   "Unknown chroma sample position",  0, AV_OPT_TYPE_CONST,
263         { .i64 = AV1_CSP_UNKNOWN },   .flags = FLAGS, .unit = "csp" },
264     { "vertical",  "Left chroma sample position",     0, AV_OPT_TYPE_CONST,
265         { .i64 = AV1_CSP_VERTICAL },  .flags = FLAGS, .unit = "csp" },
266     { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
267         { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
268
269     { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
270         OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
271         { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
272     { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
273         OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
274         { .i64 = -1 }, -1, INT_MAX, FLAGS },
275
276     { NULL }
277 };
278
279 static const AVClass av1_metadata_class = {
280     .class_name = "av1_metadata_bsf",
281     .item_name  = av_default_item_name,
282     .option     = av1_metadata_options,
283     .version    = LIBAVUTIL_VERSION_INT,
284 };
285
286 static const enum AVCodecID av1_metadata_codec_ids[] = {
287     AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
288 };
289
290 const AVBitStreamFilter ff_av1_metadata_bsf = {
291     .name           = "av1_metadata",
292     .priv_data_size = sizeof(AV1MetadataContext),
293     .priv_class     = &av1_metadata_class,
294     .init           = &av1_metadata_init,
295     .close          = &av1_metadata_close,
296     .filter         = &av1_metadata_filter,
297     .codec_ids      = av1_metadata_codec_ids,
298 };