ffmpeg / libavfilter / vsrc_buffer.c @ b8dddebf
History | View | Annotate | Download (4.56 KB)
1 | cf13f204 | Michael Niedermayer | /*
|
---|---|---|---|
2 | * Copyright (c) 2008 Vitor Sessak
|
||
3 | *
|
||
4 | * This file is part of FFmpeg.
|
||
5 | *
|
||
6 | * FFmpeg is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU Lesser General Public
|
||
8 | * License as published by the Free Software Foundation; either
|
||
9 | * version 2.1 of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * FFmpeg is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
14 | * Lesser General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU Lesser General Public
|
||
17 | * License along with FFmpeg; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
19 | */
|
||
20 | |||
21 | b5634e45 | Stefano Sabatini | /**
|
22 | * @file
|
||
23 | * memory buffer source filter
|
||
24 | */
|
||
25 | |||
26 | cf13f204 | Michael Niedermayer | #include "avfilter.h" |
27 | #include "vsrc_buffer.h" |
||
28 | |||
29 | typedef struct { |
||
30 | int64_t pts; |
||
31 | AVFrame frame; |
||
32 | int has_frame;
|
||
33 | 9164afcb | Carl Eugen Hoyos | int h, w;
|
34 | enum PixelFormat pix_fmt;
|
||
35 | cf13f204 | Michael Niedermayer | AVRational pixel_aspect; |
36 | } BufferSourceContext; |
||
37 | |||
38 | int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
|
||
39 | int64_t pts, AVRational pixel_aspect) |
||
40 | { |
||
41 | BufferSourceContext *c = buffer_filter->priv; |
||
42 | |||
43 | if (c->has_frame) {
|
||
44 | av_log(buffer_filter, AV_LOG_ERROR, |
||
45 | "Buffering several frames is not supported. "
|
||
46 | "Please consume all available frames before adding a new one.\n"
|
||
47 | ); |
||
48 | //return -1;
|
||
49 | } |
||
50 | |||
51 | memcpy(c->frame.data , frame->data , sizeof(frame->data));
|
||
52 | memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
|
||
53 | efdc74ef | Michael Niedermayer | c->frame.interlaced_frame= frame->interlaced_frame; |
54 | c->frame.top_field_first = frame->top_field_first; |
||
55 | cf13f204 | Michael Niedermayer | c->pts = pts; |
56 | c->pixel_aspect = pixel_aspect; |
||
57 | c->has_frame = 1;
|
||
58 | |||
59 | return 0; |
||
60 | } |
||
61 | |||
62 | static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
||
63 | { |
||
64 | BufferSourceContext *c = ctx->priv; |
||
65 | b8dddebf | Stefano Sabatini | int n = 0; |
66 | cf13f204 | Michael Niedermayer | |
67 | b8dddebf | Stefano Sabatini | if (!args || (n = sscanf(args, "%d:%d:%d", &c->w, &c->h, &c->pix_fmt)) != 3) { |
68 | av_log(ctx, AV_LOG_ERROR, "Expected 3 arguments, but only %d found in '%s'\n", n, args ? args : ""); |
||
69 | return AVERROR(EINVAL);
|
||
70 | } |
||
71 | cf13f204 | Michael Niedermayer | |
72 | b8dddebf | Stefano Sabatini | return 0; |
73 | cf13f204 | Michael Niedermayer | } |
74 | |||
75 | static int query_formats(AVFilterContext *ctx) |
||
76 | { |
||
77 | BufferSourceContext *c = ctx->priv; |
||
78 | enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
|
||
79 | |||
80 | avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); |
||
81 | return 0; |
||
82 | } |
||
83 | |||
84 | static int config_props(AVFilterLink *link) |
||
85 | { |
||
86 | BufferSourceContext *c = link->src->priv; |
||
87 | |||
88 | link->w = c->w; |
||
89 | link->h = c->h; |
||
90 | |||
91 | return 0; |
||
92 | } |
||
93 | |||
94 | static int request_frame(AVFilterLink *link) |
||
95 | { |
||
96 | BufferSourceContext *c = link->src->priv; |
||
97 | AVFilterPicRef *picref; |
||
98 | |||
99 | if (!c->has_frame) {
|
||
100 | av_log(link->src, AV_LOG_ERROR, |
||
101 | "request_frame() called with no available frame!\n");
|
||
102 | //return -1;
|
||
103 | } |
||
104 | |||
105 | /* This picture will be needed unmodified later for decoding the next
|
||
106 | * frame */
|
||
107 | picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE | |
||
108 | AV_PERM_REUSE2, |
||
109 | link->w, link->h); |
||
110 | |||
111 | av_picture_copy((AVPicture *)&picref->data, (AVPicture *)&c->frame, |
||
112 | picref->pic->format, link->w, link->h); |
||
113 | |||
114 | 4055438b | Stefano Sabatini | picref->pts = c->pts; |
115 | picref->pixel_aspect = c->pixel_aspect; |
||
116 | efdc74ef | Michael Niedermayer | picref->interlaced = c->frame.interlaced_frame; |
117 | picref->top_field_first = c->frame.top_field_first; |
||
118 | cf13f204 | Michael Niedermayer | avfilter_start_frame(link, avfilter_ref_pic(picref, ~0));
|
119 | avfilter_draw_slice(link, 0, link->h, 1); |
||
120 | avfilter_end_frame(link); |
||
121 | avfilter_unref_pic(picref); |
||
122 | |||
123 | c->has_frame = 0;
|
||
124 | |||
125 | return 0; |
||
126 | } |
||
127 | |||
128 | static int poll_frame(AVFilterLink *link) |
||
129 | { |
||
130 | BufferSourceContext *c = link->src->priv; |
||
131 | return !!(c->has_frame);
|
||
132 | } |
||
133 | |||
134 | 4055438b | Stefano Sabatini | AVFilter avfilter_vsrc_buffer = { |
135 | cf13f204 | Michael Niedermayer | .name = "buffer",
|
136 | ce1f8536 | Stefano Sabatini | .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
|
137 | cf13f204 | Michael Niedermayer | .priv_size = sizeof(BufferSourceContext),
|
138 | .query_formats = query_formats, |
||
139 | |||
140 | .init = init, |
||
141 | |||
142 | .inputs = (AVFilterPad[]) {{ .name = NULL }},
|
||
143 | .outputs = (AVFilterPad[]) {{ .name = "default",
|
||
144 | .type = AVMEDIA_TYPE_VIDEO, |
||
145 | .request_frame = request_frame, |
||
146 | .poll_frame = poll_frame, |
||
147 | .config_props = config_props, }, |
||
148 | { .name = NULL}},
|
||
149 | }; |