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"
35 #include <vlc_modules.h>
37 #define ENC_FRAMERATE (25 * 1000)
38 #define ENC_FRAMERATE_BASE 1000
40 struct decoder_owner_sys_t
42 sout_stream_sys_t *p_sys;
45 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
47 VLC_UNUSED(p_decoder);
48 picture_Release( p_pic );
51 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
54 picture_Hold( p_pic );
57 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
60 picture_Release( p_pic );
63 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
65 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
66 return picture_NewFromFormat( &p_dec->fmt_out.video );
69 static picture_t *video_new_buffer_encoder( encoder_t *p_enc )
71 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
72 return picture_NewFromFormat( &p_enc->fmt_in.video );
75 static picture_t *transcode_video_filter_buffer_new( filter_t *p_filter )
77 p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
78 return picture_NewFromFormat( &p_filter->fmt_out.video );
81 static void transcode_video_filter_buffer_del( filter_t *p_filter, picture_t *p_pic )
84 picture_Release( p_pic );
87 static void* EncoderThread( void *obj )
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;
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 );
103 if( p_sys->b_abort && !p_pic )
105 vlc_mutex_unlock( &p_sys->lock_out );
108 vlc_mutex_unlock( &p_sys->lock_out );
112 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
114 vlc_mutex_lock( &p_sys->lock_out );
115 block_ChainAppend( &p_sys->p_buffers, p_block );
117 vlc_mutex_unlock( &p_sys->lock_out );
118 picture_Release( p_pic );
121 vlc_mutex_lock( &p_sys->lock_out );
124 vlc_mutex_unlock( &p_sys->lock_out );
127 vlc_mutex_unlock( &p_sys->lock_out );
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 )
134 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
136 block_ChainAppend( &p_sys->p_buffers, p_block );
138 picture_Release( p_pic );
141 /*Now flush encoder*/
143 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
144 block_ChainAppend( &p_sys->p_buffers, p_block );
147 vlc_mutex_unlock( &p_sys->lock_out );
150 vlc_restorecancel (canc);
154 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
156 sout_stream_sys_t *p_sys = p_stream->p_sys;
159 * Initialization of decoder structures
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 )
175 id->p_decoder->p_owner->p_sys = p_sys;
176 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
178 id->p_decoder->p_module =
179 module_need( id->p_decoder, "decoder", "$codec", false );
181 if( !id->p_decoder->p_module )
183 msg_Err( p_stream, "cannot find video decoder" );
184 free( id->p_decoder->p_owner );
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.
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;
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;
223 id->p_encoder->i_threads = p_sys->i_threads;
224 id->p_encoder->p_cfg = p_sys->p_video_cfg;
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 )
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 );
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 )
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;
248 id->p_encoder->p_module = NULL;
250 if( p_sys->i_threads >= 1 )
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 )
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 );
268 p_sys->p_buffers = NULL;
269 p_sys->b_abort = false;
270 if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
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 );
285 static void transcode_video_filter_init( sout_stream_t *p_stream,
286 sout_stream_id_sys_t *id )
288 filter_owner_t owner = {
289 .sys = p_stream->p_sys,
291 .buffer_new = transcode_video_filter_buffer_new,
292 .buffer_del = transcode_video_filter_buffer_del,
295 es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
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 );
302 if( p_stream->p_sys->b_deinterlace )
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 );
310 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
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;
319 if( p_stream->p_sys->psz_vf2 )
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 )
326 filter_chain_AppendFilter( id->p_uf_chain,
329 &id->p_encoder->fmt_in );
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;
346 /* Take care of the scaling and chroma conversions. */
347 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
349 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
351 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
354 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
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 ) )
360 filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
363 &id->p_encoder->fmt_in );
367 static void transcode_video_encoder_init( sout_stream_t *p_stream,
368 sout_stream_id_sys_t *id )
370 sout_stream_sys_t *p_sys = p_stream->p_sys;
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 );
376 if( id->p_uf_chain ) {
377 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
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;
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;
391 /* with/height scaling */
392 float f_scale_width = 1;
393 float f_scale_height = 1;
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;
401 msg_Dbg( p_stream, "decoder aspect is %f:1", f_aspect );
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 );
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 )
411 /* Global scaling. Make sure width will remain a factor of 16 */
414 int i_new_width = i_src_visible_width * p_sys->f_scale;
416 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
417 i_new_width -= i_new_width % 16;
419 i_new_width += 16 - i_new_width % 16;
421 f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
423 i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
425 f_scale_width = f_real_scale;
426 f_scale_height = (float) i_new_height / (float) i_src_visible_height;
428 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
429 id->p_encoder->fmt_out.video.i_visible_height <= 0 )
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;
435 else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
436 id->p_encoder->fmt_out.video.i_visible_height > 0 )
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;
442 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
443 id->p_encoder->fmt_out.video.i_visible_height > 0 )
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;
450 /* check maxwidth and maxheight */
451 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
452 i_src_visible_width )
454 f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
457 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
458 i_src_visible_height )
460 f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
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 );
468 /* f_scale_width and f_scale_height are now final */
469 /* Calculate width, height from scaling
470 * Make sure its multiple of 2
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);
478 /* Change aspect ratio from scaled pixel to output frame */
479 f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
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;
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;
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
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 )
501 if( p_fmt_out->video.i_frame_rate &&
502 p_fmt_out->video.i_frame_rate_base )
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;
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;
517 id->p_encoder->fmt_in.video.orientation =
518 id->p_encoder->fmt_out.video.orientation =
519 id->p_decoder->fmt_in.video.orientation;
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;
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,
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 );
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 );
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 );
545 date_Init( &id->next_input_pts,
546 id->p_decoder->fmt_out.video.i_frame_rate,
549 date_Init( &id->next_output_pts,
550 id->p_encoder->fmt_in.video.i_frame_rate,
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 )
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,
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,
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;
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 );
583 static int transcode_video_encoder_open( sout_stream_t *p_stream,
584 sout_stream_id_sys_t *id )
586 sout_stream_sys_t *p_sys = p_stream->p_sys;
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 );
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 )
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 );
603 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
606 id->p_encoder->fmt_out.i_codec =
607 vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
609 id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
612 msg_Err( p_stream, "cannot add this stream" );
619 void transcode_video_close( sout_stream_t *p_stream,
620 sout_stream_id_sys_t *id )
622 if( p_stream->p_sys->i_threads >= 1 )
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 );
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 );
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;
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 );
644 free( id->p_decoder->p_owner );
647 if( id->p_encoder->p_module )
648 module_unneed( id->p_encoder, id->p_encoder->p_module );
652 filter_chain_Delete( id->p_f_chain );
654 filter_chain_Delete( id->p_uf_chain );
657 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
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
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
670 if( ( original_date ) <
671 ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
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) );
677 picture_Release( p_pic );
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, &fmt,
697 p_pic->date, p_pic->date, false );
699 /* Overlay subpicture */
702 if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
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 ) )
709 picture_Copy( p_tmp, p_pic );
710 picture_Release( p_pic );
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 );
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 );
727 if( p_sys->i_threads == 0 )
731 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
732 block_ChainAppend( out, p_block );
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 ) <
739 if( p_sys->i_threads )
741 if( p_sys->b_master_sync )
743 p_pic2 = video_new_buffer_encoder( id->p_encoder );
744 if( likely( p_pic2 != NULL ) )
745 picture_Copy( p_pic2, p_pic );
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 );
753 while( (p_sys->b_master_sync && b_need_duplicate ))
755 if( p_sys->i_threads >= 1 )
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 ) )
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 );
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 );
778 msg_Dbg( p_stream, "duplicated frame");
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 ) <
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 );
791 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
792 block_t *in, block_t **out )
794 sout_stream_sys_t *p_sys = p_stream->p_sys;
795 picture_t *p_pic = NULL;
798 if( unlikely( in == NULL ) )
800 if( p_sys->i_threads == 0 )
804 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
805 block_ChainAppend( out, p_block );
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 );
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 );
822 msg_Dbg( p_stream, "Flushing done");
828 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
832 id->p_encoder->p_module &&
833 !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
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
843 filter_chain_Delete( id->p_f_chain );
844 id->p_f_chain = NULL;
846 filter_chain_Delete( id->p_uf_chain );
847 id->p_uf_chain = NULL;
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;
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));
861 if( unlikely( !id->p_encoder->p_module ) )
864 filter_chain_Delete( id->p_f_chain );
866 filter_chain_Delete( id->p_uf_chain );
867 id->p_f_chain = id->p_uf_chain = NULL;
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));
874 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
876 picture_Release( p_pic );
877 transcode_video_close( p_stream, id );
878 id->b_transcode = false;
881 date_Set( &id->next_output_pts, p_pic->date );
882 date_Set( &id->next_input_pts, p_pic->date );
885 /*Input lipsync and drop check */
886 if( p_sys->b_master_sync )
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
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
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 ) )
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) );
902 picture_Release( p_pic );
906 msg_Dbg( p_stream, "not dropping frame");
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 ) )
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))
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 );
924 date_Increment( &id->next_input_pts, id->p_decoder->fmt_out.video.i_frame_rate_base );
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.
931 picture_t *p_filtered_pic = p_pic;
933 /* Run filter chain */
935 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
936 if( !p_filtered_pic )
940 picture_t *p_user_filtered_pic = p_filtered_pic;
942 /* Run user specified filter chain */
944 p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
945 if( !p_user_filtered_pic )
948 OutputFrame( p_stream, p_user_filtered_pic, id, out );
950 p_filtered_pic = NULL;
957 if( p_sys->i_threads >= 1 )
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 );
969 bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
970 sout_stream_id_sys_t *id )
972 sout_stream_sys_t *p_sys = p_stream->p_sys;
975 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
976 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
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;
984 /* Build decoder -> filter -> encoder chain */
985 if( transcode_video_new( p_stream, id ) )
987 msg_Err( p_stream, "cannot create video chain" );
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;
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);