]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
Moved most of private vout_thread_t fields out of vlc_vout.h
[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->p->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->p->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->p->picture_lock );
88     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
89     {
90         picture_t *p_pic = PP_RENDERPICTURE[(p_vout->p->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->p->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->p->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->p->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->p->render.i_last_used_pic =
146                     ( p_vout->p->render.i_last_used_pic + i_pic + 1 )
147                     % I_RENDERPICTURES;
148                 vlc_mutex_unlock( &p_vout->p->picture_lock );
149                 return( p_pic );
150
151             case FREE_PICTURE:
152                 /* Picture is empty and ready for allocation */
153                 p_vout->p->render.i_last_used_pic =
154                     ( p_vout->p->render.i_last_used_pic + i_pic + 1 )
155                     % I_RENDERPICTURES;
156                 p_freepic = p_pic;
157                 break;
158
159             default:
160                 break;
161         }
162     }
163
164     /*
165      * Prepare picture
166      */
167     if( p_freepic != NULL )
168     {
169         vout_AllocatePicture( VLC_OBJECT(p_vout),
170                               p_freepic, p_vout->fmt_render.i_chroma,
171                               p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
172                               p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den ); /* The right AR is in fmt_in and not fmt_render */
173
174         if( p_freepic->i_planes )
175         {
176             /* Copy picture information, set some default values */
177             p_freepic->i_status   = RESERVED_PICTURE;
178             p_freepic->i_type     = MEMORY_PICTURE;
179             p_freepic->b_slow     = 0;
180
181             p_freepic->i_refcount = 0;
182             p_freepic->b_force = 0;
183
184             p_freepic->b_progressive        = b_progressive;
185             p_freepic->i_nb_fields          = i_nb_fields;
186             p_freepic->b_top_field_first    = b_top_field_first;
187
188         }
189         else
190         {
191             /* Memory allocation failed : set picture as empty */
192             p_freepic->i_status = FREE_PICTURE;
193             p_freepic = NULL;
194
195             msg_Err( p_vout, "picture allocation failed" );
196         }
197
198         vlc_mutex_unlock( &p_vout->p->picture_lock );
199
200         return( p_freepic );
201     }
202
203     /* No free or destroyed picture could be found, but the decoder
204      * will try again in a while. */
205     vlc_mutex_unlock( &p_vout->p->picture_lock );
206
207     return( NULL );
208 }
209
210 /* */
211 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
212 {
213     vlc_assert_locked( &p_vout->p->picture_lock );
214
215     p_picture->i_status = DESTROYED_PICTURE;
216     picture_CleanupQuant( p_picture );
217
218     vlc_cond_signal( &p_vout->p->picture_wait );
219 }
220
221 /**
222  * Remove a permanent or reserved picture from the heap
223  *
224  * This function frees a previously reserved picture or a permanent
225  * picture. It is meant to be used when the construction of a picture aborted.
226  * Note that the picture will be destroyed even if it is linked !
227  *
228  * TODO remove it, vout_DropPicture should be used instead
229  */
230 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
231 {
232 #ifndef NDEBUG
233     /* Check if picture status is valid */
234     vlc_mutex_lock( &p_vout->p->picture_lock );
235     if( p_pic->i_status != RESERVED_PICTURE )
236     {
237         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
238                          p_pic, p_pic->i_status );
239     }
240     vlc_mutex_unlock( &p_vout->p->picture_lock );
241 #endif
242
243     vout_DropPicture( p_vout, p_pic );
244 }
245
246 /* */
247 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
248 {
249     vlc_assert_locked( &p_vout->p->picture_lock );
250     if( p_picture->i_refcount > 0 )
251     {
252         /* Pretend we displayed the picture, but don't destroy
253          * it since the decoder might still need it. */
254         p_picture->i_status = DISPLAYED_PICTURE;
255     }
256     else
257     {
258         /* Destroy the picture without displaying it */
259         DestroyPicture( p_vout, p_picture );
260     }
261 }
262
263 /* */
264 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic  )
265 {
266     vlc_mutex_lock( &p_vout->p->picture_lock );
267
268     if( p_pic->i_status == READY_PICTURE )
269     {
270         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
271         p_pic->date = 1;
272         vlc_cond_signal( &p_vout->p->picture_wait );
273     }
274     else
275     {
276         vout_UsePictureLocked( p_vout, p_pic );
277     }
278
279     vlc_mutex_unlock( &p_vout->p->picture_lock );
280 }
281
282 /**
283  * Increment reference counter of a picture
284  *
285  * This function increments the reference counter of a picture in the video
286  * heap. It needs a lock since several producer threads can access the picture.
287  */
288 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
289 {
290     vlc_mutex_lock( &p_vout->p->picture_lock );
291     p_pic->i_refcount++;
292     vlc_mutex_unlock( &p_vout->p->picture_lock );
293 }
294
295 /**
296  * Decrement reference counter of a picture
297  *
298  * This function decrement the reference counter of a picture in the video heap
299  */
300 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
301 {
302     vlc_mutex_lock( &p_vout->p->picture_lock );
303
304     if( p_pic->i_refcount > 0 )
305         p_pic->i_refcount--;
306     else
307         msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
308                  p_pic, p_pic->i_refcount );
309
310     if( p_pic->i_refcount == 0 &&
311         ( p_pic->i_status == DISPLAYED_PICTURE || p_pic->i_status == RESERVED_PICTURE ) )
312         DestroyPicture( p_vout, p_pic );
313
314     vlc_mutex_unlock( &p_vout->p->picture_lock );
315 }
316
317 /**
318  * Render a picture
319  *
320  * This function chooses whether the current picture needs to be copied
321  * before rendering, does the subpicture magic, and tells the video output
322  * thread which direct buffer needs to be displayed.
323  */
324 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
325                                subpicture_t *p_subpic, mtime_t render_date )
326 {
327     if( p_pic == NULL )
328         return NULL;
329
330     if( p_pic->i_type == DIRECT_PICTURE && !p_subpic )
331     {
332         /* No subtitles, picture is in a directbuffer so
333          * we can display it directly (even if it is still
334          * in use or not). */
335         return p_pic;
336     }
337
338     /* It is either because:
339      *  - the picture is not a direct buffer
340      *  - we have to render subtitles (we can never do it on the given
341      *  picture even if not referenced).
342      */
343     picture_t *p_render;
344     if( p_subpic != NULL && PP_OUTPUTPICTURE[0]->b_slow )
345     {
346         /* The picture buffer is in slow memory. We'll use
347          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
348          * one for subpictures rendering. */
349         p_render = &p_vout->p->p_picture[2 * VOUT_MAX_PICTURES];
350         if( p_render->i_status == FREE_PICTURE )
351         {
352             vout_AllocatePicture( VLC_OBJECT(p_vout),
353                                   p_render, p_vout->fmt_out.i_chroma,
354                                   p_vout->fmt_out.i_width,
355                                   p_vout->fmt_out.i_height,
356                                   p_vout->fmt_out.i_sar_num,
357                                   p_vout->fmt_out.i_sar_den );
358             p_render->i_type = MEMORY_PICTURE;
359             p_render->i_status = RESERVED_PICTURE;
360         }
361     }
362     else
363     {
364         /* We can directly render into a direct buffer */
365         p_render = PP_OUTPUTPICTURE[0];
366     }
367
368     /* Copy */
369     picture_Copy( p_render, p_pic );
370
371     /* Render the subtitles if present */
372     if( p_subpic )
373         spu_RenderSubpictures( p_vout->p->p_spu,
374                                p_render, &p_vout->fmt_out,
375                                p_subpic, &p_vout->fmt_in, render_date );
376     /* Copy in case we used a temporary fast buffer */
377     if( p_render != PP_OUTPUTPICTURE[0] )
378         picture_Copy( PP_OUTPUTPICTURE[0], p_render );
379
380     return PP_OUTPUTPICTURE[0];
381 }
382
383 #undef vout_AllocatePicture
384 /**
385  * Allocate a new picture in the heap.
386  *
387  * This function allocates a fake direct buffer in memory, which can be
388  * used exactly like a video buffer. The video output thread then manages
389  * how it gets displayed.
390  */
391 int vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
392                           vlc_fourcc_t i_chroma,
393                           int i_width, int i_height,
394                           int i_sar_num, int i_sar_den )
395 {
396     VLC_UNUSED(p_this);
397
398     /* Make sure the real dimensions are a multiple of 16 */
399     if( picture_Setup( p_pic, i_chroma, i_width, i_height,
400                        i_sar_num, i_sar_den ) != VLC_SUCCESS )
401         return VLC_EGENERIC;
402
403     /* Calculate how big the new image should be */
404     size_t i_bytes = 0;
405     for( int i = 0; i < p_pic->i_planes; i++ )
406     {
407         const plane_t *p = &p_pic->p[i];
408
409         if( p->i_pitch <= 0 || p->i_lines <= 0 ||
410             p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
411         {
412             p_pic->i_planes = 0;
413             return VLC_ENOMEM;
414         }
415         i_bytes += p->i_pitch * p->i_lines;
416     }
417
418     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
419     if( p_pic->p_data == NULL )
420     {
421         p_pic->i_planes = 0;
422         return VLC_EGENERIC;
423     }
424
425     /* Fill the p_pixels field for each plane */
426     p_pic->p[0].p_pixels = p_pic->p_data;
427     for( int i = 1; i < p_pic->i_planes; i++ )
428     {
429         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
430                                                         p_pic->p[i-1].i_pitch ];
431     }
432
433     return VLC_SUCCESS;
434 }
435
436 /*****************************************************************************
437  *
438  *****************************************************************************/
439 static void PictureReleaseCallback( picture_t *p_picture )
440 {
441     if( --p_picture->i_refcount > 0 )
442         return;
443     picture_Delete( p_picture );
444 }
445
446 /*****************************************************************************
447  *
448  *****************************************************************************/
449 void picture_Reset( picture_t *p_picture )
450 {
451     /* */
452     p_picture->date = VLC_TS_INVALID;
453     p_picture->b_force = false;
454     p_picture->b_progressive = false;
455     p_picture->i_nb_fields = 0;
456     p_picture->b_top_field_first = false;
457     picture_CleanupQuant( p_picture );
458 }
459
460 /*****************************************************************************
461  *
462  *****************************************************************************/
463 typedef struct
464 {
465     unsigned     i_plane_count;
466     struct
467     {
468         struct
469         {
470             unsigned i_num;
471             unsigned i_den;
472         } w;
473         struct
474         {
475             unsigned i_num;
476             unsigned i_den;
477         } h;
478     } p[VOUT_MAX_PLANES];
479     unsigned i_pixel_size;
480
481 } chroma_description_t;
482
483 #define PLANAR(n, w_den, h_den) \
484     { n, { {{1,1}, {1,1}}, {{1,w_den}, {1,h_den}}, {{1,w_den}, {1,h_den}}, {{1,1}, {1,1}} }, 1 }
485 #define PACKED(size) \
486     { 1, { {{1,1}, {1,1}} }, size }
487
488 static const struct
489 {
490     vlc_fourcc_t            p_fourcc[5];
491     chroma_description_t    description;
492 } p_chromas[] = {
493     { { VLC_CODEC_I411, 0 },                                 PLANAR(3, 4, 1) },
494     { { VLC_CODEC_I410, VLC_CODEC_YV9, 0 },                  PLANAR(3, 4, 4) },
495     { { VLC_CODEC_YV12, VLC_CODEC_I420, VLC_CODEC_J420, 0 }, PLANAR(3, 2, 2) },
496     { { VLC_CODEC_I422, VLC_CODEC_J422, 0 },                 PLANAR(3, 2, 1) },
497     { { VLC_CODEC_I440, VLC_CODEC_J440, 0 },                 PLANAR(3, 1, 2) },
498     { { VLC_CODEC_I444, VLC_CODEC_J444, 0 },                 PLANAR(3, 1, 1) },
499     { { VLC_CODEC_YUVA, 0 },                                 PLANAR(4, 1, 1) },
500
501     { { VLC_CODEC_UYVY, VLC_CODEC_VYUY, VLC_CODEC_YUYV, VLC_CODEC_YVYU, 0 }, PACKED(2) },
502     { { VLC_CODEC_RGB8, VLC_CODEC_GREY, VLC_CODEC_YUVP, VLC_CODEC_RGBP, 0 }, PACKED(1) },
503     { { VLC_CODEC_RGB16, VLC_CODEC_RGB15, 0 },                               PACKED(2) },
504     { { VLC_CODEC_RGB24, 0 },                                                PACKED(3) },
505     { { VLC_CODEC_RGB32, VLC_CODEC_RGBA, 0 },                                PACKED(4) },
506
507     { { VLC_CODEC_Y211, 0 }, { 1, { {{1,4}, {1,1}} }, 4 } },
508
509     { {0}, { 0, {}, 0 } }
510 };
511
512 #undef PACKED
513 #undef PLANAR
514
515 static const chroma_description_t *vlc_fourcc_GetChromaDescription( vlc_fourcc_t i_fourcc )
516 {
517     for( unsigned i = 0; p_chromas[i].p_fourcc[0]; i++ )
518     {
519         const vlc_fourcc_t *p_fourcc = p_chromas[i].p_fourcc;
520         for( unsigned j = 0; p_fourcc[j]; j++ )
521         {
522             if( p_fourcc[j] == i_fourcc )
523                 return &p_chromas[i].description;
524         }
525     }
526     return NULL;
527 }
528
529 static int LCM( int a, int b )
530 {
531     return a * b / GCD( a, b );
532 }
533
534 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
535                    int i_width, int i_height, int i_sar_num, int i_sar_den )
536 {
537     /* Store default values */
538     p_picture->i_planes = 0;
539     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
540     {
541         plane_t *p = &p_picture->p[i];
542         p->p_pixels = NULL;
543         p->i_pixel_pitch = 0;
544     }
545
546     p_picture->pf_release = NULL;
547     p_picture->p_release_sys = NULL;
548     p_picture->i_refcount = 0;
549
550     p_picture->i_qtype = QTYPE_NONE;
551     p_picture->i_qstride = 0;
552     p_picture->p_q = NULL;
553
554     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
555                         i_sar_num, i_sar_den );
556
557     const chroma_description_t *p_dsc =
558         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
559     if( !p_dsc )
560         return VLC_EGENERIC;
561
562     /* We want V (width/height) to respect:
563         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
564         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
565        Which is respected if you have
566        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
567     */
568     int i_modulo_w = 1;
569     int i_modulo_h = 1;
570     int i_ratio_h  = 1;
571     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
572     {
573         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.i_den );
574         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.i_den );
575         if( i_ratio_h < p_dsc->p[i].h.i_den )
576             i_ratio_h = p_dsc->p[i].h.i_den;
577     }
578
579     const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
580     const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
581     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
582     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
583     {
584         plane_t *p = &p_picture->p[i];
585
586         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
587         p->i_visible_lines = i_height * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
588         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;
589         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;
590         p->i_pixel_pitch   = p_dsc->i_pixel_size;
591
592         assert( (p->i_pitch % 16) == 0 );
593     }
594     p_picture->i_planes  = p_dsc->i_plane_count;
595
596     return VLC_SUCCESS;
597 }
598
599 /*****************************************************************************
600  *
601  *****************************************************************************/
602 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
603 {
604     video_format_t fmt = *p_fmt;
605
606     /* It is needed to be sure all informations are filled */
607     video_format_Setup( &fmt, p_fmt->i_chroma,
608                               p_fmt->i_width, p_fmt->i_height,
609                               p_fmt->i_sar_num, p_fmt->i_sar_den );
610
611     /* */
612     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
613     if( !p_picture )
614         return NULL;
615
616     if( p_resource )
617     {
618         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
619                            fmt.i_sar_num, fmt.i_sar_den ) )
620         {
621             free( p_picture );
622             return NULL;
623         }
624         p_picture->p_sys = p_resource->p_sys;
625
626         for( int i = 0; i < p_picture->i_planes; i++ )
627         {
628             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
629             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
630             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
631         }
632     }
633     else
634     {
635         if( vout_AllocatePicture( (vlc_object_t *)NULL, p_picture,
636                                   fmt.i_chroma, fmt.i_width, fmt.i_height,
637                                   fmt.i_sar_num, fmt.i_sar_den ) )
638         {
639             free( p_picture );
640             return NULL;
641         }
642     }
643     /* */
644     p_picture->format = fmt;
645     p_picture->i_refcount = 1;
646     p_picture->pf_release = PictureReleaseCallback;
647     p_picture->i_status = RESERVED_PICTURE;
648
649     return p_picture;
650 }
651 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
652 {
653     return picture_NewFromResource( p_fmt, NULL );
654 }
655 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
656 {
657     video_format_t fmt;
658
659     memset( &fmt, 0, sizeof(fmt) );
660     video_format_Setup( &fmt, i_chroma, i_width, i_height,
661                         i_sar_num, i_sar_den );
662
663     return picture_NewFromFormat( &fmt );
664 }
665
666 /*****************************************************************************
667  *
668  *****************************************************************************/
669 void picture_Delete( picture_t *p_picture )
670 {
671     assert( p_picture && p_picture->i_refcount == 0 );
672     assert( p_picture->p_release_sys == NULL );
673
674     free( p_picture->p_q );
675     free( p_picture->p_data_orig );
676     free( p_picture->p_sys );
677     free( p_picture );
678 }
679
680 /*****************************************************************************
681  *
682  *****************************************************************************/
683 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
684 {
685     int i;
686
687     for( i = 0; i < p_src->i_planes ; i++ )
688         plane_CopyPixels( p_dst->p+i, p_src->p+i );
689 }
690
691 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
692 {
693     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
694                                      p_src->i_visible_pitch );
695     const unsigned i_height = __MIN( p_dst->i_visible_lines,
696                                      p_src->i_visible_lines );
697
698     if( p_src->i_pitch == p_dst->i_pitch )
699     {
700         /* There are margins, but with the same width : perfect ! */
701         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
702                     p_src->i_pitch * i_height );
703     }
704     else
705     {
706         /* We need to proceed line by line */
707         uint8_t *p_in = p_src->p_pixels;
708         uint8_t *p_out = p_dst->p_pixels;
709         int i_line;
710
711         assert( p_in );
712         assert( p_out );
713
714         for( i_line = i_height; i_line--; )
715         {
716             vlc_memcpy( p_out, p_in, i_width );
717             p_in += p_src->i_pitch;
718             p_out += p_dst->i_pitch;
719         }
720     }
721 }
722
723 /*****************************************************************************
724  *
725  *****************************************************************************/
726 int picture_Export( vlc_object_t *p_obj,
727                     block_t **pp_image,
728                     video_format_t *p_fmt,
729                     picture_t *p_picture,
730                     vlc_fourcc_t i_format,
731                     int i_override_width, int i_override_height )
732 {
733     /* */
734     video_format_t fmt_in = p_picture->format;
735     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
736     {
737         fmt_in.i_sar_num =
738         fmt_in.i_sar_den = 1;
739     }
740
741     /* */
742     video_format_t fmt_out;
743     memset( &fmt_out, 0, sizeof(fmt_out) );
744     fmt_out.i_sar_num =
745     fmt_out.i_sar_den = 1;
746     fmt_out.i_chroma  = i_format;
747
748     /* compute original width/height */
749     unsigned int i_original_width;
750     unsigned int i_original_height;
751     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
752     {
753         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
754         i_original_height = fmt_in.i_height;
755     }
756     else
757     {
758         i_original_width =  fmt_in.i_width;
759         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
760     }
761
762     /* */
763     fmt_out.i_width  = ( i_override_width < 0 ) ?
764                        i_original_width : i_override_width;
765     fmt_out.i_height = ( i_override_height < 0 ) ?
766                        i_original_height : i_override_height;
767
768     /* scale if only one direction is provided */
769     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
770     {
771         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
772                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
773     }
774     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
775     {
776         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
777                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
778     }
779
780     image_handler_t *p_image = image_HandlerCreate( p_obj );
781
782     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
783
784     image_HandlerDelete( p_image );
785
786     if( !p_block )
787         return VLC_EGENERIC;
788
789     p_block->i_pts =
790     p_block->i_dts = p_picture->date;
791
792     if( p_fmt )
793         *p_fmt = fmt_out;
794     *pp_image = p_block;
795
796     return VLC_SUCCESS;
797 }
798