1 /*****************************************************************************
2 * ffmpeg.c: video decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ffmpeg.c,v 1.12 2002/06/01 18:04:48 sam Exp $
7 * Authors: Laurent Aimar <fenrir@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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
35 #include <unistd.h> /* getpid() */
41 #ifdef HAVE_SYS_TIMES_H
42 # include <sys/times.h>
45 #include "vdec_ext-plugins.h"
46 #include "avcodec.h" /* ffmpeg */
52 static int decoder_Probe ( u8 * );
53 static int decoder_Run ( decoder_fifo_t * );
54 static int InitThread ( videodec_thread_t * );
55 static void EndThread ( videodec_thread_t * );
56 static void DecodeThread ( videodec_thread_t * );
59 static int b_ffmpeginit = 0;
61 /*****************************************************************************
63 *****************************************************************************/
64 void _M( vdec_getfunctions )( function_list_t * p_function_list )
66 p_function_list->functions.dec.pf_probe = decoder_Probe;
67 p_function_list->functions.dec.pf_run = decoder_Run;
70 /*****************************************************************************
71 * Build configuration tree.
72 *****************************************************************************/
78 SET_DESCRIPTION( "ffmpeg video decoder (MSMPEG4v123,MPEG4)" )
79 ADD_CAPABILITY( DECODER, 70 )
83 _M( vdec_getfunctions )( &p_module->p_functions->dec );
86 MODULE_DEACTIVATE_START
87 MODULE_DEACTIVATE_STOP
90 static inline u16 __GetWordLittleEndianFromBuff( byte_t *p_buff )
93 i = (*p_buff) + ( *(p_buff + 1) <<8 );
97 static inline u32 __GetDoubleWordLittleEndianFromBuff( byte_t *p_buff )
100 i = (*p_buff) + ( *(p_buff + 1) <<8 ) +
101 ( *(p_buff + 2) <<16 ) + ( *(p_buff + 3) <<24 );
105 /*****************************************************************************
106 * decoder_Probe: probe the decoder and return score
107 *****************************************************************************
108 * Tries to launch a decoder and return score so that the interface is able
110 *****************************************************************************/
111 static int decoder_Probe( u8 *pi_type )
115 #if LIBAVCODEC_BUILD >= 4608
116 case( MSMPEG4v1_VIDEO_ES):
117 case( MSMPEG4v2_VIDEO_ES):
119 case( MSMPEG4v3_VIDEO_ES):
120 case( MPEG4_VIDEO_ES ):
127 /*****************************************************************************
129 *****************************************************************************/
131 static void __ParseBitMapInfoHeader( bitmapinfoheader_t *h, byte_t *p_data )
133 h->i_size = __GetDoubleWordLittleEndianFromBuff( p_data );
134 h->i_width = __GetDoubleWordLittleEndianFromBuff( p_data + 4 );
135 h->i_height = __GetDoubleWordLittleEndianFromBuff( p_data + 8 );
136 h->i_planes = __GetWordLittleEndianFromBuff( p_data + 12 );
137 h->i_bitcount = __GetWordLittleEndianFromBuff( p_data + 14 );
138 h->i_compression = __GetDoubleWordLittleEndianFromBuff( p_data + 16 );
139 h->i_sizeimage = __GetDoubleWordLittleEndianFromBuff( p_data + 20 );
140 h->i_xpelspermeter = __GetDoubleWordLittleEndianFromBuff( p_data + 24 );
141 h->i_ypelspermeter = __GetDoubleWordLittleEndianFromBuff( p_data + 28 );
142 h->i_clrused = __GetDoubleWordLittleEndianFromBuff( p_data + 32 );
143 h->i_clrimportant = __GetDoubleWordLittleEndianFromBuff( p_data + 36 );
145 /* get the first pes from fifo */
146 static pes_packet_t *__PES_GET( decoder_fifo_t *p_fifo )
150 vlc_mutex_lock( &p_fifo->data_lock );
152 /* if fifo is emty wait */
153 while( !p_fifo->p_first )
157 vlc_mutex_unlock( &p_fifo->data_lock );
160 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
162 p_pes = p_fifo->p_first;
164 vlc_mutex_unlock( &p_fifo->data_lock );
168 /* free the first pes and go to next */
169 static void __PES_NEXT( decoder_fifo_t *p_fifo )
171 pes_packet_t *p_next;
173 vlc_mutex_lock( &p_fifo->data_lock );
175 p_next = p_fifo->p_first->p_next;
176 p_fifo->p_first->p_next = NULL;
177 input_DeletePES( p_fifo->p_packets_mgt, p_fifo->p_first );
178 p_fifo->p_first = p_next;
181 if( !p_fifo->p_first )
183 /* No PES in the fifo */
184 /* pp_last no longer valid */
185 p_fifo->pp_last = &p_fifo->p_first;
186 while( !p_fifo->p_first )
188 vlc_cond_signal( &p_fifo->data_wait );
189 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
192 vlc_mutex_unlock( &p_fifo->data_lock );
195 static inline void __GetFrame( videodec_thread_t *p_vdec )
198 data_packet_t *p_data;
201 p_pes = __PES_GET( p_vdec->p_fifo );
202 p_vdec->i_pts = p_pes->i_pts;
204 while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
206 __PES_NEXT( p_vdec->p_fifo );
207 p_pes = __PES_GET( p_vdec->p_fifo );
209 p_vdec->i_framesize = p_pes->i_pes_size;
210 if( p_pes->i_nb_data == 1 )
212 p_vdec->p_framedata = p_pes->p_first->p_payload_start;
215 /* get a buffer and gather all data packet */
216 p_vdec->p_framedata = p_buffer = malloc( p_pes->i_pes_size );
217 p_data = p_pes->p_first;
220 p_vdec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start,
221 p_data->p_payload_end - p_data->p_payload_start );
222 p_buffer += p_data->p_payload_end - p_data->p_payload_start;
223 p_data = p_data->p_next;
227 static inline void __NextFrame( videodec_thread_t *p_vdec )
231 p_pes = __PES_GET( p_vdec->p_fifo );
232 if( p_pes->i_nb_data != 1 )
234 free( p_vdec->p_framedata ); /* FIXME keep this buffer */
236 __PES_NEXT( p_vdec->p_fifo );
241 /*****************************************************************************
242 * decoder_Run: this function is called just after the thread is created
243 *****************************************************************************/
244 static int decoder_Run ( decoder_fifo_t * p_fifo )
246 videodec_thread_t *p_vdec;
249 if ( (p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t)))
252 msg_Err( p_fifo, "out of memory" );
253 DecoderError( p_fifo );
256 memset( p_vdec, 0, sizeof( videodec_thread_t ) );
258 p_vdec->p_fifo = p_fifo;
260 if( InitThread( p_vdec ) != 0 )
262 DecoderError( p_fifo );
266 while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
268 /* decode a picture */
269 DecodeThread( p_vdec );
272 if( ( b_error = p_vdec->p_fifo->b_error ) )
274 DecoderError( p_vdec->p_fifo );
287 /*****************************************************************************
288 * InitThread: initialize vdec output thread
289 *****************************************************************************
290 * This function is called from decoder_Run and performs the second step
291 * of the initialization. It returns 0 on success. Note that the thread's
292 * flag are not modified inside this function.
293 *****************************************************************************/
294 static int InitThread( videodec_thread_t *p_vdec )
297 if( p_vdec->p_fifo->p_demux_data != NULL )
299 __ParseBitMapInfoHeader( &p_vdec->format,
300 (byte_t*)p_vdec->p_fifo->p_demux_data );
304 msg_Err( p_vdec->p_fifo, "cannot get information" );
312 avcodec_register_all();
314 msg_Dbg( p_vdec->p_fifo, "library ffmpeg initialized" );
318 msg_Dbg( p_vdec->p_fifo, "library ffmpeg already initialized" );
321 switch( p_vdec->p_fifo->i_type )
323 #if LIBAVCODEC_BUILD >= 4608 /* what is the true version */
324 case( MSMPEG4v1_VIDEO_ES):
325 p_vdec->p_codec = avcodec_find_decoder( CODEC_ID_MSMPEG4V1 );
326 p_vdec->psz_namecodec = "MS MPEG-4 v1";
328 case( MSMPEG4v2_VIDEO_ES):
329 p_vdec->p_codec = avcodec_find_decoder( CODEC_ID_MSMPEG4V2 );
330 p_vdec->psz_namecodec = "MS MPEG-4 v2";
332 case( MSMPEG4v3_VIDEO_ES):
333 p_vdec->p_codec = avcodec_find_decoder( CODEC_ID_MSMPEG4V3 );
334 p_vdec->psz_namecodec = "MS MPEG-4 v3";
337 /* fallback on this */
338 case( MSMPEG4v3_VIDEO_ES):
339 p_vdec->p_codec = avcodec_find_decoder( CODEC_ID_MSMPEG4 );
340 p_vdec->psz_namecodec = "MS MPEG-4";
343 case( MPEG4_VIDEO_ES):
344 p_vdec->p_codec = avcodec_find_decoder( CODEC_ID_MPEG4 );
345 p_vdec->psz_namecodec = "MPEG-4";
348 p_vdec->p_codec = NULL;
349 p_vdec->psz_namecodec = "Unknown";
352 if( !p_vdec->p_codec )
354 msg_Err( p_vdec->p_fifo, "codec not found (%s)",
355 p_vdec->psz_namecodec );
359 p_vdec->p_context = &p_vdec->context;
360 memset( p_vdec->p_context, 0, sizeof( AVCodecContext ) );
362 p_vdec->p_context->width = p_vdec->format.i_width;
363 p_vdec->p_context->height = p_vdec->format.i_height;
364 p_vdec->p_context->pix_fmt = PIX_FMT_YUV420P; /* I420 */
366 if (avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0)
368 msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
369 p_vdec->psz_namecodec );
374 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
375 p_vdec->psz_namecodec );
379 p_vdec->p_vout = vout_CreateThread( p_vdec->p_fifo,
380 p_vdec->format.i_width,
381 p_vdec->format.i_height,
383 VOUT_ASPECT_FACTOR * p_vdec->format.i_width /
384 p_vdec->format.i_height );
386 if( p_vdec->p_vout == NULL )
388 msg_Err( p_vdec->p_fifo, "cannot open vout, aborting" );
389 avcodec_close( p_vdec->p_context );
390 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
391 p_vdec->psz_namecodec );
395 vlc_object_yield( p_vdec->p_vout );
400 /*****************************************************************************
401 * EndThread: thread destruction
402 *****************************************************************************
403 * This function is called when the thread ends after a sucessful
405 *****************************************************************************/
406 static void EndThread( videodec_thread_t *p_vdec )
410 msg_Err( p_vdec->p_fifo, "cannot free structures" );
414 if( p_vdec->p_context != NULL)
416 avcodec_close( p_vdec->p_context );
417 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
418 p_vdec->psz_namecodec );
421 vlc_object_release( p_vdec->p_vout );
422 vout_DestroyThread( p_vdec->p_vout );
427 static void DecodeThread( videodec_thread_t *p_vdec )
432 AVPicture avpicture; /* ffmpeg picture */
433 picture_t *p_pic; /* videolan picture */
434 /* we have to get a frame stored in a pes
435 give it to ffmpeg decoder
436 and send the image to the output */
438 __GetFrame( p_vdec );
440 i_status = avcodec_decode_video( p_vdec->p_context,
444 p_vdec->i_framesize);
445 __NextFrame( p_vdec );
449 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
450 p_vdec->i_framesize );
458 /* Send decoded frame to vout */
460 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
462 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
466 msleep( VOUT_OUTMEM_SLEEP );
469 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
473 byte_t *p_dest = p_pic->p[i_plane].p_pixels;
474 byte_t *p_src = avpicture.data[i_plane];
475 if( ( !p_dest )||( !p_src ))
479 i_size = __MIN( p_pic->p[i_plane].i_pitch,
480 avpicture.linesize[i_plane] );
481 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
483 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dest, p_src, i_size );
484 p_dest += p_pic->p[i_plane].i_pitch;
485 p_src += avpicture.linesize[i_plane];
489 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
490 vout_DisplayPicture( p_vdec->p_vout, p_pic );