1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * filter for manipulating frame side data
22 */
23
24 #include "config_components.h"
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/frame.h"
29 #include "libavutil/opt.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 enum SideDataMode {
35 SIDEDATA_SELECT,
36 SIDEDATA_DELETE,
37 SIDEDATA_NB
38 };
39
40 typedef struct SideDataContext {
41 const AVClass *class;
42
43 int mode;
44 int type; // enum AVFrameSideDataType or -1 for delete side data mode
45 } SideDataContext;
46
47 #define OFFSET(x) offsetof(SideDataContext, x)
48 #define DEFINE_OPTIONS(filt_name, FLAGS) \
49 static const AVOption filt_name##_options[] = { \
50 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SIDEDATA_NB-1, FLAGS, "mode" }, \
51 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_SELECT }, 0, 0, FLAGS, "mode" }, \
52 { "delete", "delete side data", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_DELETE }, 0, 0, FLAGS, "mode" }, \
53 { "type", "set side data type", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, FLAGS, "type" }, \
54 { "PANSCAN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_PANSCAN }, 0, 0, FLAGS, "type" }, \
55 { "A53_CC", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_A53_CC }, 0, 0, FLAGS, "type" }, \
56 { "STEREO3D", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_STEREO3D }, 0, 0, FLAGS, "type" }, \
57 { "MATRIXENCODING", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MATRIXENCODING }, 0, 0, FLAGS, "type" }, \
58 { "DOWNMIX_INFO", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DOWNMIX_INFO }, 0, 0, FLAGS, "type" }, \
59 { "REPLAYGAIN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REPLAYGAIN }, 0, 0, FLAGS, "type" }, \
60 { "DISPLAYMATRIX", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DISPLAYMATRIX }, 0, 0, FLAGS, "type" }, \
61 { "AFD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AFD }, 0, 0, FLAGS, "type" }, \
62 { "MOTION_VECTORS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MOTION_VECTORS }, 0, 0, FLAGS, "type" }, \
63 { "SKIP_SAMPLES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SKIP_SAMPLES }, 0, 0, FLAGS, "type" }, \
64 { "AUDIO_SERVICE_TYPE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AUDIO_SERVICE_TYPE }, 0, 0, FLAGS, "type" }, \
65 { "MASTERING_DISPLAY_METADATA", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, 0, 0, FLAGS, "type" }, \
66 { "GOP_TIMECODE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_GOP_TIMECODE }, 0, 0, FLAGS, "type" }, \
67 { "SPHERICAL", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SPHERICAL }, 0, 0, FLAGS, "type" }, \
68 { "CONTENT_LIGHT_LEVEL", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_CONTENT_LIGHT_LEVEL }, 0, 0, FLAGS, "type" }, \
69 { "ICC_PROFILE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_ICC_PROFILE }, 0, 0, FLAGS, "type" }, \
70 { "S12M_TIMECOD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_S12M_TIMECODE }, 0, 0, FLAGS, "type" }, \
71 { "DYNAMIC_HDR_PLUS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, 0, 0, FLAGS, "type" }, \
72 { "REGIONS_OF_INTEREST", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REGIONS_OF_INTEREST }, 0, 0, FLAGS, "type" }, \
73 { "DETECTION_BOUNDING_BOXES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DETECTION_BBOXES }, 0, 0, FLAGS, "type" }, \
74 { "SEI_UNREGISTERED", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SEI_UNREGISTERED }, 0, 0, FLAGS, "type" }, \
75 { NULL } \
76 }
77
init(AVFilterContext * ctx)78 static av_cold int init(AVFilterContext *ctx)
79 {
80 SideDataContext *s = ctx->priv;
81
82 if (s->type == -1 && s->mode != SIDEDATA_DELETE) {
83 av_log(ctx, AV_LOG_ERROR, "Side data type must be set\n");
84 return AVERROR(EINVAL);
85 }
86
87 return 0;
88 }
89
filter_frame(AVFilterLink * inlink,AVFrame * frame)90 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
91 {
92 AVFilterContext *ctx = inlink->dst;
93 AVFilterLink *outlink = ctx->outputs[0];
94 SideDataContext *s = ctx->priv;
95 AVFrameSideData *sd = NULL;
96
97 if (s->type != -1)
98 sd = av_frame_get_side_data(frame, s->type);
99
100 switch (s->mode) {
101 case SIDEDATA_SELECT:
102 if (sd) {
103 return ff_filter_frame(outlink, frame);
104 }
105 break;
106 case SIDEDATA_DELETE:
107 if (s->type == -1) {
108 while (frame->nb_side_data)
109 av_frame_remove_side_data(frame, frame->side_data[0]->type);
110 } else if (sd) {
111 av_frame_remove_side_data(frame, s->type);
112 }
113 return ff_filter_frame(outlink, frame);
114 break;
115 default:
116 av_assert0(0);
117 };
118
119 av_frame_free(&frame);
120
121 return 0;
122 }
123
124 #if CONFIG_ASIDEDATA_FILTER
125
126 DEFINE_OPTIONS(asidedata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
127 AVFILTER_DEFINE_CLASS(asidedata);
128
129 static const AVFilterPad ainputs[] = {
130 {
131 .name = "default",
132 .type = AVMEDIA_TYPE_AUDIO,
133 .filter_frame = filter_frame,
134 },
135 };
136
137 static const AVFilterPad aoutputs[] = {
138 {
139 .name = "default",
140 .type = AVMEDIA_TYPE_AUDIO,
141 },
142 };
143
144 const AVFilter ff_af_asidedata = {
145 .name = "asidedata",
146 .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame side data."),
147 .priv_size = sizeof(SideDataContext),
148 .priv_class = &asidedata_class,
149 .init = init,
150 FILTER_INPUTS(ainputs),
151 FILTER_OUTPUTS(aoutputs),
152 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
153 AVFILTER_FLAG_METADATA_ONLY,
154 };
155 #endif /* CONFIG_ASIDEDATA_FILTER */
156
157 #if CONFIG_SIDEDATA_FILTER
158
159 DEFINE_OPTIONS(sidedata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
160 AVFILTER_DEFINE_CLASS(sidedata);
161
162 static const AVFilterPad inputs[] = {
163 {
164 .name = "default",
165 .type = AVMEDIA_TYPE_VIDEO,
166 .filter_frame = filter_frame,
167 },
168 };
169
170 static const AVFilterPad outputs[] = {
171 {
172 .name = "default",
173 .type = AVMEDIA_TYPE_VIDEO,
174 },
175 };
176
177 const AVFilter ff_vf_sidedata = {
178 .name = "sidedata",
179 .description = NULL_IF_CONFIG_SMALL("Manipulate video frame side data."),
180 .priv_size = sizeof(SideDataContext),
181 .priv_class = &sidedata_class,
182 .init = init,
183 FILTER_INPUTS(inputs),
184 FILTER_OUTPUTS(outputs),
185 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
186 AVFILTER_FLAG_METADATA_ONLY,
187 };
188 #endif /* CONFIG_SIDEDATA_FILTER */
189