编码音频aac的时候 (avcodec_encode_audio2)提示FFMPEG more samples than frame size 的错误

原因:

音频编码器AVCodecContext (编码器的能力)的frame_size 比编入帧AVFrame的nb_samples小 。

解决方法:

通过调试可以得到aac编码器的frame_size 为1024 所以每次应该给编码器1024个采样。可以采样AVAudioFifo缓存确保每次编入aac的数据为编码器的frame_size(1024)。


代码如下:

                                      while (( ret=av_audio_fifo_size(fifo)) < output_frame_size) 
					{
						if (init_converted_samples(&converted_input_samples, ac,
							oAFrame->nb_samples))
						{

						}

						if (convert_samples((const uint8_t**)oAFrame->extended_data, converted_input_samples,
							oAFrame->nb_samples, resample_context))
						{

						}

						add_samples_to_fifo(fifo, converted_input_samples,oAFrame->nb_samples);

						if (converted_input_samples) 
						{
							av_freep(&converted_input_samples[0]);
							free(converted_input_samples);
						}

					}

					while(av_audio_fifo_size(fifo) >= output_frame_size)
					{
						AVFrame *frame;

						frame = av_frame_alloc();

						const int frame_size = FFMIN(av_audio_fifo_size(fifo),ac->frame_size);

						(frame)->nb_samples     = frame_size;
						(frame)->channel_layout = ac->channel_layout;
						(frame)->format         = ac->sample_fmt;
						(frame)->sample_rate    = ac->sample_rate;

						int error;

						if ((error = av_frame_get_buffer(frame, 0)) < 0) 
						{

							av_frame_free(&frame);

							return error;
						}

						if (av_audio_fifo_read(fifo, (void **)frame->data, frame_size) < frame_size) 
						{
							av_frame_free(&frame);

							return AVERROR_EXIT;
						}

						AVPacket pkt;

						av_init_packet(&pkt);
						pkt.data = NULL;
						pkt.size = 0;

						//fflush(stdout);
						frame->pts = av_frame_get_best_effort_timestamp(frame);

						frame->pict_type=AV_PICTURE_TYPE_NONE;

						if ((error = avcodec_encode_audio2(ac, &pkt,frame, &data_present)) < 0) 
						{
							av_free_packet(&pkt);
							return error;
						}

						av_frame_free(&frame);

						if (data_present) 
						{
							pkt.stream_index = audio_st->index;  

							pkt.dts = av_rescale_q_rnd(pkt.dts,
								oc->streams[audioindex]->codec->time_base,
								oc->streams[audioindex]->time_base,
								(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
							pkt.pts = av_rescale_q_rnd(pkt.pts,
								oc->streams[audioindex]->codec->time_base,
								oc->streams[audioindex]->time_base,
								(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
							pkt.duration = av_rescale_q(pkt.duration,
								oc->streams[audioindex]->codec->time_base,
								oc->streams[audioindex]->time_base);

							if ((error = av_interleaved_write_frame(oc, &pkt)) < 0)
							{
								av_free_packet(&pkt);

								return error;
							}

						}//end if(data_present)

						av_free_packet(&pkt);

						}


output_frame_size 即为AVCodecContext 的frame_size



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零凌灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值