[FFmpeg-devel] [PATCH 23/28] Setup wanted pkt size in spdif muxers header parser

Mans Rullgard mans
Wed Jun 30 11:09:51 CEST 2010


From: Cory Fields <theuni-nospam- at xbmc.org>

---
 libavformat/spdif.c |   26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/libavformat/spdif.c b/libavformat/spdif.c
index 813be67..43d1b8e 100644
--- a/libavformat/spdif.c
+++ b/libavformat/spdif.c
@@ -74,7 +74,7 @@ enum IEC958DataType {
 
 typedef struct IEC958Context {
     enum IEC958DataType data_type;  ///< burst info - reference to type of payload of the data-burst
-    int pkt_size;                   ///< length code in bits
+    int pkt_size;                   ///< length code in bytes
     int pkt_offset;                 ///< data burst repetition period in bytes
     uint8_t *buffer;                ///< allocated buffer, used for swap bytes
     int buffer_size;                ///< size of allocated buffer
@@ -110,6 +110,7 @@ static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
 
     ctx->data_type  = IEC958_AC3 | (bitstream_mode << 8);
     ctx->pkt_offset = AC3_FRAME_SIZE << 2;
+    ctx->pkt_size   = FFALIGN(pkt->size, 2);
     return 0;
 }
 
@@ -149,6 +150,7 @@ static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
         return -1;
     }
     ctx->pkt_offset = blocks << 7;
+    ctx->pkt_size   = FFALIGN(pkt->size, 2);
 
     return 0;
 }
@@ -184,6 +186,7 @@ static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
         ctx->data_type  = mpeg_data_type [version & 1][layer];
         ctx->pkt_offset = mpeg_pkt_offset[version & 1][layer];
     }
+    ctx->pkt_size = FFALIGN(pkt->size, 2);
     // TODO Data type dependant info (normal/karaoke, dynamic range control)
     return 0;
 }
@@ -202,6 +205,7 @@ static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
         return -1;
     }
 
+    ctx->pkt_size   = FFALIGN(pkt->size, 2);
     ctx->pkt_offset = hdr.samples << 2;
     switch (hdr.num_aac_frames) {
     case 1:
@@ -260,12 +264,15 @@ static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
     IEC958Context *ctx = s->priv_data;
     int ret, padding;
 
-    ctx->pkt_size = FFALIGN(pkt->size, 2) << 3;
     ret = ctx->header_info(s, pkt);
     if (ret < 0)
         return -1;
+    if (ctx->pkt_size > pkt->size + 1) {
+        av_log(s, AV_LOG_ERROR, "not enough data for requested frame size\n");
+        return -1;
+    }
 
-    padding = (ctx->pkt_offset - BURST_HEADER_SIZE - pkt->size) >> 1;
+    padding = (ctx->pkt_offset - BURST_HEADER_SIZE - ctx->pkt_size) >> 1;
     if (padding < 0) {
         av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
         return -1;
@@ -274,21 +281,18 @@ static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
     put_le16(s->pb, SYNCWORD1);      //Pa
     put_le16(s->pb, SYNCWORD2);      //Pb
     put_le16(s->pb, ctx->data_type); //Pc
-    put_le16(s->pb, ctx->pkt_size);  //Pd
+    put_le16(s->pb, ctx->pkt_size << 3);  //Pd
 
 #if HAVE_BIGENDIAN
-    put_buffer(s->pb, pkt->data, pkt->size & ~1);
+    put_buffer(s->pb, pkt->data, ctx->pkt_size);
 #else
-    av_fast_malloc(&ctx->buffer, &ctx->buffer_size, pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+    av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->pkt_size + FF_INPUT_BUFFER_PADDING_SIZE);
     if (!ctx->buffer)
         return AVERROR(ENOMEM);
-    bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)pkt->data, pkt->size >> 1);
-    put_buffer(s->pb, ctx->buffer, pkt->size & ~1);
+    bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)pkt->data, ctx->pkt_size >> 1);
+    put_buffer(s->pb, ctx->buffer, ctx->pkt_size);
 #endif
 
-    if (pkt->size & 1)
-        put_be16(s->pb, pkt->data[pkt->size - 1]);
-
     for (; padding > 0; padding--)
         put_be16(s->pb, 0);
 
-- 
1.7.1.1




More information about the ffmpeg-devel mailing list