-
Notifications
You must be signed in to change notification settings - Fork 18
/
minimp4.h
3502 lines (3143 loc) · 122 KB
/
minimp4.h
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef MINIMP4_H
#define MINIMP4_H
/*
https://github.com/aspt/mp4
https://github.com/lieff/minimp4
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include "time_util.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MINIMP4_MIN(x, y) ((x) < (y) ? (x) : (y))
/************************************************************************/
/* Build configuration */
/************************************************************************/
#define FIX_BAD_ANDROID_META_BOX 1
#define MAX_CHUNKS_DEPTH 64 // Max chunks nesting level
#define MINIMP4_MAX_SPS 32
#define MINIMP4_MAX_PPS 256
#define MINIMP4_TRANSCODE_SPS_ID 0
// Support indexing of MP4 files over 4 GB.
// If disabled, files with 64-bit offset fields is still supported,
// but error signaled if such field contains too big offset
// This switch affect return type of MP4D_frame_offset() function
#define MINIMP4_ALLOW_64BIT 1
#define MP4D_TRACE_SUPPORTED 0 // Debug trace
#define MP4D_TRACE_TIMESTAMPS 1
// Support parsing of supplementary information, not necessary for decoding:
// duration, language, bitrate, metadata tags, etc
#define MP4D_INFO_SUPPORTED 1
// Enable code, which prints to stdout supplementary MP4 information:
#define MP4D_PRINT_INFO_SUPPORTED 0
#define MP4D_AVC_SUPPORTED 1
#define MP4D_HEVC_SUPPORTED 1
#define MP4D_TIMESTAMPS_SUPPORTED 1
// Enable TrackFragmentBaseMediaDecodeTimeBox support
#define MP4D_TFDT_SUPPORT 0
/************************************************************************/
/* Some values of MP4(E/D)_track_t->object_type_indication */
/************************************************************************/
// MPEG-4 AAC (all profiles)
#define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_14496_3 0x40
// MPEG-2 AAC, Main profile
#define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_13818_7_MAIN_PROFILE 0x66
// MPEG-2 AAC, LC profile
#define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_13818_7_LC_PROFILE 0x67
// MPEG-2 AAC, SSR profile
#define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_13818_7_SSR_PROFILE 0x68
// H.264 (AVC) video
#define MP4_OBJECT_TYPE_AVC 0x21
// H.265 (HEVC) video
#define MP4_OBJECT_TYPE_HEVC 0x23
// http://www.mp4ra.org/object.html 0xC0-E0 && 0xE2 - 0xFE are specified as "user private"
#define MP4_OBJECT_TYPE_USER_PRIVATE 0xC0
/************************************************************************/
/* API error codes */
/************************************************************************/
#define MP4E_STATUS_OK 0
#define MP4E_STATUS_BAD_ARGUMENTS -1
#define MP4E_STATUS_NO_MEMORY -2
#define MP4E_STATUS_FILE_WRITE_ERROR -3
#define MP4E_STATUS_ONLY_ONE_DSI_ALLOWED -4
/************************************************************************/
/* Sample kind for MP4E_put_sample() */
/************************************************************************/
#define MP4E_SAMPLE_DEFAULT 0 // (beginning of) audio or video frame
#define MP4E_SAMPLE_RANDOM_ACCESS 1 // mark sample as random access point (key frame)
#define MP4E_SAMPLE_CONTINUATION 2 // Not a sample, but continuation of previous sample (new slice)
/************************************************************************/
/* Portable 64-bit type definition */
/************************************************************************/
#if MINIMP4_ALLOW_64BIT
typedef uint64_t boxsize_t;
#else
typedef unsigned int boxsize_t;
#endif
typedef boxsize_t MP4D_file_offset_t;
/************************************************************************/
/* Some values of MP4D_track_t->handler_type */
/************************************************************************/
// Video track : 'vide'
#define MP4D_HANDLER_TYPE_VIDE 0x76696465
// Audio track : 'soun'
#define MP4D_HANDLER_TYPE_SOUN 0x736F756E
// General MPEG-4 systems streams (without specific handler).
// Used for private stream, as suggested in http://www.mp4ra.org/handler.html
#define MP4E_HANDLER_TYPE_GESM 0x6765736D
#define HEVC_NAL_VPS 32
#define HEVC_NAL_SPS 33
#define HEVC_NAL_PPS 34
#define HEVC_NAL_BLA_W_LP 16
#define HEVC_NAL_CRA_NUT 21
/************************************************************************/
/* Data structures */
/************************************************************************/
typedef struct MP4E_mux_tag MP4E_mux_t;
typedef enum
{
e_audio,
e_video,
e_private
} track_media_kind_t;
typedef struct
{
// MP4 object type code, which defined codec class for the track.
// See MP4E_OBJECT_TYPE_* values for some codecs
unsigned object_type_indication;
// Track language: 3-char ISO 639-2T code: "und", "eng", "rus", "jpn" etc...
unsigned char language[4];
track_media_kind_t track_media_kind;
// 90000 for video, sample rate for audio
unsigned time_scale;
unsigned default_duration;
union
{
struct
{
// number of channels in the audio track.
unsigned channelcount;
} a;
struct
{
int width;
int height;
} v;
} u;
} MP4E_track_t;
typedef struct MP4D_sample_to_chunk_t_tag MP4D_sample_to_chunk_t;
typedef struct
{
/************************************************************************/
/* mandatory public data */
/************************************************************************/
// How many 'samples' in the track
// The 'sample' is MP4 term, denoting audio or video frame
unsigned sample_count;
// Decoder-specific info (DSI) data
unsigned char *dsi;
// DSI data size
unsigned dsi_bytes;
// MP4 object type code
// case 0x00: return "Forbidden";
// case 0x01: return "Systems ISO/IEC 14496-1";
// case 0x02: return "Systems ISO/IEC 14496-1";
// case 0x20: return "Visual ISO/IEC 14496-2";
// case 0x40: return "Audio ISO/IEC 14496-3";
// case 0x60: return "Visual ISO/IEC 13818-2 Simple Profile";
// case 0x61: return "Visual ISO/IEC 13818-2 Main Profile";
// case 0x62: return "Visual ISO/IEC 13818-2 SNR Profile";
// case 0x63: return "Visual ISO/IEC 13818-2 Spatial Profile";
// case 0x64: return "Visual ISO/IEC 13818-2 High Profile";
// case 0x65: return "Visual ISO/IEC 13818-2 422 Profile";
// case 0x66: return "Audio ISO/IEC 13818-7 Main Profile";
// case 0x67: return "Audio ISO/IEC 13818-7 LC Profile";
// case 0x68: return "Audio ISO/IEC 13818-7 SSR Profile";
// case 0x69: return "Audio ISO/IEC 13818-3";
// case 0x6A: return "Visual ISO/IEC 11172-2";
// case 0x6B: return "Audio ISO/IEC 11172-3";
// case 0x6C: return "Visual ISO/IEC 10918-1";
unsigned object_type_indication;
#if MP4D_INFO_SUPPORTED
/************************************************************************/
/* informational public data */
/************************************************************************/
// handler_type when present in a media box, is an integer containing one of
// the following values, or a value from a derived specification:
// 'vide' Video track
// 'soun' Audio track
// 'hint' Hint track
unsigned handler_type;
// Track duration: 64-bit value split into 2 variables
unsigned duration_hi;
unsigned duration_lo;
// duration scale: duration = timescale*seconds
unsigned timescale;
// Average bitrate, bits per second
unsigned avg_bitrate_bps;
// Track language: 3-char ISO 639-2T code: "und", "eng", "rus", "jpn" etc...
unsigned char language[4];
// MP4 stream type
// case 0x00: return "Forbidden";
// case 0x01: return "ObjectDescriptorStream";
// case 0x02: return "ClockReferenceStream";
// case 0x03: return "SceneDescriptionStream";
// case 0x04: return "VisualStream";
// case 0x05: return "AudioStream";
// case 0x06: return "MPEG7Stream";
// case 0x07: return "IPMPStream";
// case 0x08: return "ObjectContentInfoStream";
// case 0x09: return "MPEGJStream";
unsigned stream_type;
union
{
// for handler_type == 'soun' tracks
struct
{
unsigned channelcount;
unsigned samplerate_hz;
} audio;
// for handler_type == 'vide' tracks
struct
{
unsigned width;
unsigned height;
} video;
} SampleDescription;
#endif
/************************************************************************/
/* private data: MP4 indexes */
/************************************************************************/
unsigned *entry_size;
unsigned sample_to_chunk_count;
struct MP4D_sample_to_chunk_t_tag *sample_to_chunk;
unsigned chunk_count;
MP4D_file_offset_t *chunk_offset;
#if MP4D_TIMESTAMPS_SUPPORTED
unsigned *timestamp;
unsigned *duration;
#endif
} MP4D_track_t;
typedef struct MP4D_demux_tag
{
/************************************************************************/
/* mandatory public data */
/************************************************************************/
int64_t read_pos;
int64_t read_size;
MP4D_track_t *track;
int (*read_callback)(int64_t offset, void *buffer, size_t size, void *token);
void *token;
unsigned track_count; // number of tracks in the movie
#if MP4D_INFO_SUPPORTED
/************************************************************************/
/* informational public data */
/************************************************************************/
// Movie duration: 64-bit value split into 2 variables
unsigned duration_hi;
unsigned duration_lo;
// duration scale: duration = timescale*seconds
unsigned timescale;
// Metadata tag (optional)
// Tags provided 'as-is', without any re-encoding
struct
{
unsigned char *title;
unsigned char *artist;
unsigned char *album;
unsigned char *year;
unsigned char *comment;
unsigned char *genre;
} tag;
#endif
} MP4D_demux_t;
struct MP4D_sample_to_chunk_t_tag
{
unsigned first_chunk;
unsigned samples_per_chunk;
};
typedef struct
{
void *sps_cache[MINIMP4_MAX_SPS];
void *pps_cache[MINIMP4_MAX_PPS];
int sps_bytes[MINIMP4_MAX_SPS];
int pps_bytes[MINIMP4_MAX_PPS];
int map_sps[MINIMP4_MAX_SPS];
int map_pps[MINIMP4_MAX_PPS];
} h264_sps_id_patcher_t;
typedef struct mp4_h26x_writer_tag
{
#if MINIMP4_TRANSCODE_SPS_ID
h264_sps_id_patcher_t sps_patcher;
#endif
MP4E_mux_t *mux;
int mux_track_id, is_hevc, need_vps, need_sps, need_pps, need_idr;
} mp4_h26x_writer_t;
int mp4_h26x_write_init(mp4_h26x_writer_t *h, MP4E_mux_t *mux, int width, int height, int is_hevc);
void mp4_h26x_write_close(mp4_h26x_writer_t *h);
int mp4_h26x_write_nal(mp4_h26x_writer_t *h, const unsigned char *nal, int length, unsigned timeStamp90kHz_next);
/************************************************************************/
/* API */
/************************************************************************/
/**
* Parse given input stream as MP4 file. Allocate and store data indexes.
* return 1 on success, 0 on failure
* The MP4 indexes may be stored at the end of stream, so this
* function may parse all stream.
* It is guaranteed that function will read/seek sequentially,
* and will never jump back.
*/
int MP4D_open(MP4D_demux_t *mp4, int (*read_callback)(int64_t offset, void *buffer, size_t size, void *token), void *token, int64_t file_size);
/**
* Return position and size for given sample from given track. The 'sample' is a
* MP4 term for 'frame'
*
* frame_bytes [OUT] - return coded frame size in bytes
* timestamp [OUT] - return frame timestamp (in mp4->timescale units)
* duration [OUT] - return frame duration (in mp4->timescale units)
*
* function return offset for the frame
*/
MP4D_file_offset_t MP4D_frame_offset(const MP4D_demux_t *mp4, unsigned int ntrack, unsigned int nsample, unsigned int *frame_bytes, unsigned *timestamp, unsigned *duration);
/**
* De-allocated memory
*/
void MP4D_close(MP4D_demux_t *mp4);
/**
* Helper functions to parse mp4.track[ntrack].dsi for H.264 SPS/PPS
* Return pointer to internal mp4 memory, it must not be free()-ed
*
* Example: process all SPS in MP4 file:
* while (sps = MP4D_read_sps(mp4, num_of_avc_track, sps_count, &sps_bytes))
* {
* process(sps, sps_bytes);
* sps_count++;
* }
*/
const void *MP4D_read_sps(const MP4D_demux_t *mp4, unsigned int ntrack, int nsps, int *sps_bytes);
const void *MP4D_read_pps(const MP4D_demux_t *mp4, unsigned int ntrack, int npps, int *pps_bytes);
#if MP4D_PRINT_INFO_SUPPORTED
/**
* Print MP4 information to stdout.
* Uses printf() as well as floating-point functions
* Given as implementation example and for test purposes
*/
void MP4D_printf_info(const MP4D_demux_t *mp4);
#endif
/**
* Allocates and initialize mp4 multiplexor
* Given file handler is transparent to the MP4 library, and used only as
* argument for given fwrite_callback() function. By appropriate definition
* of callback function application may use any other file output API (for
* example C++ streams, or Win32 file functions)
*
* return multiplexor handle on success; NULL on failure
*/
MP4E_mux_t *MP4E_open(int sequential_mode_flag, int enable_fragmentation, void *token,
int (*write_callback)(int64_t offset, const void *buffer, size_t size, void *token));
/**
* Add new track
* The track_data parameter does not referred by the multiplexer after function
* return, and may be allocated in short-time memory. The dsi member of
* track_data parameter is mandatory.
*
* return ID of added track, or error code MP4E_STATUS_*
*/
int MP4E_add_track(MP4E_mux_t *mux, const MP4E_track_t *track_data);
/**
* Add new sample to specified track
* The tracks numbered starting with 0, according to order of MP4E_add_track() calls
* 'kind' is one of MP4E_SAMPLE_... defines
*
* return error code MP4E_STATUS_*
*
* Example:
* MP4E_put_sample(mux, 0, data, data_bytes, duration, MP4E_SAMPLE_DEFAULT);
*/
int MP4E_put_sample(MP4E_mux_t *mux, int track_num, const void *data, int data_bytes, int duration, int kind);
/**
* Finalize MP4 file, de-allocated memory, and closes MP4 multiplexer.
* The close operation takes a time and disk space, since it writes MP4 file
* indexes. Please note that this function does not closes file handle,
* which was passed to open function.
*
* return error code MP4E_STATUS_*
*/
int MP4E_close(MP4E_mux_t *mux);
/**
* Set Decoder Specific Info (DSI)
* Can be used for audio and private tracks.
* MUST be used for AAC track.
* Only one DSI can be set. It is an error to set DSI again
*
* return error code MP4E_STATUS_*
*/
int MP4E_set_dsi(MP4E_mux_t *mux, int track_id, const void *dsi, int bytes);
/**
* Set VPS data. MUST be used for HEVC (H.265) track.
*
* return error code MP4E_STATUS_*
*/
int MP4E_set_vps(MP4E_mux_t *mux, int track_id, const void *vps, int bytes);
/**
* Set SPS data. MUST be used for AVC (H.264) track. Up to 32 different SPS can be used in one track.
*
* return error code MP4E_STATUS_*
*/
int MP4E_set_sps(MP4E_mux_t *mux, int track_id, const void *sps, int bytes);
/**
* Set PPS data. MUST be used for AVC (H.264) track. Up to 256 different PPS can be used in one track.
*
* return error code MP4E_STATUS_*
*/
int MP4E_set_pps(MP4E_mux_t *mux, int track_id, const void *pps, int bytes);
/**
* Set or replace ASCII test comment for the file. Set comment to NULL to remove comment.
*
* return error code MP4E_STATUS_*
*/
int MP4E_set_text_comment(MP4E_mux_t *mux, const char *comment);
#ifdef __cplusplus
}
#endif
#endif //MINIMP4_H
#define FOUR_CHAR_INT(a, b, c, d) (((uint32_t)(a) << 24) | ((b) << 16) | ((c) << 8) | (d))
enum
{
BOX_co64 = FOUR_CHAR_INT( 'c', 'o', '6', '4' ),//ChunkLargeOffsetAtomType
BOX_stco = FOUR_CHAR_INT( 's', 't', 'c', 'o' ),//ChunkOffsetAtomType
BOX_crhd = FOUR_CHAR_INT( 'c', 'r', 'h', 'd' ),//ClockReferenceMediaHeaderAtomType
BOX_ctts = FOUR_CHAR_INT( 'c', 't', 't', 's' ),//CompositionOffsetAtomType
BOX_cprt = FOUR_CHAR_INT( 'c', 'p', 'r', 't' ),//CopyrightAtomType
BOX_url_ = FOUR_CHAR_INT( 'u', 'r', 'l', ' ' ),//DataEntryURLAtomType
BOX_urn_ = FOUR_CHAR_INT( 'u', 'r', 'n', ' ' ),//DataEntryURNAtomType
BOX_dinf = FOUR_CHAR_INT( 'd', 'i', 'n', 'f' ),//DataInformationAtomType
BOX_dref = FOUR_CHAR_INT( 'd', 'r', 'e', 'f' ),//DataReferenceAtomType
BOX_stdp = FOUR_CHAR_INT( 's', 't', 'd', 'p' ),//DegradationPriorityAtomType
BOX_edts = FOUR_CHAR_INT( 'e', 'd', 't', 's' ),//EditAtomType
BOX_elst = FOUR_CHAR_INT( 'e', 'l', 's', 't' ),//EditListAtomType
BOX_uuid = FOUR_CHAR_INT( 'u', 'u', 'i', 'd' ),//ExtendedAtomType
BOX_free = FOUR_CHAR_INT( 'f', 'r', 'e', 'e' ),//FreeSpaceAtomType
BOX_hdlr = FOUR_CHAR_INT( 'h', 'd', 'l', 'r' ),//HandlerAtomType
BOX_hmhd = FOUR_CHAR_INT( 'h', 'm', 'h', 'd' ),//HintMediaHeaderAtomType
BOX_hint = FOUR_CHAR_INT( 'h', 'i', 'n', 't' ),//HintTrackReferenceAtomType
BOX_mdia = FOUR_CHAR_INT( 'm', 'd', 'i', 'a' ),//MediaAtomType
BOX_mdat = FOUR_CHAR_INT( 'm', 'd', 'a', 't' ),//MediaDataAtomType
BOX_mdhd = FOUR_CHAR_INT( 'm', 'd', 'h', 'd' ),//MediaHeaderAtomType
BOX_minf = FOUR_CHAR_INT( 'm', 'i', 'n', 'f' ),//MediaInformationAtomType
BOX_moov = FOUR_CHAR_INT( 'm', 'o', 'o', 'v' ),//MovieAtomType
BOX_mvhd = FOUR_CHAR_INT( 'm', 'v', 'h', 'd' ),//MovieHeaderAtomType
BOX_stsd = FOUR_CHAR_INT( 's', 't', 's', 'd' ),//SampleDescriptionAtomType
BOX_stsz = FOUR_CHAR_INT( 's', 't', 's', 'z' ),//SampleSizeAtomType
BOX_stz2 = FOUR_CHAR_INT( 's', 't', 'z', '2' ),//CompactSampleSizeAtomType
BOX_stbl = FOUR_CHAR_INT( 's', 't', 'b', 'l' ),//SampleTableAtomType
BOX_stsc = FOUR_CHAR_INT( 's', 't', 's', 'c' ),//SampleToChunkAtomType
BOX_stsh = FOUR_CHAR_INT( 's', 't', 's', 'h' ),//ShadowSyncAtomType
BOX_skip = FOUR_CHAR_INT( 's', 'k', 'i', 'p' ),//SkipAtomType
BOX_smhd = FOUR_CHAR_INT( 's', 'm', 'h', 'd' ),//SoundMediaHeaderAtomType
BOX_stss = FOUR_CHAR_INT( 's', 't', 's', 's' ),//SyncSampleAtomType
BOX_stts = FOUR_CHAR_INT( 's', 't', 't', 's' ),//TimeToSampleAtomType
BOX_trak = FOUR_CHAR_INT( 't', 'r', 'a', 'k' ),//TrackAtomType
BOX_tkhd = FOUR_CHAR_INT( 't', 'k', 'h', 'd' ),//TrackHeaderAtomType
BOX_tref = FOUR_CHAR_INT( 't', 'r', 'e', 'f' ),//TrackReferenceAtomType
BOX_udta = FOUR_CHAR_INT( 'u', 'd', 't', 'a' ),//UserDataAtomType
BOX_vmhd = FOUR_CHAR_INT( 'v', 'm', 'h', 'd' ),//VideoMediaHeaderAtomType
BOX_url = FOUR_CHAR_INT( 'u', 'r', 'l', ' ' ),
BOX_urn = FOUR_CHAR_INT( 'u', 'r', 'n', ' ' ),
BOX_gnrv = FOUR_CHAR_INT( 'g', 'n', 'r', 'v' ),//GenericVisualSampleEntryAtomType
BOX_gnra = FOUR_CHAR_INT( 'g', 'n', 'r', 'a' ),//GenericAudioSampleEntryAtomType
//V2 atoms
BOX_ftyp = FOUR_CHAR_INT( 'f', 't', 'y', 'p' ),//FileTypeAtomType
BOX_padb = FOUR_CHAR_INT( 'p', 'a', 'd', 'b' ),//PaddingBitsAtomType
//MP4 Atoms
BOX_sdhd = FOUR_CHAR_INT( 's', 'd', 'h', 'd' ),//SceneDescriptionMediaHeaderAtomType
BOX_dpnd = FOUR_CHAR_INT( 'd', 'p', 'n', 'd' ),//StreamDependenceAtomType
BOX_iods = FOUR_CHAR_INT( 'i', 'o', 'd', 's' ),//ObjectDescriptorAtomType
BOX_odhd = FOUR_CHAR_INT( 'o', 'd', 'h', 'd' ),//ObjectDescriptorMediaHeaderAtomType
BOX_mpod = FOUR_CHAR_INT( 'm', 'p', 'o', 'd' ),//ODTrackReferenceAtomType
BOX_nmhd = FOUR_CHAR_INT( 'n', 'm', 'h', 'd' ),//MPEGMediaHeaderAtomType
BOX_esds = FOUR_CHAR_INT( 'e', 's', 'd', 's' ),//ESDAtomType
BOX_sync = FOUR_CHAR_INT( 's', 'y', 'n', 'c' ),//OCRReferenceAtomType
BOX_ipir = FOUR_CHAR_INT( 'i', 'p', 'i', 'r' ),//IPIReferenceAtomType
BOX_mp4s = FOUR_CHAR_INT( 'm', 'p', '4', 's' ),//MPEGSampleEntryAtomType
BOX_mp4a = FOUR_CHAR_INT( 'm', 'p', '4', 'a' ),//MPEGAudioSampleEntryAtomType
BOX_mp4v = FOUR_CHAR_INT( 'm', 'p', '4', 'v' ),//MPEGVisualSampleEntryAtomType
// http://www.itscj.ipsj.or.jp/sc29/open/29view/29n7644t.doc
BOX_avc1 = FOUR_CHAR_INT( 'a', 'v', 'c', '1' ),
BOX_avc2 = FOUR_CHAR_INT( 'a', 'v', 'c', '2' ),
BOX_svc1 = FOUR_CHAR_INT( 's', 'v', 'c', '1' ),
BOX_avcC = FOUR_CHAR_INT( 'a', 'v', 'c', 'C' ),
BOX_svcC = FOUR_CHAR_INT( 's', 'v', 'c', 'C' ),
BOX_btrt = FOUR_CHAR_INT( 'b', 't', 'r', 't' ),
BOX_m4ds = FOUR_CHAR_INT( 'm', '4', 'd', 's' ),
BOX_seib = FOUR_CHAR_INT( 's', 'e', 'i', 'b' ),
// H264/HEVC
BOX_hev1 = FOUR_CHAR_INT( 'h', 'e', 'v', '1' ),
BOX_hvc1 = FOUR_CHAR_INT( 'h', 'v', 'c', '1' ),
BOX_hvcC = FOUR_CHAR_INT( 'h', 'v', 'c', 'C' ),
//3GPP atoms
BOX_samr = FOUR_CHAR_INT( 's', 'a', 'm', 'r' ),//AMRSampleEntryAtomType
BOX_sawb = FOUR_CHAR_INT( 's', 'a', 'w', 'b' ),//WB_AMRSampleEntryAtomType
BOX_damr = FOUR_CHAR_INT( 'd', 'a', 'm', 'r' ),//AMRConfigAtomType
BOX_s263 = FOUR_CHAR_INT( 's', '2', '6', '3' ),//H263SampleEntryAtomType
BOX_d263 = FOUR_CHAR_INT( 'd', '2', '6', '3' ),//H263ConfigAtomType
//V2 atoms - Movie Fragments
BOX_mvex = FOUR_CHAR_INT( 'm', 'v', 'e', 'x' ),//MovieExtendsAtomType
BOX_trex = FOUR_CHAR_INT( 't', 'r', 'e', 'x' ),//TrackExtendsAtomType
BOX_moof = FOUR_CHAR_INT( 'm', 'o', 'o', 'f' ),//MovieFragmentAtomType
BOX_mfhd = FOUR_CHAR_INT( 'm', 'f', 'h', 'd' ),//MovieFragmentHeaderAtomType
BOX_traf = FOUR_CHAR_INT( 't', 'r', 'a', 'f' ),//TrackFragmentAtomType
BOX_tfhd = FOUR_CHAR_INT( 't', 'f', 'h', 'd' ),//TrackFragmentHeaderAtomType
BOX_tfdt = FOUR_CHAR_INT( 't', 'f', 'd', 't' ),//TrackFragmentBaseMediaDecodeTimeBox
BOX_trun = FOUR_CHAR_INT( 't', 'r', 'u', 'n' ),//TrackFragmentRunAtomType
BOX_mehd = FOUR_CHAR_INT( 'm', 'e', 'h', 'd' ),//MovieExtendsHeaderBox
// Object Descriptors (OD) data coding
// These takes only 1 byte; this implementation translate <od_tag> to
// <od_tag> + OD_BASE to keep API uniform and safe for string functions
OD_BASE = FOUR_CHAR_INT( '$', '$', '$', '0' ),//
OD_ESD = FOUR_CHAR_INT( '$', '$', '$', '3' ),//SDescriptor_Tag
OD_DCD = FOUR_CHAR_INT( '$', '$', '$', '4' ),//DecoderConfigDescriptor_Tag
OD_DSI = FOUR_CHAR_INT( '$', '$', '$', '5' ),//DecoderSpecificInfo_Tag
OD_SLC = FOUR_CHAR_INT( '$', '$', '$', '6' ),//SLConfigDescriptor_Tag
BOX_meta = FOUR_CHAR_INT( 'm', 'e', 't', 'a' ),
BOX_ilst = FOUR_CHAR_INT( 'i', 'l', 's', 't' ),
// Metagata tags, see http://atomicparsley.sourceforge.net/mpeg-4files.html
BOX_calb = FOUR_CHAR_INT( '\xa9', 'a', 'l', 'b'), // album
BOX_cart = FOUR_CHAR_INT( '\xa9', 'a', 'r', 't'), // artist
BOX_aART = FOUR_CHAR_INT( 'a', 'A', 'R', 'T' ), // album artist
BOX_ccmt = FOUR_CHAR_INT( '\xa9', 'c', 'm', 't'), // comment
BOX_cday = FOUR_CHAR_INT( '\xa9', 'd', 'a', 'y'), // year (as string)
BOX_cnam = FOUR_CHAR_INT( '\xa9', 'n', 'a', 'm'), // title
BOX_cgen = FOUR_CHAR_INT( '\xa9', 'g', 'e', 'n'), // custom genre (as string or as byte!)
BOX_trkn = FOUR_CHAR_INT( 't', 'r', 'k', 'n'), // track number (byte)
BOX_disk = FOUR_CHAR_INT( 'd', 'i', 's', 'k'), // disk number (byte)
BOX_cwrt = FOUR_CHAR_INT( '\xa9', 'w', 'r', 't'), // composer
BOX_ctoo = FOUR_CHAR_INT( '\xa9', 't', 'o', 'o'), // encoder
BOX_tmpo = FOUR_CHAR_INT( 't', 'm', 'p', 'o'), // bpm (byte)
BOX_cpil = FOUR_CHAR_INT( 'c', 'p', 'i', 'l'), // compilation (byte)
BOX_covr = FOUR_CHAR_INT( 'c', 'o', 'v', 'r'), // cover art (JPEG/PNG)
BOX_rtng = FOUR_CHAR_INT( 'r', 't', 'n', 'g'), // rating/advisory (byte)
BOX_cgrp = FOUR_CHAR_INT( '\xa9', 'g', 'r', 'p'), // grouping
BOX_stik = FOUR_CHAR_INT( 's', 't', 'i', 'k'), // stik (byte) 0 = Movie 1 = Normal 2 = Audiobook 5 = Whacked Bookmark 6 = Music Video 9 = Short Film 10 = TV Show 11 = Booklet 14 = Ringtone
BOX_pcst = FOUR_CHAR_INT( 'p', 'c', 's', 't'), // podcast (byte)
BOX_catg = FOUR_CHAR_INT( 'c', 'a', 't', 'g'), // category
BOX_keyw = FOUR_CHAR_INT( 'k', 'e', 'y', 'w'), // keyword
BOX_purl = FOUR_CHAR_INT( 'p', 'u', 'r', 'l'), // podcast URL (byte)
BOX_egid = FOUR_CHAR_INT( 'e', 'g', 'i', 'd'), // episode global unique ID (byte)
BOX_desc = FOUR_CHAR_INT( 'd', 'e', 's', 'c'), // description
BOX_clyr = FOUR_CHAR_INT( '\xa9', 'l', 'y', 'r'), // lyrics (may be > 255 bytes)
BOX_tven = FOUR_CHAR_INT( 't', 'v', 'e', 'n'), // tv episode number
BOX_tves = FOUR_CHAR_INT( 't', 'v', 'e', 's'), // tv episode (byte)
BOX_tvnn = FOUR_CHAR_INT( 't', 'v', 'n', 'n'), // tv network name
BOX_tvsh = FOUR_CHAR_INT( 't', 'v', 's', 'h'), // tv show name
BOX_tvsn = FOUR_CHAR_INT( 't', 'v', 's', 'n'), // tv season (byte)
BOX_purd = FOUR_CHAR_INT( 'p', 'u', 'r', 'd'), // purchase date
BOX_pgap = FOUR_CHAR_INT( 'p', 'g', 'a', 'p'), // Gapless Playback (byte)
//BOX_aart = FOUR_CHAR_INT( 'a', 'a', 'r', 't' ), // Album artist
BOX_cART = FOUR_CHAR_INT( '\xa9', 'A', 'R', 'T'), // artist
BOX_gnre = FOUR_CHAR_INT( 'g', 'n', 'r', 'e'),
// 3GPP metatags (http://cpansearch.perl.org/src/JHAR/MP4-Info-1.12/Info.pm)
BOX_auth = FOUR_CHAR_INT( 'a', 'u', 't', 'h'), // author
BOX_titl = FOUR_CHAR_INT( 't', 'i', 't', 'l'), // title
BOX_dscp = FOUR_CHAR_INT( 'd', 's', 'c', 'p'), // description
BOX_perf = FOUR_CHAR_INT( 'p', 'e', 'r', 'f'), // performer
BOX_mean = FOUR_CHAR_INT( 'm', 'e', 'a', 'n'), //
BOX_name = FOUR_CHAR_INT( 'n', 'a', 'm', 'e'), //
BOX_data = FOUR_CHAR_INT( 'd', 'a', 't', 'a'), //
// these from http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2008-September/053151.html
BOX_albm = FOUR_CHAR_INT( 'a', 'l', 'b', 'm'), // album
BOX_yrrc = FOUR_CHAR_INT( 'y', 'r', 'r', 'c') // album
};
// Video track : 'vide'
#define MP4E_HANDLER_TYPE_VIDE 0x76696465
// Audio track : 'soun'
#define MP4E_HANDLER_TYPE_SOUN 0x736F756E
// General MPEG-4 systems streams (without specific handler).
// Used for private stream, as suggested in http://www.mp4ra.org/handler.html
#define MP4E_HANDLER_TYPE_GESM 0x6765736D
typedef struct
{
boxsize_t size;
boxsize_t offset;
unsigned duration;
unsigned flag_random_access;
} sample_t;
typedef struct {
unsigned char *data;
int bytes;
int capacity;
} minimp4_vector_t;
typedef struct
{
MP4E_track_t info;
minimp4_vector_t smpl; // sample descriptor
minimp4_vector_t pending_sample;
minimp4_vector_t vsps; // or dsi for audio
minimp4_vector_t vpps; // not used for audio
minimp4_vector_t vvps; // used for HEVC
} track_t;
typedef struct MP4E_mux_tag
{
minimp4_vector_t tracks;
int64_t write_pos;
int (*write_callback)(int64_t offset, const void *buffer, size_t size, void *token);
void *token;
char *text_comment;
int sequential_mode_flag;
int enable_fragmentation; // flag, indicating streaming-friendly 'fragmentation' mode
int fragments_count; // # of fragments in 'fragmentation' mode
} MP4E_mux_t;
static const unsigned char box_ftyp[] = {
#if 1
0,0,0,0x18,'f','t','y','p',
'm','p','4','2',0,0,0,0,
'm','p','4','2','i','s','o','m',
#else
// as in ffmpeg
0,0,0,0x20,'f','t','y','p',
'i','s','o','m',0,0,2,0,
'm','p','4','1','i','s','o','m',
'i','s','o','2','a','v','c','1',
#endif
};
/**
* Endian-independent byte-write macros
*/
#define WR(x, n) *p++ = (unsigned char)((x) >> 8*n)
#define WRITE_1(x) WR(x, 0);
#define WRITE_2(x) WR(x, 1); WR(x, 0);
#define WRITE_3(x) WR(x, 2); WR(x, 1); WR(x, 0);
#define WRITE_4(x) WR(x, 3); WR(x, 2); WR(x, 1); WR(x, 0);
#define WR4(p, x) (p)[0] = (char)((x) >> 8*3); (p)[1] = (char)((x) >> 8*2); (p)[2] = (char)((x) >> 8*1); (p)[3] = (char)((x));
// Finish atom: update atom size field
#define END_ATOM --stack; WR4((unsigned char*)*stack, p - *stack);
// Initiate atom: save position of size field on stack
#define ATOM(x) *stack++ = p; p += 4; WRITE_4(x);
// Atom with 'FullAtomVersionFlags' field
#define ATOM_FULL(x, flag) ATOM(x); WRITE_4(flag);
#define ERR(func) { int err = func; if (err) return err; }
/**
Allocate vector with given size, return 1 on success, 0 on fail
*/
static int minimp4_vector_init(minimp4_vector_t *h, int capacity)
{
h->bytes = 0;
h->capacity = capacity;
h->data = capacity ? (unsigned char *)malloc(capacity) : NULL;
return !capacity || !!h->data;
}
/**
Deallocates vector memory
*/
static void minimp4_vector_reset(minimp4_vector_t *h)
{
if (h->data)
free(h->data);
memset(h, 0, sizeof(minimp4_vector_t));
}
/**
Reallocate vector memory to the given size
*/
static int minimp4_vector_grow(minimp4_vector_t *h, int bytes)
{
void *p;
int new_size = h->capacity*2 + 1024;
if (new_size < h->capacity + bytes)
new_size = h->capacity + bytes + 1024;
p = realloc(h->data, new_size);
if (!p)
return 0;
h->data = (unsigned char*)p;
h->capacity = new_size;
return 1;
}
/**
Allocates given number of bytes at the end of vector data, increasing
vector memory if necessary.
Return allocated memory.
*/
static unsigned char *minimp4_vector_alloc_tail(minimp4_vector_t *h, int bytes)
{
unsigned char *p;
if (!h->data && !minimp4_vector_init(h, 2*bytes + 1024))
return NULL;
if ((h->capacity - h->bytes) < bytes && !minimp4_vector_grow(h, bytes))
return NULL;
assert(h->data);
assert((h->capacity - h->bytes) >= bytes);
p = h->data + h->bytes;
h->bytes += bytes;
return p;
}
/**
Append data to the end of the vector (accumulate ot enqueue)
*/
static unsigned char *minimp4_vector_put(minimp4_vector_t *h, const void *buf, int bytes)
{
unsigned char *tail = minimp4_vector_alloc_tail(h, bytes);
if (tail)
memcpy(tail, buf, bytes);
return tail;
}
/**
* Allocates and initialize mp4 multiplexer
* return multiplexor handle on success; NULL on failure
*/
MP4E_mux_t *MP4E_open(int sequential_mode_flag, int enable_fragmentation, void *token,
int (*write_callback)(int64_t offset, const void *buffer, size_t size, void *token))
{
if (write_callback(0, box_ftyp, sizeof(box_ftyp), token)) // Write fixed header: 'ftyp' box
return 0;
MP4E_mux_t *mux = (MP4E_mux_t*)malloc(sizeof(MP4E_mux_t));
if (!mux)
return mux;
mux->sequential_mode_flag = sequential_mode_flag || enable_fragmentation;
mux->enable_fragmentation = enable_fragmentation;
mux->fragments_count = 0;
mux->write_callback = write_callback;
mux->token = token;
mux->text_comment = NULL;
mux->write_pos = sizeof(box_ftyp);
if (!mux->sequential_mode_flag)
{ // Write filler, which would be updated later
if (mux->write_callback(mux->write_pos, box_ftyp, 8, mux->token))
{
free(mux);
return 0;
}
mux->write_pos += 16; // box_ftyp + box_free for 32bit or 64bit size encoding
}
minimp4_vector_init(&mux->tracks, 2*sizeof(track_t));
return mux;
}
/**
* Add new track
*/
int MP4E_add_track(MP4E_mux_t *mux, const MP4E_track_t *track_data)
{
track_t *tr;
int ntr = mux->tracks.bytes / sizeof(track_t);
if (!mux || !track_data)
return MP4E_STATUS_BAD_ARGUMENTS;
tr = (track_t*)minimp4_vector_alloc_tail(&mux->tracks, sizeof(track_t));
if (!tr)
return MP4E_STATUS_NO_MEMORY;
memset(tr, 0, sizeof(track_t));
memcpy(&tr->info, track_data, sizeof(*track_data));
if (!minimp4_vector_init(&tr->smpl, 256))
return MP4E_STATUS_NO_MEMORY;
minimp4_vector_init(&tr->vsps, 0);
minimp4_vector_init(&tr->vpps, 0);
minimp4_vector_init(&tr->pending_sample, 0);
return ntr;
}
static const unsigned char *next_dsi(const unsigned char *p, const unsigned char *end, int *bytes)
{
if (p < end + 2)
{
*bytes = p[0]*256 + p[1];
return p + 2;
} else
return NULL;
}
static int append_mem(minimp4_vector_t *v, const void *mem, int bytes)
{
int i;
unsigned char size[2];
const unsigned char *p = v->data;
for (i = 0; i + 2 < v->bytes;)
{
int cb = p[i]*256 + p[i + 1];
if (cb == bytes && !memcmp(p + i + 2, mem, cb))
return 1;
i += 2 + cb;
}
size[0] = bytes >> 8;
size[1] = bytes;
return minimp4_vector_put(v, size, 2) && minimp4_vector_put(v, mem, bytes);
}
static int items_count(minimp4_vector_t *v)
{
int i, count = 0;
const unsigned char *p = v->data;
for (i = 0; i + 2 < v->bytes;)
{
int cb = p[i]*256 + p[i + 1];
count++;
i += 2 + cb;
}
return count;
}
int MP4E_set_dsi(MP4E_mux_t *mux, int track_id, const void *dsi, int bytes)
{
track_t* tr = ((track_t*)mux->tracks.data) + track_id;
assert(tr->info.track_media_kind == e_audio ||
tr->info.track_media_kind == e_private);
if (tr->vsps.bytes)
return MP4E_STATUS_ONLY_ONE_DSI_ALLOWED; // only one DSI allowed
return append_mem(&tr->vsps, dsi, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
}
int MP4E_set_vps(MP4E_mux_t *mux, int track_id, const void *vps, int bytes)
{
track_t* tr = ((track_t*)mux->tracks.data) + track_id;
assert(tr->info.track_media_kind == e_video);
return append_mem(&tr->vvps, vps, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
}
int MP4E_set_sps(MP4E_mux_t *mux, int track_id, const void *sps, int bytes)
{
track_t* tr = ((track_t*)mux->tracks.data) + track_id;
assert(tr->info.track_media_kind == e_video);
return append_mem(&tr->vsps, sps, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
}
int MP4E_set_pps(MP4E_mux_t *mux, int track_id, const void *pps, int bytes)
{
track_t* tr = ((track_t*)mux->tracks.data) + track_id;
assert(tr->info.track_media_kind == e_video);
return append_mem(&tr->vpps, pps, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
}
static unsigned get_duration(const track_t *tr)
{
unsigned i, sum_duration = 0;
const sample_t *s = (const sample_t *)tr->smpl.data;
for (i = 0; i < tr->smpl.bytes/sizeof(sample_t); i++)
{
sum_duration += s[i].duration;
}
return sum_duration;
}
static int write_pending_data(MP4E_mux_t *mux, track_t *tr)
{
// if have pending sample && have at least one sample in the index
if (tr->pending_sample.bytes > 0 && tr->smpl.bytes >= sizeof(sample_t))
{
// Complete pending sample
sample_t *smpl_desc;
unsigned char base[8], *p = base;
assert(mux->sequential_mode_flag);
// Write each sample to a separate atom
assert(mux->sequential_mode_flag); // Separate atom needed for sequential_mode only
WRITE_4(tr->pending_sample.bytes + 8);
WRITE_4(BOX_mdat);
ERR(mux->write_callback(mux->write_pos, base, p - base, mux->token));
mux->write_pos += p - base;
// Update sample descriptor with size and offset
smpl_desc = ((sample_t*)minimp4_vector_alloc_tail(&tr->smpl, 0)) - 1;
smpl_desc->size = tr->pending_sample.bytes;
smpl_desc->offset = (boxsize_t)mux->write_pos;
// Write data
ERR(mux->write_callback(mux->write_pos, tr->pending_sample.data, tr->pending_sample.bytes, mux->token));
mux->write_pos += tr->pending_sample.bytes;
// reset buffer
tr->pending_sample.bytes = 0;
}
return MP4E_STATUS_OK;
}
static int add_sample_descriptor(MP4E_mux_t *mux, track_t *tr, int data_bytes, int duration, int kind)
{
sample_t smp;
smp.size = data_bytes;
smp.offset = (boxsize_t)mux->write_pos;
smp.duration = (duration ? duration : tr->info.default_duration);
smp.flag_random_access = (kind == MP4E_SAMPLE_RANDOM_ACCESS);
return NULL != minimp4_vector_put(&tr->smpl, &smp, sizeof(sample_t));
}
static int mp4e_flush_index(MP4E_mux_t *mux);
/**
* Write Movie Fragment: 'moof' box
*/
static int mp4e_write_fragment_header(MP4E_mux_t *mux, int track_num, int data_bytes, int duration, int kind
#if MP4D_TFDT_SUPPORT
, uint64_t timestamp
#endif
)
{
unsigned char base[888], *p = base;
unsigned char *stack_base[20]; // atoms nesting stack
unsigned char **stack = stack_base;