1 /*****************************************************************************
2 * video.c: video decoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video.c,v 1.55 2003/11/29 18:06:12 fenrir 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 *****************************************************************************/
29 #include <vlc/decoder.h>
32 #ifdef HAVE_FFMPEG_AVCODEC_H
33 # include <ffmpeg/avcodec.h>
40 /*****************************************************************************
41 * decoder_sys_t : decoder descriptor
42 *****************************************************************************/
45 /* Common part between video and audio decoder */
50 AVCodecContext *p_context;
53 /* Video decoder specific part */
59 BITMAPINFOHEADER *p_format;
61 /* for frame skipping algo */
65 /* how many decoded frames are late */
67 mtime_t i_late_frames_start;
69 /* for direct rendering */
70 int b_direct_rendering;
72 vlc_bool_t b_has_b_frames;
74 int i_buffer_orig, i_buffer;
75 char *p_buffer_orig, *p_buffer;
77 /* Postprocessing handle */
80 vlc_bool_t b_pp_async;
84 /*****************************************************************************
86 *****************************************************************************/
87 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
88 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
89 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
91 /*****************************************************************************
93 *****************************************************************************/
94 static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
99 return VLC_FOURCC('I','4','2','0');
100 case PIX_FMT_YUV422P:
101 return VLC_FOURCC('I','4','2','2');
102 case PIX_FMT_YUV444P:
103 return VLC_FOURCC('I','4','4','4');
106 return VLC_FOURCC('Y','U','Y','2');
109 return VLC_FOURCC('R','V','1','5');
111 return VLC_FOURCC('R','V','1','6');
113 return VLC_FOURCC('R','V','2','4');
115 return VLC_FOURCC('R','V','3','2');
117 return VLC_FOURCC('G','R','E','Y');
119 case PIX_FMT_YUV410P:
120 case PIX_FMT_YUV411P:
127 /* Returns a new picture buffer */
128 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
129 AVCodecContext *p_context )
131 decoder_sys_t *p_sys = p_dec->p_sys;
134 p_dec->fmt_out.video.i_width = p_context->width;
135 p_dec->fmt_out.video.i_height = p_context->height;
136 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
138 if( !p_context->width || !p_context->height )
140 return NULL; /* invalid display size */
143 if( !p_dec->fmt_out.i_codec )
145 /* we make conversion if possible*/
146 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
149 #if LIBAVCODEC_BUILD >= 4687
150 p_dec->fmt_out.video.i_aspect =
151 VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
152 p_context->width / p_context->height );
154 p_dec->fmt_out.video.i_aspect =
155 VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
157 if( p_dec->fmt_out.video.i_aspect == 0 )
159 p_dec->fmt_out.video.i_aspect =
160 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
163 p_pic = p_dec->pf_vout_buffer_new( p_dec );
166 if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
168 E_(InitPostproc)( p_dec, p_sys->p_pp, p_context->width,
169 p_context->height, p_context->pix_fmt );
170 p_sys->b_pp_init = VLC_TRUE;
177 /*****************************************************************************
178 * InitVideo: initialize the video decoder
179 *****************************************************************************
180 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
181 * opened (done after the first decoded frame).
182 *****************************************************************************/
183 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
184 AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
186 decoder_sys_t *p_sys;
192 var_Get( p_dec->p_libvlc, "avcodec", &lockval );
194 /* Allocate the memory needed to store the decoder's structure */
195 if( ( p_dec->p_sys = p_sys =
196 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
198 msg_Err( p_dec, "out of memory" );
202 p_dec->p_sys->p_context = p_context;
203 p_dec->p_sys->p_codec = p_codec;
204 p_dec->p_sys->i_codec_id = i_codec_id;
205 p_dec->p_sys->psz_namecodec = psz_namecodec;
206 p_sys->p_ff_pic = avcodec_alloc_frame();
208 /* ***** Fill p_context with init values ***** */
209 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
210 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
212 /* ***** Get configuration of ffmpeg plugin ***** */
213 i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
214 p_sys->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
216 i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
217 p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
219 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
220 var_Get( p_dec, "grayscale", &val );
221 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
223 /* Decide if we set CODEC_FLAG_TRUNCATED */
224 var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
225 var_Get( p_dec, "ffmpeg-truncated", &val );
226 if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
228 /* ***** ffmpeg frame skipping ***** */
229 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
230 var_Get( p_dec, "ffmpeg-hurry-up", &val );
231 p_sys->b_hurry_up = val.b_bool;
233 /* ***** ffmpeg direct rendering ***** */
234 p_sys->b_direct_rendering = 0;
235 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
236 var_Get( p_dec, "ffmpeg-dr", &val );
237 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
238 ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
239 /* Apparently direct rendering doesn't work with YUV422P */
240 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
241 !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
243 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
244 * so we need to do another check in ffmpeg_GetFrameBuf() */
245 p_sys->b_direct_rendering = 1;
250 p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
251 p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
254 /* ffmpeg doesn't properly release old pictures when frames are skipped */
255 //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
256 if( p_sys->b_direct_rendering )
258 msg_Dbg( p_dec, "using direct rendering" );
259 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
262 /* Always use our get_buffer wrapper so we can calculate the
264 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
265 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
266 p_sys->p_context->opaque = p_dec;
268 /* ***** init this codec with special data ***** */
269 if( p_dec->fmt_in.i_extra )
271 int i_size = p_dec->fmt_in.i_extra;
273 if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
277 p_sys->p_context->extradata_size = i_size + 12;
278 p = p_sys->p_context->extradata =
279 malloc( p_sys->p_context->extradata_size );
281 memcpy( &p[0], "SVQ3", 4 );
282 memset( &p[4], 0, 8 );
283 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
287 p_sys->p_context->extradata_size = i_size;
288 p_sys->p_context->extradata =
289 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
290 memcpy( p_sys->p_context->extradata,
291 p_dec->fmt_in.p_extra, i_size );
292 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
293 0, FF_INPUT_BUFFER_PADDING_SIZE );
297 /* ***** misc init ***** */
298 p_sys->input_pts = p_sys->input_dts = 0;
300 p_sys->b_has_b_frames = VLC_FALSE;
301 p_sys->i_late_frames = 0;
303 p_sys->i_buffer_orig = 1;
304 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );
306 /* Set output properties */
307 p_dec->fmt_out.i_cat = VIDEO_ES;
308 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
310 /* ***** Open the codec ***** */
311 vlc_mutex_lock( lockval.p_address );
312 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
314 vlc_mutex_unlock( lockval.p_address );
315 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
318 vlc_mutex_unlock( lockval.p_address );
319 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
325 /*****************************************************************************
326 * DecodeVideo: Called to decode one or more frames
327 *****************************************************************************/
328 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
330 decoder_sys_t *p_sys = p_dec->p_sys;
334 if( !pp_block || !*pp_block ) return NULL;
338 if( p_block->b_discontinuity )
341 p_sys->i_pts = 0; /* To make sure we recover properly */
343 block_Release( p_block );
347 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
349 p_sys->input_pts = p_block->i_pts;
350 p_sys->input_dts = p_block->i_dts;
352 /* Make sure we don't reuse the same timestamps twice */
353 p_block->i_pts = p_block->i_dts = 0;
356 /* TODO implement it in a better way */
357 /* A good idea could be to decode all I pictures and see for the other */
358 if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
361 if( p_sys->i_late_frames < 8 )
363 p_sys->p_context->hurry_up = 2;
367 /* picture too late, won't decode
368 * but break picture until a new I, and for mpeg4 ...*/
370 p_sys->i_late_frames--; /* needed else it will never be decrease */
371 block_Release( p_block );
379 p_sys->p_context->hurry_up = 0;
382 if( p_sys->i_late_frames > 0 &&
383 mdate() - p_sys->i_late_frames_start > I64C(5000000) )
385 msg_Err( p_dec, "more than 5 seconds of late video -> "
386 "dropping frame (computer too slow ?)" );
387 block_Release( p_block );
388 p_sys->i_pts = 0; /* To make sure we recover properly */
389 p_sys->i_late_frames--;
393 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
395 p_sys->p_context->hurry_up = 5;
399 * Do the actual decoding now
402 /* Check if post-processing was enabled */
403 p_sys->b_pp = p_sys->b_pp_async;
405 /* Don't forget that ffmpeg requires a little more bytes
406 * that the real frame size */
407 if( p_block->i_buffer > 0 )
409 p_sys->i_buffer = p_block->i_buffer;
410 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
411 p_sys->i_buffer_orig )
413 free( p_sys->p_buffer_orig );
414 p_sys->i_buffer_orig =
415 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
416 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
418 p_sys->p_buffer = p_sys->p_buffer_orig;
419 p_sys->i_buffer = p_block->i_buffer;
420 p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
422 memset( p_sys->p_buffer + p_block->i_buffer, 0,
423 FF_INPUT_BUFFER_PADDING_SIZE );
425 p_block->i_buffer = 0;
428 while( p_sys->i_buffer > 0 )
430 int i_used, b_gotpicture;
433 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
435 p_sys->p_buffer, p_sys->i_buffer );
438 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
440 block_Release( p_block );
443 else if( i_used > p_sys->i_buffer )
445 i_used = p_sys->i_buffer;
449 p_sys->i_buffer -= i_used;
450 p_sys->p_buffer += i_used;
452 /* Nothing to display */
453 if( !b_gotpicture ) continue;
455 /* Update frame late count*/
456 if( p_sys->i_pts && p_sys->i_pts <= mdate() )
458 p_sys->i_late_frames++;
459 if( p_sys->i_late_frames == 1 )
460 p_sys->i_late_frames_start = mdate();
464 p_sys->i_late_frames = 0;
467 if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
469 /* Do not display the picture */
473 if( !p_sys->b_direct_rendering || p_sys->b_pp )
475 /* Get a new picture */
476 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
479 block_Release( p_block );
483 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
485 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
489 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
493 if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
495 /* Sanity check (seems to be needed for some streams ) */
496 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
498 p_sys->b_has_b_frames = VLC_TRUE;
501 /* Send decoded frame to vout */
504 p_pic->date = p_sys->i_pts;
506 /* interpolate the next PTS */
507 if( p_sys->p_context->frame_rate > 0 )
509 p_sys->i_pts += I64C(1000000) *
510 (2 + p_sys->p_ff_pic->repeat_pict) *
511 p_sys->p_context->frame_rate_base /
512 (2 * p_sys->p_context->frame_rate);
518 p_dec->pf_vout_buffer_del( p_dec, p_pic );
522 block_Release( p_block );
526 /*****************************************************************************
527 * EndVideo: decoder destruction
528 *****************************************************************************
529 * This function is called when the thread ends after a sucessful
531 *****************************************************************************/
532 void E_(EndVideoDec)( decoder_t *p_dec )
534 decoder_sys_t *p_sys = p_dec->p_sys;
536 if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
539 E_(ClosePostproc)( p_dec, p_sys->p_pp );
542 free( p_sys->p_buffer_orig );
545 /*****************************************************************************
546 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
547 * picture_t structure (when not in direct rendering mode).
548 *****************************************************************************/
549 static void ffmpeg_CopyPicture( decoder_t *p_dec,
550 picture_t *p_pic, AVFrame *p_ff_pic )
552 decoder_sys_t *p_sys = p_dec->p_sys;
554 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
556 int i_plane, i_size, i_line;
557 uint8_t *p_dst, *p_src;
558 int i_src_stride, i_dst_stride;
561 if( p_sys->p_pp && p_sys->b_pp )
562 E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
565 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
567 p_src = p_ff_pic->data[i_plane];
568 p_dst = p_pic->p[i_plane].p_pixels;
569 i_src_stride = p_ff_pic->linesize[i_plane];
570 i_dst_stride = p_pic->p[i_plane].i_pitch;
572 i_size = __MIN( i_src_stride, i_dst_stride );
573 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
575 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
576 p_src += i_src_stride;
577 p_dst += i_dst_stride;
586 /* we need to convert to I420 */
587 switch( p_sys->p_context->pix_fmt )
589 case( PIX_FMT_YUV410P ):
590 case( PIX_FMT_YUV411P ):
591 for( i = 0; i < p_pic->i_planes; i++ )
593 dest_pic.data[i] = p_pic->p[i].p_pixels;
594 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
596 img_convert( &dest_pic, PIX_FMT_YUV420P,
597 (AVPicture *)p_ff_pic,
598 p_sys->p_context->pix_fmt,
599 p_sys->p_context->width,
600 p_sys->p_context->height );
603 msg_Err( p_dec, "don't know how to convert chroma %i",
604 p_sys->p_context->pix_fmt );
611 /*****************************************************************************
612 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
613 *****************************************************************************
614 * It is used for direct rendering as well as to get the right PTS for each
615 * decoded picture (even in indirect rendering mode).
616 *****************************************************************************/
617 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
620 decoder_t *p_dec = (decoder_t *)p_context->opaque;
621 decoder_sys_t *p_sys = p_dec->p_sys;
624 /* Set picture PTS */
625 if( p_sys->input_pts )
627 p_ff_pic->pts = p_sys->input_pts;
629 else if( p_sys->input_dts )
631 /* Some demuxers only set the dts so let's try to find a useful
632 * timestamp from this */
633 if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
634 !p_ff_pic->reference )
636 p_ff_pic->pts = p_sys->input_dts;
638 else p_ff_pic->pts = 0;
640 else p_ff_pic->pts = 0;
642 p_sys->input_pts = p_sys->input_dts = 0;
644 /* Not much to do in indirect rendering mode */
645 if( !p_sys->b_direct_rendering || p_sys->b_pp )
647 return avcodec_default_get_buffer( p_context, p_ff_pic );
650 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
651 * so this check is necessary. */
652 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
653 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
655 msg_Dbg( p_dec, "disabling direct rendering" );
656 p_sys->b_direct_rendering = 0;
657 return avcodec_default_get_buffer( p_context, p_ff_pic );
660 /* Get a new picture */
661 //p_sys->p_vout->render.b_allow_modify_pics = 0;
662 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
665 p_sys->b_direct_rendering = 0;
666 return avcodec_default_get_buffer( p_context, p_ff_pic );
668 p_sys->p_context->draw_horiz_band = NULL;
670 p_ff_pic->opaque = (void*)p_pic;
671 p_ff_pic->type = FF_BUFFER_TYPE_USER;
672 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
673 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
674 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
675 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
677 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
678 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
679 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
680 p_ff_pic->linesize[3] = 0;
682 if( p_ff_pic->reference != 0 )
684 p_dec->pf_picture_link( p_dec, p_pic );
687 /* FIXME what is that, should give good value */
688 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
693 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
696 decoder_t *p_dec = (decoder_t *)p_context->opaque;
699 if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
701 avcodec_default_release_buffer( p_context, p_ff_pic );
705 p_pic = (picture_t*)p_ff_pic->opaque;
707 p_ff_pic->data[0] = NULL;
708 p_ff_pic->data[1] = NULL;
709 p_ff_pic->data[2] = NULL;
710 p_ff_pic->data[3] = NULL;
712 if( p_ff_pic->reference != 0 )
714 p_dec->pf_picture_unlink( p_dec, p_pic );