Ticket #1104: mythtv_ac3.19.patch

File mythtv_ac3.19.patch, 63.9 KB (added by Mark Spieth, 18 years ago)

includes a small bug fix for encoder not initialized in aob

  • libs/libmyth/libmyth.pro

     
    3333SOURCES += DisplayResScreen.cpp util-x11.cpp qmdcodec.cpp
    3434SOURCES += virtualkeyboard.cpp mythobservable.cpp mythsocket.cpp
    3535
     36#INCLUDEPATH += ../libmythsamplerate ../libmythsoundtouch ../..
     37#INCLUDEPATH += ../libavutil ..
     38#DEPENDPATH += ../libmythsamplerate ../libmythsoundtouch
    3639INCLUDEPATH += ../libmythsamplerate ../libmythsoundtouch ../.. ../
     40INCLUDEPATH += ../libavutil
    3741DEPENDPATH += ../libmythsamplerate ../libmythsoundtouch ../ ../libmythui
     42DEPENDPATH += ../libavutil ../libavcodec
    3843
    3944LIBS += -L../libmythsamplerate -lmythsamplerate-$${LIBVERSION}
    4045LIBS += -L../libmythsoundtouch -lmythsoundtouch-$${LIBVERSION}
     46LIBS += -L../libavcodec -lmythavcodec-$${LIBVERSION}
    4147
    4248isEmpty(QMAKE_EXTENSION_SHLIB) {
    4349  QMAKE_EXTENSION_SHLIB=so
  • libs/libmyth/audiooutput.h

     
    3131    virtual ~AudioOutput() { };
    3232
    3333    // reconfigure sound out for new params
    34     virtual void Reconfigure(int audio_bits, int audio_channels,
    35                              int audio_samplerate, bool audio_passthru) = 0;
     34    virtual void Reconfigure(int audio_bits,
     35                             int audio_channels,
     36                             int audio_samplerate,
     37                             bool audio_passthru,
     38                             void* audio_codec = NULL
     39                             ) = 0;
    3640   
    3741    virtual void SetStretchFactor(float factor);
    3842
     
    7478        lastError = msg;
    7579        VERBOSE(VB_IMPORTANT, "AudioOutput Error: " + lastError);
    7680    }
     81    void ClearError()
     82     { lastError = QString::null; };
    7783
    7884    void Warn(QString msg)
    7985    {
  • libs/libmyth/audiooutputdx.h

     
    3535    /// END HACK HACK HACK HACK
    3636       
    3737    virtual void Reset(void);
    38     virtual void Reconfigure(int audio_bits,       int audio_channels,
    39                              int audio_samplerate, int audio_passthru);
     38    virtual void Reconfigure(int audio_bits,
     39                         int audio_channels,
     40                         int audio_samplerate,
     41                         bool audio_passthru,
     42                         AudioCodecMode aom = AUDIOCODECMODE_NORMAL);
    4043    virtual void SetBlocking(bool blocking);
    4144
    4245    virtual bool AddSamples(char *buffer, int samples, long long timecode);
  • libs/libmyth/audiooutputdx.cpp

     
    130130    // FIXME: kedl: not sure what else could be required here?
    131131}
    132132
    133 void AudioOutputDX::Reconfigure(int audio_bits, int audio_channels,
    134                                 int audio_samplerate, int audio_passthru)
     133void AudioOutputDX::Reconfigure(int audio_bits,
     134                                int audio_channels,
     135                                int audio_samplerate,
     136                                int audio_passthru,
     137                                AudioCodecMode laom
     138                                )
    135139{
    136140    if (dsbuffer)
    137141        DestroyDSBuffer();
  • libs/libmyth/audiooutputbase.h

     
    1818#include "samplerate.h"
    1919#include "SoundTouch.h"
    2020
    21 #define AUDBUFSIZE 768000
     21struct AVCodecContext;
     22class DigitalEncoder;
    2223#define AUDIO_SRC_IN_SIZE   16384
    2324#define AUDIO_SRC_OUT_SIZE (16384*6)
    2425#define AUDIO_TMP_BUF_SIZE (16384*6)
    2526
     27//#define AUDBUFSIZE 768000
     28//divisible by 12,10,8,6,4,2 and around 1024000
     29//#define AUDBUFSIZE 1024080
     30#define AUDBUFSIZE 1536000
     31
    2632class AudioOutputBase : public AudioOutput
    2733{
    2834 public:
     
    3541    virtual ~AudioOutputBase();
    3642
    3743    // reconfigure sound out for new params
    38     virtual void Reconfigure(int audio_bits, int audio_channels,
    39                              int audio_samplerate, bool audio_passthru);
     44    virtual void Reconfigure(int audio_bits,
     45                             int audio_channels,
     46                             int audio_samplerate,
     47                             bool audio_passthru,
     48                             void* audio_codec = NULL);
    4049   
    4150    // do AddSamples calls block?
    4251    virtual void SetBlocking(bool blocking);
     
    120129    bool audio_passthru;
    121130
    122131    float audio_stretchfactor;
     132    AVCodecContext *audio_codec;
    123133    AudioOutputSource source;
    124134
    125135    bool killaudio;
     
    128138    bool set_initial_vol;
    129139    bool buffer_output_data_for_use; //  used by AudioOutputNULL
    130140   
     141    int configured_audio_channels;
     142
    131143 private:
    132144    // resampler
    133145    bool need_resampler;
     
    139151
    140152    // timestretch
    141153    soundtouch::SoundTouch * pSoundStretch;
     154    DigitalEncoder * encoder;
    142155
    143156    bool blocking; // do AddSamples calls block?
    144157
     
    155168
    156169    pthread_mutex_t avsync_lock; /* must hold avsync_lock to read or write
    157170                                    'audiotime' and 'audiotime_updated' */
    158     int audiotime; // timecode of audio leaving the soundcard (same units as
     171    long long audiotime; // timecode of audio leaving the soundcard (same units as
    159172                   //                                          timecodes) ...
    160173    struct timeval audiotime_updated; // ... which was last updated at this time
    161174
    162175    /* Audio circular buffer */
    163176    unsigned char audiobuffer[AUDBUFSIZE];  /* buffer */
    164177    int raud, waud;     /* read and write positions */
    165     int audbuf_timecode;    /* timecode of audio most recently placed into
     178    long long audbuf_timecode;    /* timecode of audio most recently placed into
    166179                   buffer */
    167180
    168181    int numlowbuffer;
  • libs/libmyth/audiooutputbase.cpp

     
    1616// MythTV headers
    1717#include "audiooutputbase.h"
    1818
     19extern "C" {
     20#include "libavcodec/avcodec.h"
     21#include "libavcodec/liba52/a52.h"
     22}
     23
     24#if QT_VERSION < 0x030200
     25#define LONGLONGCONVERT (long)
     26#else
     27#define LONGLONGCONVERT
     28#endif
     29
     30#define LOC QString("DEnc: ");
     31#define MAX_AC3_FRAME_SIZE 6144
     32class DigitalEncoder
     33{
     34public:
     35    DigitalEncoder();
     36    ~DigitalEncoder();
     37    void Dispose();
     38    bool DigitalEncoder::Init(CodecID codec_id, int bitrate, int samplerate, int channels);
     39    size_t Encode(short * buff);
     40
     41    // if needed
     42    char * GetFrameBuffer()
     43    {
     44        if (!frame_buffer && av_context)
     45        {
     46            frame_buffer = new char [one_frame_bytes];
     47        }
     48        return frame_buffer;
     49    }   
     50    size_t FrameSize() const { return one_frame_bytes; }
     51    char * GetOutBuff() const { return outbuf; }
     52
     53    size_t audio_bytes_per_sample;
     54private:
     55    AVCodecContext *av_context;
     56    char * outbuf;
     57    char * frame_buffer;
     58    int outbuf_size;
     59    size_t one_frame_bytes;
     60};
     61
     62DigitalEncoder::DigitalEncoder()
     63{
     64    av_context = NULL;
     65    outbuf = NULL;
     66    outbuf_size = 0;
     67    one_frame_bytes = 0;
     68    frame_buffer = NULL;
     69}
     70
     71DigitalEncoder::~DigitalEncoder()
     72{
     73    Dispose();
     74}
     75
     76void DigitalEncoder::Dispose()
     77{
     78    if (av_context)
     79    {
     80        avcodec_close(av_context);
     81        av_free(av_context);
     82        av_context = NULL;
     83    }
     84    if (outbuf)
     85    {
     86        delete [] outbuf;
     87        outbuf = NULL;
     88        outbuf_size = 0;
     89    }
     90    if (frame_buffer)
     91    {
     92        delete [] frame_buffer;
     93        frame_buffer = NULL;
     94        one_frame_bytes = 0;
     95    }
     96}
     97
     98//CODEC_ID_AC3
     99bool DigitalEncoder::Init(CodecID codec_id, int bitrate, int samplerate, int channels)
     100{
     101    AVCodec * codec;
     102    int ret;
     103
     104    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init codecid=%1, br=%2, sr=%3, ch=%4")
     105            .arg(codec_id_string(codec_id))
     106            .arg(bitrate)
     107            .arg(samplerate)
     108            .arg(channels));
     109    //codec = avcodec_find_encoder(codec_id);
     110    // always AC3 as there is no DTS encoder at the moment 2005/1/9
     111    codec = avcodec_find_encoder(CODEC_ID_AC3);
     112    if (!codec)
     113    {
     114        VERBOSE(VB_IMPORTANT,"Error: could not find codec");
     115        return false;
     116    }
     117    av_context = avcodec_alloc_context();
     118    av_context->bit_rate = bitrate;
     119    av_context->sample_rate = samplerate;
     120    av_context->channels = channels;
     121    // open it */
     122    if ((ret = avcodec_open(av_context, codec)) < 0)
     123    {
     124        VERBOSE(VB_IMPORTANT,"Error: could not open codec, invalid bitrate or samplerate");
     125        Dispose();
     126        return false;
     127    }
     128
     129    size_t bytes_per_frame = av_context->channels * sizeof(short);
     130    audio_bytes_per_sample = bytes_per_frame;
     131    one_frame_bytes = bytes_per_frame * av_context->frame_size;
     132
     133    outbuf_size = 16384;    // ok for AC3 but DTS?
     134    outbuf = new char [outbuf_size];
     135    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
     136            .arg(av_context->frame_size)
     137            .arg(bytes_per_frame)
     138            .arg(one_frame_bytes)
     139           );
     140
     141    return true;
     142}
     143
     144static int DTS_SAMPLEFREQS[16] =
     145{
     146    0,      8000,   16000,  32000,  64000,  128000, 11025,  22050,
     147    44100,  88200,  176400, 12000,  24000,  48000,  96000,  192000
     148};
     149
     150static int DTS_BITRATES[30] =
     151{
     152    32000,    56000,    64000,    96000,    112000,   128000,
     153    192000,   224000,   256000,   320000,   384000,   448000,
     154    512000,   576000,   640000,   768000,   896000,   1024000,
     155    1152000,  1280000,  1344000,  1408000,  1411200,  1472000,
     156    1536000,  1920000,  2048000,  3072000,  3840000,  4096000
     157};
     158
     159static int dts_decode_header(uint8_t *indata_ptr, int *rate,
     160                             int *nblks, int *sfreq)
     161{
     162    uint id = ((indata_ptr[0] << 24) | (indata_ptr[1] << 16) |
     163               (indata_ptr[2] << 8)  | (indata_ptr[3]));
     164
     165    if (id != 0x7ffe8001)
     166        return -1;
     167
     168    int ftype = indata_ptr[4] >> 7;
     169
     170    int surp = (indata_ptr[4] >> 2) & 0x1f;
     171    surp = (surp + 1) % 32;
     172
     173    *nblks = (indata_ptr[4] & 0x01) << 6 | (indata_ptr[5] >> 2);
     174    ++*nblks;
     175
     176    int fsize = (indata_ptr[5] & 0x03) << 12 |
     177                (indata_ptr[6]         << 4) | (indata_ptr[7] >> 4);
     178    ++fsize;
     179
     180    *sfreq = (indata_ptr[8] >> 2) & 0x0f;
     181    *rate = (indata_ptr[8] & 0x03) << 3 | ((indata_ptr[9] >> 5) & 0x07);
     182
     183    if (ftype != 1)
     184    {
     185        VERBOSE(VB_IMPORTANT, LOC +
     186                QString("DTS: Termination frames not handled (ftype %1)")
     187                .arg(ftype));
     188        return -1;
     189    }
     190
     191    if (*sfreq != 13)
     192    {
     193        VERBOSE(VB_IMPORTANT, LOC +
     194                QString("DTS: Only 48kHz supported (sfreq %1)").arg(*sfreq));
     195        return -1;
     196    }
     197
     198    if ((fsize > 8192) || (fsize < 96))
     199    {
     200        VERBOSE(VB_IMPORTANT, LOC +
     201                QString("DTS: fsize: %1 invalid").arg(fsize));
     202        return -1;
     203    }
     204
     205    if (*nblks != 8 && *nblks != 16 && *nblks != 32 &&
     206        *nblks != 64 && *nblks != 128 && ftype == 1)
     207    {
     208        VERBOSE(VB_IMPORTANT, LOC +
     209                QString("DTS: nblks %1 not valid for normal frame")
     210                .arg(*nblks));
     211        return -1;
     212    }
     213
     214    return fsize;
     215}
     216
     217static int dts_syncinfo(uint8_t *indata_ptr, int * /*flags*/,
     218                        int *sample_rate, int *bit_rate)
     219{
     220    int nblks;
     221    int rate;
     222    int sfreq;
     223
     224    int fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
     225    if (fsize >= 0)
     226    {
     227        if (rate >= 0 && rate <= 29)
     228            *bit_rate = DTS_BITRATES[rate];
     229        else
     230            *bit_rate = 0;
     231        if (sfreq >= 1 && sfreq <= 15)
     232            *sample_rate = DTS_SAMPLEFREQS[sfreq];
     233        else
     234            *sample_rate = 0;
     235    }
     236    return fsize;
     237}
     238
     239static int encode_frame(
     240        bool dts,
     241        unsigned char *data,
     242        size_t &len)
     243{
     244    size_t enc_len;
     245    int flags, sample_rate, bit_rate;
     246
     247    // we don't do any length/crc validation of the AC3 frame here; presumably
     248    // the receiver will have enough sense to do that.  if someone has a
     249    // receiver that doesn't, here would be a good place to put in a call
     250    // to a52_crc16_block(samples+2, data_size-2) - but what do we do if the
     251    // packet is bad?  we'd need to send something that the receiver would
     252    // ignore, and if so, may as well just assume that it will ignore
     253    // anything with a bad CRC...
     254
     255    uint nr_samples = 0, block_len;
     256    if (dts)
     257    {
     258        enc_len = dts_syncinfo(data+8, &flags, &sample_rate, &bit_rate);
     259        int rate, sfreq, nblks;
     260        dts_decode_header(data+8, &rate, &nblks, &sfreq);
     261        nr_samples = nblks * 32;
     262        block_len = nr_samples * 2 * 2;
     263    }
     264    else
     265    {
     266        enc_len = a52_syncinfo(data+8, &flags, &sample_rate, &bit_rate);
     267        block_len = MAX_AC3_FRAME_SIZE;
     268    }
     269
     270    if (enc_len == 0 || enc_len > len)
     271    {
     272        int l = len;
     273        len = 0;
     274        return l;
     275    }
     276
     277    enc_len = min((uint)enc_len, block_len - 8);
     278
     279    //uint32_t x = *(uint32_t*)(data+8);
     280    // in place swab
     281    swab(data+8, data+8, enc_len);
     282    //VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     283    //        QString("DigitalEncoder::Encode swab test %1 %2")
     284    //        .arg(x,0,16).arg(*(uint32_t*)(data+8),0,16));
     285
     286    // the following values come from libmpcodecs/ad_hwac3.c in mplayer.
     287    // they form a valid IEC958 AC3 header.
     288    data[0] = 0x72;
     289    data[1] = 0xF8;
     290    data[2] = 0x1F;
     291    data[3] = 0x4E;
     292    data[4] = 0x01;
     293    if (dts)
     294    {
     295        switch(nr_samples)
     296        {
     297            case 512:
     298                data[4] = 0x0B;      /* DTS-1 (512-sample bursts) */
     299                break;
     300
     301            case 1024:
     302                data[4] = 0x0C;      /* DTS-2 (1024-sample bursts) */
     303                break;
     304
     305            case 2048:
     306                data[4] = 0x0D;      /* DTS-3 (2048-sample bursts) */
     307                break;
     308
     309            default:
     310                VERBOSE(VB_IMPORTANT, LOC +
     311                        QString("DTS: %1-sample bursts not supported")
     312                        .arg(nr_samples));
     313                data[4] = 0x00;
     314                break;
     315        }
     316    }
     317    data[5] = 0x00;
     318    data[6] = (enc_len << 3) & 0xFF;
     319    data[7] = (enc_len >> 5) & 0xFF;
     320    memset(data + 8 + enc_len, 0, block_len - 8 - enc_len);
     321    len = block_len;
     322
     323    return enc_len;
     324}
     325
     326// must have exactly 1 frames worth of data
     327size_t DigitalEncoder::Encode(short * buff)
     328{
     329    int encsize = 0;
     330    size_t outsize = 0;
     331 
     332    // put data in the correct spot for encode frame
     333    outsize = avcodec_encode_audio(
     334                av_context,
     335                ((uchar*)outbuf)+8,
     336                outbuf_size-8,
     337                buff);
     338    size_t tmpsize = outsize;
     339
     340    outsize = MAX_AC3_FRAME_SIZE;
     341    encsize = encode_frame(
     342            //av_context->codec_id==CODEC_ID_DTS,
     343            false,
     344            (unsigned char*)outbuf, outsize);
     345    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     346            QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
     347                .arg(tmpsize)
     348                .arg(encsize)
     349                .arg(outsize)
     350           );
     351
     352    return outsize;
     353}
     354#undef LOC
    19355#define LOC QString("AO: ")
    20356#define LOC_ERR QString("AO, ERROR: ")
    21357
     
    24360    int     /*laudio_bits*/,       int               /*laudio_channels*/,
    25361    int     /*laudio_samplerate*/, AudioOutputSource lsource,
    26362    bool    lset_initial_vol,      bool              /*laudio_passthru*/) :
    27 
    28363    effdsp(0),                  effdspstretched(0),
    29364    audio_channels(-1),         audio_bytes_per_sample(0),
    30365    audio_bits(-1),             audio_samplerate(-1),
     
    35370    audio_passthru_device(QDeepCopy<QString>(laudio_passthru_device)),
    36371    audio_passthru(false),      audio_stretchfactor(1.0f),
    37372
     373    audio_codec(NULL),
    38374    source(lsource),            killaudio(false),
    39375
    40376    pauseaudio(false),          audio_actually_paused(false),
     
    46382
    47383    src_ctx(NULL),
    48384
    49     pSoundStretch(NULL),        blocking(false),
     385    pSoundStretch(NULL),       
     386    encoder(NULL),
     387    blocking(false),
    50388
    51389    lastaudiolen(0),            samples_buffered(0),
    52390    audiotime(0),
     
    61399    pthread_cond_init(&audio_bufsig, NULL);
    62400
    63401    output_audio = 0; // TODO FIXME Not POSIX compatible!
     402    configured_audio_channels = gContext->GetNumSetting("MaxChannels", 2);
    64403
    65404    bzero(&src_data,          sizeof(SRC_DATA));
    66405    bzero(src_in,             sizeof(float) * AUDIO_SRC_IN_SIZE);
     
    108447            VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1")
    109448                                        .arg(audio_stretchfactor));
    110449            pSoundStretch = new soundtouch::SoundTouch();
    111             pSoundStretch->setSampleRate(audio_samplerate);
    112             pSoundStretch->setChannels(audio_channels);
     450            if (audio_codec)
     451            {
     452                if (!encoder)
     453                {
     454                    VERBOSE(VB_AUDIO, LOC + QString("Creating Encoder for codec %1 origfs %2").arg(audio_codec->codec_id).arg(audio_codec->frame_size));
     455                    encoder = new DigitalEncoder();
     456                    if (!encoder->Init(audio_codec->codec_id,
     457                                audio_codec->bit_rate,
     458                                audio_codec->sample_rate,
     459                                audio_codec->channels
     460                                ))
     461                    {
     462                        // eeks
     463                        delete encoder;
     464                        encoder = NULL;
     465                        VERBOSE(VB_AUDIO, LOC + QString("Failed to Create Encoder"));
     466                    }
     467                }
     468            }
     469            if (encoder)
     470            {
     471                pSoundStretch->setSampleRate(audio_codec->sample_rate);
     472                pSoundStretch->setChannels(audio_codec->channels);
     473            }
     474            else
     475            {
     476                pSoundStretch->setSampleRate(audio_samplerate);
     477                pSoundStretch->setChannels(audio_channels);
     478            }
    113479
    114480            pSoundStretch->setTempo(audio_stretchfactor);
    115481            pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35);
     
    132498}
    133499
    134500void AudioOutputBase::Reconfigure(int laudio_bits, int laudio_channels,
    135                                  int laudio_samplerate, bool laudio_passthru)
     501                                 int laudio_samplerate, bool laudio_passthru,
     502                                 void* laudio_codec)
    136503{
     504    int codec_id = CODEC_ID_NONE;
     505    int lcodec_id = CODEC_ID_NONE;
     506    int lcchannels = 0;
     507    int cchannels = 0;
     508    if (laudio_codec)
     509    {
     510        lcodec_id = ((AVCodecContext*)laudio_codec)->codec_id;
     511        laudio_bits = 16;
     512        laudio_channels = 2;
     513        laudio_samplerate = 48000;
     514        lcchannels = ((AVCodecContext*)laudio_codec)->channels;
     515    }
     516    if (audio_codec)
     517    {
     518        codec_id = audio_codec->codec_id;
     519        cchannels = ((AVCodecContext*)audio_codec)->channels;
     520    }
     521    ClearError();
    137522    if (laudio_bits == audio_bits && laudio_channels == audio_channels &&
    138         laudio_samplerate == audio_samplerate &&
    139         laudio_passthru == audio_passthru && !need_resampler)
     523        laudio_samplerate == audio_samplerate && !need_resampler &&
     524        laudio_passthru == audio_passthru &&
     525        lcodec_id == codec_id && lcchannels == cchannels)
    140526        return;
    141527
    142528    KillAudio();
     
    148534    waud = raud = 0;
    149535    audio_actually_paused = false;
    150536   
     537    bool redo_stretch = (pSoundStretch && audio_channels != laudio_channels);
    151538    audio_channels = laudio_channels;
    152539    audio_bits = laudio_bits;
    153540    audio_samplerate = laudio_samplerate;
     541    audio_codec = (AVCodecContext*)laudio_codec;
    154542    audio_passthru = laudio_passthru;
    155543    if (audio_bits != 8 && audio_bits != 16)
    156544    {
     
    169557   
    170558    numlowbuffer = 0;
    171559
     560    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2 sr %3")
     561            .arg(audio_main_device).arg(audio_channels).arg(audio_samplerate));
     562   
    172563    // Actually do the device specific open call
    173564    if (!OpenDevice())
    174565    {
    175566        VERBOSE(VB_AUDIO, LOC_ERR + "Aborting reconfigure");
    176567        pthread_mutex_unlock(&avsync_lock);
    177568        pthread_mutex_unlock(&audio_buflock);
     569        if (GetError().isEmpty())
     570            Error("Aborting reconfigure");
     571        VERBOSE(VB_AUDIO, "Aborting reconfigure");
    178572        return;
    179573    }
    180574
     
    197591    current_seconds = -1;
    198592    source_bitrate = -1;
    199593
     594    // NOTE: this wont do anything as above samplerate vars are set equal
    200595    // Check if we need the resampler
    201596    if (audio_samplerate != laudio_samplerate)
    202597    {
     
    221616
    222617    VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1")
    223618            .arg(audio_stretchfactor));
     619    VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
     620            .arg(audio_codec?codec_id_string(audio_codec->codec_id):"not set"));
    224621
    225     SetStretchFactorLocked(audio_stretchfactor);
    226     if (pSoundStretch)
     622    if (redo_stretch)
    227623    {
    228         pSoundStretch->setSampleRate(audio_samplerate);
    229         pSoundStretch->setChannels(audio_channels);
     624        float laudio_stretchfactor = audio_stretchfactor;
     625        delete pSoundStretch;
     626        pSoundStretch = NULL;
     627        audio_stretchfactor = 0.0;
     628        SetStretchFactorLocked(laudio_stretchfactor);
    230629    }
     630    else
     631    {
     632        SetStretchFactorLocked(audio_stretchfactor);
     633        if (pSoundStretch)
     634        {
     635            // if its passthru then we need to reencode
     636            if (audio_codec)
     637            {
     638                if (!encoder)
     639                {
     640                    VERBOSE(VB_AUDIO, LOC + QString("Creating Encoder for codec %1").arg(audio_codec->codec_id));
     641                    encoder = new DigitalEncoder();
     642                    if (!encoder->Init(audio_codec->codec_id,
     643                                audio_codec->bit_rate,
     644                                audio_codec->sample_rate,
     645                                audio_codec->channels
     646                                ))
     647                    {
     648                        // eeks
     649                        delete encoder;
     650                        encoder = NULL;
     651                        VERBOSE(VB_AUDIO, LOC + QString("Failed to Create Encoder"));
     652                    }
     653                }
     654            }
     655            if (encoder)
     656            {
     657                pSoundStretch->setSampleRate(audio_codec->sample_rate);
     658                pSoundStretch->setChannels(audio_codec->channels);
     659            }
     660            else
     661            {
     662                pSoundStretch->setSampleRate(audio_samplerate);
     663                pSoundStretch->setChannels(audio_channels);
     664            }
     665        }
     666    }
    231667
    232668    // Setup visualisations, zero the visualisations buffers
    233669    prepareVisuals();
     
    273709        pSoundStretch = NULL;
    274710    }
    275711
     712    if (encoder)
     713    {
     714        delete encoder;
     715        encoder = NULL;
     716    }
     717
    276718    CloseDevice();
    277719
    278720    killAudioLock.unlock();
     
    286728
    287729void AudioOutputBase::Pause(bool paused)
    288730{
     731    VERBOSE(VB_AUDIO, LOC+ QString("Pause %0").arg(paused));
    289732    pauseaudio = paused;
    290733    audio_actually_paused = false;
    291734}
     
    368811       The reason is that computing 'audiotime' requires acquiring the audio
    369812       lock, which the video thread should not do. So, we call 'SetAudioTime()'
    370813       from the audio thread, and then call this from the video thread. */
    371     int ret;
     814    long long ret;
    372815    struct timeval now;
    373816
    374817    if (audiotime == 0)
     
    380823
    381824    ret = (now.tv_sec - audiotime_updated.tv_sec) * 1000;
    382825    ret += (now.tv_usec - audiotime_updated.tv_usec) / 1000;
    383     ret = (int)(ret * audio_stretchfactor);
     826    ret = (long long)(ret * audio_stretchfactor);
    384827
     828#if 1
     829    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     830            QString("GetAudiotime now=%1.%2, set=%3.%4, ret=%5, audt=%6 sf=%7")
     831            .arg(now.tv_sec).arg(now.tv_usec)
     832            .arg(audiotime_updated.tv_sec).arg(audiotime_updated.tv_usec)
     833            .arg(ret)
     834            .arg(audiotime)
     835            .arg(audio_stretchfactor)
     836           );
     837#endif
     838
    385839    ret += audiotime;
    386840
    387841    pthread_mutex_unlock(&avsync_lock);
    388     return ret;
     842    return (int)ret;
    389843}
    390844
    391845void AudioOutputBase::SetAudiotime(void)
     
    422876    // include algorithmic latencies
    423877    if (pSoundStretch)
    424878    {
     879        // if encoder is active, then use its idea of audiobytes
     880        //size_t abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample;
     881
     882        // add the effect of any unused but processed samples, AC3 reencode does this
     883        totalbuffer += (int)(pSoundStretch->numSamples() * audio_bytes_per_sample);
    425884        // add the effect of unprocessed samples in time stretch algo
    426885        totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() *
    427886                              audio_bytes_per_sample) / audio_stretchfactor);
    428887    }
    429                
     888
    430889    audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 /
    431890                                   (audio_bytes_per_sample * effdspstretched));
    432891 
    433892    gettimeofday(&audiotime_updated, NULL);
     893#if 1
     894    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     895            QString("SetAudiotime set=%1.%2, audt=%3 atc=%4 tb=%5 sb=%6 eds=%7 abps=%8 sf=%9")
     896            .arg(audiotime_updated.tv_sec).arg(audiotime_updated.tv_usec)
     897            .arg(audiotime)
     898            .arg(audbuf_timecode)
     899            .arg(totalbuffer)
     900            .arg(soundcard_buffer)
     901            .arg(effdspstretched)
     902            .arg(audio_bytes_per_sample)
     903            .arg(audio_stretchfactor)
     904           );
     905#endif
    434906
    435907    pthread_mutex_unlock(&avsync_lock);
    436908    pthread_mutex_unlock(&audio_buflock);
     
    498970    // NOTE: This function is not threadsafe
    499971
    500972    int afree = audiofree(true);
    501     int len = samples * audio_bytes_per_sample;
     973    int len = samples * (encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample);
    502974
    503975    // Check we have enough space to write the data
    504976    if (need_resampler && src_ctx)
     
    509981        VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + QString(
    510982                "AddSamples FAILED bytes=%1, used=%2, free=%3, timecode=%4")
    511983                .arg(len).arg(AUDBUFSIZE-afree).arg(afree)
    512                 .arg(timecode));
    513 
     984                .arg(LONGLONGCONVERT timecode));
    514985        return false; // would overflow
    515986    }
    516987
     
    5471018
    5481019int AudioOutputBase::WaitForFreeSpace(int samples)
    5491020{
    550     int len = samples * audio_bytes_per_sample;
     1021    int abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample;
     1022    int len = samples * abps;
    5511023    int afree = audiofree(false);
    5521024
    5531025    while (len > afree)
    5541026    {
    5551027        if (blocking)
    5561028        {
    557             VERBOSE(VB_AUDIO, LOC + "Waiting for free space");
     1029            VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Waiting for free space");
    5581030            // wait for more space
    5591031            pthread_cond_wait(&audio_bufsig, &audio_buflock);
    5601032            afree = audiofree(false);
    5611033        }
    5621034        else
    5631035        {
    564             VERBOSE(VB_IMPORTANT, LOC_ERR +
    565                     "Audio buffer overflow, audio data lost!");
    566             samples = afree / audio_bytes_per_sample;
    567             len = samples * audio_bytes_per_sample;
     1036            VERBOSE(VB_IMPORTANT, LOC_ERR +
     1037                    QString("Audio buffer overflow, %1 audio samples lost!")
     1038                        .arg(samples-afree / abps));
     1039            samples = afree / abps;
     1040            len = samples * abps;
    5681041            if (src_ctx)
    5691042            {
    5701043                int error = src_reset(src_ctx);
     
    5891062   
    5901063    int afree = audiofree(false);
    5911064
    592     VERBOSE(VB_AUDIO|VB_TIMESTAMP,
    593             LOC + QString("_AddSamples bytes=%1, used=%2, free=%3, timecode=%4")
    594             .arg(samples * audio_bytes_per_sample)
    595             .arg(AUDBUFSIZE-afree).arg(afree).arg((long)timecode));
     1065    int abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample;
     1066    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     1067            LOC + QString("_AddSamples samples=%1 bytes=%2, used=%3, free=%4, timecode=%5")
     1068            .arg(samples)
     1069            .arg(samples * abps)
     1070            .arg(AUDBUFSIZE-afree).arg(afree).arg(LONGLONGCONVERT timecode));
    5961071   
    5971072    len = WaitForFreeSpace(samples);
    5981073
     
    6291104
    6301105    if (pSoundStretch)
    6311106    {
     1107
    6321108        // does not change the timecode, only the number of samples
    6331109        // back to orig pos
    6341110        org_waud = waud;
    6351111        int bdiff = AUDBUFSIZE - org_waud;
    636         int nSamplesToEnd = bdiff/audio_bytes_per_sample;
     1112        int nSamplesToEnd = bdiff/abps;
    6371113        if (bdiff < len)
    6381114        {
    6391115            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)(audiobuffer +
    6401116                                      org_waud), nSamplesToEnd);
    6411117            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)audiobuffer,
    642                                       (len - bdiff) / audio_bytes_per_sample);
     1118                                      (len - bdiff) / abps);
    6431119        }
    6441120        else
    6451121        {
    6461122            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)(audiobuffer +
    647                                       org_waud), len / audio_bytes_per_sample);
     1123                                      org_waud), len / abps);
    6481124        }
    6491125
    650         int newLen = 0;
    651         int nSamples;
    652         len = WaitForFreeSpace(pSoundStretch->numSamples() *
    653                                audio_bytes_per_sample);
    654         do
     1126        if (encoder)
    6551127        {
    656             int samplesToGet = len/audio_bytes_per_sample;
    657             if (samplesToGet > nSamplesToEnd)
     1128            // pull out a packet's worth and reencode it until we dont have enough
     1129            // for any more packets
     1130            soundtouch::SAMPLETYPE* temp_buff =
     1131                (soundtouch::SAMPLETYPE*)encoder->GetFrameBuffer();
     1132            size_t frameSize = encoder->FrameSize()/abps;
     1133            VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     1134                    QString("_AddSamples Enc sfs=%1 bfs=%2 sss=%3")
     1135                    .arg(frameSize)
     1136                    .arg(encoder->FrameSize())
     1137                    .arg(pSoundStretch->numSamples())
     1138                   );
     1139            // process the same number of samples as it creates a full encoded buffer
     1140            // just like before
     1141            while (pSoundStretch->numSamples() >= frameSize)
    6581142            {
    659                 samplesToGet = nSamplesToEnd;   
     1143                int got = pSoundStretch->receiveSamples(temp_buff, frameSize);
     1144                int amount = encoder->Encode(temp_buff);
     1145                VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     1146                        QString("_AddSamples Enc bytes=%1 got=%2 left=%3")
     1147                        .arg(amount)
     1148                        .arg(got)
     1149                        .arg(pSoundStretch->numSamples())
     1150                       );
     1151                if (amount == 0)
     1152                    continue;
     1153                //len = WaitForFreeSpace(amount);
     1154                char * ob = encoder->GetOutBuff();
     1155                if (amount >= bdiff)
     1156                {
     1157                    memcpy(audiobuffer + org_waud, ob, bdiff);
     1158                    ob += bdiff;
     1159                    amount -= bdiff;
     1160                    org_waud = 0;
     1161                }
     1162                if (amount > 0)
     1163                    memcpy(audiobuffer + org_waud, ob, amount);
     1164                bdiff = AUDBUFSIZE - amount;
     1165                org_waud += amount;
    6601166            }
    661 
    662             nSamples = pSoundStretch->receiveSamples((soundtouch::SAMPLETYPE*)
    663                                       (audiobuffer + org_waud), samplesToGet);
    664             if (nSamples == nSamplesToEnd)
     1167        }
     1168        else
     1169        {
     1170            int newLen = 0;
     1171            int nSamples;
     1172            len = WaitForFreeSpace(pSoundStretch->numSamples() *
     1173                                   audio_bytes_per_sample);
     1174            do
    6651175            {
    666                 org_waud = 0;
    667                 nSamplesToEnd = AUDBUFSIZE/audio_bytes_per_sample;
    668             }
    669             else
    670             {
    671                 org_waud += nSamples * audio_bytes_per_sample;
    672                 nSamplesToEnd -= nSamples;
    673             }
     1176                int samplesToGet = len/audio_bytes_per_sample;
     1177                if (samplesToGet > nSamplesToEnd)
     1178                {
     1179                    samplesToGet = nSamplesToEnd;   
     1180                }
    6741181
    675             newLen += nSamples * audio_bytes_per_sample;
    676             len -= nSamples * audio_bytes_per_sample;
    677         } while (nSamples > 0);
     1182                nSamples = pSoundStretch->receiveSamples((soundtouch::SAMPLETYPE*)
     1183                                          (audiobuffer + org_waud), samplesToGet);
     1184                if (nSamples == nSamplesToEnd)
     1185                {
     1186                    org_waud = 0;
     1187                    nSamplesToEnd = AUDBUFSIZE/audio_bytes_per_sample;
     1188                }
     1189                else
     1190                {
     1191                    org_waud += nSamples * audio_bytes_per_sample;
     1192                    nSamplesToEnd -= nSamples;
     1193                }
     1194
     1195                newLen += nSamples * audio_bytes_per_sample;
     1196                len -= nSamples * audio_bytes_per_sample;
     1197            } while (nSamples > 0);
     1198        }
    6781199    }
    6791200
    6801201    waud = org_waud;
     
    7501271            space_on_soundcard = getSpaceOnSoundcard();
    7511272
    7521273            if (space_on_soundcard != last_space_on_soundcard) {
    753                 VERBOSE(VB_AUDIO, LOC + QString("%1 bytes free on soundcard")
     1274                VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + QString("%1 bytes free on soundcard")
    7541275                        .arg(space_on_soundcard));
    7551276                last_space_on_soundcard = space_on_soundcard;
    7561277            }
     
    7631284                    WriteAudio(zeros, fragment_size);
    7641285                } else {
    7651286                    // this should never happen now -dag
    766                     VERBOSE(VB_AUDIO, LOC +
     1287                    VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC +
    7671288                            QString("waiting for space on soundcard "
    7681289                                    "to write zeros: have %1 need %2")
    7691290                            .arg(space_on_soundcard).arg(fragment_size));
     
    7991320        if (fragment_size > audiolen(true))
    8001321        {
    8011322            if (audiolen(true) > 0)  // only log if we're sending some audio
    802                 VERBOSE(VB_AUDIO, LOC +
     1323                VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC +
    8031324                        QString("audio waiting for buffer to fill: "
    8041325                                "have %1 want %2")
    8051326                        .arg(audiolen(true)).arg(fragment_size));
    8061327
    807             VERBOSE(VB_AUDIO, LOC + "Broadcasting free space avail");
     1328            //VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Broadcasting free space avail");
    8081329            pthread_mutex_lock(&audio_buflock);
    8091330            pthread_cond_broadcast(&audio_bufsig);
    8101331            pthread_mutex_unlock(&audio_buflock);
     
    8181339        if (fragment_size > space_on_soundcard)
    8191340        {
    8201341            if (space_on_soundcard != last_space_on_soundcard) {
    821                 VERBOSE(VB_AUDIO, LOC +
     1342                VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC +
    8221343                        QString("audio waiting for space on soundcard: "
    8231344                                "have %1 need %2")
    8241345                        .arg(space_on_soundcard).arg(fragment_size));
     
    8801401
    8811402        /* update raud */
    8821403        raud = (raud + fragment_size) % AUDBUFSIZE;
    883         VERBOSE(VB_AUDIO, LOC + "Broadcasting free space avail");
     1404        //VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Broadcasting free space avail");
    8841405        pthread_cond_broadcast(&audio_bufsig);
    8851406
    8861407        written_size = fragment_size;
  • libs/libmyth/audiooutputalsa.cpp

     
    8686    }
    8787    else
    8888    {
    89         fragment_size = 6144; // nicely divisible by 2,4,6,8 channels @ 16-bits
    90         buffer_time = 500000;  // .5 seconds
     89        //fragment_size = 6144; // nicely divisible by 2,4,6,8 channels @ 16-bits
     90        //fragment_size = 3072*audio_channels; // nicely divisible by 2,4,6,8 channels @ 16-bits
     91        fragment_size = (audio_bits * audio_channels * audio_samplerate) / (8*30);
     92        buffer_time = 100000;  // .5 seconds
    9193        period_time = buffer_time / 4;  // 4 interrupts per buffer
    9294    }
    9395
     
    159161   
    160162    tmpbuf = aubuf;
    161163
    162     VERBOSE(VB_AUDIO, QString("WriteAudio: Preparing %1 bytes (%2 frames)")
     164    VERBOSE(VB_AUDIO|VB_TIMESTAMP, QString("WriteAudio: Preparing %1 bytes (%2 frames)")
    163165            .arg(size).arg(frames));
    164166   
    165167    while (frames > 0)
  • programs/mythfrontend/globalsettings.cpp

     
    3939        dev.setNameFilter("adsp*");
    4040        gc->fillSelectionsFromDir(dev);
    4141    }
     42#ifdef USE_ALSA
     43    gc->addSelection("ALSA:default", "ALSA:default");
     44    gc->addSelection("ALSA:analog", "ALSA:analog");
     45    gc->addSelection("ALSA:digital", "ALSA:digital");
     46    gc->addSelection("ALSA:mixed-analog", "ALSA:mixed-analog");
     47    gc->addSelection("ALSA:mixed-digital", "ALSA:mixed-digital");
     48#endif
     49#ifdef USE_ARTS
     50    gc->addSelection("ARTS:", "ARTS:");
     51#endif
     52#ifdef USE_JACK
     53    gc->addSelection("JACK:output", "JACK:output");
     54#endif
     55    gc->addSelection("NULL", "NULL");
    4256
    4357    return gc;
    4458}
    4559
     60static HostComboBox *MaxAudioChannels()
     61{
     62    HostComboBox *gc = new HostComboBox("MaxChannels",false);
     63    gc->setLabel(QObject::tr("Max Audio Channels"));
     64    //gc->addSelection(QObject::tr("Mono"), "1");
     65    //gc->addSelection(QObject::tr("Stereo L+R"), "2", true); // default
     66    //gc->addSelection(QObject::tr("3 Channel: L C R"), "3");
     67    //gc->addSelection(QObject::tr("4 Channel: L R LS RS"), "4");
     68    //gc->addSelection(QObject::tr("5 Channel: L C R LS RS"), "5");
     69    //gc->addSelection(QObject::tr("6 Channel: L C R LS RS LFE"), "6");
     70    gc->addSelection(QObject::tr("Stereo"), "2", true); // default
     71    gc->addSelection(QObject::tr("6 Channel"), "6");
     72    gc->setHelpText(
     73            QObject::tr("Set the maximum number of audio channels to be decoded. "
     74                "This is for multi-channel/surround audio playback."));
     75    return gc;
     76}
     77
    4678static HostComboBox *PassThroughOutputDevice()
    4779{
    4880    HostComboBox *gc = new HostComboBox("PassThruOutputDevice", true);
     
    23722404#ifdef CONFIG_DTS
    23732405         vgrp0->addChild(DTSPassThrough());
    23742406#endif
     2407         addChild(MaxAudioChannels());
    23752408
    23762409         VerticalConfigurationGroup *vgrp1 =
    23772410             new VerticalConfigurationGroup(false, false, true, true);
  • programs/mythtranscode/transcode.cpp

     
    4646
    4747    // reconfigure sound out for new params
    4848    virtual void Reconfigure(int audio_bits, int audio_channels,
    49                              int audio_samplerate, bool audio_passthru)
     49                             int audio_samplerate, bool audio_passthru,
     50                             void * = NULL)
    5051    {
     52        ClearError();
    5153        (void)audio_samplerate;
    5254        (void)audio_passthru;
    5355        bits = audio_bits;
    5456        channels = audio_channels;
    5557        bytes_per_sample = bits * channels / 8;
     58        if (channels>2)
     59            Error("Invalid channel count");
    5660    }
    5761
    5862    // dsprate is in 100 * samples/second
  • libs/libmythtv/avformatdecoder.h

     
    241241    bool              allow_ac3_passthru;
    242242    bool              allow_dts_passthru;
    243243    bool              disable_passthru;
     244    int               max_channels;
    244245
    245246    AudioInfo         audioIn;
    246247    AudioInfo         audioOut;
  • libs/libmythtv/avformatdecoder.cpp

     
    4444#define MAX_AC3_FRAME_SIZE 6144
    4545
    4646/** Set to zero to allow any number of AC3 channels. */
     47#define MAXCHANNELSELECT 1
     48#if MAXCHANNELSELECT
     49#define MAX_OUTPUT_CHANNELS compiler error
     50#else
    4751#define MAX_OUTPUT_CHANNELS 2
     52#endif
    4853
    4954static int dts_syncinfo(uint8_t *indata_ptr, int *flags,
    5055                        int *sample_rate, int *bit_rate);
     
    298303#ifdef CONFIG_DTS
    299304    allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
    300305#endif
     306    max_channels = gContext->GetNumSetting("MaxChannels", 2);
    301307
    302308    audioIn.sample_size = -32; // force SetupAudioStream to run once
    303309    itv = GetNVP()->GetInteractiveTV();
     
    431437    framesPlayed = lastKey;
    432438    framesRead = lastKey;
    433439
     440    VERBOSE(VB_PLAYBACK, QString("AvFormatDecoder::DoFastForward newframe %5 frame %1 fps %2 ts %3 disc %4 cur_dts %6 adj %7 newts %8 fsa %9")
     441        .arg(desiredFrame)
     442        .arg(fps)
     443        .arg(ts)
     444        .arg(discardFrames)
     445        .arg(framesPlayed)
     446        .arg(st->cur_dts)
     447        .arg(adj_cur_dts)
     448        .arg(newts)
     449        .arg(frameseekadjust)
     450        );
     451
    434452    int normalframes = desiredFrame - framesPlayed;
    435453
     454#if 0
     455    if (!exactseeks)
     456        normalframes = 0;
     457#endif
     458
    436459    SeekReset(lastKey, normalframes, discardFrames, discardFrames);
    437460
    438461    if (discardFrames)
     
    800823
    801824    fmt->flags &= ~AVFMT_NOFILE;
    802825
     826#if 1
     827    if ((m_playbackinfo) || livetv || watchingrecording)
     828    {
     829        const char *name = ic->av_class->item_name(ic);
     830        VERBOSE(VB_GENERAL, QString("libavformat type %1").arg(name));
     831    }
     832#endif
     833 
     834    //struct timeval one, two, res;
     835    //gettimeofday(&one, NULL);
     836
    803837    if (!ringBuffer->isDVD() && !livetv)
    804838        av_estimate_timings(ic);
    805839
     
    810844    if (-1 == ret)
    811845        return ret;
    812846
     847    // make sure its at the real start due to av_find_stream_info reading
     848    ret = av_seek_frame(ic, -1, 0, AVSEEK_FLAG_BACKWARD);
     849    if (ret < 0)
     850        av_seek_frame(ic, -1, 0, AVSEEK_FLAG_BYTE);     // reposition to start of stream
     851
    813852    AutoSelectTracks(); // This is needed for transcoder
    814853
    815854    {
     
    839878    // If we don't have a position map, set up ffmpeg for seeking
    840879    if (!recordingHasPositionMap)
    841880    {
     881        const char *name = ic->av_class->item_name(ic);
    842882        VERBOSE(VB_PLAYBACK, LOC +
    843                 "Recording has no position -- using libavformat seeking.");
     883                QString("Recording has no position -- using libavformat seeking. %1").arg(name));
    844884        int64_t dur = ic->duration / (int64_t)AV_TIME_BASE;
    845885
    846886        if (dur > 0)
     
    885925            QString("Successfully opened decoder for file: "
    886926                    "\"%1\". novideo(%2)").arg(filename).arg(novideo));
    887927
     928    // set initial position correctly
     929    //DoFastForward(0, true);
     930
    888931    // Return true if recording has position map
    889932    return recordingHasPositionMap;
    890933}
     
    12961339                            <<") already open, leaving it alone.");
    12971340                }
    12981341                //assert(enc->codec_id);
     1342                VERBOSE(VB_GENERAL, QString("AVFD: codec %1 has %2 channels").arg(codec_id_string(enc->codec_id)).arg(enc->channels));
     1343#if 0
     1344                if (enc->channels > 2)
     1345                    enc->channels = 2;
     1346#endif
    12991347
     1348#if 0
    13001349                // HACK BEGIN REALLY UGLY HACK FOR DTS PASSTHRU
    13011350                if (enc->codec_id == CODEC_ID_DTS)
    13021351                {
     
    13051354                    // enc->bit_rate = what??;
    13061355                }
    13071356                // HACK END REALLY UGLY HACK FOR DTS PASSTHRU
     1357#endif
    13081358
    13091359                bitrate += enc->bit_rate;
    13101360                break;
     
    25632613
    25642614        AVStream *curstream = ic->streams[pkt->stream_index];
    25652615
     2616#if 0
     2617        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + QString("timecode pts:%1 dts:%2 codec:%3")
     2618                .arg(pkt->pts)
     2619                .arg(pkt->dts)
     2620                .arg((curstream && curstream->codec)?curstream->codec->codec_type:-1)
     2621               );
     2622#endif
     2623
    25662624        if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
    25672625            pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
    25682626
     
    27152773                    if (!curstream->codec->channels)
    27162774                    {
    27172775                        QMutexLocker locker(&avcodeclock);
     2776#if MAXCHANNELSELECT
     2777                        VERBOSE(VB_IMPORTANT, LOC + QString("Setting channels to %1").arg(audioOut.channels));
     2778                        curstream->codec->cqp = max_channels;
     2779                        curstream->codec->channels = audioOut.channels;
     2780#else
    27182781                        curstream->codec->channels = MAX_OUTPUT_CHANNELS;
     2782#endif
    27192783                        ret = avcodec_decode_audio(
    27202784                            curstream->codec, audioSamples,
    27212785                            &data_size, ptr, len);
     
    27692833                    {
    27702834                        AVCodecContext *ctx = curstream->codec;
    27712835
     2836#if MAXCHANNELSELECT
    27722837                        if ((ctx->channels == 0) ||
     2838                            (ctx->channels > audioOut.channels))
     2839                            ctx->channels = audioOut.channels;
     2840#else
     2841                        if ((ctx->channels == 0) ||
    27732842                            (ctx->channels > MAX_OUTPUT_CHANNELS))
    27742843                            ctx->channels = MAX_OUTPUT_CHANNELS;
     2844#endif
    27752845
    27762846                        ret = avcodec_decode_audio(
    27772847                            ctx, audioSamples, &data_size, ptr, len);
     
    30803150
    30813151void AvFormatDecoder::SetDisablePassThrough(bool disable)
    30823152{
     3153#if MAXCHANNELSELECT
     3154    // can only disable never reenable as once tiemstretch is on its on for the session
     3155    if (disable_passthru)
     3156        return;
     3157#endif
    30833158    if (selectedTrack[kTrackTypeAudio].av_stream_index < 0)
    30843159    {
    30853160        disable_passthru = disable;
    30863161        return;
    30873162    }
    3088 
     3163 
    30893164    if (disable != disable_passthru)
    30903165    {
    30913166        disable_passthru = disable;
     
    31123187    AVCodecContext *codec_ctx = NULL;
    31133188    AudioInfo old_in  = audioIn;
    31143189    AudioInfo old_out = audioOut;
     3190    bool using_passthru = false;
    31153191
    31163192    if ((currentTrack[kTrackTypeAudio] >= 0) &&
    31173193        (selectedTrack[kTrackTypeAudio].av_stream_index <= ic->nb_streams) &&
     
    31203196    {
    31213197        assert(curstream);
    31223198        assert(curstream->codec);
    3123         codec_ctx = curstream->codec;       
     3199        codec_ctx = curstream->codec;
    31243200        bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
    3125                                 !disable_passthru &&
    31263201                                (codec_ctx->codec_id == CODEC_ID_AC3));
    31273202        bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
    3128                                 !disable_passthru &&
    31293203                                (codec_ctx->codec_id == CODEC_ID_DTS));
     3204        using_passthru = do_ac3_passthru || do_dts_passthru;
    31303205        info = AudioInfo(codec_ctx->codec_id,
    31313206                         codec_ctx->sample_rate, codec_ctx->channels,
    3132                          do_ac3_passthru || do_dts_passthru);
     3207                         using_passthru && !disable_passthru);
    31333208    }
    31343209
    31353210    if (info == audioIn)
    31363211        return false; // no change
    31373212
     3213    QString ptmsg = "";
     3214    if (using_passthru)
     3215    {
     3216        ptmsg = QString(" using passthru");
     3217    }
    31383218    VERBOSE(VB_AUDIO, LOC + "Initializing audio parms from " +
    31393219            QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
    31403220
    31413221    audioOut = audioIn = info;
     3222#if MAXCHANNELSELECT
     3223    if (using_passthru)
     3224#else
    31423225    if (audioIn.do_passthru)
     3226#endif
    31433227    {
    31443228        // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
    3145         audioOut.channels    = 2;
    3146         audioOut.sample_rate = 48000;
    3147         audioOut.sample_size = 4;
     3229        AudioInfo digInfo = audioOut;
     3230        if (!disable_passthru)
     3231        {
     3232            digInfo.channels    = 2;
     3233            digInfo.sample_rate = 48000;
     3234            digInfo.sample_size = 4;
     3235        }
     3236        if (audioOut.channels > max_channels)
     3237        {
     3238            audioOut.channels = max_channels;
     3239            audioOut.sample_size = audioOut.channels * 2;
     3240            codec_ctx->channels = audioOut.channels;
     3241        }
     3242#if MAXCHANNELSELECT
     3243        VERBOSE(VB_AUDIO, LOC + "Audio format changed digital passthrough " +
     3244                QString("%1\n\t\t\tfrom %2 ; %3\n\t\t\tto   %4 ; %5")
     3245                .arg(digInfo.toString())
     3246                .arg(old_in.toString()).arg(old_out.toString())
     3247                .arg(audioIn.toString()).arg(audioOut.toString()));
     3248
     3249        if (digInfo.sample_rate > 0)
     3250            GetNVP()->SetEffDsp(digInfo.sample_rate * 100);
     3251
     3252        //GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
     3253        //                         audioOut.sample_rate);
     3254        GetNVP()->SetAudioParams(digInfo.bps(), digInfo.channels,
     3255                                 digInfo.sample_rate, audioIn.do_passthru);
     3256        // allow the audio stuff to reencode
     3257        GetNVP()->SetAudioCodec(codec_ctx);
     3258        GetNVP()->ReinitAudio();
     3259        return true;
     3260#endif
    31483261    }
     3262#if MAXCHANNELSELECT
    31493263    else
    31503264    {
     3265        if (audioOut.channels > max_channels)
     3266        {
     3267            audioOut.channels = max_channels;
     3268            audioOut.sample_size = audioOut.channels * 2;
     3269            codec_ctx->channels = audioOut.channels;
     3270        }
     3271    }
     3272    bool audiook;
     3273#if 0
     3274    do
     3275    {
     3276#endif
     3277#else
     3278    else
     3279    {
    31513280        if (audioOut.channels > MAX_OUTPUT_CHANNELS)
    31523281        {
    31533282            audioOut.channels = MAX_OUTPUT_CHANNELS;
     
    31553284            codec_ctx->channels = MAX_OUTPUT_CHANNELS;
    31563285        }
    31573286    }
     3287#endif
    31583288
    31593289    VERBOSE(VB_AUDIO, LOC + "Audio format changed " +
    31603290            QString("\n\t\t\tfrom %1 ; %2\n\t\t\tto   %3 ; %4")
     
    31673297    GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
    31683298                             audioOut.sample_rate,
    31693299                             audioIn.do_passthru);
    3170     GetNVP()->ReinitAudio();
     3300    // allow the audio stuff to reencode
     3301    GetNVP()->SetAudioCodec(using_passthru?codec_ctx:NULL);
     3302    QString errMsg = GetNVP()->ReinitAudio();
     3303#if MAXCHANNELSELECT
     3304        audiook = errMsg.isEmpty();
     3305#if 0
     3306        if (!audiook)
     3307        {
     3308            switch (audioOut.channels)
     3309            {
     3310#if 0
     3311                case 8:
     3312                    audioOut.channels = 6;
     3313                    break;
     3314#endif
     3315                case 6:
     3316#if 0
     3317                    audioOut.channels = 5;
     3318                    break;
     3319                case 5:
     3320                    audioOut.channels = 4;
     3321                    break;
     3322                case 4:
     3323                    audioOut.channels = 3;
     3324                    break;
     3325                case 3:
     3326#endif
     3327                    audioOut.channels = 2;
     3328                    break;
     3329#if 0
     3330                case 2:
     3331                    audioOut.channels = 1;
     3332                    break;
     3333#endif
     3334                default:
     3335                    // failed to reconfigure under any circumstances
     3336                    audiook = true;
     3337                    audioOut.channels = 0;
     3338                    break;
     3339            }
     3340            audioOut.sample_size = audioOut.channels * 2;
     3341            codec_ctx->channels = audioOut.channels;
     3342        }
     3343    } while (!audiook);
     3344#endif
     3345#endif
    31713346
    31723347    return true;
    31733348}
  • libs/libmythtv/NuppelVideoPlayer.h

     
    123123    void SetAudioInfo(const QString &main, const QString &passthru, uint rate);
    124124    void SetAudioParams(int bits, int channels, int samplerate, bool passthru);
    125125    void SetEffDsp(int dsprate);
     126    void SetAudioCodec(void *ac);
    126127
    127128    // Sets
    128129    void SetParentWidget(QWidget *widget)     { parentWidget = widget; }
     
    196197    bool    IsDecoderThreadAlive(void) const  { return decoder_thread_alive; }
    197198    bool    IsReallyNearEnd(void) const;
    198199    bool    IsNearEnd(long long framesRemaining = -1) const;
     200    float   GetAudioStretchFactor() { return audio_stretchfactor; }
    199201    bool    PlayingSlowForPrebuffer(void) const { return m_playing_slower; }
    200202    bool    HasAudioIn(void) const            { return !no_audio_in; }
    201203    bool    HasAudioOut(void) const           { return !no_audio_out; }
     
    226228    bool Play(float speed = 1.0, bool normal = true,
    227229              bool unpauseaudio = true);
    228230    bool GetPause(void) const;
     231    float GetNextPlaySpeed() { return next_play_speed; }
    229232
    230233    // Seek stuff
    231234    bool FastForward(float seconds);
     
    659662    int      audio_bits;
    660663    int      audio_samplerate;
    661664    float    audio_stretchfactor;
     665    void     *audio_codec;
    662666    bool     audio_passthru;
    663667
    664668    // Picture-in-Picture
  • libs/libmythtv/NuppelVideoPlayer.cpp

     
    202202      audio_passthru_device(QString::null),
    203203      audio_channels(2),            audio_bits(-1),
    204204      audio_samplerate(44100),      audio_stretchfactor(1.0f),
     205      audio_codec(NULL),
    205206      // Picture-in-Picture
    206207      pipplayer(NULL), setpipplayer(NULL), needsetpipplayer(false),
    207208      // Preview window support
     
    681682    if (audioOutput)
    682683    {
    683684        audioOutput->Reconfigure(audio_bits, audio_channels,
    684                                  audio_samplerate, audio_passthru);
     685                                 audio_samplerate, audio_passthru,
     686                                 audio_codec);
    685687        errMsg = audioOutput->GetError();
    686688        if (!errMsg.isEmpty())
    687689            audioOutput->SetStretchFactor(audio_stretchfactor);
     
    21152117            // decoding; display the frame, but don't wait for A/V Sync.
    21162118            videoOutput->PrepareFrame(buffer, kScan_Intr2ndField);
    21172119            videoOutput->Show(kScan_Intr2ndField);
    2118             VERBOSE(VB_PLAYBACK, LOC + dbg + "skipping A/V wait.");
     2120            VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + dbg + "skipping A/V wait.");
    21192121        }
    21202122        else
    21212123        {
    21222124            // If we are using software decoding, skip this frame altogether.
     2125            //VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + dbg + "dropping frame.");
    21232126            VERBOSE(VB_PLAYBACK, LOC + dbg + "dropping frame to catch up.");
    21242127        }
    21252128    }
     
    21292132        if (buffer)
    21302133            videoOutput->PrepareFrame(buffer, ps);
    21312134
     2135        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString("AVSync waitforframe %1 %2").arg(avsync_adjustment).arg(m_double_framerate));
    21322136        videosync->WaitForFrame(avsync_adjustment);
     2137        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, "AVSync show");
    21332138        if (!resetvideo)
    21342139            videoOutput->Show(ps);
    21352140
     
    21512156
    21522157            // Display the second field
    21532158            videosync->AdvanceTrigger();
    2154             videosync->WaitForFrame(0);
     2159            videosync->WaitForFrame(avsync_adjustment);
    21552160            if (!resetvideo)
    21562161            {
    21572162                videoOutput->Show(ps);
     
    21792184        // If audio is way behind of video, adjust for it...
    21802185        // by cutting the frame rate in half for the length of this frame
    21812186
    2182         avsync_adjustment = frame_interval;
     2187        //avsync_adjustment = frame_interval;
     2188        avsync_adjustment = refreshrate;
    21832189        lastsync = true;
     2190        //VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC +
     2191        //        QString("Video is %1 frames ahead of audio,\n"
     2192        //                "\t\t\tdoubling video frame interval.").arg(diverge));
    21842193        VERBOSE(VB_PLAYBACK, LOC +
    21852194                QString("Video is %1 frames ahead of audio,\n"
    21862195                        "\t\t\tdoubling video frame interval to slow down.").arg(diverge));
     
    21892198    if (audioOutput && normal_speed)
    21902199    {
    21912200        long long currentaudiotime = audioOutput->GetAudiotime();
    2192 #if 0
     2201#if 1
    21932202        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString(
    21942203                    "A/V timecodes audio %1 video %2 frameinterval %3 "
    21952204                    "avdel %4 avg %5 tcoffset %6")
     
    25142523
    25152524    usevideotimebase = gContext->GetNumSetting("UseVideoTimebase", 0);
    25162525
    2517     if ((print_verbose_messages & VB_PLAYBACK) != 0)
     2526    if ((print_verbose_messages & VB_PLAYBACK|VB_TIMESTAMP) == (VB_PLAYBACK|VB_TIMESTAMP))
    25182527        output_jmeter = new Jitterometer("video_output", 100);
    25192528    else
    25202529        output_jmeter = NULL;
     
    25602569            {
    25612570                VERBOSE(VB_IMPORTANT, "Video sync method can't support double "
    25622571                        "framerate (refresh rate too low for bob deint)");
     2572                //m_scan = kScan_Ignore;
     2573                //m_can_double = false;
    25632574                FallbackDeint();
    25642575            }
    25652576        }
     
    34073418    audio_passthru = passthru;
    34083419}
    34093420
     3421void NuppelVideoPlayer::SetAudioCodec(void* ac)
     3422{
     3423    audio_codec = ac;
     3424}
     3425
    34103426void NuppelVideoPlayer::SetEffDsp(int dsprate)
    34113427{
    34123428    if (audioOutput)
     
    34513467        tc_avcheck_framecounter++;
    34523468        if (tc_avcheck_framecounter == 30)
    34533469        {
    3454 #define AUTO_RESYNC 1
     3470#define AUTO_RESYNC 0
    34553471#if AUTO_RESYNC
    34563472            // something's terribly, terribly wrong.
    34573473            if (tc_lastval[TC_AUDIO] < tc_lastval[TC_VIDEO] - 10000000 ||
  • libs/libavcodec/a52dec.c

     
    142142    }
    143143}
    144144
     145static inline int16_t convert (int32_t i)
     146{
     147    if (i > 0x43c07fff)
     148        return 32767;
     149    else if (i < 0x43bf8000)
     150        return -32768;
     151    else
     152        return i - 0x43c00000;
     153}
     154
     155void float2s16_2 (float * _f, int16_t * s16)
     156{
     157    int i;
     158    int32_t * f = (int32_t *) _f;
     159
     160    for (i = 0; i < 256; i++) {
     161        s16[2*i] = convert (f[i]);
     162        s16[2*i+1] = convert (f[i+256]);
     163    }
     164}
     165
     166void float2s16_4 (float * _f, int16_t * s16)
     167{
     168    int i;
     169    int32_t * f = (int32_t *) _f;
     170
     171    for (i = 0; i < 256; i++) {
     172        s16[4*i] = convert (f[i]);
     173        s16[4*i+1] = convert (f[i+256]);
     174        s16[4*i+2] = convert (f[i+512]);
     175        s16[4*i+3] = convert (f[i+768]);
     176    }
     177}
     178
     179void float2s16_5 (float * _f, int16_t * s16)
     180{
     181    int i;
     182    int32_t * f = (int32_t *) _f;
     183
     184    for (i = 0; i < 256; i++) {
     185        s16[5*i] = convert (f[i]);
     186        s16[5*i+1] = convert (f[i+256]);
     187        s16[5*i+2] = convert (f[i+512]);
     188        s16[5*i+3] = convert (f[i+768]);
     189        s16[5*i+4] = convert (f[i+1024]);
     190    }
     191}
     192
     193int channels_multi (int flags)
     194{
     195    if (flags & A52_LFE)
     196        return 6;
     197    else if (flags & 1) /* center channel */
     198        return 5;
     199    else if ((flags & A52_CHANNEL_MASK) == A52_2F2R)
     200        return 4;
     201    else
     202        return 2;
     203}
     204
     205void float2s16_multi (float * _f, int16_t * s16, int flags)
     206{
     207    int i;
     208    int32_t * f = (int32_t *) _f;
     209
     210    switch (flags) {
     211    case A52_MONO:
     212        for (i = 0; i < 256; i++) {
     213            s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
     214            s16[5*i+4] = convert (f[i]);
     215        }
     216        break;
     217    case A52_CHANNEL:
     218    case A52_STEREO:
     219    case A52_DOLBY:
     220        float2s16_2 (_f, s16);
     221        break;
     222    case A52_3F:
     223        for (i = 0; i < 256; i++) {
     224            s16[5*i] = convert (f[i]);
     225            s16[5*i+1] = convert (f[i+512]);
     226            s16[5*i+2] = s16[5*i+3] = 0;
     227            s16[5*i+4] = convert (f[i+256]);
     228        }
     229        break;
     230    case A52_2F2R:
     231        float2s16_4 (_f, s16);
     232        break;
     233    case A52_3F2R:
     234        float2s16_5 (_f, s16);
     235        break;
     236    case A52_MONO | A52_LFE:
     237        for (i = 0; i < 256; i++) {
     238            s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
     239            s16[6*i+4] = convert (f[i+256]);
     240            s16[6*i+5] = convert (f[i]);
     241        }
     242        break;
     243    case A52_CHANNEL | A52_LFE:
     244    case A52_STEREO | A52_LFE:
     245    case A52_DOLBY | A52_LFE:
     246        for (i = 0; i < 256; i++) {
     247            s16[6*i] = convert (f[i+256]);
     248            s16[6*i+1] = convert (f[i+512]);
     249            s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
     250            s16[6*i+5] = convert (f[i]);
     251        }
     252        break;
     253    case A52_3F | A52_LFE:
     254        for (i = 0; i < 256; i++) {
     255            s16[6*i] = convert (f[i+256]);
     256            s16[6*i+1] = convert (f[i+768]);
     257            s16[6*i+2] = s16[6*i+3] = 0;
     258            s16[6*i+4] = convert (f[i+512]);
     259            s16[6*i+5] = convert (f[i]);
     260        }
     261        break;
     262    case A52_2F2R | A52_LFE:
     263        for (i = 0; i < 256; i++) {
     264            s16[6*i] = convert (f[i+256]);
     265            s16[6*i+1] = convert (f[i+512]);
     266            s16[6*i+2] = convert (f[i+768]);
     267            s16[6*i+3] = convert (f[i+1024]);
     268            s16[6*i+4] = 0;
     269            s16[6*i+5] = convert (f[i]);
     270        }
     271        break;
     272    case A52_3F2R | A52_LFE:
     273        for (i = 0; i < 256; i++) {
     274            s16[6*i] = convert (f[i+256]);
     275            s16[6*i+1] = convert (f[i+768]);
     276            s16[6*i+2] = convert (f[i+1024]);
     277            s16[6*i+3] = convert (f[i+1280]);
     278            s16[6*i+4] = convert (f[i+512]);
     279            s16[6*i+5] = convert (f[i]);
     280        }
     281        break;
     282    }
     283}
     284
     285
    145286/**** end */
    146287
    147288#define HEADER_SIZE 7
     
    183324                    /* update codec info */
    184325                    avctx->sample_rate = sample_rate;
    185326                    s->channels = ac3_channels[s->flags & 7];
     327                    if (avctx->cqp >= 0)
     328                        avctx->channels = avctx->cqp;
    186329                    if (s->flags & A52_LFE)
    187330                        s->channels++;
    188331                    if (avctx->channels == 0)
     
    205348            s->inbuf_ptr += len;
    206349            buf_size -= len;
    207350        } else {
     351            int chans;
    208352            flags = s->flags;
    209353            if (avctx->channels == 1)
    210354                flags = A52_MONO;
    211             else if (avctx->channels == 2)
    212                 flags = A52_STEREO;
     355            else if (avctx->channels == 2) {
     356                if (s->channels>2)
     357                    flags = A52_DOLBY;
     358                else
     359                    flags = A52_STEREO;
     360            }
    213361            else
    214362                flags |= A52_ADJUST_LEVEL;
    215363            level = 1;
     364            chans = channels_multi(flags);
    216365            if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
    217366            fail:
    218367                s->inbuf_ptr = s->inbuf;
     
    222371            for (i = 0; i < 6; i++) {
    223372                if (s->a52_block(s->state))
    224373                    goto fail;
    225                 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
     374                //float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
     375                float2s16_multi(s->samples, out_samples + i * 256 * chans, flags);
    226376            }
    227377            s->inbuf_ptr = s->inbuf;
    228378            s->frame_size = 0;
    229             *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
     379            //*data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
     380            *data_size = 6 * chans * 256 * sizeof(int16_t);
    230381            break;
    231382        }
    232383    }
  • libs/libavcodec/ac3enc.c

     
    13361336{
    13371337    AC3EncodeContext *s = avctx->priv_data;
    13381338    int16_t *samples = data;
     1339    // expects L C R LS RS LFE
     1340    // audio format is L R LS RS C LFE
     1341    static int channel_index[6] = { 0, 4, 1, 2, 3, 5 };
     1342    /*
     1343     * A52->Analog->AC3Enc
     1344     * 1->0->0
     1345     * 3->1->2
     1346     * 4->2->3
     1347     * 5->3->4
     1348     * 2->4->1
     1349     * 0->5->5
     1350     */
    13391351    int i, j, k, v, ch;
    13401352    int16_t input_samples[N];
    13411353    int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
     
    13561368            /* compute input samples */
    13571369            memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t));
    13581370            sinc = s->nb_all_channels;
    1359             sptr = samples + (sinc * (N/2) * i) + ch;
     1371            sptr = samples + (sinc * (N/2) * i) + channel_index[ch];
    13601372            for(j=0;j<N/2;j++) {
    13611373                v = *sptr;
    13621374                input_samples[j + N/2] = v;