1 /*****************************************************************************
2 * switcher.c: MPEG2 video switcher module
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_avcodec.h>
37 #include <vlc_filter.h>
39 #include <vlc_block.h>
41 #include <vlc_charset.h>
42 #include <vlc_network.h>
45 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
46 # include <libavcodec/avcodec.h>
47 #elif defined(HAVE_FFMPEG_AVCODEC_H)
48 # include <ffmpeg/avcodec.h>
53 #ifdef HAVE_POSTPROC_POSTPROCESS_H
54 # include <postproc/postprocess.h>
56 # include <libpostproc/postprocess.h>
59 #define SOUT_CFG_PREFIX "sout-switcher-"
60 #define MAX_PICTURES 10
62 #define MAX_THRESHOLD 99999999
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close ( vlc_object_t * );
70 static sout_stream_id_t *Add( sout_stream_t *, es_format_t * );
71 static int Del( sout_stream_t *, sout_stream_id_t * );
72 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
74 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
76 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
77 int i_width, int i_height,
79 static void NetCommand( sout_stream_t *p_stream );
80 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id );
81 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
83 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
86 /*****************************************************************************
88 *****************************************************************************/
89 #define FILES_TEXT N_("Files")
90 #define FILES_LONGTEXT N_( \
91 "Full paths of the files separated by colons." )
92 #define SIZES_TEXT N_("Sizes")
93 #define SIZES_LONGTEXT N_( \
94 "List of sizes separated by colons (720x576:480x576)." )
95 #define RATIO_TEXT N_("Aspect ratio")
96 #define RATIO_LONGTEXT N_( \
97 "Aspect ratio (4:3, 16:9)." )
98 #define PORT_TEXT N_("Command UDP port")
99 #define PORT_LONGTEXT N_( \
100 "UDP port to listen to for commands." )
101 #define COMMAND_TEXT N_("Command")
102 #define COMMAND_LONGTEXT N_( \
103 "Initial command to execute." )
104 #define GOP_TEXT N_("GOP size")
105 #define GOP_LONGTEXT N_( \
106 "Number of P frames between two I frames." )
107 #define QSCALE_TEXT N_("Quantizer scale")
108 #define QSCALE_LONGTEXT N_( \
109 "Fixed quantizer scale to use." )
110 #define AUDIO_TEXT N_("Mute audio")
111 #define AUDIO_LONGTEXT N_( \
112 "Mute audio when command is not 0." )
115 set_description( N_("MPEG2 video switcher stream output") )
116 set_capability( "sout stream", 50 )
117 add_shortcut( "switcher" )
118 set_callbacks( Open, Close )
120 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
121 FILES_LONGTEXT, false )
122 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
123 SIZES_LONGTEXT, false )
124 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
125 RATIO_LONGTEXT, false )
126 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
127 PORT_TEXT, PORT_LONGTEXT, true )
128 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
129 COMMAND_TEXT, COMMAND_LONGTEXT, true )
130 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
131 GOP_TEXT, GOP_LONGTEXT, true )
132 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
133 QSCALE_TEXT, QSCALE_LONGTEXT, true )
134 add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
135 AUDIO_TEXT, AUDIO_LONGTEXT, true )
138 static const char *const ppsz_sout_options[] = {
139 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
143 struct sout_stream_sys_t
145 sout_stream_t *p_out;
149 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
153 picture_t p_pictures[MAX_PICTURES];
158 int i_cmd, i_old_cmd;
161 struct sout_stream_id_t
164 bool b_switcher_video;
165 bool b_switcher_audio;
171 AVCodecContext *ff_enc_c;
173 uint8_t *p_buffer_out;
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
183 sout_stream_t *p_stream = (sout_stream_t*)p_this;
184 sout_stream_sys_t *p_sys;
186 char *psz_files, *psz_sizes;
187 int i_height = 0, i_width = 0;
189 p_sys = calloc( 1, sizeof(sout_stream_sys_t) );
193 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
196 msg_Err( p_stream, "cannot create chain" );
201 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
204 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
205 psz_files = val.psz_string;
206 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
207 psz_sizes = val.psz_string;
209 p_sys->i_nb_pictures = 0;
210 while ( psz_files && *psz_files )
212 char * psz_file = psz_files;
213 char * psz_size = psz_sizes;
215 while ( *psz_files && *psz_files != ':' )
217 if ( *psz_files == ':' )
222 while ( *psz_sizes && *psz_sizes != ':' )
224 if ( *psz_sizes == ':' )
226 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
228 msg_Err( p_stream, "bad size %s for file %s", psz_size,
235 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
236 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
241 p_sys->i_nb_pictures++;
244 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
245 if ( val.psz_string )
247 char *psz_parser = strchr( val.psz_string, ':' );
251 *psz_parser++ = '\0';
252 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
253 / atoi( psz_parser );
257 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
258 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
261 free( val.psz_string );
265 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
268 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
269 p_sys->i_fd = net_ListenUDP1( VLC_OBJECT(p_stream), NULL, val.i_int );
270 if ( p_sys->i_fd < 0 )
276 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
277 p_sys->i_cmd = val.i_int;
278 p_sys->i_old_cmd = 0;
280 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
281 p_sys->i_gop = val.i_int;
283 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
284 p_sys->i_qscale = val.i_int;
286 var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
287 p_sys->b_audio = val.b_bool;
289 p_stream->pf_add = Add;
290 p_stream->pf_del = Del;
291 p_stream->pf_send = Send;
292 p_stream->p_sys = p_sys;
295 avcodec_register_all();
300 /*****************************************************************************
302 *****************************************************************************/
303 static void Close( vlc_object_t * p_this )
305 sout_stream_t *p_stream = (sout_stream_t *)p_this;
306 sout_stream_sys_t *p_sys = p_stream->p_sys;
308 sout_StreamDelete( p_sys->p_out );
313 /*****************************************************************************
314 * Add: Add an input elementary stream
315 *****************************************************************************/
316 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
318 sout_stream_sys_t *p_sys = p_stream->p_sys;
319 sout_stream_id_t *id;
321 id = calloc( 1, sizeof( sout_stream_id_t ) );
325 if( p_fmt->i_cat == VIDEO_ES &&
326 ( p_fmt->i_codec == VLC_CODEC_MPGV ||
327 p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e') ) )
329 id->b_switcher_video = true;
330 p_fmt->i_codec = VLC_CODEC_MPGV;
331 msg_Dbg( p_stream, "creating video switcher for fcc=`%4.4s' cmd:%d",
332 (char*)&p_fmt->i_codec, p_sys->i_cmd );
334 else if ( p_fmt->i_cat == AUDIO_ES &&
335 p_fmt->i_codec == VLC_CODEC_MPGA &&
338 int i_ff_codec = CODEC_ID_MP2;
341 id->b_switcher_audio = true;
342 msg_Dbg( p_stream, "creating audio switcher for fcc=`%4.4s' cmd:%d",
343 (char*)&p_fmt->i_codec, p_sys->i_cmd );
345 /* Allocate the encoder right now. */
346 if( i_ff_codec == 0 )
348 msg_Err( p_stream, "cannot find encoder" );
353 id->ff_enc = avcodec_find_encoder( i_ff_codec );
356 msg_Err( p_stream, "cannot find encoder (avcodec)" );
361 id->ff_enc_c = avcodec_alloc_context();
363 /* Set CPU capabilities */
364 unsigned i_cpu = vlc_CPU();
365 id->ff_enc_c->dsp_mask = 0;
366 if( !(i_cpu & CPU_CAPABILITY_MMX) )
368 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
370 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
372 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
374 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
376 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
378 if( !(i_cpu & CPU_CAPABILITY_SSE) )
380 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
381 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
384 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
385 id->ff_enc_c->channels = p_fmt->audio.i_channels;
386 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
389 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
391 vlc_avcodec_unlock();
392 msg_Err( p_stream, "cannot open encoder" );
393 av_free( id->ff_enc_c );
397 vlc_avcodec_unlock();
399 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
400 id->p_samples = calloc( id->ff_enc_c->frame_size * p_fmt->audio.i_channels,
402 if( !id->p_buffer_out || !id->p_samples )
405 for( i = 0; i < MAX_AUDIO; i++ )
407 if( p_sys->pp_audio_ids[i] == NULL )
409 p_sys->pp_audio_ids[i] = id;
415 msg_Err( p_stream, "too many audio streams!" );
421 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
422 (char*)&p_fmt->i_codec );
426 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
428 /* open output stream */
429 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
436 avcodec_close( id->ff_enc_c );
437 vlc_avcodec_unlock();
438 free( id->p_samples );
439 free( id->p_buffer_out );
440 av_free( id->ff_enc_c );
445 /*****************************************************************************
446 * Del: Del an elementary stream
447 *****************************************************************************/
448 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
450 sout_stream_sys_t *p_sys = p_stream->p_sys;
452 if ( id->b_switcher_audio )
455 for ( i = 0; i < MAX_AUDIO; i++ )
457 if ( p_sys->pp_audio_ids[i] == id )
459 p_sys->pp_audio_ids[i] = NULL;
468 avcodec_close( id->ff_enc_c );
469 vlc_avcodec_unlock();
470 av_free( id->ff_enc_c );
471 av_free( id->p_frame );
472 free( id->p_buffer_out );
477 p_sys->p_out->pf_del( p_sys->p_out, id->id );
484 /*****************************************************************************
485 * Send: Process an input packet
486 *****************************************************************************/
487 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
490 sout_stream_sys_t *p_sys = p_stream->p_sys;
494 block_Release( p_buffer );
498 if ( !id->b_switcher_video && !id->b_switcher_audio )
500 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
503 block_ChainAppend( &id->p_queued, p_buffer );
505 if ( id->b_switcher_video )
507 /* Check for commands for every video frame. */
508 NetCommand( p_stream );
510 while ( id->p_queued != NULL )
515 if ( p_sys->i_old_cmd != p_sys->i_cmd )
517 i_dts = VideoCommand( p_stream, id );
520 i_dts = Process( p_stream, id, i_dts );
522 for ( i = 0; i < MAX_AUDIO; i++ )
524 if ( p_sys->pp_audio_ids[i] != NULL )
525 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
533 /*****************************************************************************
534 * Process: Process and dispatch buffers
535 *****************************************************************************/
536 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
539 sout_stream_sys_t *p_sys = p_stream->p_sys;
541 block_t *p_blocks = NULL;
542 block_t *p_blocks_out = NULL;
544 /* Find out the blocks we need. */
545 while ( id->p_queued != NULL
546 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
548 block_t *p_next = id->p_queued->p_next;
549 id->p_queued->p_next = NULL;
550 i_dts = id->p_queued->i_dts;
551 block_ChainAppend( &p_blocks, id->p_queued );
552 id->p_queued = p_next;
555 if ( p_sys->i_old_cmd == 0 )
558 if ( p_blocks != NULL )
559 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
563 if ( p_sys->i_old_cmd == -1 )
565 /* No output at all */
566 while ( p_blocks != NULL )
568 block_t * p_next = p_blocks->p_next;
569 block_Release( p_blocks );
575 while ( p_blocks != NULL )
577 block_t * p_next = p_blocks->p_next;
580 if ( id->b_switcher_video )
582 p_out = VideoGetBuffer( p_stream, id, p_blocks );
586 p_out = AudioGetBuffer( p_stream, id, p_blocks );
591 block_ChainAppend( &p_blocks_out, p_out );
595 if ( p_blocks_out != NULL )
596 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
600 /*****************************************************************************
601 * UnpackFromFile: Read a YUV picture and store it in our format
602 *****************************************************************************/
603 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
604 int i_width, int i_height,
608 FILE *p_file = utf8_fopen( psz_file, "r" );
610 if ( p_file == NULL )
612 msg_Err( p_stream, "file %s not found", psz_file );
616 if( picture_Setup( p_pic, VLC_CODEC_I420,
618 i_width * VOUT_ASPECT_FACTOR / i_height ) )
620 msg_Err( p_stream, "unknown chroma" );
623 for ( i = 0; i < p_pic->i_planes; i++ )
625 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
626 p_pic->p[i].i_pitch );
627 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
628 p_pic->p[i].i_pitch );
631 for ( i = 0; i < i_height; i++ )
634 uint8_t p_buffer[i_width * 2];
635 uint8_t *p_char = p_buffer;
636 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
637 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
638 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
640 if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
642 msg_Err( p_stream, "premature end of file %s", psz_file );
644 for ( i = 0; i < p_pic->i_planes; i++ )
646 free( p_pic->p[i].p_pixels );
652 for ( j = 0; j < i_width; j++ )
654 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
655 i_chroma = !i_chroma;
657 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
659 **pp_chroma = *p_char;
670 /*****************************************************************************
671 * NetCommand: Get a command from the network
672 *****************************************************************************/
673 static void NetCommand( sout_stream_t *p_stream )
675 sout_stream_sys_t *p_sys = p_stream->p_sys;
677 int i_len = recv( p_sys->i_fd, psz_buffer, sizeof( psz_buffer ) - 1, 0 );
681 psz_buffer[i_len] = '\0';
682 int i_cmd = strtol( psz_buffer, NULL, 0 );
683 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
685 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
689 p_sys->i_cmd = i_cmd;
691 msg_Dbg( p_stream, "new command: %d old:%d", p_sys->i_cmd,
696 /*****************************************************************************
697 * VideoCommand: Create/Delete a video encoder
698 *****************************************************************************/
699 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
701 sout_stream_sys_t *p_sys = p_stream->p_sys;
703 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
705 mtime_t i_dts = id->p_queued->i_dts;
706 block_t *p_block = id->p_queued->p_next;
708 while ( p_block != NULL )
710 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
712 i_dts = p_block->i_dts;
713 p_block = p_block->p_next;
719 p_sys->i_old_cmd = p_sys->i_cmd;
724 avcodec_close( id->ff_enc_c );
725 vlc_avcodec_unlock();
726 av_free( id->ff_enc_c );
727 av_free( id->p_frame );
728 free( id->p_buffer_out );
732 if ( p_sys->i_cmd > 0 )
734 /* Create a new encoder. */
735 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
736 int i_aspect_num, i_aspect_den;
738 if( i_ff_codec == 0 )
740 msg_Err( p_stream, "cannot find encoder" );
744 id->ff_enc = avcodec_find_encoder( i_ff_codec );
747 msg_Err( p_stream, "cannot find encoder (avcodec)" );
751 id->ff_enc_c = avcodec_alloc_context();
753 /* Set CPU capabilities */
754 unsigned i_cpu = vlc_CPU();
755 id->ff_enc_c->dsp_mask = 0;
756 if( !(i_cpu & CPU_CAPABILITY_MMX) )
758 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
760 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
762 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
764 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
766 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
768 if( !(i_cpu & CPU_CAPABILITY_SSE) )
770 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
771 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
774 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
775 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
776 av_reduce( &i_aspect_num, &i_aspect_den,
778 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
779 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
780 &id->ff_enc_c->sample_aspect_ratio.den,
781 i_aspect_num * (int64_t)id->ff_enc_c->height,
782 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
784 #if LIBAVCODEC_BUILD >= 4754
785 id->ff_enc_c->time_base.num = 1;
786 id->ff_enc_c->time_base.den = 25; /* FIXME */
788 id->ff_enc_c->frame_rate = 25; /* FIXME */
789 id->ff_enc_c->frame_rate_base = 1;
792 id->ff_enc_c->gop_size = 200;
793 id->ff_enc_c->max_b_frames = 0;
795 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
796 | CODEC_FLAG_INPUT_PRESERVED
797 | CODEC_FLAG_LOW_DELAY;
799 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
800 id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
803 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
805 vlc_avcodec_unlock();
806 msg_Err( p_stream, "cannot open encoder" );
809 vlc_avcodec_unlock();
811 id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
812 id->p_frame = avcodec_alloc_frame();
813 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
814 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
815 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
816 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
817 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
818 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
820 id->i_nb_pred = p_sys->i_gop;
826 /*****************************************************************************
827 * VideoGetBuffer: Build an alternate video buffer
828 *****************************************************************************/
829 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
832 sout_stream_sys_t *p_sys = p_stream->p_sys;
836 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
838 id->p_frame->interlaced_frame = 0;
839 id->p_frame->top_field_first = 1;
840 id->p_frame->pts = p_buffer->i_dts;
842 if ( id->i_nb_pred >= p_sys->i_gop )
844 id->p_frame->pict_type = FF_I_TYPE;
846 id->p_frame->me_threshold = 0;
847 id->p_frame->mb_threshold = 0;
853 id->p_frame->pict_type = FF_P_TYPE;
855 if ( id->p_frame->mb_type != NULL )
857 id->p_frame->me_threshold = MAX_THRESHOLD;
858 id->p_frame->mb_threshold = MAX_THRESHOLD;
864 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
865 id->ff_enc_c->width * id->ff_enc_c->height * 3,
872 if ( id->p_frame->mb_type == NULL
873 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
875 int mb_width = (id->ff_enc_c->width + 15) / 16;
876 int mb_height = (id->ff_enc_c->height + 15) / 16;
877 int h_chroma_shift, v_chroma_shift;
880 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
883 id->p_frame->motion_subsample_log2
884 = id->ff_enc_c->coded_frame->motion_subsample_log2;
885 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
886 * sizeof(uint32_t) );
887 vlc_memcpy( id->p_frame->mb_type, id->ff_enc_c->coded_frame->mb_type,
888 (mb_width + 1) * mb_height * sizeof(id->p_frame->mb_type[0]));
890 for ( i = 0; i < 2; i++ )
892 int stride = ((16 * mb_width )
893 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
894 int height = ((16 * mb_height)
895 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
896 int b8_stride = mb_width * 2 + 1;
898 if ( id->ff_enc_c->coded_frame->motion_val[i] )
900 id->p_frame->motion_val[i] = malloc( 2 * stride * height
902 vlc_memcpy( id->p_frame->motion_val[i],
903 id->ff_enc_c->coded_frame->motion_val[i],
904 2 * stride * height * sizeof(int16_t) );
906 if ( id->ff_enc_c->coded_frame->ref_index[i] )
908 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
910 vlc_memcpy( id->p_frame->ref_index[i],
911 id->ff_enc_c->coded_frame->ref_index[i],
912 b8_stride * 2 * mb_height * sizeof(int8_t));
918 p_out = block_New( p_stream, i_out );
919 vlc_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
920 p_out->i_length = p_buffer->i_length;
921 p_out->i_pts = p_buffer->i_dts;
922 p_out->i_dts = p_buffer->i_dts;
923 p_out->i_rate = p_buffer->i_rate;
925 switch ( id->ff_enc_c->coded_frame->pict_type )
928 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
931 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
934 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
940 block_Release( p_buffer );
945 /*****************************************************************************
946 * AudioGetBuffer: Build an alternate audio buffer
947 *****************************************************************************/
948 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
955 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
956 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
962 p_out = block_New( p_stream, i_out );
963 vlc_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
964 p_out->i_length = p_buffer->i_length;
965 p_out->i_pts = p_buffer->i_dts;
966 p_out->i_dts = p_buffer->i_dts;
967 p_out->i_rate = p_buffer->i_rate;
969 block_Release( p_buffer );