Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 // Copyright (c) 2010 The WebM project authors. All Rights Reserved.
2 //
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.
9 #include "EbmlIDs.h"
10 #include "WebMElement.h"
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <time.h>
16 #define kVorbisPrivateMaxSize 4000
17 #define UInt64 uint64_t
19 void writeHeader(EbmlGlobal *glob) {
20 EbmlLoc start;
21 Ebml_StartSubElement(glob, &start, EBML);
22 Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
23 Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); // EBML Read Version
24 Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); // EBML Max ID Length
25 Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); // EBML Max Size Length
26 Ebml_SerializeString(glob, DocType, "webm"); // Doc Type
27 Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); // Doc Type Version
28 Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); // Doc Type Read Version
29 Ebml_EndSubElement(glob, &start);
30 }
32 void writeSimpleBlock(EbmlGlobal *glob, unsigned char trackNumber, short timeCode,
33 int isKeyframe, unsigned char lacingFlag, int discardable,
34 unsigned char *data, unsigned long dataLength) {
35 unsigned long blockLength = 4 + dataLength;
36 unsigned char flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable;
37 Ebml_WriteID(glob, SimpleBlock);
38 blockLength |= 0x10000000; // TODO check length < 0x0FFFFFFFF
39 Ebml_Serialize(glob, &blockLength, sizeof(blockLength), 4);
40 trackNumber |= 0x80; // TODO check track nubmer < 128
41 Ebml_Write(glob, &trackNumber, 1);
42 // Ebml_WriteSigned16(glob, timeCode,2); //this is 3 bytes
43 Ebml_Serialize(glob, &timeCode, sizeof(timeCode), 2);
44 flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable;
45 Ebml_Write(glob, &flags, 1);
46 Ebml_Write(glob, data, dataLength);
47 }
49 static UInt64 generateTrackID(unsigned int trackNumber) {
50 UInt64 t = time(NULL) * trackNumber;
51 UInt64 r = rand();
52 r = r << 32;
53 r += rand();
54 // UInt64 rval = t ^ r;
55 return t ^ r;
56 }
58 void writeVideoTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing,
59 const char *codecId, unsigned int pixelWidth, unsigned int pixelHeight,
60 unsigned int displayWidth, unsigned int displayHeight,
61 double frameRate) {
62 EbmlLoc start;
63 UInt64 trackID;
64 Ebml_StartSubElement(glob, &start, TrackEntry);
65 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
66 trackID = generateTrackID(trackNumber);
67 Ebml_SerializeUnsigned(glob, TrackUID, trackID);
68 Ebml_SerializeString(glob, CodecName, "VP8"); // TODO shouldn't be fixed
70 Ebml_SerializeUnsigned(glob, TrackType, 1); // video is always 1
71 Ebml_SerializeString(glob, CodecID, codecId);
72 {
73 EbmlLoc videoStart;
74 Ebml_StartSubElement(glob, &videoStart, Video);
75 Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
76 Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
77 if (pixelWidth != displayWidth) {
78 Ebml_SerializeUnsigned(glob, DisplayWidth, displayWidth);
79 }
80 if (pixelHeight != displayHeight) {
81 Ebml_SerializeUnsigned(glob, DisplayHeight, displayHeight);
82 }
83 Ebml_SerializeFloat(glob, FrameRate, frameRate);
84 Ebml_EndSubElement(glob, &videoStart); // Video
85 }
86 Ebml_EndSubElement(glob, &start); // Track Entry
87 }
88 void writeAudioTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing,
89 const char *codecId, double samplingFrequency, unsigned int channels,
90 unsigned char *private, unsigned long privateSize) {
91 EbmlLoc start;
92 UInt64 trackID;
93 Ebml_StartSubElement(glob, &start, TrackEntry);
94 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
95 trackID = generateTrackID(trackNumber);
96 Ebml_SerializeUnsigned(glob, TrackUID, trackID);
97 Ebml_SerializeUnsigned(glob, TrackType, 2); // audio is always 2
98 // I am using defaults for thesed required fields
99 /* Ebml_SerializeUnsigned(glob, FlagEnabled, 1);
100 Ebml_SerializeUnsigned(glob, FlagDefault, 1);
101 Ebml_SerializeUnsigned(glob, FlagForced, 1);
102 Ebml_SerializeUnsigned(glob, FlagLacing, flagLacing);*/
103 Ebml_SerializeString(glob, CodecID, codecId);
104 Ebml_SerializeData(glob, CodecPrivate, private, privateSize);
106 Ebml_SerializeString(glob, CodecName, "VORBIS"); // fixed for now
107 {
108 EbmlLoc AudioStart;
109 Ebml_StartSubElement(glob, &AudioStart, Audio);
110 Ebml_SerializeFloat(glob, SamplingFrequency, samplingFrequency);
111 Ebml_SerializeUnsigned(glob, Channels, channels);
112 Ebml_EndSubElement(glob, &AudioStart);
113 }
114 Ebml_EndSubElement(glob, &start);
115 }
116 void writeSegmentInformation(EbmlGlobal *ebml, EbmlLoc *startInfo, unsigned long timeCodeScale, double duration) {
117 Ebml_StartSubElement(ebml, startInfo, Info);
118 Ebml_SerializeUnsigned(ebml, TimecodeScale, timeCodeScale);
119 Ebml_SerializeFloat(ebml, Segment_Duration, duration * 1000.0); // Currently fixed to using milliseconds
120 Ebml_SerializeString(ebml, 0x4D80, "QTmuxingAppLibWebM-0.0.1");
121 Ebml_SerializeString(ebml, 0x5741, "QTwritingAppLibWebM-0.0.1");
122 Ebml_EndSubElement(ebml, startInfo);
123 }
125 /*
126 void Mkv_InitializeSegment(Ebml& ebml_out, EbmlLoc& ebmlLoc)
127 {
128 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x18538067);
129 }
131 void Mkv_InitializeSeek(Ebml& ebml_out, EbmlLoc& ebmlLoc)
132 {
133 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x114d9b74);
134 }
135 void Mkv_WriteSeekInformation(Ebml& ebml_out, SeekStruct& seekInformation)
136 {
137 EbmlLoc ebmlLoc;
138 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x4dbb);
139 Ebml_SerializeString(ebml_out, 0x53ab, seekInformation.SeekID);
140 Ebml_SerializeUnsigned(ebml_out, 0x53ac, seekInformation.SeekPosition);
141 Ebml_EndSubElement(ebml_out, ebmlLoc);
142 }
144 void Mkv_WriteSegmentInformation(Ebml& ebml_out, SegmentInformationStruct& segmentInformation)
145 {
146 Ebml_SerializeUnsigned(ebml_out, 0x73a4, segmentInformation.segmentUID);
147 if (segmentInformation.filename != 0)
148 Ebml_SerializeString(ebml_out, 0x7384, segmentInformation.filename);
149 Ebml_SerializeUnsigned(ebml_out, 0x2AD7B1, segmentInformation.TimecodeScale);
150 Ebml_SerializeUnsigned(ebml_out, 0x4489, segmentInformation.Duration);
151 // TODO date
152 Ebml_SerializeWString(ebml_out, 0x4D80, L"MKVMUX");
153 Ebml_SerializeWString(ebml_out, 0x5741, segmentInformation.WritingApp);
154 }
156 void Mkv_InitializeTrack(Ebml& ebml_out, EbmlLoc& ebmlLoc)
157 {
158 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1654AE6B);
159 }
161 static void Mkv_WriteGenericTrackData(Ebml& ebml_out, TrackStruct& track)
162 {
163 Ebml_SerializeUnsigned(ebml_out, 0xD7, track.TrackNumber);
164 Ebml_SerializeUnsigned(ebml_out, 0x73C5, track.TrackUID);
165 Ebml_SerializeUnsigned(ebml_out, 0x83, track.TrackType);
166 Ebml_SerializeUnsigned(ebml_out, 0xB9, track.FlagEnabled ? 1 :0);
167 Ebml_SerializeUnsigned(ebml_out, 0x88, track.FlagDefault ? 1 :0);
168 Ebml_SerializeUnsigned(ebml_out, 0x55AA, track.FlagForced ? 1 :0);
169 if (track.Language != 0)
170 Ebml_SerializeString(ebml_out, 0x22B59C, track.Language);
171 if (track.CodecID != 0)
172 Ebml_SerializeString(ebml_out, 0x86, track.CodecID);
173 if (track.CodecPrivate != 0)
174 Ebml_SerializeData(ebml_out, 0x63A2, track.CodecPrivate, track.CodecPrivateLength);
175 if (track.CodecName != 0)
176 Ebml_SerializeWString(ebml_out, 0x258688, track.CodecName);
177 }
179 void Mkv_WriteVideoTrack(Ebml& ebml_out, TrackStruct & track, VideoTrackStruct& video)
180 {
181 EbmlLoc trackHeadLoc, videoHeadLoc;
182 Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE); // start Track
183 Mkv_WriteGenericTrackData(ebml_out, track);
184 Ebml_StartSubElement(ebml_out, videoHeadLoc, 0xE0); // start Video
185 Ebml_SerializeUnsigned(ebml_out, 0x9A, video.FlagInterlaced ? 1 :0);
186 Ebml_SerializeUnsigned(ebml_out, 0xB0, video.PixelWidth);
187 Ebml_SerializeUnsigned(ebml_out, 0xBA, video.PixelHeight);
188 Ebml_SerializeUnsigned(ebml_out, 0x54B0, video.PixelDisplayWidth);
189 Ebml_SerializeUnsigned(ebml_out, 0x54BA, video.PixelDisplayHeight);
190 Ebml_SerializeUnsigned(ebml_out, 0x54B2, video.displayUnit);
191 Ebml_SerializeFloat(ebml_out, 0x2383E3, video.FrameRate);
192 Ebml_EndSubElement(ebml_out, videoHeadLoc);
193 Ebml_EndSubElement(ebml_out, trackHeadLoc);
195 }
197 void Mkv_WriteAudioTrack(Ebml& ebml_out, TrackStruct & track, AudioTrackStruct& video)
198 {
199 EbmlLoc trackHeadLoc, audioHeadLoc;
200 Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE);
201 Mkv_WriteGenericTrackData(ebml_out, track);
202 Ebml_StartSubElement(ebml_out, audioHeadLoc, 0xE0); // start Audio
203 Ebml_SerializeFloat(ebml_out, 0xB5, video.SamplingFrequency);
204 Ebml_SerializeUnsigned(ebml_out, 0x9F, video.Channels);
205 Ebml_SerializeUnsigned(ebml_out, 0x6264, video.BitDepth);
206 Ebml_EndSubElement(ebml_out, audioHeadLoc); // end audio
207 Ebml_EndSubElement(ebml_out, trackHeadLoc);
208 }
210 void Mkv_WriteEbmlClusterHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, ClusterHeadStruct & clusterHead)
211 {
212 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1F43B675);
213 Ebml_SerializeUnsigned(ebml_out, 0x6264, clusterHead.TimeCode);
214 }
216 void Mkv_WriteSimpleBlockHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, SimpleBlockStruct& block)
217 {
218 Ebml_StartSubElement(ebml_out, ebmlLoc, 0xA3);
219 Ebml_Write1UInt(ebml_out, block.TrackNumber);
220 Ebml_WriteSigned16(ebml_out,block.TimeCode);
221 unsigned char flags = 0x00 | (block.iskey ? 0x80:0x00) | (block.lacing << 1) | block.discardable;
222 Ebml_Write1UInt(ebml_out, flags); // TODO this may be the wrong function
223 Ebml_Serialize(ebml_out, block.data, block.dataLength);
224 Ebml_EndSubElement(ebml_out,ebmlLoc);
225 }
226 */