1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 x264 project
7 * Authors: Mike Matsnev
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
29 int mk_seekFile(mk_Writer
*w
, uint64_t pos
) {
30 if (fseek(w
->fp
, pos
, SEEK_SET
))
41 char *mk_laceXiph(uint64_t *sizes
, uint8_t num_frames
, uint64_t *output_size
) {
44 uint64_t alloc_size
= num_frames
* 6; // Complete guess. We'll realloc if we need more space, though.
45 char *laced
= calloc(alloc_size
, sizeof(char));
49 laced
[offset
++] = num_frames
;
50 for (i
= 0; i
< num_frames
; i
++)
52 for (j
= sizes
[i
]; j
>= 255 ; j
-= 255)
54 laced
[offset
++] = 255;
55 if (offset
+ 1 >= alloc_size
) {
56 int avg_sz
= offset
/ (i
- 1); // Compute approximate average bytes/frame
57 alloc_size
+= avg_sz
* (num_frames
- i
); // Add our average + number of frames left to size
58 if ((laced
= realloc(laced
, alloc_size
)) == NULL
)
65 if (output_size
!= NULL
)
66 *output_size
= offset
;
71 mk_Writer
*mk_createWriter(const char *filename
, int64_t timescale
, uint8_t vlc_compat
) {
72 mk_Writer
*w
= calloc(1, sizeof(*w
));
76 w
->root
= mk_createContext(w
, NULL
, 0);
77 if (w
->root
== NULL
) {
82 if ((w
->cues
= mk_createContext(w
, w
->root
, 0x1c53bb6b)) == NULL
) // Cues
84 mk_destroyContexts(w
);
90 if ((w
->cluster
.seekhead
= mk_createContext(w
, w
->root
, 0x114d9b74)) == NULL
) // SeekHead
92 mk_destroyContexts(w
);
98 w
->fp
= fopen(filename
, "wb");
100 mk_destroyContexts(w
);
105 w
->timescale
= timescale
;
106 w
->vlc_compat
= vlc_compat
;
111 int mk_writeHeader(mk_Writer
*w
, const char *writingApp
) {
118 md5_starts(&w
->segment_md5
); /* Initalize MD5 */
120 if ((c
= mk_createContext(w
, w
->root
, 0x1a45dfa3)) == NULL
) // EBML
122 CHECK(mk_writeUInt(c
, 0x4286, 1)); // EBMLVersion
123 CHECK(mk_writeUInt(c
, 0x42f7, 1)); // EBMLReadVersion
124 CHECK(mk_writeUInt(c
, 0x42f2, 4)); // EBMLMaxIDLength
125 CHECK(mk_writeUInt(c
, 0x42f3, 8)); // EBMLMaxSizeLength
126 CHECK(mk_writeStr(c
, 0x4282, "matroska")); // DocType
127 CHECK(mk_writeUInt(c
, 0x4287, 1)); // DocTypeVersion
128 CHECK(mk_writeUInt(c
, 0x4285, 1)); // DocTypeReadversion
129 CHECK(mk_closeContext(c
, 0));
131 if ((c
= mk_createContext(w
, w
->root
, 0x18538067)) == NULL
) // Segment
133 CHECK(mk_flushContextID(c
));
134 w
->segment_ptr
= c
->d_cur
;
135 CHECK(mk_closeContext(c
, &w
->segment_ptr
));
138 CHECK(mk_writeVoid(w
->root
, 0x100)); // 256 bytes should be enough room for our Seek entries.
139 CHECK(mk_writeVoid(w
->root
, 0x800)); // 2048 bytes for Chapters.
142 w
->seek_data
.seekhead
= 0x80000000;
143 CHECK(mk_writeSeekHead(w
, &w
->seekhead_ptr
));
144 w
->seek_data
.seekhead
= 0;
147 if ((c
= mk_createContext(w
, w
->root
, 0x1549a966)) == NULL
) // SegmentInfo
149 w
->seek_data
.segmentinfo
= w
->root
->d_cur
- w
->segment_ptr
;
150 CHECK(mk_writeVoid(c
, 16)); /* Reserve space for a SegmentUID, we'll write the it later. */
151 CHECK(mk_writeStr(c
, 0x4d80, PACKAGE_STRING
)); // MuxingApp
152 CHECK(mk_writeStr(c
, 0x5741, writingApp
)); // WritingApp
153 CHECK(mk_writeUInt(c
, 0x2ad7b1, w
->timescale
)); // TimecodeScale
154 CHECK(mk_writeFloat(c
, 0x4489, 0)); // Duration
155 w
->duration_ptr
= c
->d_cur
- 4;
156 CHECK(mk_closeContext(c
, &offset
));
157 w
->duration_ptr
+= offset
;
158 w
->segmentuid_ptr
= offset
;
160 w
->seek_data
.tracks
= w
->root
->d_cur
- w
->segment_ptr
;
163 CHECK(mk_closeContext(w
->tracks
, 0));
165 CHECK(mk_flushContextData(w
->root
));
168 w
->def_duration
= w
->tracks_arr
[0]->default_duration
;
172 static int mk_closeCluster(mk_Writer
*w
) {
173 if (w
->cluster
.context
== NULL
)
176 CHECK(mk_closeContext(w
->cluster
.context
, 0));
177 w
->cluster
.context
= NULL
;
178 CHECK(mk_flushContextData(w
->root
));
182 int mk_flushFrame(mk_Writer
*w
, mk_Track
*track
) {
184 int64_t delta
, ref
= 0;
185 unsigned fsize
, bgsize
;
186 uint8_t flags
, c_delta
[2];
191 if (!track
->in_frame
)
194 delta
= track
->frame
.timecode
/w
->timescale
- w
->cluster
.tc_scaled
;
195 if (delta
> 2000ll || delta
< -2000ll)
196 CHECK(mk_closeCluster(w
));
198 if (w
->cluster
.context
== NULL
) {
199 w
->cluster
.tc_scaled
= track
->frame
.timecode
/ w
->timescale
;
200 w
->cluster
.context
= mk_createContext(w
, w
->root
, 0x1f43b675); // Cluster
201 if (w
->cluster
.context
== NULL
)
204 w
->cluster
.pointer
= w
->f_pos
- w
->segment_ptr
;
207 CHECK(mk_writeSeek(w
, w
->cluster
.seekhead
, 0x1f43b675, w
->cluster
.pointer
));
209 CHECK(mk_writeUInt(w
->cluster
.context
, 0xe7, w
->cluster
.tc_scaled
)); // Cluster Timecode
212 w
->cluster
.block_count
= 0;
215 /* Calculate the encoded lacing sizes. */
216 switch (track
->frame
.lacing
) {
218 laced
= mk_laceXiph(track
->frame
.lacing_sizes
, track
->frame
.lacing_num_frames
, &length
);
223 length
+= mk_ebmlSizeSize(track
->frame
.lacing_sizes
[0]) + 1; // Add one for the frame count.
224 for (i
= 1; i
< track
->frame
.lacing_num_frames
; i
++)
226 u_size
= llabs(track
->frame
.lacing_sizes
[i
] - track
->frame
.lacing_sizes
[i
-1]);
227 length
+= mk_ebmlSizeSize((u_size
) << 1); // Shift by one so we get the right size for a signed number.
231 case MK_LACING_FIXED
:
233 laced
= calloc(1, sizeof(char));
234 laced
[0] = track
->frame
.lacing_num_frames
;
240 fsize
= track
->frame
.data
? track
->frame
.data
->d_cur
: 0;
241 bgsize
= fsize
+ 4 + mk_ebmlSizeSize(fsize
+ 4 + length
) + 1 + length
;
242 if (!track
->frame
.keyframe
) {
243 ref
= track
->prev_frame_tc_scaled
- w
->cluster
.tc_scaled
- delta
;
244 bgsize
+= 1 + 1 + mk_ebmlSIntSize(ref
);
247 CHECK(mk_writeID(w
->cluster
.context
, 0xa0)); // BlockGroup
248 CHECK(mk_writeSize(w
->cluster
.context
, bgsize
));
249 CHECK(mk_writeID(w
->cluster
.context
, 0xa1)); // Block
250 CHECK(mk_writeSize(w
->cluster
.context
, fsize
+ 4 + length
)); // Block size
251 CHECK(mk_writeSize(w
->cluster
.context
, track
->track_id
)); // track number
253 w
->cluster
.block_count
++;
255 c_delta
[0] = delta
>> 8;
257 CHECK(mk_appendContextData(w
->cluster
.context
, c_delta
, 2)); // Timecode relative to Cluster.
259 // flags = ( track->frame.keyframe << 8 ) | track->frame.lacing;
260 flags
= track
->frame
.lacing
<< 1; // Flags: Bit 5-6 describe what type of lacing to use.
261 CHECK(mk_appendContextData(w
->cluster
.context
, &flags
, 1));
262 if (track
->frame
.lacing
) {
263 if (track
->frame
.lacing
== MK_LACING_EBML
) {
264 CHECK(mk_appendContextData(w
->cluster
.context
, &track
->frame
.lacing_num_frames
, 1)); // Number of frames in lace-1
265 CHECK(mk_writeSize(w
->cluster
.context
, track
->frame
.lacing_sizes
[0])); // Size of 1st frame.
266 for (i
= 1; i
< track
->frame
.lacing_num_frames
; i
++)
268 CHECK(mk_writeSSize(w
->cluster
.context
, track
->frame
.lacing_sizes
[i
] - track
->frame
.lacing_sizes
[i
-1])); // Size difference between previous size and this size.
270 } else if (length
> 0 && laced
!= NULL
) {
271 CHECK(mk_appendContextData(w
->cluster
.context
, laced
, length
));
277 if (track
->frame
.data
) {
278 CHECK(mk_appendContextData(w
->cluster
.context
, track
->frame
.data
->data
, track
->frame
.data
->d_cur
));
279 track
->frame
.data
->d_cur
= 0;
281 if (!track
->frame
.keyframe
)
282 CHECK(mk_writeSInt(w
->cluster
.context
, 0xfb, ref
)); // ReferenceBlock
284 if (track
->frame
.keyframe
&& (track
->track_type
& MK_TRACK_VIDEO
) && ((track
->prev_cue_pos
+ 3*CLSIZE
) <= w
->f_pos
|| track
->frame
.timecode
== 0)) {
285 if ((c
= mk_createContext(w
, w
->cues
, 0xbb)) == NULL
) // CuePoint
287 CHECK(mk_writeUInt(c
, 0xb3, track
->frame
.timecode
)); // CueTime
289 if ((tp
= mk_createContext(w
, c
, 0xb7)) == NULL
) // CueTrackPositions
291 CHECK(mk_writeUInt(tp
, 0xf7, track
->track_id
)); // CueTrack
292 CHECK(mk_writeUInt(tp
, 0xf1, w
->cluster
.pointer
)); // CueClusterPosition
293 // CHECK(mk_writeUInt(c, 0x5378, w->cluster.block_count)); // CueBlockNumber
294 CHECK(mk_closeContext(tp
, 0));
295 CHECK(mk_closeContext(c
, 0));
296 track
->prev_cue_pos
= w
->f_pos
;
300 track
->prev_frame_tc_scaled
= w
->cluster
.tc_scaled
+ delta
;
302 if (w
->cluster
.context
->d_cur
> CLSIZE
)
303 CHECK(mk_closeCluster(w
));
308 int mk_startFrame(mk_Writer
*w
, mk_Track
*track
) {
309 if (mk_flushFrame(w
, track
) < 0)
313 track
->frame
.keyframe
= 0;
314 track
->frame
.lacing
= MK_LACING_NONE
;
315 track
->frame
.lacing_num_frames
= 0;
316 track
->frame
.lacing_sizes
= NULL
;
321 int mk_setFrameFlags(mk_Writer
*w
, mk_Track
*track
, int64_t timestamp
, unsigned keyframe
) {
322 if (!track
->in_frame
)
325 track
->frame
.timecode
= timestamp
;
326 track
->frame
.keyframe
= keyframe
!= 0;
328 if (track
->max_frame_tc
< timestamp
)
329 track
->max_frame_tc
= timestamp
;
334 int mk_setFrameLacing(mk_Writer
*w
, mk_Track
*track
, uint8_t lacing
, uint8_t num_frames
, uint64_t sizes
[]) {
335 if (!track
->in_frame
)
337 track
->frame
.lacing_sizes
= calloc(num_frames
, sizeof(uint64_t));
339 track
->frame
.lacing
= lacing
;
340 track
->frame
.lacing_num_frames
= num_frames
;
341 memcpy(track
->frame
.lacing_sizes
, sizes
, num_frames
* sizeof(uint64_t));
346 int mk_addFrameData(mk_Writer
*w
, mk_Track
*track
, const void *data
, unsigned size
) {
347 if (!track
->in_frame
)
350 if (track
->frame
.data
== NULL
)
351 if ((track
->frame
.data
= mk_createContext(w
, NULL
, 0)) == NULL
)
354 md5_update(&w
->segment_md5
, (unsigned char *)data
, size
);
356 return mk_appendContextData(track
->frame
.data
, data
, size
);
359 int mk_writeSeek(mk_Writer
*w
, mk_Context
*c
, unsigned seek_id
, uint64_t seek_pos
) {
362 if ((s
= mk_createContext(w
, c
, 0x4dbb)) == NULL
) // Seek
364 CHECK(mk_writeUInt(s
, 0x53ab, seek_id
)); // SeekID
365 CHECK(mk_writeUInt(s
, 0x53ac, seek_pos
)); // SeekPosition
366 CHECK(mk_closeContext(s
, 0));
371 /* The offset of the SeekHead is returned in *pointer. */
372 int mk_writeSeekHead(mk_Writer
*w
, int64_t *pointer
) {
374 int64_t seekhead_ptr
;
376 if ((c
= mk_createContext(w
, w
->root
, 0x114d9b74)) == NULL
) // SeekHead
379 seekhead_ptr
= w
->f_pos
;
380 if (w
->seek_data
.seekhead
)
381 CHECK(mk_writeSeek(w
, c
, 0x114d9b74, w
->seek_data
.seekhead
));
382 if (w
->seek_data
.segmentinfo
)
383 CHECK(mk_writeSeek(w
, c
, 0x1549a966, w
->seek_data
.segmentinfo
));
384 if (w
->seek_data
.tracks
)
385 CHECK(mk_writeSeek(w
, c
, 0x1654ae6b, w
->seek_data
.tracks
));
386 if (w
->seek_data
.cues
)
387 CHECK(mk_writeSeek(w
, c
, 0x1c53bb6b, w
->seek_data
.cues
));
388 if (w
->seek_data
.attachments
)
389 CHECK(mk_writeSeek(w
, c
, 0x1941a469, w
->seek_data
.attachments
));
390 if (w
->seek_data
.chapters
)
391 CHECK(mk_writeSeek(w
, c
, 0x1043a770, w
->seek_data
.chapters
));
392 if (w
->seek_data
.tags
)
393 CHECK(mk_writeSeek(w
, c
, 0x1254c367, w
->seek_data
.tags
));
394 CHECK(mk_closeContext(c
, 0));
397 *pointer
= seekhead_ptr
;
402 int mk_close(mk_Writer
*w
) {
405 int64_t max_frame_tc
= w
->tracks_arr
[0]->max_frame_tc
;
406 uint64_t segment_size
= 0;
407 unsigned char c_size
[8];
408 unsigned char segment_uid
[16];
410 md5_finish(&w
->segment_md5
, segment_uid
);
412 for (i
= w
->num_tracks
- 1; i
>= 0; i
--)
414 tk
= w
->tracks_arr
[i
];
415 w
->tracks_arr
[i
] = NULL
;
417 if (mk_flushFrame(w
, tk
) < 0)
423 if (mk_closeCluster(w
) < 0)
426 w
->seek_data
.cues
= w
->f_pos
- w
->segment_ptr
;
427 if (mk_closeContext(w
->cues
, 0) < 0)
429 if (mk_flushContextData(w
->root
) < 0)
432 if (w
->vlc_compat
&& w
->cluster
.seekhead
) {
433 w
->seek_data
.seekhead
= w
->f_pos
- w
->segment_ptr
;
434 if (mk_closeContext(w
->cluster
.seekhead
, 0) < 0)
436 if (mk_flushContextData(w
->root
) < 0)
440 if (w
->chapters
!= NULL
)
443 if (mk_flushContextData(w
->root
) < 0)
445 if (mk_seekFile(w
, w
->segment_ptr
+ 0x100 + 3) < 0)
448 w
->seek_data
.chapters
= w
->f_pos
- w
->segment_ptr
;
450 if (mk_flushContextData(w
->root
) < 0)
453 if (mk_writeVoid(w
->root
, (0x800 - (w
->f_pos
- w
->segment_ptr
- 0x100 - 3))) < 0)
455 if (mk_flushContextData(w
->root
) < 0)
460 if (w
->wrote_header
) {
462 if (mk_seekFile(w
, w
->segment_ptr
) < 0)
466 if (mk_writeSeekHead(w
, &w
->seek_data
.seekhead
) < 0)
468 w
->seek_data
.seekhead
-= w
->segment_ptr
;
472 if (mk_flushContextData(w
->root
) < 0)
474 if (mk_writeVoid(w
->root
, (0x100 - (w
->f_pos
- w
->segment_ptr
))) < 0)
478 if (mk_flushContextData(w
->root
) < 0)
483 int i
= w
->seek_data
.segmentinfo
;
484 w
->seek_data
.segmentinfo
= 0;
485 w
->seek_data
.tracks
= 0;
486 w
->seek_data
.cues
= 0;
487 w
->seek_data
.chapters
= 0;
488 w
->seek_data
.attachments
= 0;
489 w
->seek_data
.tags
= 0;
490 if (mk_seekFile(w
, w
->segment_ptr
) < 0)
492 if (mk_writeSeekHead(w
, NULL
) < 0 ||
493 mk_flushContextData(w
->root
) < 0)
495 if (((i
+ w
->segment_ptr
) - w
->f_pos
- 2) > 1)
496 if (mk_writeVoid(w
->root
, (i
+ w
->segment_ptr
) - w
->f_pos
- 2) < 0 ||
497 mk_flushContextData(w
->root
) < 0)
501 if (mk_seekFile(w
, w
->duration_ptr
) < 0)
503 if (mk_writeFloatRaw(w
->root
, (float)((double)(max_frame_tc
+w
->def_duration
) / w
->timescale
)) < 0 ||
504 mk_flushContextData(w
->root
) < 0)
506 if (mk_seekFile(w
, w
->segment_ptr
- 8) < 0)
508 segment_size
= w
->f_eof
- w
->segment_ptr
;
509 for (i
= 7; i
> 0; --i
)
510 c_size
[i
] = segment_size
>> (8 * (7-i
));
512 if (mk_appendContextData(w
->root
, &c_size
, 8) < 0 ||
513 mk_flushContextData(w
->root
) < 0)
515 if (mk_seekFile(w
, w
->segmentuid_ptr
) < 0)
517 if (mk_writeBin(w
->root
, 0x73a4, segment_uid
, sizeof(segment_uid
)) < 0 ||
518 mk_flushContextData(w
->root
) < 0)
522 if (mk_closeContext(w
->root
, 0) < 0)
524 mk_destroyContexts(w
);