Ticket #1104: mythtv_ac3.25.patch

File mythtv_ac3.25.patch, 66.8 KB (added by Mark Spieth, 18 years ago)
  • libs/libmyth/libmyth.pro

     
    3535SOURCES += virtualkeyboard.cpp mythobservable.cpp mythsocket.cpp
    3636
    3737INCLUDEPATH += ../libmythsamplerate ../libmythsoundtouch ../.. ../
     38INCLUDEPATH += ../libavutil
    3839DEPENDPATH += ../libmythsamplerate ../libmythsoundtouch ../ ../libmythui
     40DEPENDPATH += ../libavutil ../libavcodec
    3941
    4042LIBS += -L../libmythsamplerate -lmythsamplerate-$${LIBVERSION}
    4143LIBS += -L../libmythsoundtouch -lmythsoundtouch-$${LIBVERSION}
     44LIBS += -L../libavcodec -lmythavcodec-$${LIBVERSION}
     45LIBS += -la52
    4246
    4347isEmpty(QMAKE_EXTENSION_SHLIB) {
    4448  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);
     
    125134    bool audio_passthru;
    126135
    127136    float audio_stretchfactor;
     137    AVCodecContext *audio_codec;
    128138    AudioOutputSource source;
    129139
    130140    bool killaudio;
     
    133143    bool set_initial_vol;
    134144    bool buffer_output_data_for_use; //  used by AudioOutputNULL
    135145   
     146    int configured_audio_channels;
     147
    136148 private:
    137149    // resampler
    138150    bool need_resampler;
     
    144156
    145157    // timestretch
    146158    soundtouch::SoundTouch * pSoundStretch;
     159    DigitalEncoder * encoder;
    147160
    148161    bool blocking; // do AddSamples calls block?
    149162
     
    160173
    161174    pthread_mutex_t avsync_lock; /* must hold avsync_lock to read or write
    162175                                    'audiotime' and 'audiotime_updated' */
    163     int audiotime; // timecode of audio leaving the soundcard (same units as
     176    long long audiotime; // timecode of audio leaving the soundcard (same units as
    164177                   //                                          timecodes) ...
    165178    struct timeval audiotime_updated; // ... which was last updated at this time
    166179
    167180    /* Audio circular buffer */
    168181    unsigned char audiobuffer[AUDBUFSIZE];  /* buffer */
    169182    int raud, waud;     /* read and write positions */
    170     int audbuf_timecode;    /* timecode of audio most recently placed into
     183    long long audbuf_timecode;    /* timecode of audio most recently placed into
    171184                   buffer */
    172185
    173186    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#include <a52dec/a52.h>
     23}
     24
     25#if QT_VERSION < 0x030200
     26#define LONGLONGCONVERT (long)
     27#else
     28#define LONGLONGCONVERT
     29#endif
     30
     31#define LOC QString("DEnc: ");
     32#define MAX_AC3_FRAME_SIZE 6144
     33class DigitalEncoder
     34{
     35public:
     36    DigitalEncoder();
     37    ~DigitalEncoder();
     38    void Dispose();
     39    bool Init(CodecID codec_id, int bitrate, int samplerate, int channels);
     40    size_t Encode(short * buff);
     41
     42    // if needed
     43    char * GetFrameBuffer()
     44    {
     45        if (!frame_buffer && av_context)
     46        {
     47            frame_buffer = new char [one_frame_bytes];
     48        }
     49        return frame_buffer;
     50    }   
     51    size_t FrameSize() const { return one_frame_bytes; }
     52    char * GetOutBuff() const { return outbuf; }
     53
     54    size_t audio_bytes_per_sample;
     55private:
     56    AVCodecContext *av_context;
     57    char * outbuf;
     58    char * frame_buffer;
     59    int outbuf_size;
     60    size_t one_frame_bytes;
     61};
     62
     63DigitalEncoder::DigitalEncoder()
     64{
     65    av_context = NULL;
     66    outbuf = NULL;
     67    outbuf_size = 0;
     68    one_frame_bytes = 0;
     69    frame_buffer = NULL;
     70}
     71
     72DigitalEncoder::~DigitalEncoder()
     73{
     74    Dispose();
     75}
     76
     77void DigitalEncoder::Dispose()
     78{
     79    if (av_context)
     80    {
     81        avcodec_close(av_context);
     82        av_free(av_context);
     83        av_context = NULL;
     84    }
     85    if (outbuf)
     86    {
     87        delete [] outbuf;
     88        outbuf = NULL;
     89        outbuf_size = 0;
     90    }
     91    if (frame_buffer)
     92    {
     93        delete [] frame_buffer;
     94        frame_buffer = NULL;
     95        one_frame_bytes = 0;
     96    }
     97}
     98
     99//CODEC_ID_AC3
     100bool DigitalEncoder::Init(CodecID codec_id, int bitrate, int samplerate, int channels)
     101{
     102    AVCodec * codec;
     103    int ret;
     104
     105    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init codecid=%1, br=%2, sr=%3, ch=%4")
     106            .arg(codec_id_string(codec_id))
     107            .arg(bitrate)
     108            .arg(samplerate)
     109            .arg(channels));
     110    //codec = avcodec_find_encoder(codec_id);
     111    // always AC3 as there is no DTS encoder at the moment 2005/1/9
     112    codec = avcodec_find_encoder(CODEC_ID_AC3);
     113    if (!codec)
     114    {
     115        VERBOSE(VB_IMPORTANT,"Error: could not find codec");
     116        return false;
     117    }
     118    av_context = avcodec_alloc_context();
     119    av_context->bit_rate = bitrate;
     120    av_context->sample_rate = samplerate;
     121    av_context->channels = channels;
     122    // open it */
     123    if ((ret = avcodec_open(av_context, codec)) < 0)
     124    {
     125        VERBOSE(VB_IMPORTANT,"Error: could not open codec, invalid bitrate or samplerate");
     126        Dispose();
     127        return false;
     128    }
     129
     130    size_t bytes_per_frame = av_context->channels * sizeof(short);
     131    audio_bytes_per_sample = bytes_per_frame;
     132    one_frame_bytes = bytes_per_frame * av_context->frame_size;
     133
     134    outbuf_size = 16384;    // ok for AC3 but DTS?
     135    outbuf = new char [outbuf_size];
     136    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
     137            .arg(av_context->frame_size)
     138            .arg(bytes_per_frame)
     139            .arg(one_frame_bytes)
     140           );
     141
     142    return true;
     143}
     144
     145static int DTS_SAMPLEFREQS[16] =
     146{
     147    0,      8000,   16000,  32000,  64000,  128000, 11025,  22050,
     148    44100,  88200,  176400, 12000,  24000,  48000,  96000,  192000
     149};
     150
     151static int DTS_BITRATES[30] =
     152{
     153    32000,    56000,    64000,    96000,    112000,   128000,
     154    192000,   224000,   256000,   320000,   384000,   448000,
     155    512000,   576000,   640000,   768000,   896000,   1024000,
     156    1152000,  1280000,  1344000,  1408000,  1411200,  1472000,
     157    1536000,  1920000,  2048000,  3072000,  3840000,  4096000
     158};
     159
     160static int dts_decode_header(uint8_t *indata_ptr, int *rate,
     161                             int *nblks, int *sfreq)
     162{
     163    uint id = ((indata_ptr[0] << 24) | (indata_ptr[1] << 16) |
     164               (indata_ptr[2] << 8)  | (indata_ptr[3]));
     165
     166    if (id != 0x7ffe8001)
     167        return -1;
     168
     169    int ftype = indata_ptr[4] >> 7;
     170
     171    int surp = (indata_ptr[4] >> 2) & 0x1f;
     172    surp = (surp + 1) % 32;
     173
     174    *nblks = (indata_ptr[4] & 0x01) << 6 | (indata_ptr[5] >> 2);
     175    ++*nblks;
     176
     177    int fsize = (indata_ptr[5] & 0x03) << 12 |
     178                (indata_ptr[6]         << 4) | (indata_ptr[7] >> 4);
     179    ++fsize;
     180
     181    *sfreq = (indata_ptr[8] >> 2) & 0x0f;
     182    *rate = (indata_ptr[8] & 0x03) << 3 | ((indata_ptr[9] >> 5) & 0x07);
     183
     184    if (ftype != 1)
     185    {
     186        VERBOSE(VB_IMPORTANT, LOC +
     187                QString("DTS: Termination frames not handled (ftype %1)")
     188                .arg(ftype));
     189        return -1;
     190    }
     191
     192    if (*sfreq != 13)
     193    {
     194        VERBOSE(VB_IMPORTANT, LOC +
     195                QString("DTS: Only 48kHz supported (sfreq %1)").arg(*sfreq));
     196        return -1;
     197    }
     198
     199    if ((fsize > 8192) || (fsize < 96))
     200    {
     201        VERBOSE(VB_IMPORTANT, LOC +
     202                QString("DTS: fsize: %1 invalid").arg(fsize));
     203        return -1;
     204    }
     205
     206    if (*nblks != 8 && *nblks != 16 && *nblks != 32 &&
     207        *nblks != 64 && *nblks != 128 && ftype == 1)
     208    {
     209        VERBOSE(VB_IMPORTANT, LOC +
     210                QString("DTS: nblks %1 not valid for normal frame")
     211                .arg(*nblks));
     212        return -1;
     213    }
     214
     215    return fsize;
     216}
     217
     218static int dts_syncinfo(uint8_t *indata_ptr, int * /*flags*/,
     219                        int *sample_rate, int *bit_rate)
     220{
     221    int nblks;
     222    int rate;
     223    int sfreq;
     224
     225    int fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
     226    if (fsize >= 0)
     227    {
     228        if (rate >= 0 && rate <= 29)
     229            *bit_rate = DTS_BITRATES[rate];
     230        else
     231            *bit_rate = 0;
     232        if (sfreq >= 1 && sfreq <= 15)
     233            *sample_rate = DTS_SAMPLEFREQS[sfreq];
     234        else
     235            *sample_rate = 0;
     236    }
     237    return fsize;
     238}
     239
     240static int encode_frame(
     241        bool dts,
     242        unsigned char *data,
     243        size_t &len)
     244{
     245    size_t enc_len;
     246    int flags, sample_rate, bit_rate;
     247
     248    // we don't do any length/crc validation of the AC3 frame here; presumably
     249    // the receiver will have enough sense to do that.  if someone has a
     250    // receiver that doesn't, here would be a good place to put in a call
     251    // to a52_crc16_block(samples+2, data_size-2) - but what do we do if the
     252    // packet is bad?  we'd need to send something that the receiver would
     253    // ignore, and if so, may as well just assume that it will ignore
     254    // anything with a bad CRC...
     255
     256    uint nr_samples = 0, block_len;
     257    if (dts)
     258    {
     259        enc_len = dts_syncinfo(data+8, &flags, &sample_rate, &bit_rate);
     260        int rate, sfreq, nblks;
     261        dts_decode_header(data+8, &rate, &nblks, &sfreq);
     262        nr_samples = nblks * 32;
     263        block_len = nr_samples * 2 * 2;
     264    }
     265    else
     266    {
     267        enc_len = a52_syncinfo(data+8, &flags, &sample_rate, &bit_rate);
     268        block_len = MAX_AC3_FRAME_SIZE;
     269    }
     270
     271    if (enc_len == 0 || enc_len > len)
     272    {
     273        int l = len;
     274        len = 0;
     275        return l;
     276    }
     277
     278    enc_len = min((uint)enc_len, block_len - 8);
     279
     280    //uint32_t x = *(uint32_t*)(data+8);
     281    // in place swab
     282    swab(data+8, data+8, enc_len);
     283    //VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     284    //        QString("DigitalEncoder::Encode swab test %1 %2")
     285    //        .arg(x,0,16).arg(*(uint32_t*)(data+8),0,16));
     286
     287    // the following values come from libmpcodecs/ad_hwac3.c in mplayer.
     288    // they form a valid IEC958 AC3 header.
     289    data[0] = 0x72;
     290    data[1] = 0xF8;
     291    data[2] = 0x1F;
     292    data[3] = 0x4E;
     293    data[4] = 0x01;
     294    if (dts)
     295    {
     296        switch(nr_samples)
     297        {
     298            case 512:
     299                data[4] = 0x0B;      /* DTS-1 (512-sample bursts) */
     300                break;
     301
     302            case 1024:
     303                data[4] = 0x0C;      /* DTS-2 (1024-sample bursts) */
     304                break;
     305
     306            case 2048:
     307                data[4] = 0x0D;      /* DTS-3 (2048-sample bursts) */
     308                break;
     309
     310            default:
     311                VERBOSE(VB_IMPORTANT, LOC +
     312                        QString("DTS: %1-sample bursts not supported")
     313                        .arg(nr_samples));
     314                data[4] = 0x00;
     315                break;
     316        }
     317    }
     318    data[5] = 0x00;
     319    data[6] = (enc_len << 3) & 0xFF;
     320    data[7] = (enc_len >> 5) & 0xFF;
     321    memset(data + 8 + enc_len, 0, block_len - 8 - enc_len);
     322    len = block_len;
     323
     324    return enc_len;
     325}
     326
     327// must have exactly 1 frames worth of data
     328size_t DigitalEncoder::Encode(short * buff)
     329{
     330    int encsize = 0;
     331    size_t outsize = 0;
     332 
     333    // put data in the correct spot for encode frame
     334    outsize = avcodec_encode_audio(
     335                av_context,
     336                ((uchar*)outbuf)+8,
     337                outbuf_size-8,
     338                buff);
     339    size_t tmpsize = outsize;
     340
     341    outsize = MAX_AC3_FRAME_SIZE;
     342    encsize = encode_frame(
     343            //av_context->codec_id==CODEC_ID_DTS,
     344            false,
     345            (unsigned char*)outbuf, outsize);
     346    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     347            QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
     348                .arg(tmpsize)
     349                .arg(encsize)
     350                .arg(outsize)
     351           );
     352
     353    return outsize;
     354}
     355#undef LOC
    19356#define LOC QString("AO: ")
    20357#define LOC_ERR QString("AO, ERROR: ")
    21358
     
    24361    int     /*laudio_bits*/,       int               /*laudio_channels*/,
    25362    int     /*laudio_samplerate*/, AudioOutputSource lsource,
    26363    bool    lset_initial_vol,      bool              /*laudio_passthru*/) :
    27 
    28364    effdsp(0),                  effdspstretched(0),
    29365    audio_channels(-1),         audio_bytes_per_sample(0),
    30366    audio_bits(-1),             audio_samplerate(-1),
     
    35371    audio_passthru_device(QDeepCopy<QString>(laudio_passthru_device)),
    36372    audio_passthru(false),      audio_stretchfactor(1.0f),
    37373
     374    audio_codec(NULL),
    38375    source(lsource),            killaudio(false),
    39376
    40377    pauseaudio(false),          audio_actually_paused(false),
     
    46383
    47384    src_ctx(NULL),
    48385
    49     pSoundStretch(NULL),        blocking(false),
     386    pSoundStretch(NULL),       
     387    encoder(NULL),
     388    blocking(false),
    50389
    51390    lastaudiolen(0),            samples_buffered(0),
    52391    audiotime(0),
     
    61400    pthread_cond_init(&audio_bufsig, NULL);
    62401
    63402    output_audio = 0; // TODO FIXME Not POSIX compatible!
     403    configured_audio_channels = gContext->GetNumSetting("MaxChannels", 2);
    64404
    65405    bzero(&src_data,          sizeof(SRC_DATA));
    66406    bzero(src_in,             sizeof(float) * AUDIO_SRC_IN_SIZE);
     
    108448            VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1")
    109449                                        .arg(audio_stretchfactor));
    110450            pSoundStretch = new soundtouch::SoundTouch();
    111             pSoundStretch->setSampleRate(audio_samplerate);
    112             pSoundStretch->setChannels(audio_channels);
     451            if (audio_codec)
     452            {
     453                if (!encoder)
     454                {
     455                    VERBOSE(VB_AUDIO, LOC + QString("Creating Encoder for codec %1 origfs %2").arg(audio_codec->codec_id).arg(audio_codec->frame_size));
     456                    encoder = new DigitalEncoder();
     457                    if (!encoder->Init(audio_codec->codec_id,
     458                                audio_codec->bit_rate,
     459                                audio_codec->sample_rate,
     460                                audio_codec->channels
     461                                ))
     462                    {
     463                        // eeks
     464                        delete encoder;
     465                        encoder = NULL;
     466                        VERBOSE(VB_AUDIO, LOC + QString("Failed to Create Encoder"));
     467                    }
     468                }
     469            }
     470            if (encoder)
     471            {
     472                pSoundStretch->setSampleRate(audio_codec->sample_rate);
     473                pSoundStretch->setChannels(audio_codec->channels);
     474            }
     475            else
     476            {
     477                pSoundStretch->setSampleRate(audio_samplerate);
     478                pSoundStretch->setChannels(audio_channels);
     479            }
    113480
    114481            pSoundStretch->setTempo(audio_stretchfactor);
    115482            pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35);
     
    132499}
    133500
    134501void AudioOutputBase::Reconfigure(int laudio_bits, int laudio_channels,
    135                                  int laudio_samplerate, bool laudio_passthru)
     502                                 int laudio_samplerate, bool laudio_passthru,
     503                                 void* laudio_codec)
    136504{
     505    int codec_id = CODEC_ID_NONE;
     506    int lcodec_id = CODEC_ID_NONE;
     507    int lcchannels = 0;
     508    int cchannels = 0;
     509    if (laudio_codec)
     510    {
     511        lcodec_id = ((AVCodecContext*)laudio_codec)->codec_id;
     512        laudio_bits = 16;
     513        laudio_channels = 2;
     514        laudio_samplerate = 48000;
     515        lcchannels = ((AVCodecContext*)laudio_codec)->channels;
     516    }
     517    if (audio_codec)
     518    {
     519        codec_id = audio_codec->codec_id;
     520        cchannels = ((AVCodecContext*)audio_codec)->channels;
     521    }
     522    ClearError();
    137523    if (laudio_bits == audio_bits && laudio_channels == audio_channels &&
    138         laudio_samplerate == audio_samplerate &&
    139         laudio_passthru == audio_passthru && !need_resampler)
     524        laudio_samplerate == audio_samplerate && !need_resampler &&
     525        laudio_passthru == audio_passthru &&
     526        lcodec_id == codec_id && lcchannels == cchannels)
    140527        return;
    141528
    142529    KillAudio();
     
    148535    waud = raud = 0;
    149536    audio_actually_paused = false;
    150537   
     538    bool redo_stretch = (pSoundStretch && audio_channels != laudio_channels);
    151539    audio_channels = laudio_channels;
    152540    audio_bits = laudio_bits;
    153541    audio_samplerate = laudio_samplerate;
     542    audio_codec = (AVCodecContext*)laudio_codec;
    154543    audio_passthru = laudio_passthru;
    155544    if (audio_bits != 8 && audio_bits != 16)
    156545    {
     
    169558   
    170559    numlowbuffer = 0;
    171560
     561    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2 sr %3")
     562            .arg(audio_main_device).arg(audio_channels).arg(audio_samplerate));
     563   
    172564    // Actually do the device specific open call
    173565    if (!OpenDevice())
    174566    {
    175567        VERBOSE(VB_AUDIO, LOC_ERR + "Aborting reconfigure");
    176568        pthread_mutex_unlock(&avsync_lock);
    177569        pthread_mutex_unlock(&audio_buflock);
     570        if (GetError().isEmpty())
     571            Error("Aborting reconfigure");
     572        VERBOSE(VB_AUDIO, "Aborting reconfigure");
    178573        return;
    179574    }
    180575
     
    197592    current_seconds = -1;
    198593    source_bitrate = -1;
    199594
     595    // NOTE: this wont do anything as above samplerate vars are set equal
    200596    // Check if we need the resampler
    201597    if (audio_samplerate != laudio_samplerate)
    202598    {
     
    221617
    222618    VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1")
    223619            .arg(audio_stretchfactor));
     620    VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
     621            .arg(audio_codec?codec_id_string(audio_codec->codec_id):"not set"));
    224622
    225     SetStretchFactorLocked(audio_stretchfactor);
    226     if (pSoundStretch)
     623    if (redo_stretch)
    227624    {
    228         pSoundStretch->setSampleRate(audio_samplerate);
    229         pSoundStretch->setChannels(audio_channels);
     625        float laudio_stretchfactor = audio_stretchfactor;
     626        delete pSoundStretch;
     627        pSoundStretch = NULL;
     628        audio_stretchfactor = 0.0;
     629        SetStretchFactorLocked(laudio_stretchfactor);
    230630    }
     631    else
     632    {
     633        SetStretchFactorLocked(audio_stretchfactor);
     634        if (pSoundStretch)
     635        {
     636            // if its passthru then we need to reencode
     637            if (audio_codec)
     638            {
     639                if (!encoder)
     640                {
     641                    VERBOSE(VB_AUDIO, LOC + QString("Creating Encoder for codec %1").arg(audio_codec->codec_id));
     642                    encoder = new DigitalEncoder();
     643                    if (!encoder->Init(audio_codec->codec_id,
     644                                audio_codec->bit_rate,
     645                                audio_codec->sample_rate,
     646                                audio_codec->channels
     647                                ))
     648                    {
     649                        // eeks
     650                        delete encoder;
     651                        encoder = NULL;
     652                        VERBOSE(VB_AUDIO, LOC + QString("Failed to Create Encoder"));
     653                    }
     654                }
     655            }
     656            if (encoder)
     657            {
     658                pSoundStretch->setSampleRate(audio_codec->sample_rate);
     659                pSoundStretch->setChannels(audio_codec->channels);
     660            }
     661            else
     662            {
     663                pSoundStretch->setSampleRate(audio_samplerate);
     664                pSoundStretch->setChannels(audio_channels);
     665            }
     666        }
     667    }
    231668
    232669    // Setup visualisations, zero the visualisations buffers
    233670    prepareVisuals();
     
    273710        pSoundStretch = NULL;
    274711    }
    275712
     713    if (encoder)
     714    {
     715        delete encoder;
     716        encoder = NULL;
     717    }
     718
    276719    CloseDevice();
    277720
    278721    killAudioLock.unlock();
     
    286729
    287730void AudioOutputBase::Pause(bool paused)
    288731{
     732    VERBOSE(VB_AUDIO, LOC+ QString("Pause %0").arg(paused));
    289733    pauseaudio = paused;
    290734    audio_actually_paused = false;
    291735}
     
    368812       The reason is that computing 'audiotime' requires acquiring the audio
    369813       lock, which the video thread should not do. So, we call 'SetAudioTime()'
    370814       from the audio thread, and then call this from the video thread. */
    371     int ret;
     815    long long ret;
    372816    struct timeval now;
    373817
    374818    if (audiotime == 0)
     
    380824
    381825    ret = (now.tv_sec - audiotime_updated.tv_sec) * 1000;
    382826    ret += (now.tv_usec - audiotime_updated.tv_usec) / 1000;
    383     ret = (int)(ret * audio_stretchfactor);
     827    ret = (long long)(ret * audio_stretchfactor);
    384828
     829#if 1
     830    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     831            QString("GetAudiotime now=%1.%2, set=%3.%4, ret=%5, audt=%6 sf=%7")
     832            .arg(now.tv_sec).arg(now.tv_usec)
     833            .arg(audiotime_updated.tv_sec).arg(audiotime_updated.tv_usec)
     834            .arg(ret)
     835            .arg(audiotime)
     836            .arg(audio_stretchfactor)
     837           );
     838#endif
     839
    385840    ret += audiotime;
    386841
    387842    pthread_mutex_unlock(&avsync_lock);
    388     return ret;
     843    return (int)ret;
    389844}
    390845
    391846void AudioOutputBase::SetAudiotime(void)
     
    422877    // include algorithmic latencies
    423878    if (pSoundStretch)
    424879    {
     880        // add the effect of any unused but processed samples, AC3 reencode does this
     881        totalbuffer += (int)(pSoundStretch->numSamples() * audio_bytes_per_sample);
    425882        // add the effect of unprocessed samples in time stretch algo
    426883        totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() *
    427884                              audio_bytes_per_sample) / audio_stretchfactor);
    428885    }
    429                
     886
    430887    audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 /
    431888                                   (audio_bytes_per_sample * effdspstretched));
    432889 
    433890    gettimeofday(&audiotime_updated, NULL);
     891#if 1
     892    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     893            QString("SetAudiotime set=%1.%2, audt=%3 atc=%4 tb=%5 sb=%6 eds=%7 abps=%8 sf=%9")
     894            .arg(audiotime_updated.tv_sec).arg(audiotime_updated.tv_usec)
     895            .arg(audiotime)
     896            .arg(audbuf_timecode)
     897            .arg(totalbuffer)
     898            .arg(soundcard_buffer)
     899            .arg(effdspstretched)
     900            .arg(audio_bytes_per_sample)
     901            .arg(audio_stretchfactor)
     902           );
     903#endif
    434904
    435905    pthread_mutex_unlock(&avsync_lock);
    436906    pthread_mutex_unlock(&audio_buflock);
     
    498968    // NOTE: This function is not threadsafe
    499969
    500970    int afree = audiofree(true);
    501     int len = samples * audio_bytes_per_sample;
     971    int len = samples * (encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample);
    502972
    503973    // Check we have enough space to write the data
    504974    if (need_resampler && src_ctx)
     
    509979        VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + QString(
    510980                "AddSamples FAILED bytes=%1, used=%2, free=%3, timecode=%4")
    511981                .arg(len).arg(AUDBUFSIZE-afree).arg(afree)
    512                 .arg(timecode));
    513 
     982                .arg(LONGLONGCONVERT timecode));
    514983        return false; // would overflow
    515984    }
    516985
     
    5471016
    5481017int AudioOutputBase::WaitForFreeSpace(int samples)
    5491018{
    550     int len = samples * audio_bytes_per_sample;
     1019    int abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample;
     1020    int len = samples * abps;
    5511021    int afree = audiofree(false);
    5521022
    5531023    while (len > afree)
    5541024    {
    5551025        if (blocking)
    5561026        {
    557             VERBOSE(VB_AUDIO, LOC + "Waiting for free space");
     1027            VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Waiting for free space");
    5581028            // wait for more space
    5591029            pthread_cond_wait(&audio_bufsig, &audio_buflock);
    5601030            afree = audiofree(false);
    5611031        }
    5621032        else
    5631033        {
    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;
     1034            VERBOSE(VB_IMPORTANT, LOC_ERR +
     1035                    QString("Audio buffer overflow, %1 audio samples lost!")
     1036                        .arg(samples-afree / abps));
     1037            samples = afree / abps;
     1038            len = samples * abps;
    5681039            if (src_ctx)
    5691040            {
    5701041                int error = src_reset(src_ctx);
     
    5891060   
    5901061    int afree = audiofree(false);
    5911062
    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));
     1063    int abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample;
     1064    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     1065            LOC + QString("_AddSamples samples=%1 bytes=%2, used=%3, free=%4, timecode=%5")
     1066            .arg(samples)
     1067            .arg(samples * abps)
     1068            .arg(AUDBUFSIZE-afree).arg(afree).arg(LONGLONGCONVERT timecode));
    5961069   
    5971070    len = WaitForFreeSpace(samples);
    5981071
     
    6291102
    6301103    if (pSoundStretch)
    6311104    {
     1105
    6321106        // does not change the timecode, only the number of samples
    6331107        // back to orig pos
    6341108        org_waud = waud;
    6351109        int bdiff = AUDBUFSIZE - org_waud;
    636         int nSamplesToEnd = bdiff/audio_bytes_per_sample;
     1110        int nSamplesToEnd = bdiff/abps;
    6371111        if (bdiff < len)
    6381112        {
    6391113            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)(audiobuffer +
    6401114                                      org_waud), nSamplesToEnd);
    6411115            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)audiobuffer,
    642                                       (len - bdiff) / audio_bytes_per_sample);
     1116                                      (len - bdiff) / abps);
    6431117        }
    6441118        else
    6451119        {
    6461120            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)(audiobuffer +
    647                                       org_waud), len / audio_bytes_per_sample);
     1121                                      org_waud), len / abps);
    6481122        }
    6491123
    650         int newLen = 0;
    651         int nSamples;
    652         len = WaitForFreeSpace(pSoundStretch->numSamples() *
    653                                audio_bytes_per_sample);
    654         do
     1124        if (encoder)
    6551125        {
    656             int samplesToGet = len/audio_bytes_per_sample;
    657             if (samplesToGet > nSamplesToEnd)
     1126            // pull out a packet's worth and reencode it until we dont have enough
     1127            // for any more packets
     1128            soundtouch::SAMPLETYPE* temp_buff =
     1129                (soundtouch::SAMPLETYPE*)encoder->GetFrameBuffer();
     1130            size_t frameSize = encoder->FrameSize()/abps;
     1131            VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     1132                    QString("_AddSamples Enc sfs=%1 bfs=%2 sss=%3")
     1133                    .arg(frameSize)
     1134                    .arg(encoder->FrameSize())
     1135                    .arg(pSoundStretch->numSamples())
     1136                   );
     1137            // process the same number of samples as it creates a full encoded buffer
     1138            // just like before
     1139            while (pSoundStretch->numSamples() >= frameSize)
    6581140            {
    659                 samplesToGet = nSamplesToEnd;   
     1141                int got = pSoundStretch->receiveSamples(temp_buff, frameSize);
     1142                int amount = encoder->Encode(temp_buff);
     1143                VERBOSE(VB_AUDIO|VB_TIMESTAMP,
     1144                        QString("_AddSamples Enc bytes=%1 got=%2 left=%3")
     1145                        .arg(amount)
     1146                        .arg(got)
     1147                        .arg(pSoundStretch->numSamples())
     1148                       );
     1149                if (amount == 0)
     1150                    continue;
     1151                //len = WaitForFreeSpace(amount);
     1152                char * ob = encoder->GetOutBuff();
     1153                if (amount >= bdiff)
     1154                {
     1155                    memcpy(audiobuffer + org_waud, ob, bdiff);
     1156                    ob += bdiff;
     1157                    amount -= bdiff;
     1158                    org_waud = 0;
     1159                }
     1160                if (amount > 0)
     1161                    memcpy(audiobuffer + org_waud, ob, amount);
     1162                bdiff = AUDBUFSIZE - amount;
     1163                org_waud += amount;
    6601164            }
    661 
    662             nSamples = pSoundStretch->receiveSamples((soundtouch::SAMPLETYPE*)
    663                                       (audiobuffer + org_waud), samplesToGet);
    664             if (nSamples == nSamplesToEnd)
     1165        }
     1166        else
     1167        {
     1168            int newLen = 0;
     1169            int nSamples;
     1170            len = WaitForFreeSpace(pSoundStretch->numSamples() *
     1171                                   audio_bytes_per_sample);
     1172            do
    6651173            {
    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             }
     1174                int samplesToGet = len/audio_bytes_per_sample;
     1175                if (samplesToGet > nSamplesToEnd)
     1176                {
     1177                    samplesToGet = nSamplesToEnd;   
     1178                }
    6741179
    675             newLen += nSamples * audio_bytes_per_sample;
    676             len -= nSamples * audio_bytes_per_sample;
    677         } while (nSamples > 0);
     1180                nSamples = pSoundStretch->receiveSamples((soundtouch::SAMPLETYPE*)
     1181                                          (audiobuffer + org_waud), samplesToGet);
     1182                if (nSamples == nSamplesToEnd)
     1183                {
     1184                    org_waud = 0;
     1185                    nSamplesToEnd = AUDBUFSIZE/audio_bytes_per_sample;
     1186                }
     1187                else
     1188                {
     1189                    org_waud += nSamples * audio_bytes_per_sample;
     1190                    nSamplesToEnd -= nSamples;
     1191                }
     1192
     1193                newLen += nSamples * audio_bytes_per_sample;
     1194                len -= nSamples * audio_bytes_per_sample;
     1195            } while (nSamples > 0);
     1196        }
    6781197    }
    6791198
    6801199    waud = org_waud;
     
    7501269            space_on_soundcard = getSpaceOnSoundcard();
    7511270
    7521271            if (space_on_soundcard != last_space_on_soundcard) {
    753                 VERBOSE(VB_AUDIO, LOC + QString("%1 bytes free on soundcard")
     1272                VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + QString("%1 bytes free on soundcard")
    7541273                        .arg(space_on_soundcard));
    7551274                last_space_on_soundcard = space_on_soundcard;
    7561275            }
     
    7631282                    WriteAudio(zeros, fragment_size);
    7641283                } else {
    7651284                    // this should never happen now -dag
    766                     VERBOSE(VB_AUDIO, LOC +
     1285                    VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC +
    7671286                            QString("waiting for space on soundcard "
    7681287                                    "to write zeros: have %1 need %2")
    7691288                            .arg(space_on_soundcard).arg(fragment_size));
     
    7991318        if (fragment_size > audiolen(true))
    8001319        {
    8011320            if (audiolen(true) > 0)  // only log if we're sending some audio
    802                 VERBOSE(VB_AUDIO, LOC +
     1321                VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC +
    8031322                        QString("audio waiting for buffer to fill: "
    8041323                                "have %1 want %2")
    8051324                        .arg(audiolen(true)).arg(fragment_size));
    8061325
    807             VERBOSE(VB_AUDIO, LOC + "Broadcasting free space avail");
     1326            //VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Broadcasting free space avail");
    8081327            pthread_mutex_lock(&audio_buflock);
    8091328            pthread_cond_broadcast(&audio_bufsig);
    8101329            pthread_mutex_unlock(&audio_buflock);
     
    8181337        if (fragment_size > space_on_soundcard)
    8191338        {
    8201339            if (space_on_soundcard != last_space_on_soundcard) {
    821                 VERBOSE(VB_AUDIO, LOC +
     1340                VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC +
    8221341                        QString("audio waiting for space on soundcard: "
    8231342                                "have %1 need %2")
    8241343                        .arg(space_on_soundcard).arg(fragment_size));
     
    8801399
    8811400        /* update raud */
    8821401        raud = (raud + fragment_size) % AUDBUFSIZE;
    883         VERBOSE(VB_AUDIO, LOC + "Broadcasting free space avail");
     1402        //VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Broadcasting free space avail");
    8841403        pthread_cond_broadcast(&audio_bufsig);
    8851404
    8861405        written_size = fragment_size;
  • libs/libmyth/audiooutputalsa.cpp

     
    4949    QString real_device = (audio_passthru) ?
    5050        audio_passthru_device : audio_main_device;
    5151
     52    int index;
     53    if ((index=real_device.find('|'))>=0)
     54    {
     55        if (audio_channels != 2)
     56            real_device = real_device.mid(index+1);
     57        else
     58            real_device = real_device.left(index);
     59    }
     60
    5261    VERBOSE(VB_GENERAL, QString("Opening ALSA audio device '%1'.")
    5362            .arg(real_device));
    5463
     
    8695    }
    8796    else
    8897    {
    89         fragment_size = 6144; // nicely divisible by 2,4,6,8 channels @ 16-bits
    90         buffer_time = 500000;  // .5 seconds
     98        //fragment_size = 6144; // nicely divisible by 2,4,6,8 channels @ 16-bits
     99        //fragment_size = 3072*audio_channels; // nicely divisible by 2,4,6,8 channels @ 16-bits
     100        fragment_size = (audio_bits * audio_channels * audio_samplerate) / (8*30);
     101        buffer_time = 100000;  // .5 seconds
    91102        period_time = buffer_time / 4;  // 4 interrupts per buffer
    92103    }
    93104
     
    159170   
    160171    tmpbuf = aubuf;
    161172
    162     VERBOSE(VB_AUDIO, QString("WriteAudio: Preparing %1 bytes (%2 frames)")
     173    VERBOSE(VB_AUDIO|VB_TIMESTAMP, QString("WriteAudio: Preparing %1 bytes (%2 frames)")
    163174            .arg(size).arg(frames));
    164175   
    165176    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);
     
    24872519#ifdef CONFIG_LIBDTS
    24882520         vgrp0->addChild(DTSPassThrough());
    24892521#endif
     2522         addChild(MaxAudioChannels());
    24902523
    24912524         VerticalConfigurationGroup *vgrp1 =
    24922525             new VerticalConfigurationGroup(false, false, true, true);
  • programs/mythtranscode/transcode.cpp

     
    5555
    5656    // reconfigure sound out for new params
    5757    virtual void Reconfigure(int audio_bits, int audio_channels,
    58                              int audio_samplerate, bool audio_passthru)
     58                             int audio_samplerate, bool audio_passthru,
     59                             void * = NULL)
    5960    {
     61        ClearError();
    6062        (void)audio_samplerate;
    6163        (void)audio_passthru;
    6264        bits = audio_bits;
    6365        channels = audio_channels;
    6466        bytes_per_sample = bits * channels / 8;
     67        if (channels>2)
     68            Error("Invalid channel count");
    6569    }
    6670
    6771    // dsprate is in 100 * samples/second
  • programs/mythuitest/mythuitest.pro

     
    11INCLUDEPATH += ../../libs/libmythui ../../libs/libmyth
    22
    3 LIBS += -L../../libs/libmyth -L../../libs/libmythui
     3LIBS += -L../../libs/libmyth -L../../libs/libmythui -L../../libs/libavcodec -L../../libs/libavutil
    44
    55include ( ../../config.mak )
    66include ( ../../settings.pro )
     
    99TARGET = mythuitest
    1010CONFIG += thread opengl
    1111
    12 LIBS += -lmythui-$$LIBVERSION -lmyth-$$LIBVERSION $$EXTRA_LIBS
     12LIBS += -lmythui-$$LIBVERSION -lmyth-$$LIBVERSION -lmythavcodec-$$LIBVERSION -lmythavutil-$$LIBVERSION $$EXTRA_LIBS
    1313
    1414isEmpty(QMAKE_EXTENSION_SHLIB) {
    1515  QMAKE_EXTENSION_SHLIB=so
     
    1919}
    2020TARGETDEPS += ../../libs/libmythui/libmythui-$${LIBVERSION}.$${QMAKE_EXTENSION_SHLIB}
    2121TARGETDEPS += ../../libs/libmyth/libmyth-$${LIBVERSION}.$${QMAKE_EXTENSION_SHLIB}
     22TARGETDEPS += ../../libs/libavcodec/libmythavcodec-$${LIBVERSION}.$${QMAKE_EXTENSION_SHLIB}
     23TARGETDEPS += ../../libs/libavutil/libmythavutil-$${LIBVERSION}.$${QMAKE_EXTENSION_SHLIB}
    2224
    2325macx {
    2426    # Duplication of source with libmyth (e.g. oldsettings.cpp)
  • libs/libmythtv/avformatdecoder.h

     
    248248    bool              allow_ac3_passthru;
    249249    bool              allow_dts_passthru;
    250250    bool              disable_passthru;
     251    int               max_channels;
    251252
    252253    AudioInfo         audioIn;
    253254    AudioInfo         audioOut;
  • libs/libmythtv/avformatdecoder.cpp

     
    4747#define MAX_AC3_FRAME_SIZE 6144
    4848
    4949/** Set to zero to allow any number of AC3 channels. */
     50#define MAXCHANNELSELECT 1
     51#if MAXCHANNELSELECT
     52#define MAX_OUTPUT_CHANNELS compiler error
     53#else
    5054#define MAX_OUTPUT_CHANNELS 2
     55#endif
    5156
    5257static int cc608_parity(uint8_t byte);
    5358static int cc608_good_parity(const int *parity_table, uint16_t data);
     
    310315#ifdef CONFIG_LIBDTS
    311316    allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
    312317#endif
     318    max_channels = gContext->GetNumSetting("MaxChannels", 2);
    313319
    314320    audioIn.sample_size = -32; // force SetupAudioStream to run once
    315321    itv = GetNVP()->GetInteractiveTV();
     
    458464    framesPlayed = lastKey;
    459465    framesRead = lastKey;
    460466
     467    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")
     468        .arg(desiredFrame)
     469        .arg(fps)
     470        .arg(ts)
     471        .arg(discardFrames)
     472        .arg(framesPlayed)
     473        .arg(st->cur_dts)
     474        .arg(adj_cur_dts)
     475        .arg(newts)
     476        .arg(frameseekadjust)
     477        );
     478
    461479    int normalframes = desiredFrame - framesPlayed;
    462480    normalframes = max(normalframes, 0);
     481#if 0
     482    if (!exactseeks)
     483        normalframes = 0;
     484#endif
     485
    463486    SeekReset(lastKey, normalframes, discardFrames, discardFrames);
    464487
    465488    if (discardFrames)
     
    546569    // Skip all the desired number of skipFrames
    547570    for (;skipFrames > 0 && !ateof; skipFrames--)
    548571    {
    549         GetFrame(0);
     572            GetFrame(0);
    550573        if (decoded_video_frame)
    551574            GetNVP()->DiscardVideoFrame(decoded_video_frame);
    552575    }
     
    777800
    778801    fmt->flags &= ~AVFMT_NOFILE;
    779802
     803#if 1
     804    if ((m_playbackinfo) || livetv || watchingrecording)
     805    {
     806        const char *name = ic->av_class->item_name(ic);
     807        VERBOSE(VB_GENERAL, QString("libavformat type %1").arg(name));
     808    }
     809#endif
     810 
     811    //struct timeval one, two, res;
     812    //gettimeofday(&one, NULL);
     813
    780814    if (!ringBuffer->isDVD() && !livetv)
    781815        av_estimate_timings(ic, 0);
    782816
     
    787821    if (-1 == ret)
    788822        return ret;
    789823
     824    // make sure its at the real start due to av_find_stream_info reading
     825    if (!livetv)
     826    {
     827        ret = av_seek_frame(ic, -1, 0, AVSEEK_FLAG_BACKWARD);
     828        //if (ret < 0)
     829        //    av_seek_frame(ic, -1, 0, AVSEEK_FLAG_BYTE);     // reposition to start of stream
     830    }
     831
    790832    AutoSelectTracks(); // This is needed for transcoder
    791833
    792834    {
     
    816858    // If we don't have a position map, set up ffmpeg for seeking
    817859    if (!recordingHasPositionMap)
    818860    {
     861        const char *name = ic->av_class->item_name(ic);
    819862        VERBOSE(VB_PLAYBACK, LOC +
    820                 "Recording has no position -- using libavformat seeking.");
     863                QString("Recording has no position -- using libavformat seeking. %1").arg(name));
    821864        int64_t dur = ic->duration / (int64_t)AV_TIME_BASE;
    822865
    823866        if (dur > 0)
     
    864907            QString("Successfully opened decoder for file: "
    865908                    "\"%1\". novideo(%2)").arg(filename).arg(novideo));
    866909
     910    // set initial position correctly
     911    //DoFastForward(0, true);
     912
    867913    // Return true if recording has position map
    868914    return recordingHasPositionMap;
    869915}
     
    14201466                            <<") type ("<<codec_type_string(enc->codec_type)
    14211467                            <<") already open, leaving it alone.");
    14221468                }
     1469#if MAXCHANNELSELECT
     1470                if (enc->cqp != max_channels)
     1471                {
     1472                    VERBOSE(VB_IMPORTANT, LOC + QString("Setting maxchannels to %1, %2").arg(max_channels).arg(enc->cqp));
     1473                    enc->cqp = max_channels;
     1474                }
     1475#endif
    14231476                //assert(enc->codec_id);
     1477                VERBOSE(VB_GENERAL, QString("AVFD: codec %1 has %2 channels").arg(codec_id_string(enc->codec_id)).arg(enc->channels));
     1478#if 0
     1479                if (enc->channels > 2)
     1480                    enc->channels = 2;
     1481#endif
     1482#if MAXCHANNELSELECT && 0
     1483                if (enc->channels == 0)
     1484                {
     1485                    //we have had a problem in parsing the codec params in this stream
     1486                    //enc->channels = max_channels;
     1487                }
     1488#endif
    14241489
     1490#if 0
    14251491                // HACK BEGIN REALLY UGLY HACK FOR DTS PASSTHRU
    14261492                if (enc->codec_id == CODEC_ID_DTS)
    14271493                {
     
    14301496                    // enc->bit_rate = what??;
    14311497                }
    14321498                // HACK END REALLY UGLY HACK FOR DTS PASSTHRU
     1499#endif
    14331500
    14341501                bitrate += enc->bit_rate;
    14351502                break;
     
    28522919
    28532920        AVStream *curstream = ic->streams[pkt->stream_index];
    28542921
     2922#if 0
     2923        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + QString("timecode pts:%1 dts:%2 codec:%3")
     2924                .arg(pkt->pts)
     2925                .arg(pkt->dts)
     2926                .arg((curstream && curstream->codec)?curstream->codec->codec_type:-1)
     2927               );
     2928#endif
     2929
    28552930        if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
    28562931            pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
    28572932
     
    30503125                    if (!curstream->codec->channels)
    30513126                    {
    30523127                        QMutexLocker locker(&avcodeclock);
     3128#if MAXCHANNELSELECT
     3129                        VERBOSE(VB_IMPORTANT, LOC + QString("Setting channels to %1").arg(audioOut.channels));
     3130                        curstream->codec->cqp = max_channels;
     3131                        curstream->codec->channels = audioOut.channels;
     3132#else
    30533133                        curstream->codec->channels = MAX_OUTPUT_CHANNELS;
     3134#endif
    30543135                        ret = avcodec_decode_audio(
    30553136                            curstream->codec, audioSamples,
    30563137                            &data_size, ptr, len);
    30573138
    30583139                        reselectAudioTrack |= curstream->codec->channels;
    30593140                    }
     3141#if MAXCHANNELSELECT
     3142                    if (curstream->codec->cqp != max_channels)
     3143                    {
     3144                        VERBOSE(VB_IMPORTANT, LOC + QString("Setting maxchannels to %1, %2").arg(max_channels).arg(curstream->codec->cqp));
     3145                        curstream->codec->cqp = max_channels;
     3146                    }
     3147#endif
    30603148
    30613149                    if (reselectAudioTrack)
    30623150                    {
     
    31103198                    {
    31113199                        AVCodecContext *ctx = curstream->codec;
    31123200
     3201#if MAXCHANNELSELECT
    31133202                        if ((ctx->channels == 0) ||
     3203                            (ctx->channels > audioOut.channels))
     3204                            ctx->channels = audioOut.channels;
     3205#else
     3206                        if ((ctx->channels == 0) ||
    31143207                            (ctx->channels > MAX_OUTPUT_CHANNELS))
    31153208                            ctx->channels = MAX_OUTPUT_CHANNELS;
     3209#endif
    31163210
    31173211                        ret = avcodec_decode_audio(
    31183212                            ctx, audioSamples, &data_size, ptr, len);
     
    34553549
    34563550void AvFormatDecoder::SetDisablePassThrough(bool disable)
    34573551{
     3552#if MAXCHANNELSELECT
     3553    // can only disable never reenable as once tiemstretch is on its on for the session
     3554    if (disable_passthru)
     3555        return;
     3556#endif
    34583557    if (selectedTrack[kTrackTypeAudio].av_stream_index < 0)
    34593558    {
    34603559        disable_passthru = disable;
    34613560        return;
    34623561    }
    3463 
     3562 
    34643563    if (disable != disable_passthru)
    34653564    {
    34663565        disable_passthru = disable;
     
    34873586    AVCodecContext *codec_ctx = NULL;
    34883587    AudioInfo old_in  = audioIn;
    34893588    AudioInfo old_out = audioOut;
     3589    bool using_passthru = false;
    34903590
    34913591    if ((currentTrack[kTrackTypeAudio] >= 0) &&
    34923592        (selectedTrack[kTrackTypeAudio].av_stream_index <=
     
    34963596    {
    34973597        assert(curstream);
    34983598        assert(curstream->codec);
    3499         codec_ctx = curstream->codec;       
     3599        codec_ctx = curstream->codec;
    35003600        bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
    3501                                 !disable_passthru &&
    35023601                                (codec_ctx->codec_id == CODEC_ID_AC3));
    35033602        bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
    3504                                 !disable_passthru &&
    35053603                                (codec_ctx->codec_id == CODEC_ID_DTS));
     3604        using_passthru = do_ac3_passthru || do_dts_passthru;
    35063605        info = AudioInfo(codec_ctx->codec_id,
    35073606                         codec_ctx->sample_rate, codec_ctx->channels,
    3508                          do_ac3_passthru || do_dts_passthru);
     3607                         using_passthru && !disable_passthru);
    35093608    }
    35103609
    35113610    if (info == audioIn)
    35123611        return false; // no change
    35133612
     3613    QString ptmsg = "";
     3614    if (using_passthru)
     3615    {
     3616        ptmsg = QString(" using passthru");
     3617    }
    35143618    VERBOSE(VB_AUDIO, LOC + "Initializing audio parms from " +
    35153619            QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
    35163620
    35173621    audioOut = audioIn = info;
     3622#if MAXCHANNELSELECT
     3623    if (using_passthru)
     3624#else
    35183625    if (audioIn.do_passthru)
     3626#endif
    35193627    {
    35203628        // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
    3521         audioOut.channels    = 2;
    3522         audioOut.sample_rate = 48000;
    3523         audioOut.sample_size = 4;
     3629        AudioInfo digInfo = audioOut;
     3630        if (!disable_passthru)
     3631        {
     3632            digInfo.channels    = 2;
     3633            digInfo.sample_rate = 48000;
     3634            digInfo.sample_size = 4;
     3635        }
     3636        if (audioOut.channels > max_channels)
     3637        {
     3638            audioOut.channels = max_channels;
     3639            audioOut.sample_size = audioOut.channels * 2;
     3640            codec_ctx->channels = audioOut.channels;
     3641        }
     3642#if MAXCHANNELSELECT
     3643        VERBOSE(VB_AUDIO, LOC + "Audio format changed digital passthrough " +
     3644                QString("%1\n\t\t\tfrom %2 ; %3\n\t\t\tto   %4 ; %5")
     3645                .arg(digInfo.toString())
     3646                .arg(old_in.toString()).arg(old_out.toString())
     3647                .arg(audioIn.toString()).arg(audioOut.toString()));
     3648
     3649        if (digInfo.sample_rate > 0)
     3650            GetNVP()->SetEffDsp(digInfo.sample_rate * 100);
     3651
     3652        //GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
     3653        //                         audioOut.sample_rate);
     3654        GetNVP()->SetAudioParams(digInfo.bps(), digInfo.channels,
     3655                                 digInfo.sample_rate, audioIn.do_passthru);
     3656        // allow the audio stuff to reencode
     3657        GetNVP()->SetAudioCodec(codec_ctx);
     3658        GetNVP()->ReinitAudio();
     3659        return true;
     3660#endif
    35243661    }
     3662#if MAXCHANNELSELECT
    35253663    else
    35263664    {
     3665        if (audioOut.channels > max_channels)
     3666        {
     3667            audioOut.channels = max_channels;
     3668            audioOut.sample_size = audioOut.channels * 2;
     3669            codec_ctx->channels = audioOut.channels;
     3670        }
     3671    }
     3672    bool audiook;
     3673#if 0
     3674    do
     3675    {
     3676#endif
     3677#else
     3678    else
     3679    {
    35273680        if (audioOut.channels > MAX_OUTPUT_CHANNELS)
    35283681        {
    35293682            audioOut.channels = MAX_OUTPUT_CHANNELS;
     
    35313684            codec_ctx->channels = MAX_OUTPUT_CHANNELS;
    35323685        }
    35333686    }
     3687#endif
    35343688
    35353689    VERBOSE(VB_AUDIO, LOC + "Audio format changed " +
    35363690            QString("\n\t\t\tfrom %1 ; %2\n\t\t\tto   %3 ; %4")
     
    35433697    GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
    35443698                             audioOut.sample_rate,
    35453699                             audioIn.do_passthru);
    3546     GetNVP()->ReinitAudio();
     3700    // allow the audio stuff to reencode
     3701    GetNVP()->SetAudioCodec(using_passthru?codec_ctx:NULL);
     3702    QString errMsg = GetNVP()->ReinitAudio();
     3703#if MAXCHANNELSELECT
     3704        audiook = errMsg.isEmpty();
     3705#if 0
     3706        if (!audiook)
     3707        {
     3708            switch (audioOut.channels)
     3709            {
     3710#if 0
     3711                case 8:
     3712                    audioOut.channels = 6;
     3713                    break;
     3714#endif
     3715                case 6:
     3716#if 0
     3717                    audioOut.channels = 5;
     3718                    break;
     3719                case 5:
     3720                    audioOut.channels = 4;
     3721                    break;
     3722                case 4:
     3723                    audioOut.channels = 3;
     3724                    break;
     3725                case 3:
     3726#endif
     3727                    audioOut.channels = 2;
     3728                    break;
     3729#if 0
     3730                case 2:
     3731                    audioOut.channels = 1;
     3732                    break;
     3733#endif
     3734                default:
     3735                    // failed to reconfigure under any circumstances
     3736                    audiook = true;
     3737                    audioOut.channels = 0;
     3738                    break;
     3739            }
     3740            audioOut.sample_size = audioOut.channels * 2;
     3741            codec_ctx->channels = audioOut.channels;
     3742        }
     3743    } while (!audiook);
     3744#endif
     3745#endif
    35473746
    35483747    return true;
    35493748}
  • libs/libmythtv/NuppelVideoPlayer.h

     
    127127    void SetAudioInfo(const QString &main, const QString &passthru, uint rate);
    128128    void SetAudioParams(int bits, int channels, int samplerate, bool passthru);
    129129    void SetEffDsp(int dsprate);
     130    void SetAudioCodec(void *ac);
    130131
    131132    // Sets
    132133    void SetParentWidget(QWidget *widget)     { parentWidget = widget; }
     
    677678    int      audio_bits;
    678679    int      audio_samplerate;
    679680    float    audio_stretchfactor;
     681    void     *audio_codec;
    680682    bool     audio_passthru;
    681683
    682684    // Picture-in-Picture
  • libs/libmythtv/NuppelVideoPlayer.cpp

     
    206206      audio_passthru_device(QString::null),
    207207      audio_channels(2),            audio_bits(-1),
    208208      audio_samplerate(44100),      audio_stretchfactor(1.0f),
     209      audio_codec(NULL),
    209210      // Picture-in-Picture
    210211      pipplayer(NULL), setpipplayer(NULL), needsetpipplayer(false),
    211212      // Preview window support
     
    723724    if (audioOutput)
    724725    {
    725726        audioOutput->Reconfigure(audio_bits, audio_channels,
    726                                  audio_samplerate, audio_passthru);
     727                                 audio_samplerate, audio_passthru,
     728                                 audio_codec);
    727729        errMsg = audioOutput->GetError();
    728730        if (!errMsg.isEmpty())
    729731            audioOutput->SetStretchFactor(audio_stretchfactor);
     
    21782180            // decoding; display the frame, but don't wait for A/V Sync.
    21792181            videoOutput->PrepareFrame(buffer, kScan_Intr2ndField);
    21802182            videoOutput->Show(kScan_Intr2ndField);
    2181             VERBOSE(VB_PLAYBACK, LOC + dbg + "skipping A/V wait.");
     2183            VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + dbg + "skipping A/V wait.");
    21822184        }
    21832185        else
    21842186        {
    21852187            // If we are using software decoding, skip this frame altogether.
     2188            //VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + dbg + "dropping frame.");
    21862189            VERBOSE(VB_PLAYBACK, LOC + dbg + "dropping frame to catch up.");
    21872190        }
    21882191    }
     
    21922195        if (buffer)
    21932196            videoOutput->PrepareFrame(buffer, ps);
    21942197
     2198        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString("AVSync waitforframe %1 %2").arg(avsync_adjustment).arg(m_double_framerate));
    21952199        videosync->WaitForFrame(avsync_adjustment);
     2200        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, "AVSync show");
    21962201        if (!resetvideo)
    21972202            videoOutput->Show(ps);
    21982203
     
    22142219
    22152220            // Display the second field
    22162221            videosync->AdvanceTrigger();
    2217             videosync->WaitForFrame(0);
     2222            videosync->WaitForFrame(avsync_adjustment);
    22182223            if (!resetvideo)
    22192224            {
    22202225                videoOutput->Show(ps);
     
    22422247        // If audio is way behind of video, adjust for it...
    22432248        // by cutting the frame rate in half for the length of this frame
    22442249
    2245         avsync_adjustment = frame_interval;
     2250        //avsync_adjustment = frame_interval;
     2251        avsync_adjustment = refreshrate;
    22462252        lastsync = true;
     2253        //VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC +
     2254        //        QString("Video is %1 frames ahead of audio,\n"
     2255        //                "\t\t\tdoubling video frame interval.").arg(diverge));
    22472256        VERBOSE(VB_PLAYBACK, LOC +
    22482257                QString("Video is %1 frames ahead of audio,\n"
    22492258                        "\t\t\tdoubling video frame interval to slow down.").arg(diverge));
     
    22522261    if (audioOutput && normal_speed)
    22532262    {
    22542263        long long currentaudiotime = audioOutput->GetAudiotime();
    2255 #if 0
     2264#if 1
    22562265        VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString(
    22572266                    "A/V timecodes audio %1 video %2 frameinterval %3 "
    22582267                    "avdel %4 avg %5 tcoffset %6")
     
    25902599
    25912600    usevideotimebase = gContext->GetNumSetting("UseVideoTimebase", 0);
    25922601
    2593     if ((print_verbose_messages & VB_PLAYBACK) != 0)
     2602    if ((print_verbose_messages & VB_PLAYBACK|VB_TIMESTAMP) == (VB_PLAYBACK|VB_TIMESTAMP))
    25942603        output_jmeter = new Jitterometer("video_output", 100);
    25952604    else
    25962605        output_jmeter = NULL;
     
    26362645            {
    26372646                VERBOSE(VB_IMPORTANT, "Video sync method can't support double "
    26382647                        "framerate (refresh rate too low for bob deint)");
     2648                //m_scan = kScan_Ignore;
     2649                //m_can_double = false;
    26392650                FallbackDeint();
    26402651            }
    26412652        }
     
    35233534    audio_passthru = passthru;
    35243535}
    35253536
     3537void NuppelVideoPlayer::SetAudioCodec(void* ac)
     3538{
     3539    audio_codec = ac;
     3540}
     3541
    35263542void NuppelVideoPlayer::SetEffDsp(int dsprate)
    35273543{
    35283544    if (audioOutput)
     
    35693585        tc_avcheck_framecounter++;
    35703586        if (tc_avcheck_framecounter == 30)
    35713587        {
    3572 #define AUTO_RESYNC 1
     3588#define AUTO_RESYNC 0
    35733589#if AUTO_RESYNC
    35743590            // something's terribly, terribly wrong.
    35753591            if (tc_lastval[TC_AUDIO] < tc_lastval[TC_VIDEO] - 10000000 ||
  • libs/libavcodec/a52dec.c

     
    143143    }
    144144}
    145145
     146static inline int16_t convert (int32_t i)
     147{
     148    if (i > 0x43c07fff)
     149        return 32767;
     150    else if (i < 0x43bf8000)
     151        return -32768;
     152    else
     153        return i - 0x43c00000;
     154}
     155
     156void float2s16_2 (float * _f, int16_t * s16)
     157{
     158    int i;
     159    int32_t * f = (int32_t *) _f;
     160
     161    for (i = 0; i < 256; i++) {
     162        s16[2*i] = convert (f[i]);
     163        s16[2*i+1] = convert (f[i+256]);
     164    }
     165}
     166
     167void float2s16_4 (float * _f, int16_t * s16)
     168{
     169    int i;
     170    int32_t * f = (int32_t *) _f;
     171
     172    for (i = 0; i < 256; i++) {
     173        s16[4*i] = convert (f[i]);
     174        s16[4*i+1] = convert (f[i+256]);
     175        s16[4*i+2] = convert (f[i+512]);
     176        s16[4*i+3] = convert (f[i+768]);
     177    }
     178}
     179
     180void float2s16_5 (float * _f, int16_t * s16)
     181{
     182    int i;
     183    int32_t * f = (int32_t *) _f;
     184
     185    for (i = 0; i < 256; i++) {
     186        s16[5*i] = convert (f[i]);
     187        s16[5*i+1] = convert (f[i+256]);
     188        s16[5*i+2] = convert (f[i+512]);
     189        s16[5*i+3] = convert (f[i+768]);
     190        s16[5*i+4] = convert (f[i+1024]);
     191    }
     192}
     193
     194int channels_multi (int flags)
     195{
     196    if (flags & A52_LFE)
     197        return 6;
     198    else if (flags & 1) /* center channel */
     199        return 5;
     200    else if ((flags & A52_CHANNEL_MASK) == A52_2F2R)
     201        return 4;
     202    else
     203        return 2;
     204}
     205
     206void float2s16_multi (float * _f, int16_t * s16, int flags)
     207{
     208    int i;
     209    int32_t * f = (int32_t *) _f;
     210
     211    switch (flags) {
     212    case A52_MONO:
     213        for (i = 0; i < 256; i++) {
     214            s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
     215            s16[5*i+4] = convert (f[i]);
     216        }
     217        break;
     218    case A52_CHANNEL:
     219    case A52_STEREO:
     220    case A52_DOLBY:
     221        float2s16_2 (_f, s16);
     222        break;
     223    case A52_3F:
     224        for (i = 0; i < 256; i++) {
     225            s16[5*i] = convert (f[i]);
     226            s16[5*i+1] = convert (f[i+512]);
     227            s16[5*i+2] = s16[5*i+3] = 0;
     228            s16[5*i+4] = convert (f[i+256]);
     229        }
     230        break;
     231    case A52_2F2R:
     232        float2s16_4 (_f, s16);
     233        break;
     234    case A52_3F2R:
     235        float2s16_5 (_f, s16);
     236        break;
     237    case A52_MONO | A52_LFE:
     238        for (i = 0; i < 256; i++) {
     239            s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
     240            s16[6*i+4] = convert (f[i+256]);
     241            s16[6*i+5] = convert (f[i]);
     242        }
     243        break;
     244    case A52_CHANNEL | A52_LFE:
     245    case A52_STEREO | A52_LFE:
     246    case A52_DOLBY | A52_LFE:
     247        for (i = 0; i < 256; i++) {
     248            s16[6*i] = convert (f[i+256]);
     249            s16[6*i+1] = convert (f[i+512]);
     250            s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
     251            s16[6*i+5] = convert (f[i]);
     252        }
     253        break;
     254    case A52_3F | A52_LFE:
     255        for (i = 0; i < 256; i++) {
     256            s16[6*i] = convert (f[i+256]);
     257            s16[6*i+1] = convert (f[i+768]);
     258            s16[6*i+2] = s16[6*i+3] = 0;
     259            s16[6*i+4] = convert (f[i+512]);
     260            s16[6*i+5] = convert (f[i]);
     261        }
     262        break;
     263    case A52_2F2R | A52_LFE:
     264        for (i = 0; i < 256; i++) {
     265            s16[6*i] = convert (f[i+256]);
     266            s16[6*i+1] = convert (f[i+512]);
     267            s16[6*i+2] = convert (f[i+768]);
     268            s16[6*i+3] = convert (f[i+1024]);
     269            s16[6*i+4] = 0;
     270            s16[6*i+5] = convert (f[i]);
     271        }
     272        break;
     273    case A52_3F2R | A52_LFE:
     274        for (i = 0; i < 256; i++) {
     275            s16[6*i] = convert (f[i+256]);
     276            s16[6*i+1] = convert (f[i+768]);
     277            s16[6*i+2] = convert (f[i+1024]);
     278            s16[6*i+3] = convert (f[i+1280]);
     279            s16[6*i+4] = convert (f[i+512]);
     280            s16[6*i+5] = convert (f[i]);
     281        }
     282        break;
     283    }
     284}
     285
     286
    146287/**** end */
    147288
    148289#define HEADER_SIZE 7
     
    168309        len = s->inbuf_ptr - s->inbuf;
    169310        if (s->frame_size == 0) {
    170311            /* no header seen : find one. We need at least 7 bytes to parse it */
     312            //av_log(avctx, AV_LOG_DEBUG, "ac3dec: no frame processing, len %d buf_size %d\n",len,buf_size);
    171313            len = HEADER_SIZE - len;
    172314            if (len > buf_size)
    173315                len = buf_size;
     
    186328                    /* update codec info */
    187329                    avctx->sample_rate = sample_rate;
    188330                    s->channels = ac3_channels[s->flags & 7];
     331                    if (avctx->cqp >= 0)
     332                        avctx->channels = avctx->cqp;
    189333                    if (s->flags & A52_LFE)
    190334                        s->channels++;
    191335                    if (avctx->channels == 0)
    192336                        /* No specific number of channel requested */
    193337                        avctx->channels = s->channels;
    194338                    else if (s->channels < avctx->channels) {
    195                         av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
     339                        //av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
    196340                        avctx->channels = s->channels;
    197341                    }
    198342                    avctx->bit_rate = bit_rate;
     
    208352            s->inbuf_ptr += len;
    209353            buf_size -= len;
    210354        } else {
     355            int chans;
    211356            flags = s->flags;
    212357            if (avctx->channels == 1)
    213358                flags = A52_MONO;
    214             else if (avctx->channels == 2)
    215                 flags = A52_STEREO;
     359            else if (avctx->channels == 2) {
     360                if (s->channels>2)
     361                    flags = A52_DOLBY;
     362                else
     363                    flags = A52_STEREO;
     364            }
    216365            else
    217366                flags |= A52_ADJUST_LEVEL;
    218367            level = 1;
     368            chans = channels_multi(flags);
    219369            if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
    220370            fail:
    221371                av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
     
    226376            for (i = 0; i < 6; i++) {
    227377                if (s->a52_block(s->state))
    228378                    goto fail;
    229                 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
     379                float2s16_multi(s->samples, out_samples + i * 256 * chans, flags);
    230380            }
    231381            s->inbuf_ptr = s->inbuf;
    232382            s->frame_size = 0;
    233             *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
     383            *data_size = 6 * chans * 256 * sizeof(int16_t);
    234384            break;
    235385        }
    236386    }
  • libs/libavcodec/ac3enc.c

     
    11521152{
    11531153    AC3EncodeContext *s = avctx->priv_data;
    11541154    int16_t *samples = data;
     1155    // expects L C R LS RS LFE
     1156    // audio format is L R LS RS C LFE
     1157    static int channel_index[6] = { 0, 4, 1, 2, 3, 5 };
     1158    /*
     1159     * A52->Analog->AC3Enc
     1160     * 1->0->0
     1161     * 3->1->2
     1162     * 4->2->3
     1163     * 5->3->4
     1164     * 2->4->1
     1165     * 0->5->5
     1166     */
    11551167    int i, j, k, v, ch;
    11561168    int16_t input_samples[N];
    11571169    int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
     
    11721184            /* compute input samples */
    11731185            memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t));
    11741186            sinc = s->nb_all_channels;
    1175             sptr = samples + (sinc * (N/2) * i) + ch;
     1187            sptr = samples + (sinc * (N/2) * i) + channel_index[ch];
    11761188            for(j=0;j<N/2;j++) {
    11771189                v = *sptr;
    11781190                input_samples[j + N/2] = v;