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