📄 mp3_dec.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mp3decode.h"
#include "mp3locals.h"
typedef struct
{
int buf_size ;
BUFFER_HANDLE r_h ;
int start ;
Mp3DecodeCtrl_t *p_mp3 ;
char * remain;
int remlen;
} Mp3DecodeStru_t, *pMp3DecodeStru_t ;
#define INPUT_BUFFER_MIN_SIZE (1*1024)
Mp3DecodeHandle mp3_decode_open(BUFFER_HANDLE handle,int buf_size)
{
Mp3DecodeStru_t *p = NULL ;
Mp3DecodeCtrl_t *p_mp3 = NULL ;
Mp3DecodeHandle h = 0 ;
int err = 0 ;
if (handle != 0)
{
p = (Mp3DecodeStru_t *)malloc(sizeof(Mp3DecodeStru_t)) ;
p_mp3 = (Mp3DecodeCtrl_t *)malloc(sizeof(Mp3DecodeCtrl_t)) ;
if (!(p && p_mp3))
{
err = 1 ;
goto done ;
}
memset(p, 0x00, sizeof(Mp3DecodeStru_t)) ;
memset(p_mp3, 0x00, sizeof(Mp3DecodeCtrl_t)) ;
p_mp3->r_h = handle ;
if (Mp3DecodeStart(p_mp3))
{
err = 2 ;
goto done ;
}
p->buf_size = buf_size ;
p->r_h = handle ;
p->start = 1 ;
p->p_mp3 = p_mp3 ;
p->p_mp3->r_h = handle ;
h = (Mp3DecodeHandle) p ;
p->remain = (char*)malloc(10240);
p->remlen = 0;
}
done:
printf("\n error:%d \n", err) ;
if (err)
{
if (p) free(p) ;
if (p_mp3) free(p_mp3) ;
h = 0;
}
return (h) ;
}
int mp3_decode_set(Mp3DecodeHandle handle, Mp3Option_t *p_mp3_option)
{
int result = -1 ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if (p
&& p_mp3_option
)
{
if (p->p_mp3)
{
/* if pMp3Option is not mallo */
if (p->p_mp3->pMp3Option == NULL)
p->p_mp3->pMp3Option = (Mp3Option_t *)malloc(sizeof(Mp3Option_t)) ;
if (p->p_mp3->pMp3Option)
{
memcpy(p->p_mp3->pMp3Option, p_mp3_option, sizeof(Mp3Option_t)) ;
result = 0 ;
}
}
}
return (result) ;
}
int mp3_decode_get(Mp3DecodeHandle handle, Mp3Info_t *p_mp3_info)
{
int result = -1 ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if (p
&& p_mp3_info
)
{
if (p->p_mp3)
{
if (p->p_mp3->pMp3Info)
{
memcpy(p_mp3_info, p->p_mp3->pMp3Info, sizeof(Mp3Info_t)) ;
result = 0 ;
}
}
}
return (result) ;
}
int mp3_decode_write(Mp3DecodeHandle handle, char *data, int size)
{
int result = -1 ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if (p && p->r_h
&& data
&& size > 0)
{
result = WriteFeedBufferSync(p->r_h, (char *)data, 1, size);
/*result = RingBuf_Write(p->r_h, (unsigned char *)data, 1, size) ;*/
}
return (result) ;
}
int mp3_decode_read_frame(Mp3DecodeHandle handle, char *buf, int buf_size, int *out_len)
{
int result = -1 ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if (p
&& p->p_mp3
&& p->start
)
{
p->p_mp3->outbuf = (unsigned char *)buf ;
p->p_mp3->outbuf_len = buf_size ;
result = Mp3DecodeFrame(p->p_mp3) ;
if (out_len)
*out_len = p->p_mp3->output_len ;
}
return (result) ;
}
int mp3_decode_read2(Mp3DecodeHandle handle, char *buf, int buf_size)
{
int i,bsize,rlen = 0;
int o_l ;
int r = -1 ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if(buf_size <= 0 || !buf)
return -1;
if(p->remlen > 0)
{
bsize = (buf_size > p->remlen)? p->remlen : buf_size;
memcpy(buf,p->remain,bsize);
p->remlen -= bsize;
if(p->remlen > 0)
{
memmove(p->remain,&(p->remain[bsize]),p->remlen);
}
if(bsize == buf_size)
return bsize;
p->remlen = 0;
rlen = bsize;
}
i = 0;
while (1)
{
o_l = 0 ;
r = mp3_decode_read_frame(handle, p->remain, 10240, &o_l) ;
if (r == MP3DEC_OK
|| r == MP3DEC_ERR_CANRECOV)
{
if (o_l > 0)
{
p->remlen = o_l ;
bsize = ((buf_size-rlen) > p->remlen)? p->remlen : (buf_size-rlen);
memcpy(&buf[rlen],p->remain,bsize);
p->remlen -= bsize;
rlen += bsize;
if(p->remlen > 0)
{
memmove(p->remain,&(p->remain[bsize]),p->remlen);
}
if(rlen >= buf_size)
return rlen;
p->remlen = 0;
}
}
else
{
fprintf(stderr,"\nmp3_decode_read2>>>r=%08x,o_l=%d",r,o_l);
break ;
}
}
return rlen;
}
int mp3_decode_read(Mp3DecodeHandle handle, char *buf, int buf_size, int frames, int *out_len, int *out_frames)
{
int result = -1 ;
int r = -1 ;
int i ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if (p
&& p->start
&& buf
&& buf_size > 0
&& frames > 0
)
{
char *p_s = buf ;
int b_s = buf_size ;
int o_l ;
int o_f = 0 ;
for (i=0; i<frames; i++)
{
o_l = 0 ;
r = mp3_decode_read_frame(handle, p_s, b_s, &o_l) ;
if (r == MP3DEC_OK
|| r == MP3DEC_ERR_CANRECOV
)
{
if (o_l > 0)
{
p_s += o_l ;
b_s -= o_l ;
o_f++ ;
}
}
else
{
break ;
}
}
if (out_len)
*out_len = (buf_size - b_s) ;
if (out_frames)
*out_frames = o_f ;
if ((buf_size - b_s) > 0)
result = 0 ;
else
result = r ;
}
return (result) ;
}
int mp3_decode_close(Mp3DecodeHandle handle)
{
int result = -1 ;
Mp3DecodeStru_t *p = (Mp3DecodeStru_t *)handle ;
if (p)
{
if (p->p_mp3)
{
Mp3DecodeEnd(p->p_mp3) ;
if (p->p_mp3->pMp3Option)
free(p->p_mp3->pMp3Option) ;
if (p->p_mp3->pMp3Info)
free(p->p_mp3->pMp3Info) ;
free(p->p_mp3) ;
}
p->r_h = 0;
/*if (p->r_h != 0)
RingBuf_Destroy(p->r_h) ;
*/
free(p) ;
result = 0 ;
}
return (result) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -