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