1 /*****************************************************************************
2 * ffmpeg.c: video decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ffmpeg.c,v 1.10 2002/10/24 10:33:09 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 !"
88 #define WORAROUND_BUG_LONGTEXT \
89 "Try to fix some bugs\n" \
92 "4 xvid interlaced\n" \
99 add_category_hint( N_("Ffmpeg"), NULL );
100 #if LIBAVCODEC_BUILD >= 4611
101 add_integer ( "ffmpeg-error-resilience", -1, NULL,
102 "error resilience", ERROR_RESILIENCE_LONGTEXT );
103 add_integer ( "ffmpeg-workaround-bugs", 0, NULL,
104 "workaround bugs", WORAROUND_BUG_LONGTEXT );
106 add_bool( "ffmpeg-hurry-up", 0, NULL, "hurry up", HURRY_UP_LONGTEXT );
108 add_category_hint( N_("Post processing"), NULL );
109 add_module( "ffmpeg-pp", "postprocessing",NULL, NULL,
110 N_( "ffmpeg postprocessing module" ), NULL );
111 add_integer( "ffmpeg-pp-q", 0, NULL,
112 "post processing quality", POSTPROCESSING_Q_LONGTEXT );
113 add_bool( "ffmpeg-pp-auto", 0, NULL,
114 "auto-level Post processing quality", POSTPROCESSING_AQ_LONGTEXT );
115 add_bool( "ffmpeg-db-yv", 0, NULL,
116 "force vertical luminance deblocking",
117 "force vertical luminance deblocking (override other settings)" );
118 add_bool( "ffmpeg-db-yh", 0, NULL,
119 "force horizontal luminance deblocking",
120 "force horizontal luminance deblocking (override other settings)" );
121 add_bool( "ffmpeg-db-cv", 0, NULL,
122 "force vertical chrominance deblocking",
123 "force vertical chrominance deblocking (override other settings)" );
124 add_bool( "ffmpeg-db-ch", 0, NULL,
125 "force horizontal chrominance deblocking",
126 "force horizontal chrominance deblocking (override other settings) " );
127 add_bool( "ffmpeg-dr-y", 0, NULL,
128 "force luminance deringing",
129 "force luminance deringing (override other settings)" );
130 add_bool( "ffmpeg-dr-c", 0, NULL,
131 "force chrominance deringing",
132 "force chrominance deringing (override other settings)" );
134 set_description( _("ffmpeg video decoder((MS)MPEG4,SVQ1,H263)") );
135 set_capability( "decoder", 70 );
136 set_callbacks( OpenDecoder, NULL );
139 /*****************************************************************************
140 * OpenDecoder: probe the decoder and return score
141 *****************************************************************************
142 * Tries to launch a decoder and return score so that the interface is able
144 *****************************************************************************/
145 static int OpenDecoder( vlc_object_t *p_this )
147 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
149 if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL ) )
151 p_fifo->pf_run = RunDecoder;
158 /*****************************************************************************
159 * RunDecoder: this function is called just after the thread is created
160 *****************************************************************************/
161 static int RunDecoder( decoder_fifo_t *p_fifo )
163 videodec_thread_t *p_vdec;
166 if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
168 msg_Err( p_fifo, "out of memory" );
169 DecoderError( p_fifo );
172 memset( p_vdec, 0, sizeof( videodec_thread_t ) );
174 p_vdec->p_fifo = p_fifo;
176 if( InitThread( p_vdec ) != 0 )
178 DecoderError( p_fifo );
182 while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
184 DecodeThread( p_vdec );
187 if( ( b_error = p_vdec->p_fifo->b_error ) )
189 DecoderError( p_vdec->p_fifo );
203 /*****************************************************************************
205 *****************************************************************************/
207 #define GetWLE( p ) \
208 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
210 #define GetDWLE( p ) \
211 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
212 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
214 #define FREE( p ) if( p ) free( p ); p = NULL
216 static void ffmpeg_ParseBitMapInfoHeader( bitmapinfoheader_t *p_bh,
219 p_bh->i_size = GetDWLE( p_data );
220 p_bh->i_width = GetDWLE( p_data + 4 );
221 p_bh->i_height = GetDWLE( p_data + 8 );
222 p_bh->i_planes = GetWLE( p_data + 12 );
223 p_bh->i_bitcount = GetWLE( p_data + 14 );
224 p_bh->i_compression = GetDWLE( p_data + 16 );
225 p_bh->i_sizeimage = GetDWLE( p_data + 20 );
226 p_bh->i_xpelspermeter = GetDWLE( p_data + 24 );
227 p_bh->i_ypelspermeter = GetDWLE( p_data + 28 );
228 p_bh->i_clrused = GetDWLE( p_data + 32 );
229 p_bh->i_clrimportant = GetDWLE( p_data + 36 );
231 if( p_bh->i_size > 40 )
233 p_bh->i_data = p_bh->i_size - 40;
234 if( ( p_bh->p_data = malloc( p_bh->i_data ) ) )
236 memcpy( p_bh->p_data, p_data + 40, p_bh->i_data );
251 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
256 data_packet_t *p_data;
259 p_data = p_pes->p_first;
260 while( p_data != NULL && i_count < i_max )
263 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
269 p_data->p_payload_start,
273 p_data = p_data->p_next;
278 if( i_count < i_max )
280 memset( p_buf, 0, i_max - i_count );
284 /* Check if we have a Vout with good parameters */
285 static int ffmpeg_CheckVout( vout_thread_t *p_vout,
297 /* we will try to make conversion */
298 i_chroma = VLC_FOURCC('I','4','2','0');
301 if( ( p_vout->render.i_width != i_width )||
302 ( p_vout->render.i_height != i_height )||
303 ( p_vout->render.i_chroma != i_chroma )||
304 ( p_vout->render.i_aspect !=
305 ffmpeg_FfAspect( i_width, i_height, i_aspect ) ) )
317 static vout_thread_t *ffmpeg_CreateVout( videodec_thread_t *p_vdec,
323 vout_thread_t *p_vout;
325 if( (!i_width)||(!i_height) )
327 return( NULL ); /* Can't create a new vout without display size */
332 /* we make conversion if possible*/
333 i_chroma = VLC_FOURCC('I','4','2','0');
334 msg_Warn( p_vdec->p_fifo, "Internal chroma conversion (FIXME)");
335 /* It's mainly for I410 -> I420 conversion that I've made,
336 it's buggy and very slow */
339 i_aspect = ffmpeg_FfAspect( i_width, i_height, i_aspect );
341 /* Spawn a video output if there is none. First we look for our children,
342 * then we look for any other vout that might be available. */
343 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
347 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
353 if( !ffmpeg_CheckVout( p_vout,
354 i_width, i_height, i_aspect,i_chroma ) )
356 /* We are not interested in this format, close this vout */
357 vlc_object_detach( p_vout );
358 vlc_object_release( p_vout );
359 vout_DestroyThread( p_vout );
364 /* This video output is cool! Hijack it. */
365 vlc_object_detach( p_vout );
366 vlc_object_attach( p_vout, p_vdec->p_fifo );
367 vlc_object_release( p_vout );
373 msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
375 p_vout = vout_CreateThread( p_vdec->p_fifo,
377 i_chroma, i_aspect );
383 /* FIXME FIXME FIXME this is a big shit
384 does someone want to rewrite this function ?
385 or said to me how write a better thing
388 static void ffmpeg_ConvertPictureI410toI420( picture_t *p_pic,
389 AVPicture *p_avpicture,
390 videodec_thread_t *p_vdec )
396 int i_stride, i_lines;
397 int i_height, i_width;
400 i_height = p_vdec->p_context->height;
401 i_width = p_vdec->p_context->width;
403 p_dst = p_pic->p[0].p_pixels;
404 p_src = p_avpicture->data[0];
406 /* copy first plane */
407 for( i_y = 0; i_y < i_height; i_y++ )
409 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width);
410 p_dst += p_pic->p[0].i_pitch;
411 p_src += p_avpicture->linesize[0];
414 /* process each plane in a temporary buffer */
415 for( i_plane = 1; i_plane < 3; i_plane++ )
417 i_stride = p_avpicture->linesize[i_plane];
418 i_lines = i_height / 4;
420 p_dst = p_plane[i_plane] = malloc( i_lines * i_stride * 2 * 2 );
421 p_src = p_avpicture->data[i_plane];
423 /* for each source line */
424 for( i_y = 0; i_y < i_lines; i_y++ )
426 for( i_x = 0; i_x < i_stride - 1; i_x++ )
428 p_dst[2 * i_x ] = p_src[i_x];
429 p_dst[2 * i_x + 1] = ( p_src[i_x] + p_src[i_x + 1]) / 2;
432 p_dst[2 * i_stride - 2] = p_src[i_x];
433 p_dst[2 * i_stride - 1] = p_src[i_x];
435 p_dst += 4 * i_stride; /* process the next even lines */
442 for( i_plane = 1; i_plane < 3; i_plane++ )
444 i_stride = p_avpicture->linesize[i_plane];
445 i_lines = i_height / 4;
447 p_dst = p_plane[i_plane] + 2*i_stride;
448 p_src = p_plane[i_plane];
450 for( i_y = 0; i_y < i_lines - 1; i_y++ )
452 for( i_x = 0; i_x < 2 * i_stride ; i_x++ )
454 p_dst[i_x] = ( p_src[i_x] + p_src[i_x + 4*i_stride])/2;
457 p_dst += 4 * i_stride; /* process the next odd lines */
458 p_src += 4 * i_stride;
461 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, 2*i_stride );
463 /* copy to p_pic, by block
464 if I do pixel per pixel it segfault. It's why I use
465 temporaries buffers */
466 for( i_plane = 1; i_plane < 3; i_plane++ )
470 p_src = p_plane[i_plane];
471 p_dst = p_pic->p[i_plane].p_pixels;
473 i_size = __MIN( 2*i_stride, p_pic->p[i_plane].i_pitch);
474 for( i_y = 0; i_y < __MIN(p_pic->p[i_plane].i_lines, 2 * i_lines); i_y++ )
476 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
477 p_src += 2 * i_stride;
478 p_dst += p_pic->p[i_plane].i_pitch;
480 free( p_plane[i_plane] );
485 static void ffmpeg_GetPicture( picture_t *p_pic,
486 AVPicture *p_avpicture,
487 videodec_thread_t *p_vdec )
498 if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
500 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
502 p_src = p_avpicture->data[i_plane];
503 p_dst = p_pic->p[i_plane].p_pixels;
504 i_src_stride = p_avpicture->linesize[i_plane];
505 i_dst_stride = p_pic->p[i_plane].i_pitch;
507 i_size = __MIN( i_src_stride, i_dst_stride );
508 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
510 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
511 p_src += i_src_stride;
512 p_dst += i_dst_stride;
515 if( ( p_vdec->i_pp_mode )&&
516 ( ( p_vdec->p_vout->render.i_chroma ==
517 VLC_FOURCC( 'I','4','2','0' ) )||
518 ( p_vdec->p_vout->render.i_chroma ==
519 VLC_FOURCC( 'Y','V','1','2' ) ) ) )
522 #if LIBAVCODEC_BUILD > 4313
523 p_vdec->p_pp->pf_postprocess( p_pic,
524 p_vdec->p_context->quant_store,
525 p_vdec->p_context->qstride,
532 /* we need to convert to I420 */
533 switch( p_vdec->p_context->pix_fmt )
535 #if LIBAVCODEC_BUILD >= 4615
536 case( PIX_FMT_YUV410P ):
537 ffmpeg_ConvertPictureI410toI420( p_pic, p_avpicture, p_vdec );
541 p_vdec->p_fifo->b_error = 1;
551 /*****************************************************************************
553 * Functions that initialize, decode and end the decoding process
555 *****************************************************************************/
557 /*****************************************************************************
558 * InitThread: initialize vdec output thread
559 *****************************************************************************
560 * This function is called from decoder_Run and performs the second step
561 * of the initialization. It returns 0 on success. Note that the thread's
562 * flag are not modified inside this function.
564 * ffmpeg codec will be open, some memory allocated. But Vout is not yet
565 * open (done after the first decoded frame)
566 *****************************************************************************/
567 static int InitThread( videodec_thread_t *p_vdec )
572 if( p_vdec->p_fifo->p_demux_data )
574 ffmpeg_ParseBitMapInfoHeader( &p_vdec->format,
575 (u8*)p_vdec->p_fifo->p_demux_data );
579 msg_Warn( p_vdec->p_fifo, "display informations missing" );
582 /* **** init ffmpeg library (libavcodec) ***** */
586 avcodec_register_all();
588 msg_Dbg( p_vdec->p_fifo, "library ffmpeg initialized" );
592 msg_Dbg( p_vdec->p_fifo, "library ffmpeg already initialized" );
595 /* ***** Search for codec ***** */
596 ffmpeg_GetFfmpegCodec( p_vdec->p_fifo->i_fourcc,
598 &p_vdec->psz_namecodec );
600 avcodec_find_decoder( i_ffmpeg_codec );
602 if( !p_vdec->p_codec )
604 msg_Err( p_vdec->p_fifo, "codec not found (%s)",
605 p_vdec->psz_namecodec );
609 /* ***** Fill p_context with init values ***** */
610 #if LIBAVCODEC_BUILD >= 4624
611 p_vdec->p_context = avcodec_alloc_context();
613 p_vdec->p_context = malloc( sizeof( AVCodecContext ) );
614 memset( p_vdec->p_context, 0, sizeof( AVCodecContext ) );
617 p_vdec->p_context->width = p_vdec->format.i_width;
618 p_vdec->p_context->height = p_vdec->format.i_height;
620 /* ***** Get configuration of ffmpeg plugin ***** */
621 #if LIBAVCODEC_BUILD >= 4611
622 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
623 p_vdec->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
625 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
626 p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
628 #if LIBAVCODEC_BUILD >= 4614
629 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
631 p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
634 p_vdec->b_hurry_up = config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up");
637 /* ***** Open the codec ***** */
638 if (avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0)
640 msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
641 p_vdec->psz_namecodec );
646 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
647 p_vdec->psz_namecodec );
650 /* ***** init this codec with special data ***** */
651 if( p_vdec->format.i_data )
656 switch( i_ffmpeg_codec )
658 case( CODEC_ID_MPEG4 ):
659 avcodec_decode_video( p_vdec->p_context, &avpicture,
661 p_vdec->format.p_data,
662 p_vdec->format.i_data );
665 if( p_vdec->p_fifo->i_fourcc == FOURCC_MP4S ||
666 p_vdec->p_fifo->i_fourcc == FOURCC_mp4s ||
667 p_vdec->p_fifo->i_fourcc == FOURCC_M4S2 ||
668 p_vdec->p_fifo->i_fourcc == FOURCC_m4s2 )
670 p_vdec->p_context->extradata_size = p_vdec->format.i_data;
671 p_vdec->p_context->extradata = malloc( p_vdec->format.i_data );
672 memcpy( p_vdec->p_context->extradata,
673 p_vdec->format.p_data,
674 p_vdec->format.i_data );
681 /* ***** Load post processing ***** */
683 /* get overridding settings */
684 p_vdec->i_pp_mode = 0;
685 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yv" ) )
686 p_vdec->i_pp_mode |= PP_DEBLOCK_Y_V;
687 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yh" ) )
688 p_vdec->i_pp_mode |= PP_DEBLOCK_Y_H;
689 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-cv" ) )
690 p_vdec->i_pp_mode |= PP_DEBLOCK_C_V;
691 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-ch" ) )
692 p_vdec->i_pp_mode |= PP_DEBLOCK_C_H;
693 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-y" ) )
694 p_vdec->i_pp_mode |= PP_DERING_Y;
695 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-c" ) )
696 p_vdec->i_pp_mode |= PP_DERING_C;
698 if( ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ) > 0 )||
699 ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" ) )||
700 ( p_vdec->i_pp_mode != 0 ) )
702 /* check if the codec support postproc. */
703 switch( i_ffmpeg_codec )
705 #if LIBAVCODEC_BUILD > 4608
706 case( CODEC_ID_MSMPEG4V1 ):
707 case( CODEC_ID_MSMPEG4V2 ):
708 case( CODEC_ID_MSMPEG4V3 ):
710 case( CODEC_ID_MSMPEG4 ):
712 case( CODEC_ID_MPEG4 ):
713 case( CODEC_ID_H263 ):
714 // case( CODEC_ID_H263P ): I don't use it up to now
715 case( CODEC_ID_H263I ):
716 /* Ok we can make postprocessing :)) */
717 /* first try to get a postprocess module */
718 #if LIBAVCODEC_BUILD > 4613
719 p_vdec->p_pp = vlc_object_create( p_vdec->p_fifo,
720 sizeof( postprocessing_t ) );
721 p_vdec->p_pp->psz_object_name = "postprocessing";
722 p_vdec->p_pp->p_module =
723 module_Need( p_vdec->p_pp, "postprocessing", "$ffmpeg-pp" );
725 if( !p_vdec->p_pp->p_module )
727 msg_Warn( p_vdec->p_fifo,
728 "no suitable postprocessing module" );
729 vlc_object_destroy( p_vdec->p_pp );
731 p_vdec->i_pp_mode = 0;
735 /* get mode upon quality */
737 p_vdec->p_pp->pf_getmode(
738 config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ),
739 config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" )
742 /* allocate table for postprocess */
743 p_vdec->p_context->quant_store =
744 malloc( sizeof( int ) * ( MBR + 1 ) * ( MBC + 1 ) );
745 p_vdec->p_context->qstride = MBC + 1;
748 p_vdec->i_pp_mode = 0;
749 msg_Warn( p_vdec->p_fifo,
750 "post-processing not supported, upgrade ffmpeg" );
754 p_vdec->i_pp_mode = 0;
755 msg_Warn( p_vdec->p_fifo,
756 "Post processing unsupported for this codec" );
761 // memset( &p_vdec->statistic, 0, sizeof( statistic_t ) );
766 /*****************************************************************************
767 * DecodeThread: Called for decode one frame
768 *****************************************************************************/
769 static void DecodeThread( videodec_thread_t *p_vdec )
776 AVPicture avpicture; /* ffmpeg picture */
777 picture_t *p_pic; /* videolan picture */
778 /* we have to get a frame stored in a pes
779 give it to ffmpeg decoder
780 and send the image to the output */
782 /* TODO implement it in a better way */
783 /* A good idea could be to decode all I pictures and see for the other */
784 if( ( p_vdec->b_hurry_up )&&
785 ( p_vdec->i_frame_late > 4 ) )
787 #if LIBAVCODEC_BUILD > 4603
789 if( p_vdec->i_frame_late < 8 )
791 p_vdec->p_context->hurry_up = 2;
795 /* too much late picture, won't decode
796 but break picture until a new I, and for mpeg4 ...*/
797 p_vdec->i_frame_late--; /* needed else it will never be decrease */
798 NextPES( p_vdec->p_fifo );
802 if( p_vdec->i_frame_late < 8 )
804 b_drawpicture = 0; /* not really good but .. UPGRADE FFMPEG !! */
808 /* too much late picture, won't decode
809 but break picture until a new I, and for mpeg4 ...*/
810 p_vdec->i_frame_late--; /* needed else it will never be decrease */
811 NextPES( p_vdec->p_fifo );
819 #if LIBAVCODEC_BUILD > 4603
820 p_vdec->p_context->hurry_up = 0;
826 if( !( p_pes = GetPES( p_vdec->p_fifo ) ) )
828 p_vdec->p_fifo->b_error = 1;
831 p_vdec->i_pts = p_pes->i_pts;
832 i_frame_size = p_pes->i_pes_size;
834 if( i_frame_size > 0 )
836 if( p_vdec->i_buffer < i_frame_size + 16 )
838 FREE( p_vdec->p_buffer );
839 p_vdec->p_buffer = malloc( i_frame_size + 16 );
840 p_vdec->i_buffer = i_frame_size + 16;
843 GetPESData( p_vdec->p_buffer, p_vdec->i_buffer, p_pes );
845 NextPES( p_vdec->p_fifo );
846 } while( i_frame_size <= 0 );
849 i_status = avcodec_decode_video( p_vdec->p_context,
858 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
860 p_vdec->i_frame_error++;
863 /* Update frame late count*/
864 /* I don't make statistic on decoding time */
865 if( p_vdec->i_pts <= mdate())
867 p_vdec->i_frame_late++;
871 p_vdec->i_frame_late = 0;
874 if( !b_gotpicture || avpicture.linesize[0] == 0 || !b_drawpicture)
880 if( !ffmpeg_CheckVout( p_vdec->p_vout,
881 p_vdec->p_context->width,
882 p_vdec->p_context->height,
883 p_vdec->p_context->aspect_ratio_info,
884 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
887 ffmpeg_CreateVout( p_vdec,
888 p_vdec->p_context->width,
889 p_vdec->p_context->height,
890 p_vdec->p_context->aspect_ratio_info,
891 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt));
892 if( !p_vdec->p_vout )
894 msg_Err( p_vdec->p_fifo, "cannot create vout" );
895 p_vdec->p_fifo->b_error = 1; /* abort */
900 /* Get a new picture */
901 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
903 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
907 msleep( VOUT_OUTMEM_SLEEP );
909 /* fill p_picture_t from avpicture, do I410->I420 if needed
910 and do post-processing if requested */
911 ffmpeg_GetPicture( p_pic, &avpicture, p_vdec );
913 /* FIXME correct avi and use i_dts */
915 /* Send decoded frame to vout */
916 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
917 vout_DisplayPicture( p_vdec->p_vout, p_pic );
923 /*****************************************************************************
924 * EndThread: thread destruction
925 *****************************************************************************
926 * This function is called when the thread ends after a sucessful
928 *****************************************************************************/
929 static void EndThread( videodec_thread_t *p_vdec )
938 /* release postprocessing module */
939 module_Unneed( p_vdec->p_pp, p_vdec->p_pp->p_module );
940 vlc_object_destroy( p_vdec->p_pp );
944 if( p_vdec->p_context != NULL)
946 FREE( p_vdec->p_context->quant_store );
947 FREE( p_vdec->p_context->extradata );
948 avcodec_close( p_vdec->p_context );
949 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
950 p_vdec->psz_namecodec );
951 free( p_vdec->p_context );
954 if( p_vdec->p_vout != NULL )
956 /* We are about to die. Reattach video output to p_vlc. */
957 vlc_object_detach( p_vdec->p_vout );
958 vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
961 FREE( p_vdec->format.p_data );
962 FREE( p_vdec->p_buffer );