avf_concat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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]->in_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]->in_samplerates)) < 0)
  85. return ret;
  86. layouts = ff_all_channel_layouts();
  87. if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->in_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]->out_formats)) < 0)
  93. return ret;
  94. if (type == AVMEDIA_TYPE_AUDIO) {
  95. if ((ret = ff_formats_ref(rates, &ctx->inputs[idx]->out_samplerates)) < 0 ||
  96. (ret = ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->out_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. for (seg = 1; seg < cat->nb_segments; seg++) {
  120. inlink = ctx->inputs[in_no += ctx->nb_outputs];
  121. if (!outlink->sample_aspect_ratio.num)
  122. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  123. /* possible enhancement: unsafe mode, do not check */
  124. if (outlink->w != inlink->w ||
  125. outlink->h != inlink->h ||
  126. outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num &&
  127. inlink->sample_aspect_ratio.num ||
  128. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  129. av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
  130. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  131. "output link %s parameters (%dx%d, SAR %d:%d)\n",
  132. ctx->input_pads[in_no].name, inlink->w, inlink->h,
  133. inlink->sample_aspect_ratio.num,
  134. inlink->sample_aspect_ratio.den,
  135. ctx->input_pads[out_no].name, outlink->w, outlink->h,
  136. outlink->sample_aspect_ratio.num,
  137. outlink->sample_aspect_ratio.den);
  138. if (!cat->unsafe)
  139. return AVERROR(EINVAL);
  140. }
  141. }
  142. return 0;
  143. }
  144. static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
  145. {
  146. ConcatContext *cat = ctx->priv;
  147. unsigned out_no = in_no % ctx->nb_outputs;
  148. AVFilterLink * inlink = ctx-> inputs[ in_no];
  149. AVFilterLink *outlink = ctx->outputs[out_no];
  150. struct concat_in *in = &cat->in[in_no];
  151. buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
  152. in->pts = buf->pts;
  153. in->nb_frames++;
  154. /* add duration to input PTS */
  155. if (inlink->sample_rate)
  156. /* use number of audio samples */
  157. in->pts += av_rescale_q(buf->nb_samples,
  158. av_make_q(1, inlink->sample_rate),
  159. outlink->time_base);
  160. else if (in->nb_frames >= 2)
  161. /* use mean duration */
  162. in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
  163. buf->pts += cat->delta_ts;
  164. return ff_filter_frame(outlink, buf);
  165. }
  166. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  167. {
  168. AVFilterContext *ctx = inlink->dst;
  169. unsigned in_no = FF_INLINK_IDX(inlink);
  170. AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
  171. return ff_get_video_buffer(outlink, w, h);
  172. }
  173. static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
  174. {
  175. AVFilterContext *ctx = inlink->dst;
  176. unsigned in_no = FF_INLINK_IDX(inlink);
  177. AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
  178. return ff_get_audio_buffer(outlink, nb_samples);
  179. }
  180. static void close_input(AVFilterContext *ctx, unsigned in_no)
  181. {
  182. ConcatContext *cat = ctx->priv;
  183. cat->in[in_no].eof = 1;
  184. cat->nb_in_active--;
  185. av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
  186. ctx->input_pads[in_no].name, cat->nb_in_active);
  187. }
  188. static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
  189. {
  190. ConcatContext *cat = ctx->priv;
  191. unsigned i = cat->cur_idx;
  192. unsigned imax = i + ctx->nb_outputs;
  193. int64_t pts;
  194. pts = cat->in[i++].pts;
  195. for (; i < imax; i++)
  196. pts = FFMAX(pts, cat->in[i].pts);
  197. cat->delta_ts += pts;
  198. *seg_delta = pts;
  199. }
  200. static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
  201. int64_t seg_delta)
  202. {
  203. ConcatContext *cat = ctx->priv;
  204. AVFilterLink *outlink = ctx->outputs[out_no];
  205. int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
  206. int64_t nb_samples, sent = 0;
  207. int frame_nb_samples, ret;
  208. AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
  209. AVFrame *buf;
  210. if (!rate_tb.den)
  211. return AVERROR_BUG;
  212. nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
  213. outlink->time_base, rate_tb);
  214. frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
  215. while (nb_samples) {
  216. frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
  217. buf = ff_get_audio_buffer(outlink, frame_nb_samples);
  218. if (!buf)
  219. return AVERROR(ENOMEM);
  220. av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
  221. outlink->channels, outlink->format);
  222. buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
  223. ret = ff_filter_frame(outlink, buf);
  224. if (ret < 0)
  225. return ret;
  226. sent += frame_nb_samples;
  227. nb_samples -= frame_nb_samples;
  228. }
  229. return 0;
  230. }
  231. static int flush_segment(AVFilterContext *ctx)
  232. {
  233. int ret;
  234. ConcatContext *cat = ctx->priv;
  235. unsigned str, str_max;
  236. int64_t seg_delta;
  237. find_next_delta_ts(ctx, &seg_delta);
  238. cat->cur_idx += ctx->nb_outputs;
  239. cat->nb_in_active = ctx->nb_outputs;
  240. av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
  241. cat->delta_ts);
  242. if (cat->cur_idx < ctx->nb_inputs) {
  243. /* pad audio streams with silence */
  244. str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
  245. str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
  246. for (; str < str_max; str++) {
  247. ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
  248. seg_delta);
  249. if (ret < 0)
  250. return ret;
  251. }
  252. }
  253. return 0;
  254. }
  255. static av_cold int init(AVFilterContext *ctx)
  256. {
  257. ConcatContext *cat = ctx->priv;
  258. unsigned seg, type, str;
  259. int ret;
  260. /* create input pads */
  261. for (seg = 0; seg < cat->nb_segments; seg++) {
  262. for (type = 0; type < TYPE_ALL; type++) {
  263. for (str = 0; str < cat->nb_streams[type]; str++) {
  264. AVFilterPad pad = {
  265. .type = type,
  266. .get_video_buffer = get_video_buffer,
  267. .get_audio_buffer = get_audio_buffer,
  268. };
  269. pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str);
  270. if ((ret = ff_insert_inpad(ctx, ctx->nb_inputs, &pad)) < 0) {
  271. av_freep(&pad.name);
  272. return ret;
  273. }
  274. }
  275. }
  276. }
  277. /* create output pads */
  278. for (type = 0; type < TYPE_ALL; type++) {
  279. for (str = 0; str < cat->nb_streams[type]; str++) {
  280. AVFilterPad pad = {
  281. .type = type,
  282. .config_props = config_output,
  283. };
  284. pad.name = av_asprintf("out:%c%d", "va"[type], str);
  285. if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
  286. av_freep(&pad.name);
  287. return ret;
  288. }
  289. }
  290. }
  291. cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
  292. if (!cat->in)
  293. return AVERROR(ENOMEM);
  294. cat->nb_in_active = ctx->nb_outputs;
  295. return 0;
  296. }
  297. static av_cold void uninit(AVFilterContext *ctx)
  298. {
  299. ConcatContext *cat = ctx->priv;
  300. unsigned i;
  301. for (i = 0; i < ctx->nb_inputs; i++)
  302. av_freep(&ctx->input_pads[i].name);
  303. for (i = 0; i < ctx->nb_outputs; i++)
  304. av_freep(&ctx->output_pads[i].name);
  305. av_freep(&cat->in);
  306. }
  307. static int activate(AVFilterContext *ctx)
  308. {
  309. ConcatContext *cat = ctx->priv;
  310. AVFrame *frame;
  311. unsigned i, j;
  312. int ret, status;
  313. int64_t pts;
  314. /* Forward status back */
  315. for (i = 0; i < ctx->nb_outputs; i++) {
  316. status = ff_outlink_get_status(ctx->outputs[i]);
  317. if (!status)
  318. continue;
  319. for (j = i; j < ctx->nb_inputs; j += ctx->nb_outputs) {
  320. if (!cat->in[j].eof) {
  321. cat->in[j].eof = 1;
  322. ff_inlink_set_status(ctx->inputs[j], status);
  323. return 0;
  324. }
  325. }
  326. }
  327. /* Forward available frames */
  328. if (cat->cur_idx < ctx->nb_inputs) {
  329. for (i = 0; i < ctx->nb_outputs; i++) {
  330. ret = ff_inlink_consume_frame(ctx->inputs[cat->cur_idx + i], &frame);
  331. if (ret < 0)
  332. return ret;
  333. if (ret) {
  334. ff_filter_set_ready(ctx, 10);
  335. return push_frame(ctx, cat->cur_idx + i, frame);
  336. }
  337. }
  338. }
  339. /* Forward status change */
  340. if (cat->cur_idx < ctx->nb_inputs) {
  341. for (i = 0; i < ctx->nb_outputs; i++) {
  342. ret = ff_inlink_acknowledge_status(ctx->inputs[cat->cur_idx + i], &status, &pts);
  343. /* TODO use pts */
  344. if (ret > 0) {
  345. close_input(ctx, cat->cur_idx + i);
  346. if (cat->cur_idx + ctx->nb_outputs >= ctx->nb_inputs) {
  347. ff_outlink_set_status(ctx->outputs[i], status, pts);
  348. }
  349. if (!cat->nb_in_active) {
  350. ret = flush_segment(ctx);
  351. if (ret < 0)
  352. return ret;
  353. }
  354. ff_filter_set_ready(ctx, 10);
  355. return 0;
  356. }
  357. }
  358. }
  359. ret = FFERROR_NOT_READY;
  360. for (i = 0; i < ctx->nb_outputs; i++) {
  361. if (ff_outlink_frame_wanted(ctx->outputs[i])) {
  362. if (cat->in[cat->cur_idx + i].eof) {
  363. for (j = 0; j < ctx->nb_outputs; j++)
  364. if (!cat->in[cat->cur_idx + j].eof)
  365. ff_inlink_request_frame(ctx->inputs[cat->cur_idx + j]);
  366. return 0;
  367. } else {
  368. ff_inlink_request_frame(ctx->inputs[cat->cur_idx + i]);
  369. ret = 0;
  370. }
  371. }
  372. }
  373. return ret;
  374. }
  375. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  376. char *res, int res_len, int flags)
  377. {
  378. int ret = AVERROR(ENOSYS);
  379. if (!strcmp(cmd, "next")) {
  380. av_log(ctx, AV_LOG_VERBOSE, "Command received: next\n");
  381. return flush_segment(ctx);
  382. }
  383. return ret;
  384. }
  385. AVFilter ff_avf_concat = {
  386. .name = "concat",
  387. .description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
  388. .init = init,
  389. .uninit = uninit,
  390. .query_formats = query_formats,
  391. .activate = activate,
  392. .priv_size = sizeof(ConcatContext),
  393. .inputs = NULL,
  394. .outputs = NULL,
  395. .priv_class = &concat_class,
  396. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  397. .process_command = process_command,
  398. };