Many changes.
[libmkv.git] / src / tracks.c
blobf0d4ac2425ae1939db80856d62556af0c2268faf
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 if (mk_writeUInt(ti, 0x9c, tc->flagLacing) < 0) // FlagLacing
66 return NULL;
67 if (mk_writeStr(ti, 0x86, tc->codecID) < 0) // CodecID
68 return NULL;
69 if (tc->codecPrivateSize && (tc->codecPrivate != NULL))
70 if (mk_writeBin(ti, 0x63a2, tc->codecPrivate, tc->codecPrivateSize) < 0) // CodecPrivate
71 return NULL;
72 if (tc->defaultDuration) {
73 if (mk_writeUInt(ti, 0x23e383, tc->defaultDuration) < 0)
74 return NULL;
75 track->default_duration = tc->defaultDuration;
77 if (tc->language)
78 if (mk_writeStr(ti, 0x22b59c, tc->language) < 0) // Language
79 return NULL;
80 if (mk_writeUInt(ti, 0xb9, tc->flagEnabled) < 0) // FlagEnabled
81 return NULL;
82 if (mk_writeUInt(ti, 0x88, tc->flagDefault) < 0) // FlagDefault
83 return NULL;
84 if (tc->flagForced)
85 if (mk_writeUInt(ti, 0x55aa, tc->flagForced) < 0) // FlagForced
86 return NULL;
87 if (tc->minCache)
88 if (mk_writeUInt(ti, 0x6de7, tc->minCache) < 0) // MinCache
89 return NULL;
90 /* FIXME: this won't handle NULL values, which signals that the cache is disabled. */
91 if (tc->maxCache)
92 if (mk_writeUInt(ti, 0x6df8, tc->maxCache) < 0) // MaxCache
93 return NULL;
95 switch (tc->trackType)
97 case MK_TRACK_VIDEO: // Video
98 if ((v = mk_createContext(w, ti, 0xe0)) == NULL)
99 return NULL;
100 if (tc->video.pixelCrop[0] != 0 || tc->video.pixelCrop[1] != 0 || tc->video.pixelCrop[2] != 0 || tc->video.pixelCrop[3] != 0) {
101 for (i = 0; i < 4; i++) {
102 if (mk_writeUInt(v, 0x54aa + (i * 0x11), tc->video.pixelCrop[i]) < 0) // PixelCrop
103 return NULL;
106 if (mk_writeUInt(v, 0xb0, tc->video.pixelWidth) < 0) // PixelWidth
107 return NULL;
108 if (mk_writeUInt(v, 0xba, tc->video.pixelHeight) < 0 ) // PixelHeight
109 return NULL;
110 if (mk_writeUInt(v, 0x54b0, tc->video.displayWidth) < 0) // DisplayWidth
111 return NULL;
112 if (mk_writeUInt(v, 0x54ba, tc->video.displayHeight) < 0) // DisplayHeight
113 return NULL;
114 if (tc->video.displayUnit)
115 if (mk_writeUInt(v, 0x54b2, tc->video.displayUnit) < 0) // DisplayUnit
116 return NULL;
117 break;
118 case MK_TRACK_AUDIO: // Audio
119 if ((v = mk_createContext(w, ti, 0xe1)) == NULL)
120 return NULL;
121 if (mk_writeFloat(v, 0xb5, tc->audio.samplingFreq) < 0) // SamplingFrequency
122 return NULL;
123 if (mk_writeUInt(v, 0x9f, tc->audio.channels) < 0) // Channels
124 return NULL;
125 if (tc->audio.bitDepth)
126 if (mk_writeUInt(v, 0x6264, tc->audio.bitDepth) < 0) // BitDepth
127 return NULL;
128 break;
129 default: // Other TODO: Implement other track types.
130 return NULL;
133 if (mk_closeContext(v, 0) < 0)
134 return NULL;
135 if (mk_closeContext(ti, 0) < 0)
136 return NULL;
138 return track;
141 int mk_writeTracks(mk_Writer *w, mk_Context *tracks)
143 w->seek_data.tracks = w->root->d_cur;
145 CHECK(mk_closeContext(w->tracks, 0));
147 return 0;