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