]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/cbs_vp9_syntax_template.c
avformat/mpegtsenc: Fix mpegts_write_pes() for private_stream_2 and other types
[ffmpeg] / libavcodec / cbs_vp9_syntax_template.c
index 0db0f52a6dbaa98bc3cab58d3bb82af7237601d5..2f08eccf180e93b619f448c62c58b3702bb28616 100644 (file)
 static int FUNC(frame_sync_code)(CodedBitstreamContext *ctx, RWContext *rw,
                                  VP9RawFrameHeader *current)
 {
-    uint8_t frame_sync_byte_0 = VP9_FRAME_SYNC_0;
-    uint8_t frame_sync_byte_1 = VP9_FRAME_SYNC_1;
-    uint8_t frame_sync_byte_2 = VP9_FRAME_SYNC_2;
     int err;
 
-    xf(8, frame_sync_byte_0, frame_sync_byte_0, 0);
-    xf(8, frame_sync_byte_1, frame_sync_byte_1, 0);
-    xf(8, frame_sync_byte_2, frame_sync_byte_2, 0);
-
-    if (frame_sync_byte_0 != VP9_FRAME_SYNC_0 ||
-        frame_sync_byte_1 != VP9_FRAME_SYNC_1 ||
-        frame_sync_byte_2 != VP9_FRAME_SYNC_2) {
-        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid frame sync code: "
-               "%02x %02x %02x.\n", frame_sync_byte_0,
-               frame_sync_byte_1, frame_sync_byte_2);
-        return AVERROR_INVALIDDATA;
-    }
+    fixed(8, frame_sync_byte_0, VP9_FRAME_SYNC_0);
+    fixed(8, frame_sync_byte_1, VP9_FRAME_SYNC_1);
+    fixed(8, frame_sync_byte_2, VP9_FRAME_SYNC_2);
 
     return 0;
 }
@@ -43,10 +31,14 @@ static int FUNC(frame_sync_code)(CodedBitstreamContext *ctx, RWContext *rw,
 static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
                               VP9RawFrameHeader *current, int profile)
 {
+    CodedBitstreamVP9Context *vp9 = ctx->priv_data;
     int err;
 
-    if (profile >= 2)
+    if (profile >= 2) {
         f(1, ten_or_twelve_bit);
+        vp9->bit_depth = current->ten_or_twelve_bit ? 12 : 10;
+    } else
+        vp9->bit_depth = 8;
 
     f(3, color_space);
 
@@ -55,7 +47,7 @@ static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
         if (profile == 1 || profile == 3) {
             f(1, subsampling_x);
             f(1, subsampling_y);
-            f(1, color_config_reserved_zero);
+            fixed(1, reserved_zero, 0);
         } else {
             infer(subsampling_x, 1);
             infer(subsampling_y, 1);
@@ -65,9 +57,13 @@ static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
         if (profile == 1 || profile == 3) {
             infer(subsampling_x, 0);
             infer(subsampling_y, 0);
+            fixed(1, reserved_zero, 0);
         }
     }
 
+    vp9->subsampling_x = current->subsampling_x;
+    vp9->subsampling_y = current->subsampling_y;
+
     return 0;
 }
 
@@ -80,8 +76,11 @@ static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
     f(16, frame_width_minus_1);
     f(16, frame_height_minus_1);
 
-    vp9->mi_cols = (current->frame_width_minus_1  + 8) >> 3;
-    vp9->mi_rows = (current->frame_height_minus_1 + 8) >> 3;
+    vp9->frame_width  = current->frame_width_minus_1  + 1;
+    vp9->frame_height = current->frame_height_minus_1 + 1;
+
+    vp9->mi_cols = (vp9->frame_width  + 7) >> 3;
+    vp9->mi_rows = (vp9->frame_height + 7) >> 3;
     vp9->sb64_cols = (vp9->mi_cols + 7) >> 3;
     vp9->sb64_rows = (vp9->mi_rows + 7) >> 3;
 
@@ -106,15 +105,33 @@ static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
 static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
                                       VP9RawFrameHeader *current)
 {
+    CodedBitstreamVP9Context *vp9 = ctx->priv_data;
     int err, i;
 
     for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
         fs(1, found_ref[i], 1, i);
-        if (current->found_ref[i])
+        if (current->found_ref[i]) {
+            VP9ReferenceFrameState *ref =
+                &vp9->ref[current->ref_frame_idx[i]];
+
+            vp9->frame_width   = ref->frame_width;
+            vp9->frame_height  = ref->frame_height;
+
+            vp9->subsampling_x = ref->subsampling_x;
+            vp9->subsampling_y = ref->subsampling_y;
+            vp9->bit_depth     = ref->bit_depth;
+
             break;
+        }
     }
     if (i >= VP9_REFS_PER_FRAME)
         CHECK(FUNC(frame_size)(ctx, rw, current));
