2 * Cryo Interactive Entertainment HNM4 demuxer
4 * Copyright (c) 2012 David Kment
6 * This file is part of FFmpeg .
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.
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.
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
23 #include "libavutil/intreadwrite.h"
27 #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
29 #define HNM4_SAMPLE_RATE 22050
30 #define HNM4_FRAME_FPS 24
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
37 typedef struct Hnm4DemuxContext {
47 uint32_t currentframe;
49 uint32_t superchunk_remaining;
53 static int hnm_probe(AVProbeData *p)
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;
66 static int hnm_read_header(AVFormatContext *s)
68 Hnm4DemuxContext *hnm = s->priv_data;
69 AVIOContext *pb = s->pb;
72 /* default context members */
74 av_init_packet(&hnm->vpkt);
75 hnm->vpkt.data = NULL;
78 hnm->superchunk_remaining = 0;
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);
91 hnm->currentframe = 0;
93 if (hnm->width < 320 || hnm->width > 640 ||
94 hnm->height < 150 || hnm->height > 480) {
95 av_log(s, AV_LOG_ERROR,
96 "invalid resolution: %ux%u\n", hnm->width, hnm->height);
97 return AVERROR_INVALIDDATA;
100 // TODO: find a better way to detect HNM4A
101 if (hnm->width == 640)
106 if (!(vst = avformat_new_stream(s, NULL)))
107 return AVERROR(ENOMEM);
109 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
110 vst->codec->codec_id = AV_CODEC_ID_HNM4_VIDEO;
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);
116 vst->codec->extradata_size = 1;
117 memcpy(vst->codec->extradata, &hnm->version, 1);
121 avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
126 static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
128 Hnm4DemuxContext *hnm = s->priv_data;
129 AVIOContext *pb = s->pb;
132 uint32_t superchunk_size, chunk_size;
135 if (hnm->currentframe == hnm->frames || pb->eof_reached)
138 if (hnm->superchunk_remaining == 0) {
139 /* parse next superchunk */
140 superchunk_size = avio_rl24(pb);
143 hnm->superchunk_remaining = superchunk_size - 4;
146 chunk_size = avio_rl24(pb);
148 chunk_id = avio_rl16(pb);
151 if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
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;
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)
169 case HNM4_CHUNK_ID_SD:
170 avio_skip(pb, chunk_size - 8);
171 hnm->superchunk_remaining -= chunk_size;
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;
185 static int hnm_read_close(AVFormatContext *s)
187 Hnm4DemuxContext *hnm = s->priv_data;
189 if (hnm->vpkt.size > 0)
190 av_free_packet(&hnm->vpkt);
195 AVInputFormat ff_hnm_demuxer = {
197 .long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
198 .priv_data_size = sizeof(Hnm4DemuxContext),
199 .read_probe = hnm_probe,
200 .read_header = hnm_read_header,
201 .read_packet = hnm_read_packet,
202 .read_close = hnm_read_close,
203 .flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH