]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tinterlace.c
lavf: minor bump for avformat_queue_attached_pictures()
[ffmpeg] / libavfilter / vf_tinterlace.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with FFmpeg if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * @file
25  * temporal field interlace filter, ported from MPlayer/libmpcodecs
26  */
27
28 #include "libavutil/imgutils.h"
29 #include "libavutil/avassert.h"
30 #include "avfilter.h"
31 #include "internal.h"
32
33 enum TInterlaceMode {
34     MODE_MERGE = 0,
35     MODE_DROP_EVEN,
36     MODE_DROP_ODD,
37     MODE_PAD,
38     MODE_INTERLEAVE_TOP,
39     MODE_INTERLEAVE_BOTTOM,
40     MODE_INTERLACEX2,
41 };
42
43 static const char *tinterlace_mode_str[] = {
44     "merge",
45     "drop_even",
46     "drop_odd",
47     "pad",
48     "interleave_top",
49     "interleave_bottom",
50     "interlacex2",
51     NULL
52 };
53
54 typedef struct {
55     enum TInterlaceMode mode;   ///< interlace mode selected
56     int frame;                  ///< number of the output frame
57     int vsub;                   ///< chroma vertical subsampling
58     AVFilterBufferRef *cur;
59     AVFilterBufferRef *next;
60     uint8_t *black_data[4];     ///< buffer used to fill padded lines
61     int black_linesize[4];
62 } TInterlaceContext;
63
64 #define FULL_SCALE_YUVJ_FORMATS \
65     PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
66
67 static enum PixelFormat full_scale_yuvj_pix_fmts[] = {
68     FULL_SCALE_YUVJ_FORMATS, PIX_FMT_NONE
69 };
70
71 static int query_formats(AVFilterContext *ctx)
72 {
73     static const enum PixelFormat pix_fmts[] = {
74         PIX_FMT_YUV420P,  PIX_FMT_YUV422P,  PIX_FMT_YUV444P,
75         PIX_FMT_YUV444P,  PIX_FMT_YUV410P,  PIX_FMT_YUVA420P,
76         PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
77         PIX_FMT_NONE
78     };
79
80     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
81     return 0;
82 }
83
84 static av_cold int init(AVFilterContext *ctx, const char *args)
85 {
86     TInterlaceContext *tinterlace = ctx->priv;
87     int i;
88     char c;
89
90     tinterlace->mode = MODE_MERGE;
91
92     if (args) {
93         if (sscanf(args, "%d%c", (int *)&tinterlace->mode, &c) == 1) {
94             if (tinterlace->mode > 6) {
95                 av_log(ctx, AV_LOG_ERROR,
96                        "Invalid mode '%s', use an integer between 0 and 6\n", args);
97                 return AVERROR(EINVAL);
98             }
99
100             av_log(ctx, AV_LOG_WARNING,
101                    "Using numeric constant is deprecated, use symbolic values\n");
102         } else {
103             for (i = 0; tinterlace_mode_str[i]; i++) {
104                 if (!strcmp(tinterlace_mode_str[i], args)) {
105                     tinterlace->mode = i;
106                     break;
107                 }
108             }
109             if (!tinterlace_mode_str[i]) {
110                 av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'\n", args);
111                 return AVERROR(EINVAL);
112             }
113         }
114     }
115
116     return 0;
117 }
118
119 static av_cold void uninit(AVFilterContext *ctx)
120 {
121     TInterlaceContext *tinterlace = ctx->priv;
122
123     if (tinterlace->cur ) avfilter_unref_bufferp(&tinterlace->cur );
124     if (tinterlace->next) avfilter_unref_bufferp(&tinterlace->next);
125
126     av_freep(&tinterlace->black_data[0]);
127 }
128
129 static int config_out_props(AVFilterLink *outlink)
130 {
131     AVFilterContext *ctx = outlink->src;
132     AVFilterLink *inlink = outlink->src->inputs[0];
133     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[outlink->format];
134     TInterlaceContext *tinterlace = ctx->priv;
135
136     tinterlace->vsub = desc->log2_chroma_h;
137     outlink->w = inlink->w;
138     outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
139         inlink->h*2 : inlink->h;
140
141     if (tinterlace->mode == MODE_PAD) {
142         uint8_t black[4] = { 16, 128, 128, 16 };
143         int i, ret;
144         if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
145             black[0] = black[3] = 0;
146         ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
147                              outlink->w, outlink->h, outlink->format, 1);
148         if (ret < 0)
149             return ret;
150
151         /* fill black picture with black */
152         for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
153             int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
154             memset(tinterlace->black_data[i], black[i],
155                    tinterlace->black_linesize[i] * h);
156         }
157     }
158     av_log(ctx, AV_LOG_VERBOSE, "mode:%s h:%d -> h:%d\n",
159            tinterlace_mode_str[tinterlace->mode], inlink->h, outlink->h);
160
161     return 0;
162 }
163
164 #define FIELD_UPPER           0
165 #define FIELD_LOWER           1
166 #define FIELD_UPPER_AND_LOWER 2
167
168 /**
169  * Copy picture field from src to dst.
170  *
171  * @param src_field copy from upper, lower field or both
172  * @param interleave leave a padding line between each copied line
173  * @param dst_field copy to upper or lower field,
174  *        only meaningful when interleave is selected
175  */
176 static inline
177 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
178                         uint8_t *src[4], int src_linesize[4],
179                         enum PixelFormat format, int w, int src_h,
180                         int src_field, int interleave, int dst_field)
181 {
182     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
183     int plane, vsub = desc->log2_chroma_h;
184     int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
185
186     for (plane = 0; plane < desc->nb_components; plane++) {
187         int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
188         int linesize = av_image_get_linesize(format, w, plane);
189         uint8_t *dstp = dst[plane];
190         uint8_t *srcp = src[plane];
191         lines /= k;
192         if (src_field == FIELD_LOWER)
193             srcp += src_linesize[plane];
194         if (interleave && dst_field == FIELD_LOWER)
195             dstp += dst_linesize[plane];
196         av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
197                             srcp, src_linesize[plane]*k, linesize, lines);
198     }
199 }
200
201 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
202 {
203     AVFilterContext *ctx = inlink->dst;
204     TInterlaceContext *tinterlace = ctx->priv;
205
206     avfilter_unref_buffer(tinterlace->cur);
207     tinterlace->cur  = tinterlace->next;
208     tinterlace->next = picref;
209     return 0;
210 }
211
212 static int end_frame(AVFilterLink *inlink)
213 {
214     AVFilterContext *ctx = inlink->dst;
215     AVFilterLink *outlink = ctx->outputs[0];
216     TInterlaceContext *tinterlace = ctx->priv;
217     AVFilterBufferRef *cur  = tinterlace->cur;
218     AVFilterBufferRef *next = tinterlace->next;
219     AVFilterBufferRef *out  = NULL;
220     int field, tff;
221
222     /* we need at least two frames */
223     if (!tinterlace->cur)
224         return 0;
225
226     switch (tinterlace->mode) {
227     case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
228              * the lower field, generating a double-height video at half framerate */
229         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
230         avfilter_copy_buffer_ref_props(out, cur);
231         out->video->h = outlink->h;
232         out->video->interlaced = 1;
233         out->video->top_field_first = 1;
234
235         /* write odd frame lines into the upper field of the new frame */
236         copy_picture_field(out->data, out->linesize,
237                            cur->data, cur->linesize,
238                            inlink->format, inlink->w, inlink->h,
239                            FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
240         /* write even frame lines into the lower field of the new frame */
241         copy_picture_field(out->data, out->linesize,
242                            next->data, next->linesize,
243                            inlink->format, inlink->w, inlink->h,
244                            FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
245         avfilter_unref_bufferp(&tinterlace->next);
246         break;
247
248     case MODE_DROP_ODD:  /* only output even frames, odd  frames are dropped; height unchanged, half framerate */
249     case MODE_DROP_EVEN: /* only output odd  frames, even frames are dropped; height unchanged, half framerate */
250         out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
251         avfilter_unref_bufferp(&tinterlace->next);
252         break;
253
254     case MODE_PAD: /* expand each frame to double height, but pad alternate
255                     * lines with black; framerate unchanged */
256         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
257         avfilter_copy_buffer_ref_props(out, cur);
258         out->video->h = outlink->h;
259
260         field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
261         /* copy upper and lower fields */
262         copy_picture_field(out->data, out->linesize,
263                            cur->data, cur->linesize,
264                            inlink->format, inlink->w, inlink->h,
265                            FIELD_UPPER_AND_LOWER, 1, field);
266         /* pad with black the other field */
267         copy_picture_field(out->data, out->linesize,
268                            tinterlace->black_data, tinterlace->black_linesize,
269                            inlink->format, inlink->w, inlink->h,
270                            FIELD_UPPER_AND_LOWER, 1, !field);
271         break;
272
273         /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
274          * halving the frame rate and preserving image height */
275     case MODE_INTERLEAVE_TOP:    /* top    field first */
276     case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
277         tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
278         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
279         avfilter_copy_buffer_ref_props(out, cur);
280         out->video->interlaced = 1;
281         out->video->top_field_first = tff;
282
283         /* copy upper/lower field from cur */
284         copy_picture_field(out->data, out->linesize,
285                            cur->data, cur->linesize,
286                            inlink->format, inlink->w, inlink->h,
287                            tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
288         /* copy lower/upper field from next */
289         copy_picture_field(out->data, out->linesize,
290                            next->data, next->linesize,
291                            inlink->format, inlink->w, inlink->h,
292                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
293         avfilter_unref_bufferp(&tinterlace->next);
294         break;
295     case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
296         /* output current frame first */
297         out = avfilter_ref_buffer(cur, AV_PERM_READ);
298         out->video->interlaced = 1;
299
300         ff_start_frame(outlink, out);
301         ff_draw_slice(outlink, 0, outlink->h, 1);
302         ff_end_frame(outlink);
303
304         /* output mix of current and next frame */
305         tff = next->video->top_field_first;
306         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
307         avfilter_copy_buffer_ref_props(out, next);
308         out->video->interlaced = 1;
309
310         /* write current frame second field lines into the second field of the new frame */
311         copy_picture_field(out->data, out->linesize,
312                            cur->data, cur->linesize,
313                            inlink->format, inlink->w, inlink->h,
314                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
315         /* write next frame first field lines into the first field of the new frame */
316         copy_picture_field(out->data, out->linesize,
317                            next->data, next->linesize,
318                            inlink->format, inlink->w, inlink->h,
319                            tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
320         break;
321     }
322
323     ff_start_frame(outlink, out);
324     ff_draw_slice(outlink, 0, outlink->h, 1);
325     ff_end_frame(outlink);
326
327     tinterlace->frame++;
328
329     return 0;
330 }
331
332 static int poll_frame(AVFilterLink *outlink)
333 {
334     TInterlaceContext *tinterlace = outlink->src->priv;
335     AVFilterLink *inlink = outlink->src->inputs[0];
336     int ret, val;
337
338     val = ff_poll_frame(inlink);
339
340     if (val == 1 && !tinterlace->next) {
341         if ((ret = ff_request_frame(inlink)) < 0)
342             return ret;
343         val = ff_poll_frame(inlink);
344     }
345     av_assert0(tinterlace->next);
346
347     return val;
348 }
349
350 static int request_frame(AVFilterLink *outlink)
351 {
352     TInterlaceContext *tinterlace = outlink->src->priv;
353     AVFilterLink *inlink = outlink->src->inputs[0];
354
355     do {
356         int ret;
357
358         if ((ret = ff_request_frame(inlink)) < 0)
359             return ret;
360     } while (!tinterlace->cur);
361
362     return 0;
363 }
364
365 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
366
367 AVFilter avfilter_vf_tinterlace = {
368     .name          = "tinterlace",
369     .description   = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
370     .priv_size     = sizeof(TInterlaceContext),
371     .init          = init,
372     .uninit        = uninit,
373     .query_formats = query_formats,
374
375     .inputs = (const AVFilterPad[]) {
376         { .name          = "default",
377           .type          = AVMEDIA_TYPE_VIDEO,
378           .start_frame   = start_frame,
379           .draw_slice    = null_draw_slice,
380           .end_frame     = end_frame, },
381         { .name = NULL}
382     },
383     .outputs = (const AVFilterPad[]) {
384         { .name          = "default",
385           .type          = AVMEDIA_TYPE_VIDEO,
386           .config_props  = config_out_props,
387           .poll_frame    = poll_frame,
388           .request_frame = request_frame },
389         { .name = NULL}
390     },
391 };