pastebin - collaborative debugging tool
kpaste.net RSS


ffmpeg alsa audio decoding
Posted by Anonymous on Fri 15th Aug 2025 23:23
raw | new post
modification of post by Anonymous (view diff)

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

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