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