annotate libmpcodecs/vf_lavfi.c @ 34754:9d4720deada1

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