1 /*****************************************************************************
2 * video.c: transcoding stream output module (video)
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
31 #include "transcode.h"
36 #include <vlc_modules.h>
38 #define ENC_FRAMERATE (25 * 1000)
39 #define ENC_FRAMERATE_BASE 1000
41 struct decoder_owner_sys_t
43 sout_stream_sys_t *p_sys;
46 static int video_update_format_decoder( decoder_t *p_dec )
48 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
52 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
54 return picture_NewFromFormat( &p_dec->fmt_out.video );
57 static picture_t *video_new_buffer_encoder( encoder_t *p_enc )
59 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
60 return picture_NewFromFormat( &p_enc->fmt_in.video );
63 static picture_t *transcode_video_filter_buffer_new( filter_t *p_filter )
65 p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
66 return picture_NewFromFormat( &p_filter->fmt_out.video );
69 static void* EncoderThread( void *obj )
71 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)obj;
72 sout_stream_id_sys_t *id = p_sys->id_video;
73 picture_t *p_pic = NULL;
74 int canc = vlc_savecancel ();
75 block_t *p_block = NULL;
77 vlc_mutex_lock( &p_sys->lock_out );
81 while( !p_sys->b_abort &&
82 (p_pic = picture_fifo_Pop( p_sys->pp_pics )) == NULL )
83 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
87 /* release lock while encoding */
88 vlc_mutex_unlock( &p_sys->lock_out );
89 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
90 picture_Release( p_pic );
91 vlc_mutex_lock( &p_sys->lock_out );
93 block_ChainAppend( &p_sys->p_buffers, p_block );
100 /*Encode what we have in the buffer on closing*/
101 while( (p_pic = picture_fifo_Pop( p_sys->pp_pics )) != NULL )
103 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
104 picture_Release( p_pic );
105 block_ChainAppend( &p_sys->p_buffers, p_block );
108 /*Now flush encoder*/
110 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
111 block_ChainAppend( &p_sys->p_buffers, p_block );
114 vlc_mutex_unlock( &p_sys->lock_out );
116 vlc_restorecancel (canc);
121 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
123 sout_stream_sys_t *p_sys = p_stream->p_sys;
126 * Initialization of decoder structures
128 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
129 id->p_decoder->fmt_out.i_extra = 0;
130 id->p_decoder->fmt_out.p_extra = 0;
131 id->p_decoder->pf_decode_video = NULL;
132 id->p_decoder->pf_get_cc = NULL;
133 id->p_decoder->pf_get_cc = 0;
134 id->p_decoder->pf_vout_format_update = video_update_format_decoder;
135 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
136 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
137 if( !id->p_decoder->p_owner )
140 id->p_decoder->p_owner->p_sys = p_sys;
141 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
143 id->p_decoder->p_module =
144 module_need( id->p_decoder, "decoder", "$codec", false );
146 if( !id->p_decoder->p_module )
148 msg_Err( p_stream, "cannot find video decoder" );
149 free( id->p_decoder->p_owner );
155 * Because some info about the decoded input will only be available
156 * once the first frame is decoded, we actually only test the availability
157 * of the encoder here.
160 /* Initialization of encoder format structures */
161 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
162 id->p_decoder->fmt_out.i_codec );
163 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
165 /* The dimensions will be set properly later on.
166 * Just put sensible values so we can test an encoder is available. */
167 id->p_encoder->fmt_in.video.i_width =
168 id->p_encoder->fmt_out.video.i_width
169 ? id->p_encoder->fmt_out.video.i_width
170 : id->p_decoder->fmt_in.video.i_width
171 ? id->p_decoder->fmt_in.video.i_width : 16;
172 id->p_encoder->fmt_in.video.i_height =
173 id->p_encoder->fmt_out.video.i_height
174 ? id->p_encoder->fmt_out.video.i_height
175 : id->p_decoder->fmt_in.video.i_height
176 ? id->p_decoder->fmt_in.video.i_height : 16;
177 id->p_encoder->fmt_in.video.i_visible_width =
178 id->p_encoder->fmt_out.video.i_visible_width
179 ? id->p_encoder->fmt_out.video.i_visible_width
180 : id->p_decoder->fmt_in.video.i_visible_width
181 ? id->p_decoder->fmt_in.video.i_visible_width : id->p_encoder->fmt_in.video.i_width;
182 id->p_encoder->fmt_in.video.i_visible_height =
183 id->p_encoder->fmt_out.video.i_visible_height
184 ? id->p_encoder->fmt_out.video.i_visible_height
185 : id->p_decoder->fmt_in.video.i_visible_height
186 ? id->p_decoder->fmt_in.video.i_visible_height : id->p_encoder->fmt_in.video.i_height;
188 id->p_encoder->i_threads = p_sys->i_threads;
189 id->p_encoder->p_cfg = p_sys->p_video_cfg;
191 id->p_encoder->p_module =
192 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
193 if( !id->p_encoder->p_module )
195 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s). Take a look few lines earlier to see possible reason.",
196 p_sys->psz_venc ? p_sys->psz_venc : "any",
197 (char *)&p_sys->i_vcodec );
198 module_unneed( id->p_decoder, id->p_decoder->p_module );
199 id->p_decoder->p_module = 0;
200 free( id->p_decoder->p_owner );
204 /* Close the encoder.
205 * We'll open it only when we have the first frame. */
206 module_unneed( id->p_encoder, id->p_encoder->p_module );
207 if( id->p_encoder->fmt_out.p_extra )
209 free( id->p_encoder->fmt_out.p_extra );
210 id->p_encoder->fmt_out.p_extra = NULL;
211 id->p_encoder->fmt_out.i_extra = 0;
213 id->p_encoder->p_module = NULL;
215 if( p_sys->i_threads <= 0 )
218 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
219 VLC_THREAD_PRIORITY_VIDEO;
220 p_sys->id_video = id;
221 p_sys->pp_pics = picture_fifo_New();
222 if( p_sys->pp_pics == NULL )
224 msg_Err( p_stream, "cannot create picture fifo" );
225 module_unneed( id->p_decoder, id->p_decoder->p_module );
226 id->p_decoder->p_module = NULL;
227 free( id->p_decoder->p_owner );
230 vlc_mutex_init( &p_sys->lock_out );
231 vlc_cond_init( &p_sys->cond );
232 p_sys->p_buffers = NULL;
233 p_sys->b_abort = false;
234 if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
236 msg_Err( p_stream, "cannot spawn encoder thread" );
237 vlc_mutex_destroy( &p_sys->lock_out );
238 vlc_cond_destroy( &p_sys->cond );
239 picture_fifo_Delete( p_sys->pp_pics );
240 module_unneed( id->p_decoder, id->p_decoder->p_module );
241 id->p_decoder->p_module = NULL;
242 free( id->p_decoder->p_owner );
248 static filter_t *AppendDeinterlaceFilter( sout_stream_t *p_stream,
249 filter_chain_t *p_chain,
250 const char *psz_name,
251 config_chain_t *p_cfg,
252 const es_format_t *p_fmt_in,
253 const es_format_t *p_fmt_out )
256 filter_chain_AppendFilter( p_chain, psz_name, p_cfg, p_fmt_in, p_fmt_out );
260 msg_Dbg( p_stream, "deinterlace init failed, trying colorspace conversion" );
262 /* Most likely the filter failed due to wrong (e.g. non-planar) input format.
263 * Try converting to a different color space and try again.
265 * We use the recommended fallback formats in order, to get a conversion that's
266 * as cheap (both CPU-wise and quality-wise) as possible.
268 for( const vlc_fourcc_t *fourcc = vlc_fourcc_GetYUVFallback( p_fmt_out->video.i_chroma );
272 msg_Dbg( p_stream, "trying deinterlace through colorspace conversion to %4.4s", (char *)fourcc );
274 es_format_t p_new_fmt;
275 es_format_Copy( &p_new_fmt, p_fmt_in );
276 p_new_fmt.video.i_chroma = *fourcc; // XXX: is this enough?
278 filter_chain_Reset( p_chain, NULL, NULL );
280 p_filter = filter_chain_AppendFilter( p_chain,
287 p_filter = filter_chain_AppendFilter( p_chain,
294 msg_Dbg( p_stream, "deinterlace through colorspace conversion to %4.4s succeeded", (char *)fourcc );
299 msg_Err( p_stream, "deinterlace init failed, even with colorspace conversion" );
303 static void transcode_video_filter_init( sout_stream_t *p_stream,
304 sout_stream_id_sys_t *id )
306 filter_owner_t owner = {
307 .sys = p_stream->p_sys,
309 .buffer_new = transcode_video_filter_buffer_new,
312 es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
314 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
315 id->p_f_chain = filter_chain_NewVideo( p_stream, false, &owner );
316 filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
319 if( p_stream->p_sys->b_deinterlace )
322 AppendDeinterlaceFilter( p_stream,
324 p_stream->p_sys->psz_deinterlace,
325 p_stream->p_sys->p_deinterlace_cfg,
330 msg_Err( p_stream, "deinterlace init failed, even with colorspace conversion" );
334 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
336 if( p_stream->p_sys->b_master_sync )
338 filter_chain_AppendFilter( id->p_f_chain,
342 &id->p_encoder->fmt_in );
344 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
347 /* Check that we have visible_width/height*/
348 if( !p_fmt_out->video.i_visible_height )
349 p_fmt_out->video.i_visible_height = p_fmt_out->video.i_height;
350 if( !p_fmt_out->video.i_visible_width )
351 p_fmt_out->video.i_visible_width = p_fmt_out->video.i_width;
353 if( p_stream->p_sys->psz_vf2 )
355 id->p_uf_chain = filter_chain_NewVideo( p_stream, true, &owner );
356 filter_chain_Reset( id->p_uf_chain, p_fmt_out,
357 &id->p_encoder->fmt_in );
358 if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
360 filter_chain_AppendFilter( id->p_uf_chain,
363 &id->p_encoder->fmt_in );
365 filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
366 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
367 es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
368 id->p_encoder->fmt_out.video.i_width =
369 id->p_encoder->fmt_in.video.i_width;
370 id->p_encoder->fmt_out.video.i_height =
371 id->p_encoder->fmt_in.video.i_height;
372 id->p_encoder->fmt_out.video.i_sar_num =
373 id->p_encoder->fmt_in.video.i_sar_num;
374 id->p_encoder->fmt_out.video.i_sar_den =
375 id->p_encoder->fmt_in.video.i_sar_den;
380 /* Take care of the scaling and chroma conversions. */
381 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
383 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
385 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
388 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
390 if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
391 ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
392 ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
394 filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
397 &id->p_encoder->fmt_in );
401 static void transcode_video_encoder_init( sout_stream_t *p_stream,
402 sout_stream_id_sys_t *id )
404 sout_stream_sys_t *p_sys = p_stream->p_sys;
406 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
407 if( id->p_f_chain ) {
408 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
410 if( id->p_uf_chain ) {
411 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
415 * width/height of source */
416 int i_src_visible_width = p_fmt_out->video.i_visible_width;
417 int i_src_visible_height = p_fmt_out->video.i_visible_height;
419 if (i_src_visible_width == 0)
420 i_src_visible_width = p_fmt_out->video.i_width;
421 if (i_src_visible_height == 0)
422 i_src_visible_height = p_fmt_out->video.i_height;
425 /* with/height scaling */
426 float f_scale_width = 1;
427 float f_scale_height = 1;
430 float f_aspect = (double)p_fmt_out->video.i_sar_num *
431 p_fmt_out->video.i_width /
432 p_fmt_out->video.i_sar_den /
433 p_fmt_out->video.i_height;
435 msg_Dbg( p_stream, "decoder aspect is %f:1", (double) f_aspect );
437 /* Change f_aspect from source frame to source pixel */
438 f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
439 msg_Dbg( p_stream, "source pixel aspect is %f:1", (double) f_aspect );
441 /* Calculate scaling factor for specified parameters */
442 if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
443 id->p_encoder->fmt_out.video.i_visible_height <= 0 && p_sys->f_scale )
445 /* Global scaling. Make sure width will remain a factor of 16 */
448 int i_new_width = i_src_visible_width * p_sys->f_scale;
450 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
451 i_new_width -= i_new_width % 16;
453 i_new_width += 16 - i_new_width % 16;
455 f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
457 i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
459 f_scale_width = f_real_scale;
460 f_scale_height = (float) i_new_height / (float) i_src_visible_height;
462 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
463 id->p_encoder->fmt_out.video.i_visible_height <= 0 )
465 /* Only width specified */
466 f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
467 f_scale_height = f_scale_width;
469 else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
470 id->p_encoder->fmt_out.video.i_visible_height > 0 )
472 /* Only height specified */
473 f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
474 f_scale_width = f_scale_height;
476 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
477 id->p_encoder->fmt_out.video.i_visible_height > 0 )
479 /* Width and height specified */
480 f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
481 f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
484 /* check maxwidth and maxheight */
485 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
486 i_src_visible_width )
488 f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
491 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
492 i_src_visible_height )
494 f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
498 /* Change aspect ratio from source pixel to scaled pixel */
499 f_aspect = f_aspect * f_scale_height / f_scale_width;
500 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", (double) f_aspect );
502 /* f_scale_width and f_scale_height are now final */
503 /* Calculate width, height from scaling
504 * Make sure its multiple of 2
506 /* width/height of output stream */
507 int i_dst_visible_width = 2 * lroundf(f_scale_width*i_src_visible_width/2);
508 int i_dst_visible_height = 2 * lroundf(f_scale_height*i_src_visible_height/2);
509 int i_dst_width = 2 * lroundf(f_scale_width*p_fmt_out->video.i_width/2);
510 int i_dst_height = 2 * lroundf(f_scale_height*p_fmt_out->video.i_height/2);
512 /* Change aspect ratio from scaled pixel to output frame */
513 f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
515 /* Store calculated values */
516 id->p_encoder->fmt_out.video.i_width = i_dst_width;
517 id->p_encoder->fmt_out.video.i_visible_width = i_dst_visible_width;
518 id->p_encoder->fmt_out.video.i_height = i_dst_height;
519 id->p_encoder->fmt_out.video.i_visible_height = i_dst_visible_height;
521 id->p_encoder->fmt_in.video.i_width = i_dst_width;
522 id->p_encoder->fmt_in.video.i_visible_width = i_dst_visible_width;
523 id->p_encoder->fmt_in.video.i_height = i_dst_height;
524 id->p_encoder->fmt_in.video.i_visible_height = i_dst_visible_height;
526 msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
527 i_src_visible_width, i_src_visible_height,
528 i_dst_visible_width, i_dst_visible_height
531 /* Handle frame rate conversion */
532 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
533 !id->p_encoder->fmt_out.video.i_frame_rate_base )
535 if( p_fmt_out->video.i_frame_rate &&
536 p_fmt_out->video.i_frame_rate_base )
538 id->p_encoder->fmt_out.video.i_frame_rate =
539 p_fmt_out->video.i_frame_rate;
540 id->p_encoder->fmt_out.video.i_frame_rate_base =
541 p_fmt_out->video.i_frame_rate_base;
545 /* Pick a sensible default value */
546 id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
547 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
551 id->p_encoder->fmt_in.video.orientation =
552 id->p_encoder->fmt_out.video.orientation =
553 id->p_decoder->fmt_in.video.orientation;
555 id->p_encoder->fmt_in.video.i_frame_rate =
556 id->p_encoder->fmt_out.video.i_frame_rate;
557 id->p_encoder->fmt_in.video.i_frame_rate_base =
558 id->p_encoder->fmt_out.video.i_frame_rate_base;
560 vlc_ureduce( &id->p_encoder->fmt_in.video.i_frame_rate,
561 &id->p_encoder->fmt_in.video.i_frame_rate_base,
562 id->p_encoder->fmt_in.video.i_frame_rate,
563 id->p_encoder->fmt_in.video.i_frame_rate_base,
565 msg_Dbg( p_stream, "source fps %d/%d, destination %d/%d",
566 id->p_decoder->fmt_out.video.i_frame_rate,
567 id->p_decoder->fmt_out.video.i_frame_rate_base,
568 id->p_encoder->fmt_in.video.i_frame_rate,
569 id->p_encoder->fmt_in.video.i_frame_rate_base );
572 /* Check whether a particular aspect ratio was requested */
573 if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
574 id->p_encoder->fmt_out.video.i_sar_den <= 0 )
576 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
577 &id->p_encoder->fmt_out.video.i_sar_den,
578 (uint64_t)p_fmt_out->video.i_sar_num * i_src_visible_width * i_dst_visible_height,
579 (uint64_t)p_fmt_out->video.i_sar_den * i_src_visible_height * i_dst_visible_width,
584 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
585 &id->p_encoder->fmt_out.video.i_sar_den,
586 id->p_encoder->fmt_out.video.i_sar_num,
587 id->p_encoder->fmt_out.video.i_sar_den,
591 id->p_encoder->fmt_in.video.i_sar_num =
592 id->p_encoder->fmt_out.video.i_sar_num;
593 id->p_encoder->fmt_in.video.i_sar_den =
594 id->p_encoder->fmt_out.video.i_sar_den;
596 msg_Dbg( p_stream, "encoder aspect is %i:%i",
597 id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
598 id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
602 static int transcode_video_encoder_open( sout_stream_t *p_stream,
603 sout_stream_id_sys_t *id )
605 sout_stream_sys_t *p_sys = p_stream->p_sys;
608 msg_Dbg( p_stream, "destination (after video filters) %ix%i",
609 id->p_encoder->fmt_in.video.i_width,
610 id->p_encoder->fmt_in.video.i_height );
612 id->p_encoder->p_module =
613 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
614 if( !id->p_encoder->p_module )
616 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
617 p_sys->psz_venc ? p_sys->psz_venc : "any",
618 (char *)&p_sys->i_vcodec );
622 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
625 id->p_encoder->fmt_out.i_codec =
626 vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
628 id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
631 msg_Err( p_stream, "cannot add this stream" );
638 void transcode_video_close( sout_stream_t *p_stream,
639 sout_stream_id_sys_t *id )
641 if( p_stream->p_sys->i_threads >= 1 && !p_stream->p_sys->b_abort )
643 vlc_mutex_lock( &p_stream->p_sys->lock_out );
644 p_stream->p_sys->b_abort = true;
645 vlc_cond_signal( &p_stream->p_sys->cond );
646 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
648 vlc_join( p_stream->p_sys->thread, NULL );
650 picture_fifo_Delete( p_stream->p_sys->pp_pics );
651 block_ChainRelease( p_stream->p_sys->p_buffers );
654 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
655 vlc_cond_destroy( &p_stream->p_sys->cond );
658 if( id->p_decoder->p_module )
659 module_unneed( id->p_decoder, id->p_decoder->p_module );
660 if( id->p_decoder->p_description )
661 vlc_meta_Delete( id->p_decoder->p_description );
663 free( id->p_decoder->p_owner );
666 if( id->p_encoder->p_module )
667 module_unneed( id->p_encoder, id->p_encoder->p_module );
671 filter_chain_Delete( id->p_f_chain );
673 filter_chain_Delete( id->p_uf_chain );
676 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
678 sout_stream_sys_t *p_sys = p_stream->p_sys;
679 picture_t *p_pic2 = NULL;
684 /* Check if we have a subpicture to overlay */
687 video_format_t fmt = id->p_encoder->fmt_in.video;
688 if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
690 fmt.i_visible_width = fmt.i_width;
691 fmt.i_visible_height = fmt.i_height;
696 subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt,
697 &id->p_decoder->fmt_out.video,
698 p_pic->date, p_pic->date, false );
700 /* Overlay subpicture */
703 if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
705 /* We can't modify the picture, we need to duplicate it,
706 * in this point the picture is already p_encoder->fmt.in format*/
707 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
708 if( likely( p_tmp ) )
710 picture_Copy( p_tmp, p_pic );
711 picture_Release( p_pic );
715 if( unlikely( !p_sys->p_spu_blend ) )
716 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
717 if( likely( p_sys->p_spu_blend ) )
718 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
719 subpicture_Delete( p_subpic );
723 if( p_sys->i_threads == 0 )
727 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
728 block_ChainAppend( out, p_block );
731 if( p_sys->i_threads )
733 vlc_mutex_lock( &p_sys->lock_out );
734 picture_fifo_Push( p_sys->pp_pics, p_pic );
735 vlc_cond_signal( &p_sys->cond );
736 vlc_mutex_unlock( &p_sys->lock_out );
739 if( p_sys->i_threads && p_pic2 )
740 picture_Release( p_pic2 );
741 else if ( p_sys->i_threads == 0 )
742 picture_Release( p_pic );
745 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
746 block_t *in, block_t **out )
748 sout_stream_sys_t *p_sys = p_stream->p_sys;
749 picture_t *p_pic = NULL;
752 if( unlikely( in == NULL ) )
754 if( p_sys->i_threads == 0 )
758 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
759 block_ChainAppend( out, p_block );
764 msg_Dbg( p_stream, "Flushing thread and waiting that");
765 vlc_mutex_lock( &p_stream->p_sys->lock_out );
766 p_stream->p_sys->b_abort = true;
767 vlc_cond_signal( &p_stream->p_sys->cond );
768 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
770 vlc_join( p_stream->p_sys->thread, NULL );
771 vlc_mutex_lock( &p_sys->lock_out );
772 *out = p_sys->p_buffers;
773 p_sys->p_buffers = NULL;
774 vlc_mutex_unlock( &p_sys->lock_out );
776 msg_Dbg( p_stream, "Flushing done");
782 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
786 id->p_encoder->p_module &&
787 !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
791 msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
792 id->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
793 id->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
797 filter_chain_Delete( id->p_f_chain );
798 id->p_f_chain = NULL;
800 filter_chain_Delete( id->p_uf_chain );
801 id->p_uf_chain = NULL;
803 /* Reinitialize filters */
804 id->p_encoder->fmt_out.video.i_visible_width = p_sys->i_width & ~1;
805 id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
806 id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
808 transcode_video_filter_init( p_stream, id );
809 transcode_video_encoder_init( p_stream, id );
810 conversion_video_filter_append( id );
811 memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
815 if( unlikely( !id->p_encoder->p_module ) )
818 filter_chain_Delete( id->p_f_chain );
820 filter_chain_Delete( id->p_uf_chain );
821 id->p_f_chain = id->p_uf_chain = NULL;
823 transcode_video_filter_init( p_stream, id );
824 transcode_video_encoder_init( p_stream, id );
825 conversion_video_filter_append( id );
826 memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
828 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
830 picture_Release( p_pic );
831 transcode_video_close( p_stream, id );
832 id->b_transcode = false;
837 /* Run the filter and output chains; first with the picture,
838 * and then with NULL as many times as we need until they
839 * stop outputting frames.
842 picture_t *p_filtered_pic = p_pic;
844 /* Run filter chain */
846 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
847 if( !p_filtered_pic )
851 picture_t *p_user_filtered_pic = p_filtered_pic;
853 /* Run user specified filter chain */
855 p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
856 if( !p_user_filtered_pic )
859 OutputFrame( p_stream, p_user_filtered_pic, id, out );
861 p_filtered_pic = NULL;
868 if( p_sys->i_threads >= 1 )
870 /* Pick up any return data the encoder thread wants to output. */
871 vlc_mutex_lock( &p_sys->lock_out );
872 *out = p_sys->p_buffers;
873 p_sys->p_buffers = NULL;
874 vlc_mutex_unlock( &p_sys->lock_out );
880 bool transcode_video_add( sout_stream_t *p_stream, const es_format_t *p_fmt,
881 sout_stream_id_sys_t *id )
883 sout_stream_sys_t *p_sys = p_stream->p_sys;
886 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
887 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
889 /* Complete destination format */
890 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
891 id->p_encoder->fmt_out.video.i_visible_width = p_sys->i_width & ~1;
892 id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
893 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
895 /* Build decoder -> filter -> encoder chain */
896 if( transcode_video_new( p_stream, id ) )
898 msg_Err( p_stream, "cannot create video chain" );
902 /* Stream will be added later on because we don't know
903 * all the characteristics of the decoded stream yet */
904 id->b_transcode = true;
908 id->p_encoder->fmt_in.video.i_frame_rate = id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
909 id->p_encoder->fmt_in.video.i_frame_rate_base = id->p_encoder->fmt_out.video.i_frame_rate_base = (p_sys->fps_den ? p_sys->fps_den : 1);