-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4.cc
740 lines (663 loc) · 26.2 KB
/
mp4.cc
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
*
* This is port of OpenHttpStreamer for win32
* copyright (c) 2011 [email protected]
*
* Originally:
* copyright (c) 2010 ZAO Inventos (inventos.ru)
* copyright (c) 2010 [email protected]
* copyright (c) 2010 [email protected]
* copyright (c) 2010 [email protected]
*
* This file is part of mp4frag.
*
* mp4grag is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mp4frag is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "mp4.hh"
//#include <unistd.h>
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
#include <stdexcept>
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <memory.h>
#include <fcntl.h>
#include <errno.h>
#define MP4_CHECK(exp) if ( !(exp) ) { throw std::runtime_error(std::string("mp4 format error: ")); } else
using boost::make_shared;
namespace {
uint16_t __swab16(uint16_t x)
{
return x<<8 | x>>8;
}
uint32_t __swab32(uint32_t x)
{
return x<<24 | x>>24 |
(x & (uint32_t)0x0000ff00UL)<<8 |
(x & (uint32_t)0x00ff0000UL)>>8;
}
uint64_t __swab64(uint64_t x)
{
return x<<56 | x>>56 |
(x & (uint64_t)0x000000000000ff00ULL)<<40 |
(x & (uint64_t)0x0000000000ff0000ULL)<<24 |
(x & (uint64_t)0x00000000ff000000ULL)<< 8 |
(x & (uint64_t)0x000000ff00000000ULL)>> 8 |
(x & (uint64_t)0x0000ff0000000000ULL)>>24 |
(x & (uint64_t)0x00ff000000000000ULL)>>40;
}
}
namespace mp4 {
#define UINT8(addr) (*reinterpret_cast<const uint8_t*>(addr))
#define UINT16(addr) (__swab16(*reinterpret_cast<const uint16_t*>(addr)))
#define UINT32(addr) (__swab32(*reinterpret_cast<const uint32_t*>(addr)))
#define UINT64(addr) (__swab64(*reinterpret_cast<const uint64_t*>(addr)))
#define EQ(addr, TAG) ( memcmp(addr, TAG, 4) == 0)
void Context::push_state(const ::boost::shared_ptr<Parser>& p, unsigned total, unsigned wants) {
assert ( total >= wants );
p->_up = _parser;
p->_total = total;
p->_wants = wants;
p->_to_skip = 0;
p->_ctx = this;
_parser = p;
}
void Context::pop_state() {
// std::cerr << "pop_state\n";
_parser = _parser->_up;
}
Parser::~Parser() {}
struct TopLevel : public Parser {
virtual void parse(const char *data);
};
struct Moov : public Parser {
virtual void parse(const char *data);
};
struct Trak : public Parser {
virtual void parse(const char *data);
};
struct Mdia : public Parser {
virtual void parse(const char *data);
};
struct Tkhd : public Parser {
virtual void parse(const char *data);
};
struct Minf : public Parser {
// Minf(size_t total) : Parser(total) { _wants = 8; }
virtual void parse(const char *data);
};
struct Mdhd : public Parser {
// Mdhd(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Hdlr : public Parser {
// Hdlr(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Stbl : public Parser {
// Stbl(size_t total) : Parser(total) { _wants = 8; }
virtual void parse(const char *data);
};
struct Stsz : public Parser {
// Stsz(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Stco : public Parser {
// Stco(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Stss : public Parser {
// Stss(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Ctts : public Parser {
// Stss(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Stsc : public Parser {
// Stsc(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Co64 : public Parser {
// Co64(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Stts : public Parser {
// Stts(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
struct Stsd : public Parser {
// Stts(size_t total) : Parser(total) { _wants = total; }
virtual void parse(const char *data);
};
void TopLevel::parse(const char *data) {
uint32_t sz = UINT32(data);
// std::cerr << "TopLevel: " << sz << "," << _total << "," << std::string(data + 4, 4) << "\n";
MP4_CHECK( sz <= _total );
_total -= sz;
sz -= 8;
if ( EQ(data + 4, "moov") ) {
_ctx->push_state(make_shared<Moov>(), sz, 8);
}
else {
skip(sz);
}
}
void Moov::parse(const char *data) {
uint32_t sz = UINT32(data);
MP4_CHECK( _total >= sz );
_total -= sz;
sz -= 8;
// std::cerr << "Moov: " << sz << "," << _total << "," << std::string(data + 4, 4) << "\n";
if ( EQ(data+4, "trak") ) {
_ctx->push_state(make_shared<Trak>(), sz, 8);
_ctx->_current_parsed.reset(new Track);
}
else {
skip(sz);
}
}
void Trak::parse(const char *data) {
uint32_t sz = UINT32(data);
// std::cerr << "Trak: " << "sz=" << sz << ", total= " << _total << "," << std::string(data + 4, 4) << "\n";
MP4_CHECK( _total >= sz );
_total -= sz;
sz -= 8;
if ( EQ(data+4, "mdia") ) {
_ctx->push_state(make_shared<Mdia>(), sz, 8);
}
else if ( EQ(data + 4, "tkhd") ) {
_ctx->push_state(make_shared<Tkhd>(), sz, sz);
}
else {
skip(sz);
}
}
void Mdia::parse(const char *data) {
uint32_t sz = UINT32(data);
// std::cerr << "Mdia: " << sz << "," << _total << "," << std::string(data + 4, 4) << "\n";
MP4_CHECK( _total >= sz );
_total -= sz;
sz -= 8;
if ( EQ(data+4, "minf") ) {
_ctx->push_state(make_shared<Minf>(), sz, 8);
}
else if ( EQ(data+4, "mdhd") ) {
_ctx->push_state(make_shared<Mdhd>(), sz, sz);
}
else if ( EQ(data+4, "hdlr") ) {
_ctx->push_state(make_shared<Hdlr>(), sz, sz);
}
else {
skip(sz);
}
}
void Tkhd::parse(const char *data) {
uint16_t version = UINT8(data);
if ( version == 1 ) {
// LOG(lMp4, 5, Param("duration"), UINT64(data + 28));
data += 28 + 8;
}
else {
// std::cerr << "duration: " << UINT32(data + 20) << '\n';
data += 20 + 4;
}
_ctx->_current_parsed->_pwidth = UINT32(data + 52);
_ctx->_current_parsed->_pheight = UINT32(data + 56);
/*
if ( uint64_t pheight = _ctx->_current_parsed->_pheight ) {
// LOG(lMp4, 5, Param("aspect"), double(_ctx->_current_parsed->_pwidth)/pheight);
}
*/
_ctx->pop_state();
}
void Minf::parse(const char *data) {
uint32_t sz = UINT32(data);
_total -= sz;
sz -= 8;
if ( EQ(data+4, "stbl") ) {
_ctx->push_state(make_shared<Stbl>(), sz, 8);
}
else {
skip(sz);
}
}
void Stbl::parse(const char *data) {
uint32_t sz = UINT32(data);
// std::cerr << sz << "," << _total << "," << std::string(data + 4, 4) << "\n";
_total -= sz;
sz -= 8;
if ( EQ(data+4, "stsz") ) {
_ctx->push_state(make_shared<Stsz>(), sz, sz);
}
else if ( EQ(data+4, "stco") ) {
_ctx->push_state(make_shared<Stco>(), sz, sz);
}
else if ( EQ(data+4, "stss") ) {
_ctx->push_state(make_shared<Stss>(), sz, sz);
}
else if ( EQ(data+4, "stts") ) {
_ctx->push_state(make_shared<Stts>(), sz, sz);
}
else if ( EQ(data+4, "stsc") ) {
_ctx->push_state(make_shared<Stsc>(), sz, sz);
}
else if ( EQ(data+4, "co64") ) {
_ctx->push_state(make_shared<Co64>(), sz, sz);
}
else if ( EQ(data+4, "stsd") ) {
_ctx->push_state(make_shared<Stsd>(), sz, sz);
}
else if ( EQ(data+4, "ctts") ) {
_ctx->push_state(make_shared<Ctts>(), sz, sz);
}
else {
skip(sz);
}
}
void Mdhd::parse(const char *data) {
uint16_t version = UINT8(data);
if ( version == 1 ) {
MP4_CHECK ( _total == 4 + 28 + 4 );
_ctx->_current_parsed->_timescale = UINT32(data + 20);
_ctx->_current_parsed->_duration = UINT64(data + 24);
}
else {
MP4_CHECK ( version == 0 );
MP4_CHECK ( _total == 4 + 16 + 4 );
_ctx->_current_parsed->_timescale = UINT32(data + 12);
_ctx->_current_parsed->_duration = UINT32(data + 16);
}
// std::cerr << "mdhd::version=" << version << ", timescale=" << _ctx->_current_parsed->_timescale << ", duration=" << _ctx->_current_parsed->_duration << "\n";
_ctx->pop_state();
}
void Hdlr::parse(const char *data) {
MP4_CHECK ( _total > 24 );
if ( memcmp(data + 8, "vide", 4) == 0 ) {
_ctx->_video = _ctx->_current_parsed;
// LOG(lMp4, 5, "current_parsed is video");
}
else if ( memcmp(data + 8, "soun", 4) == 0 ) {
_ctx->_audio = _ctx->_current_parsed;
// LOG(lMp4, 5, "current_parsed is audio");
}
_ctx->pop_state();
}
void Stsz::parse(const char *data) {
MP4_CHECK ( _total >= 12 );
MP4_CHECK ( UINT16(data + 2) == 0 );
uint32_t sample_size = UINT32(data + 4);
uint32_t sample_count = UINT32(data + 8);
_ctx->_current_parsed->_sample_size.resize(0);
_ctx->_current_parsed->_sample_size.reserve(sample_count);
if ( sample_size == 0 ) {
data += 12;
for ( unsigned i = 0; i < sample_count; ++i ) {
_ctx->_current_parsed->_sample_size.push_back(UINT32(data));
data += 4;
}
}
else {
for ( unsigned i = 0; i < sample_count; ++i ) {
_ctx->_current_parsed->_sample_size.push_back(sample_size);
}
}
_ctx->pop_state();
}
void Ctts::parse(const char *data) {
MP4_CHECK ( _total >= 12 );
//MP4_CHECK ( UINT16(data + 2) == 0 );
uint32_t entry_count = UINT32(data + 4);
const char* cur_record = data + 8;
for (uint32_t i = 0; i < entry_count; ++i)
{
MP4_CHECK(_total >= unsigned(cur_record + 8 - data));
_ctx->_current_parsed->_compos_deltas.push_back(std::pair<uint32_t, uint32_t>(UINT32(cur_record), UINT32(cur_record + 4)));
cur_record += 8;
}
_ctx->pop_state();
}
void Stco::parse(const char *data) {
MP4_CHECK(UINT16(data) == 0);
MP4_CHECK(UINT16(data + 2) == 0);
uint32_t entry_count = UINT32(data + 4);
MP4_CHECK(_total == 8 + entry_count * 4);
_ctx->_current_parsed->_chunk_offsets.resize(0);
_ctx->_current_parsed->_chunk_offsets.reserve(entry_count);
data += 8;
for ( unsigned i = 0; i < entry_count; ++i ) {
_ctx->_current_parsed->_chunk_offsets.push_back(UINT32(data));
data += 4;
}
_ctx->pop_state();
}
void Co64::parse(const char *data) {
MP4_CHECK(UINT16(data) == 0);
MP4_CHECK(UINT16(data + 2) == 0);
uint32_t entry_count = UINT32(data + 4);
MP4_CHECK(_total == 8 + entry_count * 8);
_ctx->_current_parsed->_chunk_offsets.resize(0);
_ctx->_current_parsed->_chunk_offsets.reserve(entry_count);
data += 8;
for ( unsigned i = 0; i < entry_count; ++i ) {
_ctx->_current_parsed->_chunk_offsets.push_back(UINT64(data));
data += 8;
}
_ctx->pop_state();
}
void Stss::parse(const char *data) {
MP4_CHECK( UINT16(data) == 0 );
MP4_CHECK( UINT16(data + 2) == 0);
uint32_t entry_count = UINT32(data + 4);
MP4_CHECK( _total == 8 + entry_count * 4 );
_ctx->_current_parsed->_stss.resize(0);
_ctx->_current_parsed->_stss.reserve(entry_count);
data += 8;
for ( unsigned i = 0; i < entry_count; ++i ) {
_ctx->_current_parsed->_stss.push_back(UINT32(data));
data += 4;
}
_ctx->pop_state();
}
void Stts::parse(const char *data) {
MP4_CHECK( UINT16(data) == 0 );
MP4_CHECK( UINT16(data + 2) == 0 );
uint32_t entry_count = UINT32(data + 4);
MP4_CHECK( _total == 8 + entry_count * 8 );
_ctx->_current_parsed->_stts.resize(0);
_ctx->_current_parsed->_stts.reserve(entry_count);
data += 8;
for ( unsigned i = 0; i < entry_count; ++i ) {
_ctx->_current_parsed->_stts.push_back(std::make_pair(UINT32(data), UINT32(data + 4)));
data += 8;
}
_ctx->pop_state();
}
void Stsc::parse(const char *data) {
MP4_CHECK( UINT16(data) == 0 );
MP4_CHECK( UINT16(data + 2) == 0 );
uint32_t entry_count = UINT32(data + 4);
MP4_CHECK( _total == 8 + entry_count * 12 );
_ctx->_current_parsed->_samples_to_chunk.resize(0);
_ctx->_current_parsed->_samples_to_chunk.reserve(entry_count);
data += 8;
for ( unsigned i = 0; i < entry_count; ++i ) {
_ctx->_current_parsed->_samples_to_chunk.push_back(Track::SampleToChunk(UINT32(data), UINT32(data + 4), UINT32(data + 8)));
data += 12;
}
// push anchor:
_ctx->_current_parsed->_samples_to_chunk.push_back(Track::SampleToChunk(UINT_MAX, 0, 1));
_ctx->pop_state();
}
void Stsd::parse(const char *data) {
// uint16_t data_reference_index = UINT16(data + 6);
uint32_t entries = UINT32(data + 4);
data += 8;
const char *end = data + _total;
for ( uint32_t iii = 0; iii < entries; ++iii ) {
uint32_t sampleentrysize = UINT32(data);
// data, data+4 - size; data+4, data+8 - signature;
// std::cerr << "Stsd: " << sampleentrysize << ":" << std::string(data+4, data+8) << std::endl;
if ( _ctx->_current_parsed == _ctx->_video ) {
// parse videosampleentry
MP4_CHECK ( UINT16(data + 16) == 0 ); // pre_defined
MP4_CHECK ( UINT16(data + 18) == 0 ); // reserved
MP4_CHECK ( UINT32(data + 20) == 0 ); // pre_defined
MP4_CHECK ( UINT32(data + 24) == 0 ); // pre_defined
MP4_CHECK ( UINT32(data + 28) == 0 ); // pre_defined
_ctx->_width = UINT16(data + 32);
_ctx->_height = UINT16(data + 34);
uint32_t h_res = UINT32(data + 36);
MP4_CHECK(h_res == 0x480000);
uint32_t v_res = UINT32(data + 40);
MP4_CHECK(v_res == 0x480000);
MP4_CHECK ( UINT32(data + 44) == 0 ); // reserved
MP4_CHECK ( UINT16(data + 48) == 1 ); // frame_count
// string[32] - compressorname, all 0's
MP4_CHECK ( UINT16(data + 82) == 0x18 ); // depth
MP4_CHECK ( UINT16(data + 84) == 0xFFFFU ); // pre_defined
//
if ( data + 86 < end ) {
MP4_CHECK ( end - data >= 86 + 8 );
uint32_t cab_size = UINT32(data + 86);
std::vector<char> tmp(data + 94, data + 94 + cab_size - 8);
std::swap(_ctx->_video->_extradata, tmp);
// std::cerr << "avcC[0]=" << std::hex << unsigned(data[94]) << " avcC[4]=" << unsigned(data[98]) << std::dec << std::endl;
}
}
else if ( _ctx->_current_parsed == _ctx->_audio ) {
// parse audiosampleentry
uint32_t esds_size = UINT32(data + 36);
#if 0
std::cerr << "-- channelcount=" << UINT16(data + 24) << "\n"
<< "-- samplesize=" << UINT16(data + 26) << "\n"
<< "-- samplerate=" << (UINT32(data + 32) >> 16) << "\n"
<< "esds: " << esds_size << ":" << std::string(data + 40, 4) << "\n";
#endif
_ctx->_audio->_extradata.clear();
_ctx->_audio->_extradata.reserve(2);
_ctx->_audio->_extradata.push_back(data[36 + esds_size - 5]);
_ctx->_audio->_extradata.push_back(data[36 + esds_size - 4]);
}
data += sampleentrysize;
}
_ctx->pop_state();
}
struct VoidParser : public Parser {
void parse(const char *) {}
};
Context::Context() {
push_state(make_shared<VoidParser>(), 0, 0);
push_state(make_shared<TopLevel>(), UINT_MAX, 8);
}
Context::~Context() {
}
size_t Context::feed(const char *data, size_t sz) {
_buffer.insert(_buffer.end(), data, data + sz);
while ( 1 ) {
while ( _parser->_total == 0 ) {
if ( _parser->_wants == 0 ) { // признак верхушки стека, конец парсинга
return sz;
}
unsigned to_skip = _parser->_to_skip; // XXX BAD!
pop_state();
_parser->_to_skip = to_skip; // XXX BAD!
}
unsigned wants = _parser->_wants;
unsigned buffersize = _buffer.size();
// LOG(lMp4, 0, "feeding", Param("to_skip"), _parser->_to_skip, Param("wants"), wants, Param("buffersize"), buffersize);
if ( wants <= buffersize ) {
_parser->parse(&_buffer[0]);
// std::cerr << "after parsing skip=" << _parser->_to_skip << ", wants=" << wants << ", buffersize=" << buffersize << "\n";
unsigned to_skip = _parser->_to_skip + wants;
if ( long(to_skip) != long(_parser->_to_skip) + long(wants) ) { // overflow
// LOG(lMp4, 0, "overflow", Param("to_skip"), _parser->_to_skip, Param("wants"), wants, Param("buffersize"), buffersize);
}
if ( (unsigned long)(_parser->_to_skip) + (unsigned long)(wants) < buffersize ) {
assert( _parser->_to_skip < buffersize );
assert ( wants < buffersize );
_buffer.erase(_buffer.begin(), _buffer.begin() + to_skip);
assert ( _buffer.size() == buffersize - to_skip );
_parser->_to_skip = 0;
}
else {
_parser->_to_skip -= buffersize - wants;
_buffer.erase(_buffer.begin(), _buffer.end());
}
}
else {
break;
}
}
return sz;
}
void Context::_unfold(const ::boost::shared_ptr<Track>& track, bool is_video) {
uint32_t number = 0;
uint64_t current_time = 0;
typedef std::pair<uint64_t,uint64_t> stts_entry_type;
unsigned keyframe_index = 0;
std::vector<Track::SampleToChunk>::const_iterator chunk_run = track->_samples_to_chunk.begin();
uint32_t samples_per_chunk = chunk_run->_samples_per_chunk;
uint32_t current_chunk = 0;
uint32_t offset_in_current_chunk = 0;
uint32_t sample_number_in_chunk = 0;
++chunk_run;
assert ( chunk_run->_desc_index == 1 );
typedef std::pair<uint32_t,uint32_t> ctts_entry_type;
unsigned int order_n = 0;
std::vector<ctts_entry_type>::iterator cur_ctts_entry = track->_compos_deltas.begin();
BOOST_FOREACH( const stts_entry_type& entry, track->_stts ) {
for ( unsigned iii = 0; iii < entry.first; ++iii ) {
_samples.push_back(SampleInfo());
SampleInfo& last = _samples.back();
last._timestamp = current_time;
last._timescale = track->_timescale;
uint32_t compos_delta;
if (cur_ctts_entry != track->_compos_deltas.end())
{
compos_delta = cur_ctts_entry->second;
++order_n;
if (order_n >= cur_ctts_entry->first)
{
++cur_ctts_entry;
order_n = 0;
}
}
else
{
compos_delta = 0;
}
last._composition_offset = compos_delta;
uint32_t sample_size = track->_sample_size[number];
last._sample_size = sample_size;
last._offset = track->_chunk_offsets[current_chunk] + offset_in_current_chunk;
if ( ++sample_number_in_chunk < samples_per_chunk ) {
offset_in_current_chunk += sample_size;
}
else {
sample_number_in_chunk = 0;
offset_in_current_chunk = 0;
if ( ++current_chunk + 1 >= chunk_run->_first_chunk ) {
assert ( current_chunk + 1 == chunk_run->_first_chunk );
samples_per_chunk = chunk_run->_samples_per_chunk;
++chunk_run;
assert ( chunk_run->_desc_index == 1 );
}
}
last._video = is_video;
if ( is_video && keyframe_index < track->_stss.size() && track->_stss[keyframe_index] == number + 1 ) {
last._keyframe = true;
++keyframe_index;
}
else {
last._keyframe = false;
}
++number;
current_time += entry.second;
}
}
}
namespace {
struct SampleSorter {
bool operator()(const SampleInfo& sample1, const SampleInfo& sample2) const {
return double(sample1._timestamp) / sample1._timescale < double(sample2._timestamp) / sample2._timescale ||
( double(sample1._timestamp) / sample1._timescale == double(sample2._timestamp) / sample2._timescale &&
sample1._offset < sample2._offset );
}
bool operator()(const SampleInfo& sample1, double timestamp) const {
return double(sample1._timestamp) / sample1._timescale < timestamp ;
}
bool operator()(double timestamp, const SampleInfo& sample2) const {
return timestamp < double(sample2._timestamp) / sample2._timescale ;
}
};
}
void Context::finalize() {
if ( _video ) _unfold(_video, true);
if ( _audio ) _unfold(_audio, false);
std::sort(_samples.begin(), _samples.end(), SampleSorter());
// XXX
uint64_t nbits = 0;
double maxtime = 0.0;
BOOST_FOREACH(SampleInfo& si, _samples) {
nbits += si._sample_size;
double ts = si.timestamp();
if ( ts > maxtime ) {
maxtime = ts;
}
}
_bitrate = nbits * 8.0 / maxtime;
// std::cerr << "maxtime=" << maxtime << std::endl;
// XXX
#if 0
unsigned count = 0;
for ( ::std::vector<SampleInfo>::const_iterator it = _samples.begin(); it < _samples.end() && count < 30; ++it, ++count) {
if ( it->_video ) {
std::cerr << "---" << *it << std::endl;
}
}
#endif
_duration = 0.0;
if ( (_has_video = _video) ) {
_duration = double(_video->_duration) / _video->_timescale;
_pwidth = _video->_pwidth;
_pheight = _video->_pheight;
_videoextra.assign(&_video->_extradata[0], _video->_extradata.size());
_video.reset();
}
if ( (_has_audio = _audio) ) {
double audio_duration = double(_audio->_duration) / _audio->_timescale;
if ( _duration < audio_duration ) {
_duration = audio_duration;
}
_audioextra.assign(&_audio->_extradata[0], _audio->_extradata.size());
_audio.reset();
}
_current_parsed.reset();
}
unsigned Context::find_sample(double timestamp) {
//decltype(_samples.begin()) iter = ::std::lower_bound(_samples.begin(), _samples.end(), timestamp, SampleSorter());
// if ( iter != _samples.end() ) {
// if ( iter == _samples.begin() ) {
// return 0;
// }
// if ( double(iter->_timestamp) / iter->_timescale < timestamp ) {
// ++iter;
// }
// return iter - _samples.begin();
// }
return _samples.size();
}
} // namespace mp4
::std::ostream& operator<<(::std::ostream& out, const mp4::Track& t) {
return
out << "-- timescale = " << t._timescale << "\n"
<< "-- sample_size: " << t._sample_size.size() << " entries\n"
<< "-- chunk_offsets: " << t._chunk_offsets.size() << " entries\n"
<< "-- stss: " << t._stss.size() << " entries\n"
<< "-- stts: " << t._stts.size() << " entries\n"
<< "-- sample_to_chunk: " << t._samples_to_chunk.size() << " entries;\n"
<< "-- presentation width: " << t._pwidth << ";\n"
<< "-- presentation height: " << t._pheight << ";\n"
<< "\n";
}
::std::ostream& operator<<(::std::ostream& o, const mp4::SampleInfo& si) {
return o
<< "[ " << si._timestamp << "/"
<< si._timescale << "="
<< si.timestamp() << ", "
// << si._number << ", "
<< si._sample_size << ", "
<< si._offset << ", "
<< si._video << ", "
<< si._keyframe << " ]";
}