libavformat/avformat.c
| 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 "config_components.h" | ||
| 23 | |||
| 24 | #include <math.h> | ||
| 25 | #include "libavutil/avassert.h" | ||
| 26 | #include "libavutil/avstring.h" | ||
| 27 | #include "libavutil/channel_layout.h" | ||
| 28 | #include "libavutil/frame.h" | ||
| 29 | #include "libavutil/iamf.h" | ||
| 30 | #include "libavutil/mem.h" | ||
| 31 | #include "libavutil/opt.h" | ||
| 32 | #include "libavutil/pixfmt.h" | ||
| 33 | #include "libavutil/samplefmt.h" | ||
| 34 | #include "libavcodec/avcodec.h" | ||
| 35 | #include "libavcodec/codec.h" | ||
| 36 | #include "libavcodec/bsf.h" | ||
| 37 | #include "libavcodec/codec_desc.h" | ||
| 38 | #include "packet_internal.h" | ||
| 39 | #include "avformat.h" | ||
| 40 | #include "avformat_internal.h" | ||
| 41 | #include "avio.h" | ||
| 42 | #include "demux.h" | ||
| 43 | #include "mux.h" | ||
| 44 | #include "internal.h" | ||
| 45 | #include "url.h" | ||
| 46 | |||
| 47 | 20167 | void ff_free_stream(AVStream **pst) | |
| 48 | { | ||
| 49 | 20167 | AVStream *st = *pst; | |
| 50 | 20167 | FFStream *const sti = ffstream(st); | |
| 51 | |||
| 52 | 20167 | if (!st) | |
| 53 | ✗ | return; | |
| 54 | |||
| 55 | 20167 | if (st->attached_pic.data) | |
| 56 | 65 | av_packet_unref(&st->attached_pic); | |
| 57 | |||
| 58 | 20167 | av_parser_close(sti->parser); | |
| 59 | 20167 | avcodec_free_context(&sti->avctx); | |
| 60 | 20167 | av_bsf_free(&sti->bsfc); | |
| 61 | 20167 | av_freep(&sti->index_entries); | |
| 62 | 20167 | av_freep(&sti->probe_data.buf); | |
| 63 | |||
| 64 | 20167 | av_packet_free(&sti->parse_pkt); | |
| 65 | |||
| 66 | 20167 | av_bsf_free(&sti->extract_extradata.bsf); | |
| 67 | |||
| 68 | 20167 | if (sti->info) { | |
| 69 | 176 | av_freep(&sti->info->duration_error); | |
| 70 | 176 | av_freep(&sti->info); | |
| 71 | } | ||
| 72 | |||
| 73 | 20167 | av_dict_free(&st->metadata); | |
| 74 | 20167 | avcodec_parameters_free(&st->codecpar); | |
| 75 | 20167 | av_freep(&st->priv_data); | |
| 76 | |||
| 77 | 20167 | av_freep(pst); | |
| 78 | } | ||
| 79 | |||
| 80 | 200 | void ff_free_stream_group(AVStreamGroup **pstg) | |
| 81 | { | ||
| 82 | 200 | AVStreamGroup *stg = *pstg; | |
| 83 | |||
| 84 | 200 | if (!stg) | |
| 85 | ✗ | return; | |
| 86 | |||
| 87 | #if FF_API_STREAM_GROUP_ENTRIES | ||
| 88 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 89 | 200 | av_freep(&stg->streams); | |
| 90 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 91 | #endif | ||
| 92 | 1082 | for (int i = 0; i < stg->nb_entries; i++) | |
| 93 | 882 | av_freep(&stg->entries[i]); | |
| 94 | 200 | av_freep(&stg->entries); | |
| 95 | 200 | av_packet_side_data_free(&stg->coded_side_data, | |
| 96 | &stg->nb_coded_side_data); | ||
| 97 | 200 | av_dict_free(&stg->metadata); | |
| 98 | 200 | av_freep(&stg->priv_data); | |
| 99 | 200 | switch (stg->type) { | |
| 100 | 64 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: { | |
| 101 | 64 | av_iamf_audio_element_free(&stg->params.iamf_audio_element); | |
| 102 | 64 | break; | |
| 103 | } | ||
| 104 | 58 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: { | |
| 105 | 58 | av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation); | |
| 106 | 58 | break; | |
| 107 | } | ||
| 108 | 15 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: | |
| 109 | 15 | av_opt_free(stg->params.tile_grid); | |
| 110 | 15 | av_freep(&stg->params.tile_grid->offsets); | |
| 111 | #if FF_API_STREAM_GROUP_ENTRIES | ||
| 112 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 113 | 15 | av_packet_side_data_free(&stg->params.tile_grid->coded_side_data, | |
| 114 | 15 | &stg->params.tile_grid->nb_coded_side_data); | |
| 115 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 116 | #endif | ||
| 117 | 15 | av_freep(&stg->params.tile_grid); | |
| 118 | 15 | break; | |
| 119 | 39 | case AV_STREAM_GROUP_PARAMS_TREF: | |
| 120 | 39 | av_opt_free(stg->params.tref); | |
| 121 | 39 | av_freep(&stg->params.tref); | |
| 122 | 39 | break; | |
| 123 | 24 | case AV_STREAM_GROUP_PARAMS_LCEVC: | |
| 124 | case AV_STREAM_GROUP_PARAMS_DOLBY_VISION: | ||
| 125 | case AV_STREAM_GROUP_PARAMS_GAIN_MAP: | ||
| 126 | case AV_STREAM_GROUP_PARAMS_ALPHA: | ||
| 127 | 24 | av_opt_free(stg->params.layered_video); | |
| 128 | 24 | av_freep(&stg->params.layered_video); | |
| 129 | 24 | break; | |
| 130 | ✗ | default: | |
| 131 | ✗ | break; | |
| 132 | } | ||
| 133 | |||
| 134 | 200 | av_freep(pstg); | |
| 135 | } | ||
| 136 | |||
| 137 | 10 | void ff_remove_stream(AVFormatContext *s, AVStream *st) | |
| 138 | { | ||
| 139 | 10 | av_assert0(s->nb_streams>0); | |
| 140 | 10 | av_assert0(s->streams[ s->nb_streams - 1 ] == st); | |
| 141 | |||
| 142 | 10 | ff_free_stream(&s->streams[ --s->nb_streams ]); | |
| 143 | 10 | } | |
| 144 | |||
| 145 | 2 | void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg) | |
| 146 | { | ||
| 147 | 2 | av_assert0(s->nb_stream_groups > 0); | |
| 148 | 2 | av_assert0(s->stream_groups[ s->nb_stream_groups - 1 ] == stg); | |
| 149 | |||
| 150 | 2 | ff_free_stream_group(&s->stream_groups[ --s->nb_stream_groups ]); | |
| 151 | 2 | } | |
| 152 | |||
| 153 | /* XXX: suppress the packet queue */ | ||
| 154 | 14092 | void ff_flush_packet_queue(AVFormatContext *s) | |
| 155 | { | ||
| 156 | 14092 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 157 | 14092 | FFFormatContext *const si = &fci->fc; | |
| 158 | 14092 | ff_packet_list_free(&fci->parse_queue); | |
| 159 | 14092 | ff_packet_list_free(&si->packet_buffer); | |
| 160 | 14092 | ff_packet_list_free(&fci->raw_packet_buffer); | |
| 161 | |||
| 162 | 14092 | fci->raw_packet_buffer_size = 0; | |
| 163 | 14092 | } | |
| 164 | |||
| 165 | 18778 | void avformat_free_context(AVFormatContext *s) | |
| 166 | { | ||
| 167 | FormatContextInternal *fci; | ||
| 168 | FFFormatContext *si; | ||
| 169 | |||
| 170 | 18778 | if (!s) | |
| 171 | 713 | return; | |
| 172 | 18065 | fci = ff_fc_internal(s); | |
| 173 | 18065 | si = &fci->fc; | |
| 174 | |||
| 175 | 18065 | if (s->oformat && ffofmt(s->oformat)->deinit && fci->initialized) | |
| 176 | ✗ | ffofmt(s->oformat)->deinit(s); | |
| 177 | |||
| 178 | 18065 | av_opt_free(s); | |
| 179 | 18065 | if (s->iformat && s->iformat->priv_class && s->priv_data) | |
| 180 | 7474 | av_opt_free(s->priv_data); | |
| 181 | 18065 | if (s->oformat && s->oformat->priv_class && s->priv_data) | |
| 182 | 1 | av_opt_free(s->priv_data); | |
| 183 | |||
| 184 | 38222 | for (unsigned i = 0; i < s->nb_streams; i++) | |
| 185 | 20157 | ff_free_stream(&s->streams[i]); | |
| 186 | 18263 | for (unsigned i = 0; i < s->nb_stream_groups; i++) | |
| 187 | 198 | ff_free_stream_group(&s->stream_groups[i]); | |
| 188 | 18065 | s->nb_stream_groups = 0; | |
| 189 | 18065 | s->nb_streams = 0; | |
| 190 | |||
| 191 | 18206 | for (unsigned i = 0; i < s->nb_programs; i++) { | |
| 192 | 141 | av_dict_free(&s->programs[i]->metadata); | |
| 193 | 141 | av_freep(&s->programs[i]->stream_index); | |
| 194 | 141 | av_freep(&s->programs[i]); | |
| 195 | } | ||
| 196 | 18065 | s->nb_programs = 0; | |
| 197 | |||
| 198 | 18065 | av_freep(&s->programs); | |
| 199 | 18065 | av_freep(&s->priv_data); | |
| 200 | 18318 | while (s->nb_chapters--) { | |
| 201 | 253 | av_dict_free(&s->chapters[s->nb_chapters]->metadata); | |
| 202 | 253 | av_freep(&s->chapters[s->nb_chapters]); | |
| 203 | } | ||
| 204 | 18065 | av_freep(&s->chapters); | |
| 205 | 18065 | av_dict_free(&s->metadata); | |
| 206 | 18065 | av_dict_free(&si->id3v2_meta); | |
| 207 | #if CONFIG_LIBCURL_PROTOCOL | ||
| 208 | 18065 | ff_curl_loop_free(&si->curl_loop); | |
| 209 | #endif | ||
| 210 | 18065 | av_packet_free(&si->pkt); | |
| 211 | 18065 | av_packet_free(&si->parse_pkt); | |
| 212 | 18065 | ff_packet_list_free(&si->packet_buffer); | |
| 213 | 18065 | av_freep(&s->streams); | |
| 214 | 18065 | av_freep(&s->stream_groups); | |
| 215 | 18065 | if (s->iformat) | |
| 216 | 8727 | ff_flush_packet_queue(s); | |
| 217 | 18065 | av_freep(&s->url); | |
| 218 | 18065 | av_freep(&s->name); | |
| 219 | 18065 | av_free(s); | |
| 220 | } | ||
| 221 | |||
| 222 | /** | ||
| 223 | * Copy all stream parameters from source to destination stream, with the | ||
| 224 | * exception of the index field, which is usually set by avformat_new_stream(). | ||
| 225 | * | ||
| 226 | * @param dst pointer to destination AVStream | ||
| 227 | * @param src pointer to source AVStream | ||
| 228 | * @return >=0 on success, AVERROR code on error | ||
| 229 | */ | ||
| 230 | 20 | static int stream_params_copy(AVStream *dst, const AVStream *src) | |
| 231 | { | ||
| 232 | int ret; | ||
| 233 | |||
| 234 | 20 | dst->id = src->id; | |
| 235 | 20 | dst->time_base = src->time_base; | |
| 236 | 20 | dst->start_time = src->start_time; | |
| 237 | 20 | dst->duration = src->duration; | |
| 238 | 20 | dst->nb_frames = src->nb_frames; | |
| 239 | 20 | dst->disposition = src->disposition; | |
| 240 | 20 | dst->discard = src->discard; | |
| 241 | 20 | dst->sample_aspect_ratio = src->sample_aspect_ratio; | |
| 242 | 20 | dst->avg_frame_rate = src->avg_frame_rate; | |
| 243 | 20 | dst->event_flags = src->event_flags; | |
| 244 | 20 | dst->r_frame_rate = src->r_frame_rate; | |
| 245 | 20 | dst->pts_wrap_bits = src->pts_wrap_bits; | |
| 246 | |||
| 247 | 20 | av_dict_free(&dst->metadata); | |
| 248 | 20 | ret = av_dict_copy(&dst->metadata, src->metadata, 0); | |
| 249 | 20 | if (ret < 0) | |
| 250 | ✗ | return ret; | |
| 251 | |||
| 252 | 20 | ret = avcodec_parameters_copy(dst->codecpar, src->codecpar); | |
| 253 | 20 | if (ret < 0) | |
| 254 | ✗ | return ret; | |
| 255 | |||
| 256 | 20 | av_packet_unref(&dst->attached_pic); | |
| 257 | 20 | if (src->attached_pic.data) { | |
| 258 | ✗ | ret = av_packet_ref(&dst->attached_pic, &src->attached_pic); | |
| 259 | ✗ | if (ret < 0) | |
| 260 | ✗ | return ret; | |
| 261 | } | ||
| 262 | |||
| 263 | 20 | return 0; | |
| 264 | } | ||
| 265 | |||
| 266 | 20 | AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src) | |
| 267 | { | ||
| 268 | AVStream *st; | ||
| 269 | int ret; | ||
| 270 | |||
| 271 | 20 | st = avformat_new_stream(dst_ctx, NULL); | |
| 272 | 20 | if (!st) | |
| 273 | ✗ | return NULL; | |
| 274 | |||
| 275 | 20 | ret = stream_params_copy(st, src); | |
| 276 | 20 | if (ret < 0) { | |
| 277 | ✗ | ff_remove_stream(dst_ctx, st); | |
| 278 | ✗ | return NULL; | |
| 279 | } | ||
| 280 | |||
| 281 | 20 | return st; | |
| 282 | } | ||
| 283 | |||
| 284 | 162 | const char *avformat_stream_group_name(enum AVStreamGroupParamsType type) | |
| 285 | { | ||
| 286 | 162 | switch(type) { | |
| 287 | 47 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: return "IAMF Audio Element"; | |
| 288 | 43 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: return "IAMF Mix Presentation"; | |
| 289 | 21 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: return "Tile Grid"; | |
| 290 | 10 | case AV_STREAM_GROUP_PARAMS_LCEVC: return "LCEVC (Split video and enhancement)"; | |
| 291 | 32 | case AV_STREAM_GROUP_PARAMS_TREF: return "Track Reference"; | |
| 292 | 1 | case AV_STREAM_GROUP_PARAMS_DOLBY_VISION: return "Dolby Vision (Split base and enhancement layer)"; | |
| 293 | 5 | case AV_STREAM_GROUP_PARAMS_GAIN_MAP: return "Gain Map (Split base rendition and gain map)"; | |
| 294 | 3 | case AV_STREAM_GROUP_PARAMS_ALPHA: return "Alpha (Split color and alpha channels)"; | |
| 295 | } | ||
| 296 | ✗ | return NULL; | |
| 297 | } | ||
| 298 | |||
| 299 | 703 | AVProgram *av_new_program(AVFormatContext *ac, int id) | |
| 300 | { | ||
| 301 | 703 | FFFormatContext *const si = ffformatcontext(ac); | |
| 302 | 703 | AVProgram *program = NULL; | |
| 303 | int ret; | ||
| 304 | |||
| 305 | 703 | av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id); | |
| 306 | |||
| 307 | 703 | if (!ac->nb_programs) { | |
| 308 | 139 | si->program_ids_monotonic = 1; | |
| 309 | 564 | } else if (!si->program_ids_monotonic || ac->programs[ac->nb_programs - 1]->id >= id) { | |
| 310 | 1124 | for (unsigned i = 0; i < ac->nb_programs; i++) | |
| 311 | 562 | if (ac->programs[i]->id == id) | |
| 312 | 562 | program = ac->programs[i]; | |
| 313 | 562 | if (!program) | |
| 314 | ✗ | si->program_ids_monotonic = 0; | |
| 315 | } | ||
| 316 | |||
| 317 | 703 | if (!program) { | |
| 318 | 141 | program = av_mallocz(sizeof(*program)); | |
| 319 | 141 | if (!program) | |
| 320 | ✗ | return NULL; | |
| 321 | 141 | ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program); | |
| 322 | 141 | if (ret < 0) { | |
| 323 | ✗ | av_free(program); | |
| 324 | ✗ | return NULL; | |
| 325 | } | ||
| 326 | 141 | program->discard = AVDISCARD_NONE; | |
| 327 | 141 | program->pmt_version = -1; | |
| 328 | 141 | program->id = id; | |
| 329 | 141 | program->pts_wrap_reference = AV_NOPTS_VALUE; | |
| 330 | 141 | program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE; | |
| 331 | 141 | program->start_time = | |
| 332 | 141 | program->end_time = AV_NOPTS_VALUE; | |
| 333 | } | ||
| 334 | 703 | return program; | |
| 335 | } | ||
| 336 | |||
| 337 | 995 | int av_program_add_stream_index2(AVFormatContext *ac, int progid, unsigned idx) | |
| 338 | { | ||
| 339 | 995 | AVProgram *program = NULL; | |
| 340 | void *tmp; | ||
| 341 | |||
| 342 | 995 | if (idx >= ac->nb_streams) { | |
| 343 | ✗ | av_log(ac, AV_LOG_ERROR, "stream index %d is greater than stream count %d\n", idx, ac->nb_streams); | |
| 344 | ✗ | return AVERROR(EINVAL); | |
| 345 | } | ||
| 346 | |||
| 347 | 997 | for (unsigned i = 0; i < ac->nb_programs; i++) { | |
| 348 | 997 | if (ac->programs[i]->id != progid) | |
| 349 | 2 | continue; | |
| 350 | 995 | program = ac->programs[i]; | |
| 351 | 6752 | for (unsigned j = 0; j < program->nb_stream_indexes; j++) | |
| 352 | 6097 | if (program->stream_index[j] == idx) | |
| 353 | 340 | return 0; | |
| 354 | |||
| 355 | 655 | tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int)); | |
| 356 | 655 | if (!tmp) | |
| 357 | ✗ | return AVERROR(ENOMEM); | |
| 358 | 655 | program->stream_index = tmp; | |
| 359 | 655 | program->stream_index[program->nb_stream_indexes++] = idx; | |
| 360 | 655 | return 0; | |
| 361 | } | ||
| 362 | |||
| 363 | ✗ | av_log(ac, AV_LOG_ERROR, "no program with id %d found\n", progid); | |
| 364 | ✗ | return AVERROR(EINVAL); | |
| 365 | } | ||
| 366 | |||
| 367 | 995 | void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx) | |
| 368 | { | ||
| 369 | 995 | av_program_add_stream_index2(ac, progid, idx); | |
| 370 | 995 | return; | |
| 371 | } | ||
| 372 | |||
| 373 | ✗ | int av_program_copy(AVFormatContext *dst, const AVFormatContext *src, int progid, int flags) | |
| 374 | { | ||
| 375 | ✗ | const AVProgram *src_prog = NULL; | |
| 376 | ✗ | AVProgram *dst_prog = NULL; | |
| 377 | ✗ | int ret, idx = -1, match = -1; | |
| 378 | ✗ | int overwrite = flags & AVFMT_PROGCOPY_OVERWRITE; | |
| 379 | |||
| 380 | ✗ | if ((flags & AVFMT_PROGCOPY_MATCH_BY_ID) && (flags & AVFMT_PROGCOPY_MATCH_BY_INDEX)) | |
| 381 | ✗ | return AVERROR(EINVAL); | |
| 382 | ✗ | else if (flags & AVFMT_PROGCOPY_MATCH_BY_ID) | |
| 383 | ✗ | match = 0; | |
| 384 | ✗ | else if (flags & AVFMT_PROGCOPY_MATCH_BY_INDEX) | |
| 385 | ✗ | match = 1; | |
| 386 | |||
| 387 | ✗ | for (unsigned i = 0; i < src->nb_programs; i++) { | |
| 388 | ✗ | if (src->programs[i]->id == progid) { | |
| 389 | ✗ | if (src_prog) { | |
| 390 | ✗ | av_log(dst, AV_LOG_ERROR, "multiple programs found in source with same id 0x%04x. Not copying.\n", progid); | |
| 391 | ✗ | return AVERROR(EINVAL); | |
| 392 | } else { | ||
| 393 | ✗ | src_prog = src->programs[i]; | |
| 394 | } | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | ✗ | if (!src_prog) { | |
| 399 | ✗ | av_log(dst, AV_LOG_ERROR, "source program not found: id=0x%04x\n", progid); | |
| 400 | ✗ | return AVERROR(EINVAL); | |
| 401 | } | ||
| 402 | |||
| 403 | ✗ | for (unsigned i = 0; i < dst->nb_programs; i++) { | |
| 404 | ✗ | if (dst->programs[i]->id == progid) { | |
| 405 | ✗ | if (idx > -1) { | |
| 406 | ✗ | av_log(dst, AV_LOG_ERROR, "multiple programs found in target with same id 0x%04x. Not copying.\n", progid); | |
| 407 | ✗ | return AVERROR(EINVAL); | |
| 408 | } else { | ||
| 409 | ✗ | idx = i; | |
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | ✗ | if (idx >= 0 && !overwrite) | |
| 415 | ✗ | return AVERROR(EEXIST); | |
| 416 | |||
| 417 | ✗ | av_log(dst, AV_LOG_TRACE, "%s program: id=0x%04x\n", idx >= 0 ? "overwriting" : "copying", progid); | |
| 418 | |||
| 419 | ✗ | if (idx >= 0) { | |
| 420 | ✗ | dst_prog = dst->programs[idx]; | |
| 421 | ✗ | av_dict_free(&dst_prog->metadata); | |
| 422 | ✗ | av_freep(&dst_prog->stream_index); | |
| 423 | ✗ | dst_prog->nb_stream_indexes = 0; | |
| 424 | } else { | ||
| 425 | ✗ | dst_prog = av_new_program(dst, progid); | |
| 426 | ✗ | if (!dst_prog) | |
| 427 | ✗ | return AVERROR(ENOMEM); | |
| 428 | } | ||
| 429 | |||
| 430 | /* public fields */ | ||
| 431 | ✗ | dst_prog->id = src_prog->id; | |
| 432 | ✗ | dst_prog->flags = src_prog->flags; | |
| 433 | ✗ | dst_prog->discard = src_prog->discard; | |
| 434 | ✗ | dst_prog->program_num = src_prog->program_num; | |
| 435 | ✗ | dst_prog->pmt_pid = src_prog->pmt_pid; | |
| 436 | ✗ | dst_prog->pcr_pid = src_prog->pcr_pid; | |
| 437 | ✗ | dst_prog->pmt_version = src_prog->pmt_version; | |
| 438 | |||
| 439 | ✗ | if (match == -1 && src->nb_streams) { | |
| 440 | ✗ | match = 0; | |
| 441 | ✗ | for (unsigned i = 0; i < src->nb_streams && !match; i++) { | |
| 442 | ✗ | int src_id = src->streams[i]->id; | |
| 443 | ✗ | if (!src_id) { | |
| 444 | ✗ | match = 1; | |
| 445 | ✗ | break; | |
| 446 | } | ||
| 447 | ✗ | for (unsigned j=i+1; j < src->nb_streams; j++) { | |
| 448 | ✗ | int sib_id = src->streams[j]->id; | |
| 449 | ✗ | if (src_id == sib_id) { | |
| 450 | ✗ | match = 1; | |
| 451 | ✗ | break; | |
| 452 | } | ||
| 453 | } | ||
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | ✗ | for (unsigned i = 0; i < dst->nb_streams; i++) { | |
| 458 | ✗ | int dst_val = match ? i : dst->streams[i]->id; | |
| 459 | |||
| 460 | ✗ | for (unsigned j = 0; j < src_prog->nb_stream_indexes; j++) { | |
| 461 | ✗ | int src_val = match ? src_prog->stream_index[j] : src->streams[src_prog->stream_index[j]]->id; | |
| 462 | |||
| 463 | ✗ | if (dst_val == src_val) { | |
| 464 | ✗ | ret = av_program_add_stream_index2(dst, dst_prog->id, i); | |
| 465 | ✗ | if (ret < 0) | |
| 466 | ✗ | return ret; | |
| 467 | } | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | ✗ | ret = av_dict_copy(&dst_prog->metadata, src_prog->metadata, 0); | |
| 472 | ✗ | if (ret < 0) | |
| 473 | ✗ | return ret; | |
| 474 | |||
| 475 | ✗ | return 0; | |
| 476 | } | ||
| 477 | |||
| 478 | 18442 | AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s) | |
| 479 | { | ||
| 480 | 20504 | for (unsigned i = 0; i < ic->nb_programs; i++) { | |
| 481 | 4349 | if (ic->programs[i] == last) { | |
| 482 | 2050 | last = NULL; | |
| 483 | } else { | ||
| 484 | 2299 | if (!last) | |
| 485 | 13956 | for (unsigned j = 0; j < ic->programs[i]->nb_stream_indexes; j++) | |
| 486 | 13950 | if (ic->programs[i]->stream_index[j] == s) | |
| 487 | 2287 | return ic->programs[i]; | |
| 488 | } | ||
| 489 | } | ||
| 490 | 16155 | return NULL; | |
| 491 | } | ||
| 492 | |||
| 493 | 2138 | int av_find_default_stream_index(AVFormatContext *s) | |
| 494 | { | ||
| 495 | 2138 | int best_stream = 0; | |
| 496 | 2138 | int best_score = INT_MIN; | |
| 497 | |||
| 498 | 2138 | if (s->nb_streams <= 0) | |
| 499 | ✗ | return -1; | |
| 500 | 4808 | for (unsigned i = 0; i < s->nb_streams; i++) { | |
| 501 | 2670 | const AVStream *const st = s->streams[i]; | |
| 502 | 2670 | const FFStream *const sti = cffstream(st); | |
| 503 | 2670 | int score = 0; | |
| 504 | 2670 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { | |
| 505 | 1502 | if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) | |
| 506 | 6 | score -= 400; | |
| 507 | 1502 | if (st->codecpar->width && st->codecpar->height) | |
| 508 | 1404 | score += 50; | |
| 509 | 1502 | score+= 25; | |
| 510 | } | ||
| 511 | 2670 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { | |
| 512 | 1135 | if (st->codecpar->sample_rate) | |
| 513 | 1102 | score += 50; | |
| 514 | } | ||
| 515 | 2670 | if (sti->codec_info_nb_frames) | |
| 516 | 2156 | score += 12; | |
| 517 | |||
| 518 | 2670 | if (st->discard != AVDISCARD_ALL) | |
| 519 | 2661 | score += 200; | |
| 520 | |||
| 521 | 2670 | if (score > best_score) { | |
| 522 | 2201 | best_score = score; | |
| 523 | 2201 | best_stream = i; | |
| 524 | } | ||
| 525 | } | ||
| 526 | 2138 | return best_stream; | |
| 527 | } | ||
| 528 | |||
| 529 | 19 | int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, | |
| 530 | int wanted_stream_nb, int related_stream, | ||
| 531 | const AVCodec **decoder_ret, int flags) | ||
| 532 | { | ||
| 533 | 19 | int nb_streams = ic->nb_streams; | |
| 534 | 19 | int ret = AVERROR_STREAM_NOT_FOUND; | |
| 535 | 19 | int best_count = -1, best_multiframe = -1, best_disposition = -1; | |
| 536 | int count, multiframe, disposition; | ||
| 537 | 19 | int64_t best_bitrate = -1; | |
| 538 | int64_t bitrate; | ||
| 539 | 19 | unsigned *program = NULL; | |
| 540 | 19 | const AVCodec *decoder = NULL, *best_decoder = NULL; | |
| 541 | |||
| 542 | 19 | if (related_stream >= 0 && wanted_stream_nb < 0) { | |
| 543 | ✗ | AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream); | |
| 544 | ✗ | if (p) { | |
| 545 | ✗ | program = p->stream_index; | |
| 546 | ✗ | nb_streams = p->nb_stream_indexes; | |
| 547 | } | ||
| 548 | } | ||
| 549 | 41 | for (unsigned i = 0; i < nb_streams; i++) { | |
| 550 | 22 | int real_stream_index = program ? program[i] : i; | |
| 551 | 22 | AVStream *st = ic->streams[real_stream_index]; | |
| 552 | 22 | AVCodecParameters *par = st->codecpar; | |
| 553 | 22 | const AVCodecDescriptor *desc = avcodec_descriptor_get(par->codec_id); | |
| 554 | 22 | if (par->codec_type != type) | |
| 555 | 3 | continue; | |
| 556 | 19 | if (desc && (desc->props & AV_CODEC_PROP_ENHANCEMENT)) | |
| 557 | ✗ | continue; | |
| 558 | 19 | if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb) | |
| 559 | ✗ | continue; | |
| 560 | 19 | if (type == AVMEDIA_TYPE_AUDIO && !(par->ch_layout.nb_channels && par->sample_rate)) | |
| 561 | ✗ | continue; | |
| 562 | 19 | if (decoder_ret) { | |
| 563 | ✗ | decoder = ff_find_decoder(ic, st, par->codec_id); | |
| 564 | ✗ | if (!decoder) { | |
| 565 | ✗ | if (ret < 0) | |
| 566 | ✗ | ret = AVERROR_DECODER_NOT_FOUND; | |
| 567 | ✗ | continue; | |
| 568 | } | ||
| 569 | } | ||
| 570 | 19 | disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED)) | |
| 571 | 19 | + !! (st->disposition & AV_DISPOSITION_DEFAULT); | |
| 572 | 19 | count = ffstream(st)->codec_info_nb_frames; | |
| 573 | 19 | bitrate = par->bit_rate; | |
| 574 | 19 | multiframe = FFMIN(5, count); | |
| 575 | 19 | if ((best_disposition > disposition) || | |
| 576 | 19 | (best_disposition == disposition && best_multiframe > multiframe) || | |
| 577 | 19 | (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) || | |
| 578 | ✗ | (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count)) | |
| 579 | ✗ | continue; | |
| 580 | 19 | best_disposition = disposition; | |
| 581 | 19 | best_count = count; | |
| 582 | 19 | best_bitrate = bitrate; | |
| 583 | 19 | best_multiframe = multiframe; | |
| 584 | 19 | ret = real_stream_index; | |
| 585 | 19 | best_decoder = decoder; | |
| 586 | 19 | if (program && i == nb_streams - 1 && ret < 0) { | |
| 587 | ✗ | program = NULL; | |
| 588 | ✗ | nb_streams = ic->nb_streams; | |
| 589 | /* no related stream found, try again with everything */ | ||
| 590 | ✗ | i = 0; | |
| 591 | } | ||
| 592 | } | ||
| 593 | 19 | if (decoder_ret) | |
| 594 | ✗ | *decoder_ret = best_decoder; | |
| 595 | 19 | return ret; | |
| 596 | } | ||
| 597 | |||
| 598 | /** | ||
| 599 | * Matches a stream specifier (but ignores requested index). | ||
| 600 | * | ||
| 601 | * @param indexptr set to point to the requested stream index if there is one | ||
| 602 | * | ||
| 603 | * @return <0 on error | ||
| 604 | * 0 if st is NOT a matching stream | ||
| 605 | * >0 if st is a matching stream | ||
| 606 | */ | ||
| 607 | 187 | static int match_stream_specifier(const AVFormatContext *s, const AVStream *st, | |
| 608 | const char *spec, const char **indexptr, | ||
| 609 | const AVStreamGroup **g, const AVProgram **p) | ||
| 610 | { | ||
| 611 | 187 | int match = 1; /* Stores if the specifier matches so far. */ | |
| 612 | 378 | while (*spec) { | |
| 613 | 317 | if (*spec <= '9' && *spec >= '0') { /* opt:index */ | |
| 614 | 126 | if (indexptr) | |
| 615 | 65 | *indexptr = spec; | |
| 616 | 126 | return match; | |
| 617 | 191 | } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || | |
| 618 | 191 | *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */ | |
| 619 | enum AVMediaType type; | ||
| 620 | 179 | int nopic = 0; | |
| 621 | |||
| 622 | 179 | switch (*spec++) { | |
| 623 | 88 | case 'v': type = AVMEDIA_TYPE_VIDEO; break; | |
| 624 | 87 | case 'a': type = AVMEDIA_TYPE_AUDIO; break; | |
| 625 | 4 | case 's': type = AVMEDIA_TYPE_SUBTITLE; break; | |
| 626 | ✗ | case 'd': type = AVMEDIA_TYPE_DATA; break; | |
| 627 | ✗ | case 't': type = AVMEDIA_TYPE_ATTACHMENT; break; | |
| 628 | ✗ | case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break; | |
| 629 | ✗ | default: av_assert0(0); | |
| 630 | } | ||
| 631 | 179 | if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */ | |
| 632 | ✗ | return AVERROR(EINVAL); | |
| 633 | |||
| 634 | 179 | if (type != st->codecpar->codec_type) | |
| 635 | 41 | match = 0; | |
| 636 | 179 | if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) | |
| 637 | ✗ | match = 0; | |
| 638 | 12 | } else if (*spec == 'g' && *(spec + 1) == ':') { | |
| 639 | ✗ | int64_t group_idx = -1, group_id = -1; | |
| 640 | ✗ | int found = 0; | |
| 641 | char *endptr; | ||
| 642 | ✗ | spec += 2; | |
| 643 | ✗ | if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) { | |
| 644 | ✗ | spec += 1 + (*spec == 'i'); | |
| 645 | ✗ | group_id = strtol(spec, &endptr, 0); | |
| 646 | ✗ | if (spec == endptr || (*endptr && *endptr++ != ':')) | |
| 647 | ✗ | return AVERROR(EINVAL); | |
| 648 | ✗ | spec = endptr; | |
| 649 | } else { | ||
| 650 | ✗ | group_idx = strtol(spec, &endptr, 0); | |
| 651 | /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */ | ||
| 652 | ✗ | if (spec == endptr || (*endptr && *endptr++ != ':')) | |
| 653 | ✗ | return AVERROR(EINVAL); | |
| 654 | ✗ | spec = endptr; | |
| 655 | } | ||
| 656 | ✗ | if (match) { | |
| 657 | ✗ | if (group_id > 0) { | |
| 658 | ✗ | for (unsigned i = 0; i < s->nb_stream_groups; i++) { | |
| 659 | ✗ | if (group_id == s->stream_groups[i]->id) { | |
| 660 | ✗ | group_idx = i; | |
| 661 | ✗ | break; | |
| 662 | } | ||
| 663 | } | ||
| 664 | } | ||
| 665 | ✗ | if (group_idx < 0 || group_idx >= s->nb_stream_groups) | |
| 666 | ✗ | return AVERROR(EINVAL); | |
| 667 | ✗ | for (unsigned j = 0; j < s->stream_groups[group_idx]->nb_entries; j++) { | |
| 668 | ✗ | if (s->stream_groups[group_idx]->entries[j]->type != AV_STREAM_GROUP_ENTRY_STREAM) | |
| 669 | ✗ | continue; | |
| 670 | ✗ | if (st->index == s->stream_groups[group_idx]->entries[j]->stream->index) { | |
| 671 | ✗ | found = 1; | |
| 672 | ✗ | if (g) | |
| 673 | ✗ | *g = s->stream_groups[group_idx]; | |
| 674 | ✗ | break; | |
| 675 | } | ||
| 676 | } | ||
| 677 | } | ||
| 678 | ✗ | if (!found) | |
| 679 | ✗ | match = 0; | |
| 680 | 24 | } else if (*spec == 'p' && *(spec + 1) == ':') { | |
| 681 | int prog_id; | ||
| 682 | 12 | int found = 0; | |
| 683 | char *endptr; | ||
| 684 | 12 | spec += 2; | |
| 685 | 12 | prog_id = strtol(spec, &endptr, 0); | |
| 686 | /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */ | ||
| 687 | 12 | if (spec == endptr || (*endptr && *endptr++ != ':')) | |
| 688 | ✗ | return AVERROR(EINVAL); | |
| 689 | 12 | spec = endptr; | |
| 690 | 12 | if (match) { | |
| 691 | 24 | for (unsigned i = 0; i < s->nb_programs; i++) { | |
| 692 | 12 | if (s->programs[i]->id != prog_id) | |
| 693 | ✗ | continue; | |
| 694 | |||
| 695 | 18 | for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) { | |
| 696 | 16 | if (st->index == s->programs[i]->stream_index[j]) { | |
| 697 | 10 | found = 1; | |
| 698 | 10 | if (p) | |
| 699 | 4 | *p = s->programs[i]; | |
| 700 | 10 | i = s->nb_programs; | |
| 701 | 10 | break; | |
| 702 | } | ||
| 703 | } | ||
| 704 | } | ||
| 705 | } | ||
| 706 | 12 | if (!found) | |
| 707 | 2 | match = 0; | |
| 708 | ✗ | } else if (*spec == '#' || | |
| 709 | ✗ | (*spec == 'i' && *(spec + 1) == ':')) { | |
| 710 | int stream_id; | ||
| 711 | char *endptr; | ||
| 712 | ✗ | spec += 1 + (*spec == 'i'); | |
| 713 | ✗ | stream_id = strtol(spec, &endptr, 0); | |
| 714 | ✗ | if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */ | |
| 715 | ✗ | return AVERROR(EINVAL); | |
| 716 | ✗ | return match && (stream_id == st->id); | |
| 717 | ✗ | } else if (*spec == 'm' && *(spec + 1) == ':') { | |
| 718 | const AVDictionaryEntry *tag; | ||
| 719 | int ret; | ||
| 720 | |||
| 721 | ✗ | if (match) { | |
| 722 | ✗ | spec += 2; | |
| 723 | ✗ | const char *val = strchr(spec, ':'); | |
| 724 | |||
| 725 | ✗ | char *key = val ? av_strndup(spec, val - spec) : av_strdup(spec); | |
| 726 | ✗ | if (!key) | |
| 727 | ✗ | return AVERROR(ENOMEM); | |
| 728 | |||
| 729 | ✗ | tag = av_dict_get(st->metadata, key, NULL, 0); | |
| 730 | ✗ | if (tag) { | |
| 731 | ✗ | if (!val || !strcmp(tag->value, val + 1)) | |
| 732 | ✗ | ret = 1; | |
| 733 | else | ||
| 734 | ✗ | ret = 0; | |
| 735 | } else | ||
| 736 | ✗ | ret = 0; | |
| 737 | |||
| 738 | ✗ | av_freep(&key); | |
| 739 | } | ||
| 740 | ✗ | return match && ret; | |
| 741 | ✗ | } else if (*spec == 'u' && *(spec + 1) == '\0') { | |
| 742 | ✗ | const AVCodecParameters *par = st->codecpar; | |
| 743 | int val; | ||
| 744 | ✗ | switch (par->codec_type) { | |
| 745 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
| 746 | ✗ | val = par->sample_rate && par->ch_layout.nb_channels; | |
| 747 | ✗ | if (par->format == AV_SAMPLE_FMT_NONE) | |
| 748 | ✗ | return 0; | |
| 749 | ✗ | break; | |
| 750 | ✗ | case AVMEDIA_TYPE_VIDEO: | |
| 751 | ✗ | val = par->width && par->height; | |
| 752 | ✗ | if (par->format == AV_PIX_FMT_NONE) | |
| 753 | ✗ | return 0; | |
| 754 | ✗ | break; | |
| 755 | ✗ | case AVMEDIA_TYPE_UNKNOWN: | |
| 756 | ✗ | val = 0; | |
| 757 | ✗ | break; | |
| 758 | ✗ | default: | |
| 759 | ✗ | val = 1; | |
| 760 | ✗ | break; | |
| 761 | } | ||
| 762 | ✗ | return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0); | |
| 763 | } else { | ||
| 764 | ✗ | return AVERROR(EINVAL); | |
| 765 | } | ||
| 766 | } | ||
| 767 | |||
| 768 | 61 | return match; | |
| 769 | } | ||
| 770 | |||
| 771 | 126 | int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, | |
| 772 | const char *spec) | ||
| 773 | { | ||
| 774 | int ret, index; | ||
| 775 | char *endptr; | ||
| 776 | 126 | const char *indexptr = NULL; | |
| 777 | 126 | const AVStreamGroup *g = NULL; | |
| 778 | 126 | const AVProgram *p = NULL; | |
| 779 | int nb_streams; | ||
| 780 | |||
| 781 | 126 | ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p); | |
| 782 | 126 | if (ret < 0) | |
| 783 | ✗ | goto error; | |
| 784 | |||
| 785 | 126 | if (!indexptr) | |
| 786 | 61 | return ret; | |
| 787 | |||
| 788 | 65 | index = strtol(indexptr, &endptr, 0); | |
| 789 | 65 | if (*endptr) { /* We can't have anything after the requested index. */ | |
| 790 | ✗ | ret = AVERROR(EINVAL); | |
| 791 | ✗ | goto error; | |
| 792 | } | ||
| 793 | |||
| 794 | /* This is not really needed but saves us a loop for simple stream index specifiers. */ | ||
| 795 | 65 | if (spec == indexptr) | |
| 796 | 8 | return (index == st->index); | |
| 797 | |||
| 798 | /* If we requested a matching stream index, we have to ensure st is that. */ | ||
| 799 | 57 | nb_streams = g ? g->nb_entries : (p ? p->nb_stream_indexes : s->nb_streams); | |
| 800 | 83 | for (int i = 0; i < nb_streams && index >= 0; i++) { | |
| 801 | 61 | if (g && g->entries[i]->type != AV_STREAM_GROUP_ENTRY_STREAM) | |
| 802 | ✗ | continue; | |
| 803 | 61 | unsigned idx = g ? g->entries[i]->stream->index : (p ? p->stream_index[i] : i); | |
| 804 | 61 | const AVStream *candidate = s->streams[idx]; | |
| 805 | 61 | ret = match_stream_specifier(s, candidate, spec, NULL, NULL, NULL); | |
| 806 | 61 | if (ret < 0) | |
| 807 | ✗ | goto error; | |
| 808 | 61 | if (ret > 0 && index-- == 0 && st == candidate) | |
| 809 | 35 | return 1; | |
| 810 | } | ||
| 811 | 22 | return 0; | |
| 812 | |||
| 813 | ✗ | error: | |
| 814 | ✗ | if (ret == AVERROR(EINVAL)) | |
| 815 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec); | |
| 816 | ✗ | return ret; | |
| 817 | } | ||
| 818 | |||
| 819 | 1068 | AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame) | |
| 820 | { | ||
| 821 | 1068 | AVRational undef = {0, 1}; | |
| 822 | 1068 | AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef; | |
| 823 | 1068 | AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef; | |
| 824 | 1068 | AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio; | |
| 825 | |||
| 826 | 1068 | av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den, | |
| 827 | 1068 | stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX); | |
| 828 | 1068 | if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0) | |
| 829 | 448 | stream_sample_aspect_ratio = undef; | |
| 830 | |||
| 831 | 1068 | av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den, | |
| 832 | 1068 | frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX); | |
| 833 | 1068 | if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0) | |
| 834 | 478 | frame_sample_aspect_ratio = undef; | |
| 835 | |||
| 836 | 1068 | if (stream_sample_aspect_ratio.num) | |
| 837 | 620 | return stream_sample_aspect_ratio; | |
| 838 | else | ||
| 839 | 448 | return frame_sample_aspect_ratio; | |
| 840 | } | ||
| 841 | |||
| 842 | 5854 | AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame) | |
| 843 | { | ||
| 844 | 5854 | AVRational fr = st->r_frame_rate; | |
| 845 | 5854 | const AVCodecDescriptor *desc = cffstream(st)->codec_desc; | |
| 846 | 5854 | AVRational avg_fr = st->avg_frame_rate; | |
| 847 | |||
| 848 | 11043 | if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 && | |
| 849 | 10353 | av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) { | |
| 850 | 405 | fr = avg_fr; | |
| 851 | } | ||
| 852 | |||
| 853 | 5854 | if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) { | |
| 854 | 330 | const AVCodecContext *const avctx = ffstream(st)->avctx; | |
| 855 | 330 | AVRational codec_fr = avctx->framerate; | |
| 856 | |||
| 857 | 330 | if ( codec_fr.num > 0 && codec_fr.den > 0 && | |
| 858 | 130 | (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)) | |
| 859 | 35 | fr = codec_fr; | |
| 860 | } | ||
| 861 | |||
| 862 | 5854 | return fr; | |
| 863 | } | ||
| 864 | |||
| 865 | 24695 | void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, | |
| 866 | unsigned int pts_num, unsigned int pts_den) | ||
| 867 | { | ||
| 868 | 24695 | FFStream *const sti = ffstream(st); | |
| 869 | AVRational new_tb; | ||
| 870 | 24695 | if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) { | |
| 871 | 24695 | if (new_tb.num != pts_num) | |
| 872 | 602 | av_log(NULL, AV_LOG_DEBUG, | |
| 873 | "st:%d removing common factor %d from timebase\n", | ||
| 874 | 602 | st->index, pts_num / new_tb.num); | |
| 875 | } else | ||
| 876 | ✗ | av_log(NULL, AV_LOG_WARNING, | |
| 877 | "st:%d has too large timebase, reducing\n", st->index); | ||
| 878 | |||
| 879 | 24695 | if (new_tb.num <= 0 || new_tb.den <= 0) { | |
| 880 | 1 | av_log(NULL, AV_LOG_ERROR, | |
| 881 | "Ignoring attempt to set invalid timebase %d/%d for st:%d\n", | ||
| 882 | new_tb.num, new_tb.den, | ||
| 883 | st->index); | ||
| 884 | 1 | return; | |
| 885 | } | ||
| 886 | 24694 | st->time_base = new_tb; | |
| 887 | 24694 | if (sti->avctx) | |
| 888 | 20013 | sti->avctx->pkt_timebase = new_tb; | |
| 889 | 24694 | st->pts_wrap_bits = pts_wrap_bits; | |
| 890 | } | ||
| 891 | |||
| 892 | 11006 | const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st, | |
| 893 | enum AVCodecID codec_id) | ||
| 894 | { | ||
| 895 | 11006 | switch (st->codecpar->codec_type) { | |
| 896 | 7228 | case AVMEDIA_TYPE_VIDEO: | |
| 897 | 7228 | if (s->video_codec) return s->video_codec; | |
| 898 | 3977 | break; | |
| 899 | 3465 | case AVMEDIA_TYPE_AUDIO: | |
| 900 | 3465 | if (s->audio_codec) return s->audio_codec; | |
| 901 | 3437 | break; | |
| 902 | 130 | case AVMEDIA_TYPE_SUBTITLE: | |
| 903 | 130 | if (s->subtitle_codec) return s->subtitle_codec; | |
| 904 | 130 | break; | |
| 905 | } | ||
| 906 | |||
| 907 | 7727 | return avcodec_find_decoder(codec_id); | |
| 908 | } | ||
| 909 | |||
| 910 | 125 | int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src) | |
| 911 | { | ||
| 912 | #define OFF(field) offsetof(AVFormatContext, field) | ||
| 913 | static const unsigned offsets[] = { | ||
| 914 | OFF(codec_whitelist), OFF(format_whitelist), | ||
| 915 | OFF(protocol_whitelist), OFF(protocol_blacklist), | ||
| 916 | }; | ||
| 917 | #undef OFF | ||
| 918 | 125 | av_assert0(!dst->codec_whitelist && | |
| 919 | !dst->format_whitelist && | ||
| 920 | !dst->protocol_whitelist && | ||
| 921 | !dst->protocol_blacklist); | ||
| 922 | 625 | for (unsigned i = 0; i < FF_ARRAY_ELEMS(offsets); i++) { | |
| 923 | 500 | const char *src_str = *(char *const*)((const char*)src + offsets[i]); | |
| 924 | |||
| 925 | 500 | if (src_str) { | |
| 926 | 125 | char *dst_str = av_strdup(src_str); | |
| 927 | 125 | if (!dst_str) { | |
| 928 | ✗ | av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n"); | |
| 929 | ✗ | return AVERROR(ENOMEM); | |
| 930 | } | ||
| 931 | |||
| 932 | 125 | *(char **)((char*)dst + offsets[i]) = dst_str; | |
| 933 | } | ||
| 934 | } | ||
| 935 | 125 | if (src->recursion_limit <= 0) { | |
| 936 | ✗ | av_log(dst, AV_LOG_ERROR, "Too deep recursion\n"); | |
| 937 | ✗ | return AVERROR_INVALIDDATA; | |
| 938 | } | ||
| 939 | 125 | dst->recursion_limit = src->recursion_limit - 1; | |
| 940 | 125 | return 0; | |
| 941 | } | ||
| 942 | |||
| 943 | 1064681 | int ff_is_intra_only(enum AVCodecID id) | |
| 944 | { | ||
| 945 | 1064681 | const AVCodecDescriptor *d = avcodec_descriptor_get(id); | |
| 946 | 1064681 | if (!d) | |
| 947 | 65 | return 0; | |
| 948 | 1064616 | if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) && | |
| 949 | 1060684 | !(d->props & AV_CODEC_PROP_INTRA_ONLY)) | |
| 950 | 223662 | return 0; | |
| 951 | 840954 | return 1; | |
| 952 | } | ||
| 953 | |||
| 954 | 151 | void ff_format_set_url(AVFormatContext *s, char *url) | |
| 955 | { | ||
| 956 | 151 | av_assert0(url); | |
| 957 | 151 | av_freep(&s->url); | |
| 958 | 151 | s->url = url; | |
| 959 | 151 | } | |
| 960 | |||
| 961 | ✗ | int ff_format_check_set_url(AVFormatContext *s, const char *url) | |
| 962 | { | ||
| 963 | URLComponents uc; | ||
| 964 | ✗ | av_assert0(url); | |
| 965 | char proto[64]; | ||
| 966 | |||
| 967 | ✗ | int ret = ff_url_decompose(&uc, url, NULL); | |
| 968 | ✗ | if (ret < 0) | |
| 969 | ✗ | return ret; | |
| 970 | ✗ | av_strlcpy(proto, uc.scheme, FFMIN(sizeof(proto), uc.url_component_end_scheme - uc.scheme)); | |
| 971 | |||
| 972 | ✗ | if (s->protocol_whitelist && av_match_list(proto, s->protocol_whitelist, ',') <= 0) { | |
| 973 | ✗ | av_log(s, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", proto, s->protocol_whitelist); | |
| 974 | ✗ | return AVERROR(EINVAL); | |
| 975 | } | ||
| 976 | |||
| 977 | ✗ | if (s->protocol_blacklist && av_match_list(proto, s->protocol_blacklist, ',') > 0) { | |
| 978 | ✗ | av_log(s, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", proto, s->protocol_blacklist); | |
| 979 | ✗ | return AVERROR(EINVAL); | |
| 980 | } | ||
| 981 | |||
| 982 | ✗ | char *urldup = av_strdup(url); | |
| 983 | ✗ | if (!urldup) | |
| 984 | ✗ | return AVERROR(ENOMEM); | |
| 985 | |||
| 986 | ✗ | av_freep(&s->url); | |
| 987 | ✗ | s->url = urldup; | |
| 988 | ✗ | return 0; | |
| 989 | } | ||
| 990 | |||
| 991 | |||
| 992 | 114663 | int ff_format_io_close(AVFormatContext *s, AVIOContext **pb) | |
| 993 | { | ||
| 994 | 114663 | int ret = 0; | |
| 995 | 114663 | if (*pb) | |
| 996 | 110864 | ret = s->io_close2(s, *pb); | |
| 997 | 114663 | *pb = NULL; | |
| 998 | 114663 | return ret; | |
| 999 | } | ||
| 1000 |