]> git.sesse.net Git - ffmpeg/blob - libavformat/raw.c
raw ac3 auto detects parameters
[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 /* ac3 read */
126 static int ac3_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_AUDIO;
136     st->codec.codec_id = CODEC_ID_AC3;
137     /* the parameters will be extracted from the compressed bitstream */
138     return 0;
139 }
140
141 /* mpeg1/h263 input */
142 static int video_read_header(AVFormatContext *s,
143                              AVFormatParameters *ap)
144 {
145     AVStream *st;
146
147     st = av_new_stream(s, 0);
148     if (!st)
149         return AVERROR_NOMEM;
150
151     st->codec.codec_type = CODEC_TYPE_VIDEO;
152     st->codec.codec_id = s->iformat->value;
153     /* for mjpeg, specify frame rate */
154     /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
155     if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
156         if (ap) {
157             st->codec.frame_rate = ap->frame_rate;
158         } else {
159             st->codec.frame_rate = 25 * FRAME_RATE_BASE;
160         }
161     }
162     return 0;
163 }
164
165 #define SEQ_START_CODE          0x000001b3
166 #define GOP_START_CODE          0x000001b8
167 #define PICTURE_START_CODE      0x00000100
168
169 /* XXX: improve that by looking at several start codes */
170 static int mpegvideo_probe(AVProbeData *p)
171 {
172     int code;
173     const uint8_t *d;
174
175     /* we search the first start code. If it is a sequence, gop or
176        picture start code then we decide it is an mpeg video
177        stream. We do not send highest value to give a chance to mpegts */
178     /* NOTE: the search range was restricted to avoid too many false
179        detections */
180
181     if (p->buf_size < 6)
182         return 0;
183     d = p->buf;
184     code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
185     if ((code & 0xffffff00) == 0x100) {
186         if (code == SEQ_START_CODE ||
187             code == GOP_START_CODE ||
188             code == PICTURE_START_CODE)
189             return 50 - 1;
190         else
191             return 0;
192     }
193     return 0;
194 }
195
196 AVInputFormat mp3_iformat = {
197     "mp3",
198     "MPEG audio",
199     0,
200     NULL,
201     mp3_read_header,
202     raw_read_packet,
203     raw_read_close,
204     .extensions = "mp2,mp3", /* XXX: use probe */
205 };
206
207 AVOutputFormat mp2_oformat = {
208     "mp2",
209     "MPEG audio layer 2",
210     "audio/x-mpeg",
211     "mp2,mp3",
212     0,
213     CODEC_ID_MP2,
214     0,
215     raw_write_header,
216     raw_write_packet,
217     raw_write_trailer,
218 };
219
220
221 AVInputFormat ac3_iformat = {
222     "ac3",
223     "raw ac3",
224     0,
225     NULL,
226     ac3_read_header,
227     raw_read_packet,
228     raw_read_close,
229     .extensions = "ac3",
230 };
231
232 AVOutputFormat ac3_oformat = {
233     "ac3",
234     "raw ac3",
235     "audio/x-ac3", 
236     "ac3",
237     0,
238     CODEC_ID_AC3,
239     0,
240     raw_write_header,
241     raw_write_packet,
242     raw_write_trailer,
243 };
244
245 AVOutputFormat h263_oformat = {
246     "h263",
247     "raw h263",
248     "video/x-h263",
249     "h263",
250     0,
251     0,
252     CODEC_ID_H263,
253     raw_write_header,
254     raw_write_packet,
255     raw_write_trailer,
256 };
257
258 AVInputFormat m4v_iformat = {
259     "m4v",
260     "raw MPEG4 video format",
261     0,
262     NULL /*mpegvideo_probe*/,
263     video_read_header,
264     raw_read_packet,
265     raw_read_close,
266     .extensions = "m4v", //FIXME remove after writing mpeg4_probe
267     .value = CODEC_ID_MPEG4,
268 };
269
270 AVOutputFormat m4v_oformat = {
271     "m4v",
272     "raw MPEG4 video format",
273     NULL,
274     "m4v",
275     0,
276     CODEC_ID_NONE,
277     CODEC_ID_MPEG4,
278     raw_write_header,
279     raw_write_packet,
280     raw_write_trailer,
281 };
282
283 AVInputFormat mpegvideo_iformat = {
284     "mpegvideo",
285     "MPEG video",
286     0,
287     mpegvideo_probe,
288     video_read_header,
289     raw_read_packet,
290     raw_read_close,
291     .value = CODEC_ID_MPEG1VIDEO,
292 };
293
294 AVOutputFormat mpeg1video_oformat = {
295     "mpeg1video",
296     "MPEG video",
297     "video/x-mpeg",
298     "mpg,mpeg",
299     0,
300     0,
301     CODEC_ID_MPEG1VIDEO,
302     raw_write_header,
303     raw_write_packet,
304     raw_write_trailer,
305 };
306
307 AVInputFormat mjpeg_iformat = {
308     "mjpeg",
309     "MJPEG video",
310     0,
311     NULL,
312     video_read_header,
313     raw_read_packet,
314     raw_read_close,
315     .extensions = "mjpg,mjpeg",
316     .value = CODEC_ID_MJPEG,
317 };
318
319 AVOutputFormat mjpeg_oformat = {
320     "mjpeg",
321     "MJPEG video",
322     "video/x-mjpeg",
323     "mjpg,mjpeg",
324     0,
325     0,
326     CODEC_ID_MJPEG,
327     raw_write_header,
328     raw_write_packet,
329     raw_write_trailer,
330 };
331
332 /* pcm formats */
333
334 #define PCMDEF(name, long_name, ext, codec) \
335 AVInputFormat pcm_ ## name ## _iformat = {\
336     #name,\
337     long_name,\
338     0,\
339     NULL,\
340     raw_read_header,\
341     raw_read_packet,\
342     raw_read_close,\
343     .extensions = ext,\
344     .value = codec,\
345 };\
346 \
347 AVOutputFormat pcm_ ## name ## _oformat = {\
348     #name,\
349     long_name,\
350     NULL,\
351     ext,\
352     0,\
353     codec,\
354     0,\
355     raw_write_header,\
356     raw_write_packet,\
357     raw_write_trailer,\
358 };
359
360 #ifdef WORDS_BIGENDIAN
361 #define BE_DEF(s) s
362 #define LE_DEF(s) NULL
363 #else
364 #define BE_DEF(s) NULL
365 #define LE_DEF(s) s
366 #endif
367
368
369 PCMDEF(s16le, "pcm signed 16 bit little endian format", 
370        LE_DEF("sw"), CODEC_ID_PCM_S16LE)
371
372 PCMDEF(s16be, "pcm signed 16 bit big endian format", 
373        BE_DEF("sw"), CODEC_ID_PCM_S16BE)
374
375 PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
376        LE_DEF("uw"), CODEC_ID_PCM_U16LE)
377
378 PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
379        BE_DEF("uw"), CODEC_ID_PCM_U16BE)
380
381 PCMDEF(s8, "pcm signed 8 bit format", 
382        "sb", CODEC_ID_PCM_S8)
383
384 PCMDEF(u8, "pcm unsigned 8 bit format", 
385        "ub", CODEC_ID_PCM_U8)
386
387 PCMDEF(mulaw, "pcm mu law format", 
388        "ul", CODEC_ID_PCM_MULAW)
389
390 PCMDEF(alaw, "pcm A law format", 
391        "al", CODEC_ID_PCM_ALAW)
392
393 int rawvideo_read_packet(AVFormatContext *s,
394                          AVPacket *pkt)
395 {
396     int packet_size, ret, width, height;
397     AVStream *st = s->streams[0];
398
399     width = st->codec.width;
400     height = st->codec.height;
401
402     switch(st->codec.pix_fmt) {
403     case PIX_FMT_YUV420P:
404         packet_size = (width * height * 3) / 2;
405         break;
406     case PIX_FMT_YUV422:
407         packet_size = (width * height * 2);
408         break;
409     case PIX_FMT_BGR24:
410     case PIX_FMT_RGB24:
411         packet_size = (width * height * 3);
412         break;
413     default:
414         av_abort();
415         break;
416     }
417
418     if (av_new_packet(pkt, packet_size) < 0)
419         return -EIO;
420
421     pkt->stream_index = 0;
422 #if 0
423     /* bypass buffered I/O */
424     ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
425 #else
426     ret = get_buffer(&s->pb, pkt->data, pkt->size);
427 #endif
428     if (ret != pkt->size) {
429         av_free_packet(pkt);
430         return -EIO;
431     } else {
432         return 0;
433     }
434 }
435
436 AVInputFormat rawvideo_iformat = {
437     "rawvideo",
438     "raw video format",
439     0,
440     NULL,
441     raw_read_header,
442     rawvideo_read_packet,
443     raw_read_close,
444     .extensions = "yuv",
445     .value = CODEC_ID_RAWVIDEO,
446 };
447
448 AVOutputFormat rawvideo_oformat = {
449     "rawvideo",
450     "raw video format",
451     NULL,
452     "yuv",
453     0,
454     CODEC_ID_NONE,
455     CODEC_ID_RAWVIDEO,
456     raw_write_header,
457     raw_write_packet,
458     raw_write_trailer,
459 };
460
461 static int null_write_packet(struct AVFormatContext *s, 
462                              int stream_index,
463                              unsigned char *buf, int size, int force_pts)
464 {
465     return 0;
466 }
467
468 AVOutputFormat null_oformat = {
469     "null",
470     "null video format",
471     NULL,
472     NULL,
473     0,
474 #ifdef WORDS_BIGENDIAN
475     CODEC_ID_PCM_S16BE,
476 #else
477     CODEC_ID_PCM_S16LE,
478 #endif
479     CODEC_ID_RAWVIDEO,
480     raw_write_header,
481     null_write_packet,
482     raw_write_trailer,
483     .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
484 };
485
486 int raw_init(void)
487 {
488     av_register_input_format(&mp3_iformat);
489     av_register_output_format(&mp2_oformat);
490     
491     av_register_input_format(&ac3_iformat);
492     av_register_output_format(&ac3_oformat);
493
494     av_register_output_format(&h263_oformat);
495     
496     av_register_input_format(&m4v_iformat);
497     av_register_output_format(&m4v_oformat);
498
499     av_register_input_format(&mpegvideo_iformat);
500     av_register_output_format(&mpeg1video_oformat);
501
502     av_register_input_format(&mjpeg_iformat);
503     av_register_output_format(&mjpeg_oformat);
504
505     av_register_input_format(&pcm_s16le_iformat);
506     av_register_output_format(&pcm_s16le_oformat);
507     av_register_input_format(&pcm_s16be_iformat);
508     av_register_output_format(&pcm_s16be_oformat);
509     av_register_input_format(&pcm_u16le_iformat);
510     av_register_output_format(&pcm_u16le_oformat);
511     av_register_input_format(&pcm_u16be_iformat);
512     av_register_output_format(&pcm_u16be_oformat);
513     av_register_input_format(&pcm_s8_iformat);
514     av_register_output_format(&pcm_s8_oformat);
515     av_register_input_format(&pcm_u8_iformat);
516     av_register_output_format(&pcm_u8_oformat);
517     av_register_input_format(&pcm_mulaw_iformat);
518     av_register_output_format(&pcm_mulaw_oformat);
519     av_register_input_format(&pcm_alaw_iformat);
520     av_register_output_format(&pcm_alaw_oformat);
521
522     av_register_input_format(&rawvideo_iformat);
523     av_register_output_format(&rawvideo_oformat);
524
525     av_register_output_format(&null_oformat);
526     return 0;
527 }