3 * Copyright (c) 2007 Marco Gerards
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/intfloat.h"
27 typedef struct ThpDemuxContext {
30 unsigned first_framesz;
37 unsigned next_framesz;
38 int video_stream_index;
39 int audio_stream_index;
41 unsigned char components[16];
48 static int thp_probe(const AVProbeData *p)
51 /* check file header */
52 if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0'))
55 d = av_int2float(AV_RB32(p->buf + 16));
56 if (d < 0.1 || d > 1000 || isnan(d))
57 return AVPROBE_SCORE_MAX/4;
59 return AVPROBE_SCORE_MAX;
62 static int thp_read_header(AVFormatContext *s)
64 ThpDemuxContext *thp = s->priv_data;
66 AVIOContext *pb = s->pb;
67 int64_t fsize= avio_size(pb);
70 /* Read the file header. */
71 avio_rb32(pb); /* Skip Magic. */
72 thp->version = avio_rb32(pb);
74 avio_rb32(pb); /* Max buf size. */
75 avio_rb32(pb); /* Max samples. */
77 thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
78 if (thp->fps.den <= 0 || thp->fps.num < 0)
79 return AVERROR_INVALIDDATA;
80 thp->framecnt = avio_rb32(pb);
81 thp->first_framesz = avio_rb32(pb);
82 pb->maxsize = avio_rb32(pb);
83 if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
86 thp->compoff = avio_rb32(pb);
87 avio_rb32(pb); /* offsetDataOffset. */
88 thp->first_frame = avio_rb32(pb);
89 thp->last_frame = avio_rb32(pb);
91 thp->next_framesz = thp->first_framesz;
92 thp->next_frame = thp->first_frame;
94 /* Read the component structure. */
95 avio_seek (pb, thp->compoff, SEEK_SET);
96 thp->compcount = avio_rb32(pb);
98 if (thp->compcount > FF_ARRAY_ELEMS(thp->components))
99 return AVERROR_INVALIDDATA;
101 /* Read the list of component types. */
102 avio_read(pb, thp->components, 16);
104 for (i = 0; i < thp->compcount; i++) {
105 if (thp->components[i] == 0) {
109 /* Video component. */
110 st = avformat_new_stream(s, NULL);
112 return AVERROR(ENOMEM);
114 /* The denominator and numerator are switched because 1/fps
116 avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
117 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
118 st->codecpar->codec_id = AV_CODEC_ID_THP;
119 st->codecpar->codec_tag = 0; /* no fourcc */
120 st->codecpar->width = avio_rb32(pb);
121 st->codecpar->height = avio_rb32(pb);
122 st->codecpar->sample_rate = av_q2d(thp->fps);
124 st->duration = thp->framecnt;
126 thp->video_stream_index = st->index;
128 if (thp->version == 0x11000)
129 avio_rb32(pb); /* Unknown. */
130 } else if (thp->components[i] == 1) {
131 if (thp->has_audio != 0)
134 /* Audio component. */
135 st = avformat_new_stream(s, NULL);
137 return AVERROR(ENOMEM);
139 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
140 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP;
141 st->codecpar->codec_tag = 0; /* no fourcc */
142 st->codecpar->channels = avio_rb32(pb); /* numChannels. */
143 st->codecpar->sample_rate = avio_rb32(pb); /* Frequency. */
144 st->duration = avio_rb32(pb);
146 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
148 thp->audio_stream_index = st->index;
154 return AVERROR_INVALIDDATA;
159 static int thp_read_packet(AVFormatContext *s,
162 ThpDemuxContext *thp = s->priv_data;
163 AVIOContext *pb = s->pb;
167 if (thp->audiosize == 0) {
168 /* Terminate when last frame is reached. */
169 if (thp->frame >= thp->framecnt)
172 avio_seek(pb, thp->next_frame, SEEK_SET);
174 /* Locate the next frame and read out its size. */
175 thp->next_frame += FFMAX(thp->next_framesz, 1);
176 thp->next_framesz = avio_rb32(pb);
178 avio_rb32(pb); /* Previous total size. */
179 size = avio_rb32(pb); /* Total size of this frame. */
181 /* Store the audiosize so the next time this function is called,
182 the audio can be read. */
184 thp->audiosize = avio_rb32(pb); /* Audio size. */
188 ret = av_get_packet(pb, pkt, size);
195 pkt->stream_index = thp->video_stream_index;
197 ret = av_get_packet(pb, pkt, thp->audiosize);
200 if (ret != thp->audiosize) {
204 pkt->stream_index = thp->audio_stream_index;
205 if (thp->audiosize >= 8)
206 pkt->duration = AV_RB32(&pkt->data[4]);
215 const AVInputFormat ff_thp_demuxer = {
217 .long_name = NULL_IF_CONFIG_SMALL("THP"),
218 .priv_data_size = sizeof(ThpDemuxContext),
219 .read_probe = thp_probe,
220 .read_header = thp_read_header,
221 .read_packet = thp_read_packet