You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5856 lines
204KB

  1. /*
  2. * various utility functions for use within FFmpeg
  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. #include <stdint.h>
  22. #include "config.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/dict.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/pixfmt.h"
  31. #include "libavutil/thread.h"
  32. #include "libavutil/time.h"
  33. #include "libavutil/timestamp.h"
  34. #include "libavcodec/bytestream.h"
  35. #include "libavcodec/internal.h"
  36. #include "libavcodec/packet_internal.h"
  37. #include "libavcodec/raw.h"
  38. #include "avformat.h"
  39. #include "avio_internal.h"
  40. #include "id3v2.h"
  41. #include "internal.h"
  42. #if CONFIG_NETWORK
  43. #include "network.h"
  44. #endif
  45. #include "url.h"
  46. #include "libavutil/ffversion.h"
  47. const char av_format_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  48. static AVMutex avformat_mutex = AV_MUTEX_INITIALIZER;
  49. /**
  50. * @file
  51. * various utility functions for use within FFmpeg
  52. */
  53. unsigned avformat_version(void)
  54. {
  55. av_assert0(LIBAVFORMAT_VERSION_MICRO >= 100);
  56. return LIBAVFORMAT_VERSION_INT;
  57. }
  58. const char *avformat_configuration(void)
  59. {
  60. return FFMPEG_CONFIGURATION;
  61. }
  62. const char *avformat_license(void)
  63. {
  64. #define LICENSE_PREFIX "libavformat license: "
  65. return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
  66. }
  67. int ff_lock_avformat(void)
  68. {
  69. return ff_mutex_lock(&avformat_mutex) ? -1 : 0;
  70. }
  71. int ff_unlock_avformat(void)
  72. {
  73. return ff_mutex_unlock(&avformat_mutex) ? -1 : 0;
  74. }
  75. #define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
  76. static int is_relative(int64_t ts) {
  77. return ts > (RELATIVE_TS_BASE - (1LL<<48));
  78. }
  79. /**
  80. * Wrap a given time stamp, if there is an indication for an overflow
  81. *
  82. * @param st stream
  83. * @param timestamp the time stamp to wrap
  84. * @return resulting time stamp
  85. */
  86. static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
  87. {
  88. if (st->internal->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
  89. st->internal->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
  90. if (st->internal->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
  91. timestamp < st->internal->pts_wrap_reference)
  92. return timestamp + (1ULL << st->pts_wrap_bits);
  93. else if (st->internal->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
  94. timestamp >= st->internal->pts_wrap_reference)
  95. return timestamp - (1ULL << st->pts_wrap_bits);
  96. }
  97. return timestamp;
  98. }
  99. #if FF_API_FORMAT_GET_SET
  100. MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
  101. #if FF_API_LAVF_FFSERVER
  102. FF_DISABLE_DEPRECATION_WARNINGS
  103. MAKE_ACCESSORS(AVStream, stream, char *, recommended_encoder_configuration)
  104. FF_ENABLE_DEPRECATION_WARNINGS
  105. #endif
  106. MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, video_codec)
  107. MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, audio_codec)
  108. MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, subtitle_codec)
  109. MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, data_codec)
  110. MAKE_ACCESSORS(AVFormatContext, format, int, metadata_header_padding)
  111. MAKE_ACCESSORS(AVFormatContext, format, void *, opaque)
  112. MAKE_ACCESSORS(AVFormatContext, format, av_format_control_message, control_message_cb)
  113. #if FF_API_OLD_OPEN_CALLBACKS
  114. FF_DISABLE_DEPRECATION_WARNINGS
  115. MAKE_ACCESSORS(AVFormatContext, format, AVOpenCallback, open_cb)
  116. FF_ENABLE_DEPRECATION_WARNINGS
  117. #endif
  118. #endif
  119. int64_t av_stream_get_end_pts(const AVStream *st)
  120. {
  121. if (st->internal->priv_pts) {
  122. return st->internal->priv_pts->val;
  123. } else
  124. return AV_NOPTS_VALUE;
  125. }
  126. struct AVCodecParserContext *av_stream_get_parser(const AVStream *st)
  127. {
  128. return st->parser;
  129. }
  130. void av_format_inject_global_side_data(AVFormatContext *s)
  131. {
  132. int i;
  133. s->internal->inject_global_side_data = 1;
  134. for (i = 0; i < s->nb_streams; i++) {
  135. AVStream *st = s->streams[i];
  136. st->internal->inject_global_side_data = 1;
  137. }
  138. }
  139. int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
  140. {
  141. av_assert0(!dst->codec_whitelist &&
  142. !dst->format_whitelist &&
  143. !dst->protocol_whitelist &&
  144. !dst->protocol_blacklist);
  145. dst-> codec_whitelist = av_strdup(src->codec_whitelist);
  146. dst->format_whitelist = av_strdup(src->format_whitelist);
  147. dst->protocol_whitelist = av_strdup(src->protocol_whitelist);
  148. dst->protocol_blacklist = av_strdup(src->protocol_blacklist);
  149. if ( (src-> codec_whitelist && !dst-> codec_whitelist)
  150. || (src-> format_whitelist && !dst-> format_whitelist)
  151. || (src->protocol_whitelist && !dst->protocol_whitelist)
  152. || (src->protocol_blacklist && !dst->protocol_blacklist)) {
  153. av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
  154. return AVERROR(ENOMEM);
  155. }
  156. return 0;
  157. }
  158. static const AVCodec *find_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
  159. {
  160. #if FF_API_LAVF_AVCTX
  161. FF_DISABLE_DEPRECATION_WARNINGS
  162. if (st->codec->codec)
  163. return st->codec->codec;
  164. FF_ENABLE_DEPRECATION_WARNINGS
  165. #endif
  166. switch (st->codecpar->codec_type) {
  167. case AVMEDIA_TYPE_VIDEO:
  168. if (s->video_codec) return s->video_codec;
  169. break;
  170. case AVMEDIA_TYPE_AUDIO:
  171. if (s->audio_codec) return s->audio_codec;
  172. break;
  173. case AVMEDIA_TYPE_SUBTITLE:
  174. if (s->subtitle_codec) return s->subtitle_codec;
  175. break;
  176. }
  177. return avcodec_find_decoder(codec_id);
  178. }
  179. static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
  180. {
  181. const AVCodec *codec;
  182. #if CONFIG_H264_DECODER
  183. /* Other parts of the code assume this decoder to be used for h264,
  184. * so force it if possible. */
  185. if (codec_id == AV_CODEC_ID_H264)
  186. return avcodec_find_decoder_by_name("h264");
  187. #endif
  188. codec = find_decoder(s, st, codec_id);
  189. if (!codec)
  190. return NULL;
  191. if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
  192. const AVCodec *probe_codec = NULL;
  193. void *iter = NULL;
  194. while ((probe_codec = av_codec_iterate(&iter))) {
  195. if (probe_codec->id == codec->id &&
  196. av_codec_is_decoder(probe_codec) &&
  197. !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
  198. return probe_codec;
  199. }
  200. }
  201. }
  202. return codec;
  203. }
  204. #if FF_API_FORMAT_GET_SET
  205. int av_format_get_probe_score(const AVFormatContext *s)
  206. {
  207. return s->probe_score;
  208. }
  209. #endif
  210. /* an arbitrarily chosen "sane" max packet size -- 50M */
  211. #define SANE_CHUNK_SIZE (50000000)
  212. int ffio_limit(AVIOContext *s, int size)
  213. {
  214. if (s->maxsize>= 0) {
  215. int64_t pos = avio_tell(s);
  216. int64_t remaining= s->maxsize - pos;
  217. if (remaining < size) {
  218. int64_t newsize = avio_size(s);
  219. if (!s->maxsize || s->maxsize<newsize)
  220. s->maxsize = newsize - !newsize;
  221. if (pos > s->maxsize && s->maxsize >= 0)
  222. s->maxsize = AVERROR(EIO);
  223. if (s->maxsize >= 0)
  224. remaining = s->maxsize - pos;
  225. }
  226. if (s->maxsize >= 0 && remaining < size && size > 1) {
  227. av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG,
  228. "Truncating packet of size %d to %"PRId64"\n",
  229. size, remaining + !remaining);
  230. size = remaining + !remaining;
  231. }
  232. }
  233. return size;
  234. }
  235. /* Read the data in sane-sized chunks and append to pkt.
  236. * Return the number of bytes read or an error. */
  237. static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
  238. {
  239. int orig_size = pkt->size;
  240. int ret;
  241. do {
  242. int prev_size = pkt->size;
  243. int read_size;
  244. /* When the caller requests a lot of data, limit it to the amount
  245. * left in file or SANE_CHUNK_SIZE when it is not known. */
  246. read_size = size;
  247. if (read_size > SANE_CHUNK_SIZE/10) {
  248. read_size = ffio_limit(s, read_size);
  249. // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
  250. if (s->maxsize < 0)
  251. read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
  252. }
  253. ret = av_grow_packet(pkt, read_size);
  254. if (ret < 0)
  255. break;
  256. ret = avio_read(s, pkt->data + prev_size, read_size);
  257. if (ret != read_size) {
  258. av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
  259. break;
  260. }
  261. size -= read_size;
  262. } while (size > 0);
  263. if (size > 0)
  264. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  265. if (!pkt->size)
  266. av_packet_unref(pkt);
  267. return pkt->size > orig_size ? pkt->size - orig_size : ret;
  268. }
  269. int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
  270. {
  271. #if FF_API_INIT_PACKET
  272. FF_DISABLE_DEPRECATION_WARNINGS
  273. av_init_packet(pkt);
  274. pkt->data = NULL;
  275. pkt->size = 0;
  276. FF_ENABLE_DEPRECATION_WARNINGS
  277. #else
  278. av_packet_unref(pkt);
  279. #endif
  280. pkt->pos = avio_tell(s);
  281. return append_packet_chunked(s, pkt, size);
  282. }
  283. int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
  284. {
  285. if (!pkt->size)
  286. return av_get_packet(s, pkt, size);
  287. return append_packet_chunked(s, pkt, size);
  288. }
  289. int av_filename_number_test(const char *filename)
  290. {
  291. char buf[1024];
  292. return filename &&
  293. (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
  294. }
  295. static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
  296. AVProbeData *pd)
  297. {
  298. static const struct {
  299. const char *name;
  300. enum AVCodecID id;
  301. enum AVMediaType type;
  302. } fmt_id_type[] = {
  303. { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
  304. { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
  305. { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO },
  306. { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
  307. { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE,AVMEDIA_TYPE_SUBTITLE },
  308. { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT,AVMEDIA_TYPE_SUBTITLE },
  309. { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
  310. { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
  311. { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
  312. { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
  313. { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
  314. { "mjpeg_2000",AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
  315. { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
  316. { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
  317. { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
  318. { 0 }
  319. };
  320. int score;
  321. const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
  322. if (fmt) {
  323. int i;
  324. av_log(s, AV_LOG_DEBUG,
  325. "Probe with size=%d, packets=%d detected %s with score=%d\n",
  326. pd->buf_size, s->max_probe_packets - st->probe_packets,
  327. fmt->name, score);
  328. for (i = 0; fmt_id_type[i].name; i++) {
  329. if (!strcmp(fmt->name, fmt_id_type[i].name)) {
  330. if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
  331. st->codecpar->sample_rate)
  332. continue;
  333. if (st->internal->request_probe > score &&
  334. st->codecpar->codec_id != fmt_id_type[i].id)
  335. continue;
  336. st->codecpar->codec_id = fmt_id_type[i].id;
  337. st->codecpar->codec_type = fmt_id_type[i].type;
  338. st->internal->need_context_update = 1;
  339. #if FF_API_LAVF_AVCTX
  340. FF_DISABLE_DEPRECATION_WARNINGS
  341. st->codec->codec_type = st->codecpar->codec_type;
  342. st->codec->codec_id = st->codecpar->codec_id;
  343. FF_ENABLE_DEPRECATION_WARNINGS
  344. #endif
  345. return score;
  346. }
  347. }
  348. }
  349. return 0;
  350. }
  351. /************************************************************/
  352. /* input media file */
  353. #if FF_API_DEMUXER_OPEN
  354. int av_demuxer_open(AVFormatContext *ic) {
  355. int err;
  356. if (ic->format_whitelist && av_match_list(ic->iformat->name, ic->format_whitelist, ',') <= 0) {
  357. av_log(ic, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", ic->format_whitelist);
  358. return AVERROR(EINVAL);
  359. }
  360. if (ic->iformat->read_header) {
  361. err = ic->iformat->read_header(ic);
  362. if (err < 0)
  363. return err;
  364. }
  365. if (ic->pb && !ic->internal->data_offset)
  366. ic->internal->data_offset = avio_tell(ic->pb);
  367. return 0;
  368. }
  369. #endif
  370. /* Open input file and probe the format if necessary. */
  371. static int init_input(AVFormatContext *s, const char *filename,
  372. AVDictionary **options)
  373. {
  374. int ret;
  375. AVProbeData pd = { filename, NULL, 0 };
  376. int score = AVPROBE_SCORE_RETRY;
  377. if (s->pb) {
  378. s->flags |= AVFMT_FLAG_CUSTOM_IO;
  379. if (!s->iformat)
  380. return av_probe_input_buffer2(s->pb, &s->iformat, filename,
  381. s, 0, s->format_probesize);
  382. else if (s->iformat->flags & AVFMT_NOFILE)
  383. av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
  384. "will be ignored with AVFMT_NOFILE format.\n");
  385. return 0;
  386. }
  387. if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
  388. (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
  389. return score;
  390. if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
  391. return ret;
  392. if (s->iformat)
  393. return 0;
  394. return av_probe_input_buffer2(s->pb, &s->iformat, filename,
  395. s, 0, s->format_probesize);
  396. }
  397. int avformat_queue_attached_pictures(AVFormatContext *s)
  398. {
  399. int i, ret;
  400. for (i = 0; i < s->nb_streams; i++)
  401. if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
  402. s->streams[i]->discard < AVDISCARD_ALL) {
  403. if (s->streams[i]->attached_pic.size <= 0) {
  404. av_log(s, AV_LOG_WARNING,
  405. "Attached picture on stream %d has invalid size, "
  406. "ignoring\n", i);
  407. continue;
  408. }
  409. ret = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
  410. &s->internal->raw_packet_buffer_end,
  411. &s->streams[i]->attached_pic,
  412. av_packet_ref, 0);
  413. if (ret < 0)
  414. return ret;
  415. }
  416. return 0;
  417. }
  418. static int update_stream_avctx(AVFormatContext *s)
  419. {
  420. int i, ret;
  421. for (i = 0; i < s->nb_streams; i++) {
  422. AVStream *st = s->streams[i];
  423. if (!st->internal->need_context_update)
  424. continue;
  425. /* close parser, because it depends on the codec */
  426. if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
  427. av_parser_close(st->parser);
  428. st->parser = NULL;
  429. }
  430. /* update internal codec context, for the parser */
  431. ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
  432. if (ret < 0)
  433. return ret;
  434. #if FF_API_LAVF_AVCTX
  435. FF_DISABLE_DEPRECATION_WARNINGS
  436. /* update deprecated public codec context */
  437. ret = avcodec_parameters_to_context(st->codec, st->codecpar);
  438. if (ret < 0)
  439. return ret;
  440. FF_ENABLE_DEPRECATION_WARNINGS
  441. #endif
  442. st->internal->need_context_update = 0;
  443. }
  444. return 0;
  445. }
  446. int avformat_open_input(AVFormatContext **ps, const char *filename,
  447. ff_const59 AVInputFormat *fmt, AVDictionary **options)
  448. {
  449. AVFormatContext *s = *ps;
  450. int i, ret = 0;
  451. AVDictionary *tmp = NULL;
  452. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  453. if (!s && !(s = avformat_alloc_context()))
  454. return AVERROR(ENOMEM);
  455. if (!s->av_class) {
  456. av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
  457. return AVERROR(EINVAL);
  458. }
  459. if (fmt)
  460. s->iformat = fmt;
  461. if (options)
  462. av_dict_copy(&tmp, *options, 0);
  463. if (s->pb) // must be before any goto fail
  464. s->flags |= AVFMT_FLAG_CUSTOM_IO;
  465. if ((ret = av_opt_set_dict(s, &tmp)) < 0)
  466. goto fail;
  467. if (!(s->url = av_strdup(filename ? filename : ""))) {
  468. ret = AVERROR(ENOMEM);
  469. goto fail;
  470. }
  471. #if FF_API_FORMAT_FILENAME
  472. FF_DISABLE_DEPRECATION_WARNINGS
  473. av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
  474. FF_ENABLE_DEPRECATION_WARNINGS
  475. #endif
  476. if ((ret = init_input(s, filename, &tmp)) < 0)
  477. goto fail;
  478. s->probe_score = ret;
  479. if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
  480. s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
  481. if (!s->protocol_whitelist) {
  482. ret = AVERROR(ENOMEM);
  483. goto fail;
  484. }
  485. }
  486. if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
  487. s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
  488. if (!s->protocol_blacklist) {
  489. ret = AVERROR(ENOMEM);
  490. goto fail;
  491. }
  492. }
  493. if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
  494. av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
  495. ret = AVERROR(EINVAL);
  496. goto fail;
  497. }
  498. avio_skip(s->pb, s->skip_initial_bytes);
  499. /* Check filename in case an image number is expected. */
  500. if (s->iformat->flags & AVFMT_NEEDNUMBER) {
  501. if (!av_filename_number_test(filename)) {
  502. ret = AVERROR(EINVAL);
  503. goto fail;
  504. }
  505. }
  506. s->duration = s->start_time = AV_NOPTS_VALUE;
  507. /* Allocate private data. */
  508. if (s->iformat->priv_data_size > 0) {
  509. if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
  510. ret = AVERROR(ENOMEM);
  511. goto fail;
  512. }
  513. if (s->iformat->priv_class) {
  514. *(const AVClass **) s->priv_data = s->iformat->priv_class;
  515. av_opt_set_defaults(s->priv_data);
  516. if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
  517. goto fail;
  518. }
  519. }
  520. /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
  521. if (s->pb)
  522. ff_id3v2_read_dict(s->pb, &s->internal->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
  523. #if FF_API_DEMUXER_OPEN
  524. if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
  525. #else
  526. if (s->iformat->read_header)
  527. #endif
  528. if ((ret = s->iformat->read_header(s)) < 0)
  529. goto fail;
  530. if (!s->metadata) {
  531. s->metadata = s->internal->id3v2_meta;
  532. s->internal->id3v2_meta = NULL;
  533. } else if (s->internal->id3v2_meta) {
  534. av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
  535. av_dict_free(&s->internal->id3v2_meta);
  536. }
  537. if (id3v2_extra_meta) {
  538. if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
  539. !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
  540. if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
  541. goto close;
  542. if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
  543. goto close;
  544. if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
  545. goto close;
  546. } else
  547. av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
  548. }
  549. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  550. if ((ret = avformat_queue_attached_pictures(s)) < 0)
  551. goto close;
  552. #if FF_API_DEMUXER_OPEN
  553. if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->pb && !s->internal->data_offset)
  554. #else
  555. if (s->pb && !s->internal->data_offset)
  556. #endif
  557. s->internal->data_offset = avio_tell(s->pb);
  558. s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
  559. update_stream_avctx(s);
  560. for (i = 0; i < s->nb_streams; i++)
  561. s->streams[i]->internal->orig_codec_id = s->streams[i]->codecpar->codec_id;
  562. if (options) {
  563. av_dict_free(options);
  564. *options = tmp;
  565. }
  566. *ps = s;
  567. return 0;
  568. close:
  569. if (s->iformat->read_close)
  570. s->iformat->read_close(s);
  571. fail:
  572. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  573. av_dict_free(&tmp);
  574. if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
  575. avio_closep(&s->pb);
  576. avformat_free_context(s);
  577. *ps = NULL;
  578. return ret;
  579. }
  580. /*******************************************************/
  581. static void force_codec_ids(AVFormatContext *s, AVStream *st)
  582. {
  583. switch (st->codecpar->codec_type) {
  584. case AVMEDIA_TYPE_VIDEO:
  585. if (s->video_codec_id)
  586. st->codecpar->codec_id = s->video_codec_id;
  587. break;
  588. case AVMEDIA_TYPE_AUDIO:
  589. if (s->audio_codec_id)
  590. st->codecpar->codec_id = s->audio_codec_id;
  591. break;
  592. case AVMEDIA_TYPE_SUBTITLE:
  593. if (s->subtitle_codec_id)
  594. st->codecpar->codec_id = s->subtitle_codec_id;
  595. break;
  596. case AVMEDIA_TYPE_DATA:
  597. if (s->data_codec_id)
  598. st->codecpar->codec_id = s->data_codec_id;
  599. break;
  600. }
  601. }
  602. static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
  603. {
  604. if (st->internal->request_probe>0) {
  605. AVProbeData *pd = &st->internal->probe_data;
  606. int end;
  607. av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, st->probe_packets);
  608. --st->probe_packets;
  609. if (pkt) {
  610. uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
  611. if (!new_buf) {
  612. av_log(s, AV_LOG_WARNING,
  613. "Failed to reallocate probe buffer for stream %d\n",
  614. st->index);
  615. goto no_packet;
  616. }
  617. pd->buf = new_buf;
  618. memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
  619. pd->buf_size += pkt->size;
  620. memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
  621. } else {
  622. no_packet:
  623. st->probe_packets = 0;
  624. if (!pd->buf_size) {
  625. av_log(s, AV_LOG_WARNING,
  626. "nothing to probe for stream %d\n", st->index);
  627. }
  628. }
  629. end= s->internal->raw_packet_buffer_remaining_size <= 0
  630. || st->probe_packets<= 0;
  631. if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
  632. int score = set_codec_from_probe_data(s, st, pd);
  633. if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
  634. || end) {
  635. pd->buf_size = 0;
  636. av_freep(&pd->buf);
  637. st->internal->request_probe = -1;
  638. if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
  639. av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
  640. } else
  641. av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
  642. }
  643. force_codec_ids(s, st);
  644. }
  645. }
  646. return 0;
  647. }
  648. static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
  649. {
  650. int64_t ref = pkt->dts;
  651. int i, pts_wrap_behavior;
  652. int64_t pts_wrap_reference;
  653. AVProgram *first_program;
  654. if (ref == AV_NOPTS_VALUE)
  655. ref = pkt->pts;
  656. if (st->internal->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
  657. return 0;
  658. ref &= (1LL << st->pts_wrap_bits)-1;
  659. // reference time stamp should be 60 s before first time stamp
  660. pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
  661. // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
  662. pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
  663. (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
  664. AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
  665. first_program = av_find_program_from_stream(s, NULL, stream_index);
  666. if (!first_program) {
  667. int default_stream_index = av_find_default_stream_index(s);
  668. if (s->streams[default_stream_index]->internal->pts_wrap_reference == AV_NOPTS_VALUE) {
  669. for (i = 0; i < s->nb_streams; i++) {
  670. if (av_find_program_from_stream(s, NULL, i))
  671. continue;
  672. s->streams[i]->internal->pts_wrap_reference = pts_wrap_reference;
  673. s->streams[i]->internal->pts_wrap_behavior = pts_wrap_behavior;
  674. }
  675. }
  676. else {
  677. st->internal->pts_wrap_reference = s->streams[default_stream_index]->internal->pts_wrap_reference;
  678. st->internal->pts_wrap_behavior = s->streams[default_stream_index]->internal->pts_wrap_behavior;
  679. }
  680. }
  681. else {
  682. AVProgram *program = first_program;
  683. while (program) {
  684. if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
  685. pts_wrap_reference = program->pts_wrap_reference;
  686. pts_wrap_behavior = program->pts_wrap_behavior;
  687. break;
  688. }
  689. program = av_find_program_from_stream(s, program, stream_index);
  690. }
  691. // update every program with differing pts_wrap_reference
  692. program = first_program;
  693. while (program) {
  694. if (program->pts_wrap_reference != pts_wrap_reference) {
  695. for (i = 0; i<program->nb_stream_indexes; i++) {
  696. s->streams[program->stream_index[i]]->internal->pts_wrap_reference = pts_wrap_reference;
  697. s->streams[program->stream_index[i]]->internal->pts_wrap_behavior = pts_wrap_behavior;
  698. }
  699. program->pts_wrap_reference = pts_wrap_reference;
  700. program->pts_wrap_behavior = pts_wrap_behavior;
  701. }
  702. program = av_find_program_from_stream(s, program, stream_index);
  703. }
  704. }
  705. return 1;
  706. }
  707. int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
  708. {
  709. int ret, i, err;
  710. AVStream *st;
  711. #if FF_API_INIT_PACKET
  712. FF_DISABLE_DEPRECATION_WARNINGS
  713. pkt->data = NULL;
  714. pkt->size = 0;
  715. av_init_packet(pkt);
  716. FF_ENABLE_DEPRECATION_WARNINGS
  717. #else
  718. av_packet_unref(pkt);
  719. #endif
  720. for (;;) {
  721. PacketList *pktl = s->internal->raw_packet_buffer;
  722. const AVPacket *pkt1;
  723. if (pktl) {
  724. st = s->streams[pktl->pkt.stream_index];
  725. if (s->internal->raw_packet_buffer_remaining_size <= 0)
  726. if ((err = probe_codec(s, st, NULL)) < 0)
  727. return err;
  728. if (st->internal->request_probe <= 0) {
  729. avpriv_packet_list_get(&s->internal->raw_packet_buffer,
  730. &s->internal->raw_packet_buffer_end, pkt);
  731. s->internal->raw_packet_buffer_remaining_size += pkt->size;
  732. return 0;
  733. }
  734. }
  735. ret = s->iformat->read_packet(s, pkt);
  736. if (ret < 0) {
  737. av_packet_unref(pkt);
  738. /* Some demuxers return FFERROR_REDO when they consume
  739. data and discard it (ignored streams, junk, extradata).
  740. We must re-call the demuxer to get the real packet. */
  741. if (ret == FFERROR_REDO)
  742. continue;
  743. if (!pktl || ret == AVERROR(EAGAIN))
  744. return ret;
  745. for (i = 0; i < s->nb_streams; i++) {
  746. st = s->streams[i];
  747. if (st->probe_packets || st->internal->request_probe > 0)
  748. if ((err = probe_codec(s, st, NULL)) < 0)
  749. return err;
  750. av_assert0(st->internal->request_probe <= 0);
  751. }
  752. continue;
  753. }
  754. err = av_packet_make_refcounted(pkt);
  755. if (err < 0) {
  756. av_packet_unref(pkt);
  757. return err;
  758. }
  759. if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
  760. av_log(s, AV_LOG_WARNING,
  761. "Packet corrupt (stream = %d, dts = %s)",
  762. pkt->stream_index, av_ts2str(pkt->dts));
  763. if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
  764. av_log(s, AV_LOG_WARNING, ", dropping it.\n");
  765. av_packet_unref(pkt);
  766. continue;
  767. }
  768. av_log(s, AV_LOG_WARNING, ".\n");
  769. }
  770. av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
  771. "Invalid stream index.\n");
  772. st = s->streams[pkt->stream_index];
  773. if (update_wrap_reference(s, st, pkt->stream_index, pkt) && st->internal->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
  774. // correct first time stamps to negative values
  775. if (!is_relative(st->first_dts))
  776. st->first_dts = wrap_timestamp(st, st->first_dts);
  777. if (!is_relative(st->start_time))
  778. st->start_time = wrap_timestamp(st, st->start_time);
  779. if (!is_relative(st->cur_dts))
  780. st->cur_dts = wrap_timestamp(st, st->cur_dts);
  781. }
  782. pkt->dts = wrap_timestamp(st, pkt->dts);
  783. pkt->pts = wrap_timestamp(st, pkt->pts);
  784. force_codec_ids(s, st);
  785. /* TODO: audio: time filter; video: frame reordering (pts != dts) */
  786. if (s->use_wallclock_as_timestamps)
  787. pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
  788. if (!pktl && st->internal->request_probe <= 0)
  789. return ret;
  790. err = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
  791. &s->internal->raw_packet_buffer_end,
  792. pkt, NULL, 0);
  793. if (err < 0) {
  794. av_packet_unref(pkt);
  795. return err;
  796. }
  797. pkt1 = &s->internal->raw_packet_buffer_end->pkt;
  798. s->internal->raw_packet_buffer_remaining_size -= pkt1->size;
  799. if ((err = probe_codec(s, st, pkt1)) < 0)
  800. return err;
  801. }
  802. }
  803. /**********************************************************/
  804. static int determinable_frame_size(AVCodecContext *avctx)
  805. {
  806. switch(avctx->codec_id) {
  807. case AV_CODEC_ID_MP1:
  808. case AV_CODEC_ID_MP2:
  809. case AV_CODEC_ID_MP3:
  810. case AV_CODEC_ID_CODEC2:
  811. return 1;
  812. }
  813. return 0;
  814. }
  815. /**
  816. * Return the frame duration in seconds. Return 0 if not available.
  817. */
  818. void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st,
  819. AVCodecParserContext *pc, AVPacket *pkt)
  820. {
  821. AVRational codec_framerate = s->iformat ? st->internal->avctx->framerate :
  822. av_mul_q(av_inv_q(st->internal->avctx->time_base), (AVRational){1, st->internal->avctx->ticks_per_frame});
  823. int frame_size, sample_rate;
  824. #if FF_API_LAVF_AVCTX
  825. FF_DISABLE_DEPRECATION_WARNINGS
  826. if ((!codec_framerate.den || !codec_framerate.num) && st->codec->time_base.den && st->codec->time_base.num)
  827. codec_framerate = av_mul_q(av_inv_q(st->codec->time_base), (AVRational){1, st->codec->ticks_per_frame});
  828. FF_ENABLE_DEPRECATION_WARNINGS
  829. #endif
  830. *pnum = 0;
  831. *pden = 0;
  832. switch (st->codecpar->codec_type) {
  833. case AVMEDIA_TYPE_VIDEO:
  834. if (st->r_frame_rate.num && !pc && s->iformat) {
  835. *pnum = st->r_frame_rate.den;
  836. *pden = st->r_frame_rate.num;
  837. } else if (st->time_base.num * 1000LL > st->time_base.den) {
  838. *pnum = st->time_base.num;
  839. *pden = st->time_base.den;
  840. } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
  841. av_assert0(st->internal->avctx->ticks_per_frame);
  842. av_reduce(pnum, pden,
  843. codec_framerate.den,
  844. codec_framerate.num * (int64_t)st->internal->avctx->ticks_per_frame,
  845. INT_MAX);
  846. if (pc && pc->repeat_pict) {
  847. av_assert0(s->iformat); // this may be wrong for interlaced encoding but its not used for that case
  848. av_reduce(pnum, pden,
  849. (*pnum) * (1LL + pc->repeat_pict),
  850. (*pden),
  851. INT_MAX);
  852. }
  853. /* If this codec can be interlaced or progressive then we need
  854. * a parser to compute duration of a packet. Thus if we have
  855. * no parser in such case leave duration undefined. */
  856. if (st->internal->avctx->ticks_per_frame > 1 && !pc)
  857. *pnum = *pden = 0;
  858. }
  859. break;
  860. case AVMEDIA_TYPE_AUDIO:
  861. if (st->internal->avctx_inited) {
  862. frame_size = av_get_audio_frame_duration(st->internal->avctx, pkt->size);
  863. sample_rate = st->internal->avctx->sample_rate;
  864. } else {
  865. frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
  866. sample_rate = st->codecpar->sample_rate;
  867. }
  868. if (frame_size <= 0 || sample_rate <= 0)
  869. break;
  870. *pnum = frame_size;
  871. *pden = sample_rate;
  872. break;
  873. default:
  874. break;
  875. }
  876. }
  877. int ff_is_intra_only(enum AVCodecID id)
  878. {
  879. const AVCodecDescriptor *d = avcodec_descriptor_get(id);
  880. if (!d)
  881. return 0;
  882. if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) &&
  883. !(d->props & AV_CODEC_PROP_INTRA_ONLY))
  884. return 0;
  885. return 1;
  886. }
  887. static int has_decode_delay_been_guessed(AVStream *st)
  888. {
  889. if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
  890. if (!st->internal->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
  891. return 1;
  892. #if CONFIG_H264_DECODER
  893. if (st->internal->avctx->has_b_frames &&
  894. avpriv_h264_has_num_reorder_frames(st->internal->avctx) == st->internal->avctx->has_b_frames)
  895. return 1;
  896. #endif
  897. if (st->internal->avctx->has_b_frames<3)
  898. return st->internal->nb_decoded_frames >= 7;
  899. else if (st->internal->avctx->has_b_frames<4)
  900. return st->internal->nb_decoded_frames >= 18;
  901. else
  902. return st->internal->nb_decoded_frames >= 20;
  903. }
  904. static PacketList *get_next_pkt(AVFormatContext *s, AVStream *st, PacketList *pktl)
  905. {
  906. if (pktl->next)
  907. return pktl->next;
  908. if (pktl == s->internal->packet_buffer_end)
  909. return s->internal->parse_queue;
  910. return NULL;
  911. }
  912. static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) {
  913. int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
  914. st->codecpar->codec_id != AV_CODEC_ID_HEVC;
  915. if(!onein_oneout) {
  916. int delay = st->internal->avctx->has_b_frames;
  917. int i;
  918. if (dts == AV_NOPTS_VALUE) {
  919. int64_t best_score = INT64_MAX;
  920. for (i = 0; i<delay; i++) {
  921. if (st->internal->pts_reorder_error_count[i]) {
  922. int64_t score = st->internal->pts_reorder_error[i] / st->internal->pts_reorder_error_count[i];
  923. if (score < best_score) {
  924. best_score = score;
  925. dts = pts_buffer[i];
  926. }
  927. }
  928. }
  929. } else {
  930. for (i = 0; i<delay; i++) {
  931. if (pts_buffer[i] != AV_NOPTS_VALUE) {
  932. int64_t diff = FFABS(pts_buffer[i] - dts)
  933. + (uint64_t)st->internal->pts_reorder_error[i];
  934. diff = FFMAX(diff, st->internal->pts_reorder_error[i]);
  935. st->internal->pts_reorder_error[i] = diff;
  936. st->internal->pts_reorder_error_count[i]++;
  937. if (st->internal->pts_reorder_error_count[i] > 250) {
  938. st->internal->pts_reorder_error[i] >>= 1;
  939. st->internal->pts_reorder_error_count[i] >>= 1;
  940. }
  941. }
  942. }
  943. }
  944. }
  945. if (dts == AV_NOPTS_VALUE)
  946. dts = pts_buffer[0];
  947. return dts;
  948. }
  949. /**
  950. * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
  951. * of the packets in a window.
  952. */
  953. static void update_dts_from_pts(AVFormatContext *s, int stream_index,
  954. PacketList *pkt_buffer)
  955. {
  956. AVStream *st = s->streams[stream_index];
  957. int delay = st->internal->avctx->has_b_frames;
  958. int i;
  959. int64_t pts_buffer[MAX_REORDER_DELAY+1];
  960. for (i = 0; i<MAX_REORDER_DELAY+1; i++)
  961. pts_buffer[i] = AV_NOPTS_VALUE;
  962. for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
  963. if (pkt_buffer->pkt.stream_index != stream_index)
  964. continue;
  965. if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  966. pts_buffer[0] = pkt_buffer->pkt.pts;
  967. for (i = 0; i<delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
  968. FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
  969. pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
  970. }
  971. }
  972. }
  973. static void update_initial_timestamps(AVFormatContext *s, int stream_index,
  974. int64_t dts, int64_t pts, AVPacket *pkt)
  975. {
  976. AVStream *st = s->streams[stream_index];
  977. PacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
  978. PacketList *pktl_it;
  979. uint64_t shift;
  980. if (st->first_dts != AV_NOPTS_VALUE ||
  981. dts == AV_NOPTS_VALUE ||
  982. st->cur_dts == AV_NOPTS_VALUE ||
  983. st->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
  984. dts < INT_MIN + (st->cur_dts - RELATIVE_TS_BASE) ||
  985. is_relative(dts))
  986. return;
  987. st->first_dts = dts - (st->cur_dts - RELATIVE_TS_BASE);
  988. st->cur_dts = dts;
  989. shift = (uint64_t)st->first_dts - RELATIVE_TS_BASE;
  990. if (is_relative(pts))
  991. pts += shift;
  992. for (pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
  993. if (pktl_it->pkt.stream_index != stream_index)
  994. continue;
  995. if (is_relative(pktl_it->pkt.pts))
  996. pktl_it->pkt.pts += shift;
  997. if (is_relative(pktl_it->pkt.dts))
  998. pktl_it->pkt.dts += shift;
  999. if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
  1000. st->start_time = pktl_it->pkt.pts;
  1001. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
  1002. st->start_time = av_sat_add64(st->start_time, av_rescale_q(st->internal->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
  1003. }
  1004. }
  1005. if (has_decode_delay_been_guessed(st)) {
  1006. update_dts_from_pts(s, stream_index, pktl);
  1007. }
  1008. if (st->start_time == AV_NOPTS_VALUE) {
  1009. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
  1010. st->start_time = pts;
  1011. }
  1012. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
  1013. st->start_time = av_sat_add64(st->start_time, av_rescale_q(st->internal->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
  1014. }
  1015. }
  1016. static void update_initial_durations(AVFormatContext *s, AVStream *st,
  1017. int stream_index, int64_t duration)
  1018. {
  1019. PacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
  1020. int64_t cur_dts = RELATIVE_TS_BASE;
  1021. if (st->first_dts != AV_NOPTS_VALUE) {
  1022. if (st->internal->update_initial_durations_done)
  1023. return;
  1024. st->internal->update_initial_durations_done = 1;
  1025. cur_dts = st->first_dts;
  1026. for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
  1027. if (pktl->pkt.stream_index == stream_index) {
  1028. if (pktl->pkt.pts != pktl->pkt.dts ||
  1029. pktl->pkt.dts != AV_NOPTS_VALUE ||
  1030. pktl->pkt.duration)
  1031. break;
  1032. cur_dts -= duration;
  1033. }
  1034. }
  1035. if (pktl && pktl->pkt.dts != st->first_dts) {
  1036. av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
  1037. av_ts2str(st->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
  1038. return;
  1039. }
  1040. if (!pktl) {
  1041. av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(st->first_dts));
  1042. return;
  1043. }
  1044. pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
  1045. st->first_dts = cur_dts;
  1046. } else if (st->cur_dts != RELATIVE_TS_BASE)
  1047. return;
  1048. for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
  1049. if (pktl->pkt.stream_index != stream_index)
  1050. continue;
  1051. if ((pktl->pkt.pts == pktl->pkt.dts ||
  1052. pktl->pkt.pts == AV_NOPTS_VALUE) &&
  1053. (pktl->pkt.dts == AV_NOPTS_VALUE ||
  1054. pktl->pkt.dts == st->first_dts ||
  1055. pktl->pkt.dts == RELATIVE_TS_BASE) &&
  1056. !pktl->pkt.duration) {
  1057. pktl->pkt.dts = cur_dts;
  1058. if (!st->internal->avctx->has_b_frames)
  1059. pktl->pkt.pts = cur_dts;
  1060. pktl->pkt.duration = duration;
  1061. } else
  1062. break;
  1063. cur_dts = pktl->pkt.dts + pktl->pkt.duration;
  1064. }
  1065. if (!pktl)
  1066. st->cur_dts = cur_dts;
  1067. }
  1068. static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
  1069. AVCodecParserContext *pc, AVPacket *pkt,
  1070. int64_t next_dts, int64_t next_pts)
  1071. {
  1072. int num, den, presentation_delayed, delay, i;
  1073. int64_t offset;
  1074. AVRational duration;
  1075. int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
  1076. st->codecpar->codec_id != AV_CODEC_ID_HEVC;
  1077. if (s->flags & AVFMT_FLAG_NOFILLIN)
  1078. return;
  1079. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
  1080. if (pkt->dts == pkt->pts && st->internal->last_dts_for_order_check != AV_NOPTS_VALUE) {
  1081. if (st->internal->last_dts_for_order_check <= pkt->dts) {
  1082. st->internal->dts_ordered++;
  1083. } else {
  1084. av_log(s, st->internal->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING,
  1085. "DTS %"PRIi64" < %"PRIi64" out of order\n",
  1086. pkt->dts,
  1087. st->internal->last_dts_for_order_check);
  1088. st->internal->dts_misordered++;
  1089. }
  1090. if (st->internal->dts_ordered + st->internal->dts_misordered > 250) {
  1091. st->internal->dts_ordered >>= 1;
  1092. st->internal->dts_misordered >>= 1;
  1093. }
  1094. }
  1095. st->internal->last_dts_for_order_check = pkt->dts;
  1096. if (st->internal->dts_ordered < 8*st->internal->dts_misordered && pkt->dts == pkt->pts)
  1097. pkt->dts = AV_NOPTS_VALUE;
  1098. }
  1099. if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
  1100. pkt->dts = AV_NOPTS_VALUE;
  1101. if (pc && pc->pict_type == AV_PICTURE_TYPE_B
  1102. && !st->internal->avctx->has_b_frames)
  1103. //FIXME Set low_delay = 0 when has_b_frames = 1
  1104. st->internal->avctx->has_b_frames = 1;
  1105. /* do we have a video B-frame ? */
  1106. delay = st->internal->avctx->has_b_frames;
  1107. presentation_delayed = 0;
  1108. /* XXX: need has_b_frame, but cannot get it if the codec is
  1109. * not initialized */
  1110. if (delay &&
  1111. pc && pc->pict_type != AV_PICTURE_TYPE_B)
  1112. presentation_delayed = 1;
  1113. if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
  1114. st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << (st->pts_wrap_bits - 1)) &&
  1115. pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
  1116. if (is_relative(st->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > st->cur_dts) {
  1117. pkt->dts -= 1LL << st->pts_wrap_bits;
  1118. } else
  1119. pkt->pts += 1LL << st->pts_wrap_bits;
  1120. }
  1121. /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
  1122. * We take the conservative approach and discard both.
  1123. * Note: If this is misbehaving for an H.264 file, then possibly
  1124. * presentation_delayed is not set correctly. */
  1125. if (delay == 1 && pkt->dts == pkt->pts &&
  1126. pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
  1127. av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
  1128. if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
  1129. && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
  1130. pkt->dts = AV_NOPTS_VALUE;
  1131. }
  1132. duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
  1133. if (pkt->duration <= 0) {
  1134. ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
  1135. if (den && num) {
  1136. duration = (AVRational) {num, den};
  1137. pkt->duration = av_rescale_rnd(1,
  1138. num * (int64_t) st->time_base.den,
  1139. den * (int64_t) st->time_base.num,
  1140. AV_ROUND_DOWN);
  1141. }
  1142. }
  1143. if (pkt->duration > 0 && (s->internal->packet_buffer || s->internal->parse_queue))
  1144. update_initial_durations(s, st, pkt->stream_index, pkt->duration);
  1145. /* Correct timestamps with byte offset if demuxers only have timestamps
  1146. * on packet boundaries */
  1147. if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
  1148. /* this will estimate bitrate based on this frame's duration and size */
  1149. offset = av_rescale(pc->offset, pkt->duration, pkt->size);
  1150. if (pkt->pts != AV_NOPTS_VALUE)
  1151. pkt->pts += offset;
  1152. if (pkt->dts != AV_NOPTS_VALUE)
  1153. pkt->dts += offset;
  1154. }
  1155. /* This may be redundant, but it should not hurt. */
  1156. if (pkt->dts != AV_NOPTS_VALUE &&
  1157. pkt->pts != AV_NOPTS_VALUE &&
  1158. pkt->pts > pkt->dts)
  1159. presentation_delayed = 1;
  1160. if (s->debug & FF_FDEBUG_TS)
  1161. av_log(s, AV_LOG_DEBUG,
  1162. "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
  1163. presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts),
  1164. pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
  1165. /* Interpolate PTS and DTS if they are not present. We skip H264
  1166. * currently because delay and has_b_frames are not reliably set. */
  1167. if ((delay == 0 || (delay == 1 && pc)) &&
  1168. onein_oneout) {
  1169. if (presentation_delayed) {
  1170. /* DTS = decompression timestamp */
  1171. /* PTS = presentation timestamp */
  1172. if (pkt->dts == AV_NOPTS_VALUE)
  1173. pkt->dts = st->last_IP_pts;
  1174. update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
  1175. if (pkt->dts == AV_NOPTS_VALUE)
  1176. pkt->dts = st->cur_dts;
  1177. /* This is tricky: the dts must be incremented by the duration
  1178. * of the frame we are displaying, i.e. the last I- or P-frame. */
  1179. if (st->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
  1180. st->last_IP_duration = pkt->duration;
  1181. if (pkt->dts != AV_NOPTS_VALUE)
  1182. st->cur_dts = av_sat_add64(pkt->dts, st->last_IP_duration);
  1183. if (pkt->dts != AV_NOPTS_VALUE &&
  1184. pkt->pts == AV_NOPTS_VALUE &&
  1185. st->last_IP_duration > 0 &&
  1186. ((uint64_t)st->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
  1187. next_dts != next_pts &&
  1188. next_pts != AV_NOPTS_VALUE)
  1189. pkt->pts = next_dts;
  1190. if ((uint64_t)pkt->duration <= INT32_MAX)
  1191. st->last_IP_duration = pkt->duration;
  1192. st->last_IP_pts = pkt->pts;
  1193. /* Cannot compute PTS if not present (we can compute it only
  1194. * by knowing the future. */
  1195. } else if (pkt->pts != AV_NOPTS_VALUE ||
  1196. pkt->dts != AV_NOPTS_VALUE ||
  1197. pkt->duration > 0 ) {
  1198. /* presentation is not delayed : PTS and DTS are the same */
  1199. if (pkt->pts == AV_NOPTS_VALUE)
  1200. pkt->pts = pkt->dts;
  1201. update_initial_timestamps(s, pkt->stream_index, pkt->pts,
  1202. pkt->pts, pkt);
  1203. if (pkt->pts == AV_NOPTS_VALUE)
  1204. pkt->pts = st->cur_dts;
  1205. pkt->dts = pkt->pts;
  1206. if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
  1207. st->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
  1208. }
  1209. }
  1210. if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  1211. st->internal->pts_buffer[0] = pkt->pts;
  1212. for (i = 0; i<delay && st->internal->pts_buffer[i] > st->internal->pts_buffer[i + 1]; i++)
  1213. FFSWAP(int64_t, st->internal->pts_buffer[i], st->internal->pts_buffer[i + 1]);
  1214. if(has_decode_delay_been_guessed(st))
  1215. pkt->dts = select_from_pts_buffer(st, st->internal->pts_buffer, pkt->dts);
  1216. }
  1217. // We skipped it above so we try here.
  1218. if (!onein_oneout)
  1219. // This should happen on the first packet
  1220. update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
  1221. if (pkt->dts > st->cur_dts)
  1222. st->cur_dts = pkt->dts;
  1223. if (s->debug & FF_FDEBUG_TS)
  1224. av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
  1225. presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), st->index, st->id);
  1226. /* update flags */
  1227. if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
  1228. pkt->flags |= AV_PKT_FLAG_KEY;
  1229. #if FF_API_CONVERGENCE_DURATION
  1230. FF_DISABLE_DEPRECATION_WARNINGS
  1231. if (pc)
  1232. pkt->convergence_duration = pc->convergence_duration;
  1233. FF_ENABLE_DEPRECATION_WARNINGS
  1234. #endif
  1235. }
  1236. /**
  1237. * Parse a packet, add all split parts to parse_queue.
  1238. *
  1239. * @param pkt Packet to parse; must not be NULL.
  1240. * @param flush Indicates whether to flush. If set, pkt must be blank.
  1241. */
  1242. static int parse_packet(AVFormatContext *s, AVPacket *pkt,
  1243. int stream_index, int flush)
  1244. {
  1245. AVPacket *out_pkt = s->internal->parse_pkt;
  1246. AVStream *st = s->streams[stream_index];
  1247. uint8_t *data = pkt->data;
  1248. int size = pkt->size;
  1249. int ret = 0, got_output = flush;
  1250. if (size || flush) {
  1251. av_packet_unref(out_pkt);
  1252. } else if (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  1253. // preserve 0-size sync packets
  1254. compute_pkt_fields(s, st, st->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
  1255. }
  1256. while (size > 0 || (flush && got_output)) {
  1257. int len;
  1258. int64_t next_pts = pkt->pts;
  1259. int64_t next_dts = pkt->dts;
  1260. len = av_parser_parse2(st->parser, st->internal->avctx,
  1261. &out_pkt->data, &out_pkt->size, data, size,
  1262. pkt->pts, pkt->dts, pkt->pos);
  1263. pkt->pts = pkt->dts = AV_NOPTS_VALUE;
  1264. pkt->pos = -1;
  1265. /* increment read pointer */
  1266. av_assert1(data || !len);
  1267. data = len ? data + len : data;
  1268. size -= len;
  1269. got_output = !!out_pkt->size;
  1270. if (!out_pkt->size)
  1271. continue;
  1272. if (pkt->buf && out_pkt->data == pkt->data) {
  1273. /* reference pkt->buf only when out_pkt->data is guaranteed to point
  1274. * to data in it and not in the parser's internal buffer. */
  1275. /* XXX: Ensure this is the case with all parsers when st->parser->flags
  1276. * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
  1277. out_pkt->buf = av_buffer_ref(pkt->buf);
  1278. if (!out_pkt->buf) {
  1279. ret = AVERROR(ENOMEM);
  1280. goto fail;
  1281. }
  1282. } else {
  1283. ret = av_packet_make_refcounted(out_pkt);
  1284. if (ret < 0)
  1285. goto fail;
  1286. }
  1287. if (pkt->side_data) {
  1288. out_pkt->side_data = pkt->side_data;
  1289. out_pkt->side_data_elems = pkt->side_data_elems;
  1290. pkt->side_data = NULL;
  1291. pkt->side_data_elems = 0;
  1292. }
  1293. /* set the duration */
  1294. out_pkt->duration = (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
  1295. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  1296. if (st->internal->avctx->sample_rate > 0) {
  1297. out_pkt->duration =
  1298. av_rescale_q_rnd(st->parser->duration,
  1299. (AVRational) { 1, st->internal->avctx->sample_rate },
  1300. st->time_base,
  1301. AV_ROUND_DOWN);
  1302. }
  1303. }
  1304. out_pkt->stream_index = st->index;
  1305. out_pkt->pts = st->parser->pts;
  1306. out_pkt->dts = st->parser->dts;
  1307. out_pkt->pos = st->parser->pos;
  1308. out_pkt->flags |= pkt->flags & AV_PKT_FLAG_DISCARD;
  1309. if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
  1310. out_pkt->pos = st->parser->frame_offset;
  1311. if (st->parser->key_frame == 1 ||
  1312. (st->parser->key_frame == -1 &&
  1313. st->parser->pict_type == AV_PICTURE_TYPE_I))
  1314. out_pkt->flags |= AV_PKT_FLAG_KEY;
  1315. if (st->parser->key_frame == -1 && st->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
  1316. out_pkt->flags |= AV_PKT_FLAG_KEY;
  1317. compute_pkt_fields(s, st, st->parser, out_pkt, next_dts, next_pts);
  1318. ret = avpriv_packet_list_put(&s->internal->parse_queue,
  1319. &s->internal->parse_queue_end,
  1320. out_pkt, NULL, 0);
  1321. if (ret < 0) {
  1322. av_packet_unref(out_pkt);
  1323. goto fail;
  1324. }
  1325. }
  1326. /* end of the stream => close and free the parser */
  1327. if (flush) {
  1328. av_parser_close(st->parser);
  1329. st->parser = NULL;
  1330. }
  1331. fail:
  1332. av_packet_unref(pkt);
  1333. return ret;
  1334. }
  1335. static int64_t ts_to_samples(AVStream *st, int64_t ts)
  1336. {
  1337. return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
  1338. }
  1339. static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
  1340. {
  1341. int ret, i, got_packet = 0;
  1342. AVDictionary *metadata = NULL;
  1343. while (!got_packet && !s->internal->parse_queue) {
  1344. AVStream *st;
  1345. /* read next packet */
  1346. ret = ff_read_packet(s, pkt);
  1347. if (ret < 0) {
  1348. if (ret == AVERROR(EAGAIN))
  1349. return ret;
  1350. /* flush the parsers */
  1351. for (i = 0; i < s->nb_streams; i++) {
  1352. st = s->streams[i];
  1353. if (st->parser && st->need_parsing)
  1354. parse_packet(s, pkt, st->index, 1);
  1355. }
  1356. /* all remaining packets are now in parse_queue =>
  1357. * really terminate parsing */
  1358. break;
  1359. }
  1360. ret = 0;
  1361. st = s->streams[pkt->stream_index];
  1362. st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
  1363. /* update context if required */
  1364. if (st->internal->need_context_update) {
  1365. if (avcodec_is_open(st->internal->avctx)) {
  1366. av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
  1367. avcodec_close(st->internal->avctx);
  1368. st->internal->info->found_decoder = 0;
  1369. }
  1370. /* close parser, because it depends on the codec */
  1371. if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
  1372. av_parser_close(st->parser);
  1373. st->parser = NULL;
  1374. }
  1375. ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
  1376. if (ret < 0) {
  1377. av_packet_unref(pkt);
  1378. return ret;
  1379. }
  1380. #if FF_API_LAVF_AVCTX
  1381. FF_DISABLE_DEPRECATION_WARNINGS
  1382. /* update deprecated public codec context */
  1383. ret = avcodec_parameters_to_context(st->codec, st->codecpar);
  1384. if (ret < 0) {
  1385. av_packet_unref(pkt);
  1386. return ret;
  1387. }
  1388. FF_ENABLE_DEPRECATION_WARNINGS
  1389. #endif
  1390. st->internal->need_context_update = 0;
  1391. }
  1392. if (pkt->pts != AV_NOPTS_VALUE &&
  1393. pkt->dts != AV_NOPTS_VALUE &&
  1394. pkt->pts < pkt->dts) {
  1395. av_log(s, AV_LOG_WARNING,
  1396. "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
  1397. pkt->stream_index,
  1398. av_ts2str(pkt->pts),
  1399. av_ts2str(pkt->dts),
  1400. pkt->size);
  1401. }
  1402. if (s->debug & FF_FDEBUG_TS)
  1403. av_log(s, AV_LOG_DEBUG,
  1404. "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
  1405. pkt->stream_index,
  1406. av_ts2str(pkt->pts),
  1407. av_ts2str(pkt->dts),
  1408. pkt->size, pkt->duration, pkt->flags);
  1409. if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
  1410. st->parser = av_parser_init(st->codecpar->codec_id);
  1411. if (!st->parser) {
  1412. av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
  1413. "%s, packets or times may be invalid.\n",
  1414. avcodec_get_name(st->codecpar->codec_id));
  1415. /* no parser available: just output the raw packets */
  1416. st->need_parsing = AVSTREAM_PARSE_NONE;
  1417. } else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
  1418. st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
  1419. else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
  1420. st->parser->flags |= PARSER_FLAG_ONCE;
  1421. else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
  1422. st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  1423. }
  1424. if (!st->need_parsing || !st->parser) {
  1425. /* no parsing needed: we just output the packet as is */
  1426. compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
  1427. if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
  1428. (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
  1429. ff_reduce_index(s, st->index);
  1430. av_add_index_entry(st, pkt->pos, pkt->dts,
  1431. 0, 0, AVINDEX_KEYFRAME);
  1432. }
  1433. got_packet = 1;
  1434. } else if (st->discard < AVDISCARD_ALL) {
  1435. if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
  1436. return ret;
  1437. st->codecpar->sample_rate = st->internal->avctx->sample_rate;
  1438. st->codecpar->bit_rate = st->internal->avctx->bit_rate;
  1439. st->codecpar->channels = st->internal->avctx->channels;
  1440. st->codecpar->channel_layout = st->internal->avctx->channel_layout;
  1441. st->codecpar->codec_id = st->internal->avctx->codec_id;
  1442. } else {
  1443. /* free packet */
  1444. av_packet_unref(pkt);
  1445. }
  1446. if (pkt->flags & AV_PKT_FLAG_KEY)
  1447. st->internal->skip_to_keyframe = 0;
  1448. if (st->internal->skip_to_keyframe) {
  1449. av_packet_unref(pkt);
  1450. got_packet = 0;
  1451. }
  1452. }
  1453. if (!got_packet && s->internal->parse_queue)
  1454. ret = avpriv_packet_list_get(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
  1455. if (ret >= 0) {
  1456. AVStream *st = s->streams[pkt->stream_index];
  1457. int discard_padding = 0;
  1458. if (st->internal->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
  1459. int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
  1460. int64_t sample = ts_to_samples(st, pts);
  1461. int duration = ts_to_samples(st, pkt->duration);
  1462. int64_t end_sample = sample + duration;
  1463. if (duration > 0 && end_sample >= st->internal->first_discard_sample &&
  1464. sample < st->internal->last_discard_sample)
  1465. discard_padding = FFMIN(end_sample - st->internal->first_discard_sample, duration);
  1466. }
  1467. if (st->internal->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
  1468. st->internal->skip_samples = st->internal->start_skip_samples;
  1469. if (st->internal->skip_samples || discard_padding) {
  1470. uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
  1471. if (p) {
  1472. AV_WL32(p, st->internal->skip_samples);
  1473. AV_WL32(p + 4, discard_padding);
  1474. av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->internal->skip_samples, discard_padding);
  1475. }
  1476. st->internal->skip_samples = 0;
  1477. }
  1478. if (st->internal->inject_global_side_data) {
  1479. for (i = 0; i < st->nb_side_data; i++) {
  1480. AVPacketSideData *src_sd = &st->side_data[i];
  1481. uint8_t *dst_data;
  1482. if (av_packet_get_side_data(pkt, src_sd->type, NULL))
  1483. continue;
  1484. dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
  1485. if (!dst_data) {
  1486. av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
  1487. continue;
  1488. }
  1489. memcpy(dst_data, src_sd->data, src_sd->size);
  1490. }
  1491. st->internal->inject_global_side_data = 0;
  1492. }
  1493. }
  1494. av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
  1495. if (metadata) {
  1496. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  1497. av_dict_copy(&s->metadata, metadata, 0);
  1498. av_dict_free(&metadata);
  1499. av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
  1500. }
  1501. #if FF_API_LAVF_AVCTX
  1502. update_stream_avctx(s);
  1503. #endif
  1504. if (s->debug & FF_FDEBUG_TS)
  1505. av_log(s, AV_LOG_DEBUG,
  1506. "read_frame_internal stream=%d, pts=%s, dts=%s, "
  1507. "size=%d, duration=%"PRId64", flags=%d\n",
  1508. pkt->stream_index,
  1509. av_ts2str(pkt->pts),
  1510. av_ts2str(pkt->dts),
  1511. pkt->size, pkt->duration, pkt->flags);
  1512. /* A demuxer might have returned EOF because of an IO error, let's
  1513. * propagate this back to the user. */
  1514. if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
  1515. ret = s->pb->error;
  1516. return ret;
  1517. }
  1518. int av_read_frame(AVFormatContext *s, AVPacket *pkt)
  1519. {
  1520. const int genpts = s->flags & AVFMT_FLAG_GENPTS;
  1521. int eof = 0;
  1522. int ret;
  1523. AVStream *st;
  1524. if (!genpts) {
  1525. ret = s->internal->packet_buffer
  1526. ? avpriv_packet_list_get(&s->internal->packet_buffer,
  1527. &s->internal->packet_buffer_end, pkt)
  1528. : read_frame_internal(s, pkt);
  1529. if (ret < 0)
  1530. return ret;
  1531. goto return_packet;
  1532. }
  1533. for (;;) {
  1534. PacketList *pktl = s->internal->packet_buffer;
  1535. if (pktl) {
  1536. AVPacket *next_pkt = &pktl->pkt;
  1537. if (next_pkt->dts != AV_NOPTS_VALUE) {
  1538. int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
  1539. // last dts seen for this stream. if any of packets following
  1540. // current one had no dts, we will set this to AV_NOPTS_VALUE.
  1541. int64_t last_dts = next_pkt->dts;
  1542. av_assert2(wrap_bits <= 64);
  1543. while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
  1544. if (pktl->pkt.stream_index == next_pkt->stream_index &&
  1545. av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
  1546. if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
  1547. // not B-frame
  1548. next_pkt->pts = pktl->pkt.dts;
  1549. }
  1550. if (last_dts != AV_NOPTS_VALUE) {
  1551. // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
  1552. last_dts = pktl->pkt.dts;
  1553. }
  1554. }
  1555. pktl = pktl->next;
  1556. }
  1557. if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
  1558. // Fixing the last reference frame had none pts issue (For MXF etc).
  1559. // We only do this when
  1560. // 1. eof.
  1561. // 2. we are not able to resolve a pts value for current packet.
  1562. // 3. the packets for this stream at the end of the files had valid dts.
  1563. next_pkt->pts = last_dts + next_pkt->duration;
  1564. }
  1565. pktl = s->internal->packet_buffer;
  1566. }
  1567. /* read packet from packet buffer, if there is data */
  1568. st = s->streams[next_pkt->stream_index];
  1569. if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
  1570. next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
  1571. ret = avpriv_packet_list_get(&s->internal->packet_buffer,
  1572. &s->internal->packet_buffer_end, pkt);
  1573. goto return_packet;
  1574. }
  1575. }
  1576. ret = read_frame_internal(s, pkt);
  1577. if (ret < 0) {
  1578. if (pktl && ret != AVERROR(EAGAIN)) {
  1579. eof = 1;
  1580. continue;
  1581. } else
  1582. return ret;
  1583. }
  1584. ret = avpriv_packet_list_put(&s->internal->packet_buffer,
  1585. &s->internal->packet_buffer_end,
  1586. pkt, NULL, 0);
  1587. if (ret < 0) {
  1588. av_packet_unref(pkt);
  1589. return ret;
  1590. }
  1591. }
  1592. return_packet:
  1593. st = s->streams[pkt->stream_index];
  1594. if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
  1595. ff_reduce_index(s, st->index);
  1596. av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
  1597. }
  1598. if (is_relative(pkt->dts))
  1599. pkt->dts -= RELATIVE_TS_BASE;
  1600. if (is_relative(pkt->pts))
  1601. pkt->pts -= RELATIVE_TS_BASE;
  1602. return ret;
  1603. }
  1604. /* XXX: suppress the packet queue */
  1605. static void flush_packet_queue(AVFormatContext *s)
  1606. {
  1607. if (!s->internal)
  1608. return;
  1609. avpriv_packet_list_free(&s->internal->parse_queue, &s->internal->parse_queue_end);
  1610. avpriv_packet_list_free(&s->internal->packet_buffer, &s->internal->packet_buffer_end);
  1611. avpriv_packet_list_free(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
  1612. s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
  1613. }
  1614. /*******************************************************/
  1615. /* seek support */
  1616. int av_find_default_stream_index(AVFormatContext *s)
  1617. {
  1618. int i;
  1619. AVStream *st;
  1620. int best_stream = 0;
  1621. int best_score = INT_MIN;
  1622. if (s->nb_streams <= 0)
  1623. return -1;
  1624. for (i = 0; i < s->nb_streams; i++) {
  1625. int score = 0;
  1626. st = s->streams[i];
  1627. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  1628. if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
  1629. score -= 400;
  1630. if (st->codecpar->width && st->codecpar->height)
  1631. score += 50;
  1632. score+= 25;
  1633. }
  1634. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  1635. if (st->codecpar->sample_rate)
  1636. score += 50;
  1637. }
  1638. if (st->codec_info_nb_frames)
  1639. score += 12;
  1640. if (st->discard != AVDISCARD_ALL)
  1641. score += 200;
  1642. if (score > best_score) {
  1643. best_score = score;
  1644. best_stream = i;
  1645. }
  1646. }
  1647. return best_stream;
  1648. }
  1649. /** Flush the frame reader. */
  1650. void ff_read_frame_flush(AVFormatContext *s)
  1651. {
  1652. AVStream *st;
  1653. int i, j;
  1654. flush_packet_queue(s);
  1655. /* Reset read state for each stream. */
  1656. for (i = 0; i < s->nb_streams; i++) {
  1657. st = s->streams[i];
  1658. if (st->parser) {
  1659. av_parser_close(st->parser);
  1660. st->parser = NULL;
  1661. }
  1662. st->last_IP_pts = AV_NOPTS_VALUE;
  1663. st->internal->last_dts_for_order_check = AV_NOPTS_VALUE;
  1664. if (st->first_dts == AV_NOPTS_VALUE)
  1665. st->cur_dts = RELATIVE_TS_BASE;
  1666. else
  1667. /* We set the current DTS to an unspecified origin. */
  1668. st->cur_dts = AV_NOPTS_VALUE;
  1669. st->probe_packets = s->max_probe_packets;
  1670. for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
  1671. st->internal->pts_buffer[j] = AV_NOPTS_VALUE;
  1672. if (s->internal->inject_global_side_data)
  1673. st->internal->inject_global_side_data = 1;
  1674. st->internal->skip_samples = 0;
  1675. }
  1676. }
  1677. void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
  1678. {
  1679. int i;
  1680. for (i = 0; i < s->nb_streams; i++) {
  1681. AVStream *st = s->streams[i];
  1682. st->cur_dts =
  1683. av_rescale(timestamp,
  1684. st->time_base.den * (int64_t) ref_st->time_base.num,
  1685. st->time_base.num * (int64_t) ref_st->time_base.den);
  1686. }
  1687. }
  1688. void ff_reduce_index(AVFormatContext *s, int stream_index)
  1689. {
  1690. AVStream *st = s->streams[stream_index];
  1691. unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
  1692. if ((unsigned) st->internal->nb_index_entries >= max_entries) {
  1693. int i;
  1694. for (i = 0; 2 * i < st->internal->nb_index_entries; i++)
  1695. st->internal->index_entries[i] = st->internal->index_entries[2 * i];
  1696. st->internal->nb_index_entries = i;
  1697. }
  1698. }
  1699. int ff_add_index_entry(AVIndexEntry **index_entries,
  1700. int *nb_index_entries,
  1701. unsigned int *index_entries_allocated_size,
  1702. int64_t pos, int64_t timestamp,
  1703. int size, int distance, int flags)
  1704. {
  1705. AVIndexEntry *entries, *ie;
  1706. int index;
  1707. if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
  1708. return -1;
  1709. if (timestamp == AV_NOPTS_VALUE)
  1710. return AVERROR(EINVAL);
  1711. if (size < 0 || size > 0x3FFFFFFF)
  1712. return AVERROR(EINVAL);
  1713. if (is_relative(timestamp)) //FIXME this maintains previous behavior but we should shift by the correct offset once known
  1714. timestamp -= RELATIVE_TS_BASE;
  1715. entries = av_fast_realloc(*index_entries,
  1716. index_entries_allocated_size,
  1717. (*nb_index_entries + 1) *
  1718. sizeof(AVIndexEntry));
  1719. if (!entries)
  1720. return -1;
  1721. *index_entries = entries;
  1722. index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
  1723. timestamp, AVSEEK_FLAG_ANY);
  1724. if (index < 0) {
  1725. index = (*nb_index_entries)++;
  1726. ie = &entries[index];
  1727. av_assert0(index == 0 || ie[-1].timestamp < timestamp);
  1728. } else {
  1729. ie = &entries[index];
  1730. if (ie->timestamp != timestamp) {
  1731. if (ie->timestamp <= timestamp)
  1732. return -1;
  1733. memmove(entries + index + 1, entries + index,
  1734. sizeof(AVIndexEntry) * (*nb_index_entries - index));
  1735. (*nb_index_entries)++;
  1736. } else if (ie->pos == pos && distance < ie->min_distance)
  1737. // do not reduce the distance
  1738. distance = ie->min_distance;
  1739. }
  1740. ie->pos = pos;
  1741. ie->timestamp = timestamp;
  1742. ie->min_distance = distance;
  1743. ie->size = size;
  1744. ie->flags = flags;
  1745. return index;
  1746. }
  1747. int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
  1748. int size, int distance, int flags)
  1749. {
  1750. timestamp = wrap_timestamp(st, timestamp);
  1751. return ff_add_index_entry(&st->internal->index_entries, &st->internal->nb_index_entries,
  1752. &st->internal->index_entries_allocated_size, pos,
  1753. timestamp, size, distance, flags);
  1754. }
  1755. int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
  1756. int64_t wanted_timestamp, int flags)
  1757. {
  1758. int a, b, m;
  1759. int64_t timestamp;
  1760. a = -1;
  1761. b = nb_entries;
  1762. // Optimize appending index entries at the end.
  1763. if (b && entries[b - 1].timestamp < wanted_timestamp)
  1764. a = b - 1;
  1765. while (b - a > 1) {
  1766. m = (a + b) >> 1;
  1767. // Search for the next non-discarded packet.
  1768. while ((entries[m].flags & AVINDEX_DISCARD_FRAME) && m < b && m < nb_entries - 1) {
  1769. m++;
  1770. if (m == b && entries[m].timestamp >= wanted_timestamp) {
  1771. m = b - 1;
  1772. break;
  1773. }
  1774. }
  1775. timestamp = entries[m].timestamp;
  1776. if (timestamp >= wanted_timestamp)
  1777. b = m;
  1778. if (timestamp <= wanted_timestamp)
  1779. a = m;
  1780. }
  1781. m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
  1782. if (!(flags & AVSEEK_FLAG_ANY))
  1783. while (m >= 0 && m < nb_entries &&
  1784. !(entries[m].flags & AVINDEX_KEYFRAME))
  1785. m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
  1786. if (m == nb_entries)
  1787. return -1;
  1788. return m;
  1789. }
  1790. void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
  1791. {
  1792. int ist1, ist2;
  1793. int64_t pos_delta = 0;
  1794. int64_t skip = 0;
  1795. //We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
  1796. const char *proto = avio_find_protocol_name(s->url);
  1797. av_assert0(time_tolerance >= 0);
  1798. if (!proto) {
  1799. av_log(s, AV_LOG_INFO,
  1800. "Protocol name not provided, cannot determine if input is local or "
  1801. "a network protocol, buffers and access patterns cannot be configured "
  1802. "optimally without knowing the protocol\n");
  1803. }
  1804. if (proto && !(strcmp(proto, "file") && strcmp(proto, "pipe") && strcmp(proto, "cache")))
  1805. return;
  1806. for (ist1 = 0; ist1 < s->nb_streams; ist1++) {
  1807. AVStream *st1 = s->streams[ist1];
  1808. for (ist2 = 0; ist2 < s->nb_streams; ist2++) {
  1809. AVStream *st2 = s->streams[ist2];
  1810. int i1, i2;
  1811. if (ist1 == ist2)
  1812. continue;
  1813. for (i1 = i2 = 0; i1 < st1->internal->nb_index_entries; i1++) {
  1814. AVIndexEntry *e1 = &st1->internal->index_entries[i1];
  1815. int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q);
  1816. skip = FFMAX(skip, e1->size);
  1817. for (; i2 < st2->internal->nb_index_entries; i2++) {
  1818. AVIndexEntry *e2 = &st2->internal->index_entries[i2];
  1819. int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q);
  1820. if (e2_pts < e1_pts || e2_pts - (uint64_t)e1_pts < time_tolerance)
  1821. continue;
  1822. pos_delta = FFMAX(pos_delta, e1->pos - e2->pos);
  1823. break;
  1824. }
  1825. }
  1826. }
  1827. }
  1828. pos_delta *= 2;
  1829. /* XXX This could be adjusted depending on protocol*/
  1830. if (s->pb->buffer_size < pos_delta && pos_delta < (1<<24)) {
  1831. av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta);
  1832. /* realloc the buffer and the original data will be retained */
  1833. if (ffio_realloc_buf(s->pb, pos_delta)) {
  1834. av_log(s, AV_LOG_ERROR, "Realloc buffer fail.\n");
  1835. return;
  1836. }
  1837. s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, pos_delta/2);
  1838. }
  1839. if (skip < (1<<23)) {
  1840. s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, skip);
  1841. }
  1842. }
  1843. int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
  1844. {
  1845. return ff_index_search_timestamp(st->internal->index_entries, st->internal->nb_index_entries,
  1846. wanted_timestamp, flags);
  1847. }
  1848. static int64_t ff_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit,
  1849. int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
  1850. {
  1851. int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
  1852. if (stream_index >= 0)
  1853. ts = wrap_timestamp(s->streams[stream_index], ts);
  1854. return ts;
  1855. }
  1856. int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
  1857. int64_t target_ts, int flags)
  1858. {
  1859. const AVInputFormat *avif = s->iformat;
  1860. int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
  1861. int64_t ts_min, ts_max, ts;
  1862. int index;
  1863. int64_t ret;
  1864. AVStream *st;
  1865. if (stream_index < 0)
  1866. return -1;
  1867. av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
  1868. ts_max =
  1869. ts_min = AV_NOPTS_VALUE;
  1870. pos_limit = -1; // GCC falsely says it may be uninitialized.
  1871. st = s->streams[stream_index];
  1872. if (st->internal->index_entries) {
  1873. AVIndexEntry *e;
  1874. /* FIXME: Whole function must be checked for non-keyframe entries in
  1875. * index case, especially read_timestamp(). */
  1876. index = av_index_search_timestamp(st, target_ts,
  1877. flags | AVSEEK_FLAG_BACKWARD);
  1878. index = FFMAX(index, 0);
  1879. e = &st->internal->index_entries[index];
  1880. if (e->timestamp <= target_ts || e->pos == e->min_distance) {
  1881. pos_min = e->pos;
  1882. ts_min = e->timestamp;
  1883. av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%s\n",
  1884. pos_min, av_ts2str(ts_min));
  1885. } else {
  1886. av_assert1(index == 0);
  1887. }
  1888. index = av_index_search_timestamp(st, target_ts,
  1889. flags & ~AVSEEK_FLAG_BACKWARD);
  1890. av_assert0(index < st->internal->nb_index_entries);
  1891. if (index >= 0) {
  1892. e = &st->internal->index_entries[index];
  1893. av_assert1(e->timestamp >= target_ts);
  1894. pos_max = e->pos;
  1895. ts_max = e->timestamp;
  1896. pos_limit = pos_max - e->min_distance;
  1897. av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
  1898. " dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
  1899. }
  1900. }
  1901. pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
  1902. ts_min, ts_max, flags, &ts, avif->read_timestamp);
  1903. if (pos < 0)
  1904. return -1;
  1905. /* do the seek */
  1906. if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
  1907. return ret;
  1908. ff_read_frame_flush(s);
  1909. ff_update_cur_dts(s, st, ts);
  1910. return 0;
  1911. }
  1912. int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
  1913. int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
  1914. {
  1915. int64_t step = 1024;
  1916. int64_t limit, ts_max;
  1917. int64_t filesize = avio_size(s->pb);
  1918. int64_t pos_max = filesize - 1;
  1919. do {
  1920. limit = pos_max;
  1921. pos_max = FFMAX(0, (pos_max) - step);
  1922. ts_max = ff_read_timestamp(s, stream_index,
  1923. &pos_max, limit, read_timestamp);
  1924. step += step;
  1925. } while (ts_max == AV_NOPTS_VALUE && 2*limit > step);
  1926. if (ts_max == AV_NOPTS_VALUE)
  1927. return -1;
  1928. for (;;) {
  1929. int64_t tmp_pos = pos_max + 1;
  1930. int64_t tmp_ts = ff_read_timestamp(s, stream_index,
  1931. &tmp_pos, INT64_MAX, read_timestamp);
  1932. if (tmp_ts == AV_NOPTS_VALUE)
  1933. break;
  1934. av_assert0(tmp_pos > pos_max);
  1935. ts_max = tmp_ts;
  1936. pos_max = tmp_pos;
  1937. if (tmp_pos >= filesize)
  1938. break;
  1939. }
  1940. if (ts)
  1941. *ts = ts_max;
  1942. if (pos)
  1943. *pos = pos_max;
  1944. return 0;
  1945. }
  1946. int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
  1947. int64_t pos_min, int64_t pos_max, int64_t pos_limit,
  1948. int64_t ts_min, int64_t ts_max,
  1949. int flags, int64_t *ts_ret,
  1950. int64_t (*read_timestamp)(struct AVFormatContext *, int,
  1951. int64_t *, int64_t))
  1952. {
  1953. int64_t pos, ts;
  1954. int64_t start_pos;
  1955. int no_change;
  1956. int ret;
  1957. av_log(s, AV_LOG_TRACE, "gen_seek: %d %s\n", stream_index, av_ts2str(target_ts));
  1958. if (ts_min == AV_NOPTS_VALUE) {
  1959. pos_min = s->internal->data_offset;
  1960. ts_min = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
  1961. if (ts_min == AV_NOPTS_VALUE)
  1962. return -1;
  1963. }
  1964. if (ts_min >= target_ts) {
  1965. *ts_ret = ts_min;
  1966. return pos_min;
  1967. }
  1968. if (ts_max == AV_NOPTS_VALUE) {
  1969. if ((ret = ff_find_last_ts(s, stream_index, &ts_max, &pos_max, read_timestamp)) < 0)
  1970. return ret;
  1971. pos_limit = pos_max;
  1972. }
  1973. if (ts_max <= target_ts) {
  1974. *ts_ret = ts_max;
  1975. return pos_max;
  1976. }
  1977. av_assert0(ts_min < ts_max);
  1978. no_change = 0;
  1979. while (pos_min < pos_limit) {
  1980. av_log(s, AV_LOG_TRACE,
  1981. "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%s dts_max=%s\n",
  1982. pos_min, pos_max, av_ts2str(ts_min), av_ts2str(ts_max));
  1983. av_assert0(pos_limit <= pos_max);
  1984. if (no_change == 0) {
  1985. int64_t approximate_keyframe_distance = pos_max - pos_limit;
  1986. // interpolate position (better than dichotomy)
  1987. pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
  1988. ts_max - ts_min) +
  1989. pos_min - approximate_keyframe_distance;
  1990. } else if (no_change == 1) {
  1991. // bisection if interpolation did not change min / max pos last time
  1992. pos = (pos_min + pos_limit) >> 1;
  1993. } else {
  1994. /* linear search if bisection failed, can only happen if there
  1995. * are very few or no keyframes between min/max */
  1996. pos = pos_min;
  1997. }
  1998. if (pos <= pos_min)
  1999. pos = pos_min + 1;
  2000. else if (pos > pos_limit)
  2001. pos = pos_limit;
  2002. start_pos = pos;
  2003. // May pass pos_limit instead of -1.
  2004. ts = ff_read_timestamp(s, stream_index, &pos, INT64_MAX, read_timestamp);
  2005. if (pos == pos_max)
  2006. no_change++;
  2007. else
  2008. no_change = 0;
  2009. av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %s %s %s"
  2010. " target:%s limit:%"PRId64" start:%"PRId64" noc:%d\n",
  2011. pos_min, pos, pos_max,
  2012. av_ts2str(ts_min), av_ts2str(ts), av_ts2str(ts_max), av_ts2str(target_ts),
  2013. pos_limit, start_pos, no_change);
  2014. if (ts == AV_NOPTS_VALUE) {
  2015. av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
  2016. return -1;
  2017. }
  2018. if (target_ts <= ts) {
  2019. pos_limit = start_pos - 1;
  2020. pos_max = pos;
  2021. ts_max = ts;
  2022. }
  2023. if (target_ts >= ts) {
  2024. pos_min = pos;
  2025. ts_min = ts;
  2026. }
  2027. }
  2028. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  2029. ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
  2030. #if 0
  2031. pos_min = pos;
  2032. ts_min = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
  2033. pos_min++;
  2034. ts_max = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
  2035. av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %s<=%s<=%s\n",
  2036. pos, av_ts2str(ts_min), av_ts2str(target_ts), av_ts2str(ts_max));
  2037. #endif
  2038. *ts_ret = ts;
  2039. return pos;
  2040. }
  2041. static int seek_frame_byte(AVFormatContext *s, int stream_index,
  2042. int64_t pos, int flags)
  2043. {
  2044. int64_t pos_min, pos_max;
  2045. pos_min = s->internal->data_offset;
  2046. pos_max = avio_size(s->pb) - 1;
  2047. if (pos < pos_min)
  2048. pos = pos_min;
  2049. else if (pos > pos_max)
  2050. pos = pos_max;
  2051. avio_seek(s->pb, pos, SEEK_SET);
  2052. s->io_repositioned = 1;
  2053. return 0;
  2054. }
  2055. static int seek_frame_generic(AVFormatContext *s, int stream_index,
  2056. int64_t timestamp, int flags)
  2057. {
  2058. int index;
  2059. int64_t ret;
  2060. AVStream *st;
  2061. AVIndexEntry *ie;
  2062. st = s->streams[stream_index];
  2063. index = av_index_search_timestamp(st, timestamp, flags);
  2064. if (index < 0 && st->internal->nb_index_entries &&
  2065. timestamp < st->internal->index_entries[0].timestamp)
  2066. return -1;
  2067. if (index < 0 || index == st->internal->nb_index_entries - 1) {
  2068. AVPacket *pkt = s->internal->pkt;
  2069. int nonkey = 0;
  2070. if (st->internal->nb_index_entries) {
  2071. av_assert0(st->internal->index_entries);
  2072. ie = &st->internal->index_entries[st->internal->nb_index_entries - 1];
  2073. if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
  2074. return ret;
  2075. ff_update_cur_dts(s, st, ie->timestamp);
  2076. } else {
  2077. if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
  2078. return ret;
  2079. }
  2080. av_packet_unref(pkt);
  2081. for (;;) {
  2082. int read_status;
  2083. do {
  2084. read_status = av_read_frame(s, pkt);
  2085. } while (read_status == AVERROR(EAGAIN));
  2086. if (read_status < 0)
  2087. break;
  2088. if (stream_index == pkt->stream_index && pkt->dts > timestamp) {
  2089. if (pkt->flags & AV_PKT_FLAG_KEY) {
  2090. av_packet_unref(pkt);
  2091. break;
  2092. }
  2093. if (nonkey++ > 1000 && st->codecpar->codec_id != AV_CODEC_ID_CDGRAPHICS) {
  2094. av_log(s, AV_LOG_ERROR,"seek_frame_generic failed as this stream seems to contain no keyframes after the target timestamp, %d non keyframes found\n", nonkey);
  2095. av_packet_unref(pkt);
  2096. break;
  2097. }
  2098. }
  2099. av_packet_unref(pkt);
  2100. }
  2101. index = av_index_search_timestamp(st, timestamp, flags);
  2102. }
  2103. if (index < 0)
  2104. return -1;
  2105. ff_read_frame_flush(s);
  2106. if (s->iformat->read_seek)
  2107. if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
  2108. return 0;
  2109. ie = &st->internal->index_entries[index];
  2110. if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
  2111. return ret;
  2112. ff_update_cur_dts(s, st, ie->timestamp);
  2113. return 0;
  2114. }
  2115. static int seek_frame_internal(AVFormatContext *s, int stream_index,
  2116. int64_t timestamp, int flags)
  2117. {
  2118. int ret;
  2119. AVStream *st;
  2120. if (flags & AVSEEK_FLAG_BYTE) {
  2121. if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
  2122. return -1;
  2123. ff_read_frame_flush(s);
  2124. return seek_frame_byte(s, stream_index, timestamp, flags);
  2125. }
  2126. if (stream_index < 0) {
  2127. stream_index = av_find_default_stream_index(s);
  2128. if (stream_index < 0)
  2129. return -1;
  2130. st = s->streams[stream_index];
  2131. /* timestamp for default must be expressed in AV_TIME_BASE units */
  2132. timestamp = av_rescale(timestamp, st->time_base.den,
  2133. AV_TIME_BASE * (int64_t) st->time_base.num);
  2134. }
  2135. /* first, we try the format specific seek */
  2136. if (s->iformat->read_seek) {
  2137. ff_read_frame_flush(s);
  2138. ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
  2139. } else
  2140. ret = -1;
  2141. if (ret >= 0)
  2142. return 0;
  2143. if (s->iformat->read_timestamp &&
  2144. !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
  2145. ff_read_frame_flush(s);
  2146. return ff_seek_frame_binary(s, stream_index, timestamp, flags);
  2147. } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
  2148. ff_read_frame_flush(s);
  2149. return seek_frame_generic(s, stream_index, timestamp, flags);
  2150. } else
  2151. return -1;
  2152. }
  2153. int av_seek_frame(AVFormatContext *s, int stream_index,
  2154. int64_t timestamp, int flags)
  2155. {
  2156. int ret;
  2157. if (s->iformat->read_seek2 && !s->iformat->read_seek) {
  2158. int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
  2159. if ((flags & AVSEEK_FLAG_BACKWARD))
  2160. max_ts = timestamp;
  2161. else
  2162. min_ts = timestamp;
  2163. return avformat_seek_file(s, stream_index, min_ts, timestamp, max_ts,
  2164. flags & ~AVSEEK_FLAG_BACKWARD);
  2165. }
  2166. ret = seek_frame_internal(s, stream_index, timestamp, flags);
  2167. if (ret >= 0)
  2168. ret = avformat_queue_attached_pictures(s);
  2169. return ret;
  2170. }
  2171. int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
  2172. int64_t ts, int64_t max_ts, int flags)
  2173. {
  2174. if (min_ts > ts || max_ts < ts)
  2175. return -1;
  2176. if (stream_index < -1 || stream_index >= (int)s->nb_streams)
  2177. return AVERROR(EINVAL);
  2178. if (s->seek2any>0)
  2179. flags |= AVSEEK_FLAG_ANY;
  2180. flags &= ~AVSEEK_FLAG_BACKWARD;
  2181. if (s->iformat->read_seek2) {
  2182. int ret;
  2183. ff_read_frame_flush(s);
  2184. if (stream_index == -1 && s->nb_streams == 1) {
  2185. AVRational time_base = s->streams[0]->time_base;
  2186. ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
  2187. min_ts = av_rescale_rnd(min_ts, time_base.den,
  2188. time_base.num * (int64_t)AV_TIME_BASE,
  2189. AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
  2190. max_ts = av_rescale_rnd(max_ts, time_base.den,
  2191. time_base.num * (int64_t)AV_TIME_BASE,
  2192. AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
  2193. stream_index = 0;
  2194. }
  2195. ret = s->iformat->read_seek2(s, stream_index, min_ts,
  2196. ts, max_ts, flags);
  2197. if (ret >= 0)
  2198. ret = avformat_queue_attached_pictures(s);
  2199. return ret;
  2200. }
  2201. if (s->iformat->read_timestamp) {
  2202. // try to seek via read_timestamp()
  2203. }
  2204. // Fall back on old API if new is not implemented but old is.
  2205. // Note the old API has somewhat different semantics.
  2206. if (s->iformat->read_seek || 1) {
  2207. int dir = (ts - (uint64_t)min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0);
  2208. int ret = av_seek_frame(s, stream_index, ts, flags | dir);
  2209. if (ret<0 && ts != min_ts && max_ts != ts) {
  2210. ret = av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
  2211. if (ret >= 0)
  2212. ret = av_seek_frame(s, stream_index, ts, flags | (dir^AVSEEK_FLAG_BACKWARD));
  2213. }
  2214. return ret;
  2215. }
  2216. // try some generic seek like seek_frame_generic() but with new ts semantics
  2217. return -1; //unreachable
  2218. }
  2219. int avformat_flush(AVFormatContext *s)
  2220. {
  2221. ff_read_frame_flush(s);
  2222. return 0;
  2223. }
  2224. /*******************************************************/
  2225. /**
  2226. * Return TRUE if the stream has accurate duration in any stream.
  2227. *
  2228. * @return TRUE if the stream has accurate duration for at least one component.
  2229. */
  2230. static int has_duration(AVFormatContext *ic)
  2231. {
  2232. int i;
  2233. AVStream *st;
  2234. for (i = 0; i < ic->nb_streams; i++) {
  2235. st = ic->streams[i];
  2236. if (st->duration != AV_NOPTS_VALUE)
  2237. return 1;
  2238. }
  2239. if (ic->duration != AV_NOPTS_VALUE)
  2240. return 1;
  2241. return 0;
  2242. }
  2243. /**
  2244. * Estimate the stream timings from the one of each components.
  2245. *
  2246. * Also computes the global bitrate if possible.
  2247. */
  2248. static void update_stream_timings(AVFormatContext *ic)
  2249. {
  2250. int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
  2251. int64_t duration, duration1, duration_text, filesize;
  2252. int i;
  2253. AVProgram *p;
  2254. start_time = INT64_MAX;
  2255. start_time_text = INT64_MAX;
  2256. end_time = INT64_MIN;
  2257. end_time_text = INT64_MIN;
  2258. duration = INT64_MIN;
  2259. duration_text = INT64_MIN;
  2260. for (i = 0; i < ic->nb_streams; i++) {
  2261. AVStream *st = ic->streams[i];
  2262. int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
  2263. st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
  2264. if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
  2265. start_time1 = av_rescale_q(st->start_time, st->time_base,
  2266. AV_TIME_BASE_Q);
  2267. if (is_text)
  2268. start_time_text = FFMIN(start_time_text, start_time1);
  2269. else
  2270. start_time = FFMIN(start_time, start_time1);
  2271. end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
  2272. AV_TIME_BASE_Q,
  2273. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  2274. if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
  2275. end_time1 += start_time1;
  2276. if (is_text)
  2277. end_time_text = FFMAX(end_time_text, end_time1);
  2278. else
  2279. end_time = FFMAX(end_time, end_time1);
  2280. }
  2281. for (p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
  2282. if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
  2283. p->start_time = start_time1;
  2284. if (p->end_time < end_time1)
  2285. p->end_time = end_time1;
  2286. }
  2287. }
  2288. if (st->duration != AV_NOPTS_VALUE) {
  2289. duration1 = av_rescale_q(st->duration, st->time_base,
  2290. AV_TIME_BASE_Q);
  2291. if (is_text)
  2292. duration_text = FFMAX(duration_text, duration1);
  2293. else
  2294. duration = FFMAX(duration, duration1);
  2295. }
  2296. }
  2297. if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
  2298. start_time = start_time_text;
  2299. else if (start_time > start_time_text)
  2300. av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
  2301. if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
  2302. end_time = end_time_text;
  2303. else if (end_time < end_time_text)
  2304. av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
  2305. if (duration == INT64_MIN || (duration < duration_text && duration_text - duration < AV_TIME_BASE))
  2306. duration = duration_text;
  2307. else if (duration < duration_text)
  2308. av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
  2309. if (start_time != INT64_MAX) {
  2310. ic->start_time = start_time;
  2311. if (end_time != INT64_MIN) {
  2312. if (ic->nb_programs > 1) {
  2313. for (i = 0; i < ic->nb_programs; i++) {
  2314. p = ic->programs[i];
  2315. if (p->start_time != AV_NOPTS_VALUE &&
  2316. p->end_time > p->start_time &&
  2317. p->end_time - (uint64_t)p->start_time <= INT64_MAX)
  2318. duration = FFMAX(duration, p->end_time - p->start_time);
  2319. }
  2320. } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
  2321. duration = FFMAX(duration, end_time - start_time);
  2322. }
  2323. }
  2324. }
  2325. if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
  2326. ic->duration = duration;
  2327. }
  2328. if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
  2329. /* compute the bitrate */
  2330. double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
  2331. (double) ic->duration;
  2332. if (bitrate >= 0 && bitrate <= INT64_MAX)
  2333. ic->bit_rate = bitrate;
  2334. }
  2335. }
  2336. static void fill_all_stream_timings(AVFormatContext *ic)
  2337. {
  2338. int i;
  2339. AVStream *st;
  2340. update_stream_timings(ic);
  2341. for (i = 0; i < ic->nb_streams; i++) {
  2342. st = ic->streams[i];
  2343. if (st->start_time == AV_NOPTS_VALUE) {
  2344. if (ic->start_time != AV_NOPTS_VALUE)
  2345. st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
  2346. st->time_base);
  2347. if (ic->duration != AV_NOPTS_VALUE)
  2348. st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
  2349. st->time_base);
  2350. }
  2351. }
  2352. }
  2353. static void estimate_timings_from_bit_rate(AVFormatContext *ic)
  2354. {
  2355. int64_t filesize, duration;
  2356. int i, show_warning = 0;
  2357. AVStream *st;
  2358. /* if bit_rate is already set, we believe it */
  2359. if (ic->bit_rate <= 0) {
  2360. int64_t bit_rate = 0;
  2361. for (i = 0; i < ic->nb_streams; i++) {
  2362. st = ic->streams[i];
  2363. if (st->codecpar->bit_rate <= 0 && st->internal->avctx->bit_rate > 0)
  2364. st->codecpar->bit_rate = st->internal->avctx->bit_rate;
  2365. if (st->codecpar->bit_rate > 0) {
  2366. if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
  2367. bit_rate = 0;
  2368. break;
  2369. }
  2370. bit_rate += st->codecpar->bit_rate;
  2371. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->codec_info_nb_frames > 1) {
  2372. // If we have a videostream with packets but without a bitrate
  2373. // then consider the sum not known
  2374. bit_rate = 0;
  2375. break;
  2376. }
  2377. }
  2378. ic->bit_rate = bit_rate;
  2379. }
  2380. /* if duration is already set, we believe it */
  2381. if (ic->duration == AV_NOPTS_VALUE &&
  2382. ic->bit_rate != 0) {
  2383. filesize = ic->pb ? avio_size(ic->pb) : 0;
  2384. if (filesize > ic->internal->data_offset) {
  2385. filesize -= ic->internal->data_offset;
  2386. for (i = 0; i < ic->nb_streams; i++) {
  2387. st = ic->streams[i];
  2388. if ( st->time_base.num <= INT64_MAX / ic->bit_rate
  2389. && st->duration == AV_NOPTS_VALUE) {
  2390. duration = av_rescale(filesize, 8LL * st->time_base.den,
  2391. ic->bit_rate *
  2392. (int64_t) st->time_base.num);
  2393. st->duration = duration;
  2394. show_warning = 1;
  2395. }
  2396. }
  2397. }
  2398. }
  2399. if (show_warning)
  2400. av_log(ic, AV_LOG_WARNING,
  2401. "Estimating duration from bitrate, this may be inaccurate\n");
  2402. }
  2403. #define DURATION_MAX_READ_SIZE 250000LL
  2404. #define DURATION_MAX_RETRY 6
  2405. /* only usable for MPEG-PS streams */
  2406. static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
  2407. {
  2408. AVPacket *pkt = ic->internal->pkt;
  2409. AVStream *st;
  2410. int num, den, read_size, i, ret;
  2411. int found_duration = 0;
  2412. int is_end;
  2413. int64_t filesize, offset, duration;
  2414. int retry = 0;
  2415. /* flush packet queue */
  2416. flush_packet_queue(ic);
  2417. for (i = 0; i < ic->nb_streams; i++) {
  2418. st = ic->streams[i];
  2419. if (st->start_time == AV_NOPTS_VALUE &&
  2420. st->first_dts == AV_NOPTS_VALUE &&
  2421. st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
  2422. av_log(ic, AV_LOG_WARNING,
  2423. "start time for stream %d is not set in estimate_timings_from_pts\n", i);
  2424. if (st->parser) {
  2425. av_parser_close(st->parser);
  2426. st->parser = NULL;
  2427. }
  2428. }
  2429. if (ic->skip_estimate_duration_from_pts) {
  2430. av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
  2431. goto skip_duration_calc;
  2432. }
  2433. av_opt_set(ic, "skip_changes", "1", AV_OPT_SEARCH_CHILDREN);
  2434. /* estimate the end time (duration) */
  2435. /* XXX: may need to support wrapping */
  2436. filesize = ic->pb ? avio_size(ic->pb) : 0;
  2437. do {
  2438. is_end = found_duration;
  2439. offset = filesize - (DURATION_MAX_READ_SIZE << retry);
  2440. if (offset < 0)
  2441. offset = 0;
  2442. avio_seek(ic->pb, offset, SEEK_SET);
  2443. read_size = 0;
  2444. for (;;) {
  2445. if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
  2446. break;
  2447. do {
  2448. ret = ff_read_packet(ic, pkt);
  2449. } while (ret == AVERROR(EAGAIN));
  2450. if (ret != 0)
  2451. break;
  2452. read_size += pkt->size;
  2453. st = ic->streams[pkt->stream_index];
  2454. if (pkt->pts != AV_NOPTS_VALUE &&
  2455. (st->start_time != AV_NOPTS_VALUE ||
  2456. st->first_dts != AV_NOPTS_VALUE)) {
  2457. if (pkt->duration == 0) {
  2458. ff_compute_frame_duration(ic, &num, &den, st, st->parser, pkt);
  2459. if (den && num) {
  2460. pkt->duration = av_rescale_rnd(1,
  2461. num * (int64_t) st->time_base.den,
  2462. den * (int64_t) st->time_base.num,
  2463. AV_ROUND_DOWN);
  2464. }
  2465. }
  2466. duration = pkt->pts + pkt->duration;
  2467. found_duration = 1;
  2468. if (st->start_time != AV_NOPTS_VALUE)
  2469. duration -= st->start_time;
  2470. else
  2471. duration -= st->first_dts;
  2472. if (duration > 0) {
  2473. if (st->duration == AV_NOPTS_VALUE || st->internal->info->last_duration<= 0 ||
  2474. (st->duration < duration && FFABS(duration - st->internal->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
  2475. st->duration = duration;
  2476. st->internal->info->last_duration = duration;
  2477. }
  2478. }
  2479. av_packet_unref(pkt);
  2480. }
  2481. /* check if all audio/video streams have valid duration */
  2482. if (!is_end) {
  2483. is_end = 1;
  2484. for (i = 0; i < ic->nb_streams; i++) {
  2485. st = ic->streams[i];
  2486. switch (st->codecpar->codec_type) {
  2487. case AVMEDIA_TYPE_VIDEO:
  2488. case AVMEDIA_TYPE_AUDIO:
  2489. if (st->duration == AV_NOPTS_VALUE)
  2490. is_end = 0;
  2491. }
  2492. }
  2493. }
  2494. } while (!is_end &&
  2495. offset &&
  2496. ++retry <= DURATION_MAX_RETRY);
  2497. av_opt_set(ic, "skip_changes", "0", AV_OPT_SEARCH_CHILDREN);
  2498. /* warn about audio/video streams which duration could not be estimated */
  2499. for (i = 0; i < ic->nb_streams; i++) {
  2500. st = ic->streams[i];
  2501. if (st->duration == AV_NOPTS_VALUE) {
  2502. switch (st->codecpar->codec_type) {
  2503. case AVMEDIA_TYPE_VIDEO:
  2504. case AVMEDIA_TYPE_AUDIO:
  2505. if (st->start_time != AV_NOPTS_VALUE || st->first_dts != AV_NOPTS_VALUE) {
  2506. av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
  2507. } else
  2508. av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
  2509. }
  2510. }
  2511. }
  2512. skip_duration_calc:
  2513. fill_all_stream_timings(ic);
  2514. avio_seek(ic->pb, old_offset, SEEK_SET);
  2515. for (i = 0; i < ic->nb_streams; i++) {
  2516. int j;
  2517. st = ic->streams[i];
  2518. st->cur_dts = st->first_dts;
  2519. st->last_IP_pts = AV_NOPTS_VALUE;
  2520. st->internal->last_dts_for_order_check = AV_NOPTS_VALUE;
  2521. for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
  2522. st->internal->pts_buffer[j] = AV_NOPTS_VALUE;
  2523. }
  2524. }
  2525. /* 1:1 map to AVDurationEstimationMethod */
  2526. static const char *const duration_name[] = {
  2527. [AVFMT_DURATION_FROM_PTS] = "pts",
  2528. [AVFMT_DURATION_FROM_STREAM] = "stream",
  2529. [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
  2530. };
  2531. static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
  2532. {
  2533. return duration_name[method];
  2534. }
  2535. static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
  2536. {
  2537. int64_t file_size;
  2538. /* get the file size, if possible */
  2539. if (ic->iformat->flags & AVFMT_NOFILE) {
  2540. file_size = 0;
  2541. } else {
  2542. file_size = avio_size(ic->pb);
  2543. file_size = FFMAX(0, file_size);
  2544. }
  2545. if ((!strcmp(ic->iformat->name, "mpeg") ||
  2546. !strcmp(ic->iformat->name, "mpegts")) &&
  2547. file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  2548. /* get accurate estimate from the PTSes */
  2549. estimate_timings_from_pts(ic, old_offset);
  2550. ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
  2551. } else if (has_duration(ic)) {
  2552. /* at least one component has timings - we use them for all
  2553. * the components */
  2554. fill_all_stream_timings(ic);
  2555. /* nut demuxer estimate the duration from PTS */
  2556. if(!strcmp(ic->iformat->name, "nut"))
  2557. ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
  2558. else
  2559. ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
  2560. } else {
  2561. /* less precise: use bitrate info */
  2562. estimate_timings_from_bit_rate(ic);
  2563. ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
  2564. }
  2565. update_stream_timings(ic);
  2566. {
  2567. int i;
  2568. AVStream av_unused *st;
  2569. for (i = 0; i < ic->nb_streams; i++) {
  2570. st = ic->streams[i];
  2571. if (st->time_base.den)
  2572. av_log(ic, AV_LOG_TRACE, "stream %d: start_time: %s duration: %s\n", i,
  2573. av_ts2timestr(st->start_time, &st->time_base),
  2574. av_ts2timestr(st->duration, &st->time_base));
  2575. }
  2576. av_log(ic, AV_LOG_TRACE,
  2577. "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
  2578. av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
  2579. av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
  2580. duration_estimate_name(ic->duration_estimation_method),
  2581. (int64_t)ic->bit_rate / 1000);
  2582. }
  2583. }
  2584. static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
  2585. {
  2586. AVCodecContext *avctx = st->internal->avctx;
  2587. #define FAIL(errmsg) do { \
  2588. if (errmsg_ptr) \
  2589. *errmsg_ptr = errmsg; \
  2590. return 0; \
  2591. } while (0)
  2592. if ( avctx->codec_id == AV_CODEC_ID_NONE
  2593. && avctx->codec_type != AVMEDIA_TYPE_DATA)
  2594. FAIL("unknown codec");
  2595. switch (avctx->codec_type) {
  2596. case AVMEDIA_TYPE_AUDIO:
  2597. if (!avctx->frame_size && determinable_frame_size(avctx))
  2598. FAIL("unspecified frame size");
  2599. if (st->internal->info->found_decoder >= 0 &&
  2600. avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
  2601. FAIL("unspecified sample format");
  2602. if (!avctx->sample_rate)
  2603. FAIL("unspecified sample rate");
  2604. if (!avctx->channels)
  2605. FAIL("unspecified number of channels");
  2606. if (st->internal->info->found_decoder >= 0 && !st->internal->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
  2607. FAIL("no decodable DTS frames");
  2608. break;
  2609. case AVMEDIA_TYPE_VIDEO:
  2610. if (!avctx->width)
  2611. FAIL("unspecified size");
  2612. if (st->internal->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
  2613. FAIL("unspecified pixel format");
  2614. if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
  2615. if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !st->codec_info_nb_frames)
  2616. FAIL("no frame in rv30/40 and no sar");
  2617. break;
  2618. case AVMEDIA_TYPE_SUBTITLE:
  2619. if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
  2620. FAIL("unspecified size");
  2621. break;
  2622. case AVMEDIA_TYPE_DATA:
  2623. if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
  2624. }
  2625. return 1;
  2626. }
  2627. /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
  2628. static int try_decode_frame(AVFormatContext *s, AVStream *st,
  2629. const AVPacket *avpkt, AVDictionary **options)
  2630. {
  2631. AVCodecContext *avctx = st->internal->avctx;
  2632. const AVCodec *codec;
  2633. int got_picture = 1, ret = 0;
  2634. AVFrame *frame = av_frame_alloc();
  2635. AVSubtitle subtitle;
  2636. AVPacket pkt = *avpkt;
  2637. int do_skip_frame = 0;
  2638. enum AVDiscard skip_frame;
  2639. if (!frame)
  2640. return AVERROR(ENOMEM);
  2641. if (!avcodec_is_open(avctx) &&
  2642. st->internal->info->found_decoder <= 0 &&
  2643. (st->codecpar->codec_id != -st->internal->info->found_decoder || !st->codecpar->codec_id)) {
  2644. AVDictionary *thread_opt = NULL;
  2645. codec = find_probe_decoder(s, st, st->codecpar->codec_id);
  2646. if (!codec) {
  2647. st->internal->info->found_decoder = -st->codecpar->codec_id;
  2648. ret = -1;
  2649. goto fail;
  2650. }
  2651. /* Force thread count to 1 since the H.264 decoder will not extract
  2652. * SPS and PPS to extradata during multi-threaded decoding. */
  2653. av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
  2654. /* Force lowres to 0. The decoder might reduce the video size by the
  2655. * lowres factor, and we don't want that propagated to the stream's
  2656. * codecpar */
  2657. av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
  2658. if (s->codec_whitelist)
  2659. av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
  2660. ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
  2661. if (!options)
  2662. av_dict_free(&thread_opt);
  2663. if (ret < 0) {
  2664. st->internal->info->found_decoder = -avctx->codec_id;
  2665. goto fail;
  2666. }
  2667. st->internal->info->found_decoder = 1;
  2668. } else if (!st->internal->info->found_decoder)
  2669. st->internal->info->found_decoder = 1;
  2670. if (st->internal->info->found_decoder < 0) {
  2671. ret = -1;
  2672. goto fail;
  2673. }
  2674. if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
  2675. do_skip_frame = 1;
  2676. skip_frame = avctx->skip_frame;
  2677. avctx->skip_frame = AVDISCARD_ALL;
  2678. }
  2679. while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
  2680. ret >= 0 &&
  2681. (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
  2682. (!st->codec_info_nb_frames &&
  2683. (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
  2684. got_picture = 0;
  2685. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
  2686. avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  2687. ret = avcodec_send_packet(avctx, &pkt);
  2688. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  2689. break;
  2690. if (ret >= 0)
  2691. pkt.size = 0;
  2692. ret = avcodec_receive_frame(avctx, frame);
  2693. if (ret >= 0)
  2694. got_picture = 1;
  2695. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  2696. ret = 0;
  2697. } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  2698. ret = avcodec_decode_subtitle2(avctx, &subtitle,
  2699. &got_picture, &pkt);
  2700. if (got_picture)
  2701. avsubtitle_free(&subtitle);
  2702. if (ret >= 0)
  2703. pkt.size = 0;
  2704. }
  2705. if (ret >= 0) {
  2706. if (got_picture)
  2707. st->internal->nb_decoded_frames++;
  2708. ret = got_picture;
  2709. }
  2710. }
  2711. if (!pkt.data && !got_picture)
  2712. ret = -1;
  2713. fail:
  2714. if (do_skip_frame) {
  2715. avctx->skip_frame = skip_frame;
  2716. }
  2717. av_frame_free(&frame);
  2718. return ret;
  2719. }
  2720. unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
  2721. {
  2722. while (tags->id != AV_CODEC_ID_NONE) {
  2723. if (tags->id == id)
  2724. return tags->tag;
  2725. tags++;
  2726. }
  2727. return 0;
  2728. }
  2729. enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
  2730. {
  2731. int i;
  2732. for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
  2733. if (tag == tags[i].tag)
  2734. return tags[i].id;
  2735. for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
  2736. if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
  2737. return tags[i].id;
  2738. return AV_CODEC_ID_NONE;
  2739. }
  2740. enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
  2741. {
  2742. if (bps <= 0 || bps > 64)
  2743. return AV_CODEC_ID_NONE;
  2744. if (flt) {
  2745. switch (bps) {
  2746. case 32:
  2747. return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
  2748. case 64:
  2749. return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
  2750. default:
  2751. return AV_CODEC_ID_NONE;
  2752. }
  2753. } else {
  2754. bps += 7;
  2755. bps >>= 3;
  2756. if (sflags & (1 << (bps - 1))) {
  2757. switch (bps) {
  2758. case 1:
  2759. return AV_CODEC_ID_PCM_S8;
  2760. case 2:
  2761. return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
  2762. case 3:
  2763. return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  2764. case 4:
  2765. return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
  2766. case 8:
  2767. return be ? AV_CODEC_ID_PCM_S64BE : AV_CODEC_ID_PCM_S64LE;
  2768. default:
  2769. return AV_CODEC_ID_NONE;
  2770. }
  2771. } else {
  2772. switch (bps) {
  2773. case 1:
  2774. return AV_CODEC_ID_PCM_U8;
  2775. case 2:
  2776. return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
  2777. case 3:
  2778. return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
  2779. case 4:
  2780. return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
  2781. default:
  2782. return AV_CODEC_ID_NONE;
  2783. }
  2784. }
  2785. }
  2786. }
  2787. unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
  2788. {
  2789. unsigned int tag;
  2790. if (!av_codec_get_tag2(tags, id, &tag))
  2791. return 0;
  2792. return tag;
  2793. }
  2794. int av_codec_get_tag2(const AVCodecTag * const *tags, enum AVCodecID id,
  2795. unsigned int *tag)
  2796. {
  2797. int i;
  2798. for (i = 0; tags && tags[i]; i++) {
  2799. const AVCodecTag *codec_tags = tags[i];
  2800. while (codec_tags->id != AV_CODEC_ID_NONE) {
  2801. if (codec_tags->id == id) {
  2802. *tag = codec_tags->tag;
  2803. return 1;
  2804. }
  2805. codec_tags++;
  2806. }
  2807. }
  2808. return 0;
  2809. }
  2810. enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
  2811. {
  2812. int i;
  2813. for (i = 0; tags && tags[i]; i++) {
  2814. enum AVCodecID id = ff_codec_get_id(tags[i], tag);
  2815. if (id != AV_CODEC_ID_NONE)
  2816. return id;
  2817. }
  2818. return AV_CODEC_ID_NONE;
  2819. }
  2820. static int chapter_start_cmp(const void *p1, const void *p2)
  2821. {
  2822. AVChapter *ch1 = *(AVChapter**)p1;
  2823. AVChapter *ch2 = *(AVChapter**)p2;
  2824. int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
  2825. if (delta)
  2826. return delta;
  2827. return (ch1 > ch2) - (ch1 < ch2);
  2828. }
  2829. static int compute_chapters_end(AVFormatContext *s)
  2830. {
  2831. unsigned int i;
  2832. int64_t max_time = 0;
  2833. AVChapter **timetable = av_malloc(s->nb_chapters * sizeof(*timetable));
  2834. if (!timetable)
  2835. return AVERROR(ENOMEM);
  2836. if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
  2837. max_time = s->duration +
  2838. ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
  2839. for (i = 0; i < s->nb_chapters; i++)
  2840. timetable[i] = s->chapters[i];
  2841. qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
  2842. for (i = 0; i < s->nb_chapters; i++)
  2843. if (timetable[i]->end == AV_NOPTS_VALUE) {
  2844. AVChapter *ch = timetable[i];
  2845. int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
  2846. ch->time_base)
  2847. : INT64_MAX;
  2848. if (i + 1 < s->nb_chapters) {
  2849. AVChapter *ch1 = timetable[i + 1];
  2850. int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
  2851. ch->time_base);
  2852. if (next_start > ch->start && next_start < end)
  2853. end = next_start;
  2854. }
  2855. ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
  2856. }
  2857. av_free(timetable);
  2858. return 0;
  2859. }
  2860. static int get_std_framerate(int i)
  2861. {
  2862. if (i < 30*12)
  2863. return (i + 1) * 1001;
  2864. i -= 30*12;
  2865. if (i < 30)
  2866. return (i + 31) * 1001 * 12;
  2867. i -= 30;
  2868. if (i < 3)
  2869. return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
  2870. i -= 3;
  2871. return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
  2872. }
  2873. /* Is the time base unreliable?
  2874. * This is a heuristic to balance between quick acceptance of the values in
  2875. * the headers vs. some extra checks.
  2876. * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
  2877. * MPEG-2 commonly misuses field repeat flags to store different framerates.
  2878. * And there are "variable" fps files this needs to detect as well. */
  2879. static int tb_unreliable(AVCodecContext *c)
  2880. {
  2881. if (c->time_base.den >= 101LL * c->time_base.num ||
  2882. c->time_base.den < 5LL * c->time_base.num ||
  2883. // c->codec_tag == AV_RL32("DIVX") ||
  2884. // c->codec_tag == AV_RL32("XVID") ||
  2885. c->codec_tag == AV_RL32("mp4v") ||
  2886. c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
  2887. c->codec_id == AV_CODEC_ID_GIF ||
  2888. c->codec_id == AV_CODEC_ID_HEVC ||
  2889. c->codec_id == AV_CODEC_ID_H264)
  2890. return 1;
  2891. return 0;
  2892. }
  2893. int ff_alloc_extradata(AVCodecParameters *par, int size)
  2894. {
  2895. av_freep(&par->extradata);
  2896. par->extradata_size = 0;
  2897. if (size < 0 || size >= INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  2898. return AVERROR(EINVAL);
  2899. par->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  2900. if (!par->extradata)
  2901. return AVERROR(ENOMEM);
  2902. memset(par->extradata + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  2903. par->extradata_size = size;
  2904. return 0;
  2905. }
  2906. int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
  2907. {
  2908. int ret = ff_alloc_extradata(par, size);
  2909. if (ret < 0)
  2910. return ret;
  2911. ret = avio_read(pb, par->extradata, size);
  2912. if (ret != size) {
  2913. av_freep(&par->extradata);
  2914. par->extradata_size = 0;
  2915. av_log(s, AV_LOG_ERROR, "Failed to read extradata of size %d\n", size);
  2916. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  2917. }
  2918. return ret;
  2919. }
  2920. int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
  2921. {
  2922. int i, j;
  2923. int64_t last = st->internal->info->last_dts;
  2924. if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
  2925. && ts - (uint64_t)last < INT64_MAX) {
  2926. double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
  2927. int64_t duration = ts - last;
  2928. if (!st->internal->info->duration_error)
  2929. st->internal->info->duration_error = av_mallocz(sizeof(st->internal->info->duration_error[0])*2);
  2930. if (!st->internal->info->duration_error)
  2931. return AVERROR(ENOMEM);
  2932. // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  2933. // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
  2934. for (i = 0; i<MAX_STD_TIMEBASES; i++) {
  2935. if (st->internal->info->duration_error[0][1][i] < 1e10) {
  2936. int framerate = get_std_framerate(i);
  2937. double sdts = dts*framerate/(1001*12);
  2938. for (j= 0; j<2; j++) {
  2939. int64_t ticks = llrint(sdts+j*0.5);
  2940. double error= sdts - ticks + j*0.5;
  2941. st->internal->info->duration_error[j][0][i] += error;
  2942. st->internal->info->duration_error[j][1][i] += error*error;
  2943. }
  2944. }
  2945. }
  2946. if (st->internal->info->rfps_duration_sum <= INT64_MAX - duration) {
  2947. st->internal->info->duration_count++;
  2948. st->internal->info->rfps_duration_sum += duration;
  2949. }
  2950. if (st->internal->info->duration_count % 10 == 0) {
  2951. int n = st->internal->info->duration_count;
  2952. for (i = 0; i<MAX_STD_TIMEBASES; i++) {
  2953. if (st->internal->info->duration_error[0][1][i] < 1e10) {
  2954. double a0 = st->internal->info->duration_error[0][0][i] / n;
  2955. double error0 = st->internal->info->duration_error[0][1][i] / n - a0*a0;
  2956. double a1 = st->internal->info->duration_error[1][0][i] / n;
  2957. double error1 = st->internal->info->duration_error[1][1][i] / n - a1*a1;
  2958. if (error0 > 0.04 && error1 > 0.04) {
  2959. st->internal->info->duration_error[0][1][i] = 2e10;
  2960. st->internal->info->duration_error[1][1][i] = 2e10;
  2961. }
  2962. }
  2963. }
  2964. }
  2965. // ignore the first 4 values, they might have some random jitter
  2966. if (st->internal->info->duration_count > 3 && is_relative(ts) == is_relative(last))
  2967. st->internal->info->duration_gcd = av_gcd(st->internal->info->duration_gcd, duration);
  2968. }
  2969. if (ts != AV_NOPTS_VALUE)
  2970. st->internal->info->last_dts = ts;
  2971. return 0;
  2972. }
  2973. void ff_rfps_calculate(AVFormatContext *ic)
  2974. {
  2975. int i, j;
  2976. for (i = 0; i < ic->nb_streams; i++) {
  2977. AVStream *st = ic->streams[i];
  2978. if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
  2979. continue;
  2980. // the check for tb_unreliable() is not completely correct, since this is not about handling
  2981. // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
  2982. // ipmovie.c produces.
  2983. if (tb_unreliable(st->internal->avctx) && st->internal->info->duration_count > 15 && st->internal->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num)
  2984. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->internal->info->duration_gcd, INT_MAX);
  2985. if (st->internal->info->duration_count>1 && !st->r_frame_rate.num
  2986. && tb_unreliable(st->internal->avctx)) {
  2987. int num = 0;
  2988. double best_error= 0.01;
  2989. AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
  2990. for (j= 0; j<MAX_STD_TIMEBASES; j++) {
  2991. int k;
  2992. if (st->internal->info->codec_info_duration &&
  2993. st->internal->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
  2994. continue;
  2995. if (!st->internal->info->codec_info_duration && get_std_framerate(j) < 1001*12)
  2996. continue;
  2997. if (av_q2d(st->time_base) * st->internal->info->rfps_duration_sum / st->internal->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
  2998. continue;
  2999. for (k= 0; k<2; k++) {
  3000. int n = st->internal->info->duration_count;
  3001. double a= st->internal->info->duration_error[k][0][j] / n;
  3002. double error= st->internal->info->duration_error[k][1][j]/n - a*a;
  3003. if (error < best_error && best_error> 0.000000001) {
  3004. best_error= error;
  3005. num = get_std_framerate(j);
  3006. }
  3007. if (error < 0.02)
  3008. av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
  3009. }
  3010. }
  3011. // do not increase frame rate by more than 1 % in order to match a standard rate.
  3012. if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
  3013. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
  3014. }
  3015. if ( !st->avg_frame_rate.num
  3016. && st->r_frame_rate.num && st->internal->info->rfps_duration_sum
  3017. && st->internal->info->codec_info_duration <= 0
  3018. && st->internal->info->duration_count > 2
  3019. && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - st->internal->info->rfps_duration_sum / (double)st->internal->info->duration_count) <= 1.0
  3020. ) {
  3021. av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
  3022. st->avg_frame_rate = st->r_frame_rate;
  3023. }
  3024. av_freep(&st->internal->info->duration_error);
  3025. st->internal->info->last_dts = AV_NOPTS_VALUE;
  3026. st->internal->info->duration_count = 0;
  3027. st->internal->info->rfps_duration_sum = 0;
  3028. }
  3029. }
  3030. static int extract_extradata_check(AVStream *st)
  3031. {
  3032. const AVBitStreamFilter *f;
  3033. f = av_bsf_get_by_name("extract_extradata");
  3034. if (!f)
  3035. return 0;
  3036. if (f->codec_ids) {
  3037. const enum AVCodecID *ids;
  3038. for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
  3039. if (*ids == st->codecpar->codec_id)
  3040. return 1;
  3041. }
  3042. return 0;
  3043. }
  3044. static int extract_extradata_init(AVStream *st)
  3045. {
  3046. AVStreamInternal *sti = st->internal;
  3047. const AVBitStreamFilter *f;
  3048. int ret;
  3049. f = av_bsf_get_by_name("extract_extradata");
  3050. if (!f)
  3051. goto finish;
  3052. /* check that the codec id is supported */
  3053. ret = extract_extradata_check(st);
  3054. if (!ret)
  3055. goto finish;
  3056. sti->extract_extradata.pkt = av_packet_alloc();
  3057. if (!sti->extract_extradata.pkt)
  3058. return AVERROR(ENOMEM);
  3059. ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
  3060. if (ret < 0)
  3061. goto fail;
  3062. ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
  3063. st->codecpar);
  3064. if (ret < 0)
  3065. goto fail;
  3066. sti->extract_extradata.bsf->time_base_in = st->time_base;
  3067. ret = av_bsf_init(sti->extract_extradata.bsf);
  3068. if (ret < 0)
  3069. goto fail;
  3070. finish:
  3071. sti->extract_extradata.inited = 1;
  3072. return 0;
  3073. fail:
  3074. av_bsf_free(&sti->extract_extradata.bsf);
  3075. av_packet_free(&sti->extract_extradata.pkt);
  3076. return ret;
  3077. }
  3078. static int extract_extradata(AVStream *st, const AVPacket *pkt)
  3079. {
  3080. AVStreamInternal *sti = st->internal;
  3081. AVPacket *pkt_ref;
  3082. int ret;
  3083. if (!sti->extract_extradata.inited) {
  3084. ret = extract_extradata_init(st);
  3085. if (ret < 0)
  3086. return ret;
  3087. }
  3088. if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
  3089. return 0;
  3090. pkt_ref = sti->extract_extradata.pkt;
  3091. ret = av_packet_ref(pkt_ref, pkt);
  3092. if (ret < 0)
  3093. return ret;
  3094. ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
  3095. if (ret < 0) {
  3096. av_packet_unref(pkt_ref);
  3097. return ret;
  3098. }
  3099. while (ret >= 0 && !sti->avctx->extradata) {
  3100. ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
  3101. if (ret < 0) {
  3102. if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  3103. return ret;
  3104. continue;
  3105. }
  3106. for (int i = 0; i < pkt_ref->side_data_elems; i++) {
  3107. AVPacketSideData *side_data = &pkt_ref->side_data[i];
  3108. if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
  3109. sti->avctx->extradata = side_data->data;
  3110. sti->avctx->extradata_size = side_data->size;
  3111. side_data->data = NULL;
  3112. side_data->size = 0;
  3113. break;
  3114. }
  3115. }
  3116. av_packet_unref(pkt_ref);
  3117. }
  3118. return 0;
  3119. }
  3120. static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
  3121. {
  3122. int i;
  3123. for (i = 0; i < avctx->nb_coded_side_data; i++) {
  3124. const AVPacketSideData *sd_src = &avctx->coded_side_data[i];
  3125. uint8_t *dst_data;
  3126. dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
  3127. if (!dst_data)
  3128. return AVERROR(ENOMEM);
  3129. memcpy(dst_data, sd_src->data, sd_src->size);
  3130. }
  3131. return 0;
  3132. }
  3133. int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
  3134. {
  3135. int i, count = 0, ret = 0, j;
  3136. int64_t read_size;
  3137. AVStream *st;
  3138. AVCodecContext *avctx;
  3139. AVPacket *pkt1 = ic->internal->pkt;
  3140. int64_t old_offset = avio_tell(ic->pb);
  3141. // new streams might appear, no options for those
  3142. int orig_nb_streams = ic->nb_streams;
  3143. int flush_codecs;
  3144. int64_t max_analyze_duration = ic->max_analyze_duration;
  3145. int64_t max_stream_analyze_duration;
  3146. int64_t max_subtitle_analyze_duration;
  3147. int64_t probesize = ic->probesize;
  3148. int eof_reached = 0;
  3149. int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
  3150. flush_codecs = probesize > 0;
  3151. av_opt_set(ic, "skip_clear", "1", AV_OPT_SEARCH_CHILDREN);
  3152. max_stream_analyze_duration = max_analyze_duration;
  3153. max_subtitle_analyze_duration = max_analyze_duration;
  3154. if (!max_analyze_duration) {
  3155. max_stream_analyze_duration =
  3156. max_analyze_duration = 5*AV_TIME_BASE;
  3157. max_subtitle_analyze_duration = 30*AV_TIME_BASE;
  3158. if (!strcmp(ic->iformat->name, "flv"))
  3159. max_stream_analyze_duration = 90*AV_TIME_BASE;
  3160. if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
  3161. max_stream_analyze_duration = 7*AV_TIME_BASE;
  3162. }
  3163. if (ic->pb)
  3164. av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
  3165. avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, ic->nb_streams);
  3166. for (i = 0; i < ic->nb_streams; i++) {
  3167. const AVCodec *codec;
  3168. AVDictionary *thread_opt = NULL;
  3169. st = ic->streams[i];
  3170. avctx = st->internal->avctx;
  3171. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
  3172. st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  3173. /* if (!st->time_base.num)
  3174. st->time_base = */
  3175. if (!avctx->time_base.num)
  3176. avctx->time_base = st->time_base;
  3177. }
  3178. /* check if the caller has overridden the codec id */
  3179. #if FF_API_LAVF_AVCTX
  3180. FF_DISABLE_DEPRECATION_WARNINGS
  3181. if (st->codec->codec_id != st->internal->orig_codec_id) {
  3182. st->codecpar->codec_id = st->codec->codec_id;
  3183. st->codecpar->codec_type = st->codec->codec_type;
  3184. st->internal->orig_codec_id = st->codec->codec_id;
  3185. }
  3186. FF_ENABLE_DEPRECATION_WARNINGS
  3187. #endif
  3188. // only for the split stuff
  3189. if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && st->internal->request_probe <= 0) {
  3190. st->parser = av_parser_init(st->codecpar->codec_id);
  3191. if (st->parser) {
  3192. if (st->need_parsing == AVSTREAM_PARSE_HEADERS) {
  3193. st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
  3194. } else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
  3195. st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  3196. }
  3197. } else if (st->need_parsing) {
  3198. av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
  3199. "%s, packets or times may be invalid.\n",
  3200. avcodec_get_name(st->codecpar->codec_id));
  3201. }
  3202. }
  3203. if (st->codecpar->codec_id != st->internal->orig_codec_id)
  3204. st->internal->orig_codec_id = st->codecpar->codec_id;
  3205. ret = avcodec_parameters_to_context(avctx, st->codecpar);
  3206. if (ret < 0)
  3207. goto find_stream_info_err;
  3208. if (st->internal->request_probe <= 0)
  3209. st->internal->avctx_inited = 1;
  3210. codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
  3211. /* Force thread count to 1 since the H.264 decoder will not extract
  3212. * SPS and PPS to extradata during multi-threaded decoding. */
  3213. av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
  3214. /* Force lowres to 0. The decoder might reduce the video size by the
  3215. * lowres factor, and we don't want that propagated to the stream's
  3216. * codecpar */
  3217. av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
  3218. if (ic->codec_whitelist)
  3219. av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
  3220. /* Ensure that subtitle_header is properly set. */
  3221. if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE
  3222. && codec && !avctx->codec) {
  3223. if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
  3224. av_log(ic, AV_LOG_WARNING,
  3225. "Failed to open codec in %s\n",__FUNCTION__);
  3226. }
  3227. // Try to just open decoders, in case this is enough to get parameters.
  3228. if (!has_codec_parameters(st, NULL) && st->internal->request_probe <= 0) {
  3229. if (codec && !avctx->codec)
  3230. if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
  3231. av_log(ic, AV_LOG_WARNING,
  3232. "Failed to open codec in %s\n",__FUNCTION__);
  3233. }
  3234. if (!options)
  3235. av_dict_free(&thread_opt);
  3236. }
  3237. for (i = 0; i < ic->nb_streams; i++) {
  3238. #if FF_API_R_FRAME_RATE
  3239. ic->streams[i]->internal->info->last_dts = AV_NOPTS_VALUE;
  3240. #endif
  3241. ic->streams[i]->internal->info->fps_first_dts = AV_NOPTS_VALUE;
  3242. ic->streams[i]->internal->info->fps_last_dts = AV_NOPTS_VALUE;
  3243. }
  3244. read_size = 0;
  3245. for (;;) {
  3246. const AVPacket *pkt;
  3247. int analyzed_all_streams;
  3248. if (ff_check_interrupt(&ic->interrupt_callback)) {
  3249. ret = AVERROR_EXIT;
  3250. av_log(ic, AV_LOG_DEBUG, "interrupted\n");
  3251. break;
  3252. }
  3253. /* check if one codec still needs to be handled */
  3254. for (i = 0; i < ic->nb_streams; i++) {
  3255. int fps_analyze_framecount = 20;
  3256. int count;
  3257. st = ic->streams[i];
  3258. if (!has_codec_parameters(st, NULL))
  3259. break;
  3260. /* If the timebase is coarse (like the usual millisecond precision
  3261. * of mkv), we need to analyze more frames to reliably arrive at
  3262. * the correct fps. */
  3263. if (av_q2d(st->time_base) > 0.0005)
  3264. fps_analyze_framecount *= 2;
  3265. if (!tb_unreliable(st->internal->avctx))
  3266. fps_analyze_framecount = 0;
  3267. if (ic->fps_probe_size >= 0)
  3268. fps_analyze_framecount = ic->fps_probe_size;
  3269. if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
  3270. fps_analyze_framecount = 0;
  3271. /* variable fps and no guess at the real fps */
  3272. count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
  3273. st->internal->info->codec_info_duration_fields/2 :
  3274. st->internal->info->duration_count;
  3275. if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
  3276. st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3277. if (count < fps_analyze_framecount)
  3278. break;
  3279. }
  3280. // Look at the first 3 frames if there is evidence of frame delay
  3281. // but the decoder delay is not set.
  3282. if (st->internal->info->frame_delay_evidence && count < 2 && st->internal->avctx->has_b_frames == 0)
  3283. break;
  3284. if (!st->internal->avctx->extradata &&
  3285. (!st->internal->extract_extradata.inited ||
  3286. st->internal->extract_extradata.bsf) &&
  3287. extract_extradata_check(st))
  3288. break;
  3289. if (st->first_dts == AV_NOPTS_VALUE &&
  3290. !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
  3291. st->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
  3292. (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
  3293. st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
  3294. break;
  3295. }
  3296. analyzed_all_streams = 0;
  3297. if (!missing_streams || !*missing_streams)
  3298. if (i == ic->nb_streams) {
  3299. analyzed_all_streams = 1;
  3300. /* NOTE: If the format has no header, then we need to read some
  3301. * packets to get most of the streams, so we cannot stop here. */
  3302. if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
  3303. /* If we found the info for all the codecs, we can stop. */
  3304. ret = count;
  3305. av_log(ic, AV_LOG_DEBUG, "All info found\n");
  3306. flush_codecs = 0;
  3307. break;
  3308. }
  3309. }
  3310. /* We did not get all the codec info, but we read too much data. */
  3311. if (read_size >= probesize) {
  3312. ret = count;
  3313. av_log(ic, AV_LOG_DEBUG,
  3314. "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
  3315. for (i = 0; i < ic->nb_streams; i++)
  3316. if (!ic->streams[i]->r_frame_rate.num &&
  3317. ic->streams[i]->internal->info->duration_count <= 1 &&
  3318. ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  3319. strcmp(ic->iformat->name, "image2"))
  3320. av_log(ic, AV_LOG_WARNING,
  3321. "Stream #%d: not enough frames to estimate rate; "
  3322. "consider increasing probesize\n", i);
  3323. break;
  3324. }
  3325. /* NOTE: A new stream can be added there if no header in file
  3326. * (AVFMTCTX_NOHEADER). */
  3327. ret = read_frame_internal(ic, pkt1);
  3328. if (ret == AVERROR(EAGAIN))
  3329. continue;
  3330. if (ret < 0) {
  3331. /* EOF or error*/
  3332. eof_reached = 1;
  3333. break;
  3334. }
  3335. if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
  3336. ret = avpriv_packet_list_put(&ic->internal->packet_buffer,
  3337. &ic->internal->packet_buffer_end,
  3338. pkt1, NULL, 0);
  3339. if (ret < 0)
  3340. goto unref_then_goto_end;
  3341. pkt = &ic->internal->packet_buffer_end->pkt;
  3342. } else {
  3343. pkt = pkt1;
  3344. }
  3345. st = ic->streams[pkt->stream_index];
  3346. if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  3347. read_size += pkt->size;
  3348. avctx = st->internal->avctx;
  3349. if (!st->internal->avctx_inited) {
  3350. ret = avcodec_parameters_to_context(avctx, st->codecpar);
  3351. if (ret < 0)
  3352. goto unref_then_goto_end;
  3353. st->internal->avctx_inited = 1;
  3354. }
  3355. if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
  3356. /* check for non-increasing dts */
  3357. if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&
  3358. st->internal->info->fps_last_dts >= pkt->dts) {
  3359. av_log(ic, AV_LOG_DEBUG,
  3360. "Non-increasing DTS in stream %d: packet %d with DTS "
  3361. "%"PRId64", packet %d with DTS %"PRId64"\n",
  3362. st->index, st->internal->info->fps_last_dts_idx,
  3363. st->internal->info->fps_last_dts, st->codec_info_nb_frames,
  3364. pkt->dts);
  3365. st->internal->info->fps_first_dts =
  3366. st->internal->info->fps_last_dts = AV_NOPTS_VALUE;
  3367. }
  3368. /* Check for a discontinuity in dts. If the difference in dts
  3369. * is more than 1000 times the average packet duration in the
  3370. * sequence, we treat it as a discontinuity. */
  3371. if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&
  3372. st->internal->info->fps_last_dts_idx > st->internal->info->fps_first_dts_idx &&
  3373. (pkt->dts - (uint64_t)st->internal->info->fps_last_dts) / 1000 >
  3374. (st->internal->info->fps_last_dts - (uint64_t)st->internal->info->fps_first_dts) /
  3375. (st->internal->info->fps_last_dts_idx - st->internal->info->fps_first_dts_idx)) {
  3376. av_log(ic, AV_LOG_WARNING,
  3377. "DTS discontinuity in stream %d: packet %d with DTS "
  3378. "%"PRId64", packet %d with DTS %"PRId64"\n",
  3379. st->index, st->internal->info->fps_last_dts_idx,
  3380. st->internal->info->fps_last_dts, st->codec_info_nb_frames,
  3381. pkt->dts);
  3382. st->internal->info->fps_first_dts =
  3383. st->internal->info->fps_last_dts = AV_NOPTS_VALUE;
  3384. }
  3385. /* update stored dts values */
  3386. if (st->internal->info->fps_first_dts == AV_NOPTS_VALUE) {
  3387. st->internal->info->fps_first_dts = pkt->dts;
  3388. st->internal->info->fps_first_dts_idx = st->codec_info_nb_frames;
  3389. }
  3390. st->internal->info->fps_last_dts = pkt->dts;
  3391. st->internal->info->fps_last_dts_idx = st->codec_info_nb_frames;
  3392. }
  3393. if (st->codec_info_nb_frames>1) {
  3394. int64_t t = 0;
  3395. int64_t limit;
  3396. if (st->time_base.den > 0)
  3397. t = av_rescale_q(st->internal->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
  3398. if (st->avg_frame_rate.num > 0)
  3399. t = FFMAX(t, av_rescale_q(st->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
  3400. if ( t == 0
  3401. && st->codec_info_nb_frames>30
  3402. && st->internal->info->fps_first_dts != AV_NOPTS_VALUE
  3403. && st->internal->info->fps_last_dts != AV_NOPTS_VALUE) {
  3404. int64_t dur = av_sat_sub64(st->internal->info->fps_last_dts, st->internal->info->fps_first_dts);
  3405. t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
  3406. }
  3407. if (analyzed_all_streams) limit = max_analyze_duration;
  3408. else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
  3409. else limit = max_stream_analyze_duration;
  3410. if (t >= limit) {
  3411. av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
  3412. limit,
  3413. t, pkt->stream_index);
  3414. if (ic->flags & AVFMT_FLAG_NOBUFFER)
  3415. av_packet_unref(pkt1);
  3416. break;
  3417. }
  3418. if (pkt->duration) {
  3419. if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time) {
  3420. st->internal->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, st->internal->info->codec_info_duration + pkt->duration);
  3421. } else
  3422. st->internal->info->codec_info_duration += pkt->duration;
  3423. st->internal->info->codec_info_duration_fields += st->parser && st->need_parsing && avctx->ticks_per_frame ==2 ? st->parser->repeat_pict + 1 : 2;
  3424. }
  3425. }
  3426. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3427. #if FF_API_R_FRAME_RATE
  3428. ff_rfps_add_frame(ic, st, pkt->dts);
  3429. #endif
  3430. if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
  3431. st->internal->info->frame_delay_evidence = 1;
  3432. }
  3433. if (!st->internal->avctx->extradata) {
  3434. ret = extract_extradata(st, pkt);
  3435. if (ret < 0)
  3436. goto unref_then_goto_end;
  3437. }
  3438. /* If still no information, we try to open the codec and to
  3439. * decompress the frame. We try to avoid that in most cases as
  3440. * it takes longer and uses more memory. For MPEG-4, we need to
  3441. * decompress for QuickTime.
  3442. *
  3443. * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
  3444. * least one frame of codec data, this makes sure the codec initializes
  3445. * the channel configuration and does not only trust the values from
  3446. * the container. */
  3447. try_decode_frame(ic, st, pkt,
  3448. (options && i < orig_nb_streams) ? &options[i] : NULL);
  3449. if (ic->flags & AVFMT_FLAG_NOBUFFER)
  3450. av_packet_unref(pkt1);
  3451. st->codec_info_nb_frames++;
  3452. count++;
  3453. }
  3454. if (eof_reached) {
  3455. int stream_index;
  3456. for (stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
  3457. st = ic->streams[stream_index];
  3458. avctx = st->internal->avctx;
  3459. if (!has_codec_parameters(st, NULL)) {
  3460. const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
  3461. if (codec && !avctx->codec) {
  3462. AVDictionary *opts = NULL;
  3463. if (ic->codec_whitelist)
  3464. av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
  3465. if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
  3466. av_log(ic, AV_LOG_WARNING,
  3467. "Failed to open codec in %s\n",__FUNCTION__);
  3468. av_dict_free(&opts);
  3469. }
  3470. }
  3471. // EOF already reached while reading the stream above.
  3472. // So continue with reoordering DTS with whatever delay we have.
  3473. if (ic->internal->packet_buffer && !has_decode_delay_been_guessed(st)) {
  3474. update_dts_from_pts(ic, stream_index, ic->internal->packet_buffer);
  3475. }
  3476. }
  3477. }
  3478. if (flush_codecs) {
  3479. AVPacket *empty_pkt = ic->internal->pkt;
  3480. int err = 0;
  3481. av_packet_unref(empty_pkt);
  3482. for (i = 0; i < ic->nb_streams; i++) {
  3483. st = ic->streams[i];
  3484. /* flush the decoders */
  3485. if (st->internal->info->found_decoder == 1) {
  3486. do {
  3487. err = try_decode_frame(ic, st, empty_pkt,
  3488. (options && i < orig_nb_streams)
  3489. ? &options[i] : NULL);
  3490. } while (err > 0 && !has_codec_parameters(st, NULL));
  3491. if (err < 0) {
  3492. av_log(ic, AV_LOG_INFO,
  3493. "decoding for stream %d failed\n", st->index);
  3494. }
  3495. }
  3496. }
  3497. }
  3498. ff_rfps_calculate(ic);
  3499. for (i = 0; i < ic->nb_streams; i++) {
  3500. st = ic->streams[i];
  3501. avctx = st->internal->avctx;
  3502. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  3503. if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
  3504. uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
  3505. if (avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), tag) == avctx->pix_fmt)
  3506. avctx->codec_tag= tag;
  3507. }
  3508. /* estimate average framerate if not set by demuxer */
  3509. if (st->internal->info->codec_info_duration_fields &&
  3510. !st->avg_frame_rate.num &&
  3511. st->internal->info->codec_info_duration) {
  3512. int best_fps = 0;
  3513. double best_error = 0.01;
  3514. AVRational codec_frame_rate = avctx->framerate;
  3515. if (st->internal->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
  3516. st->internal->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
  3517. st->internal->info->codec_info_duration < 0)
  3518. continue;
  3519. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  3520. st->internal->info->codec_info_duration_fields * (int64_t) st->time_base.den,
  3521. st->internal->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
  3522. /* Round guessed framerate to a "standard" framerate if it's
  3523. * within 1% of the original estimate. */
  3524. for (j = 0; j < MAX_STD_TIMEBASES; j++) {
  3525. AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
  3526. double error = fabs(av_q2d(st->avg_frame_rate) /
  3527. av_q2d(std_fps) - 1);
  3528. if (error < best_error) {
  3529. best_error = error;
  3530. best_fps = std_fps.num;
  3531. }
  3532. if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
  3533. error = fabs(av_q2d(codec_frame_rate) /
  3534. av_q2d(std_fps) - 1);
  3535. if (error < best_error) {
  3536. best_error = error;
  3537. best_fps = std_fps.num;
  3538. }
  3539. }
  3540. }
  3541. if (best_fps)
  3542. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  3543. best_fps, 12 * 1001, INT_MAX);
  3544. }
  3545. if (!st->r_frame_rate.num) {
  3546. if ( avctx->time_base.den * (int64_t) st->time_base.num
  3547. <= avctx->time_base.num * avctx->ticks_per_frame * (uint64_t) st->time_base.den) {
  3548. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  3549. avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);
  3550. } else {
  3551. st->r_frame_rate.num = st->time_base.den;
  3552. st->r_frame_rate.den = st->time_base.num;
  3553. }
  3554. }
  3555. if (st->internal->display_aspect_ratio.num && st->internal->display_aspect_ratio.den) {
  3556. AVRational hw_ratio = { avctx->height, avctx->width };
  3557. st->sample_aspect_ratio = av_mul_q(st->internal->display_aspect_ratio,
  3558. hw_ratio);
  3559. }
  3560. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  3561. if (!avctx->bits_per_coded_sample)
  3562. avctx->bits_per_coded_sample =
  3563. av_get_bits_per_sample(avctx->codec_id);
  3564. // set stream disposition based on audio service type
  3565. switch (avctx->audio_service_type) {
  3566. case AV_AUDIO_SERVICE_TYPE_EFFECTS:
  3567. st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
  3568. break;
  3569. case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
  3570. st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
  3571. break;
  3572. case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
  3573. st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
  3574. break;
  3575. case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
  3576. st->disposition = AV_DISPOSITION_COMMENT;
  3577. break;
  3578. case AV_AUDIO_SERVICE_TYPE_KARAOKE:
  3579. st->disposition = AV_DISPOSITION_KARAOKE;
  3580. break;
  3581. }
  3582. }
  3583. }
  3584. if (probesize)
  3585. estimate_timings(ic, old_offset);
  3586. av_opt_set(ic, "skip_clear", "0", AV_OPT_SEARCH_CHILDREN);
  3587. if (ret >= 0 && ic->nb_streams)
  3588. /* We could not have all the codec parameters before EOF. */
  3589. ret = -1;
  3590. for (i = 0; i < ic->nb_streams; i++) {
  3591. const char *errmsg;
  3592. st = ic->streams[i];
  3593. /* if no packet was ever seen, update context now for has_codec_parameters */
  3594. if (!st->internal->avctx_inited) {
  3595. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  3596. st->codecpar->format == AV_SAMPLE_FMT_NONE)
  3597. st->codecpar->format = st->internal->avctx->sample_fmt;
  3598. ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
  3599. if (ret < 0)
  3600. goto find_stream_info_err;
  3601. }
  3602. if (!has_codec_parameters(st, &errmsg)) {
  3603. char buf[256];
  3604. avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
  3605. av_log(ic, AV_LOG_WARNING,
  3606. "Could not find codec parameters for stream %d (%s): %s\n"
  3607. "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
  3608. i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
  3609. } else {
  3610. ret = 0;
  3611. }
  3612. }
  3613. ret = compute_chapters_end(ic);
  3614. if (ret < 0)
  3615. goto find_stream_info_err;
  3616. /* update the stream parameters from the internal codec contexts */
  3617. for (i = 0; i < ic->nb_streams; i++) {
  3618. st = ic->streams[i];
  3619. if (st->internal->avctx_inited) {
  3620. ret = avcodec_parameters_from_context(st->codecpar, st->internal->avctx);
  3621. if (ret < 0)
  3622. goto find_stream_info_err;
  3623. ret = add_coded_side_data(st, st->internal->avctx);
  3624. if (ret < 0)
  3625. goto find_stream_info_err;
  3626. }
  3627. #if FF_API_LAVF_AVCTX
  3628. FF_DISABLE_DEPRECATION_WARNINGS
  3629. ret = avcodec_parameters_to_context(st->codec, st->codecpar);
  3630. if (ret < 0)
  3631. goto find_stream_info_err;
  3632. // The old API (AVStream.codec) "requires" the resolution to be adjusted
  3633. // by the lowres factor.
  3634. if (st->internal->avctx->lowres && st->internal->avctx->width) {
  3635. st->codec->lowres = st->internal->avctx->lowres;
  3636. st->codec->width = st->internal->avctx->width;
  3637. st->codec->height = st->internal->avctx->height;
  3638. }
  3639. if (st->codec->codec_tag != MKTAG('t','m','c','d')) {
  3640. st->codec->time_base = st->internal->avctx->time_base;
  3641. st->codec->ticks_per_frame = st->internal->avctx->ticks_per_frame;
  3642. }
  3643. st->codec->framerate = st->avg_frame_rate;
  3644. if (st->internal->avctx->subtitle_header) {
  3645. st->codec->subtitle_header = av_malloc(st->internal->avctx->subtitle_header_size);
  3646. if (!st->codec->subtitle_header)
  3647. goto find_stream_info_err;
  3648. st->codec->subtitle_header_size = st->internal->avctx->subtitle_header_size;
  3649. memcpy(st->codec->subtitle_header, st->internal->avctx->subtitle_header,
  3650. st->codec->subtitle_header_size);
  3651. }
  3652. // Fields unavailable in AVCodecParameters
  3653. st->codec->coded_width = st->internal->avctx->coded_width;
  3654. st->codec->coded_height = st->internal->avctx->coded_height;
  3655. st->codec->properties = st->internal->avctx->properties;
  3656. FF_ENABLE_DEPRECATION_WARNINGS
  3657. #endif
  3658. st->internal->avctx_inited = 0;
  3659. }
  3660. find_stream_info_err:
  3661. for (i = 0; i < ic->nb_streams; i++) {
  3662. st = ic->streams[i];
  3663. if (st->internal->info)
  3664. av_freep(&st->internal->info->duration_error);
  3665. avcodec_close(ic->streams[i]->internal->avctx);
  3666. av_freep(&ic->streams[i]->internal->info);
  3667. av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
  3668. av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);
  3669. }
  3670. if (ic->pb)
  3671. av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
  3672. avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, count);
  3673. return ret;
  3674. unref_then_goto_end:
  3675. av_packet_unref(pkt1);
  3676. goto find_stream_info_err;
  3677. }
  3678. AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
  3679. {
  3680. int i, j;
  3681. for (i = 0; i < ic->nb_programs; i++) {
  3682. if (ic->programs[i] == last) {
  3683. last = NULL;
  3684. } else {
  3685. if (!last)
  3686. for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
  3687. if (ic->programs[i]->stream_index[j] == s)
  3688. return ic->programs[i];
  3689. }
  3690. }
  3691. return NULL;
  3692. }
  3693. int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
  3694. int wanted_stream_nb, int related_stream,
  3695. AVCodec **decoder_ret, int flags)
  3696. {
  3697. int i, nb_streams = ic->nb_streams;
  3698. int ret = AVERROR_STREAM_NOT_FOUND;
  3699. int best_count = -1, best_multiframe = -1, best_disposition = -1;
  3700. int count, multiframe, disposition;
  3701. int64_t best_bitrate = -1;
  3702. int64_t bitrate;
  3703. unsigned *program = NULL;
  3704. const AVCodec *decoder = NULL, *best_decoder = NULL;
  3705. if (related_stream >= 0 && wanted_stream_nb < 0) {
  3706. AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
  3707. if (p) {
  3708. program = p->stream_index;
  3709. nb_streams = p->nb_stream_indexes;
  3710. }
  3711. }
  3712. for (i = 0; i < nb_streams; i++) {
  3713. int real_stream_index = program ? program[i] : i;
  3714. AVStream *st = ic->streams[real_stream_index];
  3715. AVCodecParameters *par = st->codecpar;
  3716. if (par->codec_type != type)
  3717. continue;
  3718. if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
  3719. continue;
  3720. if (type == AVMEDIA_TYPE_AUDIO && !(par->channels && par->sample_rate))
  3721. continue;
  3722. if (decoder_ret) {
  3723. decoder = find_decoder(ic, st, par->codec_id);
  3724. if (!decoder) {
  3725. if (ret < 0)
  3726. ret = AVERROR_DECODER_NOT_FOUND;
  3727. continue;
  3728. }
  3729. }
  3730. disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED))
  3731. + !! (st->disposition & AV_DISPOSITION_DEFAULT);
  3732. count = st->codec_info_nb_frames;
  3733. bitrate = par->bit_rate;
  3734. multiframe = FFMIN(5, count);
  3735. if ((best_disposition > disposition) ||
  3736. (best_disposition == disposition && best_multiframe > multiframe) ||
  3737. (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) ||
  3738. (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
  3739. continue;
  3740. best_disposition = disposition;
  3741. best_count = count;
  3742. best_bitrate = bitrate;
  3743. best_multiframe = multiframe;
  3744. ret = real_stream_index;
  3745. best_decoder = decoder;
  3746. if (program && i == nb_streams - 1 && ret < 0) {
  3747. program = NULL;
  3748. nb_streams = ic->nb_streams;
  3749. /* no related stream found, try again with everything */
  3750. i = 0;
  3751. }
  3752. }
  3753. if (decoder_ret)
  3754. *decoder_ret = (AVCodec*)best_decoder;
  3755. return ret;
  3756. }
  3757. /*******************************************************/
  3758. int av_read_play(AVFormatContext *s)
  3759. {
  3760. if (s->iformat->read_play)
  3761. return s->iformat->read_play(s);
  3762. if (s->pb)
  3763. return avio_pause(s->pb, 0);
  3764. return AVERROR(ENOSYS);
  3765. }
  3766. int av_read_pause(AVFormatContext *s)
  3767. {
  3768. if (s->iformat->read_pause)
  3769. return s->iformat->read_pause(s);
  3770. if (s->pb)
  3771. return avio_pause(s->pb, 1);
  3772. return AVERROR(ENOSYS);
  3773. }
  3774. int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
  3775. {
  3776. int ret, i;
  3777. dst->id = src->id;
  3778. dst->time_base = src->time_base;
  3779. dst->nb_frames = src->nb_frames;
  3780. dst->disposition = src->disposition;
  3781. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  3782. dst->avg_frame_rate = src->avg_frame_rate;
  3783. dst->r_frame_rate = src->r_frame_rate;
  3784. av_dict_free(&dst->metadata);
  3785. ret = av_dict_copy(&dst->metadata, src->metadata, 0);
  3786. if (ret < 0)
  3787. return ret;
  3788. ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
  3789. if (ret < 0)
  3790. return ret;
  3791. /* Free existing side data*/
  3792. for (i = 0; i < dst->nb_side_data; i++)
  3793. av_free(dst->side_data[i].data);
  3794. av_freep(&dst->side_data);
  3795. dst->nb_side_data = 0;
  3796. /* Copy side data if present */
  3797. if (src->nb_side_data) {
  3798. dst->side_data = av_mallocz_array(src->nb_side_data,
  3799. sizeof(AVPacketSideData));
  3800. if (!dst->side_data)
  3801. return AVERROR(ENOMEM);
  3802. dst->nb_side_data = src->nb_side_data;
  3803. for (i = 0; i < src->nb_side_data; i++) {
  3804. uint8_t *data = av_memdup(src->side_data[i].data,
  3805. src->side_data[i].size);
  3806. if (!data)
  3807. return AVERROR(ENOMEM);
  3808. dst->side_data[i].type = src->side_data[i].type;
  3809. dst->side_data[i].size = src->side_data[i].size;
  3810. dst->side_data[i].data = data;
  3811. }
  3812. }
  3813. #if FF_API_LAVF_FFSERVER
  3814. FF_DISABLE_DEPRECATION_WARNINGS
  3815. av_freep(&dst->recommended_encoder_configuration);
  3816. if (src->recommended_encoder_configuration) {
  3817. const char *conf_str = src->recommended_encoder_configuration;
  3818. dst->recommended_encoder_configuration = av_strdup(conf_str);
  3819. if (!dst->recommended_encoder_configuration)
  3820. return AVERROR(ENOMEM);
  3821. }
  3822. FF_ENABLE_DEPRECATION_WARNINGS
  3823. #endif
  3824. return 0;
  3825. }
  3826. static void free_stream(AVStream **pst)
  3827. {
  3828. AVStream *st = *pst;
  3829. int i;
  3830. if (!st)
  3831. return;
  3832. for (i = 0; i < st->nb_side_data; i++)
  3833. av_freep(&st->side_data[i].data);
  3834. av_freep(&st->side_data);
  3835. if (st->parser)
  3836. av_parser_close(st->parser);
  3837. if (st->attached_pic.data)
  3838. av_packet_unref(&st->attached_pic);
  3839. if (st->internal) {
  3840. avcodec_free_context(&st->internal->avctx);
  3841. av_bsf_free(&st->internal->bsfc);
  3842. av_freep(&st->internal->priv_pts);
  3843. av_freep(&st->internal->index_entries);
  3844. av_freep(&st->internal->probe_data.buf);
  3845. av_bsf_free(&st->internal->extract_extradata.bsf);
  3846. av_packet_free(&st->internal->extract_extradata.pkt);
  3847. if (st->internal->info)
  3848. av_freep(&st->internal->info->duration_error);
  3849. av_freep(&st->internal->info);
  3850. }
  3851. av_freep(&st->internal);
  3852. av_dict_free(&st->metadata);
  3853. avcodec_parameters_free(&st->codecpar);
  3854. #if FF_API_LAVF_AVCTX
  3855. FF_DISABLE_DEPRECATION_WARNINGS
  3856. avcodec_free_context(&st->codec);
  3857. FF_ENABLE_DEPRECATION_WARNINGS
  3858. #endif
  3859. av_freep(&st->priv_data);
  3860. #if FF_API_LAVF_FFSERVER
  3861. FF_DISABLE_DEPRECATION_WARNINGS
  3862. av_freep(&st->recommended_encoder_configuration);
  3863. FF_ENABLE_DEPRECATION_WARNINGS
  3864. #endif
  3865. av_freep(pst);
  3866. }
  3867. void ff_free_stream(AVFormatContext *s, AVStream *st)
  3868. {
  3869. av_assert0(s->nb_streams>0);
  3870. av_assert0(s->streams[ s->nb_streams - 1 ] == st);
  3871. free_stream(&s->streams[ --s->nb_streams ]);
  3872. }
  3873. void avformat_free_context(AVFormatContext *s)
  3874. {
  3875. int i;
  3876. if (!s)
  3877. return;
  3878. if (s->oformat && s->oformat->deinit && s->internal->initialized)
  3879. s->oformat->deinit(s);
  3880. av_opt_free(s);
  3881. if (s->iformat && s->iformat->priv_class && s->priv_data)
  3882. av_opt_free(s->priv_data);
  3883. if (s->oformat && s->oformat->priv_class && s->priv_data)
  3884. av_opt_free(s->priv_data);
  3885. for (i = 0; i < s->nb_streams; i++)
  3886. free_stream(&s->streams[i]);
  3887. s->nb_streams = 0;
  3888. for (i = 0; i < s->nb_programs; i++) {
  3889. av_dict_free(&s->programs[i]->metadata);
  3890. av_freep(&s->programs[i]->stream_index);
  3891. av_freep(&s->programs[i]);
  3892. }
  3893. s->nb_programs = 0;
  3894. av_freep(&s->programs);
  3895. av_freep(&s->priv_data);
  3896. while (s->nb_chapters--) {
  3897. av_dict_free(&s->chapters[s->nb_chapters]->metadata);
  3898. av_freep(&s->chapters[s->nb_chapters]);
  3899. }
  3900. av_freep(&s->chapters);
  3901. av_dict_free(&s->metadata);
  3902. av_dict_free(&s->internal->id3v2_meta);
  3903. av_packet_free(&s->internal->pkt);
  3904. av_packet_free(&s->internal->parse_pkt);
  3905. av_freep(&s->streams);
  3906. flush_packet_queue(s);
  3907. av_freep(&s->internal);
  3908. av_freep(&s->url);
  3909. av_free(s);
  3910. }
  3911. void avformat_close_input(AVFormatContext **ps)
  3912. {
  3913. AVFormatContext *s;
  3914. AVIOContext *pb;
  3915. if (!ps || !*ps)
  3916. return;
  3917. s = *ps;
  3918. pb = s->pb;
  3919. if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
  3920. (s->flags & AVFMT_FLAG_CUSTOM_IO))
  3921. pb = NULL;
  3922. flush_packet_queue(s);
  3923. if (s->iformat)
  3924. if (s->iformat->read_close)
  3925. s->iformat->read_close(s);
  3926. avformat_free_context(s);
  3927. *ps = NULL;
  3928. avio_close(pb);
  3929. }
  3930. AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
  3931. {
  3932. AVStream *st;
  3933. int i;
  3934. AVStream **streams;
  3935. if (s->nb_streams >= FFMIN(s->max_streams, INT_MAX/sizeof(*streams))) {
  3936. if (s->max_streams < INT_MAX/sizeof(*streams))
  3937. av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter (%d), see the documentation if you wish to increase it\n", s->max_streams);
  3938. return NULL;
  3939. }
  3940. streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
  3941. if (!streams)
  3942. return NULL;
  3943. s->streams = streams;
  3944. st = av_mallocz(sizeof(AVStream));
  3945. if (!st)
  3946. return NULL;
  3947. #if FF_API_LAVF_AVCTX
  3948. FF_DISABLE_DEPRECATION_WARNINGS
  3949. st->codec = avcodec_alloc_context3(c);
  3950. if (!st->codec) {
  3951. av_free(st);
  3952. return NULL;
  3953. }
  3954. FF_ENABLE_DEPRECATION_WARNINGS
  3955. #endif
  3956. st->internal = av_mallocz(sizeof(*st->internal));
  3957. if (!st->internal)
  3958. goto fail;
  3959. st->internal->info = av_mallocz(sizeof(*st->internal->info));
  3960. if (!st->internal->info)
  3961. goto fail;
  3962. st->internal->info->last_dts = AV_NOPTS_VALUE;
  3963. st->codecpar = avcodec_parameters_alloc();
  3964. if (!st->codecpar)
  3965. goto fail;
  3966. st->internal->avctx = avcodec_alloc_context3(NULL);
  3967. if (!st->internal->avctx)
  3968. goto fail;
  3969. if (s->iformat) {
  3970. #if FF_API_LAVF_AVCTX
  3971. FF_DISABLE_DEPRECATION_WARNINGS
  3972. /* no default bitrate if decoding */
  3973. st->codec->bit_rate = 0;
  3974. FF_ENABLE_DEPRECATION_WARNINGS
  3975. #endif
  3976. /* default pts setting is MPEG-like */
  3977. avpriv_set_pts_info(st, 33, 1, 90000);
  3978. /* we set the current DTS to 0 so that formats without any timestamps
  3979. * but durations get some timestamps, formats with some unknown
  3980. * timestamps have their first few packets buffered and the
  3981. * timestamps corrected before they are returned to the user */
  3982. st->cur_dts = RELATIVE_TS_BASE;
  3983. } else {
  3984. st->cur_dts = AV_NOPTS_VALUE;
  3985. }
  3986. st->index = s->nb_streams;
  3987. st->start_time = AV_NOPTS_VALUE;
  3988. st->duration = AV_NOPTS_VALUE;
  3989. st->first_dts = AV_NOPTS_VALUE;
  3990. st->probe_packets = s->max_probe_packets;
  3991. st->internal->pts_wrap_reference = AV_NOPTS_VALUE;
  3992. st->internal->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
  3993. st->last_IP_pts = AV_NOPTS_VALUE;
  3994. st->internal->last_dts_for_order_check = AV_NOPTS_VALUE;
  3995. for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
  3996. st->internal->pts_buffer[i] = AV_NOPTS_VALUE;
  3997. st->sample_aspect_ratio = (AVRational) { 0, 1 };
  3998. #if FF_API_R_FRAME_RATE
  3999. st->internal->info->last_dts = AV_NOPTS_VALUE;
  4000. #endif
  4001. st->internal->info->fps_first_dts = AV_NOPTS_VALUE;
  4002. st->internal->info->fps_last_dts = AV_NOPTS_VALUE;
  4003. st->internal->inject_global_side_data = s->internal->inject_global_side_data;
  4004. st->internal->need_context_update = 1;
  4005. s->streams[s->nb_streams++] = st;
  4006. return st;
  4007. fail:
  4008. free_stream(&st);
  4009. return NULL;
  4010. }
  4011. AVProgram *av_new_program(AVFormatContext *ac, int id)
  4012. {
  4013. AVProgram *program = NULL;
  4014. int i;
  4015. av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
  4016. for (i = 0; i < ac->nb_programs; i++)
  4017. if (ac->programs[i]->id == id)
  4018. program = ac->programs[i];
  4019. if (!program) {
  4020. program = av_mallocz(sizeof(AVProgram));
  4021. if (!program)
  4022. return NULL;
  4023. dynarray_add(&ac->programs, &ac->nb_programs, program);
  4024. program->discard = AVDISCARD_NONE;
  4025. program->pmt_version = -1;
  4026. program->id = id;
  4027. program->pts_wrap_reference = AV_NOPTS_VALUE;
  4028. program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
  4029. program->start_time =
  4030. program->end_time = AV_NOPTS_VALUE;
  4031. }
  4032. return program;
  4033. }
  4034. AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
  4035. int64_t start, int64_t end, const char *title)
  4036. {
  4037. AVChapter *chapter = NULL;
  4038. int i;
  4039. if (end != AV_NOPTS_VALUE && start > end) {
  4040. av_log(s, AV_LOG_ERROR, "Chapter end time %"PRId64" before start %"PRId64"\n", end, start);
  4041. return NULL;
  4042. }
  4043. if (!s->nb_chapters) {
  4044. s->internal->chapter_ids_monotonic = 1;
  4045. } else if (!s->internal->chapter_ids_monotonic || s->chapters[s->nb_chapters-1]->id >= id) {
  4046. s->internal->chapter_ids_monotonic = 0;
  4047. for (i = 0; i < s->nb_chapters; i++)
  4048. if (s->chapters[i]->id == id)
  4049. chapter = s->chapters[i];
  4050. }
  4051. if (!chapter) {
  4052. chapter = av_mallocz(sizeof(AVChapter));
  4053. if (!chapter)
  4054. return NULL;
  4055. dynarray_add(&s->chapters, &s->nb_chapters, chapter);
  4056. }
  4057. av_dict_set(&chapter->metadata, "title", title, 0);
  4058. chapter->id = id;
  4059. chapter->time_base = time_base;
  4060. chapter->start = start;
  4061. chapter->end = end;
  4062. return chapter;
  4063. }
  4064. void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
  4065. {
  4066. int i, j;
  4067. AVProgram *program = NULL;
  4068. void *tmp;
  4069. if (idx >= ac->nb_streams) {
  4070. av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
  4071. return;
  4072. }
  4073. for (i = 0; i < ac->nb_programs; i++) {
  4074. if (ac->programs[i]->id != progid)
  4075. continue;
  4076. program = ac->programs[i];
  4077. for (j = 0; j < program->nb_stream_indexes; j++)
  4078. if (program->stream_index[j] == idx)
  4079. return;
  4080. tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
  4081. if (!tmp)
  4082. return;
  4083. program->stream_index = tmp;
  4084. program->stream_index[program->nb_stream_indexes++] = idx;
  4085. return;
  4086. }
  4087. }
  4088. uint64_t ff_ntp_time(void)
  4089. {
  4090. return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
  4091. }
  4092. uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
  4093. {
  4094. uint64_t ntp_ts, frac_part, sec;
  4095. uint32_t usec;
  4096. //current ntp time in seconds and micro seconds
  4097. sec = ntp_time_us / 1000000;
  4098. usec = ntp_time_us % 1000000;
  4099. //encoding in ntp timestamp format
  4100. frac_part = usec * 0xFFFFFFFFULL;
  4101. frac_part /= 1000000;
  4102. if (sec > 0xFFFFFFFFULL)
  4103. av_log(NULL, AV_LOG_WARNING, "NTP time format roll over detected\n");
  4104. ntp_ts = sec << 32;
  4105. ntp_ts |= frac_part;
  4106. return ntp_ts;
  4107. }
  4108. int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
  4109. {
  4110. const char *p;
  4111. char *q, buf1[20], c;
  4112. int nd, len, percentd_found;
  4113. q = buf;
  4114. p = path;
  4115. percentd_found = 0;
  4116. for (;;) {
  4117. c = *p++;
  4118. if (c == '\0')
  4119. break;
  4120. if (c == '%') {
  4121. do {
  4122. nd = 0;
  4123. while (av_isdigit(*p)) {
  4124. if (nd >= INT_MAX / 10 - 255)
  4125. goto fail;
  4126. nd = nd * 10 + *p++ - '0';
  4127. }
  4128. c = *p++;
  4129. } while (av_isdigit(c));
  4130. switch (c) {
  4131. case '%':
  4132. goto addchar;
  4133. case 'd':
  4134. if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) && percentd_found)
  4135. goto fail;
  4136. percentd_found = 1;
  4137. if (number < 0)
  4138. nd += 1;
  4139. snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
  4140. len = strlen(buf1);
  4141. if ((q - buf + len) > buf_size - 1)
  4142. goto fail;
  4143. memcpy(q, buf1, len);
  4144. q += len;
  4145. break;
  4146. default:
  4147. goto fail;
  4148. }
  4149. } else {
  4150. addchar:
  4151. if ((q - buf) < buf_size - 1)
  4152. *q++ = c;
  4153. }
  4154. }
  4155. if (!percentd_found)
  4156. goto fail;
  4157. *q = '\0';
  4158. return 0;
  4159. fail:
  4160. *q = '\0';
  4161. return -1;
  4162. }
  4163. int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
  4164. {
  4165. return av_get_frame_filename2(buf, buf_size, path, number, 0);
  4166. }
  4167. void av_url_split(char *proto, int proto_size,
  4168. char *authorization, int authorization_size,
  4169. char *hostname, int hostname_size,
  4170. int *port_ptr, char *path, int path_size, const char *url)
  4171. {
  4172. const char *p, *ls, *at, *at2, *col, *brk;
  4173. if (port_ptr)
  4174. *port_ptr = -1;
  4175. if (proto_size > 0)
  4176. proto[0] = 0;
  4177. if (authorization_size > 0)
  4178. authorization[0] = 0;
  4179. if (hostname_size > 0)
  4180. hostname[0] = 0;
  4181. if (path_size > 0)
  4182. path[0] = 0;
  4183. /* parse protocol */
  4184. if ((p = strchr(url, ':'))) {
  4185. av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
  4186. p++; /* skip ':' */
  4187. if (*p == '/')
  4188. p++;
  4189. if (*p == '/')
  4190. p++;
  4191. } else {
  4192. /* no protocol means plain filename */
  4193. av_strlcpy(path, url, path_size);
  4194. return;
  4195. }
  4196. /* separate path from hostname */
  4197. ls = p + strcspn(p, "/?#");
  4198. av_strlcpy(path, ls, path_size);
  4199. /* the rest is hostname, use that to parse auth/port */
  4200. if (ls != p) {
  4201. /* authorization (user[:pass]@hostname) */
  4202. at2 = p;
  4203. while ((at = strchr(p, '@')) && at < ls) {
  4204. av_strlcpy(authorization, at2,
  4205. FFMIN(authorization_size, at + 1 - at2));
  4206. p = at + 1; /* skip '@' */
  4207. }
  4208. if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
  4209. /* [host]:port */
  4210. av_strlcpy(hostname, p + 1,
  4211. FFMIN(hostname_size, brk - p));
  4212. if (brk[1] == ':' && port_ptr)
  4213. *port_ptr = atoi(brk + 2);
  4214. } else if ((col = strchr(p, ':')) && col < ls) {
  4215. av_strlcpy(hostname, p,
  4216. FFMIN(col + 1 - p, hostname_size));
  4217. if (port_ptr)
  4218. *port_ptr = atoi(col + 1);
  4219. } else
  4220. av_strlcpy(hostname, p,
  4221. FFMIN(ls + 1 - p, hostname_size));
  4222. }
  4223. }
  4224. int ff_mkdir_p(const char *path)
  4225. {
  4226. int ret = 0;
  4227. char *temp = av_strdup(path);
  4228. char *pos = temp;
  4229. char tmp_ch = '\0';
  4230. if (!path || !temp) {
  4231. return -1;
  4232. }
  4233. if (!av_strncasecmp(temp, "/", 1) || !av_strncasecmp(temp, "\\", 1)) {
  4234. pos++;
  4235. } else if (!av_strncasecmp(temp, "./", 2) || !av_strncasecmp(temp, ".\\", 2)) {
  4236. pos += 2;
  4237. }
  4238. for ( ; *pos != '\0'; ++pos) {
  4239. if (*pos == '/' || *pos == '\\') {
  4240. tmp_ch = *pos;
  4241. *pos = '\0';
  4242. ret = mkdir(temp, 0755);
  4243. *pos = tmp_ch;
  4244. }
  4245. }
  4246. if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) {
  4247. ret = mkdir(temp, 0755);
  4248. }
  4249. av_free(temp);
  4250. return ret;
  4251. }
  4252. char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
  4253. {
  4254. int i;
  4255. static const char hex_table_uc[16] = { '0', '1', '2', '3',
  4256. '4', '5', '6', '7',
  4257. '8', '9', 'A', 'B',
  4258. 'C', 'D', 'E', 'F' };
  4259. static const char hex_table_lc[16] = { '0', '1', '2', '3',
  4260. '4', '5', '6', '7',
  4261. '8', '9', 'a', 'b',
  4262. 'c', 'd', 'e', 'f' };
  4263. const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
  4264. for (i = 0; i < s; i++) {
  4265. buff[i * 2] = hex_table[src[i] >> 4];
  4266. buff[i * 2 + 1] = hex_table[src[i] & 0xF];
  4267. }
  4268. return buff;
  4269. }
  4270. int ff_hex_to_data(uint8_t *data, const char *p)
  4271. {
  4272. int c, len, v;
  4273. len = 0;
  4274. v = 1;
  4275. for (;;) {
  4276. p += strspn(p, SPACE_CHARS);
  4277. if (*p == '\0')
  4278. break;
  4279. c = av_toupper((unsigned char) *p++);
  4280. if (c >= '0' && c <= '9')
  4281. c = c - '0';
  4282. else if (c >= 'A' && c <= 'F')
  4283. c = c - 'A' + 10;
  4284. else
  4285. break;
  4286. v = (v << 4) | c;
  4287. if (v & 0x100) {
  4288. if (data)
  4289. data[len] = v;
  4290. len++;
  4291. v = 1;
  4292. }
  4293. }
  4294. return len;
  4295. }
  4296. void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
  4297. unsigned int pts_num, unsigned int pts_den)
  4298. {
  4299. AVRational new_tb;
  4300. if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
  4301. if (new_tb.num != pts_num)
  4302. av_log(NULL, AV_LOG_DEBUG,
  4303. "st:%d removing common factor %d from timebase\n",
  4304. s->index, pts_num / new_tb.num);
  4305. } else
  4306. av_log(NULL, AV_LOG_WARNING,
  4307. "st:%d has too large timebase, reducing\n", s->index);
  4308. if (new_tb.num <= 0 || new_tb.den <= 0) {
  4309. av_log(NULL, AV_LOG_ERROR,
  4310. "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
  4311. new_tb.num, new_tb.den,
  4312. s->index);
  4313. return;
  4314. }
  4315. s->time_base = new_tb;
  4316. #if FF_API_LAVF_AVCTX
  4317. FF_DISABLE_DEPRECATION_WARNINGS
  4318. s->codec->pkt_timebase = new_tb;
  4319. FF_ENABLE_DEPRECATION_WARNINGS
  4320. #endif
  4321. s->internal->avctx->pkt_timebase = new_tb;
  4322. s->pts_wrap_bits = pts_wrap_bits;
  4323. }
  4324. void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
  4325. void *context)
  4326. {
  4327. const char *ptr = str;
  4328. /* Parse key=value pairs. */
  4329. for (;;) {
  4330. const char *key;
  4331. char *dest = NULL, *dest_end;
  4332. int key_len, dest_len = 0;
  4333. /* Skip whitespace and potential commas. */
  4334. while (*ptr && (av_isspace(*ptr) || *ptr == ','))
  4335. ptr++;
  4336. if (!*ptr)
  4337. break;
  4338. key = ptr;
  4339. if (!(ptr = strchr(key, '=')))
  4340. break;
  4341. ptr++;
  4342. key_len = ptr - key;
  4343. callback_get_buf(context, key, key_len, &dest, &dest_len);
  4344. dest_end = dest + dest_len - 1;
  4345. if (*ptr == '\"') {
  4346. ptr++;
  4347. while (*ptr && *ptr != '\"') {
  4348. if (*ptr == '\\') {
  4349. if (!ptr[1])
  4350. break;
  4351. if (dest && dest < dest_end)
  4352. *dest++ = ptr[1];
  4353. ptr += 2;
  4354. } else {
  4355. if (dest && dest < dest_end)
  4356. *dest++ = *ptr;
  4357. ptr++;
  4358. }
  4359. }
  4360. if (*ptr == '\"')
  4361. ptr++;
  4362. } else {
  4363. for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
  4364. if (dest && dest < dest_end)
  4365. *dest++ = *ptr;
  4366. }
  4367. if (dest)
  4368. *dest = 0;
  4369. }
  4370. }
  4371. int ff_find_stream_index(AVFormatContext *s, int id)
  4372. {
  4373. int i;
  4374. for (i = 0; i < s->nb_streams; i++)
  4375. if (s->streams[i]->id == id)
  4376. return i;
  4377. return -1;
  4378. }
  4379. int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id,
  4380. int std_compliance)
  4381. {
  4382. if (ofmt) {
  4383. unsigned int codec_tag;
  4384. if (ofmt->query_codec)
  4385. return ofmt->query_codec(codec_id, std_compliance);
  4386. else if (ofmt->codec_tag)
  4387. return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
  4388. else if (codec_id == ofmt->video_codec ||
  4389. codec_id == ofmt->audio_codec ||
  4390. codec_id == ofmt->subtitle_codec ||
  4391. codec_id == ofmt->data_codec)
  4392. return 1;
  4393. }
  4394. return AVERROR_PATCHWELCOME;
  4395. }
  4396. int avformat_network_init(void)
  4397. {
  4398. #if CONFIG_NETWORK
  4399. int ret;
  4400. if ((ret = ff_network_init()) < 0)
  4401. return ret;
  4402. if ((ret = ff_tls_init()) < 0)
  4403. return ret;
  4404. #endif
  4405. return 0;
  4406. }
  4407. int avformat_network_deinit(void)
  4408. {
  4409. #if CONFIG_NETWORK
  4410. ff_network_close();
  4411. ff_tls_deinit();
  4412. #endif
  4413. return 0;
  4414. }
  4415. int ff_add_param_change(AVPacket *pkt, int32_t channels,
  4416. uint64_t channel_layout, int32_t sample_rate,
  4417. int32_t width, int32_t height)
  4418. {
  4419. uint32_t flags = 0;
  4420. int size = 4;
  4421. uint8_t *data;
  4422. if (!pkt)
  4423. return AVERROR(EINVAL);
  4424. if (channels) {
  4425. size += 4;
  4426. flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT;
  4427. }
  4428. if (channel_layout) {
  4429. size += 8;
  4430. flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT;
  4431. }
  4432. if (sample_rate) {
  4433. size += 4;
  4434. flags |= AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE;
  4435. }
  4436. if (width || height) {
  4437. size += 8;
  4438. flags |= AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS;
  4439. }
  4440. data = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, size);
  4441. if (!data)
  4442. return AVERROR(ENOMEM);
  4443. bytestream_put_le32(&data, flags);
  4444. if (channels)
  4445. bytestream_put_le32(&data, channels);
  4446. if (channel_layout)
  4447. bytestream_put_le64(&data, channel_layout);
  4448. if (sample_rate)
  4449. bytestream_put_le32(&data, sample_rate);
  4450. if (width || height) {
  4451. bytestream_put_le32(&data, width);
  4452. bytestream_put_le32(&data, height);
  4453. }
  4454. return 0;
  4455. }
  4456. AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
  4457. {
  4458. AVRational undef = {0, 1};
  4459. AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
  4460. AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef;
  4461. AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio;
  4462. av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
  4463. stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
  4464. if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
  4465. stream_sample_aspect_ratio = undef;
  4466. av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
  4467. frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
  4468. if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
  4469. frame_sample_aspect_ratio = undef;
  4470. if (stream_sample_aspect_ratio.num)
  4471. return stream_sample_aspect_ratio;
  4472. else
  4473. return frame_sample_aspect_ratio;
  4474. }
  4475. AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
  4476. {
  4477. AVRational fr = st->r_frame_rate;
  4478. AVRational codec_fr = st->internal->avctx->framerate;
  4479. AVRational avg_fr = st->avg_frame_rate;
  4480. if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
  4481. av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
  4482. fr = avg_fr;
  4483. }
  4484. if (st->internal->avctx->ticks_per_frame > 1) {
  4485. if ( codec_fr.num > 0 && codec_fr.den > 0 &&
  4486. (fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1))
  4487. fr = codec_fr;
  4488. }
  4489. return fr;
  4490. }
  4491. /**
  4492. * Matches a stream specifier (but ignores requested index).
  4493. *
  4494. * @param indexptr set to point to the requested stream index if there is one
  4495. *
  4496. * @return <0 on error
  4497. * 0 if st is NOT a matching stream
  4498. * >0 if st is a matching stream
  4499. */
  4500. static int match_stream_specifier(AVFormatContext *s, AVStream *st,
  4501. const char *spec, const char **indexptr, AVProgram **p)
  4502. {
  4503. int match = 1; /* Stores if the specifier matches so far. */
  4504. while (*spec) {
  4505. if (*spec <= '9' && *spec >= '0') { /* opt:index */
  4506. if (indexptr)
  4507. *indexptr = spec;
  4508. return match;
  4509. } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
  4510. *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
  4511. enum AVMediaType type;
  4512. int nopic = 0;
  4513. switch (*spec++) {
  4514. case 'v': type = AVMEDIA_TYPE_VIDEO; break;
  4515. case 'a': type = AVMEDIA_TYPE_AUDIO; break;
  4516. case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
  4517. case 'd': type = AVMEDIA_TYPE_DATA; break;
  4518. case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
  4519. case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
  4520. default: av_assert0(0);
  4521. }
  4522. if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
  4523. return AVERROR(EINVAL);
  4524. #if FF_API_LAVF_AVCTX
  4525. FF_DISABLE_DEPRECATION_WARNINGS
  4526. if (type != st->codecpar->codec_type
  4527. && (st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN || st->codec->codec_type != type))
  4528. match = 0;
  4529. FF_ENABLE_DEPRECATION_WARNINGS
  4530. #else
  4531. if (type != st->codecpar->codec_type)
  4532. match = 0;
  4533. #endif
  4534. if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  4535. match = 0;
  4536. } else if (*spec == 'p' && *(spec + 1) == ':') {
  4537. int prog_id, i, j;
  4538. int found = 0;
  4539. char *endptr;
  4540. spec += 2;
  4541. prog_id = strtol(spec, &endptr, 0);
  4542. /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
  4543. if (spec == endptr || (*endptr && *endptr++ != ':'))
  4544. return AVERROR(EINVAL);
  4545. spec = endptr;
  4546. if (match) {
  4547. for (i = 0; i < s->nb_programs; i++) {
  4548. if (s->programs[i]->id != prog_id)
  4549. continue;
  4550. for (j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
  4551. if (st->index == s->programs[i]->stream_index[j]) {
  4552. found = 1;
  4553. if (p)
  4554. *p = s->programs[i];
  4555. i = s->nb_programs;
  4556. break;
  4557. }
  4558. }
  4559. }
  4560. }
  4561. if (!found)
  4562. match = 0;
  4563. } else if (*spec == '#' ||
  4564. (*spec == 'i' && *(spec + 1) == ':')) {
  4565. int stream_id;
  4566. char *endptr;
  4567. spec += 1 + (*spec == 'i');
  4568. stream_id = strtol(spec, &endptr, 0);
  4569. if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */
  4570. return AVERROR(EINVAL);
  4571. return match && (stream_id == st->id);
  4572. } else if (*spec == 'm' && *(spec + 1) == ':') {
  4573. AVDictionaryEntry *tag;
  4574. char *key, *val;
  4575. int ret;
  4576. if (match) {
  4577. spec += 2;
  4578. val = strchr(spec, ':');
  4579. key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
  4580. if (!key)
  4581. return AVERROR(ENOMEM);
  4582. tag = av_dict_get(st->metadata, key, NULL, 0);
  4583. if (tag) {
  4584. if (!val || !strcmp(tag->value, val + 1))
  4585. ret = 1;
  4586. else
  4587. ret = 0;
  4588. } else
  4589. ret = 0;
  4590. av_freep(&key);
  4591. }
  4592. return match && ret;
  4593. } else if (*spec == 'u' && *(spec + 1) == '\0') {
  4594. AVCodecParameters *par = st->codecpar;
  4595. #if FF_API_LAVF_AVCTX
  4596. FF_DISABLE_DEPRECATION_WARNINGS
  4597. AVCodecContext *codec = st->codec;
  4598. FF_ENABLE_DEPRECATION_WARNINGS
  4599. #endif
  4600. int val;
  4601. switch (par->codec_type) {
  4602. case AVMEDIA_TYPE_AUDIO:
  4603. val = par->sample_rate && par->channels;
  4604. #if FF_API_LAVF_AVCTX
  4605. val = val || (codec->sample_rate && codec->channels);
  4606. #endif
  4607. if (par->format == AV_SAMPLE_FMT_NONE
  4608. #if FF_API_LAVF_AVCTX
  4609. && codec->sample_fmt == AV_SAMPLE_FMT_NONE
  4610. #endif
  4611. )
  4612. return 0;
  4613. break;
  4614. case AVMEDIA_TYPE_VIDEO:
  4615. val = par->width && par->height;
  4616. #if FF_API_LAVF_AVCTX
  4617. val = val || (codec->width && codec->height);
  4618. #endif
  4619. if (par->format == AV_PIX_FMT_NONE
  4620. #if FF_API_LAVF_AVCTX
  4621. && codec->pix_fmt == AV_PIX_FMT_NONE
  4622. #endif
  4623. )
  4624. return 0;
  4625. break;
  4626. case AVMEDIA_TYPE_UNKNOWN:
  4627. val = 0;
  4628. break;
  4629. default:
  4630. val = 1;
  4631. break;
  4632. }
  4633. #if FF_API_LAVF_AVCTX
  4634. return match && ((par->codec_id != AV_CODEC_ID_NONE || codec->codec_id != AV_CODEC_ID_NONE) && val != 0);
  4635. #else
  4636. return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
  4637. #endif
  4638. } else {
  4639. return AVERROR(EINVAL);
  4640. }
  4641. }
  4642. return match;
  4643. }
  4644. int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
  4645. const char *spec)
  4646. {
  4647. int ret, index;
  4648. char *endptr;
  4649. const char *indexptr = NULL;
  4650. AVProgram *p = NULL;
  4651. int nb_streams;
  4652. ret = match_stream_specifier(s, st, spec, &indexptr, &p);
  4653. if (ret < 0)
  4654. goto error;
  4655. if (!indexptr)
  4656. return ret;
  4657. index = strtol(indexptr, &endptr, 0);
  4658. if (*endptr) { /* We can't have anything after the requested index. */
  4659. ret = AVERROR(EINVAL);
  4660. goto error;
  4661. }
  4662. /* This is not really needed but saves us a loop for simple stream index specifiers. */
  4663. if (spec == indexptr)
  4664. return (index == st->index);
  4665. /* If we requested a matching stream index, we have to ensure st is that. */
  4666. nb_streams = p ? p->nb_stream_indexes : s->nb_streams;
  4667. for (int i = 0; i < nb_streams && index >= 0; i++) {
  4668. AVStream *candidate = p ? s->streams[p->stream_index[i]] : s->streams[i];
  4669. ret = match_stream_specifier(s, candidate, spec, NULL, NULL);
  4670. if (ret < 0)
  4671. goto error;
  4672. if (ret > 0 && index-- == 0 && st == candidate)
  4673. return 1;
  4674. }
  4675. return 0;
  4676. error:
  4677. if (ret == AVERROR(EINVAL))
  4678. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  4679. return ret;
  4680. }
  4681. int ff_generate_avci_extradata(AVStream *st)
  4682. {
  4683. static const uint8_t avci100_1080p_extradata[] = {
  4684. // SPS
  4685. 0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
  4686. 0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
  4687. 0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
  4688. 0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
  4689. 0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
  4690. 0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
  4691. 0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
  4692. 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
  4693. 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4694. // PPS
  4695. 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
  4696. 0xd0
  4697. };
  4698. static const uint8_t avci100_1080i_extradata[] = {
  4699. // SPS
  4700. 0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
  4701. 0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
  4702. 0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
  4703. 0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
  4704. 0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
  4705. 0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
  4706. 0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
  4707. 0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
  4708. 0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
  4709. 0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
  4710. 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x20,
  4711. // PPS
  4712. 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
  4713. 0xd0
  4714. };
  4715. static const uint8_t avci50_1080p_extradata[] = {
  4716. // SPS
  4717. 0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
  4718. 0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
  4719. 0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
  4720. 0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
  4721. 0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
  4722. 0x6e, 0x6c, 0xd3, 0x3c, 0x05, 0xa0, 0x22, 0x7e,
  4723. 0x5f, 0xfc, 0x00, 0x0c, 0x00, 0x13, 0x8c, 0x04,
  4724. 0x04, 0x05, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
  4725. 0x00, 0x03, 0x00, 0x32, 0x84, 0x00, 0x00, 0x00,
  4726. // PPS
  4727. 0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
  4728. 0x11
  4729. };
  4730. static const uint8_t avci50_1080i_extradata[] = {
  4731. // SPS
  4732. 0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
  4733. 0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
  4734. 0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
  4735. 0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
  4736. 0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
  4737. 0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
  4738. 0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
  4739. 0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
  4740. 0x81, 0x13, 0xf7, 0xff, 0x80, 0x02, 0x00, 0x01,
  4741. 0xf1, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
  4742. 0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
  4743. // PPS
  4744. 0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
  4745. 0x11
  4746. };
  4747. static const uint8_t avci100_720p_extradata[] = {
  4748. // SPS
  4749. 0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
  4750. 0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
  4751. 0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
  4752. 0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
  4753. 0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
  4754. 0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
  4755. 0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
  4756. 0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
  4757. 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
  4758. 0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
  4759. // PPS
  4760. 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
  4761. 0x11
  4762. };
  4763. static const uint8_t avci50_720p_extradata[] = {
  4764. // SPS
  4765. 0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x20,
  4766. 0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
  4767. 0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
  4768. 0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
  4769. 0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
  4770. 0x6e, 0x6c, 0xd3, 0x3c, 0x0f, 0x01, 0x6e, 0xff,
  4771. 0xc0, 0x00, 0xc0, 0x01, 0x38, 0xc0, 0x40, 0x40,
  4772. 0x50, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00,
  4773. 0x06, 0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
  4774. // PPS
  4775. 0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
  4776. 0x11
  4777. };
  4778. const uint8_t *data = NULL;
  4779. int ret, size = 0;
  4780. if (st->codecpar->width == 1920) {
  4781. if (st->codecpar->field_order == AV_FIELD_PROGRESSIVE) {
  4782. data = avci100_1080p_extradata;
  4783. size = sizeof(avci100_1080p_extradata);
  4784. } else {
  4785. data = avci100_1080i_extradata;
  4786. size = sizeof(avci100_1080i_extradata);
  4787. }
  4788. } else if (st->codecpar->width == 1440) {
  4789. if (st->codecpar->field_order == AV_FIELD_PROGRESSIVE) {
  4790. data = avci50_1080p_extradata;
  4791. size = sizeof(avci50_1080p_extradata);
  4792. } else {
  4793. data = avci50_1080i_extradata;
  4794. size = sizeof(avci50_1080i_extradata);
  4795. }
  4796. } else if (st->codecpar->width == 1280) {
  4797. data = avci100_720p_extradata;
  4798. size = sizeof(avci100_720p_extradata);
  4799. } else if (st->codecpar->width == 960) {
  4800. data = avci50_720p_extradata;
  4801. size = sizeof(avci50_720p_extradata);
  4802. }
  4803. if (!size)
  4804. return 0;
  4805. if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
  4806. return ret;
  4807. memcpy(st->codecpar->extradata, data, size);
  4808. return 0;
  4809. }
  4810. uint8_t *av_stream_get_side_data(const AVStream *st,
  4811. enum AVPacketSideDataType type, buffer_size_t *size)
  4812. {
  4813. int i;
  4814. for (i = 0; i < st->nb_side_data; i++) {
  4815. if (st->side_data[i].type == type) {
  4816. if (size)
  4817. *size = st->side_data[i].size;
  4818. return st->side_data[i].data;
  4819. }
  4820. }
  4821. if (size)
  4822. *size = 0;
  4823. return NULL;
  4824. }
  4825. int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
  4826. uint8_t *data, size_t size)
  4827. {
  4828. AVPacketSideData *sd, *tmp;
  4829. int i;
  4830. for (i = 0; i < st->nb_side_data; i++) {
  4831. sd = &st->side_data[i];
  4832. if (sd->type == type) {
  4833. av_freep(&sd->data);
  4834. sd->data = data;
  4835. sd->size = size;
  4836. return 0;
  4837. }
  4838. }
  4839. if ((unsigned)st->nb_side_data + 1 >= INT_MAX / sizeof(*st->side_data))
  4840. return AVERROR(ERANGE);
  4841. tmp = av_realloc(st->side_data, (st->nb_side_data + 1) * sizeof(*tmp));
  4842. if (!tmp) {
  4843. return AVERROR(ENOMEM);
  4844. }
  4845. st->side_data = tmp;
  4846. st->nb_side_data++;
  4847. sd = &st->side_data[st->nb_side_data - 1];
  4848. sd->type = type;
  4849. sd->data = data;
  4850. sd->size = size;
  4851. return 0;
  4852. }
  4853. uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
  4854. buffer_size_t size)
  4855. {
  4856. int ret;
  4857. uint8_t *data = av_malloc(size);
  4858. if (!data)
  4859. return NULL;
  4860. ret = av_stream_add_side_data(st, type, data, size);
  4861. if (ret < 0) {
  4862. av_freep(&data);
  4863. return NULL;
  4864. }
  4865. return data;
  4866. }
  4867. int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
  4868. {
  4869. int ret;
  4870. const AVBitStreamFilter *bsf;
  4871. AVBSFContext *bsfc;
  4872. av_assert0(!st->internal->bsfc);
  4873. if (!(bsf = av_bsf_get_by_name(name))) {
  4874. av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name);
  4875. return AVERROR_BSF_NOT_FOUND;
  4876. }
  4877. if ((ret = av_bsf_alloc(bsf, &bsfc)) < 0)
  4878. return ret;
  4879. bsfc->time_base_in = st->time_base;
  4880. if ((ret = avcodec_parameters_copy(bsfc->par_in, st->codecpar)) < 0) {
  4881. av_bsf_free(&bsfc);
  4882. return ret;
  4883. }
  4884. if (args && bsfc->filter->priv_class) {
  4885. const AVOption *opt = av_opt_next(bsfc->priv_data, NULL);
  4886. const char * shorthand[2] = {NULL};
  4887. if (opt)
  4888. shorthand[0] = opt->name;
  4889. if ((ret = av_opt_set_from_string(bsfc->priv_data, args, shorthand, "=", ":")) < 0) {
  4890. av_bsf_free(&bsfc);
  4891. return ret;
  4892. }
  4893. }
  4894. if ((ret = av_bsf_init(bsfc)) < 0) {
  4895. av_bsf_free(&bsfc);
  4896. return ret;
  4897. }
  4898. st->internal->bsfc = bsfc;
  4899. av_log(NULL, AV_LOG_VERBOSE,
  4900. "Automatically inserted bitstream filter '%s'; args='%s'\n",
  4901. name, args ? args : "");
  4902. return 1;
  4903. }
  4904. #if FF_API_OLD_BSF
  4905. FF_DISABLE_DEPRECATION_WARNINGS
  4906. int av_apply_bitstream_filters(AVCodecContext *codec, AVPacket *pkt,
  4907. AVBitStreamFilterContext *bsfc)
  4908. {
  4909. int ret = 0;
  4910. while (bsfc) {
  4911. AVPacket new_pkt = *pkt;
  4912. int a = av_bitstream_filter_filter(bsfc, codec, NULL,
  4913. &new_pkt.data, &new_pkt.size,
  4914. pkt->data, pkt->size,
  4915. pkt->flags & AV_PKT_FLAG_KEY);
  4916. if (a == 0 && new_pkt.size == 0 && new_pkt.side_data_elems == 0) {
  4917. av_packet_unref(pkt);
  4918. memset(pkt, 0, sizeof(*pkt));
  4919. return 0;
  4920. }
  4921. if(a == 0 && new_pkt.data != pkt->data) {
  4922. uint8_t *t = av_malloc(new_pkt.size + AV_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow
  4923. if (t) {
  4924. memcpy(t, new_pkt.data, new_pkt.size);
  4925. memset(t + new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  4926. new_pkt.data = t;
  4927. new_pkt.buf = NULL;
  4928. a = 1;
  4929. } else {
  4930. a = AVERROR(ENOMEM);
  4931. }
  4932. }
  4933. if (a > 0) {
  4934. new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
  4935. av_buffer_default_free, NULL, 0);
  4936. if (new_pkt.buf) {
  4937. pkt->side_data = NULL;
  4938. pkt->side_data_elems = 0;
  4939. av_packet_unref(pkt);
  4940. } else {
  4941. av_freep(&new_pkt.data);
  4942. a = AVERROR(ENOMEM);
  4943. }
  4944. }
  4945. if (a < 0) {
  4946. av_log(codec, AV_LOG_ERROR,
  4947. "Failed to open bitstream filter %s for stream %d with codec %s",
  4948. bsfc->filter->name, pkt->stream_index,
  4949. codec->codec ? codec->codec->name : "copy");
  4950. ret = a;
  4951. break;
  4952. }
  4953. *pkt = new_pkt;
  4954. bsfc = bsfc->next;
  4955. }
  4956. return ret;
  4957. }
  4958. FF_ENABLE_DEPRECATION_WARNINGS
  4959. #endif
  4960. int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options)
  4961. {
  4962. if (!s->oformat)
  4963. return AVERROR(EINVAL);
  4964. if (!(s->oformat->flags & AVFMT_NOFILE))
  4965. return s->io_open(s, &s->pb, url, AVIO_FLAG_WRITE, options);
  4966. return 0;
  4967. }
  4968. void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
  4969. {
  4970. if (*pb)
  4971. s->io_close(s, *pb);
  4972. *pb = NULL;
  4973. }
  4974. int ff_is_http_proto(char *filename) {
  4975. const char *proto = avio_find_protocol_name(filename);
  4976. return proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
  4977. }
  4978. int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
  4979. {
  4980. AVDictionaryEntry *entry;
  4981. int64_t parsed_timestamp;
  4982. int ret;
  4983. if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
  4984. if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
  4985. *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
  4986. return 1;
  4987. } else {
  4988. av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);
  4989. return ret;
  4990. }
  4991. }
  4992. return 0;
  4993. }
  4994. int ff_standardize_creation_time(AVFormatContext *s)
  4995. {
  4996. int64_t timestamp;
  4997. int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
  4998. if (ret == 1)
  4999. return avpriv_dict_set_timestamp(&s->metadata, "creation_time", timestamp);
  5000. return ret;
  5001. }
  5002. int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
  5003. {
  5004. uint8_t *side_data;
  5005. buffer_size_t size;
  5006. side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
  5007. if (side_data) {
  5008. if (size != AVPALETTE_SIZE) {
  5009. av_log(s, AV_LOG_ERROR, "Invalid palette side data\n");
  5010. return AVERROR_INVALIDDATA;
  5011. }
  5012. memcpy(palette, side_data, AVPALETTE_SIZE);
  5013. return 1;
  5014. }
  5015. if (ret == CONTAINS_PAL) {
  5016. int i;
  5017. for (i = 0; i < AVPALETTE_COUNT; i++)
  5018. palette[i] = AV_RL32(pkt->data + pkt->size - AVPALETTE_SIZE + i*4);
  5019. return 1;
  5020. }
  5021. return 0;
  5022. }
  5023. int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf)
  5024. {
  5025. int ret;
  5026. char *str;
  5027. ret = av_bprint_finalize(buf, &str);
  5028. if (ret < 0)
  5029. return ret;
  5030. if (!av_bprint_is_complete(buf)) {
  5031. av_free(str);
  5032. return AVERROR(ENOMEM);
  5033. }
  5034. par->extradata = str;
  5035. /* Note: the string is NUL terminated (so extradata can be read as a
  5036. * string), but the ending character is not accounted in the size (in
  5037. * binary formats you are likely not supposed to mux that character). When
  5038. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  5039. * zeros. */
  5040. par->extradata_size = buf->len;
  5041. return 0;
  5042. }
  5043. int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
  5044. AVStream *ost, const AVStream *ist,
  5045. enum AVTimebaseSource copy_tb)
  5046. {
  5047. //TODO: use [io]st->internal->avctx
  5048. const AVCodecContext *dec_ctx;
  5049. AVCodecContext *enc_ctx;
  5050. #if FF_API_LAVF_AVCTX
  5051. FF_DISABLE_DEPRECATION_WARNINGS
  5052. dec_ctx = ist->codec;
  5053. enc_ctx = ost->codec;
  5054. FF_ENABLE_DEPRECATION_WARNINGS
  5055. #else
  5056. dec_ctx = ist->internal->avctx;
  5057. enc_ctx = ost->internal->avctx;
  5058. #endif
  5059. enc_ctx->time_base = ist->time_base;
  5060. /*
  5061. * Avi is a special case here because it supports variable fps but
  5062. * having the fps and timebase differe significantly adds quite some
  5063. * overhead
  5064. */
  5065. if (!strcmp(ofmt->name, "avi")) {
  5066. #if FF_API_R_FRAME_RATE
  5067. if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
  5068. && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
  5069. && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
  5070. && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx->time_base)
  5071. && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
  5072. || copy_tb == AVFMT_TBCF_R_FRAMERATE) {
  5073. enc_ctx->time_base.num = ist->r_frame_rate.den;
  5074. enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
  5075. enc_ctx->ticks_per_frame = 2;
  5076. } else
  5077. #endif
  5078. if (copy_tb == AVFMT_TBCF_AUTO && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > 2*av_q2d(ist->time_base)
  5079. && av_q2d(ist->time_base) < 1.0/500
  5080. || copy_tb == AVFMT_TBCF_DECODER) {
  5081. enc_ctx->time_base = dec_ctx->time_base;
  5082. enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
  5083. enc_ctx->time_base.den *= 2;
  5084. enc_ctx->ticks_per_frame = 2;
  5085. }
  5086. } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
  5087. && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
  5088. if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->time_base.den
  5089. && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > av_q2d(ist->time_base)
  5090. && av_q2d(ist->time_base) < 1.0/500
  5091. || copy_tb == AVFMT_TBCF_DECODER) {
  5092. enc_ctx->time_base = dec_ctx->time_base;
  5093. enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
  5094. }
  5095. }
  5096. if ((enc_ctx->codec_tag == AV_RL32("tmcd") || ost->codecpar->codec_tag == AV_RL32("tmcd"))
  5097. && dec_ctx->time_base.num < dec_ctx->time_base.den
  5098. && dec_ctx->time_base.num > 0
  5099. && 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
  5100. enc_ctx->time_base = dec_ctx->time_base;
  5101. }
  5102. if (ost->avg_frame_rate.num)
  5103. enc_ctx->time_base = av_inv_q(ost->avg_frame_rate);
  5104. av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
  5105. enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
  5106. return 0;
  5107. }
  5108. AVRational av_stream_get_codec_timebase(const AVStream *st)
  5109. {
  5110. // See avformat_transfer_internal_stream_timing_info() TODO.
  5111. #if FF_API_LAVF_AVCTX
  5112. FF_DISABLE_DEPRECATION_WARNINGS
  5113. return st->codec->time_base;
  5114. FF_ENABLE_DEPRECATION_WARNINGS
  5115. #else
  5116. return st->internal->avctx->time_base;
  5117. #endif
  5118. }
  5119. void ff_format_set_url(AVFormatContext *s, char *url)
  5120. {
  5121. av_assert0(url);
  5122. av_freep(&s->url);
  5123. s->url = url;
  5124. #if FF_API_FORMAT_FILENAME
  5125. FF_DISABLE_DEPRECATION_WARNINGS
  5126. av_strlcpy(s->filename, url, sizeof(s->filename));
  5127. FF_ENABLE_DEPRECATION_WARNINGS
  5128. #endif
  5129. }