~ubuntu-branches/ubuntu/trusty/libavg/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/0002-libav-0.7.patch/src/video/FFMpegDecoder.cpp

  • Committer: Package Import Robot
  • Author(s): OXullo Intersecans
  • Date: 2011-12-06 22:44:56 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20111206224456-qc7250z3ya1vi8s9
Tags: 1.7.0-0ubuntu1
* New upstream release (LP: #899183)
* Remove patches 0002-libav-0.7.patch, 0003-fglrx-segfault-on-startup.patch
  now merged to upstream
* Remove unnecessary .la files
* Update debian/watch file
* Fix debian/copyright dep-5 compliancy
* Update standards to version 3.9.2
* Add man pages for avg_checktouch, avg_checkvsync, avg_showsvg
* Minor debian/rules enhancement
* Add librsvg2-dev, libgdk-pixbuf2.0-dev to Build-Depends
* Proper transition to dh_python2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//  libavg - Media Playback Engine. 
3
 
//  Copyright (C) 2003-2008 Ulrich von Zadow
4
 
//
5
 
//  This library is free software; you can redistribute it and/or
6
 
//  modify it under the terms of the GNU Lesser General Public
7
 
//  License as published by the Free Software Foundation; either
8
 
//  version 2 of the License, or (at your option) any later version.
9
 
//
10
 
//  This library 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 GNU
13
 
//  Lesser General Public License for more details.
14
 
//
15
 
//  You should have received a copy of the GNU Lesser General Public
16
 
//  License along with this library; if not, write to the Free Software
17
 
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
//
19
 
//  Current versions can be found at www.libavg.de
20
 
//
21
 
 
22
 
#include "FFMpegDecoder.h"
23
 
#include "AsyncDemuxer.h"
24
 
#include "FFMpegDemuxer.h"
25
 
 
26
 
#include "../base/Exception.h"
27
 
#include "../base/Logger.h"
28
 
#include "../base/ScopeTimer.h"
29
 
#include "../base/ObjectCounter.h"
30
 
 
31
 
#include "../graphics/Filterflipuv.h"
32
 
#include "../graphics/Filterfliprgba.h"
33
 
 
34
 
#include <iostream>
35
 
#include <sstream>
36
 
#ifndef _WIN32
37
 
#include <unistd.h>
38
 
#endif
39
 
#include <errno.h>
40
 
 
41
 
using namespace std;
42
 
using namespace boost;
43
 
 
44
 
#define SAMPLE_BUFFER_SIZE ((AVCODEC_MAX_AUDIO_FRAME_SIZE*3))
45
 
#define VOLUME_FADE_SAMPLES 100
46
 
 
47
 
#if LIBAVFORMAT_BUILD < ((50<<16)+(0<<8)+0)
48
 
#define PIX_FMT_BGRA PIX_FMT_RGBA32
49
 
#define PIX_FMT_YUYV422 PIX_FMT_YUV422
50
 
#endif
51
 
 
52
 
namespace avg {
53
 
 
54
 
bool FFMpegDecoder::s_bInitialized = false;
55
 
mutex FFMpegDecoder::s_OpenMutex;
56
 
 
57
 
FFMpegDecoder::FFMpegDecoder()
58
 
    : m_State(CLOSED),
59
 
      m_pFormatContext(0),
60
 
      m_PF(NO_PIXELFORMAT),
61
 
      m_pSwsContext(0),
62
 
      m_Size(0,0),
63
 
      m_bUseStreamFPS(true),
64
 
      m_AStreamIndex(-1),
65
 
      m_pSampleBuffer(0),
66
 
      m_pResampleBuffer(0),
67
 
      m_pAudioResampleContext(0),
68
 
      m_Volume(1.0),
69
 
      m_LastVolume(1.0),
70
 
      m_pDemuxer(0),
71
 
      m_pVStream(0),
72
 
      m_pAStream(0),
73
 
      m_VStreamIndex(-1),
74
 
      m_bFirstPacket(false),
75
 
      m_VideoStartTimestamp(-1),
76
 
      m_LastVideoFrameTime(-1),
77
 
      m_FPS(0)
78
 
{
79
 
    ObjectCounter::get()->incRef(&typeid(*this));
80
 
    initVideoSupport();
81
 
}
82
 
 
83
 
FFMpegDecoder::~FFMpegDecoder()
84
 
{
85
 
    if (m_pFormatContext) {
86
 
        close();
87
 
    }
88
 
    ObjectCounter::get()->decRef(&typeid(*this));
89
 
}
90
 
 
91
 
 
92
 
void avcodecError(const string& sFilename, int err)
93
 
{
94
 
    switch(err) {
95
 
        case AVERROR_NUMEXPECTED:
96
 
            throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
97
 
                    sFilename + ": Incorrect image filename syntax (use %%d to specify the image number:");
98
 
        case AVERROR_INVALIDDATA:
99
 
            throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
100
 
                    sFilename + ": Error while parsing header");
101
 
        case AVERROR_NOFMT:
102
 
            throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
103
 
                    sFilename + ": Unknown format");
104
 
        default:
105
 
            stringstream s;
106
 
            s << "'" << sFilename <<  "': Error while opening file (Num:" << err << ")";
107
 
            throw Exception(AVG_ERR_VIDEO_INIT_FAILED, s.str());
108
 
    }
109
 
}
110
 
 
111
 
 
112
 
void dump_stream_info(AVFormatContext *s)
113
 
{
114
 
    if (s->track != 0)
115
 
        fprintf(stderr, "  Track: %d\n", s->track);
116
 
    if (s->title[0] != '\0')
117
 
        fprintf(stderr, "  Title: %s\n", s->title);
118
 
    if (s->author[0] != '\0')
119
 
        fprintf(stderr, "  Author: %s\n", s->author);
120
 
    if (s->album[0] != '\0')
121
 
        fprintf(stderr, "  Album: %s\n", s->album);
122
 
    if (s->year != 0)
123
 
        fprintf(stderr, "  Year: %d\n", s->year);
124
 
    if (s->genre[0] != '\0')
125
 
        fprintf(stderr, "  Genre: %s\n", s->genre);
126
 
}
127
 
 
128
 
int openCodec(AVFormatContext* pFormatContext, int streamIndex)
129
 
