[Libva] [PATCH 5/6] test/encode/avcenc: use getopt_long_only() to parse optional command-line options

Xiang, Haihao haihao.xiang at intel.com
Wed May 4 02:54:55 UTC 2016


It's convenient to add new option later

Signed-off-by: Xiang, Haihao <haihao.xiang at intel.com>
Reviewed-By: Sean V Kelley <sean.v.kelley at intel.com>
---
 test/encode/avcenc.c | 92 +++++++++++++++++++++++++++++++++-------------------
 1 file changed, 59 insertions(+), 33 deletions(-)

diff --git a/test/encode/avcenc.c b/test/encode/avcenc.c
index fabd857..fe29cf8 100644
--- a/test/encode/avcenc.c
+++ b/test/encode/avcenc.c
@@ -99,6 +99,13 @@ static  unsigned int MaxFrameNum = (1<<12);
 static  unsigned int Log2MaxFrameNum = 12;
 static  unsigned int Log2MaxPicOrderCntLsb = 8;
 
+static const struct option longopts[] = {
+    {"qp", required_argument, 0, 1},
+    {"fb", required_argument, 0, 2},
+    {"mode", required_argument, 0, 3},
+    { NULL, 0, NULL, 0}
+};
+
 static int
 build_packed_pic_buffer(unsigned char **header_buffer);
 
@@ -1681,7 +1688,7 @@ encode_picture(FILE *yuv_fp, FILE *avc_fp,
 
 static void show_help()
 {
-    printf("Usage: avnenc <width> <height> <input_yuvfile> <output_avcfile> [qp=qpvalue|fb=framebitrate] [mode=0(I frames only)/1(I and P frames)/2(I, P and B frames)\n");
+    printf("Usage: avnenc <width> <height> <input_yuvfile> <output_avcfile> [--qp=qpvalue|--fb=framebitrate] [--mode=0(I frames only)/1(I and P frames)/2(I, P and B frames)\n");
 }
 
 static void avcenc_context_seq_param_init(VAEncSequenceParameterBufferH264 *seq_param,
@@ -1853,8 +1860,7 @@ int main(int argc, char *argv[])
 
     va_init_display_args(&argc, argv);
 
-    //TODO may be we should using option analytics library
-    if(argc != 5 && argc != 6 && argc != 7) {
+    if(argc < 5) {
         show_help();
         return -1;
     }
@@ -1864,43 +1870,63 @@ int main(int argc, char *argv[])
     picture_width_in_mbs = (picture_width + 15) / 16;
     picture_height_in_mbs = (picture_height + 15) / 16;
 
-    if (argc == 6 || argc == 7) {
-        qp_value = -1;
-        sscanf(argv[5], "qp=%d", &qp_value);
-        if ( qp_value == -1 ) {
-            frame_bit_rate = -1;
-            sscanf(argv[5], "fb=%d", &frame_bit_rate);
-            if (  frame_bit_rate == -1 ) {
+    if (argc > 5) {
+        char o;
+
+        optind = 5;
+
+        while ((o = getopt_long_only(argc, argv, "", longopts, NULL)) != -1) {
+            switch (o) {
+            case 1:     // qp
+                frame_bit_rate = -1;
+                qp_value = atoi(optarg);
+
+                if (qp_value > 51)
+                    qp_value = 51;
+
+                if (qp_value < 0)
+                    qp_value = 0;
+
+                break;
+
+            case 2:     // fb
+                qp_value = -1;
+                frame_bit_rate = atoi(optarg);
+
+                if (frame_bit_rate <= 0) {
+                    show_help();
+
+                    return -1;
+                }
+
+                break;
+
+            case 3:     // mode
+                mode_value = atoi(optarg);
+
+                if (mode_value == 0)
+                    ip_period = 0;
+                else if (mode_value == 1)
+                    ip_period = 1;
+                else if (mode_value == 2)
+                    /* Hack mechanism before adding the parameter of B-frame number */
+                    ip_period = 2;
+                else {
+                    printf("mode_value = %d\n", mode_value);
+                    show_help();
+                    return -1;
+                }
+
+                break;
+
+            default:
                 show_help();
                 return -1;
             }
-        } else if (qp_value > 51) {
-            qp_value = 51;
-        } else if (qp_value < 0) {
-            qp_value = 0;
         }
     } else
         qp_value = 28;                          //default const QP mode
 
-    if (argc == 7) {
-        sscanf(argv[6], "mode=%d", &mode_value);
-        if ( mode_value == 0 ) {
-		ip_period = 0;
-        }
-        else if ( mode_value == 1) {
-		ip_period = 1;
-        }
-        else if ( mode_value == 2 ) {
-		/* Hack mechanism before adding the parameter of B-frame number */
-		ip_period = 3;
-        }
-        else {
-                printf("mode_value=%d\n",mode_value);
-                show_help();
-                return -1;
-        }
-    }
-
     yuv_fp = fopen(argv[3],"rb");
     if ( yuv_fp == NULL){
         printf("Can't open input YUV file\n");
-- 
1.9.1



More information about the Libva mailing list