diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 218e236e9b62cb13b22de0c46fe7b5fb2e050812..f500151e8568a71ae6a4e6072612b00525903f86 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2654,6 +2654,14 @@ void av_init_packet(AVPacket *pkt);
  */
 int av_new_packet(AVPacket *pkt, int size);
 
+/**
+ * Reduce packet size, correctly zeroing padding
+ *
+ * @param pkt packet
+ * @param size new size
+ */
+void av_shrink_packet(AVPacket *pkt, int size);
+
 /**
  * @warning This is a hack - the packet memory allocation stuff is broken. The
  * packet is allocated if it was not really allocated.
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index 107afb3817b24eb1632a14bd43a9bcc19c63e7c5..d91ee3a10d021bd9c4090ea9d2f6c36b62439bf8 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -62,6 +62,13 @@ int av_new_packet(AVPacket *pkt, int size)
     return 0;
 }
 
+void av_shrink_packet(AVPacket *pkt, int size)
+{
+    if (pkt->size <= size) return;
+    pkt->size = size;
+    memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+}
+
 int av_dup_packet(AVPacket *pkt)
 {
     if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 54c92028a998ec2884c721b8b49eb7e9079185a2..d3b9c090ebe41980bdb8b59e3ec6d3f81d44aba8 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -272,7 +272,7 @@ int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size)
     if(ret<=0)
         av_free_packet(pkt);
     else
-        pkt->size= ret;
+        av_shrink_packet(pkt, ret);
 
     return ret;
 }