]> git.sesse.net Git - vlc/blob - modules/hw/mmal/vout.c
mmal/vout: Fix memory leak
[vlc] / modules / hw / mmal / vout.c
1 /*****************************************************************************
2  * mmal.c: MMAL-based vout plugin for Raspberry Pi
3  *****************************************************************************
4  * Copyright © 2014 jusst technologies GmbH
5  * $Id$
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_plugin.h>
33 #include <vlc_threads.h>
34 #include <vlc_vout_display.h>
35
36 #include <bcm_host.h>
37 #include <interface/mmal/mmal.h>
38 #include <interface/mmal/util/mmal_util.h>
39 #include <interface/mmal/util/mmal_default_components.h>
40 #include <interface/vmcs_host/vc_tvservice.h>
41 #include <interface/vmcs_host/vc_dispmanx.h>
42
43 /* This value must match the define in codec/mmal.c
44  * Think twice before changing this. Incorrect values cause havoc.
45  */
46 #define NUM_ACTUAL_OPAQUE_BUFFERS 22
47
48 #define MAX_BUFFERS_IN_TRANSIT 2
49 #define VC_TV_MAX_MODE_IDS 127
50
51 #define MMAL_LAYER_NAME "mmal-layer"
52 #define MMAL_LAYER_TEXT N_("VideoCore layer where the video is displayed.")
53 #define MMAL_LAYER_LONGTEXT N_("VideoCore layer where the video is displayed. Subpictures are displayed directly above and a black background directly below.")
54
55 #define MMAL_ADJUST_REFRESHRATE_NAME "mmal-adjust-refreshrate"
56 #define MMAL_ADJUST_REFRESHRATE_TEXT N_("Adjust HDMI refresh rate to the video.")
57 #define MMAL_ADJUST_REFRESHRATE_LONGTEXT N_("Adjust HDMI refresh rate to the video.")
58
59 static int Open(vlc_object_t *);
60 static void Close(vlc_object_t *);
61
62 vlc_module_begin()
63     set_shortname(N_("MMAL vout"))
64     set_description(N_("MMAL-based vout plugin for Raspberry Pi"))
65     set_capability("vout display", 90)
66     add_shortcut("mmal_vout")
67     add_integer(MMAL_LAYER_NAME, 1, MMAL_LAYER_TEXT, MMAL_LAYER_LONGTEXT, false)
68     add_bool(MMAL_ADJUST_REFRESHRATE_NAME, false, MMAL_ADJUST_REFRESHRATE_TEXT,
69                     MMAL_ADJUST_REFRESHRATE_LONGTEXT, false)
70     set_callbacks(Open, Close)
71 vlc_module_end()
72
73 struct dmx_region_t {
74     struct dmx_region_t *next;
75     picture_t *picture;
76     VC_RECT_T bmp_rect;
77     VC_RECT_T src_rect;
78     VC_RECT_T dst_rect;
79     VC_DISPMANX_ALPHA_T alpha;
80     DISPMANX_ELEMENT_HANDLE_T element;
81     DISPMANX_RESOURCE_HANDLE_T resource;
82     int32_t pos_x;
83     int32_t pos_y;
84 };
85
86 struct vout_display_sys_t {
87     picture_t **pictures;
88     picture_pool_t *picture_pool;
89
90     MMAL_COMPONENT_T *component;
91     MMAL_PORT_T *input;
92     MMAL_POOL_T *pool;
93     struct dmx_region_t *dmx_region;
94     plane_t planes[3];
95
96     vlc_mutex_t buffer_mutex;
97     vlc_mutex_t manage_mutex;
98     vlc_cond_t buffer_cond;
99     uint32_t buffer_size;
100     int buffers_in_transit;
101     unsigned num_buffers;
102
103     DISPMANX_DISPLAY_HANDLE_T dmx_handle;
104     DISPMANX_ELEMENT_HANDLE_T bkg_element;
105     DISPMANX_RESOURCE_HANDLE_T bkg_resource;
106     unsigned display_width;
107     unsigned display_height;
108     bool need_configure_display;
109
110     int layer;
111     bool opaque;
112 };
113
114 struct picture_sys_t {
115     vout_display_t *vd;
116     MMAL_BUFFER_HEADER_T *buffer;
117     bool displayed;
118 };
119
120 static const vlc_fourcc_t subpicture_chromas[] = {
121     VLC_CODEC_RGBA,
122     0
123 };
124
125 /* Utility functions */
126 static inline uint32_t align(uint32_t x, uint32_t y);
127 static int configure_display(vout_display_t *vd, const vout_display_cfg_t *cfg,
128                 const video_format_t *fmt);
129
130 /* VLC vout display callbacks */
131 static picture_pool_t *vd_pool(vout_display_t *vd, unsigned count);
132 static void vd_display(vout_display_t *vd, picture_t *picture,
133                 subpicture_t *subpicture);
134 static int vd_control(vout_display_t *vd, int query, va_list args);
135 static void vd_manage(vout_display_t *vd);
136
137 /* VLC picture pool */
138 static int picture_lock(picture_t *picture);
139 static void picture_unlock(picture_t *picture);
140
141 /* MMAL callbacks */
142 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
143 static void input_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
144
145 /* TV service */
146 static int query_resolution(vout_display_t *vd, unsigned *width, unsigned *height);
147 static void tvservice_cb(void *callback_data, uint32_t reason, uint32_t param1,
148                 uint32_t param2);
149 static void adjust_refresh_rate(vout_display_t *vd);
150
151 /* DispManX */
152 static void display_subpicture(vout_display_t *vd, subpicture_t *subpicture);
153 static void close_dmx(vout_display_t *vd);
154 static struct dmx_region_t *dmx_region_new(vout_display_t *vd,
155                 DISPMANX_UPDATE_HANDLE_T update, subpicture_region_t *region);
156 static void dmx_region_update(struct dmx_region_t *dmx_region,
157                 DISPMANX_UPDATE_HANDLE_T update, picture_t *picture);
158 static void dmx_region_delete(struct dmx_region_t *dmx_region,
159                 DISPMANX_UPDATE_HANDLE_T update);
160 static void show_background(vout_display_t *vd, bool enable);
161
162 static int Open(vlc_object_t *object)
163 {
164     vout_display_t *vd = (vout_display_t *)object;
165     vout_display_sys_t *sys;
166     uint32_t buffer_pitch, buffer_height;
167     vout_display_place_t place;
168     MMAL_DISPLAYREGION_T display_region;
169     uint32_t offsets[3];
170     MMAL_STATUS_T status;
171     int ret = VLC_SUCCESS;
172     unsigned i;
173
174     sys = calloc(1, sizeof(struct vout_display_sys_t));
175     if (!sys)
176         return VLC_ENOMEM;
177     vd->sys = sys;
178
179     sys->layer = var_InheritInteger(vd, MMAL_LAYER_NAME);
180     bcm_host_init();
181
182     vd->info.has_hide_mouse = true;
183     sys->opaque = vd->fmt.i_chroma == VLC_CODEC_MMAL_OPAQUE;
184
185     if (!sys->opaque)
186         vd->fmt.i_chroma = VLC_CODEC_I420;
187     vd->fmt.i_sar_num = vd->source.i_sar_num;
188     vd->fmt.i_sar_den = vd->source.i_sar_den;
189
190     buffer_pitch = align(vd->fmt.i_width, 32);
191     buffer_height = align(vd->fmt.i_height, 16);
192     sys->buffer_size = 3 * buffer_pitch * buffer_height / 2;
193
194     status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, &sys->component);
195     if (status != MMAL_SUCCESS) {
196         msg_Err(vd, "Failed to create MMAL component %s (status=%"PRIx32" %s)",
197                         MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, status, mmal_status_to_string(status));
198         ret = VLC_EGENERIC;
199         goto out;
200     }
201
202     sys->component->control->userdata = (struct MMAL_PORT_USERDATA_T *)vd;
203     status = mmal_port_enable(sys->component->control, control_port_cb);
204     if (status != MMAL_SUCCESS) {
205         msg_Err(vd, "Failed to enable control port %s (status=%"PRIx32" %s)",
206                         sys->component->control->name, status, mmal_status_to_string(status));
207         ret = VLC_EGENERIC;
208         goto out;
209     }
210
211     sys->input = sys->component->input[0];
212     sys->input->userdata = (struct MMAL_PORT_USERDATA_T *)vd;
213     if (sys->opaque)
214         sys->input->format->encoding = MMAL_ENCODING_OPAQUE;
215     else
216         sys->input->format->encoding = MMAL_ENCODING_I420;
217     sys->input->format->es->video.width = vd->fmt.i_width;
218     sys->input->format->es->video.height = vd->fmt.i_height;
219     sys->input->format->es->video.crop.x = 0;
220     sys->input->format->es->video.crop.y = 0;
221     sys->input->format->es->video.crop.width = vd->fmt.i_width;
222     sys->input->format->es->video.crop.height = vd->fmt.i_height;
223     sys->input->format->es->video.par.num = vd->source.i_sar_num;
224     sys->input->format->es->video.par.den = vd->source.i_sar_den;
225
226     status = mmal_port_format_commit(sys->input);
227     if (status != MMAL_SUCCESS) {
228         msg_Err(vd, "Failed to commit format for input port %s (status=%"PRIx32" %s)",
229                         sys->input->name, status, mmal_status_to_string(status));
230         ret = VLC_EGENERIC;
231         goto out;
232     }
233     sys->input->buffer_size = sys->input->buffer_size_recommended;
234
235     vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
236     display_region.hdr.id = MMAL_PARAMETER_DISPLAYREGION;
237     display_region.hdr.size = sizeof(MMAL_DISPLAYREGION_T);
238     display_region.fullscreen = MMAL_FALSE;
239     display_region.src_rect.x = vd->fmt.i_x_offset;
240     display_region.src_rect.y = vd->fmt.i_y_offset;
241     display_region.src_rect.width = vd->fmt.i_visible_width;
242     display_region.src_rect.height = vd->fmt.i_visible_height;
243     display_region.dest_rect.x = place.x;
244     display_region.dest_rect.y = place.y;
245     display_region.dest_rect.width = place.width;
246     display_region.dest_rect.height = place.height;
247     display_region.layer = sys->layer;
248     display_region.set = MMAL_DISPLAY_SET_FULLSCREEN | MMAL_DISPLAY_SET_SRC_RECT |
249             MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_LAYER;
250     status = mmal_port_parameter_set(sys->input, &display_region.hdr);
251     if (status != MMAL_SUCCESS) {
252         msg_Err(vd, "Failed to set display region (status=%"PRIx32" %s)",
253                         status, mmal_status_to_string(status));
254         ret = VLC_EGENERIC;
255         goto out;
256     }
257
258     offsets[0] = 0;
259     for (i = 0; i < 3; ++i) {
260         sys->planes[i].i_lines = buffer_height;
261         sys->planes[i].i_pitch = buffer_pitch;
262         sys->planes[i].i_visible_lines = vd->fmt.i_visible_height;
263         sys->planes[i].i_visible_pitch = vd->fmt.i_visible_width;
264
265         if (i > 0) {
266             offsets[i] = offsets[i - 1] + sys->planes[i - 1].i_pitch * sys->planes[i - 1].i_lines;
267             sys->planes[i].i_lines /= 2;
268             sys->planes[i].i_pitch /= 2;
269             sys->planes[i].i_visible_lines /= 2;
270             sys->planes[i].i_visible_pitch /= 2;
271         }
272
273         sys->planes[i].p_pixels = (uint8_t *)offsets[i];
274     }
275
276     vlc_mutex_init(&sys->buffer_mutex);
277     vlc_cond_init(&sys->buffer_cond);
278     vlc_mutex_init(&sys->manage_mutex);
279
280     vd->pool = vd_pool;
281     vd->display = vd_display;
282     vd->control = vd_control;
283     vd->manage = vd_manage;
284
285     vc_tv_register_callback(tvservice_cb, vd);
286
287     if (query_resolution(vd, &sys->display_width, &sys->display_height) >= 0) {
288         vout_display_SendEventDisplaySize(vd, sys->display_width, sys->display_height,
289                         vd->cfg->is_fullscreen);
290     } else {
291         sys->display_width = vd->cfg->display.width;
292         sys->display_height = vd->cfg->display.height;
293     }
294
295     sys->dmx_handle = vc_dispmanx_display_open(0);
296     vd->info.subpicture_chromas = subpicture_chromas;
297
298 out:
299     if (ret != VLC_SUCCESS)
300         Close(object);
301
302     return ret;
303 }
304
305 static void Close(vlc_object_t *object)
306 {
307     vout_display_t *vd = (vout_display_t *)object;
308     vout_display_sys_t *sys = vd->sys;
309     unsigned i;
310
311     vc_tv_unregister_callback_full(tvservice_cb, vd);
312
313     if (sys->dmx_handle)
314         close_dmx(vd);
315
316     if (sys->component && sys->component->control->is_enabled)
317         mmal_port_disable(sys->component->control);
318
319     if (sys->input && sys->input->is_enabled)
320         mmal_port_disable(sys->input);
321
322     if (sys->component && sys->component->is_enabled)
323         mmal_component_disable(sys->component);
324
325     if (sys->pool)
326         mmal_pool_destroy(sys->pool);
327
328     if (sys->component)
329         mmal_component_release(sys->component);
330
331     if (sys->picture_pool)
332         picture_pool_Delete(sys->picture_pool);
333     else
334         for (i = 0; i < sys->num_buffers; ++i)
335             if (sys->pictures[i])
336                 picture_Release(sys->pictures[i]);
337
338     vlc_mutex_destroy(&sys->buffer_mutex);
339     vlc_cond_destroy(&sys->buffer_cond);
340     vlc_mutex_destroy(&sys->manage_mutex);
341
342     free(sys->pictures);
343     free(sys);
344
345     bcm_host_deinit();
346 }
347
348 static inline uint32_t align(uint32_t x, uint32_t y) {
349     uint32_t mod = x % y;
350     if (mod == 0)
351         return x;
352     else
353         return x + y - mod;
354 }
355
356 static int configure_display(vout_display_t *vd, const vout_display_cfg_t *cfg,
357                 const video_format_t *fmt)
358 {
359     vout_display_sys_t *sys = vd->sys;
360     vout_display_place_t place;
361     MMAL_DISPLAYREGION_T display_region;
362     MMAL_STATUS_T status;
363
364     if (!cfg && !fmt)
365         return -EINVAL;
366
367     if (fmt) {
368         sys->input->format->es->video.par.num = fmt->i_sar_num;
369         sys->input->format->es->video.par.den = fmt->i_sar_den;
370
371         status = mmal_port_format_commit(sys->input);
372         if (status != MMAL_SUCCESS) {
373             msg_Err(vd, "Failed to commit format for input port %s (status=%"PRIx32" %s)",
374                             sys->input->name, status, mmal_status_to_string(status));
375             return -EINVAL;
376         }
377     } else {
378         fmt = &vd->fmt;
379     }
380
381     if (!cfg)
382         cfg = vd->cfg;
383
384     vout_display_PlacePicture(&place, fmt, cfg, false);
385
386     display_region.hdr.id = MMAL_PARAMETER_DISPLAYREGION;
387     display_region.hdr.size = sizeof(MMAL_DISPLAYREGION_T);
388     display_region.fullscreen = MMAL_FALSE;
389     display_region.src_rect.x = fmt->i_x_offset;
390     display_region.src_rect.y = fmt->i_y_offset;
391     display_region.src_rect.width = fmt->i_visible_width;
392     display_region.src_rect.height = fmt->i_visible_height;
393     display_region.dest_rect.x = place.x;
394     display_region.dest_rect.y = place.y;
395     display_region.dest_rect.width = place.width;
396     display_region.dest_rect.height = place.height;
397     display_region.layer = sys->layer;
398     display_region.set = MMAL_DISPLAY_SET_FULLSCREEN | MMAL_DISPLAY_SET_SRC_RECT |
399             MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_LAYER;
400     status = mmal_port_parameter_set(sys->input, &display_region.hdr);
401     if (status != MMAL_SUCCESS) {
402         msg_Err(vd, "Failed to set display region (status=%"PRIx32" %s)",
403                         status, mmal_status_to_string(status));
404         return -EINVAL;
405     }
406
407     show_background(vd, cfg->is_fullscreen);
408     if (var_InheritBool(vd, MMAL_ADJUST_REFRESHRATE_NAME))
409         adjust_refresh_rate(vd);
410
411     if (fmt != &vd->fmt)
412         memcpy(&vd->fmt, fmt, sizeof(video_format_t));
413
414     return 0;
415 }
416
417 static picture_pool_t *vd_pool(vout_display_t *vd, unsigned count)
418 {
419     vout_display_sys_t *sys = vd->sys;
420     picture_resource_t picture_res;
421     picture_pool_configuration_t picture_pool_cfg;
422     video_format_t fmt = vd->fmt;
423     MMAL_STATUS_T status;
424     unsigned i;
425
426     if (sys->picture_pool) {
427         if (sys->num_buffers < count)
428             msg_Warn(vd, "Picture pool with %u pictures requested, but we already have one with %u pictures",
429                             count, sys->num_buffers);
430
431         goto out;
432     }
433
434     if (sys->opaque) {
435         if (count <= NUM_ACTUAL_OPAQUE_BUFFERS)
436             count = NUM_ACTUAL_OPAQUE_BUFFERS;
437     }
438
439     if (count < sys->input->buffer_num_recommended)
440         count = sys->input->buffer_num_recommended;
441
442 #ifndef NDEBUG
443     msg_Dbg(vd, "Creating picture pool with %u pictures", count);
444 #endif
445
446     sys->input->buffer_num = count;
447     status = mmal_port_enable(sys->input, input_port_cb);
448     if (status != MMAL_SUCCESS) {
449         msg_Err(vd, "Failed to enable input port %s (status=%"PRIx32" %s)",
450                         sys->input->name, status, mmal_status_to_string(status));
451         goto out;
452     }
453
454     status = mmal_component_enable(sys->component);
455     if (status != MMAL_SUCCESS) {
456         msg_Err(vd, "Failed to enable component %s (status=%"PRIx32" %s)",
457                         sys->component->name, status, mmal_status_to_string(status));
458         goto out;
459     }
460
461     sys->num_buffers = count;
462     sys->pool = mmal_pool_create_with_allocator(sys->num_buffers, sys->input->buffer_size,
463                     sys->input,
464                     (mmal_pool_allocator_alloc_t)mmal_port_payload_alloc,
465                     (mmal_pool_allocator_free_t)mmal_port_payload_free);
466     if (!sys->pool) {
467         msg_Err(vd, "Failed to create MMAL pool for %u buffers of size %"PRIu32,
468                         count, sys->input->buffer_size);
469         goto out;
470     }
471
472     memset(&picture_res, 0, sizeof(picture_resource_t));
473     sys->pictures = calloc(sys->num_buffers, sizeof(picture_t *));
474     for (i = 0; i < sys->num_buffers; ++i) {
475         picture_res.p_sys = malloc(sizeof(picture_sys_t));
476         picture_res.p_sys->vd = vd;
477         picture_res.p_sys->buffer = NULL;
478
479         sys->pictures[i] = picture_NewFromResource(&fmt, &picture_res);
480         if (!sys->pictures[i]) {
481             msg_Err(vd, "Failed to create picture");
482             free(picture_res.p_sys);
483             goto out;
484         }
485     }
486
487     memset(&picture_pool_cfg, 0, sizeof(picture_pool_configuration_t));
488     picture_pool_cfg.picture_count = sys->num_buffers;
489     picture_pool_cfg.picture = sys->pictures;
490     picture_pool_cfg.lock = picture_lock;
491     picture_pool_cfg.unlock = picture_unlock;
492
493     sys->picture_pool = picture_pool_NewExtended(&picture_pool_cfg);
494     if (!sys->picture_pool) {
495         msg_Err(vd, "Failed to create picture pool");
496         goto out;
497     }
498
499 out:
500     return sys->picture_pool;
501 }
502
503 static void vd_display(vout_display_t *vd, picture_t *picture,
504                 subpicture_t *subpicture)
505 {
506     vout_display_sys_t *sys = vd->sys;
507     picture_sys_t *pic_sys = picture->p_sys;
508     MMAL_BUFFER_HEADER_T *buffer = pic_sys->buffer;
509     MMAL_STATUS_T status;
510
511     if (!pic_sys->displayed || !sys->opaque) {
512         buffer->cmd = 0;
513         buffer->length = sys->input->buffer_size;
514
515         vlc_mutex_lock(&sys->buffer_mutex);
516         while (sys->buffers_in_transit >= MAX_BUFFERS_IN_TRANSIT)
517             vlc_cond_wait(&sys->buffer_cond, &sys->buffer_mutex);
518
519         status = mmal_port_send_buffer(sys->input, buffer);
520         if (status == MMAL_SUCCESS)
521             ++sys->buffers_in_transit;
522
523         vlc_mutex_unlock(&sys->buffer_mutex);
524         if (status != MMAL_SUCCESS) {
525             msg_Err(vd, "Failed to send buffer to input port. Frame dropped");
526             picture_Release(picture);
527         }
528
529         pic_sys->displayed = true;
530     } else {
531         picture_Release(picture);
532     }
533
534     display_subpicture(vd, subpicture);
535
536     if (subpicture)
537         subpicture_Delete(subpicture);
538 }
539
540 static int vd_control(vout_display_t *vd, int query, va_list args)
541 {
542     vout_display_sys_t *sys = vd->sys;
543     vout_display_cfg_t cfg;
544     const vout_display_cfg_t *tmp_cfg;
545     video_format_t fmt;
546     const video_format_t *tmp_fmt;
547     int ret = VLC_EGENERIC;
548
549     switch (query) {
550         case VOUT_DISPLAY_HIDE_MOUSE:
551         case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
552             ret = VLC_SUCCESS;
553             break;
554
555         case VOUT_DISPLAY_CHANGE_FULLSCREEN:
556             tmp_cfg = va_arg(args, const vout_display_cfg_t *);
557             vout_display_SendEventDisplaySize(vd, sys->display_width,
558                             sys->display_height, tmp_cfg->is_fullscreen);
559             ret = VLC_SUCCESS;
560             break;
561
562         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
563             tmp_cfg = va_arg(args, const vout_display_cfg_t *);
564             if (tmp_cfg->display.width == sys->display_width &&
565                             tmp_cfg->display.height == sys->display_height) {
566                 cfg = *vd->cfg;
567                 cfg.display.width = sys->display_width;
568                 cfg.display.height = sys->display_height;
569                 if (configure_display(vd, &cfg, NULL) >= 0)
570                     ret = VLC_SUCCESS;
571             }
572             break;
573
574         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
575             cfg = *vd->cfg;
576             tmp_cfg = va_arg(args, const vout_display_cfg_t *);
577             cfg.is_display_filled = tmp_cfg->is_display_filled;
578             if (configure_display(vd, &cfg, NULL) >= 0)
579                 ret = VLC_SUCCESS;
580             break;
581
582         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
583             fmt = vd->fmt;
584             tmp_fmt = va_arg(args, const video_format_t *);
585             fmt.i_sar_num = tmp_fmt->i_sar_num;
586             fmt.i_sar_den = tmp_fmt->i_sar_den;
587             if (configure_display(vd, NULL, &fmt) >= 0)
588                 ret = VLC_SUCCESS;
589             break;
590
591         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
592             fmt = vd->fmt;
593             tmp_fmt = va_arg(args, const video_format_t *);
594             fmt.i_x_offset = tmp_fmt->i_x_offset;
595             fmt.i_y_offset = tmp_fmt->i_y_offset;
596             fmt.i_visible_width = tmp_fmt->i_visible_width;
597             fmt.i_visible_height = tmp_fmt->i_visible_height;
598             if (configure_display(vd, NULL, &fmt) >= 0)
599                 ret = VLC_SUCCESS;
600             break;
601
602         case VOUT_DISPLAY_CHANGE_ZOOM:
603         case VOUT_DISPLAY_RESET_PICTURES:
604         case VOUT_DISPLAY_GET_OPENGL:
605             msg_Warn(vd, "Unsupported control query %d", query);
606             break;
607
608         default:
609             msg_Warn(vd, "Unknown control query %d", query);
610             break;
611     }
612
613     return ret;
614 }
615
616 static void vd_manage(vout_display_t *vd)
617 {
618     vout_display_sys_t *sys = vd->sys;
619     unsigned width, height;
620
621     vlc_mutex_lock(&sys->manage_mutex);
622
623     if (sys->need_configure_display) {
624         close_dmx(vd);
625         sys->dmx_handle = vc_dispmanx_display_open(0);
626
627         if (query_resolution(vd, &width, &height) >= 0) {
628             sys->display_width = width;
629             sys->display_height = height;
630             vout_display_SendEventDisplaySize(vd, width, height, vd->cfg->is_fullscreen);
631         }
632
633         sys->need_configure_display = false;
634     }
635
636     vlc_mutex_unlock(&sys->manage_mutex);
637 }
638
639 static int picture_lock(picture_t *picture)
640 {
641     vout_display_t *vd = picture->p_sys->vd;
642     vout_display_sys_t *sys = vd->sys;
643     picture_sys_t *pic_sys = picture->p_sys;
644
645     MMAL_BUFFER_HEADER_T *buffer = mmal_queue_wait(sys->pool->queue);
646     if (!buffer)
647         return VLC_EGENERIC;
648
649     vlc_mutex_lock(&sys->buffer_mutex);
650
651     mmal_buffer_header_reset(buffer);
652     buffer->user_data = picture;
653     pic_sys->buffer = buffer;
654
655     memcpy(picture->p, sys->planes, sizeof(sys->planes));
656     picture->p[0].p_pixels = buffer->data;
657     picture->p[1].p_pixels += (ptrdiff_t)buffer->data;
658     picture->p[2].p_pixels += (ptrdiff_t)buffer->data;
659
660     pic_sys->displayed = false;
661
662     vlc_mutex_unlock(&sys->buffer_mutex);
663
664     return VLC_SUCCESS;
665 }
666
667 static void picture_unlock(picture_t *picture)
668 {
669     picture_sys_t *pic_sys = picture->p_sys;
670     vout_display_t *vd = pic_sys->vd;
671     vout_display_sys_t *sys = vd->sys;
672     MMAL_BUFFER_HEADER_T *buffer = pic_sys->buffer;
673
674     vlc_mutex_lock(&sys->buffer_mutex);
675
676     pic_sys->buffer = NULL;
677     if (buffer) {
678         buffer->user_data = NULL;
679         mmal_buffer_header_release(buffer);
680     }
681
682     vlc_mutex_unlock(&sys->buffer_mutex);
683 }
684
685 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
686 {
687     vout_display_t *vd = (vout_display_t *)port->userdata;
688     MMAL_STATUS_T status;
689
690     if (buffer->cmd == MMAL_EVENT_ERROR) {
691         status = *(uint32_t *)buffer->data;
692         msg_Err(vd, "MMAL error %"PRIx32" \"%s\"", status, mmal_status_to_string(status));
693     }
694
695     mmal_buffer_header_release(buffer);
696 }
697
698 static void input_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
699 {
700     vout_display_t *vd = (vout_display_t *)port->userdata;
701     vout_display_sys_t *sys = vd->sys;
702     picture_t *picture = (picture_t *)buffer->user_data;
703
704     vlc_mutex_lock(&sys->buffer_mutex);
705     --sys->buffers_in_transit;
706     vlc_cond_signal(&sys->buffer_cond);
707     vlc_mutex_unlock(&sys->buffer_mutex);
708
709     if (picture)
710         picture_Release(picture);
711 }
712
713 static int query_resolution(vout_display_t *vd, unsigned *width, unsigned *height)
714 {
715     TV_DISPLAY_STATE_T display_state;
716     int ret = 0;
717
718     if (vc_tv_get_display_state(&display_state) == 0) {
719         if (display_state.state & 0xFF) {
720             *width = display_state.display.hdmi.width;
721             *height = display_state.display.hdmi.height;
722         } else if (display_state.state & 0xFF00) {
723             *width = display_state.display.sdtv.width;
724             *height = display_state.display.sdtv.height;
725         } else {
726             msg_Warn(vd, "Invalid display state %"PRIx32, display_state.state);
727             ret = -1;
728         }
729     } else {
730         msg_Warn(vd, "Failed to query display resolution");
731         ret = -1;
732     }
733
734     return ret;
735 }
736
737 static void tvservice_cb(void *callback_data, uint32_t reason, uint32_t param1, uint32_t param2)
738 {
739     VLC_UNUSED(reason);
740     VLC_UNUSED(param1);
741     VLC_UNUSED(param2);
742
743     vout_display_t *vd = (vout_display_t *)callback_data;
744     vout_display_sys_t *sys = vd->sys;
745
746     vlc_mutex_lock(&sys->manage_mutex);
747     sys->need_configure_display = true;
748     vlc_mutex_unlock(&sys->manage_mutex);
749 }
750
751 static void adjust_refresh_rate(vout_display_t *vd)
752 {
753     TV_DISPLAY_STATE_T display_state;
754     TV_SUPPORTED_MODE_NEW_T supported_modes[VC_TV_MAX_MODE_IDS];
755     int num_modes;
756     double frame_rate = (double)vd->fmt.i_frame_rate / vd->fmt.i_frame_rate_base;
757     int best_id = -1;
758     double best_score, score;
759     int i;
760
761     vc_tv_get_display_state(&display_state);
762     if(display_state.display.hdmi.mode != HDMI_MODE_OFF) {
763         num_modes = vc_tv_hdmi_get_supported_modes_new(display_state.display.hdmi.group,
764                         supported_modes, VC_TV_MAX_MODE_IDS, NULL, NULL);
765
766         for(i = 0; i < num_modes; ++i) {
767             if(supported_modes[i].width != display_state.display.hdmi.width ||
768                             supported_modes[i].height != display_state.display.hdmi.height ||
769                             supported_modes[i].scan_mode != display_state.display.hdmi.scan_mode)
770                 continue;
771
772             score = fmod(supported_modes[i].frame_rate, frame_rate);
773             if((best_id < 0) || (score < best_score)) {
774                 best_id = i;
775                 best_score = score;
776             }
777         }
778
779         if((best_id >= 0) && (display_state.display.hdmi.frame_rate != supported_modes[best_id].frame_rate)) {
780             msg_Info(vd, "Setting HDMI refresh rate to %"PRIu32,
781                             supported_modes[best_id].frame_rate);
782             vc_tv_hdmi_power_on_explicit_new(HDMI_MODE_HDMI,
783                             supported_modes[best_id].group,
784                             supported_modes[best_id].code);
785         }
786     }
787 }
788
789 static void display_subpicture(vout_display_t *vd, subpicture_t *subpicture)
790 {
791     vout_display_sys_t *sys = vd->sys;
792     struct dmx_region_t **dmx_region = &sys->dmx_region;
793     struct dmx_region_t *unused_dmx_region;
794     DISPMANX_UPDATE_HANDLE_T update = 0;
795     picture_t *picture;
796     video_format_t *fmt;
797     struct dmx_region_t *dmx_region_next;
798
799     if(subpicture) {
800         subpicture_region_t *region = subpicture->p_region;
801         while(region) {
802             picture = region->p_picture;
803             fmt = &region->fmt;
804
805             if(!*dmx_region) {
806                 if(!update)
807                     update = vc_dispmanx_update_start(10);
808                 *dmx_region = dmx_region_new(vd, update, region);
809             } else if(((*dmx_region)->bmp_rect.width != (int32_t)fmt->i_visible_width) ||
810                     ((*dmx_region)->bmp_rect.height != (int32_t)fmt->i_visible_height) ||
811                     ((*dmx_region)->pos_x != region->i_x) ||
812                     ((*dmx_region)->pos_y != region->i_y) ||
813                     ((*dmx_region)->alpha.opacity != (uint32_t)region->i_alpha)) {
814                 dmx_region_next = (*dmx_region)->next;
815                 if(!update)
816                     update = vc_dispmanx_update_start(10);
817                 dmx_region_delete(*dmx_region, update);
818                 *dmx_region = dmx_region_new(vd, update, region);
819                 (*dmx_region)->next = dmx_region_next;
820             } else if((*dmx_region)->picture != picture) {
821                 if(!update)
822                     update = vc_dispmanx_update_start(10);
823                 dmx_region_update(*dmx_region, update, picture);
824             }
825
826             dmx_region = &(*dmx_region)->next;
827             region = region->p_next;
828         }
829     }
830
831     /* Remove remaining regions */
832     unused_dmx_region = *dmx_region;
833     while(unused_dmx_region) {
834         dmx_region_next = unused_dmx_region->next;
835         if(!update)
836             update = vc_dispmanx_update_start(10);
837         dmx_region_delete(unused_dmx_region, update);
838         unused_dmx_region = dmx_region_next;
839     }
840     *dmx_region = NULL;
841
842     if(update)
843         vc_dispmanx_update_submit_sync(update);
844 }
845
846 static void close_dmx(vout_display_t *vd)
847 {
848     vout_display_sys_t *sys = vd->sys;
849     DISPMANX_UPDATE_HANDLE_T update = vc_dispmanx_update_start(10);
850     struct dmx_region_t *dmx_region = sys->dmx_region;
851     struct dmx_region_t *dmx_region_next;
852
853     while(dmx_region) {
854         dmx_region_next = dmx_region->next;
855         dmx_region_delete(dmx_region, update);
856         dmx_region = dmx_region_next;
857     }
858
859     vc_dispmanx_update_submit_sync(update);
860     sys->dmx_region = NULL;
861
862     show_background(vd, false);
863
864     vc_dispmanx_display_close(sys->dmx_handle);
865     sys->dmx_handle = DISPMANX_NO_HANDLE;
866 }
867
868 static struct dmx_region_t *dmx_region_new(vout_display_t *vd,
869                 DISPMANX_UPDATE_HANDLE_T update, subpicture_region_t *region)
870 {
871     vout_display_sys_t *sys = vd->sys;
872     video_format_t *fmt = &region->fmt;
873     struct dmx_region_t *dmx_region = malloc(sizeof(struct dmx_region_t));
874     uint32_t image_handle;
875
876     dmx_region->pos_x = region->i_x;
877     dmx_region->pos_y = region->i_y;
878
879     vc_dispmanx_rect_set(&dmx_region->bmp_rect, 0, 0, fmt->i_visible_width,
880                     fmt->i_visible_height);
881     vc_dispmanx_rect_set(&dmx_region->src_rect, 0, 0, fmt->i_visible_width << 16,
882                     fmt->i_visible_height << 16);
883     vc_dispmanx_rect_set(&dmx_region->dst_rect, region->i_x, region->i_y,
884                     fmt->i_visible_width, fmt->i_visible_height);
885
886     dmx_region->resource = vc_dispmanx_resource_create(VC_IMAGE_RGBA32,
887                     dmx_region->bmp_rect.width | (region->p_picture->p[0].i_pitch << 16),
888                     dmx_region->bmp_rect.height | (dmx_region->bmp_rect.height << 16),
889                     &image_handle);
890     vc_dispmanx_resource_write_data(dmx_region->resource, VC_IMAGE_RGBA32,
891                     region->p_picture->p[0].i_pitch,
892                     region->p_picture->p[0].p_pixels, &dmx_region->bmp_rect);
893
894     dmx_region->alpha.flags = DISPMANX_FLAGS_ALPHA_FROM_SOURCE | DISPMANX_FLAGS_ALPHA_MIX;
895     dmx_region->alpha.opacity = region->i_alpha;
896     dmx_region->alpha.mask = DISPMANX_NO_HANDLE;
897     dmx_region->element = vc_dispmanx_element_add(update, sys->dmx_handle,
898                     sys->layer + 1, &dmx_region->dst_rect, dmx_region->resource,
899                     &dmx_region->src_rect, DISPMANX_PROTECTION_NONE,
900                     &dmx_region->alpha, NULL, VC_IMAGE_ROT0);
901
902     dmx_region->next = NULL;
903     dmx_region->picture = region->p_picture;
904
905     return dmx_region;
906 }
907
908 static void dmx_region_update(struct dmx_region_t *dmx_region,
909                 DISPMANX_UPDATE_HANDLE_T update, picture_t *picture)
910 {
911     vc_dispmanx_resource_write_data(dmx_region->resource, VC_IMAGE_RGBA32,
912                     picture->p[0].i_pitch, picture->p[0].p_pixels, &dmx_region->bmp_rect);
913     vc_dispmanx_element_change_source(update, dmx_region->element, dmx_region->resource);
914     dmx_region->picture = picture;
915 }
916
917 static void dmx_region_delete(struct dmx_region_t *dmx_region,
918                 DISPMANX_UPDATE_HANDLE_T update)
919 {
920     vc_dispmanx_element_remove(update, dmx_region->element);
921     vc_dispmanx_resource_delete(dmx_region->resource);
922     free(dmx_region);
923 }
924
925 static void show_background(vout_display_t *vd, bool enable)
926 {
927     vout_display_sys_t *sys = vd->sys;
928     uint32_t image_ptr, color = 0xFF000000;
929     VC_RECT_T dst_rect, src_rect;
930     DISPMANX_UPDATE_HANDLE_T update;
931
932     if (enable && !sys->bkg_element) {
933         sys->bkg_resource = vc_dispmanx_resource_create(VC_IMAGE_RGBA32, 1, 1,
934                         &image_ptr);
935         vc_dispmanx_rect_set(&dst_rect, 0, 0, 1, 1);
936         vc_dispmanx_resource_write_data(sys->bkg_resource, VC_IMAGE_RGBA32,
937                         sizeof(color), &color, &dst_rect);
938         vc_dispmanx_rect_set(&src_rect, 0, 0, 1 << 16, 1 << 16);
939         vc_dispmanx_rect_set(&dst_rect, 0, 0, 0, 0);
940         update = vc_dispmanx_update_start(0);
941         sys->bkg_element = vc_dispmanx_element_add(update, sys->dmx_handle,
942                         sys->layer - 1, &dst_rect, sys->bkg_resource, &src_rect,
943                         DISPMANX_PROTECTION_NONE, NULL, NULL, VC_IMAGE_ROT0);
944         vc_dispmanx_update_submit_sync(update);
945     } else if (!enable && sys->bkg_element) {
946         update = vc_dispmanx_update_start(0);
947         vc_dispmanx_element_remove(update, sys->bkg_element);
948         vc_dispmanx_resource_delete(sys->bkg_resource);
949         vc_dispmanx_update_submit_sync(update);
950         sys->bkg_element = DISPMANX_NO_HANDLE;
951         sys->bkg_resource = DISPMANX_NO_HANDLE;
952     }
953 }