]> git.sesse.net Git - ffmpeg/blob - libavformat/hnm.c
f06add5cf83775a003c6592937b9fc956ca3cb97
[ffmpeg] / libavformat / hnm.c
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 "internal.h"
28
29 #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
30
31 #define HNM4_SAMPLE_RATE 22050
32 #define HNM4_FRAME_FPS 24
33
34 #define HNM4_CHUNK_ID_PL 19536
35 #define HNM4_CHUNK_ID_IZ 23113
36 #define HNM4_CHUNK_ID_IU 21833
37 #define HNM4_CHUNK_ID_SD 17491
38
39 typedef struct Hnm4DemuxContext {
40     uint32_t frames;
41     uint32_t currentframe;
42     uint32_t superchunk_remaining;
43 } Hnm4DemuxContext;
44
45 static int hnm_probe(const AVProbeData *p)
46 {
47     if (p->buf_size < 4)
48         return 0;
49
50     // check for HNM4 header.
51     // currently only HNM v4/v4A is supported
52     if (AV_RL32(&p->buf[0]) == HNM4_TAG)
53         return AVPROBE_SCORE_MAX;
54
55     return 0;
56 }
57
58 static int hnm_read_header(AVFormatContext *s)
59 {
60     Hnm4DemuxContext *hnm = s->priv_data;
61     AVIOContext *pb = s->pb;
62     unsigned width, height;
63     AVStream *vst;
64     int ret;
65
66     avio_skip(pb, 8);
67     width          = avio_rl16(pb);
68     height         = avio_rl16(pb);
69     avio_rl32(pb); // filesize
70     hnm->frames    = avio_rl32(pb);
71     avio_skip(pb, 44);
72
73     if (width  < 256 || width  > 640 ||
74         height < 150 || height > 480) {
75         av_log(s, AV_LOG_ERROR,
76                "invalid resolution: %ux%u\n", width, height);
77         return AVERROR_INVALIDDATA;
78     }
79
80     if (!(vst = avformat_new_stream(s, NULL)))
81         return AVERROR(ENOMEM);
82
83     vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
84     vst->codecpar->codec_id   = AV_CODEC_ID_HNM4_VIDEO;
85     vst->codecpar->codec_tag  = 0;
86     vst->codecpar->width      = width;
87     vst->codecpar->height     = height;
88     if ((ret = ff_alloc_extradata(vst->codecpar, 1)) < 0)
89         return ret;
90
91     // TODO: find a better way to detect HNM4A
92     vst->codecpar->extradata[0] = width == 640 ? 0x4a : 0x40;
93
94     vst->start_time = 0;
95
96     avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
97
98     return 0;
99 }
100
101 static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
102 {
103     Hnm4DemuxContext *hnm = s->priv_data;
104     AVIOContext *pb = s->pb;
105     int ret = 0;
106
107     uint32_t superchunk_size, chunk_size;
108     uint16_t chunk_id;
109
110     if (hnm->currentframe == hnm->frames || pb->eof_reached)
111         return AVERROR_EOF;
112
113     if (hnm->superchunk_remaining == 0) {
114         /* parse next superchunk */
115         superchunk_size = avio_rl24(pb);
116         avio_skip(pb, 1);
117
118         hnm->superchunk_remaining = superchunk_size - 4;
119     }
120
121     chunk_size = avio_rl24(pb);
122     avio_skip(pb, 1);
123     chunk_id = avio_rl16(pb);
124     avio_skip(pb, 2);
125
126     if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
127         av_log(s, AV_LOG_ERROR,
128                "invalid chunk size: %"PRIu32", offset: %"PRId64"\n",
129                chunk_size, avio_tell(pb));
130         avio_skip(pb, hnm->superchunk_remaining - 8);
131         hnm->superchunk_remaining = 0;
132     }
133
134     switch (chunk_id) {
135     case HNM4_CHUNK_ID_PL:
136     case HNM4_CHUNK_ID_IZ:
137     case HNM4_CHUNK_ID_IU:
138         avio_seek(pb, -8, SEEK_CUR);
139         ret += av_get_packet(pb, pkt, chunk_size);
140         hnm->superchunk_remaining -= chunk_size;
141         if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
142             hnm->currentframe++;
143         break;
144
145     case HNM4_CHUNK_ID_SD:
146         avio_skip(pb, chunk_size - 8);
147         hnm->superchunk_remaining -= chunk_size;
148         break;
149
150     default:
151         av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n",
152                chunk_id, avio_tell(pb));
153         avio_skip(pb, chunk_size - 8);
154         hnm->superchunk_remaining -= chunk_size;
155         break;
156     }
157
158     return ret;
159 }
160
161 AVInputFormat ff_hnm_demuxer = {
162     .name           = "hnm",
163     .long_name      = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
164     .priv_data_size = sizeof(Hnm4DemuxContext),
165     .read_probe     = hnm_probe,
166     .read_header    = hnm_read_header,
167     .read_packet    = hnm_read_packet,
168     .flags          = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH
169 };