2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to deal
4 * in the Software without restriction, including without limitation the rights
5 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6 * copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * @file Intel VAAPI-accelerated encoding API usage example
23 * @example vaapi_encode.c
25 * Perform VAAPI-accelerated encoding. Read input from an NV12 raw
26 * file, and write the H.264 encoded data to an output raw file.
27 * Usage: vaapi_encode 1920 1080 input.yuv output.h264
34 #include <libavcodec/avcodec.h>
35 #include <libavutil/pixdesc.h>
36 #include <libavutil/hwcontext.h>
38 static int width, height;
39 static AVBufferRef *hw_device_ctx = NULL;
41 static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
43 AVBufferRef *hw_frames_ref;
44 AVHWFramesContext *frames_ctx = NULL;
47 if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
48 fprintf(stderr, "Failed to create VAAPI frame context.\n");
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 av_buffer_unref(&hw_frames_ref);
63 ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
64 if (!ctx->hw_frames_ctx)
65 err = AVERROR(ENOMEM);
67 av_buffer_unref(&hw_frames_ref);
71 static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
76 if (!(enc_pkt = av_packet_alloc()))
77 return AVERROR(ENOMEM);
79 if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
80 fprintf(stderr, "Error code: %s\n", av_err2str(ret));
84 ret = avcodec_receive_packet(avctx, enc_pkt);
88 enc_pkt->stream_index = 0;
89 ret = fwrite(enc_pkt->data, enc_pkt->size, 1, fout);
90 av_packet_unref(enc_pkt);
94 av_packet_free(&enc_pkt);
95 ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
99 int main(int argc, char *argv[])
102 FILE *fin = NULL, *fout = NULL;
103 AVFrame *sw_frame = NULL, *hw_frame = NULL;
104 AVCodecContext *avctx = NULL;
105 const AVCodec *codec = NULL;
106 const char *enc_name = "h264_vaapi";
109 fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
113 width = atoi(argv[1]);
114 height = atoi(argv[2]);
115 size = width * height;
117 if (!(fin = fopen(argv[3], "r"))) {
118 fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
121 if (!(fout = fopen(argv[4], "w+b"))) {
122 fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
127 err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
130 fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
134 if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
135 fprintf(stderr, "Could not find encoder.\n");
140 if (!(avctx = avcodec_alloc_context3(codec))) {
141 err = AVERROR(ENOMEM);
145 avctx->width = width;
146 avctx->height = height;
147 avctx->time_base = (AVRational){1, 25};
148 avctx->framerate = (AVRational){25, 1};
149 avctx->sample_aspect_ratio = (AVRational){1, 1};
150 avctx->pix_fmt = AV_PIX_FMT_VAAPI;
152 /* set hw_frames_ctx for encoder's AVCodecContext */
153 if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
154 fprintf(stderr, "Failed to set hwframe context.\n");
158 if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
159 fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
164 if (!(sw_frame = av_frame_alloc())) {
165 err = AVERROR(ENOMEM);
168 /* read data into software frame, and transfer them into hw frame */
169 sw_frame->width = width;
170 sw_frame->height = height;
171 sw_frame->format = AV_PIX_FMT_NV12;
172 if ((err = av_frame_get_buffer(sw_frame, 0)) < 0)
174 if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
176 if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
179 if (!(hw_frame = av_frame_alloc())) {
180 err = AVERROR(ENOMEM);
183 if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
184 fprintf(stderr, "Error code: %s.\n", av_err2str(err));
187 if (!hw_frame->hw_frames_ctx) {
188 err = AVERROR(ENOMEM);
191 if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
192 fprintf(stderr, "Error while transferring frame data to surface."
193 "Error code: %s.\n", av_err2str(err));
197 if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
198 fprintf(stderr, "Failed to encode.\n");
201 av_frame_free(&hw_frame);
202 av_frame_free(&sw_frame);
206 err = encode_write(avctx, NULL, fout);
207 if (err == AVERROR_EOF)
215 av_frame_free(&sw_frame);
216 av_frame_free(&hw_frame);
217 avcodec_free_context(&avctx);
218 av_buffer_unref(&hw_device_ctx);