diff options
Diffstat (limited to 'libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils')
3 files changed, 158 insertions, 0 deletions
diff --git a/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/AvcCsdUtils.java b/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/AvcCsdUtils.java new file mode 100644 index 000000000..049296b9b --- /dev/null +++ b/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/AvcCsdUtils.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2015 Yuya Tanaka + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.ypresto.androidtranscoder.utils; + +import android.media.MediaFormat; + +import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants; + +import java.nio.ByteBuffer; +import java.util.Arrays; + +public class AvcCsdUtils { + // Refer: https://android.googlesource.com/platform/frameworks/av/+/lollipop-release/media/libstagefright/MediaCodec.cpp#2198 + // Refer: http://stackoverflow.com/a/2861340 + private static final byte[] AVC_START_CODE_3 = {0x00, 0x00, 0x01}; + private static final byte[] AVC_START_CODE_4 = {0x00, 0x00, 0x00, 0x01}; + // Refer: http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/ + private static final byte AVC_SPS_NAL = 103; // 0<<7 + 3<<5 + 7<<0 + // https://tools.ietf.org/html/rfc6184 + private static final byte AVC_SPS_NAL_2 = 39; // 0<<7 + 1<<5 + 7<<0 + private static final byte AVC_SPS_NAL_3 = 71; // 0<<7 + 2<<5 + 7<<0 + + /** + * @return ByteBuffer contains SPS without NAL header. + */ + public static ByteBuffer getSpsBuffer(MediaFormat format) { + ByteBuffer sourceBuffer = format.getByteBuffer(MediaFormatExtraConstants.KEY_AVC_SPS).asReadOnlyBuffer(); // might be direct buffer + ByteBuffer prefixedSpsBuffer = ByteBuffer.allocate(sourceBuffer.limit()).order(sourceBuffer.order()); + prefixedSpsBuffer.put(sourceBuffer); + prefixedSpsBuffer.flip(); + + skipStartCode(prefixedSpsBuffer); + + byte spsNalData = prefixedSpsBuffer.get(); + if (spsNalData != AVC_SPS_NAL && spsNalData != AVC_SPS_NAL_2 && spsNalData != AVC_SPS_NAL_3) { + throw new IllegalStateException("Got non SPS NAL data."); + } + + return prefixedSpsBuffer.slice(); + } + + private static void skipStartCode(ByteBuffer prefixedSpsBuffer) { + byte[] prefix3 = new byte[3]; + prefixedSpsBuffer.get(prefix3); + if (Arrays.equals(prefix3, AVC_START_CODE_3)) return; + + byte[] prefix4 = Arrays.copyOf(prefix3, 4); + prefix4[3] = prefixedSpsBuffer.get(); + if (Arrays.equals(prefix4, AVC_START_CODE_4)) return; + throw new IllegalStateException("AVC NAL start code does not found in csd."); + } + + private AvcCsdUtils() { + throw new RuntimeException(); + } +} diff --git a/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/AvcSpsUtils.java b/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/AvcSpsUtils.java new file mode 100644 index 000000000..3eafbed64 --- /dev/null +++ b/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/AvcSpsUtils.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2016 Yuya Tanaka + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.ypresto.androidtranscoder.utils; + +import java.nio.ByteBuffer; + +public class AvcSpsUtils { + public static byte getProfileIdc(ByteBuffer spsBuffer) { + // Refer: http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/ + // First byte after NAL. + return spsBuffer.get(0); + } +} diff --git a/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/MediaExtractorUtils.java b/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/MediaExtractorUtils.java new file mode 100644 index 000000000..b973d2f16 --- /dev/null +++ b/libs/android-transcoder/src/main/java/net/ypresto/androidtranscoder/utils/MediaExtractorUtils.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2014 Yuya Tanaka + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.ypresto.androidtranscoder.utils; + +import android.media.MediaExtractor; +import android.media.MediaFormat; + +public class MediaExtractorUtils { + + private MediaExtractorUtils() { + } + + public static class TrackResult { + + private TrackResult() { + } + + public int mVideoTrackIndex; + public String mVideoTrackMime; + public MediaFormat mVideoTrackFormat; + public int mAudioTrackIndex; + public String mAudioTrackMime; + public MediaFormat mAudioTrackFormat; + } + + public static TrackResult getFirstVideoAndAudioTrack(MediaExtractor extractor) { + TrackResult trackResult = new TrackResult(); + trackResult.mVideoTrackIndex = -1; + trackResult.mAudioTrackIndex = -1; + int trackCount = extractor.getTrackCount(); + for (int i = 0; i < trackCount; i++) { + MediaFormat format = extractor.getTrackFormat(i); + String mime = format.getString(MediaFormat.KEY_MIME); + if (trackResult.mVideoTrackIndex < 0 && mime.startsWith("video/")) { + trackResult.mVideoTrackIndex = i; + trackResult.mVideoTrackMime = mime; + trackResult.mVideoTrackFormat = format; + } else if (trackResult.mAudioTrackIndex < 0 && mime.startsWith("audio/")) { + trackResult.mAudioTrackIndex = i; + trackResult.mAudioTrackMime = mime; + trackResult.mAudioTrackFormat = format; + } + if (trackResult.mVideoTrackIndex >= 0 && trackResult.mAudioTrackIndex >= 0) break; + } + if (trackResult.mVideoTrackIndex < 0 || trackResult.mAudioTrackIndex < 0) { + throw new IllegalArgumentException("extractor does not contain video and/or audio tracks."); + } + return trackResult; + } +} |