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