FFmpeg
hnm.c
Go to the documentation of this file.
1 /*
2  * Cryo Interactive Entertainment HNM4 demuxer
3  *
4  * Copyright (c) 2012 David Kment
5  *
6  * This file is part of FFmpeg .
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg ; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <inttypes.h>
24 
25 #include "libavutil/intreadwrite.h"
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29 
30 #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
31 
32 #define HNM4_SAMPLE_RATE 22050
33 #define HNM4_FRAME_FPS 24
34 
35 #define HNM4_CHUNK_ID_PL 19536
36 #define HNM4_CHUNK_ID_IZ 23113
37 #define HNM4_CHUNK_ID_IU 21833
38 #define HNM4_CHUNK_ID_SD 17491
39 
40 typedef struct Hnm4DemuxContext {
41  uint32_t frames;
42  uint32_t currentframe;
45 
46 static int hnm_probe(const AVProbeData *p)
47 {
48  if (p->buf_size < 4)
49  return 0;
50 
51  // check for HNM4 header.
52  // currently only HNM v4/v4A is supported
53  if (AV_RL32(&p->buf[0]) == HNM4_TAG)
54  return AVPROBE_SCORE_MAX;
55 
56  return 0;
57 }
58 
60 {
61  Hnm4DemuxContext *hnm = s->priv_data;
62  AVIOContext *pb = s->pb;
63  unsigned width, height;
64  AVStream *vst;
65  int ret;
66 
67  avio_skip(pb, 8);
68  width = avio_rl16(pb);
69  height = avio_rl16(pb);
70  avio_rl32(pb); // filesize
71  hnm->frames = avio_rl32(pb);
72  avio_skip(pb, 44);
73 
74  if (width < 256 || width > 640 ||
75  height < 150 || height > 480) {
77  "invalid resolution: %ux%u\n", width, height);
78  return AVERROR_INVALIDDATA;
79  }
80 
81  if (!(vst = avformat_new_stream(s, NULL)))
82  return AVERROR(ENOMEM);
83 
86  vst->codecpar->codec_tag = 0;
87  vst->codecpar->width = width;
88  vst->codecpar->height = height;
89  if ((ret = ff_alloc_extradata(vst->codecpar, 1)) < 0)
90  return ret;
91 
92  // TODO: find a better way to detect HNM4A
93  vst->codecpar->extradata[0] = width == 640 ? 0x4a : 0x40;
94 
95  vst->start_time = 0;
96 
98 
99  return 0;
100 }
101 
103 {
104  Hnm4DemuxContext *hnm = s->priv_data;
105  AVIOContext *pb = s->pb;
106  int ret = 0;
107 
108  uint32_t superchunk_size, chunk_size;
109  uint16_t chunk_id;
110 
111  if (hnm->currentframe == hnm->frames || pb->eof_reached)
112  return AVERROR_EOF;
113 
114  if (hnm->superchunk_remaining == 0) {
115  /* parse next superchunk */
116  superchunk_size = avio_rl24(pb);
117  avio_skip(pb, 1);
118 
119  hnm->superchunk_remaining = superchunk_size - 4;
120  }
121 
122  chunk_size = avio_rl24(pb);
123  avio_skip(pb, 1);
124  chunk_id = avio_rl16(pb);
125  avio_skip(pb, 2);
126 
127  if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
129  "invalid chunk size: %"PRIu32", offset: %"PRId64"\n",
130  chunk_size, avio_tell(pb));
131  avio_skip(pb, hnm->superchunk_remaining - 8);
132  hnm->superchunk_remaining = 0;
133  }
134 
135  switch (chunk_id) {
136  case HNM4_CHUNK_ID_PL:
137  case HNM4_CHUNK_ID_IZ:
138  case HNM4_CHUNK_ID_IU:
139  avio_seek(pb, -8, SEEK_CUR);
140  ret += av_get_packet(pb, pkt, chunk_size);
141  hnm->superchunk_remaining -= chunk_size;
142  if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
143  hnm->currentframe++;
144  break;
145 
146  case HNM4_CHUNK_ID_SD:
147  avio_skip(pb, chunk_size - 8);
148  hnm->superchunk_remaining -= chunk_size;
149  break;
150 
151  default:
152  av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n",
153  chunk_id, avio_tell(pb));
154  avio_skip(pb, chunk_size - 8);
155  hnm->superchunk_remaining -= chunk_size;
156  break;
157  }
158 
159  return ret;
160 }
161 
163  .p.name = "hnm",
164  .p.long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
166  .priv_data_size = sizeof(Hnm4DemuxContext),
170 };
HNM4_CHUNK_ID_IZ
#define HNM4_CHUNK_ID_IZ
Definition: hnm.c:36
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
HNM4_CHUNK_ID_SD
#define HNM4_CHUNK_ID_SD
Definition: hnm.c:38
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:487
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
HNM4_FRAME_FPS
#define HNM4_FRAME_FPS
Definition: hnm.c:33
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
HNM4_CHUNK_ID_PL
#define HNM4_CHUNK_ID_PL
Definition: hnm.c:35
AVFMT_NOBINSEARCH
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:485
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
hnm_probe
static int hnm_probe(const AVProbeData *p)
Definition: hnm.c:46
HNM4_CHUNK_ID_IU
#define HNM4_CHUNK_ID_IU
Definition: hnm.c:37
Hnm4DemuxContext::frames
uint32_t frames
Definition: hnm.c:41
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
Hnm4DemuxContext::superchunk_remaining
uint32_t superchunk_remaining
Definition: hnm.c:43
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
hnm_read_header
static int hnm_read_header(AVFormatContext *s)
Definition: hnm.c:59
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
height
#define height
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:722
AVCodecParameters::height
int height
Definition: codec_par.h:135
AV_CODEC_ID_HNM4_VIDEO
@ AV_CODEC_ID_HNM4_VIDEO
Definition: codec_id.h:225
demux.h
Hnm4DemuxContext::currentframe
uint32_t currentframe
Definition: hnm.c:42
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:104
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
HNM4_TAG
#define HNM4_TAG
Definition: hnm.c:30
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
ff_hnm_demuxer
const FFInputFormat ff_hnm_demuxer
Definition: hnm.c:162
AVFMT_NOGENSEARCH
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:486
AVIOContext::eof_reached
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:238
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
FFInputFormat
Definition: demux.h:37
Hnm4DemuxContext
Definition: hnm.c:40
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:792
hnm_read_packet
static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hnm.c:102
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:240