{
130
 
    AVCodecContext *enc;
131
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
132
 
    enc = &(pFormatContext->streams[streamIndex]->codec);
133
 
#else
134
 
    enc = pFormatContext->streams[streamIndex]->codec;
135
 
#endif
136
 
//    enc->debug = 0x0001; // see avcodec.h
137
 
 
138
 
    AVCodec * codec = avcodec_find_decoder(enc->codec_id);
139
 
    if (!codec || avcodec_open(enc, codec) < 0) {
140
 
        return -1;
141
 
    }
142
 
    return 0;
143
 
}
144
 
 
145
 
void FFMpegDecoder::open(const string& sFilename, bool bThreadedDemuxer)
146
 
{
147
 
    mutex::scoped_lock lock(s_OpenMutex);
148
 
    m_bThreadedDemuxer = bThreadedDemuxer;
149
 
    m_bAudioEOF = false;
150
 
    m_bVideoEOF = false;
151
 
    m_bEOFPending = false;
152
 
    m_VideoStartTimestamp = -1;
153
 
    AVFormatParameters params;
154
 
    int err;
155
 
    m_sFilename = sFilename;
156
 
 
157
 
    AVG_TRACE(Logger::MEMORY, "Opening " << sFilename);
158
 
    memset(&params, 0, sizeof(params));
159
 
 
160
 
    err = av_open_input_file(&m_pFormatContext, sFilename.c_str(), 
161
 
            0, 0, &params);
162
 
    if (err < 0) {
163
 
        m_sFilename = "";
164
 
        avcodecError(sFilename, err);
165
 
    }
166
 
    
167
 
    err = av_find_stream_info(m_pFormatContext);
168
 
    if (err < 0) {
169
 
        throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
170
 
                sFilename + ": Could not find codec parameters.");
171
 
    }
172
 
//    dump_format(m_pFormatContext, 0, sFilename.c_str(), 0);
173
 
 
174
 
    av_read_play(m_pFormatContext);
175
 
    
176
 
    // Find audio and video streams in the file
177
 
    m_VStreamIndex = -1;
178
 
    m_AStreamIndex = -1;
179
 
    for (unsigned i = 0; i < m_pFormatContext->nb_streams; i++) {
180
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
181
 
        AVCodecContext *enc = &m_pFormatContext->streams[i]->codec;
182
 
#else         
183
 
        AVCodecContext *enc = m_pFormatContext->streams[i]->codec;
184
 
#endif
185
 
        switch (enc->codec_type) {
186
 
            case CODEC_TYPE_VIDEO:
187
 
                if (m_VStreamIndex < 0) {
188
 
                    m_VStreamIndex = i;
189
 
                }
190
 
                break;
191
 
            case CODEC_TYPE_AUDIO:
192
 
                // Ignore the audio stream if we're using sync demuxing. 
193
 
                if (m_AStreamIndex < 0 && bThreadedDemuxer) {
194
 
                    m_AStreamIndex = i;
195
 
                }
196
 
                break;
197
 
            default:
198
 
                break;
199
 
        }
200
 
    }
201
 
//    dump_format(m_pFormatContext, 0, m_sFilename.c_str(), 0);
202
 
//    dump_stream_info(m_pFormatContext);
203
 
    
204
 
    // Enable video stream demuxing
205
 
    if (m_VStreamIndex >= 0) {
206
 
        m_pVStream = m_pFormatContext->streams[m_VStreamIndex];
207
 
        m_State = OPENED;
208
 
        
209
 
        // Set video parameters
210
 
        m_TimeUnitsPerSecond = 1.0/av_q2d(m_pVStream->time_base);
211
 
        if (m_bUseStreamFPS) {
212
 
            m_FPS = getNominalFPS();
213
 
        }
214
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
215
 
        m_Size = IntPoint(m_pVStream->codec.width, m_pVStream->codec.height);
216
 
#else
217
 
        m_Size = IntPoint(m_pVStream->codec->width, m_pVStream->codec->height);
218
 
#endif
219
 
        m_bFirstPacket = true;
220
 
        m_sFilename = sFilename;
221
 
        m_LastVideoFrameTime = -1;
222
 
    
223
 
        int rc = openCodec(m_pFormatContext, m_VStreamIndex);
224
 
        if (rc == -1) {
225
 
            m_VStreamIndex = -1;
226
 
            char szBuf[256];
227
 
            avcodec_string(szBuf, sizeof(szBuf), m_pVStream->codec, 0);
228
 
            throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
229
 
                    sFilename + ": unsupported codec ("+szBuf+").");
230
 
        }
231
 
        m_PF = calcPixelFormat(true);
232
 
    }
233
 
    // Enable audio stream demuxing.
234
 
    if (m_AStreamIndex >= 0) {
235
 
        m_pAStream = m_pFormatContext->streams[m_AStreamIndex];
236
 
        
237
 
        m_AudioPacket = 0;
238
 
        m_AudioPacketData = 0;
239
 
        m_AudioPacketSize = 0;
240
 
        
241
 
        m_LastAudioFrameTime = 0;
242
 
        m_AudioStartTimestamp = 0;
243
 
        
244
 
        if (m_pAStream->start_time != AV_NOPTS_VALUE) {
245
 
            m_AudioStartTimestamp = double(av_q2d(m_pAStream->time_base))
246
 
                    *m_pAStream->start_time;
247
 
        }
248
 
        m_EffectiveSampleRate = (int)(m_pAStream->codec->sample_rate);
249
 
        int rc = openCodec(m_pFormatContext, m_AStreamIndex);
250
 
        if (rc == -1) {
251
 
            m_AStreamIndex = -1;
252
 
            char szBuf[256];
253
 
            avcodec_string(szBuf, sizeof(szBuf), m_pAStream->codec, 0);
254
 
            m_pAStream = 0; 
255
 
            AVG_TRACE(Logger::WARNING, 
256
 
                    sFilename + ": unsupported codec ("+szBuf+"). Disabling audio.");
257
 
        }
258
 
    }
259
 
 
260
 
    m_State = OPENED;
261
 
}
262
 
 
263
 
void FFMpegDecoder::startDecoding(bool bDeliverYCbCr, const AudioParams* pAP)
264
 
