⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 x264.c

📁 H.264 source codes
💻 C
📖 第 1 页 / 共 4 页
字号:
#else            fprintf( stderr, "not compiled with AVIS input support\n" );            return -1;#endif        }        if( p_open_infile( psz_filename, &opt->hin, param ) )        {            fprintf( stderr, "could not open input file '%s'\n", psz_filename );            return -1;        }    }    return 0;}/***************************************************************************** * Decode: *****************************************************************************/#if 0static int  Decode( x264_param_t  *param, FILE *fh26l, hnd_t hout ){    fprintf( stderr, "decompressor not working (help is welcome)\n" );    return -1;    x264_nal_t nal;    int i_data;    int b_eof;    //param.cpu = 0;    if( ( h = x264_decoder_open( &param ) ) == NULL )    {        fprintf( stderr, "x264_decoder_open failed\n" );        return -1;    }    i_start = x264_mdate();    b_eof = 0;    i_frame = 0;    i_data  = 0;    nal.p_payload = malloc( DATA_MAX );    while( !b_ctrl_c )    {        uint8_t *p, *p_next, *end;        int i_size;        /* fill buffer */        if( i_data < DATA_MAX && !b_eof )        {            int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );            if( i_read <= 0 )            {                b_eof = 1;            }            else            {                i_data += i_read;            }        }        if( i_data < 3 )        {            break;        }        end = &data[i_data];        /* extract one nal */        p = &data[0];        while( p < end - 3 )        {            if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )            {                break;            }            p++;        }        if( p >= end - 3 )        {            fprintf( stderr, "garbage (i_data = %d)\n", i_data );            i_data = 0;            continue;        }        p_next = p + 3;        while( p_next < end - 3 )        {            if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )            {                break;            }            p_next++;        }        if( p_next == end - 3 && i_data < DATA_MAX )        {            p_next = end;        }        /* decode this nal */        i_size = p_next - p - 3;        if( i_size <= 0 )        {            if( b_eof )            {                break;            }            fprintf( stderr, "nal too large (FIXME) ?\n" );            i_data = 0;            continue;        }        x264_nal_decode( &nal, p +3, i_size );        /* decode the content of the nal */        x264_decoder_decode( h, &pic, &nal );        if( pic != NULL )        {            int i;            i_frame++;            for( i = 0; i < pic->i_plane;i++ )            {                int i_line;                int i_div;                i_div = i==0 ? 1 : 2;                for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )                {                    fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, hout );                }            }        }        memmove( &data[0], p_next, end - p_next );        i_data -= p_next - &data[0];    }    i_end = x264_mdate();    free( nal.p_payload );    fprintf( stderr, "\n" );    x264_decoder_close( h );    fclose( fh26l );    if( hout != stdout )    {        fclose( hout );    }    if( i_frame > 0 )    {        double fps = (double)i_frame * (double)1000000 /                     (double)( i_end - i_start );        fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );    }}#endifstatic int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic ){    x264_picture_t pic_out;    x264_nal_t *nal;    int i_nal, i;    int i_file = 0;    /* Do not force any parameters */    if( pic )    {        pic->i_type = X264_TYPE_AUTO;        pic->i_qpplus1 = 0;    }    if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )    {        fprintf( stderr, "x264_encoder_encode failed\n" );    }    for( i = 0; i < i_nal; i++ )    {        int i_size;        int i_data;        i_data = DATA_MAX;        if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )        {            i_file += p_write_nalu( hout, data, i_size );        }        else if( i_size < 0 )        {            fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );        }    }    if (i_nal)        p_set_eop( hout, &pic_out );    return i_file;}/***************************************************************************** * Encode: *****************************************************************************/static int  Encode( x264_param_t *param, cli_opt_t *opt ){    x264_t *h;    x264_picture_t pic;    int     i_frame, i_frame_total;    int64_t i_start, i_end;    int64_t i_file;    int     i_frame_size;    int     i_progress;    i_frame_total = p_get_frame_total( opt->hin, param->i_width, param->i_height );    if( ( h = x264_encoder_open( param ) ) == NULL )    {        fprintf( stderr, "x264_encoder_open failed\n" );        p_close_infile( opt->hin );        p_close_outfile( opt->hout );        return -1;    }    if( p_set_outfile_param( opt->hout, param ) )    {        fprintf( stderr, "can't set outfile param\n" );        p_close_infile( opt->hin );        p_close_outfile( opt->hout );        return -1;    }    /* Create a new pic */    x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );    i_start = x264_mdate();    /* Encode frames */    i_frame_total -= opt->i_seek;    if( opt->i_maxframes > 0 && opt->i_maxframes < i_frame_total )        i_frame_total = opt->i_maxframes;    for( i_frame = 0, i_file = 0, i_progress = 0;         b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )    {        if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek, param->i_width, param->i_height ) )            break;        pic.i_pts = i_frame * param->i_fps_den;        i_file += Encode_frame( h, opt->hout, &pic );        i_frame++;        /* update status line (up to 1000 times per input file) */        if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&             i_frame * 1000 / i_frame_total > i_progress )        {            int64_t i_elapsed = x264_mdate() - i_start;            double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;            i_progress = i_frame * 1000 / i_frame_total;            fprintf( stderr, "encoded frames: %d/%d (%.1f%%), %.2f fps   \r", i_frame,                     i_frame_total, (float)i_progress / 10, fps );            fflush( stderr ); // needed in windows        }    }    /* Flush delayed B-frames */    do {        i_file +=        i_frame_size = Encode_frame( h, opt->hout, NULL );    } while( i_frame_size );    i_end = x264_mdate();    x264_picture_clean( &pic );    x264_encoder_close( h );    fprintf( stderr, "\n" );    if( b_ctrl_c )        fprintf( stderr, "aborted at input frame %d\n", opt->i_seek + i_frame );    p_close_infile( opt->hin );    p_close_outfile( opt->hout );    if( i_frame > 0 )    {        double fps = (double)i_frame * (double)1000000 /                     (double)( i_end - i_start );        fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,                 (double) i_file * 8 * param->i_fps_num / ( param->i_fps_den * i_frame * 1000 ) );    }    return 0;}/* raw 420 yuv file operation */static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ){    if ((*p_handle = fopen(psz_filename, "rb")) == NULL)        return -1;    return 0;}static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height ){    FILE *f = (FILE *)handle;    int i_frame_total = 0;    if( !fseek( f, 0, SEEK_END ) )    {        int64_t i_size = ftell( f );        fseek( f, 0, SEEK_SET );        i_frame_total = (int)(i_size / ( i_width * i_height * 3 / 2 ));    }    return i_frame_total;}static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height ){    static int prev_frame = -1;    FILE *f = (FILE *)handle;    if( i_frame != prev_frame+1 )        if( fseek( f, i_frame * i_width * i_height * 3 / 2, SEEK_SET ) )            return -1;    if( fread( p_pic->img.plane[0], 1, i_width * i_height, f ) <= 0            || fread( p_pic->img.plane[1], 1, i_width * i_height / 4, f ) <= 0            || fread( p_pic->img.plane[2], 1, i_width * i_height / 4, f ) <= 0 )        return -1;    prev_frame = i_frame;    return 0;}static int close_file_yuv(hnd_t handle){    if (handle == NULL)        return 0;    return fclose((FILE *)handle);}/* avs/avi input file support under cygwin */#ifdef AVIS_INPUTstatic int gcd(int a, int b){    int c;    while (1)    {        c = a % b;        if (!c)            return b;        a = b;        b = c;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -