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
37
40
42 {
45 int err = 0;
46
48 fprintf(stderr, "Failed to create VAAPI frame context.\n");
49 return -1;
50 }
58 fprintf(stderr, "Failed to initialize VAAPI frame context."
61 return err;
62 }
64 if (!
ctx->hw_frames_ctx)
66
68 return err;
69 }
70
72 {
75
78
81 goto end;
82 }
83 while (1) {
86 break;
87
89 ret = fwrite(enc_pkt->
data, enc_pkt->
size, 1, fout);
93 break;
94 }
95 }
96
97 end:
101 }
102
103 int main(
int argc,
char *argv[])
104 {
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]);
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
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
139 fprintf(stderr, "Could not find encoder.\n");
140 err = -1;
141 goto close;
142 }
143
146 goto close;
147 }
148
155
156 /* set hw_frames_ctx for encoder's AVCodecContext */
158 fprintf(stderr, "Failed to set hwframe context.\n");
159 goto close;
160 }
161
163 fprintf(stderr,
"Cannot open video encoder codec. Error code: %s\n",
av_err2str(err));
164 goto close;
165 }
166
167 while (1) {
170 goto close;
171 }
172 /* read data into software frame, and transfer them into hw frame */
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
185 goto close;
186 }
188 fprintf(stderr,
"Error code: %s.\n",
av_err2str(err));
189 goto close;
190 }
191 if (!hw_frame->hw_frames_ctx) {
193 goto close;
194 }
196 fprintf(stderr, "Error while transferring frame data to surface."
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 }
207 }
208
209 /* flush encoder */
212 err = 0;
213
214 close:
215 if (fin)
216 fclose(fin);
217 if (fout)
218 fclose(fout);
223
224 return err;
225 }