2009-01-20 09:42:14 +02:00
|
|
|
/*
|
|
|
|
* R3D REDCODE demuxer
|
|
|
|
* Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2011-06-04 14:58:23 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2009-01-20 09:42:14 +02:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct R3DContext {
|
2009-01-20 09:42:14 +02:00
|
|
|
unsigned video_offsets_count;
|
|
|
|
unsigned rdvo_offset;
|
2015-10-21 11:22:13 +02:00
|
|
|
|
|
|
|
int audio_channels;
|
2009-01-20 09:42:14 +02:00
|
|
|
} R3DContext;
|
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct Atom {
|
2009-01-20 09:42:14 +02:00
|
|
|
unsigned size;
|
|
|
|
uint32_t tag;
|
|
|
|
uint64_t offset;
|
|
|
|
} Atom;
|
|
|
|
|
|
|
|
static int read_atom(AVFormatContext *s, Atom *atom)
|
|
|
|
{
|
2011-03-03 21:11:45 +02:00
|
|
|
atom->offset = avio_tell(s->pb);
|
2011-02-21 17:43:01 +02:00
|
|
|
atom->size = avio_rb32(s->pb);
|
2009-01-20 09:42:14 +02:00
|
|
|
if (atom->size < 8)
|
|
|
|
return -1;
|
2011-02-21 17:43:01 +02:00
|
|
|
atom->tag = avio_rl32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "atom %u %.4s offset %#"PRIx64"\n",
|
2009-01-20 09:42:14 +02:00
|
|
|
atom->size, (char*)&atom->tag, atom->offset);
|
|
|
|
return atom->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r3d_read_red1(AVFormatContext *s)
|
|
|
|
{
|
2011-06-18 12:43:24 +03:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2015-10-21 11:22:13 +02:00
|
|
|
R3DContext *r3d = s->priv_data;
|
2009-03-01 17:28:56 +02:00
|
|
|
char filename[258];
|
2011-06-03 14:52:29 +03:00
|
|
|
int tmp;
|
2020-08-20 00:51:02 +02:00
|
|
|
int ret;
|
2011-06-03 14:52:29 +03:00
|
|
|
int av_unused tmp2;
|
2012-03-02 16:08:11 +03:00
|
|
|
AVRational framerate;
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
if (!st)
|
2009-08-25 22:59:38 +03:00
|
|
|
return AVERROR(ENOMEM);
|
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_ID_JPEG2000;
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_r8(s->pb); // major version
|
|
|
|
tmp2 = avio_r8(s->pb); // minor version
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb16(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown1 %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 32, 1, tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb); // filenum
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "filenum %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, 32); // unknown
|
2009-01-20 09:42:14 +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
|
|
|
st->codecpar->width = avio_rb32(s->pb);
|
|
|
|
st->codecpar->height = avio_rb32(s->pb);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb16(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown2 %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2012-03-02 16:08:11 +03:00
|
|
|
framerate.num = avio_rb16(s->pb);
|
|
|
|
framerate.den = avio_rb16(s->pb);
|
2013-09-19 17:02:36 +03:00
|
|
|
if (framerate.num > 0 && framerate.den > 0) {
|
2012-06-26 14:10:01 +03:00
|
|
|
#if FF_API_R_FRAME_RATE
|
|
|
|
st->r_frame_rate =
|
|
|
|
#endif
|
|
|
|
st->avg_frame_rate = framerate;
|
|
|
|
}
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2015-10-21 11:22:13 +02:00
|
|
|
r3d->audio_channels = avio_r8(s->pb); // audio channels
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "audio channels %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2020-08-20 00:51:02 +02:00
|
|
|
ret = avio_read(s->pb, filename, 257);
|
|
|
|
if (ret < 257)
|
|
|
|
return ret < 0 ? ret : AVERROR_EOF;
|
2009-03-01 17:28:56 +02:00
|
|
|
filename[sizeof(filename)-1] = 0;
|
2011-05-22 13:46:29 +03:00
|
|
|
av_dict_set(&st->metadata, "filename", filename, 0);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "filename %s\n", filename);
|
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
|
|
|
av_log(s, AV_LOG_TRACE, "resolution %dx%d\n", st->codecpar->width, st->codecpar->height);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "timescale %d\n", st->time_base.den);
|
|
|
|
av_log(s, AV_LOG_TRACE, "frame rate %d/%d\n",
|
2012-03-02 16:08:11 +03:00
|
|
|
framerate.num, framerate.den);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
|
|
|
|
{
|
|
|
|
R3DContext *r3d = s->priv_data;
|
|
|
|
AVStream *st = s->streams[0];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
r3d->video_offsets_count = (atom->size - 8) / 4;
|
|
|
|
|
|
|
|
for (i = 0; i < r3d->video_offsets_count; i++) {
|
2020-03-22 06:24:19 +02:00
|
|
|
unsigned video_offset = avio_rb32(s->pb);
|
|
|
|
if (!video_offset) {
|
2009-01-20 09:42:14 +02:00
|
|
|
r3d->video_offsets_count = i;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-22 06:24:19 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "video offset %d: %#x\n", i, video_offset);
|
2009-01-20 09:42:14 +02:00
|
|
|
}
|
|
|
|
|
2012-06-26 14:10:01 +03:00
|
|
|
if (st->avg_frame_rate.num)
|
2012-03-02 16:08:11 +03:00
|
|
|
st->duration = av_rescale_q(r3d->video_offsets_count,
|
2012-08-06 00:25:27 +03:00
|
|
|
av_inv_q(st->avg_frame_rate),
|
2012-03-02 16:08:11 +03:00
|
|
|
st->time_base);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "duration %"PRId64"\n", st->duration);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void r3d_read_reos(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
R3DContext *r3d = s->priv_data;
|
2011-06-03 14:52:29 +03:00
|
|
|
int av_unused tmp;
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
r3d->rdvo_offset = avio_rb32(s->pb);
|
|
|
|
avio_rb32(s->pb); // rdvs offset
|
|
|
|
avio_rb32(s->pb); // rdao offset
|
|
|
|
avio_rb32(s->pb); // rdas offset
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "num video chunks %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "num audio chunks %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, 6*4);
|
2009-01-20 09:42:14 +02:00
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int r3d_read_header(AVFormatContext *s)
|
2009-01-20 09:42:14 +02:00
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2009-01-20 09:42:14 +02:00
|
|
|
R3DContext *r3d = s->priv_data;
|
|
|
|
Atom atom;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (read_atom(s, &atom) < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "error reading atom\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (atom.tag == MKTAG('R','E','D','1')) {
|
|
|
|
if ((ret = r3d_read_red1(s)) < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-21 11:22:13 +02:00
|
|
|
/* we cannot create the audio stream now because we do not know the
|
|
|
|
* sample rate */
|
|
|
|
if (r3d->audio_channels)
|
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
|
2021-08-24 14:58:07 +02:00
|
|
|
si->data_offset = avio_tell(s->pb);
|
|
|
|
av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", si->data_offset);
|
2016-09-27 16:26:37 +02:00
|
|
|
if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
|
2009-01-20 09:42:14 +02:00
|
|
|
return 0;
|
|
|
|
// find REOB/REOF/REOS to load index
|
2011-03-04 20:57:36 +02:00
|
|
|
avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
|
2009-01-20 09:42:14 +02:00
|
|
|
if (read_atom(s, &atom) < 0)
|
|
|
|
av_log(s, AV_LOG_ERROR, "error reading end atom\n");
|
|
|
|
|
|
|
|
if (atom.tag != MKTAG('R','E','O','B') &&
|
|
|
|
atom.tag != MKTAG('R','E','O','F') &&
|
|
|
|
atom.tag != MKTAG('R','E','O','S'))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
r3d_read_reos(s);
|
|
|
|
|
|
|
|
if (r3d->rdvo_offset) {
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
|
2009-01-20 09:42:14 +02:00
|
|
|
if (read_atom(s, &atom) < 0)
|
|
|
|
av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
|
|
|
|
if (atom.tag == MKTAG('R','D','V','O')) {
|
|
|
|
if (r3d_read_rdvo(s, &atom) < 0)
|
|
|
|
av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2021-08-24 14:58:07 +02:00
|
|
|
avio_seek(s->pb, si->data_offset, SEEK_SET);
|
2009-01-20 09:42:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
|
|
|
|
{
|
|
|
|
AVStream *st = s->streams[0];
|
2011-06-03 14:52:29 +03:00
|
|
|
int tmp;
|
|
|
|
int av_unused tmp2;
|
2015-02-02 03:39:00 +02:00
|
|
|
int64_t pos = avio_tell(s->pb);
|
2009-01-20 09:42:14 +02:00
|
|
|
unsigned dts;
|
2010-03-02 23:32:29 +02:00
|
|
|
int ret;
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
dts = avio_rb32(s->pb);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "frame num %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_r8(s->pb); // major version
|
|
|
|
tmp2 = avio_r8(s->pb); // minor version
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb16(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
if (tmp > 4) {
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb16(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb16(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "width %d\n", tmp);
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "height %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "metadata len %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
}
|
2011-03-03 21:11:45 +02:00
|
|
|
tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
|
2009-01-20 09:42:14 +02:00
|
|
|
if (tmp < 0)
|
|
|
|
return -1;
|
2010-03-02 23:32:29 +02:00
|
|
|
ret = av_get_packet(s->pb, pkt, tmp);
|
|
|
|
if (ret < 0) {
|
2009-01-20 09:42:14 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "error reading video packet\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
pkt->dts = dts;
|
2012-06-26 14:10:01 +03:00
|
|
|
if (st->avg_frame_rate.num)
|
2009-01-20 09:42:14 +02:00
|
|
|
pkt->duration = (uint64_t)st->time_base.den*
|
2012-06-26 14:10:01 +03:00
|
|
|
st->avg_frame_rate.den/st->avg_frame_rate.num;
|
2015-09-26 18:13:55 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64"\n", pkt->dts, pkt->duration);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
|
|
|
|
{
|
2015-10-21 11:22:13 +02:00
|
|
|
R3DContext *r3d = s->priv_data;
|
|
|
|
AVStream *st;
|
2011-06-03 14:52:29 +03:00
|
|
|
int av_unused tmp, tmp2;
|
|
|
|
int samples, size;
|
2015-02-02 03:39:00 +02:00
|
|
|
int64_t pos = avio_tell(s->pb);
|
2009-01-20 09:42:14 +02:00
|
|
|
unsigned dts;
|
2010-03-02 23:32:29 +02:00
|
|
|
int ret;
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2015-10-21 11:22:13 +02:00
|
|
|
if (s->nb_streams < 2) {
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
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_ID_PCM_S32BE;
|
|
|
|
st->codecpar->channels = r3d->audio_channels;
|
2015-10-21 11:22:13 +02:00
|
|
|
avpriv_set_pts_info(st, 32, 1, s->streams[0]->time_base.den);
|
|
|
|
} else {
|
|
|
|
st = s->streams[1];
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
dts = avio_rb32(s->pb);
|
2009-01-20 09:42:14 +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
|
|
|
st->codecpar->sample_rate = avio_rb32(s->pb);
|
|
|
|
if (st->codecpar->sample_rate <= 0) {
|
2013-09-19 17:02:36 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Bad sample rate\n");
|
2013-01-29 19:13:45 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
samples = avio_rb32(s->pb);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "packet num %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2012-01-25 14:25:11 +03:00
|
|
|
tmp = avio_rb16(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_r8(s->pb); // major version
|
|
|
|
tmp2 = avio_r8(s->pb); // minor version
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
tmp = avio_rb32(s->pb); // unknown
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
2011-03-03 21:11:45 +02:00
|
|
|
size = atom->size - 8 - (avio_tell(s->pb) - pos);
|
2009-01-20 09:42:14 +02:00
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
2010-03-02 23:32:29 +02:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
|
|
|
|
return ret;
|
2009-01-20 09:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pkt->stream_index = 1;
|
|
|
|
pkt->dts = dts;
|
2021-02-11 00:21:53 +02:00
|
|
|
|
|
|
|
if (st->codecpar->sample_rate && samples > 0)
|
2016-04-10 21:58:15 +02:00
|
|
|
pkt->duration = av_rescale(samples, st->time_base.den, st->codecpar->sample_rate);
|
2015-09-26 18:13:55 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64" samples %d sample rate %d\n",
|
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
|
|
|
pkt->dts, pkt->duration, samples, st->codecpar->sample_rate);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2015-10-21 11:22:13 +02:00
|
|
|
R3DContext *r3d = s->priv_data;
|
2009-01-20 09:42:14 +02:00
|
|
|
Atom atom;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
while (!err) {
|
|
|
|
if (read_atom(s, &atom) < 0) {
|
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (atom.tag) {
|
|
|
|
case MKTAG('R','E','D','V'):
|
|
|
|
if (s->streams[0]->discard == AVDISCARD_ALL)
|
|
|
|
goto skip;
|
|
|
|
if (!(err = r3d_read_redv(s, pkt, &atom)))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case MKTAG('R','E','D','A'):
|
2015-10-21 11:22:13 +02:00
|
|
|
if (!r3d->audio_channels)
|
2009-01-20 09:42:14 +02:00
|
|
|
return -1;
|
2016-01-01 16:59:30 +02:00
|
|
|
if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
|
2009-01-20 09:42:14 +02:00
|
|
|
goto skip;
|
|
|
|
if (!(err = r3d_read_reda(s, pkt, &atom)))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
skip:
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, atom.size-8);
|
2009-01-20 09:42:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int r3d_probe(const AVProbeData *p)
|
2009-01-20 09:42:14 +02:00
|
|
|
{
|
|
|
|
if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
|
|
|
|
{
|
|
|
|
AVStream *st = s->streams[0]; // video stream
|
|
|
|
R3DContext *r3d = s->priv_data;
|
|
|
|
int frame_num;
|
|
|
|
|
2012-06-26 14:10:01 +03:00
|
|
|
if (!st->avg_frame_rate.num)
|
2009-01-20 09:42:14 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-03-02 16:08:11 +03:00
|
|
|
frame_num = av_rescale_q(sample_time, st->time_base,
|
2012-08-06 00:25:27 +03:00
|
|
|
av_inv_q(st->avg_frame_rate));
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "seek frame num %d timestamp %"PRId64"\n",
|
2011-05-31 22:23:45 +03:00
|
|
|
frame_num, sample_time);
|
2009-01-20 09:42:14 +02:00
|
|
|
|
|
|
|
if (frame_num < r3d->video_offsets_count) {
|
2011-09-14 20:32:29 +03:00
|
|
|
if (avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET) < 0)
|
|
|
|
return -1;
|
2009-01-20 09:42:14 +02:00
|
|
|
} else {
|
|
|
|
av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_r3d_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "r3d",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.priv_data_size = sizeof(R3DContext),
|
|
|
|
.read_probe = r3d_probe,
|
|
|
|
.read_header = r3d_read_header,
|
|
|
|
.read_packet = r3d_read_packet,
|
|
|
|
.read_seek = r3d_seek,
|
2009-01-20 09:42:14 +02:00
|
|
|
};
|