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