Bump version to 0.6.4.1
[libmkv.git] / src / chapters.c
blob77d7d68d1537667d59bad5b8ef486aa387d2daa5
1 /*****************************************************************************
2 * chapters.c:
3 *****************************************************************************
4 * Copyright (C) 2007 libmkv
6 * Authors: Nathan Caldwell
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
22 #include "config.h"
23 #include "libmkv.h"
24 #include "matroska.h"
26 int mk_createChapterSimple(mk_Writer *w, uint64_t start, uint64_t end,
27 char *name)
29 mk_Context *ca, *cd;
30 unsigned long chapter_uid;
33 * Generate a random UID for this Chapter.
34 * NOTE: This probably should be a CRC32 of some unique chapter information.
35 * In place of being completely random.
37 chapter_uid = random();
39 if (w->chapters == NULL) {
40 unsigned long edition_uid;
41 edition_uid = random();
43 /* Chapters */
44 if ((w->chapters = mk_createContext(w, w->root, MATROSKA_ID_CHAPTERS)) == NULL)
45 return -1;
46 /* EditionEntry */
47 if ((w->edition_entry = mk_createContext(w, w->chapters, MATROSKA_ID_EDITIONENTRY)) == NULL)
48 return -1;
49 /* EditionUID - See note above about Chapter UID. */
50 CHECK(mk_writeUInt(w->edition_entry, MATROSKA_ID_EDITIONUID, edition_uid));
51 /* EditionFlagDefault - This is set to 1 to force this Edition to be the default one. */
52 CHECK(mk_writeUInt(w->edition_entry, MATROSKA_ID_EDITIONFLAGDEFAULT, 1));
53 /* EditionFlagOrdered - Force simple chapters. */
54 CHECK(mk_writeUInt(w->edition_entry, MATROSKA_ID_EDITIONFLAGORDERED, 0));
56 /* ChapterAtom */
57 if ((ca = mk_createContext(w, w->edition_entry, MATROSKA_ID_CHAPTERATOM)) == NULL)
58 return -1;
59 CHECK(mk_writeUInt(ca, MATROSKA_ID_CHAPTERUID, chapter_uid)); /* ChapterUID */
60 CHECK(mk_writeUInt(ca, MATROSKA_ID_CHAPTERTIMESTART, start)); /* ChapterTimeStart */
61 if (end != start) /* Only create a StartTime if chapter length would be 0. */
62 CHECK(mk_writeUInt(ca, MATROSKA_ID_CHAPTERTIMEEND, end)); /* ChapterTimeEnd */
63 if (name != NULL) {
64 /* ChapterDisplay */
65 if ((cd = mk_createContext(w, ca, MATROSKA_ID_CHAPTERDISPLAY)) == NULL)
66 return -1;
67 CHECK(mk_writeStr(cd, MATROSKA_ID_CHAPTERSTRING, name)); /* ChapterString */
68 CHECK(mk_closeContext(cd, 0));
70 CHECK(mk_closeContext(ca, 0));
72 return 0;
75 int mk_writeChapters(mk_Writer *w)
77 if ((w->chapters == NULL) || (w->edition_entry == NULL))
78 return -1;
79 CHECK(mk_closeContext(w->edition_entry, 0));
80 CHECK(mk_closeContext(w->chapters, 0));
82 return 0;