]> git.sesse.net Git - vlc/blob - modules/video_filter/sepia.c
mediacodec: don't loop in GetOutput
[vlc] / modules / video_filter / sepia.c
1 /*****************************************************************************
2  * sepia.c : Sepia video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Branko Kokanovic <branko.kokanovic@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_cpu.h>
36 #include <vlc_atomic.h>
37
38 #include <assert.h>
39 #include "filter_picture.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create      ( vlc_object_t * );
45 static void Destroy     ( vlc_object_t * );
46
47 static void RVSepia( picture_t *, picture_t *, int );
48 static void PlanarI420Sepia( picture_t *, picture_t *, int);
49 static void PackedYUVSepia( picture_t *, picture_t *, int);
50 static picture_t *Filter( filter_t *, picture_t * );
51 static const char *const ppsz_filter_options[] = {
52     "intensity", NULL
53 };
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
59 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
60
61 #define CFG_PREFIX "sepia-"
62
63 vlc_module_begin ()
64     set_description( N_("Sepia video filter") )
65     set_shortname( N_("Sepia" ) )
66     set_help( N_("Gives video a warmer tone by applying sepia effect") )
67     set_category( CAT_VIDEO )
68     set_subcategory( SUBCAT_VIDEO_VFILTER )
69     set_capability( "video filter2", 0 )
70     add_integer_with_range( CFG_PREFIX "intensity", 120, 0, 255,
71                            SEPIA_INTENSITY_TEXT, SEPIA_INTENSITY_LONGTEXT,
72                            false )
73     set_callbacks( Create, Destroy )
74 vlc_module_end ()
75
76 /*****************************************************************************
77  * callback prototypes
78  *****************************************************************************/
79 static int FilterCallback( vlc_object_t *, char const *,
80                            vlc_value_t, vlc_value_t, void * );
81
82 typedef void (*SepiaFunction)( picture_t *, picture_t *, int );
83
84 static const struct
85 {
86     vlc_fourcc_t i_chroma;
87     SepiaFunction pf_sepia;
88 } p_sepia_cfg[] = {
89     { VLC_CODEC_I420, PlanarI420Sepia },
90     { VLC_CODEC_RGB24, RVSepia },
91     { VLC_CODEC_RGB32, RVSepia },
92     { VLC_CODEC_UYVY, PackedYUVSepia },
93     { VLC_CODEC_VYUY, PackedYUVSepia },
94     { VLC_CODEC_YUYV, PackedYUVSepia },
95     { VLC_CODEC_YVYU, PackedYUVSepia },
96     { 0, NULL }
97 };
98
99 /*****************************************************************************
100  * filter_sys_t: adjust filter method descriptor
101  *****************************************************************************/
102 struct filter_sys_t
103 {
104     SepiaFunction pf_sepia;
105     atomic_int i_intensity;
106 };
107
108 /*****************************************************************************
109  * Create: allocates Sepia video thread output method
110  *****************************************************************************
111  * This function allocates and initializes a Sepia vout method.
112  *****************************************************************************/
113 static int Create( vlc_object_t *p_this )
114 {
115     filter_t *p_filter = (filter_t *)p_this;
116     filter_sys_t *p_sys;
117
118     /* Allocate structure */
119     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120     if( p_filter->p_sys == NULL )
121         return VLC_ENOMEM;
122
123     p_sys->pf_sepia = NULL;
124
125     for( int i = 0; p_sepia_cfg[i].i_chroma != 0; i++ )
126     {
127         if( p_sepia_cfg[i].i_chroma != p_filter->fmt_in.video.i_chroma )
128             continue;
129         p_sys->pf_sepia = p_sepia_cfg[i].pf_sepia;
130     }
131
132     if( p_sys->pf_sepia == NULL )
133     {
134         msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
135                 (char*)&(p_filter->fmt_in.video.i_chroma) );
136         free( p_sys );
137         return VLC_EGENERIC;
138     }
139
140     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
141                        p_filter->p_cfg );
142     atomic_init( &p_sys->i_intensity,
143              var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "intensity" ) );
144     var_AddCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
145
146     p_filter->pf_video_filter = Filter;
147
148     return VLC_SUCCESS;
149 }
150
151 /*****************************************************************************
152  * Destroy: destroy sepia video thread output method
153  *****************************************************************************
154  * Terminate an output method
155  *****************************************************************************/
156 static void Destroy( vlc_object_t *p_this )
157 {
158     filter_t *p_filter = (filter_t *)p_this;
159
160     var_DelCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
161
162     free( p_filter->p_sys );
163 }
164
165 /*****************************************************************************
166  * Render: displays previously rendered output
167  *****************************************************************************
168  * This function send the currently rendered image to sepia image, waits
169  * until it is displayed and switch the two rendering buffers, preparing next
170  * frame.
171  *****************************************************************************/
172 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
173 {
174     picture_t *p_outpic;
175
176     if( !p_pic ) return NULL;
177
178     filter_sys_t *p_sys = p_filter->p_sys;
179     int intensity = atomic_load( &p_sys->i_intensity );
180
181     p_outpic = filter_NewPicture( p_filter );
182     if( !p_outpic )
183     {
184         msg_Warn( p_filter, "can't get output picture" );
185         picture_Release( p_pic );
186         return NULL;
187     }
188
189     p_sys->pf_sepia( p_pic, p_outpic, intensity );
190
191     return CopyInfoAndRelease( p_outpic, p_pic );
192 }
193
194 #if defined(CAN_COMPILE_SSE2)
195 /*****************************************************************************
196  * Sepia8ySSE2
197  *****************************************************************************
198  * This function applies sepia effect to eight bytes of yellow using SSE4.1
199  * instructions. It copies those 8 bytes to 128b register and fills the gaps
200  * with zeroes and following operations are made with word-operating instructs.
201  *****************************************************************************/
202 VLC_SSE
203 static inline void Sepia8ySSE2(uint8_t * dst, const uint8_t * src,
204                          int i_intensity_spread)
205 {
206     __asm__ volatile (
207         // y = y - y / 4 + i_intensity / 4
208         "movq            (%1), %%xmm1\n"
209         "punpcklbw     %%xmm7, %%xmm1\n"
210         "movq            (%1), %%xmm2\n" // store bytes as words with 0s in between
211         "punpcklbw     %%xmm7, %%xmm2\n"
212         "movd              %2, %%xmm3\n"
213         "pshufd    $0, %%xmm3, %%xmm3\n"
214         "psrlw             $2, %%xmm2\n"    // rotate right 2
215         "psubusb       %%xmm1, %%xmm2\n"    // subtract
216         "psrlw             $2, %%xmm3\n"
217         "paddsb        %%xmm1, %%xmm3\n"    // add
218         "packuswb      %%xmm2, %%xmm1\n"    // pack back to bytes
219         "movq          %%xmm1, (%0)  \n"    // load to dest
220         :
221         :"r" (dst), "r"(src), "r"(i_intensity_spread)
222         :"memory", "xmm1", "xmm2", "xmm3");
223 }
224
225 VLC_SSE
226 static void PlanarI420SepiaSSE( picture_t *p_pic, picture_t *p_outpic,
227                                 int i_intensity )
228 {
229     /* prepared values to copy for U and V channels */
230     const uint8_t filling_const_8u = 128 - i_intensity / 6;
231     const uint8_t filling_const_8v = 128 + i_intensity / 14;
232     /* prepared value for faster broadcasting in xmm register */
233     int i_intensity_spread = 0x10001 * (uint8_t) i_intensity;
234
235     __asm__ volatile(
236         "pxor      %%xmm7, %%xmm7\n"
237         ::: "xmm7");
238
239     /* iterate for every two visible line in the frame */
240     for (int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
241     {
242         const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
243         const int i_dy_line2_start = (y + 1) * p_outpic->p[Y_PLANE].i_pitch;
244         const int i_du_line_start =  (y / 2) * p_outpic->p[U_PLANE].i_pitch;
245         const int i_dv_line_start =  (y / 2) * p_outpic->p[V_PLANE].i_pitch;
246         int x = 0;
247         /* iterate for every visible line in the frame (eight values at once) */
248         for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 15; x += 16 )
249         {
250             /* Compute yellow channel values with asm function */
251             Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
252                         &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
253                         i_intensity_spread );
254             Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
255                         &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
256                         i_intensity_spread );
257             Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
258                         &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
259                         i_intensity_spread );
260             Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
261                         &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
262                         i_intensity_spread );
263             /* Copy precomputed values to destination memory location */
264             memset(&p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)],
265                    filling_const_8u, 8 );
266             memset(&p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)],
267                    filling_const_8v, 8 );
268         }
269         /* Completing the job, the cycle above takes really big chunks, so
270            this makes sure the job will be done completely */
271         for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 1; x += 2 )
272         {
273             // y = y - y/4 {to prevent overflow} + intensity / 4
274             p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
275                 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
276                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
277                 (i_intensity >> 2);
278             p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
279                 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
280                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
281                 (i_intensity >> 2);
282             p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
283                 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
284                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
285                 (i_intensity >> 2);
286             p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
287                 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
288                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
289                 (i_intensity >> 2);
290             // u = 128 {half => B&W} - intensity / 6
291             p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
292                 filling_const_8u;
293             // v = 128 {half => B&W} + intensity / 14
294             p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
295                 filling_const_8v;
296         }
297     }
298 }
299 #endif
300
301 /*****************************************************************************
302  * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
303  *****************************************************************************
304  * This function applies sepia effect to one frame of the video by iterating
305  * through video lines. We iterate for every two lines and for every two pixels
306  * in line to calculate new sepia values for four y components as well for u
307  * and v components.
308  *****************************************************************************/
309 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
310                                int i_intensity )
311 {
312 #if defined(CAN_COMPILE_SSE2)
313     if (vlc_CPU_SSE2())
314         return PlanarI420SepiaSSE( p_pic, p_outpic, i_intensity );
315 #endif
316
317     // prepared values to copy for U and V channels
318     const uint8_t filling_const_8u = 128 - i_intensity / 6;
319     const uint8_t filling_const_8v = 128 + i_intensity / 14;
320
321     /* iterate for every two visible line in the frame */
322     for( int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
323     {
324         const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
325         const int i_dy_line2_start = ( y + 1 ) * p_outpic->p[Y_PLANE].i_pitch;
326         const int i_du_line_start = (y/2) * p_outpic->p[U_PLANE].i_pitch;
327         const int i_dv_line_start = (y/2) * p_outpic->p[V_PLANE].i_pitch;
328         // to prevent sigsegv if one pic is smaller (theoretically)
329         int i_picture_size_limit = p_pic->p[Y_PLANE].i_visible_pitch
330                   < p_outpic->p[Y_PLANE].i_visible_pitch
331                   ? (p_pic->p[Y_PLANE].i_visible_pitch - 1) :
332                   (p_outpic->p[Y_PLANE].i_visible_pitch - 1);
333         /* iterate for every two visible line in the frame */
334         for( int x = 0; x < i_picture_size_limit; x += 2)
335         {
336             // y = y - y/4 {to prevent overflow} + intensity / 4
337             p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
338                 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
339                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
340                 (i_intensity >> 2);
341             p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
342                 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
343                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
344                 (i_intensity >> 2);
345             p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
346                 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
347                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
348                 (i_intensity >> 2);
349             p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
350                 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
351                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
352                 (i_intensity >> 2);
353             // u = 128 {half => B&W} - intensity / 6
354             p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
355                 filling_const_8u;
356             // v = 128 {half => B&W} + intensity / 14
357             p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
358                 filling_const_8v;
359         }
360     }
361 }
362
363 /*****************************************************************************
364  * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
365  *****************************************************************************
366  * This function applies sepia effext to one frame of the video by iterating
367  * through video lines. In every pass, we calculate new values for pixels
368  * (UYVY, VYUY, YUYV and YVYU formats are supported)
369  *****************************************************************************/
370 static void PackedYUVSepia( picture_t *p_pic, picture_t *p_outpic,
371                            int i_intensity )
372 {
373     uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
374     int i_yindex = 1, i_uindex = 2, i_vindex = 0;
375
376     GetPackedYuvOffsets( p_outpic->format.i_chroma,
377                         &i_yindex, &i_uindex, &i_vindex );
378
379     // prepared values to copy for U and V channels
380     const uint8_t filling_const_8u = 128 - i_intensity / 6;
381     const uint8_t filling_const_8v = 128 + i_intensity / 14;
382
383     p_in = p_pic->p[0].p_pixels;
384     p_in_end = p_in + p_pic->p[0].i_visible_lines
385         * p_pic->p[0].i_pitch;
386     p_out = p_outpic->p[0].p_pixels;
387
388     {
389         while( p_in < p_in_end )
390         {
391             p_line_end = p_in + p_pic->p[0].i_visible_pitch;
392             while( p_in < p_line_end )
393             {
394                 /* calculate new, sepia values */
395                 p_out[i_yindex] =
396                     p_in[i_yindex] - (p_in[i_yindex] >> 2) + (i_intensity >> 2);
397                 p_out[i_yindex + 2] =
398                     p_in[i_yindex + 2] - (p_in[i_yindex + 2] >> 2)
399                     + (i_intensity >> 2);
400                 p_out[i_uindex] = filling_const_8u;
401                 p_out[i_vindex] = filling_const_8v;
402                 p_in += 4;
403                 p_out += 4;
404             }
405             p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
406             p_out += p_outpic->p[0].i_pitch
407                 - p_outpic->p[0].i_visible_pitch;
408         }
409     }
410 }
411
412 /*****************************************************************************
413  * RVSepia: Applies sepia to one frame of the RV24/RV32 video
414  *****************************************************************************
415  * This function applies sepia effect to one frame of the video by iterating
416  * through video lines and calculating new values for every byte in chunks of
417  * 3 (RV24) or 4 (RV32) bytes.
418  *****************************************************************************/
419 static void RVSepia( picture_t *p_pic, picture_t *p_outpic, int i_intensity )
420 {
421 #define SCALEBITS 10
422 #define ONE_HALF  (1 << (SCALEBITS - 1))
423 #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
424     uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
425     bool b_isRV32 = p_pic->format.i_chroma == VLC_CODEC_RGB32;
426     int i_rindex = 0, i_gindex = 1, i_bindex = 2;
427
428     GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
429
430     p_in = p_pic->p[0].p_pixels;
431     p_in_end = p_in + p_pic->p[0].i_visible_lines
432         * p_pic->p[0].i_pitch;
433     p_out = p_outpic->p[0].p_pixels;
434
435     /* Precompute values constant for this certain i_intensity, using the same
436      * formula as YUV functions above */
437     uint8_t r_intensity = (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity * 14)
438                         + ONE_HALF )) >> SCALEBITS;
439     uint8_t g_intensity = (( - FIX(0.34414*255.0/224.0) * ( - i_intensity / 6 )
440                         - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity * 14 )
441                         + ONE_HALF )) >> SCALEBITS;
442     uint8_t b_intensity = (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity / 6 )
443                         + ONE_HALF )) >> SCALEBITS;
444
445     while (p_in < p_in_end)
446     {
447         p_line_end = p_in + p_pic->p[0].i_visible_pitch;
448         while (p_in < p_line_end)
449         {
450             /* do sepia: this calculation is based on the formula to calculate
451              * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
452              * y = y - y/4 + intensity/4 . As Y is the only channel that changes
453              * through the whole image. After that, precomputed values are added
454              * for each RGB channel and saved in the output image.
455              * FIXME: needs cleanup */
456             uint8_t i_y = ((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] +  25
457                       * p_in[i_bindex] + 128 ) >> 8 ) * FIX(255.0/219.0))
458                       - (((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
459                       * p_in[i_bindex] + 128 ) >> 8 )
460                       * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity >> 2 );
461             p_out[i_rindex] = vlc_uint8(i_y + r_intensity);
462             p_out[i_gindex] = vlc_uint8(i_y + g_intensity);
463             p_out[i_bindex] = vlc_uint8(i_y + b_intensity);
464             p_in += 3;
465             p_out += 3;
466             /* for rv32 we take 4 chunks at the time */
467             if (b_isRV32) {
468             /* alpha channel stays the same */
469             *p_out++ = *p_in++;
470             }
471         }
472
473         p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
474         p_out += p_outpic->p[0].i_pitch
475             - p_outpic->p[0].i_visible_pitch;
476     }
477 #undef SCALEBITS
478 #undef ONE_HALF
479 #undef FIX
480 }
481
482 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
483                             vlc_value_t oldval, vlc_value_t newval,
484                             void *p_data )
485 {
486     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
487     filter_t *p_filter = (filter_t*)p_this;
488     filter_sys_t *p_sys = p_filter->p_sys;
489
490     atomic_store( &p_sys->i_intensity, newval.i_int );
491     return VLC_SUCCESS;
492 }