From b981b543c671557658d2f89408e5a284c9394e1e Mon Sep 17 00:00:00 2001
From: Dale Curtis <dalecurtis@chromium.org>
Date: Tue, 13 Aug 2024 19:24:02 +0000
Subject: [PATCH] Make H.274 film grain support optional for H.264. Saves
 ~779kb.

Film grain support adds a huge amount of overhead to the H264Context
structure for a feature that is rarely used. On low end devices or
pages that have lots of media this bloats memory usage rapidly.

This introduces a --disable-h264-film-grain option which makes
these fields optional and reduces the H264Context size from
851808 bytes to 53444 bytes.

Bug: https://crbug.com/359358875
Signed-off-by: Dale Curtis <dalecurtis@chromium.org>
---
 configure                 | 4 +++-
 libavcodec/Makefile       | 3 ++-
 libavcodec/h2645_sei.c    | 7 ++++++-
 libavcodec/h2645_sei.h    | 4 ++++
 libavcodec/h264_picture.c | 2 ++
 libavcodec/h264_sei.c     | 2 ++
 libavcodec/h264_slice.c   | 4 ++++
 libavcodec/h264dec.h      | 2 ++
 8 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/configure b/configure
index 37178d7d81..aa937ae4fe 100755
--- a/configure
+++ b/configure
@@ -138,6 +138,7 @@ Component options:
   --disable-network        disable network support [no]
   --disable-dwt            disable DWT code
   --disable-error-resilience disable error resilience code
+  --disable-h264-film-grain disable h.274 film grain support for h.264
   --disable-lsp            disable LSP code
   --disable-faan           disable floating point AAN (I)DCT code
   --disable-iamf           disable support for Immersive Audio Model
@@ -2075,6 +2076,7 @@ SUBSYSTEM_LIST="
     error_resilience
     faan
     fast_unaligned
+    h264_film_grain
     iamf
     lsp
     pixelutils
@@ -2968,7 +2970,7 @@ h263i_decoder_select="h263_decoder"
 h263p_decoder_select="h263_decoder"
 h263p_encoder_select="h263_encoder"
 h264_decoder_select="cabac golomb h264chroma h264dsp h264parse h264pred h264qpel h264_sei videodsp"
-h264_decoder_suggest="error_resilience"
+h264_decoder_suggest="error_resilience h264_film_grain"
 hap_decoder_select="snappy texturedsp"
 hap_encoder_deps="libsnappy"
 hap_encoder_select="texturedspenc"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 262d0a3d3e..21f0a21709 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -102,6 +102,7 @@ OBJS-$(CONFIG_GOLOMB)                  += golomb.o
 OBJS-$(CONFIG_H263DSP)                 += h263dsp.o
 OBJS-$(CONFIG_H264CHROMA)              += h264chroma.o
 OBJS-$(CONFIG_H264DSP)                 += h264dsp.o h264idct.o
+OBJS-$(CONFIG_H264_FILM_GRAIN)         += h274.o
 OBJS-$(CONFIG_H264PARSE)               += h264_parse.o h264_ps.o h264data.o \
                                           h2645data.o h2645_parse.o h2645_vui.o
 OBJS-$(CONFIG_H264PRED)                += h264pred.o
@@ -408,7 +409,7 @@ OBJS-$(CONFIG_H264_DECODER)            += h264dec.o h264_cabac.o h264_cavlc.o \
                                           h264_direct.o h264_loopfilter.o  \
                                           h264_mb.o h264_picture.o \
                                           h264_refs.o \
-                                          h264_slice.o h264data.o h274.o
+                                          h264_slice.o h264data.o
 OBJS-$(CONFIG_H264_AMF_ENCODER)        += amfenc_h264.o
 OBJS-$(CONFIG_H264_CUVID_DECODER)      += cuviddec.o
 OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec.o
diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c
index 7c83747cd0..2e8a41d89c 100644
--- a/libavcodec/h2645_sei.c
+++ b/libavcodec/h2645_sei.c
@@ -471,8 +471,10 @@ int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type,
         return decode_unregistered_user_data(&h->unregistered, gbyte, codec_id);
     case SEI_TYPE_DISPLAY_ORIENTATION:
         return decode_display_orientation(&h->display_orientation, gb);
+#if CONFIG_H264_FILM_GRAIN
     case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
         return decode_film_grain_characteristics(&h->film_grain_characteristics, codec_id, gb);
