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