]> git.sesse.net Git - mlt/blob - src/modules/avformat/vdpau.c
fix QObject::connect of type QTextCursor errors
[mlt] / src / modules / avformat / vdpau.c
1 /*
2  * producer_avformat/vdpau.c -- VDPAU functions for the avformat producer
3  * Copyright (C) 2009 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <libavcodec/vdpau.h>
22 #include <X11/Xlib.h>
23 #include <dlfcn.h>
24
25 extern pthread_mutex_t mlt_sdl_mutex;
26
27 static VdpDeviceCreateX11      *vdpau_device_create_x11;
28 static VdpDeviceDestroy        *vdp_device_destroy;
29 static VdpGetProcAddress       *vdp_get_proc_address;
30 static VdpGetErrorString       *vdp_get_error_string;
31 static VdpGetApiVersion        *vdp_get_api_version;
32 static VdpGetInformationString *vdp_get_information_string;
33 static VdpVideoSurfaceCreate   *vdp_surface_create;
34 static VdpVideoSurfaceDestroy  *vdp_surface_destroy;
35 static VdpVideoSurfaceGetBitsYCbCr *vdp_surface_get_bits;
36 static VdpDecoderCreate        *vdp_decoder_create;
37 static VdpDecoderDestroy       *vdp_decoder_destroy;
38 static VdpDecoderRender        *vdp_decoder_render;
39
40 static int vdpau_init_done = 0;
41
42 /** VDPAUD functions
43 */
44
45 static void vdpau_fini( producer_avformat self )
46 {
47         if ( !self->vdpau )
48                 return;
49         mlt_log_debug( NULL, "vdpau_fini (%x)\n", self->vdpau->device );
50         if ( self->vdpau->decoder != VDP_INVALID_HANDLE )
51                 vdp_decoder_destroy( self->vdpau->decoder );
52         if ( self->vdpau->device != VDP_INVALID_HANDLE )
53                 vdp_device_destroy( self->vdpau->device );
54         free( self->vdpau );
55         self->vdpau = NULL;
56 }
57
58 static int vdpau_init( producer_avformat self )
59 {
60         mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "vdpau_init\n" );
61         int success = 0;
62         mlt_properties properties = MLT_PRODUCER_PROPERTIES( self->parent );
63         Display *display = XOpenDisplay( NULL );
64         
65         if ( !display || mlt_properties_get_int( properties, "novdpau" )
66              || ( getenv( "MLT_NO_VDPAU" ) && strcmp( getenv( "MLT_NO_VDPAU" ), "1" ) == 0 ) )
67                 return success;
68
69         void *object = NULL;
70         if ( !vdpau_init_done )
71         {
72                 int flags = RTLD_NOW;
73                 object = dlopen( "/usr/lib64/libvdpau.so", flags );
74                 if ( !object )
75                         object = dlopen( "/usr/lib/libvdpau.so", flags );
76                 if ( !object )
77                         object = dlopen( "/usr/local/lib/libvdpau.so", flags );
78                 if ( object )
79                         vdpau_device_create_x11 = dlsym( object, "vdp_device_create_x11" );
80                 else
81                 {
82                         mlt_log( MLT_PRODUCER_SERVICE(self->parent), MLT_LOG_WARNING, "%s: failed to dlopen libvdpau.so\n  (%s)\n", __FUNCTION__, dlerror() );
83                         return success;
84                 }
85         }
86                         
87         if ( vdpau_device_create_x11 )
88         {
89                 int screen = mlt_properties_get_int( properties, "x11_screen" );
90
91                 self->vdpau = calloc( 1, sizeof( *self->vdpau ) );
92                 self->vdpau->device = VDP_INVALID_HANDLE;
93                 self->vdpau->decoder = VDP_INVALID_HANDLE;
94                                 
95                 mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "X11 Display = %p\n", display );
96                 if ( VDP_STATUS_OK == vdpau_device_create_x11( display, screen, &self->vdpau->device, &vdp_get_proc_address ) )
97                 {
98                         if ( !vdpau_init_done ) {
99                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_GET_ERROR_STRING, (void**) &vdp_get_error_string );
100                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_GET_API_VERSION, (void**) &vdp_get_api_version );
101                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_GET_INFORMATION_STRING, (void**) &vdp_get_information_string );
102                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_VIDEO_SURFACE_CREATE, (void**) &vdp_surface_create );
103                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, (void**) &vdp_surface_destroy );
104                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, (void**) &vdp_surface_get_bits );
105                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_DECODER_CREATE, (void**) &vdp_decoder_create );
106                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_DECODER_DESTROY, (void**) &vdp_decoder_destroy );
107                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_DECODER_RENDER, (void**) &vdp_decoder_render );
108                                 vdp_get_proc_address( self->vdpau->device, VDP_FUNC_ID_DEVICE_DESTROY, (void**) &vdp_device_destroy );
109                                 vdpau_init_done = 1;
110                         }
111                         success = 1;
112                 }
113         }
114         
115         if ( !success )
116         {
117                 mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "VDPAU failed to initialize device\n" );
118                 if ( object )
119                         dlclose( object );
120                 if ( self->vdpau )
121                         vdpau_fini( self );
122         }
123
124         return success;
125 }
126
127 static enum PixelFormat vdpau_get_format( struct AVCodecContext *s, const enum PixelFormat *fmt )
128 {
129         return PIX_FMT_VDPAU_H264;
130 }
131
132 static int vdpau_get_buffer( AVCodecContext *codec_context, AVFrame *frame )
133 {
134         int error = 0;
135         producer_avformat self = codec_context->opaque;
136         mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "vdpau_get_buffer\n" );
137         
138         if ( self->vdpau && mlt_deque_count( self->vdpau->deque ) )
139         {
140                 struct vdpau_render_state *render = mlt_deque_pop_front( self->vdpau->deque );
141                 
142                 if ( render )
143                 {
144                         frame->data[0] = (uint8_t*) render;
145                         frame->data[1] = (uint8_t*) render;
146                         frame->data[2] = (uint8_t*) render;
147                         frame->linesize[0] = 0;
148                         frame->linesize[1] = 0;
149                         frame->linesize[2] = 0;
150                         frame->type = FF_BUFFER_TYPE_USER;
151                         render->state = FF_VDPAU_STATE_USED_FOR_REFERENCE;
152                         frame->reordered_opaque = codec_context->reordered_opaque;
153                         if ( frame->reference )
154                         {
155                                 frame->age = self->vdpau->ip_age[0];
156                                 self->vdpau->ip_age[0] = self->vdpau->ip_age[1] + 1;
157                                 self->vdpau->ip_age[1] = 1;
158                                 self->vdpau->b_age++;
159                         }
160                         else
161                         {
162                                 frame->age = self->vdpau->b_age;
163                                 self->vdpau->ip_age[0] ++;
164                                 self->vdpau->ip_age[1] ++;
165                                 self->vdpau->b_age = 1;
166                         }
167                 }
168                 else
169                 {
170                         mlt_log_warning( MLT_PRODUCER_SERVICE(self->parent), "VDPAU surface underrun\n" );
171                         error = -1;
172                 }
173         }
174         else
175         {
176                 mlt_log_warning( MLT_PRODUCER_SERVICE(self->parent), "VDPAU surface underrun\n" );
177                 error = -1;
178         }
179         
180         return error;
181 }
182
183 static void vdpau_release_buffer( AVCodecContext *codec_context, AVFrame *frame )
184 {
185         producer_avformat self = codec_context->opaque;
186         if ( self->vdpau )
187         {
188                 struct vdpau_render_state *render = (struct vdpau_render_state*) frame->data[0];
189                 mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "vdpau_release_buffer (%x)\n", render->surface );
190                 int i;
191
192                 render->state &= ~FF_VDPAU_STATE_USED_FOR_REFERENCE;
193                 for ( i = 0; i < 4; i++ )
194                         frame->data[i] = NULL;
195                 mlt_deque_push_back( self->vdpau->deque, render );
196         }
197 }
198
199 static void vdpau_draw_horiz( AVCodecContext *codec_context, const AVFrame *frame, int offset[4], int y, int type, int height )
200 {
201         producer_avformat self = codec_context->opaque;
202         if ( self->vdpau )
203         {
204                 struct vdpau_render_state *render = (struct vdpau_render_state*) frame->data[0];
205                 VdpVideoSurface surface = render->surface;
206                 VdpStatus status = vdp_decoder_render( self->vdpau->decoder, surface, (void*) &render->info,
207                         render->bitstream_buffers_used, render->bitstream_buffers );
208
209                 if ( status != VDP_STATUS_OK )
210                 {
211                         self->vdpau->is_decoded = 0;
212                         mlt_log_warning( MLT_PRODUCER_SERVICE(self->parent), "VDPAU failed to decode (%s)\n",
213                                 vdp_get_error_string( status ) );
214                 }
215                 else
216                 {
217                         self->vdpau->is_decoded = 1;
218                 }
219         }
220 }
221
222 static int vdpau_decoder_init( producer_avformat self )
223 {
224         mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "vdpau_decoder_init\n" );
225         int success = 1;
226         
227         self->video_codec->opaque = self;
228         self->video_codec->get_format = vdpau_get_format;
229         self->video_codec->get_buffer = vdpau_get_buffer;
230         self->video_codec->release_buffer = vdpau_release_buffer;
231         self->video_codec->draw_horiz_band = vdpau_draw_horiz;
232         self->video_codec->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
233         self->video_codec->pix_fmt = PIX_FMT_VDPAU_H264;
234         
235         VdpDecoderProfile profile = VDP_DECODER_PROFILE_H264_HIGH;
236         uint32_t max_references = self->video_codec->refs;
237         pthread_mutex_lock( &mlt_sdl_mutex );
238         VdpStatus status = vdp_decoder_create( self->vdpau->device,
239                 profile, self->video_codec->width, self->video_codec->height, max_references, &self->vdpau->decoder );
240         pthread_mutex_unlock( &mlt_sdl_mutex );
241         
242         if ( status == VDP_STATUS_OK )
243         {
244                         int i, n = FFMIN( self->video_codec->refs + 2, MAX_VDPAU_SURFACES );
245
246                         self->vdpau->deque = mlt_deque_init();
247                         for ( i = 0; i < n; i++ )
248                         {
249                                 if ( VDP_STATUS_OK == vdp_surface_create( self->vdpau->device, VDP_CHROMA_TYPE_420,
250                                         self->video_codec->width, self->video_codec->height, &self->vdpau->render_states[i].surface ) )
251                                 {
252                                         mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "successfully created VDPAU surface %x\n",
253                                                 self->vdpau->render_states[i].surface );
254                                         mlt_deque_push_back( self->vdpau->deque, &self->vdpau->render_states[i] );
255                                 }
256                                 else
257                                 {
258                                         mlt_log_info( MLT_PRODUCER_SERVICE(self->parent), "failed to create VDPAU surface %dx%d\n",
259                                                 self->video_codec->width, self->video_codec->height );
260                                         while ( mlt_deque_count( self->vdpau->deque ) )
261                                         {
262                                                 struct vdpau_render_state *render = mlt_deque_pop_front( self->vdpau->deque );
263                                                 vdp_surface_destroy( render->surface );
264                                         }
265                                         mlt_deque_close( self->vdpau->deque );
266                                         success = 0;
267                                         break;
268                                 }
269                         }
270                         if ( self->vdpau )
271                                 self->vdpau->b_age = self->vdpau->ip_age[0] = self->vdpau->ip_age[1] = 256*256*256*64; // magic from Avidemux
272         }
273         else
274         {
275                 success = 0;
276                 self->vdpau->decoder = VDP_INVALID_HANDLE;
277                 mlt_log_error( MLT_PRODUCER_SERVICE(self->parent), "VDPAU failed to initialize decoder (%s)\n",
278                         vdp_get_error_string( status ) );
279         }
280         
281         return success;
282 }
283
284 static void vdpau_producer_close( producer_avformat self )
285 {
286         if ( self->vdpau )
287         {
288                 mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "vdpau_producer_close\n" );
289                 int i;
290                 for ( i = 0; i < MAX_VDPAU_SURFACES; i++ )
291                 {
292                         if ( self->vdpau->render_states[i].surface != VDP_INVALID_HANDLE )
293                                 vdp_surface_destroy( self->vdpau->render_states[i].surface );
294                         self->vdpau->render_states[i].surface = VDP_INVALID_HANDLE;
295                 }
296
297                 mlt_deque_close( self->vdpau->deque );
298                 if ( self->vdpau->buffer )
299                         mlt_pool_release( self->vdpau->buffer );
300                 self->vdpau->buffer = NULL;
301
302                 vdpau_fini( self );
303         }
304 }