MPEG-2 DXVA2 implementation
[ffmpeg.git] / libavformat / raw.c
1 /*
2  * RAW muxer and demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2005 Alex Beregszaszi
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/crc.h"
24 #include "libavcodec/ac3_parser.h"
25 #include "libavcodec/get_bits.h"
26 #include "libavcodec/bytestream.h"
27 #include "avformat.h"
28 #include "raw.h"
29 #include "id3v2.h"
30 #include "id3v1.h"
31
32 /* simple formats */
33
34 #if CONFIG_ROQ_MUXER
35 static int roq_write_header(struct AVFormatContext *s)
36 {
37     static const uint8_t header[] = {
38         0x84, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0x1E, 0x00
39     };
40
41     put_buffer(s->pb, header, 8);
42     put_flush_packet(s->pb);
43
44     return 0;
45 }
46 #endif
47
48 #if CONFIG_NULL_MUXER
49 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
50 {
51     return 0;
52 }
53 #endif
54
55 #if CONFIG_MUXERS
56 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
57 {
58     put_buffer(s->pb, pkt->data, pkt->size);
59     put_flush_packet(s->pb);
60     return 0;
61 }
62 #endif
63
64 #if CONFIG_DEMUXERS
65 /* raw input */
66 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
67 {
68     AVStream *st;
69     enum CodecID id;
70
71     st = av_new_stream(s, 0);
72     if (!st)
73         return AVERROR(ENOMEM);
74
75         id = s->iformat->value;
76         if (id == CODEC_ID_RAWVIDEO) {
77             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
78         } else {
79             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
80         }
81         st->codec->codec_id = id;
82
83         switch(st->codec->codec_type) {
84         case AVMEDIA_TYPE_AUDIO:
85             st->codec->sample_rate = ap->sample_rate;
86             if(ap->channels) st->codec->channels = ap->channels;
87             else             st->codec->channels = 1;
88             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
89             assert(st->codec->bits_per_coded_sample > 0);
90             st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
91             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
92             break;
93         case AVMEDIA_TYPE_VIDEO:
94             if(ap->time_base.num)
95                 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
96             else
97                 av_set_pts_info(st, 64, 1, 25);
98             st->codec->width = ap->width;
99             st->codec->height = ap->height;
100             st->codec->pix_fmt = ap->pix_fmt;
101             if(st->codec->pix_fmt == PIX_FMT_NONE)
102                 st->codec->pix_fmt= PIX_FMT_YUV420P;
103             break;
104         default:
105             return -1;
106         }
107     return 0;
108 }
109
110 #define RAW_PACKET_SIZE 1024
111 #define RAW_SAMPLES     1024
112
113 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
114 {
115     int ret, size, bps;
116     //    AVStream *st = s->streams[0];
117
118     size= RAW_SAMPLES*s->streams[0]->codec->block_align;
119
120     ret= av_get_packet(s->pb, pkt, size);
121
122     pkt->stream_index = 0;
123     if (ret < 0)
124         return ret;
125
126     bps= av_get_bits_per_sample(s->streams[0]->codec->codec_id);
127     assert(bps); // if false there IS a bug elsewhere (NOT in this function)
128     pkt->dts=
129     pkt->pts= pkt->pos*8 / (bps * s->streams[0]->codec->channels);
130
131     return ret;
132 }
133
134 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
135 {
136     int ret, size;
137
138     size = RAW_PACKET_SIZE;
139
140     if (av_new_packet(pkt, size) < 0)
141         return AVERROR(ENOMEM);
142
143     pkt->pos= url_ftell(s->pb);
144     pkt->stream_index = 0;
145     ret = get_partial_buffer(s->pb, pkt->data, size);
146     if (ret < 0) {
147         av_free_packet(pkt);
148         return ret;
149     }
150     pkt->size = ret;
151     return ret;
152 }
153 #endif
154
155 #if CONFIG_RAWVIDEO_DEMUXER
156 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
157 {
158     int packet_size, ret, width, height;
159     AVStream *st = s->streams[0];
160
161     width = st->codec->width;
162     height = st->codec->height;
163
164     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
165     if (packet_size < 0)
166         return -1;
167
168     ret= av_get_packet(s->pb, pkt, packet_size);
169     pkt->pts=
170     pkt->dts= pkt->pos / packet_size;
171
172     pkt->stream_index = 0;
173     if (ret < 0)
174         return ret;
175     return 0;
176 }
177 #endif
178
179 #if CONFIG_INGENIENT_DEMUXER
180 // http://www.artificis.hu/files/texts/ingenient.txt
181 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
182 {
183     int ret, size, w, h, unk1, unk2;
184
185     if (get_le32(s->pb) != MKTAG('M', 'J', 'P', 'G'))
186         return AVERROR(EIO); // FIXME
187
188     size = get_le32(s->pb);
189
190     w = get_le16(s->pb);
191     h = get_le16(s->pb);
192
193     url_fskip(s->pb, 8); // zero + size (padded?)
194     url_fskip(s->pb, 2);
195     unk1 = get_le16(s->pb);
196     unk2 = get_le16(s->pb);
197     url_fskip(s->pb, 22); // ASCII timestamp
198
199     av_log(s, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
200         size, w, h, unk1, unk2);
201
202     if (av_new_packet(pkt, size) < 0)
203         return AVERROR(ENOMEM);
204
205     pkt->pos = url_ftell(s->pb);
206     pkt->stream_index = 0;
207     ret = get_buffer(s->pb, pkt->data, size);
208     if (ret < 0) {
209         av_free_packet(pkt);
210         return ret;
211     }
212     pkt->size = ret;
213     return ret;
214 }
215 #endif
216
217 #if CONFIG_DEMUXERS
218 int pcm_read_seek(AVFormatContext *s,
219                   int stream_index, int64_t timestamp, int flags)
220 {
221     AVStream *st;
222     int block_align, byte_rate;
223     int64_t pos, ret;
224
225     st = s->streams[0];
226
227     block_align = st->codec->block_align ? st->codec->block_align :
228         (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
229     byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
230         block_align * st->codec->sample_rate;
231
232     if (block_align <= 0 || byte_rate <= 0)
233         return -1;
234     if (timestamp < 0) timestamp = 0;
235
236     /* compute the position by aligning it to block_align */
237     pos = av_rescale_rnd(timestamp * byte_rate,
238                          st->time_base.num,
239                          st->time_base.den * (int64_t)block_align,
240                          (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
241     pos *= block_align;
242
243     /* recompute exact position */
244     st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
245     if ((ret = url_fseek(s->pb, pos + s->data_offset, SEEK_SET)) < 0)
246         return ret;
247     return 0;
248 }
249
250 static int audio_read_header(AVFormatContext *s,
251                              AVFormatParameters *ap)
252 {
253     AVStream *st = av_new_stream(s, 0);
254     if (!st)
255         return AVERROR(ENOMEM);
256     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
257     st->codec->codec_id = s->iformat->value;
258     st->need_parsing = AVSTREAM_PARSE_FULL;
259     /* the parameters will be extracted from the compressed bitstream */
260
261     return 0;
262 }
263
264 /* MPEG-1/H.263 input */
265 static int video_read_header(AVFormatContext *s,
266                              AVFormatParameters *ap)
267 {
268     AVStream *st;
269
270     st = av_new_stream(s, 0);
271     if (!st)
272         return AVERROR(ENOMEM);
273
274     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
275     st->codec->codec_id = s->iformat->value;
276     st->need_parsing = AVSTREAM_PARSE_FULL;
277
278     /* for MJPEG, specify frame rate */
279     /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
280     if (ap->time_base.num) {
281         st->codec->time_base= ap->time_base;
282     } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
283                 st->codec->codec_id == CODEC_ID_MPEG4 ||
284                 st->codec->codec_id == CODEC_ID_DIRAC ||
285                 st->codec->codec_id == CODEC_ID_DNXHD ||
286                 st->codec->codec_id == CODEC_ID_VC1   ||
287                 st->codec->codec_id == CODEC_ID_H264) {
288         st->codec->time_base= (AVRational){1,25};
289     }
290     av_set_pts_info(st, 64, 1, 1200000);
291
292     return 0;
293 }
294 #endif
295
296 #if CONFIG_MPEGVIDEO_DEMUXER
297 #define SEQ_START_CODE          0x000001b3
298 #define GOP_START_CODE          0x000001b8
299 #define PICTURE_START_CODE      0x00000100
300 #define SLICE_START_CODE        0x00000101
301 #define PACK_START_CODE         0x000001ba
302 #define VIDEO_ID                0x000001e0
303 #define AUDIO_ID                0x000001c0
304
305 static int mpegvideo_probe(AVProbeData *p)
306 {
307     uint32_t code= -1;
308     int pic=0, seq=0, slice=0, pspack=0, pes=0;
309     int i;
310
311     for(i=0; i<p->buf_size; i++){
312         code = (code<<8) + p->buf[i];
313         if ((code & 0xffffff00) == 0x100) {
314             switch(code){
315             case     SEQ_START_CODE:   seq++; break;
316             case PICTURE_START_CODE:   pic++; break;
317             case   SLICE_START_CODE: slice++; break;
318             case    PACK_START_CODE: pspack++; break;
319             }
320             if     ((code & 0x1f0) == VIDEO_ID)   pes++;
321             else if((code & 0x1e0) == AUDIO_ID)   pes++;
322         }
323     }
324     if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack && !pes)
325         return pic>1 ? AVPROBE_SCORE_MAX/2+1 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
326     return 0;
327 }
328 #endif
329
330 #if CONFIG_CAVSVIDEO_DEMUXER
331 #define CAVS_SEQ_START_CODE       0x000001b0
332 #define CAVS_PIC_I_START_CODE     0x000001b3
333 #define CAVS_UNDEF_START_CODE     0x000001b4
334 #define CAVS_PIC_PB_START_CODE    0x000001b6
335 #define CAVS_VIDEO_EDIT_CODE      0x000001b7
336 #define CAVS_PROFILE_JIZHUN       0x20
337
338 static int cavsvideo_probe(AVProbeData *p)
339 {
340     uint32_t code= -1;
341     int pic=0, seq=0, slice_pos = 0;
342     int i;
343
344     for(i=0; i<p->buf_size; i++){
345         code = (code<<8) + p->buf[i];
346         if ((code & 0xffffff00) == 0x100) {
347             if(code < CAVS_SEQ_START_CODE) {
348                 /* slices have to be consecutive */
349                 if(code < slice_pos)
350                     return 0;
351                 slice_pos = code;
352             } else {
353                 slice_pos = 0;
354             }
355             if (code == CAVS_SEQ_START_CODE) {
356                 seq++;
357                 /* check for the only currently supported profile */
358                 if(p->buf[i+1] != CAVS_PROFILE_JIZHUN)
359                     return 0;
360             } else if ((code == CAVS_PIC_I_START_CODE) ||
361                        (code == CAVS_PIC_PB_START_CODE)) {
362                 pic++;
363             } else if ((code == CAVS_UNDEF_START_CODE) ||
364                        (code >  CAVS_VIDEO_EDIT_CODE)) {
365                 return 0;
366             }
367         }
368     }
369     if(seq && seq*9<=pic*10)
370         return AVPROBE_SCORE_MAX/2;
371     return 0;
372 }
373 #endif
374
375 #if CONFIG_M4V_DEMUXER
376 #define VISUAL_OBJECT_START_CODE       0x000001b5
377 #define VOP_START_CODE                 0x000001b6
378
379 static int mpeg4video_probe(AVProbeData *probe_packet)
380 {
381     uint32_t temp_buffer= -1;
382     int VO=0, VOL=0, VOP = 0, VISO = 0, res=0;
383     int i;
384
385     for(i=0; i<probe_packet->buf_size; i++){
386         temp_buffer = (temp_buffer<<8) + probe_packet->buf[i];
387         if ((temp_buffer & 0xffffff00) != 0x100)
388             continue;
389
390         if (temp_buffer == VOP_START_CODE)                         VOP++;
391         else if (temp_buffer == VISUAL_OBJECT_START_CODE)          VISO++;
392         else if (temp_buffer < 0x120)                              VO++;
393         else if (temp_buffer < 0x130)                              VOL++;
394         else if (   !(0x1AF < temp_buffer && temp_buffer < 0x1B7)
395                  && !(0x1B9 < temp_buffer && temp_buffer < 0x1C4)) res++;
396     }
397
398     if ( VOP >= VISO && VOP >= VOL && VO >= VOL && VOL > 0 && res==0)
399         return AVPROBE_SCORE_MAX/2;
400     return 0;
401 }
402 #endif
403
404 #if CONFIG_H264_DEMUXER
405 static int h264_probe(AVProbeData *p)
406 {
407     uint32_t code= -1;
408     int sps=0, pps=0, idr=0, res=0, sli=0;
409     int i;
410
411     for(i=0; i<p->buf_size; i++){
412         code = (code<<8) + p->buf[i];
413         if ((code & 0xffffff00) == 0x100) {
414             int ref_idc= (code>>5)&3;
415             int type   = code & 0x1F;
416             static const int8_t ref_zero[32]={
417                 2, 0, 0, 0, 0,-1, 1,-1,
418                -1, 1, 1, 1, 1,-1, 2, 2,
419                 2, 2, 2, 0, 2, 2, 2, 2,
420                 2, 2, 2, 2, 2, 2, 2, 2
421             };
422
423             if(code & 0x80) //forbidden bit
424                 return 0;
425
426             if(ref_zero[type] == 1 && ref_idc)
427                 return 0;
428             if(ref_zero[type] ==-1 && !ref_idc)
429                 return 0;
430             if(ref_zero[type] == 2)
431                 res++;
432
433             switch(type){
434             case     1:   sli++; break;
435             case     5:   idr++; break;
436             case     7:
437                 if(p->buf[i+2]&0x0F)
438                     return 0;
439                 sps++;
440                 break;
441             case     8:   pps++; break;
442             }
443         }
444     }
445     if(sps && pps && (idr||sli>3) && res<(sps+pps+idr))
446         return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
447     return 0;
448 }
449 #endif
450
451 #if CONFIG_H263_DEMUXER
452 static int h263_probe(AVProbeData *p)
453 {
454     uint64_t code= -1;
455     int i;
456     int valid_psc=0;
457     int invalid_psc=0;
458     int res_change=0;
459     int src_fmt, last_src_fmt=-1;
460
461     for(i=0; i<p->buf_size; i++){
462         code = (code<<8) + p->buf[i];
463         if ((code & 0xfffffc0000) == 0x800000) {
464             src_fmt= (code>>2)&3;
465             if(   src_fmt != last_src_fmt
466                && last_src_fmt>0 && last_src_fmt<6
467                && src_fmt<6)
468                 res_change++;
469
470             if((code&0x300)==0x200 && src_fmt){
471                 valid_psc++;
472             }else
473                 invalid_psc++;
474             last_src_fmt= src_fmt;
475         }
476     }
477 //av_log(NULL, AV_LOG_ERROR, "h263_probe: psc:%d invalid:%d res_change:%d\n", valid_psc, invalid_psc, res_change);
478 //h263_probe: psc:3 invalid:0 res_change:0 (1588/recent_ffmpeg_parses_mpg_incorrectly.mpg)
479     if(valid_psc > 2*invalid_psc + 2*res_change + 3){
480         return 50;
481     }else if(valid_psc > 2*invalid_psc)
482         return 25;
483     return 0;
484 }
485 #endif
486
487 #if CONFIG_H261_DEMUXER
488 static int h261_probe(AVProbeData *p)
489 {
490     uint32_t code= -1;
491     int i;
492     int valid_psc=0;
493     int invalid_psc=0;
494     int next_gn=0;
495     int src_fmt=0;
496     GetBitContext gb;
497
498     init_get_bits(&gb, p->buf, p->buf_size*8);
499
500     for(i=0; i<p->buf_size*8; i++){
501         if ((code & 0x01ff0000) || !(code & 0xff00)) {
502             code = (code<<8) + get_bits(&gb, 8);
503             i += 7;
504         } else
505             code = (code<<1) + get_bits1(&gb);
506         if ((code & 0xffff0000) == 0x10000) {
507             int gn= (code>>12)&0xf;
508             if(!gn)
509                 src_fmt= code&8;
510             if(gn != next_gn) invalid_psc++;
511             else              valid_psc++;
512
513             if(src_fmt){ // CIF
514                 next_gn= (gn+1     )%13;
515             }else{       //QCIF
516                 next_gn= (gn+1+!!gn)% 7;
517             }
518         }
519     }
520     if(valid_psc > 2*invalid_psc + 6){
521         return 50;
522     }else if(valid_psc > 2*invalid_psc + 2)
523         return 25;
524     return 0;
525 }
526 #endif
527
528 #if CONFIG_DTS_DEMUXER
529 #define DCA_MARKER_14B_BE 0x1FFFE800
530 #define DCA_MARKER_14B_LE 0xFF1F00E8
531 #define DCA_MARKER_RAW_BE 0x7FFE8001
532 #define DCA_MARKER_RAW_LE 0xFE7F0180
533 static int dts_probe(AVProbeData *p)
534 {
535     const uint8_t *buf, *bufp;
536     uint32_t state = -1;
537     int markers[3] = {0};
538     int sum, max;
539
540     buf = p->buf;
541
542     for(; buf < (p->buf+p->buf_size)-2; buf+=2) {
543         bufp = buf;
544         state = (state << 16) | bytestream_get_be16(&bufp);
545
546         /* regular bitstream */
547         if (state == DCA_MARKER_RAW_BE || state == DCA_MARKER_RAW_LE)
548             markers[0]++;
549
550         /* 14 bits big-endian bitstream */
551         if (state == DCA_MARKER_14B_BE)
552             if ((bytestream_get_be16(&bufp) & 0xFFF0) == 0x07F0)
553                 markers[1]++;
554
555         /* 14 bits little-endian bitstream */
556         if (state == DCA_MARKER_14B_LE)
557             if ((bytestream_get_be16(&bufp) & 0xF0FF) == 0xF007)
558                 markers[2]++;
559     }
560     sum = markers[0] + markers[1] + markers[2];
561     max = markers[1] > markers[0];
562     max = markers[2] > markers[max] ? 2 : max;
563     if (markers[max] > 3 && p->buf_size / markers[max] < 32*1024 &&
564         markers[max] * 4 > sum * 3)
565         return AVPROBE_SCORE_MAX/2+1;
566
567     return 0;
568 }
569 #endif
570
571 #if CONFIG_DIRAC_DEMUXER
572 static int dirac_probe(AVProbeData *p)
573 {
574     if (AV_RL32(p->buf) == MKTAG('B', 'B', 'C', 'D'))
575         return AVPROBE_SCORE_MAX;
576     else
577         return 0;
578 }
579 #endif
580
581 #if CONFIG_DNXHD_DEMUXER
582 static int dnxhd_probe(AVProbeData *p)
583 {
584     static const uint8_t header[] = {0x00,0x00,0x02,0x80,0x01};
585     int w, h, compression_id;
586     if (p->buf_size < 0x2c)
587         return 0;
588     if (memcmp(p->buf, header, 5))
589         return 0;
590     h = AV_RB16(p->buf + 0x18);
591     w = AV_RB16(p->buf + 0x1a);
592     if (!w || !h)
593         return 0;
594     compression_id = AV_RB32(p->buf + 0x28);
595     if (compression_id < 1237 || compression_id > 1253)
596         return 0;
597     return AVPROBE_SCORE_MAX;
598 }
599 #endif
600
601 #if CONFIG_AC3_DEMUXER || CONFIG_EAC3_DEMUXER
602 static int ac3_eac3_probe(AVProbeData *p, enum CodecID expected_codec_id)
603 {
604     int max_frames, first_frames = 0, frames;
605     uint8_t *buf, *buf2, *end;
606     AC3HeaderInfo hdr;
607     GetBitContext gbc;
608     enum CodecID codec_id = CODEC_ID_AC3;
609
610     max_frames = 0;
611     buf = p->buf;
612     end = buf + p->buf_size;
613
614     for(; buf < end; buf++) {
615         buf2 = buf;
616
617         for(frames = 0; buf2 < end; frames++) {
618             init_get_bits(&gbc, buf2, 54);
619             if(ff_ac3_parse_header(&gbc, &hdr) < 0)
620                 break;
621             if(buf2 + hdr.frame_size > end ||
622                av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, hdr.frame_size - 2))
623                 break;
624             if (hdr.bitstream_id > 10)
625                 codec_id = CODEC_ID_EAC3;
626             buf2 += hdr.frame_size;
627         }
628         max_frames = FFMAX(max_frames, frames);
629         if(buf == p->buf)
630             first_frames = frames;
631     }
632     if(codec_id != expected_codec_id) return 0;
633     // keep this in sync with mp3 probe, both need to avoid
634     // issues with MPEG-files!
635     if   (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
636     else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
637     else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
638     else if(max_frames>=1) return 1;
639     else                   return 0;
640 }
641 #endif
642
643 #if CONFIG_AC3_DEMUXER
644 static int ac3_probe(AVProbeData *p)
645 {
646     return ac3_eac3_probe(p, CODEC_ID_AC3);
647 }
648 #endif
649
650 #if CONFIG_EAC3_DEMUXER
651 static int eac3_probe(AVProbeData *p)
652 {
653     return ac3_eac3_probe(p, CODEC_ID_EAC3);
654 }
655 #endif
656
657 #if CONFIG_AAC_DEMUXER
658 static int adts_aac_probe(AVProbeData *p)
659 {
660     int max_frames = 0, first_frames = 0;
661     int fsize, frames;
662     uint8_t *buf0 = p->buf;
663     uint8_t *buf2;
664     uint8_t *buf;
665     uint8_t *end = buf0 + p->buf_size - 7;
666
667     if (ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC)) {
668         buf0 += ff_id3v2_tag_len(buf0);
669     }
670     buf = buf0;
671
672     for(; buf < end; buf= buf2+1) {
673         buf2 = buf;
674
675         for(frames = 0; buf2 < end; frames++) {
676             uint32_t header = AV_RB16(buf2);
677             if((header&0xFFF6) != 0xFFF0)
678                 break;
679             fsize = (AV_RB32(buf2+3)>>13) & 0x8FFF;
680             if(fsize < 7)
681                 break;
682             buf2 += fsize;
683         }
684         max_frames = FFMAX(max_frames, frames);
685         if(buf == buf0)
686             first_frames= frames;
687     }
688     if   (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
689     else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
690     else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
691     else if(max_frames>=1) return 1;
692     else                   return 0;
693 }
694
695 static int adts_aac_read_header(AVFormatContext *s,
696                                 AVFormatParameters *ap)
697 {
698     AVStream *st;
699
700     st = av_new_stream(s, 0);
701     if (!st)
702         return AVERROR(ENOMEM);
703
704     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
705     st->codec->codec_id = s->iformat->value;
706     st->need_parsing = AVSTREAM_PARSE_FULL;
707
708     ff_id3v1_read(s);
709     ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC);
710
711     return 0;
712 }
713
714 #endif
715
716 /* Note: Do not forget to add new entries to the Makefile as well. */
717
718 #if CONFIG_AAC_DEMUXER
719 AVInputFormat aac_demuxer = {
720     "aac",
721     NULL_IF_CONFIG_SMALL("raw ADTS AAC"),
722     0,
723     adts_aac_probe,
724     adts_aac_read_header,
725     ff_raw_read_partial_packet,
726     .flags= AVFMT_GENERIC_INDEX,
727     .extensions = "aac",
728     .value = CODEC_ID_AAC,
729 };
730 #endif
731
732 #if CONFIG_AC3_DEMUXER
733 AVInputFormat ac3_demuxer = {
734     "ac3",
735     NULL_IF_CONFIG_SMALL("raw AC-3"),
736     0,
737     ac3_probe,
738     audio_read_header,
739     ff_raw_read_partial_packet,
740     .flags= AVFMT_GENERIC_INDEX,
741     .extensions = "ac3",
742     .value = CODEC_ID_AC3,
743 };
744 #endif
745
746 #if CONFIG_AC3_MUXER
747 AVOutputFormat ac3_muxer = {
748     "ac3",
749     NULL_IF_CONFIG_SMALL("raw AC-3"),
750     "audio/x-ac3",
751     "ac3",
752     0,
753     CODEC_ID_AC3,
754     CODEC_ID_NONE,
755     NULL,
756     raw_write_packet,
757     .flags= AVFMT_NOTIMESTAMPS,
758 };
759 #endif
760
761 #if CONFIG_DIRAC_DEMUXER
762 AVInputFormat dirac_demuxer = {
763     "dirac",
764     NULL_IF_CONFIG_SMALL("raw Dirac"),
765     0,
766     dirac_probe,
767     video_read_header,
768     ff_raw_read_partial_packet,
769     .flags= AVFMT_GENERIC_INDEX,
770     .value = CODEC_ID_DIRAC,
771 };
772 #endif
773
774 #if CONFIG_DIRAC_MUXER
775 AVOutputFormat dirac_muxer = {
776     "dirac",
777     NULL_IF_CONFIG_SMALL("raw Dirac"),
778     NULL,
779     "drc",
780     0,
781     CODEC_ID_NONE,
782     CODEC_ID_DIRAC,
783     NULL,
784     raw_write_packet,
785     .flags= AVFMT_NOTIMESTAMPS,
786 };
787 #endif
788
789 #if CONFIG_DNXHD_DEMUXER
790 AVInputFormat dnxhd_demuxer = {
791     "dnxhd",
792     NULL_IF_CONFIG_SMALL("raw DNxHD (SMPTE VC-3)"),
793     0,
794     dnxhd_probe,
795     video_read_header,
796     ff_raw_read_partial_packet,
797     .flags= AVFMT_GENERIC_INDEX,
798     .value = CODEC_ID_DNXHD,
799 };
800 #endif
801
802 #if CONFIG_DNXHD_MUXER
803 AVOutputFormat dnxhd_muxer = {
804     "dnxhd",
805     NULL_IF_CONFIG_SMALL("raw DNxHD (SMPTE VC-3)"),
806     NULL,
807     "dnxhd",
808     0,
809     CODEC_ID_NONE,
810     CODEC_ID_DNXHD,
811     NULL,
812     raw_write_packet,
813     .flags= AVFMT_NOTIMESTAMPS,
814 };
815 #endif
816
817 #if CONFIG_DTS_DEMUXER
818 AVInputFormat dts_demuxer = {
819     "dts",
820     NULL_IF_CONFIG_SMALL("raw DTS"),
821     0,
822     dts_probe,
823     audio_read_header,
824     ff_raw_read_partial_packet,
825     .flags= AVFMT_GENERIC_INDEX,
826     .extensions = "dts",
827     .value = CODEC_ID_DTS,
828 };
829 #endif
830
831 #if CONFIG_DTS_MUXER
832 AVOutputFormat dts_muxer = {
833     "dts",
834     NULL_IF_CONFIG_SMALL("raw DTS"),
835     "audio/x-dca",
836     "dts",
837     0,
838     CODEC_ID_DTS,
839     CODEC_ID_NONE,
840     NULL,
841     raw_write_packet,
842     .flags= AVFMT_NOTIMESTAMPS,
843 };
844 #endif
845
846 #if CONFIG_EAC3_DEMUXER
847 AVInputFormat eac3_demuxer = {
848     "eac3",
849     NULL_IF_CONFIG_SMALL("raw E-AC-3"),
850     0,
851     eac3_probe,
852     audio_read_header,
853     ff_raw_read_partial_packet,
854     .flags= AVFMT_GENERIC_INDEX,
855     .extensions = "eac3",
856     .value = CODEC_ID_EAC3,
857 };
858 #endif
859
860 #if CONFIG_EAC3_MUXER
861 AVOutputFormat eac3_muxer = {
862     "eac3",
863     NULL_IF_CONFIG_SMALL("raw E-AC-3"),
864     "audio/x-eac3",
865     "eac3",
866     0,
867     CODEC_ID_EAC3,
868     CODEC_ID_NONE,
869     NULL,
870     raw_write_packet,
871     .flags= AVFMT_NOTIMESTAMPS,
872 };
873 #endif
874
875 #if CONFIG_GSM_DEMUXER
876 AVInputFormat gsm_demuxer = {
877     "gsm",
878     NULL_IF_CONFIG_SMALL("raw GSM"),
879     0,
880     NULL,
881     audio_read_header,
882     ff_raw_read_partial_packet,
883     .flags= AVFMT_GENERIC_INDEX,
884     .extensions = "gsm",
885     .value = CODEC_ID_GSM,
886 };
887 #endif
888
889 #if CONFIG_H261_DEMUXER
890 AVInputFormat h261_demuxer = {
891     "h261",
892     NULL_IF_CONFIG_SMALL("raw H.261"),
893     0,
894     h261_probe,
895     video_read_header,
896     ff_raw_read_partial_packet,
897     .flags= AVFMT_GENERIC_INDEX,
898     .extensions = "h261",
899     .value = CODEC_ID_H261,
900 };
901 #endif
902
903 #if CONFIG_H261_MUXER
904 AVOutputFormat h261_muxer = {
905     "h261",
906     NULL_IF_CONFIG_SMALL("raw H.261"),
907     "video/x-h261",
908     "h261",
909     0,
910     CODEC_ID_NONE,
911     CODEC_ID_H261,
912     NULL,
913     raw_write_packet,
914     .flags= AVFMT_NOTIMESTAMPS,
915 };
916 #endif
917
918 #if CONFIG_H263_DEMUXER
919 AVInputFormat h263_demuxer = {
920     "h263",
921     NULL_IF_CONFIG_SMALL("raw H.263"),
922     0,
923     h263_probe,
924     video_read_header,
925     ff_raw_read_partial_packet,
926     .flags= AVFMT_GENERIC_INDEX,
927 //    .extensions = "h263", //FIXME remove after writing mpeg4_probe
928     .value = CODEC_ID_H263,
929 };
930 #endif
931
932 #if CONFIG_H263_MUXER
933 AVOutputFormat h263_muxer = {
934     "h263",
935     NULL_IF_CONFIG_SMALL("raw H.263"),
936     "video/x-h263",
937     "h263",
938     0,
939     CODEC_ID_NONE,
940     CODEC_ID_H263,
941     NULL,
942     raw_write_packet,
943     .flags= AVFMT_NOTIMESTAMPS,
944 };
945 #endif
946
947 #if CONFIG_H264_DEMUXER
948 AVInputFormat h264_demuxer = {
949     "h264",
950     NULL_IF_CONFIG_SMALL("raw H.264 video format"),
951     0,
952     h264_probe,
953     video_read_header,
954     ff_raw_read_partial_packet,
955     .flags= AVFMT_GENERIC_INDEX,
956     .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
957     .value = CODEC_ID_H264,
958 };
959 #endif
960
961 #if CONFIG_H264_MUXER
962 AVOutputFormat h264_muxer = {
963     "h264",
964     NULL_IF_CONFIG_SMALL("raw H.264 video format"),
965     NULL,
966     "h264",
967     0,
968     CODEC_ID_NONE,
969     CODEC_ID_H264,
970     NULL,
971     raw_write_packet,
972     .flags= AVFMT_NOTIMESTAMPS,
973 };
974 #endif
975
976 #if CONFIG_INGENIENT_DEMUXER
977 AVInputFormat ingenient_demuxer = {
978     "ingenient",
979     NULL_IF_CONFIG_SMALL("raw Ingenient MJPEG"),
980     0,
981     NULL,
982     video_read_header,
983     ingenient_read_packet,
984     .flags= AVFMT_GENERIC_INDEX,
985     .extensions = "cgi", // FIXME
986     .value = CODEC_ID_MJPEG,
987 };
988 #endif
989
990 #if CONFIG_M4V_DEMUXER
991 AVInputFormat m4v_demuxer = {
992     "m4v",
993     NULL_IF_CONFIG_SMALL("raw MPEG-4 video format"),
994     0,
995     mpeg4video_probe, /** probing for MPEG-4 data */
996     video_read_header,
997     ff_raw_read_partial_packet,
998     .flags= AVFMT_GENERIC_INDEX,
999     .extensions = "m4v",
1000     .value = CODEC_ID_MPEG4,
1001 };
1002 #endif
1003
1004 #if CONFIG_M4V_MUXER
1005 AVOutputFormat m4v_muxer = {
1006     "m4v",
1007     NULL_IF_CONFIG_SMALL("raw MPEG-4 video format"),
1008     NULL,
1009     "m4v",
1010     0,
1011     CODEC_ID_NONE,
1012     CODEC_ID_MPEG4,
1013     NULL,
1014     raw_write_packet,
1015     .flags= AVFMT_NOTIMESTAMPS,
1016 };
1017 #endif
1018
1019 #if CONFIG_MJPEG_DEMUXER
1020 AVInputFormat mjpeg_demuxer = {
1021     "mjpeg",
1022     NULL_IF_CONFIG_SMALL("raw MJPEG video"),
1023     0,
1024     NULL,
1025     video_read_header,
1026     ff_raw_read_partial_packet,
1027     .flags= AVFMT_GENERIC_INDEX,
1028     .extensions = "mjpg,mjpeg",
1029     .value = CODEC_ID_MJPEG,
1030 };
1031 #endif
1032
1033 #if CONFIG_MJPEG_MUXER
1034 AVOutputFormat mjpeg_muxer = {
1035     "mjpeg",
1036     NULL_IF_CONFIG_SMALL("raw MJPEG video"),
1037     "video/x-mjpeg",
1038     "mjpg,mjpeg",
1039     0,
1040     CODEC_ID_NONE,
1041     CODEC_ID_MJPEG,
1042     NULL,
1043     raw_write_packet,
1044     .flags= AVFMT_NOTIMESTAMPS,
1045 };
1046 #endif
1047
1048 #if CONFIG_MLP_DEMUXER
1049 AVInputFormat mlp_demuxer = {
1050     "mlp",
1051     NULL_IF_CONFIG_SMALL("raw MLP"),
1052     0,
1053     NULL,
1054     audio_read_header,
1055     ff_raw_read_partial_packet,
1056     .flags= AVFMT_GENERIC_INDEX,
1057     .extensions = "mlp",
1058     .value = CODEC_ID_MLP,
1059 };
1060 #endif
1061
1062 #if CONFIG_MLP_MUXER
1063 AVOutputFormat mlp_muxer = {
1064     "mlp",
1065     NULL_IF_CONFIG_SMALL("raw MLP"),
1066     NULL,
1067     "mlp",
1068     0,
1069     CODEC_ID_MLP,
1070     CODEC_ID_NONE,
1071     NULL,
1072     raw_write_packet,
1073     .flags= AVFMT_NOTIMESTAMPS,
1074 };
1075 #endif
1076
1077 #if CONFIG_TRUEHD_DEMUXER
1078 AVInputFormat truehd_demuxer = {
1079     "truehd",
1080     NULL_IF_CONFIG_SMALL("raw TrueHD"),
1081     0,
1082     NULL,
1083     audio_read_header,
1084     ff_raw_read_partial_packet,
1085     .flags= AVFMT_GENERIC_INDEX,
1086     .extensions = "thd",
1087     .value = CODEC_ID_TRUEHD,
1088 };
1089 #endif
1090
1091 #if CONFIG_TRUEHD_MUXER
1092 AVOutputFormat truehd_muxer = {
1093     "truehd",
1094     NULL_IF_CONFIG_SMALL("raw TrueHD"),
1095     NULL,
1096     "thd",
1097     0,
1098     CODEC_ID_TRUEHD,
1099     CODEC_ID_NONE,
1100     NULL,
1101     raw_write_packet,
1102     .flags= AVFMT_NOTIMESTAMPS,
1103 };
1104 #endif
1105
1106 #if CONFIG_MPEG1VIDEO_MUXER
1107 AVOutputFormat mpeg1video_muxer = {
1108     "mpeg1video",
1109     NULL_IF_CONFIG_SMALL("raw MPEG-1 video"),
1110     "video/x-mpeg",
1111     "mpg,mpeg,m1v",
1112     0,
1113     CODEC_ID_NONE,
1114     CODEC_ID_MPEG1VIDEO,
1115     NULL,
1116     raw_write_packet,
1117     .flags= AVFMT_NOTIMESTAMPS,
1118 };
1119 #endif
1120
1121 #if CONFIG_MPEG2VIDEO_MUXER
1122 AVOutputFormat mpeg2video_muxer = {
1123     "mpeg2video",
1124     NULL_IF_CONFIG_SMALL("raw MPEG-2 video"),
1125     NULL,
1126     "m2v",
1127     0,
1128     CODEC_ID_NONE,
1129     CODEC_ID_MPEG2VIDEO,
1130     NULL,
1131     raw_write_packet,
1132     .flags= AVFMT_NOTIMESTAMPS,
1133 };
1134 #endif
1135
1136 #if CONFIG_MPEGVIDEO_DEMUXER
1137 AVInputFormat mpegvideo_demuxer = {
1138     "mpegvideo",
1139     NULL_IF_CONFIG_SMALL("raw MPEG video"),
1140     0,
1141     mpegvideo_probe,
1142     video_read_header,
1143     ff_raw_read_partial_packet,
1144     .flags= AVFMT_GENERIC_INDEX,
1145     .value = CODEC_ID_MPEG1VIDEO,
1146 };
1147 #endif
1148
1149 #if CONFIG_CAVSVIDEO_DEMUXER
1150 AVInputFormat cavsvideo_demuxer = {
1151     "cavsvideo",
1152     NULL_IF_CONFIG_SMALL("raw Chinese AVS video"),
1153     0,
1154     cavsvideo_probe,
1155     video_read_header,
1156     ff_raw_read_partial_packet,
1157     .flags= AVFMT_GENERIC_INDEX,
1158     .value = CODEC_ID_CAVS,
1159 };
1160 #endif
1161
1162 #if CONFIG_NULL_MUXER
1163 AVOutputFormat null_muxer = {
1164     "null",
1165     NULL_IF_CONFIG_SMALL("raw null video format"),
1166     NULL,
1167     NULL,
1168     0,
1169 #if HAVE_BIGENDIAN
1170     CODEC_ID_PCM_S16BE,
1171 #else
1172     CODEC_ID_PCM_S16LE,
1173 #endif
1174     CODEC_ID_RAWVIDEO,
1175     NULL,
1176     null_write_packet,
1177     .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE | AVFMT_NOTIMESTAMPS,
1178 };
1179 #endif
1180
1181 #if CONFIG_RAWVIDEO_DEMUXER
1182 AVInputFormat rawvideo_demuxer = {
1183     "rawvideo",
1184     NULL_IF_CONFIG_SMALL("raw video format"),
1185     0,
1186     NULL,
1187     raw_read_header,
1188     rawvideo_read_packet,
1189     .flags= AVFMT_GENERIC_INDEX,
1190     .extensions = "yuv,cif,qcif,rgb",
1191     .value = CODEC_ID_RAWVIDEO,
1192 };
1193 #endif
1194
1195 #if CONFIG_RAWVIDEO_MUXER
1196 AVOutputFormat rawvideo_muxer = {
1197     "rawvideo",
1198     NULL_IF_CONFIG_SMALL("raw video format"),
1199     NULL,
1200     "yuv,rgb",
1201     0,
1202     CODEC_ID_NONE,
1203     CODEC_ID_RAWVIDEO,
1204     NULL,
1205     raw_write_packet,
1206     .flags= AVFMT_NOTIMESTAMPS,
1207 };
1208 #endif
1209
1210 #if CONFIG_ROQ_MUXER
1211 AVOutputFormat roq_muxer =
1212 {
1213     "RoQ",
1214     NULL_IF_CONFIG_SMALL("raw id RoQ format"),
1215     NULL,
1216     "roq",
1217     0,
1218     CODEC_ID_ROQ_DPCM,
1219     CODEC_ID_ROQ,
1220     roq_write_header,
1221     raw_write_packet,
1222 };
1223 #endif
1224
1225 #if CONFIG_SHORTEN_DEMUXER
1226 AVInputFormat shorten_demuxer = {
1227     "shn",
1228     NULL_IF_CONFIG_SMALL("raw Shorten"),
1229     0,
1230     NULL,
1231     audio_read_header,
1232     ff_raw_read_partial_packet,
1233     .flags= AVFMT_GENERIC_INDEX,
1234     .extensions = "shn",
1235     .value = CODEC_ID_SHORTEN,
1236 };
1237 #endif
1238
1239 #if CONFIG_VC1_DEMUXER
1240 AVInputFormat vc1_demuxer = {
1241     "vc1",
1242     NULL_IF_CONFIG_SMALL("raw VC-1"),
1243     0,
1244     NULL /* vc1_probe */,
1245     video_read_header,
1246     ff_raw_read_partial_packet,
1247     .extensions = "vc1",
1248     .value = CODEC_ID_VC1,
1249 };
1250 #endif
1251
1252 /* PCM formats */
1253
1254 #define PCMINPUTDEF(name, long_name, ext, codec) \
1255 AVInputFormat pcm_ ## name ## _demuxer = {\
1256     #name,\
1257     NULL_IF_CONFIG_SMALL(long_name),\
1258     0,\
1259     NULL,\
1260     raw_read_header,\
1261     raw_read_packet,\
1262     NULL,\
1263     pcm_read_seek,\
1264     .flags= AVFMT_GENERIC_INDEX,\
1265     .extensions = ext,\
1266     .value = codec,\
1267 };
1268
1269 #define PCMOUTPUTDEF(name, long_name, ext, codec) \
1270 AVOutputFormat pcm_ ## name ## _muxer = {\
1271     #name,\
1272     NULL_IF_CONFIG_SMALL(long_name),\
1273     NULL,\
1274     ext,\
1275     0,\
1276     codec,\
1277     CODEC_ID_NONE,\
1278     NULL,\
1279     raw_write_packet,\
1280     .flags= AVFMT_NOTIMESTAMPS,\
1281 };
1282
1283
1284 #if  !CONFIG_MUXERS && CONFIG_DEMUXERS
1285 #define PCMDEF(name, long_name, ext, codec) \
1286         PCMINPUTDEF(name, long_name, ext, codec)
1287 #elif CONFIG_MUXERS && !CONFIG_DEMUXERS
1288 #define PCMDEF(name, long_name, ext, codec) \
1289         PCMOUTPUTDEF(name, long_name, ext, codec)
1290 #elif CONFIG_MUXERS && CONFIG_DEMUXERS
1291 #define PCMDEF(name, long_name, ext, codec) \
1292         PCMINPUTDEF(name, long_name, ext, codec)\
1293         PCMOUTPUTDEF(name, long_name, ext, codec)
1294 #else
1295 #define PCMDEF(name, long_name, ext, codec)
1296 #endif
1297
1298 #if HAVE_BIGENDIAN
1299 #define BE_DEF(s) s
1300 #define LE_DEF(s) NULL
1301 #else
1302 #define BE_DEF(s) NULL
1303 #define LE_DEF(s) s
1304 #endif
1305
1306 PCMDEF(f64be, "PCM 64 bit floating-point big-endian format",
1307        NULL, CODEC_ID_PCM_F64BE)
1308
1309 PCMDEF(f64le, "PCM 64 bit floating-point little-endian format",
1310        NULL, CODEC_ID_PCM_F64LE)
1311
1312 PCMDEF(f32be, "PCM 32 bit floating-point big-endian format",
1313        NULL, CODEC_ID_PCM_F32BE)
1314
1315 PCMDEF(f32le, "PCM 32 bit floating-point little-endian format",
1316        NULL, CODEC_ID_PCM_F32LE)
1317
1318 PCMDEF(s32be, "PCM signed 32 bit big-endian format",
1319        NULL, CODEC_ID_PCM_S32BE)
1320
1321 PCMDEF(s32le, "PCM signed 32 bit little-endian format",
1322        NULL, CODEC_ID_PCM_S32LE)
1323
1324 PCMDEF(s24be, "PCM signed 24 bit big-endian format",
1325        NULL, CODEC_ID_PCM_S24BE)
1326
1327 PCMDEF(s24le, "PCM signed 24 bit little-endian format",
1328        NULL, CODEC_ID_PCM_S24LE)
1329
1330 PCMDEF(s16be, "PCM signed 16 bit big-endian format",
1331        BE_DEF("sw"), CODEC_ID_PCM_S16BE)
1332
1333 PCMDEF(s16le, "PCM signed 16 bit little-endian format",
1334        LE_DEF("sw"), CODEC_ID_PCM_S16LE)
1335
1336 PCMDEF(s8, "PCM signed 8 bit format",
1337        "sb", CODEC_ID_PCM_S8)
1338
1339 PCMDEF(u32be, "PCM unsigned 32 bit big-endian format",
1340        NULL, CODEC_ID_PCM_U32BE)
1341
1342 PCMDEF(u32le, "PCM unsigned 32 bit little-endian format",
1343        NULL, CODEC_ID_PCM_U32LE)
1344
1345 PCMDEF(u24be, "PCM unsigned 24 bit big-endian format",
1346        NULL, CODEC_ID_PCM_U24BE)
1347
1348 PCMDEF(u24le, "PCM unsigned 24 bit little-endian format",
1349        NULL, CODEC_ID_PCM_U24LE)
1350
1351 PCMDEF(u16be, "PCM unsigned 16 bit big-endian format",
1352        BE_DEF("uw"), CODEC_ID_PCM_U16BE)
1353
1354 PCMDEF(u16le, "PCM unsigned 16 bit little-endian format",
1355        LE_DEF("uw"), CODEC_ID_PCM_U16LE)
1356
1357 PCMDEF(u8, "PCM unsigned 8 bit format",
1358        "ub", CODEC_ID_PCM_U8)
1359
1360 PCMDEF(alaw, "PCM A-law format",
1361        "al", CODEC_ID_PCM_ALAW)
1362
1363 PCMDEF(mulaw, "PCM mu-law format",
1364        "ul", CODEC_ID_PCM_MULAW)