1 /*****************************************************************************
2 * video.c: video decoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video.c,v 1.43 2003/10/28 14:17:52 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>
36 #ifdef HAVE_SYS_TIMES_H
37 # include <sys/times.h>
41 #ifdef HAVE_FFMPEG_AVCODEC_H
42 # include <ffmpeg/avcodec.h>
49 /*****************************************************************************
50 * decoder_sys_t : decoder descriptor
51 *****************************************************************************/
54 /* Common part between video and audio decoder */
59 AVCodecContext *p_context;
62 /* Video decoder specific part */
67 BITMAPINFOHEADER *p_format;
69 vout_thread_t *p_vout;
71 /* for frame skipping algo */
76 /* how many decoded frames are late */
78 mtime_t i_late_frames_start;
80 /* for direct rendering */
81 int b_direct_rendering;
83 vlc_bool_t b_has_b_frames;
88 /* Postprocessing handle */
92 /*****************************************************************************
94 *****************************************************************************/
95 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
96 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
97 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
99 /*****************************************************************************
101 *****************************************************************************/
102 static inline uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
104 /* FIXME FIXME some of them are wrong */
105 switch( i_ff_chroma )
107 case PIX_FMT_YUV420P:
109 return( VLC_FOURCC('I','4','2','0') );
111 return( VLC_FOURCC('R','V','2','4') );
112 case PIX_FMT_YUV422P:
113 return( VLC_FOURCC('I','4','2','2') );
114 case PIX_FMT_YUV444P:
115 return( VLC_FOURCC('I','4','4','4') );
116 case PIX_FMT_YUV410P:
117 case PIX_FMT_YUV411P:
125 static vout_thread_t *ffmpeg_CreateVout( decoder_t *p_dec,
126 AVCodecContext *p_context )
128 vout_thread_t *p_vout;
129 unsigned int i_width = p_context->width;
130 unsigned int i_height = p_context->height;
131 uint32_t i_chroma = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
132 unsigned int i_aspect;
134 if( !i_width || !i_height )
136 return( NULL ); /* Can't create a new vout without display size */
141 /* we make conversion if possible*/
142 i_chroma = VLC_FOURCC('I','4','2','0');
145 #if LIBAVCODEC_BUILD >= 4687
146 i_aspect = VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
147 p_context->width / p_context->height );
149 i_aspect = VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
153 i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
156 /* Spawn a video output if there is none. First we look for our children,
157 * then we look for any other vout that might be available. */
158 p_vout = vout_Request( p_dec, p_dec->p_sys->p_vout,
159 i_width, i_height, i_chroma, i_aspect );
162 if( p_dec->p_sys->p_pp )
163 E_(InitPostproc)( p_dec, p_dec->p_sys->p_pp, i_width, i_height,
164 p_context->pix_fmt );
170 /*****************************************************************************
171 * InitVideo: initialize the video decoder
172 *****************************************************************************
173 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
174 * opened (done after the first decoded frame).
175 *****************************************************************************/
176 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
177 AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
179 decoder_sys_t *p_sys;
183 /* Allocate the memory needed to store the decoder's structure */
184 if( ( p_dec->p_sys = p_sys =
185 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
187 msg_Err( p_dec, "out of memory" );
191 p_dec->p_sys->p_context = p_context;
192 p_dec->p_sys->p_codec = p_codec;
193 p_dec->p_sys->i_codec_id = i_codec_id;
194 p_dec->p_sys->psz_namecodec = psz_namecodec;
195 p_sys->p_ff_pic = avcodec_alloc_frame();
197 if( ( p_sys->p_format =
198 (BITMAPINFOHEADER *)p_dec->p_fifo->p_bitmapinfoheader ) != NULL )
200 /* ***** Fill p_context with init values ***** */
201 p_sys->p_context->width = p_sys->p_format->biWidth;
202 p_sys->p_context->height = p_sys->p_format->biHeight;
206 msg_Warn( p_dec, "display informations missing" );
207 p_sys->p_format = NULL;
210 /* ***** Get configuration of ffmpeg plugin ***** */
211 i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
212 p_sys->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
214 i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
215 p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
217 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
218 var_Get( p_dec, "grayscale", &val );
219 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
221 /* Decide if we set CODEC_FLAG_TRUNCATED */
222 #if LIBAVCODEC_BUILD >= 4662
223 var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
224 var_Get( p_dec, "ffmpeg-truncated", &val );
225 if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
228 /* ***** Open the codec ***** */
229 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
231 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
236 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
239 /* ***** ffmpeg frame skipping ***** */
240 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
241 var_Get( p_dec, "ffmpeg-hurry-up", &val );
242 p_sys->b_hurry_up = val.b_bool;
244 /* ***** ffmpeg direct rendering ***** */
245 p_sys->b_direct_rendering = 0;
246 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
247 var_Get( p_dec, "ffmpeg-dr", &val );
248 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
249 ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
250 /* Apparently direct rendering doesn't work with YUV422P */
251 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
252 !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
254 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
255 * so we need to do another check in ffmpeg_GetFrameBuf() */
256 p_sys->b_direct_rendering = 1;
261 if( E_(OpenPostproc)( p_dec, &p_sys->p_pp ) == VLC_SUCCESS )
263 /* for now we cannot do postproc and dr */
264 p_sys->b_direct_rendering = 0;
268 /* ffmpeg doesn't properly release old pictures when frames are skipped */
269 if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
270 if( p_sys->b_direct_rendering )
272 msg_Dbg( p_dec, "using direct rendering" );
273 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
276 /* Always use our get_buffer wrapper so we can calculate the
278 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
279 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
280 p_sys->p_context->opaque = p_dec;
282 /* ***** init this codec with special data ***** */
283 if( p_sys->p_format && p_sys->p_format->biSize > sizeof(BITMAPINFOHEADER) )
286 int i_size = p_sys->p_format->biSize - sizeof(BITMAPINFOHEADER);
288 if( p_sys->i_codec_id == CODEC_ID_MPEG4 )
290 uint8_t *p_vol = malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
292 memcpy( p_vol, &p_sys->p_format[1], i_size );
293 memset( &p_vol[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
295 avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
296 &b_gotpicture, p_vol, i_size );
299 #if LIBAVCODEC_BUILD >= 4666
300 else if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
304 p_sys->p_context->extradata_size = i_size + 12;
305 p = p_sys->p_context->extradata =
306 malloc( p_sys->p_context->extradata_size );
308 memcpy( &p[0], "SVQ3", 4 );
309 memset( &p[4], 0, 8 );
310 memcpy( &p[12], &p_sys->p_format[1], i_size );
315 p_sys->p_context->extradata_size = i_size;
316 p_sys->p_context->extradata =
317 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
318 memcpy( p_sys->p_context->extradata,
319 &p_sys->p_format[1], i_size );
320 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
321 0, FF_INPUT_BUFFER_PADDING_SIZE );
325 /* ***** misc init ***** */
326 p_sys->p_vout = NULL;
327 p_sys->input_pts = 0;
329 p_sys->b_has_b_frames = VLC_FALSE;
330 p_sys->i_late_frames = 0;
332 p_sys->p_buffer = malloc( p_sys->i_buffer );
337 /*****************************************************************************
338 * DecodeVideo: Called to decode one or more frames
339 *****************************************************************************/
340 int E_(DecodeVideo)( decoder_t *p_dec, block_t *p_block )
342 decoder_sys_t *p_sys = p_dec->p_sys;
343 int i_buffer, b_drawpicture;
346 if( p_block->i_pts > 0 )
348 p_sys->input_pts = p_block->i_pts;
351 /* TODO implement it in a better way */
352 /* A good idea could be to decode all I pictures and see for the other */
353 if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
356 if( p_sys->i_late_frames < 8 )
358 p_sys->p_context->hurry_up = 2;
362 /* picture too late, won't decode
363 * but break picture until a new I, and for mpeg4 ...*/
365 p_sys->i_late_frames--; /* needed else it will never be decrease */
366 block_Release( p_block );
373 p_sys->p_context->hurry_up = 0;
376 if( p_sys->i_late_frames > 0 &&
377 mdate() - p_sys->i_late_frames_start > I64C(5000000) )
379 msg_Err( p_dec, "more than 5 seconds of late video -> "
380 "dropping frame (computer too slow ?)" );
381 block_Release( p_block );
382 p_sys->i_pts = 0; /* To make sure we recover properly */
383 p_sys->i_late_frames--;
387 if( !p_sys->p_context->width || !p_sys->p_context->height )
389 p_sys->p_context->hurry_up = 5;
393 * Do the actual decoding now
396 /* Don't forget that ffmpeg requires a little more bytes
397 * that the real frame size */
398 i_buffer = p_block->i_buffer;
399 if( i_buffer + FF_INPUT_BUFFER_PADDING_SIZE > p_sys->i_buffer )
401 free( p_sys->p_buffer );
402 p_sys->i_buffer = i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
403 p_sys->p_buffer = malloc( p_sys->i_buffer );
405 p_buffer = p_sys->p_buffer;
406 p_dec->p_vlc->pf_memcpy( p_buffer, p_block->p_buffer, p_block->i_buffer );
407 memset( p_buffer + i_buffer, 0, FF_INPUT_BUFFER_PADDING_SIZE );
411 int i_used, b_gotpicture;
414 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
416 p_buffer, i_buffer );
419 msg_Warn( p_dec, "cannot decode one frame (%d bytes)", i_buffer );
420 p_sys->i_frame_error++;
421 block_Release( p_block );
429 /* Nothing to display */
430 if( !b_gotpicture ) continue;
432 /* Update frame late count*/
433 if( p_sys->i_pts && p_sys->i_pts <= mdate() )
435 p_sys->i_late_frames++;
436 if( p_sys->i_late_frames == 1 )
437 p_sys->i_late_frames_start = mdate();
441 p_sys->i_late_frames = 0;
444 if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
446 /* Do not display the picture */
450 if( !p_sys->b_direct_rendering )
452 p_sys->p_vout = ffmpeg_CreateVout( p_dec, p_sys->p_context );
455 msg_Err( p_dec, "cannot create vout" );
456 block_Release( p_block );
460 /* Get a new picture */
461 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
463 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
465 block_Release( p_block );
468 msleep( VOUT_OUTMEM_SLEEP );
471 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
473 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
477 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
481 * There is an ugly hack here because some demuxers pass us a dts
482 * instead of a pts so this screw up things for streams with
484 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
485 p_sys->b_has_b_frames = VLC_TRUE;
486 if( p_sys->p_ff_pic->pts &&
487 ( !p_sys->p_context->has_b_frames || !p_sys->b_has_b_frames ||
488 p_sys->p_ff_pic->pict_type == FF_B_TYPE ) )
490 p_sys->i_pts = p_sys->p_ff_pic->pts;
493 /* Send decoded frame to vout */
496 vout_DatePicture( p_sys->p_vout, p_pic, p_sys->i_pts );
497 vout_DisplayPicture( p_sys->p_vout, p_pic );
499 /* interpolate the next PTS */
500 if( p_sys->p_context->frame_rate > 0 )
502 p_sys->i_pts += I64C(1000000) *
503 (2 + p_sys->p_ff_pic->repeat_pict) *
504 p_sys->p_context->frame_rate_base /
505 (2 * p_sys->p_context->frame_rate);
510 block_Release( p_block );
514 /*****************************************************************************
515 * EndVideo: decoder destruction
516 *****************************************************************************
517 * This function is called when the thread ends after a sucessful
519 *****************************************************************************/
520 void E_(EndVideoDec)( decoder_t *p_dec )
522 decoder_sys_t *p_sys = p_dec->p_sys;
524 if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
527 E_(ClosePostproc)( p_dec, p_sys->p_pp );
530 free( p_sys->p_buffer );
532 /* We are about to die. Reattach video output to p_vlc. */
533 vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
536 /*****************************************************************************
537 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
538 * picture_t structure (when not in direct rendering mode).
539 *****************************************************************************/
540 static void ffmpeg_CopyPicture( decoder_t *p_dec,
541 picture_t *p_pic, AVFrame *p_ff_pic )
543 decoder_sys_t *p_sys = p_dec->p_sys;
545 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
547 int i_plane, i_size, i_line;
548 uint8_t *p_dst, *p_src;
549 int i_src_stride, i_dst_stride;
553 E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
556 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
558 p_src = p_ff_pic->data[i_plane];
559 p_dst = p_pic->p[i_plane].p_pixels;
560 i_src_stride = p_ff_pic->linesize[i_plane];
561 i_dst_stride = p_pic->p[i_plane].i_pitch;
563 i_size = __MIN( i_src_stride, i_dst_stride );
564 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
566 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
567 p_src += i_src_stride;
568 p_dst += i_dst_stride;
577 /* we need to convert to I420 */
578 switch( p_sys->p_context->pix_fmt )
580 case( PIX_FMT_YUV410P ):
581 case( PIX_FMT_YUV411P ):
582 for( i = 0; i < p_pic->i_planes; i++ )
584 dest_pic.data[i] = p_pic->p[i].p_pixels;
585 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
587 img_convert( &dest_pic, PIX_FMT_YUV420P,
588 (AVPicture *)p_ff_pic,
589 p_sys->p_context->pix_fmt,
590 p_sys->p_context->width,
591 p_sys->p_context->height );
594 msg_Err( p_dec, "don't know how to convert chroma %i",
595 p_sys->p_context->pix_fmt );
596 p_dec->p_fifo->b_error = 1;
602 /*****************************************************************************
603 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
604 *****************************************************************************
605 * It is used for direct rendering as well as to get the right PTS for each
606 * decoded picture (even in indirect rendering mode).
607 *****************************************************************************/
608 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
611 decoder_t *p_dec = (decoder_t *)p_context->opaque;
612 decoder_sys_t *p_sys = p_dec->p_sys;
615 /* Set picture PTS */
616 p_ff_pic->pts = p_sys->input_pts;
617 p_sys->input_pts = 0;
619 /* Not much to do in indirect rendering mode */
620 if( !p_sys->b_direct_rendering )
622 return avcodec_default_get_buffer( p_context, p_ff_pic );
625 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
626 * so this check is necessary. */
627 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
628 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
630 msg_Dbg( p_dec, "disabling direct rendering" );
631 p_sys->b_direct_rendering = 0;
632 return avcodec_default_get_buffer( p_context, p_ff_pic );
635 /* Check and (re)create our vout if needed */
636 p_sys->p_vout = ffmpeg_CreateVout( p_dec, p_sys->p_context );
639 msg_Err( p_dec, "cannot create vout" );
640 p_dec->p_fifo->b_error = 1; /* abort */
641 p_sys->b_direct_rendering = 0;
642 return avcodec_default_get_buffer( p_context, p_ff_pic );
645 p_sys->p_vout->render.b_allow_modify_pics = 0;
647 /* Get a new picture */
648 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
650 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
652 p_sys->b_direct_rendering = 0;
653 return avcodec_default_get_buffer( p_context, p_ff_pic );
655 msleep( VOUT_OUTMEM_SLEEP );
657 p_sys->p_context->draw_horiz_band = NULL;
659 p_ff_pic->opaque = (void*)p_pic;
660 p_ff_pic->type = FF_BUFFER_TYPE_USER;
661 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
662 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
663 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
664 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
666 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
667 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
668 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
669 p_ff_pic->linesize[3] = 0;
671 if( p_ff_pic->reference != 0 )
673 vout_LinkPicture( p_sys->p_vout, p_pic );
676 /* FIXME what is that, should give good value */
677 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
682 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
685 decoder_t *p_dec = (decoder_t *)p_context->opaque;
686 decoder_sys_t *p_sys = p_dec->p_sys;
689 if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
691 avcodec_default_release_buffer( p_context, p_ff_pic );
695 p_pic = (picture_t*)p_ff_pic->opaque;
697 p_ff_pic->data[0] = NULL;
698 p_ff_pic->data[1] = NULL;
699 p_ff_pic->data[2] = NULL;
700 p_ff_pic->data[3] = NULL;
702 if( p_ff_pic->reference != 0 )
704 vout_UnlinkPicture( p_sys->p_vout, p_pic );