af_astreamsync.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "avfilter.h"
  26. #include "audio.h"
  27. #include "internal.h"
  28. #define QUEUE_SIZE 16
  29. static const char * const var_names[] = {
  30. "b1", "b2",
  31. "s1", "s2",
  32. "t1", "t2",
  33. NULL
  34. };
  35. enum var_name {
  36. VAR_B1, VAR_B2,
  37. VAR_S1, VAR_S2,
  38. VAR_T1, VAR_T2,
  39. VAR_NB
  40. };
  41. typedef struct {
  42. AVExpr *expr;
  43. double var_values[VAR_NB];
  44. struct buf_queue {
  45. AVFilterBufferRef *buf[QUEUE_SIZE];
  46. unsigned tail, nb;
  47. /* buf[tail] is the oldest,
  48. buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
  49. } queue[2];
  50. int req[2];
  51. int next_out;
  52. int eof; /* bitmask, one bit for each stream */
  53. } AStreamSyncContext;
  54. static const char *default_expr = "t1-t2";
  55. static av_cold int init(AVFilterContext *ctx, const char *args0)
  56. {
  57. AStreamSyncContext *as = ctx->priv;
  58. const char *expr = args0 ? args0 : default_expr;
  59. int r, i;
  60. r = av_expr_parse(&as->expr, expr, var_names,
  61. NULL, NULL, NULL, NULL, 0, ctx);
  62. if (r < 0) {
  63. av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", expr);
  64. return r;
  65. }
  66. for (i = 0; i < 42; i++)
  67. av_expr_eval(as->expr, as->var_values, NULL); /* exercize prng */
  68. return 0;
  69. }
  70. static int query_formats(AVFilterContext *ctx)
  71. {
  72. int i;
  73. AVFilterFormats *formats, *rates;
  74. AVFilterChannelLayouts *layouts;
  75. for (i = 0; i < 2; i++) {
  76. formats = ctx->inputs[i]->in_formats;
  77. ff_formats_ref(formats, &ctx->inputs[i]->out_formats);
  78. ff_formats_ref(formats, &ctx->outputs[i]->in_formats);
  79. rates = ff_all_samplerates();
  80. ff_formats_ref(rates, &ctx->inputs[i]->out_samplerates);
  81. ff_formats_ref(rates, &ctx->outputs[i]->in_samplerates);
  82. layouts = ctx->inputs[i]->in_channel_layouts;
  83. ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
  84. ff_channel_layouts_ref(layouts, &ctx->outputs[i]->in_channel_layouts);
  85. }
  86. return 0;
  87. }
  88. static int config_output(AVFilterLink *outlink)
  89. {
  90. AVFilterContext *ctx = outlink->src;
  91. int id = outlink == ctx->outputs[1];
  92. outlink->sample_rate = ctx->inputs[id]->sample_rate;
  93. outlink->time_base = ctx->inputs[id]->time_base;
  94. return 0;
  95. }
  96. static int send_out(AVFilterContext *ctx, int out_id)
  97. {
  98. AStreamSyncContext *as = ctx->priv;
  99. struct buf_queue *queue = &as->queue[out_id];
  100. AVFilterBufferRef *buf = queue->buf[queue->tail];
  101. int ret;
  102. queue->buf[queue->tail] = NULL;
  103. as->var_values[VAR_B1 + out_id]++;
  104. as->var_values[VAR_S1 + out_id] += buf->audio->nb_samples;
  105. if (buf->pts != AV_NOPTS_VALUE)
  106. as->var_values[VAR_T1 + out_id] =
  107. av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
  108. as->var_values[VAR_T1 + out_id] += buf->audio->nb_samples /
  109. (double)ctx->inputs[out_id]->sample_rate;
  110. ret = ff_filter_samples(ctx->outputs[out_id], buf);
  111. queue->nb--;
  112. queue->tail = (queue->tail + 1) % QUEUE_SIZE;
  113. if (as->req[out_id])
  114. as->req[out_id]--;
  115. return ret;
  116. }
  117. static void send_next(AVFilterContext *ctx)
  118. {
  119. AStreamSyncContext *as = ctx->priv;
  120. int i;
  121. while (1) {
  122. if (!as->queue[as->next_out].nb)
  123. break;
  124. send_out(ctx, as->next_out);
  125. if (!as->eof)
  126. as->next_out = av_expr_eval(as->expr, as->var_values, NULL) >= 0;
  127. }
  128. for (i = 0; i < 2; i++)
  129. if (as->queue[i].nb == QUEUE_SIZE)
  130. send_out(ctx, i);
  131. }
  132. static int request_frame(AVFilterLink *outlink)
  133. {
  134. AVFilterContext *ctx = outlink->src;
  135. AStreamSyncContext *as = ctx->priv;
  136. int id = outlink == ctx->outputs[1];
  137. as->req[id]++;
  138. while (as->req[id] && !(as->eof & (1 << id))) {
  139. if (as->queue[as->next_out].nb) {
  140. send_next(ctx);
  141. } else {
  142. as->eof |= 1 << as->next_out;
  143. ff_request_frame(ctx->inputs[as->next_out]);
  144. if (as->eof & (1 << as->next_out))
  145. as->next_out = !as->next_out;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  151. {
  152. AVFilterContext *ctx = inlink->dst;
  153. AStreamSyncContext *as = ctx->priv;
  154. int id = inlink == ctx->inputs[1];
  155. as->queue[id].buf[(as->queue[id].tail + as->queue[id].nb++) % QUEUE_SIZE] =
  156. insamples;
  157. as->eof &= ~(1 << id);
  158. send_next(ctx);
  159. return 0;
  160. }
  161. AVFilter avfilter_af_astreamsync = {
  162. .name = "astreamsync",
  163. .description = NULL_IF_CONFIG_SMALL("Copy two streams of audio data "
  164. "in a configurable order."),
  165. .priv_size = sizeof(AStreamSyncContext),
  166. .init = init,
  167. .query_formats = query_formats,
  168. .inputs = (const AVFilterPad[]) {
  169. { .name = "in1",
  170. .type = AVMEDIA_TYPE_AUDIO,
  171. .filter_samples = filter_samples,
  172. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  173. { .name = "in2",
  174. .type = AVMEDIA_TYPE_AUDIO,
  175. .filter_samples = filter_samples,
  176. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  177. { .name = NULL }
  178. },
  179. .outputs = (const AVFilterPad[]) {
  180. { .name = "out1",
  181. .type = AVMEDIA_TYPE_AUDIO,
  182. .config_props = config_output,
  183. .request_frame = request_frame, },
  184. { .name = "out2",
  185. .type = AVMEDIA_TYPE_AUDIO,
  186. .config_props = config_output,
  187. .request_frame = request_frame, },
  188. { .name = NULL }
  189. },
  190. };