@@ -113,7 +113,8 @@ class M4ACommonDemuxer {
113
113
void begin () {
114
114
sampleIndex = 0 ;
115
115
buffer.clear ();
116
- sampleSizes.clear ();
116
+ p_chunk_offsets->clear ();
117
+ p_sample_sizes->clear ();
117
118
buffer.resize (1024 );
118
119
current_size = 0 ;
119
120
box_pos = 0 ;
@@ -169,7 +170,7 @@ class M4ACommonDemuxer {
169
170
// / fill buffer up to the current sample size
170
171
for (int j = 0 ; j < len; j++) {
171
172
buffer.write (data[j]);
172
- if (buffer.available () = = currentSize) {
173
+ if (buffer.available () > = currentSize) {
173
174
LOGI (" Sample# %zu: size %zu bytes" , sampleIndex, currentSize);
174
175
executeCallback (currentSize);
175
176
buffer.clear ();
@@ -185,23 +186,34 @@ class M4ACommonDemuxer {
185
186
LOGE (" No sample size defined, cannot write data" );
186
187
return j;
187
188
}
188
- resize (currentSize);
189
189
}
190
190
}
191
191
return len;
192
192
}
193
193
194
194
/* *
195
- * @brief Returns the vector of sample sizes.
196
- * @return Reference to the vector of sample sizes.
195
+ * @brief Returns the buffer of sample sizes.
196
+ * @return Reference to the buffer of sample sizes.
197
197
*/
198
- Vector <stsz_sample_size_t >& getSampleSizes () { return sampleSizes ; }
198
+ BaseBuffer <stsz_sample_size_t >& getSampleSizesBuffer () { return *p_sample_sizes ; }
199
199
200
200
/* *
201
- * @brief Returns the vector of chunk offsets .
202
- * @return Reference to the vector of chunk offsets .
201
+ * @brief Sets the buffer to use for sample sizes .
202
+ * @param buffer Reference to the buffer to use .
203
203
*/
204
- Vector<uint32_t >& getChunkOffsets () { return chunkOffsets; }
204
+ void setSampleSizesBuffer (BaseBuffer<stsz_sample_size_t > &buffer){ p_sample_sizes = &buffer;}
205
+
206
+ /* *
207
+ * @brief Returns the buffer of chunk offsets.
208
+ * @return Reference to the buffer of chunk offsets.
209
+ */
210
+ BaseBuffer<uint32_t >& getChunkOffsetsBuffer () { return *p_chunk_offsets; }
211
+
212
+ /* *
213
+ * @brief Sets the buffer to use for chunk offsets.
214
+ * @param buffer Reference to the buffer to use.
215
+ */
216
+ void setChunkOffsetsBuffer (BaseBuffer<uint32_t >&buffer){ p_chunk_offsets = &buffer;}
205
217
206
218
/* *
207
219
* @brief Sets a fixed sample size/count instead of using the sampleSizes
@@ -263,8 +275,10 @@ class M4ACommonDemuxer {
263
275
}
264
276
265
277
protected:
266
- Vector<stsz_sample_size_t > sampleSizes; // /< Table of sample sizes.
267
- Vector<uint32_t > chunkOffsets; // /< Table of chunk offsets.
278
+ SingleBuffer<stsz_sample_size_t > defaultSampleSizes; // /< Table of sample sizes.
279
+ SingleBuffer<uint32_t > defaultChunkOffsets; // /< Table of chunk offsets.
280
+ BaseBuffer<stsz_sample_size_t > *p_sample_sizes = &defaultSampleSizes;
281
+ BaseBuffer<uint32_t > *p_chunk_offsets = &defaultChunkOffsets;
268
282
Vector<uint8_t > tmp;
269
283
Codec codec = Codec::Unknown; // /< Current codec.
270
284
FrameCallback callback = nullptr ; // /< Frame callback.
@@ -305,13 +319,24 @@ class M4ACommonDemuxer {
305
319
* @return Size of the current sample.
306
320
*/
307
321
size_t currentSampleSize () {
322
+ static size_t last_index = -1 ;
323
+ static size_t last_size = -1 ;
324
+
325
+ // Return cached size
326
+ if (sampleIndex == last_index) {
327
+ return last_size;
328
+ }
329
+
308
330
// using fixed sizes w/o table
309
331
if (fixed_sample_size > 0 && fixed_sample_count > 0 &&
310
332
sampleIndex < fixed_sample_count) {
311
333
return fixed_sample_size;
312
334
}
313
- if (sampleSizes && sampleIndex < sampleSizes.size ()) {
314
- return sampleSizes[sampleIndex];
335
+ stsz_sample_size_t nextSize = 0 ;
336
+ if (p_sample_sizes->read (nextSize)) {
337
+ last_index = sampleIndex;
338
+ last_size = nextSize;
339
+ return nextSize;
315
340
}
316
341
return 0 ;
317
342
}
@@ -347,6 +372,16 @@ class M4ACommonDemuxer {
347
372
* @param cb Frame callback function.
348
373
*/
349
374
virtual void setCallback (FrameCallback cb) { frame_callback = cb; }
375
+ /* *
376
+ * @brief Sets the buffer to use for sample sizes.
377
+ * @param buffer Reference to the buffer to use.
378
+ */
379
+ void setSampleSizesBuffer (BaseBuffer<stsz_sample_size_t > &buffer){ sampleExtractor.setSampleSizesBuffer (buffer);}
380
+ /* *
381
+ * @brief Sets the buffer to use for sample sizes.
382
+ * @param buffer Reference to the buffer to use.
383
+ */
384
+ void setChunkOffsetsBuffer (BaseBuffer<uint32_t > &buffer){ sampleExtractor.setChunkOffsetsBuffer (buffer);}
350
385
351
386
protected:
352
387
FrameCallback frame_callback = nullptr ;
@@ -507,14 +542,16 @@ class M4ACommonDemuxer {
507
542
uint32_t sampleSize = readU32 (data + 4 );
508
543
uint32_t sampleCount = readU32 (data + 8 );
509
544
sampleExtractor.begin ();
510
- Vector <stsz_sample_size_t >& sampleSizes = sampleExtractor.getSampleSizes ();
545
+ BaseBuffer <stsz_sample_size_t >& sampleSizes = sampleExtractor.getSampleSizesBuffer ();
511
546
if (sampleSize == 0 ) {
512
547
LOGI (" -> Sample Sizes Count: %u" , sampleCount);
513
548
sampleSizes.resize (sampleCount);
514
549
for (uint32_t i = 0 ; i < sampleCount; ++i) {
515
550
uint32_t sampleSizes32 = readU32 (data + 12 + i * 4 );
516
- sampleSizes[i] = static_cast <stsz_sample_size_t >(sampleSizes32);
517
- assert (static_cast <uint32_t >(sampleSizes[i]) == sampleSizes32);
551
+ // if this is giving an error change the stsz_sample_size_t
552
+ assert (sampleSizes32 <= UINT16_MAX);
553
+ stsz_sample_size_t sampleSizes16 = sampleSizes32;
554
+ assert (sampleSizes.write (sampleSizes16));
518
555
}
519
556
} else {
520
557
sampleExtractor.setFixedSampleCount (sampleSize, sampleCount);
@@ -534,12 +571,12 @@ class M4ACommonDemuxer {
534
571
size_t size = box.data_size ;
535
572
if (size < 4 ) return ;
536
573
uint32_t entryCount = readU32 (data);
537
- Vector <uint32_t >& chunkOffsets = sampleExtractor.getChunkOffsets ();
574
+ BaseBuffer <uint32_t >& chunkOffsets = sampleExtractor.getChunkOffsetsBuffer ();
538
575
if (size < 4 + 4 * entryCount) return ;
539
576
chunkOffsets.resize (entryCount);
540
577
LOGI (" -> Chunk offsets count: %u" , entryCount);
541
578
for (uint32_t i = 0 ; i < entryCount; ++i) {
542
- chunkOffsets[i] = readU32 (data + 4 + i * 4 );
579
+ chunkOffsets. write ( readU32 (data + 4 + i * 4 ) );
543
580
}
544
581
stco_processed = true ;
545
582
}
0 commit comments