File: mp4.cpp

package info (click to toggle)
libtunepimp 0.5.3-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,668 kB
  • ctags: 3,470
  • sloc: ansic: 15,652; cpp: 12,752; sh: 11,929; perl: 1,179; python: 720; makefile: 310; pascal: 33
file content (374 lines) | stat: -rw-r--r-- 10,251 bytes parent folder | download | duplicates (2)
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
/*----------------------------------------------------------------------------

   libtunepimp -- The MusicBrainz tagging library.  
                  Let a thousand taggers bloom!
   
   Copyright (C) 2005 Lukas Lalinsky
   
   This file is part of libtunepimp.

   libtunepimp is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   libtunepimp is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libtunepimp; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   $Id: mp4.cpp 8497 2006-09-29 21:26:44Z luks $

----------------------------------------------------------------------------*/

#include <string.h>
#include <stdio.h>
#include <mp4.h>
#include "metadata.h"
#include "plugin.h"
#ifndef WIN32
#include "../../lib/utf8/utf8util.h"
#endif

using namespace std;

#ifndef WIN32
#define initPlugin mp4InitPlugin
#endif

#define PLUGIN_VERSION "1.0.0"
#define PLUGIN_NAME    "MP4 metadata reader/writer"

static char *formats[][2] = {
  { ".aac", "AAC/MP4" },
  { ".mp4", "MP4" },
  { ".m4a", "MP4" },
  { ".m4b", "MP4" },
  { ".m4p", "MP4" },
};

#define NUM_FORMATS 5

static char *errorString = "";

static void
mp4Shutdown()
{
  if (strlen(errorString))
    free(errorString);
}

static const char *
mp4GetVersion()
{
  return PLUGIN_VERSION;
}

static const char *
mp4GetName()
{
  return PLUGIN_NAME;
}

static int
mp4GetNumFormats(void)
{
  return NUM_FORMATS;
}

static int
mp4GetFormat(int i, char ext[TP_EXTENSION_LEN],
		char desc[TP_PLUGIN_DESC_LEN],int *functions)
{
  if (i < 0 || i >= NUM_FORMATS)
    return 0;
  
  strcpy(ext, formats[i][0]);
  strcpy(desc, formats[i][0]);
  *functions = TP_PLUGIN_FUNCTION_METADATA;

  return 1;
}

static const char *
mp4GetError()
{
  return errorString;
}

static int
mp4ReadMetadata(metadata_t *mdata, const char *fileName, int flags, const char *encoding)
{
  char *value;
  u_int16_t numval, numval2;
  u_int8_t numval3;
  u_int32_t size;
  MP4FileHandle mp4file;
  
#ifndef WIN32
  mp4file = MP4Read(utf8ToEncoding(fileName, encoding).c_str());
#else  
  mp4file = MP4Read(fileName);
#endif  

  if (mp4file == MP4_INVALID_FILE_HANDLE) 
    return 0;
 
  memset(mdata, 0, sizeof(metadata_t));
  
  if (MP4GetMetadataName(mp4file, &value) && value != NULL) {
    strcpy(mdata->track, value);
    free(value);
  }

  if (MP4GetMetadataArtist(mp4file, &value) && value != NULL) {
    strcpy(mdata->artist, value);
    free(value);
  }

  if (MP4GetMetadataYear(mp4file, &value) && value != NULL) {
    mdata->releaseYear = strtol(value, NULL, 0);
    free(value);
  }

  if (MP4GetMetadataAlbum(mp4file, &value) && value != NULL) {
    strcpy(mdata->album, value);
    free(value);
  }  
  
  if (MP4GetMetadataTrack(mp4file, &numval, &numval2)) {
    mdata->trackNum = numval;
    mdata->totalInSet = numval2;
  }

  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Sortname", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->sortName, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Track Id", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->trackId, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Id", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->albumId, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Artist Id", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->artistId, value);
    free(value);
  }

  if (MP4GetMetadataFreeForm(mp4file, "MusicIP PUID", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->filePUID, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Artist Id", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->albumArtistId, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Artist Sortname", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->albumArtistSortName, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Artist", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->albumArtist, value);
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Type", (u_int8_t **)&value, &size) && value != NULL) {
    mdata->albumType = convertToAlbumType(value);
    free(value);
  }
    
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Status", (u_int8_t **)&value, &size) && value != NULL) {
    mdata->albumStatus = convertToAlbumStatus(value);
    free(value);
  }
    
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Release Date", (u_int8_t **)&value, &size) && value != NULL) {
    int year = 0, month = 0, day = 0;
    if (sscanf(value, "%04d-%02d-%02d", &year, &month, &day) > 0) {
      mdata->releaseYear  = year;
      mdata->releaseMonth = month;
      mdata->releaseDay   = day;
    }
    free(value);
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Album Release Country", (u_int8_t **)&value, &size) && value != NULL) {
    strcpy(mdata->releaseCountry, value);
    free(value);
  }
  
  u_int32_t numTracks = MP4GetNumberOfTracks(mp4file);
  for (u_int32_t i = 0; i < numTracks; i++) {
    MP4TrackId trackId = MP4FindTrackId(mp4file, i);
    const char *trackType = MP4GetTrackType(mp4file, trackId);
    if (!strcmp(trackType, MP4_AUDIO_TRACK_TYPE)) {
      MP4Duration trackDuration = MP4GetTrackDuration(mp4file, trackId); 
      mdata->duration = (unsigned long)MP4ConvertFromTrackDuration(mp4file, trackId, trackDuration, MP4_MSECS_TIME_SCALE);    
    }
  }  

  if (MP4GetMetadataCompilation(mp4file, &numval3)) {
    mdata->variousArtist = numval3;
  }
  
  if (MP4GetMetadataFreeForm(mp4file, "MusicBrainz Non-Album", (u_int8_t **)&value, &size) && value != NULL) {
    mdata->nonAlbum = atoi(value);
    free(value);
  }
  
  strcpy(mdata->fileFormat, fileName + strlen(fileName) - 3); 
  
  if (!MP4Close(mp4file))
    return 0;
  
  return 1;
}

