FFmpeg  4.3.6
vf_untile.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "filters.h"
27 
28 typedef struct UntileContext {
29  const AVClass *class;
30  unsigned w, h;
31  unsigned current;
32  unsigned nb_frames;
35  int64_t dpts, pts;
36  int max_step[4];
38 
39 #define OFFSET(x) offsetof(UntileContext, x)
40 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
41 
42 static const AVOption untile_options[] = {
43  { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
44  {.str = "6x5"}, 0, 0, FLAGS },
45  { NULL }
46 };
47 
48 AVFILTER_DEFINE_CLASS(untile);
49 
51 {
52  UntileContext *s = ctx->priv;
53 
54  if (s->w > UINT_MAX / s->h) {
55  av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
56  s->w, s->h);
57  return AVERROR(EINVAL);
58  }
59  s->nb_frames = s->w * s->h;
60  return 0;
61 }
62 
64 {
66  int ret;
67 
68  ret = ff_formats_pixdesc_filter(&formats, 0,
72  if (ret < 0)
73  return ret;
74  return ff_set_common_formats(ctx, formats);
75 }
76 
77 static int config_output(AVFilterLink *outlink)
78 {
79  AVFilterContext *ctx = outlink->src;
80  UntileContext *s = ctx->priv;
81  AVFilterLink *inlink = ctx->inputs[0];
82  AVRational dt;
83 
84  s->desc = av_pix_fmt_desc_get(outlink->format);
85  if (inlink->w % (s->w << s->desc->log2_chroma_w) ||
86  inlink->h % (s->h << s->desc->log2_chroma_h)) {
87  av_log(ctx, AV_LOG_ERROR,
88  "Input resolution %ux%u not multiple of layout %ux%u.\n",
89  inlink->w, inlink->h, s->w, s->h);
90  return AVERROR(EINVAL);
91  }
92  outlink->w = inlink->w / s->w;
93  outlink->h = inlink->h / s->h;
94  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
95  outlink->frame_rate = av_mul_q(inlink->frame_rate, av_make_q(s->nb_frames, 1));
96  if (outlink->frame_rate.num)
97  dt = av_inv_q(outlink->frame_rate);
98  else
99  dt = av_mul_q(inlink->time_base, av_make_q(1, s->nb_frames));
100  outlink->time_base = av_gcd_q(inlink->time_base, dt, AV_TIME_BASE / 2, AV_TIME_BASE_Q);
101  s->dpts = av_rescale_q(1, dt, outlink->time_base);
102  av_log(ctx, AV_LOG_VERBOSE, "frame interval: %"PRId64"*%d/%d\n",
103  s->dpts, dt.num, dt.den);
105  return 0;
106 }
107 
109 {
110  UntileContext *s = ctx->priv;
111  AVFilterLink *inlink = ctx->inputs[0];
112  AVFilterLink *outlink = ctx->outputs[0];
113  AVFrame *out;
114  int i, x, y, ret;
115 
116  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
117  if (!s->frame) {
118  ret = ff_inlink_consume_frame(inlink, &s->frame);
119  if (ret < 0)
120  return ret;
121  if (ret)
122  s->pts = av_rescale_q(s->frame->pts, inlink->time_base, outlink->time_base);
123  }
124  if (s->frame) {
125  if (s->current == s->nb_frames - 1) {
126  out = s->frame;
127  s->frame = NULL;
128  } else {
129  out = av_frame_clone(s->frame);
130  if (!out)
131  return AVERROR(ENOMEM);
132  }
133  x = outlink->w * (s->current % s->w);
134  y = outlink->h * (s->current / s->w);
135  out->width = outlink->w;
136  out->height = outlink->h;
137  out->data[0] += y * out->linesize[0];
138  out->data[0] += x * s->max_step[0];
139  if (!(s->desc->flags & AV_PIX_FMT_FLAG_PAL || s->desc->flags & FF_PSEUDOPAL)) {
140  for (i = 1; i < 3; i ++) {
141  if (out->data[i]) {
142  out->data[i] += (y >> s->desc->log2_chroma_h) * out->linesize[i];
143  out->data[i] += (x >> s->desc->log2_chroma_w) * s->max_step[i];
144  }
145  }
146  }
147  if (out->data[3]) {
148  out->data[3] += y * out->linesize[3];
149  out->data[3] += x * s->max_step[3];
150  }
151  out->pts = s->pts;
152  s->pts += s->dpts;
153  if (++s->current == s->nb_frames)
154  s->current = 0;
155  return ff_filter_frame(outlink, out);
156  }
157  FF_FILTER_FORWARD_STATUS(inlink, outlink);
158  FF_FILTER_FORWARD_WANTED(outlink, inlink);
159  return FFERROR_NOT_READY;
160 
161 }
162 
164 {
165  UntileContext *s = ctx->priv;
166 
167  av_frame_free(&s->frame);
168 }
169 
170 static const AVFilterPad untile_inputs[] = {
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_VIDEO,
174  },
175  { NULL }
176 };
177 
178 static const AVFilterPad untile_outputs[] = {
179  {
180  .name = "default",
181  .type = AVMEDIA_TYPE_VIDEO,
182  .config_props = config_output,
183  },
184  { NULL }
185 };
186 
188  .name = "untile",
189  .description = NULL_IF_CONFIG_SMALL("Untile a frame into a sequence of frames."),
190  .init = init,
191  .uninit = uninit,
192  .query_formats = query_formats,
193  .activate = activate,
194  .priv_size = sizeof(UntileContext),
195  .inputs = untile_inputs,
196  .outputs = untile_outputs,
197  .priv_class = &untile_class,
198 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1476
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
static av_cold int init(AVFilterContext *ctx)
Definition: vf_untile.c:50
misc image utilities
Main libavfilter public API header.
int num
Numerator.
Definition: rational.h:59
static const AVOption untile_options[]
Definition: vf_untile.c:42
static int activate(AVFilterContext *ctx)
Definition: vf_untile.c:108
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
int64_t dpts
Definition: vf_untile.c:35
AVFILTER_DEFINE_CLASS(untile)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:35
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
unsigned current
Definition: vf_untile.c:31
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
int ff_formats_pixdesc_filter(AVFilterFormats **rfmts, unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:385
#define av_cold
Definition: attributes.h:88
AVOptions.
AVFilter ff_vf_untile
Definition: vf_untile.c:187
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:393
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static const AVFilterPad untile_inputs[]
Definition: vf_untile.c:170
#define av_log(a,...)
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
unsigned nb_frames
Definition: vf_untile.c:32
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
int width
Definition: frame.h:358
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int 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:605
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
static const AVFilterPad untile_outputs[]
Definition: vf_untile.c:178
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:254
const AVPixFmtDescriptor * desc
Definition: vf_untile.c:34
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:541
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int config_output(AVFilterLink *outlink)
Definition: vf_untile.c:77
#define FF_PIX_FMT_FLAG_SW_FLAT_SUB
Definition: formats.h:232
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
int max_step[4]
Definition: vf_untile.c:36
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVFrame * frame
Definition: vf_untile.c:33
const char * name
Filter name.
Definition: avfilter.h:148
int64_t pts
Definition: vf_untile.c:35
AVRational av_gcd_q(AVRational a, AVRational b, int max_den, AVRational def)
Return the best rational so that a and b are multiple of it.
Definition: rational.c:186
offset must point to two consecutive integers
Definition: opt.h:233
static int query_formats(AVFilterContext *ctx)
Definition: vf_untile.c:63
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:226
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int den
Denominator.
Definition: rational.h:60
#define FF_PSEUDOPAL
Definition: internal.h:369
unsigned w
Definition: vf_untile.c:30
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_untile.c:163
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
int height
Definition: frame.h:358
#define OFFSET(x)
Definition: vf_untile.c:39
FILE * out
Definition: movenc.c:54
formats
Definition: signature.h:48
unsigned h
Definition: vf_untile.c:30
#define FLAGS
Definition: vf_untile.c:40