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>
... | ... |
@@ -1522,6 +1522,7 @@ EXAMPLE_LIST=" |
1522 | 1522 |
scaling_video_example |
1523 | 1523 |
transcode_aac_example |
1524 | 1524 |
transcoding_example |
1525 |
+ vaapi_encode_example |
|
1525 | 1526 |
" |
1526 | 1527 |
|
1527 | 1528 |
EXTERNAL_AUTODETECT_LIBRARY_LIST=" |
... | ... |
@@ -3300,6 +3301,7 @@ resampling_audio_example_deps="avutil swresample" |
3300 | 3300 |
scaling_video_example_deps="avutil swscale" |
3301 | 3301 |
transcode_aac_example_deps="avcodec avformat swresample" |
3302 | 3302 |
transcoding_example_deps="avfilter avcodec avformat avutil" |
3303 |
+vaapi_encode_example_deps="avcodec avutil" |
|
3303 | 3304 |
|
3304 | 3305 |
# EXTRALIBS_LIST |
3305 | 3306 |
cpu_init_extralibs="pthreads_extralibs" |
... | ... |
@@ -19,6 +19,7 @@ EXAMPLES-$(CONFIG_RESAMPLING_AUDIO_EXAMPLE) += resampling_audio |
19 | 19 |
EXAMPLES-$(CONFIG_SCALING_VIDEO_EXAMPLE) += scaling_video |
20 | 20 |
EXAMPLES-$(CONFIG_TRANSCODE_AAC_EXAMPLE) += transcode_aac |
21 | 21 |
EXAMPLES-$(CONFIG_TRANSCODING_EXAMPLE) += transcoding |
22 |
+EXAMPLES-$(CONFIG_VAAPI_ENCODE_EXAMPLE) += vaapi_encode |
|
22 | 23 |
|
23 | 24 |
EXAMPLES := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)$(EXESUF)) |
24 | 25 |
EXAMPLES_G := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)_g$(EXESUF)) |
25 | 26 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,224 @@ |
0 |
+/* |
|
1 |
+ * Video Acceleration API (video encoding) encode sample |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @file |
|
22 |
+ * Intel VAAPI-accelerated encoding example. |
|
23 |
+ * |
|
24 |
+ * @example vaapi_encode.c |
|
25 |
+ * This example shows how to do VAAPI-accelerated encoding. now only support NV12 |
|
26 |
+ * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264 |
|
27 |
+ * |
|
28 |
+ */ |
|
29 |
+ |
|
30 |
+#include <stdio.h> |
|
31 |
+#include <string.h> |
|
32 |
+#include <errno.h> |
|
33 |
+ |
|
34 |
+#include <libavcodec/avcodec.h> |
|
35 |
+#include <libavutil/pixdesc.h> |
|
36 |
+#include <libavutil/hwcontext.h> |
|
37 |
+ |
|
38 |
+static int width, height; |
|
39 |
+static AVBufferRef *hw_device_ctx = NULL; |
|
40 |
+ |
|
41 |
+static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx) |
|
42 |
+{ |
|
43 |
+ AVBufferRef *hw_frames_ref; |
|
44 |
+ AVHWFramesContext *frames_ctx = NULL; |
|
45 |
+ int err = 0; |
|
46 |
+ |
|
47 |
+ if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) { |
|
48 |
+ fprintf(stderr, "Failed to create VAAPI frame context.\n"); |
|
49 |
+ return -1; |
|
50 |
+ } |
|
51 |
+ frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data); |
|
52 |
+ frames_ctx->format = AV_PIX_FMT_VAAPI; |
|
53 |
+ frames_ctx->sw_format = AV_PIX_FMT_NV12; |
|
54 |
+ frames_ctx->width = width; |
|
55 |
+ frames_ctx->height = height; |
|
56 |
+ frames_ctx->initial_pool_size = 20; |
|
57 |
+ if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) { |
|
58 |
+ fprintf(stderr, "Failed to initialize VAAPI frame context." |
|
59 |
+ "Error code: %s\n",av_err2str(err)); |
|
60 |
+ return err; |
|
61 |
+ } |
|
62 |
+ ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref); |
|
63 |
+ if (!ctx->hw_frames_ctx) |
|
64 |
+ err = AVERROR(ENOMEM); |
|
65 |
+ |
|
66 |
+ av_buffer_unref(&hw_frames_ref); |
|
67 |
+ return err; |
|
68 |
+} |
|
69 |
+ |
|
70 |
+static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout) |
|
71 |
+{ |
|
72 |
+ int ret = 0; |
|
73 |
+ AVPacket enc_pkt; |
|
74 |
+ |
|
75 |
+ av_init_packet(&enc_pkt); |
|
76 |
+ enc_pkt.data = NULL; |
|
77 |
+ enc_pkt.size = 0; |
|
78 |
+ |
|
79 |
+ if ((ret = avcodec_send_frame(avctx, frame)) < 0) { |
|
80 |
+ fprintf(stderr, "Error code: %s\n", av_err2str(ret)); |
|
81 |
+ goto end; |
|
82 |
+ } |
|
83 |
+ while (1) { |
|
84 |
+ ret = avcodec_receive_packet(avctx, &enc_pkt); |
|
85 |
+ if (ret) |
|
86 |
+ break; |
|
87 |
+ |
|
88 |
+ enc_pkt.stream_index = 0; |
|
89 |
+ ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout); |
|
90 |
+ av_packet_unref(&enc_pkt); |
|
91 |
+ } |
|
92 |
+ |
|
93 |
+end: |
|
94 |
+ ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1); |
|
95 |
+ return ret; |
|
96 |
+} |
|
97 |
+ |
|
98 |
+int main(int argc, char *argv[]) |
|
99 |
+{ |
|
100 |
+ int size, err; |
|
101 |
+ FILE *fin = NULL, *fout = NULL; |
|
102 |
+ AVFrame *sw_frame = NULL, *hw_frame = NULL; |
|
103 |
+ AVCodecContext *avctx = NULL; |
|
104 |
+ AVCodec *codec = NULL; |
|
105 |
+ const char *enc_name = "h264_vaapi"; |
|
106 |
+ |
|
107 |
+ if (argc < 5) { |
|
108 |
+ fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]); |
|
109 |
+ return -1; |
|
110 |
+ } |
|
111 |
+ |
|
112 |
+ width = atoi(argv[1]); |
|
113 |
+ height = atoi(argv[2]); |
|
114 |
+ size = width * height; |
|
115 |
+ |
|
116 |
+ if (!(fin = fopen(argv[3], "r"))) { |
|
117 |
+ fprintf(stderr, "Fail to open input file : %s\n", strerror(errno)); |
|
118 |
+ return -1; |
|
119 |
+ } |
|
120 |
+ if (!(fout = fopen(argv[4], "w+b"))) { |
|
121 |
+ fprintf(stderr, "Fail to open output file : %s\n", strerror(errno)); |
|
122 |
+ err = -1; |
|
123 |
+ goto close; |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ avcodec_register_all(); |
|
127 |
+ |
|
128 |
+ err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, |
|
129 |
+ NULL, NULL, 0); |
|
130 |
+ if (err < 0) { |
|
131 |
+ fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err)); |
|
132 |
+ goto close; |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ if (!(codec = avcodec_find_encoder_by_name(enc_name))) { |
|
136 |
+ fprintf(stderr, "Could not find encoder.\n"); |
|
137 |
+ err = -1; |
|
138 |
+ goto close; |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ if (!(avctx = avcodec_alloc_context3(codec))) { |
|
142 |
+ err = AVERROR(ENOMEM); |
|
143 |
+ goto close; |
|
144 |
+ } |
|
145 |
+ |
|
146 |
+ avctx->width = width; |
|
147 |
+ avctx->height = height; |
|
148 |
+ avctx->time_base = (AVRational){1, 25}; |
|
149 |
+ avctx->framerate = (AVRational){25, 1}; |
|
150 |
+ avctx->sample_aspect_ratio = (AVRational){1, 1}; |
|
151 |
+ avctx->pix_fmt = AV_PIX_FMT_VAAPI; |
|
152 |
+ |
|
153 |
+ /* set hw_frames_ctx for encoder's AVCodecContext */ |
|
154 |
+ if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) { |
|
155 |
+ fprintf(stderr, "Failed to set hwframe context.\n"); |
|
156 |
+ goto close; |
|
157 |
+ } |
|
158 |
+ |
|
159 |
+ if ((err = avcodec_open2(avctx, codec, NULL)) < 0) { |
|
160 |
+ fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err)); |
|
161 |
+ goto close; |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ while (1) { |
|
165 |
+ if (!(sw_frame = av_frame_alloc())) { |
|
166 |
+ err = AVERROR(ENOMEM); |
|
167 |
+ goto close; |
|
168 |
+ } |
|
169 |
+ /* read data into software frame, and transfer them into hw frame */ |
|
170 |
+ sw_frame->width = width; |
|
171 |
+ sw_frame->height = height; |
|
172 |
+ sw_frame->format = AV_PIX_FMT_NV12; |
|
173 |
+ if ((err = av_frame_get_buffer(sw_frame, 32)) < 0) |
|
174 |
+ goto close; |
|
175 |
+ if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0) |
|
176 |
+ break; |
|
177 |
+ if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0) |
|
178 |
+ break; |
|
179 |
+ |
|
180 |
+ if (!(hw_frame = av_frame_alloc())) { |
|
181 |
+ err = AVERROR(ENOMEM); |
|
182 |
+ goto close; |
|
183 |
+ } |
|
184 |
+ if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) { |
|
185 |
+ fprintf(stderr, "Error code: %s.\n", av_err2str(err)); |
|
186 |
+ goto close; |
|
187 |
+ } |
|
188 |
+ if (!hw_frame->hw_frames_ctx) { |
|
189 |
+ err = AVERROR(ENOMEM); |
|
190 |
+ goto close; |
|
191 |
+ } |
|
192 |
+ if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) { |
|
193 |
+ fprintf(stderr, "Error while transferring frame data to surface." |
|
194 |
+ "Error code: %s.\n", av_err2str(err)); |
|
195 |
+ goto close; |
|
196 |
+ } |
|
197 |
+ |
|
198 |
+ if ((err = (encode_write(avctx, hw_frame, fout))) < 0) { |
|
199 |
+ fprintf(stderr, "Failed to encode.\n"); |
|
200 |
+ goto close; |
|
201 |
+ } |
|
202 |
+ av_frame_free(&hw_frame); |
|
203 |
+ av_frame_free(&sw_frame); |
|
204 |
+ } |
|
205 |
+ |
|
206 |
+ /* flush encoder */ |
|
207 |
+ err = encode_write(avctx, NULL, fout); |
|
208 |
+ if (err == AVERROR_EOF) |
|
209 |
+ err = 0; |
|
210 |
+ |
|
211 |
+close: |
|
212 |
+ if (fin) |
|
213 |
+ fclose(fin); |
|
214 |
+ if (fout) |
|
215 |
+ fclose(fout); |
|
216 |
+ av_frame_free(&sw_frame); |
|
217 |
+ av_frame_free(&hw_frame); |
|
218 |
+ if (avctx) |
|
219 |
+ avcodec_free_context(&avctx); |
|
220 |
+ av_buffer_unref(&hw_device_ctx); |
|
221 |
+ |
|
222 |
+ return err; |
|
223 |
+} |