diff mbox series

[FFmpeg-devel,3/3] avcodec/nvdec_av1: fix setting film grain parameters for frames with update_grain == 0

Message ID 20201112205213.1179-3-jamrial@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/3] avcodec/av1dec: add a reference to the raw frame header to AV1Frames | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

James Almer Nov. 12, 2020, 8:52 p.m. UTC
The spec in section 6.8.20 states the parameters should be loaded from a
reference frame indexed by film_grain_params_ref_idx.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Untested.

 libavcodec/nvdec_av1.c | 58 ++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

Comments

Timo Rothenpieler Nov. 12, 2020, 9:19 p.m. UTC | #1
Are you sure this is necessary at all?
If apply_grain is 0, the value of those should be entirely irrelevant.
Timo Rothenpieler Nov. 12, 2020, 9:21 p.m. UTC | #2
On 12.11.2020 22:19, Timo Rothenpieler wrote:
> Are you sure this is necessary at all?
> If apply_grain is 0, the value of those should be entirely irrelevant.

As in, just always set them, and just move the fg_header definition to 
the top.
James Almer Nov. 12, 2020, 9:23 p.m. UTC | #3
On 11/12/2020 6:19 PM, Timo Rothenpieler wrote:
> Are you sure this is necessary at all?
> If apply_grain is 0, the value of those should be entirely irrelevant.

This is not about apply_grain == 0, but about apply_grain == 1 && 
update_grain == 0.
In those cases, the parser only reads a frame index from the bitstream 
(film_grain_params_ref_idx), and you're supposed to get the FG values 
from said referenced frame.

As is, nvdec_av1 is filling all CUVIDAV1PICPARAMS FG fields except 
apply_grain and random_seed with zeroes on such frames.
Timo Rothenpieler Nov. 12, 2020, 9:38 p.m. UTC | #4
On 12.11.2020 22:23, James Almer wrote:
> On 11/12/2020 6:19 PM, Timo Rothenpieler wrote:
>> Are you sure this is necessary at all?
>> If apply_grain is 0, the value of those should be entirely irrelevant.
> 
> This is not about apply_grain == 0, but about apply_grain == 1 && 
> update_grain == 0.
> In those cases, the parser only reads a frame index from the bitstream 
> (film_grain_params_ref_idx), and you're supposed to get the FG values 
> from said referenced frame.
> 
> As is, nvdec_av1 is filling all CUVIDAV1PICPARAMS FG fields except 
> apply_grain and random_seed with zeroes on such frames.

Yes, but you still could just define the fg_header at the top of the 
function, and just fill the fields from it unconditionally.
It will contain garbage on frames with !apply_grain, but it shouldn't 
matter.

It's purely a nit about the style, since I'm trying to keep as much 
stuff in the struct initialization.
diff mbox series

Patch

diff --git a/libavcodec/nvdec_av1.c b/libavcodec/nvdec_av1.c
index 9da1c840e3..5117c97eda 100644
--- a/libavcodec/nvdec_av1.c
+++ b/libavcodec/nvdec_av1.c
@@ -182,23 +182,7 @@  static int nvdec_av1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
 
             /* Film Grain Params */
             .apply_grain              = frame_header->apply_grain,
-            .overlap_flag             = frame_header->overlap_flag,
-            .scaling_shift_minus8     = frame_header->grain_scaling_minus_8,
-            .chroma_scaling_from_luma = frame_header->chroma_scaling_from_luma,
-            .ar_coeff_lag             = frame_header->ar_coeff_lag,
-            .ar_coeff_shift_minus6    = frame_header->ar_coeff_shift_minus_6,
-            .grain_scale_shift        = frame_header->grain_scale_shift,
-            .clip_to_restricted_range = frame_header->clip_to_restricted_range,
-            .num_y_points             = frame_header->num_y_points,
-            .num_cb_points            = frame_header->num_cb_points,
-            .num_cr_points            = frame_header->num_cr_points,
             .random_seed              = frame_header->grain_seed,
