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.

772 lines
25KB

  1. /*
  2. * muxing functions for use within Libav
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "metadata.h"
  30. #include "id3v2.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/parseutils.h"
  35. #include "libavutil/time.h"
  36. #include "riff.h"
  37. #include "audiointerleave.h"
  38. #include "url.h"
  39. #include <stdarg.h>
  40. #if CONFIG_NETWORK
  41. #include "network.h"
  42. #endif
  43. #undef NDEBUG
  44. #include <assert.h>
  45. /**
  46. * @file
  47. * muxing functions for use within Libav
  48. */
  49. static int validate_codec_tag(AVFormatContext *s, AVStream *st)
  50. {
  51. const AVCodecTag *avctag;
  52. int n;
  53. enum AVCodecID id = AV_CODEC_ID_NONE;
  54. unsigned int tag = 0;
  55. /**
  56. * Check that tag + id is in the table
  57. * If neither is in the table -> OK
  58. * If tag is in the table with another id -> FAIL
  59. * If id is in the table with another tag -> FAIL unless strict < normal
  60. */
  61. for (n = 0; s->oformat->codec_tag[n]; n++) {
  62. avctag = s->oformat->codec_tag[n];
  63. while (avctag->id != AV_CODEC_ID_NONE) {
  64. if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codecpar->codec_tag)) {
  65. id = avctag->id;
  66. if (id == st->codecpar->codec_id)
  67. return 1;
  68. }
  69. if (avctag->id == st->codecpar->codec_id)
  70. tag = avctag->tag;
  71. avctag++;
  72. }
  73. }
  74. if (id != AV_CODEC_ID_NONE)
  75. return 0;
  76. if (tag && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
  77. return 0;
  78. return 1;
  79. }
  80. static int init_muxer(AVFormatContext *s, AVDictionary **options)
  81. {
  82. int ret = 0, i;
  83. AVStream *st;
  84. AVDictionary *tmp = NULL;
  85. AVCodecParameters *par = NULL;
  86. AVOutputFormat *of = s->oformat;
  87. const AVCodecDescriptor *desc;
  88. if (options)
  89. av_dict_copy(&tmp, *options, 0);
  90. if ((ret = av_opt_set_dict(s, &tmp)) < 0)
  91. goto fail;
  92. #if FF_API_LAVF_BITEXACT && FF_API_LAVF_AVCTX
  93. FF_DISABLE_DEPRECATION_WARNINGS
  94. if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT)
  95. s->flags |= AVFMT_FLAG_BITEXACT;
  96. FF_ENABLE_DEPRECATION_WARNINGS
  97. #endif
  98. // some sanity checks
  99. if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
  100. av_log(s, AV_LOG_ERROR, "no streams\n");
  101. ret = AVERROR(EINVAL);
  102. goto fail;
  103. }
  104. for (i = 0; i < s->nb_streams; i++) {
  105. st = s->streams[i];
  106. par = st->codecpar;
  107. #if FF_API_LAVF_CODEC_TB && FF_API_LAVF_AVCTX
  108. FF_DISABLE_DEPRECATION_WARNINGS
  109. if (!st->time_base.num && st->codec->time_base.num) {
  110. av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
  111. "timebase hint to the muxer is deprecated. Set "
  112. "AVStream.time_base instead.\n");
  113. avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  114. }
  115. FF_ENABLE_DEPRECATION_WARNINGS
  116. #endif
  117. #if FF_API_LAVF_AVCTX
  118. FF_DISABLE_DEPRECATION_WARNINGS
  119. if (st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
  120. st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN) {
  121. av_log(s, AV_LOG_WARNING, "Using AVStream.codec to pass codec "
  122. "parameters to muxers is deprecated, use AVStream.codecpar "
  123. "instead.\n");
  124. ret = avcodec_parameters_from_context(st->codecpar, st->codec);
  125. if (ret < 0)
  126. goto fail;
  127. }
  128. FF_ENABLE_DEPRECATION_WARNINGS
  129. #endif
  130. if (!st->time_base.num) {
  131. /* fall back on the default timebase values */
  132. if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate)
  133. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  134. else
  135. avpriv_set_pts_info(st, 33, 1, 90000);
  136. }
  137. switch (par->codec_type) {
  138. case AVMEDIA_TYPE_AUDIO:
  139. if (par->sample_rate <= 0) {
  140. av_log(s, AV_LOG_ERROR, "sample rate not set\n");
  141. ret = AVERROR(EINVAL);
  142. goto fail;
  143. }
  144. if (!par->block_align)
  145. par->block_align = par->channels *
  146. av_get_bits_per_sample(par->codec_id) >> 3;
  147. break;
  148. case AVMEDIA_TYPE_VIDEO:
  149. if ((par->width <= 0 || par->height <= 0) &&
  150. !(of->flags & AVFMT_NODIMENSIONS)) {
  151. av_log(s, AV_LOG_ERROR, "dimensions not set\n");
  152. ret = AVERROR(EINVAL);
  153. goto fail;
  154. }
  155. if (av_cmp_q(st->sample_aspect_ratio,
  156. par->sample_aspect_ratio)) {
  157. if (st->sample_aspect_ratio.num != 0 &&
  158. st->sample_aspect_ratio.den != 0 &&
  159. par->sample_aspect_ratio.den != 0 &&
  160. par->sample_aspect_ratio.den != 0) {
  161. av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
  162. "(%d/%d) and encoder layer (%d/%d)\n",
  163. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  164. par->sample_aspect_ratio.num,
  165. par->sample_aspect_ratio.den);
  166. ret = AVERROR(EINVAL);
  167. goto fail;
  168. }
  169. }
  170. break;
  171. }
  172. desc = avcodec_descriptor_get(par->codec_id);
  173. if (desc && desc->props & AV_CODEC_PROP_REORDER)
  174. st->internal->reorder = 1;
  175. if (of->codec_tag) {
  176. if (par->codec_tag &&
  177. par->codec_id == AV_CODEC_ID_RAWVIDEO &&
  178. !av_codec_get_tag(of->codec_tag, par->codec_id) &&
  179. !validate_codec_tag(s, st)) {
  180. // the current rawvideo encoding system ends up setting
  181. // the wrong codec_tag for avi, we override it here
  182. par->codec_tag = 0;
  183. }
  184. if (par->codec_tag) {
  185. if (!validate_codec_tag(s, st)) {
  186. char tagbuf[32];
  187. av_get_codec_tag_string(tagbuf, sizeof(tagbuf), par->codec_tag);
  188. av_log(s, AV_LOG_ERROR,
  189. "Tag %s/0x%08x incompatible with output codec id '%d'\n",
  190. tagbuf, par->codec_tag, par->codec_id);
  191. ret = AVERROR_INVALIDDATA;
  192. goto fail;
  193. }
  194. } else
  195. par->codec_tag = av_codec_get_tag(of->codec_tag, par->codec_id);
  196. }
  197. if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT)
  198. s->internal->nb_interleaved_streams++;
  199. }
  200. if (!s->priv_data && of->priv_data_size > 0) {
  201. s->priv_data = av_mallocz(of->priv_data_size);
  202. if (!s->priv_data) {
  203. ret = AVERROR(ENOMEM);
  204. goto fail;
  205. }
  206. if (of->priv_class) {
  207. *(const AVClass **)s->priv_data = of->priv_class;
  208. av_opt_set_defaults(s->priv_data);
  209. if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
  210. goto fail;
  211. }
  212. }
  213. /* set muxer identification string */
  214. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  215. av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
  216. }
  217. if (options) {
  218. av_dict_free(options);
  219. *options = tmp;
  220. }
  221. return 0;
  222. fail:
  223. av_dict_free(&tmp);
  224. return ret;
  225. }
  226. int avformat_write_header(AVFormatContext *s, AVDictionary **options)
  227. {
  228. int ret = 0;
  229. if (ret = init_muxer(s, options))
  230. return ret;
  231. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  232. avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
  233. if (s->oformat->write_header) {
  234. ret = s->oformat->write_header(s);
  235. if (ret < 0)
  236. return ret;
  237. }
  238. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  239. avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
  240. if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO) {
  241. if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
  242. s->avoid_negative_ts = 0;
  243. } else
  244. s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
  245. }
  246. return 0;
  247. }
  248. #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  249. FF_DISABLE_DEPRECATION_WARNINGS
  250. //FIXME merge with compute_pkt_fields
  251. static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  252. {
  253. int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
  254. int num, den, i;
  255. if (!s->internal->missing_ts_warning &&
  256. !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
  257. (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
  258. av_log(s, AV_LOG_WARNING,
  259. "Timestamps are unset in a packet for stream %d. "
  260. "This is deprecated and will stop working in the future. "
  261. "Fix your code to set the timestamps properly\n", st->index);
  262. s->internal->missing_ts_warning = 1;
  263. }
  264. av_log(s, AV_LOG_TRACE, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
  265. pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
  266. /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
  267. * return AVERROR(EINVAL);*/
  268. /* duration field */
  269. if (pkt->duration == 0) {
  270. ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
  271. if (den && num) {
  272. pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
  273. }
  274. }
  275. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
  276. pkt->pts = pkt->dts;
  277. //calculate dts from pts
  278. if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  279. st->pts_buffer[0] = pkt->pts;
  280. for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
  281. st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
  282. for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
  283. FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
  284. pkt->dts = st->pts_buffer[0];
  285. }
  286. if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
  287. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
  288. st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
  289. av_log(s, AV_LOG_ERROR,
  290. "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  291. st->index, st->cur_dts, pkt->dts);
  292. return AVERROR(EINVAL);
  293. }
  294. if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
  295. av_log(s, AV_LOG_ERROR,
  296. "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  297. pkt->pts, pkt->dts,
  298. st->index);
  299. return AVERROR(EINVAL);
  300. }
  301. av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
  302. pkt->pts, pkt->dts);
  303. st->cur_dts = pkt->dts;
  304. return 0;
  305. }
  306. FF_ENABLE_DEPRECATION_WARNINGS
  307. #endif
  308. /*
  309. * FIXME: this function should NEVER get undefined pts/dts beside when the
  310. * AVFMT_NOTIMESTAMPS is set.
  311. * Those additional safety checks should be dropped once the correct checks
  312. * are set in the callers.
  313. */
  314. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  315. {
  316. int ret;
  317. // If the timestamp offsetting below is adjusted, adjust
  318. // ff_interleaved_peek similarly.
  319. if (s->avoid_negative_ts > 0) {
  320. AVRational time_base = s->streams[pkt->stream_index]->time_base;
  321. int64_t offset = 0;
  322. if (s->internal->offset == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
  323. (pkt->dts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
  324. s->internal->offset = -pkt->dts;
  325. s->internal->offset_timebase = time_base;
  326. }
  327. if (s->internal->offset != AV_NOPTS_VALUE)
  328. offset = av_rescale_q(s->internal->offset, s->internal->offset_timebase, time_base);
  329. if (pkt->dts != AV_NOPTS_VALUE)
  330. pkt->dts += offset;
  331. if (pkt->pts != AV_NOPTS_VALUE)
  332. pkt->pts += offset;
  333. if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
  334. av_log(s, AV_LOG_WARNING,
  335. "Packets poorly interleaved, failed to avoid negative "
  336. "timestamp %"PRId64" in stream %d.\n"
  337. "Try -max_interleave_delta 0 as a possible workaround.\n",
  338. pkt->dts, pkt->stream_index);
  339. }
  340. }
  341. ret = s->oformat->write_packet(s, pkt);
  342. if (s->pb && ret >= 0) {
  343. if (s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  344. avio_flush(s->pb);
  345. if (s->pb->error < 0)
  346. ret = s->pb->error;
  347. }
  348. return ret;
  349. }
  350. static int check_packet(AVFormatContext *s, AVPacket *pkt)
  351. {
  352. if (!pkt)
  353. return 0;
  354. if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
  355. av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
  356. pkt->stream_index);
  357. return AVERROR(EINVAL);
  358. }
  359. if (s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
  360. av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
  361. return AVERROR(EINVAL);
  362. }
  363. return 0;
  364. }
  365. static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt)
  366. {
  367. int ret;
  368. ret = check_packet(s, pkt);
  369. if (ret < 0)
  370. return ret;
  371. #if !FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  372. /* sanitize the timestamps */
  373. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  374. AVStream *st = s->streams[pkt->stream_index];
  375. /* when there is no reordering (so dts is equal to pts), but
  376. * only one of them is set, set the other as well */
  377. if (!st->internal->reorder) {
  378. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
  379. pkt->pts = pkt->dts;
  380. if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
  381. pkt->dts = pkt->pts;
  382. }
  383. /* check that the timestamps are set */
  384. if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
  385. av_log(s, AV_LOG_ERROR,
  386. "Timestamps are unset in a packet for stream %d\n", st->index);
  387. return AVERROR(EINVAL);
  388. }
  389. /* check that the dts are increasing (or at least non-decreasing,
  390. * if the format allows it */
  391. if (st->cur_dts != AV_NOPTS_VALUE &&
  392. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) ||
  393. st->cur_dts > pkt->dts)) {
  394. av_log(s, AV_LOG_ERROR,
  395. "Application provided invalid, non monotonically increasing "
  396. "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  397. st->index, st->cur_dts, pkt->dts);
  398. return AVERROR(EINVAL);
  399. }
  400. if (pkt->pts < pkt->dts) {
  401. av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  402. pkt->pts, pkt->dts, st->index);
  403. return AVERROR(EINVAL);
  404. }
  405. }
  406. #endif
  407. return 0;
  408. }
  409. int av_write_frame(AVFormatContext *s, AVPacket *pkt)
  410. {
  411. int ret;
  412. ret = prepare_input_packet(s, pkt);
  413. if (ret < 0)
  414. return ret;
  415. if (!pkt) {
  416. if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
  417. return s->oformat->write_packet(s, pkt);
  418. return 1;
  419. }
  420. #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  421. ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
  422. if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  423. return ret;
  424. #endif
  425. ret = write_packet(s, pkt);
  426. if (ret >= 0)
  427. s->streams[pkt->stream_index]->nb_frames++;
  428. return ret;
  429. }
  430. int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  431. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
  432. {
  433. int ret;
  434. AVPacketList **next_point, *this_pktl;
  435. this_pktl = av_mallocz(sizeof(AVPacketList));
  436. if (!this_pktl)
  437. return AVERROR(ENOMEM);
  438. if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
  439. av_free(this_pktl);
  440. return ret;
  441. }
  442. if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
  443. next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
  444. } else
  445. next_point = &s->internal->packet_buffer;
  446. if (*next_point) {
  447. if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
  448. while (!compare(s, &(*next_point)->pkt, pkt))
  449. next_point = &(*next_point)->next;
  450. goto next_non_null;
  451. } else {
  452. next_point = &(s->internal->packet_buffer_end->next);
  453. }
  454. }
  455. assert(!*next_point);
  456. s->internal->packet_buffer_end = this_pktl;
  457. next_non_null:
  458. this_pktl->next = *next_point;
  459. s->streams[pkt->stream_index]->last_in_packet_buffer =
  460. *next_point = this_pktl;
  461. av_packet_unref(pkt);
  462. return 0;
  463. }
  464. static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
  465. AVPacket *pkt)
  466. {
  467. AVStream *st = s->streams[pkt->stream_index];
  468. AVStream *st2 = s->streams[next->stream_index];
  469. int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
  470. st->time_base);
  471. if (comp == 0)
  472. return pkt->stream_index < next->stream_index;
  473. return comp > 0;
  474. }
  475. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  476. AVPacket *pkt, int flush)
  477. {
  478. AVPacketList *pktl;
  479. int stream_count = 0;
  480. int i, ret;
  481. if (pkt) {
  482. if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
  483. return ret;
  484. }
  485. if (s->max_interleave_delta > 0 && s->internal->packet_buffer && !flush) {
  486. AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
  487. int64_t delta_dts = INT64_MIN;
  488. int64_t top_dts = av_rescale_q(top_pkt->dts,
  489. s->streams[top_pkt->stream_index]->time_base,
  490. AV_TIME_BASE_Q);
  491. for (i = 0; i < s->nb_streams; i++) {
  492. int64_t last_dts;
  493. const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
  494. if (!last)
  495. continue;
  496. last_dts = av_rescale_q(last->pkt.dts,
  497. s->streams[i]->time_base,
  498. AV_TIME_BASE_Q);
  499. delta_dts = FFMAX(delta_dts, last_dts - top_dts);
  500. stream_count++;
  501. }
  502. if (delta_dts > s->max_interleave_delta) {
  503. av_log(s, AV_LOG_DEBUG,
  504. "Delay between the first packet and last packet in the "
  505. "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
  506. delta_dts, s->max_interleave_delta);
  507. flush = 1;
  508. }
  509. } else {
  510. for (i = 0; i < s->nb_streams; i++)
  511. stream_count += !!s->streams[i]->last_in_packet_buffer;
  512. }
  513. if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
  514. pktl = s->internal->packet_buffer;
  515. *out = pktl->pkt;
  516. s->internal->packet_buffer = pktl->next;
  517. if (!s->internal->packet_buffer)
  518. s->internal->packet_buffer_end = NULL;
  519. if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
  520. s->streams[out->stream_index]->last_in_packet_buffer = NULL;
  521. av_freep(&pktl);
  522. return 1;
  523. } else {
  524. av_init_packet(out);
  525. return 0;
  526. }
  527. }
  528. int ff_interleaved_peek(AVFormatContext *s, int stream,
  529. AVPacket *pkt, int add_offset)
  530. {
  531. AVPacketList *pktl = s->internal->packet_buffer;
  532. while (pktl) {
  533. if (pktl->pkt.stream_index == stream) {
  534. *pkt = pktl->pkt;
  535. if (add_offset && s->internal->offset != AV_NOPTS_VALUE) {
  536. int64_t offset = av_rescale_q(s->internal->offset,
  537. s->internal->offset_timebase,
  538. s->streams[stream]->time_base);
  539. if (pkt->dts != AV_NOPTS_VALUE)
  540. pkt->dts += offset;
  541. if (pkt->pts != AV_NOPTS_VALUE)
  542. pkt->pts += offset;
  543. }
  544. return 0;
  545. }
  546. pktl = pktl->next;
  547. }
  548. return AVERROR(ENOENT);
  549. }
  550. /**
  551. * Interleave an AVPacket correctly so it can be muxed.
  552. * @param out the interleaved packet will be output here
  553. * @param in the input packet
  554. * @param flush 1 if no further packets are available as input and all
  555. * remaining packets should be output
  556. * @return 1 if a packet was output, 0 if no packet could be output,
  557. * < 0 if an error occurred
  558. */
  559. static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
  560. {
  561. if (s->oformat->interleave_packet) {
  562. int ret = s->oformat->interleave_packet(s, out, in, flush);
  563. if (in)
  564. av_packet_unref(in);
  565. return ret;
  566. } else
  567. return ff_interleave_packet_per_dts(s, out, in, flush);
  568. }
  569. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
  570. {
  571. int ret, flush = 0;
  572. ret = prepare_input_packet(s, pkt);
  573. if (ret < 0)
  574. goto fail;
  575. if (pkt) {
  576. #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  577. AVStream *st = s->streams[pkt->stream_index];
  578. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
  579. pkt->size, pkt->dts, pkt->pts);
  580. if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  581. goto fail;
  582. #endif
  583. if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  584. ret = AVERROR(EINVAL);
  585. goto fail;
  586. }
  587. } else {
  588. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
  589. flush = 1;
  590. }
  591. for (;; ) {
  592. AVPacket opkt;
  593. int ret = interleave_packet(s, &opkt, pkt, flush);
  594. if (pkt) {
  595. memset(pkt, 0, sizeof(*pkt));
  596. av_init_packet(pkt);
  597. pkt = NULL;
  598. }
  599. if (ret <= 0) //FIXME cleanup needed for ret<0 ?
  600. return ret;
  601. ret = write_packet(s, &opkt);
  602. if (ret >= 0)
  603. s->streams[opkt.stream_index]->nb_frames++;
  604. av_packet_unref(&opkt);
  605. if (ret < 0)
  606. return ret;
  607. }
  608. fail:
  609. av_packet_unref(pkt);
  610. return ret;
  611. }
  612. int av_write_trailer(AVFormatContext *s)
  613. {
  614. int ret, i;
  615. for (;; ) {
  616. AVPacket pkt;
  617. ret = interleave_packet(s, &pkt, NULL, 1);
  618. if (ret < 0) //FIXME cleanup needed for ret<0 ?
  619. goto fail;
  620. if (!ret)
  621. break;
  622. ret = write_packet(s, &pkt);
  623. if (ret >= 0)
  624. s->streams[pkt.stream_index]->nb_frames++;
  625. av_packet_unref(&pkt);
  626. if (ret < 0)
  627. goto fail;
  628. }
  629. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  630. avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);
  631. if (s->oformat->write_trailer)
  632. ret = s->oformat->write_trailer(s);
  633. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  634. avio_flush(s->pb);
  635. fail:
  636. for (i = 0; i < s->nb_streams; i++) {
  637. av_freep(&s->streams[i]->priv_data);
  638. av_freep(&s->streams[i]->index_entries);
  639. }
  640. if (s->oformat->priv_class)
  641. av_opt_free(s->priv_data);
  642. av_freep(&s->priv_data);
  643. return ret;
  644. }
  645. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  646. AVFormatContext *src)
  647. {
  648. AVPacket local_pkt;
  649. local_pkt = *pkt;
  650. local_pkt.stream_index = dst_stream;
  651. if (pkt->pts != AV_NOPTS_VALUE)
  652. local_pkt.pts = av_rescale_q(pkt->pts,
  653. src->streams[pkt->stream_index]->time_base,
  654. dst->streams[dst_stream]->time_base);
  655. if (pkt->dts != AV_NOPTS_VALUE)
  656. local_pkt.dts = av_rescale_q(pkt->dts,
  657. src->streams[pkt->stream_index]->time_base,
  658. dst->streams[dst_stream]->time_base);
  659. return av_write_frame(dst, &local_pkt);
  660. }