Imported Upstream version 5.1.2
[platform/upstream/ffmpeg.git] / doc / examples / qsv_decode.c
1 /*
2  * Copyright (c) 2015 Anton Khirnov
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 Intel QSV-accelerated H.264 decoding API usage example
25  * @example qsv_decode.c
26  *
27  * Perform QSV-accelerated H.264 decoding with output frames in the
28  * GPU video surfaces, write the decoded frames to an output file.
29  */
30
31 #include "config.h"
32
33 #include <stdio.h>
34
35 #include "libavformat/avformat.h"
36 #include "libavformat/avio.h"
37
38 #include "libavcodec/avcodec.h"
39
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"
45
46 static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
47 {
48     while (*pix_fmts != AV_PIX_FMT_NONE) {
49         if (*pix_fmts == AV_PIX_FMT_QSV) {
50             return AV_PIX_FMT_QSV;
51         }
52
53         pix_fmts++;
54     }
55
56     fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
57
58     return AV_PIX_FMT_NONE;
59 }
60
61 static int decode_packet(AVCodecContext *decoder_ctx,
62                          AVFrame *frame, AVFrame *sw_frame,
63                          AVPacket *pkt, AVIOContext *output_ctx)
64 {
65     int ret = 0;
66
67     ret = avcodec_send_packet(decoder_ctx, pkt);
68     if (ret < 0) {
69         fprintf(stderr, "Error during decoding\n");
70         return ret;
71     }
72
73     while (ret >= 0) {
74         int i, j;
75
76         ret = avcodec_receive_frame(decoder_ctx, frame);
77         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
78             break;
79         else if (ret < 0) {
80             fprintf(stderr, "Error during decoding\n");
81             return ret;
82         }
83
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);
88         if (ret < 0) {
89             fprintf(stderr, "Error transferring the data to system memory\n");
90             goto fail;
91         }
92
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);
96
97 fail:
98         av_frame_unref(sw_frame);
99         av_frame_unref(frame);
100
101         if (ret < 0)
102             return ret;
103     }
104
105     return 0;
106 }
107
108 int main(int argc, char **argv)
109 {
110     AVFormatContext *input_ctx = NULL;
111     AVStream *video_st = NULL;
112     AVCodecContext *decoder_ctx = NULL;
113     const AVCodec *decoder;
114
115     AVPacket *pkt = NULL;
116     AVFrame *frame = NULL, *sw_frame = NULL;
117
118     AVIOContext *output_ctx = NULL;
119
120     int ret, i;
121
122     AVBufferRef *device_ref = NULL;
123
124     if (argc < 3) {
125         fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
126         return 1;
127     }
128
129     /* open the input file */
130     ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
131     if (ret < 0) {
132         fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
133         goto finish;
134     }
135
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];
139
140         if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
141             video_st = st;
142         else
143             st->discard = AVDISCARD_ALL;
144     }
145     if (!video_st) {
146         fprintf(stderr, "No H.264 video stream in the input file\n");
147         goto finish;
148     }
149
150     /* open the hardware device */
151     ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_QSV,
152                                  "auto", NULL, 0);
153     if (ret < 0) {
154         fprintf(stderr, "Cannot open the hardware device\n");
155         goto finish;
156     }
157
158     /* initialize the decoder */
159     decoder = avcodec_find_decoder_by_name("h264_qsv");
160     if (!decoder) {
161         fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
162         goto finish;
163     }
164
165     decoder_ctx = avcodec_alloc_context3(decoder);
166     if (!decoder_ctx) {
167         ret = AVERROR(ENOMEM);
168         goto finish;
169     }
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);
176             goto finish;
177         }
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;
181     }
182
183
184     decoder_ctx->hw_device_ctx = av_buffer_ref(device_ref);
185     decoder_ctx->get_format  = get_format;
186
187     ret = avcodec_open2(decoder_ctx, NULL, NULL);
188     if (ret < 0) {
189         fprintf(stderr, "Error opening the decoder: ");
190         goto finish;
191     }
192
193     /* open the output stream */
194     ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
195     if (ret < 0) {
196         fprintf(stderr, "Error opening the output context: ");
197         goto finish;
198     }
199
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);
205         goto finish;
206     }
207
208     /* actual decoding */
209     while (ret >= 0) {
210         ret = av_read_frame(input_ctx, pkt);
211         if (ret < 0)
212             break;
213
214         if (pkt->stream_index == video_st->index)
215             ret = decode_packet(decoder_ctx, frame, sw_frame, pkt, output_ctx);
216
217         av_packet_unref(pkt);
218     }
219
220     /* flush the decoder */
221     ret = decode_packet(decoder_ctx, frame, sw_frame, NULL, output_ctx);
222
223 finish:
224     if (ret < 0) {
225         char buf[1024];
226         av_strerror(ret, buf, sizeof(buf));
227         fprintf(stderr, "%s\n", buf);
228     }
229
230     avformat_close_input(&input_ctx);
231
232     av_frame_free(&frame);
233     av_frame_free(&sw_frame);
234     av_packet_free(&pkt);
235
236     avcodec_free_context(&decoder_ctx);
237
238     av_buffer_unref(&device_ref);
239
240     avio_close(output_ctx);
241
242     return ret;
243 }