| 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 "lcevc.h" | ||
| 33 | #include "lcevc_parse.h" | ||
| 34 | #include "startcode.h" | ||
| 35 | #include "vc1_common.h" | ||
| 36 | #include "vvc.h" | ||
| 37 | |||
| 38 | #include "hevc/hevc.h" | ||
| 39 | |||
| 40 | typedef struct ExtractExtradataContext { | ||
| 41 | const AVClass *class; | ||
| 42 | |||
| 43 | int (*extract)(AVBSFContext *ctx, AVPacket *pkt, | ||
| 44 | uint8_t **data, int *size); | ||
| 45 | |||
| 46 | /* AV1 specific fields */ | ||
| 47 | AV1Packet av1_pkt; | ||
| 48 | |||
| 49 | /* H264/HEVC specific fields */ | ||
| 50 | H2645Packet h2645_pkt; | ||
| 51 | |||
| 52 | /* AVOptions */ | ||
| 53 | int remove; | ||
| 54 | } ExtractExtradataContext; | ||
| 55 | |||
| 56 | 6446 | static int val_in_array(const int *arr, size_t len, int val) | |
| 57 | { | ||
| 58 |
2/2✓ Branch 0 taken 14981 times.
✓ Branch 1 taken 3566 times.
|
18547 | for (size_t i = 0; i < len; i++) |
| 59 |
2/2✓ Branch 0 taken 2880 times.
✓ Branch 1 taken 12101 times.
|
14981 | if (arr[i] == val) |
| 60 | 2880 | return 1; | |
| 61 | 3566 | return 0; | |
| 62 | } | ||
| 63 | |||
| 64 | 8 | static int metadata_is_global(const AV1OBU *obu) | |
| 65 | { | ||
| 66 | static const int metadata_obu_types[] = { | ||
| 67 | AV1_METADATA_TYPE_HDR_CLL, AV1_METADATA_TYPE_HDR_MDCV, | ||
| 68 | }; | ||
| 69 | GetBitContext gb; | ||
| 70 | int metadata_type; | ||
| 71 | |||
| 72 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (init_get_bits(&gb, obu->data, obu->size_bits) < 0) |
| 73 | ✗ | return 0; | |
| 74 | |||
| 75 | 8 | metadata_type = get_leb(&gb); | |
| 76 | |||
| 77 | 8 | return val_in_array(metadata_obu_types, FF_ARRAY_ELEMS(metadata_obu_types), | |
| 78 | metadata_type); | ||
| 79 | } | ||
| 80 | |||
| 81 | 219 | static int obu_is_global(const AV1OBU *obu) | |
| 82 | { | ||
| 83 | static const int extradata_obu_types[] = { | ||
| 84 | AV1_OBU_SEQUENCE_HEADER, AV1_OBU_METADATA, | ||
| 85 | }; | ||
| 86 | |||
| 87 |
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), |
| 88 | 219 | obu->type)) | |
| 89 | 169 | return 0; | |
| 90 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 8 times.
|
50 | if (obu->type != AV1_OBU_METADATA) |
| 91 | 42 | return 1; | |
| 92 | |||
| 93 | 8 | return metadata_is_global(obu); | |
| 94 | } | ||
| 95 | |||
| 96 | 44 | static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt, | |
| 97 | uint8_t **data, int *size) | ||
| 98 | { | ||
| 99 | |||
| 100 | 44 | ExtractExtradataContext *s = ctx->priv_data; | |
| 101 | |||
| 102 | 44 | int extradata_size = 0, filtered_size = 0; | |
| 103 | 44 | int i, has_seq = 0, ret = 0; | |
| 104 | |||
| 105 | 44 | ret = ff_av1_packet_split(&s->av1_pkt, pkt->data, pkt->size, ctx); | |
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (ret < 0) |
| 107 | ✗ | return ret; | |
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 137 times.
✓ Branch 1 taken 44 times.
|
181 | for (i = 0; i < s->av1_pkt.nb_obus; i++) { |
| 110 | 137 | AV1OBU *obu = &s->av1_pkt.obus[i]; | |
| 111 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 112 times.
|
137 | if (obu_is_global(obu)) { |
| 112 | 25 | extradata_size += obu->raw_size; | |
| 113 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 4 times.
|
25 | if (obu->type == AV1_OBU_SEQUENCE_HEADER) |
| 114 | 21 | has_seq = 1; | |
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | } else if (s->remove) { |
| 116 | ✗ | filtered_size += obu->raw_size; | |
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 |
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) { |
| 121 | 21 | AVBufferRef *filtered_buf = NULL; | |
| 122 | PutByteContext pb_filtered_data, pb_extradata; | ||
| 123 | uint8_t *extradata; | ||
| 124 | |||
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->remove) { |
| 126 | ✗ | filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 127 | ✗ | if (!filtered_buf) { | |
| 128 | ✗ | return AVERROR(ENOMEM); | |
| 129 | } | ||
| 130 | ✗ | memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 131 | } | ||
| 132 | |||
| 133 | 21 | extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!extradata) { |
| 135 | ✗ | av_buffer_unref(&filtered_buf); | |
| 136 | ✗ | return AVERROR(ENOMEM); | |
| 137 | } | ||
| 138 | |||
| 139 | 21 | *data = extradata; | |
| 140 | 21 | *size = extradata_size; | |
| 141 | |||
| 142 | 21 | bytestream2_init_writer(&pb_extradata, extradata, extradata_size); | |
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->remove) |
| 144 | ✗ | bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size); | |
| 145 | |||
| 146 |
2/2✓ Branch 0 taken 82 times.
✓ Branch 1 taken 21 times.
|
103 | for (i = 0; i < s->av1_pkt.nb_obus; i++) { |
| 147 | 82 | AV1OBU *obu = &s->av1_pkt.obus[i]; | |
| 148 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 57 times.
|
82 | if (obu_is_global(obu)) { |
| 149 | 25 | bytestream2_put_bufferu(&pb_extradata, obu->raw_data, obu->raw_size); | |
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | } else if (s->remove) { |
| 151 | ✗ | bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size); | |
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->remove) { |
| 156 | ✗ | av_buffer_unref(&pkt->buf); | |
| 157 | ✗ | pkt->buf = filtered_buf; | |
| 158 | ✗ | pkt->data = filtered_buf->data; | |
| 159 | ✗ | pkt->size = filtered_size; | |
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | 44 | return 0; | |
| 164 | } | ||
| 165 | |||
| 166 | 576 | static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt, | |
| 167 | uint8_t **data, int *size) | ||
| 168 | { | ||
| 169 | static const int extradata_nal_types_vvc[] = { | ||
| 170 | VVC_VPS_NUT, VVC_SPS_NUT, VVC_PPS_NUT, | ||
| 171 | }; | ||
| 172 | static const int extradata_nal_types_hevc[] = { | ||
| 173 | HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS, | ||
| 174 | }; | ||
| 175 | static const int extradata_nal_types_h264[] = { | ||
| 176 | H264_NAL_SPS, H264_NAL_PPS, | ||
| 177 | }; | ||
| 178 | |||
| 179 | 576 | ExtractExtradataContext *s = ctx->priv_data; | |
| 180 | |||
| 181 | 576 | int extradata_size = 0, filtered_size = 0; | |
| 182 | const int *extradata_nal_types; | ||
| 183 | size_t nb_extradata_nal_types; | ||
| 184 | 576 | int i, has_sps = 0, has_vps = 0, ret = 0; | |
| 185 | |||
| 186 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 515 times.
|
576 | if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) { |
| 187 | 61 | extradata_nal_types = extradata_nal_types_vvc; | |
| 188 | 61 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_vvc); | |
| 189 |
2/2✓ Branch 0 taken 247 times.
✓ Branch 1 taken 268 times.
|
515 | } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { |
| 190 | 247 | extradata_nal_types = extradata_nal_types_hevc; | |
| 191 | 247 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc); | |
| 192 | } else { | ||
| 193 | 268 | extradata_nal_types = extradata_nal_types_h264; | |
| 194 | 268 | nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264); | |
| 195 | } | ||
| 196 | |||
| 197 | 576 | ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size, | |
| 198 | 576 | ctx, 0, ctx->par_in->codec_id, H2645_FLAG_SMALL_PADDING); | |
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 576 times.
|
576 | if (ret < 0) |
| 200 | ✗ | return ret; | |
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 3273 times.
✓ Branch 1 taken 576 times.
|
3849 | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { |
| 203 | 3273 | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
| 204 |
2/2✓ Branch 1 taken 1411 times.
✓ Branch 2 taken 1862 times.
|
3273 | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) { |
| 205 | 1411 | extradata_size += nal->raw_size + 3; | |
| 206 |
2/2✓ Branch 0 taken 193 times.
✓ Branch 1 taken 1218 times.
|
1411 | if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) { |
| 207 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 128 times.
|
193 | if (nal->type == VVC_SPS_NUT) has_sps = 1; |
| 208 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 189 times.
|
193 | if (nal->type == VVC_VPS_NUT) has_vps = 1; |
| 209 |
2/2✓ Branch 0 taken 745 times.
✓ Branch 1 taken 473 times.
|
1218 | } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { |
| 210 |
2/2✓ Branch 0 taken 229 times.
✓ Branch 1 taken 516 times.
|
745 | if (nal->type == HEVC_NAL_SPS) has_sps = 1; |
| 211 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 521 times.
|
745 | if (nal->type == HEVC_NAL_VPS) has_vps = 1; |
| 212 | } else { | ||
| 213 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 241 times.
|
473 | if (nal->type == H264_NAL_SPS) has_sps = 1; |
| 214 | } | ||
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1862 times.
|
1862 | } else if (s->remove) { |
| 216 | ✗ | filtered_size += nal->raw_size + 3; | |
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 |
2/2✓ Branch 0 taken 507 times.
✓ Branch 1 taken 69 times.
|
576 | if (extradata_size && |
| 221 |
3/4✓ Branch 0 taken 61 times.
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
|
507 | ((ctx->par_in->codec_id == AV_CODEC_ID_VVC && has_sps) || |
| 222 |
4/6✓ Branch 0 taken 214 times.
✓ Branch 1 taken 232 times.
✓ Branch 2 taken 214 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 214 times.
|
446 | (ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) || |
| 223 |
2/4✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
|
232 | (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) { |
| 224 | 507 | AVBufferRef *filtered_buf = NULL; | |
| 225 | PutByteContext pb_filtered_data, pb_extradata; | ||
| 226 | uint8_t *extradata; | ||
| 227 | |||
| 228 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
|
507 | if (s->remove) { |
| 229 | ✗ | filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 230 | ✗ | if (!filtered_buf) { | |
| 231 | ✗ | return AVERROR(ENOMEM); | |
| 232 | } | ||
| 233 | ✗ | memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 234 | } | ||
| 235 | |||
| 236 | 507 | extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
|
507 | if (!extradata) { |
| 238 | ✗ | av_buffer_unref(&filtered_buf); | |
| 239 | ✗ | return AVERROR(ENOMEM); | |
| 240 | } | ||
| 241 | |||
| 242 | 507 | *data = extradata; | |
| 243 | 507 | *size = extradata_size; | |
| 244 | |||
| 245 | 507 | bytestream2_init_writer(&pb_extradata, extradata, extradata_size); | |
| 246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
|
507 | if (s->remove) |
| 247 | ✗ | bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size); | |
| 248 | |||
| 249 |
2/2✓ Branch 0 taken 2946 times.
✓ Branch 1 taken 507 times.
|
3453 | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { |
| 250 | 2946 | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
| 251 |
2/2✓ Branch 1 taken 1411 times.
✓ Branch 2 taken 1535 times.
|
2946 | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, |
| 252 | nal->type)) { | ||
| 253 | 1411 | bytestream2_put_be24u(&pb_extradata, 1); //startcode | |
| 254 | 1411 | bytestream2_put_bufferu(&pb_extradata, nal->raw_data, nal->raw_size); | |
| 255 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1535 times.
|
1535 | } else if (s->remove) { |
| 256 | ✗ | bytestream2_put_be24u(&pb_filtered_data, 1); // startcode | |
| 257 | ✗ | bytestream2_put_bufferu(&pb_filtered_data, nal->raw_data, nal->raw_size); | |
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
|
507 | if (s->remove) { |
| 262 | ✗ | av_buffer_unref(&pkt->buf); | |
| 263 | ✗ | pkt->buf = filtered_buf; | |
| 264 | ✗ | pkt->data = filtered_buf->data; | |
| 265 | ✗ | pkt->size = filtered_size; | |
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | 576 | return 0; | |
| 270 | } | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Rewrite the NALu stripping the unneeded blocks. | ||
| 274 | * Given that length fields coded inside the NALu are not aware of any emulation_3bytes | ||
| 275 | * present in the bitstream, we need to keep track of the raw buffer as we navigate | ||
| 276 | * the stripped buffer. | ||
| 277 | */ | ||
| 278 | ✗ | static int write_lcevc_nalu(AVBSFContext *ctx, PutByteContext *pbc, const H2645NAL *nal, | |
| 279 | int remove) | ||
| 280 | { | ||
| 281 | GetByteContext gbc, raw_gbc; | ||
| 282 | ✗ | int sc = 0, gc = 0; | |
| 283 | ✗ | int skipped_byte_pos = 0; | |
| 284 | |||
| 285 | ✗ | bytestream2_init(&gbc, nal->data, nal->size); | |
| 286 | ✗ | bytestream2_init(&raw_gbc, nal->raw_data, nal->raw_size); | |
| 287 | ✗ | bytestream2_put_be16(pbc, bytestream2_get_be16(&gbc)); | |
| 288 | ✗ | bytestream2_skip(&raw_gbc, 2); | |
| 289 | |||
| 290 | ✗ | while (bytestream2_get_bytes_left(&gbc) > 1) { | |
| 291 | GetBitContext gb; | ||
| 292 | int payload_size_type, payload_type; | ||
| 293 | uint64_t payload_size; | ||
| 294 | int block_size, raw_block_size, block_end; | ||
| 295 | |||
| 296 | ✗ | init_get_bits8(&gb, gbc.buffer, bytestream2_get_bytes_left(&gbc)); | |
| 297 | |||
| 298 | ✗ | payload_size_type = get_bits(&gb, 3); | |
| 299 | ✗ | payload_type = get_bits(&gb, 5); | |
| 300 | ✗ | payload_size = payload_size_type; | |
| 301 | ✗ | if (payload_size_type == 6) | |
| 302 | ✗ | return AVERROR_PATCHWELCOME; | |
| 303 | ✗ | if (payload_size_type == 7) | |
| 304 | ✗ | payload_size = get_mb(&gb); | |
| 305 | |||
| 306 | ✗ | if (payload_size > INT_MAX - (get_bits_count(&gb) >> 3)) | |
| 307 | ✗ | return AVERROR_INVALIDDATA; | |
| 308 | |||
| 309 | ✗ | block_size = raw_block_size = payload_size + (get_bits_count(&gb) >> 3); | |
| 310 | ✗ | if (block_size >= bytestream2_get_bytes_left(&gbc)) | |
| 311 | ✗ | return AVERROR_INVALIDDATA; | |
| 312 | |||
| 313 | ✗ | block_end = bytestream2_tell(&gbc) + block_size; | |
| 314 | // Take into account removed emulation 3bytes, as payload_size in | ||
| 315 | // the bitstream is not aware of them. | ||
| 316 | ✗ | for (; skipped_byte_pos < nal->skipped_bytes; skipped_byte_pos++) { | |
| 317 | ✗ | if (nal->skipped_bytes_pos[skipped_byte_pos] >= block_end) | |
| 318 | ✗ | break; | |
| 319 | ✗ | raw_block_size++; | |
| 320 | } | ||
| 321 | |||
| 322 | ✗ | switch (payload_type) { | |
| 323 | ✗ | case LCEVC_PAYLOAD_TYPE_SEQUENCE_CONFIG: | |
| 324 | case LCEVC_PAYLOAD_TYPE_GLOBAL_CONFIG: | ||
| 325 | case LCEVC_PAYLOAD_TYPE_ADDITIONAL_INFO: | ||
| 326 | ✗ | if (remove) | |
| 327 | ✗ | break; | |
| 328 | ✗ | bytestream2_put_buffer(pbc, raw_gbc.buffer, raw_block_size); | |
| 329 | ✗ | sc |= payload_type == LCEVC_PAYLOAD_TYPE_SEQUENCE_CONFIG; | |
| 330 | ✗ | gc |= payload_type == LCEVC_PAYLOAD_TYPE_GLOBAL_CONFIG; | |
| 331 | ✗ | break; | |
| 332 | ✗ | default: | |
| 333 | ✗ | if (!remove) | |
| 334 | ✗ | break; | |
| 335 | ✗ | bytestream2_put_buffer(pbc, raw_gbc.buffer, raw_block_size); | |
| 336 | ✗ | break; | |
| 337 | } | ||
| 338 | |||
| 339 | ✗ | bytestream2_skip(&gbc, block_size); | |
| 340 | ✗ | bytestream2_skip(&raw_gbc, raw_block_size); | |
| 341 | } | ||
| 342 | |||
| 343 | ✗ | if (!remove && !sc && !gc) | |
| 344 | ✗ | return AVERROR_INVALIDDATA; | |
| 345 | |||
| 346 | ✗ | bytestream2_put_byte(pbc, 0x80); // rbsp_alignment bits | |
| 347 | |||
| 348 | ✗ | return bytestream2_tell_p(pbc); | |
| 349 | } | ||
| 350 | |||
| 351 | ✗ | static int extract_extradata_lcevc(AVBSFContext *ctx, AVPacket *pkt, | |
| 352 | uint8_t **data, int *size) | ||
| 353 | { | ||
| 354 | static const int extradata_nal_types[] = { | ||
| 355 | LCEVC_IDR_NUT, LCEVC_NON_IDR_NUT, | ||
| 356 | }; | ||
| 357 | |||
| 358 | ✗ | ExtractExtradataContext *s = ctx->priv_data; | |
| 359 | PutByteContext pb_extradata; | ||
| 360 | ✗ | int extradata_size = 0, filtered_size = 0; | |
| 361 | ✗ | size_t nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types); | |
| 362 | ✗ | int i, ret = 0; | |
| 363 | |||
| 364 | ✗ | ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size, | |
| 365 | ✗ | ctx, 0, ctx->par_in->codec_id, H2645_FLAG_SMALL_PADDING); | |
| 366 | ✗ | if (ret < 0) | |
| 367 | ✗ | return ret; | |
| 368 | |||
| 369 | ✗ | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { | |
| 370 | ✗ | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
| 371 | ✗ | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) { | |
| 372 | ✗ | bytestream2_init_writer(&pb_extradata, NULL, 0); | |
| 373 | // dummy pass to find sc, gc or ai | ||
| 374 | ✗ | if (!write_lcevc_nalu(ctx, &pb_extradata, nal, 0)) | |
| 375 | ✗ | extradata_size += nal->raw_size + 3; | |
| 376 | } | ||
| 377 | ✗ | filtered_size += nal->raw_size + 3; | |
| 378 | } | ||
| 379 | |||
| 380 | ✗ | if (extradata_size) { | |
| 381 | ✗ | AVBufferRef *filtered_buf = NULL; | |
| 382 | PutByteContext pb_filtered_data; | ||
| 383 | uint8_t *extradata; | ||
| 384 | |||
| 385 | ✗ | if (s->remove) { | |
| 386 | ✗ | filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 387 | ✗ | if (!filtered_buf) { | |
| 388 | ✗ | return AVERROR(ENOMEM); | |
| 389 | } | ||
| 390 | ✗ | memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 391 | } | ||
| 392 | |||
| 393 | ✗ | extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 394 | ✗ | if (!extradata) { | |
| 395 | ✗ | av_buffer_unref(&filtered_buf); | |
| 396 | ✗ | return AVERROR(ENOMEM); | |
| 397 | } | ||
| 398 | |||
| 399 | ✗ | bytestream2_init_writer(&pb_extradata, extradata, extradata_size); | |
| 400 | ✗ | if (s->remove) | |
| 401 | ✗ | bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size); | |
| 402 | |||
| 403 | ✗ | for (i = 0; i < s->h2645_pkt.nb_nals; i++) { | |
| 404 | ✗ | H2645NAL *nal = &s->h2645_pkt.nals[i]; | |
| 405 | ✗ | if (val_in_array(extradata_nal_types, nb_extradata_nal_types, | |
| 406 | nal->type)) { | ||
| 407 | ✗ | bytestream2_put_be24(&pb_extradata, 1); //startcode | |
| 408 | ✗ | ret = write_lcevc_nalu(ctx, &pb_extradata, nal, 0); | |
| 409 | ✗ | if (ret < 0) { | |
| 410 | ✗ | av_freep(&extradata); | |
| 411 | ✗ | av_buffer_unref(&filtered_buf); | |
| 412 | ✗ | return ret; | |
| 413 | } | ||
| 414 | ✗ | if (s->remove) { | |
| 415 | ✗ | bytestream2_put_be24(&pb_filtered_data, 1); //startcode | |
| 416 | ✗ | ret = write_lcevc_nalu(ctx, &pb_filtered_data, nal, 1); | |
| 417 | ✗ | if (ret < 0) { | |
| 418 | ✗ | av_freep(&extradata); | |
| 419 | ✗ | av_buffer_unref(&filtered_buf); | |
| 420 | ✗ | return ret; | |
| 421 | } | ||
| 422 | } | ||
| 423 | ✗ | } else if (s->remove) { | |
| 424 | ✗ | bytestream2_put_be24(&pb_filtered_data, 1); //startcode | |
| 425 | ✗ | bytestream2_put_bufferu(&pb_filtered_data, nal->raw_data, nal->raw_size); | |
| 426 | } | ||
| 427 | } | ||
| 428 | ✗ | *data = extradata; | |
| 429 | ✗ | *size = bytestream2_tell_p(&pb_extradata); | |
| 430 | ✗ | av_assert0(*size <= extradata_size); | |
| 431 | |||
| 432 | ✗ | if (s->remove) { | |
| 433 | ✗ | av_assert0(bytestream2_tell_p(&pb_filtered_data) <= filtered_size); | |
| 434 | ✗ | av_buffer_unref(&pkt->buf); | |
| 435 | ✗ | pkt->buf = filtered_buf; | |
| 436 | ✗ | pkt->data = filtered_buf->data; | |
| 437 | ✗ | pkt->size = bytestream2_tell_p(&pb_filtered_data); | |
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | ✗ | return 0; | |
| 442 | } | ||
| 443 | |||
| 444 | 6 | static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt, | |
| 445 | uint8_t **data, int *size) | ||
| 446 | { | ||
| 447 | 6 | ExtractExtradataContext *s = ctx->priv_data; | |
| 448 | 6 | const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size; | |
| 449 | 6 | uint32_t state = UINT32_MAX; | |
| 450 | 6 | int has_extradata = 0, extradata_size = 0; | |
| 451 | |||
| 452 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | while (ptr < end) { |
| 453 | 23 | ptr = avpriv_find_start_code(ptr, end, &state); | |
| 454 |
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) { |
| 455 | 17 | has_extradata = 1; | |
| 456 |
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)) { |
| 457 | 6 | extradata_size = ptr - 4 - pkt->data; | |
| 458 | 6 | break; | |
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (extradata_size) { |
| 463 | 6 | *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 464 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!*data) |
| 465 | ✗ | return AVERROR(ENOMEM); | |
| 466 | |||
| 467 | 6 | memcpy(*data, pkt->data, extradata_size); | |
| 468 | 6 | *size = extradata_size; | |
| 469 | |||
| 470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (s->remove) { |
| 471 | ✗ | pkt->data += extradata_size; | |
| 472 | ✗ | pkt->size -= extradata_size; | |
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | 6 | return 0; | |
| 477 | } | ||
| 478 | |||
| 479 | 470 | static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt, | |
| 480 | uint8_t **data, int *size) | ||
| 481 | { | ||
| 482 | 470 | ExtractExtradataContext *s = ctx->priv_data; | |
| 483 | 470 | uint32_t state = UINT32_MAX; | |
| 484 | 470 | int i, found = 0; | |
| 485 | |||
| 486 |
2/2✓ Branch 0 taken 4340556 times.
✓ Branch 1 taken 275 times.
|
4340831 | for (i = 0; i < pkt->size; i++) { |
| 487 | 4340556 | state = (state << 8) | pkt->data[i]; | |
| 488 |
2/2✓ Branch 0 taken 195 times.
✓ Branch 1 taken 4340361 times.
|
4340556 | if (state == 0x1B3) |
| 489 | 195 | found = 1; | |
| 490 |
8/8✓ Branch 0 taken 5857 times.
✓ Branch 1 taken 4334504 times.
✓ Branch 2 taken 5656 times.
✓ Branch 3 taken 201 times.
✓ Branch 4 taken 464 times.
✓ Branch 5 taken 5192 times.
✓ Branch 6 taken 195 times.
✓ Branch 7 taken 269 times.
|
4340361 | else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) { |
| 491 | 195 | *size = i - 3; | |
| 492 | 195 | *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 493 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
|
195 | if (!*data) |
| 494 | ✗ | return AVERROR(ENOMEM); | |
| 495 | |||
| 496 | 195 | memcpy(*data, pkt->data, *size); | |
| 497 | |||
| 498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
|
195 | if (s->remove) { |
| 499 | ✗ | pkt->data += *size; | |
| 500 | ✗ | pkt->size -= *size; | |
| 501 | } | ||
| 502 | 195 | break; | |
| 503 | } | ||
| 504 | } | ||
| 505 | 470 | return 0; | |
| 506 | } | ||
| 507 | |||
| 508 | 121 | static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt, | |
| 509 | uint8_t **data, int *size) | ||
| 510 | { | ||
| 511 | 121 | ExtractExtradataContext *s = ctx->priv_data; | |
| 512 | 121 | const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size; | |
| 513 | 121 | uint32_t state = UINT32_MAX; | |
| 514 | |||
| 515 |
1/2✓ Branch 0 taken 397 times.
✗ Branch 1 not taken.
|
397 | while (ptr < end) { |
| 516 | 397 | ptr = avpriv_find_start_code(ptr, end, &state); | |
| 517 |
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) { |
| 518 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 50 times.
|
121 | if (ptr - pkt->data > 4) { |
| 519 | 71 | *size = ptr - 4 - pkt->data; | |
| 520 | 71 | *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!*data) |
| 522 | ✗ | return AVERROR(ENOMEM); | |
| 523 | |||
| 524 | 71 | memcpy(*data, pkt->data, *size); | |
| 525 | |||
| 526 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (s->remove) { |
| 527 | ✗ | pkt->data += *size; | |
| 528 | ✗ | pkt->size -= *size; | |
| 529 | } | ||
| 530 | } | ||
| 531 | 121 | break; | |
| 532 | } | ||
| 533 | } | ||
| 534 | 121 | return 0; | |
| 535 | } | ||
| 536 | |||
| 537 | static const struct { | ||
| 538 | enum AVCodecID id; | ||
| 539 | int (*extract)(AVBSFContext *ctx, AVPacket *pkt, | ||
| 540 | uint8_t **data, int *size); | ||
| 541 | } extract_tab[] = { | ||
| 542 | { AV_CODEC_ID_AV1, extract_extradata_av1 }, | ||
| 543 | { AV_CODEC_ID_AVS2, extract_extradata_mpeg4 }, | ||
| 544 | { AV_CODEC_ID_AVS3, extract_extradata_mpeg4 }, | ||
| 545 | { AV_CODEC_ID_CAVS, extract_extradata_mpeg4 }, | ||
| 546 | { AV_CODEC_ID_H264, extract_extradata_h2645 }, | ||
| 547 | { AV_CODEC_ID_HEVC, extract_extradata_h2645 }, | ||
| 548 | { AV_CODEC_ID_LCEVC, extract_extradata_lcevc }, | ||
| 549 | { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12 }, | ||
| 550 | { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12 }, | ||
| 551 | { AV_CODEC_ID_MPEG4, extract_extradata_mpeg4 }, | ||
| 552 | { AV_CODEC_ID_VC1, extract_extradata_vc1 }, | ||
| 553 | { AV_CODEC_ID_VVC, extract_extradata_h2645 }, | ||
| 554 | }; | ||
| 555 | |||
| 556 | 799 | static int extract_extradata_init(AVBSFContext *ctx) | |
| 557 | { | ||
| 558 | 799 | ExtractExtradataContext *s = ctx->priv_data; | |
| 559 | int i; | ||
| 560 | |||
| 561 |
1/2✓ Branch 0 taken 5691 times.
✗ Branch 1 not taken.
|
5691 | for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) { |
| 562 |
2/2✓ Branch 0 taken 799 times.
✓ Branch 1 taken 4892 times.
|
5691 | if (extract_tab[i].id == ctx->par_in->codec_id) { |
| 563 | 799 | s->extract = extract_tab[i].extract; | |
| 564 | 799 | break; | |
| 565 | } | ||
| 566 | } | ||
| 567 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
|
799 | if (!s->extract) |
| 568 | ✗ | return AVERROR_BUG; | |
| 569 | |||
| 570 | 799 | return 0; | |
| 571 | } | ||
| 572 | |||
| 573 | 1636 | static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt) | |
| 574 | { | ||
| 575 | 1636 | ExtractExtradataContext *s = ctx->priv_data; | |
| 576 | 1636 | uint8_t *extradata = NULL; | |
| 577 | int extradata_size; | ||
| 578 | 1636 | int ret = 0; | |
| 579 | |||
| 580 | 1636 | ret = ff_bsf_get_packet_ref(ctx, pkt); | |
| 581 |
2/2✓ Branch 0 taken 419 times.
✓ Branch 1 taken 1217 times.
|
1636 | if (ret < 0) |
| 582 | 419 | return ret; | |
| 583 | |||
| 584 | 1217 | ret = s->extract(ctx, pkt, &extradata, &extradata_size); | |
| 585 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1217 times.
|
1217 | if (ret < 0) |
| 586 | ✗ | goto fail; | |
| 587 | |||
| 588 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 417 times.
|
1217 | if (extradata) { |
| 589 | 800 | memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 590 | 800 | ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, | |
| 591 | extradata, extradata_size); | ||
| 592 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (ret < 0) { |
| 593 | ✗ | av_freep(&extradata); | |
| 594 | ✗ | goto fail; | |
| 595 | } | ||
| 596 | } | ||
| 597 | |||
| 598 | 1217 | return 0; | |
| 599 | |||
| 600 | ✗ | fail: | |
| 601 | ✗ | av_packet_unref(pkt); | |
| 602 | ✗ | return ret; | |
| 603 | } | ||
| 604 | |||
| 605 | 799 | static void extract_extradata_close(AVBSFContext *ctx) | |
| 606 | { | ||
| 607 | 799 | ExtractExtradataContext *s = ctx->priv_data; | |
| 608 | 799 | ff_av1_packet_uninit(&s->av1_pkt); | |
| 609 | 799 | ff_h2645_packet_uninit(&s->h2645_pkt); | |
| 610 | 799 | } | |
| 611 | |||
| 612 | static const enum AVCodecID codec_ids[] = { | ||
| 613 | AV_CODEC_ID_AV1, | ||
| 614 | AV_CODEC_ID_AVS2, | ||
| 615 | AV_CODEC_ID_AVS3, | ||
| 616 | AV_CODEC_ID_CAVS, | ||
| 617 | AV_CODEC_ID_H264, | ||
| 618 | AV_CODEC_ID_HEVC, | ||
| 619 | AV_CODEC_ID_LCEVC, | ||
| 620 | AV_CODEC_ID_MPEG1VIDEO, | ||
| 621 | AV_CODEC_ID_MPEG2VIDEO, | ||
| 622 | AV_CODEC_ID_MPEG4, | ||
| 623 | AV_CODEC_ID_VC1, | ||
| 624 | AV_CODEC_ID_VVC, | ||
| 625 | AV_CODEC_ID_NONE, | ||
| 626 | }; | ||
| 627 | |||
| 628 | #define OFFSET(x) offsetof(ExtractExtradataContext, x) | ||
| 629 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) | ||
| 630 | static const AVOption options[] = { | ||
| 631 | { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT, | ||
| 632 | { .i64 = 0 }, 0, 1, FLAGS }, | ||
| 633 | { NULL }, | ||
| 634 | }; | ||
| 635 | |||
| 636 | static const AVClass extract_extradata_class = { | ||
| 637 | .class_name = "extract_extradata", | ||
| 638 | .item_name = av_default_item_name, | ||
| 639 | .option = options, | ||
| 640 | .version = LIBAVUTIL_VERSION_INT, | ||
| 641 | }; | ||
| 642 | |||
| 643 | const FFBitStreamFilter ff_extract_extradata_bsf = { | ||
| 644 | .p.name = "extract_extradata", | ||
| 645 | .p.codec_ids = codec_ids, | ||
| 646 | .p.priv_class = &extract_extradata_class, | ||
| 647 | .priv_data_size = sizeof(ExtractExtradataContext), | ||
| 648 | .init = extract_extradata_init, | ||
| 649 | .filter = extract_extradata_filter, | ||
| 650 | .close = extract_extradata_close, | ||
| 651 | }; | ||
| 652 |