f_streamselect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include "libavutil/avstring.h"
  19. #include "libavutil/internal.h"
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "audio.h"
  23. #include "formats.h"
  24. #include "framesync.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct StreamSelectContext {
  28. const AVClass *class;
  29. int nb_inputs;
  30. char *map_str;
  31. int *map;
  32. int nb_map;
  33. int is_audio;
  34. int64_t *last_pts;
  35. AVFrame **frames;
  36. FFFrameSync fs;
  37. } StreamSelectContext;
  38. #define OFFSET(x) offsetof(StreamSelectContext, x)
  39. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  40. static const AVOption streamselect_options[] = {
  41. { "inputs", "number of input streams", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags=FLAGS },
  42. { "map", "input indexes to remap to outputs", OFFSET(map_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=FLAGS },
  43. { NULL }
  44. };
  45. AVFILTER_DEFINE_CLASS(streamselect);
  46. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  47. {
  48. StreamSelectContext *s = inlink->dst->priv;
  49. return ff_framesync_filter_frame(&s->fs, inlink, in);
  50. }
  51. static int process_frame(FFFrameSync *fs)
  52. {
  53. AVFilterContext *ctx = fs->parent;
  54. StreamSelectContext *s = fs->opaque;
  55. AVFrame **in = s->frames;
  56. int i, j, ret = 0;
  57. for (i = 0; i < ctx->nb_inputs; i++) {
  58. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  59. return ret;
  60. }
  61. for (j = 0; j < ctx->nb_inputs; j++) {
  62. for (i = 0; i < s->nb_map; i++) {
  63. if (s->map[i] == j) {
  64. AVFrame *out;
  65. if (s->is_audio && s->last_pts[j] == in[j]->pts &&
  66. ctx->outputs[i]->frame_count > 0)
  67. continue;
  68. out = av_frame_clone(in[j]);
  69. if (!out)
  70. return AVERROR(ENOMEM);
  71. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
  72. s->last_pts[j] = in[j]->pts;
  73. ret = ff_filter_frame(ctx->outputs[i], out);
  74. if (ret < 0)
  75. return ret;
  76. }
  77. }
  78. }
  79. return ret;
  80. }
  81. static int request_frame(AVFilterLink *outlink)
  82. {
  83. StreamSelectContext *s = outlink->src->priv;
  84. return ff_framesync_request_frame(&s->fs, outlink);
  85. }
  86. static int config_output(AVFilterLink *outlink)
  87. {
  88. AVFilterContext *ctx = outlink->src;
  89. StreamSelectContext *s = ctx->priv;
  90. const int outlink_idx = FF_OUTLINK_IDX(outlink);
  91. const int inlink_idx = s->map[outlink_idx];
  92. AVFilterLink *inlink = ctx->inputs[inlink_idx];
  93. FFFrameSyncIn *in;
  94. int i, ret;
  95. av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
  96. "with settings from input link %d\n",
  97. outlink_idx, inlink_idx);
  98. switch (outlink->type) {
  99. case AVMEDIA_TYPE_VIDEO:
  100. outlink->w = inlink->w;
  101. outlink->h = inlink->h;
  102. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  103. outlink->frame_rate = inlink->frame_rate;
  104. break;
  105. case AVMEDIA_TYPE_AUDIO:
  106. outlink->sample_rate = inlink->sample_rate;
  107. outlink->channels = inlink->channels;
  108. outlink->channel_layout = inlink->channel_layout;
  109. break;
  110. }
  111. outlink->time_base = inlink->time_base;
  112. outlink->format = inlink->format;
  113. if (s->fs.opaque == s)
  114. return 0;
  115. if ((ret = ff_framesync_init(&s->fs, ctx, ctx->nb_inputs)) < 0)
  116. return ret;
  117. in = s->fs.in;
  118. s->fs.opaque = s;
  119. s->fs.on_event = process_frame;
  120. for (i = 0; i < ctx->nb_inputs; i++) {
  121. in[i].time_base = ctx->inputs[i]->time_base;
  122. in[i].sync = 1;
  123. in[i].before = EXT_STOP;
  124. in[i].after = EXT_STOP;
  125. }
  126. s->frames = av_calloc(ctx->nb_inputs, sizeof(*s->frames));
  127. if (!s->frames)
  128. return AVERROR(ENOMEM);
  129. return ff_framesync_configure(&s->fs);
  130. }
  131. static int parse_definition(AVFilterContext *ctx, int nb_pads, void *filter_frame, int is_audio)
  132. {
  133. const int is_input = !!filter_frame;
  134. const char *padtype = is_input ? "in" : "out";
  135. int i = 0, ret = 0;
  136. for (i = 0; i < nb_pads; i++) {
  137. AVFilterPad pad = { 0 };
  138. pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
  139. pad.name = av_asprintf("%sput%d", padtype, i);
  140. if (!pad.name)
  141. return AVERROR(ENOMEM);
  142. av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
  143. if (is_input) {
  144. pad.filter_frame = filter_frame;
  145. ret = ff_insert_inpad(ctx, i, &pad);
  146. } else {
  147. pad.config_props = config_output;
  148. pad.request_frame = request_frame;
  149. ret = ff_insert_outpad(ctx, i, &pad);
  150. }
  151. if (ret < 0) {
  152. av_freep(&pad.name);
  153. return ret;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int parse_mapping(AVFilterContext *ctx, const char *map)
  159. {
  160. StreamSelectContext *s = ctx->priv;
  161. int *new_map;
  162. int new_nb_map = 0;
  163. if (!map) {
  164. av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
  165. return AVERROR(EINVAL);
  166. }
  167. new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
  168. if (!new_map)
  169. return AVERROR(ENOMEM);
  170. while (1) {
  171. char *p;
  172. const int n = strtol(map, &p, 0);
  173. av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
  174. if (map == p)
  175. break;
  176. map = p;
  177. if (new_nb_map >= s->nb_inputs) {
  178. av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
  179. "input pads available\n", s->nb_inputs);
  180. av_free(new_map);
  181. return AVERROR(EINVAL);
  182. }
  183. if (n < 0 || n >= ctx->nb_inputs) {
  184. av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
  185. "(there is only %d input streams defined)\n",
  186. n, s->nb_inputs);
  187. av_free(new_map);
  188. return AVERROR(EINVAL);
  189. }
  190. av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
  191. new_map[new_nb_map++] = n;
  192. }
  193. if (!new_nb_map) {
  194. av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
  195. av_free(new_map);
  196. return AVERROR(EINVAL);
  197. }
  198. av_freep(&s->map);
  199. s->map = new_map;
  200. s->nb_map = new_nb_map;
  201. av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
  202. return 0;
  203. }
  204. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  205. char *res, int res_len, int flags)
  206. {
  207. if (!strcmp(cmd, "map")) {
  208. int ret = parse_mapping(ctx, args);
  209. if (ret < 0)
  210. return ret;
  211. return avfilter_config_links(ctx);
  212. }
  213. return AVERROR(ENOSYS);
  214. }
  215. static av_cold int init(AVFilterContext *ctx)
  216. {
  217. StreamSelectContext *s = ctx->priv;
  218. int ret, nb_outputs = 0;
  219. char *map = s->map_str;
  220. if (!strcmp(ctx->filter->name, "astreamselect"))
  221. s->is_audio = 1;
  222. for (;;) {
  223. char *p;
  224. strtol(map, &p, 0);
  225. if (map == p)
  226. break;
  227. nb_outputs++;
  228. map = p;
  229. }
  230. s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
  231. if (!s->last_pts)
  232. return AVERROR(ENOMEM);
  233. if ((ret = parse_definition(ctx, s->nb_inputs, filter_frame, s->is_audio)) < 0 ||
  234. (ret = parse_definition(ctx, nb_outputs, NULL, s->is_audio)) < 0)
  235. return ret;
  236. av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
  237. ctx->nb_inputs, ctx->nb_outputs);
  238. return parse_mapping(ctx, s->map_str);
  239. }
  240. static av_cold void uninit(AVFilterContext *ctx)
  241. {
  242. StreamSelectContext *s = ctx->priv;
  243. av_freep(&s->last_pts);
  244. av_freep(&s->map);
  245. av_freep(&s->frames);
  246. ff_framesync_uninit(&s->fs);
  247. }
  248. static int query_formats(AVFilterContext *ctx)
  249. {
  250. AVFilterFormats *formats, *rates = NULL;
  251. AVFilterChannelLayouts *layouts = NULL;
  252. int ret, i;
  253. for (i = 0; i < ctx->nb_inputs; i++) {
  254. formats = ff_all_formats(ctx->inputs[i]->type);
  255. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  256. return ret;
  257. if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
  258. rates = ff_all_samplerates();
  259. if ((ret = ff_set_common_samplerates(ctx, rates)) < 0)
  260. return ret;
  261. layouts = ff_all_channel_counts();
  262. if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
  263. return ret;
  264. }
  265. }
  266. return 0;
  267. }
  268. AVFilter ff_vf_streamselect = {
  269. .name = "streamselect",
  270. .description = NULL_IF_CONFIG_SMALL("Select video streams"),
  271. .init = init,
  272. .query_formats = query_formats,
  273. .process_command = process_command,
  274. .uninit = uninit,
  275. .priv_size = sizeof(StreamSelectContext),
  276. .priv_class = &streamselect_class,
  277. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  278. };
  279. #define astreamselect_options streamselect_options
  280. AVFILTER_DEFINE_CLASS(astreamselect);
  281. AVFilter ff_af_astreamselect = {
  282. .name = "astreamselect",
  283. .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
  284. .init = init,
  285. .query_formats = query_formats,
  286. .process_command = process_command,
  287. .uninit = uninit,
  288. .priv_size = sizeof(StreamSelectContext),
  289. .priv_class = &astreamselect_class,
  290. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  291. };