FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1_metadata_bsf.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 27 60 45.0%
Branches: 17 58 29.3%

Line Branch Exec 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/opt.h"
21
22 #include "bsf.h"
23 #include "cbs.h"
24 #include "cbs_bsf.h"
25 #include "cbs_av1.h"
26
27 typedef struct AV1MetadataContext {
28 CBSBSFContext common;
29
30 int td;
31 AV1RawOBU td_obu;
32
33 int color_primaries;
34 int transfer_characteristics;
35 int matrix_coefficients;
36
37 int color_range;
38 int chroma_sample_position;
39
40 AVRational tick_rate;
41 int num_ticks_per_picture;
42
43 int delete_padding;
44 } AV1MetadataContext;
45
46
47 142 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
48 AV1RawSequenceHeader *seq)
49 {
50 142 AV1MetadataContext *ctx = bsf->priv_data;
51 142 AV1RawColorConfig *clc = &seq->color_config;
52 142 AV1RawTimingInfo *tim = &seq->timing_info;
53
54
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
142 if (ctx->color_primaries >= 0 ||
55
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
142 ctx->transfer_characteristics >= 0 ||
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 ctx->matrix_coefficients >= 0) {
57 clc->color_description_present_flag = 1;
58
59 if (ctx->color_primaries >= 0)
60 clc->color_primaries = ctx->color_primaries;
61 if (ctx->transfer_characteristics >= 0)
62 clc->transfer_characteristics = ctx->transfer_characteristics;
63 if (ctx->matrix_coefficients >= 0)
64 clc->matrix_coefficients = ctx->matrix_coefficients;
65 }
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (ctx->color_range >= 0) {
68 if (clc->color_primaries == AVCOL_PRI_BT709 &&
69 clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
70 clc->matrix_coefficients == AVCOL_SPC_RGB) {
71 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
72 "on RGB streams encoded in BT.709 sRGB.\n");
73 } else {
74 clc->color_range = ctx->color_range;
75 }
76 }
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (ctx->chroma_sample_position >= 0) {
79 if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
80 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
81 "can only be set for 4:2:0 streams.\n");
82 } else {
83 clc->chroma_sample_position = ctx->chroma_sample_position;
84 }
85 }
86
87
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
142 if (ctx->tick_rate.num && ctx->tick_rate.den) {
88 int num, den;
89
90 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
91 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
92
93 tim->time_scale = num;
94 tim->num_units_in_display_tick = den;
95 seq->timing_info_present_flag = 1;
96
97 if (ctx->num_ticks_per_picture > 0) {
98 tim->equal_picture_interval = 1;
99 tim->num_ticks_per_picture_minus_1 =
100 ctx->num_ticks_per_picture - 1;
101 }
102 }
103
104 142 return 0;
105 }
106
107 343 static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
108 CodedBitstreamFragment *frag)
109 {
110 343 AV1MetadataContext *ctx = bsf->priv_data;
111 int err, i;
112
113
2/2
✓ Branch 0 taken 995 times.
✓ Branch 1 taken 343 times.
1338 for (i = 0; i < frag->nb_units; i++) {
114
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 853 times.
995 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
115 142 AV1RawOBU *obu = frag->units[i].content;
116 142 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (err < 0)
118 return err;
119 }
120 }
121
122 // If a Temporal Delimiter is present, it must be the first OBU.
123
3/4
✓ Branch 0 taken 343 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 327 times.
✓ Branch 3 taken 16 times.
343 if (frag->nb_units && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (ctx->td == BSF_ELEMENT_REMOVE)
125 ff_cbs_delete_unit(frag, 0);
126
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) {
127 err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
128 &ctx->td_obu, NULL);
129 if (err < 0) {
130 av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
131 return err;
132 }
133 }
134
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 343 times.
343 if (ctx->delete_padding) {
136 for (i = frag->nb_units - 1; i >= 0; i--) {
137 if (frag->units[i].type == AV1_OBU_PADDING)
138 ff_cbs_delete_unit(frag, i);
139 }
140 }
141
142 343 return 0;
143 }
144
145 static const CBSBSFType av1_metadata_type = {
146 .codec_id = AV_CODEC_ID_AV1,
147 .fragment_name = "temporal unit",
148 .unit_name = "OBU",
149 .update_fragment = &av1_metadata_update_fragment,
150 };
151
152 16 static int av1_metadata_init(AVBSFContext *bsf)
153 {
154 16 AV1MetadataContext *ctx = bsf->priv_data;
155
156 16 ctx->td_obu = (AV1RawOBU) {
157 .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
158 };
159
160 16 return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type);
161 }
162
163 #define OFFSET(x) offsetof(AV1MetadataContext, x)
164 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
165 static const AVOption av1_metadata_options[] = {
166 BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
167 td, FLAGS),
168
169 { "color_primaries", "Set color primaries (section 6.4.2)",
170 OFFSET(color_primaries), AV_OPT_TYPE_INT,
171 { .i64 = -1 }, -1, 255, FLAGS },
172 { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
173 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
174 { .i64 = -1 }, -1, 255, FLAGS },
175 { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
176 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
177 { .i64 = -1 }, -1, 255, FLAGS },
178
179 { "color_range", "Set color range flag (section 6.4.2)",
180 OFFSET(color_range), AV_OPT_TYPE_INT,
181 { .i64 = -1 }, -1, 1, FLAGS, "cr" },
182 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
183 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
184 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
185 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
186
187 { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
188 OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
189 { .i64 = -1 }, -1, 3, FLAGS, "csp" },
190 { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
191 { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
192 { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
193 { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
194 { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
195 { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
196
197 { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
198 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
199 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
200 { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
201 OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
202 { .i64 = -1 }, -1, INT_MAX, FLAGS },
203
204 { "delete_padding", "Delete all Padding OBUs",
205 OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
206 { .i64 = 0 }, 0, 1, FLAGS},
207
208 { NULL }
209 };
210
211 static const AVClass av1_metadata_class = {
212 .class_name = "av1_metadata_bsf",
213 .item_name = av_default_item_name,
214 .option = av1_metadata_options,
215 .version = LIBAVUTIL_VERSION_INT,
216 };
217
218 static const enum AVCodecID av1_metadata_codec_ids[] = {
219 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
220 };
221
222 const AVBitStreamFilter ff_av1_metadata_bsf = {
223 .name = "av1_metadata",
224 .priv_data_size = sizeof(AV1MetadataContext),
225 .priv_class = &av1_metadata_class,
226 .init = &av1_metadata_init,
227 .close = &ff_cbs_bsf_generic_close,
228 .filter = &ff_cbs_bsf_generic_filter,
229 .codec_ids = av1_metadata_codec_ids,
230 };
231