@@ -68,6 +68,7 @@ bool cv::cudacodec::detail::VideoParser::parseVideoData(const unsigned char* dat
6868 CUVIDSOURCEDATAPACKET packet;
6969 std::memset (&packet, 0 , sizeof (CUVIDSOURCEDATAPACKET));
7070
71+ packet.flags = CUVID_PKT_TIMESTAMP;
7172 if (endOfStream)
7273 packet.flags |= CUVID_PKT_ENDOFSTREAM;
7374
@@ -107,68 +108,69 @@ int CUDAAPI cv::cudacodec::detail::VideoParser::HandleVideoSequence(void* userDa
107108
108109 thiz->unparsedPackets_ = 0 ;
109110
110- if (format->codec != thiz->videoDecoder_ ->codec () ||
111- format->coded_width != thiz->videoDecoder_ ->frameWidth () ||
112- format->coded_height != thiz->videoDecoder_ ->frameHeight () ||
113- format->chroma_format != thiz->videoDecoder_ ->chromaFormat ()||
114- format->bit_depth_luma_minus8 != thiz->videoDecoder_ ->nBitDepthMinus8 () ||
115- format->min_num_decode_surfaces != thiz->videoDecoder_ ->nDecodeSurfaces ())
111+ FormatInfo newFormat;
112+ newFormat.videoFullRangeFlag = format->video_signal_description .video_full_range_flag ;
113+ newFormat.codec = static_cast <Codec>(format->codec );
114+ newFormat.chromaFormat = static_cast <ChromaFormat>(format->chroma_format );
115+ newFormat.nBitDepthMinus8 = format->bit_depth_luma_minus8 ;
116+ newFormat.nBitDepthChromaMinus8 = format->bit_depth_chroma_minus8 ;
117+ newFormat.ulWidth = format->coded_width ;
118+ newFormat.ulHeight = format->coded_height ;
119+ newFormat.fps = format->frame_rate .numerator / static_cast <float >(format->frame_rate .denominator );
120+ newFormat.targetSz = thiz->videoDecoder_ ->getTargetSz ();
121+ newFormat.srcRoi = thiz->videoDecoder_ ->getSrcRoi ();
122+ if (newFormat.srcRoi .empty ()) {
123+ newFormat.displayArea = Rect (Point (format->display_area .left , format->display_area .top ), Point (format->display_area .right , format->display_area .bottom ));
124+ if (newFormat.targetSz .empty ())
125+ newFormat.targetSz = Size ((format->display_area .right - format->display_area .left ), (format->display_area .bottom - format->display_area .top ));
126+ }
127+ else
128+ newFormat.displayArea = newFormat.srcRoi ;
129+ newFormat.width = newFormat.targetSz .width ? newFormat.targetSz .width : format->coded_width ;
130+ newFormat.height = newFormat.targetSz .height ? newFormat.targetSz .height : format->coded_height ;
131+ newFormat.targetRoi = thiz->videoDecoder_ ->getTargetRoi ();
132+ newFormat.ulNumDecodeSurfaces = min (!thiz->allowFrameDrop_ ? max (thiz->videoDecoder_ ->nDecodeSurfaces (), static_cast <int >(format->min_num_decode_surfaces )) :
133+ format->min_num_decode_surfaces * 2 , 32 );
134+ if (format->progressive_sequence )
135+ newFormat.deinterlaceMode = Weave;
136+ else
137+ newFormat.deinterlaceMode = Adaptive;
138+ int maxW = 0 , maxH = 0 ;
139+ // AV1 has max width/height of sequence in sequence header
140+ if (format->codec == cudaVideoCodec_AV1 && format->seqhdr_data_length > 0 )
116141 {
117- FormatInfo newFormat;
118- newFormat.videoFullRangeFlag = format->video_signal_description .video_full_range_flag ;
119- newFormat.codec = static_cast <Codec>(format->codec );
120- newFormat.chromaFormat = static_cast <ChromaFormat>(format->chroma_format );
121- newFormat.nBitDepthMinus8 = format->bit_depth_luma_minus8 ;
122- newFormat.ulWidth = format->coded_width ;
123- newFormat.ulHeight = format->coded_height ;
124- newFormat.fps = format->frame_rate .numerator / static_cast <float >(format->frame_rate .denominator );
125- newFormat.targetSz = thiz->videoDecoder_ ->getTargetSz ();
126- newFormat.srcRoi = thiz->videoDecoder_ ->getSrcRoi ();
127- if (newFormat.srcRoi .empty ()) {
128- newFormat.displayArea = Rect (Point (format->display_area .left , format->display_area .top ), Point (format->display_area .right , format->display_area .bottom ));
129- if (newFormat.targetSz .empty ())
130- newFormat.targetSz = Size ((format->display_area .right - format->display_area .left ), (format->display_area .bottom - format->display_area .top ));
131- }
132- else
133- newFormat.displayArea = newFormat.srcRoi ;
134- newFormat.width = newFormat.targetSz .width ? newFormat.targetSz .width : format->coded_width ;
135- newFormat.height = newFormat.targetSz .height ? newFormat.targetSz .height : format->coded_height ;
136- newFormat.targetRoi = thiz->videoDecoder_ ->getTargetRoi ();
137- newFormat.ulNumDecodeSurfaces = min (!thiz->allowFrameDrop_ ? max (thiz->videoDecoder_ ->nDecodeSurfaces (), static_cast <int >(format->min_num_decode_surfaces )) :
138- format->min_num_decode_surfaces * 2 , 32 );
139- if (format->progressive_sequence )
140- newFormat.deinterlaceMode = Weave;
141- else
142- newFormat.deinterlaceMode = Adaptive;
143- int maxW = 0 , maxH = 0 ;
144- // AV1 has max width/height of sequence in sequence header
145- if (format->codec == cudaVideoCodec_AV1 && format->seqhdr_data_length > 0 )
146- {
147- CUVIDEOFORMATEX* vidFormatEx = (CUVIDEOFORMATEX*)format;
148- maxW = vidFormatEx->av1 .max_width ;
149- maxH = vidFormatEx->av1 .max_height ;
142+ CUVIDEOFORMATEX* vidFormatEx = (CUVIDEOFORMATEX*)format;
143+ maxW = vidFormatEx->av1 .max_width ;
144+ maxH = vidFormatEx->av1 .max_height ;
145+ }
146+ if (maxW < (int )format->coded_width )
147+ maxW = format->coded_width ;
148+ if (maxH < (int )format->coded_height )
149+ maxH = format->coded_height ;
150+ newFormat.ulMaxWidth = maxW;
151+ newFormat.ulMaxHeight = maxH;
152+
153+ thiz->frameQueue_ ->waitUntilEmpty ();
154+ int retVal = newFormat.ulNumDecodeSurfaces ;
155+ try
156+ {
157+ if (thiz->videoDecoder_ ->inited ()) {
158+ retVal = thiz->videoDecoder_ ->reconfigure (newFormat);
159+ if (retVal > 1 && newFormat.ulNumDecodeSurfaces != thiz->frameQueue_ ->getMaxSz ())
160+ thiz->frameQueue_ ->resize (newFormat.ulNumDecodeSurfaces );
150161 }
151- if (maxW < (int )format->coded_width )
152- maxW = format->coded_width ;
153- if (maxH < (int )format->coded_height )
154- maxH = format->coded_height ;
155- newFormat.ulMaxWidth = maxW;
156- newFormat.ulMaxHeight = maxH;
157- thiz->frameQueue_ ->init (newFormat.ulNumDecodeSurfaces );
158- try
159- {
160- thiz->videoDecoder_ ->release ();
162+ else {
163+ thiz->frameQueue_ ->init (newFormat.ulNumDecodeSurfaces );
161164 thiz->videoDecoder_ ->create (newFormat);
162165 }
163- catch (const cv::Exception&)
164- {
165- CV_LOG_ERROR (NULL , " Attempt to reconfigure Nvidia decoder failed!" );
166- thiz->hasError_ = true ;
167- return false ;
168- }
169166 }
170-
171- return thiz->videoDecoder_ ->nDecodeSurfaces ();
167+ catch (const cv::Exception&)
168+ {
169+ CV_LOG_ERROR (NULL , " Attempt to configure Nvidia decoder failed!" );
170+ thiz->hasError_ = true ;
171+ retVal = 0 ;
172+ }
173+ return retVal;
172174}
173175
174176int CUDAAPI cv::cudacodec::detail::VideoParser::HandlePictureDecode (void * userData, CUVIDPICPARAMS* picParams)
0 commit comments