FFmpeg 6.1.1
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
38static 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
71static 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 }
92
93end:
94 av_packet_free(&enc_pkt);
95 ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
96 return ret;
97}
98
99int main(int argc, char *argv[])
100{
101 int size, err;
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";
107
108 if (argc < 5) {
109 fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
110 return -1;
111 }
112
113 width = atoi(argv[1]);
114 height = atoi(argv[2]);
115 size = width * height;
116
117 if (!(fin = fopen(argv[3], "r"))) {
118 fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
119 return -1;
120 }
121 if (!(fout = fopen(argv[4], "w+b"))) {
122 fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
123 err = -1;
124 goto close;
125 }
126
128 NULL, NULL, 0);
129 if (err < 0) {
130 fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
131 goto close;
132 }
133
134 if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
135 fprintf(stderr, "Could not find encoder.\n");
136 err = -1;
137 goto close;
138 }
139
140 if (!(avctx = avcodec_alloc_context3(codec))) {
141 err = AVERROR(ENOMEM);
142 goto close;
143 }
144
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;
151
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");
155 goto close;
156 }
157
158 if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
159 fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
160 goto close;
161 }
162
163 while (1) {
164 if (!(sw_frame = av_frame_alloc())) {
165 err = AVERROR(ENOMEM);
166 goto close;
167 }
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)
173 goto close;
174 if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
175 break;
176 if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
177 break;
178
179 if (!(hw_frame = av_frame_alloc())) {
180 err = AVERROR(ENOMEM);
181 goto close;
182 }
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));
185 goto close;
186 }
187 if (!hw_frame->hw_frames_ctx) {
188 err = AVERROR(ENOMEM);
189 goto close;
190 }
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));
194 goto close;
195 }
196
197 if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
198 fprintf(stderr, "Failed to encode.\n");
199 goto close;
200 }
201 av_frame_free(&hw_frame);
202 av_frame_free(&sw_frame);
203 }
204
205 /* flush encoder */
206 err = encode_write(avctx, NULL, fout);
207 if (err == AVERROR_EOF)
208 err = 0;
209
210close:
211 if (fin)
212 fclose(fin);
213 if (fout)
214 fclose(fout);
215 av_frame_free(&sw_frame);
216 av_frame_free(&hw_frame);
217 avcodec_free_context(&avctx);
219
220 return err;
221}
Libavcodec external API header.
static AVFrame * frame
Definition: demux_decode.c:54
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
const AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AVERROR_EOF
End of file.
Definition: error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
#define AVERROR(e)
Definition: error.h:45
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ctx)
Allocate an AVHWFramesContext tied to a given device context.
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
@ AV_HWDEVICE_TYPE_VAAPI
Definition: hwcontext.h:31
@ 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:89
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition: pixfmt.h:119
A reference to a data buffer.
Definition: buffer.h:82
uint8_t * data
The data buffer.
Definition: buffer.h:90
main external API structure.
Definition: avcodec.h:441
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
int width
picture width / height.
Definition: avcodec.h:621
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1940
AVRational framerate
Definition: avcodec.h:1803
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:822
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:187
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
int width
Definition: frame.h:412
int height
Definition: frame.h:412
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:199
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
This structure stores compressed data.
Definition: packet.h:468
int stream_index
Definition: packet.h:493
int size
Definition: packet.h:492
uint8_t * data
Definition: packet.h:491
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static AVBufferRef * hw_device_ctx
Definition: vaapi_encode.c:39
int main(int argc, char *argv[])
Definition: vaapi_encode.c:99
static int width
Definition: vaapi_encode.c:38
static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
Definition: vaapi_encode.c:71
static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
Definition: vaapi_encode.c:41
static int height
Definition: vaapi_encode.c:38