@@ -191,60 +191,6 @@ public int depacketize(byte[] payload) throws Exception {
191191 return OBUElements .size ();
192192 }
193193
194- /**
195- * Packetizes a list of AV1 OBU elements.
196- *
197- * @param obuElements list of OBUInfo
198- * @param mtu maximum transmission unit
199- * @return list of packets
200- */
201- public List <byte []> packetize (List <OBUInfo > obuInfos , int mtu ) {
202- List <byte []> payloads = new LinkedList <>();
203- for (OBUInfo obuInfo : obuInfos ) {
204- // obuInfo.data does not include the OBU header
205- byte [] payload = obuInfo .data .array ();
206- byte frameType = (byte ) ((obuInfo .obuType .getValue () & OBU_FRAME_TYPE_MASK ) >> OBU_FRAME_TYPE_BITSHIFT );
207- if (frameType == OBU_FRAME_TYPE_SEQUENCE_HEADER ) {
208- sequenceHeader = payload ;
209- } else {
210- int payloadDataIndex = 0 ;
211- int payloadDataRemaining = payload .length ;
212- byte obuCount = 0 ;
213- // no meta no need to add to metadata size
214- int metadataSize = 0 ;
215- if (sequenceHeader != null && sequenceHeader .length > 0 ) {
216- // metadata size is small so 1 byte to hold its leb128 encoded length
217- metadataSize += sequenceHeader .length + 1 ;
218- obuCount ++;
219- }
220- do {
221- int outOffset = 1 ; // AV1_PAYLOADER_HEADER_SIZE
222- byte [] out = new byte [Math .min (mtu , payloadDataRemaining + metadataSize )];
223- out [0 ] = (byte ) (obuCount << W_BITSHIFT );
224- if (obuCount == 2 ) {
225- out [0 ] ^= N_MASK ;
226- out [1 ] = (byte ) LEB128 .encode (sequenceHeader .length );
227- System .arraycopy (sequenceHeader , 0 , out , 2 , sequenceHeader .length );
228- outOffset += sequenceHeader .length + 1 ; // 1 byte for LEB128
229- sequenceHeader = null ;
230- }
231- int outBufferRemaining = out .length - outOffset ;
232- System .arraycopy (payload , payloadDataIndex , out , outOffset , outBufferRemaining );
233- payloadDataRemaining -= outBufferRemaining ;
234- payloadDataIndex += outBufferRemaining ;
235- if (!payloads .isEmpty ()) {
236- out [0 ] ^= Z_MASK ;
237- }
238- if (payloadDataRemaining != 0 ) {
239- out [0 ] ^= Y_MASK ;
240- }
241- payloads .add (out );
242- } while (payloadDataRemaining > 0 );
243- }
244- }
245- return payloads ;
246- }
247-
248194 /**
249195 * Packetizes an AV1 payload.
250196 *
@@ -295,39 +241,49 @@ public List<byte[]> packetize(byte[] payload, int mtu) {
295241 return payloads ;
296242 }
297243
298- // Payload fragments a AV1 packet across one or more byte arrays
299- // See AV1Packet for description of AV1 Payload Header
300- public static LinkedList <byte []> marshal (int mtu , byte [] payload ) {
244+ /**
245+ * Packetizes a list of AV1 OBU, consisting of a sequence header and one or more OBU elements.
246+ *
247+ * @param obuElements list of OBUInfo
248+ * @param mtu maximum transmission unit
249+ * @return list of packets
250+ */
251+ public List <byte []> packetize (List <OBUInfo > obuInfos , int mtu ) {
301252 LinkedList <byte []> payloads = new LinkedList <>();
302253 int aggregationHeaderLength = 1 ;
303254 int maxFragmentSize = mtu - aggregationHeaderLength - 2 ;
304- int payloadDataRemaining = payload .length ;
305- int payloadDataIndex = 0 ;
306- // Make sure the fragment/payload size is correct
307- if (Math .min (maxFragmentSize , payloadDataRemaining ) > 0 ) {
308- while (payloadDataRemaining > 0 ) {
309- int currentFragmentSize = Math .min (maxFragmentSize , payloadDataRemaining );
310- int leb128Size = 1 ;
311- if (currentFragmentSize >= 127 ) {
312- leb128Size = 2 ;
313- }
314- byte [] out = new byte [aggregationHeaderLength + leb128Size + currentFragmentSize ];
315- int leb128Value = LEB128 .encode (currentFragmentSize );
316- if (leb128Size == 1 ) {
317- out [1 ] = (byte ) leb128Value ;
318- } else {
319- out [1 ] = (byte ) (leb128Value >> 8 );
320- out [2 ] = (byte ) leb128Value ;
321- }
322- System .arraycopy (payload , payloadDataIndex , out , aggregationHeaderLength + leb128Size , currentFragmentSize );
323- payloads .add (out );
324- payloadDataRemaining -= currentFragmentSize ;
325- payloadDataIndex += currentFragmentSize ;
326- if (payloads .size () > 1 ) {
327- out [0 ] ^= Z_MASK ;
328- }
329- if (payloadDataRemaining != 0 ) {
330- out [0 ] ^= Y_MASK ;
255+ for (OBUInfo obuInfo : obuInfos ) {
256+ // obuInfo.data does not include the OBU header
257+ byte frameType = (byte ) ((obuInfo .obuType .getValue () & OBU_FRAME_TYPE_MASK ) >> OBU_FRAME_TYPE_BITSHIFT );
258+ byte [] payload = obuInfo .data .array ();
259+ int payloadDataRemaining = payload .length , payloadDataIndex = 0 ;
260+ logger .debug ("Frame type: {}" , frameType );
261+ // Make sure the fragment/payload size is correct
262+ if (Math .min (maxFragmentSize , payloadDataRemaining ) > 0 ) {
263+ while (payloadDataRemaining > 0 ) {
264+ int currentFragmentSize = Math .min (maxFragmentSize , payloadDataRemaining );
265+ int leb128Value = LEB128 .encode (currentFragmentSize );
266+ byte [] out ;
267+ if (currentFragmentSize >= 127 ) { // leb takes at least 2 bytes
268+ int outLen = aggregationHeaderLength + 2 + currentFragmentSize ;
269+ out = new byte [outLen ];
270+ out [1 ] = (byte ) (leb128Value >> 8 );
271+ out [2 ] = (byte ) leb128Value ;
272+ System .arraycopy (payload , payloadDataIndex , out , aggregationHeaderLength + 2 , currentFragmentSize );
273+ } else { // leb expected to be 1 byte
274+ out = new byte [aggregationHeaderLength + 1 + currentFragmentSize ];
275+ out [1 ] = (byte ) leb128Value ;
276+ System .arraycopy (payload , payloadDataIndex , out , aggregationHeaderLength + 1 , currentFragmentSize );
277+ }
278+ payloads .add (out );
279+ payloadDataRemaining -= currentFragmentSize ;
280+ payloadDataIndex += currentFragmentSize ;
281+ if (payloads .size () > 1 ) {
282+ out [0 ] ^= Z_MASK ;
283+ }
284+ if (payloadDataRemaining != 0 ) {
285+ out [0 ] ^= Y_MASK ;
286+ }
331287 }
332288 }
333289 }
0 commit comments