MediaProfiles.cpp revision d5672bc7162fa49abf9bb5844195887e911aa7ce
1/*
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>
26#include <expat.h>
27#include <media/MediaProfiles.h>
28#include <media/stagefright/MediaDebug.h>
29
30namespace android {
31
32Mutex MediaProfiles::sLock;
33bool MediaProfiles::sIsInitialized = false;
34MediaProfiles *MediaProfiles::sInstance = NULL;
35
36const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
37    {"h263", VIDEO_ENCODER_H263},
38    {"h264", VIDEO_ENCODER_H264},
39    {"m4v",  VIDEO_ENCODER_MPEG_4_SP}
40};
41
42const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
43    {"amrnb", AUDIO_ENCODER_AMR_NB},
44    {"amrwb", AUDIO_ENCODER_AMR_WB},
45    {"aac",   AUDIO_ENCODER_AAC},
46};
47
48const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
49    {"3gp", OUTPUT_FORMAT_THREE_GPP},
50    {"mp4", OUTPUT_FORMAT_MPEG_4}
51};
52
53const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
54    {"wmv", VIDEO_DECODER_WMV}
55};
56
57const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
58    {"wma", AUDIO_DECODER_WMA}
59};
60
61const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
62    {"low", CAMCORDER_QUALITY_LOW},
63    {"high", CAMCORDER_QUALITY_HIGH},
64    {"qcif", CAMCORDER_QUALITY_QCIF},
65    {"cif", CAMCORDER_QUALITY_CIF},
66    {"480p", CAMCORDER_QUALITY_480P},
67    {"720p", CAMCORDER_QUALITY_720P},
68    {"1080p", CAMCORDER_QUALITY_1080P},
69
70    {"timelapselow",  CAMCORDER_QUALITY_TIME_LAPSE_LOW},
71    {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
72    {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
73    {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
74    {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
75    {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
76    {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P}
77};
78
79/*static*/ void
80MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec)
81{
82    LOGV("video codec:");
83    LOGV("codec = %d", codec.mCodec);
84    LOGV("bit rate: %d", codec.mBitRate);
85    LOGV("frame width: %d", codec.mFrameWidth);
86    LOGV("frame height: %d", codec.mFrameHeight);
87    LOGV("frame rate: %d", codec.mFrameRate);
88}
89
90/*static*/ void
91MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec)
92{
93    LOGV("audio codec:");
94    LOGV("codec = %d", codec.mCodec);
95    LOGV("bit rate: %d", codec.mBitRate);
96    LOGV("sample rate: %d", codec.mSampleRate);
97    LOGV("number of channels: %d", codec.mChannels);
98}
99
100/*static*/ void
101MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap)
102{
103    LOGV("video encoder cap:");
104    LOGV("codec = %d", cap.mCodec);
105    LOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
106    LOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
107    LOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
108    LOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
109}
110
111/*static*/ void
112MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap)
113{
114    LOGV("audio encoder cap:");
115    LOGV("codec = %d", cap.mCodec);
116    LOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
117    LOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
118    LOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
119}
120
121/*static*/ void
122MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap)
123{
124    LOGV("video decoder cap:");
125    LOGV("codec = %d", cap.mCodec);
126}
127
128/*static*/ void
129MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap)
130{
131    LOGV("audio codec cap:");
132    LOGV("codec = %d", cap.mCodec);
133}
134
135/*static*/ int
136MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings, const char *name)
137{
138    int tag = -1;
139    for (size_t i = 0; i < nMappings; ++i) {
140        if (!strcmp(map[i].name, name)) {
141            tag = map[i].tag;
142            break;
143        }
144    }
145    return tag;
146}
147
148/*static*/ MediaProfiles::VideoCodec*
149MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
150{
151    CHECK(!strcmp("codec",     atts[0]) &&
152          !strcmp("bitRate",   atts[2]) &&
153          !strcmp("width",     atts[4]) &&
154          !strcmp("height",    atts[6]) &&
155          !strcmp("frameRate", atts[8]));
156
157    const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
158    const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
159    CHECK(codec != -1);
160
161    MediaProfiles::VideoCodec *videoCodec =
162        new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
163            atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
164    logVideoCodec(*videoCodec);
165
166    size_t nCamcorderProfiles;
167    CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
168    profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
169    return videoCodec;
170}
171
172/*static*/ MediaProfiles::AudioCodec*
173MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
174{
175    CHECK(!strcmp("codec",      atts[0]) &&
176          !strcmp("bitRate",    atts[2]) &&
177          !strcmp("sampleRate", atts[4]) &&
178          !strcmp("channels",   atts[6]));
179    const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
180    const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
181    CHECK(codec != -1);
182
183    MediaProfiles::AudioCodec *audioCodec =
184        new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
185            atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
186    logAudioCodec(*audioCodec);
187
188    size_t nCamcorderProfiles;
189    CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
190    profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
191    return audioCodec;
192}
193/*static*/ MediaProfiles::AudioDecoderCap*
194MediaProfiles::createAudioDecoderCap(const char **atts)
195{
196    CHECK(!strcmp("name",    atts[0]) &&
197          !strcmp("enabled", atts[2]));
198
199    const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
200    const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
201    CHECK(codec != -1);
202
203    MediaProfiles::AudioDecoderCap *cap =
204        new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
205    logAudioDecoderCap(*cap);
206    return cap;
207}
208
209/*static*/ MediaProfiles::VideoDecoderCap*
210MediaProfiles::createVideoDecoderCap(const char **atts)
211{
212    CHECK(!strcmp("name",    atts[0]) &&
213          !strcmp("enabled", atts[2]));
214
215    const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
216    const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
217    CHECK(codec != -1);
218
219    MediaProfiles::VideoDecoderCap *cap =
220        new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
221    logVideoDecoderCap(*cap);
222    return cap;
223}
224
225/*static*/ MediaProfiles::VideoEncoderCap*
226MediaProfiles::createVideoEncoderCap(const char **atts)
227{
228    CHECK(!strcmp("name",           atts[0])  &&
229          !strcmp("enabled",        atts[2])  &&
230          !strcmp("minBitRate",     atts[4])  &&
231          !strcmp("maxBitRate",     atts[6])  &&
232          !strcmp("minFrameWidth",  atts[8])  &&
233          !strcmp("maxFrameWidth",  atts[10]) &&
234          !strcmp("minFrameHeight", atts[12]) &&
235          !strcmp("maxFrameHeight", atts[14]) &&
236          !strcmp("minFrameRate",   atts[16]) &&
237          !strcmp("maxFrameRate",   atts[18]));
238
239    const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
240    const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
241    CHECK(codec != -1);
242
243    MediaProfiles::VideoEncoderCap *cap =
244        new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
245            atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
246            atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
247    logVideoEncoderCap(*cap);
248    return cap;
249}
250
251/*static*/ MediaProfiles::AudioEncoderCap*
252MediaProfiles::createAudioEncoderCap(const char **atts)
253{
254    CHECK(!strcmp("name",          atts[0])  &&
255          !strcmp("enabled",       atts[2])  &&
256          !strcmp("minBitRate",    atts[4])  &&
257          !strcmp("maxBitRate",    atts[6])  &&
258          !strcmp("minSampleRate", atts[8])  &&
259          !strcmp("maxSampleRate", atts[10]) &&
260          !strcmp("minChannels",   atts[12]) &&
261          !strcmp("maxChannels",   atts[14]));
262
263    const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
264    const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
265    CHECK(codec != -1);
266
267    MediaProfiles::AudioEncoderCap *cap =
268        new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]), atoi(atts[7]),
269            atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
270            atoi(atts[15]));
271    logAudioEncoderCap(*cap);
272    return cap;
273}
274
275/*static*/ output_format
276MediaProfiles::createEncoderOutputFileFormat(const char **atts)
277{
278    CHECK(!strcmp("name", atts[0]));
279
280    const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
281    const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
282    CHECK(format != -1);
283
284    return static_cast<output_format>(format);
285}
286
287/*static*/ MediaProfiles::CamcorderProfile*
288MediaProfiles::createCamcorderProfile(int cameraId, const char **atts)
289{
290    CHECK(!strcmp("quality",    atts[0]) &&
291          !strcmp("fileFormat", atts[2]) &&
292          !strcmp("duration",   atts[4]));
293
294    const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/sizeof(sCamcorderQualityNameMap[0]);
295    const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
296    CHECK(quality != -1);
297
298    const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
299    const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
300    CHECK(fileFormat != -1);
301
302    MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
303    profile->mCameraId = cameraId;
304    profile->mFileFormat = static_cast<output_format>(fileFormat);
305    profile->mQuality = static_cast<camcorder_quality>(quality);
306    profile->mDuration = atoi(atts[5]);
307    return profile;
308}
309
310MediaProfiles::ImageEncodingQualityLevels*
311MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
312{
313    int n = mImageEncodingQualityLevels.size();
314    for (int i = 0; i < n; i++) {
315        ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
316        if (levels->mCameraId == cameraId) {
317            return levels;
318        }
319    }
320    return NULL;
321}
322
323void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
324{
325    CHECK(!strcmp("quality", atts[0]));
326    int quality = atoi(atts[1]);
327    LOGV("%s: cameraId=%d, quality=%d\n", __func__, cameraId, quality);
328    ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
329
330    if (levels == NULL) {
331        levels = new ImageEncodingQualityLevels();
332        levels->mCameraId = cameraId;
333        mImageEncodingQualityLevels.add(levels);
334    }
335
336    levels->mLevels.add(quality);
337}
338
339/*static*/ int
340MediaProfiles::getCameraId(const char** atts)
341{
342    if (!atts[0]) return 0;  // default cameraId = 0
343    CHECK(!strcmp("cameraId", atts[0]));
344    return atoi(atts[1]);
345}
346
347/*static*/ void
348MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
349{
350    MediaProfiles *profiles = (MediaProfiles *) userData;
351    if (strcmp("Video", name) == 0) {
352        createVideoCodec(atts, profiles);
353    } else if (strcmp("Audio", name) == 0) {
354        createAudioCodec(atts, profiles);
355    } else if (strcmp("VideoEncoderCap", name) == 0 &&
356               strcmp("true", atts[3]) == 0) {
357        profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
358    } else if (strcmp("AudioEncoderCap", name) == 0 &&
359               strcmp("true", atts[3]) == 0) {
360        profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
361    } else if (strcmp("VideoDecoderCap", name) == 0 &&
362               strcmp("true", atts[3]) == 0) {
363        profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
364    } else if (strcmp("AudioDecoderCap", name) == 0 &&
365               strcmp("true", atts[3]) == 0) {
366        profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
367    } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
368        profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
369    } else if (strcmp("CamcorderProfiles", name) == 0) {
370        profiles->mCurrentCameraId = getCameraId(atts);
371    } else if (strcmp("EncoderProfile", name) == 0) {
372        profiles->mCamcorderProfiles.add(
373            createCamcorderProfile(profiles->mCurrentCameraId, atts));
374    } else if (strcmp("ImageEncoding", name) == 0) {
375        profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
376    }
377}
378
379/*static*/ MediaProfiles*
380MediaProfiles::getInstance()
381{
382    LOGV("getInstance");
383    Mutex::Autolock lock(sLock);
384    if (!sIsInitialized) {
385        char value[PROPERTY_VALUE_MAX];
386        if (property_get("media.settings.xml", value, NULL) <= 0) {
387            const char *defaultXmlFile = "/etc/media_profiles.xml";
388            FILE *fp = fopen(defaultXmlFile, "r");
389            if (fp == NULL) {
390                LOGW("could not find media config xml file");
391                sInstance = createDefaultInstance();
392            } else {
393                fclose(fp);  // close the file first.
394                sInstance = createInstanceFromXmlFile(defaultXmlFile);
395            }
396        } else {
397            sInstance = createInstanceFromXmlFile(value);
398        }
399    }
400
401    return sInstance;
402}
403
404/*static*/ MediaProfiles::VideoEncoderCap*
405MediaProfiles::createDefaultH263VideoEncoderCap()
406{
407    return new MediaProfiles::VideoEncoderCap(
408        VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
409}
410
411/*static*/ MediaProfiles::VideoEncoderCap*
412MediaProfiles::createDefaultM4vVideoEncoderCap()
413{
414    return new MediaProfiles::VideoEncoderCap(
415        VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
416}
417
418
419/*static*/ void
420MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
421{
422    profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
423    profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
424}
425
426/*static*/ MediaProfiles::CamcorderProfile*
427MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
428{
429    MediaProfiles::VideoCodec *videoCodec =
430        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
431
432    AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
433    CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
434    profile->mCameraId = 0;
435    profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
436    profile->mQuality = quality;
437    profile->mDuration = 60;
438    profile->mVideoCodec = videoCodec;
439    profile->mAudioCodec = audioCodec;
440    return profile;
441}
442
443/*static*/ MediaProfiles::CamcorderProfile*
444MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
445{
446    MediaProfiles::VideoCodec *videoCodec =
447        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
448
449    AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
450    CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
451    profile->mCameraId = 0;
452    profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
453    profile->mQuality = quality;
454    profile->mDuration = 60;
455    profile->mVideoCodec = videoCodec;
456    profile->mAudioCodec = audioCodec;
457    return profile;
458}
459
460/*static*/ void
461MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
462        MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
463        MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
464    *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(CAMCORDER_QUALITY_TIME_LAPSE_LOW);
465    *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
466}
467
468/*static*/ void
469MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
470        MediaProfiles::CamcorderProfile **highTimeLapseProfile,
471        MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
472    *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
473    *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(CAMCORDER_QUALITY_TIME_LAPSE_480P);
474}
475
476/*static*/ MediaProfiles::CamcorderProfile*
477MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
478{
479    MediaProfiles::VideoCodec *videoCodec =
480        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
481
482    MediaProfiles::AudioCodec *audioCodec =
483        new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
484
485    MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
486    profile->mCameraId = 0;
487    profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
488    profile->mQuality = quality;
489    profile->mDuration = 30;
490    profile->mVideoCodec = videoCodec;
491    profile->mAudioCodec = audioCodec;
492    return profile;
493}
494
495/*static*/ MediaProfiles::CamcorderProfile*
496MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
497{
498    MediaProfiles::VideoCodec *videoCodec =
499        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
500
501    AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
502    CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
503    profile->mCameraId = 0;
504    profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
505    profile->mQuality = quality;
506    profile->mDuration = 60;
507    profile->mVideoCodec = videoCodec;
508    profile->mAudioCodec = audioCodec;
509    return profile;
510}
511
512/*static*/ void
513MediaProfiles::createDefaultCamcorderLowProfiles(
514        MediaProfiles::CamcorderProfile **lowProfile,
515        MediaProfiles::CamcorderProfile **lowSpecificProfile) {
516    *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
517    *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
518}
519
520/*static*/ void
521MediaProfiles::createDefaultCamcorderHighProfiles(
522        MediaProfiles::CamcorderProfile **highProfile,
523        MediaProfiles::CamcorderProfile **highSpecificProfile) {
524    *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
525    *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
526}
527
528/*static*/ void
529MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
530{
531    // low camcorder profiles.
532    MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
533    createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
534    profiles->mCamcorderProfiles.add(lowProfile);
535    profiles->mCamcorderProfiles.add(lowSpecificProfile);
536
537    // high camcorder profiles.
538    MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
539    createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
540    profiles->mCamcorderProfiles.add(highProfile);
541    profiles->mCamcorderProfiles.add(highSpecificProfile);
542
543    // low camcorder time lapse profiles.
544    MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
545    createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
546    profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
547    profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
548
549    // high camcorder time lapse profiles.
550    MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
551    createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile, &highSpecificTimeLapseProfile);
552    profiles->mCamcorderProfiles.add(highTimeLapseProfile);
553    profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
554}
555
556/*static*/ void
557MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
558{
559    profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
560}
561
562/*static*/ void
563MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
564{
565    MediaProfiles::VideoDecoderCap *cap =
566        new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
567
568    profiles->mVideoDecoders.add(cap);
569}
570
571/*static*/ void
572MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
573{
574    MediaProfiles::AudioDecoderCap *cap =
575        new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
576
577    profiles->mAudioDecoders.add(cap);
578}
579
580/*static*/ void
581MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
582{
583    profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
584    profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
585}
586
587/*static*/ MediaProfiles::AudioEncoderCap*
588MediaProfiles::createDefaultAmrNBEncoderCap()
589{
590    return new MediaProfiles::AudioEncoderCap(
591        AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
592}
593
594/*static*/ void
595MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
596{
597    ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
598    levels->mCameraId = 0;
599    levels->mLevels.add(70);
600    levels->mLevels.add(80);
601    levels->mLevels.add(90);
602    profiles->mImageEncodingQualityLevels.add(levels);
603}
604
605/*static*/ MediaProfiles*
606MediaProfiles::createDefaultInstance()
607{
608    MediaProfiles *profiles = new MediaProfiles;
609    createDefaultCamcorderProfiles(profiles);
610    createDefaultVideoEncoders(profiles);
611    createDefaultAudioEncoders(profiles);
612    createDefaultVideoDecoders(profiles);
613    createDefaultAudioDecoders(profiles);
614    createDefaultEncoderOutputFileFormats(profiles);
615    createDefaultImageEncodingQualityLevels(profiles);
616    sIsInitialized = true;
617    return profiles;
618}
619
620/*static*/ MediaProfiles*
621MediaProfiles::createInstanceFromXmlFile(const char *xml)
622{
623    FILE *fp = NULL;
624    CHECK((fp = fopen(xml, "r")));
625
626    XML_Parser parser = ::XML_ParserCreate(NULL);
627    CHECK(parser != NULL);
628
629    MediaProfiles *profiles = new MediaProfiles();
630    ::XML_SetUserData(parser, profiles);
631    ::XML_SetElementHandler(parser, startElementHandler, NULL);
632
633    /*
634      FIXME:
635      expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
636
637      if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
638          LOGE("failed to enable DTD support in the xml file");
639          return UNKNOWN_ERROR;
640      }
641
642    */
643
644    const int BUFF_SIZE = 512;
645    for (;;) {
646        void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
647        if (buff == NULL) {
648            LOGE("failed to in call to XML_GetBuffer()");
649            delete profiles;
650            profiles = NULL;
651            goto exit;
652        }
653
654        int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
655        if (bytes_read < 0) {
656            LOGE("failed in call to read");
657            delete profiles;
658            profiles = NULL;
659            goto exit;
660        }
661
662        CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
663
664        if (bytes_read == 0) break;  // done parsing the xml file
665    }
666
667exit:
668    ::XML_ParserFree(parser);
669    ::fclose(fp);
670    if (profiles) {
671        sIsInitialized = true;
672    }
673    return profiles;
674}
675
676Vector<output_format> MediaProfiles::getOutputFileFormats() const
677{
678    return mEncoderOutputFileFormats;  // copy out
679}
680
681Vector<video_encoder> MediaProfiles::getVideoEncoders() const
682{
683    Vector<video_encoder> encoders;
684    for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
685        encoders.add(mVideoEncoders[i]->mCodec);
686    }
687    return encoders;  // copy out
688}
689
690int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
691{
692    LOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
693    int index = -1;
694    for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
695        if (mVideoEncoders[i]->mCodec == codec) {
696            index = i;
697            break;
698        }
699    }
700    if (index == -1) {
701        LOGE("The given video encoder %d is not found", codec);
702        return -1;
703    }
704
705    if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
706    if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
707    if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
708    if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
709    if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
710    if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
711    if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
712    if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
713
714    LOGE("The given video encoder param name %s is not found", name);
715    return -1;
716}
717
718Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
719{
720    Vector<audio_encoder> encoders;
721    for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
722        encoders.add(mAudioEncoders[i]->mCodec);
723    }
724    return encoders;  // copy out
725}
726
727int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
728{
729    LOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
730    int index = -1;
731    for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
732        if (mAudioEncoders[i]->mCodec == codec) {
733            index = i;
734            break;
735        }
736    }
737    if (index == -1) {
738        LOGE("The given audio encoder %d is not found", codec);
739        return -1;
740    }
741
742    if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
743    if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
744    if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
745    if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
746    if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
747    if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
748
749    LOGE("The given audio encoder param name %s is not found", name);
750    return -1;
751}
752
753Vector<video_decoder> MediaProfiles::getVideoDecoders() const
754{
755    Vector<video_decoder> decoders;
756    for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
757        decoders.add(mVideoDecoders[i]->mCodec);
758    }
759    return decoders;  // copy out
760}
761
762Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
763{
764    Vector<audio_decoder> decoders;
765    for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
766        decoders.add(mAudioDecoders[i]->mCodec);
767    }
768    return decoders;  // copy out
769}
770
771int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
772{
773    int index = -1;
774    for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
775        if (mCamcorderProfiles[i]->mCameraId == cameraId &&
776            mCamcorderProfiles[i]->mQuality == quality) {
777            index = i;
778            break;
779        }
780    }
781    return index;
782}
783
784int MediaProfiles::getCamcorderProfileParamByName(const char *name,
785                                                  int cameraId,
786                                                  camcorder_quality quality) const
787{
788    LOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
789         name, cameraId, quality);
790
791    int index = getCamcorderProfileIndex(cameraId, quality);
792    if (index == -1) {
793        LOGE("The given camcorder profile camera %d quality %d is not found",
794             cameraId, quality);
795        return -1;
796    }
797
798    if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
799    if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
800    if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
801    if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
802    if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
803    if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
804    if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
805    if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
806    if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
807    if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
808    if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
809
810    LOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
811    return -1;
812}
813
814bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
815{
816    return (getCamcorderProfileIndex(cameraId, quality) != -1);
817}
818
819Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
820{
821    Vector<int> result;
822    ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
823    if (levels != NULL) {
824        result = levels->mLevels;  // copy out
825    }
826    return result;
827}
828
829MediaProfiles::~MediaProfiles()
830{
831    CHECK("destructor should never be called" == 0);
832#if 0
833    for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
834        delete mAudioEncoders[i];
835    }
836    mAudioEncoders.clear();
837
838    for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
839        delete mVideoEncoders[i];
840    }
841    mVideoEncoders.clear();
842
843    for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
844        delete mVideoDecoders[i];
845    }
846    mVideoDecoders.clear();
847
848    for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
849        delete mAudioDecoders[i];
850    }
851    mAudioDecoders.clear();
852
853    for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
854        delete mCamcorderProfiles[i];
855    }
856    mCamcorderProfiles.clear();
857#endif
858}
859} // namespace android
860