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