AwsError Encode()

in h264_encoder_core/src/h264_encoder.cpp [262:302]


  AwsError Encode(const uint8_t * img_data, H264EncoderResult & res)
  {
    if (nullptr == img_data) {
      return AWS_ERR_NULL_PARAM;
    }

    AVPacket pkt;

    /* Convert from image encoding to YUV420P */
    const uint8_t * buf_in[4] = {img_data, nullptr, nullptr, nullptr};
    sws_scale(convert_ctx_, (const uint8_t * const *)buf_in, &src_stride_, 0, src_height_,
              pic_in_->data, pic_in_->linesize);

    /* Encode */
    av_init_packet(&pkt);
    pkt.data = nullptr;  // packet data will be allocated by the encoder
    pkt.size = 0;

    int got_output = 0;

    int ret = avcodec_encode_video2(param_, &pkt, pic_in_, &got_output);
    ++pic_in_->pts;
    if (ret < 0) {
      AWS_LOGSTREAM_ERROR(__func__,
                          "Error encoding frame (avcodec_encode_video2() returned: " << ret << ")");
      return AWS_ERR_FAILURE;
    }
    if (got_output) {
      res.Reset();
      res.frame_data.insert(res.frame_data.end(), pkt.data, pkt.data + pkt.size);
      res.frame_pts = frame_duration_ * (0 <= pkt.pts ? pkt.pts : 0);
      res.frame_dts = frame_duration_ * (0 <= pkt.dts ? pkt.dts : 0);
      res.frame_duration = frame_duration_;
      res.key_frame = pkt.flags & AV_PKT_FLAG_KEY;
      av_free_packet(&pkt);

      return AWS_ERR_OK;
    }

    return AWS_ERR_EMPTY;
  }