mpeg2enc: Keep the packed data consistent with each parameters
[profile/ivi/libva.git] / test / encode / mpeg2enc.c
index 29d2774..73f66be 100644 (file)
@@ -62,8 +62,6 @@
         exit(1);                                                        \
     }
 
-#define INTRA_PERIOD            30
-
 static int const picture_type_patter[][2] = {{VAEncPictureTypeIntra, 1}, 
                                              {VAEncPictureTypePredictive, 3}, {VAEncPictureTypePredictive, 3},{VAEncPictureTypePredictive, 3},
                                              {VAEncPictureTypePredictive, 3}, {VAEncPictureTypePredictive, 3},{VAEncPictureTypePredictive, 3},
@@ -276,13 +274,6 @@ sps_rbsp(const VAEncSequenceParameterBufferMPEG2 *seq_param,
     bitstream_put_ui(bs, seq_param->sequence_extension.bits.frame_rate_extension_d, 5);
 
     bitstream_byte_aligning(bs, 0);
-
-    bitstream_put_ui(bs, START_CODE_GOP, 32);
-    bitstream_put_ui(bs, seq_param->gop_header.bits.time_code, 25);
-    bitstream_put_ui(bs, seq_param->gop_header.bits.closed_gop, 1);
-    bitstream_put_ui(bs, seq_param->gop_header.bits.broken_link, 1);
-
-    bitstream_byte_aligning(bs, 0);
 }
 
 static void 
@@ -293,6 +284,15 @@ pps_rbsp(const VAEncSequenceParameterBufferMPEG2 *seq_param,
     int i;
     int chroma_420_type;
 
+    if (pic_param->temporal_reference == 0) {
+        bitstream_put_ui(bs, START_CODE_GOP, 32);
+        bitstream_put_ui(bs, seq_param->gop_header.bits.time_code, 25);
+        bitstream_put_ui(bs, seq_param->gop_header.bits.closed_gop, 1);
+        bitstream_put_ui(bs, seq_param->gop_header.bits.broken_link, 1);
+
+        bitstream_byte_aligning(bs, 0);
+    }
+
     if (seq_param->sequence_extension.bits.chroma_format == CHROMA_FORMAT_420)
         chroma_420_type = pic_param->picture_coding_extension.bits.progressive_frame;
     else
@@ -793,7 +793,12 @@ mpeg2enc_init(struct mpeg2enc_context *ctx)
     ctx->codedbuf_buf_id = VA_INVALID_ID;
     ctx->codedbuf_i_size = ctx->frame_size;
     ctx->codedbuf_pb_size = 0;
-    ctx->intra_period = INTRA_PERIOD;
+
+    if (ctx->mode == 0)
+        ctx->intra_period = 1;
+    else
+        ctx->intra_period = 16;
+
     ctx->bit_rate = -1;
 
     for (i = 0; i < MAX_SLICES; i++) {
@@ -813,27 +818,82 @@ mpeg2enc_init(struct mpeg2enc_context *ctx)
                                               ctx);
 }
 
+static int 
+mpeg2enc_time_code(VAEncSequenceParameterBufferMPEG2 *seq_param,
+                   int num_frames)
+{
+    int fps = (int)(seq_param->frame_rate + 0.5);
+    int time_code = 0;
+    int time_code_pictures, time_code_seconds, time_code_minutes, time_code_hours;
+    int drop_frame_flag = 0;
+
+    assert(fps <= 60);
+
+    time_code_seconds = num_frames / fps;
+    time_code_pictures = num_frames % fps;
+    time_code |= time_code_pictures;
+
+    time_code_minutes = time_code_minutes / 60;
+    time_code_seconds = time_code_minutes % 60;
+    time_code |= (time_code_seconds << 6);
+
+    time_code_hours = time_code_minutes / 60;
+    time_code_minutes = time_code_minutes % 60;
+
+    time_code |= (1 << 12);     /* marker_bit */
+    time_code |= (time_code_minutes << 13);
+
+    time_code_hours = time_code_hours % 24;
+    time_code |= (time_code_hours << 19);
+
+    time_code |= (drop_frame_flag << 24);
+
+    return time_code;
+}
+
 /*
  * run
  */
-static void 
+static void
+mpeg2enc_update_sequence_parameter(struct mpeg2enc_context *ctx,
+                                   VAEncPictureType picture_type,
+                                   int coded_order,
+                                   int display_order)
+{
+    VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
+
+    /* update the GOP info for the new GOP */
+    if (display_order % ctx->intra_period == 0) {
+        seq_param->gop_header.bits.time_code = mpeg2enc_time_code(seq_param, display_order);
+    }
+}
+
+static void
 mpeg2enc_update_picture_parameter(struct mpeg2enc_context *ctx,
-                                 VAEncPictureType picture_type,
-                                 int coded_order,
-                                 int display_order)
+                                  VAEncPictureType picture_type,
+                                  int coded_order,
+                                  int display_order)
 {
-    VAEncPictureParameterBufferMPEG2 *pic_param;
-    VAStatus va_status;
+    VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
 
-    // Picture level
-    pic_param = &ctx->pic_param;
     pic_param->picture_type = picture_type;
-    pic_param->temporal_reference = display_order;
+    pic_param->temporal_reference = display_order % ctx->intra_period;
     pic_param->reconstructed_picture = surface_ids[SID_RECON_PICTURE];
     pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
     pic_param->backward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L1];
