From d4bdd0473d6f43132257c9fb3848d829755167a3 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 5 Nov 2012 17:02:04 +0100 Subject: Rename directories, move files (step 1 of 2) (does not compile) Tis drops the silly lib prefixes, and attempts to organize the tree in a more logical way. Make the top-level directory less cluttered as well. Renames the following directories: libaf -> audio/filter libao2 -> audio/out libvo -> video/out libmpdemux -> demux Split libmpcodecs: vf* -> video/filter vd*, dec_video.* -> video/decode mp_image*, img_format*, ... -> video/ ad*, dec_audio.* -> audio/decode libaf/format.* is moved to audio/ - this is similar to how mp_image.* is located in video/. Move most top-level .c/.h files to core. (talloc.c/.h is left on top- level, because it's external.) Park some of the more annoying files in compat/. Some of these are relicts from the time mplayer used ffmpeg internals. sub/ is not split, because it's too much of a mess (subtitle code is mixed with OSD display and rendering). Maybe the organization of core is not ideal: it mixes playback core (like mplayer.c) and utility helpers (like bstr.c/h). Should the need arise, the playback core will be moved somewhere else, while core contains all helper and common code. --- video/filter/pullup.c | 817 ++++++++++++++++++++++++++++++++++++++++ video/filter/pullup.h | 102 +++++ video/filter/vf.c | 830 +++++++++++++++++++++++++++++++++++++++++ video/filter/vf.h | 189 ++++++++++ video/filter/vf_crop.c | 196 ++++++++++ video/filter/vf_delogo.c | 379 +++++++++++++++++++ video/filter/vf_divtc.c | 723 +++++++++++++++++++++++++++++++++++ video/filter/vf_dlopen.c | 389 +++++++++++++++++++ video/filter/vf_dlopen.h | 88 +++++ video/filter/vf_down3dright.c | 166 +++++++++ video/filter/vf_dsize.c | 125 +++++++ video/filter/vf_eq2.c | 519 ++++++++++++++++++++++++++ video/filter/vf_expand.c | 350 +++++++++++++++++ video/filter/vf_flip.c | 110 ++++++ video/filter/vf_format.c | 91 +++++ video/filter/vf_gradfun.c | 450 ++++++++++++++++++++++ video/filter/vf_hqdn3d.c | 373 ++++++++++++++++++ video/filter/vf_ilpack.c | 457 +++++++++++++++++++++++ video/filter/vf_mirror.c | 131 +++++++ video/filter/vf_noformat.c | 77 ++++ video/filter/vf_noise.c | 470 +++++++++++++++++++++++ video/filter/vf_phase.c | 303 +++++++++++++++ video/filter/vf_pp.c | 196 ++++++++++ video/filter/vf_pullup.c | 333 +++++++++++++++++ video/filter/vf_rotate.c | 152 ++++++++ video/filter/vf_scale.c | 718 +++++++++++++++++++++++++++++++++++ video/filter/vf_screenshot.c | 219 +++++++++++ video/filter/vf_softpulldown.c | 182 +++++++++ video/filter/vf_stereo3d.c | 527 ++++++++++++++++++++++++++ video/filter/vf_sub.c | 309 +++++++++++++++ video/filter/vf_swapuv.c | 106 ++++++ video/filter/vf_unsharp.c | 321 ++++++++++++++++ video/filter/vf_vo.c | 202 ++++++++++ video/filter/vf_yadif.c | 533 ++++++++++++++++++++++++++ 34 files changed, 11133 insertions(+) create mode 100644 video/filter/pullup.c create mode 100644 video/filter/pullup.h create mode 100644 video/filter/vf.c create mode 100644 video/filter/vf.h create mode 100644 video/filter/vf_crop.c create mode 100644 video/filter/vf_delogo.c create mode 100644 video/filter/vf_divtc.c create mode 100644 video/filter/vf_dlopen.c create mode 100644 video/filter/vf_dlopen.h create mode 100644 video/filter/vf_down3dright.c create mode 100644 video/filter/vf_dsize.c create mode 100644 video/filter/vf_eq2.c create mode 100644 video/filter/vf_expand.c create mode 100644 video/filter/vf_flip.c create mode 100644 video/filter/vf_format.c create mode 100644 video/filter/vf_gradfun.c create mode 100644 video/filter/vf_hqdn3d.c create mode 100644 video/filter/vf_ilpack.c create mode 100644 video/filter/vf_mirror.c create mode 100644 video/filter/vf_noformat.c create mode 100644 video/filter/vf_noise.c create mode 100644 video/filter/vf_phase.c create mode 100644 video/filter/vf_pp.c create mode 100644 video/filter/vf_pullup.c create mode 100644 video/filter/vf_rotate.c create mode 100644 video/filter/vf_scale.c create mode 100644 video/filter/vf_screenshot.c create mode 100644 video/filter/vf_softpulldown.c create mode 100644 video/filter/vf_stereo3d.c create mode 100644 video/filter/vf_sub.c create mode 100644 video/filter/vf_swapuv.c create mode 100644 video/filter/vf_unsharp.c create mode 100644 video/filter/vf_vo.c create mode 100644 video/filter/vf_yadif.c (limited to 'video/filter') diff --git a/video/filter/pullup.c b/video/filter/pullup.c new file mode 100644 index 0000000000..bd25c187d6 --- /dev/null +++ b/video/filter/pullup.c @@ -0,0 +1,817 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include "config.h" +#include "pullup.h" +#include "cpudetect.h" +#include "mpcommon.h" + + + +#if ARCH_X86 +#if HAVE_MMX +static int diff_y_mmx(unsigned char *a, unsigned char *b, int s) +{ + int ret; + __asm__ volatile ( + "movl $4, %%ecx \n\t" + "pxor %%mm4, %%mm4 \n\t" + "pxor %%mm7, %%mm7 \n\t" + + "1: \n\t" + + "movq (%%"REG_S"), %%mm0 \n\t" + "movq (%%"REG_S"), %%mm2 \n\t" + "add %%"REG_a", %%"REG_S" \n\t" + "movq (%%"REG_D"), %%mm1 \n\t" + "add %%"REG_a", %%"REG_D" \n\t" + "psubusb %%mm1, %%mm2 \n\t" + "psubusb %%mm0, %%mm1 \n\t" + "movq %%mm2, %%mm0 \n\t" + "movq %%mm1, %%mm3 \n\t" + "punpcklbw %%mm7, %%mm0 \n\t" + "punpcklbw %%mm7, %%mm1 \n\t" + "punpckhbw %%mm7, %%mm2 \n\t" + "punpckhbw %%mm7, %%mm3 \n\t" + "paddw %%mm0, %%mm4 \n\t" + "paddw %%mm1, %%mm4 \n\t" + "paddw %%mm2, %%mm4 \n\t" + "paddw %%mm3, %%mm4 \n\t" + + "decl %%ecx \n\t" + "jnz 1b \n\t" + + "movq %%mm4, %%mm3 \n\t" + "punpcklwd %%mm7, %%mm4 \n\t" + "punpckhwd %%mm7, %%mm3 \n\t" + "paddd %%mm4, %%mm3 \n\t" + "movd %%mm3, %%eax \n\t" + "psrlq $32, %%mm3 \n\t" + "movd %%mm3, %%edx \n\t" + "addl %%edx, %%eax \n\t" + "emms \n\t" + : "=a" (ret) + : "S" (a), "D" (b), "a" (s) + : "%ecx", "%edx" + ); + return ret; +} + +static int licomb_y_mmx(unsigned char *a, unsigned char *b, int s) +{ + int ret; + __asm__ volatile ( + "movl $4, %%ecx \n\t" + "pxor %%mm6, %%mm6 \n\t" + "pxor %%mm7, %%mm7 \n\t" + "sub %%"REG_a", %%"REG_D" \n\t" + + "2: \n\t" + + "movq (%%"REG_D"), %%mm0 \n\t" + "movq (%%"REG_D"), %%mm1 \n\t" + "punpcklbw %%mm7, %%mm0 \n\t" + "movq (%%"REG_D",%%"REG_a"), %%mm2 \n\t" + "punpcklbw %%mm7, %%mm1 \n\t" + "punpcklbw %%mm7, %%mm2 \n\t" + "paddw %%mm0, %%mm0 \n\t" + "paddw %%mm2, %%mm1 \n\t" + "movq %%mm0, %%mm2 \n\t" + "psubusw %%mm1, %%mm0 \n\t" + "psubusw %%mm2, %%mm1 \n\t" + "paddw %%mm0, %%mm6 \n\t" + "paddw %%mm1, %%mm6 \n\t" + + "movq (%%"REG_S"), %%mm0 \n\t" + "movq (%%"REG_D"), %%mm1 \n\t" + "punpckhbw %%mm7, %%mm0 \n\t" + "movq (%%"REG_D",%%"REG_a"), %%mm2 \n\t" + "punpckhbw %%mm7, %%mm1 \n\t" + "punpckhbw %%mm7, %%mm2 \n\t" + "paddw %%mm0, %%mm0 \n\t" + "paddw %%mm2, %%mm1 \n\t" + "movq %%mm0, %%mm2 \n\t" + "psubusw %%mm1, %%mm0 \n\t" + "psubusw %%mm2, %%mm1 \n\t" + "paddw %%mm0, %%mm6 \n\t" + "paddw %%mm1, %%mm6 \n\t" + + "movq (%%"REG_D",%%"REG_a"), %%mm0 \n\t" + "movq (%%"REG_S"), %%mm1 \n\t" + "punpcklbw %%mm7, %%mm0 \n\t" + "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t" + "punpcklbw %%mm7, %%mm1 \n\t" + "punpcklbw %%mm7, %%mm2 \n\t" + "paddw %%mm0, %%mm0 \n\t" + "paddw %%mm2, %%mm1 \n\t" + "movq %%mm0, %%mm2 \n\t" + "psubusw %%mm1, %%mm0 \n\t" + "psubusw %%mm2, %%mm1 \n\t" + "paddw %%mm0, %%mm6 \n\t" + "paddw %%mm1, %%mm6 \n\t" + + "movq (%%"REG_D",%%"REG_a"), %%mm0 \n\t" + "movq (%%"REG_S"), %%mm1 \n\t" + "punpckhbw %%mm7, %%mm0 \n\t" + "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t" + "punpckhbw %%mm7, %%mm1 \n\t" + "punpckhbw %%mm7, %%mm2 \n\t" + "paddw %%mm0, %%mm0 \n\t" + "paddw %%mm2, %%mm1 \n\t" + "movq %%mm0, %%mm2 \n\t" + "psubusw %%mm1, %%mm0 \n\t" + "psubusw %%mm2, %%mm1 \n\t" + "paddw %%mm0, %%mm6 \n\t" + "paddw %%mm1, %%mm6 \n\t" + + "add %%"REG_a", %%"REG_S" \n\t" + "add %%"REG_a", %%"REG_D" \n\t" + "decl %%ecx \n\t" + "jnz 2b \n\t" + + "movq %%mm6, %%mm5 \n\t" + "punpcklwd %%mm7, %%mm6 \n\t" + "punpckhwd %%mm7, %%mm5 \n\t" + "paddd %%mm6, %%mm5 \n\t" + "movd %%mm5, %%eax \n\t" + "psrlq $32, %%mm5 \n\t" + "movd %%mm5, %%edx \n\t" + "addl %%edx, %%eax \n\t" + + "emms \n\t" + : "=a" (ret) + : "S" (a), "D" (b), "a" (s) + : "%ecx", "%edx" + ); + return ret; +} + +static int var_y_mmx(unsigned char *a, unsigned char *b, int s) +{ + int ret; + __asm__ volatile ( + "movl $3, %%ecx \n\t" + "pxor %%mm4, %%mm4 \n\t" + "pxor %%mm7, %%mm7 \n\t" + + "1: \n\t" + + "movq (%%"REG_S"), %%mm0 \n\t" + "movq (%%"REG_S"), %%mm2 \n\t" + "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t" + "add %%"REG_a", %%"REG_S" \n\t" + "psubusb %%mm1, %%mm2 \n\t" + "psubusb %%mm0, %%mm1 \n\t" + "movq %%mm2, %%mm0 \n\t" + "movq %%mm1, %%mm3 \n\t" + "punpcklbw %%mm7, %%mm0 \n\t" + "punpcklbw %%mm7, %%mm1 \n\t" + "punpckhbw %%mm7, %%mm2 \n\t" + "punpckhbw %%mm7, %%mm3 \n\t" + "paddw %%mm0, %%mm4 \n\t" + "paddw %%mm1, %%mm4 \n\t" + "paddw %%mm2, %%mm4 \n\t" + "paddw %%mm3, %%mm4 \n\t" + + "decl %%ecx \n\t" + "jnz 1b \n\t" + + "movq %%mm4, %%mm3 \n\t" + "punpcklwd %%mm7, %%mm4 \n\t" + "punpckhwd %%mm7, %%mm3 \n\t" + "paddd %%mm4, %%mm3 \n\t" + "movd %%mm3, %%eax \n\t" + "psrlq $32, %%mm3 \n\t" + "movd %%mm3, %%edx \n\t" + "addl %%edx, %%eax \n\t" + "emms \n\t" + : "=a" (ret) + : "S" (a), "a" (s) + : "%ecx", "%edx" + ); + return 4*ret; +} +#endif +#endif + +#define ABS(a) (((a)^((a)>>31))-((a)>>31)) + +static int diff_y(unsigned char *a, unsigned char *b, int s) +{ + int i, j, diff=0; + for (i=4; i; i--) { + for (j=0; j<8; j++) diff += ABS(a[j]-b[j]); + a+=s; b+=s; + } + return diff; +} + +static int licomb_y(unsigned char *a, unsigned char *b, int s) +{ + int i, j, diff=0; + for (i=4; i; i--) { + for (j=0; j<8; j++) + diff += ABS((a[j]<<1) - b[j-s] - b[j]) + + ABS((b[j]<<1) - a[j] - a[j+s]); + a+=s; b+=s; + } + return diff; +} + +#if 0 +static int qpcomb_y(unsigned char *a, unsigned char *b, int s) +{ + int i, j, diff=0; + for (i=4; i; i--) { + for (j=0; j<8; j++) + diff += ABS(a[j] - 3*b[j-s] + 3*a[j+s] - b[j]); + a+=s; b+=s; + } + return diff; +} + +static int licomb_y_test(unsigned char *a, unsigned char *b, int s) +{ + int c = licomb_y(a,b,s); + int m = licomb_y_mmx(a,b,s); + if (c != m) printf("%d != %d\n", c, m); + return m; +} +#endif + +static int var_y(unsigned char *a, unsigned char *b, int s) +{ + int i, j, var=0; + for (i=3; i; i--) { + for (j=0; j<8; j++) { + var += ABS(a[j]-a[j+s]); + } + a+=s; b+=s; + } + return 4*var; /* match comb scaling */ +} + + + + + + + + + +static void alloc_buffer(struct pullup_context *c, struct pullup_buffer *b) +{ + int i; + if (b->planes) return; + b->planes = calloc(c->nplanes, sizeof(unsigned char *)); + for (i = 0; i < c->nplanes; i++) { + b->planes[i] = malloc(c->h[i]*c->stride[i]); + /* Deal with idiotic 128=0 for chroma: */ + memset(b->planes[i], c->background[i], c->h[i]*c->stride[i]); + } +} + +struct pullup_buffer *pullup_lock_buffer(struct pullup_buffer *b, int parity) +{ + if (!b) return 0; + if ((parity+1) & 1) b->lock[0]++; + if ((parity+1) & 2) b->lock[1]++; + return b; +} + +void pullup_release_buffer(struct pullup_buffer *b, int parity) +{ + if (!b) return; + if ((parity+1) & 1) b->lock[0]--; + if ((parity+1) & 2) b->lock[1]--; +} + +struct pullup_buffer *pullup_get_buffer(struct pullup_context *c, int parity) +{ + int i; + + /* Try first to get the sister buffer for the previous field */ + if (parity < 2 && c->last && parity != c->last->parity + && !c->last->buffer->lock[parity]) { + alloc_buffer(c, c->last->buffer); + return pullup_lock_buffer(c->last->buffer, parity); + } + + /* Prefer a buffer with both fields open */ + for (i = 0; i < c->nbuffers; i++) { + if (c->buffers[i].lock[0]) continue; + if (c->buffers[i].lock[1]) continue; + alloc_buffer(c, &c->buffers[i]); + return pullup_lock_buffer(&c->buffers[i], parity); + } + + if (parity == 2) return 0; + + /* Search for any half-free buffer */ + for (i = 0; i < c->nbuffers; i++) { + if (((parity+1) & 1) && c->buffers[i].lock[0]) continue; + if (((parity+1) & 2) && c->buffers[i].lock[1]) continue; + alloc_buffer(c, &c->buffers[i]); + return pullup_lock_buffer(&c->buffers[i], parity); + } + + return 0; +} + + + + + + +static void compute_metric(struct pullup_context *c, + struct pullup_field *fa, int pa, + struct pullup_field *fb, int pb, + int (*func)(unsigned char *, unsigned char *, int), int *dest) +{ + unsigned char *a, *b; + int x, y; + int mp = c->metric_plane; + int xstep = c->bpp[mp]; + int ystep = c->stride[mp]<<3; + int s = c->stride[mp]<<1; /* field stride */ + int w = c->metric_w*xstep; + + if (!fa->buffer || !fb->buffer) return; + + /* Shortcut for duplicate fields (e.g. from RFF flag) */ + if (fa->buffer == fb->buffer && pa == pb) { + memset(dest, 0, c->metric_len * sizeof(int)); + return; + } + + a = fa->buffer->planes[mp] + pa * c->stride[mp] + c->metric_offset; + b = fb->buffer->planes[mp] + pb * c->stride[mp] + c->metric_offset; + + for (y = c->metric_h; y; y--) { + for (x = 0; x < w; x += xstep) { + *dest++ = func(a + x, b + x, s); + } + a += ystep; b += ystep; + } +} + + + + + +static void alloc_metrics(struct pullup_context *c, struct pullup_field *f) +{ + f->diffs = calloc(c->metric_len, sizeof(int)); + f->comb = calloc(c->metric_len, sizeof(int)); + f->var = calloc(c->metric_len, sizeof(int)); + /* add more metrics here as needed */ +} + +static struct pullup_field *make_field_queue(struct pullup_context *c, int len) +{ + struct pullup_field *head, *f; + f = head = calloc(1, sizeof(struct pullup_field)); + alloc_metrics(c, f); + for (; len > 0; len--) { + f->next = calloc(1, sizeof(struct pullup_field)); + f->next->prev = f; + f = f->next; + alloc_metrics(c, f); + } + f->next = head; + head->prev = f; + return head; +} + +static void check_field_queue(struct pullup_context *c) +{ + if (c->head->next == c->first) { + struct pullup_field *f = calloc(1, sizeof(struct pullup_field)); + alloc_metrics(c, f); + f->prev = c->head; + f->next = c->first; + c->head->next = f; + c->first->prev = f; + } +} + +void pullup_submit_field(struct pullup_context *c, struct pullup_buffer *b, + int parity, double pts) +{ + struct pullup_field *f; + + /* Grow the circular list if needed */ + check_field_queue(c); + + /* Cannot have two fields of same parity in a row; drop the new one */ + if (c->last && c->last->parity == parity) return; + + f = c->head; + f->parity = parity; + f->buffer = pullup_lock_buffer(b, parity); + f->flags = 0; + f->breaks = 0; + f->affinity = 0; + f->pts = pts; + + compute_metric(c, f, parity, f->prev->prev, parity, c->diff, f->diffs); + compute_metric(c, parity?f->prev:f, 0, parity?f:f->prev, 1, c->comb, f->comb); + compute_metric(c, f, parity, f, -1, c->var, f->var); + + /* Advance the circular list */ + if (!c->first) c->first = c->head; + c->last = c->head; + c->head = c->head->next; +} + + + + +#define F_HAVE_BREAKS 1 +#define F_HAVE_AFFINITY 2 + + +#define BREAK_LEFT 1 +#define BREAK_RIGHT 2 + + + + +static int queue_length(struct pullup_field *begin, struct pullup_field *end) +{ + int count = 1; + struct pullup_field *f; + + if (!begin || !end) return 0; + for (f = begin; f != end; f = f->next) count++; + return count; +} + +static int find_first_break(struct pullup_field *f, int max) +{ + int i; + for (i = 0; i < max; i++) { + if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT) + return i+1; + f = f->next; + } + return 0; +} + +static void compute_breaks(struct pullup_context *c, struct pullup_field *f0) +{ + int i; + struct pullup_field *f1 = f0->next; + struct pullup_field *f2 = f1->next; + struct pullup_field *f3 = f2->next; + int l, max_l=0, max_r=0; + //struct pullup_field *ff; + //for (i=0, ff=c->first; ff != f0; i++, ff=ff->next); + + if (f0->flags & F_HAVE_BREAKS) return; + //printf("\n%d: ", i); + f0->flags |= F_HAVE_BREAKS; + + /* Special case when fields are 100% identical */ + if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) { + f2->breaks |= BREAK_RIGHT; + return; + } + if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) { + f1->breaks |= BREAK_LEFT; + return; + } + + for (i = 0; i < c->metric_len; i++) { + l = f2->diffs[i] - f3->diffs[i]; + if (l > max_l) max_l = l; + if (-l > max_r) max_r = -l; + } + /* Don't get tripped up when differences are mostly quant error */ + //printf("%d %d\n", max_l, max_r); + if (max_l + max_r < 128) return; + if (max_l > 4*max_r) f1->breaks |= BREAK_LEFT; + if (max_r > 4*max_l) f2->breaks |= BREAK_RIGHT; +} + +static void compute_affinity(struct pullup_context *c, struct pullup_field *f) +{ + int i; + int max_l=0, max_r=0, l; + if (f->flags & F_HAVE_AFFINITY) return; + f->flags |= F_HAVE_AFFINITY; + if (f->buffer == f->next->next->buffer) { + f->affinity = 1; + f->next->affinity = 0; + f->next->next->affinity = -1; + f->next->flags |= F_HAVE_AFFINITY; + f->next->next->flags |= F_HAVE_AFFINITY; + return; + } + if (1) { + for (i = 0; i < c->metric_len; i++) { + int lv = f->prev->var[i]; + int rv = f->next->var[i]; + int v = f->var[i]; + int lc = f->comb[i] - (v+lv) + ABS(v-lv); + int rc = f->next->comb[i] - (v+rv) + ABS(v-rv); + lc = lc>0 ? lc : 0; + rc = rc>0 ? rc : 0; + l = lc - rc; + if (l > max_l) max_l = l; + if (-l > max_r) max_r = -l; + } + if (max_l + max_r < 64) return; + if (max_r > 6*max_l) f->affinity = -1; + else if (max_l > 6*max_r) f->affinity = 1; + } else { + for (i = 0; i < c->metric_len; i++) { + l = f->comb[i] - f->next->comb[i]; + if (l > max_l) max_l = l; + if (-l > max_r) max_r = -l; + } + if (max_l + max_r < 64) return; + if (max_r > 2*max_l) f->affinity = -1; + else if (max_l > 2*max_r) f->affinity = 1; + } +} + +static void foo(struct pullup_context *c) +{ + struct pullup_field *f = c->first; + int i, n = queue_length(f, c->last); + for (i = 0; i < n-1; i++) { + if (i < n-3) compute_breaks(c, f); + compute_affinity(c, f); + f = f->next; + } +} + +static int decide_frame_length(struct pullup_context *c) +{ + struct pullup_field *f0 = c->first; + struct pullup_field *f1 = f0->next; + struct pullup_field *f2 = f1->next; + int l; + + if (queue_length(c->first, c->last) < 4) return 0; + foo(c); + + if (f0->affinity == -1) return 1; + + l = find_first_break(f0, 3); + if (l == 1 && c->strict_breaks < 0) l = 0; + + switch (l) { + case 1: + if (c->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1) + return 2; + else return 1; + case 2: + /* FIXME: strictly speaking, f0->prev is no longer valid... :) */ + if (c->strict_pairs + && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT) + && (f0->affinity != 1 || f1->affinity != -1) ) + return 1; + if (f1->affinity == 1) return 1; + else return 2; + case 3: + if (f2->affinity == 1) return 2; + else return 3; + default: + /* 9 possibilities covered before switch */ + if (f1->affinity == 1) return 1; /* covers 6 */ + else if (f1->affinity == -1) return 2; /* covers 6 */ + else if (f2->affinity == -1) { /* covers 2 */ + if (f0->affinity == 1) return 3; + else return 1; + } + else return 2; /* the remaining 6 */ + } +} + + +static void print_aff_and_breaks(struct pullup_context *c, struct pullup_field *f) +{ + int i; + struct pullup_field *f0 = f; + const char aff_l[] = "+..", aff_r[] = "..+"; + printf("\naffinity: "); + for (i = 0; i < 4; i++) { + printf("%c%d%c", aff_l[1+f->affinity], i, aff_r[1+f->affinity]); + f = f->next; + } + f = f0; + printf("\nbreaks: "); + for (i=0; i<4; i++) { + printf("%c%d%c", f->breaks & BREAK_LEFT ? '|' : '.', i, f->breaks & BREAK_RIGHT ? '|' : '.'); + f = f->next; + } + printf("\n"); +} + + + + + +struct pullup_frame *pullup_get_frame(struct pullup_context *c) +{ + int i; + struct pullup_frame *fr = c->frame; + int n = decide_frame_length(c); + int aff = c->first->next->affinity; + + if (!n) return 0; + if (fr->lock) return 0; + + if (c->verbose) { + print_aff_and_breaks(c, c->first); + printf("duration: %d \n", n); + } + + fr->lock++; + fr->length = n; + fr->parity = c->first->parity; + fr->buffer = 0; + fr->pts = 0; + for (i = 0; i < n; i++) { + /* We cheat and steal the buffer without release+relock */ + fr->ifields[i] = c->first->buffer; + c->first->buffer = 0; + if (c->first->pts == MP_NOPTS_VALUE || fr->pts == MP_NOPTS_VALUE) + fr->pts = MP_NOPTS_VALUE; + else + fr->pts += c->first->pts; + c->first = c->first->next; + } + if (fr->pts != MP_NOPTS_VALUE) + fr->pts /= n; + + if (n == 1) { + fr->ofields[fr->parity] = fr->ifields[0]; + fr->ofields[fr->parity^1] = 0; + } else if (n == 2) { + fr->ofields[fr->parity] = fr->ifields[0]; + fr->ofields[fr->parity^1] = fr->ifields[1]; + } else if (n == 3) { + if (aff == 0) + aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1; + /* else if (c->verbose) printf("forced aff: %d \n", aff); */ + fr->ofields[fr->parity] = fr->ifields[1+aff]; + fr->ofields[fr->parity^1] = fr->ifields[1]; + } + pullup_lock_buffer(fr->ofields[0], 0); + pullup_lock_buffer(fr->ofields[1], 1); + + if (fr->ofields[0] == fr->ofields[1]) { + fr->buffer = fr->ofields[0]; + pullup_lock_buffer(fr->buffer, 2); + return fr; + } + return fr; +} + +static void copy_field(struct pullup_context *c, struct pullup_buffer *dest, + struct pullup_buffer *src, int parity) +{ + int i, j; + unsigned char *d, *s; + for (i = 0; i < c->nplanes; i++) { + s = src->planes[i] + parity*c->stride[i]; + d = dest->planes[i] + parity*c->stride[i]; + for (j = c->h[i]>>1; j; j--) { + memcpy(d, s, c->stride[i]); + s += c->stride[i]<<1; + d += c->stride[i]<<1; + } + } +} + +void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr) +{ + int i; + if (fr->buffer) return; + if (fr->length < 2) return; /* FIXME: deal with this */ + for (i = 0; i < 2; i++) + { + if (fr->ofields[i]->lock[i^1]) continue; + fr->buffer = fr->ofields[i]; + pullup_lock_buffer(fr->buffer, 2); + copy_field(c, fr->buffer, fr->ofields[i^1], i^1); + return; + } + fr->buffer = pullup_get_buffer(c, 2); + copy_field(c, fr->buffer, fr->ofields[0], 0); + copy_field(c, fr->buffer, fr->ofields[1], 1); +} + +void pullup_release_frame(struct pullup_frame *fr) +{ + int i; + for (i = 0; i < fr->length; i++) + pullup_release_buffer(fr->ifields[i], fr->parity ^ (i&1)); + pullup_release_buffer(fr->ofields[0], 0); + pullup_release_buffer(fr->ofields[1], 1); + if (fr->buffer) pullup_release_buffer(fr->buffer, 2); + fr->lock--; +} + + + + + + +struct pullup_context *pullup_alloc_context(void) +{ + struct pullup_context *c; + + c = calloc(1, sizeof(struct pullup_context)); + + return c; +} + +void pullup_preinit_context(struct pullup_context *c) +{ + c->bpp = calloc(c->nplanes, sizeof(int)); + c->w = calloc(c->nplanes, sizeof(int)); + c->h = calloc(c->nplanes, sizeof(int)); + c->stride = calloc(c->nplanes, sizeof(int)); + c->background = calloc(c->nplanes, sizeof(int)); +} + +void pullup_init_context(struct pullup_context *c) +{ + int mp = c->metric_plane; + if (c->nbuffers < 10) c->nbuffers = 10; + c->buffers = calloc(c->nbuffers, sizeof (struct pullup_buffer)); + + c->metric_w = (c->w[mp] - ((c->junk_left + c->junk_right) << 3)) >> 3; + c->metric_h = (c->h[mp] - ((c->junk_top + c->junk_bottom) << 1)) >> 3; + c->metric_offset = c->junk_left*c->bpp[mp] + (c->junk_top<<1)*c->stride[mp]; + c->metric_len = c->metric_w * c->metric_h; + + c->head = make_field_queue(c, 8); + + c->frame = calloc(1, sizeof (struct pullup_frame)); + c->frame->ifields = calloc(3, sizeof (struct pullup_buffer *)); + + switch(c->format) { + case PULLUP_FMT_Y: + c->diff = diff_y; + c->comb = licomb_y; + c->var = var_y; +#if ARCH_X86 +#if HAVE_MMX + if (c->cpu & PULLUP_CPU_MMX) { + c->diff = diff_y_mmx; + c->comb = licomb_y_mmx; + c->var = var_y_mmx; + } +#endif +#endif + /* c->comb = qpcomb_y; */ + break; +#if 0 + case PULLUP_FMT_YUY2: + c->diff = diff_yuy2; + break; + case PULLUP_FMT_RGB32: + c->diff = diff_rgb32; + break; +#endif + } +} + +void pullup_free_context(struct pullup_context *c) +{ + struct pullup_field *f; + free(c->buffers); + f = c->head; + do { + if (!f) break; + free(f->diffs); + free(f->comb); + f = f->next; + free(f->prev); + } while (f != c->head); + free(c->frame); + free(c); +} diff --git a/video/filter/pullup.h b/video/filter/pullup.h new file mode 100644 index 0000000000..0948737919 --- /dev/null +++ b/video/filter/pullup.h @@ -0,0 +1,102 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_PULLUP_H +#define MPLAYER_PULLUP_H + +#define PULLUP_CPU_MMX 1 +#define PULLUP_CPU_MMX2 2 +#define PULLUP_CPU_SSE 16 +#define PULLUP_CPU_SSE2 32 + +#define PULLUP_FMT_Y 1 +#define PULLUP_FMT_YUY2 2 +#define PULLUP_FMT_UYVY 3 +#define PULLUP_FMT_RGB32 4 + +struct pullup_buffer +{ + int lock[2]; + unsigned char **planes; +}; + +struct pullup_field +{ + int parity; + double pts; + struct pullup_buffer *buffer; + unsigned int flags; + int breaks; + int affinity; + int *diffs; + int *comb; + int *var; + struct pullup_field *prev, *next; +}; + +struct pullup_frame +{ + int lock; + int length; + int parity; + double pts; + struct pullup_buffer **ifields, *ofields[2]; + struct pullup_buffer *buffer; +}; + +struct pullup_context +{ + /* Public interface */ + int format; + int nplanes; + int *bpp, *w, *h, *stride, *background; + unsigned int cpu; + int junk_left, junk_right, junk_top, junk_bottom; + int verbose; + int metric_plane; + int strict_breaks; + int strict_pairs; + /* Internal data */ + struct pullup_field *first, *last, *head; + struct pullup_buffer *buffers; + int nbuffers; + int (*diff)(unsigned char *, unsigned char *, int); + int (*comb)(unsigned char *, unsigned char *, int); + int (*var)(unsigned char *, unsigned char *, int); + int metric_w, metric_h, metric_len, metric_offset; + struct pullup_frame *frame; +}; + + +struct pullup_buffer *pullup_lock_buffer(struct pullup_buffer *b, int parity); +void pullup_release_buffer(struct pullup_buffer *b, int parity); +struct pullup_buffer *pullup_get_buffer(struct pullup_context *c, int parity); + +void pullup_submit_field(struct pullup_context *c, struct pullup_buffer *b, + int parity, double pts); + +struct pullup_frame *pullup_get_frame(struct pullup_context *c); +void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr); +void pullup_release_frame(struct pullup_frame *fr); + +struct pullup_context *pullup_alloc_context(void); +void pullup_preinit_context(struct pullup_context *c); +void pullup_init_context(struct pullup_context *c); +void pullup_free_context(struct pullup_context *c); + +#endif /* MPLAYER_PULLUP_H */ diff --git a/video/filter/vf.c b/video/filter/vf.c new file mode 100644 index 0000000000..10b9fa546f --- /dev/null +++ b/video/filter/vf.c @@ -0,0 +1,830 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#include "mp_msg.h" +#include "m_option.h" +#include "m_struct.h" + + +#include "img_format.h" +#include "mp_image.h" +#include "vf.h" + +#include "libvo/fastmemcpy.h" + +extern const vf_info_t vf_info_vo; +extern const vf_info_t vf_info_crop; +extern const vf_info_t vf_info_expand; +extern const vf_info_t vf_info_pp; +extern const vf_info_t vf_info_scale; +extern const vf_info_t vf_info_format; +extern const vf_info_t vf_info_noformat; +extern const vf_info_t vf_info_flip; +extern const vf_info_t vf_info_rotate; +extern const vf_info_t vf_info_mirror; +extern const vf_info_t vf_info_noise; +extern const vf_info_t vf_info_eq2; +extern const vf_info_t vf_info_gradfun; +extern const vf_info_t vf_info_unsharp; +extern const vf_info_t vf_info_swapuv; +extern const vf_info_t vf_info_down3dright; +extern const vf_info_t vf_info_hqdn3d; +extern const vf_info_t vf_info_ilpack; +extern const vf_info_t vf_info_dsize; +extern const vf_info_t vf_info_softpulldown; +extern const vf_info_t vf_info_pullup; +extern const vf_info_t vf_info_delogo; +extern const vf_info_t vf_info_phase; +extern const vf_info_t vf_info_divtc; +extern const vf_info_t vf_info_softskip; +extern const vf_info_t vf_info_screenshot; +extern const vf_info_t vf_info_screenshot_force; +extern const vf_info_t vf_info_sub; +extern const vf_info_t vf_info_yadif; +extern const vf_info_t vf_info_stereo3d; +extern const vf_info_t vf_info_dlopen; + +// list of available filters: +static const vf_info_t *const filter_list[] = { + &vf_info_crop, + &vf_info_expand, + &vf_info_scale, + &vf_info_vo, + &vf_info_format, + &vf_info_noformat, + &vf_info_flip, + &vf_info_rotate, + &vf_info_mirror, + +#ifdef CONFIG_LIBPOSTPROC + &vf_info_pp, +#endif + + &vf_info_screenshot, + &vf_info_screenshot_force, + + &vf_info_noise, + &vf_info_eq2, + &vf_info_gradfun, + &vf_info_unsharp, + &vf_info_swapuv, + &vf_info_down3dright, + &vf_info_hqdn3d, + &vf_info_ilpack, + &vf_info_dsize, + &vf_info_softpulldown, + &vf_info_pullup, + &vf_info_delogo, + &vf_info_phase, + &vf_info_divtc, + &vf_info_sub, + &vf_info_yadif, + &vf_info_stereo3d, + &vf_info_dlopen, + NULL +}; + +// For the vf option +const m_obj_list_t vf_obj_list = { + (void **)filter_list, + M_ST_OFF(vf_info_t, name), + M_ST_OFF(vf_info_t, info), + M_ST_OFF(vf_info_t, opts) +}; + +//============================================================================ +// mpi stuff: + +void vf_mpi_clear(mp_image_t *mpi, int x0, int y0, int w, int h) +{ + int y; + if (mpi->flags & MP_IMGFLAG_PLANAR) { + y0 &= ~1; + h += h & 1; + if (x0 == 0 && w == mpi->width) { + // full width clear: + memset(mpi->planes[0] + mpi->stride[0] * y0, 0, mpi->stride[0] * h); + memset(mpi->planes[1] + mpi->stride[1] *(y0 >> mpi->chroma_y_shift), + 128, mpi->stride[1] * (h >> mpi->chroma_y_shift)); + memset(mpi->planes[2] + mpi->stride[2] *(y0 >> mpi->chroma_y_shift), + 128, mpi->stride[2] * (h >> mpi->chroma_y_shift)); + } else + for (y = y0; y < y0 + h; y += 2) { + memset(mpi->planes[0] + x0 + mpi->stride[0] * y, 0, w); + memset(mpi->planes[0] + x0 + mpi->stride[0] * (y + 1), 0, w); + memset(mpi->planes[1] + (x0 >> mpi->chroma_x_shift) + + mpi->stride[1] * (y >> mpi->chroma_y_shift), + 128, (w >> mpi->chroma_x_shift)); + memset(mpi->planes[2] + (x0 >> mpi->chroma_x_shift) + + mpi->stride[2] * (y >> mpi->chroma_y_shift), + 128, (w >> mpi->chroma_x_shift)); + } + return; + } + // packed: + for (y = y0; y < y0 + h; y++) { + unsigned char *dst = mpi->planes[0] + mpi->stride[0] * y + + (mpi->bpp >> 3) * x0; + if (mpi->flags & MP_IMGFLAG_YUV) { + unsigned int *p = (unsigned int *) dst; + int size = (mpi->bpp >> 3) * w / 4; + int i; +#ifdef BIG_ENDIAN +#define CLEAR_PACKEDYUV_PATTERN 0x00800080 +#define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000 +#else +#define CLEAR_PACKEDYUV_PATTERN 0x80008000 +#define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080 +#endif + if (mpi->flags & MP_IMGFLAG_SWAPPED) { + for (i = 0; i < size - 3; i += 4) + p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN_SWAPPED; + for (; i < size; i++) + p[i] = CLEAR_PACKEDYUV_PATTERN_SWAPPED; + } else { + for (i = 0; i < size - 3; i += 4) + p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN; + for (; i < size; i++) + p[i] = CLEAR_PACKEDYUV_PATTERN; + } + } else + memset(dst, 0, (mpi->bpp >> 3) * w); + } +} + +mp_image_t *vf_get_image(vf_instance_t *vf, unsigned int outfmt, + int mp_imgtype, int mp_imgflag, int w, int h) +{ + mp_image_t *mpi = NULL; + int w2; + int number = mp_imgtype >> 16; + + assert(w == -1 || w >= vf->w); + assert(h == -1 || h >= vf->h); + assert(vf->w > 0); + assert(vf->h > 0); + + if (w == -1) + w = vf->w; + if (h == -1) + h = vf->h; + + w2 = (mp_imgflag & MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE) ? FFALIGN(w, 32) : w; + + if (vf->put_image == vf_next_put_image) { + // passthru mode, if the filter uses the fallback/default put_image() + mpi = vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h); + mpi->usage_count++; + return mpi; + } + + // Note: we should call libvo first to check if it supports direct rendering + // and if not, then fallback to software buffers: + switch (mp_imgtype & 0xff) { + case MP_IMGTYPE_EXPORT: + if (!vf->imgctx.export_images[0]) + vf->imgctx.export_images[0] = new_mp_image(w2, h); + mpi = vf->imgctx.export_images[0]; + break; + case MP_IMGTYPE_STATIC: + if (!vf->imgctx.static_images[0]) + vf->imgctx.static_images[0] = new_mp_image(w2, h); + mpi = vf->imgctx.static_images[0]; + break; + case MP_IMGTYPE_TEMP: + if (!vf->imgctx.temp_images[0]) + vf->imgctx.temp_images[0] = new_mp_image(w2, h); + mpi = vf->imgctx.temp_images[0]; + break; + case MP_IMGTYPE_IPB: + if (!(mp_imgflag & MP_IMGFLAG_READABLE)) { // B frame: + if (!vf->imgctx.temp_images[0]) + vf->imgctx.temp_images[0] = new_mp_image(w2, h); + mpi = vf->imgctx.temp_images[0]; + break; + } + case MP_IMGTYPE_IP: + if (!vf->imgctx.static_images[vf->imgctx.static_idx]) + vf->imgctx.static_images[vf->imgctx.static_idx] = new_mp_image(w2, h); + mpi = vf->imgctx.static_images[vf->imgctx.static_idx]; + vf->imgctx.static_idx ^= 1; + break; + case MP_IMGTYPE_NUMBERED: + if (number == -1) { + int i; + for (i = 0; i < NUM_NUMBERED_MPI; i++) + if (!vf->imgctx.numbered_images[i] || + !vf->imgctx.numbered_images[i]->usage_count) + break; + number = i; + } + if (number < 0 || number >= NUM_NUMBERED_MPI) + return NULL; + if (!vf->imgctx.numbered_images[number]) + vf->imgctx.numbered_images[number] = new_mp_image(w2, h); + mpi = vf->imgctx.numbered_images[number]; + mpi->number = number; + break; + } + if (mpi) { + int missing_palette = !(mpi->flags & MP_IMGFLAG_RGB_PALETTE) && + (mp_imgflag & MP_IMGFLAG_RGB_PALETTE); + mpi->type = mp_imgtype; + mpi->w = vf->w; + mpi->h = vf->h; + // keep buffer allocation status & color flags only: + mpi->flags &= MP_IMGFLAG_ALLOCATED | MP_IMGFLAG_TYPE_DISPLAYED | + MP_IMGFLAGMASK_COLORS; + // accept restrictions, draw_slice and palette flags only: + mpi->flags |= mp_imgflag & (MP_IMGFLAGMASK_RESTRICTIONS | + MP_IMGFLAG_DRAW_CALLBACK | MP_IMGFLAG_RGB_PALETTE); + if (!vf->draw_slice) + mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK; + if (mpi->width != w2 || mpi->height != h || missing_palette) { + if (mpi->flags & MP_IMGFLAG_ALLOCATED) { + if (mpi->width < w2 || mpi->height < h || missing_palette) { + // need to re-allocate buffer memory: + av_free(mpi->planes[0]); + if (mpi->flags & MP_IMGFLAG_RGB_PALETTE) + av_free(mpi->planes[1]); + for (int n = 0; n < MP_MAX_PLANES; n++) + mpi->planes[n] = NULL; + mpi->flags &= ~MP_IMGFLAG_ALLOCATED; + mp_msg(MSGT_VFILTER, MSGL_V, + "vf.c: have to REALLOCATE buffer memory :(\n"); + } + } + mpi->width = w2; + mpi->chroma_width = (w2 + (1 << mpi->chroma_x_shift) - 1) >> + mpi->chroma_x_shift; + mpi->height = h; + mpi->chroma_height = (h + (1 << mpi->chroma_y_shift) - 1) >> + mpi->chroma_y_shift; + } + if (!mpi->bpp) + mp_image_setfmt(mpi, outfmt); + if (!(mpi->flags & MP_IMGFLAG_ALLOCATED) && + mpi->type > MP_IMGTYPE_EXPORT) { + // check libvo first! + if (vf->get_image) + vf->get_image(vf, mpi); + + if (!(mpi->flags & MP_IMGFLAG_DIRECT)) { + // non-direct and not yet allocated image. allocate it! + if (!mpi->bpp) { // no way we can allocate this + mp_msg(MSGT_DECVIDEO, MSGL_FATAL, + "vf_get_image: Tried to allocate a format that " + "can not be allocated!\n"); + return NULL; + } + + // check if codec prefer aligned stride: + if (mp_imgflag & MP_IMGFLAG_PREFER_ALIGNED_STRIDE) { + int align = (mpi->flags & MP_IMGFLAG_PLANAR && + mpi->flags & MP_IMGFLAG_YUV) ? + (16 << mpi->chroma_x_shift) - 1 : 32; // OK? + w2 = FFALIGN(w, align); + if (mpi->width != w2) { + // we have to change width... check if we CAN co it: + int flags = vf->query_format(vf, outfmt); + // should not fail + if (!(flags & (VFCAP_CSP_SUPPORTED | + VFCAP_CSP_SUPPORTED_BY_HW))) + mp_msg(MSGT_DECVIDEO, MSGL_WARN, + "??? vf_get_image{vf->query_format(outfmt)} " + "failed!\n"); + if (flags & VFCAP_ACCEPT_STRIDE) { + mpi->width = w2; + mpi->chroma_width = + (w2 + (1 << mpi->chroma_x_shift) - 1) >> + mpi->chroma_x_shift; + } + } + } + + mp_image_alloc_planes(mpi); + vf_mpi_clear(mpi, 0, 0, mpi->width, mpi->height); + } + } + if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) + if (vf->start_slice) + vf->start_slice(vf, mpi); + if (!(mpi->flags & MP_IMGFLAG_TYPE_DISPLAYED)) { + mp_msg(MSGT_DECVIDEO, MSGL_V, + "*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n", + vf->info->name, + (mpi->type == MP_IMGTYPE_EXPORT) ? "Exporting" : + ((mpi->flags & MP_IMGFLAG_DIRECT) ? + "Direct Rendering" : "Allocating"), + (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) ? " (slices)" : "", + mpi->width, mpi->height, mpi->bpp, + (mpi->flags & MP_IMGFLAG_YUV) ? "YUV" : + ((mpi->flags & MP_IMGFLAG_SWAPPED) ? "BGR" : "RGB"), + (mpi->flags & MP_IMGFLAG_PLANAR) ? "planar" : "packed", + mpi->bpp * mpi->width * mpi->height / 8); + mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "(imgfmt: %x, planes: %p,%p,%p " + "strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n", + mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2], + mpi->stride[0], mpi->stride[1], mpi->stride[2], + mpi->chroma_width, mpi->chroma_height, + mpi->chroma_x_shift, mpi->chroma_y_shift); + mpi->flags |= MP_IMGFLAG_TYPE_DISPLAYED; + } + mpi->qscale = NULL; + mpi->usage_count++; + } + return mpi; +} + +//============================================================================ + +static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt) +{ + return vf_next_query_format(vf, fmt); +} + +struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts, + const vf_info_t *const *filter_list, + vf_instance_t *next, const char *name, + char **args, int *retcode) +{ + vf_instance_t *vf; + int i; + for (i = 0;; i++) { + if (!filter_list[i]) { + mp_tmsg(MSGT_VFILTER, MSGL_ERR, + "Couldn't find video filter '%s'.\n", name); + return NULL; // no such filter! + } + if (!strcmp(filter_list[i]->name, name)) + break; + } + vf = calloc(1, sizeof *vf); + vf->opts = opts; + vf->info = filter_list[i]; + vf->next = next; + vf->config = vf_next_config; + vf->control = vf_next_control; + vf->query_format = vf_default_query_format; + vf->put_image = vf_next_put_image; + vf->default_caps = VFCAP_ACCEPT_STRIDE; + vf->default_reqs = 0; + if (vf->info->opts) { // vf_vo get some special argument + const m_struct_t *st = vf->info->opts; + void *vf_priv = m_struct_alloc(st); + int n; + for (n = 0; args && args[2 * n]; n++) + m_struct_set(st, vf_priv, args[2 * n], bstr0(args[2 * n + 1])); + vf->priv = vf_priv; + args = NULL; + } else // Otherwise we should have the '_oldargs_' + if (args && !strcmp(args[0], "_oldargs_")) + args = (char **)args[1]; + else + args = NULL; + *retcode = vf->info->vf_open(vf, (char *)args); + if (*retcode > 0) + return vf; + free(vf); + return NULL; +} + +struct vf_instance *vf_open_plugin(struct MPOpts *opts, + const vf_info_t *const *filter_list, + vf_instance_t *next, const char *name, + char **args) +{ + struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next, + name, args, &(int){0}); + if (!vf) + mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n", + name); + return vf; +} + +vf_instance_t *vf_open_filter(struct MPOpts *opts, vf_instance_t *next, + const char *name, char **args) +{ + if (args && strcmp(args[0], "_oldargs_")) { + int i, l = 0; + for (i = 0; args && args[2 * i]; i++) + l += 1 + strlen(args[2 * i]) + 1 + strlen(args[2 * i + 1]); + l += strlen(name); + { + char str[l + 1]; + char *p = str; + p += sprintf(str, "%s", name); + for (i = 0; args && args[2 * i]; i++) + p += sprintf(p, " %s=%s", args[2 * i], args[2 * i + 1]); + mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n", + mp_gtext("Opening video filter: "), str); + } + } else if (strcmp(name, "vo")) { + if (args && strcmp(args[0], "_oldargs_") == 0) + mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n", + mp_gtext("Opening video filter: "), name, args[1]); + else + mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n", + mp_gtext("Opening video filter: "), name); + } + return vf_open_plugin(opts, filter_list, next, name, args); +} + +/** + * \brief adds a filter before the last one (which should be the vo filter). + * \param vf start of the filter chain. + * \param name name of the filter to add. + * \param args argument list for the filter. + * \return pointer to the filter instance that was created. + */ +vf_instance_t *vf_add_before_vo(vf_instance_t **vf, char *name, char **args) +{ + struct MPOpts *opts = (*vf)->opts; + vf_instance_t *vo, *prev = NULL, *new; + // Find the last filter (should be vf_vo) + for (vo = *vf; vo->next; vo = vo->next) + prev = vo; + new = vf_open_filter(opts, vo, name, args); + if (prev) + prev->next = new; + else + *vf = new; + return new; +} + +//============================================================================ + +unsigned int vf_match_csp(vf_instance_t **vfp, const unsigned int *list, + unsigned int preferred) +{ + vf_instance_t *vf = *vfp; + struct MPOpts *opts = vf->opts; + const unsigned int *p; + unsigned int best = 0; + int ret; + if ((p = list)) + while (*p) { + ret = vf->query_format(vf, *p); + mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n", + vf->info->name, vo_format_name(*p), ret); + if (ret & VFCAP_CSP_SUPPORTED_BY_HW) { + best = *p; + break; + } + if (ret & VFCAP_CSP_SUPPORTED && !best) + best = *p; + ++p; + } + if (best) + return best; // bingo, they have common csp! + // ok, then try with scale: + if (vf->info == &vf_info_scale) + return 0; // avoid infinite recursion! + vf = vf_open_filter(opts, vf, "scale", NULL); + if (!vf) + return 0; // failed to init "scale" + // try the preferred csp first: + if (preferred && vf->query_format(vf, preferred)) + best = preferred; + else + // try the list again, now with "scaler" : + if ((p = list)) + while (*p) { + ret = vf->query_format(vf, *p); + mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n", + vf->info->name, vo_format_name(*p), ret); + if (ret & VFCAP_CSP_SUPPORTED_BY_HW) { + best = *p; + break; + } + if (ret & VFCAP_CSP_SUPPORTED && !best) + best = *p; + ++p; + } + if (best) + *vfp = vf; // else uninit vf !FIXME! + return best; +} + +void vf_clone_mpi_attributes(mp_image_t *dst, mp_image_t *src) +{ + dst->pict_type = src->pict_type; + dst->fields = src->fields; + dst->qscale_type = src->qscale_type; + if (dst->width == src->width && dst->height == src->height) { + dst->qstride = src->qstride; + dst->qscale = src->qscale; + dst->display_w = src->display_w; + dst->display_h = src->display_h; + } + if ((dst->flags & MP_IMGFLAG_YUV) == (src->flags & MP_IMGFLAG_YUV)) { + dst->colorspace = src->colorspace; + dst->levels = src->levels; + } +} + +void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *)) +{ + vf->continue_buffered_image = func; +} + +// Output the next buffered image (if any) from the filter chain. +// The queue could be kept as a simple stack/list instead avoiding the +// looping here, but there's currently no good context variable where +// that could be stored so this was easier to implement. + +int vf_output_queued_frame(vf_instance_t *vf) +{ + while (1) { + int ret; + vf_instance_t *current; + vf_instance_t *last = NULL; + int (*tmp)(vf_instance_t *); + for (current = vf; current; current = current->next) + if (current->continue_buffered_image) + last = current; + if (!last) + return 0; + tmp = last->continue_buffered_image; + last->continue_buffered_image = NULL; + ret = tmp(last); + if (ret) + return ret; + } +} + + +/** + * \brief Video config() function wrapper + * + * Blocks config() calls with different size or format for filters + * with VFCAP_CONSTANT + * + * First call is redirected to vf->config. + * + * In following calls, it verifies that the configuration parameters + * are unchanged, and returns either success or error. + * + */ +int vf_config_wrapper(struct vf_instance *vf, + int width, int height, int d_width, int d_height, + unsigned int flags, unsigned int outfmt) +{ + vf->fmt.have_configured = 1; + vf->fmt.orig_height = height; + vf->fmt.orig_width = width; + vf->fmt.orig_fmt = outfmt; + int r = vf->config(vf, width, height, d_width, d_height, flags, outfmt); + if (!r) + vf->fmt.have_configured = 0; + return r; +} + +int vf_next_config(struct vf_instance *vf, + int width, int height, int d_width, int d_height, + unsigned int voflags, unsigned int outfmt) +{ + struct MPOpts *opts = vf->opts; + int miss; + int flags = vf->next->query_format(vf->next, outfmt); + if (!flags) { + // hmm. colorspace mismatch!!! + // let's insert the 'scale' filter, it does the job for us: + vf_instance_t *vf2; + if (vf->next->info == &vf_info_scale) + return 0; // scale->scale + vf2 = vf_open_filter(opts, vf->next, "scale", NULL); + if (!vf2) + return 0; // shouldn't happen! + vf->next = vf2; + flags = vf->next->query_format(vf->next, outfmt); + if (!flags) { + mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Cannot find matching colorspace, " + "even by inserting 'scale' :(\n"); + return 0; // FAIL + } + } + mp_msg(MSGT_VFILTER, MSGL_V, "REQ: flags=0x%X req=0x%X \n", + flags, vf->default_reqs); + miss = vf->default_reqs - (flags & vf->default_reqs); + if (miss & VFCAP_ACCEPT_STRIDE) { + // vf requires stride support but vf->next doesn't support it! + // let's insert the 'expand' filter, it does the job for us: + vf_instance_t *vf2 = vf_open_filter(opts, vf->next, "expand", NULL); + if (!vf2) + return 0; // shouldn't happen! + vf->next = vf2; + } + vf->next->w = width; + vf->next->h = height; + return vf_config_wrapper(vf->next, width, height, d_width, d_height, + voflags, outfmt); +} + +int vf_next_control(struct vf_instance *vf, int request, void *data) +{ + return vf->next->control(vf->next, request, data); +} + +int vf_next_query_format(struct vf_instance *vf, unsigned int fmt) +{ + int flags = vf->next->query_format(vf->next, fmt); + if (flags) + flags |= vf->default_caps; + return flags; +} + +int vf_next_put_image(struct vf_instance *vf, mp_image_t *mpi, double pts) +{ + return vf->next->put_image(vf->next, mpi, pts); +} + +void vf_next_draw_slice(struct vf_instance *vf, unsigned char **src, + int *stride, int w, int h, int x, int y) +{ + if (vf->next->draw_slice) { + vf->next->draw_slice(vf->next, src, stride, w, h, x, y); + return; + } + if (!vf->dmpi) { + mp_msg(MSGT_VFILTER, MSGL_ERR, + "draw_slice: dmpi not stored by vf_%s\n", vf->info->name); + return; + } + if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) { + memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] + + vf->dmpi->bpp / 8 * x, + src[0], vf->dmpi->bpp / 8 * w, h, vf->dmpi->stride[0], + stride[0]); + return; + } + memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] + x, src[0], + w, h, vf->dmpi->stride[0], stride[0]); + memcpy_pic(vf->dmpi->planes[1] + + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[1] + + (x >> vf->dmpi->chroma_x_shift), + src[1], w >> vf->dmpi->chroma_x_shift, + h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]); + memcpy_pic(vf->dmpi->planes[2] + + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[2] + + (x >> vf->dmpi->chroma_x_shift), + src[2], w >> vf->dmpi->chroma_x_shift, + h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]); +} + +//============================================================================ + +vf_instance_t *append_filters(vf_instance_t *last, + struct m_obj_settings *vf_settings) +{ + struct MPOpts *opts = last->opts; + vf_instance_t *vf; + int i; + + if (vf_settings) { + // We want to add them in the 'right order' + for (i = 0; vf_settings[i].name; i++) + /* NOP */; + for (i--; i >= 0; i--) { + //printf("Open filter %s\n",vf_settings[i].name); + vf = vf_open_filter(opts, last, vf_settings[i].name, + vf_settings[i].attribs); + if (vf) + last = vf; + } + } + return last; +} + +//============================================================================ + +void vf_uninit_filter(vf_instance_t *vf) +{ + if (vf->uninit) + vf->uninit(vf); + free_mp_image(vf->imgctx.static_images[0]); + free_mp_image(vf->imgctx.static_images[1]); + free_mp_image(vf->imgctx.temp_images[0]); + free_mp_image(vf->imgctx.export_images[0]); + for (int i = 0; i < NUM_NUMBERED_MPI; i++) + free_mp_image(vf->imgctx.numbered_images[i]); + free(vf); +} + +void vf_uninit_filter_chain(vf_instance_t *vf) +{ + while (vf) { + vf_instance_t *next = vf->next; + vf_uninit_filter(vf); + vf = next; + } +} + +void vf_detc_init_pts_buf(struct vf_detc_pts_buf *p) +{ + p->inpts_prev = MP_NOPTS_VALUE; + p->outpts_prev = MP_NOPTS_VALUE; + p->lastdelta = 0; +} + +static double vf_detc_adjust_pts_internal(struct vf_detc_pts_buf *p, + double pts, bool reset_pattern, + bool skip_frame, double delta, + double boundfactor_minus, + double increasefactor, + double boundfactor_plus) +{ + double newpts; + + if (pts == MP_NOPTS_VALUE) + return pts; + + if (delta <= 0) { + if (p->inpts_prev == MP_NOPTS_VALUE) + delta = 0; + else if (pts == p->inpts_prev) + delta = p->lastdelta; + else + delta = pts - p->inpts_prev; + } + p->inpts_prev = pts; + p->lastdelta = delta; + + if (skip_frame) + return MP_NOPTS_VALUE; + + /* detect bogus deltas and then passthru pts (possibly caused by seeking, + * or bad input) */ + if (p->outpts_prev == MP_NOPTS_VALUE || reset_pattern || delta <= 0.0 || + delta >= 0.5) + newpts = pts; + else { + // turn 5 frames into 4 + newpts = p->outpts_prev + delta * increasefactor; + + // bound to input pts in a sensible way; these numbers come because we + // map frames the following way when ivtc'ing: + // 0/30 -> 0/24 diff=0 + // 1/30 -> 1/24 diff=1/120 + // 2/30 -> - + // 3/30 -> 2/24 diff=-1/60 + // 4/30 -> 3/24 diff=-1/120 + if (newpts < pts - delta * boundfactor_minus) + newpts = pts - delta * boundfactor_minus; + if (newpts > pts + delta * boundfactor_plus) + newpts = pts + delta * boundfactor_plus; + if (newpts < p->outpts_prev) + newpts = p->outpts_prev; // damage control + } + p->outpts_prev = newpts; + + return newpts; +} + +double vf_detc_adjust_pts(struct vf_detc_pts_buf *p, double pts, + bool reset_pattern, bool skip_frame) +{ + // standard telecine (see above) + return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame, + 0, 0.5, 1.25, 0.25); +} + +double vf_softpulldown_adjust_pts(struct vf_detc_pts_buf *p, double pts, + bool reset_pattern, bool skip_frame, + int last_frame_duration) +{ + // for the softpulldown filter we get: + // 0/60 -> 0/30 + // 2/60 -> 1/30 + // 5/60 -> 2/30 + // 7/60 -> 3/30, 4/30 + return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame, + 0, 1.0 / last_frame_duration, + 2.0 / last_frame_duration, + 1.0 / last_frame_duration); +} diff --git a/video/filter/vf.h b/video/filter/vf.h new file mode 100644 index 0000000000..4c50f0e9cc --- /dev/null +++ b/video/filter/vf.h @@ -0,0 +1,189 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_VF_H +#define MPLAYER_VF_H + +#include "mp_image.h" +#include "mpcommon.h" +#include "stdbool.h" + +#include "mpc_info.h" +#include "vfcap.h" + +struct MPOpts; +struct vf_instance; +struct vf_priv_s; + +typedef struct vf_info { + const char *info; + const char *name; + const char *author; + const char *comment; + int (*vf_open)(struct vf_instance *vf, char *args); + // Ptr to a struct dscribing the options + const void *opts; +} vf_info_t; + +#define NUM_NUMBERED_MPI 50 + +struct vf_image_context { + mp_image_t *static_images[2]; + mp_image_t *temp_images[1]; + mp_image_t *export_images[1]; + mp_image_t *numbered_images[NUM_NUMBERED_MPI]; + int static_idx; +}; + +struct vf_format_context { + int have_configured; + int orig_width, orig_height, orig_fmt; +}; + +typedef struct vf_instance { + const vf_info_t *info; + // funcs: + int (*config)(struct vf_instance *vf, + int width, int height, int d_width, int d_height, + unsigned int flags, unsigned int outfmt); + int (*control)(struct vf_instance *vf, int request, void *data); + int (*query_format)(struct vf_instance *vf, unsigned int fmt); + void (*get_image)(struct vf_instance *vf, mp_image_t *mpi); + int (*put_image)(struct vf_instance *vf, mp_image_t *mpi, double pts); + void (*start_slice)(struct vf_instance *vf, mp_image_t *mpi); + void (*draw_slice)(struct vf_instance *vf, unsigned char **src, + int *stride, int w, int h, int x, int y); + void (*uninit)(struct vf_instance *vf); + + int (*continue_buffered_image)(struct vf_instance *vf); + // caps: + unsigned int default_caps; // used by default query_format() + unsigned int default_reqs; // used by default config() + // data: + int w, h; + struct vf_image_context imgctx; + struct vf_format_context fmt; + struct vf_instance *next; + mp_image_t *dmpi; + struct vf_priv_s *priv; + struct MPOpts *opts; +} vf_instance_t; + +typedef struct vf_seteq { + const char *item; + int value; +} vf_equalizer_t; + +struct vf_ctrl_screenshot { + // When the screenshot is complete, pass it to this callback. + void (*image_callback)(void *, mp_image_t *); + void *image_callback_ctx; +}; + +#define VFCTRL_QUERY_MAX_PP_LEVEL 4 // query max postprocessing level (if any) +#define VFCTRL_SET_PP_LEVEL 5 // set postprocessing level +#define VFCTRL_SET_EQUALIZER 6 // set color options (brightness,contrast etc) +#define VFCTRL_GET_EQUALIZER 8 // get color options (brightness,contrast etc) +#define VFCTRL_DUPLICATE_FRAME 11 // For encoding - encode zero-change frame +#define VFCTRL_SKIP_NEXT_FRAME 12 // For encoding - drop the next frame that passes thru +#define VFCTRL_FLUSH_FRAMES 13 // For encoding - flush delayed frames +#define VFCTRL_SCREENSHOT 14 // Take screenshot, arg is vf_ctrl_screenshot +#define VFCTRL_INIT_OSD 15 // Filter OSD renderer present? +#define VFCTRL_SET_DEINTERLACE 18 // Set deinterlacing status +#define VFCTRL_GET_DEINTERLACE 19 // Get deinterlacing status +/* Hack to make the OSD state object available to vf_sub which + * access OSD/subtitle state outside of normal OSD draw time. */ +#define VFCTRL_SET_OSD_OBJ 20 +#define VFCTRL_SET_YUV_COLORSPACE 22 // arg is struct mp_csp_details* +#define VFCTRL_GET_YUV_COLORSPACE 23 // arg is struct mp_csp_details* + +// functions: +void vf_mpi_clear(mp_image_t *mpi, int x0, int y0, int w, int h); +mp_image_t *vf_get_image(vf_instance_t *vf, unsigned int outfmt, + int mp_imgtype, int mp_imgflag, int w, int h); + +vf_instance_t *vf_open_plugin(struct MPOpts *opts, + const vf_info_t * const *filter_list, vf_instance_t *next, + const char *name, char **args); +struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts, + const vf_info_t *const *filter_list, vf_instance_t *next, + const char *name, char **args, int *retcode); +vf_instance_t *vf_open_filter(struct MPOpts *opts, vf_instance_t *next, + const char *name, char **args); +vf_instance_t *vf_add_before_vo(vf_instance_t **vf, char *name, char **args); +vf_instance_t *vf_open_encoder(struct MPOpts *opts, vf_instance_t *next, + const char *name, char *args); + +unsigned int vf_match_csp(vf_instance_t **vfp, const unsigned int *list, + unsigned int preferred); +void vf_clone_mpi_attributes(mp_image_t *dst, mp_image_t *src); +void vf_queue_frame(vf_instance_t *vf, int (*)(vf_instance_t *)); +int vf_output_queued_frame(vf_instance_t *vf); + +// default wrappers: +int vf_next_config(struct vf_instance *vf, + int width, int height, int d_width, int d_height, + unsigned int flags, unsigned int outfmt); +int vf_next_control(struct vf_instance *vf, int request, void *data); +int vf_next_query_format(struct vf_instance *vf, unsigned int fmt); +int vf_next_put_image(struct vf_instanc