| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at> | ||
| 3 | * 2010 James Darnley <james.darnley@gmail.com> | ||
| 4 | |||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/avassert.h" | ||
| 23 | #include "libavutil/imgutils.h" | ||
| 24 | #include "filters.h" | ||
| 25 | #include "video.h" | ||
| 26 | #include "yadif.h" | ||
| 27 | |||
| 28 | 306 | static int return_frame(AVFilterContext *ctx, int is_second) | |
| 29 | { | ||
| 30 | 306 | YADIFContext *yadif = ctx->priv; | |
| 31 | 306 | AVFilterLink *link = ctx->outputs[0]; | |
| 32 | int tff, ret; | ||
| 33 | |||
| 34 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | if (yadif->parity == -1) { |
| 35 | 306 | tff = (yadif->cur->flags & AV_FRAME_FLAG_INTERLACED) ? | |
| 36 |
2/4✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 306 times.
|
306 | !!(yadif->cur->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 1; |
| 37 | } else { | ||
| 38 | ✗ | tff = yadif->parity ^ 1; | |
| 39 | } | ||
| 40 | |||
| 41 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 246 times.
|
306 | if (is_second) { |
| 42 | 60 | yadif->out = ff_get_video_buffer(link, link->w, link->h); | |
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (!yadif->out) |
| 44 | ✗ | return AVERROR(ENOMEM); | |
| 45 | |||
| 46 | 60 | av_frame_copy_props(yadif->out, yadif->cur); | |
| 47 | 60 | yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (yadif->current_field == YADIF_FIELD_BACK_END) |
| 49 | ✗ | yadif->current_field = YADIF_FIELD_END; | |
| 50 | } | ||
| 51 | |||
| 52 | 306 | yadif->filter(ctx, yadif->out, tff ^ !is_second, tff); | |
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 246 times.
|
306 | if (is_second) { |
| 55 | 60 | int64_t cur_pts = yadif->cur->pts; | |
| 56 | 60 | int64_t next_pts = yadif->next->pts; | |
| 57 | |||
| 58 |
2/4✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
|
60 | if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) { |
| 59 | 60 | yadif->out->pts = cur_pts + next_pts; | |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (yadif->pts_multiplier == 1) { |
| 61 | ✗ | yadif->out->pts >>= 1; | |
| 62 | ✗ | yadif->out->duration >>= 1; | |
| 63 | } | ||
| 64 | } else { | ||
| 65 | ✗ | yadif->out->pts = AV_NOPTS_VALUE; | |
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 306 | ff_ccfifo_inject(&yadif->cc_fifo, yadif->out); | |
| 70 | 306 | ret = ff_filter_frame(ctx->outputs[0], yadif->out); | |
| 71 | |||
| 72 |
4/4✓ Branch 0 taken 120 times.
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 60 times.
|
306 | yadif->frame_pending = (yadif->mode&1) && !is_second; |
| 73 | 306 | return ret; | |
| 74 | } | ||
| 75 | |||
| 76 | 1254 | static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b) | |
| 77 | { | ||
| 78 | int i; | ||
| 79 |
2/2✓ Branch 0 taken 3762 times.
✓ Branch 1 taken 1254 times.
|
5016 | for (i = 0; i < yadif->csp->nb_components; i++) |
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3762 times.
|
3762 | if (a->linesize[i] != b->linesize[i]) |
| 81 | ✗ | return 1; | |
| 82 | 1254 | return 0; | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | static void fixstride(AVFilterLink *link, AVFrame *f) | |
| 86 | { | ||
| 87 | ✗ | AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height); | |
| 88 | ✗ | if(!dst) | |
| 89 | ✗ | return; | |
| 90 | ✗ | av_frame_copy_props(dst, f); | |
| 91 | ✗ | av_image_copy2(dst->data, dst->linesize, | |
| 92 | ✗ | f->data, f->linesize, | |
| 93 | ✗ | dst->format, dst->width, dst->height); | |
| 94 | ✗ | av_frame_unref(f); | |
| 95 | ✗ | av_frame_move_ref(f, dst); | |
| 96 | ✗ | av_frame_free(&dst); | |
| 97 | } | ||
| 98 | |||
| 99 | 254 | int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame) | |
| 100 | { | ||
| 101 | 254 | AVFilterContext *ctx = link->dst; | |
| 102 | 254 | YADIFContext *yadif = ctx->priv; | |
| 103 | |||
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
|
254 | av_assert0(frame); |
| 105 | |||
| 106 | 254 | ff_ccfifo_extract(&yadif->cc_fifo, frame); | |
| 107 | |||
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
|
254 | if (yadif->frame_pending) |
| 109 | ✗ | return_frame(ctx, 1); | |
| 110 | |||
| 111 |
2/2✓ Branch 0 taken 238 times.
✓ Branch 1 taken 16 times.
|
254 | if (yadif->prev) |
| 112 | 238 | av_frame_free(&yadif->prev); | |
| 113 | 254 | yadif->prev = yadif->cur; | |
| 114 | 254 | yadif->cur = yadif->next; | |
| 115 | 254 | yadif->next = frame; | |
| 116 | |||
| 117 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 246 times.
|
254 | if (!yadif->cur) { |
| 118 | 8 | yadif->cur = av_frame_clone(yadif->next); | |
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (!yadif->cur) |
| 120 | ✗ | return AVERROR(ENOMEM); | |
| 121 | 8 | yadif->current_field = YADIF_FIELD_END; | |
| 122 | } | ||
| 123 | |||
| 124 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 254 times.
|
254 | if (checkstride(yadif, yadif->next, yadif->cur)) { |
| 125 | ✗ | av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n"); | |
| 126 | ✗ | fixstride(link, yadif->next); | |
| 127 | } | ||
| 128 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 254 times.
|
254 | if (checkstride(yadif, yadif->next, yadif->cur)) |
| 129 | ✗ | fixstride(link, yadif->cur); | |
| 130 |
3/4✓ Branch 0 taken 246 times.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 246 times.
|
254 | if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev)) |
| 131 | ✗ | fixstride(link, yadif->prev); | |
| 132 |
4/6✓ Branch 1 taken 254 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 246 times.
✓ Branch 4 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 246 times.
|
254 | if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) { |
| 133 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n"); | |
| 134 | ✗ | return -1; | |
| 135 | } | ||
| 136 | |||
| 137 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 246 times.
|
254 | if (!yadif->prev) |
| 138 | 8 | return 0; | |
| 139 | |||
| 140 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
246 | if ((yadif->deint && !(yadif->cur->flags & AV_FRAME_FLAG_INTERLACED)) || |
| 141 |
1/2✓ Branch 0 taken 246 times.
✗ Branch 1 not taken.
|
246 | ctx->is_disabled || |
| 142 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
246 | (yadif->deint && !(yadif->prev->flags & AV_FRAME_FLAG_INTERLACED) && yadif->prev->repeat_pict) || |
| 143 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
246 | (yadif->deint && !(yadif->next->flags & AV_FRAME_FLAG_INTERLACED) && yadif->next->repeat_pict) |
| 144 | ) { | ||
| 145 | ✗ | yadif->out = av_frame_clone(yadif->cur); | |
| 146 | ✗ | if (!yadif->out) | |
| 147 | ✗ | return AVERROR(ENOMEM); | |
| 148 | |||
| 149 | ✗ | ff_ccfifo_inject(&yadif->cc_fifo, yadif->out); | |
| 150 | ✗ | av_frame_free(&yadif->prev); | |
| 151 | ✗ | if (yadif->out->pts != AV_NOPTS_VALUE) | |
| 152 | ✗ | yadif->out->pts *= yadif->pts_multiplier; | |
| 153 | ✗ | yadif->out->duration *= yadif->pts_multiplier; | |
| 154 | ✗ | return ff_filter_frame(ctx->outputs[0], yadif->out); | |
| 155 | } | ||
| 156 | |||
| 157 | 246 | yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h); | |
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
|
246 | if (!yadif->out) |
| 159 | ✗ | return AVERROR(ENOMEM); | |
| 160 | |||
| 161 | 246 | av_frame_copy_props(yadif->out, yadif->cur); | |
| 162 | 246 | yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
| 163 | |||
| 164 |
1/2✓ Branch 0 taken 246 times.
✗ Branch 1 not taken.
|
246 | if (yadif->out->pts != AV_NOPTS_VALUE) |
| 165 | 246 | yadif->out->pts *= yadif->pts_multiplier; | |
| 166 |
2/2✓ Branch 0 taken 186 times.
✓ Branch 1 taken 60 times.
|
246 | if (!(yadif->mode & 1)) |
| 167 | 186 | yadif->out->duration *= yadif->pts_multiplier; | |
| 168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | else if (yadif->pts_multiplier == 1) |
| 169 | ✗ | yadif->out->duration >>= 1; | |
| 170 | |||
| 171 | 246 | return return_frame(ctx, 0); | |
| 172 | } | ||
| 173 | |||
| 174 | 312 | int ff_yadif_request_frame(AVFilterLink *link) | |
| 175 | { | ||
| 176 | 312 | AVFilterContext *ctx = link->src; | |
| 177 | 312 | YADIFContext *yadif = ctx->priv; | |
| 178 | int ret; | ||
| 179 | |||
| 180 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 252 times.
|
312 | if (yadif->frame_pending) { |
| 181 | 60 | return_frame(ctx, 1); | |
| 182 | 60 | return 0; | |
| 183 | } | ||
| 184 | |||
| 185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
|
252 | if (yadif->eof) |
| 186 | ✗ | return AVERROR_EOF; | |
| 187 | |||
| 188 | 252 | ret = ff_request_frame(ctx->inputs[0]); | |
| 189 | |||
| 190 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 246 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
252 | if (ret == AVERROR_EOF && yadif->cur) { |
| 191 | 6 | AVFrame *next = av_frame_clone(yadif->next); | |
| 192 | |||
| 193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!next) |
| 194 | ✗ | return AVERROR(ENOMEM); | |
| 195 | |||
| 196 | 6 | yadif->current_field = YADIF_FIELD_BACK_END; | |
| 197 | 6 | next->pts = yadif->next->pts * 2 - yadif->cur->pts; | |
| 198 | |||
| 199 | 6 | ff_yadif_filter_frame(ctx->inputs[0], next); | |
| 200 | 6 | yadif->eof = 1; | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
|
246 | } else if (ret < 0) { |
| 202 | ✗ | return ret; | |
| 203 | } | ||
| 204 | |||
| 205 | 252 | return 0; | |
| 206 | } | ||
| 207 | |||
| 208 | 8 | int ff_yadif_config_output_common(AVFilterLink *outlink) | |
| 209 | { | ||
| 210 | 8 | AVFilterContext *ctx = outlink->src; | |
| 211 | 8 | FilterLink *il = ff_filter_link(ctx->inputs[0]); | |
| 212 | 8 | FilterLink *ol = ff_filter_link(outlink); | |
| 213 | 8 | YADIFContext *yadif = ctx->priv; | |
| 214 | 8 | AVRational tb = ctx->inputs[0]->time_base; | |
| 215 | int ret; | ||
| 216 | |||
| 217 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | if (av_reduce(&outlink->time_base.num, &outlink->time_base.den, tb.num, tb.den * 2LL, INT_MAX)) { |
| 218 | 8 | yadif->pts_multiplier = 2; | |
| 219 | } else { | ||
| 220 | ✗ | av_log(ctx, AV_LOG_WARNING, "Cannot use exact output timebase\n"); | |
| 221 | ✗ | outlink->time_base = tb; | |
| 222 | ✗ | yadif->pts_multiplier = 1; | |
| 223 | } | ||
| 224 | |||
| 225 | 8 | outlink->w = ctx->inputs[0]->w; | |
| 226 | 8 | outlink->h = ctx->inputs[0]->h; | |
| 227 | |||
| 228 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (outlink->w < 3 || outlink->h < 3) { |
| 229 | ✗ | av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n"); | |
| 230 | ✗ | return AVERROR(EINVAL); | |
| 231 | } | ||
| 232 | |||
| 233 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if(yadif->mode & 1) |
| 234 | 2 | ol->frame_rate = av_mul_q(il->frame_rate, | |
| 235 | 2 | (AVRational){2, 1}); | |
| 236 | else | ||
| 237 | 6 | ol->frame_rate = il->frame_rate; | |
| 238 | |||
| 239 | 8 | ret = ff_ccfifo_init(&yadif->cc_fifo, ol->frame_rate, ctx); | |
| 240 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ret < 0) { |
| 241 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n"); | |
| 242 | ✗ | return ret; | |
| 243 | } | ||
| 244 | |||
| 245 | 8 | return 0; | |
| 246 | } | ||
| 247 | |||
| 248 | 16 | void ff_yadif_uninit(AVFilterContext *ctx) | |
| 249 | { | ||
| 250 | 16 | YADIFContext *yadif = ctx->priv; | |
| 251 | |||
| 252 | 16 | av_frame_free(&yadif->prev); | |
| 253 | 16 | av_frame_free(&yadif->cur ); | |
| 254 | 16 | av_frame_free(&yadif->next); | |
| 255 | 16 | ff_ccfifo_uninit(&yadif->cc_fifo); | |
| 256 | 16 | } | |
| 257 | |||
| 258 | #define OFFSET(x) offsetof(YADIFContext, x) | ||
| 259 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 260 | |||
| 261 | #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u } | ||
| 262 | |||
| 263 | const AVOption ff_yadif_options[] = { | ||
| 264 | { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, .unit = "mode"}, | ||
| 265 | CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"), | ||
| 266 | CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"), | ||
| 267 | CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"), | ||
| 268 | CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"), | ||
| 269 | |||
| 270 | { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" }, | ||
| 271 | CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"), | ||
| 272 | CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"), | ||
| 273 | CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"), | ||
| 274 | |||
| 275 | { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" }, | ||
| 276 | CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"), | ||
| 277 | CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"), | ||
| 278 | |||
| 279 | { NULL } | ||
| 280 | }; | ||
| 281 |