diff --git a/libavformat/spdifenc.c b/libavformat/spdifenc.c
index 84372f8..659691c 100644
--- a/libavformat/spdifenc.c
+++ b/libavformat/spdifenc.c
@@ -48,11 +48,14 @@
 #include "avio_internal.h"
 #include "spdif.h"
 #include "libavcodec/ac3.h"
+#include "libavcodec/ac3_parser.h"
 #include "libavcodec/dca.h"
 #include "libavcodec/dcadata.h"
 #include "libavcodec/aacadtsdec.h"
 #include "libavutil/opt.h"
 
+#define AC3_HEADER_SIZE 7
+
 typedef struct IEC61937Context {
     const AVClass *av_class;
     enum IEC61937DataType data_type;///< burst info - reference to type of payload of the data-burst
@@ -103,10 +106,26 @@ static const AVClass class = {
 static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
 {
     IEC61937Context *ctx = s->priv_data;
-    int bitstream_mode = pkt->data[5] & 0x7;
+    AC3HeaderInfo hdr;
+    GetBitContext gbc;
+    int ret;
 
-    ctx->data_type  = IEC61937_AC3 | (bitstream_mode << 8);
-    ctx->pkt_offset = AC3_FRAME_SIZE << 2;
+    if (pkt->size < AC3_HEADER_SIZE)
+        return AVERROR_INVALIDDATA;
+    init_get_bits(&gbc, pkt->data, AC3_HEADER_SIZE * 8);
+    ret = ff_ac3_parse_header(&gbc, &hdr);
+    if (ret) {
+        av_log(s, AV_LOG_ERROR, "Invalid AC3 header\n");
+        return AVERROR_INVALIDDATA;
+    }
+    if (hdr.num_blocks != 6) {
+        av_log(s, AV_LOG_ERROR, "AC3 num_blocks[%d] not supported for IEC-61937\n", hdr.num_blocks);
+        return AVERROR_INVALIDDATA;
+    }
+    ctx->data_type   = IEC61937_AC3 | (hdr.bitstream_mode <<8);
+    ctx->pkt_offset  = AC3_FRAME_SIZE <<2;
+    ctx->out_bytes   = hdr.frame_size;
+    ctx->length_code = ctx->out_bytes <<3;
     return 0;
 }
 
@@ -326,7 +345,7 @@ static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
     int extension = pkt->data[2] & 1;
 
     if (layer == 3 || version == 1) {
-        av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
+        av_log(s, AV_LOG_ERROR, "Invalid MPEG header\n");
         return AVERROR_INVALIDDATA;
     }
     av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
@@ -351,11 +370,13 @@ static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
     init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
     ret = ff_aac_parse_header(&gbc, &hdr);
     if (ret < 0) {
-        av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
+        av_log(s, AV_LOG_ERROR, "Invalid AAC header\n");
         return AVERROR_INVALIDDATA;
     }
 
-    ctx->pkt_offset = hdr.samples << 2;
+    ctx->out_bytes   = ret;
+    ctx->pkt_offset  = hdr.samples << 2;
+    ctx->length_code = ctx->out_bytes << 3;
     switch (hdr.num_aac_frames) {
     case 1:
         ctx->data_type = IEC61937_MPEG2_AAC;
@@ -503,6 +524,8 @@ static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
         return ret;
     if (!ctx->pkt_offset)
         return 0;
+    if (pkt->size < ctx->out_bytes)
+        return AVERROR_INVALIDDATA;
 
     padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
     if (padding < 0) {
