... | ... |
@@ -2383,6 +2383,46 @@ curves=vintage |
2383 | 2383 |
@end example |
2384 | 2384 |
@end itemize |
2385 | 2385 |
|
2386 |
+@anchor{decimate} |
|
2387 |
+@section decimate |
|
2388 |
+ |
|
2389 |
+Drop duplicated frames at regular intervals. |
|
2390 |
+ |
|
2391 |
+The filter accepts the following options: |
|
2392 |
+ |
|
2393 |
+@table @option |
|
2394 |
+@item cycle |
|
2395 |
+Set the number of frames from which one will be dropped. Setting this to |
|
2396 |
+@var{N} means one frame in every batch of @var{N} frames will be dropped. |
|
2397 |
+Default is @code{5}. |
|
2398 |
+ |
|
2399 |
+@item dupthresh |
|
2400 |
+Set the threshold for duplicate detection. If the difference metric for a frame |
|
2401 |
+is less than or equal to this value, then it is declared as duplicate. Default |
|
2402 |
+is @code{1.1} |
|
2403 |
+ |
|
2404 |
+@item scthresh |
|
2405 |
+Set scene change threshold. Default is @code{15}. |
|
2406 |
+ |
|
2407 |
+@item blockx |
|
2408 |
+@item blocky |
|
2409 |
+Set the size of the x and y-axis blocks used during metric calculations. |
|
2410 |
+Larger blocks give better noise suppression, but also give worse detection of |
|
2411 |
+small movements. Must be a power of two. Default is @code{32}. |
|
2412 |
+ |
|
2413 |
+@item ppsrc |
|
2414 |
+Mark main input as a pre-processed input and activate clean source input |
|
2415 |
+stream. This allows the input to be pre-processed with various filters to help |
|
2416 |
+the metrics calculation while keeping the frame selection lossless. When set to |
|
2417 |
+@code{1}, the first stream is for the pre-processed input, and the second |
|
2418 |
+stream is the clean source from where the kept frames are chosen. Default is |
|
2419 |
+@code{0}. |
|
2420 |
+ |
|
2421 |
+@item chroma |
|
2422 |
+Set whether or not chroma is considered in the metric calculations. Default is |
|
2423 |
+@code{1}. |
|
2424 |
+@end table |
|
2425 |
+ |
|
2386 | 2426 |
@section mpdecimate |
2387 | 2427 |
|
2388 | 2428 |
Drop frames that do not differ greatly from the previous frame in |
... | ... |
@@ -3047,6 +3087,328 @@ Specify whether to extract the top (if the value is @code{0} or |
3047 | 3047 |
@code{bottom}). |
3048 | 3048 |
@end table |
3049 | 3049 |
|
3050 |
+@section fieldmatch |
|
3051 |
+ |
|
3052 |
+Field matching filter for inverse telecine. It is meant to reconstruct the |
|
3053 |
+progressive frames from a telecined stream. The filter does not drop duplicated |
|
3054 |
+frames, so to achieve a complete inverse telecine @code{fieldmatch} needs to be |
|
3055 |
+followed by a decimation filter such as @ref{decimate} in the filtergraph. |
|
3056 |
+ |
|
3057 |
+The separation of the field matching and the decimation is notably motivated by |
|
3058 |
+the possibility of inserting a de-interlacing filter fallback between the two. |
|
3059 |
+If the source has mixed telecined and real interlaced content, |
|
3060 |
+@code{fieldmatch} will not be able to match fields for the interlaced parts. |
|
3061 |
+But these remaining combed frames will be marked as interlaced, and thus can be |
|
3062 |
+de-interlaced by a later filter such as @ref{yadif} before decimation. |
|
3063 |
+ |
|
3064 |
+In addition to the various configuration options, @code{fieldmatch} can take an |
|
3065 |
+optional second stream, activated through the @option{ppsrc} option. If |
|
3066 |
+enabled, the frames reconstruction will be based on the fields and frames from |
|
3067 |
+this second stream. This allows the first input to be pre-processed in order to |
|
3068 |
+help the various algorithms of the filter, while keeping the output lossless |
|
3069 |
+(assuming the fields are matched properly). Typically, a field-aware denoiser, |
|
3070 |
+or brightness/contrast adjustments can help. |
|
3071 |
+ |
|
3072 |
+Note that this filter uses the same algorithms as TIVTC/TFM (AviSynth project) |
|
3073 |
+and VIVTC/VFM (VapourSynth project). The later is a light clone of TFM from |
|
3074 |
+which @code{fieldmatch} is based on. While the semantic and usage are very |
|
3075 |
+close, some behaviour and options names can differ. |
|
3076 |
+ |
|
3077 |
+The filter accepts the following options: |
|
3078 |
+ |
|
3079 |
+@table @option |
|
3080 |
+@item order |
|
3081 |
+Specify the assumed field order of the input stream. Available values are: |
|
3082 |
+ |
|
3083 |
+@table @samp |
|
3084 |
+@item auto |
|
3085 |
+Auto detect parity (use FFmpeg's internal parity value). |
|
3086 |
+@item bff |
|
3087 |
+Assume bottom field first. |
|
3088 |
+@item tff |
|
3089 |
+Assume top field first. |
|
3090 |
+@end table |
|
3091 |
+ |
|
3092 |
+Note that it is sometimes recommended not to trust the parity announced by the |
|
3093 |
+stream. |
|
3094 |
+ |
|
3095 |
+Default value is @var{auto}. |
|
3096 |
+ |
|
3097 |
+@item mode |
|
3098 |
+Set the matching mode or strategy to use. @option{pc} mode is the safest in the |
|
3099 |
+sense that it wont risk creating jerkiness due to duplicate frames when |
|
3100 |
+possible, but if there are bad edits or blended fields it will end up |
|
3101 |
+outputting combed frames when a good match might actually exist. On the other |
|
3102 |
+hand, @option{pcn_ub} mode is the most risky in terms of creating jerkiness, |
|
3103 |
+but will almost always find a good frame if there is one. The other values are |
|
3104 |
+all somewhere in between @option{pc} and @option{pcn_ub} in terms of risking |
|
3105 |
+jerkiness and creating duplicate frames versus finding good matches in sections |
|
3106 |
+with bad edits, orphaned fields, blended fields, etc. |
|
3107 |
+ |
|
3108 |
+More details about p/c/n/u/b are available in @ref{p/c/n/u/b meaning} section. |
|
3109 |
+ |
|
3110 |
+Available values are: |
|
3111 |
+ |
|
3112 |
+@table @samp |
|
3113 |
+@item pc |
|
3114 |
+2-way matching (p/c) |
|
3115 |
+@item pc_n |
|
3116 |
+2-way matching, and trying 3rd match if still combed (p/c + n) |
|
3117 |
+@item pc_u |
|
3118 |
+2-way matching, and trying 3rd match (same order) if still combed (p/c + u) |
|
3119 |
+@item pc_n_ub |
|
3120 |
+2-way matching, trying 3rd match if still combed, and trying 4th/5th matches if |
|
3121 |
+still combed (p/c + n + u/b) |
|
3122 |
+@item pcn |
|
3123 |
+3-way matching (p/c/n) |
|
3124 |
+@item pcn_ub |
|
3125 |
+3-way matching, and trying 4th/5th matches if all 3 of the original matches are |
|
3126 |
+detected as combed (p/c/n + u/b) |
|
3127 |
+@end table |
|
3128 |
+ |
|
3129 |
+The parenthesis at the end indicate the matches that would be used for that |
|
3130 |
+mode assuming @option{order}=@var{tff} (and @option{field} on @var{auto} or |
|
3131 |
+@var{top}). |
|
3132 |
+ |
|
3133 |
+In terms of speed @option{pc} mode is by far the fastest and @option{pcn_ub} is |
|
3134 |
+the slowest. |
|
3135 |
+ |
|
3136 |
+Default value is @var{pc_n}. |
|
3137 |
+ |
|
3138 |
+@item ppsrc |
|
3139 |
+Mark the main input stream as a pre-processed input, and enable the secondary |
|
3140 |
+input stream as the clean source to pick the fields from. See the filter |
|
3141 |
+introduction for more details. It is similar to the @option{clip2} feature from |
|
3142 |
+VFM/TFM. |
|
3143 |
+ |
|
3144 |
+Default value is @code{0} (disabled). |
|
3145 |
+ |
|
3146 |
+@item field |
|
3147 |
+Set the field to match from. It is recommended to set this to the same value as |
|
3148 |
+@option{order} unless you experience matching failures with that setting. In |
|
3149 |
+certain circumstances changing the field that is used to match from can have a |
|
3150 |
+large impact on matching performance. Available values are: |
|
3151 |
+ |
|
3152 |
+@table @samp |
|
3153 |
+@item auto |
|
3154 |
+Automatic (same value as @option{order}). |
|
3155 |
+@item bottom |
|
3156 |
+Match from the bottom field. |
|
3157 |
+@item top |
|
3158 |
+Match from the top field. |
|
3159 |
+@end table |
|
3160 |
+ |
|
3161 |
+Default value is @var{auto}. |
|
3162 |
+ |
|
3163 |
+@item mchroma |
|
3164 |
+Set whether or not chroma is included during the match comparisons. In most |
|
3165 |
+cases it is recommended to leave this enabled. You should set this to @code{0} |
|
3166 |
+only if your clip has bad chroma problems such as heavy rainbowing or other |
|
3167 |
+artifacts. Setting this to @code{0} could also be used to speed things up at |
|
3168 |
+the cost of some accuracy. |
|
3169 |
+ |
|
3170 |
+Default value is @code{1}. |
|
3171 |
+ |
|
3172 |
+@item y0 |
|
3173 |
+@item y1 |
|
3174 |
+These define an exclusion band which excludes the lines between @option{y0} and |
|
3175 |
+@option{y1} from being included in the field matching decision. An exclusion |
|
3176 |
+band can be used to ignore subtitles, a logo, or other things that may |
|
3177 |
+interfere with the matching. @option{y0} sets the starting scan line and |
|
3178 |
+@option{y1} sets the ending line; all lines in between @option{y0} and |
|
3179 |
+@option{y1} (including @option{y0} and @option{y1}) will be ignored. Setting |
|
3180 |
+@option{y0} and @option{y1} to the same value will disable the feature. |
|
3181 |
+@option{y0} and @option{y1} defaults to @code{0}. |
|
3182 |
+ |
|
3183 |
+@item scthresh |
|
3184 |
+Set the scene change detection threshold as a percentage of maximum change on |
|
3185 |
+the luma plane. Good values are in the @code{[8.0, 14.0]} range. Scene change |
|
3186 |
+detection is only relevant in case @option{combmatch}=@var{sc}. The range for |
|
3187 |
+@option{scthresh} is @code{[0.0, 100.0]}. |
|
3188 |
+ |
|
3189 |
+Default value is @code{12.0}. |
|
3190 |
+ |
|
3191 |
+@item combmatch |
|
3192 |
+When @option{combatch} is not @var{none}, @code{fieldmatch} will take into |
|
3193 |
+account the combed scores of matches when deciding what match to use as the |
|
3194 |
+final match. Available values are: |
|
3195 |
+ |
|
3196 |
+@table @samp |
|
3197 |
+@item none |
|
3198 |
+No final matching based on combed scores. |
|
3199 |
+@item sc |
|
3200 |
+Combed scores are only used when a scene change is detected. |
|
3201 |
+@item full |
|
3202 |
+Use combed scores all the time. |
|
3203 |
+@end table |
|
3204 |
+ |
|
3205 |
+Default is @var{sc}. |
|
3206 |
+ |
|
3207 |
+@item combdbg |
|
3208 |
+Force @code{fieldmatch} to calculate the combed metrics for certain matches and |
|
3209 |
+print them. This setting is known as @option{micout} in TFM/VFM vocabulary. |
|
3210 |
+Available values are: |
|
3211 |
+ |
|
3212 |
+@table @samp |
|
3213 |
+@item none |
|
3214 |
+No forced calculation. |
|
3215 |
+@item pcn |
|
3216 |
+Force p/c/n calculations. |
|
3217 |
+@item pcnub |
|
3218 |
+Force p/c/n/u/b calculations. |
|
3219 |
+@end table |
|
3220 |
+ |
|
3221 |
+Default value is @var{none}. |
|
3222 |
+ |
|
3223 |
+@item cthresh |
|
3224 |
+This is the area combing threshold used for combed frame detection. This |
|
3225 |
+essentially controls how "strong" or "visible" combing must be to be detected. |
|
3226 |
+Larger values mean combing must be more visible and smaller values mean combing |
|
3227 |
+can be less visible or strong and still be detected. Valid settings are from |
|
3228 |
+@code{-1} (every pixel will be detected as combed) to @code{255} (no pixel will |
|
3229 |
+be detected as combed). This is basically a pixel difference value. A good |
|
3230 |
+range is @code{[8, 12]}. |
|
3231 |
+ |
|
3232 |
+Default value is @code{9}. |
|
3233 |
+ |
|
3234 |
+@item chroma |
|
3235 |
+Sets whether or not chroma is considered in the combed frame decision. Only |
|
3236 |
+disable this if your source has chroma problems (rainbowing, etc.) that are |
|
3237 |
+causing problems for the combed frame detection with chroma enabled. Actually, |
|
3238 |
+using @option{chroma}=@var{0} is usually more reliable, except for the case |
|
3239 |
+where there is chroma only combing in the source. |
|
3240 |
+ |
|
3241 |
+Default value is @code{0}. |
|
3242 |
+ |
|
3243 |
+@item blockx |
|
3244 |
+@item blocky |
|
3245 |
+Respectively set the x-axis and y-axis size of the window used during combed |
|
3246 |
+frame detection. This has to do with the size of the area in which |
|
3247 |
+@option{combpel} pixels are required to be detected as combed for a frame to be |
|
3248 |
+declared combed. See the @option{combpel} parameter description for more info. |
|
3249 |
+Possible values are any number that is a power of 2 starting at 4 and going up |
|
3250 |
+to 512. |
|
3251 |
+ |
|
3252 |
+Default value is @code{16}. |
|
3253 |
+ |
|
3254 |
+@item combpel |
|
3255 |
+The number of combed pixels inside any of the @option{blocky} by |
|
3256 |
+@option{blockx} size blocks on the frame for the frame to be detected as |
|
3257 |
+combed. While @option{cthresh} controls how "visible" the combing must be, this |
|
3258 |
+setting controls "how much" combing there must be in any localized area (a |
|
3259 |
+window defined by the @option{blockx} and @option{blocky} settings) on the |
|
3260 |
+frame. Minimum value is @code{0} and maximum is @code{blocky x blockx} (at |
|
3261 |
+which point no frames will ever be detected as combed). This setting is known |
|
3262 |
+as @option{MI} in TFM/VFM vocabulary. |
|
3263 |
+ |
|
3264 |
+Default value is @code{80}. |
|
3265 |
+@end table |
|
3266 |
+ |
|
3267 |
+@anchor{p/c/n/u/b meaning} |
|
3268 |
+@subsection p/c/n/u/b meaning |
|
3269 |
+ |
|
3270 |
+@subsubsection p/c/n |
|
3271 |
+ |
|
3272 |
+We assume the following telecined stream: |
|
3273 |
+ |
|
3274 |
+@example |
|
3275 |
+Top fields: 1 2 2 3 4 |
|
3276 |
+Bottom fields: 1 2 3 4 4 |
|
3277 |
+@end example |
|
3278 |
+ |
|
3279 |
+The numbers correspond to the progressive frame the fields relate to. Here, the |
|
3280 |
+first two frames are progressive, the 3rd and 4th are combed, and so on. |
|
3281 |
+ |
|
3282 |
+When @code{fieldmatch} is configured to run a matching from bottom |
|
3283 |
+(@option{field}=@var{bottom}) this is how this input stream get transformed: |
|
3284 |
+ |
|
3285 |
+@example |
|
3286 |
+Input stream: |
|
3287 |
+ T 1 2 2 3 4 |
|
3288 |
+ B 1 2 3 4 4 <-- matching reference |
|
3289 |
+ |
|
3290 |
+Matches: c c n n c |
|
3291 |
+ |
|
3292 |
+Output stream: |
|
3293 |
+ T 1 2 3 4 4 |
|
3294 |
+ B 1 2 3 4 4 |
|
3295 |
+@end example |
|
3296 |
+ |
|
3297 |
+As a result of the field matching, we can see that some frames get duplicated. |
|
3298 |
+To perform a complete inverse telecine, you need to rely on a decimation filter |
|
3299 |
+after this operation. See for instance the @ref{decimate} filter. |
|
3300 |
+ |
|
3301 |
+The same operation now matching from top fields (@option{field}=@var{top}) |
|
3302 |
+looks like this: |
|
3303 |
+ |
|
3304 |
+@example |
|
3305 |
+Input stream: |
|
3306 |
+ T 1 2 2 3 4 <-- matching reference |
|
3307 |
+ B 1 2 3 4 4 |
|
3308 |
+ |
|
3309 |
+Matches: c c p p c |
|
3310 |
+ |
|
3311 |
+Output stream: |
|
3312 |
+ T 1 2 2 3 4 |
|
3313 |
+ B 1 2 2 3 4 |
|
3314 |
+@end example |
|
3315 |
+ |
|
3316 |
+In these examples, we can see what @var{p}, @var{c} and @var{n} mean; |
|
3317 |
+basically, they refer to the frame and field of the opposite parity: |
|
3318 |
+ |
|
3319 |
+@itemize |
|
3320 |
+@item @var{p} matches the field of the opposite parity in the previous frame |
|
3321 |
+@item @var{c} matches the field of the opposite parity in the current frame |
|
3322 |
+@item @var{n} matches the field of the opposite parity in the next frame |
|
3323 |
+@end itemize |
|
3324 |
+ |
|
3325 |
+@subsubsection u/b |
|
3326 |
+ |
|
3327 |
+The @var{u} and @var{b} matching are a bit special in the sense that they match |
|
3328 |
+from the opposite parity flag. In the following examples, we assume that we are |
|
3329 |
+currently matching the 2nd frame (Top:2, bottom:2). According to the match, a |
|
3330 |
+'x' is placed above and below each matched fields. |
|
3331 |
+ |
|
3332 |
+With bottom matching (@option{field}=@var{bottom}): |
|
3333 |
+@example |
|
3334 |
+Match: c p n b u |
|
3335 |
+ |
|
3336 |
+ x x x x x |
|
3337 |
+ Top 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 |
|
3338 |
+ Bottom 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 |
|
3339 |
+ x x x x x |
|
3340 |
+ |
|
3341 |
+Output frames: |
|
3342 |
+ 2 1 2 2 2 |
|
3343 |
+ 2 2 2 1 3 |
|
3344 |
+@end example |
|
3345 |
+ |
|
3346 |
+With top matching (@option{field}=@var{top}): |
|
3347 |
+@example |
|
3348 |
+Match: c p n b u |
|
3349 |
+ |
|
3350 |
+ x x x x x |
|
3351 |
+ Top 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 |
|
3352 |
+ Bottom 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 |
|
3353 |
+ x x x x x |
|
3354 |
+ |
|
3355 |
+Output frames: |
|
3356 |
+ 2 2 2 1 2 |
|
3357 |
+ 2 1 3 2 2 |
|
3358 |
+@end example |
|
3359 |
+ |
|
3360 |
+@subsection Examples |
|
3361 |
+ |
|
3362 |
+Simple IVTC of a top field first telecined stream: |
|
3363 |
+@example |
|
3364 |
+fieldmatch=order=tff:combmatch=none, decimate |
|
3365 |
+@end example |
|
3366 |
+ |
|
3367 |
+Advanced IVTC, with fallback on @ref{yadif} for still combed frames: |
|
3368 |
+@example |
|
3369 |
+fieldmatch=order=tff:combmatch=full, yadif=deint=interlaced, decimate |
|
3370 |
+@end example |
|
3371 |
+ |
|
3050 | 3372 |
@section fieldorder |
3051 | 3373 |
|
3052 | 3374 |
Transform the field order of the input video. |
... | ... |
@@ -5670,6 +6032,7 @@ Flip the input video vertically. |
5670 | 5670 |
ffmpeg -i in.avi -vf "vflip" out.avi |
5671 | 5671 |
@end example |
5672 | 5672 |
|
5673 |
+@anchor{yadif} |
|
5673 | 5674 |
@section yadif |
5674 | 5675 |
|
5675 | 5676 |
Deinterlace the input video ("yadif" means "yet another deinterlacing |
... | ... |
@@ -109,6 +109,7 @@ OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o |
109 | 109 |
OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o |
110 | 110 |
OBJS-$(CONFIG_CROPDETECT_FILTER) += vf_cropdetect.o |
111 | 111 |
OBJS-$(CONFIG_CURVES_FILTER) += vf_curves.o |
112 |
+OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o |
|
112 | 113 |
OBJS-$(CONFIG_DELOGO_FILTER) += vf_delogo.o |
113 | 114 |
OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o |
114 | 115 |
OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o |
... | ... |
@@ -116,6 +117,7 @@ OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o |
116 | 116 |
OBJS-$(CONFIG_EDGEDETECT_FILTER) += vf_edgedetect.o |
117 | 117 |
OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o |
118 | 118 |
OBJS-$(CONFIG_FIELD_FILTER) += vf_field.o |
119 |
+OBJS-$(CONFIG_FIELDMATCH_FILTER) += vf_fieldmatch.o |
|
119 | 120 |
OBJS-$(CONFIG_FIELDORDER_FILTER) += vf_fieldorder.o |
120 | 121 |
OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o |
121 | 122 |
OBJS-$(CONFIG_FRAMESTEP_FILTER) += vf_framestep.o |
... | ... |
@@ -107,6 +107,7 @@ void avfilter_register_all(void) |
107 | 107 |
REGISTER_FILTER(CROP, crop, vf); |
108 | 108 |
REGISTER_FILTER(CROPDETECT, cropdetect, vf); |
109 | 109 |
REGISTER_FILTER(CURVES, curves, vf); |
110 |
+ REGISTER_FILTER(DECIMATE, decimate, vf); |
|
110 | 111 |
REGISTER_FILTER(DELOGO, delogo, vf); |
111 | 112 |
REGISTER_FILTER(DESHAKE, deshake, vf); |
112 | 113 |
REGISTER_FILTER(DRAWBOX, drawbox, vf); |
... | ... |
@@ -114,6 +115,7 @@ void avfilter_register_all(void) |
114 | 114 |
REGISTER_FILTER(EDGEDETECT, edgedetect, vf); |
115 | 115 |
REGISTER_FILTER(FADE, fade, vf); |
116 | 116 |
REGISTER_FILTER(FIELD, field, vf); |
117 |
+ REGISTER_FILTER(FIELDMATCH, fieldmatch, vf); |
|
117 | 118 |
REGISTER_FILTER(FIELDORDER, fieldorder, vf); |
118 | 119 |
REGISTER_FILTER(FORMAT, format, vf); |
119 | 120 |
REGISTER_FILTER(FPS, fps, vf); |
... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
#include "libavutil/avutil.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
32 |
-#define LIBAVFILTER_VERSION_MINOR 55 |
|
32 |
+#define LIBAVFILTER_VERSION_MINOR 56 |
|
33 | 33 |
#define LIBAVFILTER_VERSION_MICRO 100 |
34 | 34 |
|
35 | 35 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
36 | 36 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,398 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Fredrik Mellbin |
|
2 |
+ * Copyright (c) 2013 Clément Bœsch |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include "libavutil/opt.h" |
|
22 |
+#include "libavutil/pixdesc.h" |
|
23 |
+#include "libavutil/timestamp.h" |
|
24 |
+#include "avfilter.h" |
|
25 |
+#include "internal.h" |
|
26 |
+ |
|
27 |
+#define INPUT_MAIN 0 |
|
28 |
+#define INPUT_CLEANSRC 1 |
|
29 |
+ |
|
30 |
+struct qitem { |
|
31 |
+ AVFrame *frame; |
|
32 |
+ int64_t maxbdiff; |
|
33 |
+ int64_t totdiff; |
|
34 |
+}; |
|
35 |
+ |
|
36 |
+typedef struct { |
|
37 |
+ const AVClass *class; |
|
38 |
+ struct qitem *queue; ///< window of cycle frames and the associated data diff |
|
39 |
+ int fid; ///< current frame id in the queue |
|
40 |
+ int filled; ///< 1 if the queue is filled, 0 otherwise |
|
41 |
+ AVFrame *last; ///< last frame from the previous queue |
|
42 |
+ int64_t frame_count; ///< output frame counter |
|
43 |
+ AVFrame **clean_src; ///< frame queue for the clean source |
|
44 |
+ int got_frame[2]; ///< frame request flag for each input stream |
|
45 |
+ double ts_unit; ///< timestamp units for the output frames |
|
46 |
+ uint32_t eof; ///< bitmask for end of stream |
|
47 |
+ int hsub, vsub; ///< chroma subsampling values |
|
48 |
+ int depth; |
|
49 |
+ int nxblocks, nyblocks; |
|
50 |
+ int bdiffsize; |
|
51 |
+ int64_t *bdiffs; |
|
52 |
+ |
|
53 |
+ /* options */ |
|
54 |
+ int cycle; |
|
55 |
+ double dupthresh_flt; |
|
56 |
+ double scthresh_flt; |
|
57 |
+ int64_t dupthresh; |
|
58 |
+ int64_t scthresh; |
|
59 |
+ int blockx, blocky; |
|
60 |
+ int ppsrc; |
|
61 |
+ int chroma; |
|
62 |
+} DecimateContext; |
|
63 |
+ |
|
64 |
+#define OFFSET(x) offsetof(DecimateContext, x) |
|
65 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
66 |
+ |
|
67 |
+static const AVOption decimate_options[] = { |
|
68 |
+ { "cycle", "set the number of frame from which one will be dropped", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS }, |
|
69 |
+ { "dupthresh", "set duplicate threshold", OFFSET(dupthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl = 1.1}, 0, 100, FLAGS }, |
|
70 |
+ { "scthresh", "set scene change threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl = 15.0}, 0, 100, FLAGS }, |
|
71 |
+ { "blockx", "set the size of the x-axis blocks used during metric calculations", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS }, |
|
72 |
+ { "blocky", "set the size of the y-axis blocks used during metric calculations", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS }, |
|
73 |
+ { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS }, |
|
74 |
+ { "chroma", "set whether or not chroma is considered in the metric calculations", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS }, |
|
75 |
+ { NULL } |
|
76 |
+}; |
|
77 |
+ |
|
78 |
+AVFILTER_DEFINE_CLASS(decimate); |
|
79 |
+ |
|
80 |
+static void calc_diffs(const DecimateContext *dm, struct qitem *q, |
|
81 |
+ const AVFrame *f1, const AVFrame *f2) |
|
82 |
+{ |
|
83 |
+ int64_t maxdiff = -1; |
|
84 |
+ int64_t *bdiffs = dm->bdiffs; |
|
85 |
+ int plane, i, j; |
|
86 |
+ |
|
87 |
+ memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs)); |
|
88 |
+ |
|
89 |
+ for (plane = 0; plane < (dm->chroma ? 3 : 1); plane++) { |
|
90 |
+ int x, y, xl; |
|
91 |
+ const int linesize1 = f1->linesize[plane]; |
|
92 |
+ const int linesize2 = f2->linesize[plane]; |
|
93 |
+ const uint8_t *f1p = f1->data[plane]; |
|
94 |
+ const uint8_t *f2p = f2->data[plane]; |
|
95 |
+ int width = plane ? f1->width >> dm->hsub : f1->width; |
|
96 |
+ int height = plane ? f1->height >> dm->vsub : f1->height; |
|
97 |
+ int hblockx = dm->blockx / 2; |
|
98 |
+ int hblocky = dm->blocky / 2; |
|
99 |
+ |
|
100 |
+ if (plane) { |
|
101 |
+ hblockx >>= dm->hsub; |
|
102 |
+ hblocky >>= dm->vsub; |
|
103 |
+ } |
|
104 |
+ |
|
105 |
+ for (y = 0; y < height; y++) { |
|
106 |
+ int ydest = y / hblocky; |
|
107 |
+ int xdest = 0; |
|
108 |
+ |
|
109 |
+#define CALC_DIFF(nbits) do { \ |
|
110 |
+ for (x = 0; x < width; x += hblockx) { \ |
|
111 |
+ int64_t acc = 0; \ |
|
112 |
+ int m = FFMIN(width, x + hblockx); \ |
|
113 |
+ for (xl = x; xl < m; xl++) \ |
|
114 |
+ acc += abs(((const uint##nbits##_t *)f1p)[xl] - \ |
|
115 |
+ ((const uint##nbits##_t *)f2p)[xl]); \ |
|
116 |
+ bdiffs[ydest * dm->nxblocks + xdest] += acc; \ |
|
117 |
+ xdest++; \ |
|
118 |
+ } \ |
|
119 |
+} while (0) |
|
120 |
+ if (dm->depth == 8) CALC_DIFF(8); |
|
121 |
+ else CALC_DIFF(16); |
|
122 |
+ |
|
123 |
+ f1p += linesize1; |
|
124 |
+ f2p += linesize2; |
|
125 |
+ } |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ for (i = 0; i < dm->nyblocks - 1; i++) { |
|
129 |
+ for (j = 0; j < dm->nxblocks - 1; j++) { |
|
130 |
+ int64_t tmp = bdiffs[ i * dm->nxblocks + j ] |
|
131 |
+ + bdiffs[ i * dm->nxblocks + j + 1] |
|
132 |
+ + bdiffs[(i + 1) * dm->nxblocks + j ] |
|
133 |
+ + bdiffs[(i + 1) * dm->nxblocks + j + 1]; |
|
134 |
+ if (tmp > maxdiff) |
|
135 |
+ maxdiff = tmp; |
|
136 |
+ } |
|
137 |
+ } |
|
138 |
+ |
|
139 |
+ q->totdiff = 0; |
|
140 |
+ for (i = 0; i < dm->bdiffsize; i++) |
|
141 |
+ q->totdiff += bdiffs[i]; |
|
142 |
+ q->maxbdiff = maxdiff; |
|
143 |
+} |
|
144 |
+ |
|
145 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
146 |
+{ |
|
147 |
+ int scpos = -1, duppos = -1; |
|
148 |
+ int drop = INT_MIN, i, lowest = 0, ret; |
|
149 |
+ AVFilterContext *ctx = inlink->dst; |
|
150 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
151 |
+ DecimateContext *dm = ctx->priv; |
|
152 |
+ AVFrame *prv; |
|
153 |
+ |
|
154 |
+ /* update frames queue(s) */ |
|
155 |
+ if (FF_INLINK_IDX(inlink) == INPUT_MAIN) { |
|
156 |
+ dm->queue[dm->fid].frame = in; |
|
157 |
+ dm->got_frame[INPUT_MAIN] = 1; |
|
158 |
+ } else { |
|
159 |
+ dm->clean_src[dm->fid] = in; |
|
160 |
+ dm->got_frame[INPUT_CLEANSRC] = 1; |
|
161 |
+ } |
|
162 |
+ if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC])) |
|
163 |
+ return 0; |
|
164 |
+ dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0; |
|
165 |
+ |
|
166 |
+ if (in) { |
|
167 |
+ /* update frame metrics */ |
|
168 |
+ prv = dm->fid ? dm->queue[dm->fid - 1].frame : dm->last; |
|
169 |
+ if (!prv) |
|
170 |
+ prv = in; |
|
171 |
+ calc_diffs(dm, &dm->queue[dm->fid], prv, in); |
|
172 |
+ if (++dm->fid != dm->cycle) |
|
173 |
+ return 0; |
|
174 |
+ av_frame_free(&dm->last); |
|
175 |
+ dm->last = av_frame_clone(in); |
|
176 |
+ dm->fid = 0; |
|
177 |
+ |
|
178 |
+ /* we have a complete cycle, select the frame to drop */ |
|
179 |
+ lowest = 0; |
|
180 |
+ for (i = 0; i < dm->cycle; i++) { |
|
181 |
+ if (dm->queue[i].totdiff > dm->scthresh) |
|
182 |
+ scpos = i; |
|
183 |
+ if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff) |
|
184 |
+ lowest = i; |
|
185 |
+ } |
|
186 |
+ if (dm->queue[lowest].maxbdiff < dm->dupthresh) |
|
187 |
+ duppos = lowest; |
|
188 |
+ drop = scpos >= 0 && duppos < 0 ? scpos : lowest; |
|
189 |
+ } |
|
190 |
+ |
|
191 |
+ /* metrics debug */ |
|
192 |
+ if (av_log_get_level() >= AV_LOG_DEBUG) { |
|
193 |
+ av_log(ctx, AV_LOG_DEBUG, "1/%d frame drop:\n", dm->cycle); |
|
194 |
+ for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) { |
|
195 |
+ av_log(ctx, AV_LOG_DEBUG," #%d: totdiff=%08"PRIx64" maxbdiff=%08"PRIx64"%s%s%s%s\n", |
|
196 |
+ i + 1, dm->queue[i].totdiff, dm->queue[i].maxbdiff, |
|
197 |
+ i == scpos ? " sc" : "", |
|
198 |
+ i == duppos ? " dup" : "", |
|
199 |
+ i == lowest ? " lowest" : "", |
|
200 |
+ i == drop ? " [DROP]" : ""); |
|
201 |
+ } |
|
202 |
+ } |
|
203 |
+ |
|
204 |
+ /* push all frames except the drop */ |
|
205 |
+ ret = 0; |
|
206 |
+ for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) { |
|
207 |
+ if (i == drop) { |
|
208 |
+ if (dm->ppsrc) |
|
209 |
+ av_frame_free(&dm->clean_src[i]); |
|
210 |
+ av_frame_free(&dm->queue[i].frame); |
|
211 |
+ } else { |
|
212 |
+ AVFrame *frame = dm->queue[i].frame; |
|
213 |
+ if (dm->ppsrc) { |
|
214 |
+ av_frame_free(&frame); |
|
215 |
+ frame = dm->clean_src[i]; |
|
216 |
+ } |
|
217 |
+ frame->pts = dm->frame_count++ * dm->ts_unit; |
|
218 |
+ ret = ff_filter_frame(outlink, frame); |
|
219 |
+ if (ret < 0) |
|
220 |
+ break; |
|
221 |
+ } |
|
222 |
+ } |
|
223 |
+ |
|
224 |
+ return ret; |
|
225 |
+} |
|
226 |
+ |
|
227 |
+static int config_input(AVFilterLink *inlink) |
|
228 |
+{ |
|
229 |
+ int max_value; |
|
230 |
+ AVFilterContext *ctx = inlink->dst; |
|
231 |
+ DecimateContext *dm = ctx->priv; |
|
232 |
+ const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); |
|
233 |
+ const int w = inlink->w; |
|
234 |
+ const int h = inlink->h; |
|
235 |
+ |
|
236 |
+ dm->hsub = pix_desc->log2_chroma_w; |
|
237 |
+ dm->vsub = pix_desc->log2_chroma_h; |
|
238 |
+ dm->depth = pix_desc->comp[0].depth_minus1 + 1; |
|
239 |
+ max_value = (1 << dm->depth) - 1; |
|
240 |
+ dm->scthresh = (int64_t)(((int64_t)max_value * w * h * dm->scthresh_flt) / 100); |
|
241 |
+ dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100); |
|
242 |
+ dm->nxblocks = (w + dm->blockx/2 - 1) / (dm->blockx/2); |
|
243 |
+ dm->nyblocks = (h + dm->blocky/2 - 1) / (dm->blocky/2); |
|
244 |
+ dm->bdiffsize = dm->nxblocks * dm->nyblocks; |
|
245 |
+ dm->bdiffs = av_malloc(dm->bdiffsize * sizeof(*dm->bdiffs)); |
|
246 |
+ dm->queue = av_calloc(dm->cycle, sizeof(*dm->queue)); |
|
247 |
+ |
|
248 |
+ if (!dm->bdiffs || !dm->queue) |
|
249 |
+ return AVERROR(ENOMEM); |
|
250 |
+ |
|
251 |
+ if (dm->ppsrc) { |
|
252 |
+ dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src)); |
|
253 |
+ if (!dm->clean_src) |
|
254 |
+ return AVERROR(ENOMEM); |
|
255 |
+ } |
|
256 |
+ |
|
257 |
+ return 0; |
|
258 |
+} |
|
259 |
+ |
|
260 |
+static av_cold int decimate_init(AVFilterContext *ctx) |
|
261 |
+{ |
|
262 |
+ const DecimateContext *dm = ctx->priv; |
|
263 |
+ AVFilterPad pad = { |
|
264 |
+ .name = av_strdup("main"), |
|
265 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
266 |
+ .filter_frame = filter_frame, |
|
267 |
+ .config_props = config_input, |
|
268 |
+ }; |
|
269 |
+ |
|
270 |
+ if (!pad.name) |
|
271 |
+ return AVERROR(ENOMEM); |
|
272 |
+ ff_insert_inpad(ctx, INPUT_MAIN, &pad); |
|
273 |
+ |
|
274 |
+ if (dm->ppsrc) { |
|
275 |
+ pad.name = av_strdup("clean_src"); |
|
276 |
+ pad.config_props = NULL; |
|
277 |
+ if (!pad.name) |
|
278 |
+ return AVERROR(ENOMEM); |
|
279 |
+ ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad); |
|
280 |
+ } |
|
281 |
+ |
|
282 |
+ if ((dm->blockx & (dm->blockx - 1)) || |
|
283 |
+ (dm->blocky & (dm->blocky - 1))) { |
|
284 |
+ av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n"); |
|
285 |
+ return AVERROR(EINVAL); |
|
286 |
+ } |
|
287 |
+ |
|
288 |
+ return 0; |
|
289 |
+} |
|
290 |
+ |
|
291 |
+static av_cold void decimate_uninit(AVFilterContext *ctx) |
|
292 |
+{ |
|
293 |
+ int i; |
|
294 |
+ DecimateContext *dm = ctx->priv; |
|
295 |
+ |
|
296 |
+ av_frame_free(&dm->last); |
|
297 |
+ av_freep(&dm->bdiffs); |
|
298 |
+ av_freep(&dm->queue); |
|
299 |
+ av_freep(&dm->clean_src); |
|
300 |
+ for (i = 0; i < ctx->nb_inputs; i++) |
|
301 |
+ av_freep(&ctx->input_pads[i].name); |
|
302 |
+} |
|
303 |
+ |
|
304 |
+static int request_inlink(AVFilterContext *ctx, int lid) |
|
305 |
+{ |
|
306 |
+ int ret = 0; |
|
307 |
+ DecimateContext *dm = ctx->priv; |
|
308 |
+ |
|
309 |
+ if (!dm->got_frame[lid]) { |
|
310 |
+ AVFilterLink *inlink = ctx->inputs[lid]; |
|
311 |
+ ret = ff_request_frame(inlink); |
|
312 |
+ if (ret == AVERROR_EOF) { // flushing |
|
313 |
+ dm->eof |= 1 << lid; |
|
314 |
+ ret = filter_frame(inlink, NULL); |
|
315 |
+ } |
|
316 |
+ } |
|
317 |
+ return ret; |
|
318 |
+} |
|
319 |
+ |
|
320 |
+static int request_frame(AVFilterLink *outlink) |
|
321 |
+{ |
|
322 |
+ int ret; |
|
323 |
+ AVFilterContext *ctx = outlink->src; |
|
324 |
+ DecimateContext *dm = ctx->priv; |
|
325 |
+ const uint32_t eof_mask = 1<<INPUT_MAIN | dm->ppsrc<<INPUT_CLEANSRC; |
|
326 |
+ |
|
327 |
+ if ((dm->eof & eof_mask) == eof_mask) // flush done? |
|
328 |
+ return AVERROR_EOF; |
|
329 |
+ if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0) |
|
330 |
+ return ret; |
|
331 |
+ if (dm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0) |
|
332 |
+ return ret; |
|
333 |
+ return 0; |
|
334 |
+} |
|
335 |
+ |
|
336 |
+static int query_formats(AVFilterContext *ctx) |
|
337 |
+{ |
|
338 |
+ static const enum AVPixelFormat pix_fmts[] = { |
|
339 |
+#define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf |
|
340 |
+#define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf |
|
341 |
+#define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf) |
|
342 |
+ PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16), |
|
343 |
+ AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
|
344 |
+ AV_PIX_FMT_GRAY8, |
|
345 |
+ AV_PIX_FMT_NONE |
|
346 |
+ }; |
|
347 |
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
|
348 |
+ return 0; |
|
349 |
+} |
|
350 |
+ |
|
351 |
+static int config_output(AVFilterLink *outlink) |
|
352 |
+{ |
|
353 |
+ AVFilterContext *ctx = outlink->src; |
|
354 |
+ DecimateContext *dm = ctx->priv; |
|
355 |
+ const AVFilterLink *inlink = |
|
356 |
+ ctx->inputs[dm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN]; |
|
357 |
+ AVRational fps = inlink->frame_rate; |
|
358 |
+ |
|
359 |
+ if (!fps.num || !fps.den) { |
|
360 |
+ av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; " |
|
361 |
+ "current rate of %d/%d is invalid\n", fps.num, fps.den); |
|
362 |
+ return AVERROR(EINVAL); |
|
363 |
+ } |
|
364 |
+ fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle}); |
|
365 |
+ av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n", |
|
366 |
+ inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den); |
|
367 |
+ outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; |
|
368 |
+ outlink->time_base = inlink->time_base; |
|
369 |
+ outlink->frame_rate = fps; |
|
370 |
+ outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; |
|
371 |
+ outlink->w = inlink->w; |
|
372 |
+ outlink->h = inlink->h; |
|
373 |
+ dm->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base))); |
|
374 |
+ return 0; |
|
375 |
+} |
|
376 |
+ |
|
377 |
+static const AVFilterPad decimate_outputs[] = { |
|
378 |
+ { |
|
379 |
+ .name = "default", |
|
380 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
381 |
+ .request_frame = request_frame, |
|
382 |
+ .config_props = config_output, |
|
383 |
+ }, |
|
384 |
+ { NULL } |
|
385 |
+}; |
|
386 |
+ |
|
387 |
+AVFilter avfilter_vf_decimate = { |
|
388 |
+ .name = "decimate", |
|
389 |
+ .description = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."), |
|
390 |
+ .init = decimate_init, |
|
391 |
+ .uninit = decimate_uninit, |
|
392 |
+ .priv_size = sizeof(DecimateContext), |
|
393 |
+ .query_formats = query_formats, |
|
394 |
+ .outputs = decimate_outputs, |
|
395 |
+ .priv_class = &decimate_class, |
|
396 |
+ .flags = AVFILTER_FLAG_DYNAMIC_INPUTS, |
|
397 |
+}; |
0 | 398 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,986 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Fredrik Mellbin |
|
2 |
+ * Copyright (c) 2013 Clément Bœsch |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Fieldmatching filter, ported from VFM filter (VapouSsynth) by Clément. |
|
24 |
+ * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a |
|
25 |
+ * light clone of the TIVTC/TFM (AviSynth) filter written by Kevin Stone |
|
26 |
+ * (tritical), the original author. |
|
27 |
+ * |
|
28 |
+ * @see http://bengal.missouri.edu/~kes25c/ |
|
29 |
+ * @see http://www.vapoursynth.com/about/ |
|
30 |
+ */ |
|
31 |
+ |
|
32 |
+#include <inttypes.h> |
|
33 |
+ |
|
34 |
+#include "libavutil/avassert.h" |
|
35 |
+#include "libavutil/imgutils.h" |
|
36 |
+#include "libavutil/opt.h" |
|
37 |
+#include "libavutil/timestamp.h" |
|
38 |
+#include "avfilter.h" |
|
39 |
+#include "internal.h" |
|
40 |
+ |
|
41 |
+#define INPUT_MAIN 0 |
|
42 |
+#define INPUT_CLEANSRC 1 |
|
43 |
+ |
|
44 |
+enum fieldmatch_parity { |
|
45 |
+ FM_PARITY_AUTO = -1, |
|
46 |
+ FM_PARITY_BOTTOM = 0, |
|
47 |
+ FM_PARITY_TOP = 1, |
|
48 |
+}; |
|
49 |
+ |
|
50 |
+enum matching_mode { |
|
51 |
+ MODE_PC, |
|
52 |
+ MODE_PC_N, |
|
53 |
+ MODE_PC_U, |
|
54 |
+ MODE_PC_N_UB, |
|
55 |
+ MODE_PCN, |
|
56 |
+ MODE_PCN_UB, |
|
57 |
+ NB_MODE |
|
58 |
+}; |
|
59 |
+ |
|
60 |
+enum comb_matching_mode { |
|
61 |
+ COMBMATCH_NONE, |
|
62 |
+ COMBMATCH_SC, |
|
63 |
+ COMBMATCH_FULL, |
|
64 |
+ NB_COMBMATCH |
|
65 |
+}; |
|
66 |
+ |
|
67 |
+enum comb_dbg { |
|
68 |
+ COMBDBG_NONE, |
|
69 |
+ COMBDBG_PCN, |
|
70 |
+ COMBDBG_PCNUB, |
|
71 |
+ NB_COMBDBG |
|
72 |
+}; |
|
73 |
+ |
|
74 |
+typedef struct { |
|
75 |
+ const AVClass *class; |
|
76 |
+ |
|
77 |
+ AVFrame *prv, *src, *nxt; ///< main sliding window of 3 frames |
|
78 |
+ AVFrame *prv2, *src2, *nxt2; ///< sliding window of the optional second stream |
|
79 |
+ int64_t frame_count; ///< output frame counter |
|
80 |
+ int got_frame[2]; ///< frame request flag for each input stream |
|
81 |
+ int hsub, vsub; ///< chroma subsampling values |
|
82 |
+ uint32_t eof; ///< bitmask for end of stream |
|
83 |
+ int64_t lastscdiff; |
|
84 |
+ int64_t lastn; |
|
85 |
+ |
|
86 |
+ /* options */ |
|
87 |
+ int order; |
|
88 |
+ int ppsrc; |
|
89 |
+ enum matching_mode mode; |
|
90 |
+ int field; |
|
91 |
+ int mchroma; |
|
92 |
+ int y0, y1; |
|
93 |
+ int64_t scthresh; |
|
94 |
+ double scthresh_flt; |
|
95 |
+ enum comb_matching_mode combmatch; |
|
96 |
+ int combdbg; |
|
97 |
+ int cthresh; |
|
98 |
+ int chroma; |
|
99 |
+ int blockx, blocky; |
|
100 |
+ int combpel; |
|
101 |
+ |
|
102 |
+ /* misc buffers */ |
|
103 |
+ uint8_t *map_data[4]; |
|
104 |
+ int map_linesize[4]; |
|
105 |
+ uint8_t *cmask_data[4]; |
|
106 |
+ int cmask_linesize[4]; |
|
107 |
+ int *c_array; |
|
108 |
+ int tpitchy, tpitchuv; |
|
109 |
+ uint8_t *tbuffer; |
|
110 |
+} FieldMatchContext; |
|
111 |
+ |
|
112 |
+#define OFFSET(x) offsetof(FieldMatchContext, x) |
|
113 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
114 |
+ |
|
115 |
+static const AVOption fieldmatch_options[] = { |
|
116 |
+ { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "order" }, |
|
117 |
+ { "auto", "auto detect parity", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, "order" }, |
|
118 |
+ { "bff", "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "order" }, |
|
119 |
+ { "tff", "assume top field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, "order" }, |
|
120 |
+ { "mode", "set the matching mode or strategy to use", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_PC_N}, MODE_PC, NB_MODE-1, FLAGS, "mode" }, |
|
121 |
+ { "pc", "2-way match (p/c)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC}, INT_MIN, INT_MAX, FLAGS, "mode" }, |
|
122 |
+ { "pc_n", "2-way match + 3rd match on combed (p/c + u)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N}, INT_MIN, INT_MAX, FLAGS, "mode" }, |
|
123 |
+ { "pc_u", "2-way match + 3rd match (same order) on combed (p/c + u)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_U}, INT_MIN, INT_MAX, FLAGS, "mode" }, |
|
124 |
+ { "pc_n_ub", "2-way match + 3rd match on combed + 4th/5th matches if still combed (p/c + u + u/b)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N_UB}, INT_MIN, INT_MAX, FLAGS, "mode" }, |
|
125 |
+ { "pcn", "3-way match (p/c/n)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN}, INT_MIN, INT_MAX, FLAGS, "mode" }, |
|
126 |
+ { "pcn_ub", "3-way match + 4th/5th matches on combed (p/c/n + u/b)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN_UB}, INT_MIN, INT_MAX, FLAGS, "mode" }, |
|
127 |
+ { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS }, |
|
128 |
+ { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "field" }, |
|
129 |
+ { "auto", "automatic (same value as 'order')", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, "field" }, |
|
130 |
+ { "bottom", "bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field" }, |
|
131 |
+ { "top", "top field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, "field" }, |
|
132 |
+ { "mchroma", "set whether or not chroma is included during the match comparisons", OFFSET(mchroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS }, |
|
133 |
+ { "y0", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, |
|
134 |
+ { "y1", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, |
|
135 |
+ { "scthresh", "set scene change detection threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl=12}, 0, 100, FLAGS }, |
|
136 |
+ { "combmatch", "set combmatching mode", OFFSET(combmatch), AV_OPT_TYPE_INT, {.i64=COMBMATCH_SC}, COMBMATCH_NONE, NB_COMBMATCH-1, FLAGS, "combmatching" }, |
|
137 |
+ { "none", "disable combmatching", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "combmatching" }, |
|
138 |
+ { "sc", "enable combmatching only on scene change", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_SC}, INT_MIN, INT_MAX, FLAGS, "combmatching" }, |
|
139 |
+ { "full", "enable combmatching all the time", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_FULL}, INT_MIN, INT_MAX, FLAGS, "combmatching" }, |
|
140 |
+ { "combdbg", "enable comb debug", OFFSET(combdbg), AV_OPT_TYPE_INT, {.i64=COMBDBG_NONE}, COMBDBG_NONE, NB_COMBDBG-1, FLAGS, "dbglvl" }, |
|
141 |
+ { "none", "no forced calculation", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_NONE}, INT_MIN, INT_MAX, FLAGS, "dbglvl" }, |
|
142 |
+ { "pcn", "calculate p/c/n", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCN}, INT_MIN, INT_MAX, FLAGS, "dbglvl" }, |
|
143 |
+ { "pcnub", "calculate p/c/n/u/b", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCNUB}, INT_MIN, INT_MAX, FLAGS, "dbglvl" }, |
|
144 |
+ { "cthresh", "set the area combing threshold used for combed frame detection", OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff, FLAGS }, |
|
145 |
+ { "chroma", "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64= 0}, 0, 1, FLAGS }, |
|
146 |
+ { "blockx", "set the x-axis size of the window used during combed frame detection", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS }, |
|
147 |
+ { "blocky", "set the y-axis size of the window used during combed frame detection", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS }, |
|
148 |
+ { "combpel", "set the number of combed pixels inside any of the blocky by blockx size blocks on the frame for the frame to be detected as combed", OFFSET(combpel), AV_OPT_TYPE_INT, {.i64=80}, 0, INT_MAX, FLAGS }, |
|
149 |
+ { NULL } |
|
150 |
+}; |
|
151 |
+ |
|
152 |
+AVFILTER_DEFINE_CLASS(fieldmatch); |
|
153 |
+ |
|
154 |
+static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane) |
|
155 |
+{ |
|
156 |
+ return plane ? f->width >> fm->hsub : f->width; |
|
157 |
+} |
|
158 |
+ |
|
159 |
+static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane) |
|
160 |
+{ |
|
161 |
+ return plane ? f->height >> fm->vsub : f->height; |
|
162 |
+} |
|
163 |
+ |
|
164 |
+static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2) |
|
165 |
+{ |
|
166 |
+ int x, y; |
|
167 |
+ const uint8_t *srcp1 = f1->data[0]; |
|
168 |
+ const uint8_t *srcp2 = f2->data[0]; |
|
169 |
+ const int src1_linesize = f1->linesize[0]; |
|
170 |
+ const int src2_linesize = f2->linesize[0]; |
|
171 |
+ const int width = f1->width; |
|
172 |
+ const int height = f1->height; |
|
173 |
+ int64_t acc = 0; |
|
174 |
+ |
|
175 |
+ for (y = 0; y < height; y++) { |
|
176 |
+ for (x = 0; x < width; x++) |
|
177 |
+ acc += abs(srcp1[x] - srcp2[x]); |
|
178 |
+ srcp1 += src1_linesize; |
|
179 |
+ srcp2 += src2_linesize; |
|
180 |
+ } |
|
181 |
+ return acc; |
|
182 |
+} |
|
183 |
+ |
|
184 |
+static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v) |
|
185 |
+{ |
|
186 |
+ int y; |
|
187 |
+ |
|
188 |
+ for (y = 0; y < h; y++) { |
|
189 |
+ memset(data, v, w); |
|
190 |
+ data += linesize; |
|
191 |
+ } |
|
192 |
+} |
|
193 |
+ |
|
194 |
+static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src) |
|
195 |
+{ |
|
196 |
+ int x, y, plane, max_v = 0; |
|
197 |
+ const int cthresh = fm->cthresh; |
|
198 |
+ const int cthresh6 = cthresh * 6; |
|
199 |
+ |
|
200 |
+ for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) { |
|
201 |
+ const uint8_t *srcp = src->data[plane]; |
|
202 |
+ const int src_linesize = src->linesize[plane]; |
|
203 |
+ const int width = get_width (fm, src, plane); |
|
204 |
+ const int height = get_height(fm, src, plane); |
|
205 |
+ uint8_t *cmkp = fm->cmask_data[plane]; |
|
206 |
+ const int cmk_linesize = fm->cmask_linesize[plane]; |
|
207 |
+ |
|
208 |
+ if (cthresh < 0) { |
|
209 |
+ fill_buf(cmkp, width, height, cmk_linesize, 0xff); |
|
210 |
+ continue; |
|
211 |
+ } |
|
212 |
+ fill_buf(cmkp, width, height, cmk_linesize, 0); |
|
213 |
+ |
|
214 |
+ /* [1 -3 4 -3 1] vertical filter */ |
|
215 |
+#define FILTER(xm2, xm1, xp1, xp2) \ |
|
216 |
+ abs( 4 * srcp[x] \ |
|
217 |
+ -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \ |
|
218 |
+ + (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6 |
|
219 |
+ |
|
220 |
+ /* first line */ |
|
221 |
+ for (x = 0; x < width; x++) { |
|
222 |
+ const int s1 = abs(srcp[x] - srcp[x + src_linesize]); |
|
223 |
+ if (s1 > cthresh && FILTER(2, 1, 1, 2)) |
|
224 |
+ cmkp[x] = 0xff; |
|
225 |
+ } |
|
226 |
+ srcp += src_linesize; |
|
227 |
+ cmkp += cmk_linesize; |
|
228 |
+ |
|
229 |
+ /* second line */ |
|
230 |
+ for (x = 0; x < width; x++) { |
|
231 |
+ const int s1 = abs(srcp[x] - srcp[x - src_linesize]); |
|
232 |
+ const int s2 = abs(srcp[x] - srcp[x + src_linesize]); |
|
233 |
+ if (s1 > cthresh && s2 > cthresh && FILTER(2, -1, 1, 2)) |
|
234 |
+ cmkp[x] = 0xff; |
|
235 |
+ } |
|
236 |
+ srcp += src_linesize; |
|
237 |
+ cmkp += cmk_linesize; |
|
238 |
+ |
|
239 |
+ /* all lines minus first two and last two */ |
|
240 |
+ for (y = 2; y < height-2; y++) { |
|
241 |
+ for (x = 0; x < width; x++) { |
|
242 |
+ const int s1 = abs(srcp[x] - srcp[x - src_linesize]); |
|
243 |
+ const int s2 = abs(srcp[x] - srcp[x + src_linesize]); |
|
244 |
+ if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, 2)) |
|
245 |
+ cmkp[x] = 0xff; |
|
246 |
+ } |
|
247 |
+ srcp += src_linesize; |
|
248 |
+ cmkp += cmk_linesize; |
|
249 |
+ } |
|
250 |
+ |
|
251 |
+ /* before-last line */ |
|
252 |
+ for (x = 0; x < width; x++) { |
|
253 |
+ const int s1 = abs(srcp[x] - srcp[x - src_linesize]); |
|
254 |
+ const int s2 = abs(srcp[x] - srcp[x + src_linesize]); |
|
255 |
+ if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, -2)) |
|
256 |
+ cmkp[x] = 0xff; |
|
257 |
+ } |
|
258 |
+ srcp += src_linesize; |
|
259 |
+ cmkp += cmk_linesize; |
|
260 |
+ |
|
261 |
+ /* last line */ |
|
262 |
+ for (x = 0; x < width; x++) { |
|
263 |
+ const int s1 = abs(srcp[x] - srcp[x - src_linesize]); |
|
264 |
+ if (s1 > cthresh && FILTER(-2, -1, -1, -2)) |
|
265 |
+ cmkp[x] = 0xff; |
|
266 |
+ } |
|
267 |
+ } |
|
268 |
+ |
|
269 |
+ if (fm->chroma) { |
|
270 |
+ uint8_t *cmkp = fm->cmask_data[0]; |
|
271 |
+ uint8_t *cmkpU = fm->cmask_data[1]; |
|
272 |
+ uint8_t *cmkpV = fm->cmask_data[2]; |
|
273 |
+ const int width = src->width >> fm->hsub; |
|
274 |
+ const int height = src->height >> fm->vsub; |
|
275 |
+ const int cmk_linesize = fm->cmask_linesize[0] << 1; |
|
276 |
+ const int cmk_linesizeUV = fm->cmask_linesize[2]; |
|
277 |
+ uint8_t *cmkpp = cmkp - (cmk_linesize>>1); |
|
278 |
+ uint8_t *cmkpn = cmkp + (cmk_linesize>>1); |
|
279 |
+ uint8_t *cmkpnn = cmkp + cmk_linesize; |
|
280 |
+ for (y = 1; y < height - 1; y++) { |
|
281 |
+ cmkpp += cmk_linesize; |
|
282 |
+ cmkp += cmk_linesize; |
|
283 |
+ cmkpn += cmk_linesize; |
|
284 |
+ cmkpnn += cmk_linesize; |
|
285 |
+ cmkpV += cmk_linesizeUV; |
|
286 |
+ cmkpU += cmk_linesizeUV; |
|
287 |
+ for (x = 1; x < width - 1; x++) { |
|
288 |
+#define HAS_FF_AROUND(p, lz) (p[x-1 - lz] == 0xff || p[x - lz] == 0xff || p[x+1 - lz] == 0xff || \ |
|
289 |
+ p[x-1 ] == 0xff || p[x+1 ] == 0xff || \ |
|
290 |
+ p[x-1 + lz] == 0xff || p[x + lz] == 0xff || p[x+1 + lz] == 0xff) |
|
291 |
+ if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) || |
|
292 |
+ (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) { |
|
293 |
+ ((uint16_t*)cmkp)[x] = 0xffff; |
|
294 |
+ ((uint16_t*)cmkpn)[x] = 0xffff; |
|
295 |
+ if (y&1) ((uint16_t*)cmkpp)[x] = 0xffff; |
|
296 |
+ else ((uint16_t*)cmkpnn)[x] = 0xffff; |
|
297 |
+ } |
|
298 |
+ } |
|
299 |
+ } |
|
300 |
+ } |
|
301 |
+ |
|
302 |
+ { |
|
303 |
+ const int blockx = fm->blockx; |
|
304 |
+ const int blocky = fm->blocky; |
|
305 |
+ const int xhalf = blockx/2; |
|
306 |
+ const int yhalf = blocky/2; |
|
307 |
+ const int cmk_linesize = fm->cmask_linesize[0]; |
|
308 |
+ const uint8_t *cmkp = fm->cmask_data[0] + cmk_linesize; |
|
309 |
+ const int width = src->width; |
|
310 |
+ const int height = src->height; |
|
311 |
+ const int xblocks = ((width+xhalf)/blockx) + 1; |
|
312 |
+ const int xblocks4 = xblocks<<2; |
|
313 |
+ const int yblocks = ((height+yhalf)/blocky) + 1; |
|
314 |
+ int *c_array = fm->c_array; |
|
315 |
+ const int arraysize = (xblocks*yblocks)<<2; |
|
316 |
+ int heighta = (height/(blocky/2))*(blocky/2); |
|
317 |
+ const int widtha = (width /(blockx/2))*(blockx/2); |
|
318 |
+ if (heighta == height) |
|
319 |
+ heighta = height - yhalf; |
|
320 |
+ memset(c_array, 0, arraysize * sizeof(*c_array)); |
|
321 |
+ |
|
322 |
+#define C_ARRAY_ADD(v) do { \ |
|
323 |
+ const int box1 = (x / blockx) * 4; \ |
|
324 |
+ const int box2 = ((x + xhalf) / blockx) * 4; \ |
|
325 |
+ c_array[temp1 + box1 ] += v; \ |
|
326 |
+ c_array[temp1 + box2 + 1] += v; \ |
|
327 |
+ c_array[temp2 + box1 + 2] += v; \ |
|
328 |
+ c_array[temp2 + box2 + 3] += v; \ |
|
329 |
+} while (0) |
|
330 |
+ |
|
331 |
+#define VERTICAL_HALF(y_start, y_end) do { \ |
|
332 |
+ for (y = y_start; y < y_end; y++) { \ |
|
333 |
+ const int temp1 = (y / blocky) * xblocks4; \ |
|
334 |
+ const int temp2 = ((y + yhalf) / blocky) * xblocks4; \ |
|
335 |
+ for (x = 0; x < width; x++) \ |
|
336 |
+ if (cmkp[x - cmk_linesize] == 0xff && \ |
|
337 |
+ cmkp[x ] == 0xff && \ |
|
338 |
+ cmkp[x + cmk_linesize] == 0xff) \ |
|
339 |
+ C_ARRAY_ADD(1); \ |
|
340 |
+ cmkp += cmk_linesize; \ |
|
341 |
+ } \ |
|
342 |
+} while (0) |
|
343 |
+ |
|
344 |
+ VERTICAL_HALF(1, yhalf); |
|
345 |
+ |
|
346 |
+ for (y = yhalf; y < heighta; y += yhalf) { |
|
347 |
+ const int temp1 = (y / blocky) * xblocks4; |
|
348 |
+ const int temp2 = ((y + yhalf) / blocky) * xblocks4; |
|
349 |
+ |
|
350 |
+ for (x = 0; x < widtha; x += xhalf) { |
|
351 |
+ const uint8_t *cmkp_tmp = cmkp + x; |
|
352 |
+ int u, v, sum = 0; |
|
353 |
+ for (u = 0; u < yhalf; u++) { |
|
354 |
+ for (v = 0; v < xhalf; v++) |
|
355 |
+ if (cmkp_tmp[v - cmk_linesize] == 0xff && |
|
356 |
+ cmkp_tmp[v ] == 0xff && |
|
357 |
+ cmkp_tmp[v + cmk_linesize] == 0xff) |
|
358 |
+ sum++; |
|
359 |
+ cmkp_tmp += cmk_linesize; |
|
360 |
+ } |
|
361 |
+ if (sum) |
|
362 |
+ C_ARRAY_ADD(sum); |
|
363 |
+ } |
|
364 |
+ |
|
365 |
+ for (x = widtha; x < width; x++) { |
|
366 |
+ const uint8_t *cmkp_tmp = cmkp + x; |
|
367 |
+ int u, sum = 0; |
|
368 |
+ for (u = 0; u < yhalf; u++) { |
|
369 |
+ if (cmkp_tmp[-cmk_linesize] == 0xff && |
|
370 |
+ cmkp_tmp[ 0] == 0xff && |
|
371 |
+ cmkp_tmp[ cmk_linesize] == 0xff) |
|
372 |
+ sum++; |
|
373 |
+ cmkp_tmp += cmk_linesize; |
|
374 |
+ } |
|
375 |
+ if (sum) |
|
376 |
+ C_ARRAY_ADD(sum); |
|
377 |
+ } |
|
378 |
+ |
|
379 |
+ cmkp += cmk_linesize * yhalf; |
|
380 |
+ } |
|
381 |
+ |
|
382 |
+ VERTICAL_HALF(heighta, height - 1); |
|
383 |
+ |
|
384 |
+ for (x = 0; x < arraysize; x++) |
|
385 |
+ if (c_array[x] > max_v) |
|
386 |
+ max_v = c_array[x]; |
|
387 |
+ } |
|
388 |
+ return max_v; |
|
389 |
+} |
|
390 |
+ |
|
391 |
+// the secret is that tbuffer is an interlaced, offset subset of all the lines |
|
392 |
+static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize, |
|
393 |
+ const uint8_t *nxtp, int nxt_linesize, |
|
394 |
+ uint8_t *tbuffer, int tbuf_linesize, |
|
395 |
+ int width, int height) |
|
396 |
+{ |
|
397 |
+ int y, x; |
|
398 |
+ |
|
399 |
+ prvp -= prv_linesize; |
|
400 |
+ nxtp -= nxt_linesize; |
|
401 |
+ for (y = 0; y < height; y++) { |
|
402 |
+ for (x = 0; x < width; x++) |
|
403 |
+ tbuffer[x] = FFABS(prvp[x] - nxtp[x]); |
|
404 |
+ prvp += prv_linesize; |
|
405 |
+ nxtp += nxt_linesize; |
|
406 |
+ tbuffer += tbuf_linesize; |
|
407 |
+ } |
|
408 |
+} |
|
409 |
+ |
|
410 |
+/** |
|
411 |
+ * Build a map over which pixels differ a lot/a little |
|
412 |
+ */ |
|
413 |
+static void build_diff_map(FieldMatchContext *fm, |
|
414 |
+ const uint8_t *prvp, int prv_linesize, |
|
415 |
+ const uint8_t *nxtp, int nxt_linesize, |
|
416 |
+ uint8_t *dstp, int dst_linesize, int height, |
|
417 |
+ int width, int plane) |
|
418 |
+{ |
|
419 |
+ int x, y, u, diff, count; |
|
420 |
+ int tpitch = plane ? fm->tpitchuv : fm->tpitchy; |
|
421 |
+ const uint8_t *dp = fm->tbuffer + tpitch; |
|
422 |
+ |
|
423 |
+ build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize, |
|
424 |
+ fm->tbuffer, tpitch, width, height>>1); |
|
425 |
+ |
|
426 |
+ for (y = 2; y < height - 2; y += 2) { |
|
427 |
+ for (x = 1; x < width - 1; x++) { |
|
428 |
+ diff = dp[x]; |
|
429 |
+ if (diff > 3) { |
|
430 |
+ for (count = 0, u = x-1; u < x+2 && count < 2; u++) { |
|
431 |
+ count += dp[u-tpitch] > 3; |
|
432 |
+ count += dp[u ] > 3; |
|
433 |
+ count += dp[u+tpitch] > 3; |
|
434 |
+ } |
|
435 |
+ if (count > 1) { |
|
436 |
+ dstp[x] = 1; |
|
437 |
+ if (diff > 19) { |
|
438 |
+ int upper = 0, lower = 0; |
|
439 |
+ for (count = 0, u = x-1; u < x+2 && count < 6; u++) { |
|
440 |
+ if (dp[u-tpitch] > 19) { count++; upper = 1; } |
|
441 |
+ if (dp[u ] > 19) count++; |
|
442 |
+ if (dp[u+tpitch] > 19) { count++; lower = 1; } |
|
443 |
+ } |
|
444 |
+ if (count > 3) { |
|
445 |
+ if (upper && lower) { |
|
446 |
+ dstp[x] |= 1<<1; |
|
447 |
+ } else { |
|
448 |
+ int upper2 = 0, lower2 = 0; |
|
449 |
+ for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) { |
|
450 |
+ if (y != 2 && dp[u-2*tpitch] > 19) upper2 = 1; |
|
451 |
+ if ( dp[u- tpitch] > 19) upper = 1; |
|
452 |
+ if ( dp[u+ tpitch] > 19) lower = 1; |
|
453 |
+ if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1; |
|
454 |
+ } |
|
455 |
+ if ((upper && (lower || upper2)) || |
|
456 |
+ (lower && (upper || lower2))) |
|
457 |
+ dstp[x] |= 1<<1; |
|
458 |
+ else if (count > 5) |
|
459 |
+ dstp[x] |= 1<<2; |
|
460 |
+ } |
|
461 |
+ } |
|
462 |
+ } |
|
463 |
+ } |
|
464 |
+ } |
|
465 |
+ } |
|
466 |
+ dp += tpitch; |
|
467 |
+ dstp += dst_linesize; |
|
468 |
+ } |
|
469 |
+} |
|
470 |
+ |
|
471 |
+enum { mP, mC, mN, mB, mU }; |
|
472 |
+ |
|
473 |
+static int get_field_base(int match, int field) |
|
474 |
+{ |
|
475 |
+ return match < 3 ? 2 - field : 1 + field; |
|
476 |
+} |
|
477 |
+ |
|
478 |
+static AVFrame *select_frame(FieldMatchContext *fm, int match) |
|
479 |
+{ |
|
480 |
+ if (match == mP || match == mB) return fm->prv; |
|
481 |
+ else if (match == mN || match == mU) return fm->nxt; |
|
482 |
+ else /* match == mC */ return fm->src; |
|
483 |
+} |
|
484 |
+ |
|
485 |
+static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field) |
|
486 |
+{ |
|
487 |
+ int plane, ret; |
|
488 |
+ uint64_t accumPc = 0, accumPm = 0, accumPml = 0; |
|
489 |
+ uint64_t accumNc = 0, accumNm = 0, accumNml = 0; |
|
490 |
+ int norm1, norm2, mtn1, mtn2; |
|
491 |
+ float c1, c2, mr; |
|
492 |
+ const AVFrame *src = fm->src; |
|
493 |
+ |
|
494 |
+ for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) { |
|
495 |
+ int x, y, temp1, temp2, fbase; |
|
496 |
+ const AVFrame *prev, *next; |
|
497 |
+ uint8_t *mapp = fm->map_data[plane]; |
|
498 |
+ int map_linesize = fm->map_linesize[plane]; |
|
499 |
+ const uint8_t *srcp = src->data[plane]; |
|
500 |
+ const int src_linesize = src->linesize[plane]; |
|
501 |
+ const int srcf_linesize = src_linesize << 1; |
|
502 |
+ int prv_linesize, nxt_linesize; |
|
503 |
+ int prvf_linesize, nxtf_linesize; |
|
504 |
+ const int width = get_width (fm, src, plane); |
|
505 |
+ const int height = get_height(fm, src, plane); |
|
506 |
+ const int y0a = fm->y0 >> (plane != 0); |
|
507 |
+ const int y1a = fm->y1 >> (plane != 0); |
|
508 |
+ const int startx = (plane == 0 ? 8 : 4); |
|
509 |
+ const int stopx = width - startx; |
|
510 |
+ const uint8_t *srcpf, *srcf, *srcnf; |
|
511 |
+ const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf; |
|
512 |
+ |
|
513 |
+ fill_buf(mapp, width, height, map_linesize, 0); |
|
514 |
+ |
|
515 |
+ /* match1 */ |
|
516 |
+ fbase = get_field_base(match1, field); |
|
517 |
+ srcf = srcp + (fbase + 1) * src_linesize; |
|
518 |
+ srcpf = srcf - srcf_linesize; |
|
519 |
+ srcnf = srcf + srcf_linesize; |
|
520 |
+ mapp = mapp + fbase * map_linesize; |
|
521 |
+ prev = select_frame(fm, match1); |
|
522 |
+ prv_linesize = prev->linesize[plane]; |
|
523 |
+ prvf_linesize = prv_linesize << 1; |
|
524 |
+ prvpf = prev->data[plane] + fbase * prv_linesize; // previous frame, previous field |
|
525 |
+ prvnf = prvpf + prvf_linesize; // previous frame, next field |
|
526 |
+ |
|
527 |
+ /* match2 */ |
|
528 |
+ fbase = get_field_base(match2, field); |
|
529 |
+ next = select_frame(fm, match2); |
|
530 |
+ nxt_linesize = next->linesize[plane]; |
|
531 |
+ nxtf_linesize = nxt_linesize << 1; |
|
532 |
+ nxtpf = next->data[plane] + fbase * nxt_linesize; // next frame, previous field |
|
533 |
+ nxtnf = nxtpf + nxtf_linesize; // next frame, next field |
|
534 |
+ |
|
535 |
+ map_linesize <<= 1; |
|
536 |
+ if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1)) |
|
537 |
+ build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize, |
|
538 |
+ mapp, map_linesize, height, width, plane); |
|
539 |
+ else |
|
540 |
+ build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize, |
|
541 |
+ mapp + map_linesize, map_linesize, height, width, plane); |
|
542 |
+ |
|
543 |
+ for (y = 2; y < height - 2; y += 2) { |
|
544 |
+ if (y0a == y1a || y < y0a || y > y1a) { |
|
545 |
+ for (x = startx; x < stopx; x++) { |
|
546 |
+ if (mapp[x] > 0 || mapp[x + map_linesize] > 0) { |
|
547 |
+ temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1] |
|
548 |
+ |
|
549 |
+ temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1); |
|
550 |
+ if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1))) |
|
551 |
+ accumPc += temp2; |
|
552 |
+ if (temp2 > 42) { |
|
553 |
+ if ((mapp[x]&2) || (mapp[x + map_linesize]&2)) |
|
554 |
+ accumPm += temp2; |
|
555 |
+ if ((mapp[x]&4) || (mapp[x + map_linesize]&4)) |
|
556 |
+ accumPml += temp2; |
|
557 |
+ } |
|
558 |
+ |
|
559 |
+ temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1); |
|
560 |
+ if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1))) |
|
561 |
+ accumNc += temp2; |
|
562 |
+ if (temp2 > 42) { |
|
563 |
+ if ((mapp[x]&2) || (mapp[x + map_linesize]&2)) |
|
564 |
+ accumNm += temp2; |
|
565 |
+ if ((mapp[x]&4) || (mapp[x + map_linesize]&4)) |
|
566 |
+ accumNml += temp2; |
|
567 |
+ } |
|
568 |
+ } |
|
569 |
+ } |
|
570 |
+ } |
|
571 |
+ prvpf += prvf_linesize; |
|
572 |
+ prvnf += prvf_linesize; |
|
573 |
+ srcpf += srcf_linesize; |
|
574 |
+ srcf += srcf_linesize; |
|
575 |
+ srcnf += srcf_linesize; |
|
576 |
+ nxtpf += nxtf_linesize; |
|
577 |
+ nxtnf += nxtf_linesize; |
|
578 |
+ mapp += map_linesize; |
|
579 |
+ } |
|
580 |
+ } |
|
581 |
+ |
|
582 |
+ if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) && |
|
583 |
+ FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) { |
|
584 |
+ accumPm = accumPml; |
|
585 |
+ accumNm = accumNml; |
|
586 |
+ } |
|
587 |
+ |
|
588 |
+ norm1 = (int)((accumPc / 6.0f) + 0.5f); |
|
589 |
+ norm2 = (int)((accumNc / 6.0f) + 0.5f); |
|
590 |
+ mtn1 = (int)((accumPm / 6.0f) + 0.5f); |
|
591 |
+ mtn2 = (int)((accumNm / 6.0f) + 0.5f); |
|
592 |
+ c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1)); |
|
593 |
+ c2 = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMIN(mtn1, mtn2), 1)); |
|
594 |
+ mr = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMAX(norm1,norm2),1)); |
|
595 |
+ if (((mtn1 >= 500 || mtn2 >= 500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) || |
|
596 |
+ ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) || |
|
597 |
+ ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) || |
|
598 |
+ ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1)) |
|
599 |
+ ret = mtn1 > mtn2 ? match2 : match1; |
|
600 |
+ else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) |
|
601 |
+ ret = mtn1 > mtn2 ? match2 : match1; |
|
602 |
+ else |
|
603 |
+ ret = norm1 > norm2 ? match2 : match1; |
|
604 |
+ return ret; |
|
605 |
+} |
|
606 |
+ |
|
607 |
+static void copy_fields(const FieldMatchContext *fm, AVFrame *dst, |
|
608 |
+ const AVFrame *src, int field) |
|
609 |
+{ |
|
610 |
+ int plane; |
|
611 |
+ for (plane = 0; plane < 4 && src->data[plane]; plane++) |
|
612 |
+ av_image_copy_plane(dst->data[plane] + field*dst->linesize[plane], dst->linesize[plane] << 1, |
|
613 |
+ src->data[plane] + field*src->linesize[plane], src->linesize[plane] << 1, |
|
614 |
+ get_width(fm, src, plane), get_height(fm, src, plane) / 2); |
|
615 |
+} |
|
616 |
+ |
|
617 |
+static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field, |
|
618 |
+ const AVFrame *prv, AVFrame *src, const AVFrame *nxt) |
|
619 |
+{ |
|
620 |
+ AVFrame *dst; |
|
621 |
+ FieldMatchContext *fm = ctx->priv; |
|
622 |
+ |
|
623 |
+ if (match == mC) { |
|
624 |
+ dst = av_frame_clone(src); |
|
625 |
+ } else { |
|
626 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
627 |
+ |
|
628 |
+ dst = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
|
629 |
+ if (!dst) |
|
630 |
+ return NULL; |
|
631 |
+ av_frame_copy_props(dst, src); |
|
632 |
+ |
|
633 |
+ switch (match) { |
|
634 |
+ case mP: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, prv, field); break; |
|
635 |
+ case mN: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, nxt, field); break; |
|
636 |
+ case mB: copy_fields(fm, dst, src, field); copy_fields(fm, dst, prv, 1-field); break; |
|
637 |
+ case mU: copy_fields(fm, dst, src, field); copy_fields(fm, dst, nxt, 1-field); break; |
|
638 |
+ default: av_assert0(0); |
|
639 |
+ } |
|
640 |
+ } |
|
641 |
+ return dst; |
|
642 |
+} |
|
643 |
+ |
|
644 |
+static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2, |
|
645 |
+ AVFrame **gen_frames, int field) |
|
646 |
+{ |
|
647 |
+ const FieldMatchContext *fm = ctx->priv; |
|
648 |
+ |
|
649 |
+#define LOAD_COMB(mid) do { \ |
|
650 |
+ if (combs[mid] < 0) { \ |
|
651 |
+ if (!gen_frames[mid]) \ |
|
652 |
+ gen_frames[mid] = create_weave_frame(ctx, mid, field, \ |
|
653 |
+ fm->prv, fm->src, fm->nxt); \ |
|
654 |
+ combs[mid] = calc_combed_score(fm, gen_frames[mid]); \ |
|
655 |
+ } \ |
|
656 |
+} while (0) |
|
657 |
+ |
|
658 |
+ LOAD_COMB(m1); |
|
659 |
+ LOAD_COMB(m2); |
|
660 |
+ |
|
661 |
+ if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->combpel)) && |
|
662 |
+ abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->combpel) |
|
663 |
+ return m2; |
|
664 |
+ else |
|
665 |
+ return m1; |
|
666 |
+} |
|
667 |
+ |
|
668 |
+static const int fxo0m[] = { mP, mC, mN, mB, mU }; |
|
669 |
+static const int fxo1m[] = { mN, mC, mP, mU, mB }; |
|
670 |
+ |
|
671 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
672 |
+{ |
|
673 |
+ AVFilterContext *ctx = inlink->dst; |
|
674 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
675 |
+ FieldMatchContext *fm = ctx->priv; |
|
676 |
+ int combs[] = { -1, -1, -1, -1, -1 }; |
|
677 |
+ int order, field, i, match, sc = 0; |
|
678 |
+ const int *fxo; |
|
679 |
+ AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL }; |
|
680 |
+ AVFrame *dst; |
|
681 |
+ |
|
682 |
+ /* update frames queue(s) */ |
|
683 |
+#define SLIDING_FRAME_WINDOW(prv, src, nxt) do { \ |
|
684 |
+ if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \ |
|
685 |
+ av_frame_free(&prv); \ |
|
686 |
+ prv = src; \ |
|
687 |
+ src = nxt; \ |
|
688 |
+ if (in) \ |
|
689 |
+ nxt = in; \ |
|
690 |
+ if (!prv) \ |
|
691 |
+ prv = src; \ |
|
692 |
+ if (!prv) /* received only one frame at that point */ \ |
|
693 |
+ return 0; \ |
|
694 |
+ av_assert0(prv && src && nxt); \ |
|
695 |
+} while (0) |
|
696 |
+ if (FF_INLINK_IDX(inlink) == INPUT_MAIN) { |
|
697 |
+ SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt); |
|
698 |
+ fm->got_frame[INPUT_MAIN] = 1; |
|
699 |
+ } else { |
|
700 |
+ SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2); |
|
701 |
+ fm->got_frame[INPUT_CLEANSRC] = 1; |
|
702 |
+ } |
|
703 |
+ if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC])) |
|
704 |
+ return 0; |
|
705 |
+ fm->got_frame[INPUT_MAIN] = fm->got_frame[INPUT_CLEANSRC] = 0; |
|
706 |
+ in = fm->src; |
|
707 |
+ |
|
708 |
+ /* parity */ |
|
709 |
+ order = fm->order != FM_PARITY_AUTO ? fm->order : (in->interlaced_frame ? in->top_field_first : 1); |
|
710 |
+ field = fm->field != FM_PARITY_AUTO ? fm->field : order; |
|
711 |
+ av_assert0(order == 0 || order == 1 || field == 0 || field == 1); |
|
712 |
+ fxo = field ^ order ? fxo1m : fxo0m; |
|
713 |
+ |
|
714 |
+ /* debug mode: we generate all the fields combinations and their associated |
|
715 |
+ * combed score. XXX: inject as frame metadata? */ |
|
716 |
+ if (fm->combdbg) { |
|
717 |
+ for (i = 0; i < FF_ARRAY_ELEMS(combs); i++) { |
|
718 |
+ if (i > mN && fm->combdbg == COMBDBG_PCN) |
|
719 |
+ break; |
|
720 |
+ gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt); |
|
721 |
+ if (!gen_frames[i]) |
|
722 |
+ return AVERROR(ENOMEM); |
|
723 |
+ combs[i] = calc_combed_score(fm, gen_frames[i]); |
|
724 |
+ } |
|
725 |
+ av_log(ctx, AV_LOG_INFO, "COMBS: %3d %3d %3d %3d %3d\n", |
|
726 |
+ combs[0], combs[1], combs[2], combs[3], combs[4]); |
|
727 |
+ } else { |
|
728 |
+ gen_frames[mC] = av_frame_clone(fm->src); |
|
729 |
+ if (!gen_frames[mC]) |
|
730 |
+ return AVERROR(ENOMEM); |
|
731 |
+ } |
|
732 |
+ |
|
733 |
+ /* p/c selection and optional 3-way p/c/n matches */ |
|
734 |
+ match = compare_fields(fm, fxo[mC], fxo[mP], field); |
|
735 |
+ if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB) |
|
736 |
+ match = compare_fields(fm, match, fxo[mN], field); |
|
737 |
+ |
|
738 |
+ /* scene change check */ |
|
739 |
+ if (fm->combmatch == COMBMATCH_SC) { |
|
740 |
+ if (fm->lastn == fm->frame_count - 1) { |
|
741 |
+ if (fm->lastscdiff > fm->scthresh) |
|
742 |
+ sc = 1; |
|
743 |
+ } else if (luma_abs_diff(fm->prv, fm->src) > fm->scthresh) { |
|
744 |
+ sc = 1; |
|
745 |
+ } |
|
746 |
+ |
|
747 |
+ if (!sc) { |
|
748 |
+ fm->lastn = fm->frame_count; |
|
749 |
+ fm->lastscdiff = luma_abs_diff(fm->src, fm->nxt); |
|
750 |
+ sc = fm->lastscdiff > fm->scthresh; |
|
751 |
+ } |
|
752 |
+ } |
|
753 |
+ |
|
754 |
+ if (fm->combmatch == COMBMATCH_FULL || (fm->combmatch == COMBMATCH_SC && sc)) { |
|
755 |
+ switch (fm->mode) { |
|
756 |
+ /* 2-way p/c matches */ |
|
757 |
+ case MODE_PC: |
|
758 |
+ match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field); |
|
759 |
+ break; |
|
760 |
+ case MODE_PC_N: |
|
761 |
+ match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field); |
|
762 |
+ break; |
|
763 |
+ case MODE_PC_U: |
|
764 |
+ match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field); |
|
765 |
+ break; |
|
766 |
+ case MODE_PC_N_UB: |
|
767 |
+ match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field); |
|
768 |
+ match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field); |
|
769 |
+ match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field); |
|
770 |
+ break; |
|
771 |
+ /* 3-way p/c/n matches */ |
|
772 |
+ case MODE_PCN: |
|
773 |
+ match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field); |
|
774 |
+ break; |
|
775 |
+ case MODE_PCN_UB: |
|
776 |
+ match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field); |
|
777 |
+ match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field); |
|
778 |
+ break; |
|
779 |
+ default: |
|
780 |
+ av_assert0(0); |
|
781 |
+ } |
|
782 |
+ } |
|
783 |
+ |
|
784 |
+ /* get output frame and drop the others */ |
|
785 |
+ if (fm->ppsrc) { |
|
786 |
+ /* field matching was based on a filtered/post-processed input, we now |
|
787 |
+ * pick the untouched fields from the clean source */ |
|
788 |
+ dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2); |
|
789 |
+ } else { |
|
790 |
+ if (!gen_frames[match]) { // XXX: is that possible? |
|
791 |
+ dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt); |
|
792 |
+ } else { |
|
793 |
+ dst = gen_frames[match]; |
|
794 |
+ gen_frames[match] = NULL; |
|
795 |
+ } |
|
796 |
+ } |
|
797 |
+ if (!dst) |
|
798 |
+ return AVERROR(ENOMEM); |
|
799 |
+ for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++) |
|
800 |
+ av_frame_free(&gen_frames[i]); |
|
801 |
+ |
|
802 |
+ /* mark the frame we are unable to match properly as interlaced so a proper |
|
803 |
+ * de-interlacer can take the relay */ |
|
804 |
+ dst->interlaced_frame = combs[match] >= fm->combpel; |
|
805 |
+ if (dst->interlaced_frame) { |
|
806 |
+ av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n", |
|
807 |
+ fm->frame_count, av_ts2timestr(in->pts, &inlink->time_base)); |
|
808 |
+ dst->top_field_first = field; |
|
809 |
+ } |
|
810 |
+ fm->frame_count++; |
|
811 |
+ |
|
812 |
+ av_log(ctx, AV_LOG_DEBUG, "SC:%d | COMBS: %3d %3d %3d %3d %3d (combpel=%d)" |
|
813 |
+ " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4], |
|
814 |
+ fm->combpel, match, dst->interlaced_frame ? "YES" : "NO"); |
|
815 |
+ |
|
816 |
+ return ff_filter_frame(outlink, dst); |
|
817 |
+} |
|
818 |
+ |
|
819 |
+static int request_inlink(AVFilterContext *ctx, int lid) |
|
820 |
+{ |
|
821 |
+ int ret = 0; |
|
822 |
+ FieldMatchContext *fm = ctx->priv; |
|
823 |
+ |
|
824 |
+ if (!fm->got_frame[lid]) { |
|
825 |
+ AVFilterLink *inlink = ctx->inputs[lid]; |
|
826 |
+ ret = ff_request_frame(inlink); |
|
827 |
+ if (ret == AVERROR_EOF) { // flushing |
|
828 |
+ fm->eof |= 1 << lid; |
|
829 |
+ ret = filter_frame(inlink, NULL); |
|
830 |
+ } |
|
831 |
+ } |
|
832 |
+ return ret; |
|
833 |
+} |
|
834 |
+ |
|
835 |
+static int request_frame(AVFilterLink *outlink) |
|
836 |
+{ |
|
837 |
+ int ret; |
|
838 |
+ AVFilterContext *ctx = outlink->src; |
|
839 |
+ FieldMatchContext *fm = ctx->priv; |
|
840 |
+ const uint32_t eof_mask = 1<<INPUT_MAIN | fm->ppsrc<<INPUT_CLEANSRC; |
|
841 |
+ |
|
842 |
+ if ((fm->eof & eof_mask) == eof_mask) // flush done? |
|
843 |
+ return AVERROR_EOF; |
|
844 |
+ if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0) |
|
845 |
+ return ret; |
|
846 |
+ if (fm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0) |
|
847 |
+ return ret; |
|
848 |
+ return 0; |
|
849 |
+} |
|
850 |
+ |
|
851 |
+static int query_formats(AVFilterContext *ctx) |
|
852 |
+{ |
|
853 |
+ // TODO: second input source can support >8bit depth |
|
854 |
+ static const enum AVPixelFormat pix_fmts[] = { |
|
855 |
+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, |
|
856 |
+ AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
|
857 |
+ AV_PIX_FMT_NONE |
|
858 |
+ }; |
|
859 |
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
|
860 |
+ return 0; |
|
861 |
+} |
|
862 |
+ |
|
863 |
+static int config_input(AVFilterLink *inlink) |
|
864 |
+{ |
|
865 |
+ int ret; |
|
866 |
+ AVFilterContext *ctx = inlink->dst; |
|
867 |
+ FieldMatchContext *fm = ctx->priv; |
|
868 |
+ const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); |
|
869 |
+ const int w = inlink->w; |
|
870 |
+ const int h = inlink->h; |
|
871 |
+ |
|
872 |
+ fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh_flt) / 100.0); |
|
873 |
+ |
|
874 |
+ if ((ret = av_image_alloc(fm->map_data, fm->map_linesize, w, h, inlink->format, 32)) < 0 || |
|
875 |
+ (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0) |
|
876 |
+ return ret; |
|
877 |
+ |
|
878 |
+ fm->hsub = pix_desc->log2_chroma_w; |
|
879 |
+ fm->vsub = pix_desc->log2_chroma_h; |
|
880 |
+ |
|
881 |
+ fm->tpitchy = FFALIGN(w, 16); |
|
882 |
+ fm->tpitchuv = FFALIGN(w >> 1, 16); |
|
883 |
+ |
|
884 |
+ fm->tbuffer = av_malloc(h/2 * fm->tpitchy); |
|
885 |
+ fm->c_array = av_malloc((((w + fm->blockx/2)/fm->blockx)+1) * |
|
886 |
+ (((h + fm->blocky/2)/fm->blocky)+1) * |
|
887 |
+ 4 * sizeof(*fm->c_array)); |
|
888 |
+ if (!fm->tbuffer || !fm->c_array) |
|
889 |
+ return AVERROR(ENOMEM); |
|
890 |
+ |
|
891 |
+ return 0; |
|
892 |
+} |
|
893 |
+ |
|
894 |
+static av_cold int fieldmatch_init(AVFilterContext *ctx) |
|
895 |
+{ |
|
896 |
+ const FieldMatchContext *fm = ctx->priv; |
|
897 |
+ AVFilterPad pad = { |
|
898 |
+ .name = av_strdup("main"), |
|
899 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
900 |
+ .filter_frame = filter_frame, |
|
901 |
+ .config_props = config_input, |
|
902 |
+ }; |
|
903 |
+ |
|
904 |
+ if (!pad.name) |
|
905 |
+ return AVERROR(ENOMEM); |
|
906 |
+ ff_insert_inpad(ctx, INPUT_MAIN, &pad); |
|
907 |
+ |
|
908 |
+ if (fm->ppsrc) { |
|
909 |
+ pad.name = av_strdup("clean_src"); |
|
910 |
+ pad.config_props = NULL; |
|
911 |
+ if (!pad.name) |
|
912 |
+ return AVERROR(ENOMEM); |
|
913 |
+ ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad); |
|
914 |
+ } |
|
915 |
+ |
|
916 |
+ if ((fm->blockx & (fm->blockx - 1)) || |
|
917 |
+ (fm->blocky & (fm->blocky - 1))) { |
|
918 |
+ av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n"); |
|
919 |
+ return AVERROR(EINVAL); |
|
920 |
+ } |
|
921 |
+ |
|
922 |
+ if (fm->combpel > fm->blockx * fm->blocky) { |
|
923 |
+ av_log(ctx, AV_LOG_ERROR, "Combed pixel should not be larger than blockx x blocky\n"); |
|
924 |
+ return AVERROR(EINVAL); |
|
925 |
+ } |
|
926 |
+ |
|
927 |
+ return 0; |
|
928 |
+} |
|
929 |
+ |
|
930 |
+static av_cold void fieldmatch_uninit(AVFilterContext *ctx) |
|
931 |
+{ |
|
932 |
+ int i; |
|
933 |
+ FieldMatchContext *fm = ctx->priv; |
|
934 |
+ |
|
935 |
+ if (fm->prv != fm->src) |
|
936 |
+ av_frame_free(&fm->prv); |
|
937 |
+ if (fm->nxt != fm->src) |
|
938 |
+ av_frame_free(&fm->nxt); |
|
939 |
+ av_frame_free(&fm->src); |
|
940 |
+ av_freep(&fm->map_data[0]); |
|
941 |
+ av_freep(&fm->cmask_data[0]); |
|
942 |
+ av_freep(&fm->tbuffer); |
|
943 |
+ av_freep(&fm->c_array); |
|
944 |
+ for (i = 0; i < ctx->nb_inputs; i++) |
|
945 |
+ av_freep(&ctx->input_pads[i].name); |
|
946 |
+} |
|
947 |
+ |
|
948 |
+static int config_output(AVFilterLink *outlink) |
|
949 |
+{ |
|
950 |
+ AVFilterContext *ctx = outlink->src; |
|
951 |
+ const FieldMatchContext *fm = ctx->priv; |
|
952 |
+ const AVFilterLink *inlink = |
|
953 |
+ ctx->inputs[fm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN]; |
|
954 |
+ |
|
955 |
+ outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; |
|
956 |
+ outlink->time_base = inlink->time_base; |
|
957 |
+ outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; |
|
958 |
+ outlink->frame_rate = inlink->frame_rate; |
|
959 |
+ outlink->w = inlink->w; |
|
960 |
+ outlink->h = inlink->h; |
|
961 |
+ return 0; |
|
962 |
+} |
|
963 |
+ |
|
964 |
+static const AVFilterPad fieldmatch_outputs[] = { |
|
965 |
+ { |
|
966 |
+ .name = "default", |
|
967 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
968 |
+ .request_frame = request_frame, |
|
969 |
+ .config_props = config_output, |
|
970 |
+ }, |
|
971 |
+ { NULL } |
|
972 |
+}; |
|
973 |
+ |
|
974 |
+AVFilter avfilter_vf_fieldmatch = { |
|
975 |
+ .name = "fieldmatch", |
|
976 |
+ .description = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine"), |
|
977 |
+ .query_formats = query_formats, |
|
978 |
+ .priv_size = sizeof(FieldMatchContext), |
|
979 |
+ .init = fieldmatch_init, |
|
980 |
+ .uninit = fieldmatch_uninit, |
|
981 |
+ .inputs = NULL, |
|
982 |
+ .outputs = fieldmatch_outputs, |
|
983 |
+ .priv_class = &fieldmatch_class, |
|
984 |
+ .flags = AVFILTER_FLAG_DYNAMIC_INPUTS, |
|
985 |
+}; |