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 <stdint.h> | ||
20 | |||
21 | #include "libavutil/log.h" | ||
22 | #include "libavutil/mem.h" | ||
23 | #include "libavutil/opt.h" | ||
24 | |||
25 | #include "av1.h" | ||
26 | #include "av1_parse.h" | ||
27 | #include "bsf.h" | ||
28 | #include "bsf_internal.h" | ||
29 | #include "bytestream.h" | ||
30 | #include "h2645_parse.h" | ||
31 | #include "h264.h" | ||
32 | #include "startcode.h" | ||
33 | #include "vc1_common.h" | ||
34 | #include "vvc.h" | ||
35 | |||
36 | #include "hevc/hevc.h" | ||
37 | |||
38 | typedef struct ExtractExtradataContext { | ||
39 | const AVClass *class; | ||
40 | |||
41 | int (*extract)(AVBSFContext *ctx, AVPacket *pkt, | ||
42 | uint8_t **data, int *size); | ||
43 | |||
44 | /* AV1 specific fields */ | ||
45 | AV1Packet av1_pkt; | ||
46 | |||
47 | /* H264/HEVC specific fields */ | ||
48 | H2645Packet h2645_pkt; | ||
49 | |||
50 | /* AVOptions */ | ||
51 | int remove; | ||
52 | } ExtractExtradataContext; | ||
53 | |||
54 | 6144 | static int val_in_array(const int *arr, size_t len, int val) | |
55 | { | ||
56 |
2/2✓ Branch 0 taken 14159 times.
✓ Branch 1 taken 3474 times.
|
17633 | for (size_t i = 0; i < len; i++) |
57 |
2/2✓ Branch 0 taken 2670 times.
✓ Branch 1 taken 11489 times.
|
14159 | if (arr[i] == val) |
58 | 2670 | return 1; | |
59 | 3474 | return 0; | |
60 | } | ||
61 | |||
62 | 8 | static int metadata_is_global(const AV1OBU *obu) | |
63 | { | ||
64 | static const int metadata_obu_types[] = { | ||
65 | AV1_METADATA_TYPE_HDR_CLL, AV1_METADATA_TYPE_HDR_MDCV, | ||
66 | }; | ||
67 | GetBitContext gb; | ||
68 | int metadata_type; | ||
69 | |||
70 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (init_get_bits(&gb, obu->data, obu->size_bits) < 0) |
71 | ✗ | return 0; | |
72 | |||
73 | 8 | metadata_type = get_leb(&gb); | |
74 | |||
75 | 8 | return val_in_array(metadata_obu_types, FF_ARRAY_ELEMS(metadata_obu_types), | |
76 | metadata_type); | ||
77 | } | ||
78 | |||
79 | 219 | static int obu_is_global(const AV1OBU *obu) | |
80 | { | ||
81 | static const int extradata_obu_types[] = { | ||
82 | AV1_OBU_SEQUENCE_HEADER, AV1_OBU_METADATA, | ||
83 | }; | ||
84 | |||
85 |
2/2✓ Branch 0 taken 169 times.
✓ Branch 1 taken 50 times.
|
219 | if (!val_in_array(extradata_obu_types, FF_ARRAY_ELEMS(extradata_obu_types), |
86 | 219 | obu->type)) | |
87 | 169 | return 0; | |
88 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 8 times.
|
50 | if (obu->type != AV1_OBU_METADATA) |
89 | 42 | return 1; | |
90 | |||
91 | 8 | return metadata_is_global(obu); | |
92 | } | ||
93 | |||
94 | 44 | static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt, | |
95 | uint8_t **data, int *size) | ||
96 | { | ||
97 | |||
98 | 44 | ExtractExtradataContext *s = ctx->priv_data; | |
99 | |||
100 | 44 | int extradata_size = 0, filtered_size = 0; | |
101 | 44 | int i, has_seq = 0, ret = 0; | |
102 | |||
103 | 44 | ret = ff_av1_packet_split(&s->av1_pkt, pkt->data, pkt->size, ctx); | |
104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (ret < 0) |
105 | ✗ | return ret; | |
106 | |||
107 |
2/2✓ Branch 0 taken 137 times.
✓ Branch 1 taken 44 times.
|
181 | for (i = 0; i < s->av1_pkt.nb_obus; i++) { |
108 | 137 | AV1OBU *obu = &s->av1_pkt.obus[i]; | |
109 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 112 times.
|
137 | if (obu_is_global(obu)) { |
110 | 25 | extradata_size += obu->raw_size; | |
111 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 4 times.
|
25 | if (obu->type == AV1_OBU_SEQUENCE_HEADER) |
112 | 21 | has_seq = 1; | |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | } else if (s->remove) { |
114 | ✗ | filtered_size += obu->raw_size; | |
115 | } | ||
116 | } | ||
117 | |||
118 |
3/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
44 | if (extradata_size && has_seq) { |
119 | 21 | AVBufferRef *filtered_buf = NULL; | |
120 | PutByteContext pb_filtered_data, pb_extradata; | ||
121 | uint8_t *extradata; | ||
122 | |||
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->remove) { |
124 | ✗ | filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
125 | ✗ | if (!filtered_buf) { | |
126 | ✗ | return AVERROR(ENOMEM); | |
127 | } | ||
128 | ✗ | memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
129 | } | ||
130 | |||
131 | 21 | extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!extradata) { |
133 | ✗ | av_buffer_unref(&filtered_buf); | |
134 | ✗ | return AVERROR(ENOMEM); | |
135 | } | ||
136 | |||
137 | 21 | *data = extradata; | |
138 | 21 | *size = extradata_size; | |
139 | |||
140 | 21 | bytestream2_init_writer(&pb_extradata, extradata, extradata_size); | |
141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->remove) |
142 | ✗ | bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size); | |
143 | |||
144 |
2/2✓ Branch 0 taken 82 times.
✓ Branch 1 taken 21 times.
|
103 | for (i = 0; i < s->av1_pkt.nb_obus; i++) { |
145 | 82 | AV1OBU *obu = &s->av1_pkt.obus[i]; | |
146 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 57 times.
|
82 | if (obu_is_global(obu)) { |
147 | 25 | bytestream2_put_bufferu(&pb_extradata, obu->raw_data, obu->raw_size); | |
148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | } else if (s->remove) { |
149 | ✗ | bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size); | |
150 | } | ||
151 | } | ||
152 | |||
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->remove) { |
154 | ✗ | av_buffer_unref(&pkt->buf); | |
155 | ✗ | pkt->buf = filtered_buf; | |
156 | ✗ | pkt->data = filtered_buf->data; | |
157 | ✗ | pkt->size = filtered_size; | |
158 | } | ||
159 | } | ||
160 | |||
161 | 44 | return 0; | |
162 | } | ||
163 | |||
164 | 560 | static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt, | |
165 | uint8_t **data, int *size) | ||
166 | { | ||
167 | static const int extradata_nal_types_vvc[] = { | ||
168 | VVC_VPS_NUT, VVC_SPS_NUT, VVC_PPS_NUT, | ||
169 | }; | ||
170 | static const int extradata_nal_types_hevc[] = { | ||
171 | HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS, | ||
172 | }; | ||
173 | static const int extradata_nal_types_h264[] = { | ||
174 | H264_NAL_SPS, H264_NAL_PPS, | ||
175 | }; | ||
176 | |||
177 | 560 | ExtractExtradataContext *s = ctx->priv_data; | |
178 | |||
179 | 560 | int extradata_size = 0, filtered_size = 0; | |
180 | const int *extradata_nal_types; | ||
181 | size_t nb_extradata_nal_types; | ||
182 | 560 | int i, has_sps = 0, has_vps = 0, ret = 0; | |
183 | |||
184 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 506 times.
|
560 | if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) { |
185 | 54 | extradata_nal_types = extradata_nal_types_vvc; | |
186 | 54 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_vvc); | |
187 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 266 times.
|
506 | } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { |
188 | 240 | extradata_nal_types = extradata_nal_types_hevc; | |
189 | 240 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc); | |
190 | } else { | ||
191 | 266 | extradata_nal_types = extradata_nal_types_h264; | |
192 | 266 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264); | |
193 | } | ||
194 | |||
195 | 560 | ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size, | |
196 | 560 | ctx, 0, ctx->par_in->codec_id, H2645_FLAG_SMALL_PADDING); | |
197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
|
560 | if (ret < 0) |
198 | ✗ | return ret; | |
199 | |||
200 |
2/2✓ Branch 0 taken 3122 times.
✓ Branch 1 taken 560 times.
|
3682 | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { |
201 | 3122 | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
202 |
2/2✓ Branch 1 taken 1306 times.
✓ Branch 2 taken 1816 times.
|
3122 | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) { |
203 | 1306 | extradata_size += nal->raw_size + 3; | |
204 |
2/2✓ Branch 0 taken 115 times.
✓ Branch 1 taken 1191 times.
|
1306 | if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) { |
205 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 57 times.
|
115 | if (nal->type == VVC_SPS_NUT) has_sps = 1; |
206 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 112 times.
|
115 | if (nal->type == VVC_VPS_NUT) has_vps = 1; |
207 |
2/2✓ Branch 0 taken 723 times.
✓ Branch 1 taken 468 times.
|
1191 | } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { |
208 |
2/2✓ Branch 0 taken 221 times.
✓ Branch 1 taken 502 times.
|
723 | if (nal->type == HEVC_NAL_SPS) has_sps = 1; |
209 |
2/2✓ Branch 0 taken 217 times.
✓ Branch 1 taken 506 times.
|
723 | if (nal->type == HEVC_NAL_VPS) has_vps = 1; |
210 | } else { | ||
211 |
2/2✓ Branch 0 taken 230 times.
✓ Branch 1 taken 238 times.
|
468 | if (nal->type == H264_NAL_SPS) has_sps = 1; |
212 | } | ||
213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1816 times.
|
1816 | } else if (s->remove) { |
214 | ✗ | filtered_size += nal->raw_size + 3; | |
215 | } | ||
216 | } | ||
217 | |||
218 |
2/2✓ Branch 0 taken 491 times.
✓ Branch 1 taken 69 times.
|
560 | if (extradata_size && |
219 |
3/4✓ Branch 0 taken 54 times.
✓ Branch 1 taken 437 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
|
491 | ((ctx->par_in->codec_id == AV_CODEC_ID_VVC && has_sps) || |
220 |
4/6✓ Branch 0 taken 207 times.
✓ Branch 1 taken 230 times.
✓ Branch 2 taken 207 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 207 times.
|
437 | (ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) || |
221 |
2/4✓ Branch 0 taken 230 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 230 times.
✗ Branch 3 not taken.
|
230 | (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) { |
222 | 491 | AVBufferRef *filtered_buf = NULL; | |
223 | PutByteContext pb_filtered_data, pb_extradata; | ||
224 | uint8_t *extradata; | ||
225 | |||
226 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
|
491 | if (s->remove) { |
227 | ✗ | filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
228 | ✗ | if (!filtered_buf) { | |
229 | ✗ | return AVERROR(ENOMEM); | |
230 | } | ||
231 | ✗ | memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
232 | } | ||
233 | |||
234 | 491 | extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
|
491 | if (!extradata) { |
236 | ✗ | av_buffer_unref(&filtered_buf); | |
237 | ✗ | return AVERROR(ENOMEM); | |
238 | } | ||
239 | |||
240 | 491 | *data = extradata; | |
241 | 491 | *size = extradata_size; | |
242 | |||
243 | 491 | bytestream2_init_writer(&pb_extradata, extradata, extradata_size); | |
244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
|
491 | if (s->remove) |
245 | ✗ | bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size); | |
246 | |||
247 |
2/2✓ Branch 0 taken 2795 times.
✓ Branch 1 taken 491 times.
|
3286 | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { |
248 | 2795 | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
249 |
2/2✓ Branch 1 taken 1306 times.
✓ Branch 2 taken 1489 times.
|
2795 | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, |
250 | nal->type)) { | ||
251 | 1306 | bytestream2_put_be24u(&pb_extradata, 1); //startcode | |
252 | 1306 | bytestream2_put_bufferu(&pb_extradata, nal->raw_data, nal->raw_size); | |
253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1489 times.
|
1489 | } else if (s->remove) { |
254 | ✗ | bytestream2_put_be24u(&pb_filtered_data, 1); // startcode | |
255 | ✗ | bytestream2_put_bufferu(&pb_filtered_data, nal->raw_data, nal->raw_size); | |
256 | } | ||
257 | } | ||
258 | |||
259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
|
491 | if (s->remove) { |
260 | ✗ | av_buffer_unref(&pkt->buf); | |
261 | ✗ | pkt->buf = filtered_buf; | |
262 | ✗ | pkt->data = filtered_buf->data; | |
263 | ✗ | pkt->size = filtered_size; | |
264 | } | ||
265 | } | ||
266 | |||
267 | 560 | return 0; | |
268 | } | ||
269 | |||
270 | 6 | static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt, | |
271 | uint8_t **data, int *size) | ||
272 | { | ||
273 | 6 | ExtractExtradataContext *s = ctx->priv_data; | |
274 | 6 | const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size; | |
275 | 6 | uint32_t state = UINT32_MAX; | |
276 | 6 | int has_extradata = 0, extradata_size = 0; | |
277 | |||
278 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | while (ptr < end) { |
279 | 23 | ptr = avpriv_find_start_code(ptr, end, &state); | |
280 |
4/4✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 6 times.
|
23 | if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) { |
281 | 17 | has_extradata = 1; | |
282 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | } else if (has_extradata && IS_MARKER(state)) { |
283 | 6 | extradata_size = ptr - 4 - pkt->data; | |
284 | 6 | break; | |
285 | } | ||
286 | } | ||
287 | |||
288 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (extradata_size) { |
289 | 6 | *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!*data) |
291 | ✗ | return AVERROR(ENOMEM); | |
292 | |||
293 | 6 | memcpy(*data, pkt->data, extradata_size); | |
294 | 6 | *size = extradata_size; | |
295 | |||
296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (s->remove) { |
297 | ✗ | pkt->data += extradata_size; | |
298 | ✗ | pkt->size -= extradata_size; | |
299 | } | ||
300 | } | ||
301 | |||
302 | 6 | return 0; | |
303 | } | ||
304 | |||
305 | 454 | static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt, | |
306 | uint8_t **data, int *size) | ||
307 | { | ||
308 | 454 | ExtractExtradataContext *s = ctx->priv_data; | |
309 | 454 | uint32_t state = UINT32_MAX; | |
310 | 454 | int i, found = 0; | |
311 | |||
312 |
2/2✓ Branch 0 taken 4184614 times.
✓ Branch 1 taken 265 times.
|
4184879 | for (i = 0; i < pkt->size; i++) { |
313 | 4184614 | state = (state << 8) | pkt->data[i]; | |
314 |
2/2✓ Branch 0 taken 189 times.
✓ Branch 1 taken 4184425 times.
|
4184614 | if (state == 0x1B3) |
315 | 189 | found = 1; | |
316 |
8/8✓ Branch 0 taken 5547 times.
✓ Branch 1 taken 4178878 times.
✓ Branch 2 taken 5356 times.
✓ Branch 3 taken 191 times.
✓ Branch 4 taken 446 times.
✓ Branch 5 taken 4910 times.
✓ Branch 6 taken 189 times.
✓ Branch 7 taken 257 times.
|
4184425 | else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) { |
317 | 189 | *size = i - 3; | |
318 | 189 | *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); | |
319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 189 times.
|
189 | if (!*data) |
320 | ✗ | return AVERROR(ENOMEM); | |
321 | |||
322 | 189 | memcpy(*data, pkt->data, *size); | |
323 | |||
324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 189 times.
|
189 | if (s->remove) { |
325 | ✗ | pkt->data += *size; | |
326 | ✗ | pkt->size -= *size; | |
327 | } | ||
328 | 189 | break; | |
329 | } | ||
330 | } | ||
331 | 454 | return 0; | |
332 | } | ||
333 | |||
334 | 122 | static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt, | |
335 | uint8_t **data, int *size) | ||
336 | { | ||
337 | 122 | ExtractExtradataContext *s = ctx->priv_data; | |
338 | 122 | const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size; | |
339 | 122 | uint32_t state = UINT32_MAX; | |
340 | |||
341 |
1/2✓ Branch 0 taken 402 times.
✗ Branch 1 not taken.
|
402 | while (ptr < end) { |
342 | 402 | ptr = avpriv_find_start_code(ptr, end, &state); | |
343 |
4/4✓ Branch 0 taken 329 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 280 times.
|
402 | if (state == 0x1B3 || state == 0x1B6) { |
344 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 50 times.
|
122 | if (ptr - pkt->data > 4) { |
345 | 72 | *size = ptr - 4 - pkt->data; | |
346 | 72 | *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); | |
347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (!*data) |
348 | ✗ | return AVERROR(ENOMEM); | |
349 | |||
350 | 72 | memcpy(*data, pkt->data, *size); | |
351 | |||
352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (s->remove) { |
353 | ✗ | pkt->data += *size; | |
354 | ✗ | pkt->size -= *size; | |
355 | } | ||
356 | } | ||
357 | 122 | break; | |
358 | } | ||
359 | } | ||
360 | 122 | return 0; | |
361 | } | ||
362 | |||
363 | static const struct { | ||
364 | enum AVCodecID id; | ||
365 | int (*extract)(AVBSFContext *ctx, AVPacket *pkt, | ||
366 | uint8_t **data, int *size); | ||
367 | } extract_tab[] = { | ||
368 | { AV_CODEC_ID_AV1, extract_extradata_av1 }, | ||
369 | { AV_CODEC_ID_AVS2, extract_extradata_mpeg4 }, | ||
370 | { AV_CODEC_ID_AVS3, extract_extradata_mpeg4 }, | ||
371 | { AV_CODEC_ID_CAVS, extract_extradata_mpeg4 }, | ||
372 | { AV_CODEC_ID_H264, extract_extradata_h2645 }, | ||
373 | { AV_CODEC_ID_HEVC, extract_extradata_h2645 }, | ||
374 | { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12 }, | ||
375 | { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12 }, | ||
376 | { AV_CODEC_ID_MPEG4, extract_extradata_mpeg4 }, | ||
377 | { AV_CODEC_ID_VC1, extract_extradata_vc1 }, | ||
378 | { AV_CODEC_ID_VVC, extract_extradata_h2645 }, | ||
379 | }; | ||
380 | |||
381 | 778 | static int extract_extradata_init(AVBSFContext *ctx) | |
382 | { | ||
383 | 778 | ExtractExtradataContext *s = ctx->priv_data; | |
384 | int i; | ||
385 | |||
386 |
1/2✓ Branch 0 taken 5194 times.
✗ Branch 1 not taken.
|
5194 | for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) { |
387 |
2/2✓ Branch 0 taken 778 times.
✓ Branch 1 taken 4416 times.
|
5194 | if (extract_tab[i].id == ctx->par_in->codec_id) { |
388 | 778 | s->extract = extract_tab[i].extract; | |
389 | 778 | break; | |
390 | } | ||
391 | } | ||
392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 778 times.
|
778 | if (!s->extract) |
393 | ✗ | return AVERROR_BUG; | |
394 | |||
395 | 778 | return 0; | |
396 | } | ||
397 | |||
398 | 1595 | static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt) | |
399 | { | ||
400 | 1595 | ExtractExtradataContext *s = ctx->priv_data; | |
401 | 1595 | uint8_t *extradata = NULL; | |
402 | int extradata_size; | ||
403 | 1595 | int ret = 0; | |
404 | |||
405 | 1595 | ret = ff_bsf_get_packet_ref(ctx, pkt); | |
406 |
2/2✓ Branch 0 taken 409 times.
✓ Branch 1 taken 1186 times.
|
1595 | if (ret < 0) |
407 | 409 | return ret; | |
408 | |||
409 | 1186 | ret = s->extract(ctx, pkt, &extradata, &extradata_size); | |
410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1186 times.
|
1186 | if (ret < 0) |
411 | ✗ | goto fail; | |
412 | |||
413 |
2/2✓ Branch 0 taken 779 times.
✓ Branch 1 taken 407 times.
|
1186 | if (extradata) { |
414 | 779 | memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
415 | 779 | ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, | |
416 | extradata, extradata_size); | ||
417 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 779 times.
|
779 | if (ret < 0) { |
418 | ✗ | av_freep(&extradata); | |
419 | ✗ | goto fail; | |
420 | } | ||
421 | } | ||
422 | |||
423 | 1186 | return 0; | |
424 | |||
425 | ✗ | fail: | |
426 | ✗ | av_packet_unref(pkt); | |
427 | ✗ | return ret; | |
428 | } | ||
429 | |||
430 | 778 | static void extract_extradata_close(AVBSFContext *ctx) | |
431 | { | ||
432 | 778 | ExtractExtradataContext *s = ctx->priv_data; | |
433 | 778 | ff_av1_packet_uninit(&s->av1_pkt); | |
434 | 778 | ff_h2645_packet_uninit(&s->h2645_pkt); | |
435 | 778 | } | |
436 | |||
437 | static const enum AVCodecID codec_ids[] = { | ||
438 | AV_CODEC_ID_AV1, | ||
439 | AV_CODEC_ID_AVS2, | ||
440 | AV_CODEC_ID_AVS3, | ||
441 | AV_CODEC_ID_CAVS, | ||
442 | AV_CODEC_ID_H264, | ||
443 | AV_CODEC_ID_HEVC, | ||
444 | AV_CODEC_ID_MPEG1VIDEO, | ||
445 | AV_CODEC_ID_MPEG2VIDEO, | ||
446 | AV_CODEC_ID_MPEG4, | ||
447 | AV_CODEC_ID_VC1, | ||
448 | AV_CODEC_ID_VVC, | ||
449 | AV_CODEC_ID_NONE, | ||
450 | }; | ||
451 | |||
452 | #define OFFSET(x) offsetof(ExtractExtradataContext, x) | ||
453 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) | ||
454 | static const AVOption options[] = { | ||
455 | { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT, | ||
456 | { .i64 = 0 }, 0, 1, FLAGS }, | ||
457 | { NULL }, | ||
458 | }; | ||
459 | |||
460 | static const AVClass extract_extradata_class = { | ||
461 | .class_name = "extract_extradata", | ||
462 | .item_name = av_default_item_name, | ||
463 | .option = options, | ||
464 | .version = LIBAVUTIL_VERSION_INT, | ||
465 | }; | ||
466 | |||
467 | const FFBitStreamFilter ff_extract_extradata_bsf = { | ||
468 | .p.name = "extract_extradata", | ||
469 | .p.codec_ids = codec_ids, | ||
470 | .p.priv_class = &extract_extradata_class, | ||
471 | .priv_data_size = sizeof(ExtractExtradataContext), | ||
472 | .init = extract_extradata_init, | ||
473 | .filter = extract_extradata_filter, | ||
474 | .close = extract_extradata_close, | ||
475 | }; | ||
476 |