Java tutorial
//package com.java2s; /* * Copyright (C) 2016 The Android Open Source Project * * 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. */ import java.nio.ByteBuffer; public class Main { /** * Returns the number of audio samples represented by the given TrueHD syncframe, or 0 if the * buffer is not the start of a syncframe. * * @param syncframe The bytes from which to read the syncframe. Must be at least {@link * #TRUEHD_SYNCFRAME_PREFIX_LENGTH} bytes long. * @return The number of audio samples represented by the syncframe, or 0 if the buffer doesn't * contain the start of a syncframe. */ public static int parseTrueHdSyncframeAudioSampleCount(byte[] syncframe) { // TODO: Link to specification if available. // The syncword ends 0xBA for TrueHD or 0xBB for MLP. if (syncframe[4] != (byte) 0xF8 || syncframe[5] != (byte) 0x72 || syncframe[6] != (byte) 0x6F || (syncframe[7] & 0xFE) != 0xBA) { return 0; } boolean isMlp = (syncframe[7] & 0xFF) == 0xBB; return 40 << ((syncframe[isMlp ? 9 : 8] >> 4) & 0x07); } /** * Reads the number of audio samples represented by a TrueHD syncframe. The buffer's position is * not modified. * * @param buffer The {@link ByteBuffer} from which to read the syncframe. * @param offset The offset of the start of the syncframe relative to the buffer's position. * @return The number of audio samples represented by the syncframe. */ public static int parseTrueHdSyncframeAudioSampleCount(ByteBuffer buffer, int offset) { // TODO: Link to specification if available. boolean isMlp = (buffer.get(buffer.position() + offset + 7) & 0xFF) == 0xBB; return 40 << ((buffer.get(buffer.position() + offset + (isMlp ? 9 : 8)) >> 4) & 0x07); } }