GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/av1_metadata_bsf.c Lines: 25 58 43.1 %
Date: 2021-04-20 04:37:23 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
32
    int color_primaries;
33
    int transfer_characteristics;
34
    int matrix_coefficients;
35
36
    int color_range;
37
    int chroma_sample_position;
38
39
    AVRational tick_rate;
40
    int num_ticks_per_picture;
41
42
    int delete_padding;
43
} AV1MetadataContext;
44
45
46
142
static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
47
                                               AV1RawSequenceHeader *seq)
48
{
49
142
    AV1MetadataContext *ctx = bsf->priv_data;
50
142
    AV1RawColorConfig  *clc = &seq->color_config;
51
142
    AV1RawTimingInfo   *tim = &seq->timing_info;
52
53
142
    if (ctx->color_primaries >= 0          ||
54
142
        ctx->transfer_characteristics >= 0 ||
55
142
        ctx->matrix_coefficients >= 0) {
56
        clc->color_description_present_flag = 1;
57
58
        if (ctx->color_primaries >= 0)
59
            clc->color_primaries = ctx->color_primaries;
60
        if (ctx->transfer_characteristics >= 0)
61
            clc->transfer_characteristics = ctx->transfer_characteristics;
62
        if (ctx->matrix_coefficients >= 0)
63
            clc->matrix_coefficients = ctx->matrix_coefficients;
64
    }
65
66
142
    if (ctx->color_range >= 0) {
67
        if (clc->color_primaries          == AVCOL_PRI_BT709        &&
68
            clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
69
            clc->matrix_coefficients      == AVCOL_SPC_RGB) {
70
            av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
71
                   "on RGB streams encoded in BT.709 sRGB.\n");
72
        } else {
73
            clc->color_range = ctx->color_range;
74
        }
75
    }
76
77
142
    if (ctx->chroma_sample_position >= 0) {
78
        if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
79
            av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
80
                   "can only be set for 4:2:0 streams.\n");
81
        } else {
82
            clc->chroma_sample_position = ctx->chroma_sample_position;
83
        }
84
    }
85
86

142
    if (ctx->tick_rate.num && ctx->tick_rate.den) {
87
        int num, den;
88
89
        av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
90
                  UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
91
92
        tim->time_scale                = num;
93
        tim->num_units_in_display_tick = den;
94
        seq->timing_info_present_flag  = 1;
95
96
        if (ctx->num_ticks_per_picture > 0) {
97
            tim->equal_picture_interval = 1;
98
            tim->num_ticks_per_picture_minus_1 =
99
                ctx->num_ticks_per_picture - 1;
100
        }
101
    }
102
103
142
    return 0;
104
}
105
106
343
static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
107
                                        CodedBitstreamFragment *frag)
108
{
109
343
    AV1MetadataContext *ctx = bsf->priv_data;
110
    AV1RawOBU td, *obu;
111
    int err, i;
112
113
1338
    for (i = 0; i < frag->nb_units; i++) {
114
995
        if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
115
142
            obu = frag->units[i].content;
116
142
            err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
117
142
            if (err < 0)
118
                return err;
119
        }
120
    }
121
122
    // If a Temporal Delimiter is present, it must be the first OBU.
123

343
    if (frag->nb_units && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
124
327
        if (ctx->td == BSF_ELEMENT_REMOVE)
125
            ff_cbs_delete_unit(frag, 0);
126

16
    } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) {
127
        td = (AV1RawOBU) {
128
            .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
129
        };
130
131
        err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
132
                                         &td, NULL);
133
        if (err < 0) {
134
            av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
135
            return err;
136
        }
137
    }
138
139
343
    if (ctx->delete_padding) {
140
        for (i = frag->nb_units - 1; i >= 0; i--) {
141
            if (frag->units[i].type == AV1_OBU_PADDING)
142
                ff_cbs_delete_unit(frag, i);
143
        }
144
    }
145
146
343
    return 0;
147
}
148
149
static const CBSBSFType av1_metadata_type = {
150
    .codec_id        = AV_CODEC_ID_AV1,
151
    .fragment_name   = "temporal unit",
152
    .unit_name       = "OBU",
153
    .update_fragment = &av1_metadata_update_fragment,
154
};
155
156
16
static int av1_metadata_init(AVBSFContext *bsf)
157
{
158
16
    return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type);
159
}
160
161
#define OFFSET(x) offsetof(AV1MetadataContext, x)
162
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
163
static const AVOption av1_metadata_options[] = {
164
    BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
165
                            td, FLAGS),
166
167
    { "color_primaries", "Set color primaries (section 6.4.2)",
168
        OFFSET(color_primaries), AV_OPT_TYPE_INT,
169
        { .i64 = -1 }, -1, 255, FLAGS },
170
    { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
171
        OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
172
        { .i64 = -1 }, -1, 255, FLAGS },
173
    { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
174
        OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
175
        { .i64 = -1 }, -1, 255, FLAGS },
176
177
    { "color_range", "Set color range flag (section 6.4.2)",
178
        OFFSET(color_range), AV_OPT_TYPE_INT,
179
        { .i64 = -1 }, -1, 1, FLAGS, "cr" },
180
    { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
181
        { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
182
    { "pc", "PC (full) range",    0, AV_OPT_TYPE_CONST,
183
        { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
184
185
    { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
186
        OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
187
        { .i64 = -1 }, -1, 3, FLAGS, "csp" },
188
    { "unknown",   "Unknown chroma sample position",  0, AV_OPT_TYPE_CONST,
189
        { .i64 = AV1_CSP_UNKNOWN },   .flags = FLAGS, .unit = "csp" },
190
    { "vertical",  "Left chroma sample position",     0, AV_OPT_TYPE_CONST,
191
        { .i64 = AV1_CSP_VERTICAL },  .flags = FLAGS, .unit = "csp" },
192
    { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
193
        { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
194
195
    { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
196
        OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
197
        { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
198
    { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
199
        OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
200
        { .i64 = -1 }, -1, INT_MAX, FLAGS },
201
202
    { "delete_padding", "Delete all Padding OBUs",
203
        OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
204
        { .i64 = 0 }, 0, 1, FLAGS},
205
206
    { NULL }
207
};
208
209
static const AVClass av1_metadata_class = {
210
    .class_name = "av1_metadata_bsf",
211
    .item_name  = av_default_item_name,
212
    .option     = av1_metadata_options,
213
    .version    = LIBAVUTIL_VERSION_INT,
214
};
215
216
static const enum AVCodecID av1_metadata_codec_ids[] = {
217
    AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
218
};
219
220
const AVBitStreamFilter ff_av1_metadata_bsf = {
221
    .name           = "av1_metadata",
222
    .priv_data_size = sizeof(AV1MetadataContext),
223
    .priv_class     = &av1_metadata_class,
224
    .init           = &av1_metadata_init,
225
    .close          = &ff_cbs_bsf_generic_close,
226
    .filter         = &ff_cbs_bsf_generic_filter,
227
    .codec_ids      = av1_metadata_codec_ids,
228
};