summaryrefslogtreecommitdiffstats
path: root/media/ffmpeg/ffmpeg_common.cc
diff options
context:
space:
mode:
authoracolwell@chromium.org <acolwell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-01 14:18:55 +0000
committeracolwell@chromium.org <acolwell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-01 14:18:55 +0000
commitb8b9d30cc9a9fb93ffa6a301bcec79ed85239c4a (patch)
tree7feb05a189dc3c5781563ecc7798cf7ee3ad6126 /media/ffmpeg/ffmpeg_common.cc
parentddbd222c8dd1e08fb993b42adfc5159841755266 (diff)
downloadchromium_src-b8b9d30cc9a9fb93ffa6a301bcec79ed85239c4a.zip
chromium_src-b8b9d30cc9a9fb93ffa6a301bcec79ed85239c4a.tar.gz
chromium_src-b8b9d30cc9a9fb93ffa6a301bcec79ed85239c4a.tar.bz2
Create helper methods to process FFmpeg index data.
BUG=none TEST=FFmpegCommonTest Review URL: http://codereview.chromium.org/6708130 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80153 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/ffmpeg/ffmpeg_common.cc')
-rw-r--r--media/ffmpeg/ffmpeg_common.cc82
1 files changed, 81 insertions, 1 deletions
diff --git a/media/ffmpeg/ffmpeg_common.cc b/media/ffmpeg/ffmpeg_common.cc
index fd82936..fc59709 100644
--- a/media/ffmpeg/ffmpeg_common.cc
+++ b/media/ffmpeg/ffmpeg_common.cc
@@ -10,11 +10,17 @@ namespace media {
static const AVRational kMicrosBase = { 1, base::Time::kMicrosecondsPerSecond };
-base::TimeDelta ConvertTimestamp(const AVRational& time_base, int64 timestamp) {
+base::TimeDelta ConvertFromTimeBase(const AVRational& time_base,
+ int64 timestamp) {
int64 microseconds = av_rescale_q(timestamp, time_base, kMicrosBase);
return base::TimeDelta::FromMicroseconds(microseconds);
}
+int64 ConvertToTimeBase(const AVRational& time_base,
+ const base::TimeDelta& timestamp) {
+ return av_rescale_q(timestamp.InMicroseconds(), kMicrosBase, time_base);
+}
+
VideoCodec CodecIDToVideoCodec(CodecID codec_id) {
switch (codec_id) {
case CODEC_ID_VC1:
@@ -55,4 +61,78 @@ CodecID VideoCodecToCodecID(VideoCodec video_codec) {
return CODEC_ID_NONE;
}
+bool GetSeekTimeAfter(AVStream* stream, const base::TimeDelta& timestamp,
+ base::TimeDelta* seek_time) {
+ DCHECK(stream);
+ DCHECK(timestamp >= base::TimeDelta::FromSeconds(0));
+ DCHECK(seek_time);
+
+ // Make sure we have index data.
+ if (!stream->index_entries || stream->nb_index_entries <= 0)
+ return false;
+
+ // Search for the index entry >= the specified timestamp.
+ int64 stream_ts = ConvertToTimeBase(stream->time_base, timestamp);
+ int i = av_index_search_timestamp(stream, stream_ts, 0);
+
+ if (i < 0)
+ return false;
+
+ if (stream->index_entries[i].timestamp == static_cast<int64>(AV_NOPTS_VALUE))
+ return false;
+
+ *seek_time = ConvertFromTimeBase(stream->time_base,
+ stream->index_entries[i].timestamp);
+ return true;
+}
+
+
+bool GetStreamByteCountOverRange(AVStream* stream,
+ const base::TimeDelta& start_time,
+ const base::TimeDelta& end_time,
+ int64* bytes,
+ base::TimeDelta* range_start,
+ base::TimeDelta* range_end) {
+ DCHECK(stream);
+ DCHECK(start_time < end_time);
+ DCHECK(start_time >= base::TimeDelta::FromSeconds(0));
+ DCHECK(bytes);
+ DCHECK(range_start);
+ DCHECK(range_end);
+
+ // Make sure the stream has index data.
+ if (!stream->index_entries || stream->nb_index_entries <= 1)
+ return false;
+
+ // Search for the index entries associated with the timestamps.
+ int64 start_ts = ConvertToTimeBase(stream->time_base, start_time);
+ int64 end_ts = ConvertToTimeBase(stream->time_base, end_time);
+ int i = av_index_search_timestamp(stream, start_ts, AVSEEK_FLAG_BACKWARD);
+ int j = av_index_search_timestamp(stream, end_ts, 0);
+
+ // Make sure start & end indexes are valid.
+ if (i < 0 || j < 0)
+ return false;
+
+ // Shouldn't happen because start & end use different seek flags, but we want
+ // to know about it if they end up pointing to the same index entry.
+ DCHECK_NE(i, j);
+
+ AVIndexEntry* start_ie = &stream->index_entries[i];
+ AVIndexEntry* end_ie = &stream->index_entries[j];
+
+ // Make sure index entries have valid timestamps & position data.
+ if (start_ie->timestamp == static_cast<int64>(AV_NOPTS_VALUE) ||
+ end_ie->timestamp == static_cast<int64>(AV_NOPTS_VALUE) ||
+ start_ie->timestamp >= end_ie->timestamp ||
+ start_ie->pos >= end_ie->pos) {
+ return false;
+ }
+
+ *bytes = end_ie->pos - start_ie->pos;
+ *range_start = ConvertFromTimeBase(stream->time_base, start_ie->timestamp);
+ *range_end = ConvertFromTimeBase(stream->time_base, end_ie->timestamp);
+ return true;
+}
+
} // namespace media