comparison yuv4mpeg.c @ 184:2438e76dde67 libavformat

yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
author michaelni
date Tue, 05 Aug 2003 09:32:31 +0000
parents d1290621cc6a
children 7865656658dc
comparison
equal deleted inserted replaced
183:b3b056122bd8 184:2438e76dde67
171 yuv4_write_packet, 171 yuv4_write_packet,
172 yuv4_write_trailer, 172 yuv4_write_trailer,
173 .flags = AVFMT_RAWPICTURE, 173 .flags = AVFMT_RAWPICTURE,
174 }; 174 };
175 175
176 176 #define MAX_YUV4_HEADER 50
177 #define MAX_FRAME_HEADER 10
178
179 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
180 {
181 char header[MAX_YUV4_HEADER+1];
182 int i;
183 ByteIOContext *pb = &s->pb;
184 int width, height, raten, rated, aspectn, aspectd;
185 char lacing;
186 AVStream *st;
187
188 for (i=0; i<MAX_YUV4_HEADER; i++) {
189 header[i] = get_byte(pb);
190 if (header[i] == '\n') {
191 header[i+1] = 0;
192 break;
193 }
194 }
195 if (i == MAX_YUV4_HEADER) return -1;
196 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
197 sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
198 &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
199
200 st = av_new_stream(s, 0);
201 st = s->streams[0];
202 st->codec.width = width;
203 st->codec.height = height;
204 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
205 st->codec.frame_rate = raten;
206 st->codec.frame_rate_base = rated;
207 st->codec.pix_fmt = PIX_FMT_YUV420P;
208 st->codec.codec_type = CODEC_TYPE_VIDEO;
209 st->codec.codec_id = CODEC_ID_RAWVIDEO;
210
211 return 0;
212 }
213
214 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
215 {
216 int i;
217 char header[MAX_FRAME_HEADER+1];
218 int packet_size, ret, width, height;
219 AVStream *st = s->streams[0];
220
221 for (i=0; i<MAX_FRAME_HEADER; i++) {
222 header[i] = get_byte(&s->pb);
223 if (header[i] == '\n') {
224 header[i+1] = 0;
225 break;
226 }
227 }
228 if (i == MAX_FRAME_HEADER) return -1;
229 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
230
231 width = st->codec.width;
232 height = st->codec.height;
233
234 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
235 if (packet_size < 0)
236 av_abort();
237
238 if (av_new_packet(pkt, packet_size) < 0)
239 return -EIO;
240
241 pkt->stream_index = 0;
242 ret = get_buffer(&s->pb, pkt->data, pkt->size);
243 if (ret != pkt->size) {
244 av_free_packet(pkt);
245 return -EIO;
246 } else {
247 return 0;
248 }
249 }
250
251 static int yuv4_read_close(AVFormatContext *s)
252 {
253 return 0;
254 }
255
256 AVInputFormat yuv4mpegpipe_iformat = {
257 "yuv4mpegpipe",
258 "YUV4MPEG pipe format",
259 0,
260 NULL,
261 yuv4_read_header,
262 yuv4_read_packet,
263 yuv4_read_close,
264 .extensions = "yuv4mpeg"
265 };
266
267 int yuv4mpeg_init(void)
268 {
269 av_register_input_format(&yuv4mpegpipe_iformat);
270 av_register_output_format(&yuv4mpegpipe_oformat);
271 return 0;
272 }
273