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 *****************************************************************************/
39 #ifdef HAVE_FFMPEG_AVCODEC_H
40 # include <ffmpeg/avcodec.h>
45 #ifdef HAVE_POSTPROC_POSTPROCESS_H
46 # include <postproc/postprocess.h>
48 # include <libpostproc/postprocess.h>
51 #define SOUT_CFG_PREFIX "sout-switcher-"
52 #define MAX_PICTURES 10
54 #define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
55 #define MAX_THRESHOLD 99999999
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close ( vlc_object_t * );
63 static sout_stream_id_t *Add( sout_stream_t *, es_format_t * );
64 static int Del( sout_stream_t *, sout_stream_id_t * );
65 static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
67 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
69 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
70 int i_width, int i_height,
72 static void NetCommand( sout_stream_t *p_stream );
73 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id );
74 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
76 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
79 /*****************************************************************************
81 *****************************************************************************/
82 #define FILES_TEXT N_("Files")
83 #define FILES_LONGTEXT N_( \
84 "Full paths of the files separated by colons." )
85 #define SIZES_TEXT N_("Sizes")
86 #define SIZES_LONGTEXT N_( \
87 "List of sizes separated by colons (720x576:480x576)." )
88 #define RATIO_TEXT N_("Aspect ratio")
89 #define RATIO_LONGTEXT N_( \
90 "Aspect ratio (4:3, 16:9)." )
91 #define PORT_TEXT N_("Command UDP port")
92 #define PORT_LONGTEXT N_( \
93 "UDP port to listen to for commands." )
94 #define COMMAND_TEXT N_("Command")
95 #define COMMAND_LONGTEXT N_( \
96 "Initial command to execute." )
97 #define GOP_TEXT N_("GOP size")
98 #define GOP_LONGTEXT N_( \
99 "Number of P frames between two I frames." )
100 #define QSCALE_TEXT N_("Quantizer scale")
101 #define QSCALE_LONGTEXT N_( \
102 "Fixed quantizer scale to use." )
103 #define AUDIO_TEXT N_("Mute audio")
104 #define AUDIO_LONGTEXT N_( \
105 "Mute audio when command is not 0." )
108 set_description( _("MPEG2 video switcher stream output") );
109 set_capability( "sout stream", 50 );
110 add_shortcut( "switcher" );
111 set_callbacks( Open, Close );
113 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
114 FILES_LONGTEXT, VLC_FALSE );
115 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
116 SIZES_LONGTEXT, VLC_FALSE );
117 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
118 RATIO_LONGTEXT, VLC_FALSE );
119 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
120 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
121 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
122 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
123 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
124 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
125 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
126 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
127 add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
128 AUDIO_TEXT, AUDIO_LONGTEXT, VLC_TRUE );
131 static const char *ppsz_sout_options[] = {
132 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
136 struct sout_stream_sys_t
138 sout_stream_t *p_out;
142 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
146 picture_t p_pictures[MAX_PICTURES];
151 int i_cmd, i_old_cmd;
154 struct sout_stream_id_t
157 vlc_bool_t b_switcher_video;
158 vlc_bool_t b_switcher_audio;
164 AVCodecContext *ff_enc_c;
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 sout_stream_t *p_stream = (sout_stream_t*)p_this;
177 sout_stream_sys_t *p_sys;
179 char *psz_files, *psz_sizes;
180 int i_height = 0, i_width = 0;
182 p_sys = malloc( sizeof(sout_stream_sys_t) );
183 memset( p_sys, 0, sizeof(sout_stream_sys_t) );
185 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
188 msg_Err( p_stream, "cannot create chain" );
193 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
196 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
197 psz_files = val.psz_string;
198 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
199 psz_sizes = val.psz_string;
201 p_sys->i_nb_pictures = 0;
202 while ( psz_files && *psz_files )
204 char * psz_file = psz_files;
205 char * psz_size = psz_sizes;
207 while ( *psz_files && *psz_files != ':' )
209 if ( *psz_files == ':' )
214 while ( *psz_sizes && *psz_sizes != ':' )
216 if ( *psz_sizes == ':' )
218 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
220 msg_Err( p_stream, "bad size %s for file %s", psz_size,
227 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
228 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
233 p_sys->i_nb_pictures++;
236 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
237 if ( val.psz_string )
239 char *psz_parser = strchr( val.psz_string, ':' );
243 *psz_parser++ = '\0';
244 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
245 / atoi( psz_parser );
249 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
250 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
253 free( val.psz_string );
257 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
260 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
261 p_sys->i_fd = net_OpenUDP( p_stream, NULL, val.i_int, NULL, 0 );
262 if ( p_sys->i_fd < 0 )
268 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
269 p_sys->i_cmd = val.i_int;
270 p_sys->i_old_cmd = 0;
272 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
273 p_sys->i_gop = val.i_int;
275 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
276 p_sys->i_qscale = val.i_int;
278 var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
279 p_sys->b_audio = val.b_bool;
281 p_stream->pf_add = Add;
282 p_stream->pf_del = Del;
283 p_stream->pf_send = Send;
284 p_stream->p_sys = p_sys;
287 avcodec_register_all();
292 /*****************************************************************************
294 *****************************************************************************/
295 static void Close( vlc_object_t * p_this )
297 sout_stream_t *p_stream = (sout_stream_t *)p_this;
298 sout_stream_sys_t *p_sys = p_stream->p_sys;
300 sout_StreamDelete( p_sys->p_out );
305 /*****************************************************************************
306 * Add: Add an input elementary stream
307 *****************************************************************************/
308 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
310 sout_stream_sys_t *p_sys = p_stream->p_sys;
311 sout_stream_id_t *id;
313 id = malloc( sizeof( sout_stream_id_t ) );
314 memset( id, 0, sizeof( sout_stream_id_t ) );
317 if ( p_fmt->i_cat == VIDEO_ES
318 && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
319 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
321 id->b_switcher_video = VLC_TRUE;
322 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
324 "creating video switcher for fcc=`%4.4s' cmd:%d",
325 (char*)&p_fmt->i_codec, p_sys->i_cmd );
327 else if ( p_fmt->i_cat == AUDIO_ES
328 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a')
331 int i_ff_codec = CODEC_ID_MP2;
334 id->b_switcher_audio = VLC_TRUE;
336 "creating audio switcher for fcc=`%4.4s' cmd:%d",
337 (char*)&p_fmt->i_codec, p_sys->i_cmd );
339 /* Allocate the encoder right now. */
340 if( i_ff_codec == 0 )
342 msg_Err( p_stream, "cannot find encoder" );
346 id->ff_enc = avcodec_find_encoder( i_ff_codec );
349 msg_Err( p_stream, "cannot find encoder (avcodec)" );
353 id->ff_enc_c = avcodec_alloc_context();
355 /* Set CPU capabilities */
356 id->ff_enc_c->dsp_mask = 0;
357 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
359 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
361 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
363 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
365 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
367 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
369 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
371 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
372 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
375 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
376 id->ff_enc_c->channels = p_fmt->audio.i_channels;
377 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
379 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
381 msg_Err( p_stream, "cannot open encoder" );
385 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
386 id->p_samples = malloc( id->ff_enc_c->frame_size
387 * p_fmt->audio.i_channels * sizeof(int16_t) );
388 memset( id->p_samples, 0,
389 id->ff_enc_c->frame_size * p_fmt->audio.i_channels
392 for ( i = 0; i < MAX_AUDIO; i++ )
394 if ( p_sys->pp_audio_ids[i] == NULL )
396 p_sys->pp_audio_ids[i] = id;
400 if ( i == MAX_AUDIO )
402 msg_Err( p_stream, "too many audio streams!" );
409 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
410 (char*)&p_fmt->i_codec );
414 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
416 /* open output stream */
417 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
419 if ( id->id == NULL )
428 /*****************************************************************************
429 * Del: Del an elementary stream
430 *****************************************************************************/
431 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
433 sout_stream_sys_t *p_sys = p_stream->p_sys;
435 if ( id->b_switcher_audio )
438 for ( i = 0; i < MAX_AUDIO; i++ )
440 if ( p_sys->pp_audio_ids[i] == id )
442 p_sys->pp_audio_ids[i] = NULL;
450 avcodec_close( id->ff_enc_c );
451 av_free( id->ff_enc_c );
452 av_free( id->p_frame );
453 free( id->p_buffer_out );
458 p_sys->p_out->pf_del( p_sys->p_out, id->id );
465 /*****************************************************************************
466 * Send: Process an input packet
467 *****************************************************************************/
468 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
471 sout_stream_sys_t *p_sys = p_stream->p_sys;
475 block_Release( p_buffer );
479 if ( !id->b_switcher_video && !id->b_switcher_audio )
481 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
484 block_ChainAppend( &id->p_queued, p_buffer );
486 if ( id->b_switcher_video )
488 /* Check for commands for every video frame. */
489 NetCommand( p_stream );
491 while ( id->p_queued != NULL )
496 if ( p_sys->i_old_cmd != p_sys->i_cmd )
498 i_dts = VideoCommand( p_stream, id );
501 i_dts = Process( p_stream, id, i_dts );
503 for ( i = 0; i < MAX_AUDIO; i++ )
505 if ( p_sys->pp_audio_ids[i] != NULL )
506 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
514 /*****************************************************************************
515 * Process: Process and dispatch buffers
516 *****************************************************************************/
517 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
520 sout_stream_sys_t *p_sys = p_stream->p_sys;
522 block_t *p_blocks = NULL;
523 block_t *p_blocks_out = NULL;
525 /* Find out the blocks we need. */
526 while ( id->p_queued != NULL
527 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
529 block_t *p_next = id->p_queued->p_next;
530 id->p_queued->p_next = NULL;
531 i_dts = id->p_queued->i_dts;
532 block_ChainAppend( &p_blocks, id->p_queued );
533 id->p_queued = p_next;
536 if ( p_sys->i_old_cmd == 0 )
539 if ( p_blocks != NULL )
540 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
544 if ( p_sys->i_old_cmd == -1 )
546 /* No output at all */
547 while ( p_blocks != NULL )
549 block_t * p_next = p_blocks->p_next;
550 block_Release( p_blocks );
556 while ( p_blocks != NULL )
558 block_t * p_next = p_blocks->p_next;
561 if ( id->b_switcher_video )
563 p_out = VideoGetBuffer( p_stream, id, p_blocks );
567 p_out = AudioGetBuffer( p_stream, id, p_blocks );
572 block_ChainAppend( &p_blocks_out, p_out );
576 if ( p_blocks_out != NULL )
577 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
581 /*****************************************************************************
582 * UnpackFromFile: Read a YUV picture and store it in our format
583 *****************************************************************************/
584 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
585 int i_width, int i_height,
589 FILE *p_file = utf8_fopen( psz_file, "r" );
591 if ( p_file == NULL )
593 msg_Err( p_stream, "file %s not found", psz_file );
597 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
599 i_width * VOUT_ASPECT_FACTOR / i_height );
600 for ( i = 0; i < p_pic->i_planes; i++ )
602 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
603 p_pic->p[i].i_pitch );
604 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
605 p_pic->p[i].i_pitch );
608 for ( i = 0; i < i_height; i++ )
611 uint8_t p_buffer[i_width * 2];
612 uint8_t *p_char = p_buffer;
613 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
614 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
615 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
617 if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
619 msg_Err( p_stream, "premature end of file %s", psz_file );
621 for ( i = 0; i < p_pic->i_planes; i++ )
623 free( p_pic->p[i].p_pixels );
629 for ( j = 0; j < i_width; j++ )
631 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
632 i_chroma = !i_chroma;
634 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
636 **pp_chroma = *p_char;
647 /*****************************************************************************
648 * NetCommand: Get a command from the network
649 *****************************************************************************/
650 static void NetCommand( sout_stream_t *p_stream )
652 sout_stream_sys_t *p_sys = p_stream->p_sys;
654 int i_len = net_ReadNonBlock( p_stream, p_sys->i_fd, NULL, psz_buffer,
655 sizeof( psz_buffer ), 0 );
659 int i_cmd = strtol( psz_buffer, NULL, 0 );
660 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
662 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
666 p_sys->i_cmd = i_cmd;
668 msg_Dbg( p_stream, "new command: %d old:%d", p_sys->i_cmd,
673 /*****************************************************************************
674 * VideoCommand: Create/Delete a video encoder
675 *****************************************************************************/
676 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
678 sout_stream_sys_t *p_sys = p_stream->p_sys;
680 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
682 mtime_t i_dts = id->p_queued->i_dts;
683 block_t *p_block = id->p_queued->p_next;
685 while ( p_block != NULL )
687 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
689 i_dts = p_block->i_dts;
690 p_block = p_block->p_next;
696 p_sys->i_old_cmd = p_sys->i_cmd;
700 avcodec_close( id->ff_enc_c );
701 av_free( id->ff_enc_c );
702 av_free( id->p_frame );
703 free( id->p_buffer_out );
707 if ( p_sys->i_cmd > 0 )
709 /* Create a new encoder. */
710 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
711 int i_aspect_num, i_aspect_den;
713 if( i_ff_codec == 0 )
715 msg_Err( p_stream, "cannot find encoder" );
719 id->ff_enc = avcodec_find_encoder( i_ff_codec );
722 msg_Err( p_stream, "cannot find encoder (avcodec)" );
726 id->ff_enc_c = avcodec_alloc_context();
728 /* Set CPU capabilities */
729 id->ff_enc_c->dsp_mask = 0;
730 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
732 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
734 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
736 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
738 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
740 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
742 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
744 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
745 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
748 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
749 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
750 av_reduce( &i_aspect_num, &i_aspect_den,
752 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
753 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
754 &id->ff_enc_c->sample_aspect_ratio.den,
755 i_aspect_num * (int64_t)id->ff_enc_c->height,
756 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
758 #if LIBAVCODEC_BUILD >= 4754
759 id->ff_enc_c->time_base.num = 1;
760 id->ff_enc_c->time_base.den = 25; /* FIXME */
762 id->ff_enc_c->frame_rate = 25; /* FIXME */
763 id->ff_enc_c->frame_rate_base = 1;
766 id->ff_enc_c->gop_size = 200;
767 id->ff_enc_c->max_b_frames = 0;
769 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
770 | CODEC_FLAG_INPUT_PRESERVED
771 | CODEC_FLAG_LOW_DELAY;
773 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
774 id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
776 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
778 msg_Err( p_stream, "cannot open encoder" );
782 id->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
783 id->p_frame = avcodec_alloc_frame();
784 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
785 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
786 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
787 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
788 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
789 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
791 id->i_nb_pred = p_sys->i_gop;
797 /*****************************************************************************
798 * VideoGetBuffer: Build an alternate video buffer
799 *****************************************************************************/
800 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
803 sout_stream_sys_t *p_sys = p_stream->p_sys;
807 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
809 id->p_frame->interlaced_frame = 0;
810 id->p_frame->top_field_first = 1;
811 id->p_frame->pts = p_buffer->i_dts;
813 if ( id->i_nb_pred >= p_sys->i_gop )
815 id->p_frame->pict_type = FF_I_TYPE;
817 id->p_frame->me_threshold = 0;
818 id->p_frame->mb_threshold = 0;
824 id->p_frame->pict_type = FF_P_TYPE;
826 if ( id->p_frame->mb_type != NULL )
828 id->p_frame->me_threshold = MAX_THRESHOLD;
829 id->p_frame->mb_threshold = MAX_THRESHOLD;
835 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
836 AVCODEC_MAX_VIDEO_FRAME_SIZE,
843 if ( id->p_frame->mb_type == NULL
844 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
846 int mb_width = (id->ff_enc_c->width + 15) / 16;
847 int mb_height = (id->ff_enc_c->height + 15) / 16;
848 int h_chroma_shift, v_chroma_shift;
851 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
854 id->p_frame->motion_subsample_log2
855 = id->ff_enc_c->coded_frame->motion_subsample_log2;
856 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
857 * sizeof(uint32_t) );
858 p_stream->p_vlc->pf_memcpy( id->p_frame->mb_type,
859 id->ff_enc_c->coded_frame->mb_type,
860 (mb_width + 1) * mb_height
861 * sizeof(id->p_frame->mb_type[0]));
863 for ( i = 0; i < 2; i++ )
865 int stride = ((16 * mb_width )
866 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
867 int height = ((16 * mb_height)
868 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
869 int b8_stride = mb_width * 2 + 1;
871 if ( id->ff_enc_c->coded_frame->motion_val[i] )
873 id->p_frame->motion_val[i] = malloc( 2 * stride * height
875 p_stream->p_vlc->pf_memcpy( id->p_frame->motion_val[i],
876 id->ff_enc_c->coded_frame->motion_val[i],
877 2 * stride * height * sizeof(int16_t) );
879 if ( id->ff_enc_c->coded_frame->ref_index[i] )
881 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
883 p_stream->p_vlc->pf_memcpy( id->p_frame->ref_index[i],
884 id->ff_enc_c->coded_frame->ref_index[i],
885 b8_stride * 2 * mb_height * sizeof(int8_t));
891 p_out = block_New( p_stream, i_out );
892 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
893 p_out->i_length = p_buffer->i_length;
894 p_out->i_pts = p_buffer->i_dts;
895 p_out->i_dts = p_buffer->i_dts;
896 p_out->i_rate = p_buffer->i_rate;
898 switch ( id->ff_enc_c->coded_frame->pict_type )
901 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
904 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
907 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
913 block_Release( p_buffer );
918 /*****************************************************************************
919 * AudioGetBuffer: Build an alternate audio buffer
920 *****************************************************************************/
921 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
927 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
928 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
934 p_out = block_New( p_stream, i_out );
935 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
936 p_out->i_length = p_buffer->i_length;
937 p_out->i_pts = p_buffer->i_dts;
938 p_out->i_dts = p_buffer->i_dts;
939 p_out->i_rate = p_buffer->i_rate;
941 block_Release( p_buffer );