[Libav-user] Decode MP4 video file and save audio frame into raw PCM file it just noise

Chun Yu Suen sunzhenyucn at gmail.com
Wed Dec 14 08:17:14 EET 2022


Hi, experts,

I try to save raw PCM file with audio frame when decoding some MP4 file,
when it has done, i try to play saved raw PCM file it just noise.

These are my codes for save audio frame data to pcm buffer:
    case LFW_PACKET_AUDIO:
      int offset = 0;
      int i = 0;
      int ch = 0;
      // FYI: env->a_sample_size = av_get_bytes_per_sample()
      cur_sample_frame_size = env->frame_buf->nb_samples *
            env->a_sample_size *
            env->a_codec_ctx->channels;

      // LFW_AUDIO_PCM_BUFFER_SIZE = 128 * 1024
      env->pcm_buf_size = LFW_AUDIO_PCM_BUFFER_SIZE;
      unsigned char *buff = av_mallocz(env->pcm_buf_size);

      if (env->pcm_buf_size < cur_sample_frame_size) {
        new_pcm_buf_size = round_up(cur_sample_frame_size, 4);
        env->pcm_buf_size = new_pcm_buf_size;
        av_free(buff);
        buff = av_mallocz(env->pcm_buf_size);
      }

      for (i = 0; i < env->frame_buf->nb_samples; i++) {
        for (ch = 0; ch < env->a_codec_ctx->channels; ch++) {
            memcpy(buff + offset, env->frame_buf->data[ch] +
env->a_sample_size * i, env->a_sample_size);
            offset += env->a_sample_size;
        }
      }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20221214/cd8d45ab/attachment.htm>


More information about the Libav-user mailing list