[FFmpeg-devel] [PATCH]Use SND_PCM_FORMAT_S24_LE if bits_per_raw_sample <= 24

Carl Eugen Hoyos cehoyos at ag.or.at
Mon Apr 25 19:38:48 CEST 2011


Hi!

My hardware supports both 24 and 32 bit, but I imagine hardware exists where 
attached is useful.

Please comment, Carl Eugen
-------------- next part --------------
diff --git a/libavdevice/alsa-audio-common.c b/libavdevice/alsa-audio-common.c
index c00e973..3e3a98a 100644
--- a/libavdevice/alsa-audio-common.c
+++ b/libavdevice/alsa-audio-common.c
@@ -33,15 +33,20 @@
 
 #include "alsa-audio.h"
 
-static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
+static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id
+                                                       int bits_per_sample)
 {
     switch(codec_id) {
         case CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
         case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
         case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
         case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
-        case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
-        case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
+        case CODEC_ID_PCM_S32LE: return bits_per_sample > 24 ?
+                                        SND_PCM_FORMAT_S32_LE :
+                                        SND_PCM_FORMAT_S24_LE;
+        case CODEC_ID_PCM_S32BE: return bits_per_sample > 24 ?
+                                        SND_PCM_FORMAT_S32_BE :
+                                        SND_PCM_FORMAT_S24_BE;
         case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
         case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
         case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
@@ -185,7 +190,8 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
 
     if (*codec_id == CODEC_ID_NONE)
         *codec_id = DEFAULT_CODEC_ID;
-    format = codec_id_to_pcm_format(*codec_id);
+    format = codec_id_to_pcm_format(*codec_id,
+                                    ctx->streams[0]->codec->bits_per_raw_sample);
     if (format == SND_PCM_FORMAT_UNKNOWN) {
         av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
         return AVERROR(ENOSYS);


More information about the ffmpeg-devel mailing list