1 // Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
10 #include "EbmlBufferWriter.h"
12 #include "WebMElement.h"
15 #define kVorbisPrivateMaxSize 4000
17 void writeHeader(EbmlGlobal
*glob
) {
19 Ebml_StartSubElement(glob
, &start
, EBML
);
20 Ebml_SerializeUnsigned(glob
, EBMLVersion
, 1);
21 Ebml_SerializeUnsigned(glob
, EBMLReadVersion
, 1); // EBML Read Version
22 Ebml_SerializeUnsigned(glob
, EBMLMaxIDLength
, 4); // EBML Max ID Length
23 Ebml_SerializeUnsigned(glob
, EBMLMaxSizeLength
, 8); // EBML Max Size Length
24 Ebml_SerializeString(glob
, DocType
, "webm"); // Doc Type
25 Ebml_SerializeUnsigned(glob
, DocTypeVersion
, 2); // Doc Type Version
26 Ebml_SerializeUnsigned(glob
, DocTypeReadVersion
, 2); // Doc Type Read Version
27 Ebml_EndSubElement(glob
, &start
);
30 void writeSimpleBlock(EbmlGlobal
*glob
, unsigned char trackNumber
, short timeCode
,
31 int isKeyframe
, unsigned char lacingFlag
, int discardable
,
32 unsigned char *data
, unsigned long dataLength
) {
33 Ebml_WriteID(glob
, SimpleBlock
);
34 unsigned long blockLength
= 4 + dataLength
;
35 blockLength
|= 0x10000000; // TODO check length < 0x0FFFFFFFF
36 Ebml_Serialize(glob
, &blockLength
, sizeof(blockLength
), 4);
37 trackNumber
|= 0x80; // TODO check track nubmer < 128
38 Ebml_Write(glob
, &trackNumber
, 1);
39 // Ebml_WriteSigned16(glob, timeCode,2); //this is 3 bytes
40 Ebml_Serialize(glob
, &timeCode
, sizeof(timeCode
), 2);
41 unsigned char flags
= 0x00 | (isKeyframe
? 0x80 : 0x00) | (lacingFlag
<< 1) | discardable
;
42 Ebml_Write(glob
, &flags
, 1);
43 Ebml_Write(glob
, data
, dataLength
);
46 static UInt64
generateTrackID(unsigned int trackNumber
) {
47 UInt64 t
= time(NULL
) * trackNumber
;
55 void writeVideoTrack(EbmlGlobal
*glob
, unsigned int trackNumber
, int flagLacing
,
56 char *codecId
, unsigned int pixelWidth
, unsigned int pixelHeight
,
59 Ebml_StartSubElement(glob
, &start
, TrackEntry
);
60 Ebml_SerializeUnsigned(glob
, TrackNumber
, trackNumber
);
61 UInt64 trackID
= generateTrackID(trackNumber
);
62 Ebml_SerializeUnsigned(glob
, TrackUID
, trackID
);
63 Ebml_SerializeString(glob
, CodecName
, "VP8"); // TODO shouldn't be fixed
65 Ebml_SerializeUnsigned(glob
, TrackType
, 1); // video is always 1
66 Ebml_SerializeString(glob
, CodecID
, codecId
);
69 Ebml_StartSubElement(glob
, &videoStart
, Video
);
70 Ebml_SerializeUnsigned(glob
, PixelWidth
, pixelWidth
);
71 Ebml_SerializeUnsigned(glob
, PixelHeight
, pixelHeight
);
72 Ebml_SerializeFloat(glob
, FrameRate
, frameRate
);
73 Ebml_EndSubElement(glob
, &videoStart
); // Video
75 Ebml_EndSubElement(glob
, &start
); // Track Entry
77 void writeAudioTrack(EbmlGlobal
*glob
, unsigned int trackNumber
, int flagLacing
,
78 char *codecId
, double samplingFrequency
, unsigned int channels
,
79 unsigned char *private, unsigned long privateSize
) {
81 Ebml_StartSubElement(glob
, &start
, TrackEntry
);
82 Ebml_SerializeUnsigned(glob
, TrackNumber
, trackNumber
);
83 UInt64 trackID
= generateTrackID(trackNumber
);
84 Ebml_SerializeUnsigned(glob
, TrackUID
, trackID
);
85 Ebml_SerializeUnsigned(glob
, TrackType
, 2); // audio is always 2
86 // I am using defaults for thesed required fields
87 /* Ebml_SerializeUnsigned(glob, FlagEnabled, 1);
88 Ebml_SerializeUnsigned(glob, FlagDefault, 1);
89 Ebml_SerializeUnsigned(glob, FlagForced, 1);
90 Ebml_SerializeUnsigned(glob, FlagLacing, flagLacing);*/
91 Ebml_SerializeString(glob
, CodecID
, codecId
);
92 Ebml_SerializeData(glob
, CodecPrivate
, private, privateSize
);
94 Ebml_SerializeString(glob
, CodecName
, "VORBIS"); // fixed for now
97 Ebml_StartSubElement(glob
, &AudioStart
, Audio
);
98 Ebml_SerializeFloat(glob
, SamplingFrequency
, samplingFrequency
);
99 Ebml_SerializeUnsigned(glob
, Channels
, channels
);
100 Ebml_EndSubElement(glob
, &AudioStart
);
102 Ebml_EndSubElement(glob
, &start
);
104 void writeSegmentInformation(EbmlGlobal
*ebml
, EbmlLoc
*startInfo
, unsigned long timeCodeScale
, double duration
) {
105 Ebml_StartSubElement(ebml
, startInfo
, Info
);
106 Ebml_SerializeUnsigned(ebml
, TimecodeScale
, timeCodeScale
);
107 Ebml_SerializeFloat(ebml
, Segment_Duration
, duration
* 1000.0); // Currently fixed to using milliseconds
108 Ebml_SerializeString(ebml
, 0x4D80, "QTmuxingAppLibWebM-0.0.1");
109 Ebml_SerializeString(ebml
, 0x5741, "QTwritingAppLibWebM-0.0.1");
110 Ebml_EndSubElement(ebml
, startInfo
);
114 void Mkv_InitializeSegment(Ebml& ebml_out, EbmlLoc& ebmlLoc)
116 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x18538067);
119 void Mkv_InitializeSeek(Ebml& ebml_out, EbmlLoc& ebmlLoc)
121 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x114d9b74);
123 void Mkv_WriteSeekInformation(Ebml& ebml_out, SeekStruct& seekInformation)
126 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x4dbb);
127 Ebml_SerializeString(ebml_out, 0x53ab, seekInformation.SeekID);
128 Ebml_SerializeUnsigned(ebml_out, 0x53ac, seekInformation.SeekPosition);
129 Ebml_EndSubElement(ebml_out, ebmlLoc);
132 void Mkv_WriteSegmentInformation(Ebml& ebml_out, SegmentInformationStruct& segmentInformation)
134 Ebml_SerializeUnsigned(ebml_out, 0x73a4, segmentInformation.segmentUID);
135 if (segmentInformation.filename != 0)
136 Ebml_SerializeString(ebml_out, 0x7384, segmentInformation.filename);
137 Ebml_SerializeUnsigned(ebml_out, 0x2AD7B1, segmentInformation.TimecodeScale);
138 Ebml_SerializeUnsigned(ebml_out, 0x4489, segmentInformation.Duration);
140 Ebml_SerializeWString(ebml_out, 0x4D80, L"MKVMUX");
141 Ebml_SerializeWString(ebml_out, 0x5741, segmentInformation.WritingApp);
144 void Mkv_InitializeTrack(Ebml& ebml_out, EbmlLoc& ebmlLoc)
146 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1654AE6B);
149 static void Mkv_WriteGenericTrackData(Ebml& ebml_out, TrackStruct& track)
151 Ebml_SerializeUnsigned(ebml_out, 0xD7, track.TrackNumber);
152 Ebml_SerializeUnsigned(ebml_out, 0x73C5, track.TrackUID);
153 Ebml_SerializeUnsigned(ebml_out, 0x83, track.TrackType);
154 Ebml_SerializeUnsigned(ebml_out, 0xB9, track.FlagEnabled ? 1 :0);
155 Ebml_SerializeUnsigned(ebml_out, 0x88, track.FlagDefault ? 1 :0);
156 Ebml_SerializeUnsigned(ebml_out, 0x55AA, track.FlagForced ? 1 :0);
157 if (track.Language != 0)
158 Ebml_SerializeString(ebml_out, 0x22B59C, track.Language);
159 if (track.CodecID != 0)
160 Ebml_SerializeString(ebml_out, 0x86, track.CodecID);
161 if (track.CodecPrivate != 0)
162 Ebml_SerializeData(ebml_out, 0x63A2, track.CodecPrivate, track.CodecPrivateLength);
163 if (track.CodecName != 0)
164 Ebml_SerializeWString(ebml_out, 0x258688, track.CodecName);
167 void Mkv_WriteVideoTrack(Ebml& ebml_out, TrackStruct & track, VideoTrackStruct& video)
169 EbmlLoc trackHeadLoc, videoHeadLoc;
170 Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE); // start Track
171 Mkv_WriteGenericTrackData(ebml_out, track);
172 Ebml_StartSubElement(ebml_out, videoHeadLoc, 0xE0); // start Video
173 Ebml_SerializeUnsigned(ebml_out, 0x9A, video.FlagInterlaced ? 1 :0);
174 Ebml_SerializeUnsigned(ebml_out, 0xB0, video.PixelWidth);
175 Ebml_SerializeUnsigned(ebml_out, 0xBA, video.PixelHeight);
176 Ebml_SerializeUnsigned(ebml_out, 0x54B0, video.PixelDisplayWidth);
177 Ebml_SerializeUnsigned(ebml_out, 0x54BA, video.PixelDisplayHeight);
178 Ebml_SerializeUnsigned(ebml_out, 0x54B2, video.displayUnit);
179 Ebml_SerializeFloat(ebml_out, 0x2383E3, video.FrameRate);
180 Ebml_EndSubElement(ebml_out, videoHeadLoc);
181 Ebml_EndSubElement(ebml_out, trackHeadLoc);
185 void Mkv_WriteAudioTrack(Ebml& ebml_out, TrackStruct & track, AudioTrackStruct& video)
187 EbmlLoc trackHeadLoc, audioHeadLoc;
188 Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE);
189 Mkv_WriteGenericTrackData(ebml_out, track);
190 Ebml_StartSubElement(ebml_out, audioHeadLoc, 0xE0); // start Audio
191 Ebml_SerializeFloat(ebml_out, 0xB5, video.SamplingFrequency);
192 Ebml_SerializeUnsigned(ebml_out, 0x9F, video.Channels);
193 Ebml_SerializeUnsigned(ebml_out, 0x6264, video.BitDepth);
194 Ebml_EndSubElement(ebml_out, audioHeadLoc); // end audio
195 Ebml_EndSubElement(ebml_out, trackHeadLoc);
198 void Mkv_WriteEbmlClusterHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, ClusterHeadStruct & clusterHead)
200 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1F43B675);
201 Ebml_SerializeUnsigned(ebml_out, 0x6264, clusterHead.TimeCode);
204 void Mkv_WriteSimpleBlockHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, SimpleBlockStruct& block)
206 Ebml_StartSubElement(ebml_out, ebmlLoc, 0xA3);
207 Ebml_Write1UInt(ebml_out, block.TrackNumber);
208 Ebml_WriteSigned16(ebml_out,block.TimeCode);
209 unsigned char flags = 0x00 | (block.iskey ? 0x80:0x00) | (block.lacing << 1) | block.discardable;
210 Ebml_Write1UInt(ebml_out, flags); // TODO this may be the wrong function
211 Ebml_Serialize(ebml_out, block.data, block.dataLength);
212 Ebml_EndSubElement(ebml_out,ebmlLoc);