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