FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_bsf.c
Date: 2026-07-21 08:37:06
Exec Total Coverage
Lines: 69 91 75.8%
Functions: 4 4 100.0%
Branches: 23 36 63.9%

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 "bsf.h"
20 #include "bsf_internal.h"
21 #include "cbs_bsf.h"
22
23 #include "libavutil/attributes.h"
24
25 const enum AVCodecID ff_cbs_all_codec_ids[] = {
26 #if CONFIG_CBS_APV
27 AV_CODEC_ID_APV,
28 #endif
29 #if CONFIG_CBS_AV1
30 AV_CODEC_ID_AV1,
31 #endif
32 #if CONFIG_CBS_H264
33 AV_CODEC_ID_H264,
34 #endif
35 #if CONFIG_CBS_H265
36 AV_CODEC_ID_H265,
37 #endif
38 #if CONFIG_CBS_H266
39 AV_CODEC_ID_H266,
40 #endif
41 #if CONFIG_CBS_LCEVC
42 AV_CODEC_ID_LCEVC,
43 #endif
44 #if CONFIG_CBS_JPEG
45 AV_CODEC_ID_MJPEG,
46 #endif
47 #if CONFIG_CBS_MPEG2
48 AV_CODEC_ID_MPEG2VIDEO,
49 #endif
50 #if CONFIG_CBS_VP8
51 AV_CODEC_ID_VP8,
52 #endif
53 #if CONFIG_CBS_VP9
54 AV_CODEC_ID_VP9,
55 #endif
56 AV_CODEC_ID_NONE
57 };
58
59 7652 static int cbs_bsf_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
60 {
61 7652 CBSBSFContext *ctx = bsf->priv_data;
62 7652 CodedBitstreamFragment *frag = &ctx->fragment;
63 uint8_t *side_data;
64 int err;
65
66
2/2
✓ Branch 1 taken 7651 times.
✓ Branch 2 taken 1 times.
7652 if (!av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, NULL))
67 7651 return 0;
68
69 1 err = ff_cbs_read_packet_side_data(ctx->input, frag, pkt);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0) {
71 av_log(bsf, AV_LOG_ERROR,
72 "Failed to read extradata from packet side data.\n");
73 return err;
74 }
75
76 1 err = ctx->type->update_fragment(bsf, NULL, frag);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
78 return err;
79
80 1 err = ff_cbs_write_fragment_data(ctx->output, frag);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0) {
82 av_log(bsf, AV_LOG_ERROR,
83 "Failed to write extradata into packet side data.\n");
84 return err;
85 }
86
87 1 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
88 frag->data_size);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!side_data)
90 return AVERROR(ENOMEM);
91 1 memcpy(side_data, frag->data, frag->data_size);
92
93 1 ff_cbs_fragment_reset(frag);
94 1 return 0;
95 }
96
97 15407 int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
98 {
99 15407 CBSBSFContext *ctx = bsf->priv_data;
100 15407 CodedBitstreamFragment *frag = &ctx->fragment;
101 int err;
102
103 15407 err = ff_bsf_get_packet_ref(bsf, pkt);
104
2/2
✓ Branch 0 taken 7755 times.
✓ Branch 1 taken 7652 times.
15407 if (err < 0)
105 7755 return err;
106
107 7652 err = cbs_bsf_update_side_data(bsf, pkt);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7652 times.
7652 if (err < 0)
109 goto fail;
110
111 7652 err = ff_cbs_read_packet(ctx->input, frag, pkt);
112
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7651 times.
7652 if (err < 0) {
113 1 av_log(bsf, AV_LOG_ERROR, "Failed to read %s from packet.\n",
114 1 ctx->type->fragment_name);
115 1 goto fail;
116 }
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7651 times.
7651 if (frag->nb_units == 0) {
119 av_log(bsf, AV_LOG_ERROR, "No %s found in packet.\n",
120 ctx->type->unit_name);
121 err = AVERROR_INVALIDDATA;
122 goto fail;
123 }
124
125 7651 err = ctx->type->update_fragment(bsf, pkt, frag);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7651 times.
7651 if (err < 0)
127 goto fail;
128
129 7651 err = ff_cbs_write_packet(ctx->output, pkt, frag);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7651 times.
7651 if (err < 0) {
131 av_log(bsf, AV_LOG_ERROR, "Failed to write %s into packet.\n",
132 ctx->type->fragment_name);
133 goto fail;
134 }
135
136 7651 err = 0;
137 7652 fail:
138 7652 ff_cbs_fragment_reset(frag);
139
140
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7651 times.
7652 if (err < 0)
141 1 av_packet_unref(pkt);
142
143 7652 return err;
144 }
145
146 105 av_cold int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
147 {
148 105 CBSBSFContext *ctx = bsf->priv_data;
149 105 CodedBitstreamFragment *frag = &ctx->fragment;
150 int err;
151
152 105 ctx->type = type;
153
154 105 err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (err < 0)
156 return err;
157
158 105 err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (err < 0)
160 return err;
161
162 105 ctx->output->trace_enable = 1;
163 105 ctx->output->trace_level = AV_LOG_TRACE;
164 105 ctx->output->trace_context = ctx->output;
165 105 ctx->output->trace_write_callback = ff_cbs_trace_write_log;
166
167
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 16 times.
105 if (bsf->par_in->extradata) {
168 89 err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (err < 0) {
170 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
171 goto fail;
172 }
173
174 89 err = type->update_fragment(bsf, NULL, frag);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (err < 0)
176 goto fail;
177
178 89 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (err < 0) {
180 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
181 goto fail;
182 }
183 }
184
185 105 err = 0;
186 105 fail:
187 105 ff_cbs_fragment_reset(frag);
188 105 return err;
189 }
190
191 105 av_cold void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
192 {
193 105 CBSBSFContext *ctx = bsf->priv_data;
194
195 105 ff_cbs_fragment_free(&ctx->fragment);
196 105 ff_cbs_close(&ctx->input);
197 105 ff_cbs_close(&ctx->output);
198 105 }
199