Line |
Branch |
Exec |
Source |
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 |
|
|
|
22 |
|
✗ |
AVFilmGrainParams *av_film_grain_params_alloc(size_t *size) |
23 |
|
|
{ |
24 |
|
✗ |
AVFilmGrainParams *params = av_mallocz(sizeof(AVFilmGrainParams)); |
25 |
|
|
|
26 |
|
✗ |
if (size) |
27 |
|
✗ |
*size = sizeof(*params); |
28 |
|
|
|
29 |
|
✗ |
return params; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
✗ |
AVFilmGrainParams *av_film_grain_params_create_side_data(AVFrame *frame) |
33 |
|
|
{ |
34 |
|
|
AVFilmGrainParams *fgp; |
35 |
|
✗ |
AVFrameSideData *side_data = av_frame_new_side_data(frame, |
36 |
|
|
AV_FRAME_DATA_FILM_GRAIN_PARAMS, |
37 |
|
|
sizeof(AVFilmGrainParams)); |
38 |
|
✗ |
if (!side_data) |
39 |
|
✗ |
return NULL; |
40 |
|
|
|
41 |
|
✗ |
fgp = (AVFilmGrainParams *) side_data->data; |
42 |
|
✗ |
*fgp = (AVFilmGrainParams) { |
43 |
|
|
.color_range = AVCOL_RANGE_UNSPECIFIED, |
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 |
|
|
|
52 |
|
✗ |
const AVFilmGrainParams *av_film_grain_params_select(const AVFrame *frame) |
53 |
|
|
{ |
54 |
|
✗ |
const AVFilmGrainParams *fgp, *best = NULL; |
55 |
|
✗ |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); |
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++) { |
65 |
|
✗ |
if (frame->side_data[i]->type != AV_FRAME_DATA_FILM_GRAIN_PARAMS) |
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); |
78 |
|
✗ |
CHECK(fgp->color_range, frame->color_range, AVCOL_RANGE_UNSPECIFIED); |
79 |
|
✗ |
CHECK(fgp->color_primaries, frame->color_primaries, AVCOL_PRI_UNSPECIFIED); |
80 |
|
✗ |
CHECK(fgp->color_trc, frame->color_trc, AVCOL_TRC_UNSPECIFIED); |
81 |
|
✗ |
CHECK(fgp->color_space, frame->colorspace, AVCOL_SPC_UNSPECIFIED); |
82 |
|
|
|
83 |
|
✗ |
switch (fgp->type) { |
84 |
|
✗ |
case AV_FILM_GRAIN_PARAMS_NONE: |
85 |
|
✗ |
continue; |
86 |
|
✗ |
case AV_FILM_GRAIN_PARAMS_AV1: |
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; |
92 |
|
✗ |
case AV_FILM_GRAIN_PARAMS_H274: |
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 |
|
|
} |
106 |
|
|
|