2 * Copyright (c) 2019 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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 GNU
14 * Lesser General Public License for more details.
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
21 #include <pocketsphinx/pocketsphinx.h>
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/opt.h"
31 typedef struct ASRContext {
48 #define OFFSET(x) offsetof(ASRContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
50 static const AVOption asr_options[] = {
51 { "rate", "set sampling rate", OFFSET(rate), AV_OPT_TYPE_INT, {.i64=16000}, 0, INT_MAX, .flags = FLAGS },
52 { "hmm", "set directory containing acoustic model files", OFFSET(hmm), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
53 { "dict", "set pronunciation dictionary", OFFSET(dict), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
54 { "lm", "set language model file", OFFSET(lm), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
55 { "lmctl", "set language model set", OFFSET(lmctl), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
56 { "lmname","set which language model to use", OFFSET(lmname), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
57 { "logfn", "set output for log messages", OFFSET(logfn), AV_OPT_TYPE_STRING, {.str="/dev/null"}, .flags = FLAGS },
61 AVFILTER_DEFINE_CLASS(asr);
63 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
65 AVFilterContext *ctx = inlink->dst;
66 AVDictionary **metadata = &in->metadata;
67 ASRContext *s = ctx->priv;
71 ps_process_raw(s->ps, (const int16_t *)in->data[0], in->nb_samples, 0, 0);
72 have_speech = ps_get_in_speech(s->ps);
73 if (have_speech && !s->utt_started)
75 if (!have_speech && s->utt_started) {
77 speech = ps_get_hyp(s->ps, NULL);
79 av_dict_set(metadata, "lavfi.asr.text", speech, 0);
84 return ff_filter_frame(ctx->outputs[0], in);
87 static int config_input(AVFilterLink *inlink)
89 AVFilterContext *ctx = inlink->dst;
90 ASRContext *s = ctx->priv;
97 static av_cold int asr_init(AVFilterContext *ctx)
99 ASRContext *s = ctx->priv;
100 const float frate = s->rate;
101 char *rate = av_asprintf("%f", frate);
102 const char *argv[] = { "-logfn", s->logfn,
106 "-lmname", s->lmname,
111 s->config = cmd_ln_parse_r(NULL, ps_args(), 14, (char **)argv, 0);
114 return AVERROR(ENOMEM);
116 ps_default_search_args(s->config);
117 s->ps = ps_init(s->config);
119 return AVERROR(ENOMEM);
124 static int query_formats(AVFilterContext *ctx)
126 ASRContext *s = ctx->priv;
127 int sample_rates[] = { s->rate, -1 };
130 AVFilterFormats *formats = NULL;
131 AVFilterChannelLayouts *layout = NULL;
133 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 ||
134 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
135 (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_MONO )) < 0 ||
136 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
137 (ret = ff_set_common_samplerates (ctx , ff_make_format_list(sample_rates) )) < 0)
143 static av_cold void asr_uninit(AVFilterContext *ctx)
145 ASRContext *s = ctx->priv;
149 cmd_ln_free_r(s->config);
153 static const AVFilterPad asr_inputs[] = {
156 .type = AVMEDIA_TYPE_AUDIO,
157 .filter_frame = filter_frame,
158 .config_props = config_input,
163 static const AVFilterPad asr_outputs[] = {
166 .type = AVMEDIA_TYPE_AUDIO,
171 const AVFilter ff_af_asr = {
173 .description = NULL_IF_CONFIG_SMALL("Automatic Speech Recognition."),
174 .priv_size = sizeof(ASRContext),
175 .priv_class = &asr_class,
177 .uninit = asr_uninit,
178 .query_formats = query_formats,
179 .inputs = asr_inputs,
180 .outputs = asr_outputs,