| @@ -39,6 +39,29 @@ enum PhaseMode { · 38 unmodified lines |
| 39 | AUTO_ANALYZE | 39 | AUTO_ANALYZE |
| 40 | }; | 40 | }; |
| 41 | | 41 | |
| | 42 | #define DEPTH 8 |
| | 43 | #include "phase_template.c" |
| | 44 | |
| | 45 | #undef DEPTH |
| | 46 | #define DEPTH 9 |
| | 47 | #include "phase_template.c" |
| | 48 | |
| | 49 | #undef DEPTH |
| | 50 | #define DEPTH 10 |
| | 51 | #include "phase_template.c" |
| | 52 | |
| | 53 | #undef DEPTH |
| | 54 | #define DEPTH 12 |
| | 55 | #include "phase_template.c" |
| | 56 | |
| | 57 | #undef DEPTH |
| | 58 | #define DEPTH 14 |
| | 59 | #include "phase_template.c" |
| | 60 | |
| | 61 | #undef DEPTH |
| | 62 | #define DEPTH 16 |
| | 63 | #include "phase_template.c" |
| | 64 | |
| 42 | typedef struct PhaseContext { | 65 | typedef struct PhaseContext { |
| 43 | const AVClass *class; | 66 | const AVClass *class; |
| 44 | int mode; ///<PhaseMode | 67 | int mode; ///<PhaseMode |
| @@ -46,6 +69,8 @@ typedef struct PhaseContext { · 1 unmodified line |
| 46 | int nb_planes; | 69 | int nb_planes; |
| 47 | int planeheight[4]; | 70 | int planeheight[4]; |
| 48 | int linesize[4]; | 71 | int linesize[4]; |
| | 72 | |
| | 73 | enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new); |
| 49 | } PhaseContext; | 74 | } PhaseContext; |
| 50 | | 75 | |
| 51 | #define OFFSET(x) offsetof(PhaseContext, x) | 76 | #define OFFSET(x) offsetof(PhaseContext, x) |
| @@ -71,10 +96,33 @@ AVFILTER_DEFINE_CLASS(phase); · 19 unmodified lines |
| 71 | static int query_formats(AVFilterContext *ctx) | 96 | static int query_formats(AVFilterContext *ctx) |
| 72 | { | 97 | { |
| 73 | static const enum AVPixelFormat pix_fmts[] = { | 98 | static const enum AVPixelFormat pix_fmts[] = { |
| 74 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, | 99 | AV_PIX_FMT_GRAY8, |
| 75 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P, | 100 | AV_PIX_FMT_GRAY9, |
| 76 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, | 101 | AV_PIX_FMT_GRAY10, |
| 77 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE | 102 | AV_PIX_FMT_GRAY12, |
| | 103 | AV_PIX_FMT_GRAY14, |
| | 104 | AV_PIX_FMT_GRAY16, |
| | 105 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
| | 106 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
| | 107 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
| | 108 | AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
| | 109 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
| | 110 | AV_PIX_FMT_YUVJ411P, |
| | 111 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
| | 112 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
| | 113 | AV_PIX_FMT_YUV440P10, |
| | 114 | AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
| | 115 | AV_PIX_FMT_YUV440P12, |
| | 116 | AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
| | 117 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
| | 118 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
| | 119 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
| | 120 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
| | 121 | AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, |
| | 122 | AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, |
| | 123 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, |
| | 124 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
| | 125 | AV_PIX_FMT_NONE |
| 78 | }; | 126 | }; |
| 79 | | 127 | |
| 80 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); | 128 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); |
| @@ -89,6 +137,16 @@ static int config_input(AVFilterLink *inlink) · 8 unmodified lines |
| 89 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | 137 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
| 90 | int ret; | 138 | int ret; |
| 91 | | 139 | |
| | 140 | switch (desc->comp[0].depth) { |
| | 141 | case 8: s->analyze_plane = analyze_plane_8; break; |
| | 142 | case 9: s->analyze_plane = analyze_plane_9; break; |
| | 143 | case 10: s->analyze_plane = analyze_plane_10; break; |
| | 144 | case 12: s->analyze_plane = analyze_plane_12; break; |
| | 145 | case 14: s->analyze_plane = analyze_plane_14; break; |
| | 146 | case 16: s->analyze_plane = analyze_plane_16; break; |
| | 147 | default: av_assert0(0); |
| | 148 | }; |
| | 149 | |
| 92 | if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) | 150 | if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) |
| 93 | return ret; | 151 | return ret; |
| 94 | | 152 | |
| @@ -100,149 +158,6 @@ static int config_input(AVFilterLink *inlink) · 5 unmodified lines |
| 100 | return 0; | 158 | return 0; |
| 101 | } | 159 | } |
| 102 | | 160 | |
| 103 | /* | | |
| 104 | * This macro interpolates the value of both fields at a point halfway | | |
| 105 | * between lines and takes the squared difference. In field resolution | | |
| 106 | * the point is a quarter pixel below a line in one field and a quarter | | |
| 107 | * pixel above a line in other. | | |
| 108 | * | | |
| 109 | * (The result is actually multiplied by 25) | | |
| 110 | */ | | |
| 111 | #define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) << 2) + (a)[(as) << 1] - (b)[-(bs)], (t) * (t)) | | |
| 112 | | | |
| 113 | /* | | |
| 114 | * Find which field combination has the smallest average squared difference | | |
| 115 | * between the fields. | | |
| 116 | */ | | |
| 117 | static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new) | | |
| 118 | { | | |
| 119 | double bdiff, tdiff, pdiff; | | |
| 120 | | | |
| 121 | if (mode == AUTO) { | | |
| 122 | mode = new->interlaced_frame ? new->top_field_first ? | | |
| 123 | TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE; | | |
| 124 | } else if (mode == AUTO_ANALYZE) { | | |
| 125 | mode = new->interlaced_frame ? new->top_field_first ? | | |
| 126 | TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE; | | |
| 127 | } | | |
| 128 | | | |
| 129 | if (mode <= BOTTOM_FIRST) { | | |
| 130 | bdiff = pdiff = tdiff = 65536.0; | | |
| 131 | } else { | | |
| 132 | const int ns = new->linesize[0]; | | |
| 133 | const int os = old->linesize[0]; | | |
| 134 | const uint8_t *nptr = new->data[0]; | | |
| 135 | const uint8_t *optr = old->data[0]; | | |
| 136 | const int h = new->height; | | |
| 137 | const int w = new->width; | | |
| 138 | int bdif, tdif, pdif; | | |
| 139 | double scale; | | |
| 140 | | | |
| 141 | int top = 0, t; | | |
| 142 | const uint8_t *rend, *end = nptr + (h - 2) * ns; | | |
| 143 | | | |
| 144 | bdiff = pdiff = tdiff = 0.0; | | |
| 145 | | | |
| 146 | nptr += ns; | | |
| 147 | optr += os; | | |
| 148 | while (nptr < end) { | | |
| 149 | pdif = tdif = bdif = 0; | | |
| 150 | | | |
| 151 | switch (mode) { | | |
| 152 | case TOP_FIRST_ANALYZE: | | |
| 153 | if (top) { | | |
| 154 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 155 | pdif += DIFF(nptr, ns, nptr, ns); | | |
| 156 | tdif += DIFF(nptr, ns, optr, os); | | |
| 157 | } | | |
| 158 | } else { | | |
| 159 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 160 | pdif += DIFF(nptr, ns, nptr, ns); | | |
| 161 | tdif += DIFF(optr, os, nptr, ns); | | |
| 162 | } | | |
| 163 | } | | |
| 164 | break; | | |
| 165 | case BOTTOM_FIRST_ANALYZE: | | |
| 166 | if (top) { | | |
| 167 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 168 | pdif += DIFF(nptr, ns, nptr, ns); | | |
| 169 | bdif += DIFF(optr, os, nptr, ns); | | |
| 170 | } | | |
| 171 | } else { | | |
| 172 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 173 | pdif += DIFF(nptr, ns, nptr, ns); | | |
| 174 | bdif += DIFF(nptr, ns, optr, os); | | |
| 175 | } | | |
| 176 | } | | |
| 177 | break; | | |
| 178 | case ANALYZE: | | |
| 179 | if (top) { | | |
| 180 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 181 | tdif += DIFF(nptr, ns, optr, os); | | |
| 182 | bdif += DIFF(optr, os, nptr, ns); | | |
| 183 | } | | |
| 184 | } else { | | |
| 185 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 186 | bdif += DIFF(nptr, ns, optr, os); | | |
| 187 | tdif += DIFF(optr, os, nptr, ns); | | |
| 188 | } | | |
| 189 | } | | |
| 190 | break; | | |
| 191 | case FULL_ANALYZE: | | |
| 192 | if (top) { | | |
| 193 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 194 | pdif += DIFF(nptr, ns, nptr, ns); | | |
| 195 | tdif += DIFF(nptr, ns, optr, os); | | |
| 196 | bdif += DIFF(optr, os, nptr, ns); | | |
| 197 | } | | |
| 198 | } else { | | |
| 199 | for (rend = nptr + w; nptr < rend; nptr++, optr++) { | | |
| 200 | pdif += DIFF(nptr, ns, nptr, ns); | | |
| 201 | bdif += DIFF(nptr, ns, optr, os); | | |
| 202 | tdif += DIFF(optr, os, nptr, ns); | | |
| 203 | } | | |
| 204 | } | | |
| 205 | break; | | |
| 206 | default: | | |
| 207 | av_assert0(0); | | |
| 208 | } | | |
| 209 | | | |
| 210 | pdiff += (double)pdif; | | |
| 211 | tdiff += (double)tdif; | | |
| 212 | bdiff += (double)bdif; | | |
| 213 | nptr += ns - w; | | |
| 214 | optr += os - w; | | |
| 215 | top ^= 1; | | |
| 216 | } | | |
| 217 | | | |
| 218 | scale = 1.0 / (w * (h - 3)) / 25.0; | | |
| 219 | pdiff *= scale; | | |
| 220 | tdiff *= scale; | | |
| 221 | bdiff *= scale; | | |
| 222 | | | |
| 223 | if (mode == TOP_FIRST_ANALYZE) { | | |
| 224 | bdiff = 65536.0; | | |
| 225 | } else if (mode == BOTTOM_FIRST_ANALYZE) { | | |
| 226 | tdiff = 65536.0; | | |
| 227 | } else if (mode == ANALYZE) { | | |
| 228 | pdiff = 65536.0; | | |
| 229 | } | | |
| 230 | | | |
| 231 | if (bdiff < pdiff && bdiff < tdiff) { | | |
| 232 | mode = BOTTOM_FIRST; | | |
| 233 | } else if (tdiff < pdiff && tdiff < bdiff) { | | |
| 234 | mode = TOP_FIRST; | | |
| 235 | } else { | | |
| 236 | mode = PROGRESSIVE; | | |
| 237 | } | | |
| 238 | } | | |
| 239 | | | |
| 240 | av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n", | | |
| 241 | mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p', | | |
| 242 | tdiff, bdiff, pdiff); | | |
| 243 | return mode; | | |
| 244 | } | | |
| 245 | | | |
| 246 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | 161 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| 247 | { | 162 | { |
| 248 | AVFilterContext *ctx = inlink->dst; | 163 | AVFilterContext *ctx = inlink->dst; |
| @@ -272,7 +187,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 23 unmodified lines |
| 272 | s->frame = in; | 187 | s->frame = in; |
| 273 | mode = PROGRESSIVE; | 188 | mode = PROGRESSIVE; |
| 274 | } else { | 189 | } else { |
| 275 | mode = analyze_plane(ctx, s->mode, s->frame, in); | 190 | mode = s->analyze_plane(ctx, s->mode, s->frame, in); |
| 276 | } | 191 | } |
| 277 | | 192 | |
| 278 | for (plane = 0; plane < s->nb_planes; plane++) { | 193 | for (plane = 0; plane < s->nb_planes; plane++) { |