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.

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