1 /*****************************************************************************
2 * switcher.c: MPEG2 video switcher module
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
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 12
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." )
104 set_description( _("MPEG2 video switcher stream output") );
105 set_capability( "sout stream", 50 );
106 add_shortcut( "switcher" );
107 set_callbacks( Open, Close );
109 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
110 FILES_LONGTEXT, VLC_FALSE );
111 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
112 SIZES_LONGTEXT, VLC_FALSE );
113 add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
114 RATIO_LONGTEXT, VLC_FALSE );
115 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
116 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
117 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
118 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
119 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
120 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
121 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
122 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
125 static const char *ppsz_sout_options[] = {
126 "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale", NULL
129 struct sout_stream_sys_t
131 sout_stream_t *p_out;
135 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
138 picture_t p_pictures[MAX_PICTURES];
143 int i_cmd, i_old_cmd;
146 struct sout_stream_id_t
149 vlc_bool_t b_switcher_video;
150 vlc_bool_t b_switcher_audio;
156 AVCodecContext *ff_enc_c;
163 /*****************************************************************************
165 *****************************************************************************/
166 static int Open( vlc_object_t *p_this )
168 sout_stream_t *p_stream = (sout_stream_t*)p_this;
169 sout_stream_sys_t *p_sys;
171 char *psz_files, *psz_sizes;
172 int i_height = 0, i_width = 0;
174 p_sys = malloc( sizeof(sout_stream_sys_t) );
175 memset( p_sys, 0, sizeof(sout_stream_sys_t) );
177 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
180 msg_Err( p_stream, "cannot create chain" );
185 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
188 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
189 psz_files = val.psz_string;
190 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
191 psz_sizes = val.psz_string;
193 p_sys->i_nb_pictures = 0;
194 while ( psz_files && *psz_files )
196 char * psz_file = psz_files;
197 char * psz_size = psz_sizes;
199 while ( *psz_files && *psz_files != ':' )
201 if ( *psz_files == ':' )
206 while ( *psz_sizes && *psz_sizes != ':' )
208 if ( *psz_sizes == ':' )
210 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
212 msg_Err( p_stream, "bad size %s for file %s", psz_size,
219 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
220 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
225 p_sys->i_nb_pictures++;
228 var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
229 if ( val.psz_string )
231 char *psz_parser = strchr( val.psz_string, ':' );
235 *psz_parser++ = '\0';
236 p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
237 / atoi( psz_parser );
241 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
242 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
245 free( val.psz_string );
249 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
252 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
253 p_sys->i_fd = net_OpenUDP( p_stream, NULL, val.i_int, NULL, 0 );
254 if ( p_sys->i_fd < 0 )
260 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
261 p_sys->i_cmd = val.i_int;
262 p_sys->i_old_cmd = 0;
264 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
265 p_sys->i_gop = val.i_int;
267 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
268 p_sys->i_qscale = val.i_int;
270 p_stream->pf_add = Add;
271 p_stream->pf_del = Del;
272 p_stream->pf_send = Send;
273 p_stream->p_sys = p_sys;
276 avcodec_register_all();
281 /*****************************************************************************
283 *****************************************************************************/
284 static void Close( vlc_object_t * p_this )
286 sout_stream_t *p_stream = (sout_stream_t *)p_this;
287 sout_stream_sys_t *p_sys = p_stream->p_sys;
289 sout_StreamDelete( p_sys->p_out );
294 /*****************************************************************************
295 * Add: Add an input elementary stream
296 *****************************************************************************/
297 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
299 sout_stream_sys_t *p_sys = p_stream->p_sys;
300 sout_stream_id_t *id;
302 id = malloc( sizeof( sout_stream_id_t ) );
303 memset( id, 0, sizeof( sout_stream_id_t ) );
306 if ( p_fmt->i_cat == VIDEO_ES
307 && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
308 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
310 id->b_switcher_video = VLC_TRUE;
311 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
313 "creating video switcher for fcc=`%4.4s'",
314 (char*)&p_fmt->i_codec );
316 else if ( p_fmt->i_cat == AUDIO_ES
317 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a') )
319 int i_ff_codec = CODEC_ID_MP2;
322 id->b_switcher_audio = VLC_TRUE;
324 "creating audio switcher for fcc=`%4.4s'",
325 (char*)&p_fmt->i_codec );
327 /* Allocate the encoder right now. */
328 if( i_ff_codec == 0 )
330 msg_Err( p_stream, "cannot find encoder" );
334 id->ff_enc = avcodec_find_encoder( i_ff_codec );
337 msg_Err( p_stream, "cannot find encoder (avcodec)" );
341 id->ff_enc_c = avcodec_alloc_context();
343 /* Set CPU capabilities */
344 id->ff_enc_c->dsp_mask = 0;
345 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
347 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
349 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
351 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
353 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
355 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
357 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
359 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
360 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
363 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
364 id->ff_enc_c->channels = p_fmt->audio.i_channels;
365 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
367 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
369 msg_Err( p_stream, "cannot open encoder" );
373 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
374 id->p_samples = malloc( id->ff_enc_c->frame_size
375 * p_fmt->audio.i_channels * sizeof(int16_t) );
376 memset( id->p_samples, 0,
377 id->ff_enc_c->frame_size * p_fmt->audio.i_channels
380 for ( i = 0; i < MAX_AUDIO; i++ )
382 if ( p_sys->pp_audio_ids[i] == NULL )
384 p_sys->pp_audio_ids[i] = id;
388 if ( i == MAX_AUDIO )
390 msg_Err( p_stream, "too many audio streams !" );
397 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
398 (char*)&p_fmt->i_codec );
402 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
404 /* open output stream */
405 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
407 if ( id->id == NULL )
416 /*****************************************************************************
417 * Del: Del an elementary stream
418 *****************************************************************************/
419 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
421 sout_stream_sys_t *p_sys = p_stream->p_sys;
423 if ( id->b_switcher_audio )
426 for ( i = 0; i < MAX_AUDIO; i++ )
428 if ( p_sys->pp_audio_ids[i] == id )
430 p_sys->pp_audio_ids[i] = NULL;
438 avcodec_close( id->ff_enc_c );
439 av_free( id->ff_enc_c );
440 av_free( id->p_frame );
441 free( id->p_buffer_out );
446 p_sys->p_out->pf_del( p_sys->p_out, id->id );
453 /*****************************************************************************
454 * Send: Process an input packet
455 *****************************************************************************/
456 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
459 sout_stream_sys_t *p_sys = p_stream->p_sys;
463 block_Release( p_buffer );
467 if ( !id->b_switcher_video && !id->b_switcher_audio )
469 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
472 block_ChainAppend( &id->p_queued, p_buffer );
474 if ( id->b_switcher_video )
476 /* Check for commands for every video frame. */
477 NetCommand( p_stream );
479 while ( id->p_queued != NULL )
484 if ( p_sys->i_old_cmd != p_sys->i_cmd )
486 i_dts = VideoCommand( p_stream, id );
489 i_dts = Process( p_stream, id, i_dts );
491 for ( i = 0; i < MAX_AUDIO; i++ )
493 if ( p_sys->pp_audio_ids[i] != NULL )
494 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
502 /*****************************************************************************
503 * Process: Process and dispatch buffers
504 *****************************************************************************/
505 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
508 sout_stream_sys_t *p_sys = p_stream->p_sys;
510 block_t *p_blocks = NULL;
511 block_t *p_blocks_out = NULL;
513 /* Find out the blocks we need. */
514 while ( id->p_queued != NULL
515 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
517 block_t *p_next = id->p_queued->p_next;
518 id->p_queued->p_next = NULL;
519 i_dts = id->p_queued->i_dts;
520 block_ChainAppend( &p_blocks, id->p_queued );
521 id->p_queued = p_next;
524 if ( p_sys->i_old_cmd == 0 )
527 if ( p_blocks != NULL )
528 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
532 if ( p_sys->i_old_cmd == -1 )
534 /* No output at all */
535 while ( p_blocks != NULL )
537 block_t * p_next = p_blocks->p_next;
538 block_Release( p_blocks );
544 while ( p_blocks != NULL )
546 block_t * p_next = p_blocks->p_next;
549 if ( id->b_switcher_video )
551 p_out = VideoGetBuffer( p_stream, id, p_blocks );
555 p_out = AudioGetBuffer( p_stream, id, p_blocks );
560 block_ChainAppend( &p_blocks_out, p_out );
564 if ( p_blocks_out != NULL )
565 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
569 /*****************************************************************************
570 * UnpackFromFile: Read a YUV picture and store it in our format
571 *****************************************************************************/
572 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
573 int i_width, int i_height,
577 FILE *p_file = fopen( psz_file, "r" );
579 if ( p_file == NULL )
581 msg_Err( p_stream, "file %s not found", psz_file );
585 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
587 i_width * VOUT_ASPECT_FACTOR / i_height );
588 for ( i = 0; i < p_pic->i_planes; i++ )
590 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
591 p_pic->p[i].i_pitch );
592 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
593 p_pic->p[i].i_pitch );
596 for ( i = 0; i < i_height; i++ )
599 uint8_t p_buffer[i_width * 2];
600 uint8_t *p_char = p_buffer;
601 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
602 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
603 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
605 if ( fread( p_buffer, 2, i_width, p_file ) != i_width )
607 msg_Err( p_stream, "premature end of file %s", psz_file );
609 for ( i = 0; i < p_pic->i_planes; i++ )
611 free( p_pic->p[i].p_pixels );
617 for ( j = 0; j < i_width; j++ )
619 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
620 i_chroma = !i_chroma;
622 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
624 **pp_chroma = *p_char;
635 /*****************************************************************************
636 * NetCommand: Get a command from the network
637 *****************************************************************************/
638 static void NetCommand( sout_stream_t *p_stream )
640 sout_stream_sys_t *p_sys = p_stream->p_sys;
642 int i_len = net_ReadNonBlock( p_stream, p_sys->i_fd, NULL, psz_buffer,
643 sizeof( psz_buffer ), 0 );
647 int i_cmd = strtol( psz_buffer, NULL, 0 );
648 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
650 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
654 p_sys->i_cmd = i_cmd;
658 /*****************************************************************************
659 * VideoCommand: Create/Delete a video encoder
660 *****************************************************************************/
661 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
663 sout_stream_sys_t *p_sys = p_stream->p_sys;
665 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
667 mtime_t i_dts = id->p_queued->i_dts;
668 block_t *p_block = id->p_queued->p_next;
670 while ( p_block != NULL )
672 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
674 i_dts = p_block->i_dts;
675 p_block = p_block->p_next;
681 p_sys->i_old_cmd = p_sys->i_cmd;
685 avcodec_close( id->ff_enc_c );
686 av_free( id->ff_enc_c );
687 av_free( id->p_frame );
688 free( id->p_buffer_out );
692 if ( p_sys->i_cmd > 0 )
694 /* Create a new encoder. */
695 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
696 int i_aspect_num, i_aspect_den;
698 if( i_ff_codec == 0 )
700 msg_Err( p_stream, "cannot find encoder" );
704 id->ff_enc = avcodec_find_encoder( i_ff_codec );
707 msg_Err( p_stream, "cannot find encoder (avcodec)" );
711 id->ff_enc_c = avcodec_alloc_context();
713 /* Set CPU capabilities */
714 id->ff_enc_c->dsp_mask = 0;
715 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
717 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
719 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
721 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
723 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
725 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
727 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
729 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
730 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
733 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
734 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
735 av_reduce( &i_aspect_num, &i_aspect_den,
737 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
738 av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
739 &id->ff_enc_c->sample_aspect_ratio.den,
740 i_aspect_num * (int64_t)id->ff_enc_c->height,
741 i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
743 id->ff_enc_c->frame_rate = 25; /* FIXME */
744 id->ff_enc_c->frame_rate_base = 1;
746 id->ff_enc_c->gop_size = 200;
747 id->ff_enc_c->max_b_frames = 0;
749 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
750 | CODEC_FLAG_INPUT_PRESERVED
751 | CODEC_FLAG_LOW_DELAY;
753 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
755 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
757 msg_Err( p_stream, "cannot open encoder" );
761 id->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
762 id->p_frame = avcodec_alloc_frame();
763 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
764 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
765 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
766 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
767 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
768 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
770 id->i_nb_pred = p_sys->i_gop;
776 /*****************************************************************************
777 * VideoGetBuffer: Build an alternate video buffer
778 *****************************************************************************/
779 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
782 sout_stream_sys_t *p_sys = p_stream->p_sys;
786 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
788 id->p_frame->interlaced_frame = 0;
789 id->p_frame->top_field_first = 1;
790 id->p_frame->pts = p_buffer->i_dts;
792 if ( id->i_nb_pred >= p_sys->i_gop )
794 id->p_frame->pict_type = FF_I_TYPE;
796 id->p_frame->me_threshold = 0;
797 id->p_frame->mb_threshold = 0;
803 id->p_frame->pict_type = FF_P_TYPE;
805 if ( id->p_frame->mb_type != NULL )
807 id->p_frame->me_threshold = MAX_THRESHOLD;
808 id->p_frame->mb_threshold = MAX_THRESHOLD;
814 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
815 AVCODEC_MAX_VIDEO_FRAME_SIZE,
822 if ( id->p_frame->mb_type == NULL
823 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
825 int mb_width = (id->ff_enc_c->width + 15) / 16;
826 int mb_height = (id->ff_enc_c->height + 15) / 16;
827 int h_chroma_shift, v_chroma_shift;
830 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
833 id->p_frame->motion_subsample_log2
834 = id->ff_enc_c->coded_frame->motion_subsample_log2;
835 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
836 * sizeof(uint32_t) );
837 p_stream->p_vlc->pf_memcpy( id->p_frame->mb_type,
838 id->ff_enc_c->coded_frame->mb_type,
839 (mb_width + 1) * mb_height
840 * sizeof(id->p_frame->mb_type[0]));
842 for ( i = 0; i < 2; i++ )
844 int stride = ((16 * mb_width )
845 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
846 int height = ((16 * mb_height)
847 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
848 int b8_stride = mb_width * 2 + 1;
850 if ( id->ff_enc_c->coded_frame->motion_val[i] )
852 id->p_frame->motion_val[i] = malloc( 2 * stride * height
854 p_stream->p_vlc->pf_memcpy( id->p_frame->motion_val[i],
855 id->ff_enc_c->coded_frame->motion_val[i],
856 2 * stride * height * sizeof(int16_t) );
858 if ( id->ff_enc_c->coded_frame->ref_index[i] )
860 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
862 p_stream->p_vlc->pf_memcpy( id->p_frame->ref_index[i],
863 id->ff_enc_c->coded_frame->ref_index[i],
864 b8_stride * 2 * mb_height * sizeof(int8_t));
870 p_out = block_New( p_stream, i_out );
871 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
872 p_out->i_length = p_buffer->i_length;
873 p_out->i_pts = p_buffer->i_dts;
874 p_out->i_dts = p_buffer->i_dts;
876 switch ( id->ff_enc_c->coded_frame->pict_type )
879 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
882 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
885 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
891 block_Release( p_buffer );
896 /*****************************************************************************
897 * AudioGetBuffer: Build an alternate audio buffer
898 *****************************************************************************/
899 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
905 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
906 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
912 p_out = block_New( p_stream, i_out );
913 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
914 p_out->i_length = p_buffer->i_length;
915 p_out->i_pts = p_buffer->i_dts;
916 p_out->i_dts = p_buffer->i_dts;
918 block_Release( p_buffer );