树莓派4基于FFmpeg libavcodec实现毫秒级RGBA图像压缩优化求助
树莓派4上RGBA图像快速压缩至JPEG级别需求与优化问题
我们需要在树莓派4上通过C/C++程序,将生成的1024×2048的32位RGBA原始图像(大小8MB)压缩至JPEG级别(200-500KB),且压缩耗时需控制在数毫秒内,否则对我们毫无意义。
我们尝试使用FFmpeg开发库及C/C++代码进行编码,但修改官方编码示例后,得到的耗时结果无法接受。
修改后的帧创建代码
for (i = 0; i < 25; i++) { #ifdef MEASURE_TIME auto start_time = std::chrono::high_resolution_clock::now(); std::cout << "START Encoding frame...\n"; #endif fflush(stdout); ret = av_frame_make_writable(frame); if (ret < 0) exit(1); // 将32位RGBA图像转换为YUV像素格式 for (y = 0; y < c->height; y++) { for (x = 0; x < c->width; x++) { int imageIndexY = y * frame->linesize[0] + x; uint32_t rgbPixel = ((uint32_t*)OutputDataImage)[imageIndexY]; double Y, U, V; uint8_t R = rgbPixel << 24; uint8_t G = rgbPixel << 16; uint8_t B = rgbPixel << 8; YUVfromRGB(Y, U, V, (double)R, (double)G, (double)B); frame->data[0][imageIndexY] = (uint8_t)Y; if (y % 2 == 0 && x % 2 == 0) { int imageIndexU = (y / 2) * frame->linesize[1] + (x / 2); int imageIndexV = (y / 2) * frame->linesize[2] + (x / 2); frame->data[1][imageIndexU] = (uint8_t)U; frame->data[2][imageIndexV] = (uint8_t)Y; } } } frame->pts = i; /* 编码图像 */ encode(c, frame, pkt, f); #ifdef MEASURE_TIME auto end_time = std::chrono::high_resolution_clock::now(); auto time = end_time - start_time; std::cout << "FINISHED Encoding frame in: " << time / std::chrono::milliseconds(1) << "ms.\n"; #endif }
函数前序关键代码
codec_name = "mpeg4"; codec = avcodec_find_encoder_by_name(codec_name); c = avcodec_alloc_context3(codec); c->bit_rate = 1000000; c->width = IMAGE_WIDTH; c->height = IMAGE_HEIGHT; c->gop_size = 1; c->max_b_frames = 1; c->pix_fmt = AV_PIX_FMT_YUV420P;
其中IMAGE_WIDTH和IMAGE_HEIGHT分别为1024和2048。
树莓派4上的运行结果
START Encoding frame... Send frame 0 FINISHED Encoding frame in: 40ms. START Encoding frame... Send frame 1 Write packet 0 (size=11329) FINISHED Encoding frame in: 60ms. START Encoding frame... Send frame 2 Write packet 1 (size=11329) FINISHED Encoding frame in: 58ms.
其余相关代码
void RGBfromYUV(double& R, double& G, double& B, double Y, double U, double V) { Y -= 16; U -= 128; V -= 128; R = 1.164 * Y + 1.596 * V; G = 1.164 * Y - 0.392 * U - 0.813 * V; B = 1.164 * Y + 2.017 * U; }
求助需求
我在编码及Codec使用方面完全是新手,现寻求最优实现方案,希望将压缩耗时降至数毫秒,同时不确定当前选择的Codec或像素格式是否合适,盼得到专业指导。
内容的提问来源于stack exchange,提问作者Jerry Switalski
相关产品推荐
相关产品推荐

