Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * THP Demuxer | ||
3 | * Copyright (c) 2007 Marco Gerards | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include "libavutil/intreadwrite.h" | ||
23 | #include "libavutil/intfloat.h" | ||
24 | #include "avformat.h" | ||
25 | #include "avio_internal.h" | ||
26 | #include "demux.h" | ||
27 | #include "internal.h" | ||
28 | |||
29 | typedef struct ThpDemuxContext { | ||
30 | int version; | ||
31 | unsigned first_frame; | ||
32 | unsigned first_framesz; | ||
33 | unsigned last_frame; | ||
34 | int compoff; | ||
35 | unsigned framecnt; | ||
36 | AVRational fps; | ||
37 | unsigned frame; | ||
38 | int64_t next_frame; | ||
39 | unsigned next_framesz; | ||
40 | int video_stream_index; | ||
41 | int audio_stream_index; | ||
42 | int compcount; | ||
43 | unsigned char components[16]; | ||
44 | AVStream* vst; | ||
45 | int has_audio; | ||
46 | unsigned audiosize; | ||
47 | } ThpDemuxContext; | ||
48 | |||
49 | |||
50 | 7186 | static int thp_probe(const AVProbeData *p) | |
51 | { | ||
52 | double d; | ||
53 | /* check file header */ | ||
54 |
2/2✓ Branch 0 taken 7184 times.
✓ Branch 1 taken 2 times.
|
7186 | if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0')) |
55 | 7184 | return 0; | |
56 | |||
57 | 2 | d = av_int2float(AV_RB32(p->buf + 16)); | |
58 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (d < 0.1 || d > 1000 || isnan(d)) |
59 | ✗ | return AVPROBE_SCORE_MAX/4; | |
60 | |||
61 | 2 | return AVPROBE_SCORE_MAX; | |
62 | } | ||
63 | |||
64 | 2 | static int thp_read_header(AVFormatContext *s) | |
65 | { | ||
66 | 2 | ThpDemuxContext *thp = s->priv_data; | |
67 | AVStream *st; | ||
68 | 2 | AVIOContext *pb = s->pb; | |
69 | 2 | int64_t fsize= avio_size(pb); | |
70 | uint32_t maxsize; | ||
71 | int i; | ||
72 | |||
73 | /* Read the file header. */ | ||
74 | 2 | avio_rb32(pb); /* Skip Magic. */ | |
75 | 2 | thp->version = avio_rb32(pb); | |
76 | |||
77 | 2 | avio_rb32(pb); /* Max buf size. */ | |
78 | 2 | avio_rb32(pb); /* Max samples. */ | |
79 | |||
80 | 2 | thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX); | |
81 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (thp->fps.den <= 0 || thp->fps.num < 0) |
82 | ✗ | return AVERROR_INVALIDDATA; | |
83 | 2 | thp->framecnt = avio_rb32(pb); | |
84 | 2 | thp->first_framesz = avio_rb32(pb); | |
85 | 2 | maxsize = avio_rb32(pb); | |
86 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | if (fsize > 0 && (!maxsize || fsize < maxsize)) |
87 | 2 | maxsize = fsize; | |
88 | 2 | ffiocontext(pb)->maxsize = fsize; | |
89 | |||
90 | 2 | thp->compoff = avio_rb32(pb); | |
91 | 2 | avio_rb32(pb); /* offsetDataOffset. */ | |
92 | 2 | thp->first_frame = avio_rb32(pb); | |
93 | 2 | thp->last_frame = avio_rb32(pb); | |
94 | |||
95 | 2 | thp->next_framesz = thp->first_framesz; | |
96 | 2 | thp->next_frame = thp->first_frame; | |
97 | |||
98 | /* Read the component structure. */ | ||
99 | 2 | avio_seek (pb, thp->compoff, SEEK_SET); | |
100 | 2 | thp->compcount = avio_rb32(pb); | |
101 | |||
102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (thp->compcount > FF_ARRAY_ELEMS(thp->components)) |
103 | ✗ | return AVERROR_INVALIDDATA; | |
104 | |||
105 | /* Read the list of component types. */ | ||
106 | 2 | avio_read(pb, thp->components, 16); | |
107 | |||
108 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (i = 0; i < thp->compcount; i++) { |
109 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (thp->components[i] == 0) { |
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (thp->vst) |
111 | ✗ | break; | |
112 | |||
113 | /* Video component. */ | ||
114 | 2 | st = avformat_new_stream(s, NULL); | |
115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
116 | ✗ | return AVERROR(ENOMEM); | |
117 | |||
118 | /* The denominator and numerator are switched because 1/fps | ||
119 | is required. */ | ||
120 | 2 | avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num); | |
121 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
122 | 2 | st->codecpar->codec_id = AV_CODEC_ID_THP; | |
123 | 2 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
124 | 2 | st->codecpar->width = avio_rb32(pb); | |
125 | 2 | st->codecpar->height = avio_rb32(pb); | |
126 | 2 | st->codecpar->sample_rate = av_q2d(thp->fps); | |
127 | 2 | st->nb_frames = | |
128 | 2 | st->duration = thp->framecnt; | |
129 | 2 | thp->vst = st; | |
130 | 2 | thp->video_stream_index = st->index; | |
131 | |||
132 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (thp->version == 0x11000) |
133 | 2 | avio_rb32(pb); /* Unknown. */ | |
134 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } else if (thp->components[i] == 1) { |
135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (thp->has_audio != 0) |
136 | ✗ | break; | |
137 | |||
138 | /* Audio component. */ | ||
139 | 2 | st = avformat_new_stream(s, NULL); | |
140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
141 | ✗ | return AVERROR(ENOMEM); | |
142 | |||
143 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
144 | 2 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP; | |
145 | 2 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
146 | 2 | st->codecpar->ch_layout.nb_channels = avio_rb32(pb); | |
147 | 2 | st->codecpar->sample_rate = avio_rb32(pb); /* Frequency. */ | |
148 | 2 | st->duration = avio_rb32(pb); | |
149 | |||
150 | 2 | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); | |
151 | |||
152 | 2 | thp->audio_stream_index = st->index; | |
153 | 2 | thp->has_audio = 1; | |
154 | } | ||
155 | } | ||
156 | |||
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!thp->vst) |
158 | ✗ | return AVERROR_INVALIDDATA; | |
159 | |||
160 | 2 | return 0; | |
161 | } | ||
162 | |||
163 | 288 | static int thp_read_packet(AVFormatContext *s, | |
164 | AVPacket *pkt) | ||
165 | { | ||
166 | 288 | ThpDemuxContext *thp = s->priv_data; | |
167 | 288 | AVIOContext *pb = s->pb; | |
168 | unsigned int size; | ||
169 | int ret; | ||
170 | |||
171 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 144 times.
|
288 | if (thp->audiosize == 0) { |
172 | /* Terminate when last frame is reached. */ | ||
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (thp->frame >= thp->framecnt) |
174 | ✗ | return AVERROR_EOF; | |
175 | |||
176 | 144 | avio_seek(pb, thp->next_frame, SEEK_SET); | |
177 | |||
178 | /* Locate the next frame and read out its size. */ | ||
179 | 144 | thp->next_frame += FFMAX(thp->next_framesz, 1); | |
180 | 144 | thp->next_framesz = avio_rb32(pb); | |
181 | |||
182 | 144 | avio_rb32(pb); /* Previous total size. */ | |
183 | 144 | size = avio_rb32(pb); /* Total size of this frame. */ | |
184 | |||
185 | /* Store the audiosize so the next time this function is called, | ||
186 | the audio can be read. */ | ||
187 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | if (thp->has_audio) |
188 | 144 | thp->audiosize = avio_rb32(pb); /* Audio size. */ | |
189 | else | ||
190 | ✗ | thp->frame++; | |
191 | |||
192 | 144 | ret = av_get_packet(pb, pkt, size); | |
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (ret < 0) |
194 | ✗ | return ret; | |
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (ret != size) { |
196 | ✗ | return AVERROR(EIO); | |
197 | } | ||
198 | |||
199 | 144 | pkt->stream_index = thp->video_stream_index; | |
200 | } else { | ||
201 | 144 | ret = av_get_packet(pb, pkt, thp->audiosize); | |
202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (ret < 0) |
203 | ✗ | return ret; | |
204 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 142 times.
|
144 | if (ret != thp->audiosize) { |
205 | 2 | return AVERROR(EIO); | |
206 | } | ||
207 | |||
208 | 142 | pkt->stream_index = thp->audio_stream_index; | |
209 |
1/2✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 | if (thp->audiosize >= 8) |
210 | 142 | pkt->duration = AV_RB32(&pkt->data[4]); | |
211 | |||
212 | 142 | thp->audiosize = 0; | |
213 | 142 | thp->frame++; | |
214 | } | ||
215 | |||
216 | 286 | return 0; | |
217 | } | ||
218 | |||
219 | const FFInputFormat ff_thp_demuxer = { | ||
220 | .p.name = "thp", | ||
221 | .p.long_name = NULL_IF_CONFIG_SMALL("THP"), | ||
222 | .priv_data_size = sizeof(ThpDemuxContext), | ||
223 | .read_probe = thp_probe, | ||
224 | .read_header = thp_read_header, | ||
225 | .read_packet = thp_read_packet | ||
226 | }; | ||
227 |