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.

1187 lines
39KB

  1. /*
  2. * muxing 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 "avformat.h"
  22. #include "avio_internal.h"
  23. #include "internal.h"
  24. #include "libavcodec/internal.h"
  25. #include "libavcodec/bytestream.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/dict.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/timestamp.h"
  30. #include "metadata.h"
  31. #include "id3v2.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/internal.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/parseutils.h"
  37. #include "libavutil/time.h"
  38. #include "riff.h"
  39. #include "audiointerleave.h"
  40. #include "url.h"
  41. #include <stdarg.h>
  42. #if CONFIG_NETWORK
  43. #include "network.h"
  44. #endif
  45. /**
  46. * @file
  47. * muxing functions for use within libavformat
  48. */
  49. /* fraction handling */
  50. /**
  51. * f = val + (num / den) + 0.5.
  52. *
  53. * 'num' is normalized so that it is such as 0 <= num < den.
  54. *
  55. * @param f fractional number
  56. * @param val integer value
  57. * @param num must be >= 0
  58. * @param den must be >= 1
  59. */
  60. static void frac_init(FFFrac *f, int64_t val, int64_t num, int64_t den)
  61. {
  62. num += (den >> 1);
  63. if (num >= den) {
  64. val += num / den;
  65. num = num % den;
  66. }
  67. f->val = val;
  68. f->num = num;
  69. f->den = den;
  70. }
  71. /**
  72. * Fractional addition to f: f = f + (incr / f->den).
  73. *
  74. * @param f fractional number
  75. * @param incr increment, can be positive or negative
  76. */
  77. static void frac_add(FFFrac *f, int64_t incr)
  78. {
  79. int64_t num, den;
  80. num = f->num + incr;
  81. den = f->den;
  82. if (num < 0) {
  83. f->val += num / den;
  84. num = num % den;
  85. if (num < 0) {
  86. num += den;
  87. f->val--;
  88. }
  89. } else if (num >= den) {
  90. f->val += num / den;
  91. num = num % den;
  92. }
  93. f->num = num;
  94. }
  95. AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
  96. {
  97. AVRational q;
  98. int j;
  99. q = st->time_base;
  100. for (j=2; j<14; j+= 1+(j>2))
  101. while (q.den / q.num < min_precision && q.num % j == 0)
  102. q.num /= j;
  103. while (q.den / q.num < min_precision && q.den < (1<<24))
  104. q.den <<= 1;
  105. return q;
  106. }
  107. enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
  108. {
  109. AVCodecContext *avctx = st->codec;
  110. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  111. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  112. return avctx->chroma_sample_location;
  113. if (pix_desc) {
  114. if (pix_desc->log2_chroma_h == 0) {
  115. return AVCHROMA_LOC_TOPLEFT;
  116. } else if (pix_desc->log2_chroma_w == 1 && pix_desc->log2_chroma_h == 1) {
  117. if (avctx->field_order == AV_FIELD_UNKNOWN || avctx->field_order == AV_FIELD_PROGRESSIVE) {
  118. switch (avctx->codec_id) {
  119. case AV_CODEC_ID_MJPEG:
  120. case AV_CODEC_ID_MPEG1VIDEO: return AVCHROMA_LOC_CENTER;
  121. }
  122. }
  123. if (avctx->field_order == AV_FIELD_UNKNOWN || avctx->field_order != AV_FIELD_PROGRESSIVE) {
  124. switch (avctx->codec_id) {
  125. case AV_CODEC_ID_MPEG2VIDEO: return AVCHROMA_LOC_LEFT;
  126. }
  127. }
  128. }
  129. }
  130. return AVCHROMA_LOC_UNSPECIFIED;
  131. }
  132. int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
  133. const char *format, const char *filename)
  134. {
  135. AVFormatContext *s = avformat_alloc_context();
  136. int ret = 0;
  137. *avctx = NULL;
  138. if (!s)
  139. goto nomem;
  140. if (!oformat) {
  141. if (format) {
  142. oformat = av_guess_format(format, NULL, NULL);
  143. if (!oformat) {
  144. av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
  145. ret = AVERROR(EINVAL);
  146. goto error;
  147. }
  148. } else {
  149. oformat = av_guess_format(NULL, filename, NULL);
  150. if (!oformat) {
  151. ret = AVERROR(EINVAL);
  152. av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
  153. filename);
  154. goto error;
  155. }
  156. }
  157. }
  158. s->oformat = oformat;
  159. if (s->oformat->priv_data_size > 0) {
  160. s->priv_data = av_mallocz(s->oformat->priv_data_size);
  161. if (!s->priv_data)
  162. goto nomem;
  163. if (s->oformat->priv_class) {
  164. *(const AVClass**)s->priv_data= s->oformat->priv_class;
  165. av_opt_set_defaults(s->priv_data);
  166. }
  167. } else
  168. s->priv_data = NULL;
  169. if (filename)
  170. av_strlcpy(s->filename, filename, sizeof(s->filename));
  171. *avctx = s;
  172. return 0;
  173. nomem:
  174. av_log(s, AV_LOG_ERROR, "Out of memory\n");
  175. ret = AVERROR(ENOMEM);
  176. error:
  177. avformat_free_context(s);
  178. return ret;
  179. }
  180. static int validate_codec_tag(AVFormatContext *s, AVStream *st)
  181. {
  182. const AVCodecTag *avctag;
  183. int n;
  184. enum AVCodecID id = AV_CODEC_ID_NONE;
  185. int64_t tag = -1;
  186. /**
  187. * Check that tag + id is in the table
  188. * If neither is in the table -> OK
  189. * If tag is in the table with another id -> FAIL
  190. * If id is in the table with another tag -> FAIL unless strict < normal
  191. */
  192. for (n = 0; s->oformat->codec_tag[n]; n++) {
  193. avctag = s->oformat->codec_tag[n];
  194. while (avctag->id != AV_CODEC_ID_NONE) {
  195. if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
  196. id = avctag->id;
  197. if (id == st->codec->codec_id)
  198. return 1;
  199. }
  200. if (avctag->id == st->codec->codec_id)
  201. tag = avctag->tag;
  202. avctag++;
  203. }
  204. }
  205. if (id != AV_CODEC_ID_NONE)
  206. return 0;
  207. if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
  208. return 0;
  209. return 1;
  210. }
  211. static int init_muxer(AVFormatContext *s, AVDictionary **options)
  212. {
  213. int ret = 0, i;
  214. AVStream *st;
  215. AVDictionary *tmp = NULL;
  216. AVCodecContext *codec = NULL;
  217. AVOutputFormat *of = s->oformat;
  218. const AVCodecDescriptor *desc;
  219. AVDictionaryEntry *e;
  220. if (options)
  221. av_dict_copy(&tmp, *options, 0);
  222. if ((ret = av_opt_set_dict(s, &tmp)) < 0)
  223. goto fail;
  224. if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
  225. (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  226. goto fail;
  227. if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT) {
  228. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  229. #if FF_API_LAVF_BITEXACT
  230. av_log(s, AV_LOG_WARNING,
  231. "Setting the AVFormatContext to bitexact mode, because "
  232. "the AVCodecContext is in that mode. This behavior will "
  233. "change in the future. To keep the current behavior, set "
  234. "AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
  235. s->flags |= AVFMT_FLAG_BITEXACT;
  236. #else
  237. av_log(s, AV_LOG_WARNING,
  238. "The AVFormatContext is not in set to bitexact mode, only "
  239. "the AVCodecContext. If this is not intended, set "
  240. "AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
  241. #endif
  242. }
  243. }
  244. // some sanity checks
  245. if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
  246. av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
  247. ret = AVERROR(EINVAL);
  248. goto fail;
  249. }
  250. for (i = 0; i < s->nb_streams; i++) {
  251. st = s->streams[i];
  252. codec = st->codec;
  253. #if FF_API_LAVF_CODEC_TB
  254. FF_DISABLE_DEPRECATION_WARNINGS
  255. if (!st->time_base.num && codec->time_base.num) {
  256. av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
  257. "timebase hint to the muxer is deprecated. Set "
  258. "AVStream.time_base instead.\n");
  259. avpriv_set_pts_info(st, 64, codec->time_base.num, codec->time_base.den);
  260. }
  261. FF_ENABLE_DEPRECATION_WARNINGS
  262. #endif
  263. if (!st->time_base.num) {
  264. /* fall back on the default timebase values */
  265. if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->sample_rate)
  266. avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
  267. else
  268. avpriv_set_pts_info(st, 33, 1, 90000);
  269. }
  270. switch (codec->codec_type) {
  271. case AVMEDIA_TYPE_AUDIO:
  272. if (codec->sample_rate <= 0) {
  273. av_log(s, AV_LOG_ERROR, "sample rate not set\n");
  274. ret = AVERROR(EINVAL);
  275. goto fail;
  276. }
  277. if (!codec->block_align)
  278. codec->block_align = codec->channels *
  279. av_get_bits_per_sample(codec->codec_id) >> 3;
  280. break;
  281. case AVMEDIA_TYPE_VIDEO:
  282. if ((codec->width <= 0 || codec->height <= 0) &&
  283. !(of->flags & AVFMT_NODIMENSIONS)) {
  284. av_log(s, AV_LOG_ERROR, "dimensions not set\n");
  285. ret = AVERROR(EINVAL);
  286. goto fail;
  287. }
  288. if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
  289. && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
  290. ) {
  291. if (st->sample_aspect_ratio.num != 0 &&
  292. st->sample_aspect_ratio.den != 0 &&
  293. codec->sample_aspect_ratio.num != 0 &&
  294. codec->sample_aspect_ratio.den != 0) {
  295. av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
  296. "(%d/%d) and encoder layer (%d/%d)\n",
  297. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  298. codec->sample_aspect_ratio.num,
  299. codec->sample_aspect_ratio.den);
  300. ret = AVERROR(EINVAL);
  301. goto fail;
  302. }
  303. }
  304. break;
  305. }
  306. desc = avcodec_descriptor_get(codec->codec_id);
  307. if (desc && desc->props & AV_CODEC_PROP_REORDER)
  308. st->internal->reorder = 1;
  309. if (of->codec_tag) {
  310. if ( codec->codec_tag
  311. && codec->codec_id == AV_CODEC_ID_RAWVIDEO
  312. && ( av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
  313. || av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
  314. && !validate_codec_tag(s, st)) {
  315. // the current rawvideo encoding system ends up setting
  316. // the wrong codec_tag for avi/mov, we override it here
  317. codec->codec_tag = 0;
  318. }
  319. if (codec->codec_tag) {
  320. if (!validate_codec_tag(s, st)) {
  321. char tagbuf[32], tagbuf2[32];
  322. av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
  323. av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
  324. av_log(s, AV_LOG_ERROR,
  325. "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
  326. tagbuf, codec->codec_tag, codec->codec_id, tagbuf2);
  327. ret = AVERROR_INVALIDDATA;
  328. goto fail;
  329. }
  330. } else
  331. codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
  332. }
  333. if (of->flags & AVFMT_GLOBALHEADER &&
  334. !(codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
  335. av_log(s, AV_LOG_WARNING,
  336. "Codec for stream %d does not use global headers "
  337. "but container format requires global headers\n", i);
  338. if (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
  339. s->internal->nb_interleaved_streams++;
  340. }
  341. if (!s->priv_data && of->priv_data_size > 0) {
  342. s->priv_data = av_mallocz(of->priv_data_size);
  343. if (!s->priv_data) {
  344. ret = AVERROR(ENOMEM);
  345. goto fail;
  346. }
  347. if (of->priv_class) {
  348. *(const AVClass **)s->priv_data = of->priv_class;
  349. av_opt_set_defaults(s->priv_data);
  350. if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  351. goto fail;
  352. }
  353. }
  354. /* set muxer identification string */
  355. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  356. av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
  357. } else {
  358. av_dict_set(&s->metadata, "encoder", NULL, 0);
  359. }
  360. for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
  361. av_dict_set(&s->metadata, e->key, NULL, 0);
  362. }
  363. if (options) {
  364. av_dict_free(options);
  365. *options = tmp;
  366. }
  367. return 0;
  368. fail:
  369. av_dict_free(&tmp);
  370. return ret;
  371. }
  372. static int init_pts(AVFormatContext *s)
  373. {
  374. int i;
  375. AVStream *st;
  376. /* init PTS generation */
  377. for (i = 0; i < s->nb_streams; i++) {
  378. int64_t den = AV_NOPTS_VALUE;
  379. st = s->streams[i];
  380. switch (st->codec->codec_type) {
  381. case AVMEDIA_TYPE_AUDIO:
  382. den = (int64_t)st->time_base.num * st->codec->sample_rate;
  383. break;
  384. case AVMEDIA_TYPE_VIDEO:
  385. den = (int64_t)st->time_base.num * st->codec->time_base.den;
  386. break;
  387. default:
  388. break;
  389. }
  390. if (!st->priv_pts)
  391. st->priv_pts = av_mallocz(sizeof(*st->priv_pts));
  392. if (!st->priv_pts)
  393. return AVERROR(ENOMEM);
  394. if (den != AV_NOPTS_VALUE) {
  395. if (den <= 0)
  396. return AVERROR_INVALIDDATA;
  397. frac_init(st->priv_pts, 0, 0, den);
  398. }
  399. }
  400. return 0;
  401. }
  402. int avformat_write_header(AVFormatContext *s, AVDictionary **options)
  403. {
  404. int ret = 0;
  405. if ((ret = init_muxer(s, options)) < 0)
  406. return ret;
  407. if (s->oformat->write_header) {
  408. ret = s->oformat->write_header(s);
  409. if (ret >= 0 && s->pb && s->pb->error < 0)
  410. ret = s->pb->error;
  411. if (ret < 0)
  412. return ret;
  413. if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  414. avio_flush(s->pb);
  415. }
  416. if ((ret = init_pts(s)) < 0)
  417. return ret;
  418. if (s->avoid_negative_ts < 0) {
  419. av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
  420. if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
  421. s->avoid_negative_ts = 0;
  422. } else
  423. s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
  424. }
  425. return 0;
  426. }
  427. #define AV_PKT_FLAG_UNCODED_FRAME 0x2000
  428. /* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but
  429. it is only being used internally to this file as a consistency check.
  430. The value is chosen to be very unlikely to appear on its own and to cause
  431. immediate failure if used anywhere as a real size. */
  432. #define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame))
  433. #if FF_API_COMPUTE_PKT_FIELDS2
  434. //FIXME merge with compute_pkt_fields
  435. static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  436. {
  437. int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
  438. int num, den, i;
  439. int frame_size;
  440. if (!s->internal->missing_ts_warning &&
  441. !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
  442. (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
  443. av_log(s, AV_LOG_WARNING,
  444. "Timestamps are unset in a packet for stream %d. "
  445. "This is deprecated and will stop working in the future. "
  446. "Fix your code to set the timestamps properly\n", st->index);
  447. s->internal->missing_ts_warning = 1;
  448. }
  449. if (s->debug & FF_FDEBUG_TS)
  450. av_log(s, AV_LOG_TRACE, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
  451. av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
  452. if (pkt->duration < 0 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  453. av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n",
  454. pkt->duration, pkt->stream_index);
  455. pkt->duration = 0;
  456. }
  457. /* duration field */
  458. if (pkt->duration == 0) {
  459. ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
  460. if (den && num) {
  461. pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
  462. }
  463. }
  464. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
  465. pkt->pts = pkt->dts;
  466. //XXX/FIXME this is a temporary hack until all encoders output pts
  467. if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
  468. static int warned;
  469. if (!warned) {
  470. av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
  471. warned = 1;
  472. }
  473. pkt->dts =
  474. // pkt->pts= st->cur_dts;
  475. pkt->pts = st->priv_pts->val;
  476. }
  477. //calculate dts from pts
  478. if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  479. st->pts_buffer[0] = pkt->pts;
  480. for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
  481. st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
  482. for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
  483. FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
  484. pkt->dts = st->pts_buffer[0];
  485. }
  486. if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
  487. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
  488. st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE &&
  489. st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
  490. av_log(s, AV_LOG_ERROR,
  491. "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
  492. st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
  493. return AVERROR(EINVAL);
  494. }
  495. if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
  496. av_log(s, AV_LOG_ERROR,
  497. "pts (%s) < dts (%s) in stream %d\n",
  498. av_ts2str(pkt->pts), av_ts2str(pkt->dts),
  499. st->index);
  500. return AVERROR(EINVAL);
  501. }
  502. if (s->debug & FF_FDEBUG_TS)
  503. av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%s dts2:%s\n",
  504. av_ts2str(pkt->pts), av_ts2str(pkt->dts));
  505. st->cur_dts = pkt->dts;
  506. st->priv_pts->val = pkt->dts;
  507. /* update pts */
  508. switch (st->codec->codec_type) {
  509. case AVMEDIA_TYPE_AUDIO:
  510. frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
  511. ((AVFrame *)pkt->data)->nb_samples :
  512. av_get_audio_frame_duration(st->codec, pkt->size);
  513. /* HACK/FIXME, we skip the initial 0 size packets as they are most
  514. * likely equal to the encoder delay, but it would be better if we
  515. * had the real timestamps from the encoder */
  516. if (frame_size >= 0 && (pkt->size || st->priv_pts->num != st->priv_pts->den >> 1 || st->priv_pts->val)) {
  517. frac_add(st->priv_pts, (int64_t)st->time_base.den * frame_size);
  518. }
  519. break;
  520. case AVMEDIA_TYPE_VIDEO:
  521. frac_add(st->priv_pts, (int64_t)st->time_base.den * st->codec->time_base.num);
  522. break;
  523. }
  524. return 0;
  525. }
  526. #endif
  527. /**
  528. * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
  529. * sidedata.
  530. *
  531. * FIXME: this function should NEVER get undefined pts/dts beside when the
  532. * AVFMT_NOTIMESTAMPS is set.
  533. * Those additional safety checks should be dropped once the correct checks
  534. * are set in the callers.
  535. */
  536. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  537. {
  538. int ret, did_split;
  539. if (s->output_ts_offset) {
  540. AVStream *st = s->streams[pkt->stream_index];
  541. int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
  542. if (pkt->dts != AV_NOPTS_VALUE)
  543. pkt->dts += offset;
  544. if (pkt->pts != AV_NOPTS_VALUE)
  545. pkt->pts += offset;
  546. }
  547. if (s->avoid_negative_ts > 0) {
  548. AVStream *st = s->streams[pkt->stream_index];
  549. int64_t offset = st->mux_ts_offset;
  550. int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
  551. if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
  552. (ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
  553. s->internal->offset = -ts;
  554. s->internal->offset_timebase = st->time_base;
  555. }
  556. if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
  557. offset = st->mux_ts_offset =
  558. av_rescale_q_rnd(s->internal->offset,
  559. s->internal->offset_timebase,
  560. st->time_base,
  561. AV_ROUND_UP);
  562. }
  563. if (pkt->dts != AV_NOPTS_VALUE)
  564. pkt->dts += offset;
  565. if (pkt->pts != AV_NOPTS_VALUE)
  566. pkt->pts += offset;
  567. if (s->internal->avoid_negative_ts_use_pts) {
  568. if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
  569. av_log(s, AV_LOG_WARNING, "failed to avoid negative "
  570. "pts %s in stream %d.\n"
  571. "Try -avoid_negative_ts 1 as a possible workaround.\n",
  572. av_ts2str(pkt->dts),
  573. pkt->stream_index
  574. );
  575. }
  576. } else {
  577. av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
  578. if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
  579. av_log(s, AV_LOG_WARNING,
  580. "Packets poorly interleaved, failed to avoid negative "
  581. "timestamp %s in stream %d.\n"
  582. "Try -max_interleave_delta 0 as a possible workaround.\n",
  583. av_ts2str(pkt->dts),
  584. pkt->stream_index
  585. );
  586. }
  587. }
  588. }
  589. did_split = av_packet_split_side_data(pkt);
  590. if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
  591. AVFrame *frame = (AVFrame *)pkt->data;
  592. av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
  593. ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
  594. av_frame_free(&frame);
  595. } else {
  596. ret = s->oformat->write_packet(s, pkt);
  597. }
  598. if (s->pb && ret >= 0) {
  599. if (s->flush_packets && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  600. avio_flush(s->pb);
  601. if (s->pb->error < 0)
  602. ret = s->pb->error;
  603. }
  604. if (did_split)
  605. av_packet_merge_side_data(pkt);
  606. return ret;
  607. }
  608. static int check_packet(AVFormatContext *s, AVPacket *pkt)
  609. {
  610. if (!pkt)
  611. return 0;
  612. if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
  613. av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
  614. pkt->stream_index);
  615. return AVERROR(EINVAL);
  616. }
  617. if (s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
  618. av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
  619. return AVERROR(EINVAL);
  620. }
  621. return 0;
  622. }
  623. static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt)
  624. {
  625. int ret;
  626. ret = check_packet(s, pkt);
  627. if (ret < 0)
  628. return ret;
  629. #if !FF_API_COMPUTE_PKT_FIELDS2
  630. /* sanitize the timestamps */
  631. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  632. AVStream *st = s->streams[pkt->stream_index];
  633. /* when there is no reordering (so dts is equal to pts), but
  634. * only one of them is set, set the other as well */
  635. if (!st->internal->reorder) {
  636. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
  637. pkt->pts = pkt->dts;
  638. if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
  639. pkt->dts = pkt->pts;
  640. }
  641. /* check that the timestamps are set */
  642. if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
  643. av_log(s, AV_LOG_ERROR,
  644. "Timestamps are unset in a packet for stream %d\n", st->index);
  645. return AVERROR(EINVAL);
  646. }
  647. /* check that the dts are increasing (or at least non-decreasing,
  648. * if the format allows it */
  649. if (st->cur_dts != AV_NOPTS_VALUE &&
  650. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) ||
  651. st->cur_dts > pkt->dts)) {
  652. av_log(s, AV_LOG_ERROR,
  653. "Application provided invalid, non monotonically increasing "
  654. "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  655. st->index, st->cur_dts, pkt->dts);
  656. return AVERROR(EINVAL);
  657. }
  658. if (pkt->pts < pkt->dts) {
  659. av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  660. pkt->pts, pkt->dts, st->index);
  661. return AVERROR(EINVAL);
  662. }
  663. }
  664. #endif
  665. return 0;
  666. }
  667. int av_write_frame(AVFormatContext *s, AVPacket *pkt)
  668. {
  669. int ret;
  670. ret = prepare_input_packet(s, pkt);
  671. if (ret < 0)
  672. return ret;
  673. if (!pkt) {
  674. if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
  675. ret = s->oformat->write_packet(s, NULL);
  676. if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  677. avio_flush(s->pb);
  678. if (ret >= 0 && s->pb && s->pb->error < 0)
  679. ret = s->pb->error;
  680. return ret;
  681. }
  682. return 1;
  683. }
  684. #if FF_API_COMPUTE_PKT_FIELDS2
  685. ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt);
  686. if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  687. return ret;
  688. #endif
  689. ret = write_packet(s, pkt);
  690. if (ret >= 0 && s->pb && s->pb->error < 0)
  691. ret = s->pb->error;
  692. if (ret >= 0)
  693. s->streams[pkt->stream_index]->nb_frames++;
  694. return ret;
  695. }
  696. #define CHUNK_START 0x1000
  697. int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  698. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
  699. {
  700. int ret;
  701. AVPacketList **next_point, *this_pktl;
  702. AVStream *st = s->streams[pkt->stream_index];
  703. int chunked = s->max_chunk_size || s->max_chunk_duration;
  704. this_pktl = av_mallocz(sizeof(AVPacketList));
  705. if (!this_pktl)
  706. return AVERROR(ENOMEM);
  707. if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
  708. av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
  709. av_assert0(((AVFrame *)pkt->data)->buf);
  710. }
  711. if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
  712. av_free(this_pktl);
  713. return ret;
  714. }
  715. if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
  716. next_point = &(st->last_in_packet_buffer->next);
  717. } else {
  718. next_point = &s->internal->packet_buffer;
  719. }
  720. if (chunked) {
  721. uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
  722. st->interleaver_chunk_size += pkt->size;
  723. st->interleaver_chunk_duration += pkt->duration;
  724. if ( (s->max_chunk_size && st->interleaver_chunk_size > s->max_chunk_size)
  725. || (max && st->interleaver_chunk_duration > max)) {
  726. st->interleaver_chunk_size = 0;
  727. this_pktl->pkt.flags |= CHUNK_START;
  728. if (max && st->interleaver_chunk_duration > max) {
  729. int64_t syncoffset = (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
  730. int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
  731. st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
  732. } else
  733. st->interleaver_chunk_duration = 0;
  734. }
  735. }
  736. if (*next_point) {
  737. if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
  738. goto next_non_null;
  739. if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
  740. while ( *next_point
  741. && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
  742. || !compare(s, &(*next_point)->pkt, pkt)))
  743. next_point = &(*next_point)->next;
  744. if (*next_point)
  745. goto next_non_null;
  746. } else {
  747. next_point = &(s->internal->packet_buffer_end->next);
  748. }
  749. }
  750. av_assert1(!*next_point);
  751. s->internal->packet_buffer_end = this_pktl;
  752. next_non_null:
  753. this_pktl->next = *next_point;
  754. s->streams[pkt->stream_index]->last_in_packet_buffer =
  755. *next_point = this_pktl;
  756. av_packet_unref(pkt);
  757. return 0;
  758. }
  759. static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
  760. AVPacket *pkt)
  761. {
  762. AVStream *st = s->streams[pkt->stream_index];
  763. AVStream *st2 = s->streams[next->stream_index];
  764. int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
  765. st->time_base);
  766. if (s->audio_preload && ((st->codec->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codec->codec_type == AVMEDIA_TYPE_AUDIO))) {
  767. int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO);
  768. int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO);
  769. if (ts == ts2) {
  770. ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den
  771. -( next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
  772. ts2=0;
  773. }
  774. comp= (ts>ts2) - (ts<ts2);
  775. }
  776. if (comp == 0)
  777. return pkt->stream_index < next->stream_index;
  778. return comp > 0;
  779. }
  780. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  781. AVPacket *pkt, int flush)
  782. {
  783. AVPacketList *pktl;
  784. int stream_count = 0;
  785. int noninterleaved_count = 0;
  786. int i, ret;
  787. if (pkt) {
  788. if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
  789. return ret;
  790. }
  791. for (i = 0; i < s->nb_streams; i++) {
  792. if (s->streams[i]->last_in_packet_buffer) {
  793. ++stream_count;
  794. } else if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
  795. s->streams[i]->codec->codec_id != AV_CODEC_ID_VP8 &&
  796. s->streams[i]->codec->codec_id != AV_CODEC_ID_VP9) {
  797. ++noninterleaved_count;
  798. }
  799. }
  800. if (s->internal->nb_interleaved_streams == stream_count)
  801. flush = 1;
  802. if (s->max_interleave_delta > 0 &&
  803. s->internal->packet_buffer &&
  804. !flush &&
  805. s->internal->nb_interleaved_streams == stream_count+noninterleaved_count
  806. ) {
  807. AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
  808. int64_t delta_dts = INT64_MIN;
  809. int64_t top_dts = av_rescale_q(top_pkt->dts,
  810. s->streams[top_pkt->stream_index]->time_base,
  811. AV_TIME_BASE_Q);
  812. for (i = 0; i < s->nb_streams; i++) {
  813. int64_t last_dts;
  814. const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
  815. if (!last)
  816. continue;
  817. last_dts = av_rescale_q(last->pkt.dts,
  818. s->streams[i]->time_base,
  819. AV_TIME_BASE_Q);
  820. delta_dts = FFMAX(delta_dts, last_dts - top_dts);
  821. }
  822. if (delta_dts > s->max_interleave_delta) {
  823. av_log(s, AV_LOG_DEBUG,
  824. "Delay between the first packet and last packet in the "
  825. "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
  826. delta_dts, s->max_interleave_delta);
  827. flush = 1;
  828. }
  829. }
  830. if (stream_count && flush) {
  831. AVStream *st;
  832. pktl = s->internal->packet_buffer;
  833. *out = pktl->pkt;
  834. st = s->streams[out->stream_index];
  835. s->internal->packet_buffer = pktl->next;
  836. if (!s->internal->packet_buffer)
  837. s->internal->packet_buffer_end = NULL;
  838. if (st->last_in_packet_buffer == pktl)
  839. st->last_in_packet_buffer = NULL;
  840. av_freep(&pktl);
  841. return 1;
  842. } else {
  843. av_init_packet(out);
  844. return 0;
  845. }
  846. }
  847. /**
  848. * Interleave an AVPacket correctly so it can be muxed.
  849. * @param out the interleaved packet will be output here
  850. * @param in the input packet
  851. * @param flush 1 if no further packets are available as input and all
  852. * remaining packets should be output
  853. * @return 1 if a packet was output, 0 if no packet could be output,
  854. * < 0 if an error occurred
  855. */
  856. static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
  857. {
  858. if (s->oformat->interleave_packet) {
  859. int ret = s->oformat->interleave_packet(s, out, in, flush);
  860. if (in)
  861. av_packet_unref(in);
  862. return ret;
  863. } else
  864. return ff_interleave_packet_per_dts(s, out, in, flush);
  865. }
  866. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
  867. {
  868. int ret, flush = 0;
  869. ret = prepare_input_packet(s, pkt);
  870. if (ret < 0)
  871. goto fail;
  872. if (pkt) {
  873. AVStream *st = s->streams[pkt->stream_index];
  874. if (s->debug & FF_FDEBUG_TS)
  875. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
  876. pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
  877. #if FF_API_COMPUTE_PKT_FIELDS2
  878. if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  879. goto fail;
  880. #endif
  881. if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  882. ret = AVERROR(EINVAL);
  883. goto fail;
  884. }
  885. } else {
  886. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
  887. flush = 1;
  888. }
  889. for (;; ) {
  890. AVPacket opkt;
  891. int ret = interleave_packet(s, &opkt, pkt, flush);
  892. if (pkt) {
  893. memset(pkt, 0, sizeof(*pkt));
  894. av_init_packet(pkt);
  895. pkt = NULL;
  896. }
  897. if (ret <= 0) //FIXME cleanup needed for ret<0 ?
  898. return ret;
  899. ret = write_packet(s, &opkt);
  900. if (ret >= 0)
  901. s->streams[opkt.stream_index]->nb_frames++;
  902. av_packet_unref(&opkt);
  903. if (ret < 0)
  904. return ret;
  905. if(s->pb && s->pb->error)
  906. return s->pb->error;
  907. }
  908. fail:
  909. av_packet_unref(pkt);
  910. return ret;
  911. }
  912. int av_write_trailer(AVFormatContext *s)
  913. {
  914. int ret, i;
  915. for (;; ) {
  916. AVPacket pkt;
  917. ret = interleave_packet(s, &pkt, NULL, 1);
  918. if (ret < 0)
  919. goto fail;
  920. if (!ret)
  921. break;
  922. ret = write_packet(s, &pkt);
  923. if (ret >= 0)
  924. s->streams[pkt.stream_index]->nb_frames++;
  925. av_packet_unref(&pkt);
  926. if (ret < 0)
  927. goto fail;
  928. if(s->pb && s->pb->error)
  929. goto fail;
  930. }
  931. fail:
  932. if (s->oformat->write_trailer)
  933. if (ret >= 0) {
  934. ret = s->oformat->write_trailer(s);
  935. } else {
  936. s->oformat->write_trailer(s);
  937. }
  938. if (s->pb)
  939. avio_flush(s->pb);
  940. if (ret == 0)
  941. ret = s->pb ? s->pb->error : 0;
  942. for (i = 0; i < s->nb_streams; i++) {
  943. av_freep(&s->streams[i]->priv_data);
  944. av_freep(&s->streams[i]->index_entries);
  945. }
  946. if (s->oformat->priv_class)
  947. av_opt_free(s->priv_data);
  948. av_freep(&s->priv_data);
  949. return ret;
  950. }
  951. int av_get_output_timestamp(struct AVFormatContext *s, int stream,
  952. int64_t *dts, int64_t *wall)
  953. {
  954. if (!s->oformat || !s->oformat->get_output_timestamp)
  955. return AVERROR(ENOSYS);
  956. s->oformat->get_output_timestamp(s, stream, dts, wall);
  957. return 0;
  958. }
  959. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  960. AVFormatContext *src, int interleave)
  961. {
  962. AVPacket local_pkt;
  963. int ret;
  964. local_pkt = *pkt;
  965. local_pkt.stream_index = dst_stream;
  966. if (pkt->pts != AV_NOPTS_VALUE)
  967. local_pkt.pts = av_rescale_q(pkt->pts,
  968. src->streams[pkt->stream_index]->time_base,
  969. dst->streams[dst_stream]->time_base);
  970. if (pkt->dts != AV_NOPTS_VALUE)
  971. local_pkt.dts = av_rescale_q(pkt->dts,
  972. src->streams[pkt->stream_index]->time_base,
  973. dst->streams[dst_stream]->time_base);
  974. if (pkt->duration)
  975. local_pkt.duration = av_rescale_q(pkt->duration,
  976. src->streams[pkt->stream_index]->time_base,
  977. dst->streams[dst_stream]->time_base);
  978. if (interleave) ret = av_interleaved_write_frame(dst, &local_pkt);
  979. else ret = av_write_frame(dst, &local_pkt);
  980. pkt->buf = local_pkt.buf;
  981. pkt->side_data = local_pkt.side_data;
  982. pkt->side_data_elems = local_pkt.side_data_elems;
  983. return ret;
  984. }
  985. static int av_write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
  986. AVFrame *frame, int interleaved)
  987. {
  988. AVPacket pkt, *pktp;
  989. av_assert0(s->oformat);
  990. if (!s->oformat->write_uncoded_frame)
  991. return AVERROR(ENOSYS);
  992. if (!frame) {
  993. pktp = NULL;
  994. } else {
  995. pktp = &pkt;
  996. av_init_packet(&pkt);
  997. pkt.data = (void *)frame;
  998. pkt.size = UNCODED_FRAME_PACKET_SIZE;
  999. pkt.pts =
  1000. pkt.dts = frame->pts;
  1001. pkt.duration = av_frame_get_pkt_duration(frame);
  1002. pkt.stream_index = stream_index;
  1003. pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME;
  1004. }
  1005. return interleaved ? av_interleaved_write_frame(s, pktp) :
  1006. av_write_frame(s, pktp);
  1007. }
  1008. int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
  1009. AVFrame *frame)
  1010. {
  1011. return av_write_uncoded_frame_internal(s, stream_index, frame, 0);
  1012. }
  1013. int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index,
  1014. AVFrame *frame)
  1015. {
  1016. return av_write_uncoded_frame_internal(s, stream_index, frame, 1);
  1017. }
  1018. int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
  1019. {
  1020. av_assert0(s->oformat);
  1021. if (!s->oformat->write_uncoded_frame)
  1022. return AVERROR(ENOSYS);
  1023. return s->oformat->write_uncoded_frame(s, stream_index, NULL,
  1024. AV_WRITE_UNCODED_FRAME_QUERY);
  1025. }