Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "internal.h"
26 
27 #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
28 
29 #define HNM4_SAMPLE_RATE 22050
30 #define HNM4_FRAME_FPS 24
31 
32 #define HNM4_CHUNK_ID_PL 19536
33 #define HNM4_CHUNK_ID_IZ 23113
34 #define HNM4_CHUNK_ID_IU 21833
35 #define HNM4_CHUNK_ID_SD 17491
36 
37 typedef struct Hnm4DemuxContext {
39  uint16_t width;
40  uint16_t height;
41  uint32_t filesize;
42  uint32_t frames;
43  uint32_t taboffset;
44  uint16_t bits;
45  uint16_t channels;
46  uint32_t framesize;
47  uint32_t currentframe;
48  int64_t pts;
52 
53 static int hnm_probe(AVProbeData *p)
54 {
55  if (p->buf_size < 4)
56  return 0;
57 
58  // check for HNM4 header.
59  // currently only HNM v4/v4A is supported
60  if (AV_RL32(&p->buf[0]) == HNM4_TAG)
61  return AVPROBE_SCORE_MAX;
62 
63  return 0;
64 }
65 
67 {
68  Hnm4DemuxContext *hnm = s->priv_data;
69  AVIOContext *pb = s->pb;
70  AVStream *vst;
71 
72  /* default context members */
73  hnm->pts = 0;
74  av_init_packet(&hnm->vpkt);
75  hnm->vpkt.data = NULL;
76  hnm->vpkt.size = 0;
77 
78  hnm->superchunk_remaining = 0;
79 
80  avio_skip(pb, 8);
81  hnm->width = avio_rl16(pb);
82  hnm->height = avio_rl16(pb);
83  hnm->filesize = avio_rl32(pb);
84  hnm->frames = avio_rl32(pb);
85  hnm->taboffset = avio_rl32(pb);
86  hnm->bits = avio_rl16(pb);
87  hnm->channels = avio_rl16(pb);
88  hnm->framesize = avio_rl32(pb);
89  avio_skip(pb, 32);
90 
91  hnm->currentframe = 0;
92 
93  if (hnm->width < 320 || hnm->width > 640 ||
94  hnm->height < 150 || hnm->height > 480) {
96  "invalid resolution: %ux%u\n", hnm->width, hnm->height);
97  return AVERROR_INVALIDDATA;
98  }
99 
100  // TODO: find a better way to detect HNM4A
101  if (hnm->width == 640)
102  hnm->version = 0x4a;
103  else
104  hnm->version = 0x40;
105 
106  if (!(vst = avformat_new_stream(s, NULL)))
107  return AVERROR(ENOMEM);
108 
111  vst->codec->codec_tag = 0;
112  vst->codec->width = hnm->width;
113  vst->codec->height = hnm->height;
114  vst->codec->extradata = av_mallocz(1);
115 
116  vst->codec->extradata_size = 1;
117  memcpy(vst->codec->extradata, &hnm->version, 1);
118 
119  vst->start_time = 0;
120 
121  avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
122 
123  return 0;
124 }
125 
127 {
128  Hnm4DemuxContext *hnm = s->priv_data;
129  AVIOContext *pb = s->pb;
130  int ret = 0;
131 
132  uint32_t superchunk_size, chunk_size;
133  uint16_t chunk_id;
134 
135  if (hnm->currentframe == hnm->frames || pb->eof_reached)
136  return AVERROR_EOF;
137 
138  if (hnm->superchunk_remaining == 0) {
139  /* parse next superchunk */
140  superchunk_size = avio_rl24(pb);
141  avio_skip(pb, 1);
142 
143  hnm->superchunk_remaining = superchunk_size - 4;
144  }
145 
146  chunk_size = avio_rl24(pb);
147  avio_skip(pb, 1);
148  chunk_id = avio_rl16(pb);
149  avio_skip(pb, 2);
150 
151  if (chunk_size > hnm->superchunk_remaining) {
152  av_log(s, AV_LOG_ERROR, "invalid chunk size: %u, offset: %u\n",
153  chunk_size, (int) avio_tell(pb));
154  avio_skip(pb, hnm->superchunk_remaining - 8);
155  hnm->superchunk_remaining = 0;
156  }
157 
158  switch (chunk_id) {
159  case HNM4_CHUNK_ID_PL:
160  case HNM4_CHUNK_ID_IZ:
161  case HNM4_CHUNK_ID_IU:
162  avio_seek(pb, -8, SEEK_CUR);
163  ret += av_get_packet(pb, pkt, chunk_size);
164  hnm->superchunk_remaining -= chunk_size;
165  if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
166  hnm->currentframe++;
167  break;
168 
169  case HNM4_CHUNK_ID_SD:
170  avio_skip(pb, chunk_size - 8);
171  hnm->superchunk_remaining -= chunk_size;
172  break;
173 
174  default:
175  av_log(s, AV_LOG_WARNING, "unknown chunk found: %d, offset: %d\n",
176  chunk_id, (int) avio_tell(pb));
177  avio_skip(pb, chunk_size - 8);
178  hnm->superchunk_remaining -= chunk_size;
179  break;
180  }
181 
182  return ret;
183 }
184 
186 {
187  Hnm4DemuxContext *hnm = s->priv_data;
188 
189  if (hnm->vpkt.size > 0)
190  av_free_packet(&hnm->vpkt);
191 
192  return 0;
193 }
194 
196  .name = "hnm",
197  .long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
198  .priv_data_size = sizeof(Hnm4DemuxContext),
204 };
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:413
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
uint8_t version
Definition: hnm.c:38
static int hnm_probe(AVProbeData *p)
Definition: hnm.c:53
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3208
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
#define HNM4_TAG
Definition: hnm.c:27
AVPacket vpkt
Definition: hnm.c:50
int64_t pts
Definition: hnm.c:48
uint16_t bits
Definition: hnm.c:44
Format I/O context.
Definition: avformat.h:871
uint32_t frames
Definition: hnm.c:42
uint16_t channels
Definition: hnm.c:45
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hnm.c:126
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define HNM4_CHUNK_ID_IZ
Definition: hnm.c:33
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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:118
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
#define HNM4_FRAME_FPS
Definition: hnm.c:30
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:558
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2662
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:391
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:390
uint32_t superchunk_remaining
Definition: hnm.c:49
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1217
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
uint32_t taboffset
Definition: hnm.c:43
AVInputFormat ff_hnm_demuxer
Definition: hnm.c:195
#define AV_RL32
Definition: intreadwrite.h:146
static int hnm_read_header(AVFormatContext *s)
Definition: hnm.c:66
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
Stream structure.
Definition: avformat.h:683
NULL
Definition: eval.c:55
static int hnm_read_close(AVFormatContext *s)
Definition: hnm.c:185
enum AVMediaType codec_type
Definition: avcodec.h:1062
enum AVCodecID codec_id
Definition: avcodec.h:1065
uint32_t framesize
Definition: hnm.c:46
AVIOContext * pb
I/O context.
Definition: avformat.h:913
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1080
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:414
#define HNM4_CHUNK_ID_PL
Definition: hnm.c:32
int extradata_size
Definition: avcodec.h:1163
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
uint16_t height
Definition: hnm.c:40
#define HNM4_CHUNK_ID_IU
Definition: hnm.c:34
This structure contains the data a format has to probe a file.
Definition: avformat.h:388
uint16_t width
Definition: hnm.c:39
uint32_t filesize
Definition: hnm.c:41
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:395
uint32_t currentframe
Definition: hnm.c:47
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:542
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:727
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:415
int eof_reached
true if eof reached
Definition: avio.h:96
void * priv_data
Format private data.
Definition: avformat.h:899
#define HNM4_CHUNK_ID_SD
Definition: hnm.c:35
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:516
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:550
This structure stores compressed data.
Definition: avcodec.h:950
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205