简体   繁体   English

ffmpeg mono8 / rgb24图像到yuv420p到x264的转换

[英]ffmpeg mono8/rgb24 images to yuv420p to x264 conversion

this is probably a trivial question but I'm going crazy with this (ffmpeg) framework. 这可能是一个琐碎的问题,但是我对这个(ffmpeg)框架感到疯狂。 Based on this post ( sws_scale YUV --> RGB distorted image ) (and much more searches) I've written the following code to take a char* pointer to a buffer image (Mono8 or RGB24) and I convert to YUV420P to encode in x264. 基于这篇文章( sws_scale YUV-> RGB失真图像 )(以及更多搜索),我编写了以下代码,将char *指针带到缓冲区图像(Mono8或RGB24),然后转换为YUV420P进行编码x264。 I have to create a streaming of this images between two PC. 我必须在两台PC之间创建此图像的流。

This is the code: 这是代码:

bool compressImageX264(Frame *f, int size, char* image){
 codec = avcodec_find_encoder(AV_CODEC_ID_H264);
 if (!codec) {
  std::cout << "Codec not found" << std::endl;
  return false;
 }
 c = avcodec_alloc_context3(codec);
 if (!c) {
  std::cout << "Could not allocate video codec context" << std::endl;
  return false;
 }
 av_opt_set(c->priv_data, "preset", "veryfast", 0);
 av_opt_set(c->priv_data, "tune", "zerolatency", 0);
 c->width = std::stoi(f->width);
 c->height = std::stoi(f->height);
 c->gop_size = 10;
 c->max_b_frames = 1;
 EDIT: c->pix_fmt = AV_PIX_FMT_YUV420P;
 /* open it */
 if (avcodec_open2(c, codec, NULL) < 0) {
  std::cout << "Could not open codec" << std::endl;
  return false;
 }

 AVFrame *avFrameRGB = av_frame_alloc();
 if (!avFrameRGB) {
  std::cout << "Could not allocate video frame" << std::endl;
  return false;
 }
 avFrameRGB->format = std::stoi(f->channels) > 1 ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_GRAY8;
 avFrameRGB->width = c->width;
 avFrameRGB->height = c->height;

 int ret = av_image_alloc(
   avFrameRGB->data, avFrameRGB->linesize,
   avFrameRGB->width, avFrameRGB->height, AVPixelFormat(avFrameRGB->format),
   32);
 if (ret < 0) {
  std::cout << "Could not allocate raw picture buffer" << std::endl;
  return false;
 }

 uint8_t *p = avFrameRGB->data[0];
 for (int i = 0; i < size; i++){
   *p++ = image[i];
 }
 AVFrame* avFrameYUV = av_frame_alloc();
 avFrameYUV->format = AV_PIX_FMT_YUV420P;
 avFrameYUV->width = c->width;
 avFrameYUV->height = c->height;
 ret = av_image_alloc(
    avFrameYUV->data, avFrameYUV->linesize,
    avFrameYUV->width, avFrameYUV->height, AVPixelFormat(avFrameYUV->format),
    32);

 SwsContext *img_convert_ctx = sws_getContext(c->width, c->height, AVPixelFormat(avFrameRGB->format),
   c->width, c->height, AV_PIX_FMT_YUV420P,
   SWS_FAST_BILINEAR, NULL, NULL, NULL);
 ret = sws_scale(img_convert_ctx, 
   avFrameRGB->data, avFrameRGB->linesize,
   0, c->height, 
   avFrameYUV->data, avFrameYUV->linesize);
 sws_freeContext(img_convert_ctx);

 AVPacket pkt;
 av_init_packet(&pkt);
 pkt.data = NULL; // packet data will be allocated by the encoder
 pkt.size = 0;

 avFrameYUV->pts = frameCount; frameCount++; //GLOBAL VARIABLE
 int got_output;
 ret = avcodec_encode_video2(c, &pkt, avFrameYUV, &got_output);
 if (ret < 0) { //<-- Where the code broke
   std::cout << "Error encoding frame" << std::endl;
   return false;
 }
 if (got_output) {
    std::cout << "Write frame " << frameCount - 1 << "(size = " << pkt.size << ")" << std::endl;
    char* buffer = new char[pkt.size];
    for (int i = 0; i < pkt.size; i++){
      buffer[i] = pkt.data[i];
    }
    f->buffer = buffer;
    f->size = std::to_string(pkt.size);
    f->compression = std::string("x264");
    av_free_packet(&pkt);
  }
  return true;
}

I know that maybe this is a lot inefficient but for now I'm worried to make it work. 我知道这可能效率很低,但是现在我担心要使其工作。 I failed when I call avcodec_encode_video2. 致电avcodec_encode_video2时失败。 On console it's printed this message: "Input picture width (640) is greater than stride (320)". 在控制台上,它显示以下消息:“输入图片宽度(640)大于步幅(320)”。 I think that conversion is the assassin. 我认为conversion依是刺客。 But I don't fully know the parameters meanings Thanks for all your help. 但是我不完全了解参数含义,谢谢您的所有帮助。

EDIT: Ok, I have founded the "first" error. 编辑:好的,我已经建立了“第一个”错误。 Now the conversion work properly and avcodec_encode_video2 return 0. The problem now is that got_output is always equal zero and on the console nothing is available. 现在,转换可以正常工作,并且avcodec_encode_video2返回0。现在的问题是,got_output始终等于零,并且在控制台上没有任何可用。

I've found this solution: Encoding raw YUV420P to h264 with AVCodec on iOS 我找到了这个解决方案: 在iOS上使用AVCodec将原始YUV420P编码为h264

Can I call encode in sync way? 我可以以同步方式调用编码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM