summaryrefslogtreecommitdiffstats
path: root/src/metadata/m4a/mp4audiosampleentry.cpp
blob: fb87547e8c6467f321ac591535964f0d2f8e42d9 (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
/***************************************************************************
    copyright            : (C) 2002, 2003, 2006 by Jochen Issing
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *   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 <iostream>
#include "mp4audiosampleentry.h"
#include "mp4isobox.h"
#include "mp4file.h"
#include "mp4propsproxy.h"

using namespace TagLib;

class MP4::Mp4AudioSampleEntry::Mp4AudioSampleEntryPrivate
{
public:
  TagLib::uint channelcount;
  TagLib::uint samplerate;
  TagLib::uint bitrate;
};

MP4::Mp4AudioSampleEntry::Mp4AudioSampleEntry( TagLib::File* file, MP4::Fourcc fourcc, uint size, long offset )
	:Mp4SampleEntry(file, fourcc, size, offset)
{
  d = new MP4::Mp4AudioSampleEntry::Mp4AudioSampleEntryPrivate();
}

MP4::Mp4AudioSampleEntry::~Mp4AudioSampleEntry()
{
  delete d;
}

TagLib::uint MP4::Mp4AudioSampleEntry::channels() const
{
  return d->channelcount;
}

TagLib::uint MP4::Mp4AudioSampleEntry::samplerate() const
{
  return d->samplerate;
}

TagLib::uint MP4::Mp4AudioSampleEntry::bitrate() const
{
  return d->bitrate;
}

void MP4::Mp4AudioSampleEntry::parseEntry()
{
  TagLib::MP4::File* mp4file = dynamic_cast<TagLib::MP4::File*>(file());
  if(!mp4file)
    return;

  // read 8 reserved bytes
  mp4file->seek( 8, TagLib::File::Current );
  // read channelcount
  if(!mp4file->readShort( d->channelcount ))
    return;
  // seek over samplesize, pre_defined and reserved
  mp4file->seek( 6, TagLib::File::Current );
  // read samplerate
  if(!mp4file->readInt( d->samplerate ))
    return;

  // register box at proxy
  mp4file->propProxy()->registerAudioSampleEntry( this );


  //std::cout << "fourcc of audio sample entry: " << fourcc().toString() << std::endl;
  // check for both mp4a (plain files) and drms (encrypted files)
  if( (fourcc() == MP4::Fourcc("mp4a")) ||
      (fourcc() == MP4::Fourcc("drms"))  )
  {
    TagLib::MP4::Fourcc fourcc;
    TagLib::uint        esds_size;

    if (!mp4file->readSizeAndType( esds_size, fourcc ))
	return;

    // read esds' main parts
    if( size()-48 > 0 )
      ByteVector flags_version = mp4file->readBlock(4);
    else
      return;

    ByteVector EsDescrTag = mp4file->readBlock(1);
    // first 4 bytes contain full box specifics (version & flags)
    // upcoming byte must be ESDescrTag (0x03)
    if( EsDescrTag[0] == 0x03 )
    {
      TagLib::uint descr_len = mp4file->readSystemsLen();
      TagLib::uint EsId;
      if( !mp4file->readShort( EsId ) )
	return;
      ByteVector priority = mp4file->readBlock(1);
      if( descr_len < 20 )
	return;
    }
    else
    {
      TagLib::uint EsId;
      if( !mp4file->readShort( EsId ) )
	return;
    }
    // read decoder configuration tag (0x04)
    ByteVector DecCfgTag = mp4file->readBlock(1);
    if( DecCfgTag[0] != 0x04 )
      return;
    // read decoder configuration length
    // TagLib::uint deccfg_len = mp4file->readSystemsLen();
    // read object type Id
    ByteVector objId = mp4file->readBlock(1);
    // read stream type id
    ByteVector strId = mp4file->readBlock(1);
    // read buffer Size DB
    ByteVector bufferSizeDB = mp4file->readBlock(3);
    // read max bitrate
    TagLib::uint max_bitrate;
    if( !mp4file->readInt( max_bitrate ) )
      return;
    // read average bitrate
    if( !mp4file->readInt( d->bitrate ) )
      return;
    // skip the rest
    mp4file->seek( offset()+size()-8, File::Beginning );
  }
  else
    mp4file->seek( size()-36, File::Current );
}