]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtpdec_h263_rfc2190.c
lavc decoders: properly initialize AVFrame.
[ffmpeg] / libavformat / rtpdec_h263_rfc2190.c
index baec6a427c61a55bfa7478f9dbab750133ee397c..4b6e9967701631e83d399a40242b1a6d9145639b 100644 (file)
@@ -35,6 +35,7 @@ struct PayloadContext {
     uint8_t      endbyte;
     int          endbyte_bits;
     uint32_t     timestamp;
+    int          newformat;
 };
 
 static PayloadContext *h263_new_context(void)
@@ -54,12 +55,26 @@ static void h263_free_context(PayloadContext *data)
     av_free(data);
 }
 
+static int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
+{
+    if (st_index < 0)
+        return 0;
+    ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
+    return 0;
+}
+
 static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
-                              const uint8_t *buf, int len, int flags)
+                              const uint8_t *buf, int len, uint16_t seq,
+                              int flags)
 {
-    int f, p, i, sbit, ebit; /* Corresponding to header fields in the RFC */
-    int header_size;
+    /* Corresponding to header fields in the RFC */
+    int f, p, i, sbit, ebit, src, r;
+    int header_size, ret;
+
+    if (data->newformat)
+        return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
+                                     seq, flags);
 
     if (data->buf && data->timestamp != *timestamp) {
         /* Dropping old buffered, unfinished data */
@@ -80,6 +95,7 @@ static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
         /* Mode A */
         header_size = 4;
         i = buf[1] & 0x10;
+        r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
     } else if (!p) {
         /* Mode B */
         header_size = 8;
@@ -89,6 +105,7 @@ static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                    len, header_size);
             return AVERROR_INVALIDDATA;
         }
+        r = buf[3] & 0x03;
         i = buf[4] & 0x80;
     } else {
         /* Mode C */
@@ -99,10 +116,24 @@ static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                    len, header_size);
             return AVERROR_INVALIDDATA;
         }
+        r = buf[3] & 0x03;
         i = buf[4] & 0x80;
     }
     sbit = (buf[0] >> 3) & 0x7;
     ebit =  buf[0]       & 0x7;
+    src  = (buf[1] & 0xe0) >> 5;
+    if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
+        if ((src == 0 || src >= 6) && r) {
+            /* Invalid src for this format, and bits that should be zero
+             * according to RFC 2190 aren't zero. */
+            av_log(ctx, AV_LOG_WARNING,
+                   "Interpreting H263 RTP data as RFC 2429/4629 even though "
+                   "signalled with a static payload type.\n");
+            data->newformat = 1;
+            return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
+                                         len, seq, flags);
+        }
+    }
 
     buf += header_size;
     len -= header_size;
@@ -110,8 +141,8 @@ static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     if (!data->buf) {
         /* Check the picture start code, only start buffering a new frame
          * if this is correct */
-        if (!f && len > 4 && AV_RB32(buf) >> 10 == 0x20) {
-            int ret = avio_open_dyn_buf(&data->buf);
+        if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
+            ret = avio_open_dyn_buf(&data->buf);
             if (ret < 0)
                 return ret;
             data->timestamp = *timestamp;
@@ -163,20 +194,19 @@ static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
         avio_w8(data->buf, data->endbyte);
     data->endbyte_bits = 0;
 
-    av_init_packet(pkt);
-    pkt->size         = avio_close_dyn_buf(data->buf, &pkt->data);
-    pkt->destruct     = av_destruct_packet;
-    pkt->stream_index = st->index;
+    ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
+    if (ret < 0)
+        return ret;
     if (!i)
         pkt->flags   |= AV_PKT_FLAG_KEY;
-    data->buf = NULL;
 
     return 0;
 }
 
 RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
     .codec_type        = AVMEDIA_TYPE_VIDEO,
-    .codec_id          = CODEC_ID_H263,
+    .codec_id          = AV_CODEC_ID_H263,
+    .init              = h263_init,
     .parse_packet      = h263_handle_packet,
     .alloc             = h263_new_context,
     .free              = h263_free_context,