1/* 2 * Core demuxing component 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 <stdint.h> 23 24#include "config_components.h" 25 26#include "libavutil/avassert.h" 27#include "libavutil/avstring.h" 28#include "libavutil/dict.h" 29#include "libavutil/internal.h" 30#include "libavutil/intreadwrite.h" 31#include "libavutil/mathematics.h" 32#include "libavutil/opt.h" 33#include "libavutil/pixfmt.h" 34#include "libavutil/time.h" 35#include "libavutil/timestamp.h" 36 37#include "libavcodec/bsf.h" 38#include "libavcodec/internal.h" 39#include "libavcodec/packet_internal.h" 40#include "libavcodec/raw.h" 41 42#include "avformat.h" 43#include "avio_internal.h" 44#include "demux.h" 45#include "id3v2.h" 46#include "internal.h" 47#include "url.h" 48 49static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp) 50{ 51 const FFStream *const sti = cffstream(st); 52 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 && 53 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) { 54 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET && 55 timestamp < sti->pts_wrap_reference) 56 return timestamp + (1ULL << st->pts_wrap_bits); 57 else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET && 58 timestamp >= sti->pts_wrap_reference) 59 return timestamp - (1ULL << st->pts_wrap_bits); 60 } 61 return timestamp; 62} 63 64int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp) 65{ 66 return wrap_timestamp(st, timestamp); 67} 68 69static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id) 70{ 71 const AVCodec *codec; 72 73#if CONFIG_H264_DECODER 74 /* Other parts of the code assume this decoder to be used for h264, 75 * so force it if possible. */ 76 if (codec_id == AV_CODEC_ID_H264) 77 return avcodec_find_decoder_by_name("h264"); 78#endif 79 80 codec = ff_find_decoder(s, st, codec_id); 81 if (!codec) 82 return NULL; 83 84 if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) { 85 const AVCodec *probe_codec = NULL; 86 void *iter = NULL; 87 while ((probe_codec = av_codec_iterate(&iter))) { 88 if (probe_codec->id == codec->id && 89 av_codec_is_decoder(probe_codec) && 90 !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) { 91 return probe_codec; 92 } 93 } 94 } 95 96 return codec; 97} 98 99static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, 100 AVProbeData *pd) 101{ 102 static const struct { 103 const char *name; 104 enum AVCodecID id; 105 enum AVMediaType type; 106 } fmt_id_type[] = { 107 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO }, 108 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO }, 109 { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO }, 110 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO }, 111 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE }, 112 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE }, 113 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO }, 114 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO }, 115 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO }, 116 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO }, 117 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO }, 118 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO }, 119 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO }, 120 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO }, 121 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO }, 122#ifdef OHOS_OPT_COMPAT 123 { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO }, 124#endif 125 { 0 } 126 }; 127 int score; 128 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score); 129 FFStream *const sti = ffstream(st); 130 131 if (fmt) { 132 av_log(s, AV_LOG_DEBUG, 133 "Probe with size=%d, packets=%d detected %s with score=%d\n", 134 pd->buf_size, s->max_probe_packets - sti->probe_packets, 135 fmt->name, score); 136 for (int i = 0; fmt_id_type[i].name; i++) { 137 if (!strcmp(fmt->name, fmt_id_type[i].name)) { 138 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO && 139 st->codecpar->sample_rate) 140 continue; 141 if (sti->request_probe > score && 142 st->codecpar->codec_id != fmt_id_type[i].id) 143 continue; 144 st->codecpar->codec_id = fmt_id_type[i].id; 145 st->codecpar->codec_type = fmt_id_type[i].type; 146 sti->need_context_update = 1; 147 return score; 148 } 149 } 150 } 151 return 0; 152} 153 154static int init_input(AVFormatContext *s, const char *filename, 155 AVDictionary **options) 156{ 157 int ret; 158 AVProbeData pd = { filename, NULL, 0 }; 159 int score = AVPROBE_SCORE_RETRY; 160 161 if (s->pb) { 162 s->flags |= AVFMT_FLAG_CUSTOM_IO; 163 if (!s->iformat) 164 return av_probe_input_buffer2(s->pb, &s->iformat, filename, 165 s, 0, s->format_probesize); 166 else if (s->iformat->flags & AVFMT_NOFILE) 167 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and " 168 "will be ignored with AVFMT_NOFILE format.\n"); 169 return 0; 170 } 171 172 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) || 173 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score)))) 174 return score; 175 176 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0) 177 return ret; 178 179 if (s->iformat) 180 return 0; 181 return av_probe_input_buffer2(s->pb, &s->iformat, filename, 182 s, 0, s->format_probesize); 183} 184 185static int update_stream_avctx(AVFormatContext *s) 186{ 187 int ret; 188 for (unsigned i = 0; i < s->nb_streams; i++) { 189 AVStream *const st = s->streams[i]; 190 FFStream *const sti = ffstream(st); 191 192 if (!sti->need_context_update) 193 continue; 194 195 /* close parser, because it depends on the codec */ 196 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) { 197 av_parser_close(sti->parser); 198 sti->parser = NULL; 199 } 200 201#if FF_API_OLD_CHANNEL_LAYOUT 202FF_DISABLE_DEPRECATION_WARNINGS 203 if (st->codecpar->ch_layout.nb_channels && 204 !st->codecpar->channels) { 205 st->codecpar->channels = st->codecpar->ch_layout.nb_channels; 206 st->codecpar->channel_layout = st->codecpar->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? 207 st->codecpar->ch_layout.u.mask : 0; 208 209 } 210FF_ENABLE_DEPRECATION_WARNINGS 211#endif 212 213 /* update internal codec context, for the parser */ 214 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); 215 if (ret < 0) 216 return ret; 217 218 sti->need_context_update = 0; 219 } 220 return 0; 221} 222 223int avformat_open_input(AVFormatContext **ps, const char *filename, 224 const AVInputFormat *fmt, AVDictionary **options) 225{ 226 AVFormatContext *s = *ps; 227 FFFormatContext *si; 228 AVDictionary *tmp = NULL; 229 ID3v2ExtraMeta *id3v2_extra_meta = NULL; 230 int ret = 0; 231 232 if (!s && !(s = avformat_alloc_context())) 233 return AVERROR(ENOMEM); 234 si = ffformatcontext(s); 235 if (!s->av_class) { 236 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n"); 237 return AVERROR(EINVAL); 238 } 239 if (fmt) 240 s->iformat = fmt; 241 242 if (options) 243 av_dict_copy(&tmp, *options, 0); 244 245 if (s->pb) // must be before any goto fail 246 s->flags |= AVFMT_FLAG_CUSTOM_IO; 247 248 if ((ret = av_opt_set_dict(s, &tmp)) < 0) 249 goto fail; 250 251 if (!(s->url = av_strdup(filename ? filename : ""))) { 252 ret = AVERROR(ENOMEM); 253 goto fail; 254 } 255 256 if ((ret = init_input(s, filename, &tmp)) < 0) 257 goto fail; 258 s->probe_score = ret; 259 260 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) { 261 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist); 262 if (!s->protocol_whitelist) { 263 ret = AVERROR(ENOMEM); 264 goto fail; 265 } 266 } 267 268 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) { 269 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist); 270 if (!s->protocol_blacklist) { 271 ret = AVERROR(ENOMEM); 272 goto fail; 273 } 274 } 275 276 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) { 277 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist); 278 ret = AVERROR(EINVAL); 279 goto fail; 280 } 281 282 avio_skip(s->pb, s->skip_initial_bytes); 283 284 /* Check filename in case an image number is expected. */ 285 if (s->iformat->flags & AVFMT_NEEDNUMBER) { 286 if (!av_filename_number_test(filename)) { 287 ret = AVERROR(EINVAL); 288 goto fail; 289 } 290 } 291 292 s->duration = s->start_time = AV_NOPTS_VALUE; 293 294 /* Allocate private data. */ 295 if (s->iformat->priv_data_size > 0) { 296 if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) { 297 ret = AVERROR(ENOMEM); 298 goto fail; 299 } 300 if (s->iformat->priv_class) { 301 *(const AVClass **) s->priv_data = s->iformat->priv_class; 302 av_opt_set_defaults(s->priv_data); 303 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0) 304 goto fail; 305 } 306 } 307 308 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */ 309 if (s->pb) 310 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta); 311 312 if (s->iformat->read_header) 313 if ((ret = s->iformat->read_header(s)) < 0) { 314 if (s->iformat->flags_internal & FF_FMT_INIT_CLEANUP) 315 goto close; 316 goto fail; 317 } 318 319 if (!s->metadata) { 320 s->metadata = si->id3v2_meta; 321 si->id3v2_meta = NULL; 322 } else if (si->id3v2_meta) { 323 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n"); 324 av_dict_free(&si->id3v2_meta); 325 } 326 327 if (id3v2_extra_meta) { 328 if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") || 329 !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) { 330 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0) 331 goto close; 332 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0) 333 goto close; 334 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0) 335 goto close; 336 } else 337 av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n"); 338 ff_id3v2_free_extra_meta(&id3v2_extra_meta); 339 } 340 341 if ((ret = avformat_queue_attached_pictures(s)) < 0) 342 goto close; 343 344 if (s->pb && !si->data_offset) 345 si->data_offset = avio_tell(s->pb); 346 347 si->raw_packet_buffer_size = 0; 348 349 update_stream_avctx(s); 350 351 if (options) { 352 av_dict_free(options); 353 *options = tmp; 354 } 355 *ps = s; 356 return 0; 357 358close: 359 if (s->iformat->read_close) 360 s->iformat->read_close(s); 361fail: 362 ff_id3v2_free_extra_meta(&id3v2_extra_meta); 363 av_dict_free(&tmp); 364 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO)) 365 avio_closep(&s->pb); 366 avformat_free_context(s); 367 *ps = NULL; 368 return ret; 369} 370 371void avformat_close_input(AVFormatContext **ps) 372{ 373 AVFormatContext *s; 374 AVIOContext *pb; 375 376 if (!ps || !*ps) 377 return; 378 379 s = *ps; 380 pb = s->pb; 381 382 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) || 383 (s->flags & AVFMT_FLAG_CUSTOM_IO)) 384 pb = NULL; 385 386 if (s->iformat) 387 if (s->iformat->read_close) 388 s->iformat->read_close(s); 389 390 avformat_free_context(s); 391 392 *ps = NULL; 393 394 avio_close(pb); 395} 396 397static void force_codec_ids(AVFormatContext *s, AVStream *st) 398{ 399 switch (st->codecpar->codec_type) { 400 case AVMEDIA_TYPE_VIDEO: 401 if (s->video_codec_id) 402 st->codecpar->codec_id = s->video_codec_id; 403 break; 404 case AVMEDIA_TYPE_AUDIO: 405 if (s->audio_codec_id) 406 st->codecpar->codec_id = s->audio_codec_id; 407 break; 408 case AVMEDIA_TYPE_SUBTITLE: 409 if (s->subtitle_codec_id) 410 st->codecpar->codec_id = s->subtitle_codec_id; 411 break; 412 case AVMEDIA_TYPE_DATA: 413 if (s->data_codec_id) 414 st->codecpar->codec_id = s->data_codec_id; 415 break; 416 } 417} 418 419static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt) 420{ 421 FFFormatContext *const si = ffformatcontext(s); 422 FFStream *const sti = ffstream(st); 423 424 if (sti->request_probe > 0) { 425 AVProbeData *const pd = &sti->probe_data; 426 int end; 427 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets); 428 --sti->probe_packets; 429 430 if (pkt) { 431 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE); 432 if (!new_buf) { 433 av_log(s, AV_LOG_WARNING, 434 "Failed to reallocate probe buffer for stream %d\n", 435 st->index); 436 goto no_packet; 437 } 438 pd->buf = new_buf; 439 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size); 440 pd->buf_size += pkt->size; 441 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE); 442 } else { 443no_packet: 444 sti->probe_packets = 0; 445 if (!pd->buf_size) { 446 av_log(s, AV_LOG_WARNING, 447 "nothing to probe for stream %d\n", st->index); 448 } 449 } 450 451 end = si->raw_packet_buffer_size >= s->probesize 452 || sti->probe_packets <= 0; 453 454 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) { 455 int score = set_codec_from_probe_data(s, st, pd); 456 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY) 457 || end) { 458 pd->buf_size = 0; 459 av_freep(&pd->buf); 460 sti->request_probe = -1; 461 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) { 462 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index); 463 } else 464 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index); 465 } 466 force_codec_ids(s, st); 467 } 468 } 469 return 0; 470} 471 472static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt) 473{ 474 FFStream *const sti = ffstream(st); 475 int64_t ref = pkt->dts; 476 int pts_wrap_behavior; 477 int64_t pts_wrap_reference; 478 AVProgram *first_program; 479 480 if (ref == AV_NOPTS_VALUE) 481 ref = pkt->pts; 482 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow) 483 return 0; 484 ref &= (1LL << st->pts_wrap_bits)-1; 485 486 // reference time stamp should be 60 s before first time stamp 487 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num); 488 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset 489 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) || 490 (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ? 491 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET; 492 493 first_program = av_find_program_from_stream(s, NULL, stream_index); 494 495 if (!first_program) { 496 int default_stream_index = av_find_default_stream_index(s); 497 FFStream *const default_sti = ffstream(s->streams[default_stream_index]); 498 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) { 499 for (unsigned i = 0; i < s->nb_streams; i++) { 500 FFStream *const sti = ffstream(s->streams[i]); 501 if (av_find_program_from_stream(s, NULL, i)) 502 continue; 503 sti->pts_wrap_reference = pts_wrap_reference; 504 sti->pts_wrap_behavior = pts_wrap_behavior; 505 } 506 } else { 507 sti->pts_wrap_reference = default_sti->pts_wrap_reference; 508 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior; 509 } 510 } else { 511 AVProgram *program = first_program; 512 while (program) { 513 if (program->pts_wrap_reference != AV_NOPTS_VALUE) { 514 pts_wrap_reference = program->pts_wrap_reference; 515 pts_wrap_behavior = program->pts_wrap_behavior; 516 break; 517 } 518 program = av_find_program_from_stream(s, program, stream_index); 519 } 520 521 // update every program with differing pts_wrap_reference 522 program = first_program; 523 while (program) { 524 if (program->pts_wrap_reference != pts_wrap_reference) { 525 for (unsigned i = 0; i < program->nb_stream_indexes; i++) { 526 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]); 527 sti->pts_wrap_reference = pts_wrap_reference; 528 sti->pts_wrap_behavior = pts_wrap_behavior; 529 } 530 531 program->pts_wrap_reference = pts_wrap_reference; 532 program->pts_wrap_behavior = pts_wrap_behavior; 533 } 534 program = av_find_program_from_stream(s, program, stream_index); 535 } 536 } 537 return 1; 538} 539 540int ff_read_packet(AVFormatContext *s, AVPacket *pkt) 541{ 542 FFFormatContext *const si = ffformatcontext(s); 543 int err; 544 545#if FF_API_INIT_PACKET 546FF_DISABLE_DEPRECATION_WARNINGS 547 pkt->data = NULL; 548 pkt->size = 0; 549 av_init_packet(pkt); 550FF_ENABLE_DEPRECATION_WARNINGS 551#else 552 av_packet_unref(pkt); 553#endif 554 555 for (;;) { 556 PacketListEntry *pktl = si->raw_packet_buffer.head; 557 AVStream *st; 558 FFStream *sti; 559 const AVPacket *pkt1; 560 561 if (pktl) { 562 AVStream *const st = s->streams[pktl->pkt.stream_index]; 563 if (si->raw_packet_buffer_size >= s->probesize) 564 if ((err = probe_codec(s, st, NULL)) < 0) 565 return err; 566 if (ffstream(st)->request_probe <= 0) { 567 avpriv_packet_list_get(&si->raw_packet_buffer, pkt); 568 si->raw_packet_buffer_size -= pkt->size; 569 return 0; 570 } 571 } 572 573 err = s->iformat->read_packet(s, pkt); 574 if (err < 0) { 575 av_packet_unref(pkt); 576 577 /* Some demuxers return FFERROR_REDO when they consume 578 data and discard it (ignored streams, junk, extradata). 579 We must re-call the demuxer to get the real packet. */ 580 if (err == FFERROR_REDO) 581 continue; 582 if (!pktl || err == AVERROR(EAGAIN)) 583 return err; 584 for (unsigned i = 0; i < s->nb_streams; i++) { 585 AVStream *const st = s->streams[i]; 586 FFStream *const sti = ffstream(st); 587 if (sti->probe_packets || sti->request_probe > 0) 588 if ((err = probe_codec(s, st, NULL)) < 0) 589 return err; 590 av_assert0(sti->request_probe <= 0); 591 } 592 continue; 593 } 594 595 err = av_packet_make_refcounted(pkt); 596 if (err < 0) { 597 av_packet_unref(pkt); 598 return err; 599 } 600 601 if (pkt->flags & AV_PKT_FLAG_CORRUPT) { 602 av_log(s, AV_LOG_WARNING, 603 "Packet corrupt (stream = %d, dts = %s)", 604 pkt->stream_index, av_ts2str(pkt->dts)); 605 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) { 606 av_log(s, AV_LOG_WARNING, ", dropping it.\n"); 607 av_packet_unref(pkt); 608 continue; 609 } 610 av_log(s, AV_LOG_WARNING, ".\n"); 611 } 612 613 av_assert0(pkt->stream_index < (unsigned)s->nb_streams && 614 "Invalid stream index.\n"); 615 616 st = s->streams[pkt->stream_index]; 617 sti = ffstream(st); 618 619 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) { 620 // correct first time stamps to negative values 621 if (!is_relative(sti->first_dts)) 622 sti->first_dts = wrap_timestamp(st, sti->first_dts); 623 if (!is_relative(st->start_time)) 624 st->start_time = wrap_timestamp(st, st->start_time); 625 if (!is_relative(sti->cur_dts)) 626 sti->cur_dts = wrap_timestamp(st, sti->cur_dts); 627 } 628 629 pkt->dts = wrap_timestamp(st, pkt->dts); 630 pkt->pts = wrap_timestamp(st, pkt->pts); 631 632 force_codec_ids(s, st); 633 634 /* TODO: audio: time filter; video: frame reordering (pts != dts) */ 635 if (s->use_wallclock_as_timestamps) 636 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base); 637 638 if (!pktl && sti->request_probe <= 0) 639 return 0; 640 641 err = avpriv_packet_list_put(&si->raw_packet_buffer, 642 pkt, NULL, 0); 643 if (err < 0) { 644 av_packet_unref(pkt); 645 return err; 646 } 647 pkt1 = &si->raw_packet_buffer.tail->pkt; 648 si->raw_packet_buffer_size += pkt1->size; 649 650 if ((err = probe_codec(s, st, pkt1)) < 0) 651 return err; 652 } 653} 654 655/** 656 * Return the frame duration in seconds. Return 0 if not available. 657 */ 658static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, 659 AVStream *st, AVCodecParserContext *pc, 660 AVPacket *pkt) 661{ 662 FFStream *const sti = ffstream(st); 663 AVRational codec_framerate = sti->avctx->framerate; 664 int frame_size, sample_rate; 665 666 *pnum = 0; 667 *pden = 0; 668 switch (st->codecpar->codec_type) { 669 case AVMEDIA_TYPE_VIDEO: 670 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) { 671 *pnum = st->r_frame_rate.den; 672 *pden = st->r_frame_rate.num; 673 } else if (st->time_base.num * 1000LL > st->time_base.den) { 674 *pnum = st->time_base.num; 675 *pden = st->time_base.den; 676 } else if (codec_framerate.den * 1000LL > codec_framerate.num) { 677 av_assert0(sti->avctx->ticks_per_frame); 678 av_reduce(pnum, pden, 679 codec_framerate.den, 680 codec_framerate.num * (int64_t)sti->avctx->ticks_per_frame, 681 INT_MAX); 682 683 if (pc && pc->repeat_pict) { 684 av_reduce(pnum, pden, 685 (*pnum) * (1LL + pc->repeat_pict), 686 (*pden), 687 INT_MAX); 688 } 689 /* If this codec can be interlaced or progressive then we need 690 * a parser to compute duration of a packet. Thus if we have 691 * no parser in such case leave duration undefined. */ 692 if (sti->avctx->ticks_per_frame > 1 && !pc) 693 *pnum = *pden = 0; 694 } 695 break; 696 case AVMEDIA_TYPE_AUDIO: 697 if (sti->avctx_inited) { 698 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size); 699 sample_rate = sti->avctx->sample_rate; 700 } else { 701 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size); 702 sample_rate = st->codecpar->sample_rate; 703 } 704 if (frame_size <= 0 || sample_rate <= 0) 705 break; 706 *pnum = frame_size; 707 *pden = sample_rate; 708 break; 709 default: 710 break; 711 } 712} 713 714static int has_decode_delay_been_guessed(AVStream *st) 715{ 716 FFStream *const sti = ffstream(st); 717 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1; 718 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy 719 return 1; 720#if CONFIG_H264_DECODER 721 if (sti->avctx->has_b_frames && 722 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames) 723 return 1; 724#endif 725 if (sti->avctx->has_b_frames < 3) 726 return sti->nb_decoded_frames >= 7; 727 else if (sti->avctx->has_b_frames < 4) 728 return sti->nb_decoded_frames >= 18; 729 else 730 return sti->nb_decoded_frames >= 20; 731} 732 733static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st, 734 PacketListEntry *pktl) 735{ 736 FFFormatContext *const si = ffformatcontext(s); 737 if (pktl->next) 738 return pktl->next; 739 if (pktl == si->packet_buffer.tail) 740 return si->parse_queue.head; 741 return NULL; 742} 743 744static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) 745{ 746 FFStream *const sti = ffstream(st); 747#ifdef OHOS_OPT_COMPAT 748 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && 749 st->codecpar->codec_id != AV_CODEC_ID_HEVC && 750 st->codecpar->codec_id != AV_CODEC_ID_VVC; 751#else 752 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && 753 st->codecpar->codec_id != AV_CODEC_ID_HEVC; 754#endif 755 if (!onein_oneout) { 756 int delay = sti->avctx->has_b_frames; 757 758 if (dts == AV_NOPTS_VALUE) { 759 int64_t best_score = INT64_MAX; 760 for (int i = 0; i < delay; i++) { 761 if (sti->pts_reorder_error_count[i]) { 762 int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i]; 763 if (score < best_score) { 764 best_score = score; 765 dts = pts_buffer[i]; 766 } 767 } 768 } 769 } else { 770 for (int i = 0; i < delay; i++) { 771 if (pts_buffer[i] != AV_NOPTS_VALUE) { 772 int64_t diff = FFABS(pts_buffer[i] - dts) 773 + (uint64_t)sti->pts_reorder_error[i]; 774 diff = FFMAX(diff, sti->pts_reorder_error[i]); 775 sti->pts_reorder_error[i] = diff; 776 sti->pts_reorder_error_count[i]++; 777 if (sti->pts_reorder_error_count[i] > 250) { 778 sti->pts_reorder_error[i] >>= 1; 779 sti->pts_reorder_error_count[i] >>= 1; 780 } 781 } 782 } 783 } 784 } 785 786 if (dts == AV_NOPTS_VALUE) 787 dts = pts_buffer[0]; 788 789 return dts; 790} 791 792/** 793 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts 794 * of the packets in a window. 795 */ 796static void update_dts_from_pts(AVFormatContext *s, int stream_index, 797 PacketListEntry *pkt_buffer) 798{ 799 AVStream *const st = s->streams[stream_index]; 800 int delay = ffstream(st)->avctx->has_b_frames; 801 802 int64_t pts_buffer[MAX_REORDER_DELAY+1]; 803 804 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++) 805 pts_buffer[i] = AV_NOPTS_VALUE; 806 807 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) { 808 if (pkt_buffer->pkt.stream_index != stream_index) 809 continue; 810 811 if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) { 812 pts_buffer[0] = pkt_buffer->pkt.pts; 813 for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++) 814 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]); 815 816 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts); 817 } 818 } 819} 820 821static void update_initial_timestamps(AVFormatContext *s, int stream_index, 822 int64_t dts, int64_t pts, AVPacket *pkt) 823{ 824 FFFormatContext *const si = ffformatcontext(s); 825 AVStream *const st = s->streams[stream_index]; 826 FFStream *const sti = ffstream(st); 827 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head; 828 829 uint64_t shift; 830 831 if (sti->first_dts != AV_NOPTS_VALUE || 832 dts == AV_NOPTS_VALUE || 833 sti->cur_dts == AV_NOPTS_VALUE || 834 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE || 835 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) || 836 is_relative(dts)) 837 return; 838 839 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE); 840 sti->cur_dts = dts; 841 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE; 842 843 if (is_relative(pts)) 844 pts += shift; 845 846 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) { 847 if (pktl_it->pkt.stream_index != stream_index) 848 continue; 849 if (is_relative(pktl_it->pkt.pts)) 850 pktl_it->pkt.pts += shift; 851 852 if (is_relative(pktl_it->pkt.dts)) 853 pktl_it->pkt.dts += shift; 854 855 if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) { 856 st->start_time = pktl_it->pkt.pts; 857 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) 858 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base)); 859 } 860 } 861 862 if (has_decode_delay_been_guessed(st)) 863 update_dts_from_pts(s, stream_index, pktl); 864 865 if (st->start_time == AV_NOPTS_VALUE) { 866 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) { 867 st->start_time = pts; 868 } 869 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) 870 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base)); 871 } 872} 873 874static void update_initial_durations(AVFormatContext *s, AVStream *st, 875 int stream_index, int64_t duration) 876{ 877 FFFormatContext *const si = ffformatcontext(s); 878 FFStream *const sti = ffstream(st); 879 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head; 880 int64_t cur_dts = RELATIVE_TS_BASE; 881 882 if (sti->first_dts != AV_NOPTS_VALUE) { 883 if (sti->update_initial_durations_done) 884 return; 885 sti->update_initial_durations_done = 1; 886 cur_dts = sti->first_dts; 887 for (; pktl; pktl = get_next_pkt(s, st, pktl)) { 888 if (pktl->pkt.stream_index == stream_index) { 889 if (pktl->pkt.pts != pktl->pkt.dts || 890 pktl->pkt.dts != AV_NOPTS_VALUE || 891 pktl->pkt.duration) 892 break; 893 cur_dts -= duration; 894 } 895 } 896 if (pktl && pktl->pkt.dts != sti->first_dts) { 897 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n", 898 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration); 899 return; 900 } 901 if (!pktl) { 902 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts)); 903 return; 904 } 905 pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head; 906 sti->first_dts = cur_dts; 907 } else if (sti->cur_dts != RELATIVE_TS_BASE) 908 return; 909 910 for (; pktl; pktl = get_next_pkt(s, st, pktl)) { 911 if (pktl->pkt.stream_index != stream_index) 912 continue; 913 if ((pktl->pkt.pts == pktl->pkt.dts || 914 pktl->pkt.pts == AV_NOPTS_VALUE) && 915 (pktl->pkt.dts == AV_NOPTS_VALUE || 916 pktl->pkt.dts == sti->first_dts || 917 pktl->pkt.dts == RELATIVE_TS_BASE) && 918 !pktl->pkt.duration && 919 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration 920 ) { 921 pktl->pkt.dts = cur_dts; 922 if (!sti->avctx->has_b_frames) 923 pktl->pkt.pts = cur_dts; 924 pktl->pkt.duration = duration; 925 } else 926 break; 927 cur_dts = pktl->pkt.dts + pktl->pkt.duration; 928 } 929 if (!pktl) 930 sti->cur_dts = cur_dts; 931} 932 933static void compute_pkt_fields(AVFormatContext *s, AVStream *st, 934 AVCodecParserContext *pc, AVPacket *pkt, 935 int64_t next_dts, int64_t next_pts) 936{ 937 FFFormatContext *const si = ffformatcontext(s); 938 FFStream *const sti = ffstream(st); 939 int num, den, presentation_delayed, delay; 940 int64_t offset; 941 AVRational duration; 942#ifdef OHOS_OPT_COMPAT 943 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && 944 st->codecpar->codec_id != AV_CODEC_ID_HEVC && 945 st->codecpar->codec_id != AV_CODEC_ID_VVC; 946#else 947 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && 948 st->codecpar->codec_id != AV_CODEC_ID_HEVC; 949#endif 950 if (s->flags & AVFMT_FLAG_NOFILLIN) 951 return; 952 953 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) { 954 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) { 955 if (sti->last_dts_for_order_check <= pkt->dts) { 956 sti->dts_ordered++; 957 } else { 958 av_log(s, sti->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING, 959 "DTS %"PRIi64" < %"PRIi64" out of order\n", 960 pkt->dts, 961 sti->last_dts_for_order_check); 962 sti->dts_misordered++; 963 } 964 if (sti->dts_ordered + sti->dts_misordered > 250) { 965 sti->dts_ordered >>= 1; 966 sti->dts_misordered >>= 1; 967 } 968 } 969 970 sti->last_dts_for_order_check = pkt->dts; 971 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts) 972 pkt->dts = AV_NOPTS_VALUE; 973 } 974 975 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE) 976 pkt->dts = AV_NOPTS_VALUE; 977 978 if (pc && pc->pict_type == AV_PICTURE_TYPE_B 979 && !sti->avctx->has_b_frames) 980 //FIXME Set low_delay = 0 when has_b_frames = 1 981 sti->avctx->has_b_frames = 1; 982 983 /* do we have a video B-frame ? */ 984 delay = sti->avctx->has_b_frames; 985 presentation_delayed = 0; 986 987 /* XXX: need has_b_frame, but cannot get it if the codec is 988 * not initialized */ 989 if (delay && 990 pc && pc->pict_type != AV_PICTURE_TYPE_B) 991 presentation_delayed = 1; 992 993 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && 994 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) && 995 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) { 996 if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) { 997 pkt->dts -= 1LL << st->pts_wrap_bits; 998 } else 999 pkt->pts += 1LL << st->pts_wrap_bits; 1000 } 1001 1002 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg). 1003 * We take the conservative approach and discard both. 1004 * Note: If this is misbehaving for an H.264 file, then possibly 1005 * presentation_delayed is not set correctly. */ 1006 if (delay == 1 && pkt->dts == pkt->pts && 1007 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) { 1008 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts); 1009 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") 1010 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism 1011 pkt->dts = AV_NOPTS_VALUE; 1012 } 1013 1014 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base); 1015 if (pkt->duration <= 0) { 1016 compute_frame_duration(s, &num, &den, st, pc, pkt); 1017 if (den && num) { 1018 duration = (AVRational) {num, den}; 1019 pkt->duration = av_rescale_rnd(1, 1020 num * (int64_t) st->time_base.den, 1021 den * (int64_t) st->time_base.num, 1022 AV_ROUND_DOWN); 1023 } 1024 } 1025 1026 if (pkt->duration > 0 && (si->packet_buffer.head || si->parse_queue.head)) 1027 update_initial_durations(s, st, pkt->stream_index, pkt->duration); 1028 1029 /* Correct timestamps with byte offset if demuxers only have timestamps 1030 * on packet boundaries */ 1031 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) { 1032 /* this will estimate bitrate based on this frame's duration and size */ 1033 offset = av_rescale(pc->offset, pkt->duration, pkt->size); 1034 if (pkt->pts != AV_NOPTS_VALUE) 1035 pkt->pts += offset; 1036 if (pkt->dts != AV_NOPTS_VALUE) 1037 pkt->dts += offset; 1038 } 1039 1040 /* This may be redundant, but it should not hurt. */ 1041 if (pkt->dts != AV_NOPTS_VALUE && 1042 pkt->pts != AV_NOPTS_VALUE && 1043 pkt->pts > pkt->dts) 1044 presentation_delayed = 1; 1045 1046 if (s->debug & FF_FDEBUG_TS) 1047 av_log(s, AV_LOG_DEBUG, 1048 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n", 1049 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), 1050 pkt->stream_index, pc, pkt->duration, delay, onein_oneout); 1051 1052 /* Interpolate PTS and DTS if they are not present. We skip H264 1053 * currently because delay and has_b_frames are not reliably set. */ 1054 if ((delay == 0 || (delay == 1 && pc)) && 1055 onein_oneout) { 1056 if (presentation_delayed) { 1057 /* DTS = decompression timestamp */ 1058 /* PTS = presentation timestamp */ 1059 if (pkt->dts == AV_NOPTS_VALUE) 1060 pkt->dts = sti->last_IP_pts; 1061 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt); 1062 if (pkt->dts == AV_NOPTS_VALUE) 1063 pkt->dts = sti->cur_dts; 1064 1065 /* This is tricky: the dts must be incremented by the duration 1066 * of the frame we are displaying, i.e. the last I- or P-frame. */ 1067 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX) 1068 sti->last_IP_duration = pkt->duration; 1069 if (pkt->dts != AV_NOPTS_VALUE) 1070 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration); 1071 if (pkt->dts != AV_NOPTS_VALUE && 1072 pkt->pts == AV_NOPTS_VALUE && 1073 sti->last_IP_duration > 0 && 1074 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 && 1075 next_dts != next_pts && 1076 next_pts != AV_NOPTS_VALUE) 1077 pkt->pts = next_dts; 1078 1079 if ((uint64_t)pkt->duration <= INT32_MAX) 1080 sti->last_IP_duration = pkt->duration; 1081 sti->last_IP_pts = pkt->pts; 1082 /* Cannot compute PTS if not present (we can compute it only 1083 * by knowing the future. */ 1084 } else if (pkt->pts != AV_NOPTS_VALUE || 1085 pkt->dts != AV_NOPTS_VALUE || 1086 pkt->duration > 0 ) { 1087 1088 /* presentation is not delayed : PTS and DTS are the same */ 1089 if (pkt->pts == AV_NOPTS_VALUE) 1090 pkt->pts = pkt->dts; 1091 update_initial_timestamps(s, pkt->stream_index, pkt->pts, 1092 pkt->pts, pkt); 1093 if (pkt->pts == AV_NOPTS_VALUE) 1094 pkt->pts = sti->cur_dts; 1095 pkt->dts = pkt->pts; 1096 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0) 1097 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1); 1098 } 1099 } 1100 1101 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) { 1102 sti->pts_buffer[0] = pkt->pts; 1103 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++) 1104 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]); 1105 1106 if (has_decode_delay_been_guessed(st)) 1107 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts); 1108 } 1109 // We skipped it above so we try here. 1110 if (!onein_oneout) 1111 // This should happen on the first packet 1112 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt); 1113 if (pkt->dts > sti->cur_dts) 1114 sti->cur_dts = pkt->dts; 1115 1116 if (s->debug & FF_FDEBUG_TS) 1117 av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n", 1118 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id); 1119 1120 /* update flags */ 1121 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id)) 1122 pkt->flags |= AV_PKT_FLAG_KEY; 1123} 1124 1125/** 1126 * Parse a packet, add all split parts to parse_queue. 1127 * 1128 * @param pkt Packet to parse; must not be NULL. 1129 * @param flush Indicates whether to flush. If set, pkt must be blank. 1130 */ 1131static int parse_packet(AVFormatContext *s, AVPacket *pkt, 1132 int stream_index, int flush) 1133{ 1134 FFFormatContext *const si = ffformatcontext(s); 1135 AVPacket *out_pkt = si->parse_pkt; 1136 AVStream *st = s->streams[stream_index]; 1137 FFStream *const sti = ffstream(st); 1138 const uint8_t *data = pkt->data; 1139 int size = pkt->size; 1140 int ret = 0, got_output = flush; 1141 1142 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) { 1143 // preserve 0-size sync packets 1144 compute_pkt_fields(s, st, sti->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE); 1145 } 1146 1147 while (size > 0 || (flush && got_output)) { 1148 int64_t next_pts = pkt->pts; 1149 int64_t next_dts = pkt->dts; 1150 int len; 1151 1152 len = av_parser_parse2(sti->parser, sti->avctx, 1153 &out_pkt->data, &out_pkt->size, data, size, 1154 pkt->pts, pkt->dts, pkt->pos); 1155 1156 pkt->pts = pkt->dts = AV_NOPTS_VALUE; 1157 pkt->pos = -1; 1158 /* increment read pointer */ 1159 av_assert1(data || !len); 1160 data = len ? data + len : data; 1161 size -= len; 1162 1163 got_output = !!out_pkt->size; 1164 1165 if (!out_pkt->size) 1166 continue; 1167 1168 if (pkt->buf && out_pkt->data == pkt->data) { 1169 /* reference pkt->buf only when out_pkt->data is guaranteed to point 1170 * to data in it and not in the parser's internal buffer. */ 1171 /* XXX: Ensure this is the case with all parsers when sti->parser->flags 1172 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */ 1173 out_pkt->buf = av_buffer_ref(pkt->buf); 1174 if (!out_pkt->buf) { 1175 ret = AVERROR(ENOMEM); 1176 goto fail; 1177 } 1178 } else { 1179 ret = av_packet_make_refcounted(out_pkt); 1180 if (ret < 0) 1181 goto fail; 1182 } 1183 1184 if (pkt->side_data) { 1185 out_pkt->side_data = pkt->side_data; 1186 out_pkt->side_data_elems = pkt->side_data_elems; 1187 pkt->side_data = NULL; 1188 pkt->side_data_elems = 0; 1189 } 1190 1191 /* set the duration */ 1192 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0; 1193 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { 1194 if (sti->avctx->sample_rate > 0) { 1195 out_pkt->duration = 1196 av_rescale_q_rnd(sti->parser->duration, 1197 (AVRational) { 1, sti->avctx->sample_rate }, 1198 st->time_base, 1199 AV_ROUND_DOWN); 1200 } 1201 } 1202 1203 out_pkt->stream_index = st->index; 1204 out_pkt->pts = sti->parser->pts; 1205 out_pkt->dts = sti->parser->dts; 1206 out_pkt->pos = sti->parser->pos; 1207 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT); 1208 1209 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) 1210 out_pkt->pos = sti->parser->frame_offset; 1211 1212 if (sti->parser->key_frame == 1 || 1213 (sti->parser->key_frame == -1 && 1214 sti->parser->pict_type == AV_PICTURE_TYPE_I)) 1215 out_pkt->flags |= AV_PKT_FLAG_KEY; 1216 1217 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY)) 1218 out_pkt->flags |= AV_PKT_FLAG_KEY; 1219 1220 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts); 1221 1222 ret = avpriv_packet_list_put(&si->parse_queue, 1223 out_pkt, NULL, 0); 1224 if (ret < 0) 1225 goto fail; 1226 } 1227 1228 /* end of the stream => close and free the parser */ 1229 if (flush) { 1230 av_parser_close(sti->parser); 1231 sti->parser = NULL; 1232 } 1233 1234fail: 1235 if (ret < 0) 1236 av_packet_unref(out_pkt); 1237 av_packet_unref(pkt); 1238 return ret; 1239} 1240 1241static int64_t ts_to_samples(AVStream *st, int64_t ts) 1242{ 1243 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den); 1244} 1245 1246static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) 1247{ 1248 FFFormatContext *const si = ffformatcontext(s); 1249 int ret, got_packet = 0; 1250 AVDictionary *metadata = NULL; 1251 1252 while (!got_packet && !si->parse_queue.head) { 1253 AVStream *st; 1254 FFStream *sti; 1255 1256 /* read next packet */ 1257 ret = ff_read_packet(s, pkt); 1258 if (ret < 0) { 1259 if (ret == AVERROR(EAGAIN)) 1260 return ret; 1261 /* flush the parsers */ 1262 for (unsigned i = 0; i < s->nb_streams; i++) { 1263 AVStream *const st = s->streams[i]; 1264 FFStream *const sti = ffstream(st); 1265 if (sti->parser && sti->need_parsing) 1266 parse_packet(s, pkt, st->index, 1); 1267 } 1268 /* all remaining packets are now in parse_queue => 1269 * really terminate parsing */ 1270 break; 1271 } 1272 ret = 0; 1273 st = s->streams[pkt->stream_index]; 1274 sti = ffstream(st); 1275 1276 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS; 1277 1278 /* update context if required */ 1279 if (sti->need_context_update) { 1280 if (avcodec_is_open(sti->avctx)) { 1281 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n"); 1282 avcodec_close(sti->avctx); 1283 sti->info->found_decoder = 0; 1284 } 1285 1286 /* close parser, because it depends on the codec */ 1287 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) { 1288 av_parser_close(sti->parser); 1289 sti->parser = NULL; 1290 } 1291 1292 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); 1293 if (ret < 0) { 1294 av_packet_unref(pkt); 1295 return ret; 1296 } 1297 1298 sti->need_context_update = 0; 1299 } 1300 1301 if (pkt->pts != AV_NOPTS_VALUE && 1302 pkt->dts != AV_NOPTS_VALUE && 1303 pkt->pts < pkt->dts) { 1304 av_log(s, AV_LOG_WARNING, 1305 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n", 1306 pkt->stream_index, 1307 av_ts2str(pkt->pts), 1308 av_ts2str(pkt->dts), 1309 pkt->size); 1310 } 1311 if (s->debug & FF_FDEBUG_TS) 1312 av_log(s, AV_LOG_DEBUG, 1313 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n", 1314 pkt->stream_index, 1315 av_ts2str(pkt->pts), 1316 av_ts2str(pkt->dts), 1317 pkt->size, pkt->duration, pkt->flags); 1318 1319 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) { 1320 sti->parser = av_parser_init(st->codecpar->codec_id); 1321 if (!sti->parser) { 1322 av_log(s, AV_LOG_VERBOSE, "parser not found for codec " 1323 "%s, packets or times may be invalid.\n", 1324 avcodec_get_name(st->codecpar->codec_id)); 1325 /* no parser available: just output the raw packets */ 1326 sti->need_parsing = AVSTREAM_PARSE_NONE; 1327 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) 1328 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; 1329 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE) 1330 sti->parser->flags |= PARSER_FLAG_ONCE; 1331 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) 1332 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS; 1333 } 1334 1335 if (!sti->need_parsing || !sti->parser) { 1336 /* no parsing needed: we just output the packet as is */ 1337 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE); 1338 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && 1339 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) { 1340 ff_reduce_index(s, st->index); 1341 av_add_index_entry(st, pkt->pos, pkt->dts, 1342 0, 0, AVINDEX_KEYFRAME); 1343 } 1344 got_packet = 1; 1345 } else if (st->discard < AVDISCARD_ALL) { 1346 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0) 1347 return ret; 1348 st->codecpar->sample_rate = sti->avctx->sample_rate; 1349 st->codecpar->bit_rate = sti->avctx->bit_rate; 1350#if FF_API_OLD_CHANNEL_LAYOUT 1351FF_DISABLE_DEPRECATION_WARNINGS 1352 st->codecpar->channels = sti->avctx->ch_layout.nb_channels; 1353 st->codecpar->channel_layout = sti->avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? 1354 sti->avctx->ch_layout.u.mask : 0; 1355FF_ENABLE_DEPRECATION_WARNINGS 1356#endif 1357 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout); 1358 if (ret < 0) 1359 return ret; 1360 st->codecpar->codec_id = sti->avctx->codec_id; 1361 } else { 1362 /* free packet */ 1363 av_packet_unref(pkt); 1364 } 1365 if (pkt->flags & AV_PKT_FLAG_KEY) 1366 sti->skip_to_keyframe = 0; 1367 if (sti->skip_to_keyframe) { 1368 av_packet_unref(pkt); 1369 got_packet = 0; 1370 } 1371 } 1372 1373 if (!got_packet && si->parse_queue.head) 1374 ret = avpriv_packet_list_get(&si->parse_queue, pkt); 1375 1376 if (ret >= 0) { 1377 AVStream *const st = s->streams[pkt->stream_index]; 1378 FFStream *const sti = ffstream(st); 1379 int discard_padding = 0; 1380 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) { 1381 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0); 1382 int64_t sample = ts_to_samples(st, pts); 1383 int64_t duration = ts_to_samples(st, pkt->duration); 1384 int64_t end_sample = sample + duration; 1385 if (duration > 0 && end_sample >= sti->first_discard_sample && 1386 sample < sti->last_discard_sample) 1387 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration); 1388 } 1389 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE)) 1390 sti->skip_samples = sti->start_skip_samples; 1391 sti->skip_samples = FFMAX(0, sti->skip_samples); 1392 if (sti->skip_samples || discard_padding) { 1393 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10); 1394 if (p) { 1395 AV_WL32(p, sti->skip_samples); 1396 AV_WL32(p + 4, discard_padding); 1397 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n", 1398 (unsigned)sti->skip_samples, (unsigned)discard_padding); 1399 } 1400 sti->skip_samples = 0; 1401 } 1402 1403 if (sti->inject_global_side_data) { 1404 for (int i = 0; i < st->nb_side_data; i++) { 1405 const AVPacketSideData *const src_sd = &st->side_data[i]; 1406 uint8_t *dst_data; 1407 1408 if (av_packet_get_side_data(pkt, src_sd->type, NULL)) 1409 continue; 1410 1411 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size); 1412 if (!dst_data) { 1413 av_log(s, AV_LOG_WARNING, "Could not inject global side data\n"); 1414 continue; 1415 } 1416 1417 memcpy(dst_data, src_sd->data, src_sd->size); 1418 } 1419 sti->inject_global_side_data = 0; 1420 } 1421 } 1422 1423 if (!si->metafree) { 1424 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata); 1425 if (metadata) { 1426 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; 1427 av_dict_copy(&s->metadata, metadata, 0); 1428 av_dict_free(&metadata); 1429 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN); 1430 } 1431 si->metafree = metaret == AVERROR_OPTION_NOT_FOUND; 1432 } 1433 1434 if (s->debug & FF_FDEBUG_TS) 1435 av_log(s, AV_LOG_DEBUG, 1436 "read_frame_internal stream=%d, pts=%s, dts=%s, " 1437 "size=%d, duration=%"PRId64", flags=%d\n", 1438 pkt->stream_index, 1439 av_ts2str(pkt->pts), 1440 av_ts2str(pkt->dts), 1441 pkt->size, pkt->duration, pkt->flags); 1442 1443 /* A demuxer might have returned EOF because of an IO error, let's 1444 * propagate this back to the user. */ 1445 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN)) 1446 ret = s->pb->error; 1447 1448 return ret; 1449} 1450 1451int av_read_frame(AVFormatContext *s, AVPacket *pkt) 1452{ 1453 FFFormatContext *const si = ffformatcontext(s); 1454 const int genpts = s->flags & AVFMT_FLAG_GENPTS; 1455 int eof = 0; 1456 int ret; 1457 AVStream *st; 1458 1459 if (!genpts) { 1460 ret = si->packet_buffer.head 1461 ? avpriv_packet_list_get(&si->packet_buffer, pkt) 1462 : read_frame_internal(s, pkt); 1463 if (ret < 0) 1464 return ret; 1465 goto return_packet; 1466 } 1467 1468 for (;;) { 1469 PacketListEntry *pktl = si->packet_buffer.head; 1470 1471 if (pktl) { 1472 AVPacket *next_pkt = &pktl->pkt; 1473 1474 if (next_pkt->dts != AV_NOPTS_VALUE) { 1475 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits; 1476 // last dts seen for this stream. if any of packets following 1477 // current one had no dts, we will set this to AV_NOPTS_VALUE. 1478 int64_t last_dts = next_pkt->dts; 1479 av_assert2(wrap_bits <= 64); 1480 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) { 1481 if (pktl->pkt.stream_index == next_pkt->stream_index && 1482 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) { 1483 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) { 1484 // not B-frame 1485 next_pkt->pts = pktl->pkt.dts; 1486 } 1487 if (last_dts != AV_NOPTS_VALUE) { 1488 // Once last dts was set to AV_NOPTS_VALUE, we don't change it. 1489 last_dts = pktl->pkt.dts; 1490 } 1491 } 1492 pktl = pktl->next; 1493 } 1494 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) { 1495 // Fixing the last reference frame had none pts issue (For MXF etc). 1496 // We only do this when 1497 // 1. eof. 1498 // 2. we are not able to resolve a pts value for current packet. 1499 // 3. the packets for this stream at the end of the files had valid dts. 1500 next_pkt->pts = last_dts + next_pkt->duration; 1501 } 1502 pktl = si->packet_buffer.head; 1503 } 1504 1505 /* read packet from packet buffer, if there is data */ 1506 st = s->streams[next_pkt->stream_index]; 1507 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL && 1508 next_pkt->dts != AV_NOPTS_VALUE && !eof)) { 1509 ret = avpriv_packet_list_get(&si->packet_buffer, pkt); 1510 goto return_packet; 1511 } 1512 } 1513 1514 ret = read_frame_internal(s, pkt); 1515 if (ret < 0) { 1516 if (pktl && ret != AVERROR(EAGAIN)) { 1517 eof = 1; 1518 continue; 1519 } else 1520 return ret; 1521 } 1522 1523 ret = avpriv_packet_list_put(&si->packet_buffer, 1524 pkt, NULL, 0); 1525 if (ret < 0) { 1526 av_packet_unref(pkt); 1527 return ret; 1528 } 1529 } 1530 1531return_packet: 1532 st = s->streams[pkt->stream_index]; 1533 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) { 1534 ff_reduce_index(s, st->index); 1535 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME); 1536 } 1537 1538 if (is_relative(pkt->dts)) 1539 pkt->dts -= RELATIVE_TS_BASE; 1540 if (is_relative(pkt->pts)) 1541 pkt->pts -= RELATIVE_TS_BASE; 1542 1543 return ret; 1544} 1545 1546/** 1547 * Return TRUE if the stream has accurate duration in any stream. 1548 * 1549 * @return TRUE if the stream has accurate duration for at least one component. 1550 */ 1551static int has_duration(AVFormatContext *ic) 1552{ 1553 for (unsigned i = 0; i < ic->nb_streams; i++) { 1554 const AVStream *const st = ic->streams[i]; 1555 if (st->duration != AV_NOPTS_VALUE) 1556 return 1; 1557 } 1558 if (ic->duration != AV_NOPTS_VALUE) 1559 return 1; 1560 return 0; 1561} 1562 1563/** 1564 * Estimate the stream timings from the one of each components. 1565 * 1566 * Also computes the global bitrate if possible. 1567 */ 1568static void update_stream_timings(AVFormatContext *ic) 1569{ 1570 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text; 1571 int64_t duration, duration1, duration_text, filesize; 1572 1573 start_time = INT64_MAX; 1574 start_time_text = INT64_MAX; 1575 end_time = INT64_MIN; 1576 end_time_text = INT64_MIN; 1577 duration = INT64_MIN; 1578 duration_text = INT64_MIN; 1579 1580 for (unsigned i = 0; i < ic->nb_streams; i++) { 1581 AVStream *const st = ic->streams[i]; 1582 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || 1583 st->codecpar->codec_type == AVMEDIA_TYPE_DATA; 1584 1585 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) { 1586 start_time1 = av_rescale_q(st->start_time, st->time_base, 1587 AV_TIME_BASE_Q); 1588 if (is_text) 1589 start_time_text = FFMIN(start_time_text, start_time1); 1590 else 1591 start_time = FFMIN(start_time, start_time1); 1592 end_time1 = av_rescale_q_rnd(st->duration, st->time_base, 1593 AV_TIME_BASE_Q, 1594 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); 1595 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) { 1596 end_time1 += start_time1; 1597 if (is_text) 1598 end_time_text = FFMAX(end_time_text, end_time1); 1599 else 1600 end_time = FFMAX(end_time, end_time1); 1601 } 1602 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) { 1603 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1) 1604 p->start_time = start_time1; 1605 if (p->end_time < end_time1) 1606 p->end_time = end_time1; 1607 } 1608 } 1609 if (st->duration != AV_NOPTS_VALUE) { 1610 duration1 = av_rescale_q(st->duration, st->time_base, 1611 AV_TIME_BASE_Q); 1612 if (is_text) 1613 duration_text = FFMAX(duration_text, duration1); 1614 else 1615 duration = FFMAX(duration, duration1); 1616 } 1617 } 1618 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE)) 1619 start_time = start_time_text; 1620 else if (start_time > start_time_text) 1621 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE); 1622 1623 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE)) 1624 end_time = end_time_text; 1625 else if (end_time < end_time_text) 1626 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE); 1627 1628 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE)) 1629 duration = duration_text; 1630 else if (duration < duration_text) 1631 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE); 1632 1633 if (start_time != INT64_MAX) { 1634 ic->start_time = start_time; 1635 if (end_time != INT64_MIN) { 1636 if (ic->nb_programs > 1) { 1637 for (unsigned i = 0; i < ic->nb_programs; i++) { 1638 AVProgram *const p = ic->programs[i]; 1639 1640 if (p->start_time != AV_NOPTS_VALUE && 1641 p->end_time > p->start_time && 1642 p->end_time - (uint64_t)p->start_time <= INT64_MAX) 1643 duration = FFMAX(duration, p->end_time - p->start_time); 1644 } 1645 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) { 1646 duration = FFMAX(duration, end_time - start_time); 1647 } 1648 } 1649 } 1650 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) { 1651 ic->duration = duration; 1652 } 1653 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) { 1654 /* compute the bitrate */ 1655 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE / 1656 (double) ic->duration; 1657 if (bitrate >= 0 && bitrate <= INT64_MAX) 1658 ic->bit_rate = bitrate; 1659 } 1660} 1661 1662static void fill_all_stream_timings(AVFormatContext *ic) 1663{ 1664 update_stream_timings(ic); 1665 for (unsigned i = 0; i < ic->nb_streams; i++) { 1666 AVStream *const st = ic->streams[i]; 1667 1668 if (st->start_time == AV_NOPTS_VALUE) { 1669 if (ic->start_time != AV_NOPTS_VALUE) 1670 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, 1671 st->time_base); 1672 if (ic->duration != AV_NOPTS_VALUE) 1673 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, 1674 st->time_base); 1675 } 1676 } 1677} 1678 1679static void estimate_timings_from_bit_rate(AVFormatContext *ic) 1680{ 1681 FFFormatContext *const si = ffformatcontext(ic); 1682 int show_warning = 0; 1683 1684 /* if bit_rate is already set, we believe it */ 1685 if (ic->bit_rate <= 0) { 1686 int64_t bit_rate = 0; 1687 for (unsigned i = 0; i < ic->nb_streams; i++) { 1688 const AVStream *const st = ic->streams[i]; 1689 const FFStream *const sti = cffstream(st); 1690 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0) 1691 st->codecpar->bit_rate = sti->avctx->bit_rate; 1692 if (st->codecpar->bit_rate > 0) { 1693 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) { 1694 bit_rate = 0; 1695 break; 1696 } 1697 bit_rate += st->codecpar->bit_rate; 1698 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) { 1699 // If we have a videostream with packets but without a bitrate 1700 // then consider the sum not known 1701 bit_rate = 0; 1702 break; 1703 } 1704 } 1705 ic->bit_rate = bit_rate; 1706 } 1707 1708 /* if duration is already set, we believe it */ 1709 if (ic->duration == AV_NOPTS_VALUE && 1710 ic->bit_rate != 0) { 1711 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0; 1712 if (filesize > si->data_offset) { 1713 filesize -= si->data_offset; 1714 for (unsigned i = 0; i < ic->nb_streams; i++) { 1715 AVStream *const st = ic->streams[i]; 1716 1717 if ( st->time_base.num <= INT64_MAX / ic->bit_rate 1718 && st->duration == AV_NOPTS_VALUE) { 1719 st->duration = av_rescale(filesize, 8LL * st->time_base.den, 1720 ic->bit_rate * 1721 (int64_t) st->time_base.num); 1722 show_warning = 1; 1723 } 1724 } 1725 } 1726 } 1727 if (show_warning) 1728 av_log(ic, AV_LOG_WARNING, 1729 "Estimating duration from bitrate, this may be inaccurate\n"); 1730} 1731 1732#define DURATION_MAX_READ_SIZE 250000LL 1733#define DURATION_MAX_RETRY 6 1734 1735/* only usable for MPEG-PS streams */ 1736static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) 1737{ 1738 FFFormatContext *const si = ffformatcontext(ic); 1739 AVPacket *const pkt = si->pkt; 1740 int num, den, read_size, ret; 1741 int found_duration = 0; 1742 int is_end; 1743 int64_t filesize, offset, duration; 1744 int retry = 0; 1745 1746 /* flush packet queue */ 1747 ff_flush_packet_queue(ic); 1748 1749 for (unsigned i = 0; i < ic->nb_streams; i++) { 1750 AVStream *const st = ic->streams[i]; 1751 FFStream *const sti = ffstream(st); 1752 1753 if (st->start_time == AV_NOPTS_VALUE && 1754 sti->first_dts == AV_NOPTS_VALUE && 1755 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN) 1756 av_log(ic, AV_LOG_WARNING, 1757 "start time for stream %d is not set in estimate_timings_from_pts\n", i); 1758 1759 if (sti->parser) { 1760 av_parser_close(sti->parser); 1761 sti->parser = NULL; 1762 } 1763 } 1764 1765 if (ic->skip_estimate_duration_from_pts) { 1766 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n"); 1767 goto skip_duration_calc; 1768 } 1769 1770 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN); 1771 /* estimate the end time (duration) */ 1772 /* XXX: may need to support wrapping */ 1773 filesize = ic->pb ? avio_size(ic->pb) : 0; 1774 do { 1775 is_end = found_duration; 1776 offset = filesize - (DURATION_MAX_READ_SIZE << retry); 1777 if (offset < 0) 1778 offset = 0; 1779 1780 avio_seek(ic->pb, offset, SEEK_SET); 1781 read_size = 0; 1782 for (;;) { 1783 AVStream *st; 1784 FFStream *sti; 1785 if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0))) 1786 break; 1787 1788 do { 1789 ret = ff_read_packet(ic, pkt); 1790 } while (ret == AVERROR(EAGAIN)); 1791 if (ret != 0) 1792 break; 1793 read_size += pkt->size; 1794 st = ic->streams[pkt->stream_index]; 1795 sti = ffstream(st); 1796 if (pkt->pts != AV_NOPTS_VALUE && 1797 (st->start_time != AV_NOPTS_VALUE || 1798 sti->first_dts != AV_NOPTS_VALUE)) { 1799 if (pkt->duration == 0) { 1800 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt); 1801 if (den && num) { 1802 pkt->duration = av_rescale_rnd(1, 1803 num * (int64_t) st->time_base.den, 1804 den * (int64_t) st->time_base.num, 1805 AV_ROUND_DOWN); 1806 } 1807 } 1808 duration = pkt->pts + pkt->duration; 1809 found_duration = 1; 1810 if (st->start_time != AV_NOPTS_VALUE) 1811 duration -= st->start_time; 1812 else 1813 duration -= sti->first_dts; 1814 if (duration > 0) { 1815 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 || 1816 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num)) 1817 st->duration = duration; 1818 sti->info->last_duration = duration; 1819 } 1820 } 1821 av_packet_unref(pkt); 1822 } 1823 1824 /* check if all audio/video streams have valid duration */ 1825 if (!is_end) { 1826 is_end = 1; 1827 for (unsigned i = 0; i < ic->nb_streams; i++) { 1828 const AVStream *const st = ic->streams[i]; 1829 switch (st->codecpar->codec_type) { 1830 case AVMEDIA_TYPE_VIDEO: 1831 case AVMEDIA_TYPE_AUDIO: 1832 if (st->duration == AV_NOPTS_VALUE) 1833 is_end = 0; 1834 } 1835 } 1836 } 1837 } while (!is_end && 1838 offset && 1839 ++retry <= DURATION_MAX_RETRY); 1840 1841 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN); 1842 1843 /* warn about audio/video streams which duration could not be estimated */ 1844 for (unsigned i = 0; i < ic->nb_streams; i++) { 1845 const AVStream *const st = ic->streams[i]; 1846 const FFStream *const sti = cffstream(st); 1847 1848 if (st->duration == AV_NOPTS_VALUE) { 1849 switch (st->codecpar->codec_type) { 1850 case AVMEDIA_TYPE_VIDEO: 1851 case AVMEDIA_TYPE_AUDIO: 1852 if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) { 1853 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i); 1854 } else 1855 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i); 1856 } 1857 } 1858 } 1859skip_duration_calc: 1860 fill_all_stream_timings(ic); 1861 1862 avio_seek(ic->pb, old_offset, SEEK_SET); 1863 for (unsigned i = 0; i < ic->nb_streams; i++) { 1864 AVStream *const st = ic->streams[i]; 1865 FFStream *const sti = ffstream(st); 1866 1867 sti->cur_dts = sti->first_dts; 1868 sti->last_IP_pts = AV_NOPTS_VALUE; 1869 sti->last_dts_for_order_check = AV_NOPTS_VALUE; 1870 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++) 1871 sti->pts_buffer[j] = AV_NOPTS_VALUE; 1872 } 1873} 1874 1875/* 1:1 map to AVDurationEstimationMethod */ 1876static const char *const duration_name[] = { 1877 [AVFMT_DURATION_FROM_PTS] = "pts", 1878 [AVFMT_DURATION_FROM_STREAM] = "stream", 1879 [AVFMT_DURATION_FROM_BITRATE] = "bit rate", 1880}; 1881 1882static const char *duration_estimate_name(enum AVDurationEstimationMethod method) 1883{ 1884 return duration_name[method]; 1885} 1886 1887static void estimate_timings(AVFormatContext *ic, int64_t old_offset) 1888{ 1889 int64_t file_size; 1890 1891 /* get the file size, if possible */ 1892 if (ic->iformat->flags & AVFMT_NOFILE) { 1893 file_size = 0; 1894 } else { 1895 file_size = avio_size(ic->pb); 1896 file_size = FFMAX(0, file_size); 1897 } 1898 1899 if ((!strcmp(ic->iformat->name, "mpeg") || 1900 !strcmp(ic->iformat->name, "mpegts")) && 1901 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) { 1902 /* get accurate estimate from the PTSes */ 1903 estimate_timings_from_pts(ic, old_offset); 1904 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS; 1905 } else if (has_duration(ic)) { 1906 /* at least one component has timings - we use them for all 1907 * the components */ 1908 fill_all_stream_timings(ic); 1909 /* nut demuxer estimate the duration from PTS */ 1910 if (!strcmp(ic->iformat->name, "nut")) 1911 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS; 1912 else 1913 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM; 1914 } else { 1915 /* less precise: use bitrate info */ 1916 estimate_timings_from_bit_rate(ic); 1917 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE; 1918 } 1919 update_stream_timings(ic); 1920 1921 for (unsigned i = 0; i < ic->nb_streams; i++) { 1922 AVStream *const st = ic->streams[i]; 1923 if (st->time_base.den) 1924 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i, 1925 av_ts2timestr(st->start_time, &st->time_base), 1926 av_ts2timestr(st->duration, &st->time_base)); 1927 } 1928 av_log(ic, AV_LOG_TRACE, 1929 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n", 1930 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q), 1931 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q), 1932 duration_estimate_name(ic->duration_estimation_method), 1933 (int64_t)ic->bit_rate / 1000); 1934} 1935 1936static int determinable_frame_size(const AVCodecContext *avctx) 1937{ 1938 switch(avctx->codec_id) { 1939 case AV_CODEC_ID_MP1: 1940 case AV_CODEC_ID_MP2: 1941 case AV_CODEC_ID_MP3: 1942 case AV_CODEC_ID_CODEC2: 1943 return 1; 1944 } 1945 1946 return 0; 1947} 1948 1949static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr) 1950{ 1951 const FFStream *const sti = cffstream(st); 1952 const AVCodecContext *const avctx = sti->avctx; 1953 1954#define FAIL(errmsg) do { \ 1955 if (errmsg_ptr) \ 1956 *errmsg_ptr = errmsg; \ 1957 return 0; \ 1958 } while (0) 1959 1960 if ( avctx->codec_id == AV_CODEC_ID_NONE 1961 && avctx->codec_type != AVMEDIA_TYPE_DATA) 1962 FAIL("unknown codec"); 1963 switch (avctx->codec_type) { 1964 case AVMEDIA_TYPE_AUDIO: 1965 if (!avctx->frame_size && determinable_frame_size(avctx)) 1966 FAIL("unspecified frame size"); 1967 if (sti->info->found_decoder >= 0 && 1968 avctx->sample_fmt == AV_SAMPLE_FMT_NONE) 1969 FAIL("unspecified sample format"); 1970 if (!avctx->sample_rate) 1971 FAIL("unspecified sample rate"); 1972 if (!avctx->ch_layout.nb_channels) 1973 FAIL("unspecified number of channels"); 1974 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS) 1975 FAIL("no decodable DTS frames"); 1976 break; 1977 case AVMEDIA_TYPE_VIDEO: 1978 if (!avctx->width) 1979#ifdef OHOS_OPTIMIZE_DELAY 1980 if (!(st->codecpar->codec_id == AV_CODEC_ID_HEVC) && 1981 !(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) 1982#endif 1983 FAIL("unspecified size"); 1984 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE) 1985 FAIL("unspecified pixel format"); 1986 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40) 1987 if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !sti->codec_info_nb_frames) 1988 FAIL("no frame in rv30/40 and no sar"); 1989 break; 1990 case AVMEDIA_TYPE_SUBTITLE: 1991 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width) 1992 FAIL("unspecified size"); 1993 break; 1994 case AVMEDIA_TYPE_DATA: 1995 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1; 1996 } 1997 1998 return 1; 1999} 2000 2001/* returns 1 or 0 if or if not decoded data was returned, or a negative error */ 2002static int try_decode_frame(AVFormatContext *s, AVStream *st, 2003 const AVPacket *avpkt, AVDictionary **options) 2004{ 2005 FFStream *const sti = ffstream(st); 2006 AVCodecContext *const avctx = sti->avctx; 2007 const AVCodec *codec; 2008 int got_picture = 1, ret = 0; 2009 AVFrame *frame = av_frame_alloc(); 2010 AVSubtitle subtitle; 2011 AVPacket pkt = *avpkt; 2012 int do_skip_frame = 0; 2013 enum AVDiscard skip_frame; 2014 2015 if (!frame) 2016 return AVERROR(ENOMEM); 2017 2018 if (!avcodec_is_open(avctx) && 2019 sti->info->found_decoder <= 0 && 2020 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) { 2021 AVDictionary *thread_opt = NULL; 2022 2023 codec = find_probe_decoder(s, st, st->codecpar->codec_id); 2024 2025 if (!codec) { 2026 sti->info->found_decoder = -st->codecpar->codec_id; 2027 ret = -1; 2028 goto fail; 2029 } 2030 2031 /* Force thread count to 1 since the H.264 decoder will not extract 2032 * SPS and PPS to extradata during multi-threaded decoding. */ 2033 av_dict_set(options ? options : &thread_opt, "threads", "1", 0); 2034 /* Force lowres to 0. The decoder might reduce the video size by the 2035 * lowres factor, and we don't want that propagated to the stream's 2036 * codecpar */ 2037 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0); 2038 if (s->codec_whitelist) 2039 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0); 2040 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt); 2041 if (!options) 2042 av_dict_free(&thread_opt); 2043 if (ret < 0) { 2044 sti->info->found_decoder = -avctx->codec_id; 2045 goto fail; 2046 } 2047 sti->info->found_decoder = 1; 2048 } else if (!sti->info->found_decoder) 2049 sti->info->found_decoder = 1; 2050 2051 if (sti->info->found_decoder < 0) { 2052 ret = -1; 2053 goto fail; 2054 } 2055 2056 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) { 2057 do_skip_frame = 1; 2058 skip_frame = avctx->skip_frame; 2059 avctx->skip_frame = AVDISCARD_ALL; 2060 } 2061 2062 while ((pkt.size > 0 || (!pkt.data && got_picture)) && 2063 ret >= 0 && 2064 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) || 2065 (!sti->codec_info_nb_frames && 2066 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) { 2067 got_picture = 0; 2068 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || 2069 avctx->codec_type == AVMEDIA_TYPE_AUDIO) { 2070 ret = avcodec_send_packet(avctx, &pkt); 2071 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) 2072 break; 2073 if (ret >= 0) 2074 pkt.size = 0; 2075 ret = avcodec_receive_frame(avctx, frame); 2076 if (ret >= 0) 2077 got_picture = 1; 2078 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) 2079 ret = 0; 2080 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) { 2081 ret = avcodec_decode_subtitle2(avctx, &subtitle, 2082 &got_picture, &pkt); 2083 if (got_picture) 2084 avsubtitle_free(&subtitle); 2085 if (ret >= 0) 2086 pkt.size = 0; 2087 } 2088 if (ret >= 0) { 2089 if (got_picture) 2090 sti->nb_decoded_frames++; 2091 ret = got_picture; 2092 } 2093 } 2094 2095fail: 2096 if (do_skip_frame) { 2097 avctx->skip_frame = skip_frame; 2098 } 2099 2100 av_frame_free(&frame); 2101 return ret; 2102} 2103 2104static int chapter_start_cmp(const void *p1, const void *p2) 2105{ 2106 const AVChapter *const ch1 = *(AVChapter**)p1; 2107 const AVChapter *const ch2 = *(AVChapter**)p2; 2108 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base); 2109 if (delta) 2110 return delta; 2111 return FFDIFFSIGN(ch1->id, ch2->id); 2112} 2113 2114static int compute_chapters_end(AVFormatContext *s) 2115{ 2116 int64_t max_time = 0; 2117 AVChapter **timetable; 2118 2119 if (!s->nb_chapters) 2120 return 0; 2121 2122 if (s->duration > 0 && s->start_time < INT64_MAX - s->duration) 2123 max_time = s->duration + 2124 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time); 2125 2126 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable)); 2127 if (!timetable) 2128 return AVERROR(ENOMEM); 2129 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp); 2130 2131 for (unsigned i = 0; i < s->nb_chapters; i++) 2132 if (timetable[i]->end == AV_NOPTS_VALUE) { 2133 AVChapter *const ch = timetable[i]; 2134 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, 2135 ch->time_base) 2136 : INT64_MAX; 2137 2138 if (i + 1 < s->nb_chapters) { 2139 const AVChapter *const ch1 = timetable[i + 1]; 2140 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, 2141 ch->time_base); 2142 if (next_start > ch->start && next_start < end) 2143 end = next_start; 2144 } 2145 ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end; 2146 } 2147 av_free(timetable); 2148 return 0; 2149} 2150 2151static int get_std_framerate(int i) 2152{ 2153 if (i < 30*12) 2154 return (i + 1) * 1001; 2155 i -= 30*12; 2156 2157 if (i < 30) 2158 return (i + 31) * 1001 * 12; 2159 i -= 30; 2160 2161 if (i < 3) 2162 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12; 2163 2164 i -= 3; 2165 2166 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12; 2167} 2168 2169/* Is the time base unreliable? 2170 * This is a heuristic to balance between quick acceptance of the values in 2171 * the headers vs. some extra checks. 2172 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps. 2173 * MPEG-2 commonly misuses field repeat flags to store different framerates. 2174 * And there are "variable" fps files this needs to detect as well. */ 2175static int tb_unreliable(AVCodecContext *c) 2176{ 2177 if (c->time_base.den >= 101LL * c->time_base.num || 2178 c->time_base.den < 5LL * c->time_base.num || 2179 // c->codec_tag == AV_RL32("DIVX") || 2180 // c->codec_tag == AV_RL32("XVID") || 2181 c->codec_tag == AV_RL32("mp4v") || 2182 c->codec_id == AV_CODEC_ID_MPEG2VIDEO || 2183 c->codec_id == AV_CODEC_ID_GIF || 2184 c->codec_id == AV_CODEC_ID_HEVC || 2185 c->codec_id == AV_CODEC_ID_H264) 2186 return 1; 2187 return 0; 2188} 2189 2190int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts) 2191{ 2192 FFStream *const sti = ffstream(st); 2193 FFStreamInfo *info = sti->info; 2194 int64_t last = info->last_dts; 2195 2196 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last 2197 && ts - (uint64_t)last < INT64_MAX) { 2198 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base); 2199 int64_t duration = ts - last; 2200 2201 if (!info->duration_error) 2202 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2); 2203 if (!info->duration_error) 2204 return AVERROR(ENOMEM); 2205 2206// if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) 2207// av_log(NULL, AV_LOG_ERROR, "%f\n", dts); 2208 for (int i = 0; i < MAX_STD_TIMEBASES; i++) { 2209 if (info->duration_error[0][1][i] < 1e10) { 2210 int framerate = get_std_framerate(i); 2211 double sdts = dts*framerate/(1001*12); 2212 for (int j = 0; j < 2; j++) { 2213 int64_t ticks = llrint(sdts+j*0.5); 2214 double error = sdts - ticks + j*0.5; 2215 info->duration_error[j][0][i] += error; 2216 info->duration_error[j][1][i] += error*error; 2217 } 2218 } 2219 } 2220 if (info->rfps_duration_sum <= INT64_MAX - duration) { 2221 info->duration_count++; 2222 info->rfps_duration_sum += duration; 2223 } 2224 2225 if (info->duration_count % 10 == 0) { 2226 int n = info->duration_count; 2227 for (int i = 0; i < MAX_STD_TIMEBASES; i++) { 2228 if (info->duration_error[0][1][i] < 1e10) { 2229 double a0 = info->duration_error[0][0][i] / n; 2230 double error0 = info->duration_error[0][1][i] / n - a0*a0; 2231 double a1 = info->duration_error[1][0][i] / n; 2232 double error1 = info->duration_error[1][1][i] / n - a1*a1; 2233 if (error0 > 0.04 && error1 > 0.04) { 2234 info->duration_error[0][1][i] = 2e10; 2235 info->duration_error[1][1][i] = 2e10; 2236 } 2237 } 2238 } 2239 } 2240 2241 // ignore the first 4 values, they might have some random jitter 2242 if (info->duration_count > 3 && is_relative(ts) == is_relative(last)) 2243 info->duration_gcd = av_gcd(info->duration_gcd, duration); 2244 } 2245 if (ts != AV_NOPTS_VALUE) 2246 info->last_dts = ts; 2247 2248 return 0; 2249} 2250 2251void ff_rfps_calculate(AVFormatContext *ic) 2252{ 2253 for (unsigned i = 0; i < ic->nb_streams; i++) { 2254 AVStream *const st = ic->streams[i]; 2255 FFStream *const sti = ffstream(st); 2256 2257 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) 2258 continue; 2259 // the check for tb_unreliable() is not completely correct, since this is not about handling 2260 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g. 2261 // ipmovie.c produces. 2262 if (tb_unreliable(sti->avctx) && sti->info->duration_count > 15 && sti->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num && 2263 sti->info->duration_gcd < INT64_MAX / st->time_base.num) 2264 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX); 2265 if (sti->info->duration_count > 1 && !st->r_frame_rate.num 2266 && tb_unreliable(sti->avctx)) { 2267 int num = 0; 2268 double best_error = 0.01; 2269 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base); 2270 2271 for (int j = 0; j < MAX_STD_TIMEBASES; j++) { 2272 if (sti->info->codec_info_duration && 2273 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j)) 2274 continue; 2275 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12) 2276 continue; 2277 2278 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j)) 2279 continue; 2280 2281 for (int k = 0; k < 2; k++) { 2282 int n = sti->info->duration_count; 2283 double a = sti->info->duration_error[k][0][j] / n; 2284 double error = sti->info->duration_error[k][1][j]/n - a*a; 2285 2286 if (error < best_error && best_error> 0.000000001) { 2287 best_error= error; 2288 num = get_std_framerate(j); 2289 } 2290 if (error < 0.02) 2291 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error); 2292 } 2293 } 2294 // do not increase frame rate by more than 1 % in order to match a standard rate. 2295 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate))) 2296 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX); 2297 } 2298 if ( !st->avg_frame_rate.num 2299 && st->r_frame_rate.num && sti->info->rfps_duration_sum 2300 && sti->info->codec_info_duration <= 0 2301 && sti->info->duration_count > 2 2302 && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - sti->info->rfps_duration_sum / (double)sti->info->duration_count) <= 1.0 2303 ) { 2304 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n"); 2305 st->avg_frame_rate = st->r_frame_rate; 2306 } 2307 2308 av_freep(&sti->info->duration_error); 2309 sti->info->last_dts = AV_NOPTS_VALUE; 2310 sti->info->duration_count = 0; 2311 sti->info->rfps_duration_sum = 0; 2312 } 2313} 2314 2315static int extract_extradata_check(AVStream *st) 2316{ 2317 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata"); 2318 if (!f) 2319 return 0; 2320 2321 if (f->codec_ids) { 2322 const enum AVCodecID *ids; 2323 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++) 2324 if (*ids == st->codecpar->codec_id) 2325 return 1; 2326 } 2327 2328 return 0; 2329} 2330 2331static int extract_extradata_init(AVStream *st) 2332{ 2333 FFStream *const sti = ffstream(st); 2334 const AVBitStreamFilter *f; 2335 int ret; 2336 2337 f = av_bsf_get_by_name("extract_extradata"); 2338 if (!f) 2339 goto finish; 2340 2341 /* check that the codec id is supported */ 2342 ret = extract_extradata_check(st); 2343 if (!ret) 2344 goto finish; 2345 2346 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf); 2347 if (ret < 0) 2348 return ret; 2349 2350 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in, 2351 st->codecpar); 2352 if (ret < 0) 2353 goto fail; 2354 2355 sti->extract_extradata.bsf->time_base_in = st->time_base; 2356 2357 ret = av_bsf_init(sti->extract_extradata.bsf); 2358 if (ret < 0) 2359 goto fail; 2360 2361finish: 2362 sti->extract_extradata.inited = 1; 2363 2364 return 0; 2365fail: 2366 av_bsf_free(&sti->extract_extradata.bsf); 2367 return ret; 2368} 2369 2370static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt) 2371{ 2372 FFStream *const sti = ffstream(st); 2373 AVPacket *const pkt_ref = si->parse_pkt; 2374 int ret; 2375 2376 if (!sti->extract_extradata.inited) { 2377 ret = extract_extradata_init(st); 2378 if (ret < 0) 2379 return ret; 2380 } 2381 2382 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf) 2383 return 0; 2384 2385 ret = av_packet_ref(pkt_ref, pkt); 2386 if (ret < 0) 2387 return ret; 2388 2389 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref); 2390 if (ret < 0) { 2391 av_packet_unref(pkt_ref); 2392 return ret; 2393 } 2394 2395 while (ret >= 0 && !sti->avctx->extradata) { 2396 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref); 2397 if (ret < 0) { 2398 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) 2399 return ret; 2400 continue; 2401 } 2402 2403 for (int i = 0; i < pkt_ref->side_data_elems; i++) { 2404 AVPacketSideData *const side_data = &pkt_ref->side_data[i]; 2405 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) { 2406 sti->avctx->extradata = side_data->data; 2407 sti->avctx->extradata_size = side_data->size; 2408 side_data->data = NULL; 2409 side_data->size = 0; 2410 break; 2411 } 2412 } 2413 av_packet_unref(pkt_ref); 2414 } 2415 2416 return 0; 2417} 2418 2419static int add_coded_side_data(AVStream *st, AVCodecContext *avctx) 2420{ 2421 for (int i = 0; i < avctx->nb_coded_side_data; i++) { 2422 const AVPacketSideData *const sd_src = &avctx->coded_side_data[i]; 2423 uint8_t *dst_data; 2424 dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size); 2425 if (!dst_data) 2426 return AVERROR(ENOMEM); 2427 memcpy(dst_data, sd_src->data, sd_src->size); 2428 } 2429 return 0; 2430} 2431 2432int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) 2433{ 2434 FFFormatContext *const si = ffformatcontext(ic); 2435 int count = 0, ret = 0; 2436 int64_t read_size; 2437 AVPacket *pkt1 = si->pkt; 2438 int64_t old_offset = avio_tell(ic->pb); 2439 // new streams might appear, no options for those 2440 int orig_nb_streams = ic->nb_streams; 2441 int flush_codecs; 2442 int64_t max_analyze_duration = ic->max_analyze_duration; 2443 int64_t max_stream_analyze_duration; 2444 int64_t max_subtitle_analyze_duration; 2445 int64_t probesize = ic->probesize; 2446 int eof_reached = 0; 2447 int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams"); 2448 2449 flush_codecs = probesize > 0; 2450 2451 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN); 2452 2453 max_stream_analyze_duration = max_analyze_duration; 2454 max_subtitle_analyze_duration = max_analyze_duration; 2455 if (!max_analyze_duration) { 2456 max_stream_analyze_duration = 2457 max_analyze_duration = 5*AV_TIME_BASE; 2458 max_subtitle_analyze_duration = 30*AV_TIME_BASE; 2459 if (!strcmp(ic->iformat->name, "flv")) 2460 max_stream_analyze_duration = 90*AV_TIME_BASE; 2461 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts")) 2462 max_stream_analyze_duration = 7*AV_TIME_BASE; 2463 } 2464 2465 if (ic->pb) { 2466 FFIOContext *const ctx = ffiocontext(ic->pb); 2467 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n", 2468 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams); 2469 } 2470 2471 for (unsigned i = 0; i < ic->nb_streams; i++) { 2472 const AVCodec *codec; 2473 AVDictionary *thread_opt = NULL; 2474 AVStream *const st = ic->streams[i]; 2475 FFStream *const sti = ffstream(st); 2476 AVCodecContext *const avctx = sti->avctx; 2477 2478 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || 2479 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { 2480/* if (!st->time_base.num) 2481 st->time_base = */ 2482 if (!avctx->time_base.num) 2483 avctx->time_base = st->time_base; 2484 } 2485 2486 /* check if the caller has overridden the codec id */ 2487 // only for the split stuff 2488 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) { 2489 sti->parser = av_parser_init(st->codecpar->codec_id); 2490 if (sti->parser) { 2491 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) { 2492 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; 2493 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) { 2494 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS; 2495 } 2496 } else if (sti->need_parsing) { 2497 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec " 2498 "%s, packets or times may be invalid.\n", 2499 avcodec_get_name(st->codecpar->codec_id)); 2500 } 2501 } 2502 2503 ret = avcodec_parameters_to_context(avctx, st->codecpar); 2504 if (ret < 0) 2505 goto find_stream_info_err; 2506 if (sti->request_probe <= 0) 2507 sti->avctx_inited = 1; 2508 2509 codec = find_probe_decoder(ic, st, st->codecpar->codec_id); 2510 2511 /* Force thread count to 1 since the H.264 decoder will not extract 2512 * SPS and PPS to extradata during multi-threaded decoding. */ 2513 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0); 2514 /* Force lowres to 0. The decoder might reduce the video size by the 2515 * lowres factor, and we don't want that propagated to the stream's 2516 * codecpar */ 2517 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0); 2518 2519 if (ic->codec_whitelist) 2520 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0); 2521 2522 // Try to just open decoders, in case this is enough to get parameters. 2523 // Also ensure that subtitle_header is properly set. 2524 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 || 2525 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { 2526 if (codec && !avctx->codec) 2527 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0) 2528 av_log(ic, AV_LOG_WARNING, 2529 "Failed to open codec in %s\n",__FUNCTION__); 2530 } 2531 if (!options) 2532 av_dict_free(&thread_opt); 2533 } 2534 2535 read_size = 0; 2536 for (;;) { 2537 const AVPacket *pkt; 2538 AVStream *st; 2539 FFStream *sti; 2540 AVCodecContext *avctx; 2541 int analyzed_all_streams; 2542 unsigned i; 2543 if (ff_check_interrupt(&ic->interrupt_callback)) { 2544 ret = AVERROR_EXIT; 2545 av_log(ic, AV_LOG_DEBUG, "interrupted\n"); 2546 break; 2547 } 2548 2549 /* check if one codec still needs to be handled */ 2550 for (i = 0; i < ic->nb_streams; i++) { 2551 AVStream *const st = ic->streams[i]; 2552 FFStream *const sti = ffstream(st); 2553 int fps_analyze_framecount = 20; 2554 int count; 2555 2556 if (!has_codec_parameters(st, NULL)) 2557 break; 2558 /* If the timebase is coarse (like the usual millisecond precision 2559 * of mkv), we need to analyze more frames to reliably arrive at 2560 * the correct fps. */ 2561 if (av_q2d(st->time_base) > 0.0005) 2562 fps_analyze_framecount *= 2; 2563 if (!tb_unreliable(sti->avctx)) 2564 fps_analyze_framecount = 0; 2565 if (ic->fps_probe_size >= 0) 2566 fps_analyze_framecount = ic->fps_probe_size; 2567 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) 2568 fps_analyze_framecount = 0; 2569 /* variable fps and no guess at the real fps */ 2570 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ? 2571 sti->info->codec_info_duration_fields/2 : 2572 sti->info->duration_count; 2573 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) && 2574 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { 2575 if (count < fps_analyze_framecount) 2576 break; 2577 } 2578 // Look at the first 3 frames if there is evidence of frame delay 2579 // but the decoder delay is not set. 2580 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0) 2581 break; 2582 if (!sti->avctx->extradata && 2583 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) && 2584 extract_extradata_check(st)) 2585 break; 2586 if (sti->first_dts == AV_NOPTS_VALUE && 2587 !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) && 2588 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) && 2589 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || 2590 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)) 2591 break; 2592 } 2593 analyzed_all_streams = 0; 2594 if (!missing_streams || !*missing_streams) 2595 if (i == ic->nb_streams) { 2596 analyzed_all_streams = 1; 2597 /* NOTE: If the format has no header, then we need to read some 2598 * packets to get most of the streams, so we cannot stop here. */ 2599 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { 2600 /* If we found the info for all the codecs, we can stop. */ 2601 ret = count; 2602 av_log(ic, AV_LOG_DEBUG, "All info found\n"); 2603 flush_codecs = 0; 2604 break; 2605 } 2606 } 2607 /* We did not get all the codec info, but we read too much data. */ 2608 if (read_size >= probesize) { 2609 ret = count; 2610 av_log(ic, AV_LOG_DEBUG, 2611 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize); 2612 for (unsigned i = 0; i < ic->nb_streams; i++) { 2613 AVStream *const st = ic->streams[i]; 2614 FFStream *const sti = ffstream(st); 2615 if (!st->r_frame_rate.num && 2616 sti->info->duration_count <= 1 && 2617 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && 2618 strcmp(ic->iformat->name, "image2")) 2619 av_log(ic, AV_LOG_WARNING, 2620 "Stream #%d: not enough frames to estimate rate; " 2621 "consider increasing probesize\n", i); 2622 } 2623 break; 2624 } 2625 2626 /* NOTE: A new stream can be added there if no header in file 2627 * (AVFMTCTX_NOHEADER). */ 2628 ret = read_frame_internal(ic, pkt1); 2629 if (ret == AVERROR(EAGAIN)) 2630 continue; 2631 2632 if (ret < 0) { 2633 /* EOF or error*/ 2634 eof_reached = 1; 2635 break; 2636 } 2637 2638 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) { 2639 ret = avpriv_packet_list_put(&si->packet_buffer, 2640 pkt1, NULL, 0); 2641 if (ret < 0) 2642 goto unref_then_goto_end; 2643 2644 pkt = &si->packet_buffer.tail->pkt; 2645 } else { 2646 pkt = pkt1; 2647 } 2648 2649 st = ic->streams[pkt->stream_index]; 2650 sti = ffstream(st); 2651 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) 2652 read_size += pkt->size; 2653 2654 avctx = sti->avctx; 2655 if (!sti->avctx_inited) { 2656 ret = avcodec_parameters_to_context(avctx, st->codecpar); 2657 if (ret < 0) 2658 goto unref_then_goto_end; 2659 sti->avctx_inited = 1; 2660 } 2661 2662 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) { 2663 /* check for non-increasing dts */ 2664 if (sti->info->fps_last_dts != AV_NOPTS_VALUE && 2665 sti->info->fps_last_dts >= pkt->dts) { 2666 av_log(ic, AV_LOG_DEBUG, 2667 "Non-increasing DTS in stream %d: packet %d with DTS " 2668 "%"PRId64", packet %d with DTS %"PRId64"\n", 2669 st->index, sti->info->fps_last_dts_idx, 2670 sti->info->fps_last_dts, sti->codec_info_nb_frames, 2671 pkt->dts); 2672 sti->info->fps_first_dts = 2673 sti->info->fps_last_dts = AV_NOPTS_VALUE; 2674 } 2675 /* Check for a discontinuity in dts. If the difference in dts 2676 * is more than 1000 times the average packet duration in the 2677 * sequence, we treat it as a discontinuity. */ 2678 if (sti->info->fps_last_dts != AV_NOPTS_VALUE && 2679 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx && 2680 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 > 2681 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) / 2682 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) { 2683 av_log(ic, AV_LOG_WARNING, 2684 "DTS discontinuity in stream %d: packet %d with DTS " 2685 "%"PRId64", packet %d with DTS %"PRId64"\n", 2686 st->index, sti->info->fps_last_dts_idx, 2687 sti->info->fps_last_dts, sti->codec_info_nb_frames, 2688 pkt->dts); 2689 sti->info->fps_first_dts = 2690 sti->info->fps_last_dts = AV_NOPTS_VALUE; 2691 } 2692 2693 /* update stored dts values */ 2694 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) { 2695 sti->info->fps_first_dts = pkt->dts; 2696 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames; 2697 } 2698 sti->info->fps_last_dts = pkt->dts; 2699 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames; 2700 } 2701 if (sti->codec_info_nb_frames > 1) { 2702 int64_t t = 0; 2703 int64_t limit; 2704 2705 if (st->time_base.den > 0) 2706 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q); 2707 if (st->avg_frame_rate.num > 0) 2708 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q)); 2709 2710 if ( t == 0 2711 && sti->codec_info_nb_frames > 30 2712 && sti->info->fps_first_dts != AV_NOPTS_VALUE 2713 && sti->info->fps_last_dts != AV_NOPTS_VALUE) { 2714 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts); 2715 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q)); 2716 } 2717 2718 if (analyzed_all_streams) limit = max_analyze_duration; 2719 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration; 2720 else limit = max_stream_analyze_duration; 2721 2722 if (t >= limit) { 2723 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n", 2724 limit, 2725 t, pkt->stream_index); 2726 if (ic->flags & AVFMT_FLAG_NOBUFFER) 2727 av_packet_unref(pkt1); 2728 break; 2729 } 2730 if (pkt->duration > 0) { 2731 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time 2732 && (uint64_t)pkt->pts - st->start_time < INT64_MAX 2733 ) { 2734 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration); 2735 } else 2736 sti->info->codec_info_duration += pkt->duration; 2737 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && avctx->ticks_per_frame == 2 2738 ? sti->parser->repeat_pict + 1 : 2; 2739 } 2740 } 2741 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { 2742#if FF_API_R_FRAME_RATE 2743 ff_rfps_add_frame(ic, st, pkt->dts); 2744#endif 2745 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE) 2746 sti->info->frame_delay_evidence = 1; 2747 } 2748 if (!sti->avctx->extradata) { 2749 ret = extract_extradata(si, st, pkt); 2750 if (ret < 0) 2751 goto unref_then_goto_end; 2752 } 2753 2754 /* If still no information, we try to open the codec and to 2755 * decompress the frame. We try to avoid that in most cases as 2756 * it takes longer and uses more memory. For MPEG-4, we need to 2757 * decompress for QuickTime. 2758 * 2759 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at 2760 * least one frame of codec data, this makes sure the codec initializes 2761 * the channel configuration and does not only trust the values from 2762 * the container. */ 2763 try_decode_frame(ic, st, pkt, 2764 (options && i < orig_nb_streams) ? &options[i] : NULL); 2765 2766 if (ic->flags & AVFMT_FLAG_NOBUFFER) 2767 av_packet_unref(pkt1); 2768 2769 sti->codec_info_nb_frames++; 2770 count++; 2771 } 2772 2773 if (eof_reached) { 2774 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) { 2775 AVStream *const st = ic->streams[stream_index]; 2776 AVCodecContext *const avctx = ffstream(st)->avctx; 2777 if (!has_codec_parameters(st, NULL)) { 2778 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id); 2779 if (codec && !avctx->codec) { 2780 AVDictionary *opts = NULL; 2781 if (ic->codec_whitelist) 2782 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0); 2783 if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0) 2784 av_log(ic, AV_LOG_WARNING, 2785 "Failed to open codec in %s\n",__FUNCTION__); 2786 av_dict_free(&opts); 2787 } 2788 } 2789 2790 // EOF already reached while reading the stream above. 2791 // So continue with reoordering DTS with whatever delay we have. 2792 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) { 2793 update_dts_from_pts(ic, stream_index, si->packet_buffer.head); 2794 } 2795 } 2796 } 2797 2798 if (flush_codecs) { 2799 AVPacket *empty_pkt = si->pkt; 2800 int err = 0; 2801 av_packet_unref(empty_pkt); 2802 2803 for (unsigned i = 0; i < ic->nb_streams; i++) { 2804 AVStream *const st = ic->streams[i]; 2805 FFStream *const sti = ffstream(st); 2806 2807 /* flush the decoders */ 2808 if (sti->info->found_decoder == 1) { 2809 err = try_decode_frame(ic, st, empty_pkt, 2810 (options && i < orig_nb_streams) 2811 ? &options[i] : NULL); 2812 2813 if (err < 0) { 2814 av_log(ic, AV_LOG_INFO, 2815 "decoding for stream %d failed\n", st->index); 2816 } 2817 } 2818 } 2819 } 2820 2821 ff_rfps_calculate(ic); 2822 2823 for (unsigned i = 0; i < ic->nb_streams; i++) { 2824 AVStream *const st = ic->streams[i]; 2825 FFStream *const sti = ffstream(st); 2826 AVCodecContext *const avctx = sti->avctx; 2827 2828 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { 2829 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) { 2830 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt); 2831 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt) 2832 avctx->codec_tag= tag; 2833 } 2834 2835 /* estimate average framerate if not set by demuxer */ 2836 if (sti->info->codec_info_duration_fields && 2837 !st->avg_frame_rate.num && 2838 sti->info->codec_info_duration) { 2839 int best_fps = 0; 2840 double best_error = 0.01; 2841 AVRational codec_frame_rate = avctx->framerate; 2842 2843 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2|| 2844 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den || 2845 sti->info->codec_info_duration < 0) 2846 continue; 2847 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, 2848 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den, 2849 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000); 2850 2851 /* Round guessed framerate to a "standard" framerate if it's 2852 * within 1% of the original estimate. */ 2853 for (int j = 0; j < MAX_STD_TIMEBASES; j++) { 2854 AVRational std_fps = { get_std_framerate(j), 12 * 1001 }; 2855 double error = fabs(av_q2d(st->avg_frame_rate) / 2856 av_q2d(std_fps) - 1); 2857 2858 if (error < best_error) { 2859 best_error = error; 2860 best_fps = std_fps.num; 2861 } 2862 2863 if (si->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) { 2864 error = fabs(av_q2d(codec_frame_rate) / 2865 av_q2d(std_fps) - 1); 2866 if (error < best_error) { 2867 best_error = error; 2868 best_fps = std_fps.num; 2869 } 2870 } 2871 } 2872 if (best_fps) 2873 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, 2874 best_fps, 12 * 1001, INT_MAX); 2875 } 2876 2877 if (!st->r_frame_rate.num) { 2878 if ( avctx->time_base.den * (int64_t) st->time_base.num 2879 <= avctx->time_base.num * (uint64_t)avctx->ticks_per_frame * st->time_base.den) { 2880 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, 2881 avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX); 2882 } else { 2883 st->r_frame_rate.num = st->time_base.den; 2884 st->r_frame_rate.den = st->time_base.num; 2885 } 2886 } 2887 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) { 2888 AVRational hw_ratio = { avctx->height, avctx->width }; 2889 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio, 2890 hw_ratio); 2891 } 2892 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { 2893 if (!avctx->bits_per_coded_sample) 2894 avctx->bits_per_coded_sample = 2895 av_get_bits_per_sample(avctx->codec_id); 2896 // set stream disposition based on audio service type 2897 switch (avctx->audio_service_type) { 2898 case AV_AUDIO_SERVICE_TYPE_EFFECTS: 2899 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS; 2900 break; 2901 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED: 2902 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED; 2903 break; 2904 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED: 2905 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED; 2906 break; 2907 case AV_AUDIO_SERVICE_TYPE_COMMENTARY: 2908 st->disposition = AV_DISPOSITION_COMMENT; 2909 break; 2910 case AV_AUDIO_SERVICE_TYPE_KARAOKE: 2911 st->disposition = AV_DISPOSITION_KARAOKE; 2912 break; 2913 } 2914 } 2915 } 2916 2917 if (probesize) 2918 estimate_timings(ic, old_offset); 2919 2920 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN); 2921 2922 if (ret >= 0 && ic->nb_streams) 2923 /* We could not have all the codec parameters before EOF. */ 2924 ret = -1; 2925 for (unsigned i = 0; i < ic->nb_streams; i++) { 2926 AVStream *const st = ic->streams[i]; 2927 FFStream *const sti = ffstream(st); 2928 const char *errmsg; 2929 2930 /* if no packet was ever seen, update context now for has_codec_parameters */ 2931 if (!sti->avctx_inited) { 2932 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && 2933 st->codecpar->format == AV_SAMPLE_FMT_NONE) 2934 st->codecpar->format = sti->avctx->sample_fmt; 2935 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); 2936 if (ret < 0) 2937 goto find_stream_info_err; 2938 } 2939 if (!has_codec_parameters(st, &errmsg)) { 2940 char buf[256]; 2941 avcodec_string(buf, sizeof(buf), sti->avctx, 0); 2942 av_log(ic, AV_LOG_WARNING, 2943 "Could not find codec parameters for stream %d (%s): %s\n" 2944 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n", 2945 i, buf, errmsg, ic->max_analyze_duration, ic->probesize); 2946 } else { 2947 ret = 0; 2948 } 2949 } 2950 2951 ret = compute_chapters_end(ic); 2952 if (ret < 0) 2953 goto find_stream_info_err; 2954 2955 /* update the stream parameters from the internal codec contexts */ 2956 for (unsigned i = 0; i < ic->nb_streams; i++) { 2957 AVStream *const st = ic->streams[i]; 2958 FFStream *const sti = ffstream(st); 2959 2960 if (sti->avctx_inited) { 2961 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx); 2962 if (ret < 0) 2963 goto find_stream_info_err; 2964 ret = add_coded_side_data(st, sti->avctx); 2965 if (ret < 0) 2966 goto find_stream_info_err; 2967 } 2968 2969 sti->avctx_inited = 0; 2970 } 2971 2972find_stream_info_err: 2973 for (unsigned i = 0; i < ic->nb_streams; i++) { 2974 AVStream *const st = ic->streams[i]; 2975 FFStream *const sti = ffstream(st); 2976 if (sti->info) { 2977 av_freep(&sti->info->duration_error); 2978 av_freep(&sti->info); 2979 } 2980 avcodec_close(sti->avctx); 2981 // FIXME: avcodec_close() frees AVOption settable fields which includes ch_layout, 2982 // so we need to restore it. 2983 av_channel_layout_copy(&sti->avctx->ch_layout, &st->codecpar->ch_layout); 2984 av_bsf_free(&sti->extract_extradata.bsf); 2985 } 2986 if (ic->pb) { 2987 FFIOContext *const ctx = ffiocontext(ic->pb); 2988 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n", 2989 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count); 2990 } 2991 return ret; 2992 2993unref_then_goto_end: 2994 av_packet_unref(pkt1); 2995 goto find_stream_info_err; 2996} 2997