Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 5/5] avcodec/cfhddata: Reduce stack usage
Date: Sat,  3 Sep 2022 22:35:59 +0200
Message-ID: <AS8P250MB0744D466DE062DB6D899FB478F7D9@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <AS8P250MB07443029028BA4485D8478F18F7D9@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM>

Creating CFHD RL VLC tables works by first extending
the codes by the sign, followed by creating a VLC,
followed by deriving the RL VLC from this VLC (which
is then discarded). Extending the codes uses stack arrays.

The tables used to initialize the VLC are already sorted
from left-to-right in the tree. This means that the
corresponding VLC entries are generally also ascending,
but not always: Entries from subtables always follow
the corresponding main table although it is possible
for the right-most node to fit into the main table.

This suggests that one can try to use the final destination
buffer as scratch buffer for the tables with sign included.
Unfortunately it works for neither of the tables if one
uses the right-most part of the RL VLC buffer as scratch buffer;
using the left-most part of the RL VLC buffer as scratch buffer
might work if one traverses the VLC entries from end to start.
But it works only for the little RL VLC (table 9), not for table 18.

Therefore this patch uses the RL VLC buffer for table 9
as scratch buffer for creating the bigger table 18.
Afterwards the left part of the buffer for table 9 is
used as scratch buffer to create table 9.

This fixes the cfhd part of ticket #9399 (if it is not already fixed).
Notice that I do not consider the previous stack usage excessive.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
I actually regard #9399 as a toolchain issue and not as a reason
to pessimize the code for all the other arches/toolchains
where it works.

 libavcodec/cfhddata.c | 47 +++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 24 deletions(-)