{
265
 
    AVG_ASSERT(m_State == OPENED);
266
 
    if (m_VStreamIndex >= 0) {
267
 
        m_PF = calcPixelFormat(bDeliverYCbCr);
268
 
    }
269
 
    bool bAudioEnabled = (pAP && m_bThreadedDemuxer);
270
 
    if (bAudioEnabled) {
271
 
        m_AP = *pAP;
272
 
    } else {
273
 
        m_AStreamIndex = -1;
274
 
        if (m_pAStream) {
275
 
            avcodec_close(m_pAStream->codec);
276
 
        }
277
 
        m_pAStream = 0;
278
 
    }
279
 
 
280
 
    if (m_AStreamIndex >= 0) {
281
 
        if (m_pAStream->codec->channels > m_AP.m_Channels) {
282
 
            AVG_TRACE(Logger::WARNING, 
283
 
                    m_sFilename << ": unsupported number of channels (" << 
284
 
                            m_pAStream->codec->channels << "). Disabling audio.");
285
 
            m_AStreamIndex = -1;
286
 
            m_pAStream = 0; 
287
 
        } else {
288
 
            m_pSampleBuffer = (char*)av_mallocz(SAMPLE_BUFFER_SIZE);
289
 
            m_SampleBufferStart = 0;
290
 
            m_SampleBufferEnd = 0;
291
 
            m_SampleBufferLeft = SAMPLE_BUFFER_SIZE;
292
 
 
293
 
            m_ResampleBufferSize = 0;
294
 
            m_pResampleBuffer = 0;
295
 
            m_ResampleBufferStart = 0;
296
 
            m_ResampleBufferEnd = 0;
297
 
        }
298
 
    }
299
 
 
300
 
    if (m_VStreamIndex < 0 && m_AStreamIndex < 0) {
301
 
        throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
302
 
                m_sFilename + " does not contain any valid audio or video streams.");
303
 
    }
304
 
 
305
 
    // Create demuxer
306
 
    AVG_ASSERT(!m_pDemuxer);
307
 
    vector<int> streamIndexes;
308
 
    if (m_VStreamIndex >= 0) {
309
 
        streamIndexes.push_back(m_VStreamIndex);
310
 
    }
311
 
    if (m_AStreamIndex >= 0) {
312
 
        streamIndexes.push_back(m_AStreamIndex);
313
 
    }
314
 
    if (m_bThreadedDemuxer) {
315
 
        m_pDemuxer = new AsyncDemuxer(m_pFormatContext, streamIndexes);
316
 
    } else {
317
 
        m_pDemuxer = new FFMpegDemuxer(m_pFormatContext, streamIndexes);
318
 
    }
319
 
    
320
 
    m_State = DECODING;
321
 
}
322
 
 
323
 
void FFMpegDecoder::close() 
324
 
{
325
 
    mutex::scoped_lock lock(s_OpenMutex);
326
 
    mutex::scoped_lock lock2(m_AudioMutex);
327
 
    AVG_TRACE(Logger::MEMORY, "Closing " << m_sFilename);
328
 
    
329
 
    delete m_pDemuxer;
330
 
    m_pDemuxer = 0;
331
 
    
332
 
    // Close audio and video codecs
333
 
    if (m_pVStream) {
334
 
        avcodec_close(m_pVStream->codec);
335
 
        m_pVStream = 0;
336
 
        m_VStreamIndex = -1;
337
 
    }
338
 
 
339
 
    if (m_pAStream) {
340
 
        avcodec_close(m_pAStream->codec);
341
 
        if (m_AudioPacket) {
342
 
            av_free_packet(m_AudioPacket);
343
 
            delete m_AudioPacket;
344
 
            m_AudioPacket = 0;
345
 
        }
346
 
        if (m_pAudioResampleContext) {
347
 
            audio_resample_close(m_pAudioResampleContext);
348
 
            m_pAudioResampleContext = 0;
349
 
        }
350
 
        
351
 
        if (m_pSampleBuffer) {
352
 
            av_free(m_pSampleBuffer);
353
 
            m_pSampleBuffer = 0;
354
 
        }
355
 
        if (m_pResampleBuffer) {
356
 
            av_free(m_pResampleBuffer);
357
 
            m_pResampleBuffer = 0;
358
 
        }
359
 
 
360
 
        m_AudioPacketData = 0;
361
 
        m_AudioPacketSize = 0;
362
 
        
363
 
        m_SampleBufferStart = 0;
364
 
        m_SampleBufferEnd = 0;
365
 
        m_SampleBufferLeft = 0;
366
 
 
367
 
        m_ResampleBufferStart = 0;
368
 
        m_ResampleBufferEnd = 0;
369
 
        m_ResampleBufferSize = 0;
370
 
        
371
 
        m_LastAudioFrameTime = 0;
372
 
        m_AudioStartTimestamp = 0;
373
 
        
374
 
        m_pAStream = 0;
375
 
        m_AStreamIndex = -1;
376
 
    }
377
 
    if (m_pFormatContext) {
378
 
        av_close_input_file(m_pFormatContext);
379
 
        m_pFormatContext = 0;
380
 
    }
381
 
    
382
 
    if (m_pSwsContext) {
383
 
        sws_freeContext(m_pSwsContext);
384
 
        m_pSwsContext = 0;
385
 
    }
386
 
    m_State = CLOSED;
387
 
}
388
 
 
389
 
IVideoDecoder::DecoderState FFMpegDecoder::getState() const
390
 
{
391
 
    return m_State;
392
 
}
393
 
 
394
 
VideoInfo FFMpegDecoder::getVideoInfo() const
395
 
{
396
 
    AVG_ASSERT(m_State != CLOSED);
397
 
    double duration = 0;
398
 
    if (m_pVStream || m_pAStream) {
399
 
        duration = getDuration();
400
 
    }
401
 
    VideoInfo info(duration, m_pFormatContext->bit_rate, m_pVStream != 0,
402
 
            m_pAStream != 0);
403
 
    if (m_pVStream) {
404
 
        info.setVideoData(m_Size, getStreamPF(), getNumFrames(), getNominalFPS(), m_FPS,
405
 
                m_pVStream->codec->codec->name);
406
 
    }
407
 
    if (m_pAStream) {
408
 
        AVCodecContext * pACodec = m_pAStream->codec;
409
 
        info.setAudioData(pACodec->codec->name, pACodec->sample_rate,
410
 
                pACodec->channels);
411
 
    }
412
 
    return info;
413
 
}
414
 
 
415
 
