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 PORT_TEXT N_("Command UDP port")
88 #define PORT_LONGTEXT N_( \
89 "UDP port to listen to for commands." )
90 #define COMMAND_TEXT N_("Command")
91 #define COMMAND_LONGTEXT N_( \
92 "Initial command to execute." )
93 #define GOP_TEXT N_("GOP size")
94 #define GOP_LONGTEXT N_( \
95 "Number of P frames between two I frames." )
96 #define QSCALE_TEXT N_("Quantizer scale")
97 #define QSCALE_LONGTEXT N_( \
101 set_description( _("MPEG2 video switcher stream output") );
102 set_capability( "sout stream", 50 );
103 add_shortcut( "switcher" );
104 set_callbacks( Open, Close );
106 add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
107 FILES_LONGTEXT, VLC_FALSE );
108 add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
109 SIZES_LONGTEXT, VLC_FALSE );
110 add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
111 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
112 add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
113 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
114 add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
115 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
116 add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
117 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
120 static const char *ppsz_sout_options[] = {
121 "files", "sizes", "port", "command", "gop", "qscale", NULL
124 struct sout_stream_sys_t
126 sout_stream_t *p_out;
129 sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
132 picture_t p_pictures[MAX_PICTURES];
137 int i_cmd, i_old_cmd;
140 struct sout_stream_id_t
143 vlc_bool_t b_switcher_video;
144 vlc_bool_t b_switcher_audio;
150 AVCodecContext *ff_enc_c;
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 sout_stream_t *p_stream = (sout_stream_t*)p_this;
163 sout_stream_sys_t *p_sys;
165 char *psz_files, *psz_sizes;
166 int i_height = 0, i_width = 0;
168 p_sys = malloc( sizeof(sout_stream_sys_t) );
169 memset( p_sys, 0, sizeof(sout_stream_sys_t) );
171 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
174 msg_Err( p_stream, "cannot create chain" );
179 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
182 var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
183 psz_files = val.psz_string;
184 var_Get( p_stream, SOUT_CFG_PREFIX "sizes", &val );
185 psz_sizes = val.psz_string;
187 p_sys->i_nb_pictures = 0;
188 while ( psz_files && *psz_files )
190 char * psz_file = psz_files;
191 char * psz_size = psz_sizes;
193 while ( *psz_files && *psz_files != ':' )
195 if ( *psz_files == ':' )
200 while ( *psz_sizes && *psz_sizes != ':' )
202 if ( *psz_sizes == ':' )
204 if ( sscanf( psz_size, "%dx%d", &i_width, &i_height ) != 2 )
206 msg_Err( p_stream, "bad size %s for file %s", psz_size,
213 if ( UnpackFromFile( p_stream, psz_file, i_width, i_height,
214 &p_sys->p_pictures[p_sys->i_nb_pictures] ) < 0 )
219 p_sys->i_nb_pictures++;
222 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
223 p_sys->i_fd = net_OpenUDP( p_stream, NULL, val.i_int, NULL, 0 );
224 if ( p_sys->i_fd < 0 )
230 var_Get( p_stream, SOUT_CFG_PREFIX "command", &val );
231 p_sys->i_cmd = val.i_int;
232 p_sys->i_old_cmd = 0;
234 var_Get( p_stream, SOUT_CFG_PREFIX "gop", &val );
235 p_sys->i_gop = val.i_int;
237 var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
238 p_sys->i_qscale = val.i_int;
240 p_stream->pf_add = Add;
241 p_stream->pf_del = Del;
242 p_stream->pf_send = Send;
243 p_stream->p_sys = p_sys;
246 avcodec_register_all();
251 /*****************************************************************************
253 *****************************************************************************/
254 static void Close( vlc_object_t * p_this )
256 sout_stream_t *p_stream = (sout_stream_t *)p_this;
257 sout_stream_sys_t *p_sys = p_stream->p_sys;
259 sout_StreamDelete( p_sys->p_out );
264 /*****************************************************************************
265 * Add: Add an input elementary stream
266 *****************************************************************************/
267 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
269 sout_stream_sys_t *p_sys = p_stream->p_sys;
270 sout_stream_id_t *id;
272 id = malloc( sizeof( sout_stream_id_t ) );
273 memset( id, 0, sizeof( sout_stream_id_t ) );
276 if ( p_fmt->i_cat == VIDEO_ES
277 && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
278 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
280 id->b_switcher_video = VLC_TRUE;
281 p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
283 "creating video switcher for fcc=`%4.4s'",
284 (char*)&p_fmt->i_codec );
286 else if ( p_fmt->i_cat == AUDIO_ES
287 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a') )
289 int i_ff_codec = CODEC_ID_MP2;
292 id->b_switcher_audio = VLC_TRUE;
294 "creating audio switcher for fcc=`%4.4s'",
295 (char*)&p_fmt->i_codec );
297 /* Allocate the encoder right now. */
298 if( i_ff_codec == 0 )
300 msg_Err( p_stream, "cannot find encoder" );
304 id->ff_enc = avcodec_find_encoder( i_ff_codec );
307 msg_Err( p_stream, "cannot find encoder (avcodec)" );
311 id->ff_enc_c = avcodec_alloc_context();
313 /* Set CPU capabilities */
314 id->ff_enc_c->dsp_mask = 0;
315 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
317 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
319 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
321 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
323 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
325 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
327 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
329 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
330 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
333 id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
334 id->ff_enc_c->channels = p_fmt->audio.i_channels;
335 id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
337 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
339 msg_Err( p_stream, "cannot open encoder" );
343 id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
344 id->p_samples = malloc( id->ff_enc_c->frame_size
345 * p_fmt->audio.i_channels * sizeof(int16_t) );
346 memset( id->p_samples, 0,
347 id->ff_enc_c->frame_size * p_fmt->audio.i_channels
350 for ( i = 0; i < MAX_AUDIO; i++ )
352 if ( p_sys->pp_audio_ids[i] == NULL )
354 p_sys->pp_audio_ids[i] = id;
358 if ( i == MAX_AUDIO )
360 msg_Err( p_stream, "too many audio streams !" );
367 msg_Dbg( p_stream, "do not know what to do when switching (fcc=`%4.4s')",
368 (char*)&p_fmt->i_codec );
372 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
374 /* open output stream */
375 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
377 if ( id->id == NULL )
386 /*****************************************************************************
387 * Del: Del an elementary stream
388 *****************************************************************************/
389 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
391 sout_stream_sys_t *p_sys = p_stream->p_sys;
393 if ( id->b_switcher_audio )
396 for ( i = 0; i < MAX_AUDIO; i++ )
398 if ( p_sys->pp_audio_ids[i] == id )
400 p_sys->pp_audio_ids[i] = NULL;
408 avcodec_close( id->ff_enc_c );
409 av_free( id->ff_enc_c );
410 av_free( id->p_frame );
411 free( id->p_buffer_out );
416 p_sys->p_out->pf_del( p_sys->p_out, id->id );
423 /*****************************************************************************
424 * Send: Process an input packet
425 *****************************************************************************/
426 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
429 sout_stream_sys_t *p_sys = p_stream->p_sys;
433 block_Release( p_buffer );
437 if ( !id->b_switcher_video && !id->b_switcher_audio )
439 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
442 block_ChainAppend( &id->p_queued, p_buffer );
444 if ( id->b_switcher_video )
446 /* Check for commands for every video frame. */
447 NetCommand( p_stream );
449 while ( id->p_queued != NULL )
454 if ( p_sys->i_old_cmd != p_sys->i_cmd )
456 i_dts = VideoCommand( p_stream, id );
459 i_dts = Process( p_stream, id, i_dts );
461 for ( i = 0; i < MAX_AUDIO; i++ )
463 if ( p_sys->pp_audio_ids[i] != NULL )
464 Process( p_stream, p_sys->pp_audio_ids[i], i_dts );
472 /*****************************************************************************
473 * Process: Process and dispatch buffers
474 *****************************************************************************/
475 static mtime_t Process( sout_stream_t *p_stream, sout_stream_id_t *id,
478 sout_stream_sys_t *p_sys = p_stream->p_sys;
480 block_t *p_blocks = NULL;
481 block_t *p_blocks_out = NULL;
483 /* Find out the blocks we need. */
484 while ( id->p_queued != NULL
485 && (!i_max_dts || id->p_queued->i_dts <= i_max_dts) )
487 block_t *p_next = id->p_queued->p_next;
488 id->p_queued->p_next = NULL;
489 i_dts = id->p_queued->i_dts;
490 block_ChainAppend( &p_blocks, id->p_queued );
491 id->p_queued = p_next;
494 if ( p_sys->i_old_cmd == 0 )
497 if ( p_blocks != NULL )
498 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks );
502 if ( p_sys->i_old_cmd == -1 )
504 /* No output at all */
505 while ( p_blocks != NULL )
507 block_t * p_next = p_blocks->p_next;
508 block_Release( p_blocks );
514 while ( p_blocks != NULL )
516 block_t * p_next = p_blocks->p_next;
519 if ( id->b_switcher_video )
521 p_out = VideoGetBuffer( p_stream, id, p_blocks );
525 p_out = AudioGetBuffer( p_stream, id, p_blocks );
530 block_ChainAppend( &p_blocks_out, p_out );
534 if ( p_blocks_out != NULL )
535 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_blocks_out );
539 /*****************************************************************************
540 * UnpackFromFile: Read a YUV picture and store it in our format
541 *****************************************************************************/
542 static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
543 int i_width, int i_height,
547 FILE *p_file = fopen( psz_file, "r" );
549 if ( p_file == NULL )
551 msg_Err( p_stream, "file %s not found", psz_file );
555 vout_InitPicture( VLC_OBJECT(p_stream), p_pic, VLC_FOURCC('I','4','2','0'),
557 i_width * VOUT_ASPECT_FACTOR / i_height );
558 for ( i = 0; i < p_pic->i_planes; i++ )
560 p_pic->p[i].p_pixels = malloc( p_pic->p[i].i_lines *
561 p_pic->p[i].i_pitch );
562 memset( p_pic->p[i].p_pixels, 0, p_pic->p[i].i_lines *
563 p_pic->p[i].i_pitch );
566 for ( i = 0; i < i_height; i++ )
569 uint8_t p_buffer[i_width * 2];
570 uint8_t *p_char = p_buffer;
571 uint8_t *p_y = &p_pic->p[0].p_pixels[i * p_pic->p[0].i_pitch];
572 uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
573 uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
575 if ( fread( p_buffer, 2, i_width, p_file ) != i_width )
577 msg_Err( p_stream, "premature end of file %s", psz_file );
579 for ( i = 0; i < p_pic->i_planes; i++ )
581 free( p_pic->p[i].p_pixels );
587 for ( j = 0; j < i_width; j++ )
589 uint8_t **pp_chroma = i_chroma ? &p_v : &p_u;
590 i_chroma = !i_chroma;
592 **pp_chroma = (**pp_chroma + *p_char + 1) / 2;
594 **pp_chroma = *p_char;
605 /*****************************************************************************
606 * NetCommand: Get a command from the network
607 *****************************************************************************/
608 static void NetCommand( sout_stream_t *p_stream )
610 sout_stream_sys_t *p_sys = p_stream->p_sys;
612 int i_len = net_ReadNonBlock( p_stream, p_sys->i_fd, NULL, psz_buffer,
613 sizeof( psz_buffer ), 0 );
617 int i_cmd = strtol( psz_buffer, NULL, 0 );
618 if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
620 msg_Err( p_stream, "got a wrong command (%d)", i_cmd );
624 p_sys->i_cmd = i_cmd;
628 /*****************************************************************************
629 * VideoCommand: Create/Delete a video encoder
630 *****************************************************************************/
631 static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
633 sout_stream_sys_t *p_sys = p_stream->p_sys;
635 if ( p_sys->i_cmd == 0 && !(id->p_queued->i_flags & BLOCK_FLAG_TYPE_I) )
637 mtime_t i_dts = id->p_queued->i_dts;
638 block_t *p_block = id->p_queued->p_next;
640 while ( p_block != NULL )
642 if ( p_block->i_flags & BLOCK_FLAG_TYPE_I )
644 i_dts = p_block->i_dts;
645 p_block = p_block->p_next;
651 p_sys->i_old_cmd = p_sys->i_cmd;
655 avcodec_close( id->ff_enc_c );
656 av_free( id->ff_enc_c );
657 av_free( id->p_frame );
658 free( id->p_buffer_out );
662 if ( p_sys->i_cmd > 0 )
664 /* Create a new encoder. */
665 int i_ff_codec = CODEC_ID_MPEG2VIDEO;
666 if( i_ff_codec == 0 )
668 msg_Err( p_stream, "cannot find encoder" );
672 id->ff_enc = avcodec_find_encoder( i_ff_codec );
675 msg_Err( p_stream, "cannot find encoder (avcodec)" );
679 id->ff_enc_c = avcodec_alloc_context();
681 /* Set CPU capabilities */
682 id->ff_enc_c->dsp_mask = 0;
683 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
685 id->ff_enc_c->dsp_mask |= FF_MM_MMX;
687 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
689 id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
691 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
693 id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
695 if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
697 id->ff_enc_c->dsp_mask |= FF_MM_SSE;
698 id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
701 id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
702 id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
704 id->ff_enc_c->frame_rate = 25; /* FIXME */
705 id->ff_enc_c->frame_rate_base = 1;
707 id->ff_enc_c->gop_size = 200;
708 id->ff_enc_c->max_b_frames = 0;
710 id->ff_enc_c->flags |= CODEC_FLAG_QSCALE
711 | CODEC_FLAG_INPUT_PRESERVED
712 | CODEC_FLAG_LOW_DELAY;
714 id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
716 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
718 msg_Err( p_stream, "cannot open encoder" );
722 id->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
723 id->p_frame = avcodec_alloc_frame();
724 id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
725 id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
726 id->p_frame->linesize[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].i_pitch;
727 id->p_frame->data[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].p_pixels;
728 id->p_frame->data[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].p_pixels;
729 id->p_frame->data[2] = p_sys->p_pictures[p_sys->i_cmd-1].p[2].p_pixels;
731 id->i_nb_pred = p_sys->i_gop;
737 /*****************************************************************************
738 * VideoGetBuffer: Build an alternate video buffer
739 *****************************************************************************/
740 static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
743 sout_stream_sys_t *p_sys = p_stream->p_sys;
747 id->p_frame->quality = p_sys->i_qscale * powf(2.0, FF_LAMBDA_SHIFT + 7.0)
749 id->p_frame->interlaced_frame = 0;
750 id->p_frame->top_field_first = 1;
751 id->p_frame->pts = p_buffer->i_dts;
753 if ( id->i_nb_pred >= p_sys->i_gop )
755 id->p_frame->pict_type = FF_I_TYPE;
757 id->p_frame->me_threshold = 0;
758 id->p_frame->mb_threshold = 0;
764 id->p_frame->pict_type = FF_P_TYPE;
766 if ( id->p_frame->mb_type != NULL )
768 id->p_frame->me_threshold = MAX_THRESHOLD;
769 id->p_frame->mb_threshold = MAX_THRESHOLD;
775 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
776 AVCODEC_MAX_VIDEO_FRAME_SIZE,
783 if ( id->p_frame->mb_type == NULL
784 && id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE )
786 int mb_width = (id->ff_enc_c->width + 15) / 16;
787 int mb_height = (id->ff_enc_c->height + 15) / 16;
788 int h_chroma_shift, v_chroma_shift;
791 avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
794 id->p_frame->motion_subsample_log2
795 = id->ff_enc_c->coded_frame->motion_subsample_log2;
796 id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
797 * sizeof(uint32_t) );
798 p_stream->p_vlc->pf_memcpy( id->p_frame->mb_type,
799 id->ff_enc_c->coded_frame->mb_type,
800 (mb_width + 1) * mb_height
801 * sizeof(id->p_frame->mb_type[0]));
803 for ( i = 0; i < 2; i++ )
805 int stride = ((16 * mb_width )
806 >> id->ff_enc_c->coded_frame->motion_subsample_log2) + 1;
807 int height = ((16 * mb_height)
808 >> id->ff_enc_c->coded_frame->motion_subsample_log2);
809 int b8_stride = mb_width * 2 + 1;
811 if ( id->ff_enc_c->coded_frame->motion_val[i] )
813 id->p_frame->motion_val[i] = malloc( 2 * stride * height
815 p_stream->p_vlc->pf_memcpy( id->p_frame->motion_val[i],
816 id->ff_enc_c->coded_frame->motion_val[i],
817 2 * stride * height * sizeof(int16_t) );
819 if ( id->ff_enc_c->coded_frame->ref_index[i] )
821 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
823 p_stream->p_vlc->pf_memcpy( id->p_frame->ref_index[i],
824 id->ff_enc_c->coded_frame->ref_index[i],
825 b8_stride * 2 * mb_height * sizeof(int8_t));
831 p_out = block_New( p_stream, i_out );
832 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
833 p_out->i_length = p_buffer->i_length;
834 p_out->i_pts = p_buffer->i_dts;
835 p_out->i_dts = p_buffer->i_dts;
837 switch ( id->ff_enc_c->coded_frame->pict_type )
840 p_out->i_flags |= BLOCK_FLAG_TYPE_I;
843 p_out->i_flags |= BLOCK_FLAG_TYPE_P;
846 p_out->i_flags |= BLOCK_FLAG_TYPE_B;
852 block_Release( p_buffer );
857 /*****************************************************************************
858 * AudioGetBuffer: Build an alternate audio buffer
859 *****************************************************************************/
860 static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
866 i_out = avcodec_encode_audio( id->ff_enc_c, id->p_buffer_out,
867 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
873 p_out = block_New( p_stream, i_out );
874 p_stream->p_vlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
875 p_out->i_length = p_buffer->i_length;
876 p_out->i_pts = p_buffer->i_dts;
877 p_out->i_dts = p_buffer->i_dts;
879 block_Release( p_buffer );