access: cdda: use track cdtext over album cdtext data
[vlc.git] / src / misc / picture.c
1 /*****************************************************************************
2  * picture.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2010 VLC authors and VideoLAN
5  * Copyright (C) 2009-2010 Laurent Aimar
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 #include <assert.h>
34 #include <limits.h>
35
36 #include <vlc_common.h>
37 #include "picture.h"
38 #include <vlc_image.h>
39 #include <vlc_block.h>
40
41 static void PictureDestroyContext( picture_t *p_picture )
42 {
43     picture_context_t *ctx = p_picture->context;
44     if (ctx != NULL)
45     {
46         ctx->destroy(ctx);
47         p_picture->context = NULL;
48     }
49 }
50
51 /**
52  * Destroys a picture allocated by picture_NewFromResource() but without
53  * a custom destruction callback.
54  */
55 static void picture_DestroyDummy( picture_t *p_picture )
56 {
57     (void) p_picture;
58 }
59
60 /**
61  * Destroys a picture allocated with picture_NewFromFormat().
62  */
63 static void picture_DestroyFromFormat(picture_t *pic)
64 {
65     picture_buffer_t *res = pic->p_sys;
66
67     if (res != NULL)
68         picture_Deallocate(res->fd, res->base, res->size);
69 }
70
71 VLC_WEAK void *picture_Allocate(int *restrict fdp, size_t size)
72 {
73     assert((size % 64) == 0);
74     *fdp = -1;
75     return aligned_alloc(64, size);
76 }
77
78 VLC_WEAK void picture_Deallocate(int fd, void *base, size_t size)
79 {
80     assert(fd == -1);
81     aligned_free(base);
82     assert((size % 64) == 0);
83 }
84
85 /*****************************************************************************
86  *
87  *****************************************************************************/
88 void picture_Reset( picture_t *p_picture )
89 {
90     /* */
91     p_picture->date = VLC_TICK_INVALID;
92     p_picture->b_force = false;
93     p_picture->b_still = false;
94     p_picture->b_progressive = false;
95     p_picture->i_nb_fields = 2;
96     p_picture->b_top_field_first = false;
97     PictureDestroyContext( p_picture );
98 }
99
100 /*****************************************************************************
101  *
102  *****************************************************************************/
103 static int LCM( int a, int b )
104 {
105     return a * b / GCD( a, b );
106 }
107
108 int picture_Setup( picture_t *p_picture, const video_format_t *restrict fmt )
109 {
110     const vlc_chroma_description_t *p_dsc =
111         vlc_fourcc_GetChromaDescription( fmt->i_chroma );
112     if( unlikely(!p_dsc) )
113         return VLC_EGENERIC;
114
115     /* Store default values */
116     p_picture->i_planes = 0;
117     for( unsigned i = 0; i < ARRAY_SIZE(p_picture->p); i++ )
118     {
119         plane_t *p = &p_picture->p[i];
120         p->p_pixels = NULL;
121         p->i_pixel_pitch = 0;
122     }
123
124     p_picture->i_nb_fields = 2;
125
126     video_format_Setup( &p_picture->format, fmt->i_chroma, fmt->i_width, fmt->i_height,
127                         fmt->i_visible_width, fmt->i_visible_height,
128                         fmt->i_sar_num, fmt->i_sar_den );
129     if( fmt->i_x_offset < fmt->i_width &&
130         fmt->i_y_offset < fmt->i_height &&
131         fmt->i_visible_width  > 0 && fmt->i_x_offset + fmt->i_visible_width  <= fmt->i_width &&
132         fmt->i_visible_height > 0 && fmt->i_y_offset + fmt->i_visible_height <= fmt->i_height )
133         video_format_CopyCrop( &p_picture->format, fmt );
134
135     /* We want V (width/height) to respect:
136         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
137         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
138        Which is respected if you have
139        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
140     */
141     unsigned i_modulo_w = 1;
142     unsigned i_modulo_h = 1;
143     unsigned i_ratio_h  = 1;
144
145     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
146     {
147         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
148         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.den );
149         if( i_ratio_h < p_dsc->p[i].h.den )
150             i_ratio_h = p_dsc->p[i].h.den;
151     }
152     i_modulo_h = LCM( i_modulo_h, 32 );
153
154     unsigned width, height;
155
156     if (unlikely(add_overflow(fmt->i_width, i_modulo_w - 1, &width))
157      || unlikely(add_overflow(fmt->i_height, i_modulo_h - 1, &height)))
158         return VLC_EGENERIC;
159
160     width = width / i_modulo_w * i_modulo_w;
161     height = height / i_modulo_h * i_modulo_h;
162
163     /* plane_t uses 'int'. */
164     if (unlikely(width > INT_MAX) || unlikely(height > INT_MAX))
165         return VLC_EGENERIC;
166
167     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
168     {
169         plane_t *p = &p_picture->p[i];
170         const vlc_rational_t *h = &p_dsc->p[i].h;
171         const vlc_rational_t *w = &p_dsc->p[i].w;
172
173         /* A plane cannot be over-sampled. This could lead to overflow. */
174         assert(h->den >= h->num);
175         assert(w->den >= w->num);
176
177         p->i_lines = height * h->num / h->den;
178         p->i_visible_lines = (fmt->i_visible_height + (h->den - 1)) / h->den * h->num;
179
180         p->i_pitch = width * w->num / w->den * p_dsc->pixel_size;
181         p->i_visible_pitch = (fmt->i_visible_width + (w->den - 1)) / w->den * w->num
182                              * p_dsc->pixel_size;
183         p->i_pixel_pitch = p_dsc->pixel_size;
184
185         assert( (p->i_pitch % 16) == 0 );
186     }
187     p_picture->i_planes = p_dsc->plane_count;
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  *
194  *****************************************************************************/
195
196 static picture_priv_t *picture_NewPrivate(const video_format_t *restrict p_fmt,
197                                           size_t extra)
198 {
199     /* */
200     picture_priv_t *priv = malloc(sizeof (*priv) + extra);
201     if( unlikely(priv == NULL) )
202         return NULL;
203
204     picture_t *p_picture = &priv->picture;
205
206     memset( p_picture, 0, sizeof( *p_picture ) );
207
208     p_picture->format = *p_fmt;
209     /* Make sure the real dimensions are a multiple of 16 */
210     if( picture_Setup( p_picture, p_fmt ) )
211     {
212         free( p_picture );
213         return NULL;
214     }
215
216     atomic_init(&p_picture->refs, 1);
217     priv->gc.opaque = NULL;
218
219     return priv;
220 }
221
222 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
223 {
224     assert(p_resource != NULL);
225
226     picture_priv_t *priv = picture_NewPrivate(p_fmt, 0);
227     if (unlikely(priv == NULL))
228         return NULL;
229
230     picture_t *p_picture = &priv->picture;
231
232     p_picture->p_sys = p_resource->p_sys;
233
234     if( p_resource->pf_destroy != NULL )
235         priv->gc.destroy = p_resource->pf_destroy;
236     else
237         priv->gc.destroy = picture_DestroyDummy;
238
239     for( int i = 0; i < p_picture->i_planes; i++ )
240     {
241         p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
242         p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
243         p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
244     }
245
246     return p_picture;
247 }
248
249 #define PICTURE_SW_SIZE_MAX (UINT32_C(1) << 28) /* 256MB: 8K * 8K * 4*/
250
251 picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
252 {
253     picture_priv_t *priv = picture_NewPrivate(fmt, sizeof (picture_buffer_t));
254     if (unlikely(priv == NULL))
255         return NULL;
256
257     priv->gc.destroy = picture_DestroyFromFormat;
258
259     picture_t *pic = &priv->picture;
260     if (pic->i_planes == 0) {
261         pic->p_sys = NULL;
262         return pic;
263     }
264
265     /* Calculate how big the new image should be */
266     size_t plane_sizes[PICTURE_PLANE_MAX];
267     size_t pic_size = 0;
268
269     for (int i = 0; i < pic->i_planes; i++)
270     {
271         const plane_t *p = &pic->p[i];
272
273         if (unlikely(mul_overflow(p->i_pitch, p->i_lines, &plane_sizes[i]))
274          || unlikely(add_overflow(pic_size, plane_sizes[i], &pic_size)))
275             goto error;
276     }
277
278     if (unlikely(pic_size >= PICTURE_SW_SIZE_MAX))
279         goto error;
280
281     picture_buffer_t *res = (void *)priv->extra;
282
283     unsigned char *buf = picture_Allocate(&res->fd, pic_size);
284     if (unlikely(buf == NULL))
285         goto error;
286
287     res->base = buf;
288     res->size = pic_size;
289     res->offset = 0;
290     pic->p_sys = res;
291
292     /* Fill the p_pixels field for each plane */
293     for (int i = 0; i < pic->i_planes; i++)
294     {
295         pic->p[i].p_pixels = buf;
296         buf += plane_sizes[i];
297     }
298
299     return pic;
300 error:
301     free(pic);
302     return NULL;
303 }
304
305 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
306 {
307     video_format_t fmt;
308
309     video_format_Init( &fmt, 0 );
310     video_format_Setup( &fmt, i_chroma, i_width, i_height,
311                         i_width, i_height, i_sar_num, i_sar_den );
312
313     return picture_NewFromFormat( &fmt );
314 }
315
316 /*****************************************************************************
317  *
318  *****************************************************************************/
319
320 void picture_Destroy(picture_t *picture)
321 {
322     /* See changes from other threads */
323     atomic_thread_fence(memory_order_acquire);
324     assert(atomic_load_explicit(&picture->refs, memory_order_relaxed) == 0);
325
326     PictureDestroyContext(picture);
327
328     picture_priv_t *priv = container_of(picture, picture_priv_t, picture);
329     assert(priv->gc.destroy != NULL);
330     priv->gc.destroy(picture);
331     free(priv);
332 }
333
334 /*****************************************************************************
335  *
336  *****************************************************************************/
337 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
338 {
339     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
340                                      p_src->i_visible_pitch );
341     const unsigned i_height = __MIN( p_dst->i_visible_lines,
342                                      p_src->i_visible_lines );
343
344     /* The 2x visible pitch check does two things:
345        1) Makes field plane_t's work correctly (see the deinterlacer module)
346        2) Moves less data if the pitch and visible pitch differ much.
347     */
348     if( p_src->i_pitch == p_dst->i_pitch  &&
349         p_src->i_pitch < 2*p_src->i_visible_pitch )
350     {
351         /* There are margins, but with the same width : perfect ! */
352         memcpy( p_dst->p_pixels, p_src->p_pixels,
353                     p_src->i_pitch * i_height );
354     }
355     else
356     {
357         /* We need to proceed line by line */
358         uint8_t *p_in = p_src->p_pixels;
359         uint8_t *p_out = p_dst->p_pixels;
360
361         assert( p_in );
362         assert( p_out );
363
364         for( int i_line = i_height; i_line--; )
365         {
366             memcpy( p_out, p_in, i_width );
367             p_in += p_src->i_pitch;
368             p_out += p_dst->i_pitch;
369         }
370     }
371 }
372
373 void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
374 {
375     p_dst->date = p_src->date;
376     p_dst->b_force = p_src->b_force;
377     p_dst->b_still = p_src->b_still;
378
379     p_dst->b_progressive = p_src->b_progressive;
380     p_dst->i_nb_fields = p_src->i_nb_fields;
381     p_dst->b_top_field_first = p_src->b_top_field_first;
382 }
383
384 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
385 {
386     for( int i = 0; i < p_src->i_planes ; i++ )
387         plane_CopyPixels( p_dst->p+i, p_src->p+i );
388
389     assert( p_dst->context == NULL );
390
391     if( p_src->context != NULL )
392         p_dst->context = p_src->context->copy( p_src->context );
393 }
394
395 void picture_Copy( picture_t *p_dst, const picture_t *p_src )
396 {
397     picture_CopyPixels( p_dst, p_src );
398     picture_CopyProperties( p_dst, p_src );
399 }
400
401 static void picture_DestroyClone(picture_t *clone)
402 {
403     picture_t *picture = ((picture_priv_t *)clone)->gc.opaque;
404
405     picture_Release(picture);
406 }
407
408 picture_t *picture_Clone(picture_t *picture)
409 {
410     /* TODO: merge common code with picture_pool_ClonePicture(). */
411     picture_resource_t res = {
412         .p_sys = picture->p_sys,
413         .pf_destroy = picture_DestroyClone,
414     };
415
416     for (int i = 0; i < picture->i_planes; i++) {
417         res.p[i].p_pixels = picture->p[i].p_pixels;
418         res.p[i].i_lines = picture->p[i].i_lines;
419         res.p[i].i_pitch = picture->p[i].i_pitch;
420     }
421
422     picture_t *clone = picture_NewFromResource(&picture->format, &res);
423     if (likely(clone != NULL)) {
424         ((picture_priv_t *)clone)->gc.opaque = picture;
425         picture_Hold(picture);
426
427         if (picture->context != NULL)
428             clone->context = picture->context->copy(picture->context);
429     }
430     return clone;
431 }
432
433 /*****************************************************************************
434  *
435  *****************************************************************************/
436 int picture_Export( vlc_object_t *p_obj,
437                     block_t **pp_image,
438                     video_format_t *p_fmt,
439                     picture_t *p_picture,
440                     vlc_fourcc_t i_format,
441                     int i_override_width, int i_override_height,
442                     bool b_crop )
443 {
444     /* */
445     video_format_t fmt_in = p_picture->format;
446     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
447     {
448         fmt_in.i_sar_num =
449         fmt_in.i_sar_den = 1;
450     }
451
452     /* */
453     video_format_t fmt_out;
454     memset( &fmt_out, 0, sizeof(fmt_out) );
455     fmt_out.i_sar_num =
456     fmt_out.i_sar_den = 1;
457     fmt_out.i_chroma  = i_format;
458
459     /* compute original width/height */
460     unsigned int i_width, i_height, i_original_width, i_original_height;
461     if( fmt_in.i_visible_width > 0 && fmt_in.i_visible_height > 0 )
462     {
463         i_width = fmt_in.i_visible_width;
464         i_height = fmt_in.i_visible_height;
465     }
466     else
467     {
468         i_width = fmt_in.i_width;
469         i_height = fmt_in.i_height;
470     }
471     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
472     {
473         i_original_width = (int64_t)i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
474         i_original_height = i_height;
475     }
476     else
477     {
478         i_original_width =  i_width;
479         i_original_height = i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
480     }
481
482     /* */
483     if( b_crop && i_override_width > 0 && i_override_height > 0 )
484     {
485         float f_ar_dest = (float)i_override_width / i_override_height;
486         float f_ar_src = (float)i_width / i_height;
487         unsigned int i_crop_width, i_crop_height;
488         if ( f_ar_dest > f_ar_src )
489         {
490             i_crop_width = i_width;
491             i_crop_height = (float)i_crop_width / f_ar_dest;
492         }
493         else
494         {
495             i_crop_height = i_height;
496             i_crop_width = (float)i_crop_height * f_ar_dest;
497         }
498         fmt_out.i_width = i_override_width;
499         fmt_out.i_height = i_override_height;
500         fmt_in.i_visible_width = i_crop_width;
501         fmt_in.i_visible_height = i_crop_height;
502         fmt_in.i_x_offset += (i_width - i_crop_width) / 2;
503         fmt_in.i_y_offset += (i_height - i_crop_height) / 2;
504     }
505     else
506     {
507         fmt_out.i_width  = ( i_override_width < 0 ) ?
508                            i_original_width : (unsigned)i_override_width;
509         fmt_out.i_height = ( i_override_height < 0 ) ?
510                            i_original_height : (unsigned)i_override_height;
511     }
512
513     /* scale if only one direction is provided */
514     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
515     {
516         fmt_out.i_height = i_height * fmt_out.i_width
517                          * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
518     }
519     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
520     {
521         fmt_out.i_width  = i_width * fmt_out.i_height
522                          * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
523     }
524
525     image_handler_t *p_image = image_HandlerCreate( p_obj );
526     if( !p_image )
527         return VLC_ENOMEM;
528
529     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
530
531     image_HandlerDelete( p_image );
532
533     if( !p_block )
534         return VLC_EGENERIC;
535
536     p_block->i_pts =
537     p_block->i_dts = p_picture->date;
538
539     if( p_fmt )
540         *p_fmt = fmt_out;
541     *pp_image = p_block;
542
543     return VLC_SUCCESS;
544 }