From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id CD82F4D6E1
	for <ffmpegdev@gitmailbox.com>; Sat, 19 Apr 2025 12:53:26 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C5033687D80;
	Sat, 19 Apr 2025 15:53:20 +0300 (EEST)
Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com
 [209.85.214.175])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EC6F6687D75
 for <ffmpeg-devel@ffmpeg.org>; Sat, 19 Apr 2025 15:53:13 +0300 (EEST)
Received: by mail-pl1-f175.google.com with SMTP id
 d9443c01a7336-224191d92e4so28200555ad.3
 for <ffmpeg-devel@ffmpeg.org>; Sat, 19 Apr 2025 05:53:13 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1745067191; x=1745671991; darn=ffmpeg.org;
 h=content-transfer-encoding:mime-version:message-id:date:subject:cc
 :to:from:from:to:cc:subject:date:message-id:reply-to;
 bh=5Fn8rgxf89OGWw2cWu7rYqY1Mvn3LqKK2AGmzGhFTlI=;
 b=bluh4disQdAbBbyB2aW70j80a6RaKJrCe7hzUr7um2wWJ4SsOWT+NCTl5UsrOXTc7u
 d9Fe2oGgvqRCkGajjvRUOLvsBsrrhwt0HWZ5C830qTG3trPF3yjGlyx4reQyoTcu14Jf
 BSQZ2HInDmmFDDUGwaeJke4pFqBxc8U5OJfM3ZzeYjPnbF4VlqJtWFvJ1rHUHQV+1K+D
 RJZHw4M511wsE1IncaEGqDZT2I8WyOOpEyb7/FeFGuI+Audo7agCOf0lun0QuU4UxNlB
 NBEmQ9iMDWUtSoTE+/ojA38ZRGW3F+IQvRn0H876qs3S80TChGauH29eZDjVdrplUYkU
 NIqw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1745067191; x=1745671991;
 h=content-transfer-encoding:mime-version:message-id:date:subject:cc
 :to:from:x-gm-message-state:from:to:cc:subject:date:message-id
 :reply-to;
 bh=5Fn8rgxf89OGWw2cWu7rYqY1Mvn3LqKK2AGmzGhFTlI=;
 b=ibUhLETUT11VmmssjMPGl6UTIrnS4FzWblekAkg3get/eSPsr28nuBiIHGsXzm3xKe
 7tNzJBR192TlQFRa4FlSdkVt7lhgGFzYvgCUZVKGMlGtXVfnCFGGuv/C0HiR7JiM4cB4
 24zApBsxrUSU1ZfDvbOfdjVorjvu1aPcQVkMMThV8H5gv+/sSO3Jta1BxRsI5xnZ6K+M
 sRg0LXlNB4mXMl1g4AV8/s5kpO0ejMqRoA566lKJLUBherkLAWrdGNL24qN1kcrxUH+g
 bcqWXRLLLhi/cJQUn/gGPczaOkgZ1y7Vji7lfsq5CWj6lxlJrqGyQTn1sjSyXSpjjN+s
 c+QQ==
X-Gm-Message-State: AOJu0YzP+UuPE1J/1vMu2UxwHClgxHrUr7YygqNjyakzUVVPGzIIPWzC
 tVuBelqEdBUfTGr+qw3/dyweiDWs4NI4U4TcGn8w2mbFYG4ai5bWM5kg5Z1tJWI=
X-Gm-Gg: ASbGncvkoYv2/m2x2q/Hn7fRggcatoxVtZF0GEFnB2Wpdc/J4b+Xq5O9D+B4vF5t8ix
 6WOA2iWlFBoC+4VZyJiOcTDDyahrAidPer7v/csY5SNFCvSmaDyYc3Q0Ccvwk4SMNqSjw2PUKad
 IaXTbLJkqKNd94DKdrL5PU7LU/E50AFXgtYGE1Ds4apfssHeii2GYAkWgVz6pWKAQXcjG/YUvib
 zR8bnXbb3j6XCPIk1SzYnaZfDpiZcwKWRww1WmNq2bnQLG0JArWFDe1zxqzMrZXfDPkFWnXyq5O
 8OTtVqDnW9yzx9/ojXGaCu/06NM8AhIH5uAhPYx4+uVAjw0SGllvX+GvZ78E2jh9dJgFNQ==
X-Google-Smtp-Source: AGHT+IFmop7f681TUhS3mVcg2CMbd9wsZN2amUOTGe+AcW06zmaOpaNhhmmVrQHAqcxJPdldvAP/WA==
X-Received: by 2002:a17:902:f70d:b0:224:2175:b0cd with SMTP id
 d9443c01a7336-22c535a9bbemr83428585ad.26.1745067191122; 
 Sat, 19 Apr 2025 05:53:11 -0700 (PDT)
