improve mpeg1/2-es detection
[libav.git] / libavformat / raw.c
1 /*
2 * RAW encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2005 Alex Beregszaszi
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "avformat.h"
21
22 #ifdef CONFIG_MUXERS
23 /* simple formats */
24 static int raw_write_header(struct AVFormatContext *s)
25 {
26 return 0;
27 }
28
29 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
30 {
31 put_buffer(&s->pb, pkt->data, pkt->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_MUXERS
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 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
65 break;
66 case CODEC_TYPE_VIDEO:
67 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
68 st->codec->width = ap->width;
69 st->codec->height = ap->height;
70 st->codec->pix_fmt = ap->pix_fmt;
71 if(st->codec->pix_fmt == PIX_FMT_NONE)
72 st->codec->pix_fmt= PIX_FMT_YUV420P;
73 break;
74 default:
75 return -1;
76 }
77 } else {
78 return -1;
79 }
80 return 0;
81 }
82
83 #define RAW_PACKET_SIZE 1024
84
85 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
86 {
87 int ret, size;
88 // AVStream *st = s->streams[0];
89
90 size= RAW_PACKET_SIZE;
91
92 ret= av_get_packet(&s->pb, pkt, size);
93
94 pkt->stream_index = 0;
95 if (ret <= 0) {
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->pos= url_ftell(&s->pb);
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 AVERROR_IO;
119 }
120 pkt->size = ret;
121 return ret;
122 }
123
124 // http://www.artificis.hu/files/texts/ingenient.txt
125 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
126 {
127 int ret, size, w, h, unk1, unk2;
128
129 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
130 return AVERROR_IO; // FIXME
131
132 size = get_le32(&s->pb);
133
134 w = get_le16(&s->pb);
135 h = get_le16(&s->pb);
136
137 url_fskip(&s->pb, 8); // zero + size (padded?)
138 url_fskip(&s->pb, 2);
139 unk1 = get_le16(&s->pb);
140 unk2 = get_le16(&s->pb);
141 url_fskip(&s->pb, 22); // ascii timestamp
142
143 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
144 size, w, h, unk1, unk2);
145
146 if (av_new_packet(pkt, size) < 0)
147 return AVERROR_IO;
148
149 pkt->pos = url_ftell(&s->pb);
150 pkt->stream_index = 0;
151 ret = get_buffer(&s->pb, pkt->data, size);
152 if (ret <= 0) {
153 av_free_packet(pkt);
154 return AVERROR_IO;
155 }
156 pkt->size = ret;
157 return ret;
158 }
159
160 static int raw_read_close(AVFormatContext *s)
161 {
162 return 0;
163 }
164
165 int pcm_read_seek(AVFormatContext *s,
166 int stream_index, int64_t timestamp, int flags)
167 {
168 AVStream *st;
169 int block_align, byte_rate;
170 int64_t pos;
171
172 st = s->streams[0];
173 switch(st->codec->codec_id) {
174 case CODEC_ID_PCM_S16LE:
175 case CODEC_ID_PCM_S16BE:
176 case CODEC_ID_PCM_U16LE:
177 case CODEC_ID_PCM_U16BE:
178 block_align = 2 * st->codec->channels;
179 byte_rate = block_align * st->codec->sample_rate;
180 break;
181 case CODEC_ID_PCM_S8:
182 case CODEC_ID_PCM_U8:
183 case CODEC_ID_PCM_MULAW:
184 case CODEC_ID_PCM_ALAW:
185 block_align = st->codec->channels;
186 byte_rate = block_align * st->codec->sample_rate;
187 break;
188 default:
189 block_align = st->codec->block_align;
190 byte_rate = st->codec->bit_rate / 8;
191 break;
192 }
193
194 if (block_align <= 0 || byte_rate <= 0)
195 return -1;
196
197 /* compute the position by aligning it to block_align */
198 pos = av_rescale_rnd(timestamp * byte_rate,
199 st->time_base.num,
200 st->time_base.den * (int64_t)block_align,
201 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
202 pos *= block_align;
203
204 /* recompute exact position */
205 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
206 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
207 return 0;
208 }
209
210 /* ac3 read */
211 static int ac3_read_header(AVFormatContext *s,
212 AVFormatParameters *ap)
213 {
214 AVStream *st;
215
216 st = av_new_stream(s, 0);
217 if (!st)
218 return AVERROR_NOMEM;
219
220 st->codec->codec_type = CODEC_TYPE_AUDIO;
221 st->codec->codec_id = CODEC_ID_AC3;
222 st->need_parsing = 1;
223 /* the parameters will be extracted from the compressed bitstream */
224 return 0;
225 }
226
227 static int shorten_read_header(AVFormatContext *s,
228 AVFormatParameters *ap)
229 {
230 AVStream *st;
231
232 st = av_new_stream(s, 0);
233 if (!st)
234 return AVERROR_NOMEM;
235 st->codec->codec_type = CODEC_TYPE_AUDIO;
236 st->codec->codec_id = CODEC_ID_SHORTEN;
237 st->need_parsing = 1;
238 /* the parameters will be extracted from the compressed bitstream */
239 return 0;
240 }
241
242 /* dts read */
243 static int dts_read_header(AVFormatContext *s,
244 AVFormatParameters *ap)
245 {
246 AVStream *st;
247
248 st = av_new_stream(s, 0);
249 if (!st)
250 return AVERROR_NOMEM;
251
252 st->codec->codec_type = CODEC_TYPE_AUDIO;
253 st->codec->codec_id = CODEC_ID_DTS;
254 st->need_parsing = 1;
255 /* the parameters will be extracted from the compressed bitstream */
256 return 0;
257 }
258
259 /* mpeg1/h263 input */
260 static int video_read_header(AVFormatContext *s,
261 AVFormatParameters *ap)
262 {
263 AVStream *st;
264
265 st = av_new_stream(s, 0);
266 if (!st)
267 return AVERROR_NOMEM;
268
269 st->codec->codec_type = CODEC_TYPE_VIDEO;
270 st->codec->codec_id = s->iformat->value;
271 st->need_parsing = 1;
272
273 /* for mjpeg, specify frame rate */
274 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
275 if (ap && ap->time_base.num) {
276 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
277 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
278 st->codec->codec_id == CODEC_ID_MPEG4 ||
279 st->codec->codec_id == CODEC_ID_H264) {
280 av_set_pts_info(st, 64, 1, 25);
281 }
282
283 return 0;
284 }
285
286 #define SEQ_START_CODE 0x000001b3
287 #define GOP_START_CODE 0x000001b8
288 #define PICTURE_START_CODE 0x00000100
289 #define SLICE_START_CODE 0x00000101
290 #define PACK_START_CODE 0x000001ba
291
292 static int mpegvideo_probe(AVProbeData *p)
293 {
294 uint32_t code= -1;
295 int pic=0, seq=0, slice=0, pspack=0;
296 int i;
297
298 for(i=0; i<p->buf_size; i++){
299 code = (code<<8) + p->buf[i];
300 if ((code & 0xffffff00) == 0x100) {
301 switch(code){
302 case SEQ_START_CODE: seq++; break;
303 case PICTURE_START_CODE: pic++; break;
304 case SLICE_START_CODE: slice++; break;
305 case PACK_START_CODE: pspack++; break;
306 }
307 }
308 }
309 if(seq && pic && slice && seq<pic && (ABS(pic-slice)-1)*10 < pic && !pspack)
310 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
311 return 0;
312 }
313
314 static int h263_probe(AVProbeData *p)
315 {
316 int code;
317 const uint8_t *d;
318
319 if (p->buf_size < 6)
320 return 0;
321 d = p->buf;
322 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
323 if (code == 0x20) {
324 return 50;
325 }
326 return 0;
327 }
328
329 static int h261_probe(AVProbeData *p)
330 {
331 int code;
332 const uint8_t *d;
333
334 if (p->buf_size < 6)
335 return 0;
336 d = p->buf;
337 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
338 if (code == 0x10) {
339 return 50;
340 }
341 return 0;
342 }
343
344 AVInputFormat shorten_iformat = {
345 "shn",
346 "raw shorten",
347 0,
348 NULL,
349 shorten_read_header,
350 raw_read_partial_packet,
351 raw_read_close,
352 .extensions = "shn",
353 };
354
355 AVInputFormat ac3_iformat = {
356 "ac3",
357 "raw ac3",
358 0,
359 NULL,
360 ac3_read_header,
361 raw_read_partial_packet,
362 raw_read_close,
363 .extensions = "ac3",
364 };
365
366 #ifdef CONFIG_MUXERS
367 AVOutputFormat ac3_oformat = {
368 "ac3",
369 "raw ac3",
370 "audio/x-ac3",
371 "ac3",
372 0,
373 CODEC_ID_AC3,
374 0,
375 raw_write_header,
376 raw_write_packet,
377 raw_write_trailer,
378 };
379 #endif //CONFIG_MUXERS
380
381 AVInputFormat dts_iformat = {
382 "dts",
383 "raw dts",
384 0,
385 NULL,
386 dts_read_header,
387 raw_read_partial_packet,
388 raw_read_close,
389 .extensions = "dts",
390 };
391
392 AVInputFormat h261_iformat = {
393 "h261",
394 "raw h261",
395 0,
396 h261_probe,
397 video_read_header,
398 raw_read_partial_packet,
399 raw_read_close,
400 .extensions = "h261",
401 .value = CODEC_ID_H261,
402 };
403
404 #ifdef CONFIG_MUXERS
405 AVOutputFormat h261_oformat = {
406 "h261",
407 "raw h261",
408 "video/x-h261",
409 "h261",
410 0,
411 0,
412 CODEC_ID_H261,
413 raw_write_header,
414 raw_write_packet,
415 raw_write_trailer,
416 };
417 #endif //CONFIG_MUXERS
418
419 AVInputFormat h263_iformat = {
420 "h263",
421 "raw h263",
422 0,
423 h263_probe,
424 video_read_header,
425 raw_read_partial_packet,
426 raw_read_close,
427 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
428 .value = CODEC_ID_H263,
429 };
430
431 #ifdef CONFIG_MUXERS
432 AVOutputFormat h263_oformat = {
433 "h263",
434 "raw h263",
435 "video/x-h263",
436 "h263",
437 0,
438 0,
439 CODEC_ID_H263,
440 raw_write_header,
441 raw_write_packet,
442 raw_write_trailer,
443 };
444 #endif //CONFIG_MUXERS
445
446 AVInputFormat m4v_iformat = {
447 "m4v",
448 "raw MPEG4 video format",
449 0,
450 NULL /*mpegvideo_probe*/,
451 video_read_header,
452 raw_read_partial_packet,
453 raw_read_close,
454 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
455 .value = CODEC_ID_MPEG4,
456 };
457
458 #ifdef CONFIG_MUXERS
459 AVOutputFormat m4v_oformat = {
460 "m4v",
461 "raw MPEG4 video format",
462 NULL,
463 "m4v",
464 0,
465 CODEC_ID_NONE,
466 CODEC_ID_MPEG4,
467 raw_write_header,
468 raw_write_packet,
469 raw_write_trailer,
470 };
471 #endif //CONFIG_MUXERS
472
473 AVInputFormat h264_iformat = {
474 "h264",
475 "raw H264 video format",
476 0,
477 NULL /*mpegvideo_probe*/,
478 video_read_header,
479 raw_read_partial_packet,
480 raw_read_close,
481 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
482 .value = CODEC_ID_H264,
483 };
484
485 #ifdef CONFIG_MUXERS
486 AVOutputFormat h264_oformat = {
487 "h264",
488 "raw H264 video format",
489 NULL,
490 "h264",
491 0,
492 CODEC_ID_NONE,
493 CODEC_ID_H264,
494 raw_write_header,
495 raw_write_packet,
496 raw_write_trailer,
497 };
498 #endif //CONFIG_MUXERS
499
500 AVInputFormat mpegvideo_iformat = {
501 "mpegvideo",
502 "MPEG video",
503 0,
504 mpegvideo_probe,
505 video_read_header,
506 raw_read_partial_packet,
507 raw_read_close,
508 .value = CODEC_ID_MPEG1VIDEO,
509 };
510
511 #ifdef CONFIG_MUXERS
512 AVOutputFormat mpeg1video_oformat = {
513 "mpeg1video",
514 "MPEG video",
515 "video/x-mpeg",
516 "mpg,mpeg,m1v",
517 0,
518 0,
519 CODEC_ID_MPEG1VIDEO,
520 raw_write_header,
521 raw_write_packet,
522 raw_write_trailer,
523 };
524 #endif //CONFIG_MUXERS
525
526 #ifdef CONFIG_MUXERS
527 AVOutputFormat mpeg2video_oformat = {
528 "mpeg2video",
529 "MPEG2 video",
530 NULL,
531 "m2v",
532 0,
533 0,
534 CODEC_ID_MPEG2VIDEO,
535 raw_write_header,
536 raw_write_packet,
537 raw_write_trailer,
538 };
539 #endif //CONFIG_MUXERS
540
541 AVInputFormat mjpeg_iformat = {
542 "mjpeg",
543 "MJPEG video",
544 0,
545 NULL,
546 video_read_header,
547 raw_read_partial_packet,
548 raw_read_close,
549 .extensions = "mjpg,mjpeg",
550 .value = CODEC_ID_MJPEG,
551 };
552
553 AVInputFormat ingenient_iformat = {
554 "ingenient",
555 "Ingenient MJPEG",
556 0,
557 NULL,
558 video_read_header,
559 ingenient_read_packet,
560 raw_read_close,
561 .extensions = "cgi", // FIXME
562 .value = CODEC_ID_MJPEG,
563 };
564
565 #ifdef CONFIG_MUXERS
566 AVOutputFormat mjpeg_oformat = {
567 "mjpeg",
568 "MJPEG video",
569 "video/x-mjpeg",
570 "mjpg,mjpeg",
571 0,
572 0,
573 CODEC_ID_MJPEG,
574 raw_write_header,
575 raw_write_packet,
576 raw_write_trailer,
577 };
578 #endif //CONFIG_MUXERS
579
580 /* pcm formats */
581
582 #define PCMINPUTDEF(name, long_name, ext, codec) \
583 AVInputFormat pcm_ ## name ## _iformat = {\
584 #name,\
585 long_name,\
586 0,\
587 NULL,\
588 raw_read_header,\
589 raw_read_packet,\
590 raw_read_close,\
591 pcm_read_seek,\
592 .extensions = ext,\
593 .value = codec,\
594 };
595
596 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
597
598 #define PCMDEF(name, long_name, ext, codec) \
599 PCMINPUTDEF(name, long_name, ext, codec)
600
601 #else
602
603 #define PCMDEF(name, long_name, ext, codec) \
604 PCMINPUTDEF(name, long_name, ext, codec)\
605 \
606 AVOutputFormat pcm_ ## name ## _oformat = {\
607 #name,\
608 long_name,\
609 NULL,\
610 ext,\
611 0,\
612 codec,\
613 0,\
614 raw_write_header,\
615 raw_write_packet,\
616 raw_write_trailer,\
617 };
618 #endif //CONFIG_MUXERS
619
620 #ifdef WORDS_BIGENDIAN
621 #define BE_DEF(s) s
622 #define LE_DEF(s) NULL
623 #else
624 #define BE_DEF(s) NULL
625 #define LE_DEF(s) s
626 #endif
627
628
629 PCMDEF(s16le, "pcm signed 16 bit little endian format",
630 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
631
632 PCMDEF(s16be, "pcm signed 16 bit big endian format",
633 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
634
635 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
636 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
637
638 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
639 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
640
641 PCMDEF(s8, "pcm signed 8 bit format",
642 "sb", CODEC_ID_PCM_S8)
643
644 PCMDEF(u8, "pcm unsigned 8 bit format",
645 "ub", CODEC_ID_PCM_U8)
646
647 PCMDEF(mulaw, "pcm mu law format",
648 "ul", CODEC_ID_PCM_MULAW)
649
650 PCMDEF(alaw, "pcm A law format",
651 "al", CODEC_ID_PCM_ALAW)
652
653 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
654 {
655 int packet_size, ret, width, height;
656 AVStream *st = s->streams[0];
657
658 width = st->codec->width;
659 height = st->codec->height;
660
661 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
662 if (packet_size < 0)
663 return -1;
664
665 ret= av_get_packet(&s->pb, pkt, packet_size);
666
667 pkt->stream_index = 0;
668 if (ret != packet_size) {
669 return AVERROR_IO;
670 } else {
671 return 0;
672 }
673 }
674
675 AVInputFormat rawvideo_iformat = {
676 "rawvideo",
677 "raw video format",
678 0,
679 NULL,
680 raw_read_header,
681 rawvideo_read_packet,
682 raw_read_close,
683 .extensions = "yuv,cif,qcif",
684 .value = CODEC_ID_RAWVIDEO,
685 };
686
687 #ifdef CONFIG_MUXERS
688 AVOutputFormat rawvideo_oformat = {
689 "rawvideo",
690 "raw video format",
691 NULL,
692 "yuv",
693 0,
694 CODEC_ID_NONE,
695 CODEC_ID_RAWVIDEO,
696 raw_write_header,
697 raw_write_packet,
698 raw_write_trailer,
699 };
700 #endif //CONFIG_MUXERS
701
702 #ifdef CONFIG_MUXERS
703 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
704 {
705 return 0;
706 }
707
708 AVOutputFormat null_oformat = {
709 "null",
710 "null video format",
711 NULL,
712 NULL,
713 0,
714 #ifdef WORDS_BIGENDIAN
715 CODEC_ID_PCM_S16BE,
716 #else
717 CODEC_ID_PCM_S16LE,
718 #endif
719 CODEC_ID_RAWVIDEO,
720 raw_write_header,
721 null_write_packet,
722 raw_write_trailer,
723 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
724 };
725 #endif //CONFIG_MUXERS
726
727 #ifndef CONFIG_MUXERS
728 #define av_register_output_format(format)
729 #endif
730 #ifndef CONFIG_DEMUXERS
731 #define av_register_input_format(format)
732 #endif
733
734 int raw_init(void)
735 {
736
737 av_register_input_format(&shorten_iformat);
738
739 av_register_input_format(&ac3_iformat);
740 av_register_output_format(&ac3_oformat);
741
742 av_register_input_format(&dts_iformat);
743
744 av_register_input_format(&h261_iformat);
745 av_register_output_format(&h261_oformat);
746
747 av_register_input_format(&h263_iformat);
748 av_register_output_format(&h263_oformat);
749
750 av_register_input_format(&m4v_iformat);
751 av_register_output_format(&m4v_oformat);
752
753 av_register_input_format(&h264_iformat);
754 av_register_output_format(&h264_oformat);
755
756 av_register_input_format(&mpegvideo_iformat);
757 av_register_output_format(&mpeg1video_oformat);
758
759 av_register_output_format(&mpeg2video_oformat);
760
761 av_register_input_format(&mjpeg_iformat);
762 av_register_output_format(&mjpeg_oformat);
763
764 av_register_input_format(&ingenient_iformat);
765
766 av_register_input_format(&pcm_s16le_iformat);
767 av_register_output_format(&pcm_s16le_oformat);
768 av_register_input_format(&pcm_s16be_iformat);
769 av_register_output_format(&pcm_s16be_oformat);
770 av_register_input_format(&pcm_u16le_iformat);
771 av_register_output_format(&pcm_u16le_oformat);
772 av_register_input_format(&pcm_u16be_iformat);
773 av_register_output_format(&pcm_u16be_oformat);
774 av_register_input_format(&pcm_s8_iformat);
775 av_register_output_format(&pcm_s8_oformat);
776 av_register_input_format(&pcm_u8_iformat);
777 av_register_output_format(&pcm_u8_oformat);
778 av_register_input_format(&pcm_mulaw_iformat);
779 av_register_output_format(&pcm_mulaw_oformat);
780 av_register_input_format(&pcm_alaw_iformat);
781 av_register_output_format(&pcm_alaw_oformat);
782
783 av_register_input_format(&rawvideo_iformat);
784 av_register_output_format(&rawvideo_oformat);
785
786 av_register_output_format(&null_oformat);
787 return 0;
788 }