Merge "Tokenize MB optimized"
[libvpx.git] / libmkv / WebMElement.c
blob0ef5100bb35976af1d8836b638711ee703b93b07
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.
10 #include "EbmlBufferWriter.h"
11 #include "EbmlIDs.h"
12 #include "WebMElement.h"
13 #include <stdio.h>
15 #define kVorbisPrivateMaxSize 4000
17 void writeHeader(EbmlGlobal *glob)
19 EbmlLoc start;
20 Ebml_StartSubElement(glob, &start, EBML);
21 Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
22 Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); //EBML Read Version
23 Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); //EBML Max ID Length
24 Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); //EBML Max Size Length
25 Ebml_SerializeString(glob, DocType, "webm"); //Doc Type
26 Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); //Doc Type Version
27 Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); //Doc Type Read Version
28 Ebml_EndSubElement(glob, &start);
31 void writeSimpleBlock(EbmlGlobal *glob, unsigned char trackNumber, short timeCode,
32 int isKeyframe, unsigned char lacingFlag, int discardable,
33 unsigned char *data, unsigned long dataLength)
35 Ebml_WriteID(glob, SimpleBlock);
36 unsigned long blockLength = 4 + dataLength;
37 blockLength |= 0x10000000; //TODO check length < 0x0FFFFFFFF
38 Ebml_Serialize(glob, &blockLength, sizeof(blockLength), 4);
39 trackNumber |= 0x80; //TODO check track nubmer < 128
40 Ebml_Write(glob, &trackNumber, 1);
41 //Ebml_WriteSigned16(glob, timeCode,2); //this is 3 bytes
42 Ebml_Serialize(glob, &timeCode, sizeof(timeCode), 2);
43 unsigned char flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable;
44 Ebml_Write(glob, &flags, 1);
45 Ebml_Write(glob, data, dataLength);
48 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 rval;
58 void writeVideoTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing,
59 char *codecId, unsigned int pixelWidth, unsigned int pixelHeight,
60 double frameRate)
62 EbmlLoc start;
63 Ebml_StartSubElement(glob, &start, TrackEntry);
64 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
65 UInt64 trackID = generateTrackID(trackNumber);
66 Ebml_SerializeUnsigned(glob, TrackUID, trackID);
67 Ebml_SerializeString(glob, CodecName, "VP8"); //TODO shouldn't be fixed
69 Ebml_SerializeUnsigned(glob, TrackType, 1); //video is always 1
70 Ebml_SerializeString(glob, CodecID, codecId);
72 EbmlLoc videoStart;
73 Ebml_StartSubElement(glob, &videoStart, Video);
74 Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
75 Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
76 Ebml_SerializeFloat(glob, FrameRate, frameRate);
77 Ebml_EndSubElement(glob, &videoStart); //Video
79 Ebml_EndSubElement(glob, &start); //Track Entry
81 void writeAudioTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing,
82 char *codecId, double samplingFrequency, unsigned int channels,
83 unsigned char *private, unsigned long privateSize)
85 EbmlLoc start;
86 Ebml_StartSubElement(glob, &start, TrackEntry);
87 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
88 UInt64 trackID = generateTrackID(trackNumber);
89 Ebml_SerializeUnsigned(glob, TrackUID, trackID);
90 Ebml_SerializeUnsigned(glob, TrackType, 2); //audio is always 2
91 //I am using defaults for thesed required fields
92 /* Ebml_SerializeUnsigned(glob, FlagEnabled, 1);
93 Ebml_SerializeUnsigned(glob, FlagDefault, 1);
94 Ebml_SerializeUnsigned(glob, FlagForced, 1);
95 Ebml_SerializeUnsigned(glob, FlagLacing, flagLacing);*/
96 Ebml_SerializeString(glob, CodecID, codecId);
97 Ebml_SerializeData(glob, CodecPrivate, private, privateSize);
99 Ebml_SerializeString(glob, CodecName, "VORBIS"); //fixed for now
101 EbmlLoc AudioStart;
102 Ebml_StartSubElement(glob, &AudioStart, Audio);
103 Ebml_SerializeFloat(glob, SamplingFrequency, samplingFrequency);
104 Ebml_SerializeUnsigned(glob, Channels, channels);
105 Ebml_EndSubElement(glob, &AudioStart);
107 Ebml_EndSubElement(glob, &start);
109 void writeSegmentInformation(EbmlGlobal *ebml, EbmlLoc* startInfo, unsigned long timeCodeScale, double duration)
111 Ebml_StartSubElement(ebml, startInfo, Info);
112 Ebml_SerializeUnsigned(ebml, TimecodeScale, timeCodeScale);
113 Ebml_SerializeFloat(ebml, Segment_Duration, duration * 1000.0); //Currently fixed to using milliseconds
114 Ebml_SerializeString(ebml, 0x4D80, "QTmuxingAppLibWebM-0.0.1");
115 Ebml_SerializeString(ebml, 0x5741, "QTwritingAppLibWebM-0.0.1");
116 Ebml_EndSubElement(ebml, startInfo);
120 void Mkv_InitializeSegment(Ebml& ebml_out, EbmlLoc& ebmlLoc)
122 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x18538067);
125 void Mkv_InitializeSeek(Ebml& ebml_out, EbmlLoc& ebmlLoc)
127 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x114d9b74);
129 void Mkv_WriteSeekInformation(Ebml& ebml_out, SeekStruct& seekInformation)
131 EbmlLoc ebmlLoc;
132 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x4dbb);
133 Ebml_SerializeString(ebml_out, 0x53ab, seekInformation.SeekID);
134 Ebml_SerializeUnsigned(ebml_out, 0x53ac, seekInformation.SeekPosition);
135 Ebml_EndSubElement(ebml_out, ebmlLoc);
138 void Mkv_WriteSegmentInformation(Ebml& ebml_out, SegmentInformationStruct& segmentInformation)
140 Ebml_SerializeUnsigned(ebml_out, 0x73a4, segmentInformation.segmentUID);
141 if (segmentInformation.filename != 0)
142 Ebml_SerializeString(ebml_out, 0x7384, segmentInformation.filename);
143 Ebml_SerializeUnsigned(ebml_out, 0x2AD7B1, segmentInformation.TimecodeScale);
144 Ebml_SerializeUnsigned(ebml_out, 0x4489, segmentInformation.Duration);
145 //TODO date
146 Ebml_SerializeWString(ebml_out, 0x4D80, L"MKVMUX");
147 Ebml_SerializeWString(ebml_out, 0x5741, segmentInformation.WritingApp);
150 void Mkv_InitializeTrack(Ebml& ebml_out, EbmlLoc& ebmlLoc)
152 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1654AE6B);
155 static void Mkv_WriteGenericTrackData(Ebml& ebml_out, TrackStruct& track)
157 Ebml_SerializeUnsigned(ebml_out, 0xD7, track.TrackNumber);
158 Ebml_SerializeUnsigned(ebml_out, 0x73C5, track.TrackUID);
159 Ebml_SerializeUnsigned(ebml_out, 0x83, track.TrackType);
160 Ebml_SerializeUnsigned(ebml_out, 0xB9, track.FlagEnabled ? 1 :0);
161 Ebml_SerializeUnsigned(ebml_out, 0x88, track.FlagDefault ? 1 :0);
162 Ebml_SerializeUnsigned(ebml_out, 0x55AA, track.FlagForced ? 1 :0);
163 if (track.Language != 0)
164 Ebml_SerializeString(ebml_out, 0x22B59C, track.Language);
165 if (track.CodecID != 0)
166 Ebml_SerializeString(ebml_out, 0x86, track.CodecID);
167 if (track.CodecPrivate != 0)
168 Ebml_SerializeData(ebml_out, 0x63A2, track.CodecPrivate, track.CodecPrivateLength);
169 if (track.CodecName != 0)
170 Ebml_SerializeWString(ebml_out, 0x258688, track.CodecName);
173 void Mkv_WriteVideoTrack(Ebml& ebml_out, TrackStruct & track, VideoTrackStruct& video)
175 EbmlLoc trackHeadLoc, videoHeadLoc;
176 Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE); //start Track
177 Mkv_WriteGenericTrackData(ebml_out, track);
178 Ebml_StartSubElement(ebml_out, videoHeadLoc, 0xE0); //start Video
179 Ebml_SerializeUnsigned(ebml_out, 0x9A, video.FlagInterlaced ? 1 :0);
180 Ebml_SerializeUnsigned(ebml_out, 0xB0, video.PixelWidth);
181 Ebml_SerializeUnsigned(ebml_out, 0xBA, video.PixelHeight);
182 Ebml_SerializeUnsigned(ebml_out, 0x54B0, video.PixelDisplayWidth);
183 Ebml_SerializeUnsigned(ebml_out, 0x54BA, video.PixelDisplayHeight);
184 Ebml_SerializeUnsigned(ebml_out, 0x54B2, video.displayUnit);
185 Ebml_SerializeFloat(ebml_out, 0x2383E3, video.FrameRate);
186 Ebml_EndSubElement(ebml_out, videoHeadLoc);
187 Ebml_EndSubElement(ebml_out, trackHeadLoc);
191 void Mkv_WriteAudioTrack(Ebml& ebml_out, TrackStruct & track, AudioTrackStruct& video)
193 EbmlLoc trackHeadLoc, audioHeadLoc;
194 Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE);
195 Mkv_WriteGenericTrackData(ebml_out, track);
196 Ebml_StartSubElement(ebml_out, audioHeadLoc, 0xE0); //start Audio
197 Ebml_SerializeFloat(ebml_out, 0xB5, video.SamplingFrequency);
198 Ebml_SerializeUnsigned(ebml_out, 0x9F, video.Channels);
199 Ebml_SerializeUnsigned(ebml_out, 0x6264, video.BitDepth);
200 Ebml_EndSubElement(ebml_out, audioHeadLoc); // end audio
201 Ebml_EndSubElement(ebml_out, trackHeadLoc);
204 void Mkv_WriteEbmlClusterHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, ClusterHeadStruct & clusterHead)
206 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1F43B675);
207 Ebml_SerializeUnsigned(ebml_out, 0x6264, clusterHead.TimeCode);
210 void Mkv_WriteSimpleBlockHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, SimpleBlockStruct& block)
212 Ebml_StartSubElement(ebml_out, ebmlLoc, 0xA3);
213 Ebml_Write1UInt(ebml_out, block.TrackNumber);
214 Ebml_WriteSigned16(ebml_out,block.TimeCode);
215 unsigned char flags = 0x00 | (block.iskey ? 0x80:0x00) | (block.lacing << 1) | block.discardable;
216 Ebml_Write1UInt(ebml_out, flags); //TODO this may be the wrong function
217 Ebml_Serialize(ebml_out, block.data, block.dataLength);
218 Ebml_EndSubElement(ebml_out,ebmlLoc);