1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007 libmkv
7 * Authors: Mike Matsnev
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 *****************************************************************************/
28 /* TODO: Figure out what can actually fail without damaging the track. */
30 mk_Track
*mk_createTrack(mk_Writer
*w
, mk_TrackConfig
*tc
)
34 mk_Track
*track
= calloc(1, sizeof(*track
));
38 if ((w
->tracks_arr
= realloc(w
->tracks_arr
, (w
->num_tracks
+ 1) * sizeof(mk_Track
*))) == NULL
)
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
49 if ((ti
= mk_createContext(w
, w
->tracks
, 0xae)) == NULL
) // TrackEntry
51 if (mk_writeUInt(ti
, 0xd7, track
->track_id
) < 0) // TrackNumber
55 if (mk_writeUInt(ti
, 0x73c5, tc
->trackUID
) < 0) // TrackUID
60 if (mk_writeUInt(ti
, 0x73c5, track
->track_id
) < 0)
63 if (mk_writeUInt(ti
, 0x83, tc
->trackType
) < 0) // TrackType
65 track
->track_type
= tc
->trackType
;
66 if (mk_writeUInt(ti
, 0x9c, tc
->flagLacing
) < 0) // FlagLacing
68 if (mk_writeStr(ti
, 0x86, tc
->codecID
) < 0) // CodecID
70 if (tc
->codecPrivateSize
&& (tc
->codecPrivate
!= NULL
))
71 if (mk_writeBin(ti
, 0x63a2, tc
->codecPrivate
, tc
->codecPrivateSize
) < 0) // CodecPrivate
73 if (tc
->defaultDuration
) {
74 if (mk_writeUInt(ti
, 0x23e383, tc
->defaultDuration
) < 0)
76 track
->default_duration
= tc
->defaultDuration
;
79 if (mk_writeStr(ti
, 0x22b59c, tc
->language
) < 0) // Language
81 if (tc
->flagEnabled
!= 1)
82 if (mk_writeUInt(ti
, 0xb9, tc
->flagEnabled
) < 0) // FlagEnabled
84 if (mk_writeUInt(ti
, 0x88, tc
->flagDefault
) < 0) // FlagDefault
87 if (mk_writeUInt(ti
, 0x55aa, tc
->flagForced
) < 0) // FlagForced
90 if (mk_writeUInt(ti
, 0x6de7, tc
->minCache
) < 0) // MinCache
92 /* FIXME: this won't handle NULL values, which signals that the cache is disabled. */
94 if (mk_writeUInt(ti
, 0x6df8, tc
->maxCache
) < 0) // MaxCache
97 switch (tc
->trackType
)
99 case MK_TRACK_VIDEO
: // Video
100 if ((v
= mk_createContext(w
, ti
, 0xe0)) == 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
108 if (mk_writeUInt(v
, 0xb0, tc
->video
.pixelWidth
) < 0) // PixelWidth
110 if (mk_writeUInt(v
, 0xba, tc
->video
.pixelHeight
) < 0 ) // PixelHeight
112 if (mk_writeUInt(v
, 0x54b0, tc
->video
.displayWidth
) < 0) // DisplayWidth
114 if (mk_writeUInt(v
, 0x54ba, tc
->video
.displayHeight
) < 0) // DisplayHeight
116 if (tc
->video
.displayUnit
)
117 if (mk_writeUInt(v
, 0x54b2, tc
->video
.displayUnit
) < 0) // DisplayUnit
120 case MK_TRACK_AUDIO
: // Audio
121 if ((v
= mk_createContext(w
, ti
, 0xe1)) == NULL
)
123 if (mk_writeFloat(v
, 0xb5, tc
->audio
.samplingFreq
) < 0) // SamplingFrequency
125 if (mk_writeUInt(v
, 0x9f, tc
->audio
.channels
) < 0) // Channels
127 if (tc
->audio
.bitDepth
)
128 if (mk_writeUInt(v
, 0x6264, tc
->audio
.bitDepth
) < 0) // BitDepth
131 default: // Other TODO: Implement other track types.
135 if (mk_closeContext(v
, 0) < 0)
137 if (mk_closeContext(ti
, 0) < 0)
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));