+#endif
     case SEI_TYPE_FRAME_PACKING_ARRANGEMENT:
         return decode_frame_packing_arrangement(&h->frame_packing, gb, codec_id);
     case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
@@ -777,7 +779,7 @@ int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
             sei->afd.present = 0;
         }
     }
-
+#if CONFIG_H264_FILM_GRAIN
     if (sei->film_grain_characteristics.present) {
         H2645SEIFilmGrainCharacteristics *fgc = &sei->film_grain_characteristics;
         AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
@@ -849,6 +851,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
         avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
     }
+#endif
 
 #if CONFIG_HEVC_SEI
     ret = ff_aom_attach_film_grain_sets(&sei->aom_film_grain, frame);
@@ -879,5 +882,7 @@ void ff_h2645_sei_reset(H2645SEI *s)
     s->ambient_viewing_environment.present = 0;
     s->mastering_display.present = 0;
     s->content_light.present = 0;
+#if CONFIG_H264_FILM_GRAIN
     s->aom_film_grain.enable = 0;
+#endif
 }
diff --git a/libavcodec/h2645_sei.h b/libavcodec/h2645_sei.h
index 488dbcad7e..5d2603ac47 100644
--- a/libavcodec/h2645_sei.h
+++ b/libavcodec/h2645_sei.h
@@ -130,11 +130,15 @@ typedef struct H2645SEI {
     H2645SEIFramePacking frame_packing;
     H2645SEIDisplayOrientation display_orientation;
     H2645SEIAlternativeTransfer alternative_transfer;
+#if CONFIG_H264_FILM_GRAIN
     H2645SEIFilmGrainCharacteristics film_grain_characteristics;
+#endif
     H2645SEIAmbientViewingEnvironment ambient_viewing_environment;
     H2645SEIMasteringDisplay mastering_display;
     H2645SEIContentLight content_light;
+#if CONFIG_H264_FILM_GRAIN
     AVFilmGrainAFGS1Params aom_film_grain;
+#endif
 } H2645SEI;
 
 enum {
diff --git a/libavcodec/h264_picture.c b/libavcodec/h264_picture.c
index 3234141dbd..6de801a95a 100644
--- a/libavcodec/h264_picture.c
+++ b/libavcodec/h264_picture.c
@@ -212,9 +212,11 @@ int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
         const AVFrameSideData *sd = av_frame_get_side_data(cur->f, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
 
         err = AVERROR_INVALIDDATA;
+#if CONFIG_H264_FILM_GRAIN
         if (sd) // a decoding error may have happened before the side data could be allocated
             err = ff_h274_apply_film_grain(cur->f_grain, cur->f, &h->h274db,
                                            (AVFilmGrainParams *) sd->data);
+#endif
         if (err < 0) {
             av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film "
                    "grain, ignoring: %s\n", av_err2str(err));
diff --git a/libavcodec/h264_sei.c b/libavcodec/h264_sei.c
index 8d6dc77943..4b2b292bec 100644
--- a/libavcodec/h264_sei.c
+++ b/libavcodec/h264_sei.c
@@ -55,7 +55,9 @@ void ff_h264_sei_uninit(H264SEIContext *h)
     h->picture_timing.present      = 0;
     h->buffering_period.present    = 0;
     h->common.frame_packing.present       = 0;
+#if CONFIG_H264_FILM_GRAIN
     h->common.film_grain_characteristics.present = 0;
+#endif
     h->common.display_orientation.present = 0;
     h->common.afd.present                 =  0;
 
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index a66b75ca80..436449171f 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -516,8 +516,12 @@ static int h264_frame_start(H264Context *h)
     pic->f->crop_top    = h->crop_top;
     pic->f->crop_bottom = h->crop_bottom;
 
+#if CONFIG_H264_FILM_GRAIN
     pic->needs_fg = h->sei.common.film_grain_characteristics.present && !h->avctx->hwaccel &&
         !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
+#else
+    pic->needs_fg = 0;
+#endif
 
     if ((ret = alloc_picture(h, pic)) < 0)
         return ret;
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index ccd7583bf4..82b78ec5ab 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -344,7 +344,9 @@ typedef struct H264Context {
     H264DSPContext h264dsp;
     H264ChromaContext h264chroma;
     H264QpelContext h264qpel;
+#if CONFIG_H264_FILM_GRAIN
     H274FilmGrainDatabase h274db;
+#endif
 
     H264Picture DPB[H264_MAX_PICTURE_COUNT];
     H264Picture *cur_pic_ptr;
-- 
2.46.0.76.ge559c4bf1a-goog

