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 );
80 static void transcode_video_filter_buffer_del( filter_t *p_filter, picture_t *p_pic )
83 picture_Release( p_pic );
86 static int transcode_video_filter_allocation_init( filter_t *p_filter,
90 p_filter->pf_video_buffer_new = transcode_video_filter_buffer_new;
91 p_filter->pf_video_buffer_del = transcode_video_filter_buffer_del;
95 static void transcode_video_filter_allocation_clear( filter_t *p_filter )
100 static void* EncoderThread( void *obj )
102 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)obj;
103 sout_stream_id_sys_t *id = p_sys->id_video;
104 picture_t *p_pic = NULL;
105 int canc = vlc_savecancel ();
106 block_t *p_block = NULL;
111 vlc_mutex_lock( &p_sys->lock_out );
112 while( !p_sys->b_abort &&
113 (p_pic = picture_fifo_Pop( p_sys->pp_pics )) == NULL )
114 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
116 if( p_sys->b_abort && !p_pic )
118 vlc_mutex_unlock( &p_sys->lock_out );
121 vlc_mutex_unlock( &p_sys->lock_out );
125 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
127 vlc_mutex_lock( &p_sys->lock_out );
128 block_ChainAppend( &p_sys->p_buffers, p_block );
130 vlc_mutex_unlock( &p_sys->lock_out );
131 picture_Release( p_pic );
134 vlc_mutex_lock( &p_sys->lock_out );
137 vlc_mutex_unlock( &p_sys->lock_out );
140 vlc_mutex_unlock( &p_sys->lock_out );
143 /*Encode what we have in the buffer on closing*/
144 vlc_mutex_lock( &p_sys->lock_out );
145 while( (p_pic = picture_fifo_Pop( p_sys->pp_pics )) != NULL )
147 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
149 block_ChainAppend( &p_sys->p_buffers, p_block );
151 picture_Release( p_pic );
154 /*Now flush encoder*/
156 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
157 block_ChainAppend( &p_sys->p_buffers, p_block );
160 vlc_mutex_unlock( &p_sys->lock_out );
163 vlc_restorecancel (canc);
167 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
169 sout_stream_sys_t *p_sys = p_stream->p_sys;
172 * Initialization of decoder structures
174 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
175 id->p_decoder->fmt_out.i_extra = 0;
176 id->p_decoder->fmt_out.p_extra = 0;
177 id->p_decoder->pf_decode_video = NULL;
178 id->p_decoder->pf_get_cc = NULL;
179 id->p_decoder->pf_get_cc = 0;
180 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
181 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
182 id->p_decoder->pf_picture_link = video_link_picture_decoder;
183 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
184 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
185 if( !id->p_decoder->p_owner )
188 id->p_decoder->p_owner->p_sys = p_sys;
189 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
191 id->p_decoder->p_module =
192 module_need( id->p_decoder, "decoder", "$codec", false );
194 if( !id->p_decoder->p_module )
196 msg_Err( p_stream, "cannot find video decoder" );
197 free( id->p_decoder->p_owner );
203 * Because some info about the decoded input will only be available
204 * once the first frame is decoded, we actually only test the availability
205 * of the encoder here.
208 /* Initialization of encoder format structures */
209 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
210 id->p_decoder->fmt_out.i_codec );
211 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
213 /* The dimensions will be set properly later on.
214 * Just put sensible values so we can test an encoder is available. */
215 id->p_encoder->fmt_in.video.i_width =
216 id->p_encoder->fmt_out.video.i_width
217 ? id->p_encoder->fmt_out.video.i_width
218 : id->p_decoder->fmt_in.video.i_width
219 ? id->p_decoder->fmt_in.video.i_width : 16;
220 id->p_encoder->fmt_in.video.i_height =
221 id->p_encoder->fmt_out.video.i_height
222 ? id->p_encoder->fmt_out.video.i_height
223 : id->p_decoder->fmt_in.video.i_height
224 ? id->p_decoder->fmt_in.video.i_height : 16;
225 id->p_encoder->fmt_in.video.i_visible_width =
226 id->p_encoder->fmt_out.video.i_visible_width
227 ? id->p_encoder->fmt_out.video.i_visible_width
228 : id->p_decoder->fmt_in.video.i_visible_width
229 ? id->p_decoder->fmt_in.video.i_visible_width : id->p_encoder->fmt_in.video.i_width;
230 id->p_encoder->fmt_in.video.i_visible_height =
231 id->p_encoder->fmt_out.video.i_visible_height
232 ? id->p_encoder->fmt_out.video.i_visible_height
233 : id->p_decoder->fmt_in.video.i_visible_height
234 ? id->p_decoder->fmt_in.video.i_visible_height : id->p_encoder->fmt_in.video.i_height;
236 id->p_encoder->i_threads = p_sys->i_threads;
237 id->p_encoder->p_cfg = p_sys->p_video_cfg;
239 id->p_encoder->p_module =
240 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
241 if( !id->p_encoder->p_module )
243 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s). Take a look few lines earlier to see possible reason.",
244 p_sys->psz_venc ? p_sys->psz_venc : "any",
245 (char *)&p_sys->i_vcodec );
246 module_unneed( id->p_decoder, id->p_decoder->p_module );
247 id->p_decoder->p_module = 0;
248 free( id->p_decoder->p_owner );
252 /* Close the encoder.
253 * We'll open it only when we have the first frame. */
254 module_unneed( id->p_encoder, id->p_encoder->p_module );
255 if( id->p_encoder->fmt_out.p_extra )
257 free( id->p_encoder->fmt_out.p_extra );
258 id->p_encoder->fmt_out.p_extra = NULL;
259 id->p_encoder->fmt_out.i_extra = 0;
261 id->p_encoder->p_module = NULL;
263 if( p_sys->i_threads >= 1 )
265 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
266 VLC_THREAD_PRIORITY_VIDEO;
267 p_sys->id_video = id;
268 vlc_mutex_init( &p_sys->lock_out );
269 vlc_cond_init( &p_sys->cond );
270 p_sys->pp_pics = picture_fifo_New();
271 if( p_sys->pp_pics == NULL )
273 msg_Err( p_stream, "cannot create picture fifo" );
274 vlc_mutex_destroy( &p_sys->lock_out );
275 vlc_cond_destroy( &p_sys->cond );
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 );
281 p_sys->p_buffers = NULL;
282 p_sys->b_abort = false;
283 if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
285 msg_Err( p_stream, "cannot spawn encoder thread" );
286 vlc_mutex_destroy( &p_sys->lock_out );
287 vlc_cond_destroy( &p_sys->cond );
288 picture_fifo_Delete( p_sys->pp_pics );
289 module_unneed( id->p_decoder, id->p_decoder->p_module );
290 id->p_decoder->p_module = NULL;
291 free( id->p_decoder->p_owner );
298 static void transcode_video_filter_init( sout_stream_t *p_stream,
299 sout_stream_id_sys_t *id )
301 es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
302 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
304 id->p_f_chain = filter_chain_New( p_stream, "video filter2",
306 transcode_video_filter_allocation_init,
307 transcode_video_filter_allocation_clear,
309 filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
312 if( p_stream->p_sys->b_deinterlace )
314 filter_chain_AppendFilter( id->p_f_chain,
315 p_stream->p_sys->psz_deinterlace,
316 p_stream->p_sys->p_deinterlace_cfg,
317 &id->p_decoder->fmt_out,
318 &id->p_decoder->fmt_out );
320 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
323 /* Check that we have visible_width/height*/
324 if( !p_fmt_out->video.i_visible_height )
325 p_fmt_out->video.i_visible_height = p_fmt_out->video.i_height;
326 if( !p_fmt_out->video.i_visible_width )
327 p_fmt_out->video.i_visible_width = p_fmt_out->video.i_width;
329 if( p_stream->p_sys->psz_vf2 )
331 id->p_uf_chain = filter_chain_New( p_stream, "video filter2",
333 transcode_video_filter_allocation_init,
334 transcode_video_filter_allocation_clear,
336 filter_chain_Reset( id->p_uf_chain, p_fmt_out,
337 &id->p_encoder->fmt_in );
338 if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
340 filter_chain_AppendFilter( id->p_uf_chain,
343 &id->p_encoder->fmt_in );
345 filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
346 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
347 es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
348 id->p_encoder->fmt_out.video.i_width =
349 id->p_encoder->fmt_in.video.i_width;
350 id->p_encoder->fmt_out.video.i_height =
351 id->p_encoder->fmt_in.video.i_height;
352 id->p_encoder->fmt_out.video.i_sar_num =
353 id->p_encoder->fmt_in.video.i_sar_num;
354 id->p_encoder->fmt_out.video.i_sar_den =
355 id->p_encoder->fmt_in.video.i_sar_den;
360 /* Take care of the scaling and chroma conversions. */
361 static void conversion_video_filter_append( sout_stream_id_sys_t *id )
363 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
365 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
368 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
370 if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
371 ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
372 ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
374 filter_chain_AppendFilter( id->p_uf_chain ? id->p_uf_chain : id->p_f_chain,
377 &id->p_encoder->fmt_in );
381 static void transcode_video_encoder_init( sout_stream_t *p_stream,
382 sout_stream_id_sys_t *id )
384 sout_stream_sys_t *p_sys = p_stream->p_sys;
386 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
387 if( id->p_f_chain ) {
388 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
390 if( id->p_uf_chain ) {
391 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
395 * width/height of source */
396 int i_src_visible_width = p_fmt_out->video.i_visible_width;
397 int i_src_visible_height = p_fmt_out->video.i_visible_height;
399 if (i_src_visible_width == 0)
400 i_src_visible_width = p_fmt_out->video.i_width;
401 if (i_src_visible_height == 0)
402 i_src_visible_height = p_fmt_out->video.i_height;
405 /* with/height scaling */
406 float f_scale_width = 1;
407 float f_scale_height = 1;
410 float f_aspect = (double)p_fmt_out->video.i_sar_num *
411 p_fmt_out->video.i_width /
412 p_fmt_out->video.i_sar_den /
413 p_fmt_out->video.i_height;
415 msg_Dbg( p_stream, "decoder aspect is %f:1", f_aspect );
417 /* Change f_aspect from source frame to source pixel */
418 f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
419 msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
421 /* Calculate scaling factor for specified parameters */
422 if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
423 id->p_encoder->fmt_out.video.i_visible_height <= 0 && p_sys->f_scale )
425 /* Global scaling. Make sure width will remain a factor of 16 */
428 int i_new_width = i_src_visible_width * p_sys->f_scale;
430 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
431 i_new_width -= i_new_width % 16;
433 i_new_width += 16 - i_new_width % 16;
435 f_real_scale = (float)( i_new_width ) / (float) i_src_visible_width;
437 i_new_height = __MAX( 16, i_src_visible_height * (float)f_real_scale );
439 f_scale_width = f_real_scale;
440 f_scale_height = (float) i_new_height / (float) i_src_visible_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 /* Only width specified */
446 f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
447 f_scale_height = f_scale_width;
449 else if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
450 id->p_encoder->fmt_out.video.i_visible_height > 0 )
452 /* Only height specified */
453 f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
454 f_scale_width = f_scale_height;
456 else if( id->p_encoder->fmt_out.video.i_visible_width > 0 &&
457 id->p_encoder->fmt_out.video.i_visible_height > 0 )
459 /* Width and height specified */
460 f_scale_width = (float)id->p_encoder->fmt_out.video.i_visible_width/i_src_visible_width;
461 f_scale_height = (float)id->p_encoder->fmt_out.video.i_visible_height/i_src_visible_height;
464 /* check maxwidth and maxheight */
465 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
466 i_src_visible_width )
468 f_scale_width = (float)p_sys->i_maxwidth / i_src_visible_width;
471 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
472 i_src_visible_height )
474 f_scale_height = (float)p_sys->i_maxheight / i_src_visible_height;
478 /* Change aspect ratio from source pixel to scaled pixel */
479 f_aspect = f_aspect * f_scale_height / f_scale_width;
480 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
482 /* f_scale_width and f_scale_height are now final */
483 /* Calculate width, height from scaling
484 * Make sure its multiple of 2
486 /* width/height of output stream */
487 int i_dst_visible_width = 2 * (int)(f_scale_width*i_src_visible_width/2+0.5);
488 int i_dst_visible_height = 2 * (int)(f_scale_height*i_src_visible_height/2+0.5);
489 int i_dst_width = 2 * (int)(f_scale_width*p_fmt_out->video.i_width/2+0.5);
490 int i_dst_height = 2 * (int)(f_scale_height*p_fmt_out->video.i_height/2+0.5);
492 /* Change aspect ratio from scaled pixel to output frame */
493 f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
495 /* Store calculated values */
496 id->p_encoder->fmt_out.video.i_width = i_dst_width;
497 id->p_encoder->fmt_out.video.i_visible_width = i_dst_visible_width;
498 id->p_encoder->fmt_out.video.i_height = i_dst_height;
499 id->p_encoder->fmt_out.video.i_visible_height = i_dst_visible_height;
501 id->p_encoder->fmt_in.video.i_width = i_dst_width;
502 id->p_encoder->fmt_in.video.i_visible_width = i_dst_visible_width;
503 id->p_encoder->fmt_in.video.i_height = i_dst_height;
504 id->p_encoder->fmt_in.video.i_visible_height = i_dst_visible_height;
506 msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
507 i_src_visible_width, i_src_visible_height,
508 i_dst_visible_width, i_dst_visible_height
511 /* Handle frame rate conversion */
512 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
513 !id->p_encoder->fmt_out.video.i_frame_rate_base )
515 if( p_fmt_out->video.i_frame_rate &&
516 p_fmt_out->video.i_frame_rate_base )
518 id->p_encoder->fmt_out.video.i_frame_rate =
519 p_fmt_out->video.i_frame_rate;
520 id->p_encoder->fmt_out.video.i_frame_rate_base =
521 p_fmt_out->video.i_frame_rate_base;
525 /* Pick a sensible default value */
526 id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
527 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
531 id->p_encoder->fmt_in.video.orientation =
532 id->p_encoder->fmt_out.video.orientation =
533 id->p_decoder->fmt_in.video.orientation;
535 id->p_encoder->fmt_in.video.i_frame_rate =
536 id->p_encoder->fmt_out.video.i_frame_rate;
537 id->p_encoder->fmt_in.video.i_frame_rate_base =
538 id->p_encoder->fmt_out.video.i_frame_rate_base;
540 vlc_ureduce( &id->p_encoder->fmt_in.video.i_frame_rate,
541 &id->p_encoder->fmt_in.video.i_frame_rate_base,
542 id->p_encoder->fmt_in.video.i_frame_rate,
543 id->p_encoder->fmt_in.video.i_frame_rate_base,
545 msg_Dbg( p_stream, "source fps %d/%d, destination %d/%d",
546 id->p_decoder->fmt_out.video.i_frame_rate,
547 id->p_decoder->fmt_out.video.i_frame_rate_base,
548 id->p_encoder->fmt_in.video.i_frame_rate,
549 id->p_encoder->fmt_in.video.i_frame_rate_base );
551 id->i_output_frame_interval = id->p_encoder->fmt_out.video.i_frame_rate_base * CLOCK_FREQ / id->p_encoder->fmt_out.video.i_frame_rate;
552 msg_Info( p_stream, "output interval %d (base %d)",
553 id->i_output_frame_interval, id->p_encoder->fmt_in.video.i_frame_rate_base );
555 date_Init( &id->next_output_pts,
556 id->p_encoder->fmt_in.video.i_frame_rate,
559 /* Check whether a particular aspect ratio was requested */
560 if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
561 id->p_encoder->fmt_out.video.i_sar_den <= 0 )
563 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
564 &id->p_encoder->fmt_out.video.i_sar_den,
565 (uint64_t)p_fmt_out->video.i_sar_num * i_src_visible_width * i_dst_visible_height,
566 (uint64_t)p_fmt_out->video.i_sar_den * i_src_visible_height * i_dst_visible_width,
571 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
572 &id->p_encoder->fmt_out.video.i_sar_den,
573 id->p_encoder->fmt_out.video.i_sar_num,
574 id->p_encoder->fmt_out.video.i_sar_den,
578 id->p_encoder->fmt_in.video.i_sar_num =
579 id->p_encoder->fmt_out.video.i_sar_num;
580 id->p_encoder->fmt_in.video.i_sar_den =
581 id->p_encoder->fmt_out.video.i_sar_den;
583 msg_Dbg( p_stream, "encoder aspect is %i:%i",
584 id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
585 id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
589 static int transcode_video_encoder_open( sout_stream_t *p_stream,
590 sout_stream_id_sys_t *id )
592 sout_stream_sys_t *p_sys = p_stream->p_sys;
595 msg_Dbg( p_stream, "destination (after video filters) %ix%i",
596 id->p_encoder->fmt_in.video.i_width,
597 id->p_encoder->fmt_in.video.i_height );
599 id->p_encoder->p_module =
600 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
601 if( !id->p_encoder->p_module )
603 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
604 p_sys->psz_venc ? p_sys->psz_venc : "any",
605 (char *)&p_sys->i_vcodec );
609 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
612 id->p_encoder->fmt_out.i_codec =
613 vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
615 id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
618 msg_Err( p_stream, "cannot add this stream" );
625 void transcode_video_close( sout_stream_t *p_stream,
626 sout_stream_id_sys_t *id )
628 if( p_stream->p_sys->i_threads >= 1 )
630 vlc_mutex_lock( &p_stream->p_sys->lock_out );
631 p_stream->p_sys->b_abort = true;
632 vlc_cond_signal( &p_stream->p_sys->cond );
633 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
635 vlc_join( p_stream->p_sys->thread, NULL );
636 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
637 vlc_cond_destroy( &p_stream->p_sys->cond );
639 picture_fifo_Delete( p_stream->p_sys->pp_pics );
640 block_ChainRelease( p_stream->p_sys->p_buffers );
641 p_stream->p_sys->pp_pics = NULL;
645 if( id->p_decoder->p_module )
646 module_unneed( id->p_decoder, id->p_decoder->p_module );
647 if( id->p_decoder->p_description )
648 vlc_meta_Delete( id->p_decoder->p_description );
650 free( id->p_decoder->p_owner );
653 if( id->p_encoder->p_module )
654 module_unneed( id->p_encoder, id->p_encoder->p_module );
658 filter_chain_Delete( id->p_f_chain );
660 filter_chain_Delete( id->p_uf_chain );
663 static void OutputFrame( sout_stream_t *p_stream, picture_t *p_pic, sout_stream_id_sys_t *id, block_t **out )
665 sout_stream_sys_t *p_sys = p_stream->p_sys;
666 picture_t *p_pic2 = NULL;
667 const mtime_t original_date = p_pic->date;
668 bool b_need_duplicate=false;
669 /* If input pts is lower than next_output_pts - output_frame_interval
670 * Then the future input frame should fit better and we can drop this one
672 * We check it here also because we can have case that video filters outputs multiple
673 * pictures but we don't need to use them all, for example yadif2x and outputting to some
674 * different fps value
676 if( ( original_date ) <
677 ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
680 msg_Dbg( p_stream, "dropping frame (%"PRId64" + %"PRId64" vs %"PRId64")",
681 p_pic->date, id->i_input_frame_interval, date_Get(&id->next_output_pts) );
683 picture_Release( p_pic );
690 /* Check if we have a subpicture to overlay */
693 video_format_t fmt = id->p_encoder->fmt_in.video;
694 if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
696 fmt.i_visible_width = fmt.i_width;
697 fmt.i_visible_height = fmt.i_height;
702 subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt, &fmt,
703 p_pic->date, p_pic->date, false );
705 /* Overlay subpicture */
708 if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
710 /* We can't modify the picture, we need to duplicate it,
711 * in this point the picture is already p_encoder->fmt.in format*/
712 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
713 if( likely( p_tmp ) )
715 picture_Copy( p_tmp, p_pic );
716 picture_Release( p_pic );
720 if( unlikely( !p_sys->p_spu_blend ) )
721 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
722 if( likely( p_sys->p_spu_blend ) )
723 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
724 subpicture_Delete( p_subpic );
729 p_pic->date = date_Get( &id->next_output_pts );
730 /*This pts is handled, increase clock to next one*/
731 date_Increment( &id->next_output_pts, id->p_encoder->fmt_in.video.i_frame_rate_base );
733 if( p_sys->i_threads == 0 )
737 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
738 block_ChainAppend( out, p_block );
741 /* we need to duplicate while next_output_pts + output_frame_interval < input_pts (next input pts)*/
742 b_need_duplicate = ( date_Get( &id->next_output_pts ) + id->i_output_frame_interval ) <
745 if( p_sys->i_threads )
747 if( p_sys->b_master_sync )
749 p_pic2 = video_new_buffer_encoder( id->p_encoder );
750 if( likely( p_pic2 != NULL ) )
751 picture_Copy( p_pic2, p_pic );
753 vlc_mutex_lock( &p_sys->lock_out );
754 picture_fifo_Push( p_sys->pp_pics, p_pic );
755 vlc_cond_signal( &p_sys->cond );
756 vlc_mutex_unlock( &p_sys->lock_out );
759 while( (p_sys->b_master_sync && b_need_duplicate ))
761 if( p_sys->i_threads >= 1 )
763 picture_t *p_tmp = NULL;
764 /* We can't modify the picture, we need to duplicate it */
765 p_tmp = video_new_buffer_encoder( id->p_encoder );
766 if( likely( p_tmp != NULL ) )
768 picture_Copy( p_tmp, p_pic2 );
769 p_tmp->date = date_Get( &id->next_output_pts );
770 vlc_mutex_lock( &p_sys->lock_out );
771 picture_fifo_Push( p_sys->pp_pics, p_tmp );
772 vlc_cond_signal( &p_sys->cond );
773 vlc_mutex_unlock( &p_sys->lock_out );
779 p_pic->date = date_Get( &id->next_output_pts );
780 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
781 block_ChainAppend( out, p_block );
784 msg_Dbg( p_stream, "duplicated frame");
786 date_Increment( &id->next_output_pts, id->p_encoder->fmt_in.video.i_frame_rate_base );
787 b_need_duplicate = ( date_Get( &id->next_output_pts ) + id->i_output_frame_interval ) <
791 if( p_sys->i_threads && p_pic2 )
792 picture_Release( p_pic2 );
793 else if ( p_sys->i_threads == 0 )
794 picture_Release( p_pic );
797 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
798 block_t *in, block_t **out )
800 sout_stream_sys_t *p_sys = p_stream->p_sys;
801 picture_t *p_pic = NULL;
804 if( unlikely( in == NULL ) )
806 if( p_sys->i_threads == 0 )
810 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
811 block_ChainAppend( out, p_block );
816 msg_Dbg( p_stream, "Flushing thread and waiting that");
817 vlc_mutex_lock( &p_stream->p_sys->lock_out );
818 p_stream->p_sys->b_abort = true;
819 vlc_cond_signal( &p_stream->p_sys->cond );
820 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
822 vlc_join( p_stream->p_sys->thread, NULL );
823 vlc_mutex_lock( &p_sys->lock_out );
824 *out = p_sys->p_buffers;
825 p_sys->p_buffers = NULL;
826 vlc_mutex_unlock( &p_sys->lock_out );
828 msg_Dbg( p_stream, "Flushing done");
834 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
838 id->p_encoder->p_module &&
839 !video_format_IsSimilar( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video )
843 msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
844 p_sys->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
845 p_sys->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
849 filter_chain_Delete( id->p_f_chain );
850 id->p_f_chain = NULL;
852 filter_chain_Delete( id->p_uf_chain );
853 id->p_uf_chain = NULL;
855 /* Reinitialize filters */
856 id->p_encoder->fmt_out.video.i_visible_width = p_sys->i_width & ~1;
857 id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
858 id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
860 transcode_video_filter_init( p_stream, id );
861 transcode_video_encoder_init( p_stream, id );
862 conversion_video_filter_append( id );
863 memcpy( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
867 if( unlikely( !id->p_encoder->p_module ) )
870 filter_chain_Delete( id->p_f_chain );
872 filter_chain_Delete( id->p_uf_chain );
873 id->p_f_chain = id->p_uf_chain = NULL;
875 transcode_video_filter_init( p_stream, id );
876 transcode_video_encoder_init( p_stream, id );
877 conversion_video_filter_append( id );
878 memcpy( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
880 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
882 picture_Release( p_pic );
883 transcode_video_close( p_stream, id );
884 id->b_transcode = false;
887 date_Set( &id->next_output_pts, p_pic->date );
890 /*Input lipsync and drop check */
891 if( p_sys->b_master_sync )
893 /* If input pts lower than next_output_pts - output_frame_interval
894 * Then the future input frame should fit better and we can drop this one
896 * We check this here as we don't need to run video filter at all for pictures
897 * we are going to drop anyway
899 * Duplication need is checked in OutputFrame */
900 if( ( p_pic->date ) <
901 ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
904 msg_Dbg( p_stream, "dropping frame (%"PRId64" + %"PRId64" vs %"PRId64")",
905 p_pic->date, id->i_input_frame_interval, date_Get(&id->next_output_pts) );
907 picture_Release( p_pic );
911 msg_Dbg( p_stream, "not dropping frame");
916 /* Run the filter and output chains; first with the picture,
917 * and then with NULL as many times as we need until they
918 * stop outputting frames.
921 picture_t *p_filtered_pic = p_pic;
923 /* Run filter chain */
925 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
926 if( !p_filtered_pic )
930 picture_t *p_user_filtered_pic = p_filtered_pic;
932 /* Run user specified filter chain */
934 p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
935 if( !p_user_filtered_pic )
938 OutputFrame( p_stream, p_user_filtered_pic, id, out );
940 p_filtered_pic = NULL;
947 if( p_sys->i_threads >= 1 )
949 /* Pick up any return data the encoder thread wants to output. */
950 vlc_mutex_lock( &p_sys->lock_out );
951 *out = p_sys->p_buffers;
952 p_sys->p_buffers = NULL;
953 vlc_mutex_unlock( &p_sys->lock_out );
959 bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
960 sout_stream_id_sys_t *id )
962 sout_stream_sys_t *p_sys = p_stream->p_sys;
965 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
966 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
968 /* Complete destination format */
969 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
970 id->p_encoder->fmt_out.video.i_visible_width = p_sys->i_width & ~1;
971 id->p_encoder->fmt_out.video.i_visible_height = p_sys->i_height & ~1;
972 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
974 /* Build decoder -> filter -> encoder chain */
975 if( transcode_video_new( p_stream, id ) )
977 msg_Err( p_stream, "cannot create video chain" );
981 /* Stream will be added later on because we don't know
982 * all the characteristics of the decoded stream yet */
983 id->b_transcode = true;
987 id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
988 id->p_encoder->fmt_out.video.i_frame_rate_base = (p_sys->fps_den ? p_sys->fps_den : 1);