0

Mac OS X 10.9.5 で libx264 を使用して、MacBook Pro の内蔵 FaceTime HD カメラからの入力画像をリアルタイムで H.264 ビデオ ストリームにエンコードしようとしています。

以下は私が取ったステップです:

  1. AVFoundation API (AVCaptureDevice クラスなど) を使用して、15fps でカメラから 1280x720 32BGRA 画像を取得します。
  2. libswscale を使用して、画像を 320x180 YUV420P 形式に変換します。
  3. libx264 を使用して、画像を H.264 ビデオ ストリーム (ベースライン プロファイル) にエンコードします。

カメラから画像を取得するたびに上記の手順を適用します。エンコーダーはエンコード状態を追跡し、利用可能になったときに NAL ユニットを生成すると信じています。

エンコーダーに入力画像を提供しながらエンコードされたフレームを取得したかったので、30 フレーム (2 秒) ごとにエンコーダーをフラッシュする (x264_encoder_delayed_frames() を呼び出す) ことにしました。

ただし、エンコードを再開すると、しばらくするとエンコーダーが停止します (x264_encoder_encode() は決して戻りません)。フラッシュする前のフレーム数を変更してみましたが、状況は変わりませんでした。

以下に関連するコードを示します(画像キャプチャのコードは問題ないようなので省略します)。

私が間違っているかもしれないことを指摘できますか?

x264_t *encoder;
x264_param_t param;

// Will be called only first time.
int initEncoder() {
  int ret;

  if ((ret = x264_param_default_preset(&param, "medium", NULL)) < 0) {
    return ret;
  }

  param.i_csp = X264_CSP_I420;
  param.i_width  = 320;
  param.i_height = 180;
  param.b_vfr_input = 0;
  param.b_repeat_headers = 1;
  param.b_annexb = 1;

  if ((ret = x264_param_apply_profile(&param, "baseline")) < 0) {
    return ret;
  }

  encoder = x264_encoder_open(&param);
  if (!encoder) {
    return AVERROR_UNKNOWN;
  }

  return 0;
}

// Will be called from encodeFrame() defined below.
int convertImage(const enum AVPixelFormat srcFmt, const int srcW, const int srcH, const uint8_t *srcData, const enum AVPixelFormat dstFmt, const int dstW, const int dstH, x264_image_t *dstData) {
  struct SwsContext *sws_ctx;
  int ret;
  int src_linesize[4];
  uint8_t *src_data[4];

  sws_ctx = sws_getContext(srcW, srcH, srcFmt,
                       dstW, dstH, dstFmt,
                       SWS_BILINEAR, NULL, NULL, NULL);

  if (!sws_ctx) {
    return AVERROR_UNKNOWN;
  }

  if ((ret = av_image_fill_linesizes(src_linesize, srcFmt, srcW)) < 0) {
    sws_freeContext(sws_ctx);
    return ret;
  }

  if ((ret = av_image_fill_pointers(src_data, srcFmt, srcH, (uint8_t *) srcData, src_linesize)) < 0) {
    sws_freeContext(sws_ctx);
    return ret;
  }

  sws_scale(sws_ctx, (const uint8_t * const*)src_data, src_linesize, 0, srcH, dstData->plane, dstData->i_stride);
  sws_freeContext(sws_ctx);
  return 0;
}

// Will be called for each frame.
int encodeFrame(const uint8_t *data, const int width, const int height) {
  int ret;
  x264_picture_t pic;
  x264_picture_t pic_out;
  x264_nal_t *nal;
  int i_nal;

  if ((ret = x264_picture_alloc(&pic, param.i_csp, param.i_width, param.i_height)) < 0) {
    return ret;
  }

  if ((ret = convertImage(AV_PIX_FMT_RGB32, width, height, data, AV_PIX_FMT_YUV420P, 320, 180, &pic.img)) < 0) {
    x264_picture_clean(&pic);
    return ret;
  }

  if ((ret = x264_encoder_encode(encoder, &nal, &i_nal, &pic, &pic_out)) < 0) {
    x264_picture_clean(&pic);
    return ret;
  }

  if(ret) {
    for (int i = 0; i < i_nal; i++) {
      printNAL(nal + i);
    }
  }

  x264_picture_clean(&pic);
  return 0;
}

// Will be called every 30 frames.
int flushEncoder() {
  int ret;
  x264_nal_t *nal;
  int i_nal;
  x264_picture_t pic_out;

  /* Flush delayed frames */
  while (x264_encoder_delayed_frames(encoder)) {
    if ((ret = x264_encoder_encode(encoder, &nal, &i_nal, NULL, &pic_out)) < 0) {
      return ret;
    }

    if (ret) {
      for (int j = 0; j < i_nal; j++) {
        printNAL(nal + j);
      }
    }
  }
}
4

1 に答える 1

1

すべてのフレームの後に遅延フレームをフラッシュするのではなく、入力フレームがなくなったとき、つまりエンコードの最後に一度だけフラッシュする必要があります。

于 2015-04-21T18:07:57.613 に答える