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;
}
{
int ret = 0;
fprintf(stderr,
"Error code: %s\n",
av_err2str(ret));
}
while (1) {
if (ret)
break;
ret = fwrite(enc_pkt.
data, enc_pkt.
size, 1, fout);
}
ret = ((ret ==
AVERROR(EAGAIN)) ? 0 : -1);
return ret;
}
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;
if ((err = fread((
uint8_t*)(sw_frame->
data[0]), size, 1, fin)) <= 0)
break;
if ((err = fread((
uint8_t*)(sw_frame->
data[1]), size/2, 1, fin)) <= 0)
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;
}