]> git.sesse.net Git - ffmpeg/blob - doc/examples/hw_decode.c
lavu: Add OpenCL hardware pixfmt
[ffmpeg] / doc / examples / hw_decode.c
1 /*
2  * Copyright (c) 2017 Jun Zhao
3  * Copyright (c) 2017 Kaixuan Liu
4  *
5  * HW Acceleration API (video decoding) decode sample
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * HW-Accelerated decoding example.
27  *
28  * @example hw_decode.c
29  * This example shows how to do HW-accelerated decoding with output
30  * frames from the HW video surfaces.
31  */
32
33 #include <stdio.h>
34
35 #include <libavcodec/avcodec.h>
36 #include <libavformat/avformat.h>
37 #include <libavutil/pixdesc.h>
38 #include <libavutil/hwcontext.h>
39 #include <libavutil/opt.h>
40 #include <libavutil/avassert.h>
41 #include <libavutil/imgutils.h>
42
43 static AVBufferRef *hw_device_ctx = NULL;
44 static enum AVPixelFormat hw_pix_fmt;
45 static FILE *output_file = NULL;
46
47 static enum AVPixelFormat find_fmt_by_hw_type(const enum AVHWDeviceType type)
48 {
49     enum AVPixelFormat fmt;
50
51     switch (type) {
52     case AV_HWDEVICE_TYPE_VAAPI:
53         fmt = AV_PIX_FMT_VAAPI;
54         break;
55     case AV_HWDEVICE_TYPE_DXVA2:
56         fmt = AV_PIX_FMT_DXVA2_VLD;
57         break;
58     case AV_HWDEVICE_TYPE_D3D11VA:
59         fmt = AV_PIX_FMT_D3D11;
60         break;
61     case AV_HWDEVICE_TYPE_VDPAU:
62         fmt = AV_PIX_FMT_VDPAU;
63         break;
64     case AV_HWDEVICE_TYPE_VIDEOTOOLBOX:
65         fmt = AV_PIX_FMT_VIDEOTOOLBOX;
66         break;
67     default:
68         fmt = AV_PIX_FMT_NONE;
69         break;
70     }
71
72     return fmt;
73 }
74
75 static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
76 {
77     int err = 0;
78
79     if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
80                                       NULL, NULL, 0)) < 0) {
81         fprintf(stderr, "Failed to create specified HW device.\n");
82         return err;
83     }
84     ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
85
86     return err;
87 }
88
89 static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
90                                         const enum AVPixelFormat *pix_fmts)
91 {
92     const enum AVPixelFormat *p;
93
94     for (p = pix_fmts; *p != -1; p++) {
95         if (*p == hw_pix_fmt)
96             return *p;
97     }
98
99     fprintf(stderr, "Failed to get HW surface format.\n");
100     return AV_PIX_FMT_NONE;
101 }
102
103 static int decode_write(AVCodecContext *avctx, AVPacket *packet)
104 {
105     AVFrame *frame = NULL, *sw_frame = NULL;
106     AVFrame *tmp_frame = NULL;
107     uint8_t *buffer = NULL;
108     int size;
109     int ret = 0;
110
111     ret = avcodec_send_packet(avctx, packet);
112     if (ret < 0) {
113         fprintf(stderr, "Error during decoding\n");
114         return ret;
115     }
116
117     while (ret >= 0) {
118         if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
119             fprintf(stderr, "Can not alloc frame\n");
120             ret = AVERROR(ENOMEM);
121             goto fail;
122         }
123
124         ret = avcodec_receive_frame(avctx, frame);
125         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
126             av_frame_free(&frame);
127             av_frame_free(&sw_frame);
128             return 0;
129         } else if (ret < 0) {
130             fprintf(stderr, "Error while decoding\n");
131             goto fail;
132         }
133
134         if (frame->format == hw_pix_fmt) {
135             /* retrieve data from GPU to CPU */
136             if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
137                 fprintf(stderr, "Error transferring the data to system memory\n");
138                 goto fail;
139             }
140             tmp_frame = sw_frame;
141         } else
142             tmp_frame = frame;
143
144         size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
145                                         tmp_frame->height, 1);
146         buffer = av_malloc(size);
147         if (!buffer) {
148             fprintf(stderr, "Can not alloc buffer\n");
149             ret = AVERROR(ENOMEM);
150             goto fail;
151         }
152         ret = av_image_copy_to_buffer(buffer, size,
153                                       (const uint8_t * const *)tmp_frame->data,
154                                       (const int *)tmp_frame->linesize, tmp_frame->format,
155                                       tmp_frame->width, tmp_frame->height, 1);
156         if (ret < 0) {
157             fprintf(stderr, "Can not copy image to buffer\n");
158             goto fail;
159         }
160
161         if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
162             fprintf(stderr, "Failed to dump raw data.\n");
163             goto fail;
164         }
165
166     fail:
167         av_frame_free(&frame);
168         av_frame_free(&sw_frame);
169         if (buffer)
170             av_freep(&buffer);
171         if (ret < 0)
172             return ret;
173     }
174
175     return 0;
176 }
177
178 int main(int argc, char *argv[])
179 {
180     AVFormatContext *input_ctx = NULL;
181     int video_stream, ret;
182     AVStream *video = NULL;
183     AVCodecContext *decoder_ctx = NULL;
184     AVCodec *decoder = NULL;
185     AVPacket packet;
186     enum AVHWDeviceType type;
187
188     if (argc < 4) {
189         fprintf(stderr, "Usage: %s <vaapi|vdpau|dxva2|d3d11va> <input file> <output file>\n", argv[0]);
190         return -1;
191     }
192
193     av_register_all();
194
195     type = av_hwdevice_find_type_by_name(argv[1]);
196     hw_pix_fmt = find_fmt_by_hw_type(type);
197     if (hw_pix_fmt == -1) {
198         fprintf(stderr, "Cannot support '%s' in this example.\n", argv[1]);
199         return -1;
200     }
201
202     /* open the input file */
203     if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
204         fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
205         return -1;
206     }
207
208     if (avformat_find_stream_info(input_ctx, NULL) < 0) {
209         fprintf(stderr, "Cannot find input stream information.\n");
210         return -1;
211     }
212
213     /* find the video stream information */
214     ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
215     if (ret < 0) {
216         fprintf(stderr, "Cannot find a video stream in the input file\n");
217         return -1;
218     }
219     video_stream = ret;
220
221     if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
222         return AVERROR(ENOMEM);
223
224     video = input_ctx->streams[video_stream];
225     if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
226         return -1;
227
228     decoder_ctx->get_format  = get_hw_format;
229     av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0);
230
231     if (hw_decoder_init(decoder_ctx, type) < 0)
232         return -1;
233
234     if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
235         fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
236         return -1;
237     }
238
239     /* open the file to dump raw data */
240     output_file = fopen(argv[3], "w+");
241
242     /* actual decoding and dump the raw data */
243     while (ret >= 0) {
244         if ((ret = av_read_frame(input_ctx, &packet)) < 0)
245             break;
246
247         if (video_stream == packet.stream_index)
248             ret = decode_write(decoder_ctx, &packet);
249
250         av_packet_unref(&packet);
251     }
252
253     /* flush the decoder */
254     packet.data = NULL;
255     packet.size = 0;
256     ret = decode_write(decoder_ctx, &packet);
257     av_packet_unref(&packet);
258
259     if (output_file)
260         fclose(output_file);
261     avcodec_free_context(&decoder_ctx);
262     avformat_close_input(&input_ctx);
263     av_buffer_unref(&hw_device_ctx);
264
265     return 0;
266 }