]> git.sesse.net Git - ffmpeg/blob - libavformat/thp.c
7c3a1cd18d74001d897c0a899815e1f4ec627eac
[ffmpeg] / libavformat / thp.c
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 "internal.h"
26
27 typedef struct ThpDemuxContext {
28     int              version;
29     unsigned         data_size;
30     int              first_frame;
31     int              first_framesz;
32     int              last_frame;
33     int              compoff;
34     int              framecnt;
35     AVRational       fps;
36     int              frame;
37     int              next_frame;
38     int              next_framesz;
39     int              video_stream_index;
40     int              audio_stream_index;
41     int              compcount;
42     unsigned char    components[16];
43     AVStream*        vst;
44     int              has_audio;
45     unsigned         audiosize;
46 } ThpDemuxContext;
47
48
49 static int thp_probe(AVProbeData *p)
50 {
51     /* check file header */
52     if (AV_RL32(p->buf) == MKTAG('T', 'H', 'P', '\0'))
53         return AVPROBE_SCORE_MAX;
54     else
55         return 0;
56 }
57
58 static int thp_read_header(AVFormatContext *s,
59                            AVFormatParameters *ap)
60 {
61     ThpDemuxContext *thp = s->priv_data;
62     AVStream *st;
63     AVIOContext *pb = s->pb;
64     int64_t fsize= avio_size(pb);
65     int i;
66
67     /* Read the file header.  */
68                            avio_rb32(pb); /* Skip Magic.  */
69     thp->version         = avio_rb32(pb);
70
71                            avio_rb32(pb); /* Max buf size.  */
72                            avio_rb32(pb); /* Max samples.  */
73
74     thp->fps             = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
75     thp->framecnt        = avio_rb32(pb);
76     thp->first_framesz   = avio_rb32(pb);
77     thp->data_size       = avio_rb32(pb);
78     if(fsize>0 && (!thp->data_size || fsize < thp->data_size))
79         thp->data_size= fsize;
80
81     thp->compoff         = avio_rb32(pb);
82                            avio_rb32(pb); /* offsetDataOffset.  */
83     thp->first_frame     = avio_rb32(pb);
84     thp->last_frame      = avio_rb32(pb);
85
86     thp->next_framesz    = thp->first_framesz;
87     thp->next_frame      = thp->first_frame;
88
89     /* Read the component structure.  */
90     avio_seek (pb, thp->compoff, SEEK_SET);
91     thp->compcount       = avio_rb32(pb);
92
93     /* Read the list of component types.  */
94     avio_read(pb, thp->components, 16);
95
96     for (i = 0; i < thp->compcount; i++) {
97         if (thp->components[i] == 0) {
98             if (thp->vst != 0)
99                 break;
100
101             /* Video component.  */
102             st = avformat_new_stream(s, NULL);
103             if (!st)
104                 return AVERROR(ENOMEM);
105
106             /* The denominator and numerator are switched because 1/fps
107                is required.  */
108             avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
109             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
110             st->codec->codec_id = CODEC_ID_THP;
111             st->codec->codec_tag = 0;  /* no fourcc */
112             st->codec->width = avio_rb32(pb);
113             st->codec->height = avio_rb32(pb);
114             st->codec->sample_rate = av_q2d(thp->fps);
115             thp->vst = st;
116             thp->video_stream_index = st->index;
117
118             if (thp->version == 0x11000)
119                 avio_rb32(pb); /* Unknown.  */
120         } else if (thp->components[i] == 1) {
121             if (thp->has_audio != 0)
122                 break;
123
124             /* Audio component.  */
125             st = avformat_new_stream(s, NULL);
126             if (!st)
127                 return AVERROR(ENOMEM);
128
129             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
130             st->codec->codec_id = CODEC_ID_ADPCM_THP;
131             st->codec->codec_tag = 0;  /* no fourcc */
132             st->codec->channels    = avio_rb32(pb); /* numChannels.  */
133             st->codec->sample_rate = avio_rb32(pb); /* Frequency.  */
134
135             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
136
137             thp->audio_stream_index = st->index;
138             thp->has_audio = 1;
139         }
140     }
141
142     return 0;
143 }
144
145 static int thp_read_packet(AVFormatContext *s,
146                             AVPacket *pkt)
147 {
148     ThpDemuxContext *thp = s->priv_data;
149     AVIOContext *pb = s->pb;
150     unsigned int size;
151     int ret;
152
153     if (thp->audiosize == 0) {
154         /* Terminate when last frame is reached.  */
155         if (thp->frame >= thp->framecnt)
156             return AVERROR(EIO);
157
158         avio_seek(pb, thp->next_frame, SEEK_SET);
159
160         /* Locate the next frame and read out its size.  */
161         thp->next_frame += thp->next_framesz;
162         thp->next_framesz = avio_rb32(pb);
163
164                         avio_rb32(pb); /* Previous total size.  */
165         size          = avio_rb32(pb); /* Total size of this frame.  */
166         if(thp->data_size && avio_tell(pb) + size > thp->data_size) {
167             av_log(s, AV_LOG_ERROR, "Video packet truncated\n");
168             size= thp->data_size - avio_tell(pb);
169         }
170
171         /* Store the audiosize so the next time this function is called,
172            the audio can be read.  */
173         if (thp->has_audio)
174             thp->audiosize = avio_rb32(pb); /* Audio size.  */
175         else
176             thp->frame++;
177
178         ret = av_get_packet(pb, pkt, size);
179         if (ret != size) {
180             av_free_packet(pkt);
181             return AVERROR(EIO);
182         }
183
184         pkt->stream_index = thp->video_stream_index;
185     } else {
186         if(thp->data_size && avio_tell(pb) + thp->audiosize > thp->data_size){
187             av_log(s, AV_LOG_ERROR, "Audio packet truncated\n");
188             thp->audiosize= thp->data_size - avio_tell(pb);
189         }
190
191         ret = av_get_packet(pb, pkt, thp->audiosize);
192         if (ret != thp->audiosize) {
193             av_free_packet(pkt);
194             return AVERROR(EIO);
195         }
196
197         pkt->stream_index = thp->audio_stream_index;
198         thp->audiosize = 0;
199         thp->frame++;
200     }
201
202     return 0;
203 }
204
205 AVInputFormat ff_thp_demuxer = {
206     .name           = "thp",
207     .long_name      = NULL_IF_CONFIG_SMALL("THP"),
208     .priv_data_size = sizeof(ThpDemuxContext),
209     .read_probe     = thp_probe,
210     .read_header    = thp_read_header,
211     .read_packet    = thp_read_packet
212 };