1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libmkv/WebMElement.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,226 @@ 1.4 +// Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.5 +// 1.6 +// Use of this source code is governed by a BSD-style license 1.7 +// that can be found in the LICENSE file in the root of the source 1.8 +// tree. An additional intellectual property rights grant can be found 1.9 +// in the file PATENTS. All contributing project authors may 1.10 +// be found in the AUTHORS file in the root of the source tree. 1.11 + 1.12 +#include "EbmlIDs.h" 1.13 +#include "WebMElement.h" 1.14 +#include <stdio.h> 1.15 +#include <stdint.h> 1.16 +#include <stdlib.h> 1.17 +#include <time.h> 1.18 + 1.19 +#define kVorbisPrivateMaxSize 4000 1.20 +#define UInt64 uint64_t 1.21 + 1.22 +void writeHeader(EbmlGlobal *glob) { 1.23 + EbmlLoc start; 1.24 + Ebml_StartSubElement(glob, &start, EBML); 1.25 + Ebml_SerializeUnsigned(glob, EBMLVersion, 1); 1.26 + Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); // EBML Read Version 1.27 + Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); // EBML Max ID Length 1.28 + Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); // EBML Max Size Length 1.29 + Ebml_SerializeString(glob, DocType, "webm"); // Doc Type 1.30 + Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); // Doc Type Version 1.31 + Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); // Doc Type Read Version 1.32 + Ebml_EndSubElement(glob, &start); 1.33 +} 1.34 + 1.35 +void writeSimpleBlock(EbmlGlobal *glob, unsigned char trackNumber, short timeCode, 1.36 + int isKeyframe, unsigned char lacingFlag, int discardable, 1.37 + unsigned char *data, unsigned long dataLength) { 1.38 + unsigned long blockLength = 4 + dataLength; 1.39 + unsigned char flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable; 1.40 + Ebml_WriteID(glob, SimpleBlock); 1.41 + blockLength |= 0x10000000; // TODO check length < 0x0FFFFFFFF 1.42 + Ebml_Serialize(glob, &blockLength, sizeof(blockLength), 4); 1.43 + trackNumber |= 0x80; // TODO check track nubmer < 128 1.44 + Ebml_Write(glob, &trackNumber, 1); 1.45 + // Ebml_WriteSigned16(glob, timeCode,2); //this is 3 bytes 1.46 + Ebml_Serialize(glob, &timeCode, sizeof(timeCode), 2); 1.47 + flags = 0x00 | (isKeyframe ? 0x80 : 0x00) | (lacingFlag << 1) | discardable; 1.48 + Ebml_Write(glob, &flags, 1); 1.49 + Ebml_Write(glob, data, dataLength); 1.50 +} 1.51 + 1.52 +static UInt64 generateTrackID(unsigned int trackNumber) { 1.53 + UInt64 t = time(NULL) * trackNumber; 1.54 + UInt64 r = rand(); 1.55 + r = r << 32; 1.56 + r += rand(); 1.57 +// UInt64 rval = t ^ r; 1.58 + return t ^ r; 1.59 +} 1.60 + 1.61 +void writeVideoTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing, 1.62 + const char *codecId, unsigned int pixelWidth, unsigned int pixelHeight, 1.63 + unsigned int displayWidth, unsigned int displayHeight, 1.64 + double frameRate) { 1.65 + EbmlLoc start; 1.66 + UInt64 trackID; 1.67 + Ebml_StartSubElement(glob, &start, TrackEntry); 1.68 + Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber); 1.69 + trackID = generateTrackID(trackNumber); 1.70 + Ebml_SerializeUnsigned(glob, TrackUID, trackID); 1.71 + Ebml_SerializeString(glob, CodecName, "VP8"); // TODO shouldn't be fixed 1.72 + 1.73 + Ebml_SerializeUnsigned(glob, TrackType, 1); // video is always 1 1.74 + Ebml_SerializeString(glob, CodecID, codecId); 1.75 + { 1.76 + EbmlLoc videoStart; 1.77 + Ebml_StartSubElement(glob, &videoStart, Video); 1.78 + Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth); 1.79 + Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight); 1.80 + if (pixelWidth != displayWidth) { 1.81 + Ebml_SerializeUnsigned(glob, DisplayWidth, displayWidth); 1.82 + } 1.83 + if (pixelHeight != displayHeight) { 1.84 + Ebml_SerializeUnsigned(glob, DisplayHeight, displayHeight); 1.85 + } 1.86 + Ebml_SerializeFloat(glob, FrameRate, frameRate); 1.87 + Ebml_EndSubElement(glob, &videoStart); // Video 1.88 + } 1.89 + Ebml_EndSubElement(glob, &start); // Track Entry 1.90 +} 1.91 +void writeAudioTrack(EbmlGlobal *glob, unsigned int trackNumber, int flagLacing, 1.92 + const char *codecId, double samplingFrequency, unsigned int channels, 1.93 + unsigned char *private, unsigned long privateSize) { 1.94 + EbmlLoc start; 1.95 + UInt64 trackID; 1.96 + Ebml_StartSubElement(glob, &start, TrackEntry); 1.97 + Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber); 1.98 + trackID = generateTrackID(trackNumber); 1.99 + Ebml_SerializeUnsigned(glob, TrackUID, trackID); 1.100 + Ebml_SerializeUnsigned(glob, TrackType, 2); // audio is always 2 1.101 + // I am using defaults for thesed required fields 1.102 + /* Ebml_SerializeUnsigned(glob, FlagEnabled, 1); 1.103 + Ebml_SerializeUnsigned(glob, FlagDefault, 1); 1.104 + Ebml_SerializeUnsigned(glob, FlagForced, 1); 1.105 + Ebml_SerializeUnsigned(glob, FlagLacing, flagLacing);*/ 1.106 + Ebml_SerializeString(glob, CodecID, codecId); 1.107 + Ebml_SerializeData(glob, CodecPrivate, private, privateSize); 1.108 + 1.109 + Ebml_SerializeString(glob, CodecName, "VORBIS"); // fixed for now 1.110 + { 1.111 + EbmlLoc AudioStart; 1.112 + Ebml_StartSubElement(glob, &AudioStart, Audio); 1.113 + Ebml_SerializeFloat(glob, SamplingFrequency, samplingFrequency); 1.114 + Ebml_SerializeUnsigned(glob, Channels, channels); 1.115 + Ebml_EndSubElement(glob, &AudioStart); 1.116 + } 1.117 + Ebml_EndSubElement(glob, &start); 1.118 +} 1.119 +void writeSegmentInformation(EbmlGlobal *ebml, EbmlLoc *startInfo, unsigned long timeCodeScale, double duration) { 1.120 + Ebml_StartSubElement(ebml, startInfo, Info); 1.121 + Ebml_SerializeUnsigned(ebml, TimecodeScale, timeCodeScale); 1.122 + Ebml_SerializeFloat(ebml, Segment_Duration, duration * 1000.0); // Currently fixed to using milliseconds 1.123 + Ebml_SerializeString(ebml, 0x4D80, "QTmuxingAppLibWebM-0.0.1"); 1.124 + Ebml_SerializeString(ebml, 0x5741, "QTwritingAppLibWebM-0.0.1"); 1.125 + Ebml_EndSubElement(ebml, startInfo); 1.126 +} 1.127 + 1.128 +/* 1.129 +void Mkv_InitializeSegment(Ebml& ebml_out, EbmlLoc& ebmlLoc) 1.130 +{ 1.131 + Ebml_StartSubElement(ebml_out, ebmlLoc, 0x18538067); 1.132 +} 1.133 + 1.134 +void Mkv_InitializeSeek(Ebml& ebml_out, EbmlLoc& ebmlLoc) 1.135 +{ 1.136 + Ebml_StartSubElement(ebml_out, ebmlLoc, 0x114d9b74); 1.137 +} 1.138 +void Mkv_WriteSeekInformation(Ebml& ebml_out, SeekStruct& seekInformation) 1.139 +{ 1.140 + EbmlLoc ebmlLoc; 1.141 + Ebml_StartSubElement(ebml_out, ebmlLoc, 0x4dbb); 1.142 + Ebml_SerializeString(ebml_out, 0x53ab, seekInformation.SeekID); 1.143 + Ebml_SerializeUnsigned(ebml_out, 0x53ac, seekInformation.SeekPosition); 1.144 + Ebml_EndSubElement(ebml_out, ebmlLoc); 1.145 +} 1.146 + 1.147 +void Mkv_WriteSegmentInformation(Ebml& ebml_out, SegmentInformationStruct& segmentInformation) 1.148 +{ 1.149 + Ebml_SerializeUnsigned(ebml_out, 0x73a4, segmentInformation.segmentUID); 1.150 + if (segmentInformation.filename != 0) 1.151 + Ebml_SerializeString(ebml_out, 0x7384, segmentInformation.filename); 1.152 + Ebml_SerializeUnsigned(ebml_out, 0x2AD7B1, segmentInformation.TimecodeScale); 1.153 + Ebml_SerializeUnsigned(ebml_out, 0x4489, segmentInformation.Duration); 1.154 + // TODO date 1.155 + Ebml_SerializeWString(ebml_out, 0x4D80, L"MKVMUX"); 1.156 + Ebml_SerializeWString(ebml_out, 0x5741, segmentInformation.WritingApp); 1.157 +} 1.158 + 1.159 +void Mkv_InitializeTrack(Ebml& ebml_out, EbmlLoc& ebmlLoc) 1.160 +{ 1.161 + Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1654AE6B); 1.162 +} 1.163 + 1.164 +static void Mkv_WriteGenericTrackData(Ebml& ebml_out, TrackStruct& track) 1.165 +{ 1.166 + Ebml_SerializeUnsigned(ebml_out, 0xD7, track.TrackNumber); 1.167 + Ebml_SerializeUnsigned(ebml_out, 0x73C5, track.TrackUID); 1.168 + Ebml_SerializeUnsigned(ebml_out, 0x83, track.TrackType); 1.169 + Ebml_SerializeUnsigned(ebml_out, 0xB9, track.FlagEnabled ? 1 :0); 1.170 + Ebml_SerializeUnsigned(ebml_out, 0x88, track.FlagDefault ? 1 :0); 1.171 + Ebml_SerializeUnsigned(ebml_out, 0x55AA, track.FlagForced ? 1 :0); 1.172 + if (track.Language != 0) 1.173 + Ebml_SerializeString(ebml_out, 0x22B59C, track.Language); 1.174 + if (track.CodecID != 0) 1.175 + Ebml_SerializeString(ebml_out, 0x86, track.CodecID); 1.176 + if (track.CodecPrivate != 0) 1.177 + Ebml_SerializeData(ebml_out, 0x63A2, track.CodecPrivate, track.CodecPrivateLength); 1.178 + if (track.CodecName != 0) 1.179 + Ebml_SerializeWString(ebml_out, 0x258688, track.CodecName); 1.180 +} 1.181 + 1.182 +void Mkv_WriteVideoTrack(Ebml& ebml_out, TrackStruct & track, VideoTrackStruct& video) 1.183 +{ 1.184 + EbmlLoc trackHeadLoc, videoHeadLoc; 1.185 + Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE); // start Track 1.186 + Mkv_WriteGenericTrackData(ebml_out, track); 1.187 + Ebml_StartSubElement(ebml_out, videoHeadLoc, 0xE0); // start Video 1.188 + Ebml_SerializeUnsigned(ebml_out, 0x9A, video.FlagInterlaced ? 1 :0); 1.189 + Ebml_SerializeUnsigned(ebml_out, 0xB0, video.PixelWidth); 1.190 + Ebml_SerializeUnsigned(ebml_out, 0xBA, video.PixelHeight); 1.191 + Ebml_SerializeUnsigned(ebml_out, 0x54B0, video.PixelDisplayWidth); 1.192 + Ebml_SerializeUnsigned(ebml_out, 0x54BA, video.PixelDisplayHeight); 1.193 + Ebml_SerializeUnsigned(ebml_out, 0x54B2, video.displayUnit); 1.194 + Ebml_SerializeFloat(ebml_out, 0x2383E3, video.FrameRate); 1.195 + Ebml_EndSubElement(ebml_out, videoHeadLoc); 1.196 + Ebml_EndSubElement(ebml_out, trackHeadLoc); 1.197 + 1.198 +} 1.199 + 1.200 +void Mkv_WriteAudioTrack(Ebml& ebml_out, TrackStruct & track, AudioTrackStruct& video) 1.201 +{ 1.202 + EbmlLoc trackHeadLoc, audioHeadLoc; 1.203 + Ebml_StartSubElement(ebml_out, trackHeadLoc, 0xAE); 1.204 + Mkv_WriteGenericTrackData(ebml_out, track); 1.205 + Ebml_StartSubElement(ebml_out, audioHeadLoc, 0xE0); // start Audio 1.206 + Ebml_SerializeFloat(ebml_out, 0xB5, video.SamplingFrequency); 1.207 + Ebml_SerializeUnsigned(ebml_out, 0x9F, video.Channels); 1.208 + Ebml_SerializeUnsigned(ebml_out, 0x6264, video.BitDepth); 1.209 + Ebml_EndSubElement(ebml_out, audioHeadLoc); // end audio 1.210 + Ebml_EndSubElement(ebml_out, trackHeadLoc); 1.211 +} 1.212 + 1.213 +void Mkv_WriteEbmlClusterHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, ClusterHeadStruct & clusterHead) 1.214 +{ 1.215 + Ebml_StartSubElement(ebml_out, ebmlLoc, 0x1F43B675); 1.216 + Ebml_SerializeUnsigned(ebml_out, 0x6264, clusterHead.TimeCode); 1.217 +} 1.218 + 1.219 +void Mkv_WriteSimpleBlockHead(Ebml& ebml_out, EbmlLoc& ebmlLoc, SimpleBlockStruct& block) 1.220 +{ 1.221 + Ebml_StartSubElement(ebml_out, ebmlLoc, 0xA3); 1.222 + Ebml_Write1UInt(ebml_out, block.TrackNumber); 1.223 + Ebml_WriteSigned16(ebml_out,block.TimeCode); 1.224 + unsigned char flags = 0x00 | (block.iskey ? 0x80:0x00) | (block.lacing << 1) | block.discardable; 1.225 + Ebml_Write1UInt(ebml_out, flags); // TODO this may be the wrong function 1.226 + Ebml_Serialize(ebml_out, block.data, block.dataLength); 1.227 + Ebml_EndSubElement(ebml_out,ebmlLoc); 1.228 +} 1.229 +*/