vdpau/chroma: fix leak on error
[vlc.git] / modules / hw / vdpau / chroma.c
1 /*****************************************************************************
2  * chroma.c: VLC picture import into VDPAU
3  *****************************************************************************
4  * Copyright (C) 2013 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <inttypes.h>
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_filter.h>
32 #include "vlc_vdpau.h"
33
34 /* Picture history as recommended by VDPAU documentation */
35 #define MAX_PAST   2
36 #define MAX_FUTURE 1
37
38 struct filter_sys_t
39 {
40     vdp_t *vdp;
41     VdpDevice device;
42     VdpVideoMixer mixer;
43     VdpChromaType chroma;
44     VdpYCbCrFormat format;
45     picture_t *(*import)(filter_t *, picture_t *);
46
47     struct
48     {
49         vlc_vdp_video_field_t *field;
50         mtime_t date;
51         bool force;
52     } history[MAX_PAST + 1 + MAX_FUTURE];
53
54     struct
55     {
56         float brightness;
57         float contrast;
58         float saturation;
59         float hue;
60     } procamp;
61 };
62
63 /** Initialize the colour space conversion matrix */
64 static VdpStatus MixerSetupColors(filter_t *filter, const VdpProcamp *procamp,
65                                   VdpCSCMatrix *restrict csc)
66 {
67     filter_sys_t *sys = filter->p_sys;
68     VdpStatus err;
69     VdpColorStandard std = (filter->fmt_in.video.i_height > 576)
70                          ? VDP_COLOR_STANDARD_ITUR_BT_709
71                          : VDP_COLOR_STANDARD_ITUR_BT_601;
72
73     err = vdp_generate_csc_matrix(sys->vdp, procamp, std, csc);
74     if (err != VDP_STATUS_OK)
75     {
76         msg_Err(filter, "video %s failure: %s", "color space matrix",
77                 vdp_get_error_string(sys->vdp, err));
78         return err;
79     }
80
81     if (procamp != NULL)
82     {
83         sys->procamp.brightness = procamp->brightness;
84         sys->procamp.contrast = procamp->contrast;
85         sys->procamp.saturation = procamp->saturation;
86         sys->procamp.hue = procamp->hue;
87     }
88     else
89     {
90         sys->procamp.brightness = 0.f;
91         sys->procamp.contrast = 1.f;
92         sys->procamp.saturation = 1.f;
93         sys->procamp.hue = 0.f;
94     }
95     return VDP_STATUS_OK;
96 }
97
98 /** Create VDPAU video mixer */
99 static VdpVideoMixer MixerCreate(filter_t *filter)
100 {
101     filter_sys_t *sys = filter->p_sys;
102     VdpVideoMixer mixer;
103     VdpStatus err;
104     VdpBool ok;
105
106     /* Check for potentially useful features */
107     VdpVideoMixerFeature featv[5];
108     unsigned featc = 0;
109
110     int algo = var_InheritInteger(filter, "vdpau-deinterlace");
111     bool ivtc = false;
112     if (algo == VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL)
113     {
114         err = vdp_video_mixer_query_feature_support(sys->vdp, sys->device,
115                                                     algo, &ok);
116         if (err == VDP_STATUS_OK && ok == VDP_TRUE)
117             msg_Dbg(filter, "using video mixer %s feature",
118                     "temporal-spatial deinterlace");
119         else
120             algo = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL; /* fallback */
121     }
122     if (algo == VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL)
123     {
124         err = vdp_video_mixer_query_feature_support(sys->vdp, sys->device,
125                                                     algo, &ok);
126         if (err == VDP_STATUS_OK && ok == VDP_TRUE)
127             msg_Dbg(filter, "using video mixer %s feature",
128                     "temporal deinterlace");
129         else
130             algo = -1;
131     }
132     if (algo >= 0)
133     {
134         featv[featc++] = algo;
135         ivtc = var_InheritBool(filter, "vdpau-ivtc");
136         if (ivtc)
137         {
138             err = vdp_video_mixer_query_feature_support(sys->vdp, sys->device,
139                                 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE, &ok);
140             if (err == VDP_STATUS_OK && ok == VDP_TRUE)
141                 msg_Dbg(filter, "using video mixer %s feature",
142                         "inverse telecine");
143             featv[featc++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
144         }
145     }
146
147     const float noise = var_InheritFloat(filter, "vdpau-noise-reduction");
148     if (noise > 0.f)
149     {
150         err = vdp_video_mixer_query_feature_support(sys->vdp, sys->device,
151                                  VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION, &ok);
152         if (err == VDP_STATUS_OK && ok == VDP_TRUE)
153         {
154             msg_Dbg(filter, "using video mixer %s feature", "noise reduction");
155             featv[featc++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
156         }
157     }
158
159     err = vdp_video_mixer_query_feature_support(sys->vdp, sys->device,
160                                        VDP_VIDEO_MIXER_FEATURE_SHARPNESS, &ok);
161     if (err == VDP_STATUS_OK && ok == VDP_TRUE)
162     {
163         msg_Dbg(filter, "using video mixer %s feature", "sharpness");
164         featv[featc++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
165     }
166
167     const int offset = VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 - 1;
168     unsigned level = var_InheritInteger(filter, "vdpau-scaling");
169     while (level > 0)
170     {
171
172         err = vdp_video_mixer_query_feature_support(sys->vdp, sys->device,
173                                                     offset + level, &ok);
174         if (err == VDP_STATUS_OK && ok == VDP_TRUE)
175         {
176             msg_Dbg(filter, "using video mixer high quality scaling L%u",
177                     level);
178             featv[featc++] = offset + level;
179             break;
180         }
181         level--; /* fallback to lower quality */
182     }
183
184     /* Create the mixer */
185     VdpVideoMixerParameter parms[3] = {
186         VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
187         VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
188         VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE,
189     };
190     uint32_t width = filter->fmt_in.video.i_width;
191     uint32_t height = filter->fmt_in.video.i_height;
192     const void *values[3] = { &width, &height, &sys->chroma, };
193
194     err = vdp_video_mixer_create(sys->vdp, sys->device, featc, featv,
195                                  3, parms, values, &mixer);
196     if (err != VDP_STATUS_OK)
197     {
198         msg_Err(filter, "video %s %s failure: %s", "mixer", "creation",
199                 vdp_get_error_string(sys->vdp, err));
200         return VDP_INVALID_HANDLE;
201     }
202
203     msg_Dbg(filter, "using video mixer %"PRIu32, mixer);
204
205     /* Set initial features and attributes */
206     VdpVideoMixerAttribute attrv[3];
207     const void *valv[3];
208     unsigned attrc = 0;
209     VdpCSCMatrix csc;
210     uint8_t chroma_skip;
211
212     featc = 0;
213
214     if (MixerSetupColors(filter, NULL, &csc) == VDP_STATUS_OK)
215     {
216         attrv[attrc] = VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX;
217         valv[attrc] = &csc;
218         attrc++;
219     }
220
221     if (algo >= 0)
222     {
223         featv[featc++] = algo;
224         if (ivtc)
225             featv[featc++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
226
227         chroma_skip = var_InheritBool(filter, "vdpau-chroma-skip");
228         attrv[attrc] = VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE;
229         valv[attrc] = &chroma_skip;
230         attrc++;
231     }
232
233     if (noise > 0.f)
234     {
235         featv[featc++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
236
237         attrv[attrc] = VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL;
238         valv[attrc] = &noise;
239         attrc++;
240     }
241
242     if (level > 0)
243         featv[featc++] = offset + level;
244
245     if (featc > 0)
246     {
247         VdpBool enablev[featc];
248
249         for (unsigned i = 0; i < featc; i++)
250             enablev[i] = VDP_TRUE;
251
252         err = vdp_video_mixer_set_feature_enables(sys->vdp, mixer,
253                                                   featc, featv, enablev);
254         if (err != VDP_STATUS_OK)
255             msg_Err(filter, "video %s %s failure: %s", "mixer", "features",
256                     vdp_get_error_string(sys->vdp, err));
257     }
258
259     if (attrc > 0)
260     {
261         err = vdp_video_mixer_set_attribute_values(sys->vdp, mixer,
262                                                    attrc, attrv, valv);
263         if (err != VDP_STATUS_OK)
264             msg_Err(filter, "video %s %s failure: %s", "mixer", "attributes",
265                     vdp_get_error_string(sys->vdp, err));
266     }
267
268     return mixer;
269 }
270
271 static void Flush(filter_t *filter)
272 {
273     filter_sys_t *sys = filter->p_sys;
274
275     for (unsigned i = 0; i < MAX_PAST + MAX_FUTURE; i++)
276         if (sys->history[i].field != NULL)
277         {
278             sys->history[i].field->destroy(sys->history[i].field);
279             sys->history[i].field = NULL;
280         }
281 }
282
283 /** Get a VLC picture for a VDPAU output surface */
284 static picture_t *OutputAllocate(filter_t *filter)
285 {
286     filter_sys_t *sys = filter->p_sys;
287
288     picture_t *pic = filter_NewPicture(filter);
289     if (pic == NULL)
290         return NULL;
291
292     picture_sys_t *psys = pic->p_sys;
293     assert(psys->vdp != NULL);
294
295     if (likely(sys->mixer != VDP_INVALID_HANDLE))
296     {   /* Not the first output picture */
297         assert(psys->vdp == sys->vdp);
298         return pic;
299     }
300
301     /* First picture: get the context and allocate the mixer */
302     sys->vdp = vdp_hold_x11(psys->vdp, NULL);
303     sys->device = psys->device;
304     sys->mixer = MixerCreate(filter);
305     if (sys->mixer != VDP_INVALID_HANDLE)
306         return pic;
307
308     vdp_release_x11(psys->vdp);
309     psys->vdp = NULL;
310     picture_Release(pic);
311     return NULL;
312 }
313
314 /** Export a VDPAU video surface picture to a normal VLC picture */
315 static picture_t *VideoExport(filter_t *filter, picture_t *src, picture_t *dst)
316 {
317     filter_sys_t *sys = filter->p_sys;
318     vlc_vdp_video_field_t *field = src->context;
319     vlc_vdp_video_frame_t *psys = field->frame;
320     VdpStatus err;
321     VdpVideoSurface surface = psys->surface;
322     void *planes[3];
323     uint32_t pitches[3];
324
325     picture_CopyProperties(dst, src);
326
327     for (int i = 0; i < dst->i_planes; i++)
328     {
329         planes[i] = dst->p[i].p_pixels;
330         pitches[i] = dst->p[i].i_pitch;
331     }
332     if (dst->format.i_chroma == VLC_CODEC_I420
333      || dst->format.i_chroma == VLC_CODEC_I422)
334     {
335         planes[1] = dst->p[2].p_pixels;
336         planes[2] = dst->p[1].p_pixels;
337         pitches[1] = dst->p[2].i_pitch;
338         pitches[2] = dst->p[1].i_pitch;
339     }
340     err = vdp_video_surface_get_bits_y_cb_cr(psys->vdp, surface, sys->format,
341                                              planes, pitches);
342     if (err != VDP_STATUS_OK)
343     {
344         msg_Err(filter, "video %s %s failure: %s", "surface", "export",
345                 vdp_get_error_string(psys->vdp, err));
346         picture_Release(dst);
347         dst = NULL;
348     }
349     picture_Release(src);
350     return dst;
351 }
352
353 /** Import VLC picture into VDPAU video surface */
354 static picture_t *VideoImport(filter_t *filter, picture_t *src)
355 {
356     filter_sys_t *sys = filter->p_sys;
357     VdpVideoSurface surface;
358     VdpStatus err;
359
360     if (sys->vdp == NULL)
361         goto drop;
362
363     /* Create surface (TODO: reuse?) */
364     err = vdp_video_surface_create(sys->vdp, sys->device, sys->chroma,
365                                    filter->fmt_in.video.i_width,
366                                    filter->fmt_in.video.i_height, &surface);
367     if (err != VDP_STATUS_OK)
368     {
369         msg_Err(filter, "video %s %s failure: %s", "surface", "creation",
370                 vdp_get_error_string(sys->vdp, err));
371         goto drop;
372     }
373
374     /* Put bits */
375     const void *planes[3];
376     uint32_t pitches[3];
377     for (int i = 0; i < src->i_planes; i++)
378     {
379         planes[i] = src->p[i].p_pixels;
380         pitches[i] = src->p[i].i_pitch;
381     }
382     if (src->format.i_chroma == VLC_CODEC_I420)
383     {
384         planes[1] = src->p[2].p_pixels;
385         planes[2] = src->p[1].p_pixels;
386         pitches[1] = src->p[2].i_pitch;
387         pitches[2] = src->p[1].i_pitch;
388     }
389     err = vdp_video_surface_put_bits_y_cb_cr(sys->vdp, surface, sys->format,
390                                              planes, pitches);
391     if (err != VDP_STATUS_OK)
392     {
393         msg_Err(filter, "video %s %s failure: %s", "surface", "import",
394                 vdp_get_error_string(sys->vdp, err));
395         goto error;
396     }
397
398     /* Wrap surface into a picture */
399     video_format_t fmt = src->format;
400     fmt.i_chroma = (sys->chroma == VDP_CHROMA_TYPE_420)
401         ? VLC_CODEC_VDPAU_VIDEO_420 : VLC_CODEC_VDPAU_VIDEO_422;
402
403     picture_t *dst = picture_NewFromFormat(&fmt);
404     if (unlikely(dst == NULL))
405         goto error;
406     picture_CopyProperties(dst, src);
407     picture_Release(src);
408
409     err = vlc_vdp_video_attach(sys->vdp, surface, dst);
410     if (unlikely(err != VDP_STATUS_OK))
411     {
412         picture_Release(dst);
413         dst = NULL;
414     }
415     return dst;
416 error:
417     vdp_video_surface_destroy(sys->vdp, surface);
418 drop:
419     picture_Release(src);
420     return NULL;
421 }
422
423 static picture_t *VideoPassthrough(filter_t *filter, picture_t *src)
424 {
425     filter_sys_t *sys = filter->p_sys;
426     vlc_vdp_video_field_t *field = src->context;
427
428     if (unlikely(field == NULL))
429     {
430         msg_Err(filter, "corrupt VDPAU video surface");
431         goto error;
432     }
433
434     vlc_vdp_video_frame_t *psys = field->frame;
435
436     /* Corner case: different VDPAU instances decoding and rendering */
437     if (psys->vdp != sys->vdp)
438     {
439         video_format_t fmt = src->format;
440         switch (sys->chroma)
441         {
442              case VDP_CHROMA_TYPE_420: fmt.i_chroma = VLC_CODEC_NV12; break;
443              case VDP_CHROMA_TYPE_422: fmt.i_chroma = VLC_CODEC_UYVY; break;
444              case VDP_CHROMA_TYPE_444: fmt.i_chroma = VLC_CODEC_NV24; break;
445              default: assert(0);
446         }
447
448         picture_t *pic = picture_NewFromFormat(&fmt);
449         if (unlikely(pic == NULL))
450             goto error;
451
452         pic = VideoExport(filter, src, pic);
453         if (pic == NULL)
454             return NULL;
455
456         src = VideoImport(filter, pic);
457     }
458     return src;
459 error:
460     picture_Release(src);
461     return NULL;
462 }
463
464 static inline VdpVideoSurface picture_GetVideoSurface(const picture_t *pic)
465 {
466     vlc_vdp_video_field_t *field = pic->context;
467     return field->frame->surface;
468 }
469
470 static picture_t *MixerRender(filter_t *filter, picture_t *src)
471 {
472     filter_sys_t *sys = filter->p_sys;
473     VdpStatus err;
474
475     picture_t *dst = OutputAllocate(filter);
476
477     src = sys->import(filter, src);
478     /* Update history and take "present" picture field */
479     if (likely(src != NULL))
480     {
481         sys->history[MAX_PAST + MAX_FUTURE].field =
482                                               vlc_vdp_video_copy(src->context);
483         sys->history[MAX_PAST + MAX_FUTURE].date = src->date;
484         sys->history[MAX_PAST + MAX_FUTURE].force = src->b_force;
485         picture_Release(src);
486     }
487     else
488         sys->history[MAX_PAST + MAX_FUTURE].field = NULL;
489
490     if (dst == NULL)
491         goto skip;
492
493     vlc_vdp_video_field_t *f = sys->history[MAX_PAST].field;
494     if (f == NULL)
495     {
496         picture_Release(dst);
497         dst = NULL;
498         goto skip;
499     }
500
501     dst->date = sys->history[MAX_PAST].date;
502     dst->b_force = sys->history[MAX_PAST].force;
503
504     /* Enable/Disable features */
505     const VdpVideoMixerFeature features[] = {
506         VDP_VIDEO_MIXER_FEATURE_SHARPNESS,
507     };
508     const VdpBool enables[] = {
509         f->sharpen != 0.f,
510     };
511
512     err = vdp_video_mixer_set_feature_enables(sys->vdp, sys->mixer,
513                   sizeof (features) / sizeof (features[0]), features, enables);
514     if (err != VDP_STATUS_OK)
515         msg_Err(filter, "video %s %s failure: %s", "mixer", "features",
516                 vdp_get_error_string(sys->vdp, err));
517
518     /* Configure mixer depending on upstream video filters */
519     VdpVideoMixerAttribute attrs[2] = {
520         VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL,
521     };
522     const void *values[2] = {
523         &f->sharpen,
524     };
525     unsigned count = 1;
526     VdpCSCMatrix csc;
527
528     if ((sys->procamp.brightness != f->procamp.brightness
529       || sys->procamp.contrast != f->procamp.contrast
530       || sys->procamp.saturation != f->procamp.saturation
531       || sys->procamp.hue != f->procamp.hue)
532      && (MixerSetupColors(filter, &f->procamp, &csc) == VDP_STATUS_OK))
533     {
534         attrs[count] = VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX;
535         values[count] = &csc;
536         count++;
537     }
538
539     err = vdp_video_mixer_set_attribute_values(sys->vdp, sys->mixer,
540                                                count, attrs, values);
541     if (err != VDP_STATUS_OK)
542         msg_Err(filter, "video %s %s failure: %s", "mixer", "attributes",
543                 vdp_get_error_string(sys->vdp, err));
544
545     /* Render video into output */
546     VdpVideoMixerPictureStructure structure = f->structure;
547     VdpVideoSurface past[MAX_PAST];
548     VdpVideoSurface surface = f->frame->surface;
549     VdpVideoSurface future[MAX_FUTURE];
550     VdpRect src_rect = {
551         filter->fmt_in.video.i_x_offset, filter->fmt_in.video.i_y_offset,
552         filter->fmt_in.video.i_visible_width + filter->fmt_in.video.i_x_offset,
553         filter->fmt_in.video.i_visible_height + filter->fmt_in.video.i_y_offset
554     };
555     VdpOutputSurface output = dst->p_sys->surface;
556     VdpRect dst_rect = {
557         0, 0,
558         filter->fmt_out.video.i_visible_width,
559         filter->fmt_out.video.i_visible_height
560     };
561
562     for (unsigned i = 0; i < MAX_PAST; i++)
563     {
564         f = sys->history[(MAX_PAST - 1) - i].field;
565         past[i] = (f != NULL) ? f->frame->surface : VDP_INVALID_HANDLE;
566     }
567     for (unsigned i = 0; i < MAX_FUTURE; i++)
568     {
569         f = sys->history[(MAX_PAST + 1) + i].field;
570         future[i] = (f != NULL) ? f->frame->surface : VDP_INVALID_HANDLE;
571     }
572
573     err = vdp_video_mixer_render(sys->vdp, sys->mixer, VDP_INVALID_HANDLE,
574                                  NULL, structure,
575                                  MAX_PAST, past, surface, MAX_FUTURE, future,
576                                  &src_rect, output, &dst_rect, NULL, 0, NULL);
577     if (err != VDP_STATUS_OK)
578     {
579         msg_Err(filter, "video %s %s failure: %s", "mixer", "rendering",
580                 vdp_get_error_string(sys->vdp, err));
581         picture_Release(dst);
582         dst = NULL;
583     }
584
585 skip:
586     f = sys->history[0].field;
587     if (f != NULL)
588         f->destroy(f); /* Release oldest field */
589     memmove(sys->history, sys->history + 1, /* Advance history */
590             sizeof (sys->history[0]) * (MAX_PAST + MAX_FUTURE));
591
592     return dst;
593 }
594
595 static int OutputOpen(vlc_object_t *obj)
596 {
597     filter_t *filter = (filter_t *)obj;
598     if (filter->fmt_out.video.i_chroma != VLC_CODEC_VDPAU_OUTPUT)
599         return VLC_EGENERIC;
600
601     filter_sys_t *sys = malloc(sizeof (*sys));
602     if (unlikely(sys == NULL))
603         return VLC_ENOMEM;
604
605     sys->vdp = NULL;
606     sys->mixer = VDP_INVALID_HANDLE;
607
608     if (filter->fmt_in.video.i_chroma == VLC_CODEC_VDPAU_VIDEO_444)
609     {
610         sys->chroma = VDP_CHROMA_TYPE_444;
611         sys->format = VDP_YCBCR_FORMAT_NV12;
612         sys->import = VideoPassthrough;
613     }
614     else
615     if (filter->fmt_in.video.i_chroma == VLC_CODEC_VDPAU_VIDEO_422)
616     {
617         sys->chroma = VDP_CHROMA_TYPE_422;
618         /* TODO: check if the drivery supports NV12 or UYVY */
619         sys->format = VDP_YCBCR_FORMAT_UYVY;
620         sys->import = VideoPassthrough;
621     }
622     else
623     if (filter->fmt_in.video.i_chroma == VLC_CODEC_VDPAU_VIDEO_420)
624     {
625         sys->chroma = VDP_CHROMA_TYPE_420;
626         sys->format = VDP_YCBCR_FORMAT_NV12;
627         sys->import = VideoPassthrough;
628     }
629     else
630     if (vlc_fourcc_to_vdp_ycc(filter->fmt_in.video.i_chroma,
631                               &sys->chroma, &sys->format))
632         sys->import = VideoImport;
633     else
634     {
635         free(sys);
636         return VLC_EGENERIC;
637     }
638
639     /* NOTE: The video mixer capabilities should be checked here, and the
640      * then video mixer set up. But:
641      * 1) The VDPAU back-end is accessible only once the first picture
642      *    gets filtered. Thus the video mixer is created later.
643      * 2) Bailing out due to insufficient capabilities would break the
644      *    video pipeline. Thus capabilities should be checked earlier. */
645
646     for (unsigned i = 0; i < MAX_PAST + MAX_FUTURE; i++)
647         sys->history[i].field = NULL;
648
649     sys->procamp.brightness = 0.f;
650     sys->procamp.contrast = 1.f;
651     sys->procamp.saturation = 1.f;
652     sys->procamp.hue = 0.f;
653
654     filter->pf_video_filter = MixerRender;
655     filter->pf_video_flush = Flush;
656     filter->p_sys = sys;
657     return VLC_SUCCESS;
658 }
659
660 static void OutputClose(vlc_object_t *obj)
661 {
662     filter_t *filter = (filter_t *)obj;
663     filter_sys_t *sys = filter->p_sys;
664
665     Flush(filter);
666     if (sys->mixer != VDP_INVALID_HANDLE)
667     {
668         vdp_video_mixer_destroy(sys->vdp, sys->mixer);
669         vdp_release_x11(sys->vdp);
670     }
671     free(sys);
672 }
673
674 static picture_t *VideoExport_Filter(filter_t *filter, picture_t *src)
675 {
676     if (unlikely(src->context == NULL))
677     {
678         msg_Err(filter, "corrupt VDPAU video surface %p", src);
679         picture_Release(src);
680         return NULL;
681     }
682
683     picture_t *dst = filter_NewPicture(filter);
684     if (dst == NULL)
685         return NULL;
686
687     return VideoExport(filter, src, dst);
688 }
689
690 static int YCbCrOpen(vlc_object_t *obj)
691 {
692     filter_t *filter = (filter_t *)obj;
693     if (filter->fmt_in.video.i_chroma != VLC_CODEC_VDPAU_VIDEO_420
694      && filter->fmt_in.video.i_chroma != VLC_CODEC_VDPAU_VIDEO_422
695      && filter->fmt_in.video.i_chroma != VLC_CODEC_VDPAU_VIDEO_444)
696         return VLC_EGENERIC;
697
698     if (filter->fmt_in.video.i_visible_width
699                                        != filter->fmt_out.video.i_visible_width
700      || filter->fmt_in.video.i_visible_height
701                                       != filter->fmt_out.video.i_visible_height
702      || filter->fmt_in.video.i_x_offset != filter->fmt_out.video.i_x_offset
703      || filter->fmt_in.video.i_y_offset != filter->fmt_out.video.i_y_offset
704      || (filter->fmt_in.video.i_sar_num * filter->fmt_out.video.i_sar_den
705           != filter->fmt_in.video.i_sar_den * filter->fmt_out.video.i_sar_num))
706         return VLC_EGENERIC;
707
708     filter_sys_t *sys = malloc(sizeof (*sys));
709     if (unlikely(sys == NULL))
710         return VLC_ENOMEM;
711
712     if (!vlc_fourcc_to_vdp_ycc(filter->fmt_out.video.i_chroma,
713                                &sys->chroma, &sys->format))
714     {
715         free(sys);
716         return VLC_EGENERIC;
717     }
718
719     filter->pf_video_filter = VideoExport_Filter;
720     filter->p_sys = sys;
721     return VLC_SUCCESS;
722 }
723
724 static void YCbCrClose(vlc_object_t *obj)
725 {
726     filter_t *filter = (filter_t *)obj;
727     filter_sys_t *sys = filter->p_sys;
728
729     free(sys);
730 }
731
732 static const int algo_values[] = {
733     -1,
734     VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
735     VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
736 };
737
738 static const char *const algo_names[] = {
739     N_("Bob"), N_("Temporal"), N_("Temporal-spatial"),
740 };
741
742 vlc_module_begin()
743     set_shortname(N_("VDPAU"))
744     set_description(N_("VDPAU surface conversions"))
745     set_capability("video filter2", 10)
746     set_category(CAT_VIDEO)
747     set_subcategory(SUBCAT_VIDEO_VFILTER)
748     set_callbacks(OutputOpen, OutputClose)
749
750     add_integer("vdpau-deinterlace",
751                 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
752                 N_("Deinterlace"), N_("Deinterlacing algorithm"), true)
753         change_integer_list(algo_values, algo_names)
754     add_bool("vdpau-ivtc", false,
755              N_("Inverse telecine"), N_("Inverse telecine"), true)
756     add_bool("vdpau-chroma-skip", false,
757              N_("Deinterlace chroma skip"),
758              N_("Whether temporal deinterlacing applies to luma only"), true)
759     add_float_with_range("vdpau-noise-reduction", 0., 0., 1.,
760         N_("Noise reduction level"), N_("Noise reduction level"), true)
761     add_integer_with_range("vdpau-scaling", 0, 0, 9,
762        N_("Scaling quality"), N_("High quality scaling level"), true)
763
764     add_submodule()
765     set_callbacks(YCbCrOpen, YCbCrClose)
766 vlc_module_end()