examples/filtering_video: update to new API

In particular, fix crash.
This commit is contained in:
Stefano Sabatini 2013-03-13 21:56:14 +01:00
parent 7a71544f9d
commit f0da370a52

View File

@ -138,33 +138,33 @@ static int init_filters(const char *filters_descr)
return 0; return 0;
} }
static void display_picref(AVFilterBufferRef *picref, AVRational time_base) static void display_frame(AVFrame *frame, AVRational time_base)
{ {
int x, y; int x, y;
uint8_t *p0, *p; uint8_t *p0, *p;
int64_t delay; int64_t delay;
if (picref->pts != AV_NOPTS_VALUE) { if (frame->pts != AV_NOPTS_VALUE) {
if (last_pts != AV_NOPTS_VALUE) { if (last_pts != AV_NOPTS_VALUE) {
/* sleep roughly the right amount of time; /* sleep roughly the right amount of time;
* usleep is in microseconds, just like AV_TIME_BASE. */ * usleep is in microseconds, just like AV_TIME_BASE. */
delay = av_rescale_q(picref->pts - last_pts, delay = av_rescale_q(frame->pts - last_pts,
time_base, AV_TIME_BASE_Q); time_base, AV_TIME_BASE_Q);
if (delay > 0 && delay < 1000000) if (delay > 0 && delay < 1000000)
usleep(delay); usleep(delay);
} }
last_pts = picref->pts; last_pts = frame->pts;
} }
/* Trivial ASCII grayscale display. */ /* Trivial ASCII grayscale display. */
p0 = picref->data[0]; p0 = frame->data[0];
puts("\033c"); puts("\033c");
for (y = 0; y < picref->video->h; y++) { for (y = 0; y < frame->height; y++) {
p = p0; p = p0;
for (x = 0; x < picref->video->w; x++) for (x = 0; x < frame->width; x++)
putchar(" .-+#"[*(p++) / 52]); putchar(" .-+#"[*(p++) / 52]);
putchar('\n'); putchar('\n');
p0 += picref->linesize[0]; p0 += frame->linesize[0];
} }
fflush(stdout); fflush(stdout);
} }
@ -173,10 +173,11 @@ int main(int argc, char **argv)
{ {
int ret; int ret;
AVPacket packet; AVPacket packet;
AVFrame *frame = avcodec_alloc_frame(); AVFrame *frame = av_frame_alloc();
AVFrame *filt_frame = av_frame_alloc();
int got_frame; int got_frame;
if (!frame) { if (!frame || !filt_frame) {
perror("Could not allocate frame"); perror("Could not allocate frame");
exit(1); exit(1);
} }
@ -196,7 +197,6 @@ int main(int argc, char **argv)
/* read all packets */ /* read all packets */
while (1) { while (1) {
AVFilterBufferRef *picref;
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
break; break;
@ -213,23 +213,20 @@ int main(int argc, char **argv)
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) { if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 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;
} }
/* pull filtered pictures from the filtergraph */ /* pull filtered frames from the filtergraph */
while (1) { while (1) {
ret = av_buffersink_get_buffer_ref(buffersink_ctx, &picref, 0); ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break; break;
if (ret < 0) if (ret < 0)
goto end; goto end;
display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
if (picref) { av_frame_unref(filt_frame);
display_picref(picref, buffersink_ctx->inputs[0]->time_base);
avfilter_unref_bufferp(&picref);
}
} }
} }
} }
@ -240,7 +237,8 @@ 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); av_frame_free(&frame);
av_frame_free(&filt_frame);
if (ret < 0 && ret != AVERROR_EOF) { if (ret < 0 && ret != AVERROR_EOF) {
char buf[1024]; char buf[1024];