Tracks: Write TrackFlagEnabled only if it's not 1.
[libmkv.git] / src / tracks.c
blob3f8be40f9943890aa04d62409a6768ff084e837a
1 /*****************************************************************************
2 * tracks.c:
3 *****************************************************************************
4 * Copyright (C) 2007 libmkv
5 * $Id: $
7 * Authors: Mike Matsnev
8 * Nathan Caldwell
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
24 #include "libmkv.h"
25 #include "matroska.h"
26 #include "config.h"
28 /* TODO: Figure out what can actually fail without damaging the track. */
30 mk_Track *mk_createTrack(mk_Writer *w, mk_TrackConfig *tc)
32 mk_Context *ti, *v;
33 int i;
34 mk_Track *track = calloc(1, sizeof(*track));
35 if (track == NULL)
36 return NULL;
38 if ((w->tracks_arr = realloc(w->tracks_arr, (w->num_tracks + 1) * sizeof(mk_Track *))) == NULL)
39 return NULL; // FIXME
40 w->tracks_arr[w->num_tracks] = track;
41 track->track_id = ++w->num_tracks;
43 if (w->tracks == NULL)
45 if ((w->tracks = mk_createContext(w, w->root, 0x1654ae6b)) == NULL) // tracks
46 return NULL;
49 if ((ti = mk_createContext(w, w->tracks, 0xae)) == NULL) // TrackEntry
50 return NULL;
51 if (mk_writeUInt(ti, 0xd7, track->track_id) < 0) // TrackNumber
52 return NULL;
53 if (tc->trackUID)
55 if (mk_writeUInt(ti, 0x73c5, tc->trackUID) < 0) // TrackUID
56 return NULL;
58 else
60 if (mk_writeUInt(ti, 0x73c5, track->track_id) < 0)
61 return NULL;
63 if (mk_writeUInt(ti, 0x83, tc->trackType) < 0) // TrackType
64 return NULL;
65 track->track_type = tc->trackType;
66 if (mk_writeUInt(ti, 0x9c, tc->flagLacing) < 0) // FlagLacing
67 return NULL;
68 if (mk_writeStr(ti, 0x86, tc->codecID) < 0) // CodecID
69 return NULL;
70 if (tc->codecPrivateSize && (tc->codecPrivate != NULL))
71 if (mk_writeBin(ti, 0x63a2, tc->codecPrivate, tc->codecPrivateSize) < 0) // CodecPrivate
72 return NULL;
73 if (tc->defaultDuration) {
74 if (mk_writeUInt(ti, 0x23e383, tc->defaultDuration) < 0)
75 return NULL;
76 track->default_duration = tc->defaultDuration;
78 if (tc->language)
79 if (mk_writeStr(ti, 0x22b59c, tc->language) < 0) // Language
80 return NULL;
81 if (tc->flagEnabled != 1)
82 if (mk_writeUInt(ti, 0xb9, tc->flagEnabled) < 0) // FlagEnabled
83 return NULL;
84 if (mk_writeUInt(ti, 0x88, tc->flagDefault) < 0) // FlagDefault
85 return NULL;
86 if (tc->flagForced)
87 if (mk_writeUInt(ti, 0x55aa, tc->flagForced) < 0) // FlagForced
88 return NULL;
89 if (tc->minCache)
90 if (mk_writeUInt(ti, 0x6de7, tc->minCache) < 0) // MinCache
91 return NULL;
92 /* FIXME: this won't handle NULL values, which signals that the cache is disabled. */
93 if (tc->maxCache)
94 if (mk_writeUInt(ti, 0x6df8, tc->maxCache) < 0) // MaxCache
95 return NULL;
97 switch (tc->trackType)
99 case MK_TRACK_VIDEO: // Video
100 if ((v = mk_createContext(w, ti, 0xe0)) == NULL)
101 return NULL;
102 if (tc->video.pixelCrop[0] != 0 || tc->video.pixelCrop[1] != 0 || tc->video.pixelCrop[2] != 0 || tc->video.pixelCrop[3] != 0) {
103 for (i = 0; i < 4; i++) {
104 if (mk_writeUInt(v, 0x54aa + (i * 0x11), tc->video.pixelCrop[i]) < 0) // PixelCrop
105 return NULL;
108 if (mk_writeUInt(v, 0xb0, tc->video.pixelWidth) < 0) // PixelWidth
109 return NULL;
110 if (mk_writeUInt(v, 0xba, tc->video.pixelHeight) < 0 ) // PixelHeight
111 return NULL;
112 if (mk_writeUInt(v, 0x54b0, tc->video.displayWidth) < 0) // DisplayWidth
113 return NULL;
114 if (mk_writeUInt(v, 0x54ba, tc->video.displayHeight) < 0) // DisplayHeight
115 return NULL;
116 if (tc->video.displayUnit)
117 if (mk_writeUInt(v, 0x54b2, tc->video.displayUnit) < 0) // DisplayUnit
118 return NULL;
119 break;
120 case MK_TRACK_AUDIO: // Audio
121 if ((v = mk_createContext(w, ti, 0xe1)) == NULL)
122 return NULL;
123 if (mk_writeFloat(v, 0xb5, tc->audio.samplingFreq) < 0) // SamplingFrequency
124 return NULL;
125 if (mk_writeUInt(v, 0x9f, tc->audio.channels) < 0) // Channels
126 return NULL;
127 if (tc->audio.bitDepth)
128 if (mk_writeUInt(v, 0x6264, tc->audio.bitDepth) < 0) // BitDepth
129 return NULL;
130 break;
131 default: // Other TODO: Implement other track types.
132 return NULL;
135 if (mk_closeContext(v, 0) < 0)
136 return NULL;
137 if (mk_closeContext(ti, 0) < 0)
138 return NULL;
140 return track;
143 int mk_writeTracks(mk_Writer *w, mk_Context *tracks)
145 w->seek_data.tracks = w->root->d_cur;
147 CHECK(mk_closeContext(w->tracks, 0));
149 return 0;