1 /*****************************************************************************
2  * mmal.c: MMAL-based vout plugin for Raspberry Pi
3  *****************************************************************************
4  * Copyright © 2014 jusst technologies GmbH
5  * $Id: 76188a457cb57c2f1cadb9612fc25c993d2effaa $
6  *
7  * Authors: Dennis Hamester <dennis.hamester@gmail.com>
8  *          Julian Scheel <julian@jusst.de>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <math.h>
30 
31 #include <vlc_common.h>
32 #include <vlc_atomic.h>
33 #include <vlc_plugin.h>
34 #include <vlc_threads.h>
35 #include <vlc_vout_display.h>
36 
37 #include "mmal_picture.h"
38 
39 #include <bcm_host.h>
40 #include <interface/mmal/mmal.h>
41 #include <interface/mmal/util/mmal_util.h>
42 #include <interface/mmal/util/mmal_default_components.h>
43 #include <interface/vmcs_host/vc_tvservice.h>
44 #include <interface/vmcs_host/vc_dispmanx.h>
45 
46 #define MAX_BUFFERS_IN_TRANSIT 1
47 #define VC_TV_MAX_MODE_IDS 127
48 
49 #define MMAL_LAYER_NAME "mmal-layer"
50 #define MMAL_LAYER_TEXT N_("VideoCore layer where the video is displayed.")
51 #define MMAL_LAYER_LONGTEXT N_("VideoCore layer where the video is displayed. Subpictures are displayed directly above and a black background directly below.")
52 
53 #define MMAL_BLANK_BACKGROUND_NAME "mmal-blank-background"
54 #define MMAL_BLANK_BACKGROUND_TEXT N_("Blank screen below video.")
55 #define MMAL_BLANK_BACKGROUND_LONGTEXT N_("Render blank screen below video. " \
56         "Increases VideoCore load.")
57 
58 #define MMAL_ADJUST_REFRESHRATE_NAME "mmal-adjust-refreshrate"
59 #define MMAL_ADJUST_REFRESHRATE_TEXT N_("Adjust HDMI refresh rate to the video.")
60 #define MMAL_ADJUST_REFRESHRATE_LONGTEXT N_("Adjust HDMI refresh rate to the video.")
61 
62 #define MMAL_NATIVE_INTERLACED "mmal-native-interlaced"
63 #define MMAL_NATIVE_INTERLACE_TEXT N_("Force interlaced video mode.")
64 #define MMAL_NATIVE_INTERLACE_LONGTEXT N_("Force the HDMI output into an " \
65         "interlaced video mode for interlaced video content.")
66 
67 /* Ideal rendering phase target is at rough 25% of frame duration */
68 #define PHASE_OFFSET_TARGET ((double)0.25)
69 #define PHASE_CHECK_INTERVAL 100
70 
71 static int Open(vlc_object_t *);
72 static void Close(vlc_object_t *);
73 
74 vlc_module_begin()
75     set_shortname(N_("MMAL vout"))
76     set_description(N_("MMAL-based vout plugin for Raspberry Pi"))
77     set_capability("vout display", 90)
78     add_shortcut("mmal_vout")
79     add_integer(MMAL_LAYER_NAME, 1, MMAL_LAYER_TEXT, MMAL_LAYER_LONGTEXT, false)
80     add_bool(MMAL_BLANK_BACKGROUND_NAME, true, MMAL_BLANK_BACKGROUND_TEXT,
81                     MMAL_BLANK_BACKGROUND_LONGTEXT, true);
82     add_bool(MMAL_ADJUST_REFRESHRATE_NAME, false, MMAL_ADJUST_REFRESHRATE_TEXT,
83                     MMAL_ADJUST_REFRESHRATE_LONGTEXT, false)
84     add_bool(MMAL_NATIVE_INTERLACED, false, MMAL_NATIVE_INTERLACE_TEXT,
85                     MMAL_NATIVE_INTERLACE_LONGTEXT, false)
86     set_callbacks(Open, Close)
87 vlc_module_end()
88 
89 struct dmx_region_t {
90     struct dmx_region_t *next;
91     picture_t *picture;
92     VC_RECT_T bmp_rect;
93     VC_RECT_T src_rect;
94     VC_RECT_T dst_rect;
95     VC_DISPMANX_ALPHA_T alpha;
96     DISPMANX_ELEMENT_HANDLE_T element;
97     DISPMANX_RESOURCE_HANDLE_T resource;
98     int32_t pos_x;
99     int32_t pos_y;
100 };
101 
102 struct vout_display_sys_t {
103     vlc_cond_t buffer_cond;
104     vlc_mutex_t buffer_mutex;
105     vlc_mutex_t manage_mutex;
106 
107     plane_t planes[3]; /* Depending on video format up to 3 planes are used */
108     picture_t **pictures; /* Actual list of alloced pictures passed into picture_pool */
109     picture_pool_t *picture_pool;
110 
111     MMAL_COMPONENT_T *component;
112     MMAL_PORT_T *input;
113     MMAL_POOL_T *pool; /* mmal buffer headers, used for pushing pictures to component*/
114     struct dmx_region_t *dmx_region;
115     int i_planes; /* Number of actually used planes, 1 for opaque, 3 for i420 */
116 
117     uint32_t buffer_size; /* size of actual mmal buffers */
118     int buffers_in_transit; /* number of buffers currently pushed to mmal component */
119     unsigned num_buffers; /* number of buffers allocated at mmal port */
120 
121     DISPMANX_DISPLAY_HANDLE_T dmx_handle;
122     DISPMANX_ELEMENT_HANDLE_T bkg_element;
123     DISPMANX_RESOURCE_HANDLE_T bkg_resource;
124     unsigned display_width;
125     unsigned display_height;
126 
127     int i_frame_rate_base; /* cached framerate to detect changes for rate adjustment */
128     int i_frame_rate;
129 
130     int next_phase_check; /* lowpass for phase check frequency */
131     int phase_offset; /* currently applied offset to presentation time in ns */
132     int layer; /* the dispman layer (z-index) used for video rendering */
133 
134     bool need_configure_display; /* indicates a required display reconfigure to main thread */
135     bool adjust_refresh_rate;
136     bool native_interlaced;
137     bool b_top_field_first; /* cached interlaced settings to detect changes for native mode */
138     bool b_progressive;
139     bool opaque; /* indicated use of opaque picture format (zerocopy) */
140 };
141 
142 static const vlc_fourcc_t subpicture_chromas[] = {
143     VLC_CODEC_RGBA,
144     0
145 };
146 
147 /* Utility functions */
148 static inline uint32_t align(uint32_t x, uint32_t y);
149 static int configure_display(vout_display_t *vd, const vout_display_cfg_t *cfg,
150                 const video_format_t *fmt);
151 
152 /* VLC vout display callbacks */
153 static picture_pool_t *vd_pool(vout_display_t *vd, unsigned count);
154 static void vd_prepare(vout_display_t *vd, picture_t *picture,
155                 subpicture_t *subpicture);
156 static void vd_display(vout_display_t *vd, picture_t *picture,
157                 subpicture_t *subpicture);
158 static int vd_control(vout_display_t *vd, int query, va_list args);
159 static void vd_manage(vout_display_t *vd);
160 
161 /* MMAL callbacks */
162 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
163 static void input_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
164 
165 /* TV service */
166 static int query_resolution(vout_display_t *vd, unsigned *width, unsigned *height);
167 static void tvservice_cb(void *callback_data, uint32_t reason, uint32_t param1,
168                 uint32_t param2);
169 static void adjust_refresh_rate(vout_display_t *vd, const video_format_t *fmt);
170 static int set_latency_target(vout_display_t *vd, bool enable);
171 
172 /* DispManX */
173 static void display_subpicture(vout_display_t *vd, subpicture_t *subpicture);
174 static void close_dmx(vout_display_t *vd);
175 static struct dmx_region_t *dmx_region_new(vout_display_t *vd,
176                 DISPMANX_UPDATE_HANDLE_T update, subpicture_region_t *region);
177 static void dmx_region_update(struct dmx_region_t *dmx_region,
178                 DISPMANX_UPDATE_HANDLE_T update, picture_t *picture);
179 static void dmx_region_delete(struct dmx_region_t *dmx_region,
180                 DISPMANX_UPDATE_HANDLE_T update);
181 static void show_background(vout_display_t *vd, bool enable);
182 static void maintain_phase_sync(vout_display_t *vd);
183 
Open(vlc_object_t * object)184 static int Open(vlc_object_t *object)
185 {
186     vout_display_t *vd = (vout_display_t *)object;
187     vout_display_sys_t *sys;
188     uint32_t buffer_pitch, buffer_height;
189     vout_display_place_t place;
190     MMAL_DISPLAYREGION_T display_region;
191     MMAL_STATUS_T status;
192     int ret = VLC_SUCCESS;
193     unsigned i;
194 
195     if (vout_display_IsWindowed(vd))
196         return VLC_EGENERIC;
197 
198     sys = calloc(1, sizeof(struct vout_display_sys_t));
199     if (!sys)
200         return VLC_ENOMEM;
201     vd->sys = sys;
202 
203     sys->layer = var_InheritInteger(vd, MMAL_LAYER_NAME);
204     bcm_host_init();
205 
206     sys->opaque = vd->fmt.i_chroma == VLC_CODEC_MMAL_OPAQUE;
207 
208     status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, &sys->component);
209     if (status != MMAL_SUCCESS) {
210         msg_Err(vd, "Failed to create MMAL component %s (status=%"PRIx32" %s)",
211                         MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, status, mmal_status_to_string(status));
212         ret = VLC_EGENERIC;
213         goto out;
214     }
215 
216     sys->component->control->userdata = (struct MMAL_PORT_USERDATA_T *)vd;
217     status = mmal_port_enable(sys->component->control, control_port_cb);
218     if (status != MMAL_SUCCESS) {
219         msg_Err(vd, "Failed to enable control port %s (status=%"PRIx32" %s)",
220                         sys->component->control->name, status, mmal_status_to_string(status));
221         ret = VLC_EGENERIC;
222         goto out;
223     }
224 
225     sys->input = sys->component->input[0];
226     sys->input->userdata = (struct MMAL_PORT_USERDATA_T *)vd;
227 
228     if (sys->opaque) {
229         sys->input->format->encoding = MMAL_ENCODING_OPAQUE;
230         sys->i_planes = 1;
231         sys->buffer_size = sys->input->buffer_size_recommended;
232     } else {
233         sys->input->format->encoding = MMAL_ENCODING_I420;
234         vd->fmt.i_chroma = VLC_CODEC_I420;
235         buffer_pitch = align(vd->fmt.i_width, 32);
236         buffer_height = align(vd->fmt.i_height, 16);
237         sys->i_planes = 3;
238         sys->buffer_size = 3 * buffer_pitch * buffer_height / 2;
239     }
240 
241     sys->input->format->es->video.width = vd->fmt.i_width;
242     sys->input->format->es->video.height = vd->fmt.i_height;
243     sys->input->format->es->video.crop.x = 0;
244     sys->input->format->es->video.crop.y = 0;
245     sys->input->format->es->video.crop.width = vd->fmt.i_width;
246     sys->input->format->es->video.crop.height = vd->fmt.i_height;
247     sys->input->format->es->video.par.num = vd->source.i_sar_num;
248     sys->input->format->es->video.par.den = vd->source.i_sar_den;
249 
250     status = mmal_port_format_commit(sys->input);
251     if (status != MMAL_SUCCESS) {
252         msg_Err(vd, "Failed to commit format for input port %s (status=%"PRIx32" %s)",
253                         sys->input->name, status, mmal_status_to_string(status));
254         ret = VLC_EGENERIC;
255         goto out;
256     }
257     sys->input->buffer_size = sys->input->buffer_size_recommended;
258 
259     vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
260     display_region.hdr.id = MMAL_PARAMETER_DISPLAYREGION;
261     display_region.hdr.size = sizeof(MMAL_DISPLAYREGION_T);
262     display_region.fullscreen = MMAL_FALSE;
263     display_region.src_rect.x = vd->fmt.i_x_offset;
264     display_region.src_rect.y = vd->fmt.i_y_offset;
265     display_region.src_rect.width = vd->fmt.i_visible_width;
266     display_region.src_rect.height = vd->fmt.i_visible_height;
267     display_region.dest_rect.x = place.x;
268     display_region.dest_rect.y = place.y;
269     display_region.dest_rect.width = place.width;
270     display_region.dest_rect.height = place.height;
271     display_region.layer = sys->layer;
272     display_region.set = MMAL_DISPLAY_SET_FULLSCREEN | MMAL_DISPLAY_SET_SRC_RECT |
273             MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_LAYER;
274     status = mmal_port_parameter_set(sys->input, &display_region.hdr);
275     if (status != MMAL_SUCCESS) {
276         msg_Err(vd, "Failed to set display region (status=%"PRIx32" %s)",
277                         status, mmal_status_to_string(status));
278         ret = VLC_EGENERIC;
279         goto out;
280     }
281 
282     for (i = 0; i < sys->i_planes; ++i) {
283         sys->planes[i].i_lines = buffer_height;
284         sys->planes[i].i_pitch = buffer_pitch;
285         sys->planes[i].i_visible_lines = vd->fmt.i_visible_height;
286         sys->planes[i].i_visible_pitch = vd->fmt.i_visible_width;
287 
288         if (i > 0) {
289             sys->planes[i].i_lines /= 2;
290             sys->planes[i].i_pitch /= 2;
291             sys->planes[i].i_visible_lines /= 2;
292             sys->planes[i].i_visible_pitch /= 2;
293         }
294     }
295 
296     vlc_mutex_init(&sys->buffer_mutex);
297     vlc_cond_init(&sys->buffer_cond);
298     vlc_mutex_init(&sys->manage_mutex);
299 
300     vd->pool = vd_pool;
301     vd->prepare = vd_prepare;
302     vd->display = vd_display;
303     vd->control = vd_control;
304     vd->manage = vd_manage;
305 
306     vc_tv_register_callback(tvservice_cb, vd);
307 
308     if (query_resolution(vd, &sys->display_width, &sys->display_height) >= 0) {
309         vout_display_SendEventDisplaySize(vd, sys->display_width, sys->display_height);
310     } else {
311         sys->display_width = vd->cfg->display.width;
312         sys->display_height = vd->cfg->display.height;
313     }
314 
315     sys->dmx_handle = vc_dispmanx_display_open(0);
316     vd->info.subpicture_chromas = subpicture_chromas;
317 
318     vout_display_DeleteWindow(vd, NULL);
319 
320 out:
321     if (ret != VLC_SUCCESS)
322         Close(object);
323 
324     return ret;
325 }
326 
Close(vlc_object_t * object)327 static void Close(vlc_object_t *object)
328 {
329     vout_display_t *vd = (vout_display_t *)object;
330     vout_display_sys_t *sys = vd->sys;
331     char response[20]; /* answer is hvs_update_fields=%1d */
332     unsigned i;
333 
334     vc_tv_unregister_callback_full(tvservice_cb, vd);
335 
336     if (sys->dmx_handle)
337         close_dmx(vd);
338 
339     if (sys->component && sys->component->control->is_enabled)
340         mmal_port_disable(sys->component->control);
341 
342     if (sys->input && sys->input->is_enabled)
343         mmal_port_disable(sys->input);
344 
345     if (sys->component && sys->component->is_enabled)
346         mmal_component_disable(sys->component);
347 
348     if (sys->pool)
349         mmal_port_pool_destroy(sys->input, sys->pool);
350 
351     if (sys->component)
352         mmal_component_release(sys->component);
353 
354     if (sys->picture_pool)
355         picture_pool_Release(sys->picture_pool);
356     else
357         for (i = 0; i < sys->num_buffers; ++i)
358             if (sys->pictures[i]) {
359                 mmal_buffer_header_release(sys->pictures[i]->p_sys->buffer);
360                 picture_Release(sys->pictures[i]);
361             }
362 
363     vlc_mutex_destroy(&sys->buffer_mutex);
364     vlc_cond_destroy(&sys->buffer_cond);
365     vlc_mutex_destroy(&sys->manage_mutex);
366 
367     if (sys->native_interlaced) {
368         if (vc_gencmd(response, sizeof(response), "hvs_update_fields 0") < 0 ||
369                 response[18] != '0')
370             msg_Warn(vd, "Could not reset hvs field mode");
371     }
372 
373     free(sys->pictures);
374     free(sys);
375 
376     bcm_host_deinit();
377 }
378 
align(uint32_t x,uint32_t y)379 static inline uint32_t align(uint32_t x, uint32_t y) {
380     uint32_t mod = x % y;
381     if (mod == 0)
382         return x;
383     else
384         return x + y - mod;
385 }
386 
configure_display(vout_display_t * vd,const vout_display_cfg_t * cfg,const video_format_t * fmt)387 static int configure_display(vout_display_t *vd, const vout_display_cfg_t *cfg,
388                 const video_format_t *fmt)
389 {
390     vout_display_sys_t *sys = vd->sys;
391     vout_display_place_t place;
392     MMAL_DISPLAYREGION_T display_region;
393     MMAL_STATUS_T status;
394 
395     if (!cfg && !fmt)
396         return -EINVAL;
397 
398     if (fmt) {
399         sys->input->format->es->video.par.num = fmt->i_sar_num;
400         sys->input->format->es->video.par.den = fmt->i_sar_den;
401 
402         status = mmal_port_format_commit(sys->input);
403         if (status != MMAL_SUCCESS) {
404             msg_Err(vd, "Failed to commit format for input port %s (status=%"PRIx32" %s)",
405                             sys->input->name, status, mmal_status_to_string(status));
406             return -EINVAL;
407         }
408     } else {
409         fmt = &vd->source;
410     }
411 
412     if (!cfg)
413         cfg = vd->cfg;
414 
415     vout_display_PlacePicture(&place, fmt, cfg, false);
416 
417     display_region.hdr.id = MMAL_PARAMETER_DISPLAYREGION;
418     display_region.hdr.size = sizeof(MMAL_DISPLAYREGION_T);
419     display_region.fullscreen = MMAL_FALSE;
420     display_region.src_rect.x = fmt->i_x_offset;
421     display_region.src_rect.y = fmt->i_y_offset;
422     display_region.src_rect.width = fmt->i_visible_width;
423     display_region.src_rect.height = fmt->i_visible_height;
424     display_region.dest_rect.x = place.x;
425     display_region.dest_rect.y = place.y;
426     display_region.dest_rect.width = place.width;
427     display_region.dest_rect.height = place.height;
428     display_region.layer = sys->layer;
429     display_region.set = MMAL_DISPLAY_SET_FULLSCREEN | MMAL_DISPLAY_SET_SRC_RECT |
430             MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_LAYER;
431     status = mmal_port_parameter_set(sys->input, &display_region.hdr);
432     if (status != MMAL_SUCCESS) {
433         msg_Err(vd, "Failed to set display region (status=%"PRIx32" %s)",
434                         status, mmal_status_to_string(status));
435         return -EINVAL;
436     }
437 
438     show_background(vd, var_InheritBool(vd, MMAL_BLANK_BACKGROUND_NAME));
439     sys->adjust_refresh_rate = var_InheritBool(vd, MMAL_ADJUST_REFRESHRATE_NAME);
440     sys->native_interlaced = var_InheritBool(vd, MMAL_NATIVE_INTERLACED);
441     if (sys->adjust_refresh_rate) {
442         adjust_refresh_rate(vd, fmt);
443         set_latency_target(vd, true);
444     }
445 
446     return 0;
447 }
448 
vd_pool(vout_display_t * vd,unsigned count)449 static picture_pool_t *vd_pool(vout_display_t *vd, unsigned count)
450 {
451     vout_display_sys_t *sys = vd->sys;
452     picture_resource_t picture_res;
453     picture_pool_configuration_t picture_pool_cfg;
454     video_format_t fmt = vd->fmt;
455     MMAL_STATUS_T status;
456     unsigned i;
457 
458     if (sys->picture_pool) {
459         if (sys->num_buffers < count)
460             msg_Warn(vd, "Picture pool with %u pictures requested, but we already have one with %u pictures",
461                             count, sys->num_buffers);
462 
463         goto out;
464     }
465 
466     if (sys->opaque) {
467         if (count <= NUM_ACTUAL_OPAQUE_BUFFERS)
468             count = NUM_ACTUAL_OPAQUE_BUFFERS;
469 
470         MMAL_PARAMETER_BOOLEAN_T zero_copy = {
471             { MMAL_PARAMETER_ZERO_COPY, sizeof(MMAL_PARAMETER_BOOLEAN_T) },
472             1
473         };
474 
475         status = mmal_port_parameter_set(sys->input, &zero_copy.hdr);
476         if (status != MMAL_SUCCESS) {
477            msg_Err(vd, "Failed to set zero copy on port %s (status=%"PRIx32" %s)",
478                     sys->input->name, status, mmal_status_to_string(status));
479            goto out;
480         }
481     }
482 
483     if (count < sys->input->buffer_num_recommended)
484         count = sys->input->buffer_num_recommended;
485 
486 #ifndef NDEBUG
487     msg_Dbg(vd, "Creating picture pool with %u pictures", count);
488 #endif
489 
490     sys->input->buffer_num = count;
491     status = mmal_port_enable(sys->input, input_port_cb);
492     if (status != MMAL_SUCCESS) {
493         msg_Err(vd, "Failed to enable input port %s (status=%"PRIx32" %s)",
494                         sys->input->name, status, mmal_status_to_string(status));
495         goto out;
496     }
497 
498     status = mmal_component_enable(sys->component);
499     if (status != MMAL_SUCCESS) {
500         msg_Err(vd, "Failed to enable component %s (status=%"PRIx32" %s)",
501                         sys->component->name, status, mmal_status_to_string(status));
502         goto out;
503     }
504 
505     sys->num_buffers = count;
506     sys->pool = mmal_port_pool_create(sys->input, sys->num_buffers,
507             sys->input->buffer_size);
508     if (!sys->pool) {
509         msg_Err(vd, "Failed to create MMAL pool for %u buffers of size %"PRIu32,
510                         count, sys->input->buffer_size);
511         goto out;
512     }
513 
514     memset(&picture_res, 0, sizeof(picture_resource_t));
515     sys->pictures = calloc(sys->num_buffers, sizeof(picture_t *));
516     for (i = 0; i < sys->num_buffers; ++i) {
517         picture_res.p_sys = calloc(1, sizeof(picture_sys_t));
518         picture_res.p_sys->owner = (vlc_object_t *)vd;
519         picture_res.p_sys->buffer = mmal_queue_get(sys->pool->queue);
520 
521         sys->pictures[i] = picture_NewFromResource(&fmt, &picture_res);
522         if (!sys->pictures[i]) {
523             msg_Err(vd, "Failed to create picture");
524             free(picture_res.p_sys);
525             goto out;
526         }
527 
528         sys->pictures[i]->i_planes = sys->i_planes;
529         memcpy(sys->pictures[i]->p, sys->planes, sys->i_planes * sizeof(plane_t));
530     }
531 
532     memset(&picture_pool_cfg, 0, sizeof(picture_pool_configuration_t));
533     picture_pool_cfg.picture_count = sys->num_buffers;
534     picture_pool_cfg.picture = sys->pictures;
535     picture_pool_cfg.lock = mmal_picture_lock;
536 
537     sys->picture_pool = picture_pool_NewExtended(&picture_pool_cfg);
538     if (!sys->picture_pool) {
539         msg_Err(vd, "Failed to create picture pool");
540         goto out;
541     }
542 
543 out:
544     return sys->picture_pool;
545 }
546 
vd_prepare(vout_display_t * vd,picture_t * picture,subpicture_t * subpicture)547 static void vd_prepare(vout_display_t *vd, picture_t *picture,
548                 subpicture_t *subpicture)
549 {
550     vout_display_sys_t *sys = vd->sys;
551     picture_sys_t *pic_sys = picture->p_sys;
552 
553     if (!sys->adjust_refresh_rate || pic_sys->displayed)
554         return;
555 
556     /* Apply the required phase_offset to the picture, so that vd_display()
557      * will be called at the corrected time from the core */
558     picture->date += sys->phase_offset;
559 }
560 
vd_display(vout_display_t * vd,picture_t * picture,subpicture_t * subpicture)561 static void vd_display(vout_display_t *vd, picture_t *picture,
562                 subpicture_t *subpicture)
563 {
564     vout_display_sys_t *sys = vd->sys;
565     picture_sys_t *pic_sys = picture->p_sys;
566     MMAL_BUFFER_HEADER_T *buffer = pic_sys->buffer;
567     MMAL_STATUS_T status;
568 
569     if (picture->format.i_frame_rate != sys->i_frame_rate ||
570         picture->format.i_frame_rate_base != sys->i_frame_rate_base ||
571         picture->b_progressive != sys->b_progressive ||
572         picture->b_top_field_first != sys->b_top_field_first) {
573         sys->b_top_field_first = picture->b_top_field_first;
574         sys->b_progressive = picture->b_progressive;
575         sys->i_frame_rate = picture->format.i_frame_rate;
576         sys->i_frame_rate_base = picture->format.i_frame_rate_base;
577         configure_display(vd, NULL, &picture->format);
578     }
579 
580     if (!pic_sys->displayed || !sys->opaque) {
581         buffer->cmd = 0;
582         buffer->length = sys->input->buffer_size;
583         buffer->user_data = picture;
584 
585         status = mmal_port_send_buffer(sys->input, buffer);
586         if (status == MMAL_SUCCESS)
587             atomic_fetch_add(&sys->buffers_in_transit, 1);
588 
589         if (status != MMAL_SUCCESS) {
590             msg_Err(vd, "Failed to send buffer to input port. Frame dropped");
591             picture_Release(picture);
592         }
593 
594         pic_sys->displayed = true;
595     } else {
596         picture_Release(picture);
597     }
598 
599     display_subpicture(vd, subpicture);
600 
601     if (subpicture)
602         subpicture_Delete(subpicture);
603 
604     if (sys->next_phase_check == 0 && sys->adjust_refresh_rate)
605         maintain_phase_sync(vd);
606     sys->next_phase_check = (sys->next_phase_check + 1) % PHASE_CHECK_INTERVAL;
607 
608     if (sys->opaque) {
609         vlc_mutex_lock(&sys->buffer_mutex);
610         while (atomic_load(&sys->buffers_in_transit) >= MAX_BUFFERS_IN_TRANSIT)
611             vlc_cond_wait(&sys->buffer_cond, &sys->buffer_mutex);
612         vlc_mutex_unlock(&sys->buffer_mutex);
613     }
614 }
615 
vd_control(vout_display_t * vd,int query,va_list args)616 static int vd_control(vout_display_t *vd, int query, va_list args)
617 {
618     vout_display_sys_t *sys = vd->sys;
619     vout_display_cfg_t cfg;
620     const vout_display_cfg_t *tmp_cfg;
621     int ret = VLC_EGENERIC;
622 
623     switch (query) {
624         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
625             tmp_cfg = va_arg(args, const vout_display_cfg_t *);
626             if (tmp_cfg->display.width == sys->display_width &&
627                             tmp_cfg->display.height == sys->display_height) {
628                 cfg = *vd->cfg;
629                 cfg.display.width = sys->display_width;
630                 cfg.display.height = sys->display_height;
631                 if (configure_display(vd, &cfg, NULL) >= 0)
632                     ret = VLC_SUCCESS;
633             }
634             break;
635 
636         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
637         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
638             if (configure_display(vd, NULL, &vd->source) >= 0)
639                 ret = VLC_SUCCESS;
640             break;
641 
642         case VOUT_DISPLAY_RESET_PICTURES:
643             vlc_assert_unreachable();
644         case VOUT_DISPLAY_CHANGE_ZOOM:
645             msg_Warn(vd, "Unsupported control query %d", query);
646             break;
647 
648         default:
649             msg_Warn(vd, "Unknown control query %d", query);
650             break;
651     }
652 
653     return ret;
654 }
655 
vd_manage(vout_display_t * vd)656 static void vd_manage(vout_display_t *vd)
657 {
658     vout_display_sys_t *sys = vd->sys;
659     unsigned width, height;
660 
661     vlc_mutex_lock(&sys->manage_mutex);
662 
663     if (sys->need_configure_display) {
664         close_dmx(vd);
665         sys->dmx_handle = vc_dispmanx_display_open(0);
666 
667         if (query_resolution(vd, &width, &height) >= 0) {
668             sys->display_width = width;
669             sys->display_height = height;
670             vout_display_SendEventDisplaySize(vd, width, height);
671         }
672 
673         sys->need_configure_display = false;
674     }
675 
676     vlc_mutex_unlock(&sys->manage_mutex);
677 }
678 
control_port_cb(MMAL_PORT_T * port,MMAL_BUFFER_HEADER_T * buffer)679 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
680 {
681     vout_display_t *vd = (vout_display_t *)port->userdata;
682     MMAL_STATUS_T status;
683 
684     if (buffer->cmd == MMAL_EVENT_ERROR) {
685         status = *(uint32_t *)buffer->data;
686         msg_Err(vd, "MMAL error %"PRIx32" \"%s\"", status, mmal_status_to_string(status));
687     }
688 
689     mmal_buffer_header_release(buffer);
690 }
691 
input_port_cb(MMAL_PORT_T * port,MMAL_BUFFER_HEADER_T * buffer)692 static void input_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
693 {
694     vout_display_t *vd = (vout_display_t *)port->userdata;
695     vout_display_sys_t *sys = vd->sys;
696     picture_t *picture = (picture_t *)buffer->user_data;
697 
698     if (picture)
699         picture_Release(picture);
700 
701     vlc_mutex_lock(&sys->buffer_mutex);
702     atomic_fetch_sub(&sys->buffers_in_transit, 1);
703     vlc_cond_signal(&sys->buffer_cond);
704     vlc_mutex_unlock(&sys->buffer_mutex);
705 }
706 
query_resolution(vout_display_t * vd,unsigned * width,unsigned * height)707 static int query_resolution(vout_display_t *vd, unsigned *width, unsigned *height)
708 {
709     TV_DISPLAY_STATE_T display_state;
710     int ret = 0;
711 
712     if (vc_tv_get_display_state(&display_state) == 0) {
713         if (display_state.state & 0xFF) {
714             *width = display_state.display.hdmi.width;
715             *height = display_state.display.hdmi.height;
716         } else if (display_state.state & 0xFF00) {
717             *width = display_state.display.sdtv.width;
718             *height = display_state.display.sdtv.height;
719         } else {
720             msg_Warn(vd, "Invalid display state %"PRIx32, display_state.state);
721             ret = -1;
722         }
723     } else {
724         msg_Warn(vd, "Failed to query display resolution");
725         ret = -1;
726     }
727 
728     return ret;
729 }
730 
tvservice_cb(void * callback_data,uint32_t reason,uint32_t param1,uint32_t param2)731 static void tvservice_cb(void *callback_data, uint32_t reason, uint32_t param1, uint32_t param2)
732 {
733     VLC_UNUSED(reason);
734     VLC_UNUSED(param1);
735     VLC_UNUSED(param2);
736 
737     vout_display_t *vd = (vout_display_t *)callback_data;
738     vout_display_sys_t *sys = vd->sys;
739 
740     vlc_mutex_lock(&sys->manage_mutex);
741     sys->need_configure_display = true;
742     vlc_mutex_unlock(&sys->manage_mutex);
743 }
744 
set_latency_target(vout_display_t * vd,bool enable)745 static int set_latency_target(vout_display_t *vd, bool enable)
746 {
747     vout_display_sys_t *sys = vd->sys;
748     MMAL_STATUS_T status;
749 
750     MMAL_PARAMETER_AUDIO_LATENCY_TARGET_T latency_target = {
751         .hdr = { MMAL_PARAMETER_AUDIO_LATENCY_TARGET, sizeof(latency_target) },
752         .enable = enable ? MMAL_TRUE : MMAL_FALSE,
753         .filter = 2,
754         .target = 4000,
755         .shift = 3,
756         .speed_factor = -135,
757         .inter_factor = 500,
758         .adj_cap = 20
759     };
760 
761     status = mmal_port_parameter_set(sys->input, &latency_target.hdr);
762     if (status != MMAL_SUCCESS) {
763         msg_Err(vd, "Failed to configure latency target on input port %s (status=%"PRIx32" %s)",
764                         sys->input->name, status, mmal_status_to_string(status));
765         return VLC_EGENERIC;
766     }
767 
768     return VLC_SUCCESS;
769 }
770 
adjust_refresh_rate(vout_display_t * vd,const video_format_t * fmt)771 static void adjust_refresh_rate(vout_display_t *vd, const video_format_t *fmt)
772 {
773     vout_display_sys_t *sys = vd->sys;
774     TV_DISPLAY_STATE_T display_state;
775     TV_SUPPORTED_MODE_NEW_T supported_modes[VC_TV_MAX_MODE_IDS];
776     char response[20]; /* answer is hvs_update_fields=%1d */
777     int num_modes;
778     double frame_rate = (double)fmt->i_frame_rate / fmt->i_frame_rate_base;
779     int best_id = -1;
780     double best_score, score;
781     int i;
782 
783     vc_tv_get_display_state(&display_state);
784     if(display_state.display.hdmi.mode != HDMI_MODE_OFF) {
785         num_modes = vc_tv_hdmi_get_supported_modes_new(display_state.display.hdmi.group,
786                         supported_modes, VC_TV_MAX_MODE_IDS, NULL, NULL);
787 
788         for (i = 0; i < num_modes; ++i) {
789             TV_SUPPORTED_MODE_NEW_T *mode = &supported_modes[i];
790             if (!sys->native_interlaced) {
791                 if (mode->width != display_state.display.hdmi.width ||
792                                 mode->height != display_state.display.hdmi.height ||
793                                 mode->scan_mode == HDMI_INTERLACED)
794                     continue;
795             } else {
796                 if (mode->width != vd->fmt.i_visible_width ||
797                         mode->height != vd->fmt.i_visible_height)
798                     continue;
799                 if (mode->scan_mode != sys->b_progressive ? HDMI_NONINTERLACED : HDMI_INTERLACED)
800                     continue;
801             }
802 
803             score = fmod(supported_modes[i].frame_rate, frame_rate);
804             if((best_id < 0) || (score < best_score)) {
805                 best_id = i;
806                 best_score = score;
807             }
808         }
809 
810         if((best_id >= 0) && (display_state.display.hdmi.mode != supported_modes[best_id].code)) {
811             msg_Info(vd, "Setting HDMI refresh rate to %"PRIu32,
812                             supported_modes[best_id].frame_rate);
813             vc_tv_hdmi_power_on_explicit_new(HDMI_MODE_HDMI,
814                             supported_modes[best_id].group,
815                             supported_modes[best_id].code);
816         }
817 
818         if (sys->native_interlaced &&
819                 supported_modes[best_id].scan_mode == HDMI_INTERLACED) {
820             char hvs_mode = sys->b_top_field_first ? '1' : '2';
821             if (vc_gencmd(response, sizeof(response), "hvs_update_fields %c",
822                     hvs_mode) != 0 || response[18] != hvs_mode)
823                 msg_Warn(vd, "Could not set hvs field mode");
824             else
825                 msg_Info(vd, "Configured hvs field mode for interlaced %s playback",
826                         sys->b_top_field_first ? "tff" : "bff");
827         }
828     }
829 }
830 
display_subpicture(vout_display_t * vd,subpicture_t * subpicture)831 static void display_subpicture(vout_display_t *vd, subpicture_t *subpicture)
832 {
833     vout_display_sys_t *sys = vd->sys;
834     struct dmx_region_t **dmx_region = &sys->dmx_region;
835     struct dmx_region_t *unused_dmx_region;
836     DISPMANX_UPDATE_HANDLE_T update = 0;
837     picture_t *picture;
838     video_format_t *fmt;
839     struct dmx_region_t *dmx_region_next;
840 
841     if(subpicture) {
842         subpicture_region_t *region = subpicture->p_region;
843         while(region) {
844             picture = region->p_picture;
845             fmt = &region->fmt;
846 
847             if(!*dmx_region) {
848                 if(!update)
849                     update = vc_dispmanx_update_start(10);
850                 *dmx_region = dmx_region_new(vd, update, region);
851             } else if(((*dmx_region)->bmp_rect.width != (int32_t)fmt->i_visible_width) ||
852                     ((*dmx_region)->bmp_rect.height != (int32_t)fmt->i_visible_height) ||
853                     ((*dmx_region)->pos_x != region->i_x) ||
854                     ((*dmx_region)->pos_y != region->i_y) ||
855                     ((*dmx_region)->alpha.opacity != (uint32_t)region->i_alpha)) {
856                 dmx_region_next = (*dmx_region)->next;
857                 if(!update)
858                     update = vc_dispmanx_update_start(10);
859                 dmx_region_delete(*dmx_region, update);
860                 *dmx_region = dmx_region_new(vd, update, region);
861                 (*dmx_region)->next = dmx_region_next;
862             } else if((*dmx_region)->picture != picture) {
863                 if(!update)
864                     update = vc_dispmanx_update_start(10);
865                 dmx_region_update(*dmx_region, update, picture);
866             }
867 
868             dmx_region = &(*dmx_region)->next;
869             region = region->p_next;
870         }
871     }
872 
873     /* Remove remaining regions */
874     unused_dmx_region = *dmx_region;
875     while(unused_dmx_region) {
876         dmx_region_next = unused_dmx_region->next;
877         if(!update)
878             update = vc_dispmanx_update_start(10);
879         dmx_region_delete(unused_dmx_region, update);
880         unused_dmx_region = dmx_region_next;
881     }
882     *dmx_region = NULL;
883 
884     if(update)
885         vc_dispmanx_update_submit_sync(update);
886 }
887 
close_dmx(vout_display_t * vd)888 static void close_dmx(vout_display_t *vd)
889 {
890     vout_display_sys_t *sys = vd->sys;
891     DISPMANX_UPDATE_HANDLE_T update = vc_dispmanx_update_start(10);
892     struct dmx_region_t *dmx_region = sys->dmx_region;
893     struct dmx_region_t *dmx_region_next;
894 
895     while(dmx_region) {
896         dmx_region_next = dmx_region->next;
897         dmx_region_delete(dmx_region, update);
898         dmx_region = dmx_region_next;
899     }
900 
901     vc_dispmanx_update_submit_sync(update);
902     sys->dmx_region = NULL;
903 
904     show_background(vd, false);
905 
906     vc_dispmanx_display_close(sys->dmx_handle);
907     sys->dmx_handle = DISPMANX_NO_HANDLE;
908 }
909 
dmx_region_new(vout_display_t * vd,DISPMANX_UPDATE_HANDLE_T update,subpicture_region_t * region)910 static struct dmx_region_t *dmx_region_new(vout_display_t *vd,
911                 DISPMANX_UPDATE_HANDLE_T update, subpicture_region_t *region)
912 {
913     vout_display_sys_t *sys = vd->sys;
914     video_format_t *fmt = &region->fmt;
915     struct dmx_region_t *dmx_region = malloc(sizeof(struct dmx_region_t));
916     uint32_t image_handle;
917 
918     dmx_region->pos_x = region->i_x;
919     dmx_region->pos_y = region->i_y;
920 
921     vc_dispmanx_rect_set(&dmx_region->bmp_rect, 0, 0, fmt->i_visible_width,
922                     fmt->i_visible_height);
923     vc_dispmanx_rect_set(&dmx_region->src_rect, 0, 0, fmt->i_visible_width << 16,
924                     fmt->i_visible_height << 16);
925     vc_dispmanx_rect_set(&dmx_region->dst_rect, region->i_x, region->i_y,
926                     fmt->i_visible_width, fmt->i_visible_height);
927 
928     dmx_region->resource = vc_dispmanx_resource_create(VC_IMAGE_RGBA32,
929                     dmx_region->bmp_rect.width | (region->p_picture->p[0].i_pitch << 16),
930                     dmx_region->bmp_rect.height | (dmx_region->bmp_rect.height << 16),
931                     &image_handle);
932     vc_dispmanx_resource_write_data(dmx_region->resource, VC_IMAGE_RGBA32,
933                     region->p_picture->p[0].i_pitch,
934                     region->p_picture->p[0].p_pixels, &dmx_region->bmp_rect);
935 
936     dmx_region->alpha.flags = DISPMANX_FLAGS_ALPHA_FROM_SOURCE | DISPMANX_FLAGS_ALPHA_MIX;
937     dmx_region->alpha.opacity = region->i_alpha;
938     dmx_region->alpha.mask = DISPMANX_NO_HANDLE;
939     dmx_region->element = vc_dispmanx_element_add(update, sys->dmx_handle,
940                     sys->layer + 1, &dmx_region->dst_rect, dmx_region->resource,
941                     &dmx_region->src_rect, DISPMANX_PROTECTION_NONE,
942                     &dmx_region->alpha, NULL, VC_IMAGE_ROT0);
943 
944     dmx_region->next = NULL;
945     dmx_region->picture = region->p_picture;
946 
947     return dmx_region;
948 }
949 
dmx_region_update(struct dmx_region_t * dmx_region,DISPMANX_UPDATE_HANDLE_T update,picture_t * picture)950 static void dmx_region_update(struct dmx_region_t *dmx_region,
951                 DISPMANX_UPDATE_HANDLE_T update, picture_t *picture)
952 {
953     vc_dispmanx_resource_write_data(dmx_region->resource, VC_IMAGE_RGBA32,
954                     picture->p[0].i_pitch, picture->p[0].p_pixels, &dmx_region->bmp_rect);
955     vc_dispmanx_element_change_source(update, dmx_region->element, dmx_region->resource);
956     dmx_region->picture = picture;
957 }
958 
dmx_region_delete(struct dmx_region_t * dmx_region,DISPMANX_UPDATE_HANDLE_T update)959 static void dmx_region_delete(struct dmx_region_t *dmx_region,
960                 DISPMANX_UPDATE_HANDLE_T update)
961 {
962     vc_dispmanx_element_remove(update, dmx_region->element);
963     vc_dispmanx_resource_delete(dmx_region->resource);
964     free(dmx_region);
965 }
966 
maintain_phase_sync(vout_display_t * vd)967 static void maintain_phase_sync(vout_display_t *vd)
968 {
969     MMAL_PARAMETER_VIDEO_RENDER_STATS_T render_stats = {
970         .hdr = { MMAL_PARAMETER_VIDEO_RENDER_STATS, sizeof(render_stats) },
971     };
972     int32_t frame_duration = 1000000 /
973         ((double)vd->sys->i_frame_rate /
974         vd->sys->i_frame_rate_base);
975     vout_display_sys_t *sys = vd->sys;
976     int32_t phase_offset;
977     MMAL_STATUS_T status;
978 
979     status = mmal_port_parameter_get(sys->input, &render_stats.hdr);
980     if (status != MMAL_SUCCESS) {
981         msg_Err(vd, "Failed to read render stats on control port %s (status=%"PRIx32" %s)",
982                         sys->input->name, status, mmal_status_to_string(status));
983         return;
984     }
985 
986     if (render_stats.valid) {
987 #ifndef NDEBUG
988         msg_Dbg(vd, "render_stats: match: %u, period: %u ms, phase: %u ms, hvs: %u",
989                 render_stats.match, render_stats.period / 1000, render_stats.phase / 1000,
990                 render_stats.hvs_status);
991 #endif
992 
993         if (render_stats.phase > 0.1 * frame_duration &&
994                 render_stats.phase < 0.75 * frame_duration)
995             return;
996 
997         phase_offset = frame_duration * PHASE_OFFSET_TARGET - render_stats.phase;
998         if (phase_offset < 0)
999             phase_offset += frame_duration;
1000         else
1001             phase_offset %= frame_duration;
1002 
1003         sys->phase_offset += phase_offset;
1004         sys->phase_offset %= frame_duration;
1005         msg_Dbg(vd, "Apply phase offset of %"PRId32" ms (total offset %"PRId32" ms)",
1006                 phase_offset / 1000, sys->phase_offset / 1000);
1007 
1008         /* Reset the latency target, so that it does not get confused
1009          * by the jump in the offset */
1010         set_latency_target(vd, false);
1011         set_latency_target(vd, true);
1012     }
1013 }
1014 
show_background(vout_display_t * vd,bool enable)1015 static void show_background(vout_display_t *vd, bool enable)
1016 {
1017     vout_display_sys_t *sys = vd->sys;
1018     uint32_t image_ptr, color = 0xFF000000;
1019     VC_RECT_T dst_rect, src_rect;
1020     DISPMANX_UPDATE_HANDLE_T update;
1021 
1022     if (enable && !sys->bkg_element) {
1023         sys->bkg_resource = vc_dispmanx_resource_create(VC_IMAGE_RGBA32, 1, 1,
1024                         &image_ptr);
1025         vc_dispmanx_rect_set(&dst_rect, 0, 0, 1, 1);
1026         vc_dispmanx_resource_write_data(sys->bkg_resource, VC_IMAGE_RGBA32,
1027                         sizeof(color), &color, &dst_rect);
1028         vc_dispmanx_rect_set(&src_rect, 0, 0, 1 << 16, 1 << 16);
1029         vc_dispmanx_rect_set(&dst_rect, 0, 0, 0, 0);
1030         update = vc_dispmanx_update_start(0);
1031         sys->bkg_element = vc_dispmanx_element_add(update, sys->dmx_handle,
1032                         sys->layer - 1, &dst_rect, sys->bkg_resource, &src_rect,
1033                         DISPMANX_PROTECTION_NONE, NULL, NULL, VC_IMAGE_ROT0);
1034         vc_dispmanx_update_submit_sync(update);
1035     } else if (!enable && sys->bkg_element) {
1036         update = vc_dispmanx_update_start(0);
1037         vc_dispmanx_element_remove(update, sys->bkg_element);
1038         vc_dispmanx_resource_delete(sys->bkg_resource);
1039         vc_dispmanx_update_submit_sync(update);
1040         sys->bkg_element = DISPMANX_NO_HANDLE;
1041         sys->bkg_resource = DISPMANX_NO_HANDLE;
1042     }
1043 }
1044