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.

658 lines
21KB

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