summaryrefslogtreecommitdiffstats
path: root/audio
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-11-24 19:56:01 +0100
committerwm4 <wm4@nowhere>2014-11-24 19:56:01 +0100
commit28b6ce39d3d88d0159b54c8ca558e993eac93056 (patch)
treefa3c94a84cd2efebf24b1ea5fd75dd1fbcc01c12 /audio
parent2228d4737367fb0303bec263bc06ca90c9ff8757 (diff)
downloadmpv-28b6ce39d3d88d0159b54c8ca558e993eac93056.tar.bz2
mpv-28b6ce39d3d88d0159b54c8ca558e993eac93056.tar.xz
audio: make mp_chmap_to_str() return a stack-allocated string
Simplifies memory management.
Diffstat (limited to 'audio')
-rw-r--r--audio/audio.c7
-rw-r--r--audio/chmap.c26
-rw-r--r--audio/chmap.h4
-rw-r--r--audio/out/ao.c5
-rw-r--r--audio/out/ao_alsa.c8
5 files changed, 24 insertions, 26 deletions
diff --git a/audio/audio.c b/audio/audio.c
index 0458e813e7..2cda1ba110 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -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;
}
diff --git a/audio/chmap.c b/audio/chmap.c
index a4d06bb700..5396d40657 100644
--- a/audio/chmap.c
+++ b/audio/chmap.c
@@ -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()),
diff --git a/audio/chmap.h b/audio/chmap.h
index c7851a8657..d0b9806ff5 100644
--- a/audio/chmap.h
+++ b/audio/chmap.h
@@ -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;
diff --git a/audio/out/ao.c b/audio/out/ao.c
index f0461b8c09..893699e2f5 100644
--- a/audio/out/ao.c
+++ b/audio/out/ao.c
@@ -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);
diff --git a/audio/out/ao_alsa.c b/audio/out/ao_alsa.c
index 910ac98437..01414d63fc 100644
--- a/audio/out/ao_alsa.c
+++ b/audio/out/ao_alsa.c
@@ -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");