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