| 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 | 6398 | static int val_in_array(const int *arr, size_t len, int val) | |
| 55 | { | ||
| 56 |
2/2✓ Branch 0 taken 14867 times.
✓ Branch 1 taken 3546 times.
|
18413 | for (size_t i = 0; i < len; i++) |
| 57 |
2/2✓ Branch 0 taken 2852 times.
✓ Branch 1 taken 12015 times.
|
14867 | if (arr[i] == val) |
| 58 | 2852 | return 1; | |
| 59 | 3546 | 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 | 571 | 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 | 571 | ExtractExtradataContext *s = ctx->priv_data; | |
| 178 | |||
| 179 | 571 | int extradata_size = 0, filtered_size = 0; | |
| 180 | const int *extradata_nal_types; | ||
| 181 | size_t nb_extradata_nal_types; | ||
| 182 | 571 | int i, has_sps = 0, has_vps = 0, ret = 0; | |
| 183 | |||
| 184 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 511 times.
|
571 | if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) { |
| 185 | 60 | extradata_nal_types = extradata_nal_types_vvc; | |
| 186 | 60 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_vvc); | |
| 187 |
2/2✓ Branch 0 taken 244 times.
✓ Branch 1 taken 267 times.
|
511 | } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { |
| 188 | 244 | extradata_nal_types = extradata_nal_types_hevc; | |
| 189 | 244 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc); | |
| 190 | } else { | ||
| 191 | 267 | extradata_nal_types = extradata_nal_types_h264; | |
| 192 | 267 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264); | |
| 193 | } | ||
| 194 | |||
| 195 | 571 | ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size, | |
| 196 | 571 | ctx, 0, ctx->par_in->codec_id, H2645_FLAG_SMALL_PADDING); | |
| 197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 571 times.
|
571 | if (ret < 0) |
| 198 | ✗ | return ret; | |
| 199 | |||
| 200 |
2/2✓ Branch 0 taken 3249 times.
✓ Branch 1 taken 571 times.
|
3820 | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { |
| 201 | 3249 | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
| 202 |
2/2✓ Branch 1 taken 1397 times.
✓ Branch 2 taken 1852 times.
|
3249 | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) { |
| 203 | 1397 | extradata_size += nal->raw_size + 3; | |
| 204 |
2/2✓ Branch 0 taken 191 times.
✓ Branch 1 taken 1206 times.
|
1397 | if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) { |
| 205 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 127 times.
|
191 | if (nal->type == VVC_SPS_NUT) has_sps = 1; |
| 206 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 187 times.
|
191 | if (nal->type == VVC_VPS_NUT) has_vps = 1; |
| 207 |
2/2✓ Branch 0 taken 735 times.
✓ Branch 1 taken 471 times.
|
1206 | } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { |
| 208 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 510 times.
|
735 | if (nal->type == HEVC_NAL_SPS) has_sps = 1; |
| 209 |
2/2✓ Branch 0 taken 221 times.
✓ Branch 1 taken 514 times.
|
735 | if (nal->type == HEVC_NAL_VPS) has_vps = 1; |
| 210 | } else { | ||
| 211 |
2/2✓ Branch 0 taken 231 times.
✓ Branch 1 taken 240 times.
|
471 | if (nal->type == H264_NAL_SPS) has_sps = 1; |
| 212 | } | ||
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1852 times.
|
1852 | } else if (s->remove) { |
| 214 | ✗ | filtered_size += nal->raw_size + 3; | |
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 |
2/2✓ Branch 0 taken 502 times.
✓ Branch 1 taken 69 times.
|
571 | if (extradata_size && |
| 219 |
3/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 442 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
|
502 | ((ctx->par_in->codec_id == AV_CODEC_ID_VVC && has_sps) || |
| 220 |
4/6✓ Branch 0 taken 211 times.
✓ Branch 1 taken 231 times.
✓ Branch 2 taken 211 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 211 times.
|
442 | (ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) || |
| 221 |
2/4✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 231 times.
✗ Branch 3 not taken.
|
231 | (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) { |
| 222 | 502 | 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 502 times.
|
502 | 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 | 502 | extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 502 times.
|
502 | if (!extradata) { |
| 236 | ✗ | av_buffer_unref(&filtered_buf); | |
| 237 | ✗ | return AVERROR(ENOMEM); | |
| 238 | } | ||
| 239 | |||
| 240 | 502 | *data = extradata; | |
| 241 | 502 | *size = extradata_size; | |
| 242 | |||
| 243 | 502 | bytestream2_init_writer(&pb_extradata, extradata, extradata_size); | |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 502 times.
|
502 | if (s->remove) |
| 245 | ✗ | bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size); | |
| 246 | |||
| 247 |
2/2✓ Branch 0 taken 2922 times.
✓ Branch 1 taken 502 times.
|
3424 | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { |
| 248 | 2922 | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
| 249 |
2/2✓ Branch 1 taken 1397 times.
✓ Branch 2 taken 1525 times.
|
2922 | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, |
| 250 | nal->type)) { | ||
| 251 | 1397 | bytestream2_put_be24u(&pb_extradata, 1); //startcode | |
| 252 | 1397 | bytestream2_put_bufferu(&pb_extradata, nal->raw_data, nal->raw_size); | |
| 253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1525 times.
|
1525 | } 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 502 times.
|
502 | 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 | 571 | 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 | 459 | static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt, | |
| 306 | uint8_t **data, int *size) | ||
| 307 | { | ||
| 308 | 459 | ExtractExtradataContext *s = ctx->priv_data; | |
| 309 | 459 | uint32_t state = UINT32_MAX; | |
| 310 | 459 | int i, found = 0; | |
| 311 | |||
| 312 |
2/2✓ Branch 0 taken 4184922 times.
✓ Branch 1 taken 265 times.
|
4185187 | for (i = 0; i < pkt->size; i++) { |
| 313 | 4184922 | state = (state << 8) | pkt->data[i]; | |
| 314 |
2/2✓ Branch 0 taken 194 times.
✓ Branch 1 taken 4184728 times.
|
4184922 | if (state == 0x1B3) |
| 315 | 194 | found = 1; | |
| 316 |
8/8✓ Branch 0 taken 5835 times.
✓ Branch 1 taken 4178893 times.
✓ Branch 2 taken 5635 times.
✓ Branch 3 taken 200 times.
✓ Branch 4 taken 463 times.
✓ Branch 5 taken 5172 times.
✓ Branch 6 taken 194 times.
✓ Branch 7 taken 269 times.
|
4184728 | else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) { |
| 317 | 194 | *size = i - 3; | |
| 318 | 194 | *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
|
194 | if (!*data) |
| 320 | ✗ | return AVERROR(ENOMEM); | |
| 321 | |||
| 322 | 194 | memcpy(*data, pkt->data, *size); | |
| 323 | |||
| 324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
|
194 | if (s->remove) { |
| 325 | ✗ | pkt->data += *size; | |
| 326 | ✗ | pkt->size -= *size; | |
| 327 | } | ||
| 328 | 194 | break; | |
| 329 | } | ||
| 330 | } | ||
| 331 | 459 | return 0; | |
| 332 | } | ||
| 333 | |||
| 334 | 121 | static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt, | |
| 335 | uint8_t **data, int *size) | ||
| 336 | { | ||
| 337 | 121 | ExtractExtradataContext *s = ctx->priv_data; | |
| 338 | 121 | const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size; | |
| 339 | 121 | uint32_t state = UINT32_MAX; | |
| 340 | |||
| 341 |
1/2✓ Branch 0 taken 397 times.
✗ Branch 1 not taken.
|
397 | while (ptr < end) { |
| 342 | 397 | ptr = avpriv_find_start_code(ptr, end, &state); | |
| 343 |
4/4✓ Branch 0 taken 325 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 276 times.
|
397 | if (state == 0x1B3 || state == 0x1B6) { |
| 344 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 50 times.
|
121 | if (ptr - pkt->data > 4) { |
| 345 | 71 | *size = ptr - 4 - pkt->data; | |
| 346 | 71 | *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!*data) |
| 348 | ✗ | return AVERROR(ENOMEM); | |
| 349 | |||
| 350 | 71 | memcpy(*data, pkt->data, *size); | |
| 351 | |||
| 352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (s->remove) { |
| 353 | ✗ | pkt->data += *size; | |
| 354 | ✗ | pkt->size -= *size; | |
| 355 | } | ||
| 356 | } | ||
| 357 | 121 | break; | |
| 358 | } | ||
| 359 | } | ||
| 360 | 121 | 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 | 793 | static int extract_extradata_init(AVBSFContext *ctx) | |
| 382 | { | ||
| 383 | 793 | ExtractExtradataContext *s = ctx->priv_data; | |
| 384 | int i; | ||
| 385 | |||
| 386 |
1/2✓ Branch 0 taken 5320 times.
✗ Branch 1 not taken.
|
5320 | for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) { |
| 387 |
2/2✓ Branch 0 taken 793 times.
✓ Branch 1 taken 4527 times.
|
5320 | if (extract_tab[i].id == ctx->par_in->codec_id) { |
| 388 | 793 | s->extract = extract_tab[i].extract; | |
| 389 | 793 | break; | |
| 390 | } | ||
| 391 | } | ||
| 392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 793 times.
|
793 | if (!s->extract) |
| 393 | ✗ | return AVERROR_BUG; | |
| 394 | |||
| 395 | 793 | return 0; | |
| 396 | } | ||
| 397 | |||
| 398 | 1610 | static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt) | |
| 399 | { | ||
| 400 | 1610 | ExtractExtradataContext *s = ctx->priv_data; | |
| 401 | 1610 | uint8_t *extradata = NULL; | |
| 402 | int extradata_size; | ||
| 403 | 1610 | int ret = 0; | |
| 404 | |||
| 405 | 1610 | ret = ff_bsf_get_packet_ref(ctx, pkt); | |
| 406 |
2/2✓ Branch 0 taken 409 times.
✓ Branch 1 taken 1201 times.
|
1610 | if (ret < 0) |
| 407 | 409 | return ret; | |
| 408 | |||
| 409 | 1201 | ret = s->extract(ctx, pkt, &extradata, &extradata_size); | |
| 410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (ret < 0) |
| 411 | ✗ | goto fail; | |
| 412 | |||
| 413 |
2/2✓ Branch 0 taken 794 times.
✓ Branch 1 taken 407 times.
|
1201 | if (extradata) { |
| 414 | 794 | memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 415 | 794 | 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 794 times.
|
794 | if (ret < 0) { |
| 418 | ✗ | av_freep(&extradata); | |
| 419 | ✗ | goto fail; | |
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | 1201 | return 0; | |
| 424 | |||
| 425 | ✗ | fail: | |
| 426 | ✗ | av_packet_unref(pkt); | |
| 427 | ✗ | return ret; | |
| 428 | } | ||
| 429 | |||
| 430 | 793 | static void extract_extradata_close(AVBSFContext *ctx) | |
| 431 | { | ||
| 432 | 793 | ExtractExtradataContext *s = ctx->priv_data; | |
| 433 | 793 | ff_av1_packet_uninit(&s->av1_pkt); | |
| 434 | 793 | ff_h2645_packet_uninit(&s->h2645_pkt); | |
| 435 | 793 | } | |
| 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 |