[45913] | 1 | /* $Id: EbmlWriter.cpp 45924 2013-05-06 19:34:30Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * EbmlWriter.cpp - EBML writer + WebM container
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (C) 2013 Oracle Corporation
|
---|
| 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | /*
|
---|
| 19 | * This code is based on:
|
---|
| 20 | *
|
---|
| 21 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
---|
| 22 | * Use of this source code is governed by a BSD-style license
|
---|
| 23 | * that can be found in the LICENSE file in the root of the source
|
---|
| 24 | * tree. An additional intellectual property rights grant can be found
|
---|
| 25 | * in the file PATENTS. All contributing project authors may
|
---|
| 26 | * be found in the AUTHORS file in the root of the source tree.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include "EbmlWriter.h"
|
---|
[45922] | 30 | #include <iprt/asm.h>
|
---|
[45913] | 31 | #include <iprt/mem.h>
|
---|
| 32 | #include <iprt/string.h>
|
---|
| 33 | #include <VBox/log.h>
|
---|
| 34 |
|
---|
[45922] | 35 | static int ebml_Write(EbmlGlobal *glob, const void *pv, size_t cb)
|
---|
| 36 | {
|
---|
| 37 | return RTFileWrite(glob->file, pv, cb, NULL);
|
---|
| 38 | }
|
---|
[45913] | 39 |
|
---|
[45922] | 40 | static int ebml_WriteU8(EbmlGlobal *glob, uint8_t u8)
|
---|
[45913] | 41 | {
|
---|
[45922] | 42 | return ebml_Write(glob, &u8, 1);
|
---|
[45913] | 43 | }
|
---|
| 44 |
|
---|
[45922] | 45 | static int ebml_WriteU16(EbmlGlobal *glob, uint16_t u16)
|
---|
[45913] | 46 | {
|
---|
[45922] | 47 | return ebml_Write(glob, &u16, 2);
|
---|
| 48 | }
|
---|
[45913] | 49 |
|
---|
[45922] | 50 | static int ebml_WriteU32(EbmlGlobal *glob, uint32_t u32)
|
---|
| 51 | {
|
---|
| 52 | return ebml_Write(glob, &u32, 4);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | static int ebml_WriteU64(EbmlGlobal *glob, uint64_t u64)
|
---|
| 56 | {
|
---|
| 57 | return ebml_Write(glob, &u64, 8);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | static int ebml_Serialize(EbmlGlobal *glob, const uint8_t *pb, size_t cb)
|
---|
| 61 | {
|
---|
| 62 | for (; cb; cb--)
|
---|
[45913] | 63 | {
|
---|
[45922] | 64 | int rc = ebml_WriteU8(glob, pb[cb-1]);
|
---|
[45913] | 65 | if (RT_FAILURE(rc))
|
---|
| 66 | return rc;
|
---|
| 67 | }
|
---|
| 68 | return VINF_SUCCESS;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[45922] | 71 | static int ebml_WriteID(EbmlGlobal *glob, uint32_t class_id)
|
---|
[45913] | 72 | {
|
---|
[45922] | 73 | int rc;
|
---|
| 74 | if (class_id >= 0x01000000)
|
---|
| 75 | rc = ebml_WriteU32(glob, RT_H2BE_U32(class_id));
|
---|
| 76 | else if (class_id >= 0x00010000)
|
---|
| 77 | rc = ebml_Serialize(glob, (uint8_t*)&class_id, 3);
|
---|
| 78 | else if (class_id >= 0x00000100)
|
---|
| 79 | rc = ebml_WriteU16(glob, RT_H2BE_U16((uint16_t)class_id));
|
---|
| 80 | else
|
---|
| 81 | rc = ebml_WriteU8(glob, (uint8_t)class_id);
|
---|
| 82 | return rc;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | static int ebml_SerializeUnsigned32(EbmlGlobal *glob, uint32_t class_id, uint32_t ui)
|
---|
| 86 | {
|
---|
| 87 | int rc = ebml_WriteID(glob, class_id);
|
---|
| 88 | rc = ebml_WriteU8(glob, 4 | 0x80);
|
---|
[45913] | 89 | if (RT_SUCCESS(rc))
|
---|
[45922] | 90 | rc = ebml_WriteU32(glob, RT_H2BE_U32(ui));
|
---|
[45913] | 91 | return rc;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[45922] | 94 | static int ebml_StartSubElement(EbmlGlobal *glob, uint64_t *ebmlLoc, uint32_t class_id)
|
---|
[45913] | 95 | {
|
---|
| 96 | // todo this is always taking 8 bytes, this may need later optimization
|
---|
| 97 | // this is a key that says lenght unknown
|
---|
| 98 | uint64_t unknownLen = UINT64_C(0x01FFFFFFFFFFFFFF);
|
---|
| 99 |
|
---|
[45922] | 100 | ebml_WriteID(glob, class_id);
|
---|
[45913] | 101 | *ebmlLoc = RTFileTell(glob->file);
|
---|
[45922] | 102 | return ebml_WriteU64(glob, RT_H2BE_U64(unknownLen));
|
---|
[45913] | 103 | }
|
---|
| 104 |
|
---|
[45922] | 105 | static int ebml_EndSubElement(EbmlGlobal *glob, uint64_t ebmlLoc)
|
---|
[45913] | 106 | {
|
---|
| 107 | /* Save the current file pointer */
|
---|
| 108 | uint64_t pos = RTFileTell(glob->file);
|
---|
| 109 |
|
---|
| 110 | /* Calculate the size of this element */
|
---|
[45922] | 111 | uint64_t size = pos - ebmlLoc - 8;
|
---|
[45913] | 112 | size |= UINT64_C(0x0100000000000000);
|
---|
| 113 |
|
---|
| 114 | /* Seek back to the beginning of the element and write the new size */
|
---|
[45922] | 115 | RTFileSeek(glob->file, ebmlLoc, RTFILE_SEEK_BEGIN, NULL);
|
---|
| 116 | int rc = ebml_WriteU64(glob, RT_H2BE_U64(size));
|
---|
[45913] | 117 |
|
---|
| 118 | /* Reset the file pointer */
|
---|
| 119 | RTFileSeek(glob->file, pos, RTFILE_SEEK_BEGIN, NULL);
|
---|
| 120 |
|
---|
| 121 | return rc;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[45922] | 124 | static int ebml_WriteLen(EbmlGlobal *glob, uint64_t val)
|
---|
[45913] | 125 | {
|
---|
| 126 | //TODO check and make sure we are not > than 0x0100000000000000LLU
|
---|
[45922] | 127 | size_t size = 8;
|
---|
[45913] | 128 | uint64_t minVal = UINT64_C(0x00000000000000ff); //mask to compare for byte size
|
---|
| 129 |
|
---|
| 130 | for (size = 1; size < 8; size ++)
|
---|
| 131 | {
|
---|
| 132 | if (val < minVal)
|
---|
| 133 | break;
|
---|
| 134 |
|
---|
| 135 | minVal = (minVal << 7);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | val |= (UINT64_C(0x000000000000080) << ((size - 1) * 7));
|
---|
| 139 |
|
---|
[45922] | 140 | return ebml_Serialize(glob, (uint8_t *)&val, size);
|
---|
[45913] | 141 | }
|
---|
| 142 |
|
---|
[45922] | 143 | static int ebml_WriteString(EbmlGlobal *glob, const char *str)
|
---|
[45913] | 144 | {
|
---|
[45922] | 145 | const size_t cb = strlen(str);
|
---|
| 146 | int rc = ebml_WriteLen(glob, cb);
|
---|
[45913] | 147 | //TODO: it's not clear from the spec whether the nul terminator
|
---|
| 148 | //should be serialized too. For now we omit the null terminator.
|
---|
| 149 | if (RT_SUCCESS(rc))
|
---|
[45922] | 150 | rc = ebml_Write(glob, str, cb);
|
---|
[45913] | 151 | return rc;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | int Ebml_SerializeUnsigned64(EbmlGlobal *glob, uint32_t class_id, uint64_t ui)
|
---|
| 155 | {
|
---|
[45922] | 156 | int rc = ebml_WriteID(glob, class_id);
|
---|
[45913] | 157 | if (RT_SUCCESS(rc))
|
---|
[45922] | 158 | rc = ebml_WriteU8(glob, 8 | 0x80);
|
---|
[45913] | 159 | if (RT_SUCCESS(rc))
|
---|
[45922] | 160 | rc = ebml_WriteU64(glob, RT_H2BE_U64(ui));
|
---|
[45913] | 161 | return rc;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | int Ebml_SerializeUnsigned(EbmlGlobal *glob, uint32_t class_id, uint32_t ui)
|
---|
| 165 | {
|
---|
[45922] | 166 | int rc = ebml_WriteID(glob, class_id);
|
---|
[45913] | 167 | if (RT_FAILURE(rc))
|
---|
| 168 | return rc;
|
---|
| 169 |
|
---|
[45922] | 170 | uint32_t minVal = 0x7fLU; //mask to compare for byte size
|
---|
| 171 | size_t size = 8; //size in bytes to output
|
---|
[45913] | 172 | for (size = 1; size < 4; size ++)
|
---|
| 173 | {
|
---|
| 174 | if (ui < minVal)
|
---|
| 175 | break;
|
---|
| 176 |
|
---|
| 177 | minVal <<= 7;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[45922] | 180 | rc = ebml_WriteU8(glob, 0x80 | size);
|
---|
[45913] | 181 | if (RT_SUCCESS(rc))
|
---|
[45922] | 182 | rc = ebml_Serialize(glob, (uint8_t*)&ui, size);
|
---|
[45913] | 183 | return rc;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | //TODO: perhaps this is a poor name for this id serializer helper function
|
---|
| 187 | int Ebml_SerializeBinary(EbmlGlobal *glob, uint32_t class_id, uint32_t bin)
|
---|
| 188 | {
|
---|
| 189 | int size;
|
---|
| 190 | for (size = 4; size > 1; size--)
|
---|
| 191 | {
|
---|
| 192 | if (bin & 0x000000ff << ((size-1) * 8))
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
[45922] | 195 | int rc = ebml_WriteID(glob, class_id);
|
---|
[45913] | 196 | if (RT_SUCCESS(rc))
|
---|
[45922] | 197 | rc = ebml_WriteLen(glob, size);
|
---|
[45913] | 198 | if (RT_SUCCESS(rc))
|
---|
[45922] | 199 | rc = ebml_WriteID(glob, bin);
|
---|
[45913] | 200 | return rc;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | int Ebml_SerializeFloat(EbmlGlobal *glob, uint32_t class_id, double d)
|
---|
| 204 | {
|
---|
[45922] | 205 | int rc = ebml_WriteID(glob, class_id);
|
---|
[45913] | 206 | if (RT_SUCCESS(rc))
|
---|
[45922] | 207 | rc = ebml_WriteU8(glob, 0x80 | 8);
|
---|
[45913] | 208 | if (RT_SUCCESS(rc))
|
---|
[45922] | 209 | rc = ebml_WriteU64(glob, RT_H2BE_U64(*(uint64_t*)&d));
|
---|
[45913] | 210 | return rc;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | int Ebml_SerializeString(EbmlGlobal *glob, uint32_t class_id, const char *s)
|
---|
| 214 | {
|
---|
[45922] | 215 | int rc = ebml_WriteID(glob, class_id);
|
---|
[45913] | 216 | if (RT_SUCCESS(rc))
|
---|
[45922] | 217 | rc = ebml_WriteString(glob, s);
|
---|
[45913] | 218 | return rc;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | int Ebml_WriteWebMSeekElement(EbmlGlobal *ebml, uint32_t id, uint64_t pos)
|
---|
| 222 | {
|
---|
| 223 | uint64_t offset = pos - ebml->position_reference;
|
---|
| 224 | uint64_t start;
|
---|
[45922] | 225 | int rc = ebml_StartSubElement(ebml, &start, Seek);
|
---|
[45913] | 226 | if (RT_SUCCESS(rc))
|
---|
| 227 | rc = Ebml_SerializeBinary(ebml, SeekID, id);
|
---|
| 228 | if (RT_SUCCESS(rc))
|
---|
| 229 | rc = Ebml_SerializeUnsigned64(ebml, SeekPosition, offset);
|
---|
| 230 | if (RT_SUCCESS(rc))
|
---|
[45922] | 231 | rc = ebml_EndSubElement(ebml, start);
|
---|
[45913] | 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | int Ebml_WriteWebMSeekInfo(EbmlGlobal *ebml)
|
---|
| 236 | {
|
---|
| 237 | int rc = VINF_SUCCESS;
|
---|
| 238 |
|
---|
| 239 | /* Save the current file pointer */
|
---|
[45922] | 240 | uint64_t pos = RTFileTell(ebml->file);
|
---|
[45913] | 241 |
|
---|
| 242 | if (ebml->seek_info_pos)
|
---|
| 243 | rc = RTFileSeek(ebml->file, ebml->seek_info_pos, RTFILE_SEEK_BEGIN, NULL);
|
---|
| 244 | else
|
---|
| 245 | ebml->seek_info_pos = pos;
|
---|
| 246 |
|
---|
| 247 | {
|
---|
| 248 | uint64_t start;
|
---|
| 249 |
|
---|
| 250 | if (RT_SUCCESS(rc))
|
---|
[45922] | 251 | rc = ebml_StartSubElement(ebml, &start, SeekHead);
|
---|
[45913] | 252 | if (RT_SUCCESS(rc))
|
---|
| 253 | rc = Ebml_WriteWebMSeekElement(ebml, Tracks, ebml->track_pos);
|
---|
| 254 | if (RT_SUCCESS(rc))
|
---|
[45922] | 255 | rc = Ebml_WriteWebMSeekElement(ebml, Cues, ebml->cue_pos);
|
---|
[45913] | 256 | if (RT_SUCCESS(rc))
|
---|
[45922] | 257 | rc = Ebml_WriteWebMSeekElement(ebml, Info, ebml->segment_info_pos);
|
---|
[45913] | 258 | if (RT_SUCCESS(rc))
|
---|
[45922] | 259 | rc = ebml_EndSubElement(ebml, start);
|
---|
[45913] | 260 | }
|
---|
| 261 | {
|
---|
| 262 | //segment info
|
---|
| 263 | uint64_t startInfo;
|
---|
| 264 | uint64_t frame_time;
|
---|
| 265 |
|
---|
[45922] | 266 | frame_time = (uint64_t)1000 * ebml->framerate.den / ebml->framerate.num;
|
---|
[45913] | 267 | ebml->segment_info_pos = RTFileTell(ebml->file);
|
---|
| 268 | if (RT_SUCCESS(rc))
|
---|
[45922] | 269 | rc = ebml_StartSubElement(ebml, &startInfo, Info);
|
---|
[45913] | 270 | if (RT_SUCCESS(rc))
|
---|
| 271 | rc = Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
|
---|
| 272 | if (RT_SUCCESS(rc))
|
---|
| 273 | rc = Ebml_SerializeFloat(ebml, Segment_Duration,
|
---|
| 274 | (double)(ebml->last_pts_ms + frame_time));
|
---|
| 275 | char szVersion[64];
|
---|
| 276 | RTStrPrintf(szVersion, sizeof(szVersion), "vpxenc%",
|
---|
| 277 | ebml->debug ? vpx_codec_version_str() : "");
|
---|
| 278 | if (RT_SUCCESS(rc))
|
---|
[45922] | 279 | rc = Ebml_SerializeString(ebml, MuxingApp, szVersion);
|
---|
[45913] | 280 | if (RT_SUCCESS(rc))
|
---|
[45922] | 281 | rc = Ebml_SerializeString(ebml, WritingApp, szVersion);
|
---|
[45913] | 282 | if (RT_SUCCESS(rc))
|
---|
[45922] | 283 | rc = ebml_EndSubElement(ebml, startInfo);
|
---|
[45913] | 284 | }
|
---|
| 285 | return rc;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | int Ebml_WriteWebMFileHeader(EbmlGlobal *glob,
|
---|
| 289 | const vpx_codec_enc_cfg_t *cfg,
|
---|
| 290 | const struct vpx_rational *fps)
|
---|
| 291 | {
|
---|
| 292 | int rc = VINF_SUCCESS;
|
---|
| 293 | {
|
---|
| 294 | uint64_t start;
|
---|
[45922] | 295 | rc = ebml_StartSubElement(glob, &start, EBML);
|
---|
[45913] | 296 | if (RT_SUCCESS(rc))
|
---|
| 297 | rc = Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
|
---|
| 298 | if (RT_SUCCESS(rc))
|
---|
[45922] | 299 | rc = Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1);
|
---|
[45913] | 300 | if (RT_SUCCESS(rc))
|
---|
[45922] | 301 | rc = Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4);
|
---|
[45913] | 302 | if (RT_SUCCESS(rc))
|
---|
[45922] | 303 | rc = Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8);
|
---|
[45913] | 304 | if (RT_SUCCESS(rc))
|
---|
[45922] | 305 | rc = Ebml_SerializeString(glob, DocType, "webm");
|
---|
[45913] | 306 | if (RT_SUCCESS(rc))
|
---|
[45922] | 307 | rc = Ebml_SerializeUnsigned(glob, DocTypeVersion, 2);
|
---|
[45913] | 308 | if (RT_SUCCESS(rc))
|
---|
[45922] | 309 | rc = Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2);
|
---|
[45913] | 310 | if (RT_SUCCESS(rc))
|
---|
[45922] | 311 | rc = ebml_EndSubElement(glob, start);
|
---|
[45913] | 312 | }
|
---|
| 313 | {
|
---|
| 314 | if (RT_SUCCESS(rc))
|
---|
[45922] | 315 | rc = ebml_StartSubElement(glob, &glob->startSegment, Segment);
|
---|
[45913] | 316 | glob->position_reference = RTFileTell(glob->file);
|
---|
| 317 | glob->framerate = *fps;
|
---|
| 318 | if (RT_SUCCESS(rc))
|
---|
| 319 | rc = Ebml_WriteWebMSeekInfo(glob);
|
---|
| 320 | {
|
---|
| 321 | uint64_t trackStart;
|
---|
| 322 | glob->track_pos = RTFileTell(glob->file);
|
---|
| 323 | if (RT_SUCCESS(rc))
|
---|
[45922] | 324 | rc = ebml_StartSubElement(glob, &trackStart, Tracks);
|
---|
[45913] | 325 | {
|
---|
[45922] | 326 | uint32_t trackNumber = 1;
|
---|
| 327 | uint32_t trackID = 0;
|
---|
[45913] | 328 |
|
---|
| 329 | uint64_t start;
|
---|
| 330 | if (RT_SUCCESS(rc))
|
---|
[45922] | 331 | rc = ebml_StartSubElement(glob, &start, TrackEntry);
|
---|
[45913] | 332 | if (RT_SUCCESS(rc))
|
---|
| 333 | rc = Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
|
---|
| 334 | glob->track_id_pos = RTFileTell(glob->file);
|
---|
[45922] | 335 | ebml_SerializeUnsigned32(glob, TrackUID, trackID);
|
---|
[45913] | 336 | Ebml_SerializeUnsigned(glob, TrackType, 1); //video is always 1
|
---|
| 337 | Ebml_SerializeString(glob, CodecID, "V_VP8");
|
---|
| 338 | {
|
---|
| 339 | uint64_t videoStart;
|
---|
| 340 | if (RT_SUCCESS(rc))
|
---|
[45922] | 341 | rc = ebml_StartSubElement(glob, &videoStart, Video);
|
---|
| 342 | uint32_t pixelWidth = cfg->g_w;
|
---|
[45913] | 343 | if (RT_SUCCESS(rc))
|
---|
| 344 | rc = Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
|
---|
[45922] | 345 | uint32_t pixelHeight = cfg->g_h;
|
---|
[45913] | 346 | if (RT_SUCCESS(rc))
|
---|
| 347 | rc = Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
|
---|
[45922] | 348 | double frameRate = (double)fps->num / (double)fps->den;
|
---|
[45913] | 349 | if (RT_SUCCESS(rc))
|
---|
| 350 | rc = Ebml_SerializeFloat(glob, FrameRate, frameRate);
|
---|
| 351 | if (RT_SUCCESS(rc))
|
---|
[45922] | 352 | rc = ebml_EndSubElement(glob, videoStart);
|
---|
[45913] | 353 | }
|
---|
| 354 | if (RT_SUCCESS(rc))
|
---|
[45922] | 355 | rc = ebml_EndSubElement(glob, start); //Track Entry
|
---|
[45913] | 356 | }
|
---|
| 357 | if (RT_SUCCESS(rc))
|
---|
[45922] | 358 | rc = ebml_EndSubElement(glob, trackStart);
|
---|
[45913] | 359 | }
|
---|
| 360 | // segment element is open
|
---|
| 361 | }
|
---|
| 362 | return rc;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | int Ebml_WriteWebMBlock(EbmlGlobal *glob,
|
---|
| 366 | const vpx_codec_enc_cfg_t *cfg,
|
---|
| 367 | const vpx_codec_cx_pkt_t *pkt)
|
---|
| 368 | {
|
---|
[45922] | 369 | uint16_t block_timecode = 0;
|
---|
| 370 | int64_t pts_ms;
|
---|
| 371 | int start_cluster = 0;
|
---|
| 372 | int rc = VINF_SUCCESS;
|
---|
[45913] | 373 |
|
---|
| 374 | /* Calculate the PTS of this frame in milliseconds */
|
---|
| 375 | pts_ms = pkt->data.frame.pts * 1000
|
---|
| 376 | * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
|
---|
| 377 | if (pts_ms <= glob->last_pts_ms)
|
---|
| 378 | pts_ms = glob->last_pts_ms + 1;
|
---|
| 379 | glob->last_pts_ms = pts_ms;
|
---|
| 380 |
|
---|
| 381 | /* Calculate the relative time of this block */
|
---|
| 382 | if (pts_ms - glob->cluster_timecode > 65536)
|
---|
| 383 | start_cluster = 1;
|
---|
| 384 | else
|
---|
[45922] | 385 | block_timecode = (uint16_t)(pts_ms - glob->cluster_timecode);
|
---|
[45913] | 386 |
|
---|
[45922] | 387 | int fKeyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
|
---|
| 388 | if (start_cluster || fKeyframe)
|
---|
[45913] | 389 | {
|
---|
| 390 | if (glob->cluster_open)
|
---|
[45922] | 391 | rc = ebml_EndSubElement(glob, glob->startCluster);
|
---|
[45913] | 392 |
|
---|
| 393 | /* Open the new cluster */
|
---|
| 394 | block_timecode = 0;
|
---|
| 395 | glob->cluster_open = 1;
|
---|
[45914] | 396 | glob->cluster_timecode = (uint32_t)pts_ms;
|
---|
[45913] | 397 | glob->cluster_pos = RTFileTell(glob->file);
|
---|
| 398 | if (RT_SUCCESS(rc))
|
---|
[45922] | 399 | rc = ebml_StartSubElement(glob, &glob->startCluster, Cluster); //cluster
|
---|
[45913] | 400 | if (RT_SUCCESS(rc))
|
---|
| 401 | rc = Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
|
---|
| 402 |
|
---|
| 403 | /* Save a cue point if this is a keyframe. */
|
---|
[45922] | 404 | if (fKeyframe)
|
---|
[45913] | 405 | {
|
---|
| 406 | struct cue_entry *cue;
|
---|
| 407 |
|
---|
| 408 | glob->cue_list = (cue_entry*)RTMemRealloc(glob->cue_list, (glob->cues+1) * sizeof(cue_entry));
|
---|
| 409 | cue = &glob->cue_list[glob->cues];
|
---|
| 410 | cue->time = glob->cluster_timecode;
|
---|
| 411 | cue->loc = glob->cluster_pos;
|
---|
| 412 | glob->cues++;
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /* Write the Simple Block */
|
---|
| 417 | if (RT_SUCCESS(rc))
|
---|
[45922] | 418 | rc = ebml_WriteID(glob, SimpleBlock);
|
---|
[45913] | 419 |
|
---|
[45924] | 420 | uint32_t block_length = pkt->data.frame.sz + 4;
|
---|
[45913] | 421 | block_length |= 0x10000000;
|
---|
| 422 | if (RT_SUCCESS(rc))
|
---|
[45922] | 423 | rc = ebml_WriteU32(glob, RT_H2BE_U32(block_length));
|
---|
[45913] | 424 |
|
---|
[45922] | 425 | uint8_t track_number = 0x80 | 1;
|
---|
[45913] | 426 | if (RT_SUCCESS(rc))
|
---|
[45922] | 427 | rc = ebml_WriteU8(glob, track_number);
|
---|
[45913] | 428 |
|
---|
| 429 | if (RT_SUCCESS(rc))
|
---|
[45922] | 430 | rc = ebml_WriteU16(glob, RT_H2BE_U16(block_timecode));
|
---|
[45913] | 431 |
|
---|
[45922] | 432 | uint8_t flags = 0;
|
---|
| 433 | if (fKeyframe)
|
---|
[45913] | 434 | flags |= 0x80;
|
---|
| 435 | if (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
|
---|
| 436 | flags |= 0x08;
|
---|
| 437 | if (RT_SUCCESS(rc))
|
---|
[45922] | 438 | rc = ebml_WriteU8(glob, flags);
|
---|
[45913] | 439 |
|
---|
| 440 | if (RT_SUCCESS(rc))
|
---|
[45922] | 441 | rc = ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
|
---|
[45913] | 442 |
|
---|
| 443 | return rc;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | int Ebml_WriteWebMFileFooter(EbmlGlobal *glob, long hash)
|
---|
| 447 | {
|
---|
| 448 | int rc = VINF_SUCCESS;
|
---|
| 449 | if (glob->cluster_open)
|
---|
[45922] | 450 | rc = ebml_EndSubElement(glob, glob->startCluster);
|
---|
[45913] | 451 |
|
---|
| 452 | {
|
---|
| 453 | uint64_t start;
|
---|
| 454 |
|
---|
| 455 | glob->cue_pos = RTFileTell(glob->file);
|
---|
| 456 | if (RT_SUCCESS(rc))
|
---|
[45922] | 457 | rc = ebml_StartSubElement(glob, &start, Cues);
|
---|
| 458 | for (unsigned i = 0; i < glob->cues; i++)
|
---|
[45913] | 459 | {
|
---|
| 460 | struct cue_entry *cue = &glob->cue_list[i];
|
---|
| 461 | uint64_t startSub;
|
---|
| 462 |
|
---|
| 463 | if (RT_SUCCESS(rc))
|
---|
[45922] | 464 | rc = ebml_StartSubElement(glob, &startSub, CuePoint);
|
---|
[45913] | 465 | {
|
---|
| 466 | uint64_t startSubsub;
|
---|
| 467 |
|
---|
| 468 | if (RT_SUCCESS(rc))
|
---|
| 469 | rc = Ebml_SerializeUnsigned(glob, CueTime, cue->time);
|
---|
| 470 | if (RT_SUCCESS(rc))
|
---|
[45922] | 471 | rc = ebml_StartSubElement(glob, &startSubsub, CueTrackPositions);
|
---|
[45913] | 472 | if (RT_SUCCESS(rc))
|
---|
| 473 | rc = Ebml_SerializeUnsigned(glob, CueTrack, 1);
|
---|
| 474 | if (RT_SUCCESS(rc))
|
---|
| 475 | rc = Ebml_SerializeUnsigned64(glob, CueClusterPosition,
|
---|
| 476 | cue->loc - glob->position_reference);
|
---|
| 477 | //Ebml_SerializeUnsigned(glob, CueBlockNumber, cue->blockNumber);
|
---|
| 478 | if (RT_SUCCESS(rc))
|
---|
[45922] | 479 | rc = ebml_EndSubElement(glob, startSubsub);
|
---|
[45913] | 480 | }
|
---|
| 481 | if (RT_SUCCESS(rc))
|
---|
[45922] | 482 | rc = ebml_EndSubElement(glob, startSub);
|
---|
[45913] | 483 | }
|
---|
| 484 | if (RT_SUCCESS(rc))
|
---|
[45922] | 485 | rc = ebml_EndSubElement(glob, start);
|
---|
[45913] | 486 | }
|
---|
| 487 |
|
---|
| 488 | if (RT_SUCCESS(rc))
|
---|
[45922] | 489 | rc = ebml_EndSubElement(glob, glob->startSegment);
|
---|
[45913] | 490 |
|
---|
| 491 | /* Patch up the seek info block */
|
---|
| 492 | if (RT_SUCCESS(rc))
|
---|
| 493 | rc = Ebml_WriteWebMSeekInfo(glob);
|
---|
| 494 |
|
---|
| 495 | /* Patch up the track id */
|
---|
| 496 | if (RT_SUCCESS(rc))
|
---|
| 497 | rc = RTFileSeek(glob->file, glob->track_id_pos, RTFILE_SEEK_BEGIN, NULL);
|
---|
| 498 | if (RT_SUCCESS(rc))
|
---|
[45922] | 499 | rc = ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
|
---|
[45913] | 500 |
|
---|
| 501 | if (RT_SUCCESS(rc))
|
---|
| 502 | rc = RTFileSeek(glob->file, 0, RTFILE_SEEK_END, NULL);
|
---|
| 503 | return rc;
|
---|
| 504 | }
|
---|