diff mbox series

[FFmpeg-devel] avformat/rtpdec: Avoid allocations of small dynamic buffers

Message ID 20210127152819.2351127-1-andreas.rheinhardt@gmail.com
State Accepted
Commit ad2cc0e2f41f3be59e8c7c1a0ab5c813d7073643
Headers show
Series [FFmpeg-devel] avformat/rtpdec: Avoid allocations of small dynamic buffers | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Jan. 27, 2021, 3:28 p.m. UTC
Besides avoiding allocations this also fixes a design defect of
ff_rtp_send_punch_packets: It did not return an error in case of
these allocations failed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/rtpdec.c | 38 +++++++++++++-------------------------
 1 file changed, 13 insertions(+), 25 deletions(-)

Comments

Andreas Rheinhardt Jan. 30, 2021, 1:18 a.m. UTC | #1
Andreas Rheinhardt:
> Besides avoiding allocations this also fixes a design defect of
> ff_rtp_send_punch_packets: It did not return an error in case of
> these allocations failed.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/rtpdec.c | 38 +++++++++++++-------------------------
>  1 file changed, 13 insertions(+), 25 deletions(-)
> 
> diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
> index f40c020c33..d592e34893 100644
> --- a/libavformat/rtpdec.c
> +++ b/libavformat/rtpdec.c
> @@ -404,38 +404,26 @@ int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
>  
>  void ff_rtp_send_punch_packets(URLContext *rtp_handle)
>  {
> -    AVIOContext *pb;
> -    uint8_t *buf;
> -    int len;
> +    uint8_t buf[RTP_MIN_PACKET_LENGTH], *ptr = buf;
>  
>      /* Send a small RTP packet */
> -    if (avio_open_dyn_buf(&pb) < 0)
> -        return;
>  
> -    avio_w8(pb, (RTP_VERSION << 6));
> -    avio_w8(pb, 0); /* Payload type */
> -    avio_wb16(pb, 0); /* Seq */
> -    avio_wb32(pb, 0); /* Timestamp */
> -    avio_wb32(pb, 0); /* SSRC */
> +    bytestream_put_byte(&ptr, (RTP_VERSION << 6));
> +    bytestream_put_byte(&ptr, 0); /* Payload type */
> +    bytestream_put_be16(&ptr, 0); /* Seq */
> +    bytestream_put_be32(&ptr, 0); /* Timestamp */
> +    bytestream_put_be32(&ptr, 0); /* SSRC */
>  
> -    len = avio_close_dyn_buf(pb, &buf);
> -    if ((len > 0) && buf)
> -        ffurl_write(rtp_handle, buf, len);
> -    av_free(buf);
> +    ffurl_write(rtp_handle, buf, ptr - buf);
>  
>      /* Send a minimal RTCP RR */
> -    if (avio_open_dyn_buf(&pb) < 0)
> -        return;
> +    ptr = buf;
> +    bytestream_put_byte(&ptr, (RTP_VERSION << 6));
> +    bytestream_put_byte(&ptr, RTCP_RR); /* receiver report */
> +    bytestream_put_be16(&ptr, 1); /* length in words - 1 */
> +    bytestream_put_be32(&ptr, 0); /* our own SSRC */
>  
> -    avio_w8(pb, (RTP_VERSION << 6));
> -    avio_w8(pb, RTCP_RR); /* receiver report */
> -    avio_wb16(pb, 1); /* length in words - 1 */
> -    avio_wb32(pb, 0); /* our own SSRC */
> -
> -    len = avio_close_dyn_buf(pb, &buf);
> -    if ((len > 0) && buf)
> -        ffurl_write(rtp_handle, buf, len);
> -    av_free(buf);
> +    ffurl_write(rtp_handle, buf, ptr - buf);
>  }
>  
>  static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
> 
Will apply tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index f40c020c33..d592e34893 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -404,38 +404,26 @@  int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
 
 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
 {
-    AVIOContext *pb;
-    uint8_t *buf;
-    int len;
+    uint8_t buf[RTP_MIN_PACKET_LENGTH], *ptr = buf;
 
     /* Send a small RTP packet */
-    if (avio_open_dyn_buf(&pb) < 0)
-        return;
 
-    avio_w8(pb, (RTP_VERSION << 6));
-    avio_w8(pb, 0); /* Payload type */
-    avio_wb16(pb, 0); /* Seq */
-    avio_wb32(pb, 0); /* Timestamp */
-    avio_wb32(pb, 0); /* SSRC */
+    bytestream_put_byte(&ptr, (RTP_VERSION << 6));
+    bytestream_put_byte(&ptr, 0); /* Payload type */
+    bytestream_put_be16(&ptr, 0); /* Seq */
+    bytestream_put_be32(&ptr, 0); /* Timestamp */
+    bytestream_put_be32(&ptr, 0); /* SSRC */
 
-    len = avio_close_dyn_buf(pb, &buf);
-    if ((len > 0) && buf)
-        ffurl_write(rtp_handle, buf, len);
-    av_free(buf);
+    ffurl_write(rtp_handle, buf, ptr - buf);
 
     /* Send a minimal RTCP RR */
-    if (avio_open_dyn_buf(&pb) < 0)
-        return;
+    ptr = buf;
+    bytestream_put_byte(&ptr, (RTP_VERSION << 6));
+    bytestream_put_byte(&ptr, RTCP_RR); /* receiver report */
+    bytestream_put_be16(&ptr, 1); /* length in words - 1 */
+    bytestream_put_be32(&ptr, 0); /* our own SSRC */
 
-    avio_w8(pb, (RTP_VERSION << 6));
-    avio_w8(pb, RTCP_RR); /* receiver report */
-    avio_wb16(pb, 1); /* length in words - 1 */
-    avio_wb32(pb, 0); /* our own SSRC */
-
-    len = avio_close_dyn_buf(pb, &buf);
-    if ((len > 0) && buf)
-        ffurl_write(rtp_handle, buf, len);
-    av_free(buf);
+    ffurl_write(rtp_handle, buf, ptr - buf);
 }
 
 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,