Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(905)

Unified Diff: media/filters/ffmpeg_glue.cc

Issue 2819863003: Only run container name detection upon ffmpeg parse failure. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/container_names.cc ('k') | media/filters/ffmpeg_glue_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/filters/ffmpeg_glue.cc
diff --git a/media/filters/ffmpeg_glue.cc b/media/filters/ffmpeg_glue.cc
index 77cf6700868cd63c442e5b87e3cabc2c856cd902..c1a2145d4c1f343635b884861d9a45c8ad0f0eb7 100644
--- a/media/filters/ffmpeg_glue.cc
+++ b/media/filters/ffmpeg_glue.cc
@@ -137,25 +137,63 @@ bool FFmpegGlue::OpenContext() {
// destruction path to avoid double frees.
open_called_ = true;
- // Attempt to recognize the container by looking at the first few bytes of the
- // stream. The stream position is left unchanged.
- std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(8192));
-
- int64_t pos = AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_CUR);
- AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_SET);
- int numRead = AVIOReadOperation(
- avio_context_.get()->opaque, buffer.get()->data(), buffer.get()->size());
- AVIOSeekOperation(avio_context_.get()->opaque, pos, SEEK_SET);
- if (numRead > 0) {
- // < 0 means Read failed
- container_names::MediaContainerName container =
- container_names::DetermineContainer(buffer.get()->data(), numRead);
- UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", container);
- }
-
// By passing nullptr for the filename (second parameter) we are telling
// FFmpeg to use the AVIO context we setup from the AVFormatContext structure.
- return avformat_open_input(&format_context_, nullptr, nullptr, nullptr) == 0;
+ const int ret =
+ avformat_open_input(&format_context_, nullptr, nullptr, nullptr);
+
+ // If FFmpeg can't identify the file, read the first 8k and attempt to guess
+ // at the container type ourselves. This way we can track emergent formats.
+ // Only try on AVERROR_INVALIDDATA to avoid running after I/O errors.
+ if (ret == AVERROR_INVALIDDATA) {
+ std::vector<uint8_t> buffer(8192);
+
+ const int64_t pos = AVIOSeekOperation(avio_context_->opaque, 0, SEEK_SET);
+ if (pos < 0)
+ return false;
+
+ const int num_read =
+ AVIOReadOperation(avio_context_->opaque, buffer.data(), buffer.size());
+ if (num_read < container_names::kMinimumContainerSize)
+ return false;
+
+ UMA_HISTOGRAM_SPARSE_SLOWLY(
+ "Media.DetectedContainer",
+ container_names::DetermineContainer(buffer.data(), num_read));
+ return false;
+ } else if (ret < 0) {
+ return false;
+ }
+
+ // Rely on ffmpeg's parsing if we're able to succesfully open the file.
+ container_names::MediaContainerName container =
+ container_names::CONTAINER_UNKNOWN;
+ if (strcmp(format_context_->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") == 0)
jrummell 2017/04/17 18:00:15 Too bad there isn't a better way to compute this w
DaleCurtis 2017/04/17 19:08:46 Yeah, hence all the test cases to make sure we sti
+ container = container_names::CONTAINER_MOV;
+ else if (strcmp(format_context_->iformat->name, "flac") == 0)
+ container = container_names::CONTAINER_FLAC;
+ else if (strcmp(format_context_->iformat->name, "matroska,webm") == 0)
+ container = container_names::CONTAINER_WEBM;
+ else if (strcmp(format_context_->iformat->name, "ogg") == 0)
+ container = container_names::CONTAINER_OGG;
+ else if (strcmp(format_context_->iformat->name, "wav") == 0)
+ container = container_names::CONTAINER_WAV;
+ else if (strcmp(format_context_->iformat->name, "aac") == 0)
+ container = container_names::CONTAINER_AAC;
+ else if (strcmp(format_context_->iformat->name, "mp3") == 0)
+ container = container_names::CONTAINER_MP3;
+ else if (strcmp(format_context_->iformat->name, "amr") == 0)
+ container = container_names::CONTAINER_AMR;
+ else if (strcmp(format_context_->iformat->name, "avi") == 0)
+ container = container_names::CONTAINER_AVI;
+ // TODO(jrummell): Remove GSM detection. http://crbug.com/711774
+ else if (strcmp(format_context_->iformat->name, "gsm") == 0)
+ container = container_names::CONTAINER_GSM;
+
+ DCHECK_NE(container, container_names::CONTAINER_UNKNOWN);
+ UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", container);
+
+ return true;
}
FFmpegGlue::~FFmpegGlue() {
« no previous file with comments | « media/base/container_names.cc ('k') | media/filters/ffmpeg_glue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698