diff --git a/libavcodec/cfhddata.c b/libavcodec/cfhddata.c
index efe932dc3b..fd5cc8174e 100644
--- a/libavcodec/cfhddata.c
+++ b/libavcodec/cfhddata.c
@@ -127,11 +127,8 @@ static const CFHD_RL_ELEM table_18_vlc[NB_VLC_TABLE_18] = {
 
 static av_cold int cfhd_init_vlc(CFHD_RL_VLC_ELEM out[], unsigned out_size,
                                  const CFHD_RL_ELEM table_vlc[], unsigned table_size,
-                                 void *logctx)
+                                 CFHD_RL_VLC_ELEM tmp[], void *logctx)
 {
-    uint8_t  new_cfhd_vlc_len[NB_VLC_TABLE_18 * 2];
-    uint16_t new_cfhd_vlc_run[NB_VLC_TABLE_18 * 2];
-    int16_t  new_cfhd_vlc_level[NB_VLC_TABLE_18 * 2];
     VLC vlc;
     unsigned j;
     int ret;
@@ -139,27 +136,28 @@ static av_cold int cfhd_init_vlc(CFHD_RL_VLC_ELEM out[], unsigned out_size,
     /** Similar to dv.c, generate signed VLC tables **/
 
     for (unsigned i = j = 0; i < table_size; i++, j++) {
-        new_cfhd_vlc_len[j]   = table_vlc[i].len;
-        new_cfhd_vlc_run[j]   = table_vlc[i].run;
-        new_cfhd_vlc_level[j] = table_vlc[i].level;
+        tmp[j].len   = table_vlc[i].len;
+        tmp[j].run   = table_vlc[i].run;
+        tmp[j].level = table_vlc[i].level;
 
         /* Don't include the zero level nor escape bits */
         if (table_vlc[i].level && table_vlc[i].run) {
-            new_cfhd_vlc_len[j]++;
+            tmp[j].len++;
             j++;
-            new_cfhd_vlc_len[j]   =  table_vlc[i].len + 1;
-            new_cfhd_vlc_run[j]   =  table_vlc[i].run;
-            new_cfhd_vlc_level[j] = -table_vlc[i].level;
+            tmp[j].len   =  table_vlc[i].len + 1;
+            tmp[j].run   =  table_vlc[i].run;
+            tmp[j].level = -table_vlc[i].level;
         }
     }
 
-    ret = ff_init_vlc_from_lengths(&vlc, VLC_BITS, j, new_cfhd_vlc_len,
-                                   1, NULL, 0, 0, 0, 0, logctx);
+    ret = ff_init_vlc_from_lengths(&vlc, VLC_BITS, j,
+                                   &tmp[0].len, sizeof(tmp[0]),
+                                   NULL, 0, 0, 0, 0, logctx);
     if (ret < 0)
         return ret;
     av_assert0(vlc.table_size == out_size);
 
-    for (unsigned i = 0; i < out_size; i++) {
+    for (unsigned i = out_size; i-- > 0;) {
         int code = vlc.table[i].sym;
         int len  = vlc.table[i].len;
         int level, run;
@@ -168,8 +166,8 @@ static av_cold int cfhd_init_vlc(CFHD_RL_VLC_ELEM out[], unsigned out_size,
             run   = 0;
             level = code;
         } else {
-            run   = new_cfhd_vlc_run[code];
-            level = new_cfhd_vlc_level[code];
+            run   = tmp[code].run;
+            level = tmp[code].level;
         }
         out[i].len   = len;
         out[i].level = level;
@@ -184,16 +182,17 @@ av_cold int ff_cfhd_init_vlcs(CFHDContext *s)
 {
     int ret;
 
-    /* Table 9 */
-    ret = cfhd_init_vlc(s->table_9_rl_vlc, FF_ARRAY_ELEMS(s->table_9_rl_vlc),
-                        table_9_vlc,       FF_ARRAY_ELEMS(table_9_vlc),
-                        s->avctx);
-    if (ret < 0)
-        return ret;
-    /* Table 18 */
+    /* Table 18 - we reuse the unused table_9_rl_vlc as scratch buffer here */
     ret = cfhd_init_vlc(s->table_18_rl_vlc, FF_ARRAY_ELEMS(s->table_18_rl_vlc),
                         table_18_vlc,       FF_ARRAY_ELEMS(table_18_vlc),
-                        s->avctx);
+                        s->table_9_rl_vlc, s->avctx);
+    if (ret < 0)
+        return ret;
+    /* Table 9 - table_9_rl_vlc itself is used as scratch buffer; it works
+     * because we are counting down in the final loop */
+    ret = cfhd_init_vlc(s->table_9_rl_vlc, FF_ARRAY_ELEMS(s->table_9_rl_vlc),
+                        table_9_vlc,       FF_ARRAY_ELEMS(table_9_vlc),
+                        s->table_9_rl_vlc, s->avctx);
     if (ret < 0)
         return ret;
     return 0;
-- 
2.34.1

_______________________________________________
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".

  parent reply	other threads:[~2022-09-03 20:36 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-03 20:30 [FFmpeg-devel] [PATCH 1/5] avcodec/cfhd, cfhddata: Simplify check for escape Andreas Rheinhardt
2022-09-03 20:35 ` [FFmpeg-devel] [PATCH 2/5] avcodec/cfhddata: Avoid code tables Andreas Rheinhardt
2022-09-03 20:35 ` [FFmpeg-devel] [PATCH 3/5] avcodec/cfhddata: Avoid code duplication when creating codebooks Andreas Rheinhardt
2022-09-03 20:35 ` [FFmpeg-devel] [PATCH 4/5] avcodec/cfhd, cfhddata: Free VLC as soon as it is not needed Andreas Rheinhardt
2022-09-03 20:35 ` Andreas Rheinhardt [this message]
2022-09-03 21:49   ` [FFmpeg-devel] [PATCH 5/5] avcodec/cfhddata: Reduce stack usage Paul B Mahol
2022-09-03 21:56     ` Andreas Rheinhardt
2022-09-05 10:25       ` Paul B Mahol

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AS8P250MB0744D466DE062DB6D899FB478F7D9@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git