options, libaf: unify audio format name handling

Remove the duplicated list of audio format names from m_option.c. Remove
"long" audio format names, and let af_fmt2str() print the usual short
format names. The long names were overly verbose, and were actually
rarely user visible. The only difference between af_fmt2str() and
af_fmt2str_short() is that the former prints unknown format values as
hexadecimal string as fallback.
This commit is contained in:
wm4 2012-08-29 00:34:21 +02:00
parent 7bb95cd8a5
commit 7c45be712f
4 changed files with 36 additions and 123 deletions

View File

@ -144,7 +144,7 @@ static int control(struct af_instance_s* af, int cmd, void* arg)
return AF_OK;
}
case AF_CONTROL_COMMAND_LINE:{
int format = af_str2fmt_short(arg);
int format = af_str2fmt_short(bstr0(arg));
if (format == -1) {
mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] %s is not a valid format\n", (char *)arg);
return AF_ERROR;

View File

@ -25,6 +25,7 @@
#include <sys/types.h>
#include "config.h"
#include "bstr.h"
// Endianness
#define AF_FORMAT_BE (0<<0) // Big Endian
@ -118,7 +119,14 @@
#define AF_FORMAT_IS_AC3(fmt) (((fmt) & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_AC3)
#define AF_FORMAT_IS_IEC61937(fmt) (((fmt) & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_IEC61937)
int af_str2fmt_short(const char *str);
struct af_fmt_entry {
const char *name;
int format;
};
extern const struct af_fmt_entry af_fmtstr_table[];
int af_str2fmt_short(bstr str);
int af_fmt2bits(int format);
int af_bits2fmt(int bits);
char* af_fmt2str(int format, char* str, int size);

View File

@ -53,64 +53,16 @@ int af_bits2fmt(int bits)
converted string, size is the size of the buffer */
char* af_fmt2str(int format, char* str, int size)
{
int i=0;
if (size < 1)
return NULL;
size--; // reserve one for terminating 0
// Endianness
if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK))
i+=snprintf(str,size-i,"little-endian ");
else
i+=snprintf(str,size-i,"big-endian ");
if(format & AF_FORMAT_SPECIAL_MASK){
switch(format & AF_FORMAT_SPECIAL_MASK){
case(AF_FORMAT_MU_LAW):
i+=snprintf(&str[i],size-i,"mu-law "); break;
case(AF_FORMAT_A_LAW):
i+=snprintf(&str[i],size-i,"A-law "); break;
case(AF_FORMAT_MPEG2):
i+=snprintf(&str[i],size-i,"MPEG-2 "); break;
case(AF_FORMAT_AC3):
i+=snprintf(&str[i],size-i,"AC3 "); break;
case(AF_FORMAT_IEC61937):
i+=snprintf(&str[i],size-i,"IEC61937 "); break;
case(AF_FORMAT_IMA_ADPCM):
i+=snprintf(&str[i],size-i,"IMA-ADPCM "); break;
default:
i+=snprintf(&str[i],size-i,"%s",mp_gtext("unknown format "));
const char *name = af_fmt2str_short(format);
if (name) {
snprintf(str, size, "%s", name);
} else {
snprintf(str, size, "%#x", format);
}
}
else{
// Bits
i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format));
// Point
if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK))
i+=snprintf(&str[i],size-i,"float ");
else{
// Sign
if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK))
i+=snprintf(&str[i],size-i,"unsigned ");
else
i+=snprintf(&str[i],size-i,"signed ");
i+=snprintf(&str[i],size-i,"int ");
}
}
// remove trailing space
if (i > 0 && str[i - 1] == ' ')
i--;
str[i] = 0; // make sure it is 0 terminated.
return str;
return str;
}
static struct {
const char *name;
const int format;
} af_fmtstr_table[] = {
const struct af_fmt_entry af_fmtstr_table[] = {
{ "mulaw", AF_FORMAT_MU_LAW },
{ "alaw", AF_FORMAT_A_LAW },
{ "mpeg2", AF_FORMAT_MPEG2 },
@ -146,7 +98,7 @@ static struct {
{ "floatbe", AF_FORMAT_FLOAT_BE },
{ "floatne", AF_FORMAT_FLOAT_NE },
{ NULL, 0 }
{0}
};
const char *af_fmt2str_short(int format)
@ -160,13 +112,18 @@ const char *af_fmt2str_short(int format)
return "??";
}
int af_str2fmt_short(const char* str)
int af_str2fmt_short(bstr str)
{
int i;
if (bstr_startswith0(str, "0x")) {
bstr rest;
int fmt = bstrtoll(str, &rest, 16);
if (rest.len == 0)
return fmt;
}
for (i = 0; af_fmtstr_table[i].name; i++)
if (!strcasecmp(str, af_fmtstr_table[i].name))
return af_fmtstr_table[i].format;
for (int i = 0; af_fmtstr_table[i].name; i++)
if (!bstrcasecmp0(str, af_fmtstr_table[i].name))
return af_fmtstr_table[i].format;
return -1;
}

View File

@ -953,78 +953,26 @@ const m_option_type_t m_option_type_imgfmt = {
#include "libaf/af_format.h"
/* FIXME: snyc with af_format.h */
static struct {
const char *name;
unsigned int fmt;
} mp_afmt_list[] = {
// SPECIAL
{"mulaw", AF_FORMAT_MU_LAW},
{"alaw", AF_FORMAT_A_LAW},
{"mpeg2", AF_FORMAT_MPEG2},
{"ac3le", AF_FORMAT_AC3_LE},
{"ac3be", AF_FORMAT_AC3_BE},
{"ac3ne", AF_FORMAT_AC3_NE},
{"imaadpcm", AF_FORMAT_IMA_ADPCM},
// ORDINARY
{"u8", AF_FORMAT_U8},
{"s8", AF_FORMAT_S8},
{"u16le", AF_FORMAT_U16_LE},
{"u16be", AF_FORMAT_U16_BE},
{"u16ne", AF_FORMAT_U16_NE},
{"s16le", AF_FORMAT_S16_LE},
{"s16be", AF_FORMAT_S16_BE},
{"s16ne", AF_FORMAT_S16_NE},
{"u24le", AF_FORMAT_U24_LE},
{"u24be", AF_FORMAT_U24_BE},
{"u24ne", AF_FORMAT_U24_NE},
{"s24le", AF_FORMAT_S24_LE},
{"s24be", AF_FORMAT_S24_BE},
{"s24ne", AF_FORMAT_S24_NE},
{"u32le", AF_FORMAT_U32_LE},
{"u32be", AF_FORMAT_U32_BE},
{"u32ne", AF_FORMAT_U32_NE},
{"s32le", AF_FORMAT_S32_LE},
{"s32be", AF_FORMAT_S32_BE},
{"s32ne", AF_FORMAT_S32_NE},
{"floatle", AF_FORMAT_FLOAT_LE},
{"floatbe", AF_FORMAT_FLOAT_BE},
{"floatne", AF_FORMAT_FLOAT_NE},
{ NULL, 0 }
};
static int parse_afmt(const m_option_t *opt, struct bstr name,
struct bstr param, void *dst)
{
uint32_t fmt = 0;
int i;
if (param.len == 0)
return M_OPT_MISSING_PARAM;
if (!bstrcmp0(param, "help")) {
mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Available formats:");
for (i = 0; mp_afmt_list[i].name; i++)
mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s", mp_afmt_list[i].name);
for (int i = 0; af_fmtstr_table[i].name; i++)
mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s", af_fmtstr_table[i].name);
mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
return M_OPT_EXIT - 1;
}
if (bstr_startswith0(param, "0x"))
fmt = bstrtoll(param, NULL, 16);
else {
for (i = 0; mp_afmt_list[i].name; i++) {
if (!bstrcasecmp0(param, mp_afmt_list[i].name)) {
fmt = mp_afmt_list[i].fmt;
break;
}
}
if (!mp_afmt_list[i].name) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"Option %.*s: unknown format name: '%.*s'\n",
BSTR_P(name), BSTR_P(param));
return M_OPT_INVALID;
}
int fmt = af_str2fmt_short(param);
if (fmt == -1) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"Option %.*s: unknown format name: '%.*s'\n",
BSTR_P(name), BSTR_P(param));
return M_OPT_INVALID;
}
if (dst)