blob: d2e181bfa6f2ac66c9318b0974c67a27c49c8f2e [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 Hughese8057dd2012-09-09 14:58:14 -070026#include <libexpat/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},
40 {"m4v", VIDEO_ENCODER_MPEG_4_SP}
41};
42
43const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
Dave Burkeaeb8fd42012-04-19 00:14:27 -070044 {"amrnb", AUDIO_ENCODER_AMR_NB},
45 {"amrwb", AUDIO_ENCODER_AMR_WB},
46 {"aac", AUDIO_ENCODER_AAC},
Dave Burkef60c6602012-04-28 21:58:22 -070047 {"heaac", AUDIO_ENCODER_HE_AAC},
48 {"aaceld", AUDIO_ENCODER_AAC_ELD}
James Dong1d7491b2010-01-19 17:45:38 -080049};
50
51const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
52 {"3gp", OUTPUT_FORMAT_THREE_GPP},
53 {"mp4", OUTPUT_FORMAT_MPEG_4}
54};
55
56const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
57 {"wmv", VIDEO_DECODER_WMV}
58};
59
60const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
61 {"wma", AUDIO_DECODER_WMA}
62};
63
64const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
Nipun Kwatrac0a84782010-09-06 15:59:02 -070065 {"low", CAMCORDER_QUALITY_LOW},
James Dong1d7491b2010-01-19 17:45:38 -080066 {"high", CAMCORDER_QUALITY_HIGH},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070067 {"qcif", CAMCORDER_QUALITY_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070068 {"cif", CAMCORDER_QUALITY_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070069 {"480p", CAMCORDER_QUALITY_480P},
70 {"720p", CAMCORDER_QUALITY_720P},
71 {"1080p", CAMCORDER_QUALITY_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070072 {"2160p", CAMCORDER_QUALITY_2160P},
James Dong669012d2011-09-19 16:27:31 -070073 {"qvga", CAMCORDER_QUALITY_QVGA},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070074
75 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
76 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
77 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070078 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070079 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
80 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -070081 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070082 {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
James Dong669012d2011-09-19 16:27:31 -070083 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
Zhijun Hee0790972014-07-23 15:17:26 -070084
85 {"highspeedlow", CAMCORDER_QUALITY_HIGH_SPEED_LOW},
86 {"highspeedhigh", CAMCORDER_QUALITY_HIGH_SPEED_HIGH},
87 {"highspeed480p", CAMCORDER_QUALITY_HIGH_SPEED_480P},
88 {"highspeed720p", CAMCORDER_QUALITY_HIGH_SPEED_720P},
89 {"highspeed1080p", CAMCORDER_QUALITY_HIGH_SPEED_1080P},
James Dong1d7491b2010-01-19 17:45:38 -080090};
91
Glenn Kasten80520382014-01-31 16:49:31 -080092#if LOG_NDEBUG
93#define UNUSED __unused
94#else
95#define UNUSED
96#endif
97
James Dong1d7491b2010-01-19 17:45:38 -080098/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -080099MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800100{
Steve Block3856b092011-10-20 11:56:00 +0100101 ALOGV("video codec:");
102 ALOGV("codec = %d", codec.mCodec);
103 ALOGV("bit rate: %d", codec.mBitRate);
104 ALOGV("frame width: %d", codec.mFrameWidth);
105 ALOGV("frame height: %d", codec.mFrameHeight);
106 ALOGV("frame rate: %d", codec.mFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800107}
108
109/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800110MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800111{
Steve Block3856b092011-10-20 11:56:00 +0100112 ALOGV("audio codec:");
113 ALOGV("codec = %d", codec.mCodec);
114 ALOGV("bit rate: %d", codec.mBitRate);
115 ALOGV("sample rate: %d", codec.mSampleRate);
116 ALOGV("number of channels: %d", codec.mChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800117}
118
119/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800120MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800121{
Steve Block3856b092011-10-20 11:56:00 +0100122 ALOGV("video encoder cap:");
123 ALOGV("codec = %d", cap.mCodec);
124 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
125 ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
126 ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
127 ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800128}
129
130/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800131MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800132{
Steve Block3856b092011-10-20 11:56:00 +0100133 ALOGV("audio encoder cap:");
134 ALOGV("codec = %d", cap.mCodec);
135 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
136 ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
137 ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800138}
139
140/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800141MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800142{
Steve Block3856b092011-10-20 11:56:00 +0100143 ALOGV("video decoder cap:");
144 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800145}
146
147/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800148MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800149{
Steve Block3856b092011-10-20 11:56:00 +0100150 ALOGV("audio codec cap:");
151 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800152}
153
Hong Tengcabd5f82011-07-06 18:33:09 -0700154/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800155MediaProfiles::logVideoEditorCap(const MediaProfiles::VideoEditorCap& cap UNUSED)
Hong Tengcabd5f82011-07-06 18:33:09 -0700156{
Steve Block3856b092011-10-20 11:56:00 +0100157 ALOGV("videoeditor cap:");
158 ALOGV("mMaxInputFrameWidth = %d", cap.mMaxInputFrameWidth);
159 ALOGV("mMaxInputFrameHeight = %d", cap.mMaxInputFrameHeight);
160 ALOGV("mMaxOutputFrameWidth = %d", cap.mMaxOutputFrameWidth);
161 ALOGV("mMaxOutputFrameHeight = %d", cap.mMaxOutputFrameHeight);
Hong Tengcabd5f82011-07-06 18:33:09 -0700162}
163
James Dong1d7491b2010-01-19 17:45:38 -0800164/*static*/ int
165MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings, const char *name)
166{
167 int tag = -1;
168 for (size_t i = 0; i < nMappings; ++i) {
169 if (!strcmp(map[i].name, name)) {
170 tag = map[i].tag;
171 break;
172 }
173 }
174 return tag;
175}
176
177/*static*/ MediaProfiles::VideoCodec*
178MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
179{
180 CHECK(!strcmp("codec", atts[0]) &&
181 !strcmp("bitRate", atts[2]) &&
182 !strcmp("width", atts[4]) &&
183 !strcmp("height", atts[6]) &&
184 !strcmp("frameRate", atts[8]));
185
186 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
187 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
188 CHECK(codec != -1);
189
190 MediaProfiles::VideoCodec *videoCodec =
191 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
192 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
193 logVideoCodec(*videoCodec);
194
195 size_t nCamcorderProfiles;
196 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
197 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
198 return videoCodec;
199}
200
201/*static*/ MediaProfiles::AudioCodec*
202MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
203{
204 CHECK(!strcmp("codec", atts[0]) &&
205 !strcmp("bitRate", atts[2]) &&
206 !strcmp("sampleRate", atts[4]) &&
207 !strcmp("channels", atts[6]));
208 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
209 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
210 CHECK(codec != -1);
211
212 MediaProfiles::AudioCodec *audioCodec =
213 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
214 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
215 logAudioCodec(*audioCodec);
216
217 size_t nCamcorderProfiles;
218 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
219 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
220 return audioCodec;
221}
222/*static*/ MediaProfiles::AudioDecoderCap*
223MediaProfiles::createAudioDecoderCap(const char **atts)
224{
225 CHECK(!strcmp("name", atts[0]) &&
226 !strcmp("enabled", atts[2]));
227
228 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
229 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
230 CHECK(codec != -1);
231
232 MediaProfiles::AudioDecoderCap *cap =
233 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
234 logAudioDecoderCap(*cap);
235 return cap;
236}
237
238/*static*/ MediaProfiles::VideoDecoderCap*
239MediaProfiles::createVideoDecoderCap(const char **atts)
240{
241 CHECK(!strcmp("name", atts[0]) &&
242 !strcmp("enabled", atts[2]));
243
244 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
245 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
246 CHECK(codec != -1);
247
248 MediaProfiles::VideoDecoderCap *cap =
249 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
250 logVideoDecoderCap(*cap);
251 return cap;
252}
253
254/*static*/ MediaProfiles::VideoEncoderCap*
255MediaProfiles::createVideoEncoderCap(const char **atts)
256{
257 CHECK(!strcmp("name", atts[0]) &&
258 !strcmp("enabled", atts[2]) &&
259 !strcmp("minBitRate", atts[4]) &&
260 !strcmp("maxBitRate", atts[6]) &&
261 !strcmp("minFrameWidth", atts[8]) &&
262 !strcmp("maxFrameWidth", atts[10]) &&
263 !strcmp("minFrameHeight", atts[12]) &&
264 !strcmp("maxFrameHeight", atts[14]) &&
265 !strcmp("minFrameRate", atts[16]) &&
266 !strcmp("maxFrameRate", atts[18]));
267
268 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
269 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
270 CHECK(codec != -1);
271
272 MediaProfiles::VideoEncoderCap *cap =
273 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
274 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
275 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
276 logVideoEncoderCap(*cap);
277 return cap;
278}
279
280/*static*/ MediaProfiles::AudioEncoderCap*
281MediaProfiles::createAudioEncoderCap(const char **atts)
282{
283 CHECK(!strcmp("name", atts[0]) &&
284 !strcmp("enabled", atts[2]) &&
285 !strcmp("minBitRate", atts[4]) &&
286 !strcmp("maxBitRate", atts[6]) &&
287 !strcmp("minSampleRate", atts[8]) &&
288 !strcmp("maxSampleRate", atts[10]) &&
289 !strcmp("minChannels", atts[12]) &&
290 !strcmp("maxChannels", atts[14]));
291
292 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
293 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
294 CHECK(codec != -1);
295
296 MediaProfiles::AudioEncoderCap *cap =
297 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]), atoi(atts[7]),
298 atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
299 atoi(atts[15]));
300 logAudioEncoderCap(*cap);
301 return cap;
302}
303
304/*static*/ output_format
305MediaProfiles::createEncoderOutputFileFormat(const char **atts)
306{
307 CHECK(!strcmp("name", atts[0]));
308
309 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
310 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
311 CHECK(format != -1);
312
313 return static_cast<output_format>(format);
314}
315
James Dong2a7e0a12011-02-28 21:07:39 -0800316static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
317 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
318 if (cameraId == cameraIds[i]) {
319 return true;
320 }
321 }
322 return false;
323}
324
James Dong1d7491b2010-01-19 17:45:38 -0800325/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800326MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800327{
328 CHECK(!strcmp("quality", atts[0]) &&
329 !strcmp("fileFormat", atts[2]) &&
330 !strcmp("duration", atts[4]));
331
332 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/sizeof(sCamcorderQualityNameMap[0]);
333 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
334 CHECK(quality != -1);
335
336 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
337 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
338 CHECK(fileFormat != -1);
339
340 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800341 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800342 if (!isCameraIdFound(cameraId, cameraIds)) {
343 cameraIds.add(cameraId);
344 }
James Dong1d7491b2010-01-19 17:45:38 -0800345 profile->mFileFormat = static_cast<output_format>(fileFormat);
346 profile->mQuality = static_cast<camcorder_quality>(quality);
347 profile->mDuration = atoi(atts[5]);
348 return profile;
349}
350
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800351MediaProfiles::ImageEncodingQualityLevels*
352MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
353{
354 int n = mImageEncodingQualityLevels.size();
355 for (int i = 0; i < n; i++) {
356 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
357 if (levels->mCameraId == cameraId) {
358 return levels;
359 }
360 }
361 return NULL;
362}
363
364void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800365{
366 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800367 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800368 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800369 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
370
371 if (levels == NULL) {
372 levels = new ImageEncodingQualityLevels();
373 levels->mCameraId = cameraId;
374 mImageEncodingQualityLevels.add(levels);
375 }
376
377 levels->mLevels.add(quality);
378}
379
380/*static*/ int
381MediaProfiles::getCameraId(const char** atts)
382{
383 if (!atts[0]) return 0; // default cameraId = 0
384 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800385 return atoi(atts[1]);
386}
387
James Dong0f056292011-05-09 18:49:31 -0700388void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
389{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700390 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700391 if (atts[2]) {
392 CHECK(!strcmp("startOffsetMs", atts[2]));
393 offsetTimeMs = atoi(atts[3]);
394 }
395
Steve Block3856b092011-10-20 11:56:00 +0100396 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700397 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
398}
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700399/*static*/ MediaProfiles::ExportVideoProfile*
400MediaProfiles::createExportVideoProfile(const char **atts)
401{
402 CHECK(!strcmp("name", atts[0]) &&
403 !strcmp("profile", atts[2]) &&
404 !strcmp("level", atts[4]));
James Dong0f056292011-05-09 18:49:31 -0700405
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700406 const size_t nMappings =
407 sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
408 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
409 CHECK(codec != -1);
410
411 MediaProfiles::ExportVideoProfile *profile =
412 new MediaProfiles::ExportVideoProfile(
413 codec, atoi(atts[3]), atoi(atts[5]));
414
415 return profile;
416}
Hong Tengcabd5f82011-07-06 18:33:09 -0700417/*static*/ MediaProfiles::VideoEditorCap*
418MediaProfiles::createVideoEditorCap(const char **atts, MediaProfiles *profiles)
419{
420 CHECK(!strcmp("maxInputFrameWidth", atts[0]) &&
421 !strcmp("maxInputFrameHeight", atts[2]) &&
422 !strcmp("maxOutputFrameWidth", atts[4]) &&
Hong Teng3a9cefe2011-11-10 14:54:26 -0800423 !strcmp("maxOutputFrameHeight", atts[6]) &&
424 !strcmp("maxPrefetchYUVFrames", atts[8]));
Hong Tengcabd5f82011-07-06 18:33:09 -0700425
426 MediaProfiles::VideoEditorCap *pVideoEditorCap =
427 new MediaProfiles::VideoEditorCap(atoi(atts[1]), atoi(atts[3]),
Hong Teng3a9cefe2011-11-10 14:54:26 -0800428 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
Hong Tengcabd5f82011-07-06 18:33:09 -0700429
430 logVideoEditorCap(*pVideoEditorCap);
431 profiles->mVideoEditorCap = pVideoEditorCap;
432
433 return pVideoEditorCap;
434}
435
James Dong1d7491b2010-01-19 17:45:38 -0800436/*static*/ void
437MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
438{
439 MediaProfiles *profiles = (MediaProfiles *) userData;
440 if (strcmp("Video", name) == 0) {
441 createVideoCodec(atts, profiles);
442 } else if (strcmp("Audio", name) == 0) {
443 createAudioCodec(atts, profiles);
444 } else if (strcmp("VideoEncoderCap", name) == 0 &&
445 strcmp("true", atts[3]) == 0) {
446 profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
447 } else if (strcmp("AudioEncoderCap", name) == 0 &&
448 strcmp("true", atts[3]) == 0) {
449 profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
450 } else if (strcmp("VideoDecoderCap", name) == 0 &&
451 strcmp("true", atts[3]) == 0) {
452 profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
453 } else if (strcmp("AudioDecoderCap", name) == 0 &&
454 strcmp("true", atts[3]) == 0) {
455 profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
456 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
457 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800458 } else if (strcmp("CamcorderProfiles", name) == 0) {
459 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700460 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800461 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800462 profiles->mCamcorderProfiles.add(
James Dong2a7e0a12011-02-28 21:07:39 -0800463 createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
James Dongf5a83852010-02-23 17:21:44 -0800464 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800465 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
Hong Tengcabd5f82011-07-06 18:33:09 -0700466 } else if (strcmp("VideoEditorCap", name) == 0) {
467 createVideoEditorCap(atts, profiles);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700468 } else if (strcmp("ExportVideoProfile", name) == 0) {
469 profiles->mVideoEditorExportProfiles.add(createExportVideoProfile(atts));
James Dong1d7491b2010-01-19 17:45:38 -0800470 }
471}
472
James Dong2a7e0a12011-02-28 21:07:39 -0800473static bool isCamcorderProfile(camcorder_quality quality) {
474 return quality >= CAMCORDER_QUALITY_LIST_START &&
475 quality <= CAMCORDER_QUALITY_LIST_END;
476}
477
478static bool isTimelapseProfile(camcorder_quality quality) {
479 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
480 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
481}
482
Zhijun Hee0790972014-07-23 15:17:26 -0700483static bool isHighSpeedProfile(camcorder_quality quality) {
484 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
485 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
486}
487
James Dong2a7e0a12011-02-28 21:07:39 -0800488void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700489 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800490 CHECK(cameraIds.size() > 0);
491 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
492 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
493 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
494 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
495 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
496 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
497 if ((j & 1) == 0) { // low resolution
498 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
499 } else { // high resolution
500 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
501 }
502 }
503 }
504}
505
506int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
507 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
508 if (mCameraIds[i] == cameraId) {
509 return i;
510 }
511 }
512 return -1;
513}
514
515void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
516 if (sIsInitialized) {
517 return;
518 }
519
520 initRequiredProfileRefs(mCameraIds);
521
522 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
523 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
524 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
525
526 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
527 int cameraId = mCamcorderProfiles[i]->mCameraId;
528 int index = -1;
529 int refIndex = getRequiredProfileRefIndex(cameraId);
530 CHECK(refIndex != -1);
531 RequiredProfileRefInfo *info;
532 camcorder_quality refQuality;
533 VideoCodec *codec = NULL;
534
Zhijun Hee0790972014-07-23 15:17:26 -0700535 // Check high and low from either camcorder profile, timelapse profile
536 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800537 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200538 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800539 if (isTimelapseProfile(quality)) {
540 // Check timelapse profile instead.
541 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200542 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700543 } else if (isHighSpeedProfile(quality)) {
544 // Skip the check for high speed profile.
545 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800546 } else {
547 // Must be camcorder profile.
548 CHECK(isCamcorderProfile(quality));
549 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200550 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800551 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
552 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
553 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
554 continue;
555 }
556 switch (j) {
557 case 0:
558 refQuality = CAMCORDER_QUALITY_LOW;
559 break;
560 case 1:
561 refQuality = CAMCORDER_QUALITY_HIGH;
562 break;
563 case 2:
564 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
565 break;
566 case 3:
567 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
568 break;
569 default:
570 CHECK(!"Should never reach here");
571 }
572
573 if (!info->mHasRefProfile) {
574 index = getCamcorderProfileIndex(cameraId, refQuality);
575 }
576 if (index == -1) {
577 // New high or low quality profile is found.
578 // Update its reference.
579 info->mHasRefProfile = true;
580 info->mRefProfileIndex = i;
581 info->mResolutionProduct = product;
582 }
583 }
584 }
585
586 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
587 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
588 int refIndex = getRequiredProfileRefIndex(cameraId);
589 CHECK(refIndex != -1);
590 RequiredProfileRefInfo *info =
591 &mRequiredProfileRefs[refIndex].mRefs[j];
592
593 if (info->mHasRefProfile) {
594
595 CamcorderProfile *profile =
596 new CamcorderProfile(
597 *mCamcorderProfiles[info->mRefProfileIndex]);
598
599 // Overwrite the quality
600 switch (j % kNumRequiredProfiles) {
601 case 0:
602 profile->mQuality = CAMCORDER_QUALITY_LOW;
603 break;
604 case 1:
605 profile->mQuality = CAMCORDER_QUALITY_HIGH;
606 break;
607 case 2:
608 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
609 break;
610 case 3:
611 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
612 break;
613 default:
614 CHECK(!"Should never come here");
615 }
616
617 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
618 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700619 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800620 profile->mQuality, cameraId);
621 CHECK(index == refIndex);
622 continue;
623 }
624
625 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700626 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800627 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
628 profile->mQuality, cameraId);
629
630 mCamcorderProfiles.add(profile);
631 }
632 }
633 }
634}
635
James Dong1d7491b2010-01-19 17:45:38 -0800636/*static*/ MediaProfiles*
637MediaProfiles::getInstance()
638{
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800640 Mutex::Autolock lock(sLock);
641 if (!sIsInitialized) {
642 char value[PROPERTY_VALUE_MAX];
643 if (property_get("media.settings.xml", value, NULL) <= 0) {
644 const char *defaultXmlFile = "/etc/media_profiles.xml";
645 FILE *fp = fopen(defaultXmlFile, "r");
646 if (fp == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000647 ALOGW("could not find media config xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800648 sInstance = createDefaultInstance();
649 } else {
650 fclose(fp); // close the file first.
651 sInstance = createInstanceFromXmlFile(defaultXmlFile);
652 }
653 } else {
654 sInstance = createInstanceFromXmlFile(value);
655 }
James Dong2a7e0a12011-02-28 21:07:39 -0800656 CHECK(sInstance != NULL);
657 sInstance->checkAndAddRequiredProfilesIfNecessary();
658 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800659 }
660
661 return sInstance;
662}
663
664/*static*/ MediaProfiles::VideoEncoderCap*
665MediaProfiles::createDefaultH263VideoEncoderCap()
666{
667 return new MediaProfiles::VideoEncoderCap(
668 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
669}
670
671/*static*/ MediaProfiles::VideoEncoderCap*
672MediaProfiles::createDefaultM4vVideoEncoderCap()
673{
674 return new MediaProfiles::VideoEncoderCap(
675 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
676}
677
678
679/*static*/ void
680MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
681{
682 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
683 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
684}
685
686/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700687MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700688{
689 MediaProfiles::VideoCodec *videoCodec =
690 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
691
692 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
693 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
694 profile->mCameraId = 0;
695 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700696 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700697 profile->mDuration = 60;
698 profile->mVideoCodec = videoCodec;
699 profile->mAudioCodec = audioCodec;
700 return profile;
701}
702
703/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700704MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800705{
706 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700707 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800708
709 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800710 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
711 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800712 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700713 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800714 profile->mDuration = 60;
715 profile->mVideoCodec = videoCodec;
716 profile->mAudioCodec = audioCodec;
717 return profile;
718}
719
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700720/*static*/ void
721MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
722 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
723 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
724 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(CAMCORDER_QUALITY_TIME_LAPSE_LOW);
725 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
726}
727
728/*static*/ void
729MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
730 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
731 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
732 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
733 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(CAMCORDER_QUALITY_TIME_LAPSE_480P);
734}
735
James Dong1d7491b2010-01-19 17:45:38 -0800736/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700737MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800738{
739 MediaProfiles::VideoCodec *videoCodec =
740 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
741
742 MediaProfiles::AudioCodec *audioCodec =
743 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
744
745 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800746 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800747 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700748 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800749 profile->mDuration = 30;
750 profile->mVideoCodec = videoCodec;
751 profile->mAudioCodec = audioCodec;
752 return profile;
753}
754
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700755/*static*/ MediaProfiles::CamcorderProfile*
756MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
757{
758 MediaProfiles::VideoCodec *videoCodec =
759 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
760
761 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
762 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
763 profile->mCameraId = 0;
764 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
765 profile->mQuality = quality;
766 profile->mDuration = 60;
767 profile->mVideoCodec = videoCodec;
768 profile->mAudioCodec = audioCodec;
769 return profile;
770}
771
772/*static*/ void
773MediaProfiles::createDefaultCamcorderLowProfiles(
774 MediaProfiles::CamcorderProfile **lowProfile,
775 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
776 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
777 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
778}
779
780/*static*/ void
781MediaProfiles::createDefaultCamcorderHighProfiles(
782 MediaProfiles::CamcorderProfile **highProfile,
783 MediaProfiles::CamcorderProfile **highSpecificProfile) {
784 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
785 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
786}
787
James Dong1d7491b2010-01-19 17:45:38 -0800788/*static*/ void
789MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
790{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700791 // low camcorder profiles.
792 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
793 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
794 profiles->mCamcorderProfiles.add(lowProfile);
795 profiles->mCamcorderProfiles.add(lowSpecificProfile);
796
797 // high camcorder profiles.
798 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
799 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
800 profiles->mCamcorderProfiles.add(highProfile);
801 profiles->mCamcorderProfiles.add(highSpecificProfile);
802
803 // low camcorder time lapse profiles.
804 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
805 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
806 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
807 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
808
809 // high camcorder time lapse profiles.
810 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
811 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile, &highSpecificTimeLapseProfile);
812 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
813 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700814
815 // For emulator and other legacy devices which does not have a
816 // media_profiles.xml file, We assume that the default camera id
817 // is 0 and that is the only camera available.
818 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800819}
820
821/*static*/ void
822MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
823{
824 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
825}
826
827/*static*/ void
828MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
829{
830 MediaProfiles::VideoDecoderCap *cap =
831 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
832
833 profiles->mVideoDecoders.add(cap);
834}
835
836/*static*/ void
837MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
838{
839 MediaProfiles::AudioDecoderCap *cap =
840 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
841
842 profiles->mAudioDecoders.add(cap);
843}
844
845/*static*/ void
846MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
847{
848 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
849 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
850}
851
852/*static*/ MediaProfiles::AudioEncoderCap*
853MediaProfiles::createDefaultAmrNBEncoderCap()
854{
855 return new MediaProfiles::AudioEncoderCap(
856 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
857}
858
James Dongf5a83852010-02-23 17:21:44 -0800859/*static*/ void
860MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
861{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800862 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
863 levels->mCameraId = 0;
864 levels->mLevels.add(70);
865 levels->mLevels.add(80);
866 levels->mLevels.add(90);
867 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800868}
869
Hong Tengcabd5f82011-07-06 18:33:09 -0700870/*static*/ void
871MediaProfiles::createDefaultVideoEditorCap(MediaProfiles *profiles)
872{
873 profiles->mVideoEditorCap =
874 new MediaProfiles::VideoEditorCap(
875 VIDEOEDITOR_DEFAULT_MAX_INPUT_FRAME_WIDTH,
876 VIDEOEDITOR_DEFUALT_MAX_INPUT_FRAME_HEIGHT,
877 VIDEOEDITOR_DEFAULT_MAX_OUTPUT_FRAME_WIDTH,
Hong Teng3a9cefe2011-11-10 14:54:26 -0800878 VIDEOEDITOR_DEFUALT_MAX_OUTPUT_FRAME_HEIGHT,
879 VIDEOEDITOR_DEFAULT_MAX_PREFETCH_YUV_FRAMES);
Hong Tengcabd5f82011-07-06 18:33:09 -0700880}
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700881/*static*/ void
882MediaProfiles::createDefaultExportVideoProfiles(MediaProfiles *profiles)
883{
884 // Create default video export profiles
885 profiles->mVideoEditorExportProfiles.add(
886 new ExportVideoProfile(VIDEO_ENCODER_H263,
887 OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level10));
888 profiles->mVideoEditorExportProfiles.add(
889 new ExportVideoProfile(VIDEO_ENCODER_MPEG_4_SP,
890 OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level1));
891 profiles->mVideoEditorExportProfiles.add(
892 new ExportVideoProfile(VIDEO_ENCODER_H264,
893 OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel13));
894}
Hong Tengcabd5f82011-07-06 18:33:09 -0700895
James Dong1d7491b2010-01-19 17:45:38 -0800896/*static*/ MediaProfiles*
897MediaProfiles::createDefaultInstance()
898{
899 MediaProfiles *profiles = new MediaProfiles;
900 createDefaultCamcorderProfiles(profiles);
901 createDefaultVideoEncoders(profiles);
902 createDefaultAudioEncoders(profiles);
903 createDefaultVideoDecoders(profiles);
904 createDefaultAudioDecoders(profiles);
905 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800906 createDefaultImageEncodingQualityLevels(profiles);
Hong Tengcabd5f82011-07-06 18:33:09 -0700907 createDefaultVideoEditorCap(profiles);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700908 createDefaultExportVideoProfiles(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800909 return profiles;
910}
911
912/*static*/ MediaProfiles*
913MediaProfiles::createInstanceFromXmlFile(const char *xml)
914{
915 FILE *fp = NULL;
916 CHECK((fp = fopen(xml, "r")));
917
918 XML_Parser parser = ::XML_ParserCreate(NULL);
919 CHECK(parser != NULL);
920
921 MediaProfiles *profiles = new MediaProfiles();
922 ::XML_SetUserData(parser, profiles);
923 ::XML_SetElementHandler(parser, startElementHandler, NULL);
924
925 /*
926 FIXME:
927 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
928
929 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000930 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800931 return UNKNOWN_ERROR;
932 }
933
934 */
935
936 const int BUFF_SIZE = 512;
937 for (;;) {
938 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
939 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000940 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800941 delete profiles;
942 profiles = NULL;
943 goto exit;
944 }
945
946 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
947 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000948 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800949 delete profiles;
950 profiles = NULL;
951 goto exit;
952 }
953
954 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
955
956 if (bytes_read == 0) break; // done parsing the xml file
957 }
958
959exit:
960 ::XML_ParserFree(parser);
961 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -0800962 return profiles;
963}
964
965Vector<output_format> MediaProfiles::getOutputFileFormats() const
966{
967 return mEncoderOutputFileFormats; // copy out
968}
969
970Vector<video_encoder> MediaProfiles::getVideoEncoders() const
971{
972 Vector<video_encoder> encoders;
973 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
974 encoders.add(mVideoEncoders[i]->mCodec);
975 }
976 return encoders; // copy out
977}
978
979int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
980{
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800982 int index = -1;
983 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
984 if (mVideoEncoders[i]->mCodec == codec) {
985 index = i;
986 break;
987 }
988 }
989 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000990 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800991 return -1;
992 }
993
994 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
995 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
996 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
997 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
998 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
999 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
1000 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
1001 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
1002
Steve Block29357bc2012-01-06 19:20:56 +00001003 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001004 return -1;
1005}
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -07001006int MediaProfiles::getVideoEditorExportParamByName(
1007 const char *name, int codec) const
1008{
Steve Block3856b092011-10-20 11:56:00 +01001009 ALOGV("getVideoEditorExportParamByName: name %s codec %d", name, codec);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -07001010 ExportVideoProfile *exportProfile = NULL;
1011 int index = -1;
1012 for (size_t i =0; i < mVideoEditorExportProfiles.size(); i++) {
1013 exportProfile = mVideoEditorExportProfiles[i];
1014 if (exportProfile->mCodec == codec) {
1015 index = i;
1016 break;
1017 }
1018 }
1019 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001020 ALOGE("The given video decoder %d is not found", codec);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -07001021 return -1;
1022 }
1023 if (!strcmp("videoeditor.export.profile", name))
1024 return exportProfile->mProfile;
1025 if (!strcmp("videoeditor.export.level", name))
1026 return exportProfile->mLevel;
James Dong1d7491b2010-01-19 17:45:38 -08001027
Steve Block29357bc2012-01-06 19:20:56 +00001028 ALOGE("The given video editor export param name %s is not found", name);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -07001029 return -1;
1030}
Hong Tengcabd5f82011-07-06 18:33:09 -07001031int MediaProfiles::getVideoEditorCapParamByName(const char *name) const
1032{
Steve Block3856b092011-10-20 11:56:00 +01001033 ALOGV("getVideoEditorCapParamByName: %s", name);
Hong Tengcabd5f82011-07-06 18:33:09 -07001034
1035 if (mVideoEditorCap == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00001036 ALOGE("The mVideoEditorCap is not created, then create default cap.");
Hong Tengcabd5f82011-07-06 18:33:09 -07001037 createDefaultVideoEditorCap(sInstance);
1038 }
1039
1040 if (!strcmp("videoeditor.input.width.max", name))
1041 return mVideoEditorCap->mMaxInputFrameWidth;
1042 if (!strcmp("videoeditor.input.height.max", name))
1043 return mVideoEditorCap->mMaxInputFrameHeight;
1044 if (!strcmp("videoeditor.output.width.max", name))
1045 return mVideoEditorCap->mMaxOutputFrameWidth;
1046 if (!strcmp("videoeditor.output.height.max", name))
1047 return mVideoEditorCap->mMaxOutputFrameHeight;
Hong Teng3a9cefe2011-11-10 14:54:26 -08001048 if (!strcmp("maxPrefetchYUVFrames", name))
1049 return mVideoEditorCap->mMaxPrefetchYUVFrames;
Hong Tengcabd5f82011-07-06 18:33:09 -07001050
Steve Block29357bc2012-01-06 19:20:56 +00001051 ALOGE("The given video editor param name %s is not found", name);
Hong Tengcabd5f82011-07-06 18:33:09 -07001052 return -1;
1053}
1054
James Dong1d7491b2010-01-19 17:45:38 -08001055Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
1056{
1057 Vector<audio_encoder> encoders;
1058 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1059 encoders.add(mAudioEncoders[i]->mCodec);
1060 }
1061 return encoders; // copy out
1062}
1063
1064int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1065{
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001067 int index = -1;
1068 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
1069 if (mAudioEncoders[i]->mCodec == codec) {
1070 index = i;
1071 break;
1072 }
1073 }
1074 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001075 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001076 return -1;
1077 }
1078
1079 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
1080 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
1081 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
1082 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
1083 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
1084 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
1085
Steve Block29357bc2012-01-06 19:20:56 +00001086 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001087 return -1;
1088}
1089
1090Vector<video_decoder> MediaProfiles::getVideoDecoders() const
1091{
1092 Vector<video_decoder> decoders;
1093 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1094 decoders.add(mVideoDecoders[i]->mCodec);
1095 }
1096 return decoders; // copy out
1097}
1098
1099Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1100{
1101 Vector<audio_decoder> decoders;
1102 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1103 decoders.add(mAudioDecoders[i]->mCodec);
1104 }
1105 return decoders; // copy out
1106}
1107
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001108int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001109{
James Dong1d7491b2010-01-19 17:45:38 -08001110 int index = -1;
1111 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001112 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1113 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001114 index = i;
1115 break;
1116 }
1117 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001118 return index;
1119}
1120
1121int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1122 int cameraId,
1123 camcorder_quality quality) const
1124{
Steve Block3856b092011-10-20 11:56:00 +01001125 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001126 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001127
1128 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001129 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001130 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001131 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001132 return -1;
1133 }
1134
James Dongf5a83852010-02-23 17:21:44 -08001135 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001136 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1137 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1138 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1139 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1140 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1141 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1142 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1143 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1144 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1145 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1146
Steve Block29357bc2012-01-06 19:20:56 +00001147 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001148 return -1;
1149}
1150
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001151bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1152{
1153 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1154}
1155
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001156Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001157{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001158 Vector<int> result;
1159 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1160 if (levels != NULL) {
1161 result = levels->mLevels; // copy out
1162 }
1163 return result;
James Dongf5a83852010-02-23 17:21:44 -08001164}
1165
James Dong0f056292011-05-09 18:49:31 -07001166int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1167 int offsetTimeMs = -1;
1168 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1169 if (index >= 0) {
1170 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1171 }
Steve Block3856b092011-10-20 11:56:00 +01001172 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001173 return offsetTimeMs;
1174}
1175
James Dong1d7491b2010-01-19 17:45:38 -08001176MediaProfiles::~MediaProfiles()
1177{
1178 CHECK("destructor should never be called" == 0);
1179#if 0
1180 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1181 delete mAudioEncoders[i];
1182 }
1183 mAudioEncoders.clear();
1184
1185 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1186 delete mVideoEncoders[i];
1187 }
1188 mVideoEncoders.clear();
1189
1190 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1191 delete mVideoDecoders[i];
1192 }
1193 mVideoDecoders.clear();
1194
1195 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1196 delete mAudioDecoders[i];
1197 }
1198 mAudioDecoders.clear();
1199
1200 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1201 delete mCamcorderProfiles[i];
1202 }
1203 mCamcorderProfiles.clear();
1204#endif
1205}
1206} // namespace android