1 /*
2 * Video Acceleration API (video transcoding) transcode sample
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file
25 * Intel VAAPI-accelerated transcoding example.
26 *
27 * @example vaapi_transcode.c
28 * This example shows how to do VAAPI-accelerated transcoding.
29 * Usage: vaapi_transcode input_stream codec output_stream
30 * e.g: - vaapi_transcode input.mp4 h264_vaapi output_h264.mp4
31 * - vaapi_transcode input.mp4 vp9_vaapi output_vp9.ivf
32 */
33
34 #include <stdio.h>
35 #include <errno.h>
36
40
47
50 {
52
55 return *p;
56 }
57
58 fprintf(stderr, "Unable to decode this file using VA-API.\n");
60 }
61
63 {
67
69 fprintf(stderr, "Cannot open input file '%s', Error code: %s\n",
72 }
73
75 fprintf(stderr, "Cannot find input stream information. Error code: %s\n",
78 }
79
82 fprintf(stderr, "Cannot find a video stream in the input file. "
85 }
87
90
93 fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n",
96 }
97
100 fprintf(stderr, "A hardware device reference create failed.\n");
102 }
104
106 fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
108
110 }
111
113 {
115
117
119 fprintf(stderr,
"Error during encoding. Error code: %s\n",
av_err2str(
ret));
120 goto end;
121 }
122 while (1) {
125 break;
126
132 fprintf(stderr, "Error during writing data to output file. "
134 return -1;
135 }
136 }
137
138 end:
140 return 0;
143 }
144
146 {
149
152 fprintf(stderr,
"Error during decoding. Error code: %s\n",
av_err2str(
ret));
154 }
155
159
163 return 0;
164 }
else if (
ret < 0) {
165 fprintf(stderr,
"Error while decoding. Error code: %s\n",
av_err2str(
ret));
167 }
168
170 /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec.
171 Only after we get a decoded frame, can we obtain its hw_frames_ctx */
176 }
177 /* set AVCodecContext Parameters for encoder, here we keep them stay
178 * the same as decoder.
179 * xxx: now the sample can't handle resolution change case.
180 */
185
187 fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
190 }
191
193 fprintf(stderr, "Failed to allocate stream for output format.\n");
196 }
197
201 fprintf(stderr, "Failed to copy the stream parameters. "
204 }
205
206 /* write the stream header */
208 fprintf(stderr, "Error while writing stream header. "
211 }
212
214 }
215
217 fprintf(stderr, "Error during encoding and writing.\n");
218
223 }
224 return 0;
225 }
226
227 int main(
int argc,
char **argv)
228 {
232
233 if (argc != 4) {
234 fprintf(stderr, "Usage: %s <input file> <encode codec> <output file>\n"
235 "The output format is guessed according to the file extension.\n"
236 "\n", argv[0]);
237 return -1;
238 }
239
242 fprintf(stderr,
"Failed to create a VAAPI device. Error code: %s\n",
av_err2str(
ret));
243 return -1;
244 }
245
247 if (!dec_pkt) {
248 fprintf(stderr, "Failed to allocate decode packet\n");
249 goto end;
250 }
251
253 goto end;
254
256 fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
258 goto end;
259 }
260
262 fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
264 goto end;
265 }
266
269 goto end;
270 }
271
274 fprintf(stderr, "Cannot open output file. "
276 goto end;
277 }
278
279 /* read all packets and only transcoding video */
282 break;
283
286
288 }
289
290 /* flush decoder */
293
294 /* flush encoder */
296
297 /* write the trailer for output stream */
299
300 end:
308 }