Received: from DESKTOP-5E1KVU4.localdomain
 ([2408:8207:2420:9010:34ed:1eb6:cbf:bddf])
 by smtp.gmail.com with ESMTPSA id
 d9443c01a7336-22c50bf55edsm33099145ad.84.2025.04.19.05.53.08
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sat, 19 Apr 2025 05:53:10 -0700 (PDT)
From: Parallelc <realparallelc@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Sat, 19 Apr 2025 20:52:45 +0800
Message-ID: <20250419125245.8366-1-realparallelc@gmail.com>
X-Mailer: git-send-email 2.43.0
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH] rtpdec_vp9: Update header parsing to RFC 9628
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Parallelc <realparallelc@gmail.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250419125245.8366-1-realparallelc@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

Signed-off-by: Parallelc <realparallelc@gmail.com>
---
 libavformat/rtpdec_vp9.c | 141 ++++++++++++++++++++-------------------
 1 file changed, 72 insertions(+), 69 deletions(-)

diff --git a/libavformat/rtpdec_vp9.c b/libavformat/rtpdec_vp9.c
index 6bbdf4847a..924065b5da 100644
--- a/libavformat/rtpdec_vp9.c
+++ b/libavformat/rtpdec_vp9.c
@@ -1,5 +1,5 @@
 /*
- * RTP parser for VP9 payload format (draft version 02) - experimental
+ * RTP parser for VP9 payload format (RFC 9628) - experimental
  * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
  *
  * This file is part of FFmpeg.
@@ -47,8 +47,7 @@ static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
 {
     int has_pic_id, has_layer_idc, has_ref_idc, has_ss_data;
     av_unused int pic_id = 0, non_key_frame = 0, inter_picture_layer_frame;
-    av_unused int layer_temporal = -1, layer_spatial = -1, layer_quality = -1;
-    int ref_fields = 0, has_ref_field_ext_pic_id = 0;
+    av_unused int layer_temporal = -1, layer_spatial = -1;
     int first_fragment, last_fragment;
     int rtp_m;
     int res = 0;
@@ -68,16 +67,17 @@ static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
      *
      *      0 1 2 3 4 5 6 7
      *     +-+-+-+-+-+-+-+-+
-     *     |I|P|L|F|B|E|V|-| (REQUIRED)
+     *     |I|P|L|F|B|E|V|Z| (REQUIRED)
      *     +-+-+-+-+-+-+-+-+
      *
-     *     I: PictureID present
-     *     P: Inter-picture predicted layer frame
+     *     I: Picture ID (PID) present
+     *     P: Inter-picture predicted frame
      *     L: Layer indices present
      *     F: Flexible mode
-     *     B: Start of VP9 frame
-     *     E: End of picture
-     *     V: Scalability Structure (SS) present
+     *     B: Start of Frame
+     *     E: End of Frame
+     *     V: Scalability Structure (SS) data present
+     *     Z: Not a reference frame for upper spatial layers
      */
     has_pic_id     = !!(buf[0] & 0x80);
     inter_picture_layer_frame = !!(buf[0] & 0x40);
@@ -89,7 +89,7 @@ static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
 
     rtp_m = !!(flags & RTP_FLAG_MARKER);
 
