Chapter stuff. This is will NOT even compile, do NOT use!
[libmkv.git] / src / chapters.c
blob4a050fe0fc836871f7fd2a5d51a8123b8ab06554
1 /*****************************************************************************
2 * chapters.c:
3 *****************************************************************************
4 * Copyright (C) 2007 libmkv
5 * $Id: $
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 *****************************************************************************/
23 #include "libmkv.h"
24 #include "matroska.h"
25 #include "config.h"
27 int mk_createChapterSimple(mk_Writer *w, uint64_t start, uint64_t end, char *name)
29 mk_Context *ca, *cd;
31 if (w->chapters == NULL) {
32 if ((w->chapters = mk_createContext(w, w->root, 0x1043a770)) == NULL) // Chapters
33 return -1;
34 if ((w->edition_entry = mk_createContext(w, w->chapters, 0x45b9)) == NULL) // EditionEntry
35 return -1;
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
40 return -1;
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
45 if (name != NULL) {
46 if ((cd = mk_createContext(w, ca, 0x80)) == NULL) // ChapterDisplay
47 return -1;
48 CHECK(mk_writeStr(cd, 0x85, name)); // ChapterDisplay
49 CHECK(mk_closeContext(cd, 0));
51 CHECK(mk_closeContext(ca, 0));
53 return 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));
59 if (ed == NULL)
60 return NULL;
62 if (w->chapters.context == NULL) {
63 if ((w->chapters.context = mk_createContext(w, w->root, 0x1043a770)) == NULL) // Chapters
64 return NULL;
65 ed->tail = calloc(1, sizeof(*mk_Edition));
66 w->chapters.children = ed;
67 } else {
68 ed->tail = w->chapters.children->tail;
69 **(ed->tail)->next = ed;
70 ed->prev = *(ed->tail);
73 *(ed->tail) = ed;
75 if ((ed->context = mk_createContext(w, w->chapters, 0x45b9)) == NULL) // EditionEntry
76 return NULL;
78 if (flagHidden != 0)
79 CHECK(mk_writeUInt(ed, 0x45bd, flagHidden)); // EditionFlagHidden
80 if (flagDefault != 0)
81 CHECK(mk_writeUInt(ed, 0x45db, flagDefault)); // EditionFlagDefault
82 if (flagOrdered != 0)
83 CHECK(mk_writeUInt(ed, 0x45dd, flagOrdered)); // EditionFlagOrdered
85 return ed;
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)
92 return NULL;
93 memcpy(chapter, config, sizeof(*chapter));
95 if (parent) {
96 if (parent->children) {
97 chapter->tail = parent->children->tail;
98 **(chapter->tail)->next = chapter;
99 chapter->prev = *(chapter->tail);
100 } else {
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
107 return NULL;
108 } else {
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
125 return c;
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) {
130 mk_Context *c;
132 if ((c = mk_createContext(w, chapter, 0x80)) == NULL) // ChapterDisplay
133 return -1;
135 CHECK(mk_writeStr(c, 0x85, string)); // ChapString
136 if (lang != NULL)
137 CHECK(mk_writeStr(c, 0x437c, lang)); // ChapLanguage
138 if (country != NULL)
139 CHECK(mk_writeStr(c, 0x437e, country)); // ChapCountry
141 CHECK(mk_closeContext(c));
143 return 0;
146 int mk_appendChapterTrack(mk_Writer *w, mk_Context *chapter, mk_Track *track) {
147 mk_Context *c;
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))
154 return -1;
155 CHECK(mk_closeContext(w->edition_entry, 0));
156 CHECK(mk_closeContext(w->chapters, 0));
158 return 0;