OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
acolwell GONE FROM CHROMIUM
2014/01/07 00:27:24
ditto
tommycli
2014/01/07 00:55:28
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/audio_video_metadata_extractor.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/time/time.h" | |
13 #include "media/ffmpeg/ffmpeg_common.h" | |
14 #include "media/filters/blocking_url_protocol.h" | |
15 #include "media/filters/ffmpeg_glue.h" | |
16 | |
17 namespace media { | |
18 | |
19 namespace { | |
20 | |
21 static void OnError(bool* called) { | |
22 *called = false; | |
23 } | |
24 | |
25 // Returns true if the |expected_key| matches |key_lower|. | |
26 bool ExtractString(AVDictionaryEntry* tag, const std::string& key_lower, | |
27 const std::string& expected_key, std::string* destination) { | |
28 if (key_lower != expected_key) | |
29 return false; | |
30 | |
31 if (destination->empty()) | |
32 *destination = tag->value; | |
33 | |
34 return true; | |
35 } | |
36 | |
37 // Returns true if the |expected_key| matches |key_lower|. | |
38 bool ExtractInt(AVDictionaryEntry* tag, const std::string& key_lower, | |
39 const std::string& expected_key, int* destination) { | |
40 if (key_lower != expected_key) | |
41 return false; | |
42 | |
43 if (*destination < 0) | |
44 base::StringToInt(tag->value, destination); | |
acolwell GONE FROM CHROMIUM
2014/01/07 00:27:24
nit: You might want to stage the parsed value in a
tommycli
2014/01/07 00:55:28
Done.
| |
45 | |
46 return true; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 AudioVideoMetadataExtractor::AudioVideoMetadataExtractor() | |
52 : extracted_(false), | |
53 duration_(-1), | |
54 width_(-1), | |
55 height_(-1), | |
56 disc_(-1), | |
57 track_(-1) { | |
58 } | |
59 | |
60 AudioVideoMetadataExtractor::~AudioVideoMetadataExtractor() { | |
61 } | |
62 | |
63 bool AudioVideoMetadataExtractor::Extract(media::DataSource* source) { | |
64 DCHECK(!extracted_); | |
65 | |
66 bool read_ok = true; | |
67 media::BlockingUrlProtocol protocol(source, base::Bind(&OnError, &read_ok)); | |
68 media::FFmpegGlue glue(&protocol); | |
69 AVFormatContext* format_context = glue.format_context(); | |
70 | |
71 if (!glue.OpenContext()) | |
72 return false; | |
73 | |
74 if (!read_ok) | |
75 return false; | |
76 | |
77 if (!format_context->iformat) | |
78 return false; | |
79 | |
80 if (avformat_find_stream_info(format_context, NULL) < 0) | |
81 return false; | |
82 | |
83 if (format_context->duration != AV_NOPTS_VALUE) | |
84 duration_ = static_cast<double>(format_context->duration) / AV_TIME_BASE; | |
85 | |
86 ExtractDictionary(format_context->metadata); | |
87 | |
88 for (unsigned int i = 0; i < format_context->nb_streams; ++i) { | |
89 AVStream* stream = format_context->streams[i]; | |
90 if (!stream) | |
91 continue; | |
92 | |
93 // Ignore attached pictures for metadata extraction. | |
94 if ((stream->disposition & AV_DISPOSITION_ATTACHED_PIC) != 0) | |
95 continue; | |
96 | |
97 // Extract dictionary from streams also. Needed for containers that attach | |
98 // metadata to contained streams instead the container itself, like OGG. | |
99 ExtractDictionary(stream->metadata); | |
100 | |
101 if (!stream->codec) | |
102 continue; | |
103 | |
104 // Extract dimensions of largest stream that's not an attached picture. | |
105 if (stream->codec->width > 0 && stream->codec->width > width_ && | |
106 stream->codec->height > 0 && stream->codec->height > height_) { | |
107 width_ = stream->codec->width; | |
108 height_ = stream->codec->height; | |
109 } | |
110 } | |
111 | |
112 extracted_ = true; | |
113 return true; | |
114 } | |
115 | |
116 double AudioVideoMetadataExtractor::duration() const { | |
117 DCHECK(extracted_); | |
118 return duration_; | |
119 } | |
120 | |
121 int AudioVideoMetadataExtractor::width() const { | |
122 DCHECK(extracted_); | |
123 return width_; | |
124 } | |
125 | |
126 int AudioVideoMetadataExtractor::height() const { | |
127 DCHECK(extracted_); | |
128 return height_; | |
129 } | |
130 | |
131 const std::string& AudioVideoMetadataExtractor::album() const { | |
132 DCHECK(extracted_); | |
133 return album_; | |
134 } | |
135 | |
136 const std::string& AudioVideoMetadataExtractor::artist() const { | |
137 DCHECK(extracted_); | |
138 return artist_; | |
139 } | |
140 | |
141 const std::string& AudioVideoMetadataExtractor::comment() const { | |
142 DCHECK(extracted_); | |
143 return comment_; | |
144 } | |
145 | |
146 const std::string& AudioVideoMetadataExtractor::copyright() const { | |
147 DCHECK(extracted_); | |
148 return copyright_; | |
149 } | |
150 | |
151 const std::string& AudioVideoMetadataExtractor::date() const { | |
152 DCHECK(extracted_); | |
153 return date_; | |
154 } | |
155 | |
156 int AudioVideoMetadataExtractor::disc() const { | |
157 DCHECK(extracted_); | |
158 return disc_; | |
159 } | |
160 | |
161 const std::string& AudioVideoMetadataExtractor::encoder() const { | |
162 DCHECK(extracted_); | |
163 return encoder_; | |
164 } | |
165 | |
166 const std::string& AudioVideoMetadataExtractor::encoded_by() const { | |
167 DCHECK(extracted_); | |
168 return encoded_by_; | |
169 } | |
170 | |
171 const std::string& AudioVideoMetadataExtractor::genre() const { | |
172 DCHECK(extracted_); | |
173 return genre_; | |
174 } | |
175 | |
176 const std::string& AudioVideoMetadataExtractor::language() const { | |
177 DCHECK(extracted_); | |
178 return language_; | |
179 } | |
180 | |
181 const std::string& AudioVideoMetadataExtractor::title() const { | |
182 DCHECK(extracted_); | |
183 return title_; | |
184 } | |
185 | |
186 int AudioVideoMetadataExtractor::track() const { | |
187 DCHECK(extracted_); | |
188 return track_; | |
189 } | |
190 | |
191 void AudioVideoMetadataExtractor::ExtractDictionary(AVDictionary* metadata) { | |
192 if (!metadata) | |
193 return; | |
194 | |
195 AVDictionaryEntry* tag = NULL; | |
196 while ((tag = av_dict_get(metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) { | |
197 std::string key = tag->key; | |
198 StringToLowerASCII(&key); | |
199 | |
200 // Try extracting all the defined keys. | |
201 if (ExtractString(tag, key, "album", &album_)) continue; | |
202 if (ExtractString(tag, key, "artist", &artist_)) continue; | |
203 if (ExtractString(tag, key, "comment", &comment_)) continue; | |
204 if (ExtractString(tag, key, "copyright", ©right_)) continue; | |
205 if (ExtractString(tag, key, "date", &date_)) continue; | |
206 if (ExtractInt(tag, key, "disc", &disc_)) continue; | |
207 if (ExtractString(tag, key, "encoder", &encoder_)) continue; | |
208 if (ExtractString(tag, key, "encoded_by", &encoded_by_)) continue; | |
209 if (ExtractString(tag, key, "genre", &genre_)) continue; | |
210 if (ExtractString(tag, key, "language", &language_)) continue; | |
211 if (ExtractString(tag, key, "title", &title_)) continue; | |
212 if (ExtractInt(tag, key, "track", &track_)) continue; | |
213 } | |
214 } | |
215 | |
216 } // namespace media | |
OLD | NEW |