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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 # include <ffmpeg/avcodec.h>
44 #ifdef HAVE_POSTPROC_POSTPROCESS_H
45 # include <postproc/postprocess.h>
47 # include <libpostproc/postprocess.h>
50 #define SOUT_CFG_PREFIX "sout-switcher-"
51 #define MAX_PICTURES 10
53 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
54 #define MAX_THRESHOLD 99999999
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close ( vlc_object_t * );
62 static sout_stream_id_t *Add( sout_stream_t *, es_format_t * );
63 static int Del( sout_stream_t *, sout_stream_id_t * );
64 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
66 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
68 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
69 int i_width, int i_height,
71 static void NetCommand( sout_stream_t *p_stream );
72 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id );
73 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
75 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
78 /*****************************************************************************
80 *****************************************************************************/
81 #define FILES_TEXT N_("Files")
82 #define FILES_LONGTEXT N_( \
83 "Full paths of the files separated by colons." )
84 #define SIZES_TEXT N_("Sizes")
85 #define SIZES_LONGTEXT N_( \
86 "List of sizes separated by colons (720x576:480x576)." )
87 #define RATIO_TEXT N_("Aspect ratio")
88 #define RATIO_LONGTEXT N_( \
89 "Aspect ratio (4:3, 16:9)." )
90 #define PORT_TEXT N_("Command UDP port")
91 #define PORT_LONGTEXT N_( \
92 "UDP port to listen to for commands." )
93 #define COMMAND_TEXT N_("Command")
94 #define COMMAND_LONGTEXT N_( \
95 "Initial command to execute." )
96 #define GOP_TEXT N_("GOP size")
97 #define GOP_LONGTEXT N_( \
98 "Number of P frames between two I frames." )
99 #define QSCALE_TEXT N_("Quantizer scale")
100 #define QSCALE_LONGTEXT N_( \
101 "Fixed quantizer scale to use." )
102 #define AUDIO_TEXT N_("Mute audio")
103 #define AUDIO_LONGTEXT N_( \
104 "Mute audio when command is not 0." )
107 set_description( _("MPEG2 video switcher stream output") );
108 set_capability( "sout stream", 50 );
109 add_shortcut( "switcher" );
110 set_callbacks( Open, Close );
112 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
113 FILES_LONGTEXT, VLC_FALSE );
114 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
115 SIZES_LONGTEXT, VLC_FALSE );
116 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
117 RATIO_LONGTEXT, VLC_FALSE );
118 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
119 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
120 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
121 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
122 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
123 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
124 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
125 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
126 add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
127 AUDIO_TEXT, AUDIO_LONGTEXT, VLC_TRUE );
130 static const char *ppsz_sout_options[] = {
131 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
135 struct sout_stream_sys_t
137 sout_stream_t *p_out;
141 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
145 picture_t p_pictures[MAX_PICTURES];
150 int i_cmd, i_old_cmd;
153 struct sout_stream_id_t
156 vlc_bool_t b_switcher_video;
157 vlc_bool_t b_switcher_audio;
163 AVCodecContext *ff_enc_c;
170 /*****************************************************************************
172 *****************************************************************************/
173 static int Open( vlc_object_t *p_this )
175 sout_stream_t *p_stream = (sout_stream_t*)p_this;
176 sout_stream_sys_t *p_sys;
178 char *psz_files, *psz_sizes;
179 int i_height = 0, i_width = 0;
181 p_sys = malloc( sizeof(sout_stream_sys_t) );
182 memset( p_sys, 0, sizeof(sout_stream_sys_t) );
184 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
187 msg_Err( p_stream, "cannot create chain" );
192 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
195 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
196 psz_files = val.psz_string;
197 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
198 psz_sizes = val.psz_string;
200 p_sys->i_nb_pictures = 0;
201 while ( psz_files && *psz_files )
203 char * psz_file = psz_files;
204 char * psz_size = psz_sizes;
206 while ( *psz_files && *psz_files != ':' )
208 if ( *psz_files == ':' )
213 while ( *psz_sizes && *psz_sizes != ':' )
215 if ( *psz_sizes == ':' )
217 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
219 msg_Err( p_stream, "bad size %s for file %s", psz_size,
226 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
227 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
232 p_sys->i_nb_pictures++;
235 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
236 if ( val.psz_string )
238 char *psz_parser = strchr( val.psz_string, ':' );
242 *psz_parser++ = '\0';
243 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
244 / atoi( psz_parser );
248 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
249 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
252 free( val.psz_string );
256 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
259 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
260 p_sys->i_fd = net_OpenUDP( p_stream, NULL, val.i_int, NULL, 0 );
261 if ( p_sys->i_fd < 0 )
267 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
268 p_sys->i_cmd = val.i_int;
269 p_sys->i_old_cmd = 0;
271 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
272 p_sys->i_gop = val.i_int;
274 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
275 p_sys->i_qscale = val.i_int;
277 var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
278 p_sys->b_audio = val.b_bool;
280 p_stream->pf_add = Add;
281 p_stream->pf_del = Del;
282 p_stream->pf_send = Send;
283 p_stream->p_sys = p_sys;
286 avcodec_register_all();
291 /*****************************************************************************
293 *****************************************************************************/
294 static void Close( vlc_object_t * p_this )
296 sout_stream_t *p_stream = (sout_stream_t *)p_this;
297 sout_stream_sys_t *p_sys = p_stream->p_sys;
299 sout_StreamDelete( p_sys->p_out );
304 /*****************************************************************************
305 * Add: Add an input elementary stream
306 *****************************************************************************/
307 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
309 sout_stream_sys_t *p_sys = p_stream->p_sys;
310 sout_stream_id_t *id;
312 id = malloc( sizeof( sout_stream_id_t ) );
313 memset( id, 0, sizeof( sout_stream_id_t ) );
316 if ( p_fmt->i_cat == VIDEO_ES
317 && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
318 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
320 id->b_switcher_video = VLC_TRUE;
321 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
323 "creating video switcher for fcc=`%4.4s'",
324 (char*)&p_fmt->i_codec );
326 else if ( p_fmt->i_cat == AUDIO_ES
327 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a')
330 int i_ff_codec = CODEC_ID_MP2;
333 id->b_switcher_audio = VLC_TRUE;
335 "creating audio switcher for fcc=`%4.4s'",
336 (char*)&p_fmt->i_codec );
338 /* Allocate the encoder right now. */
339 if( i_ff_codec == 0 )
341 msg_Err( p_stream, "cannot find encoder" );
345 id->ff_enc = avcodec_find_encoder( i_ff_codec );
348 msg_Err( p_stream, "cannot find encoder (avcodec)" );
352 id->ff_enc_c = avcodec_alloc_context();
354 /* Set CPU capabilities */
355 id->ff_enc_c->dsp_mask = 0;
356 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
358 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
360 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
362 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
364 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
366 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
368 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
370 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
371 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
374 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
375 id->ff_enc_c->channels = p_fmt->audio.i_channels;
376 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
378 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
380 msg_Err( p_stream, "cannot open encoder" );
384 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
385 id->p_samples = malloc( id->ff_enc_c->frame_size
386 * p_fmt->audio.i_channels * sizeof(int16_t) );
387 memset( id->p_samples, 0,
388 id->ff_enc_c->frame_size * p_fmt->audio.i_channels
391 for ( i = 0; i < MAX_AUDIO; i++ )
393 if ( p_sys->pp_audio_ids[i] == NULL )
395 p_sys->pp_audio_ids[i] = id;
399 if ( i == MAX_AUDIO )
401 msg_Err( p_stream, "too many audio streams !" );
408 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
409 (char*)&p_fmt->i_codec );
413 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
415 /* open output stream */
416 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
418 if ( id->id == NULL )
427 /*****************************************************************************
428 * Del: Del an elementary stream
429 *****************************************************************************/
430 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
432 sout_stream_sys_t *p_sys = p_stream->p_sys;
434 if ( id->b_switcher_audio )
437 for ( i = 0; i < MAX_AUDIO; i++ )
439 if ( p_sys->pp_audio_ids[i] == id )
441 p_sys->pp_audio_ids[i] = NULL;
449 avcodec_close( id->ff_enc_c );
450 av_free( id->ff_enc_c );
451 av_free( id->p_frame );
452 free( id->p_buffer_out );
457 p_sys->p_out->pf_del( p_sys->p_out, id->id );
464 /*****************************************************************************
465 * Send: Process an input packet
466 *****************************************************************************/
467 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
470 sout_stream_sys_t *p_sys = p_stream->p_sys;
474 block_Release( p_buffer );
478 if ( !id->b_switcher_video && !id->b_switcher_audio )
480 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
483 block_ChainAppend( &id->p_queued, p_buffer );
485 if ( id->b_switcher_video )
487 /* Check for commands for every video frame. */
488 NetCommand( p_stream );
490 while ( id->p_queued != NULL )
495 if ( p_sys->i_old_cmd != p_sys->i_cmd )
497 i_dts = VideoCommand( p_stream, id );
500 i_dts = Process( p_stream, id, i_dts );
502 for ( i = 0; i < MAX_AUDIO; i++ )
504 if ( p_sys->pp_audio_ids[i] != NULL )
505 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
513 /*****************************************************************************
514 * Process: Process and dispatch buffers
515 *****************************************************************************/
516 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
519 sout_stream_sys_t *p_sys = p_stream->p_sys;
521 block_t *p_blocks = NULL;
522 block_t *p_blocks_out = NULL;
524 /* Find out the blocks we need. */
525 while ( id->p_queued != NULL
526 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
528 block_t *p_next = id->p_queued->p_next;
529 id->p_queued->p_next = NULL;
530 i_dts = id->p_queued->i_dts;
531 block_ChainAppend( &p_blocks, id->p_queued );
532 id->p_queued = p_next;
535 if ( p_sys->i_old_cmd == 0 )
538 if ( p_blocks != NULL )
539 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
543 if ( p_sys->i_old_cmd == -1 )
545 /* No output at all */
546 while ( p_blocks != NULL )
548 block_t * p_next = p_blocks->p_next;
549 block_Release( p_blocks );
555 while ( p_blocks != NULL )
557 block_t * p_next = p_blocks->p_next;
560 if ( id->b_switcher_video )
562 p_out = VideoGetBuffer( p_stream, id, p_blocks );
566 p_out = AudioGetBuffer( p_stream, id, p_blocks );
571 block_ChainAppend( &p_blocks_out, p_out );
575 if ( p_blocks_out != NULL )
576 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
580 /*****************************************************************************
581 * UnpackFromFile: Read a YUV picture and store it in our format
582 *****************************************************************************/
583 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
584 int i_width, int i_height,
588 FILE *p_file = fopen( psz_file, "r" );
590 if ( p_file == NULL )
592 msg_Err( p_stream, "file %s not found", psz_file );
596 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
598 i_width * VOUT_ASPECT_FACTOR / i_height );
599 for ( i = 0; i < p_pic->i_planes; i++ )
601 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
602 p_pic->p[i].i_pitch );
603 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
604 p_pic->p[i].i_pitch );
607 for ( i = 0; i < i_height; i++ )
610 uint8_t p_buffer[i_width * 2];
611 uint8_t *p_char = p_buffer;
612 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
613 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
614 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
616 if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
618 msg_Err( p_stream, "premature end of file %s", psz_file );
620 for ( i = 0; i < p_pic->i_planes; i++ )
622 free( p_pic->p[i].p_pixels );
628 for ( j = 0; j < i_width; j++ )
630 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
631 i_chroma = !i_chroma;
633 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
635 **pp_chroma = *p_char;
646 /*****************************************************************************
647 * NetCommand: Get a command from the network
648 *****************************************************************************/
649 static void NetCommand( sout_stream_t *p_stream )
651 sout_stream_sys_t *p_sys = p_stream->p_sys;
653 int i_len = net_ReadNonBlock( p_stream, p_sys->i_fd, NULL, psz_buffer,
654 sizeof( psz_buffer ), 0 );
658 int i_cmd = strtol( psz_buffer, NULL, 0 );
659 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
661 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
665 p_sys->i_cmd = i_cmd;
669 /*****************************************************************************
670 * VideoCommand: Create/Delete a video encoder
671 *****************************************************************************/
672 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
674 sout_stream_sys_t *p_sys = p_stream->p_sys;
676 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
678 mtime_t i_dts = id->p_queued->i_dts;
679 block_t *p_block = id->p_queued->p_next;
681 while ( p_block != NULL )
683 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
685 i_dts = p_block->i_dts;
686 p_block = p_block->p_next;
692 p_sys->i_old_cmd = p_sys->i_cmd;
696 avcodec_close( id->ff_enc_c );
697 av_free( id->ff_enc_c );
698 av_free( id->p_frame );
699 free( id->p_buffer_out );
703 if ( p_sys->i_cmd > 0 )
705 /* Create a new encoder. */
706 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
707 int i_aspect_num, i_aspect_den;
709 if( i_ff_codec == 0 )
711 msg_Err( p_stream, "cannot find encoder" );
715 id->ff_enc = avcodec_find_encoder( i_ff_codec );
718 msg_Err( p_stream, "cannot find encoder (avcodec)" );
722 id->ff_enc_c = avcodec_alloc_context();
724 /* Set CPU capabilities */
725 id->ff_enc_c->dsp_mask = 0;
726 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
728 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
730 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
732 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
734 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
736 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
738 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
740 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
741 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
744 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
745 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
746 av_reduce( &i_aspect_num, &i_aspect_den,
748 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
749 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
750 &id->ff_enc_c->sample_aspect_ratio.den,
751 i_aspect_num * (int64_t)id->ff_enc_c->height,
752 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
754 #if LIBAVCODEC_BUILD >= 4754
755 id->ff_enc_c->time_base.num = 1;
756 id->ff_enc_c->time_base.den = 25; /* FIXME */
758 id->ff_enc_c->frame_rate = 25; /* FIXME */
759 id->ff_enc_c->frame_rate_base = 1;
762 id->ff_enc_c->gop_size = 200;
763 id->ff_enc_c->max_b_frames = 0;
765 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
766 | CODEC_FLAG_INPUT_PRESERVED
767 | CODEC_FLAG_LOW_DELAY;
769 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
770 id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
772 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
774 msg_Err( p_stream, "cannot open encoder" );
778 id->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
779 id->p_frame = avcodec_alloc_frame();
780 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
781 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
782 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
783 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
784 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
785 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
787 id->i_nb_pred = p_sys->i_gop;
793 /*****************************************************************************
794 * VideoGetBuffer: Build an alternate video buffer
795 *****************************************************************************/
796 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
799 sout_stream_sys_t *p_sys = p_stream->p_sys;
803 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
805 id->p_frame->interlaced_frame = 0;
806 id->p_frame->top_field_first = 1;
807 id->p_frame->pts = p_buffer->i_dts;
809 if ( id->i_nb_pred >= p_sys->i_gop )
811 id->p_frame->pict_type = FF_I_TYPE;
813 id->p_frame->me_threshold = 0;
814 id->p_frame->mb_threshold = 0;
820 id->p_frame->pict_type = FF_P_TYPE;
822 if ( id->p_frame->mb_type != NULL )
824 id->p_frame->me_threshold = MAX_THRESHOLD;
825 id->p_frame->mb_threshold = MAX_THRESHOLD;
831 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
832 AVCODEC_MAX_VIDEO_FRAME_SIZE,
839 if ( id->p_frame->mb_type == NULL
840 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
842 int mb_width = (id->ff_enc_c->width + 15) / 16;
843 int mb_height = (id->ff_enc_c->height + 15) / 16;
844 int h_chroma_shift, v_chroma_shift;
847 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
850 id->p_frame->motion_subsample_log2
851 = id->ff_enc_c->coded_frame->motion_subsample_log2;
852 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
853 * sizeof(uint32_t) );
854 p_stream->p_vlc->pf_memcpy( id->p_frame->mb_type,
855 id->ff_enc_c->coded_frame->mb_type,
856 (mb_width + 1) * mb_height
857 * sizeof(id->p_frame->mb_type[0]));
859 for ( i = 0; i < 2; i++ )
861 int stride = ((16 * mb_width )
862 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
863 int height = ((16 * mb_height)
864 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
865 int b8_stride = mb_width * 2 + 1;
867 if ( id->ff_enc_c->coded_frame->motion_val[i] )
869 id->p_frame->motion_val[i] = malloc( 2 * stride * height
871 p_stream->p_vlc->pf_memcpy( id->p_frame->motion_val[i],
872 id->ff_enc_c->coded_frame->motion_val[i],
873 2 * stride * height * sizeof(int16_t) );
875 if ( id->ff_enc_c->coded_frame->ref_index[i] )
877 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
879 p_stream->p_vlc->pf_memcpy( id->p_frame->ref_index[i],
880 id->ff_enc_c->coded_frame->ref_index[i],
881 b8_stride * 2 * mb_height * sizeof(int8_t));
887 p_out = block_New( p_stream, i_out );
888 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
889 p_out->i_length = p_buffer->i_length;
890 p_out->i_pts = p_buffer->i_dts;
891 p_out->i_dts = p_buffer->i_dts;
892 p_out->i_rate = p_buffer->i_rate;
894 switch ( id->ff_enc_c->coded_frame->pict_type )
897 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
900 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
903 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
909 block_Release( p_buffer );
914 /*****************************************************************************
915 * AudioGetBuffer: Build an alternate audio buffer
916 *****************************************************************************/
917 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
923 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
924 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
930 p_out = block_New( p_stream, i_out );
931 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
932 p_out->i_length = p_buffer->i_length;
933 p_out->i_pts = p_buffer->i_dts;
934 p_out->i_dts = p_buffer->i_dts;
935 p_out->i_rate = p_buffer->i_rate;
937 block_Release( p_buffer );