1 /*****************************************************************************
2 * ffmpeg.c: video decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ffmpeg.c,v 1.7 2002/10/14 21:59:44 fenrir 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 "avcodec.h" /* ffmpeg */
47 #include "postprocessing/postprocessing.h"
54 static int OpenDecoder ( vlc_object_t * );
55 static int RunDecoder ( decoder_fifo_t * );
56 static int InitThread ( videodec_thread_t * );
57 static void EndThread ( videodec_thread_t * );
58 static void DecodeThread ( videodec_thread_t * );
61 static int b_ffmpeginit = 0;
63 /*****************************************************************************
65 *****************************************************************************/
67 #define ERROR_RESILIENCE_LONGTEXT \
68 "ffmpeg can make errors resiliences. \n"\
69 "Nevertheless, with buggy encoder (like ISO MPEG-4 encoder from M$) " \
70 "this will produce a lot of errors.\n" \
71 "Valid range is -1 to 99 (-1 disable all errors resiliences)."
73 #define HURRY_UP_LONGTEXT \
74 "Allow the decoder to partially decode or skip frame(s) " \
75 "when there not enough time.\n It's usefull with low CPU power " \
76 "but it could produce broken pictures."
78 #define POSTPROCESSING_Q_LONGTEXT \
79 "Quality of post processing\n"\
80 "Valid range is 0 to 6\n" \
81 "( Overridden by others setting)"
83 #define POSTPROCESSING_AQ_LONGTEXT \
84 "Post processing quality is selected upon time left" \
85 "but no more than requested quality\n" \
86 "Not yet implemented !"
89 add_category_hint( N_("Ffmpeg"), NULL );
90 #if LIBAVCODEC_BUILD >= 4611
91 add_integer ( "ffmpeg-error-resilience", -1, NULL,
92 "error resilience", ERROR_RESILIENCE_LONGTEXT );
93 add_integer ( "ffmpeg-workaround-bugs", 0, NULL,
94 "workaround bugs", "0-99, seems to be for msmpeg v3\n" );
96 add_bool( "ffmpeg-hurry-up", 0, NULL, "hurry up", HURRY_UP_LONGTEXT );
98 add_category_hint( N_("Post processing"), NULL );
99 add_module( "ffmpeg-pp", "postprocessing",NULL, NULL,
100 N_( "ffmpeg postprocessing module" ), NULL );
101 add_integer( "ffmpeg-pp-q", 0, NULL,
102 "post processing quality", POSTPROCESSING_Q_LONGTEXT );
103 add_bool( "ffmpeg-pp-auto", 0, NULL,
104 "auto-level Post processing quality", POSTPROCESSING_AQ_LONGTEXT );
105 add_bool( "ffmpeg-db-yv", 0, NULL,
106 "force vertical luminance deblocking",
107 "force vertical luminance deblocking (override other settings)" );
108 add_bool( "ffmpeg-db-yh", 0, NULL,
109 "force horizontal luminance deblocking",
110 "force horizontal luminance deblocking (override other settings)" );
111 add_bool( "ffmpeg-db-cv", 0, NULL,
112 "force vertical chrominance deblocking",
113 "force vertical chrominance deblocking (override other settings)" );
114 add_bool( "ffmpeg-db-ch", 0, NULL,
115 "force horizontal chrominance deblocking",
116 "force horizontal chrominance deblocking (override other settings) " );
117 add_bool( "ffmpeg-dr-y", 0, NULL,
118 "force luminance deringing",
119 "force luminance deringing (override other settings)" );
120 add_bool( "ffmpeg-dr-c", 0, NULL,
121 "force chrominance deringing",
122 "force chrominance deringing (override other settings)" );
124 set_description( _("ffmpeg video decoder((MS)MPEG4,SVQ1,H263)") );
125 set_capability( "decoder", 70 );
126 set_callbacks( OpenDecoder, NULL );
129 /*****************************************************************************
130 * OpenDecoder: probe the decoder and return score
131 *****************************************************************************
132 * Tries to launch a decoder and return score so that the interface is able
134 *****************************************************************************/
135 static int OpenDecoder( vlc_object_t *p_this )
137 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
139 if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL ) )
141 p_fifo->pf_run = RunDecoder;
148 /*****************************************************************************
149 * RunDecoder: this function is called just after the thread is created
150 *****************************************************************************/
151 static int RunDecoder( decoder_fifo_t *p_fifo )
153 videodec_thread_t *p_vdec;
156 if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
158 msg_Err( p_fifo, "out of memory" );
159 DecoderError( p_fifo );
162 memset( p_vdec, 0, sizeof( videodec_thread_t ) );
164 p_vdec->p_fifo = p_fifo;
166 if( InitThread( p_vdec ) != 0 )
168 DecoderError( p_fifo );
172 while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
174 DecodeThread( p_vdec );
177 if( ( b_error = p_vdec->p_fifo->b_error ) )
179 DecoderError( p_vdec->p_fifo );
193 /*****************************************************************************
195 *****************************************************************************/
197 #define GetWLE( p ) \
198 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
200 #define GetDWLE( p ) \
201 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
202 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
204 #define FREE( p ) if( p ) free( p ); p = NULL
206 static void ffmpeg_ParseBitMapInfoHeader( bitmapinfoheader_t *p_bh,
209 p_bh->i_size = GetDWLE( p_data );
210 p_bh->i_width = GetDWLE( p_data + 4 );
211 p_bh->i_height = GetDWLE( p_data + 8 );
212 p_bh->i_planes = GetWLE( p_data + 12 );
213 p_bh->i_bitcount = GetWLE( p_data + 14 );
214 p_bh->i_compression = GetDWLE( p_data + 16 );
215 p_bh->i_sizeimage = GetDWLE( p_data + 20 );
216 p_bh->i_xpelspermeter = GetDWLE( p_data + 24 );
217 p_bh->i_ypelspermeter = GetDWLE( p_data + 28 );
218 p_bh->i_clrused = GetDWLE( p_data + 32 );
219 p_bh->i_clrimportant = GetDWLE( p_data + 36 );
221 if( p_bh->i_size > 40 )
223 p_bh->i_data = p_bh->i_size - 40;
224 if( ( p_bh->p_data = malloc( p_bh->i_data ) ) )
226 memcpy( p_bh->p_data, p_data + 40, p_bh->i_data );
240 /* get the first pes from fifo */
241 static pes_packet_t *__PES_GET( decoder_fifo_t *p_fifo )
245 vlc_mutex_lock( &p_fifo->data_lock );
247 /* if fifo is emty wait */
248 while( !p_fifo->p_first )
252 vlc_mutex_unlock( &p_fifo->data_lock );
255 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
257 p_pes = p_fifo->p_first;
259 vlc_mutex_unlock( &p_fifo->data_lock );
264 /* free the first pes and go to next */
265 static void __PES_NEXT( decoder_fifo_t *p_fifo )
267 pes_packet_t *p_next;
269 vlc_mutex_lock( &p_fifo->data_lock );
271 p_next = p_fifo->p_first->p_next;
272 p_fifo->p_first->p_next = NULL;
273 input_DeletePES( p_fifo->p_packets_mgt, p_fifo->p_first );
274 p_fifo->p_first = p_next;
277 if( !p_fifo->p_first )
279 /* No PES in the fifo */
280 /* pp_last no longer valid */
281 p_fifo->pp_last = &p_fifo->p_first;
282 while( !p_fifo->p_first )
284 vlc_cond_signal( &p_fifo->data_wait );
285 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
288 vlc_mutex_unlock( &p_fifo->data_lock );
291 static inline void __GetFrame( videodec_thread_t *p_vdec )
294 data_packet_t *p_data;
297 p_pes = __PES_GET( p_vdec->p_fifo );
298 p_vdec->i_pts = p_pes->i_pts;
300 while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
302 __PES_NEXT( p_vdec->p_fifo );
303 p_pes = __PES_GET( p_vdec->p_fifo );
305 p_vdec->i_framesize = p_pes->i_pes_size;
306 if( p_pes->i_nb_data == 1 )
308 p_vdec->p_framedata = p_pes->p_first->p_payload_start;
311 /* get a buffer and gather all data packet */
312 if( p_vdec->i_buffer_size < p_pes->i_pes_size )
314 if( p_vdec->p_buffer )
316 p_vdec->p_buffer = realloc( p_vdec->p_buffer,
321 p_vdec->p_buffer = malloc( p_pes->i_pes_size );
323 p_vdec->i_buffer_size = p_pes->i_pes_size;
326 p_buffer = p_vdec->p_framedata = p_vdec->p_buffer;
327 p_data = p_pes->p_first;
330 p_vdec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start,
331 p_data->p_payload_end - p_data->p_payload_start );
332 p_buffer += p_data->p_payload_end - p_data->p_payload_start;
333 p_data = p_data->p_next;
337 static inline void __NextFrame( videodec_thread_t *p_vdec )
339 __PES_NEXT( p_vdec->p_fifo );
343 /* Check if we have a Vout with good parameters */
344 static int ffmpeg_CheckVout( vout_thread_t *p_vout,
356 /* we will try to make conversion */
357 i_chroma = VLC_FOURCC('I','4','2','0');
360 if( ( p_vout->render.i_width != i_width )||
361 ( p_vout->render.i_height != i_height )||
362 ( p_vout->render.i_chroma != i_chroma )||
363 ( p_vout->render.i_aspect !=
364 ffmpeg_FfAspect( i_width, i_height, i_aspect ) ) )
376 static vout_thread_t *ffmpeg_CreateVout( videodec_thread_t *p_vdec,
382 vout_thread_t *p_vout;
384 if( (!i_width)||(!i_height) )
386 return( NULL ); /* Can't create a new vout without display size */
391 /* we make conversion if possible*/
392 i_chroma = VLC_FOURCC('I','4','2','0');
393 msg_Warn( p_vdec->p_fifo, "Internal chroma conversion (FIXME)");
394 /* It's mainly for I410 -> I420 conversion that I've made,
395 it's buggy and very slow */
398 i_aspect = ffmpeg_FfAspect( i_width, i_height, i_aspect );
400 /* Spawn a video output if there is none. First we look for our children,
401 * then we look for any other vout that might be available. */
402 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
406 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
412 if( !ffmpeg_CheckVout( p_vout,
413 i_width, i_height, i_aspect,i_chroma ) )
415 /* We are not interested in this format, close this vout */
416 vlc_object_detach( p_vout );
417 vlc_object_release( p_vout );
418 vout_DestroyThread( p_vout );
423 /* This video output is cool! Hijack it. */
424 vlc_object_detach( p_vout );
425 vlc_object_attach( p_vout, p_vdec->p_fifo );
426 vlc_object_release( p_vout );
432 msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
434 p_vout = vout_CreateThread( p_vdec->p_fifo,
436 i_chroma, i_aspect );
442 /* FIXME FIXME FIXME this is a big shit
443 does someone want to rewrite this function ?
444 or said to me how write a better thing
447 static void ffmpeg_ConvertPictureI410toI420( picture_t *p_pic,
448 AVPicture *p_avpicture,
449 videodec_thread_t *p_vdec )
455 int i_stride, i_lines;
456 int i_height, i_width;
459 i_height = p_vdec->p_context->height;
460 i_width = p_vdec->p_context->width;
462 p_dst = p_pic->p[0].p_pixels;
463 p_src = p_avpicture->data[0];
465 /* copy first plane */
466 for( i_y = 0; i_y < i_height; i_y++ )
468 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width);
469 p_dst += p_pic->p[0].i_pitch;
470 p_src += p_avpicture->linesize[0];
473 /* process each plane in a temporary buffer */
474 for( i_plane = 1; i_plane < 3; i_plane++ )
476 i_stride = p_avpicture->linesize[i_plane];
477 i_lines = i_height / 4;
479 p_dst = p_plane[i_plane] = malloc( i_lines * i_stride * 2 * 2 );
480 p_src = p_avpicture->data[i_plane];
482 /* for each source line */
483 for( i_y = 0; i_y < i_lines; i_y++ )
485 for( i_x = 0; i_x < i_stride - 1; i_x++ )
487 p_dst[2 * i_x ] = p_src[i_x];
488 p_dst[2 * i_x + 1] = ( p_src[i_x] + p_src[i_x + 1]) / 2;
491 p_dst[2 * i_stride - 2] = p_src[i_x];
492 p_dst[2 * i_stride - 1] = p_src[i_x];
494 p_dst += 4 * i_stride; /* process the next even lines */
501 for( i_plane = 1; i_plane < 3; i_plane++ )
503 i_stride = p_avpicture->linesize[i_plane];
504 i_lines = i_height / 4;
506 p_dst = p_plane[i_plane] + 2*i_stride;
507 p_src = p_plane[i_plane];
509 for( i_y = 0; i_y < i_lines - 1; i_y++ )
511 for( i_x = 0; i_x < 2 * i_stride ; i_x++ )
513 p_dst[i_x] = ( p_src[i_x] + p_src[i_x + 4*i_stride])/2;
516 p_dst += 4 * i_stride; /* process the next odd lines */
517 p_src += 4 * i_stride;
520 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, 2*i_stride );
522 /* copy to p_pic, by block
523 if I do pixel per pixel it segfault. It's why I use
524 temporaries buffers */
525 for( i_plane = 1; i_plane < 3; i_plane++ )
529 p_src = p_plane[i_plane];
530 p_dst = p_pic->p[i_plane].p_pixels;
532 i_size = __MIN( 2*i_stride, p_pic->p[i_plane].i_pitch);
533 for( i_y = 0; i_y < __MIN(p_pic->p[i_plane].i_lines, 2 * i_lines); i_y++ )
535 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
536 p_src += 2 * i_stride;
537 p_dst += p_pic->p[i_plane].i_pitch;
539 free( p_plane[i_plane] );
544 static void ffmpeg_GetPicture( picture_t *p_pic,
545 AVPicture *p_avpicture,
546 videodec_thread_t *p_vdec )
557 if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
559 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
561 p_src = p_avpicture->data[i_plane];
562 p_dst = p_pic->p[i_plane].p_pixels;
563 i_src_stride = p_avpicture->linesize[i_plane];
564 i_dst_stride = p_pic->p[i_plane].i_pitch;
566 i_size = __MIN( i_src_stride, i_dst_stride );
567 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
569 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
570 p_src += i_src_stride;
571 p_dst += i_dst_stride;
574 if( ( p_vdec->i_pp_mode )&&
575 ( ( p_vdec->p_vout->render.i_chroma ==
576 VLC_FOURCC( 'I','4','2','0' ) )||
577 ( p_vdec->p_vout->render.i_chroma ==
578 VLC_FOURCC( 'Y','V','1','2' ) ) ) )
581 #if LIBAVCODEC_BUILD > 4313
582 p_vdec->p_pp->pf_postprocess( p_pic,
583 p_vdec->p_context->quant_store,
584 p_vdec->p_context->qstride,
591 /* we need to convert to I420 */
592 switch( p_vdec->p_context->pix_fmt )
594 #if LIBAVCODEC_BUILD >= 4615
595 case( PIX_FMT_YUV410P ):
596 ffmpeg_ConvertPictureI410toI420( p_pic, p_avpicture, p_vdec );
600 p_vdec->p_fifo->b_error = 1;
610 /*****************************************************************************
612 * Functions that initialize, decode and end the decoding process
614 *****************************************************************************/
616 /*****************************************************************************
617 * InitThread: initialize vdec output thread
618 *****************************************************************************
619 * This function is called from decoder_Run and performs the second step
620 * of the initialization. It returns 0 on success. Note that the thread's
621 * flag are not modified inside this function.
623 * ffmpeg codec will be open, some memory allocated. But Vout is not yet
624 * open (done after the first decoded frame)
625 *****************************************************************************/
626 static int InitThread( videodec_thread_t *p_vdec )
631 if( p_vdec->p_fifo->p_demux_data )
633 ffmpeg_ParseBitMapInfoHeader( &p_vdec->format,
634 (u8*)p_vdec->p_fifo->p_demux_data );
638 msg_Warn( p_vdec->p_fifo, "display informations missing" );
641 /* **** init ffmpeg library (libavcodec) ***** */
645 avcodec_register_all();
647 msg_Dbg( p_vdec->p_fifo, "library ffmpeg initialized" );
651 msg_Dbg( p_vdec->p_fifo, "library ffmpeg already initialized" );
654 /* ***** Search for codec ***** */
655 ffmpeg_GetFfmpegCodec( p_vdec->p_fifo->i_fourcc,
657 &p_vdec->psz_namecodec );
659 avcodec_find_decoder( i_ffmpeg_codec );
661 if( !p_vdec->p_codec )
663 msg_Err( p_vdec->p_fifo, "codec not found (%s)",
664 p_vdec->psz_namecodec );
668 /* ***** Fill p_context with init values ***** */
669 #if LIBAVCODEC_BUILD >= 4624
670 p_vdec->p_context = avcodec_alloc_context();
672 p_vdec->p_context = malloc( sizeof( AVCodecContext ) );
673 memset( p_vdec->p_context, 0, sizeof( AVCodecContext ) );
676 p_vdec->p_context->width = p_vdec->format.i_width;
677 p_vdec->p_context->height = p_vdec->format.i_height;
679 /* ***** Get configuration of ffmpeg plugin ***** */
680 #if LIBAVCODEC_BUILD >= 4611
681 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
682 p_vdec->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
684 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
685 p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
687 #if LIBAVCODEC_BUILD >= 4614
688 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
690 p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
693 p_vdec->b_hurry_up = config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up");
696 /* ***** Open the codec ***** */
697 if (avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0)
699 msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
700 p_vdec->psz_namecodec );
705 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
706 p_vdec->psz_namecodec );
709 /* ***** init this codec with special data(up to now MPEG4 only) ***** */
710 if( p_vdec->format.i_data )
715 switch( i_ffmpeg_codec )
717 case( CODEC_ID_MPEG4 ):
718 avcodec_decode_video( p_vdec->p_context, &avpicture,
720 p_vdec->format.p_data,
721 p_vdec->format.i_data );
728 /* ***** Load post processing ***** */
730 /* get overridding settings */
731 p_vdec->i_pp_mode = 0;
732 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yv" ) )
733 p_vdec->i_pp_mode |= PP_DEBLOCK_Y_V;
734 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yh" ) )
735 p_vdec->i_pp_mode |= PP_DEBLOCK_Y_H;
736 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-cv" ) )
737 p_vdec->i_pp_mode |= PP_DEBLOCK_C_V;
738 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-ch" ) )
739 p_vdec->i_pp_mode |= PP_DEBLOCK_C_H;
740 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-y" ) )
741 p_vdec->i_pp_mode |= PP_DERING_Y;
742 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-c" ) )
743 p_vdec->i_pp_mode |= PP_DERING_C;
745 if( ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ) > 0 )||
746 ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" ) )||
747 ( p_vdec->i_pp_mode != 0 ) )
749 /* check if the codec support postproc. */
750 switch( i_ffmpeg_codec )
752 #if LIBAVCODEC_BUILD > 4608
753 case( CODEC_ID_MSMPEG4V1 ):
754 case( CODEC_ID_MSMPEG4V2 ):
755 case( CODEC_ID_MSMPEG4V3 ):
757 case( CODEC_ID_MSMPEG4 ):
759 case( CODEC_ID_MPEG4 ):
760 case( CODEC_ID_H263 ):
761 // case( CODEC_ID_H263P ): I don't use it up to now
762 case( CODEC_ID_H263I ):
763 /* Ok we can make postprocessing :)) */
764 /* first try to get a postprocess module */
765 #if LIBAVCODEC_BUILD > 4613
766 p_vdec->p_pp = vlc_object_create( p_vdec->p_fifo,
767 sizeof( postprocessing_t ) );
768 p_vdec->p_pp->psz_object_name = "postprocessing";
769 p_vdec->p_pp->p_module =
770 module_Need( p_vdec->p_pp, "postprocessing", "$ffmpeg-pp" );
772 if( !p_vdec->p_pp->p_module )
774 msg_Warn( p_vdec->p_fifo,
775 "no suitable postprocessing module" );
776 vlc_object_destroy( p_vdec->p_pp );
778 p_vdec->i_pp_mode = 0;
782 /* get mode upon quality */
784 p_vdec->p_pp->pf_getmode(
785 config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ),
786 config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" )
789 /* allocate table for postprocess */
790 p_vdec->p_context->quant_store =
791 malloc( sizeof( int ) * ( MBR + 1 ) * ( MBC + 1 ) );
792 p_vdec->p_context->qstride = MBC + 1;
795 p_vdec->i_pp_mode = 0;
796 msg_Warn( p_vdec->p_fifo,
797 "post-processing not supported, upgrade ffmpeg" );
801 p_vdec->i_pp_mode = 0;
802 msg_Warn( p_vdec->p_fifo,
803 "Post processing unsupported for this codec" );
808 // memset( &p_vdec->statistic, 0, sizeof( statistic_t ) );
813 /*****************************************************************************
814 * DecodeThread: Called for decode one frame
815 *****************************************************************************/
816 static void DecodeThread( videodec_thread_t *p_vdec )
821 AVPicture avpicture; /* ffmpeg picture */
822 picture_t *p_pic; /* videolan picture */
823 /* we have to get a frame stored in a pes
824 give it to ffmpeg decoder
825 and send the image to the output */
827 /* TODO implement it in a better way */
828 /* A good idea could be to decode all I pictures and see for the other */
829 if( ( p_vdec->b_hurry_up )&&
830 ( p_vdec->i_frame_late > 4 ) )
832 #if LIBAVCODEC_BUILD > 4603
834 if( p_vdec->i_frame_late < 8 )
836 p_vdec->p_context->hurry_up = 2;
840 /* too much late picture, won't decode
841 but break picture until a new I, and for mpeg4 ...*/
842 p_vdec->i_frame_late--; /* needed else it will never be decrease */
843 __PES_NEXT( p_vdec->p_fifo );
847 if( p_vdec->i_frame_late < 8 )
849 b_drawpicture = 0; /* not really good but .. UPGRADE FFMPEG !! */
853 /* too much late picture, won't decode
854 but break picture until a new I, and for mpeg4 ...*/
855 p_vdec->i_frame_late--; /* needed else it will never be decrease */
856 __PES_NEXT( p_vdec->p_fifo );
864 #if LIBAVCODEC_BUILD > 4603
865 p_vdec->p_context->hurry_up = 0;
869 __GetFrame( p_vdec );
871 i_status = avcodec_decode_video( p_vdec->p_context,
875 p_vdec->i_framesize);
877 __NextFrame( p_vdec );
881 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
882 p_vdec->i_framesize );
883 p_vdec->i_frame_error++;
886 /* Update frame late count*/
887 /* I don't make statistic on decoding time */
888 if( p_vdec->i_pts <= mdate())
890 p_vdec->i_frame_late++;
894 p_vdec->i_frame_late = 0;
897 if( !b_gotpicture || avpicture.linesize[0] == 0 || !b_drawpicture)
903 if( !ffmpeg_CheckVout( p_vdec->p_vout,
904 p_vdec->p_context->width,
905 p_vdec->p_context->height,
906 p_vdec->p_context->aspect_ratio_info,
907 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
910 ffmpeg_CreateVout( p_vdec,
911 p_vdec->p_context->width,
912 p_vdec->p_context->height,
913 p_vdec->p_context->aspect_ratio_info,
914 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt));
915 if( !p_vdec->p_vout )
917 msg_Err( p_vdec->p_fifo, "cannot create vout" );
918 p_vdec->p_fifo->b_error = 1; /* abort */
923 /* Get a new picture */
924 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
926 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
930 msleep( VOUT_OUTMEM_SLEEP );
932 /* fill p_picture_t from avpicture, do I410->I420 if needed
933 and do post-processing if requested */
934 ffmpeg_GetPicture( p_pic, &avpicture, p_vdec );
936 /* FIXME correct avi and use i_dts */
938 /* Send decoded frame to vout */
939 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
940 vout_DisplayPicture( p_vdec->p_vout, p_pic );
946 /*****************************************************************************
947 * EndThread: thread destruction
948 *****************************************************************************
949 * This function is called when the thread ends after a sucessful
951 *****************************************************************************/
952 static void EndThread( videodec_thread_t *p_vdec )
961 /* release postprocessing module */
962 module_Unneed( p_vdec->p_pp, p_vdec->p_pp->p_module );
963 vlc_object_destroy( p_vdec->p_pp );
967 if( p_vdec->p_context != NULL)
969 FREE( p_vdec->p_context->quant_store );
970 avcodec_close( p_vdec->p_context );
971 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
972 p_vdec->psz_namecodec );
973 free( p_vdec->p_context );
976 if( p_vdec->p_vout != NULL )
978 /* We are about to die. Reattach video output to p_vlc. */
979 vlc_object_detach( p_vdec->p_vout );
980 vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
983 FREE( p_vdec->format.p_data );
984 FREE( p_vdec->p_buffer );