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