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.

358 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. #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
  42. static const AVOption streamselect_options[] = {
  43. { "inputs", "number of input streams", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags=FLAGS },
  44. { "map", "input indexes to remap to outputs", OFFSET(map_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=TFLAGS },
  45. { NULL }
  46. };
  47. AVFILTER_DEFINE_CLASS(streamselect);
  48. static int process_frame(FFFrameSync *fs)
  49. {
  50. AVFilterContext *ctx = fs->parent;
  51. StreamSelectContext *s = fs->opaque;
  52. AVFrame **in = s->frames;
  53. int i, j, ret = 0, have_out = 0;
  54. for (i = 0; i < ctx->nb_inputs; i++) {
  55. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  56. return ret;
  57. }
  58. for (j = 0; j < ctx->nb_inputs; j++) {
  59. for (i = 0; i < s->nb_map; i++) {
  60. if (s->map[i] == j) {
  61. AVFrame *out;
  62. if (s->is_audio && s->last_pts[j] == in[j]->pts &&
  63. ctx->outputs[i]->frame_count_in > 0)
  64. continue;
  65. out = av_frame_clone(in[j]);
  66. if (!out)
  67. return AVERROR(ENOMEM);
  68. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
  69. s->last_pts[j] = in[j]->pts;
  70. ret = ff_filter_frame(ctx->outputs[i], out);
  71. have_out = 1;
  72. if (ret < 0)
  73. return ret;
  74. }
  75. }
  76. }
  77. if (!have_out)
  78. ff_filter_set_ready(ctx, 100);
  79. return ret;
  80. }
  81. static int activate(AVFilterContext *ctx)
  82. {
  83. StreamSelectContext *s = ctx->priv;
  84. return ff_framesync_activate(&s->fs);
  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, int is_input, int is_audio)
  132. {
  133. const char *padtype = is_input ? "in" : "out";
  134. int i = 0, ret = 0;
  135. for (i = 0; i < nb_pads; i++) {
  136. AVFilterPad pad = { 0 };
  137. pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
  138. pad.name = av_asprintf("%sput%d", padtype, i);
  139. if (!pad.name)
  140. return AVERROR(ENOMEM);
  141. av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
  142. if (is_input) {
  143. ret = ff_insert_inpad(ctx, i, &pad);
  144. } else {
  145. pad.config_props = config_output;
  146. ret = ff_insert_outpad(ctx, i, &pad);
  147. }
  148. if (ret < 0) {
  149. av_freep(&pad.name);
  150. return ret;
  151. }
  152. }
  153. return 0;
  154. }
  155. static int parse_mapping(AVFilterContext *ctx, const char *map)
  156. {
  157. StreamSelectContext *s = ctx->priv;
  158. int *new_map;
  159. int new_nb_map = 0;
  160. if (!map) {
  161. av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
  162. return AVERROR(EINVAL);
  163. }
  164. new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
  165. if (!new_map)
  166. return AVERROR(ENOMEM);
  167. while (1) {
  168. char *p;
  169. const int n = strtol(map, &p, 0);
  170. av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
  171. if (map == p)
  172. break;
  173. map = p;
  174. if (new_nb_map >= s->nb_inputs) {
  175. av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
  176. "input pads available\n", s->nb_inputs);
  177. av_free(new_map);
  178. return AVERROR(EINVAL);
  179. }
  180. if (n < 0 || n >= ctx->nb_inputs) {
  181. av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
  182. "(there is only %d input streams defined)\n",
  183. n, s->nb_inputs);
  184. av_free(new_map);
  185. return AVERROR(EINVAL);
  186. }
  187. av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
  188. new_map[new_nb_map++] = n;
  189. }
  190. if (!new_nb_map) {
  191. av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
  192. av_free(new_map);
  193. return AVERROR(EINVAL);
  194. }
  195. av_freep(&s->map);
  196. s->map = new_map;
  197. s->nb_map = new_nb_map;
  198. av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
  199. return 0;
  200. }
  201. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  202. char *res, int res_len, int flags)
  203. {
  204. if (!strcmp(cmd, "map")) {
  205. int ret = parse_mapping(ctx, args);
  206. if (ret < 0)
  207. return ret;
  208. return avfilter_config_links(ctx);
  209. }
  210. return AVERROR(ENOSYS);
  211. }
  212. static av_cold int init(AVFilterContext *ctx)
  213. {
  214. StreamSelectContext *s = ctx->priv;
  215. int ret, nb_outputs = 0;
  216. char *map = s->map_str;
  217. if (!strcmp(ctx->filter->name, "astreamselect"))
  218. s->is_audio = 1;
  219. for (; map;) {
  220. char *p;
  221. strtol(map, &p, 0);
  222. if (map == p)
  223. break;
  224. nb_outputs++;
  225. map = p;
  226. }
  227. s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
  228. if (!s->last_pts)
  229. return AVERROR(ENOMEM);
  230. if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
  231. (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
  232. return ret;
  233. av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
  234. ctx->nb_inputs, ctx->nb_outputs);
  235. return parse_mapping(ctx, s->map_str);
  236. }
  237. static av_cold void uninit(AVFilterContext *ctx)
  238. {
  239. StreamSelectContext *s = ctx->priv;
  240. av_freep(&s->last_pts);
  241. av_freep(&s->map);
  242. av_freep(&s->frames);
  243. ff_framesync_uninit(&s->fs);
  244. for (int i = 0; i < ctx->nb_inputs; i++)
  245. av_freep(&ctx->input_pads[i].name);
  246. for (int i = 0; i < ctx->nb_outputs; i++)
  247. av_freep(&ctx->output_pads[i].name);
  248. }
  249. static int query_formats(AVFilterContext *ctx)
  250. {
  251. AVFilterFormats *formats, *rates = NULL;
  252. AVFilterChannelLayouts *layouts = NULL;
  253. int ret, i;
  254. for (i = 0; i < ctx->nb_inputs; i++) {
  255. formats = ff_all_formats(ctx->inputs[i]->type);
  256. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  257. return ret;
  258. if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
  259. rates = ff_all_samplerates();
  260. if ((ret = ff_set_common_samplerates(ctx, rates)) < 0)
  261. return ret;
  262. layouts = ff_all_channel_counts();
  263. if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
  264. return ret;
  265. }
  266. }
  267. return 0;
  268. }
  269. AVFilter ff_vf_streamselect = {
  270. .name = "streamselect",
  271. .description = NULL_IF_CONFIG_SMALL("Select video streams"),
  272. .init = init,
  273. .query_formats = query_formats,
  274. .process_command = process_command,
  275. .uninit = uninit,
  276. .activate = activate,
  277. .priv_size = sizeof(StreamSelectContext),
  278. .priv_class = &streamselect_class,
  279. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  280. };
  281. #define astreamselect_options streamselect_options
  282. AVFILTER_DEFINE_CLASS(astreamselect);
  283. AVFilter ff_af_astreamselect = {
  284. .name = "astreamselect",
  285. .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
  286. .init = init,
  287. .query_formats = query_formats,
  288. .process_command = process_command,
  289. .uninit = uninit,
  290. .activate = activate,
  291. .priv_size = sizeof(StreamSelectContext),
  292. .priv_class = &astreamselect_class,
  293. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  294. };