2 * Copyright (c) 2013 Vittorio Giovara
4 * This file is part of FFmpeg.
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.
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.
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
23 * Generate a frame packed video, by combining two views in a single surface.
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/rational.h"
32 #include "libavutil/stereo3d.h"
42 typedef struct FramepackContext {
45 const AVPixFmtDescriptor *pix_desc; ///< agreed pixel format
47 enum AVStereo3DType format; ///< frame pack type output
49 AVFrame *input_views[2]; ///< input frames
51 int64_t double_pts; ///< new pts for frameseq mode
54 static const enum AVPixelFormat formats_supported[] = {
55 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
56 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVJ420P,
57 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
61 static int query_formats(AVFilterContext *ctx)
63 // this will ensure that formats are the same on all pads
64 ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
68 static av_cold void framepack_uninit(AVFilterContext *ctx)
70 FramepackContext *s = ctx->priv;
72 // clean any leftover frame
73 av_frame_free(&s->input_views[LEFT]);
74 av_frame_free(&s->input_views[RIGHT]);
77 static int config_output(AVFilterLink *outlink)
79 AVFilterContext *ctx = outlink->src;
80 FramepackContext *s = outlink->src->priv;
82 int width = ctx->inputs[LEFT]->w;
83 int height = ctx->inputs[LEFT]->h;
84 AVRational time_base = ctx->inputs[LEFT]->time_base;
85 AVRational frame_rate = ctx->inputs[LEFT]->frame_rate;
87 // check size and fps match on the other input
88 if (width != ctx->inputs[RIGHT]->w ||
89 height != ctx->inputs[RIGHT]->h) {
90 av_log(ctx, AV_LOG_ERROR,
91 "Left and right sizes differ (%dx%d vs %dx%d).\n",
93 ctx->inputs[RIGHT]->w, ctx->inputs[RIGHT]->h);
94 return AVERROR_INVALIDDATA;
95 } else if (av_cmp_q(time_base, ctx->inputs[RIGHT]->time_base) != 0) {
96 av_log(ctx, AV_LOG_ERROR,
97 "Left and right time bases differ (%d/%d vs %d/%d).\n",
98 time_base.num, time_base.den,
99 ctx->inputs[RIGHT]->time_base.num,
100 ctx->inputs[RIGHT]->time_base.den);
101 return AVERROR_INVALIDDATA;
102 } else if (av_cmp_q(frame_rate, ctx->inputs[RIGHT]->frame_rate) != 0) {
103 av_log(ctx, AV_LOG_ERROR,
104 "Left and right framerates differ (%d/%d vs %d/%d).\n",
105 frame_rate.num, frame_rate.den,
106 ctx->inputs[RIGHT]->frame_rate.num,
107 ctx->inputs[RIGHT]->frame_rate.den);
108 return AVERROR_INVALIDDATA;
111 s->pix_desc = av_pix_fmt_desc_get(outlink->format);
115 // modify output properties as needed
117 case AV_STEREO3D_FRAMESEQUENCE:
121 s->double_pts = AV_NOPTS_VALUE;
123 case AV_STEREO3D_COLUMNS:
124 case AV_STEREO3D_SIDEBYSIDE:
127 case AV_STEREO3D_LINES:
128 case AV_STEREO3D_TOPBOTTOM:
132 av_log(ctx, AV_LOG_ERROR, "Unknown packing mode.");
133 return AVERROR_INVALIDDATA;
138 outlink->time_base = time_base;
139 outlink->frame_rate= frame_rate;
144 static void horizontal_frame_pack(FramepackContext *s,
149 int length = dst->width / 2;
150 int lines = dst->height;
152 for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
153 const uint8_t *leftp = s->input_views[LEFT]->data[plane];
154 const uint8_t *rightp = s->input_views[RIGHT]->data[plane];
155 uint8_t *dstp = dst->data[plane];
157 if (plane == 1 || plane == 2) {
158 length = FF_CEIL_RSHIFT(dst->width / 2, s->pix_desc->log2_chroma_w);
159 lines = FF_CEIL_RSHIFT(dst->height, s->pix_desc->log2_chroma_h);
163 for (i = 0; i < lines; i++) {
167 for (j = 0; j < length; j++) {
168 dstp[k++] = leftp[j];
169 dstp[k++] = rightp[j];
172 dstp += dst->linesize[plane];
173 leftp += s->input_views[LEFT]->linesize[plane];
174 rightp += s->input_views[RIGHT]->linesize[plane];
177 av_image_copy_plane(dst->data[plane], dst->linesize[plane],
178 leftp, s->input_views[LEFT]->linesize[plane],
180 av_image_copy_plane(dst->data[plane] + length, dst->linesize[plane],
181 rightp, s->input_views[RIGHT]->linesize[plane],
187 static void vertical_frame_pack(FramepackContext *s,
192 int length = dst->width;
193 int lines = dst->height / 2;
195 for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
196 if (plane == 1 || plane == 2) {
197 length = -(-(dst->width) >> s->pix_desc->log2_chroma_w);
198 lines = -(-(dst->height / 2) >> s->pix_desc->log2_chroma_h);
201 offset = interleaved ? dst->linesize[plane] : dst->linesize[plane] * lines;
203 av_image_copy_plane(dst->data[plane],
204 dst->linesize[plane] << interleaved,
205 s->input_views[LEFT]->data[plane],
206 s->input_views[LEFT]->linesize[plane],
208 av_image_copy_plane(dst->data[plane] + offset,
209 dst->linesize[plane] << interleaved,
210 s->input_views[RIGHT]->data[plane],
211 s->input_views[RIGHT]->linesize[plane],
216 static av_always_inline void spatial_frame_pack(FramepackContext *s, AVFrame *dst)
219 case AV_STEREO3D_SIDEBYSIDE:
220 horizontal_frame_pack(s, dst, 0);
222 case AV_STEREO3D_COLUMNS:
223 horizontal_frame_pack(s, dst, 1);
225 case AV_STEREO3D_TOPBOTTOM:
226 vertical_frame_pack(s, dst, 0);
228 case AV_STEREO3D_LINES:
229 vertical_frame_pack(s, dst, 1);
234 static int filter_frame_left(AVFilterLink *inlink, AVFrame *frame)
236 FramepackContext *s = inlink->dst->priv;
237 s->input_views[LEFT] = frame;
241 static int filter_frame_right(AVFilterLink *inlink, AVFrame *frame)
243 FramepackContext *s = inlink->dst->priv;
244 s->input_views[RIGHT] = frame;
248 static int request_frame(AVFilterLink *outlink)
250 AVFilterContext *ctx = outlink->src;
251 FramepackContext *s = ctx->priv;
255 /* get a frame on the either input, stop as soon as a video ends */
256 for (i = 0; i < 2; i++) {
257 if (!s->input_views[i]) {
258 ret = ff_request_frame(ctx->inputs[i]);
264 if (s->format == AV_STEREO3D_FRAMESEQUENCE) {
265 if (s->double_pts == AV_NOPTS_VALUE)
266 s->double_pts = s->input_views[LEFT]->pts;
268 for (i = 0; i < 2; i++) {
269 // set correct timestamps
270 s->input_views[i]->pts = s->double_pts++;
272 // set stereo3d side data
273 stereo = av_stereo3d_create_side_data(s->input_views[i]);
275 return AVERROR(ENOMEM);
276 stereo->type = s->format;
278 // filter the frame and immediately relinquish its pointer
279 ret = ff_filter_frame(outlink, s->input_views[i]);
280 s->input_views[i] = NULL;
286 AVFrame *dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
288 return AVERROR(ENOMEM);
290 spatial_frame_pack(s, dst);
292 // get any property from the original frame
293 ret = av_frame_copy_props(dst, s->input_views[LEFT]);
299 for (i = 0; i < 2; i++)
300 av_frame_free(&s->input_views[i]);
302 // set stereo3d side data
303 stereo = av_stereo3d_create_side_data(dst);
306 return AVERROR(ENOMEM);
308 stereo->type = s->format;
310 return ff_filter_frame(outlink, dst);
314 #define OFFSET(x) offsetof(FramepackContext, x)
315 #define V AV_OPT_FLAG_VIDEO_PARAM
316 static const AVOption options[] = {
317 { "format", "Frame pack output format", OFFSET(format), AV_OPT_TYPE_INT,
318 { .i64 = AV_STEREO3D_SIDEBYSIDE }, 0, INT_MAX, .flags = V, .unit = "format" },
319 { "sbs", "Views are packed next to each other", 0, AV_OPT_TYPE_CONST,
320 { .i64 = AV_STEREO3D_SIDEBYSIDE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
321 { "tab", "Views are packed on top of each other", 0, AV_OPT_TYPE_CONST,
322 { .i64 = AV_STEREO3D_TOPBOTTOM }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
323 { "frameseq", "Views are one after the other", 0, AV_OPT_TYPE_CONST,
324 { .i64 = AV_STEREO3D_FRAMESEQUENCE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
325 { "lines", "Views are interleaved by lines", 0, AV_OPT_TYPE_CONST,
326 { .i64 = AV_STEREO3D_LINES }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
327 { "columns", "Views are interleaved by columns", 0, AV_OPT_TYPE_CONST,
328 { .i64 = AV_STEREO3D_COLUMNS }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
332 static const AVClass framepack_class = {
333 .class_name = "framepack",
334 .item_name = av_default_item_name,
336 .version = LIBAVUTIL_VERSION_INT,
339 static const AVFilterPad framepack_inputs[] = {
342 .type = AVMEDIA_TYPE_VIDEO,
343 .filter_frame = filter_frame_left,
348 .type = AVMEDIA_TYPE_VIDEO,
349 .filter_frame = filter_frame_right,
355 static const AVFilterPad framepack_outputs[] = {
358 .type = AVMEDIA_TYPE_VIDEO,
359 .config_props = config_output,
360 .request_frame = request_frame,
365 AVFilter ff_vf_framepack = {
367 .description = NULL_IF_CONFIG_SMALL("Generate a frame packed stereoscopic video."),
368 .priv_size = sizeof(FramepackContext),
369 .priv_class = &framepack_class,
370 .query_formats = query_formats,
371 .inputs = framepack_inputs,
372 .outputs = framepack_outputs,
373 .uninit = framepack_uninit,