FFmpeg
film_grain_params.c
Go to the documentation of this file.
1 /**
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "film_grain_params.h"
20 #include "pixdesc.h"
21 
23 {
25 
26  if (size)
27  *size = sizeof(*params);
28 
29  return params;
30 }
31 
33 {
34  AVFilmGrainParams *fgp;
37  sizeof(AVFilmGrainParams));
38  if (!side_data)
39  return NULL;
40 
41  fgp = (AVFilmGrainParams *) side_data->data;
42  *fgp = (AVFilmGrainParams) {
44  .color_primaries = AVCOL_PRI_UNSPECIFIED,
45  .color_trc = AVCOL_TRC_UNSPECIFIED,
46  .color_space = AVCOL_SPC_UNSPECIFIED,
47  };
48 
49  return fgp;
50 }
51 
53 {
54  const AVFilmGrainParams *fgp, *best = NULL;
56  int bit_depth_luma, bit_depth_chroma;
57  if (!desc)
58  return NULL;
59 
60  /* There are no YUV formats with different bit depth per component,
61  * so just check both against the first component for simplicity */
62  bit_depth_luma = bit_depth_chroma = desc->comp[0].depth;
63 
64  for (int i = 0; i < frame->nb_side_data; i++) {
66  continue;
67  fgp = (const AVFilmGrainParams*)frame->side_data[i]->data;
68  if (fgp->width && fgp->width > frame->width ||
69  fgp->height && fgp->height > frame->height)
70  continue;
71 
72 #define CHECK(a, b, unspec) \
73  if ((a) != (unspec) && (b) != (unspec) && (a) != (b)) \
74  continue
75 
76  CHECK(fgp->bit_depth_luma, bit_depth_luma, 0);
77  CHECK(fgp->bit_depth_chroma, bit_depth_chroma, 0);
82 
83  switch (fgp->type) {
85  continue;
87  /* AOM FGS needs an exact match for the chroma resolution */
88  if (fgp->subsampling_x != desc->log2_chroma_w ||
89  fgp->subsampling_y != desc->log2_chroma_h)
90  continue;
91  break;
93  /* H.274 FGS can be adapted to any lower chroma resolution */
94  if (fgp->subsampling_x > desc->log2_chroma_w ||
95  fgp->subsampling_y > desc->log2_chroma_h)
96  continue;
97  break;
98  }
99 
100  if (!best || best->width < fgp->width || best->height < fgp->height)
101  best = fgp;
102  }
103 
104  return best;
105 }
AVFrame::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:627
AVFrame::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:623
AVFilmGrainParams::bit_depth_luma
int bit_depth_luma
Intended bit depth, or 0 for unknown/unspecified.
Definition: film_grain_params.h:287
av_frame_new_side_data
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, size_t size)
Add a new side data to a frame.
Definition: frame.c:765
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
av_film_grain_params_alloc
AVFilmGrainParams * av_film_grain_params_alloc(size_t *size)
This file is part of FFmpeg.
Definition: film_grain_params.c:22
AVFrame::nb_side_data
int nb_side_data
Definition: frame.h:578
AV_FRAME_DATA_FILM_GRAIN_PARAMS
@ AV_FRAME_DATA_FILM_GRAIN_PARAMS
Film grain parameters for a frame, described by AVFilmGrainParams.
Definition: frame.h:188
AVFrame::color_primaries
enum AVColorPrimaries color_primaries
Definition: frame.h:625
AVFrame::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:634
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
pixdesc.h
AVFrame::width
int width
Definition: frame.h:416
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:583
AVFilmGrainParams::color_space
enum AVColorSpace color_space
Definition: film_grain_params.h:282
AVFilmGrainParams::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: film_grain_params.h:281
av_film_grain_params_select
const AVFilmGrainParams * av_film_grain_params_select(const AVFrame *frame)
Select the most appropriate film grain parameters set for the frame, taking into account the frame's ...
Definition: film_grain_params.c:52
AVFilmGrainParams::bit_depth_chroma
int bit_depth_chroma
Definition: film_grain_params.h:288
film_grain_params.h
AVFilmGrainParams::width
int width
Intended display resolution.
Definition: film_grain_params.h:269
av_film_grain_params_create_side_data
AVFilmGrainParams * av_film_grain_params_create_side_data(AVFrame *frame)
Allocate a complete AVFilmGrainParams and add it to the frame.
Definition: film_grain_params.c:32
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:558
AV_FILM_GRAIN_PARAMS_NONE
@ AV_FILM_GRAIN_PARAMS_NONE
Definition: film_grain_params.h:25
frame
static AVFrame * frame
Definition: demux_decode.c:54
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
AVFilmGrainParams::subsampling_x
int subsampling_x
Intended subsampling ratio, or 0 for luma-only streams.
Definition: film_grain_params.h:274
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:649
size
int size
Definition: twinvq_data.h:10344
CHECK
#define CHECK(a, b, unspec)
AVFrameSideData::data
uint8_t * data
Definition: frame.h:252
AVFilmGrainParams
This structure describes how to handle film grain synthesis in video for specific codecs.
Definition: film_grain_params.h:238
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:431
AVFilmGrainParams::color_primaries
enum AVColorPrimaries color_primaries
Definition: film_grain_params.h:280
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVFilmGrainParams::subsampling_y
int subsampling_y
Definition: film_grain_params.h:274
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVFrame::side_data
AVFrameSideData ** side_data
Definition: frame.h:577
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:612
AVFilmGrainParams::height
int height
Definition: film_grain_params.h:269
AVFrame::height
int height
Definition: frame.h:416
AV_FILM_GRAIN_PARAMS_H274
@ AV_FILM_GRAIN_PARAMS_H274
The union is valid when interpreted as AVFilmGrainH274Params (codec.h274)
Definition: film_grain_params.h:35
AVFrameSideData::type
enum AVFrameSideDataType type
Definition: frame.h:251
AVFilmGrainParams::color_range
enum AVColorRange color_range
Intended video signal characteristics.
Definition: film_grain_params.h:279
desc
const char * desc
Definition: libsvtav1.c:75
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:250
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AV_FILM_GRAIN_PARAMS_AV1
@ AV_FILM_GRAIN_PARAMS_AV1
The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
Definition: film_grain_params.h:30
AVFilmGrainParams::type
enum AVFilmGrainParamsType type
Specifies the codec for which this structure is valid.
Definition: film_grain_params.h:242