FFmpeg
vaapi_encode.c
Go to the documentation of this file.
1 /*
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:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
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
18  * THE SOFTWARE.
19  */
20 
21 /**
22  * @file Intel VAAPI-accelerated encoding API usage example
23  * @example vaapi_encode.c
24  *
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
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;
40 
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  av_buffer_unref(&hw_frames_ref);
61  return err;
62  }
63  ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
64  if (!ctx->hw_frames_ctx)
65  err = AVERROR(ENOMEM);
66 
67  av_buffer_unref(&hw_frames_ref);
68  return err;
69 }
70 
71 static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
72 {
73  int ret = 0;
74  AVPacket *enc_pkt;
75 
76  if (!(enc_pkt = av_packet_alloc()))
77  return AVERROR(ENOMEM);
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  if (ret != enc_pkt->size) {
92  ret = AVERROR(errno);
93  break;
94  }
95  }
96 
97 end:
98  av_packet_free(&enc_pkt);
99  ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
100  return ret;
101 }
102 
103 int main(int argc, char *argv[])
104 {
105  int size, err;
106  FILE *fin = NULL, *fout = NULL;
107  AVFrame *sw_frame = NULL, *hw_frame = NULL;
108  AVCodecContext *avctx = NULL;
109  const AVCodec *codec = NULL;
110  const char *enc_name = "h264_vaapi";
111 
112  if (argc < 5) {
113  fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
114  return -1;
115  }
116 
117  width = atoi(argv[1]);
118  height = atoi(argv[2]);
119  size = width * height;
120 
121  if (!(fin = fopen(argv[3], "r"))) {
122  fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
123  return -1;
124  }
125  if (!(fout = fopen(argv[4], "w+b"))) {
126  fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
127  err = -1;
128  goto close;
129  }
130 
132  NULL, NULL, 0);
133  if (err < 0) {
134  fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
135  goto close;
136  }
137 
138  if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
139  fprintf(stderr, "Could not find encoder.\n");
140  err = -1;
141  goto close;
142  }
143 
144  if (!(avctx = avcodec_alloc_context3(codec))) {
145  err = AVERROR(ENOMEM);
146  goto close;
147  }
148 
149  avctx->width = width;
150  avctx->height = height;
151  avctx->time_base = (AVRational){1, 25};
152  avctx->framerate = (AVRational){25, 1};
153  avctx->sample_aspect_ratio = (AVRational){1, 1};
154  avctx->pix_fmt = AV_PIX_FMT_VAAPI;
155 
156  /* set hw_frames_ctx for encoder's AVCodecContext */
157  if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
158  fprintf(stderr, "Failed to set hwframe context.\n");
159  goto close;
160  }
161 
162  if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
163  fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
164  goto close;
165  }
166 
167  while (1) {
168  if (!(sw_frame = av_frame_alloc())) {
169  err = AVERROR(ENOMEM);
170  goto close;
171  }
172  /* read data into software frame, and transfer them into hw frame */
173  sw_frame->width = width;
174  sw_frame->height = height;
175  sw_frame->format = AV_PIX_FMT_NV12;
176  if ((err = av_frame_get_buffer(sw_frame, 0)) < 0)
177  goto close;
178  if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
179  break;
180  if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
181  break;
182 
183  if (!(hw_frame = av_frame_alloc())) {
184  err = AVERROR(ENOMEM);
185  goto close;
186  }
187  if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
188  fprintf(stderr, "Error code: %s.\n", av_err2str(err));
189  goto close;
190  }
191  if (!hw_frame->hw_frames_ctx) {
192  err = AVERROR(ENOMEM);
193  goto close;
194  }
195  if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
196  fprintf(stderr, "Error while transferring frame data to surface."
197  "Error code: %s.\n", av_err2str(err));
198  goto close;
199  }
200 
201  if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
202  fprintf(stderr, "Failed to encode.\n");
203  goto close;
204  }
205  av_frame_free(&hw_frame);
206  av_frame_free(&sw_frame);
207  }
208 
209  /* flush encoder */
210  err = encode_write(avctx, NULL, fout);
211  if (err == AVERROR_EOF)
212  err = 0;
213 
214 close:
215  if (fin)
216  fclose(fin);
217  if (fout)
218  fclose(fout);
219  av_frame_free(&sw_frame);
220  av_frame_free(&hw_frame);
221  avcodec_free_context(&avctx);
223 
224  return err;
225 }
main
int main(int argc, char *argv[])
Definition: vaapi_encode.c:103
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:430
AVCodec
AVCodec.
Definition: codec.h:187
avcodec_receive_packet
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:541
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:305
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:197
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:322
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
pixdesc.h
AVFrame::width
int width
Definition: frame.h:475
av_hwframe_ctx_alloc
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:248
AVPacket::data
uint8_t * data
Definition: packet.h:539
enc_name
const char enc_name[6]
Definition: rtp.c:36
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:217
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:75
width
static int width
Definition: vaapi_encode.c:38
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:424
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:574
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:151
AVHWFramesContext::height
int height
Definition: hwcontext.h:217
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
ctx
AVFormatContext * ctx
Definition: movenc.c:49
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:210
av_buffer_unref
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:139
avcodec_free_context
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:164
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:143
set_hwframe_ctx
static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
Definition: vaapi_encode.c:41
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:558
AVPacket::size
int size
Definition: packet.h:540
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
size
int size
Definition: twinvq_data.h:10344
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:490
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:64
AV_PIX_FMT_VAAPI
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition: pixfmt.h:126
hw_device_ctx
static AVBufferRef * hw_device_ctx
Definition: vaapi_encode.c:39
AV_HWDEVICE_TYPE_VAAPI
@ AV_HWDEVICE_TYPE_VAAPI
Definition: hwcontext.h:31
AVCodecContext::height
int height
Definition: avcodec.h:632
avcodec_send_frame
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: encode.c:508
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:671
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1493
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
ret
ret
Definition: filter_design.txt:187
height
static int height
Definition: vaapi_encode.c:38
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
av_hwdevice_ctx_create
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:600
av_hwframe_transfer_data
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:433
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AVFrame::height
int height
Definition: frame.h:475
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AVHWFramesContext::initial_pool_size
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:187
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
hwcontext.h
av_hwframe_get_buffer
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:491
AVCodecContext::sample_aspect_ratio
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:656
avcodec_find_encoder_by_name
const AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
Definition: allcodecs.c:1036
encode_write
static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
Definition: vaapi_encode.c:71