AVCodecContext->frame_size 和 AVFrame->nb_samples 之间音频样本大小的差异

Difference in audio sample size between AVCodecContext->frame_size and AVFrame->nb_samples

我正在玩 ffmpeg 来理解音频数据,但我看到音频数据之间存在差异,AVCodecContext->frame_size 显示它是 1152,但我从 AVFrame->nb_samples 获得的值显示它为 47。两个数据字段描述的是同一件事,即每个通道的音频帧中没有样本,那么为什么会有差异。作为参考,我粘贴了 AVFrame 和 AVCodecContext 对象,它们很大,但它也会给你任何你想要的信息

AVCodecContext

{av_class = 0x7ffff74e24c0, log_level_offset = 0, codec_type = AVMEDIA_TYPE_AUDIO, codec = 0x7ffff751c600, codec_id = AV_CODEC_ID_MP3, codec_tag = 0, priv_data = 0x5555555ac040, 
  internal = 0x5555555a0e40, opaque = 0x0, bit_rate = 96000, bit_rate_tolerance = 0, global_quality = 0, compression_level = -1, flags = 0, flags2 = 0, extradata = 0x0, extradata_size = 0, time_base = {
    num = 1, den = 32000}, ticks_per_frame = 1, delay = 0, width = 0, height = 0, coded_width = 0, coded_height = 0, gop_size = 0, pix_fmt = AV_PIX_FMT_NONE, draw_horiz_band = 0x0, get_format = 
    0x7ffff69ae940 <avcodec_default_get_format>, max_b_frames = 0, b_quant_factor = 0, b_frame_strategy = 0, b_quant_offset = 0, has_b_frames = 0, mpeg_quant = 0, i_quant_factor = 0, 
  i_quant_offset = 0, lumi_masking = 0, temporal_cplx_masking = 0, spatial_cplx_masking = 0, p_masking = 0, dark_masking = 0, slice_count = 0, prediction_method = 0, slice_offset = 0x0, 
  sample_aspect_ratio = {num = 0, den = 1}, me_cmp = 0, me_sub_cmp = 0, mb_cmp = 0, ildct_cmp = 0, dia_size = 0, last_predictor_count = 0, pre_me = 0, me_pre_cmp = 0, pre_dia_size = 0, 
  me_subpel_quality = 0, me_range = 0, slice_flags = 0, mb_decision = 0, intra_matrix = 0x0, inter_matrix = 0x0, scenechange_threshold = 0, noise_reduction = 0, intra_dc_precision = 0, skip_top = 0, 
  skip_bottom = 0, mb_lmin = 0, mb_lmax = 0, me_penalty_compensation = 0, bidir_refine = 0, brd_scale = 0, keyint_min = 0, refs = 0, chromaoffset = 0, mv0_threshold = 0, b_sensitivity = 0, 
  color_primaries = AVCOL_PRI_RESERVED0, color_trc = AVCOL_TRC_RESERVED0, colorspace = AVCOL_SPC_RGB, color_range = AVCOL_RANGE_UNSPECIFIED, chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED, 
  slices = 0, field_order = AV_FIELD_UNKNOWN, sample_rate = 32000, channels = 2, sample_fmt = AV_SAMPLE_FMT_FLTP, frame_size = 1152, frame_number = 1, block_align = 0, cutoff = 0, channel_layout = 3, 
  request_channel_layout = 0, audio_service_type = AV_AUDIO_SERVICE_TYPE_MAIN, request_sample_fmt = AV_SAMPLE_FMT_NONE, get_buffer2 = 0x7ffff69af1d0 <avcodec_default_get_buffer2>, 
  refcounted_frames = 0, qcompress = 0, qblur = 0, qmin = 0, qmax = 0, max_qdiff = 0, rc_buffer_size = 0, rc_override_count = 0, rc_override = 0x0, rc_max_rate = 0, rc_min_rate = 0, 
  rc_max_available_vbv_use = 0, rc_min_vbv_overflow_use = 0, rc_initial_buffer_occupancy = 0, coder_type = 0, context_model = 0, frame_skip_threshold = 0, frame_skip_factor = 0, frame_skip_exp = 0, 
  frame_skip_cmp = 0, trellis = 0, min_prediction_order = -1, max_prediction_order = -1, timecode_frame_start = 0, rtp_callback = 0x0, rtp_payload_size = 0, mv_bits = 0, header_bits = 0, 
  i_tex_bits = 0, p_tex_bits = 0, i_count = 0, p_count = 0, skip_count = 0, misc_bits = 0, frame_bits = 0, stats_out = 0x0, stats_in = 0x0, workaround_bugs = 0, strict_std_compliance = 0, 
  error_concealment = 0, debug = 0, err_recognition = 0, reordered_opaque = -9223372036854775808, hwaccel = 0x0, hwaccel_context = 0x0, error = {0, 0, 0, 0, 0, 0, 0, 0}, dct_algo = 0, idct_algo = 0, 
  bits_per_coded_sample = 0, bits_per_raw_sample = 0, lowres = 0, coded_frame = 0x0, thread_count = 1, thread_type = 3, active_thread_type = 0, thread_safe_callbacks = 0, execute = 
    0x7ffff6e7bc30 <avcodec_default_execute>, execute2 = 0x7ffff6e7bd00 <avcodec_default_execute2>, nsse_weight = 0, profile = -99, level = -99, skip_loop_filter = AVDISCARD_DEFAULT, 
  skip_idct = AVDISCARD_DEFAULT, skip_frame = AVDISCARD_DEFAULT, subtitle_header = 0x0, subtitle_header_size = 0, vbv_delay = 0, side_data_only_packets = 1, initial_padding = 0, framerate = {num = 0, 
    den = 1}, sw_pix_fmt = AV_PIX_FMT_NONE, pkt_timebase = {num = 0, den = 1}, codec_descriptor = 0x7ffff74fc910, pts_correction_num_faulty_pts = 0, pts_correction_num_faulty_dts = 0, 
  pts_correction_last_pts = 0, pts_correction_last_dts = 0, sub_charenc = 0x0, sub_charenc_mode = 0, skip_alpha = 0, seek_preroll = 0, debug_mv = 0, chroma_intra_matrix = 0x0, dump_separator = 0x0, 
  codec_whitelist = 0x0, properties = 0, coded_side_data = 0x0, nb_coded_side_data = 0, hw_frames_ctx = 0x0, sub_text_format = 0, trailing_padding = 0, max_pixels = 2147483647, hw_device_ctx = 0x0, 
  hwaccel_flags = 0, apply_cropping = 0, extra_hw_frames = 0, discard_damaged_percentage = 0}

