1 /*****************************************************************************
2 * video.c: video decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video.c,v 1.5 2002/11/19 20:45:09 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 #include <vlc/decoder.h>
34 #include <vlc/input.h>
38 #ifdef HAVE_SYS_TIMES_H
39 # include <sys/times.h>
42 #include "avcodec.h" /* ffmpeg */
44 #include "postprocessing/postprocessing.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 static void ffmpeg_CopyPicture( picture_t *, AVPicture *, vdec_thread_t * );
53 static void ffmpeg_PostProcPicture( vdec_thread_t *, picture_t * );
54 static int ffmpeg_GetFrameBuf( struct AVCodecContext *, int, int, int );
56 /* FIXME FIXME some of them are wrong */
57 static int i_ffmpeg_PixFmtToChroma[] =
59 /* PIX_FMT_ANY = -1, PIX_FMT_YUV420P,
60 PIX_FMT_YUV422, PIX_FMT_RGB24,
61 PIX_FMT_BGR24, PIX_FMT_YUV422P,
62 PIX_FMT_YUV444P, PIX_FMT_YUV410P
64 0, VLC_FOURCC('I','4','2','0'),
65 VLC_FOURCC('I','4','2','0'), VLC_FOURCC('R','V','2','4'),
66 0, VLC_FOURCC('Y','4','2','2'),
67 VLC_FOURCC('I','4','4','4'), 0
70 /*****************************************************************************
72 *****************************************************************************/
74 static inline u32 ffmpeg_PixFmtToChroma( int i_ffmpegchroma )
76 if( ++i_ffmpegchroma > 7 )
82 return( i_ffmpeg_PixFmtToChroma[i_ffmpegchroma] );
86 static inline int ffmpeg_FfAspect( int i_width, int i_height, int i_ffaspect )
90 case( FF_ASPECT_4_3_625 ):
91 case( FF_ASPECT_4_3_525 ):
92 return( VOUT_ASPECT_FACTOR * 4 / 3);
93 case( FF_ASPECT_16_9_625 ):
94 case( FF_ASPECT_16_9_525 ):
95 return( VOUT_ASPECT_FACTOR * 16 / 9 );
96 case( FF_ASPECT_SQUARE ):
98 return( VOUT_ASPECT_FACTOR * i_width / i_height );
102 /* Check if we have a Vout with good parameters */
103 static int ffmpeg_CheckVout( vout_thread_t *p_vout,
115 /* we will try to make conversion */
116 i_chroma = VLC_FOURCC('I','4','2','0');
119 if( ( p_vout->render.i_width != i_width )||
120 ( p_vout->render.i_height != i_height )||
121 ( p_vout->render.i_chroma != i_chroma )||
122 ( p_vout->render.i_aspect !=
123 ffmpeg_FfAspect( i_width, i_height, i_aspect ) ) )
134 static vout_thread_t *ffmpeg_CreateVout( vdec_thread_t *p_vdec,
140 vout_thread_t *p_vout;
142 if( (!i_width)||(!i_height) )
144 return( NULL ); /* Can't create a new vout without display size */
149 /* we make conversion if possible*/
150 i_chroma = VLC_FOURCC('I','4','2','0');
151 msg_Warn( p_vdec->p_fifo, "Internal chroma conversion (FIXME)");
152 /* It's mainly for I410 -> I420 conversion that I've made,
153 it's buggy and very slow */
156 i_aspect = ffmpeg_FfAspect( i_width, i_height, i_aspect );
158 /* Spawn a video output if there is none. First we look for our children,
159 * then we look for any other vout that might be available. */
160 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
164 p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
170 if( !ffmpeg_CheckVout( p_vout,
171 i_width, i_height, i_aspect,i_chroma ) )
173 /* We are not interested in this format, close this vout */
174 vlc_object_detach( p_vout );
175 vlc_object_release( p_vout );
176 vout_DestroyThread( p_vout );
181 /* This video output is cool! Hijack it. */
182 vlc_object_detach( p_vout );
183 vlc_object_attach( p_vout, p_vdec->p_fifo );
184 vlc_object_release( p_vout );
190 msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
192 p_vout = vout_CreateThread( p_vdec->p_fifo,
194 i_chroma, i_aspect );
200 /* FIXME FIXME FIXME this is a big shit
201 does someone want to rewrite this function ?
202 or said to me how write a better thing
205 static void ffmpeg_ConvertPictureI410toI420( picture_t *p_pic,
206 AVPicture *p_avpicture,
207 vdec_thread_t *p_vdec )
213 int i_stride, i_lines;
214 int i_height, i_width;
217 i_height = p_vdec->p_context->height;
218 i_width = p_vdec->p_context->width;
220 p_dst = p_pic->p[0].p_pixels;
221 p_src = p_avpicture->data[0];
223 /* copy first plane */
224 for( i_y = 0; i_y < i_height; i_y++ )
226 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width);
227 p_dst += p_pic->p[0].i_pitch;
228 p_src += p_avpicture->linesize[0];
231 /* process each plane in a temporary buffer */
232 for( i_plane = 1; i_plane < 3; i_plane++ )
234 i_stride = p_avpicture->linesize[i_plane];
235 i_lines = i_height / 4;
237 p_dst = p_plane[i_plane] = malloc( i_lines * i_stride * 2 * 2 );
238 p_src = p_avpicture->data[i_plane];
240 /* for each source line */
241 for( i_y = 0; i_y < i_lines; i_y++ )
243 for( i_x = 0; i_x < i_stride - 1; i_x++ )
245 p_dst[2 * i_x ] = p_src[i_x];
246 p_dst[2 * i_x + 1] = ( p_src[i_x] + p_src[i_x + 1]) / 2;
249 p_dst[2 * i_stride - 2] = p_src[i_x];
250 p_dst[2 * i_stride - 1] = p_src[i_x];
252 p_dst += 4 * i_stride; /* process the next even lines */
257 for( i_plane = 1; i_plane < 3; i_plane++ )
259 i_stride = p_avpicture->linesize[i_plane];
260 i_lines = i_height / 4;
262 p_dst = p_plane[i_plane] + 2*i_stride;
263 p_src = p_plane[i_plane];
265 for( i_y = 0; i_y < i_lines - 1; i_y++ )
267 for( i_x = 0; i_x < 2 * i_stride ; i_x++ )
269 p_dst[i_x] = ( p_src[i_x] + p_src[i_x + 4*i_stride])/2;
272 p_dst += 4 * i_stride; /* process the next odd lines */
273 p_src += 4 * i_stride;
276 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, 2*i_stride );
278 /* copy to p_pic, by block
279 if I do pixel per pixel it segfault. It's why I use
280 temporaries buffers */
281 for( i_plane = 1; i_plane < 3; i_plane++ )
284 p_src = p_plane[i_plane];
285 p_dst = p_pic->p[i_plane].p_pixels;
287 i_size = __MIN( 2*i_stride, p_pic->p[i_plane].i_pitch);
288 for( i_y = 0; i_y < __MIN(p_pic->p[i_plane].i_lines, 2 * i_lines); i_y++ )
290 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
291 p_src += 2 * i_stride;
292 p_dst += p_pic->p[i_plane].i_pitch;
294 free( p_plane[i_plane] );
298 /*****************************************************************************
300 * Functions that initialize, decode and end the decoding process
302 * Functions exported for ffmpeg.c
303 * * E_( InitThread_Video )
304 * * E_( DecodeThread )
305 * * E_( EndThread_Video )
306 *****************************************************************************/
308 /*****************************************************************************
309 * InitThread: initialize vdec output thread
310 *****************************************************************************
311 * This function is called from decoder_Run and performs the second step
312 * of the initialization. It returns 0 on success. Note that the thread's
313 * flag are not modified inside this function.
315 * ffmpeg codec will be open, some memory allocated. But Vout is not yet
316 * open (done after the first decoded frame)
317 *****************************************************************************/
318 int E_( InitThread_Video )( vdec_thread_t *p_vdec )
322 if( p_vdec->p_fifo->p_demux_data )
324 p_vdec->p_format = (BITMAPINFOHEADER *)p_vdec->p_fifo->p_demux_data;
328 msg_Warn( p_vdec->p_fifo, "display informations missing" );
331 /* ***** Fill p_context with init values ***** */
332 p_vdec->p_context->width = p_vdec->p_format->biWidth;
333 p_vdec->p_context->height = p_vdec->p_format->biHeight;
335 /* ***** Get configuration of ffmpeg plugin ***** */
336 #if LIBAVCODEC_BUILD >= 4611
337 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
338 p_vdec->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
340 i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
341 p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
343 #if LIBAVCODEC_BUILD >= 4614
344 if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
346 p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
350 p_vdec->b_hurry_up = config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up");
352 p_vdec->p_lastpic = NULL;
353 p_vdec->p_secondlastpic = NULL;
354 p_vdec->b_direct_rendering = 0;
355 #if LIBAVCODEC_BUILD > 4615
356 if( (p_vdec->p_codec->capabilities & CODEC_CAP_DR1)
357 && (p_vdec->p_context->pix_fmt != PIX_FMT_YUV410P) &&
358 config_GetInt( p_vdec->p_fifo, "ffmpeg-dr" ) )
360 msg_Dbg( p_vdec->p_fifo, "using direct rendering" );
361 p_vdec->b_direct_rendering = 1;
362 p_vdec->p_context->flags|= CODEC_FLAG_EMU_EDGE | CODEC_FLAG_DR1;
363 p_vdec->p_context->get_buffer_callback = ffmpeg_GetFrameBuf;
364 p_vdec->p_context->opaque = p_vdec;
368 /* ***** Open the codec ***** */
369 if( avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0 )
371 msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
372 p_vdec->psz_namecodec );
377 msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
378 p_vdec->psz_namecodec );
381 /* ***** init this codec with special data ***** */
382 if( p_vdec->p_format->biSize > sizeof(BITMAPINFOHEADER) )
387 switch( p_vdec->i_codec_id )
389 case( CODEC_ID_MPEG4 ):
390 avcodec_decode_video( p_vdec->p_context, &avpicture,
392 (void *)&p_vdec->p_format[1],
393 p_vdec->p_format->biSize
394 - sizeof(BITMAPINFOHEADER) );
397 if( p_vdec->p_fifo->i_fourcc == FOURCC_MP4S ||
398 p_vdec->p_fifo->i_fourcc == FOURCC_mp4s ||
399 p_vdec->p_fifo->i_fourcc == FOURCC_M4S2 ||
400 p_vdec->p_fifo->i_fourcc == FOURCC_m4s2 )
402 p_vdec->p_context->extradata_size =
403 p_vdec->p_format->biSize - sizeof(BITMAPINFOHEADER);
404 p_vdec->p_context->extradata =
405 malloc( p_vdec->p_context->extradata_size );
406 memcpy( p_vdec->p_context->extradata,
407 &p_vdec->p_format[1],
408 p_vdec->p_context->extradata_size );
415 /* ***** Load post processing ***** */
417 /* get overridding settings */
418 p_vdec->i_pp_mode = 0;
419 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yv" ) )
420 p_vdec->i_pp_mode |= PP_DEBLOCK_Y_V;
421 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yh" ) )
422 p_vdec->i_pp_mode |= PP_DEBLOCK_Y_H;
423 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-cv" ) )
424 p_vdec->i_pp_mode |= PP_DEBLOCK_C_V;
425 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-ch" ) )
426 p_vdec->i_pp_mode |= PP_DEBLOCK_C_H;
427 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-y" ) )
428 p_vdec->i_pp_mode |= PP_DERING_Y;
429 if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-c" ) )
430 p_vdec->i_pp_mode |= PP_DERING_C;
432 if( ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ) > 0 )||
433 ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" ) )||
434 ( p_vdec->i_pp_mode != 0 ) )
436 /* check if the codec support postproc. */
437 switch( p_vdec->i_codec_id )
439 #if LIBAVCODEC_BUILD > 4608
440 case( CODEC_ID_MSMPEG4V1 ):
441 case( CODEC_ID_MSMPEG4V2 ):
442 case( CODEC_ID_MSMPEG4V3 ):
444 case( CODEC_ID_MSMPEG4 ):
446 case( CODEC_ID_MPEG4 ):
447 case( CODEC_ID_H263 ):
448 // case( CODEC_ID_H263P ): I don't use it up to now
449 case( CODEC_ID_H263I ):
450 /* Ok we can make postprocessing :)) */
451 /* first try to get a postprocess module */
452 #if LIBAVCODEC_BUILD >= 4633
453 p_vdec->p_pp = vlc_object_create( p_vdec->p_fifo,
454 sizeof( postprocessing_t ) );
455 p_vdec->p_pp->psz_object_name = "postprocessing";
456 p_vdec->p_pp->p_module =
457 module_Need( p_vdec->p_pp, "postprocessing", "$ffmpeg-pp" );
459 if( !p_vdec->p_pp->p_module )
461 msg_Warn( p_vdec->p_fifo,
462 "no suitable postprocessing module" );
463 vlc_object_destroy( p_vdec->p_pp );
465 p_vdec->i_pp_mode = 0;
469 /* get mode upon quality */
471 p_vdec->p_pp->pf_getmode(
472 config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ),
473 config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" )
476 /* allocate table for postprocess */
477 // p_vdec->p_context->quant_store =
478 // malloc( sizeof( int ) * ( MBR + 1 ) * ( MBC + 1 ) );
479 // p_vdec->p_context->qstride = MBC + 1;
482 p_vdec->i_pp_mode = 0;
483 msg_Warn( p_vdec->p_fifo,
484 "post-processing not supported, upgrade ffmpeg" );
488 p_vdec->i_pp_mode = 0;
489 msg_Warn( p_vdec->p_fifo,
490 "Post processing unsupported for this codec" );
495 // memset( &p_vdec->statistic, 0, sizeof( statistic_t ) );
500 /*****************************************************************************
501 * DecodeThread: Called to decode one frame
502 *****************************************************************************
503 * We have to get a frame stored in a pes, give it to ffmpeg decoder and send
504 * the image to the output.
505 *****************************************************************************/
506 void E_( DecodeThread_Video )( vdec_thread_t *p_vdec )
513 AVPicture avpicture; /* ffmpeg picture */
514 picture_t *p_pic; /* videolan picture */
516 /* TODO implement it in a better way */
517 /* A good idea could be to decode all I pictures and see for the other */
518 if( ( p_vdec->b_hurry_up )&& ( p_vdec->i_frame_late > 4 ) )
520 #if LIBAVCODEC_BUILD > 4603
522 if( p_vdec->i_frame_late < 8 )
524 p_vdec->p_context->hurry_up = 2;
528 /* too much late picture, won't decode
529 but break picture until a new I, and for mpeg4 ...*/
530 p_vdec->i_frame_late--; /* needed else it will never be decrease */
531 input_ExtractPES( p_vdec->p_fifo, NULL );
535 if( p_vdec->i_frame_late < 8 )
537 b_drawpicture = 0; /* not really good but .. UPGRADE FFMPEG !! */
541 /* too much late picture, won't decode
542 but break picture until a new I, and for mpeg4 ...*/
543 p_vdec->i_frame_late--; /* needed else it will never be decrease */
544 input_ExtractPES( p_vdec->p_fifo, NULL );
552 #if LIBAVCODEC_BUILD > 4603
553 p_vdec->p_context->hurry_up = 0;
559 input_ExtractPES( p_vdec->p_fifo, &p_pes );
562 p_vdec->p_fifo->b_error = 1;
565 p_vdec->pts = p_pes->i_pts;
566 i_frame_size = p_pes->i_pes_size;
568 if( i_frame_size > 0 )
570 if( p_vdec->i_buffer < i_frame_size + 16 )
572 FREE( p_vdec->p_buffer );
573 p_vdec->p_buffer = malloc( i_frame_size + 16 );
574 p_vdec->i_buffer = i_frame_size + 16;
577 E_( GetPESData )( p_vdec->p_buffer, p_vdec->i_buffer, p_pes );
579 input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
580 } while( i_frame_size <= 0 );
583 i_status = avcodec_decode_video( p_vdec->p_context,
591 msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
593 p_vdec->i_frame_error++;
596 /* Update frame late count*/
597 /* I don't make statistic on decoding time */
598 if( p_vdec->pts <= mdate())
600 p_vdec->i_frame_late++;
604 p_vdec->i_frame_late = 0;
607 if( !b_gotpicture || avpicture.linesize[0] == 0 || !b_drawpicture )
612 if( !p_vdec->b_direct_rendering )
615 if( !ffmpeg_CheckVout( p_vdec->p_vout,
616 p_vdec->p_context->width,
617 p_vdec->p_context->height,
618 p_vdec->p_context->aspect_ratio_info,
619 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
621 p_vdec->p_vout = ffmpeg_CreateVout( p_vdec,
622 p_vdec->p_context->width,
623 p_vdec->p_context->height,
624 p_vdec->p_context->aspect_ratio_info,
625 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt) );
626 if( !p_vdec->p_vout )
628 msg_Err( p_vdec->p_fifo, "cannot create vout" );
629 p_vdec->p_fifo->b_error = 1; /* abort */
634 /* Get a new picture */
635 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
637 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
641 msleep( VOUT_OUTMEM_SLEEP );
644 /* fill p_picture_t from avpicture, do I410->I420 if needed */
645 ffmpeg_CopyPicture( p_pic, &avpicture, p_vdec );
649 #if LIBAVCODEC_BUILD > 4615
650 p_pic = (picture_t *)p_vdec->p_context->dr_opaque_frame;
654 /* Do post-processing if requested */
655 ffmpeg_PostProcPicture( p_vdec, p_pic );
657 /* FIXME correct avi and use i_dts */
659 /* Send decoded frame to vout */
660 vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->pts);
661 vout_DisplayPicture( p_vdec->p_vout, p_pic );
666 /*****************************************************************************
667 * EndThread: thread destruction
668 *****************************************************************************
669 * This function is called when the thread ends after a sucessful
671 *****************************************************************************/
672 void E_( EndThread_Video )( vdec_thread_t *p_vdec )
674 if( p_vdec->p_secondlastpic )
675 vout_UnlinkPicture( p_vdec->p_vout, p_vdec->p_secondlastpic );
676 if( p_vdec->p_lastpic )
677 vout_UnlinkPicture( p_vdec->p_vout, p_vdec->p_lastpic );
681 /* release postprocessing module */
682 module_Unneed( p_vdec->p_pp, p_vdec->p_pp->p_module );
683 vlc_object_destroy( p_vdec->p_pp );
687 if( p_vdec->p_vout != NULL )
689 /* We are about to die. Reattach video output to p_vlc. */
690 vlc_object_detach( p_vdec->p_vout );
691 vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
695 /*****************************************************************************
696 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
697 * picture_t structure (when not in direct rendering mode).
698 *****************************************************************************/
699 static void ffmpeg_CopyPicture( picture_t *p_pic, AVPicture *p_avpicture,
700 vdec_thread_t *p_vdec )
711 if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
713 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
715 p_src = p_avpicture->data[i_plane];
716 p_dst = p_pic->p[i_plane].p_pixels;
717 i_src_stride = p_avpicture->linesize[i_plane];
718 i_dst_stride = p_pic->p[i_plane].i_pitch;
720 i_size = __MIN( i_src_stride, i_dst_stride );
721 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
723 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
724 p_src += i_src_stride;
725 p_dst += i_dst_stride;
731 /* we need to convert to I420 */
732 switch( p_vdec->p_context->pix_fmt )
734 #if LIBAVCODEC_BUILD >= 4615
735 case( PIX_FMT_YUV410P ):
736 ffmpeg_ConvertPictureI410toI420( p_pic, p_avpicture, p_vdec );
740 p_vdec->p_fifo->b_error = 1;
746 /*****************************************************************************
747 * ffmpeg_PostProcPicture: Postprocessing is done here.
748 *****************************************************************************/
749 static void ffmpeg_PostProcPicture( vdec_thread_t *p_vdec, picture_t *p_pic )
751 #if LIBAVCODEC_BUILD > 4313
752 if( ( p_vdec->i_pp_mode )&&
753 ( ( p_vdec->p_vout->render.i_chroma ==
754 VLC_FOURCC( 'I','4','2','0' ) )||
755 ( p_vdec->p_vout->render.i_chroma ==
756 VLC_FOURCC( 'Y','V','1','2' ) ) ) )
759 p_vdec->p_pp->pf_postprocess( p_pic,
760 p_vdec->p_context->display_qscale_table,
761 // p_vdec->p_context->current_qscale_table,
762 p_vdec->p_context->qstride,
768 /*****************************************************************************
769 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
770 * (used for direct rendering)
771 *****************************************************************************/
772 static int ffmpeg_GetFrameBuf( struct AVCodecContext *avctx, int width,
773 int height, int pict_type )
775 #if LIBAVCODEC_BUILD > 4615
776 vdec_thread_t *p_vdec = (vdec_thread_t *)avctx->opaque;
780 if( !ffmpeg_CheckVout( p_vdec->p_vout,
781 p_vdec->p_context->width,
782 p_vdec->p_context->height,
783 p_vdec->p_context->aspect_ratio_info,
784 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
786 p_vdec->p_vout = ffmpeg_CreateVout( p_vdec,
787 p_vdec->p_context->width,
788 p_vdec->p_context->height,
789 p_vdec->p_context->aspect_ratio_info,
790 ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt) );
791 if( !p_vdec->p_vout )
793 msg_Err( p_vdec->p_fifo, "cannot create vout" );
794 p_vdec->p_fifo->b_error = 1; /* abort */
799 /* Get a new picture */
800 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
802 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
806 msleep( VOUT_OUTMEM_SLEEP );
809 /* FIXME: We keep the last picture linked until the current one is decoded,
810 * this trick won't work with streams with B frames though. */
811 vout_LinkPicture( p_vdec->p_vout, p_pic );
812 if( p_vdec->p_secondlastpic )
813 vout_UnlinkPicture( p_vdec->p_vout, p_vdec->p_secondlastpic );
814 p_vdec->p_secondlastpic = p_vdec->p_lastpic;
815 p_vdec->p_lastpic = p_pic;
817 avctx->draw_horiz_band= NULL;
818 avctx->dr_buffer[0]= p_pic->p[0].p_pixels;
819 avctx->dr_buffer[1]= p_pic->p[1].p_pixels;
820 avctx->dr_buffer[2]= p_pic->p[2].p_pixels;
822 avctx->dr_stride = p_pic->p[0].i_pitch;
823 avctx->dr_uvstride = p_pic->p[1].i_pitch;
825 avctx->dr_opaque_frame = p_pic;
827 /* This variable is used to determine if a macro-block to be written
828 * can be skipped. The idea behind this is that if a macro-block hasn't
829 * changed and all the frame-buffers already have the value of this
830 * macro-block, then we don't need to write it again. */
831 avctx->dr_ip_buffer_count = p_vdec->p_vout->render.i_pictures;
832 p_vdec->p_vout->render.b_allow_modify_pics = 0;