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_LIBAVCODEC_AVCODEC_H
42 # include <libavcodec/avcodec.h>
43 #elif defined(HAVE_FFMPEG_AVCODEC_H)
44 # include <ffmpeg/avcodec.h>
49 #ifdef HAVE_POSTPROC_POSTPROCESS_H
50 # include <postproc/postprocess.h>
52 # include <libpostproc/postprocess.h>
55 #define SOUT_CFG_PREFIX "sout-switcher-"
56 #define MAX_PICTURES 10
58 #define MAX_THRESHOLD 99999999
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
66 static sout_stream_id_t *Add( sout_stream_t *, es_format_t * );
67 static int Del( sout_stream_t *, sout_stream_id_t * );
68 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
70 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
72 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
73 int i_width, int i_height,
75 static void NetCommand( sout_stream_t *p_stream );
76 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id );
77 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
79 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
82 /*****************************************************************************
84 *****************************************************************************/
85 #define FILES_TEXT N_("Files")
86 #define FILES_LONGTEXT N_( \
87 "Full paths of the files separated by colons." )
88 #define SIZES_TEXT N_("Sizes")
89 #define SIZES_LONGTEXT N_( \
90 "List of sizes separated by colons (720x576:480x576)." )
91 #define RATIO_TEXT N_("Aspect ratio")
92 #define RATIO_LONGTEXT N_( \
93 "Aspect ratio (4:3, 16:9)." )
94 #define PORT_TEXT N_("Command UDP port")
95 #define PORT_LONGTEXT N_( \
96 "UDP port to listen to for commands." )
97 #define COMMAND_TEXT N_("Command")
98 #define COMMAND_LONGTEXT N_( \
99 "Initial command to execute." )
100 #define GOP_TEXT N_("GOP size")
101 #define GOP_LONGTEXT N_( \
102 "Number of P frames between two I frames." )
103 #define QSCALE_TEXT N_("Quantizer scale")
104 #define QSCALE_LONGTEXT N_( \
105 "Fixed quantizer scale to use." )
106 #define AUDIO_TEXT N_("Mute audio")
107 #define AUDIO_LONGTEXT N_( \
108 "Mute audio when command is not 0." )
111 set_description( _("MPEG2 video switcher stream output") );
112 set_capability( "sout stream", 50 );
113 add_shortcut( "switcher" );
114 set_callbacks( Open, Close );
116 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
117 FILES_LONGTEXT, VLC_FALSE );
118 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
119 SIZES_LONGTEXT, VLC_FALSE );
120 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
121 RATIO_LONGTEXT, VLC_FALSE );
122 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
123 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
124 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
125 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
126 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
127 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
128 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
129 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
130 add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
131 AUDIO_TEXT, AUDIO_LONGTEXT, VLC_TRUE );
134 static const char *ppsz_sout_options[] = {
135 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
139 struct sout_stream_sys_t
141 sout_stream_t *p_out;
145 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
149 picture_t p_pictures[MAX_PICTURES];
154 int i_cmd, i_old_cmd;
157 struct sout_stream_id_t
160 vlc_bool_t b_switcher_video;
161 vlc_bool_t b_switcher_audio;
167 AVCodecContext *ff_enc_c;
169 uint8_t *p_buffer_out;
174 /*****************************************************************************
176 *****************************************************************************/
177 static int Open( vlc_object_t *p_this )
179 sout_stream_t *p_stream = (sout_stream_t*)p_this;
180 sout_stream_sys_t *p_sys;
182 char *psz_files, *psz_sizes;
183 int i_height = 0, i_width = 0;
185 p_sys = malloc( sizeof(sout_stream_sys_t) );
186 memset( p_sys, 0, sizeof(sout_stream_sys_t) );
188 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
191 msg_Err( p_stream, "cannot create chain" );
196 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
199 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
200 psz_files = val.psz_string;
201 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
202 psz_sizes = val.psz_string;
204 p_sys->i_nb_pictures = 0;
205 while ( psz_files && *psz_files )
207 char * psz_file = psz_files;
208 char * psz_size = psz_sizes;
210 while ( *psz_files && *psz_files != ':' )
212 if ( *psz_files == ':' )
217 while ( *psz_sizes && *psz_sizes != ':' )
219 if ( *psz_sizes == ':' )
221 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
223 msg_Err( p_stream, "bad size %s for file %s", psz_size,
230 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
231 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
236 p_sys->i_nb_pictures++;
239 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
240 if ( val.psz_string )
242 char *psz_parser = strchr( val.psz_string, ':' );
246 *psz_parser++ = '\0';
247 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
248 / atoi( psz_parser );
252 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
253 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
256 free( val.psz_string );
260 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
263 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
264 p_sys->i_fd = net_ListenUDP1( p_stream, NULL, val.i_int );
265 if ( p_sys->i_fd < 0 )
271 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
272 p_sys->i_cmd = val.i_int;
273 p_sys->i_old_cmd = 0;
275 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
276 p_sys->i_gop = val.i_int;
278 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
279 p_sys->i_qscale = val.i_int;
281 var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
282 p_sys->b_audio = val.b_bool;
284 p_stream->pf_add = Add;
285 p_stream->pf_del = Del;
286 p_stream->pf_send = Send;
287 p_stream->p_sys = p_sys;
290 avcodec_register_all();
295 /*****************************************************************************
297 *****************************************************************************/
298 static void Close( vlc_object_t * p_this )
300 sout_stream_t *p_stream = (sout_stream_t *)p_this;
301 sout_stream_sys_t *p_sys = p_stream->p_sys;
303 sout_StreamDelete( p_sys->p_out );
308 /*****************************************************************************
309 * Add: Add an input elementary stream
310 *****************************************************************************/
311 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
313 sout_stream_sys_t *p_sys = p_stream->p_sys;
314 sout_stream_id_t *id;
316 id = malloc( sizeof( sout_stream_id_t ) );
317 memset( id, 0, sizeof( sout_stream_id_t ) );
320 if ( p_fmt->i_cat == VIDEO_ES
321 && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
322 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
324 id->b_switcher_video = VLC_TRUE;
325 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
327 "creating video switcher for fcc=`%4.4s' cmd:%d",
328 (char*)&p_fmt->i_codec, p_sys->i_cmd );
330 else if ( p_fmt->i_cat == AUDIO_ES
331 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a')
334 int i_ff_codec = CODEC_ID_MP2;
337 id->b_switcher_audio = VLC_TRUE;
339 "creating audio switcher for fcc=`%4.4s' cmd:%d",
340 (char*)&p_fmt->i_codec, p_sys->i_cmd );
342 /* Allocate the encoder right now. */
343 if( i_ff_codec == 0 )
345 msg_Err( p_stream, "cannot find encoder" );
349 id->ff_enc = avcodec_find_encoder( i_ff_codec );
352 msg_Err( p_stream, "cannot find encoder (avcodec)" );
356 id->ff_enc_c = avcodec_alloc_context();
358 /* Set CPU capabilities */
359 unsigned i_cpu = vlc_CPU();
360 id->ff_enc_c->dsp_mask = 0;
361 if( !(i_cpu & CPU_CAPABILITY_MMX) )
363 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
365 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
367 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
369 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
371 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
373 if( !(i_cpu & CPU_CAPABILITY_SSE) )
375 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
376 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
379 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
380 id->ff_enc_c->channels = p_fmt->audio.i_channels;
381 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
383 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
385 msg_Err( p_stream, "cannot open encoder" );
389 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
390 id->p_samples = malloc( id->ff_enc_c->frame_size
391 * p_fmt->audio.i_channels * sizeof(int16_t) );
392 memset( id->p_samples, 0,
393 id->ff_enc_c->frame_size * p_fmt->audio.i_channels
396 for ( i = 0; i < MAX_AUDIO; i++ )
398 if ( p_sys->pp_audio_ids[i] == NULL )
400 p_sys->pp_audio_ids[i] = id;
404 if ( i == MAX_AUDIO )
406 msg_Err( p_stream, "too many audio streams!" );
413 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
414 (char*)&p_fmt->i_codec );
418 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
420 /* open output stream */
421 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
423 if ( id->id == NULL )
432 /*****************************************************************************
433 * Del: Del an elementary stream
434 *****************************************************************************/
435 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
437 sout_stream_sys_t *p_sys = p_stream->p_sys;
439 if ( id->b_switcher_audio )
442 for ( i = 0; i < MAX_AUDIO; i++ )
444 if ( p_sys->pp_audio_ids[i] == id )
446 p_sys->pp_audio_ids[i] = NULL;
454 avcodec_close( id->ff_enc_c );
455 av_free( id->ff_enc_c );
456 av_free( id->p_frame );
457 free( id->p_buffer_out );
462 p_sys->p_out->pf_del( p_sys->p_out, id->id );
469 /*****************************************************************************
470 * Send: Process an input packet
471 *****************************************************************************/
472 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
475 sout_stream_sys_t *p_sys = p_stream->p_sys;
479 block_Release( p_buffer );
483 if ( !id->b_switcher_video && !id->b_switcher_audio )
485 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
488 block_ChainAppend( &id->p_queued, p_buffer );
490 if ( id->b_switcher_video )
492 /* Check for commands for every video frame. */
493 NetCommand( p_stream );
495 while ( id->p_queued != NULL )
500 if ( p_sys->i_old_cmd != p_sys->i_cmd )
502 i_dts = VideoCommand( p_stream, id );
505 i_dts = Process( p_stream, id, i_dts );
507 for ( i = 0; i < MAX_AUDIO; i++ )
509 if ( p_sys->pp_audio_ids[i] != NULL )
510 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
518 /*****************************************************************************
519 * Process: Process and dispatch buffers
520 *****************************************************************************/
521 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
524 sout_stream_sys_t *p_sys = p_stream->p_sys;
526 block_t *p_blocks = NULL;
527 block_t *p_blocks_out = NULL;
529 /* Find out the blocks we need. */
530 while ( id->p_queued != NULL
531 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
533 block_t *p_next = id->p_queued->p_next;
534 id->p_queued->p_next = NULL;
535 i_dts = id->p_queued->i_dts;
536 block_ChainAppend( &p_blocks, id->p_queued );
537 id->p_queued = p_next;
540 if ( p_sys->i_old_cmd == 0 )
543 if ( p_blocks != NULL )
544 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
548 if ( p_sys->i_old_cmd == -1 )
550 /* No output at all */
551 while ( p_blocks != NULL )
553 block_t * p_next = p_blocks->p_next;
554 block_Release( p_blocks );
560 while ( p_blocks != NULL )
562 block_t * p_next = p_blocks->p_next;
565 if ( id->b_switcher_video )
567 p_out = VideoGetBuffer( p_stream, id, p_blocks );
571 p_out = AudioGetBuffer( p_stream, id, p_blocks );
576 block_ChainAppend( &p_blocks_out, p_out );
580 if ( p_blocks_out != NULL )
581 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
585 /*****************************************************************************
586 * UnpackFromFile: Read a YUV picture and store it in our format
587 *****************************************************************************/
588 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
589 int i_width, int i_height,
593 FILE *p_file = utf8_fopen( psz_file, "r" );
595 if ( p_file == NULL )
597 msg_Err( p_stream, "file %s not found", psz_file );
601 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
603 i_width * VOUT_ASPECT_FACTOR / i_height );
604 for ( i = 0; i < p_pic->i_planes; i++ )
606 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
607 p_pic->p[i].i_pitch );
608 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
609 p_pic->p[i].i_pitch );
612 for ( i = 0; i < i_height; i++ )
615 uint8_t p_buffer[i_width * 2];
616 uint8_t *p_char = p_buffer;
617 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
618 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
619 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
621 if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
623 msg_Err( p_stream, "premature end of file %s", psz_file );
625 for ( i = 0; i < p_pic->i_planes; i++ )
627 free( p_pic->p[i].p_pixels );
633 for ( j = 0; j < i_width; j++ )
635 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
636 i_chroma = !i_chroma;
638 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
640 **pp_chroma = *p_char;
651 /*****************************************************************************
652 * NetCommand: Get a command from the network
653 *****************************************************************************/
654 static void NetCommand( sout_stream_t *p_stream )
656 sout_stream_sys_t *p_sys = p_stream->p_sys;
658 int i_len = recv( p_sys->i_fd, psz_buffer, sizeof( psz_buffer ) - 1, 0 );
662 psz_buffer[i_len] = '\0';
663 int i_cmd = strtol( psz_buffer, NULL, 0 );
664 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
666 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
670 p_sys->i_cmd = i_cmd;
672 msg_Dbg( p_stream, "new command: %d old:%d", p_sys->i_cmd,
677 /*****************************************************************************
678 * VideoCommand: Create/Delete a video encoder
679 *****************************************************************************/
680 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
682 sout_stream_sys_t *p_sys = p_stream->p_sys;
684 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
686 mtime_t i_dts = id->p_queued->i_dts;
687 block_t *p_block = id->p_queued->p_next;
689 while ( p_block != NULL )
691 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
693 i_dts = p_block->i_dts;
694 p_block = p_block->p_next;
700 p_sys->i_old_cmd = p_sys->i_cmd;
704 avcodec_close( id->ff_enc_c );
705 av_free( id->ff_enc_c );
706 av_free( id->p_frame );
707 free( id->p_buffer_out );
711 if ( p_sys->i_cmd > 0 )
713 /* Create a new encoder. */
714 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
715 int i_aspect_num, i_aspect_den;
717 if( i_ff_codec == 0 )
719 msg_Err( p_stream, "cannot find encoder" );
723 id->ff_enc = avcodec_find_encoder( i_ff_codec );
726 msg_Err( p_stream, "cannot find encoder (avcodec)" );
730 id->ff_enc_c = avcodec_alloc_context();
732 /* Set CPU capabilities */
733 unsigned i_cpu = vlc_CPU();
734 id->ff_enc_c->dsp_mask = 0;
735 if( !(i_cpu & CPU_CAPABILITY_MMX) )
737 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
739 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
741 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
743 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
745 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
747 if( !(i_cpu & CPU_CAPABILITY_SSE) )
749 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
750 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
753 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
754 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
755 av_reduce( &i_aspect_num, &i_aspect_den,
757 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
758 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
759 &id->ff_enc_c->sample_aspect_ratio.den,
760 i_aspect_num * (int64_t)id->ff_enc_c->height,
761 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
763 #if LIBAVCODEC_BUILD >= 4754
764 id->ff_enc_c->time_base.num = 1;
765 id->ff_enc_c->time_base.den = 25; /* FIXME */
767 id->ff_enc_c->frame_rate = 25; /* FIXME */
768 id->ff_enc_c->frame_rate_base = 1;
771 id->ff_enc_c->gop_size = 200;
772 id->ff_enc_c->max_b_frames = 0;
774 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
775 | CODEC_FLAG_INPUT_PRESERVED
776 | CODEC_FLAG_LOW_DELAY;
778 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
779 id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
781 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
783 msg_Err( p_stream, "cannot open encoder" );
787 id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
788 id->p_frame = avcodec_alloc_frame();
789 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
790 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
791 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
792 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
793 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
794 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
796 id->i_nb_pred = p_sys->i_gop;
802 /*****************************************************************************
803 * VideoGetBuffer: Build an alternate video buffer
804 *****************************************************************************/
805 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
808 sout_stream_sys_t *p_sys = p_stream->p_sys;
812 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
814 id->p_frame->interlaced_frame = 0;
815 id->p_frame->top_field_first = 1;
816 id->p_frame->pts = p_buffer->i_dts;
818 if ( id->i_nb_pred >= p_sys->i_gop )
820 id->p_frame->pict_type = FF_I_TYPE;
822 id->p_frame->me_threshold = 0;
823 id->p_frame->mb_threshold = 0;
829 id->p_frame->pict_type = FF_P_TYPE;
831 if ( id->p_frame->mb_type != NULL )
833 id->p_frame->me_threshold = MAX_THRESHOLD;
834 id->p_frame->mb_threshold = MAX_THRESHOLD;
840 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
841 id->ff_enc_c->width * id->ff_enc_c->height * 3,
848 if ( id->p_frame->mb_type == NULL
849 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
851 int mb_width = (id->ff_enc_c->width + 15) / 16;
852 int mb_height = (id->ff_enc_c->height + 15) / 16;
853 int h_chroma_shift, v_chroma_shift;
856 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
859 id->p_frame->motion_subsample_log2
860 = id->ff_enc_c->coded_frame->motion_subsample_log2;
861 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
862 * sizeof(uint32_t) );
863 p_stream->p_libvlc->pf_memcpy( id->p_frame->mb_type,
864 id->ff_enc_c->coded_frame->mb_type,
865 (mb_width + 1) * mb_height
866 * sizeof(id->p_frame->mb_type[0]));
868 for ( i = 0; i < 2; i++ )
870 int stride = ((16 * mb_width )
871 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
872 int height = ((16 * mb_height)
873 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
874 int b8_stride = mb_width * 2 + 1;
876 if ( id->ff_enc_c->coded_frame->motion_val[i] )
878 id->p_frame->motion_val[i] = malloc( 2 * stride * height
880 p_stream->p_libvlc->pf_memcpy( id->p_frame->motion_val[i],
881 id->ff_enc_c->coded_frame->motion_val[i],
882 2 * stride * height * sizeof(int16_t) );
884 if ( id->ff_enc_c->coded_frame->ref_index[i] )
886 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
888 p_stream->p_libvlc->pf_memcpy( id->p_frame->ref_index[i],
889 id->ff_enc_c->coded_frame->ref_index[i],
890 b8_stride * 2 * mb_height * sizeof(int8_t));
896 p_out = block_New( p_stream, i_out );
897 p_stream->p_libvlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
898 p_out->i_length = p_buffer->i_length;
899 p_out->i_pts = p_buffer->i_dts;
900 p_out->i_dts = p_buffer->i_dts;
901 p_out->i_rate = p_buffer->i_rate;
903 switch ( id->ff_enc_c->coded_frame->pict_type )
906 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
909 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
912 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
918 block_Release( p_buffer );
923 /*****************************************************************************
924 * AudioGetBuffer: Build an alternate audio buffer
925 *****************************************************************************/
926 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
932 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
933 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
939 p_out = block_New( p_stream, i_out );
940 p_stream->p_libvlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
941 p_out->i_length = p_buffer->i_length;
942 p_out->i_pts = p_buffer->i_dts;
943 p_out->i_dts = p_buffer->i_dts;
944 p_out->i_rate = p_buffer->i_rate;
946 block_Release( p_buffer );