1 /*****************************************************************************
2 * ffmpeg.c: video decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ffmpeg.c,v 1.21 2002/07/31 20:56:51 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 OpenDecoder ( vlc_object_t * );
53 static int RunDecoder ( 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 *****************************************************************************/
65 #define ERROR_RESILIENCE_LONGTEXT \
66 "ffmpeg can make errors resiliences. \n"\
67 "Nevertheless, with buggy encoder (like ISO MPEG-4 encoder from M$) " \
68 "this will produce a lot of errors.\n" \
69 "Valid range is -1 to 99 (-1 disable all errors resiliences)."
71 #define HURRY_UP_LONGTEXT \
72 "Allow the decoder to partially decode or skip frame(s) " \
73 "when there not enough time.\n It's usefull with low CPU power " \
74 "but it could produce broken pictures."
77 add_category_hint( N_("Miscellaneous"), NULL );
78 #if LIBAVCODEC_BUILD >= 4611
79 add_integer ( "ffmpeg-error-resilience", 0, NULL,
80 "error resilience", ERROR_RESILIENCE_LONGTEXT );
81 add_integer ( "ffmpeg-workaround-bugs", 0, NULL,
82 "workaround bugs", "0-99, seems to be for msmpeg v3\n" );
84 add_bool( "ffmpeg-hurry-up", 0, NULL, "hurry up", HURRY_UP_LONGTEXT );
85 set_description( _("ffmpeg video decoder((MS)MPEG4,SVQ1,H263)") );
86 set_capability( "decoder", 70 );
87 set_callbacks( OpenDecoder, NULL );
90 /*****************************************************************************
91 * OpenDecoder: probe the decoder and return score
92 *****************************************************************************
93 * Tries to launch a decoder and return score so that the interface is able
95 *****************************************************************************/
96 static int OpenDecoder( vlc_object_t *p_this )
98 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
100 if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL ) )
102 p_fifo->pf_run = RunDecoder;
109 /*****************************************************************************
110 * RunDecoder: this function is called just after the thread is created
111 *****************************************************************************/
112 static int RunDecoder( decoder_fifo_t *p_fifo )
114 videodec_thread_t *p_vdec;
117 if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
119 msg_Err( p_fifo, "out of memory" );
120 DecoderError( p_fifo );
123 memset( p_vdec, 0, sizeof( videodec_thread_t ) );
125 p_vdec->p_fifo = p_fifo;
127 if( InitThread( p_vdec ) != 0 )
129 DecoderError( p_fifo );
133 while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
135 DecodeThread( p_vdec );
138 if( ( b_error = p_vdec->p_fifo->b_error ) )
140 DecoderError( p_vdec->p_fifo );
154 /*****************************************************************************
156 *****************************************************************************/
158 #define GetWLE( p ) \
159 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
161 #define GetDWLE( p ) \
162 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
163 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
165 static void ffmpeg_ParseBitMapInfoHeader( bitmapinfoheader_t *p_bh,
168 p_bh->i_size = GetDWLE( p_data );
169 p_bh->i_width = GetDWLE( p_data + 4 );
170 p_bh->i_height = GetDWLE( p_data + 8 );
171 p_bh->i_planes = GetWLE( p_data + 12 );
172 p_bh->i_bitcount = GetWLE( p_data + 14 );
173 p_bh->i_compression = GetDWLE( p_data + 16 );
174 p_bh->i_sizeimage = GetDWLE( p_data + 20 );
175 p_bh->i_xpelspermeter = GetDWLE( p_data + 24 );
176 p_bh->i_ypelspermeter = GetDWLE( p_data + 28 );
177 p_bh->i_clrused = GetDWLE( p_data + 32 );
178 p_bh->i_clrimportant = GetDWLE( p_data + 36 );
180 if( p_bh->i_size > 40 )
182 p_bh->i_data = p_bh->i_size - 40;
183 p_bh->p_data = malloc( p_bh->i_data );
184 memcpy( p_bh->p_data, p_data + 40, p_bh->i_data );
193 /* get the first pes from fifo */
194 static pes_packet_t *__PES_GET( decoder_fifo_t *p_fifo )
198 vlc_mutex_lock( &p_fifo->data_lock );
200 /* if fifo is emty wait */
201 while( !p_fifo->p_first )
205 vlc_mutex_unlock( &p_fifo->data_lock );
208 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
210 p_pes = p_fifo->p_first;
212 vlc_mutex_unlock( &p_fifo->data_lock );
217 /* free the first pes and go to next */
218 static void __PES_NEXT( decoder_fifo_t *p_fifo )
220 pes_packet_t *p_next;
222 vlc_mutex_lock( &p_fifo->data_lock );
224 p_next = p_fifo->p_first->p_next;
225 p_fifo->p_first->p_next = NULL;
226 input_DeletePES( p_fifo->p_packets_mgt, p_fifo->p_first );
227 p_fifo->p_first = p_next;
230 if( !p_fifo->p_first )
232 /* No PES in the fifo */
233 /* pp_last no longer valid */
234 p_fifo->pp_last = &p_fifo->p_first;
235 while( !p_fifo->p_first )
237 vlc_cond_signal( &p_fifo->data_wait );
238 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
241 vlc_mutex_unlock( &p_fifo->data_lock );
244 static inline void __GetFrame( videodec_thread_t *p_vdec )
247 data_packet_t *p_data;
250 p_pes = __PES_GET( p_vdec->p_fifo );
251 p_vdec->i_pts = p_pes->i_pts;
253 while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
255 __PES_NEXT( p_vdec->p_fifo );
256 p_pes = __PES_GET( p_vdec->p_fifo );
258 p_vdec->i_framesize = p_pes->i_pes_size;
259 if( p_pes->i_nb_data == 1 )
261 p_vdec->p_framedata = p_pes->p_first->p_payload_start;
264 /* get a buffer and gather all data packet */
265 p_vdec->p_framedata = p_buffer = malloc( p_pes->i_pes_size );
266 p_data = p_pes->p_first;
269 p_vdec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start,
270 p_data->p_payload_end - p_data->p_payload_start );
271 p_buffer += p_data->p_payload_end - p_data->p_payload_start;
272 p_data = p_data->p_next;
276 static inline void __NextFrame( videodec_thread_t *p_vdec )
280 p_pes = __PES_GET( p_vdec->p_fifo );
281 if( p_pes->i_nb_data != 1 )
283 free( p_vdec->p_framedata ); /* FIXME keep this buffer */
285 __PES_NEXT( p_vdec->p_fifo );
288 /* FIXME FIXME some of them are wrong */
289 static int i_ffmpeg_PixFmtToChroma[] =
291 /* PIX_FMT_ANY = -1,PIX_FMT_YUV420P, PIX_FMT_YUV422,
292 PIX_FMT_RGB24, PIX_FMT_BGR24, PIX_FMT_YUV422P,
293 PIX_FMT_YUV444P, PIX_FMT_YUV410P */
295 0, VLC_FOURCC('I','4','2','0'), VLC_FOURCC('I','4','2','0'),
296 VLC_FOURCC('R','V','2','4'), 0, VLC_FOURCC('Y','4','2','2'),
297 VLC_FOURCC('I','4','4','4'), 0
300 static inline u32 ffmpeg_PixFmtToChroma( int i_ffmpegchroma )
302 if( ++i_ffmpegchroma > 7 )
308 return( i_ffmpeg_PixFmtToChroma[i_ffmpegchroma] );
312 static inline int ffmpeg_FfAspect( int i_width, int i_height, int i_ffaspect )
316 case( FF_ASPECT_4_3_625 ):
317 case( FF_ASPECT_4_3_525 ):
318 return( VOUT_ASPECT_FACTOR * 4 / 3);
319 case( FF_ASPECT_16_9_625 ):
320 case( FF_ASPECT_16_9_525 ):
321 return( VOUT_ASPECT_FACTOR * 16 / 9 );
322 case( FF_ASPECT_SQUARE ):
324 return( VOUT_ASPECT_FACTOR * i_width / i_height );
328 static int ffmpeg_CheckVout( vout_thread_t *p_vout,
340 /* we will try to make conversion */
341 i_chroma = VLC_FOURCC('I','4','2','0');
344 if( ( p_vout->render.i_width != i_width )||
345 ( p_vout->render.i_height != i_height )||
346 ( p_vout->render.i_chroma != i_chroma )||
347 ( p_vout->render.i_aspect !=
348 ffmpeg_FfAspect( i_width, i_height, i_aspect ) ) )
360 static vout_thread_t *ffmpeg_CreateVout( videodec_thread_t *p_vdec,
366 vout_thread_t *p_vout;
368 if( (!i_width)||(!i_height) )
370 return( NULL ); /* Can't create a new vout without display size */
375 /* we make conversion if possible*/
376 i_chroma = VLC_FOURCC('I','4','2','0');
377 msg_Warn( p_vdec->p_fifo, "Internal chroma conversion (FIXME)");
378 /* It's mainly for I410 -> I420 conversion that I've made,
379 it's buggy and very slow */
382 i_aspect = ffmpeg_FfAspect( i_width, i_height, i_aspect );
384 /* Spawn a video output if there is none. First we look for our children,
385 * then we look for any other vout that might be available. */
386 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
390 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
396 if( !ffmpeg_CheckVout( p_vout,
397 i_width, i_height, i_aspect,i_chroma ) )
399 /* We are not interested in this format, close this vout */
400 vlc_object_detach_all( p_vout );
401 vlc_object_release( p_vout );
402 vout_DestroyThread( p_vout );
407 /* This video output is cool! Hijack it. */
408 vlc_object_detach_all( p_vout );
409 vlc_object_attach( p_vout, p_vdec->p_fifo );
410 vlc_object_release( p_vout );
416 msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
418 p_vout = vout_CreateThread( p_vdec->p_fifo,
428 /* FIXME FIXME FIXME this is a big shit
429 does someone want to rewrite this function ?
430 or said to me how write a better thing
434 static void ffmpeg_ConvertPictureI410toI420( picture_t *p_pic,
435 AVPicture *p_avpicture,
436 videodec_thread_t *p_vdec )
442 int i_stride, i_lines;
443 int i_height, i_width;
446 i_height = p_vdec->p_context->height;
447 i_width = p_vdec->p_context->width;
449 p_dst = p_pic->p[0].p_pixels;
450 p_src = p_avpicture->data[0];
452 /* copy first plane */
453 for( i_y = 0; i_y < i_height; i_y++ )
455 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width);
456 p_dst += p_pic->p[0].i_pitch;
457 p_src += p_avpicture->linesize[0];
460 /* process each plane in a temporary buffer */
461 for( i_plane = 1; i_plane < 3; i_plane++ )
463 i_stride = p_avpicture->linesize[i_plane];
464 i_lines = i_height / 4;
466 p_dst = p_plane[i_plane] = malloc( i_lines * i_stride * 2 * 2 );
467 p_src = p_avpicture->data[i_plane];
469 /* for each source line */
470 for( i_y = 0; i_y < i_lines; i_y++ )
472 for( i_x = 0; i_x < i_stride - 1; i_x++ )
474 p_dst[2 * i_x ] = p_src[i_x];
475 p_dst[2 * i_x + 1] = ( p_src[i_x] + p_src[i_x + 1]) / 2;
478 p_dst[2 * i_stride - 2] = p_src[i_x];
479 p_dst[2 * i_stride - 1] = p_src[i_x];
481 p_dst += 4 * i_stride; /* process the next even lines */
488 for( i_plane = 1; i_plane < 3; i_plane++ )
490 i_stride = p_avpicture->linesize[i_plane];
491 i_lines = i_height / 4;
493 p_dst = p_plane[i_plane] + 2*i_stride;
494 p_src = p_plane[i_plane];
496 for( i_y = 0; i_y < i_lines - 1; i_y++ )
498 for( i_x = 0; i_x < 2 * i_stride ; i_x++ )
500 p_dst[i_x] = ( p_src[i_x] + p_src[i_x + 4*i_stride])/2;
503 p_dst += 4 * i_stride; /* process the next odd lines */
504 p_src += 4 * i_stride;
507 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, 2*i_stride );
509 /* copy to p_pic, by block
510 if I do pixel per pixel it segfault. It's why I use
511 temporaries buffers */
512 for( i_plane = 1; i_plane < 3; i_plane++ )
516 p_src = p_plane[i_plane];
517 p_dst = p_pic->p[i_plane].p_pixels;
519 i_size = __MIN( 2*i_stride, p_pic->p[i_plane].i_pitch);
520 for( i_y = 0; i_y < __MIN(p_pic->p[i_plane].i_lines, 2 * i_lines); i_y++ )
522 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
523 p_src += 2 * i_stride;
524 p_dst += p_pic->p[i_plane].i_pitch;
526 free( p_plane[i_plane] );
531 static void ffmpeg_ConvertPicture( picture_t *p_pic,
532 AVPicture *p_avpicture,
533 videodec_thread_t *p_vdec )
544 if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
546 /* convert ffmpeg picture to our format */
547 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
549 p_src = p_avpicture->data[i_plane];
550 p_dst = p_pic->p[i_plane].p_pixels;
551 i_src_stride = p_avpicture->linesize[i_plane];
552 i_dst_stride = p_pic->p[i_plane].i_pitch;
554 i_size = __MIN( i_src_stride, i_dst_stride );
555 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
557 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
558 p_src += i_src_stride;
559 p_dst += i_dst_stride;
565 /* we need to convert to I420 */
566 switch( p_vdec->p_context->pix_fmt )
568 #if LIBAVCODEC_BUILD >= 4615
569 case( PIX_FMT_YUV410P ):
570 ffmpeg_ConvertPictureI410toI420( p_pic, p_avpicture, p_vdec );
574 p_vdec->p_fifo->b_error =1;
580 /*****************************************************************************
582 * Functions that initialize, decode and end the decoding process
584 *****************************************************************************/
586 /*****************************************************************************
587 * InitThread: initialize vdec output thread
588 *****************************************************************************
589 * This function is called from RunDecoderoder and performs the second step
590 * of the initialization. It returns 0 on success. Note that the thread's
591 * flag are not modified inside this function.
592 *****************************************************************************/
594 static int InitThread( videodec_thread_t *p_vdec )
599 if( p_vdec->p_fifo->p_demux_data )
601 ffmpeg_ParseBitMapInfoHeader( &p_vdec->format,
602 (u8*)p_vdec->p_fifo->p_demux_data );
606 msg_Warn( p_vdec->p_fifo, "display informations missing" );
613 avcodec_register_all();
615 msg_Dbg( p_vdec->p_fifo, "library ffmpeg initialized" );
619 msg_Dbg( p_vdec->p_fifo, "library ffmpeg already initialized" );
621 ffmpeg_GetFfmpegCodec( p_vdec->p_fifo->i_fourcc,
623 &p_vdec->psz_namecodec );
625 avcodec_find_decoder( i_ffmpeg_codec );
627 if( !p_vdec->p_codec )
629 msg_Err( p_vdec->p_fifo, "codec not found (%s)",
630 p_vdec->psz_namecodec );
634 p_vdec->p_context = &p_vdec->context;
635 memset( p_vdec->p_context, 0, sizeof( AVCodecContext ) );
637 p_vdec->p_context->width = p_vdec->format.i_width;
638 p_vdec->p_context->height = p_vdec->format.i_height;
641 p_vdec->p_context->workaround_bugs
642 --> seems to be for msmpeg 3 but can't know what is supposed to do
644 p_vdec->p_context->strict_std_compliance
645 --> strictly follow mpeg4 standard for decoder or encoder ??
647 p_vdec->p_context->error_resilience
648 --> don't make error resilience, because of some ms encoder witch
649 use some wrong VLC code.
652 #if LIBAVCODEC_BUILD >= 4611
653 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
654 p_vdec->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
656 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
657 p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
659 #if LIBAVCODEC_BUILD >= 4614
660 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
662 p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
666 if (avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0)
668 msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
669 p_vdec->psz_namecodec );
674 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
675 p_vdec->psz_namecodec );
678 /* first give init data */
679 if( p_vdec->format.i_data )
684 switch( i_ffmpeg_codec )
686 case( CODEC_ID_MPEG4 ):
687 avcodec_decode_video( p_vdec->p_context, &avpicture,
689 p_vdec->format.p_data,
690 p_vdec->format.i_data );
697 /* This will be created after the first decoded frame */
698 p_vdec->p_vout = NULL;
703 /*****************************************************************************
704 * DecodeThread: Called for decode one frame
705 *****************************************************************************/
706 static void DecodeThread( videodec_thread_t *p_vdec )
711 AVPicture avpicture; /* ffmpeg picture */
712 picture_t *p_pic; /* videolan picture */
713 /* we have to get a frame stored in a pes
714 give it to ffmpeg decoder
715 and send the image to the output */
717 /* TODO implement it in a better way */
719 if( ( config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up") )&&
720 ( p_vdec->i_frame_late > 4 ) )
722 #if LIBAVCODEC_BUILD > 4603
724 if( p_vdec->i_frame_late < 8 )
726 p_vdec->p_context->hurry_up = 2;
730 /* too much late picture, won't decode
731 but break picture until a new I, and for mpeg4 ...*/
732 p_vdec->i_frame_late--; /* needed else it will never be decrease */
733 __PES_NEXT( p_vdec->p_fifo );
737 if( p_vdec->i_frame_late < 8 )
739 b_drawpicture = 0; /* not really good but .. */
743 /* too much late picture, won't decode
744 but break picture until a new I, and for mpeg4 ...*/
745 p_vdec->i_frame_late--; /* needed else it will never be decrease */
746 __PES_NEXT( p_vdec->p_fifo );
754 #if LIBAVCODEC_BUILD > 4603
755 p_vdec->p_context->hurry_up = 0;
759 __GetFrame( p_vdec );
761 i_status = avcodec_decode_video( p_vdec->p_context,
765 p_vdec->i_framesize);
767 __NextFrame( p_vdec );
771 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
772 p_vdec->i_framesize );
773 p_vdec->i_frame_error++;
776 /* Update frame late count*/
777 /* I don't make statistic on decoding time */
778 if( p_vdec->i_pts <= mdate())
780 p_vdec->i_frame_late++;
784 p_vdec->i_frame_late = 0;
787 if( !b_gotpicture || avpicture.linesize[0] == 0 || !b_drawpicture)
793 if( !ffmpeg_CheckVout( p_vdec->p_vout,
794 p_vdec->p_context->width,
795 p_vdec->p_context->height,
796 p_vdec->p_context->aspect_ratio_info,
797 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
800 ffmpeg_CreateVout( p_vdec,
801 p_vdec->p_context->width,
802 p_vdec->p_context->height,
803 p_vdec->p_context->aspect_ratio_info,
804 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt));
805 if( !p_vdec->p_vout )
807 msg_Err( p_vdec->p_fifo, "cannot create vout" );
808 p_vdec->p_fifo->b_error = 1; /* abort */
813 /* Send decoded frame to vout */
814 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
816 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
820 msleep( VOUT_OUTMEM_SLEEP );
823 ffmpeg_ConvertPicture( p_pic,
828 /* FIXME correct avi and use i_dts */
829 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
830 vout_DisplayPicture( p_vdec->p_vout, p_pic );
836 /*****************************************************************************
837 * EndThread: thread destruction
838 *****************************************************************************
839 * This function is called when the thread ends after a sucessful
841 *****************************************************************************/
842 static void EndThread( videodec_thread_t *p_vdec )
849 if( p_vdec->p_context != NULL)
851 avcodec_close( p_vdec->p_context );
852 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
853 p_vdec->psz_namecodec );
856 if( p_vdec->p_vout != NULL )
858 /* We are about to die. Reattach video output to p_vlc. */
859 vlc_object_detach( p_vdec->p_vout, p_vdec->p_fifo );
860 vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
863 if( p_vdec->format.p_data != NULL)
865 free( p_vdec->format.p_data );