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.

464 lines
16KB

  1. /*
  2. * Copyright (c) 2012 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * concat audio-video filter
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #include "avfilter.h"
  29. #include "filters.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #include "audio.h"
  33. #define TYPE_ALL 2
  34. typedef struct ConcatContext {
  35. const AVClass *class;
  36. unsigned nb_streams[TYPE_ALL]; /**< number of out streams of each type */
  37. unsigned nb_segments;
  38. unsigned cur_idx; /**< index of the first input of current segment */
  39. int64_t delta_ts; /**< timestamp to add to produce output timestamps */
  40. unsigned nb_in_active; /**< number of active inputs in current segment */
  41. unsigned unsafe;
  42. struct concat_in {
  43. int64_t pts;
  44. int64_t nb_frames;
  45. unsigned eof;
  46. } *in;
  47. } ConcatContext;
  48. #define OFFSET(x) offsetof(ConcatContext, x)
  49. #define A AV_OPT_FLAG_AUDIO_PARAM
  50. #define F AV_OPT_FLAG_FILTERING_PARAM
  51. #define V AV_OPT_FLAG_VIDEO_PARAM
  52. static const AVOption concat_options[] = {
  53. { "n", "specify the number of segments", OFFSET(nb_segments),
  54. AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, V|A|F},
  55. { "v", "specify the number of video streams",
  56. OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]),
  57. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F },
  58. { "a", "specify the number of audio streams",
  59. OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]),
  60. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F},
  61. { "unsafe", "enable unsafe mode",
  62. OFFSET(unsafe),
  63. AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, V|A|F},
  64. { NULL }
  65. };
  66. AVFILTER_DEFINE_CLASS(concat);
  67. static int query_formats(AVFilterContext *ctx)
  68. {
  69. ConcatContext *cat = ctx->priv;
  70. unsigned type, nb_str, idx0 = 0, idx, str, seg;
  71. AVFilterFormats *formats, *rates = NULL;
  72. AVFilterChannelLayouts *layouts = NULL;
  73. int ret;
  74. for (type = 0; type < TYPE_ALL; type++) {
  75. nb_str = cat->nb_streams[type];
  76. for (str = 0; str < nb_str; str++) {
  77. idx = idx0;
  78. /* Set the output formats */
  79. formats = ff_all_formats(type);
  80. if ((ret = ff_formats_ref(formats, &ctx->outputs[idx]->incfg.formats)) < 0)
  81. return ret;
  82. if (type == AVMEDIA_TYPE_AUDIO) {
  83. rates = ff_all_samplerates();
  84. if ((ret = ff_formats_ref(rates, &ctx->outputs[idx]->incfg.samplerates)) < 0)
  85. return ret;
  86. layouts = ff_all_channel_layouts();
  87. if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->incfg.channel_layouts)) < 0)
  88. return ret;
  89. }
  90. /* Set the same formats for each corresponding input */
  91. for (seg = 0; seg < cat->nb_segments; seg++) {
  92. if ((ret = ff_formats_ref(formats, &ctx->inputs[idx]->outcfg.formats)) < 0)
  93. return ret;
  94. if (type == AVMEDIA_TYPE_AUDIO) {
  95. if ((ret = ff_formats_ref(rates, &ctx->inputs[idx]->outcfg.samplerates)) < 0 ||
  96. (ret = ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->outcfg.channel_layouts)) < 0)
  97. return ret;
  98. }
  99. idx += ctx->nb_outputs;
  100. }
  101. idx0++;
  102. }
  103. }
  104. return 0;
  105. }
  106. static int config_output(AVFilterLink *outlink)
  107. {
  108. AVFilterContext *ctx = outlink->src;
  109. ConcatContext *cat = ctx->priv;
  110. unsigned out_no = FF_OUTLINK_IDX(outlink);
  111. unsigned in_no = out_no, seg;
  112. AVFilterLink *inlink = ctx->inputs[in_no];
  113. /* enhancement: find a common one */
  114. outlink->time_base = AV_TIME_BASE_Q;
  115. outlink->w = inlink->w;
  116. outlink->h = inlink->h;
  117. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  118. outlink->format = inlink->format;
  119. outlink->frame_rate = inlink->frame_rate;
  120. for (seg = 1; seg < cat->nb_segments; seg++) {
  121. inlink = ctx->inputs[in_no + seg * ctx->nb_outputs];
  122. if (outlink->frame_rate.num != inlink->frame_rate.num ||
  123. outlink->frame_rate.den != inlink->frame_rate.den) {
  124. av_log(ctx, AV_LOG_VERBOSE,
  125. "Video inputs have different frame rates, output will be VFR\n");
  126. outlink->frame_rate = av_make_q(1, 0);
  127. break;
  128. }
  129. }
  130. for (seg = 1; seg < cat->nb_segments; seg++) {
  131. inlink = ctx->inputs[in_no + seg * ctx->nb_outputs];
  132. if (!outlink->sample_aspect_ratio.num)
  133. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  134. /* possible enhancement: unsafe mode, do not check */
  135. if (outlink->w != inlink->w ||
  136. outlink->h != inlink->h ||
  137. outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num &&
  138. inlink->sample_aspect_ratio.num ||
  139. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  140. av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
  141. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  142. "output link %s parameters (%dx%d, SAR %d:%d)\n",
  143. ctx->input_pads[in_no].name, inlink->w, inlink->h,
  144. inlink->sample_aspect_ratio.num,
  145. inlink->sample_aspect_ratio.den,
  146. ctx->input_pads[out_no].name, outlink->w, outlink->h,
  147. outlink->sample_aspect_ratio.num,
  148. outlink->sample_aspect_ratio.den);
  149. if (!cat->unsafe)
  150. return AVERROR(EINVAL);
  151. }
  152. }
  153. return 0;
  154. }
  155. static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
  156. {
  157. ConcatContext *cat = ctx->priv;
  158. unsigned out_no = in_no % ctx->nb_outputs;
  159. AVFilterLink * inlink = ctx-> inputs[ in_no];
  160. AVFilterLink *outlink = ctx->outputs[out_no];
  161. struct concat_in *in = &cat->in[in_no];
  162. buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
  163. in->pts = buf->pts;
  164. in->nb_frames++;
  165. /* add duration to input PTS */
  166. if (inlink->sample_rate)
  167. /* use number of audio samples */
  168. in->pts += av_rescale_q(buf->nb_samples,
  169. av_make_q(1, inlink->sample_rate),
  170. outlink->time_base);
  171. else if (in->nb_frames >= 2)
  172. /* use mean duration */
  173. in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
  174. buf->pts += cat->delta_ts;
  175. return ff_filter_frame(outlink, buf);
  176. }
  177. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  178. {
  179. AVFilterContext *ctx = inlink->dst;
  180. unsigned in_no = FF_INLINK_IDX(inlink);
  181. AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
  182. return ff_get_video_buffer(outlink, w, h);
  183. }
  184. static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
  185. {
  186. AVFilterContext *ctx = inlink->dst;
  187. unsigned in_no = FF_INLINK_IDX(inlink);
  188. AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
  189. return ff_get_audio_buffer(outlink, nb_samples);
  190. }
  191. static void close_input(AVFilterContext *ctx, unsigned in_no)
  192. {
  193. ConcatContext *cat = ctx->priv;
  194. cat->in[in_no].eof = 1;
  195. cat->nb_in_active--;
  196. av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
  197. ctx->input_pads[in_no].name, cat->nb_in_active);
  198. }
  199. static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
  200. {
  201. ConcatContext *cat = ctx->priv;
  202. unsigned i = cat->cur_idx;
  203. unsigned imax = i + ctx->nb_outputs;
  204. int64_t pts;
  205. pts = cat->in[i++].pts;
  206. for (; i < imax; i++)
  207. pts = FFMAX(pts, cat->in[i].pts);
  208. cat->delta_ts += pts;
  209. *seg_delta = pts;
  210. }
  211. static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
  212. int64_t seg_delta)
  213. {
  214. ConcatContext *cat = ctx->priv;
  215. AVFilterLink *outlink = ctx->outputs[out_no];
  216. int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
  217. int64_t nb_samples, sent = 0;
  218. int frame_nb_samples, ret;
  219. AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
  220. AVFrame *buf;
  221. if (!rate_tb.den)
  222. return AVERROR_BUG;
  223. if (cat->in[in_no].pts < INT64_MIN + seg_delta)
  224. return AVERROR_INVALIDDATA;
  225. if (seg_delta < cat->in[in_no].pts)
  226. return AVERROR_INVALIDDATA;
  227. nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
  228. outlink->time_base, rate_tb);
  229. frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
  230. while (nb_samples) {
  231. frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
  232. buf = ff_get_audio_buffer(outlink, frame_nb_samples);
  233. if (!buf)
  234. return AVERROR(ENOMEM);
  235. av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
  236. outlink->channels, outlink->format);
  237. buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
  238. ret = ff_filter_frame(outlink, buf);
  239. if (ret < 0)
  240. return ret;
  241. sent += frame_nb_samples;
  242. nb_samples -= frame_nb_samples;
  243. }
  244. return 0;
  245. }
  246. static int flush_segment(AVFilterContext *ctx)
  247. {
  248. int ret;
  249. ConcatContext *cat = ctx->priv;
  250. unsigned str, str_max;
  251. int64_t seg_delta;
  252. find_next_delta_ts(ctx, &seg_delta);
  253. cat->cur_idx += ctx->nb_outputs;
  254. cat->nb_in_active = ctx->nb_outputs;
  255. av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
  256. cat->delta_ts);
  257. if (cat->cur_idx < ctx->nb_inputs) {
  258. /* pad audio streams with silence */
  259. str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
  260. str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
  261. for (; str < str_max; str++) {
  262. ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
  263. seg_delta);
  264. if (ret < 0)
  265. return ret;
  266. }
  267. }
  268. return 0;
  269. }
  270. static av_cold int init(AVFilterContext *ctx)
  271. {
  272. ConcatContext *cat = ctx->priv;
  273. unsigned seg, type, str;
  274. int ret;
  275. /* create input pads */
  276. for (seg = 0; seg < cat->nb_segments; seg++) {
  277. for (type = 0; type < TYPE_ALL; type++) {
  278. for (str = 0; str < cat->nb_streams[type]; str++) {
  279. AVFilterPad pad = {
  280. .type = type,
  281. .get_video_buffer = get_video_buffer,
  282. .get_audio_buffer = get_audio_buffer,
  283. };
  284. pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str);
  285. if ((ret = ff_insert_inpad(ctx, ctx->nb_inputs, &pad)) < 0) {
  286. av_freep(&pad.name);
  287. return ret;
  288. }
  289. }
  290. }
  291. }
  292. /* create output pads */
  293. for (type = 0; type < TYPE_ALL; type++) {
  294. for (str = 0; str < cat->nb_streams[type]; str++) {
  295. AVFilterPad pad = {
  296. .type = type,
  297. .config_props = config_output,
  298. };
  299. pad.name = av_asprintf("out:%c%d", "va"[type], str);
  300. if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
  301. av_freep(&pad.name);
  302. return ret;
  303. }
  304. }
  305. }
  306. cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
  307. if (!cat->in)
  308. return AVERROR(ENOMEM);
  309. cat->nb_in_active = ctx->nb_outputs;
  310. return 0;
  311. }
  312. static av_cold void uninit(AVFilterContext *ctx)
  313. {
  314. ConcatContext *cat = ctx->priv;
  315. unsigned i;
  316. for (i = 0; i < ctx->nb_inputs; i++)
  317. av_freep(&ctx->input_pads[i].name);
  318. for (i = 0; i < ctx->nb_outputs; i++)
  319. av_freep(&ctx->output_pads[i].name);
  320. av_freep(&cat->in);
  321. }
  322. static int activate(AVFilterContext *ctx)
  323. {
  324. ConcatContext *cat = ctx->priv;
  325. AVFrame *frame;
  326. unsigned i, j;
  327. int ret, status;
  328. int64_t pts;
  329. /* Forward status back */
  330. for (i = 0; i < ctx->nb_outputs; i++) {
  331. status = ff_outlink_get_status(ctx->outputs[i]);
  332. if (!status)
  333. continue;
  334. for (j = i; j < ctx->nb_inputs; j += ctx->nb_outputs) {
  335. if (!cat->in[j].eof) {
  336. cat->in[j].eof = 1;
  337. ff_inlink_set_status(ctx->inputs[j], status);
  338. return 0;
  339. }
  340. }
  341. }
  342. /* Forward available frames */
  343. if (cat->cur_idx < ctx->nb_inputs) {
  344. for (i = 0; i < ctx->nb_outputs; i++) {
  345. ret = ff_inlink_consume_frame(ctx->inputs[cat->cur_idx + i], &frame);
  346. if (ret < 0)
  347. return ret;
  348. if (ret) {
  349. ff_filter_set_ready(ctx, 10);
  350. return push_frame(ctx, cat->cur_idx + i, frame);
  351. }
  352. }
  353. }
  354. /* Forward status change */
  355. if (cat->cur_idx < ctx->nb_inputs) {
  356. for (i = 0; i < ctx->nb_outputs; i++) {
  357. ret = ff_inlink_acknowledge_status(ctx->inputs[cat->cur_idx + i], &status, &pts);
  358. /* TODO use pts */
  359. if (ret > 0) {
  360. close_input(ctx, cat->cur_idx + i);
  361. if (cat->cur_idx + ctx->nb_outputs >= ctx->nb_inputs) {
  362. ff_outlink_set_status(ctx->outputs[i], status, pts);
  363. }
  364. if (!cat->nb_in_active) {
  365. ret = flush_segment(ctx);
  366. if (ret < 0)
  367. return ret;
  368. }
  369. ff_filter_set_ready(ctx, 10);
  370. return 0;
  371. }
  372. }
  373. }
  374. ret = FFERROR_NOT_READY;
  375. for (i = 0; i < ctx->nb_outputs; i++) {
  376. if (ff_outlink_frame_wanted(ctx->outputs[i])) {
  377. if (cat->in[cat->cur_idx + i].eof) {
  378. for (j = 0; j < ctx->nb_outputs; j++)
  379. if (!cat->in[cat->cur_idx + j].eof)
  380. ff_inlink_request_frame(ctx->inputs[cat->cur_idx + j]);
  381. return 0;
  382. } else {
  383. ff_inlink_request_frame(ctx->inputs[cat->cur_idx + i]);
  384. ret = 0;
  385. }
  386. }
  387. }
  388. return ret;
  389. }
  390. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  391. char *res, int res_len, int flags)
  392. {
  393. int ret = AVERROR(ENOSYS);
  394. if (!strcmp(cmd, "next")) {
  395. av_log(ctx, AV_LOG_VERBOSE, "Command received: next\n");
  396. return flush_segment(ctx);
  397. }
  398. return ret;
  399. }
  400. AVFilter ff_avf_concat = {
  401. .name = "concat",
  402. .description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
  403. .init = init,
  404. .uninit = uninit,
  405. .query_formats = query_formats,
  406. .activate = activate,
  407. .priv_size = sizeof(ConcatContext),
  408. .inputs = NULL,
  409. .outputs = NULL,
  410. .priv_class = &concat_class,
  411. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  412. .process_command = process_command,
  413. };