]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
Removed dead code in vout.
[vlc] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <libvlc.h>
36 #include <vlc_vout.h>
37 #include <vlc_osd.h>
38 #include <vlc_filter.h>
39 #include <vlc_image.h>
40 #include <vlc_block.h>
41 #include <vlc_picture_fifo.h>
42 #include <vlc_picture_pool.h>
43
44 #include "vout_pictures.h"
45 #include "vout_internal.h"
46
47 /**
48  * Display a picture
49  *
50  * Remove the reservation flag of a picture, which will cause it to be ready
51  * for display.
52  */
53 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
54 {
55     vlc_mutex_lock( &p_vout->picture_lock );
56
57     if( p_pic->i_status == RESERVED_PICTURE )
58     {
59         p_pic->i_status = READY_PICTURE;
60         vlc_cond_signal( &p_vout->p->picture_wait );
61     }
62     else
63     {
64         msg_Err( p_vout, "picture to display %p has invalid status %d",
65                          p_pic, p_pic->i_status );
66     }
67     p_vout->p->i_picture_qtype = p_pic->i_qtype;
68     p_vout->p->b_picture_interlaced = !p_pic->b_progressive;
69
70     vlc_mutex_unlock( &p_vout->picture_lock );
71 }
72
73 /**
74  * Allocate a picture in the video output heap.
75  *
76  * This function creates a reserved image in the video output heap.
77  * A null pointer is returned if the function fails. This method provides an
78  * already allocated zone of memory in the picture data fields.
79  * It needs locking since several pictures can be created by several producers
80  * threads.
81  */
82 int vout_CountPictureAvailable( vout_thread_t *p_vout )
83 {
84     int i_free = 0;
85     int i_pic;
86
87     vlc_mutex_lock( &p_vout->picture_lock );
88     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
89     {
90         picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
91
92         switch( p_pic->i_status )
93         {
94             case DESTROYED_PICTURE:
95                 i_free++;
96                 break;
97
98             case FREE_PICTURE:
99                 i_free++;
100                 break;
101
102             default:
103                 break;
104         }
105     }
106     vlc_mutex_unlock( &p_vout->picture_lock );
107
108     return i_free;
109 }
110
111 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
112                                bool b_progressive,
113                                bool b_top_field_first,
114                                unsigned int i_nb_fields )
115 {
116     int         i_pic;                                      /* picture index */
117     picture_t * p_pic;
118     picture_t * p_freepic = NULL;                      /* first free picture */
119
120     /* Get lock */
121     vlc_mutex_lock( &p_vout->picture_lock );
122
123     /*
124      * Look for an empty place in the picture heap.
125      */
126     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
127     {
128         p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
129                                  % I_RENDERPICTURES];
130
131         switch( p_pic->i_status )
132         {
133             case DESTROYED_PICTURE:
134                 /* Memory will not be reallocated, and function can end
135                  * immediately - this is the best possible case, since no
136                  * memory allocation needs to be done */
137                 p_pic->i_status   = RESERVED_PICTURE;
138                 p_pic->i_refcount = 0;
139                 p_pic->b_force    = 0;
140
141                 p_pic->b_progressive        = b_progressive;
142                 p_pic->i_nb_fields          = i_nb_fields;
143                 p_pic->b_top_field_first    = b_top_field_first;
144
145                 p_vout->i_heap_size++;
146                 p_vout->render.i_last_used_pic =
147                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
148                     % I_RENDERPICTURES;
149                 vlc_mutex_unlock( &p_vout->picture_lock );
150                 return( p_pic );
151
152             case FREE_PICTURE:
153                 /* Picture is empty and ready for allocation */
154                 p_vout->render.i_last_used_pic =
155                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
156                     % I_RENDERPICTURES;
157                 p_freepic = p_pic;
158                 break;
159
160             default:
161                 break;
162         }
163     }
164
165     /*
166      * Prepare picture
167      */
168     if( p_freepic != NULL )
169     {
170         vout_AllocatePicture( VLC_OBJECT(p_vout),
171                               p_freepic, p_vout->render.i_chroma,
172                               p_vout->render.i_width, p_vout->render.i_height,
173                               p_vout->render.i_aspect * p_vout->render.i_height,
174                               VOUT_ASPECT_FACTOR      * p_vout->render.i_width);
175
176         if( p_freepic->i_planes )
177         {
178             /* Copy picture information, set some default values */
179             p_freepic->i_status   = RESERVED_PICTURE;
180             p_freepic->i_type     = MEMORY_PICTURE;
181             p_freepic->b_slow     = 0;
182
183             p_freepic->i_refcount = 0;
184             p_freepic->b_force = 0;
185
186             p_freepic->b_progressive        = b_progressive;
187             p_freepic->i_nb_fields          = i_nb_fields;
188             p_freepic->b_top_field_first    = b_top_field_first;
189
190             p_vout->i_heap_size++;
191         }
192         else
193         {
194             /* Memory allocation failed : set picture as empty */
195             p_freepic->i_status = FREE_PICTURE;
196             p_freepic = NULL;
197
198             msg_Err( p_vout, "picture allocation failed" );
199         }
200
201         vlc_mutex_unlock( &p_vout->picture_lock );
202
203         return( p_freepic );
204     }
205
206     /* No free or destroyed picture could be found, but the decoder
207      * will try again in a while. */
208     vlc_mutex_unlock( &p_vout->picture_lock );
209
210     return( NULL );
211 }
212
213 /* */
214 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
215 {
216     vlc_assert_locked( &p_vout->picture_lock );
217
218     p_picture->i_status = DESTROYED_PICTURE;
219     p_vout->i_heap_size--;
220     picture_CleanupQuant( p_picture );
221
222     vlc_cond_signal( &p_vout->p->picture_wait );
223 }
224
225 /**
226  * Remove a permanent or reserved picture from the heap
227  *
228  * This function frees a previously reserved picture or a permanent
229  * picture. It is meant to be used when the construction of a picture aborted.
230  * Note that the picture will be destroyed even if it is linked !
231  *
232  * TODO remove it, vout_DropPicture should be used instead
233  */
234 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
235 {
236 #ifndef NDEBUG
237     /* Check if picture status is valid */
238     vlc_mutex_lock( &p_vout->picture_lock );
239     if( p_pic->i_status != RESERVED_PICTURE )
240     {
241         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
242                          p_pic, p_pic->i_status );
243     }
244     vlc_mutex_unlock( &p_vout->picture_lock );
245 #endif
246
247     vout_DropPicture( p_vout, p_pic );
248 }
249
250 /* */
251 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
252 {
253     vlc_assert_locked( &p_vout->picture_lock );
254     if( p_picture->i_refcount > 0 )
255     {
256         /* Pretend we displayed the picture, but don't destroy
257          * it since the decoder might still need it. */
258         p_picture->i_status = DISPLAYED_PICTURE;
259     }
260     else
261     {
262         /* Destroy the picture without displaying it */
263         DestroyPicture( p_vout, p_picture );
264     }
265 }
266
267 /* */
268 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic  )
269 {
270     vlc_mutex_lock( &p_vout->picture_lock );
271
272     if( p_pic->i_status == READY_PICTURE )
273     {
274         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
275         p_pic->date = 1;
276         vlc_cond_signal( &p_vout->p->picture_wait );
277     }
278     else
279     {
280         vout_UsePictureLocked( p_vout, p_pic );
281     }
282
283     vlc_mutex_unlock( &p_vout->picture_lock );
284 }
285
286 /**
287  * Increment reference counter of a picture
288  *
289  * This function increments the reference counter of a picture in the video
290  * heap. It needs a lock since several producer threads can access the picture.
291  */
292 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
293 {
294     vlc_mutex_lock( &p_vout->picture_lock );
295     p_pic->i_refcount++;
296     vlc_mutex_unlock( &p_vout->picture_lock );
297 }
298
299 /**
300  * Decrement reference counter of a picture
301  *
302  * This function decrement the reference counter of a picture in the video heap
303  */
304 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
305 {
306     vlc_mutex_lock( &p_vout->picture_lock );
307
308     if( p_pic->i_refcount > 0 )
309         p_pic->i_refcount--;
310     else
311         msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
312                  p_pic, p_pic->i_refcount );
313
314     if( p_pic->i_refcount == 0 &&
315         ( p_pic->i_status == DISPLAYED_PICTURE || p_pic->i_status == RESERVED_PICTURE ) )
316         DestroyPicture( p_vout, p_pic );
317
318     vlc_mutex_unlock( &p_vout->picture_lock );
319 }
320
321 /**
322  * Render a picture
323  *
324  * This function chooses whether the current picture needs to be copied
325  * before rendering, does the subpicture magic, and tells the video output
326  * thread which direct buffer needs to be displayed.
327  */
328 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
329                                subpicture_t *p_subpic, mtime_t render_date )
330 {
331     if( p_pic == NULL )
332         return NULL;
333
334     if( p_pic->i_type == DIRECT_PICTURE && !p_subpic )
335     {
336         /* No subtitles, picture is in a directbuffer so
337          * we can display it directly (even if it is still
338          * in use or not). */
339         return p_pic;
340     }
341
342     /* It is either because:
343      *  - the picture is not a direct buffer
344      *  - we have to render subtitles (we can never do it on the given
345      *  picture even if not referenced).
346      */
347     picture_t *p_render;
348     if( p_subpic != NULL && PP_OUTPUTPICTURE[0]->b_slow )
349     {
350         /* The picture buffer is in slow memory. We'll use
351          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
352          * one for subpictures rendering. */
353         p_render = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
354         if( p_render->i_status == FREE_PICTURE )
355         {
356             vout_AllocatePicture( VLC_OBJECT(p_vout),
357                                   p_render, p_vout->fmt_out.i_chroma,
358                                   p_vout->fmt_out.i_width,
359                                   p_vout->fmt_out.i_height,
360                                   p_vout->fmt_out.i_sar_num,
361                                   p_vout->fmt_out.i_sar_den );
362             p_render->i_type = MEMORY_PICTURE;
363             p_render->i_status = RESERVED_PICTURE;
364         }
365     }
366     else
367     {
368         /* We can directly render into a direct buffer */
369         p_render = PP_OUTPUTPICTURE[0];
370     }
371
372     /* Copy */
373     picture_Copy( p_render, p_pic );
374
375     /* Render the subtitles if present */
376     if( p_subpic )
377         spu_RenderSubpictures( p_vout->p_spu,
378                                p_render, &p_vout->fmt_out,
379                                p_subpic, &p_vout->fmt_in, render_date );
380     /* Copy in case we used a temporary fast buffer */
381     if( p_render != PP_OUTPUTPICTURE[0] )
382         picture_Copy( PP_OUTPUTPICTURE[0], p_render );
383
384     return PP_OUTPUTPICTURE[0];
385 }
386
387 /**
388  * Calculate image window coordinates
389  *
390  * This function will be accessed by plugins. It calculates the relative
391  * position of the output window and the image window.
392  */
393 void vout_PlacePicture( const vout_thread_t *p_vout,
394                         unsigned int i_width, unsigned int i_height,
395                         unsigned int *restrict pi_x,
396                         unsigned int *restrict pi_y,
397                         unsigned int *restrict pi_width,
398                         unsigned int *restrict pi_height )
399 {
400     if( i_width <= 0 || i_height <= 0 )
401     {
402         *pi_width = *pi_height = *pi_x = *pi_y = 0;
403         return;
404     }
405
406     if( p_vout->b_autoscale )
407     {
408         *pi_width = i_width;
409         *pi_height = i_height;
410     }
411     else
412     {
413         int i_zoom = p_vout->i_zoom;
414         /* be realistic, scaling factor confined between .2 and 10. */
415         if( i_zoom > 10 * ZOOM_FP_FACTOR )      i_zoom = 10 * ZOOM_FP_FACTOR;
416         else if( i_zoom <  ZOOM_FP_FACTOR / 5 ) i_zoom = ZOOM_FP_FACTOR / 5;
417
418         unsigned int i_original_width, i_original_height;
419
420         if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
421         {
422             i_original_width = p_vout->fmt_in.i_visible_width *
423                                p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den;
424             i_original_height =  p_vout->fmt_in.i_visible_height;
425         }
426         else
427         {
428             i_original_width =  p_vout->fmt_in.i_visible_width;
429             i_original_height = p_vout->fmt_in.i_visible_height *
430                                 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num;
431         }
432 #ifdef WIN32
433         /* On windows, inner video window exceeding container leads to black screen */
434         *pi_width = __MIN( i_width, i_original_width * i_zoom / ZOOM_FP_FACTOR );
435         *pi_height = __MIN( i_height, i_original_height * i_zoom / ZOOM_FP_FACTOR );
436 #else
437         *pi_width = i_original_width * i_zoom / ZOOM_FP_FACTOR ;
438         *pi_height = i_original_height * i_zoom / ZOOM_FP_FACTOR ;
439 #endif
440     }
441
442     int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
443                               *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
444     int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
445                                *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
446
447     if( i_scaled_width <= 0 || i_scaled_height <= 0 )
448     {
449         msg_Warn( p_vout, "ignoring broken aspect ratio" );
450         i_scaled_width = *pi_width;
451         i_scaled_height = *pi_height;
452     }
453
454     if( i_scaled_width > *pi_width )
455         *pi_height = i_scaled_height;
456     else
457         *pi_width = i_scaled_width;
458
459     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
460     {
461     case VOUT_ALIGN_LEFT:
462         *pi_x = 0;
463         break;
464     case VOUT_ALIGN_RIGHT:
465         *pi_x = i_width - *pi_width;
466         break;
467     default:
468         *pi_x = ( i_width - *pi_width ) / 2;
469     }
470
471     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
472     {
473     case VOUT_ALIGN_TOP:
474         *pi_y = 0;
475         break;
476     case VOUT_ALIGN_BOTTOM:
477         *pi_y = i_height - *pi_height;
478         break;
479     default:
480         *pi_y = ( i_height - *pi_height ) / 2;
481     }
482 }
483
484 #undef vout_AllocatePicture
485 /**
486  * Allocate a new picture in the heap.
487  *
488  * This function allocates a fake direct buffer in memory, which can be
489  * used exactly like a video buffer. The video output thread then manages
490  * how it gets displayed.
491  */
492 int vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
493                           vlc_fourcc_t i_chroma,
494                           int i_width, int i_height,
495                           int i_sar_num, int i_sar_den )
496 {
497     VLC_UNUSED(p_this);
498
499     /* Make sure the real dimensions are a multiple of 16 */
500     if( picture_Setup( p_pic, i_chroma, i_width, i_height,
501                        i_sar_num, i_sar_den ) != VLC_SUCCESS )
502         return VLC_EGENERIC;
503
504     /* Calculate how big the new image should be */
505     size_t i_bytes = 0;
506     for( int i = 0; i < p_pic->i_planes; i++ )
507     {
508         const plane_t *p = &p_pic->p[i];
509
510         if( p->i_pitch <= 0 || p->i_lines <= 0 ||
511             p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
512         {
513             p_pic->i_planes = 0;
514             return VLC_ENOMEM;
515         }
516         i_bytes += p->i_pitch * p->i_lines;
517     }
518
519     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
520     if( p_pic->p_data == NULL )
521     {
522         p_pic->i_planes = 0;
523         return VLC_EGENERIC;
524     }
525
526     /* Fill the p_pixels field for each plane */
527     p_pic->p[0].p_pixels = p_pic->p_data;
528     for( int i = 1; i < p_pic->i_planes; i++ )
529     {
530         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
531                                                         p_pic->p[i-1].i_pitch ];
532     }
533
534     return VLC_SUCCESS;
535 }
536
537 /*****************************************************************************
538  *
539  *****************************************************************************/
540 static void PictureReleaseCallback( picture_t *p_picture )
541 {
542     if( --p_picture->i_refcount > 0 )
543         return;
544     picture_Delete( p_picture );
545 }
546
547 /*****************************************************************************
548  *
549  *****************************************************************************/
550 void picture_Reset( picture_t *p_picture )
551 {
552     /* */
553     p_picture->date = VLC_TS_INVALID;
554     p_picture->b_force = false;
555     p_picture->b_progressive = false;
556     p_picture->i_nb_fields = 0;
557     p_picture->b_top_field_first = false;
558     picture_CleanupQuant( p_picture );
559 }
560
561 /*****************************************************************************
562  *
563  *****************************************************************************/
564 typedef struct
565 {
566     unsigned     i_plane_count;
567     struct
568     {
569         struct
570         {
571             unsigned i_num;
572             unsigned i_den;
573         } w;
574         struct
575         {
576             unsigned i_num;
577             unsigned i_den;
578         } h;
579     } p[VOUT_MAX_PLANES];
580     unsigned i_pixel_size;
581
582 } chroma_description_t;
583
584 #define PLANAR(n, w_den, h_den) \
585     { n, { {{1,1}, {1,1}}, {{1,w_den}, {1,h_den}}, {{1,w_den}, {1,h_den}}, {{1,1}, {1,1}} }, 1 }
586 #define PACKED(size) \
587     { 1, { {{1,1}, {1,1}} }, size }
588
589 static const struct
590 {
591     vlc_fourcc_t            p_fourcc[5];
592     chroma_description_t    description;
593 } p_chromas[] = {
594     { { VLC_CODEC_I411, 0 },                                 PLANAR(3, 4, 1) },
595     { { VLC_CODEC_I410, VLC_CODEC_YV9, 0 },                  PLANAR(3, 4, 4) },
596     { { VLC_CODEC_YV12, VLC_CODEC_I420, VLC_CODEC_J420, 0 }, PLANAR(3, 2, 2) },
597     { { VLC_CODEC_I422, VLC_CODEC_J422, 0 },                 PLANAR(3, 2, 1) },
598     { { VLC_CODEC_I440, VLC_CODEC_J440, 0 },                 PLANAR(3, 1, 2) },
599     { { VLC_CODEC_I444, VLC_CODEC_J444, 0 },                 PLANAR(3, 1, 1) },
600     { { VLC_CODEC_YUVA, 0 },                                 PLANAR(4, 1, 1) },
601
602     { { VLC_CODEC_UYVY, VLC_CODEC_VYUY, VLC_CODEC_YUYV, VLC_CODEC_YVYU, 0 }, PACKED(2) },
603     { { VLC_CODEC_RGB8, VLC_CODEC_GREY, VLC_CODEC_YUVP, VLC_CODEC_RGBP, 0 }, PACKED(1) },
604     { { VLC_CODEC_RGB16, VLC_CODEC_RGB15, 0 },                               PACKED(2) },
605     { { VLC_CODEC_RGB24, 0 },                                                PACKED(3) },
606     { { VLC_CODEC_RGB32, VLC_CODEC_RGBA, 0 },                                PACKED(4) },
607
608     { { VLC_CODEC_Y211, 0 }, { 1, { {{1,4}, {1,1}} }, 4 } },
609
610     { {0}, { 0, {}, 0 } }
611 };
612
613 #undef PACKED
614 #undef PLANAR
615
616 static const chroma_description_t *vlc_fourcc_GetChromaDescription( vlc_fourcc_t i_fourcc )
617 {
618     for( unsigned i = 0; p_chromas[i].p_fourcc[0]; i++ )
619     {
620         const vlc_fourcc_t *p_fourcc = p_chromas[i].p_fourcc;
621         for( unsigned j = 0; p_fourcc[j]; j++ )
622         {
623             if( p_fourcc[j] == i_fourcc )
624                 return &p_chromas[i].description;
625         }
626     }
627     return NULL;
628 }
629
630 static int LCM( int a, int b )
631 {
632     return a * b / GCD( a, b );
633 }
634
635 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
636                    int i_width, int i_height, int i_sar_num, int i_sar_den )
637 {
638     /* Store default values */
639     p_picture->i_planes = 0;
640     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
641     {
642         plane_t *p = &p_picture->p[i];
643         p->p_pixels = NULL;
644         p->i_pixel_pitch = 0;
645     }
646
647     p_picture->pf_release = NULL;
648     p_picture->p_release_sys = NULL;
649     p_picture->i_refcount = 0;
650
651     p_picture->i_qtype = QTYPE_NONE;
652     p_picture->i_qstride = 0;
653     p_picture->p_q = NULL;
654
655     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
656                         i_sar_num, i_sar_den );
657
658     const chroma_description_t *p_dsc =
659         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
660     if( !p_dsc )
661         return VLC_EGENERIC;
662
663     /* We want V (width/height) to respect:
664         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
665         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
666        Which is respected if you have
667        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
668     */
669     int i_modulo_w = 1;
670     int i_modulo_h = 1;
671     int i_ratio_h  = 1;
672     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
673     {
674         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.i_den );
675         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.i_den );
676         if( i_ratio_h < p_dsc->p[i].h.i_den )
677             i_ratio_h = p_dsc->p[i].h.i_den;
678     }
679
680     const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
681     const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
682     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
683     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
684     {
685         plane_t *p = &p_picture->p[i];
686
687         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
688         p->i_visible_lines = i_height * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
689         p->i_pitch         = i_width_aligned * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
690         p->i_visible_pitch = i_width * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
691         p->i_pixel_pitch   = p_dsc->i_pixel_size;
692
693         assert( (p->i_pitch % 16) == 0 );
694     }
695     p_picture->i_planes  = p_dsc->i_plane_count;
696
697     return VLC_SUCCESS;
698 }
699
700 /*****************************************************************************
701  *
702  *****************************************************************************/
703 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
704 {
705     video_format_t fmt = *p_fmt;
706
707     /* It is needed to be sure all informations are filled */
708     video_format_Setup( &fmt, p_fmt->i_chroma,
709                               p_fmt->i_width, p_fmt->i_height,
710                               p_fmt->i_sar_num, p_fmt->i_sar_den );
711
712     /* */
713     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
714     if( !p_picture )
715         return NULL;
716
717     if( p_resource )
718     {
719         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
720                            fmt.i_sar_num, fmt.i_sar_den ) )
721         {
722             free( p_picture );
723             return NULL;
724         }
725         p_picture->p_sys = p_resource->p_sys;
726
727         for( int i = 0; i < p_picture->i_planes; i++ )
728         {
729             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
730             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
731             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
732         }
733     }
734     else
735     {
736         if( vout_AllocatePicture( (vlc_object_t *)NULL, p_picture,
737                                   fmt.i_chroma, fmt.i_width, fmt.i_height,
738                                   fmt.i_sar_num, fmt.i_sar_den ) )
739         {
740             free( p_picture );
741             return NULL;
742         }
743     }
744     /* */
745     p_picture->format = fmt;
746     p_picture->i_refcount = 1;
747     p_picture->pf_release = PictureReleaseCallback;
748     p_picture->i_status = RESERVED_PICTURE;
749
750     return p_picture;
751 }
752 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
753 {
754     return picture_NewFromResource( p_fmt, NULL );
755 }
756 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
757 {
758     video_format_t fmt;
759
760     memset( &fmt, 0, sizeof(fmt) );
761     video_format_Setup( &fmt, i_chroma, i_width, i_height,
762                         i_sar_num, i_sar_den );
763
764     return picture_NewFromFormat( &fmt );
765 }
766
767 /*****************************************************************************
768  *
769  *****************************************************************************/
770 void picture_Delete( picture_t *p_picture )
771 {
772     assert( p_picture && p_picture->i_refcount == 0 );
773     assert( p_picture->p_release_sys == NULL );
774
775     free( p_picture->p_q );
776     free( p_picture->p_data_orig );
777     free( p_picture->p_sys );
778     free( p_picture );
779 }
780
781 /*****************************************************************************
782  *
783  *****************************************************************************/
784 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
785 {
786     int i;
787
788     for( i = 0; i < p_src->i_planes ; i++ )
789         plane_CopyPixels( p_dst->p+i, p_src->p+i );
790 }
791
792 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
793 {
794     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
795                                      p_src->i_visible_pitch );
796     const unsigned i_height = __MIN( p_dst->i_visible_lines,
797                                      p_src->i_visible_lines );
798
799     if( p_src->i_pitch == p_dst->i_pitch )
800     {
801         /* There are margins, but with the same width : perfect ! */
802         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
803                     p_src->i_pitch * i_height );
804     }
805     else
806     {
807         /* We need to proceed line by line */
808         uint8_t *p_in = p_src->p_pixels;
809         uint8_t *p_out = p_dst->p_pixels;
810         int i_line;
811
812         assert( p_in );
813         assert( p_out );
814
815         for( i_line = i_height; i_line--; )
816         {
817             vlc_memcpy( p_out, p_in, i_width );
818             p_in += p_src->i_pitch;
819             p_out += p_dst->i_pitch;
820         }
821     }
822 }
823
824 /*****************************************************************************
825  *
826  *****************************************************************************/
827 int picture_Export( vlc_object_t *p_obj,
828                     block_t **pp_image,
829                     video_format_t *p_fmt,
830                     picture_t *p_picture,
831                     vlc_fourcc_t i_format,
832                     int i_override_width, int i_override_height )
833 {
834     /* */
835     video_format_t fmt_in = p_picture->format;
836     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
837     {
838         fmt_in.i_sar_num =
839         fmt_in.i_sar_den = 1;
840     }
841
842     /* */
843     video_format_t fmt_out;
844     memset( &fmt_out, 0, sizeof(fmt_out) );
845     fmt_out.i_sar_num =
846     fmt_out.i_sar_den = 1;
847     fmt_out.i_chroma  = i_format;
848
849     /* compute original width/height */
850     unsigned int i_original_width;
851     unsigned int i_original_height;
852     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
853     {
854         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
855         i_original_height = fmt_in.i_height;
856     }
857     else
858     {
859         i_original_width =  fmt_in.i_width;
860         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
861     }
862
863     /* */
864     fmt_out.i_width  = ( i_override_width < 0 ) ?
865                        i_original_width : i_override_width;
866     fmt_out.i_height = ( i_override_height < 0 ) ?
867                        i_original_height : i_override_height;
868
869     /* scale if only one direction is provided */
870     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
871     {
872         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
873                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
874     }
875     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
876     {
877         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
878                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
879     }
880
881     image_handler_t *p_image = image_HandlerCreate( p_obj );
882
883     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
884
885     image_HandlerDelete( p_image );
886
887     if( !p_block )
888         return VLC_EGENERIC;
889
890     p_block->i_pts =
891     p_block->i_dts = p_picture->date;
892
893     if( p_fmt )
894         *p_fmt = fmt_out;
895     *pp_image = p_block;
896
897     return VLC_SUCCESS;
898 }
899