blob: ff0e52e3d2e878358f220ec0264d24e80a8e20ec [file] [log] [blame]
James Dong1d7491b2010-01-19 17:45:38 -08001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "MediaProfiles"
21
22#include <stdlib.h>
23#include <utils/Log.h>
24#include <utils/Vector.h>
25#include <cutils/properties.h>
Elliott Hughes242b4002015-07-10 11:09:23 -070026#include <expat.h>
James Dong1d7491b2010-01-19 17:45:38 -080027#include <media/MediaProfiles.h>
James Dongf1d5aa12012-02-06 23:46:37 -080028#include <media/stagefright/foundation/ADebug.h>
James Dong6c6b4d02012-03-12 14:37:53 -070029#include <OMX_Video.h>
James Dong1d7491b2010-01-19 17:45:38 -080030
31namespace android {
32
33Mutex MediaProfiles::sLock;
34bool MediaProfiles::sIsInitialized = false;
35MediaProfiles *MediaProfiles::sInstance = NULL;
36
37const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
38 {"h263", VIDEO_ENCODER_H263},
39 {"h264", VIDEO_ENCODER_H264},
Wonsik Kim9aa87d42015-12-07 13:52:02 +090040 {"m4v", VIDEO_ENCODER_MPEG_4_SP},
41 {"hevc", VIDEO_ENCODER_HEVC}
James Dong1d7491b2010-01-19 17:45:38 -080042};
43
44const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
Dave Burkeaeb8fd42012-04-19 00:14:27 -070045 {"amrnb", AUDIO_ENCODER_AMR_NB},
46 {"amrwb", AUDIO_ENCODER_AMR_WB},
47 {"aac", AUDIO_ENCODER_AAC},
Dave Burkef60c6602012-04-28 21:58:22 -070048 {"heaac", AUDIO_ENCODER_HE_AAC},
49 {"aaceld", AUDIO_ENCODER_AAC_ELD}
James Dong1d7491b2010-01-19 17:45:38 -080050};
51
52const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
53 {"3gp", OUTPUT_FORMAT_THREE_GPP},
54 {"mp4", OUTPUT_FORMAT_MPEG_4}
55};
56
57const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
58 {"wmv", VIDEO_DECODER_WMV}
59};
60
61const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
62 {"wma", AUDIO_DECODER_WMA}
63};
64
65const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
Nipun Kwatrac0a84782010-09-06 15:59:02 -070066 {"low", CAMCORDER_QUALITY_LOW},
James Dong1d7491b2010-01-19 17:45:38 -080067 {"high", CAMCORDER_QUALITY_HIGH},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070068 {"qcif", CAMCORDER_QUALITY_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070069 {"cif", CAMCORDER_QUALITY_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070070 {"480p", CAMCORDER_QUALITY_480P},
71 {"720p", CAMCORDER_QUALITY_720P},
72 {"1080p", CAMCORDER_QUALITY_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070073 {"2160p", CAMCORDER_QUALITY_2160P},
James Dong669012d2011-09-19 16:27:31 -070074 {"qvga", CAMCORDER_QUALITY_QVGA},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070075
76 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
77 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
78 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070079 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070080 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
81 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -070082 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070083 {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
James Dong669012d2011-09-19 16:27:31 -070084 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
Zhijun Hee0790972014-07-23 15:17:26 -070085
86 {"highspeedlow", CAMCORDER_QUALITY_HIGH_SPEED_LOW},
87 {"highspeedhigh", CAMCORDER_QUALITY_HIGH_SPEED_HIGH},
88 {"highspeed480p", CAMCORDER_QUALITY_HIGH_SPEED_480P},
89 {"highspeed720p", CAMCORDER_QUALITY_HIGH_SPEED_720P},
90 {"highspeed1080p", CAMCORDER_QUALITY_HIGH_SPEED_1080P},
Zhijun He9520aa62014-09-09 16:18:31 -070091 {"highspeed2160p", CAMCORDER_QUALITY_HIGH_SPEED_2160P},
James Dong1d7491b2010-01-19 17:45:38 -080092};
93
Glenn Kasten80520382014-01-31 16:49:31 -080094#if LOG_NDEBUG
95#define UNUSED __unused
96#else
97#define UNUSED
98#endif
99
James Dong1d7491b2010-01-19 17:45:38 -0800100/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800101MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800102{
Steve Block3856b092011-10-20 11:56:00 +0100103 ALOGV("video codec:");
104 ALOGV("codec = %d", codec.mCodec);
105 ALOGV("bit rate: %d", codec.mBitRate);
106 ALOGV("frame width: %d", codec.mFrameWidth);
107 ALOGV("frame height: %d", codec.mFrameHeight);
108 ALOGV("frame rate: %d", codec.mFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800109}
110
111/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800112MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800113{
Steve Block3856b092011-10-20 11:56:00 +0100114 ALOGV("audio codec:");
115 ALOGV("codec = %d", codec.mCodec);
116 ALOGV("bit rate: %d", codec.mBitRate);
117 ALOGV("sample rate: %d", codec.mSampleRate);
118 ALOGV("number of channels: %d", codec.mChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800119}
120
121/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800122MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800123{
Steve Block3856b092011-10-20 11:56:00 +0100124 ALOGV("video encoder cap:");
125 ALOGV("codec = %d", cap.mCodec);
126 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
127 ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
128 ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
129 ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800130}
131
132/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800133MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800134{
Steve Block3856b092011-10-20 11:56:00 +0100135 ALOGV("audio encoder cap:");
136 ALOGV("codec = %d", cap.mCodec);
137 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
138 ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
139 ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800140}
141
142/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800143MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800144{
Steve Block3856b092011-10-20 11:56:00 +0100145 ALOGV("video decoder cap:");
146 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800147}
148
149/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800150MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800151{
Steve Block3856b092011-10-20 11:56:00 +0100152 ALOGV("audio codec cap:");
153 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800154}
155
156/*static*/ int
Glenn Kastenb187de12014-12-30 08:18:15 -0800157MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings,
158 const char *name)
James Dong1d7491b2010-01-19 17:45:38 -0800159{
160 int tag = -1;
161 for (size_t i = 0; i < nMappings; ++i) {
162 if (!strcmp(map[i].name, name)) {
163 tag = map[i].tag;
164 break;
165 }
166 }
167 return tag;
168}
169
170/*static*/ MediaProfiles::VideoCodec*
171MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
172{
173 CHECK(!strcmp("codec", atts[0]) &&
174 !strcmp("bitRate", atts[2]) &&
175 !strcmp("width", atts[4]) &&
176 !strcmp("height", atts[6]) &&
177 !strcmp("frameRate", atts[8]));
178
179 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
180 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
181 CHECK(codec != -1);
182
183 MediaProfiles::VideoCodec *videoCodec =
184 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
185 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
186 logVideoCodec(*videoCodec);
187
188 size_t nCamcorderProfiles;
189 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
190 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
191 return videoCodec;
192}
193
194/*static*/ MediaProfiles::AudioCodec*
195MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
196{
197 CHECK(!strcmp("codec", atts[0]) &&
198 !strcmp("bitRate", atts[2]) &&
199 !strcmp("sampleRate", atts[4]) &&
200 !strcmp("channels", atts[6]));
201 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
202 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
203 CHECK(codec != -1);
204
205 MediaProfiles::AudioCodec *audioCodec =
206 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
207 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
208 logAudioCodec(*audioCodec);
209
210 size_t nCamcorderProfiles;
211 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
212 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
213 return audioCodec;
214}
215/*static*/ MediaProfiles::AudioDecoderCap*
216MediaProfiles::createAudioDecoderCap(const char **atts)
217{
218 CHECK(!strcmp("name", atts[0]) &&
219 !strcmp("enabled", atts[2]));
220
221 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
222 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
223 CHECK(codec != -1);
224
225 MediaProfiles::AudioDecoderCap *cap =
226 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
227 logAudioDecoderCap(*cap);
228 return cap;
229}
230
231/*static*/ MediaProfiles::VideoDecoderCap*
232MediaProfiles::createVideoDecoderCap(const char **atts)
233{
234 CHECK(!strcmp("name", atts[0]) &&
235 !strcmp("enabled", atts[2]));
236
237 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
238 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
239 CHECK(codec != -1);
240
241 MediaProfiles::VideoDecoderCap *cap =
242 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
243 logVideoDecoderCap(*cap);
244 return cap;
245}
246
247/*static*/ MediaProfiles::VideoEncoderCap*
248MediaProfiles::createVideoEncoderCap(const char **atts)
249{
250 CHECK(!strcmp("name", atts[0]) &&
251 !strcmp("enabled", atts[2]) &&
252 !strcmp("minBitRate", atts[4]) &&
253 !strcmp("maxBitRate", atts[6]) &&
254 !strcmp("minFrameWidth", atts[8]) &&
255 !strcmp("maxFrameWidth", atts[10]) &&
256 !strcmp("minFrameHeight", atts[12]) &&
257 !strcmp("maxFrameHeight", atts[14]) &&
258 !strcmp("minFrameRate", atts[16]) &&
259 !strcmp("maxFrameRate", atts[18]));
260
261 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
262 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
263 CHECK(codec != -1);
264
265 MediaProfiles::VideoEncoderCap *cap =
266 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
267 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
268 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
269 logVideoEncoderCap(*cap);
270 return cap;
271}
272
273/*static*/ MediaProfiles::AudioEncoderCap*
274MediaProfiles::createAudioEncoderCap(const char **atts)
275{
276 CHECK(!strcmp("name", atts[0]) &&
277 !strcmp("enabled", atts[2]) &&
278 !strcmp("minBitRate", atts[4]) &&
279 !strcmp("maxBitRate", atts[6]) &&
280 !strcmp("minSampleRate", atts[8]) &&
281 !strcmp("maxSampleRate", atts[10]) &&
282 !strcmp("minChannels", atts[12]) &&
283 !strcmp("maxChannels", atts[14]));
284
285 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
286 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
287 CHECK(codec != -1);
288
289 MediaProfiles::AudioEncoderCap *cap =
Glenn Kastenb187de12014-12-30 08:18:15 -0800290 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
291 atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
James Dong1d7491b2010-01-19 17:45:38 -0800292 logAudioEncoderCap(*cap);
293 return cap;
294}
295
296/*static*/ output_format
297MediaProfiles::createEncoderOutputFileFormat(const char **atts)
298{
299 CHECK(!strcmp("name", atts[0]));
300
301 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
302 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
303 CHECK(format != -1);
304
305 return static_cast<output_format>(format);
306}
307
James Dong2a7e0a12011-02-28 21:07:39 -0800308static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
309 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
310 if (cameraId == cameraIds[i]) {
311 return true;
312 }
313 }
314 return false;
315}
316
James Dong1d7491b2010-01-19 17:45:38 -0800317/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800318MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800319{
320 CHECK(!strcmp("quality", atts[0]) &&
321 !strcmp("fileFormat", atts[2]) &&
322 !strcmp("duration", atts[4]));
323
Glenn Kastenb187de12014-12-30 08:18:15 -0800324 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
325 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800326 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
327 CHECK(quality != -1);
328
329 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
330 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
331 CHECK(fileFormat != -1);
332
333 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800334 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800335 if (!isCameraIdFound(cameraId, cameraIds)) {
336 cameraIds.add(cameraId);
337 }
James Dong1d7491b2010-01-19 17:45:38 -0800338 profile->mFileFormat = static_cast<output_format>(fileFormat);
339 profile->mQuality = static_cast<camcorder_quality>(quality);
340 profile->mDuration = atoi(atts[5]);
341 return profile;
342}
343
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800344MediaProfiles::ImageEncodingQualityLevels*
345MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
346{
347 int n = mImageEncodingQualityLevels.size();
348 for (int i = 0; i < n; i++) {
349 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
350 if (levels->mCameraId == cameraId) {
351 return levels;
352 }
353 }
354 return NULL;
355}
356
357void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800358{
359 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800360 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800361 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800362 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
363
364 if (levels == NULL) {
365 levels = new ImageEncodingQualityLevels();
366 levels->mCameraId = cameraId;
367 mImageEncodingQualityLevels.add(levels);
368 }
369
370 levels->mLevels.add(quality);
371}
372
373/*static*/ int
374MediaProfiles::getCameraId(const char** atts)
375{
376 if (!atts[0]) return 0; // default cameraId = 0
377 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800378 return atoi(atts[1]);
379}
380
James Dong0f056292011-05-09 18:49:31 -0700381void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
382{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700383 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700384 if (atts[2]) {
385 CHECK(!strcmp("startOffsetMs", atts[2]));
386 offsetTimeMs = atoi(atts[3]);
387 }
388
Steve Block3856b092011-10-20 11:56:00 +0100389 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700390 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
391}
Hong Tengcabd5f82011-07-06 18:33:09 -0700392
James Dong1d7491b2010-01-19 17:45:38 -0800393/*static*/ void
394MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
395{
396 MediaProfiles *profiles = (MediaProfiles *) userData;
397 if (strcmp("Video", name) == 0) {
398 createVideoCodec(atts, profiles);
399 } else if (strcmp("Audio", name) == 0) {
400 createAudioCodec(atts, profiles);
401 } else if (strcmp("VideoEncoderCap", name) == 0 &&
402 strcmp("true", atts[3]) == 0) {
403 profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
404 } else if (strcmp("AudioEncoderCap", name) == 0 &&
405 strcmp("true", atts[3]) == 0) {
406 profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
407 } else if (strcmp("VideoDecoderCap", name) == 0 &&
408 strcmp("true", atts[3]) == 0) {
409 profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
410 } else if (strcmp("AudioDecoderCap", name) == 0 &&
411 strcmp("true", atts[3]) == 0) {
412 profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
413 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
414 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800415 } else if (strcmp("CamcorderProfiles", name) == 0) {
416 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700417 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800418 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800419 profiles->mCamcorderProfiles.add(
James Dong2a7e0a12011-02-28 21:07:39 -0800420 createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
James Dongf5a83852010-02-23 17:21:44 -0800421 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800422 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800423 }
424}
425
James Dong2a7e0a12011-02-28 21:07:39 -0800426static bool isCamcorderProfile(camcorder_quality quality) {
427 return quality >= CAMCORDER_QUALITY_LIST_START &&
428 quality <= CAMCORDER_QUALITY_LIST_END;
429}
430
431static bool isTimelapseProfile(camcorder_quality quality) {
432 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
433 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
434}
435
Zhijun Hee0790972014-07-23 15:17:26 -0700436static bool isHighSpeedProfile(camcorder_quality quality) {
437 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
438 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
439}
440
James Dong2a7e0a12011-02-28 21:07:39 -0800441void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700442 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800443 CHECK(cameraIds.size() > 0);
444 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
445 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
446 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
447 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
448 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
449 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
450 if ((j & 1) == 0) { // low resolution
451 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
452 } else { // high resolution
453 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
454 }
455 }
456 }
457}
458
459int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
460 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
461 if (mCameraIds[i] == cameraId) {
462 return i;
463 }
464 }
465 return -1;
466}
467
468void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
469 if (sIsInitialized) {
470 return;
471 }
472
473 initRequiredProfileRefs(mCameraIds);
474
475 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
476 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
477 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
478
479 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
480 int cameraId = mCamcorderProfiles[i]->mCameraId;
481 int index = -1;
482 int refIndex = getRequiredProfileRefIndex(cameraId);
483 CHECK(refIndex != -1);
484 RequiredProfileRefInfo *info;
485 camcorder_quality refQuality;
James Dong2a7e0a12011-02-28 21:07:39 -0800486
Zhijun Hee0790972014-07-23 15:17:26 -0700487 // Check high and low from either camcorder profile, timelapse profile
488 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800489 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200490 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800491 if (isTimelapseProfile(quality)) {
492 // Check timelapse profile instead.
493 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200494 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700495 } else if (isHighSpeedProfile(quality)) {
496 // Skip the check for high speed profile.
497 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800498 } else {
499 // Must be camcorder profile.
500 CHECK(isCamcorderProfile(quality));
501 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200502 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800503 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
504 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
505 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
506 continue;
507 }
508 switch (j) {
509 case 0:
510 refQuality = CAMCORDER_QUALITY_LOW;
511 break;
512 case 1:
513 refQuality = CAMCORDER_QUALITY_HIGH;
514 break;
515 case 2:
516 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
517 break;
518 case 3:
519 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
520 break;
521 default:
522 CHECK(!"Should never reach here");
523 }
524
525 if (!info->mHasRefProfile) {
526 index = getCamcorderProfileIndex(cameraId, refQuality);
527 }
528 if (index == -1) {
529 // New high or low quality profile is found.
530 // Update its reference.
531 info->mHasRefProfile = true;
532 info->mRefProfileIndex = i;
533 info->mResolutionProduct = product;
534 }
535 }
536 }
537
538 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
539 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
540 int refIndex = getRequiredProfileRefIndex(cameraId);
541 CHECK(refIndex != -1);
542 RequiredProfileRefInfo *info =
543 &mRequiredProfileRefs[refIndex].mRefs[j];
544
545 if (info->mHasRefProfile) {
546
547 CamcorderProfile *profile =
548 new CamcorderProfile(
549 *mCamcorderProfiles[info->mRefProfileIndex]);
550
551 // Overwrite the quality
552 switch (j % kNumRequiredProfiles) {
553 case 0:
554 profile->mQuality = CAMCORDER_QUALITY_LOW;
555 break;
556 case 1:
557 profile->mQuality = CAMCORDER_QUALITY_HIGH;
558 break;
559 case 2:
560 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
561 break;
562 case 3:
563 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
564 break;
565 default:
566 CHECK(!"Should never come here");
567 }
568
569 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
570 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700571 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800572 profile->mQuality, cameraId);
573 CHECK(index == refIndex);
574 continue;
575 }
576
577 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700578 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800579 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
580 profile->mQuality, cameraId);
581
582 mCamcorderProfiles.add(profile);
583 }
584 }
585 }
586}
587
James Dong1d7491b2010-01-19 17:45:38 -0800588/*static*/ MediaProfiles*
589MediaProfiles::getInstance()
590{
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800592 Mutex::Autolock lock(sLock);
593 if (!sIsInitialized) {
594 char value[PROPERTY_VALUE_MAX];
595 if (property_get("media.settings.xml", value, NULL) <= 0) {
596 const char *defaultXmlFile = "/etc/media_profiles.xml";
597 FILE *fp = fopen(defaultXmlFile, "r");
598 if (fp == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000599 ALOGW("could not find media config xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800600 sInstance = createDefaultInstance();
601 } else {
602 fclose(fp); // close the file first.
603 sInstance = createInstanceFromXmlFile(defaultXmlFile);
604 }
605 } else {
606 sInstance = createInstanceFromXmlFile(value);
607 }
James Dong2a7e0a12011-02-28 21:07:39 -0800608 CHECK(sInstance != NULL);
609 sInstance->checkAndAddRequiredProfilesIfNecessary();
610 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800611 }
612
613 return sInstance;
614}
615
616/*static*/ MediaProfiles::VideoEncoderCap*
617MediaProfiles::createDefaultH263VideoEncoderCap()
618{
619 return new MediaProfiles::VideoEncoderCap(
620 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
621}
622
623/*static*/ MediaProfiles::VideoEncoderCap*
624MediaProfiles::createDefaultM4vVideoEncoderCap()
625{
626 return new MediaProfiles::VideoEncoderCap(
627 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
628}
629
630
631/*static*/ void
632MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
633{
634 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
635 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
636}
637
638/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700639MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700640{
641 MediaProfiles::VideoCodec *videoCodec =
642 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
643
644 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
645 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
646 profile->mCameraId = 0;
647 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700648 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700649 profile->mDuration = 60;
650 profile->mVideoCodec = videoCodec;
651 profile->mAudioCodec = audioCodec;
652 return profile;
653}
654
655/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700656MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800657{
658 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700659 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800660
661 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800662 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
663 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800664 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700665 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800666 profile->mDuration = 60;
667 profile->mVideoCodec = videoCodec;
668 profile->mAudioCodec = audioCodec;
669 return profile;
670}
671
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700672/*static*/ void
673MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
674 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
675 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800676 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
677 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
678 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
679 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700680}
681
682/*static*/ void
683MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
684 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
685 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800686 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
687 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
688 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
689 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700690}
691
James Dong1d7491b2010-01-19 17:45:38 -0800692/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700693MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800694{
695 MediaProfiles::VideoCodec *videoCodec =
696 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
697
698 MediaProfiles::AudioCodec *audioCodec =
699 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
700
701 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800702 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800703 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700704 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800705 profile->mDuration = 30;
706 profile->mVideoCodec = videoCodec;
707 profile->mAudioCodec = audioCodec;
708 return profile;
709}
710
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700711/*static*/ MediaProfiles::CamcorderProfile*
712MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
713{
714 MediaProfiles::VideoCodec *videoCodec =
715 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
716
717 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
718 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
719 profile->mCameraId = 0;
720 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
721 profile->mQuality = quality;
722 profile->mDuration = 60;
723 profile->mVideoCodec = videoCodec;
724 profile->mAudioCodec = audioCodec;
725 return profile;
726}
727
728/*static*/ void
729MediaProfiles::createDefaultCamcorderLowProfiles(
730 MediaProfiles::CamcorderProfile **lowProfile,
731 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
732 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
733 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
734}
735
736/*static*/ void
737MediaProfiles::createDefaultCamcorderHighProfiles(
738 MediaProfiles::CamcorderProfile **highProfile,
739 MediaProfiles::CamcorderProfile **highSpecificProfile) {
740 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
741 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
742}
743
James Dong1d7491b2010-01-19 17:45:38 -0800744/*static*/ void
745MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
746{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700747 // low camcorder profiles.
748 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
749 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
750 profiles->mCamcorderProfiles.add(lowProfile);
751 profiles->mCamcorderProfiles.add(lowSpecificProfile);
752
753 // high camcorder profiles.
754 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
755 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
756 profiles->mCamcorderProfiles.add(highProfile);
757 profiles->mCamcorderProfiles.add(highSpecificProfile);
758
759 // low camcorder time lapse profiles.
760 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
761 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
762 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
763 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
764
765 // high camcorder time lapse profiles.
766 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
Glenn Kastenb187de12014-12-30 08:18:15 -0800767 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
768 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700769 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
770 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700771
772 // For emulator and other legacy devices which does not have a
773 // media_profiles.xml file, We assume that the default camera id
774 // is 0 and that is the only camera available.
775 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800776}
777
778/*static*/ void
779MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
780{
781 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
782}
783
784/*static*/ void
785MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
786{
787 MediaProfiles::VideoDecoderCap *cap =
788 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
789
790 profiles->mVideoDecoders.add(cap);
791}
792
793/*static*/ void
794MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
795{
796 MediaProfiles::AudioDecoderCap *cap =
797 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
798
799 profiles->mAudioDecoders.add(cap);
800}
801
802/*static*/ void
803MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
804{
805 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
806 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
807}
808
809/*static*/ MediaProfiles::AudioEncoderCap*
810MediaProfiles::createDefaultAmrNBEncoderCap()
811{
812 return new MediaProfiles::AudioEncoderCap(
813 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
814}
815
James Dongf5a83852010-02-23 17:21:44 -0800816/*static*/ void
817MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
818{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800819 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
820 levels->mCameraId = 0;
821 levels->mLevels.add(70);
822 levels->mLevels.add(80);
823 levels->mLevels.add(90);
824 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800825}
826
James Dong1d7491b2010-01-19 17:45:38 -0800827/*static*/ MediaProfiles*
828MediaProfiles::createDefaultInstance()
829{
830 MediaProfiles *profiles = new MediaProfiles;
831 createDefaultCamcorderProfiles(profiles);
832 createDefaultVideoEncoders(profiles);
833 createDefaultAudioEncoders(profiles);
834 createDefaultVideoDecoders(profiles);
835 createDefaultAudioDecoders(profiles);
836 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800837 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800838 return profiles;
839}
840
841/*static*/ MediaProfiles*
842MediaProfiles::createInstanceFromXmlFile(const char *xml)
843{
844 FILE *fp = NULL;
845 CHECK((fp = fopen(xml, "r")));
846
847 XML_Parser parser = ::XML_ParserCreate(NULL);
848 CHECK(parser != NULL);
849
850 MediaProfiles *profiles = new MediaProfiles();
851 ::XML_SetUserData(parser, profiles);
852 ::XML_SetElementHandler(parser, startElementHandler, NULL);
853
854 /*
855 FIXME:
856 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
857
858 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000859 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800860 return UNKNOWN_ERROR;
861 }
862
863 */
864
865 const int BUFF_SIZE = 512;
866 for (;;) {
867 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
868 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000869 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800870 delete profiles;
871 profiles = NULL;
872 goto exit;
873 }
874
875 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
876 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000877 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800878 delete profiles;
879 profiles = NULL;
880 goto exit;
881 }
882
883 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
884
885 if (bytes_read == 0) break; // done parsing the xml file
886 }
887
888exit:
889 ::XML_ParserFree(parser);
890 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -0800891 return profiles;
892}
893
894Vector<output_format> MediaProfiles::getOutputFileFormats() const
895{
896 return mEncoderOutputFileFormats; // copy out
897}
898
899Vector<video_encoder> MediaProfiles::getVideoEncoders() const
900{
901 Vector<video_encoder> encoders;
902 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
903 encoders.add(mVideoEncoders[i]->mCodec);
904 }
905 return encoders; // copy out
906}
907
908int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
909{
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800911 int index = -1;
912 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
913 if (mVideoEncoders[i]->mCodec == codec) {
914 index = i;
915 break;
916 }
917 }
918 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000919 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800920 return -1;
921 }
922
923 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
924 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
925 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
926 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
927 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
928 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
929 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
930 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
931
Steve Block29357bc2012-01-06 19:20:56 +0000932 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800933 return -1;
934}
Hong Tengcabd5f82011-07-06 18:33:09 -0700935
James Dong1d7491b2010-01-19 17:45:38 -0800936Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
937{
938 Vector<audio_encoder> encoders;
939 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
940 encoders.add(mAudioEncoders[i]->mCodec);
941 }
942 return encoders; // copy out
943}
944
945int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
946{
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800948 int index = -1;
949 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
950 if (mAudioEncoders[i]->mCodec == codec) {
951 index = i;
952 break;
953 }
954 }
955 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000956 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800957 return -1;
958 }
959
960 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
961 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
962 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
963 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
964 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
965 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
966
Steve Block29357bc2012-01-06 19:20:56 +0000967 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800968 return -1;
969}
970
971Vector<video_decoder> MediaProfiles::getVideoDecoders() const
972{
973 Vector<video_decoder> decoders;
974 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
975 decoders.add(mVideoDecoders[i]->mCodec);
976 }
977 return decoders; // copy out
978}
979
980Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
981{
982 Vector<audio_decoder> decoders;
983 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
984 decoders.add(mAudioDecoders[i]->mCodec);
985 }
986 return decoders; // copy out
987}
988
Nipun Kwatra8bb56032010-09-09 16:25:08 -0700989int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -0800990{
James Dong1d7491b2010-01-19 17:45:38 -0800991 int index = -1;
992 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800993 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
994 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -0800995 index = i;
996 break;
997 }
998 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -0700999 return index;
1000}
1001
1002int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1003 int cameraId,
1004 camcorder_quality quality) const
1005{
Steve Block3856b092011-10-20 11:56:00 +01001006 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001007 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001008
1009 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001010 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001011 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001012 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001013 return -1;
1014 }
1015
James Dongf5a83852010-02-23 17:21:44 -08001016 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001017 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1018 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1019 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1020 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1021 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1022 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1023 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1024 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1025 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1026 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1027
Steve Block29357bc2012-01-06 19:20:56 +00001028 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001029 return -1;
1030}
1031
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001032bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1033{
1034 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1035}
1036
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001037Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001038{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001039 Vector<int> result;
1040 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1041 if (levels != NULL) {
1042 result = levels->mLevels; // copy out
1043 }
1044 return result;
James Dongf5a83852010-02-23 17:21:44 -08001045}
1046
James Dong0f056292011-05-09 18:49:31 -07001047int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1048 int offsetTimeMs = -1;
1049 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1050 if (index >= 0) {
1051 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1052 }
Steve Block3856b092011-10-20 11:56:00 +01001053 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001054 return offsetTimeMs;
1055}
1056
James Dong1d7491b2010-01-19 17:45:38 -08001057MediaProfiles::~MediaProfiles()
1058{
1059 CHECK("destructor should never be called" == 0);
1060#if 0
1061 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1062 delete mAudioEncoders[i];
1063 }
1064 mAudioEncoders.clear();
1065
1066 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1067 delete mVideoEncoders[i];
1068 }
1069 mVideoEncoders.clear();
1070
1071 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1072 delete mVideoDecoders[i];
1073 }
1074 mVideoDecoders.clear();
1075
1076 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1077 delete mAudioDecoders[i];
1078 }
1079 mAudioDecoders.clear();
1080
1081 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1082 delete mCamcorderProfiles[i];
1083 }
1084 mCamcorderProfiles.clear();
1085#endif
1086}
1087} // namespace android