Jooyoung Lee
기록 일지
Jooyoung Lee
  • 분류 전체보기 (27)
    • 임베디드 (9)
    • 딥러닝 (11)
      • ML (3)
      • Notions (4)
      • AI math (4)
      • 강의 리뷰 (0)
    • 논문 리뷰 (1)
    • 알고리즘 스터디 (1)
    • 컴퓨터 비전 (5)

블로그 메뉴

  • About me,
GitHub Contribution
No contributions on May 19th.No contributions on May 26th.No contributions on June 2nd.No contributions on June 9th.No contributions on June 16th.No contributions on June 23rd.No contributions on June 30th.No contributions on July 7th.No contributions on July 14th.No contributions on July 21st.No contributions on July 28th.No contributions on August 4th.No contributions on August 11th.No contributions on August 18th.No contributions on August 25th.No contributions on September 1st.No contributions on September 8th.No contributions on September 15th.No contributions on September 22nd.No contributions on September 29th.No contributions on October 6th.No contributions on October 13th.No contributions on October 20th.No contributions on October 27th.No contributions on November 3rd.No contributions on November 10th.No contributions on November 17th.No contributions on November 24th.No contributions on December 1st.No contributions on December 8th.No contributions on December 15th.No contributions on December 22nd.No contributions on December 29th.No contributions on January 5th.No contributions on January 12th.No contributions on January 19th.No contributions on January 26th.No contributions on February 2nd.No contributions on February 9th.No contributions on February 16th.No contributions on February 23rd.No contributions on March 2nd.No contributions on March 9th.No contributions on March 16th.No contributions on March 23rd.No contributions on March 30th.No contributions on April 6th.No contributions on April 13th.No contributions on April 20th.No contributions on April 27th.No contributions on May 4th.No contributions on May 11th.No contributions on May 18th.No contributions on May 20th.No contributions on May 27th.No contributions on June 3rd.No contributions on June 10th.No contributions on June 17th.No contributions on June 24th.No contributions on July 1st.No contributions on July 8th.No contributions on July 15th.No contributions on July 22nd.No contributions on July 29th.No contributions on August 5th.No contributions on August 12th.No contributions on August 19th.No contributions on August 26th.No contributions on September 2nd.No contributions on September 9th.No contributions on September 16th.No contributions on September 23rd.No contributions on September 30th.No contributions on October 7th.No contributions on October 14th.No contributions on October 21st.No contributions on October 28th.No contributions on November 4th.No contributions on November 11th.No contributions on November 18th.No contributions on November 25th.No contributions on December 2nd.No contributions on December 9th.No contributions on December 16th.No contributions on December 23rd.No contributions on December 30th.No contributions on January 6th.No contributions on January 13th.No contributions on January 20th.No contributions on January 27th.No contributions on February 3rd.No contributions on February 10th.No contributions on February 17th.No contributions on February 24th.No contributions on March 3rd.No contributions on March 10th.No contributions on March 17th.No contributions on March 24th.No contributions on March 31st.No contributions on April 7th.No contributions on April 14th.No contributions on April 21st.No contributions on April 28th.No contributions on May 5th.No contributions on May 12th.No contributions on May 19th.No contributions on May 21st.No contributions on May 28th.No contributions on June 4th.No contributions on June 11th.No contributions on June 18th.No contributions on June 25th.No contributions on July 2nd.No contributions on July 9th.No contributions on July 16th.No contributions on July 23rd.No contributions on July 30th.No contributions on August 6th.No contributions on August 13th.No contributions on August 20th.No contributions on August 27th.No contributions on September 3rd.No contributions on September 10th.No contributions on September 17th.No contributions on September 24th.No contributions on October 1st.No contributions on October 8th.No contributions on October 15th.No contributions on October 22nd.No contributions on October 29th.No contributions on November 5th.No contributions on November 12th.No contributions on November 19th.No contributions on November 26th.No contributions on December 3rd.No contributions on December 10th.No contributions on December 17th.No contributions on December 24th.No contributions on December 31st.No contributions on January 7th.No contributions on January 14th.No contributions on January 21st.No contributions on January 28th.No contributions on February 4th.No contributions on February 11th.No contributions on February 18th.No contributions on February 25th.No contributions on March 4th.No contributions on March 11th.No contributions on March 18th.No contributions on March 25th.No contributions on April 1st.No contributions on April 8th.No contributions on April 15th.No contributions on April 22nd.No contributions on April 29th.No contributions on May 6th.No contributions on May 13th.No contributions on May 22nd.No contributions on May 29th.No contributions on June 5th.No contributions on June 12th.No contributions on June 19th.No contributions on June 26th.No contributions on July 3rd.No contributions on July 10th.No contributions on July 17th.No contributions on July 24th.No contributions on July 31st.No contributions on August 7th.No contributions on August 14th.No contributions on August 21st.No contributions on August 28th.No contributions on September 4th.No contributions on September 11th.No contributions on September 18th.No contributions on September 25th.No contributions on October 2nd.No contributions on October 9th.No contributions on October 16th.No contributions on October 23rd.No contributions on October 30th.No contributions on November 6th.No contributions on November 13th.No contributions on November 20th.No contributions on November 27th.No contributions on December 4th.No contributions on December 11th.No contributions on December 18th.No contributions on December 25th.No contributions on January 1st.No contributions on January 8th.No contributions on January 15th.No contributions on January 22nd.No contributions on January 29th.No contributions on February 5th.No contributions on February 12th.No contributions on February 19th.No contributions on February 26th.No contributions on March 5th.No contributions on March 12th.No contributions on March 19th.No contributions on March 26th.No contributions on April 2nd.No contributions on April 9th.No contributions on April 16th.No contributions on April 23rd.No contributions on April 30th.No contributions on May 7th.No contributions on May 14th.No contributions on May 23rd.No contributions on May 30th.No contributions on June 6th.No contributions on June 13th.No contributions on June 20th.No contributions on June 27th.No contributions on July 4th.No contributions on July 11th.No contributions on July 18th.No contributions on July 25th.No contributions on August 1st.No contributions on August 8th.No contributions on August 15th.No contributions on August 22nd.No contributions on August 29th.No contributions on September 5th.No contributions on September 12th.No contributions on September 19th.No contributions on September 26th.No contributions on October 3rd.No contributions on October 10th.No contributions on October 17th.No contributions on October 24th.No contributions on October 31st.No contributions on November 7th.No contributions on November 14th.No contributions on November 21st.No contributions on November 28th.No contributions on December 5th.No contributions on December 12th.No contributions on December 19th.No contributions on December 26th.No contributions on January 2nd.No contributions on January 9th.No contributions on January 16th.No contributions on January 23rd.No contributions on January 30th.No contributions on February 6th.No contributions on February 13th.No contributions on February 20th.No contributions on February 27th.No contributions on March 6th.No contributions on March 13th.No contributions on March 20th.No contributions on March 27th.No contributions on April 3rd.No contributions on April 10th.No contributions on April 17th.No contributions on April 24th.No contributions on May 1st.No contributions on May 8th.No contributions on May 15th.No contributions on May 24th.No contributions on May 31st.No contributions on June 7th.No contributions on June 14th.No contributions on June 21st.No contributions on June 28th.No contributions on July 5th.No contributions on July 12th.No contributions on July 19th.No contributions on July 26th.No contributions on August 2nd.No contributions on August 9th.No contributions on August 16th.No contributions on August 23rd.No contributions on August 30th.No contributions on September 6th.No contributions on September 13th.No contributions on September 20th.No contributions on September 27th.No contributions on October 4th.No contributions on October 11th.No contributions on October 18th.No contributions on October 25th.No contributions on November 1st.No contributions on November 8th.No contributions on November 15th.No contributions on November 22nd.No contributions on November 29th.No contributions on December 6th.No contributions on December 13th.No contributions on December 20th.No contributions on December 27th.No contributions on January 3rd.No contributions on January 10th.No contributions on January 17th.No contributions on January 24th.No contributions on January 31st.No contributions on February 7th.No contributions on February 14th.No contributions on February 21st.No contributions on February 28th.No contributions on March 7th.No contributions on March 14th.No contributions on March 21st.No contributions on March 28th.No contributions on April 4th.No contributions on April 11th.No contributions on April 18th.No contributions on April 25th.No contributions on May 2nd.No contributions on May 9th.No contributions on May 16th.No contributions on May 25th.No contributions on June 1st.No contributions on June 8th.No contributions on June 15th.No contributions on June 22nd.No contributions on June 29th.No contributions on July 6th.No contributions on July 13th.No contributions on July 20th.No contributions on July 27th.No contributions on August 3rd.No contributions on August 10th.No contributions on August 17th.No contributions on August 24th.No contributions on August 31st.No contributions on September 7th.No contributions on September 14th.No contributions on September 21st.No contributions on September 28th.No contributions on October 5th.No contributions on October 12th.No contributions on October 19th.No contributions on October 26th.No contributions on November 2nd.No contributions on November 9th.No contributions on November 16th.No contributions on November 23rd.2 contributions on November 30th.No contributions on December 7th.No contributions on December 14th.No contributions on December 21st.No contributions on December 28th.No contributions on January 4th.No contributions on January 11th.No contributions on January 18th.No contributions on January 25th.No contributions on February 1st.No contributions on February 8th.No contributions on February 15th.No contributions on February 22nd.No contributions on March 1st.No contributions on March 8th.No contributions on March 15th.No contributions on March 22nd.No contributions on March 29th.No contributions on April 5th.No contributions on April 12th.No contributions on April 19th.No contributions on April 26th.No contributions on May 3rd.No contributions on May 10th.No contributions on May 17th.
Contribution Graph
Day of Week May May June Jun July Jul August Aug September Sep October Oct November Nov December Dec January Jan February Feb March Mar April Apr May May
Sunday Sun
Monday Mon
Tuesday Tue
Wednesday Wed
Thursday Thu
Friday Fri
Saturday Sat
Learn how we count contributions
Less
No contributions.
Low contributions.
Medium-low contributions.
Medium-high contributions.
High contributions.
More
Jooyoung Lee
임베디드