-    pic_param->coded_buf = ctx->codedbuf_buf_id;
+}
 
+static void
+mpeg2enc_update_picture_parameter_buffer(struct mpeg2enc_context *ctx,
+                                         VAEncPictureType picture_type,
+                                         int coded_order,
+                                         int display_order)
+{
+    VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
+    VAStatus va_status;
+
+    /* update the coded buffer id */
+    pic_param->coded_buf = ctx->codedbuf_buf_id;
     va_status = vaCreateBuffer(ctx->va_dpy,
                                ctx->context_id,
                                VAEncPictureParameterBufferType,
@@ -903,6 +963,9 @@ begin_picture(struct mpeg2enc_context *ctx,
     ctx->current_input_surface = ctx->current_upload_surface;
     ctx->current_upload_surface = tmp;
 
+    mpeg2enc_update_sequence_parameter(ctx, picture_type, coded_order, display_order);
+    mpeg2enc_update_picture_parameter(ctx, picture_type, coded_order, display_order);
+
     if (coded_order == 0) {
         assert(picture_type == VAEncPictureTypeIntra);
         length_in_bits = build_packed_seq_buffer(&ctx->seq_param, &packed_seq_buffer);
@@ -1163,7 +1226,7 @@ encode_picture(struct mpeg2enc_context *ctx,
         CHECK_VASTATUS(va_status,"vaCreateBuffer");
 
         /* picture parameter set */
-        mpeg2enc_update_picture_parameter(ctx, picture_type, coded_order, display_order);
+        mpeg2enc_update_picture_parameter_buffer(ctx, picture_type, coded_order, display_order);
 
         mpeg2enc_render_picture(ctx);
 
@@ -1210,10 +1273,9 @@ static void
 mpeg2enc_run(struct mpeg2enc_context *ctx)
 {
     int display_order = 0, coded_order = 0;
-    int i_frame_only = 1, i_p_frame_only = 1;
 
     for (display_order = 0; display_order < ctx->num_pictures;) {
-        if (i_frame_only) {
+        if (ctx->mode == 0) {
             encode_picture(ctx,
                            coded_order,
                            display_order,
@@ -1222,7 +1284,8 @@ mpeg2enc_run(struct mpeg2enc_context *ctx)
                            display_order + 1);
             display_order++;
             coded_order++;
-        } else if (i_p_frame_only) {
+        } else if (ctx->mode == 1) {
+            assert(0);
             if ((display_order % ctx->intra_period) == 0) {
                 encode_picture(ctx,
                                coded_order,
@@ -1245,6 +1308,8 @@ mpeg2enc_run(struct mpeg2enc_context *ctx)
         } else { // follow the i,p,b pattern
             static int fcurrent = 0;
             int fnext;
+
+            assert(0);
             
             fcurrent = fcurrent % (sizeof(picture_type_patter)/sizeof(int[2]));
             fnext = (fcurrent+1) % (sizeof(picture_type_patter)/sizeof(int[2]));