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.

758 lines
24KB

  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 (s->avoid_negative_ts > 0) {
  318. AVRational time_base = s->streams[pkt->stream_index]->time_base;
  319. int64_t offset = 0;
  320. if (s->internal->offset == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
  321. (pkt->dts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
  322. s->internal->offset = -pkt->dts;
  323. s->internal->offset_timebase = time_base;
  324. }
  325. if (s->internal->offset != AV_NOPTS_VALUE)
  326. offset = av_rescale_q(s->internal->offset, s->internal->offset_timebase, time_base);
  327. if (pkt->dts != AV_NOPTS_VALUE)
  328. pkt->dts += offset;
  329. if (pkt->pts != AV_NOPTS_VALUE)
  330. pkt->pts += offset;
  331. if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
  332. av_log(s, AV_LOG_WARNING,
  333. "Packets poorly interleaved, failed to avoid negative "
  334. "timestamp %"PRId64" in stream %d.\n"
  335. "Try -max_interleave_delta 0 as a possible workaround.\n",
  336. pkt->dts, pkt->stream_index);
  337. }
  338. }
  339. ret = s->oformat->write_packet(s, pkt);
  340. if (s->pb && ret >= 0) {
  341. if (s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  342. avio_flush(s->pb);
  343. if (s->pb->error < 0)
  344. ret = s->pb->error;
  345. }
  346. return ret;
  347. }
  348. static int check_packet(AVFormatContext *s, AVPacket *pkt)
  349. {
  350. if (!pkt)
  351. return 0;
  352. if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
  353. av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
  354. pkt->stream_index);
  355. return AVERROR(EINVAL);
  356. }
  357. if (s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
  358. av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
  359. return AVERROR(EINVAL);
  360. }
  361. return 0;
  362. }
  363. static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt)
  364. {
  365. int ret;
  366. ret = check_packet(s, pkt);
  367. if (ret < 0)
  368. return ret;
  369. #if !FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  370. /* sanitize the timestamps */
  371. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  372. AVStream *st = s->streams[pkt->stream_index];
  373. /* when there is no reordering (so dts is equal to pts), but
  374. * only one of them is set, set the other as well */
  375. if (!st->internal->reorder) {
  376. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
  377. pkt->pts = pkt->dts;
  378. if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
  379. pkt->dts = pkt->pts;
  380. }
  381. /* check that the timestamps are set */
  382. if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
  383. av_log(s, AV_LOG_ERROR,
  384. "Timestamps are unset in a packet for stream %d\n", st->index);
  385. return AVERROR(EINVAL);
  386. }
  387. /* check that the dts are increasing (or at least non-decreasing,
  388. * if the format allows it */
  389. if (st->cur_dts != AV_NOPTS_VALUE &&
  390. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) ||
  391. st->cur_dts > pkt->dts)) {
  392. av_log(s, AV_LOG_ERROR,
  393. "Application provided invalid, non monotonically increasing "
  394. "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  395. st->index, st->cur_dts, pkt->dts);
  396. return AVERROR(EINVAL);
  397. }
  398. if (pkt->pts < pkt->dts) {
  399. av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  400. pkt->pts, pkt->dts, st->index);
  401. return AVERROR(EINVAL);
  402. }
  403. }
  404. #endif
  405. return 0;
  406. }
  407. int av_write_frame(AVFormatContext *s, AVPacket *pkt)
  408. {
  409. int ret;
  410. ret = prepare_input_packet(s, pkt);
  411. if (ret < 0)
  412. return ret;
  413. if (!pkt) {
  414. if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
  415. return s->oformat->write_packet(s, pkt);
  416. return 1;
  417. }
  418. #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  419. ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
  420. if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  421. return ret;
  422. #endif
  423. ret = write_packet(s, pkt);
  424. if (ret >= 0)
  425. s->streams[pkt->stream_index]->nb_frames++;
  426. return ret;
  427. }
  428. int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  429. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
  430. {
  431. int ret;
  432. AVPacketList **next_point, *this_pktl;
  433. this_pktl = av_mallocz(sizeof(AVPacketList));
  434. if (!this_pktl)
  435. return AVERROR(ENOMEM);
  436. if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
  437. av_free(this_pktl);
  438. return ret;
  439. }
  440. if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
  441. next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
  442. } else
  443. next_point = &s->internal->packet_buffer;
  444. if (*next_point) {
  445. if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
  446. while (!compare(s, &(*next_point)->pkt, pkt))
  447. next_point = &(*next_point)->next;
  448. goto next_non_null;
  449. } else {
  450. next_point = &(s->internal->packet_buffer_end->next);
  451. }
  452. }
  453. assert(!*next_point);
  454. s->internal->packet_buffer_end = this_pktl;
  455. next_non_null:
  456. this_pktl->next = *next_point;
  457. s->streams[pkt->stream_index]->last_in_packet_buffer =
  458. *next_point = this_pktl;
  459. av_packet_unref(pkt);
  460. return 0;
  461. }
  462. static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
  463. AVPacket *pkt)
  464. {
  465. AVStream *st = s->streams[pkt->stream_index];
  466. AVStream *st2 = s->streams[next->stream_index];
  467. int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
  468. st->time_base);
  469. if (comp == 0)
  470. return pkt->stream_index < next->stream_index;
  471. return comp > 0;
  472. }
  473. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  474. AVPacket *pkt, int flush)
  475. {
  476. AVPacketList *pktl;
  477. int stream_count = 0;
  478. int i, ret;
  479. if (pkt) {
  480. if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
  481. return ret;
  482. }
  483. if (s->max_interleave_delta > 0 && s->internal->packet_buffer && !flush) {
  484. AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
  485. int64_t delta_dts = INT64_MIN;
  486. int64_t top_dts = av_rescale_q(top_pkt->dts,
  487. s->streams[top_pkt->stream_index]->time_base,
  488. AV_TIME_BASE_Q);
  489. for (i = 0; i < s->nb_streams; i++) {
  490. int64_t last_dts;
  491. const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
  492. if (!last)
  493. continue;
  494. last_dts = av_rescale_q(last->pkt.dts,
  495. s->streams[i]->time_base,
  496. AV_TIME_BASE_Q);
  497. delta_dts = FFMAX(delta_dts, last_dts - top_dts);
  498. stream_count++;
  499. }
  500. if (delta_dts > s->max_interleave_delta) {
  501. av_log(s, AV_LOG_DEBUG,
  502. "Delay between the first packet and last packet in the "
  503. "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
  504. delta_dts, s->max_interleave_delta);
  505. flush = 1;
  506. }
  507. } else {
  508. for (i = 0; i < s->nb_streams; i++)
  509. stream_count += !!s->streams[i]->last_in_packet_buffer;
  510. }
  511. if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
  512. pktl = s->internal->packet_buffer;
  513. *out = pktl->pkt;
  514. s->internal->packet_buffer = pktl->next;
  515. if (!s->internal->packet_buffer)
  516. s->internal->packet_buffer_end = NULL;
  517. if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
  518. s->streams[out->stream_index]->last_in_packet_buffer = NULL;
  519. av_freep(&pktl);
  520. return 1;
  521. } else {
  522. av_init_packet(out);
  523. return 0;
  524. }
  525. }
  526. const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
  527. {
  528. AVPacketList *pktl = s->internal->packet_buffer;
  529. while (pktl) {
  530. if (pktl->pkt.stream_index == stream)
  531. return &pktl->pkt;
  532. pktl = pktl->next;
  533. }
  534. return NULL;
  535. }
  536. /**
  537. * Interleave an AVPacket correctly so it can be muxed.
  538. * @param out the interleaved packet will be output here
  539. * @param in the input packet
  540. * @param flush 1 if no further packets are available as input and all
  541. * remaining packets should be output
  542. * @return 1 if a packet was output, 0 if no packet could be output,
  543. * < 0 if an error occurred
  544. */
  545. static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
  546. {
  547. if (s->oformat->interleave_packet) {
  548. int ret = s->oformat->interleave_packet(s, out, in, flush);
  549. if (in)
  550. av_packet_unref(in);
  551. return ret;
  552. } else
  553. return ff_interleave_packet_per_dts(s, out, in, flush);
  554. }
  555. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
  556. {
  557. int ret, flush = 0;
  558. ret = prepare_input_packet(s, pkt);
  559. if (ret < 0)
  560. goto fail;
  561. if (pkt) {
  562. #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
  563. AVStream *st = s->streams[pkt->stream_index];
  564. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
  565. pkt->size, pkt->dts, pkt->pts);
  566. if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  567. goto fail;
  568. #endif
  569. if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  570. ret = AVERROR(EINVAL);
  571. goto fail;
  572. }
  573. } else {
  574. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
  575. flush = 1;
  576. }
  577. for (;; ) {
  578. AVPacket opkt;
  579. int ret = interleave_packet(s, &opkt, pkt, flush);
  580. if (pkt) {
  581. memset(pkt, 0, sizeof(*pkt));
  582. av_init_packet(pkt);
  583. pkt = NULL;
  584. }
  585. if (ret <= 0) //FIXME cleanup needed for ret<0 ?
  586. return ret;
  587. ret = write_packet(s, &opkt);
  588. if (ret >= 0)
  589. s->streams[opkt.stream_index]->nb_frames++;
  590. av_packet_unref(&opkt);
  591. if (ret < 0)
  592. return ret;
  593. }
  594. fail:
  595. av_packet_unref(pkt);
  596. return ret;
  597. }
  598. int av_write_trailer(AVFormatContext *s)
  599. {
  600. int ret, i;
  601. for (;; ) {
  602. AVPacket pkt;
  603. ret = interleave_packet(s, &pkt, NULL, 1);
  604. if (ret < 0) //FIXME cleanup needed for ret<0 ?
  605. goto fail;
  606. if (!ret)
  607. break;
  608. ret = write_packet(s, &pkt);
  609. if (ret >= 0)
  610. s->streams[pkt.stream_index]->nb_frames++;
  611. av_packet_unref(&pkt);
  612. if (ret < 0)
  613. goto fail;
  614. }
  615. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  616. avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);
  617. if (s->oformat->write_trailer)
  618. ret = s->oformat->write_trailer(s);
  619. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  620. avio_flush(s->pb);
  621. fail:
  622. for (i = 0; i < s->nb_streams; i++) {
  623. av_freep(&s->streams[i]->priv_data);
  624. av_freep(&s->streams[i]->index_entries);
  625. }
  626. if (s->oformat->priv_class)
  627. av_opt_free(s->priv_data);
  628. av_freep(&s->priv_data);
  629. return ret;
  630. }
  631. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  632. AVFormatContext *src)
  633. {
  634. AVPacket local_pkt;
  635. local_pkt = *pkt;
  636. local_pkt.stream_index = dst_stream;
  637. if (pkt->pts != AV_NOPTS_VALUE)
  638. local_pkt.pts = av_rescale_q(pkt->pts,
  639. src->streams[pkt->stream_index]->time_base,
  640. dst->streams[dst_stream]->time_base);
  641. if (pkt->dts != AV_NOPTS_VALUE)
  642. local_pkt.dts = av_rescale_q(pkt->dts,
  643. src->streams[pkt->stream_index]->time_base,
  644. dst->streams[dst_stream]->time_base);
  645. return av_write_frame(dst, &local_pkt);
  646. }