V4L2采集图像并在LCD上显示的图像格式问题
发布网友
发布时间:2022-04-11 20:06
我来回答
共3个回答
懂视网
时间:2022-04-12 00:27
结合前面的 采集 v4l2 视频, 使用 live555, 通过 rtsp 发布实时流. capture.h, capture.cpp, vcompress.h, vcompress.cpp 需要参考前面几片文章. 这里仅仅贴出 v4l2_x264_service.cpp [cpp] view plaincopy #includestdio.h #includestdlib.h #includeunistd
结合前面的 采集 v4l2 视频, 使用 live555, 通过 rtsp 发布实时流. capture.h, capture.cpp, vcompress.h, vcompress.cpp 需要参考前面几片文章. 这里仅仅贴出 v4l2_x264_service.cpp
[cpp] view
plaincopy
-
#include
-
#include
-
#include
-
#include
-
-
#include
-
#include
-
#include
-
-
#include
-
#include
-
-
#include "capture.h"
-
#include "vcompress.h"
-
-
static UsageEnvironment *_env = 0;
-
-
#define SINK_PORT 3030
-
-
#define VIDEO_WIDTH 320
-
#define VIDEO_HEIGHT 240
-
#define FRAME_PER_SEC 5.0
-
-
pid_t gettid()
-
{
-
return syscall(SYS_gettid);
-
}
-
-
-
// 使用 webcam + x264
-
class WebcamFrameSource : public FramedSource
-
{
-
void *mp_capture, *mp_compress; // v4l2 + x264 encoder
-
int m_started;
-
void *mp_token;
-
-
public:
-
WebcamFrameSource (UsageEnvironment &env)
-
: FramedSource(env)
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
mp_capture = capture_open("/dev/video0", VIDEO_WIDTH, VIDEO_HEIGHT, PIX_FMT_YUV420P);
-
if (!mp_capture) {
-
fprintf(stderr, "%s: open /dev/video0 err
", __func__);
-
exit(-1);
-
}
-
-
mp_compress = vc_open(VIDEO_WIDTH, VIDEO_HEIGHT, FRAME_PER_SEC);
-
if (!mp_compress) {
-
fprintf(stderr, "%s: open x264 err
", __func__);
-
exit(-1);
-
}
-
-
m_started = 0;
-
mp_token = 0;
-
}
-
-
~WebcamFrameSource ()
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
-
if (m_started) {
-
envir().taskScheduler().unscheduleDelayedTask(mp_token);
-
}
-
-
if (mp_compress)
-
vc_close(mp_compress);
-
if (mp_capture)
-
capture_close(mp_capture);
-
}
-
-
protected:
-
virtual void doGetNextFrame ()
-
{
-
if (m_started) return;
-
m_started = 1;
-
-
// 根据 fps, 计算等待时间
-
double delay = 1000.0 / FRAME_PER_SEC;
-
int to_delay = delay * 1000; // us
-
-
mp_token = envir().taskScheduler().scheduleDelayedTask(to_delay,
-
getNextFrame, this);
-
}
[cpp] view
plaincopy
-
virtual unsigned maxFrameSize() const // 这个很重要, 如果不设置, 可能导致 getNextFrame() 出现 fMaxSize 小于实际编码帧的情况, 导致图像不完整
[cpp] view
plaincopy
-
{ return 100*1024; }
[cpp] view
plaincopy
-
private:
-
static void getNextFrame (void *ptr)
-
{
-
((WebcamFrameSource*)ptr)->getNextFrame1();
-
}
-
-
void getNextFrame1 ()
-
{
-
// capture:
-
Picture pic;
-
if (capture_get_picture(mp_capture, &pic) < 0) {
-
fprintf(stderr, "==== %s: capture_get_picture err
", __func__);
-
m_started = 0;
-
return;
-
}
-
-
// compress
-
const void *outbuf;
-
int outlen;
-
if (vc_compress(mp_compress, pic.data, pic.stride, &outbuf, &outlen) < 0) {
-
fprintf(stderr, "==== %s: vc_compress err
", __func__);
-
m_started = 0;
-
return;
-
}
-
-
int64_t pts, dts;
-
int key;
-
vc_get_last_frame_info(mp_compress, &key, &pts, &dts);
-
-
// save outbuf
-
gettimeofday(&fPresentationTime, 0);
-
fFrameSize = outlen;
-
if (fFrameSize > fMaxSize) {
-
fNumTruncatedBytes = fFrameSize - fMaxSize;
-
fFrameSize = fMaxSize;
-
}
-
else {
-
fNumTruncatedBytes = 0;
-
}
-
-
memmove(fTo, outbuf, fFrameSize);
-
-
// notify
-
afterGetting(this);
-
-
m_started = 0;
-
}
-
};
-
-
class WebcamOndemandMediaSubsession : public OnDemandServerMediaSubsession
-
{
-
public:
-
static WebcamOndemandMediaSubsession *createNew (UsageEnvironment &env, FramedSource *source)
-
{
-
return new WebcamOndemandMediaSubsession(env, source);
-
}
-
-
protected:
-
WebcamOndemandMediaSubsession (UsageEnvironment &env, FramedSource *source)
-
: OnDemandServerMediaSubsession(env, True) // reuse the first source
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
mp_source = source;
-
mp_sdp_line = 0;
-
}
-
-
~WebcamOndemandMediaSubsession ()
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
if (mp_sdp_line) free(mp_sdp_line);
-
}
-
-
private:
-
static void afterPlayingDummy (void *ptr)
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
// ok
-
WebcamOndemandMediaSubsession *This = (WebcamOndemandMediaSubsession*)ptr;
-
This->m_done = 0xff;
-
}
-
-
static void chkForAuxSDPLine (void *ptr)
-
{
-
WebcamOndemandMediaSubsession *This = (WebcamOndemandMediaSubsession *)ptr;
-
This->chkForAuxSDPLine1();
-
}
-
-
void chkForAuxSDPLine1 ()
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
if (mp_dummy_rtpsink->auxSDPLine())
-
m_done = 0xff;
-
else {
-
int delay = 100*1000; // 100ms
-
nextTask() = envir().taskScheduler().scheduleDelayedTask(delay,
-
chkForAuxSDPLine, this);
-
}
-
}
-
-
protected:
-
virtual const char *getAuxSDPLine (RTPSink *sink, FramedSource *source)
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
if (mp_sdp_line) return mp_sdp_line;
-
-
mp_dummy_rtpsink = sink;
-
mp_dummy_rtpsink->startPlaying(*source, 0, 0);
-
//mp_dummy_rtpsink->startPlaying(*source, afterPlayingDummy, this);
-
chkForAuxSDPLine(this);
-
m_done = 0;
-
envir().taskScheduler().doEventLoop(&m_done);
-
mp_sdp_line = strdup(mp_dummy_rtpsink->auxSDPLine());
-
mp_dummy_rtpsink->stopPlaying();
-
-
return mp_sdp_line;
-
}
-
-
virtual RTPSink *createNewRTPSink(Groupsock *rtpsock, unsigned char type, FramedSource *source)
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
return H264VideoRTPSink::createNew(envir(), rtpsock, type);
-
}
-
-
virtual FramedSource *createNewStreamSource (unsigned sid, unsigned &bitrate)
-
{
-
fprintf(stderr, "[%d] %s .... calling
", gettid(), __func__);
-
bitrate = 500;
-
return H264VideoStreamFramer::createNew(envir(), new WebcamFrameSource(envir()));
-
}
-
-
private:
-
FramedSource *mp_source; // 对应 WebcamFrameSource
-
char *mp_sdp_line;
-
RTPSink *mp_dummy_rtpsink;
-
char m_done;
-
};
-
-
static void test_task (void *ptr)
-
{
-
fprintf(stderr, "test: task ....
");
-
_env->taskScheduler().scheduleDelayedTask(100000, test_task, 0);
-
}
-
-
static void test (UsageEnvironment &env)
-
{
-
fprintf(stderr, "test: begin...
");
-
-
char done = 0;
-
int delay = 100 * 1000;
-
env.taskScheduler().scheduleDelayedTask(delay, test_task, 0);
-
env.taskScheduler().doEventLoop(&done);
-
-
fprintf(stderr, "test: end..
");
-
}
-
-
int main (int argc, char **argv)
-
{
-
// env
-
TaskScheduler *scheduler = BasicTaskScheduler::createNew();
-
_env = BasicUsageEnvironment::createNew(*scheduler);
-
-
// test
-
//test(*_env);
-
-
// rtsp server
-
RTSPServer *rtspServer = RTSPServer::createNew(*_env, 8554);
-
if (!rtspServer) {
-
fprintf(stderr, "ERR: create RTSPServer err
");
-
::exit(-1);
-
}
-
-
// add live stream
-
do {
-
WebcamFrameSource *webcam_source = 0;
-
-
ServerMediaSession *sms = ServerMediaSession::createNew(*_env, "webcam", 0, "Session from /dev/video0");
-
sms->addSubsession(WebcamOndemandMediaSubsession::createNew(*_env, webcam_source));
-
rtspServer->addServerMediaSession(sms);
-
-
char *url = rtspServer->rtspURL(sms);
-
*_env << "using url "" << url << ""
";
-
delete [] url;
-
} while (0);
-
-
// run loop
-
_env->taskScheduler().doEventLoop();
-
-
return 1;
-
}
需要 live555 + libavcodec + libswscale + libx264, client 使用 vlc, mplayer, quicktime, .....
热心网友
时间:2022-04-11 21:35
摄像头采集的视频数据是JPEG格式,需要通过libjpeg库进行解压,解压后得到的事RGB 24位的,在转换成RGB16位即可。 具体转换是将RGB24位的前8位右移3位,中间8位右移2位,最后8位右移3位,就得到RGB16位数据了。。。具体代码:
unsigned short RGB888toRGB565(unsigned char red, unsigned char green, unsigned char blue)
{
unsigned short B = (blue >> 3) & 0x001F;
unsigned short G = ((green >> 2) << 5) & 0x07E0;
unsigned short R = ((red >> 3) << 11) & 0xF800;
return (unsigned short) (R | G | B);
}
热心网友
时间:2022-04-11 22:53
图3图像采集卡在采集数据时的工作时序
其中,VSYNV为场同步输出脉冲,HREF为也用于SRAM和FLASH的片选和读写控制,同时还负责LCD的显示控制。
2
软件设计
V4L2采集图像并在LCD上显示的图像格式问题
摄像头采集的视频数据是JPEG格式,需要通过libjpeg库进行解压,解压后得到的事RGB 24位的,在转换成RGB16位即可。 具体转换是将RGB24位的前8位右移3位,中间8位右移2位,最后8位右移3位,就得到RGB16位数据了。。。具体代码:unsigned short RGB888toRGB565(unsigned char red, unsigned char gree...
Linux v4l2图片采集问题
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;ioctl (fd, VIDIOC_S_FMT, &fmt);指定了采集图像的格式为YUYV格式。要像采集成JPEG图像,得查询一下摄像头是否有相应功能,如果没有相应功能即使将fmt设置为jpeg最终采集到的还是yuyv格式。yuyv可以转换为bm...
嵌入式软件设计中,关于液晶屏横屏改竖屏显示的问题
液晶显示横屏还是竖的,主要是往framebuffer中的帧图像像素点存的位置换下就行,按道理V4L2的应用程序可以控制LCD的显示,寄存器肯定不需要修改的,驱动也没必要