V4L2 (Video 4 Linux 2)

V4L2 (Video 4 Linux 2)
임베디드

V4L2 (Video 4 Linux 2)

2025. 2. 26. 18:35
Audio/Video Device에 접근할 수 있도록 하는 일종의 Kernel API

 

V4L2 Flow

 

동작 순서

  • Application에서 ioctl을 이용하여 카메라 그동에 필요한 명령을 순차적으로 전송
  • 명렁들의 순서가 바뀌거나 누락되는 경우 V4L2 드라이버에서 오류 발생 확률 증가

  • QBUF, DQBUFC, STREAMON은 일반적으로 반복 수행 되어야 영상을 얻을 수 있다.
  • 위 동작을 반복하지 않는다면 1 프레임만 얻을 수 있다.
    • 대부분 반복문을 사용해서 지속성을 유지 시킴

QEURYCAP (Query Capability)

  • 수행되어야 하는 명령 중 가장 간단한 것 중 하나로 연결된 디바이스 이름
  • 수행 가능한 동작 등 장치의 정보를 사용자 영역에 알려주는 역할
  • 정보들을 v4l2_capability 구조체에 저장

S_FMT_VID_CAP

  • 비디오 디바이스를 실질적으로 동작 시키기 전, 디바이스 몇 부분을 설정하는 명령어
  • 이미지 포맷, 해상도 등등

