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.

728 lines
22KB

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