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.

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