V4L2 기본 개념 정리

  • V4L2 애플리케이션 (User-Space)
    • /dev/videoX 장치를 open() 해서 접근
    • ioctl() 호출을 통해 드라이버와 상호작용
    • ex) GStreamer, OpenCV, FFmpeg 등에서 활용
  • V4L2 드라이버 (Kernel-Space)
    • 카메라 센서 드라이버, ISP 드라이버, MIPI CSI/USB 드라이버가 포함됨.
    • V4L2 프레임워크를 사용하여 VIDIOC_* 명령을 처리
  • V4L2 장치 (Hardware)
    • 카메라 센서, 비디오 캡처 카드, SoC의 ISP(이미지 프로세서) 등 물리적인 하드웨어
User-Space:
  - open("/dev/video0")
  - ioctl(VIDIOC_QUERYCAP)   -> 드라이버에게 장치 정보 요청
  - ioctl(VIDIOC_S_FMT)      -> 해상도, 포맷 설정
  - ioctl(VIDIOC_REQBUFS)    -> 버퍼 요청
  - ioctl(VIDIOC_QBUF)       -> 버퍼를 큐에 넣음
  - ioctl(VIDIOC_STREAMON)   -> 스트리밍 시작
  - ioctl(VIDIOC_DQBUF)      -> 캡처된 프레임 가져오기

Kernel-Space (드라이버):
  - 위 요청을 받아 처리 후 응답

 

V4L2 장치 파일 : /dev/videoX

  • /dev/video0, /dev/video1 … 같은 형태로 존재
  • ls /dev/video* → 연결된 V4L2 장치 확인 가능

V4L2 드라이버 정보 조회 : VIDIOC_QUERYCAP

  • ioctl(fd, VIDIOC_QUERYCAP, &cap) 호출 시 드라이버가 지원하는 기능 확인 가능
