1 /***************************************************************************
2     \file muxerMp4v2.h
3     \brief muxer using libmp4v2
4     \author mean fixounet@free.fr
5  ***************************************************************************/
6 
7 /***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 #ifndef ADM_MUXER_MP4V2
16 #define ADM_MUXER_MP4V2
17 #include "ADM_cpp.h"
18 #include "ADM_muxer.h"
19 #include "ADM_audioClock.h"
20 #include "mp4v2/mp4v2.h"
21 #include "ADM_paramList.h"
22 #include "mp4v2_muxer.h"
23 
24 /**
25     \class mp4v2AudioPacket
26 */
27 #define AUDIO_BUFFER_SIZE 16*1024*2
28 #define MP4V2_MAX_JITTER (40*1000) // 40 ms
29 class mp4v2AudioPacket
30 {
31     public:
32          class mp4v2AudioBlock
33          {
34             public:
35                     uint8_t     *buffer;
36                     uint64_t    dts;
37                     uint32_t    nbSamples;
38                     uint32_t    sizeInBytes;
39                     bool        present;
40             public:
mp4v2AudioBlock()41                   mp4v2AudioBlock() {buffer=new uint8_t[AUDIO_BUFFER_SIZE];present=false;}
~mp4v2AudioBlock()42                   ~mp4v2AudioBlock() {delete [] buffer;buffer=NULL;}
43          };
44             bool                eos;
45             mp4v2AudioBlock     blocks[2];
46             int                 nextWrite;
47             audioClock          *clock;
mp4v2AudioPacket()48             mp4v2AudioPacket() {eos=false;nextWrite=0;clock=NULL;}
~mp4v2AudioPacket()49             ~mp4v2AudioPacket() {if(clock) delete clock;clock=NULL;}
50 
51 
52 };
53 /**
54     \class muxerMp4v2
55 */
56 class muxerMp4v2 : public ADM_muxer
57 {
58 protected:
59         MP4FileHandle   handle;
60         MP4TrackId      videoTrackId;
61         MP4TrackId      *audioTrackIds;
62         mp4v2AudioPacket *audioPackets;
63         uint32_t        videoBufferSize;
64         uint8_t         *videoBuffer[2];
65         ADMBitstream    in[2];
66         int             nextWrite;
67         uint64_t        audioDelay; // In fact videoDelay, but must be added to all audioTrack
68         bool            needToConvertFromAnnexB;
69         uint8_t         *scratchBuffer;
70         string          targetFileName;
71         uint64_t        lastVideoDts;
72 
73 protected: // video
74         bool            initMpeg4(void);
75         bool            initH264(void);
76         bool            initVideo(void);
77         bool            loadNextVideoFrame(ADMBitstream *bs);
78         bool            setMaxDurationPerChunk(MP4TrackId track, uint32_t samples);
79 protected: // audio
80         bool            initAudio(void);
81         bool            fillAudio(uint64_t targetDts);
82 static  uint64_t        timeScale(uint64_t timeUs);
83 static  uint64_t        inverseTimeScale(uint64_t timeTick);
84         bool            loadAndToggleAudioSlot(int index);
85         bool            writeAudioBlock(int index,mp4v2AudioPacket::mp4v2AudioBlock *block,uint64_t duration90);
86 protected:
87         bool            addAc3(int index, WAVHeader *header);
88 public:
89                 muxerMp4v2();
90         virtual ~muxerMp4v2();
91         virtual bool open(const char *file, ADM_videoStream *s,uint32_t nbAudioTrack,ADM_audioStream **a);
92         virtual bool save(void) ;
93         virtual bool close(void) ;
useGlobalHeader(void)94         virtual bool useGlobalHeader(void) {return true;}
95                 void setPercent(int percent);
96 
97 };
98 
99 #endif
100