FFmpeg  4.0
vaapi_encode.c
Go to the documentation of this file.
1 /*
2  * Video Acceleration API (video encoding) encode sample
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * Intel VAAPI-accelerated encoding example.
24  *
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
28  *
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 
35 #include <libavcodec/avcodec.h>
36 #include <libavutil/pixdesc.h>
37 #include <libavutil/hwcontext.h>
38 
39 static int width, height;
41 
42 static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
43 {
44  AVBufferRef *hw_frames_ref;
45  AVHWFramesContext *frames_ctx = NULL;
46  int err = 0;
47 
48  if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
49  fprintf(stderr, "Failed to create VAAPI frame context.\n");
50  return -1;
51  }
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);
62  return err;
63  }
64  ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
65  if (!ctx->hw_frames_ctx)
66  err = AVERROR(ENOMEM);
67 
68  av_buffer_unref(&hw_frames_ref);
69  return err;
70 }
71 
72 static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
73 {
74  int ret = 0;
75  AVPacket enc_pkt;
76 
77  av_init_packet(&enc_pkt);
78  enc_pkt.data = NULL;
79  enc_pkt.size = 0;
80 
81  if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
82  fprintf(stderr, "Error code: %s\n", av_err2str(ret));
83  goto end;
84  }
85  while (1) {
86  ret = avcodec_receive_packet(avctx, &enc_pkt);
87  if (ret)
88  break;
89 
90  enc_pkt.stream_index = 0;
91  ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout);
92  av_packet_unref(&enc_pkt);
93  }
94 
95 end:
96  ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
97  return ret;
98 }
99 
100 int main(int argc, char *argv[])
101 {
102  int size, err;
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";
108 
109  if (argc < 5) {
110  fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
111  return -1;
112  }
113 
114  width = atoi(argv[1]);
115  height = atoi(argv[2]);
116  size = width * height;
117 
118  if (!(fin = fopen(argv[3], "r"))) {
119  fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
120  return -1;
121  }
122  if (!(fout = fopen(argv[4], "w+b"))) {
123  fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
124  err = -1;
125  goto close;
126  }
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  avcodec_free_context(&avctx);
219  av_buffer_unref(&hw_device_ctx);
220 
221  return err;
222 }
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3040
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:420
int size
Definition: avcodec.h:1431
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1896
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:228
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:208
AVCodec.
Definition: avcodec.h:3408
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1640
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int main(int argc, char *argv[])
Definition: vaapi_encode.c:100
const char enc_name[6]
Definition: rtp.c:36
static AVFrame * frame
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:571
uint8_t * data
Definition: avcodec.h:1430
#define AVERROR_EOF
End of file.
Definition: error.h:55
AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
Definition: allcodecs.c:885
static int width
Definition: vaapi_encode.c:39
int width
Definition: frame.h:276
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:85
static AVBufferRef * hw_device_ctx
Definition: vaapi_encode.c:40
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:329
static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
Definition: vaapi_encode.c:42
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:465
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:439
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:198
static int height
Definition: vaapi_encode.c:39
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:156
int width
picture width / height.
Definition: avcodec.h:1690
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3197
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:291
Libavcodec external API header.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:171
main external API structure.
Definition: avcodec.h:1518
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
uint8_t * data
The data buffer.
Definition: buffer.h:89
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: encode.c:390
Rational number (pair of numerator and denominator).
Definition: rational.h:58
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:538
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:322
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
A reference to a data buffer.
Definition: buffer.h:81
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:243
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
Definition: vaapi_encode.c:72
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int height
Definition: frame.h:276
int stream_index
Definition: avcodec.h:1432
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:221
This structure stores compressed data.
Definition: avcodec.h:1407