1 /*
2  * Copyright (C) 2000-2020 the xine project
3  *
4  * This file is part of xine, a unix video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  */
20 
21 #if defined(HAVE_CONFIG_H) && !defined(__XINE_LIB_CONFIG_H__)
22 #  error config.h not included
23 #endif
24 
25 #ifdef HAVE_X11
26 #  include <X11/Xlib.h>
27 #endif
28 
29 #include <xine/xine_internal.h>
30 #include <xine/vo_scale.h>
31 #include "dxr3_scr.h"
32 #include "dxr3.h"
33 
34 /* the number of supported encoders */
35 #define SUPPORTED_ENCODER_COUNT 3
36 
37 
38 /* plugin structures */
39 typedef struct encoder_data_s encoder_data_t;
40 typedef struct spu_encoder_s spu_encoder_t;
41 
42 typedef enum { ENC_FAME, ENC_RTE, ENC_LAVC } encoder_type;
43 
44 
45 struct coeff {
46   float            k,m;
47 };
48 
49 typedef struct dxr3_overlay_s {
50   xine_t          *xine;
51 
52   int              fd_control;
53 
54   int              xoffset;
55   int              yoffset;
56   int              xcorr;
57   int              jitter;
58   int              stability;
59   int              colorkey;
60   float            color_interval;
61   int              screen_xres;
62   int              screen_yres;
63   int              screen_depth;
64   int              shrink;
65 
66   struct coeff     colcal_upper[3];
67   struct coeff     colcal_lower[3];
68 } dxr3_overlay_t;
69 
70 typedef struct dxr3_driver_class_s {
71   video_driver_class_t  video_driver_class;
72   xine_t               *xine;
73 
74   int                   visual_type;
75   int                   instance;           /* we allow only one instance of this plugin */
76 
77   int                   devnum;
78 } dxr3_driver_class_t;
79 
80 typedef struct dxr3_driver_s {
81   vo_driver_t          vo_driver;
82   dxr3_driver_class_t *class;
83   dxr3_scr_t           *scr;                /* to provide dxr3 clocking */
84 
85   int                  fd_control;
86   pthread_mutex_t      video_device_lock;
87   int                  fd_video;
88   pthread_mutex_t      spu_device_lock;
89   int                  fd_spu;              /* to access the relevant dxr3 devices */
90   int                  clut_cluttered;      /* to tell spu decoder that it has to restore the palette */
91 
92   int                  enhanced_mode;
93   int                  swap_fields;         /* swap fields */
94   int                  add_bars;            /* add black bars to correct a.r. */
95 
96   int                  aspect;
97   int                  tv_mode;
98   int                  pan_scan;
99   int                  overlay_enabled;
100   int                  tv_switchable;       /* can switch from overlay<->tvout */
101   int                  widescreen_enabled;
102   em8300_bcs_t         bcs;
103 
104   encoder_data_t      *enc;                 /* mpeg encoder data */
105   spu_encoder_t       *spu_enc;             /* spu encoder */
106   int                  need_update;         /* the mpeg encoder needs to be updated */
107 
108   uint32_t             video_iheight;       /* input height (before adding black bars) */
109   uint32_t             video_oheight;       /* output height (after adding black bars) */
110   uint32_t             video_width;
111   double               video_ratio;
112   int                  video_aspect;
113   int                  top_bar;             /* the height of the upper black bar */
114 
115   vo_scale_t           scale;
116   alphablend_t         alphablend_extra_data;
117 
118   dxr3_overlay_t       overlay;
119 #ifdef HAVE_X11
120   Display             *display;
121   Drawable             win;
122   GC                   gc;
123   XColor               black;
124   XColor               key;
125 #endif
126 
127 } dxr3_driver_t;
128 
129 typedef struct dxr3_frame_s {
130   vo_frame_t       vo_frame;
131   uint32_t         oheight;
132   int              aspect, pan_scan;
133   void            *mem;           /* allocated for YV12 or YUY2 buffers */
134   uint8_t         *real_base[3];  /* yuv/yuy2 buffers in mem aligned on 16 */
135   int              swap_fields;   /* shifts Y buffer one line to exchange odd/even lines */
136 } dxr3_frame_t;
137 
138 struct encoder_data_s {
139   encoder_type     type;
140   int            (*on_update_format)(dxr3_driver_t *, dxr3_frame_t *);
141   int            (*on_frame_copy)(dxr3_driver_t *, dxr3_frame_t *, uint8_t **src);
142   int            (*on_display_frame)(dxr3_driver_t *, dxr3_frame_t *);
143   int            (*on_unneeded)(dxr3_driver_t *);
144   int            (*on_close)(dxr3_driver_t *);
145 
146   /* this is only used by the libavcodec encoder */
147   void            *handle;
148 };
149 
150 struct spu_encoder_s {
151   vo_overlay_t   *overlay;
152   int             need_reencode;
153   uint8_t        *target;
154   int             size;
155   int             malloc_size;
156   uint32_t        color[16];
157   uint8_t         trans[4];
158   int             map[OVL_PALETTE_SIZE];
159   uint32_t        hili_color[16];
160   uint8_t         hili_trans[4];
161   int             clip_map[OVL_PALETTE_SIZE];
162 };
163 
164 /* mpeg encoder plugins initialization functions */
165 #ifdef HAVE_LIBRTE
166 int dxr3_rte_init(dxr3_driver_t *);
167 #endif
168 #ifdef HAVE_LIBFAME
169 int dxr3_fame_init(dxr3_driver_t *);
170 #endif
171 int dxr3_lavc_init(dxr3_driver_t *, plugin_node_t *);
172 
173 /* spu encoder functions */
174 spu_encoder_t *dxr3_spu_encoder_init(void);
175 void           dxr3_spu_encode(spu_encoder_t *);
176 
177 #define dxr3_video_setpts(fd,arg)	dxr3_compat_ioctl((fd), EM8300_IOCTL_VIDEO_SETPTS, (arg))
178 #define dxr3_spu_setpts(fd,arg)		dxr3_compat_ioctl((fd), EM8300_IOCTL_SPU_SETPTS, (arg))
179 #define dxr3_spu_setpalette(fd,arg)	dxr3_compat_ioctl((fd), EM8300_IOCTL_SPU_SETPALETTE, (arg))
180 #define dxr3_spu_button(fd,arg)		dxr3_compat_ioctl((fd), EM8300_IOCTL_SPU_BUTTON, (arg))
181 int dxr3_compat_ioctl (int, int, void *);
182