@@ -144,7 +144,7 @@ BetaCudaDeviceInterface::BetaCudaDeviceInterface(const torch::Device& device)
144
144
145
145
BetaCudaDeviceInterface::~BetaCudaDeviceInterface () {
146
146
// 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_ ?
148
148
149
149
if (decoder_) {
150
150
NVDECCache::getCache (device_.index ())
@@ -328,40 +328,34 @@ int BetaCudaDeviceInterface::frameReadyForDecoding(CUVIDPICPARAMS* picParams) {
328
328
329
329
// Send frame to be decoded by NVDEC - non-blocking call.
330
330
CUresult result = cuvidDecodePicture (*decoder_.get (), picParams);
331
- if (result != CUDA_SUCCESS) {
332
- return 0 ; // Yes, you're reading that right, 0 means error.
333
- }
334
331
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) ;
337
334
}
338
335
339
336
int BetaCudaDeviceInterface::frameReadyInDisplayOrder (
340
337
CUVIDPARSERDISPINFO* dispInfo) {
341
- frameBuffer_.markSlotReadyAndSetInfo (
342
- /* slotId=*/ dispInfo->picture_index , dispInfo);
343
- return 1 ;
338
+ readyFrames_.push (*dispInfo);
339
+ return 1 ; // success
344
340
}
345
341
346
342
// Moral equivalent of avcodec_receive_frame().
347
343
int BetaCudaDeviceInterface::receiveFrame (UniqueAVFrame& avFrame) {
348
- FrameBuffer::Slot* slot = frameBuffer_.findReadySlotWithLowestPts ();
349
- if (slot == nullptr ) {
344
+ if (readyFrames_.empty ()) {
350
345
// No frame found, instruct caller to try again later after sending more
351
346
// packets.
352
347
return AVERROR (EAGAIN);
353
348
}
349
+ CUVIDPARSERDISPINFO dispInfo = readyFrames_.front ();
350
+ readyFrames_.pop ();
354
351
355
352
CUVIDPROCPARAMS procParams = {};
356
- CUVIDPARSERDISPINFO dispInfo = slot->dispInfo ;
357
353
procParams.progressive_frame = dispInfo.progressive_frame ;
358
354
procParams.top_field_first = dispInfo.top_field_first ;
359
355
procParams.unpaired_field = dispInfo.repeat_first_field < 0 ;
360
356
CUdeviceptr framePtr = 0 ;
361
357
unsigned int pitch = 0 ;
362
358
363
- frameBuffer_.free (slot->slotId );
364
-
365
359
// We know the frame we want was sent to the hardware decoder, but now we need
366
360
// to "map" it to an "output surface" before we can use its data. This is a
367
361
// blocking calls that waits until the frame is fully decoded and ready to be
@@ -478,7 +472,8 @@ void BetaCudaDeviceInterface::flush() {
478
472
479
473
isFlushing_ = false ;
480
474
481
- frameBuffer_.clear ();
475
+ std::queue<CUVIDPARSERDISPINFO> emptyQueue;
476
+ std::swap (readyFrames_, emptyQueue);
482
477
483
478
eofSent_ = false ;
484
479
}
@@ -512,57 +507,4 @@ void BetaCudaDeviceInterface::convertAVFrameToFrameOutput(
512
507
preAllocatedOutputTensor);
513
508
}
514
509
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
-
568
510
} // namespace facebook::torchcodec
0 commit comments