mirror of https://git.ffmpeg.org/ffmpeg.git
output example: use the new AVFrame API to allocate audio frames
This commit is contained in:
parent
ac85f631c9
commit
5e7b125b6a
|
@ -59,7 +59,6 @@ typedef struct OutputStream {
|
||||||
/* audio output */
|
/* audio output */
|
||||||
|
|
||||||
static float t, tincr, tincr2;
|
static float t, tincr, tincr2;
|
||||||
static int16_t *samples;
|
|
||||||
static int audio_input_frame_size;
|
static int audio_input_frame_size;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -122,20 +121,24 @@ static void open_audio(AVFormatContext *oc, AVStream *st)
|
||||||
audio_input_frame_size = 10000;
|
audio_input_frame_size = 10000;
|
||||||
else
|
else
|
||||||
audio_input_frame_size = c->frame_size;
|
audio_input_frame_size = c->frame_size;
|
||||||
samples = av_malloc(audio_input_frame_size *
|
|
||||||
av_get_bytes_per_sample(c->sample_fmt) *
|
|
||||||
c->channels);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
|
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
|
||||||
* 'nb_channels' channels. */
|
* 'nb_channels' channels. */
|
||||||
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
|
static void get_audio_frame(AVFrame *frame, int nb_channels)
|
||||||
{
|
{
|
||||||
int j, i, v;
|
int j, i, v, ret;
|
||||||
int16_t *q;
|
int16_t *q = (int16_t*)frame->data[0];
|
||||||
|
|
||||||
q = samples;
|
/* when we pass a frame to the encoder, it may keep a reference to it
|
||||||
for (j = 0; j < frame_size; j++) {
|
* internally;
|
||||||
|
* make sure we do not overwrite it here
|
||||||
|
*/
|
||||||
|
ret = av_frame_make_writable(frame);
|
||||||
|
if (ret < 0)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
for (j = 0; j < frame->nb_samples; j++) {
|
||||||
v = (int)(sin(t) * 10000);
|
v = (int)(sin(t) * 10000);
|
||||||
for (i = 0; i < nb_channels; i++)
|
for (i = 0; i < nb_channels; i++)
|
||||||
*q++ = v;
|
*q++ = v;
|
||||||
|
@ -149,18 +152,22 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
|
||||||
AVCodecContext *c;
|
AVCodecContext *c;
|
||||||
AVPacket pkt = { 0 }; // data and size must be 0;
|
AVPacket pkt = { 0 }; // data and size must be 0;
|
||||||
AVFrame *frame = av_frame_alloc();
|
AVFrame *frame = av_frame_alloc();
|
||||||
int got_packet;
|
int got_packet, ret;
|
||||||
|
|
||||||
av_init_packet(&pkt);
|
av_init_packet(&pkt);
|
||||||
c = st->codec;
|
c = st->codec;
|
||||||
|
|
||||||
get_audio_frame(samples, audio_input_frame_size, c->channels);
|
frame->sample_rate = c->sample_rate;
|
||||||
frame->nb_samples = audio_input_frame_size;
|
frame->nb_samples = audio_input_frame_size;
|
||||||
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
|
frame->format = AV_SAMPLE_FMT_S16;
|
||||||
(uint8_t *)samples,
|
frame->channel_layout = c->channel_layout;
|
||||||
audio_input_frame_size *
|
ret = av_frame_get_buffer(frame, 0);
|
||||||
av_get_bytes_per_sample(c->sample_fmt) *
|
if (ret < 0) {
|
||||||
c->channels, 1);
|
fprintf(stderr, "Could not allocate an audio frame.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
get_audio_frame(frame, c->channels);
|
||||||
|
|
||||||
avcodec_encode_audio2(c, &pkt, frame, &got_packet);
|
avcodec_encode_audio2(c, &pkt, frame, &got_packet);
|
||||||
if (!got_packet)
|
if (!got_packet)
|
||||||
|
@ -179,8 +186,6 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
|
||||||
static void close_audio(AVFormatContext *oc, AVStream *st)
|
static void close_audio(AVFormatContext *oc, AVStream *st)
|
||||||
{
|
{
|
||||||
avcodec_close(st->codec);
|
avcodec_close(st->codec);
|
||||||
|
|
||||||
av_free(samples);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************/
|
/**************************************************************/
|
||||||
|
|
Loading…
Reference in New Issue