void FFMpegDecoder::seek(double destTime) 
416
 
{
417
 
    AVG_ASSERT(m_State == DECODING);
418
 
    if (m_bFirstPacket && m_pVStream) {
419
 
        AVFrame frame;
420
 
        readFrame(frame);
421
 
    }
422
 
    m_pDemuxer->seek(destTime + getStartTime());
423
 
    if (m_pVStream) {
424
 
        m_LastVideoFrameTime = destTime - 1.0/m_FPS;
425
 
    }
426
 
    if (m_pAStream) {
427
 
        mutex::scoped_lock lock(m_AudioMutex);
428
 
        m_LastAudioFrameTime = destTime;
429
 
        m_SampleBufferStart = m_SampleBufferEnd = 0;
430
 
        m_SampleBufferLeft = SAMPLE_BUFFER_SIZE;
431
 
        m_ResampleBufferStart = m_ResampleBufferEnd = 0;
432
 
        m_AudioPacketSize = 0;
433
 
    }
434
 
    m_bVideoEOF = false;
435
 
    m_bAudioEOF = false;
436
 
}
437
 
 
438
 
void FFMpegDecoder::loop()
439
 
{
440
 
    seek(0);
441
 
}
442
 
 
443
 
IntPoint FFMpegDecoder::getSize() const
444
 
{
445
 
    AVG_ASSERT(m_State != CLOSED);
446
 
    return m_Size;
447
 
}
448
 
 
449
 
int FFMpegDecoder::getCurFrame() const
450
 
{
451
 
    AVG_ASSERT(m_State != CLOSED);
452
 
    return int(m_LastVideoFrameTime*getNominalFPS()+0.5);
453
 
}
454
 
 
455
 
int FFMpegDecoder::getNumFramesQueued() const
456
 
{
457
 
    return 0;
458
 
}
459
 
 
460
 
double FFMpegDecoder::getCurTime(StreamSelect stream) const
461
 
{
462
 
    AVG_ASSERT(m_State != CLOSED);
463
 
    switch(stream) {
464
 
        case SS_DEFAULT:
465
 
        case SS_VIDEO:
466
 
            AVG_ASSERT(m_pVStream);
467
 
            return m_LastVideoFrameTime;
468
 
        case SS_AUDIO:
469
 
            AVG_ASSERT(m_pAStream);
470
 
            return m_LastAudioFrameTime;
471
 
        default:
472
 
            return -1;
473
 
    }
474
 
}
475
 
 
476
 
double FFMpegDecoder::getDuration() const
477
 
{
478
 
    AVG_ASSERT(m_State != CLOSED);
479
 
    long long duration;
480
 
    AVRational time_base;
481
 
    if (m_pVStream) {
482
 
        duration=m_pVStream->duration;
483
 
        time_base=m_pVStream->time_base;
484
 
    } else {
485
 
        duration=m_pAStream->duration;
486
 
        time_base=m_pAStream->time_base;
487
 
    }
488
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
489
 
    return double(duration)/AV_TIME_BASE;
490
 
#else
491
 
    return double(duration)*av_q2d(time_base);
492
 
#endif 
493
 
}
494
 
 
495
 
double FFMpegDecoder::getNominalFPS() const
496
 
{
497
 
    AVG_ASSERT(m_State != CLOSED);
498
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
499
 
    return m_pVStream->r_frame_rate;
500
 
#else
501
 
    return av_q2d(m_pVStream->r_frame_rate);
502
 
#endif 
503
 
}
504
 
 
505
 
double FFMpegDecoder::getFPS() const
506
 
{
507
 
    AVG_ASSERT(m_State != CLOSED);
508
 
    return m_FPS;
509
 
}
510
 
 
511
 
void FFMpegDecoder::setFPS(double fps)
512
 
{
513
 
    m_bUseStreamFPS = (fps == 0);
514
 
    if (fps == 0) {
515
 
        m_FPS = calcStreamFPS();
516
 
    } else {
517
 
        m_FPS = fps;
518
 
    }
519
 
}
520
 
 
521
 
double FFMpegDecoder::getVolume() const
522
 
{
523
 
    AVG_ASSERT(m_State != CLOSED);
524
 
    return m_Volume;
525
 
}
526
 
 
527
 
void FFMpegDecoder::setVolume(double volume)
528
 
{
529
 
    m_Volume = volume;
530
 
    if (m_State != DECODING) {
531
 
        m_LastVolume = volume;
532
 
    }
533
 
}
534
 
 
535
 
void copyPlaneToBmp(BitmapPtr pBmp, unsigned char * pData, int stride)
536
 
{
537
 
    unsigned char * pSrc=pData;
538
 
    unsigned char * pDest= pBmp->getPixels();
539
 
    int destStride = pBmp->getStride();
540
 
    int height = pBmp->getSize().y;
541
 
    int width = pBmp->getSize().x;
542
 
    for (int y = 0; y < height; y++) {
543
 
        memcpy(pDest, pSrc, width);
544
 
        pSrc += stride;
545
 
        pDest += destStride;
546
 
    }
547
 
}
548
 
 
549
 
static ProfilingZoneID RenderToBmpProfilingZone("FFMpeg: renderToBmp");
550
 
static ProfilingZoneID CopyImageProfilingZone("FFMpeg: copy image");
551
 
 
552
 
FrameAvailableCode FFMpegDecoder::renderToBmps(vector<BitmapPtr>& pBmps, 
553
 
        double timeWanted)
554
 
{
555
 
    AVG_ASSERT(m_State == DECODING);
556
 
    ScopeTimer timer(RenderToBmpProfilingZone);
557
 
    AVFrame frame;
558
 
    FrameAvailableCode frameAvailable;
559
 
    if (timeWanted == -1) {
560
 
        readFrame(frame);
561
 
        frameAvailable = FA_NEW_FRAME;
562
 
    } else {
563
 
        frameAvailable = readFrameForTime(frame, timeWanted);
564
 
    }
565
 
    if (!m_bVideoEOF && frameAvailable == FA_NEW_FRAME) {
566
 
        if (pixelFormatIsPlanar(m_PF)) {
567
 
            ScopeTimer timer(CopyImageProfilingZone);
568
 
            for (unsigned i = 0; i < pBmps.size(); ++i) {
569
 
                copyPlaneToBmp(pBmps[i], frame.data[i], frame.linesize[i]);
570
 
            }
571
 
        } else {
572
 
            convertFrameToBmp(frame, pBmps[0]);
573
 
        }
574
 
        return FA_NEW_FRAME;
575
 
    }
576
 
    return FA_USE_LAST_FRAME;
577
 
}
578
 
 
579
 
