Как Video4Linux и ALSA работают с кодеками

2366
pnongrata

Насколько я понимаю, кодек - это и декодер, и спецификация для декодирования. Поэтому некоторые группы пишут кодек, который объясняет, как декодировать из своего формата обратно в необработанный поток битов, и они оставляют реализацию кодирования любому, кто хочет написать совместимый кодер. Так, например, h.264 - это известный видеокодек (декодер), а x264 - это известный FOSS-кодер для h.264. Но есть много других кодеров h.264.

Насколько я понимаю, VideoForLinux (V4L2) - это набор библиотек ядра и пользовательского пространства для работы с потоковым видео на компьютерах с Linux.

Насколько я понимаю, ALSA - это набор библиотек ядра и пользовательского пространства для работы с потоковым аудио на компьютерах с Linux.

Если что-то, что я изложил выше, неверно, пожалуйста, начните с исправления / уточнения меня!

Если предположить, что я более или менее прав, то мне не удается понять, какую роль (если таковые имеются) должны играть V4L2 и ALSA при попытке кодировать, сжимать и передавать аудио- и видеопотоки на компьютере с Linux.

Если бы я мог использовать x264 для кодирования моего видеопотока, faac для кодирования моего аудиопотока, а затем ffmpegдля сжатия, мультиплексирования и передачи двух потоков, скажем, в контейнере MPEG-TS, тогда где в игру вступают V4L2 и ALSA?

И если ответ «они этого не делают», то кто-нибудь может привести пример или два, когда будет использоваться V4L2 / ALSA , чтобы я мог представить их в контексте и понять случаи использования, где они необходимы? Заранее спасибо.

2

2 ответа на вопрос

1
Frank Thomas
  1. Just a fine point detail, most codecs start as specs defining the format, and the methodology by which encoding and decoding occur, including API specs if applicable. From there implementations of both the encoder and decoder may be created. Not all implementations are created equal. For instance the CoreAVC H.264 decoder is multithreaded, whereas many other implementations are not.

  2. V4L2 is primarilly used for creating/capturing video on Linux systems, so it has an encoder built into it (or more likely it references an external one). It's largely used for webcams and video capture/TV cards. See more on Wikipedia. Apps that create raw streams use V4L2 to encode the video to their taste. The decoding is done by a different package (libavcodec perhaps).

  3. ALSA contains codecs, but is itself the support stack for all audio operations in Linux, so wherever you hear sound, ALSA is whats making that happen. I'm sure ALSA has components for audio input (mic/line-in) so it must be capable of some form of encoding, but I've not worked with it.

In your example, I don't believe that V4L2 would be involved at all, since you are using an H.264 encoder, which likely provides much better quality and performance than V4L2. ALSA will be use to play your video back, but will not be used in encoding it.

When you play any sound, that's ALSA at work. When you use your webcam, that's L4V2 at work.

0
LordNeckbeard

As for ffmpeg, ALSA and V4L2 are called input devices, not codecs (not that the term codec is always accurate—I generally call something an encoder and/or decoder to be more specific). Other input devices include JACK, pulse, x11grab, iec61883, dv1394, etc. From the FFmpeg documentation:

Input devices are configured elements in FFmpeg which allow to access the data coming from a multimedia device attached to your system.

For example, you can use ALSA to capture audio from a microphone that is plugged into your audio card. You can use V4L2 to capture video from a webcam. Basic, untested example command:

ffmpeg -f video4linux2 -i /dev/video0 -f alsa -i hw:0 output.mkv 

You mention:

h.264 is a famous video codec (decoder)

H.264 is a standard, not a codec. FFmpeg has a decoder named h264. It can be confusing. See What is a Codec (e.g. DivX?), and how does it differ from a File Format (e.g. MPG)?