af_astreamsync.c 6.3 KB

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