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