Update to 6762
[qball-mpd.git] / src / mp4ff / .svn / text-base / mp4util.c.svn-base
blob1a77c97ae790e13e556460e2cd425ffda67cba59
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: mp4util.c,v 1.15 2004/01/11 15:52:19 menno Exp $
26 **/
28 #include "mp4ffint.h"
29 #include <stdlib.h>
31 int32_t mp4ff_read_data(mp4ff_t *f, int8_t *data, uint32_t size)
33     int32_t result = 1;
35     result = f->stream->read(f->stream->user_data, data, size);
37     f->current_position += size;
39     return result;
42 int32_t mp4ff_truncate(mp4ff_t * f)
44         return f->stream->truncate(f->stream->user_data);
47 int32_t mp4ff_write_data(mp4ff_t *f, int8_t *data, uint32_t size)
49     int32_t result = 1;
51     result = f->stream->write(f->stream->user_data, data, size);
53     f->current_position += size;
55     return result;
58 int32_t mp4ff_write_int32(mp4ff_t *f,const uint32_t data)
60         uint32_t result;
61     uint32_t a, b, c, d;
62     int8_t temp[4];
63     
64     *(uint32_t*)temp = data;
65     a = (uint8_t)temp[0];
66     b = (uint8_t)temp[1];
67     c = (uint8_t)temp[2];
68     d = (uint8_t)temp[3];
70     result = (a<<24) | (b<<16) | (c<<8) | d;
72     return mp4ff_write_data(f,(uint8_t*)&result,sizeof(result));
75 int32_t mp4ff_set_position(mp4ff_t *f, const int64_t position)
77     f->stream->seek(f->stream->user_data, position);
78     f->current_position = position;
80     return 0;
83 int64_t mp4ff_position(const mp4ff_t *f)
85     return f->current_position;
88 uint64_t mp4ff_read_int64(mp4ff_t *f)
90     uint8_t data[8];
91     uint64_t result = 0;
92     int8_t i;
94     mp4ff_read_data(f, data, 8);
96     for (i = 0; i < 8; i++)
97     {
98         result |= ((uint64_t)data[i]) << ((7 - i) * 8);
99     }
101     return result;
104 uint32_t mp4ff_read_int32(mp4ff_t *f)
106     uint32_t result;
107     uint32_t a, b, c, d;
108     int8_t data[4];
109     
110     mp4ff_read_data(f, data, 4);
111     a = (uint8_t)data[0];
112     b = (uint8_t)data[1];
113     c = (uint8_t)data[2];
114     d = (uint8_t)data[3];
116     result = (a<<24) | (b<<16) | (c<<8) | d;
117     return (uint32_t)result;
120 uint32_t mp4ff_read_int24(mp4ff_t *f)
122     uint32_t result;
123     uint32_t a, b, c;
124     int8_t data[4];
125     
126     mp4ff_read_data(f, data, 3);
127     a = (uint8_t)data[0];
128     b = (uint8_t)data[1];
129     c = (uint8_t)data[2];
131     result = (a<<16) | (b<<8) | c;
132     return (uint32_t)result;
135 uint16_t mp4ff_read_int16(mp4ff_t *f)
137     uint32_t result;
138     uint32_t a, b;
139     int8_t data[2];
140     
141     mp4ff_read_data(f, data, 2);
142     a = (uint8_t)data[0];
143     b = (uint8_t)data[1];
145     result = (a<<8) | b;
146     return (uint16_t)result;
149 char * mp4ff_read_string(mp4ff_t * f,uint32_t length)
151         char * str = (char*)malloc(length + 1);
152         if (str!=0)
153         {
154                 if ((uint32_t)mp4ff_read_data(f,str,length)!=length)
155                 {
156                         free(str);
157                         str = 0;
158                 }
159                 else
160                 {
161                         str[length] = 0;
162                 }
163         }
164         return str;     
167 uint8_t mp4ff_read_char(mp4ff_t *f)
169     uint8_t output;
170     mp4ff_read_data(f, &output, 1);
171     return output;
174 uint32_t mp4ff_read_mp4_descr_length(mp4ff_t *f)
176     uint8_t b;
177     uint8_t numBytes = 0;
178     uint32_t length = 0;
180     do
181     {
182         b = mp4ff_read_char(f);
183         numBytes++;
184         length = (length << 7) | (b & 0x7F);
185     } while ((b & 0x80) && numBytes < 4);
187     return length;