-    /* sanity check for markers: B should always be equal to the RTP M marker */
+    /* sanity check for markers: E should always be equal to the RTP M marker */
     if (last_fragment != rtp_m) {
         av_log(ctx, AV_LOG_ERROR, "Invalid combination of B and M marker (%d != %d)\n", last_fragment, rtp_m);
         return AVERROR_INVALIDDATA;
@@ -134,72 +134,70 @@ static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
      *
      *          0 1 2 3 4 5 6 7
      *         +-+-+-+-+-+-+-+-+
-     *   L:    | T | S | Q | R | (CONDITIONALLY RECOMMENDED)
+     *   L:    | TID |U| SID |D| (Conditionally RECOMMENDED)
+     *         +-+-+-+-+-+-+-+-+
+     *         |   TL0PICIDX   | (Conditionally REQUIRED)
      *         +-+-+-+-+-+-+-+-+
      *
-     *   T, S and Q are 2-bit indices for temporal, spatial, and quality layers.
-     *   If "F" is set in the initial octet, R is 2 bits representing the number
-     *   of reference fields this frame refers to.
+     *   TID: Temporal layer ID (3 bits)
+     *   U: Switching up point (1 bit)
+     *   SID: Spatial layer ID (3 bits)
+     *   D: Inter-layer dependency used (1 bit)
+     *   TL0PICIDX: Temporal Layer 0 Picture Index (8 bits, non-flexible mode only)
      */
     if (has_layer_idc) {
         if (len < 1) {
             av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
             return AVERROR_INVALIDDATA;
         }
-        layer_temporal = buf[0] & 0xC0;
-        layer_spatial  = buf[0] & 0x30;
-        layer_quality  = buf[0] & 0x0C;
-        if (has_ref_idc) {
-            ref_fields = buf[0] & 0x03;
-            if (ref_fields)
-                non_key_frame = 1;
-        }
+        layer_temporal = buf[0] >> 5;
+        layer_spatial  = (buf[0] >> 1) & 0x07;
         buf++;
         len--;
+
+        if (!has_ref_idc) {
+            if (len < 1) {
+                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
+                return AVERROR_INVALIDDATA;
+            }
+            /* ignore TL0PICIDX */
+            buf++;
+            len--;
+        }
     }
 
     /*
-     *         decode the reference fields
+     *         decode reference indices
      *
      *          0 1 2 3 4 5 6 7
-     *         +-+-+-+-+-+-+-+-+              -\
-     *   F:    | PID |X| RS| RQ| (OPTIONAL)    .
-     *         +-+-+-+-+-+-+-+-+               . - R times
-     *   X:    | EXTENDED PID  | (OPTIONAL)    .
-     *         +-+-+-+-+-+-+-+-+              -/
+     *         +-+-+-+-+-+-+-+-+                             -\
+     *   P,F:  | P_DIFF      |N| (Conditionally REQUIRED)    - up to 3 times
+     *         +-+-+-+-+-+-+-+-+                             -/
      *
-     *   PID:  The relative Picture ID referred to by this frame.
-     *   RS and RQ:  The spatial and quality layer IDs.
-     *   X: 1 if this layer index has an extended relative Picture ID.
+     *   P_DIFF: Relative Picture ID (7 bits)
+     *   N: 1 if another P_DIFF follows
      */
-    if (has_ref_idc) {
-        while (ref_fields) {
+    if (has_ref_idc && inter_picture_layer_frame) {
+        int i, p_diff, has_more;
+        for (i = 0; i < 3; i++) {
             if (len < 1) {
                 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                 return AVERROR_INVALIDDATA;
             }
 
-            has_ref_field_ext_pic_id = buf[0] & 0x10;
-
-            /* pass ref. field */
-            if (has_ref_field_ext_pic_id) {
-                if (len < 2) {
-                    av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
-                    return AVERROR_INVALIDDATA;
-                }
-
-                /* ignore ref. data */
+            p_diff = buf[0] >> 1;
+            has_more = buf[0] & 0x01;
 
-                buf += 2;
-                len -= 2;
-            } else {
+            if (!p_diff) {
+                av_log(ctx, AV_LOG_ERROR, "Invalid P_DIFF value 0\n");
+                return AVERROR_INVALIDDATA;
+            }
 
-                /* ignore ref. data */
+            buf++;
+            len--;
 
-                buf++;
-                len--;
-            }
-            ref_fields--;
+            if (!has_more)
+                break;
         }
     }
 
@@ -208,18 +206,30 @@ static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
      *
      *          0 1 2 3 4 5 6 7
      *         +-+-+-+-+-+-+-+-+
-     *   V:    | PATTERN LENGTH|
-     *         +-+-+-+-+-+-+-+-+                           -\
-     *         | T | S | Q | R | (OPTIONAL)                 .
-     *         +-+-+-+-+-+-+-+-+              -\            .
-     *         | PID |X| RS| RQ| (OPTIONAL)    .            . - PAT. LEN. times
-     *         +-+-+-+-+-+-+-+-+               . - R times  .
-     *   X:    | EXTENDED PID  | (OPTIONAL)    .            .
-     *         +-+-+-+-+-+-+-+-+              -/           -/
+     *   V:    | N_S |Y|G|-|-|-|
+     *         +-+-+-+-+-+-+-+-+              -\
+     *   Y:    |     WIDTH     | (OPTIONAL)    .
+     *         +               +               .
+     *         |               | (OPTIONAL)    .
+     *         +-+-+-+-+-+-+-+-+               . - N_S + 1 times
+     *         |     HEIGHT    | (OPTIONAL)    .
+     *         +               +               .
+     *         |               | (OPTIONAL)    .
+     *         +-+-+-+-+-+-+-+-+              -/
+     *   G:    |      N_G      | (OPTIONAL)
+     *         +-+-+-+-+-+-+-+-+                            -\
+     *   N_G:  | TID |U| R |-|-| (OPTIONAL)                 .
+     *         +-+-+-+-+-+-+-+-+              -\            . - N_G times
+     *         |    P_DIFF     | (OPTIONAL)    . - R times  .
+     *         +-+-+-+-+-+-+-+-+              -/            -/
      *
-     *   PID:  The relative Picture ID referred to by this frame.
-     *   RS and RQ:  The spatial and quality layer IDs.
-     *   X: 1 if this layer index has an extended relative Picture ID.
+     *   N_S: Number of spatial layers minus 1
+     *   Y: Each spatial layer's resolution present
+     *   G: Picture Group description present
+     *   N_G: Number of pictures in Picture Group
+     *   TID: Temporal layer ID
+     *   U: Switching up point
+     *   R: Number of P_DIFF fields
      */
     if (has_ss_data) {
         int n_s, y, g, i;
@@ -282,13 +292,6 @@ static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
         }
     }
 
-    /*
-     * decode the VP9 payload header
-     *
-     *  spec. is tbd
-     */
-    //XXX: implement when specified
-
     /* sanity check: 1 byte payload as minimum */
     if (len < 1) {
         av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
-- 
2.43.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".