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.

730 lines
23KB

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