audio: make mp_chmap_to_str() return a stack-allocated string

Simplifies memory management.
This commit is contained in:
wm4 2014-11-24 19:56:01 +01:00
parent 2228d47373
commit 28b6ce39d3
6 changed files with 25 additions and 27 deletions

View File

@ -93,10 +93,9 @@ bool mp_audio_config_valid(const struct mp_audio *mpa)
char *mp_audio_fmt_to_str(int srate, const struct mp_chmap *chmap, int format)
{
char *chstr = mp_chmap_to_str(chmap);
char *res = talloc_asprintf(NULL, "%dHz %s %dch %s", srate, chstr,
chmap->num, af_fmt_to_str(format));
talloc_free(chstr);
char *res = talloc_asprintf(NULL, "%dHz %s %dch %s", srate,
mp_chmap_to_str(chmap), chmap->num,
af_fmt_to_str(format));
return res;
}

View File

@ -18,6 +18,7 @@
#include <stdlib.h>
#include <assert.h>
#include "common/common.h"
#include "common/msg.h"
#include "chmap.h"
@ -376,34 +377,35 @@ void mp_chmap_get_reorder(int dst[MP_NUM_CHANNELS], const struct mp_chmap *from,
// Returns something like "fl-fr-fc". If there's a standard layout in lavc
// order, return that, e.g. "3.0" instead of "fl-fr-fc".
// Unassigned but valid speakers get names like "sp28".
char *mp_chmap_to_str(const struct mp_chmap *src)
char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src)
{
char *res = talloc_strdup(NULL, "");
buf[0] = '\0';
if (mp_chmap_is_unknown(src))
return talloc_asprintf_append_buffer(res, "unknown%d", src->num);
if (mp_chmap_is_unknown(src)) {
snprintf(buf, buf_size, "unknown%d", src->num);
return buf;
}
for (int n = 0; n < src->num; n++) {
int sp = src->speaker[n];
const char *s = sp < MP_SPEAKER_ID_COUNT ? speaker_names[sp][0] : NULL;
char buf[10];
char sp_buf[10];
if (!s) {
snprintf(buf, sizeof(buf), "sp%d", sp);
s = buf;
snprintf(sp_buf, sizeof(sp_buf), "sp%d", sp);
s = sp_buf;
}
res = talloc_asprintf_append_buffer(res, "%s%s", n > 0 ? "-" : "", s);
mp_snprintf_cat(buf, buf_size, "%s%s", n > 0 ? "-" : "", s);
}
// To standard layout name
for (int n = 0; std_layout_names[n][0]; n++) {
if (res && strcmp(res, std_layout_names[n][1]) == 0) {
talloc_free(res);
res = talloc_strdup(NULL, std_layout_names[n][0]);
if (strcmp(buf, std_layout_names[n][1]) == 0) {
snprintf(buf, buf_size, "%s", std_layout_names[n][0]);
break;
}
}
return res;
return buf;
}
// If src can be parsed as channel map (as produced by mp_chmap_to_str()),

View File

@ -120,7 +120,9 @@ void mp_chmap_reorder_to_lavc(struct mp_chmap *map);
void mp_chmap_get_reorder(int dst[MP_NUM_CHANNELS], const struct mp_chmap *from,
const struct mp_chmap *to);
char *mp_chmap_to_str(const struct mp_chmap *src);
char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src);
#define mp_chmap_to_str(m) mp_chmap_to_str_buf((char[64]){0}, 64, (m))
bool mp_chmap_from_str(struct mp_chmap *dst, bstr src);
struct mp_log;

View File

@ -214,10 +214,9 @@ static struct ao *ao_alloc_pb(bool probing, struct mpv_global *global,
static int ao_init(struct ao *ao)
{
char *chmap = mp_chmap_to_str(&ao->channels);
MP_VERBOSE(ao, "requested format: %d Hz, %s channels, %s\n",
ao->samplerate, chmap, af_fmt_to_str(ao->format));
talloc_free(chmap);
ao->samplerate, mp_chmap_to_str(&ao->channels),
af_fmt_to_str(ao->format));
ao->api = ao->driver->play ? &ao_api_push : &ao_api_pull;
ao->api_priv = talloc_zero_size(ao, ao->api->priv_size);

View File

@ -317,10 +317,8 @@ static const char *select_chmap(struct ao *ao)
return device_channel_layouts[n][0];
}
char *name = mp_chmap_to_str(&ao->channels);
MP_ERR(ao, "channel layout %s (%d ch) not supported.\n",
name, ao->channels.num);
talloc_free(name);
mp_chmap_to_str(&ao->channels), ao->channels.num);
return "default";
}
@ -587,9 +585,7 @@ static int init(struct ao *ao)
for (int c = 0; c < chmap.num; c++)
chmap.speaker[c] = find_mp_channel(alsa_chmap->pos[c]);
char *mp_map_str = mp_chmap_to_str(&chmap);
MP_VERBOSE(ao, "which we understand as: %s\n", mp_map_str);
talloc_free(mp_map_str);
MP_VERBOSE(ao, "which we understand as: %s\n", mp_chmap_to_str(&chmap));
if (mp_chmap_is_valid(&chmap) && chmap.num == ao->channels.num) {
MP_VERBOSE(ao, "using the ALSA channel map.\n");

View File

@ -1571,7 +1571,7 @@ static int mp_property_channels(void *ctx, struct m_property *prop,
return M_PROPERTY_UNAVAILABLE;
switch (action) {
case M_PROPERTY_PRINT:
*(char **) arg = mp_chmap_to_str(&fmt.channels);
*(char **) arg = talloc_strdup(NULL, mp_chmap_to_str(&fmt.channels));
return M_PROPERTY_OK;
case M_PROPERTY_GET:
*(int *)arg = fmt.channels.num;