Files
François Doray 9badbee44d Heap-allocate H.274 film grain tables in h274.c on first use
In Chrome-branded builds, libavcodec/h274.c places 700,440 bytes of film
grain buffers in chrome.dll's .bss (`film_grain_db` at 692,248 B and
`init_slice_c::tmp` at 8,192 B), even though these buffers are only
touched when software H.264 decoding encounters an H.274 Film Grain
Characteristics SEI. On Windows, the PE loader pre-commits chrome.dll's
entire .bss in every process.

Move `init_slice_c::tmp` into `H274FilmGrainDatabase` (700,440 B total)
and allocate `film_grain_db` on the heap via `ff_thread_once()` on the
first call to `ff_h274_apply_film_grain()`. This is safe because:
- `ff_h274_apply_film_grain()` is the sole entry point that accesses
  `film_grain_db`. Its callers in `h264_picture.c` and `vvc/dec.c`
  already handle negative error returns (`AVERROR(ENOMEM)`), and this CL
  updates `hevc_frame_end()` in `hevc/hevcdec.c` to propagate negative
  returns instead of asserting `ret >= 0`.
- `init_slice_c()` is only called while holding `init_slice()`'s static
  mutex, so storing `tmp` in `H274FilmGrainDatabase` preserves the same
  thread safety without a large stack allocation.

Impact (Windows x64 official Chrome-branded chrome.dll):
- .data VirtualSize: 3,758,744 B -> 3,058,328 B (-684 KiB /
  ~-9.1 MiB at p50 process count).
- Binary file size (.text/.data RawSize): +0 B.
2026-10-20 00:36:30 +00:00

82 lines
2.7 KiB
C

/*
* H.274 film grain synthesis
* Copyright (c) 2021 Niklas Haas <ffmpeg@haasn.xyz>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* H.274 film grain synthesis.
* @author Niklas Haas <ffmpeg@haasn.xyz>
*/
#ifndef AVCODEC_H274_H
#define AVCODEC_H274_H
#include "libavutil/film_grain_params.h"
/**
* Check whether ff_h274_apply_film_grain() supports the given parameter combination.
*
* @param model_id model_id from AVFilmGrainParams to be supplied
* @param pix_fmt pixel format of the frames to be supplied
*/
static inline int ff_h274_film_grain_params_supported(int model_id, enum AVPixelFormat pix_fmt)
{
return model_id == 0 && pix_fmt == AV_PIX_FMT_YUV420P;
}
// Synthesizes film grain on top of `in` and stores the result to `out`. `out`
// must already have been allocated and set to the same size and format as
// `in`.
//
// Returns a negative error code on error, such as invalid params or
// AVERROR(ENOMEM). If ff_h274_film_grain_params_supported() indicated that the
// parameters are supported and the arguments given to
// ff_h274_film_grain_params_supported() coincide with actual values from the
// frames and params, only AVERROR(ENOMEM) can be returned.
int ff_h274_apply_film_grain(AVFrame *out, const AVFrame *in,
const AVFilmGrainParams *params);
typedef struct H274HashContext H274HashContext;
typedef struct H274SEIPictureHash {
int present;
union {
uint8_t md5[3][16];
uint16_t crc[3];
uint32_t checksum[3];
};
uint8_t hash_type;
} H274SEIPictureHash;
int ff_h274_hash_init(H274HashContext **c, int type);
int ff_h274_hash_verify(H274HashContext *c, const H274SEIPictureHash *hash,
const AVFrame *frame, int coded_width, int coded_height);
void ff_h274_hash_freep(H274HashContext **c);
typedef struct H274SEIFrameFieldInfo {
int present;
int picture_struct;
uint8_t display_elemental_periods;
uint8_t source_scan_type;
uint8_t duplicate_flag;
} H274SEIFrameFieldInfo;
#endif /* AVCODEC_H274_H */