]> git.sesse.net Git - mlt/blob - src/modules/avformat/vdpau.c
Make VDPAU independent of SDL X11 Display.
[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 <vdpau.h>
22 #include <X11/Xlib.h>
23
24 extern pthread_mutex_t mlt_sdl_mutex;
25
26 static VdpGetProcAddress       *vdp_get_proc_address;
27 static VdpGetErrorString       *vdp_get_error_string;
28 static VdpGetApiVersion        *vdp_get_api_version;
29 static VdpGetInformationString *vdp_get_information_string;
30 static VdpVideoSurfaceCreate   *vdp_surface_create;
31 static VdpVideoSurfaceDestroy  *vdp_surface_destroy;
32 static VdpVideoSurfaceGetBitsYCbCr *vdp_surface_get_bits;
33 static VdpDecoderCreate        *vdp_decoder_create;
34 static VdpDecoderDestroy       *vdp_decoder_destroy;
35 static VdpDecoderRender        *vdp_decoder_render;
36
37 struct
38 {
39         VdpDevice device;
40         VdpDecoder decoder;
41         void *producer;
42 } *g_vdpau = NULL;
43
44 /** VDPAUD functions
45 */
46
47 static void vdpau_decoder_close();
48
49 static int vdpau_init( producer_avformat this )
50 {
51         mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "vdpau_init\n" );
52         int success = 0;
53         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this->parent );
54         Display *display = XOpenDisplay( NULL );
55         
56         if ( !display || mlt_properties_get_int( properties, "novdpau" ) )
57                 return success;
58
59         if ( !g_vdpau )
60         {
61                 int flags = RTLD_NOW;
62                 void *object = dlopen( "/usr/lib/libvdpau.so", flags );
63                 
64                 if ( object )
65                 {
66                         VdpDeviceCreateX11 *create_device = dlsym( object, "vdp_device_create_x11" );
67                         if ( create_device )
68                         {
69                                 int screen = mlt_properties_get_int( properties, "x11_screen" );
70                                 VdpDevice device;
71                                 
72                                 mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "X11 Display = %p\n", display );
73                                 if ( VDP_STATUS_OK == create_device( display, screen, &device, &vdp_get_proc_address ) )
74                                 {
75                                         // Allocate the global VDPAU context
76                                         g_vdpau = calloc( 1, sizeof( *g_vdpau ) );
77                                         if ( g_vdpau )
78                                         {
79                                                 g_vdpau->device = device;
80                                                 g_vdpau->decoder = VDP_INVALID_HANDLE;
81                                                 g_vdpau->producer = this;
82                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_GET_ERROR_STRING, (void**) &vdp_get_error_string );
83                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_GET_API_VERSION, (void**) &vdp_get_api_version );
84                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_GET_INFORMATION_STRING, (void**) &vdp_get_information_string );
85                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_VIDEO_SURFACE_CREATE, (void**) &vdp_surface_create );
86                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, (void**) &vdp_surface_destroy );
87                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, (void**) &vdp_surface_get_bits );
88                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_DECODER_CREATE, (void**) &vdp_decoder_create );
89                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_DECODER_DESTROY, (void**) &vdp_decoder_destroy );
90                                                 vdp_get_proc_address( g_vdpau->device, VDP_FUNC_ID_DECODER_RENDER, (void**) &vdp_decoder_render );
91                                                 success = 1;
92                                         }
93                                 }
94                         }
95                         if ( !success )
96                         {
97                                 mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "VDPAU failed to initialize device\n" );
98                                 dlclose( object );
99                         }
100                 }
101         }
102         else
103         {
104                 success = 1;
105                 mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "VDPAU already initialized\n" );
106         }
107         
108         if ( g_vdpau && g_vdpau->producer != this )
109                 vdpau_decoder_close();
110         
111         return success;
112 }
113
114 static enum PixelFormat vdpau_get_format( struct AVCodecContext *s, const enum PixelFormat *fmt )
115 {
116         return PIX_FMT_VDPAU_H264;
117 }
118
119 static int vdpau_get_buffer( AVCodecContext *codec_context, AVFrame *frame )
120 {
121         int error = 0;
122         producer_avformat this = codec_context->opaque;
123         mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "vdpau_get_buffer\n" );
124         
125         if ( g_vdpau->producer == this && mlt_deque_count( this->vdpau->deque ) )
126         {
127                 struct vdpau_render_state *render = mlt_deque_pop_front( this->vdpau->deque );
128                 
129                 if ( render )
130                 {
131                         frame->data[0] = (uint8_t*) render;
132                         frame->data[1] = (uint8_t*) render;
133                         frame->data[2] = (uint8_t*) render;
134                         frame->linesize[0] = 0;
135                         frame->linesize[1] = 0;
136                         frame->linesize[2] = 0;
137                         frame->type = FF_BUFFER_TYPE_USER;
138                         render->state = FF_VDPAU_STATE_USED_FOR_REFERENCE;
139                         frame->reordered_opaque = codec_context->reordered_opaque;
140                         if ( frame->reference )
141                         {
142                                 frame->age = this->vdpau->ip_age[0];
143                                 this->vdpau->ip_age[0] = this->vdpau->ip_age[1] + 1;
144                                 this->vdpau->ip_age[1] = 1;
145                                 this->vdpau->b_age++;
146                         }
147                         else
148                         {
149                                 frame->age = this->vdpau->b_age;
150                                 this->vdpau->ip_age[0] ++;
151                                 this->vdpau->ip_age[1] ++;
152                                 this->vdpau->b_age = 1;
153                         }
154                 }
155                 else
156                 {
157                         mlt_log_warning( MLT_PRODUCER_SERVICE(this->parent), "VDPAU surface underrun\n" );
158                         error = -1;
159                 }
160         }
161         else
162         {
163                 mlt_log_warning( MLT_PRODUCER_SERVICE(this->parent), "VDPAU surface underrun\n" );
164                 error = -1;
165         }
166         
167         return error;
168 }
169
170 static void vdpau_release_buffer( AVCodecContext *codec_context, AVFrame *frame )
171 {
172         producer_avformat this = codec_context->opaque;
173         struct vdpau_render_state *render = (struct vdpau_render_state*) frame->data[0];
174         mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "vdpau_release_buffer (%x)\n", render->surface );
175         int i;
176
177         render->state &= ~FF_VDPAU_STATE_USED_FOR_REFERENCE;
178         for ( i = 0; i < 4; i++ )
179                 frame->data[i] = NULL;
180         mlt_deque_push_back( this->vdpau->deque, render );
181 }
182
183 static void vdpau_draw_horiz( AVCodecContext *codec_context, const AVFrame *frame, int offset[4], int y, int type, int height )
184 {
185         producer_avformat this = codec_context->opaque;
186         struct vdpau_render_state *render = (struct vdpau_render_state*) frame->data[0];
187         VdpVideoSurface surface = render->surface;
188         VdpStatus status = vdp_decoder_render( g_vdpau->decoder, surface, (void*) &render->info,
189                 render->bitstream_buffers_used, render->bitstream_buffers );
190         
191         if ( status != VDP_STATUS_OK )
192         {
193                 this->vdpau->is_decoded = 0;
194                 mlt_log_warning( MLT_PRODUCER_SERVICE(this->parent), "VDPAU failed to decode (%s)\n",
195                         vdp_get_error_string( status ) );
196         }
197         else
198         {
199                 this->vdpau->is_decoded = 1;
200         }
201 }
202
203 static int vdpau_decoder_init( producer_avformat this )
204 {
205         mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "vdpau_decoder_init\n" );
206         int success = 1;
207         
208         this->video_codec->opaque = this;
209         this->video_codec->get_format = vdpau_get_format;
210         this->video_codec->get_buffer = vdpau_get_buffer;
211         this->video_codec->release_buffer = vdpau_release_buffer;
212         this->video_codec->draw_horiz_band = vdpau_draw_horiz;
213         this->video_codec->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
214         this->video_codec->pix_fmt = PIX_FMT_VDPAU_H264;
215         
216         VdpDecoderProfile profile = VDP_DECODER_PROFILE_H264_HIGH;
217         uint32_t max_references = this->video_codec->refs;
218         pthread_mutex_lock( &mlt_sdl_mutex );
219         VdpStatus status = vdp_decoder_create( g_vdpau->device,
220                 profile, this->video_codec->width, this->video_codec->height, max_references, &g_vdpau->decoder );
221         pthread_mutex_unlock( &mlt_sdl_mutex );
222         
223         if ( status == VDP_STATUS_OK )
224         {
225                 if ( !this->vdpau )
226                 {
227                         int i, n = FFMIN( this->video_codec->refs + 1, MAX_VDPAU_SURFACES );
228         
229                         this->vdpau = calloc( 1, sizeof( *this->vdpau ) );
230                         this->vdpau->deque = mlt_deque_init();
231                         for ( i = 0; i < n; i++ )
232                         {
233                                 if ( VDP_STATUS_OK == vdp_surface_create( g_vdpau->device, VDP_CHROMA_TYPE_420,
234                                         this->video_codec->width, this->video_codec->height, &this->vdpau->render_states[i].surface ) )
235                                 {
236                                         mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "successfully created VDPAU surface %x\n",
237                                                 this->vdpau->render_states[i].surface );
238                                         mlt_deque_push_back( this->vdpau->deque, &this->vdpau->render_states[i] );
239                                 }
240                                 else
241                                 {
242                                         mlt_log_info( MLT_PRODUCER_SERVICE(this->parent), "failed to create VDPAU surface %dx%d\n",
243                                                 this->video_codec->width, this->video_codec->height );
244                                         while ( mlt_deque_count( this->vdpau->deque ) )
245                                         {
246                                                 struct vdpau_render_state *render = mlt_deque_pop_front( this->vdpau->deque );
247                                                 vdp_surface_destroy( render->surface );
248                                         }
249                                         mlt_deque_close( this->vdpau->deque );
250                                         free( this->vdpau );
251                                         this->vdpau = NULL;
252                                         vdp_decoder_destroy( g_vdpau->decoder );
253                                         g_vdpau->decoder = VDP_INVALID_HANDLE;
254                                         success = 0;
255                                         break;
256                                 }
257                         }
258                         this->vdpau->b_age = this->vdpau->ip_age[0] = this->vdpau->ip_age[1] = 256*256*256*64; // magic from Avidemux
259                 }
260                 g_vdpau->producer = this;
261         }
262         else
263         {
264                 success = 0;
265                 g_vdpau->decoder = VDP_INVALID_HANDLE;
266                 mlt_log_error( MLT_PRODUCER_SERVICE(this->parent), "VDPAU failed to initialize decoder (%s)\n",
267                         vdp_get_error_string( status ) );
268         }
269         
270         return success;
271 }
272
273 static void vdpau_producer_close( producer_avformat this )
274 {
275         if ( this->vdpau )
276         {
277                 mlt_log_debug( MLT_PRODUCER_SERVICE(this->parent), "vdpau_producer_close\n" );
278                 int i;
279                 for ( i = 0; i < MAX_VDPAU_SURFACES; i++ )
280                 {
281                         if ( this->vdpau->render_states[i].surface != VDP_INVALID_HANDLE )
282                                 vdp_surface_destroy( this->vdpau->render_states[i].surface );
283                         this->vdpau->render_states[i].surface = VDP_INVALID_HANDLE;
284                 }
285                 mlt_deque_close( this->vdpau->deque );
286                 if ( this->vdpau->buffer )
287                         mlt_pool_release( this->vdpau->buffer );
288                 this->vdpau->buffer = NULL;
289                 free( this->vdpau );
290                 this->vdpau = NULL;
291         }
292 }
293
294 static void vdpau_decoder_close( )
295 {
296         mlt_log_debug( NULL, "vdpau_decoder_close (%x)\n", g_vdpau->decoder );
297         if ( g_vdpau && g_vdpau->decoder != VDP_INVALID_HANDLE )
298         {
299                 vdp_decoder_destroy( g_vdpau->decoder );
300                 g_vdpau->decoder = VDP_INVALID_HANDLE;
301                 g_vdpau->producer = NULL;
302         }
303         
304 }
305
306 #if 0
307 static void vdpau_close( void *ignored )
308 {
309         mlt_log_debug( NULL, "vdpau_close\n" );
310         if ( g_vdpau )
311         {
312                 vdpau_decoder_close( );
313                 free( g_vdpau );
314                 g_vdpau = NULL;
315         }
316 }
317 #endif