filter_chain: introduce dedicated filter_chain_NewVideo() for video filters
[vlc.git] / modules / stream_out / transcode / video.c
1 /*****************************************************************************
2  * video.c: transcoding stream output module (video)
3  *****************************************************************************
4  * Copyright (C) 2003-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10  *          Antoine Cellerier <dionoea at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "transcode.h"
32
33 #include <vlc_meta.h>
34 #include <vlc_spu.h>
35 #include <vlc_modules.h>
36
37 #define ENC_FRAMERATE (25 * 1000)
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 void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
46 {
47     VLC_UNUSED(p_decoder);
48     picture_Release( p_pic );
49 }
50
51 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
52 {
53     VLC_UNUSED(p_dec);
54     picture_Hold( p_pic );
55 }
56
57 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
58 {
59     VLC_UNUSED(p_dec);
60     picture_Release( p_pic );
61 }
62
63 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
64 {
65     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
66     return picture_NewFromFormat( &p_dec->fmt_out.video );
67 }
68
69 static picture_t *video_new_buffer_encoder( encoder_t *p_enc )
70 {
71     p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
72     return picture_NewFromFormat( &p_enc->fmt_in.video );
73 }
74
75 static picture_t *transcode_video_filter_buffer_new( filter_t *p_filter )
76 {
77     p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
78     return picture_NewFromFormat( &p_filter->fmt_out.video );
79 }
80
81 static void transcode_video_filter_buffer_del( filter_t *p_filter, picture_t *p_pic )
82 {
83     VLC_UNUSED(p_filter);
84     picture_Release( p_pic );
85 }
86
87 static void* EncoderThread( void *obj )
88 {
89     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)obj;
90     sout_stream_id_sys_t *id = p_sys->id_video;
91     picture_t *p_pic = NULL;
92     int canc = vlc_savecancel ();
93     block_t *p_block = NULL;
94
95     for( ;; )
96     {
97
98         vlc_mutex_lock( &p_sys->lock_out );
99         while( !p_sys->b_abort &&
100                (p_pic = picture_fifo_Pop( p_sys->pp_pics )) == NULL )
101             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
102
103         if( p_sys->b_abort && !p_pic )
104         {
105             vlc_mutex_unlock( &p_sys->lock_out );
106             break;
107         }
108         vlc_mutex_unlock( &p_sys->lock_out );
109
110         if( p_pic )
111         {
112             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
113
114             vlc_mutex_lock( &p_sys->lock_out );
115             block_ChainAppend( &p_sys->p_buffers, p_block );
116
117             vlc_mutex_unlock( &p_sys->lock_out );
118             picture_Release( p_pic );
119         }
120
121         vlc_mutex_lock( &p_sys->lock_out );
122         if( p_sys->b_abort )
123         {
124             vlc_mutex_unlock( &p_sys->lock_out );
125             break;
126         }
127         vlc_mutex_unlock( &p_sys->lock_out );
128     }
129
130     /*Encode what we have in the buffer on closing*/
131     vlc_mutex_lock( &p_sys->lock_out );
132     while( (p_pic = picture_fifo_Pop( p_sys->pp_pics )) != NULL )
133     {
134         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
135
136         block_ChainAppend( &p_sys->p_buffers, p_block );
137
138         picture_Release( p_pic );
139     }
140
141     /*Now flush encoder*/
142     do {
143        p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
144        block_ChainAppend( &p_sys->p_buffers, p_block );
145     } while( p_block );
146
147     vlc_mutex_unlock( &p_sys->lock_out );
148
149
150     vlc_restorecancel (canc);
151     return NULL;
152 }
153
154 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
155 {
156     sout_stream_sys_t *p_sys = p_stream->p_sys;
157
158     /* Open decoder
159      * Initialization of decoder structures
160      */
161     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
162     id->p_decoder->fmt_out.i_extra = 0;
163     id->p_decoder->fmt_out.p_extra = 0;
164     id->p_decoder->pf_decode_video = NULL;
165     id->p_decoder->pf_get_cc = NULL;
166     id->p_decoder->pf_get_cc = 0;
167     id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
168     id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
169     id->p_decoder->pf_picture_link    = video_link_picture_decoder;
170     id->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
171     id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
172     if( !id->p_decoder->p_owner )
173         return VLC_EGENERIC;
174
175     id->p_decoder->p_owner->p_sys = p_sys;
176     /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
177
178     id->p_decoder->p_module =
179         module_need( id->p_decoder, "decoder", "$codec", false );
180
181     if( !id->p_decoder->p_module )
182     {
183         msg_Err( p_stream, "cannot find video decoder" );
184         free( id->p_decoder->p_owner );
185         return VLC_EGENERIC;
186     }
187
188     /*
189      * Open encoder.
190      * Because some info about the decoded input will only be available
191      * once the first frame is decoded, we actually only test the availability
192      * of the encoder here.
193      */
194
195     /* Initialization of encoder format structures */
196     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
197                     id->p_decoder->fmt_out.i_codec );
198     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
199
200     /* The dimensions will be set properly later on.
201      * Just put sensible values so we can test an encoder is available. */
202     id->p_encoder->fmt_in.video.i_width =
203         id->p_encoder->fmt_out.video.i_width
204           ? id->p_encoder->fmt_out.video.i_width
205           : id->p_decoder->fmt_in.video.i_width
206             ? id->p_decoder->fmt_in.video.i_width : 16;
207     id->p_encoder->fmt_in.video.i_height =
208         id->p_encoder->fmt_out.video.i_height
209           ? id->p_encoder->fmt_out.video.i_height
210           : id->p_decoder->fmt_in.video.i_height
211             ? id->p_decoder->fmt_in.video.i_height : 16;
212     id->p_encoder->fmt_in.video.i_visible_width =
213         id->p_encoder->fmt_out.video.i_visible_width
214           ? id->p_encoder->fmt_out.video.i_visible_width
215           : id->p_decoder->fmt_in.video.i_visible_width
216             ? id->p_decoder->fmt_in.video.i_visible_width : id->p_encoder->fmt_in.video.i_width;
217     id->p_encoder->fmt_in.video.i_visible_height =
218         id->p_encoder->fmt_out.video.i_visible_height
219           ? id->p_encoder->fmt_out.video.i_visible_height
220           : id->p_decoder->fmt_in.video.i_visible_height
221             ? id->p_decoder->fmt_in.video.i_visible_height : id->p_encoder->fmt_in.video.i_height;
222
223     id->p_encoder->i_threads = p_sys->i_threads;
224     id->p_encoder->p_cfg = p_sys->p_video_cfg;
225
226     id->p_encoder->p_module =
227         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
228     if( !id->p_encoder->p_module )
229     {
230         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s). Take a look few lines earlier to see possible reason.",
231                  p_sys->psz_venc ? p_sys->psz_venc : "any",
232                  (char *)&p_sys->i_vcodec );
233         module_unneed( id->p_decoder, id->p_decoder->p_module );
234         id->p_decoder->p_module = 0;
235         free( id->p_decoder->p_owner );
236         return VLC_EGENERIC;
237     }
238
239     /* Close the encoder.
240      * We'll open it only when we have the first frame. */
241     module_unneed( id->p_encoder, id->p_encoder->p_module );
242     if( id->p_encoder->fmt_out.p_extra )
243     {
244         free( id->p_encoder->fmt_out.p_extra );
245         id->p_encoder->fmt_out.p_extra = NULL;
246         id->p_encoder->fmt_out.i_extra = 0;
247     }
248     id->p_encoder->p_module = NULL;
249
250     if( p_sys->i_threads >= 1 )
251     {
252         int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
253                            VLC_THREAD_PRIORITY_VIDEO;
254         p_sys->id_video = id;
255         vlc_mutex_init( &p_sys->lock_out );
256         vlc_cond_init( &p_sys->cond );
257         p_sys->pp_pics = picture_fifo_New();
258         if( p_sys->pp_pics == NULL )
259         {
260             msg_Err( p_stream, "cannot create picture fifo" );
261             vlc_mutex_destroy( &p_sys->lock_out );
262             vlc_cond_destroy( &p_sys->cond );
263             module_unneed( id->p_decoder, id->p_decoder->p_module );
264             id->p_decoder->p_module = NULL;
265             free( id->p_decoder->p_owner );
266             return VLC_ENOMEM;
267         }
268         p_sys->p_buffers = NULL;
269         p_sys->b_abort = false;
270         if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
271         {
272             msg_Err( p_stream, "cannot spawn encoder thread" );
273             vlc_mutex_destroy( &p_sys->lock_out );
274             vlc_cond_destroy( &p_sys->cond );
275             picture_fifo_Delete( p_sys->pp_pics );
276             module_unneed( id->p_decoder, id->p_decoder->p_module );
277             id->p_decoder->p_module = NULL;
278             free( id->p_decoder->p_owner );
279             return VLC_EGENERIC;
280         }
281     }
282     return VLC_SUCCESS;
283 }
284
285 static void transcode_video_filter_init( sout_stream_t *p_stream,
286                                          sout_stream_id_sys_t *id )
287 {
288     filter_owner_t owner = {
289         .sys = p_stream->p_sys,
290         .video = {
291             .buffer_new = transcode_video_filter_buffer_new,
292             .buffer_del = transcode_video_filter_buffer_del,
293         },
294     };
295     es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
296
297     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
298     id->p_f_chain = filter_chain_New( p_stream, false, &owner );
299     filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
300
301     /* Deinterlace */
302     if( p_stream->p_sys->b_deinterlace )
303     {
304         filter_chain_AppendFilter( id->p_f_chain,
305                                    p_stream->p_sys->psz_deinterlace,
306                                    p_stream->p_sys->p_deinterlace_cfg,
307                                    &id->p_decoder->fmt_out,
308                                    &id->p_decoder->fmt_out );
309
310         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
311     }
312
313     /* Check that we have visible_width/height*/
314     if( !p_fmt_out->video.i_visible_height )
315         p_fmt_out->video.i_visible_height = p_fmt_out->video.i_height;
316     if( !p_fmt_out->video.i_visible_width )
317         p_fmt_out->video.i_visible_width = p_fmt_out->video.i_width;
318
319     if( p_stream->p_sys->psz_vf2 )
320     {
321         id->p_uf_chain = filter_chain_New( p_stream, true, &owner );
322         filter_chain_Reset( id->p_uf_chain, p_fmt_out,
323                             &id->p_encoder->fmt_in );
324         if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
325         {
326             filter_chain_AppendFilter( id->p_uf_chain,
327                                    NULL, NULL,
328                                    p_fmt_out,
329                                    &id->p_encoder->fmt_in );
330         }
331         filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
332         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
333         es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
334         id->p_encoder->fmt_out.video.i_width =
335             id->p_encoder->fmt_in.video.i_width;
336         id->p_encoder->fmt_out.video.i_height =
337             id->p_encoder->fmt_in.video.i_height;
338         id->p_encoder->fmt_out.video.i_sar_num =
339             id->p_encoder->fmt_in.video.i_sar_num;
340         id->p_encoder->fmt_out.video.i_sar_den =
341             id->p_encoder->fmt_in.video.i_sar_den;
342     }
343
344 }
345
346 /* Take care of the scaling and chroma conversions. */
347 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
348 {
349     const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
350     if( id->p_f_chain )
351         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
352
353     if( id->p_uf_chain )
354         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
355
356     if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
357         ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
358         ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
359     {
360         filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
361                                    NULL, NULL,
362                                    p_fmt_out,
363                                    &id->p_encoder->fmt_in );
364     }
365 }
366
367 static void transcode_video_encoder_init( sout_stream_t *p_stream,
368                                           sout_stream_id_sys_t *id )
369 {
370     sout_stream_sys_t *p_sys = p_stream->p_sys;
371
372     const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
373     if( id->p_f_chain ) {
374         p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
375     }
376     if( id->p_uf_chain ) {
377         p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
378     }
379
380     /* Calculate scaling
381      * width/height of source */
382     int i_src_visible_width = p_fmt_out->video.i_visible_width;
383     int i_src_visible_height = p_fmt_out->video.i_visible_height;
384
385     if (i_src_visible_width == 0)
386         i_src_visible_width = p_fmt_out->video.i_width;
387     if (i_src_visible_height == 0)
388         i_src_visible_height = p_fmt_out->video.i_height;
389
390
391     /* with/height scaling */
392     float f_scale_width = 1;
393     float f_scale_height = 1;
394
395     /* aspect ratio */
396     float f_aspect = (double)p_fmt_out->video.i_sar_num *
397                      p_fmt_out->video.i_width /
398                      p_fmt_out->video.i_sar_den /
399                      p_fmt_out->video.i_height;
400
401     msg_Dbg( p_stream, "decoder aspect is %f:1", f_aspect );
402
403     /* Change f_aspect from source frame to source pixel */
404     f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
405     msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
406
407     /* Calculate scaling factor for specified parameters */
408     if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
409         id->p_encoder->fmt_out.video.i_visible_height <= 0 && p_sys->f_scale )
410     {
411         /* Global scaling. Make sure width will remain a factor of 16 */
412         float f_real_scale;
413         int  i_new_height;
414         int i_new_width = i_src_visible_width * p_sys->f_scale;
415
416         if( i_new_width % 16 <= 7 && i_new_width >= 16 )
417             i_new_width -= i_new_width % 16;
418         else
419             i_new_width += 16 - i_new_width % 16;
420
421         f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
422
423         i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
424
425         f_scale_width = f_real_scale;
426         f_scale_height = (float) i_new_height / (float) i_src_visible_height;
427     }
428     else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
429              id->p_encoder->fmt_out.video.i_visible_height <= 0 )
430     {
431         /* Only width specified */
432         f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
433         f_scale_height = f_scale_width;
434     }
435     else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
436              id->p_encoder->fmt_out.video.i_visible_height > 0 )
437     {
438          /* Only height specified */
439          f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
440          f_scale_width = f_scale_height;
441      }
442      else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
443               id->p_encoder->fmt_out.video.i_visible_height > 0 )
444      {
445          /* Width and height specified */
446          f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
447          f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
448      }
449
450      /* check maxwidth and maxheight */
451      if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
452                                                      i_src_visible_width )
453      {
454          f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
455      }
456
457      if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
458                                                        i_src_visible_height )
459      {
460          f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
461      }
462
463
464      /* Change aspect ratio from source pixel to scaled pixel */
465      f_aspect = f_aspect * f_scale_height / f_scale_width;
466      msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
467
468      /* f_scale_width and f_scale_height are now final */
469      /* Calculate width, height from scaling
470       * Make sure its multiple of 2
471       */
472      /* width/height of output stream */
473      int i_dst_visible_width =  2 * (int)(f_scale_width*i_src_visible_width/2+0.5);
474      int i_dst_visible_height = 2 * (int)(f_scale_height*i_src_visible_height/2+0.5);
475      int i_dst_width =  2 * (int)(f_scale_width*p_fmt_out->video.i_width/2+0.5);
476      int i_dst_height = 2 * (int)(f_scale_height*p_fmt_out->video.i_height/2+0.5);
477
478      /* Change aspect ratio from scaled pixel to output frame */
479      f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
480
481      /* Store calculated values */
482      id->p_encoder->fmt_out.video.i_width = i_dst_width;
483      id->p_encoder->fmt_out.video.i_visible_width = i_dst_visible_width;
484      id->p_encoder->fmt_out.video.i_height = i_dst_height;
485      id->p_encoder->fmt_out.video.i_visible_height = i_dst_visible_height;
486
487      id->p_encoder->fmt_in.video.i_width = i_dst_width;
488      id->p_encoder->fmt_in.video.i_visible_width = i_dst_visible_width;
489      id->p_encoder->fmt_in.video.i_height = i_dst_height;
490      id->p_encoder->fmt_in.video.i_visible_height = i_dst_visible_height;
491
492      msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
493          i_src_visible_width, i_src_visible_height,
494          i_dst_visible_width, i_dst_visible_height
495      );
496
497     /* Handle frame rate conversion */
498     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
499         !id->p_encoder->fmt_out.video.i_frame_rate_base )
500     {
501         if( p_fmt_out->video.i_frame_rate &&
502             p_fmt_out->video.i_frame_rate_base )
503         {
504             id->p_encoder->fmt_out.video.i_frame_rate =
505                 p_fmt_out->video.i_frame_rate;
506             id->p_encoder->fmt_out.video.i_frame_rate_base =
507                 p_fmt_out->video.i_frame_rate_base;
508         }
509         else
510         {
511             /* Pick a sensible default value */
512             id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
513             id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
514         }
515     }
516
517     id->p_encoder->fmt_in.video.orientation =
518         id->p_encoder->fmt_out.video.orientation =
519         id->p_decoder->fmt_in.video.orientation;
520
521     id->p_encoder->fmt_in.video.i_frame_rate =
522         id->p_encoder->fmt_out.video.i_frame_rate;
523     id->p_encoder->fmt_in.video.i_frame_rate_base =
524         id->p_encoder->fmt_out.video.i_frame_rate_base;
525
526     vlc_ureduce( &id->p_encoder->fmt_in.video.i_frame_rate,
527         &id->p_encoder->fmt_in.video.i_frame_rate_base,
528         id->p_encoder->fmt_in.video.i_frame_rate,
529         id->p_encoder->fmt_in.video.i_frame_rate_base,
530         0 );
531      msg_Dbg( p_stream, "source fps %d/%d, destination %d/%d",
532         id->p_decoder->fmt_out.video.i_frame_rate,
533         id->p_decoder->fmt_out.video.i_frame_rate_base,
534         id->p_encoder->fmt_in.video.i_frame_rate,
535         id->p_encoder->fmt_in.video.i_frame_rate_base );
536
537     id->i_input_frame_interval  = id->p_decoder->fmt_out.video.i_frame_rate_base * CLOCK_FREQ / id->p_decoder->fmt_out.video.i_frame_rate;
538     msg_Info( p_stream, "input interval %d (base %d)",
539                         id->i_input_frame_interval, id->p_decoder->fmt_out.video.i_frame_rate_base );
540
541     id->i_output_frame_interval = id->p_encoder->fmt_in.video.i_frame_rate_base * CLOCK_FREQ / id->p_encoder->fmt_in.video.i_frame_rate;
542     msg_Info( p_stream, "output interval %d (base %d)",
543                         id->i_output_frame_interval, id->p_encoder->fmt_in.video.i_frame_rate_base );
544
545     date_Init( &id->next_input_pts,
546                id->p_decoder->fmt_out.video.i_frame_rate,
547                1 );
548
549     date_Init( &id->next_output_pts,
550                id->p_encoder->fmt_in.video.i_frame_rate,
551                1 );
552
553     /* Check whether a particular aspect ratio was requested */
554     if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
555         id->p_encoder->fmt_out.video.i_sar_den <= 0 )
556     {
557         vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
558                      &id->p_encoder->fmt_out.video.i_sar_den,
559                      (uint64_t)p_fmt_out->video.i_sar_num * i_src_visible_width  * i_dst_visible_height,
560                      (uint64_t)p_fmt_out->video.i_sar_den * i_src_visible_height * i_dst_visible_width,
561                      0 );
562     }
563     else
564     {
565         vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
566                      &id->p_encoder->fmt_out.video.i_sar_den,
567                      id->p_encoder->fmt_out.video.i_sar_num,
568                      id->p_encoder->fmt_out.video.i_sar_den,
569                      0 );
570     }
571
572     id->p_encoder->fmt_in.video.i_sar_num =
573         id->p_encoder->fmt_out.video.i_sar_num;
574     id->p_encoder->fmt_in.video.i_sar_den =
575         id->p_encoder->fmt_out.video.i_sar_den;
576
577     msg_Dbg( p_stream, "encoder aspect is %i:%i",
578              id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
579              id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
580
581 }
582
583 static int transcode_video_encoder_open( sout_stream_t *p_stream,
584                                          sout_stream_id_sys_t *id )
585 {
586     sout_stream_sys_t *p_sys = p_stream->p_sys;
587
588
589     msg_Dbg( p_stream, "destination (after video filters) %ix%i",
590              id->p_encoder->fmt_in.video.i_width,
591              id->p_encoder->fmt_in.video.i_height );
592
593     id->p_encoder->p_module =
594         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
595     if( !id->p_encoder->p_module )
596     {
597         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
598                  p_sys->psz_venc ? p_sys->psz_venc : "any",
599                  (char *)&p_sys->i_vcodec );
600         return VLC_EGENERIC;
601     }
602
603     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
604
605     /*  */
606     id->p_encoder->fmt_out.i_codec =
607         vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
608
609     id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
610     if( !id->id )
611     {
612         msg_Err( p_stream, "cannot add this stream" );
613         return VLC_EGENERIC;
614     }
615
616     return VLC_SUCCESS;
617 }
618
619 void transcode_video_close( sout_stream_t *p_stream,
620                                    sout_stream_id_sys_t *id )
621 {
622     if( p_stream->p_sys->i_threads >= 1 )
623     {
624         vlc_mutex_lock( &p_stream->p_sys->lock_out );
625         p_stream->p_sys->b_abort = true;
626         vlc_cond_signal( &p_stream->p_sys->cond );
627         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
628
629         vlc_join( p_stream->p_sys->thread, NULL );
630         vlc_mutex_destroy( &p_stream->p_sys->lock_out );
631         vlc_cond_destroy( &p_stream->p_sys->cond );
632
633         picture_fifo_Delete( p_stream->p_sys->pp_pics );
634         block_ChainRelease( p_stream->p_sys->p_buffers );
635         p_stream->p_sys->pp_pics = NULL;
636     }
637
638     /* Close decoder */
639     if( id->p_decoder->p_module )
640         module_unneed( id->p_decoder, id->p_decoder->p_module );
641     if( id->p_decoder->p_description )
642         vlc_meta_Delete( id->p_decoder->p_description );
643
644     free( id->p_decoder->p_owner );
645
646     /* Close encoder */
647     if( id->p_encoder->p_module )
648         module_unneed( id->p_encoder, id->p_encoder->p_module );
649
650     /* Close filters */
651     if( id->p_f_chain )
652         filter_chain_Delete( id->p_f_chain );
653     if( id->p_uf_chain )
654         filter_chain_Delete( id->p_uf_chain );
655 }
656
657 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
658 {
659     sout_stream_sys_t *p_sys = p_stream->p_sys;
660     picture_t *p_pic2 = NULL;
661     const mtime_t original_date = p_pic->date;
662     bool b_need_duplicate=false;
663     /* If input pts is lower than next_output_pts - output_frame_interval
664      * Then the future input frame should fit better and we can drop this one 
665      *
666      * We check it here also because we can have case that video filters outputs multiple
667      * pictures but we don't need to use them all, for example yadif2x and outputting to some
668      * different fps value
669      */
670     if( ( original_date ) <
671         ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
672     {
673 #if 0
674         msg_Dbg( p_stream, "dropping frame (%"PRId64" + %"PRId64" vs %"PRId64")",
675                  p_pic->date, id->i_input_frame_interval, date_Get(&id->next_output_pts) );
676 #endif
677         picture_Release( p_pic );
678         return;
679     }
680
681     /*
682      * Encoding
683      */
684     /* Check if we have a subpicture to overlay */
685     if( p_sys->p_spu )
686     {
687         video_format_t fmt = id->p_encoder->fmt_in.video;
688         if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
689         {
690             fmt.i_visible_width  = fmt.i_width;
691             fmt.i_visible_height = fmt.i_height;
692             fmt.i_x_offset       = 0;
693             fmt.i_y_offset       = 0;
694         }
695
696         subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt, &fmt,
697                                              p_pic->date, p_pic->date, false );
698
699         /* Overlay subpicture */
700         if( p_subpic )
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                  * in this point the picture is already p_encoder->fmt.in format*/
706                 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
707                 if( likely( p_tmp ) )
708                 {
709                     picture_Copy( p_tmp, p_pic );
710                     picture_Release( p_pic );
711                     p_pic = p_tmp;
712                 }
713             }
714             if( unlikely( !p_sys->p_spu_blend ) )
715                 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
716             if( likely( p_sys->p_spu_blend ) )
717                 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
718             subpicture_Delete( p_subpic );
719         }
720     }
721
722     /* set output pts*/
723     p_pic->date = date_Get( &id->next_output_pts );
724     /*This pts is handled, increase clock to next one*/
725     date_Increment( &id->next_output_pts, id->p_encoder->fmt_in.video.i_frame_rate_base );
726
727     if( p_sys->i_threads == 0 )
728     {
729         block_t *p_block;
730
731         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
732         block_ChainAppend( out, p_block );
733     }
734
735     /* we need to duplicate while next_output_pts + output_frame_interval < input_pts (next input pts)*/
736     b_need_duplicate = ( date_Get( &id->next_output_pts ) + id->i_output_frame_interval ) <
737                        ( original_date );
738
739     if( p_sys->i_threads )
740     {
741         if( p_sys->b_master_sync )
742         {
743             p_pic2 = video_new_buffer_encoder( id->p_encoder );
744             if( likely( p_pic2 != NULL ) )
745                 picture_Copy( p_pic2, p_pic );
746         }
747         vlc_mutex_lock( &p_sys->lock_out );
748         picture_fifo_Push( p_sys->pp_pics, p_pic );
749         vlc_cond_signal( &p_sys->cond );
750         vlc_mutex_unlock( &p_sys->lock_out );
751     }
752
753     while( (p_sys->b_master_sync && b_need_duplicate ))
754     {
755         if( p_sys->i_threads >= 1 )
756         {
757             picture_t *p_tmp = NULL;
758             /* We can't modify the picture, we need to duplicate it */
759             p_tmp = video_new_buffer_encoder( id->p_encoder );
760             if( likely( p_tmp != NULL ) )
761             {
762                 picture_Copy( p_tmp, p_pic2 );
763                 p_tmp->date = date_Get( &id->next_output_pts );
764                 vlc_mutex_lock( &p_sys->lock_out );
765                 picture_fifo_Push( p_sys->pp_pics, p_tmp );
766                 vlc_cond_signal( &p_sys->cond );
767                 vlc_mutex_unlock( &p_sys->lock_out );
768             }
769         }
770         else
771         {
772             block_t *p_block;
773             p_pic->date = date_Get( &id->next_output_pts );
774             p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
775             block_ChainAppend( out, p_block );
776         }
777 #if 0
778         msg_Dbg( p_stream, "duplicated frame");
779 #endif
780         date_Increment( &id->next_output_pts, id->p_encoder->fmt_in.video.i_frame_rate_base );
781         b_need_duplicate = ( date_Get( &id->next_output_pts ) + id->i_output_frame_interval ) <
782                            ( original_date );
783     }
784
785     if( p_sys->i_threads && p_pic2 )
786         picture_Release( p_pic2 );
787     else if ( p_sys->i_threads == 0 )
788         picture_Release( p_pic );
789 }
790
791 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
792                                     block_t *in, block_t **out )
793 {
794     sout_stream_sys_t *p_sys = p_stream->p_sys;
795     picture_t *p_pic = NULL;
796     *out = NULL;
797
798     if( unlikely( in == NULL ) )
799     {
800         if( p_sys->i_threads == 0 )
801         {
802             block_t *p_block;
803             do {
804                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
805                 block_ChainAppend( out, p_block );
806             } while( p_block );
807         }
808         else
809         {
810             msg_Dbg( p_stream, "Flushing thread and waiting that");
811             vlc_mutex_lock( &p_stream->p_sys->lock_out );
812             p_stream->p_sys->b_abort = true;
813             vlc_cond_signal( &p_stream->p_sys->cond );
814             vlc_mutex_unlock( &p_stream->p_sys->lock_out );
815
816             vlc_join( p_stream->p_sys->thread, NULL );
817             vlc_mutex_lock( &p_sys->lock_out );
818             *out = p_sys->p_buffers;
819             p_sys->p_buffers = NULL;
820             vlc_mutex_unlock( &p_sys->lock_out );
821
822             msg_Dbg( p_stream, "Flushing done");
823         }
824         return VLC_SUCCESS;
825     }
826
827
828     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
829     {
830
831         if( unlikely (
832              id->p_encoder->p_module &&
833              !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
834             )
835           )
836         {
837             msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
838                         id->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
839                         id->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
840                     );
841             /* Close filters */
842             if( id->p_f_chain )
843                 filter_chain_Delete( id->p_f_chain );
844             id->p_f_chain = NULL;
845             if( id->p_uf_chain )
846                 filter_chain_Delete( id->p_uf_chain );
847             id->p_uf_chain = NULL;
848
849             /* Reinitialize filters */
850             id->p_encoder->fmt_out.video.i_visible_width  = p_sys->i_width & ~1;
851             id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
852             id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
853
854             transcode_video_filter_init( p_stream, id );
855             transcode_video_encoder_init( p_stream, id );
856             conversion_video_filter_append( id );
857             memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
858         }
859
860
861         if( unlikely( !id->p_encoder->p_module ) )
862         {
863             if( id->p_f_chain )
864                 filter_chain_Delete( id->p_f_chain );
865             if( id->p_uf_chain )
866                 filter_chain_Delete( id->p_uf_chain );
867             id->p_f_chain = id->p_uf_chain = NULL;
868
869             transcode_video_filter_init( p_stream, id );
870             transcode_video_encoder_init( p_stream, id );
871             conversion_video_filter_append( id );
872             memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
873
874             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
875             {
876                 picture_Release( p_pic );
877                 transcode_video_close( p_stream, id );
878                 id->b_transcode = false;
879                 return VLC_EGENERIC;
880             }
881             date_Set( &id->next_output_pts, p_pic->date );
882             date_Set( &id->next_input_pts, p_pic->date );
883         }
884
885         /*Input lipsync and drop check */
886         if( p_sys->b_master_sync )
887         {
888             /* If input pts lower than next_output_pts - output_frame_interval
889              * Then the future input frame should fit better and we can drop this one 
890              *
891              * We check this here as we don't need to run video filter at all for pictures
892              * we are going to drop anyway
893              *
894              * Duplication need is checked in OutputFrame */
895             if( ( p_pic->date ) <
896                 ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
897             {
898 #if 0
899                 msg_Dbg( p_stream, "dropping frame (%"PRId64" + %"PRId64" vs %"PRId64")",
900                          p_pic->date, id->i_input_frame_interval, date_Get(&id->next_output_pts) );
901 #endif
902                 picture_Release( p_pic );
903                 continue;
904             }
905 #if 0
906             msg_Dbg( p_stream, "not dropping frame");
907 #endif
908
909         }
910         /* Check input drift regardless, if it's more than 100ms from our approximation, we most likely have lost pictures
911          * and are in danger to become out of sync, so better reset timestamps then */
912         if( likely( p_pic->date != VLC_TS_INVALID ) )
913         {
914             mtime_t input_drift = p_pic->date - date_Get( &id->next_input_pts );
915             if( unlikely( (input_drift > (CLOCK_FREQ/10)) ||
916                           (input_drift < -(CLOCK_FREQ/10))
917                ) )
918             {
919                 msg_Warn( p_stream, "Reseting video sync" );
920                 date_Set( &id->next_output_pts, p_pic->date );
921                 date_Set( &id->next_input_pts, p_pic->date );
922             }
923         }
924         date_Increment( &id->next_input_pts, id->p_decoder->fmt_out.video.i_frame_rate_base );
925
926         /* Run the filter and output chains; first with the picture,
927          * and then with NULL as many times as we need until they
928          * stop outputting frames.
929          */
930         for ( ;; ) {
931             picture_t *p_filtered_pic = p_pic;
932
933             /* Run filter chain */
934             if( id->p_f_chain )
935                 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
936             if( !p_filtered_pic )
937                 break;
938
939             for ( ;; ) {
940                 picture_t *p_user_filtered_pic = p_filtered_pic;
941
942                 /* Run user specified filter chain */
943                 if( id->p_uf_chain )
944                     p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
945                 if( !p_user_filtered_pic )
946                     break;
947
948                 OutputFrame( p_stream, p_user_filtered_pic, id, out );
949
950                 p_filtered_pic = NULL;
951             }
952
953             p_pic = NULL;
954         }
955     }
956
957     if( p_sys->i_threads >= 1 )
958     {
959         /* Pick up any return data the encoder thread wants to output. */
960         vlc_mutex_lock( &p_sys->lock_out );
961         *out = p_sys->p_buffers;
962         p_sys->p_buffers = NULL;
963         vlc_mutex_unlock( &p_sys->lock_out );
964     }
965
966     return VLC_SUCCESS;
967 }
968
969 bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
970                                 sout_stream_id_sys_t *id )
971 {
972     sout_stream_sys_t *p_sys = p_stream->p_sys;
973
974     msg_Dbg( p_stream,
975              "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
976              (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
977
978     /* Complete destination format */
979     id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
980     id->p_encoder->fmt_out.video.i_visible_width  = p_sys->i_width & ~1;
981     id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
982     id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
983
984     /* Build decoder -> filter -> encoder chain */
985     if( transcode_video_new( p_stream, id ) )
986     {
987         msg_Err( p_stream, "cannot create video chain" );
988         return false;
989     }
990
991     /* Stream will be added later on because we don't know
992      * all the characteristics of the decoded stream yet */
993     id->b_transcode = true;
994
995     if( p_sys->fps_num )
996     {
997         id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
998         id->p_encoder->fmt_out.video.i_frame_rate_base = (p_sys->fps_den ? p_sys->fps_den : 1);
999     }
1000
1001     return true;
1002 }
1003