static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) { unsigned char *start=NULL; int y,len=-1; while(len<minlen){ AVPacket pkt; int len2=maxlen; double pts; int x=ds_get_packet_pts(sh_audio->ds,&start, &pts); if(x<=0) { start = NULL; x = 0; ds_parse(sh_audio->ds, &start, &x, MP_NOPTS_VALUE, 0); if (x <= 0) break; // error } else { int in_size = x; int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0); sh_audio->ds->buffer_pos -= in_size - consumed; } av_init_packet(&pkt); pkt.data = start; pkt.size = x; if (pts != MP_NOPTS_VALUE) { sh_audio->pts = pts; sh_audio->pts_bytes = 0; } y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt); //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout); // LATM may need many packets to find mux info if (y == AVERROR(EAGAIN)) continue; if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; } if(!sh_audio->parser && y<x) sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!) if(len2>0){ if (((AVCodecContext *)sh_audio->context)->channels >= 5) { int samplesize = av_get_bytes_per_sample(((AVCodecContext *) sh_audio->context)->sample_fmt); reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, ((AVCodecContext *)sh_audio->context)->channels, len2 / samplesize, samplesize); } //len=len2;break; if(len<0) len=len2; else len+=len2; buf+=len2; maxlen -= len2; sh_audio->pts_bytes += len2; } mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2); if (setup_format(sh_audio, sh_audio->context)) break; } return len; }
static int encode_faac(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size) { if (encoder->params.channels >= 5) reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, AF_CHANNEL_LAYOUT_AAC_DEFAULT, encoder->params.channels, len / divisor, divisor); // len is divided by the number of bytes per sample enc_frame_size = faacEncEncode(faac, (int32_t*) src, len / divisor, dest, max_size); return enc_frame_size; }
static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) { unsigned len = sh_audio->channels*sh_audio->samplesize; len = (minlen + len - 1) / len * len; if (len > maxlen) // if someone needs hundreds of channels adjust audio_out_minsize // based on channels in preinit() return -1; len=demux_read_data(sh_audio->ds,buf,len); if (len > 0 && sh_audio->channels >= 5) { reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, sh_audio->channels, len / sh_audio->samplesize, sh_audio->samplesize); } return len; }
static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size) { int n; if ((encoder->params.channels == 6 || encoder->params.channels == 5) && (!strcmp(lavc_acodec->name,"ac3") || !strcmp(lavc_acodec->name,"libfaac"))) { int isac3 = !strcmp(lavc_acodec->name,"ac3"); reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, isac3 ? AF_CHANNEL_LAYOUT_LAVC_DEFAULT : AF_CHANNEL_LAYOUT_AAC_DEFAULT, encoder->params.channels, size / 2, 2); } n = avcodec_encode_audio(lavc_actx, dest, size, src); compressed_frame_size = n; return n; }
int PcmDecoder::decode_audio(sh_audio_t *sh_audio,unsigned char **inbuf,int *inlen,unsigned char* outbuf,int *outlen) { *outlen = 0; if (*inlen > 0 && sh_audio->channels >= 5) { reorder_channel_nch(*inbuf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, sh_audio->channels, *inlen / sh_audio->samplesize, sh_audio->samplesize); } // *outlen = *inlen/sh_audio->samplesize/sh_audio->channels*2; // memcpy(outbuf,*inbuf,*outlen*sh_audio->samplesize); #if 1 if (sh_audio->sample_format == AF_FORMAT_S16_BE){ unsigned char *tbuf = outbuf; int i; for (i = 0; i < *inlen; i+=2){ tbuf[0] = (*inbuf)[i+1]; tbuf[1] = (*inbuf)[i]; tbuf+=2; } }else if(sh_audio->sample_format == AF_FORMAT_S24_LE){ int i,n=*inlen/3; uint8_t *in=*inbuf; for(i=0;i<n;i++){ in++; *outbuf++=*in++; *outbuf++=*in++; } }else{ memcpy(outbuf,*inbuf,*inlen); } #else memcpy(outbuf,*inbuf,*inlen); #endif /*outlen is out sample num*/ *outlen = *inlen/sh_audio->samplesize; *inbuf += *inlen; *inlen = 0; return *outlen; }
static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen, int maxlen) { struct priv *priv = sh_audio->context; AVCodecContext *avctx = priv->avctx; int len = -1; while (len < minlen) { if (!priv->output_left) { if (decode_new_packet(sh_audio) < 0) break; continue; } if (setup_format(sh_audio, avctx)) return len; int size = (minlen - len + priv->unitsize - 1); size -= size % priv->unitsize; size = FFMIN(size, priv->output_left); if (size > maxlen) abort(); memcpy(buf, priv->output, size); priv->output += size; priv->output_left -= size; if (avctx->channels >= 5) { int samplesize = av_get_bytes_per_sample(avctx->sample_fmt); reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, avctx->channels, size / samplesize, samplesize); } if (len < 0) len = size; else len += size; buf += size; maxlen -= size; sh_audio->pts_bytes += size; } return len; }
static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen) { int len = 0; int samples; #ifdef CONFIG_TREMOR ogg_int32_t **pcm; #else float scale; float **pcm; #endif struct ov_struct_st *ov = sh->context; while(len < minlen) { while((samples=vorbis_synthesis_pcmout(&ov->vd,&pcm))<=0){ ogg_packet op; double pts; memset(&op,0,sizeof(op)); //op.b_o_s = op.e_o_s = 0; op.bytes = ds_get_packet_pts(sh->ds,&op.packet, &pts); if(op.bytes<=0) break; if (pts != MP_NOPTS_VALUE) { sh->pts = pts; sh->pts_bytes = 0; } if(vorbis_synthesis(&ov->vb,&op)==0) /* test for success! */ vorbis_synthesis_blockin(&ov->vd,&ov->vb); } if(samples<=0) break; // error/EOF while(samples>0){ int i,j; int clipflag=0; int convsize=(maxlen-len)/(2*ov->vi.channels); // max size! int bout=((samples<convsize)?samples:convsize); if(bout<=0) break; // no buffer space /* convert floats to 16 bit signed ints (host order) and interleave */ #ifdef CONFIG_TREMOR if (ov->rg_scale_int == 64) { for(i=0;i<ov->vi.channels;i++){ ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]); ogg_int16_t *ptr=convbuffer+i; ogg_int32_t *mono=pcm[i]; for(j=0;j<bout;j++){ int val=mono[j]>>9; /* might as well guard against clipping */ if(val>32767){ val=32767; clipflag=1; } if(val<-32768){ val=-32768; clipflag=1; } *ptr=val; ptr+=ov->vi.channels; } } } else #endif /* CONFIG_TREMOR */ { #ifndef CONFIG_TREMOR scale = 32767.f * ov->rg_scale; #endif for(i=0;i<ov->vi.channels;i++){ ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]); ogg_int16_t *ptr=convbuffer+i; #ifdef CONFIG_TREMOR ogg_int32_t *mono=pcm[i]; for(j=0;j<bout;j++){ int val=(mono[j]*ov->rg_scale_int)>>(9+6); #else float *mono=pcm[i]; for(j=0;j<bout;j++){ int val=mono[j]*scale; /* might as well guard against clipping */ if(val>32767){ val=32767; clipflag=1; } if(val<-32768){ val=-32768; clipflag=1; } #endif /* CONFIG_TREMOR */ *ptr=val; ptr+=ov->vi.channels; } } } if(clipflag) mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"Clipping in frame %ld\n",(long)(ov->vd.sequence)); len+=2*ov->vi.channels*bout; sh->pts_bytes += 2*ov->vi.channels*bout; mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"\n[decoded: %d / %d ]\n",bout,samples); samples-=bout; vorbis_synthesis_read(&ov->vd,bout); /* tell libvorbis how many samples we actually consumed */ } //while(samples>0) // if (!samples) break; // why? how? } if (len > 0 && ov->vi.channels >= 5) { reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_VORBIS_DEFAULT, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, ov->vi.channels, len / sh->samplesize, sh->samplesize); } return len; }
// Filter data through filter static af_data_t* play(struct af_instance_s* af, af_data_t* data) { af_ac3enc_t *s = af->setup; af_data_t *c = data; // Current working data af_data_t *l; int len, left, outsize = 0, destsize; char *buf, *src, *dest; int max_output_len; int frame_num = (data->len + s->pending_len) / s->expect_len; if (s->add_iec61937_header) max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num; else max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num; if (af->data->len < max_output_len) { mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, " "old len = %i, new len = %i\n", af->info->name, af->data->len, max_output_len); free(af->data->audio); af->data->audio = malloc(max_output_len); if (!af->data->audio) { mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n"); return NULL; } af->data->len = max_output_len; } l = af->data; // Local data buf = (char *)l->audio; src = (char *)c->audio; left = c->len; while (left > 0) { if (left + s->pending_len < s->expect_len) { memcpy(s->pending_data + s->pending_len, src, left); src += left; s->pending_len += left; left = 0; break; } dest = s->add_iec61937_header ? buf + 8 : buf; destsize = (char *)l->audio + l->len - buf; if (s->pending_len) { int needs = s->expect_len - s->pending_len; if (needs > 0) { memcpy(s->pending_data + s->pending_len, src, needs); src += needs; left -= needs; } if (c->nch >= 5) reorder_channel_nch(s->pending_data, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, c->nch, s->expect_len / 2, 2); len = avcodec_encode_audio(s->lavc_actx, dest, destsize, (void *)s->pending_data); s->pending_len = 0; } else { if (c->nch >= 5) reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, c->nch, s->expect_len / 2, 2); len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src); src += s->expect_len; left -= s->expect_len; } mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n", len, s->pending_len); if (s->add_iec61937_header) { int bsmod = dest[5] & 0x7; AV_WB16(buf, 0xF872); // iec 61937 syncword 1 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2 buf[4] = bsmod; // bsmod buf[5] = 0x01; // data-type ac3 AV_WB16(buf + 6, len << 3); // number of bits in payload memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len); len = AC3_FRAME_SIZE * 2 * 2; } outsize += len; buf += len; } c->audio = l->audio; c->nch = 2; c->bps = 2; c->len = outsize; mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n", outsize, s->pending_len); return c; }