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.

724 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 (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
  184. s->internal->nb_interleaved_streams++;
  185. }
  186. if (!s->priv_data && of->priv_data_size > 0) {
  187. s->priv_data = av_mallocz(of->priv_data_size);
  188. if (!s->priv_data) {
  189. ret = AVERROR(ENOMEM);
  190. goto fail;
  191. }
  192. if (of->priv_class) {
  193. *(const AVClass **)s->priv_data = of->priv_class;
  194. av_opt_set_defaults(s->priv_data);
  195. if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
  196. goto fail;
  197. }
  198. }
  199. /* set muxer identification string */
  200. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  201. av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
  202. }
  203. if (options) {
  204. av_dict_free(options);
  205. *options = tmp;
  206. }
  207. return 0;
  208. fail:
  209. av_dict_free(&tmp);
  210. return ret;
  211. }
  212. int avformat_write_header(AVFormatContext *s, AVDictionary **options)
  213. {
  214. int ret = 0;
  215. if (ret = init_muxer(s, options))
  216. return ret;
  217. if (s->oformat->write_header) {
  218. ret = s->oformat->write_header(s);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO) {
  223. if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
  224. s->avoid_negative_ts = 0;
  225. } else
  226. s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
  227. }
  228. return 0;
  229. }
  230. #if FF_API_COMPUTE_PKT_FIELDS2
  231. //FIXME merge with compute_pkt_fields
  232. static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  233. {
  234. int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
  235. int num, den, i;
  236. if (!s->internal->missing_ts_warning &&
  237. !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
  238. (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
  239. av_log(s, AV_LOG_WARNING,
  240. "Timestamps are unset in a packet for stream %d. "
  241. "This is deprecated and will stop working in the future. "
  242. "Fix your code to set the timestamps properly\n", st->index);
  243. s->internal->missing_ts_warning = 1;
  244. }
  245. av_log(s, AV_LOG_TRACE, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
  246. pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
  247. /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
  248. * return AVERROR(EINVAL);*/
  249. /* duration field */
  250. if (pkt->duration == 0) {
  251. ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
  252. if (den && num) {
  253. pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
  254. }
  255. }
  256. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
  257. pkt->pts = pkt->dts;
  258. //calculate dts from pts
  259. if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  260. st->pts_buffer[0] = pkt->pts;
  261. for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
  262. st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
  263. for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
  264. FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
  265. pkt->dts = st->pts_buffer[0];
  266. }
  267. if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
  268. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
  269. st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
  270. av_log(s, AV_LOG_ERROR,
  271. "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  272. st->index, st->cur_dts, pkt->dts);
  273. return AVERROR(EINVAL);
  274. }
  275. if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
  276. av_log(s, AV_LOG_ERROR,
  277. "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  278. pkt->pts, pkt->dts,
  279. st->index);
  280. return AVERROR(EINVAL);
  281. }
  282. av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
  283. pkt->pts, pkt->dts);
  284. st->cur_dts = pkt->dts;
  285. return 0;
  286. }
  287. #endif
  288. /*
  289. * FIXME: this function should NEVER get undefined pts/dts beside when the
  290. * AVFMT_NOTIMESTAMPS is set.
  291. * Those additional safety checks should be dropped once the correct checks
  292. * are set in the callers.
  293. */
  294. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  295. {
  296. int ret;
  297. if (s->avoid_negative_ts > 0) {
  298. AVRational time_base = s->streams[pkt->stream_index]->time_base;
  299. int64_t offset = 0;
  300. if (s->internal->offset == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
  301. (pkt->dts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
  302. s->internal->offset = -pkt->dts;
  303. s->internal->offset_timebase = time_base;
  304. }
  305. if (s->internal->offset != AV_NOPTS_VALUE)
  306. offset = av_rescale_q(s->internal->offset, s->internal->offset_timebase, time_base);
  307. if (pkt->dts != AV_NOPTS_VALUE)
  308. pkt->dts += offset;
  309. if (pkt->pts != AV_NOPTS_VALUE)
  310. pkt->pts += offset;
  311. if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
  312. av_log(s, AV_LOG_WARNING,
  313. "Packets poorly interleaved, failed to avoid negative "
  314. "timestamp %"PRId64" in stream %d.\n"
  315. "Try -max_interleave_delta 0 as a possible workaround.\n",
  316. pkt->dts, pkt->stream_index);
  317. }
  318. }
  319. ret = s->oformat->write_packet(s, pkt);
  320. if (s->pb && ret >= 0) {
  321. if (s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  322. avio_flush(s->pb);
  323. if (s->pb->error < 0)
  324. ret = s->pb->error;
  325. }
  326. return ret;
  327. }
  328. static int check_packet(AVFormatContext *s, AVPacket *pkt)
  329. {
  330. if (!pkt)
  331. return 0;
  332. if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
  333. av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
  334. pkt->stream_index);
  335. return AVERROR(EINVAL);
  336. }
  337. if (s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
  338. av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
  339. return AVERROR(EINVAL);
  340. }
  341. return 0;
  342. }
  343. static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt)
  344. {
  345. int ret;
  346. ret = check_packet(s, pkt);
  347. if (ret < 0)
  348. return ret;
  349. #if !FF_API_COMPUTE_PKT_FIELDS2
  350. /* sanitize the timestamps */
  351. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  352. AVStream *st = s->streams[pkt->stream_index];
  353. /* when there is no reordering (so dts is equal to pts), but
  354. * only one of them is set, set the other as well */
  355. if (!st->internal->reorder) {
  356. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
  357. pkt->pts = pkt->dts;
  358. if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
  359. pkt->dts = pkt->pts;
  360. }
  361. /* check that the timestamps are set */
  362. if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
  363. av_log(s, AV_LOG_ERROR,
  364. "Timestamps are unset in a packet for stream %d\n", st->index);
  365. return AVERROR(EINVAL);
  366. }
  367. /* check that the dts are increasing (or at least non-decreasing,
  368. * if the format allows it */
  369. if (st->cur_dts != AV_NOPTS_VALUE &&
  370. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) ||
  371. st->cur_dts > pkt->dts)) {
  372. av_log(s, AV_LOG_ERROR,
  373. "Application provided invalid, non monotonically increasing "
  374. "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  375. st->index, st->cur_dts, pkt->dts);
  376. return AVERROR(EINVAL);
  377. }
  378. if (pkt->pts < pkt->dts) {
  379. av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  380. pkt->pts, pkt->dts, st->index);
  381. return AVERROR(EINVAL);
  382. }
  383. }
  384. #endif
  385. return 0;
  386. }
  387. int av_write_frame(AVFormatContext *s, AVPacket *pkt)
  388. {
  389. int ret;
  390. ret = prepare_input_packet(s, pkt);
  391. if (ret < 0)
  392. return ret;
  393. if (!pkt) {
  394. if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
  395. return s->oformat->write_packet(s, pkt);
  396. return 1;
  397. }
  398. #if FF_API_COMPUTE_PKT_FIELDS2
  399. ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
  400. if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  401. return ret;
  402. #endif
  403. ret = write_packet(s, pkt);
  404. if (ret >= 0)
  405. s->streams[pkt->stream_index]->nb_frames++;
  406. return ret;
  407. }
  408. int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  409. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
  410. {
  411. int ret;
  412. AVPacketList **next_point, *this_pktl;
  413. this_pktl = av_mallocz(sizeof(AVPacketList));
  414. if (!this_pktl)
  415. return AVERROR(ENOMEM);
  416. if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
  417. av_free(this_pktl);
  418. return ret;
  419. }
  420. if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
  421. next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
  422. } else
  423. next_point = &s->internal->packet_buffer;
  424. if (*next_point) {
  425. if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
  426. while (!compare(s, &(*next_point)->pkt, pkt))
  427. next_point = &(*next_point)->next;
  428. goto next_non_null;
  429. } else {
  430. next_point = &(s->internal->packet_buffer_end->next);
  431. }
  432. }
  433. assert(!*next_point);
  434. s->internal->packet_buffer_end = this_pktl;
  435. next_non_null:
  436. this_pktl->next = *next_point;
  437. s->streams[pkt->stream_index]->last_in_packet_buffer =
  438. *next_point = this_pktl;
  439. av_packet_unref(pkt);
  440. return 0;
  441. }
  442. static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
  443. AVPacket *pkt)
  444. {
  445. AVStream *st = s->streams[pkt->stream_index];
  446. AVStream *st2 = s->streams[next->stream_index];
  447. int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
  448. st->time_base);
  449. if (comp == 0)
  450. return pkt->stream_index < next->stream_index;
  451. return comp > 0;
  452. }
  453. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  454. AVPacket *pkt, int flush)
  455. {
  456. AVPacketList *pktl;
  457. int stream_count = 0;
  458. int i, ret;
  459. if (pkt) {
  460. if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
  461. return ret;
  462. }
  463. if (s->max_interleave_delta > 0 && s->internal->packet_buffer && !flush) {
  464. AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
  465. int64_t delta_dts = INT64_MIN;
  466. int64_t top_dts = av_rescale_q(top_pkt->dts,
  467. s->streams[top_pkt->stream_index]->time_base,
  468. AV_TIME_BASE_Q);
  469. for (i = 0; i < s->nb_streams; i++) {
  470. int64_t last_dts;
  471. const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
  472. if (!last)
  473. continue;
  474. last_dts = av_rescale_q(last->pkt.dts,
  475. s->streams[i]->time_base,
  476. AV_TIME_BASE_Q);
  477. delta_dts = FFMAX(delta_dts, last_dts - top_dts);
  478. stream_count++;
  479. }
  480. if (delta_dts > s->max_interleave_delta) {
  481. av_log(s, AV_LOG_DEBUG,
  482. "Delay between the first packet and last packet in the "
  483. "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
  484. delta_dts, s->max_interleave_delta);
  485. flush = 1;
  486. }
  487. } else {
  488. for (i = 0; i < s->nb_streams; i++)
  489. stream_count += !!s->streams[i]->last_in_packet_buffer;
  490. }
  491. if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
  492. pktl = s->internal->packet_buffer;
  493. *out = pktl->pkt;
  494. s->internal->packet_buffer = pktl->next;
  495. if (!s->internal->packet_buffer)
  496. s->internal->packet_buffer_end = NULL;
  497. if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
  498. s->streams[out->stream_index]->last_in_packet_buffer = NULL;
  499. av_freep(&pktl);
  500. return 1;
  501. } else {
  502. av_init_packet(out);
  503. return 0;
  504. }
  505. }
  506. /**
  507. * Interleave an AVPacket correctly so it can be muxed.
  508. * @param out the interleaved packet will be output here
  509. * @param in the input packet
  510. * @param flush 1 if no further packets are available as input and all
  511. * remaining packets should be output
  512. * @return 1 if a packet was output, 0 if no packet could be output,
  513. * < 0 if an error occurred
  514. */
  515. static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
  516. {
  517. if (s->oformat->interleave_packet) {
  518. int ret = s->oformat->interleave_packet(s, out, in, flush);
  519. if (in)
  520. av_packet_unref(in);
  521. return ret;
  522. } else
  523. return ff_interleave_packet_per_dts(s, out, in, flush);
  524. }
  525. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
  526. {
  527. int ret, flush = 0;
  528. ret = prepare_input_packet(s, pkt);
  529. if (ret < 0)
  530. goto fail;
  531. if (pkt) {
  532. AVStream *st = s->streams[pkt->stream_index];
  533. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
  534. pkt->size, pkt->dts, pkt->pts);
  535. #if FF_API_COMPUTE_PKT_FIELDS2
  536. if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  537. goto fail;
  538. #endif
  539. if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  540. ret = AVERROR(EINVAL);
  541. goto fail;
  542. }
  543. } else {
  544. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
  545. flush = 1;
  546. }
  547. for (;; ) {
  548. AVPacket opkt;
  549. int ret = interleave_packet(s, &opkt, pkt, flush);
  550. if (pkt) {
  551. memset(pkt, 0, sizeof(*pkt));
  552. av_init_packet(pkt);
  553. pkt = NULL;
  554. }
  555. if (ret <= 0) //FIXME cleanup needed for ret<0 ?
  556. return ret;
  557. ret = write_packet(s, &opkt);
  558. if (ret >= 0)
  559. s->streams[opkt.stream_index]->nb_frames++;
  560. av_packet_unref(&opkt);
  561. if (ret < 0)
  562. return ret;
  563. }
  564. fail:
  565. av_packet_unref(pkt);
  566. return ret;
  567. }
  568. int av_write_trailer(AVFormatContext *s)
  569. {
  570. int ret, i;
  571. for (;; ) {
  572. AVPacket pkt;
  573. ret = interleave_packet(s, &pkt, NULL, 1);
  574. if (ret < 0) //FIXME cleanup needed for ret<0 ?
  575. goto fail;
  576. if (!ret)
  577. break;
  578. ret = write_packet(s, &pkt);
  579. if (ret >= 0)
  580. s->streams[pkt.stream_index]->nb_frames++;
  581. av_packet_unref(&pkt);
  582. if (ret < 0)
  583. goto fail;
  584. }
  585. if (s->oformat->write_trailer)
  586. ret = s->oformat->write_trailer(s);
  587. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  588. avio_flush(s->pb);
  589. fail:
  590. for (i = 0; i < s->nb_streams; i++) {
  591. av_freep(&s->streams[i]->priv_data);
  592. av_freep(&s->streams[i]->index_entries);
  593. }
  594. if (s->oformat->priv_class)
  595. av_opt_free(s->priv_data);
  596. av_freep(&s->priv_data);
  597. return ret;
  598. }
  599. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  600. AVFormatContext *src)
  601. {
  602. AVPacket local_pkt;
  603. local_pkt = *pkt;
  604. local_pkt.stream_index = dst_stream;
  605. if (pkt->pts != AV_NOPTS_VALUE)
  606. local_pkt.pts = av_rescale_q(pkt->pts,
  607. src->streams[pkt->stream_index]->time_base,
  608. dst->streams[dst_stream]->time_base);
  609. if (pkt->dts != AV_NOPTS_VALUE)
  610. local_pkt.dts = av_rescale_q(pkt->dts,
  611. src->streams[pkt->stream_index]->time_base,
  612. dst->streams[dst_stream]->time_base);
  613. return av_write_frame(dst, &local_pkt);
  614. }