2 * Video Acceleration API (video encoding) encode sample
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Intel VAAPI-accelerated encoding example.
25 * @example vaapi_encode.c
26 * This example shows how to do VAAPI-accelerated encoding. now only support NV12
27 * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264
35 #include <libavcodec/avcodec.h>
36 #include <libavutil/pixdesc.h>
37 #include <libavutil/hwcontext.h>
39 static int width, height;
40 static AVBufferRef *hw_device_ctx = NULL;
42 static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
44 AVBufferRef *hw_frames_ref;
45 AVHWFramesContext *frames_ctx = NULL;
48 if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
49 fprintf(stderr, "Failed to create VAAPI frame context.\n");
52 frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
53 frames_ctx->format = AV_PIX_FMT_VAAPI;
54 frames_ctx->sw_format = AV_PIX_FMT_NV12;
55 frames_ctx->width = width;
56 frames_ctx->height = height;
57 frames_ctx->initial_pool_size = 20;
58 if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
59 fprintf(stderr, "Failed to initialize VAAPI frame context."
60 "Error code: %s\n",av_err2str(err));
61 av_buffer_unref(&hw_frames_ref);
64 ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
65 if (!ctx->hw_frames_ctx)
66 err = AVERROR(ENOMEM);
68 av_buffer_unref(&hw_frames_ref);
72 static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
77 av_init_packet(&enc_pkt);
81 if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
82 fprintf(stderr, "Error code: %s\n", av_err2str(ret));
86 ret = avcodec_receive_packet(avctx, &enc_pkt);
90 enc_pkt.stream_index = 0;
91 ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout);
92 av_packet_unref(&enc_pkt);
96 ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
100 int main(int argc, char *argv[])
103 FILE *fin = NULL, *fout = NULL;
104 AVFrame *sw_frame = NULL, *hw_frame = NULL;
105 AVCodecContext *avctx = NULL;
106 AVCodec *codec = NULL;
107 const char *enc_name = "h264_vaapi";
110 fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
114 width = atoi(argv[1]);
115 height = atoi(argv[2]);
116 size = width * height;
118 if (!(fin = fopen(argv[3], "r"))) {
119 fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
122 if (!(fout = fopen(argv[4], "w+b"))) {
123 fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
128 err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
131 fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
135 if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
136 fprintf(stderr, "Could not find encoder.\n");
141 if (!(avctx = avcodec_alloc_context3(codec))) {
142 err = AVERROR(ENOMEM);
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;
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");
159 if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
160 fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
165 if (!(sw_frame = av_frame_alloc())) {
166 err = AVERROR(ENOMEM);
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)
175 if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
177 if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
180 if (!(hw_frame = av_frame_alloc())) {
181 err = AVERROR(ENOMEM);
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));
188 if (!hw_frame->hw_frames_ctx) {
189 err = AVERROR(ENOMEM);
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));
198 if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
199 fprintf(stderr, "Failed to encode.\n");
202 av_frame_free(&hw_frame);
203 av_frame_free(&sw_frame);
207 err = encode_write(avctx, NULL, fout);
208 if (err == AVERROR_EOF)
216 av_frame_free(&sw_frame);
217 av_frame_free(&hw_frame);
218 avcodec_free_context(&avctx);
219 av_buffer_unref(&hw_device_ctx);