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 *****************************************************************************/
37 #include <vlc_charset.h>
38 #include <vlc_network.h>
41 #ifdef HAVE_FFMPEG_AVCODEC_H
42 # include <ffmpeg/avcodec.h>
47 #ifdef HAVE_POSTPROC_POSTPROCESS_H
48 # include <postproc/postprocess.h>
50 # include <libpostproc/postprocess.h>
53 #define SOUT_CFG_PREFIX "sout-switcher-"
54 #define MAX_PICTURES 10
56 #define MAX_THRESHOLD 99999999
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close ( vlc_object_t * );
64 static sout_stream_id_t *Add( sout_stream_t *, es_format_t * );
65 static int Del( sout_stream_t *, sout_stream_id_t * );
66 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
68 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
70 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
71 int i_width, int i_height,
73 static void NetCommand( sout_stream_t *p_stream );
74 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id );
75 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
77 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
80 /*****************************************************************************
82 *****************************************************************************/
83 #define FILES_TEXT N_("Files")
84 #define FILES_LONGTEXT N_( \
85 "Full paths of the files separated by colons." )
86 #define SIZES_TEXT N_("Sizes")
87 #define SIZES_LONGTEXT N_( \
88 "List of sizes separated by colons (720x576:480x576)." )
89 #define RATIO_TEXT N_("Aspect ratio")
90 #define RATIO_LONGTEXT N_( \
91 "Aspect ratio (4:3, 16:9)." )
92 #define PORT_TEXT N_("Command UDP port")
93 #define PORT_LONGTEXT N_( \
94 "UDP port to listen to for commands." )
95 #define COMMAND_TEXT N_("Command")
96 #define COMMAND_LONGTEXT N_( \
97 "Initial command to execute." )
98 #define GOP_TEXT N_("GOP size")
99 #define GOP_LONGTEXT N_( \
100 "Number of P frames between two I frames." )
101 #define QSCALE_TEXT N_("Quantizer scale")
102 #define QSCALE_LONGTEXT N_( \
103 "Fixed quantizer scale to use." )
104 #define AUDIO_TEXT N_("Mute audio")
105 #define AUDIO_LONGTEXT N_( \
106 "Mute audio when command is not 0." )
109 set_description( _("MPEG2 video switcher stream output") );
110 set_capability( "sout stream", 50 );
111 add_shortcut( "switcher" );
112 set_callbacks( Open, Close );
114 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
115 FILES_LONGTEXT, VLC_FALSE );
116 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
117 SIZES_LONGTEXT, VLC_FALSE );
118 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
119 RATIO_LONGTEXT, VLC_FALSE );
120 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
121 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
122 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
123 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
124 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
125 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
126 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
127 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
128 add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
129 AUDIO_TEXT, AUDIO_LONGTEXT, VLC_TRUE );
132 static const char *ppsz_sout_options[] = {
133 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
137 struct sout_stream_sys_t
139 sout_stream_t *p_out;
143 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
147 picture_t p_pictures[MAX_PICTURES];
152 int i_cmd, i_old_cmd;
155 struct sout_stream_id_t
158 vlc_bool_t b_switcher_video;
159 vlc_bool_t b_switcher_audio;
165 AVCodecContext *ff_enc_c;
167 uint8_t *p_buffer_out;
172 /*****************************************************************************
174 *****************************************************************************/
175 static int Open( vlc_object_t *p_this )
177 sout_stream_t *p_stream = (sout_stream_t*)p_this;
178 sout_stream_sys_t *p_sys;
180 char *psz_files, *psz_sizes;
181 int i_height = 0, i_width = 0;
183 p_sys = malloc( sizeof(sout_stream_sys_t) );
184 memset( p_sys, 0, sizeof(sout_stream_sys_t) );
186 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
189 msg_Err( p_stream, "cannot create chain" );
194 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
197 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
198 psz_files = val.psz_string;
199 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
200 psz_sizes = val.psz_string;
202 p_sys->i_nb_pictures = 0;
203 while ( psz_files && *psz_files )
205 char * psz_file = psz_files;
206 char * psz_size = psz_sizes;
208 while ( *psz_files && *psz_files != ':' )
210 if ( *psz_files == ':' )
215 while ( *psz_sizes && *psz_sizes != ':' )
217 if ( *psz_sizes == ':' )
219 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
221 msg_Err( p_stream, "bad size %s for file %s", psz_size,
228 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
229 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
234 p_sys->i_nb_pictures++;
237 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
238 if ( val.psz_string )
240 char *psz_parser = strchr( val.psz_string, ':' );
244 *psz_parser++ = '\0';
245 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
246 / atoi( psz_parser );
250 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
251 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
254 free( val.psz_string );
258 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
261 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
262 p_sys->i_fd = net_ListenUDP1( p_stream, NULL, val.i_int );
263 if ( p_sys->i_fd < 0 )
269 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
270 p_sys->i_cmd = val.i_int;
271 p_sys->i_old_cmd = 0;
273 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
274 p_sys->i_gop = val.i_int;
276 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
277 p_sys->i_qscale = val.i_int;
279 var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
280 p_sys->b_audio = val.b_bool;
282 p_stream->pf_add = Add;
283 p_stream->pf_del = Del;
284 p_stream->pf_send = Send;
285 p_stream->p_sys = p_sys;
288 avcodec_register_all();
293 /*****************************************************************************
295 *****************************************************************************/
296 static void Close( vlc_object_t * p_this )
298 sout_stream_t *p_stream = (sout_stream_t *)p_this;
299 sout_stream_sys_t *p_sys = p_stream->p_sys;
301 sout_StreamDelete( p_sys->p_out );
306 /*****************************************************************************
307 * Add: Add an input elementary stream
308 *****************************************************************************/
309 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
311 sout_stream_sys_t *p_sys = p_stream->p_sys;
312 sout_stream_id_t *id;
314 id = malloc( sizeof( sout_stream_id_t ) );
315 memset( id, 0, sizeof( sout_stream_id_t ) );
318 if ( p_fmt->i_cat == VIDEO_ES
319 && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
320 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
322 id->b_switcher_video = VLC_TRUE;
323 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
325 "creating video switcher for fcc=`%4.4s' cmd:%d",
326 (char*)&p_fmt->i_codec, p_sys->i_cmd );
328 else if ( p_fmt->i_cat == AUDIO_ES
329 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a')
332 int i_ff_codec = CODEC_ID_MP2;
335 id->b_switcher_audio = VLC_TRUE;
337 "creating audio switcher for fcc=`%4.4s' cmd:%d",
338 (char*)&p_fmt->i_codec, p_sys->i_cmd );
340 /* Allocate the encoder right now. */
341 if( i_ff_codec == 0 )
343 msg_Err( p_stream, "cannot find encoder" );
347 id->ff_enc = avcodec_find_encoder( i_ff_codec );
350 msg_Err( p_stream, "cannot find encoder (avcodec)" );
354 id->ff_enc_c = avcodec_alloc_context();
356 /* Set CPU capabilities */
357 unsigned i_cpu = vlc_CPU();
358 id->ff_enc_c->dsp_mask = 0;
359 if( !(i_cpu & CPU_CAPABILITY_MMX) )
361 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
363 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
365 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
367 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
369 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
371 if( !(i_cpu & CPU_CAPABILITY_SSE) )
373 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
374 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
377 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
378 id->ff_enc_c->channels = p_fmt->audio.i_channels;
379 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
381 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
383 msg_Err( p_stream, "cannot open encoder" );
387 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
388 id->p_samples = malloc( id->ff_enc_c->frame_size
389 * p_fmt->audio.i_channels * sizeof(int16_t) );
390 memset( id->p_samples, 0,
391 id->ff_enc_c->frame_size * p_fmt->audio.i_channels
394 for ( i = 0; i < MAX_AUDIO; i++ )
396 if ( p_sys->pp_audio_ids[i] == NULL )
398 p_sys->pp_audio_ids[i] = id;
402 if ( i == MAX_AUDIO )
404 msg_Err( p_stream, "too many audio streams!" );
411 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
412 (char*)&p_fmt->i_codec );
416 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
418 /* open output stream */
419 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
421 if ( id->id == NULL )
430 /*****************************************************************************
431 * Del: Del an elementary stream
432 *****************************************************************************/
433 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
435 sout_stream_sys_t *p_sys = p_stream->p_sys;
437 if ( id->b_switcher_audio )
440 for ( i = 0; i < MAX_AUDIO; i++ )
442 if ( p_sys->pp_audio_ids[i] == id )
444 p_sys->pp_audio_ids[i] = NULL;
452 avcodec_close( id->ff_enc_c );
453 av_free( id->ff_enc_c );
454 av_free( id->p_frame );
455 free( id->p_buffer_out );
460 p_sys->p_out->pf_del( p_sys->p_out, id->id );
467 /*****************************************************************************
468 * Send: Process an input packet
469 *****************************************************************************/
470 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
473 sout_stream_sys_t *p_sys = p_stream->p_sys;
477 block_Release( p_buffer );
481 if ( !id->b_switcher_video && !id->b_switcher_audio )
483 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
486 block_ChainAppend( &id->p_queued, p_buffer );
488 if ( id->b_switcher_video )
490 /* Check for commands for every video frame. */
491 NetCommand( p_stream );
493 while ( id->p_queued != NULL )
498 if ( p_sys->i_old_cmd != p_sys->i_cmd )
500 i_dts = VideoCommand( p_stream, id );
503 i_dts = Process( p_stream, id, i_dts );
505 for ( i = 0; i < MAX_AUDIO; i++ )
507 if ( p_sys->pp_audio_ids[i] != NULL )
508 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
516 /*****************************************************************************
517 * Process: Process and dispatch buffers
518 *****************************************************************************/
519 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
522 sout_stream_sys_t *p_sys = p_stream->p_sys;
524 block_t *p_blocks = NULL;
525 block_t *p_blocks_out = NULL;
527 /* Find out the blocks we need. */
528 while ( id->p_queued != NULL
529 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
531 block_t *p_next = id->p_queued->p_next;
532 id->p_queued->p_next = NULL;
533 i_dts = id->p_queued->i_dts;
534 block_ChainAppend( &p_blocks, id->p_queued );
535 id->p_queued = p_next;
538 if ( p_sys->i_old_cmd == 0 )
541 if ( p_blocks != NULL )
542 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
546 if ( p_sys->i_old_cmd == -1 )
548 /* No output at all */
549 while ( p_blocks != NULL )
551 block_t * p_next = p_blocks->p_next;
552 block_Release( p_blocks );
558 while ( p_blocks != NULL )
560 block_t * p_next = p_blocks->p_next;
563 if ( id->b_switcher_video )
565 p_out = VideoGetBuffer( p_stream, id, p_blocks );
569 p_out = AudioGetBuffer( p_stream, id, p_blocks );
574 block_ChainAppend( &p_blocks_out, p_out );
578 if ( p_blocks_out != NULL )
579 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
583 /*****************************************************************************
584 * UnpackFromFile: Read a YUV picture and store it in our format
585 *****************************************************************************/
586 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
587 int i_width, int i_height,
591 FILE *p_file = utf8_fopen( psz_file, "r" );
593 if ( p_file == NULL )
595 msg_Err( p_stream, "file %s not found", psz_file );
599 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
601 i_width * VOUT_ASPECT_FACTOR / i_height );
602 for ( i = 0; i < p_pic->i_planes; i++ )
604 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
605 p_pic->p[i].i_pitch );
606 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
607 p_pic->p[i].i_pitch );
610 for ( i = 0; i < i_height; i++ )
613 uint8_t p_buffer[i_width * 2];
614 uint8_t *p_char = p_buffer;
615 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
616 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
617 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
619 if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
621 msg_Err( p_stream, "premature end of file %s", psz_file );
623 for ( i = 0; i < p_pic->i_planes; i++ )
625 free( p_pic->p[i].p_pixels );
631 for ( j = 0; j < i_width; j++ )
633 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
634 i_chroma = !i_chroma;
636 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
638 **pp_chroma = *p_char;
649 /*****************************************************************************
650 * NetCommand: Get a command from the network
651 *****************************************************************************/
652 static void NetCommand( sout_stream_t *p_stream )
654 sout_stream_sys_t *p_sys = p_stream->p_sys;
656 int i_len = recv( p_sys->i_fd, psz_buffer, sizeof( psz_buffer ) - 1, 0 );
660 psz_buffer[i_len] = '\0';
661 int i_cmd = strtol( psz_buffer, NULL, 0 );
662 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
664 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
668 p_sys->i_cmd = i_cmd;
670 msg_Dbg( p_stream, "new command: %d old:%d", p_sys->i_cmd,
675 /*****************************************************************************
676 * VideoCommand: Create/Delete a video encoder
677 *****************************************************************************/
678 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
680 sout_stream_sys_t *p_sys = p_stream->p_sys;
682 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
684 mtime_t i_dts = id->p_queued->i_dts;
685 block_t *p_block = id->p_queued->p_next;
687 while ( p_block != NULL )
689 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
691 i_dts = p_block->i_dts;
692 p_block = p_block->p_next;
698 p_sys->i_old_cmd = p_sys->i_cmd;
702 avcodec_close( id->ff_enc_c );
703 av_free( id->ff_enc_c );
704 av_free( id->p_frame );
705 free( id->p_buffer_out );
709 if ( p_sys->i_cmd > 0 )
711 /* Create a new encoder. */
712 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
713 int i_aspect_num, i_aspect_den;
715 if( i_ff_codec == 0 )
717 msg_Err( p_stream, "cannot find encoder" );
721 id->ff_enc = avcodec_find_encoder( i_ff_codec );
724 msg_Err( p_stream, "cannot find encoder (avcodec)" );
728 id->ff_enc_c = avcodec_alloc_context();
730 /* Set CPU capabilities */
731 unsigned i_cpu = vlc_CPU();
732 id->ff_enc_c->dsp_mask = 0;
733 if( !(i_cpu & CPU_CAPABILITY_MMX) )
735 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
737 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
739 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
741 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
743 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
745 if( !(i_cpu & CPU_CAPABILITY_SSE) )
747 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
748 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
751 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
752 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
753 av_reduce( &i_aspect_num, &i_aspect_den,
755 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
756 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
757 &id->ff_enc_c->sample_aspect_ratio.den,
758 i_aspect_num * (int64_t)id->ff_enc_c->height,
759 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
761 #if LIBAVCODEC_BUILD >= 4754
762 id->ff_enc_c->time_base.num = 1;
763 id->ff_enc_c->time_base.den = 25; /* FIXME */
765 id->ff_enc_c->frame_rate = 25; /* FIXME */
766 id->ff_enc_c->frame_rate_base = 1;
769 id->ff_enc_c->gop_size = 200;
770 id->ff_enc_c->max_b_frames = 0;
772 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
773 | CODEC_FLAG_INPUT_PRESERVED
774 | CODEC_FLAG_LOW_DELAY;
776 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
777 id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
779 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
781 msg_Err( p_stream, "cannot open encoder" );
785 id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
786 id->p_frame = avcodec_alloc_frame();
787 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
788 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
789 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
790 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
791 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
792 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
794 id->i_nb_pred = p_sys->i_gop;
800 /*****************************************************************************
801 * VideoGetBuffer: Build an alternate video buffer
802 *****************************************************************************/
803 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
806 sout_stream_sys_t *p_sys = p_stream->p_sys;
810 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
812 id->p_frame->interlaced_frame = 0;
813 id->p_frame->top_field_first = 1;
814 id->p_frame->pts = p_buffer->i_dts;
816 if ( id->i_nb_pred >= p_sys->i_gop )
818 id->p_frame->pict_type = FF_I_TYPE;
820 id->p_frame->me_threshold = 0;
821 id->p_frame->mb_threshold = 0;
827 id->p_frame->pict_type = FF_P_TYPE;
829 if ( id->p_frame->mb_type != NULL )
831 id->p_frame->me_threshold = MAX_THRESHOLD;
832 id->p_frame->mb_threshold = MAX_THRESHOLD;
838 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
839 id->ff_enc_c->width * id->ff_enc_c->height * 3,
846 if ( id->p_frame->mb_type == NULL
847 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
849 int mb_width = (id->ff_enc_c->width + 15) / 16;
850 int mb_height = (id->ff_enc_c->height + 15) / 16;
851 int h_chroma_shift, v_chroma_shift;
854 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
857 id->p_frame->motion_subsample_log2
858 = id->ff_enc_c->coded_frame->motion_subsample_log2;
859 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
860 * sizeof(uint32_t) );
861 p_stream->p_libvlc->pf_memcpy( id->p_frame->mb_type,
862 id->ff_enc_c->coded_frame->mb_type,
863 (mb_width + 1) * mb_height
864 * sizeof(id->p_frame->mb_type[0]));
866 for ( i = 0; i < 2; i++ )
868 int stride = ((16 * mb_width )
869 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
870 int height = ((16 * mb_height)
871 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
872 int b8_stride = mb_width * 2 + 1;
874 if ( id->ff_enc_c->coded_frame->motion_val[i] )
876 id->p_frame->motion_val[i] = malloc( 2 * stride * height
878 p_stream->p_libvlc->pf_memcpy( id->p_frame->motion_val[i],
879 id->ff_enc_c->coded_frame->motion_val[i],
880 2 * stride * height * sizeof(int16_t) );
882 if ( id->ff_enc_c->coded_frame->ref_index[i] )
884 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
886 p_stream->p_libvlc->pf_memcpy( id->p_frame->ref_index[i],
887 id->ff_enc_c->coded_frame->ref_index[i],
888 b8_stride * 2 * mb_height * sizeof(int8_t));
894 p_out = block_New( p_stream, i_out );
895 p_stream->p_libvlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
896 p_out->i_length = p_buffer->i_length;
897 p_out->i_pts = p_buffer->i_dts;
898 p_out->i_dts = p_buffer->i_dts;
899 p_out->i_rate = p_buffer->i_rate;
901 switch ( id->ff_enc_c->coded_frame->pict_type )
904 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
907 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
910 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
916 block_Release( p_buffer );
921 /*****************************************************************************
922 * AudioGetBuffer: Build an alternate audio buffer
923 *****************************************************************************/
924 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
930 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
931 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
937 p_out = block_New( p_stream, i_out );
938 p_stream->p_libvlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
939 p_out->i_length = p_buffer->i_length;
940 p_out->i_pts = p_buffer->i_dts;
941 p_out->i_dts = p_buffer->i_dts;
942 p_out->i_rate = p_buffer->i_rate;
944 block_Release( p_buffer );