summaryrefslogtreecommitdiffstats
path: root/src/metadata/m4a/mp4file.cpp
blob: 7d37d597bf4b056b77e0aef6bfb900c14f71f771 (plain)
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
/***************************************************************************
    copyright            : (C) 2002, 2003, 2006 by Jochen Issing
    email                : jochen.issing@isign-softart.de
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it  under the terms of the GNU Lesser General Public License version  *
 *   2.1 as published by the Free Software Foundation.                     *
 *                                                                         *
 *   This library 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     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            *
 *   MA  02110-1301  USA                                                   *
 ***************************************************************************/

#include <tbytevector.h>
#include <tstring.h>
#include "tlist.h"

#include "mp4itunestag.h"
#include "mp4file.h"
#include "boxfactory.h"
#include "mp4tagsproxy.h"
#include "mp4propsproxy.h"
#include "mp4audioproperties.h"
#include "itunesdatabox.h"

using namespace TagLib;

class MP4::File::FilePrivate
{
public:
  //! list for all boxes of the mp4 file
  TagLib::List<MP4::Mp4IsoBox*> boxes;
  //! the box factory - will create all boxes by tag and size
  MP4::BoxFactory             boxfactory;
  //! proxy for the tags is filled after parsing
  MP4::Mp4TagsProxy           tagsProxy;
  //! proxy for audio properties
  MP4::Mp4PropsProxy          propsProxy;
  //! the tag returned by tag() function
  MP4::Tag                    mp4tag;
  //! container for the audio properties returned by properties() function
  MP4::AudioProperties        mp4audioproperties;
  //! is set to valid after successfully parsing
  bool                        isValid;
};

//! function to fill the tags with converted proxy data, which has been parsed out of the file previously
static void fillTagFromProxy( MP4::Mp4TagsProxy& proxy, MP4::Tag& mp4tag );

MP4::File::File(const char *file, bool , AudioProperties::ReadStyle  )
	:TagLib::File( file )
{
  // create member container
  d = new MP4::File::FilePrivate();


  d->isValid = false;
  TagLib::uint size;
  MP4::Fourcc  fourcc;

  while( readSizeAndType( size, fourcc ) == true )
  {
    // create the appropriate subclass and parse it
    MP4::Mp4IsoBox* curbox = d->boxfactory.createInstance( this, fourcc, size, tell() );
    curbox->parsebox();
    d->boxes.append( curbox );
  }

  for( TagLib::List<MP4::Mp4IsoBox*>::Iterator iter  = d->boxes.begin();
                                               iter != d->boxes.end();
					       iter++ )
  {
    if( (*iter)->fourcc() == MP4::Fourcc("moov") )
    {
      d->isValid = true;
      break;
    }
  }
   
  //if( d->isValid )
    //debug( "file is valid" );
  //else
    //debug( "file is NOT valid" );

  // fill tags from proxy data
  fillTagFromProxy( d->tagsProxy, d->mp4tag );
}

MP4::File::~File()
{
  TagLib::List<Mp4IsoBox*>::Iterator delIter;
  for( delIter  = d->boxes.begin();
       delIter != d->boxes.end();
       delIter++ )
  {
    delete *delIter;
  }
  delete d;
}

Tag *MP4::File::tag() const
{
  return &d->mp4tag;
}

AudioProperties * MP4::File::audioProperties() const
{
  d->mp4audioproperties.setProxy( &d->propsProxy );
  return &d->mp4audioproperties;
}

bool MP4::File::save()
{
  return false;
}

void MP4::File::remove()
{
}

TagLib::uint MP4::File::readSystemsLen()
{
  TagLib::uint length = 0;
  TagLib::uint nbytes = 0;
  ByteVector   input;
  TagLib::uchar tmp_input;

  do
  {
    input = readBlock(1);
    tmp_input = static_cast<TagLib::uchar>(input[0]);
    nbytes++;
    length = (length<<7) | (tmp_input&0x7F);
  } while( (tmp_input&0x80) && (nbytes<4) );

  return length;
}

bool MP4::File::readSizeAndType( TagLib::uint& size, MP4::Fourcc& fourcc )
{
  // read the two blocks from file
  ByteVector readsize = readBlock(4);
  ByteVector readtype = readBlock(4);

  if( (readsize.size() != 4) || (readtype.size() != 4) )
    return false;

  // set size
  size = static_cast<unsigned char>(readsize[0]) << 24 | 
         static_cast<unsigned char>(readsize[1]) << 16 |
         static_cast<unsigned char>(readsize[2]) <<  8 |
         static_cast<unsigned char>(readsize[3]);

  // type and size seem to be part of the stored size
  if( size < 8 )
    return false;

  // set fourcc
  fourcc = readtype.data();

  return true;
}

