| 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 "mem.h" | ||
| 21 | #include "pixdesc.h" | ||
| 22 | |||
| 23 | 2 | AVFilmGrainParams *av_film_grain_params_alloc(size_t *size) | |
| 24 | { | ||
| 25 | 2 | AVFilmGrainParams *params = av_mallocz(sizeof(AVFilmGrainParams)); | |
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (size) |
| 28 | 1 | *size = sizeof(*params); | |
| 29 | |||
| 30 | 2 | return params; | |
| 31 | } | ||
| 32 | |||
| 33 | 22 | AVFilmGrainParams *av_film_grain_params_create_side_data(AVFrame *frame) | |
| 34 | { | ||
| 35 | AVFilmGrainParams *fgp; | ||
| 36 | 22 | AVFrameSideData *side_data = av_frame_new_side_data(frame, | |
| 37 | AV_FRAME_DATA_FILM_GRAIN_PARAMS, | ||
| 38 | sizeof(AVFilmGrainParams)); | ||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!side_data) |
| 40 | ✗ | return NULL; | |
| 41 | |||
| 42 | 22 | fgp = (AVFilmGrainParams *) side_data->data; | |
| 43 | 22 | *fgp = (AVFilmGrainParams) { | |
| 44 | .color_range = AVCOL_RANGE_UNSPECIFIED, | ||
| 45 | .color_primaries = AVCOL_PRI_UNSPECIFIED, | ||
| 46 | .color_trc = AVCOL_TRC_UNSPECIFIED, | ||
| 47 | .color_space = AVCOL_SPC_UNSPECIFIED, | ||
| 48 | }; | ||
| 49 | |||
| 50 | 22 | return fgp; | |
| 51 | } | ||
| 52 | |||
| 53 | 21 | const AVFilmGrainParams *av_film_grain_params_select(const AVFrame *frame) | |
| 54 | { | ||
| 55 | 21 | const AVFilmGrainParams *fgp, *best = NULL; | |
| 56 | 21 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); | |
| 57 | int bit_depth_luma, bit_depth_chroma; | ||
| 58 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
|
21 | if (!desc) |
| 59 | 1 | return NULL; | |
| 60 | |||
| 61 | /* There are no YUV formats with different bit depth per component, | ||
| 62 | * so just check both against the first component for simplicity */ | ||
| 63 | 20 | bit_depth_luma = bit_depth_chroma = desc->comp[0].depth; | |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 20 times.
|
41 | for (int i = 0; i < frame->nb_side_data; i++) { |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (frame->side_data[i]->type != AV_FRAME_DATA_FILM_GRAIN_PARAMS) |
| 67 | ✗ | continue; | |
| 68 | 21 | fgp = (const AVFilmGrainParams*)frame->side_data[i]->data; | |
| 69 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
21 | if (fgp->width && fgp->width > frame->width || |
| 70 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
|
20 | fgp->height && fgp->height > frame->height) |
| 71 | 2 | continue; | |
| 72 | |||
| 73 | #define CHECK(a, b, unspec) \ | ||
| 74 | if ((a) != (unspec) && (b) != (unspec) && (a) != (b)) \ | ||
| 75 | continue | ||
| 76 | |||
| 77 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
19 | CHECK(fgp->bit_depth_luma, bit_depth_luma, 0); |
| 78 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
18 | CHECK(fgp->bit_depth_chroma, bit_depth_chroma, 0); |
| 79 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
17 | CHECK(fgp->color_range, frame->color_range, AVCOL_RANGE_UNSPECIFIED); |
| 80 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
16 | CHECK(fgp->color_primaries, frame->color_primaries, AVCOL_PRI_UNSPECIFIED); |
| 81 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
15 | CHECK(fgp->color_trc, frame->color_trc, AVCOL_TRC_UNSPECIFIED); |
| 82 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
14 | CHECK(fgp->color_space, frame->colorspace, AVCOL_SPC_UNSPECIFIED); |
| 83 | |||
| 84 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
13 | switch (fgp->type) { |
| 85 | 1 | case AV_FILM_GRAIN_PARAMS_NONE: | |
| 86 | 1 | continue; | |
| 87 | 9 | case AV_FILM_GRAIN_PARAMS_AV1: | |
| 88 | /* AOM FGS needs an exact match for the chroma resolution */ | ||
| 89 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | if (fgp->subsampling_x != desc->log2_chroma_w || |
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | fgp->subsampling_y != desc->log2_chroma_h) |
| 91 | 1 | continue; | |
| 92 | 8 | break; | |
| 93 | 3 | case AV_FILM_GRAIN_PARAMS_H274: | |
| 94 | /* H.274 FGS can be adapted to any lower chroma resolution */ | ||
| 95 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (fgp->subsampling_x > desc->log2_chroma_w || |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | fgp->subsampling_y > desc->log2_chroma_h) |
| 97 | 1 | continue; | |
| 98 | 2 | break; | |
| 99 | } | ||
| 100 | |||
| 101 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
10 | if (!best || best->width < fgp->width || best->height < fgp->height) |
| 102 | 10 | best = fgp; | |
| 103 | } | ||
| 104 | |||
| 105 | 20 | return best; | |
| 106 | } | ||
| 107 |