+    else {
+        vp9->mi_cols = (vp9->frame_width  + 7) >> 3;
+        vp9->mi_rows = (vp9->frame_height + 7) >> 3;
+        vp9->sb64_cols = (vp9->mi_cols + 7) >> 3;
+        vp9->sb64_rows = (vp9->mi_rows + 7) >> 3;
+    }
     CHECK(FUNC(render_size)(ctx, rw, current));
 
     return 0;
@@ -177,8 +194,8 @@ static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
 static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
                                      VP9RawFrameHeader *current)
 {
-    static const int segmentation_feature_bits[VP9_SEG_LVL_MAX]   = { 8, 6, 2, 0 };
-    static const int segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 };
+    static const uint8_t segmentation_feature_bits[VP9_SEG_LVL_MAX]   = { 8, 6, 2, 0 };
+    static const uint8_t segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 };
 
     int err, i, j;
 
@@ -248,16 +265,16 @@ static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
 static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
                                      VP9RawFrameHeader *current)
 {
-    int profile, i;
-    int err;
+    CodedBitstreamVP9Context *vp9 = ctx->priv_data;
+    int err, i;
 
     f(2, frame_marker);
 
     f(1, profile_low_bit);
     f(1, profile_high_bit);
-    profile = (current->profile_high_bit << 1) + current->profile_low_bit;
-    if (profile == 3)
-        f(1, profile_reserved_zero);
+    vp9->profile = (current->profile_high_bit << 1) + current->profile_low_bit;
+    if (vp9->profile == 3)
+        fixed(1, reserved_zero, 0);
 
     f(1, show_existing_frame);
     if (current->show_existing_frame) {
@@ -274,7 +291,7 @@ static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
 
     if (current->frame_type == VP9_KEY_FRAME) {
         CHECK(FUNC(frame_sync_code)(ctx, rw, current));
-        CHECK(FUNC(color_config)(ctx, rw, current, profile));
+        CHECK(FUNC(color_config)(ctx, rw, current, vp9->profile));
         CHECK(FUNC(frame_size)(ctx, rw, current));
         CHECK(FUNC(render_size)(ctx, rw, current));
 
@@ -294,12 +311,16 @@ static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
          if (current->intra_only == 1) {
              CHECK(FUNC(frame_sync_code)(ctx, rw, current));
 
-             if (profile > 0) {
-                 CHECK(FUNC(color_config)(ctx, rw, current, profile));
+             if (vp9->profile > 0) {
+                 CHECK(FUNC(color_config)(ctx, rw, current, vp9->profile));
              } else {
                  infer(color_space,   1);
                  infer(subsampling_x, 1);
                  infer(subsampling_y, 1);
+                 vp9->bit_depth = 8;
+
+                 vp9->subsampling_x = current->subsampling_x;
+                 vp9->subsampling_y = current->subsampling_y;
              }
 
              f(8, refresh_frame_flags);
@@ -338,15 +359,33 @@ static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
 
     f(16, header_size_in_bytes);
 
+    for (i = 0; i < VP9_NUM_REF_FRAMES; i++) {
+        if (current->refresh_frame_flags & (1 << i)) {
+            vp9->ref[i] = (VP9ReferenceFrameState) {
+                .frame_width    = vp9->frame_width,
+                .frame_height   = vp9->frame_height,
+                .subsampling_x  = vp9->subsampling_x,
+                .subsampling_y  = vp9->subsampling_y,
+                .bit_depth      = vp9->bit_depth,
+            };
+        }
+    }
+
+    av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame:  size %dx%d  "
+           "subsample %dx%d  bit_depth %d  tiles %dx%d.\n",
+           vp9->frame_width, vp9->frame_height,
+           vp9->subsampling_x, vp9->subsampling_y,
+           vp9->bit_depth, 1 << current->tile_cols_log2,
+           1 << current->tile_rows_log2);
+
     return 0;
 }
 
 static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
 {
     int err;
-    av_unused int zero = 0;
     while (byte_alignment(rw) != 0)
-        xf(1, zero_bit, zero, 0);
+        fixed(1, zero_bit, 0);
 
     return 0;
 }