FFmpeg
af_asr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Paul B Mahol
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 GNU
14  * 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 #include <pocketsphinx/pocketsphinx.h>
22 
23 #include "libavutil/avstring.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "filters.h"
30 #include "formats.h"
31 
32 typedef struct ASRContext {
33  const AVClass *class;
34 
35  int rate;
36  char *hmm;
37  char *dict;
38  char *lm;
39  char *lmctl;
40  char *lmname;
41  char *logfn;
42 
43  ps_decoder_t *ps;
44  cmd_ln_t *config;
45 
47 } ASRContext;
48 
49 #define OFFSET(x) offsetof(ASRContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption asr_options[] = {
52  { "rate", "set sampling rate", OFFSET(rate), AV_OPT_TYPE_INT, {.i64=16000}, 0, INT_MAX, .flags = FLAGS },
53  { "hmm", "set directory containing acoustic model files", OFFSET(hmm), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
54  { "dict", "set pronunciation dictionary", OFFSET(dict), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
55  { "lm", "set language model file", OFFSET(lm), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
56  { "lmctl", "set language model set", OFFSET(lmctl), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
57  { "lmname","set which language model to use", OFFSET(lmname), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
58  { "logfn", "set output for log messages", OFFSET(logfn), AV_OPT_TYPE_STRING, {.str="/dev/null"}, .flags = FLAGS },
59  { NULL }
60 };
61 
63 
65 {
66  AVFilterContext *ctx = inlink->dst;
67  AVDictionary **metadata = &in->metadata;
68  ASRContext *s = ctx->priv;
69  int have_speech;
70  const char *speech;
71 
72  ps_process_raw(s->ps, (const int16_t *)in->data[0], in->nb_samples, 0, 0);
73  have_speech = ps_get_in_speech(s->ps);
74  if (have_speech && !s->utt_started)
75  s->utt_started = 1;
76  if (!have_speech && s->utt_started) {
77  ps_end_utt(s->ps);
78  speech = ps_get_hyp(s->ps, NULL);
79  if (speech != NULL)
80  av_dict_set(metadata, "lavfi.asr.text", speech, 0);
81  ps_start_utt(s->ps);
82  s->utt_started = 0;
83  }
84 
85  return ff_filter_frame(ctx->outputs[0], in);
86 }
87 
89 {
90  AVFilterContext *ctx = inlink->dst;
91  ASRContext *s = ctx->priv;
92 
93  ps_start_utt(s->ps);
94 
95  return 0;
96 }
97 
99 {
100  ASRContext *s = ctx->priv;
101  const float frate = s->rate;
102  char *rate = av_asprintf("%f", frate);
103  const char *argv[] = { "-logfn", s->logfn,
104  "-hmm", s->hmm,
105  "-lm", s->lm,
106  "-lmctl", s->lmctl,
107  "-lmname", s->lmname,
108  "-dict", s->dict,
109  "-samprate", rate,
110  NULL };
111 
112  s->config = cmd_ln_parse_r(NULL, ps_args(), 14, (char **)argv, 0);
113  av_free(rate);
114  if (!s->config)
115  return AVERROR(ENOMEM);
116 
117  ps_default_search_args(s->config);
118  s->ps = ps_init(s->config);
119  if (!s->ps)
120  return AVERROR(ENOMEM);
121 
122  return 0;
123 }
124 
126  AVFilterFormatsConfig **cfg_in,
127  AVFilterFormatsConfig **cfg_out)
128 {
129  static const enum AVSampleFormat formats[] = {
132  };
133  static const AVChannelLayout layouts[] = {
135  { .nb_channels = 0 },
136  };
137 
138  const ASRContext *s = ctx->priv;
139  int sample_rates[] = { s->rate, -1 };
140  int ret;
141 
142  ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats);
143  if (ret < 0)
144  return ret;
145 
147  if (ret < 0)
148  return ret;
149 
151  if (ret < 0)
152  return ret;
153 
154  return 0;
155 }
156 
158 {
159  ASRContext *s = ctx->priv;
160 
161  ps_free(s->ps);
162  s->ps = NULL;
163  cmd_ln_free_r(s->config);
164  s->config = NULL;
165 }
166 
167 static const AVFilterPad asr_inputs[] = {
168  {
169  .name = "default",
170  .type = AVMEDIA_TYPE_AUDIO,
171  .filter_frame = filter_frame,
172  .config_props = config_input,
173  },
174 };
175 
177  .name = "asr",
178  .description = NULL_IF_CONFIG_SMALL("Automatic Speech Recognition."),
179  .priv_size = sizeof(ASRContext),
180  .priv_class = &asr_class,
181  .init = asr_init,
182  .uninit = asr_uninit,
187 };
formats
formats
Definition: signature.h:47
ASRContext::logfn
char * logfn
Definition: af_asr.c:41
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:335
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:115
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
sample_rates
static const int sample_rates[]
Definition: dcaenc.h:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVOption
AVOption.
Definition: opt.h:429
ff_set_common_channel_layouts_from_list2
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition: formats.c:920
asr_options
static const AVOption asr_options[]
Definition: af_asr.c:51
AVDictionary
Definition: dict.c:34
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
formats.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asr.c:64
FLAGS
#define FLAGS
Definition: af_asr.c:50
ASRContext::hmm
char * hmm
Definition: af_asr.c:36
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
ASRContext
Definition: af_asr.c:32
av_cold
#define av_cold
Definition: attributes.h:90
ASRContext::ps
ps_decoder_t * ps
Definition: af_asr.c:43
s
#define s(width, name)
Definition: cbs_vp9.c:198
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asr.c:88
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
ff_set_common_samplerates_from_list2
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition: formats.c:944
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ASRContext::utt_started
int utt_started
Definition: af_asr.c:46
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
asr_init
static av_cold int asr_init(AVFilterContext *ctx)
Definition: af_asr.c:98
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: af_asr.c:125
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:34
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
ASRContext::dict
char * dict
Definition: af_asr.c:37
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:469
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
asr_uninit
static av_cold void asr_uninit(AVFilterContext *ctx)
Definition: af_asr.c:157
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
ASRContext::config
cmd_ln_t * config
Definition: af_asr.c:44
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
channel_layout.h
ASRContext::lmname
char * lmname
Definition: af_asr.c:40
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:707
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:168
ASRContext::lmctl
char * lmctl
Definition: af_asr.c:39
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asr)
mem.h
audio.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:386
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
ASRContext::rate
int rate
Definition: af_asr.c:35
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
asr_inputs
static const AVFilterPad asr_inputs[]
Definition: af_asr.c:167
avstring.h
OFFSET
#define OFFSET(x)
Definition: af_asr.c:49
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
ff_af_asr
const AVFilter ff_af_asr
Definition: af_asr.c:176
ASRContext::lm
char * lm
Definition: af_asr.c:38