aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJun Zhao <jun.zhao@intel.com>2017-11-06 14:45:27 +0800
committerMark Thompson <sw@jkqxz.net>2017-11-28 22:14:07 +0000
commit23db3a1ae6d1be3438aec73c4dc91185d7958300 (patch)
tree84d8cd202f0f2c17403db1af4a5f4814fbb70e85
parentc5f3e85792d5f98d7974e8ca55cd30f80fe0c77c (diff)
downloadffmpeg-23db3a1ae6d1be3438aec73c4dc91185d7958300.tar.gz
examples: Add a VA-API encode example.
Supports only raw NV12 input. Example use: ./vaapi_encode 1920 1080 test.yuv test.h264 Signed-off-by: Jun Zhao <jun.zhao@intel.com> Signed-off-by: Liu, Kaixuan <kaixuan.liu@intel.com> Signed-off-by: Mark Thompson <sw@jkqxz.net>
-rwxr-xr-xconfigure2
-rw-r--r--doc/examples/Makefile1
-rw-r--r--doc/examples/vaapi_encode.c224
3 files changed, 227 insertions, 0 deletions
diff --git a/configure b/configure
index 28902d41f5..11eeeaa609 100755
--- a/configure
+++ b/configure
@@ -1522,6 +1522,7 @@ EXAMPLE_LIST="
scaling_video_example
transcode_aac_example
transcoding_example
+ vaapi_encode_example
"
EXTERNAL_AUTODETECT_LIBRARY_LIST="
@@ -3300,6 +3301,7 @@ resampling_audio_example_deps="avutil swresample"
scaling_video_example_deps="avutil swscale"
transcode_aac_example_deps="avcodec avformat swresample"
transcoding_example_deps="avfilter avcodec avformat avutil"
+vaapi_encode_example_deps="avcodec avutil"
# EXTRALIBS_LIST
cpu_init_extralibs="pthreads_extralibs"
diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index 58afd71b85..da5af36532 100644
--- a/doc/examples/Makefile
+++ b/doc/examples/Makefile
@@ -19,6 +19,7 @@ EXAMPLES-$(CONFIG_RESAMPLING_AUDIO_EXAMPLE) += resampling_audio
EXAMPLES-$(CONFIG_SCALING_VIDEO_EXAMPLE) += scaling_video
EXAMPLES-$(CONFIG_TRANSCODE_AAC_EXAMPLE) += transcode_aac
EXAMPLES-$(CONFIG_TRANSCODING_EXAMPLE) += transcoding
+EXAMPLES-$(CONFIG_VAAPI_ENCODE_EXAMPLE) += vaapi_encode
EXAMPLES := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)$(EXESUF))
EXAMPLES_G := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)_g$(EXESUF))
diff --git a/doc/examples/vaapi_encode.c b/doc/examples/vaapi_encode.c
new file mode 100644
index 0000000000..866b03d58c
--- /dev/null
+++ b/doc/examples/vaapi_encode.c
@@ -0,0 +1,224 @@
+/*
+ * Video Acceleration API (video encoding) encode sample
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Intel VAAPI-accelerated encoding example.
+ *
+ * @example vaapi_encode.c
+ * This example shows how to do VAAPI-accelerated encoding. now only support NV12
+ * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <libavcodec/avcodec.h>
+#include <libavutil/pixdesc.h>
+#include <libavutil/hwcontext.h>
+
+static int width, height;
+static AVBufferRef *hw_device_ctx = NULL;
+
+static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
+{
+ AVBufferRef *hw_frames_ref;
+ AVHWFramesContext *frames_ctx = NULL;
+ int err = 0;
+
+ if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
+ fprintf(stderr, "Failed to create VAAPI frame context.\n");
+ return -1;
+ }
+ frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
+ frames_ctx->format = AV_PIX_FMT_VAAPI;
+ frames_ctx->sw_format = AV_PIX_FMT_NV12;
+ frames_ctx->width = width;
+ frames_ctx->height = height;
+ frames_ctx->initial_pool_size = 20;
+ if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
+ fprintf(stderr, "Failed to initialize VAAPI frame context."
+ "Error code: %s\n",av_err2str(err));
+ return err;
+ }
+ ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
+ if (!ctx->hw_frames_ctx)
+ err = AVERROR(ENOMEM);
+
+ av_buffer_unref(&hw_frames_ref);
+ return err;
+}
+
+static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
+{
+ int ret = 0;
+ AVPacket enc_pkt;
+
+ av_init_packet(&enc_pkt);
+ enc_pkt.data = NULL;
+ enc_pkt.size = 0;
+
+ if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
+ fprintf(stderr, "Error code: %s\n", av_err2str(ret));
+ goto end;
+ }
+ while (1) {
+ ret = avcodec_receive_packet(avctx, &enc_pkt);
+ if (ret)
+ break;
+
+ enc_pkt.stream_index = 0;
+ ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout);
+ av_packet_unref(&enc_pkt);
+ }
+
+end:
+ ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ int size, err;
+ FILE *fin = NULL, *fout = NULL;
+ AVFrame *sw_frame = NULL, *hw_frame = NULL;
+ AVCodecContext *avctx = NULL;
+ AVCodec *codec = NULL;
+ const char *enc_name = "h264_vaapi";
+
+ if (argc < 5) {
+ fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
+ return -1;
+ }
+
+ width = atoi(argv[1]);
+ height = atoi(argv[2]);
+ size = width * height;
+
+ if (!(fin = fopen(argv[3], "r"))) {
+ fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
+ return -1;
+ }
+ if (!(fout = fopen(argv[4], "w+b"))) {
+ fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
+ err = -1;
+ goto close;
+ }
+
+ avcodec_register_all();
+
+ err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
+ NULL, NULL, 0);
+ if (err < 0) {
+ fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
+ goto close;
+ }
+
+ if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
+ fprintf(stderr, "Could not find encoder.\n");
+ err = -1;
+ goto close;
+ }
+
+ if (!(avctx = avcodec_alloc_context3(codec))) {
+ err = AVERROR(ENOMEM);
+ goto close;
+ }
+
+ avctx->width = width;
+ avctx->height = height;
+ avctx->time_base = (AVRational){1, 25};
+ avctx->framerate = (AVRational){25, 1};
+ avctx->sample_aspect_ratio = (AVRational){1, 1};
+ avctx->pix_fmt = AV_PIX_FMT_VAAPI;
+
+ /* set hw_frames_ctx for encoder's AVCodecContext */
+ if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
+ fprintf(stderr, "Failed to set hwframe context.\n");
+ goto close;
+ }
+
+ if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
+ fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
+ goto close;
+ }
+
+ while (1) {
+ if (!(sw_frame = av_frame_alloc())) {
+ err = AVERROR(ENOMEM);
+ goto close;
+ }
+ /* read data into software frame, and transfer them into hw frame */
+ sw_frame->width = width;
+ sw_frame->height = height;
+ sw_frame->format = AV_PIX_FMT_NV12;
+ if ((err = av_frame_get_buffer(sw_frame, 32)) < 0)
+ goto close;
+ if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
+ break;
+ if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
+ break;
+
+ if (!(hw_frame = av_frame_alloc())) {
+ err = AVERROR(ENOMEM);
+ goto close;
+ }
+ if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
+ fprintf(stderr, "Error code: %s.\n", av_err2str(err));
+ goto close;
+ }
+ if (!hw_frame->hw_frames_ctx) {
+ err = AVERROR(ENOMEM);
+ goto close;
+ }
+ if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
+ fprintf(stderr, "Error while transferring frame data to surface."
+ "Error code: %s.\n", av_err2str(err));
+ goto close;
+ }
+
+ if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
+ fprintf(stderr, "Failed to encode.\n");
+ goto close;
+ }
+ av_frame_free(&hw_frame);
+ av_frame_free(&sw_frame);
+ }
+
+ /* flush encoder */
+ err = encode_write(avctx, NULL, fout);
+ if (err == AVERROR_EOF)
+ err = 0;
+
+close:
+ if (fin)
+ fclose(fin);
+ if (fout)
+ fclose(fout);
+ av_frame_free(&sw_frame);
+ av_frame_free(&hw_frame);
+ if (avctx)
+ avcodec_free_context(&avctx);
+ av_buffer_unref(&hw_device_ctx);
+
+ return err;
+}