大家好,又见面了,我是你们的朋友全栈君。
代码主要用到了libyuv库和libjpeg库。
编译:
g++ demo.cpp -lyuv -ljpeg -o demo
ubuntu下测试(需安装ffmpeg,width以及height按实际情况填写):
ffplay -i test.yuv -pixel_format nv12 -video_size widthxheight
demo:
#include <stdio.h>
#include <stdlib.h>
#include <libyuv.h>
#define align_buffer_page_end(var, size) \
uint8_t* var##_mem = \
reinterpret_cast<uint8_t*>(malloc(((size) + 4095 + 63) & ~4095)); \
uint8_t* var = reinterpret_cast<uint8_t*>( \
(intptr_t)(var##_mem + (((size) + 4095 + 63) & ~4095) - (size)) & ~63)
#define free_aligned_buffer_page_end(var) \
free(var##_mem); \
var = 0
using namespace libyuv;
int main(int argc, char *argv[])
{
int32_t width = 0, height = 0;
FILE *dst;
FILE *ori = fopen("test.jpg", "rb+");
fseek(ori, 0, SEEK_END);
uint32_t data_size = ftell(ori);
fseek(ori, 0, SEEK_SET);
uint8_t *data = (uint8_t *)calloc(1, data_size);
fread(data, data_size, 1, ori);
fclose(ori);
int32_t ret = MJPGSize(data, data_size, &width, &height);
int32_t half_width = (width + 1) / 2;
int32_t half_height = (height + 1) / 2;
align_buffer_page_end(dst_y, width * height);
align_buffer_page_end(dst_uv, half_width * half_height * 2);
ret = MJPGToNV12(data, data_size, dst_y, width, dst_uv, half_width * 2, width, height, width, height);
if (0 == ret) {
printf("change to nv12 success\n");
} else {
printf("change to nv12 failed\n");
goto errChange;
}
dst = fopen("test.yuv", "wb+");
fwrite(dst_y, width * height, 1, dst);
fwrite(dst_uv, half_width * half_height * 2, 1, dst);
fclose(dst);
errChange:
free(data);
free_aligned_buffer_page_end(dst_y);
free_aligned_buffer_page_end(dst_uv);
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/192969.html原文链接:https://javaforall.cn