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