AVFormatContext * fa_libav_open_format(AVIOContext *avio, const char *url, char *errbuf, size_t errlen) { AVInputFormat *fmt = NULL; AVFormatContext *fctx; int err; avio_seek(avio, 0, SEEK_SET); if((err = av_probe_input_buffer(avio, &fmt, url, NULL, 0, 0)) != 0) return fa_libav_open_error(errbuf, errlen, "Unable to probe file", err); if(fmt == NULL) { snprintf(errbuf, errlen, "Unknown file format"); return NULL; } if((err = av_open_input_stream(&fctx, avio, url, fmt, NULL)) != 0) return fa_libav_open_error(errbuf, errlen, "Unable to open file as input format", err); if(av_find_stream_info(fctx) < 0) { av_close_input_stream(fctx); return fa_libav_open_error(errbuf, errlen, "Unable to handle file contents", err); } return fctx; }
AVFormatContext * fa_libav_open_format(AVIOContext *avio, const char *url, char *errbuf, size_t errlen, const char *mimetype) { AVInputFormat *fmt = NULL; AVFormatContext *fctx; int err; avio_seek(avio, 0, SEEK_SET); if(mimetype != NULL) { int i; for(i = 0; i < sizeof(mimetype2fmt) / sizeof(mimetype2fmt[0]); i++) { if(!strcmp(mimetype, mimetype2fmt[i].mimetype)) { fmt = av_find_input_format(mimetype2fmt[i].fmt); break; } } if(fmt == NULL) TRACE(TRACE_DEBUG, "probe", "Don't know mimetype %s, probing instead", mimetype); } if(fmt == NULL) { if((err = av_probe_input_buffer(avio, &fmt, url, NULL, 0, 0)) != 0) return fa_libav_open_error(errbuf, errlen, "Unable to probe file", err); if(fmt == NULL) { snprintf(errbuf, errlen, "Unknown file format"); return NULL; } } fctx = avformat_alloc_context(); fctx->pb = avio; if((err = avformat_open_input(&fctx, url, fmt, NULL)) != 0) { if(mimetype != NULL) return fa_libav_open_format(avio, url, errbuf, errlen, NULL); return fa_libav_open_error(errbuf, errlen, "Unable to open file as input format", err); } if(av_find_stream_info(fctx) < 0) { av_close_input_stream(fctx); if(mimetype != NULL) return fa_libav_open_format(avio, url, errbuf, errlen, NULL); return fa_libav_open_error(errbuf, errlen, "Unable to handle file contents", err); } return fctx; }
AVFormatContext * fa_libav_open_format(AVIOContext *avio, const char *url, char *errbuf, size_t errlen, const char *mimetype, int probe_size, int max_analyze_duration, int fps_probe_frames) { AVInputFormat *fmt = NULL; AVFormatContext *fctx; int err; avio_seek(avio, 0, SEEK_SET); if(mimetype != NULL) { int i; for(i = 0; i < sizeof(mimetype2fmt) / sizeof(mimetype2fmt[0]); i++) { if(!strcasecmp(mimetype, mimetype2fmt[i].mimetype)) { fmt = av_find_input_format(mimetype2fmt[i].fmt); break; } } if(fmt == NULL) TRACE(TRACE_DEBUG, "probe", "%s: Don't know mimetype %s, probing instead", url, mimetype); } if(fmt == NULL) { if((err = av_probe_input_buffer(avio, &fmt, url, NULL, 0, probe_size)) != 0) return fa_libav_open_error(errbuf, errlen, "Unable to probe file", err); if(fmt == NULL) { snprintf(errbuf, errlen, "Unknown file format"); return NULL; } TRACE(TRACE_DEBUG, "probe", "%s: Probed as %s", url, fmt->name); } fctx = avformat_alloc_context(); fctx->pb = avio; if(max_analyze_duration != -1) fctx->max_analyze_duration = max_analyze_duration; if((err = avformat_open_input(&fctx, url, fmt, NULL)) != 0) { if(mimetype != NULL) { TRACE(TRACE_DEBUG, "libav", "Unable to open using mimetype %s, retrying with probe", mimetype); return fa_libav_open_format(avio, url, errbuf, errlen, NULL, probe_size, max_analyze_duration, fps_probe_frames); } return fa_libav_open_error(errbuf, errlen, "Unable to open file as input format", err); } if(fps_probe_frames != -1) fctx->fps_probe_size = fps_probe_frames; if(avformat_find_stream_info(fctx, NULL) < 0) { avformat_close_input(&fctx); if(mimetype != NULL) { TRACE(TRACE_DEBUG, "libav", "Unable to find stream info using mimetype %s, retrying with probe", mimetype); return fa_libav_open_format(avio, url, errbuf, errlen, NULL, probe_size, max_analyze_duration, fps_probe_frames); } return fa_libav_open_error(errbuf, errlen, "Unable to handle file contents", err); } return fctx; }