void FFMpegDecoder::throwAwayFrame(double timeWanted)
580
 
{
581
 
    AVG_ASSERT(m_State == DECODING);
582
 
    AVFrame frame;
583
 
    readFrameForTime(frame, timeWanted);
584
 
}
585
 
 
586
 
bool FFMpegDecoder::isEOF(StreamSelect stream) const
587
 
{
588
 
    AVG_ASSERT(m_State == DECODING);
589
 
    switch(stream) {
590
 
        case SS_AUDIO:
591
 
            return (!m_pAStream || m_bAudioEOF);
592
 
        case SS_VIDEO:
593
 
            return (!m_pVStream || m_bVideoEOF);
594
 
        case SS_ALL:
595
 
            return isEOF(SS_VIDEO) && isEOF(SS_AUDIO);
596
 
        default:
597
 
            return false;
598
 
    }
599
 
}
600
 
 
601
 
int FFMpegDecoder::copyRawAudio(unsigned char* pBuffer, int size)
602
 
{
603
 
    int bytesWritten = min(m_SampleBufferEnd - m_SampleBufferStart, size);
604
 
    memcpy(pBuffer, m_pSampleBuffer + m_SampleBufferStart, bytesWritten);
605
 
    
606
 
    m_SampleBufferStart += bytesWritten;
607
 
    
608
 
    if (m_SampleBufferStart == m_SampleBufferEnd) {
609
 
        m_SampleBufferStart = 0;
610
 
        m_SampleBufferEnd = 0;
611
 
        m_SampleBufferLeft = SAMPLE_BUFFER_SIZE;
612
 
    }
613
 
    
614
 
    return bytesWritten;
615
 
}
616
 
 
617
 
int FFMpegDecoder::copyResampledAudio(unsigned char* pBuffer, int size)
618
 
{
619
 
    int bytesWritten = 0;
620
 
                        
621
 
    // If there is no buffered resampled data, resample some more
622
 
    if (m_ResampleBufferStart >= m_ResampleBufferEnd) {
623
 
        resampleAudio();
624
 
    }
625
 
    
626
 
    // If we have some data in the resample buffer, copy it over
627
 
    if (m_ResampleBufferStart < m_ResampleBufferEnd) {
628
 
        bytesWritten = min(m_ResampleBufferEnd - m_ResampleBufferStart, size);
629
 
        memcpy(pBuffer, m_pResampleBuffer + m_ResampleBufferStart, bytesWritten);
630
 
        
631
 
        m_ResampleBufferStart += bytesWritten;
632
 
        if (m_ResampleBufferStart >= m_ResampleBufferEnd) {
633
 
            m_ResampleBufferStart = 0;
634
 
            m_ResampleBufferEnd = 0;
635
 
        }
636
 
        
637
 
        if (m_SampleBufferStart == m_SampleBufferEnd) {
638
 
            m_SampleBufferStart = 0;
639
 
            m_SampleBufferEnd = 0;
640
 
            m_SampleBufferLeft = SAMPLE_BUFFER_SIZE;
641
 
        }
642
 
    }
643
 
    
644
 
    return bytesWritten;
645
 
}
646
 
 
647
 
void FFMpegDecoder::resampleAudio()
648
 
{
649
 
    if (!m_pAudioResampleContext) {
650
 
        m_pAudioResampleContext = audio_resample_init(
651
 
                m_AP.m_Channels, m_pAStream->codec->channels, 
652
 
                m_AP.m_SampleRate, m_EffectiveSampleRate);
653
 
    }
654
 
    
655
 
    if (!m_pResampleBuffer) {
656
 
        m_ResampleBufferSize = (int)(SAMPLE_BUFFER_SIZE * 
657
 
                ((double)m_AP.m_SampleRate / (double)m_EffectiveSampleRate));
658
 
        m_pResampleBuffer = (char*)av_mallocz(m_ResampleBufferSize);
659
 
    }
660
 
    
661
 
    int inputSamples = 
662
 
        (m_SampleBufferEnd - m_SampleBufferStart) / 
663
 
        (2 * m_pAStream->codec->channels);
664
 
    
665
 
    int outputSamples = audio_resample(m_pAudioResampleContext,
666
 
            (short*)m_pResampleBuffer,
667
 
            (short*)(m_pSampleBuffer + m_SampleBufferStart), 
668
 
            inputSamples);
669
 
    
670
 
    // Adjust buffer pointers
671
 
    m_ResampleBufferEnd += outputSamples * 2 * m_AP.m_Channels;
672
 
    m_SampleBufferStart += inputSamples * 2 * m_pAStream->codec->channels;
673
 
}
674
 
 
675
 
int FFMpegDecoder::decodeAudio()
676
 
{
677
 
    // Save current size of the audio buffer
678
 
    int lastSampleBufferSize = m_SampleBufferLeft;
679
 
 
680
 
#if LIBAVCODEC_BUILD > ((51<<16)+(11<<8)+0)
681
 
    // Decode some data from packet into the audio buffer
682
 
    int packetBytesDecoded = avcodec_decode_audio2(
683
 
            m_pAStream->codec, 
684
 
            (short*)(m_pSampleBuffer + m_SampleBufferEnd),
685
 
            &m_SampleBufferLeft, 
686
 
            m_AudioPacketData,
687
 
            m_AudioPacketSize);
688
 
#else
689
 
    int packetBytesDecoded = avcodec_decode_audio(
690
 
            m_pAStream->codec, 
691
 
            (short*)(m_pSampleBuffer + m_SampleBufferEnd),
692
 
            &m_SampleBufferLeft, 
693
 
            m_AudioPacketData,
694
 
            m_AudioPacketSize);
695
 
#endif
696
 
 
697
 
    // Skip frame on error
698
 
    if (packetBytesDecoded < 0) {
699
 
        return -1;
700
 
    }
701
 
    
702
 
    // Did not get any data, try again
703
 
    if (packetBytesDecoded == 0) {
704
 
        return 0;
705
 
    }
706
 
    
707
 
    // Adjust audio buffer pointers
708
 
    m_SampleBufferEnd += m_SampleBufferLeft;
709
 
    m_SampleBufferLeft = lastSampleBufferSize - m_SampleBufferLeft;
710
 
                
711
 
    // Adjust packet data pointers
712
 
    m_AudioPacketData += packetBytesDecoded;
713
 
    m_AudioPacketSize -= packetBytesDecoded;
714
 
    return packetBytesDecoded;
715
 
}
716
 
 
717
 
