Mercurial > libavformat.hg
comparison thp.c @ 1974:51c5fe603dba libavformat
support for the THP game format by Marco Gerards, mgerards xs4all nl
author | diego |
---|---|
date | Sun, 01 Apr 2007 14:28:48 +0000 |
parents | |
children | deacffc2740e |
comparison
equal
deleted
inserted
replaced
1973:a2bb063b75b7 | 1974:51c5fe603dba |
---|---|
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 | |
23 #include "avformat.h" | |
24 #include "allformats.h" | |
25 | |
26 typedef struct ThpDemuxContext { | |
27 int version; | |
28 int first_frame; | |
29 int first_framesz; | |
30 int last_frame; | |
31 int compoff; | |
32 int framecnt; | |
33 AVRational fps; | |
34 int frame; | |
35 int next_frame; | |
36 int next_framesz; | |
37 int video_stream_index; | |
38 int compcount; | |
39 unsigned char components[16]; | |
40 AVStream* vst; | |
41 int has_audio; | |
42 } ThpDemuxContext; | |
43 | |
44 | |
45 static int thp_probe(AVProbeData *p) | |
46 { | |
47 /* check file header */ | |
48 if (p->buf_size < 4) | |
49 return 0; | |
50 | |
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 ByteIOContext *pb = &s->pb; | |
63 int i; | |
64 | |
65 /* Read the file header. */ | |
66 | |
67 get_be32(pb); /* Skip Magic. */ | |
68 thp->version = get_be32(pb); | |
69 | |
70 get_be32(pb); /* Max buf size. */ | |
71 get_be32(pb); /* Max samples. */ | |
72 | |
73 thp->fps = av_d2q(av_int2flt(get_be32(pb)), INT_MAX); | |
74 thp->framecnt = get_be32(pb); | |
75 thp->first_framesz = get_be32(pb); | |
76 get_be32(pb); /* Data size. */ | |
77 | |
78 thp->compoff = get_be32(pb); | |
79 get_be32(pb); /* offsetDataOffset. */ | |
80 thp->first_frame = get_be32(pb); | |
81 thp->last_frame = get_be32(pb); | |
82 | |
83 thp->next_framesz = thp->first_framesz; | |
84 thp->next_frame = thp->first_frame; | |
85 | |
86 /* Read the component structure. */ | |
87 url_fseek (pb, thp->compoff, SEEK_SET); | |
88 thp->compcount = get_be32(pb); | |
89 | |
90 /* Read the list of component types. */ | |
91 get_buffer(pb, thp->components, 16); | |
92 | |
93 for (i = 0; i < thp->compcount; i++) { | |
94 if (thp->components[i] == 0) { | |
95 if (thp->vst != 0) | |
96 break; | |
97 | |
98 /* Video component. */ | |
99 st = av_new_stream(s, 0); | |
100 if (!st) | |
101 return AVERROR_NOMEM; | |
102 | |
103 /* The denominator and numerator are switched because 1/fps | |
104 is required. */ | |
105 av_set_pts_info(st, 64, thp->fps.den, thp->fps.num); | |
106 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
107 st->codec->codec_id = CODEC_ID_THP; | |
108 st->codec->codec_tag = 0; /* no fourcc */ | |
109 st->codec->width = get_be32(pb); | |
110 st->codec->height = get_be32(pb); | |
111 st->codec->sample_rate = av_q2d(thp->fps); | |
112 thp->vst = st; | |
113 thp->video_stream_index = st->index; | |
114 | |
115 if (thp->version == 0x11000) | |
116 get_be32(pb); /* Unknown. */ | |
117 } | |
118 else if (thp->components[i] == 1) { | |
119 /* XXX: Required for audio playback. */ | |
120 thp->has_audio = 1; | |
121 } | |
122 } | |
123 | |
124 return 0; | |
125 } | |
126 | |
127 static int thp_read_packet(AVFormatContext *s, | |
128 AVPacket *pkt) | |
129 { | |
130 ThpDemuxContext *thp = s->priv_data; | |
131 ByteIOContext *pb = &s->pb; | |
132 int size; | |
133 int ret; | |
134 | |
135 /* Terminate when last frame is reached. */ | |
136 if (thp->frame >= thp->framecnt) | |
137 return AVERROR_IO; | |
138 | |
139 url_fseek(pb, thp->next_frame, SEEK_SET); | |
140 | |
141 /* Locate the next frame and read out its size. */ | |
142 thp->next_frame += thp->next_framesz; | |
143 thp->next_framesz = get_be32(pb); | |
144 | |
145 get_be32(pb); /* Previous total size. */ | |
146 size = get_be32(pb); /* Total size of this frame. */ | |
147 | |
148 if (thp->has_audio) | |
149 get_be32(pb); /* Audio size. */ | |
150 | |
151 ret = av_get_packet(pb, pkt, size); | |
152 if (ret != size) { | |
153 av_free_packet(pkt); | |
154 return AVERROR_IO; | |
155 } | |
156 | |
157 pkt->stream_index = thp->video_stream_index; | |
158 thp->frame++; | |
159 | |
160 return 0; | |
161 } | |
162 | |
163 AVInputFormat thp_demuxer = { | |
164 "thp", | |
165 "THP", | |
166 sizeof(ThpDemuxContext), | |
167 thp_probe, | |
168 thp_read_header, | |
169 thp_read_packet | |
170 }; |