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