npv: tidy mutex locking
[nyanmp.git] / npv / audio / filt / public / code.frag.c
blob57ff928fffb79efdccfeeac43becfc897c33983b
1 #define AGAIN 0
2 #define PUSHED_ONE_SET 1
3 #define NO_DEC_SET 2
4 #define FILT_SWITCHED_TO_DRAINING 3
5 STATIC u8 filt_push_dec_sets(void)
7 u8 r8;
8 int ri;
9 avutil_audio_set_ref_t **a;
11 npv_audio_dec_sets_lock();
12 if (npv_audio_dec_sets_p.n == 0) {
13 if (npv_audio_dec_sets_p.eof_receive) {
14 ri = avfilter_bufsrc_add_audio_set_flags(abufsrc_ctx_l,
15 0, AVFILTER_BUFSRC_FLAG_PUSH
16 | AVFILTER_BUFSRC_FLAG_KEEP_REF);
17 if (ri < 0)
18 fatal("ffmpeg:unable to notify the end of data to the filter source audio buffer context\n");
19 pout("ffmpeg:interactive filter switched to draining\n");
20 r8 = FILT_SWITCHED_TO_DRAINING;
21 goto unlock;
23 r8 = NO_DEC_SET;
24 goto unlock;
26 a = npv_audio_dec_sets_p.a;
27 /* the dec_sets_p bufs will be unref in avcodec_audio_receive_set */
28 ri = avfilter_bufsrc_add_audio_set_flags(abufsrc_ctx_l, a[0],
29 AVFILTER_BUFSRC_FLAG_PUSH | AVFILTER_BUFSRC_FLAG_KEEP_REF);
30 if (ri >= 0) {
31 /* rotate the ptrs if needed */
32 if (npv_audio_dec_sets_p.n > 1) {
33 avutil_audio_set_ref_t *save;
35 save = a[0];
36 memmove(&a[0], &a[1], sizeof(*a)
37 * (npv_audio_dec_sets_p.n - 1));
38 a[npv_audio_dec_sets_p.n - 1] = save;
40 npv_audio_dec_sets_p.n--;
41 r8 = PUSHED_ONE_SET;
42 goto unlock;
43 } else if (ri == AVERROR(EAGAIN)) {
44 r8 = AGAIN;
45 goto unlock;
47 fatal("ffmpeg:unable to submit a decoder set of frames to the filter source audio buffer context\n");
48 unlock:
49 npv_audio_dec_sets_unlock();
50 return r8;
52 #undef AGAIN
53 #undef PUSHED_ONE_SET
54 #undef NO_DEC_SET
55 #undef PUSHED_NULL_SET
56 #undef FILT_SWITCHED_TO_DRAINING
57 #define AGAIN 0
58 #define HAVE_FILT_SET 1
59 #define EOF_FILT 2
60 STATIC u8 filt_set_try_get(void)
62 int r;
64 * the last dec set should switch the filt in draining mode, and
65 * filt_p.set won't matter.
67 r = avfilter_bufsink_get_audio_set(abufsink_ctx_l, filt_p.set);
68 if (r == AVERROR(EAGAIN)) {
69 return AGAIN;
70 } else if (r >= 0) {
71 filt_p.pcm_written_ufrs_n = 0;
72 return HAVE_FILT_SET;
73 } else if (r == AVUTIL_AVERROR_EOF) {
74 return EOF_FILT;
76 fatal("ffmpeg:error while getting frames from the filter\n");
78 #undef AGAIN
79 #undef HAVE_FILT_SET
80 #undef EOF_FILT
81 #define DONT_PRINT_INFO false
82 STATIC void filt_flush(void)
84 enum avutil_audio_fr_fmt_t dst_fmt;
85 int dst_rate;
86 int dst_chans_n;
87 uint64_t dst_chans_layout;
89 avutil_audio_set_unref(filt_p.set);
90 filt_p.pcm_written_ufrs_n = 0;
92 npv_audio_pcm2ff(npv_audio_pcm_p, &dst_fmt, &dst_rate, &dst_chans_n,
93 &dst_chans_layout, DONT_PRINT_INFO);
94 npv_audio_filt_cfg(
95 npv_audio_dec_ctx_p->fr_fmt, npv_audio_dec_ctx_p->fr_rate,
96 npv_audio_dec_ctx_p->chans_n, npv_audio_dec_ctx_p->chans_layout,
97 filt_p.muted, filt_p.vol,
98 dst_fmt, dst_rate, dst_chans_n, dst_chans_layout,
99 DONT_PRINT_INFO);
101 #undef DONT_PRINT_INFO
102 STATIC void init_once(double initial_vol)
104 init_once_local();
105 init_once_public(initial_vol);
107 STATIC void cfg(enum avutil_audio_fr_fmt_t src_fmt, int src_rate,
108 int src_chans_n, uint64_t src_chans_layout,
109 bool muted, double vol,
110 enum avutil_audio_fr_fmt_t dst_fmt, int dst_rate,
111 int dst_chans_n, uint64_t dst_chans_layout,
112 bool print_info)
114 int r;
115 char *dump_str;
117 avfilter_graph_free(&graph_l);
119 graph_l = avfilter_graph_alloc();
120 if (graph_l == 0)
121 fatal("unable to create filter graph\n");
122 abufsrc_cfg(src_fmt, src_rate, src_chans_n, src_chans_layout,
123 print_info);
124 vol_cfg(muted, vol);
125 afmt_cfg(dst_fmt, dst_rate, dst_chans_n, dst_chans_layout, print_info);
126 abufsink_cfg();
127 r = avfilter_link(abufsrc_ctx_l, 0, vol_ctx_l, 0);
128 if (r < 0)
129 fatal("unable to connect the audio buffer source filter to the volume filter\n");
130 r = avfilter_link(vol_ctx_l, 0, afmt_ctx_l, 0);
131 if (r < 0)
132 fatal("unable to connect the volume filter to the audio format filter\n");
133 r = avfilter_link(afmt_ctx_l, 0, abufsink_ctx_l, 0);
134 if (r < 0)
135 fatal("unable to connect the audio format filter to the audio buffer sink filter\n");
136 r = avfilter_graph_config(graph_l, 0);
137 if (r < 0)
138 fatal("unable to configure the filter graph\n");
139 /*--------------------------------------------------------------------*/
140 if (!print_info)
141 return;
142 dump_str = avfilter_graph_dump(graph_l, 0);
143 if (dump_str == 0) {
144 warning("unable to get a filter graph description\n");
145 return;
147 pout("GRAPH START-------------------------------------------------------\n");
148 npv_pout("%s", dump_str);
149 avutil_free(dump_str);
150 pout("GRAPH END---------------------------------------------------------\n");
152 STATIC void npv_audio_filt_cmd_mute(void)
154 int r;
155 u8 vol_l10n_str[sizeof("xxx.xx")]; /* should be overkill */
156 u8 resp[STR_SZ];
158 if (filt_p.muted) {
159 pout("COMMAND:unmuting\n");
161 snprintf(vol_l10n_str, sizeof(vol_l10n_str), "%f", filt_p.vol);
162 r = avfilter_graph_send_cmd(graph_l, "vol", "volume",
163 vol_l10n_str, resp, sizeof(resp), 0);
164 if (r < 0) {
165 warning("ffmpeg:volume context:unable to mute the volume to 0:response from volume filter:%s\n", resp);
166 } else {
167 filt_p.muted = false;
169 } else {
170 pout("COMMAND:muting\n");
172 r = avfilter_graph_send_cmd(graph_l, "vol", "volume",
173 double_zero_l10n_str_l, resp, sizeof(resp), 0);
174 if (r < 0) {
175 warning("ffmpeg:volume context:unable to mute the volume to 0:response from volume filter:%s\n", resp);
176 } else {
177 filt_p.muted = true;
181 STATIC void npv_audio_filt_cmd_vol_down(void)
183 int r;
184 u8 vol_l10n_str[sizeof("xxx.xx")]; /* should be overkill */
185 u8 resp[STR_SZ];
187 filt_p.vol -= VOL_DELTA;
188 if (filt_p.vol < 0.)
189 filt_p.vol = 0.;
190 snprintf(vol_l10n_str, sizeof(vol_l10n_str), "%f", filt_p.vol);
191 pout("COMMAND:volume down to value %s\n", vol_l10n_str);
192 if (!filt_p.muted) {
193 r = avfilter_graph_send_cmd(graph_l, "vol", "volume",
194 vol_l10n_str, resp, sizeof(resp), 0);
195 if (r < 0)
196 warning("ffmpeg:volume context:unable to set the volume down to \"%s\":response from volume filter:\"%s\"\n", resp);
199 STATIC void npv_audio_filt_cmd_vol_up(void)
201 int r;
202 u8 vol_l10n_str[sizeof("xxx.xx")]; /* should be overkill */
203 u8 resp[STR_SZ];
205 filt_p.vol += VOL_DELTA;
206 if (filt_p.vol > 1.)
207 filt_p.vol = 1.;
208 snprintf(vol_l10n_str, sizeof(vol_l10n_str), "%f", filt_p.vol);
209 pout("COMMAND:volume up to value %s\n", vol_l10n_str);
210 if (!filt_p.muted) {
211 r = avfilter_graph_send_cmd(graph_l, "vol", "volume",
212 vol_l10n_str, resp, sizeof(resp), 0);
213 if (r < 0)
214 warning("ffmpeg:volume context:unable to set the volume up to \"%s\":response from volume filter:\"%s\"\n", resp);