]> git.sesse.net Git - ffmpeg/blob - doc/examples/vaapi_transcode.c
Merge commit 'c7ab6aff66cba2f265f656ce8d56aa428d4ada76'
[ffmpeg] / doc / examples / vaapi_transcode.c
1 /*
2  * Video Acceleration API (video transcoding) transcode sample
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Intel VAAPI-accelerated transcoding example.
24  *
25  * @example vaapi_transcode.c
26  * This example shows how to do VAAPI-accelerated transcoding.
27  * Usage: vaapi_transcode input_stream codec output_stream
28  * e.g: - vaapi_transcode input.mp4 h264_vaapi output_h264.mp4
29  *      - vaapi_transcode input.mp4 vp9_vaapi output_vp9.ivf
30  */
31
32 #include <stdio.h>
33 #include <errno.h>
34
35 #include <libavutil/hwcontext.h>
36 #include <libavcodec/avcodec.h>
37 #include <libavformat/avformat.h>
38
39 static AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
40 static AVBufferRef *hw_device_ctx = NULL;
41 static AVCodecContext *decoder_ctx = NULL, *encoder_ctx = NULL;
42 static int video_stream = -1;
43 static AVStream *ost;
44 static int initialized = 0;
45
46 static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx,
47                                            const enum AVPixelFormat *pix_fmts)
48 {
49     const enum AVPixelFormat *p;
50
51     for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
52         if (*p == AV_PIX_FMT_VAAPI)
53             return *p;
54     }
55
56     fprintf(stderr, "Unable to decode this file using VA-API.\n");
57     return AV_PIX_FMT_NONE;
58 }
59
60 static int open_input_file(const char *filename)
61 {
62     int ret;
63     AVCodec *decoder = NULL;
64     AVStream *video = NULL;
65
66     if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
67         fprintf(stderr, "Cannot open input file '%s', Error code: %s\n",
68                 filename, av_err2str(ret));
69         return ret;
70     }
71
72     if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
73         fprintf(stderr, "Cannot find input stream information. Error code: %s\n",
74                 av_err2str(ret));
75         return ret;
76     }
77
78     ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
79     if (ret < 0) {
80         fprintf(stderr, "Cannot find a video stream in the input file. "
81                 "Error code: %s\n", av_err2str(ret));
82         return ret;
83     }
84     video_stream = ret;
85
86     if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
87         return AVERROR(ENOMEM);
88
89     video = ifmt_ctx->streams[video_stream];
90     if ((ret = avcodec_parameters_to_context(decoder_ctx, video->codecpar)) < 0) {
91         fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n",
92                 av_err2str(ret));
93         return ret;
94     }
95
96     decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
97     if (!decoder_ctx->hw_device_ctx) {
98         fprintf(stderr, "A hardware device reference create failed.\n");
99         return AVERROR(ENOMEM);
100     }
101     decoder_ctx->get_format    = get_vaapi_format;
102
103     if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0)
104         fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
105                 av_err2str(ret));
106
107     return ret;
108 }
109
110 static int encode_write(AVFrame *frame)
111 {
112     int ret = 0;
113     AVPacket enc_pkt;
114
115     av_init_packet(&enc_pkt);
116     enc_pkt.data = NULL;
117     enc_pkt.size = 0;
118
119     if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) {
120         fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret));
121         goto end;
122     }
123     while (1) {
124         ret = avcodec_receive_packet(encoder_ctx, &enc_pkt);
125         if (ret)
126             break;
127
128         enc_pkt.stream_index = 0;
129         av_packet_rescale_ts(&enc_pkt, ifmt_ctx->streams[video_stream]->time_base,
130                              ofmt_ctx->streams[0]->time_base);
131         ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
132         if (ret < 0) {
133             fprintf(stderr, "Error during writing data to output file. "
134                     "Error code: %s\n", av_err2str(ret));
135             return -1;
136         }
137     }
138
139 end:
140     if (ret == AVERROR_EOF)
141         return 0;
142     ret = ((ret == AVERROR(EAGAIN)) ? 0:-1);
143     return ret;
144 }
145
146 static int dec_enc(AVPacket *pkt, AVCodec *enc_codec)
147 {
148     AVFrame *frame;
149     int ret = 0;
150
151     ret = avcodec_send_packet(decoder_ctx, pkt);
152     if (ret < 0) {
153         fprintf(stderr, "Error during decoding. Error code: %s\n", av_err2str(ret));
154         return ret;
155     }
156
157     while (ret >= 0) {
158         if (!(frame = av_frame_alloc()))
159             return AVERROR(ENOMEM);
160
161         ret = avcodec_receive_frame(decoder_ctx, frame);
162         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
163             av_frame_free(&frame);
164             return 0;
165         } else if (ret < 0) {
166             fprintf(stderr, "Error while decoding. Error code: %s\n", av_err2str(ret));
167             goto fail;
168         }
169
170         if (!initialized) {
171             /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec.
172                Only after we get a decoded frame, can we obtain its hw_frames_ctx */
173             encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx);
174             if (!encoder_ctx->hw_frames_ctx) {
175                 ret = AVERROR(ENOMEM);
176                 goto fail;
177             }
178             /* set AVCodecContext Parameters for encoder, here we keep them stay
179              * the same as decoder.
180              * xxx: now the the sample can't handle resolution change case.
181              */
182             encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate);
183             encoder_ctx->pix_fmt   = AV_PIX_FMT_VAAPI;
184             encoder_ctx->width     = decoder_ctx->width;
185             encoder_ctx->height    = decoder_ctx->height;
186
187             if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) {
188                 fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
189                         av_err2str(ret));
190                 goto fail;
191             }
192
193             if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) {
194                 fprintf(stderr, "Failed to allocate stream for output format.\n");
195                 ret = AVERROR(ENOMEM);
196                 goto fail;
197             }
198
199             ost->time_base = encoder_ctx->time_base;
200             ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx);
201             if (ret < 0) {
202                 fprintf(stderr, "Failed to copy the stream parameters. "
203                         "Error code: %s\n", av_err2str(ret));
204                 goto fail;
205             }
206
207             /* write the stream header */
208             if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0) {
209                 fprintf(stderr, "Error while writing stream header. "
210                         "Error code: %s\n", av_err2str(ret));
211                 goto fail;
212             }
213
214             initialized = 1;
215         }
216
217         if ((ret = encode_write(frame)) < 0)
218             fprintf(stderr, "Error during encoding and writing.\n");
219
220 fail:
221         av_frame_free(&frame);
222         if (ret < 0)
223             return ret;
224     }
225     return 0;
226 }
227
228 int main(int argc, char **argv)
229 {
230     int ret = 0;
231     AVPacket dec_pkt;
232     AVCodec *enc_codec;
233
234     if (argc != 4) {
235         fprintf(stderr, "Usage: %s <input file> <encode codec> <output file>\n"
236                 "The output format is guessed according to the file extension.\n"
237                 "\n", argv[0]);
238         return -1;
239     }
240
241     ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0);
242     if (ret < 0) {
243         fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(ret));
244         return -1;
245     }
246
247     if ((ret = open_input_file(argv[1])) < 0)
248         goto end;
249
250     if (!(enc_codec = avcodec_find_encoder_by_name(argv[2]))) {
251         fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
252         ret = -1;
253         goto end;
254     }
255
256     if ((ret = (avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, argv[3]))) < 0) {
257         fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
258                 "%s\n", av_err2str(ret));
259         goto end;
260     }
261
262     if (!(encoder_ctx = avcodec_alloc_context3(enc_codec))) {
263         ret = AVERROR(ENOMEM);
264         goto end;
265     }
266
267     ret = avio_open(&ofmt_ctx->pb, argv[3], AVIO_FLAG_WRITE);
268     if (ret < 0) {
269         fprintf(stderr, "Cannot open output file. "
270                 "Error code: %s\n", av_err2str(ret));
271         goto end;
272     }
273
274     /* read all packets and only transcoding video */
275     while (ret >= 0) {
276         if ((ret = av_read_frame(ifmt_ctx, &dec_pkt)) < 0)
277             break;
278
279         if (video_stream == dec_pkt.stream_index)
280             ret = dec_enc(&dec_pkt, enc_codec);
281
282         av_packet_unref(&dec_pkt);
283     }
284
285     /* flush decoder */
286     dec_pkt.data = NULL;
287     dec_pkt.size = 0;
288     ret = dec_enc(&dec_pkt, enc_codec);
289     av_packet_unref(&dec_pkt);
290
291     /* flush encoder */
292     ret = encode_write(NULL);
293
294     /* write the trailer for output stream */
295     av_write_trailer(ofmt_ctx);
296
297 end:
298     avformat_close_input(&ifmt_ctx);
299     avformat_close_input(&ofmt_ctx);
300     avcodec_free_context(&decoder_ctx);
301     avcodec_free_context(&encoder_ctx);
302     av_buffer_unref(&hw_device_ctx);
303     return ret;
304 }