]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/raw.c
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot...
[frescor/ffmpeg.git] / 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 #ifdef CONFIG_ENCODERS
22 /* simple formats */
23 static int raw_write_header(struct AVFormatContext *s)
24 {
25     return 0;
26 }
27
28 static int raw_write_packet(struct AVFormatContext *s, int stream_index,
29                             const uint8_t *buf, int size, int64_t pts)
30 {
31     put_buffer(&s->pb, buf, size);
32     put_flush_packet(&s->pb);
33     return 0;
34 }
35
36 static int raw_write_trailer(struct AVFormatContext *s)
37 {
38     return 0;
39 }
40 #endif //CONFIG_ENCODERS
41
42 /* raw input */
43 static int raw_read_header(AVFormatContext *s, 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.frame_rate_base = ap->frame_rate_base;
68             st->codec.width = ap->width;
69             st->codec.height = ap->height;
70             st->codec.pix_fmt = ap->pix_fmt;
71             break;
72         default:
73             return -1;
74         }
75     } else {
76         return -1;
77     }
78     return 0;
79 }
80
81 #define RAW_PACKET_SIZE 1024
82
83 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
84 {
85     int ret, size;
86     //    AVStream *st = s->streams[0];
87     
88     size= RAW_PACKET_SIZE;
89
90     if (av_new_packet(pkt, size) < 0)
91         return -EIO;
92
93     pkt->stream_index = 0;
94     ret = get_buffer(&s->pb, pkt->data, size);
95     if (ret <= 0) {
96         av_free_packet(pkt);
97         return -EIO;
98     }
99     /* note: we need to modify the packet size here to handle the last
100        packet */
101     pkt->size = ret;
102     return ret;
103 }
104
105 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
106 {
107     int ret, size;
108
109     size = RAW_PACKET_SIZE;
110
111     if (av_new_packet(pkt, size) < 0)
112         return -EIO;
113
114     pkt->stream_index = 0;
115     ret = get_partial_buffer(&s->pb, pkt->data, size);
116     if (ret <= 0) {
117         av_free_packet(pkt);
118         return -EIO;
119     }
120     pkt->size = ret;
121     return ret;
122 }
123
124 static int raw_read_close(AVFormatContext *s)
125 {
126     return 0;
127 }
128
129 int pcm_read_seek(AVFormatContext *s, 
130                   int stream_index, int64_t timestamp)
131 {
132     AVStream *st;
133     int block_align, byte_rate;
134     int64_t pos;
135
136     st = s->streams[0];
137     switch(st->codec.codec_id) {
138     case CODEC_ID_PCM_S16LE:
139     case CODEC_ID_PCM_S16BE:
140     case CODEC_ID_PCM_U16LE:
141     case CODEC_ID_PCM_U16BE:
142         block_align = 2 * st->codec.channels;
143         byte_rate = block_align * st->codec.sample_rate;
144         break;
145     case CODEC_ID_PCM_S8:
146     case CODEC_ID_PCM_U8:
147     case CODEC_ID_PCM_MULAW:
148     case CODEC_ID_PCM_ALAW:
149         block_align = st->codec.channels;
150         byte_rate = block_align * st->codec.sample_rate;
151         break;
152     default:
153         block_align = st->codec.block_align;
154         byte_rate = st->codec.bit_rate / 8;
155         break;
156     }
157     
158     if (block_align <= 0 || byte_rate <= 0)
159         return -1;
160
161     /* compute the position by aligning it to block_align */
162     pos = (timestamp * byte_rate) / AV_TIME_BASE;
163     pos = (pos / block_align) * block_align;
164
165     /* recompute exact position */
166     st->cur_dts = (pos * AV_TIME_BASE) / byte_rate;
167     url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
168     return 0;
169 }
170
171 /* ac3 read */
172 static int ac3_read_header(AVFormatContext *s,
173                            AVFormatParameters *ap)
174 {
175     AVStream *st;
176
177     st = av_new_stream(s, 0);
178     if (!st)
179         return AVERROR_NOMEM;
180
181     st->codec.codec_type = CODEC_TYPE_AUDIO;
182     st->codec.codec_id = CODEC_ID_AC3;
183     st->need_parsing = 1;
184     /* the parameters will be extracted from the compressed bitstream */
185     return 0;
186 }
187
188 /* mpeg1/h263 input */
189 static int video_read_header(AVFormatContext *s,
190                              AVFormatParameters *ap)
191 {
192     AVStream *st;
193
194     st = av_new_stream(s, 0);
195     if (!st)
196         return AVERROR_NOMEM;
197
198     st->codec.codec_type = CODEC_TYPE_VIDEO;
199     st->codec.codec_id = s->iformat->value;
200     st->need_parsing = 1;
201
202     /* for mjpeg, specify frame rate */
203     /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
204     if (st->codec.codec_id == CODEC_ID_MJPEG || 
205         st->codec.codec_id == CODEC_ID_MPEG4) {
206         if (ap && ap->frame_rate) {
207             st->codec.frame_rate      = ap->frame_rate;
208             st->codec.frame_rate_base = ap->frame_rate_base;
209         } else {
210             st->codec.frame_rate      = 25;
211             st->codec.frame_rate_base = 1;
212         }
213     }
214     return 0;
215 }
216
217 #define SEQ_START_CODE          0x000001b3
218 #define GOP_START_CODE          0x000001b8
219 #define PICTURE_START_CODE      0x00000100
220
221 /* XXX: improve that by looking at several start codes */
222 static int mpegvideo_probe(AVProbeData *p)
223 {
224     int code;
225     const uint8_t *d;
226
227     /* we search the first start code. If it is a sequence, gop or
228        picture start code then we decide it is an mpeg video
229        stream. We do not send highest value to give a chance to mpegts */
230     /* NOTE: the search range was restricted to avoid too many false
231        detections */
232
233     if (p->buf_size < 6)
234         return 0;
235     d = p->buf;
236     code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
237     if ((code & 0xffffff00) == 0x100) {
238         if (code == SEQ_START_CODE ||
239             code == GOP_START_CODE ||
240             code == PICTURE_START_CODE)
241             return 50 - 1;
242         else
243             return 0;
244     }
245     return 0;
246 }
247
248 static int h263_probe(AVProbeData *p)
249 {
250     int code;
251     const uint8_t *d;
252
253     if (p->buf_size < 6)
254         return 0;
255     d = p->buf;
256     code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
257     if (code == 0x20) {
258         return 50;
259     }
260     return 0;
261 }
262
263 AVInputFormat ac3_iformat = {
264     "ac3",
265     "raw ac3",
266     0,
267     NULL,
268     ac3_read_header,
269     raw_read_partial_packet,
270     raw_read_close,
271     .extensions = "ac3",
272 };
273
274 #ifdef CONFIG_ENCODERS
275 AVOutputFormat ac3_oformat = {
276     "ac3",
277     "raw ac3",
278     "audio/x-ac3", 
279     "ac3",
280     0,
281     CODEC_ID_AC3,
282     0,
283     raw_write_header,
284     raw_write_packet,
285     raw_write_trailer,
286 };
287 #endif //CONFIG_ENCODERS
288
289 AVInputFormat h263_iformat = {
290     "h263",
291     "raw h263",
292     0,
293     h263_probe,
294     video_read_header,
295     raw_read_partial_packet,
296     raw_read_close,
297 //    .extensions = "h263", //FIXME remove after writing mpeg4_probe
298     .value = CODEC_ID_H263,
299 };
300
301 #ifdef CONFIG_ENCODERS
302 AVOutputFormat h263_oformat = {
303     "h263",
304     "raw h263",
305     "video/x-h263",
306     "h263",
307     0,
308     0,
309     CODEC_ID_H263,
310     raw_write_header,
311     raw_write_packet,
312     raw_write_trailer,
313 };
314 #endif //CONFIG_ENCODERS
315
316 AVInputFormat m4v_iformat = {
317     "m4v",
318     "raw MPEG4 video format",
319     0,
320     NULL /*mpegvideo_probe*/,
321     video_read_header,
322     raw_read_partial_packet,
323     raw_read_close,
324     .extensions = "m4v", //FIXME remove after writing mpeg4_probe
325     .value = CODEC_ID_MPEG4,
326 };
327
328 #ifdef CONFIG_ENCODERS
329 AVOutputFormat m4v_oformat = {
330     "m4v",
331     "raw MPEG4 video format",
332     NULL,
333     "m4v",
334     0,
335     CODEC_ID_NONE,
336     CODEC_ID_MPEG4,
337     raw_write_header,
338     raw_write_packet,
339     raw_write_trailer,
340 };
341 #endif //CONFIG_ENCODERS
342
343 AVInputFormat h264_iformat = {
344     "h264",
345     "raw H264 video format",
346     0,
347     NULL /*mpegvideo_probe*/,
348     video_read_header,
349     raw_read_partial_packet,
350     raw_read_close,
351     .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
352     .value = CODEC_ID_H264,
353 };
354
355 #ifdef CONFIG_ENCODERS
356 AVOutputFormat h264_oformat = {
357     "h264",
358     "raw H264 video format",
359     NULL,
360     "h264",
361     0,
362     CODEC_ID_NONE,
363     CODEC_ID_H264,
364     raw_write_header,
365     raw_write_packet,
366     raw_write_trailer,
367 };
368 #endif //CONFIG_ENCODERS
369
370 AVInputFormat mpegvideo_iformat = {
371     "mpegvideo",
372     "MPEG video",
373     0,
374     mpegvideo_probe,
375     video_read_header,
376     raw_read_partial_packet,
377     raw_read_close,
378     .value = CODEC_ID_MPEG1VIDEO,
379 };
380
381 #ifdef CONFIG_ENCODERS
382 AVOutputFormat mpeg1video_oformat = {
383     "mpeg1video",
384     "MPEG video",
385     "video/x-mpeg",
386     "mpg,mpeg",
387     0,
388     0,
389     CODEC_ID_MPEG1VIDEO,
390     raw_write_header,
391     raw_write_packet,
392     raw_write_trailer,
393 };
394 #endif //CONFIG_ENCODERS
395
396 AVInputFormat mjpeg_iformat = {
397     "mjpeg",
398     "MJPEG video",
399     0,
400     NULL,
401     video_read_header,
402     raw_read_partial_packet,
403     raw_read_close,
404     .extensions = "mjpg,mjpeg",
405     .value = CODEC_ID_MJPEG,
406 };
407
408 #ifdef CONFIG_ENCODERS
409 AVOutputFormat mjpeg_oformat = {
410     "mjpeg",
411     "MJPEG video",
412     "video/x-mjpeg",
413     "mjpg,mjpeg",
414     0,
415     0,
416     CODEC_ID_MJPEG,
417     raw_write_header,
418     raw_write_packet,
419     raw_write_trailer,
420 };
421 #endif //CONFIG_ENCODERS
422
423 /* pcm formats */
424
425 #define PCMINPUTDEF(name, long_name, ext, codec) \
426 AVInputFormat pcm_ ## name ## _iformat = {\
427     #name,\
428     long_name,\
429     0,\
430     NULL,\
431     raw_read_header,\
432     raw_read_packet,\
433     raw_read_close,\
434     pcm_read_seek,\
435     .extensions = ext,\
436     .value = codec,\
437 };
438
439 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
440
441 #define PCMDEF(name, long_name, ext, codec) \
442     PCMINPUTDEF(name, long_name, ext, codec)
443
444 #else
445
446 #define PCMDEF(name, long_name, ext, codec) \
447     PCMINPUTDEF(name, long_name, ext, codec)\
448 \
449 AVOutputFormat pcm_ ## name ## _oformat = {\
450     #name,\
451     long_name,\
452     NULL,\
453     ext,\
454     0,\
455     codec,\
456     0,\
457     raw_write_header,\
458     raw_write_packet,\
459     raw_write_trailer,\
460 };
461 #endif //CONFIG_ENCODERS
462
463 #ifdef WORDS_BIGENDIAN
464 #define BE_DEF(s) s
465 #define LE_DEF(s) NULL
466 #else
467 #define BE_DEF(s) NULL
468 #define LE_DEF(s) s
469 #endif
470
471
472 PCMDEF(s16le, "pcm signed 16 bit little endian format", 
473        LE_DEF("sw"), CODEC_ID_PCM_S16LE)
474
475 PCMDEF(s16be, "pcm signed 16 bit big endian format", 
476        BE_DEF("sw"), CODEC_ID_PCM_S16BE)
477
478 PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
479        LE_DEF("uw"), CODEC_ID_PCM_U16LE)
480
481 PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
482        BE_DEF("uw"), CODEC_ID_PCM_U16BE)
483
484 PCMDEF(s8, "pcm signed 8 bit format", 
485        "sb", CODEC_ID_PCM_S8)
486
487 PCMDEF(u8, "pcm unsigned 8 bit format", 
488        "ub", CODEC_ID_PCM_U8)
489
490 PCMDEF(mulaw, "pcm mu law format", 
491        "ul", CODEC_ID_PCM_MULAW)
492
493 PCMDEF(alaw, "pcm A law format", 
494        "al", CODEC_ID_PCM_ALAW)
495
496 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
497 {
498     int packet_size, ret, width, height;
499     AVStream *st = s->streams[0];
500
501     width = st->codec.width;
502     height = st->codec.height;
503
504     packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
505     if (packet_size < 0)
506         av_abort();
507
508     if (av_new_packet(pkt, packet_size) < 0)
509         return -EIO;
510
511     pkt->stream_index = 0;
512 #if 0
513     /* bypass buffered I/O */
514     ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
515 #else
516     ret = get_buffer(&s->pb, pkt->data, pkt->size);
517 #endif
518     if (ret != pkt->size) {
519         av_free_packet(pkt);
520         return -EIO;
521     } else {
522         return 0;
523     }
524 }
525
526 AVInputFormat rawvideo_iformat = {
527     "rawvideo",
528     "raw video format",
529     0,
530     NULL,
531     raw_read_header,
532     rawvideo_read_packet,
533     raw_read_close,
534     .extensions = "yuv",
535     .value = CODEC_ID_RAWVIDEO,
536 };
537
538 #ifdef CONFIG_ENCODERS
539 AVOutputFormat rawvideo_oformat = {
540     "rawvideo",
541     "raw video format",
542     NULL,
543     "yuv",
544     0,
545     CODEC_ID_NONE,
546     CODEC_ID_RAWVIDEO,
547     raw_write_header,
548     raw_write_packet,
549     raw_write_trailer,
550 };
551 #endif //CONFIG_ENCODERS
552
553 #ifdef CONFIG_ENCODERS
554 static int null_write_packet(struct AVFormatContext *s, 
555                              int stream_index,
556                              const uint8_t *buf, int size, int64_t pts)
557 {
558     return 0;
559 }
560
561 AVOutputFormat null_oformat = {
562     "null",
563     "null video format",
564     NULL,
565     NULL,
566     0,
567 #ifdef WORDS_BIGENDIAN
568     CODEC_ID_PCM_S16BE,
569 #else
570     CODEC_ID_PCM_S16LE,
571 #endif
572     CODEC_ID_RAWVIDEO,
573     raw_write_header,
574     null_write_packet,
575     raw_write_trailer,
576     .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
577 };
578 #endif //CONFIG_ENCODERS
579
580 #ifndef CONFIG_ENCODERS
581 #define av_register_output_format(format)
582 #endif
583 #ifndef CONFIG_DECODERS
584 #define av_register_input_format(format)
585 #endif
586
587 int raw_init(void)
588 {
589     av_register_input_format(&ac3_iformat);
590     av_register_output_format(&ac3_oformat);
591
592     av_register_input_format(&h263_iformat);
593     av_register_output_format(&h263_oformat);
594     
595     av_register_input_format(&m4v_iformat);
596     av_register_output_format(&m4v_oformat);
597     
598     av_register_input_format(&h264_iformat);
599     av_register_output_format(&h264_oformat);
600
601     av_register_input_format(&mpegvideo_iformat);
602     av_register_output_format(&mpeg1video_oformat);
603
604     av_register_input_format(&mjpeg_iformat);
605     av_register_output_format(&mjpeg_oformat);
606
607     av_register_input_format(&pcm_s16le_iformat);
608     av_register_output_format(&pcm_s16le_oformat);
609     av_register_input_format(&pcm_s16be_iformat);
610     av_register_output_format(&pcm_s16be_oformat);
611     av_register_input_format(&pcm_u16le_iformat);
612     av_register_output_format(&pcm_u16le_oformat);
613     av_register_input_format(&pcm_u16be_iformat);
614     av_register_output_format(&pcm_u16be_oformat);
615     av_register_input_format(&pcm_s8_iformat);
616     av_register_output_format(&pcm_s8_oformat);
617     av_register_input_format(&pcm_u8_iformat);
618     av_register_output_format(&pcm_u8_oformat);
619     av_register_input_format(&pcm_mulaw_iformat);
620     av_register_output_format(&pcm_mulaw_oformat);
621     av_register_input_format(&pcm_alaw_iformat);
622     av_register_output_format(&pcm_alaw_oformat);
623
624     av_register_input_format(&rawvideo_iformat);
625     av_register_output_format(&rawvideo_oformat);
626
627     av_register_output_format(&null_oformat);
628     return 0;
629 }