annotate libmpcodecs/vf_lavfi.c @ 34341:946ecaf41dc1

Don't allow option gui-include in config files. This option may only be evaluated after MPlayer has called cfg_read() or a segmentation fault occurs. Prevent this by restricting its usage.
author ib
date Sun, 11 Dec 2011 14:54:56 +0000
parents a93891202051
children b6191f9936ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34102
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
1 /*
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
2 * Copyright (C) 2011 Nicolas George <nicolas.george@normalesup.org>
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
3 *
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
4 * This file is part of MPlayer.
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
5 *
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
6 * MPlayer is free software; you can redistribute it and/or modify
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
9 * (at your option) any later version.
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
10 *
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
11 * MPlayer is distributed in the hope that it will be useful,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
14 * GNU General Public License for more details.
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
15 *
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License along
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
19 */
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
20
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
21 #include "vf.h"
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
22 #include "m_struct.h"
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
23 #include "fmt-conversion.h"
34174
a93891202051 Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents: 34102
diff changeset
24 #include "mp_msg.h"
34102
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
25 #include "libavfilter/avfilter.h"
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
26 #include "libavfilter/avfiltergraph.h"
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
27 #include "libavutil/pixdesc.h"
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
28
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
29 struct vf_priv_s {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
30 AVFilterGraph *graph;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
31 AVFilterContext *in;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
32 AVFilterContext *out;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
33 int in_w;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
34 int in_h;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
35 enum PixelFormat in_pixfmt;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
36 int in_imgfmt;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
37 AVRational in_sar;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
38 int out_w;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
39 int out_h;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
40 enum PixelFormat out_pixfmt;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
41 int out_imgfmt;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
42 AVRational out_sar;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
43 struct AVFilterBufferRef *in_buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
44 mp_image_t *in_mpi;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
45 };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
46
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
47 static void buf_mpi_free(AVFilterBuffer *buf)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
48 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
49 ((mp_image_t *)buf->priv)->usage_count--;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
50 av_free(buf);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
51 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
52
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
53 static AVFilterBufferRef *mpi_to_bufref(mp_image_t *mpi, enum PixelFormat fmt,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
54 AVRational sar)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
55 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
56 AVFilterBufferRef *buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
57 int perms = AV_PERM_READ;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
58
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
59 if ((mpi->flags & MP_IMGFLAG_ALLOCATED))
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
60 perms |= AV_PERM_REUSE2;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
61 if (!(mpi->flags & MP_IMGFLAG_PRESERVE))
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
62 perms |= AV_PERM_WRITE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
63 buf = avfilter_get_video_buffer_ref_from_arrays(mpi->planes, mpi->stride,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
64 perms,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
65 mpi->w, mpi->h,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
66 fmt);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
67 buf->video->sample_aspect_ratio = sar;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
68 buf->buf->priv = mpi;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
69 buf->buf->free = buf_mpi_free;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
70 return buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
71 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
72
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
73 static void bufref_to_mpi(AVFilterBufferRef *ref, mp_image_t *mpi)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
74 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
75 memcpy(mpi->planes, ref->data, sizeof(ref->data));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
76 memcpy(mpi->stride, ref->linesize, sizeof(ref->linesize));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
77 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
78
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
79 struct mpsink_priv {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
80 struct vf_instance *vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
81 };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
82
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
83 static int mpsink_init(AVFilterContext *ctx,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
84 av_unused const char *args, void *opaque)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
85 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
86 struct mpsink_priv *c = ctx->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
87 c->vf = opaque;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
88 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
89 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
90
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
91 static int mpsink_query_formats(AVFilterContext *ctx)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
92 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
93 struct mpsink_priv *c = ctx->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
94 struct vf_instance *vf = c->vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
95 AVFilterFormats *all;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
96 enum PixelFormat *sup;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
97 unsigned i, nsup = 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
98 int ifmt;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
99
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
100 all = avfilter_all_formats(AVMEDIA_TYPE_VIDEO);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
101 sup = av_mallocz(sizeof(*sup) * (all->format_count + 1));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
102 if (!sup)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
103 return AVERROR(errno);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
104 for(i = 0; i < all->format_count; i++) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
105 ifmt = pixfmt2imgfmt(all->formats[i]);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
106 if (vf->next->query_format(vf->next, ifmt) > 0)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
107 sup[nsup++] = all->formats[i];
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
108 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
109 sup[nsup++] = PIX_FMT_NONE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
110 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(sup));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
111 av_free(sup);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
112 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
113 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
114
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
115 static AVFilterBufferRef *mpsink_get_video_buffer(AVFilterLink *link,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
116 int perms, int w, int h)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
117 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
118 struct mpsink_priv *c = link->dst->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
119 struct vf_instance *vf = c->vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
120 mp_image_t *dmpi;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
121 int type;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
122 int flags = 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
123
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
124 type = MP_IMGTYPE_NUMBERED | (-1 << 16);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
125 if ((perms & AV_PERM_PRESERVE))
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
126 flags |= MP_IMGFLAG_PRESERVE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
127 if ((perms & AV_PERM_READ))
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
128 flags |= MP_IMGFLAG_READABLE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
129 if ((perms & AV_PERM_NEG_LINESIZES))
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
130 flags |= MP_IMGFLAG_ACCEPT_STRIDE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
131 if (vf->priv->in_mpi) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
132 type = vf->priv->in_mpi->type;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
133 vf->priv->in_mpi = NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
134 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
135 dmpi = vf_get_image(vf->next, vf->priv->out_imgfmt, type, flags, w, h);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
136 return mpi_to_bufref(dmpi, vf->priv->out_pixfmt, vf->priv->out_sar);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
137 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
138
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
139 static void mpsink_end_frame(AVFilterLink *link)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
140 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
141 struct mpsink_priv *c = link->dst->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
142 struct vf_instance *vf = c->vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
143 AVFilterBufferRef *buf = link->cur_buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
144 mp_image_t *mpi = buf->buf->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
145 double pts;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
146
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
147 pts = buf->pts == (int64_t)AV_NOPTS_VALUE ? MP_NOPTS_VALUE :
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
148 buf->pts * av_q2d(link->time_base);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
149 mpi->pict_type = buf->video->pict_type;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
150 mpi->fields = (buf->video->interlaced ? MP_IMGFIELD_INTERLACED : 0) |
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
151 (buf->video->top_field_first ? MP_IMGFIELD_TOP_FIRST : 0);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
152 vf_next_put_image(vf, mpi, pts);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
153 avfilter_unref_buffer(link->cur_buf);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
154 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
155
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
156 static AVFilter mpsink = {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
157 .name = "mpsink",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
158 .description = "Video sink for mplayer interaction",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
159 .priv_size = sizeof(struct mpsink_priv),
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
160
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
161 .init = mpsink_init,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
162 .query_formats = mpsink_query_formats,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
163
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
164 .inputs = (AVFilterPad[]) {{ .name = "default",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
165 .type = AVMEDIA_TYPE_VIDEO,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
166 .end_frame = mpsink_end_frame,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
167 .get_video_buffer = mpsink_get_video_buffer,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
168 .min_perms = AV_PERM_READ, },
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
169 { .name = NULL }},
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
170 .outputs = (AVFilterPad[]) {{ .name = NULL }},
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
171 };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
172
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
173 struct mpsrc_priv {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
174 struct vf_instance *vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
175 };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
176
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
177 static int mpsrc_init(AVFilterContext *ctx,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
178 av_unused const char *args, void *opaque)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
179 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
180 struct mpsrc_priv *c = ctx->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
181 c->vf = opaque;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
182 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
183 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
184
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
185 static int mpsrc_query_formats(AVFilterContext *ctx)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
186 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
187 struct mpsrc_priv *c = ctx->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
188 enum PixelFormat pix_fmts[] = { c->vf->priv->in_pixfmt, PIX_FMT_NONE };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
189 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
190 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
191 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
192
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
193 static int mpsrc_config_props(AVFilterLink *link)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
194 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
195 struct mpsrc_priv *c = link->src->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
196 struct vf_instance *vf = c->vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
197 link->w = vf->priv->in_w;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
198 link->h = vf->priv->in_h;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
199 link->sample_aspect_ratio = vf->priv->in_sar;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
200 link->time_base = AV_TIME_BASE_Q;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
201 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
202 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
203
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
204 static int mpsrc_request_frame(AVFilterLink *link)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
205 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
206 struct mpsrc_priv *c = link->src->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
207 struct vf_instance *vf = c->vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
208
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
209 if (!vf->priv->in_buf)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
210 return AVERROR(EINVAL);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
211 avfilter_start_frame(link, avfilter_ref_buffer(vf->priv->in_buf, ~0));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
212 avfilter_draw_slice(link, 0, link->h, 1);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
213 avfilter_end_frame(link);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
214 vf->priv->in_buf = NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
215 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
216 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
217
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
218 static int mpsrc_poll_frame(AVFilterLink *link)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
219 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
220 struct mpsrc_priv *c = link->src->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
221 struct vf_instance *vf = c->vf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
222 return vf->priv->in_buf != NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
223 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
224
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
225 AVFilter mpsrc = {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
226 .name = "mpsrc",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
227 .description = "Video source for mplayer interaction",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
228 .priv_size = sizeof(struct mpsrc_priv),
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
229 .query_formats = mpsrc_query_formats,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
230
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
231 .init = mpsrc_init,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
232
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
233 .inputs = (AVFilterPad[]) {{ .name = NULL }},
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
234 .outputs = (AVFilterPad[]) {{ .name = "default",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
235 .type = AVMEDIA_TYPE_VIDEO,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
236 .request_frame = mpsrc_request_frame,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
237 .poll_frame = mpsrc_poll_frame,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
238 .config_props = mpsrc_config_props, },
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
239 { .name = NULL }},
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
240 };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
241
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
242 static int config(struct vf_instance *vf, int w, int h, int dw, int dh,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
243 unsigned flags, unsigned fmt)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
244 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
245 int ret;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
246 AVFilterLink *out;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
247 AVRational iar, dar;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
248
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
249 av_reduce(&iar.num, &iar.den, w, h, INT_MAX);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
250 av_reduce(&dar.num, &dar.den, dw, dh, INT_MAX);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
251 vf->priv->in_pixfmt = imgfmt2pixfmt(fmt);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
252 vf->priv->in_imgfmt = fmt;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
253 vf->priv->in_w = w;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
254 vf->priv->in_h = h;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
255 vf->priv->in_sar = av_div_q(dar, iar);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
256 ret = avfilter_graph_config(vf->priv->graph, NULL);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
257 if (ret < 0)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
258 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
259 out = vf->priv->out->inputs[0];
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
260 vf->priv->out_w = out->w;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
261 vf->priv->out_h = out->h;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
262 vf->priv->out_pixfmt = out->format;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
263 vf->priv->out_imgfmt = pixfmt2imgfmt(out->format);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
264 vf->priv->out_sar = out->sample_aspect_ratio;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
265 if (vf->priv->out_sar.num != vf->priv->in_sar.num ||
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
266 vf->priv->out_sar.den != vf->priv->in_sar.den ||
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
267 out->w != w || out->h != h) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
268 av_reduce(&iar.num, &iar.den, out->w, out->h, INT_MAX);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
269 dar = av_mul_q(iar, out->sample_aspect_ratio);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
270 if (av_cmp_q(dar, iar) >= 0) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
271 dh = out->h;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
272 dw = av_rescale(dh, dar.num, dar.den);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
273 } else {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
274 dw = out->w;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
275 dh = av_rescale(dw, dar.den, dar.num);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
276 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
277 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
278 return vf_next_config(vf, out->w, out->h, dw, dh, flags, fmt);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
279 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
280
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
281 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
282 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
283 AVFilterBufferRef *buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
284 unsigned perms = AV_PERM_WRITE | AV_PERM_REUSE2;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
285
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
286 avfilter_unref_buffer(mpi->priv);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
287 mpi->priv = NULL; /* for safety */
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
288 if (mpi->flags & MP_IMGFLAG_READABLE)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
289 perms |= AV_PERM_READ;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
290 if (mpi->flags & MP_IMGFLAG_PRESERVE)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
291 perms |= AV_PERM_PRESERVE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
292 vf->priv->in_mpi = mpi;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
293 buf = avfilter_get_video_buffer(vf->priv->in->outputs[0], perms,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
294 mpi->w, mpi->h);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
295 vf->priv->in_mpi = NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
296 bufref_to_mpi(buf, mpi);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
297 mpi->flags |= MP_IMGFLAG_DIRECT;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
298 mpi->flags &= ~MP_IMGFLAG_ALLOCATED;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
299 mpi->priv = buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
300 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
301
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
302 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
303 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
304 AVFilterBufferRef *buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
305 mp_image_t *cmpi = NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
306
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
307 if (!(mpi->flags & MP_IMGFLAG_DIRECT)) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
308 cmpi = vf_get_image(vf, mpi->imgfmt, MP_IMGTYPE_TEMP,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
309 MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
310 mpi->w, mpi->h);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
311 copy_mpi(cmpi, mpi);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
312 buf = cmpi->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
313 } else {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
314 buf = mpi->priv;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
315 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
316 buf->video->key_frame = mpi->pict_type == 1;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
317 buf->video->pict_type = mpi->pict_type; /* seems to be the same code */
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
318 buf->video->interlaced = !!(mpi->fields & MP_IMGFIELD_INTERLACED);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
319 buf->video->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
320 vf->priv->in_buf = buf;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
321 if (pts != MP_NOPTS_VALUE)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
322 buf->pts = pts * AV_TIME_BASE;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
323 while (avfilter_poll_frame(vf->priv->out->inputs[0])) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
324 if (avfilter_request_frame(vf->priv->out->inputs[0]))
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
325 break;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
326 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
327 return 1;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
328 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
329
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
330 static void uninit(struct vf_instance *vf)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
331 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
332 unsigned i;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
333
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
334 #define FREE_MPI_ARRAY(field) \
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
335 for (i = 0; i < FF_ARRAY_ELEMS(vf->imgctx.field); i++) \
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
336 if (vf->imgctx.field[i]) \
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
337 avfilter_unref_buffer(vf->imgctx.field[i]->priv);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
338 FREE_MPI_ARRAY(static_images);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
339 FREE_MPI_ARRAY(temp_images);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
340 FREE_MPI_ARRAY(export_images);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
341 FREE_MPI_ARRAY(numbered_images);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
342 avfilter_graph_free(&vf->priv->graph);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
343 av_free(vf->priv);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
344 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
345
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
346 static int lavfi_open(struct vf_instance *vf, char *args)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
347 {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
348 AVFilterInOut *outputs;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
349 AVFilterInOut *inputs;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
350 int ret;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
351
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
352 avfilter_register_all();
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
353 if (!args) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
354 mp_msg(MSGT_VFILTER, MSGL_ERR, "lavfi: filtergraph needed\n");
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
355 goto fail;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
356 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
357 if (args[0] == '$') {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
358 char *e = getenv(args + 1);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
359 if (!e) {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
360 mp_msg(MSGT_VFILTER, MSGL_ERR, "lavfi: %s not defined\n", args);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
361 goto fail;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
362 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
363 args = e;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
364 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
365 vf->priv = av_mallocz(sizeof(struct vf_priv_s));
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
366 if (!vf->priv)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
367 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
368
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
369 vf->priv->graph = avfilter_graph_alloc();
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
370 if (!vf->priv->graph)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
371 goto fail;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
372 ret = avfilter_graph_create_filter(&vf->priv->in, &mpsrc, "in",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
373 NULL, vf, vf->priv->graph);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
374 if (ret < 0)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
375 goto fail;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
376 ret = avfilter_graph_create_filter(&vf->priv->out, &mpsink, "out",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
377 NULL, vf, vf->priv->graph);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
378 if (ret < 0)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
379 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
380 outputs = avfilter_inout_alloc();
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
381 inputs = avfilter_inout_alloc();
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
382 if (!outputs || !inputs)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
383 goto fail;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
384 outputs->name = av_strdup("in");
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
385 outputs->filter_ctx = vf->priv->in;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
386 outputs->pad_idx = 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
387 outputs->next = NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
388 inputs->name = av_strdup("out");
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
389 inputs->filter_ctx = vf->priv->out;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
390 inputs->pad_idx = 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
391 inputs->next = NULL;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
392 ret = avfilter_graph_parse(vf->priv->graph, args, &inputs, &outputs, NULL);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
393 if (ret < 0)
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
394 goto fail;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
395
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
396 vf->config = config;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
397 vf->uninit = uninit;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
398 vf->put_image = put_image;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
399 vf->get_image = get_image;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
400 return 1;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
401
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
402 fail:
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
403 avfilter_inout_free(&inputs);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
404 avfilter_inout_free(&outputs);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
405 avfilter_graph_free(&vf->priv->graph);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
406 av_free(vf->priv);
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
407 return 0;
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
408 }
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
409
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
410 static const m_option_t vf_opts_fields[] = { { .name = NULL } };
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
411
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
412 const vf_info_t vf_info_lavfi = {
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
413 "libavfilter wrapper",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
414 "lavfi",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
415 "Nicolas George",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
416 "",
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
417 lavfi_open,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
418 NULL,
dd8320c2a2cb libmpcodec: add vf_lavfi.
cigaes
parents:
diff changeset
419 };