static AVInputFormat * get_format_by_content (const gchar * name, VFSFile * file) { AUDDBG ("Get format by content: %s\n", name); AVInputFormat * f = NULL; guchar buf[16384 + AVPROBE_PADDING_SIZE]; gint size = 16; gint filled = 0; gint target = 100; gint score = 0; while (1) { if (filled < size) filled += vfs_fread (buf + filled, 1, size - filled, file); memset (buf + filled, 0, AVPROBE_PADDING_SIZE); AVProbeData d = {name, buf, filled}; score = target; f = av_probe_input_format2 (& d, TRUE, & score); if (f) break; if (size < 16384 && filled == size) size *= 4; else if (target > 10) target = 10; else break; } if (f) AUDDBG ("Format %s, buffer size %d, score %d.\n", f->name, filled, score); else AUDDBG ("Format unknown.\n"); if (vfs_fseek (file, 0, SEEK_SET) < 0) ; /* ignore errors here */ return f; }
AVbinFile *avbin_open_filename_with_format(const char *filename, char* format) { AVbinFile *file = malloc(sizeof *file); AVInputFormat *avformat = NULL; if(format!=NULL && avformat==NULL) avformat = av_find_input_format(format); if(avformat==NULL) { AVProbeData probe_data; probe_data.filename = filename; probe_data.buf_size = 4096000; probe_data.buf = malloc(probe_data.buf_size); int maxScore = 0; avformat = av_probe_input_format2(&probe_data, 1, &maxScore); free(probe_data.buf); } if(avformat!=NULL) { int test = avformat->flags; } file->context = NULL; // Zero-initialize if (avformat_open_input(&file->context, filename, avformat, NULL) != 0) goto error; if (avformat_find_stream_info(file->context, NULL) < 0) goto error; int flags = file->context->iformat->flags; file->packet = NULL; return file; error: free(file); return NULL; }
lp_ac_proberesult CALL_CONVT ac_probe_input_buffer( void* buf, int bufsize, char* filename, int* score_max) { AVProbeData pd; AVInputFormat *fmt = NULL; //Initialize FFMpeg libraries ac_init_ffmpeg(); //Set the filename pd.filename = ""; if (filename) { pd.filename = filename; } //The given buffer has to be copied to a new one, which is aligned and padded void* aligned_buf = av_malloc(bufsize + AVPROBE_PADDING_SIZE); memset(aligned_buf, 0, bufsize + AVPROBE_PADDING_SIZE); memcpy(aligned_buf, buf, bufsize); //Set the probe data buffer pd.buf = aligned_buf; pd.buf_size = bufsize; //Test it fmt = (AVInputFormat*)av_probe_input_format2(&pd, 1, score_max); //Free the temporary buffer av_free(aligned_buf); return (lp_ac_proberesult)fmt; }
int av_probe_input_buffer2(AVIOContext *pb, AVInputFormat **fmt, const char *filename, void *logctx, unsigned int offset, unsigned int max_probe_size) { AVProbeData pd = { filename ? filename : "" }; uint8_t *buf = NULL; int ret = 0, probe_size, buf_offset = 0; int score = 0; int ret2; if (!max_probe_size) max_probe_size = PROBE_BUF_MAX; else if (max_probe_size < PROBE_BUF_MIN) { av_log(logctx, AV_LOG_ERROR, "Specified probe size value %u cannot be < %u\n", max_probe_size, PROBE_BUF_MIN); return AVERROR(EINVAL); } if (offset >= max_probe_size) return AVERROR(EINVAL); if (pb->av_class) { uint8_t *mime_type_opt = NULL; char *semi; av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt); pd.mime_type = (const char *)mime_type_opt; semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL; if (semi) { *semi = '\0'; } } #if 0 if (!*fmt && pb->av_class && av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type) >= 0 && mime_type) { if (!av_strcasecmp(mime_type, "audio/aacp")) { *fmt = av_find_input_format("aac"); } av_freep(&mime_type); } #endif for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt; probe_size = FFMIN(probe_size << 1, FFMAX(max_probe_size, probe_size + 1))) { score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0; /* Read probe data. */ if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0) goto fail; if ((ret = avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) { /* Fail if error was not end of file, otherwise, lower score. */ if (ret != AVERROR_EOF) goto fail; score = 0; ret = 0; /* error was end of file, nothing read */ } buf_offset += ret; if (buf_offset < offset) continue; pd.buf_size = buf_offset - offset; pd.buf = &buf[offset]; memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE); /* Guess file format. */ *fmt = av_probe_input_format2(&pd, 1, &score); if (*fmt) { /* This can only be true in the last iteration. */ if (score <= AVPROBE_SCORE_RETRY) { av_log(logctx, AV_LOG_WARNING, "Format %s detected only with low score of %d, " "misdetection possible!\n", (*fmt)->name, score); } else av_log(logctx, AV_LOG_DEBUG, "Format %s probed with size=%d and score=%d\n", (*fmt)->name, probe_size, score); #if 0 FILE *f = fopen("probestat.tmp", "ab"); fprintf(f, "probe_size:%d format:%s score:%d filename:%s\n", probe_size, (*fmt)->name, score, filename); fclose(f); #endif } } if (!*fmt) ret = AVERROR_INVALIDDATA; fail: /* Rewind. Reuse probe buffer to avoid seeking. */ ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset); if (ret >= 0) ret = ret2; av_freep(&pd.mime_type); return ret < 0 ? ret : score; }
AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) { int score = 0; return av_probe_input_format2(pd, is_opened, &score); }
static int lavf_check_file(demuxer_t *demuxer){ AVProbeData avpd; lavf_priv_t *priv; int probe_data_size = 0; int read_size = INITIAL_PROBE_SIZE; int score; if(!demuxer->priv) demuxer->priv=calloc(sizeof(lavf_priv_t),1); priv= demuxer->priv; init_avformat(); if (opt_format) { if (strcmp(opt_format, "help") == 0) { list_formats(); return 0; } priv->avif= av_find_input_format(opt_format); if (!priv->avif) { mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format); return 0; } mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name); return DEMUXER_TYPE_LAVF; } avpd.buf = av_mallocz(FFMAX(BIO_BUFFER_SIZE, PROBE_BUF_SIZE) + FF_INPUT_BUFFER_PADDING_SIZE); do { read_size = stream_read(demuxer->stream, avpd.buf + probe_data_size, read_size); if(read_size < 0) { av_free(avpd.buf); return 0; } probe_data_size += read_size; avpd.filename= demuxer->stream->url; if (!avpd.filename) { mp_msg(MSGT_DEMUX, MSGL_WARN, "Stream url is not set!\n"); avpd.filename = ""; } if (!strncmp(avpd.filename, "ffmpeg://", 9)) avpd.filename += 9; avpd.buf_size= probe_data_size; score = 0; priv->avif= av_probe_input_format2(&avpd, probe_data_size > 0, &score); read_size = FFMIN(2*read_size, PROBE_BUF_SIZE - probe_data_size); } while ((demuxer->desc->type != DEMUXER_TYPE_LAVF_PREFERRED || probe_data_size < SMALL_MAX_PROBE_SIZE) && score <= AVPROBE_SCORE_MAX / 4 && read_size > 0 && probe_data_size < PROBE_BUF_SIZE); av_free(avpd.buf); if(!priv->avif){ mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n"); return 0; }else mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name); return DEMUXER_TYPE_LAVF; }
int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *filename, void *logctx, unsigned int offset, unsigned int max_probe_size) { AVProbeData pd = { filename ? filename : "" }; uint8_t *buf = NULL; int ret = 0, probe_size; if (!max_probe_size) max_probe_size = PROBE_BUF_MAX; else if (max_probe_size > PROBE_BUF_MAX) max_probe_size = PROBE_BUF_MAX; else if (max_probe_size < PROBE_BUF_MIN) return AVERROR(EINVAL); if (offset >= max_probe_size) return AVERROR(EINVAL); avio_skip(pb, offset); max_probe_size -= offset; if (pb->av_class) av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &pd.mime_type); for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt; probe_size = FFMIN(probe_size << 1, FFMAX(max_probe_size, probe_size + 1))) { int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0; /* Read probe data. */ if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0) goto fail; if ((ret = avio_read(pb, buf + pd.buf_size, probe_size - pd.buf_size)) < 0) { /* Fail if error was not end of file, otherwise, lower score. */ if (ret != AVERROR_EOF) goto fail; score = 0; ret = 0; /* error was end of file, nothing read */ } pd.buf_size += ret; pd.buf = buf; memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE); /* Guess file format. */ *fmt = av_probe_input_format2(&pd, 1, &score); if (*fmt) { /* This can only be true in the last iteration. */ if (score <= AVPROBE_SCORE_MAX / 4) { av_log(logctx, AV_LOG_WARNING, "Format detected only with low score of %d, " "misdetection possible!\n", score); } else av_log(logctx, AV_LOG_DEBUG, "Probed with size=%d and score=%d\n", probe_size, score); } } if (!*fmt) ret = AVERROR_INVALIDDATA; fail: /* Rewind. Reuse probe buffer to avoid seeking. */ if (ret < 0 || (ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0) { av_free(buf); } av_free(pd.mime_type); return ret; }