2 * muxing functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "avio_internal.h"
25 #include "libavcodec/internal.h"
26 #include "libavcodec/bytestream.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/timestamp.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/time.h"
40 #include "audiointerleave.h"
52 * muxing functions for use within libavformat
55 /* fraction handling */
58 * f = val + (num / den) + 0.5.
60 * 'num' is normalized so that it is such as 0 <= num < den.
62 * @param f fractional number
63 * @param val integer value
64 * @param num must be >= 0
65 * @param den must be >= 1
67 static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
80 * Fractional addition to f: f = f + (incr / f->den).
82 * @param f fractional number
83 * @param incr increment, can be positive or negative
85 static void frac_add(AVFrac *f, int64_t incr)
98 } else if (num >= den) {
105 AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precission)
110 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
111 q = (AVRational){1, st->codec->sample_rate};
113 q = st->codec->time_base;
115 for (j=2; j<14; j+= 1+(j>2))
116 while (q.den / q.num < min_precission && q.num % j == 0)
118 while (q.den / q.num < min_precission && q.den < (1<<24))
124 int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
125 const char *format, const char *filename)
127 AVFormatContext *s = avformat_alloc_context();
136 oformat = av_guess_format(format, NULL, NULL);
138 av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
139 ret = AVERROR(EINVAL);
143 oformat = av_guess_format(NULL, filename, NULL);
145 ret = AVERROR(EINVAL);
146 av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
153 s->oformat = oformat;
154 if (s->oformat->priv_data_size > 0) {
155 s->priv_data = av_mallocz(s->oformat->priv_data_size);
158 if (s->oformat->priv_class) {
159 *(const AVClass**)s->priv_data= s->oformat->priv_class;
160 av_opt_set_defaults(s->priv_data);
166 av_strlcpy(s->filename, filename, sizeof(s->filename));
170 av_log(s, AV_LOG_ERROR, "Out of memory\n");
171 ret = AVERROR(ENOMEM);
173 avformat_free_context(s);
177 #if FF_API_ALLOC_OUTPUT_CONTEXT
178 AVFormatContext *avformat_alloc_output_context(const char *format,
179 AVOutputFormat *oformat, const char *filename)
181 AVFormatContext *avctx;
182 int ret = avformat_alloc_output_context2(&avctx, oformat, format, filename);
183 return ret < 0 ? NULL : avctx;
187 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
189 const AVCodecTag *avctag;
191 enum AVCodecID id = AV_CODEC_ID_NONE;
195 * Check that tag + id is in the table
196 * If neither is in the table -> OK
197 * If tag is in the table with another id -> FAIL
198 * If id is in the table with another tag -> FAIL unless strict < normal
200 for (n = 0; s->oformat->codec_tag[n]; n++) {
201 avctag = s->oformat->codec_tag[n];
202 while (avctag->id != AV_CODEC_ID_NONE) {
203 if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
205 if (id == st->codec->codec_id)
208 if (avctag->id == st->codec->codec_id)
213 if (id != AV_CODEC_ID_NONE)
215 if (tag >= 0 && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
221 static int init_muxer(AVFormatContext *s, AVDictionary **options)
225 AVDictionary *tmp = NULL;
226 AVCodecContext *codec = NULL;
227 AVOutputFormat *of = s->oformat;
230 av_dict_copy(&tmp, *options, 0);
232 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
234 if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
235 (ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
238 // some sanity checks
239 if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
240 av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
241 ret = AVERROR(EINVAL);
245 for (i = 0; i < s->nb_streams; i++) {
249 switch (codec->codec_type) {
250 case AVMEDIA_TYPE_AUDIO:
251 if (codec->sample_rate <= 0) {
252 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
253 ret = AVERROR(EINVAL);
256 if (!codec->block_align)
257 codec->block_align = codec->channels *
258 av_get_bits_per_sample(codec->codec_id) >> 3;
260 case AVMEDIA_TYPE_VIDEO:
261 if (codec->time_base.num <= 0 ||
262 codec->time_base.den <= 0) { //FIXME audio too?
263 av_log(s, AV_LOG_ERROR, "time base not set\n");
264 ret = AVERROR(EINVAL);
268 if ((codec->width <= 0 || codec->height <= 0) &&
269 !(of->flags & AVFMT_NODIMENSIONS)) {
270 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
271 ret = AVERROR(EINVAL);
274 if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
275 && FFABS(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
277 if (st->sample_aspect_ratio.num != 0 &&
278 st->sample_aspect_ratio.den != 0 &&
279 codec->sample_aspect_ratio.den != 0 &&
280 codec->sample_aspect_ratio.den != 0) {
281 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
282 "(%d/%d) and encoder layer (%d/%d)\n",
283 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
284 codec->sample_aspect_ratio.num,
285 codec->sample_aspect_ratio.den);
286 ret = AVERROR(EINVAL);
294 if ( codec->codec_tag
295 && codec->codec_id == AV_CODEC_ID_RAWVIDEO
296 && ( av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
297 || av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
298 && !validate_codec_tag(s, st)) {
299 // the current rawvideo encoding system ends up setting
300 // the wrong codec_tag for avi/mov, we override it here
301 codec->codec_tag = 0;
303 if (codec->codec_tag) {
304 if (!validate_codec_tag(s, st)) {
305 char tagbuf[32], tagbuf2[32];
306 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
307 av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
308 av_log(s, AV_LOG_ERROR,
309 "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
310 tagbuf, codec->codec_tag, codec->codec_id, tagbuf2);
311 ret = AVERROR_INVALIDDATA;
315 codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
318 if (of->flags & AVFMT_GLOBALHEADER &&
319 !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
320 av_log(s, AV_LOG_WARNING,
321 "Codec for stream %d does not use global headers "
322 "but container format requires global headers\n", i);
324 if (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
325 s->internal->nb_interleaved_streams++;
328 if (!s->priv_data && of->priv_data_size > 0) {
329 s->priv_data = av_mallocz(of->priv_data_size);
331 ret = AVERROR(ENOMEM);
334 if (of->priv_class) {
335 *(const AVClass **)s->priv_data = of->priv_class;
336 av_opt_set_defaults(s->priv_data);
337 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
342 /* set muxer identification string */
343 if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
344 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
346 av_dict_set(&s->metadata, "encoder", NULL, 0);
350 av_dict_free(options);
361 static int init_pts(AVFormatContext *s)
366 /* init PTS generation */
367 for (i = 0; i < s->nb_streams; i++) {
368 int64_t den = AV_NOPTS_VALUE;
371 switch (st->codec->codec_type) {
372 case AVMEDIA_TYPE_AUDIO:
373 den = (int64_t)st->time_base.num * st->codec->sample_rate;
375 case AVMEDIA_TYPE_VIDEO:
376 den = (int64_t)st->time_base.num * st->codec->time_base.den;
381 if (den != AV_NOPTS_VALUE) {
383 return AVERROR_INVALIDDATA;
385 frac_init(&st->pts, 0, 0, den);
392 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
396 if (ret = init_muxer(s, options))
399 if (s->oformat->write_header) {
400 ret = s->oformat->write_header(s);
401 if (ret >= 0 && s->pb && s->pb->error < 0)
407 if ((ret = init_pts(s)) < 0)
410 if (s->avoid_negative_ts < 0) {
411 if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
412 s->avoid_negative_ts = 0;
414 s->avoid_negative_ts = 1;
420 //FIXME merge with compute_pkt_fields
421 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
423 int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
424 int num, den, frame_size, i;
426 av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
427 av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
430 if (pkt->duration == 0) {
431 ff_compute_frame_duration(&num, &den, st, NULL, pkt);
433 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
437 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
440 //XXX/FIXME this is a temporary hack until all encoders output pts
441 if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
444 av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
448 // pkt->pts= st->cur_dts;
449 pkt->pts = st->pts.val;
452 //calculate dts from pts
453 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
454 st->pts_buffer[0] = pkt->pts;
455 for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
456 st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
457 for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
458 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
460 pkt->dts = st->pts_buffer[0];
463 if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
464 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
465 st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
466 av_log(s, AV_LOG_ERROR,
467 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
468 st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
469 return AVERROR(EINVAL);
471 if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
472 av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
473 av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
474 return AVERROR(EINVAL);
477 av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
478 av_ts2str(pkt->pts), av_ts2str(pkt->dts));
479 st->cur_dts = pkt->dts;
480 st->pts.val = pkt->dts;
483 switch (st->codec->codec_type) {
484 case AVMEDIA_TYPE_AUDIO:
485 frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
487 /* HACK/FIXME, we skip the initial 0 size packets as they are most
488 * likely equal to the encoder delay, but it would be better if we
489 * had the real timestamps from the encoder */
490 if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
491 frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
494 case AVMEDIA_TYPE_VIDEO:
495 frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
504 * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
507 * FIXME: this function should NEVER get undefined pts/dts beside when the
508 * AVFMT_NOTIMESTAMPS is set.
509 * Those additional safety checks should be dropped once the correct checks
510 * are set in the callers.
512 static int write_packet(AVFormatContext *s, AVPacket *pkt)
516 if (s->output_ts_offset) {
517 AVStream *st = s->streams[pkt->stream_index];
518 int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
520 if (pkt->dts != AV_NOPTS_VALUE)
522 if (pkt->pts != AV_NOPTS_VALUE)
526 if (s->avoid_negative_ts > 0) {
527 AVStream *st = s->streams[pkt->stream_index];
528 int64_t offset = st->mux_ts_offset;
530 if (pkt->dts < 0 && pkt->dts != AV_NOPTS_VALUE && !s->offset) {
531 s->offset = -pkt->dts;
532 s->offset_timebase = st->time_base;
535 if (s->offset && !offset) {
536 offset = st->mux_ts_offset =
537 av_rescale_q_rnd(s->offset,
543 if (pkt->dts != AV_NOPTS_VALUE)
545 if (pkt->pts != AV_NOPTS_VALUE)
548 av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0);
551 did_split = av_packet_split_side_data(pkt);
552 ret = s->oformat->write_packet(s, pkt);
554 if (s->flush_packets && s->pb && ret >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
558 av_packet_merge_side_data(pkt);
563 static int check_packet(AVFormatContext *s, AVPacket *pkt)
568 if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
569 av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
571 return AVERROR(EINVAL);
574 if (s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
575 av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
576 return AVERROR(EINVAL);
582 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
586 ret = check_packet(s, pkt);
591 if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
592 ret = s->oformat->write_packet(s, NULL);
593 if (s->flush_packets && s->pb && s->pb->error >= 0)
595 if (ret >= 0 && s->pb && s->pb->error < 0)
602 ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
604 if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
607 ret = write_packet(s, pkt);
608 if (ret >= 0 && s->pb && s->pb->error < 0)
612 s->streams[pkt->stream_index]->nb_frames++;
616 #define CHUNK_START 0x1000
618 int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
619 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
621 AVPacketList **next_point, *this_pktl;
622 AVStream *st = s->streams[pkt->stream_index];
623 int chunked = s->max_chunk_size || s->max_chunk_duration;
625 this_pktl = av_mallocz(sizeof(AVPacketList));
627 return AVERROR(ENOMEM);
628 this_pktl->pkt = *pkt;
629 #if FF_API_DESTRUCT_PACKET
630 FF_DISABLE_DEPRECATION_WARNINGS
631 pkt->destruct = NULL; // do not free original but only the copy
632 FF_ENABLE_DEPRECATION_WARNINGS
635 av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-allocated memory
636 av_copy_packet_side_data(&this_pktl->pkt, &this_pktl->pkt); // copy side data
638 if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
639 next_point = &(st->last_in_packet_buffer->next);
641 next_point = &s->packet_buffer;
645 uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
646 st->interleaver_chunk_size += pkt->size;
647 st->interleaver_chunk_duration += pkt->duration;
648 if ( (s->max_chunk_size && st->interleaver_chunk_size > s->max_chunk_size)
649 || (max && st->interleaver_chunk_duration > max)) {
650 st->interleaver_chunk_size = 0;
651 this_pktl->pkt.flags |= CHUNK_START;
652 if (max && st->interleaver_chunk_duration > max) {
653 int64_t syncoffset = (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
654 int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
656 st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
658 st->interleaver_chunk_duration = 0;
662 if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
665 if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
667 && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
668 || !compare(s, &(*next_point)->pkt, pkt)))
669 next_point = &(*next_point)->next;
673 next_point = &(s->packet_buffer_end->next);
676 av_assert1(!*next_point);
678 s->packet_buffer_end = this_pktl;
681 this_pktl->next = *next_point;
683 s->streams[pkt->stream_index]->last_in_packet_buffer =
684 *next_point = this_pktl;
688 static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
691 AVStream *st = s->streams[pkt->stream_index];
692 AVStream *st2 = s->streams[next->stream_index];
693 int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
695 if (s->audio_preload && ((st->codec->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codec->codec_type == AVMEDIA_TYPE_AUDIO))) {
696 int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO);
697 int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO);
699 ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den
700 -( next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
703 comp= (ts>ts2) - (ts<ts2);
707 return pkt->stream_index < next->stream_index;
711 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
712 AVPacket *pkt, int flush)
715 int stream_count = 0, noninterleaved_count = 0;
719 ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts);
724 for (i = 0; i < s->nb_streams; i++) {
725 if (s->streams[i]->last_in_packet_buffer) {
727 } else if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
728 ++noninterleaved_count;
732 if (s->internal->nb_interleaved_streams == stream_count)
735 if (s->max_interleave_delta > 0 && s->packet_buffer && !flush) {
736 AVPacket *top_pkt = &s->packet_buffer->pkt;
737 int64_t delta_dts = INT64_MIN;
738 int64_t top_dts = av_rescale_q(top_pkt->dts,
739 s->streams[top_pkt->stream_index]->time_base,
742 for (i = 0; i < s->nb_streams; i++) {
744 const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
749 last_dts = av_rescale_q(last->pkt.dts,
750 s->streams[i]->time_base,
752 delta_dts = FFMAX(delta_dts, last_dts - top_dts);
755 if (delta_dts > s->max_interleave_delta) {
756 av_log(s, AV_LOG_DEBUG,
757 "Delay between the first packet and last packet in the "
758 "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
759 delta_dts, s->max_interleave_delta);
764 if (stream_count && flush) {
766 pktl = s->packet_buffer;
768 st = s->streams[out->stream_index];
770 s->packet_buffer = pktl->next;
771 if (!s->packet_buffer)
772 s->packet_buffer_end = NULL;
774 if (st->last_in_packet_buffer == pktl)
775 st->last_in_packet_buffer = NULL;
786 * Interleave an AVPacket correctly so it can be muxed.
787 * @param out the interleaved packet will be output here
788 * @param in the input packet
789 * @param flush 1 if no further packets are available as input and all
790 * remaining packets should be output
791 * @return 1 if a packet was output, 0 if no packet could be output,
792 * < 0 if an error occurred
794 static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
796 if (s->oformat->interleave_packet) {
797 int ret = s->oformat->interleave_packet(s, out, in, flush);
802 return ff_interleave_packet_per_dts(s, out, in, flush);
805 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
809 ret = check_packet(s, pkt);
814 AVStream *st = s->streams[pkt->stream_index];
816 //FIXME/XXX/HACK drop zero sized packets
817 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
820 av_dlog(s, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
821 pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
822 if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
825 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
826 return AVERROR(EINVAL);
828 av_dlog(s, "av_interleaved_write_frame FLUSH\n");
834 int ret = interleave_packet(s, &opkt, pkt, flush);
835 if (ret <= 0) //FIXME cleanup needed for ret<0 ?
838 ret = write_packet(s, &opkt);
840 s->streams[opkt.stream_index]->nb_frames++;
842 av_free_packet(&opkt);
847 if(s->pb && s->pb->error)
852 int av_write_trailer(AVFormatContext *s)
858 ret = interleave_packet(s, &pkt, NULL, 1);
859 if (ret < 0) //FIXME cleanup needed for ret<0 ?
864 ret = write_packet(s, &pkt);
866 s->streams[pkt.stream_index]->nb_frames++;
868 av_free_packet(&pkt);
872 if(s->pb && s->pb->error)
876 if (s->oformat->write_trailer)
877 ret = s->oformat->write_trailer(s);
883 ret = s->pb ? s->pb->error : 0;
884 for (i = 0; i < s->nb_streams; i++) {
885 av_freep(&s->streams[i]->priv_data);
886 av_freep(&s->streams[i]->index_entries);
888 if (s->oformat->priv_class)
889 av_opt_free(s->priv_data);
890 av_freep(&s->priv_data);
894 int av_get_output_timestamp(struct AVFormatContext *s, int stream,
895 int64_t *dts, int64_t *wall)
897 if (!s->oformat || !s->oformat->get_output_timestamp)
898 return AVERROR(ENOSYS);
899 s->oformat->get_output_timestamp(s, stream, dts, wall);
903 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
904 AVFormatContext *src)
909 local_pkt.stream_index = dst_stream;
910 if (pkt->pts != AV_NOPTS_VALUE)
911 local_pkt.pts = av_rescale_q(pkt->pts,
912 src->streams[pkt->stream_index]->time_base,
913 dst->streams[dst_stream]->time_base);
914 if (pkt->dts != AV_NOPTS_VALUE)
915 local_pkt.dts = av_rescale_q(pkt->dts,
916 src->streams[pkt->stream_index]->time_base,
917 dst->streams[dst_stream]->time_base);
919 local_pkt.duration = av_rescale_q(pkt->duration,
920 src->streams[pkt->stream_index]->time_base,
921 dst->streams[dst_stream]->time_base);
922 return av_write_frame(dst, &local_pkt);