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/avassert.h"
24 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 
31 typedef struct ASRContext {
32  const AVClass *class;
33 
34  int rate;
35  char *hmm;
36  char *dict;
37  char *lm;
38  char *lmctl;
39  char *lmname;
40  char *logfn;
41 
42  ps_decoder_t *ps;
43  cmd_ln_t *config;
44 
46 } ASRContext;
47 
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 },
58  { NULL }
59 };
60 
62 
64 {
65  AVFilterContext *ctx = inlink->dst;
66  AVDictionary **metadata = &in->metadata;
67  ASRContext *s = ctx->priv;
68  int have_speech;
69  const char *speech;
70 
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)
74  s->utt_started = 1;
75  if (!have_speech && s->utt_started) {
76  ps_end_utt(s->ps);
77  speech = ps_get_hyp(s->ps, NULL);
78  if (speech != NULL)
79  av_dict_set(metadata, "lavfi.asr.text", speech, 0);
80  ps_start_utt(s->ps);
81  s->utt_started = 0;
82  }
83 
84  return ff_filter_frame(ctx->outputs[0], in);
85 }
86 
88 {
89  AVFilterContext *ctx = inlink->dst;
90  ASRContext *s = ctx->priv;
91 
92  ps_start_utt(s->ps);
93 
94  return 0;
95 }
96 
98 {
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,
103  "-hmm", s->hmm,
104  "-lm", s->lm,
105  "-lmctl", s->lmctl,
106  "-lmname", s->lmname,
107  "-dict", s->dict,
108  "-samprate", rate,
109  NULL };
110 
111  s->config = cmd_ln_parse_r(NULL, ps_args(), 14, (char **)argv, 0);
112  av_free(rate);
113  if (!s->config)
114  return AVERROR(ENOMEM);
115 
116  ps_default_search_args(s->config);
117  s->ps = ps_init(s->config);
118  if (!s->ps)
119  return AVERROR(ENOMEM);
120 
121  return 0;
122 }
123 
125 {
126  ASRContext *s = ctx->priv;
127  int sample_rates[] = { s->rate, -1 };
128  int ret;
129 
132 
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)
138  return ret;
139 
140  return 0;
141 }
142 
144 {
145  ASRContext *s = ctx->priv;
146 
147  ps_free(s->ps);
148  s->ps = NULL;
149  cmd_ln_free_r(s->config);
150  s->config = NULL;
151 }
152 
153 static const AVFilterPad asr_inputs[] = {
154  {
155  .name = "default",
156  .type = AVMEDIA_TYPE_AUDIO,
157  .filter_frame = filter_frame,
158  .config_props = config_input,
159  },
160  { NULL }
161 };
162 
163 static const AVFilterPad asr_outputs[] = {
164  {
165  .name = "default",
166  .type = AVMEDIA_TYPE_AUDIO,
167  },
168  { NULL }
169 };
170 
172  .name = "asr",
173  .description = NULL_IF_CONFIG_SMALL("Automatic Speech Recognition."),
174  .priv_size = sizeof(ASRContext),
175  .priv_class = &asr_class,
176  .init = asr_init,
177  .uninit = asr_uninit,
179  .inputs = asr_inputs,
180  .outputs = asr_outputs,
181 };
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
ps_decoder_t * ps
Definition: af_asr.c:42
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const AVOption asr_options[]
Definition: af_asr.c:50
#define FLAGS
Definition: af_asr.c:49
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
AVOptions.
char * hmm
Definition: af_asr.c:35
static int config_input(AVFilterLink *inlink)
Definition: af_asr.c:87
AVDictionary * metadata
metadata.
Definition: frame.h:581
char * lmname
Definition: af_asr.c:39
int utt_started
Definition: af_asr.c:45
A filter pad used for either input or output.
Definition: internal.h:54
char * lmctl
Definition: af_asr.c:38
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:568
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asr.c:63
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
char * dict
Definition: af_asr.c:36
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
audio channel layout utility functions
char * logfn
Definition: af_asr.c:40
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
AVFilter ff_af_asr
Definition: af_asr.c:171
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
static av_cold int asr_init(AVFilterContext *ctx)
Definition: af_asr.c:97
char * lm
Definition: af_asr.c:37
cmd_ln_t * config
Definition: af_asr.c:43
static av_cold void asr_uninit(AVFilterContext *ctx)
Definition: af_asr.c:143
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
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
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
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
#define OFFSET(x)
Definition: af_asr.c:48
static const AVFilterPad asr_outputs[]
Definition: af_asr.c:163
AVFILTER_DEFINE_CLASS(asr)
sample_rates
signed 16 bits
Definition: samplefmt.h:61
#define av_free(p)
static const AVFilterPad asr_inputs[]
Definition: af_asr.c:153
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
formats
Definition: signature.h:48
internal API functions
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
int rate
Definition: af_asr.c:34
#define AV_CH_LAYOUT_MONO
static int query_formats(AVFilterContext *ctx)
Definition: af_asr.c:124
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
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
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556