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 *****************************************************************************/
28 #define RESERVED_SEEKHEAD 0x100
29 /* 256 bytes should be enough room for our Seek entries. */
30 #define RESERVED_CHAPTERS 0x800
31 /* 2048 bytes, hopefully enough for Chapters. */
33 int mk_seekFile(mk_Writer
*w
, uint64_t pos
)
35 if (fseek(w
->fp
, pos
, SEEK_SET
))
46 char *mk_laceXiph(uint64_t *sizes
, uint8_t num_frames
,
47 uint64_t *output_size
)
51 uint64_t alloc_size
= num_frames
* 6; /* Complete guess. It gets realloc'd
52 * below if we need more space, though.
54 char *laced
= calloc(alloc_size
, sizeof(*laced
));
58 laced
[offset
++] = num_frames
;
59 for (i
= 0; i
< num_frames
; i
++) {
60 for (j
= sizes
[i
]; j
>= 255; j
-= 255) {
61 laced
[offset
++] = 255;
62 if (offset
+ 1 >= alloc_size
) {
63 int avg_sz
= offset
/ (i
- 1); /* Compute approximate average bytes/frame */
64 alloc_size
+= avg_sz
* (num_frames
- i
);
65 /* Sum average so far and number of frames left
68 if ((laced
= realloc(laced
, alloc_size
)) == NULL
)
75 if (output_size
!= NULL
)
76 *output_size
= offset
;
81 mk_Writer
*mk_createWriter(const char *filename
, int64_t timescale
,
84 mk_Writer
*w
= calloc(1, sizeof(*w
));
88 w
->root
= mk_createContext(w
, NULL
, 0);
89 if (w
->root
== NULL
) {
95 if ((w
->cues
= mk_createContext(w
, w
->root
, MATROSKA_ID_CUES
)) == NULL
)
97 mk_destroyContexts(w
);
104 if ((w
->cluster
.seekhead
= mk_createContext(w
, w
->root
, MATROSKA_ID_SEEKHEAD
)) == NULL
)
106 mk_destroyContexts(w
);
112 w
->fp
= fopen(filename
, "wb");
114 mk_destroyContexts(w
);
119 w
->timescale
= timescale
;
120 w
->vlc_compat
= vlc_compat
;
125 int mk_writeHeader(mk_Writer
*w
, const char *writingApp
)
133 md5_starts(&w
->segment_md5
); /* Initalize MD5 */
135 CHECK(mk_writeEbmlHeader(w
, "matroska", MATROSKA_VERSION
, MATROSKA_VERSION
));
138 if ((c
= mk_createContext(w
, w
->root
, MATROSKA_ID_SEGMENT
)) == NULL
)
140 CHECK(mk_flushContextID(c
));
141 w
->segment_ptr
= c
->d_cur
;
142 CHECK(mk_closeContext(c
, &w
->segment_ptr
));
145 CHECK(mk_writeVoid(w
->root
, RESERVED_SEEKHEAD
)); /* Reserved space for SeekHead */
146 CHECK(mk_writeVoid(w
->root
, RESERVED_CHAPTERS
)); /* Reserved space for Chapters */
148 w
->seek_data
.seekhead
= 0x80000000;
149 CHECK(mk_writeSeekHead(w
, &w
->seekhead_ptr
));
150 w
->seek_data
.seekhead
= 0;
153 if ((c
= mk_createContext(w
, w
->root
, MATROSKA_ID_INFO
)) == NULL
) /* SegmentInfo */
155 w
->seek_data
.segmentinfo
= w
->root
->d_cur
- w
->segment_ptr
;
156 CHECK(mk_writeVoid(c
, 16)); /* Reserve space for a SegmentUID, to be written it later. */
157 CHECK(mk_writeStr(c
, MATROSKA_ID_MUXINGAPP
, PACKAGE_STRING
)); /* MuxingApp */
158 CHECK(mk_writeStr(c
, MATROSKA_ID_WRITINGAPP
, writingApp
)); /* WritingApp */
159 CHECK(mk_writeUInt(c
, MATROSKA_ID_TIMECODESCALE
, w
->timescale
)); /* TimecodeScale */
160 CHECK(mk_writeFloat(c
, MATROSKA_ID_DURATION
, 0)); /* Duration */
161 w
->duration_ptr
= c
->d_cur
- 4;
162 CHECK(mk_closeContext(c
, &offset
));
163 w
->duration_ptr
+= offset
;
164 w
->segmentuid_ptr
= offset
;
166 w
->seek_data
.tracks
= w
->root
->d_cur
- w
->segment_ptr
;
169 CHECK(mk_closeContext(w
->tracks
, 0));
171 CHECK(mk_flushContextData(w
->root
));
174 w
->def_duration
= w
->tracks_arr
[0]->default_duration
;
178 static int mk_closeCluster(mk_Writer
*w
)
180 if (w
->cluster
.context
== NULL
)
183 CHECK(mk_closeContext(w
->cluster
.context
, 0));
184 w
->cluster
.context
= NULL
;
185 CHECK(mk_flushContextData(w
->root
));
190 int mk_flushFrame(mk_Writer
*w
, mk_Track
*track
)
193 int64_t delta
, ref
= 0;
194 unsigned fsize
, bgsize
;
195 uint8_t flags
, c_delta
[2];
200 if (!track
->in_frame
)
203 delta
= track
->frame
.timecode
/ w
->timescale
- w
->cluster
.tc_scaled
;
204 if (delta
> 2000ll || delta
< -2000ll)
205 CHECK(mk_closeCluster(w
));
207 if (w
->cluster
.context
== NULL
) {
208 w
->cluster
.tc_scaled
= track
->frame
.timecode
/ w
->timescale
;
210 w
->cluster
.context
= mk_createContext(w
, w
->root
, MATROSKA_ID_CLUSTER
);
211 if (w
->cluster
.context
== NULL
)
214 w
->cluster
.pointer
= w
->f_pos
- w
->segment_ptr
;
217 /* Cluster SeekEntry */
218 CHECK(mk_writeSeek(w
, w
->cluster
.seekhead
, MATROSKA_ID_CLUSTER
,
219 w
->cluster
.pointer
));
222 /* Cluster Timecode */
223 CHECK(mk_writeUInt(w
->cluster
.context
, MATROSKA_ID_CLUSTERTIMECODE
, w
->cluster
.tc_scaled
));
226 w
->cluster
.block_count
= 0;
229 /* Calculate the encoded lacing sizes. */
230 switch (track
->frame
.lacing
) {
232 laced
= mk_laceXiph(track
->frame
.lacing_sizes
,
233 track
->frame
.lacing_num_frames
, &length
);
238 /* Add one below for the frame count. */
239 length
+= mk_ebmlSizeSize(track
->frame
.lacing_sizes
[0]) + 1;
240 for (i
= 1; i
< track
->frame
.lacing_num_frames
; i
++) {
241 u_size
= llabs(track
->frame
.lacing_sizes
[i
] -
242 track
->frame
.lacing_sizes
[i
- 1]);
243 /* Shift by one so we get the right size for a signed number. */
244 length
+= mk_ebmlSizeSize((u_size
) << 1);
248 case MK_LACING_FIXED
:
250 laced
= calloc(1, sizeof(*laced
));
251 laced
[0] = track
->frame
.lacing_num_frames
;
259 fsize
= track
->frame
.data
? track
->frame
.data
->d_cur
: 0;
260 bgsize
= fsize
+ 4 + mk_ebmlSizeSize(fsize
+ 4 + length
) + 1 + length
;
261 if (!track
->frame
.keyframe
) {
262 ref
= track
->prev_frame_tc_scaled
- w
->cluster
.tc_scaled
- delta
;
263 bgsize
+= 1 + 1 + mk_ebmlSIntSize(ref
);
266 CHECK(mk_writeID(w
->cluster
.context
, MATROSKA_ID_BLOCKGROUP
)); /* BlockGroup */
267 CHECK(mk_writeSize(w
->cluster
.context
, bgsize
));
268 CHECK(mk_writeID(w
->cluster
.context
, MATROSKA_ID_BLOCK
)); /* Block */
269 CHECK(mk_writeSize(w
->cluster
.context
, fsize
+ 4 + length
)); /* BlockSize */
270 CHECK(mk_writeSize(w
->cluster
.context
, track
->track_id
)); /* track number */
272 w
->cluster
.block_count
++;
274 c_delta
[0] = delta
>> 8;
276 /* Timecode relative to Cluster. */
277 CHECK(mk_appendContextData(w
->cluster
.context
, c_delta
, 2));
279 /* flags = ( track->frame.keyframe << 8 ) | track->frame.lacing; */
280 flags
= track
->frame
.lacing
<< 1; /* Flags: Bit 5-6 describe what type of lacing to use. */
281 CHECK(mk_appendContextData(w
->cluster
.context
, &flags
, 1));
282 if (track
->frame
.lacing
) {
283 if (track
->frame
.lacing
== MK_LACING_EBML
) {
284 /* Number of frames in lace - 1 */
285 CHECK(mk_appendContextData(w
->cluster
.context
, &track
->frame
.lacing_num_frames
, 1));
286 /* Size of 1st frame. */
287 CHECK(mk_writeSize(w
->cluster
.context
, track
->frame
.lacing_sizes
[0]));
288 for (i
= 1; i
< track
->frame
.lacing_num_frames
; i
++) {
289 /* Size difference between previous size and this size. */
290 CHECK(mk_writeSSize(w
->cluster
.context
, track
->frame
.lacing_sizes
[i
] - track
->frame
.lacing_sizes
[i
- 1]));
292 } else if (length
> 0 && laced
!= NULL
) {
293 CHECK(mk_appendContextData(w
->cluster
.context
, laced
, length
));
299 if (track
->frame
.data
) {
300 CHECK(mk_appendContextData(w
->cluster
.context
, track
->frame
.data
->data
,
301 track
->frame
.data
->d_cur
));
302 track
->frame
.data
->d_cur
= 0;
304 if (!track
->frame
.keyframe
)
306 CHECK(mk_writeSInt(w
->cluster
.context
, MATROSKA_ID_REFERENCEBLOCK
, ref
));
308 /* This may get a little out of hand, but it seems sane enough for now. */
309 if (track
->frame
.keyframe
&& (track
->track_type
== MK_TRACK_VIDEO
)) {
310 /* if (track->frame.keyframe && (track->track_type & MK_TRACK_VIDEO) && ((track->prev_cue_pos + 3*CLSIZE) <= w->f_pos || track->frame.timecode == 0)) { */
312 if ((c
= mk_createContext(w
, w
->cues
, MATROSKA_ID_CUEPOINT
)) == NULL
)
315 CHECK(mk_writeUInt(c
, MATROSKA_ID_CUETIME
, (track
->frame
.timecode
/ w
->timescale
)));
317 /* CueTrackPositions */
318 if ((tp
= mk_createContext(w
, c
, MATROSKA_ID_CUETRACKPOSITIONS
)) == NULL
)
321 CHECK(mk_writeUInt(tp
, MATROSKA_ID_CUETRACK
, track
->track_id
));
322 /* CueClusterPosition */
323 CHECK(mk_writeUInt(tp
, MATROSKA_ID_CUECLUSTERPOSITION
, w
->cluster
.pointer
));
325 /* CHECK(mk_writeUInt(c, MATROSKA_ID_CUEBLOCKNUMBER, w->cluster.block_count)); */
326 CHECK(mk_closeContext(tp
, 0));
327 CHECK(mk_closeContext(c
, 0));
328 track
->prev_cue_pos
= w
->f_pos
;
332 track
->prev_frame_tc_scaled
= w
->cluster
.tc_scaled
+ delta
;
334 if (w
->cluster
.context
->d_cur
> CLSIZE
)
335 CHECK(mk_closeCluster(w
));
340 int mk_startFrame(mk_Writer
*w
, mk_Track
*track
)
342 if (mk_flushFrame(w
, track
) < 0)
346 track
->frame
.keyframe
= 0;
347 track
->frame
.lacing
= MK_LACING_NONE
;
348 track
->frame
.lacing_num_frames
= 0;
349 track
->frame
.lacing_sizes
= NULL
;
354 int mk_setFrameFlags(mk_Writer
*w
, mk_Track
*track
, int64_t timestamp
,
357 if (!track
->in_frame
)
360 track
->frame
.timecode
= timestamp
;
361 track
->frame
.keyframe
= keyframe
!= 0;
363 if (track
->max_frame_tc
< timestamp
)
364 track
->max_frame_tc
= timestamp
;
369 int mk_setFrameLacing(mk_Writer
*w
, mk_Track
*track
,
370 mk_LacingType lacing
, uint8_t num_frames
,
373 if (!track
->in_frame
)
375 track
->frame
.lacing_sizes
= calloc(num_frames
, sizeof(*sizes
));
377 track
->frame
.lacing
= lacing
;
378 track
->frame
.lacing_num_frames
= num_frames
;
379 memcpy(track
->frame
.lacing_sizes
, sizes
,
380 num_frames
* sizeof(*sizes
));
385 int mk_addFrameData(mk_Writer
*w
, mk_Track
*track
, const void *data
,
388 if (!track
->in_frame
)
391 if (track
->frame
.data
== NULL
) {
392 if ((track
->frame
.data
= mk_createContext(w
, NULL
, 0)) == NULL
)
396 md5_update(&w
->segment_md5
, (unsigned char *) data
, size
);
398 return mk_appendContextData(track
->frame
.data
, data
, size
);
401 int mk_writeSeek(mk_Writer
*w
, mk_Context
*c
, unsigned seek_id
,
406 if ((s
= mk_createContext(w
, c
, MATROSKA_ID_SEEKENTRY
)) == NULL
) /* Seek */
408 CHECK(mk_writeUInt(s
, MATROSKA_ID_SEEKID
, seek_id
)); /* SeekID */
409 CHECK(mk_writeUInt(s
, MATROSKA_ID_SEEKPOSITION
, seek_pos
)); /* SeekPosition */
410 CHECK(mk_closeContext(s
, 0));
415 /* The offset of the SeekHead is returned in pointer. */
416 int mk_writeSeekHead(mk_Writer
*w
, int64_t *pointer
)
419 int64_t seekhead_ptr
;
422 if ((c
= mk_createContext(w
, w
->root
, MATROSKA_ID_SEEKHEAD
)) == NULL
)
425 seekhead_ptr
= w
->f_pos
;
426 if (w
->seek_data
.seekhead
)
427 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_SEEKHEAD
, w
->seek_data
.seekhead
));
428 if (w
->seek_data
.segmentinfo
)
429 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_INFO
, w
->seek_data
.segmentinfo
));
430 if (w
->seek_data
.tracks
)
431 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_TRACKS
, w
->seek_data
.tracks
));
432 if (w
->seek_data
.cues
)
433 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_CUES
, w
->seek_data
.cues
));
434 if (w
->seek_data
.attachments
)
435 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_ATTACHMENTS
, w
->seek_data
.attachments
));
436 if (w
->seek_data
.chapters
)
437 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_CHAPTERS
, w
->seek_data
.chapters
));
438 if (w
->seek_data
.tags
)
439 CHECK(mk_writeSeek(w
, c
, MATROSKA_ID_TAGS
, w
->seek_data
.tags
));
440 CHECK(mk_closeContext(c
, 0));
443 *pointer
= seekhead_ptr
;
448 int mk_close(mk_Writer
*w
)
452 int64_t max_frame_tc
= w
->tracks_arr
[0]->max_frame_tc
;
453 uint64_t segment_size
= 0;
454 unsigned char c_size
[8];
455 unsigned char segment_uid
[16];
457 md5_finish(&w
->segment_md5
, segment_uid
);
459 for (i
= w
->num_tracks
- 1; i
>= 0; i
--) {
460 tk
= w
->tracks_arr
[i
];
461 w
->tracks_arr
[i
] = NULL
;
463 if (mk_flushFrame(w
, tk
) < 0)
469 if (mk_closeCluster(w
) < 0)
472 w
->seek_data
.cues
= w
->f_pos
- w
->segment_ptr
;
473 if (mk_closeContext(w
->cues
, 0) < 0)
475 if (mk_flushContextData(w
->root
) < 0)
478 if (w
->vlc_compat
&& w
->cluster
.seekhead
) {
479 w
->seek_data
.seekhead
= w
->f_pos
- w
->segment_ptr
;
480 if (mk_closeContext(w
->cluster
.seekhead
, 0) < 0)
482 if (mk_flushContextData(w
->root
) < 0)
486 if (w
->chapters
!= NULL
) {
488 if (mk_flushContextData(w
->root
) < 0)
490 if (mk_seekFile(w
, w
->segment_ptr
+ 0x100 + 3) < 0)
493 w
->seek_data
.chapters
= w
->f_pos
- w
->segment_ptr
;
495 if (mk_flushContextData(w
->root
) < 0)
498 if (mk_writeVoid(w
->root
, (0x800 - (w
->f_pos
- w
->segment_ptr
- 0x100 - 3))) < 0)
500 if (mk_flushContextData(w
->root
) < 0)
505 if (w
->wrote_header
) {
507 if (mk_seekFile(w
, w
->segment_ptr
) < 0)
511 if (mk_writeSeekHead(w
, &w
->seek_data
.seekhead
) < 0)
513 w
->seek_data
.seekhead
-= w
->segment_ptr
;
516 if (mk_flushContextData(w
->root
) < 0)
518 if (mk_writeVoid(w
->root
, (0x100 - (w
->f_pos
- w
->segment_ptr
))) < 0)
522 if (mk_flushContextData(w
->root
) < 0)
525 if (!w
->vlc_compat
) {
526 int i
= w
->seek_data
.segmentinfo
;
527 w
->seek_data
.segmentinfo
= 0;
528 w
->seek_data
.tracks
= 0;
529 w
->seek_data
.cues
= 0;
530 w
->seek_data
.chapters
= 0;
531 w
->seek_data
.attachments
= 0;
532 w
->seek_data
.tags
= 0;
533 if (mk_seekFile(w
, w
->segment_ptr
) < 0)
535 if (mk_writeSeekHead(w
, NULL
) < 0 || mk_flushContextData(w
->root
) < 0)
537 if (((i
+ w
->segment_ptr
) - w
->f_pos
- 2) > 1)
538 if (mk_writeVoid(w
->root
, (i
+ w
->segment_ptr
) - w
->f_pos
- 2) < 0
539 || mk_flushContextData(w
->root
) < 0)
543 if (mk_seekFile(w
, w
->duration_ptr
) < 0)
545 if (mk_writeFloatRaw(w
->root
,
546 (float) ((double) (max_frame_tc
+ w
->def_duration
) /
548 || mk_flushContextData(w
->root
) < 0)
550 if (mk_seekFile(w
, w
->segment_ptr
- 8) < 0)
552 segment_size
= w
->f_eof
- w
->segment_ptr
;
553 for (i
= 7; i
> 0; --i
)
554 c_size
[i
] = segment_size
>> (8 * (7 - i
));
556 if (mk_appendContextData(w
->root
, &c_size
, 8) < 0 ||
557 mk_flushContextData(w
->root
) < 0)
559 if (mk_seekFile(w
, w
->segmentuid_ptr
) < 0)
561 if (mk_writeBin(w
->root
, MATROSKA_ID_SEGMENTUID
, segment_uid
,
562 sizeof(segment_uid
)) < 0 ||
563 mk_flushContextData(w
->root
) < 0)
567 if (mk_closeContext(w
->root
, 0) < 0)
569 mk_destroyContexts(w
);