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 void transcode_video_filter_init( sout_stream_t *p_stream,
249 sout_stream_id_sys_t *id )
251 filter_owner_t owner = {
252 .sys = p_stream->p_sys,
254 .buffer_new = transcode_video_filter_buffer_new,
257 es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
259 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
260 id->p_f_chain = filter_chain_NewVideo( p_stream, false, &owner );
261 filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
264 if( p_stream->p_sys->b_deinterlace )
266 filter_chain_AppendFilter( id->p_f_chain,
267 p_stream->p_sys->psz_deinterlace,
268 p_stream->p_sys->p_deinterlace_cfg,
269 &id->p_decoder->fmt_out,
270 &id->p_decoder->fmt_out );
272 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
274 if( p_stream->p_sys->b_master_sync )
276 filter_chain_AppendFilter( id->p_f_chain,
280 &id->p_encoder->fmt_in );
282 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
285 /* Check that we have visible_width/height*/
286 if( !p_fmt_out->video.i_visible_height )
287 p_fmt_out->video.i_visible_height = p_fmt_out->video.i_height;
288 if( !p_fmt_out->video.i_visible_width )
289 p_fmt_out->video.i_visible_width = p_fmt_out->video.i_width;
291 if( p_stream->p_sys->psz_vf2 )
293 id->p_uf_chain = filter_chain_NewVideo( p_stream, true, &owner );
294 filter_chain_Reset( id->p_uf_chain, p_fmt_out,
295 &id->p_encoder->fmt_in );
296 if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
298 filter_chain_AppendFilter( id->p_uf_chain,
301 &id->p_encoder->fmt_in );
303 filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
304 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
305 es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
306 id->p_encoder->fmt_out.video.i_width =
307 id->p_encoder->fmt_in.video.i_width;
308 id->p_encoder->fmt_out.video.i_height =
309 id->p_encoder->fmt_in.video.i_height;
310 id->p_encoder->fmt_out.video.i_sar_num =
311 id->p_encoder->fmt_in.video.i_sar_num;
312 id->p_encoder->fmt_out.video.i_sar_den =
313 id->p_encoder->fmt_in.video.i_sar_den;
318 /* Take care of the scaling and chroma conversions. */
319 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
321 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
323 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
326 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
328 if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
329 ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
330 ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
332 filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
335 &id->p_encoder->fmt_in );
339 static void transcode_video_encoder_init( sout_stream_t *p_stream,
340 sout_stream_id_sys_t *id )
342 sout_stream_sys_t *p_sys = p_stream->p_sys;
344 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
345 if( id->p_f_chain ) {
346 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
348 if( id->p_uf_chain ) {
349 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
353 * width/height of source */
354 int i_src_visible_width = p_fmt_out->video.i_visible_width;
355 int i_src_visible_height = p_fmt_out->video.i_visible_height;
357 if (i_src_visible_width == 0)
358 i_src_visible_width = p_fmt_out->video.i_width;
359 if (i_src_visible_height == 0)
360 i_src_visible_height = p_fmt_out->video.i_height;
363 /* with/height scaling */
364 float f_scale_width = 1;
365 float f_scale_height = 1;
368 float f_aspect = (double)p_fmt_out->video.i_sar_num *
369 p_fmt_out->video.i_width /
370 p_fmt_out->video.i_sar_den /
371 p_fmt_out->video.i_height;
373 msg_Dbg( p_stream, "decoder aspect is %f:1", (double) f_aspect );
375 /* Change f_aspect from source frame to source pixel */
376 f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
377 msg_Dbg( p_stream, "source pixel aspect is %f:1", (double) f_aspect );
379 /* Calculate scaling factor for specified parameters */
380 if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
381 id->p_encoder->fmt_out.video.i_visible_height <= 0 && p_sys->f_scale )
383 /* Global scaling. Make sure width will remain a factor of 16 */
386 int i_new_width = i_src_visible_width * p_sys->f_scale;
388 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
389 i_new_width -= i_new_width % 16;
391 i_new_width += 16 - i_new_width % 16;
393 f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
395 i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
397 f_scale_width = f_real_scale;
398 f_scale_height = (float) i_new_height / (float) i_src_visible_height;
400 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
401 id->p_encoder->fmt_out.video.i_visible_height <= 0 )
403 /* Only width specified */
404 f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
405 f_scale_height = f_scale_width;
407 else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
408 id->p_encoder->fmt_out.video.i_visible_height > 0 )
410 /* Only height specified */
411 f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
412 f_scale_width = f_scale_height;
414 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
415 id->p_encoder->fmt_out.video.i_visible_height > 0 )
417 /* Width and height specified */
418 f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
419 f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
422 /* check maxwidth and maxheight */
423 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
424 i_src_visible_width )
426 f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
429 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
430 i_src_visible_height )
432 f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
436 /* Change aspect ratio from source pixel to scaled pixel */
437 f_aspect = f_aspect * f_scale_height / f_scale_width;
438 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", (double) f_aspect );
440 /* f_scale_width and f_scale_height are now final */
441 /* Calculate width, height from scaling
442 * Make sure its multiple of 2
444 /* width/height of output stream */
445 int i_dst_visible_width = 2 * lroundf(f_scale_width*i_src_visible_width/2);
446 int i_dst_visible_height = 2 * lroundf(f_scale_height*i_src_visible_height/2);
447 int i_dst_width = 2 * lroundf(f_scale_width*p_fmt_out->video.i_width/2);
448 int i_dst_height = 2 * lroundf(f_scale_height*p_fmt_out->video.i_height/2);
450 /* Change aspect ratio from scaled pixel to output frame */
451 f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
453 /* Store calculated values */
454 id->p_encoder->fmt_out.video.i_width = i_dst_width;
455 id->p_encoder->fmt_out.video.i_visible_width = i_dst_visible_width;
456 id->p_encoder->fmt_out.video.i_height = i_dst_height;
457 id->p_encoder->fmt_out.video.i_visible_height = i_dst_visible_height;
459 id->p_encoder->fmt_in.video.i_width = i_dst_width;
460 id->p_encoder->fmt_in.video.i_visible_width = i_dst_visible_width;
461 id->p_encoder->fmt_in.video.i_height = i_dst_height;
462 id->p_encoder->fmt_in.video.i_visible_height = i_dst_visible_height;
464 msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
465 i_src_visible_width, i_src_visible_height,
466 i_dst_visible_width, i_dst_visible_height
469 /* Handle frame rate conversion */
470 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
471 !id->p_encoder->fmt_out.video.i_frame_rate_base )
473 if( p_fmt_out->video.i_frame_rate &&
474 p_fmt_out->video.i_frame_rate_base )
476 id->p_encoder->fmt_out.video.i_frame_rate =
477 p_fmt_out->video.i_frame_rate;
478 id->p_encoder->fmt_out.video.i_frame_rate_base =
479 p_fmt_out->video.i_frame_rate_base;
483 /* Pick a sensible default value */
484 id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
485 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
489 id->p_encoder->fmt_in.video.orientation =
490 id->p_encoder->fmt_out.video.orientation =
491 id->p_decoder->fmt_in.video.orientation;
493 id->p_encoder->fmt_in.video.i_frame_rate =
494 id->p_encoder->fmt_out.video.i_frame_rate;
495 id->p_encoder->fmt_in.video.i_frame_rate_base =
496 id->p_encoder->fmt_out.video.i_frame_rate_base;
498 vlc_ureduce( &id->p_encoder->fmt_in.video.i_frame_rate,
499 &id->p_encoder->fmt_in.video.i_frame_rate_base,
500 id->p_encoder->fmt_in.video.i_frame_rate,
501 id->p_encoder->fmt_in.video.i_frame_rate_base,
503 msg_Dbg( p_stream, "source fps %d/%d, destination %d/%d",
504 id->p_decoder->fmt_out.video.i_frame_rate,
505 id->p_decoder->fmt_out.video.i_frame_rate_base,
506 id->p_encoder->fmt_in.video.i_frame_rate,
507 id->p_encoder->fmt_in.video.i_frame_rate_base );
510 /* Check whether a particular aspect ratio was requested */
511 if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
512 id->p_encoder->fmt_out.video.i_sar_den <= 0 )
514 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
515 &id->p_encoder->fmt_out.video.i_sar_den,
516 (uint64_t)p_fmt_out->video.i_sar_num * i_src_visible_width * i_dst_visible_height,
517 (uint64_t)p_fmt_out->video.i_sar_den * i_src_visible_height * i_dst_visible_width,
522 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
523 &id->p_encoder->fmt_out.video.i_sar_den,
524 id->p_encoder->fmt_out.video.i_sar_num,
525 id->p_encoder->fmt_out.video.i_sar_den,
529 id->p_encoder->fmt_in.video.i_sar_num =
530 id->p_encoder->fmt_out.video.i_sar_num;
531 id->p_encoder->fmt_in.video.i_sar_den =
532 id->p_encoder->fmt_out.video.i_sar_den;
534 msg_Dbg( p_stream, "encoder aspect is %i:%i",
535 id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
536 id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
540 static int transcode_video_encoder_open( sout_stream_t *p_stream,
541 sout_stream_id_sys_t *id )
543 sout_stream_sys_t *p_sys = p_stream->p_sys;
546 msg_Dbg( p_stream, "destination (after video filters) %ix%i",
547 id->p_encoder->fmt_in.video.i_width,
548 id->p_encoder->fmt_in.video.i_height );
550 id->p_encoder->p_module =
551 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
552 if( !id->p_encoder->p_module )
554 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
555 p_sys->psz_venc ? p_sys->psz_venc : "any",
556 (char *)&p_sys->i_vcodec );
560 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
563 id->p_encoder->fmt_out.i_codec =
564 vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
566 id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
569 msg_Err( p_stream, "cannot add this stream" );
576 void transcode_video_close( sout_stream_t *p_stream,
577 sout_stream_id_sys_t *id )
579 if( p_stream->p_sys->i_threads >= 1 && !p_stream->p_sys->b_abort )
581 vlc_mutex_lock( &p_stream->p_sys->lock_out );
582 p_stream->p_sys->b_abort = true;
583 vlc_cond_signal( &p_stream->p_sys->cond );
584 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
586 vlc_join( p_stream->p_sys->thread, NULL );
588 picture_fifo_Delete( p_stream->p_sys->pp_pics );
589 block_ChainRelease( p_stream->p_sys->p_buffers );
592 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
593 vlc_cond_destroy( &p_stream->p_sys->cond );
596 if( id->p_decoder->p_module )
597 module_unneed( id->p_decoder, id->p_decoder->p_module );
598 if( id->p_decoder->p_description )
599 vlc_meta_Delete( id->p_decoder->p_description );
601 free( id->p_decoder->p_owner );
604 if( id->p_encoder->p_module )
605 module_unneed( id->p_encoder, id->p_encoder->p_module );
609 filter_chain_Delete( id->p_f_chain );
611 filter_chain_Delete( id->p_uf_chain );
614 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
616 sout_stream_sys_t *p_sys = p_stream->p_sys;
617 picture_t *p_pic2 = NULL;
622 /* Check if we have a subpicture to overlay */
625 video_format_t fmt = id->p_encoder->fmt_in.video;
626 if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
628 fmt.i_visible_width = fmt.i_width;
629 fmt.i_visible_height = fmt.i_height;
634 subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt,
635 &id->p_decoder->fmt_out.video,
636 p_pic->date, p_pic->date, false );
638 /* Overlay subpicture */
641 if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
643 /* We can't modify the picture, we need to duplicate it,
644 * in this point the picture is already p_encoder->fmt.in format*/
645 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
646 if( likely( p_tmp ) )
648 picture_Copy( p_tmp, p_pic );
649 picture_Release( p_pic );
653 if( unlikely( !p_sys->p_spu_blend ) )
654 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
655 if( likely( p_sys->p_spu_blend ) )
656 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
657 subpicture_Delete( p_subpic );
661 if( p_sys->i_threads == 0 )
665 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
666 block_ChainAppend( out, p_block );
669 if( p_sys->i_threads )
671 vlc_mutex_lock( &p_sys->lock_out );
672 picture_fifo_Push( p_sys->pp_pics, p_pic );
673 vlc_cond_signal( &p_sys->cond );
674 vlc_mutex_unlock( &p_sys->lock_out );
677 if( p_sys->i_threads && p_pic2 )
678 picture_Release( p_pic2 );
679 else if ( p_sys->i_threads == 0 )
680 picture_Release( p_pic );
683 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
684 block_t *in, block_t **out )
686 sout_stream_sys_t *p_sys = p_stream->p_sys;
687 picture_t *p_pic = NULL;
690 if( unlikely( in == NULL ) )
692 if( p_sys->i_threads == 0 )
696 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
697 block_ChainAppend( out, p_block );
702 msg_Dbg( p_stream, "Flushing thread and waiting that");
703 vlc_mutex_lock( &p_stream->p_sys->lock_out );
704 p_stream->p_sys->b_abort = true;
705 vlc_cond_signal( &p_stream->p_sys->cond );
706 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
708 vlc_join( p_stream->p_sys->thread, NULL );
709 vlc_mutex_lock( &p_sys->lock_out );
710 *out = p_sys->p_buffers;
711 p_sys->p_buffers = NULL;
712 vlc_mutex_unlock( &p_sys->lock_out );
714 msg_Dbg( p_stream, "Flushing done");
720 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
724 id->p_encoder->p_module &&
725 !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
729 msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
730 id->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
731 id->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
735 filter_chain_Delete( id->p_f_chain );
736 id->p_f_chain = NULL;
738 filter_chain_Delete( id->p_uf_chain );
739 id->p_uf_chain = NULL;
741 /* Reinitialize filters */
742 id->p_encoder->fmt_out.video.i_visible_width = p_sys->i_width & ~1;
743 id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
744 id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
746 transcode_video_filter_init( p_stream, id );
747 transcode_video_encoder_init( p_stream, id );
748 conversion_video_filter_append( id );
749 memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
753 if( unlikely( !id->p_encoder->p_module ) )
756 filter_chain_Delete( id->p_f_chain );
758 filter_chain_Delete( id->p_uf_chain );
759 id->p_f_chain = id->p_uf_chain = NULL;
761 transcode_video_filter_init( p_stream, id );
762 transcode_video_encoder_init( p_stream, id );
763 conversion_video_filter_append( id );
764 memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
766 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
768 picture_Release( p_pic );
769 transcode_video_close( p_stream, id );
770 id->b_transcode = false;
775 /* Run the filter and output chains; first with the picture,
776 * and then with NULL as many times as we need until they
777 * stop outputting frames.
780 picture_t *p_filtered_pic = p_pic;
782 /* Run filter chain */
784 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
785 if( !p_filtered_pic )
789 picture_t *p_user_filtered_pic = p_filtered_pic;
791 /* Run user specified filter chain */
793 p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
794 if( !p_user_filtered_pic )
797 OutputFrame( p_stream, p_user_filtered_pic, id, out );
799 p_filtered_pic = NULL;
806 if( p_sys->i_threads >= 1 )
808 /* Pick up any return data the encoder thread wants to output. */
809 vlc_mutex_lock( &p_sys->lock_out );
810 *out = p_sys->p_buffers;
811 p_sys->p_buffers = NULL;
812 vlc_mutex_unlock( &p_sys->lock_out );
818 bool transcode_video_add( sout_stream_t *p_stream, const es_format_t *p_fmt,
819 sout_stream_id_sys_t *id )
821 sout_stream_sys_t *p_sys = p_stream->p_sys;
824 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
825 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
827 /* Complete destination format */
828 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
829 id->p_encoder->fmt_out.video.i_visible_width = p_sys->i_width & ~1;
830 id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
831 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
833 /* Build decoder -> filter -> encoder chain */
834 if( transcode_video_new( p_stream, id ) )
836 msg_Err( p_stream, "cannot create video chain" );
840 /* Stream will be added later on because we don't know
841 * all the characteristics of the decoded stream yet */
842 id->b_transcode = true;
846 id->p_encoder->fmt_in.video.i_frame_rate = id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
847 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);