]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/video.c
c0fc4800104435dd558a537cada77de830f65375
[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 void transcode_video_filter_init( sout_stream_t *p_stream,
249                                          sout_stream_id_sys_t *id )
250 {
251     filter_owner_t owner = {
252         .sys = p_stream->p_sys,
253         .video = {
254             .buffer_new = transcode_video_filter_buffer_new,
255         },
256     };
257     es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
258
259     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
260     id->p_f_chain = filter_chain_NewVideo( p_stream, false, &owner );
261     filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
262
263     /* Deinterlace */
264     if( p_stream->p_sys->b_deinterlace )
265     {
266         filter_chain_AppendFilter( id->p_f_chain,
267                                    p_stream->p_sys->psz_deinterlace,
268                                    p_stream->p_sys->p_deinterlace_cfg,
269                                    &id->p_decoder->fmt_out,
270                                    &id->p_decoder->fmt_out );
271
272         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
273     }
274     if( p_stream->p_sys->b_master_sync )
275     {
276         filter_chain_AppendFilter( id->p_f_chain,
277                                    "fps",
278                                    NULL,
279                                    p_fmt_out,
280                                    &id->p_encoder->fmt_in );
281
282         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
283     }
284
285     /* Check that we have visible_width/height*/
286     if( !p_fmt_out->video.i_visible_height )
287         p_fmt_out->video.i_visible_height = p_fmt_out->video.i_height;
288     if( !p_fmt_out->video.i_visible_width )
289         p_fmt_out->video.i_visible_width = p_fmt_out->video.i_width;
290
291     if( p_stream->p_sys->psz_vf2 )
292     {
293         id->p_uf_chain = filter_chain_NewVideo( p_stream, true, &owner );
294         filter_chain_Reset( id->p_uf_chain, p_fmt_out,
295                             &id->p_encoder->fmt_in );
296         if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
297         {
298             filter_chain_AppendFilter( id->p_uf_chain,
299                                    NULL, NULL,
300                                    p_fmt_out,
301                                    &id->p_encoder->fmt_in );
302         }
303         filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
304         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
305         es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
306         id->p_encoder->fmt_out.video.i_width =
307             id->p_encoder->fmt_in.video.i_width;
308         id->p_encoder->fmt_out.video.i_height =
309             id->p_encoder->fmt_in.video.i_height;
310         id->p_encoder->fmt_out.video.i_sar_num =
311             id->p_encoder->fmt_in.video.i_sar_num;
312         id->p_encoder->fmt_out.video.i_sar_den =
313             id->p_encoder->fmt_in.video.i_sar_den;
314     }
315
316 }
317
318 /* Take care of the scaling and chroma conversions. */
319 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
320 {
321     const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
322     if( id->p_f_chain )
323         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
324
325     if( id->p_uf_chain )
326         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
327
328     if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
329         ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
330         ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
331     {
332         filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
333                                    NULL, NULL,
334                                    p_fmt_out,
335                                    &id->p_encoder->fmt_in );
336     }
337 }
338
339 static void transcode_video_encoder_init( sout_stream_t *p_stream,
340                                           sout_stream_id_sys_t *id )
341 {
342     sout_stream_sys_t *p_sys = p_stream->p_sys;
343
344     const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
345     if( id->p_f_chain ) {
346         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
347     }
348     if( id->p_uf_chain ) {
349         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
350     }
351
352     /* Calculate scaling
353      * width/height of source */
354     int i_src_visible_width = p_fmt_out->video.i_visible_width;
355     int i_src_visible_height = p_fmt_out->video.i_visible_height;
356
357     if (i_src_visible_width == 0)
358         i_src_visible_width = p_fmt_out->video.i_width;
359     if (i_src_visible_height == 0)
360         i_src_visible_height = p_fmt_out->video.i_height;
361
362
363     /* with/height scaling */
364     float f_scale_width = 1;
365     float f_scale_height = 1;
366
367     /* aspect ratio */
368     float f_aspect = (double)p_fmt_out->video.i_sar_num *
369                      p_fmt_out->video.i_width /
370                      p_fmt_out->video.i_sar_den /
371                      p_fmt_out->video.i_height;
372
373     msg_Dbg( p_stream, "decoder aspect is %f:1", (double) f_aspect );
374
375     /* Change f_aspect from source frame to source pixel */
376     f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
377     msg_Dbg( p_stream, "source pixel aspect is %f:1", (double) f_aspect );
378
379     /* Calculate scaling factor for specified parameters */
380     if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
381         id->p_encoder->fmt_out.video.i_visible_height <= 0 && p_sys->f_scale )
382     {
383         /* Global scaling. Make sure width will remain a factor of 16 */
384         float f_real_scale;
385         int  i_new_height;
386         int i_new_width = i_src_visible_width * p_sys->f_scale;
387
388         if( i_new_width % 16 <= 7 && i_new_width >= 16 )
389             i_new_width -= i_new_width % 16;
390         else
391             i_new_width += 16 - i_new_width % 16;
392
393         f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
394
395         i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
396
397         f_scale_width = f_real_scale;
398         f_scale_height = (float) i_new_height / (float) i_src_visible_height;
399     }
400     else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
401              id->p_encoder->fmt_out.video.i_visible_height <= 0 )
402     {
403         /* Only width specified */
404         f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
405         f_scale_height = f_scale_width;
406     }
407     else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
408              id->p_encoder->fmt_out.video.i_visible_height > 0 )
409     {
410          /* Only height specified */
411          f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
412          f_scale_width = f_scale_height;
413      }
414      else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
415               id->p_encoder->fmt_out.video.i_visible_height > 0 )
416      {
417          /* Width and height specified */
418          f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
419          f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
420      }
421
422      /* check maxwidth and maxheight */
423      if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
424                                                      i_src_visible_width )
425      {
426          f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
427      }
428
429      if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
430                                                        i_src_visible_height )
431      {
432          f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
433      }
434
435
436      /* Change aspect ratio from source pixel to scaled pixel */
437      f_aspect = f_aspect * f_scale_height / f_scale_width;
438      msg_Dbg( p_stream, "scaled pixel aspect is %f:1", (double) f_aspect );
439
440      /* f_scale_width and f_scale_height are now final */
441      /* Calculate width, height from scaling
442       * Make sure its multiple of 2
443       */
444      /* width/height of output stream */
445      int i_dst_visible_width =  2 * lroundf(f_scale_width*i_src_visible_width/2);
446      int i_dst_visible_height = 2 * lroundf(f_scale_height*i_src_visible_height/2);
447      int i_dst_width =  2 * lroundf(f_scale_width*p_fmt_out->video.i_width/2);
448      int i_dst_height = 2 * lroundf(f_scale_height*p_fmt_out->video.i_height/2);
449
450      /* Change aspect ratio from scaled pixel to output frame */
451      f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
452
453      /* Store calculated values */
454      id->p_encoder->fmt_out.video.i_width = i_dst_width;
455      id->p_encoder->fmt_out.video.i_visible_width = i_dst_visible_width;
456      id->p_encoder->fmt_out.video.i_height = i_dst_height;
457      id->p_encoder->fmt_out.video.i_visible_height = i_dst_visible_height;
458
459      id->p_encoder->fmt_in.video.i_width = i_dst_width;
460      id->p_encoder->fmt_in.video.i_visible_width = i_dst_visible_width;
461      id->p_encoder->fmt_in.video.i_height = i_dst_height;
462      id->p_encoder->fmt_in.video.i_visible_height = i_dst_visible_height;
463
464      msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
465          i_src_visible_width, i_src_visible_height,
466          i_dst_visible_width, i_dst_visible_height
467      );
468
469     /* Handle frame rate conversion */
470     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
471         !id->p_encoder->fmt_out.video.i_frame_rate_base )
472     {
473         if( p_fmt_out->video.i_frame_rate &&
474             p_fmt_out->video.i_frame_rate_base )
475         {
476             id->p_encoder->fmt_out.video.i_frame_rate =
477                 p_fmt_out->video.i_frame_rate;
478             id->p_encoder->fmt_out.video.i_frame_rate_base =
479                 p_fmt_out->video.i_frame_rate_base;
480         }
481         else
482         {
483             /* Pick a sensible default value */
484             id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
485             id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
486         }
487     }
488
489     id->p_encoder->fmt_in.video.orientation =
490         id->p_encoder->fmt_out.video.orientation =
491         id->p_decoder->fmt_in.video.orientation;
492
493     id->p_encoder->fmt_in.video.i_frame_rate =
494         id->p_encoder->fmt_out.video.i_frame_rate;
495     id->p_encoder->fmt_in.video.i_frame_rate_base =
496         id->p_encoder->fmt_out.video.i_frame_rate_base;
497
498     vlc_ureduce( &id->p_encoder->fmt_in.video.i_frame_rate,
499         &id->p_encoder->fmt_in.video.i_frame_rate_base,
500         id->p_encoder->fmt_in.video.i_frame_rate,
501         id->p_encoder->fmt_in.video.i_frame_rate_base,
502         0 );
503      msg_Dbg( p_stream, "source fps %d/%d, destination %d/%d",
504         id->p_decoder->fmt_out.video.i_frame_rate,
505         id->p_decoder->fmt_out.video.i_frame_rate_base,
506         id->p_encoder->fmt_in.video.i_frame_rate,
507         id->p_encoder->fmt_in.video.i_frame_rate_base );
508
509
510     /* Check whether a particular aspect ratio was requested */
511     if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
512         id->p_encoder->fmt_out.video.i_sar_den <= 0 )
513     {
514         vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
515                      &id->p_encoder->fmt_out.video.i_sar_den,
516                      (uint64_t)p_fmt_out->video.i_sar_num * i_src_visible_width  * i_dst_visible_height,
517                      (uint64_t)p_fmt_out->video.i_sar_den * i_src_visible_height * i_dst_visible_width,
518                      0 );
519     }
520     else
521     {
522         vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
523                      &id->p_encoder->fmt_out.video.i_sar_den,
524                      id->p_encoder->fmt_out.video.i_sar_num,
525                      id->p_encoder->fmt_out.video.i_sar_den,
526                      0 );
527     }
528
529     id->p_encoder->fmt_in.video.i_sar_num =
530         id->p_encoder->fmt_out.video.i_sar_num;
531     id->p_encoder->fmt_in.video.i_sar_den =
532         id->p_encoder->fmt_out.video.i_sar_den;
533
534     msg_Dbg( p_stream, "encoder aspect is %i:%i",
535              id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
536              id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
537
538 }
539
540 static int transcode_video_encoder_open( sout_stream_t *p_stream,
541                                          sout_stream_id_sys_t *id )
542 {
543     sout_stream_sys_t *p_sys = p_stream->p_sys;
544
545
546     msg_Dbg( p_stream, "destination (after video filters) %ix%i",
547              id->p_encoder->fmt_in.video.i_width,
548              id->p_encoder->fmt_in.video.i_height );
549
550     id->p_encoder->p_module =
551         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
552     if( !id->p_encoder->p_module )
553     {
554         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
555                  p_sys->psz_venc ? p_sys->psz_venc : "any",
556                  (char *)&p_sys->i_vcodec );
557         return VLC_EGENERIC;
558     }
559
560     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
561
562     /*  */
563     id->p_encoder->fmt_out.i_codec =
564         vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
565
566     id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
567     if( !id->id )
568     {
569         msg_Err( p_stream, "cannot add this stream" );
570         return VLC_EGENERIC;
571     }
572
573     return VLC_SUCCESS;
574 }
575
576 void transcode_video_close( sout_stream_t *p_stream,
577                                    sout_stream_id_sys_t *id )
578 {
579     if( p_stream->p_sys->i_threads >= 1 && !p_stream->p_sys->b_abort )
580     {
581         vlc_mutex_lock( &p_stream->p_sys->lock_out );
582         p_stream->p_sys->b_abort = true;
583         vlc_cond_signal( &p_stream->p_sys->cond );
584         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
585
586         vlc_join( p_stream->p_sys->thread, NULL );
587
588         picture_fifo_Delete( p_stream->p_sys->pp_pics );
589         block_ChainRelease( p_stream->p_sys->p_buffers );
590     }
591
592     vlc_mutex_destroy( &p_stream->p_sys->lock_out );
593     vlc_cond_destroy( &p_stream->p_sys->cond );
594
595     /* Close decoder */
596     if( id->p_decoder->p_module )
597         module_unneed( id->p_decoder, id->p_decoder->p_module );
598     if( id->p_decoder->p_description )
599         vlc_meta_Delete( id->p_decoder->p_description );
600
601     free( id->p_decoder->p_owner );
602
603     /* Close encoder */
604     if( id->p_encoder->p_module )
605         module_unneed( id->p_encoder, id->p_encoder->p_module );
606
607     /* Close filters */
608     if( id->p_f_chain )
609         filter_chain_Delete( id->p_f_chain );
610     if( id->p_uf_chain )
611         filter_chain_Delete( id->p_uf_chain );
612 }
613
614 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
615 {
616     sout_stream_sys_t *p_sys = p_stream->p_sys;
617     picture_t *p_pic2 = NULL;
618
619     /*
620      * Encoding
621      */
622     /* Check if we have a subpicture to overlay */
623     if( p_sys->p_spu )
624     {
625         video_format_t fmt = id->p_encoder->fmt_in.video;
626         if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
627         {
628             fmt.i_visible_width  = fmt.i_width;
629             fmt.i_visible_height = fmt.i_height;
630             fmt.i_x_offset       = 0;
631             fmt.i_y_offset       = 0;
632         }
633
634         subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt,
635                                              &id->p_decoder->fmt_out.video,
636                                              p_pic->date, p_pic->date, false );
637
638         /* Overlay subpicture */
639         if( p_subpic )
640         {
641             if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
642             {
643                 /* We can't modify the picture, we need to duplicate it,
644                  * in this point the picture is already p_encoder->fmt.in format*/
645                 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
646                 if( likely( p_tmp ) )
647                 {
648                     picture_Copy( p_tmp, p_pic );
649                     picture_Release( p_pic );
650                     p_pic = p_tmp;
651                 }
652             }
653             if( unlikely( !p_sys->p_spu_blend ) )
654                 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
655             if( likely( p_sys->p_spu_blend ) )
656                 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
657             subpicture_Delete( p_subpic );
658         }
659     }
660
661     if( p_sys->i_threads == 0 )
662     {
663         block_t *p_block;
664
665         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
666         block_ChainAppend( out, p_block );
667     }
668
669     if( p_sys->i_threads )
670     {
671         vlc_mutex_lock( &p_sys->lock_out );
672         picture_fifo_Push( p_sys->pp_pics, p_pic );
673         vlc_cond_signal( &p_sys->cond );
674         vlc_mutex_unlock( &p_sys->lock_out );
675     }
676
677     if( p_sys->i_threads && p_pic2 )
678         picture_Release( p_pic2 );
679     else if ( p_sys->i_threads == 0 )
680         picture_Release( p_pic );
681 }
682
683 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
684                                     block_t *in, block_t **out )
685 {
686     sout_stream_sys_t *p_sys = p_stream->p_sys;
687     picture_t *p_pic = NULL;
688     *out = NULL;
689
690     if( unlikely( in == NULL ) )
691     {
692         if( p_sys->i_threads == 0 )
693         {
694             block_t *p_block;
695             do {
696                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
697                 block_ChainAppend( out, p_block );
698             } while( p_block );
699         }
700         else
701         {
702             msg_Dbg( p_stream, "Flushing thread and waiting that");
703             vlc_mutex_lock( &p_stream->p_sys->lock_out );
704             p_stream->p_sys->b_abort = true;
705             vlc_cond_signal( &p_stream->p_sys->cond );
706             vlc_mutex_unlock( &p_stream->p_sys->lock_out );
707
708             vlc_join( p_stream->p_sys->thread, NULL );
709             vlc_mutex_lock( &p_sys->lock_out );
710             *out = p_sys->p_buffers;
711             p_sys->p_buffers = NULL;
712             vlc_mutex_unlock( &p_sys->lock_out );
713
714             msg_Dbg( p_stream, "Flushing done");
715         }
716         return VLC_SUCCESS;
717     }
718
719
720     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
721     {
722
723         if( unlikely (
724              id->p_encoder->p_module &&
725              !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
726             )
727           )
728         {
729             msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
730                         id->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
731                         id->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
732                     );
733             /* Close filters */
734             if( id->p_f_chain )
735                 filter_chain_Delete( id->p_f_chain );
736             id->p_f_chain = NULL;
737             if( id->p_uf_chain )
738                 filter_chain_Delete( id->p_uf_chain );
739             id->p_uf_chain = NULL;
740
741             /* Reinitialize filters */
742             id->p_encoder->fmt_out.video.i_visible_width  = p_sys->i_width & ~1;
743             id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
744             id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
745
746             transcode_video_filter_init( p_stream, id );
747             transcode_video_encoder_init( p_stream, id );
748             conversion_video_filter_append( id );
749             memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
750         }
751
752
753         if( unlikely( !id->p_encoder->p_module ) )
754         {
755             if( id->p_f_chain )
756                 filter_chain_Delete( id->p_f_chain );
757             if( id->p_uf_chain )
758                 filter_chain_Delete( id->p_uf_chain );
759             id->p_f_chain = id->p_uf_chain = NULL;
760
761             transcode_video_filter_init( p_stream, id );
762             transcode_video_encoder_init( p_stream, id );
763             conversion_video_filter_append( id );
764             memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
765
766             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
767             {
768                 picture_Release( p_pic );
769                 transcode_video_close( p_stream, id );
770                 id->b_transcode = false;
771                 return VLC_EGENERIC;
772             }
773         }
774
775         /* Run the filter and output chains; first with the picture,
776          * and then with NULL as many times as we need until they
777          * stop outputting frames.
778          */
779         for ( ;; ) {
780             picture_t *p_filtered_pic = p_pic;
781
782             /* Run filter chain */
783             if( id->p_f_chain )
784                 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
785             if( !p_filtered_pic )
786                 break;
787
788             for ( ;; ) {
789                 picture_t *p_user_filtered_pic = p_filtered_pic;
790
791                 /* Run user specified filter chain */
792                 if( id->p_uf_chain )
793                     p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
794                 if( !p_user_filtered_pic )
795                     break;
796
797                 OutputFrame( p_stream, p_user_filtered_pic, id, out );
798
799                 p_filtered_pic = NULL;
800             }
801
802             p_pic = NULL;
803         }
804     }
805
806     if( p_sys->i_threads >= 1 )
807     {
808         /* Pick up any return data the encoder thread wants to output. */
809         vlc_mutex_lock( &p_sys->lock_out );
810         *out = p_sys->p_buffers;
811         p_sys->p_buffers = NULL;
812         vlc_mutex_unlock( &p_sys->lock_out );
813     }
814
815     return VLC_SUCCESS;
816 }
817
818 bool transcode_video_add( sout_stream_t *p_stream, const es_format_t *p_fmt,
819                                 sout_stream_id_sys_t *id )
820 {
821     sout_stream_sys_t *p_sys = p_stream->p_sys;
822
823     msg_Dbg( p_stream,
824              "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
825              (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
826
827     /* Complete destination format */
828     id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
829     id->p_encoder->fmt_out.video.i_visible_width  = p_sys->i_width & ~1;
830     id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
831     id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
832
833     /* Build decoder -> filter -> encoder chain */
834     if( transcode_video_new( p_stream, id ) )
835     {
836         msg_Err( p_stream, "cannot create video chain" );
837         return false;
838     }
839
840     /* Stream will be added later on because we don't know
841      * all the characteristics of the decoded stream yet */
842     id->b_transcode = true;
843
844     if( p_sys->fps_num )
845     {
846         id->p_encoder->fmt_in.video.i_frame_rate = id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
847         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);
848     }
849
850     return true;
851 }
852