Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / media / libmkv / WebMElement.c
blobb653211d14d55a7cca02ac90391d2a7a8930ccd0
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);
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);
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;
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 EbmlLoc start;
62 UInt64 trackID;
63 Ebml_StartSubElement(glob, &start, TrackEntry);
64 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
65 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 if (pixelWidth != displayWidth) {
77 Ebml_SerializeUnsigned(glob, DisplayWidth, displayWidth);
79 if (pixelHeight != displayHeight) {
80 Ebml_SerializeUnsigned(glob, DisplayHeight, displayHeight);
82 Ebml_EndSubElement(glob, &videoStart); // Video
84 Ebml_EndSubElement(glob, &start); // Track Entry
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 unsigned char *private, unsigned long privateSize) {
90 EbmlLoc start;
91 UInt64 trackID;
92 Ebml_StartSubElement(glob, &start, TrackEntry);
93 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
94 trackID = generateTrackID(trackNumber);
95 Ebml_SerializeUnsigned(glob, TrackUID, trackID);
96 Ebml_SerializeUnsigned(glob, TrackType, 2); // audio is always 2
97 Ebml_SerializeUnsigned(glob, CodecDelay, codecDelay);
98 Ebml_SerializeUnsigned(glob, SeekPreRoll, seekPreRoll);
99 // I am using defaults for thesed required fields
100 /* Ebml_SerializeUnsigned(glob, FlagEnabled, 1);
101 Ebml_SerializeUnsigned(glob, FlagDefault, 1);
102 Ebml_SerializeUnsigned(glob, FlagForced, 1);
103 Ebml_SerializeUnsigned(glob, FlagLacing, flagLacing);*/
104 Ebml_SerializeString(glob, CodecID, codecId);
105 Ebml_SerializeData(glob, CodecPrivate, private, privateSize);
107 Ebml_SerializeString(glob, CodecName, "OPUS"); // fixed for now
109 EbmlLoc AudioStart;
110 Ebml_StartSubElement(glob, &AudioStart, Audio);
111 Ebml_SerializeFloat(glob, SamplingFrequency, samplingFrequency);
112 Ebml_SerializeUnsigned(glob, Channels, channels);
113 Ebml_EndSubElement(glob, &AudioStart);
115 Ebml_EndSubElement(glob, &start);
117 void writeSegmentInformation(EbmlGlobal *ebml, EbmlLoc *startInfo, unsigned long timeCodeScale, double duration) {
118 Ebml_StartSubElement(ebml, startInfo, Info);
119 Ebml_SerializeUnsigned(ebml, TimecodeScale, timeCodeScale);
120 Ebml_SerializeFloat(ebml, Segment_Duration, duration * 1000.0); // Currently fixed to using milliseconds
121 Ebml_SerializeString(ebml, 0x4D80, "QTmuxingAppLibWebM-0.0.1");
122 Ebml_SerializeString(ebml, 0x5741, "QTwritingAppLibWebM-0.0.1");
123 Ebml_EndSubElement(ebml, startInfo);
127 void Mkv_InitializeSegment(Ebml& ebml_out, EbmlLoc& ebmlLoc)
129 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x18538067);
132 void Mkv_InitializeSeek(Ebml& ebml_out, EbmlLoc& ebmlLoc)
134 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x114d9b74);
136 void Mkv_WriteSeekInformation(Ebml& ebml_out, SeekStruct& seekInformation)
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);
145 void Mkv_WriteSegmentInformation(Ebml& ebml_out, SegmentInformationStruct& segmentInformation)
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);
157 void Mkv_InitializeTrack(Ebml& ebml_out, EbmlLoc& ebmlLoc)
159 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1654AE6B);
162 static void Mkv_WriteGenericTrackData(Ebml& ebml_out, TrackStruct& track)
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);
180 void Mkv_WriteVideoTrack(Ebml& ebml_out, TrackStruct & track, VideoTrackStruct& video)
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);
198 void Mkv_WriteAudioTrack(Ebml& ebml_out, TrackStruct & track, AudioTrackStruct& video)
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);
211 void Mkv_WriteEbmlClusterHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, ClusterHeadStruct & clusterHead)
213 Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1F43B675);
214 Ebml_SerializeUnsigned(ebml_out, 0x6264, clusterHead.TimeCode);
217 void Mkv_WriteSimpleBlockHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, SimpleBlockStruct& block)
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);