FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vaapi_transcode.c
Go to the documentation of this file.
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 
42 static int video_stream = -1;
43 static AVStream *ost;
44 static int initialized = 0;
45 
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 
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 */
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);
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 
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 */
296 
297 end:
298  avformat_close_input(&ifmt_ctx);
300  avcodec_free_context(&decoder_ctx);
302  av_buffer_unref(&hw_device_ctx);
303  return ret;
304 }
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1154
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3040
static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
static int encode_write(AVFrame *frame)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:1185
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:420
static AVCodecContext * decoder_ctx
int size
Definition: avcodec.h:1431
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:655
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3408
static int open_input_file(const char *filename)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1640
Format I/O context.
Definition: avformat.h:1342
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4450
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2078
static AVFrame * frame
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:571
uint8_t * data
Definition: avcodec.h:1430
#define AVERROR_EOF
End of file.
Definition: error.h:55
static AVBufferRef * hw_device_ctx
AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
Definition: allcodecs.c:885
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags)
Find the "best" stream in the file.
Definition: utils.c:4178
void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
Definition: avpacket.c:694
#define AVERROR(e)
Definition: error.h:43
static AVFormatContext * ofmt_ctx
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:709
#define fail()
Definition: checkasm.h:116
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:508
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:156
static const chunk_decoder decoder[8]
Definition: dfa.c:330
int width
picture width / height.
Definition: avcodec.h:1690
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3197
static int initialized
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
static int video_stream
Stream structure.
Definition: avformat.h:873
static AVCodecContext * encoder_ctx
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:642
Libavcodec external API header.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:171
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
main external API structure.
Definition: avcodec.h:1518
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: encode.c:390
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1769
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2021
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:538
static int dec_enc(AVPacket *pkt, AVCodec *enc_codec)
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1767
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
A reference to a data buffer.
Definition: buffer.h:81
static AVStream * ost
Main libavformat public API header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3559
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4422
int main(int argc, char **argv)
static AVFormatContext * ifmt_ctx
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:537
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1247
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
int stream_index
Definition: avcodec.h:1432
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:3249
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1407