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