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/opt.h"
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 
30 typedef struct ASRContext {
31  const AVClass *class;
32 
33  int rate;
34  char *hmm;
35  char *dict;
36  char *lm;
37  char *lmctl;
38  char *lmname;
39  char *logfn;
40 
41  ps_decoder_t *ps;
42  cmd_ln_t *config;
43 
45 } ASRContext;
46 
47 #define OFFSET(x) offsetof(ASRContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
49 static const AVOption asr_options[] = {
50  { "rate", "set sampling rate", OFFSET(rate), AV_OPT_TYPE_INT, {.i64=16000}, 0, INT_MAX, .flags = FLAGS },
51  { "hmm", "set directory containing acoustic model files", OFFSET(hmm), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
52  { "dict", "set pronunciation dictionary", OFFSET(dict), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
53  { "lm", "set language model file", OFFSET(lm), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
54  { "lmctl", "set language model set", OFFSET(lmctl), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
55  { "lmname","set which language model to use", OFFSET(lmname), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
56  { "logfn", "set output for log messages", OFFSET(logfn), AV_OPT_TYPE_STRING, {.str="/dev/null"}, .flags = FLAGS },
57  { NULL }
58 };
59 
61 
63 {
64  AVFilterContext *ctx = inlink->dst;
65  AVDictionary **metadata = &in->metadata;
66  ASRContext *s = ctx->priv;
67  int have_speech;
68  const char *speech;
69 
70  ps_process_raw(s->ps, (const int16_t *)in->data[0], in->nb_samples, 0, 0);
71  have_speech = ps_get_in_speech(s->ps);
72  if (have_speech && !s->utt_started)
73  s->utt_started = 1;
74  if (!have_speech && s->utt_started) {
75  ps_end_utt(s->ps);
76  speech = ps_get_hyp(s->ps, NULL);
77  if (speech != NULL)
78  av_dict_set(metadata, "lavfi.asr.text", speech, 0);
79  ps_start_utt(s->ps);
80  s->utt_started = 0;
81  }
82 
83  return ff_filter_frame(ctx->outputs[0], in);
84 }
85 
87 {
88  AVFilterContext *ctx = inlink->dst;
89  ASRContext *s = ctx->priv;
90 
91  ps_start_utt(s->ps);
92 
93  return 0;
94 }
95 
97 {
98  ASRContext *s = ctx->priv;
99  const float frate = s->rate;
100  char *rate = av_asprintf("%f", frate);
101  const char *argv[] = { "-logfn", s->logfn,
102  "-hmm", s->hmm,
103  "-lm", s->lm,
104  "-lmctl", s->lmctl,
105  "-lmname", s->lmname,
106  "-dict", s->dict,
107  "-samprate", rate,
108  NULL };
109 
110  s->config = cmd_ln_parse_r(NULL, ps_args(), 14, (char **)argv, 0);
111  av_free(rate);
112  if (!s->config)
113  return AVERROR(ENOMEM);
114 
115  ps_default_search_args(s->config);
116  s->ps = ps_init(s->config);
117  if (!s->ps)
118  return AVERROR(ENOMEM);
119 
120  return 0;
121 }
122 
124 {
125  ASRContext *s = ctx->priv;
126  int sample_rates[] = { s->rate, -1 };
127  int ret;
128 
131 
132  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 ||
133  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
137  return ret;
138 
139  return 0;
140 }
141 
143 {
144  ASRContext *s = ctx->priv;
145 
146  ps_free(s->ps);
147  s->ps = NULL;
148  cmd_ln_free_r(s->config);
149  s->config = NULL;
150 }
151 
152 static const AVFilterPad asr_inputs[] = {
153  {
154  .name = "default",
155  .type = AVMEDIA_TYPE_AUDIO,
156  .filter_frame = filter_frame,
157  .config_props = config_input,
158  },
159 };
160 
161 static const AVFilterPad asr_outputs[] = {
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_AUDIO,
165  },
166 };
167 
169  .name = "asr",
170  .description = NULL_IF_CONFIG_SMALL("Automatic Speech Recognition."),
171  .priv_size = sizeof(ASRContext),
172  .priv_class = &asr_class,
173  .init = asr_init,
174  .uninit = asr_uninit,
179 };
formats
formats
Definition: signature.h:48
ASRContext::logfn
char * logfn
Definition: af_asr.c:39
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_asr.c:123
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
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:1018
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
ff_set_common_samplerates_from_list
int ff_set_common_samplerates_from_list(AVFilterContext *ctx, const int *samplerates)
Equivalent to ff_set_common_samplerates(ctx, ff_make_format_list(samplerates))
Definition: formats.c:683
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:113
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
asr_options
static const AVOption asr_options[]
Definition: af_asr.c:49
AVDictionary
Definition: dict.c:30
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
init
static int init
Definition: av_tx.c:47
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asr.c:62
FLAGS
#define FLAGS
Definition: af_asr.c:48
ASRContext::hmm
char * hmm
Definition: af_asr.c:34
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
ASRContext
Definition: af_asr.c:30
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:699
ASRContext::ps
ps_decoder_t * ps
Definition: af_asr.c:41
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:426
s
#define s(width, name)
Definition: cbs_vp9.c:257
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asr.c:86
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
ASRContext::utt_started
int utt_started
Definition: af_asr.c:44
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
asr_init
static av_cold int asr_init(AVFilterContext *ctx)
Definition: af_asr.c:96
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:420
ASRContext::dict
char * dict
Definition: af_asr.c:35
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:117
sample_rates
sample_rates
Definition: ffmpeg_filter.c:153
internal.h
layout
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 layout
Definition: filter_design.txt:18
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:397
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
asr_uninit
static av_cold void asr_uninit(AVFilterContext *ctx)
Definition: af_asr.c:142
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
ASRContext::config
cmd_ln_t * config
Definition: af_asr.c:42
AVFilter
Filter definition.
Definition: avfilter.h:165
asr_outputs
static const AVFilterPad asr_outputs[]
Definition: af_asr.c:161
ret
ret
Definition: filter_design.txt:187
channel_layout.h
ASRContext::lmname
char * lmname
Definition: af_asr.c:38
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:608
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:137
ASRContext::lmctl
char * lmctl
Definition: af_asr.c:37
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asr)
audio.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
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:70
ASRContext::rate
int rate
Definition: af_asr.c:33
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
asr_inputs
static const AVFilterPad asr_inputs[]
Definition: af_asr.c:152
uninit
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:282
avstring.h
OFFSET
#define OFFSET(x)
Definition: af_asr.c:47
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
ff_af_asr
const AVFilter ff_af_asr
Definition: af_asr.c:168
ASRContext::lm
char * lm
Definition: af_asr.c:36
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
Helpers for query_formats() which set all free audio links to the same list of channel layouts/sample...
Definition: formats.c:658