Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * RAW encoder and decoder | |
3 | * Copyright (c) 2001 Gerard Lantau. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | #include <stdlib.h> | |
20 | #include <stdio.h> | |
21 | #include <netinet/in.h> | |
22 | #include <string.h> | |
23 | #include <errno.h> | |
24 | #include "avformat.h" | |
25 | ||
26 | /* simple formats */ | |
27 | int raw_write_header(struct AVFormatContext *s) | |
28 | { | |
29 | return 0; | |
30 | } | |
31 | ||
32 | int raw_write_packet(struct AVFormatContext *s, | |
33 | int stream_index, | |
34 | unsigned char *buf, int size) | |
35 | { | |
36 | put_buffer(&s->pb, buf, size); | |
37 | put_flush_packet(&s->pb); | |
38 | return 0; | |
39 | } | |
40 | ||
41 | int raw_write_trailer(struct AVFormatContext *s) | |
42 | { | |
43 | return 0; | |
44 | } | |
45 | ||
46 | /* raw input */ | |
47 | static int raw_read_header(AVFormatContext *s, | |
48 | AVFormatParameters *ap) | |
49 | { | |
50 | AVStream *st; | |
51 | ||
52 | st = malloc(sizeof(AVStream)); | |
53 | if (!st) | |
54 | return -1; | |
55 | s->nb_streams = 1; | |
56 | s->streams[0] = st; | |
57 | ||
58 | st->id = 0; | |
59 | ||
60 | if (ap) { | |
61 | if (s->format->audio_codec != CODEC_ID_NONE) { | |
62 | st->codec.codec_type = CODEC_TYPE_AUDIO; | |
63 | st->codec.codec_id = s->format->audio_codec; | |
64 | } else if (s->format->video_codec != CODEC_ID_NONE) { | |
65 | st->codec.codec_type = CODEC_TYPE_VIDEO; | |
66 | st->codec.codec_id = s->format->video_codec; | |
67 | } else { | |
68 | free(st); | |
69 | return -1; | |
70 | } | |
71 | ||
72 | switch(st->codec.codec_type) { | |
73 | case CODEC_TYPE_AUDIO: | |
74 | st->codec.sample_rate = ap->sample_rate; | |
75 | st->codec.channels = ap->channels; | |
76 | /* XXX: endianness */ | |
77 | break; | |
78 | case CODEC_TYPE_VIDEO: | |
79 | st->codec.frame_rate = ap->frame_rate; | |
80 | st->codec.width = ap->width; | |
81 | st->codec.height = ap->height; | |
82 | break; | |
83 | default: | |
84 | abort(); | |
85 | break; | |
86 | } | |
87 | } else { | |
88 | abort(); | |
89 | } | |
90 | return 0; | |
91 | } | |
92 | ||
93 | #define MIN_SIZE 1024 | |
94 | ||
95 | int raw_read_packet(AVFormatContext *s, | |
96 | AVPacket *pkt) | |
97 | { | |
98 | int packet_size, n, ret; | |
99 | ||
100 | if (url_feof(&s->pb)) | |
101 | return -EIO; | |
102 | ||
103 | packet_size = url_get_packet_size(&s->pb); | |
104 | n = MIN_SIZE / packet_size; | |
105 | if (n <= 0) | |
106 | n = 1; | |
107 | if (av_new_packet(pkt, n * packet_size) < 0) | |
108 | return -EIO; | |
109 | ||
110 | pkt->stream_index = 0; | |
111 | ret = get_buffer(&s->pb, pkt->data, pkt->size); | |
112 | if (ret < 0) | |
113 | av_free_packet(pkt); | |
114 | return ret; | |
115 | } | |
116 | ||
117 | int raw_read_close(AVFormatContext *s) | |
118 | { | |
119 | return 0; | |
120 | } | |
121 | ||
122 | /* mp3 read */ | |
123 | static int mp3_read_header(AVFormatContext *s, | |
124 | AVFormatParameters *ap) | |
125 | { | |
126 | AVStream *st; | |
127 | ||
128 | st = malloc(sizeof(AVStream)); | |
129 | if (!st) | |
130 | return -1; | |
131 | s->nb_streams = 1; | |
132 | s->streams[0] = st; | |
133 | ||
134 | st->id = 0; | |
135 | ||
136 | st->codec.codec_type = CODEC_TYPE_AUDIO; | |
137 | st->codec.codec_id = CODEC_ID_MP2; | |
138 | /* XXX: read the first frame and extract rate and channels */ | |
139 | st->codec.sample_rate = 44100; | |
140 | st->codec.channels = 2; | |
141 | return 0; | |
142 | } | |
143 | ||
144 | /* mpeg1/h263 input */ | |
145 | static int video_read_header(AVFormatContext *s, | |
146 | AVFormatParameters *ap) | |
147 | { | |
148 | AVStream *st; | |
149 | ||
150 | st = av_mallocz(sizeof(AVStream)); | |
151 | if (!st) | |
152 | return -1; | |
153 | s->nb_streams = 1; | |
154 | s->streams[0] = st; | |
155 | ||
156 | st->codec.codec_type = CODEC_TYPE_VIDEO; | |
157 | st->codec.codec_id = s->format->video_codec; | |
158 | return 0; | |
159 | } | |
160 | ||
161 | AVFormat mp2_format = { | |
162 | "mp2", | |
163 | "MPEG audio", | |
164 | "audio/x-mpeg", | |
165 | "mp2,mp3", | |
166 | CODEC_ID_MP2, | |
167 | 0, | |
168 | raw_write_header, | |
169 | raw_write_packet, | |
170 | raw_write_trailer, | |
171 | ||
172 | mp3_read_header, | |
173 | raw_read_packet, | |
174 | raw_read_close, | |
175 | }; | |
176 | ||
177 | AVFormat ac3_format = { | |
178 | "ac3", | |
179 | "raw ac3", | |
180 | "audio/x-ac3", | |
181 | "ac3", | |
182 | CODEC_ID_AC3, | |
183 | 0, | |
184 | raw_write_header, | |
185 | raw_write_packet, | |
186 | raw_write_trailer, | |
187 | }; | |
188 | ||
189 | AVFormat h263_format = { | |
190 | "h263", | |
191 | "raw h263", | |
192 | "video/x-h263", | |
193 | "h263", | |
194 | 0, | |
195 | CODEC_ID_H263, | |
196 | raw_write_header, | |
197 | raw_write_packet, | |
198 | raw_write_trailer, | |
199 | video_read_header, | |
200 | raw_read_packet, | |
201 | raw_read_close, | |
202 | }; | |
203 | ||
204 | AVFormat mpeg1video_format = { | |
205 | "mpegvideo", | |
206 | "MPEG video", | |
207 | "video/x-mpeg", | |
208 | "mpg,mpeg", | |
209 | 0, | |
210 | CODEC_ID_MPEG1VIDEO, | |
211 | raw_write_header, | |
212 | raw_write_packet, | |
213 | raw_write_trailer, | |
214 | video_read_header, | |
215 | raw_read_packet, | |
216 | raw_read_close, | |
217 | }; | |
218 | ||
219 | AVFormat pcm_format = { | |
220 | "pcm", | |
221 | "pcm raw format", | |
222 | NULL, | |
223 | "sw", | |
224 | CODEC_ID_PCM, | |
225 | 0, | |
226 | raw_write_header, | |
227 | raw_write_packet, | |
228 | raw_write_trailer, | |
229 | ||
230 | raw_read_header, | |
231 | raw_read_packet, | |
232 | raw_read_close, | |
233 | }; | |
234 | ||
235 | int rawvideo_read_packet(AVFormatContext *s, | |
236 | AVPacket *pkt) | |
237 | { | |
238 | int packet_size, ret, width, height; | |
239 | AVStream *st = s->streams[0]; | |
240 | ||
241 | width = st->codec.width; | |
242 | height = st->codec.height; | |
243 | ||
244 | switch(st->codec.pix_fmt) { | |
245 | case PIX_FMT_YUV420P: | |
246 | packet_size = (width * height * 3) / 2; | |
247 | break; | |
248 | case PIX_FMT_YUV422: | |
249 | packet_size = (width * height * 2); | |
250 | break; | |
251 | case PIX_FMT_BGR24: | |
252 | case PIX_FMT_RGB24: | |
253 | packet_size = (width * height * 3); | |
254 | break; | |
255 | default: | |
256 | abort(); | |
257 | break; | |
258 | } | |
259 | ||
260 | if (av_new_packet(pkt, packet_size) < 0) | |
261 | return -EIO; | |
262 | ||
263 | pkt->stream_index = 0; | |
264 | /* bypass buffered I/O */ | |
265 | ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size); | |
266 | if (ret != pkt->size) { | |
267 | av_free_packet(pkt); | |
268 | return -EIO; | |
269 | } else { | |
270 | return 0; | |
271 | } | |
272 | } | |
273 | ||
274 | AVFormat rawvideo_format = { | |
275 | "rawvideo", | |
276 | "raw video format", | |
277 | NULL, | |
278 | "yuv", | |
279 | CODEC_ID_NONE, | |
280 | CODEC_ID_RAWVIDEO, | |
281 | raw_write_header, | |
282 | raw_write_packet, | |
283 | raw_write_trailer, | |
284 | ||
285 | raw_read_header, | |
286 | rawvideo_read_packet, | |
287 | raw_read_close, | |
288 | }; |