af_astreamsync.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
  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. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Stream (de)synchronization filter
  23. */
  24. #include "libavutil/eval.h"
  25. #include "libavutil/opt.h"
  26. #include "avfilter.h"
  27. #include "audio.h"
  28. #include "internal.h"
  29. #define QUEUE_SIZE 16
  30. static const char * const var_names[] = {
  31. "b1", "b2",
  32. "s1", "s2",
  33. "t1", "t2",
  34. NULL
  35. };
  36. enum var_name {
  37. VAR_B1, VAR_B2,
  38. VAR_S1, VAR_S2,
  39. VAR_T1, VAR_T2,
  40. VAR_NB
  41. };
  42. typedef struct {
  43. const AVClass *class;
  44. AVExpr *expr;
  45. char *expr_str;
  46. double var_values[VAR_NB];
  47. struct buf_queue {
  48. AVFrame *buf[QUEUE_SIZE];
  49. unsigned tail, nb;
  50. /* buf[tail] is the oldest,
  51. buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
  52. } queue[2];
  53. int req[2];
  54. int next_out;
  55. int eof; /* bitmask, one bit for each stream */
  56. } AStreamSyncContext;
  57. #define OFFSET(x) offsetof(AStreamSyncContext, x)
  58. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  59. static const AVOption astreamsync_options[] = {
  60. { "expr", "set stream selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "t1-t2" }, .flags = FLAGS },
  61. { "e", "set stream selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "t1-t2" }, .flags = FLAGS },
  62. { NULL }
  63. };
  64. AVFILTER_DEFINE_CLASS(astreamsync);
  65. static av_cold int init(AVFilterContext *ctx)
  66. {
  67. AStreamSyncContext *as = ctx->priv;
  68. int r, i;
  69. r = av_expr_parse(&as->expr, as->expr_str, var_names,
  70. NULL, NULL, NULL, NULL, 0, ctx);
  71. if (r < 0) {
  72. av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", as->expr_str);
  73. return r;
  74. }
  75. for (i = 0; i < 42; i++)
  76. av_expr_eval(as->expr, as->var_values, NULL); /* exercize prng */
  77. return 0;
  78. }
  79. static int query_formats(AVFilterContext *ctx)
  80. {
  81. int i;
  82. AVFilterFormats *formats, *rates;
  83. AVFilterChannelLayouts *layouts;
  84. for (i = 0; i < 2; i++) {
  85. formats = ctx->inputs[i]->in_formats;
  86. ff_formats_ref(formats, &ctx->inputs[i]->out_formats);
  87. ff_formats_ref(formats, &ctx->outputs[i]->in_formats);
  88. rates = ff_all_samplerates();
  89. ff_formats_ref(rates, &ctx->inputs[i]->out_samplerates);
  90. ff_formats_ref(rates, &ctx->outputs[i]->in_samplerates);
  91. layouts = ctx->inputs[i]->in_channel_layouts;
  92. ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
  93. ff_channel_layouts_ref(layouts, &ctx->outputs[i]->in_channel_layouts);
  94. }
  95. return 0;
  96. }
  97. static int config_output(AVFilterLink *outlink)
  98. {
  99. AVFilterContext *ctx = outlink->src;
  100. int id = outlink == ctx->outputs[1];
  101. outlink->sample_rate = ctx->inputs[id]->sample_rate;
  102. outlink->time_base = ctx->inputs[id]->time_base;
  103. return 0;
  104. }
  105. static int send_out(AVFilterContext *ctx, int out_id)
  106. {
  107. AStreamSyncContext *as = ctx->priv;
  108. struct buf_queue *queue = &as->queue[out_id];
  109. AVFrame *buf = queue->buf[queue->tail];
  110. int ret;
  111. queue->buf[queue->tail] = NULL;
  112. as->var_values[VAR_B1 + out_id]++;
  113. as->var_values[VAR_S1 + out_id] += buf->nb_samples;
  114. if (buf->pts != AV_NOPTS_VALUE)
  115. as->var_values[VAR_T1 + out_id] =
  116. av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
  117. as->var_values[VAR_T1 + out_id] += buf->nb_samples /
  118. (double)ctx->inputs[out_id]->sample_rate;
  119. ret = ff_filter_frame(ctx->outputs[out_id], buf);
  120. queue->nb--;
  121. queue->tail = (queue->tail + 1) % QUEUE_SIZE;
  122. if (as->req[out_id])
  123. as->req[out_id]--;
  124. return ret;
  125. }
  126. static void send_next(AVFilterContext *ctx)
  127. {
  128. AStreamSyncContext *as = ctx->priv;
  129. int i;
  130. while (1) {
  131. if (!as->queue[as->next_out].nb)
  132. break;
  133. send_out(ctx, as->next_out);
  134. if (!as->eof)
  135. as->next_out = av_expr_eval(as->expr, as->var_values, NULL) >= 0;
  136. }
  137. for (i = 0; i < 2; i++)
  138. if (as->queue[i].nb == QUEUE_SIZE)
  139. send_out(ctx, i);
  140. }
  141. static int request_frame(AVFilterLink *outlink)
  142. {
  143. AVFilterContext *ctx = outlink->src;
  144. AStreamSyncContext *as = ctx->priv;
  145. int id = outlink == ctx->outputs[1];
  146. as->req[id]++;
  147. while (as->req[id] && !(as->eof & (1 << id))) {
  148. if (as->queue[as->next_out].nb) {
  149. send_next(ctx);
  150. } else {
  151. as->eof |= 1 << as->next_out;
  152. ff_request_frame(ctx->inputs[as->next_out]);
  153. if (as->eof & (1 << as->next_out))
  154. as->next_out = !as->next_out;
  155. }
  156. }
  157. return 0;
  158. }
  159. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  160. {
  161. AVFilterContext *ctx = inlink->dst;
  162. AStreamSyncContext *as = ctx->priv;
  163. int id = inlink == ctx->inputs[1];
  164. as->queue[id].buf[(as->queue[id].tail + as->queue[id].nb++) % QUEUE_SIZE] =
  165. insamples;
  166. as->eof &= ~(1 << id);
  167. send_next(ctx);
  168. return 0;
  169. }
  170. static av_cold void uninit(AVFilterContext *ctx)
  171. {
  172. AStreamSyncContext *as = ctx->priv;
  173. av_expr_free(as->expr);
  174. as->expr = NULL;
  175. }
  176. static const AVFilterPad astreamsync_inputs[] = {
  177. {
  178. .name = "in1",
  179. .type = AVMEDIA_TYPE_AUDIO,
  180. .filter_frame = filter_frame,
  181. },{
  182. .name = "in2",
  183. .type = AVMEDIA_TYPE_AUDIO,
  184. .filter_frame = filter_frame,
  185. },
  186. { NULL }
  187. };
  188. static const AVFilterPad astreamsync_outputs[] = {
  189. {
  190. .name = "out1",
  191. .type = AVMEDIA_TYPE_AUDIO,
  192. .config_props = config_output,
  193. .request_frame = request_frame,
  194. },{
  195. .name = "out2",
  196. .type = AVMEDIA_TYPE_AUDIO,
  197. .config_props = config_output,
  198. .request_frame = request_frame,
  199. },
  200. { NULL }
  201. };
  202. AVFilter ff_af_astreamsync = {
  203. .name = "astreamsync",
  204. .description = NULL_IF_CONFIG_SMALL("Copy two streams of audio data "
  205. "in a configurable order."),
  206. .priv_size = sizeof(AStreamSyncContext),
  207. .init = init,
  208. .uninit = uninit,
  209. .query_formats = query_formats,
  210. .inputs = astreamsync_inputs,
  211. .outputs = astreamsync_outputs,
  212. .priv_class = &astreamsync_class,
  213. };