[FFmpeg-trac] #6156(avcodec:new): Issue with FFMPEG ALSA CPU usage

FFmpeg trac at avcodec.org
Sat Feb 18 04:01:21 EET 2017

#6156: Issue with FFMPEG ALSA CPU usage
             Reporter:  mushm0m  |                     Type:  defect
               Status:  new      |                 Priority:  normal
            Component:  avcodec  |                  Version:  unspecified
             Keywords:  ALSA     |               Blocked By:
             Blocking:           |  Reproduced by developer:  0
Analyzed by developer:  0        |
 I am on a Raspberry Pi Zero with a Pi Camera and an I2S MEMS mic, running
 the latest Raspbian Jessie. I am attempting to livestream audio and video.
 I've discovered that when I record ALSA audio with FFMPEG, that process
 takes 95% of my CPU and produces stuttering, unusable audio in the stream.

 I have compiled FFMPEG with ALSA following this: http://vpaste.net/cXV9F

 I isolated the issue to the following:

 1. ffmpeg -f alsa -acodec pcm_s32le -i mic_sv temp.wav
 This single command takes 95% of my CPU.

 2. arecord -Dmic_sv -c2 -r48000 -fS32_LE -twav temp.wav
 This single command takes <5% of my CPU.

 I have added a custom hook to ffmpeg so we can see the PCM settings. I
 have ensured that settings (buffer size, period size, etc.) are identical
 between the two commands.

 Here is the full console output of FFMPEG: http://vpaste.net/2M5gk
 Here is the console output of Arecord:  http://vpaste.net/CPk3A

 After a long chat in the FFMPEG IRC channel, we believe there may be a bug
 in the way FFMPEG waits for ALSA packets. Arecord uses snd_pcm_wait() to
 wait for packets, and this command is not used in FFMPEG sources.
 Inserting '-re' before -i into my FFMPEG command reduces CPU usage to <5%,
 but is not the right approach, and causes other problems (bitrate is
 almost 0kbps, audio file generated is <5 kilobytes).

Ticket URL: <https://trac.ffmpeg.org/ticket/6156>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker

More information about the FFmpeg-trac mailing list