jpg转nv12
代碼主要用到了libyuv庫和libjpeg庫。
編譯:
g++ demo.cpp -lyuv -ljpeg -o demoubuntu下測試(需安裝ffmpeg,width以及height按實際情況填寫):?
ffplay -i test.yuv -pixel_format nv12 -video_size widthxheightdemo:?
#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 = 0using 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; }總結
- 上一篇: ubuntu 下WebStorm 无法输
- 下一篇: 智慧新泰时空大数据与云平台_《智慧城市时