Skip to content

Crash while encoding audio frame #9

@yunsun330

Description

@yunsun330

I am trying to generate an audio frame for slient audio.

/* frame containing input raw audio */
AVFrame *audio_frame = av_frame_alloc();
if (!audio_frame) {
    return NO;
}

audio_frame->nb_samples     = audio_out_stream->codec->frame_size;
audio_frame->format         = audio_out_stream->codec->sample_fmt;
audio_frame->channel_layout = audio_out_stream->codec->channel_layout;

/* the codec gives us the frame size, in samples,
 * we calculate the size of the samples buffer in bytes */
int buffer_size = av_samples_get_buffer_size(NULL,
                                             audio_out_stream->codec->channels,
                                             audio_out_stream->codec->frame_size,
                                             audio_out_stream->codec->sample_fmt, 0);
if (buffer_size < 0) {
    return NO;
}

uint16_t *samples = av_malloc(buffer_size);
memset(samples, 0, buffer_size);

/* setup the data pointers in the AVFrame */
if (avcodec_fill_audio_frame(audio_frame,
                             audio_out_stream->codec->channels,
                             audio_out_stream->codec->sample_fmt,
                             (const uint8_t*)samples, buffer_size, 0) < 0) {
    return NO;
}

/* encode a single tone sound */
    AVPacket packet;

    av_init_packet(&packet);
    packet.data = NULL; // packet data will be allocated by the encoder
    packet.size = 0;

    /* encode the samples */
    int got_output;
    if (avcodec_encode_audio2(audio_out_stream->codec, &packet, audio_frame, &got_output) < 0) {
        return NO;
    }
    if (got_output) {
        packet.pts = 30*numPacketsSent;
        numPacketsSent++;

        int res = av_write_frame(ofmt_ctx, &packet);
        if (res < 0) {
            return NO;
        }
    }
}
av_frame_free(&audio_frame);

And, I am getting crash in avcodec_encode_audio2 function.
Can you please let me know what's wrong here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions