@@ -252,6 +252,8 @@ int BetaCudaDeviceInterface::sendPacket(ReferenceAVPacket& packet) {
252252 CUVIDSOURCEDATAPACKET cuvidPacket = {};
253253
254254 if (packet.get () && packet->data && packet->size > 0 ) {
255+ applyBSF (packet);
256+
255257 // Regular packet with data
256258 cuvidPacket.payload = packet->data ;
257259 cuvidPacket.payload_size = packet->size ;
@@ -275,27 +277,36 @@ int BetaCudaDeviceInterface::sendPacket(ReferenceAVPacket& packet) {
275277 return AVSUCCESS;
276278}
277279
278- // TODONVDEC P0: cleanup this raw pointer / reference monstruosity.
279- ReferenceAVPacket* BetaCudaDeviceInterface::applyBSF (
280- ReferenceAVPacket& packet,
281- [[maybe_unused]] AutoAVPacket& filteredAutoPacket,
282- ReferenceAVPacket& filteredPacket) {
280+ void BetaCudaDeviceInterface::applyBSF (ReferenceAVPacket& packet) {
283281 if (!bitstreamFilter_) {
284- return &packet ;
282+ return ;
285283 }
284+
286285 int retVal = av_bsf_send_packet (bitstreamFilter_.get (), packet.get ());
287286 TORCH_CHECK (
288287 retVal >= AVSUCCESS,
289288 " Failed to send packet to bitstream filter: " ,
290289 getFFMPEGErrorStringFromErrorCode (retVal));
291290
291+ // Create a temporary packet to receive the filtered data
292+ // TODO P1: the docs mention there can theoretically be multiple output
293+ // packets for a single input, i.e. we may need to call av_bsf_receive_packet
294+ // more than once. We should figure out whether that applies to the BSF we're
295+ // using.
296+ AutoAVPacket filteredAutoPacket;
297+ ReferenceAVPacket filteredPacket (filteredAutoPacket);
292298 retVal = av_bsf_receive_packet (bitstreamFilter_.get (), filteredPacket.get ());
293299 TORCH_CHECK (
294300 retVal >= AVSUCCESS,
295301 " Failed to receive packet from bitstream filter: " ,
296302 getFFMPEGErrorStringFromErrorCode (retVal));
297303
298- return &filteredPacket;
304+ // Free the original packet's data which isn't needed anymore, and move the
305+ // fields of the filtered packet into the original packet. The filtered packet
306+ // fields are re-set by av_packet_move_ref, so when it goes out of scope and
307+ // gets destructed, it's not going to affect the original packet.
308+ av_packet_unref (packet.get ());
309+ av_packet_move_ref (packet.get (), filteredPacket.get ());
299310}
300311
301312// Parser triggers this callback within cuvidParseVideoData when a frame is
0 commit comments