~ubuntu-branches/ubuntu/maverick/blender/maverick

« back to all changes in this revision

Viewing changes to intern/SoundSystem/intern/SND_Utils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Khashayar Naderehvandi, Khashayar Naderehvandi, Alessio Treglia
  • Date: 2009-01-22 16:53:59 UTC
  • mfrom: (14.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090122165359-v0996tn7fbit64ni
Tags: 2.48a+dfsg-1ubuntu1
[ Khashayar Naderehvandi ]
* Merge from debian experimental (LP: #320045), Ubuntu remaining changes:
  - Add patch correcting header file locations.
  - Add libvorbis-dev and libgsm1-dev to Build-Depends.
  - Use avcodec_decode_audio2() in source/blender/src/hddaudio.c

[ Alessio Treglia ]
* Add missing previous changelog entries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Util functions for soundthingies
5
5
 *
6
 
 * $Id: SND_Utils.cpp 14444 2008-04-16 22:40:48Z hos $
 
6
 * $Id: SND_Utils.cpp 17039 2008-10-12 10:39:45Z campbellbarton $
7
7
 *
8
8
 * ***** BEGIN GPL LICENSE BLOCK *****
9
9
 *
34
34
#include "SND_Utils.h"
35
35
#include "SoundDefines.h"
36
36
#include "SND_DependKludge.h"
37
 
/*
38
 
extern "C" { 
39
 
#include "license_key.h"
40
 
}
41
 
*/
42
37
#include <stdio.h>
43
38
#include <stdlib.h>
44
39
#include <fcntl.h>
204
199
 
205
200
 
206
201
/* checks if the passed pointer is a valid sample */
207
 
bool CheckSample(void* sample)
 
202
static bool CheckSample(void* sample)
208
203
{
209
204
        bool valid = false;
210
205
        char buffer[32];
290
285
 
291
286
 
292
287
/* gets the length of the actual sample data (without the header) */
293
 
unsigned int SND_GetNumberOfSamples(void* sample)
 
288
unsigned int SND_GetNumberOfSamples(void* sample, int sample_length)
294
289
{
295
 
        unsigned int chunklength, length = 0, offset = 16;
296
 
        char data[4];
297
 
        
 
290
        unsigned int chunklength, length = 0, offset;
 
291
        unsigned short block_align;
298
292
        if (CheckSample(sample))
299
293
        {
300
 
                memcpy(&chunklength, ((char*)sample) + offset, 4);
 
294
                memcpy(&chunklength, ((char*)sample) + 16, 4);
 
295
                memcpy(&block_align, ((char*)sample) + 32, 2); /* always 2 or 4 it seems */
 
296
                
301
297
                /* This was endian unsafe. See top of the file for the define. */
302
 
                if (SND_fEndian == SND_endianBig) SWITCH_INT(chunklength);
303
 
 
304
 
                offset = offset + chunklength + 4;
305
 
                memcpy(data, ((char*)sample) + offset, 4);
 
298
                if (SND_fEndian == SND_endianBig)
 
299
                {
 
300
                        SWITCH_INT(chunklength);
 
301
                        SWITCH_SHORT(block_align);
 
302
                }
 
303
                                
 
304
                offset = 16 + chunklength + 4;
306
305
 
307
306
                /* This seems very unsafe, what if data is never found (f.i. corrupt file)... */
308
307
                // lets find "data"
309
 
                while (memcmp(data, "data", 4))
 
308
                while (memcmp(((char*)sample) + offset, "data", 4))
310
309
                {
311
 
                        offset += 4;
312
 
                        memcpy(data, ((char*)sample) + offset, 4);
 
310
                        offset += block_align;
 
311
                        
 
312
                        if (offset+block_align > sample_length) /* save us from crashing */
 
313
                                return 0;
313
314
                }
314
315
                offset += 4;
315
316
                memcpy(&length, ((char*)sample) + offset, 4);
324
325
 
325
326
 
326
327
/* gets the size of the entire header (file - sampledata) */
327
 
unsigned int SND_GetHeaderSize(void* sample)
 
328
unsigned int SND_GetHeaderSize(void* sample, int sample_length)
328
329
{
329
330
        unsigned int chunklength, headersize = 0, offset = 16;
330
 
        char data[4];
331
 
        
 
331
        unsigned short block_align;
332
332
        if (CheckSample(sample))
333
333
        {
334
334
                memcpy(&chunklength, ((char*)sample) + offset, 4);
 
335
                memcpy(&block_align, ((char*)sample) + 32, 2); /* always 2 or 4 it seems */
 
336
                
335
337
                /* This was endian unsafe. See top of the file for the define. */
336
 
                if (SND_fEndian == SND_endianBig) SWITCH_INT(chunklength);
 
338
                if (SND_fEndian == SND_endianBig)
 
339
                {
 
340
                        SWITCH_INT(chunklength);
 
341
                        SWITCH_SHORT(block_align);
 
342
                }
337
343
                offset = offset + chunklength + 4;
338
 
                memcpy(data, ((char*)sample) + offset, 4);
339
344
 
340
345
                // lets find "data"
341
 
                while (memcmp(data, "data", 4))
 
346
                while (memcmp(((char*)sample) + offset, "data", 4))
342
347
                {
343
 
                        offset += 4;
344
 
                        memcpy(data, ((char*)sample) + offset, 4);
 
348
                        offset += block_align;
 
349
                        
 
350
                        if (offset+block_align > sample_length) /* save us from crashing */
 
351
                                return 0;
345
352
                }
346
353
                headersize = offset + 8;
347
354
        }
348
355
 
349
 
 
350
356
        return headersize;
351
357
}
352
358
 
353
359
 
354
 
 
355
360
unsigned int SND_GetExtraChunk(void* sample)
356
361
{
357
362
        unsigned int extrachunk = 0, chunklength, offset = 16;
387
392
        if (CheckSample(sample))
388
393
        {
389
394
                memcpy(&fileheader, sample, sizeof(WavFileHeader));
390
 
                fileheader.size = SND_GetHeaderSize(sample);
391
 
                sample += sizeof(WavFileHeader);
392
 
                fileheader.size = ((fileheader.size+1) & ~1) - 4;
 
395
                fileheader.size = SND_GetHeaderSize(sample, waveslot->GetFileSize());
 
396
                if (fileheader.size) { /* this may fail for corrupt files */
 
397
                        sample += sizeof(WavFileHeader);
 
398
                        fileheader.size = ((fileheader.size+1) & ~1) - 4;
393
399
 
394
 
                while ((fileheader.size > 0) && (memcpy(&chunkheader, sample, sizeof(WavChunkHeader))))
395
 
                {
396
 
                        sample += sizeof(WavChunkHeader);
397
 
                        if (!memcmp(chunkheader.id, "fmt ", 4))
 
400
                        while ((fileheader.size > 0) && (memcpy(&chunkheader, sample, sizeof(WavChunkHeader))))
398
401
                        {
399
 
                                memcpy(&fmtheader, sample, sizeof(WavFmtHeader));
400
 
                                waveslot->SetSampleFormat(fmtheader.format);
 
402
                                sample += sizeof(WavChunkHeader);
 
403
                                if (!memcmp(chunkheader.id, "fmt ", 4))
 
404
                                {
 
405
                                        memcpy(&fmtheader, sample, sizeof(WavFmtHeader));
 
406
                                        waveslot->SetSampleFormat(fmtheader.format);
401
407
 
402
 
                                if (fmtheader.format == 0x0001)
403
 
                                {
404
 
                                        waveslot->SetNumberOfChannels(fmtheader.numberofchannels);
405
 
                                        waveslot->SetBitRate(fmtheader.bitrate);
406
 
                                        waveslot->SetSampleRate(fmtheader.samplerate);
 
408
                                        if (fmtheader.format == 0x0001)
 
409
                                        {
 
410
                                                waveslot->SetNumberOfChannels(fmtheader.numberofchannels);
 
411
                                                waveslot->SetBitRate(fmtheader.bitrate);
 
412
                                                waveslot->SetSampleRate(fmtheader.samplerate);
 
413
                                                sample += chunkheader.size;
 
414
                                        } 
 
415
                                        else
 
416
                                        {
 
417
                                                memcpy(&fmtexheader, sample, sizeof(WavFmtExHeader));
 
418
                                                sample += chunkheader.size;
 
419
                                        }
 
420
                                }
 
421
                                else if (!memcmp(chunkheader.id, "data", 4))
 
422
                                {
 
423
                                        if (fmtheader.format == 0x0001)
 
424
                                        {
 
425
                                                waveslot->SetNumberOfSamples(chunkheader.size);
 
426
                                                sample += chunkheader.size;
 
427
                                        }
 
428
                                        else if (fmtheader.format == 0x0011)
 
429
                                        {
 
430
                                                //IMA ADPCM
 
431
                                        }
 
432
                                        else if (fmtheader.format == 0x0055)
 
433
                                        {
 
434
                                                //MP3 WAVE
 
435
                                        }
 
436
                                }
 
437
                                else if (!memcmp(chunkheader.id, "smpl", 4))
 
438
                                {
 
439
                                        memcpy(&sampleheader, sample, sizeof(WavSampleHeader));
 
440
                                        //loop = sampleheader.loops;
407
441
                                        sample += chunkheader.size;
408
 
                                } 
 
442
                                }
409
443
                                else
410
 
                                {
411
 
                                        memcpy(&fmtexheader, sample, sizeof(WavFmtExHeader));
412
 
                                        sample += chunkheader.size;
413
 
                                }
414
 
                        }
415
 
                        else if (!memcmp(chunkheader.id, "data", 4))
416
 
                        {
417
 
                                if (fmtheader.format == 0x0001)
418
 
                                {
419
 
                                        waveslot->SetNumberOfSamples(chunkheader.size);
420
 
                                        sample += chunkheader.size;
421
 
                                }
422
 
                                else if (fmtheader.format == 0x0011)
423
 
                                {
424
 
                                        //IMA ADPCM
425
 
                                }
426
 
                                else if (fmtheader.format == 0x0055)
427
 
                                {
428
 
                                        //MP3 WAVE
429
 
                                }
430
 
                        }
431
 
                        else if (!memcmp(chunkheader.id, "smpl", 4))
432
 
                        {
433
 
                                memcpy(&sampleheader, sample, sizeof(WavSampleHeader));
434
 
                                //loop = sampleheader.loops;
435
 
                                sample += chunkheader.size;
436
 
                        }
437
 
                        else
438
 
                                sample += chunkheader.size;
 
444
                                        sample += chunkheader.size;
439
445
 
440
 
                        sample += chunkheader.size & 1;
441
 
                        fileheader.size -= (((chunkheader.size + 1) & ~1) + 8);
 
446
                                sample += chunkheader.size & 1;
 
447
                                fileheader.size -= (((chunkheader.size + 1) & ~1) + 8);
 
448
                        }
442
449
                }
443
450
        }
444
451
}