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