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>
38 #include <vlc_block.h>
40 #include <vlc_charset.h>
41 #include <vlc_network.h>
44 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
45 # include <libavcodec/avcodec.h>
46 #elif defined(HAVE_FFMPEG_AVCODEC_H)
47 # include <ffmpeg/avcodec.h>
52 #ifdef HAVE_POSTPROC_POSTPROCESS_H
53 # include <postproc/postprocess.h>
55 # include <libpostproc/postprocess.h>
58 #define SOUT_CFG_PREFIX "sout-switcher-"
59 #define MAX_PICTURES 10
61 #define MAX_THRESHOLD 99999999
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
69 static sout_stream_id_t *Add( sout_stream_t *, es_format_t * );
70 static int Del( sout_stream_t *, sout_stream_id_t * );
71 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
73 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
75 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
76 int i_width, int i_height,
78 static void NetCommand( sout_stream_t *p_stream );
79 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id );
80 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
82 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
85 /*****************************************************************************
87 *****************************************************************************/
88 #define FILES_TEXT N_("Files")
89 #define FILES_LONGTEXT N_( \
90 "Full paths of the files separated by colons." )
91 #define SIZES_TEXT N_("Sizes")
92 #define SIZES_LONGTEXT N_( \
93 "List of sizes separated by colons (720x576:480x576)." )
94 #define RATIO_TEXT N_("Aspect ratio")
95 #define RATIO_LONGTEXT N_( \
96 "Aspect ratio (4:3, 16:9)." )
97 #define PORT_TEXT N_("Command UDP port")
98 #define PORT_LONGTEXT N_( \
99 "UDP port to listen to for commands." )
100 #define COMMAND_TEXT N_("Command")
101 #define COMMAND_LONGTEXT N_( \
102 "Initial command to execute." )
103 #define GOP_TEXT N_("GOP size")
104 #define GOP_LONGTEXT N_( \
105 "Number of P frames between two I frames." )
106 #define QSCALE_TEXT N_("Quantizer scale")
107 #define QSCALE_LONGTEXT N_( \
108 "Fixed quantizer scale to use." )
109 #define AUDIO_TEXT N_("Mute audio")
110 #define AUDIO_LONGTEXT N_( \
111 "Mute audio when command is not 0." )
114 set_description( N_("MPEG2 video switcher stream output") )
115 set_capability( "sout stream", 50 )
116 add_shortcut( "switcher" )
117 set_callbacks( Open, Close )
119 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
120 FILES_LONGTEXT, false );
121 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
122 SIZES_LONGTEXT, false );
123 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
124 RATIO_LONGTEXT, false );
125 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
126 PORT_TEXT, PORT_LONGTEXT, true );
127 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
128 COMMAND_TEXT, COMMAND_LONGTEXT, true );
129 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
130 GOP_TEXT, GOP_LONGTEXT, true );
131 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
132 QSCALE_TEXT, QSCALE_LONGTEXT, true );
133 add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
134 AUDIO_TEXT, AUDIO_LONGTEXT, true );
137 static const char *const ppsz_sout_options[] = {
138 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
142 struct sout_stream_sys_t
144 sout_stream_t *p_out;
148 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
152 picture_t p_pictures[MAX_PICTURES];
157 int i_cmd, i_old_cmd;
160 struct sout_stream_id_t
163 bool b_switcher_video;
164 bool b_switcher_audio;
170 AVCodecContext *ff_enc_c;
172 uint8_t *p_buffer_out;
177 /*****************************************************************************
179 *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
182 sout_stream_t *p_stream = (sout_stream_t*)p_this;
183 sout_stream_sys_t *p_sys;
185 char *psz_files, *psz_sizes;
186 int i_height = 0, i_width = 0;
188 p_sys = calloc( 1, sizeof(sout_stream_sys_t) );
192 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
195 msg_Err( p_stream, "cannot create chain" );
200 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
203 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
204 psz_files = val.psz_string;
205 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
206 psz_sizes = val.psz_string;
208 p_sys->i_nb_pictures = 0;
209 while ( psz_files && *psz_files )
211 char * psz_file = psz_files;
212 char * psz_size = psz_sizes;
214 while ( *psz_files && *psz_files != ':' )
216 if ( *psz_files == ':' )
221 while ( *psz_sizes && *psz_sizes != ':' )
223 if ( *psz_sizes == ':' )
225 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
227 msg_Err( p_stream, "bad size %s for file %s", psz_size,
234 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
235 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
240 p_sys->i_nb_pictures++;
243 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
244 if ( val.psz_string )
246 char *psz_parser = strchr( val.psz_string, ':' );
250 *psz_parser++ = '\0';
251 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
252 / atoi( psz_parser );
256 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
257 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
260 free( val.psz_string );
264 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
267 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
268 p_sys->i_fd = net_ListenUDP1( p_stream, NULL, val.i_int );
269 if ( p_sys->i_fd < 0 )
275 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
276 p_sys->i_cmd = val.i_int;
277 p_sys->i_old_cmd = 0;
279 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
280 p_sys->i_gop = val.i_int;
282 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
283 p_sys->i_qscale = val.i_int;
285 var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
286 p_sys->b_audio = val.b_bool;
288 p_stream->pf_add = Add;
289 p_stream->pf_del = Del;
290 p_stream->pf_send = Send;
291 p_stream->p_sys = p_sys;
294 avcodec_register_all();
299 /*****************************************************************************
301 *****************************************************************************/
302 static void Close( vlc_object_t * p_this )
304 sout_stream_t *p_stream = (sout_stream_t *)p_this;
305 sout_stream_sys_t *p_sys = p_stream->p_sys;
307 sout_StreamDelete( p_sys->p_out );
312 /*****************************************************************************
313 * Add: Add an input elementary stream
314 *****************************************************************************/
315 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
317 sout_stream_sys_t *p_sys = p_stream->p_sys;
318 sout_stream_id_t *id;
320 id = calloc( 1, sizeof( sout_stream_id_t ) );
324 if( p_fmt->i_cat == VIDEO_ES &&
325 ( p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') ||
326 p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e') ) )
328 id->b_switcher_video = true;
329 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
330 msg_Dbg( p_stream, "creating video switcher for fcc=`%4.4s' cmd:%d",
331 (char*)&p_fmt->i_codec, p_sys->i_cmd );
333 else if ( p_fmt->i_cat == AUDIO_ES &&
334 p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a') &&
337 int i_ff_codec = CODEC_ID_MP2;
340 id->b_switcher_audio = true;
341 msg_Dbg( p_stream, "creating audio switcher for fcc=`%4.4s' cmd:%d",
342 (char*)&p_fmt->i_codec, p_sys->i_cmd );
344 /* Allocate the encoder right now. */
345 if( i_ff_codec == 0 )
347 msg_Err( p_stream, "cannot find encoder" );
352 id->ff_enc = avcodec_find_encoder( i_ff_codec );
355 msg_Err( p_stream, "cannot find encoder (avcodec)" );
360 id->ff_enc_c = avcodec_alloc_context();
362 /* Set CPU capabilities */
363 unsigned i_cpu = vlc_CPU();
364 id->ff_enc_c->dsp_mask = 0;
365 if( !(i_cpu & CPU_CAPABILITY_MMX) )
367 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
369 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
371 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
373 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
375 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
377 if( !(i_cpu & CPU_CAPABILITY_SSE) )
379 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
380 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
383 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
384 id->ff_enc_c->channels = p_fmt->audio.i_channels;
385 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
387 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
389 msg_Err( p_stream, "cannot open encoder" );
390 av_free( id->ff_enc_c );
395 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
396 id->p_samples = calloc( id->ff_enc_c->frame_size * p_fmt->audio.i_channels,
398 if( !id->p_buffer_out || !id->p_samples )
401 for( i = 0; i < MAX_AUDIO; i++ )
403 if( p_sys->pp_audio_ids[i] == NULL )
405 p_sys->pp_audio_ids[i] = id;
411 msg_Err( p_stream, "too many audio streams!" );
417 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
418 (char*)&p_fmt->i_codec );
422 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
424 /* open output stream */
425 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
431 avcodec_close( id->ff_enc_c );
432 free( id->p_samples );
433 free( id->p_buffer_out );
434 av_free( id->ff_enc_c );
439 /*****************************************************************************
440 * Del: Del an elementary stream
441 *****************************************************************************/
442 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
444 sout_stream_sys_t *p_sys = p_stream->p_sys;
446 if ( id->b_switcher_audio )
449 for ( i = 0; i < MAX_AUDIO; i++ )
451 if ( p_sys->pp_audio_ids[i] == id )
453 p_sys->pp_audio_ids[i] = NULL;
461 avcodec_close( id->ff_enc_c );
462 av_free( id->ff_enc_c );
463 av_free( id->p_frame );
464 free( id->p_buffer_out );
469 p_sys->p_out->pf_del( p_sys->p_out, id->id );
476 /*****************************************************************************
477 * Send: Process an input packet
478 *****************************************************************************/
479 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
482 sout_stream_sys_t *p_sys = p_stream->p_sys;
486 block_Release( p_buffer );
490 if ( !id->b_switcher_video && !id->b_switcher_audio )
492 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
495 block_ChainAppend( &id->p_queued, p_buffer );
497 if ( id->b_switcher_video )
499 /* Check for commands for every video frame. */
500 NetCommand( p_stream );
502 while ( id->p_queued != NULL )
507 if ( p_sys->i_old_cmd != p_sys->i_cmd )
509 i_dts = VideoCommand( p_stream, id );
512 i_dts = Process( p_stream, id, i_dts );
514 for ( i = 0; i < MAX_AUDIO; i++ )
516 if ( p_sys->pp_audio_ids[i] != NULL )
517 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
525 /*****************************************************************************
526 * Process: Process and dispatch buffers
527 *****************************************************************************/
528 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
531 sout_stream_sys_t *p_sys = p_stream->p_sys;
533 block_t *p_blocks = NULL;
534 block_t *p_blocks_out = NULL;
536 /* Find out the blocks we need. */
537 while ( id->p_queued != NULL
538 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
540 block_t *p_next = id->p_queued->p_next;
541 id->p_queued->p_next = NULL;
542 i_dts = id->p_queued->i_dts;
543 block_ChainAppend( &p_blocks, id->p_queued );
544 id->p_queued = p_next;
547 if ( p_sys->i_old_cmd == 0 )
550 if ( p_blocks != NULL )
551 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
555 if ( p_sys->i_old_cmd == -1 )
557 /* No output at all */
558 while ( p_blocks != NULL )
560 block_t * p_next = p_blocks->p_next;
561 block_Release( p_blocks );
567 while ( p_blocks != NULL )
569 block_t * p_next = p_blocks->p_next;
572 if ( id->b_switcher_video )
574 p_out = VideoGetBuffer( p_stream, id, p_blocks );
578 p_out = AudioGetBuffer( p_stream, id, p_blocks );
583 block_ChainAppend( &p_blocks_out, p_out );
587 if ( p_blocks_out != NULL )
588 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
592 /*****************************************************************************
593 * UnpackFromFile: Read a YUV picture and store it in our format
594 *****************************************************************************/
595 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
596 int i_width, int i_height,
600 FILE *p_file = utf8_fopen( psz_file, "r" );
602 if ( p_file == NULL )
604 msg_Err( p_stream, "file %s not found", psz_file );
608 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
610 i_width * VOUT_ASPECT_FACTOR / i_height );
611 for ( i = 0; i < p_pic->i_planes; i++ )
613 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
614 p_pic->p[i].i_pitch );
615 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
616 p_pic->p[i].i_pitch );
619 for ( i = 0; i < i_height; i++ )
622 uint8_t p_buffer[i_width * 2];
623 uint8_t *p_char = p_buffer;
624 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
625 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
626 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
628 if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
630 msg_Err( p_stream, "premature end of file %s", psz_file );
632 for ( i = 0; i < p_pic->i_planes; i++ )
634 free( p_pic->p[i].p_pixels );
640 for ( j = 0; j < i_width; j++ )
642 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
643 i_chroma = !i_chroma;
645 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
647 **pp_chroma = *p_char;
658 /*****************************************************************************
659 * NetCommand: Get a command from the network
660 *****************************************************************************/
661 static void NetCommand( sout_stream_t *p_stream )
663 sout_stream_sys_t *p_sys = p_stream->p_sys;
665 int i_len = recv( p_sys->i_fd, psz_buffer, sizeof( psz_buffer ) - 1, 0 );
669 psz_buffer[i_len] = '\0';
670 int i_cmd = strtol( psz_buffer, NULL, 0 );
671 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
673 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
677 p_sys->i_cmd = i_cmd;
679 msg_Dbg( p_stream, "new command: %d old:%d", p_sys->i_cmd,
684 /*****************************************************************************
685 * VideoCommand: Create/Delete a video encoder
686 *****************************************************************************/
687 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
689 sout_stream_sys_t *p_sys = p_stream->p_sys;
691 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
693 mtime_t i_dts = id->p_queued->i_dts;
694 block_t *p_block = id->p_queued->p_next;
696 while ( p_block != NULL )
698 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
700 i_dts = p_block->i_dts;
701 p_block = p_block->p_next;
707 p_sys->i_old_cmd = p_sys->i_cmd;
711 avcodec_close( id->ff_enc_c );
712 av_free( id->ff_enc_c );
713 av_free( id->p_frame );
714 free( id->p_buffer_out );
718 if ( p_sys->i_cmd > 0 )
720 /* Create a new encoder. */
721 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
722 int i_aspect_num, i_aspect_den;
724 if( i_ff_codec == 0 )
726 msg_Err( p_stream, "cannot find encoder" );
730 id->ff_enc = avcodec_find_encoder( i_ff_codec );
733 msg_Err( p_stream, "cannot find encoder (avcodec)" );
737 id->ff_enc_c = avcodec_alloc_context();
739 /* Set CPU capabilities */
740 unsigned i_cpu = vlc_CPU();
741 id->ff_enc_c->dsp_mask = 0;
742 if( !(i_cpu & CPU_CAPABILITY_MMX) )
744 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
746 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
748 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
750 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
752 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
754 if( !(i_cpu & CPU_CAPABILITY_SSE) )
756 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
757 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
760 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
761 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
762 av_reduce( &i_aspect_num, &i_aspect_den,
764 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
765 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
766 &id->ff_enc_c->sample_aspect_ratio.den,
767 i_aspect_num * (int64_t)id->ff_enc_c->height,
768 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
770 #if LIBAVCODEC_BUILD >= 4754
771 id->ff_enc_c->time_base.num = 1;
772 id->ff_enc_c->time_base.den = 25; /* FIXME */
774 id->ff_enc_c->frame_rate = 25; /* FIXME */
775 id->ff_enc_c->frame_rate_base = 1;
778 id->ff_enc_c->gop_size = 200;
779 id->ff_enc_c->max_b_frames = 0;
781 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
782 | CODEC_FLAG_INPUT_PRESERVED
783 | CODEC_FLAG_LOW_DELAY;
785 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
786 id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
788 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
790 msg_Err( p_stream, "cannot open encoder" );
794 id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
795 id->p_frame = avcodec_alloc_frame();
796 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
797 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
798 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
799 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
800 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
801 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
803 id->i_nb_pred = p_sys->i_gop;
809 /*****************************************************************************
810 * VideoGetBuffer: Build an alternate video buffer
811 *****************************************************************************/
812 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
815 sout_stream_sys_t *p_sys = p_stream->p_sys;
819 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
821 id->p_frame->interlaced_frame = 0;
822 id->p_frame->top_field_first = 1;
823 id->p_frame->pts = p_buffer->i_dts;
825 if ( id->i_nb_pred >= p_sys->i_gop )
827 id->p_frame->pict_type = FF_I_TYPE;
829 id->p_frame->me_threshold = 0;
830 id->p_frame->mb_threshold = 0;
836 id->p_frame->pict_type = FF_P_TYPE;
838 if ( id->p_frame->mb_type != NULL )
840 id->p_frame->me_threshold = MAX_THRESHOLD;
841 id->p_frame->mb_threshold = MAX_THRESHOLD;
847 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
848 id->ff_enc_c->width * id->ff_enc_c->height * 3,
855 if ( id->p_frame->mb_type == NULL
856 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
858 int mb_width = (id->ff_enc_c->width + 15) / 16;
859 int mb_height = (id->ff_enc_c->height + 15) / 16;
860 int h_chroma_shift, v_chroma_shift;
863 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
866 id->p_frame->motion_subsample_log2
867 = id->ff_enc_c->coded_frame->motion_subsample_log2;
868 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
869 * sizeof(uint32_t) );
870 vlc_memcpy( id->p_frame->mb_type, id->ff_enc_c->coded_frame->mb_type,
871 (mb_width + 1) * mb_height * sizeof(id->p_frame->mb_type[0]));
873 for ( i = 0; i < 2; i++ )
875 int stride = ((16 * mb_width )
876 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
877 int height = ((16 * mb_height)
878 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
879 int b8_stride = mb_width * 2 + 1;
881 if ( id->ff_enc_c->coded_frame->motion_val[i] )
883 id->p_frame->motion_val[i] = malloc( 2 * stride * height
885 vlc_memcpy( id->p_frame->motion_val[i],
886 id->ff_enc_c->coded_frame->motion_val[i],
887 2 * stride * height * sizeof(int16_t) );
889 if ( id->ff_enc_c->coded_frame->ref_index[i] )
891 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
893 vlc_memcpy( id->p_frame->ref_index[i],
894 id->ff_enc_c->coded_frame->ref_index[i],
895 b8_stride * 2 * mb_height * sizeof(int8_t));
901 p_out = block_New( p_stream, i_out );
902 vlc_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
903 p_out->i_length = p_buffer->i_length;
904 p_out->i_pts = p_buffer->i_dts;
905 p_out->i_dts = p_buffer->i_dts;
906 p_out->i_rate = p_buffer->i_rate;
908 switch ( id->ff_enc_c->coded_frame->pict_type )
911 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
914 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
917 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
923 block_Release( p_buffer );
928 /*****************************************************************************
929 * AudioGetBuffer: Build an alternate audio buffer
930 *****************************************************************************/
931 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
937 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
938 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
944 p_out = block_New( p_stream, i_out );
945 vlc_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
946 p_out->i_length = p_buffer->i_length;
947 p_out->i_pts = p_buffer->i_dts;
948 p_out->i_dts = p_buffer->i_dts;
949 p_out->i_rate = p_buffer->i_rate;
951 block_Release( p_buffer );