牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

CELT编码解码

转载自:https://gist.github.com/haxar/2402477


解码:

 
#include 
<stdio.h>
#include 
<stdlib.h>
#include 
<stdint.h>
#include 
<string.h>
#include 
<unistd.h>
#include 
<errno.h>
 
#include 
"celt.h"
 
int
main(
int argc, char **argv) {
    
char data[2048= { };
    
short pcm[2048= { };
    
int rate        = 48000;
    
int framesize   = 960;
    
int channels    = 1;
    
int tmp;
    
int rem;
    
void *mode;
    
void *state;
    uint16_t len;
 
    
while ((tmp = getopt(argc, argv, "r:f:c:")) != -1{
        
switch (tmp) {
          
case 'r':
            rate 
= atoi(optarg);
            
break;
          
case 'f':
            framesize 
= atoi(optarg);
            
break;
          
case 'c':
            channels 
= atoi(optarg);
            
break;
        }

    }

    
if (!(mode = celt_mode_create(rate, framesize, &tmp))) {
        fprintf(stderr, 
"error: celt_mode_create: %s\n", celt_strerror(tmp));
        
return 1;
    }

    
if (!(state = celt_decoder_create_custom(mode, channels, &tmp))) {
        fprintf(stderr, 
"error: celt_decoder_create_custom: %s\n", celt_strerror(tmp));
        celt_mode_destroy(mode);
        
return 1;
    }

 
    
for (len = 0;;) {
        
if (!len) {
            
if (read(STDIN_FILENO, &len, sizeof(len)) != sizeof(len)) {
                
break;
            }

            
if (len > sizeof(data)) {
                fprintf(stderr, 
"error: celt packet larger than buffer\n");
                celt_decoder_destroy(state);
                celt_mode_destroy(mode);
                
return 1;
            }

            rem 
= len;
        }

        
if ((tmp = read(STDIN_FILENO, data + (len - rem), rem)) < 0{
            fprintf(stderr, 
"error: read: %s\n", strerror(errno));
            celt_decoder_destroy(state);
            celt_mode_destroy(mode);
            
return 1;
        }

        
if (!tmp) {
            
break;
        }

        
if (tmp != rem) {
            rem 
-= tmp;
            
continue;
        }

        
if ((tmp = celt_decode(state, (void *)data, len, pcm, framesize)) < 0{
            fprintf(stderr, 
"error: celt_decode: %s\n", celt_strerror(tmp));
            celt_decoder_destroy(state);
            celt_mode_destroy(mode);
            
return 1;
        }

        len 
= 0;
        
if (write(STDOUT_FILENO, pcm, sizeof(*pcm) * framesize * channels) < 0{
            fprintf(stderr, 
"error: write: %s\n", strerror(errno));
            celt_decoder_destroy(state);
            celt_mode_destroy(mode);
            
return 1;
        }

    }

 
    celt_decoder_destroy(state);
    celt_mode_destroy(mode);
 
    
return 0;
}



编码:
#include <stdio.h>
#include 
<stdlib.h>
#include 
<stdint.h>
#include 
<string.h>
#include 
<unistd.h>
#include 
<errno.h>
 
#include 
"celt.h"
 
int
main(
int argc, char **argv) {
    
short pcm[2048= { };
    
char data[2048= { };
    
int rate        = 48000;
    
int framesize   = 960;
    
int channels    = 1;
    
int bitrate     = 0;
    
int variable    = 0;
    
int prediction  = 2;
    
int complexity  = 10;
    
int tmp;
    
int rem;
    
void *mode;
    
void *state;
    uint16_t len;
 
    
while ((tmp = getopt(argc, argv, "r:f:c:b:vp:x:")) != -1{
        
switch (tmp) {
          
case 'r':
            rate 
= atoi(optarg);
            
break;
          
case 'f':
            framesize 
= atoi(optarg);
            
break;
          
case 'c':
            channels 
= atoi(optarg);
            
break;
          
case 'b':
            bitrate 
= atoi(optarg);
            
break;
          
case 'v':
            variable 
= 1;
            
break;
          
case 'p':
            prediction 
= atoi(optarg);
            
break;
          
case 'x':
            complexity 
= atoi(optarg);
            
break;
        }

    }

    
if (!(mode = celt_mode_create(rate, framesize, &tmp))) {
        fprintf(stderr, 
"error: celt_mode_create: %s\n", celt_strerror(tmp));
        
return 1;
    }

    
if (!(state = celt_encoder_create_custom(mode, channels, &tmp))) {
        fprintf(stderr, 
"error: celt_encoder_create_custom: %s\n", celt_strerror(tmp));
        celt_mode_destroy(mode);
        
return 1;
    }

    
if (bitrate && celt_encoder_ctl(state, CELT_SET_BITRATE(bitrate)) != CELT_OK) {
        fprintf(stderr, 
"error: celt_encoder_ctl: CELT_SET_BITRATE: bitrate request failed\n");
        celt_encoder_destroy(state);
        celt_mode_destroy(mode);
        
return 1;
    }

    
if (variable && celt_encoder_ctl(state, CELT_SET_VBR(variable)) != CELT_OK) {
        fprintf(stderr, 
"error: celt_encoder_ctl: CELT_SET_VBR: vbr request failed\n");
        celt_encoder_destroy(state);
        celt_mode_destroy(mode);
        
return 1;
    }

    
if (celt_encoder_ctl(state, CELT_SET_PREDICTION(prediction)) != CELT_OK) {
        fprintf(stderr, 
"error: celt_encoder_ctl: CELT_SET_PREDICTION: prediction request failed\n");
        celt_encoder_destroy(state);
        celt_mode_destroy(mode);
        
return 1;
    }

    
if (celt_encoder_ctl(state, CELT_SET_COMPLEXITY(complexity)) != CELT_OK) {
        fprintf(stderr, 
"error: celt_encoder_ctl: CELT_SET_COMPLEXITY: complexity 0 through 10 is only supported\n");
        celt_encoder_destroy(state);
        celt_mode_destroy(mode);
        
return 1;
    }

 
    
for (len = 0;;) {
        
if (!len) {
            len 
= sizeof(*pcm) * framesize * channels;
            
if (len > sizeof(pcm)) {
                fprintf(stderr, 
"error: pcm frame larger than buffer\n");
                celt_encoder_destroy(state);
                celt_mode_destroy(mode);
                
return 1;
            }

            rem 
= len;
        }

        
if ((tmp = read(STDIN_FILENO, (void *)pcm + (len - rem), rem)) < 0{
            fprintf(stderr, 
"error: read: %s\n", strerror(errno));
            celt_encoder_destroy(state);
            celt_mode_destroy(mode);
            
return 1;
        }

        
if (!tmp) {
            
break;
        }

        
if (tmp != rem) {
            rem 
-= tmp;
            
continue;
        }

        
if ((tmp = celt_encode(state, pcm, framesize, (void *)data, sizeof(data))) < 0{
            fprintf(stderr, 
"error: celt_encode: %s\n", celt_strerror(tmp));
            celt_encoder_destroy(state);
            celt_mode_destroy(mode);
            
return 1;
        }

        len 
= tmp;
        
if (write(STDOUT_FILENO, &len, sizeof(len)) < 0 || write(STDOUT_FILENO, data, len) < 0{
            fprintf(stderr, 
"error: write: %s\n", strerror(errno));
            celt_encoder_destroy(state);
            celt_mode_destroy(mode);
            
return 1;
        }

        len 
= 0;
    }

 
    celt_encoder_destroy(state);
    celt_mode_destroy(mode);
 
    
return 0;
}


posted on 2013-02-10 13:09 杨粼波 阅读(1752) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理