1 /*
2 * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
3 * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
4 * Based on the process described by Martin Weston for BBC R&D
5 * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
33
36 int filter;
///< 0 is simple, 1 is more complex
37 int deint;
///< which frames to deinterlace
38 int linesize[4];
///< bytes of pixel data per line for each plane
40 int field;
///< which field are we on, 0 or 1
47
50
51 #define OFFSET(x) offsetof(W3FDIFContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
54
60 CONST(
"all",
"deinterlace all frames", 0,
"deint"),
61 CONST(
"interlaced",
"only deinterlace frames marked as interlaced", 1,
"deint"),
63 };
64
66
68 {
85 };
86
88 if (!fmts_list)
91 }
92
95 const int16_t *coef, int linesize)
96 {
97 int i;
98
99 for (i = 0; i < linesize; i++) {
100 *work_line = *in_lines_cur[0]++ * coef[0];
101 *work_line++ += *in_lines_cur[1]++ * coef[1];
102 }
103 }
104
107 const int16_t *coef, int linesize)
108 {
109 int i;
110
111 for (i = 0; i < linesize; i++) {
112 *work_line = *in_lines_cur[0]++ * coef[0];
113 *work_line += *in_lines_cur[1]++ * coef[1];
114 *work_line += *in_lines_cur[2]++ * coef[2];
115 *work_line++ += *in_lines_cur[3]++ * coef[3];
116 }
117 }
118
122 const int16_t *coef, int linesize)
123 {
124 int i;
125
126 for (i = 0; i < linesize; i++) {
127 *work_line += *in_lines_cur[0]++ * coef[0];
128 *work_line += *in_lines_adj[0]++ * coef[0];
129 *work_line += *in_lines_cur[1]++ * coef[1];
130 *work_line += *in_lines_adj[1]++ * coef[1];
131 *work_line += *in_lines_cur[2]++ * coef[2];
132 *work_line++ += *in_lines_adj[2]++ * coef[2];
133 }
134 }
135
139 const int16_t *coef, int linesize)
140 {
141 int i;
142
143 for (i = 0; i < linesize; i++) {
144 *work_line += *in_lines_cur[0]++ * coef[0];
145 *work_line += *in_lines_adj[0]++ * coef[0];
146 *work_line += *in_lines_cur[1]++ * coef[1];
147 *work_line += *in_lines_adj[1]++ * coef[1];
148 *work_line += *in_lines_cur[2]++ * coef[2];
149 *work_line += *in_lines_adj[2]++ * coef[2];
150 *work_line += *in_lines_cur[3]++ * coef[3];
151 *work_line += *in_lines_adj[3]++ * coef[3];
152 *work_line += *in_lines_cur[4]++ * coef[4];
153 *work_line++ += *in_lines_adj[4]++ * coef[4];
154 }
155 }
156
158 {
159 int j;
160
161 for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
162 *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
163 }
164
167 const int16_t *coef, int linesize)
168 {
169 uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] };
170 int i;
171
172 linesize /= 2;
173 for (i = 0; i < linesize; i++) {
174 *work_line = *in_lines_cur[0]++ * coef[0];
175 *work_line++ += *in_lines_cur[1]++ * coef[1];
176 }
177 }
178
181 const int16_t *coef, int linesize)
182 {
183 uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0],
184 (uint16_t *)in_lines_cur8[1],
185 (uint16_t *)in_lines_cur8[2],
186 (uint16_t *)in_lines_cur8[3] };
187 int i;
188
189 linesize /= 2;
190 for (i = 0; i < linesize; i++) {
191 *work_line = *in_lines_cur[0]++ * coef[0];
192 *work_line += *in_lines_cur[1]++ * coef[1];
193 *work_line += *in_lines_cur[2]++ * coef[2];
194 *work_line++ += *in_lines_cur[3]++ * coef[3];
195 }
196 }
197
201 const int16_t *coef, int linesize)
202 {
203 uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0],
204 (uint16_t *)in_lines_cur8[1],
205 (uint16_t *)in_lines_cur8[2] };
206 uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0],
207 (uint16_t *)in_lines_adj8[1],
208 (uint16_t *)in_lines_adj8[2] };
209 int i;
210
211 linesize /= 2;
212 for (i = 0; i < linesize; i++) {
213 *work_line += *in_lines_cur[0]++ * coef[0];
214 *work_line += *in_lines_adj[0]++ * coef[0];
215 *work_line += *in_lines_cur[1]++ * coef[1];
216 *work_line += *in_lines_adj[1]++ * coef[1];
217 *work_line += *in_lines_cur[2]++ * coef[2];
218 *work_line++ += *in_lines_adj[2]++ * coef[2];
219 }
220 }
221
225 const int16_t *coef, int linesize)
226 {
227 uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0],
228 (uint16_t *)in_lines_cur8[1],
229 (uint16_t *)in_lines_cur8[2],
230 (uint16_t *)in_lines_cur8[3],
231 (uint16_t *)in_lines_cur8[4] };
232 uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0],
233 (uint16_t *)in_lines_adj8[1],
234 (uint16_t *)in_lines_adj8[2],
235 (uint16_t *)in_lines_adj8[3],
236 (uint16_t *)in_lines_adj8[4] };
237 int i;
238
239 linesize /= 2;
240 for (i = 0; i < linesize; i++) {
241 *work_line += *in_lines_cur[0]++ * coef[0];
242 *work_line += *in_lines_adj[0]++ * coef[0];
243 *work_line += *in_lines_cur[1]++ * coef[1];
244 *work_line += *in_lines_adj[1]++ * coef[1];
245 *work_line += *in_lines_cur[2]++ * coef[2];
246 *work_line += *in_lines_adj[2]++ * coef[2];
247 *work_line += *in_lines_cur[3]++ * coef[3];
248 *work_line += *in_lines_adj[3]++ * coef[3];
249 *work_line += *in_lines_cur[4]++ * coef[4];
250 *work_line++ += *in_lines_adj[4]++ * coef[4];
251 }
252 }
253
255 {
256 uint16_t *out_pixel = (uint16_t *)out_pixel8;
257 int j;
258
259 linesize /= 2;
260 for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
261 *out_pixel = av_clip(*work_pixel, 0, max) >> 15;
262 }
263
265 {
269 int ret, i, depth;
270
272 return ret;
273
276
282
287 }
288
290 s->
max = ((1 << depth) - 1) * 256 * 128;
291 if (depth <= 8) {
297 } else {
303 }
304
305 if (ARCH_X86)
307
308 return 0;
309 }
310
312 {
314
319
320 return 0;
321 }
322
323 /*
324 * Filter coefficients from PH-2071, scaled by 256 * 128.
325 * Each set of coefficients has a set for low-frequencies and high-frequencies.
326 * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
327 * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
328 * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
329 * and high-frequencies for simple and more-complex mode.
330 */
332 static const int16_t
coef_lf[2][4] = {{ 16384, 16384, 0, 0},
333 { -852, 17236, 17236, -852}};
335 static const int16_t
coef_hf[2][5] = {{ -2048, 4096, -2048, 0, 0},
336 { 1016, -3801, 5570, -3801, 1016}};
337
342
344 {
352 uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
354 int32_t *work_line, *work_pixel;
363 const int start = (height * jobnr) / nb_jobs;
364 const int end = (height * (jobnr+1)) / nb_jobs;
365 const int max = s->
max;
366 int j, y_in, y_out;
367
368 /* copy unchanged the lines of the field */
370
371 in_line = cur_data + (y_out * cur_line_stride);
372 out_line = dst_data + (y_out * dst_line_stride);
373
374 while (y_out < end) {
375 memcpy(out_line, in_line, linesize);
376 y_out += 2;
377 in_line += cur_line_stride * 2;
378 out_line += dst_line_stride * 2;
379 }
380
381 /* interpolate other lines of the field */
383
384 out_line = dst_data + (y_out * dst_line_stride);
385
386 while (y_out < end) {
387 /* get low vertical frequencies from current field */
388 for (j = 0; j < n_coef_lf[
filter]; j++) {
389 y_in = (y_out + 1) + (j * 2) - n_coef_lf[
filter];
390
391 while (y_in < 0)
392 y_in += 2;
393 while (y_in >= height)
394 y_in -= 2;
395
396 in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
397 }
398
400 switch (n_coef_lf[filter]) {
401 case 2:
403 coef_lf[filter], linesize);
404 break;
405 case 4:
407 coef_lf[filter], linesize);
408 }
409
410 /* get high vertical frequencies from adjacent fields */
411 for (j = 0; j < n_coef_hf[
filter]; j++) {
412 y_in = (y_out + 1) + (j * 2) - n_coef_hf[
filter];
413
414 while (y_in < 0)
415 y_in += 2;
416 while (y_in >= height)
417 y_in -= 2;
418
419 in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
420 in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
421 }
422
424 switch (n_coef_hf[filter]) {
425 case 3:
427 coef_hf[filter], linesize);
428 break;
429 case 5:
431 coef_hf[filter], linesize);
432 }
433
434 /* save scaled result to the output frame, scaling down by 256 * 128 */
436 out_pixel = out_line;
437
439
440 /* move on to next line */
441 y_out += 2;
442 out_line += dst_line_stride * 2;
443 }
444
445 return 0;
446 }
447
449 {
455
457 if (!out)
461
462 if (!is_second) {
465 } else {
466 int64_t cur_pts = s->
cur->
pts;
467 int64_t next_pts = s->
next->
pts;
468
470 out->
pts = cur_pts + next_pts;
471 } else {
473 }
474 }
475
478 for (plane = 0; plane < s->
nb_planes; plane++) {
481 }
482
484
486 }
487
489 {
492 int ret;
493
498
503 }
504
507 if (!out)
509
514 }
515
517 return 0;
518
520 if (ret < 0)
521 return ret;
522
524 }
525
527 {
530 int ret;
531
534
536
539 if (!next)
544 } else if (ret < 0) {
545 return ret;
546 }
547
548 return 0;
549 }
550
552 {
554 int i;
555
559
562
564 }
565
567 {
572 },
574 };
575
577 {
582 },
584 };
585
590 .priv_class = &w3fdif_class,
596 };
static int filter(AVFilterContext *ctx, int is_second)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
#define AV_PIX_FMT_YUV444P14
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Main libavfilter public API header.
static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max)
int h
agreed upon image height
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_YUV420P12
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
int is_disabled
the enabled state from the last expression evaluation
static void filter_complex_low(int32_t *work_line, uint8_t *in_lines_cur[4], const int16_t *coef, int linesize)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
void(* filter_scale)(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
const char * name
Pad name.
static const AVOption w3fdif_options[]
AVFilterLink ** inputs
array of pointers to input links
static int config_input(AVFilterLink *inlink)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
void(* filter_simple_high)(int32_t *work_line, uint8_t *in_lines_cur[3], uint8_t *in_lines_adj[3], const int16_t *coef, int linesize)
AVFILTER_DEFINE_CLASS(w3fdif)
static av_cold int end(AVCodecContext *avctx)
void(* filter_complex_high)(int32_t *work_line, uint8_t *in_lines_cur[5], uint8_t *in_lines_adj[5], const int16_t *coef, int linesize)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static void filter16_complex_high(int32_t *work_line, uint8_t *in_lines_cur8[5], uint8_t *in_lines_adj8[5], const int16_t *coef, int linesize)
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
static void filter_complex_high(int32_t *work_line, uint8_t *in_lines_cur[5], uint8_t *in_lines_adj[5], const int16_t *coef, int linesize)
#define AVERROR_EOF
End of file.
int interlaced_frame
The content of the picture is interlaced.
#define AV_PIX_FMT_YUV422P12
A filter pad used for either input or output.
int linesize[4]
bytes of pixel data per line for each plane
A link between two filters.
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
AVFrame * next
previous, current, next frames
static int query_formats(AVFilterContext *ctx)
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable; if left to 0/0, will be automatically copied from the first input of the source filter if it exists.
static const int16_t coef_hf[2][5]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
void(* filter_complex_low)(int32_t *work_line, uint8_t *in_lines_cur[4], const int16_t *coef, int linesize)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
#define AV_PIX_FMT_YUV444P10
int planeheight[4]
height of each plane
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int w
agreed upon image width
#define AV_PIX_FMT_YUV422P9
static void filter16_complex_low(int32_t *work_line, uint8_t *in_lines_cur8[4], const int16_t *coef, int linesize)
void(* filter_simple_low)(int32_t *work_line, uint8_t *in_lines_cur[2], const int16_t *coef, int linesize)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
int field
which field are we on, 0 or 1
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
static const int16_t coef_lf[2][4]
AVFilterContext * src
source filter
static const int8_t n_coef_hf[2]
static const AVFilterPad inputs[]
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_GBRP14
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
static const AVFilterPad outputs[]
int format
agreed upon media format
static int config_output(AVFilterLink *outlink)
#define AV_PIX_FMT_YUV420P14
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static const int8_t n_coef_lf[2]
#define AV_PIX_FMT_YUV420P10
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Describe the class of an AVClass context structure.
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
const char * name
Filter name.
#define AV_PIX_FMT_YUV420P9
static void filter16_simple_high(int32_t *work_line, uint8_t *in_lines_cur8[3], uint8_t *in_lines_adj8[3], const int16_t *coef, int linesize)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
AVFilterLink ** outputs
array of pointers to output links
static enum AVPixelFormat pix_fmts[]
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_GBRP12
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV444P12
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define CONST(name, help, val, unit)
static const AVFilterPad w3fdif_inputs[]
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal and external API header
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
planar GBRA 4:4:4:4 32bpp
int deint
which frames to deinterlace
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
avfilter_execute_func * execute
int top_field_first
If the content is interlaced, is top field displayed first.
AVFilterContext * dst
dest filter
static const AVFilterPad w3fdif_outputs[]
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
static void filter16_simple_low(int32_t *work_line, uint8_t *in_lines_cur8[2], const int16_t *coef, int linesize)
static av_cold void uninit(AVFilterContext *ctx)
void ff_w3fdif_init_x86(W3FDIFDSPContext *dsp, int depth)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
static void filter_simple_low(int32_t *work_line, uint8_t *in_lines_cur[2], const int16_t *coef, int linesize)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
int32_t ** work_line
lines we are calculating
static int request_frame(AVFilterLink *outlink)
int depth
Number of bits in the component.
AVPixelFormat
Pixel format.
int filter
0 is simple, 1 is more complex
static void filter_simple_high(int32_t *work_line, uint8_t *in_lines_cur[3], uint8_t *in_lines_adj[3], const int16_t *coef, int linesize)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define AV_CEIL_RSHIFT(a, b)