vsrc_buffer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Vitor Sessak
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include "avfilter.h"
27 #include "buffersrc.h"
28 #include "vsrc_buffer.h"
29 #include "libavutil/imgutils.h"
30 
31 typedef struct {
33  int h, w;
38 
39 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
40  if (c->w != width || c->h != height || c->pix_fmt != format) {\
41  av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
42  return AVERROR(EINVAL);\
43  }
44 
46  int64_t pts, AVRational pixel_aspect)
47 {
48  BufferSourceContext *c = buffer_filter->priv;
49 
50  if (c->buf) {
51  av_log(buffer_filter, AV_LOG_ERROR,
52  "Buffering several frames is not supported. "
53  "Please consume all available frames before adding a new one.\n"
54  );
55  //return -1;
56  }
57 
58  CHECK_PARAM_CHANGE(buffer_filter, c, frame->width, frame->height, frame->format);
59 
60  c->buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
61  c->w, c->h);
62  av_image_copy(c->buf->data, c->buf->linesize, frame->data, frame->linesize,
63  c->pix_fmt, c->w, c->h);
64 
65  avfilter_copy_frame_props(c->buf, frame);
66  c->buf->pts = pts;
68 
69  return 0;
70 }
71 
73 {
74  BufferSourceContext *c = s->priv;
75 
76  if (c->buf) {
78  "Buffering several frames is not supported. "
79  "Please consume all available frames before adding a new one.\n"
80  );
81  return AVERROR(EINVAL);
82  }
83 
84  CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
85 
86  c->buf = buf;
87 
88  return 0;
89 }
90 
91 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
92 {
93  BufferSourceContext *c = ctx->priv;
94  char pix_fmt_str[128];
95  int n = 0;
96 
97  if (!args ||
98  (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
99  &c->time_base.num, &c->time_base.den,
100  &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
101  av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
102  return AVERROR(EINVAL);
103  }
104  if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
105  char *tail;
106  c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
107  if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
108  av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
109  return AVERROR(EINVAL);
110  }
111  }
112 
113  av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name);
114  return 0;
115 }
116 
117 static av_cold void uninit(AVFilterContext *ctx)
118 {
119  BufferSourceContext *s = ctx->priv;
120  if (s->buf)
122  s->buf = NULL;
123 }
124 
126 {
127  BufferSourceContext *c = ctx->priv;
128  enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
129 
131  return 0;
132 }
133 
134 static int config_props(AVFilterLink *link)
135 {
136  BufferSourceContext *c = link->src->priv;
137 
138  link->w = c->w;
139  link->h = c->h;
141  link->time_base = c->time_base;
142 
143  return 0;
144 }
145 
146 static int request_frame(AVFilterLink *link)
147 {
148  BufferSourceContext *c = link->src->priv;
149 
150  if (!c->buf) {
151  av_log(link->src, AV_LOG_ERROR,
152  "request_frame() called with no available frame!\n");
153  //return -1;
154  }
155 
157  avfilter_draw_slice(link, 0, link->h, 1);
158  avfilter_end_frame(link);
160  c->buf = NULL;
161 
162  return 0;
163 }
164 
165 static int poll_frame(AVFilterLink *link)
166 {
167  BufferSourceContext *c = link->src->priv;
168  return !!c->buf;
169 }
170 
172  .name = "buffer",
173  .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
174  .priv_size = sizeof(BufferSourceContext),
176 
177  .init = init,
178  .uninit = uninit,
179 
180  .inputs = (AVFilterPad[]) {{ .name = NULL }},
181  .outputs = (AVFilterPad[]) {{ .name = "default",
182  .type = AVMEDIA_TYPE_VIDEO,
183  .request_frame = request_frame,
184  .poll_frame = poll_frame,
185  .config_props = config_props, },
186  { .name = NULL}},
187 };
AVFilter avfilter_vsrc_buffer
Definition: vsrc_buffer.c:171
static int config_props(AVFilterLink *link)
Definition: vsrc_buffer.c:134
AVFilterBufferRef * avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: avfilter.c:289
Audio Video Frame.
Definition: avcodec.h:985
AVRational pixel_aspect
Definition: vsrc_buffer.c:36
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:141
int linesize[8]
number of bytes per line
Definition: avfilter.h:127
misc image utilities
Memory buffer source API.
enum PixelFormat pix_fmt
Definition: v4l.c:65
int num
numerator
Definition: rational.h:44
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:160
static const AVRational pixel_aspect[17]
Definition: h264_ps.c:43
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
Definition: vsrc_buffer.c:91
static int request_frame(AVFilterLink *link)
Definition: vsrc_buffer.c:146
#define av_cold
Definition: attributes.h:71
enum PixelFormat pix_fmt
Definition: vsrc_buffer.c:34
void avfilter_end_frame(AVFilterLink *link)
Notifie the next filter that the current frame has finished.
Definition: avfilter.c:430
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:109
const char * name
Definition: pixdesc.h:56
int64_t pts
presentation timestamp.
Definition: avfilter.h:135
int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
Add a buffer to the filtergraph s.
Definition: vsrc_buffer.c:72
A filter pad used for either input or output.
Definition: avfilter.h:312
int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
Copy the frame properties of src to dst, without copying the actual image data.
Definition: avfilter.c:685
int width
width and height of the video frame
Definition: avcodec.h:1299
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: defaults.c:234
int h
image height
Definition: avfilter.h:108
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void * priv
private data for use by the filter
Definition: avfilter.h:553
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVFilterFormats * avfilter_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:90
AVRational time_base
time_base to set in the output link
Definition: vsrc_buffer.c:35
const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB]
The array of all the pixel format descriptors.
Definition: pixdesc.c:119
AVFilterBufferRef * buf
Definition: vsrc_buffer.c:32
static int poll_frame(AVFilterLink *link)
Definition: vsrc_buffer.c:165
static int query_formats(AVFilterContext *ctx)
Definition: vsrc_buffer.c:125
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum PixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:237
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
Definition: avfilter.c:447
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: avfilter.c:73
int format
format of the frame, -1 if unknown or unset Values correspond to enum PixelFormat for video frames...
Definition: avcodec.h:1308
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
Filter definition.
Definition: avfilter.h:497
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: avfilter.c:47
rational number numerator/denominator
Definition: rational.h:43
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
const char * name
filter name
Definition: avfilter.h:498
#define CHECK_PARAM_CHANGE(s, c, width, height, format)
Definition: vsrc_buffer.c:39
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
PixelFormat
Pixel format.
Definition: pixfmt.h:62
int den
denominator
Definition: rational.h:45
static av_cold void uninit(AVFilterContext *ctx)
Definition: vsrc_buffer.c:117
memory buffer source API for video
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:82
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:126
int format
media format
Definition: avfilter.h:128
int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame, int64_t pts, AVRational pixel_aspect)
Definition: vsrc_buffer.c:45
An instance of a filter.
Definition: avfilter.h:538
int height
Definition: avcodec.h:1299
#define AV_LOG_INFO
Definition: log.h:119
enum PixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1116