@@ -54,7 +54,8 @@ class DeviceInterface {
5454 // Initialize the device with parameters generic to all kinds of decoding.
5555 virtual void initialize (
5656 const AVStream* avStream,
57- const UniqueDecodingAVFormatContext& avFormatCtx) = 0;
57+ const UniqueDecodingAVFormatContext& avFormatCtx,
58+ const SharedAVCodecContext& codecContext) = 0;
5859
5960 // Initialize the device with parameters specific to video decoding. There is
6061 // a default empty implementation.
@@ -80,23 +81,14 @@ class DeviceInterface {
8081 // Extension points for custom decoding paths
8182 // ------------------------------------------
8283
83- // Set the codec context for default FFmpeg decoding operations
84- // This must be called during initialization before using
85- // sendPacket/receiveFrame
86- virtual void setCodecContext (AVCodecContext* codecContext) {
87- codecContext_ = codecContext;
88- }
89-
9084 // Returns AVSUCCESS on success, AVERROR(EAGAIN) if decoder queue full, or
9185 // other AVERROR on failure
9286 // Default implementation uses FFmpeg directly
9387 virtual int sendPacket (ReferenceAVPacket& avPacket) {
94- if (!codecContext_) {
95- TORCH_CHECK (
96- false , " Codec context not available for default packet sending" );
97- return AVERROR (EINVAL);
98- }
99- return avcodec_send_packet (codecContext_, avPacket.get ());
88+ TORCH_CHECK (
89+ codecContext_ != nullptr ,
90+ " Codec context not available for default packet sending" );
91+ return avcodec_send_packet (codecContext_.get (), avPacket.get ());
10092 }
10193
10294 // Send an EOF packet to flush the decoder
@@ -107,29 +99,30 @@ class DeviceInterface {
10799 TORCH_CHECK (false , " Codec context not available for EOF packet sending" );
108100 return AVERROR (EINVAL);
109101 }
110- return avcodec_send_packet (codecContext_, nullptr );
102+ return avcodec_send_packet (codecContext_. get () , nullptr );
111103 }
112104
113105 // Returns AVSUCCESS on success, AVERROR(EAGAIN) if no frame ready,
114106 // AVERROR_EOF if end of stream, or other AVERROR on failure
115107 // Default implementation uses FFmpeg directly
116108 virtual int receiveFrame (UniqueAVFrame& avFrame) {
117- if (!codecContext_) {
118- TORCH_CHECK (false , " Codec context not available for frame receiving" );
119- return AVERROR (EINVAL);
120- }
121- return avcodec_receive_frame (codecContext_, avFrame.get ());
109+ TORCH_CHECK (
110+ codecContext_ != nullptr ,
111+ " Codec context not available for default frame receiving" );
112+ return avcodec_receive_frame (codecContext_.get (), avFrame.get ());
122113 }
123114
124115 // Flush remaining frames from decoder
125116 virtual void flush () {
126- // Default implementation is no-op for standard decoders
127- // Custom decoders can override this method
117+ TORCH_CHECK (
118+ codecContext_ != nullptr ,
119+ " Codec context not available for default flushing" );
120+ avcodec_flush_buffers (codecContext_.get ());
128121 }
129122
130123 protected:
131124 torch::Device device_;
132- AVCodecContext* codecContext_ = nullptr ; // Non-owning pointer
125+ SharedAVCodecContext codecContext_;
133126};
134127
135128using CreateDeviceInterfaceFn =
0 commit comments