]> git.sesse.net Git - mlt/blob - src/modules/avformat/filter_avcolour_space.c
Fix YUV to RGB conversion when profile colorspace not 601.
[mlt] / src / modules / avformat / filter_avcolour_space.c
1 /*
2  * filter_avcolour_space.c -- Colour space filter
3  * Copyright (C) 2004-2014 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
24 #include <framework/mlt_profile.h>
25 #include <framework/mlt_producer.h>
26
27 // ffmpeg Header files
28 #include <libavformat/avformat.h>
29 #include <libswscale/swscale.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #if 0 // This test might come in handy elsewhere someday.
35 static int is_big_endian( )
36 {
37         union { int i; char c[ 4 ]; } big_endian_test;
38         big_endian_test.i = 1;
39
40         return big_endian_test.c[ 0 ] != 1;
41 }
42 #endif
43
44 static int convert_mlt_to_av_cs( mlt_image_format format )
45 {
46         int value = 0;
47
48         switch( format )
49         {
50                 case mlt_image_rgb24:
51                         value = PIX_FMT_RGB24;
52                         break;
53                 case mlt_image_rgb24a:
54                 case mlt_image_opengl:
55                         value = PIX_FMT_RGBA;
56                         break;
57                 case mlt_image_yuv422:
58                         value = PIX_FMT_YUYV422;
59                         break;
60                 case mlt_image_yuv420p:
61                         value = PIX_FMT_YUV420P;
62                         break;
63                 default:
64                         mlt_log_error( NULL, "[filter avcolor_space] Invalid format %s\n",
65                                 mlt_image_format_name( format ) );
66                         break;
67         }
68
69         return value;
70 }
71
72 static int set_luma_transfer( struct SwsContext *context, int src_colorspace, int dst_colorspace, int full_range )
73 {
74         const int *src_coefficients = sws_getCoefficients( SWS_CS_DEFAULT );
75         const int *dst_coefficients = sws_getCoefficients( SWS_CS_DEFAULT );
76         int brightness = 0;
77         int contrast = 1 << 16;
78         int saturation = 1  << 16;
79
80         switch ( src_colorspace )
81         {
82         case 170:
83         case 470:
84         case 601:
85         case 624:
86                 src_coefficients = sws_getCoefficients( SWS_CS_ITU601 );
87                 break;
88         case 240:
89                 src_coefficients = sws_getCoefficients( SWS_CS_SMPTE240M );
90                 break;
91         case 709:
92                 src_coefficients = sws_getCoefficients( SWS_CS_ITU709 );
93                 break;
94         default:
95                 break;
96         }
97         switch ( dst_colorspace )
98         {
99         case 170:
100         case 470:
101         case 601:
102         case 624:
103                 src_coefficients = sws_getCoefficients( SWS_CS_ITU601 );
104                 break;
105         case 240:
106                 src_coefficients = sws_getCoefficients( SWS_CS_SMPTE240M );
107                 break;
108         case 709:
109                 src_coefficients = sws_getCoefficients( SWS_CS_ITU709 );
110                 break;
111         default:
112                 break;
113         }
114         return sws_setColorspaceDetails( context, src_coefficients, full_range, dst_coefficients, full_range,
115                 brightness, contrast, saturation );
116 }
117
118 // returns set_lumage_transfer result
119 static int av_convert_image( uint8_t *out, uint8_t *in, int out_fmt, int in_fmt,
120         int width, int height, int src_colorspace, int dst_colorspace, int use_full_range )
121 {
122         AVPicture input;
123         AVPicture output;
124         int flags = SWS_BICUBIC | SWS_ACCURATE_RND;
125         int error = -1;
126
127         if ( out_fmt == PIX_FMT_YUYV422 )
128                 flags |= SWS_FULL_CHR_H_INP;
129         else
130                 flags |= SWS_FULL_CHR_H_INT;
131 #ifdef USE_MMX
132         flags |= SWS_CPU_CAPS_MMX;
133 #endif
134 #ifdef USE_SSE
135         flags |= SWS_CPU_CAPS_MMX2;
136 #endif
137         if ( out_fmt == PIX_FMT_YUV420P && use_full_range )
138                 out_fmt = PIX_FMT_YUVJ420P;
139
140         avpicture_fill( &input, in, in_fmt, width, height );
141         avpicture_fill( &output, out, out_fmt, width, height );
142         struct SwsContext *context = sws_getContext( width, height, in_fmt,
143                 width, height, out_fmt, flags, NULL, NULL, NULL);
144         if ( context )
145         {
146                 // libswscale wants the RGB colorspace to be SWS_CS_DEFAULT, which is = SWS_CS_ITU601.
147                 if ( out_fmt == PIX_FMT_RGB24 || out_fmt == PIX_FMT_RGBA )
148                         dst_colorspace = 601;
149                 error = set_luma_transfer( context, src_colorspace, dst_colorspace, use_full_range );
150                 sws_scale( context, (const uint8_t* const*) input.data, input.linesize, 0, height,
151                         output.data, output.linesize);
152                 sws_freeContext( context );
153         }
154         return error;
155 }
156
157 /** Do it :-).
158 */
159
160 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
161 {
162         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
163         int width = mlt_properties_get_int( properties, "width" );
164         int height = mlt_properties_get_int( properties, "height" );
165         int error = 0;
166
167         if ( *format != output_format )
168         {
169                 mlt_profile profile = mlt_service_profile(
170                         MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) );
171                 int colorspace = mlt_properties_get_int( properties, "colorspace" );
172                 int force_full_luma = 0;
173                 
174                 mlt_log_debug( NULL, "[filter avcolor_space] %s -> %s @ %dx%d space %d->%d\n",
175                         mlt_image_format_name( *format ), mlt_image_format_name( output_format ),
176                         width, height, colorspace, profile->colorspace );
177
178                 int in_fmt = convert_mlt_to_av_cs( *format );
179                 int out_fmt = convert_mlt_to_av_cs( output_format );
180                 int size = FFMAX( avpicture_get_size( out_fmt, width, height ),
181                         mlt_image_format_size( output_format, width, height, NULL ) );
182                 uint8_t *output = mlt_pool_alloc( size );
183
184                 if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
185                 {
186                         register int len = width * height;
187                         uint8_t *alpha = mlt_pool_alloc( len );
188
189                         if ( alpha )
190                         {
191                                 // Extract the alpha mask from the RGBA image using Duff's Device
192                                 register uint8_t *s = *image + 3; // start on the alpha component
193                                 register uint8_t *d = alpha;
194                                 register int n = ( len + 7 ) / 8;
195
196                                 switch ( len % 8 )
197                                 {
198                                         case 0: do { *d++ = *s; s += 4;
199                                         case 7:          *d++ = *s; s += 4;
200                                         case 6:          *d++ = *s; s += 4;
201                                         case 5:          *d++ = *s; s += 4;
202                                         case 4:          *d++ = *s; s += 4;
203                                         case 3:          *d++ = *s; s += 4;
204                                         case 2:          *d++ = *s; s += 4;
205                                         case 1:          *d++ = *s; s += 4;
206                                                         }
207                                                         while ( --n > 0 );
208                                 }
209                                 mlt_frame_set_alpha( frame, alpha, len, mlt_pool_release );
210                         }
211                 }
212
213                 // Update the output
214                 if ( *format == mlt_image_yuv422 && mlt_properties_get( properties, "force_full_luma" )
215                      && ( output_format == mlt_image_rgb24 || output_format == mlt_image_rgb24a ) )
216                 {
217                         // By removing the frame property we only permit the luma to skip scaling once.
218                         // Thereafter, we let swscale scale the luma range as it pleases since it seems
219                         // we do not have control over the RGB to YUV conversion.
220                         force_full_luma = mlt_properties_get_int( properties, "force_full_luma" );
221                         mlt_properties_set( properties, "force_full_luma", NULL );
222                 }
223                 if ( !av_convert_image( output, *image, out_fmt, in_fmt, width, height,
224                                         colorspace, profile->colorspace, force_full_luma ) )
225                 {
226                         // The new colorspace is only valid if destination is YUV.
227                         if ( output_format == mlt_image_yuv422 || output_format == mlt_image_yuv420p )
228                                 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
229                 }
230                 *image = output;
231                 *format = output_format;
232                 mlt_frame_set_image( frame, output, size, mlt_pool_release );
233                 mlt_properties_set_int( properties, "format", output_format );
234
235                 if ( output_format == mlt_image_rgb24a || output_format == mlt_image_opengl )
236                 {
237                         register int len = width * height;
238                         int alpha_size = 0;
239                         uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
240                         mlt_properties_get_data( properties, "alpha", &alpha_size );
241
242                         if ( alpha && alpha_size >= len )
243                         {
244                                 // Merge the alpha mask from into the RGBA image using Duff's Device
245                                 register uint8_t *s = alpha;
246                                 register uint8_t *d = *image + 3; // start on the alpha component
247                                 register int n = ( len + 7 ) / 8;
248
249                                 switch ( len % 8 )
250                                 {
251                                         case 0: do { *d = *s++; d += 4;
252                                         case 7:          *d = *s++; d += 4;
253                                         case 6:          *d = *s++; d += 4;
254                                         case 5:          *d = *s++; d += 4;
255                                         case 4:          *d = *s++; d += 4;
256                                         case 3:          *d = *s++; d += 4;
257                                         case 2:          *d = *s++; d += 4;
258                                         case 1:          *d = *s++; d += 4;
259                                                         }
260                                                         while ( --n > 0 );
261                                 }
262                         }
263                 }
264         }
265         return error;
266 }
267
268 /* TODO: Enable this to force colorspace conversion. Cost is heavy due to RGB conversions. */
269 #if 0
270 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
271 {
272         int error = 0;
273         mlt_profile profile = (mlt_profile) mlt_frame_pop_get_image( frame );
274         mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
275         mlt_image_format format_from = *format;
276         mlt_image_format format_to = mlt_image_rgb24;
277         
278         error = mlt_frame_get_image( frame, image, format, width, height, writable );
279         
280         int frame_colorspace = mlt_properties_get_int( properties, "colorspace" );
281         
282         if ( !error && *format == mlt_image_yuv422 && profile->colorspace > 0 &&
283              frame_colorspace > 0 && frame_colorspace != profile->colorspace )
284         {
285                 mlt_log_debug( NULL, "[filter avcolor_space] colorspace %d -> %d\n",
286                         frame_colorspace, profile->colorspace );
287                 
288                 // Convert to RGB using frame's colorspace
289                 error = convert_image( frame, image, &format_from, format_to );
290
291                 // Convert to YUV using profile's colorspace
292                 if ( !error )
293                 {
294                         *image = mlt_properties_get_data( properties, "image", NULL );
295                         format_from = mlt_image_rgb24;
296                         format_to = *format;
297                         mlt_properties_set_int( properties, "colorspace", profile->colorspace );
298                         error = convert_image( frame, image, &format_from, format_to );
299                         *image = mlt_properties_get_data( properties, "image", NULL );
300                 }
301         }
302         
303         return error;
304 }
305 #endif
306
307 /** Filter processing.
308 */
309
310 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
311 {
312         // Set a default colorspace on the frame if not yet set by the producer.
313         // The producer may still change it during get_image.
314         // This way we do not have to modify each producer to set a valid colorspace.
315         mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
316         if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
317                 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
318
319         if ( !frame->convert_image )
320                 frame->convert_image = convert_image;
321
322 //      Not working yet - see comment for get_image() above.
323 //      mlt_frame_push_service( frame, mlt_service_profile( MLT_FILTER_SERVICE( filter ) ) );
324 //      mlt_frame_push_get_image( frame, get_image );
325
326         return frame;
327 }
328
329 /** Constructor for the filter.
330 */
331
332 mlt_filter filter_avcolour_space_init( void *arg )
333 {
334         // Test to see if swscale accepts the arg as resolution
335         if ( arg )
336         {
337                 int *width = (int*) arg;
338                 if ( *width > 0 )
339                 {
340                         struct SwsContext *context = sws_getContext( *width, *width, PIX_FMT_RGB32, 64, 64, PIX_FMT_RGB32, SWS_BILINEAR, NULL, NULL, NULL);
341                         if ( context )
342                                 sws_freeContext( context );
343                         else
344                                 return NULL;
345                 }
346         }
347         mlt_filter filter = mlt_filter_new( );
348         if ( filter != NULL )
349                 filter->process = filter_process;
350         return filter;
351 }
352