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 transcoding API usage example
23 * @example vaapi_transcode.c
24 *
25 * Perform VAAPI-accelerated transcoding.
26 * Usage: vaapi_transcode input_stream codec output_stream
27 * e.g: - vaapi_transcode input.mp4 h264_vaapi output_h264.mp4
28 * - vaapi_transcode input.mp4 vp9_vaapi output_vp9.ivf
29 */
30
31 #include <stdio.h>
32 #include <errno.h>
33
37
44
47 {
49
52 return *p;
53 }
54
55 fprintf(stderr, "Unable to decode this file using VA-API.\n");
57 }
58
60 {
64
66 fprintf(stderr, "Cannot open input file '%s', Error code: %s\n",
69 }
70
72 fprintf(stderr, "Cannot find input stream information. Error code: %s\n",
75 }
76
79 fprintf(stderr, "Cannot find a video stream in the input file. "
82 }
84
87
90 fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n",
93 }
94
97 fprintf(stderr, "A hardware device reference create failed.\n");
99 }
101
103 fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
105
107 }
108
110 {
112
114
116 fprintf(stderr,
"Error during encoding. Error code: %s\n",
av_err2str(
ret));
117 goto end;
118 }
119 while (1) {
122 break;
123
129 fprintf(stderr, "Error during writing data to output file. "
131 return -1;
132 }
133 }
134
135 end:
137 return 0;
140 }
141
143 {
146
149 fprintf(stderr,
"Error during decoding. Error code: %s\n",
av_err2str(
ret));
151 }
152
156
160 return 0;
161 }
else if (
ret < 0) {
162 fprintf(stderr,
"Error while decoding. Error code: %s\n",
av_err2str(
ret));
164 }
165
167 /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec.
168 Only after we get a decoded frame, can we obtain its hw_frames_ctx */
173 }
174 /* set AVCodecContext Parameters for encoder, here we keep them stay
175 * the same as decoder.
176 * xxx: now the sample can't handle resolution change case.
177 */
182
184 fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
187 }
188
190 fprintf(stderr, "Failed to allocate stream for output format.\n");
193 }
194
198 fprintf(stderr, "Failed to copy the stream parameters. "
201 }
202
203 /* write the stream header */
205 fprintf(stderr, "Error while writing stream header. "
208 }
209
211 }
212
214 fprintf(stderr, "Error during encoding and writing.\n");
215
218 }
220 }
221
222 int main(
int argc,
char **argv)
223 {
227
228 if (argc != 4) {
229 fprintf(stderr, "Usage: %s <input file> <encode codec> <output file>\n"
230 "The output format is guessed according to the file extension.\n"
231 "\n", argv[0]);
232 return -1;
233 }
234
237 fprintf(stderr,
"Failed to create a VAAPI device. Error code: %s\n",
av_err2str(
ret));
238 return -1;
239 }
240
242 if (!dec_pkt) {
243 fprintf(stderr, "Failed to allocate decode packet\n");
244 goto end;
245 }
246
248 goto end;
249
251 fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
253 goto end;
254 }
255
257 fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
259 goto end;
260 }
261
264 goto end;
265 }
266
269 fprintf(stderr, "Cannot open output file. "
271 goto end;
272 }
273
274 /* read all packets and only transcoding video */
277 break;
278
281
283 }
284
285 /* flush decoder */
288
289 /* flush encoder */
291
292 /* write the trailer for output stream */
294
295 end:
303 }