2006-11-11 03:35:50 +02:00
|
|
|
/*
|
|
|
|
* "NUT" Container Format demuxer
|
|
|
|
* Copyright (c) 2004-2006 Michael Niedermayer
|
|
|
|
* Copyright (c) 2003 Alex Beregszaszi
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/avstring.h"
|
2012-04-28 22:11:38 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/bswap.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2013-12-23 04:58:36 +03: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"
|
2013-12-23 04:58:36 +03:00
|
|
|
#include "libavcodec/bytestream.h"
|
2011-03-14 21:38:57 +02:00
|
|
|
#include "avio_internal.h"
|
2022-05-06 18:28:08 +02:00
|
|
|
#include "demux.h"
|
2014-07-08 17:26:23 +03:00
|
|
|
#include "isom.h"
|
2006-11-11 03:35:50 +02:00
|
|
|
#include "nut.h"
|
2012-11-27 19:52:28 +03:00
|
|
|
#include "riff.h"
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2010-08-10 19:36:36 +03:00
|
|
|
#define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */
|
|
|
|
|
2012-06-30 18:19:43 +03:00
|
|
|
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t *pos_arg, int64_t pos_limit);
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
|
|
|
|
{
|
|
|
|
unsigned int len = ffio_read_varlen(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (len && maxlen)
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_read(bc, string, FFMIN(len, maxlen));
|
2011-12-02 19:14:05 +03:00
|
|
|
while (len > maxlen) {
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
len--;
|
2015-05-20 00:31:24 +02:00
|
|
|
if (bc->eof_reached)
|
|
|
|
len = maxlen;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (maxlen)
|
|
|
|
string[FFMIN(len, maxlen - 1)] = 0;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2015-05-20 17:13:15 +02:00
|
|
|
if (bc->eof_reached)
|
|
|
|
return AVERROR_EOF;
|
2011-12-02 19:14:05 +03:00
|
|
|
if (maxlen == len)
|
2006-11-11 03:35:50 +02:00
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int64_t get_s(AVIOContext *bc)
|
|
|
|
{
|
2011-03-14 21:38:57 +02:00
|
|
|
int64_t v = ffio_read_varlen(bc) + 1;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (v & 1)
|
|
|
|
return -(v >> 1);
|
|
|
|
else
|
|
|
|
return (v >> 1);
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static uint64_t get_fourcc(AVIOContext *bc)
|
|
|
|
{
|
|
|
|
unsigned int len = ffio_read_varlen(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (len == 2)
|
|
|
|
return avio_rl16(bc);
|
|
|
|
else if (len == 4)
|
|
|
|
return avio_rl32(bc);
|
2013-03-04 18:49:17 +03:00
|
|
|
else {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Unsupported fourcc length %d\n", len);
|
2011-12-02 19:14:05 +03:00
|
|
|
return -1;
|
2013-03-04 18:49:17 +03:00
|
|
|
}
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int get_packetheader(NUTContext *nut, AVIOContext *bc,
|
|
|
|
int calculate_checksum, uint64_t startcode)
|
2006-11-11 03:35:50 +02:00
|
|
|
{
|
2007-04-24 08:50:30 +03:00
|
|
|
int64_t size;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
startcode = av_be2ne64(startcode);
|
|
|
|
startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
|
2007-08-10 00:35:13 +03:00
|
|
|
|
2011-03-17 13:56:25 +02:00
|
|
|
ffio_init_checksum(bc, ff_crc04C11DB7_update, startcode);
|
2011-12-02 19:14:05 +03:00
|
|
|
size = ffio_read_varlen(bc);
|
|
|
|
if (size > 4096)
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_rb32(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (ffio_get_checksum(bc) && size > 4096)
|
2007-08-09 15:51:08 +03:00
|
|
|
return -1;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-03-17 13:56:25 +02:00
|
|
|
ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
|
|
|
|
{
|
|
|
|
uint64_t state = 0;
|
|
|
|
|
|
|
|
if (pos >= 0)
|
|
|
|
/* Note, this may fail if the stream is not seekable, but that should
|
|
|
|
* not matter, as in this case we simply start where we currently are */
|
|
|
|
avio_seek(bc, pos, SEEK_SET);
|
2014-08-07 23:12:41 +03:00
|
|
|
while (!avio_feof(bc)) {
|
2011-12-02 19:14:05 +03:00
|
|
|
state = (state << 8) | avio_r8(bc);
|
|
|
|
if ((state >> 56) != 'N')
|
2006-11-11 03:35:50 +02:00
|
|
|
continue;
|
2011-12-02 19:14:05 +03:00
|
|
|
switch (state) {
|
2006-11-11 03:35:50 +02:00
|
|
|
case MAIN_STARTCODE:
|
|
|
|
case STREAM_STARTCODE:
|
|
|
|
case SYNCPOINT_STARTCODE:
|
|
|
|
case INFO_STARTCODE:
|
|
|
|
case INDEX_STARTCODE:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-08-17 13:45:50 +03:00
|
|
|
* Find the given startcode.
|
2006-11-11 03:35:50 +02:00
|
|
|
* @param code the startcode
|
|
|
|
* @param pos the start position of the search, or -1 if the current position
|
2010-03-30 18:50:57 +03:00
|
|
|
* @return the position of the startcode or -1 if not found
|
2006-11-11 03:35:50 +02:00
|
|
|
*/
|
2011-12-02 19:14:05 +03:00
|
|
|
static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
uint64_t startcode = find_any_startcode(bc, pos);
|
|
|
|
if (startcode == code)
|
2011-03-03 21:11:45 +02:00
|
|
|
return avio_tell(bc) - 8;
|
2011-12-02 19:14:05 +03:00
|
|
|
else if (startcode == 0)
|
2006-11-11 03:35:50 +02:00
|
|
|
return -1;
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = -1;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int nut_probe(const AVProbeData *p)
|
2011-12-02 19:14:05 +03:00
|
|
|
{
|
2006-11-11 03:35:50 +02:00
|
|
|
int i;
|
|
|
|
|
2014-06-24 19:32:14 +03:00
|
|
|
for (i = 0; i < p->buf_size-8; i++) {
|
|
|
|
if (AV_RB32(p->buf+i) != MAIN_STARTCODE>>32)
|
|
|
|
continue;
|
|
|
|
if (AV_RB32(p->buf+i+4) == (MAIN_STARTCODE & 0xFFFFFFFF))
|
2006-11-11 03:35:50 +02:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-02 05:13:23 +03:00
|
|
|
#define GET_V(dst, check) \
|
|
|
|
do { \
|
|
|
|
tmp = ffio_read_varlen(bc); \
|
|
|
|
if (!(check)) { \
|
|
|
|
av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA; \
|
|
|
|
goto fail; \
|
2012-10-02 05:13:23 +03:00
|
|
|
} \
|
|
|
|
dst = tmp; \
|
|
|
|
} while (0)
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int skip_reserved(AVIOContext *bc, int64_t pos)
|
|
|
|
{
|
2011-03-03 21:11:45 +02:00
|
|
|
pos -= avio_tell(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (pos < 0) {
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(bc, pos, SEEK_CUR);
|
2013-05-06 12:39:30 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-02 19:14:05 +03:00
|
|
|
} else {
|
2015-05-20 00:31:24 +02:00
|
|
|
while (pos--) {
|
|
|
|
if (bc->eof_reached)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(bc);
|
2015-05-20 00:31:24 +02:00
|
|
|
}
|
2006-11-11 03:35:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int decode_main_header(NUTContext *nut)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2020-12-19 01:00:40 +02:00
|
|
|
uint64_t tmp, end, length;
|
2006-11-11 03:35:50 +02:00
|
|
|
unsigned int stream_count;
|
2015-05-22 23:02:07 +02:00
|
|
|
int i, j, count, ret;
|
2011-12-02 19:14:05 +03:00
|
|
|
int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2020-12-19 01:00:40 +02:00
|
|
|
length = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
|
2022-07-06 23:54:49 +02:00
|
|
|
if (length == (uint64_t)-1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2020-12-19 01:00:40 +02:00
|
|
|
end = length + avio_tell(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
nut->version = ffio_read_varlen(bc);
|
2017-05-02 08:43:12 +02:00
|
|
|
if (nut->version < NUT_MIN_VERSION ||
|
2014-07-07 21:48:34 +03:00
|
|
|
nut->version > NUT_MAX_VERSION) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
|
|
|
|
nut->version);
|
2013-05-06 12:39:30 +03:00
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
2013-12-23 05:25:25 +03:00
|
|
|
if (nut->version > 3)
|
|
|
|
nut->minor_version = ffio_read_varlen(bc);
|
2013-05-06 12:39:30 +03:00
|
|
|
|
2012-10-02 05:13:23 +03:00
|
|
|
GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-03-14 21:38:57 +02:00
|
|
|
nut->max_distance = ffio_read_varlen(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (nut->max_distance > 65536) {
|
2006-11-11 03:35:50 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
|
2011-12-02 19:14:05 +03:00
|
|
|
nut->max_distance = 65536;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-19 01:00:40 +02:00
|
|
|
GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational) && tmp < length/2);
|
2015-03-30 04:32:58 +02:00
|
|
|
nut->time_base = av_malloc_array(nut->time_base_count, sizeof(AVRational));
|
2013-09-11 14:30:15 +03:00
|
|
|
if (!nut->time_base)
|
|
|
|
return AVERROR(ENOMEM);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
for (i = 0; i < nut->time_base_count; i++) {
|
2012-10-02 05:13:23 +03:00
|
|
|
GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
|
|
|
|
GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
|
2011-12-02 19:14:05 +03:00
|
|
|
if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
|
2015-11-24 02:24:47 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n",
|
|
|
|
nut->time_base[i].num,
|
|
|
|
nut->time_base[i].den);
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
tmp_pts = 0;
|
|
|
|
tmp_mul = 1;
|
|
|
|
tmp_stream = 0;
|
|
|
|
tmp_head_idx = 0;
|
|
|
|
for (i = 0; i < 256;) {
|
|
|
|
int tmp_flags = ffio_read_varlen(bc);
|
|
|
|
int tmp_fields = ffio_read_varlen(bc);
|
2022-09-18 14:47:25 +02:00
|
|
|
if (tmp_fields < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "fields %d is invalid\n", tmp_fields);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
|
|
|
|
if (tmp_fields > 0)
|
|
|
|
tmp_pts = get_s(bc);
|
|
|
|
if (tmp_fields > 1)
|
|
|
|
tmp_mul = ffio_read_varlen(bc);
|
|
|
|
if (tmp_fields > 2)
|
|
|
|
tmp_stream = ffio_read_varlen(bc);
|
|
|
|
if (tmp_fields > 3)
|
|
|
|
tmp_size = ffio_read_varlen(bc);
|
|
|
|
else
|
|
|
|
tmp_size = 0;
|
|
|
|
if (tmp_fields > 4)
|
|
|
|
tmp_res = ffio_read_varlen(bc);
|
|
|
|
else
|
|
|
|
tmp_res = 0;
|
|
|
|
if (tmp_fields > 5)
|
|
|
|
count = ffio_read_varlen(bc);
|
|
|
|
else
|
2021-01-16 23:44:33 +02:00
|
|
|
count = tmp_mul - (unsigned)tmp_size;
|
2011-12-02 19:14:05 +03:00
|
|
|
if (tmp_fields > 6)
|
|
|
|
get_s(bc);
|
|
|
|
if (tmp_fields > 7)
|
|
|
|
tmp_head_idx = ffio_read_varlen(bc);
|
|
|
|
|
2015-05-20 00:31:24 +02:00
|
|
|
while (tmp_fields-- > 8) {
|
|
|
|
if (bc->eof_reached) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "reached EOF while decoding main header\n");
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2015-05-20 00:31:24 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
ffio_read_varlen(bc);
|
2015-05-20 00:31:24 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
|
2015-04-28 22:37:19 +02:00
|
|
|
if (count <= 0 || count > 256 - (i <= 'N') - i) {
|
2006-11-11 03:35:50 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
if (tmp_stream >= stream_count) {
|
2015-11-24 02:24:47 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n",
|
|
|
|
tmp_stream, stream_count);
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2021-04-25 20:01:03 +02:00
|
|
|
}
|
|
|
|
if (tmp_size < 0 || tmp_size > INT_MAX - count) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "illegal size\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
for (j = 0; j < count; j++, i++) {
|
2006-11-11 03:35:50 +02:00
|
|
|
if (i == 'N') {
|
2011-12-02 19:14:05 +03:00
|
|
|
nut->frame_code[i].flags = FLAG_INVALID;
|
2006-11-11 03:35:50 +02:00
|
|
|
j--;
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
nut->frame_code[i].flags = tmp_flags;
|
|
|
|
nut->frame_code[i].pts_delta = tmp_pts;
|
|
|
|
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].reserved_count = tmp_res;
|
|
|
|
nut->frame_code[i].header_idx = tmp_head_idx;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-28 22:11:38 +03:00
|
|
|
av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (end > avio_tell(bc) + 4) {
|
|
|
|
int rem = 1024;
|
2012-10-02 05:13:23 +03:00
|
|
|
GET_V(nut->header_count, tmp < 128U);
|
2008-02-15 04:36:09 +02:00
|
|
|
nut->header_count++;
|
2011-12-02 19:14:05 +03:00
|
|
|
for (i = 1; i < nut->header_count; i++) {
|
2012-04-18 22:48:36 +03:00
|
|
|
uint8_t *hdr;
|
2011-12-02 19:14:05 +03:00
|
|
|
GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
|
2015-11-24 02:24:47 +02:00
|
|
|
if (rem < nut->header_len[i]) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"invalid elision header %d : %d > %d\n",
|
|
|
|
i, nut->header_len[i], rem);
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2008-02-15 04:36:09 +02:00
|
|
|
}
|
2015-11-24 02:24:47 +02:00
|
|
|
rem -= nut->header_len[i];
|
2012-04-18 22:48:36 +03:00
|
|
|
hdr = av_malloc(nut->header_len[i]);
|
2015-05-22 23:02:07 +02:00
|
|
|
if (!hdr) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-04-18 22:48:36 +03:00
|
|
|
avio_read(bc, hdr, nut->header_len[i]);
|
|
|
|
nut->header[i] = hdr;
|
2008-02-15 04:36:09 +02:00
|
|
|
}
|
2012-04-28 22:11:38 +03:00
|
|
|
av_assert0(nut->header_len[0] == 0);
|
2008-02-15 04:36:09 +02:00
|
|
|
}
|
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
// flags had been effectively introduced in version 4
|
2014-05-29 02:48:02 +03:00
|
|
|
if (nut->version > 3 && end > avio_tell(bc) + 4) {
|
2014-03-06 19:58:34 +03:00
|
|
|
nut->flags = ffio_read_varlen(bc);
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
|
2007-08-17 13:45:50 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 14:30:15 +03:00
|
|
|
nut->stream = av_calloc(stream_count, sizeof(StreamContext));
|
2015-05-22 23:02:07 +02:00
|
|
|
if (!nut->stream) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-02-23 04:31:59 +02:00
|
|
|
for (i = 0; i < stream_count; i++) {
|
|
|
|
if (!avformat_new_stream(s, NULL)) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2006-11-11 03:35:50 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-05-22 23:02:07 +02:00
|
|
|
fail:
|
|
|
|
av_freep(&nut->time_base);
|
|
|
|
for (i = 1; i < nut->header_count; i++) {
|
|
|
|
av_freep(&nut->header[i]);
|
|
|
|
}
|
|
|
|
nut->header_count = 0;
|
|
|
|
return ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int decode_stream_header(NUTContext *nut)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2006-11-11 03:35:50 +02:00
|
|
|
StreamContext *stc;
|
2015-05-22 23:02:07 +02:00
|
|
|
int class, stream_id, ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
uint64_t tmp, end;
|
2015-05-22 23:02:07 +02:00
|
|
|
AVStream *st = NULL;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
|
2011-03-03 21:11:45 +02:00
|
|
|
end += avio_tell(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2007-08-09 14:04:43 +03:00
|
|
|
GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
|
2011-12-02 19:14:05 +03:00
|
|
|
stc = &nut->stream[stream_id];
|
|
|
|
st = s->streams[stream_id];
|
2006-11-11 03:35:50 +02:00
|
|
|
if (!st)
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
class = ffio_read_varlen(bc);
|
|
|
|
tmp = get_fourcc(bc);
|
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
|
|
|
st->codecpar->codec_tag = tmp;
|
2011-12-02 19:14:05 +03:00
|
|
|
switch (class) {
|
|
|
|
case 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
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
|
2011-12-02 19:14:05 +03:00
|
|
|
ff_nut_video_tags,
|
2012-10-12 16:49:36 +03:00
|
|
|
ff_codec_bmp_tags,
|
2014-07-08 17:26:23 +03:00
|
|
|
ff_codec_movvideo_tags,
|
2011-12-02 19:14:05 +03:00
|
|
|
0
|
|
|
|
},
|
|
|
|
tmp);
|
|
|
|
break;
|
|
|
|
case 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
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
|
2012-10-12 18:29:43 +03:00
|
|
|
ff_nut_audio_tags,
|
|
|
|
ff_codec_wav_tags,
|
2014-04-10 00:37:55 +03:00
|
|
|
ff_nut_audio_extra_tags,
|
2012-10-12 18:29:43 +03:00
|
|
|
0
|
|
|
|
},
|
|
|
|
tmp);
|
2011-12-02 19:14:05 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
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
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
|
|
|
|
st->codecpar->codec_id = ff_codec_get_id(ff_nut_subtitle_tags, tmp);
|
2011-12-02 19:14:05 +03:00
|
|
|
break;
|
|
|
|
case 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
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
|
|
|
|
st->codecpar->codec_id = ff_codec_get_id(ff_nut_data_tags, tmp);
|
2011-12-02 19:14:05 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
|
2013-05-06 12:39:30 +03:00
|
|
|
return AVERROR(ENOSYS);
|
2006-11-11 03:35:50 +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 (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE)
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Unknown codec tag '0x%04x' for stream number %d\n",
|
|
|
|
(unsigned int) tmp, stream_id);
|
|
|
|
|
|
|
|
GET_V(stc->time_base_id, tmp < nut->time_base_count);
|
|
|
|
GET_V(stc->msb_pts_shift, tmp < 16);
|
|
|
|
stc->max_pts_distance = ffio_read_varlen(bc);
|
|
|
|
GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->video_delay = stc->decode_delay;
|
2011-12-02 19:14:05 +03:00
|
|
|
ffio_read_varlen(bc); // stream flags
|
|
|
|
|
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
|
|
|
GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
|
|
|
|
if (st->codecpar->extradata_size) {
|
2019-12-10 23:59:53 +02:00
|
|
|
ret = ff_get_extradata(s, st->codecpar, bc,
|
|
|
|
st->codecpar->extradata_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-11-11 03:35:50 +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 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
GET_V(st->codecpar->width, tmp > 0);
|
|
|
|
GET_V(st->codecpar->height, tmp > 0);
|
2011-12-02 19:14:05 +03:00
|
|
|
st->sample_aspect_ratio.num = ffio_read_varlen(bc);
|
|
|
|
st->sample_aspect_ratio.den = ffio_read_varlen(bc);
|
|
|
|
if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
|
|
|
|
st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2011-03-14 21:38:57 +02:00
|
|
|
ffio_read_varlen(bc); /* csp type */
|
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 (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
GET_V(st->codecpar->sample_rate, tmp > 0);
|
2011-03-14 21:38:57 +02:00
|
|
|
ffio_read_varlen(bc); // samplerate_den
|
2017-03-31 17:52:39 +02:00
|
|
|
GET_V(st->codecpar->ch_layout.nb_channels, tmp > 0);
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"stream header %d checksum mismatch\n", stream_id);
|
2015-05-22 23:02:07 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
stc->time_base = &nut->time_base[stc->time_base_id];
|
|
|
|
avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
|
|
|
|
stc->time_base->den);
|
2006-11-11 03:35:50 +02:00
|
|
|
return 0;
|
2015-05-22 23:02:07 +02:00
|
|
|
fail:
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st && st->codecpar) {
|
|
|
|
av_freep(&st->codecpar->extradata);
|
|
|
|
st->codecpar->extradata_size = 0;
|
2015-05-22 23:02:07 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static void set_disposition_bits(AVFormatContext *avf, char *value,
|
|
|
|
int stream_id)
|
|
|
|
{
|
2008-03-07 21:25:09 +02:00
|
|
|
int flag = 0, i;
|
2011-12-02 19:14:05 +03:00
|
|
|
|
|
|
|
for (i = 0; ff_nut_dispositions[i].flag; ++i)
|
2008-03-07 21:25:09 +02:00
|
|
|
if (!strcmp(ff_nut_dispositions[i].str, value))
|
|
|
|
flag = ff_nut_dispositions[i].flag;
|
|
|
|
if (!flag)
|
|
|
|
av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
|
|
|
|
for (i = 0; i < avf->nb_streams; ++i)
|
|
|
|
if (stream_id == i || stream_id == -1)
|
|
|
|
avf->streams[i]->disposition |= flag;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int decode_info_header(NUTContext *nut)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2010-03-01 09:26:41 +02:00
|
|
|
uint64_t tmp, chapter_start, chapter_len;
|
|
|
|
unsigned int stream_id_plus1, count;
|
2021-03-16 09:29:59 +02:00
|
|
|
int i, ret = 0;
|
|
|
|
int64_t chapter_id, value, end;
|
2008-02-04 02:31:31 +02:00
|
|
|
char name[256], str_value[1024], type_str[256];
|
2008-02-04 02:33:52 +02:00
|
|
|
const char *type;
|
2014-10-18 18:58:47 +03:00
|
|
|
int *event_flags = NULL;
|
2011-12-02 19:14:05 +03:00
|
|
|
AVChapter *chapter = NULL;
|
|
|
|
AVStream *st = NULL;
|
2011-05-22 13:46:29 +03:00
|
|
|
AVDictionary **metadata = NULL;
|
2014-08-13 00:03:53 +03:00
|
|
|
int metadata_flag = 0;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
|
2011-03-03 21:11:45 +02:00
|
|
|
end += avio_tell(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2012-10-02 05:13:23 +03:00
|
|
|
GET_V(stream_id_plus1, tmp <= s->nb_streams);
|
2011-12-02 19:14:05 +03:00
|
|
|
chapter_id = get_s(bc);
|
|
|
|
chapter_start = ffio_read_varlen(bc);
|
|
|
|
chapter_len = ffio_read_varlen(bc);
|
|
|
|
count = ffio_read_varlen(bc);
|
|
|
|
|
|
|
|
if (chapter_id && !stream_id_plus1) {
|
|
|
|
int64_t start = chapter_start / nut->time_base_count;
|
|
|
|
chapter = avpriv_new_chapter(s, chapter_id,
|
|
|
|
nut->time_base[chapter_start %
|
|
|
|
nut->time_base_count],
|
|
|
|
start, start + chapter_len, NULL);
|
2015-04-28 20:57:59 +02:00
|
|
|
if (!chapter) {
|
2015-04-28 20:57:59 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
|
2015-04-28 20:57:59 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2010-10-24 10:28:04 +03:00
|
|
|
metadata = &chapter->metadata;
|
2011-12-02 19:14:05 +03:00
|
|
|
} else if (stream_id_plus1) {
|
|
|
|
st = s->streams[stream_id_plus1 - 1];
|
2010-10-24 10:28:04 +03:00
|
|
|
metadata = &st->metadata;
|
2014-08-13 00:03:53 +03:00
|
|
|
event_flags = &st->event_flags;
|
|
|
|
metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
|
|
|
|
} else {
|
2010-10-24 10:28:04 +03:00
|
|
|
metadata = &s->metadata;
|
2014-08-13 00:03:53 +03:00
|
|
|
event_flags = &s->event_flags;
|
|
|
|
metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
|
|
|
|
}
|
2008-05-23 16:52:54 +03:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
for (i = 0; i < count; i++) {
|
2015-05-20 00:34:42 +02:00
|
|
|
ret = get_str(bc, name, sizeof(name));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
value = get_s(bc);
|
2015-05-12 18:20:23 +02:00
|
|
|
str_value[0] = 0;
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (value == -1) {
|
|
|
|
type = "UTF-8";
|
2015-05-22 22:58:55 +02:00
|
|
|
ret = get_str(bc, str_value, sizeof(str_value));
|
2011-12-02 19:14:05 +03:00
|
|
|
} else if (value == -2) {
|
2015-05-22 22:58:55 +02:00
|
|
|
ret = get_str(bc, type_str, sizeof(type_str));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
type = type_str;
|
2015-05-22 22:58:55 +02:00
|
|
|
ret = get_str(bc, str_value, sizeof(str_value));
|
2011-12-02 19:14:05 +03:00
|
|
|
} else if (value == -3) {
|
|
|
|
type = "s";
|
|
|
|
value = get_s(bc);
|
|
|
|
} else if (value == -4) {
|
|
|
|
type = "t";
|
|
|
|
value = ffio_read_varlen(bc);
|
|
|
|
} else if (value < -4) {
|
|
|
|
type = "r";
|
2006-11-11 03:35:50 +02:00
|
|
|
get_s(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
} else {
|
|
|
|
type = "v";
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 22:58:55 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-05-04 13:29:12 +03:00
|
|
|
if (stream_id_plus1 > s->nb_streams) {
|
2015-11-24 02:25:04 +02:00
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"invalid stream id %d for info packet\n",
|
|
|
|
stream_id_plus1);
|
2008-03-07 21:25:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (!strcmp(type, "UTF-8")) {
|
|
|
|
if (chapter_id == 0 && !strcmp(name, "Disposition")) {
|
2008-03-07 21:25:09 +02:00
|
|
|
set_disposition_bits(s, str_value, stream_id_plus1 - 1);
|
2010-10-24 10:28:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
2012-10-02 01:41:40 +03:00
|
|
|
|
|
|
|
if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) {
|
|
|
|
sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den);
|
2015-04-28 20:31:56 +02:00
|
|
|
if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den ||
|
2018-12-14 00:17:00 +02:00
|
|
|
st->r_frame_rate.num < 0 || st->r_frame_rate.den < 0)
|
2012-12-18 06:05:44 +03:00
|
|
|
st->r_frame_rate.num = st->r_frame_rate.den = 0;
|
2012-10-02 01:41:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (metadata && av_strcasecmp(name, "Uses") &&
|
2014-08-13 00:03:53 +03:00
|
|
|
av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
|
2014-08-20 17:45:08 +03:00
|
|
|
if (event_flags)
|
|
|
|
*event_flags |= metadata_flag;
|
2011-05-22 13:46:29 +03:00
|
|
|
av_dict_set(metadata, name, str_value, 0);
|
2014-08-13 00:03:53 +03:00
|
|
|
}
|
2008-05-23 16:52:54 +03:00
|
|
|
}
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
|
2007-08-17 13:45:50 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
|
2013-05-06 12:39:30 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2015-05-22 23:02:07 +02:00
|
|
|
fail:
|
|
|
|
return FFMIN(ret, 0);
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2012-09-21 07:27:38 +03:00
|
|
|
int64_t end;
|
|
|
|
uint64_t tmp;
|
2013-10-22 18:11:11 +03:00
|
|
|
int ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
nut->last_syncpoint_pos = avio_tell(bc) - 8;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
|
2011-03-03 21:11:45 +02:00
|
|
|
end += avio_tell(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
tmp = ffio_read_varlen(bc);
|
|
|
|
*back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
|
|
|
|
if (*back_ptr < 0)
|
2013-03-04 18:51:21 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
|
|
|
|
tmp / nut->time_base_count);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
if (nut->flags & NUT_BROADCAST) {
|
|
|
|
tmp = ffio_read_varlen(bc);
|
|
|
|
av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
|
|
|
|
av_rescale_q(tmp / nut->time_base_count,
|
|
|
|
nut->time_base[tmp % nut->time_base_count],
|
|
|
|
AV_TIME_BASE_Q));
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
|
2006-11-11 21:37:21 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
|
2013-03-04 18:51:21 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2006-11-14 15:19:51 +02:00
|
|
|
|
2012-09-21 07:29:24 +03:00
|
|
|
*ts = tmp / nut->time_base_count *
|
|
|
|
av_q2d(nut->time_base[tmp % nut->time_base_count]) * AV_TIME_BASE;
|
2013-10-22 18:11:11 +03:00
|
|
|
|
|
|
|
if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
|
|
|
|
return ret;
|
2006-11-14 15:19:51 +02:00
|
|
|
|
2006-11-11 03:35:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-30 18:19:43 +03:00
|
|
|
//FIXME calculate exactly, this is just a good approximation.
|
|
|
|
static int64_t find_duration(NUTContext *nut, int64_t filesize)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
int64_t duration = 0;
|
|
|
|
|
2013-07-06 23:05:35 +03:00
|
|
|
ff_find_last_ts(s, -1, &duration, NULL, nut_read_timestamp);
|
|
|
|
|
2012-06-30 19:58:52 +03:00
|
|
|
if(duration > 0)
|
|
|
|
s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
|
2012-06-30 18:19:43 +03:00
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int find_and_decode_index(NUTContext *nut)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2006-11-12 14:07:42 +02:00
|
|
|
uint64_t tmp, end;
|
|
|
|
int i, j, syncpoint_count;
|
2011-12-02 19:14:05 +03:00
|
|
|
int64_t filesize = avio_size(bc);
|
2015-05-22 23:02:07 +02:00
|
|
|
int64_t *syncpoints = NULL;
|
2013-05-02 19:44:22 +03:00
|
|
|
uint64_t max_pts;
|
2015-05-22 23:02:07 +02:00
|
|
|
int8_t *has_keyframe = NULL;
|
2013-05-06 12:39:30 +03:00
|
|
|
int ret = AVERROR_INVALIDDATA;
|
2006-11-12 14:07:42 +02:00
|
|
|
|
2012-06-30 18:01:59 +03:00
|
|
|
if(filesize <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
avio_seek(bc, filesize - 12, SEEK_SET);
|
|
|
|
avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
|
|
|
|
if (avio_rb64(bc) != INDEX_STARTCODE) {
|
2015-11-24 02:25:04 +02:00
|
|
|
av_log(s, AV_LOG_WARNING, "no index at the end\n");
|
2012-06-30 18:19:43 +03:00
|
|
|
|
|
|
|
if(s->duration<=0)
|
|
|
|
s->duration = find_duration(nut, filesize);
|
2013-05-06 12:39:30 +03:00
|
|
|
return ret;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
|
2011-03-03 21:11:45 +02:00
|
|
|
end += avio_tell(bc);
|
2006-11-12 14:07:42 +02:00
|
|
|
|
2013-05-02 19:44:22 +03:00
|
|
|
max_pts = ffio_read_varlen(bc);
|
|
|
|
s->duration = av_rescale_q(max_pts / nut->time_base_count,
|
|
|
|
nut->time_base[max_pts % nut->time_base_count],
|
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
|
|
|
|
|
2012-10-02 05:13:23 +03:00
|
|
|
GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
|
2013-09-11 14:30:15 +03:00
|
|
|
syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t));
|
|
|
|
has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t));
|
|
|
|
if (!syncpoints || !has_keyframe) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
for (i = 0; i < syncpoint_count; i++) {
|
2011-03-14 21:38:57 +02:00
|
|
|
syncpoints[i] = ffio_read_varlen(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (syncpoints[i] <= 0)
|
2008-05-27 23:48:27 +03:00
|
|
|
goto fail;
|
2011-12-02 19:14:05 +03:00
|
|
|
if (i)
|
|
|
|
syncpoints[i] += syncpoints[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
int64_t last_pts = -1;
|
|
|
|
for (j = 0; j < syncpoint_count;) {
|
|
|
|
uint64_t x = ffio_read_varlen(bc);
|
|
|
|
int type = x & 1;
|
|
|
|
int n = j;
|
|
|
|
x >>= 1;
|
|
|
|
if (type) {
|
|
|
|
int flag = x & 1;
|
|
|
|
x >>= 1;
|
|
|
|
if (n + x >= syncpoint_count + 1) {
|
2012-08-27 10:14:40 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1);
|
2008-05-27 23:48:27 +03:00
|
|
|
goto fail;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
while (x--)
|
|
|
|
has_keyframe[n++] = flag;
|
|
|
|
has_keyframe[n++] = !flag;
|
|
|
|
} else {
|
2015-05-23 00:23:05 +02:00
|
|
|
if (x <= 1) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
while (x != 1) {
|
|
|
|
if (n >= syncpoint_count + 1) {
|
2006-11-12 14:07:42 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "index overflow B\n");
|
2008-05-27 23:48:27 +03:00
|
|
|
goto fail;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
has_keyframe[n++] = x & 1;
|
|
|
|
x >>= 1;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
if (has_keyframe[0]) {
|
2006-11-12 14:07:42 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
|
2008-05-27 23:48:27 +03:00
|
|
|
goto fail;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
2012-04-28 22:11:38 +03:00
|
|
|
av_assert0(n <= syncpoint_count + 1);
|
2011-12-02 19:14:05 +03:00
|
|
|
for (; j < n && j < syncpoint_count; j++) {
|
|
|
|
if (has_keyframe[j]) {
|
|
|
|
uint64_t B, A = ffio_read_varlen(bc);
|
|
|
|
if (!A) {
|
|
|
|
A = ffio_read_varlen(bc);
|
|
|
|
B = ffio_read_varlen(bc);
|
|
|
|
// eor_pts[j][i] = last_pts + A + B
|
|
|
|
} else
|
|
|
|
B = 0;
|
|
|
|
av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
|
|
|
|
last_pts + A, 0, 0, AVINDEX_KEYFRAME);
|
2006-11-12 14:07:42 +02:00
|
|
|
last_pts += A + B;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
|
2007-08-17 13:45:50 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
|
2008-05-27 23:48:27 +03:00
|
|
|
goto fail;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
ret = 0;
|
|
|
|
|
2008-05-27 23:48:27 +03:00
|
|
|
fail:
|
|
|
|
av_free(syncpoints);
|
|
|
|
av_free(has_keyframe);
|
|
|
|
return ret;
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:29:49 +02:00
|
|
|
static int nut_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
av_freep(&nut->time_base);
|
|
|
|
av_freep(&nut->stream);
|
|
|
|
ff_nut_free_sp(nut);
|
|
|
|
for (i = 1; i < nut->header_count; i++)
|
|
|
|
av_freep(&nut->header[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-28 20:58:21 +02:00
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int nut_read_header(AVFormatContext *s)
|
2006-11-11 03:35:50 +02:00
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *bc = s->pb;
|
2006-11-11 03:35:50 +02:00
|
|
|
int64_t pos;
|
2022-02-23 04:31:59 +02:00
|
|
|
int initialized_stream_count, ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
nut->avf = s;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
|
|
|
/* main header */
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = 0;
|
2022-02-23 04:31:59 +02:00
|
|
|
ret = 0;
|
2011-12-02 19:14:05 +03:00
|
|
|
do {
|
2022-02-23 04:31:59 +02:00
|
|
|
if (ret == AVERROR(ENOMEM))
|
|
|
|
return ret;
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
|
|
|
|
if (pos < 0 + 1) {
|
2007-08-17 13:45:50 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
|
2020-03-21 19:31:06 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2022-02-23 04:31:59 +02:00
|
|
|
} while ((ret = decode_main_header(nut)) < 0);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
|
|
|
/* stream headers */
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = 0;
|
|
|
|
for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
|
|
|
|
pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1;
|
|
|
|
if (pos < 0 + 1) {
|
2007-08-17 13:45:50 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
|
2020-03-21 19:31:06 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
if (decode_stream_header(nut) >= 0)
|
2008-02-13 11:26:10 +02:00
|
|
|
initialized_stream_count++;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* info headers */
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = 0;
|
|
|
|
for (;;) {
|
|
|
|
uint64_t startcode = find_any_startcode(bc, pos);
|
|
|
|
pos = avio_tell(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (startcode == 0) {
|
2006-11-11 03:35:50 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
|
2020-03-21 19:31:06 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-02 19:14:05 +03:00
|
|
|
} else if (startcode == SYNCPOINT_STARTCODE) {
|
|
|
|
nut->next_startcode = startcode;
|
2006-11-11 03:35:50 +02:00
|
|
|
break;
|
2011-12-02 19:14:05 +03:00
|
|
|
} else if (startcode != INFO_STARTCODE) {
|
2006-11-11 03:35:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_info_header(nut);
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:58:07 +02:00
|
|
|
ffformatcontext(s)->data_offset = pos - 8;
|
2006-11-11 21:37:21 +02:00
|
|
|
|
2016-09-27 16:26:37 +02:00
|
|
|
if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
|
2011-12-02 19:14:05 +03:00
|
|
|
int64_t orig_pos = avio_tell(bc);
|
2006-11-12 14:07:42 +02:00
|
|
|
find_and_decode_index(nut);
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(bc, orig_pos, SEEK_SET);
|
2006-11-12 14:07:42 +02:00
|
|
|
}
|
2012-04-28 22:11:38 +03:00
|
|
|
av_assert0(nut->next_startcode == SYNCPOINT_STARTCODE);
|
2006-11-12 14:07:42 +02:00
|
|
|
|
2010-10-15 22:04:25 +03:00
|
|
|
ff_metadata_conv_ctx(s, NULL, ff_nut_metadata_conv);
|
|
|
|
|
2015-05-14 01:23:09 +02:00
|
|
|
return 0;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
|
|
|
|
{
|
|
|
|
int count = ffio_read_varlen(bc);
|
|
|
|
int skip_start = 0;
|
|
|
|
int skip_end = 0;
|
|
|
|
int channels = 0;
|
|
|
|
int64_t channel_layout = 0;
|
|
|
|
int sample_rate = 0;
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2015-05-20 00:34:42 +02:00
|
|
|
int i, ret;
|
2013-12-23 04:58:36 +03:00
|
|
|
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
uint8_t name[256], str_value[256], type_str[256];
|
2014-01-05 19:27:23 +03:00
|
|
|
int value;
|
2013-12-23 04:58:36 +03:00
|
|
|
if (avio_tell(bc) >= maxpos)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2015-05-20 00:34:42 +02:00
|
|
|
ret = get_str(bc, name, sizeof(name));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2014-01-05 19:27:23 +03:00
|
|
|
value = get_s(bc);
|
2013-12-23 04:58:36 +03:00
|
|
|
|
|
|
|
if (value == -1) {
|
2015-05-22 22:58:55 +02:00
|
|
|
ret = get_str(bc, str_value, sizeof(str_value));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-23 04:58:36 +03:00
|
|
|
av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
|
|
|
|
} else if (value == -2) {
|
|
|
|
uint8_t *dst = NULL;
|
|
|
|
int64_t v64, value_len;
|
|
|
|
|
2015-05-22 22:58:55 +02:00
|
|
|
ret = get_str(bc, type_str, sizeof(type_str));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-23 04:58:36 +03:00
|
|
|
value_len = ffio_read_varlen(bc);
|
2015-12-19 13:02:56 +02:00
|
|
|
if (value_len < 0 || value_len >= maxpos - avio_tell(bc))
|
2013-12-23 04:58:36 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
if (!strcmp(name, "Palette")) {
|
|
|
|
dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, value_len);
|
|
|
|
} else if (!strcmp(name, "Extradata")) {
|
|
|
|
dst = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, value_len);
|
|
|
|
} else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
|
|
|
|
dst = av_packet_new_side_data(pkt, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, value_len + 8);
|
|
|
|
if(!dst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
AV_WB64(dst, v64);
|
|
|
|
dst += 8;
|
|
|
|
} else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
|
|
|
|
channel_layout = avio_rl64(bc);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
|
|
|
|
avio_skip(bc, value_len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!dst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
avio_read(bc, dst, value_len);
|
|
|
|
} else if (value == -3) {
|
|
|
|
value = get_s(bc);
|
|
|
|
} else if (value == -4) {
|
|
|
|
value = ffio_read_varlen(bc);
|
|
|
|
} else if (value < -4) {
|
|
|
|
get_s(bc);
|
|
|
|
} else {
|
|
|
|
if (!strcmp(name, "SkipStart")) {
|
|
|
|
skip_start = value;
|
|
|
|
} else if (!strcmp(name, "SkipEnd")) {
|
|
|
|
skip_end = value;
|
|
|
|
} else if (!strcmp(name, "Channels")) {
|
|
|
|
channels = value;
|
|
|
|
} else if (!strcmp(name, "SampleRate")) {
|
|
|
|
sample_rate = value;
|
|
|
|
} else if (!strcmp(name, "Width")) {
|
|
|
|
width = value;
|
|
|
|
} else if (!strcmp(name, "Height")) {
|
|
|
|
height = value;
|
|
|
|
} else {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channels || channel_layout || sample_rate || width || height) {
|
|
|
|
uint8_t *dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, 28);
|
|
|
|
if (!dst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
bytestream_put_le32(&dst,
|
2017-03-31 17:52:39 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
2013-12-23 04:58:36 +03:00
|
|
|
AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT*(!!channels) +
|
|
|
|
AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT*(!!channel_layout) +
|
2017-03-31 17:52:39 +02:00
|
|
|
#endif
|
2013-12-23 04:58:36 +03:00
|
|
|
AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE*(!!sample_rate) +
|
|
|
|
AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS*(!!(width|height))
|
|
|
|
);
|
|
|
|
if (channels)
|
|
|
|
bytestream_put_le32(&dst, channels);
|
|
|
|
if (channel_layout)
|
|
|
|
bytestream_put_le64(&dst, channel_layout);
|
|
|
|
if (sample_rate)
|
|
|
|
bytestream_put_le32(&dst, sample_rate);
|
|
|
|
if (width || height){
|
|
|
|
bytestream_put_le32(&dst, width);
|
|
|
|
bytestream_put_le32(&dst, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip_start || skip_end) {
|
|
|
|
uint8_t *dst = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
|
|
|
|
if (!dst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
AV_WL32(dst, skip_start);
|
|
|
|
AV_WL32(dst+4, skip_end);
|
|
|
|
}
|
|
|
|
|
2015-06-27 17:50:56 +02:00
|
|
|
if (avio_tell(bc) >= maxpos)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2013-12-23 04:58:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
|
|
|
|
uint8_t *header_idx, int frame_code)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2006-11-11 03:35:50 +02:00
|
|
|
StreamContext *stc;
|
2015-05-22 23:02:07 +02:00
|
|
|
int size, flags, size_mul, pts_delta, i, reserved_count, ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
uint64_t tmp;
|
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
if (!(nut->flags & NUT_PIPE) &&
|
|
|
|
avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
|
|
|
|
avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
|
2011-01-12 18:40:28 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flags = nut->frame_code[frame_code].flags;
|
|
|
|
size_mul = nut->frame_code[frame_code].size_mul;
|
|
|
|
size = nut->frame_code[frame_code].size_lsb;
|
|
|
|
*stream_id = nut->frame_code[frame_code].stream_id;
|
|
|
|
pts_delta = nut->frame_code[frame_code].pts_delta;
|
|
|
|
reserved_count = nut->frame_code[frame_code].reserved_count;
|
2008-02-15 04:36:09 +02:00
|
|
|
*header_idx = nut->frame_code[frame_code].header_idx;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (flags & FLAG_INVALID)
|
2011-01-12 18:40:28 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-02 19:14:05 +03:00
|
|
|
if (flags & FLAG_CODED)
|
2011-03-14 21:38:57 +02:00
|
|
|
flags ^= ffio_read_varlen(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (flags & FLAG_STREAM_ID) {
|
2012-10-02 05:13:23 +03:00
|
|
|
GET_V(*stream_id, tmp < s->nb_streams);
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
stc = &nut->stream[*stream_id];
|
|
|
|
if (flags & FLAG_CODED_PTS) {
|
2018-12-14 22:48:17 +02:00
|
|
|
int64_t coded_pts = ffio_read_varlen(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
// FIXME check last_pts validity?
|
2018-12-14 22:48:17 +02:00
|
|
|
if (coded_pts < (1LL << stc->msb_pts_shift)) {
|
2011-12-02 19:14:05 +03:00
|
|
|
*pts = ff_lsb2full(stc, coded_pts);
|
|
|
|
} else
|
2012-10-20 21:45:32 +03:00
|
|
|
*pts = coded_pts - (1LL << stc->msb_pts_shift);
|
2011-12-02 19:14:05 +03:00
|
|
|
} else
|
|
|
|
*pts = stc->last_pts + pts_delta;
|
|
|
|
if (flags & FLAG_SIZE_MSB)
|
|
|
|
size += size_mul * ffio_read_varlen(bc);
|
|
|
|
if (flags & FLAG_MATCH_TIME)
|
2008-02-13 22:17:28 +02:00
|
|
|
get_s(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (flags & FLAG_HEADER_IDX)
|
|
|
|
*header_idx = ffio_read_varlen(bc);
|
|
|
|
if (flags & FLAG_RESERVED)
|
|
|
|
reserved_count = ffio_read_varlen(bc);
|
2015-05-20 00:31:24 +02:00
|
|
|
for (i = 0; i < reserved_count; i++) {
|
|
|
|
if (bc->eof_reached) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2011-03-14 21:38:57 +02:00
|
|
|
ffio_read_varlen(bc);
|
2015-05-20 00:31:24 +02:00
|
|
|
}
|
2008-02-15 04:36:09 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (*header_idx >= (unsigned)nut->header_count) {
|
2008-02-15 04:36:09 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
|
2011-01-12 18:40:28 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-02-15 04:36:09 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
if (size > 4096)
|
|
|
|
*header_idx = 0;
|
2008-02-15 04:36:09 +02:00
|
|
|
size -= nut->header_len[*header_idx];
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (flags & FLAG_CHECKSUM) {
|
|
|
|
avio_rb32(bc); // FIXME check this
|
2014-03-06 19:58:34 +03:00
|
|
|
} else if (!(nut->flags & NUT_PIPE) &&
|
|
|
|
size > 2 * nut->max_distance ||
|
|
|
|
FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
|
2006-11-15 00:24:10 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
|
2011-01-12 18:40:28 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
stc->last_pts = *pts;
|
|
|
|
stc->last_flags = flags;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
|
|
|
return size;
|
2015-05-22 23:02:07 +02:00
|
|
|
fail:
|
|
|
|
return ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
|
|
|
|
{
|
|
|
|
AVFormatContext *s = nut->avf;
|
|
|
|
AVIOContext *bc = s->pb;
|
2014-10-20 16:11:19 +03:00
|
|
|
int size, stream_id, discard, ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
int64_t pts, last_IP_pts;
|
2006-11-16 13:03:40 +02:00
|
|
|
StreamContext *stc;
|
2008-02-15 04:36:09 +02:00
|
|
|
uint8_t header_idx;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
|
|
|
|
if (size < 0)
|
2011-01-12 18:40:28 +02:00
|
|
|
return size;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
stc = &nut->stream[stream_id];
|
2006-11-16 13:03:40 +02:00
|
|
|
|
|
|
|
if (stc->last_flags & FLAG_KEY)
|
2011-12-02 19:14:05 +03:00
|
|
|
stc->skip_until_key_frame = 0;
|
|
|
|
|
|
|
|
discard = s->streams[stream_id]->discard;
|
2021-08-24 19:41:16 +02:00
|
|
|
last_IP_pts = ffstream(s->streams[stream_id])->last_IP_pts;
|
2011-12-02 19:14:05 +03:00
|
|
|
if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
|
|
|
|
(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
|
|
|
|
last_IP_pts > pts) ||
|
|
|
|
discard >= AVDISCARD_ALL ||
|
|
|
|
stc->skip_until_key_frame) {
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(bc, size);
|
2006-11-11 03:35:50 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-20 16:11:19 +03:00
|
|
|
ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-12-18 16:18:47 +02:00
|
|
|
if (nut->header[header_idx])
|
|
|
|
memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
|
2011-12-02 19:14:05 +03:00
|
|
|
pkt->pos = avio_tell(bc); // FIXME
|
2013-12-23 04:58:36 +03:00
|
|
|
if (stc->last_flags & FLAG_SM_DATA) {
|
|
|
|
int sm_size;
|
2015-05-22 23:02:07 +02:00
|
|
|
if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) {
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) {
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-12-23 04:58:36 +03:00
|
|
|
sm_size = avio_tell(bc) - pkt->pos;
|
|
|
|
size -= sm_size;
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:58:24 +03:00
|
|
|
ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size);
|
|
|
|
if (ret != size) {
|
|
|
|
if (ret < 0)
|
2015-05-22 23:02:07 +02:00
|
|
|
goto fail;
|
2014-01-07 00:58:24 +03:00
|
|
|
}
|
2014-01-10 02:32:39 +03:00
|
|
|
av_shrink_packet(pkt, nut->header_len[header_idx] + ret);
|
2008-02-15 04:36:09 +02:00
|
|
|
|
2006-11-11 03:35:50 +02:00
|
|
|
pkt->stream_index = stream_id;
|
2006-11-16 13:03:40 +02:00
|
|
|
if (stc->last_flags & FLAG_KEY)
|
2010-03-31 15:29:58 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2006-11-11 03:35:50 +02:00
|
|
|
pkt->pts = pts;
|
|
|
|
|
|
|
|
return 0;
|
2015-05-22 23:02:07 +02:00
|
|
|
fail:
|
2015-10-27 15:35:30 +02:00
|
|
|
av_packet_unref(pkt);
|
2015-05-22 23:02:07 +02:00
|
|
|
return ret;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *bc = s->pb;
|
2011-12-02 19:14:05 +03:00
|
|
|
int i, frame_code = 0, ret, skip;
|
2006-11-14 15:19:51 +02:00
|
|
|
int64_t ts, back_ptr;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
for (;;) {
|
|
|
|
int64_t pos = avio_tell(bc);
|
|
|
|
uint64_t tmp = nut->next_startcode;
|
|
|
|
nut->next_startcode = 0;
|
2006-11-11 03:35:50 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
if (tmp) {
|
|
|
|
pos -= 8;
|
|
|
|
} else {
|
2011-02-21 17:43:01 +02:00
|
|
|
frame_code = avio_r8(bc);
|
2014-08-07 23:12:41 +03:00
|
|
|
if (avio_feof(bc))
|
2013-05-06 12:39:30 +03:00
|
|
|
return AVERROR_EOF;
|
2011-12-02 19:14:05 +03:00
|
|
|
if (frame_code == 'N') {
|
|
|
|
tmp = frame_code;
|
|
|
|
for (i = 1; i < 8; i++)
|
|
|
|
tmp = (tmp << 8) + avio_r8(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
switch (tmp) {
|
2006-11-11 03:35:50 +02:00
|
|
|
case MAIN_STARTCODE:
|
|
|
|
case STREAM_STARTCODE:
|
|
|
|
case INDEX_STARTCODE:
|
2011-12-02 19:14:05 +03:00
|
|
|
skip = get_packetheader(nut, bc, 0, tmp);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(bc, skip);
|
2006-11-11 03:35:50 +02:00
|
|
|
break;
|
|
|
|
case INFO_STARTCODE:
|
2011-12-02 19:14:05 +03:00
|
|
|
if (decode_info_header(nut) < 0)
|
2006-11-11 03:35:50 +02:00
|
|
|
goto resync;
|
|
|
|
break;
|
|
|
|
case SYNCPOINT_STARTCODE:
|
2011-12-02 19:14:05 +03:00
|
|
|
if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
|
2006-11-11 03:35:50 +02:00
|
|
|
goto resync;
|
2011-02-21 17:43:01 +02:00
|
|
|
frame_code = avio_r8(bc);
|
2006-11-11 03:35:50 +02:00
|
|
|
case 0:
|
2011-12-02 19:14:05 +03:00
|
|
|
ret = decode_frame(nut, pkt, frame_code);
|
|
|
|
if (ret == 0)
|
2006-11-11 03:35:50 +02:00
|
|
|
return 0;
|
2011-12-02 19:14:05 +03:00
|
|
|
else if (ret == 1) // OK but discard packet
|
2006-11-11 03:35:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
resync:
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
|
2015-05-20 00:06:05 +02:00
|
|
|
tmp = find_any_startcode(bc, FFMAX(nut->last_syncpoint_pos, nut->last_resync_pos) + 1);
|
|
|
|
nut->last_resync_pos = avio_tell(bc);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (tmp == 0)
|
2011-01-12 18:40:28 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "sync\n");
|
|
|
|
nut->next_startcode = tmp;
|
2006-11-11 03:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t *pos_arg, int64_t pos_limit)
|
|
|
|
{
|
2006-11-11 21:37:21 +02:00
|
|
|
NUTContext *nut = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *bc = s->pb;
|
2006-11-14 15:19:51 +02:00
|
|
|
int64_t pos, pts, back_ptr;
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
|
|
|
|
stream_index, *pos_arg, pos_limit);
|
2006-11-11 21:37:21 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = *pos_arg;
|
|
|
|
do {
|
|
|
|
pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1;
|
|
|
|
if (pos < 1) {
|
2007-08-17 13:45:50 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
|
2006-11-11 21:37:21 +02:00
|
|
|
return AV_NOPTS_VALUE;
|
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
} while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
|
|
|
|
*pos_arg = pos - 1;
|
2012-04-28 22:11:38 +03:00
|
|
|
av_assert0(nut->last_syncpoint_pos == *pos_arg);
|
2006-11-11 21:37:21 +02:00
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
|
2012-04-28 22:14:03 +03:00
|
|
|
if (stream_index == -2)
|
2011-12-02 19:14:05 +03:00
|
|
|
return back_ptr;
|
2012-04-28 22:14:03 +03:00
|
|
|
av_assert0(stream_index == -1);
|
|
|
|
return pts;
|
2006-11-11 21:37:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 19:14:05 +03:00
|
|
|
static int read_seek(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t pts, int flags)
|
|
|
|
{
|
|
|
|
NUTContext *nut = s->priv_data;
|
|
|
|
AVStream *st = s->streams[stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const sti = ffstream(st);
|
2011-12-02 19:14:05 +03:00
|
|
|
Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
|
|
|
|
Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
|
|
|
|
Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
|
2006-11-14 15:19:51 +02:00
|
|
|
int64_t pos, pos2, ts;
|
2006-11-16 12:20:29 +02:00
|
|
|
int i;
|
2006-11-14 15:19:51 +02:00
|
|
|
|
2014-03-06 19:58:34 +03:00
|
|
|
if (nut->flags & NUT_PIPE) {
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:41:16 +02:00
|
|
|
if (sti->index_entries) {
|
2011-12-02 19:14:05 +03:00
|
|
|
int index = av_index_search_timestamp(st, pts, flags);
|
2012-08-20 00:30:27 +03:00
|
|
|
if (index < 0)
|
|
|
|
index = av_index_search_timestamp(st, pts, flags ^ AVSEEK_FLAG_BACKWARD);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (index < 0)
|
2006-11-14 15:32:14 +02:00
|
|
|
return -1;
|
|
|
|
|
2021-08-24 19:41:16 +02:00
|
|
|
pos2 = sti->index_entries[index].pos;
|
|
|
|
ts = sti->index_entries[index].timestamp;
|
2011-12-02 19:14:05 +03:00
|
|
|
} else {
|
2015-10-24 23:53:21 +02:00
|
|
|
av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp,
|
2010-03-28 12:59:58 +03:00
|
|
|
(void **) next_node);
|
2011-12-02 19:14:05 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
|
|
|
|
next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
|
|
|
|
next_node[1]->ts);
|
|
|
|
pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
|
|
|
|
next_node[1]->pos, next_node[1]->pos,
|
|
|
|
next_node[0]->ts, next_node[1]->ts,
|
|
|
|
AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
|
2015-07-01 14:28:41 +02:00
|
|
|
if (pos < 0)
|
|
|
|
return pos;
|
2011-12-02 19:14:05 +03:00
|
|
|
|
|
|
|
if (!(flags & AVSEEK_FLAG_BACKWARD)) {
|
|
|
|
dummy.pos = pos + 16;
|
|
|
|
next_node[1] = &nopts_sp;
|
2015-10-24 23:53:21 +02:00
|
|
|
av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp,
|
2010-03-28 12:59:58 +03:00
|
|
|
(void **) next_node);
|
2011-12-02 19:14:05 +03:00
|
|
|
pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
|
|
|
|
next_node[1]->pos, next_node[1]->pos,
|
|
|
|
next_node[0]->back_ptr, next_node[1]->back_ptr,
|
|
|
|
flags, &ts, nut_read_timestamp);
|
|
|
|
if (pos2 >= 0)
|
|
|
|
pos = pos2;
|
|
|
|
// FIXME dir but I think it does not matter
|
2006-11-14 15:33:05 +02:00
|
|
|
}
|
2011-12-02 19:14:05 +03:00
|
|
|
dummy.pos = pos;
|
2015-10-24 23:53:21 +02:00
|
|
|
sp = av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp,
|
2011-12-02 19:14:05 +03:00
|
|
|
NULL);
|
2006-11-14 15:33:05 +02:00
|
|
|
|
2012-04-28 22:11:38 +03:00
|
|
|
av_assert0(sp);
|
2011-12-02 19:14:05 +03:00
|
|
|
pos2 = sp->back_ptr - 15;
|
2006-11-14 15:32:14 +02:00
|
|
|
}
|
2019-09-28 04:21:11 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
|
2011-12-02 19:14:05 +03:00
|
|
|
pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(s->pb, pos, SEEK_SET);
|
2015-05-20 17:32:48 +02:00
|
|
|
nut->last_syncpoint_pos = pos;
|
2019-09-28 04:21:11 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
|
2011-12-02 19:14:05 +03:00
|
|
|
if (pos2 > pos || pos2 + 15 < pos)
|
2019-09-28 04:21:11 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
|
2011-12-02 19:14:05 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++)
|
|
|
|
nut->stream[i].skip_until_key_frame = 1;
|
2006-11-16 12:20:29 +02:00
|
|
|
|
2015-05-20 00:06:05 +02:00
|
|
|
nut->last_resync_pos = 0;
|
|
|
|
|
2006-11-14 15:19:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_nut_demuxer = {
|
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"),
|
2012-05-16 02:32:33 +03:00
|
|
|
.flags = AVFMT_SEEK_TO_PTS,
|
2011-07-16 23:18:12 +03:00
|
|
|
.priv_data_size = sizeof(NUTContext),
|
2020-03-21 19:31:06 +02:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2011-07-16 23:18:12 +03:00
|
|
|
.read_probe = nut_probe,
|
|
|
|
.read_header = nut_read_header,
|
|
|
|
.read_packet = nut_read_packet,
|
|
|
|
.read_close = nut_read_close,
|
|
|
|
.read_seek = read_seek,
|
2012-04-06 17:50:48 +03:00
|
|
|
.extensions = "nut",
|
2012-10-12 16:49:36 +03:00
|
|
|
.codec_tag = ff_nut_codec_tags,
|
2006-11-11 03:35:50 +02:00
|
|
|
};
|