From 05c7de20c4e33f9a16a2f60b0a6c703dbd9662fe Mon Sep 17 00:00:00 2001 From: Rolf Ahrenberg Date: Fri, 20 Jul 2018 23:06:01 +0300 Subject: [PATCH] Remove VideoSwitch parameter with some cleanups. --- audio.c | 2 +- codec.c | 18 ++++++++---------- codec.h | 2 +- vaapidev.c | 10 +--------- vaapidevice.cpp | 12 +++++++----- video.c | 50 ++++++++++++++++++++++--------------------------- 6 files changed, 40 insertions(+), 54 deletions(-) diff --git a/audio.c b/audio.c index 43a992b3..7855887c 100644 --- a/audio.c +++ b/audio.c @@ -857,7 +857,7 @@ static int AlsaThread(void) } // wait for space in kernel buffers if ((err = snd_pcm_wait(AlsaPCMHandle, 24)) < 0) { - Error("audio/alsa: wait underrun error? '%s'", snd_strerror(err)); + Debug("audio/alsa: wait underrun error? '%s'", snd_strerror(err)); err = snd_pcm_recover(AlsaPCMHandle, err, 0); if (err >= 0) { continue; diff --git a/codec.c b/codec.c index 1661763a..e54c178c 100644 --- a/codec.c +++ b/codec.c @@ -450,7 +450,7 @@ void CodecVideoFlushBuffers(VideoDecoder * decoder) ** ** @param decoder video decoder data */ -const char* CodecVideoGetCodecName(VideoDecoder * decoder) +const char *CodecVideoGetCodecName(VideoDecoder * decoder) { if (decoder && decoder->VideoCodec) return avcodec_get_name(decoder->VideoCodec->id); @@ -458,7 +458,6 @@ const char* CodecVideoGetCodecName(VideoDecoder * decoder) return avcodec_get_name(AV_CODEC_ID_NONE); } - //---------------------------------------------------------------------------- // Audio //---------------------------------------------------------------------------- @@ -567,7 +566,7 @@ void CodecAudioOpen(AudioDecoder * audio_decoder, int codec_id_old) switch (device_get_atype()) { case 0: break; - case 0x3: /* FALLTHRU */ + case 0x3: /* FALLTHRU */ case 0x4: codec_id = AV_CODEC_ID_MP2; break; @@ -600,7 +599,7 @@ void CodecAudioOpen(AudioDecoder * audio_decoder, int codec_id_old) audio_decoder->AudioFmtCtx->audio_codec_id = codec_id; av_dict_set_int(&options, "analyzeduration", 500, 0); - av_dict_set_int(&options, "probesize", alloc_size / 2 , 0); + av_dict_set_int(&options, "probesize", alloc_size / 2, 0); input_format = av_find_input_format("mpeg"); if (!input_format) { @@ -622,21 +621,18 @@ void CodecAudioOpen(AudioDecoder * audio_decoder, int codec_id_old) Error("codec: failed to add audio stream to context"); goto error_avformat_new_stream; } - -#if 1 audio_decoder->AudioFmtCtx->streams[0]->codecpar->codec_id = codec_id; audio_decoder->AudioFmtCtx->streams[0]->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; -#endif -#if 1 + ret = avformat_find_stream_info(audio_decoder->AudioFmtCtx, NULL); if (ret < 0) { Error("codec: can't find audio stream info: %s", av_err2str(ret)); goto error_avformat_find_stream_info; } -#endif av_dump_format(audio_decoder->AudioFmtCtx, 0, "vaapidevice audio", 0); -#if 0 +//#define AVFORMAT_FIND_BEST_AUDIO_STREAM +#ifdef AVFORMAT_FIND_BEST_AUDIO_STREAM ret = av_find_best_stream(audio_decoder->AudioFmtCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &audio_codec, 0); if (ret < 0) { Error("codec: can't find best audio stream: %s", av_err2str(ret)); @@ -704,7 +700,9 @@ void CodecAudioOpen(AudioDecoder * audio_decoder, int codec_id_old) error_avcodec_alloc_context3: error_avcodec_find_decoder: error_avformat_new_stream: +#ifdef AVFORMAT_FIND_BEST_AUDIO_STREAM error_avformat_find_best_stream: +#endif error_avformat_find_stream_info: error_avformat_open_input: av_freep(avio_ctx); diff --git a/codec.h b/codec.h index 3e0ada47..b4a9276e 100644 --- a/codec.h +++ b/codec.h @@ -75,7 +75,7 @@ extern void CodecVideoDecode(VideoDecoder *); extern void CodecVideoFlushBuffers(VideoDecoder *); /// Get video decoder name. -extern const char* CodecVideoGetCodecName(VideoDecoder *); +extern const char *CodecVideoGetCodecName(VideoDecoder *); /// Allocate a new audio decoder context. extern AudioDecoder *CodecAudioNewDecoder(void); diff --git a/vaapidev.c b/vaapidev.c index 4dbda95f..9d620d1c 100644 --- a/vaapidev.c +++ b/vaapidev.c @@ -36,7 +36,6 @@ #include "video.h" #include "codec.h" - ////////////////////////////////////////////////////////////////////////////// // Variables ////////////////////////////////////////////////////////////////////////////// @@ -482,11 +481,6 @@ struct __video_stream__ static VideoStream MyVideoStream[1]; ///< normal video stream -#ifdef DEBUG -uint32_t VideoSwitch; ///< debug video switch ticks -static int VideoMaxPacketSize; ///< biggest used packet buffer -#endif - const char *X11DisplayName; ///< x11 display name static volatile char Usr1Signal; ///< true got usr1 signal @@ -1473,9 +1467,6 @@ int Start(void) */ void Stop(void) { -#ifdef DEBUG - Debug1("video: max used PES packet size: %d", VideoMaxPacketSize); -#endif } /** @@ -1613,6 +1604,7 @@ char *GetVideoStats(void) char *GetVideoInfo(void) { const char *codec = CodecVideoGetCodecName(MyVideoStream->Decoder); + return MyVideoStream->HwDecoder ? VideoGetInfo(MyVideoStream->HwDecoder, codec) : NULL; } diff --git a/vaapidevice.cpp b/vaapidevice.cpp index 51075891..d65a6bd5 100644 --- a/vaapidevice.cpp +++ b/vaapidevice.cpp @@ -1979,6 +1979,7 @@ void cVaapiDevice::GetOsdSize(int &width, int &height, double &pixel_aspect) int cVaapiDevice::PlayAudio(const uchar * data, int length, uchar id) { const int ringBufferSize = KILOBYTE(512); + if (length > ringBufferSize) { Error("Audio PES packet size (%d) too large for frame ringbuffer", length); return 0; @@ -1993,6 +1994,7 @@ int cVaapiDevice::PlayAudio(const uchar * data, int length, uchar id) if (audioEof || !audioBuffer->Put(packet)) { // If this happens often enough vdr will start asking for a clear of device delete packet; + return 0; } // TODO: remove vaapidev code @@ -2040,6 +2042,7 @@ void cVaapiDevice::SetVolumeDevice(int volume) int cVaapiDevice::PlayVideo(const uchar * data, int length) { const int ringBufferSize = MEGABYTE(1); + if (length > ringBufferSize) { Error("Video PES packet size (%d) too large for frame ringbuffer", length); return 0; @@ -2054,6 +2057,7 @@ int cVaapiDevice::PlayVideo(const uchar * data, int length) if (videoEof || !videoBuffer->Put(packet)) { // If this happens often enough vdr will start asking for a clear of device delete packet; + return 0; } @@ -2118,10 +2122,9 @@ int cVaapiDevice::DeviceGetAtype() eTrackType track = GetCurrentAudioTrack(); if (IS_AUDIO_TRACK(track)) { - return PatPmtParser()->Atype(int(track - ttAudioFirst)); - } - else if (IS_DOLBY_TRACK(track)) { - return PatPmtParser()->Dtype(int(track - ttDolbyFirst)); + return PatPmtParser()->Atype(int (track - ttAudioFirst)); + } else if (IS_DOLBY_TRACK(track)) { + return PatPmtParser()->Dtype(int (track - ttDolbyFirst)); } return -1; @@ -2203,7 +2206,6 @@ int cVaapiDevice::DeviceAudioReadCallback(uchar * data, int size) } while (!packet && ffmpegMode == 0 && retries++ < 3); - if (!packet) { return ffmpegMode ? 0 : AVERROR_EOF; } diff --git a/video.c b/video.c index 7dcfd5ea..9a0449c8 100644 --- a/video.c +++ b/video.c @@ -298,9 +298,6 @@ static xcb_atom_t WmDeleteWindowAtom; ///< WM delete message atom static xcb_atom_t NetWmState; ///< wm-state message atom static xcb_atom_t NetWmStateFullscreen; ///< fullscreen wm-state message atom -#ifdef DEBUG -extern uint32_t VideoSwitch; ///< ticks for channel switch -#endif extern void AudioVideoReady(int64_t); ///< tell audio video is ready extern int IsReplay(void); @@ -366,7 +363,6 @@ static void VideoSetPts(int64_t * pts_p, int interlaced, const AVCodecContext * // update video clock if (*pts_p != (int64_t) AV_NOPTS_VALUE) { *pts_p += duration * 90; - //Info("video: %s +pts", Timestamp2String(*pts_p)); } //av_opt_ptr(avcodec_get_frame_class(), frame, "best_effort_timestamp"); //pts = frame->best_effort_timestamp; @@ -1354,6 +1350,11 @@ static VaapiDecoder *VaapiNewHwDecoder(VideoStream * stream) decoder->LastAudioPTS = 0; decoder->SyncCounter = 0; decoder->StartCounter = 0; + decoder->FramesDuped = 0; + decoder->FramesMissed = 0; + decoder->FramesDropped = 0; + decoder->FrameCounter = 0; + decoder->FramesDisplayed = 0; VaapiDecoders[VaapiDecoderN++] = decoder; @@ -1440,9 +1441,12 @@ static void VaapiCleanup(VaapiDecoder * decoder) decoder->PostProcSurfaceWrite = 0; decoder->SyncCounter = 0; + decoder->StartCounter = 0; + decoder->FramesDuped = 0; + decoder->FramesMissed = 0; + decoder->FramesDropped = 0; decoder->FrameCounter = 0; decoder->FramesDisplayed = 0; - decoder->StartCounter = 0; decoder->Closing = 0; decoder->PTS = AV_NOPTS_VALUE; VideoDeltaPTS = 0; @@ -3615,7 +3619,8 @@ char *VaapiGetStats(VaapiDecoder * decoder) if (snprintf(&buffer[0], sizeof(buffer), " Frames: missed(%d) duped(%d) dropped(%d) total(%d) PTS(%s) drift(%" PRId64 ") audio(%" PRId64 ") video(%" PRId64 ")", decoder->FramesMissed, decoder->FramesDuped, decoder->FramesDropped, decoder->FrameCounter, - Timestamp2String(video_clock), (video_clock - audio_clock) / 90, AudioGetDelay() / 90, VideoDeltaPTS / 90)) { + Timestamp2String(video_clock), (video_clock - audio_clock) / 90, AudioGetDelay() / 90, + VideoDeltaPTS / 90)) { return strdup(buffer); } @@ -3730,7 +3735,7 @@ static void VaapiSyncDecoder(VaapiDecoder * decoder) // Calc correct diff err = VaapiMessage(1, "AV-PTS Rollover, correcting AV-PTS Difference"); - diff = (video_clock - audio_clock - (int64_t) VideoAudioDelay); + diff = video_clock - audio_clock - (int64_t) VideoAudioDelay; } // Low-Pass Filter AV-PTS-Diff, if in Sync-Range of +/- 250 ms if (abs(diff) < (int64_t) 250 * 90) { @@ -3762,11 +3767,7 @@ static void VaapiSyncDecoder(VaapiDecoder * decoder) } #if defined(DEBUG) || defined(AV_INFO) if (!decoder->SyncCounter && decoder->StartCounter < 1000) { -#ifdef DEBUG - Debug7("video/vaapi: synced after %d frames %dms", decoder->StartCounter, GetMsTicks() - VideoSwitch); -#else Info("video/vaapi: synced after %d frames", decoder->StartCounter); -#endif decoder->StartCounter += 1000; } #endif @@ -3779,7 +3780,7 @@ static void VaapiSyncDecoder(VaapiDecoder * decoder) ++decoder->FramesDuped; // FIXME: don't warn after stream start, don't warn during pause err = - VaapiMessage(0, "video: decoder buffer empty, duping frame (%u/%u) %d v-buf", decoder->FramesDuped, + VaapiMessage(0, "video: decoder buffer empty, duping frame (%d/%d) %d v-buf", decoder->FramesDuped, decoder->FrameCounter, VideoGetBuffers(decoder->Stream)); // some time no new picture if (decoder->Closing < -300) { @@ -3798,11 +3799,13 @@ static void VaapiSyncDecoder(VaapiDecoder * decoder) if (!err) { VaapiMessage(0, NULL); } - Info("video: %s%+5" PRId64 " %4" PRId64 " %3d/\\ms %3d%+d v-buf", Timestamp2String(video_clock), - (video_clock - audio_clock) / 90, AudioGetDelay() / 90, (int)VideoDeltaPTS / 90, - VideoGetBuffers(decoder->Stream), decoder->Interlaced ? - (2 * atomic_read(&decoder->SurfacesFilled) - decoder->SurfaceField) : - atomic_read(&decoder->SurfacesFilled)); + if (audio_clock != (int64_t) AV_NOPTS_VALUE && video_clock != (int64_t) AV_NOPTS_VALUE) { + Info("video: %s%+5" PRId64 " %4" PRId64 " %3d/\\ms %3d%+d v-buf", Timestamp2String(video_clock), + (video_clock - audio_clock) / 90, AudioGetDelay() / 90, (int)VideoDeltaPTS / 90, + VideoGetBuffers(decoder->Stream), + decoder->Interlaced ? (2 * atomic_read(&decoder->SurfacesFilled) - + decoder->SurfaceField) : atomic_read(&decoder->SurfacesFilled)); + } if (!(decoder->FramesDisplayed % (5 * 60 * 60))) { VaapiPrintFrames(decoder); } @@ -3844,12 +3847,6 @@ static void VaapiSyncDisplayFrame(void) /// static void VaapiSyncRenderFrame(VaapiDecoder * decoder, const AVCodecContext * video_ctx, const AVFrame * frame) { -#ifdef DEBUG - if (!atomic_read(&decoder->SurfacesFilled)) { - Debug7("video: new stream frame %dms", GetMsTicks() - VideoSwitch); - } -#endif - // if video output buffer is full, wait and display surface. // loop for interlace if (atomic_read(&decoder->SurfacesFilled) >= VIDEO_SURFACES_MAX - 1) { @@ -4726,14 +4723,11 @@ enum AVPixelFormat Video_get_format(VideoHwDecoder * hw_decoder, AVCodecContext const enum AVPixelFormat *fmt) { #ifdef DEBUG - int ms_delay; - // FIXME: use frame time - ms_delay = (1000 * video_ctx->time_base.num * video_ctx->ticks_per_frame) + int ms_delay = (1000 * video_ctx->time_base.num * video_ctx->ticks_per_frame) / video_ctx->time_base.den; - Debug7("video: ready %s %2dms/frame %dms", Timestamp2String(VideoGetClock(hw_decoder)), ms_delay, - GetMsTicks() - VideoSwitch); + Debug7("video: ready %s %2dms/frame", Timestamp2String(VideoGetClock(hw_decoder)), ms_delay); #endif return VideoUsedModule->get_format(hw_decoder, video_ctx, fmt);