FFmpeg
asrc_flite.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
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 /**
22  * @file
23  * flite voice synth source
24  */
25 
26 #include <flite/flite.h>
28 #include "libavutil/file.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/thread.h"
31 #include "avfilter.h"
32 #include "audio.h"
33 #include "formats.h"
34 #include "internal.h"
35 
36 typedef struct FliteContext {
37  const AVClass *class;
38  char *voice_str;
39  char *textfile;
40  char *text;
41  cst_wave *wave;
42  int16_t *wave_samples;
45  cst_voice *voice;
47  int64_t pts;
48  int frame_nb_samples; ///< number of samples per frame
49 } FliteContext;
50 
51 #define OFFSET(x) offsetof(FliteContext, x)
52 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53 
54 static const AVOption flite_options[] = {
55  { "list_voices", "list voices and exit", OFFSET(list_voices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
56  { "nb_samples", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
57  { "n", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
58  { "text", "set text to speak", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
59  { "textfile", "set filename of the text to speak", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
60  { "v", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
61  { "voice", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
62  { NULL }
63 };
64 
66 
68 
69 static int flite_inited = 0;
70 
71 /* declare functions for all the supported voices */
72 #define DECLARE_REGISTER_VOICE_FN(name) \
73  cst_voice *register_cmu_us_## name(const char *); \
74  void unregister_cmu_us_## name(cst_voice *)
80 
81 struct voice_entry {
82  const char *name;
83  cst_voice * (*register_fn)(const char *);
84  void (*unregister_fn)(cst_voice *);
85  cst_voice *voice;
86  unsigned usage_count;
87 };
88 
89 #define MAKE_VOICE_STRUCTURE(voice_name) { \
90  .name = #voice_name, \
91  .register_fn = register_cmu_us_ ## voice_name, \
92  .unregister_fn = unregister_cmu_us_ ## voice_name, \
93 }
94 static struct voice_entry voice_entries[] = {
97  MAKE_VOICE_STRUCTURE(kal16),
100 };
101 
102 static void list_voices(void *log_ctx, const char *sep)
103 {
104  int i, n = FF_ARRAY_ELEMS(voice_entries);
105  for (i = 0; i < n; i++)
106  av_log(log_ctx, AV_LOG_INFO, "%s%s",
107  voice_entries[i].name, i < (n-1) ? sep : "\n");
108 }
109 
110 static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
111 {
112  int i;
113 
114  for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) {
115  struct voice_entry *entry = &voice_entries[i];
116  if (!strcmp(entry->name, voice_name)) {
117  cst_voice *voice;
119  if (!entry->voice)
120  entry->voice = entry->register_fn(NULL);
121  voice = entry->voice;
122  if (voice)
123  entry->usage_count++;
125  if (!voice) {
126  av_log(log_ctx, AV_LOG_ERROR,
127  "Could not register voice '%s'\n", voice_name);
128  return AVERROR_UNKNOWN;
129  }
130  *entry_ret = entry;
131  return 0;
132  }
133  }
134 
135  av_log(log_ctx, AV_LOG_ERROR, "Could not find voice '%s'\n", voice_name);
136  av_log(log_ctx, AV_LOG_INFO, "Choose between the voices: ");
137  list_voices(log_ctx, ", ");
138 
139  return AVERROR(EINVAL);
140 }
141 
143 {
144  FliteContext *flite = ctx->priv;
145  int ret = 0;
146 
147  if (flite->list_voices) {
148  list_voices(ctx, "\n");
149  return AVERROR_EXIT;
150  }
151 
153  if (!flite_inited) {
154  if ((ret = flite_init()) >= 0)
155  flite_inited = 1;
156  }
158  if (ret < 0) {
159  av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n");
160  return AVERROR_EXTERNAL;
161  }
162 
163  if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0)
164  return ret;
165  flite->voice = flite->voice_entry->voice;
166 
167  if (flite->textfile && flite->text) {
169  "Both text and textfile options set: only one must be specified\n");
170  return AVERROR(EINVAL);
171  }
172 
173  if (flite->textfile) {
174  uint8_t *textbuf;
175  size_t textbuf_size;
176 
177  if ((ret = av_file_map(flite->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
179  "The text file '%s' could not be read: %s\n",
180  flite->textfile, av_err2str(ret));
181  return ret;
182  }
183 
184  if (!(flite->text = av_malloc(textbuf_size+1))) {
185  av_file_unmap(textbuf, textbuf_size);
186  return AVERROR(ENOMEM);
187  }
188  memcpy(flite->text, textbuf, textbuf_size);
189  flite->text[textbuf_size] = 0;
190  av_file_unmap(textbuf, textbuf_size);
191  }
192 
193  if (!flite->text) {
195  "No speech text specified, specify the 'text' or 'textfile' option\n");
196  return AVERROR(EINVAL);
197  }
198 
199  /* synth all the file data in block */
200  flite->wave = flite_text_to_wave(flite->text, flite->voice);
201  flite->wave_samples = flite->wave->samples;
202  flite->wave_nb_samples = flite->wave->num_samples;
203  return 0;
204 }
205 
207 {
208  FliteContext *flite = ctx->priv;
209 
210  if (flite->voice_entry) {
212  if (!--flite->voice_entry->usage_count) {
213  flite->voice_entry->unregister_fn(flite->voice);
214  flite->voice_entry->voice = NULL;
215  }
217  }
218  delete_wave(flite->wave);
219  flite->wave = NULL;
220 }
221 
223 {
224  FliteContext *flite = ctx->priv;
225  int ret;
226 
227  AVFilterChannelLayouts *chlayouts = NULL;
228  AVFilterFormats *sample_formats = NULL;
230  AVChannelLayout chlayout = { 0 };
231 
232  av_channel_layout_default(&chlayout, flite->wave->num_channels);
233 
234  if ((ret = ff_add_channel_layout (&chlayouts , &chlayout )) < 0 ||
235  (ret = ff_set_common_channel_layouts (ctx , chlayouts )) < 0 ||
236  (ret = ff_add_format (&sample_formats, AV_SAMPLE_FMT_S16 )) < 0 ||
237  (ret = ff_set_common_formats (ctx , sample_formats )) < 0 ||
238  (ret = ff_add_format (&sample_rates , flite->wave->sample_rate)) < 0 ||
240  return ret;
241 
242  return 0;
243 }
244 
245 static int config_props(AVFilterLink *outlink)
246 {
247  AVFilterContext *ctx = outlink->src;
248  FliteContext *flite = ctx->priv;
249 
250  outlink->sample_rate = flite->wave->sample_rate;
251  outlink->time_base = (AVRational){1, flite->wave->sample_rate};
252 
253  av_log(ctx, AV_LOG_VERBOSE, "voice:%s fmt:%s sample_rate:%d\n",
254  flite->voice_str,
255  av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
256  return 0;
257 }
258 
259 static int request_frame(AVFilterLink *outlink)
260 {
261  AVFrame *samplesref;
262  FliteContext *flite = outlink->src->priv;
263  int nb_samples = FFMIN(flite->wave_nb_samples, flite->frame_nb_samples);
264 
265  if (!nb_samples)
266  return AVERROR_EOF;
267 
268  samplesref = ff_get_audio_buffer(outlink, nb_samples);
269  if (!samplesref)
270  return AVERROR(ENOMEM);
271 
272  memcpy(samplesref->data[0], flite->wave_samples,
273  nb_samples * flite->wave->num_channels * 2);
274  samplesref->pts = flite->pts;
275  samplesref->pkt_pos = -1;
276  samplesref->sample_rate = flite->wave->sample_rate;
277  flite->pts += nb_samples;
278  flite->wave_samples += nb_samples * flite->wave->num_channels;
279  flite->wave_nb_samples -= nb_samples;
280 
281  return ff_filter_frame(outlink, samplesref);
282 }
283 
284 static const AVFilterPad flite_outputs[] = {
285  {
286  .name = "default",
287  .type = AVMEDIA_TYPE_AUDIO,
288  .config_props = config_props,
289  .request_frame = request_frame,
290  },
291 };
292 
294  .name = "flite",
295  .description = NULL_IF_CONFIG_SMALL("Synthesize voice from text using libflite."),
296  .init = init,
297  .uninit = uninit,
298  .priv_size = sizeof(FliteContext),
299  .inputs = NULL,
302  .priv_class = &flite_class,
303 };
OFFSET
#define OFFSET(x)
Definition: asrc_flite.c:51
ff_asrc_flite
const AVFilter ff_asrc_flite
Definition: asrc_flite.c:293
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:100
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
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
flite_options
static const AVOption flite_options[]
Definition: asrc_flite.c:54
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:999
thread.h
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FliteContext::textfile
char * textfile
Definition: asrc_flite.c:39
voice_entry::voice
cst_voice * voice
Definition: asrc_flite.c:85
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:432
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
voice_entry::usage_count
unsigned usage_count
Definition: asrc_flite.c:86
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
FliteContext::voice_entry
struct voice_entry * voice_entry
Definition: asrc_flite.c:46
FliteContext
Definition: asrc_flite.c:36
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(flite)
voice_entry::register_fn
cst_voice *(* register_fn)(const char *)
Definition: asrc_flite.c:83
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
MAKE_VOICE_STRUCTURE
#define MAKE_VOICE_STRUCTURE(voice_name)
Definition: asrc_flite.c:89
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
flite_mutex
static AVMutex flite_mutex
Definition: asrc_flite.c:67
av_file_map
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition: file.c:53
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:423
FliteContext::wave_samples
int16_t * wave_samples
Definition: asrc_flite.c:42
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
FliteContext::list_voices
int list_voices
Definition: asrc_flite.c:44
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
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:749
AVMutex
#define AVMutex
Definition: thread.h:168
AVFrame::pkt_pos
int64_t pkt_pos
reordered pos from the last AVPacket that has been input into the decoder
Definition: frame.h:605
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
list_voices
static void list_voices(void *log_ctx, const char *sep)
Definition: asrc_flite.c:102
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_get_sample_fmt_name
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:51
av_file_unmap
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:144
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
flite_outputs
static const AVFilterPad flite_outputs[]
Definition: asrc_flite.c:284
voice_entries
static struct voice_entry voice_entries[]
Definition: asrc_flite.c:94
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: asrc_flite.c:222
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
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:449
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:960
inputs
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
Definition: filter_design.txt:243
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:466
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:77
voice_entry::unregister_fn
void(* unregister_fn)(cst_voice *)
Definition: asrc_flite.c:84
FliteContext::voice_str
char * voice_str
Definition: asrc_flite.c:38
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:502
AV_MUTEX_INITIALIZER
#define AV_MUTEX_INITIALIZER
Definition: thread.h:169
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
config_props
static int config_props(AVFilterLink *outlink)
Definition: asrc_flite.c:245
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
sample_rates
sample_rates
Definition: ffmpeg_filter.c:153
internal.h
FliteContext::pts
int64_t pts
Definition: asrc_flite.c:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
FliteContext::text
char * text
Definition: asrc_flite.c:40
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:171
ret
ret
Definition: filter_design.txt:187
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_flite.c:206
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_flite.c:142
file.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
flite_inited
static int flite_inited
Definition: asrc_flite.c:69
FLAGS
#define FLAGS
Definition: asrc_flite.c:52
DECLARE_REGISTER_VOICE_FN
#define DECLARE_REGISTER_VOICE_FN(name)
Definition: asrc_flite.c:72
audio.h
FliteContext::wave_nb_samples
int wave_nb_samples
Definition: asrc_flite.c:43
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
select_voice
static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
Definition: asrc_flite.c:110
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:726
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
FliteContext::frame_nb_samples
int frame_nb_samples
number of samples per frame
Definition: asrc_flite.c:48
FliteContext::voice
cst_voice * voice
Definition: asrc_flite.c:45
voice_entry
Definition: asrc_flite.c:81
voice_entry::name
const char * name
Definition: asrc_flite.c:82
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: asrc_flite.c:259
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:73
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:708
FliteContext::wave
cst_wave * wave
Definition: asrc_flite.c:41