bool MP4::File::readInt( TagLib::uint& toRead )
{
  ByteVector readbuffer = readBlock(4);
  if( readbuffer.size() != 4 )
    return false;

  toRead = static_cast<unsigned char>(readbuffer[0]) << 24 | 
           static_cast<unsigned char>(readbuffer[1]) << 16 |
           static_cast<unsigned char>(readbuffer[2]) <<  8 |
           static_cast<unsigned char>(readbuffer[3]);
  return true;
}

bool MP4::File::readShort( TagLib::uint& toRead )
{
  ByteVector readbuffer = readBlock(2);
  if( readbuffer.size() != 2 )
    return false;

  toRead = static_cast<unsigned char>(readbuffer[0]) <<  8 |
           static_cast<unsigned char>(readbuffer[1]);
  return true;
}

bool MP4::File::readLongLong( TagLib::ulonglong& toRead )
{
  ByteVector readbuffer = readBlock(8);
  if( readbuffer.size() != 8 )
    return false;

  toRead = static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[0])) << 56 | 
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[1])) << 48 |
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[2])) << 40 |
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[3])) << 32 |
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[4])) << 24 | 
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[5])) << 16 |
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[6])) <<  8 |
           static_cast<ulonglong>(static_cast<unsigned char>(readbuffer[7]));
  return true;
}

bool MP4::File::readFourcc( TagLib::MP4::Fourcc& fourcc )
{
  ByteVector readtype = readBlock(4);

  if( readtype.size() != 4)
    return false;

  // set fourcc
  fourcc = readtype.data();

  return true;
}

MP4::Mp4TagsProxy* MP4::File::tagProxy() const
{
  return &d->tagsProxy;
}

MP4::Mp4PropsProxy* MP4::File::propProxy() const
{
  return &d->propsProxy;
}

void fillTagFromProxy( MP4::Mp4TagsProxy& proxy, MP4::Tag& mp4tag )
{
  // tmp buffer for each tag
  MP4::ITunesDataBox* databox;
  
  databox = proxy.titleData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setTitle( datastring );
  }

  databox = proxy.artistData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setArtist( datastring );
  }

  databox = proxy.albumData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setAlbum( datastring );
  }

  databox = proxy.genreData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setGenre( datastring );
  }

  databox = proxy.yearData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setYear( datastring.toInt() );
  }

  databox = proxy.trknData();
  if( databox != 0 )
  {
    // convert data to uint
    TagLib::ByteVector datavec = databox->data();
    if( datavec.size() >= 4 )
    {
      TagLib::uint trackno = static_cast<TagLib::uint>( static_cast<unsigned char>(datavec[0]) << 24 |
	                                                static_cast<unsigned char>(datavec[1]) << 16 |
	                                                static_cast<unsigned char>(datavec[2]) <<  8 |
	                                                static_cast<unsigned char>(datavec[3]) );
      mp4tag.setTrack( trackno );
    }
    else
      mp4tag.setTrack( 0 );
  }

  databox = proxy.commentData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setComment( datastring );
  }

  databox = proxy.groupingData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setGrouping( datastring );
  }

  databox = proxy.composerData();
  if( databox != 0 )
  {
    // convert data to string
    TagLib::String datastring( databox->data(), String::UTF8 );
    // check if string was set
    if( !datastring.isEmpty() )
      mp4tag.setComposer( datastring );
  }

  databox = proxy.diskData();
  if( databox != 0 )
  {
    // convert data to uint
    TagLib::ByteVector datavec = databox->data();
    if( datavec.size() >= 4 )
    {
      TagLib::uint discno = static_cast<TagLib::uint>( static_cast<unsigned char>(datavec[0]) << 24 |
	                                               static_cast<unsigned char>(datavec[1]) << 16 |
	                                               static_cast<unsigned char>(datavec[2]) <<  8 |
	                                               static_cast<unsigned char>(datavec[3]) );
      mp4tag.setDisk( discno );
    }
    else
      mp4tag.setDisk( 0 );
  }

  databox = proxy.bpmData();
  if( databox != 0 )
  {
    // convert data to uint
    TagLib::ByteVector datavec = databox->data();

    if( datavec.size() >= 2 )
    {
      TagLib::uint bpm = static_cast<TagLib::uint>( static_cast<unsigned char>(datavec[0]) <<  8 |
	                                            static_cast<unsigned char>(datavec[1]) );
      mp4tag.setBpm( bpm );
    }
    else
      mp4tag.setBpm( 0 );
  }

  databox = proxy.coverData();
  if( databox != 0 )
  {
    // get byte vector
    mp4tag.setCover( databox->data() );
  }
}