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.

423 lines
14KB

  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. #define FF_BUFQUEUE_SIZE 256
  30. #include "bufferqueue.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. #include "audio.h"
  34. #define TYPE_ALL 2
  35. typedef struct {
  36. const AVClass *class;
  37. unsigned nb_streams[TYPE_ALL]; /**< number of out streams of each type */
  38. unsigned nb_segments;
  39. unsigned cur_idx; /**< index of the first input of current segment */
  40. int64_t delta_ts; /**< timestamp to add to produce output timestamps */
  41. unsigned nb_in_active; /**< number of active inputs in current segment */
  42. unsigned unsafe;
  43. struct concat_in {
  44. int64_t pts;
  45. int64_t nb_frames;
  46. unsigned eof;
  47. struct FFBufQueue queue;
  48. } *in;
  49. } ConcatContext;
  50. #define OFFSET(x) offsetof(ConcatContext, x)
  51. #define A AV_OPT_FLAG_AUDIO_PARAM
  52. #define F AV_OPT_FLAG_FILTERING_PARAM
  53. #define V AV_OPT_FLAG_VIDEO_PARAM
  54. static const AVOption concat_options[] = {
  55. { "n", "specify the number of segments", OFFSET(nb_segments),
  56. AV_OPT_TYPE_INT, { .i64 = 2 }, 2, INT_MAX, V|A|F},
  57. { "v", "specify the number of video streams",
  58. OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]),
  59. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F },
  60. { "a", "specify the number of audio streams",
  61. OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]),
  62. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F},
  63. { "unsafe", "enable unsafe mode",
  64. OFFSET(unsafe),
  65. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|A|F},
  66. { 0 }
  67. };
  68. AVFILTER_DEFINE_CLASS(concat);
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. ConcatContext *cat = ctx->priv;
  72. unsigned type, nb_str, idx0 = 0, idx, str, seg;
  73. AVFilterFormats *formats, *rates = NULL;
  74. AVFilterChannelLayouts *layouts = NULL;
  75. for (type = 0; type < TYPE_ALL; type++) {
  76. nb_str = cat->nb_streams[type];
  77. for (str = 0; str < nb_str; str++) {
  78. idx = idx0;
  79. /* Set the output formats */
  80. formats = ff_all_formats(type);
  81. if (!formats)
  82. return AVERROR(ENOMEM);
  83. ff_formats_ref(formats, &ctx->outputs[idx]->in_formats);
  84. if (type == AVMEDIA_TYPE_AUDIO) {
  85. rates = ff_all_samplerates();
  86. if (!rates)
  87. return AVERROR(ENOMEM);
  88. ff_formats_ref(rates, &ctx->outputs[idx]->in_samplerates);
  89. layouts = ff_all_channel_layouts();
  90. if (!layouts)
  91. return AVERROR(ENOMEM);
  92. ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->in_channel_layouts);
  93. }
  94. /* Set the same formats for each corresponding input */
  95. for (seg = 0; seg < cat->nb_segments; seg++) {
  96. ff_formats_ref(formats, &ctx->inputs[idx]->out_formats);
  97. if (type == AVMEDIA_TYPE_AUDIO) {
  98. ff_formats_ref(rates, &ctx->inputs[idx]->out_samplerates);
  99. ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->out_channel_layouts);
  100. }
  101. idx += ctx->nb_outputs;
  102. }
  103. idx0++;
  104. }
  105. }
  106. return 0;
  107. }
  108. static int config_output(AVFilterLink *outlink)
  109. {
  110. AVFilterContext *ctx = outlink->src;
  111. ConcatContext *cat = ctx->priv;
  112. unsigned out_no = FF_OUTLINK_IDX(outlink);
  113. unsigned in_no = out_no, seg;
  114. AVFilterLink *inlink = ctx->inputs[in_no];
  115. /* enhancement: find a common one */
  116. outlink->time_base = AV_TIME_BASE_Q;
  117. outlink->w = inlink->w;
  118. outlink->h = inlink->h;
  119. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  120. outlink->format = inlink->format;
  121. for (seg = 1; seg < cat->nb_segments; seg++) {
  122. inlink = ctx->inputs[in_no += ctx->nb_outputs];
  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. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  128. av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
  129. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  130. "output link %s parameters (%dx%d, SAR %d:%d)\n",
  131. ctx->input_pads[in_no].name, inlink->w, inlink->h,
  132. inlink->sample_aspect_ratio.num,
  133. inlink->sample_aspect_ratio.den,
  134. ctx->input_pads[out_no].name, outlink->w, outlink->h,
  135. outlink->sample_aspect_ratio.num,
  136. outlink->sample_aspect_ratio.den);
  137. if (!cat->unsafe)
  138. return AVERROR(EINVAL);
  139. }
  140. }
  141. return 0;
  142. }
  143. static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
  144. {
  145. ConcatContext *cat = ctx->priv;
  146. unsigned out_no = in_no % ctx->nb_outputs;
  147. AVFilterLink * inlink = ctx-> inputs[ in_no];
  148. AVFilterLink *outlink = ctx->outputs[out_no];
  149. struct concat_in *in = &cat->in[in_no];
  150. buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
  151. in->pts = buf->pts;
  152. in->nb_frames++;
  153. /* add duration to input PTS */
  154. if (inlink->sample_rate)
  155. /* use number of audio samples */
  156. in->pts += av_rescale_q(buf->nb_samples,
  157. (AVRational){ 1, inlink->sample_rate },
  158. outlink->time_base);
  159. else if (in->nb_frames >= 2)
  160. /* use mean duration */
  161. in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
  162. buf->pts += cat->delta_ts;
  163. return ff_filter_frame(outlink, buf);
  164. }
  165. static int process_frame(AVFilterLink *inlink, AVFrame *buf)
  166. {
  167. AVFilterContext *ctx = inlink->dst;
  168. ConcatContext *cat = ctx->priv;
  169. unsigned in_no = FF_INLINK_IDX(inlink);
  170. if (in_no < cat->cur_idx) {
  171. av_log(ctx, AV_LOG_ERROR, "Frame after EOF on input %s\n",
  172. ctx->input_pads[in_no].name);
  173. av_frame_free(&buf);
  174. } else if (in_no >= cat->cur_idx + ctx->nb_outputs) {
  175. ff_bufqueue_add(ctx, &cat->in[in_no].queue, buf);
  176. } else {
  177. return push_frame(ctx, in_no, buf);
  178. }
  179. return 0;
  180. }
  181. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  182. {
  183. AVFilterContext *ctx = inlink->dst;
  184. unsigned in_no = FF_INLINK_IDX(inlink);
  185. AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
  186. return ff_get_video_buffer(outlink, w, h);
  187. }
  188. static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
  189. {
  190. AVFilterContext *ctx = inlink->dst;
  191. unsigned in_no = FF_INLINK_IDX(inlink);
  192. AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
  193. return ff_get_audio_buffer(outlink, nb_samples);
  194. }
  195. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  196. {
  197. return process_frame(inlink, buf);
  198. }
  199. static void close_input(AVFilterContext *ctx, unsigned in_no)
  200. {
  201. ConcatContext *cat = ctx->priv;
  202. cat->in[in_no].eof = 1;
  203. cat->nb_in_active--;
  204. av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
  205. ctx->input_pads[in_no].name, cat->nb_in_active);
  206. }
  207. static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
  208. {
  209. ConcatContext *cat = ctx->priv;
  210. unsigned i = cat->cur_idx;
  211. unsigned imax = i + ctx->nb_outputs;
  212. int64_t pts;
  213. pts = cat->in[i++].pts;
  214. for (; i < imax; i++)
  215. pts = FFMAX(pts, cat->in[i].pts);
  216. cat->delta_ts += pts;
  217. *seg_delta = pts;
  218. }
  219. static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
  220. int64_t seg_delta)
  221. {
  222. ConcatContext *cat = ctx->priv;
  223. AVFilterLink *outlink = ctx->outputs[out_no];
  224. int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
  225. int64_t nb_samples, sent = 0;
  226. int frame_nb_samples, ret;
  227. AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
  228. AVFrame *buf;
  229. int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
  230. if (!rate_tb.den)
  231. return AVERROR_BUG;
  232. nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
  233. outlink->time_base, rate_tb);
  234. frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
  235. while (nb_samples) {
  236. frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
  237. buf = ff_get_audio_buffer(outlink, frame_nb_samples);
  238. if (!buf)
  239. return AVERROR(ENOMEM);
  240. av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
  241. nb_channels, outlink->format);
  242. buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
  243. ret = ff_filter_frame(outlink, buf);
  244. if (ret < 0)
  245. return ret;
  246. sent += frame_nb_samples;
  247. nb_samples -= frame_nb_samples;
  248. }
  249. return 0;
  250. }
  251. static int flush_segment(AVFilterContext *ctx)
  252. {
  253. int ret;
  254. ConcatContext *cat = ctx->priv;
  255. unsigned str, str_max;
  256. int64_t seg_delta;
  257. find_next_delta_ts(ctx, &seg_delta);
  258. cat->cur_idx += ctx->nb_outputs;
  259. cat->nb_in_active = ctx->nb_outputs;
  260. av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
  261. cat->delta_ts);
  262. if (cat->cur_idx < ctx->nb_inputs) {
  263. /* pad audio streams with silence */
  264. str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
  265. str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
  266. for (; str < str_max; str++) {
  267. ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
  268. seg_delta);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. /* flush queued buffers */
  273. /* possible enhancement: flush in PTS order */
  274. str_max = cat->cur_idx + ctx->nb_outputs;
  275. for (str = cat->cur_idx; str < str_max; str++) {
  276. while (cat->in[str].queue.available) {
  277. ret = push_frame(ctx, str, ff_bufqueue_get(&cat->in[str].queue));
  278. if (ret < 0)
  279. return ret;
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. static int request_frame(AVFilterLink *outlink)
  286. {
  287. AVFilterContext *ctx = outlink->src;
  288. ConcatContext *cat = ctx->priv;
  289. unsigned out_no = FF_OUTLINK_IDX(outlink);
  290. unsigned in_no = out_no + cat->cur_idx;
  291. unsigned str, str_max;
  292. int ret;
  293. while (1) {
  294. if (in_no >= ctx->nb_inputs)
  295. return AVERROR_EOF;
  296. if (!cat->in[in_no].eof) {
  297. ret = ff_request_frame(ctx->inputs[in_no]);
  298. if (ret != AVERROR_EOF)
  299. return ret;
  300. close_input(ctx, in_no);
  301. }
  302. /* cycle on all inputs to finish the segment */
  303. /* possible enhancement: request in PTS order */
  304. str_max = cat->cur_idx + ctx->nb_outputs - 1;
  305. for (str = cat->cur_idx; cat->nb_in_active;
  306. str = str == str_max ? cat->cur_idx : str + 1) {
  307. if (cat->in[str].eof)
  308. continue;
  309. ret = ff_request_frame(ctx->inputs[str]);
  310. if (ret == AVERROR_EOF)
  311. close_input(ctx, str);
  312. else if (ret < 0)
  313. return ret;
  314. }
  315. ret = flush_segment(ctx);
  316. if (ret < 0)
  317. return ret;
  318. in_no += ctx->nb_outputs;
  319. }
  320. }
  321. static av_cold int init(AVFilterContext *ctx)
  322. {
  323. ConcatContext *cat = ctx->priv;
  324. unsigned seg, type, str;
  325. /* create input pads */
  326. for (seg = 0; seg < cat->nb_segments; seg++) {
  327. for (type = 0; type < TYPE_ALL; type++) {
  328. for (str = 0; str < cat->nb_streams[type]; str++) {
  329. AVFilterPad pad = {
  330. .type = type,
  331. .get_video_buffer = get_video_buffer,
  332. .get_audio_buffer = get_audio_buffer,
  333. .filter_frame = filter_frame,
  334. };
  335. pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str);
  336. ff_insert_inpad(ctx, ctx->nb_inputs, &pad);
  337. }
  338. }
  339. }
  340. /* create output pads */
  341. for (type = 0; type < TYPE_ALL; type++) {
  342. for (str = 0; str < cat->nb_streams[type]; str++) {
  343. AVFilterPad pad = {
  344. .type = type,
  345. .config_props = config_output,
  346. .request_frame = request_frame,
  347. };
  348. pad.name = av_asprintf("out:%c%d", "va"[type], str);
  349. ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
  350. }
  351. }
  352. cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
  353. if (!cat->in)
  354. return AVERROR(ENOMEM);
  355. cat->nb_in_active = ctx->nb_outputs;
  356. return 0;
  357. }
  358. static av_cold void uninit(AVFilterContext *ctx)
  359. {
  360. ConcatContext *cat = ctx->priv;
  361. unsigned i;
  362. for (i = 0; i < ctx->nb_inputs; i++) {
  363. av_freep(&ctx->input_pads[i].name);
  364. ff_bufqueue_discard_all(&cat->in[i].queue);
  365. }
  366. for (i = 0; i < ctx->nb_outputs; i++)
  367. av_freep(&ctx->output_pads[i].name);
  368. av_free(cat->in);
  369. }
  370. AVFilter avfilter_avf_concat = {
  371. .name = "concat",
  372. .description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
  373. .init = init,
  374. .uninit = uninit,
  375. .query_formats = query_formats,
  376. .priv_size = sizeof(ConcatContext),
  377. .inputs = NULL,
  378. .outputs = NULL,
  379. .priv_class = &concat_class,
  380. };