]> git.sesse.net Git - ffmpeg/blob - doc/examples/hw_decode.c
doc/examples/hw_decode: Remove useless NULL check
[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 int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
48 {
49     int err = 0;
50
51     if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
52                                       NULL, NULL, 0)) < 0) {
53         fprintf(stderr, "Failed to create specified HW device.\n");
54         return err;
55     }
56     ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
57
58     return err;
59 }
60
61 static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
62                                         const enum AVPixelFormat *pix_fmts)
63 {
64     const enum AVPixelFormat *p;
65
66     for (p = pix_fmts; *p != -1; p++) {
67         if (*p == hw_pix_fmt)
68             return *p;
69     }
70
71     fprintf(stderr, "Failed to get HW surface format.\n");
72     return AV_PIX_FMT_NONE;
73 }
74
75 static int decode_write(AVCodecContext *avctx, AVPacket *packet)
76 {
77     AVFrame *frame = NULL, *sw_frame = NULL;
78     AVFrame *tmp_frame = NULL;
79     uint8_t *buffer = NULL;
80     int size;
81     int ret = 0;
82
83     ret = avcodec_send_packet(avctx, packet);
84     if (ret < 0) {
85         fprintf(stderr, "Error during decoding\n");
86         return ret;
87     }
88
89     while (ret >= 0) {
90         if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
91             fprintf(stderr, "Can not alloc frame\n");
92             ret = AVERROR(ENOMEM);
93             goto fail;
94         }
95
96         ret = avcodec_receive_frame(avctx, frame);
97         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
98             av_frame_free(&frame);
99             av_frame_free(&sw_frame);
100             return 0;
101         } else if (ret < 0) {
102             fprintf(stderr, "Error while decoding\n");
103             goto fail;
104         }
105
106         if (frame->format == hw_pix_fmt) {
107             /* retrieve data from GPU to CPU */
108             if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
109                 fprintf(stderr, "Error transferring the data to system memory\n");
110                 goto fail;
111             }
112             tmp_frame = sw_frame;
113         } else
114             tmp_frame = frame;
115
116         size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
117                                         tmp_frame->height, 1);
118         buffer = av_malloc(size);
119         if (!buffer) {
120             fprintf(stderr, "Can not alloc buffer\n");
121             ret = AVERROR(ENOMEM);
122             goto fail;
123         }
124         ret = av_image_copy_to_buffer(buffer, size,
125                                       (const uint8_t * const *)tmp_frame->data,
126                                       (const int *)tmp_frame->linesize, tmp_frame->format,
127                                       tmp_frame->width, tmp_frame->height, 1);
128         if (ret < 0) {
129             fprintf(stderr, "Can not copy image to buffer\n");
130             goto fail;
131         }
132
133         if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
134             fprintf(stderr, "Failed to dump raw data.\n");
135             goto fail;
136         }
137
138     fail:
139         av_frame_free(&frame);
140         av_frame_free(&sw_frame);
141         av_freep(&buffer);
142         if (ret < 0)
143             return ret;
144     }
145
146     return 0;
147 }
148
149 int main(int argc, char *argv[])
150 {
151     AVFormatContext *input_ctx = NULL;
152     int video_stream, ret;
153     AVStream *video = NULL;
154     AVCodecContext *decoder_ctx = NULL;
155     AVCodec *decoder = NULL;
156     AVPacket packet;
157     enum AVHWDeviceType type;
158     int i;
159
160     if (argc < 4) {
161         fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]);
162         return -1;
163     }
164
165     type = av_hwdevice_find_type_by_name(argv[1]);
166     if (type == AV_HWDEVICE_TYPE_NONE) {
167         fprintf(stderr, "Device type %s is not supported.\n", argv[1]);
168         fprintf(stderr, "Available device types:");
169         while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
170             fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
171         fprintf(stderr, "\n");
172         return -1;
173     }
174
175     /* open the input file */
176     if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
177         fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
178         return -1;
179     }
180
181     if (avformat_find_stream_info(input_ctx, NULL) < 0) {
182         fprintf(stderr, "Cannot find input stream information.\n");
183         return -1;
184     }
185
186     /* find the video stream information */
187     ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
188     if (ret < 0) {
189         fprintf(stderr, "Cannot find a video stream in the input file\n");
190         return -1;
191     }
192     video_stream = ret;
193
194     for (i = 0;; i++) {
195         const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
196         if (!config) {
197             fprintf(stderr, "Decoder %s does not support device type %s.\n",
198                     decoder->name, av_hwdevice_get_type_name(type));
199             return -1;
200         }
201         if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
202             config->device_type == type) {
203             hw_pix_fmt = config->pix_fmt;
204             break;
205         }
206     }
207
208     if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
209         return AVERROR(ENOMEM);
210
211     video = input_ctx->streams[video_stream];
212     if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
213         return -1;
214
215     decoder_ctx->get_format  = get_hw_format;
216     av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0);
217
218     if (hw_decoder_init(decoder_ctx, type) < 0)
219         return -1;
220
221     if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
222         fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
223         return -1;
224     }
225
226     /* open the file to dump raw data */
227     output_file = fopen(argv[3], "w+");
228
229     /* actual decoding and dump the raw data */
230     while (ret >= 0) {
231         if ((ret = av_read_frame(input_ctx, &packet)) < 0)
232             break;
233
234         if (video_stream == packet.stream_index)
235             ret = decode_write(decoder_ctx, &packet);
236
237         av_packet_unref(&packet);
238     }
239
240     /* flush the decoder */
241     packet.data = NULL;
242     packet.size = 0;
243     ret = decode_write(decoder_ctx, &packet);
244     av_packet_unref(&packet);
245
246     if (output_file)
247         fclose(output_file);
248     avcodec_free_context(&decoder_ctx);
249     avformat_close_input(&input_ctx);
250     av_buffer_unref(&hw_device_ctx);
251
252     return 0;
253 }