Browse code

Merge commit 'a7d0e7ead95e584c4866617f046b2493066975c6'

* commit 'a7d0e7ead95e584c4866617f046b2493066975c6':
lavfi: add framepack filter

Conflicts:
Changelog
doc/filters.texi
libavfilter/allfilters.c
libavfilter/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2014/01/10 10:53:15
Showing 6 changed files
... ...
@@ -18,6 +18,7 @@ version <next>
18 18
 - ATRAC3+ decoder
19 19
 - VP8 in Ogg demuxing
20 20
 - side & metadata support in NUT
21
+- framepack filter
21 22
 
22 23
 
23 24
 version 2.1:
... ...
@@ -4521,6 +4521,51 @@ fps=fps=film:round=near
4521 4521
 @end example
4522 4522
 @end itemize
4523 4523
 
4524
+@section framepack
4525
+
4526
+Pack two different video streams into a stereoscopic video, setting proper
4527
+metadata on supported codecs. The two views should have the same size and
4528
+framerate and processing will stop when the shorter video ends. Please note
4529
+that you may conveniently adjust view properties with the @ref{scale} and
4530
+@ref{fps} filters.
4531
+
4532
+This filter accepts the following named parameters:
4533
+@table @option
4534
+
4535
+@item format
4536
+Desired packing format. Supported values are:
4537
+
4538
+@table @option
4539
+
4540
+@item sbs
4541
+Views are next to each other (default).
4542
+
4543
+@item tab
4544
+Views are on top of each other.
4545
+
4546
+@item lines
4547
+Views are packed by line.
4548
+
4549
+@item columns
4550
+Views are eacked by column.
4551
+
4552
+@item frameseq
4553
+Views are temporally interleaved.
4554
+
4555
+@end table
4556
+
4557
+@end table
4558
+
4559
+Some examples follow:
4560
+
4561
+@example
4562
+# Convert left and right views into a frame sequential video.
4563
+ffmpeg -i LEFT -i RIGHT -filter_complex framepack=frameseq OUTPUT
4564
+
4565
+# Convert views into a side-by-side video with the same output resolution as the input.
4566
+ffmpeg -i LEFT -i RIGHT -filter_complex [0:v]scale=w=iw/2[left],[1:v]scale=w=iw/2[right],[left][right]framepack=sbs OUTPUT
4567
+@end example
4568
+
4524 4569
 @section framestep
4525 4570
 
4526 4571
 Select one frame every N-th frame.
... ...
@@ -140,6 +140,7 @@ OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
140 140
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
141 141
 OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
142 142
 OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
143
+OBJS-$(CONFIG_FRAMEPACK_FILTER)              += vf_framepack.o
143 144
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
144 145
 OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
145 146
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
... ...
@@ -136,6 +136,7 @@ void avfilter_register_all(void)
136 136
     REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
137 137
     REGISTER_FILTER(FORMAT,         format,         vf);
138 138
     REGISTER_FILTER(FPS,            fps,            vf);
139
+    REGISTER_FILTER(FRAMEPACK,      framepack,      vf);
139 140
     REGISTER_FILTER(FRAMESTEP,      framestep,      vf);
140 141
     REGISTER_FILTER(FREI0R,         frei0r,         vf);
141 142
     REGISTER_FILTER(GEQ,            geq,            vf);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   4
