|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Yuya Tanaka |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.ypresto.androidtranscoder.format; |
| 17 | + |
| 18 | +import android.media.MediaCodecInfo; |
| 19 | +import android.media.MediaFormat; |
| 20 | +import android.util.Log; |
| 21 | + |
| 22 | +class Android720pFormatStrategy implements MediaFormatStrategy { |
| 23 | + private static final String TAG = "Android720pFormatStrategy"; |
| 24 | + private static final int LONGER_LENGTH = 1280; |
| 25 | + private static final int SHORTER_LENGTH = 720; |
| 26 | + private static final int DEFAULT_BITRATE = 8000 * 1000; // From Nexus 4 Camera in 720p |
| 27 | + private final int mBitRate; |
| 28 | + |
| 29 | + public Android720pFormatStrategy() { |
| 30 | + mBitRate = DEFAULT_BITRATE; |
| 31 | + } |
| 32 | + |
| 33 | + public Android720pFormatStrategy(int bitRate) { |
| 34 | + mBitRate = bitRate; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) { |
| 39 | + int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH); |
| 40 | + int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT); |
| 41 | + int longer, shorter, outWidth, outHeight; |
| 42 | + if (width >= height) { |
| 43 | + longer = width; |
| 44 | + shorter = height; |
| 45 | + outWidth = LONGER_LENGTH; |
| 46 | + outHeight = SHORTER_LENGTH; |
| 47 | + } else { |
| 48 | + shorter = width; |
| 49 | + longer = height; |
| 50 | + outWidth = SHORTER_LENGTH; |
| 51 | + outHeight = LONGER_LENGTH; |
| 52 | + } |
| 53 | + if (longer * 9 != shorter * 16) { |
| 54 | + throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")"); |
| 55 | + } |
| 56 | + if (shorter <= SHORTER_LENGTH) { |
| 57 | + Log.d(TAG, "This video is less or equal to 720p, pass-through. (" + width + "x" + height + ")"); |
| 58 | + return null; |
| 59 | + } |
| 60 | + MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight); |
| 61 | + // From Nexus 4 Camera in 720p |
| 62 | + format.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate); |
| 63 | + format.setInteger(MediaFormat.KEY_FRAME_RATE, 30); |
| 64 | + format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3); |
| 65 | + format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); |
| 66 | + return format; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) { |
| 71 | + return null; |
| 72 | + } |
| 73 | +} |
0 commit comments