1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 x264 project
6 * Authors: Mike Matsnev
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
30 #define RESERVED_SEEKHEAD 0x100
31 /* 256 bytes should be enough room for our Seek entries. */
32 #define RESERVED_CHAPTERS 0x1000
33 /* 4096 bytes, hopefully enough for Chapters. */
35 int mk_seekFile(mk_Writer
*w
, uint64_t pos
)
37 if (fseek(w
->fp
, pos
, SEEK_SET
))
48 char *mk_laceXiph(uint64_t *sizes
, uint8_t num_frames
,
49 uint64_t *output_size
)
53 uint64_t alloc_size
= num_frames
* 6; /* Complete guess. It gets realloc'd
54 * below if we need more space, though.
56 char *laced
= calloc(alloc_size
, sizeof(*laced
));
60 laced
[offset
++] = num_frames
;
61 for (i
= 0; i
< num_frames
; i
++) {
62 for (j
= sizes
[i
]; j
>= 255; j
-= 255) {
63 laced
[offset
++] = 255;
64 if (offset
+ 1 >= alloc_size
) {
65 int avg_sz
= offset
/ (i
- 1); /* Compute approximate average bytes/frame */
66 alloc_size
+= avg_sz
* (num_frames
- i
);
67 /* Sum average so far and number of frames left
70 if ((laced
= realloc(laced
, alloc_size
)) == NULL
)
77 if (output_size
!= NULL
)
78 *output_size
= offset
;
83 mk_Writer
*mk_createWriter(const char *filename
, int64_t timescale
,
86 mk_Writer
*w
= calloc(1, sizeof(*w
));
92 w
->root
= mk_createContext(w
, NULL
, 0);
93 if (w
->root
== NULL
) {
99 if ((w
->cues
= mk_createContext(w
, w
->root
, MATROSKA_ID_CUES
)) == NULL
)
101 mk_destroyContexts(w
);
106 /* Clusters SeekHead */
107 if ((w
->cluster
.seekhead
= mk_createContext(w
, w
->root
, MATROSKA_ID_SEEKHEAD
)) == NULL
)
109 mk_destroyContexts(w
);
114 w
->fp
= fopen(filename
, "wb");
116 mk_destroyContexts(w
);
121 gettimeofday( &tv
, NULL
);
122 srandom(tv
.tv_sec
^ tv
.tv_usec
);
124 w
->timescale
= timescale
;
125 w
->vlc_compat
= vlc_compat
;
130 int mk_writeHeader(mk_Writer
*w
, const char *writingApp
)
138 md5_starts(&w
->segment_md5
); /* Initalize MD5 */
140 CHECK(mk_writeEbmlHeader(w
, "matroska", MATROSKA_VERSION
, MATROSKA_VERSION
));
143 if ((c
= mk_createContext(w
, w
->root
, MATROSKA_ID_SEGMENT
)) == NULL
)
145 CHECK(mk_flushContextID(c
));
146 w
->segment_ptr
= c
->d_cur
;
147 CHECK(mk_closeContext(c
, &w
->segment_ptr
));
150 CHECK(mk_writeVoid(w
->root
, RESERVED_SEEKHEAD
)); /* Reserved space for SeekHead */
151 CHECK(mk_writeVoid(w
->root
, RESERVED_CHAPTERS
)); /* Reserved space for Chapters */
153 w
->seek_data
.seekhead
= 0x80000000;
154 CHECK(mk_writeSeekHead(w
, &w
->seekhead_ptr
));
155 w
->seek_data
.seekhead
= 0;
158 if ((c
= mk_createContext(w
, w
->root
, MATROSKA_ID_INFO
)) == NULL
) /* SegmentInfo */
160 w
->seek_data
.segmentinfo
= w
->root
->d_cur
- w
->segment_ptr
;
161 CHECK(mk_writeVoid(c
, 16)); /* Reserve space for a SegmentUID, to be written it later. */
162 CHECK(mk_writeStr(c
, MATROSKA_ID_MUXINGAPP
, PACKAGE_STRING
)); /* MuxingApp */
163 CHECK(mk_writeStr(c
, MATROSKA_ID_WRITINGAPP
, writingApp
)); /* WritingApp */
164 CHECK(mk_writeUInt(c
, MATROSKA_ID_TIMECODESCALE
, w
->timescale
)); /* TimecodeScale */
165 CHECK(mk_writeFloat(c
, MATROSKA_ID_DURATION
, 0)); /* Duration */
166 w
->duration_ptr
= c
->d_cur
- 4;
167 CHECK(mk_closeContext(c
, &offset
));
168 w
->duration_ptr
+= offset
;
169 w
->segmentuid_ptr
= offset
;
171 w
->seek_data
.tracks
= w
->root
->d_cur
- w
->segment_ptr
;
174 CHECK(mk_closeContext(w
->tracks
, 0));
176 CHECK(mk_flushContextData(w
->root
));
179 w
->def_duration
= w
->tracks_arr
[0]->default_duration
;
183 static int mk_closeCluster(mk_Writer
*w
)
185 if (w
->cluster
.context
== NULL
)
188 CHECK(mk_closeContext(w
->cluster
.context
, 0));
189 w
->cluster
.context
= NULL
;
190 CHECK(mk_flushContextData(w
->root
));
195 int mk_flushFrame(mk_Writer
*w
, mk_Track
*track
)
198 int64_t delta
, ref
= 0;
199 unsigned fsize
, bgsize
;
200 uint8_t flags
, c_delta
[2];
205 if (!track
->in_frame
)
208 delta
= track
->frame
.timecode
/ w
->timescale
- w
->cluster
.tc_scaled
;
209 /* If switch rapidly back-and-forth between tracks with drastically different timecodes
210 * this causes a new cluster to be written each time a switch is made. This causes
211 * unnecessary overhead.
214 if (delta
> 20000ll || delta
< -20000ll)
215 CHECK(mk_closeCluster(w
));
217 if (w
->cluster
.context
== NULL
) {
218 w
->cluster
.tc_scaled
= track
->frame
.timecode
/ w
->timescale
;
220 w
->cluster
.context
= mk_createContext(w
, w
->root
, MATROSKA_ID_CLUSTER
);
221 if (w
->cluster
.context
== NULL
)
224 w
->cluster
.pointer
= w
->f_pos
- w
->segment_ptr
;
226 /* Cluster SeekEntry */
227 CHECK(mk_writeSeek(w
, w
->cluster
.seekhead
, MATROSKA_ID_CLUSTER
,
228 w
->cluster
.pointer
));
230 /* Cluster Timecode */
231 CHECK(mk_writeUInt(w
->cluster
.context
, MATROSKA_ID_CLUSTERTIMECODE
, w
->cluster
.tc_scaled
));
234 w
->cluster
.block_count
= 0;
237 /* Calculate the encoded lacing sizes. */
238 switch (track
->frame
.lacing
) {
240 laced
= mk_laceXiph(track
->frame
.lacing_sizes
,
241 track
->frame
.lacing_num_frames
, &length
);
246 /* Add one below for the frame count. */
247 length
+= mk_ebmlSizeSize(track
->frame
.lacing_sizes
[0]) + 1;
248 for (i
= 1; i
< track
->frame
.lacing_num_frames
; i
++) {
249 u_size
= llabs(track
->frame
.lacing_sizes
[i
] -
250 track
->frame
.lacing_sizes
[i
- 1]);
251 /* Shift by one so we get the right size for a signed number. */
252 length
+= mk_ebmlSizeSize((u_size
) << 1);
256 case MK_LACING_FIXED
:
258 laced
= calloc(1, sizeof(*laced
));
259 laced
[0] = track
->frame
.lacing_num_frames
;
267 fsize
= track
->frame
.data
? track
->frame
.data
->d_cur
: 0;
268 bgsize
= fsize
+ 4 + mk_ebmlSizeSize(fsize
+ 4 + length
) + 1 + length
;
269 if (!track
->frame
.keyframe
) {
270 ref
= track
->prev_frame_tc_scaled
- w
->cluster
.tc_scaled
- delta
;
271 bgsize
+= 1 + 1 + mk_ebmlSIntSize(ref
);
274 CHECK(mk_writeID(w
->cluster
.context
, MATROSKA_ID_BLOCKGROUP
)); /* BlockGroup */
275 CHECK(mk_writeSize(w
->cluster
.context
, bgsize
));
276 CHECK(mk_writeID(w
->cluster
.context
, MATROSKA_ID_BLOCK
)); /* Block */
277 CHECK(mk_writeSize(w
->cluster
.context
, fsize
+ 4 + length
)); /* BlockSize */
278 CHECK(mk_writeSize(w
->cluster
.context
, track
->track_id
)); /* track number */
280 w
->cluster
.block_count
++;
282 c_delta
[0] = delta
>> 8;
284 /* Timecode relative to Cluster. */
285 CHECK(mk_appendContextData(w
->cluster
.context
, c_delta
, 2));
287 /* flags = ( track->frame.keyframe << 8 ) | track->frame.lacing; */
288 flags
= track
->frame
.lacing
<< 1; /* Flags: Bit 5-6 describe what type of lacing to use. */
289 CHECK(mk_appendContextData(w
->cluster
.context
, &flags
, 1));
290 if (track
->frame
.lacing
) {
291 if (track
->frame
.lacing
== MK_LACING_EBML
) {
292 /* Number of frames in lace - 1 */
293 CHECK(mk_appendContextData(w
->cluster
.context
, &track
->frame
.lacing_num_frames
, 1));
294 /* Size of 1st frame. */
295 CHECK(mk_writeSize(w
->cluster
.context
, track
->frame
.lacing_sizes
[0]));
296 for (i
= 1; i
< track
->frame
.lacing_num_frames
; i
++) {
297 /* Size difference between previous size and this size. */
298 CHECK(mk_writeSSize(w
->cluster
.context
, track
->frame
.lacing_sizes
[i
] - track
->frame
.lacing_sizes
[i
- 1]));
300 } else if (length
> 0 && laced
!= NULL
) {
301 CHECK(mk_appendContextData(w
->cluster
.context
, laced
, length
));
307 if (track
->frame
.data
) {
308 CHECK(mk_appendContextData(w
->cluster
.context
, track
->frame
.data
->data
,
309 track
->frame
.data
->d_cur
));
310 track
->frame
.data
->d_cur
= 0;
312 if (!track
->frame
.keyframe
)
314 CHECK(mk_writeSInt(w
->cluster
.context
, MATROSKA_ID_REFERENCEBLOCK
, ref
));
316 /* This may get a little out of hand, but it seems sane enough for now. */
317 if (track
->frame
.keyframe
&& (track
->track_type
== MK_TRACK_VIDEO
)) {
318 /* if (track->frame.keyframe && (track->track_type & MK_TRACK_VIDEO) && ((track->prev_cue_pos + 3*CLSIZE) <= w->f_pos || track->frame.timecode == 0)) { */
320 if ((c
= mk_createContext(w
, w
->cues
, MATROSKA_ID_CUEPOINT
)) == NULL
)
323 CHECK(mk_writeUInt(c
, MATROSKA_ID_CUETIME
, (track
->frame
.timecode
/ w
->timescale
)));
325 /* CueTrackPositions */
326 if ((tp
= mk_createContext(w
, c
, MATROSKA_ID_CUETRACKPOSITIONS
)) == NULL
)
329 CHECK(mk_writeUInt(tp
, MATROSKA_ID_CUETRACK
, track
->track_id
));
330 /* CueClusterPosition */
331 CHECK(mk_writeUInt(tp
, MATROSKA_ID_CUECLUSTERPOSITION
, w
->cluster
.pointer
));
333 /* CHECK(mk_writeUInt(c, MATROSKA_ID_CUEBLOCKNUMBER, w->cluster.block_count)); */
334 CHECK(mk_closeContext(tp
, 0));
335 CHECK(mk_closeContext(c
, 0));
336 track
->prev_cue_pos
= w
->f_pos
;
340 track
->prev_frame_tc_scaled
= w
->cluster
.tc_scaled
+ delta
;
342 /* Write a new Cluster ~ every 5MB */
343 if (w
->cluster
.context
->d_cur
> 5 * CLSIZE
)
344 CHECK(mk_closeCluster(w
));
349 int mk_startFrame(mk_Writer
*w
, mk_Track
*track
)
351 if (mk_flushFrame(w
, track
) < 0)
355 track
->frame
.keyframe
= 0;
356 track
->frame
.lacing
= MK_LACING_NONE
;
357 track
->frame
.lacing_num_frames
= 0;
358 track
->frame
.lacing_sizes
= NULL
;
363 int mk_setFrameFlags(mk_Writer
*w
, mk_Track
*track
, int64_t timestamp
,
366 if (!track
->in_frame
)
369 track
->frame
.timecode
= timestamp
;
370 track
->frame
.keyframe
= keyframe
!= 0;
372 if (track
->max_frame_tc
< timestamp
)
373 track
->max_frame_tc
= timestamp
;
378 int mk_setFrameLacing(mk_Writer
*w
, mk_Track
*track
,
379 mk_LacingType lacing
, uint8_t num_frames
,
382 if (!track
->in_frame
)
384 track
->frame
.lacing_sizes
= calloc(num_frames
, sizeof(*sizes
));
386 track
->frame
.lacing
= lacing
;
387 track
->frame
.lacing_num_frames
= num_frames
;
388 memcpy(track
->frame
.lacing_sizes
, sizes
,
389 num_frames
* sizeof(*sizes
));
394 int mk_addFrameData(mk_Writer
*w
, mk_Track
*track
, const void *data
,
397 if (!track
->in_frame
)
400 if (track
->frame
.data
== NULL
) {
401 if ((track
->frame
.data
= mk_createContext(w
, NULL
, 0)) == NULL
)
405 md5_update(&w
->segment_md5
, (unsigned char *) data
, size
);
407 return mk_appendContextData(track
->frame
.data
, data
, size
);
410 int mk_writeSeek(mk_Writer
*w
, mk_Context
*c
, unsigned seek_id
,
415 if ((s
= mk_createContext(w
, c
, MATROSKA_ID_SEEKENTRY
)) == NULL
) /* Seek */
417 CHECK(mk_writeUInt(s
, MATROSKA_ID_SEEKID
, seek_id
)); /* SeekID */
418 CHECK(mk_writeUInt(s
, MATROSKA_ID_SEEKPOSITION
, seek_pos
)); /* SeekPosition */
419 CHECK(mk_closeContext(s
, 0));
424 /* The offset of the SeekHead is returned in pointer. */
425 int mk_writeSeekHead(mk_Writer
*w
, int64_t *pointer
)
428 int64_t seekhead_ptr
;
431 if ((c
= mk_createContext(w
, w
->root
, MATROSKA_ID_SEEKHEAD
)) == NULL
)
434 seekhead_ptr
= w
->f_pos
;
435 if (w
->seek_data
.seekhead
)
436 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_SEEKHEAD
, w
->seek_data
.seekhead
));
437 if (w
->seek_data
.segmentinfo
)
438 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_INFO
, w
->seek_data
.segmentinfo
));
439 if (w
->seek_data
.tracks
)
440 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_TRACKS
, w
->seek_data
.tracks
));
441 if (w
->seek_data
.cues
)
442 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_CUES
, w
->seek_data
.cues
));
443 if (w
->seek_data
.attachments
)
444 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_ATTACHMENTS
, w
->seek_data
.attachments
));
445 if (w
->seek_data
.chapters
)
446 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_CHAPTERS
, w
->seek_data
.chapters
));
447 if (w
->seek_data
.tags
)
448 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_TAGS
, w
->seek_data
.tags
));
449 CHECK(mk_closeContext(c
, 0));
452 *pointer
= seekhead_ptr
;
457 int mk_close(mk_Writer
*w
)
461 int64_t max_frame_tc
= w
->tracks_arr
[0]->max_frame_tc
;
462 uint64_t segment_size
= 0;
463 unsigned char c_size
[8];
464 unsigned char segment_uid
[16];
466 md5_finish(&w
->segment_md5
, segment_uid
);
468 for (i
= w
->num_tracks
- 1; i
>= 0; i
--) {
469 tk
= w
->tracks_arr
[i
];
470 w
->tracks_arr
[i
] = NULL
;
472 if (mk_flushFrame(w
, tk
) < 0)
478 if (mk_closeCluster(w
) < 0)
481 w
->seek_data
.cues
= w
->f_pos
- w
->segment_ptr
;
482 if (mk_closeContext(w
->cues
, 0) < 0)
484 if (mk_flushContextData(w
->root
) < 0)
487 if (w
->cluster
.seekhead
) {
488 w
->seek_data
.seekhead
= w
->f_pos
- w
->segment_ptr
;
489 if (mk_closeContext(w
->cluster
.seekhead
, 0) < 0)
491 if (mk_flushContextData(w
->root
) < 0)
495 if (w
->attachments
!= NULL
) {
496 w
->seek_data
.attachments
= w
->f_pos
- w
->segment_ptr
;
497 mk_writeAttachments(w
);
498 if (mk_flushContextData(w
->root
) < 0)
502 if (w
->tags
!= NULL
) {
503 w
->seek_data
.tags
= w
->f_pos
- w
->segment_ptr
;
505 if (mk_flushContextData(w
->root
) < 0)
509 if (w
->chapters
!= NULL
) {
511 if (mk_flushContextData(w
->root
) < 0)
513 if (mk_seekFile(w
, w
->segment_ptr
+ RESERVED_SEEKHEAD
+ 3) < 0)
516 w
->seek_data
.chapters
= w
->f_pos
- w
->segment_ptr
;
518 if (mk_flushContextData(w
->root
) < 0)
521 if (mk_writeVoid(w
->root
, (RESERVED_CHAPTERS
- (w
->f_pos
- w
->segment_ptr
- RESERVED_SEEKHEAD
- 3))) < 0)
523 if (mk_flushContextData(w
->root
) < 0)
528 if (w
->wrote_header
) {
530 if (mk_seekFile(w
, w
->segment_ptr
) < 0)
534 if (mk_writeSeekHead(w
, &w
->seek_data
.seekhead
) < 0)
536 w
->seek_data
.seekhead
-= w
->segment_ptr
;
539 if (mk_flushContextData(w
->root
) < 0)
541 if (mk_writeVoid(w
->root
, (RESERVED_SEEKHEAD
- (w
->f_pos
- w
->segment_ptr
))) < 0)
545 if (mk_flushContextData(w
->root
) < 0)
548 if (!w
->vlc_compat
) {
549 int i
= w
->seek_data
.segmentinfo
;
550 w
->seek_data
.segmentinfo
= 0;
551 w
->seek_data
.tracks
= 0;
552 w
->seek_data
.cues
= 0;
553 w
->seek_data
.chapters
= 0;
554 w
->seek_data
.attachments
= 0;
555 w
->seek_data
.tags
= 0;
556 if (mk_seekFile(w
, w
->segment_ptr
) < 0)
558 if (mk_writeSeekHead(w
, NULL
) < 0 || mk_flushContextData(w
->root
) < 0)
560 // The conditional below is easier to understand, but incorrect
561 // because f_pos is unsigned and causes the lhs to be evaluated
562 // as an unsigned quantity.
563 // if (((i + w->segment_ptr) - w->f_pos - 2) > 1)
564 if ((i
+ w
->segment_ptr
) > (w
->f_pos
+ 3))
565 if (mk_writeVoid(w
->root
, (i
+ w
->segment_ptr
) - w
->f_pos
- 2) < 0
566 || mk_flushContextData(w
->root
) < 0)
570 if (mk_seekFile(w
, w
->duration_ptr
) < 0)
572 if (mk_writeFloatRaw(w
->root
,
573 (float) ((double) (max_frame_tc
+ w
->def_duration
) /
575 || mk_flushContextData(w
->root
) < 0)
577 if (mk_seekFile(w
, w
->segment_ptr
- 8) < 0)
579 segment_size
= w
->f_eof
- w
->segment_ptr
;
580 for (i
= 7; i
> 0; --i
)
581 c_size
[i
] = segment_size
>> (8 * (7 - i
));
583 if (mk_appendContextData(w
->root
, &c_size
, 8) < 0 ||
584 mk_flushContextData(w
->root
) < 0)
586 if (mk_seekFile(w
, w
->segmentuid_ptr
) < 0)
588 if (mk_writeBin(w
->root
, MATROSKA_ID_SEGMENTUID
, segment_uid
,
589 sizeof(segment_uid
)) < 0 ||
590 mk_flushContextData(w
->root
) < 0)
594 if (mk_closeContext(w
->root
, 0) < 0)
596 mk_destroyContexts(w
);