33
-#define LIBAVFILTER_VERSION_MINOR   0
34
-#define LIBAVFILTER_VERSION_MICRO 103
33
+#define LIBAVFILTER_VERSION_MINOR   1
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \
38 38
new file mode 100644
... ...
@@ -0,0 +1,363 @@
0
+/*
1
+ * Copyright (c) 2013 Vittorio Giovara
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Generate a frame packed video, by combining two views in a single surface.
23
+ */
24
+
25
+#include <string.h>
26
+
27
+#include "libavutil/imgutils.h"
28
+#include "libavutil/opt.h"
29
+#include "libavutil/pixdesc.h"
30
+#include "libavutil/rational.h"
31
+#include "libavutil/stereo3d.h"
32
+
33
+#include "avfilter.h"
34
+#include "formats.h"
35
+#include "internal.h"
36
+#include "video.h"
37
+
38
+#define LEFT  0
39
+#define RIGHT 1
40
+
41
+typedef struct FramepackContext {
42
+    const AVClass *class;
43
+
44
+    const AVPixFmtDescriptor *pix_desc; ///< agreed pixel format
45
+
46
+    enum AVStereo3DType format;         ///< frame pack type output
47
+
48
+    AVFrame *input_views[2];            ///< input frames
49
+
50
+    int64_t double_pts;                 ///< new pts for frameseq mode
51
+} FramepackContext;
52
+
53
+static const enum AVPixelFormat formats_supported[] = {
54
+    AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
55
+    AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVJ420P,
56
+    AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
57
+    AV_PIX_FMT_NONE
58
+};
59
+
60
+static int query_formats(AVFilterContext *ctx)
61
+{
62
+    // this will ensure that formats are the same on all pads
63
+    ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
64
+    return 0;
65
+}
66
+
67
+static av_cold void framepack_uninit(AVFilterContext *ctx)
68
+{
69
+    FramepackContext *s = ctx->priv;
70
+
71
+    // clean any leftover frame
72
+    av_frame_free(&s->input_views[LEFT]);
73
+    av_frame_free(&s->input_views[RIGHT]);
74
+}
75
+
76
+static int config_output(AVFilterLink *outlink)
77
+{
78
+    AVFilterContext *ctx = outlink->src;
79
+    FramepackContext *s  = outlink->src->priv;
80
+
81
+    int width            = ctx->inputs[LEFT]->w;
82
+    int height           = ctx->inputs[LEFT]->h;
83
+    AVRational time_base = ctx->inputs[LEFT]->time_base;
84
+
85
+    // check size and fps match on the other input
86
+    if (width  != ctx->inputs[RIGHT]->w ||
87
+        height != ctx->inputs[RIGHT]->h) {
88
+        av_log(ctx, AV_LOG_ERROR,
89
+               "Left and right sizes differ (%dx%d vs %dx%d).\n",
90
+               width, height,
91
+               ctx->inputs[RIGHT]->w, ctx->inputs[RIGHT]->h);
92
+        return AVERROR_INVALIDDATA;
93
+    } else if (av_cmp_q(time_base, ctx->inputs[RIGHT]->time_base) != 0) {
94
+        av_log(ctx, AV_LOG_ERROR,
95
+               "Left and right framerates differ (%d/%d vs %d/%d).\n",
96
+               time_base.num, time_base.den,
97
+               ctx->inputs[RIGHT]->time_base.num,
98
+               ctx->inputs[RIGHT]->time_base.den);
99
+        return AVERROR_INVALIDDATA;
100
+    }
101
+
102
+    s->pix_desc = av_pix_fmt_desc_get(outlink->format);
103
+    if (!s->pix_desc)
104
+        return AVERROR_BUG;
105
+
106
+    // modify output properties as needed
107
+    switch (s->format) {
108
+    case AV_STEREO3D_FRAMESEQUENCE:
109
+        time_base.den *= 2;
110
+        s->double_pts = AV_NOPTS_VALUE;
111
+        break;
112
+    case AV_STEREO3D_COLUMNS:
113
+    case AV_STEREO3D_SIDEBYSIDE:
114
+        width *= 2;
115
+        break;
116
+    case AV_STEREO3D_LINES:
117
+    case AV_STEREO3D_TOPBOTTOM:
118
+        height *= 2;
119
+        break;
120
+    default:
121
+        av_log(ctx, AV_LOG_ERROR, "Unknown packing mode.");
122
+        return AVERROR_INVALIDDATA;
123
+    }
124
+
125
+    outlink->w         = width;
126
+    outlink->h         = height;
127
+    outlink->time_base = time_base;
128
+
129
+    return 0;
130
+}
131
+
132
+static void horizontal_frame_pack(FramepackContext *s,
133
+                                  AVFrame *dst,
134
+                                  int interleaved)
135
+{
136
+    int plane, i;
137
+    int length = dst->width / 2;
138
+    int lines  = dst->height;
139
+
140
+    for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
141
+        const uint8_t *leftp  = s->input_views[LEFT]->data[plane];
142
+        const uint8_t *rightp = s->input_views[RIGHT]->data[plane];
143
+        uint8_t *dstp         = dst->data[plane];
144
+
145
+        if (plane == 1 || plane == 2) {
146
+            length = -(-(dst->width / 2) >> s->pix_desc->log2_chroma_w);
147
+            lines  = -(-(dst->height)    >> s->pix_desc->log2_chroma_h);
148
+        }
149
+
150
+        if (interleaved) {
151
+            for (i = 0; i < lines; i++) {
152
+                int j;
153
+                int k = 0;
154
+
155
+                for (j = 0; j < length; j++) {
156
+                    dstp[k++] = leftp[j];
157
+                    dstp[k++] = rightp[j];
158
+                }
159
+
160
+                dstp   += dst->linesize[plane];
161
+                leftp  += s->input_views[LEFT]->linesize[plane];
162
+                rightp += s->input_views[RIGHT]->linesize[plane];
163
+            }
164
+        } else {
165
+            av_image_copy_plane(dst->data[plane], dst->linesize[plane],
166
+                                leftp, s->input_views[LEFT]->linesize[plane],
167
+                                length, lines);
168
+            av_image_copy_plane(dst->data[plane] + length, dst->linesize[plane],
169
+                                rightp, s->input_views[RIGHT]->linesize[plane],
170
+                                length, lines);
171
+        }
172
+    }
173
+}
174
+
175
+static void vertical_frame_pack(FramepackContext *s,
176
+                                AVFrame *dst,
177
+                                int interleaved)
178
+{
179
+    int plane, offset;
180
+    int length = dst->width;
181
+    int lines  = dst->height / 2;
182
+
183
+    for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
184
+        if (plane == 1 || plane == 2) {
185
+            length = -(-(dst->width)      >> s->pix_desc->log2_chroma_w);
186
+            lines  = -(-(dst->height / 2) >> s->pix_desc->log2_chroma_h);
187
+        }
188
+
189
+        offset = interleaved ? dst->linesize[plane] : dst->linesize[plane] * lines;
190
+
191
+        av_image_copy_plane(dst->data[plane],
192
+                            dst->linesize[plane] << interleaved,
193
+                            s->input_views[LEFT]->data[plane],
194
+                            s->input_views[LEFT]->linesize[plane],
195
+                            length, lines);
196
+        av_image_copy_plane(dst->data[plane] + offset,
197
+                            dst->linesize[plane] << interleaved,
198
+                            s->input_views[RIGHT]->data[plane],
199
+                            s->input_views[RIGHT]->linesize[plane],
200
+                            length, lines);
201
+    }
202
+}
203
+
204
+static av_always_inline void spatial_frame_pack(FramepackContext *s, AVFrame *dst)
205
+{
206
+    switch (s->format) {
207
+    case AV_STEREO3D_SIDEBYSIDE:
208
+        horizontal_frame_pack(s, dst, 0);
209
+        break;
210
+    case AV_STEREO3D_COLUMNS:
211
+        horizontal_frame_pack(s, dst, 1);
212
+        break;
213
+    case AV_STEREO3D_TOPBOTTOM:
214
+        vertical_frame_pack(s, dst, 0);
215
+        break;
216
+    case AV_STEREO3D_LINES:
217
+        vertical_frame_pack(s, dst, 1);
218
+        break;
219
+    }
220
+}
221
+
222
+static int filter_frame_left(AVFilterLink *inlink, AVFrame *frame)
223
+{
224
+    FramepackContext *s = inlink->dst->priv;
225
+    s->input_views[LEFT] = frame;
226
+    return 0;
227
+}
228
+
229
+static int filter_frame_right(AVFilterLink *inlink, AVFrame *frame)
230
+{
231
+    FramepackContext *s = inlink->dst->priv;
232
+    s->input_views[RIGHT] = frame;
233
+    return 0;
234
+}
235
+
236
+static int request_frame(AVFilterLink *outlink)
237
+{
238
+    AVFilterContext *ctx = outlink->src;
239
+    FramepackContext *s = ctx->priv;
240
+    AVStereo3D *stereo;
241
+    int ret, i;
242
+
243
+    /* get a frame on the either input, stop as soon as a video ends */
244
+    for (i = 0; i < 2; i++) {
245
+        if (!s->input_views[i]) {
246
+            ret = ff_request_frame(ctx->inputs[i]);
247
+            if (ret < 0)
248
+                return ret;
249
+        }
250
+    }
251
+
252
+    if (s->format == AV_STEREO3D_FRAMESEQUENCE) {
253
+        if (s->double_pts == AV_NOPTS_VALUE)
254
+            s->double_pts = s->input_views[LEFT]->pts;
255
+
256
+        for (i = 0; i < 2; i++) {
257
+            // set correct timestamps
258
+            s->input_views[i]->pts = s->double_pts++;
259
+
260
+            // set stereo3d side data
261
+            stereo = av_stereo3d_create_side_data(s->input_views[i]);
262
+            if (!stereo)
263
+                return AVERROR(ENOMEM);
264
+            stereo->type = s->format;
265
+
266
+            // filter the frame and immediately relinquish its pointer
267
+            ret = ff_filter_frame(outlink, s->input_views[i]);
268
+            s->input_views[i] = NULL;
269
+            if (ret < 0)
270
+                return ret;
271
+        }
272
+        return ret;
273
+    } else {
274
+        AVFrame *dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
275
+        if (!dst)
276
+            return AVERROR(ENOMEM);
277
+
278
+        spatial_frame_pack(s, dst);
279
+
280
+        // get any property from the original frame
281
+        ret = av_frame_copy_props(dst, s->input_views[LEFT]);
282
+        if (ret < 0) {
283
+            av_frame_free(&dst);
284
+            return ret;
285
+        }
286
+
287
+        for (i = 0; i < 2; i++)
288
+            av_frame_free(&s->input_views[i]);
289
+
290
+        // set stereo3d side data
291
+        stereo = av_stereo3d_create_side_data(dst);
292
+        if (!stereo) {
293
+            av_frame_free(&dst);
294
+            return AVERROR(ENOMEM);
295
+        }
296
+        stereo->type = s->format;
297
+
298
+        return ff_filter_frame(outlink, dst);
299
+    }
300
+}
301
+
302
+#define OFFSET(x) offsetof(FramepackContext, x)
303
+#define V AV_OPT_FLAG_VIDEO_PARAM
304
+static const AVOption options[] = {
305
+    { "format", "Frame pack output format", OFFSET(format), AV_OPT_TYPE_INT,
306
+        { .i64 = AV_STEREO3D_SIDEBYSIDE }, 0, INT_MAX, .flags = V, .unit = "format" },
307
+    { "sbs", "Views are packed next to each other", 0, AV_OPT_TYPE_CONST,
308
+        { .i64 = AV_STEREO3D_SIDEBYSIDE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
309
+    { "tab", "Views are packed on top of each other", 0, AV_OPT_TYPE_CONST,
310
+        { .i64 = AV_STEREO3D_TOPBOTTOM }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
311
+    { "frameseq", "Views are one after the other", 0, AV_OPT_TYPE_CONST,
312
+        { .i64 = AV_STEREO3D_FRAMESEQUENCE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
313
+    { "lines", "Views are interleaved by lines", 0, AV_OPT_TYPE_CONST,
314
+        { .i64 = AV_STEREO3D_LINES }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
315
+    { "columns", "Views are interleaved by columns", 0, AV_OPT_TYPE_CONST,
316
+        { .i64 = AV_STEREO3D_COLUMNS }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
317
+    { NULL },
318
+};
319
+
320
+static const AVClass framepack_class = {
321
+    .class_name = "framepack",
322
+    .item_name  = av_default_item_name,
323
+    .option     = options,
324
+    .version    = LIBAVUTIL_VERSION_INT,
325
+};
326
+
327
+static const AVFilterPad framepack_inputs[] = {
328
+    {
329
+        .name         = "left",
330
+        .type         = AVMEDIA_TYPE_VIDEO,
331
+        .filter_frame = filter_frame_left,
332
+        .needs_fifo   = 1,
333
+    },
334
+    {
335
+        .name         = "right",
336
+        .type         = AVMEDIA_TYPE_VIDEO,
337
+        .filter_frame = filter_frame_right,
338
+        .needs_fifo   = 1,
339
+    },
340
+    { NULL }
341
+};
342
+
343
+static const AVFilterPad framepack_outputs[] = {
344
+    {
345
+        .name          = "packed",
346
+        .type          = AVMEDIA_TYPE_VIDEO,
347
+        .config_props  = config_output,
348
+        .request_frame = request_frame,
349
+    },
350
+    { NULL }
351
+};
352
+
353
+AVFilter ff_vf_framepack = {
354
+    .name          = "framepack",
355
+    .description   = NULL_IF_CONFIG_SMALL("Generate a frame packed stereoscopic video."),
356
+    .priv_size     = sizeof(FramepackContext),
357
+    .priv_class    = &framepack_class,
358
+    .query_formats = query_formats,
359
+    .inputs        = framepack_inputs,
360
+    .outputs       = framepack_outputs,
361
+    .uninit        = framepack_uninit,
362
+};