【FFMPEG】YUV420P格式图像叠加,拼接

YUV420P格式图像叠加,拼接

网上的很多代码都有问题!!!
话不多说上代码:

需要用到库:
  • ffmpeg,用于解码h264,也可以直接用yuv图像进行拼接,只要记住yuv420p的数据存放格式和采样格式就不会出错。
  • sdl2,用于实时显示
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
#define snprintf _snprintf
//Windows
extern "C"
{
   
#include "libavcodec/avcodec.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libswscale/swscale.h"
#include "libavutil/avutil.h"
#include "libavutil/imgutils.h"
#include "SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
   
#endif
#include <libavfilter/avfiltergraph.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include "SDL.h"

#ifdef __cplusplus
};
#endif
#endif


#define ENABLE_SDL	1
#define ENABLE_YUV	0

#define REFRESH_EVENT  (SDL_USEREVENT + 1)
#define BREAK_EVENT  (SDL_USEREVENT + 2)

#define FRAMEWITH 640
#define FRAMEHEIGTH 480

#define BGWIDTH 800	
#define BGHEIGHT 600




static int thread_exit = 0;


int refresh_video1(void* opaque) {
   
	thread_exit = 0;
	while (!thread_exit) {
   
		SDL_Event event;
		event.type = REFRESH_EVENT;
		SDL_PushEvent(&event);
		SDL_Delay(40);
	}
	thread_exit = 0;
	SDL_Event event;
	event.type = BREAK_EVENT;
	SDL_PushEvent(&event);

	return 0;
}



int main(int argc, char* argv[])
{
   
	AVFormatContext* pFormatCtx;
	int             i, videoindex;
	AVCodecContext* pCodecCtx;
	AVCodec* pCodec;
	AVFrame* pFrame, * pFrameYUV, * pDstFrame;
	uint8_t* out_buffer;
	AVPacket* packet;
	int y_size;
	int ret, got_picture;
	struct SwsContext* img_convert_ctx = nullptr;
	//输入文件路径
	//char filepath[] = "test_640x480.mp4";
	char filepath[] = "C:/Users/Li/Desktop/test.h264";

	int frame_cnt;

	using namespace std;

	av_register_all();
	avformat_network_init();
	pFormatCtx = avformat_alloc_context();

	if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {
   
		printf("Couldn't open input stream.\n");
		return -1;
	}
	if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
   
		printf("Couldn't find stream information.\n");
		return -1;
	}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值