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.

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