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 "formats.h"
00026 #include "internal.h"
00027 #include "video.h"
00028 #include "yadif.h"
00029
00030 #undef NDEBUG
00031 #include <assert.h>
00032
00033 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
00034
00035 #define CHECK(j)\
00036 { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
00037 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
00038 + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
00039 if (score < spatial_score) {\
00040 spatial_score= score;\
00041 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
00042
00043 #define FILTER \
00044 for (x = 0; x < w; x++) { \
00045 int c = cur[mrefs]; \
00046 int d = (prev2[0] + next2[0])>>1; \
00047 int e = cur[prefs]; \
00048 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
00049 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
00050 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
00051 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
00052 int spatial_pred = (c+e) >> 1; \
00053 int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
00054 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
00055 \
00056 CHECK(-1) CHECK(-2) }} }} \
00057 CHECK( 1) CHECK( 2) }} }} \
00058 \
00059 if (mode < 2) { \
00060 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
00061 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
00062 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
00063 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
00064 \
00065 diff = FFMAX3(diff, min, -max); \
00066 } \
00067 \
00068 if (spatial_pred > d + diff) \
00069 spatial_pred = d + diff; \
00070 else if (spatial_pred < d - diff) \
00071 spatial_pred = d - diff; \
00072 \
00073 dst[0] = spatial_pred; \
00074 \
00075 dst++; \
00076 cur++; \
00077 prev++; \
00078 next++; \
00079 prev2++; \
00080 next2++; \
00081 }
00082
00083 static void filter_line_c(uint8_t *dst,
00084 uint8_t *prev, uint8_t *cur, uint8_t *next,
00085 int w, int prefs, int mrefs, int parity, int mode)
00086 {
00087 int x;
00088 uint8_t *prev2 = parity ? prev : cur ;
00089 uint8_t *next2 = parity ? cur : next;
00090
00091 FILTER
00092 }
00093
00094 static void filter_line_c_16bit(uint16_t *dst,
00095 uint16_t *prev, uint16_t *cur, uint16_t *next,
00096 int w, int prefs, int mrefs, int parity,
00097 int mode)
00098 {
00099 int x;
00100 uint16_t *prev2 = parity ? prev : cur ;
00101 uint16_t *next2 = parity ? cur : next;
00102 mrefs /= 2;
00103 prefs /= 2;
00104
00105 FILTER
00106 }
00107
00108 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
00109 int parity, int tff)
00110 {
00111 YADIFContext *yadif = ctx->priv;
00112 int y, i;
00113
00114 for (i = 0; i < yadif->csp->nb_components; i++) {
00115 int w = dstpic->video->w;
00116 int h = dstpic->video->h;
00117 int refs = yadif->cur->linesize[i];
00118 int absrefs = FFABS(refs);
00119 int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
00120
00121 if (i == 1 || i == 2) {
00122
00123 w >>= yadif->csp->log2_chroma_w;
00124 h >>= yadif->csp->log2_chroma_h;
00125 }
00126
00127 if(yadif->temp_line_size < absrefs) {
00128 av_free(yadif->temp_line);
00129 yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
00130 yadif->temp_line_size = absrefs;
00131 }
00132
00133 for (y = 0; y < h; y++) {
00134 if ((y ^ parity) & 1) {
00135 uint8_t *prev = &yadif->prev->data[i][y * refs];
00136 uint8_t *cur = &yadif->cur ->data[i][y * refs];
00137 uint8_t *next = &yadif->next->data[i][y * refs];
00138 uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
00139 int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
00140 int prefs = y+1<h ? refs : -refs;
00141 int mrefs = y ?-refs : refs;
00142
00143 if(y<=1 || y+2>=h) {
00144 uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
00145 if(mode<2)
00146 memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
00147 memcpy(tmp+mrefs, cur+mrefs, w*df);
00148 memcpy(tmp , cur , w*df);
00149 if(prefs != mrefs) {
00150 memcpy(tmp+prefs, cur+prefs, w*df);
00151 if(mode<2)
00152 memcpy(tmp+2*prefs, cur+2*prefs, w*df);
00153 }
00154 cur = tmp;
00155 }
00156
00157 yadif->filter_line(dst, prev, cur, next, w,
00158 prefs, mrefs,
00159 parity ^ tff, mode);
00160 } else {
00161 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
00162 &yadif->cur->data[i][y * refs], w * df);
00163 }
00164 }
00165 }
00166
00167 emms_c();
00168 }
00169
00170 static int return_frame(AVFilterContext *ctx, int is_second)
00171 {
00172 YADIFContext *yadif = ctx->priv;
00173 AVFilterLink *link = ctx->outputs[0];
00174 int tff, ret;
00175
00176 if (yadif->parity == -1) {
00177 tff = yadif->cur->video->interlaced ?
00178 yadif->cur->video->top_field_first : 1;
00179 } else {
00180 tff = yadif->parity ^ 1;
00181 }
00182
00183 if (is_second) {
00184 yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
00185 if (!yadif->out)
00186 return AVERROR(ENOMEM);
00187
00188 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
00189 yadif->out->video->interlaced = 0;
00190 }
00191
00192 if (!yadif->csp)
00193 yadif->csp = &av_pix_fmt_descriptors[link->format];
00194 if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
00195 yadif->filter_line = (void*)filter_line_c_16bit;
00196
00197 filter(ctx, yadif->out, tff ^ !is_second, tff);
00198
00199 if (is_second) {
00200 int64_t cur_pts = yadif->cur->pts;
00201 int64_t next_pts = yadif->next->pts;
00202
00203 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
00204 yadif->out->pts = cur_pts + next_pts;
00205 } else {
00206 yadif->out->pts = AV_NOPTS_VALUE;
00207 }
00208 ret = ff_start_frame(ctx->outputs[0], yadif->out);
00209 if (ret < 0)
00210 return ret;
00211 }
00212 if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
00213 (ret = ff_end_frame(ctx->outputs[0])) < 0)
00214 return ret;
00215
00216 yadif->frame_pending = (yadif->mode&1) && !is_second;
00217 return 0;
00218 }
00219
00220 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00221 {
00222 AVFilterContext *ctx = link->dst;
00223 YADIFContext *yadif = ctx->priv;
00224
00225 av_assert0(picref);
00226
00227 if (picref->video->h < 3 || picref->video->w < 3) {
00228 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
00229 return AVERROR(EINVAL);
00230 }
00231
00232 if (yadif->frame_pending)
00233 return_frame(ctx, 1);
00234
00235 if (yadif->prev)
00236 avfilter_unref_buffer(yadif->prev);
00237 yadif->prev = yadif->cur;
00238 yadif->cur = yadif->next;
00239 yadif->next = picref;
00240 link->cur_buf = NULL;
00241
00242 if (!yadif->cur)
00243 return 0;
00244
00245 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
00246 yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
00247 if (!yadif->out)
00248 return AVERROR(ENOMEM);
00249
00250 avfilter_unref_bufferp(&yadif->prev);
00251 if (yadif->out->pts != AV_NOPTS_VALUE)
00252 yadif->out->pts *= 2;
00253 return ff_start_frame(ctx->outputs[0], yadif->out);
00254 }
00255
00256 if (!yadif->prev &&
00257 !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
00258 return AVERROR(ENOMEM);
00259
00260 yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
00261 link->w, link->h);
00262 if (!yadif->out)
00263 return AVERROR(ENOMEM);
00264
00265 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
00266 yadif->out->video->interlaced = 0;
00267
00268 if (yadif->out->pts != AV_NOPTS_VALUE)
00269 yadif->out->pts *= 2;
00270
00271 return ff_start_frame(ctx->outputs[0], yadif->out);
00272 }
00273
00274 static int end_frame(AVFilterLink *link)
00275 {
00276 AVFilterContext *ctx = link->dst;
00277 YADIFContext *yadif = ctx->priv;
00278
00279 if (!yadif->out)
00280 return 0;
00281
00282 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
00283 int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
00284 if (ret >= 0)
00285 ret = ff_end_frame(ctx->outputs[0]);
00286 return ret;
00287 }
00288
00289 return_frame(ctx, 0);
00290 return 0;
00291 }
00292
00293 static int request_frame(AVFilterLink *link)
00294 {
00295 AVFilterContext *ctx = link->src;
00296 YADIFContext *yadif = ctx->priv;
00297
00298 if (yadif->frame_pending) {
00299 return_frame(ctx, 1);
00300 return 0;
00301 }
00302
00303 do {
00304 int ret;
00305
00306 if (yadif->eof)
00307 return AVERROR_EOF;
00308
00309 ret = ff_request_frame(link->src->inputs[0]);
00310
00311 if (ret == AVERROR_EOF && yadif->cur) {
00312 AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
00313
00314 if (!next)
00315 return AVERROR(ENOMEM);
00316
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 = ff_poll_frame(link->src->inputs[0]);
00339 if (val <= 0)
00340 return val;
00341
00342
00343 if (val >= 1 && !yadif->next) {
00344 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
00345 return ret;
00346 val = ff_poll_frame(link->src->inputs[0]);
00347 if (val <= 0)
00348 return val;
00349 }
00350 assert(yadif->next || !val);
00351
00352 if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
00353 return val;
00354
00355 return val * ((yadif->mode&1)+1);
00356 }
00357
00358 static av_cold void uninit(AVFilterContext *ctx)
00359 {
00360 YADIFContext *yadif = ctx->priv;
00361
00362 if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
00363 if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
00364 if (yadif->next) avfilter_unref_bufferp(&yadif->next);
00365 av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
00366 }
00367
00368 static int query_formats(AVFilterContext *ctx)
00369 {
00370 static const enum PixelFormat pix_fmts[] = {
00371 PIX_FMT_YUV420P,
00372 PIX_FMT_YUV422P,
00373 PIX_FMT_YUV444P,
00374 PIX_FMT_YUV410P,
00375 PIX_FMT_YUV411P,
00376 PIX_FMT_GRAY8,
00377 PIX_FMT_YUVJ420P,
00378 PIX_FMT_YUVJ422P,
00379 PIX_FMT_YUVJ444P,
00380 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
00381 PIX_FMT_YUV440P,
00382 PIX_FMT_YUVJ440P,
00383 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
00384 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
00385 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
00386 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
00387 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
00388 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
00389 PIX_FMT_YUVA420P,
00390 PIX_FMT_YUVA422P,
00391 PIX_FMT_YUVA444P,
00392 PIX_FMT_NONE
00393 };
00394
00395 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00396
00397 return 0;
00398 }
00399
00400 static av_cold int init(AVFilterContext *ctx, const char *args)
00401 {
00402 YADIFContext *yadif = ctx->priv;
00403
00404 yadif->mode = 0;
00405 yadif->parity = -1;
00406 yadif->auto_enable = 0;
00407 yadif->csp = NULL;
00408
00409 if (args)
00410 sscanf(args, "%d:%d:%d",
00411 &yadif->mode, &yadif->parity, &yadif->auto_enable);
00412
00413 yadif->filter_line = filter_line_c;
00414
00415 if (HAVE_MMX)
00416 ff_yadif_init_x86(yadif);
00417
00418 av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
00419 yadif->mode, yadif->parity, yadif->auto_enable);
00420
00421 return 0;
00422 }
00423
00424 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00425 {
00426 return 0;
00427 }
00428
00429 static int config_props(AVFilterLink *link)
00430 {
00431 YADIFContext *yadif = link->src->priv;
00432
00433 link->time_base.num = link->src->inputs[0]->time_base.num;
00434 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
00435 link->w = link->src->inputs[0]->w;
00436 link->h = link->src->inputs[0]->h;
00437
00438 if(yadif->mode&1)
00439 link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
00440
00441 return 0;
00442 }
00443
00444 AVFilter avfilter_vf_yadif = {
00445 .name = "yadif",
00446 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
00447
00448 .priv_size = sizeof(YADIFContext),
00449 .init = init,
00450 .uninit = uninit,
00451 .query_formats = query_formats,
00452
00453 .inputs = (const AVFilterPad[]) {{ .name = "default",
00454 .type = AVMEDIA_TYPE_VIDEO,
00455 .start_frame = start_frame,
00456 .draw_slice = null_draw_slice,
00457 .end_frame = end_frame,
00458 .min_perms = AV_PERM_PRESERVE, },
00459 { .name = NULL}},
00460
00461 .outputs = (const AVFilterPad[]) {{ .name = "default",
00462 .type = AVMEDIA_TYPE_VIDEO,
00463 .poll_frame = poll_frame,
00464 .request_frame = request_frame,
00465 .config_props = config_props, },
00466 { .name = NULL}},
00467 };