Package: audacious-plugins / 3.2.4-1

ffaudio.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
Description: Make audacious work with current unstable version of FFAUDIO
Author: Cyril Lavier <cyril.lavier@davromaniak.eu>
Last-Update: 2012-01-28
---
 configure.ac               |    2 
 src/ffaudio/ffaudio-core.c |  105 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 94 insertions(+), 13 deletions(-)

--- audacious-plugins.orig/configure.ac
+++ audacious-plugins/configure.ac
@@ -545,7 +545,7 @@ AC_ARG_ENABLE(ffaudio,
 
 if test $enable_ffaudio = yes ; then
     PKG_CHECK_MODULES([FFMPEG],
-     [libavcodec >= 53.40.0 libavformat >= 53.5.0 libavutil >= 50.42.0],
+     [libavcodec >= 52.64.0 libavformat >= 52.110.0 libavutil >= 50.42.0],
      [have_ffaudio=yes], [have_ffaudio=no])
 fi
 
--- audacious-plugins.orig/src/ffaudio/ffaudio-core.c
+++ audacious-plugins/src/ffaudio/ffaudio-core.c
@@ -37,6 +37,14 @@
 #include <audacious/audtag.h>
 #include <libaudcore/audstrings.h>
 
+#if ! CHECK_LIBAVFORMAT_VERSION (53, 5, 0)
+#define avformat_find_stream_info(i, o) av_find_stream_info (i)
+#endif
+
+#if ! CHECK_LIBAVCODEC_VERSION (53, 8, 0)
+#define avcodec_open2(a, c, o) avcodec_open (a, c)
+#endif
+
 static GMutex *ctrl_mutex = NULL;
 static GCond *ctrl_cond = NULL;
 static gint64 seek_value = -1;
@@ -416,8 +424,11 @@ static gboolean ffaudio_play (InputPlayb
     AVCodecContext *c = NULL;
     AVStream *s = NULL;
     AVPacket pkt = {.data = NULL};
+    guint8 *outbuf = NULL, *resbuf = NULL;
     gint i, stream_id, errcount;
-    gboolean codec_opened = FALSE;
+    gint in_sample_size, out_sample_size, chunk_size;
+    ReSampleContext *resctx = NULL;
+    gboolean codec_opened = FALSE, do_resampling = FALSE;
     gint out_fmt;
     gboolean seekable;
     gboolean error = FALSE;
@@ -453,14 +464,38 @@ static gboolean ffaudio_play (InputPlayb
 
     codec_opened = TRUE;
 
+    /* Determine if audio conversion or resampling is needed */
+    in_sample_size = av_get_bytes_per_sample (c->sample_fmt);
+    out_sample_size = av_get_bytes_per_sample (SAMPLE_FMT_S16);
+
+    chunk_size = out_sample_size * c->channels * (c->sample_rate / 50);
+
+
     switch (c->sample_fmt) {
         case AV_SAMPLE_FMT_U8: out_fmt = FMT_U8; break;
         case AV_SAMPLE_FMT_S16: out_fmt = FMT_S16_NE; break;
         case AV_SAMPLE_FMT_S32: out_fmt = FMT_S32_NE; break;
         case AV_SAMPLE_FMT_FLT: out_fmt = FMT_FLOAT; break;
-    default:
-        fprintf (stderr, "ffaudio: Unsupported audio format %d\n", (int) c->sample_fmt);
-        goto error_exit;
+        default: do_resampling = TRUE; break;
+    }
+
+    if (do_resampling)
+    {
+        /* Initialize resampling context */
+        out_fmt = FMT_S16_NE;
+
+        AUDDBG("resampling needed chn=%d, rate=%d, fmt=%d -> chn=%d, rate=%d, fmt=S16NE\n",
+            c->channels, c->sample_rate, c->sample_fmt,
+            c->channels, c->sample_rate);
+
+        resctx = av_audio_resample_init(
+            c->channels, c->channels,
+            c->sample_rate, c->sample_rate,
+            SAMPLE_FMT_S16, c->sample_fmt,
+            16, 10, 0, 0.8);
+
+        if (resctx == NULL)
+            goto error_exit;
     }
 
     /* Open audio output */
@@ -474,6 +509,10 @@ static gboolean ffaudio_play (InputPlayb
 
     playback->set_gain_from_playlist(playback);
 
+    /* Allocate output buffer aligned to 16 byte boundary */
+    outbuf = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
+    resbuf = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
+
     AUDDBG("setting parameters\n");
 
     if (pause)
@@ -546,6 +585,9 @@ static gboolean ffaudio_play (InputPlayb
         memcpy(&tmp, &pkt, sizeof(tmp));
         while (tmp.size > 0 && !stop_flag)
         {
+            gint len, out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+            guint8 *outbuf_p;
+
             /* Check for seek request and bail out if we have one */
             g_mutex_lock(ctrl_mutex);
             if (seek_value != -1)
@@ -563,25 +605,60 @@ static gboolean ffaudio_play (InputPlayb
             }
             g_mutex_unlock(ctrl_mutex);
 
-            AVFrame * frame = avcodec_alloc_frame ();
-            int decoded = 0;
-            int len = avcodec_decode_audio4 (c, frame, & decoded, & tmp);
+            /* Decode whatever we can of the frame data */
+            len = avcodec_decode_audio3(c, (gint16 *)outbuf, &out_size, &tmp);
 
             if (len < 0)
             {
-                fprintf (stderr, "ffaudio: decode_audio() failed, code %d\n", len);
+                AUDDBG("codec failure, breaking out of loop\n");
                 break;
             }
 
             tmp.size -= len;
             tmp.data += len;
 
-            if (! decoded)
+            if (out_size <= 0)
                 continue;
 
-            playback->output->write_audio (frame->data[0], FMT_SIZEOF (out_fmt)
-             * c->channels * frame->nb_samples);
-            av_free (frame);
+            /* Perform audio resampling if necessary */
+            if (do_resampling)
+            {
+                out_size = audio_resample(resctx,
+                    (gint16 *)resbuf, (gint16 *)outbuf,
+                    out_size / in_sample_size) * out_sample_size;
+                outbuf_p = resbuf;
+            }
+            else
+                outbuf_p = outbuf;
+
+            /* Output audio in small blocks */
+            while (out_size > 0 && !stop_flag && (stop_time < 0 ||
+             playback->output->written_time () < stop_time))
+            {
+                gint writeoff = MIN (chunk_size, out_size);
+
+                playback->output->write_audio((gint16 *)outbuf_p, writeoff);
+
+                outbuf_p += writeoff;
+                out_size -= writeoff;
+
+                /* Check for seek request and bail out if we have one */
+                g_mutex_lock(ctrl_mutex);
+                if (seek_value != -1)
+                {
+                    if (!seekable)
+                    {
+                        seek_value = -1;
+                        g_cond_signal(ctrl_cond);
+                    }
+                    else
+                    {
+                        g_mutex_unlock(ctrl_mutex);
+                        break;
+                    }
+                }
+                g_mutex_unlock(ctrl_mutex);
+            }
         }
 
         if (pkt.data)
@@ -604,6 +681,10 @@ error_exit:
 
     stop_flag = TRUE;
 
+    av_free(outbuf);
+    av_free(resbuf);
+    if (resctx != NULL)
+        audio_resample_close(resctx);
     if (pkt.data)
         av_free_packet(&pkt);
     if (codec_opened)