@@ -893,17 +893,28 @@ std::pair<int, const error_category *> CodecContext2::decodeCommon(AVFrame *outF
893893 if (!decodeProc)
894894 return make_error_pair (Errors::CodecInvalidDecodeProc);
895895
896- if (offset && inPacket. size () && offset >= inPacket. size ())
897- return make_error_pair (Errors::CodecDecodingOffsetToLarge) ;
898-
896+ // sizeof(AVPacket) is not a part of the public API
897+ std::unique_ptr<AVPacket, av::SmartDeleter> pkt ;
898+ auto processPkt = inPacket. raw ();
899899 frameFinished = 0 ;
900900
901- AVPacket pkt = *inPacket.raw ();
902- pkt.data += offset;
903- pkt.size -= offset;
901+ if (inPacket.raw () && offset) {
902+ if (offset && inPacket.size () && offset >= inPacket.size ())
903+ return make_error_pair (Errors::CodecDecodingOffsetToLarge);
904+
905+ std::error_code ec;
906+ pkt.reset (inPacket.makeRef (ec));
907+ if (ec) {
908+ return {ec.value (), &ec.category ()};
909+ }
910+
911+ pkt->data += offset;
912+ pkt->size -= offset;
913+ processPkt = pkt.get ();
914+ }
904915
905- int decoded = decodeProc (m_raw, outFrame, &frameFinished, &pkt);
906- return make_error_pair (decoded );
916+ // not a "flush" internally: just read-out Frame from output queue
917+ return make_error_pair (decodeProc (m_raw, outFrame, &frameFinished, processPkt) );
907918}
908919
909920std::pair<int , const error_category *> CodecContext2::encodeCommon (Packet &outPacket, const AVFrame *inFrame, int &gotPacket, int (*encodeProc)(AVCodecContext *, AVPacket *, const AVFrame *, int *)) noexcept
@@ -1028,7 +1039,7 @@ CodecContext2::decodeCommon(T &outFrame,
10281039
10291040 // Dial with PTS/DTS in packet/stream timebase
10301041
1031- if (inPacket.timeBase () != Rational ())
1042+ if (inPacket.raw () && inPacket. timeBase () != Rational ())
10321043 outFrame.setTimeBase (inPacket.timeBase ());
10331044#if AVCPP_HAS_AVFORMAT
10341045 else
@@ -1051,7 +1062,7 @@ CodecContext2::decodeCommon(T &outFrame,
10511062 // Convert to decoder/frame time base. Seems not nessesary.
10521063 outFrame.setTimeBase (timeBase ());
10531064
1054- if (inPacket)
1065+ if (inPacket. raw () && inPacket )
10551066 outFrame.setStreamIndex (inPacket.streamIndex ());
10561067#if AVCPP_HAS_AVFORMAT
10571068 else
0 commit comments