FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/apv_metadata.c
Date: 2026-11-01 11:15:23
Exec Total Coverage
Lines: 19 58 32.8%
Functions: 3 3 100.0%
Branches: 10 41 24.4%

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 "libavcodec/bsf.h"
23 #include "libavcodec/bsf_internal.h"
24 #include "libavcodec/cbs.h"
25 #include "libavcodec/cbs_bsf.h"
26 #include "libavcodec/cbs_apv.h"
27
28 typedef struct APVMetadataContext {
29 CBSBSFContext common;
30
31 int color_primaries;
32 int transfer_characteristics;
33 int matrix_coefficients;
34 int full_range_flag;
35
36 int delete_filler;
37 } APVMetadataContext;
38
39
40 6 static int apv_metadata_update_frame_header(AVBSFContext *bsf,
41 APVRawFrameHeader *hdr)
42 {
43 6 APVMetadataContext *ctx = bsf->priv_data;
44
45
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (ctx->color_primaries >= 0 ||
46
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 ctx->transfer_characteristics >= 0 ||
47
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 ctx->matrix_coefficients >= 0 ||
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 ctx->full_range_flag >= 0) {
49 ✗ hdr->color_description_present_flag = 1;
50
51 ✗ if (ctx->color_primaries >= 0)
52 ✗ hdr->color_primaries = ctx->color_primaries;
53 ✗ if (ctx->transfer_characteristics >= 0)
54 ✗ hdr->transfer_characteristics = ctx->transfer_characteristics;
55 ✗ if (ctx->matrix_coefficients >= 0)
56 ✗ hdr->matrix_coefficients = ctx->matrix_coefficients;
57 ✗ if (ctx->full_range_flag >= 0)
58 ✗ hdr->full_range_flag = ctx->full_range_flag;
59 }
60
61 6 return 0;
62 }
63
64 6 static int apv_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
65 CodedBitstreamFragment *frag)
66 {
67 6 APVMetadataContext *ctx = bsf->priv_data;
68 6 const CodedBitstreamAPVContext *priv = ctx->common.input->priv_data;
69 int err, i;
70
71
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (i = 0; i < frag->nb_units; i++) {
72
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (frag->units[i].type == APV_PBU_PRIMARY_FRAME) {
73 6 APVRawFrame *pbu = frag->units[i].content;
74 6 err = apv_metadata_update_frame_header(bsf, &pbu->frame_header);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err < 0)
76 ✗ return err;
77 }
78 }
79
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ctx->delete_filler) {
81 // Types of filler to delete:
82 // - Filler PBUs.
83 // - Filler at end of frame PBUs.
84 // - Dummy bytes trailing in tile data.
85 // - Filler at end of AU info PBUs.
86 // - Filler at end of metadata PBUs.
87 // - Filler metadata payloads.
88
89 ✗ for (i = frag->nb_units - 1; i >= 0; i--) {
90 ✗ switch (frag->units[i].type) {
91 ✗ case APV_PBU_FILLER:
92 ✗ ff_cbs_delete_unit(frag, i);
93 ✗ break;
94 ✗ case APV_PBU_PRIMARY_FRAME:
95 case APV_PBU_NON_PRIMARY_FRAME:
96 case APV_PBU_PREVIEW_FRAME:
97 case APV_PBU_DEPTH_FRAME:
98 case APV_PBU_ALPHA_FRAME:
99 {
100 ✗ APVRawFrame *frame = frag->units[i].content;
101 ✗ frame->filler.filler_size = 0;
102 ✗ for (int t = 0; t < priv->num_tiles; t++)
103 ✗ frame->tile[t].tile_dummy_byte_size = 0;
104 // tile_size[t] may be wrong here, but are
105 // recalculated during CBS write.
106 }
107 ✗ break;
108 ✗ case APV_PBU_ACCESS_UNIT_INFORMATION:
109 {
110 ✗ APVRawAUInfo *au_info = frag->units[i].content;
111 ✗ au_info->filler.filler_size = 0;
112 }
113 ✗ break;
114 ✗ case APV_PBU_METADATA:
115 {
116 ✗ APVRawMetadata *md = frag->units[i].content;
117 ✗ int k = 0;
118 ✗ md->filler.filler_size = 0;
119 ✗ for (int j = 0; j < md->metadata_count; j++) {
120 ✗ APVRawMetadataPayload *pl = &md->payloads[j];
121 ✗ if (pl->payload_type == APV_METADATA_FILLER) {
122 // Drop this payload.
123 } else {
124 ✗ if (k < j)
125 ✗ md->payloads[k] = md->payloads[j];
126 ✗ ++k;
127 }
128 }
129 ✗ if (k == 0) {
130 // This metadata PBU contained only a filler
131 // payload, so now we have to delete the whole
132 // thing because a metadata PBU must contain at
133 // least one payload.
134 ✗ ff_cbs_delete_unit(frag, i);
135 } else {
136 ✗ md->metadata_count = k;
137 // metadata_size may be wrong here, but is
138 // recalculated during CBS write.
139 }
140 }
141 ✗ break;
142 }
143 }
144 }
145
146 6 return 0;
147 }
148
149 static const CBSBSFType apv_metadata_type = {
150 .codec_id = AV_CODEC_ID_APV,
151 .fragment_name = "access unit",
152 .unit_name = "PBU",
153 .update_fragment = &apv_metadata_update_fragment,
154 };
155
156 2 static int apv_metadata_init(AVBSFContext *bsf)
157 {
158 2 return ff_cbs_bsf_generic_init(bsf, &apv_metadata_type);
159 }
160
161 #define OFFSET(x) offsetof(APVMetadataContext, x)
162 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
163 static const AVOption apv_metadata_options[] = {
164 { "color_primaries", "Set color primaries (section 5.3.5)",
165 OFFSET(color_primaries), AV_OPT_TYPE_INT,
166 { .i64 = -1 }, -1, 255, FLAGS },
167 { "transfer_characteristics", "Set transfer characteristics (section 5.3.5)",
168 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
169 { .i64 = -1 }, -1, 255, FLAGS },
170 { "matrix_coefficients", "Set matrix coefficients (section 5.3.5)",
171 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
172 { .i64 = -1 }, -1, 255, FLAGS },
173
174 { "full_range_flag", "Set full range flag flag (section 5.3.5)",
175 OFFSET(full_range_flag), AV_OPT_TYPE_INT,
176 { .i64 = -1 }, -1, 1, FLAGS, .unit = "cr" },
177 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
178 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
179 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
180 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
181
182 { "delete_filler", "Delete all filler in the stream",
183 OFFSET(delete_filler), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
184
185 { NULL }
186 };
187
188 static const AVClass apv_metadata_class = {
189 .class_name = "apv_metadata_bsf",
190 .item_name = av_default_item_name,
191 .option = apv_metadata_options,
192 .version = LIBAVUTIL_VERSION_INT,
193 };
194
195 static const enum AVCodecID apv_metadata_codec_ids[] = {
196 AV_CODEC_ID_APV, AV_CODEC_ID_NONE,
197 };
198
199 const FFBitStreamFilter ff_apv_metadata_bsf = {
200 .p.name = "apv_metadata",
201 .p.codec_ids = apv_metadata_codec_ids,
202 .p.priv_class = &apv_metadata_class,
203 .priv_data_size = sizeof(APVMetadataContext),
204 .init = &apv_metadata_init,
205 .close = &ff_cbs_bsf_generic_close,
206 .filter = &ff_cbs_bsf_generic_filter,
207 };
208