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