将摄像头原始RGB数据流编码成H.264文件

查阅了很多资料,都是将YUV文件编码成H.264视频,几乎没有找到用摄像头数据直接存储为H.264文件的。

以下是我的实现方法,借鉴了网上的一些做法,整合而成。需要先安装ffshow

extern "C"

{

#include "avcodec.h"

#include "avformat.h"

#include "avio.h"

#include "avutil.h"

#include "common.h"

#include "intfloat_readwrite.h"

#include "inttypes.h"

#include "log.h"

#include "mathematics.h"

#include "mem.h"

#include "rational.h"

#include "stdint.h"

#include "swscale.h"

}


#pragma comment(lib,"avcodec.lib")

#pragma comment(lib,"avformat.lib")

#pragma comment(lib,"avutil.lib")

#define RGBTOYUV(B, G, R, Y, U, V) \

Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;\

U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;\

V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;


size_t BufSize = 9437184;

BYTE *pStream = new BYTE[BufSize];

BYTE* pBmp24;

int CALLBACK SnapThreadCallback(BYTE *pBuffer) //摄像头显示回调函数

{


pBmp24 = CameraISP(pBuffer);

if(pBmp24)

{

if (bVideo)

{

memcpy(pStream, pBmp24, BufSize);

}

CameraDisplayRGB24(pBmp24);

}

return TRUE;

}


void CXXDlg::OnBtnVideo() 

{

    // TODO: Add your control notification handler code here

    //CWinThread *pVedioStart;

    if (m_VideoMode != VIDEOMODE_PLAY)

    {

        GetDlgItem(IDC_BTN_VIDEO)->SetWindowText("停止录像");    

        AfxBeginThread((AFX_THREADPROC)VIDEO, this, THREAD_PRIORITY_ABOVE_NORMAL, 0, NULL);

        m_VideoMode = VIDEOMODE_PLAY;        

    }

    else

    {

        m_VideoMode = VIDEOMODE_STOP;

        GetDlgItem(IDC_BTN_VIDEO)->SetWindowText("开始录像");        

    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
H.264是一种视频编码标准,其中的帧内预测是其中一种压缩技术。在MATLAB中实现H.264编码可以使用一些开源库和工具,例如JM、x264等。以下是一个示例代码,实现了H.264帧内预测编码: ```matlab % Read input video vidObj = VideoReader('input_video.mp4'); % Create output video object outputVideo = VideoWriter('output_video.mp4','MPEG-4'); outputVideo.FrameRate = vidObj.FrameRate; open(outputVideo); % Set H.264 encoder parameters params = struct('Profile','High','Level','4.0','BitRate',5000000,'FrameRate',vidObj.FrameRate); % Loop through each video frame while hasFrame(vidObj) % Read current frame frame = readFrame(vidObj); % Convert to YCbCr color space YCbCr = rgb2ycbcr(frame); % Split YCbCr into separate components Y = YCbCr(:,:,1); Cb = YCbCr(:,:,2); Cr = YCbCr(:,:,3); % Perform frame intra prediction on Y component Y_pred = intra_prediction(Y); % Combine YCbCr components YCbCr_pred = cat(3,Y_pred,Cb,Cr); % Convert back to RGB color space frame_pred = ycbcr2rgb(YCbCr_pred); % Write encoded frame to output video writeVideo(outputVideo,frame_pred); end % Close output video object close(outputVideo); function Y_pred = intra_prediction(Y) % Block size (8x8) block_size = 8; % Pad input image [rows,cols] = size(Y); Y_pad = padarray(Y,[block_size-1 block_size-1],'replicate','post'); % Perform intra prediction on each block for r = 1:block_size:rows for c = 1:block_size:cols % Get current block block = Y_pad(r:r+block_size-1,c:c+block_size-1); % Calculate prediction mode mode = calculate_mode(block); % Perform prediction switch mode case 0 % DC mode pred = mean(block(:)); case 1 % Horizontal mode pred = repmat(block(1,:),[block_size 1]); case 2 % Vertical mode pred = repmat(block(:,1),[1 block_size]); case 3 % Diagonal mode pred = diag(block); pred = repmat(pred(:),[1 block_size]) + repmat(pred(:)',[block_size 1]); pred = pred/(block_size+1); otherwise error('Invalid prediction mode'); end % Subtract prediction from current block Y_pred(r:r+block_size-1,c:c+block_size-1) = block - pred; end end end function mode = calculate_mode(block) % Calculate sum of absolute differences (SAD) for each mode SAD = zeros(4,1); SAD(1) = sum(abs(block(:)-mean(block(:)))); SAD(2) = sum(abs(block(1,:)-mean(block(:)))); SAD(3) = sum(abs(block(:,1)-mean(block(:)))); SAD(4) = sum(abs(diag(block)-mean(block(:)))); % Find mode with minimum SAD [~,mode] = min(SAD); mode = mode-1; end ``` 这段代码实现了一个简单的帧内预测编码器,基于直接模式选择方法(mode selection),并且使用直流、水平、垂直和对角线预测模式进行预测。在实际应用中,需要使用更复杂的编码器和预测算法,以达到更好的压缩效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值