2012-09-30 22:49:16 +00:00
|
|
|
/*
|
2012-10-02 16:15:13 +00:00
|
|
|
* muxing functions for use within FFmpeg
|
2012-09-30 22:49:16 +00:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
|
|
|
*
|
2012-10-02 16:15:13 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-09-30 22:49:16 +00:00
|
|
|
*
|
2012-10-02 16:15:13 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-09-30 22:49:16 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-10-02 16:15:13 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-09-30 22:49:16 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-10-02 16:15:13 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-09-30 22:49:16 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "avio_internal.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "libavcodec/internal.h"
|
|
|
|
#include "libavcodec/bytestream.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/dict.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
2012-10-02 16:15:13 +00:00
|
|
|
#include "libavutil/timestamp.h"
|
2012-09-30 22:49:16 +00:00
|
|
|
#include "metadata.h"
|
|
|
|
#include "id3v2.h"
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2013-03-27 17:36:51 +00:00
|
|
|
#include "libavutil/internal.h"
|
2012-09-30 22:49:16 +00:00
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "libavutil/time.h"
|
|
|
|
#include "riff.h"
|
|
|
|
#include "audiointerleave.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#if CONFIG_NETWORK
|
|
|
|
#include "network.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2012-10-02 16:15:13 +00:00
|
|
|
* muxing functions for use within libavformat
|
2012-09-30 22:49:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* fraction handling */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* f = val + (num / den) + 0.5.
|
|
|
|
*
|
|
|
|
* 'num' is normalized so that it is such as 0 <= num < den.
|
|
|
|
*
|
|
|
|
* @param f fractional number
|
|
|
|
* @param val integer value
|
|
|
|
* @param num must be >= 0
|
|
|
|
* @param den must be >= 1
|
|
|
|
*/
|
|
|
|
static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
|
|
|
|
{
|
|
|
|
num += (den >> 1);
|
|
|
|
if (num >= den) {
|
|
|
|
val += num / den;
|
|
|
|
num = num % den;
|
|
|
|
}
|
|
|
|
f->val = val;
|
|
|
|
f->num = num;
|
|
|
|
f->den = den;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fractional addition to f: f = f + (incr / f->den).
|
|
|
|
*
|
|
|
|
* @param f fractional number
|
|
|
|
* @param incr increment, can be positive or negative
|
|
|
|
*/
|
|
|
|
static void frac_add(AVFrac *f, int64_t incr)
|
|
|
|
{
|
|
|
|
int64_t num, den;
|
|
|
|
|
|
|
|
num = f->num + incr;
|
|
|
|
den = f->den;
|
|
|
|
if (num < 0) {
|
|
|
|
f->val += num / den;
|
|
|
|
num = num % den;
|
|
|
|
if (num < 0) {
|
|
|
|
num += den;
|
|
|
|
f->val--;
|
|
|
|
}
|
|
|
|
} else if (num >= den) {
|
|
|
|
f->val += num / den;
|
|
|
|
num = num % den;
|
|
|
|
}
|
|
|
|
f->num = num;
|
|
|
|
}
|
|
|
|
|
2012-10-03 19:34:43 +00:00
|
|
|
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precission)
|
|
|
|
{
|
|
|
|
AVRational q;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
q = (AVRational){1, st->codec->sample_rate};
|
|
|
|
} else {
|
|
|
|
q = st->codec->time_base;
|
|
|
|
}
|
2012-10-03 19:38:56 +00:00
|
|
|
for (j=2; j<14; j+= 1+(j>2))
|
2012-10-03 19:34:43 +00:00
|
|
|
while (q.den / q.num < min_precission && q.num % j == 0)
|
|
|
|
q.num /= j;
|
|
|
|
while (q.den / q.num < min_precission && q.den < (1<<24))
|
|
|
|
q.den <<= 1;
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
|
|
|
|
const char *format, const char *filename)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = avformat_alloc_context();
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
*avctx = NULL;
|
|
|
|
if (!s)
|
|
|
|
goto nomem;
|
|
|
|
|
|
|
|
if (!oformat) {
|
|
|
|
if (format) {
|
|
|
|
oformat = av_guess_format(format, NULL, NULL);
|
|
|
|
if (!oformat) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
oformat = av_guess_format(NULL, filename, NULL);
|
|
|
|
if (!oformat) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
|
|
|
|
filename);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->oformat = oformat;
|
|
|
|
if (s->oformat->priv_data_size > 0) {
|
|
|
|
s->priv_data = av_mallocz(s->oformat->priv_data_size);
|
|
|
|
if (!s->priv_data)
|
|
|
|
goto nomem;
|
|
|
|
if (s->oformat->priv_class) {
|
|
|
|
*(const AVClass**)s->priv_data= s->oformat->priv_class;
|
|
|
|
av_opt_set_defaults(s->priv_data);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
s->priv_data = NULL;
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
av_strlcpy(s->filename, filename, sizeof(s->filename));
|
|
|
|
*avctx = s;
|
|
|
|
return 0;
|
|
|
|
nomem:
|
|
|
|
av_log(s, AV_LOG_ERROR, "Out of memory\n");
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
error:
|
|
|
|
avformat_free_context(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FF_API_ALLOC_OUTPUT_CONTEXT
|
|
|
|
AVFormatContext *avformat_alloc_output_context(const char *format,
|
|
|
|
AVOutputFormat *oformat, const char *filename)
|
|
|
|
{
|
|
|
|
AVFormatContext *avctx;
|
|
|
|
int ret = avformat_alloc_output_context2(&avctx, oformat, format, filename);
|
|
|
|
return ret < 0 ? NULL : avctx;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
|
|
|
|
{
|
|
|
|
const AVCodecTag *avctag;
|
|
|
|
int n;
|
|
|
|
enum AVCodecID id = AV_CODEC_ID_NONE;
|
2014-01-01 21:29:06 +00:00
|
|
|
int64_t tag = -1;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that tag + id is in the table
|
|
|
|
* If neither is in the table -> OK
|
|
|
|
* If tag is in the table with another id -> FAIL
|
|
|
|
* If id is in the table with another tag -> FAIL unless strict < normal
|
|
|
|
*/
|
|
|
|
for (n = 0; s->oformat->codec_tag[n]; n++) {
|
|
|
|
avctag = s->oformat->codec_tag[n];
|
|
|
|
while (avctag->id != AV_CODEC_ID_NONE) {
|
|
|
|
if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
|
|
|
|
id = avctag->id;
|
|
|
|
if (id == st->codec->codec_id)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (avctag->id == st->codec->codec_id)
|
|
|
|
tag = avctag->tag;
|
|
|
|
avctag++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (id != AV_CODEC_ID_NONE)
|
|
|
|
return 0;
|
2014-01-01 21:29:06 +00:00
|
|
|
if (tag >= 0 && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
|
2012-09-30 22:49:16 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:18 +00:00
|
|
|
|
|
|
|
static int init_muxer(AVFormatContext *s, AVDictionary **options)
|
2012-09-30 22:49:16 +00:00
|
|
|
{
|
|
|
|
int ret = 0, i;
|
|
|
|
AVStream *st;
|
|
|
|
AVDictionary *tmp = NULL;
|
2012-09-30 22:49:17 +00:00
|
|
|
AVCodecContext *codec = NULL;
|
|
|
|
AVOutputFormat *of = s->oformat;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
if (options)
|
|
|
|
av_dict_copy(&tmp, *options, 0);
|
2012-09-30 22:49:17 +00:00
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
if ((ret = av_opt_set_dict(s, &tmp)) < 0)
|
|
|
|
goto fail;
|
2012-10-02 16:15:13 +00:00
|
|
|
if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
|
|
|
|
(ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
|
|
|
|
goto fail;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
// some sanity checks
|
2012-09-30 22:49:17 +00:00
|
|
|
if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
|
2013-12-15 17:12:10 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
|
2012-09-30 22:49:16 +00:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2012-09-30 22:49:17 +00:00
|
|
|
st = s->streams[i];
|
|
|
|
codec = st->codec;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
2012-09-30 22:49:17 +00:00
|
|
|
switch (codec->codec_type) {
|
2012-09-30 22:49:16 +00:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2012-09-30 22:49:17 +00:00
|
|
|
if (codec->sample_rate <= 0) {
|
2012-09-30 22:49:16 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "sample rate not set\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-30 22:49:17 +00:00
|
|
|
if (!codec->block_align)
|
|
|
|
codec->block_align = codec->channels *
|
|
|
|
av_get_bits_per_sample(codec->codec_id) >> 3;
|
2012-09-30 22:49:16 +00:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2012-09-30 22:49:17 +00:00
|
|
|
if (codec->time_base.num <= 0 ||
|
|
|
|
codec->time_base.den <= 0) { //FIXME audio too?
|
2012-09-30 22:49:16 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "time base not set\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-30 22:49:17 +00:00
|
|
|
|
|
|
|
if ((codec->width <= 0 || codec->height <= 0) &&
|
|
|
|
!(of->flags & AVFMT_NODIMENSIONS)) {
|
2012-09-30 22:49:16 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-02 16:31:47 +00:00
|
|
|
if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
|
|
|
|
&& FFABS(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
|
2012-10-02 16:15:13 +00:00
|
|
|
) {
|
2013-06-23 21:00:34 +00:00
|
|
|
if (st->sample_aspect_ratio.num != 0 &&
|
|
|
|
st->sample_aspect_ratio.den != 0 &&
|
|
|
|
codec->sample_aspect_ratio.den != 0 &&
|
|
|
|
codec->sample_aspect_ratio.den != 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
|
2013-06-24 09:42:42 +00:00
|
|
|
"(%d/%d) and encoder layer (%d/%d)\n",
|
|
|
|
st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
|
|
|
|
codec->sample_aspect_ratio.num,
|
|
|
|
codec->sample_aspect_ratio.den);
|
2013-06-23 21:00:34 +00:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:17 +00:00
|
|
|
if (of->codec_tag) {
|
2012-10-02 16:31:47 +00:00
|
|
|
if ( codec->codec_tag
|
|
|
|
&& codec->codec_id == AV_CODEC_ID_RAWVIDEO
|
|
|
|
&& ( av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
|
|
|
|
|| av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
|
|
|
|
&& !validate_codec_tag(s, st)) {
|
2012-09-30 22:49:17 +00:00
|
|
|
// the current rawvideo encoding system ends up setting
|
2012-10-02 16:31:47 +00:00
|
|
|
// the wrong codec_tag for avi/mov, we override it here
|
2012-09-30 22:49:17 +00:00
|
|
|
codec->codec_tag = 0;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
2012-09-30 22:49:17 +00:00
|
|
|
if (codec->codec_tag) {
|
2012-09-30 22:49:16 +00:00
|
|
|
if (!validate_codec_tag(s, st)) {
|
2013-06-26 15:13:06 +00:00
|
|
|
char tagbuf[32], tagbuf2[32];
|
2012-09-30 22:49:17 +00:00
|
|
|
av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
|
2013-06-26 15:13:06 +00:00
|
|
|
av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
|
2012-09-30 22:49:16 +00:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-10-02 16:15:13 +00:00
|
|
|
"Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
|
2013-06-26 15:13:06 +00:00
|
|
|
tagbuf, codec->codec_tag, codec->codec_id, tagbuf2);
|
2012-09-30 22:49:16 +00:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else
|
2012-09-30 22:49:17 +00:00
|
|
|
codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:17 +00:00
|
|
|
if (of->flags & AVFMT_GLOBALHEADER &&
|
|
|
|
!(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
|
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"Codec for stream %d does not use global headers "
|
|
|
|
"but container format requires global headers\n", i);
|
2014-01-20 12:59:06 +00:00
|
|
|
|
|
|
|
if (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
|
|
|
|
s->internal->nb_interleaved_streams++;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:17 +00:00
|
|
|
if (!s->priv_data && of->priv_data_size > 0) {
|
|
|
|
s->priv_data = av_mallocz(of->priv_data_size);
|
2012-09-30 22:49:16 +00:00
|
|
|
if (!s->priv_data) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-30 22:49:17 +00:00
|
|
|
if (of->priv_class) {
|
|
|
|
*(const AVClass **)s->priv_data = of->priv_class;
|
2012-09-30 22:49:16 +00:00
|
|
|
av_opt_set_defaults(s->priv_data);
|
|
|
|
if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set muxer identification string */
|
|
|
|
if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
|
|
|
|
av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
|
2013-12-06 03:35:35 +00:00
|
|
|
} else {
|
|
|
|
av_dict_set(&s->metadata, "encoder", NULL, 0);
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:18 +00:00
|
|
|
if (options) {
|
|
|
|
av_dict_free(options);
|
|
|
|
*options = tmp;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:18 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_dict_free(&tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_pts(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
AVStream *st;
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
/* init PTS generation */
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
int64_t den = AV_NOPTS_VALUE;
|
|
|
|
st = s->streams[i];
|
|
|
|
|
|
|
|
switch (st->codec->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
den = (int64_t)st->time_base.num * st->codec->sample_rate;
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
den = (int64_t)st->time_base.num * st->codec->time_base.den;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (den != AV_NOPTS_VALUE) {
|
2012-09-30 22:49:18 +00:00
|
|
|
if (den <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
frac_init(&st->pts, 0, 0, den);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (ret = init_muxer(s, options))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (s->oformat->write_header) {
|
|
|
|
ret = s->oformat->write_header(s);
|
2012-10-02 16:48:56 +00:00
|
|
|
if (ret >= 0 && s->pb && s->pb->error < 0)
|
|
|
|
ret = s->pb->error;
|
2012-09-30 22:49:18 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
2012-09-30 22:49:18 +00:00
|
|
|
|
2013-01-13 23:07:26 +00:00
|
|
|
if ((ret = init_pts(s)) < 0)
|
2012-09-30 22:49:18 +00:00
|
|
|
return ret;
|
|
|
|
|
2013-04-26 09:52:51 +00:00
|
|
|
if (s->avoid_negative_ts < 0) {
|
|
|
|
if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
|
|
|
|
s->avoid_negative_ts = 0;
|
|
|
|
} else
|
|
|
|
s->avoid_negative_ts = 1;
|
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-31 13:09:48 +00:00
|
|
|
#define AV_PKT_FLAG_UNCODED_FRAME 0x2000
|
|
|
|
|
|
|
|
/* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but
|
|
|
|
it is only being used internally to this file as a consistency check.
|
|
|
|
The value is chosen to be very unlikely to appear on its own and to cause
|
|
|
|
immediate failure if used anywhere as a real size. */
|
|
|
|
#define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame))
|
|
|
|
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
//FIXME merge with compute_pkt_fields
|
|
|
|
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
|
|
|
|
{
|
2012-10-02 16:15:13 +00:00
|
|
|
int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
|
2012-09-30 22:49:16 +00:00
|
|
|
int num, den, frame_size, i;
|
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
|
|
|
|
av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
/* duration field */
|
|
|
|
if (pkt->duration == 0) {
|
|
|
|
ff_compute_frame_duration(&num, &den, st, NULL, pkt);
|
|
|
|
if (den && num) {
|
|
|
|
pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
|
|
|
|
pkt->pts = pkt->dts;
|
|
|
|
|
|
|
|
//XXX/FIXME this is a temporary hack until all encoders output pts
|
|
|
|
if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
|
2012-10-02 16:15:13 +00:00
|
|
|
static int warned;
|
|
|
|
if (!warned) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
|
|
|
|
warned = 1;
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
pkt->dts =
|
|
|
|
// pkt->pts= st->cur_dts;
|
|
|
|
pkt->pts = st->pts.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
//calculate dts from pts
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
|
|
|
|
st->pts_buffer[0] = pkt->pts;
|
|
|
|
for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
|
|
|
|
st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
|
|
|
|
for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
|
|
|
|
FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
|
|
|
|
|
|
|
|
pkt->dts = st->pts_buffer[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
|
|
|
|
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
|
|
|
|
st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-10-02 16:15:13 +00:00
|
|
|
"Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
|
|
|
|
st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
|
2012-09-30 22:49:16 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
|
2012-10-02 16:15:13 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
|
|
|
|
av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
|
2012-09-30 22:49:16 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
|
|
|
|
av_ts2str(pkt->pts), av_ts2str(pkt->dts));
|
2012-09-30 22:49:16 +00:00
|
|
|
st->cur_dts = pkt->dts;
|
|
|
|
st->pts.val = pkt->dts;
|
|
|
|
|
|
|
|
/* update pts */
|
|
|
|
switch (st->codec->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2013-12-31 13:09:48 +00:00
|
|
|
frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
|
|
|
|
((AVFrame *)pkt->data)->nb_samples :
|
|
|
|
ff_get_audio_frame_size(st->codec, pkt->size, 1);
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
/* HACK/FIXME, we skip the initial 0 size packets as they are most
|
|
|
|
* likely equal to the encoder delay, but it would be better if we
|
|
|
|
* had the real timestamps from the encoder */
|
|
|
|
if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
|
|
|
|
frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-03 22:13:53 +00:00
|
|
|
/**
|
2013-04-26 08:47:31 +00:00
|
|
|
* Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
|
|
|
|
* sidedata.
|
|
|
|
*
|
2013-04-03 12:11:10 +00:00
|
|
|
* FIXME: this function should NEVER get undefined pts/dts beside when the
|
|
|
|
* AVFMT_NOTIMESTAMPS is set.
|
|
|
|
* Those additional safety checks should be dropped once the correct checks
|
|
|
|
* are set in the callers.
|
2012-12-03 22:13:53 +00:00
|
|
|
*/
|
2013-04-03 12:11:10 +00:00
|
|
|
static int write_packet(AVFormatContext *s, AVPacket *pkt)
|
2012-12-03 22:13:53 +00:00
|
|
|
{
|
2013-04-26 09:37:42 +00:00
|
|
|
int ret, did_split;
|
2013-04-26 08:47:31 +00:00
|
|
|
|
2014-01-27 18:16:45 +00:00
|
|
|
if (s->output_ts_offset) {
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
|
|
|
|
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts += offset;
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts += offset;
|
|
|
|
}
|
|
|
|
|
2013-04-26 08:47:31 +00:00
|
|
|
if (s->avoid_negative_ts > 0) {
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
2013-04-26 09:52:28 +00:00
|
|
|
int64_t offset = st->mux_ts_offset;
|
2013-04-26 09:37:42 +00:00
|
|
|
|
2014-02-21 01:43:04 +00:00
|
|
|
if ((pkt->dts < 0 || s->avoid_negative_ts == 2) && pkt->dts != AV_NOPTS_VALUE && !s->offset) {
|
2013-04-26 09:37:42 +00:00
|
|
|
s->offset = -pkt->dts;
|
|
|
|
s->offset_timebase = st->time_base;
|
2013-04-26 08:47:31 +00:00
|
|
|
}
|
2013-04-26 09:37:42 +00:00
|
|
|
|
2013-04-26 09:52:28 +00:00
|
|
|
if (s->offset && !offset) {
|
|
|
|
offset = st->mux_ts_offset =
|
2013-04-26 09:37:42 +00:00
|
|
|
av_rescale_q_rnd(s->offset,
|
|
|
|
s->offset_timebase,
|
|
|
|
st->time_base,
|
|
|
|
AV_ROUND_UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
2013-04-26 09:52:28 +00:00
|
|
|
pkt->dts += offset;
|
2013-04-26 08:47:31 +00:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
2013-04-26 09:52:28 +00:00
|
|
|
pkt->pts += offset;
|
2013-04-26 09:42:54 +00:00
|
|
|
|
|
|
|
av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0);
|
2013-04-26 08:47:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 21:39:25 +00:00
|
|
|
did_split = av_packet_split_side_data(pkt);
|
2013-12-31 13:09:48 +00:00
|
|
|
if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
|
|
|
|
AVFrame *frame = (AVFrame *)pkt->data;
|
|
|
|
av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
|
|
|
|
ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
} else {
|
|
|
|
ret = s->oformat->write_packet(s, pkt);
|
|
|
|
}
|
2013-09-11 12:02:06 +00:00
|
|
|
|
2013-09-17 10:01:35 +00:00
|
|
|
if (s->flush_packets && s->pb && ret >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
|
2013-03-12 15:54:42 +00:00
|
|
|
avio_flush(s->pb);
|
2013-09-11 12:02:06 +00:00
|
|
|
|
2013-01-08 21:39:25 +00:00
|
|
|
if (did_split)
|
|
|
|
av_packet_merge_side_data(pkt);
|
2013-09-17 10:01:35 +00:00
|
|
|
|
2012-12-03 22:13:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-20 13:10:01 +00:00
|
|
|
static int check_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
if (!pkt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
|
|
|
|
pkt->stream_index);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-01-20 13:10:01 +00:00
|
|
|
ret = check_packet(s, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
if (!pkt) {
|
2012-10-02 16:15:13 +00:00
|
|
|
if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
|
2012-12-03 22:13:53 +00:00
|
|
|
ret = s->oformat->write_packet(s, NULL);
|
2013-04-08 11:31:12 +00:00
|
|
|
if (s->flush_packets && s->pb && s->pb->error >= 0)
|
|
|
|
avio_flush(s->pb);
|
2012-10-02 16:15:13 +00:00
|
|
|
if (ret >= 0 && s->pb && s->pb->error < 0)
|
|
|
|
ret = s->pb->error;
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
|
|
|
|
|
|
|
|
if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
|
|
|
|
return ret;
|
|
|
|
|
2013-04-03 12:11:10 +00:00
|
|
|
ret = write_packet(s, pkt);
|
2012-10-02 16:15:13 +00:00
|
|
|
if (ret >= 0 && s->pb && s->pb->error < 0)
|
|
|
|
ret = s->pb->error;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
s->streams[pkt->stream_index]->nb_frames++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
#define CHUNK_START 0x1000
|
|
|
|
|
|
|
|
int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
|
2012-09-30 22:49:16 +00:00
|
|
|
int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
|
|
|
|
{
|
|
|
|
AVPacketList **next_point, *this_pktl;
|
2012-10-02 16:15:13 +00:00
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
int chunked = s->max_chunk_size || s->max_chunk_duration;
|
2014-02-19 16:43:46 +00:00
|
|
|
int ret;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
this_pktl = av_mallocz(sizeof(AVPacketList));
|
2012-10-02 16:15:13 +00:00
|
|
|
if (!this_pktl)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-09-30 22:49:16 +00:00
|
|
|
this_pktl->pkt = *pkt;
|
2012-10-31 07:53:18 +00:00
|
|
|
#if FF_API_DESTRUCT_PACKET
|
2013-03-27 17:36:51 +00:00
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2012-09-30 22:49:16 +00:00
|
|
|
pkt->destruct = NULL; // do not free original but only the copy
|
2013-03-27 17:36:51 +00:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
2012-10-31 07:53:18 +00:00
|
|
|
#endif
|
|
|
|
pkt->buf = NULL;
|
2013-12-31 13:09:48 +00:00
|
|
|
if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
|
|
|
|
av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
|
|
|
|
av_assert0(((AVFrame *)pkt->data)->buf);
|
|
|
|
} else {
|
2014-02-19 16:43:46 +00:00
|
|
|
// duplicate the packet if it uses non-allocated memory
|
|
|
|
if ((ret = av_dup_packet(&this_pktl->pkt)) < 0) {
|
|
|
|
av_free(this_pktl);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-31 13:09:48 +00:00
|
|
|
av_copy_packet_side_data(&this_pktl->pkt, &this_pktl->pkt); // copy side data
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
|
2012-10-02 16:15:13 +00:00
|
|
|
next_point = &(st->last_in_packet_buffer->next);
|
|
|
|
} else {
|
2012-09-30 22:49:16 +00:00
|
|
|
next_point = &s->packet_buffer;
|
2012-10-02 16:15:13 +00:00
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
|
2013-01-15 18:58:23 +00:00
|
|
|
if (chunked) {
|
|
|
|
uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
|
2013-01-15 22:56:37 +00:00
|
|
|
st->interleaver_chunk_size += pkt->size;
|
|
|
|
st->interleaver_chunk_duration += pkt->duration;
|
2013-01-17 14:08:50 +00:00
|
|
|
if ( (s->max_chunk_size && st->interleaver_chunk_size > s->max_chunk_size)
|
|
|
|
|| (max && st->interleaver_chunk_duration > max)) {
|
2013-01-15 23:03:36 +00:00
|
|
|
st->interleaver_chunk_size = 0;
|
2013-01-15 18:58:23 +00:00
|
|
|
this_pktl->pkt.flags |= CHUNK_START;
|
2013-01-15 23:03:36 +00:00
|
|
|
if (max && st->interleaver_chunk_duration > max) {
|
|
|
|
int64_t syncoffset = (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
|
|
|
|
int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
|
|
|
|
|
|
|
|
st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
|
|
|
|
} else
|
|
|
|
st->interleaver_chunk_duration = 0;
|
2012-10-02 16:15:13 +00:00
|
|
|
}
|
2013-01-15 18:58:23 +00:00
|
|
|
}
|
|
|
|
if (*next_point) {
|
|
|
|
if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
|
|
|
|
goto next_non_null;
|
2012-10-02 16:15:13 +00:00
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
|
2012-10-02 16:15:13 +00:00
|
|
|
while ( *next_point
|
|
|
|
&& ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
|
|
|
|
|| !compare(s, &(*next_point)->pkt, pkt)))
|
2012-09-30 22:49:16 +00:00
|
|
|
next_point = &(*next_point)->next;
|
2012-10-02 16:15:13 +00:00
|
|
|
if (*next_point)
|
|
|
|
goto next_non_null;
|
2012-09-30 22:49:16 +00:00
|
|
|
} else {
|
|
|
|
next_point = &(s->packet_buffer_end->next);
|
|
|
|
}
|
|
|
|
}
|
2012-10-07 21:15:18 +00:00
|
|
|
av_assert1(!*next_point);
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
s->packet_buffer_end = this_pktl;
|
|
|
|
next_non_null:
|
|
|
|
|
|
|
|
this_pktl->next = *next_point;
|
|
|
|
|
|
|
|
s->streams[pkt->stream_index]->last_in_packet_buffer =
|
|
|
|
*next_point = this_pktl;
|
2012-10-02 16:15:13 +00:00
|
|
|
return 0;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 19:47:15 +00:00
|
|
|
static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
|
|
|
|
AVPacket *pkt)
|
2012-09-30 22:49:16 +00:00
|
|
|
{
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
AVStream *st2 = s->streams[next->stream_index];
|
|
|
|
int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
|
|
|
|
st->time_base);
|
2012-10-02 16:15:13 +00:00
|
|
|
if (s->audio_preload && ((st->codec->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codec->codec_type == AVMEDIA_TYPE_AUDIO))) {
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
if (ts == ts2) {
|
|
|
|
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
|
|
|
|
-( 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;
|
|
|
|
ts2=0;
|
|
|
|
}
|
|
|
|
comp= (ts>ts2) - (ts<ts2);
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
if (comp == 0)
|
|
|
|
return pkt->stream_index < next->stream_index;
|
|
|
|
return comp > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
|
|
|
|
AVPacket *pkt, int flush)
|
|
|
|
{
|
|
|
|
AVPacketList *pktl;
|
2012-10-02 16:15:13 +00:00
|
|
|
int stream_count = 0, noninterleaved_count = 0;
|
|
|
|
int i, ret;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
if (pkt) {
|
2013-05-01 16:28:48 +00:00
|
|
|
ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts);
|
2012-10-02 16:15:13 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
if (s->streams[i]->last_in_packet_buffer) {
|
|
|
|
++stream_count;
|
|
|
|
} else if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
++noninterleaved_count;
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
|
2014-02-05 02:17:16 +00:00
|
|
|
if (s->internal->nb_interleaved_streams == stream_count)
|
2012-10-02 16:15:13 +00:00
|
|
|
flush = 1;
|
2014-02-05 02:17:16 +00:00
|
|
|
|
2014-01-20 12:28:37 +00:00
|
|
|
if (s->max_interleave_delta > 0 && s->packet_buffer && !flush) {
|
|
|
|
AVPacket *top_pkt = &s->packet_buffer->pkt;
|
|
|
|
int64_t delta_dts = INT64_MIN;
|
|
|
|
int64_t top_dts = av_rescale_q(top_pkt->dts,
|
|
|
|
s->streams[top_pkt->stream_index]->time_base,
|
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
int64_t last_dts;
|
|
|
|
const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
|
|
|
|
|
|
|
|
if (!last)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
last_dts = av_rescale_q(last->pkt.dts,
|
|
|
|
s->streams[i]->time_base,
|
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
delta_dts = FFMAX(delta_dts, last_dts - top_dts);
|
2012-10-02 16:15:13 +00:00
|
|
|
}
|
2014-01-20 12:28:37 +00:00
|
|
|
|
|
|
|
if (delta_dts > s->max_interleave_delta) {
|
|
|
|
av_log(s, AV_LOG_DEBUG,
|
|
|
|
"Delay between the first packet and last packet in the "
|
|
|
|
"muxing queue is %"PRId64" > %"PRId64": forcing output\n",
|
|
|
|
delta_dts, s->max_interleave_delta);
|
2012-10-02 16:15:13 +00:00
|
|
|
flush = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 12:28:37 +00:00
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
if (stream_count && flush) {
|
|
|
|
AVStream *st;
|
2012-09-30 22:49:16 +00:00
|
|
|
pktl = s->packet_buffer;
|
|
|
|
*out = pktl->pkt;
|
2012-10-02 16:15:13 +00:00
|
|
|
st = s->streams[out->stream_index];
|
2012-09-30 22:49:16 +00:00
|
|
|
|
|
|
|
s->packet_buffer = pktl->next;
|
|
|
|
if (!s->packet_buffer)
|
|
|
|
s->packet_buffer_end = NULL;
|
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
if (st->last_in_packet_buffer == pktl)
|
|
|
|
st->last_in_packet_buffer = NULL;
|
2012-09-30 22:49:16 +00:00
|
|
|
av_freep(&pktl);
|
2012-10-02 16:15:13 +00:00
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
av_init_packet(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interleave an AVPacket correctly so it can be muxed.
|
|
|
|
* @param out the interleaved packet will be output here
|
|
|
|
* @param in the input packet
|
|
|
|
* @param flush 1 if no further packets are available as input and all
|
|
|
|
* remaining packets should be output
|
|
|
|
* @return 1 if a packet was output, 0 if no packet could be output,
|
|
|
|
* < 0 if an error occurred
|
|
|
|
*/
|
|
|
|
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
|
|
|
|
{
|
|
|
|
if (s->oformat->interleave_packet) {
|
|
|
|
int ret = s->oformat->interleave_packet(s, out, in, flush);
|
|
|
|
if (in)
|
|
|
|
av_free_packet(in);
|
|
|
|
return ret;
|
|
|
|
} else
|
|
|
|
return ff_interleave_packet_per_dts(s, out, in, flush);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret, flush = 0;
|
|
|
|
|
2014-01-20 13:10:01 +00:00
|
|
|
ret = check_packet(s, pkt);
|
|
|
|
if (ret < 0)
|
2014-02-04 14:58:11 +00:00
|
|
|
goto fail;
|
2014-01-20 13:10:01 +00:00
|
|
|
|
2012-09-30 22:49:16 +00:00
|
|
|
if (pkt) {
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
|
|
|
|
//FIXME/XXX/HACK drop zero sized packets
|
2014-02-04 14:58:11 +00:00
|
|
|
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
|
2012-10-02 16:15:13 +00:00
|
|
|
av_dlog(s, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
|
|
|
|
pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
|
2012-09-30 22:49:16 +00:00
|
|
|
if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
|
2014-02-04 14:58:11 +00:00
|
|
|
goto fail;
|
2012-09-30 22:49:16 +00:00
|
|
|
|
2014-02-04 14:58:11 +00:00
|
|
|
if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
} else {
|
|
|
|
av_dlog(s, "av_interleaved_write_frame FLUSH\n");
|
|
|
|
flush = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;; ) {
|
|
|
|
AVPacket opkt;
|
|
|
|
int ret = interleave_packet(s, &opkt, pkt, flush);
|
2014-02-04 14:58:11 +00:00
|
|
|
if (pkt) {
|
|
|
|
memset(pkt, 0, sizeof(*pkt));
|
|
|
|
av_init_packet(pkt);
|
|
|
|
pkt = NULL;
|
|
|
|
}
|
2012-09-30 22:49:16 +00:00
|
|
|
if (ret <= 0) //FIXME cleanup needed for ret<0 ?
|
|
|
|
return ret;
|
|
|
|
|
2013-04-03 12:11:10 +00:00
|
|
|
ret = write_packet(s, &opkt);
|
2012-09-30 22:49:16 +00:00
|
|
|
if (ret >= 0)
|
|
|
|
s->streams[opkt.stream_index]->nb_frames++;
|
|
|
|
|
|
|
|
av_free_packet(&opkt);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-10-02 16:15:13 +00:00
|
|
|
if(s->pb && s->pb->error)
|
|
|
|
return s->pb->error;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
2014-02-04 14:58:11 +00:00
|
|
|
fail:
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
for (;; ) {
|
|
|
|
AVPacket pkt;
|
|
|
|
ret = interleave_packet(s, &pkt, NULL, 1);
|
|
|
|
if (ret < 0) //FIXME cleanup needed for ret<0 ?
|
|
|
|
goto fail;
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
|
2013-04-03 12:11:10 +00:00
|
|
|
ret = write_packet(s, &pkt);
|
2012-09-30 22:49:16 +00:00
|
|
|
if (ret >= 0)
|
|
|
|
s->streams[pkt.stream_index]->nb_frames++;
|
|
|
|
|
|
|
|
av_free_packet(&pkt);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2012-10-02 16:15:13 +00:00
|
|
|
if(s->pb && s->pb->error)
|
|
|
|
goto fail;
|
2012-09-30 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->oformat->write_trailer)
|
|
|
|
ret = s->oformat->write_trailer(s);
|
|
|
|
|
|
|
|
fail:
|
2012-10-02 16:15:13 +00:00
|
|
|
if (s->pb)
|
|
|
|
avio_flush(s->pb);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = s->pb ? s->pb->error : 0;
|
2012-09-30 22:49:16 +00:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
av_freep(&s->streams[i]->priv_data);
|
|
|
|
av_freep(&s->streams[i]->index_entries);
|
|
|
|
}
|
|
|
|
if (s->oformat->priv_class)
|
|
|
|
av_opt_free(s->priv_data);
|
|
|
|
av_freep(&s->priv_data);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-02 16:15:13 +00:00
|
|
|
|
|
|
|
int av_get_output_timestamp(struct AVFormatContext *s, int stream,
|
|
|
|
int64_t *dts, int64_t *wall)
|
|
|
|
{
|
|
|
|
if (!s->oformat || !s->oformat->get_output_timestamp)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
s->oformat->get_output_timestamp(s, stream, dts, wall);
|
|
|
|
return 0;
|
2012-10-02 16:31:47 +00:00
|
|
|
}
|
2013-06-16 21:58:59 +00:00
|
|
|
|
2013-06-15 09:56:36 +00:00
|
|
|
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
|
|
|
|
AVFormatContext *src)
|
|
|
|
{
|
|
|
|
AVPacket local_pkt;
|
|
|
|
|
|
|
|
local_pkt = *pkt;
|
|
|
|
local_pkt.stream_index = dst_stream;
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
local_pkt.pts = av_rescale_q(pkt->pts,
|
|
|
|
src->streams[pkt->stream_index]->time_base,
|
|
|
|
dst->streams[dst_stream]->time_base);
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
local_pkt.dts = av_rescale_q(pkt->dts,
|
|
|
|
src->streams[pkt->stream_index]->time_base,
|
|
|
|
dst->streams[dst_stream]->time_base);
|
2013-06-16 21:58:59 +00:00
|
|
|
if (pkt->duration)
|
|
|
|
local_pkt.duration = av_rescale_q(pkt->duration,
|
|
|
|
src->streams[pkt->stream_index]->time_base,
|
|
|
|
dst->streams[dst_stream]->time_base);
|
2013-06-15 09:56:36 +00:00
|
|
|
return av_write_frame(dst, &local_pkt);
|
|
|
|
}
|
2013-12-31 13:09:48 +00:00
|
|
|
|
|
|
|
static int av_write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
|
|
|
|
AVFrame *frame, int interleaved)
|
|
|
|
{
|
|
|
|
AVPacket pkt, *pktp;
|
|
|
|
|
|
|
|
av_assert0(s->oformat);
|
|
|
|
if (!s->oformat->write_uncoded_frame)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
pktp = NULL;
|
|
|
|
} else {
|
|
|
|
pktp = &pkt;
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
pkt.data = (void *)frame;
|
|
|
|
pkt.size = UNCODED_FRAME_PACKET_SIZE;
|
|
|
|
pkt.pts =
|
|
|
|
pkt.dts = frame->pts;
|
|
|
|
pkt.duration = av_frame_get_pkt_duration(frame);
|
|
|
|
pkt.stream_index = stream_index;
|
|
|
|
pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
return interleaved ? av_interleaved_write_frame(s, pktp) :
|
|
|
|
av_write_frame(s, pktp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
|
|
|
|
AVFrame *frame)
|
|
|
|
{
|
|
|
|
return av_write_uncoded_frame_internal(s, stream_index, frame, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index,
|
|
|
|
AVFrame *frame)
|
|
|
|
{
|
|
|
|
return av_write_uncoded_frame_internal(s, stream_index, frame, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
|
|
|
|
{
|
|
|
|
av_assert0(s->oformat);
|
|
|
|
if (!s->oformat->write_uncoded_frame)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
return s->oformat->write_uncoded_frame(s, stream_index, NULL,
|
|
|
|
AV_WRITE_UNCODED_FRAME_QUERY);
|
|
|
|
}
|