Commit dcc73aaa authored by Nicolas George's avatar Nicolas George

doc/examples: do not allocate AVFrame directly.

The size of the AVFrame structure is not part of the ABI;
it can grow with later versions. Therefore, applications
are not supposed to allocate AVFrame directly, they are
supposed to use avcodec_alloc_frame() instead.
parent 59e46ef6
...@@ -169,9 +169,13 @@ int main(int argc, char **argv) ...@@ -169,9 +169,13 @@ int main(int argc, char **argv)
{ {
int ret; int ret;
AVPacket packet; AVPacket packet;
AVFrame frame; AVFrame *frame = avcodec_alloc_frame();
int got_frame; int got_frame;
if (!frame) {
perror("Could not allocate frame");
exit(1);
}
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s file | %s\n", argv[0], player); fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
exit(1); exit(1);
...@@ -193,9 +197,9 @@ int main(int argc, char **argv) ...@@ -193,9 +197,9 @@ int main(int argc, char **argv)
break; break;
if (packet.stream_index == audio_stream_index) { if (packet.stream_index == audio_stream_index) {
avcodec_get_frame_defaults(&frame); avcodec_get_frame_defaults(frame);
got_frame = 0; got_frame = 0;
ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &packet); ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, &packet);
if (ret < 0) { if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n"); av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n");
continue; continue;
...@@ -203,7 +207,7 @@ int main(int argc, char **argv) ...@@ -203,7 +207,7 @@ int main(int argc, char **argv)
if (got_frame) { if (got_frame) {
/* push the audio data from decoded frame into the filtergraph */ /* push the audio data from decoded frame into the filtergraph */
if (av_buffersrc_add_frame(buffersrc_ctx, &frame, 0) < 0) { if (av_buffersrc_add_frame(buffersrc_ctx, frame, 0) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n"); av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
break; break;
} }
...@@ -229,6 +233,7 @@ end: ...@@ -229,6 +233,7 @@ end:
if (dec_ctx) if (dec_ctx)
avcodec_close(dec_ctx); avcodec_close(dec_ctx);
avformat_close_input(&fmt_ctx); avformat_close_input(&fmt_ctx);
av_freep(&frame);
if (ret < 0 && ret != AVERROR_EOF) { if (ret < 0 && ret != AVERROR_EOF) {
char buf[1024]; char buf[1024];
......
...@@ -173,9 +173,13 @@ int main(int argc, char **argv) ...@@ -173,9 +173,13 @@ int main(int argc, char **argv)
{ {
int ret; int ret;
AVPacket packet; AVPacket packet;
AVFrame frame; AVFrame *frame = avcodec_alloc_frame();
int got_frame; int got_frame;
if (!frame) {
perror("Could not allocate frame");
exit(1);
}
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]); fprintf(stderr, "Usage: %s file\n", argv[0]);
exit(1); exit(1);
...@@ -197,19 +201,19 @@ int main(int argc, char **argv) ...@@ -197,19 +201,19 @@ int main(int argc, char **argv)
break; break;
if (packet.stream_index == video_stream_index) { if (packet.stream_index == video_stream_index) {
avcodec_get_frame_defaults(&frame); avcodec_get_frame_defaults(frame);
got_frame = 0; got_frame = 0;
ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet); ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
if (ret < 0) { if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error decoding video\n"); av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
break; break;
} }
if (got_frame) { if (got_frame) {
frame.pts = av_frame_get_best_effort_timestamp(&frame); frame->pts = av_frame_get_best_effort_timestamp(frame);
/* push the decoded frame into the filtergraph */ /* push the decoded frame into the filtergraph */
if (av_buffersrc_add_frame(buffersrc_ctx, &frame, 0) < 0) { if (av_buffersrc_add_frame(buffersrc_ctx, frame, 0) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n"); av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
break; break;
} }
...@@ -236,6 +240,7 @@ end: ...@@ -236,6 +240,7 @@ end:
if (dec_ctx) if (dec_ctx)
avcodec_close(dec_ctx); avcodec_close(dec_ctx);
avformat_close_input(&fmt_ctx); avformat_close_input(&fmt_ctx);
av_freep(&frame);
if (ret < 0 && ret != AVERROR_EOF) { if (ret < 0 && ret != AVERROR_EOF) {
char buf[1024]; char buf[1024];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment