]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/video.c
Fix deinterlacing of packed YUV formats.
[vlc] / modules / stream_out / transcode / video.c
1 /*****************************************************************************
2  * video.c: transcoding stream output module (video)
3  *****************************************************************************
4  * Copyright (C) 2003-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10  *          Antoine Cellerier <dionoea at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "transcode.h"
32
33 #include <math.h>
34 #include <vlc_meta.h>
35 #include <vlc_spu.h>
36 #include <vlc_modules.h>
37
38 #define ENC_FRAMERATE (25 * 1000)
39 #define ENC_FRAMERATE_BASE 1000
40
41 struct decoder_owner_sys_t
42 {
43     sout_stream_sys_t *p_sys;
44 };
45
46 static int video_update_format_decoder( decoder_t *p_dec )
47 {
48     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
49     return 0;
50 }
51
52 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
53 {
54     return picture_NewFromFormat( &p_dec->fmt_out.video );
55 }
56
57 static picture_t *video_new_buffer_encoder( encoder_t *p_enc )
58 {
59     p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
60     return picture_NewFromFormat( &p_enc->fmt_in.video );
61 }
62
63 static picture_t *transcode_video_filter_buffer_new( filter_t *p_filter )
64 {
65     p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
66     return picture_NewFromFormat( &p_filter->fmt_out.video );
67 }
68
69 static void* EncoderThread( void *obj )
70 {
71     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)obj;
72     sout_stream_id_sys_t *id = p_sys->id_video;
73     picture_t *p_pic = NULL;
74     int canc = vlc_savecancel ();
75     block_t *p_block = NULL;
76
77     vlc_mutex_lock( &p_sys->lock_out );
78
79     for( ;; )
80     {
81         while( !p_sys->b_abort &&
82                (p_pic = picture_fifo_Pop( p_sys->pp_pics )) == NULL )
83             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
84
85         if( p_pic )
86         {
87             /* release lock while encoding */
88             vlc_mutex_unlock( &p_sys->lock_out );
89             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
90             picture_Release( p_pic );
91             vlc_mutex_lock( &p_sys->lock_out );
92
93             block_ChainAppend( &p_sys->p_buffers, p_block );
94         }
95
96         if( p_sys->b_abort )
97             break;
98     }
99
100     /*Encode what we have in the buffer on closing*/
101     while( (p_pic = picture_fifo_Pop( p_sys->pp_pics )) != NULL )
102     {
103         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
104         picture_Release( p_pic );
105         block_ChainAppend( &p_sys->p_buffers, p_block );
106     }
107
108     /*Now flush encoder*/
109     do {
110         p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
111         block_ChainAppend( &p_sys->p_buffers, p_block );
112     } while( p_block );
113
114     vlc_mutex_unlock( &p_sys->lock_out );
115
116     vlc_restorecancel (canc);
117
118     return NULL;
119 }
120
121 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
122 {
123     sout_stream_sys_t *p_sys = p_stream->p_sys;
124
125     /* Open decoder
126      * Initialization of decoder structures
127      */
128     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
129     id->p_decoder->fmt_out.i_extra = 0;
130     id->p_decoder->fmt_out.p_extra = 0;
131     id->p_decoder->pf_decode_video = NULL;
132     id->p_decoder->pf_get_cc = NULL;
133     id->p_decoder->pf_get_cc = 0;
134     id->p_decoder->pf_vout_format_update = video_update_format_decoder;
135     id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
136     id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
137     if( !id->p_decoder->p_owner )
138         return VLC_EGENERIC;
139
140     id->p_decoder->p_owner->p_sys = p_sys;
141     /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
142
143     id->p_decoder->p_module =
144         module_need( id->p_decoder, "decoder", "$codec", false );
145
146     if( !id->p_decoder->p_module )
147     {
148         msg_Err( p_stream, "cannot find video decoder" );
149         free( id->p_decoder->p_owner );
150         return VLC_EGENERIC;
151     }
152
153     /*
154      * Open encoder.
155      * Because some info about the decoded input will only be available
156      * once the first frame is decoded, we actually only test the availability
157      * of the encoder here.
158      */
159
160     /* Initialization of encoder format structures */
161     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
162                     id->p_decoder->fmt_out.i_codec );
163     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
164
165     /* The dimensions will be set properly later on.
166      * Just put sensible values so we can test an encoder is available. */
167     id->p_encoder->fmt_in.video.i_width =
168         id->p_encoder->fmt_out.video.i_width
169           ? id->p_encoder->fmt_out.video.i_width
170           : id->p_decoder->fmt_in.video.i_width
171             ? id->p_decoder->fmt_in.video.i_width : 16;
172     id->p_encoder->fmt_in.video.i_height =
173         id->p_encoder->fmt_out.video.i_height
174           ? id->p_encoder->fmt_out.video.i_height
175           : id->p_decoder->fmt_in.video.i_height
176             ? id->p_decoder->fmt_in.video.i_height : 16;
177     id->p_encoder->fmt_in.video.i_visible_width =
178         id->p_encoder->fmt_out.video.i_visible_width
179           ? id->p_encoder->fmt_out.video.i_visible_width
180           : id->p_decoder->fmt_in.video.i_visible_width
181             ? id->p_decoder->fmt_in.video.i_visible_width : id->p_encoder->fmt_in.video.i_width;
182     id->p_encoder->fmt_in.video.i_visible_height =
183         id->p_encoder->fmt_out.video.i_visible_height
184           ? id->p_encoder->fmt_out.video.i_visible_height
185           : id->p_decoder->fmt_in.video.i_visible_height
186             ? id->p_decoder->fmt_in.video.i_visible_height : id->p_encoder->fmt_in.video.i_height;
187
188     id->p_encoder->i_threads = p_sys->i_threads;
189     id->p_encoder->p_cfg = p_sys->p_video_cfg;
190
191     id->p_encoder->p_module =
192         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
193     if( !id->p_encoder->p_module )
194     {
195         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s). Take a look few lines earlier to see possible reason.",
196                  p_sys->psz_venc ? p_sys->psz_venc : "any",
197                  (char *)&p_sys->i_vcodec );
198         module_unneed( id->p_decoder, id->p_decoder->p_module );
199         id->p_decoder->p_module = 0;
200         free( id->p_decoder->p_owner );
201         return VLC_EGENERIC;
202     }
203
204     /* Close the encoder.
205      * We'll open it only when we have the first frame. */
206     module_unneed( id->p_encoder, id->p_encoder->p_module );
207     if( id->p_encoder->fmt_out.p_extra )
208     {
209         free( id->p_encoder->fmt_out.p_extra );
210         id->p_encoder->fmt_out.p_extra = NULL;
211         id->p_encoder->fmt_out.i_extra = 0;
212     }
213     id->p_encoder->p_module = NULL;
214
215     if( p_sys->i_threads <= 0 )
216         return VLC_SUCCESS;
217
218     int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
219                        VLC_THREAD_PRIORITY_VIDEO;
220     p_sys->id_video = id;
221     p_sys->pp_pics = picture_fifo_New();
222     if( p_sys->pp_pics == NULL )
223     {
224         msg_Err( p_stream, "cannot create picture fifo" );
225         module_unneed( id->p_decoder, id->p_decoder->p_module );
226         id->p_decoder->p_module = NULL;
227         free( id->p_decoder->p_owner );
228         return VLC_ENOMEM;
229     }
230     vlc_mutex_init( &p_sys->lock_out );
231     vlc_cond_init( &p_sys->cond );
232     p_sys->p_buffers = NULL;
233     p_sys->b_abort = false;
234     if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
235     {
236         msg_Err( p_stream, "cannot spawn encoder thread" );
237         vlc_mutex_destroy( &p_sys->lock_out );
238         vlc_cond_destroy( &p_sys->cond );
239         picture_fifo_Delete( p_sys->pp_pics );
240         module_unneed( id->p_decoder, id->p_decoder->p_module );
241         id->p_decoder->p_module = NULL;
242         free( id->p_decoder->p_owner );
243         return VLC_EGENERIC;
244     }
245     return VLC_SUCCESS;
246 }
247
248 static filter_t *AppendDeinterlaceFilter( sout_stream_t *p_stream,
249                                           filter_chain_t *p_chain,
250                                           const char *psz_name,
251                                           config_chain_t *p_cfg,
252                                           const es_format_t *p_fmt_in,
253                                           const es_format_t *p_fmt_out )
254 {
255     filter_t *p_filter =
256         filter_chain_AppendFilter( p_chain, psz_name, p_cfg, p_fmt_in, p_fmt_out );
257     if( p_filter )
258         return p_filter;
259
260     msg_Dbg( p_stream, "deinterlace init failed, trying colorspace conversion" );
261
262     /* Most likely the filter failed due to wrong (e.g. non-planar) input format.
263      * Try converting to a different color space and try again.
264      *
265      * We use the recommended fallback formats in order, to get a conversion that's
266      * as cheap (both CPU-wise and quality-wise) as possible.
267      */
268     for( const vlc_fourcc_t *fourcc = vlc_fourcc_GetYUVFallback( p_fmt_out->video.i_chroma );
269          *fourcc != 0;
270          ++fourcc )
271     {
272         msg_Dbg( p_stream, "trying deinterlace through colorspace conversion to %4.4s", (char *)fourcc );
273
274         es_format_t p_new_fmt;
275         es_format_Copy( &p_new_fmt, p_fmt_in );
276         p_new_fmt.video.i_chroma = *fourcc;  // XXX: is this enough?
277
278         filter_chain_Reset( p_chain, NULL, NULL ); 
279
280         p_filter = filter_chain_AppendFilter( p_chain,
281                                               NULL, NULL,
282                                               p_fmt_in,
283                                               &p_new_fmt );
284         if( !p_filter )
285             continue;
286                    
287         p_filter = filter_chain_AppendFilter( p_chain,
288                                               psz_name,
289                                               p_cfg,
290                                               &p_new_fmt,
291                                               &p_new_fmt );
292         if( p_filter )
293         {
294             msg_Dbg( p_stream, "deinterlace through colorspace conversion to %4.4s succeeded", (char *)fourcc );
295             return p_filter;
296         }
297     }
298
299     msg_Err( p_stream, "deinterlace init failed, even with colorspace conversion" );
300     return NULL;
301 }
302  
303 static void transcode_video_filter_init( sout_stream_t *p_stream,
304                                          sout_stream_id_sys_t *id )
305 {
306     filter_owner_t owner = {
307         .sys = p_stream->p_sys,
308         .video = {
309             .buffer_new = transcode_video_filter_buffer_new,
310         },
311     };
312     es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
313
314     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
315     id->p_f_chain = filter_chain_NewVideo( p_stream, false, &owner );
316     filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
317
318     /* Deinterlace */
319     if( p_stream->p_sys->b_deinterlace )
320     {
321         filter_t *p_filter =
322             AppendDeinterlaceFilter( p_stream,
323                                      id->p_f_chain,
324                                      p_stream->p_sys->psz_deinterlace,
325                                      p_stream->p_sys->p_deinterlace_cfg,
326                                      p_fmt_out,
327                                      p_fmt_out );
328         if( !p_filter )
329         {
330             msg_Err( p_stream, "deinterlace init failed, even with colorspace conversion" );
331             return;
332         } 
333
334         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
335     }
336     if( p_stream->p_sys->b_master_sync )
337     {
338         filter_chain_AppendFilter( id->p_f_chain,
339                                    "fps",
340                                    NULL,
341                                    p_fmt_out,
342                                    &id->p_encoder->fmt_in );
343
344         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
345     }
346
347     /* Check that we have visible_width/height*/
348     if( !p_fmt_out->video.i_visible_height )
349         p_fmt_out->video.i_visible_height = p_fmt_out->video.i_height;
350     if( !p_fmt_out->video.i_visible_width )
351         p_fmt_out->video.i_visible_width = p_fmt_out->video.i_width;
352
353     if( p_stream->p_sys->psz_vf2 )
354     {
355         id->p_uf_chain = filter_chain_NewVideo( p_stream, true, &owner );
356         filter_chain_Reset( id->p_uf_chain, p_fmt_out,
357                             &id->p_encoder->fmt_in );
358         if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
359         {
360             filter_chain_AppendFilter( id->p_uf_chain,
361                                    NULL, NULL,
362                                    p_fmt_out,
363                                    &id->p_encoder->fmt_in );
364         }
365         filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
366         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
367         es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
368         id->p_encoder->fmt_out.video.i_width =
369             id->p_encoder->fmt_in.video.i_width;
370         id->p_encoder->fmt_out.video.i_height =
371             id->p_encoder->fmt_in.video.i_height;
372         id->p_encoder->fmt_out.video.i_sar_num =
373             id->p_encoder->fmt_in.video.i_sar_num;
374         id->p_encoder->fmt_out.video.i_sar_den =
375             id->p_encoder->fmt_in.video.i_sar_den;
376     }
377
378 }
379
380 /* Take care of the scaling and chroma conversions. */
381 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
382 {
383     const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
384     if( id->p_f_chain )
385         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
386
387     if( id->p_uf_chain )
388         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
389
390     if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
391         ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
392         ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
393     {
394         filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
395                                    NULL, NULL,
396                                    p_fmt_out,
397                                    &id->p_encoder->fmt_in );
398     }
399 }
400
401 static void transcode_video_encoder_init( sout_stream_t *p_stream,
402                                           sout_stream_id_sys_t *id )
403 {
404     sout_stream_sys_t *p_sys = p_stream->p_sys;
405
406     const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
407     if( id->p_f_chain ) {
408         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
409     }
410     if( id->p_uf_chain ) {
411         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
412     }
413
414     /* Calculate scaling
415      * width/height of source */
416     int i_src_visible_width = p_fmt_out->video.i_visible_width;
417     int i_src_visible_height = p_fmt_out->video.i_visible_height;
418
419     if (i_src_visible_width == 0)
420         i_src_visible_width = p_fmt_out->video.i_width;
421     if (i_src_visible_height == 0)
422         i_src_visible_height = p_fmt_out->video.i_height;
423
424
425     /* with/height scaling */
426     float f_scale_width = 1;
427     float f_scale_height = 1;
428
429     /* aspect ratio */
430     float f_aspect = (double)p_fmt_out->video.i_sar_num *
431                      p_fmt_out->video.i_width /
432                      p_fmt_out->video.i_sar_den /
433                      p_fmt_out->video.i_height;
434
435     msg_Dbg( p_stream, "decoder aspect is %f:1", (double) f_aspect );
436
437     /* Change f_aspect from source frame to source pixel */
438     f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
439     msg_Dbg( p_stream, "source pixel aspect is %f:1", (double) f_aspect );
440
441     /* Calculate scaling factor for specified parameters */
442     if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
443         id->p_encoder->fmt_out.video.i_visible_height <= 0 && p_sys->f_scale )
444     {
445         /* Global scaling. Make sure width will remain a factor of 16 */
446         float f_real_scale;
447         int  i_new_height;
448         int i_new_width = i_src_visible_width * p_sys->f_scale;
449
450         if( i_new_width % 16 <= 7 && i_new_width >= 16 )
451             i_new_width -= i_new_width % 16;
452         else
453             i_new_width += 16 - i_new_width % 16;
454
455         f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
456
457         i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
458
459         f_scale_width = f_real_scale;
460         f_scale_height = (float) i_new_height / (float) i_src_visible_height;
461     }
462     else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
463              id->p_encoder->fmt_out.video.i_visible_height <= 0 )
464     {
465         /* Only width specified */
466         f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
467         f_scale_height = f_scale_width;
468     }
469     else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
470              id->p_encoder->fmt_out.video.i_visible_height > 0 )
471     {
472          /* Only height specified */
473          f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
474          f_scale_width = f_scale_height;
475      }
476      else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
477               id->p_encoder->fmt_out.video.i_visible_height > 0 )
478      {
479          /* Width and height specified */
480          f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
481          f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
482      }
483
484      /* check maxwidth and maxheight */
485      if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
486                                                      i_src_visible_width )
487      {
488          f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
489      }
490
491      if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
492                                                        i_src_visible_height )
493      {
494          f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
495      }
496
497
498      /* Change aspect ratio from source pixel to scaled pixel */
499      f_aspect = f_aspect * f_scale_height / f_scale_width;
500      msg_Dbg( p_stream, "scaled pixel aspect is %f:1", (double) f_aspect );
501
502      /* f_scale_width and f_scale_height are now final */
503      /* Calculate width, height from scaling
504       * Make sure its multiple of 2
505       */
506      /* width/height of output stream */
507      int i_dst_visible_width =  2 * lroundf(f_scale_width*i_src_visible_width/2);
508      int i_dst_visible_height = 2 * lroundf(f_scale_height*i_src_visible_height/2);
509      int i_dst_width =  2 * lroundf(f_scale_width*p_fmt_out->video.i_width/2);
510      int i_dst_height = 2 * lroundf(f_scale_height*p_fmt_out->video.i_height/2);
511
512      /* Change aspect ratio from scaled pixel to output frame */
513      f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
514
515      /* Store calculated values */
516      id->p_encoder->fmt_out.video.i_width = i_dst_width;
517      id->p_encoder->fmt_out.video.i_visible_width = i_dst_visible_width;
518      id->p_encoder->fmt_out.video.i_height = i_dst_height;
519      id->p_encoder->fmt_out.video.i_visible_height = i_dst_visible_height;
520
521      id->p_encoder->fmt_in.video.i_width = i_dst_width;
522      id->p_encoder->fmt_in.video.i_visible_width = i_dst_visible_width;
523      id->p_encoder->fmt_in.video.i_height = i_dst_height;
524      id->p_encoder->fmt_in.video.i_visible_height = i_dst_visible_height;
525
526      msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
527          i_src_visible_width, i_src_visible_height,
528          i_dst_visible_width, i_dst_visible_height
529      );
530
531     /* Handle frame rate conversion */
532     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
533         !id->p_encoder->fmt_out.video.i_frame_rate_base )
534     {
535         if( p_fmt_out->video.i_frame_rate &&
536             p_fmt_out->video.i_frame_rate_base )
537         {
538             id->p_encoder->fmt_out.video.i_frame_rate =
539                 p_fmt_out->video.i_frame_rate;
540             id->p_encoder->fmt_out.video.i_frame_rate_base =
541                 p_fmt_out->video.i_frame_rate_base;
542         }
543         else
544         {
545             /* Pick a sensible default value */
546             id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
547             id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
548         }
549     }
550
551     id->p_encoder->fmt_in.video.orientation =
552         id->p_encoder->fmt_out.video.orientation =
553         id->p_decoder->fmt_in.video.orientation;
554
555     id->p_encoder->fmt_in.video.i_frame_rate =
556         id->p_encoder->fmt_out.video.i_frame_rate;
557     id->p_encoder->fmt_in.video.i_frame_rate_base =
558         id->p_encoder->fmt_out.video.i_frame_rate_base;
559
560     vlc_ureduce( &id->p_encoder->fmt_in.video.i_frame_rate,
561         &id->p_encoder->fmt_in.video.i_frame_rate_base,
562         id->p_encoder->fmt_in.video.i_frame_rate,
563         id->p_encoder->fmt_in.video.i_frame_rate_base,
564         0 );
565      msg_Dbg( p_stream, "source fps %d/%d, destination %d/%d",
566         id->p_decoder->fmt_out.video.i_frame_rate,
567         id->p_decoder->fmt_out.video.i_frame_rate_base,
568         id->p_encoder->fmt_in.video.i_frame_rate,
569         id->p_encoder->fmt_in.video.i_frame_rate_base );
570
571
572     /* Check whether a particular aspect ratio was requested */
573     if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
574         id->p_encoder->fmt_out.video.i_sar_den <= 0 )
575     {
576         vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
577                      &id->p_encoder->fmt_out.video.i_sar_den,
578                      (uint64_t)p_fmt_out->video.i_sar_num * i_src_visible_width  * i_dst_visible_height,
579                      (uint64_t)p_fmt_out->video.i_sar_den * i_src_visible_height * i_dst_visible_width,
580                      0 );
581     }
582     else
583     {
584         vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
585                      &id->p_encoder->fmt_out.video.i_sar_den,
586                      id->p_encoder->fmt_out.video.i_sar_num,
587                      id->p_encoder->fmt_out.video.i_sar_den,
588                      0 );
589     }
590
591     id->p_encoder->fmt_in.video.i_sar_num =
592         id->p_encoder->fmt_out.video.i_sar_num;
593     id->p_encoder->fmt_in.video.i_sar_den =
594         id->p_encoder->fmt_out.video.i_sar_den;
595
596     msg_Dbg( p_stream, "encoder aspect is %i:%i",
597              id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
598              id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
599
600 }
601
602 static int transcode_video_encoder_open( sout_stream_t *p_stream,
603                                          sout_stream_id_sys_t *id )
604 {
605     sout_stream_sys_t *p_sys = p_stream->p_sys;
606
607
608     msg_Dbg( p_stream, "destination (after video filters) %ix%i",
609              id->p_encoder->fmt_in.video.i_width,
610              id->p_encoder->fmt_in.video.i_height );
611
612     id->p_encoder->p_module =
613         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
614     if( !id->p_encoder->p_module )
615     {
616         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
617                  p_sys->psz_venc ? p_sys->psz_venc : "any",
618                  (char *)&p_sys->i_vcodec );
619         return VLC_EGENERIC;
620     }
621
622     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
623
624     /*  */
625     id->p_encoder->fmt_out.i_codec =
626         vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
627
628     id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
629     if( !id->id )
630     {
631         msg_Err( p_stream, "cannot add this stream" );
632         return VLC_EGENERIC;
633     }
634
635     return VLC_SUCCESS;
636 }
637
638 void transcode_video_close( sout_stream_t *p_stream,
639                                    sout_stream_id_sys_t *id )
640 {
641     if( p_stream->p_sys->i_threads >= 1 && !p_stream->p_sys->b_abort )
642     {
643         vlc_mutex_lock( &p_stream->p_sys->lock_out );
644         p_stream->p_sys->b_abort = true;
645         vlc_cond_signal( &p_stream->p_sys->cond );
646         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
647
648         vlc_join( p_stream->p_sys->thread, NULL );
649
650         picture_fifo_Delete( p_stream->p_sys->pp_pics );
651         block_ChainRelease( p_stream->p_sys->p_buffers );
652     }
653
654     vlc_mutex_destroy( &p_stream->p_sys->lock_out );
655     vlc_cond_destroy( &p_stream->p_sys->cond );
656
657     /* Close decoder */
658     if( id->p_decoder->p_module )
659         module_unneed( id->p_decoder, id->p_decoder->p_module );
660     if( id->p_decoder->p_description )
661         vlc_meta_Delete( id->p_decoder->p_description );
662
663     free( id->p_decoder->p_owner );
664
665     /* Close encoder */
666     if( id->p_encoder->p_module )
667         module_unneed( id->p_encoder, id->p_encoder->p_module );
668
669     /* Close filters */
670     if( id->p_f_chain )
671         filter_chain_Delete( id->p_f_chain );
672     if( id->p_uf_chain )
673         filter_chain_Delete( id->p_uf_chain );
674 }
675
676 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
677 {
678     sout_stream_sys_t *p_sys = p_stream->p_sys;
679     picture_t *p_pic2 = NULL;
680
681     /*
682      * Encoding
683      */
684     /* Check if we have a subpicture to overlay */
685     if( p_sys->p_spu )
686     {
687         video_format_t fmt = id->p_encoder->fmt_in.video;
688         if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
689         {
690             fmt.i_visible_width  = fmt.i_width;
691             fmt.i_visible_height = fmt.i_height;
692             fmt.i_x_offset       = 0;
693             fmt.i_y_offset       = 0;
694         }
695
696         subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt,
697                                              &id->p_decoder->fmt_out.video,
698                                              p_pic->date, p_pic->date, false );
699
700         /* Overlay subpicture */
701         if( p_subpic )
702         {
703             if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
704             {
705                 /* We can't modify the picture, we need to duplicate it,
706                  * in this point the picture is already p_encoder->fmt.in format*/
707                 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
708                 if( likely( p_tmp ) )
709                 {
710                     picture_Copy( p_tmp, p_pic );
711                     picture_Release( p_pic );
712                     p_pic = p_tmp;
713                 }
714             }
715             if( unlikely( !p_sys->p_spu_blend ) )
716                 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
717             if( likely( p_sys->p_spu_blend ) )
718                 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
719             subpicture_Delete( p_subpic );
720         }
721     }
722
723     if( p_sys->i_threads == 0 )
724     {
725         block_t *p_block;
726
727         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
728         block_ChainAppend( out, p_block );
729     }
730
731     if( p_sys->i_threads )
732     {
733         vlc_mutex_lock( &p_sys->lock_out );
734         picture_fifo_Push( p_sys->pp_pics, p_pic );
735         vlc_cond_signal( &p_sys->cond );
736         vlc_mutex_unlock( &p_sys->lock_out );
737     }
738
739     if( p_sys->i_threads && p_pic2 )
740         picture_Release( p_pic2 );
741     else if ( p_sys->i_threads == 0 )
742         picture_Release( p_pic );
743 }
744
745 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
746                                     block_t *in, block_t **out )
747 {
748     sout_stream_sys_t *p_sys = p_stream->p_sys;
749     picture_t *p_pic = NULL;
750     *out = NULL;
751
752     if( unlikely( in == NULL ) )
753     {
754         if( p_sys->i_threads == 0 )
755         {
756             block_t *p_block;
757             do {
758                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
759                 block_ChainAppend( out, p_block );
760             } while( p_block );
761         }
762         else
763         {
764             msg_Dbg( p_stream, "Flushing thread and waiting that");
765             vlc_mutex_lock( &p_stream->p_sys->lock_out );
766             p_stream->p_sys->b_abort = true;
767             vlc_cond_signal( &p_stream->p_sys->cond );
768             vlc_mutex_unlock( &p_stream->p_sys->lock_out );
769
770             vlc_join( p_stream->p_sys->thread, NULL );
771             vlc_mutex_lock( &p_sys->lock_out );
772             *out = p_sys->p_buffers;
773             p_sys->p_buffers = NULL;
774             vlc_mutex_unlock( &p_sys->lock_out );
775
776             msg_Dbg( p_stream, "Flushing done");
777         }
778         return VLC_SUCCESS;
779     }
780
781
782     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
783     {
784
785         if( unlikely (
786              id->p_encoder->p_module &&
787              !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
788             )
789           )
790         {
791             msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
792                         id->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
793                         id->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
794                     );
795             /* Close filters */
796             if( id->p_f_chain )
797                 filter_chain_Delete( id->p_f_chain );
798             id->p_f_chain = NULL;
799             if( id->p_uf_chain )
800                 filter_chain_Delete( id->p_uf_chain );
801             id->p_uf_chain = NULL;
802
803             /* Reinitialize filters */
804             id->p_encoder->fmt_out.video.i_visible_width  = p_sys->i_width & ~1;
805             id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
806             id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
807
808             transcode_video_filter_init( p_stream, id );
809             transcode_video_encoder_init( p_stream, id );
810             conversion_video_filter_append( id );
811             memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
812         }
813
814
815         if( unlikely( !id->p_encoder->p_module ) )
816         {
817             if( id->p_f_chain )
818                 filter_chain_Delete( id->p_f_chain );
819             if( id->p_uf_chain )
820                 filter_chain_Delete( id->p_uf_chain );
821             id->p_f_chain = id->p_uf_chain = NULL;
822
823             transcode_video_filter_init( p_stream, id );
824             transcode_video_encoder_init( p_stream, id );
825             conversion_video_filter_append( id );
826             memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
827
828             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
829             {
830                 picture_Release( p_pic );
831                 transcode_video_close( p_stream, id );
832                 id->b_transcode = false;
833                 return VLC_EGENERIC;
834             }
835         }
836
837         /* Run the filter and output chains; first with the picture,
838          * and then with NULL as many times as we need until they
839          * stop outputting frames.
840          */
841         for ( ;; ) {
842             picture_t *p_filtered_pic = p_pic;
843
844             /* Run filter chain */
845             if( id->p_f_chain )
846                 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
847             if( !p_filtered_pic )
848                 break;
849
850             for ( ;; ) {
851                 picture_t *p_user_filtered_pic = p_filtered_pic;
852
853                 /* Run user specified filter chain */
854                 if( id->p_uf_chain )
855                     p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
856                 if( !p_user_filtered_pic )
857                     break;
858
859                 OutputFrame( p_stream, p_user_filtered_pic, id, out );
860
861                 p_filtered_pic = NULL;
862             }
863
864             p_pic = NULL;
865         }
866     }
867
868     if( p_sys->i_threads >= 1 )
869     {
870         /* Pick up any return data the encoder thread wants to output. */
871         vlc_mutex_lock( &p_sys->lock_out );
872         *out = p_sys->p_buffers;
873         p_sys->p_buffers = NULL;
874         vlc_mutex_unlock( &p_sys->lock_out );
875     }
876
877     return VLC_SUCCESS;
878 }
879
880 bool transcode_video_add( sout_stream_t *p_stream, const es_format_t *p_fmt,
881                                 sout_stream_id_sys_t *id )
882 {
883     sout_stream_sys_t *p_sys = p_stream->p_sys;
884
885     msg_Dbg( p_stream,
886              "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
887              (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
888
889     /* Complete destination format */
890     id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
891     id->p_encoder->fmt_out.video.i_visible_width  = p_sys->i_width & ~1;
892     id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
893     id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
894
895     /* Build decoder -> filter -> encoder chain */
896     if( transcode_video_new( p_stream, id ) )
897     {
898         msg_Err( p_stream, "cannot create video chain" );
899         return false;
900     }
901
902     /* Stream will be added later on because we don't know
903      * all the characteristics of the decoded stream yet */
904     id->b_transcode = true;
905
906     if( p_sys->fps_num )
907     {
908         id->p_encoder->fmt_in.video.i_frame_rate = id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
909         id->p_encoder->fmt_in.video.i_frame_rate_base = id->p_encoder->fmt_out.video.i_frame_rate_base = (p_sys->fps_den ? p_sys->fps_den : 1);
910     }
911
912     return true;
913 }
914