2007-08-09 04:12:48 +03:00
|
|
|
/*
|
|
|
|
* nut muxer
|
|
|
|
* Copyright (c) 2004-2007 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-11-23 23:32:55 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-06-04 14:58:23 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/tree.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2012-06-13 19:51:50 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2014-03-06 19:58:34 +03:00
|
|
|
#include "libavutil/time.h"
|
|
|
|
#include "libavutil/opt.h"
|
2013-12-23 04:58:36 +03:00
|
|
|
#include "libavcodec/bytestream.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavcodec/mpegaudiodata.h"
|
2007-08-09 04:12:48 +03:00
|
|
|
#include "nut.h"
|
2010-05-22 19:01:32 +03:00
|
|
|
#include "internal.h"
|
2011-03-17 13:56:25 +02:00
|
|
|
#include "avio_internal.h"
|
2012-11-27 19:52:28 +03:00
|
|
|
#include "riff.h"
|
2022-02-23 14:18:39 +02:00
|
|
|
#include "version.h"
|
2008-02-15 04:57:19 +02: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
|
|
|
static int find_expected_header(AVCodecParameters *p, int size, int key_frame,
|
2012-10-27 01:41:02 +03:00
|
|
|
uint8_t out[64])
|
|
|
|
{
|
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
|
|
|
int sample_rate = p->sample_rate;
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (size > 4096)
|
2008-02-15 04:57:19 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
AV_WB24(out, 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 (p->codec_id == AV_CODEC_ID_MPEG4) {
|
2012-10-27 01:41:02 +03:00
|
|
|
if (key_frame) {
|
2008-02-15 04:57:19 +02:00
|
|
|
return 3;
|
2012-10-27 01:41:02 +03:00
|
|
|
} else {
|
|
|
|
out[3] = 0xB6;
|
2008-02-15 04:57:19 +02:00
|
|
|
return 4;
|
|
|
|
}
|
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
|
|
|
} else if (p->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
|
|
|
|
p->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
|
2008-02-15 04:57:19 +02:00
|
|
|
return 3;
|
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
|
|
|
} else if (p->codec_id == AV_CODEC_ID_H264) {
|
2008-02-15 04:57:19 +02:00
|
|
|
return 3;
|
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
|
|
|
} else if (p->codec_id == AV_CODEC_ID_MP3 ||
|
|
|
|
p->codec_id == AV_CODEC_ID_MP2) {
|
2008-02-15 04:57:19 +02:00
|
|
|
int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
|
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
|
|
|
int layer = p->codec_id == AV_CODEC_ID_MP3 ? 3 : 2;
|
2012-10-27 01:41:02 +03:00
|
|
|
unsigned int header = 0xFFF00000;
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
lsf = sample_rate < (24000 + 32000) / 2;
|
|
|
|
mpeg25 = sample_rate < (12000 + 16000) / 2;
|
2008-02-15 04:57:19 +02:00
|
|
|
sample_rate <<= lsf + mpeg25;
|
2012-10-28 16:04:43 +03:00
|
|
|
if (sample_rate < (32000 + 44100) / 2) sample_rate_index = 2;
|
|
|
|
else if (sample_rate < (44100 + 48000) / 2) sample_rate_index = 0;
|
|
|
|
else sample_rate_index = 1;
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2021-03-09 13:15:35 +02:00
|
|
|
sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (bitrate_index = 2; bitrate_index < 30; bitrate_index++) {
|
|
|
|
frame_size =
|
2021-03-09 13:15:35 +02:00
|
|
|
ff_mpa_bitrate_tab[lsf][layer - 1][bitrate_index >> 1];
|
2012-10-27 01:41:02 +03:00
|
|
|
frame_size = (frame_size * 144000) / (sample_rate << lsf) +
|
|
|
|
(bitrate_index & 1);
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (frame_size == size)
|
2008-02-15 04:57:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
header |= (!lsf) << 19;
|
|
|
|
header |= (4 - layer) << 17;
|
|
|
|
header |= 1 << 16; //no crc
|
2008-02-15 04:57:19 +02:00
|
|
|
AV_WB32(out, header);
|
2012-10-27 01:41:02 +03:00
|
|
|
if (size <= 0)
|
|
|
|
return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead
|
|
|
|
if (bitrate_index == 30)
|
|
|
|
return -1; //something is wrong ...
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
header |= (bitrate_index >> 1) << 12;
|
|
|
|
header |= sample_rate_index << 10;
|
|
|
|
header |= (bitrate_index & 1) << 9;
|
2008-02-15 04:57:19 +02:00
|
|
|
|
|
|
|
return 2; //FIXME actually put the needed ones in build_elision_headers()
|
2013-02-14 14:42:57 +03:00
|
|
|
//return 3; //we guess that the private bit is not set
|
2008-03-10 20:42:09 +02:00
|
|
|
//FIXME the above assumptions should be checked, if these turn out false too often something should be done
|
2008-02-15 04:57:19 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
static int find_header_idx(AVFormatContext *s, AVCodecParameters *p, int size,
|
2012-10-27 01:41:02 +03:00
|
|
|
int frame_type)
|
|
|
|
{
|
2008-02-15 04:57:19 +02:00
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
uint8_t out[64];
|
|
|
|
int i;
|
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
|
|
|
int len = find_expected_header(p, size, frame_type, out);
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 1; i < nut->header_count; i++) {
|
|
|
|
if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) {
|
2008-02-15 04:57:19 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2012-10-27 01:41:02 +03:00
|
|
|
|
2008-02-15 04:57:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static void build_elision_headers(AVFormatContext *s)
|
|
|
|
{
|
2008-02-15 04:57:19 +02:00
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
int i;
|
|
|
|
//FIXME this is lame
|
|
|
|
//FIXME write a 2pass mode to find the maximal headers
|
2012-10-27 01:41:02 +03:00
|
|
|
static const uint8_t headers[][5] = {
|
|
|
|
{ 3, 0x00, 0x00, 0x01 },
|
|
|
|
{ 4, 0x00, 0x00, 0x01, 0xB6},
|
|
|
|
{ 2, 0xFF, 0xFA }, //mp3+crc
|
|
|
|
{ 2, 0xFF, 0xFB }, //mp3
|
|
|
|
{ 2, 0xFF, 0xFC }, //mp2+crc
|
|
|
|
{ 2, 0xFF, 0xFD }, //mp2
|
2008-02-15 04:57:19 +02:00
|
|
|
};
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->header_count = 7;
|
|
|
|
for (i = 1; i < nut->header_count; i++) {
|
|
|
|
nut->header_len[i] = headers[i - 1][0];
|
|
|
|
nut->header[i] = &headers[i - 1][1];
|
2008-02-15 04:57:19 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static void build_frame_code(AVFormatContext *s)
|
|
|
|
{
|
2007-08-09 04:12:48 +03:00
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
int key_frame, index, pred, stream_id;
|
2012-10-27 01:41:02 +03:00
|
|
|
int start = 1;
|
|
|
|
int end = 254;
|
|
|
|
int keyframe_0_esc = s->nb_streams > 2;
|
2007-08-09 04:12:48 +03:00
|
|
|
int pred_table[10];
|
2007-08-09 23:52:49 +03:00
|
|
|
FrameCode *ft;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
ft = &nut->frame_code[start];
|
|
|
|
ft->flags = FLAG_CODED;
|
|
|
|
ft->size_mul = 1;
|
|
|
|
ft->pts_delta = 1;
|
2007-08-09 23:52:49 +03:00
|
|
|
start++;
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (keyframe_0_esc) {
|
2007-08-09 04:12:48 +03:00
|
|
|
/* keyframe = 0 escape */
|
2012-10-27 01:41:02 +03:00
|
|
|
FrameCode *ft = &nut->frame_code[start];
|
|
|
|
ft->flags = FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
|
|
|
|
ft->size_mul = 1;
|
2007-08-09 04:12:48 +03:00
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (stream_id = 0; stream_id < s->nb_streams; stream_id++) {
|
|
|
|
int start2 = start + (end - start) * stream_id / s->nb_streams;
|
|
|
|
int end2 = start + (end - start) * (stream_id + 1) / s->nb_streams;
|
2016-04-10 21:58:15 +02:00
|
|
|
AVCodecParameters *par = s->streams[stream_id]->codecpar;
|
|
|
|
int is_audio = par->codec_type == AVMEDIA_TYPE_AUDIO;
|
2012-10-27 01:41:02 +03:00
|
|
|
int intra_only = /*codec->intra_only || */ is_audio;
|
2007-08-09 04:12:48 +03:00
|
|
|
int pred_count;
|
2012-10-02 00:51:35 +03:00
|
|
|
int frame_size = 0;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
frame_size = av_get_audio_frame_duration2(par, 0);
|
|
|
|
if (par->codec_id == AV_CODEC_ID_VORBIS && !frame_size)
|
2012-10-02 00:51:35 +03:00
|
|
|
frame_size = 64;
|
|
|
|
} else {
|
2016-04-10 21:58:15 +02:00
|
|
|
AVRational f = av_div_q(av_inv_q(s->streams[stream_id]->avg_frame_rate), *nut->stream[stream_id].time_base);
|
2012-10-28 16:04:43 +03:00
|
|
|
if (f.den == 1 && f.num>0)
|
2012-10-02 00:51:35 +03:00
|
|
|
frame_size = f.num;
|
2012-10-01 22:56:27 +03:00
|
|
|
}
|
2012-10-28 16:04:43 +03:00
|
|
|
if (!frame_size)
|
2012-10-01 22:56:27 +03:00
|
|
|
frame_size = 1;
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (key_frame = 0; key_frame < 2; key_frame++) {
|
|
|
|
if (!intra_only || !keyframe_0_esc || key_frame != 0) {
|
|
|
|
FrameCode *ft = &nut->frame_code[start2];
|
|
|
|
ft->flags = FLAG_KEY * key_frame;
|
|
|
|
ft->flags |= FLAG_SIZE_MSB | FLAG_CODED_PTS;
|
|
|
|
ft->stream_id = stream_id;
|
|
|
|
ft->size_mul = 1;
|
|
|
|
if (is_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
|
|
|
ft->header_idx = find_header_idx(s, par, -1, key_frame);
|
2007-08-09 04:12:48 +03:00
|
|
|
start2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
key_frame = intra_only;
|
2011-04-27 05:09:35 +03:00
|
|
|
#if 1
|
2012-10-27 01:41:02 +03:00
|
|
|
if (is_audio) {
|
2016-02-21 11:55:18 +02:00
|
|
|
int frame_bytes;
|
2007-08-09 04:12:48 +03:00
|
|
|
int pts;
|
2016-02-21 11:55:18 +02: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->block_align > 0) {
|
|
|
|
frame_bytes = par->block_align;
|
2016-02-21 11:55:18 +02:00
|
|
|
} 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
|
|
|
int frame_size = av_get_audio_frame_duration2(par, 0);
|
|
|
|
frame_bytes = frame_size * (int64_t)par->bit_rate / (8 * par->sample_rate);
|
2016-02-21 11:55:18 +02:00
|
|
|
}
|
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
for (pts = 0; pts < 2; pts++) {
|
2012-10-27 01:41:02 +03:00
|
|
|
for (pred = 0; pred < 2; pred++) {
|
2012-10-28 16:04:43 +03:00
|
|
|
FrameCode *ft = &nut->frame_code[start2];
|
2012-10-27 01:41:02 +03:00
|
|
|
ft->flags = FLAG_KEY * key_frame;
|
|
|
|
ft->stream_id = stream_id;
|
|
|
|
ft->size_mul = frame_bytes + 2;
|
|
|
|
ft->size_lsb = frame_bytes + pred;
|
2012-10-28 16:04:43 +03:00
|
|
|
ft->pts_delta = pts * frame_size;
|
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
|
|
|
ft->header_idx = find_header_idx(s, par, frame_bytes + pred, key_frame);
|
2007-08-09 04:12:48 +03:00
|
|
|
start2++;
|
|
|
|
}
|
|
|
|
}
|
2012-10-27 01:41:02 +03:00
|
|
|
} else {
|
|
|
|
FrameCode *ft = &nut->frame_code[start2];
|
|
|
|
ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
|
|
|
|
ft->stream_id = stream_id;
|
|
|
|
ft->size_mul = 1;
|
2012-10-28 16:04:43 +03:00
|
|
|
ft->pts_delta = frame_size;
|
2007-08-09 04:12:48 +03:00
|
|
|
start2++;
|
|
|
|
}
|
2011-04-27 05:09:35 +03:00
|
|
|
#endif
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->video_delay) {
|
2012-10-27 01:41:02 +03:00
|
|
|
pred_count = 5;
|
|
|
|
pred_table[0] = -2;
|
|
|
|
pred_table[1] = -1;
|
|
|
|
pred_table[2] = 1;
|
|
|
|
pred_table[3] = 3;
|
|
|
|
pred_table[4] = 4;
|
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
|
|
|
} else if (par->codec_id == AV_CODEC_ID_VORBIS) {
|
2012-10-27 01:41:02 +03:00
|
|
|
pred_count = 3;
|
|
|
|
pred_table[0] = 2;
|
|
|
|
pred_table[1] = 9;
|
|
|
|
pred_table[2] = 16;
|
|
|
|
} else {
|
|
|
|
pred_count = 1;
|
|
|
|
pred_table[0] = 1;
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (pred = 0; pred < pred_count; pred++) {
|
|
|
|
int start3 = start2 + (end2 - start2) * pred / pred_count;
|
|
|
|
int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-01 22:56:27 +03:00
|
|
|
pred_table[pred] *= frame_size;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (index = start3; index < end3; index++) {
|
|
|
|
FrameCode *ft = &nut->frame_code[index];
|
|
|
|
ft->flags = FLAG_KEY * key_frame;
|
|
|
|
ft->flags |= FLAG_SIZE_MSB;
|
|
|
|
ft->stream_id = stream_id;
|
2007-08-09 04:12:48 +03:00
|
|
|
//FIXME use single byte size and pred from last
|
2012-10-27 01:41:02 +03:00
|
|
|
ft->size_mul = end3 - start3;
|
|
|
|
ft->size_lsb = index - start3;
|
|
|
|
ft->pts_delta = pred_table[pred];
|
|
|
|
if (is_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
|
|
|
ft->header_idx = find_header_idx(s, par, -1, key_frame);
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-28 16:04:43 +03:00
|
|
|
memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->frame_code[0].flags =
|
|
|
|
nut->frame_code[255].flags =
|
|
|
|
nut->frame_code['N'].flags = FLAG_INVALID;
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
/**
|
|
|
|
* Get the length in bytes which is needed to store val as v.
|
|
|
|
*/
|
|
|
|
static int get_v_length(uint64_t val)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
while (val >>= 7)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put val using a variable number of bytes.
|
|
|
|
*/
|
|
|
|
static void put_v(AVIOContext *bc, uint64_t val)
|
|
|
|
{
|
|
|
|
int i = get_v_length(val);
|
|
|
|
|
|
|
|
while (--i > 0)
|
|
|
|
avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
|
|
|
|
|
|
|
|
avio_w8(bc, val & 127);
|
|
|
|
}
|
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
|
2012-10-27 01:41:02 +03:00
|
|
|
{
|
2007-08-09 23:56:42 +03:00
|
|
|
val *= nut->time_base_count;
|
2011-03-16 07:42:43 +02:00
|
|
|
val += time_base - nut->time_base;
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, val);
|
2007-08-09 23:56:42 +03:00
|
|
|
}
|
2007-08-09 04:12:48 +03:00
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Store a string as vb.
|
2007-08-09 04:12:48 +03:00
|
|
|
*/
|
2012-10-27 01:41:02 +03:00
|
|
|
static void put_str(AVIOContext *bc, const char *string)
|
|
|
|
{
|
2015-05-10 16:26:58 +02:00
|
|
|
size_t len = strlen(string);
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, len);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, string, len);
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static void put_s(AVIOContext *bc, int64_t val)
|
|
|
|
{
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 2 * FFABS(val) - (val > 0));
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
|
2020-05-04 11:13:58 +02:00
|
|
|
uint64_t startcode)
|
2012-10-27 01:41:02 +03:00
|
|
|
{
|
|
|
|
uint8_t *dyn_buf = NULL;
|
2020-05-04 12:46:45 +02:00
|
|
|
int dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
|
2020-05-04 11:13:58 +02:00
|
|
|
int forw_ptr = dyn_size + 4;
|
2007-08-09 15:48:52 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (forw_ptr > 4096)
|
2011-03-17 13:56:25 +02:00
|
|
|
ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_wb64(bc, startcode);
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, forw_ptr);
|
2012-10-27 01:41:02 +03:00
|
|
|
if (forw_ptr > 4096)
|
2011-03-17 14:04:38 +02:00
|
|
|
avio_wl32(bc, ffio_get_checksum(bc));
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2020-05-04 11:13:58 +02:00
|
|
|
ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, dyn_buf, dyn_size);
|
2020-05-04 11:13:58 +02:00
|
|
|
avio_wl32(bc, ffio_get_checksum(bc));
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2020-05-04 12:46:45 +02:00
|
|
|
ffio_reset_dyn_buf(dyn_bc);
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static void write_mainheader(NUTContext *nut, AVIOContext *bc)
|
|
|
|
{
|
|
|
|
int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
|
|
|
|
tmp_head_idx;
|
2008-02-15 04:57:19 +02:00
|
|
|
int64_t tmp_match;
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->version);
|
2013-12-23 05:25:25 +03:00
|
|
|
if (nut->version > 3)
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->minor_version = 1);
|
|
|
|
put_v(bc, nut->avf->nb_streams);
|
|
|
|
put_v(bc, nut->max_distance);
|
|
|
|
put_v(bc, nut->time_base_count);
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 0; i < nut->time_base_count; i++) {
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->time_base[i].num);
|
|
|
|
put_v(bc, nut->time_base[i].den);
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
tmp_pts = 0;
|
|
|
|
tmp_mul = 1;
|
|
|
|
tmp_stream = 0;
|
|
|
|
tmp_match = 1 - (1LL << 62);
|
|
|
|
tmp_head_idx = 0;
|
|
|
|
for (i = 0; i < 256; ) {
|
|
|
|
tmp_fields = 0;
|
|
|
|
tmp_size = 0;
|
2007-08-09 04:12:48 +03:00
|
|
|
// tmp_res=0;
|
2012-10-28 16:04:43 +03:00
|
|
|
if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
|
|
|
|
if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
|
|
|
|
if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
|
|
|
|
if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4;
|
|
|
|
// if (tmp_res != nut->frame_code[i].res ) tmp_fields=5;
|
|
|
|
if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
|
2012-10-27 01:41:02 +03:00
|
|
|
|
|
|
|
tmp_pts = nut->frame_code[i].pts_delta;
|
|
|
|
tmp_flags = nut->frame_code[i].flags;
|
|
|
|
tmp_stream = nut->frame_code[i].stream_id;
|
|
|
|
tmp_mul = nut->frame_code[i].size_mul;
|
|
|
|
tmp_size = nut->frame_code[i].size_lsb;
|
2007-08-09 04:12:48 +03:00
|
|
|
// tmp_res = nut->frame_code[i].res;
|
2012-10-27 01:41:02 +03:00
|
|
|
tmp_head_idx = nut->frame_code[i].header_idx;
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (j = 0; i < 256; j++, i++) {
|
|
|
|
if (i == 'N') {
|
2007-08-09 04:12:48 +03:00
|
|
|
j--;
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-27 01:41:02 +03:00
|
|
|
if (nut->frame_code[i].pts_delta != tmp_pts ||
|
|
|
|
nut->frame_code[i].flags != tmp_flags ||
|
|
|
|
nut->frame_code[i].stream_id != tmp_stream ||
|
|
|
|
nut->frame_code[i].size_mul != tmp_mul ||
|
|
|
|
nut->frame_code[i].size_lsb != tmp_size + j ||
|
|
|
|
// nut->frame_code[i].res != tmp_res ||
|
|
|
|
nut->frame_code[i].header_idx != tmp_head_idx)
|
|
|
|
break;
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
2012-10-27 01:41:02 +03:00
|
|
|
if (j != tmp_mul - tmp_size)
|
|
|
|
tmp_fields = 6;
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, tmp_flags);
|
|
|
|
put_v(bc, tmp_fields);
|
2012-10-28 16:04:43 +03:00
|
|
|
if (tmp_fields > 0) put_s(bc, tmp_pts);
|
2020-04-09 17:41:50 +02:00
|
|
|
if (tmp_fields > 1) put_v(bc, tmp_mul);
|
|
|
|
if (tmp_fields > 2) put_v(bc, tmp_stream);
|
|
|
|
if (tmp_fields > 3) put_v(bc, tmp_size);
|
|
|
|
if (tmp_fields > 4) put_v(bc, 0 /*tmp_res*/);
|
|
|
|
if (tmp_fields > 5) put_v(bc, j);
|
|
|
|
if (tmp_fields > 6) put_v(bc, tmp_match);
|
|
|
|
if (tmp_fields > 7) put_v(bc, tmp_head_idx);
|
2008-02-15 04:57:19 +02:00
|
|
|
}
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->header_count - 1);
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 1; i < nut->header_count; i++) {
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->header_len[i]);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, nut->header[i], nut->header_len[i]);
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
2014-03-06 19:58:34 +03:00
|
|
|
// flags had been effectively introduced in version 4
|
2014-05-29 02:41:56 +03:00
|
|
|
if (nut->version > 3)
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->flags);
|
2007-08-09 15:22:02 +03:00
|
|
|
}
|
2007-08-09 04:12:48 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc,
|
|
|
|
AVStream *st, int i)
|
|
|
|
{
|
|
|
|
NUTContext *nut = avctx->priv_data;
|
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
|
|
|
AVCodecParameters *par = st->codecpar;
|
2012-10-12 16:49:36 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, i);
|
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) {
|
2020-04-09 17:41:50 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO: put_v(bc, 0); break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO: put_v(bc, 1); break;
|
|
|
|
case AVMEDIA_TYPE_SUBTITLE: put_v(bc, 2); break;
|
|
|
|
default: put_v(bc, 3); break;
|
2007-08-09 15:25:58 +03:00
|
|
|
}
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 4);
|
2012-10-12 16:49:36 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_tag) {
|
|
|
|
avio_wl32(bc, par->codec_tag);
|
2010-06-03 00:57:40 +03:00
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
|
2010-06-03 00:57:37 +03:00
|
|
|
return AVERROR(EINVAL);
|
2010-06-03 00:57:40 +03:00
|
|
|
}
|
2007-08-09 15:25:58 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->stream[i].time_base - nut->time_base);
|
|
|
|
put_v(bc, nut->stream[i].msb_pts_shift);
|
|
|
|
put_v(bc, nut->stream[i].max_pts_distance);
|
|
|
|
put_v(bc, par->video_delay);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
|
2007-08-09 15:25:58 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, par->extradata_size);
|
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
|
|
|
avio_write(bc, par->extradata, par->extradata_size);
|
2007-08-09 15:25:58 +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
|
|
|
switch (par->codec_type) {
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, par->sample_rate);
|
|
|
|
put_v(bc, 1);
|
2017-03-31 17:52:39 +02:00
|
|
|
put_v(bc, par->ch_layout.nb_channels);
|
2007-08-09 15:25:58 +03:00
|
|
|
break;
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, par->width);
|
|
|
|
put_v(bc, par->height);
|
2007-08-10 17:23:18 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (st->sample_aspect_ratio.num <= 0 ||
|
|
|
|
st->sample_aspect_ratio.den <= 0) {
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 0);
|
|
|
|
put_v(bc, 0);
|
2012-10-27 01:41:02 +03:00
|
|
|
} else {
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, st->sample_aspect_ratio.num);
|
|
|
|
put_v(bc, st->sample_aspect_ratio.den);
|
2007-08-10 17:23:18 +03:00
|
|
|
}
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 0); /* csp type -- unknown */
|
2007-08-09 15:25:58 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int add_info(AVIOContext *bc, const char *type, const char *value)
|
|
|
|
{
|
2007-08-10 14:52:28 +03:00
|
|
|
put_str(bc, type);
|
|
|
|
put_s(bc, -1);
|
|
|
|
put_str(bc, value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
2011-05-22 13:46:29 +03:00
|
|
|
AVDictionaryEntry *t = NULL;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *dyn_bc;
|
2012-10-27 01:41:02 +03:00
|
|
|
uint8_t *dyn_buf = NULL;
|
|
|
|
int count = 0, dyn_size;
|
|
|
|
int ret = avio_open_dyn_buf(&dyn_bc);
|
|
|
|
if (ret < 0)
|
2007-11-21 09:41:00 +02:00
|
|
|
return ret;
|
2007-08-10 14:52:28 +03:00
|
|
|
|
2016-02-28 04:11:34 +02:00
|
|
|
ff_standardize_creation_time(s);
|
2011-05-22 13:46:29 +03:00
|
|
|
while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
|
2010-02-24 08:27:12 +02:00
|
|
|
count += add_info(dyn_bc, t->key, t->value);
|
2007-08-10 14:52:28 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 0); //stream_if_plus1
|
|
|
|
put_v(bc, 0); //chapter_id
|
|
|
|
put_v(bc, 0); //timestamp_start
|
|
|
|
put_v(bc, 0); //length
|
2007-08-10 14:52:28 +03:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, count);
|
2007-08-10 14:52:28 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, dyn_buf, dyn_size);
|
2007-08-10 14:52:28 +03:00
|
|
|
av_free(dyn_buf);
|
2007-11-21 09:41:00 +02:00
|
|
|
return 0;
|
2007-08-10 14:52:28 +03:00
|
|
|
}
|
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
|
2008-03-07 21:25:09 +02:00
|
|
|
AVFormatContext *s= nut->avf;
|
|
|
|
AVStream* st = s->streams[stream_id];
|
2012-08-19 01:30:30 +03:00
|
|
|
AVDictionaryEntry *t = NULL;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *dyn_bc;
|
2008-03-07 21:25:09 +02:00
|
|
|
uint8_t *dyn_buf=NULL;
|
|
|
|
int count=0, dyn_size, i;
|
2011-03-17 09:13:34 +02:00
|
|
|
int ret = avio_open_dyn_buf(&dyn_bc);
|
2012-10-28 16:04:43 +03:00
|
|
|
if (ret < 0)
|
2008-03-07 21:25:09 +02:00
|
|
|
return ret;
|
|
|
|
|
2012-08-19 01:30:30 +03:00
|
|
|
while ((t = av_dict_get(st->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
|
|
|
|
count += add_info(dyn_bc, t->key, t->value);
|
2008-03-07 21:25:09 +02:00
|
|
|
for (i=0; ff_nut_dispositions[i].flag; ++i) {
|
|
|
|
if (st->disposition & ff_nut_dispositions[i].flag)
|
|
|
|
count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2012-10-02 01:41:40 +03:00
|
|
|
uint8_t buf[256];
|
2014-12-14 02:12:27 +02:00
|
|
|
if (st->r_frame_rate.num>0 && st->r_frame_rate.den>0)
|
|
|
|
snprintf(buf, sizeof(buf), "%d/%d", st->r_frame_rate.num, st->r_frame_rate.den);
|
|
|
|
else
|
2016-04-10 21:58:15 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%d/%d", st->avg_frame_rate.num, st->avg_frame_rate.den);
|
2012-10-02 01:41:40 +03:00
|
|
|
count += add_info(dyn_bc, "r_frame_rate", buf);
|
|
|
|
}
|
2011-03-17 09:16:07 +02:00
|
|
|
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
|
2008-03-07 21:25:09 +02:00
|
|
|
|
|
|
|
if (count) {
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, stream_id + 1); //stream_id_plus1
|
|
|
|
put_v(bc, 0); //chapter_id
|
|
|
|
put_v(bc, 0); //timestamp_start
|
|
|
|
put_v(bc, 0); //length
|
2008-03-07 21:25:09 +02:00
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, count);
|
2008-03-07 21:25:09 +02:00
|
|
|
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, dyn_buf, dyn_size);
|
2008-03-07 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free(dyn_buf);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-03-16 07:42:43 +02:00
|
|
|
static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
|
|
|
|
{
|
|
|
|
AVIOContext *dyn_bc;
|
2012-10-27 01:41:02 +03:00
|
|
|
uint8_t *dyn_buf = NULL;
|
2011-05-22 13:46:29 +03:00
|
|
|
AVDictionaryEntry *t = NULL;
|
2012-10-27 01:41:02 +03:00
|
|
|
AVChapter *ch = nut->avf->chapters[id];
|
2011-03-16 07:42:43 +02:00
|
|
|
int ret, dyn_size, count = 0;
|
|
|
|
|
2011-03-17 09:13:34 +02:00
|
|
|
ret = avio_open_dyn_buf(&dyn_bc);
|
2011-03-16 07:42:43 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 0); // stream_id_plus1
|
2011-03-16 07:42:43 +02:00
|
|
|
put_s(bc, id + 1); // chapter_id
|
|
|
|
put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, ch->end - ch->start); // chapter_len
|
2011-03-16 07:42:43 +02:00
|
|
|
|
2011-05-22 13:46:29 +03:00
|
|
|
while ((t = av_dict_get(ch->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
|
2011-03-16 07:42:43 +02:00
|
|
|
count += add_info(dyn_bc, t->key, t->value);
|
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, count);
|
2011-03-16 07:42:43 +02:00
|
|
|
|
2011-03-17 09:16:07 +02:00
|
|
|
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
|
2011-03-16 07:42:43 +02:00
|
|
|
avio_write(bc, dyn_buf, dyn_size);
|
|
|
|
av_freep(&dyn_buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
static int write_index(NUTContext *nut, AVIOContext *bc) {
|
2012-08-20 00:36:26 +03:00
|
|
|
int i;
|
|
|
|
Syncpoint dummy= { .pos= 0 };
|
|
|
|
Syncpoint *next_node[2] = { NULL };
|
|
|
|
int64_t startpos = avio_tell(bc);
|
|
|
|
int64_t payload_size;
|
|
|
|
|
|
|
|
put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
|
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nut->sp_count);
|
2012-08-20 00:36:26 +03:00
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
for (i=0; i<nut->sp_count; i++) {
|
2015-10-24 23:53:21 +02:00
|
|
|
av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, (void**)next_node);
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
|
2012-08-20 00:36:26 +03:00
|
|
|
dummy.pos = next_node[1]->pos;
|
|
|
|
}
|
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
for (i=0; i<nut->avf->nb_streams; i++) {
|
2012-08-20 00:36:26 +03:00
|
|
|
StreamContext *nus= &nut->stream[i];
|
|
|
|
int64_t last_pts= -1;
|
|
|
|
int j, k;
|
2012-10-28 16:04:43 +03:00
|
|
|
for (j=0; j<nut->sp_count; j++) {
|
2013-12-22 20:47:28 +03:00
|
|
|
int flag;
|
2012-08-20 00:36:26 +03:00
|
|
|
int n = 0;
|
2013-12-22 20:47:28 +03:00
|
|
|
|
|
|
|
if (j && nus->keyframe_pts[j] == nus->keyframe_pts[j-1]) {
|
|
|
|
av_log(nut->avf, AV_LOG_WARNING, "Multiple keyframes with same PTS\n");
|
|
|
|
nus->keyframe_pts[j] = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
|
2012-10-28 16:04:43 +03:00
|
|
|
for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
|
2012-08-20 00:36:26 +03:00
|
|
|
n++;
|
|
|
|
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, 1 + 2 * flag + 4 * n);
|
2012-10-28 16:04:43 +03:00
|
|
|
for (k= j - n; k<=j && k<nut->sp_count; k++) {
|
|
|
|
if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
|
2012-08-20 00:36:26 +03:00
|
|
|
continue;
|
|
|
|
av_assert0(nus->keyframe_pts[k] > last_pts);
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, nus->keyframe_pts[k] - last_pts);
|
2012-08-20 00:36:26 +03:00
|
|
|
last_pts = nus->keyframe_pts[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
payload_size = avio_tell(bc) - startpos + 8 + 4;
|
|
|
|
|
|
|
|
avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
|
|
|
|
{
|
2010-06-03 00:57:30 +03:00
|
|
|
NUTContext *nut = avctx->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *dyn_bc;
|
2007-11-21 09:41:00 +02:00
|
|
|
int i, ret;
|
2007-08-10 00:10:11 +03:00
|
|
|
|
2010-10-15 22:04:25 +03:00
|
|
|
ff_metadata_conv_ctx(avctx, ff_nut_metadata_conv, NULL);
|
|
|
|
|
2011-03-17 09:13:34 +02:00
|
|
|
ret = avio_open_dyn_buf(&dyn_bc);
|
2012-10-27 01:41:02 +03:00
|
|
|
if (ret < 0)
|
2007-11-21 09:41:00 +02:00
|
|
|
return ret;
|
|
|
|
write_mainheader(nut, dyn_bc);
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, MAIN_STARTCODE);
|
2007-08-10 00:10:11 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 0; i < nut->avf->nb_streams; i++) {
|
|
|
|
ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
|
2019-10-19 19:37:27 +02:00
|
|
|
if (ret < 0) {
|
2020-05-04 13:06:41 +02:00
|
|
|
goto fail;
|
2019-10-19 19:37:27 +02:00
|
|
|
}
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, STREAM_STARTCODE);
|
2007-08-10 00:10:11 +03:00
|
|
|
}
|
2007-08-10 14:52:28 +03:00
|
|
|
|
2007-11-21 09:41:00 +02:00
|
|
|
write_globalinfo(nut, dyn_bc);
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
|
2007-08-10 15:59:17 +03:00
|
|
|
|
2008-03-07 21:25:09 +02:00
|
|
|
for (i = 0; i < nut->avf->nb_streams; i++) {
|
|
|
|
ret = write_streaminfo(nut, dyn_bc, i);
|
|
|
|
if (ret > 0)
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
|
2020-05-04 12:46:45 +02:00
|
|
|
else if (ret < 0) {
|
2020-05-04 13:06:41 +02:00
|
|
|
goto fail;
|
2019-10-19 19:37:27 +02:00
|
|
|
}
|
2008-03-07 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 07:42:43 +02:00
|
|
|
for (i = 0; i < nut->avf->nb_chapters; i++) {
|
|
|
|
ret = write_chapter(nut, dyn_bc, i);
|
|
|
|
if (ret < 0) {
|
2020-05-04 13:06:41 +02:00
|
|
|
goto fail;
|
2011-03-16 07:42:43 +02:00
|
|
|
}
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
|
2011-03-16 07:42:43 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->last_syncpoint_pos = INT_MIN;
|
2007-08-10 15:59:17 +03:00
|
|
|
nut->header_count++;
|
2020-05-04 12:46:45 +02:00
|
|
|
|
2020-05-04 13:06:41 +02:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
2020-05-04 12:46:45 +02:00
|
|
|
ffio_free_dyn_buf(&dyn_bc);
|
|
|
|
|
2020-05-04 13:06:41 +02:00
|
|
|
return ret;
|
2007-08-10 00:10:11 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int nut_write_header(AVFormatContext *s)
|
|
|
|
{
|
2007-08-09 15:22:02 +03:00
|
|
|
NUTContext *nut = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *bc = s->pb;
|
2010-06-03 00:57:35 +03:00
|
|
|
int i, j, ret;
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->avf = s;
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2014-05-29 02:37:46 +03:00
|
|
|
nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags);
|
2014-06-10 17:36:41 +03:00
|
|
|
if (nut->version > 3 && s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
|
2014-03-06 19:58:34 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"The additional syncpoint modes require version %d, "
|
|
|
|
"that is currently not finalized, "
|
|
|
|
"please set -f_strict experimental in order to enable it.\n",
|
|
|
|
nut->version);
|
|
|
|
return AVERROR_EXPERIMENTAL;
|
|
|
|
}
|
|
|
|
|
2013-09-14 17:23:00 +03:00
|
|
|
nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
|
|
|
|
nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
|
|
|
|
nut->time_base= av_calloc(s->nb_streams +
|
|
|
|
s->nb_chapters, sizeof(*nut->time_base));
|
2020-03-14 18:32:17 +02:00
|
|
|
if (!nut->stream || !nut->chapter || !nut->time_base)
|
2011-05-03 15:19:42 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
AVStream *st = s->streams[i];
|
2007-08-09 15:22:02 +03:00
|
|
|
int ssize;
|
|
|
|
AVRational time_base;
|
2014-06-18 18:47:12 +03:00
|
|
|
ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num);
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) {
|
|
|
|
time_base = (AVRational) {1, st->codecpar->sample_rate};
|
2012-10-02 00:51:35 +03:00
|
|
|
} else {
|
2012-10-03 22:34:43 +03:00
|
|
|
time_base = ff_choose_timebase(s, st, 48000);
|
2012-10-01 22:56:27 +03:00
|
|
|
}
|
|
|
|
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (j = 0; j < nut->time_base_count; j++)
|
|
|
|
if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) {
|
2007-08-09 15:22:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->time_base[j] = time_base;
|
|
|
|
nut->stream[i].time_base = &nut->time_base[j];
|
|
|
|
if (j == nut->time_base_count)
|
2007-08-09 15:22:02 +03:00
|
|
|
nut->time_base_count++;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (INT64_C(1000) * time_base.num >= time_base.den)
|
2007-08-09 15:22:02 +03:00
|
|
|
nut->stream[i].msb_pts_shift = 7;
|
|
|
|
else
|
|
|
|
nut->stream[i].msb_pts_shift = 14;
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->stream[i].max_pts_distance =
|
|
|
|
FFMAX(time_base.den, time_base.num) / time_base.num;
|
2007-08-09 15:22:02 +03:00
|
|
|
}
|
|
|
|
|
2011-03-16 07:42:43 +02:00
|
|
|
for (i = 0; i < s->nb_chapters; i++) {
|
|
|
|
AVChapter *ch = s->chapters[i];
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (j = 0; j < nut->time_base_count; j++)
|
2011-03-16 07:42:43 +02:00
|
|
|
if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
|
|
|
|
break;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->time_base[j] = ch->time_base;
|
2011-03-16 07:42:43 +02:00
|
|
|
nut->chapter[i].time_base = &nut->time_base[j];
|
2012-10-27 01:41:02 +03:00
|
|
|
if (j == nut->time_base_count)
|
2011-03-16 07:42:43 +02:00
|
|
|
nut->time_base_count++;
|
|
|
|
}
|
|
|
|
|
2008-02-04 12:34:35 +02:00
|
|
|
nut->max_distance = MAX_DISTANCE;
|
2008-02-15 04:57:19 +02:00
|
|
|
build_elision_headers(s);
|
2007-08-09 15:22:02 +03:00
|
|
|
build_frame_code(s);
|
2012-06-13 19:51:50 +03:00
|
|
|
av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, ID_STRING, strlen(ID_STRING));
|
|
|
|
avio_w8(bc, 0);
|
2007-08-09 15:22:02 +03:00
|
|
|
|
2010-06-03 00:57:35 +03:00
|
|
|
if ((ret = write_headers(s, bc)) < 0)
|
|
|
|
return ret;
|
2007-08-09 13:45:19 +03:00
|
|
|
|
2012-09-26 16:56:06 +03:00
|
|
|
if (s->avoid_negative_ts < 0)
|
|
|
|
s->avoid_negative_ts = 1;
|
|
|
|
|
2007-08-09 04:12:48 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (pkt->flags & AV_PKT_FLAG_KEY)
|
|
|
|
flags |= FLAG_KEY;
|
|
|
|
if (pkt->stream_index != fc->stream_id)
|
|
|
|
flags |= FLAG_STREAM_ID;
|
|
|
|
if (pkt->size / fc->size_mul)
|
|
|
|
flags |= FLAG_SIZE_MSB;
|
|
|
|
if (pkt->pts - nus->last_pts != fc->pts_delta)
|
|
|
|
flags |= FLAG_CODED_PTS;
|
2013-12-23 04:58:36 +03:00
|
|
|
if (pkt->side_data_elems && nut->version > 3)
|
|
|
|
flags |= FLAG_SM_DATA;
|
2012-10-27 01:41:02 +03:00
|
|
|
if (pkt->size > 2 * nut->max_distance)
|
|
|
|
flags |= FLAG_CHECKSUM;
|
|
|
|
if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
|
|
|
|
flags |= FLAG_CHECKSUM;
|
2019-11-01 11:02:29 +02:00
|
|
|
if (fc->header_idx)
|
|
|
|
if (pkt->size < nut->header_len[fc->header_idx] ||
|
|
|
|
pkt->size > 4096 ||
|
|
|
|
memcmp(pkt->data, nut->header [fc->header_idx],
|
|
|
|
nut->header_len[fc->header_idx]))
|
|
|
|
flags |= FLAG_HEADER_IDX;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2007-08-11 14:10:29 +03:00
|
|
|
return flags | (fc->flags & FLAG_CODED);
|
2007-08-10 00:01:22 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
|
|
|
|
{
|
2008-02-15 04:57:19 +02:00
|
|
|
int i;
|
2012-10-27 01:41:02 +03:00
|
|
|
int best_i = 0;
|
|
|
|
int best_len = 0;
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (pkt->size > 4096)
|
2008-02-15 04:57:19 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 1; i < nut->header_count; i++)
|
|
|
|
if (pkt->size >= nut->header_len[i]
|
|
|
|
&& nut->header_len[i] > best_len
|
|
|
|
&& !memcmp(pkt->data, nut->header[i], nut->header_len[i])) {
|
|
|
|
best_i = i;
|
|
|
|
best_len = nut->header_len[i];
|
2008-02-15 04:57:19 +02:00
|
|
|
}
|
|
|
|
return best_i;
|
|
|
|
}
|
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
|
|
|
|
{
|
|
|
|
int ret, i, dyn_size;
|
|
|
|
unsigned flags;
|
|
|
|
AVIOContext *dyn_bc;
|
|
|
|
int sm_data_count = 0;
|
|
|
|
uint8_t tmp[256];
|
|
|
|
uint8_t *dyn_buf;
|
|
|
|
|
|
|
|
ret = avio_open_dyn_buf(&dyn_bc);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i<pkt->side_data_elems; i++) {
|
|
|
|
const uint8_t *data = pkt->side_data[i].data;
|
|
|
|
int size = pkt->side_data[i].size;
|
|
|
|
const uint8_t *data_end = data + size;
|
|
|
|
|
|
|
|
if (is_meta) {
|
|
|
|
if ( pkt->side_data[i].type == AV_PKT_DATA_METADATA_UPDATE
|
|
|
|
|| pkt->side_data[i].type == AV_PKT_DATA_STRINGS_METADATA) {
|
2014-06-10 02:40:32 +03:00
|
|
|
if (!size || data[size-1]) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-12-23 04:58:36 +03:00
|
|
|
while (data < data_end) {
|
|
|
|
const uint8_t *key = data;
|
|
|
|
const uint8_t *val = data + strlen(key) + 1;
|
|
|
|
|
2014-06-10 02:40:32 +03:00
|
|
|
if(val >= data_end) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-12-23 04:58:36 +03:00
|
|
|
put_str(dyn_bc, key);
|
|
|
|
put_s(dyn_bc, -1);
|
|
|
|
put_str(dyn_bc, val);
|
|
|
|
data = val + strlen(val) + 1;
|
|
|
|
sm_data_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (pkt->side_data[i].type) {
|
|
|
|
case AV_PKT_DATA_PALETTE:
|
|
|
|
case AV_PKT_DATA_NEW_EXTRADATA:
|
|
|
|
case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL:
|
|
|
|
default:
|
|
|
|
if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
|
|
|
|
put_str(dyn_bc, "Palette");
|
|
|
|
} else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) {
|
|
|
|
put_str(dyn_bc, "Extradata");
|
|
|
|
} else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
|
|
|
|
snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data));
|
|
|
|
put_str(dyn_bc, tmp);
|
|
|
|
} else {
|
|
|
|
snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d",
|
2014-05-18 05:02:44 +03:00
|
|
|
(s->flags & AVFMT_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT,
|
2013-12-23 04:58:36 +03:00
|
|
|
pkt->side_data[i].type);
|
|
|
|
put_str(dyn_bc, tmp);
|
|
|
|
}
|
|
|
|
put_s(dyn_bc, -2);
|
|
|
|
put_str(dyn_bc, "bin");
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(dyn_bc, pkt->side_data[i].size);
|
2013-12-23 04:58:36 +03:00
|
|
|
avio_write(dyn_bc, data, pkt->side_data[i].size);
|
|
|
|
sm_data_count++;
|
|
|
|
break;
|
|
|
|
case AV_PKT_DATA_PARAM_CHANGE:
|
|
|
|
flags = bytestream_get_le32(&data);
|
2013-06-05 08:02:53 +03:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
2013-12-23 04:58:36 +03:00
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
|
|
|
|
put_str(dyn_bc, "Channels");
|
|
|
|
put_s(dyn_bc, bytestream_get_le32(&data));
|
|
|
|
sm_data_count++;
|
|
|
|
}
|
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
|
|
|
|
put_str(dyn_bc, "ChannelLayout");
|
|
|
|
put_s(dyn_bc, -2);
|
|
|
|
put_str(dyn_bc, "u64");
|
2020-05-04 18:49:22 +02:00
|
|
|
put_v(dyn_bc, 8);
|
2013-12-23 04:58:36 +03:00
|
|
|
avio_write(dyn_bc, data, 8); data+=8;
|
|
|
|
sm_data_count++;
|
|
|
|
}
|
2013-06-05 08:02:53 +03:00
|
|
|
#endif
|
2013-12-23 04:58:36 +03:00
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
|
|
|
|
put_str(dyn_bc, "SampleRate");
|
|
|
|
put_s(dyn_bc, bytestream_get_le32(&data));
|
|
|
|
sm_data_count++;
|
|
|
|
}
|
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
|
|
|
|
put_str(dyn_bc, "Width");
|
|
|
|
put_s(dyn_bc, bytestream_get_le32(&data));
|
|
|
|
put_str(dyn_bc, "Height");
|
|
|
|
put_s(dyn_bc, bytestream_get_le32(&data));
|
|
|
|
sm_data_count+=2;
|
|
|
|
}
|
2014-01-24 06:17:15 +03:00
|
|
|
break;
|
2013-12-23 04:58:36 +03:00
|
|
|
case AV_PKT_DATA_SKIP_SAMPLES:
|
|
|
|
if (AV_RL32(data)) {
|
|
|
|
put_str(dyn_bc, "SkipStart");
|
|
|
|
put_s(dyn_bc, (unsigned)AV_RL32(data));
|
|
|
|
sm_data_count++;
|
|
|
|
}
|
|
|
|
if (AV_RL32(data+4)) {
|
|
|
|
put_str(dyn_bc, "SkipEnd");
|
|
|
|
put_s(dyn_bc, (unsigned)AV_RL32(data+4));
|
|
|
|
sm_data_count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_PKT_DATA_METADATA_UPDATE:
|
|
|
|
case AV_PKT_DATA_STRINGS_METADATA:
|
2015-07-25 18:39:17 +02:00
|
|
|
case AV_PKT_DATA_QUALITY_STATS:
|
2013-12-23 04:58:36 +03:00
|
|
|
// belongs into meta, not side data
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:40:32 +03:00
|
|
|
fail:
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, sm_data_count);
|
2013-12-23 04:58:36 +03:00
|
|
|
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
|
|
|
|
avio_write(bc, dyn_buf, dyn_size);
|
|
|
|
av_freep(&dyn_buf);
|
|
|
|
|
2014-06-10 02:40:32 +03:00
|
|
|
return ret;
|
2013-12-23 04:58:36 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
StreamContext *nus = &nut->stream[pkt->stream_index];
|
2013-12-23 04:58:36 +03:00
|
|
|
AVIOContext *bc = s->pb, *dyn_bc, *sm_bc = NULL;
|
2007-08-10 00:01:22 +03:00
|
|
|
FrameCode *fc;
|
|
|
|
int64_t coded_pts;
|
2012-10-28 16:04:43 +03:00
|
|
|
int best_length, frame_code, flags, needed_flags, i, header_idx;
|
|
|
|
int best_header_idx;
|
2010-03-31 15:29:58 +03:00
|
|
|
int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
|
2012-10-27 01:41:02 +03:00
|
|
|
int store_sp = 0;
|
2014-06-10 16:41:33 +03:00
|
|
|
int ret = 0;
|
2013-12-23 04:58:36 +03:00
|
|
|
int sm_size = 0;
|
|
|
|
int data_size = pkt->size;
|
2014-06-10 16:41:33 +03:00
|
|
|
uint8_t *sm_buf = NULL;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-07-08 14:43:47 +03:00
|
|
|
if (pkt->pts < 0) {
|
2012-10-27 17:56:37 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Negative pts not supported stream %d, pts %"PRId64"\n",
|
|
|
|
pkt->stream_index, pkt->pts);
|
2014-05-30 18:53:36 +03:00
|
|
|
if (pkt->pts == AV_NOPTS_VALUE)
|
|
|
|
av_log(s, AV_LOG_ERROR, "Try to enable the genpts flag\n");
|
2012-07-08 14:43:47 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2008-06-26 01:37:26 +03:00
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
if (pkt->side_data_elems && nut->version > 3) {
|
|
|
|
ret = avio_open_dyn_buf(&sm_bc);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-06-10 16:41:33 +03:00
|
|
|
ret = write_sm_data(s, sm_bc, pkt, 0);
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = write_sm_data(s, sm_bc, pkt, 1);
|
2013-12-23 04:58:36 +03:00
|
|
|
sm_size = avio_close_dyn_buf(sm_bc, &sm_buf);
|
2014-06-10 16:41:33 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2013-12-23 04:58:36 +03:00
|
|
|
data_size += sm_size;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
|
2010-06-03 00:57:30 +03:00
|
|
|
write_headers(s, bc);
|
2007-08-10 15:59:17 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (key_frame && !(nus->last_flags & FLAG_KEY))
|
|
|
|
store_sp = 1;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
|
2012-10-27 01:41:02 +03:00
|
|
|
store_sp = 1;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2007-08-17 13:45:50 +03:00
|
|
|
//FIXME: Ensure store_sp is 1 in the first place.
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
if (store_sp &&
|
|
|
|
(!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) {
|
2014-05-29 04:20:04 +03:00
|
|
|
int64_t sp_pos = INT64_MAX;
|
2007-08-10 02:29:58 +03:00
|
|
|
|
2007-08-10 00:01:22 +03:00
|
|
|
ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
|
2012-10-27 01:41:02 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
AVStream *st = s->streams[i];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2008-02-04 12:48:41 +02:00
|
|
|
int64_t dts_tb = av_rescale_rnd(pkt->dts,
|
|
|
|
nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
|
|
|
|
nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
|
|
|
|
AV_ROUND_DOWN);
|
2012-10-27 01:41:02 +03:00
|
|
|
int index = av_index_search_timestamp(st, dts_tb,
|
|
|
|
AVSEEK_FLAG_BACKWARD);
|
2014-05-29 04:48:02 +03:00
|
|
|
if (index >= 0) {
|
2021-08-24 19:41:16 +02:00
|
|
|
sp_pos = FFMIN(sp_pos, sti->index_entries[index].pos);
|
|
|
|
if (!nut->write_index && 2*index > sti->nb_index_entries) {
|
|
|
|
memmove(sti->index_entries,
|
|
|
|
sti->index_entries + index,
|
|
|
|
sizeof(*sti->index_entries) * (sti->nb_index_entries - index));
|
|
|
|
sti->nb_index_entries -= index;
|
2014-05-29 04:48:02 +03:00
|
|
|
}
|
|
|
|
}
|
2007-08-10 02:29:58 +03:00
|
|
|
}
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
nut->last_syncpoint_pos = avio_tell(bc);
|
|
|
|
ret = avio_open_dyn_buf(&dyn_bc);
|
|
|
|
if (ret < 0)
|
2014-06-10 16:41:33 +03:00
|
|
|
goto fail;
|
2011-03-16 07:42:43 +02:00
|
|
|
put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(dyn_bc, sp_pos != INT64_MAX ? (nut->last_syncpoint_pos - sp_pos) >> 4 : 0);
|
2014-03-06 19:58:34 +03:00
|
|
|
|
|
|
|
if (nut->flags & NUT_BROADCAST) {
|
|
|
|
put_tt(nut, nus->time_base, dyn_bc,
|
|
|
|
av_rescale_q(av_gettime(), AV_TIME_BASE_Q, *nus->time_base));
|
|
|
|
}
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, SYNCPOINT_STARTCODE);
|
2020-05-04 12:46:45 +02:00
|
|
|
ffio_free_dyn_buf(&dyn_bc);
|
2007-08-10 02:29:58 +03:00
|
|
|
|
2014-05-29 04:32:44 +03:00
|
|
|
if (nut->write_index) {
|
2013-10-22 18:11:11 +03:00
|
|
|
if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, pkt->dts)) < 0)
|
2014-06-10 16:41:33 +03:00
|
|
|
goto fail;
|
2012-08-20 00:33:47 +03:00
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
if ((1ll<<60) % nut->sp_count == 0)
|
|
|
|
for (i=0; i<s->nb_streams; i++) {
|
2012-08-20 00:33:47 +03:00
|
|
|
int j;
|
|
|
|
StreamContext *nus = &nut->stream[i];
|
2013-09-14 17:27:15 +03:00
|
|
|
av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
|
2014-06-10 16:41:33 +03:00
|
|
|
if (!nus->keyframe_pts) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-28 16:04:43 +03:00
|
|
|
for (j=nut->sp_count == 1 ? 0 : nut->sp_count; j<2*nut->sp_count; j++)
|
2012-08-20 00:33:47 +03:00
|
|
|
nus->keyframe_pts[j] = AV_NOPTS_VALUE;
|
|
|
|
}
|
2014-05-29 04:32:44 +03:00
|
|
|
}
|
2007-08-10 00:01:22 +03:00
|
|
|
}
|
2012-06-13 19:51:50 +03:00
|
|
|
av_assert0(nus->last_pts != AV_NOPTS_VALUE);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
|
|
|
|
if (ff_lsb2full(nus, coded_pts) != pkt->pts)
|
|
|
|
coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
best_header_idx = find_best_header_idx(nut, pkt);
|
2008-02-15 04:57:19 +02:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
best_length = INT_MAX;
|
|
|
|
frame_code = -1;
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
int length = 0;
|
|
|
|
FrameCode *fc = &nut->frame_code[i];
|
|
|
|
int flags = fc->flags;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_INVALID)
|
2007-08-10 00:01:22 +03:00
|
|
|
continue;
|
2012-10-27 01:41:02 +03:00
|
|
|
needed_flags = get_needed_flags(nut, nus, fc, pkt);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_CODED) {
|
2007-08-10 00:01:22 +03:00
|
|
|
length++;
|
2007-08-11 14:12:34 +03:00
|
|
|
flags = needed_flags;
|
2007-08-10 00:01:22 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if ((flags & needed_flags) != needed_flags)
|
2007-08-10 00:01:22 +03:00
|
|
|
continue;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if ((flags ^ needed_flags) & FLAG_KEY)
|
2007-08-10 00:01:22 +03:00
|
|
|
continue;
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_STREAM_ID)
|
2020-04-09 17:41:50 +02:00
|
|
|
length += get_v_length(pkt->stream_index);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
if (data_size % fc->size_mul != fc->size_lsb)
|
2007-08-10 00:01:22 +03:00
|
|
|
continue;
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_SIZE_MSB)
|
2020-04-09 17:41:50 +02:00
|
|
|
length += get_v_length(data_size / fc->size_mul);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_CHECKSUM)
|
|
|
|
length += 4;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_CODED_PTS)
|
2020-04-09 17:41:50 +02:00
|
|
|
length += get_v_length(coded_pts);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
if ( (flags & FLAG_CODED)
|
|
|
|
&& nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) {
|
2008-02-15 04:57:19 +02:00
|
|
|
flags |= FLAG_HEADER_IDX;
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_HEADER_IDX) {
|
2008-02-15 04:57:19 +02:00
|
|
|
length += 1 - nut->header_len[best_header_idx];
|
2012-10-27 01:41:02 +03:00
|
|
|
} else {
|
2008-02-15 04:57:19 +02:00
|
|
|
length -= nut->header_len[fc->header_idx];
|
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
length *= 4;
|
|
|
|
length += !(flags & FLAG_CODED_PTS);
|
|
|
|
length += !(flags & FLAG_CHECKSUM);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
if (length < best_length) {
|
|
|
|
best_length = length;
|
|
|
|
frame_code = i;
|
2007-08-10 00:01:22 +03:00
|
|
|
}
|
|
|
|
}
|
2012-06-15 20:38:03 +03:00
|
|
|
av_assert0(frame_code != -1);
|
2014-10-20 16:11:20 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
fc = &nut->frame_code[frame_code];
|
|
|
|
flags = fc->flags;
|
|
|
|
needed_flags = get_needed_flags(nut, nus, fc, pkt);
|
|
|
|
header_idx = fc->header_idx;
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2011-03-17 13:56:25 +02:00
|
|
|
ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_w8(bc, frame_code);
|
2012-10-27 01:41:02 +03:00
|
|
|
if (flags & FLAG_CODED) {
|
2020-04-09 17:41:50 +02:00
|
|
|
put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
|
2007-08-10 00:01:22 +03:00
|
|
|
flags = needed_flags;
|
|
|
|
}
|
2020-04-09 17:41:50 +02:00
|
|
|
if (flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index);
|
|
|
|
if (flags & FLAG_CODED_PTS) put_v(bc, coded_pts);
|
|
|
|
if (flags & FLAG_SIZE_MSB ) put_v(bc, data_size / fc->size_mul);
|
|
|
|
if (flags & FLAG_HEADER_IDX) put_v(bc, header_idx = best_header_idx);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
|
|
|
|
else ffio_get_checksum(bc);
|
2007-08-10 00:01:22 +03:00
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
if (flags & FLAG_SM_DATA) {
|
|
|
|
avio_write(bc, sm_buf, sm_size);
|
|
|
|
}
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
|
2013-12-23 04:58:36 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
nus->last_flags = flags;
|
|
|
|
nus->last_pts = pkt->pts;
|
2007-08-10 02:29:58 +03:00
|
|
|
|
|
|
|
//FIXME just store one per syncpoint
|
2014-05-29 02:09:51 +03:00
|
|
|
if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) {
|
2007-08-10 02:29:58 +03:00
|
|
|
av_add_index_entry(
|
|
|
|
s->streams[pkt->stream_index],
|
|
|
|
nut->last_syncpoint_pos,
|
|
|
|
pkt->pts,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
AVINDEX_KEYFRAME);
|
2012-10-28 16:04:43 +03:00
|
|
|
if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
|
2012-08-20 00:33:47 +03:00
|
|
|
nus->keyframe_pts[nut->sp_count] = pkt->pts;
|
|
|
|
}
|
2007-08-10 02:29:58 +03:00
|
|
|
|
2012-10-28 16:04:43 +03:00
|
|
|
if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
|
2012-08-20 00:34:38 +03:00
|
|
|
nut->max_pts = pkt->pts;
|
|
|
|
nut->max_pts_tb = nus->time_base;
|
|
|
|
}
|
|
|
|
|
2014-06-10 16:41:33 +03:00
|
|
|
fail:
|
|
|
|
av_freep(&sm_buf);
|
|
|
|
|
|
|
|
return ret;
|
2007-08-09 04:12:48 +03:00
|
|
|
}
|
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
static int nut_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
2012-08-20 00:36:26 +03:00
|
|
|
AVIOContext *bc = s->pb, *dyn_bc;
|
2016-01-21 14:07:07 +02:00
|
|
|
int ret;
|
2007-08-10 00:14:00 +03:00
|
|
|
|
2012-10-27 01:41:02 +03:00
|
|
|
while (nut->header_count < 3)
|
2010-06-03 00:57:30 +03:00
|
|
|
write_headers(s, bc);
|
2012-08-20 00:36:26 +03:00
|
|
|
|
2019-10-19 01:53:10 +02:00
|
|
|
if (!nut->sp_count)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-20 00:36:26 +03:00
|
|
|
ret = avio_open_dyn_buf(&dyn_bc);
|
2019-10-19 01:53:10 +02:00
|
|
|
if (ret >= 0) {
|
2018-12-14 22:52:09 +02:00
|
|
|
av_assert1(nut->write_index); // sp_count should be 0 if no index is going to be written
|
2012-08-20 00:36:26 +03:00
|
|
|
write_index(nut, dyn_bc);
|
2020-05-04 11:13:58 +02:00
|
|
|
put_packet(nut, bc, dyn_bc, INDEX_STARTCODE);
|
2020-05-04 12:46:45 +02:00
|
|
|
ffio_free_dyn_buf(&dyn_bc);
|
2012-08-20 00:36:26 +03:00
|
|
|
}
|
|
|
|
|
2016-01-21 14:07:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nut_write_deinit(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
int i;
|
|
|
|
|
2010-03-03 19:31:24 +02:00
|
|
|
ff_nut_free_sp(nut);
|
2016-01-21 14:07:07 +02:00
|
|
|
if (nut->stream)
|
|
|
|
for (i=0; i<s->nb_streams; i++)
|
|
|
|
av_freep(&nut->stream[i].keyframe_pts);
|
2012-08-20 00:33:47 +03:00
|
|
|
|
2010-02-28 22:49:21 +02:00
|
|
|
av_freep(&nut->stream);
|
2011-03-17 18:00:21 +02:00
|
|
|
av_freep(&nut->chapter);
|
2010-02-28 22:49:21 +02:00
|
|
|
av_freep(&nut->time_base);
|
2007-08-10 00:14:00 +03:00
|
|
|
}
|
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
#define OFFSET(x) offsetof(NUTContext, x)
|
|
|
|
#define E AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "syncpoints", "NUT syncpoint behaviour", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
|
|
|
|
{ "default", "", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
|
|
|
|
{ "none", "Disable syncpoints, low overhead and unseekable", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_PIPE}, INT_MIN, INT_MAX, E, "syncpoints" },
|
|
|
|
{ "timestamped", "Extend syncpoints with a wallclock timestamp", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_BROADCAST}, INT_MIN, INT_MAX, E, "syncpoints" },
|
2015-11-21 23:05:07 +02:00
|
|
|
{ "write_index", "Write index", OFFSET(write_index), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E, },
|
2014-03-06 19:58:34 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass class = {
|
|
|
|
.class_name = "nutenc",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVOutputFormat ff_nut_muxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "nut",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("NUT"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.mime_type = "video/x-nut",
|
|
|
|
.extensions = "nut",
|
|
|
|
.priv_data_size = sizeof(NUTContext),
|
2012-08-05 12:11:04 +03:00
|
|
|
.audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
|
|
|
|
CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
|
|
|
|
.video_codec = AV_CODEC_ID_MPEG4,
|
2011-08-31 20:27:17 +03:00
|
|
|
.write_header = nut_write_header,
|
|
|
|
.write_packet = nut_write_packet,
|
|
|
|
.write_trailer = nut_write_trailer,
|
2016-01-21 14:07:07 +02:00
|
|
|
.deinit = nut_write_deinit,
|
2012-04-06 17:50:48 +03:00
|
|
|
.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
|
2012-10-12 16:49:36 +03:00
|
|
|
.codec_tag = ff_nut_codec_tags,
|
2014-03-06 19:58:34 +03:00
|
|
|
.priv_class = &class,
|
2007-08-09 04:12:48 +03:00
|
|
|
};
|