1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007 libmkv
7 * Authors: Nathan Caldwell
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 *****************************************************************************/
27 int mk_createChapterSimple(mk_Writer
*w
, uint64_t start
, uint64_t end
, char *name
)
31 if (w
->chapters
== NULL
) {
32 if ((w
->chapters
= mk_createContext(w
, w
->root
, 0x1043a770)) == NULL
) // Chapters
34 if ((w
->edition_entry
= mk_createContext(w
, w
->chapters
, 0x45b9)) == NULL
) // EditionEntry
36 CHECK(mk_writeUInt(w
->edition_entry
, 0x45db, 1)); // EditionFlagDefault - Force this to be the default.
37 CHECK(mk_writeUInt(w
->edition_entry
, 0x45dd, 0)); // EditionFlagOrdered - Force simple chapters.
39 if ((ca
= mk_createContext(w
, w
->edition_entry
, 0xb6)) == NULL
) // ChapterAtom
41 CHECK(mk_writeUInt(ca
, 0x73c4, ++w
->chapter_uid
));
42 CHECK(mk_writeUInt(ca
, 0x91, start
)); // ChapterTimeStart
43 if (end
!= start
) // Just create a StartTime if chapter length would be 0.
44 CHECK(mk_writeUInt(ca
, 0x92, end
)); // ChapterTimeEnd
46 if ((cd
= mk_createContext(w
, ca
, 0x80)) == NULL
) // ChapterDisplay
48 CHECK(mk_writeStr(cd
, 0x85, name
)); // ChapterDisplay
49 CHECK(mk_closeContext(cd
, 0));
51 CHECK(mk_closeContext(ca
, 0));
56 /* FIXME: Don't use CHECK when we need to return a pointer! */
57 mk_Edition
*mk_createEdition(mk_Writer
*w
, uint8_t flagHidden
, uint8_t flagDefault
, uint8_t flagOrdered
) {
58 mk_Edition
*ed
= calloc(1, sizeof(*ed
));
62 if (w
->chapters
.context
== NULL
) {
63 if ((w
->chapters
.context
= mk_createContext(w
, w
->root
, 0x1043a770)) == NULL
) // Chapters
65 ed
->tail
= calloc(1, sizeof(*mk_Edition
));
66 w
->chapters
.children
= ed
;
68 ed
->tail
= w
->chapters
.children
->tail
;
69 **(ed
->tail
)->next
= ed
;
70 ed
->prev
= *(ed
->tail
);
75 if ((ed
->context
= mk_createContext(w
, w
->chapters
, 0x45b9)) == NULL
) // EditionEntry
79 CHECK(mk_writeUInt(ed
, 0x45bd, flagHidden
)); // EditionFlagHidden
81 CHECK(mk_writeUInt(ed
, 0x45db, flagDefault
)); // EditionFlagDefault
83 CHECK(mk_writeUInt(ed
, 0x45dd, flagOrdered
)); // EditionFlagOrdered
88 /* edition, and parent are mutially exclusive. If you specify both, edition will be ignored. */
89 mk_Chapter
*mk_createChapter(mk_Writer
*w
, mk_Chapter
*config
, mk_Edition
*edition
, mk_Chapter
*parent
) {
90 mk_Chapter
*chapter
= calloc(1, sizeof(*chapter
));
91 if (config
== NULL
|| chapter
== NULL
)
93 memcpy(chapter
, config
, sizeof(*chapter
));
96 if (parent
->children
) {
97 chapter
->tail
= parent
->children
->tail
;
98 **(chapter
->tail
)->next
= chapter
;
99 chapter
->prev
= *(chapter
->tail
);
101 chapter
->tail
= calloc(1, sizeof(*mk_Chapter
));
102 parent
->children
= chapter
;
104 *(chapter
->tail
) = chapter
;
106 if ((chapter
->context
= mk_createContext(w
, parent
->context
, 0xb6)) == NULL
) // ChapterAtom
112 CHECK(mk_writeUInt(c
, 0x73c4, chapter
->chapterUID
)); // ChapterUID - FIXME
113 CHECK(mk_writeUInt(c
, 0x91, chapter
->chapterTimeStart
)); // ChapterTimeStart
114 if (chapter
->chapterTimeStart
<= chapter
->chapterTimeEnd
) // We don't write ChapterTimeEnd if we have a start time that would result in a negative or 0-length chapter.
115 CHECK(mk_writeUInt(c
, 0x92, chapter
->chapterTimeEnd
)); // ChapterTimeEnd
116 if (chapter
->chapterFlagHidden
!= 0)
117 CHECK(mk_writeUInt(c
, 0x98, chapter
->chapterFlagHidden
)); // ChapterFlagHidden
118 if (chapter
->chapterFlagEnabled
!= 1)
119 CHECK(mk_writeUInt(c
, 0x4598, chapter
->chapterFlagEnabled
)); // ChapterFlagEnabled
120 if (chapter
->chapterSegmentUID
!= 0)
121 CHECK(mk_writeUInt(c
, 0x6e67, chapter
->chapterSegmentUID
)); // ChapterSegmentUID - FIXME
122 if (chapter
->chapterSegmentEditionUID
!= 0)
123 CHECK(mk_writeUInt(c
, 0x6ebc, chapter
->chapterSegmentEditionUID
)); // ChapterSegmentEditionUID - FIXME
128 /* country is optional. If lang is blank or NULL the language will be set to 'und' (Unknown/Undefined) */
129 int mk_appendChapterDisplay(mk_Writer
*w
, mk_Context
*chapter
, char *string
, char *lang
, char *country
) {
132 if ((c
= mk_createContext(w
, chapter
, 0x80)) == NULL
) // ChapterDisplay
135 CHECK(mk_writeStr(c
, 0x85, string
)); // ChapString
137 CHECK(mk_writeStr(c
, 0x437c, lang
)); // ChapLanguage
139 CHECK(mk_writeStr(c
, 0x437e, country
)); // ChapCountry
141 CHECK(mk_closeContext(c
));
146 int mk_appendChapterTrack(mk_Writer
*w
, mk_Context
*chapter
, mk_Track
*track
) {
149 if ((c
= mk_createContext(w
, chapter
, 0x8f)) == NULL
)
152 int mk_writeChapters(mk_Writer
*w
) {
153 if ((w
->chapters
== NULL
) || (w
->edition_entry
== NULL
))
155 CHECK(mk_closeContext(w
->edition_entry
, 0));
156 CHECK(mk_closeContext(w
->chapters
, 0));