diff mbox

[FFmpeg-devel,2/2] avcodec/cbs_av1: keep separate reference frame state for reading and writing

Message ID 20191115183805.1736-2-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer Nov. 15, 2019, 6:38 p.m. UTC
In scearios where a Temporal Unit is written right after reading it using the same
CBS context (av1_metadata, av1_frame_merge, etc), the reference frame state used
by the writer must not be the state that's the result of the reader having already
parsed the current frame in question.

This fixes writing frames of type Switch.

Signed-off-by: James Almer <jamrial@gmail.com>
---
This may be needed for other modules as well. I see at least VP9 and h264/hevc
keeping some sort of state.

Alternatively, we could just require a given CBS context to be used to either read
or write, but not both.

 libavcodec/cbs_av1.c | 14 ++++++++++++++
 libavcodec/cbs_av1.h |  4 +++-
 2 files changed, 17 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c
index cca112ecf8..ddd8ea955d 100644
--- a/libavcodec/cbs_av1.c
+++ b/libavcodec/cbs_av1.c
@@ -640,6 +640,8 @@  static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
 
 #define byte_alignment(rw) (get_bits_count(rw) % 8)
 
+#define ref read_ref
+
 #include "cbs_av1_syntax_template.c"
 
 #undef READ
@@ -655,6 +657,7 @@  static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
 #undef leb128
 #undef infer
 #undef byte_alignment
+#undef ref
 
 
 #define WRITE
@@ -717,6 +720,8 @@  static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
 
 #define byte_alignment(rw) (put_bits_count(rw) % 8)
 
+#define ref write_ref
+
 #include "cbs_av1_syntax_template.c"
 
 #undef WRITE
@@ -732,6 +737,7 @@  static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
 #undef leb128
 #undef infer
 #undef byte_alignment
+#undef ref
 
 
 static int cbs_av1_split_fragment(CodedBitstreamContext *ctx,
@@ -973,6 +979,8 @@  static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
                                                 unit->data_ref);
             if (err < 0)
                 return err;
+
+            priv->ref = (AV1ReferenceFrameState *)&priv->read_ref;
         }
         break;
     case AV1_OBU_TILE_GROUP:
@@ -995,6 +1003,8 @@  static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
             if (err < 0)
                 return err;
 
+            priv->ref = (AV1ReferenceFrameState *)&priv->read_ref;
+
             err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
                                         &obu->obu.frame.tile_group.tile_data);
             if (err < 0)
@@ -1115,6 +1125,8 @@  static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
                                                  NULL);
             if (err < 0)
                 return err;
+
+            priv->ref = (AV1ReferenceFrameState *)&priv->write_ref;
         }
         break;
     case AV1_OBU_TILE_GROUP:
@@ -1134,6 +1146,8 @@  static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
                 return err;
 
             td = &obu->obu.frame.tile_group.tile_data;
+
+            priv->ref = (AV1ReferenceFrameState *)&priv->write_ref;
         }
         break;
     case AV1_OBU_TILE_LIST:
diff --git a/libavcodec/cbs_av1.h b/libavcodec/cbs_av1.h
index 1fb668ada4..675ed28f60 100644
--- a/libavcodec/cbs_av1.h
+++ b/libavcodec/cbs_av1.h
@@ -441,7 +441,9 @@  typedef struct CodedBitstreamAV1Context {
     int tile_cols;
     int tile_rows;
 
-    AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES];
+    AV1ReferenceFrameState *ref;
+    AV1ReferenceFrameState read_ref[AV1_NUM_REF_FRAMES];
+    AV1ReferenceFrameState write_ref[AV1_NUM_REF_FRAMES];
 
     // Write buffer.
     uint8_t *write_buffer;