| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | #include "video.h" | ||
| 28 | |||
| 29 | typedef struct UntileContext { | ||
| 30 | const AVClass *class; | ||
| 31 | unsigned w, h; | ||
| 32 | unsigned current; | ||
| 33 | unsigned nb_frames; | ||
| 34 | AVFrame *frame; | ||
| 35 | const AVPixFmtDescriptor *desc; | ||
| 36 | int64_t dpts, pts; | ||
| 37 | int max_step[4]; | ||
| 38 | } UntileContext; | ||
| 39 | |||
| 40 | #define OFFSET(x) offsetof(UntileContext, x) | ||
| 41 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 42 | |||
| 43 | static const AVOption untile_options[] = { | ||
| 44 | { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, | ||
| 45 | {.str = "6x5"}, 0, 0, FLAGS }, | ||
| 46 | { NULL } | ||
| 47 | }; | ||
| 48 | |||
| 49 | AVFILTER_DEFINE_CLASS(untile); | ||
| 50 | |||
| 51 | 4 | static av_cold int init(AVFilterContext *ctx) | |
| 52 | { | ||
| 53 | 4 | UntileContext *s = ctx->priv; | |
| 54 | |||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->w > UINT_MAX / s->h) { |
| 56 | ✗ | av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n", | |
| 57 | s->w, s->h); | ||
| 58 | ✗ | return AVERROR(EINVAL); | |
| 59 | } | ||
| 60 | 4 | s->nb_frames = s->w * s->h; | |
| 61 | 4 | return 0; | |
| 62 | } | ||
| 63 | |||
| 64 | 2 | static int query_formats(const AVFilterContext *ctx, | |
| 65 | AVFilterFormatsConfig **cfg_in, | ||
| 66 | AVFilterFormatsConfig **cfg_out) | ||
| 67 | { | ||
| 68 | 2 | int reject_flags = AV_PIX_FMT_FLAG_HWACCEL | | |
| 69 | AV_PIX_FMT_FLAG_BITSTREAM | | ||
| 70 | FF_PIX_FMT_FLAG_SW_FLAT_SUB; | ||
| 71 | |||
| 72 | 2 | return ff_set_common_formats2(ctx, cfg_in, cfg_out, | |
| 73 | ff_formats_pixdesc_filter(0, reject_flags)); | ||
| 74 | } | ||
| 75 | |||
| 76 | 2 | static int config_output(AVFilterLink *outlink) | |
| 77 | { | ||
| 78 | 2 | AVFilterContext *ctx = outlink->src; | |
| 79 | 2 | UntileContext *s = ctx->priv; | |
| 80 | 2 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 81 | 2 | FilterLink *il = ff_filter_link(inlink); | |
| 82 | 2 | FilterLink *ol = ff_filter_link(outlink); | |
| 83 | AVRational dt; | ||
| 84 | |||
| 85 | 2 | s->desc = av_pix_fmt_desc_get(outlink->format); | |
| 86 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (inlink->w % (s->w << s->desc->log2_chroma_w) || |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | inlink->h % (s->h << s->desc->log2_chroma_h)) { |
| 88 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
| 89 | "Input resolution %ux%u not multiple of layout %ux%u.\n", | ||
| 90 | inlink->w, inlink->h, s->w, s->h); | ||
| 91 | ✗ | return AVERROR(EINVAL); | |
| 92 | } | ||
| 93 | 2 | outlink->w = inlink->w / s->w; | |
| 94 | 2 | outlink->h = inlink->h / s->h; | |
| 95 | 2 | outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; | |
| 96 | 2 | ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(s->nb_frames, 1)); | |
| 97 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (ol->frame_rate.num) |
| 98 | 2 | dt = av_inv_q(ol->frame_rate); | |
| 99 | else | ||
| 100 | ✗ | dt = av_mul_q(inlink->time_base, av_make_q(1, s->nb_frames)); | |
| 101 | 2 | outlink->time_base = av_gcd_q(inlink->time_base, dt, AV_TIME_BASE / 2, AV_TIME_BASE_Q); | |
| 102 | 2 | s->dpts = av_rescale_q(1, dt, outlink->time_base); | |
| 103 | 2 | av_log(ctx, AV_LOG_VERBOSE, "frame interval: %"PRId64"*%d/%d\n", | |
| 104 | s->dpts, dt.num, dt.den); | ||
| 105 | 2 | av_image_fill_max_pixsteps(s->max_step, NULL, s->desc); | |
| 106 | 2 | return 0; | |
| 107 | } | ||
| 108 | |||
| 109 | 24 | static int activate(AVFilterContext *ctx) | |
| 110 | { | ||
| 111 | 24 | UntileContext *s = ctx->priv; | |
| 112 | 24 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 113 | 24 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 114 | AVFrame *out; | ||
| 115 | int i, x, y, ret; | ||
| 116 | |||
| 117 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
| 118 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (!s->frame) { |
| 119 | 12 | ret = ff_inlink_consume_frame(inlink, &s->frame); | |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
| 121 | ✗ | return ret; | |
| 122 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
|
12 | if (ret) |
| 123 | 4 | s->pts = av_rescale_q(s->frame->pts, inlink->time_base, outlink->time_base); | |
| 124 | } | ||
| 125 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | if (s->frame) { |
| 126 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
|
16 | if (s->current == s->nb_frames - 1) { |
| 127 | 4 | out = s->frame; | |
| 128 | 4 | s->frame = NULL; | |
| 129 | } else { | ||
| 130 | 12 | out = av_frame_clone(s->frame); | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!out) |
| 132 | ✗ | return AVERROR(ENOMEM); | |
| 133 | } | ||
| 134 | 16 | x = outlink->w * (s->current % s->w); | |
| 135 | 16 | y = outlink->h * (s->current / s->w); | |
| 136 | 16 | out->width = outlink->w; | |
| 137 | 16 | out->height = outlink->h; | |
| 138 | 16 | out->data[0] += y * out->linesize[0]; | |
| 139 | 16 | out->data[0] += x * s->max_step[0]; | |
| 140 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if (!(s->desc->flags & AV_PIX_FMT_FLAG_PAL)) { |
| 141 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
|
48 | for (i = 1; i < 3; i ++) { |
| 142 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (out->data[i]) { |
| 143 | 32 | out->data[i] += (y >> s->desc->log2_chroma_h) * out->linesize[i]; | |
| 144 | 32 | out->data[i] += (x >> s->desc->log2_chroma_w) * s->max_step[i]; | |
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (out->data[3]) { |
| 149 | ✗ | out->data[3] += y * out->linesize[3]; | |
| 150 | ✗ | out->data[3] += x * s->max_step[3]; | |
| 151 | } | ||
| 152 | 16 | out->pts = s->pts; | |
| 153 | 16 | s->pts += s->dpts; | |
| 154 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
|
16 | if (++s->current == s->nb_frames) |
| 155 | 4 | s->current = 0; | |
| 156 | 16 | return ff_filter_frame(outlink, out); | |
| 157 | } | ||
| 158 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
|
8 | FF_FILTER_FORWARD_STATUS(inlink, outlink); |
| 159 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
| 160 | ✗ | return FFERROR_NOT_READY; | |
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | 4 | static av_cold void uninit(AVFilterContext *ctx) | |
| 165 | { | ||
| 166 | 4 | UntileContext *s = ctx->priv; | |
| 167 | |||
| 168 | 4 | av_frame_free(&s->frame); | |
| 169 | 4 | } | |
| 170 | |||
| 171 | static const AVFilterPad untile_outputs[] = { | ||
| 172 | { | ||
| 173 | .name = "default", | ||
| 174 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 175 | .config_props = config_output, | ||
| 176 | }, | ||
| 177 | }; | ||
| 178 | |||
| 179 | const FFFilter ff_vf_untile = { | ||
| 180 | .p.name = "untile", | ||
| 181 | .p.description = NULL_IF_CONFIG_SMALL("Untile a frame into a sequence of frames."), | ||
| 182 | .p.priv_class = &untile_class, | ||
| 183 | .init = init, | ||
| 184 | .uninit = uninit, | ||
| 185 | .activate = activate, | ||
| 186 | .priv_size = sizeof(UntileContext), | ||
| 187 | FILTER_INPUTS(ff_video_default_filterpad), | ||
| 188 | FILTER_OUTPUTS(untile_outputs), | ||
| 189 | FILTER_QUERY_FUNC2(query_formats), | ||
| 190 | }; | ||
| 191 |