+J2KWAVEncoder::process_audio (uint8_t* data, int size)
+{
+ /* This is a buffer we might use if we are sample-rate converting;
+ it will need freeing if so.
+ */
+ uint8_t* out_buffer = 0;
+
+ /* Maybe sample-rate convert */
+#if HAVE_SWRESAMPLE
+ if (_swr_context) {
+
+ uint8_t const * in[2] = {
+ data,
+ 0
+ };
+
+ /* Here's samples per channel */
+ int const samples = size / _fs->bytes_per_sample();
+
+ /* And here's frames (where 1 frame is a collection of samples, 1 for each channel,
+ so for 5.1 a frame would be 6 samples)
+ */
+ int const frames = samples / _fs->audio_channels;
+
+ /* Compute the resampled frame count and add 32 for luck */
+ int const out_buffer_size_frames = ceil (frames * target_sample_rate() / _fs->audio_sample_rate) + 32;
+ int const out_buffer_size_bytes = out_buffer_size_frames * _fs->audio_channels * _fs->bytes_per_sample();
+ out_buffer = new uint8_t[out_buffer_size_bytes];
+
+ uint8_t* out[2] = {
+ out_buffer,
+ 0
+ };
+
+ /* Resample audio */
+ int out_frames = swr_convert (_swr_context, out, out_buffer_size_frames, in, size);
+ if (out_frames < 0) {
+ throw EncodeError ("could not run sample-rate converter");
+ }
+
+ /* And point our variables at the resampled audio */
+ data = out_buffer;
+ size = out_frames * _fs->audio_channels * _fs->bytes_per_sample();
+ }
+#endif
+
+ write_audio (data, size);
+
+ /* Delete the sample-rate conversion buffer, if it exists */
+ delete[] out_buffer;
+}
+
+void
+J2KWAVEncoder::write_audio (uint8_t* data, int size)