@@ -144,7 +144,7 @@ BetaCudaDeviceInterface::BetaCudaDeviceInterface(const torch::Device& device)
144144
145145BetaCudaDeviceInterface::~BetaCudaDeviceInterface () {
146146 // TODONVDEC P0: we probably need to free the frames that have been decoded by
147- // NVDEC but not yet "mapped" - i.e. those that are still in frameBuffer_ ?
147+ // NVDEC but not yet "mapped" - i.e. those that are still in readyFrames_ ?
148148
149149 if (decoder_) {
150150 NVDECCache::getCache (device_.index ())
@@ -328,40 +328,34 @@ int BetaCudaDeviceInterface::frameReadyForDecoding(CUVIDPICPARAMS* picParams) {
328328
329329 // Send frame to be decoded by NVDEC - non-blocking call.
330330 CUresult result = cuvidDecodePicture (*decoder_.get (), picParams);
331- if (result != CUDA_SUCCESS) {
332- return 0 ; // Yes, you're reading that right, 0 means error.
333- }
334331
335- frameBuffer_. markAsBeingDecoded ( /* slotId= */ picParams-> CurrPicIdx );
336- return 1 ;
332+ // Yes, you're reading that right, 0 means error, 1 means success
333+ return (result == CUDA_SUCCESS) ;
337334}
338335
339336int BetaCudaDeviceInterface::frameReadyInDisplayOrder (
340337 CUVIDPARSERDISPINFO* dispInfo) {
341- frameBuffer_.markSlotReadyAndSetInfo (
342- /* slotId=*/ dispInfo->picture_index , dispInfo);
343- return 1 ;
338+ readyFrames_.push (*dispInfo);
339+ return 1 ; // success
344340}
345341
346342// Moral equivalent of avcodec_receive_frame().
347343int BetaCudaDeviceInterface::receiveFrame (UniqueAVFrame& avFrame) {
348- FrameBuffer::Slot* slot = frameBuffer_.findReadySlotWithLowestPts ();
349- if (slot == nullptr ) {
344+ if (readyFrames_.empty ()) {
350345 // No frame found, instruct caller to try again later after sending more
351346 // packets.
352347 return AVERROR (EAGAIN);
353348 }
349+ CUVIDPARSERDISPINFO dispInfo = readyFrames_.front ();
350+ readyFrames_.pop ();
354351
355352 CUVIDPROCPARAMS procParams = {};
356- CUVIDPARSERDISPINFO dispInfo = slot->dispInfo ;
357353 procParams.progressive_frame = dispInfo.progressive_frame ;
358354 procParams.top_field_first = dispInfo.top_field_first ;
359355 procParams.unpaired_field = dispInfo.repeat_first_field < 0 ;
360356 CUdeviceptr framePtr = 0 ;
361357 unsigned int pitch = 0 ;
362358
363- frameBuffer_.free (slot->slotId );
364-
365359 // We know the frame we want was sent to the hardware decoder, but now we need
366360 // to "map" it to an "output surface" before we can use its data. This is a
367361 // blocking calls that waits until the frame is fully decoded and ready to be
@@ -478,7 +472,8 @@ void BetaCudaDeviceInterface::flush() {
478472
479473 isFlushing_ = false ;
480474
481- frameBuffer_.clear ();
475+ std::queue<CUVIDPARSERDISPINFO> emptyQueue;
476+ std::swap (readyFrames_, emptyQueue);
482477
483478 eofSent_ = false ;
484479}
@@ -512,57 +507,4 @@ void BetaCudaDeviceInterface::convertAVFrameToFrameOutput(
512507 preAllocatedOutputTensor);
513508}
514509
515- void BetaCudaDeviceInterface::FrameBuffer::markAsBeingDecoded (int slotId) {
516- auto it = map_.find (slotId);
517- TORCH_CHECK (
518- it == map_.end (),
519- " Slot " ,
520- slotId,
521- " is already occupied. This should never happen." );
522-
523- map_.emplace (slotId, Slot (slotId, SlotState::BEING_DECODED));
524- }
525-
526- void BetaCudaDeviceInterface::FrameBuffer::markSlotReadyAndSetInfo (
527- int slotId,
528- CUVIDPARSERDISPINFO* dispInfo) {
529- auto it = map_.find (slotId);
530- TORCH_CHECK (
531- it != map_.end (),
532- " Could not find matching slot with slotId " ,
533- slotId,
534- " . This should never happen." );
535-
536- TORCH_CHECK (
537- it->second .state == SlotState::BEING_DECODED,
538- " Slot " ,
539- slotId,
540- " is not in BEING_DECODED state. This should never happen." );
541- it->second .state = SlotState::READY_FOR_OUTPUT;
542- it->second .dispInfo = *dispInfo;
543- }
544-
545- void BetaCudaDeviceInterface::FrameBuffer::free (int slotId) {
546- auto it = map_.find (slotId);
547- TORCH_CHECK (
548- it != map_.end (),
549- " Tried to free non-existing slot with slotId" ,
550- slotId,
551- " . This should never happen." );
552- map_.erase (it);
553- }
554-
555- BetaCudaDeviceInterface::FrameBuffer::Slot*
556- BetaCudaDeviceInterface::FrameBuffer::findReadySlotWithLowestPts () {
557- Slot* outputSlot = nullptr ;
558- for (auto & [_, slot] : map_) {
559- if (slot.state == SlotState::READY_FOR_OUTPUT &&
560- (outputSlot == nullptr ||
561- slot.dispInfo .timestamp < outputSlot->dispInfo .timestamp )) {
562- outputSlot = &slot;
563- }
564- }
565- return outputSlot;
566- }
567-
568510} // namespace facebook::torchcodec
0 commit comments