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