This example shows how to do VAAPI-accelerated encoding. now only support NV12 raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264
#include <stdio.h>
#include <string.h>
#include <errno.h>
{
int err = 0;
fprintf(stderr, "Failed to create VAAPI frame context.\n");
return -1;
}
fprintf(stderr, "Failed to initialize VAAPI frame context."
return err;
}
return err;
}
{
}
while (1) {
break;
}
}
int main(
int argc,
char *argv[])
{
if (argc < 5) {
fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
return -1;
}
if (!(fin = fopen(argv[3], "r"))) {
fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
return -1;
}
if (!(fout = fopen(argv[4], "w+b"))) {
fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
err = -1;
goto close;
}
if (err < 0) {
fprintf(stderr,
"Failed to create a VAAPI device. Error code: %s\n",
av_err2str(err));
goto close;
}
fprintf(stderr, "Could not find encoder.\n");
err = -1;
goto close;
}
goto close;
}
fprintf(stderr, "Failed to set hwframe context.\n");
goto close;
}
fprintf(stderr,
"Cannot open video encoder codec. Error code: %s\n",
av_err2str(err));
goto close;
}
while (1) {
goto close;
}
goto close;
break;
break;
goto close;
}
fprintf(stderr,
"Error code: %s.\n",
av_err2str(err));
goto close;
}
if (!hw_frame->hw_frames_ctx) {
goto close;
}
fprintf(stderr, "Error while transferring frame data to surface."
goto close;
}
fprintf(stderr, "Failed to encode.\n");
goto close;
}
}
err = 0;
close:
if (fin)
fclose(fin);
if (fout)
fclose(fout);
return err;
}
int main(int argc, char *argv[])
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
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
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
#define AVERROR_EOF
End of file.
uint8_t * data
The data buffer.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static av_cold int end(AVCodecContext *avctx)
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
This structure describes decoded (raw) audio or video data.
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
int width
The allocated dimensions of the frames in this pool.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Rational number (pair of numerator and denominator).
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
static AVBufferRef * hw_device_ctx
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
This struct describes a set or pool of "hardware" frames (i.e.
@ 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...
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
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.
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
main external API structure.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
int initial_pool_size
Initial size of the frame pool.
A reference to a data buffer.
This structure stores compressed data.
int width
picture width / height.
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)