1 /*****************************************************************************
2 * video.c: video decoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video.c,v 1.51 2003/11/23 20:37:04 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 *****************************************************************************/
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;
190 /* Allocate the memory needed to store the decoder's structure */
191 if( ( p_dec->p_sys = p_sys =
192 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
194 msg_Err( p_dec, "out of memory" );
198 p_dec->p_sys->p_context = p_context;
199 p_dec->p_sys->p_codec = p_codec;
200 p_dec->p_sys->i_codec_id = i_codec_id;
201 p_dec->p_sys->psz_namecodec = psz_namecodec;
202 p_sys->p_ff_pic = avcodec_alloc_frame();
204 /* ***** Fill p_context with init values ***** */
205 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
206 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
208 /* ***** Get configuration of ffmpeg plugin ***** */
209 i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
210 p_sys->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
212 i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
213 p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
215 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
216 var_Get( p_dec, "grayscale", &val );
217 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
219 /* Decide if we set CODEC_FLAG_TRUNCATED */
220 #if LIBAVCODEC_BUILD >= 4662
221 var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
222 var_Get( p_dec, "ffmpeg-truncated", &val );
223 if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
226 /* ***** Open the codec ***** */
227 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
229 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
234 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
237 /* ***** ffmpeg frame skipping ***** */
238 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
239 var_Get( p_dec, "ffmpeg-hurry-up", &val );
240 p_sys->b_hurry_up = val.b_bool;
242 /* ***** ffmpeg direct rendering ***** */
243 p_sys->b_direct_rendering = 0;
244 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
245 var_Get( p_dec, "ffmpeg-dr", &val );
246 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
247 ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
248 /* Apparently direct rendering doesn't work with YUV422P */
249 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
250 !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
252 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
253 * so we need to do another check in ffmpeg_GetFrameBuf() */
254 p_sys->b_direct_rendering = 1;
259 p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
260 p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
263 /* ffmpeg doesn't properly release old pictures when frames are skipped */
264 if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
265 if( p_sys->b_direct_rendering )
267 msg_Dbg( p_dec, "using direct rendering" );
268 p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
271 /* Always use our get_buffer wrapper so we can calculate the
273 p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
274 p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
275 p_sys->p_context->opaque = p_dec;
277 /* ***** init this codec with special data ***** */
278 if( p_dec->fmt_in.i_extra )
281 int i_size = p_dec->fmt_in.i_extra;
283 if( p_sys->i_codec_id == CODEC_ID_MPEG4 )
285 uint8_t *p_vol = malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
286 memcpy( p_vol, p_dec->fmt_in.p_extra, i_size );
287 memset( &p_vol[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
289 avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
290 &b_gotpicture, p_vol, i_size );
293 #if LIBAVCODEC_BUILD >= 4666
294 else if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
298 p_sys->p_context->extradata_size = i_size + 12;
299 p = p_sys->p_context->extradata =
300 malloc( p_sys->p_context->extradata_size );
302 memcpy( &p[0], "SVQ3", 4 );
303 memset( &p[4], 0, 8 );
304 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
309 p_sys->p_context->extradata_size = i_size;
310 p_sys->p_context->extradata =
311 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
312 memcpy( p_sys->p_context->extradata,
313 p_dec->fmt_in.p_extra, i_size );
314 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
315 0, FF_INPUT_BUFFER_PADDING_SIZE );
319 /* ***** misc init ***** */
320 p_sys->input_pts = p_sys->input_dts = 0;
322 p_sys->b_has_b_frames = VLC_FALSE;
323 p_sys->i_late_frames = 0;
325 p_sys->i_buffer_orig = 1;
326 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );
328 /* Set output properties */
329 p_dec->fmt_out.i_cat = VIDEO_ES;
330 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
335 /*****************************************************************************
336 * DecodeVideo: Called to decode one or more frames
337 *****************************************************************************/
338 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
340 decoder_sys_t *p_sys = p_dec->p_sys;
344 if( !pp_block || !*pp_block ) return NULL;
348 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
350 p_sys->input_pts = p_block->i_pts;
351 p_sys->input_dts = p_block->i_dts;
353 /* Make sure we don't reuse the same timestamps twice */
354 p_block->i_pts = p_block->i_dts = 0;
357 /* TODO implement it in a better way */
358 /* A good idea could be to decode all I pictures and see for the other */
359 if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
362 if( p_sys->i_late_frames < 8 )
364 p_sys->p_context->hurry_up = 2;
368 /* picture too late, won't decode
369 * but break picture until a new I, and for mpeg4 ...*/
371 p_sys->i_late_frames--; /* needed else it will never be decrease */
372 block_Release( p_block );
380 p_sys->p_context->hurry_up = 0;
383 if( p_sys->i_late_frames > 0 &&
384 mdate() - p_sys->i_late_frames_start > I64C(5000000) )
386 msg_Err( p_dec, "more than 5 seconds of late video -> "
387 "dropping frame (computer too slow ?)" );
388 block_Release( p_block );
389 p_sys->i_pts = 0; /* To make sure we recover properly */
390 p_sys->i_late_frames--;
394 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
396 p_sys->p_context->hurry_up = 5;
400 * Do the actual decoding now
403 /* Check if post-processing was enabled */
404 p_sys->b_pp = p_sys->b_pp_async;
406 /* Don't forget that ffmpeg requires a little more bytes
407 * that the real frame size */
408 if( p_block->i_buffer > 0 )
410 p_sys->i_buffer = p_block->i_buffer;
411 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
412 p_sys->i_buffer_orig )
414 free( p_sys->p_buffer_orig );
415 p_sys->i_buffer_orig =
416 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
417 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
419 p_sys->p_buffer = p_sys->p_buffer_orig;
420 p_sys->i_buffer = p_block->i_buffer;
421 p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
423 memset( p_sys->p_buffer + p_block->i_buffer, 0,
424 FF_INPUT_BUFFER_PADDING_SIZE );
426 p_block->i_buffer = 0;
429 while( p_sys->i_buffer > 0 )
431 int i_used, b_gotpicture;
434 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
436 p_sys->p_buffer, p_sys->i_buffer );
439 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
441 block_Release( p_block );
444 else if( i_used > p_sys->i_buffer )
446 i_used = p_sys->i_buffer;
450 p_sys->i_buffer -= i_used;
451 p_sys->p_buffer += i_used;
453 /* Nothing to display */
454 if( !b_gotpicture ) continue;
456 /* Update frame late count*/
457 if( p_sys->i_pts && p_sys->i_pts <= mdate() )
459 p_sys->i_late_frames++;
460 if( p_sys->i_late_frames == 1 )
461 p_sys->i_late_frames_start = mdate();
465 p_sys->i_late_frames = 0;
468 if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
470 /* Do not display the picture */
474 if( !p_sys->b_direct_rendering || p_sys->b_pp )
476 /* Get a new picture */
477 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
480 block_Release( p_block );
484 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
486 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
490 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
494 if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
496 /* Sanity check (seems to be needed for some streams ) */
497 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
499 p_sys->b_has_b_frames = VLC_TRUE;
502 /* Send decoded frame to vout */
505 p_pic->date = p_sys->i_pts;
507 /* interpolate the next PTS */
508 if( p_sys->p_context->frame_rate > 0 )
510 p_sys->i_pts += I64C(1000000) *
511 (2 + p_sys->p_ff_pic->repeat_pict) *
512 p_sys->p_context->frame_rate_base /
513 (2 * p_sys->p_context->frame_rate);
519 p_dec->pf_vout_buffer_del( p_dec, p_pic );
523 block_Release( p_block );
527 /*****************************************************************************
528 * EndVideo: decoder destruction
529 *****************************************************************************
530 * This function is called when the thread ends after a sucessful
532 *****************************************************************************/
533 void E_(EndVideoDec)( decoder_t *p_dec )
535 decoder_sys_t *p_sys = p_dec->p_sys;
537 if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
540 E_(ClosePostproc)( p_dec, p_sys->p_pp );
543 free( p_sys->p_buffer_orig );
546 /*****************************************************************************
547 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
548 * picture_t structure (when not in direct rendering mode).
549 *****************************************************************************/
550 static void ffmpeg_CopyPicture( decoder_t *p_dec,
551 picture_t *p_pic, AVFrame *p_ff_pic )
553 decoder_sys_t *p_sys = p_dec->p_sys;
555 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
557 int i_plane, i_size, i_line;
558 uint8_t *p_dst, *p_src;
559 int i_src_stride, i_dst_stride;
562 if( p_sys->p_pp && p_sys->b_pp )
563 E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
566 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
568 p_src = p_ff_pic->data[i_plane];
569 p_dst = p_pic->p[i_plane].p_pixels;
570 i_src_stride = p_ff_pic->linesize[i_plane];
571 i_dst_stride = p_pic->p[i_plane].i_pitch;
573 i_size = __MIN( i_src_stride, i_dst_stride );
574 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
576 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
577 p_src += i_src_stride;
578 p_dst += i_dst_stride;
587 /* we need to convert to I420 */
588 switch( p_sys->p_context->pix_fmt )
590 case( PIX_FMT_YUV410P ):
591 case( PIX_FMT_YUV411P ):
592 for( i = 0; i < p_pic->i_planes; i++ )
594 dest_pic.data[i] = p_pic->p[i].p_pixels;
595 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
597 img_convert( &dest_pic, PIX_FMT_YUV420P,
598 (AVPicture *)p_ff_pic,
599 p_sys->p_context->pix_fmt,
600 p_sys->p_context->width,
601 p_sys->p_context->height );
604 msg_Err( p_dec, "don't know how to convert chroma %i",
605 p_sys->p_context->pix_fmt );
612 /*****************************************************************************
613 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
614 *****************************************************************************
615 * It is used for direct rendering as well as to get the right PTS for each
616 * decoded picture (even in indirect rendering mode).
617 *****************************************************************************/
618 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
621 decoder_t *p_dec = (decoder_t *)p_context->opaque;
622 decoder_sys_t *p_sys = p_dec->p_sys;
625 /* Set picture PTS */
626 if( p_sys->input_pts )
628 p_ff_pic->pts = p_sys->input_pts;
630 else if( p_sys->input_dts )
632 /* Some demuxers only set the dts so let's try to find a useful
633 * timestamp from this */
634 if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
635 !p_ff_pic->reference )
637 p_ff_pic->pts = p_sys->input_dts;
639 else p_ff_pic->pts = 0;
641 else p_ff_pic->pts = 0;
643 p_sys->input_pts = p_sys->input_dts = 0;
645 /* Not much to do in indirect rendering mode */
646 if( !p_sys->b_direct_rendering || p_sys->b_pp )
648 return avcodec_default_get_buffer( p_context, p_ff_pic );
651 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
652 * so this check is necessary. */
653 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
654 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
656 msg_Dbg( p_dec, "disabling direct rendering" );
657 p_sys->b_direct_rendering = 0;
658 return avcodec_default_get_buffer( p_context, p_ff_pic );
661 /* Get a new picture */
662 //p_sys->p_vout->render.b_allow_modify_pics = 0;
663 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
666 p_sys->b_direct_rendering = 0;
667 return avcodec_default_get_buffer( p_context, p_ff_pic );
669 p_sys->p_context->draw_horiz_band = NULL;
671 p_ff_pic->opaque = (void*)p_pic;
672 p_ff_pic->type = FF_BUFFER_TYPE_USER;
673 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
674 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
675 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
676 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
678 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
679 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
680 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
681 p_ff_pic->linesize[3] = 0;
683 if( p_ff_pic->reference != 0 )
685 //vout_LinkPicture( p_sys->p_vout, p_pic );
688 /* FIXME what is that, should give good value */
689 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
694 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
697 decoder_t *p_dec = (decoder_t *)p_context->opaque;
700 if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
702 avcodec_default_release_buffer( p_context, p_ff_pic );
706 p_pic = (picture_t*)p_ff_pic->opaque;
708 p_ff_pic->data[0] = NULL;
709 p_ff_pic->data[1] = NULL;
710 p_ff_pic->data[2] = NULL;
711 p_ff_pic->data[3] = NULL;
713 if( p_ff_pic->reference != 0 )
715 //vout_UnlinkPicture( p_sys->p_vout, p_pic );