1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-18 04:51:52 +00:00

audio: simplify further

Drop mp_chmap_diff() (which is unused too now), and implement
mp_chmap_diffn() in a slightly simpler way. (Too bad there is no
standard function for counting set bits.)
This commit is contained in:
wm4 2015-05-08 21:22:39 +02:00
parent 8d5924f2c9
commit 00130651da
3 changed files with 11 additions and 28 deletions

View File

@ -395,26 +395,20 @@ void mp_chmap_get_reorder(int src[MP_NUM_CHANNELS], const struct mp_chmap *from,
assert(src[n] < 0 || (to->speaker[n] == from->speaker[src[n]])); assert(src[n] < 0 || (to->speaker[n] == from->speaker[src[n]]));
} }
// Return channels that are only in a. static int popcount64(uint64_t bits)
// Performs the difference between a and b, and store it in diff. If b has
// channels that do not appear in a, those will not appear in the difference.
// To get to those the argument ordering in the function call has to be
// inverted. For the same reason, the diff with a superset will return no
// speakers.
void mp_chmap_diff(const struct mp_chmap *a, const struct mp_chmap *b,
struct mp_chmap *diff)
{ {
uint64_t a_mask = mp_chmap_to_lavc_unchecked(a); int r = 0;
uint64_t b_mask = mp_chmap_to_lavc_unchecked(b); for (int n = 0; n < 64; n++)
mp_chmap_from_lavc(diff, (a_mask ^ b_mask) & a_mask); r += !!(bits & (1ULL << n));
return r;
} }
// Return the number of channels only in a. // Return the number of channels only in a.
int mp_chmap_diffn(const struct mp_chmap *a, const struct mp_chmap *b) int mp_chmap_diffn(const struct mp_chmap *a, const struct mp_chmap *b)
{ {
struct mp_chmap diff; uint64_t a_mask = mp_chmap_to_lavc_unchecked(a);
mp_chmap_diff(a, b, &diff); uint64_t b_mask = mp_chmap_to_lavc_unchecked(b);
return diff.num; return popcount64((a_mask ^ b_mask) & a_mask);
} }
// Returns something like "fl-fr-fc". If there's a standard layout in lavc // Returns something like "fl-fr-fc". If there's a standard layout in lavc

View File

@ -123,8 +123,6 @@ void mp_chmap_reorder_to_lavc(struct mp_chmap *map);
void mp_chmap_get_reorder(int src[MP_NUM_CHANNELS], const struct mp_chmap *from, void mp_chmap_get_reorder(int src[MP_NUM_CHANNELS], const struct mp_chmap *from,
const struct mp_chmap *to); const struct mp_chmap *to);
void mp_chmap_diff(const struct mp_chmap *a, const struct mp_chmap *b,
struct mp_chmap *diff);
int mp_chmap_diffn(const struct mp_chmap *a, const struct mp_chmap *b); int mp_chmap_diffn(const struct mp_chmap *a, const struct mp_chmap *b);
char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src); char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src);

View File

@ -4,24 +4,15 @@
static void test_mp_chmap_diff(void **state) { static void test_mp_chmap_diff(void **state) {
struct mp_chmap a; struct mp_chmap a;
struct mp_chmap b; struct mp_chmap b;
struct mp_chmap diff;
mp_chmap_from_str(&a, bstr0("3.1")); mp_chmap_from_str(&a, bstr0("3.1"));
mp_chmap_from_str(&b, bstr0("2.1")); mp_chmap_from_str(&b, bstr0("2.1"));
mp_chmap_diff(&a, &b, &diff); assert_int_equal(mp_chmap_diffn(&a, &b), 1);
assert_int_equal(diff.num, 1);
assert_int_equal(diff.speaker[0], MP_SPEAKER_ID_FC);
mp_chmap_from_str(&b, bstr0("6.1(back)")); mp_chmap_from_str(&b, bstr0("6.1(back)"));
mp_chmap_diff(&a, &b, &diff); assert_int_equal(mp_chmap_diffn(&a, &b), 0);
assert_int_equal(diff.num, 0); assert_int_equal(mp_chmap_diffn(&b, &a), 3);
mp_chmap_diff(&b, &a, &diff);
assert_int_equal(diff.num, 3);
assert_int_equal(diff.speaker[0], MP_SPEAKER_ID_BL);
assert_int_equal(diff.speaker[1], MP_SPEAKER_ID_BR);
assert_int_equal(diff.speaker[2], MP_SPEAKER_ID_BC);
} }
int main(void) { int main(void) {