a35e7f4d1b23723fb02116d6c98eb2ce7c841af6
[openwrt/staging/blocktrron.git] /
1 From e866f9fc7c6dd6af1e74ce6fa50db9ba21acae5e Mon Sep 17 00:00:00 2001
2 From: Matthias Reichl <hias@horus.com>
3 Date: Sat, 24 Jun 2023 18:52:16 +0200
4 Subject: [PATCH] ALSA: pcm: fix ELD constraints for (E)AC3, DTS(-HD) and MLP
5 formats
6
7 commit 04b49b90caeed0b5544ff616d654900d27d403b6 upstream.
8
9 The SADs of compressed formats contain the channel and sample rate
10 info of the audio data inside the compressed stream, but when
11 building constraints we must use the rates and channels used to
12 transport the compressed streams.
13
14 eg 48kHz 6ch EAC3 needs to be transmitted as a 2ch 192kHz stream.
15
16 This patch fixes the constraints for the common AC3 and DTS formats,
17 the constraints for the less common MPEG, DSD etc formats are copied
18 directly from the info in the SADs as before as I don't have the specs
19 and equipment to test those.
20
21 Signed-off-by: Matthias Reichl <hias@horus.com>
22 Link: https://lore.kernel.org/r/20230624165216.5719-1-hias@horus.com
23 Signed-off-by: Takashi Iwai <tiwai@suse.de>
24 ---
25 sound/core/pcm_drm_eld.c | 73 ++++++++++++++++++++++++++++++++++++++--
26 1 file changed, 70 insertions(+), 3 deletions(-)
27
28 --- a/sound/core/pcm_drm_eld.c
29 +++ b/sound/core/pcm_drm_eld.c
30 @@ -2,11 +2,25 @@
31 /*
32 * PCM DRM helpers
33 */
34 +#include <linux/bitfield.h>
35 #include <linux/export.h>
36 +#include <linux/hdmi.h>
37 #include <drm/drm_edid.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_drm_eld.h>
40
41 +#define SAD0_CHANNELS_MASK GENMASK(2, 0) /* max number of channels - 1 */
42 +#define SAD0_FORMAT_MASK GENMASK(6, 3) /* audio format */
43 +
44 +#define SAD1_RATE_MASK GENMASK(6, 0) /* bitfield of supported rates */
45 +#define SAD1_RATE_32000_MASK BIT(0)
46 +#define SAD1_RATE_44100_MASK BIT(1)
47 +#define SAD1_RATE_48000_MASK BIT(2)
48 +#define SAD1_RATE_88200_MASK BIT(3)
49 +#define SAD1_RATE_96000_MASK BIT(4)
50 +#define SAD1_RATE_176400_MASK BIT(5)
51 +#define SAD1_RATE_192000_MASK BIT(6)
52 +
53 static const unsigned int eld_rates[] = {
54 32000,
55 44100,
56 @@ -17,9 +31,62 @@ static const unsigned int eld_rates[] =
57 192000,
58 };
59
60 +static unsigned int map_rate_families(const u8 *sad,
61 + unsigned int mask_32000,
62 + unsigned int mask_44100,
63 + unsigned int mask_48000)
64 +{
65 + unsigned int rate_mask = 0;
66 +
67 + if (sad[1] & SAD1_RATE_32000_MASK)
68 + rate_mask |= mask_32000;
69 + if (sad[1] & (SAD1_RATE_44100_MASK | SAD1_RATE_88200_MASK | SAD1_RATE_176400_MASK))
70 + rate_mask |= mask_44100;
71 + if (sad[1] & (SAD1_RATE_48000_MASK | SAD1_RATE_96000_MASK | SAD1_RATE_192000_MASK))
72 + rate_mask |= mask_48000;
73 + return rate_mask;
74 +}
75 +
76 +static unsigned int sad_rate_mask(const u8 *sad)
77 +{
78 + switch (FIELD_GET(SAD0_FORMAT_MASK, sad[0])) {
79 + case HDMI_AUDIO_CODING_TYPE_PCM:
80 + return sad[1] & SAD1_RATE_MASK;
81 + case HDMI_AUDIO_CODING_TYPE_AC3:
82 + case HDMI_AUDIO_CODING_TYPE_DTS:
83 + return map_rate_families(sad,
84 + SAD1_RATE_32000_MASK,
85 + SAD1_RATE_44100_MASK,
86 + SAD1_RATE_48000_MASK);
87 + case HDMI_AUDIO_CODING_TYPE_EAC3:
88 + case HDMI_AUDIO_CODING_TYPE_DTS_HD:
89 + case HDMI_AUDIO_CODING_TYPE_MLP:
90 + return map_rate_families(sad,
91 + 0,
92 + SAD1_RATE_176400_MASK,
93 + SAD1_RATE_192000_MASK);
94 + default:
95 + /* TODO adjust for other compressed formats as well */
96 + return sad[1] & SAD1_RATE_MASK;
97 + }
98 +}
99 +
100 static unsigned int sad_max_channels(const u8 *sad)
101 {
102 - return 1 + (sad[0] & 7);
103 + switch (FIELD_GET(SAD0_FORMAT_MASK, sad[0])) {
104 + case HDMI_AUDIO_CODING_TYPE_PCM:
105 + return 1 + FIELD_GET(SAD0_CHANNELS_MASK, sad[0]);
106 + case HDMI_AUDIO_CODING_TYPE_AC3:
107 + case HDMI_AUDIO_CODING_TYPE_DTS:
108 + case HDMI_AUDIO_CODING_TYPE_EAC3:
109 + return 2;
110 + case HDMI_AUDIO_CODING_TYPE_DTS_HD:
111 + case HDMI_AUDIO_CODING_TYPE_MLP:
112 + return 8;
113 + default:
114 + /* TODO adjust for other compressed formats as well */
115 + return 1 + FIELD_GET(SAD0_CHANNELS_MASK, sad[0]);
116 + }
117 }
118
119 static int eld_limit_rates(struct snd_pcm_hw_params *params,
120 @@ -42,7 +109,7 @@ static int eld_limit_rates(struct snd_pc
121 * requested number of channels.
122 */
123 if (c->min <= max_channels)
124 - rate_mask |= sad[1];
125 + rate_mask |= sad_rate_mask(sad);
126 }
127 }
128
129 @@ -70,7 +137,7 @@ static int eld_limit_channels(struct snd
130 rate_mask |= BIT(i);
131
132 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3)
133 - if (rate_mask & sad[1])
134 + if (rate_mask & sad_rate_mask(sad))
135 t.max = max(t.max, sad_max_channels(sad));
136 }
137