FFMPEG:在 h264rgb/libx264rgb 错误中解码视频

FFMPEG: Decode video in h264rgb/libx264rgb error

我做了一个小程序,用 ffmpeg 在 h264rgb 编解码器中对原始图像进行编码。 我使用这个编解码器是因为我需要编码无损 rgb 图像(经典的 h264 编解码器不可能)。

但是现在,我遇到了一个问题。我无法解码使用 ffmpeg 生成的视频。我为此做了第二个小程序,但是当我到达 avcodec_decode_video2() 函数时出现段错误。

我正确地完成了所有的初始化。我没有忘记 avcodec_register_all() 和 av_init_packet() 函数。

初始化代码如下:

  _c = NULL;
  _frame_nb = 0;

  // Register all formats and codecs
  #pragma omp critical
  {
      avcodec_register_all();
  }

  _pkt = new AVPacket;
  av_init_packet(_pkt);  // a defaut de pouvoir ecrire : pkt = av_packet_alloc();

  if(!_pkt)
      exit(1);

    _codec = avcodec_find_encoder_by_name("libx264rgb");

  if (!_codec) {
      fprintf(stderr, "codec not found\n");
      exit(1);
  }

  _c = avcodec_alloc_context3(_codec);
  if (!_c) {
      fprintf(stderr, "Could not allocate video codec context\n");
      exit(1);
  }

  _c->debug = true;
  _c->pix_fmt =  (AVPixelFormat)AV_PIX_FMT_RGB24;
  _c->width = this->_headerCam[this->_currentCam]->iNbCol;
  _c->height = this->_headerCam[this->_currentCam]->iNbLine;

  _picture = av_frame_alloc();
  if (!_picture) {
      fprintf(stderr, "Could not allocate video _picture\n");
      exit(1);
  }

  _tmp_picture = av_frame_alloc();
  if (!_tmp_picture) {
      fprintf(stderr, "Could not allocate video _tmp_picture\n");
      exit(1);
  }


      _tmp_picture->format = (AVPixelFormat)AV_PIX_FMT_RGB24;
      _tmp_picture->width = this->_headerCam[this->_currentCam]->iNbCol;
      _tmp_picture->height = this->_headerCam[this->_currentCam]->iNbLine;
      _tmp_picture->linesize[0] = this->_headerCam[this->_currentCam]->iNbCol;

  /* open it */
  if (avcodec_open2(_c, _codec, NULL) < 0) {
      fprintf(stderr, "could not open codec\n");
      exit(1);
  }

解码函数:

        _pkt->data = NULL;    // packet data will be allocated by the encoder
        _pkt->size = 0;

        unsigned char * inbuf;
        inbuf = (uint8_t*)av_malloc(w*h*3);

        //! read img size
        int size_img;
        fread(&size_img, sizeof(int), 1, this->_pFile);
        _pkt->size = fread(inbuf, 1, size_img, this->_pFile);

        _pkt->data = (unsigned char*)inbuf;

        if(_pkt->size)
        {
            len = avcodec_decode_video2(_c, _tmp_picture, &got_picture, _pkt);
            ...
        }

有什么想法吗?

+评论说的,而不是 inbuf = (uint8_t*)av_malloc(w*h*3); 你应该使用这个:

int buffer_size = av_image_get_buffer_size((AVPixelFormat)AV_PIX_FMT_RGB24, w, h, 1);
inbuf = (uint8_t*)av_malloc(buffer_size);

由于对齐的可移植性等问题。

同时修正这一行 _tmp_picture->linesize[0] = ... 有了这个:
_tmp_picture->linesize[0] = av_image_get_linesize((AVPixelFormat)AV_PIX_FMT_RGB24, w, 0);

希望对您有所帮助。

好吧,我终于找到了答案的开头: 我用了avcodec_find_encoder_by_name()而不是avcodec_find_decoder_by_name()...如果我想解码数据当然更好...

但是现在,我遇到了 _codec = avcodec_find_decoder_by_name("libx264rgb"); 找不到编解码器的问题。但它适用于 _codec = avcodec_find_encoder_by_name("libx264rgb"); 当我编码我的视频时。

编辑:使用 _codec = avcodec_find_decoder(AV_CODEC_ID_H264); 而不是 _codec = avcodec_find_decoder_by_name("libx264rgb");,它能够解码我的视频。这很奇怪但没关系。现在,我希望我能得到无损图像。

EDIT2:现在,一切正常!我使用 AV_CODEC_ID_H264 解码器获得了无损解码图像。总而言之,如果您想解码使用 libx264rgb 编码的视频,您可以使用经典的 AV_CODEC_ID_H264 编解码器。