From e60b8f181dec744af25c3a52fb88f600cd1b63ea Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 22 Oct 2013 01:20:43 +0200 Subject: audio/filter: split af_format into separate filters, rename af_force af_format is the old audio conversion filter. It could do all possible conversions supported by the audio chain. However, ever since the addition of af_lavrresample, most conversions are done by libav/swresample, and af_format is used as fallback. Separate out the fallback cases and remove af_format. af_convert24 does 24 bit <-> 32 bit conversions, while af_convertsignendian does sign and endian conversions. Maybe the way the conversions are split sounds a bit odd. But the former changes the size of the audio data, while the latter is fully in-place, so there's at least different buffer management. This requires a quite complicated algorithm to make sure all these "partial" conversion filters can actually get from one format to another. E.g. s24le->s32be always requires convertsignendian and convert24, but af.c has no idea what the intermediate format should be. So I added a graph search (trying every possible format and filter) to determine required format and filter. When I wrote this, it seemed this was still better than messing everything into af_lavrresample, but maybe this is overkill and I'll change my opinion. For now, it seems nice to get rid of af_format though. The AC3->IEC61937 conversion isn't supported anymore, but I don't think this is needed anywhere. Most AOs test all formats explicitly, or use the AF_FORMAT_IS_IEC61937() macro (which includes AC3). One positive consequence of this change is that conversions always include dithering (done by libav/swresample), instead of possibly going through af_format, which doesn't do anything fancy. Rename af_force to af_format. It's essentially compatible with command line uses of af_format. We retain a compatibility alias for af_force. --- audio/filter/af_format.c | 494 ----------------------------------------------- 1 file changed, 494 deletions(-) delete mode 100644 audio/filter/af_format.c (limited to 'audio/filter/af_format.c') diff --git a/audio/filter/af_format.c b/audio/filter/af_format.c deleted file mode 100644 index 6166ee3ff0..0000000000 --- a/audio/filter/af_format.c +++ /dev/null @@ -1,494 +0,0 @@ -/* - * This audio filter changes the format of a data block. Valid - * formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE - * AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE. - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "af.h" -#include "compat/mpbswap.h" - -/* Functions used by play to convert the input audio to the correct - format */ - -// Switch endianness -static void endian(void* in, void* out, int len, int bps); -// From signed to unsigned and the other way -static void si2us(void* data, int len, int bps); -// Change the number of bits per sample -static void change_bps(void* in, void* out, int len, int inbps, int outbps); -// From float to int signed -static void float2int(float* in, void* out, int len, int bps); -// From signed int to float -static void int2float(void* in, float* out, int len, int bps); - -static struct mp_audio* play(struct af_instance* af, struct mp_audio* data); -static struct mp_audio* play_swapendian(struct af_instance* af, struct mp_audio* data); -static struct mp_audio* play_float_s16(struct af_instance* af, struct mp_audio* data); -static struct mp_audio* play_s16_float(struct af_instance* af, struct mp_audio* data); - -// Helper functions to check sanity for input arguments - -// Sanity check for bytes per sample -static int check_bps(int bps) -{ - if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ - mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] The number of bytes per sample" - " must be 1, 2, 3 or 4. Current value is %i \n",bps); - return AF_ERROR; - } - return AF_OK; -} - -// Check for unsupported formats -static int check_format(int format) -{ - char buf[256]; - if ((format & AF_FORMAT_SPECIAL_MASK) == 0) - return AF_OK; - mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] Sample format %s not yet supported \n", - af_fmt2str(format,buf,256)); - return AF_ERROR; -} - -static bool test_conversion(int src_format, int dst_format) -{ - // This is the fallback conversion filter, so this filter is always - // inserted on format mismatches if no other filter can handle it. - // Initializing the filter might still fail. - return true; -} - -// Initialization and runtime control -static int control(struct af_instance* af, int cmd, void* arg) -{ - switch(cmd){ - case AF_CONTROL_REINIT:{ - char buf1[256]; - char buf2[256]; - struct mp_audio *data = arg; - int supported_ac3 = 0; - - // Make sure this filter isn't redundant - if(af->data->format == data->format) - return AF_DETACH; - - // A bit complex because we can convert AC3 - // to generic iec61937 but not the other way - // round. - if (AF_FORMAT_IS_AC3(af->data->format)) - supported_ac3 = AF_FORMAT_IS_AC3(data->format); - else if (AF_FORMAT_IS_IEC61937(af->data->format)) - supported_ac3 = AF_FORMAT_IS_IEC61937(data->format); - - // Allow trivial AC3-endianness conversion - if (!supported_ac3) - // Check for errors in configuration - if((AF_OK != check_bps(data->bps)) || - (AF_OK != check_format(data->format)) || - (AF_OK != check_bps(af->data->bps)) || - (AF_OK != check_format(af->data->format))) - return AF_ERROR; - - af_fmt2str(data->format,buf1,256); - af_fmt2str(af->data->format,buf2,256); - mp_msg(MSGT_AFILTER, MSGL_V, "[format] Changing sample format from %s to %s\n", - buf1, buf2); - - af->data->rate = data->rate; - mp_audio_set_channels(af->data, &data->channels); - af->mul = (double)af->data->bps / data->bps; - - af->play = play; // set default - - // look whether only endianness differences are there - if ((af->data->format & ~AF_FORMAT_END_MASK) == - (data->format & ~AF_FORMAT_END_MASK)) - { - mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated endianness conversion only\n"); - af->play = play_swapendian; - } - if ((data->format == AF_FORMAT_FLOAT_NE) && - (af->data->format == AF_FORMAT_S16_NE)) - { - mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", - buf1, buf2); - af->play = play_float_s16; - } - if ((data->format == AF_FORMAT_S16_NE) && - (af->data->format == AF_FORMAT_FLOAT_NE)) - { - mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", - buf1, buf2); - af->play = play_s16_float; - } - return AF_OK; - } - case AF_CONTROL_COMMAND_LINE:{ - int format = af_str2fmt_short(bstr0(arg)); - if (!format) { - mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] %s is not a valid format\n", (char *)arg); - return AF_ERROR; - } - if(AF_OK != af->control(af, AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) - return AF_ERROR; - return AF_OK; - } - case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ - // Check for errors in configuration - if(!AF_FORMAT_IS_AC3(*(int*)arg) && AF_OK != check_format(*(int*)arg)) - return AF_ERROR; - - mp_audio_set_format(af->data, *(int*)arg); - - return AF_OK; - } - } - return AF_UNKNOWN; -} - -// Deallocate memory -static void uninit(struct af_instance* af) -{ - if (af->data) - free(af->data->audio); - free(af->data); - af->setup = 0; -} - -static struct mp_audio* play_swapendian(struct af_instance* af, struct mp_audio* data) -{ - struct mp_audio* l = af->data; // Local data - struct mp_audio* c = data; // Current working data - int len = c->len/c->bps; // Length in samples of current audio block - - if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; - - endian(c->audio,l->audio,len,c->bps); - - c->audio = l->audio; - mp_audio_set_format(c, l->format); - - return c; -} - -static struct mp_audio* play_float_s16(struct af_instance* af, struct mp_audio* data) -{ - struct mp_audio* l = af->data; // Local data - struct mp_audio* c = data; // Current working data - int len = c->len/4; // Length in samples of current audio block - - if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; - - float2int(c->audio, l->audio, len, 2); - - c->audio = l->audio; - mp_audio_set_format(c, l->format); - c->len = len*2; - - return c; -} - -static struct mp_audio* play_s16_float(struct af_instance* af, struct mp_audio* data) -{ - struct mp_audio* l = af->data; // Local data - struct mp_audio* c = data; // Current working data - int len = c->len/2; // Length in samples of current audio block - - if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; - - int2float(c->audio, l->audio, len, 2); - - c->audio = l->audio; - mp_audio_set_format(c, l->format); - c->len = len*4; - - return c; -} - -// Filter data through filter -static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) -{ - struct mp_audio* l = af->data; // Local data - struct mp_audio* c = data; // Current working data - int len = c->len/c->bps; // Length in samples of current audio block - - if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; - - // Change to cpu native endian format - if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) - endian(c->audio,c->audio,len,c->bps); - - // Conversion table - if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { - float2int(c->audio, l->audio, len, l->bps); - if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) - si2us(l->audio,len,l->bps); - } else { - // Input must be int - - // Change signed/unsigned - if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ - si2us(c->audio,len,c->bps); - } - // Convert to special formats - switch(l->format&AF_FORMAT_POINT_MASK){ - case(AF_FORMAT_F): - int2float(c->audio, l->audio, len, c->bps); - break; - default: - // Change the number of bits - if(c->bps != l->bps) - change_bps(c->audio,l->audio,len,c->bps,l->bps); - else - memcpy(l->audio,c->audio,len*c->bps); - break; - } - } - - // Switch from cpu native endian to the correct endianness - if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) - endian(l->audio,l->audio,len,l->bps); - - // Set output data - c->audio = l->audio; - mp_audio_set_format(c, l->format); - c->len = len*l->bps; - return c; -} - -// Allocate memory and set function pointers -static int af_open(struct af_instance* af){ - af->control=control; - af->uninit=uninit; - af->play=play; - af->mul=1; - af->data=calloc(1,sizeof(struct mp_audio)); - if(af->data == NULL) - return AF_ERROR; - return AF_OK; -} - -// Description of this filter -struct af_info af_info_format = { - "Sample format conversion", - "format", - "Anders", - "", - AF_FLAGS_REENTRANT, - af_open, - .test_conversion = test_conversion, -}; - -static inline uint32_t load24bit(void* data, int pos) { -#if BYTE_ORDER == BIG_ENDIAN - return (((uint32_t)((uint8_t*)data)[3*pos])<<24) | - (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | - (((uint32_t)((uint8_t*)data)[3*pos+2])<<8); -#else - return (((uint32_t)((uint8_t*)data)[3*pos])<<8) | - (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | - (((uint32_t)((uint8_t*)data)[3*pos+2])<<24); -#endif -} - -static inline void store24bit(void* data, int pos, uint32_t expanded_value) { -#if BYTE_ORDER == BIG_ENDIAN - ((uint8_t*)data)[3*pos]=expanded_value>>24; - ((uint8_t*)data)[3*pos+1]=expanded_value>>16; - ((uint8_t*)data)[3*pos+2]=expanded_value>>8; -#else - ((uint8_t*)data)[3*pos]=expanded_value>>8; - ((uint8_t*)data)[3*pos+1]=expanded_value>>16; - ((uint8_t*)data)[3*pos+2]=expanded_value>>24; -#endif -} - -// Function implementations used by play -static void endian(void* in, void* out, int len, int bps) -{ - register int i; - switch(bps){ - case(2):{ - for(i=0;i>8); - break; - case(3): - for(i=0;i>24); - break; - case(2): - for(i=0;i>16); - break; - case(4): - for(i=0;i>24); - break; - case(2): - for(i=0;i>16); - break; - case(3): - for(i=0;i