/***************************************************************************** * DecodeVideo: Called to decode one or more frames *****************************************************************************/ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; AVCodecContext *p_context = p_sys->p_context; int b_drawpicture; int b_null_size = false; block_t *p_block; if( !pp_block || !*pp_block ) return NULL; if( !p_context->extradata_size && p_dec->fmt_in.i_extra ) { ffmpeg_InitCodec( p_dec ); if( p_sys->b_delayed_open ) { if( ffmpeg_OpenCodec( p_dec ) ) msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec ); } } p_block = *pp_block; if( p_sys->b_delayed_open ) { block_Release( p_block ); return NULL; } if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */ p_sys->i_late_frames = 0; if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) avcodec_flush_buffers( p_context ); block_Release( p_block ); return NULL; } if( p_block->i_flags & BLOCK_FLAG_PREROLL ) { /* Do not care about late frames when prerolling * TODO avoid decoding of non reference frame * (ie all B except for H264 where it depends only on nal_ref_idc) */ p_sys->i_late_frames = 0; } if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) && (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) ) { if( p_sys->i_pts > VLC_TS_INVALID ) { msg_Err( p_dec, "more than 5 seconds of late video -> " "dropping frame (computer too slow ?)" ); p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */ } block_Release( p_block ); p_sys->i_late_frames--; return NULL; } /* A good idea could be to decode all I pictures and see for the other */ if( !p_dec->b_pace_control && p_sys->b_hurry_up && (p_sys->i_late_frames > 4) ) { b_drawpicture = 0; if( p_sys->i_late_frames < 12 ) { p_context->skip_frame = (p_sys->i_skip_frame <= AVDISCARD_BIDIR) ? AVDISCARD_BIDIR : p_sys->i_skip_frame; } else { /* picture too late, won't decode * but break picture until a new I, and for mpeg4 ...*/ p_sys->i_late_frames--; /* needed else it will never be decrease */ block_Release( p_block ); return NULL; } } else { if( p_sys->b_hurry_up ) p_context->skip_frame = p_sys->i_skip_frame; if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) ) b_drawpicture = 1; else b_drawpicture = 0; } if( p_context->width <= 0 || p_context->height <= 0 ) { if( p_sys->b_hurry_up ) p_context->skip_frame = p_sys->i_skip_frame; b_null_size = true; } else if( !b_drawpicture ) { /* It creates broken picture * FIXME either our parser or ffmpeg is broken */ #if 0 if( p_sys->b_hurry_up ) p_context->skip_frame = __MAX( p_context->skip_frame, AVDISCARD_NONREF ); #endif } /* * Do the actual decoding now */ /* Don't forget that ffmpeg requires a little more bytes * that the real frame size */ if( p_block->i_buffer > 0 ) { p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0; p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE ); if( !p_block ) return NULL; p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE; *pp_block = p_block; memset( p_block->p_buffer + p_block->i_buffer, 0, FF_INPUT_BUFFER_PADDING_SIZE ); } while( p_block->i_buffer > 0 || p_sys->b_flush ) { int i_used, b_gotpicture; picture_t *p_pic; /* Set the PTS/DTS in the context reordered_opaque field */ if( p_block->i_pts > VLC_TS_INVALID ) p_context->reordered_opaque = (p_block->i_pts << 1) | 0; else if( p_block->i_dts > VLC_TS_INVALID ) p_context->reordered_opaque = (p_block->i_dts << 1) | 1; else p_context->reordered_opaque = INT64_MIN; p_sys->p_ff_pic->reordered_opaque = p_context->reordered_opaque; /* Make sure we don't reuse the same timestamps twice */ p_block->i_pts = p_block->i_dts = VLC_TS_INVALID; post_mt( p_sys ); i_used = avcodec_decode_video( p_context, p_sys->p_ff_pic, &b_gotpicture, p_block->i_buffer <= 0 && p_sys->b_flush ? NULL : p_block->p_buffer, p_block->i_buffer ); if( b_null_size && !p_sys->b_flush && p_context->width > 0 && p_context->height > 0 ) { /* Reparse it to not drop the I frame */ b_null_size = false; if( p_sys->b_hurry_up ) p_context->skip_frame = p_sys->i_skip_frame; i_used = avcodec_decode_video( p_context, p_sys->p_ff_pic, &b_gotpicture, p_block->p_buffer, p_block->i_buffer ); } wait_mt( p_sys ); if( p_sys->b_flush ) p_sys->b_first_frame = true; if( p_block->i_buffer <= 0 ) p_sys->b_flush = false; if( i_used < 0 ) { if( b_drawpicture ) msg_Warn( p_dec, "cannot decode one frame (%zu bytes)", p_block->i_buffer ); block_Release( p_block ); return NULL; } else if( i_used > p_block->i_buffer || p_context->thread_count > 1 ) { i_used = p_block->i_buffer; } /* Consumed bytes */ p_block->i_buffer -= i_used; p_block->p_buffer += i_used; /* Nothing to display */ if( !b_gotpicture ) { if( i_used == 0 ) break; continue; } /* Compute the PTS */ mtime_t i_pts = VLC_TS_INVALID; if( p_sys->p_ff_pic->reordered_opaque != INT64_MIN ) { mtime_t i_ts = p_sys->p_ff_pic->reordered_opaque >> 1; bool b_dts = p_sys->p_ff_pic->reordered_opaque & 1; if( b_dts ) { if( !p_context->has_b_frames || !p_sys->b_has_b_frames || !p_sys->p_ff_pic->reference || p_sys->i_pts <= VLC_TS_INVALID ) i_pts = i_ts; } else { i_pts = i_ts; } } if( i_pts <= VLC_TS_INVALID ) i_pts = p_sys->i_pts; /* Interpolate the next PTS */ if( i_pts > VLC_TS_INVALID ) p_sys->i_pts = i_pts; if( p_sys->i_pts > VLC_TS_INVALID ) { /* interpolate the next PTS */ if( p_dec->fmt_in.video.i_frame_rate > 0 && p_dec->fmt_in.video.i_frame_rate_base > 0 ) { p_sys->i_pts += INT64_C(1000000) * (2 + p_sys->p_ff_pic->repeat_pict) * p_dec->fmt_in.video.i_frame_rate_base / (2 * p_dec->fmt_in.video.i_frame_rate); } else if( p_context->time_base.den > 0 ) { #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52,20,0) int i_tick = p_context->ticks_per_frame; if( i_tick <= 0 ) i_tick = 1; #else int i_tick = 1; #endif p_sys->i_pts += INT64_C(1000000) * (2 + p_sys->p_ff_pic->repeat_pict) * i_tick * p_context->time_base.num / (2 * p_context->time_base.den); } } /* Update frame late count (except when doing preroll) */ mtime_t i_display_date = 0; if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) ) i_display_date = decoder_GetDisplayDate( p_dec, i_pts ); if( i_display_date > 0 && i_display_date <= mdate() ) { p_sys->i_late_frames++; if( p_sys->i_late_frames == 1 ) p_sys->i_late_frames_start = mdate(); } else { p_sys->i_late_frames = 0; } if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) ) continue; if( !p_sys->p_ff_pic->opaque ) { /* Get a new picture */ p_pic = ffmpeg_NewPictBuf( p_dec, p_context ); if( !p_pic ) { block_Release( p_block ); return NULL; } /* Fill p_picture_t from AVVideoFrame and do chroma conversion * if needed */ ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic ); } else { p_pic = (picture_t *)p_sys->p_ff_pic->opaque; decoder_LinkPicture( p_dec, p_pic ); } /* Sanity check (seems to be needed for some streams) */ if( p_sys->p_ff_pic->pict_type == FF_B_TYPE ) { p_sys->b_has_b_frames = true; } if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den ) { /* Fetch again the aspect ratio in case it changed */ p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num; p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den; if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den ) { p_dec->fmt_out.video.i_sar_num = 1; p_dec->fmt_out.video.i_sar_den = 1; } } /* Send decoded frame to vout */ if( i_pts > VLC_TS_INVALID) { p_pic->date = i_pts; if( p_sys->b_first_frame ) { /* Hack to force display of still pictures */ p_sys->b_first_frame = false; p_pic->b_force = true; } p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict; p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame; p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first; p_pic->i_qstride = p_sys->p_ff_pic->qstride; int i_mb_h = ( p_pic->format.i_height + 15 ) / 16; p_pic->p_q = malloc( p_pic->i_qstride * i_mb_h ); memcpy( p_pic->p_q, p_sys->p_ff_pic->qscale_table, p_pic->i_qstride * i_mb_h ); switch( p_sys->p_ff_pic->qscale_type ) { case FF_QSCALE_TYPE_MPEG1: p_pic->i_qtype = QTYPE_MPEG1; break; case FF_QSCALE_TYPE_MPEG2: p_pic->i_qtype = QTYPE_MPEG2; break; case FF_QSCALE_TYPE_H264: p_pic->i_qtype = QTYPE_H264; break; } return p_pic; } else { decoder_DeletePicture( p_dec, p_pic ); } }
/***************************************************************************** * DecodeVideo: *****************************************************************************/ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_block; picture_t *p_pic; mtime_t i_pts; ComponentResult cres; #ifdef LOADER /* We must do open and close in the same thread (unless we do * Setup_LDT_Keeper in the main thread before all others */ if( p_sys == NULL ) { if( OpenVideo( p_dec ) ) { /* Fatal */ p_dec->b_error = true; return NULL; } p_sys = p_dec->p_sys; } #endif if( pp_block == NULL || *pp_block == NULL ) { return NULL; } p_block = *pp_block; *pp_block = NULL; i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts; mtime_t i_display_date = 0; if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) ) i_display_date = decoder_GetDisplayDate( p_dec, i_pts ); if( i_display_date > 0 && i_display_date < mdate() ) { p_sys->i_late++; } else { p_sys->i_late = 0; } #ifndef NDEBUG msg_Dbg( p_dec, "bufsize: %d", (int)p_block->i_buffer); #endif if( p_sys->i_late > 10 ) { msg_Dbg( p_dec, "late buffer dropped (%"PRId64")", i_pts ); block_Release( p_block ); return NULL; } vlc_mutex_lock( &qt_mutex ); if( ( p_pic = decoder_NewPicture( p_dec ) ) ) { p_sys->decpar.data = (Ptr)p_block->p_buffer; p_sys->decpar.bufferSize = p_block->i_buffer; (**p_sys->framedescHandle).dataSize = p_block->i_buffer; cres = p_sys->ImageCodecBandDecompress( p_sys->ci, &p_sys->decpar ); ++p_sys->decpar.frameNumber; if( cres &0xFFFF ) { msg_Dbg( p_dec, "quicktime_video: ImageCodecBandDecompress" " cres=0x%X (-0x%X) %d :(", (int)cres,(int)-cres, (int)cres ); } memcpy( p_pic->p[0].p_pixels, p_sys->plane, p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height * 2 ); p_pic->date = i_pts; } vlc_mutex_unlock( &qt_mutex ); block_Release( p_block ); return p_pic; }
/***************************************************************************** * decoder_SynchroChoose : Decide whether we will decode a picture or not *****************************************************************************/ bool decoder_SynchroChoose( decoder_synchro_t * p_synchro, int i_coding_type, int i_render_time, bool b_low_delay ) { #define TAU_PRIME( coding_type ) (p_synchro->p_tau[(coding_type)] \ + (p_synchro->p_tau[(coding_type)] >> 1) \ + p_synchro->i_render_time) #define S (*p_synchro) mtime_t now, period; mtime_t pts; bool b_decode = 0; int i_current_rate; if ( p_synchro->b_no_skip ) return 1; i_current_rate = decoder_GetDisplayRate( p_synchro->p_dec ); now = mdate(); period = 1000000 * 1001 / p_synchro->i_frame_rate * i_current_rate / INPUT_RATE_DEFAULT; p_synchro->i_render_time = i_render_time; switch( i_coding_type ) { case I_CODING_TYPE: if( b_low_delay ) { pts = decoder_GetDisplayDate( p_synchro->p_dec, S.current_pts ); } else if( S.backward_pts ) { pts = decoder_GetDisplayDate( p_synchro->p_dec, S.backward_pts ); } else { /* displaying order : B B P B B I * ^ ^ * | +- current picture * +- current PTS */ pts = decoder_GetDisplayDate( p_synchro->p_dec, S.current_pts ) + period * (S.i_n_b + 2); } if( (1 + S.i_n_p * (S.i_n_b + 1)) * period > S.p_tau[I_CODING_TYPE] ) { b_decode = 1; } else { b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA); } if( pts <= VLC_TS_INVALID ) b_decode = 1; if( !b_decode && !p_synchro->b_quiet ) { msg_Warn( p_synchro->p_dec, "synchro trashing I (%"PRId64")", pts - now ); } break; case P_CODING_TYPE: if( b_low_delay ) { pts = decoder_GetDisplayDate( p_synchro->p_dec, S.current_pts ); } else if( S.backward_pts ) { pts = decoder_GetDisplayDate( p_synchro->p_dec, S.backward_pts ); } else { pts = decoder_GetDisplayDate( p_synchro->p_dec, S.current_pts + period * (S.i_n_b + 1) ); } if( p_synchro->i_nb_ref < 1 ) { b_decode = 0; } else if( (1 + S.i_n_p * (S.i_n_b + 1)) * period > S.p_tau[I_CODING_TYPE] ) { if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] ) { /* Security in case we're _really_ late */ b_decode = (pts - now > 0); } else { b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA); /* next I */ b_decode &= (pts - now + period * ( (S.i_n_p - S.i_eta_p) * (1 + S.i_n_b) - 1 )) > (TAU_PRIME(P_CODING_TYPE) + TAU_PRIME(I_CODING_TYPE) + DELTA); } } else { b_decode = 0; } if( p_synchro->i_nb_ref >= 1 && pts <= VLC_TS_INVALID ) b_decode = 1; break; case B_CODING_TYPE: pts = decoder_GetDisplayDate( p_synchro->p_dec, S.current_pts ); if( p_synchro->i_nb_ref < 2 ) { b_decode = 0; } else if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] ) { b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA); } else { b_decode = 0; } if( p_synchro->i_nb_ref >= 2 && pts <= VLC_TS_INVALID ) b_decode = 1; break; } if( !b_decode ) { S.i_not_chosen_pic++; } return( b_decode ); #undef S #undef TAU_PRIME }
/***************************************************************************** * DecodeAudio: *****************************************************************************/ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_block; int i_error; #ifdef LOADER /* We must do open and close in the same thread (unless we do * Setup_LDT_Keeper in the main thread before all others */ if( p_sys == NULL ) { if( OpenAudio( p_dec ) ) { /* Fatal */ p_dec->b_error = true; return NULL; } p_sys = p_dec->p_sys; } #endif if( pp_block == NULL || *pp_block == NULL ) { return NULL; } p_block = *pp_block; if( p_sys->i_out_frames > 0 && p_sys->i_out >= p_sys->i_out_frames ) { /* Ask new data */ p_sys->i_out = 0; p_sys->i_out_frames = 0; *pp_block = NULL; return NULL; } if( p_sys->i_out_frames <= 0 ) { p_sys->pts = p_block->i_pts; mtime_t i_display_date = 0; if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) ) i_display_date = decoder_GetDisplayDate( p_dec, p_block->i_pts ); if( i_display_date > 0 && i_display_date < mdate() ) { block_Release( p_block ); *pp_block = NULL; return NULL; } /* Append data */ if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer ) { p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer + 1024; p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size ); } memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer, p_block->i_buffer ); p_sys->i_buffer += p_block->i_buffer; if( p_sys->i_buffer > p_sys->InFrameSize ) { int i_frames = p_sys->i_buffer / p_sys->InFrameSize; unsigned long i_out_frames, i_out_bytes; vlc_mutex_lock( &qt_mutex ); i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter, p_sys->p_buffer, i_frames, p_sys->out_buffer, &i_out_frames, &i_out_bytes ); vlc_mutex_unlock( &qt_mutex ); /* msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)", i_frames, i_out_frames, i_error ); msg_Dbg( p_dec, "decoded %ld frames = %ld bytes", i_out_frames, i_out_bytes ); */ p_sys->i_buffer -= i_frames * p_sys->InFrameSize; if( p_sys->i_buffer > 0 ) { memmove( &p_sys->p_buffer[0], &p_sys->p_buffer[i_frames * p_sys->InFrameSize], p_sys->i_buffer ); } if( p_sys->pts != 0 && p_sys->pts != date_Get( &p_sys->date ) ) { date_Set( &p_sys->date, p_sys->pts ); } else if( !date_Get( &p_sys->date ) ) { return NULL; } if( !i_error && i_out_frames > 0 ) { /* we have others samples */ p_sys->i_out_frames = i_out_frames; p_sys->i_out = 0; } } } if( p_sys->i_out < p_sys->i_out_frames ) { aout_buffer_t *p_out; int i_frames = __MIN( p_sys->i_out_frames - p_sys->i_out, 1000 ); p_out = decoder_NewAudioBuffer( p_dec, i_frames ); if( p_out ) { p_out->i_pts = date_Get( &p_sys->date ); p_out->i_length = date_Increment( &p_sys->date, i_frames ) - p_out->i_pts; memcpy( p_out->p_buffer, &p_sys->out_buffer[2 * p_sys->i_out * p_dec->fmt_out.audio.i_channels], p_out->i_buffer ); p_sys->i_out += i_frames; } return p_out; } return NULL; }
/***************************************************************************** * DecodeVideo: Called to decode one or more frames *****************************************************************************/ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; AVCodecContext *p_context = p_sys->p_context; int b_drawpicture; block_t *p_block; if( !pp_block ) return NULL; if( !p_context->extradata_size && p_dec->fmt_in.i_extra ) { ffmpeg_InitCodec( p_dec ); if( p_sys->b_delayed_open ) { if( ffmpeg_OpenCodec( p_dec ) ) msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec ); } } p_block = *pp_block; if(!p_block && !(p_sys->p_codec->capabilities & CODEC_CAP_DELAY) ) return NULL; if( p_sys->b_delayed_open ) { if( p_block ) block_Release( p_block ); return NULL; } if( p_block) { if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */ p_sys->i_late_frames = 0; post_mt( p_sys ); if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) avcodec_flush_buffers( p_context ); wait_mt( p_sys ); block_Release( p_block ); return NULL; } if( p_block->i_flags & BLOCK_FLAG_PREROLL ) { /* Do not care about late frames when prerolling * TODO avoid decoding of non reference frame * (ie all B except for H264 where it depends only on nal_ref_idc) */ p_sys->i_late_frames = 0; } } if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) && (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) ) { if( p_sys->i_pts > VLC_TS_INVALID ) { p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */ } if( p_block ) block_Release( p_block ); p_sys->i_late_frames--; msg_Err( p_dec, "more than 5 seconds of late video -> " "dropping frame (computer too slow ?)" ); return NULL; } /* A good idea could be to decode all I pictures and see for the other */ if( !p_dec->b_pace_control && p_sys->b_hurry_up && (p_sys->i_late_frames > 4) ) { b_drawpicture = 0; if( p_sys->i_late_frames < 12 ) { p_context->skip_frame = (p_sys->i_skip_frame <= AVDISCARD_NONREF) ? AVDISCARD_NONREF : p_sys->i_skip_frame; } else { /* picture too late, won't decode * but break picture until a new I, and for mpeg4 ...*/ p_sys->i_late_frames--; /* needed else it will never be decrease */ if( p_block ) block_Release( p_block ); msg_Warn( p_dec, "More than 4 late frames, dropping frame" ); return NULL; } } else { if( p_sys->b_hurry_up ) p_context->skip_frame = p_sys->i_skip_frame; if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) ) b_drawpicture = 1; else b_drawpicture = 0; } if( p_context->width <= 0 || p_context->height <= 0 ) { if( p_sys->b_hurry_up ) p_context->skip_frame = p_sys->i_skip_frame; } else if( !b_drawpicture ) { /* It creates broken picture * FIXME either our parser or ffmpeg is broken */ #if 0 if( p_sys->b_hurry_up ) p_context->skip_frame = __MAX( p_context->skip_frame, AVDISCARD_NONREF ); #endif } /* * Do the actual decoding now */ /* Don't forget that libavcodec requires a little more bytes * that the real frame size */ if( p_block && p_block->i_buffer > 0 ) { p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0; p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE ); if( !p_block ) return NULL; p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE; *pp_block = p_block; memset( p_block->p_buffer + p_block->i_buffer, 0, FF_INPUT_BUFFER_PADDING_SIZE ); } while( !p_block || p_block->i_buffer > 0 || p_sys->b_flush ) { int i_used, b_gotpicture; picture_t *p_pic; AVPacket pkt; post_mt( p_sys ); av_init_packet( &pkt ); if( p_block ) { pkt.data = p_block->p_buffer; pkt.size = p_block->i_buffer; pkt.pts = p_block->i_pts; pkt.dts = p_block->i_dts; } else { /* Return delayed frames if codec has CODEC_CAP_DELAY */ pkt.data = NULL; pkt.size = 0; } #if LIBAVCODEC_VERSION_MAJOR >= 54 if( !p_sys->palette_sent ) { uint8_t *pal = av_packet_new_side_data(&pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); if (pal) { memcpy(pal, p_dec->fmt_in.video.p_palette->palette, AVPALETTE_SIZE); p_sys->palette_sent = true; } } #endif /* Make sure we don't reuse the same timestamps twice */ if( p_block ) { p_block->i_pts = p_block->i_dts = VLC_TS_INVALID; } i_used = avcodec_decode_video2( p_context, p_sys->p_ff_pic, &b_gotpicture, &pkt ); wait_mt( p_sys ); if( p_sys->b_flush ) p_sys->b_first_frame = true; if( p_block ) { if( p_block->i_buffer <= 0 ) p_sys->b_flush = false; if( i_used < 0 ) { if( b_drawpicture ) msg_Warn( p_dec, "cannot decode one frame (%zu bytes)", p_block->i_buffer ); block_Release( p_block ); return NULL; } else if( (unsigned)i_used > p_block->i_buffer || p_context->thread_count > 1 ) { i_used = p_block->i_buffer; } /* Consumed bytes */ p_block->i_buffer -= i_used; p_block->p_buffer += i_used; } /* Nothing to display */ if( !b_gotpicture ) { if( i_used == 0 ) break; continue; } /* Sanity check (seems to be needed for some streams) */ if( p_sys->p_ff_pic->pict_type == AV_PICTURE_TYPE_B) { p_sys->b_has_b_frames = true; } /* Compute the PTS */ mtime_t i_pts = p_sys->p_ff_pic->pkt_pts; if (i_pts <= VLC_TS_INVALID) i_pts = p_sys->p_ff_pic->pkt_dts; if( i_pts <= VLC_TS_INVALID ) i_pts = p_sys->i_pts; /* Interpolate the next PTS */ if( i_pts > VLC_TS_INVALID ) p_sys->i_pts = i_pts; if( p_sys->i_pts > VLC_TS_INVALID ) { /* interpolate the next PTS */ if( p_dec->fmt_in.video.i_frame_rate > 0 && p_dec->fmt_in.video.i_frame_rate_base > 0 ) { p_sys->i_pts += INT64_C(1000000) * (2 + p_sys->p_ff_pic->repeat_pict) * p_dec->fmt_in.video.i_frame_rate_base / (2 * p_dec->fmt_in.video.i_frame_rate); } else if( p_context->time_base.den > 0 ) { int i_tick = p_context->ticks_per_frame; if( i_tick <= 0 ) i_tick = 1; p_sys->i_pts += INT64_C(1000000) * (2 + p_sys->p_ff_pic->repeat_pict) * i_tick * p_context->time_base.num / (2 * p_context->time_base.den); } } /* Update frame late count (except when doing preroll) */ mtime_t i_display_date = 0; if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) ) i_display_date = decoder_GetDisplayDate( p_dec, i_pts ); if( i_display_date > 0 && i_display_date <= mdate() ) { p_sys->i_late_frames++; if( p_sys->i_late_frames == 1 ) p_sys->i_late_frames_start = mdate(); } else { p_sys->i_late_frames = 0; } if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) ) continue; if( p_sys->p_va != NULL || p_sys->p_ff_pic->opaque == NULL ) { /* Get a new picture */ p_pic = ffmpeg_NewPictBuf( p_dec, p_context ); if( !p_pic ) { if( p_block ) block_Release( p_block ); return NULL; } /* Fill p_picture_t from AVVideoFrame and do chroma conversion * if needed */ ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic ); } else { p_pic = (picture_t *)p_sys->p_ff_pic->opaque; decoder_LinkPicture( p_dec, p_pic ); } if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den ) { /* Fetch again the aspect ratio in case it changed */ p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num; p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den; if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den ) { p_dec->fmt_out.video.i_sar_num = 1; p_dec->fmt_out.video.i_sar_den = 1; } } /* Send decoded frame to vout */ if( i_pts > VLC_TS_INVALID) { p_pic->date = i_pts; if( p_sys->b_first_frame ) { /* Hack to force display of still pictures */ p_sys->b_first_frame = false; p_pic->b_force = true; } p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict; p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame; p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first; return p_pic; } else { decoder_DeletePicture( p_dec, p_pic ); } } if( p_block ) block_Release( p_block ); return NULL; }