OSDN Git Service

Fix building issues of Android 8.0
[android-x86/external-ffmpeg.git] / ffmpeg_vaapi.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/frame.h"
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/log.h"
25
26 #include "ffmpeg.h"
27
28 #ifdef ANDROID
29 int hwaccel_lax_profile_check = 0;
30 AVBufferRef *hw_device_ctx;
31 #endif
32
33 static AVClass vaapi_class = {
34     .class_name = "vaapi",
35     .item_name  = av_default_item_name,
36     .version    = LIBAVUTIL_VERSION_INT,
37 };
38
39 #define DEFAULT_SURFACES 20
40
41 typedef struct VAAPIDecoderContext {
42     const AVClass *class;
43
44     AVBufferRef       *device_ref;
45     AVHWDeviceContext *device;
46     AVBufferRef       *frames_ref;
47     AVHWFramesContext *frames;
48
49     // The output need not have the same format, width and height as the
50     // decoded frames - the copy for non-direct-mapped access is actually
51     // a whole vpp instance which can do arbitrary scaling and format
52     // conversion.
53     enum AVPixelFormat output_format;
54 } VAAPIDecoderContext;
55
56
57 static int vaapi_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
58 {
59     InputStream *ist = avctx->opaque;
60     VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
61     int err;
62
63     err = av_hwframe_get_buffer(ctx->frames_ref, frame, 0);
64     if (err < 0) {
65         av_log(ctx, AV_LOG_ERROR, "Failed to allocate decoder surface.\n");
66     } else {
67         av_log(ctx, AV_LOG_DEBUG, "Decoder given surface %#x.\n",
68                (unsigned int)(uintptr_t)frame->data[3]);
69     }
70     return err;
71 }
72
73 static int vaapi_retrieve_data(AVCodecContext *avctx, AVFrame *input)
74 {
75     InputStream *ist = avctx->opaque;
76     VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
77     AVFrame *output = 0;
78     int err;
79
80     av_assert0(input->format == AV_PIX_FMT_VAAPI);
81
82     if (ctx->output_format == AV_PIX_FMT_VAAPI) {
83         // Nothing to do.
84         return 0;
85     }
86
87     av_log(ctx, AV_LOG_DEBUG, "Retrieve data from surface %#x.\n",
88            (unsigned int)(uintptr_t)input->data[3]);
89
90     output = av_frame_alloc();
91     if (!output)
92         return AVERROR(ENOMEM);
93
94     output->format = ctx->output_format;
95
96     err = av_hwframe_transfer_data(output, input, 0);
97     if (err < 0) {
98         av_log(ctx, AV_LOG_ERROR, "Failed to transfer data to "
99                "output frame: %d.\n", err);
100         goto fail;
101     }
102
103     err = av_frame_copy_props(output, input);
104     if (err < 0) {
105         av_frame_unref(output);
106         goto fail;
107     }
108
109     av_frame_unref(input);
110     av_frame_move_ref(input, output);
111     av_frame_free(&output);
112
113     return 0;
114
115 fail:
116     if (output)
117         av_frame_free(&output);
118     return err;
119 }
120
121 static void vaapi_decode_uninit(AVCodecContext *avctx)
122 {
123     InputStream *ist = avctx->opaque;
124     VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
125
126     if (ctx) {
127         av_buffer_unref(&ctx->frames_ref);
128         av_buffer_unref(&ctx->device_ref);
129         av_free(ctx);
130     }
131
132     av_buffer_unref(&ist->hw_frames_ctx);
133
134     ist->hwaccel_ctx           = NULL;
135     ist->hwaccel_uninit        = NULL;
136     ist->hwaccel_get_buffer    = NULL;
137     ist->hwaccel_retrieve_data = NULL;
138 }
139
140 int vaapi_decode_init(AVCodecContext *avctx)
141 {
142     InputStream *ist = avctx->opaque;
143     VAAPIDecoderContext *ctx;
144     int err;
145     int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
146                                                      : AV_LOG_ERROR);
147
148     if (ist->hwaccel_ctx)
149         vaapi_decode_uninit(avctx);
150
151     // We have -hwaccel without -vaapi_device, so just initialise here with
152     // the device passed as -hwaccel_device (if -vaapi_device was passed, it
153     // will always have been called before now).
154     if (!hw_device_ctx) {
155         err = vaapi_device_init(ist->hwaccel_device);
156         if (err < 0)
157             return err;
158     }
159
160     ctx = av_mallocz(sizeof(*ctx));
161     if (!ctx)
162         return AVERROR(ENOMEM);
163     ctx->class = &vaapi_class;
164     ist->hwaccel_ctx = ctx;
165
166     ctx->device_ref = av_buffer_ref(hw_device_ctx);
167     ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
168
169     ctx->output_format = ist->hwaccel_output_format;
170     avctx->pix_fmt = ctx->output_format;
171
172     ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
173     if (!ctx->frames_ref) {
174         av_log(ctx, loglevel, "Failed to create VAAPI frame context.\n");
175         err = AVERROR(ENOMEM);
176         goto fail;
177     }
178
179     ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
180
181     ctx->frames->format = AV_PIX_FMT_VAAPI;
182     ctx->frames->width  = avctx->coded_width;
183     ctx->frames->height = avctx->coded_height;
184
185     // It would be nice if we could query the available formats here,
186     // but unfortunately we don't have a VAConfigID to do it with.
187     // For now, just assume an NV12 format (or P010 if 10-bit).
188     ctx->frames->sw_format = (avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
189                               AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
190
191     // For frame-threaded decoding, at least one additional surface
192     // is needed for each thread.
193     ctx->frames->initial_pool_size = DEFAULT_SURFACES;
194     if (avctx->active_thread_type & FF_THREAD_FRAME)
195         ctx->frames->initial_pool_size += avctx->thread_count;
196
197     err = av_hwframe_ctx_init(ctx->frames_ref);
198     if (err < 0) {
199         av_log(ctx, loglevel, "Failed to initialise VAAPI frame "
200                "context: %d\n", err);
201         goto fail;
202     }
203
204     ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
205     if (!ist->hw_frames_ctx) {
206         err = AVERROR(ENOMEM);
207         goto fail;
208     }
209
210     ist->hwaccel_uninit        = &vaapi_decode_uninit;
211     ist->hwaccel_get_buffer    = &vaapi_get_buffer;
212     ist->hwaccel_retrieve_data = &vaapi_retrieve_data;
213
214     return 0;
215
216 fail:
217     vaapi_decode_uninit(avctx);
218     return err;
219 }
220
221 static AVClass *vaapi_log = &vaapi_class;
222
223 av_cold int vaapi_device_init(const char *device)
224 {
225     int err;
226
227     av_buffer_unref(&hw_device_ctx);
228
229     err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
230                                  device, NULL, 0);
231     if (err < 0) {
232         av_log(&vaapi_log, AV_LOG_ERROR, "Failed to create a VAAPI device\n");
233         return err;
234     }
235
236     return 0;
237 }