-            .cb_mult                  = frame_header->cb_mult,
-            .cb_luma_mult             = frame_header->cb_luma_mult,
-            .cb_offset                = frame_header->cb_offset,
-            .cr_mult                  = frame_header->cr_mult,
-            .cr_luma_mult             = frame_header->cr_luma_mult,
-            .cr_offset                = frame_header->cr_offset
         }
     };
 
@@ -259,22 +243,46 @@  static int nvdec_av1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
 
     /* Film Grain Params */
     if (frame_header->apply_grain) {
+        const AV1RawFrameHeader *fg_header;
+
+        if (frame_header->update_grain)
+            fg_header = frame_header;
+        else
+            fg_header = s->ref[frame_header->film_grain_params_ref_idx].raw_frame_header;
+
+        ppc->overlap_flag             = fg_header->overlap_flag;
+        ppc->scaling_shift_minus8     = fg_header->grain_scaling_minus_8;
+        ppc->chroma_scaling_from_luma = fg_header->chroma_scaling_from_luma;
+        ppc->ar_coeff_lag             = fg_header->ar_coeff_lag;
+        ppc->ar_coeff_shift_minus6    = fg_header->ar_coeff_shift_minus_6;
+        ppc->grain_scale_shift        = fg_header->grain_scale_shift;
+        ppc->clip_to_restricted_range = fg_header->clip_to_restricted_range;
+        ppc->num_y_points             = fg_header->num_y_points;
+        ppc->num_cb_points            = fg_header->num_cb_points;
+        ppc->num_cr_points            = fg_header->num_cr_points;
+        ppc->cb_mult                  = fg_header->cb_mult;
+        ppc->cb_luma_mult             = fg_header->cb_luma_mult;
+        ppc->cb_offset                = fg_header->cb_offset;
+        ppc->cr_mult                  = fg_header->cr_mult;
+        ppc->cr_luma_mult             = fg_header->cr_luma_mult;
+        ppc->cr_offset                = fg_header->cr_offset;
+
         for (i = 0; i < 14; ++i) {
-            ppc->scaling_points_y[i][0] = frame_header->point_y_value[i];
-            ppc->scaling_points_y[i][1] = frame_header->point_y_scaling[i];
+            ppc->scaling_points_y[i][0] = fg_header->point_y_value[i];
+            ppc->scaling_points_y[i][1] = fg_header->point_y_scaling[i];
         }
         for (i = 0; i < 10; ++i) {
-            ppc->scaling_points_cb[i][0] = frame_header->point_cb_value[i];
-            ppc->scaling_points_cb[i][1] = frame_header->point_cb_scaling[i];
-            ppc->scaling_points_cr[i][0] = frame_header->point_cr_value[i];
-            ppc->scaling_points_cr[i][1] = frame_header->point_cr_scaling[i];
+            ppc->scaling_points_cb[i][0] = fg_header->point_cb_value[i];
+            ppc->scaling_points_cb[i][1] = fg_header->point_cb_scaling[i];
+            ppc->scaling_points_cr[i][0] = fg_header->point_cr_value[i];
+            ppc->scaling_points_cr[i][1] = fg_header->point_cr_scaling[i];
         }
         for (i = 0; i < 24; ++i) {
-            ppc->ar_coeffs_y[i] = (short)frame_header->ar_coeffs_y_plus_128[i] - 128;
+            ppc->ar_coeffs_y[i] = (short)fg_header->ar_coeffs_y_plus_128[i] - 128;
         }
         for (i = 0; i < 25; ++i) {
-            ppc->ar_coeffs_cb[i] = (short)frame_header->ar_coeffs_cb_plus_128[i] - 128;
-            ppc->ar_coeffs_cr[i] = (short)frame_header->ar_coeffs_cr_plus_128[i] - 128;
+            ppc->ar_coeffs_cb[i] = (short)fg_header->ar_coeffs_cb_plus_128[i] - 128;
+            ppc->ar_coeffs_cr[i] = (short)fg_header->ar_coeffs_cr_plus_128[i] - 128;
         }
     }