点击(此处)折叠或打开
- static struct muxer* init_muxer(enum CodecID id, const char* name, const char* ext, struct audio_param* in){
- int bufid = buf_length_register(1024);
- if(bufid == -1){
- return NULL;
- }
- struct muxer* muxerp = (struct muxer *) malloc(sizeof(struct muxer));
- if(muxerp == NULL){
- return NULL;
- }
- INIT_LIST_HEAD(&muxerp->head);
- memset(muxerp->line, 0, sizeof(muxerp->line));
- muxerp->cur_buf = NULL;
- muxerp->bufid = bufid;
- muxerp->avctx = NULL;
- AVIOContext* ioctx = NULL;
- AVStream *st = NULL;
- AVFormatContext* fmtctx = NULL;
- avformat_alloc_output_context2(&fmtctx, NULL, name, ext);
- if(fmtctx == NULL){
- goto LABEL;
- }
- st = av_new_stream(fmtctx, 0);
- if(st == NULL){
- goto LABEL;
- }
- st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
- st->codec->codec_id = id;
- st->codec->frame_size = in->spf; // this should be the pcm frame size
- st->codec->sample_rate = in->rate; // this should be the pcm sample rate
- st->codec->bit_rate = in->bps; // this should be output stream's bitrate
- st->codec->channels = in->chans;
- ioctx = avio_alloc_context(&muxerp->line[0], sizeof(muxerp->line), 0,
- (void *) muxerp, NULL, write_packet, NULL);
- if(ioctx == NULL){
- goto LABEL;
- }
- fmtctx->pb = ioctx;
- if(0 != avformat_write_header(fmtctx, NULL)){
- goto LABEL;
- }
- muxerp->avctx = fmtctx;
- return muxerp;
- LABEL:
- if(fmtctx){
- avformat_free_context(fmtctx);
- if(ioctx){
- av_free(ioctx);
- }
- }
- free(muxerp);
- return NULL;
- }
点击(此处)折叠或打开
- st->codec->frame_size = in->spf; // this should be the pcm frame size
- st->codec->sample_rate = in->rate; // this should be the pcm sample rate
由以下代码推导出来:
- if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
- if (!pcr_st->codec->frame_size) {
- av_log(s, AV_LOG_WARNING, "frame size not set\n");
- service->pcr_packet_period =
- pcr_st->codec->sample_rate/(10*512);
- } else {
- service->pcr_packet_period =
- pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
- }
点击(此处)折叠或打开
- st->codec->bit_rate = in->bps; // this should be output stream's bitrate
- 由以下代码推导出来:
- static int get_audio_frame_size(AVCodecContext *enc, int size)
- {
- int frame_size;
- if(enc->codec_id == CODEC_ID_VORBIS)
- return -1;
- if (enc->frame_size <= 1) {
- int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
- if (bits_per_sample) {
- if (enc->channels == 0)
- return -1;
- frame_size = (size << 3) / (bits_per_sample * enc->channels);
- } else {
- /* used for example by ADPCM codecs */
- if (enc->bit_rate == 0)
- return -1;
- frame_size = ((int64_t)size * 8 * enc->sample_rate) / enc->bit_rate;
- }
- } else {
- frame_size = enc->frame_size;
- }
- return frame_size;