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 "libavutil/avassert.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/opt.h"
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31
32 enum SideDataMode {
33 SIDEDATA_SELECT,
34 SIDEDATA_DELETE,
35 SIDEDATA_NB
36 };
37
38 typedef struct SideDataContext {
39 const AVClass *class;
40
41 int mode;
42 int type; // enum AVFrameSideDataType or -1 for delete side data mode
43 } SideDataContext;
44
45 #define OFFSET(x) offsetof(SideDataContext, x)
46 #if FF_API_FRAME_QP
47 #define DEFINE_OPTIONS(filt_name, FLAGS) \
48 static const AVOption filt_name##_options[] = { \
49 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SIDEDATA_NB-1, FLAGS, "mode" }, \
50 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_SELECT }, 0, 0, FLAGS, "mode" }, \
51 { "delete", "delete side data", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_DELETE }, 0, 0, FLAGS, "mode" }, \
52 { "type", "set side data type", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, FLAGS, "type" }, \
53 { "PANSCAN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_PANSCAN }, 0, 0, FLAGS, "type" }, \
54 { "A53_CC", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_A53_CC }, 0, 0, FLAGS, "type" }, \
55 { "STEREO3D", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_STEREO3D }, 0, 0, FLAGS, "type" }, \
56 { "MATRIXENCODING", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MATRIXENCODING }, 0, 0, FLAGS, "type" }, \
57 { "DOWNMIX_INFO", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DOWNMIX_INFO }, 0, 0, FLAGS, "type" }, \
58 { "REPLAYGAIN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REPLAYGAIN }, 0, 0, FLAGS, "type" }, \
59 { "DISPLAYMATRIX", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DISPLAYMATRIX }, 0, 0, FLAGS, "type" }, \
60 { "AFD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AFD }, 0, 0, FLAGS, "type" }, \
61 { "MOTION_VECTORS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MOTION_VECTORS }, 0, 0, FLAGS, "type" }, \
62 { "SKIP_SAMPLES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SKIP_SAMPLES }, 0, 0, FLAGS, "type" }, \
63 { "AUDIO_SERVICE_TYPE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AUDIO_SERVICE_TYPE }, 0, 0, FLAGS, "type" }, \
64 { "MASTERING_DISPLAY_METADATA", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, 0, 0, FLAGS, "type" }, \
65 { "GOP_TIMECODE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_GOP_TIMECODE }, 0, 0, FLAGS, "type" }, \
66 { "SPHERICAL", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SPHERICAL }, 0, 0, FLAGS, "type" }, \
67 { "CONTENT_LIGHT_LEVEL", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_CONTENT_LIGHT_LEVEL }, 0, 0, FLAGS, "type" }, \
68 { "ICC_PROFILE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_ICC_PROFILE }, 0, 0, FLAGS, "type" }, \
69 { "QP_TABLE_PROPERTIES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_QP_TABLE_PROPERTIES }, 0, 0, FLAGS, "type" }, \
70 { "QP_TABLE_DATA", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_QP_TABLE_DATA }, 0, 0, FLAGS, "type" }, \
71 { "S12M_TIMECOD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_S12M_TIMECODE }, 0, 0, FLAGS, "type" }, \
72 { "DYNAMIC_HDR_PLUS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, 0, 0, FLAGS, "type" }, \
73 { "REGIONS_OF_INTEREST", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REGIONS_OF_INTEREST }, 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 #else
78 #define DEFINE_OPTIONS(filt_name, FLAGS) \
79 static const AVOption filt_name##_options[] = { \
80 { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SIDEDATA_NB-1, FLAGS, "mode" }, \
81 { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_SELECT }, 0, 0, FLAGS, "mode" }, \
82 { "delete", "delete side data", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_DELETE }, 0, 0, FLAGS, "mode" }, \
83 { "type", "set side data type", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, FLAGS, "type" }, \
84 { "PANSCAN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_PANSCAN }, 0, 0, FLAGS, "type" }, \
85 { "A53_CC", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_A53_CC }, 0, 0, FLAGS, "type" }, \
86 { "STEREO3D", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_STEREO3D }, 0, 0, FLAGS, "type" }, \
87 { "MATRIXENCODING", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MATRIXENCODING }, 0, 0, FLAGS, "type" }, \
88 { "DOWNMIX_INFO", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DOWNMIX_INFO }, 0, 0, FLAGS, "type" }, \
89 { "REPLAYGAIN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REPLAYGAIN }, 0, 0, FLAGS, "type" }, \
90 { "DISPLAYMATRIX", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DISPLAYMATRIX }, 0, 0, FLAGS, "type" }, \
91 { "AFD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AFD }, 0, 0, FLAGS, "type" }, \
92 { "MOTION_VECTORS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MOTION_VECTORS }, 0, 0, FLAGS, "type" }, \
93 { "SKIP_SAMPLES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SKIP_SAMPLES }, 0, 0, FLAGS, "type" }, \
94 { "AUDIO_SERVICE_TYPE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AUDIO_SERVICE_TYPE }, 0, 0, FLAGS, "type" }, \
95 { "MASTERING_DISPLAY_METADATA", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, 0, 0, FLAGS, "type" }, \
96 { "GOP_TIMECODE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_GOP_TIMECODE }, 0, 0, FLAGS, "type" }, \
97 { "SPHERICAL", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SPHERICAL }, 0, 0, FLAGS, "type" }, \
98 { "CONTENT_LIGHT_LEVEL", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_CONTENT_LIGHT_LEVEL }, 0, 0, FLAGS, "type" }, \
99 { "ICC_PROFILE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_ICC_PROFILE }, 0, 0, FLAGS, "type" }, \
100 { "S12M_TIMECOD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_S12M_TIMECODE }, 0, 0, FLAGS, "type" }, \
101 { "DYNAMIC_HDR_PLUS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, 0, 0, FLAGS, "type" }, \
102 { "REGIONS_OF_INTEREST", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REGIONS_OF_INTEREST }, 0, 0, FLAGS, "type" }, \
103 { "SEI_UNREGISTERED", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SEI_UNREGISTERED }, 0, 0, FLAGS, "type" }, \
104 { NULL } \
105 }
106 #endif
107
init(AVFilterContext * ctx)108 static av_cold int init(AVFilterContext *ctx)
109 {
110 SideDataContext *s = ctx->priv;
111
112 if (s->type == -1 && s->mode != SIDEDATA_DELETE) {
113 av_log(ctx, AV_LOG_ERROR, "Side data type must be set\n");
114 return AVERROR(EINVAL);
115 }
116
117 return 0;
118 }
119
filter_frame(AVFilterLink * inlink,AVFrame * frame)120 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
121 {
122 AVFilterContext *ctx = inlink->dst;
123 AVFilterLink *outlink = ctx->outputs[0];
124 SideDataContext *s = ctx->priv;
125 AVFrameSideData *sd = NULL;
126
127 if (s->type != -1)
128 sd = av_frame_get_side_data(frame, s->type);
129
130 switch (s->mode) {
131 case SIDEDATA_SELECT:
132 if (sd) {
133 return ff_filter_frame(outlink, frame);
134 }
135 break;
136 case SIDEDATA_DELETE:
137 if (s->type == -1) {
138 while (frame->nb_side_data)
139 av_frame_remove_side_data(frame, frame->side_data[0]->type);
140 } else if (sd) {
141 av_frame_remove_side_data(frame, s->type);
142 }
143 return ff_filter_frame(outlink, frame);
144 break;
145 default:
146 av_assert0(0);
147 };
148
149 av_frame_free(&frame);
150
151 return 0;
152 }
153
154 #if CONFIG_ASIDEDATA_FILTER
155
156 DEFINE_OPTIONS(asidedata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
157 AVFILTER_DEFINE_CLASS(asidedata);
158
159 static const AVFilterPad ainputs[] = {
160 {
161 .name = "default",
162 .type = AVMEDIA_TYPE_AUDIO,
163 .filter_frame = filter_frame,
164 },
165 { NULL }
166 };
167
168 static const AVFilterPad aoutputs[] = {
169 {
170 .name = "default",
171 .type = AVMEDIA_TYPE_AUDIO,
172 },
173 { NULL }
174 };
175
176 AVFilter ff_af_asidedata = {
177 .name = "asidedata",
178 .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame side data."),
179 .priv_size = sizeof(SideDataContext),
180 .priv_class = &asidedata_class,
181 .init = init,
182 .inputs = ainputs,
183 .outputs = aoutputs,
184 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
185 };
186 #endif /* CONFIG_ASIDEDATA_FILTER */
187
188 #if CONFIG_SIDEDATA_FILTER
189
190 DEFINE_OPTIONS(sidedata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
191 AVFILTER_DEFINE_CLASS(sidedata);
192
193 static const AVFilterPad inputs[] = {
194 {
195 .name = "default",
196 .type = AVMEDIA_TYPE_VIDEO,
197 .filter_frame = filter_frame,
198 },
199 { NULL }
200 };
201
202 static const AVFilterPad outputs[] = {
203 {
204 .name = "default",
205 .type = AVMEDIA_TYPE_VIDEO,
206 },
207 { NULL }
208 };
209
210 AVFilter ff_vf_sidedata = {
211 .name = "sidedata",
212 .description = NULL_IF_CONFIG_SMALL("Manipulate video frame side data."),
213 .priv_size = sizeof(SideDataContext),
214 .priv_class = &sidedata_class,
215 .init = init,
216 .inputs = inputs,
217 .outputs = outputs,
218 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
219 };
220 #endif /* CONFIG_SIDEDATA_FILTER */
221