FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/remove_extradata.c
Date: 2024-11-12 23:50:39
Exec Total Coverage
Lines: 22 117 18.8%
Functions: 2 7 28.6%
Branches: 18 103 17.5%

Line Branch Exec Source
1 /*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
23
24 #include "av1_parse.h"
25 #include "bsf.h"
26 #include "bsf_internal.h"
27 #include "h264.h"
28 #include "startcode.h"
29 #include "vc1_common.h"
30
31 #include "hevc/hevc.h"
32
33 enum RemoveFreq {
34 REMOVE_FREQ_KEYFRAME,
35 REMOVE_FREQ_ALL,
36 REMOVE_FREQ_NONKEYFRAME,
37 };
38
39 #define START_CODE 0x000001
40
41 typedef struct RemoveExtradataContext {
42 const AVClass *class;
43 int freq;
44 } RemoveExtradataContext;
45
46 static int av1_split(const uint8_t *buf, int buf_size, void *logctx)
47 {
48 AV1OBU obu;
49 const uint8_t *ptr = buf, *end = buf + buf_size;
50
51 while (ptr < end) {
52 int len = ff_av1_extract_obu(&obu, ptr, buf_size, logctx);
53 if (len < 0)
54 break;
55
56 if (obu.type == AV1_OBU_FRAME_HEADER ||
57 obu.type == AV1_OBU_FRAME) {
58 return ptr - buf;
59 }
60 ptr += len;
61 buf_size -= len;
62 }
63
64 return 0;
65 }
66
67 static int h264_split(const uint8_t *buf, int buf_size)
68 {
69 const uint8_t *ptr = buf, *end = buf + buf_size;
70 uint32_t state = -1;
71 int has_sps = 0;
72 int has_pps = 0;
73 int nalu_type;
74
75 while (ptr < end) {
76 ptr = avpriv_find_start_code(ptr, end, &state);
77 if ((state & 0xFFFFFF00) != 0x100)
78 break;
79 nalu_type = state & 0x1F;
80 if (nalu_type == H264_NAL_SPS) {
81 has_sps = 1;
82 } else if (nalu_type == H264_NAL_PPS)
83 has_pps = 1;
84 /* else if (nalu_type == 0x01 ||
85 * nalu_type == 0x02 ||
86 * nalu_type == 0x05) {
87 * }
88 */
89 else if ((nalu_type != H264_NAL_SEI || has_pps) &&
90 nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
91 nalu_type != 0x0f) {
92 if (has_sps) {
93 while (ptr - 4 > buf && ptr[-5] == 0)
94 ptr--;
95 return ptr - 4 - buf;
96 }
97 }
98 }
99
100 return 0;
101 }
102
103 // Split after the parameter sets at the beginning of the stream if they exist.
104 static int hevc_split(const uint8_t *buf, int buf_size)
105 {
106 const uint8_t *ptr = buf, *end = buf + buf_size;
107 uint32_t state = -1;
108 int has_vps = 0;
109 int has_sps = 0;
110 int has_pps = 0;
111 int nut;
112
113 while (ptr < end) {
114 ptr = avpriv_find_start_code(ptr, end, &state);
115 if ((state >> 8) != START_CODE)
116 break;
117 nut = (state >> 1) & 0x3F;
118 if (nut == HEVC_NAL_VPS)
119 has_vps = 1;
120 else if (nut == HEVC_NAL_SPS)
121 has_sps = 1;
122 else if (nut == HEVC_NAL_PPS)
123 has_pps = 1;
124 else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
125 nut != HEVC_NAL_AUD) {
126 if (has_vps && has_sps) {
127 while (ptr - 4 > buf && ptr[-5] == 0)
128 ptr--;
129 return ptr - 4 - buf;
130 }
131 }
132 }
133 return 0;
134 }
135
136 static int mpegvideo_split(const uint8_t *buf, int buf_size)
137 {
138 uint32_t state = -1;
139 int found = 0;
140
141 for (int i = 0; i < buf_size; i++) {
142 state = (state << 8) | buf[i];
143 if (state == 0x1B3) {
144 found = 1;
145 } else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100)
146 return i - 3;
147 }
148 return 0;
149 }
150
151 50 static int mpeg4video_split(const uint8_t *buf, int buf_size)
152 {
153 50 const uint8_t *ptr = buf, *end = buf + buf_size;
154 50 uint32_t state = -1;
155
156
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 while (ptr < end) {
157 74 ptr = avpriv_find_start_code(ptr, end, &state);
158
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 24 times.
74 if (state == 0x1B3 || state == 0x1B6)
159 50 return ptr - 4 - buf;
160 }
161
162 return 0;
163 }
164
165 static int vc1_split(const uint8_t *buf, int buf_size)
166 {
167 const uint8_t *ptr = buf, *end = buf + buf_size;
168 uint32_t state = -1;
169 int charged = 0;
170
171 while (ptr < end) {
172 ptr = avpriv_find_start_code(ptr, end, &state);
173 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
174 charged = 1;
175 } else if (charged && IS_MARKER(state))
176 return ptr - 4 - buf;
177 }
178
179 return 0;
180 }
181
182 153 static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
183 {
184 153 RemoveExtradataContext *s = ctx->priv_data;
185
186 int ret;
187
188 153 ret = ff_bsf_get_packet_ref(ctx, pkt);
189
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 75 times.
153 if (ret < 0)
190 78 return ret;
191
192
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 25 times.
75 if (s->freq == REMOVE_FREQ_ALL ||
193
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 22 times.
50 (s->freq == REMOVE_FREQ_NONKEYFRAME && !(pkt->flags & AV_PKT_FLAG_KEY)) ||
194
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 22 times.
28 (s->freq == REMOVE_FREQ_KEYFRAME && pkt->flags & AV_PKT_FLAG_KEY)) {
195 int i;
196
197
1/7
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
50 switch (ctx->par_in->codec_id) {
198 case AV_CODEC_ID_AV1:
199 i = av1_split(pkt->data, pkt->size, ctx);
200 break;
201 50 case AV_CODEC_ID_AVS2:
202 case AV_CODEC_ID_AVS3:
203 case AV_CODEC_ID_CAVS:
204 case AV_CODEC_ID_MPEG4:
205 50 i = mpeg4video_split(pkt->data, pkt->size);
206 50 break;
207 case AV_CODEC_ID_H264:
208 i = h264_split(pkt->data, pkt->size);
209 break;
210 case AV_CODEC_ID_HEVC:
211 i = hevc_split(pkt->data, pkt->size);
212 break;
213 case AV_CODEC_ID_MPEG1VIDEO:
214 case AV_CODEC_ID_MPEG2VIDEO:
215 i = mpegvideo_split(pkt->data, pkt->size);
216 break;
217 case AV_CODEC_ID_VC1:
218 i = vc1_split(pkt->data, pkt->size);
219 break;
220 default:
221 i = 0;
222 }
223
224 50 pkt->data += i;
225 50 pkt->size -= i;
226 }
227
228 75 return 0;
229 }
230
231 #define OFFSET(x) offsetof(RemoveExtradataContext, x)
232 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
233 static const AVOption options[] = {
234 { "freq", NULL, OFFSET(freq), AV_OPT_TYPE_INT, { .i64 = REMOVE_FREQ_KEYFRAME }, REMOVE_FREQ_KEYFRAME, REMOVE_FREQ_NONKEYFRAME, FLAGS, .unit = "freq" },
235 { "k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_NONKEYFRAME }, .flags = FLAGS, .unit = "freq" },
236 { "keyframe", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_KEYFRAME }, .flags = FLAGS, .unit = "freq" },
237 { "e", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .flags = FLAGS, .unit = "freq" },
238 { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .flags = FLAGS, .unit = "freq" },
239 { NULL },
240 };
241
242 static const AVClass remove_extradata_class = {
243 .class_name = "remove_extradata",
244 .item_name = av_default_item_name,
245 .option = options,
246 .version = LIBAVUTIL_VERSION_INT,
247 };
248
249 const FFBitStreamFilter ff_remove_extradata_bsf = {
250 .p.name = "remove_extra",
251 .p.priv_class = &remove_extradata_class,
252 .priv_data_size = sizeof(RemoveExtradataContext),
253 .filter = remove_extradata,
254 };
255