Line | Branch | Exec | Source |
---|---|---|---|
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; | ||
43 | uint32_t superchunk_remaining; | ||
44 | } Hnm4DemuxContext; | ||
45 | |||
46 | 7186 | static int hnm_probe(const AVProbeData *p) | |
47 | { | ||
48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
|
7186 | if (p->buf_size < 4) |
49 | ✗ | return 0; | |
50 | |||
51 | // check for HNM4 header. | ||
52 | // currently only HNM v4/v4A is supported | ||
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
|
7186 | if (AV_RL32(&p->buf[0]) == HNM4_TAG) |
54 | ✗ | return AVPROBE_SCORE_MAX; | |
55 | |||
56 | 7186 | return 0; | |
57 | } | ||
58 | |||
59 | ✗ | static int hnm_read_header(AVFormatContext *s) | |
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) { | |
76 | ✗ | av_log(s, AV_LOG_ERROR, | |
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 | |||
84 | ✗ | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
85 | ✗ | vst->codecpar->codec_id = AV_CODEC_ID_HNM4_VIDEO; | |
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 | |||
97 | ✗ | avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS); | |
98 | |||
99 | ✗ | return 0; | |
100 | } | ||
101 | |||
102 | ✗ | static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
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 | ✗ | if (superchunk_size < 4) | |
118 | ✗ | return AVERROR_INVALIDDATA; | |
119 | ✗ | avio_skip(pb, 1); | |
120 | |||
121 | ✗ | hnm->superchunk_remaining = superchunk_size - 4; | |
122 | } | ||
123 | |||
124 | ✗ | chunk_size = avio_rl24(pb); | |
125 | ✗ | avio_skip(pb, 1); | |
126 | ✗ | chunk_id = avio_rl16(pb); | |
127 | ✗ | avio_skip(pb, 2); | |
128 | |||
129 | ✗ | if (chunk_size > hnm->superchunk_remaining || chunk_size < 8) { | |
130 | ✗ | av_log(s, AV_LOG_ERROR, | |
131 | "invalid chunk size: %"PRIu32", offset: %"PRId64"\n", | ||
132 | chunk_size, avio_tell(pb)); | ||
133 | ✗ | avio_skip(pb, hnm->superchunk_remaining - 8); | |
134 | ✗ | hnm->superchunk_remaining = 0; | |
135 | } | ||
136 | |||
137 | ✗ | switch (chunk_id) { | |
138 | ✗ | case HNM4_CHUNK_ID_PL: | |
139 | case HNM4_CHUNK_ID_IZ: | ||
140 | case HNM4_CHUNK_ID_IU: | ||
141 | ✗ | avio_seek(pb, -8, SEEK_CUR); | |
142 | ✗ | ret += av_get_packet(pb, pkt, chunk_size); | |
143 | ✗ | hnm->superchunk_remaining -= chunk_size; | |
144 | ✗ | if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU) | |
145 | ✗ | hnm->currentframe++; | |
146 | ✗ | break; | |
147 | |||
148 | ✗ | case HNM4_CHUNK_ID_SD: | |
149 | ✗ | avio_skip(pb, chunk_size - 8); | |
150 | ✗ | hnm->superchunk_remaining -= chunk_size; | |
151 | ✗ | break; | |
152 | |||
153 | ✗ | default: | |
154 | ✗ | av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n", | |
155 | chunk_id, avio_tell(pb)); | ||
156 | ✗ | avio_skip(pb, chunk_size - 8); | |
157 | ✗ | hnm->superchunk_remaining -= chunk_size; | |
158 | ✗ | break; | |
159 | } | ||
160 | |||
161 | ✗ | return ret; | |
162 | } | ||
163 | |||
164 | const FFInputFormat ff_hnm_demuxer = { | ||
165 | .p.name = "hnm", | ||
166 | .p.long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"), | ||
167 | .p.flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH, | ||
168 | .priv_data_size = sizeof(Hnm4DemuxContext), | ||
169 | .read_probe = hnm_probe, | ||
170 | .read_header = hnm_read_header, | ||
171 | .read_packet = hnm_read_packet, | ||
172 | }; | ||
173 |