move to new ffmpeg channel layout handling
[nyanmp.git] / npv / audio / filt / local / code.frag.c
blob06f1667a8edffb37f0d3376bfcde6f8f5808e2ab
1 STATIC void fatal(u8 *fmt, ...)
3 va_list ap;
5 npv_perr("audio:filter:");
6 va_start(ap, fmt);
7 npv_vfatal(fmt, ap);
8 va_end(ap);
10 STATIC void warning(u8 *fmt, ...)
12 va_list ap;
14 npv_perr("audio:filter:");
15 va_start(ap, fmt);
16 npv_vwarning(fmt, ap);
17 va_end(ap);
19 STATIC void pout(u8 *fmt, ...)
21 va_list ap;
23 npv_pout("audio:filter:");
24 va_start(ap, fmt);
25 npv_vpout(fmt, ap);
26 va_end(ap);
28 STATIC bool is_recfg_required(avutil_audio_set_ref_t *src_set)
31 if (av_channel_layout_compare(&src_set->ch_layout,
32 &abufsrc_l.key.chans_layout) != 0
33 || src_set->rate != abufsrc_l.key.rate
34 || src_set->fmt != abufsrc_l.key.fmt)
35 return true;
36 return false;
38 STATIC void abufsrc_cfg(avutil_rational_t tb, AVChannelLayout *chans_layout,
39 int rate, enum avutil_audio_fr_fmt_t fmt, bool print_info)
41 int r;
42 u8 chans_layout_str[STR_SZ]; /* should be overkill */
44 abufsrc_l.this = avfilter_get_by_name("abuffer");
45 if (abufsrc_l.this == 0)
46 fatal("audio buffer source:could not find the filter\n");
47 abufsrc_l.ctx = avfilter_graph_alloc_filt(graph_l, abufsrc_l.this,
48 "src_abuf");
49 if (abufsrc_l.ctx == 0)
50 fatal("audio buffer source context:could not allocate the instance in the filter graph\n");
51 r = avutil_opt_set(abufsrc_l.ctx, "sample_fmt",
52 avutil_get_audio_fr_fmt_name(fmt),
53 AVUTIL_OPT_SEARCH_CHILDREN);
54 if (r < 0)
55 fatal("audio buffer source context:unable to set the decoder frame format option\n");
56 r = avutil_opt_set_int(abufsrc_l.ctx, "sample_rate",
57 rate, AVUTIL_OPT_SEARCH_CHILDREN);
58 if (r < 0)
59 fatal("audio buffer source context:unable to set the decoder rate option\n");
60 av_channel_layout_describe(chans_layout, chans_layout_str,
61 sizeof(chans_layout_str));
62 if (print_info)
63 pout("audio buffer source context:using channels layout \"%s\"\n", chans_layout_str);
64 r = avutil_opt_set(abufsrc_l.ctx, "channel_layout", chans_layout_str,
65 AVUTIL_OPT_SEARCH_CHILDREN);
66 if (r < 0)
67 fatal("audio buffer source context:unable to set the decoder channel layout option\n");
68 r = avutil_opt_set_q(abufsrc_l.ctx, "time_base", tb,
69 AVUTIL_OPT_SEARCH_CHILDREN);
70 if (r < 0)
71 fatal("audio buffer source context:unable to set the time base option\n");
72 r = avfilter_init_str(abufsrc_l.ctx, 0);
73 if (r < 0)
74 fatal("audio buffer source context:unable to initialize\n");
76 STATIC void vol_cfg(bool muted, double vol_cfg)
78 double vol;
79 u8 vol_l10n_str[sizeof("xxx.xx")]; /* should be overkill */
80 int r;
82 vol_l = avfilter_get_by_name("volume");
83 if (vol_l == 0)
84 fatal("volume:could not find the filter\n");
85 vol_ctx_l = avfilter_graph_alloc_filt(graph_l, vol_l, "vol");
86 if (vol_ctx_l == 0)
87 fatal("volume context:could not allocate the instance in the filter graph\n");
88 if (muted)
89 vol = 0.0;
90 else
91 vol = vol_cfg;
92 /* yeah the radix is localized, can be '.', ','... */
93 snprintf(vol_l10n_str, sizeof(vol_l10n_str), "%f", vol);
94 r = avutil_opt_set(vol_ctx_l, "volume", vol_l10n_str,
95 AVUTIL_OPT_SEARCH_CHILDREN);
96 if (r < 0)
97 fatal("volume context:unable to set the volume option\n");
98 r = avfilter_init_str(vol_ctx_l, 0);
99 if (r < 0)
100 fatal("volume buffer context:unable to initialize\n");
102 STATIC void afmt_cfg(AVChannelLayout *chans_layout, int rate,
103 enum avutil_audio_fr_fmt_t fmt, bool print_info)
105 int r;
106 u8 rate_str[sizeof("dddddd")];
107 u8 chans_layout_str[STR_SZ]; /* should be overkill */
109 afmt_l = avfilter_get_by_name("aformat");
110 if (afmt_l == 0)
111 fatal("audio format:could not find the filter");
112 afmt_ctx_l = avfilter_graph_alloc_filt(graph_l, afmt_l, "afmt");
113 if (afmt_ctx_l == 0)
114 fatal("audio format:could not allocate the instance in the filter graph\n");
115 r = avutil_opt_set(afmt_ctx_l, "sample_fmts",
116 avutil_get_audio_fr_fmt_name(fmt), AVUTIL_OPT_SEARCH_CHILDREN);
117 if (r < 0)
118 fatal("audio format context:could to set the pcm sample format\n");
119 snprintf(rate_str, sizeof(rate_str), "%d", rate);
120 r = avutil_opt_set(afmt_ctx_l, "sample_rates", rate_str,
121 AVUTIL_OPT_SEARCH_CHILDREN);
122 if (r < 0)
123 fatal("audio format context:could not set the pcm rate\n");
124 av_channel_layout_describe(chans_layout, chans_layout_str,
125 sizeof(chans_layout_str));
126 r = avutil_opt_set(afmt_ctx_l, "channel_layouts", chans_layout_str,
127 AVUTIL_OPT_SEARCH_CHILDREN);
128 if (r < 0)
129 fatal("audio format context:could not set the layout of channels\n");
130 if (print_info)
131 pout("audio format context:channel layout is \"%s\"\n", chans_layout_str);
132 r = avfilter_init_str(afmt_ctx_l, 0);
133 if (r < 0)
134 fatal("audio format context:unable to initialize\n");
136 STATIC void abufsink_cfg(void)
138 int r;
140 abufsink_l = avfilter_get_by_name("abuffersink");
141 if (abufsink_l == 0)
142 fatal("audio buffer sink:could not find the filter\n");
143 filt_p.abufsink_ctx = avfilter_graph_alloc_filt(graph_l, abufsink_l,
144 "sink_abuf");
145 if (filt_p.abufsink_ctx == 0)
146 fatal("audio buffer sink context:could not allocate the instance in the filter graph\n");
147 r = avfilter_init_str(filt_p.abufsink_ctx, 0);
148 if (r < 0)
149 fatal("audio buffer sink context:unable to initialize\n");
151 STATIC void init_once_local(void)
153 graph_l = 0;
154 memset(&abufsrc_l, 0, sizeof(abufsrc_l));
155 vol_ctx_l = 0;
156 vol_l = 0;
157 afmt_ctx_l = 0;
158 afmt_l = 0;
159 abufsink_l = 0;
160 /* floating point strs are localized... erk... */
161 snprintf(double_zero_l10n_str_l, sizeof(double_zero_l10n_str_l),
162 "%f", 0.);
164 STATIC void init_once_public(double initial_vol)
166 filt_p.set = avutil_audio_set_ref_alloc();
167 if (filt_p.set == 0)
168 fatal("ffmpeg:unable to allocate a reference on set of frames for the public part of the interactive latency filter\n");
169 filt_p.pcm_written_ufrs_n = 0;
170 filt_p.vol = initial_vol;
171 filt_p.muted = false;
172 filt_p.abufsink_ctx = 0;