use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.
the class VideoTrackTranscoder method setup.
@Override
public void setup() {
mExtractor.selectTrack(mTrackIndex);
try {
mEncoder = MediaCodec.createEncoderByType(mOutputFormat.getString(MediaFormat.KEY_MIME));
} catch (IOException e) {
throw new IllegalStateException(e);
}
mEncoder.configure(mOutputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncoderInputSurfaceWrapper = new InputSurface(mEncoder.createInputSurface());
mEncoderInputSurfaceWrapper.makeCurrent();
mEncoder.start();
mEncoderStarted = true;
mEncoderOutputBuffers = mEncoder.getOutputBuffers();
MediaFormat inputFormat = mExtractor.getTrackFormat(mTrackIndex);
if (inputFormat.containsKey(MediaFormatExtraConstants.KEY_ROTATION_DEGREES)) {
// Decoded video is rotated automatically in Android 5.0 lollipop.
// Turn off here because we don't want to encode rotated one.
// refer: https://android.googlesource.com/platform/frameworks/av/+blame/lollipop-release/media/libstagefright/Utils.cpp
inputFormat.setInteger(MediaFormatExtraConstants.KEY_ROTATION_DEGREES, 0);
}
mDecoderOutputSurfaceWrapper = new OutputSurface();
try {
mDecoder = MediaCodec.createDecoderByType(inputFormat.getString(MediaFormat.KEY_MIME));
} catch (IOException e) {
throw new IllegalStateException(e);
}
mDecoder.configure(inputFormat, mDecoderOutputSurfaceWrapper.getSurface(), null, 0);
mDecoder.start();
mDecoderStarted = true;
mDecoderInputBuffers = mDecoder.getInputBuffers();
}
use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.
the class Android720pFormatStrategy method createVideoOutputFormat.
@Override
public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
int longer, shorter, outWidth, outHeight;
if (width >= height) {
longer = width;
shorter = height;
outWidth = LONGER_LENGTH;
outHeight = SHORTER_LENGTH;
} else {
shorter = width;
longer = height;
outWidth = SHORTER_LENGTH;
outHeight = LONGER_LENGTH;
}
if (longer * 9 != shorter * 16) {
throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
}
if (shorter < SHORTER_LENGTH) {
Log.d(TAG, "This video is less to 720p, pass-through. (" + width + "x" + height + ")");
return null;
}
MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);
// From Nexus 4 Camera in 720p
format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
return format;
}
use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.
the class Android720pFormatStrategy method createAudioOutputFormat.
@Override
public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
if (mAudioBitrate == AUDIO_BITRATE_AS_IS || mAudioChannels == AUDIO_CHANNELS_AS_IS)
return null;
// Use original sample rate, as resampling is not supported yet.
final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
return format;
}
use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.
the class ExportPreset960x540Strategy method createVideoOutputFormat.
@Override
public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
// TODO: detect non-baseline profile and throw exception
int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
MediaFormat outputFormat = MediaFormatPresets.getExportPreset960x540(width, height);
int outWidth = outputFormat.getInteger(MediaFormat.KEY_WIDTH);
int outHeight = outputFormat.getInteger(MediaFormat.KEY_HEIGHT);
Log.d(TAG, String.format("inputFormat: %dx%d => outputFormat: %dx%d", width, height, outWidth, outHeight));
return outputFormat;
}
use of android.media.MediaFormat in project LanSoEditor_common by LanSoSdk.
the class AudioEncodeDecode method decodeAudio.
/**
* 把原始音频流,包括MP3 或aac解码成裸码流保存起来, 小端模式
* 中间可以读取各种音频信息,但没有读取.
* 一般播放采用ffplay -f s16le -ar 44.1k -ac 2 hong222.pcm
* @param srcAudioPath
* @param dstAudioPcmPath
* @return
*/
public static int decodeAudio(String srcAudioPath, String dstAudioPcmPath) {
// int mChannels;
// int mNumSamples;
// int mSampleRate;
// int mNumFrames;
File tmpFile = new File(srcAudioPath);
if (tmpFile.exists() == false)
return -1;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(dstAudioPcmPath);
bos = new BufferedOutputStream(fos);
} catch (IOException ioe) {
Log.e("lansongeditor", ioe.toString());
return -1;
}
MediaExtractor extractor = new MediaExtractor();
MediaFormat format = null;
int i;
try {
extractor.setDataSource(srcAudioPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int numTracks = extractor.getTrackCount();
for (i = 0; i < numTracks; i++) {
format = extractor.getTrackFormat(i);
if (format.getString(MediaFormat.KEY_MIME).startsWith("audio/")) {
extractor.selectTrack(i);
break;
}
}
if (i == numTracks) {
extractor.release();
Log.e("lansongedit", "No audio track found in " + srcAudioPath);
try {
bos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.e("lansongeditor", e1.toString());
return -1;
}
return -1;
}
MediaCodec codec;
try {
codec = MediaCodec.createDecoderByType(format.getString(MediaFormat.KEY_MIME));
codec.configure(format, null, null, 0);
codec.start();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Log.e("lansongedit", "No audio track found in " + srcAudioPath);
if (extractor != null)
extractor.release();
try {
bos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.e("lansongeditor", e1.toString());
return -1;
}
return -1;
}
// size of the output buffer containing decoded samples.
int decodedSamplesSize = 0;
byte[] decodedSamples = null;
ByteBuffer[] inputBuffers = codec.getInputBuffers();
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
int sample_size;
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
long presentation_time;
boolean done_reading = false;
// Set the size of the decoded samples buffer to 1MB (~6sec of a stereo stream at 44.1kHz).
// For longer streams, the buffer size will be increased later on, calculating a rough
// estimate of the total size needed to store all the samples in order to resize the buffer
// only once.
Boolean firstSampleData = true;
while (true) {
// read data from file and feed it to the decoder input buffers.
int inputBufferIndex = codec.dequeueInputBuffer(100);
if (!done_reading && inputBufferIndex >= 0) {
sample_size = extractor.readSampleData(inputBuffers[inputBufferIndex], 0);
if (firstSampleData && format.getString(MediaFormat.KEY_MIME).equals("audio/mp4a-latm") && sample_size == 2) {
// For some reasons on some devices (e.g. the Samsung S3) you should not
// provide the first two bytes of an AAC stream, otherwise the MediaCodec will
// crash. These two bytes do not contain music data but basic info on the
// stream (e.g. channel configuration and sampling frequency), and skipping them
// seems OK with other devices (MediaCodec has already been configured and
// already knows these parameters).
extractor.advance();
} else if (sample_size < 0) {
// All samples have been read.
codec.queueInputBuffer(inputBufferIndex, 0, 0, -1, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
done_reading = true;
} else {
presentation_time = extractor.getSampleTime();
codec.queueInputBuffer(inputBufferIndex, 0, sample_size, presentation_time, 0);
extractor.advance();
}
firstSampleData = false;
}
// Get decoded stream from the decoder output buffers.
int outputBufferIndex = codec.dequeueOutputBuffer(info, 100);
if (outputBufferIndex >= 0 && info.size > 0) {
if (decodedSamplesSize < info.size) {
decodedSamplesSize = info.size;
decodedSamples = new byte[decodedSamplesSize];
}
outputBuffers[outputBufferIndex].get(decodedSamples, 0, info.size);
outputBuffers[outputBufferIndex].clear();
try {
bos.write(decodedSamples);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
codec.releaseOutputBuffer(outputBufferIndex, false);
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = codec.getOutputBuffers();
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// Subsequent data will conform to new format.
// We could check that codec.getOutputFormat(), which is the new output format,
// is what we expect.
}
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
// lower pitch.)
break;
}
}
extractor.release();
extractor = null;
codec.stop();
codec.release();
codec = null;
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException ioe) {
Log.e("lansongeditor", ioe.toString());
return -1;
}
return 0;
}
Aggregations