| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Various functions used by both muxers and demuxers | ||
| 3 | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <math.h> | ||
| 23 | #include "libavutil/avassert.h" | ||
| 24 | #include "libavutil/avstring.h" | ||
| 25 | #include "libavutil/channel_layout.h" | ||
| 26 | #include "libavutil/frame.h" | ||
| 27 | #include "libavutil/iamf.h" | ||
| 28 | #include "libavutil/intreadwrite.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "libavutil/opt.h" | ||
| 31 | #include "libavutil/pixfmt.h" | ||
| 32 | #include "libavutil/samplefmt.h" | ||
| 33 | #include "libavcodec/avcodec.h" | ||
| 34 | #include "libavcodec/codec.h" | ||
| 35 | #include "libavcodec/bsf.h" | ||
| 36 | #include "libavcodec/codec_desc.h" | ||
| 37 | #include "libavcodec/packet_internal.h" | ||
| 38 | #include "avformat.h" | ||
| 39 | #include "avformat_internal.h" | ||
| 40 | #include "avio.h" | ||
| 41 | #include "demux.h" | ||
| 42 | #include "mux.h" | ||
| 43 | #include "internal.h" | ||
| 44 | #include "url.h" | ||
| 45 | |||
| 46 | 17936 | void ff_free_stream(AVStream **pst) | |
| 47 | { | ||
| 48 | 17936 | AVStream *st = *pst; | |
| 49 | 17936 | FFStream *const sti = ffstream(st); | |
| 50 | |||
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17936 times.
|
17936 | if (!st) |
| 52 | ✗ | return; | |
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 17880 times.
|
17936 | if (st->attached_pic.data) |
| 55 | 56 | av_packet_unref(&st->attached_pic); | |
| 56 | |||
| 57 | 17936 | av_parser_close(sti->parser); | |
| 58 | 17936 | avcodec_free_context(&sti->avctx); | |
| 59 | 17936 | av_bsf_free(&sti->bsfc); | |
| 60 | 17936 | av_freep(&sti->index_entries); | |
| 61 | 17936 | av_freep(&sti->probe_data.buf); | |
| 62 | |||
| 63 | 17936 | av_packet_free(&sti->parse_pkt); | |
| 64 | |||
| 65 | 17936 | av_bsf_free(&sti->extract_extradata.bsf); | |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 17905 times.
|
17936 | if (sti->info) { |
| 68 | 31 | av_freep(&sti->info->duration_error); | |
| 69 | 31 | av_freep(&sti->info); | |
| 70 | } | ||
| 71 | |||
| 72 | 17936 | av_dict_free(&st->metadata); | |
| 73 | 17936 | avcodec_parameters_free(&st->codecpar); | |
| 74 | 17936 | av_freep(&st->priv_data); | |
| 75 | |||
| 76 | 17936 | av_freep(pst); | |
| 77 | } | ||
| 78 | |||
| 79 | 110 | void ff_free_stream_group(AVStreamGroup **pstg) | |
| 80 | { | ||
| 81 | 110 | AVStreamGroup *stg = *pstg; | |
| 82 | |||
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
|
110 | if (!stg) |
| 84 | ✗ | return; | |
| 85 | |||
| 86 | 110 | av_freep(&stg->streams); | |
| 87 | 110 | av_dict_free(&stg->metadata); | |
| 88 | 110 | av_freep(&stg->priv_data); | |
| 89 |
3/5✓ Branch 0 taken 52 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
110 | switch (stg->type) { |
| 90 | 52 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: { | |
| 91 | 52 | av_iamf_audio_element_free(&stg->params.iamf_audio_element); | |
| 92 | 52 | break; | |
| 93 | } | ||
| 94 | 49 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: { | |
| 95 | 49 | av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation); | |
| 96 | 49 | break; | |
| 97 | } | ||
| 98 | 9 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: | |
| 99 | 9 | av_opt_free(stg->params.tile_grid); | |
| 100 | 9 | av_freep(&stg->params.tile_grid->offsets); | |
| 101 | 9 | av_packet_side_data_free(&stg->params.tile_grid->coded_side_data, | |
| 102 | 9 | &stg->params.tile_grid->nb_coded_side_data); | |
| 103 | 9 | av_freep(&stg->params.tile_grid); | |
| 104 | 9 | break; | |
| 105 | ✗ | case AV_STREAM_GROUP_PARAMS_LCEVC: | |
| 106 | ✗ | av_opt_free(stg->params.lcevc); | |
| 107 | ✗ | av_freep(&stg->params.lcevc); | |
| 108 | ✗ | break; | |
| 109 | ✗ | default: | |
| 110 | ✗ | break; | |
| 111 | } | ||
| 112 | |||
| 113 | 110 | av_freep(pstg); | |
| 114 | } | ||
| 115 | |||
| 116 | 10 | void ff_remove_stream(AVFormatContext *s, AVStream *st) | |
| 117 | { | ||
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | av_assert0(s->nb_streams>0); |
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | av_assert0(s->streams[ s->nb_streams - 1 ] == st); |
| 120 | |||
| 121 | 10 | ff_free_stream(&s->streams[ --s->nb_streams ]); | |
| 122 | 10 | } | |
| 123 | |||
| 124 | ✗ | void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg) | |
| 125 | { | ||
| 126 | ✗ | av_assert0(s->nb_stream_groups > 0); | |
| 127 | ✗ | av_assert0(s->stream_groups[ s->nb_stream_groups - 1 ] == stg); | |
| 128 | |||
| 129 | ✗ | ff_free_stream_group(&s->stream_groups[ --s->nb_stream_groups ]); | |
| 130 | ✗ | } | |
| 131 | |||
| 132 | /* XXX: suppress the packet queue */ | ||
| 133 | 12791 | void ff_flush_packet_queue(AVFormatContext *s) | |
| 134 | { | ||
| 135 | 12791 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 136 | 12791 | FFFormatContext *const si = &fci->fc; | |
| 137 | 12791 | avpriv_packet_list_free(&fci->parse_queue); | |
| 138 | 12791 | avpriv_packet_list_free(&si->packet_buffer); | |
| 139 | 12791 | avpriv_packet_list_free(&fci->raw_packet_buffer); | |
| 140 | |||
| 141 | 12791 | fci->raw_packet_buffer_size = 0; | |
| 142 | 12791 | } | |
| 143 | |||
| 144 | 17062 | void avformat_free_context(AVFormatContext *s) | |
| 145 | { | ||
| 146 | FormatContextInternal *fci; | ||
| 147 | FFFormatContext *si; | ||
| 148 | |||
| 149 |
2/2✓ Branch 0 taken 541 times.
✓ Branch 1 taken 16521 times.
|
17062 | if (!s) |
| 150 | 541 | return; | |
| 151 | 16521 | fci = ff_fc_internal(s); | |
| 152 | 16521 | si = &fci->fc; | |
| 153 | |||
| 154 |
5/6✓ Branch 0 taken 8638 times.
✓ Branch 1 taken 7883 times.
✓ Branch 3 taken 4362 times.
✓ Branch 4 taken 4276 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4362 times.
|
16521 | if (s->oformat && ffofmt(s->oformat)->deinit && fci->initialized) |
| 155 | ✗ | ffofmt(s->oformat)->deinit(s); | |
| 156 | |||
| 157 | 16521 | av_opt_free(s); | |
| 158 |
5/6✓ Branch 0 taken 7883 times.
✓ Branch 1 taken 8638 times.
✓ Branch 2 taken 6838 times.
✓ Branch 3 taken 1045 times.
✓ Branch 4 taken 6838 times.
✗ Branch 5 not taken.
|
16521 | if (s->iformat && s->iformat->priv_class && s->priv_data) |
| 159 | 6838 | av_opt_free(s->priv_data); | |
| 160 |
5/6✓ Branch 0 taken 8638 times.
✓ Branch 1 taken 7883 times.
✓ Branch 2 taken 4431 times.
✓ Branch 3 taken 4207 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4431 times.
|
16521 | if (s->oformat && s->oformat->priv_class && s->priv_data) |
| 161 | ✗ | av_opt_free(s->priv_data); | |
| 162 | |||
| 163 |
2/2✓ Branch 0 taken 17926 times.
✓ Branch 1 taken 16521 times.
|
34447 | for (unsigned i = 0; i < s->nb_streams; i++) |
| 164 | 17926 | ff_free_stream(&s->streams[i]); | |
| 165 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 16521 times.
|
16631 | for (unsigned i = 0; i < s->nb_stream_groups; i++) |
| 166 | 110 | ff_free_stream_group(&s->stream_groups[i]); | |
| 167 | 16521 | s->nb_stream_groups = 0; | |
| 168 | 16521 | s->nb_streams = 0; | |
| 169 | |||
| 170 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 16521 times.
|
16599 | for (unsigned i = 0; i < s->nb_programs; i++) { |
| 171 | 78 | av_dict_free(&s->programs[i]->metadata); | |
| 172 | 78 | av_freep(&s->programs[i]->stream_index); | |
| 173 | 78 | av_freep(&s->programs[i]); | |
| 174 | } | ||
| 175 | 16521 | s->nb_programs = 0; | |
| 176 | |||
| 177 | 16521 | av_freep(&s->programs); | |
| 178 | 16521 | av_freep(&s->priv_data); | |
| 179 |
2/2✓ Branch 0 taken 105 times.
✓ Branch 1 taken 16521 times.
|
16626 | while (s->nb_chapters--) { |
| 180 | 105 | av_dict_free(&s->chapters[s->nb_chapters]->metadata); | |
| 181 | 105 | av_freep(&s->chapters[s->nb_chapters]); | |
| 182 | } | ||
| 183 | 16521 | av_freep(&s->chapters); | |
| 184 | 16521 | av_dict_free(&s->metadata); | |
| 185 | 16521 | av_dict_free(&si->id3v2_meta); | |
| 186 | 16521 | av_packet_free(&si->pkt); | |
| 187 | 16521 | av_packet_free(&si->parse_pkt); | |
| 188 | 16521 | avpriv_packet_list_free(&si->packet_buffer); | |
| 189 | 16521 | av_freep(&s->streams); | |
| 190 | 16521 | av_freep(&s->stream_groups); | |
| 191 |
2/2✓ Branch 0 taken 7883 times.
✓ Branch 1 taken 8638 times.
|
16521 | if (s->iformat) |
| 192 | 7883 | ff_flush_packet_queue(s); | |
| 193 | 16521 | av_freep(&s->url); | |
| 194 | 16521 | av_freep(&s->name); | |
| 195 | 16521 | av_free(s); | |
| 196 | } | ||
| 197 | |||
| 198 | /** | ||
| 199 | * Copy all stream parameters from source to destination stream, with the | ||
| 200 | * exception of the index field, which is usually set by avformat_new_stream(). | ||
| 201 | * | ||
| 202 | * @param dst pointer to destination AVStream | ||
| 203 | * @param src pointer to source AVStream | ||
| 204 | * @return >=0 on success, AVERROR code on error | ||
| 205 | */ | ||
| 206 | 20 | static int stream_params_copy(AVStream *dst, const AVStream *src) | |
| 207 | { | ||
| 208 | int ret; | ||
| 209 | |||
| 210 | 20 | dst->id = src->id; | |
| 211 | 20 | dst->time_base = src->time_base; | |
| 212 | 20 | dst->start_time = src->start_time; | |
| 213 | 20 | dst->duration = src->duration; | |
| 214 | 20 | dst->nb_frames = src->nb_frames; | |
| 215 | 20 | dst->disposition = src->disposition; | |
| 216 | 20 | dst->discard = src->discard; | |
| 217 | 20 | dst->sample_aspect_ratio = src->sample_aspect_ratio; | |
| 218 | 20 | dst->avg_frame_rate = src->avg_frame_rate; | |
| 219 | 20 | dst->event_flags = src->event_flags; | |
| 220 | 20 | dst->r_frame_rate = src->r_frame_rate; | |
| 221 | 20 | dst->pts_wrap_bits = src->pts_wrap_bits; | |
| 222 | |||
| 223 | 20 | av_dict_free(&dst->metadata); | |
| 224 | 20 | ret = av_dict_copy(&dst->metadata, src->metadata, 0); | |
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (ret < 0) |
| 226 | ✗ | return ret; | |
| 227 | |||
| 228 | 20 | ret = avcodec_parameters_copy(dst->codecpar, src->codecpar); | |
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (ret < 0) |
| 230 | ✗ | return ret; | |
| 231 | |||
| 232 | 20 | av_packet_unref(&dst->attached_pic); | |
| 233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (src->attached_pic.data) { |
| 234 | ✗ | ret = av_packet_ref(&dst->attached_pic, &src->attached_pic); | |
| 235 | ✗ | if (ret < 0) | |
| 236 | ✗ | return ret; | |
| 237 | } | ||
| 238 | |||
| 239 | 20 | return 0; | |
| 240 | } | ||
| 241 | |||
| 242 | 20 | AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src) | |
| 243 | { | ||
| 244 | AVStream *st; | ||
| 245 | int ret; | ||
| 246 | |||
| 247 | 20 | st = avformat_new_stream(dst_ctx, NULL); | |
| 248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!st) |
| 249 | ✗ | return NULL; | |
| 250 | |||
| 251 | 20 | ret = stream_params_copy(st, src); | |
| 252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (ret < 0) { |
| 253 | ✗ | ff_remove_stream(dst_ctx, st); | |
| 254 | ✗ | return NULL; | |
| 255 | } | ||
| 256 | |||
| 257 | 20 | return st; | |
| 258 | } | ||
| 259 | |||
| 260 | 83 | const char *avformat_stream_group_name(enum AVStreamGroupParamsType type) | |
| 261 | { | ||
| 262 |
3/5✓ Branch 0 taken 38 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
83 | switch(type) { |
| 263 | 38 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: return "IAMF Audio Element"; | |
| 264 | 36 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: return "IAMF Mix Presentation"; | |
| 265 | 9 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: return "Tile Grid"; | |
| 266 | ✗ | case AV_STREAM_GROUP_PARAMS_LCEVC: return "LCEVC (Split video and enhancement)"; | |
| 267 | } | ||
| 268 | ✗ | return NULL; | |
| 269 | } | ||
| 270 | |||
| 271 | 378 | AVProgram *av_new_program(AVFormatContext *ac, int id) | |
| 272 | { | ||
| 273 | 378 | AVProgram *program = NULL; | |
| 274 | int ret; | ||
| 275 | |||
| 276 | 378 | av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id); | |
| 277 | |||
| 278 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 378 times.
|
678 | for (unsigned i = 0; i < ac->nb_programs; i++) |
| 279 |
1/2✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
|
300 | if (ac->programs[i]->id == id) |
| 280 | 300 | program = ac->programs[i]; | |
| 281 | |||
| 282 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 300 times.
|
378 | if (!program) { |
| 283 | 78 | program = av_mallocz(sizeof(*program)); | |
| 284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if (!program) |
| 285 | ✗ | return NULL; | |
| 286 | 78 | ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program); | |
| 287 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if (ret < 0) { |
| 288 | ✗ | av_free(program); | |
| 289 | ✗ | return NULL; | |
| 290 | } | ||
| 291 | 78 | program->discard = AVDISCARD_NONE; | |
| 292 | 78 | program->pmt_version = -1; | |
| 293 | 78 | program->id = id; | |
| 294 | 78 | program->pts_wrap_reference = AV_NOPTS_VALUE; | |
| 295 | 78 | program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE; | |
| 296 | 78 | program->start_time = | |
| 297 | 78 | program->end_time = AV_NOPTS_VALUE; | |
| 298 | } | ||
| 299 | 378 | return program; | |
| 300 | } | ||
| 301 | |||
| 302 | 608 | void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx) | |
| 303 | { | ||
| 304 | 608 | AVProgram *program = NULL; | |
| 305 | void *tmp; | ||
| 306 | |||
| 307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 608 times.
|
608 | if (idx >= ac->nb_streams) { |
| 308 | ✗ | av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx); | |
| 309 | ✗ | return; | |
| 310 | } | ||
| 311 | |||
| 312 |
1/2✓ Branch 0 taken 608 times.
✗ Branch 1 not taken.
|
608 | for (unsigned i = 0; i < ac->nb_programs; i++) { |
| 313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 608 times.
|
608 | if (ac->programs[i]->id != progid) |
| 314 | ✗ | continue; | |
| 315 | 608 | program = ac->programs[i]; | |
| 316 |
2/2✓ Branch 0 taken 955 times.
✓ Branch 1 taken 372 times.
|
1327 | for (unsigned j = 0; j < program->nb_stream_indexes; j++) |
| 317 |
2/2✓ Branch 0 taken 236 times.
✓ Branch 1 taken 719 times.
|
955 | if (program->stream_index[j] == idx) |
| 318 | 236 | return; | |
| 319 | |||
| 320 | 372 | tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int)); | |
| 321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 372 times.
|
372 | if (!tmp) |
| 322 | ✗ | return; | |
| 323 | 372 | program->stream_index = tmp; | |
| 324 | 372 | program->stream_index[program->nb_stream_indexes++] = idx; | |
| 325 | 372 | return; | |
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | 14666 | AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s) | |
| 330 | { | ||
| 331 |
2/2✓ Branch 0 taken 1777 times.
✓ Branch 1 taken 13817 times.
|
15594 | for (unsigned i = 0; i < ic->nb_programs; i++) { |
| 332 |
2/2✓ Branch 0 taken 925 times.
✓ Branch 1 taken 852 times.
|
1777 | if (ic->programs[i] == last) { |
| 333 | 925 | last = NULL; | |
| 334 | } else { | ||
| 335 |
1/2✓ Branch 0 taken 852 times.
✗ Branch 1 not taken.
|
852 | if (!last) |
| 336 |
2/2✓ Branch 0 taken 1966 times.
✓ Branch 1 taken 3 times.
|
1969 | for (unsigned j = 0; j < ic->programs[i]->nb_stream_indexes; j++) |
| 337 |
2/2✓ Branch 0 taken 849 times.
✓ Branch 1 taken 1117 times.
|
1966 | if (ic->programs[i]->stream_index[j] == s) |
| 338 | 849 | return ic->programs[i]; | |
| 339 | } | ||
| 340 | } | ||
| 341 | 13817 | return NULL; | |
| 342 | } | ||
| 343 | |||
| 344 | 1976 | int av_find_default_stream_index(AVFormatContext *s) | |
| 345 | { | ||
| 346 | 1976 | int best_stream = 0; | |
| 347 | 1976 | int best_score = INT_MIN; | |
| 348 | |||
| 349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1976 times.
|
1976 | if (s->nb_streams <= 0) |
| 350 | ✗ | return -1; | |
| 351 |
2/2✓ Branch 0 taken 2398 times.
✓ Branch 1 taken 1976 times.
|
4374 | for (unsigned i = 0; i < s->nb_streams; i++) { |
| 352 | 2398 | const AVStream *const st = s->streams[i]; | |
| 353 | 2398 | const FFStream *const sti = cffstream(st); | |
| 354 | 2398 | int score = 0; | |
| 355 |
2/2✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 1004 times.
|
2398 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 356 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1390 times.
|
1394 | if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) |
| 357 | 4 | score -= 400; | |
| 358 |
3/4✓ Branch 0 taken 1304 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 1304 times.
✗ Branch 3 not taken.
|
1394 | if (st->codecpar->width && st->codecpar->height) |
| 359 | 1304 | score += 50; | |
| 360 | 1394 | score+= 25; | |
| 361 | } | ||
| 362 |
2/2✓ Branch 0 taken 973 times.
✓ Branch 1 taken 1425 times.
|
2398 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 363 |
2/2✓ Branch 0 taken 941 times.
✓ Branch 1 taken 32 times.
|
973 | if (st->codecpar->sample_rate) |
| 364 | 941 | score += 50; | |
| 365 | } | ||
| 366 |
2/2✓ Branch 0 taken 1979 times.
✓ Branch 1 taken 419 times.
|
2398 | if (sti->codec_info_nb_frames) |
| 367 | 1979 | score += 12; | |
| 368 | |||
| 369 |
2/2✓ Branch 0 taken 2393 times.
✓ Branch 1 taken 5 times.
|
2398 | if (st->discard != AVDISCARD_ALL) |
| 370 | 2393 | score += 200; | |
| 371 | |||
| 372 |
2/2✓ Branch 0 taken 2018 times.
✓ Branch 1 taken 380 times.
|
2398 | if (score > best_score) { |
| 373 | 2018 | best_score = score; | |
| 374 | 2018 | best_stream = i; | |
| 375 | } | ||
| 376 | } | ||
| 377 | 1976 | return best_stream; | |
| 378 | } | ||
| 379 | |||
| 380 | 19 | int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, | |
| 381 | int wanted_stream_nb, int related_stream, | ||
| 382 | const AVCodec **decoder_ret, int flags) | ||
| 383 | { | ||
| 384 | 19 | int nb_streams = ic->nb_streams; | |
| 385 | 19 | int ret = AVERROR_STREAM_NOT_FOUND; | |
| 386 | 19 | int best_count = -1, best_multiframe = -1, best_disposition = -1; | |
| 387 | int count, multiframe, disposition; | ||
| 388 | 19 | int64_t best_bitrate = -1; | |
| 389 | int64_t bitrate; | ||
| 390 | 19 | unsigned *program = NULL; | |
| 391 | 19 | const AVCodec *decoder = NULL, *best_decoder = NULL; | |
| 392 | |||
| 393 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
19 | if (related_stream >= 0 && wanted_stream_nb < 0) { |
| 394 | ✗ | AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream); | |
| 395 | ✗ | if (p) { | |
| 396 | ✗ | program = p->stream_index; | |
| 397 | ✗ | nb_streams = p->nb_stream_indexes; | |
| 398 | } | ||
| 399 | } | ||
| 400 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 19 times.
|
41 | for (unsigned i = 0; i < nb_streams; i++) { |
| 401 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | int real_stream_index = program ? program[i] : i; |
| 402 | 22 | AVStream *st = ic->streams[real_stream_index]; | |
| 403 | 22 | AVCodecParameters *par = st->codecpar; | |
| 404 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
|
22 | if (par->codec_type != type) |
| 405 | 3 | continue; | |
| 406 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
19 | if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb) |
| 407 | ✗ | continue; | |
| 408 |
4/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
19 | if (type == AVMEDIA_TYPE_AUDIO && !(par->ch_layout.nb_channels && par->sample_rate)) |
| 409 | ✗ | continue; | |
| 410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (decoder_ret) { |
| 411 | ✗ | decoder = ff_find_decoder(ic, st, par->codec_id); | |
| 412 | ✗ | if (!decoder) { | |
| 413 | ✗ | if (ret < 0) | |
| 414 | ✗ | ret = AVERROR_DECODER_NOT_FOUND; | |
| 415 | ✗ | continue; | |
| 416 | } | ||
| 417 | } | ||
| 418 | 19 | disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED)) | |
| 419 | 19 | + !! (st->disposition & AV_DISPOSITION_DEFAULT); | |
| 420 | 19 | count = ffstream(st)->codec_info_nb_frames; | |
| 421 | 19 | bitrate = par->bit_rate; | |
| 422 | 19 | multiframe = FFMIN(5, count); | |
| 423 |
2/4✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
|
19 | if ((best_disposition > disposition) || |
| 424 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
|
19 | (best_disposition == disposition && best_multiframe > multiframe) || |
| 425 |
1/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
|
19 | (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) || |
| 426 | ✗ | (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count)) | |
| 427 | ✗ | continue; | |
| 428 | 19 | best_disposition = disposition; | |
| 429 | 19 | best_count = count; | |
| 430 | 19 | best_bitrate = bitrate; | |
| 431 | 19 | best_multiframe = multiframe; | |
| 432 | 19 | ret = real_stream_index; | |
| 433 | 19 | best_decoder = decoder; | |
| 434 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
19 | if (program && i == nb_streams - 1 && ret < 0) { |
| 435 | ✗ | program = NULL; | |
| 436 | ✗ | nb_streams = ic->nb_streams; | |
| 437 | /* no related stream found, try again with everything */ | ||
| 438 | ✗ | i = 0; | |
| 439 | } | ||
| 440 | } | ||
| 441 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (decoder_ret) |
| 442 | ✗ | *decoder_ret = best_decoder; | |
| 443 | 19 | return ret; | |
| 444 | } | ||
| 445 | |||
| 446 | /** | ||
| 447 | * Matches a stream specifier (but ignores requested index). | ||
| 448 | * | ||
| 449 | * @param indexptr set to point to the requested stream index if there is one | ||
| 450 | * | ||
| 451 | * @return <0 on error | ||
| 452 | * 0 if st is NOT a matching stream | ||
| 453 | * >0 if st is a matching stream | ||
| 454 | */ | ||
| 455 | 58 | static int match_stream_specifier(const AVFormatContext *s, const AVStream *st, | |
| 456 | const char *spec, const char **indexptr, | ||
| 457 | const AVStreamGroup **g, const AVProgram **p) | ||
| 458 | { | ||
| 459 | 58 | int match = 1; /* Stores if the specifier matches so far. */ | |
| 460 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 36 times.
|
114 | while (*spec) { |
| 461 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
78 | if (*spec <= '9' && *spec >= '0') { /* opt:index */ |
| 462 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
|
22 | if (indexptr) |
| 463 | 15 | *indexptr = spec; | |
| 464 | 22 | return match; | |
| 465 |
7/8✓ Branch 0 taken 25 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
56 | } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || |
| 466 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
56 | *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */ |
| 467 | enum AVMediaType type; | ||
| 468 | 50 | int nopic = 0; | |
| 469 | |||
| 470 |
3/7✓ Branch 0 taken 31 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
50 | switch (*spec++) { |
| 471 | 31 | case 'v': type = AVMEDIA_TYPE_VIDEO; break; | |
| 472 | 15 | case 'a': type = AVMEDIA_TYPE_AUDIO; break; | |
| 473 | 4 | case 's': type = AVMEDIA_TYPE_SUBTITLE; break; | |
| 474 | ✗ | case 'd': type = AVMEDIA_TYPE_DATA; break; | |
| 475 | ✗ | case 't': type = AVMEDIA_TYPE_ATTACHMENT; break; | |
| 476 | ✗ | case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break; | |
| 477 | ✗ | default: av_assert0(0); | |
| 478 | } | ||
| 479 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
|
50 | if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */ |
| 480 | ✗ | return AVERROR(EINVAL); | |
| 481 | |||
| 482 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 37 times.
|
50 | if (type != st->codecpar->codec_type) |
| 483 | 13 | match = 0; | |
| 484 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
50 | if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) |
| 485 | ✗ | match = 0; | |
| 486 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 | } else if (*spec == 'g' && *(spec + 1) == ':') { |
| 487 | ✗ | int64_t group_idx = -1, group_id = -1; | |
| 488 | ✗ | int found = 0; | |
| 489 | char *endptr; | ||
| 490 | ✗ | spec += 2; | |
| 491 | ✗ | if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) { | |
| 492 | ✗ | spec += 1 + (*spec == 'i'); | |
| 493 | ✗ | group_id = strtol(spec, &endptr, 0); | |
| 494 | ✗ | if (spec == endptr || (*endptr && *endptr++ != ':')) | |
| 495 | ✗ | return AVERROR(EINVAL); | |
| 496 | ✗ | spec = endptr; | |
| 497 | } else { | ||
| 498 | ✗ | group_idx = strtol(spec, &endptr, 0); | |
| 499 | /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */ | ||
| 500 | ✗ | if (spec == endptr || (*endptr && *endptr++ != ':')) | |
| 501 | ✗ | return AVERROR(EINVAL); | |
| 502 | ✗ | spec = endptr; | |
| 503 | } | ||
| 504 | ✗ | if (match) { | |
| 505 | ✗ | if (group_id > 0) { | |
| 506 | ✗ | for (unsigned i = 0; i < s->nb_stream_groups; i++) { | |
| 507 | ✗ | if (group_id == s->stream_groups[i]->id) { | |
| 508 | ✗ | group_idx = i; | |
| 509 | ✗ | break; | |
| 510 | } | ||
| 511 | } | ||
| 512 | } | ||
| 513 | ✗ | if (group_idx < 0 || group_idx >= s->nb_stream_groups) | |
| 514 | ✗ | return AVERROR(EINVAL); | |
| 515 | ✗ | for (unsigned j = 0; j < s->stream_groups[group_idx]->nb_streams; j++) { | |
| 516 | ✗ | if (st->index == s->stream_groups[group_idx]->streams[j]->index) { | |
| 517 | ✗ | found = 1; | |
| 518 | ✗ | if (g) | |
| 519 | ✗ | *g = s->stream_groups[group_idx]; | |
| 520 | ✗ | break; | |
| 521 | } | ||
| 522 | } | ||
| 523 | } | ||
| 524 | ✗ | if (!found) | |
| 525 | ✗ | match = 0; | |
| 526 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
12 | } else if (*spec == 'p' && *(spec + 1) == ':') { |
| 527 | int prog_id; | ||
| 528 | 6 | int found = 0; | |
| 529 | char *endptr; | ||
| 530 | 6 | spec += 2; | |
| 531 | 6 | prog_id = strtol(spec, &endptr, 0); | |
| 532 | /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */ | ||
| 533 |
3/6✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
|
6 | if (spec == endptr || (*endptr && *endptr++ != ':')) |
| 534 | ✗ | return AVERROR(EINVAL); | |
| 535 | 6 | spec = endptr; | |
| 536 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (match) { |
| 537 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | for (unsigned i = 0; i < s->nb_programs; i++) { |
| 538 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (s->programs[i]->id != prog_id) |
| 539 | ✗ | continue; | |
| 540 | |||
| 541 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) { |
| 542 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
|
8 | if (st->index == s->programs[i]->stream_index[j]) { |
| 543 | 5 | found = 1; | |
| 544 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (p) |
| 545 | 2 | *p = s->programs[i]; | |
| 546 | 5 | i = s->nb_programs; | |
| 547 | 5 | break; | |
| 548 | } | ||
| 549 | } | ||
| 550 | } | ||
| 551 | } | ||
| 552 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (!found) |
| 553 | 1 | match = 0; | |
| 554 | ✗ | } else if (*spec == '#' || | |
| 555 | ✗ | (*spec == 'i' && *(spec + 1) == ':')) { | |
| 556 | int stream_id; | ||
| 557 | char *endptr; | ||
| 558 | ✗ | spec += 1 + (*spec == 'i'); | |
| 559 | ✗ | stream_id = strtol(spec, &endptr, 0); | |
| 560 | ✗ | if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */ | |
| 561 | ✗ | return AVERROR(EINVAL); | |
| 562 | ✗ | return match && (stream_id == st->id); | |
| 563 | ✗ | } else if (*spec == 'm' && *(spec + 1) == ':') { | |
| 564 | const AVDictionaryEntry *tag; | ||
| 565 | char *key, *val; | ||
| 566 | int ret; | ||
| 567 | |||
| 568 | ✗ | if (match) { | |
| 569 | ✗ | spec += 2; | |
| 570 | ✗ | val = strchr(spec, ':'); | |
| 571 | |||
| 572 | ✗ | key = val ? av_strndup(spec, val - spec) : av_strdup(spec); | |
| 573 | ✗ | if (!key) | |
| 574 | ✗ | return AVERROR(ENOMEM); | |
| 575 | |||
| 576 | ✗ | tag = av_dict_get(st->metadata, key, NULL, 0); | |
| 577 | ✗ | if (tag) { | |
| 578 | ✗ | if (!val || !strcmp(tag->value, val + 1)) | |
| 579 | ✗ | ret = 1; | |
| 580 | else | ||
| 581 | ✗ | ret = 0; | |
| 582 | } else | ||
| 583 | ✗ | ret = 0; | |
| 584 | |||
| 585 | ✗ | av_freep(&key); | |
| 586 | } | ||
| 587 | ✗ | return match && ret; | |
| 588 | ✗ | } else if (*spec == 'u' && *(spec + 1) == '\0') { | |
| 589 | ✗ | const AVCodecParameters *par = st->codecpar; | |
| 590 | int val; | ||
| 591 | ✗ | switch (par->codec_type) { | |
| 592 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
| 593 | ✗ | val = par->sample_rate && par->ch_layout.nb_channels; | |
| 594 | ✗ | if (par->format == AV_SAMPLE_FMT_NONE) | |
| 595 | ✗ | return 0; | |
| 596 | ✗ | break; | |
| 597 | ✗ | case AVMEDIA_TYPE_VIDEO: | |
| 598 | ✗ | val = par->width && par->height; | |
| 599 | ✗ | if (par->format == AV_PIX_FMT_NONE) | |
| 600 | ✗ | return 0; | |
| 601 | ✗ | break; | |
| 602 | ✗ | case AVMEDIA_TYPE_UNKNOWN: | |
| 603 | ✗ | val = 0; | |
| 604 | ✗ | break; | |
| 605 | ✗ | default: | |
| 606 | ✗ | val = 1; | |
| 607 | ✗ | break; | |
| 608 | } | ||
| 609 | ✗ | return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0); | |
| 610 | } else { | ||
| 611 | ✗ | return AVERROR(EINVAL); | |
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | 36 | return match; | |
| 616 | } | ||
| 617 | |||
| 618 | 51 | int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, | |
| 619 | const char *spec) | ||
| 620 | { | ||
| 621 | int ret, index; | ||
| 622 | char *endptr; | ||
| 623 | 51 | const char *indexptr = NULL; | |
| 624 | 51 | const AVStreamGroup *g = NULL; | |
| 625 | 51 | const AVProgram *p = NULL; | |
| 626 | int nb_streams; | ||
| 627 | |||
| 628 | 51 | ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p); | |
| 629 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
|
51 | if (ret < 0) |
| 630 | ✗ | goto error; | |
| 631 | |||
| 632 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 15 times.
|
51 | if (!indexptr) |
| 633 | 36 | return ret; | |
| 634 | |||
| 635 | 15 | index = strtol(indexptr, &endptr, 0); | |
| 636 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (*endptr) { /* We can't have anything after the requested index. */ |
| 637 | ✗ | ret = AVERROR(EINVAL); | |
| 638 | ✗ | goto error; | |
| 639 | } | ||
| 640 | |||
| 641 | /* This is not really needed but saves us a loop for simple stream index specifiers. */ | ||
| 642 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
|
15 | if (spec == indexptr) |
| 643 | 8 | return (index == st->index); | |
| 644 | |||
| 645 | /* If we requested a matching stream index, we have to ensure st is that. */ | ||
| 646 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
|
7 | nb_streams = g ? g->nb_streams : (p ? p->nb_stream_indexes : s->nb_streams); |
| 647 |
3/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 4 times.
|
11 | for (int i = 0; i < nb_streams && index >= 0; i++) { |
| 648 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
|
7 | unsigned idx = g ? g->streams[i]->index : (p ? p->stream_index[i] : i); |
| 649 | 7 | const AVStream *candidate = s->streams[idx]; | |
| 650 | 7 | ret = match_stream_specifier(s, candidate, spec, NULL, NULL, NULL); | |
| 651 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (ret < 0) |
| 652 | ✗ | goto error; | |
| 653 |
4/6✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 4 times.
|
7 | if (ret > 0 && index-- == 0 && st == candidate) |
| 654 | 3 | return 1; | |
| 655 | } | ||
| 656 | 4 | return 0; | |
| 657 | |||
| 658 | ✗ | error: | |
| 659 | ✗ | if (ret == AVERROR(EINVAL)) | |
| 660 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec); | |
| 661 | ✗ | return ret; | |
| 662 | } | ||
| 663 | |||
| 664 | 690 | AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame) | |
| 665 | { | ||
| 666 | 690 | AVRational undef = {0, 1}; | |
| 667 |
1/2✓ Branch 0 taken 690 times.
✗ Branch 1 not taken.
|
690 | AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef; |
| 668 |
2/4✓ Branch 0 taken 690 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 690 times.
✗ Branch 3 not taken.
|
690 | AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef; |
| 669 |
2/2✓ Branch 0 taken 584 times.
✓ Branch 1 taken 106 times.
|
690 | AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio; |
| 670 | |||
| 671 | 690 | av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den, | |
| 672 | 690 | stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX); | |
| 673 |
3/4✓ Branch 0 taken 538 times.
✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 538 times.
|
690 | if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0) |
| 674 | 152 | stream_sample_aspect_ratio = undef; | |
| 675 | |||
| 676 | 690 | av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den, | |
| 677 | 690 | frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX); | |
| 678 |
3/4✓ Branch 0 taken 459 times.
✓ Branch 1 taken 231 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 459 times.
|
690 | if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0) |
| 679 | 231 | frame_sample_aspect_ratio = undef; | |
| 680 | |||
| 681 |
2/2✓ Branch 0 taken 538 times.
✓ Branch 1 taken 152 times.
|
690 | if (stream_sample_aspect_ratio.num) |
| 682 | 538 | return stream_sample_aspect_ratio; | |
| 683 | else | ||
| 684 | 152 | return frame_sample_aspect_ratio; | |
| 685 | } | ||
| 686 | |||
| 687 | 5683 | AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame) | |
| 688 | { | ||
| 689 | 5683 | AVRational fr = st->r_frame_rate; | |
| 690 | 5683 | const AVCodecDescriptor *desc = cffstream(st)->codec_desc; | |
| 691 | 5683 | AVRational avg_fr = st->avg_frame_rate; | |
| 692 | |||
| 693 |
7/10✓ Branch 0 taken 5038 times.
✓ Branch 1 taken 645 times.
✓ Branch 2 taken 5038 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5038 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5038 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5013 times.
✓ Branch 9 taken 25 times.
|
10721 | if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 && |
| 694 |
2/2✓ Branch 2 taken 392 times.
✓ Branch 3 taken 4621 times.
|
10051 | av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) { |
| 695 | 392 | fr = avg_fr; | |
| 696 | } | ||
| 697 | |||
| 698 |
3/4✓ Branch 0 taken 5683 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 325 times.
✓ Branch 3 taken 5358 times.
|
5683 | if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) { |
| 699 | 325 | const AVCodecContext *const avctx = ffstream(st)->avctx; | |
| 700 | 325 | AVRational codec_fr = avctx->framerate; | |
| 701 | |||
| 702 |
3/4✓ Branch 0 taken 129 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
|
325 | if ( codec_fr.num > 0 && codec_fr.den > 0 && |
| 703 |
5/6✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 92 times.
✓ Branch 7 taken 35 times.
✓ Branch 8 taken 2 times.
|
129 | (fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1)) |
| 704 | 35 | fr = codec_fr; | |
| 705 | } | ||
| 706 | |||
| 707 | 5683 | return fr; | |
| 708 | } | ||
| 709 | |||
| 710 | #if FF_API_INTERNAL_TIMING | ||
| 711 | ✗ | int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt, | |
| 712 | AVStream *ost, const AVStream *ist, | ||
| 713 | enum AVTimebaseSource copy_tb) | ||
| 714 | { | ||
| 715 | ✗ | const AVCodecDescriptor *desc = cffstream(ist)->codec_desc; | |
| 716 | ✗ | const AVCodecContext *const dec_ctx = cffstream(ist)->avctx; | |
| 717 | |||
| 718 | ✗ | AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 }; | |
| 719 | ✗ | AVRational dec_ctx_framerate = dec_ctx ? dec_ctx->framerate : (AVRational){ 0, 0 }; | |
| 720 | ✗ | AVRational dec_ctx_tb = dec_ctx_framerate.num ? av_inv_q(av_mul_q(dec_ctx_framerate, mul)) | |
| 721 | ✗ | : (ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? (AVRational){0, 1} | |
| 722 | ✗ | : ist->time_base); | |
| 723 | ✗ | AVRational enc_tb = ist->time_base; | |
| 724 | |||
| 725 | /* | ||
| 726 | * Avi is a special case here because it supports variable fps but | ||
| 727 | * having the fps and timebase differe significantly adds quite some | ||
| 728 | * overhead | ||
| 729 | */ | ||
| 730 | ✗ | if (!strcmp(ofmt->name, "avi")) { | |
| 731 | #if FF_API_R_FRAME_RATE | ||
| 732 | ✗ | if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num | |
| 733 | ✗ | && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate) | |
| 734 | ✗ | && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base) | |
| 735 | ✗ | && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx_tb) | |
| 736 | ✗ | && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx_tb) < 1.0/500 | |
| 737 | ✗ | || copy_tb == AVFMT_TBCF_R_FRAMERATE) { | |
| 738 | ✗ | enc_tb.num = ist->r_frame_rate.den; | |
| 739 | ✗ | enc_tb.den = 2*ist->r_frame_rate.num; | |
| 740 | } else | ||
| 741 | #endif | ||
| 742 | ✗ | if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num && | |
| 743 | ✗ | av_q2d(av_inv_q(dec_ctx_framerate)) > 2*av_q2d(ist->time_base) | |
| 744 | ✗ | && av_q2d(ist->time_base) < 1.0/500 | |
| 745 | ✗ | || (copy_tb == AVFMT_TBCF_DECODER && | |
| 746 | ✗ | (dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) { | |
| 747 | ✗ | enc_tb = dec_ctx_tb; | |
| 748 | ✗ | enc_tb.den *= 2; | |
| 749 | } | ||
| 750 | ✗ | } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS) | |
| 751 | ✗ | && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) { | |
| 752 | ✗ | if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num | |
| 753 | ✗ | && av_q2d(av_inv_q(dec_ctx_framerate)) > av_q2d(ist->time_base) | |
| 754 | ✗ | && av_q2d(ist->time_base) < 1.0/500 | |
| 755 | ✗ | || (copy_tb == AVFMT_TBCF_DECODER && | |
| 756 | ✗ | (dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) { | |
| 757 | ✗ | enc_tb = dec_ctx_tb; | |
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | ✗ | if (ost->codecpar->codec_tag == AV_RL32("tmcd") | |
| 762 | ✗ | && dec_ctx_tb.num < dec_ctx_tb.den | |
| 763 | ✗ | && dec_ctx_tb.num > 0 | |
| 764 | ✗ | && 121LL*dec_ctx_tb.num > dec_ctx_tb.den) { | |
| 765 | ✗ | enc_tb = dec_ctx_tb; | |
| 766 | } | ||
| 767 | |||
| 768 | ✗ | av_reduce(&ffstream(ost)->transferred_mux_tb.num, | |
| 769 | ✗ | &ffstream(ost)->transferred_mux_tb.den, | |
| 770 | ✗ | enc_tb.num, enc_tb.den, INT_MAX); | |
| 771 | |||
| 772 | ✗ | return 0; | |
| 773 | } | ||
| 774 | |||
| 775 | ✗ | AVRational av_stream_get_codec_timebase(const AVStream *st) | |
| 776 | { | ||
| 777 | ✗ | return cffstream(st)->avctx ? cffstream(st)->avctx->time_base : cffstream(st)->transferred_mux_tb; | |
| 778 | } | ||
| 779 | #endif | ||
| 780 | |||
| 781 | 21717 | void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, | |
| 782 | unsigned int pts_num, unsigned int pts_den) | ||
| 783 | { | ||
| 784 | 21717 | FFStream *const sti = ffstream(st); | |
| 785 | AVRational new_tb; | ||
| 786 |
1/2✓ Branch 1 taken 21717 times.
✗ Branch 2 not taken.
|
21717 | if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) { |
| 787 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 21173 times.
|
21717 | if (new_tb.num != pts_num) |
| 788 | 544 | av_log(NULL, AV_LOG_DEBUG, | |
| 789 | "st:%d removing common factor %d from timebase\n", | ||
| 790 | 544 | st->index, pts_num / new_tb.num); | |
| 791 | } else | ||
| 792 | ✗ | av_log(NULL, AV_LOG_WARNING, | |
| 793 | "st:%d has too large timebase, reducing\n", st->index); | ||
| 794 | |||
| 795 |
3/4✓ Branch 0 taken 21716 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21716 times.
|
21717 | if (new_tb.num <= 0 || new_tb.den <= 0) { |
| 796 | 1 | av_log(NULL, AV_LOG_ERROR, | |
| 797 | "Ignoring attempt to set invalid timebase %d/%d for st:%d\n", | ||
| 798 | new_tb.num, new_tb.den, | ||
| 799 | st->index); | ||
| 800 | 1 | return; | |
| 801 | } | ||
| 802 | 21716 | st->time_base = new_tb; | |
| 803 |
2/2✓ Branch 0 taken 17502 times.
✓ Branch 1 taken 4214 times.
|
21716 | if (sti->avctx) |
| 804 | 17502 | sti->avctx->pkt_timebase = new_tb; | |
| 805 | 21716 | st->pts_wrap_bits = pts_wrap_bits; | |
| 806 | } | ||
| 807 | |||
| 808 | 9711 | const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st, | |
| 809 | enum AVCodecID codec_id) | ||
| 810 | { | ||
| 811 |
4/4✓ Branch 0 taken 6775 times.
✓ Branch 1 taken 2691 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 142 times.
|
9711 | switch (st->codecpar->codec_type) { |
| 812 | 6775 | case AVMEDIA_TYPE_VIDEO: | |
| 813 |
2/2✓ Branch 0 taken 3144 times.
✓ Branch 1 taken 3631 times.
|
6775 | if (s->video_codec) return s->video_codec; |
| 814 | 3631 | break; | |
| 815 | 2691 | case AVMEDIA_TYPE_AUDIO: | |
| 816 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2673 times.
|
2691 | if (s->audio_codec) return s->audio_codec; |
| 817 | 2673 | break; | |
| 818 | 103 | case AVMEDIA_TYPE_SUBTITLE: | |
| 819 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | if (s->subtitle_codec) return s->subtitle_codec; |
| 820 | 103 | break; | |
| 821 | } | ||
| 822 | |||
| 823 | 6549 | return avcodec_find_decoder(codec_id); | |
| 824 | } | ||
| 825 | |||
| 826 | 85 | int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src) | |
| 827 | { | ||
| 828 | #define OFF(field) offsetof(AVFormatContext, field) | ||
| 829 | static const unsigned offsets[] = { | ||
| 830 | OFF(codec_whitelist), OFF(format_whitelist), | ||
| 831 | OFF(protocol_whitelist), OFF(protocol_blacklist), | ||
| 832 | }; | ||
| 833 | #undef OFF | ||
| 834 |
4/8✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 85 times.
|
85 | av_assert0(!dst->codec_whitelist && |
| 835 | !dst->format_whitelist && | ||
| 836 | !dst->protocol_whitelist && | ||
| 837 | !dst->protocol_blacklist); | ||
| 838 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 85 times.
|
425 | for (unsigned i = 0; i < FF_ARRAY_ELEMS(offsets); i++) { |
| 839 | 340 | const char *src_str = *(char *const*)((const char*)src + offsets[i]); | |
| 840 | |||
| 841 |
2/2✓ Branch 0 taken 85 times.
✓ Branch 1 taken 255 times.
|
340 | if (src_str) { |
| 842 | 85 | char *dst_str = av_strdup(src_str); | |
| 843 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
|
85 | if (!dst_str) { |
| 844 | ✗ | av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n"); | |
| 845 | ✗ | return AVERROR(ENOMEM); | |
| 846 | } | ||
| 847 | |||
| 848 | 85 | *(char **)((char*)dst + offsets[i]) = dst_str; | |
| 849 | } | ||
| 850 | } | ||
| 851 | 85 | return 0; | |
| 852 | } | ||
| 853 | |||
| 854 | 631244 | int ff_is_intra_only(enum AVCodecID id) | |
| 855 | { | ||
| 856 | 631244 | const AVCodecDescriptor *d = avcodec_descriptor_get(id); | |
| 857 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 631179 times.
|
631244 | if (!d) |
| 858 | 65 | return 0; | |
| 859 |
4/4✓ Branch 0 taken 373545 times.
✓ Branch 1 taken 257634 times.
✓ Branch 2 taken 370099 times.
✓ Branch 3 taken 3446 times.
|
631179 | if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) && |
| 860 |
2/2✓ Branch 0 taken 200728 times.
✓ Branch 1 taken 427005 times.
|
627733 | !(d->props & AV_CODEC_PROP_INTRA_ONLY)) |
| 861 | 200728 | return 0; | |
| 862 | 430451 | return 1; | |
| 863 | } | ||
| 864 | |||
| 865 | 94 | void ff_format_set_url(AVFormatContext *s, char *url) | |
| 866 | { | ||
| 867 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
|
94 | av_assert0(url); |
| 868 | 94 | av_freep(&s->url); | |
| 869 | 94 | s->url = url; | |
| 870 | 94 | } | |
| 871 | |||
| 872 | ✗ | int ff_format_check_set_url(AVFormatContext *s, const char *url) | |
| 873 | { | ||
| 874 | URLComponents uc; | ||
| 875 | ✗ | av_assert0(url); | |
| 876 | char proto[64]; | ||
| 877 | |||
| 878 | ✗ | int ret = ff_url_decompose(&uc, url, NULL); | |
| 879 | ✗ | if (ret < 0) | |
| 880 | ✗ | return ret; | |
| 881 | ✗ | av_strlcpy(proto, uc.scheme, FFMIN(sizeof(proto), uc.url_component_end_scheme - uc.scheme)); | |
| 882 | |||
| 883 | ✗ | if (s->protocol_whitelist && av_match_list(proto, s->protocol_whitelist, ',') <= 0) { | |
| 884 | ✗ | av_log(s, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", proto, s->protocol_whitelist); | |
| 885 | ✗ | return AVERROR(EINVAL); | |
| 886 | } | ||
| 887 | |||
| 888 | ✗ | if (s->protocol_blacklist && av_match_list(proto, s->protocol_blacklist, ',') > 0) { | |
| 889 | ✗ | av_log(s, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", proto, s->protocol_blacklist); | |
| 890 | ✗ | return AVERROR(EINVAL); | |
| 891 | } | ||
| 892 | |||
| 893 | ✗ | char *urldup = av_strdup(url); | |
| 894 | ✗ | if (!urldup) | |
| 895 | ✗ | return AVERROR(ENOMEM); | |
| 896 | |||
| 897 | ✗ | av_freep(&s->url); | |
| 898 | ✗ | s->url = urldup; | |
| 899 | ✗ | return 0; | |
| 900 | } | ||
| 901 | |||
| 902 | |||
| 903 | 111364 | int ff_format_io_close(AVFormatContext *s, AVIOContext **pb) | |
| 904 | { | ||
| 905 | 111364 | int ret = 0; | |
| 906 |
2/2✓ Branch 0 taken 107909 times.
✓ Branch 1 taken 3455 times.
|
111364 | if (*pb) |
| 907 | 107909 | ret = s->io_close2(s, *pb); | |
| 908 | 111364 | *pb = NULL; | |
| 909 | 111364 | return ret; | |
| 910 | } | ||
| 911 |