f_streamselect.c 9.9 KB

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