Ticket #12547: 0001-ffmpeg-addon-only-use-swresample-for-planar-audio.patch

File 0001-ffmpeg-addon-only-use-swresample-for-planar-audio.patch, 3.6 KB (added by jessicah, 5 years ago)
  • src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp

    From a86d783468d57fab15f519ef32418d4a005f951f Mon Sep 17 00:00:00 2001
    From: Jessica Hamilton <jessica.l.hamilton@gmail.com>
    Date: Tue, 29 Dec 2015 17:43:24 +1300
    Subject: [PATCH] ffmpeg addon: only use swresample for planar audio.
    
    Fixes #12547.
    ---
     .../media/plugins/ffmpeg/AVCodecDecoder.cpp        | 43 +++++++++++++---------
     src/add-ons/media/plugins/ffmpeg/Utilities.h       | 16 ++++++++
     2 files changed, 41 insertions(+), 18 deletions(-)
    
    diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
    index c162b13..69e405b 100644
    a b AVCodecDecoder::_NegotiateAudioOutputFormat(media_format* inOutFormat)  
    408408    if (fRawDecodedAudio->opaque == NULL)
    409409        return B_NO_MEMORY;
    410410
    411     fResampleContext = swr_alloc_set_opts(NULL,
    412         fContext->channel_layout, fContext->request_sample_fmt,
    413         fContext->sample_rate,
    414         fContext->channel_layout, fContext->sample_fmt, fContext->sample_rate,
    415         0, NULL);
    416     swr_init(fResampleContext);
     411    if (AVSampleFormatIsPlanar(fContext->sample_fmt)) {
     412        fResampleContext = swr_alloc_set_opts(NULL,
     413            fContext->channel_layout, fContext->request_sample_fmt,
     414            fContext->sample_rate,
     415            fContext->channel_layout, fContext->sample_fmt, fContext->sample_rate,
     416            0, NULL);
     417        swr_init(fResampleContext);
     418    }
    417419
    418420    TRACE("  bit_rate = %d, sample_rate = %d, channels = %d, "
    419421        "output frame size: %d, count: %ld, rate: %.2f\n",
    AVCodecDecoder::_MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes()  
    923925    // "planar" audio (each channel separated instead of interleaved samples).
    924926    // In that case, we use swresample to convert the data (and it is
    925927    // smart enough to do just a copy, when possible)
    926     const uint8_t* ptr[8];
    927     for (int i = 0; i < 8; i++) {
    928         if (fDecodedDataBuffer->data[i] == NULL)
    929             ptr[i] = NULL;
    930         else
    931             ptr[i] = fDecodedDataBuffer->data[i] + fDecodedDataBufferOffset;
    932     }
     928    if (AVSampleFormatIsPlanar(fContext->sample_fmt)) {
     929        const uint8_t* ptr[8];
     930        for (int i = 0; i < 8; i++) {
     931            if (fDecodedDataBuffer->data[i] == NULL)
     932                ptr[i] = NULL;
     933            else
     934                ptr[i] = fDecodedDataBuffer->data[i] + fDecodedDataBufferOffset;
     935        }
    933936
    934     int32 result = swr_convert(fResampleContext, fRawDecodedAudio->data,
    935         outFrames, ptr, inFrames);
     937        int32 result = swr_convert(fResampleContext, fRawDecodedAudio->data,
     938            outFrames, ptr, inFrames);
     939
     940        if (result < 0)
     941            debugger("resampling failed");
     942    } else {
     943        memcpy(fRawDecodedAudio->data[0], fDecodedDataBuffer->data[0]
     944                + fDecodedDataBufferOffset, frames * fOutputFrameSize);
     945    }
    936946
    937947    size_t remainingSize = inFrames * fOutputFrameSize;
    938948    size_t decodedSize = outFrames * fOutputFrameSize;
    939949    fDecodedDataBufferSize -= inFrames;
    940950
    941     if (result < 0)
    942         debugger("resampling failed");
    943 
    944951    bool firstAudioFramesCopiedToRawDecodedAudio
    945952        = fRawDecodedAudio->data[0] != fDecodedData;
    946953    if (!firstAudioFramesCopiedToRawDecodedAudio) {
  • src/add-ons/media/plugins/ffmpeg/Utilities.h

    diff --git a/src/add-ons/media/plugins/ffmpeg/Utilities.h b/src/add-ons/media/plugins/ffmpeg/Utilities.h
    index 209bd1e..3a9e0e4 100644
    a b ConvertAVSampleFormatToRawAudioFormat(AVSampleFormat sampleFormatIn,  
    317317}
    318318
    319319
     320inline bool
     321AVSampleFormatIsPlanar(AVSampleFormat sampleFormat)
     322{
     323    switch (sampleFormat) {
     324        case AV_SAMPLE_FMT_FLTP:
     325        case AV_SAMPLE_FMT_DBLP:
     326        case AV_SAMPLE_FMT_S32P:
     327        case AV_SAMPLE_FMT_S16P:
     328        case AV_SAMPLE_FMT_U8P:
     329            return true;
     330        default:
     331            return false;
     332    }
     333}
     334
     335
    320336#endif // UTILITIES_H