int FFMpegDecoder::fillAudioBuffer(AudioBufferPtr pBuffer)
718
 
{
719
 
    AVG_ASSERT(m_State == DECODING);
720
 
    mutex::scoped_lock lock(m_AudioMutex);
721
 
 
722
 
    unsigned char* pOutputBuffer = (unsigned char*)(pBuffer->getData());
723
 
    int outputAudioBufferSize = pBuffer->getNumBytes();
724
 
 
725
 
    AVG_ASSERT (m_pAStream);
726
 
    if (m_bAudioEOF) {
727
 
        return 0;
728
 
    }
729
 
    
730
 
    int packetBytesDecoded;
731
 
    int bytesProduced;
732
 
    unsigned char* pCurBufferPos = pOutputBuffer;
733
 
    int bufferLeft = outputAudioBufferSize;
734
 
    bool bFormatMatch = (m_EffectiveSampleRate == m_AP.m_SampleRate &&
735
 
                         m_pAStream->codec->channels == m_AP.m_Channels);
736
 
 
737
 
    while (true) {
738
 
        while (true) {
739
 
            // Consume any data left in the sample buffers
740
 
            while (m_SampleBufferStart < m_SampleBufferEnd ||
741
 
                  m_ResampleBufferStart < m_ResampleBufferEnd)
742
 
            {
743
 
                // If the output format is different from the decoded format,
744
 
                // then convert it, else copy it over
745
 
                if (bFormatMatch) {
746
 
                    bytesProduced = copyRawAudio(pCurBufferPos, bufferLeft);
747
 
                } else {
748
 
                    bytesProduced = copyResampledAudio(pCurBufferPos, bufferLeft);
749
 
                }
750
 
                
751
 
                pCurBufferPos += bytesProduced;
752
 
                bufferLeft -= bytesProduced;
753
 
 
754
 
                m_LastAudioFrameTime += (double(bytesProduced) /
755
 
                        (2 * m_AP.m_Channels * m_AP.m_SampleRate));
756
 
                if (bufferLeft == 0) {
757
 
                    volumize(pBuffer);
758
 
                    return pBuffer->getNumFrames();
759
 
                }
760
 
            }
761
 
            
762
 
            if (m_AudioPacketSize <= 0)
763
 
                break;
764
 
            
765
 
            packetBytesDecoded = decodeAudio();
766
 
            
767
 
            // Skip frame on error
768
 
            if (packetBytesDecoded < 0)
769
 
                break;
770
 
            
771
 
            // Did not get any data, try again
772
 
            if (packetBytesDecoded == 0)
773
 
                continue;
774
 
        }
775
 
        
776
 
        // We have decoded all data in the packet, free it
777
 
        if (m_AudioPacket) {
778
 
            av_free_packet(m_AudioPacket);
779
 
            delete m_AudioPacket;
780
 
        }
781
 
        
782
 
        // Get a new packet from the audio stream
783
 
        m_AudioPacket = m_pDemuxer->getPacket(m_AStreamIndex);
784
 
        if (!m_AudioPacket) {
785
 
            m_bAudioEOF = true;
786
 
            volumize(pBuffer);
787
 
            return pBuffer->getNumFrames()-bufferLeft/(pBuffer->getFrameSize());
788
 
        }
789
 
 
790
 
        // Initialize packet data pointers
791
 
        m_AudioPacketData = m_AudioPacket->data;
792
 
        m_AudioPacketSize = m_AudioPacket->size;
793
 
    }
794
 
}
795
 
 
796
 
PixelFormat FFMpegDecoder::calcPixelFormat(bool bUseYCbCr)
797
 
{
798
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
799
 
        AVCodecContext *enc = &m_pVStream->codec;
800
 
#else
801
 
        AVCodecContext *enc = m_pVStream->codec;
802
 
#endif
803
 
    if (bUseYCbCr) {
804
 
        switch(enc->pix_fmt) {
805
 
            case PIX_FMT_YUV420P:
806
 
                return YCbCr420p;
807
 
            case PIX_FMT_YUVJ420P:
808
 
                return YCbCrJ420p;
809
 
            case PIX_FMT_YUVA420P:
810
 
                return YCbCrA420p;
811
 
            default:
812
 
                break;
813
 
        }
814
 
    }
815
 
    if (enc->pix_fmt == PIX_FMT_BGRA || enc->pix_fmt == PIX_FMT_YUVA420P) {
816
 
        return B8G8R8A8;
817
 
    }
818
 
    return B8G8R8X8;
819
 
}
820
 
 
821
 
static ProfilingZoneID ConvertImageLibavgProfilingZone(
822
 
            "FFMpeg: colorspace conv (libavg)");
823
 
static ProfilingZoneID ConvertImageSWSProfilingZone("FFMpeg: colorspace conv (SWS)");
824
 
static ProfilingZoneID SetAlphaProfilingZone("FFMpeg: set alpha channel");
825
 
 
826
 
void FFMpegDecoder::convertFrameToBmp(AVFrame& frame, BitmapPtr pBmp)
827
 