static int
mp4WriteMetadata(const metadata_t *mdata, const char *fileName, int flags,
		   const char *encoding)
{
  char temp[256];
  string temp2;
  MP4FileHandle mp4file;
  
#ifndef WIN32
  mp4file = MP4Modify(utf8ToEncoding(fileName, encoding).c_str());
#else  
  mp4file = MP4Modify(fileName);
#endif  

  if (mp4file == MP4_INVALID_FILE_HANDLE)
    return 0;

  if ((flags & TP_PLUGIN_FLAGS_GENERAL_CLEAR_TAGS) != 0)
    MP4MetadataDelete(mp4file);
  
  MP4SetMetadataName(mp4file, mdata->track);
  
  MP4SetMetadataArtist(mp4file, mdata->artist);
  
  sprintf(temp, "%04d", mdata->releaseYear);
  MP4SetMetadataYear(mp4file, temp);
  
  MP4SetMetadataAlbum(mp4file, mdata->album);
  
  MP4SetMetadataTrack(mp4file, mdata->trackNum, mdata->totalInSet);
  
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Sortname", (u_int8_t *)mdata->sortName, strlen(mdata->sortName) + 1);
  
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Track Id", (u_int8_t *)mdata->trackId, strlen(mdata->trackId) + 1);
  
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Id", (u_int8_t *)mdata->albumId, strlen(mdata->albumId) + 1);
  
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Artist Id", (u_int8_t *)mdata->artistId, strlen(mdata->artistId) + 1);

  MP4SetMetadataFreeForm(mp4file, "MusicIP PUID", (u_int8_t *)mdata->filePUID, strlen(mdata->filePUID) + 1);

  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Artist Id", (u_int8_t *)mdata->albumArtistId, strlen(mdata->albumArtistId) + 1);

  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Artist Sortname", (u_int8_t *)mdata->albumArtistSortName, strlen(mdata->albumArtistSortName) + 1);

  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Artist", (u_int8_t *)mdata->albumArtist, strlen(mdata->albumArtist) + 1);

  convertFromAlbumType(mdata->albumType, temp2);
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Type", (u_int8_t *)temp2.c_str(), temp2.length() + 1);
  
  convertFromAlbumStatus(mdata->albumStatus, temp2);
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Status", (u_int8_t *)temp2.c_str(), temp2.length() + 1);
  
  if (mdata->releaseYear > 0) {
    if (mdata->releaseMonth > 0) {
      if (mdata->releaseDay > 0) {
        sprintf(temp, "%04d-%02d-%02d", mdata->releaseYear, mdata->releaseMonth, mdata->releaseDay);
      }
      else {
        sprintf(temp, "%04d-%02d", mdata->releaseYear, mdata->releaseMonth);
      }
    }
    else { 
      sprintf(temp, "%04d", mdata->releaseYear);
    }
  }
  else {
    strcpy(temp, "");
  }
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Release Date", (u_int8_t *)temp, strlen(temp) + 1);
  
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Album Release Country", (u_int8_t *)mdata->releaseCountry, strlen(mdata->releaseCountry) + 1);

  MP4SetMetadataCompilation(mp4file, mdata->variousArtist ? 1 : 0);

  sprintf(temp, "%d", mdata->nonAlbum);  
  MP4SetMetadataFreeForm(mp4file, "MusicBrainz Non-Album", (u_int8_t *)temp, strlen(temp) + 1);
  
  if (!MP4Close(mp4file))
    return 0;

#ifndef WIN32
  if (!MP4Optimize(utf8ToEncoding(fileName, encoding).c_str()))
#else  
  if (!MP4Optimize(fileName))
#endif    
    return 0;
  
  return 1;
}

static unsigned long
mp4GetDuration(const char *fileName, int flags, const char *encoding)
{
  return 0;
}

static Plugin methods = {
  mp4Shutdown,
  mp4GetVersion,
  mp4GetName,
  mp4GetNumFormats,
  mp4GetFormat,
  mp4GetError,
  mp4ReadMetadata,
  mp4WriteMetadata,
  mp4GetDuration,
  NULL,
  NULL,
  NULL,
  NULL
};

extern "C" Plugin *
initPlugin()
{
  return &methods;
}

#ifdef WIN32

#include <sys/timeb.h>

extern "C" int
gettimeofday (struct timeval *t, void *foo)
{
	struct _timeb temp;
	_ftime(&temp);
	t->tv_sec = temp.time;
	t->tv_usec = temp.millitm * 1000;
	return (0);
}

#endif