1 /*****************************************************************************
2 * video.c: video decoder using the ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video.c,v 1.46 2003/11/17 02:52:39 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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
36 #ifdef HAVE_FFMPEG_AVCODEC_H
37 # include <ffmpeg/avcodec.h>
44 /*****************************************************************************
45 * decoder_sys_t : decoder descriptor
46 *****************************************************************************/
49 /* Common part between video and audio decoder */
54 AVCodecContext *p_context;
57 /* Video decoder specific part */
62 BITMAPINFOHEADER *p_format;
64 /* for frame skipping algo */
68 /* how many decoded frames are late */
70 mtime_t i_late_frames_start;
72 /* for direct rendering */
73 int b_direct_rendering;
75 vlc_bool_t b_has_b_frames;
77 int i_buffer_orig, i_buffer;
78 char *p_buffer_orig, *p_buffer;
80 /* Postprocessing handle */
85 /*****************************************************************************
87 *****************************************************************************/
88 static void ffmpeg_CopyPicture ( decoder_t *, picture_t *, AVFrame * );
89 static int ffmpeg_GetFrameBuf ( struct AVCodecContext *, AVFrame * );
90 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
92 /*****************************************************************************
94 *****************************************************************************/
95 static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
100 return VLC_FOURCC('I','4','2','0');
101 case PIX_FMT_YUV422P:
102 return VLC_FOURCC('I','4','2','2');
103 case PIX_FMT_YUV444P:
104 return VLC_FOURCC('I','4','4','4');
107 return VLC_FOURCC('Y','U','Y','2');
110 return VLC_FOURCC('R','V','1','5');
112 return VLC_FOURCC('R','V','1','6');
114 return VLC_FOURCC('R','V','2','4');
116 return VLC_FOURCC('R','V','3','2');
118 return VLC_FOURCC('G','R','E','Y');
120 case PIX_FMT_YUV410P:
121 case PIX_FMT_YUV411P:
128 /* Returns a new picture buffer */
129 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
130 AVCodecContext *p_context )
132 decoder_sys_t *p_sys = p_dec->p_sys;
135 p_dec->fmt_out.video.i_width = p_context->width;
136 p_dec->fmt_out.video.i_height = p_context->height;
137 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
139 if( !p_context->width || !p_context->height )
141 return NULL; /* invalid display size */
144 if( !p_dec->fmt_out.i_codec )
146 /* we make conversion if possible*/
147 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
150 #if LIBAVCODEC_BUILD >= 4687
151 p_dec->fmt_out.video.i_aspect =
152 VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
153 p_context->width / p_context->height );
155 p_dec->fmt_out.video.i_aspect =
156 VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
158 if( p_dec->fmt_out.video.i_aspect == 0 )
160 p_dec->fmt_out.video.i_aspect =
161 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
164 p_pic = p_dec->pf_vout_buffer_new( p_dec );
167 if( p_sys->p_pp && !p_sys->b_pp_init )
169 E_(InitPostproc)( p_dec, p_sys->p_pp, p_context->width,
170 p_context->height, p_context->pix_fmt );
171 p_sys->b_pp_init = VLC_TRUE;
178 /*****************************************************************************
179 * InitVideo: initialize the video decoder
180 *****************************************************************************
181 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
182 * opened (done after the first decoded frame).
183 *****************************************************************************/
184 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
185 AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
187 decoder_sys_t *p_sys;
191 /* Allocate the memory needed to store the decoder's structure */
192 if( ( p_dec->p_sys = p_sys =
193 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
195 msg_Err( p_dec, "out of memory" );
199 p_dec->p_sys->p_context = p_context;
200 p_dec->p_sys->p_codec = p_codec;
201 p_dec->p_sys->i_codec_id = i_codec_id;
202 p_dec->p_sys->psz_namecodec = psz_namecodec;
203 p_sys->p_ff_pic = avcodec_alloc_frame();
205 /* ***** Fill p_context with init values ***** */
206 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
207 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
209 /* ***** Get configuration of ffmpeg plugin ***** */
210 i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
211 p_sys->p_context->workaround_bugs = __MAX( __MIN( i_tmp, 99 ), 0 );
213 i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
214 p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
216 var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
217 var_Get( p_dec, "grayscale", &val );
218 if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
220 /* Decide if we set CODEC_FLAG_TRUNCATED */
221 #if LIBAVCODEC_BUILD >= 4662
222 var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
223 var_Get( p_dec, "ffmpeg-truncated", &val );
224 if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
227 /* ***** Open the codec ***** */
228 if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
230 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
235 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
238 /* ***** ffmpeg frame skipping ***** */
239 var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
240 var_Get( p_dec, "ffmpeg-hurry-up", &val );
241 p_sys->b_hurry_up = val.b_bool;
243 /* ***** ffmpeg direct rendering ***** */
244 p_sys->b_direct_rendering = 0;
245 var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
246 var_Get( p_dec, "ffmpeg-dr", &val );
247 if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
248 ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
249 /* Apparently direct rendering doesn't work with YUV422P */
250 p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
251 !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
253 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
254 * so we need to do another check in ffmpeg_GetFrameBuf() */
255 p_sys->b_direct_rendering = 1;
260 p_dec->p_sys->b_pp_init = VLC_FALSE;
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_dec->fmt_in.i_extra )
286 int i_size = p_dec->fmt_in.i_extra;
288 if( p_sys->i_codec_id == CODEC_ID_MPEG4 )
290 uint8_t *p_vol = malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
291 memcpy( p_vol, p_dec->fmt_in.p_extra, i_size );
292 memset( &p_vol[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
294 avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
295 &b_gotpicture, p_vol, i_size );
298 #if LIBAVCODEC_BUILD >= 4666
299 else if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
303 p_sys->p_context->extradata_size = i_size + 12;
304 p = p_sys->p_context->extradata =
305 malloc( p_sys->p_context->extradata_size );
307 memcpy( &p[0], "SVQ3", 4 );
308 memset( &p[4], 0, 8 );
309 memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
314 p_sys->p_context->extradata_size = i_size;
315 p_sys->p_context->extradata =
316 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
317 memcpy( p_sys->p_context->extradata,
318 p_dec->fmt_in.p_extra, i_size );
319 memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
320 0, FF_INPUT_BUFFER_PADDING_SIZE );
324 /* ***** misc init ***** */
325 p_sys->input_pts = 0;
327 p_sys->b_has_b_frames = VLC_FALSE;
328 p_sys->i_late_frames = 0;
330 p_sys->i_buffer_orig = 1;
331 p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );
333 /* Set output properties */
334 p_dec->fmt_out.i_cat = VIDEO_ES;
335 p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
340 /*****************************************************************************
341 * DecodeVideo: Called to decode one or more frames
342 *****************************************************************************/
343 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
345 decoder_sys_t *p_sys = p_dec->p_sys;
349 if( !pp_block || !*pp_block ) return NULL;
353 if( p_block->i_pts > 0 )
355 p_sys->input_pts = p_block->i_pts;
358 /* TODO implement it in a better way */
359 /* A good idea could be to decode all I pictures and see for the other */
360 if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
363 if( p_sys->i_late_frames < 8 )
365 p_sys->p_context->hurry_up = 2;
369 /* picture too late, won't decode
370 * but break picture until a new I, and for mpeg4 ...*/
372 p_sys->i_late_frames--; /* needed else it will never be decrease */
373 block_Release( p_block );
381 p_sys->p_context->hurry_up = 0;
384 if( p_sys->i_late_frames > 0 &&
385 mdate() - p_sys->i_late_frames_start > I64C(5000000) )
387 msg_Err( p_dec, "more than 5 seconds of late video -> "
388 "dropping frame (computer too slow ?)" );
389 block_Release( p_block );
390 p_sys->i_pts = 0; /* To make sure we recover properly */
391 p_sys->i_late_frames--;
395 if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
397 p_sys->p_context->hurry_up = 5;
401 * Do the actual decoding now
404 /* Don't forget that ffmpeg requires a little more bytes
405 * that the real frame size */
406 if( p_block->i_buffer > 0 )
408 p_sys->i_buffer = p_block->i_buffer;
409 if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
410 p_sys->i_buffer_orig )
412 free( p_sys->p_buffer_orig );
413 p_sys->i_buffer_orig =
414 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
415 p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
417 p_sys->p_buffer = p_sys->p_buffer_orig;
418 p_sys->i_buffer = p_block->i_buffer;
419 p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
421 memset( p_sys->p_buffer + p_block->i_buffer, 0,
422 FF_INPUT_BUFFER_PADDING_SIZE );
424 p_block->i_buffer = 0;
427 while( p_sys->i_buffer > 0 )
429 int i_used, b_gotpicture;
432 i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
434 p_sys->p_buffer, p_sys->i_buffer );
437 msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
439 block_Release( p_block );
442 else if( i_used > p_sys->i_buffer )
444 i_used = p_sys->i_buffer;
448 p_sys->i_buffer -= i_used;
449 p_sys->p_buffer += i_used;
451 /* Nothing to display */
452 if( !b_gotpicture ) continue;
454 /* Update frame late count*/
455 if( p_sys->i_pts && p_sys->i_pts <= mdate() )
457 p_sys->i_late_frames++;
458 if( p_sys->i_late_frames == 1 )
459 p_sys->i_late_frames_start = mdate();
463 p_sys->i_late_frames = 0;
466 if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
468 /* Do not display the picture */
472 if( !p_sys->b_direct_rendering )
474 /* Get a new picture */
475 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
478 block_Release( p_block );
482 /* Fill p_picture_t from AVVideoFrame and do chroma conversion
484 ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
488 p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
492 * There is an ugly hack here because some demuxers pass us a dts
493 * instead of a pts so this screw up things for streams with
495 if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
496 p_sys->b_has_b_frames = VLC_TRUE;
497 if( p_sys->p_ff_pic->pts &&
498 ( !p_sys->p_context->has_b_frames || !p_sys->b_has_b_frames ||
499 p_sys->p_ff_pic->pict_type == FF_B_TYPE ) )
501 p_sys->i_pts = p_sys->p_ff_pic->pts;
504 /* Send decoded frame to vout */
507 p_pic->date = p_sys->i_pts;
509 /* interpolate the next PTS */
510 if( p_sys->p_context->frame_rate > 0 )
512 p_sys->i_pts += I64C(1000000) *
513 (2 + p_sys->p_ff_pic->repeat_pict) *
514 p_sys->p_context->frame_rate_base /
515 (2 * p_sys->p_context->frame_rate);
521 p_dec->pf_vout_buffer_del( p_dec, p_pic );
525 block_Release( p_block );
529 /*****************************************************************************
530 * EndVideo: decoder destruction
531 *****************************************************************************
532 * This function is called when the thread ends after a sucessful
534 *****************************************************************************/
535 void E_(EndVideoDec)( decoder_t *p_dec )
537 decoder_sys_t *p_sys = p_dec->p_sys;
539 if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
542 E_(ClosePostproc)( p_dec, p_sys->p_pp );
545 free( p_sys->p_buffer_orig );
548 /*****************************************************************************
549 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
550 * picture_t structure (when not in direct rendering mode).
551 *****************************************************************************/
552 static void ffmpeg_CopyPicture( decoder_t *p_dec,
553 picture_t *p_pic, AVFrame *p_ff_pic )
555 decoder_sys_t *p_sys = p_dec->p_sys;
557 if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
559 int i_plane, i_size, i_line;
560 uint8_t *p_dst, *p_src;
561 int i_src_stride, i_dst_stride;
565 E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
568 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
570 p_src = p_ff_pic->data[i_plane];
571 p_dst = p_pic->p[i_plane].p_pixels;
572 i_src_stride = p_ff_pic->linesize[i_plane];
573 i_dst_stride = p_pic->p[i_plane].i_pitch;
575 i_size = __MIN( i_src_stride, i_dst_stride );
576 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
578 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
579 p_src += i_src_stride;
580 p_dst += i_dst_stride;
589 /* we need to convert to I420 */
590 switch( p_sys->p_context->pix_fmt )
592 case( PIX_FMT_YUV410P ):
593 case( PIX_FMT_YUV411P ):
594 for( i = 0; i < p_pic->i_planes; i++ )
596 dest_pic.data[i] = p_pic->p[i].p_pixels;
597 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
599 img_convert( &dest_pic, PIX_FMT_YUV420P,
600 (AVPicture *)p_ff_pic,
601 p_sys->p_context->pix_fmt,
602 p_sys->p_context->width,
603 p_sys->p_context->height );
606 msg_Err( p_dec, "don't know how to convert chroma %i",
607 p_sys->p_context->pix_fmt );
608 p_dec->p_fifo->b_error = 1;
614 /*****************************************************************************
615 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
616 *****************************************************************************
617 * It is used for direct rendering as well as to get the right PTS for each
618 * decoded picture (even in indirect rendering mode).
619 *****************************************************************************/
620 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
623 decoder_t *p_dec = (decoder_t *)p_context->opaque;
624 decoder_sys_t *p_sys = p_dec->p_sys;
627 /* Set picture PTS */
628 p_ff_pic->pts = p_sys->input_pts;
629 p_sys->input_pts = 0;
631 /* Not much to do in indirect rendering mode */
632 if( !p_sys->b_direct_rendering )
634 return avcodec_default_get_buffer( p_context, p_ff_pic );
637 /* Some codecs set pix_fmt only after the 1st frame has been decoded,
638 * so this check is necessary. */
639 if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
640 p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
642 msg_Dbg( p_dec, "disabling direct rendering" );
643 p_sys->b_direct_rendering = 0;
644 return avcodec_default_get_buffer( p_context, p_ff_pic );
647 /* Get a new picture */
648 //p_sys->p_vout->render.b_allow_modify_pics = 0;
649 p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
652 p_sys->b_direct_rendering = 0;
653 return avcodec_default_get_buffer( p_context, p_ff_pic );
655 p_sys->p_context->draw_horiz_band = NULL;
657 p_ff_pic->opaque = (void*)p_pic;
658 p_ff_pic->type = FF_BUFFER_TYPE_USER;
659 p_ff_pic->data[0] = p_pic->p[0].p_pixels;
660 p_ff_pic->data[1] = p_pic->p[1].p_pixels;
661 p_ff_pic->data[2] = p_pic->p[2].p_pixels;
662 p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
664 p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
665 p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
666 p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
667 p_ff_pic->linesize[3] = 0;
669 if( p_ff_pic->reference != 0 )
671 //vout_LinkPicture( p_sys->p_vout, p_pic );
674 /* FIXME what is that, should give good value */
675 p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
680 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
683 decoder_t *p_dec = (decoder_t *)p_context->opaque;
686 if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
688 avcodec_default_release_buffer( p_context, p_ff_pic );
692 p_pic = (picture_t*)p_ff_pic->opaque;
694 p_ff_pic->data[0] = NULL;
695 p_ff_pic->data[1] = NULL;
696 p_ff_pic->data[2] = NULL;
697 p_ff_pic->data[3] = NULL;
699 if( p_ff_pic->reference != 0 )
701 //vout_UnlinkPicture( p_sys->p_vout, p_pic );