You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
10KB

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