1 /*****************************************************************************
2 * video.c: video decoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video.c,v 1.52 2003/11/24 00:39:01 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 #if LIBAVCODEC_BUILD >= 4662
225 var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
226 var_Get( p_dec, "ffmpeg-truncated", &val );
227 if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
230 /* ***** Open the codec ***** */
231 vlc_mutex_lock( lockval.p_address );
232 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
234 vlc_mutex_unlock( lockval.p_address );
235 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
238 vlc_mutex_unlock( lockval.p_address );
239 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
241 /* ***** ffmpeg frame skipping ***** */
242 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
243 var_Get( p_dec, "ffmpeg-hurry-up", &val );
244 p_sys->b_hurry_up = val.b_bool;
246 /* ***** ffmpeg direct rendering ***** */
247 p_sys->b_direct_rendering = 0;
248 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
249 var_Get( p_dec, "ffmpeg-dr", &val );
250 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
251 ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
252 /* Apparently direct rendering doesn't work with YUV422P */
253 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
254 !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
256 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
257 * so we need to do another check in ffmpeg_GetFrameBuf() */
258 p_sys->b_direct_rendering = 1;
263 p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
264 p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
267 /* ffmpeg doesn't properly release old pictures when frames are skipped */
268 if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
269 if( p_sys->b_direct_rendering )
271 msg_Dbg( p_dec, "using direct rendering" );
272 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
275 /* Always use our get_buffer wrapper so we can calculate the
277 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
278 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
279 p_sys->p_context->opaque = p_dec;
281 /* ***** init this codec with special data ***** */
282 if( p_dec->fmt_in.i_extra )
285 int i_size = p_dec->fmt_in.i_extra;
287 if( p_sys->i_codec_id == CODEC_ID_MPEG4 )
289 uint8_t *p_vol = malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
290 memcpy( p_vol, p_dec->fmt_in.p_extra, i_size );
291 memset( &p_vol[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
293 avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
294 &b_gotpicture, p_vol, i_size );
297 #if LIBAVCODEC_BUILD >= 4666
298 else if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
302 p_sys->p_context->extradata_size = i_size + 12;
303 p = p_sys->p_context->extradata =
304 malloc( p_sys->p_context->extradata_size );
306 memcpy( &p[0], "SVQ3", 4 );
307 memset( &p[4], 0, 8 );
308 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
313 p_sys->p_context->extradata_size = i_size;
314 p_sys->p_context->extradata =
315 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
316 memcpy( p_sys->p_context->extradata,
317 p_dec->fmt_in.p_extra, i_size );
318 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
319 0, FF_INPUT_BUFFER_PADDING_SIZE );
323 /* ***** misc init ***** */
324 p_sys->input_pts = p_sys->input_dts = 0;
326 p_sys->b_has_b_frames = VLC_FALSE;
327 p_sys->i_late_frames = 0;
329 p_sys->i_buffer_orig = 1;
330 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );
332 /* Set output properties */
333 p_dec->fmt_out.i_cat = VIDEO_ES;
334 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
339 /*****************************************************************************
340 * DecodeVideo: Called to decode one or more frames
341 *****************************************************************************/
342 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
344 decoder_sys_t *p_sys = p_dec->p_sys;
348 if( !pp_block || !*pp_block ) return NULL;
352 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
354 p_sys->input_pts = p_block->i_pts;
355 p_sys->input_dts = p_block->i_dts;
357 /* Make sure we don't reuse the same timestamps twice */
358 p_block->i_pts = p_block->i_dts = 0;
361 /* TODO implement it in a better way */
362 /* A good idea could be to decode all I pictures and see for the other */
363 if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
366 if( p_sys->i_late_frames < 8 )
368 p_sys->p_context->hurry_up = 2;
372 /* picture too late, won't decode
373 * but break picture until a new I, and for mpeg4 ...*/
375 p_sys->i_late_frames--; /* needed else it will never be decrease */
376 block_Release( p_block );
384 p_sys->p_context->hurry_up = 0;
387 if( p_sys->i_late_frames > 0 &&
388 mdate() - p_sys->i_late_frames_start > I64C(5000000) )
390 msg_Err( p_dec, "more than 5 seconds of late video -> "
391 "dropping frame (computer too slow ?)" );
392 block_Release( p_block );
393 p_sys->i_pts = 0; /* To make sure we recover properly */
394 p_sys->i_late_frames--;
398 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
400 p_sys->p_context->hurry_up = 5;
404 * Do the actual decoding now
407 /* Check if post-processing was enabled */
408 p_sys->b_pp = p_sys->b_pp_async;
410 /* Don't forget that ffmpeg requires a little more bytes
411 * that the real frame size */
412 if( p_block->i_buffer > 0 )
414 p_sys->i_buffer = p_block->i_buffer;
415 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
416 p_sys->i_buffer_orig )
418 free( p_sys->p_buffer_orig );
419 p_sys->i_buffer_orig =
420 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
421 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
423 p_sys->p_buffer = p_sys->p_buffer_orig;
424 p_sys->i_buffer = p_block->i_buffer;
425 p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
427 memset( p_sys->p_buffer + p_block->i_buffer, 0,
428 FF_INPUT_BUFFER_PADDING_SIZE );
430 p_block->i_buffer = 0;
433 while( p_sys->i_buffer > 0 )
435 int i_used, b_gotpicture;
438 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
440 p_sys->p_buffer, p_sys->i_buffer );
443 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
445 block_Release( p_block );
448 else if( i_used > p_sys->i_buffer )
450 i_used = p_sys->i_buffer;
454 p_sys->i_buffer -= i_used;
455 p_sys->p_buffer += i_used;
457 /* Nothing to display */
458 if( !b_gotpicture ) continue;
460 /* Update frame late count*/
461 if( p_sys->i_pts && p_sys->i_pts <= mdate() )
463 p_sys->i_late_frames++;
464 if( p_sys->i_late_frames == 1 )
465 p_sys->i_late_frames_start = mdate();
469 p_sys->i_late_frames = 0;
472 if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
474 /* Do not display the picture */
478 if( !p_sys->b_direct_rendering || p_sys->b_pp )
480 /* Get a new picture */
481 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
484 block_Release( p_block );
488 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
490 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
494 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
498 if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
500 /* Sanity check (seems to be needed for some streams ) */
501 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
503 p_sys->b_has_b_frames = VLC_TRUE;
506 /* Send decoded frame to vout */
509 p_pic->date = p_sys->i_pts;
511 /* interpolate the next PTS */
512 if( p_sys->p_context->frame_rate > 0 )
514 p_sys->i_pts += I64C(1000000) *
515 (2 + p_sys->p_ff_pic->repeat_pict) *
516 p_sys->p_context->frame_rate_base /
517 (2 * p_sys->p_context->frame_rate);
523 p_dec->pf_vout_buffer_del( p_dec, p_pic );
527 block_Release( p_block );
531 /*****************************************************************************
532 * EndVideo: decoder destruction
533 *****************************************************************************
534 * This function is called when the thread ends after a sucessful
536 *****************************************************************************/
537 void E_(EndVideoDec)( decoder_t *p_dec )
539 decoder_sys_t *p_sys = p_dec->p_sys;
541 if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
544 E_(ClosePostproc)( p_dec, p_sys->p_pp );
547 free( p_sys->p_buffer_orig );
550 /*****************************************************************************
551 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
552 * picture_t structure (when not in direct rendering mode).
553 *****************************************************************************/
554 static void ffmpeg_CopyPicture( decoder_t *p_dec,
555 picture_t *p_pic, AVFrame *p_ff_pic )
557 decoder_sys_t *p_sys = p_dec->p_sys;
559 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
561 int i_plane, i_size, i_line;
562 uint8_t *p_dst, *p_src;
563 int i_src_stride, i_dst_stride;
566 if( p_sys->p_pp && p_sys->b_pp )
567 E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
570 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
572 p_src = p_ff_pic->data[i_plane];
573 p_dst = p_pic->p[i_plane].p_pixels;
574 i_src_stride = p_ff_pic->linesize[i_plane];
575 i_dst_stride = p_pic->p[i_plane].i_pitch;
577 i_size = __MIN( i_src_stride, i_dst_stride );
578 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
580 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
581 p_src += i_src_stride;
582 p_dst += i_dst_stride;
591 /* we need to convert to I420 */
592 switch( p_sys->p_context->pix_fmt )
594 case( PIX_FMT_YUV410P ):
595 case( PIX_FMT_YUV411P ):
596 for( i = 0; i < p_pic->i_planes; i++ )
598 dest_pic.data[i] = p_pic->p[i].p_pixels;
599 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
601 img_convert( &dest_pic, PIX_FMT_YUV420P,
602 (AVPicture *)p_ff_pic,
603 p_sys->p_context->pix_fmt,
604 p_sys->p_context->width,
605 p_sys->p_context->height );
608 msg_Err( p_dec, "don't know how to convert chroma %i",
609 p_sys->p_context->pix_fmt );
616 /*****************************************************************************
617 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
618 *****************************************************************************
619 * It is used for direct rendering as well as to get the right PTS for each
620 * decoded picture (even in indirect rendering mode).
621 *****************************************************************************/
622 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
625 decoder_t *p_dec = (decoder_t *)p_context->opaque;
626 decoder_sys_t *p_sys = p_dec->p_sys;
629 /* Set picture PTS */
630 if( p_sys->input_pts )
632 p_ff_pic->pts = p_sys->input_pts;
634 else if( p_sys->input_dts )
636 /* Some demuxers only set the dts so let's try to find a useful
637 * timestamp from this */
638 if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
639 !p_ff_pic->reference )
641 p_ff_pic->pts = p_sys->input_dts;
643 else p_ff_pic->pts = 0;
645 else p_ff_pic->pts = 0;
647 p_sys->input_pts = p_sys->input_dts = 0;
649 /* Not much to do in indirect rendering mode */
650 if( !p_sys->b_direct_rendering || p_sys->b_pp )
652 return avcodec_default_get_buffer( p_context, p_ff_pic );
655 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
656 * so this check is necessary. */
657 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
658 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
660 msg_Dbg( p_dec, "disabling direct rendering" );
661 p_sys->b_direct_rendering = 0;
662 return avcodec_default_get_buffer( p_context, p_ff_pic );
665 /* Get a new picture */
666 //p_sys->p_vout->render.b_allow_modify_pics = 0;
667 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
670 p_sys->b_direct_rendering = 0;
671 return avcodec_default_get_buffer( p_context, p_ff_pic );
673 p_sys->p_context->draw_horiz_band = NULL;
675 p_ff_pic->opaque = (void*)p_pic;
676 p_ff_pic->type = FF_BUFFER_TYPE_USER;
677 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
678 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
679 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
680 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
682 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
683 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
684 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
685 p_ff_pic->linesize[3] = 0;
687 if( p_ff_pic->reference != 0 )
689 //vout_LinkPicture( p_sys->p_vout, p_pic );
692 /* FIXME what is that, should give good value */
693 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
698 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
701 decoder_t *p_dec = (decoder_t *)p_context->opaque;
704 if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
706 avcodec_default_release_buffer( p_context, p_ff_pic );
710 p_pic = (picture_t*)p_ff_pic->opaque;
712 p_ff_pic->data[0] = NULL;
713 p_ff_pic->data[1] = NULL;
714 p_ff_pic->data[2] = NULL;
715 p_ff_pic->data[3] = NULL;
717 if( p_ff_pic->reference != 0 )
719 //vout_UnlinkPicture( p_sys->p_vout, p_pic );