• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/vf_yadif.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
00003  *               2010      James Darnley <james.darnley@gmail.com>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with Libav; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 
00022 #include "libavutil/cpu.h"
00023 #include "libavutil/common.h"
00024 #include "libavutil/pixdesc.h"
00025 #include "avfilter.h"
00026 #include "yadif.h"
00027 
00028 #undef NDEBUG
00029 #include <assert.h>
00030 
00031 typedef struct {
00038     int mode;
00039 
00045     int parity;
00046 
00047     int frame_pending;
00048 
00053     int auto_enable;
00054 
00055     AVFilterBufferRef *cur;
00056     AVFilterBufferRef *next;
00057     AVFilterBufferRef *prev;
00058     AVFilterBufferRef *out;
00059     void (*filter_line)(uint8_t *dst,
00060                         uint8_t *prev, uint8_t *cur, uint8_t *next,
00061                         int w, int prefs, int mrefs, int parity, int mode);
00062 
00063     const AVPixFmtDescriptor *csp;
00064 } YADIFContext;
00065 
00066 #define CHECK(j)\
00067     {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
00068                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
00069                   + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
00070         if (score < spatial_score) {\
00071             spatial_score= score;\
00072             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
00073 
00074 #define FILTER \
00075     for (x = 0;  x < w; x++) { \
00076         int c = cur[mrefs]; \
00077         int d = (prev2[0] + next2[0])>>1; \
00078         int e = cur[prefs]; \
00079         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
00080         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
00081         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
00082         int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
00083         int spatial_pred = (c+e)>>1; \
00084         int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
00085                           + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
00086  \
00087         CHECK(-1) CHECK(-2) }} }} \
00088         CHECK( 1) CHECK( 2) }} }} \
00089  \
00090         if (mode < 2) { \
00091             int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
00092             int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
00093             int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
00094             int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
00095  \
00096             diff = FFMAX3(diff, min, -max); \
00097         } \
00098  \
00099         if (spatial_pred > d + diff) \
00100            spatial_pred = d + diff; \
00101         else if (spatial_pred < d - diff) \
00102            spatial_pred = d - diff; \
00103  \
00104         dst[0] = spatial_pred; \
00105  \
00106         dst++; \
00107         cur++; \
00108         prev++; \
00109         next++; \
00110         prev2++; \
00111         next2++; \
00112     }
00113 
00114 static void filter_line_c(uint8_t *dst,
00115                           uint8_t *prev, uint8_t *cur, uint8_t *next,
00116                           int w, int prefs, int mrefs, int parity, int mode)
00117 {
00118     int x;
00119     uint8_t *prev2 = parity ? prev : cur ;
00120     uint8_t *next2 = parity ? cur  : next;
00121 
00122     FILTER
00123 }
00124 
00125 static void filter_line_c_16bit(uint16_t *dst,
00126                                 uint16_t *prev, uint16_t *cur, uint16_t *next,
00127                                 int w, int prefs, int mrefs, int parity, int mode)
00128 {
00129     int x;
00130     uint16_t *prev2 = parity ? prev : cur ;
00131     uint16_t *next2 = parity ? cur  : next;
00132     mrefs /= 2;
00133     prefs /= 2;
00134 
00135     FILTER
00136 }
00137 
00138 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
00139                    int parity, int tff)
00140 {
00141     YADIFContext *yadif = ctx->priv;
00142     int y, i;
00143 
00144     for (i = 0; i < yadif->csp->nb_components; i++) {
00145         int w = dstpic->video->w;
00146         int h = dstpic->video->h;
00147         int refs = yadif->cur->linesize[i];
00148         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
00149 
00150         if (i == 1 || i == 2) {
00151         /* Why is this not part of the per-plane description thing? */
00152             w >>= yadif->csp->log2_chroma_w;
00153             h >>= yadif->csp->log2_chroma_h;
00154         }
00155 
00156         for (y = 0; y < h; y++) {
00157             if ((y ^ parity) & 1) {
00158                 uint8_t *prev = &yadif->prev->data[i][y*refs];
00159                 uint8_t *cur  = &yadif->cur ->data[i][y*refs];
00160                 uint8_t *next = &yadif->next->data[i][y*refs];
00161                 uint8_t *dst  = &dstpic->data[i][y*dstpic->linesize[i]];
00162                 int     mode  = y==1 || y+2==h ? 2 : yadif->mode;
00163                 yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
00164             } else {
00165                 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
00166                        &yadif->cur->data[i][y*refs], w*df);
00167             }
00168         }
00169     }
00170 #if HAVE_MMX
00171     __asm__ volatile("emms \n\t" : : : "memory");
00172 #endif
00173 }
00174 
00175 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00176 {
00177     AVFilterBufferRef *picref;
00178     int width = FFALIGN(w, 32);
00179     int height= FFALIGN(h+2, 32);
00180     int i;
00181 
00182     picref = avfilter_default_get_video_buffer(link, perms, width, height);
00183 
00184     picref->video->w = w;
00185     picref->video->h = h;
00186 
00187     for (i = 0; i < 3; i++)
00188         picref->data[i] += picref->linesize[i];
00189 
00190     return picref;
00191 }
00192 
00193 static void return_frame(AVFilterContext *ctx, int is_second)
00194 {
00195     YADIFContext *yadif = ctx->priv;
00196     AVFilterLink *link= ctx->outputs[0];
00197     int tff;
00198 
00199     if (yadif->parity == -1) {
00200         tff = yadif->cur->video->interlaced ?
00201             yadif->cur->video->top_field_first : 1;
00202     } else {
00203         tff = yadif->parity^1;
00204     }
00205 
00206     if (is_second) {
00207         yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
00208                                                AV_PERM_REUSE, link->w, link->h);
00209         avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
00210         yadif->out->video->interlaced = 0;
00211     }
00212 
00213     if (!yadif->csp)
00214         yadif->csp = &av_pix_fmt_descriptors[link->format];
00215     if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
00216         yadif->filter_line = filter_line_c_16bit;
00217 
00218     filter(ctx, yadif->out, tff ^ !is_second, tff);
00219 
00220     if (is_second) {
00221         if (yadif->next->pts != AV_NOPTS_VALUE &&
00222             yadif->cur->pts != AV_NOPTS_VALUE) {
00223             yadif->out->pts =
00224                 (yadif->next->pts&yadif->cur->pts) +
00225                 ((yadif->next->pts^yadif->cur->pts)>>1);
00226         } else {
00227             yadif->out->pts = AV_NOPTS_VALUE;
00228         }
00229         avfilter_start_frame(ctx->outputs[0], yadif->out);
00230     }
00231     avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
00232     avfilter_end_frame(ctx->outputs[0]);
00233 
00234     yadif->frame_pending = (yadif->mode&1) && !is_second;
00235 }
00236 
00237 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00238 {
00239     AVFilterContext *ctx = link->dst;
00240     YADIFContext *yadif = ctx->priv;
00241 
00242     if (yadif->frame_pending)
00243         return_frame(ctx, 1);
00244 
00245     if (yadif->prev)
00246         avfilter_unref_buffer(yadif->prev);
00247     yadif->prev = yadif->cur;
00248     yadif->cur  = yadif->next;
00249     yadif->next = picref;
00250 
00251     if (!yadif->cur)
00252         return;
00253 
00254     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
00255         yadif->out  = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
00256         avfilter_unref_buffer(yadif->prev);
00257         yadif->prev = NULL;
00258         avfilter_start_frame(ctx->outputs[0], yadif->out);
00259         return;
00260     }
00261 
00262     if (!yadif->prev)
00263         yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
00264 
00265     yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
00266                                        AV_PERM_REUSE, link->w, link->h);
00267 
00268     avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
00269     yadif->out->video->interlaced = 0;
00270     avfilter_start_frame(ctx->outputs[0], yadif->out);
00271 }
00272 
00273 static void end_frame(AVFilterLink *link)
00274 {
00275     AVFilterContext *ctx = link->dst;
00276     YADIFContext *yadif = ctx->priv;
00277 
00278     if (!yadif->out)
00279         return;
00280 
00281     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
00282         avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
00283         avfilter_end_frame(ctx->outputs[0]);
00284         return;
00285     }
00286 
00287     return_frame(ctx, 0);
00288 }
00289 
00290 static int request_frame(AVFilterLink *link)
00291 {
00292     AVFilterContext *ctx = link->src;
00293     YADIFContext *yadif = ctx->priv;
00294 
00295     if (yadif->frame_pending) {
00296         return_frame(ctx, 1);
00297         return 0;
00298     }
00299 
00300     do {
00301         int ret;
00302 
00303         if ((ret = avfilter_request_frame(link->src->inputs[0])))
00304             return ret;
00305     } while (!yadif->cur);
00306 
00307     return 0;
00308 }
00309 
00310 static int poll_frame(AVFilterLink *link)
00311 {
00312     YADIFContext *yadif = link->src->priv;
00313     int ret, val;
00314 
00315     if (yadif->frame_pending)
00316         return 1;
00317 
00318     val = avfilter_poll_frame(link->src->inputs[0]);
00319 
00320     if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
00321         if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
00322             return ret;
00323         val = avfilter_poll_frame(link->src->inputs[0]);
00324     }
00325     assert(yadif->next || !val);
00326 
00327     if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
00328         return val;
00329 
00330     return val * ((yadif->mode&1)+1);
00331 }
00332 
00333 static av_cold void uninit(AVFilterContext *ctx)
00334 {
00335     YADIFContext *yadif = ctx->priv;
00336 
00337     if (yadif->prev) avfilter_unref_buffer(yadif->prev);
00338     if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
00339     if (yadif->next) avfilter_unref_buffer(yadif->next);
00340 }
00341 
00342 static int query_formats(AVFilterContext *ctx)
00343 {
00344     static const enum PixelFormat pix_fmts[] = {
00345         PIX_FMT_YUV420P,
00346         PIX_FMT_YUV422P,
00347         PIX_FMT_YUV444P,
00348         PIX_FMT_YUV410P,
00349         PIX_FMT_YUV411P,
00350         PIX_FMT_GRAY8,
00351         PIX_FMT_YUVJ420P,
00352         PIX_FMT_YUVJ422P,
00353         PIX_FMT_YUVJ444P,
00354         AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
00355         PIX_FMT_YUV440P,
00356         PIX_FMT_YUVJ440P,
00357         AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
00358         AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
00359         AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
00360         AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
00361         AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
00362         AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
00363         PIX_FMT_YUVA420P,
00364         PIX_FMT_NONE
00365     };
00366 
00367     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00368 
00369     return 0;
00370 }
00371 
00372 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00373 {
00374     YADIFContext *yadif = ctx->priv;
00375     av_unused int cpu_flags = av_get_cpu_flags();
00376 
00377     yadif->mode = 0;
00378     yadif->parity = -1;
00379     yadif->auto_enable = 0;
00380     yadif->csp = NULL;
00381 
00382     if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
00383 
00384     yadif->filter_line = filter_line_c;
00385     if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
00386         yadif->filter_line = ff_yadif_filter_line_ssse3;
00387     else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
00388         yadif->filter_line = ff_yadif_filter_line_sse2;
00389     else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
00390         yadif->filter_line = ff_yadif_filter_line_mmx;
00391 
00392     av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
00393 
00394     return 0;
00395 }
00396 
00397 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
00398 
00399 AVFilter avfilter_vf_yadif = {
00400     .name          = "yadif",
00401     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
00402 
00403     .priv_size     = sizeof(YADIFContext),
00404     .init          = init,
00405     .uninit        = uninit,
00406     .query_formats = query_formats,
00407 
00408     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00409                                     .type             = AVMEDIA_TYPE_VIDEO,
00410                                     .start_frame      = start_frame,
00411                                     .get_video_buffer = get_video_buffer,
00412                                     .draw_slice       = null_draw_slice,
00413                                     .end_frame        = end_frame, },
00414                                   { .name = NULL}},
00415 
00416     .outputs   = (AVFilterPad[]) {{ .name             = "default",
00417                                     .type             = AVMEDIA_TYPE_VIDEO,
00418                                     .poll_frame       = poll_frame,
00419                                     .request_frame    = request_frame, },
00420                                   { .name = NULL}},
00421 };
Generated on Sat Mar 17 2012 12:57:52 for Libav by doxygen 1.7.1