MediaProfiles.cpp revision 9783ed8f85af3d7c72030098acfed7e1d6638349
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::createDefaultCamcorderTimeLapseHighProfile()
428{
429    MediaProfiles::VideoCodec *videoCodec =
430        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 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 = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
437    profile->mDuration = 60;
438    profile->mVideoCodec = videoCodec;
439    profile->mAudioCodec = audioCodec;
440    return profile;
441}
442
443/*static*/ MediaProfiles::CamcorderProfile*
444MediaProfiles::createDefaultCamcorderTimeLapseLowProfile()
445{
446    MediaProfiles::VideoCodec *videoCodec =
447        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 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 = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
454    profile->mDuration = 60;
455    profile->mVideoCodec = videoCodec;
456    profile->mAudioCodec = audioCodec;
457    return profile;
458}
459
460/*static*/ MediaProfiles::CamcorderProfile*
461MediaProfiles::createDefaultCamcorderHighProfile()
462{
463    MediaProfiles::VideoCodec *videoCodec =
464        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
465
466    AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
467    CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
468    profile->mCameraId = 0;
469    profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
470    profile->mQuality = CAMCORDER_QUALITY_HIGH;
471    profile->mDuration = 60;
472    profile->mVideoCodec = videoCodec;
473    profile->mAudioCodec = audioCodec;
474    return profile;
475}
476
477/*static*/ MediaProfiles::CamcorderProfile*
478MediaProfiles::createDefaultCamcorderLowProfile()
479{
480    MediaProfiles::VideoCodec *videoCodec =
481        new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
482
483    MediaProfiles::AudioCodec *audioCodec =
484        new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
485
486    MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
487    profile->mCameraId = 0;
488    profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
489    profile->mQuality = CAMCORDER_QUALITY_LOW;
490    profile->mDuration = 30;
491    profile->mVideoCodec = videoCodec;
492    profile->mAudioCodec = audioCodec;
493    return profile;
494}
495
496/*static*/ void
497MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
498{
499    profiles->mCamcorderProfiles.add(createDefaultCamcorderTimeLapseHighProfile());
500    profiles->mCamcorderProfiles.add(createDefaultCamcorderTimeLapseLowProfile());
501    profiles->mCamcorderProfiles.add(createDefaultCamcorderHighProfile());
502    profiles->mCamcorderProfiles.add(createDefaultCamcorderLowProfile());
503}
504
505/*static*/ void
506MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
507{
508    profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
509}
510
511/*static*/ void
512MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
513{
514    MediaProfiles::VideoDecoderCap *cap =
515        new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
516
517    profiles->mVideoDecoders.add(cap);
518}
519
520/*static*/ void
521MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
522{
523    MediaProfiles::AudioDecoderCap *cap =
524        new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
525
526    profiles->mAudioDecoders.add(cap);
527}
528
529/*static*/ void
530MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
531{
532    profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
533    profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
534}
535
536/*static*/ MediaProfiles::AudioEncoderCap*
537MediaProfiles::createDefaultAmrNBEncoderCap()
538{
539    return new MediaProfiles::AudioEncoderCap(
540        AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
541}
542
543/*static*/ void
544MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
545{
546    ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
547    levels->mCameraId = 0;
548    levels->mLevels.add(70);
549    levels->mLevels.add(80);
550    levels->mLevels.add(90);
551    profiles->mImageEncodingQualityLevels.add(levels);
552}
553
554/*static*/ MediaProfiles*
555MediaProfiles::createDefaultInstance()
556{
557    MediaProfiles *profiles = new MediaProfiles;
558    createDefaultCamcorderProfiles(profiles);
559    createDefaultVideoEncoders(profiles);
560    createDefaultAudioEncoders(profiles);
561    createDefaultVideoDecoders(profiles);
562    createDefaultAudioDecoders(profiles);
563    createDefaultEncoderOutputFileFormats(profiles);
564    createDefaultImageEncodingQualityLevels(profiles);
565    sIsInitialized = true;
566    return profiles;
567}
568
569/*static*/ MediaProfiles*
570MediaProfiles::createInstanceFromXmlFile(const char *xml)
571{
572    FILE *fp = NULL;
573    CHECK((fp = fopen(xml, "r")));
574
575    XML_Parser parser = ::XML_ParserCreate(NULL);
576    CHECK(parser != NULL);
577
578    MediaProfiles *profiles = new MediaProfiles();
579    ::XML_SetUserData(parser, profiles);
580    ::XML_SetElementHandler(parser, startElementHandler, NULL);
581
582    /*
583      FIXME:
584      expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
585
586      if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
587          LOGE("failed to enable DTD support in the xml file");
588          return UNKNOWN_ERROR;
589      }
590
591    */
592
593    const int BUFF_SIZE = 512;
594    for (;;) {
595        void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
596        if (buff == NULL) {
597            LOGE("failed to in call to XML_GetBuffer()");
598            delete profiles;
599            profiles = NULL;
600            goto exit;
601        }
602
603        int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
604        if (bytes_read < 0) {
605            LOGE("failed in call to read");
606            delete profiles;
607            profiles = NULL;
608            goto exit;
609        }
610
611        CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
612
613        if (bytes_read == 0) break;  // done parsing the xml file
614    }
615
616exit:
617    ::XML_ParserFree(parser);
618    ::fclose(fp);
619    if (profiles) {
620        sIsInitialized = true;
621    }
622    return profiles;
623}
624
625Vector<output_format> MediaProfiles::getOutputFileFormats() const
626{
627    return mEncoderOutputFileFormats;  // copy out
628}
629
630Vector<video_encoder> MediaProfiles::getVideoEncoders() const
631{
632    Vector<video_encoder> encoders;
633    for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
634        encoders.add(mVideoEncoders[i]->mCodec);
635    }
636    return encoders;  // copy out
637}
638
639int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
640{
641    LOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
642    int index = -1;
643    for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
644        if (mVideoEncoders[i]->mCodec == codec) {
645            index = i;
646            break;
647        }
648    }
649    if (index == -1) {
650        LOGE("The given video encoder %d is not found", codec);
651        return -1;
652    }
653
654    if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
655    if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
656    if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
657    if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
658    if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
659    if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
660    if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
661    if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
662
663    LOGE("The given video encoder param name %s is not found", name);
664    return -1;
665}
666
667Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
668{
669    Vector<audio_encoder> encoders;
670    for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
671        encoders.add(mAudioEncoders[i]->mCodec);
672    }
673    return encoders;  // copy out
674}
675
676int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
677{
678    LOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
679    int index = -1;
680    for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
681        if (mAudioEncoders[i]->mCodec == codec) {
682            index = i;
683            break;
684        }
685    }
686    if (index == -1) {
687        LOGE("The given audio encoder %d is not found", codec);
688        return -1;
689    }
690
691    if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
692    if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
693    if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
694    if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
695    if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
696    if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
697
698    LOGE("The given audio encoder param name %s is not found", name);
699    return -1;
700}
701
702Vector<video_decoder> MediaProfiles::getVideoDecoders() const
703{
704    Vector<video_decoder> decoders;
705    for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
706        decoders.add(mVideoDecoders[i]->mCodec);
707    }
708    return decoders;  // copy out
709}
710
711Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
712{
713    Vector<audio_decoder> decoders;
714    for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
715        decoders.add(mAudioDecoders[i]->mCodec);
716    }
717    return decoders;  // copy out
718}
719
720int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
721{
722    int index = -1;
723    for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
724        if (mCamcorderProfiles[i]->mCameraId == cameraId &&
725            mCamcorderProfiles[i]->mQuality == quality) {
726            index = i;
727            break;
728        }
729    }
730    return index;
731}
732
733int MediaProfiles::getCamcorderProfileParamByName(const char *name,
734                                                  int cameraId,
735                                                  camcorder_quality quality) const
736{
737    LOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
738         name, cameraId, quality);
739
740    int index = getCamcorderProfileIndex(cameraId, quality);
741    if (index == -1) {
742        LOGE("The given camcorder profile camera %d quality %d is not found",
743             cameraId, quality);
744        return -1;
745    }
746
747    if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
748    if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
749    if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
750    if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
751    if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
752    if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
753    if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
754    if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
755    if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
756    if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
757    if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
758
759    LOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
760    return -1;
761}
762
763bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
764{
765    return (getCamcorderProfileIndex(cameraId, quality) != -1);
766}
767
768Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
769{
770    Vector<int> result;
771    ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
772    if (levels != NULL) {
773        result = levels->mLevels;  // copy out
774    }
775    return result;
776}
777
778MediaProfiles::~MediaProfiles()
779{
780    CHECK("destructor should never be called" == 0);
781#if 0
782    for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
783        delete mAudioEncoders[i];
784    }
785    mAudioEncoders.clear();
786
787    for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
788        delete mVideoEncoders[i];
789    }
790    mVideoEncoders.clear();
791
792    for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
793        delete mVideoDecoders[i];
794    }
795    mVideoDecoders.clear();
796
797    for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
798        delete mAudioDecoders[i];
799    }
800    mAudioDecoders.clear();
801
802    for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
803        delete mCamcorderProfiles[i];
804    }
805    mCamcorderProfiles.clear();
806#endif
807}
808} // namespace android
809