MediaProfiles.h revision 8f74b712fbf5c24de8bc799b7129dc802747a6f4
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#ifndef ANDROID_MEDIAPROFILES_H
19#define ANDROID_MEDIAPROFILES_H
20
21#include <utils/threads.h>
22#include <media/mediarecorder.h>
23
24namespace android {
25
26enum camcorder_quality {
27    CAMCORDER_QUALITY_LIST_START = 0,
28    CAMCORDER_QUALITY_LOW  = 0,
29    CAMCORDER_QUALITY_HIGH = 1,
30    CAMCORDER_QUALITY_QCIF = 2,
31    CAMCORDER_QUALITY_CIF = 3,
32    CAMCORDER_QUALITY_480P = 4,
33    CAMCORDER_QUALITY_720P = 5,
34    CAMCORDER_QUALITY_1080P = 6,
35    CAMCORDER_QUALITY_LIST_END = 6,
36
37    CAMCORDER_QUALITY_TIME_LAPSE_LIST_START = 1000,
38    CAMCORDER_QUALITY_TIME_LAPSE_LOW  = 1000,
39    CAMCORDER_QUALITY_TIME_LAPSE_HIGH = 1001,
40    CAMCORDER_QUALITY_TIME_LAPSE_QCIF = 1002,
41    CAMCORDER_QUALITY_TIME_LAPSE_CIF = 1003,
42    CAMCORDER_QUALITY_TIME_LAPSE_480P = 1004,
43    CAMCORDER_QUALITY_TIME_LAPSE_720P = 1005,
44    CAMCORDER_QUALITY_TIME_LAPSE_1080P = 1006,
45    CAMCORDER_QUALITY_TIME_LAPSE_LIST_END = 1006,
46};
47
48/**
49 *Set CIF as default maximum import and export resolution of video editor.
50 *The maximum import and export resolutions are platform specific,
51 *which should be defined in media_profiles.xml.
52 */
53enum videoeditor_capability {
54    VIDEOEDITOR_DEFAULT_MAX_INPUT_FRAME_WIDTH = 352,
55    VIDEOEDITOR_DEFUALT_MAX_INPUT_FRAME_HEIGHT = 288,
56    VIDEOEDITOR_DEFAULT_MAX_OUTPUT_FRAME_WIDTH = 352,
57    VIDEOEDITOR_DEFUALT_MAX_OUTPUT_FRAME_HEIGHT = 288,
58};
59
60enum video_decoder {
61    VIDEO_DECODER_WMV,
62};
63
64enum audio_decoder {
65    AUDIO_DECODER_WMA,
66};
67
68
69class MediaProfiles
70{
71public:
72
73    /**
74     * Returns the singleton instance for subsequence queries.
75     * or NULL if error.
76     */
77    static MediaProfiles* getInstance();
78
79    /**
80     * Returns the value for the given param name for the given camera at
81     * the given quality level, or -1 if error.
82     *
83     * Supported param name are:
84     * duration - the recording duration.
85     * file.format - output file format. see mediarecorder.h for details
86     * vid.codec - video encoder. see mediarecorder.h for details.
87     * aud.codec - audio encoder. see mediarecorder.h for details.
88     * vid.width - video frame width
89     * vid.height - video frame height
90     * vid.fps - video frame rate
91     * vid.bps - video bit rate
92     * aud.bps - audio bit rate
93     * aud.hz - audio sample rate
94     * aud.ch - number of audio channels
95     */
96    int getCamcorderProfileParamByName(const char *name, int cameraId,
97                                       camcorder_quality quality) const;
98
99    /**
100     * Returns true if a profile for the given camera at the given quality exists,
101     * or false if not.
102     */
103    bool hasCamcorderProfile(int cameraId, camcorder_quality quality) const;
104
105    /**
106     * Returns the output file formats supported.
107     */
108    Vector<output_format> getOutputFileFormats() const;
109
110    /**
111     * Returns the video encoders supported.
112     */
113    Vector<video_encoder> getVideoEncoders() const;
114
115    /**
116     * Returns the value for the given param name for the given video encoder
117     * returned from getVideoEncoderByIndex or -1 if error.
118     *
119     * Supported param name are:
120     * enc.vid.width.min - min video frame width
121     * enc.vid.width.max - max video frame width
122     * enc.vid.height.min - min video frame height
123     * enc.vid.height.max - max video frame height
124     * enc.vid.bps.min - min bit rate in bits per second
125     * enc.vid.bps.max - max bit rate in bits per second
126     * enc.vid.fps.min - min frame rate in frames per second
127     * enc.vid.fps.max - max frame rate in frames per second
128     */
129    int getVideoEncoderParamByName(const char *name, video_encoder codec) const;
130
131    /**
132     * Returns the value for the given param name for the video editor cap
133     * param or -1 if error.
134     * Supported param name are:
135     * videoeditor.input.width.max - max input video frame width
136     * videoeditor.input.height.max - max input video frame height
137     * videoeditor.output.width.max - max output video frame width
138     * videoeditor.output.height.max - max output video frame height
139     */
140    int getVideoEditorCapParamByName(const char *name) const;
141
142    /**
143     * Returns the value for the given param name for the video editor export codec format
144     * param or -1 if error.
145     * Supported param name are:
146     * videoeditor.export.profile - export video profile
147     * videoeditor.export.level - export video level
148     * Supported param codec are:
149     * 1 for h263
150     * 2 for h264
151     * 3 for mpeg4
152     */
153    int getVideoEditorExportParamByName(const char *name, int codec) const;
154
155    /**
156     * Returns the audio encoders supported.
157     */
158    Vector<audio_encoder> getAudioEncoders() const;
159
160    /**
161     * Returns the value for the given param name for the given audio encoder
162     * returned from getAudioEncoderByIndex or -1 if error.
163     *
164     * Supported param name are:
165     * enc.aud.ch.min - min number of channels
166     * enc.aud.ch.max - max number of channels
167     * enc.aud.bps.min - min bit rate in bits per second
168     * enc.aud.bps.max - max bit rate in bits per second
169     * enc.aud.hz.min - min sample rate in samples per second
170     * enc.aud.hz.max - max sample rate in samples per second
171     */
172    int getAudioEncoderParamByName(const char *name, audio_encoder codec) const;
173
174    /**
175      * Returns the video decoders supported.
176      */
177    Vector<video_decoder> getVideoDecoders() const;
178
179     /**
180      * Returns the audio decoders supported.
181      */
182    Vector<audio_decoder> getAudioDecoders() const;
183
184    /**
185     * Returns the number of image encoding quality levels supported.
186     */
187    Vector<int> getImageEncodingQualityLevels(int cameraId) const;
188
189    /**
190     * Returns the start time offset (in ms) for the given camera Id.
191     * If the given camera Id does not exist, -1 will be returned.
192     */
193    int getStartTimeOffsetMs(int cameraId) const;
194
195private:
196    enum {
197        // Camcorder profiles (high/low) and timelapse profiles (high/low)
198        kNumRequiredProfiles = 4,
199    };
200
201    MediaProfiles& operator=(const MediaProfiles&);  // Don't call me
202    MediaProfiles(const MediaProfiles&);             // Don't call me
203    MediaProfiles() { mVideoEditorCap = NULL; }        // Dummy default constructor
204    ~MediaProfiles();                                // Don't delete me
205
206    struct VideoCodec {
207        VideoCodec(video_encoder codec, int bitRate, int frameWidth, int frameHeight, int frameRate)
208            : mCodec(codec),
209              mBitRate(bitRate),
210              mFrameWidth(frameWidth),
211              mFrameHeight(frameHeight),
212              mFrameRate(frameRate) {}
213
214        VideoCodec(const VideoCodec& copy) {
215            mCodec = copy.mCodec;
216            mBitRate = copy.mBitRate;
217            mFrameWidth = copy.mFrameWidth;
218            mFrameHeight = copy.mFrameHeight;
219            mFrameRate = copy.mFrameRate;
220        }
221
222        ~VideoCodec() {}
223
224        video_encoder mCodec;
225        int mBitRate;
226        int mFrameWidth;
227        int mFrameHeight;
228        int mFrameRate;
229    };
230
231    struct AudioCodec {
232        AudioCodec(audio_encoder codec, int bitRate, int sampleRate, int channels)
233            : mCodec(codec),
234              mBitRate(bitRate),
235              mSampleRate(sampleRate),
236              mChannels(channels) {}
237
238        AudioCodec(const AudioCodec& copy) {
239            mCodec = copy.mCodec;
240            mBitRate = copy.mBitRate;
241            mSampleRate = copy.mSampleRate;
242            mChannels = copy.mChannels;
243        }
244
245        ~AudioCodec() {}
246
247        audio_encoder mCodec;
248        int mBitRate;
249        int mSampleRate;
250        int mChannels;
251    };
252
253    struct CamcorderProfile {
254        CamcorderProfile()
255            : mCameraId(0),
256              mFileFormat(OUTPUT_FORMAT_THREE_GPP),
257              mQuality(CAMCORDER_QUALITY_HIGH),
258              mDuration(0),
259              mVideoCodec(0),
260              mAudioCodec(0) {}
261
262        CamcorderProfile(const CamcorderProfile& copy) {
263            mCameraId = copy.mCameraId;
264            mFileFormat = copy.mFileFormat;
265            mQuality = copy.mQuality;
266            mDuration = copy.mDuration;
267            mVideoCodec = new VideoCodec(*copy.mVideoCodec);
268            mAudioCodec = new AudioCodec(*copy.mAudioCodec);
269        }
270
271        ~CamcorderProfile() {
272            delete mVideoCodec;
273            delete mAudioCodec;
274        }
275
276        int mCameraId;
277        output_format mFileFormat;
278        camcorder_quality mQuality;
279        int mDuration;
280        VideoCodec *mVideoCodec;
281        AudioCodec *mAudioCodec;
282    };
283
284    struct VideoEncoderCap {
285        // Ugly constructor
286        VideoEncoderCap(video_encoder codec,
287                        int minBitRate, int maxBitRate,
288                        int minFrameWidth, int maxFrameWidth,
289                        int minFrameHeight, int maxFrameHeight,
290                        int minFrameRate, int maxFrameRate)
291            : mCodec(codec),
292              mMinBitRate(minBitRate), mMaxBitRate(maxBitRate),
293              mMinFrameWidth(minFrameWidth), mMaxFrameWidth(maxFrameWidth),
294              mMinFrameHeight(minFrameHeight), mMaxFrameHeight(maxFrameHeight),
295              mMinFrameRate(minFrameRate), mMaxFrameRate(maxFrameRate) {}
296
297         ~VideoEncoderCap() {}
298
299        video_encoder mCodec;
300        int mMinBitRate, mMaxBitRate;
301        int mMinFrameWidth, mMaxFrameWidth;
302        int mMinFrameHeight, mMaxFrameHeight;
303        int mMinFrameRate, mMaxFrameRate;
304    };
305
306    struct AudioEncoderCap {
307        // Ugly constructor
308        AudioEncoderCap(audio_encoder codec,
309                        int minBitRate, int maxBitRate,
310                        int minSampleRate, int maxSampleRate,
311                        int minChannels, int maxChannels)
312            : mCodec(codec),
313              mMinBitRate(minBitRate), mMaxBitRate(maxBitRate),
314              mMinSampleRate(minSampleRate), mMaxSampleRate(maxSampleRate),
315              mMinChannels(minChannels), mMaxChannels(maxChannels) {}
316
317        ~AudioEncoderCap() {}
318
319        audio_encoder mCodec;
320        int mMinBitRate, mMaxBitRate;
321        int mMinSampleRate, mMaxSampleRate;
322        int mMinChannels, mMaxChannels;
323    };
324
325    struct VideoDecoderCap {
326        VideoDecoderCap(video_decoder codec): mCodec(codec) {}
327        ~VideoDecoderCap() {}
328
329        video_decoder mCodec;
330    };
331
332    struct AudioDecoderCap {
333        AudioDecoderCap(audio_decoder codec): mCodec(codec) {}
334        ~AudioDecoderCap() {}
335
336        audio_decoder mCodec;
337    };
338
339    struct NameToTagMap {
340        const char* name;
341        int tag;
342    };
343
344    struct ImageEncodingQualityLevels {
345        int mCameraId;
346        Vector<int> mLevels;
347    };
348    struct ExportVideoProfile {
349        ExportVideoProfile(int codec, int profile, int level)
350            :mCodec(codec),mProfile(profile),mLevel(level) {}
351        ~ExportVideoProfile() {}
352        int mCodec;
353        int mProfile;
354        int mLevel;
355    };
356    struct VideoEditorCap {
357        VideoEditorCap(int inFrameWidth, int inFrameHeight,
358            int outFrameWidth, int outFrameHeight)
359            : mMaxInputFrameWidth(inFrameWidth),
360              mMaxInputFrameHeight(inFrameHeight),
361              mMaxOutputFrameWidth(outFrameWidth),
362              mMaxOutputFrameHeight(outFrameHeight) {}
363
364        ~VideoEditorCap() {}
365
366        int mMaxInputFrameWidth;
367        int mMaxInputFrameHeight;
368        int mMaxOutputFrameWidth;
369        int mMaxOutputFrameHeight;
370    };
371
372    int getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const;
373    void initRequiredProfileRefs(const Vector<int>& cameraIds);
374    int getRequiredProfileRefIndex(int cameraId);
375
376    // Debug
377    static void logVideoCodec(const VideoCodec& codec);
378    static void logAudioCodec(const AudioCodec& codec);
379    static void logVideoEncoderCap(const VideoEncoderCap& cap);
380    static void logAudioEncoderCap(const AudioEncoderCap& cap);
381    static void logVideoDecoderCap(const VideoDecoderCap& cap);
382    static void logAudioDecoderCap(const AudioDecoderCap& cap);
383    static void logVideoEditorCap(const VideoEditorCap& cap);
384
385    // If the xml configuration file does exist, use the settings
386    // from the xml
387    static MediaProfiles* createInstanceFromXmlFile(const char *xml);
388    static output_format createEncoderOutputFileFormat(const char **atts);
389    static VideoCodec* createVideoCodec(const char **atts, MediaProfiles *profiles);
390    static AudioCodec* createAudioCodec(const char **atts, MediaProfiles *profiles);
391    static AudioDecoderCap* createAudioDecoderCap(const char **atts);
392    static VideoDecoderCap* createVideoDecoderCap(const char **atts);
393    static VideoEncoderCap* createVideoEncoderCap(const char **atts);
394    static AudioEncoderCap* createAudioEncoderCap(const char **atts);
395    static VideoEditorCap* createVideoEditorCap(
396                const char **atts, MediaProfiles *profiles);
397    static ExportVideoProfile* createExportVideoProfile(const char **atts);
398
399    static CamcorderProfile* createCamcorderProfile(
400                int cameraId, const char **atts, Vector<int>& cameraIds);
401
402    static int getCameraId(const char **atts);
403
404    void addStartTimeOffset(int cameraId, const char **atts);
405
406    ImageEncodingQualityLevels* findImageEncodingQualityLevels(int cameraId) const;
407    void addImageEncodingQualityLevel(int cameraId, const char** atts);
408
409    // Customized element tag handler for parsing the xml configuration file.
410    static void startElementHandler(void *userData, const char *name, const char **atts);
411
412    // If the xml configuration file does not exist, use hard-coded values
413    static MediaProfiles* createDefaultInstance();
414
415    static CamcorderProfile *createDefaultCamcorderQcifProfile(camcorder_quality quality);
416    static CamcorderProfile *createDefaultCamcorderCifProfile(camcorder_quality quality);
417    static void createDefaultCamcorderLowProfiles(
418            MediaProfiles::CamcorderProfile **lowProfile,
419            MediaProfiles::CamcorderProfile **lowSpecificProfile);
420    static void createDefaultCamcorderHighProfiles(
421            MediaProfiles::CamcorderProfile **highProfile,
422            MediaProfiles::CamcorderProfile **highSpecificProfile);
423
424    static CamcorderProfile *createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality);
425    static CamcorderProfile *createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality);
426    static void createDefaultCamcorderTimeLapseLowProfiles(
427            MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
428            MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile);
429    static void createDefaultCamcorderTimeLapseHighProfiles(
430            MediaProfiles::CamcorderProfile **highTimeLapseProfile,
431            MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile);
432
433    static void createDefaultCamcorderProfiles(MediaProfiles *profiles);
434    static void createDefaultVideoEncoders(MediaProfiles *profiles);
435    static void createDefaultAudioEncoders(MediaProfiles *profiles);
436    static void createDefaultVideoDecoders(MediaProfiles *profiles);
437    static void createDefaultAudioDecoders(MediaProfiles *profiles);
438    static void createDefaultEncoderOutputFileFormats(MediaProfiles *profiles);
439    static void createDefaultImageEncodingQualityLevels(MediaProfiles *profiles);
440    static void createDefaultImageDecodingMaxMemory(MediaProfiles *profiles);
441    static void createDefaultVideoEditorCap(MediaProfiles *profiles);
442    static void createDefaultExportVideoProfiles(MediaProfiles *profiles);
443
444    static VideoEncoderCap* createDefaultH263VideoEncoderCap();
445    static VideoEncoderCap* createDefaultM4vVideoEncoderCap();
446    static AudioEncoderCap* createDefaultAmrNBEncoderCap();
447
448    static int findTagForName(const NameToTagMap *map, size_t nMappings, const char *name);
449
450    /**
451     * Check on existing profiles with the following criteria:
452     * 1. Low quality profile must have the lowest video
453     *    resolution product (width x height)
454     * 2. High quality profile must have the highest video
455     *    resolution product (width x height)
456     *
457     * and add required low/high quality camcorder/timelapse
458     * profiles if they are not found. This allows to remove
459     * duplicate profile definitions in the media_profiles.xml
460     * file.
461     */
462    void checkAndAddRequiredProfilesIfNecessary();
463
464
465    // Mappings from name (for instance, codec name) to enum value
466    static const NameToTagMap sVideoEncoderNameMap[];
467    static const NameToTagMap sAudioEncoderNameMap[];
468    static const NameToTagMap sFileFormatMap[];
469    static const NameToTagMap sVideoDecoderNameMap[];
470    static const NameToTagMap sAudioDecoderNameMap[];
471    static const NameToTagMap sCamcorderQualityNameMap[];
472
473    static bool sIsInitialized;
474    static MediaProfiles *sInstance;
475    static Mutex sLock;
476    int mCurrentCameraId;
477
478    Vector<CamcorderProfile*> mCamcorderProfiles;
479    Vector<AudioEncoderCap*>  mAudioEncoders;
480    Vector<VideoEncoderCap*>  mVideoEncoders;
481    Vector<AudioDecoderCap*>  mAudioDecoders;
482    Vector<VideoDecoderCap*>  mVideoDecoders;
483    Vector<output_format>     mEncoderOutputFileFormats;
484    Vector<ImageEncodingQualityLevels *>  mImageEncodingQualityLevels;
485    KeyedVector<int, int> mStartTimeOffsets;
486
487    typedef struct {
488        bool mHasRefProfile;      // Refers to an existing profile
489        int  mRefProfileIndex;    // Reference profile index
490        int  mResolutionProduct;  // width x height
491    } RequiredProfileRefInfo;     // Required low and high profiles
492
493    typedef struct {
494        RequiredProfileRefInfo mRefs[kNumRequiredProfiles];
495        int mCameraId;
496    } RequiredProfiles;
497
498    RequiredProfiles *mRequiredProfileRefs;
499    Vector<int>              mCameraIds;
500    VideoEditorCap* mVideoEditorCap;
501    Vector<ExportVideoProfile*> mVideoEditorExportProfiles;
502};
503
504}; // namespace android
505
506#endif // ANDROID_MEDIAPROFILES_H
507
508