OSDN Git Service

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