1#include "avformat.h" 2 3#define PROBE_BUF_MIN 2048 4#define PROBE_BUF_MAX (1<<20) 5 6extern AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max); 7 8int ms_av_open_input_file(AVFormatContext **ic_ptr, const char *filename, 9 AVInputFormat *fmt, 10 int buf_size, 11 AVFormatParameters *ap) { 12 13 int err, probe_size; 14 AVProbeData probe_data, *pd = &probe_data; 15 ByteIOContext *pb = NULL; 16 17 pd->filename = ""; 18 if (filename) 19 pd->filename = filename; 20 pd->buf = NULL; 21 pd->buf_size = 0; 22 23 if (!fmt) { 24 /* guess format if no file can be opened */ 25 fmt = av_probe_input_format(pd, 0); 26 } 27 /* Do not open file if the format does not need it. XXX: specific 28 hack needed to handle RTSP/TCP */ 29 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) { 30 /* if no file needed do not try to open one */ 31 if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) { 32 goto fail; 33 } 34 if (buf_size > 0) { 35 url_setbufsize(pb, buf_size); 36 } 37 for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){ 38 int score= probe_size < PROBE_BUF_MAX ? AVPROBE_SCORE_MAX/4 : 0; 39 /* read probe data */ 40 pd->buf= av_realloc(pd->buf, probe_size + AVPROBE_PADDING_SIZE); 41 pd->buf_size = get_buffer(pb, pd->buf, probe_size); 42 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE); 43 if (url_fseek(pb, 0, SEEK_SET) < 0) { 44 url_fclose(pb); 45 if (url_fopen(&pb, filename, URL_RDONLY) < 0) { 46 pb = NULL; 47 err = AVERROR(EIO); 48 goto fail; 49 } 50 } 51 /* guess file format */ 52 fmt = av_probe_input_format2(pd, 1, &score); 53 } 54 av_freep(&pd->buf); 55 } 56 /* if still no format found, error */ 57 if (!fmt) { 58 err = AVERROR_NOFMT; 59 goto fail; 60 } 61 62 /* check filename in case an image number is expected */ 63 if (fmt->flags & AVFMT_NEEDNUMBER) { 64 if (!av_filename_number_test(filename)) { 65 err = AVERROR_NUMEXPECTED; 66 goto fail; 67 } 68 } 69 err = ms_av_open_input_stream(ic_ptr, pb, filename, fmt, ap); 70 if (err) 71 goto fail; 72 return 0; 73 fail: 74 av_freep(&pd->buf); 75 if (pb) 76 url_fclose(pb); 77 *ic_ptr = NULL; 78 return err; 79} 80 81int ms_av_open_input_stream(AVFormatContext **ic_ptr, 82 ByteIOContext *pb, const char *filename, 83 AVInputFormat *fmt, AVFormatParameters *ap) { 84 int err; 85 AVFormatContext *ic; 86 AVFormatParameters default_ap; 87 88 if(!ap){ 89 ap=&default_ap; 90 memset(ap, 0, sizeof(default_ap)); 91 } 92 93 if(!ap->prealloced_context) 94 ic = av_alloc_format_context(); 95 else 96 ic = *ic_ptr; 97 if (!ic) { 98 err = AVERROR(ENOMEM); 99 goto fail; 100 } 101 ic->iformat = fmt; 102 ic->pb = pb; 103 ic->duration = AV_NOPTS_VALUE; 104 ic->start_time = AV_NOPTS_VALUE; 105 av_strlcpy(ic->filename, filename, sizeof(ic->filename)); 106 107 ic->ms_flag = 1; 108 109 /* allocate private data */ 110 if (fmt->priv_data_size > 0) { 111 ic->priv_data = av_mallocz(fmt->priv_data_size); 112 if (!ic->priv_data) { 113 err = AVERROR(ENOMEM); 114 goto fail; 115 } 116 } else { 117 ic->priv_data = NULL; 118 } 119 err = ic->iformat->read_header(ic, ap); 120 if (err < 0) 121 goto fail; 122 123 if (pb && !ic->data_offset) 124 ic->data_offset = url_ftell(ic->pb); 125 126 *ic_ptr = ic; 127 return 0; 128 fail: 129 if (ic) { 130 av_freep(&ic->priv_data); 131 } 132 av_free(ic); 133 *ic_ptr = NULL; 134 return err; 135} 136