pastebin - collaborative debugging tool
kpaste.net RSS


ffmpeg alsa audio decoding
Posted by Anonymous on Fri 15th Aug 2025 23:22
raw | new post
view followups (newest first): ffmpeg alsa audio decoding by Anonymous

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libavcodec/avcodec.h>
  4. #include <libavformat/avformat.h>
  5. #include <libavutil/avutil.h>
  6. #include <alsa/asoundlib.h>
  7.  
  8. int
  9. main(int argc, char *argv[])
  10. {
  11.         if (argc < 2) {
  12.                 fprintf(stderr, "argc\n");
  13.                 return 1;
  14.         }
  15.  
  16.         printf("[INFO] %s\n", argv[1]);
  17.  
  18.         snd_pcm_t *handle = NULL;
  19.         if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
  20.                 goto end_playback;
  21.  
  22.         struct AVFormatContext *formatContext = NULL;
  23.         if (avformat_open_input(&formatContext, argv[1], NULL, NULL) < 0)
  24.                 goto end_playback;
  25.  
  26.         if (avformat_find_stream_info(formatContext, NULL) < 0)
  27.                 goto end_playback;
  28.  
  29.         int streamId;
  30.         if ((streamId = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) < 0)
  31.                 goto end_playback;
  32.  
  33.         const struct AVCodec *codec = avcodec_find_decoder(formatContext->streams[streamId]->codecpar->codec_id);
  34.         if (!codec)
  35.                 goto end_playback;
  36.  
  37.         struct AVCodecContext *codecContext = avcodec_alloc_context3(codec);
  38.         if (!codecContext)
  39.                 goto end_playback;
  40.  
  41.         if (avcodec_parameters_to_context(codecContext, formatContext->streams[streamId]->codecpar))
  42.                 goto end_playback;
  43.  
  44.         if (avcodec_open2(codecContext, codec, NULL) < 0)
  45.                 goto end_playback;
  46.  
  47.         if (snd_pcm_set_params(handle,
  48.                         SND_PCM_FORMAT_S16_LE,
  49.                         SND_PCM_ACCESS_RW_INTERLEAVED,
  50.                         formatContext->streams[streamId]->codecpar->ch_layout.nb_channels,
  51.                         codecContext->sample_rate,
  52.                         1, 500000) < 0)
  53.                 goto end_playback;
  54.  
  55.         AVPacket *packet = av_packet_alloc();
  56.         AVFrame *frame = av_frame_alloc();
  57.         if (!packet || !frame)
  58.                 goto end_playback;
  59.  
  60.         while (av_read_frame(formatContext, packet) >= 0)
  61.         {
  62.                 if (packet->stream_index != streamId)
  63.                         continue;
  64.                 if (avcodec_send_packet(codecContext, packet) != 0) {
  65.                         av_packet_unref(packet);
  66.                         break;
  67.                 }
  68.  
  69.                 while (!avcodec_receive_frame(codecContext, frame))
  70.                 {
  71.                         uint8_t *audio_data = frame->data[0];
  72.                         snd_pcm_sframes_t frames = frame->nb_samples;
  73.                         while (frames > 0)
  74.                         {
  75.                                 snd_pcm_sframes_t written = snd_pcm_writei(handle, audio_data, frames);
  76.                                 if (written < 0) {
  77.                                         if (written == -EAGAIN)
  78.                                                 continue;
  79.  
  80.                                         goto end_playback;
  81.                                 } else {
  82.                                         frames -= written;
  83.                                         audio_data += written * av_get_bytes_per_sample(frame->format) * formatContext->streams[streamId]->codecpar->ch_layout.nb_channels;
  84.                                 }
  85.                                 if (written < 0 && written != -EAGAIN)
  86.                                         goto end_playback;
  87.                         }
  88.                         av_frame_unref(frame);
  89.                 }
  90.  
  91.                 av_packet_unref(packet);
  92.         }
  93.  
  94. end_playback:
  95.         printf("END PLAYBACK\n");
  96.         if (packet)
  97.                 av_packet_free(&packet);
  98.         if (frame)
  99.                 av_frame_free(&frame);
  100.         if (codecContext)
  101.                 avcodec_free_context(&codecContext);
  102.         if (formatContext)
  103.                 avformat_close_input(&formatContext);
  104.         if (handle)
  105.                 snd_pcm_close(handle);
  106.  
  107.  
  108.         return 0;
  109. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at