2013-10-28 03:38:02 +03:00
|
|
|
/*
|
|
|
|
* Cryo Interactive Entertainment HNM4 demuxer
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 David Kment
|
|
|
|
*
|
2013-11-01 12:39:50 +03:00
|
|
|
* This file is part of FFmpeg .
|
2013-10-28 03:38:02 +03:00
|
|
|
*
|
2013-11-01 12:39:50 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-10-28 03:38:02 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-11-01 12:39:50 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-10-28 03:38:02 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-11-01 12:39:50 +03:00
|
|
|
* License along with FFmpeg ; if not, write to the Free Software
|
2013-10-28 03:38:02 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2014-03-10 17:35:59 +03:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2013-10-28 03:38:02 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#define HNM4_TAG MKTAG('H', 'N', 'M', '4')
|
|
|
|
|
|
|
|
#define HNM4_SAMPLE_RATE 22050
|
|
|
|
#define HNM4_FRAME_FPS 24
|
|
|
|
|
|
|
|
#define HNM4_CHUNK_ID_PL 19536
|
|
|
|
#define HNM4_CHUNK_ID_IZ 23113
|
|
|
|
#define HNM4_CHUNK_ID_IU 21833
|
|
|
|
#define HNM4_CHUNK_ID_SD 17491
|
|
|
|
|
|
|
|
typedef struct Hnm4DemuxContext {
|
|
|
|
uint32_t frames;
|
|
|
|
uint32_t currentframe;
|
|
|
|
uint32_t superchunk_remaining;
|
|
|
|
} Hnm4DemuxContext;
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int hnm_probe(const AVProbeData *p)
|
2013-10-28 03:38:02 +03:00
|
|
|
{
|
|
|
|
if (p->buf_size < 4)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// check for HNM4 header.
|
|
|
|
// currently only HNM v4/v4A is supported
|
|
|
|
if (AV_RL32(&p->buf[0]) == HNM4_TAG)
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hnm_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
Hnm4DemuxContext *hnm = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
2020-03-21 07:17:36 +02:00
|
|
|
unsigned width, height;
|
2013-10-28 03:38:02 +03:00
|
|
|
AVStream *vst;
|
2020-03-21 08:31:17 +02:00
|
|
|
int ret;
|
2013-10-28 03:38:02 +03:00
|
|
|
|
|
|
|
avio_skip(pb, 8);
|
2020-03-21 07:17:36 +02:00
|
|
|
width = avio_rl16(pb);
|
|
|
|
height = avio_rl16(pb);
|
|
|
|
avio_rl32(pb); // filesize
|
2013-10-28 03:38:02 +03:00
|
|
|
hnm->frames = avio_rl32(pb);
|
2020-03-21 07:17:36 +02:00
|
|
|
avio_skip(pb, 44);
|
2013-10-28 03:38:02 +03:00
|
|
|
|
2020-03-21 07:17:36 +02:00
|
|
|
if (width < 256 || width > 640 ||
|
|
|
|
height < 150 || height > 480) {
|
2013-10-28 03:38:02 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2020-03-21 07:17:36 +02:00
|
|
|
"invalid resolution: %ux%u\n", width, height);
|
2013-10-28 03:38:02 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(vst = avformat_new_stream(s, NULL)))
|
|
|
|
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
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_HNM4_VIDEO;
|
|
|
|
vst->codecpar->codec_tag = 0;
|
2020-03-21 07:17:36 +02:00
|
|
|
vst->codecpar->width = width;
|
|
|
|
vst->codecpar->height = height;
|
2020-03-21 08:31:17 +02:00
|
|
|
if ((ret = ff_alloc_extradata(vst->codecpar, 1)) < 0)
|
|
|
|
return ret;
|
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
|
|
|
|
2020-03-21 07:17:36 +02:00
|
|
|
// TODO: find a better way to detect HNM4A
|
|
|
|
vst->codecpar->extradata[0] = width == 640 ? 0x4a : 0x40;
|
2013-10-28 03:38:02 +03:00
|
|
|
|
|
|
|
vst->start_time = 0;
|
|
|
|
|
|
|
|
avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
Hnm4DemuxContext *hnm = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
uint32_t superchunk_size, chunk_size;
|
|
|
|
uint16_t chunk_id;
|
|
|
|
|
|
|
|
if (hnm->currentframe == hnm->frames || pb->eof_reached)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
if (hnm->superchunk_remaining == 0) {
|
|
|
|
/* parse next superchunk */
|
|
|
|
superchunk_size = avio_rl24(pb);
|
|
|
|
avio_skip(pb, 1);
|
|
|
|
|
|
|
|
hnm->superchunk_remaining = superchunk_size - 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk_size = avio_rl24(pb);
|
|
|
|
avio_skip(pb, 1);
|
|
|
|
chunk_id = avio_rl16(pb);
|
|
|
|
avio_skip(pb, 2);
|
|
|
|
|
2013-11-23 01:44:57 +03:00
|
|
|
if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
|
2014-03-10 17:35:59 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"invalid chunk size: %"PRIu32", offset: %"PRId64"\n",
|
|
|
|
chunk_size, avio_tell(pb));
|
2013-10-28 03:38:02 +03:00
|
|
|
avio_skip(pb, hnm->superchunk_remaining - 8);
|
|
|
|
hnm->superchunk_remaining = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (chunk_id) {
|
|
|
|
case HNM4_CHUNK_ID_PL:
|
|
|
|
case HNM4_CHUNK_ID_IZ:
|
|
|
|
case HNM4_CHUNK_ID_IU:
|
|
|
|
avio_seek(pb, -8, SEEK_CUR);
|
|
|
|
ret += av_get_packet(pb, pkt, chunk_size);
|
|
|
|
hnm->superchunk_remaining -= chunk_size;
|
|
|
|
if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
|
|
|
|
hnm->currentframe++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HNM4_CHUNK_ID_SD:
|
|
|
|
avio_skip(pb, chunk_size - 8);
|
|
|
|
hnm->superchunk_remaining -= chunk_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-03-10 17:35:59 +03:00
|
|
|
av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n",
|
|
|
|
chunk_id, avio_tell(pb));
|
2013-10-28 03:38:02 +03:00
|
|
|
avio_skip(pb, chunk_size - 8);
|
|
|
|
hnm->superchunk_remaining -= chunk_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_hnm_demuxer = {
|
|
|
|
.name = "hnm",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
|
|
|
|
.priv_data_size = sizeof(Hnm4DemuxContext),
|
|
|
|
.read_probe = hnm_probe,
|
|
|
|
.read_header = hnm_read_header,
|
|
|
|
.read_packet = hnm_read_packet,
|
|
|
|
.flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH
|
|
|
|
};
|