]> git.sesse.net Git - ffmpeg/blob - libavformat/raw.c
518206ea563532e33a14341295d1ead62a2fe68d
[ffmpeg] / libavformat / raw.c
1 /* 
2  * RAW encoder and decoder
3  * Copyright (c) 2001 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #include "avformat.h"
20
21 /* simple formats */
22 int raw_write_header(struct AVFormatContext *s)
23 {
24     return 0;
25 }
26
27 int raw_write_packet(struct AVFormatContext *s, 
28                      int stream_index,
29                      unsigned char *buf, int size, int force_pts)
30 {
31     put_buffer(&s->pb, buf, size);
32     put_flush_packet(&s->pb);
33     return 0;
34 }
35
36 int raw_write_trailer(struct AVFormatContext *s)
37 {
38     return 0;
39 }
40
41 /* raw input */
42 static int raw_read_header(AVFormatContext *s,
43                            AVFormatParameters *ap)
44 {
45     AVStream *st;
46     int id;
47
48     st = av_new_stream(s, 0);
49     if (!st)
50         return AVERROR_NOMEM;
51     if (ap) {
52         id = s->iformat->value;
53         if (id == CODEC_ID_RAWVIDEO) {
54             st->codec.codec_type = CODEC_TYPE_VIDEO;
55         } else {
56             st->codec.codec_type = CODEC_TYPE_AUDIO;
57         }
58         st->codec.codec_id = id;
59
60         switch(st->codec.codec_type) {
61         case CODEC_TYPE_AUDIO:
62             st->codec.sample_rate = ap->sample_rate;
63             st->codec.channels = ap->channels;
64             break;
65         case CODEC_TYPE_VIDEO:
66             st->codec.frame_rate = ap->frame_rate;
67             st->codec.width = ap->width;
68             st->codec.height = ap->height;
69             break;
70         default:
71             return -1;
72         }
73     } else {
74         return -1;
75     }
76     return 0;
77 }
78
79 #define RAW_PACKET_SIZE 1024
80
81 int raw_read_packet(AVFormatContext *s,
82                     AVPacket *pkt)
83 {
84     int ret, size;
85     AVStream *st = s->streams[0];
86     
87     size= RAW_PACKET_SIZE;
88
89     if (av_new_packet(pkt, size) < 0)
90         return -EIO;
91
92     pkt->stream_index = 0;
93     ret = get_buffer(&s->pb, pkt->data, size);
94     if (ret <= 0) {
95         av_free_packet(pkt);
96         return -EIO;
97     }
98     /* note: we need to modify the packet size here to handle the last
99        packet */
100     pkt->size = ret;
101     return ret;
102 }
103
104 int raw_read_close(AVFormatContext *s)
105 {
106     return 0;
107 }
108
109 /* mp3 read */
110 static int mp3_read_header(AVFormatContext *s,
111                            AVFormatParameters *ap)
112 {
113     AVStream *st;
114
115     st = av_new_stream(s, 0);
116     if (!st)
117         return AVERROR_NOMEM;
118
119     st->codec.codec_type = CODEC_TYPE_AUDIO;
120     st->codec.codec_id = CODEC_ID_MP2;
121     /* the parameters will be extracted from the compressed bitstream */
122     return 0;
123 }
124
125 /* mpeg1/h263 input */
126 static int video_read_header(AVFormatContext *s,
127                              AVFormatParameters *ap)
128 {
129     AVStream *st;
130
131     st = av_new_stream(s, 0);
132     if (!st)
133         return AVERROR_NOMEM;
134
135     st->codec.codec_type = CODEC_TYPE_VIDEO;
136     st->codec.codec_id = s->iformat->value;
137     /* for mjpeg, specify frame rate */
138     /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
139     if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
140         if (ap) {
141             st->codec.frame_rate = ap->frame_rate;
142         } else {
143             st->codec.frame_rate = 25 * FRAME_RATE_BASE;
144         }
145     }
146     return 0;
147 }
148
149 #define SEQ_START_CODE          0x000001b3
150 #define GOP_START_CODE          0x000001b8
151 #define PICTURE_START_CODE      0x00000100
152
153 /* XXX: improve that by looking at several start codes */
154 static int mpegvideo_probe(AVProbeData *p)
155 {
156     int code, c, i;
157     code = 0xff;
158
159     /* we search the first start code. If it is a sequence, gop or
160        picture start code then we decide it is an mpeg video
161        stream. We do not send highest value to give a chance to mpegts */
162     for(i=0;i<p->buf_size;i++) {
163         c = p->buf[i];
164         code = (code << 8) | c;
165         if ((code & 0xffffff00) == 0x100) {
166             if (code == SEQ_START_CODE ||
167                 code == GOP_START_CODE ||
168                 code == PICTURE_START_CODE)
169                 return 50 - 1;
170             else
171                 return 0;
172         }
173     }
174     return 0;
175 }
176
177 AVInputFormat mp3_iformat = {
178     "mp3",
179     "MPEG audio",
180     0,
181     NULL,
182     mp3_read_header,
183     raw_read_packet,
184     raw_read_close,
185     .extensions = "mp2,mp3", /* XXX: use probe */
186 };
187
188 AVOutputFormat mp2_oformat = {
189     "mp2",
190     "MPEG audio layer 2",
191     "audio/x-mpeg",
192     "mp2,mp3",
193     0,
194     CODEC_ID_MP2,
195     0,
196     raw_write_header,
197     raw_write_packet,
198     raw_write_trailer,
199 };
200
201
202 AVInputFormat ac3_iformat = {
203     "ac3",
204     "raw ac3",
205     0,
206     NULL,
207     raw_read_header,
208     raw_read_packet,
209     raw_read_close,
210     .extensions = "ac3",
211     .value = CODEC_ID_AC3,
212 };
213
214 AVOutputFormat ac3_oformat = {
215     "ac3",
216     "raw ac3",
217     "audio/x-ac3", 
218     "ac3",
219     0,
220     CODEC_ID_AC3,
221     0,
222     raw_write_header,
223     raw_write_packet,
224     raw_write_trailer,
225 };
226
227 AVOutputFormat h263_oformat = {
228     "h263",
229     "raw h263",
230     "video/x-h263",
231     "h263",
232     0,
233     0,
234     CODEC_ID_H263,
235     raw_write_header,
236     raw_write_packet,
237     raw_write_trailer,
238 };
239
240 AVInputFormat m4v_iformat = {
241     "m4v",
242     "raw MPEG4 video format",
243     0,
244     NULL /*mpegvideo_probe*/,
245     video_read_header,
246     raw_read_packet,
247     raw_read_close,
248     .extensions = "m4v", //FIXME remove after writing mpeg4_probe
249     .value = CODEC_ID_MPEG4,
250 };
251
252 AVOutputFormat m4v_oformat = {
253     "m4v",
254     "raw MPEG4 video format",
255     NULL,
256     "m4v",
257     0,
258     CODEC_ID_NONE,
259     CODEC_ID_MPEG4,
260     raw_write_header,
261     raw_write_packet,
262     raw_write_trailer,
263 };
264
265 AVInputFormat mpegvideo_iformat = {
266     "mpegvideo",
267     "MPEG video",
268     0,
269     mpegvideo_probe,
270     video_read_header,
271     raw_read_packet,
272     raw_read_close,
273     .value = CODEC_ID_MPEG1VIDEO,
274 };
275
276 AVOutputFormat mpeg1video_oformat = {
277     "mpeg1video",
278     "MPEG video",
279     "video/x-mpeg",
280     "mpg,mpeg",
281     0,
282     0,
283     CODEC_ID_MPEG1VIDEO,
284     raw_write_header,
285     raw_write_packet,
286     raw_write_trailer,
287 };
288
289 AVInputFormat mjpeg_iformat = {
290     "mjpeg",
291     "MJPEG video",
292     0,
293     NULL,
294     video_read_header,
295     raw_read_packet,
296     raw_read_close,
297     .extensions = "mjpg,mjpeg",
298     .value = CODEC_ID_MJPEG,
299 };
300
301 AVOutputFormat mjpeg_oformat = {
302     "mjpeg",
303     "MJPEG video",
304     "video/x-mjpeg",
305     "mjpg,mjpeg",
306     0,
307     0,
308     CODEC_ID_MJPEG,
309     raw_write_header,
310     raw_write_packet,
311     raw_write_trailer,
312 };
313
314 /* pcm formats */
315
316 #define PCMDEF(name, long_name, ext, codec) \
317 AVInputFormat pcm_ ## name ## _iformat = {\
318     #name,\
319     long_name,\
320     0,\
321     NULL,\
322     raw_read_header,\
323     raw_read_packet,\
324     raw_read_close,\
325     .extensions = ext,\
326     .value = codec,\
327 };\
328 \
329 AVOutputFormat pcm_ ## name ## _oformat = {\
330     #name,\
331     long_name,\
332     NULL,\
333     ext,\
334     0,\
335     codec,\
336     0,\
337     raw_write_header,\
338     raw_write_packet,\
339     raw_write_trailer,\
340 };
341
342 #ifdef WORDS_BIGENDIAN
343 #define BE_DEF(s) s
344 #define LE_DEF(s) NULL
345 #else
346 #define BE_DEF(s) NULL
347 #define LE_DEF(s) s
348 #endif
349
350
351 PCMDEF(s16le, "pcm signed 16 bit little endian format", 
352        LE_DEF("sw"), CODEC_ID_PCM_S16LE)
353
354 PCMDEF(s16be, "pcm signed 16 bit big endian format", 
355        BE_DEF("sw"), CODEC_ID_PCM_S16BE)
356
357 PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
358        LE_DEF("uw"), CODEC_ID_PCM_U16LE)
359
360 PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
361        BE_DEF("uw"), CODEC_ID_PCM_U16BE)
362
363 PCMDEF(s8, "pcm signed 8 bit format", 
364        "sb", CODEC_ID_PCM_S8)
365
366 PCMDEF(u8, "pcm unsigned 8 bit format", 
367        "ub", CODEC_ID_PCM_U8)
368
369 PCMDEF(mulaw, "pcm mu law format", 
370        "ul", CODEC_ID_PCM_MULAW)
371
372 PCMDEF(alaw, "pcm A law format", 
373        "al", CODEC_ID_PCM_ALAW)
374
375 int rawvideo_read_packet(AVFormatContext *s,
376                          AVPacket *pkt)
377 {
378     int packet_size, ret, width, height;
379     AVStream *st = s->streams[0];
380
381     width = st->codec.width;
382     height = st->codec.height;
383
384     switch(st->codec.pix_fmt) {
385     case PIX_FMT_YUV420P:
386         packet_size = (width * height * 3) / 2;
387         break;
388     case PIX_FMT_YUV422:
389         packet_size = (width * height * 2);
390         break;
391     case PIX_FMT_BGR24:
392     case PIX_FMT_RGB24:
393         packet_size = (width * height * 3);
394         break;
395     default:
396         av_abort();
397         break;
398     }
399
400     if (av_new_packet(pkt, packet_size) < 0)
401         return -EIO;
402
403     pkt->stream_index = 0;
404 #if 0
405     /* bypass buffered I/O */
406     ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
407 #else
408     ret = get_buffer(&s->pb, pkt->data, pkt->size);
409 #endif
410     if (ret != pkt->size) {
411         av_free_packet(pkt);
412         return -EIO;
413     } else {
414         return 0;
415     }
416 }
417
418 AVInputFormat rawvideo_iformat = {
419     "rawvideo",
420     "raw video format",
421     0,
422     NULL,
423     raw_read_header,
424     rawvideo_read_packet,
425     raw_read_close,
426     .extensions = "yuv",
427     .value = CODEC_ID_RAWVIDEO,
428 };
429
430 AVOutputFormat rawvideo_oformat = {
431     "rawvideo",
432     "raw video format",
433     NULL,
434     "yuv",
435     0,
436     CODEC_ID_NONE,
437     CODEC_ID_RAWVIDEO,
438     raw_write_header,
439     raw_write_packet,
440     raw_write_trailer,
441 };
442
443 static int null_write_packet(struct AVFormatContext *s, 
444                              int stream_index,
445                              unsigned char *buf, int size, int force_pts)
446 {
447     return 0;
448 }
449
450 AVOutputFormat null_oformat = {
451     "null",
452     "null video format",
453     NULL,
454     NULL,
455     0,
456 #ifdef WORDS_BIGENDIAN
457     CODEC_ID_PCM_S16BE,
458 #else
459     CODEC_ID_PCM_S16LE,
460 #endif
461     CODEC_ID_RAWVIDEO,
462     raw_write_header,
463     null_write_packet,
464     raw_write_trailer,
465     .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
466 };
467
468 int raw_init(void)
469 {
470     av_register_input_format(&mp3_iformat);
471     av_register_output_format(&mp2_oformat);
472     
473     av_register_input_format(&ac3_iformat);
474     av_register_output_format(&ac3_oformat);
475
476     av_register_output_format(&h263_oformat);
477     
478     av_register_input_format(&m4v_iformat);
479     av_register_output_format(&m4v_oformat);
480
481     av_register_input_format(&mpegvideo_iformat);
482     av_register_output_format(&mpeg1video_oformat);
483
484     av_register_input_format(&mjpeg_iformat);
485     av_register_output_format(&mjpeg_oformat);
486
487     av_register_input_format(&pcm_s16le_iformat);
488     av_register_output_format(&pcm_s16le_oformat);
489     av_register_input_format(&pcm_s16be_iformat);
490     av_register_output_format(&pcm_s16be_oformat);
491     av_register_input_format(&pcm_u16le_iformat);
492     av_register_output_format(&pcm_u16le_oformat);
493     av_register_input_format(&pcm_u16be_iformat);
494     av_register_output_format(&pcm_u16be_oformat);
495     av_register_input_format(&pcm_s8_iformat);
496     av_register_output_format(&pcm_s8_oformat);
497     av_register_input_format(&pcm_u8_iformat);
498     av_register_output_format(&pcm_u8_oformat);
499     av_register_input_format(&pcm_mulaw_iformat);
500     av_register_output_format(&pcm_mulaw_oformat);
501     av_register_input_format(&pcm_alaw_iformat);
502     av_register_output_format(&pcm_alaw_oformat);
503
504     av_register_input_format(&rawvideo_iformat);
505     av_register_output_format(&rawvideo_oformat);
506
507     av_register_output_format(&null_oformat);
508     return 0;
509 }