1 /* 2 * Copyright 2008 CoreMedia AG, Hamburg 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 17 package com.coremedia.iso.boxes; 18 19 20 import com.coremedia.iso.IsoTypeReader; 21 import com.coremedia.iso.IsoTypeWriter; 22 import com.googlecode.mp4parser.AbstractFullBox; 23 24 import java.nio.ByteBuffer; 25 26 import static com.googlecode.mp4parser.util.CastUtils.l2i; 27 28 /** 29 * This box containes the sample count and a table giving the size in bytes of each sample. 30 * Defined in ISO/IEC 14496-12. 31 */ 32 public class SampleSizeBox extends AbstractFullBox { 33 private long sampleSize; 34 private long[] sampleSizes = new long[0]; 35 public static final String TYPE = "stsz"; 36 int sampleCount; 37 SampleSizeBox()38 public SampleSizeBox() { 39 super(TYPE); 40 } 41 42 /** 43 * Returns the field sample size. 44 * If sampleSize > 0 every sample has the same size. 45 * If sampleSize == 0 the samples have different size as stated in the sampleSizes field. 46 * 47 * @return the sampleSize field 48 */ getSampleSize()49 public long getSampleSize() { 50 return sampleSize; 51 } 52 setSampleSize(long sampleSize)53 public void setSampleSize(long sampleSize) { 54 this.sampleSize = sampleSize; 55 } 56 57 getSampleSizeAtIndex(int index)58 public long getSampleSizeAtIndex(int index) { 59 if (sampleSize > 0) { 60 return sampleSize; 61 } else { 62 return sampleSizes[index]; 63 } 64 } 65 getSampleCount()66 public long getSampleCount() { 67 if (sampleSize > 0) { 68 return sampleCount; 69 } else { 70 return sampleSizes.length; 71 } 72 73 } 74 getSampleSizes()75 public long[] getSampleSizes() { 76 return sampleSizes; 77 } 78 setSampleSizes(long[] sampleSizes)79 public void setSampleSizes(long[] sampleSizes) { 80 this.sampleSizes = sampleSizes; 81 } 82 getContentSize()83 protected long getContentSize() { 84 return 12 + (sampleSize == 0 ? sampleSizes.length * 4 : 0); 85 } 86 87 @Override _parseDetails(ByteBuffer content)88 public void _parseDetails(ByteBuffer content) { 89 parseVersionAndFlags(content); 90 sampleSize = IsoTypeReader.readUInt32(content); 91 sampleCount = l2i(IsoTypeReader.readUInt32(content)); 92 93 if (sampleSize == 0) { 94 sampleSizes = new long[(int) sampleCount]; 95 96 for (int i = 0; i < sampleCount; i++) { 97 sampleSizes[i] = IsoTypeReader.readUInt32(content); 98 } 99 } 100 } 101 102 @Override getContent(ByteBuffer byteBuffer)103 protected void getContent(ByteBuffer byteBuffer) { 104 writeVersionAndFlags(byteBuffer); 105 IsoTypeWriter.writeUInt32(byteBuffer, sampleSize); 106 107 if (sampleSize == 0) { 108 IsoTypeWriter.writeUInt32(byteBuffer, sampleSizes.length); 109 for (long sampleSize1 : sampleSizes) { 110 IsoTypeWriter.writeUInt32(byteBuffer, sampleSize1); 111 } 112 } else { 113 IsoTypeWriter.writeUInt32(byteBuffer, sampleCount); 114 } 115 116 } 117 toString()118 public String toString() { 119 return "SampleSizeBox[sampleSize=" + getSampleSize() + ";sampleCount=" + getSampleCount() + "]"; 120 } 121 } 122