{
828
 
    AVPicture destPict;
829
 
    unsigned char * pDestBits = pBmp->getPixels();
830
 
    destPict.data[0] = pDestBits;
831
 
    destPict.linesize[0] = pBmp->getStride();
832
 
#if LIBAVFORMAT_BUILD >= ((50<<16)+(0<<8)+0)
833
 
    ::PixelFormat destFmt;
834
 
#else
835
 
    int destFmt;
836
 
#endif
837
 
    switch(pBmp->getPixelFormat()) {
838
 
        case R8G8B8X8:
839
 
        case R8G8B8A8:
840
 
            // XXX: Unused and broken.
841
 
            destFmt = PIX_FMT_BGRA;
842
 
            break;
843
 
        case B8G8R8X8:
844
 
        case B8G8R8A8:
845
 
            destFmt = PIX_FMT_BGRA;
846
 
            break;
847
 
        case R8G8B8:
848
 
            destFmt = PIX_FMT_RGB24;
849
 
            break;
850
 
        case B8G8R8:
851
 
            destFmt = PIX_FMT_BGR24;
852
 
            break;
853
 
        case YCbCr422:
854
 
            destFmt = PIX_FMT_YUYV422;
855
 
            break;
856
 
        default:
857
 
            AVG_TRACE(Logger::ERROR, "FFMpegDecoder: Dest format " 
858
 
                    << pBmp->getPixelFormat() << " not supported.");
859
 
            AVG_ASSERT(false);
860
 
            destFmt = PIX_FMT_BGRA;
861
 
    }
862
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
863
 
    AVCodecContext *enc = &m_pVStream->codec;
864
 
#else
865
 
    AVCodecContext *enc = m_pVStream->codec;
866
 
#endif
867
 
    {
868
 
        if (destFmt == PIX_FMT_BGRA && (enc->pix_fmt == PIX_FMT_YUV420P || 
869
 
                enc->pix_fmt == PIX_FMT_YUVJ420P))
870
 
        {
871
 
            ScopeTimer timer(ConvertImageLibavgProfilingZone);
872
 
            BitmapPtr pBmpY(new Bitmap(pBmp->getSize(), I8, frame.data[0],
873
 
                    frame.linesize[0], false));
874
 
            BitmapPtr pBmpU(new Bitmap(pBmp->getSize(), I8, frame.data[1],
875
 
                    frame.linesize[1], false));
876
 
            BitmapPtr pBmpV(new Bitmap(pBmp->getSize(), I8, frame.data[2],
877
 
                    frame.linesize[2], false));
878
 
            pBmp->copyYUVPixels(*pBmpY, *pBmpU, *pBmpV, enc->pix_fmt == PIX_FMT_YUVJ420P);
879
 
        } else {
880
 
            if (!m_pSwsContext) {
881
 
                m_pSwsContext = sws_getContext(enc->width, enc->height, enc->pix_fmt,
882
 
                            enc->width, enc->height, destFmt, SWS_BICUBIC, 
883
 
                            NULL, NULL, NULL);
884
 
                AVG_ASSERT(m_pSwsContext);
885
 
            }
886
 
            {
887
 
                ScopeTimer timer(ConvertImageSWSProfilingZone);
888
 
                sws_scale(m_pSwsContext, frame.data, frame.linesize, 0, 
889
 
                    enc->height, destPict.data, destPict.linesize);
890
 
            }
891
 
            if (pBmp->getPixelFormat() == B8G8R8X8) {
892
 
                ScopeTimer timer(SetAlphaProfilingZone);
893
 
                // Make sure the alpha channel is white.
894
 
                // TODO: This is slow. Make OpenGL do it.
895
 
                unsigned char * pLine = pBmp->getPixels();
896
 
                IntPoint size = pBmp->getSize();
897
 
                for (int y = 0; y < size.y; ++y) {
898
 
                    unsigned char * pPixel = pLine;
899
 
                    for (int x = 0; x < size.x; ++x) {
900
 
                        pPixel[3] = 0xFF;
901
 
                        pPixel += 4;
902
 
                    }
903
 
                    pLine = pLine + pBmp->getStride();
904
 
                }
905
 
            }
906
 
        }
907
 
    }
908
 
}
909
 
       
910
 
PixelFormat FFMpegDecoder::getPixelFormat() const
911
 
{
912
 
    AVG_ASSERT(m_State != CLOSED);
913
 
    return m_PF;
914
 
}
915
 
 
916
 
void FFMpegDecoder::initVideoSupport()
917
 
{
918
 
    if (!s_bInitialized) {
919
 
        av_register_all();
920
 
        s_bInitialized = true;
921
 
        // Tune libavcodec console spam.
922
 
//        av_log_set_level(AV_LOG_DEBUG);
923
 
        av_log_set_level(AV_LOG_QUIET);
924
 
    }
925
 
}
926
 
 
927
 
int FFMpegDecoder::getNumFrames() const
928
 
{
929
 
    AVG_ASSERT(m_State != CLOSED);
930
 
    // This is broken for some videos, but the code here is correct.
931
 
    // So fix ffmpeg :-).
932
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
933
 
    return m_pVStream->r_frame_rate*(m_pVStream->duration/AV_TIME_BASE);
934
 
#else
935
 
    return int(m_pVStream->nb_frames);
936
 
#endif 
937
 
}
938
 
 
939
 
FrameAvailableCode FFMpegDecoder::readFrameForTime(AVFrame& frame, double timeWanted)
940
 
{
941
 
    AVG_ASSERT(m_State == DECODING);
942
 
//    cerr << "        readFrameForTime " << timeWanted << ", LastFrameTime= " 
943
 
//            << m_LastVideoFrameTime << ", diff= " << m_LastVideoFrameTime-timeWanted 
944
 
//            << endl;
945
 
    AVG_ASSERT(timeWanted != -1);
946
 
    double timePerFrame = 1.0/m_FPS;
947
 
    if (timeWanted-m_LastVideoFrameTime < 0.5*timePerFrame) {
948
 
//        cerr << "DISPLAY AGAIN." << endl;
949
 
        // The last frame is still current. Display it again.
950
 
        return FA_USE_LAST_FRAME;
951
 
    } else {
952
 
        double frameTime = -1;
953
 
        while (frameTime-timeWanted < -0.5*timePerFrame && !m_bVideoEOF) {
954
 
            frameTime = readFrame(frame);
955
 
//            cerr << "        readFrame returned time " << frameTime << ", diff= " <<
956
 
//                    frameTime-timeWanted <<  endl;
957
 
        }
958
 
//        cerr << "NEW FRAME." << endl;
959
 
    }
960
 
    return FA_NEW_FRAME;
961
 
}
962
 
 
963
 
static ProfilingZoneID DecodeProfilingZone("FFMpeg: decode");
964
 
 
965
 
