2012-10-01 01:49:16 +03:00
|
|
|
/*
|
2012-10-02 19:15:13 +03:00
|
|
|
* muxing functions for use within FFmpeg
|
2012-10-01 01:49:16 +03:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
|
|
|
*
|
2012-10-02 19:15:13 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-10-01 01:49:16 +03:00
|
|
|
*
|
2012-10-02 19:15:13 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-10-01 01:49:16 +03: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 19:15:13 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-10-01 01:49:16 +03: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 19:15:13 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-10-01 01:49:16 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
2021-06-11 23:35:33 +02:00
|
|
|
#include "libavcodec/bsf.h"
|
2012-10-01 01:49:16 +03:00
|
|
|
#include "libavcodec/internal.h"
|
2021-03-05 16:26:24 +02:00
|
|
|
#include "libavcodec/packet_internal.h"
|
2012-10-01 01:49:16 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/dict.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
2012-10-02 19:15:13 +03:00
|
|
|
#include "libavutil/timestamp.h"
|
2012-10-01 01:49:16 +03:00
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2013-03-27 20:36:51 +03:00
|
|
|
#include "libavutil/internal.h"
|
2012-10-01 01:49:16 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2012-10-02 19:15:13 +03:00
|
|
|
* muxing functions for use within libavformat
|
2012-10-01 01:49:16 +03: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
|
|
|
|
*/
|
2015-08-17 16:30:16 +02:00
|
|
|
static void frac_init(FFFrac *f, int64_t val, int64_t num, int64_t den)
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
2015-08-17 16:30:16 +02:00
|
|
|
static void frac_add(FFFrac *f, int64_t incr)
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:35:25 +03:00
|
|
|
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
|
2012-10-03 22:34:43 +03:00
|
|
|
{
|
|
|
|
AVRational q;
|
|
|
|
|
2014-06-18 21:09:55 +03:00
|
|
|
q = st->time_base;
|
|
|
|
|
2021-08-24 17:47:04 +02:00
|
|
|
for (int j = 2; j < 14; j += 1 + (j > 2))
|
2014-04-14 23:35:25 +03:00
|
|
|
while (q.den / q.num < min_precision && q.num % j == 0)
|
2012-10-03 22:34:43 +03:00
|
|
|
q.num /= j;
|
2014-04-14 23:35:25 +03:00
|
|
|
while (q.den / q.num < min_precision && q.den < (1<<24))
|
2012-10-03 22:34:43 +03:00
|
|
|
q.den <<= 1;
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:42:24 +02:00
|
|
|
enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
|
|
|
|
{
|
2016-04-10 21:58:15 +02:00
|
|
|
AVCodecParameters *par = st->codecpar;
|
|
|
|
const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(par->format);
|
2015-05-22 13:42:24 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->chroma_location != AVCHROMA_LOC_UNSPECIFIED)
|
|
|
|
return par->chroma_location;
|
2015-05-22 13:42:24 +02:00
|
|
|
|
|
|
|
if (pix_desc) {
|
|
|
|
if (pix_desc->log2_chroma_h == 0) {
|
|
|
|
return AVCHROMA_LOC_TOPLEFT;
|
|
|
|
} else if (pix_desc->log2_chroma_w == 1 && pix_desc->log2_chroma_h == 1) {
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->field_order == AV_FIELD_UNKNOWN || par->field_order == AV_FIELD_PROGRESSIVE) {
|
|
|
|
switch (par->codec_id) {
|
2015-05-22 13:42:24 +02:00
|
|
|
case AV_CODEC_ID_MJPEG:
|
|
|
|
case AV_CODEC_ID_MPEG1VIDEO: return AVCHROMA_LOC_CENTER;
|
|
|
|
}
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->field_order == AV_FIELD_UNKNOWN || par->field_order != AV_FIELD_PROGRESSIVE) {
|
|
|
|
switch (par->codec_id) {
|
2015-05-22 13:42:24 +02:00
|
|
|
case AV_CODEC_ID_MPEG2VIDEO: return AVCHROMA_LOC_LEFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVCHROMA_LOC_UNSPECIFIED;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-25 04:11:32 +02:00
|
|
|
int avformat_alloc_output_context2(AVFormatContext **avctx, const AVOutputFormat *oformat,
|
2012-10-02 19:15:13 +03:00
|
|
|
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;
|
|
|
|
|
2017-12-29 02:01:37 +02:00
|
|
|
if (filename) {
|
|
|
|
if (!(s->url = av_strdup(filename)))
|
|
|
|
goto nomem;
|
|
|
|
|
|
|
|
}
|
2012-10-02 19:15:13 +03:00
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:16 +03:00
|
|
|
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
|
|
|
|
{
|
|
|
|
const AVCodecTag *avctag;
|
|
|
|
enum AVCodecID id = AV_CODEC_ID_NONE;
|
2014-01-02 00:29:06 +03:00
|
|
|
int64_t tag = -1;
|
2012-10-01 01:49:16 +03: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
|
|
|
|
*/
|
2021-08-24 17:47:04 +02:00
|
|
|
for (int n = 0; s->oformat->codec_tag[n]; n++) {
|
2012-10-01 01:49:16 +03:00
|
|
|
avctag = s->oformat->codec_tag[n];
|
|
|
|
while (avctag->id != AV_CODEC_ID_NONE) {
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codecpar->codec_tag)) {
|
2012-10-01 01:49:16 +03:00
|
|
|
id = avctag->id;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (id == st->codecpar->codec_id)
|
2012-10-01 01:49:16 +03:00
|
|
|
return 1;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (avctag->id == st->codecpar->codec_id)
|
2012-10-01 01:49:16 +03:00
|
|
|
tag = avctag->tag;
|
|
|
|
avctag++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (id != AV_CODEC_ID_NONE)
|
|
|
|
return 0;
|
2014-11-06 14:10:01 +02:00
|
|
|
if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
|
2012-10-01 01:49:16 +03:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:18 +03:00
|
|
|
|
|
|
|
static int init_muxer(AVFormatContext *s, AVDictionary **options)
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2012-10-01 01:49:16 +03:00
|
|
|
AVDictionary *tmp = NULL;
|
2019-03-20 19:38:48 +02:00
|
|
|
const AVOutputFormat *of = s->oformat;
|
2014-05-20 03:30:26 +03:00
|
|
|
AVDictionaryEntry *e;
|
2021-08-24 17:47:04 +02:00
|
|
|
int ret = 0;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
|
|
|
if (options)
|
|
|
|
av_dict_copy(&tmp, *options, 0);
|
2012-10-01 01:49:17 +03:00
|
|
|
|
2012-10-01 01:49:16 +03:00
|
|
|
if ((ret = av_opt_set_dict(s, &tmp)) < 0)
|
|
|
|
goto fail;
|
2012-10-02 19:15:13 +03:00
|
|
|
if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
|
2014-05-04 08:11:50 +03:00
|
|
|
(ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
|
2012-10-02 19:15:13 +03:00
|
|
|
goto fail;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2017-12-29 02:19:37 +02:00
|
|
|
if (!s->url && !(s->url = av_strdup(""))) {
|
2017-12-29 02:01:37 +02:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:16 +03:00
|
|
|
// some sanity checks
|
2012-10-01 01:49:17 +03:00
|
|
|
if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
|
2013-12-15 20:12:10 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
|
2012-10-01 01:49:16 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:47:04 +02:00
|
|
|
for (unsigned i = 0; i < s->nb_streams; i++) {
|
|
|
|
AVStream *const st = s->streams[i];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2021-08-24 17:47:04 +02:00
|
|
|
AVCodecParameters *const par = st->codecpar;
|
|
|
|
const AVCodecDescriptor *desc;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2014-05-18 13:12:59 +03:00
|
|
|
if (!st->time_base.num) {
|
|
|
|
/* fall back on the default timebase values */
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate)
|
|
|
|
avpriv_set_pts_info(st, 64, 1, par->sample_rate);
|
2014-05-18 13:12:59 +03:00
|
|
|
else
|
|
|
|
avpriv_set_pts_info(st, 33, 1, 90000);
|
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
switch (par->codec_type) {
|
2012-10-01 01:49:16 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (par->sample_rate <= 0) {
|
2012-10-01 01:49:16 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "sample rate not set\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (!par->block_align)
|
|
|
|
par->block_align = par->channels *
|
|
|
|
av_get_bits_per_sample(par->codec_id) >> 3;
|
2012-10-01 01:49:16 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if ((par->width <= 0 || par->height <= 0) &&
|
2012-10-01 01:49:17 +03:00
|
|
|
!(of->flags & AVFMT_NODIMENSIONS)) {
|
2012-10-01 01:49:16 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (av_cmp_q(st->sample_aspect_ratio, par->sample_aspect_ratio)
|
|
|
|
&& fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(par->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
|
2012-10-02 19:15:13 +03:00
|
|
|
) {
|
2013-06-24 00:00:34 +03:00
|
|
|
if (st->sample_aspect_ratio.num != 0 &&
|
|
|
|
st->sample_aspect_ratio.den != 0 &&
|
2016-04-10 21:58:15 +02:00
|
|
|
par->sample_aspect_ratio.num != 0 &&
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->sample_aspect_ratio.den != 0) {
|
2013-06-24 00:00:34 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
|
2013-06-24 12:42:42 +03:00
|
|
|
"(%d/%d) and encoder layer (%d/%d)\n",
|
|
|
|
st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
|
2016-04-10 21:58:15 +02:00
|
|
|
par->sample_aspect_ratio.num,
|
|
|
|
par->sample_aspect_ratio.den);
|
2013-06-24 00:00:34 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
desc = avcodec_descriptor_get(par->codec_id);
|
2015-10-07 15:51:11 +02:00
|
|
|
if (desc && desc->props & AV_CODEC_PROP_REORDER)
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->reorder = 1;
|
2015-10-07 15:51:11 +02:00
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->is_intra_only = ff_is_intra_only(par->codec_id);
|
avformat/mux: Set AV_PKT_FLAG_KEY for is_intra_only packet
The patch will make audio and subtitle packets be marked as AV_PKT_FLAG_KEY.
For audio, it'll caused the audio sample to be sync sample.
To verify ref/fate/movenc results:
1. Get the movenc test data
[lmwang@vpn ffmpeg]$ libavformat/tests/movenc -w && mkdir -p audio_old && mv *.mp4 audio_old_
After applied the patch:
[lmwang@vpn ffmpeg]$ make fate-movenc SAMPLES=../fate-suite
[lmwang@vpn ffmpeg]$ libavformat/tests/movenc -w && mkdir -p audio_key && mv *.mp4 audio_key
2. Get l-smash and build boxdumper
https://github.com/l-smash/l-smash.git
3. dump the box of crc change mp4 and diff -u
[lmwang@vpn ffmpeg]$ ../l-smash/cli/boxdumper --box audio_key/non-empty-moov-no-elst.mp4 > audio_key/non-empty-moov-no-elst.log
[lmwang@vpn ffmpeg]$ ../l-smash/cli/boxdumper --box audio_old/non-empty-moov-no-elst.mp4 > audio_old/non-empty-moov-no-elst.log
[lmwang@vpn ffmpeg]$ diff -u audio_key/non-empty-moov-no-elst.log audio_old/non-empty-moov-no-elst.log
- default_sample_flags = 0x02000000
- independent
- sync sample
+ default_sample_flags = 0x01010000
+ dependent
+ non-sync sample
4. have checked the change of crc are caused by default_sample_flags
non-empty-moov.mp4, non-empty-moov-elst.mp4,
non-empty-moov-no-elst.mp4, empty-moov.mp4, delay-moov-content.mp4,
empty-moov-second-frag.mp4, empty-moov-second-frag-discont.mp4,
delay-moov-second-frag-discont.mp4, delay-moov-elst-second-frag.mp4
etc
5 For subtitle, it'll effect for tests/ref/fate/binsub-movtextenc and
tests/ref/fate/sub2video, that's expecting result for the subtitle is
marked as keyframe. Below is the checking result of binsub-movtextenc:
[lmwang@vpn ffmpeg]$ ./ffmpeg -i ../fate-suite/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -flags +bitexact -fflags +bitexact -movflags frag_keyframe+empty_moov audio_key/binsub-movtextenc.mp4
[lmwang@vpn ffmpeg]$ ./ffmpeg -i ../fate-suite/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -flags +bitexact -fflags +bitexact -movflags frag_keyframe+empty_moov audio_old/binsub-movtextenc.mp4
[lmwang@vpn ffmpeg]$../l-smash/cli/boxdumper audio_key/binsub-movtextenc.mp4 > audio_key/binsub-movtextenc.log
[lmwang@vpn ffmpeg]$../l-smash/cli/boxdumper audio_old/binsub-movtextenc.mp4 > audio_old/binsub-movtextenc.log
[lmwang@vpn ffmpeg]$ diff -u audio_key/binsub-movtextenc.log audio_old/binsub-movtextenc.log
.... // the key difference is the flag for sync sample
- flags = 0x000701
+ flags = 0x000301
data-offset-present
sample-duration-present
sample-size-present
- sample-flags-present
sample_count = 6
- data_offset = 188
+ data_offset = 164
sample[0]
sample_duration = 1570000
sample_size = 21
- sample_flags = 0x02000000
- independent
- sync sample
- degradation_priority = 0
sample[1]
sample_duration = 510000
sample_size = 2
- sample_flags = 0x01010000
- dependent
- non-sync sample
- degradation_priority = 0
sample[2]
sample_duration = 1690000
sample_size = 9
- sample_flags = 0x02000000
- independent
- sync sample
- degradation_priority = 0
Suggested-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Suggested-by: Nicolas George <george@nsup.org>
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
2020-04-21 17:35:24 +02:00
|
|
|
|
2012-10-01 01:49:17 +03:00
|
|
|
if (of->codec_tag) {
|
2016-04-10 21:58:15 +02:00
|
|
|
if ( par->codec_tag
|
|
|
|
&& par->codec_id == AV_CODEC_ID_RAWVIDEO
|
|
|
|
&& ( av_codec_get_tag(of->codec_tag, par->codec_id) == 0
|
|
|
|
|| av_codec_get_tag(of->codec_tag, par->codec_id) == MKTAG('r', 'a', 'w', ' '))
|
2012-10-02 19:31:47 +03:00
|
|
|
&& !validate_codec_tag(s, st)) {
|
2012-10-01 01:49:17 +03:00
|
|
|
// the current rawvideo encoding system ends up setting
|
2012-10-02 19:31:47 +03:00
|
|
|
// the wrong codec_tag for avi/mov, we override it here
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->codec_tag = 0;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (par->codec_tag) {
|
2012-10-01 01:49:16 +03:00
|
|
|
if (!validate_codec_tag(s, st)) {
|
2017-03-27 01:31:52 +02:00
|
|
|
const uint32_t otag = av_codec_get_tag(s->oformat->codec_tag, par->codec_id);
|
2012-10-01 01:49:16 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2017-03-27 21:31:46 +02:00
|
|
|
"Tag %s incompatible with output codec id '%d' (%s)\n",
|
|
|
|
av_fourcc2str(par->codec_tag), par->codec_id, av_fourcc2str(otag));
|
2012-10-01 01:49:16 +03:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->codec_tag = av_codec_get_tag(of->codec_tag, par->codec_id);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT)
|
2021-08-24 13:37:28 +02:00
|
|
|
si->nb_interleaved_streams++;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
2021-10-09 16:13:32 +02:00
|
|
|
si->interleave_packet = of->interleave_packet;
|
|
|
|
if (!si->interleave_packet)
|
|
|
|
si->interleave_packet = ff_interleave_packet_per_dts;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2012-10-01 01:49:17 +03:00
|
|
|
if (!s->priv_data && of->priv_data_size > 0) {
|
|
|
|
s->priv_data = av_mallocz(of->priv_data_size);
|
2012-10-01 01:49:16 +03:00
|
|
|
if (!s->priv_data) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-01 01:49:17 +03:00
|
|
|
if (of->priv_class) {
|
|
|
|
*(const AVClass **)s->priv_data = of->priv_class;
|
2012-10-01 01:49:16 +03:00
|
|
|
av_opt_set_defaults(s->priv_data);
|
2014-04-30 01:00:44 +03:00
|
|
|
if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
|
2012-10-01 01:49:16 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set muxer identification string */
|
2014-05-01 11:43:10 +03:00
|
|
|
if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
|
2012-10-01 01:49:16 +03:00
|
|
|
av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
|
2013-12-06 06:35:35 +03:00
|
|
|
} else {
|
|
|
|
av_dict_set(&s->metadata, "encoder", NULL, 0);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2014-05-20 03:30:26 +03:00
|
|
|
for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
|
|
|
|
av_dict_set(&s->metadata, e->key, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:18 +03:00
|
|
|
if (options) {
|
|
|
|
av_dict_free(options);
|
|
|
|
*options = tmp;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2016-06-25 05:02:50 +02:00
|
|
|
if (s->oformat->init) {
|
|
|
|
if ((ret = s->oformat->init(s)) < 0) {
|
|
|
|
if (s->oformat->deinit)
|
|
|
|
s->oformat->deinit(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return ret == 0;
|
2015-10-08 04:32:14 +02:00
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:18 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_dict_free(&tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_pts(AVFormatContext *s)
|
|
|
|
{
|
2012-10-01 01:49:16 +03:00
|
|
|
/* init PTS generation */
|
2021-08-24 17:47:04 +02:00
|
|
|
for (unsigned i = 0; i < s->nb_streams; i++) {
|
|
|
|
AVStream *const st = s->streams[i];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2012-10-01 01:49:16 +03:00
|
|
|
int64_t den = AV_NOPTS_VALUE;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
switch (st->codecpar->codec_type) {
|
2012-10-01 01:49:16 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2016-04-10 21:58:15 +02:00
|
|
|
den = (int64_t)st->time_base.num * st->codecpar->sample_rate;
|
2012-10-01 01:49:16 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2016-04-10 21:58:15 +02:00
|
|
|
den = (int64_t)st->time_base.num * st->time_base.den;
|
2012-10-01 01:49:16 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-08-17 16:30:16 +02:00
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
if (!sti->priv_pts)
|
|
|
|
sti->priv_pts = av_mallocz(sizeof(*sti->priv_pts));
|
|
|
|
if (!sti->priv_pts)
|
2015-08-17 16:30:16 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-10-01 01:49:16 +03:00
|
|
|
if (den != AV_NOPTS_VALUE) {
|
2012-10-01 01:49:18 +03:00
|
|
|
if (den <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
frac_init(sti->priv_pts, 0, 0, den);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 06:02:30 +02:00
|
|
|
if (s->avoid_negative_ts < 0) {
|
|
|
|
av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
|
|
|
|
if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
|
|
|
|
s->avoid_negative_ts = 0;
|
|
|
|
} else
|
|
|
|
s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
|
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:18 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-04 04:19:33 +02:00
|
|
|
static void flush_if_needed(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
if (s->pb && s->pb->error >= 0) {
|
|
|
|
if (s->flush_packets == 1 || s->flags & AVFMT_FLAG_FLUSH_PACKETS)
|
|
|
|
avio_flush(s->pb);
|
|
|
|
else if (s->flush_packets && !(s->oformat->flags & AVFMT_NOFILE))
|
|
|
|
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 04:23:32 +02:00
|
|
|
static void deinit_muxer(AVFormatContext *s)
|
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-08-24 13:37:28 +02:00
|
|
|
if (s->oformat && s->oformat->deinit && si->initialized)
|
2019-10-19 04:23:32 +02:00
|
|
|
s->oformat->deinit(s);
|
2021-08-24 13:37:28 +02:00
|
|
|
si->initialized =
|
|
|
|
si->streams_initialized = 0;
|
2019-10-19 04:23:32 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 05:02:50 +02:00
|
|
|
int avformat_init_output(AVFormatContext *s, AVDictionary **options)
|
2012-10-01 01:49:18 +03:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2012-10-01 01:49:18 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
2015-05-16 18:33:51 +02:00
|
|
|
if ((ret = init_muxer(s, options)) < 0)
|
2012-10-01 01:49:18 +03:00
|
|
|
return ret;
|
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
si->initialized = 1;
|
|
|
|
si->streams_initialized = ret;
|
2016-06-25 05:02:50 +02:00
|
|
|
|
|
|
|
if (s->oformat->init && ret) {
|
|
|
|
if ((ret = init_pts(s)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return AVSTREAM_INIT_IN_INIT_OUTPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVSTREAM_INIT_IN_WRITE_HEADER;
|
|
|
|
}
|
|
|
|
|
|
|
|
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
|
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-08-24 13:37:28 +02:00
|
|
|
int already_initialized = si->initialized;
|
|
|
|
int streams_already_initialized = si->streams_initialized;
|
2016-06-25 05:02:50 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!already_initialized)
|
|
|
|
if ((ret = avformat_init_output(s, options)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-11-26 21:47:26 +02:00
|
|
|
if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
|
|
|
|
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
|
|
|
|
if (s->oformat->write_header) {
|
|
|
|
ret = s->oformat->write_header(s);
|
|
|
|
if (ret >= 0 && s->pb && s->pb->error < 0)
|
|
|
|
ret = s->pb->error;
|
2012-10-01 01:49:18 +03:00
|
|
|
if (ret < 0)
|
2016-06-11 18:52:27 +02:00
|
|
|
goto fail;
|
2017-11-26 21:47:26 +02:00
|
|
|
flush_if_needed(s);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
2017-11-26 21:47:26 +02:00
|
|
|
if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
|
|
|
|
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
|
2012-10-01 01:49:18 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (!si->streams_initialized) {
|
2016-06-25 05:02:50 +02:00
|
|
|
if ((ret = init_pts(s)) < 0)
|
|
|
|
goto fail;
|
2013-04-26 12:52:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-25 05:02:50 +02:00
|
|
|
return streams_already_initialized;
|
2016-06-11 18:52:27 +02:00
|
|
|
|
|
|
|
fail:
|
2019-10-19 04:23:32 +02:00
|
|
|
deinit_muxer(s);
|
2016-06-11 18:52:27 +02:00
|
|
|
return ret;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2013-12-31 16:09:48 +03:00
|
|
|
#define AV_PKT_FLAG_UNCODED_FRAME 0x2000
|
|
|
|
|
|
|
|
|
2021-04-04 11:48:48 +02:00
|
|
|
#if FF_API_COMPUTE_PKT_FIELDS2
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2012-10-01 01:49:16 +03:00
|
|
|
//FIXME merge with compute_pkt_fields
|
2015-11-13 00:06:32 +02:00
|
|
|
static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2021-08-26 20:07:36 +02:00
|
|
|
int delay = st->codecpar->video_delay;
|
2014-05-19 19:38:10 +03:00
|
|
|
int frame_size;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (!si->missing_ts_warning &&
|
2015-10-07 15:51:11 +02:00
|
|
|
!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
|
2016-11-13 02:01:52 +02:00
|
|
|
(!(st->disposition & AV_DISPOSITION_ATTACHED_PIC) || (st->disposition & AV_DISPOSITION_TIMED_THUMBNAILS)) &&
|
2015-10-07 15:51:11 +02:00
|
|
|
(pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
|
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"Timestamps are unset in a packet for stream %d. "
|
|
|
|
"This is deprecated and will stop working in the future. "
|
|
|
|
"Fix your code to set the timestamps properly\n", st->index);
|
2021-08-24 13:37:28 +02:00
|
|
|
si->missing_ts_warning = 1;
|
2015-10-07 15:51:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 17:29:20 +02:00
|
|
|
if (s->debug & FF_FDEBUG_TS)
|
2018-08-13 09:02:41 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
|
2021-08-24 19:07:51 +02:00
|
|
|
av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), delay, pkt->size, pkt->stream_index);
|
2012-10-01 01:49:16 +03:00
|
|
|
|
|
|
|
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 19:15:13 +03: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-10-01 01:49:16 +03:00
|
|
|
pkt->dts =
|
|
|
|
// pkt->pts= st->cur_dts;
|
2021-08-24 19:07:51 +02:00
|
|
|
pkt->pts = sti->priv_pts->val;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//calculate dts from pts
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->pts_buffer[0] = pkt->pts;
|
|
|
|
for (int i = 1; i < delay + 1 && sti->pts_buffer[i] == AV_NOPTS_VALUE; i++)
|
|
|
|
sti->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
|
|
|
|
for (int i = 0; i<delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
|
|
|
|
FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
pkt->dts = sti->pts_buffer[0];
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->cur_dts && sti->cur_dts != AV_NOPTS_VALUE &&
|
2012-10-01 01:49:16 +03:00
|
|
|
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE &&
|
|
|
|
st->codecpar->codec_type != AVMEDIA_TYPE_DATA &&
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->cur_dts >= pkt->dts) || sti->cur_dts > pkt->dts)) {
|
2012-10-01 01:49:16 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-10-02 19:15:13 +03:00
|
|
|
"Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
|
2021-08-24 19:07:51 +02:00
|
|
|
st->index, av_ts2str(sti->cur_dts), av_ts2str(pkt->dts));
|
2012-10-01 01:49:16 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
|
2014-10-09 18:18:03 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2014-10-10 21:34:14 +03:00
|
|
|
"pts (%s) < dts (%s) in stream %d\n",
|
|
|
|
av_ts2str(pkt->pts), av_ts2str(pkt->dts),
|
2014-10-09 18:18:03 +03:00
|
|
|
st->index);
|
2012-10-01 01:49:16 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2015-04-20 17:29:20 +02:00
|
|
|
if (s->debug & FF_FDEBUG_TS)
|
2018-08-13 09:02:41 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%s dts2:%s\n",
|
2012-10-02 19:15:13 +03:00
|
|
|
av_ts2str(pkt->pts), av_ts2str(pkt->dts));
|
2015-04-20 17:29:20 +02:00
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->cur_dts = pkt->dts;
|
|
|
|
sti->priv_pts->val = pkt->dts;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
|
|
|
/* update pts */
|
2016-07-17 04:10:38 +02:00
|
|
|
switch (st->codecpar->codec_type) {
|
2012-10-01 01:49:16 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2013-12-31 16:09:48 +03:00
|
|
|
frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
|
2019-08-09 14:32:15 +02:00
|
|
|
(*(AVFrame **)pkt->data)->nb_samples :
|
2021-04-04 11:48:48 +02:00
|
|
|
av_get_audio_frame_duration2(st->codecpar, pkt->size);
|
2012-10-01 01:49:16 +03: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 */
|
2021-08-24 19:07:51 +02:00
|
|
|
if (frame_size >= 0 && (pkt->size || sti->priv_pts->num != sti->priv_pts->den >> 1 || sti->priv_pts->val)) {
|
|
|
|
frac_add(sti->priv_pts, (int64_t)st->time_base.den * frame_size);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2021-08-24 19:07:51 +02:00
|
|
|
frac_add(sti->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
|
2012-10-01 01:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
2015-10-07 15:51:11 +02:00
|
|
|
#endif
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2020-11-29 18:40:14 +02:00
|
|
|
static void guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
if (pkt->duration < 0 && st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n",
|
|
|
|
pkt->duration, pkt->stream_index);
|
|
|
|
pkt->duration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->duration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (st->codecpar->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0) {
|
|
|
|
pkt->duration = av_rescale_q(1, av_inv_q(st->avg_frame_rate),
|
|
|
|
st->time_base);
|
|
|
|
} else if (st->time_base.num * 1000LL > st->time_base.den)
|
|
|
|
pkt->duration = 1;
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO: {
|
|
|
|
int frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
|
|
|
|
if (frame_size && st->codecpar->sample_rate) {
|
|
|
|
pkt->duration = av_rescale_q(frame_size,
|
|
|
|
(AVRational){1, st->codecpar->sample_rate},
|
|
|
|
st->time_base);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 01:13:53 +03:00
|
|
|
/**
|
2019-08-13 04:47:26 +02:00
|
|
|
* Shift timestamps and call muxer; the original pts/dts are not kept.
|
2013-04-26 11:47:31 +03:00
|
|
|
*
|
2013-04-03 15:11:10 +03: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-04 01:13:53 +03:00
|
|
|
*/
|
2013-04-03 15:11:10 +03:00
|
|
|
static int write_packet(AVFormatContext *s, AVPacket *pkt)
|
2012-12-04 01:13:53 +03:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-09-01 21:14:41 +02:00
|
|
|
AVStream *const st = s->streams[pkt->stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2017-10-22 06:29:41 +02:00
|
|
|
int ret;
|
2013-04-26 11:47:31 +03:00
|
|
|
|
2016-06-24 22:38:54 +02:00
|
|
|
// If the timestamp offsetting below is adjusted, adjust
|
|
|
|
// ff_interleaved_peek similarly.
|
2014-01-27 21:16:45 +03:00
|
|
|
if (s->output_ts_offset) {
|
|
|
|
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 11:47:31 +03:00
|
|
|
if (s->avoid_negative_ts > 0) {
|
2021-08-24 19:07:51 +02:00
|
|
|
int64_t offset = sti->mux_ts_offset;
|
2021-08-24 13:37:28 +02:00
|
|
|
int64_t ts = si->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
|
2013-04-26 12:37:42 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
|
2015-05-05 12:44:20 +02:00
|
|
|
(ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
|
2021-08-24 13:37:28 +02:00
|
|
|
si->offset = -ts;
|
|
|
|
si->offset_timebase = st->time_base;
|
2013-04-26 11:47:31 +03:00
|
|
|
}
|
2013-04-26 12:37:42 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->offset != AV_NOPTS_VALUE && !offset) {
|
2021-08-24 19:07:51 +02:00
|
|
|
offset = sti->mux_ts_offset =
|
2021-08-24 13:37:28 +02:00
|
|
|
av_rescale_q_rnd(si->offset,
|
|
|
|
si->offset_timebase,
|
2013-04-26 12:37:42 +03:00
|
|
|
st->time_base,
|
|
|
|
AV_ROUND_UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
2013-04-26 12:52:28 +03:00
|
|
|
pkt->dts += offset;
|
2013-04-26 11:47:31 +03:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
2013-04-26 12:52:28 +03:00
|
|
|
pkt->pts += offset;
|
2013-04-26 12:42:54 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->avoid_negative_ts_use_pts) {
|
2015-05-05 12:44:20 +02:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "failed to avoid negative "
|
|
|
|
"pts %s in stream %d.\n"
|
|
|
|
"Try -avoid_negative_ts 1 as a possible workaround.\n",
|
2017-05-26 18:01:31 +02:00
|
|
|
av_ts2str(pkt->pts),
|
2015-05-05 12:44:20 +02:00
|
|
|
pkt->stream_index
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
|
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"Packets poorly interleaved, failed to avoid negative "
|
|
|
|
"timestamp %s in stream %d.\n"
|
|
|
|
"Try -max_interleave_delta 0 as a possible workaround.\n",
|
|
|
|
av_ts2str(pkt->dts),
|
|
|
|
pkt->stream_index
|
|
|
|
);
|
|
|
|
}
|
2014-10-22 17:15:02 +03:00
|
|
|
}
|
2013-04-26 11:47:31 +03:00
|
|
|
}
|
|
|
|
|
2013-12-31 16:09:48 +03:00
|
|
|
if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
|
2019-08-09 14:32:15 +02:00
|
|
|
AVFrame **frame = (AVFrame **)pkt->data;
|
|
|
|
av_assert0(pkt->size == sizeof(*frame));
|
|
|
|
ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, frame, 0);
|
2013-12-31 16:09:48 +03:00
|
|
|
} else {
|
|
|
|
ret = s->oformat->write_packet(s, pkt);
|
|
|
|
}
|
2013-09-11 15:02:06 +03:00
|
|
|
|
2015-08-27 06:04:16 +02:00
|
|
|
if (s->pb && ret >= 0) {
|
2017-06-04 04:19:33 +02:00
|
|
|
flush_if_needed(s);
|
2015-08-27 06:04:16 +02:00
|
|
|
if (s->pb->error < 0)
|
|
|
|
ret = s->pb->error;
|
|
|
|
}
|
2013-09-11 15:02:06 +03:00
|
|
|
|
2019-08-13 04:47:26 +02:00
|
|
|
if (ret >= 0)
|
2021-09-01 21:14:41 +02:00
|
|
|
st->nb_frames++;
|
2016-08-02 15:24:19 +02:00
|
|
|
|
2012-12-04 01:13:53 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-20 16:10:01 +03:00
|
|
|
static int check_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
|
2014-01-20 16:10:01 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-16 02:04:34 +02:00
|
|
|
static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2021-04-04 11:48:48 +02:00
|
|
|
#if !FF_API_COMPUTE_PKT_FIELDS2
|
2015-10-07 15:51:11 +02:00
|
|
|
/* sanitize the timestamps */
|
|
|
|
if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
|
|
|
|
|
|
|
|
/* when there is no reordering (so dts is equal to pts), but
|
|
|
|
* only one of them is set, set the other as well */
|
2021-08-24 19:07:51 +02:00
|
|
|
if (!sti->reorder) {
|
2015-10-07 15:51:11 +02:00
|
|
|
if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts = pkt->dts;
|
|
|
|
if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts = pkt->pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that the timestamps are set */
|
|
|
|
if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Timestamps are unset in a packet for stream %d\n", st->index);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that the dts are increasing (or at least non-decreasing,
|
|
|
|
* if the format allows it */
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->cur_dts != AV_NOPTS_VALUE &&
|
|
|
|
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && sti->cur_dts >= pkt->dts) ||
|
|
|
|
sti->cur_dts > pkt->dts)) {
|
2015-10-07 15:51:11 +02:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Application provided invalid, non monotonically increasing "
|
|
|
|
"dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
|
2021-08-24 19:07:51 +02:00
|
|
|
st->index, sti->cur_dts, pkt->dts);
|
2015-10-07 15:51:11 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->pts < pkt->dts) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
|
|
|
|
pkt->pts, pkt->dts, st->index);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
avformat/mux: Set AV_PKT_FLAG_KEY for is_intra_only packet
The patch will make audio and subtitle packets be marked as AV_PKT_FLAG_KEY.
For audio, it'll caused the audio sample to be sync sample.
To verify ref/fate/movenc results:
1. Get the movenc test data
[lmwang@vpn ffmpeg]$ libavformat/tests/movenc -w && mkdir -p audio_old && mv *.mp4 audio_old_
After applied the patch:
[lmwang@vpn ffmpeg]$ make fate-movenc SAMPLES=../fate-suite
[lmwang@vpn ffmpeg]$ libavformat/tests/movenc -w && mkdir -p audio_key && mv *.mp4 audio_key
2. Get l-smash and build boxdumper
https://github.com/l-smash/l-smash.git
3. dump the box of crc change mp4 and diff -u
[lmwang@vpn ffmpeg]$ ../l-smash/cli/boxdumper --box audio_key/non-empty-moov-no-elst.mp4 > audio_key/non-empty-moov-no-elst.log
[lmwang@vpn ffmpeg]$ ../l-smash/cli/boxdumper --box audio_old/non-empty-moov-no-elst.mp4 > audio_old/non-empty-moov-no-elst.log
[lmwang@vpn ffmpeg]$ diff -u audio_key/non-empty-moov-no-elst.log audio_old/non-empty-moov-no-elst.log
- default_sample_flags = 0x02000000
- independent
- sync sample
+ default_sample_flags = 0x01010000
+ dependent
+ non-sync sample
4. have checked the change of crc are caused by default_sample_flags
non-empty-moov.mp4, non-empty-moov-elst.mp4,
non-empty-moov-no-elst.mp4, empty-moov.mp4, delay-moov-content.mp4,
empty-moov-second-frag.mp4, empty-moov-second-frag-discont.mp4,
delay-moov-second-frag-discont.mp4, delay-moov-elst-second-frag.mp4
etc
5 For subtitle, it'll effect for tests/ref/fate/binsub-movtextenc and
tests/ref/fate/sub2video, that's expecting result for the subtitle is
marked as keyframe. Below is the checking result of binsub-movtextenc:
[lmwang@vpn ffmpeg]$ ./ffmpeg -i ../fate-suite/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -flags +bitexact -fflags +bitexact -movflags frag_keyframe+empty_moov audio_key/binsub-movtextenc.mp4
[lmwang@vpn ffmpeg]$ ./ffmpeg -i ../fate-suite/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -flags +bitexact -fflags +bitexact -movflags frag_keyframe+empty_moov audio_old/binsub-movtextenc.mp4
[lmwang@vpn ffmpeg]$../l-smash/cli/boxdumper audio_key/binsub-movtextenc.mp4 > audio_key/binsub-movtextenc.log
[lmwang@vpn ffmpeg]$../l-smash/cli/boxdumper audio_old/binsub-movtextenc.mp4 > audio_old/binsub-movtextenc.log
[lmwang@vpn ffmpeg]$ diff -u audio_key/binsub-movtextenc.log audio_old/binsub-movtextenc.log
.... // the key difference is the flag for sync sample
- flags = 0x000701
+ flags = 0x000301
data-offset-present
sample-duration-present
sample-size-present
- sample-flags-present
sample_count = 6
- data_offset = 188
+ data_offset = 164
sample[0]
sample_duration = 1570000
sample_size = 21
- sample_flags = 0x02000000
- independent
- sync sample
- degradation_priority = 0
sample[1]
sample_duration = 510000
sample_size = 2
- sample_flags = 0x01010000
- dependent
- non-sync sample
- degradation_priority = 0
sample[2]
sample_duration = 1690000
sample_size = 9
- sample_flags = 0x02000000
- independent
- sync sample
- degradation_priority = 0
Suggested-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Suggested-by: Nicolas George <george@nsup.org>
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
2020-04-21 17:35:24 +02:00
|
|
|
/* update flags */
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->is_intra_only)
|
avformat/mux: Set AV_PKT_FLAG_KEY for is_intra_only packet
The patch will make audio and subtitle packets be marked as AV_PKT_FLAG_KEY.
For audio, it'll caused the audio sample to be sync sample.
To verify ref/fate/movenc results:
1. Get the movenc test data
[lmwang@vpn ffmpeg]$ libavformat/tests/movenc -w && mkdir -p audio_old && mv *.mp4 audio_old_
After applied the patch:
[lmwang@vpn ffmpeg]$ make fate-movenc SAMPLES=../fate-suite
[lmwang@vpn ffmpeg]$ libavformat/tests/movenc -w && mkdir -p audio_key && mv *.mp4 audio_key
2. Get l-smash and build boxdumper
https://github.com/l-smash/l-smash.git
3. dump the box of crc change mp4 and diff -u
[lmwang@vpn ffmpeg]$ ../l-smash/cli/boxdumper --box audio_key/non-empty-moov-no-elst.mp4 > audio_key/non-empty-moov-no-elst.log
[lmwang@vpn ffmpeg]$ ../l-smash/cli/boxdumper --box audio_old/non-empty-moov-no-elst.mp4 > audio_old/non-empty-moov-no-elst.log
[lmwang@vpn ffmpeg]$ diff -u audio_key/non-empty-moov-no-elst.log audio_old/non-empty-moov-no-elst.log
- default_sample_flags = 0x02000000
- independent
- sync sample
+ default_sample_flags = 0x01010000
+ dependent
+ non-sync sample
4. have checked the change of crc are caused by default_sample_flags
non-empty-moov.mp4, non-empty-moov-elst.mp4,
non-empty-moov-no-elst.mp4, empty-moov.mp4, delay-moov-content.mp4,
empty-moov-second-frag.mp4, empty-moov-second-frag-discont.mp4,
delay-moov-second-frag-discont.mp4, delay-moov-elst-second-frag.mp4
etc
5 For subtitle, it'll effect for tests/ref/fate/binsub-movtextenc and
tests/ref/fate/sub2video, that's expecting result for the subtitle is
marked as keyframe. Below is the checking result of binsub-movtextenc:
[lmwang@vpn ffmpeg]$ ./ffmpeg -i ../fate-suite/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -flags +bitexact -fflags +bitexact -movflags frag_keyframe+empty_moov audio_key/binsub-movtextenc.mp4
[lmwang@vpn ffmpeg]$ ./ffmpeg -i ../fate-suite/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -flags +bitexact -fflags +bitexact -movflags frag_keyframe+empty_moov audio_old/binsub-movtextenc.mp4
[lmwang@vpn ffmpeg]$../l-smash/cli/boxdumper audio_key/binsub-movtextenc.mp4 > audio_key/binsub-movtextenc.log
[lmwang@vpn ffmpeg]$../l-smash/cli/boxdumper audio_old/binsub-movtextenc.mp4 > audio_old/binsub-movtextenc.log
[lmwang@vpn ffmpeg]$ diff -u audio_key/binsub-movtextenc.log audio_old/binsub-movtextenc.log
.... // the key difference is the flag for sync sample
- flags = 0x000701
+ flags = 0x000301
data-offset-present
sample-duration-present
sample-size-present
- sample-flags-present
sample_count = 6
- data_offset = 188
+ data_offset = 164
sample[0]
sample_duration = 1570000
sample_size = 21
- sample_flags = 0x02000000
- independent
- sync sample
- degradation_priority = 0
sample[1]
sample_duration = 510000
sample_size = 2
- sample_flags = 0x01010000
- dependent
- non-sync sample
- degradation_priority = 0
sample[2]
sample_duration = 1690000
sample_size = 9
- sample_flags = 0x02000000
- independent
- sync sample
- degradation_priority = 0
Suggested-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Suggested-by: Nicolas George <george@nsup.org>
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
2020-04-21 17:35:24 +02:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2015-10-07 15:51:11 +02:00
|
|
|
|
2021-09-03 22:05:21 +02:00
|
|
|
if (!pkt->data && !pkt->side_data_elems) {
|
|
|
|
/* Such empty packets signal EOS for the BSF API; so sanitize
|
|
|
|
* the packet by allocating data of size 0 (+ padding). */
|
|
|
|
av_buffer_unref(&pkt->buf);
|
|
|
|
return av_packet_make_refcounted(pkt);
|
|
|
|
}
|
|
|
|
|
2015-10-07 15:51:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-02 19:15:13 +03:00
|
|
|
#define CHUNK_START 0x1000
|
|
|
|
|
|
|
|
int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
|
2019-08-13 04:47:16 +02:00
|
|
|
int (*compare)(AVFormatContext *, const AVPacket *, const AVPacket *))
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
2014-07-14 09:22:44 +03:00
|
|
|
int ret;
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-03-05 16:26:24 +02:00
|
|
|
PacketList **next_point, *this_pktl;
|
2019-08-13 04:47:17 +02:00
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2019-08-13 04:47:17 +02:00
|
|
|
int chunked = s->max_chunk_size || s->max_chunk_duration;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-03-05 16:26:24 +02:00
|
|
|
this_pktl = av_malloc(sizeof(PacketList));
|
2020-04-11 13:37:24 +02:00
|
|
|
if (!this_pktl) {
|
|
|
|
av_packet_unref(pkt);
|
2012-10-02 19:15:13 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2020-04-11 13:37:24 +02:00
|
|
|
}
|
2019-08-09 14:32:15 +02:00
|
|
|
if ((ret = av_packet_make_refcounted(pkt)) < 0) {
|
|
|
|
av_free(this_pktl);
|
2020-04-11 13:37:24 +02:00
|
|
|
av_packet_unref(pkt);
|
2019-08-09 14:32:15 +02:00
|
|
|
return ret;
|
2013-12-31 16:09:48 +03:00
|
|
|
}
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2019-08-13 04:47:14 +02:00
|
|
|
av_packet_move_ref(&this_pktl->pkt, pkt);
|
|
|
|
pkt = &this_pktl->pkt;
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->last_in_packet_buffer) {
|
|
|
|
next_point = &(sti->last_in_packet_buffer->next);
|
2012-10-02 19:15:13 +03:00
|
|
|
} else {
|
2021-08-24 13:37:28 +02:00
|
|
|
next_point = &si->packet_buffer;
|
2012-10-02 19:15:13 +03:00
|
|
|
}
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2013-01-15 21:58:23 +03:00
|
|
|
if (chunked) {
|
|
|
|
uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->interleaver_chunk_size += pkt->size;
|
|
|
|
sti->interleaver_chunk_duration += pkt->duration;
|
|
|
|
if ( (s->max_chunk_size && sti->interleaver_chunk_size > s->max_chunk_size)
|
|
|
|
|| (max && sti->interleaver_chunk_duration > max)) {
|
|
|
|
sti->interleaver_chunk_size = 0;
|
2019-08-13 04:47:17 +02:00
|
|
|
pkt->flags |= CHUNK_START;
|
2021-08-24 19:07:51 +02:00
|
|
|
if (max && sti->interleaver_chunk_duration > max) {
|
2016-04-10 21:58:15 +02:00
|
|
|
int64_t syncoffset = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
|
2013-01-16 02:03:36 +03:00
|
|
|
int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
|
2013-01-16 02:03:36 +03:00
|
|
|
} else
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->interleaver_chunk_duration = 0;
|
2012-10-02 19:15:13 +03:00
|
|
|
}
|
2013-01-15 21:58:23 +03:00
|
|
|
}
|
|
|
|
if (*next_point) {
|
2019-08-13 04:47:17 +02:00
|
|
|
if (chunked && !(pkt->flags & CHUNK_START))
|
2013-01-15 21:58:23 +03:00
|
|
|
goto next_non_null;
|
2012-10-02 19:15:13 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (compare(s, &si->packet_buffer_end->pkt, pkt)) {
|
2012-10-02 19:15:13 +03:00
|
|
|
while ( *next_point
|
|
|
|
&& ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
|
|
|
|
|| !compare(s, &(*next_point)->pkt, pkt)))
|
2012-10-01 01:49:16 +03:00
|
|
|
next_point = &(*next_point)->next;
|
2012-10-02 19:15:13 +03:00
|
|
|
if (*next_point)
|
|
|
|
goto next_non_null;
|
2012-10-01 01:49:16 +03:00
|
|
|
} else {
|
2021-08-24 13:37:28 +02:00
|
|
|
next_point = &(si->packet_buffer_end->next);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
}
|
2012-10-08 00:15:18 +03:00
|
|
|
av_assert1(!*next_point);
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
si->packet_buffer_end = this_pktl;
|
2012-10-01 01:49:16 +03:00
|
|
|
next_non_null:
|
|
|
|
|
|
|
|
this_pktl->next = *next_point;
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->last_in_packet_buffer = *next_point = this_pktl;
|
2014-07-14 09:22:44 +03:00
|
|
|
|
2012-10-02 19:15:13 +03:00
|
|
|
return 0;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 04:47:16 +02:00
|
|
|
static int interleave_compare_dts(AVFormatContext *s, const AVPacket *next,
|
|
|
|
const AVPacket *pkt)
|
2012-10-01 01:49:16 +03: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);
|
2019-06-24 23:16:18 +02:00
|
|
|
if (s->audio_preload) {
|
|
|
|
int preload = st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
|
|
|
|
int preload2 = st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
|
|
|
|
if (preload != preload2) {
|
2019-06-27 19:47:23 +02:00
|
|
|
int64_t ts, ts2;
|
2019-06-24 23:16:18 +02:00
|
|
|
preload *= s->audio_preload;
|
|
|
|
preload2 *= s->audio_preload;
|
2019-06-27 19:47:23 +02:00
|
|
|
ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - preload;
|
|
|
|
ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - preload2;
|
2019-06-24 23:16:18 +02:00
|
|
|
if (ts == ts2) {
|
|
|
|
ts = ((uint64_t)pkt ->dts*st ->time_base.num*AV_TIME_BASE - (uint64_t)preload *st ->time_base.den)*st2->time_base.den
|
|
|
|
- ((uint64_t)next->dts*st2->time_base.num*AV_TIME_BASE - (uint64_t)preload2*st2->time_base.den)*st ->time_base.den;
|
|
|
|
ts2 = 0;
|
|
|
|
}
|
|
|
|
comp = (ts2 > ts) - (ts2 < ts);
|
2012-10-02 19:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 01:49:16 +03:00
|
|
|
|
|
|
|
if (comp == 0)
|
|
|
|
return pkt->stream_index < next->stream_index;
|
|
|
|
return comp > 0;
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:33:37 +02:00
|
|
|
int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *pkt,
|
|
|
|
int flush, int has_packet)
|
2012-10-01 01:49:16 +03:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2012-10-01 01:49:16 +03:00
|
|
|
int stream_count = 0;
|
2014-07-17 15:18:20 +03:00
|
|
|
int noninterleaved_count = 0;
|
2021-08-24 17:47:04 +02:00
|
|
|
int ret;
|
2016-08-12 21:28:08 +02:00
|
|
|
int eof = flush;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-09-05 17:33:37 +02:00
|
|
|
if (has_packet) {
|
2014-07-14 09:22:44 +03:00
|
|
|
if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
|
2012-10-02 19:15:13 +03:00
|
|
|
return ret;
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 17:47:04 +02:00
|
|
|
for (unsigned i = 0; i < s->nb_streams; i++) {
|
2021-08-24 19:07:51 +02:00
|
|
|
const AVStream *const st = s->streams[i];
|
2021-08-24 19:41:16 +02:00
|
|
|
const FFStream *const sti = cffstream(st);
|
2021-08-24 19:07:51 +02:00
|
|
|
const AVCodecParameters *const par = st->codecpar;
|
|
|
|
if (sti->last_in_packet_buffer) {
|
2012-10-02 19:15:13 +03:00
|
|
|
++stream_count;
|
2021-08-24 19:07:51 +02:00
|
|
|
} else if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
|
|
|
|
par->codec_id != AV_CODEC_ID_VP8 &&
|
|
|
|
par->codec_id != AV_CODEC_ID_VP9) {
|
2012-10-02 19:15:13 +03:00
|
|
|
++noninterleaved_count;
|
|
|
|
}
|
|
|
|
}
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->nb_interleaved_streams == stream_count)
|
2012-10-02 19:15:13 +03:00
|
|
|
flush = 1;
|
2014-02-05 05:17:16 +03:00
|
|
|
|
2014-07-26 01:56:32 +03:00
|
|
|
if (s->max_interleave_delta > 0 &&
|
2021-08-24 13:37:28 +02:00
|
|
|
si->packet_buffer &&
|
2014-07-26 01:56:32 +03:00
|
|
|
!flush &&
|
2021-08-24 13:37:28 +02:00
|
|
|
si->nb_interleaved_streams == stream_count+noninterleaved_count
|
2014-07-26 01:56:32 +03:00
|
|
|
) {
|
2021-08-24 13:37:28 +02:00
|
|
|
AVPacket *const top_pkt = &si->packet_buffer->pkt;
|
2014-01-20 15:28:37 +03:00
|
|
|
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);
|
|
|
|
|
2021-08-24 17:47:04 +02:00
|
|
|
for (unsigned i = 0; i < s->nb_streams; i++) {
|
2021-08-24 19:07:51 +02:00
|
|
|
const AVStream *const st = s->streams[i];
|
2021-08-24 19:41:16 +02:00
|
|
|
const FFStream *const sti = cffstream(st);
|
2021-08-24 19:07:51 +02:00
|
|
|
const PacketList *last = sti->last_in_packet_buffer;
|
2014-01-20 15:28:37 +03:00
|
|
|
int64_t last_dts;
|
|
|
|
|
|
|
|
if (!last)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
last_dts = av_rescale_q(last->pkt.dts,
|
2021-08-24 19:07:51 +02:00
|
|
|
st->time_base,
|
2014-01-20 15:28:37 +03:00
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
delta_dts = FFMAX(delta_dts, last_dts - top_dts);
|
2012-10-02 19:15:13 +03:00
|
|
|
}
|
2014-01-20 15:28:37 +03: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 19:15:13 +03:00
|
|
|
flush = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 15:28:37 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->packet_buffer &&
|
2016-08-12 21:28:08 +02:00
|
|
|
eof &&
|
|
|
|
(s->flags & AVFMT_FLAG_SHORTEST) &&
|
2021-08-24 13:37:28 +02:00
|
|
|
si->shortest_end == AV_NOPTS_VALUE) {
|
|
|
|
AVPacket *const top_pkt = &si->packet_buffer->pkt;
|
2016-08-12 21:28:08 +02:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
si->shortest_end = av_rescale_q(top_pkt->dts,
|
2016-08-12 21:28:08 +02:00
|
|
|
s->streams[top_pkt->stream_index]->time_base,
|
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->shortest_end != AV_NOPTS_VALUE) {
|
|
|
|
while (si->packet_buffer) {
|
|
|
|
PacketList *pktl = si->packet_buffer;
|
2021-08-24 17:47:04 +02:00
|
|
|
AVPacket *const top_pkt = &pktl->pkt;
|
|
|
|
AVStream *const st = s->streams[top_pkt->stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2021-08-24 17:47:04 +02:00
|
|
|
int64_t top_dts = av_rescale_q(top_pkt->dts, st->time_base,
|
2016-08-12 21:28:08 +02:00
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
if (si->shortest_end + 1 >= top_dts)
|
2016-08-12 21:28:08 +02:00
|
|
|
break;
|
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
si->packet_buffer = pktl->next;
|
|
|
|
if (!si->packet_buffer)
|
|
|
|
si->packet_buffer_end = NULL;
|
2016-08-12 21:28:08 +02:00
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->last_in_packet_buffer == pktl)
|
|
|
|
sti->last_in_packet_buffer = NULL;
|
2016-08-12 21:28:08 +02:00
|
|
|
|
|
|
|
av_packet_unref(&pktl->pkt);
|
|
|
|
av_freep(&pktl);
|
|
|
|
flush = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 19:15:13 +03:00
|
|
|
if (stream_count && flush) {
|
2021-08-24 13:37:28 +02:00
|
|
|
PacketList *pktl = si->packet_buffer;
|
2021-08-24 17:47:04 +02:00
|
|
|
AVStream *const st = s->streams[pktl->pkt.stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2021-08-24 19:07:51 +02:00
|
|
|
|
2021-09-05 17:33:37 +02:00
|
|
|
*pkt = pktl->pkt;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 13:37:28 +02:00
|
|
|
si->packet_buffer = pktl->next;
|
|
|
|
if (!si->packet_buffer)
|
|
|
|
si->packet_buffer_end = NULL;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->last_in_packet_buffer == pktl)
|
|
|
|
sti->last_in_packet_buffer = NULL;
|
2012-10-01 01:49:16 +03:00
|
|
|
av_freep(&pktl);
|
2012-10-02 19:15:13 +03:00
|
|
|
|
2012-10-01 01:49:16 +03:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:30:47 +02:00
|
|
|
int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset)
|
|
|
|
{
|
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
if (stream_index < 0 || stream_index >= s->nb_streams)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
st = s->streams[stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
*offset = ffstream(st)->mux_ts_offset;
|
2021-02-08 23:30:47 +02:00
|
|
|
|
|
|
|
if (s->output_ts_offset)
|
|
|
|
*offset += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:04:05 +02:00
|
|
|
const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
|
2016-04-20 22:10:37 +02:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-08-24 13:37:28 +02:00
|
|
|
PacketList *pktl = si->packet_buffer;
|
2016-04-20 22:10:37 +02:00
|
|
|
while (pktl) {
|
2016-06-25 02:54:43 +02:00
|
|
|
if (pktl->pkt.stream_index == stream) {
|
2021-02-09 00:04:05 +02:00
|
|
|
return &pktl->pkt;
|
2016-06-25 02:54:43 +02:00
|
|
|
}
|
2016-04-20 22:10:37 +02:00
|
|
|
pktl = pktl->next;
|
|
|
|
}
|
2021-02-09 00:04:05 +02:00
|
|
|
return NULL;
|
2016-04-20 22:10:37 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:41:16 +02:00
|
|
|
static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
|
2020-04-18 14:20:51 +02:00
|
|
|
{
|
2020-04-27 21:07:07 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!(s->flags & AVFMT_FLAG_AUTO_BSF))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (s->oformat->check_bitstream) {
|
2021-08-24 19:07:51 +02:00
|
|
|
if (!sti->bitstream_checked) {
|
2020-04-27 21:07:07 +02:00
|
|
|
if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret == 1)
|
2021-08-24 19:07:51 +02:00
|
|
|
sti->bitstream_checked = 1;
|
2020-04-27 21:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:33:37 +02:00
|
|
|
static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt,
|
|
|
|
int flush, int has_packet)
|
2020-03-27 01:00:53 +02:00
|
|
|
{
|
2021-10-09 16:13:32 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2020-03-27 01:00:53 +02:00
|
|
|
for (;; ) {
|
2021-10-09 16:13:32 +02:00
|
|
|
int ret = si->interleave_packet(s, pkt, flush, has_packet);
|
2020-03-27 01:00:53 +02:00
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-09-05 17:33:37 +02:00
|
|
|
has_packet = 0;
|
2020-03-27 01:00:53 +02:00
|
|
|
|
2021-09-05 17:33:37 +02:00
|
|
|
ret = write_packet(s, pkt);
|
|
|
|
av_packet_unref(pkt);
|
2020-03-27 01:00:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:22:53 +02:00
|
|
|
static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (s->debug & FF_FDEBUG_TS)
|
|
|
|
av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __FUNCTION__,
|
|
|
|
pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
|
|
|
|
|
2020-11-29 18:40:14 +02:00
|
|
|
guess_pkt_duration(s, st, pkt);
|
|
|
|
|
2021-04-04 11:48:48 +02:00
|
|
|
#if FF_API_COMPUTE_PKT_FIELDS2
|
2020-03-27 03:22:53 +02:00
|
|
|
if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (interleaved) {
|
|
|
|
if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
|
|
|
|
return AVERROR(EINVAL);
|
2021-09-05 17:33:37 +02:00
|
|
|
return interleaved_write_packet(s, pkt, 0, 1);
|
2020-03-27 03:22:53 +02:00
|
|
|
} else {
|
|
|
|
return write_packet(s, pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 14:20:51 +02:00
|
|
|
static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
|
|
|
|
{
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2021-08-24 19:07:51 +02:00
|
|
|
AVBSFContext *const bsfc = sti->bsfc;
|
2020-04-18 14:20:51 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = av_bsf_send_packet(bsfc, pkt)) < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Failed to send packet to filter %s for stream %d\n",
|
|
|
|
bsfc->filter->name, st->index);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = av_bsf_receive_packet(bsfc, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
|
|
|
|
return 0;
|
|
|
|
av_log(s, AV_LOG_ERROR, "Error applying bitstream filters to an output "
|
|
|
|
"packet for stream #%d: %s\n", st->index, av_err2str(ret));
|
|
|
|
if (!(s->error_recognition & AV_EF_EXPLODE) && ret != AVERROR(ENOMEM))
|
|
|
|
continue;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
av_packet_rescale_ts(pkt, bsfc->time_base_out, st->time_base);
|
|
|
|
ret = write_packet_common(s, st, pkt, interleaved);
|
|
|
|
if (ret >= 0 && !interleaved) // a successful write_packet_common already unrefed pkt for interleaved
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
} while (ret >= 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:22:53 +02:00
|
|
|
static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleaved)
|
|
|
|
{
|
2020-05-08 00:46:10 +02:00
|
|
|
AVStream *st;
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *sti;
|
2020-05-16 02:04:34 +02:00
|
|
|
int ret = check_packet(s, pkt);
|
2020-03-27 03:22:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-05-08 00:46:10 +02:00
|
|
|
st = s->streams[pkt->stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
sti = ffstream(st);
|
2020-03-27 03:22:53 +02:00
|
|
|
|
2020-05-16 02:04:34 +02:00
|
|
|
ret = prepare_input_packet(s, st, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
ret = check_bitstream(s, sti, pkt);
|
2020-04-18 14:20:51 +02:00
|
|
|
if (ret < 0)
|
2020-03-27 03:22:53 +02:00
|
|
|
return ret;
|
|
|
|
|
2021-08-24 19:07:51 +02:00
|
|
|
if (sti->bsfc) {
|
2020-04-18 14:20:51 +02:00
|
|
|
return write_packets_from_bsfs(s, st, pkt, interleaved);
|
|
|
|
} else {
|
|
|
|
return write_packet_common(s, st, pkt, interleaved);
|
|
|
|
}
|
2020-03-27 03:22:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 21:07:07 +02:00
|
|
|
int av_write_frame(AVFormatContext *s, AVPacket *in)
|
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-09-05 19:05:01 +02:00
|
|
|
AVPacket *pkt = si->parse_pkt;
|
2020-04-27 21:07:07 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!in) {
|
|
|
|
if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
|
|
|
|
ret = s->oformat->write_packet(s, NULL);
|
|
|
|
flush_if_needed(s);
|
|
|
|
if (ret >= 0 && s->pb && s->pb->error < 0)
|
|
|
|
ret = s->pb->error;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in->flags & AV_PKT_FLAG_UNCODED_FRAME) {
|
|
|
|
pkt = in;
|
|
|
|
} else {
|
|
|
|
/* We don't own in, so we have to make sure not to modify it.
|
2021-09-01 21:14:41 +02:00
|
|
|
* (ff_write_chained() relies on this fact.)
|
2020-04-27 21:07:07 +02:00
|
|
|
* The following avoids copying in's data unnecessarily.
|
|
|
|
* Copying side data is unavoidable as a bitstream filter
|
|
|
|
* may change it, e.g. free it on errors. */
|
|
|
|
pkt->data = in->data;
|
|
|
|
pkt->size = in->size;
|
|
|
|
ret = av_packet_copy_props(pkt, in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (in->buf) {
|
|
|
|
pkt->buf = av_buffer_ref(in->buf);
|
|
|
|
if (!pkt->buf) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:22:53 +02:00
|
|
|
ret = write_packets_common(s, pkt, 0/*non-interleaved*/);
|
2020-04-27 21:07:07 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
// Uncoded frames using the noninterleaved codepath are also freed here
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-01 01:49:16 +03:00
|
|
|
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2020-03-27 03:22:53 +02:00
|
|
|
int ret;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
|
|
|
if (pkt) {
|
2020-03-27 03:22:53 +02:00
|
|
|
ret = write_packets_common(s, pkt, 1/*interleaved*/);
|
2019-08-09 03:19:42 +02:00
|
|
|
if (ret < 0)
|
2020-03-27 03:22:53 +02:00
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
2012-10-01 01:49:16 +03:00
|
|
|
} else {
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
|
2021-09-05 17:33:37 +02:00
|
|
|
return interleaved_write_packet(s, ffformatcontext(s)->parse_pkt, 1/*flush*/, 0);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-09-05 19:05:01 +02:00
|
|
|
AVPacket *const pkt = si->parse_pkt;
|
2021-08-24 17:47:04 +02:00
|
|
|
int ret1, ret = 0;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2021-08-24 17:47:04 +02:00
|
|
|
for (unsigned i = 0; i < s->nb_streams; i++) {
|
2021-09-06 03:29:19 +02:00
|
|
|
AVStream *const st = s->streams[i];
|
|
|
|
FFStream *const sti = ffstream(st);
|
|
|
|
if (sti->bsfc) {
|
|
|
|
ret1 = write_packets_from_bsfs(s, st, pkt, 1/*interleaved*/);
|
2020-04-18 14:20:51 +02:00
|
|
|
if (ret1 < 0)
|
2021-01-30 20:04:24 +02:00
|
|
|
av_packet_unref(pkt);
|
2020-04-18 14:20:51 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
ret = ret1;
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 17:33:37 +02:00
|
|
|
ret1 = interleaved_write_packet(s, pkt, 1, 0);
|
2020-04-18 14:20:51 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
ret = ret1;
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2017-11-26 21:47:26 +02:00
|
|
|
if (s->oformat->write_trailer) {
|
2016-06-23 15:27:00 +02:00
|
|
|
if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
|
|
|
|
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);
|
2014-11-20 11:14:51 +02:00
|
|
|
if (ret >= 0) {
|
2012-10-01 01:49:16 +03:00
|
|
|
ret = s->oformat->write_trailer(s);
|
2014-11-20 11:14:51 +02:00
|
|
|
} else {
|
|
|
|
s->oformat->write_trailer(s);
|
|
|
|
}
|
2016-06-23 15:27:00 +02:00
|
|
|
}
|
2012-10-01 01:49:16 +03:00
|
|
|
|
2019-10-19 04:23:32 +02:00
|
|
|
deinit_muxer(s);
|
2016-06-25 05:02:50 +02:00
|
|
|
|
2012-10-02 19:15:13 +03:00
|
|
|
if (s->pb)
|
|
|
|
avio_flush(s->pb);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = s->pb ? s->pb->error : 0;
|
2021-08-24 17:47:04 +02:00
|
|
|
for (unsigned i = 0; i < s->nb_streams; i++) {
|
2012-10-01 01:49:16 +03:00
|
|
|
av_freep(&s->streams[i]->priv_data);
|
2021-08-24 19:41:16 +02:00
|
|
|
av_freep(&ffstream(s->streams[i])->index_entries);
|
2012-10-01 01:49:16 +03:00
|
|
|
}
|
|
|
|
if (s->oformat->priv_class)
|
|
|
|
av_opt_free(s->priv_data);
|
|
|
|
av_freep(&s->priv_data);
|
2021-09-05 23:18:29 +02:00
|
|
|
av_packet_unref(si->pkt);
|
2012-10-01 01:49:16 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2012-10-02 19:15:13 +03: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 19:31:47 +03:00
|
|
|
}
|
2013-06-17 00:58:59 +03:00
|
|
|
|
2013-06-15 12:56:36 +03:00
|
|
|
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
|
2014-07-24 23:39:22 +03:00
|
|
|
AVFormatContext *src, int interleave)
|
2013-06-15 12:56:36 +03:00
|
|
|
{
|
2021-09-01 21:14:41 +02:00
|
|
|
int64_t pts = pkt->pts, dts = pkt->dts, duration = pkt->duration;
|
|
|
|
int stream_index = pkt->stream_index;
|
|
|
|
AVRational time_base = pkt->time_base;
|
2014-07-24 23:33:21 +03:00
|
|
|
int ret;
|
2013-06-15 12:56:36 +03:00
|
|
|
|
2021-09-01 21:14:41 +02:00
|
|
|
pkt->stream_index = dst_stream;
|
2019-08-13 04:47:19 +02:00
|
|
|
|
2021-09-01 21:14:41 +02:00
|
|
|
av_packet_rescale_ts(pkt,
|
|
|
|
src->streams[stream_index]->time_base,
|
2019-08-13 04:47:19 +02:00
|
|
|
dst->streams[dst_stream]->time_base);
|
2014-07-24 23:33:21 +03:00
|
|
|
|
2021-09-01 21:14:41 +02:00
|
|
|
if (!interleave) {
|
|
|
|
ret = av_write_frame(dst, pkt);
|
|
|
|
/* We only have to backup and restore the fields that
|
|
|
|
* we changed ourselves, because av_write_frame() does not
|
|
|
|
* modify the packet given to it. */
|
|
|
|
pkt->pts = pts;
|
|
|
|
pkt->dts = dts;
|
|
|
|
pkt->duration = duration;
|
|
|
|
pkt->stream_index = stream_index;
|
|
|
|
pkt->time_base = time_base;
|
|
|
|
} else
|
|
|
|
ret = av_interleaved_write_frame(dst, pkt);
|
|
|
|
|
2014-07-24 23:33:21 +03:00
|
|
|
return ret;
|
2013-06-15 12:56:36 +03:00
|
|
|
}
|
2013-12-31 16:09:48 +03:00
|
|
|
|
2019-08-09 14:32:15 +02:00
|
|
|
static void uncoded_frame_free(void *unused, uint8_t *data)
|
|
|
|
{
|
|
|
|
av_frame_free((AVFrame **)data);
|
|
|
|
av_free(data);
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:47:22 +02:00
|
|
|
static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
|
|
|
|
AVFrame *frame, int interleaved)
|
2013-12-31 16:09:48 +03:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2021-09-05 19:05:01 +02:00
|
|
|
AVPacket *pkt = si->parse_pkt;
|
2013-12-31 16:09:48 +03:00
|
|
|
|
|
|
|
av_assert0(s->oformat);
|
2019-08-09 14:32:15 +02:00
|
|
|
if (!s->oformat->write_uncoded_frame) {
|
|
|
|
av_frame_free(&frame);
|
2013-12-31 16:09:48 +03:00
|
|
|
return AVERROR(ENOSYS);
|
2019-08-09 14:32:15 +02:00
|
|
|
}
|
2013-12-31 16:09:48 +03:00
|
|
|
|
|
|
|
if (!frame) {
|
2021-01-30 20:04:24 +02:00
|
|
|
pkt = NULL;
|
2013-12-31 16:09:48 +03:00
|
|
|
} else {
|
2019-08-09 14:32:15 +02:00
|
|
|
size_t bufsize = sizeof(frame) + AV_INPUT_BUFFER_PADDING_SIZE;
|
|
|
|
AVFrame **framep = av_mallocz(bufsize);
|
|
|
|
|
|
|
|
if (!framep)
|
|
|
|
goto fail;
|
2021-01-30 20:04:24 +02:00
|
|
|
pkt->buf = av_buffer_create((void *)framep, bufsize,
|
2019-08-09 14:32:15 +02:00
|
|
|
uncoded_frame_free, NULL, 0);
|
2021-01-30 20:04:24 +02:00
|
|
|
if (!pkt->buf) {
|
2019-08-09 14:32:15 +02:00
|
|
|
av_free(framep);
|
|
|
|
fail:
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
*framep = frame;
|
|
|
|
|
2021-01-30 20:04:24 +02:00
|
|
|
pkt->data = (void *)framep;
|
|
|
|
pkt->size = sizeof(frame);
|
|
|
|
pkt->pts =
|
|
|
|
pkt->dts = frame->pts;
|
|
|
|
pkt->duration = frame->pkt_duration;
|
|
|
|
pkt->stream_index = stream_index;
|
|
|
|
pkt->flags |= AV_PKT_FLAG_UNCODED_FRAME;
|
2013-12-31 16:09:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:04:24 +02:00
|
|
|
return interleaved ? av_interleaved_write_frame(s, pkt) :
|
|
|
|
av_write_frame(s, pkt);
|
2013-12-31 16:09:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
|
|
|
|
AVFrame *frame)
|
|
|
|
{
|
2019-08-13 04:47:22 +02:00
|
|
|
return write_uncoded_frame_internal(s, stream_index, frame, 0);
|
2013-12-31 16:09:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index,
|
|
|
|
AVFrame *frame)
|
|
|
|
{
|
2019-08-13 04:47:22 +02:00
|
|
|
return write_uncoded_frame_internal(s, stream_index, frame, 1);
|
2013-12-31 16:09:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|