forked from gonzoua/AudioBookBinder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MP4File.m
424 lines (367 loc) · 13.8 KB
/
MP4File.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//
// Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@bluezbox.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice unmodified, this list of conditions, and the following
// disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
#import "ABLog.h"
#import "MP4Atom.h"
#import "MP4File.h"
// 4M seems to be reasonable buffer
#define TMP_BUFFER_SIZE 16*1024*1024
@implementation MP4File
@synthesize artist, album, title, coverFile;
@synthesize track, tracksTotal, gaplessPlay;
-(id) initWithFileName: (NSString*)fileName
{
[super init];
_fh = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
self.artist = nil;
self.album = nil;
self.title = nil;
self.genre = nil;
self.year = nil;
self.composer = nil;
self.comment = nil;
self.coverFile = nil;
track = tracksTotal = 0;
gaplessPlay = NO;
UInt64 pos = 0;
NSData *buffer;
UInt64 end = [_fh seekToEndOfFile];
[_fh seekToFileOffset:0];
while (pos < end) {
// load atoms
buffer = [_fh readDataOfLength:8];
MP4Atom *atom = [[MP4Atom alloc] initWithHeaderData:buffer
andOffset:pos];
pos += [atom length];
[_fh seekToFileOffset:pos];
}
return self;
}
-(void) dealloc
{
self.artist = nil;
self.title = nil;
self.genre = nil;
self.composer = nil;
self.year = nil;
self.comment = nil;
self.coverFile = nil;
[super dealloc];
}
-(id) findAtom: (NSString*)atomName
{
UInt64 pos = 0;
UInt64 end = 0;
NSData *buffer;
id result = nil;
NSMutableArray *chunks = [[NSMutableArray alloc]
initWithArray:[atomName componentsSeparatedByString: @"."]];
end = [_fh seekToEndOfFile];
[_fh seekToFileOffset:0];
while (pos < end) {
// load atoms
buffer = [_fh readDataOfLength:8];
MP4Atom *atom = [[MP4Atom alloc] initWithHeaderData:buffer
andOffset:pos];
if ([[atom name] isEqualToString: [chunks objectAtIndex:0]])
{
end = pos + [atom length];
// meta header has 4 bytes of data after header
if ([[atom name] isEqualToString: @"meta"])
pos += 12;
else
// skip only atom header and start with content
pos += 8;
[chunks removeObjectAtIndex:0];
if ([chunks count] == 0)
{
result = atom;
break;
}
}
else
pos += [atom length];
[_fh seekToFileOffset:pos];
}
return result;
}
/*
* This function assumes that we work with fresh, newly-created file,
* there should be no "meta" atom
*/
-(BOOL) updateFile
{
NSMutableData *hdlrContent = [NSMutableData dataWithData:[self encodeHDLRAtom]];
MP4Atom *freeAtom = [self findAtom:@"free"];
BOOL haveIlstAtom, haveMetaAtom, haveUdtaAtom;
MP4Atom *moovAtom = [self findAtom:@"moov"];
MP4Atom *udtaAtom = [self findAtom:@"moov.udta"];
MP4Atom *metaAtom = [self findAtom:@"moov.udta.meta"];
MP4Atom *ilstAtom = [self findAtom:@"moov.udta.meta.ilst"];
NSAssert(moovAtom != nil,
@"File contains no moov atom");
NSAssert(freeAtom != nil,
@"File contains no free atom");
haveUdtaAtom = (udtaAtom != nil);
haveMetaAtom = (metaAtom != nil);
haveIlstAtom = (ilstAtom != nil);
NSMutableData *newAtomsData = [[NSMutableData alloc] init];
if (title != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©nam"
value:[title dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (album != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©alb"
value:[album dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (artist != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©ART"
value:[artist dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (genre != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©GNRE"
value:[genre dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (composer != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©WRT"
value:[composer dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (comment != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©CMT"
value:[comment dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (year != nil)
[newAtomsData appendData:[self encodeMetaDataAtom:@"©DAY"
value:[year dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (track) {
short bytes[4];
bytes[0] = bytes[3] = 0;
bytes[1] = htons(track);
bytes[2] = htons(tracksTotal);
NSData * data = [[NSData alloc] initWithBytes:bytes length:8];
[newAtomsData appendData:[self encodeMetaDataAtom:@"trkn"
value:data
type:ITUNES_METADATA_IMPLICIT_CLASS]];
[data release];
}
if (gaplessPlay) {
char pgap = 1;
NSData * data = [[NSData alloc] initWithBytes:&pgap length:1];
[newAtomsData appendData:[self encodeMetaDataAtom:@"pgap"
value:data
type:ITUNES_METADATA_UINT8_CLASS]];
[data release];
}
[newAtomsData appendData:[self encodeMetaDataAtom:@"©gen"
value:[@"Audiobooks" dataUsingEncoding:NSUTF8StringEncoding]
type:ITUNES_METADATA_STRING_CLASS]];
if (coverFile != nil)
{
NSData *fileData = [NSData dataWithContentsOfFile:coverFile];
if (fileData)
[newAtomsData appendData:[self encodeMetaDataAtom:@"covr"
value:fileData
type:ITUNES_METADATA_IMAGE_CLASS]];
}
UInt32 additionalLength = [newAtomsData length];
if (ilstAtom)
{
[ilstAtom setLength:[ilstAtom length] + additionalLength];
[self fixupAtom:ilstAtom];
}
else
{
additionalLength += 4 + 4; // length and name
ilstAtom = [[MP4Atom alloc] initWithName:@"ilst" andLength:additionalLength];
}
if (metaAtom)
{
[metaAtom setLength:[metaAtom length] + additionalLength];
[self fixupAtom:metaAtom];
}
else
{
// length, name and misterious junk
additionalLength += [hdlrContent length] + 4 + 4 + 4;
metaAtom = [[MP4Atom alloc] initWithName:@"meta" andLength:additionalLength];
}
if (udtaAtom)
{
[udtaAtom setLength:[udtaAtom length] + additionalLength];
[self fixupAtom:udtaAtom];
}
else {
additionalLength += 4 + 4; // length and name
udtaAtom = [[MP4Atom alloc] initWithName:@"udta" andLength:additionalLength];
}
NSMutableData *atomData = [NSMutableData data];
if (!haveUdtaAtom)
[atomData appendData:[udtaAtom encode]];
if (!haveMetaAtom)
{
[atomData appendData:[metaAtom encode]];
UInt32 flags = 0;
// append
[atomData appendBytes:&flags length:4];
[atomData appendData:hdlrContent];
}
if (!haveIlstAtom)
[atomData appendData:[ilstAtom encode]];
[atomData appendData:newAtomsData];
UInt64 newAtomsOffset = [moovAtom offset] + [moovAtom length];
// is there enough free space for udta tag?
if ((freeAtom == nil) || ([freeAtom length] < additionalLength))
{
MP4Atom *mdatAtom = [self findAtom:@"mdat"];
NSAssert(mdatAtom != nil, @"Failed to find mdat atom");
[self reserveSpace:additionalLength at:newAtomsOffset];
// Make sure mdat atom comes after moov atom
if ([mdatAtom offset] > [moovAtom offset])
[self fixSTCOAtomBy:additionalLength];
}
else
{
// update free atom
[freeAtom setLength:[freeAtom length] - additionalLength];
[freeAtom setOffset:[freeAtom offset] + additionalLength];
[self fixupAtom:freeAtom];
}
// write newly created atoms
[_fh seekToFileOffset:newAtomsOffset];
[_fh writeData:atomData];
// update moov atom
[moovAtom setLength:[moovAtom length] + additionalLength];
[self fixupAtom:moovAtom];
return TRUE;
}
-(void) fixupAtom: (MP4Atom*)atom
{
[_fh seekToFileOffset:[atom offset]];
[_fh writeData:[atom encode]];
}
/*
* Encode iTunes metadata atoms
*/
-(NSData*) encodeMetaDataAtom: (NSString*)name value:(NSData*)value
type:(UInt32)type
{
UInt32 dataAtomSize =
[value length] +
4 + 4 + 4 + 4;
UInt32 atomSize = dataAtomSize + 4 + 4;
MP4Atom *atom = [[MP4Atom alloc] initWithName:name andLength:atomSize];
NSMutableData *data = [NSMutableData dataWithData:[atom encode]];
MP4Atom *dataAtom = [[MP4Atom alloc] initWithName:@"data"
andLength:dataAtomSize];
[data appendData:[dataAtom encode]];
// version and flags
type = htonl(type);
[data appendBytes:&type length:4];
// null data
UInt32 zeroData = 0;
[data appendBytes:&zeroData length:4];
[data appendData:value];
return [NSData dataWithData: data];
}
/*
* Create hdlr atom. Without this atom iTunes refuses to accept file metadata
*/
-(NSData*) encodeHDLRAtom
{
MP4Atom *hdlrAtom = [[MP4Atom alloc] initWithName:@"hdlr" andLength:34];
UInt32 zeroData = 0;
const char *tmp = "mdir";
const char *tmp2 = "appl";
NSMutableData *data = [NSMutableData dataWithData:[hdlrAtom encode]];
[data appendBytes:&zeroData length:4];
[data appendBytes:&zeroData length:4];
[data appendBytes:tmp length:4];
[data appendBytes:tmp2 length:4];
[data appendBytes:&zeroData length:4];
[data appendBytes:&zeroData length:4];
[data appendBytes:&zeroData length:2];
return [NSData dataWithData: data];
}
/*
* Inserts size bytes at offset in file
*/
-(void) reserveSpace:(UInt64)size at:(UInt64)offset
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UInt64 end = [_fh seekToEndOfFile];
#if 0
NSLog(@"size: %lld, start offset: %lld, file size: %lld",
size, offset, end);
#endif
do {
UInt64 bufferSize = MIN(end - offset, TMP_BUFFER_SIZE);
[_fh seekToFileOffset:(end - bufferSize)];
NSData *buffer = [_fh readDataOfLength:bufferSize];
if ([buffer length] == 0)
break;
[_fh seekToFileOffset:(end - [buffer length]) + size];
#if 0
NSLog(@"from: %lld, to: %lld, %lld bytes", (end - bufferSize),
(end - [buffer length]) + size, [buffer length]);
#endif
[_fh writeData:buffer];
end -= [buffer length];
} while(end > offset);
[pool release];
}
/*
* stco atom is an index table that contains offsets of
* mdata "chunks" from the files start. Formar:
* [length] [atom] [version/flags] [nentries] [offs1] ...
*/
-(void) fixSTCOAtomBy:(UInt64)shift
{
UInt32 entries, i, offset;
NSRange r;
MP4Atom *stcoAtom = [self findAtom:@"moov.trak.mdia.minf.stbl.stco"];
NSAssert(stcoAtom != nil,
@"Failed to find moov.trak.mdia.minf.stbl.stco atom");
NSData *origTable;
[_fh seekToFileOffset:[stcoAtom offset]+12]; // size, tag and vesrion/flags
origTable = [_fh readDataOfLength:[stcoAtom length] - 12];
NSMutableData *fixedTable = [[NSMutableData alloc] initWithData:origTable];
[fixedTable getBytes:&entries length:4];
entries = ntohl(entries);
r.location = 4;
r.length = 4;
for (i = 0 ; i < entries; i++)
{
[fixedTable getBytes:&offset range:r];
offset = htonl(ntohl(offset) + shift);
[fixedTable replaceBytesInRange:r withBytes:&offset];
r.location += 4;
}
[_fh seekToFileOffset:[stcoAtom offset]+12]; // size, tag and vesrion/flags
[_fh writeData:fixedTable];
[fixedTable release];
}
@end