double FFMpegDecoder::readFrame(AVFrame& frame)
966
 
{
967
 
    AVG_ASSERT(m_State == DECODING);
968
 
    ScopeTimer timer(DecodeProfilingZone); 
969
 
 
970
 
    if (m_bEOFPending) {
971
 
        m_bVideoEOF = true;
972
 
        m_bEOFPending = false;
973
 
        return m_LastVideoFrameTime;
974
 
    }
975
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
976
 
    AVCodecContext *enc = &m_pVStream->codec;
977
 
#else
978
 
    AVCodecContext *enc = m_pVStream->codec;
979
 
#endif
980
 
    int bGotPicture = 0;
981
 
    AVPacket* pPacket = 0;
982
 
    double frameTime = -1;
983
 
    while (!bGotPicture && !m_bVideoEOF) {
984
 
        pPacket = m_pDemuxer->getPacket(m_VStreamIndex);
985
 
        m_bFirstPacket = false;
986
 
        if (pPacket) {
987
 
            int len1 = avcodec_decode_video(enc, &frame, &bGotPicture, pPacket->data,
988
 
                    pPacket->size);
989
 
            if (len1 > 0) {
990
 
                AVG_ASSERT(len1 == pPacket->size);
991
 
            }
992
 
            if (bGotPicture) {
993
 
                frameTime = getFrameTime(pPacket->dts);
994
 
            }
995
 
            av_free_packet(pPacket);
996
 
            delete pPacket;
997
 
        } else {
998
 
            // No more packets -> EOF. Decode the last data we got.
999
 
            avcodec_decode_video(enc, &frame, &bGotPicture, 0, 0);
1000
 
            if (bGotPicture) {
1001
 
                m_bEOFPending = true;
1002
 
            } else {
1003
 
                m_bVideoEOF = true;
1004
 
            }
1005
 
            // We don't have a timestamp for the last frame, so we'll
1006
 
            // calculate it based on the frame before.
1007
 
            frameTime = m_LastVideoFrameTime+1.0/m_FPS;
1008
 
            m_LastVideoFrameTime = frameTime;
1009
 
        }
1010
 
    }
1011
 
    AVG_ASSERT(frameTime != -1)
1012
 
    return frameTime;
1013
 
/*
1014
 
    cerr << "coded_picture_number: " << frame.coded_picture_number <<
1015
 
            ", display_picture_number: " << frame.display_picture_number <<
1016
 
            ", pts: " << frame.pts << endl;
1017
 
 
1018
 
    cerr << "key_frame: " << frame.key_frame << 
1019
 
           ", pict_type: " << frame.pict_type << endl;
1020
 
    AVFrac spts = m_pVStream->pts;
1021
 
    cerr << "Stream.pts: " << spts.val + double(spts.num)/spts.den << endl;
1022
 
*/
1023
 
}
1024
 
 
1025
 
double FFMpegDecoder::getFrameTime(long long dts)
1026
 
{
1027
 
    if (m_VideoStartTimestamp == -1) {
1028
 
        m_VideoStartTimestamp = double(dts)/m_TimeUnitsPerSecond;
1029
 
    }
1030
 
    double frameTime;
1031
 
    if (m_bUseStreamFPS) {
1032
 
        frameTime = double(dts)/m_TimeUnitsPerSecond-m_VideoStartTimestamp;
1033
 
    } else {
1034
 
        if (m_LastVideoFrameTime == -1) {
1035
 
            frameTime = 0;
1036
 
        } else {
1037
 
            frameTime = m_LastVideoFrameTime + 1.0/m_FPS;
1038
 
        }
1039
 
    }
1040
 
    m_LastVideoFrameTime = frameTime;
1041
 
    return frameTime;
1042
 
}
1043
 
 
1044
 
double FFMpegDecoder::getStartTime()
1045
 
{
1046
 
    if (m_pVStream) {
1047
 
        return m_VideoStartTimestamp;
1048
 
    } else {
1049
 
        return m_AudioStartTimestamp;
1050
 
    }
1051
 
}
1052
 
 
1053
 
double FFMpegDecoder::calcStreamFPS() const
1054
 
{
1055
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
1056
 
    return m_pVStream->r_frame_rate;
1057
 
#else
1058
 
    return (m_pVStream->r_frame_rate.num/m_pVStream->r_frame_rate.den);
1059
 
#endif 
1060
 
}
1061
 
 
1062
 
string FFMpegDecoder::getStreamPF() const
1063
 
{
1064
 
#if LIBAVFORMAT_BUILD < ((49<<16)+(0<<8)+0)
1065
 
    AVCodecContext *pCodec = &m_pVStream->codec;
1066
 
#else
1067
 
    AVCodecContext *pCodec = m_pVStream->codec;
1068
 
#endif
1069
 
    ::PixelFormat pf = pCodec->pix_fmt;
1070
 
    return avcodec_get_pix_fmt_name(pf);
1071
 
}
1072
 
 
1073
 
// TODO: this should be logarithmic...
1074
 
void FFMpegDecoder::volumize(AudioBufferPtr pBuffer)
1075
 
{
1076
 
    double curVol = m_Volume;
1077
 
    double volDiff = m_LastVolume - curVol;
1078
 
    
1079
 
    if (curVol == 1.0 && volDiff == 0.0) {
1080
 
        return;
1081
 
    }
1082
 
   
1083
 
    short * pData = pBuffer->getData();
1084
 
    for (int i = 0; i < pBuffer->getNumFrames()*pBuffer->getNumChannels(); i++) {
1085
 
        double fadeVol = 0;
1086
 
        if (volDiff != 0 && i < VOLUME_FADE_SAMPLES) {
1087
 
            fadeVol = volDiff * (VOLUME_FADE_SAMPLES - i) / VOLUME_FADE_SAMPLES;
1088
 
        }
1089
 
        
1090
 
        int s = int(pData[i] * (curVol + fadeVol));
1091
 
        
1092
 
        if (s < -32768)
1093
 
            s = -32768;
1094
 
        if (s >  32767)
1095
 
            s = 32767;
1096
 
        
1097
 
        pData[i] = s;
1098
 
    }
1099
 
    m_LastVolume = curVol;
1100
 
}
1101
 
 
1102
 
}
1103