1 /*
2 * Copyright 2007 Bobby Bingham
3 * Copyright Stefano Sabatini <stefasab gmail com>
4 * Copyright Vitor Sessak <vitor1001 gmail com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <string.h>
24 #include <stdio.h>
25
26 #include "libavutil/buffer.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/hwcontext.h"
29 #include "libavutil/imgutils.h"
30
31 #include "avfilter.h"
32 #include "framepool.h"
33 #include "internal.h"
34 #include "video.h"
35
ff_null_get_video_buffer(AVFilterLink * link,int w,int h)36 AVFrame *ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
37 {
38 return ff_get_video_buffer(link->dst->outputs[0], w, h);
39 }
40
ff_default_get_video_buffer2(AVFilterLink * link,int w,int h,int align)41 AVFrame *ff_default_get_video_buffer2(AVFilterLink *link, int w, int h, int align)
42 {
43 AVFrame *frame = NULL;
44 int pool_width = 0;
45 int pool_height = 0;
46 int pool_align = 0;
47 enum AVPixelFormat pool_format = AV_PIX_FMT_NONE;
48
49 if (link->hw_frames_ctx &&
50 ((AVHWFramesContext*)link->hw_frames_ctx->data)->format == link->format) {
51 int ret;
52 frame = av_frame_alloc();
53
54 if (!frame)
55 return NULL;
56
57 ret = av_hwframe_get_buffer(link->hw_frames_ctx, frame, 0);
58 if (ret < 0)
59 av_frame_free(&frame);
60
61 return frame;
62 }
63
64 if (!link->frame_pool) {
65 link->frame_pool = ff_frame_pool_video_init(av_buffer_allocz, w, h,
66 link->format, align);
67 if (!link->frame_pool)
68 return NULL;
69 } else {
70 if (ff_frame_pool_get_video_config(link->frame_pool,
71 &pool_width, &pool_height,
72 &pool_format, &pool_align) < 0) {
73 return NULL;
74 }
75
76 if (pool_width != w || pool_height != h ||
77 pool_format != link->format || pool_align != align) {
78
79 ff_frame_pool_uninit((FFFramePool **)&link->frame_pool);
80 link->frame_pool = ff_frame_pool_video_init(av_buffer_allocz, w, h,
81 link->format, align);
82 if (!link->frame_pool)
83 return NULL;
84 }
85 }
86
87 frame = ff_frame_pool_get(link->frame_pool);
88 if (!frame)
89 return NULL;
90
91 frame->sample_aspect_ratio = link->sample_aspect_ratio;
92
93 return frame;
94 }
95
ff_default_get_video_buffer(AVFilterLink * link,int w,int h)96 AVFrame *ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
97 {
98 return ff_default_get_video_buffer2(link, w, h, av_cpu_max_align());
99 }
100
ff_get_video_buffer(AVFilterLink * link,int w,int h)101 AVFrame *ff_get_video_buffer(AVFilterLink *link, int w, int h)
102 {
103 AVFrame *ret = NULL;
104
105 FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
106
107 if (link->dstpad->get_buffer.video)
108 ret = link->dstpad->get_buffer.video(link, w, h);
109
110 if (!ret)
111 ret = ff_default_get_video_buffer(link, w, h);
112
113 return ret;
114 }
115