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