summaryrefslogtreecommitdiff
path: root/doc/examples/encode_audio.c
diff options
context:
space:
mode:
authorClément Bœsch <u@pkh.me>2017-03-29 13:31:44 +0200
committerClément Bœsch <u@pkh.me>2017-03-29 13:31:44 +0200
commit780cc080d85656429ba97426f35c7bc1f7f201bb (patch)
treeed9b40bf7439caf2b86cb7b8660a6ae403d91aea /doc/examples/encode_audio.c
parentf38e7566c69bc6da35eb22e29384d202c87c054a (diff)
parentf76698e759a08e8d3b629c06edb0439f474e7fee (diff)
Merge commit 'f76698e759a08e8d3b629c06edb0439f474e7fee'
* commit 'f76698e759a08e8d3b629c06edb0439f474e7fee': examples/encode_audio: use the AVFrame API for allocating the data Merged-by: Clément Bœsch <u@pkh.me>
Diffstat (limited to 'doc/examples/encode_audio.c')
-rw-r--r--doc/examples/encode_audio.c30
1 files changed, 10 insertions, 20 deletions
diff --git a/doc/examples/encode_audio.c b/doc/examples/encode_audio.c
index b788775a56..f3bb102d51 100644
--- a/doc/examples/encode_audio.c
+++ b/doc/examples/encode_audio.c
@@ -99,7 +99,6 @@ int main(int argc, char **argv)
AVFrame *frame;
AVPacket pkt;
int i, j, k, ret, got_output;
- int buffer_size;
FILE *f;
uint16_t *samples;
float t, tincr;
@@ -165,25 +164,10 @@ int main(int argc, char **argv)
frame->format = c->sample_fmt;
frame->channel_layout = c->channel_layout;
- /* the codec gives us the frame size, in samples,
- * we calculate the size of the samples buffer in bytes */
- buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
- c->sample_fmt, 0);
- if (buffer_size < 0) {
- fprintf(stderr, "Could not get sample buffer size\n");
- exit(1);
- }
- samples = av_malloc(buffer_size);
- if (!samples) {
- fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
- buffer_size);
- exit(1);
- }
- /* setup the data pointers in the AVFrame */
- ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
- (const uint8_t*)samples, buffer_size, 0);
+ /* allocate the data buffers */
+ ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
- fprintf(stderr, "Could not setup audio frame\n");
+ fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
@@ -195,6 +179,13 @@ int main(int argc, char **argv)
pkt.data = NULL; // packet data will be allocated by the encoder
pkt.size = 0;
+ /* make sure the frame is writable -- makes a copy if the encoder
+ * kept a reference internally */
+ ret = av_frame_make_writable(frame);
+ if (ret < 0)
+ exit(1);
+ samples = (uint16_t*)frame->data[0];
+
for (j = 0; j < c->frame_size; j++) {
samples[2*j] = (int)(sin(t) * 10000);
@@ -229,7 +220,6 @@ int main(int argc, char **argv)
}
fclose(f);
- av_freep(&samples);
av_frame_free(&frame);
avcodec_free_context(&c);
}