00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "avformat.h"
00025 #include "avio_internal.h"
00026 #include "internal.h"
00027 #include "libavcodec/internal.h"
00028 #include "libavcodec/bytestream.h"
00029 #include "libavutil/opt.h"
00030 #include "libavutil/dict.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "libavutil/timestamp.h"
00033 #include "metadata.h"
00034 #include "id3v2.h"
00035 #include "libavutil/avassert.h"
00036 #include "libavutil/avstring.h"
00037 #include "libavutil/mathematics.h"
00038 #include "libavutil/parseutils.h"
00039 #include "libavutil/time.h"
00040 #include "riff.h"
00041 #include "audiointerleave.h"
00042 #include "url.h"
00043 #include <stdarg.h>
00044 #if CONFIG_NETWORK
00045 #include "network.h"
00046 #endif
00047
00048 #undef NDEBUG
00049 #include <assert.h>
00050
00056
00057
00068 static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
00069 {
00070 num += (den >> 1);
00071 if (num >= den) {
00072 val += num / den;
00073 num = num % den;
00074 }
00075 f->val = val;
00076 f->num = num;
00077 f->den = den;
00078 }
00079
00086 static void frac_add(AVFrac *f, int64_t incr)
00087 {
00088 int64_t num, den;
00089
00090 num = f->num + incr;
00091 den = f->den;
00092 if (num < 0) {
00093 f->val += num / den;
00094 num = num % den;
00095 if (num < 0) {
00096 num += den;
00097 f->val--;
00098 }
00099 } else if (num >= den) {
00100 f->val += num / den;
00101 num = num % den;
00102 }
00103 f->num = num;
00104 }
00105
00106 AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precission)
00107 {
00108 AVRational q;
00109 int j;
00110
00111 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00112 q = (AVRational){1, st->codec->sample_rate};
00113 } else {
00114 q = st->codec->time_base;
00115 }
00116 for (j=2; j<14; j+= 1+(j>2))
00117 while (q.den / q.num < min_precission && q.num % j == 0)
00118 q.num /= j;
00119 while (q.den / q.num < min_precission && q.den < (1<<24))
00120 q.den <<= 1;
00121
00122 return q;
00123 }
00124
00125 int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
00126 const char *format, const char *filename)
00127 {
00128 AVFormatContext *s = avformat_alloc_context();
00129 int ret = 0;
00130
00131 *avctx = NULL;
00132 if (!s)
00133 goto nomem;
00134
00135 if (!oformat) {
00136 if (format) {
00137 oformat = av_guess_format(format, NULL, NULL);
00138 if (!oformat) {
00139 av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
00140 ret = AVERROR(EINVAL);
00141 goto error;
00142 }
00143 } else {
00144 oformat = av_guess_format(NULL, filename, NULL);
00145 if (!oformat) {
00146 ret = AVERROR(EINVAL);
00147 av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
00148 filename);
00149 goto error;
00150 }
00151 }
00152 }
00153
00154 s->oformat = oformat;
00155 if (s->oformat->priv_data_size > 0) {
00156 s->priv_data = av_mallocz(s->oformat->priv_data_size);
00157 if (!s->priv_data)
00158 goto nomem;
00159 if (s->oformat->priv_class) {
00160 *(const AVClass**)s->priv_data= s->oformat->priv_class;
00161 av_opt_set_defaults(s->priv_data);
00162 }
00163 } else
00164 s->priv_data = NULL;
00165
00166 if (filename)
00167 av_strlcpy(s->filename, filename, sizeof(s->filename));
00168 *avctx = s;
00169 return 0;
00170 nomem:
00171 av_log(s, AV_LOG_ERROR, "Out of memory\n");
00172 ret = AVERROR(ENOMEM);
00173 error:
00174 avformat_free_context(s);
00175 return ret;
00176 }
00177
00178 #if FF_API_ALLOC_OUTPUT_CONTEXT
00179 AVFormatContext *avformat_alloc_output_context(const char *format,
00180 AVOutputFormat *oformat, const char *filename)
00181 {
00182 AVFormatContext *avctx;
00183 int ret = avformat_alloc_output_context2(&avctx, oformat, format, filename);
00184 return ret < 0 ? NULL : avctx;
00185 }
00186 #endif
00187
00188 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
00189 {
00190 const AVCodecTag *avctag;
00191 int n;
00192 enum AVCodecID id = AV_CODEC_ID_NONE;
00193 unsigned int tag = 0;
00194
00201 for (n = 0; s->oformat->codec_tag[n]; n++) {
00202 avctag = s->oformat->codec_tag[n];
00203 while (avctag->id != AV_CODEC_ID_NONE) {
00204 if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
00205 id = avctag->id;
00206 if (id == st->codec->codec_id)
00207 return 1;
00208 }
00209 if (avctag->id == st->codec->codec_id)
00210 tag = avctag->tag;
00211 avctag++;
00212 }
00213 }
00214 if (id != AV_CODEC_ID_NONE)
00215 return 0;
00216 if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
00217 return 0;
00218 return 1;
00219 }
00220
00221
00222 static int init_muxer(AVFormatContext *s, AVDictionary **options)
00223 {
00224 int ret = 0, i;
00225 AVStream *st;
00226 AVDictionary *tmp = NULL;
00227 AVCodecContext *codec = NULL;
00228 AVOutputFormat *of = s->oformat;
00229
00230 if (options)
00231 av_dict_copy(&tmp, *options, 0);
00232
00233 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
00234 goto fail;
00235 if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
00236 (ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
00237 goto fail;
00238
00239
00240 if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
00241 av_log(s, AV_LOG_ERROR, "no streams\n");
00242 ret = AVERROR(EINVAL);
00243 goto fail;
00244 }
00245
00246 for (i = 0; i < s->nb_streams; i++) {
00247 st = s->streams[i];
00248 codec = st->codec;
00249
00250 switch (codec->codec_type) {
00251 case AVMEDIA_TYPE_AUDIO:
00252 if (codec->sample_rate <= 0) {
00253 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
00254 ret = AVERROR(EINVAL);
00255 goto fail;
00256 }
00257 if (!codec->block_align)
00258 codec->block_align = codec->channels *
00259 av_get_bits_per_sample(codec->codec_id) >> 3;
00260 break;
00261 case AVMEDIA_TYPE_VIDEO:
00262 if (codec->time_base.num <= 0 ||
00263 codec->time_base.den <= 0) {
00264 av_log(s, AV_LOG_ERROR, "time base not set\n");
00265 ret = AVERROR(EINVAL);
00266 goto fail;
00267 }
00268
00269 if ((codec->width <= 0 || codec->height <= 0) &&
00270 !(of->flags & AVFMT_NODIMENSIONS)) {
00271 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
00272 ret = AVERROR(EINVAL);
00273 goto fail;
00274 }
00275 if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
00276 && FFABS(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
00277 ) {
00278 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
00279 "(%d/%d) and encoder layer (%d/%d)\n",
00280 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
00281 codec->sample_aspect_ratio.num,
00282 codec->sample_aspect_ratio.den);
00283 ret = AVERROR(EINVAL);
00284 goto fail;
00285 }
00286 break;
00287 }
00288
00289 if (of->codec_tag) {
00290 if ( codec->codec_tag
00291 && codec->codec_id == AV_CODEC_ID_RAWVIDEO
00292 && ( av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
00293 || av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
00294 && !validate_codec_tag(s, st)) {
00295
00296
00297 codec->codec_tag = 0;
00298 }
00299 if (codec->codec_tag) {
00300 if (!validate_codec_tag(s, st)) {
00301 char tagbuf[32], cortag[32];
00302 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
00303 av_get_codec_tag_string(cortag, sizeof(cortag), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
00304 av_log(s, AV_LOG_ERROR,
00305 "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
00306 tagbuf, codec->codec_tag, codec->codec_id, cortag);
00307 ret = AVERROR_INVALIDDATA;
00308 goto fail;
00309 }
00310 } else
00311 codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
00312 }
00313
00314 if (of->flags & AVFMT_GLOBALHEADER &&
00315 !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
00316 av_log(s, AV_LOG_WARNING,
00317 "Codec for stream %d does not use global headers "
00318 "but container format requires global headers\n", i);
00319 }
00320
00321 if (!s->priv_data && of->priv_data_size > 0) {
00322 s->priv_data = av_mallocz(of->priv_data_size);
00323 if (!s->priv_data) {
00324 ret = AVERROR(ENOMEM);
00325 goto fail;
00326 }
00327 if (of->priv_class) {
00328 *(const AVClass **)s->priv_data = of->priv_class;
00329 av_opt_set_defaults(s->priv_data);
00330 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
00331 goto fail;
00332 }
00333 }
00334
00335
00336 if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
00337 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
00338 }
00339
00340 if (options) {
00341 av_dict_free(options);
00342 *options = tmp;
00343 }
00344
00345 return 0;
00346
00347 fail:
00348 av_dict_free(&tmp);
00349 return ret;
00350 }
00351
00352 static int init_pts(AVFormatContext *s)
00353 {
00354 int i;
00355 AVStream *st;
00356
00357
00358 for (i = 0; i < s->nb_streams; i++) {
00359 int64_t den = AV_NOPTS_VALUE;
00360 st = s->streams[i];
00361
00362 switch (st->codec->codec_type) {
00363 case AVMEDIA_TYPE_AUDIO:
00364 den = (int64_t)st->time_base.num * st->codec->sample_rate;
00365 break;
00366 case AVMEDIA_TYPE_VIDEO:
00367 den = (int64_t)st->time_base.num * st->codec->time_base.den;
00368 break;
00369 default:
00370 break;
00371 }
00372 if (den != AV_NOPTS_VALUE) {
00373 if (den <= 0)
00374 return AVERROR_INVALIDDATA;
00375
00376 frac_init(&st->pts, 0, 0, den);
00377 }
00378 }
00379
00380 return 0;
00381 }
00382
00383 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
00384 {
00385 int ret = 0;
00386
00387 if (ret = init_muxer(s, options))
00388 return ret;
00389
00390 if (s->oformat->write_header) {
00391 ret = s->oformat->write_header(s);
00392 if (ret >= 0 && s->pb && s->pb->error < 0)
00393 ret = s->pb->error;
00394 if (ret < 0)
00395 return ret;
00396 }
00397
00398 if ((ret = init_pts(s) < 0))
00399 return ret;
00400
00401 return 0;
00402 }
00403
00404
00405 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
00406 {
00407 int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
00408 int num, den, frame_size, i;
00409
00410 av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
00411 av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
00412
00413
00414 if (pkt->duration == 0) {
00415 ff_compute_frame_duration(&num, &den, st, NULL, pkt);
00416 if (den && num) {
00417 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
00418 }
00419 }
00420
00421 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
00422 pkt->pts = pkt->dts;
00423
00424
00425 if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
00426 static int warned;
00427 if (!warned) {
00428 av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
00429 warned = 1;
00430 }
00431 pkt->dts =
00432
00433 pkt->pts = st->pts.val;
00434 }
00435
00436
00437 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
00438 st->pts_buffer[0] = pkt->pts;
00439 for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
00440 st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
00441 for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
00442 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
00443
00444 pkt->dts = st->pts_buffer[0];
00445 }
00446
00447 if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
00448 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
00449 st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
00450 av_log(s, AV_LOG_ERROR,
00451 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
00452 st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
00453 return AVERROR(EINVAL);
00454 }
00455 if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
00456 av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
00457 av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
00458 return AVERROR(EINVAL);
00459 }
00460
00461 av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
00462 av_ts2str(pkt->pts), av_ts2str(pkt->dts));
00463 st->cur_dts = pkt->dts;
00464 st->pts.val = pkt->dts;
00465
00466
00467 switch (st->codec->codec_type) {
00468 case AVMEDIA_TYPE_AUDIO:
00469 frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
00470
00471
00472
00473
00474 if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
00475 frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
00476 }
00477 break;
00478 case AVMEDIA_TYPE_VIDEO:
00479 frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
00480 break;
00481 default:
00482 break;
00483 }
00484 return 0;
00485 }
00486
00487 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
00488 {
00489 int ret;
00490
00491 if (!pkt) {
00492 if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
00493 ret = s->oformat->write_packet(s, pkt);
00494 if (ret >= 0 && s->pb && s->pb->error < 0)
00495 ret = s->pb->error;
00496 return ret;
00497 }
00498 return 1;
00499 }
00500
00501 ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
00502
00503 if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
00504 return ret;
00505
00506 ret = s->oformat->write_packet(s, pkt);
00507 if (ret >= 0 && s->pb && s->pb->error < 0)
00508 ret = s->pb->error;
00509
00510 if (ret >= 0)
00511 s->streams[pkt->stream_index]->nb_frames++;
00512 return ret;
00513 }
00514
00515 #define CHUNK_START 0x1000
00516
00517 int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
00518 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
00519 {
00520 AVPacketList **next_point, *this_pktl;
00521 AVStream *st = s->streams[pkt->stream_index];
00522 int chunked = s->max_chunk_size || s->max_chunk_duration;
00523
00524 this_pktl = av_mallocz(sizeof(AVPacketList));
00525 if (!this_pktl)
00526 return AVERROR(ENOMEM);
00527 this_pktl->pkt = *pkt;
00528 pkt->destruct = NULL;
00529 av_dup_packet(&this_pktl->pkt);
00530
00531 if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
00532 next_point = &(st->last_in_packet_buffer->next);
00533 } else {
00534 next_point = &s->packet_buffer;
00535 }
00536
00537 if (*next_point) {
00538 if (chunked) {
00539 uint64_t max= av_rescale_q(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base);
00540 if ( st->interleaver_chunk_size + pkt->size <= s->max_chunk_size-1U
00541 && st->interleaver_chunk_duration + pkt->duration <= max-1U) {
00542 st->interleaver_chunk_size += pkt->size;
00543 st->interleaver_chunk_duration += pkt->duration;
00544 goto next_non_null;
00545 } else {
00546 st->interleaver_chunk_size =
00547 st->interleaver_chunk_duration = 0;
00548 this_pktl->pkt.flags |= CHUNK_START;
00549 }
00550 }
00551
00552 if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
00553 while ( *next_point
00554 && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
00555 || !compare(s, &(*next_point)->pkt, pkt)))
00556 next_point = &(*next_point)->next;
00557 if (*next_point)
00558 goto next_non_null;
00559 } else {
00560 next_point = &(s->packet_buffer_end->next);
00561 }
00562 }
00563 av_assert1(!*next_point);
00564
00565 s->packet_buffer_end = this_pktl;
00566 next_non_null:
00567
00568 this_pktl->next = *next_point;
00569
00570 s->streams[pkt->stream_index]->last_in_packet_buffer =
00571 *next_point = this_pktl;
00572 return 0;
00573 }
00574
00575 static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
00576 {
00577 AVStream *st = s->streams[pkt->stream_index];
00578 AVStream *st2 = s->streams[next->stream_index];
00579 int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
00580 st->time_base);
00581 if (s->audio_preload && ((st->codec->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codec->codec_type == AVMEDIA_TYPE_AUDIO))) {
00582 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);
00583 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);
00584 if (ts == ts2) {
00585 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
00586 -( 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;
00587 ts2=0;
00588 }
00589 comp= (ts>ts2) - (ts<ts2);
00590 }
00591
00592 if (comp == 0)
00593 return pkt->stream_index < next->stream_index;
00594 return comp > 0;
00595 }
00596
00597 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
00598 AVPacket *pkt, int flush)
00599 {
00600 AVPacketList *pktl;
00601 int stream_count = 0, noninterleaved_count = 0;
00602 int64_t delta_dts_max = 0;
00603 int i, ret;
00604
00605 if (pkt) {
00606 ret = ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
00607 if (ret < 0)
00608 return ret;
00609 }
00610
00611 for (i = 0; i < s->nb_streams; i++) {
00612 if (s->streams[i]->last_in_packet_buffer) {
00613 ++stream_count;
00614 } else if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
00615 ++noninterleaved_count;
00616 }
00617 }
00618
00619 if (s->nb_streams == stream_count) {
00620 flush = 1;
00621 } else if (!flush) {
00622 for (i=0; i < s->nb_streams; i++) {
00623 if (s->streams[i]->last_in_packet_buffer) {
00624 int64_t delta_dts =
00625 av_rescale_q(s->streams[i]->last_in_packet_buffer->pkt.dts,
00626 s->streams[i]->time_base,
00627 AV_TIME_BASE_Q) -
00628 av_rescale_q(s->packet_buffer->pkt.dts,
00629 s->streams[s->packet_buffer->pkt.stream_index]->time_base,
00630 AV_TIME_BASE_Q);
00631 delta_dts_max= FFMAX(delta_dts_max, delta_dts);
00632 }
00633 }
00634 if (s->nb_streams == stream_count+noninterleaved_count &&
00635 delta_dts_max > 20*AV_TIME_BASE) {
00636 av_log(s, AV_LOG_DEBUG, "flushing with %d noninterleaved\n", noninterleaved_count);
00637 flush = 1;
00638 }
00639 }
00640 if (stream_count && flush) {
00641 AVStream *st;
00642 pktl = s->packet_buffer;
00643 *out = pktl->pkt;
00644 st = s->streams[out->stream_index];
00645
00646 s->packet_buffer = pktl->next;
00647 if (!s->packet_buffer)
00648 s->packet_buffer_end = NULL;
00649
00650 if (st->last_in_packet_buffer == pktl)
00651 st->last_in_packet_buffer = NULL;
00652 av_freep(&pktl);
00653
00654 if (s->avoid_negative_ts > 0) {
00655 if (out->dts != AV_NOPTS_VALUE) {
00656 if (!st->mux_ts_offset && out->dts < 0) {
00657 for (i = 0; i < s->nb_streams; i++) {
00658 s->streams[i]->mux_ts_offset =
00659 av_rescale_q_rnd(-out->dts,
00660 st->time_base,
00661 s->streams[i]->time_base,
00662 AV_ROUND_UP);
00663 }
00664 }
00665 out->dts += st->mux_ts_offset;
00666 }
00667 if (out->pts != AV_NOPTS_VALUE)
00668 out->pts += st->mux_ts_offset;
00669 }
00670
00671 return 1;
00672 } else {
00673 av_init_packet(out);
00674 return 0;
00675 }
00676 }
00677
00678 #if FF_API_INTERLEAVE_PACKET
00679 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
00680 AVPacket *pkt, int flush)
00681 {
00682 return ff_interleave_packet_per_dts(s, out, pkt, flush);
00683 }
00684
00685 #endif
00686
00696 static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
00697 {
00698 if (s->oformat->interleave_packet) {
00699 int ret = s->oformat->interleave_packet(s, out, in, flush);
00700 if (in)
00701 av_free_packet(in);
00702 return ret;
00703 } else
00704 return ff_interleave_packet_per_dts(s, out, in, flush);
00705 }
00706
00707 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
00708 {
00709 int ret, flush = 0;
00710
00711 if (pkt) {
00712 AVStream *st = s->streams[pkt->stream_index];
00713
00714
00715 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
00716 return 0;
00717
00718 av_dlog(s, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
00719 pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
00720 if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
00721 return ret;
00722
00723 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
00724 return AVERROR(EINVAL);
00725 } else {
00726 av_dlog(s, "av_interleaved_write_frame FLUSH\n");
00727 flush = 1;
00728 }
00729
00730 for (;; ) {
00731 AVPacket opkt;
00732 int ret = interleave_packet(s, &opkt, pkt, flush);
00733 if (ret <= 0)
00734 return ret;
00735
00736 ret = s->oformat->write_packet(s, &opkt);
00737 if (ret >= 0)
00738 s->streams[opkt.stream_index]->nb_frames++;
00739
00740 av_free_packet(&opkt);
00741 pkt = NULL;
00742
00743 if (ret < 0)
00744 return ret;
00745 if(s->pb && s->pb->error)
00746 return s->pb->error;
00747 }
00748 }
00749
00750 int av_write_trailer(AVFormatContext *s)
00751 {
00752 int ret, i;
00753
00754 for (;; ) {
00755 AVPacket pkt;
00756 ret = interleave_packet(s, &pkt, NULL, 1);
00757 if (ret < 0)
00758 goto fail;
00759 if (!ret)
00760 break;
00761
00762 ret = s->oformat->write_packet(s, &pkt);
00763 if (ret >= 0)
00764 s->streams[pkt.stream_index]->nb_frames++;
00765
00766 av_free_packet(&pkt);
00767
00768 if (ret < 0)
00769 goto fail;
00770 if(s->pb && s->pb->error)
00771 goto fail;
00772 }
00773
00774 if (s->oformat->write_trailer)
00775 ret = s->oformat->write_trailer(s);
00776
00777 fail:
00778 if (s->pb)
00779 avio_flush(s->pb);
00780 if (ret == 0)
00781 ret = s->pb ? s->pb->error : 0;
00782 for (i = 0; i < s->nb_streams; i++) {
00783 av_freep(&s->streams[i]->priv_data);
00784 av_freep(&s->streams[i]->index_entries);
00785 }
00786 if (s->oformat->priv_class)
00787 av_opt_free(s->priv_data);
00788 av_freep(&s->priv_data);
00789 return ret;
00790 }
00791
00792 int av_get_output_timestamp(struct AVFormatContext *s, int stream,
00793 int64_t *dts, int64_t *wall)
00794 {
00795 if (!s->oformat || !s->oformat->get_output_timestamp)
00796 return AVERROR(ENOSYS);
00797 s->oformat->get_output_timestamp(s, stream, dts, wall);
00798 return 0;
00799 }