-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrack.cpp
629 lines (538 loc) · 24.5 KB
/
track.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
Untrunc - track.cpp
GPLv2 or later
Greetings to work of: 2010, Federico Ponchio
*/
//edited 2017
#include "track.h"
#include "atom.h"
#include "portable_endian.h"
#include "loginfo.h"
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
recoveredSample Codec::getSampleSize(uint8_t *inbuf, int32_t blockLength, int32_t minLength, AVCodecParameters *streamCodec, uint32_t samplesCount, uint8_t nalSizeField) {
recoveredSample sizeResults;
recoveredSample recoveredSTmp;
AVCodec *codec;
AVCodecContext *c = NULL;
//uint8_t inbufmpeg[inbuf + AV_INPUT_BUFFER_PADDING_SIZE];
//uint8_t *inbuf;
//inbuf = inbufRaw;
codec = avcodec_find_decoder(streamCodec->codec_id);
if (!codec) {
throw string("Codec not found! id:")+to_string(streamCodec->codec_id);
//exit(1);
}
c = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(c, streamCodec);
if (!c) {
throw string("Could not allocate codec context!");
//exit(1);
}
logMe(LOG_DBG, "CodecID " + to_string(streamCodec->codec_id));
logMe(LOG_DBG, codec->long_name);
if (avcodec_open2(c, codec, NULL) < 0) {
throw string("Error. Could not open codec!");
//exit(1);
}
AVPacket *avpkt;
AVFrame *decoded_frame = NULL;
decoded_frame = av_frame_alloc();
//av_init_packet(avpkt);
avpkt = av_packet_alloc();
if (!avpkt)
throw string("Error. AVpacket allocate failure.");
avpkt->data = inbuf;
avpkt->size = minLength; //put minimum size of the packet here
logMe(LOG_DBG, " blockLength " + to_string(blockLength));
logMe(LOG_DBG, "initial avpkt->size " + to_string(avpkt->size));
int ret;
ret = -1; //assume samples not found, size unknown
uint8_t nalFirstByte;//NAL first byte to get forbidden_zero_bit, nal_ref_idc and nal_type
switch (streamCodec->codec_id) {
case AV_CODEC_ID_H264:
//try to recognize AVC sample and confirm its size without decoding it
if (h264alg == 10) {
//v1.0
logMe(LOG_DBG, "v1.0 in use");
//!!!___works only for: nalSizeField = 4 bytes; max sample size < 16777215; nal_type = [1..20]; min nal size = 5
if ((*inbuf) == 0 &&
((*inbuf +4) & 0x1F) < 21) {
ret = 0; //all OK
}
} else if (h264alg == 11) {
//v1.1
logMe(LOG_DBG, "v1.1 in use");
//!!!___works only for: nalSizeField = 4 bytes; max sample size < 16777215; nal_type = [1..20]; min nal size = 5
if (inbuf[0] == 0 &&
(inbuf[4] & 0x1F) < 21 &&
(inbuf[4] & 0x1F) > 0){
ret = 0; //all OK
}
} else {
//v1.2
logMe(LOG_DBG, "v1.2 in use");
nalFirstByte = inbuf[nalSizeField];
logMe(LOG_DBG, "nal first byte " + to_string(nalFirstByte));
//nal size lower then max allowed sample size for the track and
//nal_type (5-bits) in range [1..21] and
//nal_forbidden_bit (1-bit) equals to 0
if ((minLength <= blockLength) &&
(nalFirstByte & 0x1F) < 22 &&
(nalFirstByte & 0x1F) > 0 &&
(nalFirstByte & 0x80) == 0) {
switch (nalFirstByte & 0x1F) {
case 6:
case 9:
case 10:
case 11:
case 12:
//nal_ref_idc (2-bits)
if ((nalFirstByte & 0x60) == 0)
ret = 0;
break;
case 5:
case 7:
case 8:
case 13:
case 15:
//nal_ref_idc (2-bits)
if ((nalFirstByte & 0x60) > 0)
ret = 0;
break;
case 1:
case 2:
case 3:
case 4:
//nal_ref_idc (2-bits)
//any value
ret = 0;
break;
default:
//16,17,18 - unknown NAL
ret = -1;
break;
}
} //if
}
break;
//TODO: add AV_CODEC_ID_HEVC and others codecs
/*
Single channel audio (Mono/Center) or in terms of AAC - individual_channel_stream(common_window = 0)
can has leading zero bits.
Raw stream structure (ISO/IEC 13818-7 2004):
ID_SCE = 3 bits [0]
element_instance_tag = 4 bits [0..15]
global_gain = 8 bits [0..255]
ics_reserved_bit = 1 bit [0] - reserved bit for future use
window_sequence = 2 bit [0..3]
window_shape = 1 bit [0..1]
if (window_sequence == 2)
then
max_sfb = 4 bit [0..15] - can it be zero?
scale_factor_grouping = 7 bits [0..127]
else
max_sfb = 6 bit [0..63] - can it be zero?
predictor_data_present = 1 bit [0..1]
if (predictor_data_present == 1)
then
predictor_reset = 1 bit [0..1]
if (predictor_reset == 1)
then
predictor_reset_group_number = 5 bits [0..31]
...
section_data
scale_factor_data
...
So, if first byte is zero, data may be recognized as:
if [0] == 0 and [1] > 0 and [1] & 0x1 == 0
Valid Sigle channel data mask (element_instance_tag = 0):
0000 000X XXXX XXX0 XXXX XXXX XXXX XX
if global_gain < 128
then valid Sigle channel data mask (element_instance_tag = 0):
0000 0000 XXXX XXX0 XXXX XXXX XXXX XX (or leading zero-byte sample)
*/
case AV_CODEC_ID_AAC:
if ((inbuf[0] == 0) && (blockLength > 1)) {
//By logic, the ics_reserved_bit check only required and should be compared first.
//But global_gain comparison is faster with short logic and real samples,
//because it can cut off small h264 samples with NAL-size field = 4 far earlier.
//FFmpeg reaches MAX allowed sample size for the track much slower.
if ((inbuf[1] >= (uint8_t) aacGG) &&
(inbuf[1] & 0x01) == 0) {
logMe(LOG_DBG, "assuming AAC (1 channel) with global_gain = " + to_string(inbuf[1] >> 1));
} else {
logMe(LOG_DBG, "Not an AAC; global_gain = " + to_string(inbuf[1] >> 1));
break; //skip leading zero-byte tracks. Let's assume that is not AAC track (ISO/IEC 13818-7 2004)
}
}
//unknown exact sample size, so try to use ffmpeg by slowly increasing packet size
while ((avpkt->size <= blockLength) && (avpkt->size > 0) && (ret < 0) && !(ret == AVERROR_EOF)) {
//logMe(LOG_DBG, "avpkt->size = " + to_string(avpkt->size)); //too verbose...
ret = avcodec_send_packet(c, avpkt);
//Here AVERROR(EAGAIN) return value means that decoder's buffer full and it's time to
//call avcodec_receive_frame. Then, probably, will be possible to send this packet again...
if (ret < 0) {
if (ret == AVERROR_EOF) {
logMe(LOG_DBG, "send AVERROR_EOF");
}
if (ret == AVERROR(EAGAIN)) {
logMe(LOG_DBG, "send AVERROR(EAGAIN)");
}
avpkt->size++;
}
} //while
break;
default:
//unknown exact sample size, so try to use ffmpeg by slowly increasing packet size
while ((avpkt->size <= blockLength) && (avpkt->size > 0) && (ret < 0) && !(ret == AVERROR_EOF)) {
//logMe(LOG_DBG, "avpkt->size = " + to_string(avpkt->size)); //too verbose...
ret = avcodec_send_packet(c, avpkt);
//Here AVERROR(EAGAIN) return value means that decoder's buffer full and it's time to
//call avcodec_receive_frame. Then, probably, will be possible to send this packet again...
if (ret < 0) {
if (ret == AVERROR_EOF) {
logMe(LOG_DBG, "send AVERROR_EOF");
}
if (ret == AVERROR(EAGAIN)) {
logMe(LOG_DBG, "send AVERROR(EAGAIN)");
}
avpkt->size++;
}
} //while
} //switch
recoveredSTmp.smplSize = avpkt->size; //send was success or not
recoveredSTmp.smplSend = ret;
logMe(LOG_DBG, "send packet ret = " + to_string(ret));
int32_t len = 0;
for (uint32_t i = 0; i < samplesCount; i++) {
//TODO: enable this code later
//len = avcodec_receive_frame(c, decoded_frame);
//Here AVERROR(EAGAIN) return value means that new input data is required to
//return new output.
if ((len < 0) && (len != AVERROR(EAGAIN)) && (len != AVERROR_EOF)) {
logMe(LOG_DBG, "Cannot decode the frame...");
}
if (len == AVERROR(EAGAIN)) len = avpkt->size; //sample consumes whole packet, this is equal to 1 sample per chunk
recoveredSTmp.smplNumInChunk = i +1;
sizeResults = recoveredSTmp;
logMe(LOG_DBG, "len = " + to_string(len));
} //for
//TODO:
//keyframe, IDR (check it later)
//NALUnitType
//by H264Context
//by HEVCContext
//check for timestamp, there is no two same timestamps
//there is no big gaps between timestamps (probably sample just missed)
//special flushPacket which signals the end of the stream
//avpkt.data = NULL;
//avpkt.size = 0;
//
avcodec_free_context(&c);
av_frame_free(&decoded_frame);
av_packet_free(&avpkt);
return sizeResults;
}
uint32_t Track::optMinSampleSize(Track &track, clFile &brokenFile, uint64_t sampleOffset, AVCodecParameters *streamCodec) {
uint32_t sampleSizeAssumption; //expected size of the sample
uint32_t bb;
switch(streamCodec->codec_id) {
case AV_CODEC_ID_H264:
//Place here optimizations for minimum sample size
//
//read NAL-size field and return its value as minimal size for the sample
//the NAL-size field can be 1, 2 or 4 bytes long
//nalSizeField stores lenght of this field per track
sampleSizeAssumption = brokenFile.readUintP(sampleOffset);
logMe(LOG_DBG, "sampleSizeAssumption = " + to_string(sampleSizeAssumption));
bb = 8 * (4 - track.nalSizeField);
logMe(LOG_DBG, " field length = " + to_string(track.nalSizeField));
sampleSizeAssumption = sampleSizeAssumption >> bb;
sampleSizeAssumption += track.nalSizeField; //sample size should include NAL-size field
logMe(LOG_DBG, "First sampleSizeAssumption = " + to_string(sampleSizeAssumption));
if (sampleSizeAssumption > (track.sizesMinMax[1] *2)) { //twice bigger video samples almost not possible
sampleSizeAssumption = (track.sizesMinMax[1] *2);
logMe(LOG_DBG, "Sample size assumption exceeds max sample size of the working example");
logMe(LOG_DBG, "New sampleSizeAssumption = " + to_string(sampleSizeAssumption));
}
return sampleSizeAssumption;
break;
case AV_CODEC_ID_HEVC:
//Place here optimizations for minimum sample size
sampleSizeAssumption = 4;
logMe(LOG_DBG, "First sampleSizeAssumption = " + to_string(sampleSizeAssumption));
if (sampleSizeAssumption > (track.sizesMinMax[1] *2)) { //twice bigger video samples almost not possible
sampleSizeAssumption = (track.sizesMinMax[1] *2);
logMe(LOG_DBG, "Sample size assumption exceeds max sample size of the working example");
logMe(LOG_DBG, "New sampleSizeAssumption = " + to_string(sampleSizeAssumption));
}
return sampleSizeAssumption;
break;
case AV_CODEC_ID_AAC:
//Place here optimizations for minimum sample size
//TODO: optimizations on AAC packet size, maybe...
sampleSizeAssumption = 6;
if ((track.sizesMinMax[0] < 6) && (track.sizesMinMax[0] > 1)) //4 is possible value
sampleSizeAssumption = track.sizesMinMax[0];
logMe(LOG_DBG, "First sampleSizeAssumption = " + to_string(sampleSizeAssumption));
if (sampleSizeAssumption > 12000) { //!!!assumption based on own experience; subject to change
sampleSizeAssumption = (track.sizesMinMax[1] *2); //make it twice bigger
logMe(LOG_DBG, "Sample size assumption exceeds max sample size of the working example (" + to_string(track.sizesMinMax[1]) + ")");
logMe(LOG_DBG, "New sampleSizeAssumption = " + to_string(sampleSizeAssumption));
}
return sampleSizeAssumption;
break;
case AV_CODEC_ID_PCM_S16LE:
//Place here optimizations for minimum sample size
//format: 'sowt' ('soun' sound media handler_type); uncompressed PCM signed 16-bit little-endian
sampleSizeAssumption = track.sizesMinMax[0]; //minimum and always fixed
logMe(LOG_DBG, "First sampleSizeAssumption = " + to_string(sampleSizeAssumption));
return sampleSizeAssumption;
break;
default:
return 4; //sampleSizeAssumption for unknown codec
}
}
void Track::parse(Track *track, Box *t, clFile &workingFile) {
logMe(LOG_DBG, "===============");
Box *tkhd = t->getBoxByType((uint32_t)'tkhd');
if(!tkhd)
throw string("No 'tkhd' box inside current 'trak': unknown track ID");
if (((tkhd->vpFlags) >> 24) == 1) {
//for FullBox version 1
track->trkID = workingFile.readUintP((tkhd->contentOffset) +8 +8);
} else {
track->trkID = workingFile.readUintP((tkhd->contentOffset) +4 +4);
}
logMe(LOG_DBG, "track ID = " + to_string(track->trkID));
Box *mdhd = t->getBoxByType((uint32_t)'mdhd');
if(!mdhd)
throw string("No 'mdhd' box inside current 'trak': unknown track duration and timescale");
//test
//AAC (857 samples of 1024 time delta + 1 sample of 0 time delta at the end)
//track timescale = 44100
//track duration = 877568
//time ~19.899501133786848072562358276644
//H264 (498 samples of 512 time delta)
//track timescale = 12800
//track duration = 254976
//time ~19.92
if (((mdhd->vpFlags) >> 24) == 1) {
//for FullBox version 1
timescale = workingFile.readUintP((mdhd->contentOffset) +8 +8);
duration64 = workingFile.readUint64(); //always lies next to timescale
} else {
timescale = workingFile.readUintP((mdhd->contentOffset) +4 +4);
duration = workingFile.readUint(); //always lies next to timescale
}
logMe(LOG_DBG, "track timescale = " + to_string(timescale));
logMe(LOG_DBG, "track duration = " + to_string(duration));
logMe(LOG_DBG, "track duration64 = " + to_string(duration64));
track->times = getSampleDeltas(t, workingFile);
track->keyframes = getKeyframes(t, workingFile);
//TODO: add option to define custom min and max sizes
track->sizesMinMax = getMinMaxSTSZ(t, workingFile);
logMe(LOG_INFO, "Samples size: MIN " + to_string(track->sizesMinMax[0]) + "; MAX " + to_string(track->sizesMinMax[1]));
track->offsets = getChunkOffsets(t, workingFile);
if (track->offsets.size() == 0) {
logMe(LOG_INFO, "Chunks offset box 'stco' empty. Trying 'co64' box...");
track->longOffsets64 = getChunkOffsets64(t, workingFile);
track->useOffsets64 = true;
} else {
track->useOffsets64 = false;
};
track->samplesFPD = getSamplesNum(t, workingFile);
//for (int i=0; i<samplesFPD.size();i++) {
//logMe(LOG_DBG, to_string(samplesFPD[i].firstChunk) + " ");
//logMe(LOG_DBG, to_string(samplesFPD[i].samplesCount) + " ");
//logMe(LOG_DBG, to_string(samplesFPD[i].descriptionIndex) + " ");
//}
logMe(LOG_DBG, "");
track->nalSizeField = getNalSizeField(track, t, workingFile);
logMe(LOG_DBG, "nalSizeField= " + to_string(track->nalSizeField));
} //temporal end.
//By now, I don't see any reason to continue parsing working tracks,
//because we can get all we need from FFmpeg...
//not parsing, just collection of deltas from the 'stts' box
vector<uint32_t> Track::getSampleDeltas(Box *t, clFile &workingFile) {
vector<uint32_t> sampleTimes;
//decoding time to sample
Box *stts = t->getBoxByType((uint32_t)'stts');
if (!stts)
throw string("Missing sample to time box ('stts' not found)");
uint32_t entries = workingFile.readUintP(stts->contentOffset);
logMe(LOG_INFO, "number of entries (stts) = " + to_string(entries));
for(uint32_t i = 0; i < entries; i++) {
uint32_t nsamples = workingFile.readUint(); //sample_count
logMe(LOG_DBG, "nsamples = " + to_string(nsamples));
uint32_t sampleDelta = workingFile.readUint(); //sample_delta
logMe(LOG_DBG, "sample delta = " + to_string(sampleDelta));
//for(int j = 0; j < nsamples; j++)
sampleTimes.push_back(sampleDelta); //just pushing deltas without samples count for this deltas
}
return sampleTimes;
}
vector<uint32_t> Track::getKeyframes(Box *t, clFile &workingFile) {
vector<uint32_t> sampleKey;
Box *stss = t->getBoxByType((uint32_t)'stss');
if (!stss)
return sampleKey;
uint32_t entries = workingFile.readUintP(stss->contentOffset);
logMe(LOG_INFO, "number of entries (stss) = " + to_string(entries));
for(uint32_t i = 0; i < entries; i++)
sampleKey.push_back(workingFile.readUint()); //sample_number
return sampleKey;
}
//not parsing, just looking for min/max values
std::vector<uint32_t> Track::getMinMaxSTSZ(Box *t, clFile &workingFile) {
vector<uint32_t> sampleMinMaxSizes;
uint32_t sampleOfMinSize;
uint32_t sampleOfMaxSize;
uint32_t currentSampleSize;
Box *stsz = t->getBoxByType((uint32_t)'stsz');
if (stsz) {
uint32_t defaultSize = workingFile.readUintP(stsz->contentOffset); //sample_size
logMe(LOG_DBG, "default sample size (stsz) = " + to_string(defaultSize));
uint32_t entries = workingFile.readUint(); //sample_count
logMe(LOG_INFO, "number of entries (stsz) = " + to_string(entries));
if (defaultSize == 0) {
sampleOfMinSize = workingFile.readUint(); //read first entry
sampleOfMaxSize = sampleOfMinSize;
//read all next entries and find min/max values
for (uint32_t i = 1; i < entries; i++) {
currentSampleSize = workingFile.readUint(); //entry_size
if (currentSampleSize < sampleOfMinSize) {
sampleOfMinSize = currentSampleSize;
}
if (currentSampleSize > sampleOfMaxSize) {
sampleOfMaxSize = currentSampleSize;
}
}
sampleMinMaxSizes.push_back(sampleOfMinSize); //[0] is min
sampleMinMaxSizes.push_back(sampleOfMaxSize); //[0] is max
} else {
sampleMinMaxSizes.resize(2, defaultSize); //all samples has default size in bytes (same value)
}
} else {
logMe(LOG_INFO, "'stsz' box not found. Trying compact sample size box 'stz2'...");
Box *stz2 = t->getBoxByType((uint32_t)'stz2');
if (!stz2)
throw string("Missing sample to size box ('stz2' not found)");
uint32_t fldSize = workingFile.readUintP(stz2->contentOffset); //field_size
logMe(LOG_DBG, "'stz2' field_size = " + to_string(fldSize)); //000X - first 3 bytes: reserved = 0
uint32_t entries = workingFile.readUint(); //sample_count
logMe(LOG_INFO, "number of entries (stz2) = " + to_string(entries));
sampleOfMinSize = 0xFFFFFFFF; //max possible value
sampleOfMaxSize = 0;
//read all entries and find min/max values
uint32_t i = 1;
uint32_t curSmpl; //small sample storage (first few bits never used)
while (i < entries) {
currentSampleSize = workingFile.readUint(); //next 4 bytes
for (int j = 0; j < (int)(32 / fldSize); j++) {
i++;
if (i > entries)
break; //to skip padding bytes reading
//possible sizes 4; 8; 16 bit, in case of 4 - two values at 1 byte (byte[xxxxyyyy] xxxx-first, yyyy-second);
curSmpl = currentSampleSize >> (32 - fldSize * (j +1)); //entry_size
if (curSmpl < sampleOfMinSize) {
sampleOfMinSize = curSmpl;
}
if (curSmpl > sampleOfMaxSize) {
sampleOfMaxSize = curSmpl;
}
} //for
} //while
sampleMinMaxSizes.push_back(sampleOfMinSize); //[0] is min
sampleMinMaxSizes.push_back(sampleOfMaxSize); //[0] is max
} //if
return sampleMinMaxSizes; //[0] is min; [1] is max;
}
std::vector<uint32_t> Track::getChunkOffsets(Box *t, clFile &workingFile) {
vector<uint32_t> chunkOffsets;
//chunk offsets
Box *stco = t->getBoxByType((uint32_t)'stco');
// 'stco' box not always present ('co64' is possible)
if (stco) {
uint32_t nchunks = workingFile.readUintP(stco->contentOffset); //entry_count
logMe(LOG_INFO, "number of entries (stco) = " + to_string(nchunks));
for (uint32_t i = 0; i < nchunks; i++) {
chunkOffsets.push_back(workingFile.readUint()); //chunk_offset
}
}
return chunkOffsets;
}
std::vector<uint64_t> Track::getChunkOffsets64(Box *t, clFile &workingFile) {
vector<uint64_t> chunkOffsets64;
Box *co64 = t->getBoxByType((uint32_t)'co64');
if (!co64)
throw string("Missing chunk offset box ('co64' not found)");
uint32_t nchunks = workingFile.readUintP(co64->contentOffset); //entry_count
logMe(LOG_INFO, "number of entries (co64) = " + to_string(nchunks));
for (uint32_t i = 0; i < nchunks; i++)
chunkOffsets64.push_back(workingFile.readUint64()); //chunk_offset
return chunkOffsets64;
}
std::vector<ChunkMembers> Track::getSamplesNum(Box *t, clFile &workingFile) {
vector<ChunkMembers> firstChunkSamplesDesc;
Box *stsc = t->getBoxByType((uint32_t)'stsc');
if (!stsc)
throw string("Missing sample to chunk box ('stsc' not found)");
uint32_t entries = workingFile.readUintP(stsc->contentOffset); //entry_count
logMe(LOG_INFO, "number of entries (stsc) = " + to_string(entries));
for (uint32_t i = 0; i < entries; i++) {
ChunkMembers x;
x.firstChunk = workingFile.readUint(); //first_chunk
x.samplesCount = workingFile.readUint(); //samples_per_chunk
x.descriptionIndex = workingFile.readUint(); //sample_description_index
firstChunkSamplesDesc.push_back(x);
}
return firstChunkSamplesDesc;
}
uint8_t Track::getNalSizeField(Track *track, Box *t, clFile &workingFile) {
//try to get 'stsd' box from current track
Box *stsd = t->getBoxByType((uint32_t)'stsd');
uint8_t nalFld = 0; //only 1, 2 or 4 bytes allowed
AVCodecID codecID = track->codec.codecParamByFFmpeg->codec_id;
logM(LOG_DBG, "CodecID= " + to_string(codecID) + "; ");
switch(codecID) {
case AV_CODEC_ID_H264:
//for avcC nal_length_size = (data[4] & 0x03) + 1; see h264_parse.c
//read fifth byte from avcC and apply 0000 0011 mask, add 1 to get field length for nal size.
//example, FF & 0x03 = 3 (field size = 4)
//or use h264_init_context
if (stsd) {
int64_t count = stsd->contentSize; //the 'stsd' box size always fits uint32_t, its content - too (even int32_t).
logM(LOG_DBG, "stsd->contentSize: ");
logMe(LOG_DBG, to_string(stsd->contentSize));
uint8_t *buffer;
buffer = (uint8_t*) malloc(sizeof(uint8_t) * count);
if (buffer == NULL)
throw string("Error. Cannot allocate memory buffer to get NAL size field.");
//1. Read whole content of 'stsd' box to buffer
//2. Search for avcC instead of parsing whole 'stsd' box
//'avcC' decimal: 1635148611 or 97 118 99 67; hex: 61 76 63 43
workingFile.readBlockOfBytesP(stsd->contentOffset, buffer, count);
int32_t index = workingFile.findLastUInt(buffer, (int32_t) count, (uint32_t)'avcC'); //count casting to int32_t due to findLastUInt
if (index > 0) {
//read for fifth byte next to avcC,
//which is two fields of 6+2 bit, the last one - unsigned int(2) is lengthSizeMinusOne.
nalFld = buffer[sizeof((uint32_t)'avcC') + index +4] & (uint8_t) 0x03;
nalFld += (uint8_t) 1; //lengthSizeMinusOne +1 from avcC ISO 14496-15 2010
}
free(buffer);
}
return nalFld;
break;
case AV_CODEC_ID_HEVC:
nalFld = 2; //nal units in the hvcC always have length coded with 2 bytes; see hevc.c ffmpeg
return nalFld;
break;
default:
logMe(LOG_DBG, "NAL-size for codec not present.");
}
return 0; //assuming there is no NALs for this codec
}