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.

211 lines
6.4KB

  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 "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, void *opaque)
  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. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  78. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  79. rates = ff_all_samplerates();
  80. avfilter_formats_ref(rates, &ctx->inputs[i]->out_samplerates);
  81. avfilter_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 void 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. queue->buf[queue->tail] = NULL;
  102. as->var_values[VAR_B1 + out_id]++;
  103. as->var_values[VAR_S1 + out_id] += buf->audio->nb_samples;
  104. if (buf->pts != AV_NOPTS_VALUE)
  105. as->var_values[VAR_T1 + out_id] =
  106. av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
  107. as->var_values[VAR_T1 + out_id] += buf->audio->nb_samples /
  108. (double)ctx->inputs[out_id]->sample_rate;
  109. ff_filter_samples(ctx->outputs[out_id], buf);
  110. queue->nb--;
  111. queue->tail = (queue->tail + 1) % QUEUE_SIZE;
  112. if (as->req[out_id])
  113. as->req[out_id]--;
  114. }
  115. static void send_next(AVFilterContext *ctx)
  116. {
  117. AStreamSyncContext *as = ctx->priv;
  118. int i;
  119. while (1) {
  120. if (!as->queue[as->next_out].nb)
  121. break;
  122. send_out(ctx, as->next_out);
  123. if (!as->eof)
  124. as->next_out = av_expr_eval(as->expr, as->var_values, NULL) >= 0;
  125. }
  126. for (i = 0; i < 2; i++)
  127. if (as->queue[i].nb == QUEUE_SIZE)
  128. send_out(ctx, i);
  129. }
  130. static int request_frame(AVFilterLink *outlink)
  131. {
  132. AVFilterContext *ctx = outlink->src;
  133. AStreamSyncContext *as = ctx->priv;
  134. int id = outlink == ctx->outputs[1];
  135. as->req[id]++;
  136. while (as->req[id] && !(as->eof & (1 << id))) {
  137. if (as->queue[as->next_out].nb) {
  138. send_next(ctx);
  139. } else {
  140. as->eof |= 1 << as->next_out;
  141. avfilter_request_frame(ctx->inputs[as->next_out]);
  142. if (as->eof & (1 << as->next_out))
  143. as->next_out = !as->next_out;
  144. }
  145. }
  146. return 0;
  147. }
  148. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  149. {
  150. AVFilterContext *ctx = inlink->dst;
  151. AStreamSyncContext *as = ctx->priv;
  152. int id = inlink == ctx->inputs[1];
  153. as->queue[id].buf[(as->queue[id].tail + as->queue[id].nb++) % QUEUE_SIZE] =
  154. insamples;
  155. as->eof &= ~(1 << id);
  156. send_next(ctx);
  157. }
  158. AVFilter avfilter_af_astreamsync = {
  159. .name = "astreamsync",
  160. .description = NULL_IF_CONFIG_SMALL("Copy two streams of audio data "
  161. "in a configurable order."),
  162. .priv_size = sizeof(AStreamSyncContext),
  163. .init = init,
  164. .query_formats = query_formats,
  165. .inputs = (const AVFilterPad[]) {
  166. { .name = "in1",
  167. .type = AVMEDIA_TYPE_AUDIO,
  168. .filter_samples = filter_samples,
  169. .min_perms = AV_PERM_READ, },
  170. { .name = "in2",
  171. .type = AVMEDIA_TYPE_AUDIO,
  172. .filter_samples = filter_samples,
  173. .min_perms = AV_PERM_READ, },
  174. { .name = NULL }
  175. },
  176. .outputs = (const AVFilterPad[]) {
  177. { .name = "out1",
  178. .type = AVMEDIA_TYPE_AUDIO,
  179. .config_props = config_output,
  180. .request_frame = request_frame, },
  181. { .name = "out2",
  182. .type = AVMEDIA_TYPE_AUDIO,
  183. .config_props = config_output,
  184. .request_frame = request_frame, },
  185. { .name = NULL }
  186. },
  187. };