AVFrame

{data = {
    0x5555555c2b00 "3c;03[=13=]1?16[\n1623102H8tBs11о9Q[7140̹ҍ51\a0504v512[=13=]27:50a1ڱҹle.9r720L4200060ñ@9H", 
    0x5555555b5e00 "Q\r_055[=13=]6154S161511321.V20*2c134x944[=13=]31*+61!z61xw3732ĸh24060\t135ǹ15075'\f951\t162\b8/\"105", 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, linesize = {4608, 0, 0, 0, 0, 0, 0, 0}, extended_data = 0x5555555a2640, width = 0, height = 0, 
  nb_samples = 47, format = 8, key_frame = 1, pict_type = AV_PICTURE_TYPE_NONE, sample_aspect_ratio = {num = 0, den = 1}, pts = 0, pkt_pts = 0, pkt_dts = 0, coded_picture_number = 0, 
  display_picture_number = 0, quality = 0, opaque = 0x0, error = {0, 0, 0, 0, 0, 0, 0, 0}, repeat_pict = 0, interlaced_frame = 0, top_field_first = 0, palette_has_changed = 0, 
  reordered_opaque = -9223372036854775808, sample_rate = 32000, channel_layout = 3, buf = {0x5555555a3d80, 0x55555559fe40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, extended_buf = 0x0, nb_extended_buf = 0, 
  side_data = 0x0, nb_side_data = 0, flags = 0, color_range = AVCOL_RANGE_UNSPECIFIED, color_primaries = AVCOL_PRI_RESERVED0, color_trc = AVCOL_TRC_RESERVED0, colorspace = AVCOL_SPC_RGB, 
  chroma_location = AVCHROMA_LOC_UNSPECIFIED, best_effort_timestamp = 0, pkt_pos = 5412, pkt_duration = 508032, metadata = 0x0, decode_error_flags = 0, channels = 2, pkt_size = 432, qscale_table = 0x0, 
  qstride = 0, qscale_type = 0, qp_table_buf = 0x0, hw_frames_ctx = 0x0, opaque_ref = 0x0, crop_top = 0, crop_bottom = 0, crop_left = 0, crop_right = 0, private_ref = 0x0}

这就是我解码音频帧的方式

while(av_read_frame(avc,avpacket) == 0){
        if(avpacket->stream_index == audioStream){
            int res = avcodec_send_packet(avctx,avpacket);
            if(res == AVERROR(EAGAIN)){
                continue;
            }
            else if(res<0){
                std::cout<<"error reading packet\n";
                break;
            }
            else{
                AVFrame* avframe = av_frame_alloc();
                res = avcodec_receive_frame(avctx,avframe);
                if(res == AVERROR(EAGAIN)){
                    continue;
                }
                else if(res<0){
                    std::cout<<"Error reading frame";
                    break;
                }
                
                else{
                    audio_buffer.push(*avframe);
                }
                av_frame_free(&avframe);
            }
        }
        av_packet_unref(avpacket);
    }

对于 MP3,前 1105 个样本是解码器延迟,从 1152 个样本中返回 47 个样本。参见