diff options
-rw-r--r-- | TOOLS/vdpau_functions.py | 58 | ||||
-rw-r--r-- | libmpcodecs/vf.h | 1 | ||||
-rw-r--r-- | libmpcodecs/vf_vo.c | 16 | ||||
-rw-r--r-- | libvo/vdpau_template.c | 39 | ||||
-rw-r--r-- | libvo/video_out.c | 51 | ||||
-rw-r--r-- | libvo/video_out.h | 174 | ||||
-rw-r--r-- | libvo/video_out_internal.h | 1 | ||||
-rw-r--r-- | libvo/vo_vdpau.c | 1864 | ||||
-rw-r--r-- | libvo/vo_xv.c | 6 | ||||
-rw-r--r-- | mencoder.c | 1 | ||||
-rw-r--r-- | mp_core.h | 4 | ||||
-rw-r--r-- | mplayer.c | 27 |
12 files changed, 1341 insertions, 901 deletions
diff --git a/TOOLS/vdpau_functions.py b/TOOLS/vdpau_functions.py new file mode 100644 index 0000000000..e628cb00c3 --- /dev/null +++ b/TOOLS/vdpau_functions.py @@ -0,0 +1,58 @@ +# Generate vdpau_template.c + +functions = """ +# get_error_string should be first, because the function lookup loop should +# have it available to print errors for other functions +get_error_string + +bitmap_surface_create +bitmap_surface_destroy +bitmap_surface_put_bits_native +bitmap_surface_query_capabilities +decoder_create +decoder_destroy +decoder_render +device_destroy +generate_csc_matrix GenerateCSCMatrix # CSC completely capitalized +output_surface_create +output_surface_destroy +output_surface_put_bits_indexed +output_surface_put_bits_native +output_surface_render_bitmap_surface +output_surface_render_output_surface +preemption_callback_register +presentation_queue_block_until_surface_idle +presentation_queue_create +presentation_queue_destroy +presentation_queue_display +presentation_queue_target_create_x11 +presentation_queue_target_destroy +video_mixer_create +video_mixer_destroy +video_mixer_render +video_mixer_set_attribute_values +video_mixer_set_feature_enables +video_surface_create +video_surface_destroy +video_surface_put_bits_y_cb_cr +""" + +print(""" +/* List the VDPAU functions used by MPlayer. + * Generated by vdpau_functions.py. + * First argument on each line is the VDPAU function type name, + * second macro name needed to get function address, + * third name MPlayer uses for the function. + */ +""") +for line in functions.splitlines(): + parts = line.split('#')[0].strip().split() + if not parts: + continue # empty/comment line + if len(parts) > 1: + mp_name, vdpau_name = parts + else: + mp_name = parts[0] + vdpau_name = ''.join(part.capitalize() for part in mp_name.split('_')) + macro_name = mp_name.upper() + print('VDP_FUNCTION(Vdp%s, VDP_FUNC_ID_%s, %s)' % (vdpau_name, macro_name, mp_name)) diff --git a/libmpcodecs/vf.h b/libmpcodecs/vf.h index 99321b3afa..099135c5da 100644 --- a/libmpcodecs/vf.h +++ b/libmpcodecs/vf.h @@ -88,7 +88,6 @@ typedef struct vf_seteq_s #define VFCTRL_SCREENSHOT 14 /* Make a screenshot */ #define VFCTRL_INIT_EOSD 15 /* Select EOSD renderer */ #define VFCTRL_DRAW_EOSD 16 /* Render EOSD */ -#define VFCTRL_GET_PTS 17 /* Return last pts value that reached vf_vo*/ #define VFCTRL_SET_DEINTERLACE 18 /* Set deinterlacing status */ #define VFCTRL_GET_DEINTERLACE 19 /* Get deinterlacing status */ /* Hack to make the OSD state object available to vf_expand which accesses diff --git a/libmpcodecs/vf_vo.c b/libmpcodecs/vf_vo.c index 7b3e16224f..c782a5517b 100644 --- a/libmpcodecs/vf_vo.c +++ b/libmpcodecs/vf_vo.c @@ -22,7 +22,6 @@ extern int sub_visibility; extern float sub_delay; struct vf_priv_s { - double pts; struct vo *vo; #ifdef CONFIG_ASS ASS_Renderer *ass_priv; @@ -127,7 +126,7 @@ static int control(struct vf_instance* vf, int request, void* data) case VFCTRL_DRAW_EOSD: { mp_eosd_images_t images = {NULL, 2}; - double pts = vf->priv->pts; + double pts = video_out->next_pts; if (!video_out->config_ok || !vf->priv->ass_priv) return CONTROL_FALSE; if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE)) { mp_eosd_res_t res; @@ -148,11 +147,6 @@ static int control(struct vf_instance* vf, int request, void* data) return vo_control(video_out, VOCTRL_DRAW_EOSD, &images) == VO_TRUE; } #endif - case VFCTRL_GET_PTS: - { - *(double *)data = vf->priv->pts; - return CONTROL_TRUE; - } } return CONTROL_UNKNOWN; } @@ -179,10 +173,9 @@ static void get_image(struct vf_instance* vf, static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){ if(!video_out->config_ok) return 0; // vo not configured? - // record pts (potentially modified by filters) for main loop - vf->priv->pts = pts; // first check, maybe the vo/vf plugin implements draw_image using mpi: - if (vo_control(video_out, VOCTRL_DRAW_IMAGE,mpi)==VO_TRUE) return 1; // done. + if (vo_draw_image(video_out, mpi, pts) >= 0) + return 1; // nope, fallback to old draw_frame/draw_slice: if(!(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK))){ // blit frame: @@ -210,6 +203,9 @@ static void draw_slice(struct vf_instance* vf, static void uninit(struct vf_instance* vf) { if (vf->priv) { + /* Allow VO (which may live on to work with another instance of vf_vo) + * to get rid of numbered-mpi references that will now be invalid. */ + vo_control(video_out, VOCTRL_RESET, NULL); #ifdef CONFIG_ASS if (vf->priv->ass_priv) ass_renderer_done(vf->priv->ass_priv); diff --git a/libvo/vdpau_template.c b/libvo/vdpau_template.c new file mode 100644 index 0000000000..ca1a6f6056 --- /dev/null +++ b/libvo/vdpau_template.c @@ -0,0 +1,39 @@ + +/* List the VDPAU functions used by MPlayer. + * Generated by vdpau_functions.py. + * First argument on each line is the VDPAU function type name, + * second macro name needed to get function address, + * third name MPlayer uses for the function. + */ + +VDP_FUNCTION(VdpGetErrorString, VDP_FUNC_ID_GET_ERROR_STRING, get_error_string) +VDP_FUNCTION(VdpBitmapSurfaceCreate, VDP_FUNC_ID_BITMAP_SURFACE_CREATE, bitmap_surface_create) +VDP_FUNCTION(VdpBitmapSurfaceDestroy, VDP_FUNC_ID_BITMAP_SURFACE_DESTROY, bitmap_surface_destroy) +VDP_FUNCTION(VdpBitmapSurfacePutBitsNative, VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE, bitmap_surface_put_bits_native) +VDP_FUNCTION(VdpBitmapSurfaceQueryCapabilities, VDP_FUNC_ID_BITMAP_SURFACE_QUERY_CAPABILITIES, bitmap_surface_query_capabilities) +VDP_FUNCTION(VdpDecoderCreate, VDP_FUNC_ID_DECODER_CREATE, decoder_create) +VDP_FUNCTION(VdpDecoderDestroy, VDP_FUNC_ID_DECODER_DESTROY, decoder_destroy) +VDP_FUNCTION(VdpDecoderRender, VDP_FUNC_ID_DECODER_RENDER, decoder_render) +VDP_FUNCTION(VdpDeviceDestroy, VDP_FUNC_ID_DEVICE_DESTROY, device_destroy) +VDP_FUNCTION(VdpGenerateCSCMatrix, VDP_FUNC_ID_GENERATE_CSC_MATRIX, generate_csc_matrix) +VDP_FUNCTION(VdpOutputSurfaceCreate, VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, output_surface_create) +VDP_FUNCTION(VdpOutputSurfaceDestroy, VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, output_surface_destroy) +VDP_FUNCTION(VdpOutputSurfacePutBitsIndexed, VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED, output_surface_put_bits_indexed) +VDP_FUNCTION(VdpOutputSurfacePutBitsNative, VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE, output_surface_put_bits_native) +VDP_FUNCTION(VdpOutputSurfaceRenderBitmapSurface, VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE, output_surface_render_bitmap_surface) +VDP_FUNCTION(VdpOutputSurfaceRenderOutputSurface, VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, output_surface_render_output_surface) +VDP_FUNCTION(VdpPreemptionCallbackRegister, VDP_FUNC_ID_PREEMPTION_CALLBACK_REGISTER, preemption_callback_register) +VDP_FUNCTION(VdpPresentationQueueBlockUntilSurfaceIdle, VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE, presentation_queue_block_until_surface_idle) +VDP_FUNCTION(VdpPresentationQueueCreate, VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, presentation_queue_create) +VDP_FUNCTION(VdpPresentationQueueDestroy, VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY, presentation_queue_destroy) +VDP_FUNCTION(VdpPresentationQueueDisplay, VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY, presentation_queue_display) +VDP_FUNCTION(VdpPresentationQueueTargetCreateX11, VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11, presentation_queue_target_create_x11) +VDP_FUNCTION(VdpPresentationQueueTargetDestroy, VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY, presentation_queue_target_destroy) +VDP_FUNCTION(VdpVideoMixerCreate, VDP_FUNC_ID_VIDEO_MIXER_CREATE, video_mixer_create) +VDP_FUNCTION(VdpVideoMixerDestroy, VDP_FUNC_ID_VIDEO_MIXER_DESTROY, video_mixer_destroy) +VDP_FUNCTION(VdpVideoMixerRender, VDP_FUNC_ID_VIDEO_MIXER_RENDER, video_mixer_render) +VDP_FUNCTION(VdpVideoMixerSetAttributeValues, VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES, video_mixer_set_attribute_values) +VDP_FUNCTION(VdpVideoMixerSetFeatureEnables, VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES, video_mixer_set_feature_enables) +VDP_FUNCTION(VdpVideoSurfaceCreate, VDP_FUNC_ID_VIDEO_SURFACE_CREATE, video_surface_create) +VDP_FUNCTION(VdpVideoSurfaceDestroy, VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, video_surface_destroy) +VDP_FUNCTION(VdpVideoSurfacePutBitsYCbCr, VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR, video_surface_put_bits_y_cb_cr) diff --git a/libvo/video_out.c b/libvo/video_out.c index 01c04f3ca6..7a41fcf1d4 100644 --- a/libvo/video_out.c +++ b/libvo/video_out.c @@ -21,6 +21,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <assert.h> +#include <stdbool.h> #include <unistd.h> //#include <sys/mman.h> @@ -31,6 +33,7 @@ #include "video_out.h" #include "aspect.h" #include "geometry.h" +#include "old_vo_wrapper.h" #include "mp_msg.h" #include "help_mp.h" @@ -162,12 +165,12 @@ const struct vo_driver *video_out_drivers[] = #ifdef CONFIG_3DFX &video_out_3dfx, #endif -#ifdef CONFIG_XV - &video_out_xv, -#endif #if CONFIG_VDPAU &video_out_vdpau, #endif +#ifdef CONFIG_XV + &video_out_xv, +#endif #ifdef CONFIG_X11 &video_out_x11, &video_out_xover, @@ -276,11 +279,41 @@ int vo_control(struct vo *vo, uint32_t request, void *data) return vo->driver->control(vo, request, data); } +// Return -1 if driver appears not to support a draw_image interface, +// 0 otherwise (whether the driver actually drew something or not). +int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts) +{ + if (!vo->config_ok) + return 0; + if (vo->driver->buffer_frames) { + vo->driver->draw_image(vo, mpi, pts); + return 0; + } + vo->frame_loaded = true; + vo->next_pts = pts; + if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL) + return -1; + return 0; +} + +int vo_get_buffered_frame(struct vo *vo, bool eof) +{ + if (!vo->config_ok) + return -1; + if (vo->frame_loaded) + return 0; + if (!vo->driver->buffer_frames) + return -1; + vo->driver->get_buffered_frame(vo, eof); + return vo->frame_loaded ? 0 : -1; +} + int vo_draw_frame(struct vo *vo, uint8_t *src[]) { + assert(!vo->driver->is_new); if (!vo->config_ok) return 0; - return vo->driver->draw_frame(vo, src); + return old_vo_draw_frame(vo, src); } int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) @@ -299,6 +332,8 @@ void vo_flip_page(struct vo *vo) { if (!vo->config_ok) return; + vo->frame_loaded = false; + vo->next_pts = (-1LL<<63); // MP_NOPTS_VALUE vo->driver->flip_page(vo); } @@ -309,6 +344,14 @@ void vo_check_events(struct vo *vo) vo->driver->check_events(vo); } +void vo_seek_reset(struct vo *vo) +{ + if (!vo->config_ok) + return; + vo_control(vo, VOCTRL_RESET, NULL); + vo->frame_loaded = false; +} + void vo_destroy(struct vo *vo) { vo->driver->uninit(vo); diff --git a/libvo/video_out.h b/libvo/video_out.h index a55a2f87be..9bbfd3dc41 100644 --- a/libvo/video_out.h +++ b/libvo/video_out.h @@ -24,7 +24,7 @@ #define MPLAYER_VIDEO_OUT_H #include <inttypes.h> -#include <stdarg.h> +#include <stdbool.h> //#include "font_load.h" #include "libmpcodecs/img_format.h" @@ -117,91 +117,98 @@ typedef struct { typedef struct vo_info_s { - /* driver name ("Matrox Millennium G200/G400" */ - const char *name; - /* short name (for config strings) ("mga") */ - const char *short_name; - /* author ("Aaron Holtzman <aholtzma@ess.engr.uvic.ca>") */ - const char *author; - /* any additional comments */ - const char *comment; + /* driver name ("Matrox Millennium G200/G400" */ + const char *name; + /* short name (for config strings) ("mga") */ + const char *short_name; + /* author ("Aaron Holtzman <aholtzma@ess.engr.uvic.ca>") */ + const char *author; + /* any additional comments */ + const char *comment; } vo_info_t; struct vo; struct osd_state; +struct mp_image; struct vo_driver { - // Driver uses new API - int is_new; - - // This is set if the driver is not new and contains pointers to - // old-API functions to be used instead of the ones below. - struct vo_old_functions *old_functions; - - const vo_info_t *info; - /* - * Preinitializes driver (real INITIALIZATION) - * arg - currently it's vo_subdevice - * returns: zero on successful initialization, non-zero on error. - */ - int (*preinit)(struct vo *vo, const char *arg); - /* - * Initialize (means CONFIGURE) the display driver. - * params: - * width,height: image source size - * d_width,d_height: size of the requested window size, just a hint - * fullscreen: flag, 0=windowd 1=fullscreen, just a hint - * title: window title, if available - * format: fourcc of pixel format - * returns : zero on successful initialization, non-zero on error. - */ - int (*config)(struct vo *vo, uint32_t width, uint32_t height, - uint32_t d_width, uint32_t d_height, uint32_t fullscreen, - char *title, uint32_t format); - - /* - * Control interface - */ - int (*control)(struct vo *vo, uint32_t request, void *data); - - /* - * Display a new RGB/BGR frame of the video to the screen. - * params: - * src[0] - pointer to the image - */ - int (*draw_frame)(struct vo *vo, uint8_t *src[]); - - /* - * Draw a planar YUV slice to the buffer: - * params: - * src[3] = source image planes (Y,U,V) - * stride[3] = source image planes line widths (in bytes) - * w,h = width*height of area to be copied (in Y pixels) - * x,y = position at the destination image (in Y pixels) - */ - int (*draw_slice)(struct vo *vo, uint8_t *src[], int stride[], int w, - int h, int x, int y); - - /* - * Draws OSD to the screen buffer - */ - void (*draw_osd)(struct vo *vo, struct osd_state *osd); - - /* - * Blit/Flip buffer to the screen. Must be called after each frame! - */ - void (*flip_page)(struct vo *vo); - - /* - * This func is called after every frames to handle keyboard and - * other events. It's called in PAUSE mode too! - */ - void (*check_events)(struct vo *vo); - - /* - * Closes driver. Should restore the original state of the system. - */ - void (*uninit)(struct vo *vo); + // Driver uses new API + bool is_new; + // Driver buffers or adds (deinterlace) frames and will keep track + // of pts values itself + bool buffer_frames; + + // This is set if the driver is not new and contains pointers to + // old-API functions to be used instead of the ones below. + struct vo_old_functions *old_functions; + + const vo_info_t *info; + /* + * Preinitializes driver (real INITIALIZATION) + * arg - currently it's vo_subdevice + * returns: zero on successful initialization, non-zero on error. + */ + int (*preinit)(struct vo *vo, const char *arg); + /* + * Initialize (means CONFIGURE) the display driver. + * params: + * width,height: image source size + * d_width,d_height: size of the requested window size, just a hint + * fullscreen: flag, 0=windowd 1=fullscreen, just a hint + * title: window title, if available + * format: fourcc of pixel format + * returns : zero on successful initialization, non-zero on error. + */ + int (*config)(struct vo *vo, uint32_t width, uint32_t height, + uint32_t d_width, uint32_t d_height, uint32_t fullscreen, + char *title, uint32_t format); + + /* + * Control interface + */ + int (*control)(struct vo *vo, uint32_t request, void *data); + + void (*draw_image)(struct vo *vo, struct mp_image *mpi, double pts); + + /* + * Get extra frames from the VO, such as those added by VDPAU + * deinterlace. Preparing the next such frame if any could be done + * automatically by the VO after a previous flip_page(), but having + * it as a separate step seems to allow making code more robust. + */ + void (*get_buffered_frame)(struct vo *vo, bool eof); + + /* + * Draw a planar YUV slice to the buffer: + * params: + * src[3] = source image planes (Y,U,V) + * stride[3] = source image planes line widths (in bytes) + * w,h = width*height of area to be copied (in Y pixels) + * x,y = position at the destination image (in Y pixels) + */ + int (*draw_slice)(struct vo *vo, uint8_t *src[], int stride[], int w, + int h, int x, int y); + + /* + * Draws OSD to the screen buffer + */ + void (*draw_osd)(struct vo *vo, struct osd_state *osd); + + /* + * Blit/Flip buffer to the screen. Must be called after each frame! + */ + void (*flip_page)(struct vo *vo); + + /* + * This func is called after every frames to handle keyboard and + * other events. It's called in PAUSE mode too! + */ + void (*check_events)(struct vo *vo); + + /* + * Closes driver. Should restore the original state of the system. + */ + void (*uninit)(struct vo *vo); }; struct vo_old_functions { @@ -221,6 +228,10 @@ struct vo_old_functions { struct vo { int config_ok; // Last config call was successful? int config_count; // Total number of successful config calls + + bool frame_loaded; // Is there a next frame the VO could flip to? + double next_pts; // pts value of the next frame if any + const struct vo_driver *driver; void *priv; struct MPOpts *opts; @@ -258,11 +269,14 @@ int vo_config(struct vo *vo, uint32_t width, uint32_t height, void list_video_out(void); int vo_control(struct vo *vo, uint32_t request, void *data); +int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts); +int vo_get_buffered_frame(struct vo *vo, bool eof); int vo_draw_frame(struct vo *vo, uint8_t *src[]); int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y); void vo_draw_osd(struct vo *vo, struct osd_state *osd); void vo_flip_page(struct vo *vo); void vo_check_events(struct vo *vo); +void vo_seek_reset(struct vo *vo); void vo_destroy(struct vo *vo); diff --git a/libvo/video_out_internal.h b/libvo/video_out_internal.h index 166a91cc1c..8595d4ef05 100644 --- a/libvo/video_out_internal.h +++ b/libvo/video_out_internal.h @@ -50,7 +50,6 @@ static int preinit(const char *); .preinit = old_vo_preinit,\ .config = old_vo_config,\ .control = old_vo_control,\ - .draw_frame = old_vo_draw_frame,\ .draw_slice = old_vo_draw_slice,\ .draw_osd = old_vo_draw_osd,\ .flip_page = old_vo_flip_page,\ diff --git a/libvo/vo_vdpau.c b/libvo/vo_vdpau.c index 486a6e699f..e613205834 100644 --- a/libvo/vo_vdpau.c +++ b/libvo/vo_vdpau.c @@ -20,58 +20,56 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -/** - * \defgroup VDPAU_Presentation VDPAU Presentation - * \ingroup Decoder - * +/* * Actual decoding and presentation are implemented here. * All necessary frame information is collected through * the "vdpau_render_state" structure after parsing all headers * etc. in libavcodec for different codecs. - * - * @{ */ #include <stdio.h> +#include <stdlib.h> #include <dlfcn.h> +#include <stdint.h> +#include <stdbool.h> #include "config.h" #include "mp_msg.h" +#include "options.h" +#include "talloc.h" #include "video_out.h" -#include "video_out_internal.h" #include "x11_common.h" #include "aspect.h" #include "sub.h" #include "subopt-helper.h" -#include "font_load.h" +#include "libmpcodecs/vfcap.h" +#include "libmpcodecs/mp_image.h" +#include "osdep/timer.h" #include "libavcodec/vdpau.h" +#include "font_load.h" + #include "libavutil/common.h" #include "libavutil/mathematics.h" #include "ass_mp.h" -static vo_info_t info = { - "VDPAU with X11", - "vdpau", - "Rajib Mahapatra <rmahapatra@nvidia.com> and others", - "" -}; - -LIBVO_EXTERN(vdpau) - #define CHECK_ST_ERROR(message) \ - if (vdp_st != VDP_STATUS_OK) { \ - mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \ - message, vdp_get_error_string(vdp_st)); \ - return -1; \ - } + do { \ + if (vdp_st != VDP_STATUS_OK) { \ + mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \ + message, vdp->get_error_string(vdp_st)); \ + return -1; \ + } \ + } while (0) #define CHECK_ST_WARNING(message) \ - if (vdp_st != VDP_STATUS_OK) \ - mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] %s: %s\n", \ - message, vdp_get_error_string(vdp_st)); + do { \ + if (vdp_st != VDP_STATUS_OK) \ + mp_msg(MSGT_VO, MSGL_WARN, "[ vdpau] %s: %s\n", \ + message, vdp->get_error_string(vdp_st)); \ + } while (0) /* number of video and output surfaces */ #define NUM_OUTPUT_SURFACES 2 @@ -80,423 +78,456 @@ LIBVO_EXTERN(vdpau) /* number of palette entries */ #define PALETTE_SIZE 256 -/* Initial maximum number of EOSD surfaces */ -#define EOSD_SURFACES_INITIAL 512 +/* Initial size of EOSD surface in pixels (x*x) */ +#define EOSD_SURFACE_INITIAL_SIZE 256 /* * Global variable declaration - VDPAU specific */ -/* Declaration for all variables of win_x11_init_vdpau_procs() and - * win_x11_init_vdpau_flip_queue() functions - */ -static VdpDevice vdp_device; -static VdpDeviceCreateX11 *vdp_device_create; -static VdpGetProcAddress *vdp_get_proc_address; +struct vdp_functions { +#define VDP_FUNCTION(vdp_type, _, mp_name) vdp_type *mp_name; +#include "vdpau_template.c" +#undef VDP_FUNCTION +}; -static VdpPresentationQueueTarget vdp_flip_target; -static VdpPresentationQueue vdp_flip_queue; +struct vdpctx { + struct vdp_functions *vdp; + + VdpDevice vdp_device; + bool is_preempted; + bool preemption_acked; + bool preemption_user_notified; + unsigned int last_preemption_retry_fail; + VdpDeviceCreateX11 *vdp_device_create; + VdpGetProcAddress *vdp_get_proc_address; + + VdpPresentationQueueTarget flip_target; + VdpPresentationQueue flip_queue; + + void *vdpau_lib_handle; + + /* output_surfaces[NUM_OUTPUT_SURFACES] is misused for OSD. */ +#define osd_surface vc->output_surfaces[NUM_OUTPUT_SURFACES] + VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES + 1]; + VdpVideoSurface deint_surfaces[3]; + double deint_pts[3]; + int deint_queue_pos; + mp_image_t *deint_mpi[3]; + int output_surface_width, output_surface_height; + + VdpVideoMixer video_mixer; + int deint; + int deint_type; + int deint_counter; + int pullup; + float denoise; + float sharpen; + int chroma_deint; + int top_field_first; + + VdpDecoder decoder; + int decoder_max_refs; + + VdpRect src_rect_vid; + VdpRect out_rect_vid; + int border_x, border_y; + + struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES]; + int surface_num; + uint32_t vid_width, vid_height; + uint32_t image_format; + VdpChromaType vdp_chroma_type; + VdpYCbCrFormat vdp_pixel_format; + + /* draw_osd */ + unsigned char *index_data; + int index_data_size; + uint32_t palette[PALETTE_SIZE]; + + // EOSD + // Pool of surfaces + struct eosd_bitmap_surface { + VdpBitmapSurface surface; + int w; + int h; + uint32_t max_width; + uint32_t max_height; + } eosd_surface; + + // List of surfaces to be rendered + struct eosd_target { + VdpRect source; + VdpRect dest; + VdpColor color; + } *eosd_targets; + int eosd_targets_size; + int *eosd_scratch; + + int eosd_render_count; + + // Video equalizer + VdpProcamp procamp; + + bool visible_buf; + bool paused; + + // These tell what's been initialized and uninit() should free/uninitialize + bool mode_switched; +}; -static VdpDeviceDestroy *vdp_device_destroy; -static VdpVideoSurfaceCreate *vdp_video_surface_create; -static VdpVideoSurfaceDestroy *vdp_video_surface_destroy; -static VdpGetErrorString *vdp_get_error_string; +static void flip_page(struct vo *vo); +static int video_to_output_surface(struct vo *vo) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + VdpTime dummy; + VdpStatus vdp_st; + if (vc->deint_queue_pos < 0) + return -1; -/* May be used in software filtering/postprocessing options - * in MPlayer (./mplayer -vf ..) if we copy video_surface data to - * system memory. - */ -static VdpVideoSurfacePutBitsYCbCr *vdp_video_surface_put_bits_y_cb_cr; -static VdpOutputSurfacePutBitsNative *vdp_output_surface_put_bits_native; - -static VdpOutputSurfaceCreate *vdp_output_surface_create; -static VdpOutputSurfaceDestroy *vdp_output_surface_destroy; - -/* VideoMixer puts video_surface data on displayable output_surface. */ -static VdpVideoMixerCreate *vdp_video_mixer_create; -static VdpVideoMixerDestroy *vdp_video_mixer_destroy; -static VdpVideoMixerRender *vdp_video_mixer_render; -static VdpVideoMixerSetFeatureEnables *vdp_video_mixer_set_feature_enables; -static VdpVideoMixerSetAttributeValues *vdp_video_mixer_set_attribute_values; - -static VdpPresentationQueueTargetDestroy *vdp_presentation_queue_target_destroy; -static VdpPresentationQueueCreate *vdp_presentation_queue_create; -static VdpPresentationQueueDestroy *vdp_presentation_queue_destroy; -static VdpPresentationQueueDisplay *vdp_presentation_queue_display; -static VdpPresentationQueueBlockUntilSurfaceIdle *vdp_presentation_queue_block_until_surface_idle; -static VdpPresentationQueueTargetCreateX11 *vdp_presentation_queue_target_create_x11; - -static VdpOutputSurfaceRenderOutputSurface *vdp_output_surface_render_output_surface; -static VdpOutputSurfacePutBitsIndexed *vdp_output_surface_put_bits_indexed; -static VdpOutputSurfaceRenderBitmapSurface *vdp_output_surface_render_bitmap_surface; - -static VdpBitmapSurfaceCreate *vdp_bitmap_surface_create; -static VdpBitmapSurfaceDestroy *vdp_bitmap_surface_destroy; -static VdpBitmapSurfacePutBitsNative *vdp_bitmap_surface_putbits_native; - -static VdpDecoderCreate *vdp_decoder_create; -static VdpDecoderDestroy *vdp_decoder_destroy; -static VdpDecoderRender *vdp_decoder_render; - -static VdpGenerateCSCMatrix *vdp_generate_csc_matrix; - -static void *vdpau_lib_handle; -/* output_surfaces[NUM_OUTPUT_SURFACES] is misused for OSD. */ -#define osd_surface output_surfaces[NUM_OUTPUT_SURFACES] -static VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES + 1]; -static VdpVideoSurface deint_surfaces[3]; -static mp_image_t *deint_mpi[2]; -static int output_surface_width, output_surface_height; - -static VdpVideoMixer video_mixer; -static int deint; -static int deint_type; -static int deint_counter; -static int deint_buffer_past_frames; -static int pullup; -static float denoise; -static float sharpen; -static int chroma_deint; -static int top_field_first; - -static VdpDecoder decoder; -static int decoder_max_refs; - -static VdpRect src_rect_vid; -static VdpRect out_rect_vid; -static int border_x, border_y; - -static struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES]; -static int surface_num; -static int vid_surface_num; -static uint32_t vid_width, vid_height; -static uint32_t image_format; -static VdpChromaType vdp_chroma_type; -static VdpYCbCrFormat vdp_pixel_format; - -/* draw_osd */ -static unsigned char *index_data; -static int index_data_size; -static uint32_t palette[PALETTE_SIZE]; - -// EOSD -// Pool of surfaces -struct { - VdpBitmapSurface surface; - int w; - int h; - char in_use; -} *eosd_surfaces; - -// List of surfaces to be rendered -struct { - VdpBitmapSurface surface; - VdpRect source; - VdpRect dest; - VdpColor color; -} *eosd_targets; - -static int eosd_render_count; -static int eosd_surface_count; - -// Video equalizer -static VdpProcamp procamp; + int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME; + unsigned int dp = vc->deint_queue_pos; + // dp==0 means last field of latest frame, 1 earlier field of latest frame, + // 2 last field of previous frame and so on + if (vc->deint) { + field = vc->top_field_first ^ (dp & 1) ? + VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD: + VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD; + } + VdpVideoSurface *q = vc->deint_surfaces; + const VdpVideoSurface *past_fields = (const VdpVideoSurface []){ + q[(dp+1)/2], q[(dp+2)/2]}; + const VdpVideoSurface *future_fields = (const VdpVideoSurface []){ + q[(dp-1)/2]}; + VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num]; + vdp_st = vdp->presentation_queue_block_until_surface_idle(vc->flip_queue, + output_surface, + &dummy); + CHECK_ST_WARNING("Error when calling " + "vdp_presentation_queue_block_until_surface_idle"); + + vdp_st = vdp->video_mixer_render(vc->video_mixer, VDP_INVALID_HANDLE, + 0, field, 2, past_fields, + vc->deint_surfaces[dp/2], 1, future_fields, + &vc->src_rect_vid, output_surface, + NULL, &vc->out_rect_vid, 0, NULL); + CHECK_ST_WARNING("Error when calling vdp_video_mixer_render"); + return 0; +} -/* - * X11 specific - */ -static int visible_buf; -static int int_pause; +static void add_new_video_surface(struct vo *vo, VdpVideoSurface surface, + struct mp_image *reserved_mpi, double pts) +{ + struct vdpctx *vc = vo->priv; -static void draw_eosd(void); + if (reserved_mpi) + reserved_mpi->usage_count++; + if (vc->deint_mpi[2]) + vc->deint_mpi[2]->usage_count--; -static void push_deint_surface(VdpVideoSurface surface) -{ - deint_surfaces[2] = deint_surfaces[1]; - deint_surfaces[1] = deint_surfaces[0]; - deint_surfaces[0] = surface; + for (int i = 2; i > 0; i--) { + vc->deint_mpi[i] = vc->deint_mpi[i - 1]; + vc->deint_surfaces[i] = vc->deint_surfaces[i - 1]; + vc->deint_pts[i] = vc->deint_pts[i - 1]; + } + vc->deint_mpi[0] = reserved_mpi; + vc->deint_surfaces[0] = surface; + vc->deint_pts[0] = pts; + + vo->frame_loaded = true; + vo->next_pts = pts; + if (vc->deint >= 2 && vc->deint_queue_pos >= 0) { + vc->deint_queue_pos = 2; + double diff = vc->deint_pts[0] - vc->deint_pts[1]; + if (diff > 0 && diff < 0.5) + vo->next_pts = (vc->deint_pts[0] + vc->deint_pts[1]) / 2; + else + vo->next_pts = vc->deint_pts[1]; + } else + vc->deint_queue_pos = 1; + video_to_output_surface(vo); } -static void video_to_output_surface(void) +static void forget_frames(struct vo *vo) { - VdpTime dummy; - VdpStatus vdp_st; - int i; - if (vid_surface_num < 0) - return; - - if (deint < 2 || deint_surfaces[0] == VDP_INVALID_HANDLE) - push_deint_surface(surface_render[vid_surface_num].surface); - - for (i = 0; i <= !!(deint > 1); i++) { - int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME; - VdpOutputSurface output_surface; - if (i) { - draw_eosd(); - draw_osd(); - flip_page(); - } - if (deint) - field = (top_field_first == i) ^ (deint > 1) ? - VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD: - VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD; - output_surface = output_surfaces[surface_num]; - vdp_st = vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue, - output_surface, - &dummy); - CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle") - - vdp_st = vdp_video_mixer_render(video_mixer, VDP_INVALID_HANDLE, 0, - field, 2, deint_surfaces + 1, - deint_surfaces[0], - 1, &surface_render[vid_surface_num].surface, - &src_rect_vid, - output_surface, - NULL, &out_rect_vid, 0, NULL); - CHECK_ST_WARNING("Error when calling vdp_video_mixer_render") - push_deint_surface(surface_render[vid_surface_num].surface); + struct vdpctx *vc = vo->priv; + + vc->deint_queue_pos = -1; + for (int i = 0; i < 3; i++) { + vc->deint_surfaces[i] = VDP_INVALID_HANDLE; + if (vc->deint_mpi[i]) + vc->deint_mpi[i]->usage_count--; + vc->deint_mpi[i] = NULL; } } -static void resize(void) +static void resize(struct vo *vo) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; int i; struct vo_rect src_rect; struct vo_rect dst_rect; struct vo_rect borders; - calc_src_dst_rects(vid_width, vid_height, &src_rect, &dst_rect, &borders, NULL); - out_rect_vid.x0 = dst_rect.left; - out_rect_vid.x1 = dst_rect.right; - out_rect_vid.y0 = dst_rect.top; - out_rect_vid.y1 = dst_rect.bottom; - src_rect_vid.x0 = src_rect.left; - src_rect_vid.x1 = src_rect.right; - src_rect_vid.y0 = src_rect.top; - src_rect_vid.y1 = src_rect.bottom; - border_x = borders.left; - border_y = borders.top; + calc_src_dst_rects(vo, vc->vid_width, vc->vid_height, &src_rect, &dst_rect, + &borders, NULL); + vc->out_rect_vid.x0 = dst_rect.left; + vc->out_rect_vid.x1 = dst_rect.right; + vc->out_rect_vid.y0 = dst_rect.top; + vc->out_rect_vid.y1 = dst_rect.bottom; + vc->src_rect_vid.x0 = src_rect.left; + vc->src_rect_vid.x1 = src_rect.right; + vc->src_rect_vid.y0 = src_rect.top; + vc->src_rect_vid.y1 = src_rect.bottom; + vc->border_x = borders.left; + vc->border_y = borders.top; #ifdef CONFIG_FREETYPE // adjust font size to display size force_load_font = 1; #endif vo_osd_changed(OSDTYPE_OSD); - if (output_surface_width < vo_dwidth || output_surface_height < vo_dheight) { - if (output_surface_width < vo_dwidth) { - output_surface_width += output_surface_width >> 1; - output_surface_width = FFMAX(output_surface_width, vo_dwidth); + if (vc->output_surface_width < vo->dwidth + || vc->output_surface_height < vo->dheight) { + if (vc->output_surface_width < vo->dwidth) { + vc->output_surface_width += vc->output_surface_width >> 1; + vc->output_surface_width = FFMAX(vc->output_surface_width, + vo->dwidth); } - if (output_surface_height < vo_dheight) { - output_surface_height += output_surface_height >> 1; - output_surface_height = FFMAX(output_surface_height, vo_dheight); + if (vc->output_surface_height < vo->dheight) { + vc->output_surface_height += vc->output_surface_height >> 1; + vc->output_surface_height = FFMAX(vc->output_surface_height, + vo->dheight); } // Creation of output_surfaces for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) { - if (output_surfaces[i] != VDP_INVALID_HANDLE) - vdp_output_surface_destroy(output_surfaces[i]); - vdp_st = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8, - output_surface_width, output_surface_height, - &output_surfaces[i]); - CHECK_ST_WARNING("Error when calling vdp_output_surface_create") - mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", output_surfaces[i]); + if (vc->output_surfaces[i] != VDP_INVALID_HANDLE) + vdp->output_surface_destroy(vc->output_surfaces[i]); + vdp_st = vdp->output_surface_create(vc->vdp_device, + VDP_RGBA_FORMAT_B8G8R8A8, + vc->output_surface_width, + vc->output_surface_height, + &vc->output_surfaces[i]); + CHECK_ST_WARNING("Error when calling vdp_output_surface_create"); + mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", + vc->output_surfaces[i]); } } - video_to_output_surface(); - if (visible_buf) - flip_page(); + if (vc->paused && vc->visible_buf) + if (video_to_output_surface(vo) >= 0) + flip_page(vo); +} + +static void preemption_callback(VdpDevice device, void *context) +{ + struct vdpctx *vc = context; + vc->is_preempted = true; + vc->preemption_acked = false; } /* Initialize vdp_get_proc_address, called from preinit() */ -static int win_x11_init_vdpau_procs(void) +static int win_x11_init_vdpau_procs(struct vo *vo) { + struct vo_x11_state *x11 = vo->x11; + struct vdpctx *vc = vo->priv; + talloc_free(vc->vdp); // In case this is reinitialization after preemption + struct vdp_functions *vdp = talloc_zero(vc, struct vdp_functions); + vc->vdp = vdp; VdpStatus vdp_st; struct vdp_function { const int id; - void *pointer; + int offset; }; const struct vdp_function *dsc; static const struct vdp_function vdp_func[] = { - {VDP_FUNC_ID_GET_ERROR_STRING, &vdp_get_error_string}, - {VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy}, - {VDP_FUNC_ID_VIDEO_SURFACE_CREATE, &vdp_video_surface_create}, - {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, &vdp_video_surface_destroy}, - {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR, - &vdp_video_surface_put_bits_y_cb_cr}, - {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE, - &vdp_output_surface_put_bits_native}, - {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create}, - {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy}, - {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create}, - {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy}, - {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render}, - {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES, - &vdp_video_mixer_set_feature_enables}, - {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES, - &vdp_video_mixer_set_attribute_values}, - {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY, - &vdp_presentation_queue_target_destroy}, - {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, &vdp_presentation_queue_create}, - {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY, - &vdp_presentation_queue_destroy}, - {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY, - &vdp_presentation_queue_display}, - {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE, - &vdp_presentation_queue_block_until_surface_idle}, - {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11, - &vdp_presentation_queue_target_create_x11}, - {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, - &vdp_output_surface_render_output_surface}, - {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED, - &vdp_output_surface_put_bits_indexed}, - {VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create}, - {VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render}, - {VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy}, - {VDP_FUNC_ID_BITMAP_SURFACE_CREATE, &vdp_bitmap_surface_create}, - {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY, &vdp_bitmap_surface_destroy}, - {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE, - &vdp_bitmap_surface_putbits_native}, - {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE, - &vdp_output_surface_render_bitmap_surface}, - {VDP_FUNC_ID_GENERATE_CSC_MATRIX, &vdp_generate_csc_matrix}, - {0, NULL} +#define VDP_FUNCTION(_, macro_name, mp_name) {macro_name, offsetof(struct vdp_functions, mp_name)}, +#include "vdpau_template.c" +#undef VDP_FUNCTION + {0, -1} }; - vdp_st = vdp_device_create(mDisplay, mScreen, - &vdp_device, &vdp_get_proc_address); + vdp_st = vc->vdp_device_create(x11->display, x11->screen,&vc->vdp_device, + &vc->vdp_get_proc_address); if (vdp_st != VDP_STATUS_OK) { - mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st); + mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling " + "vdp_device_create_x11: %i\n", vdp_st); return -1; } - vdp_get_error_string = NULL; - for (dsc = vdp_func; dsc->pointer; dsc++) { - vdp_st = vdp_get_proc_address(vdp_device, dsc->id, dsc->pointer); + vdp->get_error_string = NULL; + for (dsc = vdp_func; dsc->offset >= 0; dsc++) { + vdp_st = vc->vdp_get_proc_address(vc->vdp_device, dsc->id, + (void **)((char *)vdp + dsc->offset)); if (vdp_st != VDP_STATUS_OK) { - mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_get_proc_address(function id %d): %s\n", dsc->id, vdp_get_error_string ? vdp_get_error_string(vdp_st) : "?"); + mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling " + "vdp_get_proc_address(function id %d): %s\n", dsc->id, + vdp->get_error_string ? vdp->get_error_string(vdp_st) : "?"); return -1; } } + vdp_st = vdp->preemption_callback_register(vc->vdp_device, + preemption_callback, vc); return 0; } -/* Initialize vdpau_flip_queue, called from config() */ -static int win_x11_init_vdpau_flip_queue(void) +static int win_x11_init_vdpau_flip_queue(struct vo *vo) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + struct vo_x11_state *x11 = vo->x11; VdpStatus vdp_st; - vdp_st = vdp_presentation_queue_target_create_x11(vdp_device, vo_window, - &vdp_flip_target); - CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11") + if (vc->flip_target == VDP_INVALID_HANDLE) { + vdp_st = vdp->presentation_queue_target_create_x11(vc->vdp_device, + x11->window, + &vc->flip_target); + CHECK_ST_ERROR("Error when calling " + "vdp_presentation_queue_target_create_x11"); + } - vdp_st = vdp_presentation_queue_create(vdp_device, vdp_flip_target, - &vdp_flip_queue); - CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create") + /* Emperically this seems to be the first call which fails when we + * try to reinit after preemption while the user is still switched + * from X to a virtual terminal (creating the vdp_device initially + * succeeds, as does creating the flip_target above). This is + * probably not guaranteed behavior, but we'll assume it as a simple + * way to reduce warnings while trying to recover from preemption. + */ + if (vc->flip_queue == VDP_INVALID_HANDLE) { + vdp_st = vdp->presentation_queue_create(vc->vdp_device, vc->flip_target, + &vc->flip_queue); + if (vc->is_preempted && vdp_st != VDP_STATUS_OK) { + mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Failed to create flip queue " + "while preempted: %s\n", vdp->get_error_string(vdp_st)); + return -1; + } else + CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create"); + } return 0; } -static int create_vdp_mixer(VdpChromaType vdp_chroma_type) { +static int create_vdp_mixer(struct vo *vo, VdpChromaType vdp_chroma_type) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; #define VDP_NUM_MIXER_PARAMETER 3 #define MAX_NUM_FEATURES 5 int i; VdpStatus vdp_st; + + if (vc->video_mixer != VDP_INVALID_HANDLE) + return 0; + int feature_count = 0; VdpVideoMixerFeature features[MAX_NUM_FEATURES]; VdpBool feature_enables[MAX_NUM_FEATURES]; - static const VdpVideoMixerAttribute denoise_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL}; - const void * const denoise_value[] = {&denoise}; - static const VdpVideoMixerAttribute sharpen_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL}; - const void * const sharpen_value[] = {&sharpen}; - static const VdpVideoMixerAttribute skip_chroma_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE}; + static const VdpVideoMixerAttribute denoise_attrib[] = + {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL}; + const void * const denoise_value[] = {&vc->denoise}; + static const VdpVideoMixerAttribute sharpen_attrib[] = + {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL}; + const void * const sharpen_value[] = {&vc->sharpen}; + static const VdpVideoMixerAttribute skip_chroma_attrib[] = + {VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE}; const uint8_t skip_chroma_value = 1; const void * const skip_chroma_value_ptr[] = {&skip_chroma_value}; static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = { VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT, - VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE + VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE, }; const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = { - &vid_width, - &vid_height, - &vdp_chroma_type + &vc->vid_width, + &vc->vid_height, + &vdp_chroma_type, }; features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL; - if (deint == 4) - features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL; - if (pullup) + if (vc->deint == 4) + features[feature_count++] = + VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL; + if (vc->pullup) features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE; - if (denoise) + if (vc->denoise) features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION; - if (sharpen) + if (vc->sharpen) features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS; - vdp_st = vdp_video_mixer_create(vdp_device, feature_count, features, - VDP_NUM_MIXER_PARAMETER, - parameters, parameter_values, - &video_mixer); - CHECK_ST_ERROR("Error when calling vdp_video_mixer_create") + vdp_st = vdp->video_mixer_create(vc->vdp_device, feature_count, features, + VDP_NUM_MIXER_PARAMETER, + parameters, parameter_values, + &vc->video_mixer); + CHECK_ST_ERROR("Error when calling vdp_video_mixer_create"); - for (i = 0; i < feature_count; i++) feature_enables[i] = VDP_TRUE; - if (deint < 3) + for (i = 0; i < feature_count; i++) + feature_enables[i] = VDP_TRUE; + if (vc->deint < 3) feature_enables[0] = VDP_FALSE; if (feature_count) - vdp_video_mixer_set_feature_enables(video_mixer, feature_count, features, feature_enables); - if (denoise) - vdp_video_mixer_set_attribute_values(video_mixer, 1, denoise_attrib, denoise_value); - if (sharpen) - vdp_video_mixer_set_attribute_values(video_mixer, 1, sharpen_attrib, sharpen_value); - if (!chroma_deint) - vdp_video_mixer_set_attribute_values(video_mixer, 1, skip_chroma_attrib, skip_chroma_value_ptr); + vdp->video_mixer_set_feature_enables(vc->video_mixer, feature_count, + features, feature_enables); + if (vc->denoise) + vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, + denoise_attrib, denoise_value); + if (vc->sharpen) + vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, + sharpen_attrib, sharpen_value); + if (!vc->chroma_deint) + vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, + skip_chroma_attrib, + skip_chroma_value_ptr); return 0; } // Free everything specific to a certain video file -static void free_video_specific(void) { +static void free_video_specific(struct vo *vo) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; int i; VdpStatus vdp_st; - if (decoder != VDP_INVALID_HANDLE) - vdp_decoder_destroy(decoder); - decoder = VDP_INVALID_HANDLE; - decoder_max_refs = -1; - - for (i = 0; i < 3; i++) - deint_surfaces[i] = VDP_INVALID_HANDLE; + if (vc->decoder != VDP_INVALID_HANDLE) + vdp->decoder_destroy(vc->decoder); + vc->decoder = VDP_INVALID_HANDLE; + vc->decoder_max_refs = -1; for (i = 0; i < 2; i++) - if (deint_mpi[i]) { - deint_mpi[i]->usage_count--; - deint_mpi[i] = NULL; + if (vc->deint_mpi[i]) { + vc->deint_mpi[i]->usage_count--; + vc->deint_mpi[i] = NULL; } for (i = 0; i < MAX_VIDEO_SURFACES; i++) { - if (surface_render[i].surface != VDP_INVALID_HANDLE) { - vdp_st = vdp_video_surface_destroy(surface_render[i].surface); - CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy") + if (vc->surface_render[i].surface != VDP_INVALID_HANDLE) { + vdp_st = vdp->video_surface_destroy(vc->surface_render[i].surface); + CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy"); } - surface_render[i].surface = VDP_INVALID_HANDLE; + vc->surface_render[i].surface = VDP_INVALID_HANDLE; } - if (video_mixer != VDP_INVALID_HANDLE) { - vdp_st = vdp_video_mixer_destroy(video_mixer); - CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy") + if (vc->video_mixer != VDP_INVALID_HANDLE) { + vdp_st = vdp->video_mixer_destroy(vc->video_mixer); + CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy"); } - video_mixer = VDP_INVALID_HANDLE; + vc->video_mixer = VDP_INVALID_HANDLE; } -static int create_vdp_decoder(int max_refs) +static int create_vdp_decoder(struct vo *vo, int max_refs) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; VdpDecoderProfile vdp_decoder_profile; - if (decoder != VDP_INVALID_HANDLE) - vdp_decoder_destroy(decoder); - switch (image_format) { + if (vc->decoder != VDP_INVALID_HANDLE) + vdp->decoder_destroy(vc->decoder); + switch (vc->image_format) { case IMGFMT_VDPAU_MPEG1: vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1; break; @@ -505,7 +536,8 @@ static int create_vdp_decoder(int max_refs) break; case IMGFMT_VDPAU_H264: vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH; - mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder for %d reference frames.\n", max_refs); + mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder " + "for %d reference frames.\n", max_refs); break; case IMGFMT_VDPAU_WMV3: vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN; @@ -514,15 +546,111 @@ static int create_vdp_decoder(int max_refs) vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED; break; } - vdp_st = vdp_decoder_create(vdp_device, vdp_decoder_profile, - vid_width, vid_height, max_refs, &decoder); + vdp_st = vdp->decoder_create(vc->vdp_device, vdp_decoder_profile, + vc->vid_width, vc->vid_height, max_refs, + &vc->decoder); CHECK_ST_WARNING("Failed creating VDPAU decoder"); if (vdp_st != VDP_STATUS_OK) { - decoder = VDP_INVALID_HANDLE; - decoder_max_refs = 0; + vc->decoder = VDP_INVALID_HANDLE; + vc->decoder_max_refs = 0; return 0; } - decoder_max_refs = max_refs; + vc->decoder_max_refs = max_refs; + return 1; +} + +int initialize_vdpau_objects(struct vo *vo) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + VdpStatus vdp_st; + + vc->vdp_chroma_type = VDP_CHROMA_TYPE_420; + switch (vc->image_format) { + case IMGFMT_YV12: + case IMGFMT_I420: + case IMGFMT_IYUV: + vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YV12; + break; + case IMGFMT_NV12: + vc->vdp_pixel_format = VDP_YCBCR_FORMAT_NV12; + break; + case IMGFMT_YUY2: + vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV; + vc->vdp_chroma_type = VDP_CHROMA_TYPE_422; + break; + case IMGFMT_UYVY: + vc->vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY; + vc->vdp_chroma_type = VDP_CHROMA_TYPE_422; + } + if (win_x11_init_vdpau_flip_queue(vo) < 0) + return -1; + + if (create_vdp_mixer(vo, vc->vdp_chroma_type) < 0) + return -1; + + vdp_st = vdp-> + bitmap_surface_query_capabilities(vc->vdp_device, + VDP_RGBA_FORMAT_A8, + &(VdpBool){0}, + &vc->eosd_surface.max_width, + &vc->eosd_surface.max_height); + CHECK_ST_WARNING("Query to get max EOSD surface size failed"); + vc->surface_num = 0; + forget_frames(vo); + resize(vo); + return 0; +} + +static void mark_vdpau_objects_uninitialized(struct vo *vo) +{ + struct vdpctx *vc = vo->priv; + + vc->decoder = VDP_INVALID_HANDLE; + for (int i = 0; i < MAX_VIDEO_SURFACES; i++) + vc->surface_render[i].surface = VDP_INVALID_HANDLE; + forget_frames(vo); + vc->video_mixer = VDP_INVALID_HANDLE; + vc->flip_queue = VDP_INVALID_HANDLE; + vc->flip_target = VDP_INVALID_HANDLE; + for (int i = 0; i <= NUM_OUTPUT_SURFACES; i++) + vc->output_surfaces[i] = VDP_INVALID_HANDLE; + vc->vdp_device = VDP_INVALID_HANDLE; + vc->eosd_surface = (struct eosd_bitmap_surface){ + .surface = VDP_INVALID_HANDLE, + }; + vc->output_surface_width = vc->output_surface_height = -1; + vc->eosd_render_count = 0; + vc->visible_buf = false; +} + +static int handle_preemption(struct vo *vo) +{ + struct vdpctx *vc = vo->priv; + + if (!vc->is_preempted) + return 0; + if (!vc->preemption_acked) + mark_vdpau_objects_uninitialized(vo); + vc->preemption_acked = true; + if (!vc->preemption_user_notified) { + mp_tmsg(MSGT_VO, MSGL_ERR, "[vdpau] Got display preemption notice! " + "Will attempt to recover.\n"); + vc->preemption_user_notified = true; + } + /* Trying to initialize seems to be quite slow, so only try once a + * second to avoid using 100% CPU. */ + if (vc->last_preemption_retry_fail + && GetTimerMS() - vc->last_preemption_retry_fail < 1000) + return -1; + if (win_x11_init_vdpau_procs(vo) < 0 || initialize_vdpau_objects(vo) < 0) { + vc->last_preemption_retry_fail = GetTimerMS() | 1; + return -1; + } + vc->last_preemption_retry_fail = 0; + vc->is_preempted = false; + vc->preemption_user_notified = false; + mp_tmsg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n"); return 1; } @@ -530,10 +658,12 @@ static int create_vdp_decoder(int max_refs) * connect to X server, create and map window, initialize all * VDPAU objects, create different surfaces etc. */ -static int config(uint32_t width, uint32_t height, uint32_t d_width, - uint32_t d_height, uint32_t flags, char *title, - uint32_t format) +static int config(struct vo *vo, uint32_t width, uint32_t height, + uint32_t d_width, uint32_t d_height, uint32_t flags, + char *title, uint32_t format) { + struct vdpctx *vc = vo->priv; + struct vo_x11_state *x11 = vo->x11; XVisualInfo vinfo; XSetWindowAttributes xswa; XWindowAttributes attribs; @@ -544,130 +674,114 @@ static int config(uint32_t width, uint32_t height, uint32_t d_width, int vm = flags & VOFLAG_MODESWITCHING; #endif - image_format = format; - vid_width = width; - vid_height = height; - free_video_specific(); - if (IMGFMT_IS_VDPAU(image_format) && !create_vdp_decoder(2)) + if (handle_preemption(vo) < 0) + return -1; + vc->image_format = format; + vc->vid_width = width; + vc->vid_height = height; + free_video_specific(vo); + if (IMGFMT_IS_VDPAU(vc->image_format) && !create_vdp_decoder(vo, 2)) return -1; - visible_buf = 0; - - { #ifdef CONFIG_XF86VM - if (vm) - vo_vm_switch(); - else + if (vm) { + vo_vm_switch(vo); + vc->mode_switched = true; + } #endif - XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs); - depth = attribs.depth; - if (depth != 15 && depth != 16 && depth != 24 && depth != 32) - depth = 24; - XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo); - - xswa.background_pixel = 0; - xswa.border_pixel = 0; - /* Do not use CWBackPixel: It leads to VDPAU errors after - aspect ratio changes. */ - xswamask = CWBorderPixel; - - vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height, - flags, CopyFromParent, "vdpau", title); - XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa); + XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display), + &attribs); + depth = attribs.depth; + if (depth != 15 && depth != 16 && depth != 24 && depth != 32) + depth = 24; + XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo); + + xswa.background_pixel = 0; + xswa.border_pixel = 0; + /* Do not use CWBackPixel: It leads to VDPAU errors after + * aspect ratio changes. */ + xswamask = CWBorderPixel; + + vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, d_width, d_height, + flags, CopyFromParent, "vdpau", title); + XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa); #ifdef CONFIG_XF86VM - if (vm) { - /* Grab the mouse pointer in our window */ - if (vo_grabpointer) - XGrabPointer(mDisplay, vo_window, True, 0, - GrabModeAsync, GrabModeAsync, - vo_window, None, CurrentTime); - XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime); - } -#endif + if (vm) { + /* Grab the mouse pointer in our window */ + if (vo_grabpointer) + XGrabPointer(x11->display, x11->window, True, 0, + GrabModeAsync, GrabModeAsync, + x11->window, None, CurrentTime); + XSetInputFocus(x11->display, x11->window, RevertToNone, CurrentTime); } +#endif if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0) vo_fs = 1; - /* -----VDPAU related code here -------- */ - if (vdp_flip_queue == VDP_INVALID_HANDLE && win_x11_init_vdpau_flip_queue()) - return -1; - - vdp_chroma_type = VDP_CHROMA_TYPE_420; - switch (image_format) { - case IMGFMT_YV12: - case IMGFMT_I420: - case IMGFMT_IYUV: - vdp_pixel_format = VDP_YCBCR_FORMAT_YV12; - break; - case IMGFMT_NV12: - vdp_pixel_format = VDP_YCBCR_FORMAT_NV12; - break; - case IMGFMT_YUY2: - vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV; - vdp_chroma_type = VDP_CHROMA_TYPE_422; - break; - case IMGFMT_UYVY: - vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY; - vdp_chroma_type = VDP_CHROMA_TYPE_422; - } - if (create_vdp_mixer(vdp_chroma_type)) + if (initialize_vdpau_objects(vo) < 0) return -1; - surface_num = 0; - vid_surface_num = -1; - resize(); - return 0; } -static void check_events(void) +static void check_events(struct vo *vo) { - int e = vo_x11_check_events(mDisplay); + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; - if (e & VO_EVENT_RESIZE) - resize(); + if (handle_preemption(vo) < 0) + return; + + int e = vo_x11_check_events(vo); - if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && int_pause) { + if (e & VO_EVENT_RESIZE) + resize(vo); + else if (e & VO_EVENT_EXPOSE && vc->paused) { /* did we already draw a buffer */ - if (visible_buf) { + if (vc->visible_buf) { /* redraw the last visible buffer */ VdpStatus vdp_st; - vdp_st = vdp_presentation_queue_display(vdp_flip_queue, - output_surfaces[surface_num], - vo_dwidth, vo_dheight, - 0); - CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display") + int last_surface = (vc->surface_num + NUM_OUTPUT_SURFACES - 1) + % NUM_OUTPUT_SURFACES; + vdp_st = vdp->presentation_queue_display(vc->flip_queue, + vc->output_surfaces[last_surface], + vo->dwidth, vo->dheight, 0); + CHECK_ST_WARNING("Error when calling " + "vdp_presentation_queue_display"); } } } -static void draw_osd_I8A8(int x0,int y0, int w,int h, unsigned char *src, - unsigned char *srca, int stride) +static void draw_osd_I8A8(void *ctx, int x0, int y0, int w, int h, + unsigned char *src, unsigned char *srca, int stride) { - VdpOutputSurface output_surface = output_surfaces[surface_num]; + struct vo *vo = ctx; + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num]; VdpStatus vdp_st; - int i, j; + int i; int pitch; int index_data_size_required; VdpRect output_indexed_rect_vid; - VdpOutputSurfaceRenderBlendState blend_state; if (!w || !h) return; index_data_size_required = 2*w*h; - if (index_data_size < index_data_size_required) { - index_data = realloc(index_data, index_data_size_required); - index_data_size = index_data_size_required; + if (vc->index_data_size < index_data_size_required) { + vc->index_data = talloc_realloc_size(vc, vc->index_data, + index_data_size_required); + vc->index_data_size = index_data_size_required; } // index_data creation, component order - I, A, I, A, ..... for (i = 0; i < h; i++) - for (j = 0; j < w; j++) { - index_data[i*2*w + j*2] = src [i*stride+j]; - index_data[i*2*w + j*2 + 1] = -srca[i*stride+j]; + for (int j = 0; j < w; j++) { + vc->index_data[i*2*w + j*2] = src [i*stride+j]; + vc->index_data[i*2*w + j*2 + 1] = -srca[i*stride+j]; } output_indexed_rect_vid.x0 = x0; @@ -678,256 +792,410 @@ static void draw_osd_I8A8(int x0,int y0, int w,int h, unsigned char *src, pitch = w*2; // write source_data to osd_surface. - vdp_st = vdp_output_surface_put_bits_indexed(osd_surface, - VDP_INDEXED_FORMAT_I8A8, - (const void *const*)&index_data, - &pitch, - &output_indexed_rect_vid, - VDP_COLOR_TABLE_FORMAT_B8G8R8X8, - (void *)palette); - CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed") - - blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION; - blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE; - blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE; - blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; - blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; - blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD; - blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD; - - vdp_st = vdp_output_surface_render_output_surface(output_surface, - &output_indexed_rect_vid, - osd_surface, - &output_indexed_rect_vid, - NULL, - &blend_state, - VDP_OUTPUT_SURFACE_RENDER_ROTATE_0); - CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface") + vdp_st = vdp-> + output_surface_put_bits_indexed(osd_surface, VDP_INDEXED_FORMAT_I8A8, + (const void *const*)&vc->index_data, + &pitch, &output_indexed_rect_vid, + VDP_COLOR_TABLE_FORMAT_B8G8R8X8, + (void *)vc->palette); + CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed"); + + VdpOutputSurfaceRenderBlendState blend_state = { + .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION, + .blend_factor_source_color = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE, + .blend_factor_source_alpha = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE, + .blend_factor_destination_color = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .blend_factor_destination_alpha = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD, + .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD, + }; + + vdp_st = vdp-> + output_surface_render_output_surface(output_surface, + &output_indexed_rect_vid, + osd_surface, + &output_indexed_rect_vid, + NULL, &blend_state, + VDP_OUTPUT_SURFACE_RENDER_ROTATE_0); + CHECK_ST_WARNING("Error when calling " + "vdp_output_surface_render_output_surface"); } -static void draw_eosd(void) { +static void draw_eosd(struct vo *vo) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; - VdpOutputSurface output_surface = output_surfaces[surface_num]; - VdpOutputSurfaceRenderBlendState blend_state; + VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num]; int i; - blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION; - blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA; - blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE; - blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; - blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA; - blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD; - blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD; - - for (i=0; i<eosd_render_count; i++) { - vdp_st = vdp_output_surface_render_bitmap_surface( - output_surface, &eosd_targets[i].dest, - eosd_targets[i].surface, &eosd_targets[i].source, - &eosd_targets[i].color, &blend_state, - VDP_OUTPUT_SURFACE_RENDER_ROTATE_0); - CHECK_ST_WARNING("EOSD: Error when rendering") + if (handle_preemption(vo) < 0) + return; + + VdpOutputSurfaceRenderBlendState blend_state = { + .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION, + .blend_factor_source_color = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA, + .blend_factor_source_alpha = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE, + .blend_factor_destination_color = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .blend_factor_destination_alpha = + VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA, + .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD, + .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD, + }; + + for (i = 0; i < vc->eosd_render_count; i++) { + vdp_st = vdp-> + output_surface_render_bitmap_surface(output_surface, + &vc->eosd_targets[i].dest, + vc->eosd_surface.surface, + &vc->eosd_targets[i].source, + &vc->eosd_targets[i].color, + &blend_state, + VDP_OUTPUT_SURFACE_RENDER_ROTATE_0); + CHECK_ST_WARNING("EOSD: Error when rendering"); } } -static void generate_eosd(mp_eosd_images_t *imgs) { +#define HEIGHT_SORT_BITS 4 +static int size_index(struct eosd_target *r) +{ + unsigned int h = r->source.y1; + int n = av_log2_16bit(h); + return (n << HEIGHT_SORT_BITS) + + (- 1 - (h << HEIGHT_SORT_BITS >> n) & (1 << HEIGHT_SORT_BITS) - 1); +} + +/* Pack the given rectangles into an area of size w * h. + * The size of each rectangle is read from .source.x1/.source.y1. + * The height of each rectangle must be at least 1 and less than 65536. + * The .source rectangle is then set corresponding to the packed position. + * 'scratch' must point to work memory for num_rects+16 ints. + * Return 0 on success, -1 if the rectangles did not fit in w*h. + * + * The rectangles are placed in rows in order approximately sorted by + * height (the approximate sorting is simpler than a full one would be, + * and allows the algorithm to work in linear time). Additionally, to + * reduce wasted space when there are a few tall rectangles, empty + * lower-right parts of rows are filled recursively when the size of + * rectangles in the row drops past a power-of-two threshold. So if a + * row starts with rectangles of size 3x50, 10x40 and 5x20 then the + * free rectangle with corners (13, 20)-(w, 50) is filled recursively. + */ +static int pack_rectangles(struct eosd_target *rects, int num_rects, + int w, int h, int *scratch) +{ + int bins[16 << HEIGHT_SORT_BITS]; + int sizes[16 << HEIGHT_SORT_BITS] = {}; + for (int i = 0; i < num_rects; i++) + sizes[size_index(rects + i)]++; + int idx = 0; + for (int i = 0; i < 16 << HEIGHT_SORT_BITS; i += 1 << HEIGHT_SORT_BITS) { + for (int j = 0; j < 1 << HEIGHT_SORT_BITS; j++) { + bins[i + j] = idx; + idx += sizes[i + j]; + } + scratch[idx++] = -1; + } + for (int i = 0; i < num_rects; i++) + scratch[bins[size_index(rects + i)]++] = i; + for (int i = 0; i < 16; i++) + bins[i] = bins[i << HEIGHT_SORT_BITS] - sizes[i << HEIGHT_SORT_BITS]; + struct { + int size, x, bottom; + } stack[16] = {{15, 0, h}}, s = {}; + int stackpos = 1; + int y; + while (stackpos) { + y = s.bottom; + s = stack[--stackpos]; + s.size++; + while (s.size--) { + int maxy = -1; + int obj; + while ((obj = scratch[bins[s.size]]) >= 0) { + int bottom = y + rects[obj].source.y1; + if (bottom > s.bottom) + break; + int right = s.x + rects[obj].source.x1; + if (right > w) + break; + bins[s.size]++; + rects[obj].source.x0 = s.x; + rects[obj].source.x1 += s.x; + rects[obj].source.y0 = y; + rects[obj].source.y1 += y; + num_rects--; + if (maxy <= 0) + stack[stackpos++] = s; + s.x = right; + maxy = FFMAX(maxy, bottom); + } + if (maxy > 0) + s.bottom = maxy; + } + } + return num_rects ? -1 : 0; +} + +static void generate_eosd(struct vo *vo, mp_eosd_images_t *imgs) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; - VdpRect destRect; - int j, found; + int i; ASS_Image *img = imgs->imgs; - ASS_Image *i; + ASS_Image *p; + struct eosd_bitmap_surface *sfc = &vc->eosd_surface; + bool need_upload = false; - // Nothing changed, no need to redraw if (imgs->changed == 0) - return; - eosd_render_count = 0; - // There's nothing to render! + return; // Nothing changed, no need to redraw + + vc->eosd_render_count = 0; + if (!img) - return; + return; // There's nothing to render! if (imgs->changed == 1) goto eosd_skip_upload; - for (j=0; j<eosd_surface_count; j++) - eosd_surfaces[j].in_use = 0; - - for (i = img; i; i = i->next) { - // Try to reuse a suitable surface - found = -1; - for (j=0; j<eosd_surface_count; j++) { - if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE && !eosd_surfaces[j].in_use && - eosd_surfaces[j].w >= i->w && eosd_surfaces[j].h >= i->h) { - found = j; - break; - } - } - // None found, allocate a new surface - if (found < 0) { - for (j=0; j<eosd_surface_count; j++) { - if (!eosd_surfaces[j].in_use) { - if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE) - vdp_bitmap_surface_destroy(eosd_surfaces[j].surface); - found = j; - break; - } - } + need_upload = true; + bool reallocate = false; + while (1) { + for (p = img, i = 0; p; p = p->next) { + if (p->w <= 0 || p->h <= 0) + continue; // Allocate new space for surface/target arrays - if (found < 0) { - j = found = eosd_surface_count; - eosd_surface_count = eosd_surface_count ? eosd_surface_count*2 : EOSD_SURFACES_INITIAL; - eosd_surfaces = realloc(eosd_surfaces, eosd_surface_count * sizeof(*eosd_surfaces)); - eosd_targets = realloc(eosd_targets, eosd_surface_count * sizeof(*eosd_targets)); - for(j=found; j<eosd_surface_count; j++) { - eosd_surfaces[j].surface = VDP_INVALID_HANDLE; - eosd_surfaces[j].in_use = 0; - } + if (i >= vc->eosd_targets_size) { + vc->eosd_targets_size = FFMAX(vc->eosd_targets_size * 2, 512); + vc->eosd_targets = + talloc_realloc_size(vc, vc->eosd_targets, + vc->eosd_targets_size + * sizeof(*vc->eosd_targets)); + vc->eosd_scratch = + talloc_realloc_size(vc, vc->eosd_scratch, + (vc->eosd_targets_size + 16) + * sizeof(*vc->eosd_scratch)); } - vdp_st = vdp_bitmap_surface_create(vdp_device, VDP_RGBA_FORMAT_A8, - i->w, i->h, VDP_TRUE, &eosd_surfaces[found].surface); - CHECK_ST_WARNING("EOSD: error when creating surface") - eosd_surfaces[found].w = i->w; - eosd_surfaces[found].h = i->h; + vc->eosd_targets[i].source.x1 = p->w; + vc->eosd_targets[i].source.y1 = p->h; + i++; + } + if (pack_rectangles(vc->eosd_targets, i, sfc->w, sfc->h, + vc->eosd_scratch) >= 0) + break; + int w = FFMIN(FFMAX(sfc->w * 2, EOSD_SURFACE_INITIAL_SIZE), + sfc->max_width); + int h = FFMIN(FFMAX(sfc->h * 2, EOSD_SURFACE_INITIAL_SIZE), + sfc->max_height); + if (w == sfc->w && h == sfc->h) { + mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] EOSD bitmaps do not fit on " + "a surface with the maximum supported size\n"); + return; + } else { + sfc->w = w; + sfc->h = h; } - eosd_surfaces[found].in_use = 1; - eosd_targets[eosd_render_count].surface = eosd_surfaces[found].surface; - destRect.x0 = 0; - destRect.y0 = 0; - destRect.x1 = i->w; - destRect.y1 = i->h; - vdp_st = vdp_bitmap_surface_putbits_native(eosd_targets[eosd_render_count].surface, - (const void *) &i->bitmap, &i->stride, &destRect); - CHECK_ST_WARNING("EOSD: putbits failed") - eosd_render_count++; + reallocate = true; + } + if (reallocate) { + if (sfc->surface != VDP_INVALID_HANDLE) + vdp->bitmap_surface_destroy(sfc->surface); + mp_msg(MSGT_VO, MSGL_V, "[vdpau] Allocating a %dx%d surface for " + "EOSD bitmaps.\n", sfc->w, sfc->h); + vdp_st = vdp->bitmap_surface_create(vc->vdp_device, VDP_RGBA_FORMAT_A8, + sfc->w, sfc->h, true, + &sfc->surface); + if (vdp_st != VDP_STATUS_OK) + sfc->surface = VDP_INVALID_HANDLE; + CHECK_ST_WARNING("EOSD: error when creating surface"); } eosd_skip_upload: - eosd_render_count = 0; - for (i = img; i; i = i->next) { + if (sfc->surface == VDP_INVALID_HANDLE) + return; + for (p = img; p; p = p->next) { + if (p->w <= 0 || p->h <= 0) + continue; + struct eosd_target *target = &vc->eosd_targets[vc->eosd_render_count]; + if (need_upload) { + vdp_st = vdp-> + bitmap_surface_put_bits_native(sfc->surface, + (const void *) &p->bitmap, + &p->stride, &target->source); + CHECK_ST_WARNING("EOSD: putbits failed"); + } // Render dest, color, etc. - eosd_targets[eosd_render_count].color.alpha = 1.0 - ((i->color >> 0) & 0xff) / 255.0; - eosd_targets[eosd_render_count].color.blue = ((i->color >> 8) & 0xff) / 255.0; - eosd_targets[eosd_render_count].color.green = ((i->color >> 16) & 0xff) / 255.0; - eosd_targets[eosd_render_count].color.red = ((i->color >> 24) & 0xff) / 255.0; - eosd_targets[eosd_render_count].dest.x0 = i->dst_x; - eosd_targets[eosd_render_count].dest.y0 = i->dst_y; - eosd_targets[eosd_render_count].dest.x1 = i->w + i->dst_x; - eosd_targets[eosd_render_count].dest.y1 = i->h + i->dst_y; - eosd_targets[eosd_render_count].source.x0 = 0; - eosd_targets[eosd_render_count].source.y0 = 0; - eosd_targets[eosd_render_count].source.x1 = i->w; - eosd_targets[eosd_render_count].source.y1 = i->h; - eosd_render_count++; + target->color.alpha = 1.0 - ((p->color >> 0) & 0xff) / 255.0; + target->color.blue = ((p->color >> 8) & 0xff) / 255.0; + target->color.green = ((p->color >> 16) & 0xff) / 255.0; + target->color.red = ((p->color >> 24) & 0xff) / 255.0; + target->dest.x0 = p->dst_x; + target->dest.y0 = p->dst_y; + target->dest.x1 = p->w + p->dst_x; + target->dest.y1 = p->h + p->dst_y; + vc->eosd_render_count++; } } -static void draw_osd(void) +static void draw_osd(struct vo *vo, struct osd_state *osd) { + struct vdpctx *vc = vo->priv; mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n"); - vo_draw_text_ext(vo_dwidth, vo_dheight, border_x, border_y, border_x, border_y, - vid_width, vid_height, draw_osd_I8A8); + if (handle_preemption(vo) < 0) + return; + + osd_draw_text_ext(osd, vo->dwidth, vo->dheight, vc->border_x, vc->border_y, + vc->border_x, vc->border_y, vc->vid_width, + vc->vid_height, draw_osd_I8A8, vo); } -static void flip_page(void) +static void flip_page(struct vo *vo) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; - mp_msg(MSGT_VO, MSGL_DBG2, "\nFLIP_PAGE VID:%u -> OUT:%u\n", - surface_render[vid_surface_num].surface, output_surfaces[surface_num]); - vdp_st = vdp_presentation_queue_display(vdp_flip_queue, output_surfaces[surface_num], - vo_dwidth, vo_dheight, - 0); - CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display") + if (handle_preemption(vo) < 0) + return; + + vdp_st = + vdp->presentation_queue_display(vc->flip_queue, + vc->output_surfaces[vc->surface_num], + vo->dwidth, vo->dheight, 0); + CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display"); - surface_num = (surface_num + 1) % NUM_OUTPUT_SURFACES; - visible_buf = 1; + vc->surface_num = (vc->surface_num + 1) % NUM_OUTPUT_SURFACES; + vc->visible_buf = true; } -static int draw_slice(uint8_t *image[], int stride[], int w, int h, - int x, int y) +static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w, + int h, int x, int y) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; + + if (handle_preemption(vo) < 0) + return VO_TRUE; + struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0]; - int max_refs = image_format == IMGFMT_VDPAU_H264 ? rndr->info.h264.num_ref_frames : 2; - if (!IMGFMT_IS_VDPAU(image_format)) + int max_refs = vc->image_format == IMGFMT_VDPAU_H264 ? + rndr->info.h264.num_ref_frames : 2; + if (!IMGFMT_IS_VDPAU(vc->image_format)) return VO_FALSE; - if ((decoder == VDP_INVALID_HANDLE || decoder_max_refs < max_refs) - && !create_vdp_decoder(max_refs)) + if ((vc->decoder == VDP_INVALID_HANDLE || vc->decoder_max_refs < max_refs) + && !create_vdp_decoder(vo, max_refs)) return VO_FALSE; - vdp_st = vdp_decoder_render(decoder, rndr->surface, (void *)&rndr->info, rndr->bitstream_buffers_used, rndr->bitstream_buffers); + vdp_st = vdp->decoder_render(vc->decoder, rndr->surface, + (void *)&rndr->info, + rndr->bitstream_buffers_used, + rndr->bitstream_buffers); CHECK_ST_WARNING("Failed VDPAU decoder rendering"); return VO_TRUE; } -static int draw_frame(uint8_t *src[]) +static struct vdpau_render_state *get_surface(struct vo *vo, int number) { - return VO_ERROR; -} + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; -static struct vdpau_render_state *get_surface(int number) -{ if (number > MAX_VIDEO_SURFACES) return NULL; - if (surface_render[number].surface == VDP_INVALID_HANDLE) { + if (vc->surface_render[number].surface == VDP_INVALID_HANDLE + && !vc->is_preempted) { VdpStatus vdp_st; - vdp_st = vdp_video_surface_create(vdp_device, vdp_chroma_type, - vid_width, vid_height, - &surface_render[number].surface); - CHECK_ST_WARNING("Error when calling vdp_video_surface_create") - if (vdp_st != VDP_STATUS_OK) - return NULL; + vdp_st = vdp->video_surface_create(vc->vdp_device, vc->vdp_chroma_type, + vc->vid_width, vc->vid_height, + &vc->surface_render[number].surface); + CHECK_ST_WARNING("Error when calling vdp_video_surface_create"); } - mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n", surface_render[number].surface); - return &surface_render[number]; + mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n", + vc->surface_render[number].surface); + return &vc->surface_render[number]; } -static uint32_t draw_image(mp_image_t *mpi) +static void draw_image(struct vo *vo, mp_image_t *mpi, double pts) { - if (IMGFMT_IS_VDPAU(image_format)) { - struct vdpau_render_state *rndr = mpi->priv; - vid_surface_num = rndr - surface_render; - if (deint_buffer_past_frames) { - mpi->usage_count++; - if (deint_mpi[1]) - deint_mpi[1]->usage_count--; - deint_mpi[1] = deint_mpi[0]; - deint_mpi[0] = mpi; - } + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + struct mp_image *reserved_mpi = NULL; + struct vdpau_render_state *rndr; + + if (vc->is_preempted) { + vo->frame_loaded = true; + return; + } + + if (IMGFMT_IS_VDPAU(vc->image_format)) { + rndr = mpi->priv; + reserved_mpi = mpi; } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) { VdpStatus vdp_st; void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]}; - struct vdpau_render_state *rndr = get_surface(deint_counter); - deint_counter = (deint_counter + 1) % 3; - vid_surface_num = rndr - surface_render; - if (image_format == IMGFMT_NV12) + rndr = get_surface(vo, vc->deint_counter); + vc->deint_counter = (vc->deint_counter + 1) % 3; + if (vc->image_format == IMGFMT_NV12) destdata[1] = destdata[2]; - vdp_st = vdp_video_surface_put_bits_y_cb_cr(rndr->surface, - vdp_pixel_format, - (const void *const*)destdata, - mpi->stride); // pitch - CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr") - } + vdp_st = + vdp->video_surface_put_bits_y_cb_cr(rndr->surface, + vc->vdp_pixel_format, + (const void *const*)destdata, + mpi->stride); // pitch + CHECK_ST_WARNING("Error when calling " + "vdp_video_surface_put_bits_y_cb_cr"); + } else + // We don't support slice callbacks so this shouldn't occur - + // I think the flags test above in pointless, but I'm adding + // this instead of removing it just in case. + abort(); if (mpi->fields & MP_IMGFIELD_ORDERED) - top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST); + vc->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST); else - top_field_first = 1; + vc->top_field_first = 1; - video_to_output_surface(); - return VO_TRUE; + add_new_video_surface(vo, rndr->surface, mpi, pts); + + return; } -static uint32_t get_image(mp_image_t *mpi) +static void get_buffered_frame(struct vo *vo, bool eof) { + struct vdpctx *vc = vo->priv; + + if (vc->deint_queue_pos < 2) + return; + vc->deint_queue_pos = 1; + video_to_output_surface(vo); + vo->next_pts = vc->deint_pts[0]; + vo->frame_loaded = true; +} + +static uint32_t get_image(struct vo *vo, mp_image_t *mpi) +{ + struct vdpctx *vc = vo->priv; struct vdpau_render_state *rndr; // no dr for non-decoding for now - if (!IMGFMT_IS_VDPAU(image_format)) return VO_FALSE; - if (mpi->type != MP_IMGTYPE_NUMBERED) return VO_FALSE; + if (!IMGFMT_IS_VDPAU(vc->image_format)) + return VO_FALSE; + if (mpi->type != MP_IMGTYPE_NUMBERED) + return VO_FALSE; - rndr = get_surface(mpi->number); + rndr = get_surface(vo, mpi->number); if (!rndr) { - mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in get_image\n"); + mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in " + "get_image\n"); // TODO: this probably breaks things forever, provide a dummy buffer? return VO_FALSE; } @@ -943,7 +1211,9 @@ static uint32_t get_image(mp_image_t *mpi) static int query_format(uint32_t format) { - int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD | VFCAP_EOSD_UNSCALED; + int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW + | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD + | VFCAP_EOSD_UNSCALED; switch (format) { case IMGFMT_YV12: case IMGFMT_I420: @@ -962,71 +1232,59 @@ static int query_format(uint32_t format) return 0; } -static void DestroyVdpauObjects(void) +static void destroy_vdpau_objects(struct vo *vo) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + int i; VdpStatus vdp_st; - free_video_specific(); + free_video_specific(vo); - vdp_st = vdp_presentation_queue_destroy(vdp_flip_queue); - CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy") + if (vc->flip_queue != VDP_INVALID_HANDLE) { + vdp_st = vdp->presentation_queue_destroy(vc->flip_queue); + CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy"); + } - vdp_st = vdp_presentation_queue_target_destroy(vdp_flip_target); - CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy") + if (vc->flip_target != VDP_INVALID_HANDLE) { + vdp_st = vdp->presentation_queue_target_destroy(vc->flip_target); + CHECK_ST_WARNING("Error when calling " + "vdp_presentation_queue_target_destroy"); + } for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) { - vdp_st = vdp_output_surface_destroy(output_surfaces[i]); - output_surfaces[i] = VDP_INVALID_HANDLE; - CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy") + if (vc->output_surfaces[i] == VDP_INVALID_HANDLE) + continue; + vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]); + CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy"); } - for (i = 0; i<eosd_surface_count; i++) { - if (eosd_surfaces[i].surface != VDP_INVALID_HANDLE) { - vdp_st = vdp_bitmap_surface_destroy(eosd_surfaces[i].surface); - CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy") - } - eosd_surfaces[i].surface = VDP_INVALID_HANDLE; + if (vc->eosd_surface.surface != VDP_INVALID_HANDLE) { + vdp_st = vdp->bitmap_surface_destroy(vc->eosd_surface.surface); + CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy"); } - vdp_st = vdp_device_destroy(vdp_device); - CHECK_ST_WARNING("Error when calling vdp_device_destroy") + vdp_st = vdp->device_destroy(vc->vdp_device); + CHECK_ST_WARNING("Error when calling vdp_device_destroy"); } -static void uninit(void) +static void uninit(struct vo *vo) { - if (!vo_config_count) - return; - visible_buf = 0; + struct vdpctx *vc = vo->priv; /* Destroy all vdpau objects */ - DestroyVdpauObjects(); - - free(index_data); - index_data = NULL; - - free(eosd_surfaces); - eosd_surfaces = NULL; - free(eosd_targets); - eosd_targets = NULL; + destroy_vdpau_objects(vo); #ifdef CONFIG_XF86VM - vo_vm_close(); + if (vc->mode_switched) + vo_vm_close(vo); #endif - vo_x11_uninit(); + vo_x11_uninit(vo); - dlclose(vdpau_lib_handle); + dlclose(vc->vdpau_lib_handle); } -static const opt_t subopts[] = { - {"deint", OPT_ARG_INT, &deint, (opt_test_f)int_non_neg}, - {"chroma-deint", OPT_ARG_BOOL, &chroma_deint, NULL}, - {"pullup", OPT_ARG_BOOL, &pullup, NULL}, - {"denoise", OPT_ARG_FLOAT, &denoise, NULL}, - {"sharpen", OPT_ARG_FLOAT, &sharpen, NULL}, - {NULL} -}; - static const char help_msg[] = "\n-vo vdpau command line help:\n" "Example: mplayer -vo vdpau:deint=2\n" @@ -1048,197 +1306,241 @@ static const char help_msg[] = " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n" ; -static int preinit(const char *arg) +static int preinit(struct vo *vo, const char *arg) { int i; - static const char *vdpaulibrary = "libvdpau.so.1"; - static const char *vdpau_device_create = "vdp_device_create_x11"; - - deint = 0; - deint_type = 3; - deint_counter = 0; - deint_buffer_past_frames = 0; - deint_mpi[0] = deint_mpi[1] = NULL; - chroma_deint = 1; - pullup = 0; - denoise = 0; - sharpen = 0; + + struct vdpctx *vc = talloc_zero(vo, struct vdpctx); + vo->priv = vc; + + // Mark everything as invalid first so uninit() can tell what has been + // allocated + mark_vdpau_objects_uninitialized(vo); + + vc->deint_type = 3; + vc->chroma_deint = 1; + const opt_t subopts[] = { + {"deint", OPT_ARG_INT, &vc->deint, (opt_test_f)int_non_neg}, + {"chroma-deint", OPT_ARG_BOOL, &vc->chroma_deint, NULL}, + {"pullup", OPT_ARG_BOOL, &vc->pullup, NULL}, + {"denoise", OPT_ARG_FLOAT, &vc->denoise, NULL}, + {"sharpen", OPT_ARG_FLOAT, &vc->sharpen, NULL}, + {NULL} + }; if (subopt_parse(arg, subopts) != 0) { mp_msg(MSGT_VO, MSGL_FATAL, help_msg); return -1; } - if (deint) - deint_type = deint; - if (deint > 1) - deint_buffer_past_frames = 1; - - vdpau_lib_handle = dlopen(vdpaulibrary, RTLD_LAZY); - if (!vdpau_lib_handle) { - mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not open dynamic library %s\n", - vdpaulibrary); + if (vc->deint) + vc->deint_type = vc->deint; + + char *vdpaulibrary = "libvdpau.so.1"; + char *vdpau_device_create = "vdp_device_create_x11"; + vc->vdpau_lib_handle = dlopen(vdpaulibrary, RTLD_LAZY); + if (!vc->vdpau_lib_handle) { + mp_msg(MSGT_VO, MSGL_ERR, + "[vdpau] Could not open dynamic library %s\n", vdpaulibrary); return -1; } - vdp_device_create = dlsym(vdpau_lib_handle, vdpau_device_create); - if (!vdp_device_create) { + vc->vdp_device_create = dlsym(vc->vdpau_lib_handle, vdpau_device_create); + if (!vc->vdp_device_create) { mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not find function %s in %s\n", vdpau_device_create, vdpaulibrary); + dlclose(vc->vdpau_lib_handle); return -1; } - if (!vo_init() || win_x11_init_vdpau_procs()) + if (!vo_init(vo)) { + dlclose(vc->vdpau_lib_handle); return -1; + } + + // After this calling uninit() should work to free resources - decoder = VDP_INVALID_HANDLE; - for (i = 0; i < MAX_VIDEO_SURFACES; i++) - surface_render[i].surface = VDP_INVALID_HANDLE; - video_mixer = VDP_INVALID_HANDLE; - for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) - output_surfaces[i] = VDP_INVALID_HANDLE; - vdp_flip_queue = VDP_INVALID_HANDLE; - output_surface_width = output_surface_height = -1; + if (win_x11_init_vdpau_procs(vo) < 0) { + if (vc->vdp->device_destroy) + vc->vdp->device_destroy(vc->vdp_device); + vo_x11_uninit(vo); + dlclose(vc->vdpau_lib_handle); + return -1; + } // full grayscale palette. for (i = 0; i < PALETTE_SIZE; ++i) - palette[i] = (i << 16) | (i << 8) | i; - index_data = NULL; - index_data_size = 0; - - eosd_surface_count = eosd_render_count = 0; - eosd_surfaces = NULL; - eosd_targets = NULL; + vc->palette[i] = (i << 16) | (i << 8) | i; - procamp.struct_version = VDP_PROCAMP_VERSION; - procamp.brightness = 0.0; - procamp.contrast = 1.0; - procamp.saturation = 1.0; - procamp.hue = 0.0; + vc->procamp.struct_version = VDP_PROCAMP_VERSION; + vc->procamp.brightness = 0.0; + vc->procamp.contrast = 1.0; + vc->procamp.saturation = 1.0; + vc->procamp.hue = 0.0; return 0; } -static int get_equalizer(char *name, int *value) { +static int get_equalizer(struct vo *vo, const char *name, int *value) +{ + struct vdpctx *vc = vo->priv; + if (!strcasecmp(name, "brightness")) - *value = procamp.brightness * 100; + *value = vc->procamp.brightness * 100; else if (!strcasecmp(name, "contrast")) - *value = (procamp.contrast-1.0) * 100; + *value = (vc->procamp.contrast - 1.0) * 100; else if (!strcasecmp(name, "saturation")) - *value = (procamp.saturation-1.0) * 100; + *value = (vc->procamp.saturation - 1.0) * 100; else if (!strcasecmp(name, "hue")) - *value = procamp.hue * 100 / M_PI; + *value = vc->procamp.hue * 100 / M_PI; else return VO_NOTIMPL; return VO_TRUE; } -static int set_equalizer(char *name, int value) { +static int set_equalizer(struct vo *vo, const char *name, int value) +{ + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; VdpStatus vdp_st; VdpCSCMatrix matrix; - static const VdpVideoMixerAttribute attributes[] = {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX}; + static const VdpVideoMixerAttribute attributes[] = + {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX}; const void *attribute_values[] = {&matrix}; if (!strcasecmp(name, "brightness")) - procamp.brightness = value / 100.0; + vc->procamp.brightness = value / 100.0; else if (!strcasecmp(name, "contrast")) - procamp.contrast = value / 100.0 + 1.0; + vc->procamp.contrast = value / 100.0 + 1.0; else if (!strcasecmp(name, "saturation")) - procamp.saturation = value / 100.0 + 1.0; + vc->procamp.saturation = value / 100.0 + 1.0; else if (!strcasecmp(name, "hue")) - procamp.hue = value / 100.0 * M_PI; + vc->procamp.hue = value / 100.0 * M_PI; else return VO_NOTIMPL; - vdp_st = vdp_generate_csc_matrix(&procamp, VDP_COLOR_STANDARD_ITUR_BT_601, - &matrix); - CHECK_ST_WARNING("Error when generating CSC matrix") - vdp_st = vdp_video_mixer_set_attribute_values(video_mixer, 1, attributes, - attribute_values); - CHECK_ST_WARNING("Error when setting CSC matrix") + vdp_st = vdp->generate_csc_matrix(&vc->procamp, + VDP_COLOR_STANDARD_ITUR_BT_601, + &matrix); + CHECK_ST_WARNING("Error when generating CSC matrix"); + vdp_st = vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, + attributes, + attribute_values); + CHECK_ST_WARNING("Error when setting CSC matrix"); return VO_TRUE; } -static int control(uint32_t request, void *data) +static int control(struct vo *vo, uint32_t request, void *data) { + struct vdpctx *vc = vo->priv; + struct vdp_functions *vdp = vc->vdp; + + handle_preemption(vo); + switch (request) { - case VOCTRL_GET_DEINTERLACE: - *(int*)data = deint; - return VO_TRUE; - case VOCTRL_SET_DEINTERLACE: - deint = *(int*)data; - if (deint) - deint = deint_type; - if (deint_type > 2) { - VdpStatus vdp_st; - VdpVideoMixerFeature features[1] = - {deint_type == 3 ? - VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL : - VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}; - VdpBool feature_enables[1] = {deint ? VDP_TRUE : VDP_FALSE}; - vdp_st = vdp_video_mixer_set_feature_enables(video_mixer, 1, - features, - feature_enables); - CHECK_ST_WARNING("Error changing deinterlacing settings") - deint_buffer_past_frames = 1; - } - return VO_TRUE; - case VOCTRL_PAUSE: - return (int_pause = 1); - case VOCTRL_RESUME: - return (int_pause = 0); - case VOCTRL_QUERY_FORMAT: - return query_format(*(uint32_t *)data); - case VOCTRL_GET_IMAGE: - return get_image(data); - case VOCTRL_DRAW_IMAGE: - return draw_image(data); - case VOCTRL_BORDER: - vo_x11_border(); - resize(); - return VO_TRUE; - case VOCTRL_FULLSCREEN: - vo_x11_fullscreen(); - resize(); - return VO_TRUE; - case VOCTRL_GET_PANSCAN: - return VO_TRUE; - case VOCTRL_SET_PANSCAN: - resize(); - return VO_TRUE; - case VOCTRL_SET_EQUALIZER: { - struct voctrl_set_equalizer_args *args = data; - return set_equalizer(args->name, args->value); - } - case VOCTRL_GET_EQUALIZER: - { - struct voctrl_get_equalizer_args *args = data; - return get_equalizer(args->name, args->valueptr); + case VOCTRL_GET_DEINTERLACE: + *(int*)data = vc->deint; + return VO_TRUE; + case VOCTRL_SET_DEINTERLACE: + vc->deint = *(int*)data; + if (vc->deint) + vc->deint = vc->deint_type; + if (vc->deint_type > 2) { + VdpStatus vdp_st; + VdpVideoMixerFeature features[1] = + {vc->deint_type == 3 ? + VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL : + VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}; + VdpBool feature_enables[1] = {vc->deint ? VDP_TRUE : VDP_FALSE}; + vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer, + 1, features, + feature_enables); + CHECK_ST_WARNING("Error changing deinterlacing settings"); } - case VOCTRL_ONTOP: - vo_x11_ontop(); - return VO_TRUE; - case VOCTRL_UPDATE_SCREENINFO: - update_xinerama_info(); - return VO_TRUE; - case VOCTRL_DRAW_EOSD: - if (!data) - return VO_FALSE; - generate_eosd(data); - draw_eosd(); - return VO_TRUE; - case VOCTRL_GET_EOSD_RES: { - mp_eosd_res_t *r = data; - r->mt = r->mb = r->ml = r->mr = 0; - if (vo_fs) { - r->w = vo_screenwidth; - r->h = vo_screenheight; - r->ml = r->mr = border_x; - r->mt = r->mb = border_y; - } else { - r->w = vo_dwidth; - r->h = vo_dheight; - } - return VO_TRUE; + return VO_TRUE; + case VOCTRL_PAUSE: + return (vc->paused = true); + case VOCTRL_RESUME: + return (vc->paused = false); + case VOCTRL_QUERY_FORMAT: + return query_format(*(uint32_t *)data); + case VOCTRL_GET_IMAGE: + return get_image(vo, data); + case VOCTRL_DRAW_IMAGE: + abort(); // draw_image() should get called directly + case VOCTRL_BORDER: + vo_x11_border(vo); + resize(vo); + return VO_TRUE; + case VOCTRL_FULLSCREEN: + vo_x11_fullscreen(vo); + resize(vo); + return VO_TRUE; + case VOCTRL_GET_PANSCAN: + return VO_TRUE; + case VOCTRL_SET_PANSCAN: + resize(vo); + return VO_TRUE; + case VOCTRL_SET_EQUALIZER: { + struct voctrl_set_equalizer_args *args = data; + return set_equalizer(vo, args->name, args->value); + } + case VOCTRL_GET_EQUALIZER: { + struct voctrl_get_equalizer_args *args = data; + return get_equalizer(vo, args->name, args->valueptr); + } + case VOCTRL_ONTOP: + vo_x11_ontop(vo); + return VO_TRUE; + case VOCTRL_UPDATE_SCREENINFO: + update_xinerama_info(vo); + return VO_TRUE; + case VOCTRL_DRAW_EOSD: + if (!data) + return VO_FALSE; + generate_eosd(vo, data); + draw_eosd(vo); + return VO_TRUE; + case VOCTRL_GET_EOSD_RES: { + mp_eosd_res_t *r = data; + r->mt = r->mb = r->ml = r->mr = 0; + if (vo_fs) { + r->w = vo->opts->vo_screenwidth; + r->h = vo->opts->vo_screenheight; + r->ml = r->mr = vc->border_x; + r->mt = r->mb = vc->border_y; + } else { + r->w = vo->dwidth; + r->h = vo->dheight; } + return VO_TRUE; + } + case VOCTRL_REDRAW_OSD: + video_to_output_surface(vo); + draw_eosd(vo); + draw_osd(vo, data); + flip_page(vo); + return true; + case VOCTRL_RESET: + forget_frames(vo); + return true; } return VO_NOTIMPL; } -/* @} */ +const struct vo_driver video_out_vdpau = { + .is_new = true, + .buffer_frames = true, + .info = &(const struct vo_info_s){ + "VDPAU with X11", + "vdpau", + "Rajib Mahapatra <rmahapatra@nvidia.com> and others", + "" + }, + .preinit = preinit, + .config = config, + .control = control, + .draw_image = draw_image, + .get_buffered_frame = get_buffered_frame, + .draw_slice = draw_slice, + .draw_osd = draw_osd, + .flip_page = flip_page, + .check_events = check_events, + .uninit = uninit, +}; diff --git a/libvo/vo_xv.c b/libvo/vo_xv.c index 2fe1616eb3..f9579c1e19 100644 --- a/libvo/vo_xv.c +++ b/libvo/vo_xv.c @@ -518,11 +518,6 @@ static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w, return 0; } -static int draw_frame(struct vo *vo, uint8_t *src[]) -{ - return VO_ERROR; -} - static uint32_t draw_image(struct vo *vo, mp_image_t *mpi) { struct xvctx *ctx = vo->priv; @@ -840,7 +835,6 @@ const struct vo_driver video_out_xv = { .preinit = preinit, .config = config, .control = control, - .draw_frame = draw_frame, .draw_slice = draw_slice, .draw_osd = draw_osd, .flip_page = flip_page, diff --git a/mencoder.c b/mencoder.c index de862fac48..d1472a0568 100644 --- a/mencoder.c +++ b/mencoder.c @@ -204,6 +204,7 @@ int vo_config(struct vo *vo, uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format) { abort(); } int vo_control(struct vo *vo, uint32_t request, void *data) { abort(); } +int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts) { abort(); } int vo_draw_frame(struct vo *vo, uint8_t *src[]) { abort(); } int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) { abort(); } void vo_draw_osd(struct vo *vo, struct osd_state *osd) { abort(); } @@ -103,10 +103,6 @@ typedef struct MPContext { struct demux_stream *d_sub; mixer_t mixer; struct vo *video_out; - // Frames buffered in the vo ready to flip. Currently always 0 or 1. - // This is really a vo variable but currently there's no suitable vo - // struct. - int num_buffered_frames; // Show a video frame as quickly as possible without trying to adjust // for AV sync. Used when starting a file or after seeking. @@ -2266,6 +2266,7 @@ static double update_video_nocorrect_pts(struct MPContext *mpctx, static double update_video(struct MPContext *mpctx, int *blit_frame) { struct sh_video *sh_video = mpctx->sh_video; + struct vo *video_out = mpctx->video_out; *blit_frame = 0; sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, mpctx->osd); // hack for vf_expand @@ -2274,14 +2275,18 @@ static double update_video(struct MPContext *mpctx, int *blit_frame) double pts; - while (1) { + bool hit_eof = false; + while (!video_out->frame_loaded) { current_module = "filter_video"; + if (vo_get_buffered_frame(video_out, hit_eof) >= 0) + break; + if (hit_eof) + return -1; // XXX Time used in this call is not counted in any performance // timer now, OSD time is not updated correctly for filter-added frames if (vf_output_queued_frame(sh_video->vfilter)) break; unsigned char *packet = NULL; - bool hit_eof = false; int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts); if (pts != MP_NOPTS_VALUE) pts += mpctx->video_offset; @@ -2304,12 +2309,12 @@ static double update_video(struct MPContext *mpctx, int *blit_frame) update_osd_msg(mpctx); current_module = "filter video"; if (filter_video(sh_video, decoded_frame, sh_video->pts)) - break; - } else if (hit_eof) - return -1; + if (!video_out->config_ok) + break; // We'd likely hang in this loop otherwise + } } - sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts); + pts = video_out->next_pts; if (pts == MP_NOPTS_VALUE) { mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n"); // Try to use decoder pts from before filters @@ -2565,11 +2570,9 @@ static int seek(MPContext *mpctx, double amount, int style) if (mpctx->sh_video) { current_module = "seek_video_reset"; resync_video_stream(mpctx->sh_video); - if (mpctx->video_out->config_ok) - vo_control(mpctx->video_out, VOCTRL_RESET, NULL); + vo_seek_reset(mpctx->video_out); mpctx->sh_video->num_buffered_pts = 0; mpctx->sh_video->last_pts = MP_NOPTS_VALUE; - mpctx->num_buffered_frames = 0; mpctx->delay = 0; mpctx->time_frame = 0; mpctx->update_video_immediately = true; @@ -3799,7 +3802,6 @@ if(verbose) term_osd = 0; int frame_time_remaining=0; // flag int blit_frame=0; -mpctx->num_buffered_frames=0; // Make sure old OSD does not stay around, // e.g. with -fixed-vo and same-resolution files @@ -3948,7 +3950,7 @@ if(!mpctx->sh_video) { vo_pts=mpctx->sh_video->timer*90000.0; vo_fps=mpctx->sh_video->fps; - if (!mpctx->num_buffered_frames) { + if (!mpctx->video_out->frame_loaded) { double frame_time = update_video(mpctx, &blit_frame); mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time); if (mpctx->sh_video->vf_initialized < 0) { @@ -3965,8 +3967,6 @@ if(!mpctx->sh_video) { if (frame_time < 0) mpctx->stop_play = AT_END_OF_FILE; else { - // might return with !eof && !blit_frame if !correct_pts - mpctx->num_buffered_frames += blit_frame; if (mpctx->update_video_immediately) { // Show this frame immediately, rest normally mpctx->update_video_immediately = false; @@ -4018,7 +4018,6 @@ if(!mpctx->sh_video) { unsigned int t2=GetTimer(); vo_flip_page(mpctx->video_out); - mpctx->num_buffered_frames--; mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001; vout_time_usage += mpctx->last_vo_flip_duration; |