sample_enc_avc.cpp
Upload User: wangqun
Upload Date: 2019-10-16
Package Size: 34701k
Code Size: 6k
Development Platform:

Visual C++

  1. /********************************************************************
  2.  Created: 2005/12/24 
  3.  File name: sample_enc_avc.cpp
  4.  Purpose: command-line sample for H.264/AVC encoder
  5.  Copyright (c) 2005-2009 MainConcept GmbH. All rights reserved.
  6.  This software is the confidential and proprietary information of
  7.  MainConcept GmbH and may be used only in accordance with the terms of
  8.  your license from MainConcept GmbH.
  9. *********************************************************************/
  10. #include <stdio.h>
  11. #include "mctypes.h"
  12. #include "mcfourcc.h"
  13. #include "bufstrm.h"
  14. #include "sample_common_args.h"
  15. #include "sample_common_misc.h"
  16. #include "buf_file.h"
  17. #include "enc_avc.h"
  18. #include "enc_avc_def.h"
  19. #include "config_avc.h"
  20. // guess desired video_type
  21. int get_video_type(int width, int height, double frame_rate)
  22. {
  23.     if (width <= 320)
  24.     {
  25.         return H264_CIF;
  26.     }
  27.     else if (width < 1280)
  28.     {
  29.         return H264_MAIN;
  30.     }
  31.     else if (width < 1440)
  32.     {
  33.         return H264_HDTV_720p;
  34.     }
  35.     return H264_BD_HDMV;
  36. }
  37. int main(int argc, char* argv[])
  38. {
  39.     avc_param_set param_set;
  40.     struct h264_v_settings * v_settings = &param_set.params;
  41.     h264_v_encoder * v_encoder = NULL;
  42.     unsigned char * input_video_buffer = NULL;
  43.     bufstream_tt * videobs = NULL;
  44.     char * in_file;
  45.     char * out_file;
  46.     char * cfg_file;
  47.     int32_t width;
  48.     int32_t height;
  49.     int32_t fourcc;
  50.     double frame_rate;
  51.     int32_t bit_rate;
  52.     arg_item_t params[] =
  53.     {
  54.         { IDS_VIDEO_FILE,    1,  &in_file},
  55.         { IDS_OUTPUT_FILE,   1,  &out_file},
  56.         { IDS_CONFIG_FILE,   0,  &cfg_file},
  57.         { IDI_V_WIDTH,       1,  &width},
  58.         { IDI_V_HEIGHT,      1,  &height},
  59.         { IDN_V_FOURCC,      1,  &fourcc},
  60.         { IDD_V_FRAMERATE,   0,  &frame_rate},
  61.         { IDI_BITRATE,       0,  &bit_rate},
  62.     };
  63.     int init_options = 0;
  64.     void * opt_list[10];
  65.     if (parse_args(argc - 1, argv + 1, sizeof(params) / sizeof(params[0]), params) >= 0)
  66.     {
  67.         int line_size, img_start;
  68.         int video_frame_size = get_video_frame_size(width, height, fourcc, &line_size, &img_start);
  69.         int video_type = get_video_type(width, height, frame_rate);
  70.     
  71.         h264OutVideoDefaults(v_settings, video_type, 0);
  72.         if (cfg_file)
  73.         {
  74.             APIEXTFUNC_LOADPARAMSET load_param_func = (APIEXTFUNC_LOADPARAMSET) AVCConfigGetAPIExt(MCAPI_LoadParamSet);
  75.             if (load_param_func(&param_set, cfg_file) != MCAPI_NOERROR)
  76.             {
  77.                 printf("nInvalid config file. Terminating...n");
  78.                 return 0;
  79.             }
  80.         }
  81.         v_settings->def_horizontal_size = width;
  82.         v_settings->def_vertical_size = height;
  83.         v_settings->frame_rate = frame_rate > 0.0 ? frame_rate : v_settings->frame_rate;
  84.         v_settings->bit_rate = bit_rate >= 0 ? bit_rate * 1000 : v_settings->bit_rate;
  85.         if (h264OutVideoChkSettings(get_rc, v_settings, NULL, NULL))
  86.         {
  87.             printf("nInvalid settings, h264OutVideoChkSettings failed. Terminating...n");
  88.             return 0;
  89.         }
  90.         v_encoder = h264OutVideoNew(get_rc, v_settings, 0, 0xFFFFFFFF, 0, 0);
  91.         if(!v_encoder)
  92.         {
  93.             printf("h264OutVideoNew failedn");
  94.             return 0;
  95.         }
  96.         videobs = open_file_buf_write(out_file, 65536, NULL);
  97.         // save original auxinfo handler
  98.         org_auxinfo = videobs->auxinfo;
  99.         // in order to get encoding statistics
  100.         videobs->auxinfo = auxinfo;
  101.         if(h264OutVideoInit(v_encoder, videobs, init_options, &opt_list[0]))
  102.         {
  103.             printf("h264OutVideoInit fails.n");
  104.             return 0;
  105.         }
  106.         input_video_buffer = new unsigned char [video_frame_size];
  107.         FILE * input_video_file = fopen(in_file, "rb");
  108.         int aborted = 0;
  109.         int frame_cnt = 0;
  110.         void * ext_info_stack[16] = {0};
  111.         while (1)
  112.         {
  113.             unsigned int option_flags = 0;
  114.             unsigned int option_cnt = 0;
  115.             void ** ext_info = &ext_info_stack[0];
  116.             if (fread(input_video_buffer, sizeof(unsigned char), video_frame_size, input_video_file) != video_frame_size)
  117.             {
  118.                 // end of the file
  119.                 break;
  120.             }
  121.             if (h264OutVideoPutFrame(v_encoder, input_video_buffer + img_start, line_size, width, height, fourcc, option_flags, ext_info))
  122.             {
  123.                 break;
  124.             }
  125.             frame_cnt++;
  126.             if(has_pressed_key(NULL)){
  127.                 aborted = 1;
  128.                 break;
  129.             }
  130.         }
  131.         if (v_encoder)
  132.         {
  133.             h264OutVideoDone(v_encoder, aborted);
  134.             h264OutVideoFree(v_encoder);
  135.         }
  136.         if (videobs)
  137.         {
  138.             videobs->done(videobs, aborted);
  139.             videobs->free(videobs);
  140.         }
  141.         if (input_video_buffer)
  142.             delete [] input_video_buffer;
  143.         if (input_video_file)
  144.             fclose(input_video_file);
  145.     }
  146.     else
  147.     {
  148.         printf("n==== MainConcept AVC encoder sample ====n"
  149.             "Usage:nsample_enc_avc -yv12 -w 720 -h 576 -f 25 -b 2500 -p -perf 9 -v video.yuv -o video.h264n"
  150.             "Supported input colour spaces are:nYV12   (-yv12)nIYUV   (-iyuv)nUYVY   (-uyvy)nYUYV   (-yuyv)nYUY2   (-yuy2)n"
  151.             "RGB24  (-rgb24 or -bgr3)nRGB32  (-rgb32 or -bgr4)nARGB32 (-argb32 or -bgra)nn"
  152.             "Options:n-w    picture widthn-h    picture heightn-f    frame raten-b    target bit rate (kbps)n"
  153.             );
  154.     }
  155.     return 0;
  156. }