blob: 1074da95732900b02c87211f0f70a8480004ad0b [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},
James Dong669012d2011-09-19 16:27:31 -070072 {"qvga", CAMCORDER_QUALITY_QVGA},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070073
74 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
75 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
76 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070077 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070078 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
79 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -070080 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
81 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
James Dong1d7491b2010-01-19 17:45:38 -080082};
83
Glenn Kasten80520382014-01-31 16:49:31 -080084#if LOG_NDEBUG
85#define UNUSED __unused
86#else
87#define UNUSED
88#endif
89
James Dong1d7491b2010-01-19 17:45:38 -080090/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -080091MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -080092{
Steve Block3856b092011-10-20 11:56:00 +010093 ALOGV("video codec:");
94 ALOGV("codec = %d", codec.mCodec);
95 ALOGV("bit rate: %d", codec.mBitRate);
96 ALOGV("frame width: %d", codec.mFrameWidth);
97 ALOGV("frame height: %d", codec.mFrameHeight);
98 ALOGV("frame rate: %d", codec.mFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -080099}
100
101/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800102MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800103{
Steve Block3856b092011-10-20 11:56:00 +0100104 ALOGV("audio codec:");
105 ALOGV("codec = %d", codec.mCodec);
106 ALOGV("bit rate: %d", codec.mBitRate);
107 ALOGV("sample rate: %d", codec.mSampleRate);
108 ALOGV("number of channels: %d", codec.mChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800109}
110
111/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800112MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800113{
Steve Block3856b092011-10-20 11:56:00 +0100114 ALOGV("video encoder cap:");
115 ALOGV("codec = %d", cap.mCodec);
116 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
117 ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
118 ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
119 ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800120}
121
122/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800123MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800124{
Steve Block3856b092011-10-20 11:56:00 +0100125 ALOGV("audio encoder cap:");
126 ALOGV("codec = %d", cap.mCodec);
127 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
128 ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
129 ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800130}
131
132/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800133MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800134{
Steve Block3856b092011-10-20 11:56:00 +0100135 ALOGV("video decoder cap:");
136 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800137}
138
139/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800140MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800141{
Steve Block3856b092011-10-20 11:56:00 +0100142 ALOGV("audio codec cap:");
143 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800144}
145
Hong Tengcabd5f82011-07-06 18:33:09 -0700146/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800147MediaProfiles::logVideoEditorCap(const MediaProfiles::VideoEditorCap& cap UNUSED)
Hong Tengcabd5f82011-07-06 18:33:09 -0700148{
Steve Block3856b092011-10-20 11:56:00 +0100149 ALOGV("videoeditor cap:");
150 ALOGV("mMaxInputFrameWidth = %d", cap.mMaxInputFrameWidth);
151 ALOGV("mMaxInputFrameHeight = %d", cap.mMaxInputFrameHeight);
152 ALOGV("mMaxOutputFrameWidth = %d", cap.mMaxOutputFrameWidth);
153 ALOGV("mMaxOutputFrameHeight = %d", cap.mMaxOutputFrameHeight);
Hong Tengcabd5f82011-07-06 18:33:09 -0700154}
155
James Dong1d7491b2010-01-19 17:45:38 -0800156/*static*/ int
157MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings, const char *name)
158{
159 int tag = -1;
160 for (size_t i = 0; i < nMappings; ++i) {
161 if (!strcmp(map[i].name, name)) {
162 tag = map[i].tag;
163 break;
164 }
165 }
166 return tag;
167}
168
169/*static*/ MediaProfiles::VideoCodec*
170MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
171{
172 CHECK(!strcmp("codec", atts[0]) &&
173 !strcmp("bitRate", atts[2]) &&
174 !strcmp("width", atts[4]) &&
175 !strcmp("height", atts[6]) &&
176 !strcmp("frameRate", atts[8]));
177
178 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
179 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
180 CHECK(codec != -1);
181
182 MediaProfiles::VideoCodec *videoCodec =
183 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
184 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
185 logVideoCodec(*videoCodec);
186
187 size_t nCamcorderProfiles;
188 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
189 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
190 return videoCodec;
191}
192
193/*static*/ MediaProfiles::AudioCodec*
194MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
195{
196 CHECK(!strcmp("codec", atts[0]) &&
197 !strcmp("bitRate", atts[2]) &&
198 !strcmp("sampleRate", atts[4]) &&
199 !strcmp("channels", atts[6]));
200 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
201 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
202 CHECK(codec != -1);
203
204 MediaProfiles::AudioCodec *audioCodec =
205 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
206 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
207 logAudioCodec(*audioCodec);
208
209 size_t nCamcorderProfiles;
210 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
211 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
212 return audioCodec;
213}
214/*static*/ MediaProfiles::AudioDecoderCap*
215MediaProfiles::createAudioDecoderCap(const char **atts)
216{
217 CHECK(!strcmp("name", atts[0]) &&
218 !strcmp("enabled", atts[2]));
219
220 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
221 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
222 CHECK(codec != -1);
223
224 MediaProfiles::AudioDecoderCap *cap =
225 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
226 logAudioDecoderCap(*cap);
227 return cap;
228}
229
230/*static*/ MediaProfiles::VideoDecoderCap*
231MediaProfiles::createVideoDecoderCap(const char **atts)
232{
233 CHECK(!strcmp("name", atts[0]) &&
234 !strcmp("enabled", atts[2]));
235
236 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
237 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
238 CHECK(codec != -1);
239
240 MediaProfiles::VideoDecoderCap *cap =
241 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
242 logVideoDecoderCap(*cap);
243 return cap;
244}
245
246/*static*/ MediaProfiles::VideoEncoderCap*
247MediaProfiles::createVideoEncoderCap(const char **atts)
248{
249 CHECK(!strcmp("name", atts[0]) &&
250 !strcmp("enabled", atts[2]) &&
251 !strcmp("minBitRate", atts[4]) &&
252 !strcmp("maxBitRate", atts[6]) &&
253 !strcmp("minFrameWidth", atts[8]) &&
254 !strcmp("maxFrameWidth", atts[10]) &&
255 !strcmp("minFrameHeight", atts[12]) &&
256 !strcmp("maxFrameHeight", atts[14]) &&
257 !strcmp("minFrameRate", atts[16]) &&
258 !strcmp("maxFrameRate", atts[18]));
259
260 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
261 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
262 CHECK(codec != -1);
263
264 MediaProfiles::VideoEncoderCap *cap =
265 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
266 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
267 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
268 logVideoEncoderCap(*cap);
269 return cap;
270}
271
272/*static*/ MediaProfiles::AudioEncoderCap*
273MediaProfiles::createAudioEncoderCap(const char **atts)
274{
275 CHECK(!strcmp("name", atts[0]) &&
276 !strcmp("enabled", atts[2]) &&
277 !strcmp("minBitRate", atts[4]) &&
278 !strcmp("maxBitRate", atts[6]) &&
279 !strcmp("minSampleRate", atts[8]) &&
280 !strcmp("maxSampleRate", atts[10]) &&
281 !strcmp("minChannels", atts[12]) &&
282 !strcmp("maxChannels", atts[14]));
283
284 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
285 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
286 CHECK(codec != -1);
287
288 MediaProfiles::AudioEncoderCap *cap =
289 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]), atoi(atts[7]),
290 atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
291 atoi(atts[15]));
292 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
324 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/sizeof(sCamcorderQualityNameMap[0]);
325 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
326 CHECK(quality != -1);
327
328 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
329 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
330 CHECK(fileFormat != -1);
331
332 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800333 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800334 if (!isCameraIdFound(cameraId, cameraIds)) {
335 cameraIds.add(cameraId);
336 }
James Dong1d7491b2010-01-19 17:45:38 -0800337 profile->mFileFormat = static_cast<output_format>(fileFormat);
338 profile->mQuality = static_cast<camcorder_quality>(quality);
339 profile->mDuration = atoi(atts[5]);
340 return profile;
341}
342
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800343MediaProfiles::ImageEncodingQualityLevels*
344MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
345{
346 int n = mImageEncodingQualityLevels.size();
347 for (int i = 0; i < n; i++) {
348 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
349 if (levels->mCameraId == cameraId) {
350 return levels;
351 }
352 }
353 return NULL;
354}
355
356void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800357{
358 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800359 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800360 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800361 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
362
363 if (levels == NULL) {
364 levels = new ImageEncodingQualityLevels();
365 levels->mCameraId = cameraId;
366 mImageEncodingQualityLevels.add(levels);
367 }
368
369 levels->mLevels.add(quality);
370}
371
372/*static*/ int
373MediaProfiles::getCameraId(const char** atts)
374{
375 if (!atts[0]) return 0; // default cameraId = 0
376 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800377 return atoi(atts[1]);
378}
379
James Dong0f056292011-05-09 18:49:31 -0700380void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
381{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700382 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700383 if (atts[2]) {
384 CHECK(!strcmp("startOffsetMs", atts[2]));
385 offsetTimeMs = atoi(atts[3]);
386 }
387
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700389 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
390}
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700391/*static*/ MediaProfiles::ExportVideoProfile*
392MediaProfiles::createExportVideoProfile(const char **atts)
393{
394 CHECK(!strcmp("name", atts[0]) &&
395 !strcmp("profile", atts[2]) &&
396 !strcmp("level", atts[4]));
James Dong0f056292011-05-09 18:49:31 -0700397
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700398 const size_t nMappings =
399 sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
400 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
401 CHECK(codec != -1);
402
403 MediaProfiles::ExportVideoProfile *profile =
404 new MediaProfiles::ExportVideoProfile(
405 codec, atoi(atts[3]), atoi(atts[5]));
406
407 return profile;
408}
Hong Tengcabd5f82011-07-06 18:33:09 -0700409/*static*/ MediaProfiles::VideoEditorCap*
410MediaProfiles::createVideoEditorCap(const char **atts, MediaProfiles *profiles)
411{
412 CHECK(!strcmp("maxInputFrameWidth", atts[0]) &&
413 !strcmp("maxInputFrameHeight", atts[2]) &&
414 !strcmp("maxOutputFrameWidth", atts[4]) &&
Hong Teng3a9cefe2011-11-10 14:54:26 -0800415 !strcmp("maxOutputFrameHeight", atts[6]) &&
416 !strcmp("maxPrefetchYUVFrames", atts[8]));
Hong Tengcabd5f82011-07-06 18:33:09 -0700417
418 MediaProfiles::VideoEditorCap *pVideoEditorCap =
419 new MediaProfiles::VideoEditorCap(atoi(atts[1]), atoi(atts[3]),
Hong Teng3a9cefe2011-11-10 14:54:26 -0800420 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
Hong Tengcabd5f82011-07-06 18:33:09 -0700421
422 logVideoEditorCap(*pVideoEditorCap);
423 profiles->mVideoEditorCap = pVideoEditorCap;
424
425 return pVideoEditorCap;
426}
427
James Dong1d7491b2010-01-19 17:45:38 -0800428/*static*/ void
429MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
430{
431 MediaProfiles *profiles = (MediaProfiles *) userData;
432 if (strcmp("Video", name) == 0) {
433 createVideoCodec(atts, profiles);
434 } else if (strcmp("Audio", name) == 0) {
435 createAudioCodec(atts, profiles);
436 } else if (strcmp("VideoEncoderCap", name) == 0 &&
437 strcmp("true", atts[3]) == 0) {
438 profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
439 } else if (strcmp("AudioEncoderCap", name) == 0 &&
440 strcmp("true", atts[3]) == 0) {
441 profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
442 } else if (strcmp("VideoDecoderCap", name) == 0 &&
443 strcmp("true", atts[3]) == 0) {
444 profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
445 } else if (strcmp("AudioDecoderCap", name) == 0 &&
446 strcmp("true", atts[3]) == 0) {
447 profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
448 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
449 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800450 } else if (strcmp("CamcorderProfiles", name) == 0) {
451 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700452 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800453 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800454 profiles->mCamcorderProfiles.add(
James Dong2a7e0a12011-02-28 21:07:39 -0800455 createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
James Dongf5a83852010-02-23 17:21:44 -0800456 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800457 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
Hong Tengcabd5f82011-07-06 18:33:09 -0700458 } else if (strcmp("VideoEditorCap", name) == 0) {
459 createVideoEditorCap(atts, profiles);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700460 } else if (strcmp("ExportVideoProfile", name) == 0) {
461 profiles->mVideoEditorExportProfiles.add(createExportVideoProfile(atts));
James Dong1d7491b2010-01-19 17:45:38 -0800462 }
463}
464
James Dong2a7e0a12011-02-28 21:07:39 -0800465static bool isCamcorderProfile(camcorder_quality quality) {
466 return quality >= CAMCORDER_QUALITY_LIST_START &&
467 quality <= CAMCORDER_QUALITY_LIST_END;
468}
469
470static bool isTimelapseProfile(camcorder_quality quality) {
471 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
472 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
473}
474
475void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("Number of camera ids: %d", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800477 CHECK(cameraIds.size() > 0);
478 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
479 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
480 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
481 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
482 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
483 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
484 if ((j & 1) == 0) { // low resolution
485 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
486 } else { // high resolution
487 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
488 }
489 }
490 }
491}
492
493int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
494 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
495 if (mCameraIds[i] == cameraId) {
496 return i;
497 }
498 }
499 return -1;
500}
501
502void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
503 if (sIsInitialized) {
504 return;
505 }
506
507 initRequiredProfileRefs(mCameraIds);
508
509 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
510 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
511 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
512
513 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
514 int cameraId = mCamcorderProfiles[i]->mCameraId;
515 int index = -1;
516 int refIndex = getRequiredProfileRefIndex(cameraId);
517 CHECK(refIndex != -1);
518 RequiredProfileRefInfo *info;
519 camcorder_quality refQuality;
520 VideoCodec *codec = NULL;
521
522 // Check high and low from either camcorder profile or timelapse profile
523 // but not both. Default, check camcorder profile
524 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200525 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800526 if (isTimelapseProfile(quality)) {
527 // Check timelapse profile instead.
528 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200529 o = kNumRequiredProfiles;
James Dong2a7e0a12011-02-28 21:07:39 -0800530 } else {
531 // Must be camcorder profile.
532 CHECK(isCamcorderProfile(quality));
533 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200534 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800535 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
536 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
537 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
538 continue;
539 }
540 switch (j) {
541 case 0:
542 refQuality = CAMCORDER_QUALITY_LOW;
543 break;
544 case 1:
545 refQuality = CAMCORDER_QUALITY_HIGH;
546 break;
547 case 2:
548 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
549 break;
550 case 3:
551 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
552 break;
553 default:
554 CHECK(!"Should never reach here");
555 }
556
557 if (!info->mHasRefProfile) {
558 index = getCamcorderProfileIndex(cameraId, refQuality);
559 }
560 if (index == -1) {
561 // New high or low quality profile is found.
562 // Update its reference.
563 info->mHasRefProfile = true;
564 info->mRefProfileIndex = i;
565 info->mResolutionProduct = product;
566 }
567 }
568 }
569
570 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
571 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
572 int refIndex = getRequiredProfileRefIndex(cameraId);
573 CHECK(refIndex != -1);
574 RequiredProfileRefInfo *info =
575 &mRequiredProfileRefs[refIndex].mRefs[j];
576
577 if (info->mHasRefProfile) {
578
579 CamcorderProfile *profile =
580 new CamcorderProfile(
581 *mCamcorderProfiles[info->mRefProfileIndex]);
582
583 // Overwrite the quality
584 switch (j % kNumRequiredProfiles) {
585 case 0:
586 profile->mQuality = CAMCORDER_QUALITY_LOW;
587 break;
588 case 1:
589 profile->mQuality = CAMCORDER_QUALITY_HIGH;
590 break;
591 case 2:
592 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
593 break;
594 case 3:
595 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
596 break;
597 default:
598 CHECK(!"Should never come here");
599 }
600
601 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
602 if (index != -1) {
Steve Block3856b092011-10-20 11:56:00 +0100603 ALOGV("Profile quality %d for camera %d already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800604 profile->mQuality, cameraId);
605 CHECK(index == refIndex);
606 continue;
607 }
608
609 // Insert the new profile
Steve Block3856b092011-10-20 11:56:00 +0100610 ALOGV("Add a profile: quality %d=>%d for camera %d",
James Dong2a7e0a12011-02-28 21:07:39 -0800611 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
612 profile->mQuality, cameraId);
613
614 mCamcorderProfiles.add(profile);
615 }
616 }
617 }
618}
619
James Dong1d7491b2010-01-19 17:45:38 -0800620/*static*/ MediaProfiles*
621MediaProfiles::getInstance()
622{
Steve Block3856b092011-10-20 11:56:00 +0100623 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800624 Mutex::Autolock lock(sLock);
625 if (!sIsInitialized) {
626 char value[PROPERTY_VALUE_MAX];
627 if (property_get("media.settings.xml", value, NULL) <= 0) {
628 const char *defaultXmlFile = "/etc/media_profiles.xml";
629 FILE *fp = fopen(defaultXmlFile, "r");
630 if (fp == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000631 ALOGW("could not find media config xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800632 sInstance = createDefaultInstance();
633 } else {
634 fclose(fp); // close the file first.
635 sInstance = createInstanceFromXmlFile(defaultXmlFile);
636 }
637 } else {
638 sInstance = createInstanceFromXmlFile(value);
639 }
James Dong2a7e0a12011-02-28 21:07:39 -0800640 CHECK(sInstance != NULL);
641 sInstance->checkAndAddRequiredProfilesIfNecessary();
642 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800643 }
644
645 return sInstance;
646}
647
648/*static*/ MediaProfiles::VideoEncoderCap*
649MediaProfiles::createDefaultH263VideoEncoderCap()
650{
651 return new MediaProfiles::VideoEncoderCap(
652 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
653}
654
655/*static*/ MediaProfiles::VideoEncoderCap*
656MediaProfiles::createDefaultM4vVideoEncoderCap()
657{
658 return new MediaProfiles::VideoEncoderCap(
659 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
660}
661
662
663/*static*/ void
664MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
665{
666 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
667 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
668}
669
670/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700671MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700672{
673 MediaProfiles::VideoCodec *videoCodec =
674 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
675
676 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
677 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
678 profile->mCameraId = 0;
679 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700680 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700681 profile->mDuration = 60;
682 profile->mVideoCodec = videoCodec;
683 profile->mAudioCodec = audioCodec;
684 return profile;
685}
686
687/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700688MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800689{
690 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700691 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800692
693 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800694 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
695 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800696 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700697 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800698 profile->mDuration = 60;
699 profile->mVideoCodec = videoCodec;
700 profile->mAudioCodec = audioCodec;
701 return profile;
702}
703
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700704/*static*/ void
705MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
706 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
707 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
708 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(CAMCORDER_QUALITY_TIME_LAPSE_LOW);
709 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
710}
711
712/*static*/ void
713MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
714 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
715 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
716 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
717 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(CAMCORDER_QUALITY_TIME_LAPSE_480P);
718}
719
James Dong1d7491b2010-01-19 17:45:38 -0800720/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700721MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800722{
723 MediaProfiles::VideoCodec *videoCodec =
724 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
725
726 MediaProfiles::AudioCodec *audioCodec =
727 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
728
729 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800730 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800731 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700732 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800733 profile->mDuration = 30;
734 profile->mVideoCodec = videoCodec;
735 profile->mAudioCodec = audioCodec;
736 return profile;
737}
738
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700739/*static*/ MediaProfiles::CamcorderProfile*
740MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
741{
742 MediaProfiles::VideoCodec *videoCodec =
743 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
744
745 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
746 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
747 profile->mCameraId = 0;
748 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
749 profile->mQuality = quality;
750 profile->mDuration = 60;
751 profile->mVideoCodec = videoCodec;
752 profile->mAudioCodec = audioCodec;
753 return profile;
754}
755
756/*static*/ void
757MediaProfiles::createDefaultCamcorderLowProfiles(
758 MediaProfiles::CamcorderProfile **lowProfile,
759 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
760 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
761 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
762}
763
764/*static*/ void
765MediaProfiles::createDefaultCamcorderHighProfiles(
766 MediaProfiles::CamcorderProfile **highProfile,
767 MediaProfiles::CamcorderProfile **highSpecificProfile) {
768 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
769 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
770}
771
James Dong1d7491b2010-01-19 17:45:38 -0800772/*static*/ void
773MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
774{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700775 // low camcorder profiles.
776 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
777 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
778 profiles->mCamcorderProfiles.add(lowProfile);
779 profiles->mCamcorderProfiles.add(lowSpecificProfile);
780
781 // high camcorder profiles.
782 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
783 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
784 profiles->mCamcorderProfiles.add(highProfile);
785 profiles->mCamcorderProfiles.add(highSpecificProfile);
786
787 // low camcorder time lapse profiles.
788 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
789 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
790 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
791 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
792
793 // high camcorder time lapse profiles.
794 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
795 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile, &highSpecificTimeLapseProfile);
796 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
797 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700798
799 // For emulator and other legacy devices which does not have a
800 // media_profiles.xml file, We assume that the default camera id
801 // is 0 and that is the only camera available.
802 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800803}
804
805/*static*/ void
806MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
807{
808 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
809}
810
811/*static*/ void
812MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
813{
814 MediaProfiles::VideoDecoderCap *cap =
815 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
816
817 profiles->mVideoDecoders.add(cap);
818}
819
820/*static*/ void
821MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
822{
823 MediaProfiles::AudioDecoderCap *cap =
824 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
825
826 profiles->mAudioDecoders.add(cap);
827}
828
829/*static*/ void
830MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
831{
832 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
833 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
834}
835
836/*static*/ MediaProfiles::AudioEncoderCap*
837MediaProfiles::createDefaultAmrNBEncoderCap()
838{
839 return new MediaProfiles::AudioEncoderCap(
840 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
841}
842
James Dongf5a83852010-02-23 17:21:44 -0800843/*static*/ void
844MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
845{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800846 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
847 levels->mCameraId = 0;
848 levels->mLevels.add(70);
849 levels->mLevels.add(80);
850 levels->mLevels.add(90);
851 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800852}
853
Hong Tengcabd5f82011-07-06 18:33:09 -0700854/*static*/ void
855MediaProfiles::createDefaultVideoEditorCap(MediaProfiles *profiles)
856{
857 profiles->mVideoEditorCap =
858 new MediaProfiles::VideoEditorCap(
859 VIDEOEDITOR_DEFAULT_MAX_INPUT_FRAME_WIDTH,
860 VIDEOEDITOR_DEFUALT_MAX_INPUT_FRAME_HEIGHT,
861 VIDEOEDITOR_DEFAULT_MAX_OUTPUT_FRAME_WIDTH,
Hong Teng3a9cefe2011-11-10 14:54:26 -0800862 VIDEOEDITOR_DEFUALT_MAX_OUTPUT_FRAME_HEIGHT,
863 VIDEOEDITOR_DEFAULT_MAX_PREFETCH_YUV_FRAMES);
Hong Tengcabd5f82011-07-06 18:33:09 -0700864}
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700865/*static*/ void
866MediaProfiles::createDefaultExportVideoProfiles(MediaProfiles *profiles)
867{
868 // Create default video export profiles
869 profiles->mVideoEditorExportProfiles.add(
870 new ExportVideoProfile(VIDEO_ENCODER_H263,
871 OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level10));
872 profiles->mVideoEditorExportProfiles.add(
873 new ExportVideoProfile(VIDEO_ENCODER_MPEG_4_SP,
874 OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level1));
875 profiles->mVideoEditorExportProfiles.add(
876 new ExportVideoProfile(VIDEO_ENCODER_H264,
877 OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel13));
878}
Hong Tengcabd5f82011-07-06 18:33:09 -0700879
James Dong1d7491b2010-01-19 17:45:38 -0800880/*static*/ MediaProfiles*
881MediaProfiles::createDefaultInstance()
882{
883 MediaProfiles *profiles = new MediaProfiles;
884 createDefaultCamcorderProfiles(profiles);
885 createDefaultVideoEncoders(profiles);
886 createDefaultAudioEncoders(profiles);
887 createDefaultVideoDecoders(profiles);
888 createDefaultAudioDecoders(profiles);
889 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800890 createDefaultImageEncodingQualityLevels(profiles);
Hong Tengcabd5f82011-07-06 18:33:09 -0700891 createDefaultVideoEditorCap(profiles);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700892 createDefaultExportVideoProfiles(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800893 return profiles;
894}
895
896/*static*/ MediaProfiles*
897MediaProfiles::createInstanceFromXmlFile(const char *xml)
898{
899 FILE *fp = NULL;
900 CHECK((fp = fopen(xml, "r")));
901
902 XML_Parser parser = ::XML_ParserCreate(NULL);
903 CHECK(parser != NULL);
904
905 MediaProfiles *profiles = new MediaProfiles();
906 ::XML_SetUserData(parser, profiles);
907 ::XML_SetElementHandler(parser, startElementHandler, NULL);
908
909 /*
910 FIXME:
911 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
912
913 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000914 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800915 return UNKNOWN_ERROR;
916 }
917
918 */
919
920 const int BUFF_SIZE = 512;
921 for (;;) {
922 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
923 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000924 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800925 delete profiles;
926 profiles = NULL;
927 goto exit;
928 }
929
930 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
931 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000932 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800933 delete profiles;
934 profiles = NULL;
935 goto exit;
936 }
937
938 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
939
940 if (bytes_read == 0) break; // done parsing the xml file
941 }
942
943exit:
944 ::XML_ParserFree(parser);
945 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -0800946 return profiles;
947}
948
949Vector<output_format> MediaProfiles::getOutputFileFormats() const
950{
951 return mEncoderOutputFileFormats; // copy out
952}
953
954Vector<video_encoder> MediaProfiles::getVideoEncoders() const
955{
956 Vector<video_encoder> encoders;
957 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
958 encoders.add(mVideoEncoders[i]->mCodec);
959 }
960 return encoders; // copy out
961}
962
963int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
964{
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800966 int index = -1;
967 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
968 if (mVideoEncoders[i]->mCodec == codec) {
969 index = i;
970 break;
971 }
972 }
973 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000974 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800975 return -1;
976 }
977
978 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
979 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
980 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
981 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
982 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
983 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
984 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
985 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
986
Steve Block29357bc2012-01-06 19:20:56 +0000987 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800988 return -1;
989}
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700990int MediaProfiles::getVideoEditorExportParamByName(
991 const char *name, int codec) const
992{
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("getVideoEditorExportParamByName: name %s codec %d", name, codec);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -0700994 ExportVideoProfile *exportProfile = NULL;
995 int index = -1;
996 for (size_t i =0; i < mVideoEditorExportProfiles.size(); i++) {
997 exportProfile = mVideoEditorExportProfiles[i];
998 if (exportProfile->mCodec == codec) {
999 index = i;
1000 break;
1001 }
1002 }
1003 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001004 ALOGE("The given video decoder %d is not found", codec);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -07001005 return -1;
1006 }
1007 if (!strcmp("videoeditor.export.profile", name))
1008 return exportProfile->mProfile;
1009 if (!strcmp("videoeditor.export.level", name))
1010 return exportProfile->mLevel;
James Dong1d7491b2010-01-19 17:45:38 -08001011
Steve Block29357bc2012-01-06 19:20:56 +00001012 ALOGE("The given video editor export param name %s is not found", name);
Rajneesh Chowdury8f74b712011-08-12 16:43:37 -07001013 return -1;
1014}
Hong Tengcabd5f82011-07-06 18:33:09 -07001015int MediaProfiles::getVideoEditorCapParamByName(const char *name) const
1016{
Steve Block3856b092011-10-20 11:56:00 +01001017 ALOGV("getVideoEditorCapParamByName: %s", name);
Hong Tengcabd5f82011-07-06 18:33:09 -07001018
1019 if (mVideoEditorCap == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00001020 ALOGE("The mVideoEditorCap is not created, then create default cap.");
Hong Tengcabd5f82011-07-06 18:33:09 -07001021 createDefaultVideoEditorCap(sInstance);
1022 }
1023
1024 if (!strcmp("videoeditor.input.width.max", name))
1025 return mVideoEditorCap->mMaxInputFrameWidth;
1026 if (!strcmp("videoeditor.input.height.max", name))
1027 return mVideoEditorCap->mMaxInputFrameHeight;
1028 if (!strcmp("videoeditor.output.width.max", name))
1029 return mVideoEditorCap->mMaxOutputFrameWidth;
1030 if (!strcmp("videoeditor.output.height.max", name))
1031 return mVideoEditorCap->mMaxOutputFrameHeight;
Hong Teng3a9cefe2011-11-10 14:54:26 -08001032 if (!strcmp("maxPrefetchYUVFrames", name))
1033 return mVideoEditorCap->mMaxPrefetchYUVFrames;
Hong Tengcabd5f82011-07-06 18:33:09 -07001034
Steve Block29357bc2012-01-06 19:20:56 +00001035 ALOGE("The given video editor param name %s is not found", name);
Hong Tengcabd5f82011-07-06 18:33:09 -07001036 return -1;
1037}
1038
James Dong1d7491b2010-01-19 17:45:38 -08001039Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
1040{
1041 Vector<audio_encoder> encoders;
1042 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1043 encoders.add(mAudioEncoders[i]->mCodec);
1044 }
1045 return encoders; // copy out
1046}
1047
1048int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1049{
Steve Block3856b092011-10-20 11:56:00 +01001050 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001051 int index = -1;
1052 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
1053 if (mAudioEncoders[i]->mCodec == codec) {
1054 index = i;
1055 break;
1056 }
1057 }
1058 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001059 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001060 return -1;
1061 }
1062
1063 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
1064 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
1065 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
1066 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
1067 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
1068 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
1069
Steve Block29357bc2012-01-06 19:20:56 +00001070 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001071 return -1;
1072}
1073
1074Vector<video_decoder> MediaProfiles::getVideoDecoders() const
1075{
1076 Vector<video_decoder> decoders;
1077 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1078 decoders.add(mVideoDecoders[i]->mCodec);
1079 }
1080 return decoders; // copy out
1081}
1082
1083Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1084{
1085 Vector<audio_decoder> decoders;
1086 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1087 decoders.add(mAudioDecoders[i]->mCodec);
1088 }
1089 return decoders; // copy out
1090}
1091
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001092int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001093{
James Dong1d7491b2010-01-19 17:45:38 -08001094 int index = -1;
1095 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001096 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1097 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001098 index = i;
1099 break;
1100 }
1101 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001102 return index;
1103}
1104
1105int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1106 int cameraId,
1107 camcorder_quality quality) const
1108{
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001110 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001111
1112 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001113 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001114 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001115 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001116 return -1;
1117 }
1118
James Dongf5a83852010-02-23 17:21:44 -08001119 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001120 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1121 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1122 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1123 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1124 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1125 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1126 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1127 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1128 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1129 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1130
Steve Block29357bc2012-01-06 19:20:56 +00001131 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001132 return -1;
1133}
1134
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001135bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1136{
1137 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1138}
1139
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001140Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001141{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001142 Vector<int> result;
1143 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1144 if (levels != NULL) {
1145 result = levels->mLevels; // copy out
1146 }
1147 return result;
James Dongf5a83852010-02-23 17:21:44 -08001148}
1149
James Dong0f056292011-05-09 18:49:31 -07001150int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1151 int offsetTimeMs = -1;
1152 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1153 if (index >= 0) {
1154 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1155 }
Steve Block3856b092011-10-20 11:56:00 +01001156 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001157 return offsetTimeMs;
1158}
1159
James Dong1d7491b2010-01-19 17:45:38 -08001160MediaProfiles::~MediaProfiles()
1161{
1162 CHECK("destructor should never be called" == 0);
1163#if 0
1164 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1165 delete mAudioEncoders[i];
1166 }
1167 mAudioEncoders.clear();
1168
1169 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1170 delete mVideoEncoders[i];
1171 }
1172 mVideoEncoders.clear();
1173
1174 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1175 delete mVideoDecoders[i];
1176 }
1177 mVideoDecoders.clear();
1178
1179 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1180 delete mAudioDecoders[i];
1181 }
1182 mAudioDecoders.clear();
1183
1184 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1185 delete mCamcorderProfiles[i];
1186 }
1187 mCamcorderProfiles.clear();
1188#endif
1189}
1190} // namespace android