id	summary	reporter	owner	description	type	status	priority	component	version	resolution	keywords	cc	blockedby	blocking	reproduced	analyzed
1834	swr_convert() results in integer division by zero exception	mbradshaw	michael	"'''Summary of the bug:'''
When I call `swr_convert()` I get an integer division by zero exception at 0x707c86ee. The disassembly around 0x707c86ee, should it help, is:
{{{
707C86AB  je          707C86E4  
707C86AD  mov         edx,dword ptr [ebx+4E8h]  
707C86B3  test        edx,edx  
707C86B5  jne         707C8743  
707C86BB  mov         dword ptr [esp+0Ch],edi  
707C86BF  mov         dword ptr [esp+8],ebp  
707C86C3  mov         dword ptr [esp+4],esi  
707C86C7  mov         dword ptr [esp],eax  
707C86CA  mov         dword ptr [esp+2Ch],ecx  
707C86CE  call        707C2AF4  
707C86D3  mov         ecx,dword ptr [esp+2Ch]  
707C86D7  mov         eax,ecx  
707C86D9  add         esp,3Ch  
707C86DC  pop         ebx  
707C86DD  pop         esi  
707C86DE  pop         edi  
707C86DF  pop         ebp  
707C86E0  ret  
707C86E1  lea         esi,[esi]  
707C86E4  test        edi,edi  
707C86E6  js          707C873C  
707C86E8  mov         eax,3FFFFFFFh  
707C86ED  cdq  
707C86EE  idiv        eax,dword ptr [ebx+1B4h]  
707C86F4  cdq  
707C86F5  idiv        eax,dword ptr [ebx+1B0h]  
707C86FB  cmp         edi,eax  
707C86FD  jg          707C873C  
707C86FF  cmp         edi,dword ptr [ebx+1B8h]  
707C8705  jle         707C8724  
707C8707  lea         eax,[ebx+12Ch]  
707C870D  mov         edx,edi  
707C870F  mov         dword ptr [esp+2Ch],ecx  
707C8713  call        707C77C8  
707C8718  test        eax,eax  
707C871A  mov         ecx,dword ptr [esp+2Ch]  
707C871E  jns         707C8724  
707C8720  mov         ecx,eax  
707C8722  jmp         707C86D7  
707C8724  mov         dword ptr [esp+54h],edi  
707C8728  mov         dword ptr [esp+50h],ebp  
707C872C  mov         edx,esi  
707C872E  mov         eax,ebx  
707C8730  add         esp,3Ch  
707C8733  pop         ebx  
707C8734  pop         esi  
707C8735  pop         edi  
707C8736  pop         ebp  
707C8737  jmp         707C80B4  
707C873C  mov         ecx,0FFFFFFEAh  
}}}

This happens with more than just the attached sample file, but I've attached a file just in case. The very first call to `swr_convert()` results in this division by zero exception.

'''How to reproduce:''' (some lame sample code just to show when it's happening)
{{{
#include <iostream>

extern ""C""
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
};

int main()
{
    av_register_all();

    AVFrame* frame = avcodec_alloc_frame();
    if (!frame)
    {
        std::cout << ""Error allocating the frame"" << std::endl;
        return 1;
    }

    AVFormatContext* formatContext = NULL;
    if (avformat_open_input(&formatContext, ""C:/Users/mbradshaw/Desktop/samples/TimeCode.mov"", NULL, NULL) != 0)
    {
        av_free(frame);
        std::cout << ""Error opening the file"" << std::endl;
        return 1;
    }

    if (avformat_find_stream_info(formatContext, NULL) < 0)
    {
        av_free(frame);
        av_close_input_file(formatContext);
        std::cout << ""Error finding the stream info"" << std::endl;
        return 1;
    }

    AVStream* audioStream = NULL;
    for (unsigned int i = 0; i < formatContext->nb_streams; ++i)
    {
        if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            audioStream = formatContext->streams[i];
            break;
        }
    }

    if (audioStream == NULL)
    {
        av_free(frame);
        av_close_input_file(formatContext);
        std::cout << ""Could not find any audio stream in the file"" << std::endl;
        return 1;
    }

    AVCodecContext* codecContext = audioStream->codec;

    codecContext->codec = avcodec_find_decoder(codecContext->codec_id);
    if (codecContext->codec == NULL)
    {
        av_free(frame);
        av_close_input_file(formatContext);
        std::cout << ""Couldn't find a proper decoder"" << std::endl;
        return 1;
    }
    else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
    {
        av_free(frame);
        av_close_input_file(formatContext);
        std::cout << ""Couldn't open the context with the decoder"" << std::endl;
        return 1;
    }

    SwrContext* swrContext = NULL;
    swrContext = swr_alloc_set_opts(swrContext,
        codecContext->channel_layout, // out channel layout
        codecContext->sample_fmt, // out sample format
        codecContext->sample_rate, // out sample rate
        codecContext->channel_layout, // in channel layout
        codecContext->sample_fmt, // in sample format
        codecContext->sample_rate, // in sample rate
        0, // log offset
        NULL); // log context

    if (!swrContext)
    {
        av_free(frame);
        avcodec_close(codecContext);
        av_close_input_file(formatContext);
        std::cout << ""Couldn't allocate and set the resampling context"" << std::endl;
        return 1;
    }

    int bufSize = av_samples_get_buffer_size(NULL, codecContext->channels, codecContext->sample_rate, codecContext->sample_fmt, 0);
    uint8_t* buf = new uint8_t[bufSize];

    AVPacket packet;
    av_init_packet(&packet);

    int packetCount = 0;
    int decodedFrameCount = 0;
    while (av_read_frame(formatContext, &packet) == 0)
    {
        ++packetCount;
        if (packet.stream_index == audioStream->index)
        {
            int frameFinished = 0;
            avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet);

            if (frameFinished)
            {
                const uint8_t** in = (const uint8_t**)frame->extended_data;
                uint8_t* out[SWR_CH_MAX] = {buf, NULL};

                std::cout << ""converted: "" << swr_convert(swrContext, out, codecContext->sample_rate, in, frame->nb_samples) << std::endl;
            }
        }

        av_free_packet(&packet);
    }

    delete [] buf;
    swr_free(&swrContext);
    av_free(frame);
    avcodec_close(codecContext);
    av_close_input_file(formatContext);
}
}}}"	defect	closed	normal	swresample	git-master	invalid					0	0
