2 * Copyright (c) 2015 Anton Khirnov
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
24 * @file Intel QSV-accelerated H.264 decoding API usage example
25 * @example qsv_decode.c
27 * Perform QSV-accelerated H.264 decoding with output frames in the
28 * GPU video surfaces, write the decoded frames to an output file.
35 #include "libavformat/avformat.h"
36 #include "libavformat/avio.h"
38 #include "libavcodec/avcodec.h"
40 #include "libavutil/buffer.h"
41 #include "libavutil/error.h"
42 #include "libavutil/hwcontext.h"
43 #include "libavutil/hwcontext_qsv.h"
44 #include "libavutil/mem.h"
46 static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
48 while (*pix_fmts != AV_PIX_FMT_NONE) {
49 if (*pix_fmts == AV_PIX_FMT_QSV) {
50 return AV_PIX_FMT_QSV;
56 fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
58 return AV_PIX_FMT_NONE;
61 static int decode_packet(AVCodecContext *decoder_ctx,
62 AVFrame *frame, AVFrame *sw_frame,
63 AVPacket *pkt, AVIOContext *output_ctx)
67 ret = avcodec_send_packet(decoder_ctx, pkt);
69 fprintf(stderr, "Error during decoding\n");
76 ret = avcodec_receive_frame(decoder_ctx, frame);
77 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
80 fprintf(stderr, "Error during decoding\n");
84 /* A real program would do something useful with the decoded frame here.
85 * We just retrieve the raw data and write it to a file, which is rather
86 * useless but pedagogic. */
87 ret = av_hwframe_transfer_data(sw_frame, frame, 0);
89 fprintf(stderr, "Error transferring the data to system memory\n");
93 for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++)
94 for (j = 0; j < (sw_frame->height >> (i > 0)); j++)
95 avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], sw_frame->width);
98 av_frame_unref(sw_frame);
99 av_frame_unref(frame);
108 int main(int argc, char **argv)
110 AVFormatContext *input_ctx = NULL;
111 AVStream *video_st = NULL;
112 AVCodecContext *decoder_ctx = NULL;
113 const AVCodec *decoder;
115 AVPacket *pkt = NULL;
116 AVFrame *frame = NULL, *sw_frame = NULL;
118 AVIOContext *output_ctx = NULL;
122 AVBufferRef *device_ref = NULL;
125 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
129 /* open the input file */
130 ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
132 fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
136 /* find the first H.264 video stream */
137 for (i = 0; i < input_ctx->nb_streams; i++) {
138 AVStream *st = input_ctx->streams[i];
140 if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
143 st->discard = AVDISCARD_ALL;
146 fprintf(stderr, "No H.264 video stream in the input file\n");
150 /* open the hardware device */
151 ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_QSV,
154 fprintf(stderr, "Cannot open the hardware device\n");
158 /* initialize the decoder */
159 decoder = avcodec_find_decoder_by_name("h264_qsv");
161 fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
165 decoder_ctx = avcodec_alloc_context3(decoder);
167 ret = AVERROR(ENOMEM);
170 decoder_ctx->codec_id = AV_CODEC_ID_H264;
171 if (video_st->codecpar->extradata_size) {
172 decoder_ctx->extradata = av_mallocz(video_st->codecpar->extradata_size +
173 AV_INPUT_BUFFER_PADDING_SIZE);
174 if (!decoder_ctx->extradata) {
175 ret = AVERROR(ENOMEM);
178 memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,
179 video_st->codecpar->extradata_size);
180 decoder_ctx->extradata_size = video_st->codecpar->extradata_size;
184 decoder_ctx->hw_device_ctx = av_buffer_ref(device_ref);
185 decoder_ctx->get_format = get_format;
187 ret = avcodec_open2(decoder_ctx, NULL, NULL);
189 fprintf(stderr, "Error opening the decoder: ");
193 /* open the output stream */
194 ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
196 fprintf(stderr, "Error opening the output context: ");
200 frame = av_frame_alloc();
201 sw_frame = av_frame_alloc();
202 pkt = av_packet_alloc();
203 if (!frame || !sw_frame || !pkt) {
204 ret = AVERROR(ENOMEM);
208 /* actual decoding */
210 ret = av_read_frame(input_ctx, pkt);
214 if (pkt->stream_index == video_st->index)
215 ret = decode_packet(decoder_ctx, frame, sw_frame, pkt, output_ctx);
217 av_packet_unref(pkt);
220 /* flush the decoder */
221 ret = decode_packet(decoder_ctx, frame, sw_frame, NULL, output_ctx);
226 av_strerror(ret, buf, sizeof(buf));
227 fprintf(stderr, "%s\n", buf);
230 avformat_close_input(&input_ctx);
232 av_frame_free(&frame);
233 av_frame_free(&sw_frame);
234 av_packet_free(&pkt);
236 avcodec_free_context(&decoder_ctx);
238 av_buffer_unref(&device_ref);
240 avio_close(output_ctx);