sd_ass: initialize structs for external tracks properly
[mplayer.git] / libmpcodecs / vf_pullup.c
blob056f9528394b98983018878221a0bfb60fa30a49
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "cpudetect.h"
27 #include "img_format.h"
28 #include "mp_image.h"
29 #include "vf.h"
31 #include "libvo/fastmemcpy.h"
33 #include "pullup.h"
35 #undef MAX
36 #define MAX(a,b) ((a)>(b)?(a):(b))
38 struct vf_priv_s {
39 struct pullup_context *ctx;
40 int init;
41 int fakecount;
42 char *qbuf;
43 double lastpts;
46 static void init_pullup(struct vf_instance *vf, mp_image_t *mpi)
48 struct pullup_context *c = vf->priv->ctx;
50 if (mpi->flags & MP_IMGFLAG_PLANAR) {
51 c->format = PULLUP_FMT_Y;
52 c->nplanes = 4;
53 pullup_preinit_context(c);
54 c->bpp[0] = c->bpp[1] = c->bpp[2] = 8;
55 c->w[0] = mpi->w;
56 c->h[0] = mpi->h;
57 c->w[1] = c->w[2] = mpi->chroma_width;
58 c->h[1] = c->h[2] = mpi->chroma_height;
59 c->w[3] = ((mpi->w+15)/16) * ((mpi->h+15)/16);
60 c->h[3] = 2;
61 c->stride[0] = mpi->width;
62 c->stride[1] = c->stride[2] = mpi->chroma_width;
63 c->stride[3] = c->w[3];
64 c->background[1] = c->background[2] = 128;
67 if (gCpuCaps.hasMMX) c->cpu |= PULLUP_CPU_MMX;
68 if (gCpuCaps.hasMMX2) c->cpu |= PULLUP_CPU_MMX2;
69 if (gCpuCaps.has3DNow) c->cpu |= PULLUP_CPU_3DNOW;
70 if (gCpuCaps.has3DNowExt) c->cpu |= PULLUP_CPU_3DNOWEXT;
71 if (gCpuCaps.hasSSE) c->cpu |= PULLUP_CPU_SSE;
72 if (gCpuCaps.hasSSE2) c->cpu |= PULLUP_CPU_SSE2;
74 pullup_init_context(c);
76 vf->priv->init = 1;
77 vf->priv->qbuf = malloc(c->w[3]);
81 #if 0
82 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
84 struct pullup_context *c = vf->priv->ctx;
85 struct pullup_buffer *b;
87 if (mpi->type == MP_IMGTYPE_STATIC) return;
89 if (!vf->priv->init) init_pullup(vf, mpi);
91 b = pullup_get_buffer(c, 2);
92 if (!b) return; /* shouldn't happen... */
94 mpi->priv = b;
96 mpi->planes[0] = b->planes[0];
97 mpi->planes[1] = b->planes[1];
98 mpi->planes[2] = b->planes[2];
99 mpi->stride[0] = c->stride[0];
100 mpi->stride[1] = c->stride[1];
101 mpi->stride[2] = c->stride[2];
103 mpi->flags |= MP_IMGFLAG_DIRECT;
104 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
106 #endif
108 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
110 struct pullup_context *c = vf->priv->ctx;
111 struct pullup_buffer *b;
112 struct pullup_frame *f;
113 mp_image_t *dmpi;
114 int ret;
115 int p;
116 int i;
118 if (!vf->priv->init) init_pullup(vf, mpi);
120 if (mpi->flags & MP_IMGFLAG_DIRECT) {
121 b = mpi->priv;
122 mpi->priv = 0;
123 } else {
124 b = pullup_get_buffer(c, 2);
125 if (!b) {
126 mp_msg(MSGT_VFILTER,MSGL_ERR,"Could not get buffer from pullup!\n");
127 f = pullup_get_frame(c);
128 pullup_release_frame(f);
129 return 0;
131 memcpy_pic(b->planes[0], mpi->planes[0], mpi->w, mpi->h,
132 c->stride[0], mpi->stride[0]);
133 if (mpi->flags & MP_IMGFLAG_PLANAR) {
134 memcpy_pic(b->planes[1], mpi->planes[1],
135 mpi->chroma_width, mpi->chroma_height,
136 c->stride[1], mpi->stride[1]);
137 memcpy_pic(b->planes[2], mpi->planes[2],
138 mpi->chroma_width, mpi->chroma_height,
139 c->stride[2], mpi->stride[2]);
142 if (mpi->qscale) {
143 fast_memcpy(b->planes[3], mpi->qscale, c->w[3]);
144 fast_memcpy(b->planes[3]+c->w[3], mpi->qscale, c->w[3]);
147 p = mpi->fields & MP_IMGFIELD_TOP_FIRST ? 0 :
148 (mpi->fields & MP_IMGFIELD_ORDERED ? 1 : 0);
150 if (pts == MP_NOPTS_VALUE) {
151 pullup_submit_field(c, b, p, MP_NOPTS_VALUE);
152 pullup_submit_field(c, b, p^1, MP_NOPTS_VALUE);
153 if (mpi->fields & MP_IMGFIELD_REPEAT_FIRST)
154 pullup_submit_field(c, b, p, MP_NOPTS_VALUE);
155 } else {
156 double delta;
157 if (vf->priv->lastpts == MP_NOPTS_VALUE)
158 delta = 1001.0/60000.0; // delta = field time distance
159 else
160 delta = (pts - vf->priv->lastpts) / 2;
161 if (delta <= 0.0 || delta >= 0.5)
162 delta = 0.0;
163 vf->priv->lastpts = pts;
164 if (mpi->fields & MP_IMGFIELD_REPEAT_FIRST) {
165 pullup_submit_field(c, b, p, pts - delta);
166 pullup_submit_field(c, b, p^1, pts);
167 pullup_submit_field(c, b, p, pts + delta);
168 } else {
169 pullup_submit_field(c, b, p, pts - delta * 0.5);
170 pullup_submit_field(c, b, p^1, pts + delta * 0.5);
174 pullup_release_buffer(b, 2);
176 f = pullup_get_frame(c);
178 /* Fake yes for first few frames (buffer depth) to keep from
179 * breaking A/V sync with G1's bad architecture... */
180 if (!f) return vf->priv->fakecount ? (--vf->priv->fakecount,1) : 0;
182 if (f->length < 2) {
183 pullup_release_frame(f);
184 f = pullup_get_frame(c);
185 if (!f) return 0;
186 if (f->length < 2) {
187 pullup_release_frame(f);
188 if (!(mpi->fields & MP_IMGFIELD_REPEAT_FIRST))
189 return 0;
190 f = pullup_get_frame(c);
191 if (!f) return 0;
192 if (f->length < 2) {
193 pullup_release_frame(f);
194 return 0;
199 #if 0
200 /* Average qscale tables from both frames. */
201 if (mpi->qscale) {
202 for (i=0; i<c->w[3]; i++) {
203 vf->priv->qbuf[i] = (f->ofields[0]->planes[3][i]
204 + f->ofields[1]->planes[3][i+c->w[3]])>>1;
207 #else
208 /* Take worst of qscale tables from both frames. */
209 if (mpi->qscale) {
210 for (i=0; i<c->w[3]; i++) {
211 vf->priv->qbuf[i] = MAX(f->ofields[0]->planes[3][i], f->ofields[1]->planes[3][i+c->w[3]]);
214 #endif
216 /* If the frame isn't already exportable... */
217 while (!f->buffer) {
218 dmpi = vf_get_image(vf->next, mpi->imgfmt,
219 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
220 mpi->width, mpi->height);
221 /* FIXME: Is it ok to discard dmpi if it's not direct? */
222 if (!(dmpi->flags & MP_IMGFLAG_DIRECT)) {
223 pullup_pack_frame(c, f);
224 break;
226 /* Direct render fields into output buffer */
227 my_memcpy_pic(dmpi->planes[0], f->ofields[0]->planes[0],
228 mpi->w, mpi->h/2, dmpi->stride[0]*2, c->stride[0]*2);
229 my_memcpy_pic(dmpi->planes[0] + dmpi->stride[0],
230 f->ofields[1]->planes[0] + c->stride[0],
231 mpi->w, mpi->h/2, dmpi->stride[0]*2, c->stride[0]*2);
232 if (mpi->flags & MP_IMGFLAG_PLANAR) {
233 my_memcpy_pic(dmpi->planes[1], f->ofields[0]->planes[1],
234 mpi->chroma_width, mpi->chroma_height/2,
235 dmpi->stride[1]*2, c->stride[1]*2);
236 my_memcpy_pic(dmpi->planes[1] + dmpi->stride[1],
237 f->ofields[1]->planes[1] + c->stride[1],
238 mpi->chroma_width, mpi->chroma_height/2,
239 dmpi->stride[1]*2, c->stride[1]*2);
240 my_memcpy_pic(dmpi->planes[2], f->ofields[0]->planes[2],
241 mpi->chroma_width, mpi->chroma_height/2,
242 dmpi->stride[2]*2, c->stride[2]*2);
243 my_memcpy_pic(dmpi->planes[2] + dmpi->stride[2],
244 f->ofields[1]->planes[2] + c->stride[2],
245 mpi->chroma_width, mpi->chroma_height/2,
246 dmpi->stride[2]*2, c->stride[2]*2);
248 pullup_release_frame(f);
249 if (mpi->qscale) {
250 dmpi->qscale = vf->priv->qbuf;
251 dmpi->qstride = mpi->qstride;
252 dmpi->qscale_type = mpi->qscale_type;
254 return vf_next_put_image(vf, dmpi, f->pts);
256 dmpi = vf_get_image(vf->next, mpi->imgfmt,
257 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
258 mpi->width, mpi->height);
260 dmpi->planes[0] = f->buffer->planes[0];
261 dmpi->planes[1] = f->buffer->planes[1];
262 dmpi->planes[2] = f->buffer->planes[2];
264 dmpi->stride[0] = c->stride[0];
265 dmpi->stride[1] = c->stride[1];
266 dmpi->stride[2] = c->stride[2];
268 if (mpi->qscale) {
269 dmpi->qscale = vf->priv->qbuf;
270 dmpi->qstride = mpi->qstride;
271 dmpi->qscale_type = mpi->qscale_type;
273 ret = vf_next_put_image(vf, dmpi, f->pts);
274 pullup_release_frame(f);
275 return ret;
278 static int query_format(struct vf_instance *vf, unsigned int fmt)
280 /* FIXME - support more formats */
281 switch (fmt) {
282 case IMGFMT_YV12:
283 case IMGFMT_IYUV:
284 case IMGFMT_I420:
285 return vf_next_query_format(vf, fmt);
287 return 0;
290 static int config(struct vf_instance *vf,
291 int width, int height, int d_width, int d_height,
292 unsigned int flags, unsigned int outfmt)
294 if (height&3) return 0;
295 return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
298 static void uninit(struct vf_instance *vf)
300 pullup_free_context(vf->priv->ctx);
301 free(vf->priv);
304 static int vf_open(vf_instance_t *vf, char *args)
306 struct vf_priv_s *p;
307 struct pullup_context *c;
308 //vf->get_image = get_image;
309 vf->put_image = put_image;
310 vf->config = config;
311 vf->query_format = query_format;
312 vf->uninit = uninit;
313 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
314 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
315 p->ctx = c = pullup_alloc_context();
316 p->fakecount = 1;
317 c->verbose = verbose>0;
318 c->junk_left = c->junk_right = 1;
319 c->junk_top = c->junk_bottom = 4;
320 c->strict_breaks = 0;
321 c->metric_plane = 0;
322 if (args) {
323 sscanf(args, "%d:%d:%d:%d:%d:%d", &c->junk_left, &c->junk_right, &c->junk_top, &c->junk_bottom, &c->strict_breaks, &c->metric_plane);
325 return 1;
328 const vf_info_t vf_info_pullup = {
329 "pullup (from field sequence to frames)",
330 "pullup",
331 "Rich Felker",
333 vf_open,
334 NULL