struct v4l2_capability cap;
ioctl(fd, VIDIOC_QUERYCAP, &cap);
printf("Driver: %s\n", cap.driver);
printf("Device: %s\n", cap.card);
printf("Bus: %s\n", cap.bus_info);
printf("Capabilities: %08x\n", cap.capabilities);

 

V4L2 포맷 설정 : VIDIOC_S_FMT

  • 영상 크기나 픽셀 포맷을 설정
  • 포맷은 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_NV12, V4L2_PIX_FMT_MJPEG 등 이 있음
struct v4l2_format fmt;
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 1920;
fmt.fmt.pix.height = 1080;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;

ioctl(fd, VIDIOC_S_FMT, &fmt);

 

V4L2 버퍼 관리 : Memory buffering

V4L2에서는 프레임을 저장할 버퍼를 미리 할당하고, 장치와 사용자 공간이 이를 공유

버퍼 할당 (VIDIOC_REQBUFS)

  • V4L2_MEMORY_MMAP또는 V4L2_MEMORY_DMABUF(DMA) 방식 사용 가능
struct v4l2_requestbuffers req;
memset(&req, 0, sizeof(req));
req.count = 4;  // 버퍼 개수
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;

ioctl(fd, VIDIOC_REQBUFS, &req);
  • V4L2_MEMORY_DMABUF로 하면 CPU 개입 없이 DMA로 직접 전송 가능.

버퍼 매핑 (VIDIOC_QUERYBUF, mmap())

  • 드라이버에서 할당된 버퍼를 mmap()을 이용해 사용자 공간으로 매핑
struct v4l2_buffer buf;
buf.index = 0;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;

ioctl(fd, VIDIOC_QUERYBUF, &buf);
void *buffer_start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);

 

V4L2 CAPTURE FLOW

V4L2에서는 Queue와 Dequeue 방식으로 프레임 처리

VIDIOC_QBUF

  • 비디오 버퍼를 장치에 등록
struct v4l2_buffer buf;

buf.index = 0;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;

ioctl(fd, VIDIOC_QBUF, &buf);

 

VIDIOC_STREAMON

  • 장치에서 캡처된 데이터를 가져옴
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(fd, VIDIOC_STREAMON, $type)

 

VIDIOC_DQBUF

  • 장치에서 캡처된 데이터를 가져옴
struct v4l2_buffer buf;

buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;

ioctl(fd, VIDIOC_DQBUF, &buf);
process_frame(buffer_start);  // 프레임 데이터 처리
ioctl(fd, VIDIOC_QBUF, &buf);  // 다시 큐에 넣기
  • 위와 같은 방식으로 프레임이 큐 → 캡처 → 디큐 → 다시 큐 방식으로 순회

V4L2 기본 플로우

1. open("/dev/video0")  // 장치 오픈
2. ioctl(VIDIOC_QUERYCAP)  // 장치 정보 확인
3. ioctl(VIDIOC_S_FMT)  // 포맷 설정
4. ioctl(VIDIOC_REQBUFS)  // 버퍼 요청
5. ioctl(VIDIOC_QUERYBUF)  // 버퍼 정보 확인
6. mmap()  // 버퍼 매핑
7. ioctl(VIDIOC_QBUF)  // 버퍼 큐잉
8. ioctl(VIDIOC_STREAMON)  // 스트리밍 시작
9. ioctl(VIDIOC_DQBUF)  // 프레임 가져오기
10. ioctl(VIDIOC_QBUF)  // 버퍼 다시 큐에 넣기
11. ioctl(VIDIOC_STREAMOFF)  // 스트리밍 종료
12. close(fd)  // 장치 닫기

'임베디드' 카테고리의 다른 글

ARM assembly  (0) 2025.02.20
Basic #1  (0) 2024.08.26
SocketCAN  (0) 2024.04.22
ALSA  (0) 2023.12.21
Automotive SPICE (업데이트 中)  (0) 2023.06.20
  • V4L2 기본 개념 정리
  • V4L2 장치 파일 : /dev/videoX
  • V4L2 드라이버 정보 조회 : VIDIOC_QUERYCAP
  • V4L2 포맷 설정 : VIDIOC_S_FMT
  • V4L2 버퍼 관리 : Memory buffering
  • V4L2 CAPTURE FLOW
'임베디드' 카테고리의 다른 글
  • ARM assembly
  • Basic #1
  • SocketCAN
  • ALSA
Jooyoung Lee
Jooyoung Lee
Embedded software engineer

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.