Libav
vf_framepack.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Vittorio Giovara
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include <string.h>
27 
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"
33 
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 #define LEFT 0
40 #define RIGHT 1
41 
42 typedef struct FramepackContext {
43  const AVClass *class;
44 
46 
48 
50 
51  int64_t double_pts;
53 
54 static const enum AVPixelFormat formats_supported[] = {
59 };
60 
62 {
63  // this will ensure that formats are the same on all pads
65  return 0;
66 }
67 
69 {
70  FramepackContext *s = ctx->priv;
71 
72  // clean any leftover frame
75 }
76 
77 static int config_output(AVFilterLink *outlink)
78 {
79  AVFilterContext *ctx = outlink->src;
80  FramepackContext *s = outlink->src->priv;
81 
82  int width = ctx->inputs[LEFT]->w;
83  int height = ctx->inputs[LEFT]->h;
84  AVRational time_base = ctx->inputs[LEFT]->time_base;
85 
86  // check size and fps match on the other input
87  if (width != ctx->inputs[RIGHT]->w ||
88  height != ctx->inputs[RIGHT]->h) {
89  av_log(ctx, AV_LOG_ERROR,
90  "Left and right sizes differ (%dx%d vs %dx%d).\n",
91  width, height,
92  ctx->inputs[RIGHT]->w, ctx->inputs[RIGHT]->h);
93  return AVERROR_INVALIDDATA;
94  } else if (av_cmp_q(time_base, ctx->inputs[RIGHT]->time_base) != 0) {
95  av_log(ctx, AV_LOG_ERROR,
96  "Left and right framerates differ (%d/%d vs %d/%d).\n",
97  time_base.num, time_base.den,
98  ctx->inputs[RIGHT]->time_base.num,
99  ctx->inputs[RIGHT]->time_base.den);
100  return AVERROR_INVALIDDATA;
101  }
102 
103  s->pix_desc = av_pix_fmt_desc_get(outlink->format);
104  if (!s->pix_desc)
105  return AVERROR_BUG;
106 
107  // modify output properties as needed
108  switch (s->format) {
110  time_base.den *= 2;
112  break;
113  case AV_STEREO3D_COLUMNS:
115  width *= 2;
116  break;
117  case AV_STEREO3D_LINES:
119  height *= 2;
120  break;
121  default:
122  av_log(ctx, AV_LOG_ERROR, "Unknown packing mode.");
123  return AVERROR_INVALIDDATA;
124  }
125 
126  outlink->w = width;
127  outlink->h = height;
128  outlink->time_base = time_base;
129 
130  return 0;
131 }
132 
134  AVFrame *dst,
135  int interleaved)
136 {
137  int plane, i;
138  int length = dst->width / 2;
139  int lines = dst->height;
140 
141  for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
142  const uint8_t *leftp = s->input_views[LEFT]->data[plane];
143  const uint8_t *rightp = s->input_views[RIGHT]->data[plane];
144  uint8_t *dstp = dst->data[plane];
145 
146  if (plane == 1 || plane == 2) {
147  length = -(-(dst->width / 2) >> s->pix_desc->log2_chroma_w);
148  lines = -(-(dst->height) >> s->pix_desc->log2_chroma_h);
149  }
150 
151  if (interleaved) {
152  for (i = 0; i < lines; i++) {
153  int j;
154  int k = 0;
155 
156  for (j = 0; j < length; j++) {
157  dstp[k++] = leftp[j];
158  dstp[k++] = rightp[j];
159  }
160 
161  dstp += dst->linesize[plane];
162  leftp += s->input_views[LEFT]->linesize[plane];
163  rightp += s->input_views[RIGHT]->linesize[plane];
164  }
165  } else {
166  av_image_copy_plane(dst->data[plane], dst->linesize[plane],
167  leftp, s->input_views[LEFT]->linesize[plane],
168  length, lines);
169  av_image_copy_plane(dst->data[plane] + length, dst->linesize[plane],
170  rightp, s->input_views[RIGHT]->linesize[plane],
171  length, lines);
172  }
173  }
174 }
175 
177  AVFrame *dst,
178  int interleaved)
179 {
180  int plane, offset;
181  int length = dst->width;
182  int lines = dst->height / 2;
183 
184  for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
185  if (plane == 1 || plane == 2) {
186  length = -(-(dst->width) >> s->pix_desc->log2_chroma_w);
187  lines = -(-(dst->height / 2) >> s->pix_desc->log2_chroma_h);
188  }
189 
190  offset = interleaved ? dst->linesize[plane] : dst->linesize[plane] * lines;
191 
192  av_image_copy_plane(dst->data[plane],
193  dst->linesize[plane] << interleaved,
194  s->input_views[LEFT]->data[plane],
195  s->input_views[LEFT]->linesize[plane],
196  length, lines);
197  av_image_copy_plane(dst->data[plane] + offset,
198  dst->linesize[plane] << interleaved,
199  s->input_views[RIGHT]->data[plane],
200  s->input_views[RIGHT]->linesize[plane],
201  length, lines);
202  }
203 }
204 
206 {
207  switch (s->format) {
209  horizontal_frame_pack(s, dst, 0);
210  break;
211  case AV_STEREO3D_COLUMNS:
212  horizontal_frame_pack(s, dst, 1);
213  break;
215  vertical_frame_pack(s, dst, 0);
216  break;
217  case AV_STEREO3D_LINES:
218  vertical_frame_pack(s, dst, 1);
219  break;
220  }
221 }
222 
223 static int filter_frame_left(AVFilterLink *inlink, AVFrame *frame)
224 {
225  FramepackContext *s = inlink->dst->priv;
226  s->input_views[LEFT] = frame;
227  return 0;
228 }
229 
230 static int filter_frame_right(AVFilterLink *inlink, AVFrame *frame)
231 {
232  FramepackContext *s = inlink->dst->priv;
233  s->input_views[RIGHT] = frame;
234  return 0;
235 }
236 
237 static int request_frame(AVFilterLink *outlink)
238 {
239  AVFilterContext *ctx = outlink->src;
240  FramepackContext *s = ctx->priv;
241  AVStereo3D *stereo;
242  int ret, i;
243 
244  /* get a frame on the either input, stop as soon as a video ends */
245  for (i = 0; i < 2; i++) {
246  if (!s->input_views[i]) {
247  ret = ff_request_frame(ctx->inputs[i]);
248  if (ret < 0)
249  return ret;
250  }
251  }
252 
253  if (s->format == AV_STEREO3D_FRAMESEQUENCE) {
254  if (s->double_pts == AV_NOPTS_VALUE)
255  s->double_pts = s->input_views[LEFT]->pts;
256 
257  for (i = 0; i < 2; i++) {
258  // set correct timestamps
259  s->input_views[i]->pts = s->double_pts++;
260 
261  // set stereo3d side data
263  if (!stereo)
264  return AVERROR(ENOMEM);
265  stereo->type = s->format;
266 
267  // filter the frame and immediately relinquish its pointer
268  ret = ff_filter_frame(outlink, s->input_views[i]);
269  s->input_views[i] = NULL;
270  if (ret < 0)
271  return ret;
272  }
273  return ret;
274  } else {
275  AVFrame *dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
276  if (!dst)
277  return AVERROR(ENOMEM);
278 
279  spatial_frame_pack(s, dst);
280 
281  // get any property from the original frame
282  ret = av_frame_copy_props(dst, s->input_views[LEFT]);
283  if (ret < 0) {
284  av_frame_free(&dst);
285  return ret;
286  }
287 
288  for (i = 0; i < 2; i++)
289  av_frame_free(&s->input_views[i]);
290 
291  // set stereo3d side data
292  stereo = av_stereo3d_create_side_data(dst);
293  if (!stereo) {
294  av_frame_free(&dst);
295  return AVERROR(ENOMEM);
296  }
297  stereo->type = s->format;
298 
299  return ff_filter_frame(outlink, dst);
300  }
301 }
302 
303 #define OFFSET(x) offsetof(FramepackContext, x)
304 #define V AV_OPT_FLAG_VIDEO_PARAM
305 static const AVOption options[] = {
306  { "format", "Frame pack output format", OFFSET(format), AV_OPT_TYPE_INT,
307  { .i64 = AV_STEREO3D_SIDEBYSIDE }, 0, INT_MAX, .flags = V, .unit = "format" },
308  { "sbs", "Views are packed next to each other", 0, AV_OPT_TYPE_CONST,
309  { .i64 = AV_STEREO3D_SIDEBYSIDE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
310  { "tab", "Views are packed on top of each other", 0, AV_OPT_TYPE_CONST,
311  { .i64 = AV_STEREO3D_TOPBOTTOM }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
312  { "frameseq", "Views are one after the other", 0, AV_OPT_TYPE_CONST,
313  { .i64 = AV_STEREO3D_FRAMESEQUENCE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
314  { "lines", "Views are interleaved by lines", 0, AV_OPT_TYPE_CONST,
315  { .i64 = AV_STEREO3D_LINES }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
316  { "columns", "Views are interleaved by columns", 0, AV_OPT_TYPE_CONST,
317  { .i64 = AV_STEREO3D_COLUMNS }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
318  { NULL },
319 };
320 
321 static const AVClass framepack_class = {
322  .class_name = "framepack",
323  .item_name = av_default_item_name,
324  .option = options,
325  .version = LIBAVUTIL_VERSION_INT,
326 };
327 
328 static const AVFilterPad framepack_inputs[] = {
329  {
330  .name = "left",
331  .type = AVMEDIA_TYPE_VIDEO,
332  .filter_frame = filter_frame_left,
333  .needs_fifo = 1,
334  },
335  {
336  .name = "right",
337  .type = AVMEDIA_TYPE_VIDEO,
338  .filter_frame = filter_frame_right,
339  .needs_fifo = 1,
340  },
341  { NULL }
342 };
343 
344 static const AVFilterPad framepack_outputs[] = {
345  {
346  .name = "packed",
347  .type = AVMEDIA_TYPE_VIDEO,
348  .config_props = config_output,
349  .request_frame = request_frame,
350  },
351  { NULL }
352 };
353 
355  .name = "framepack",
356  .description = NULL_IF_CONFIG_SMALL("Generate a frame packed stereoscopic video."),
357  .priv_size = sizeof(FramepackContext),
358  .priv_class = &framepack_class,
360  .inputs = framepack_inputs,
361  .outputs = framepack_outputs,
363 };
static int filter_frame_right(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_framepack.c:230
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Views are packed per line, as if interlaced.
Definition: stereo3d.h:94
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1507
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
AVOption.
Definition: opt.h:233
Views are alternated temporally.
Definition: stereo3d.h:63
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
Main libavfilter public API header.
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_amix.c:514
int num
numerator
Definition: rational.h:44
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:55
static enum AVPixelFormat formats_supported[]
Definition: vf_framepack.c:54
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104
#define RIGHT
Definition: vf_framepack.c:40
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:68
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define OFFSET(x)
Definition: vf_framepack.c:303
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:728
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:104
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:120
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:183
static int query_formats(AVFilterContext *ctx)
Definition: vf_framepack.c:61
static const AVClass framepack_class
Definition: vf_framepack.c:321
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:103
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
static const AVOption options[]
Definition: vf_framepack.c:305
A filter pad used for either input or output.
Definition: internal.h:36
int width
width and height of the video frame
Definition: frame.h:146
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:77
#define V
Definition: vf_framepack.c:304
AVFilter ff_vf_framepack
Definition: vf_framepack.c:354
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void spatial_frame_pack(FramepackContext *s, AVFrame *dst)
Definition: vf_framepack.c:205
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void * priv
private data for use by the filter
Definition: avfilter.h:584
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
int64_t double_pts
new pts for frameseq mode
Definition: vf_framepack.c:51
static int filter_frame_left(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_framepack.c:223
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
static void vertical_frame_pack(FramepackContext *s, AVFrame *dst, int interleaved)
Definition: vf_framepack.c:176
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:59
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
AVFrame * input_views[2]
input frames
Definition: vf_framepack.c:49
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:124
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
av_default_item_name
Definition: dnxhdenc.c:45
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:57
#define LEFT
Definition: vf_framepack.c:39
enum AVStereo3DType format
frame pack type output
Definition: vf_framepack.c:47
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:111
rational number numerator/denominator
Definition: rational.h:43
static int request_frame(AVFilterLink *outlink)
Definition: vf_framepack.c:237
static const AVFilterPad framepack_inputs[]
Definition: vf_framepack.c:328
const char * name
Filter name.
Definition: avfilter.h:425
AVStereo3DType
List of possible 3D Types.
Definition: stereo3d.h:28
const AVPixFmtDescriptor * pix_desc
agreed pixel format
Definition: vf_framepack.c:45
Views are on top of each other.
Definition: stereo3d.h:52
static int config_output(AVFilterLink *outlink)
Definition: vf_framepack.c:77
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:31
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
static av_cold void framepack_uninit(AVFilterContext *ctx)
Definition: vf_framepack.c:68
int height
Definition: gxfenc.c:72
Views are next to each other.
Definition: stereo3d.h:42
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
rational numbers
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
int den
denominator
Definition: rational.h:45
Views are packed per column.
Definition: stereo3d.h:104
An instance of a filter.
Definition: avfilter.h:563
int height
Definition: frame.h:146
static const AVFilterPad framepack_outputs[]
Definition: vf_framepack.c:344
#define av_always_inline
Definition: attributes.h:40
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:244
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:231
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:362
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static void horizontal_frame_pack(FramepackContext *s, AVFrame *dst, int interleaved)
Definition: vf_framepack.c:133