The simplest Audio Player Based on FFMPEG + SDL: Split-decoder and player, ffmpegsdl

Source: Internet
Author: User

The simplest Audio Player Based on FFMPEG + SDL: Split-decoder and player, ffmpegsdl

Two examples of the simplest FFMPEG + SDL-based audio player are described in this document: FFmpeg audio decoder and SDL audio sampling data player. These two parts are two examples split from the audio player. The FFmpeg audio decoder decodes the video data from PCM sampling data, while the SDL audio sampling data player implements the playback of PCM Data to audio devices. In short, the original FFmpeg + SDL audio player is implemented:

Audio Data-> PCM-> audio device

The FFmpeg audio decoder implements:

Audio Data-> PCM

The SDL audio sampling data player provides the following features:

PCM> audio devices
Source code of FFmpeg audio decoder
/*** The Simplest FFmpeg-based Audio Decoder * Simplest FFmpeg Audio Decoder ** leixiao Li Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV technology * Communication University of China/ digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program can, (AAC) is decoded as PCM sample data. * Is the simplest tutorial on FFmpeg audio decoding. * By studying this example, you can understand the FFmpeg decoding process. ** This software decode audio streams (AAC, MP3 ...) to PCM data. * Suitable for beginner of FFmpeg. **/# include <stdio. h> # include <stdlib. h> # include <string. h> # define _ STDC_CONSTANT_MACROS # ifdef _ WIN32 // Windowsextern "C" {# include "libavcodec/avcodec. h "# include" libavformat/avformat. h "# include" libswresample/swresample. h "}; # else // Linux... # ifdef _ cplusplusextern "C" {# endif # include <libavcodec/av Codec. h> # include <libavformat/avformat. h> # include <libswresample/swresample. h> # ifdef _ cplusplus}; # endif # define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48 khz 32bit audioint main (int argc, char * argv []) {AVFormatContext * pFormatCtx; inti, audioStream; AVCodecContext * pCodecCtx; AVCodec * pCodec; AVPacket * packet; using * out_buffer; AVFrame * pFrame; int ret; using len = 0; int got_picture; int index = 0; int64_t in_channel_layout; struct SwrContext * au_convert_ctx; FILE * pFile = fopen ("output. pcm "," wb "); char url [] =" skycity1.mp3 "; av_register_all (); avformat_network_init (); pFormatCtx = avformat_alloc_context (); // Openif (avformat_open_input (& pFormatCtx, url, NULL, NULL )! = 0) {printf ("Couldn't open input stream. \ n "); return-1;} // Retrieve stream informationif (avformat_find_stream_info (pFormatCtx, NULL) <0) {printf (" Couldn't find stream information. \ n "); return-1;} // Dump valid information onto standard errorav_dump_format (pFormatCtx, 0, url, false); // Find the first audio streamaudioStream =-1; for (I = 0; I <pFormatCtx-> nb_streams; I ++) if (pFormatCtx-> streams [I]-> codec-> codec_type = AVMEDIA_TYPE_AUDIO) {audioStream = I; break;} if (audioStream =-1) {printf ("Didn't find a audio stream. \ n "); return-1;} // Get a pointer to the codec context for the audio streampCodecCtx = pFormatCtx-> streams [audioStream]-> codec; // Find the decoder for the audio streampCodec = avcodec_find_decoder (pCodecCtx-> codec_id); if (pCodec = NULL) {printf ("Codec not found. \ n "); return-1;} // Open codecif (avcodec_open2 (pCodecCtx, pCodec, NULL) <0) {printf (" cocould not open codec. \ n "); return-1;} packet = (AVPacket *) av_malloc (sizeof (AVPacket); av_init_packet (packet); // Out Audio Paramuint64_t out_channel_layout = bytes; // nb_samples: AAC-1024 MP3-1152int out_nb_samples = pCodecCtx-> frame_size; AVSampleFormat out_sample_fmt = bytes; int out_sample_rate = 44100; int out_channels = queues (out_channel_layout ); // Out Buffer Sizeint out_buffer_size = bytes (NULL, out_channels, channels, buffers, 1); out_buffer = (uint8_t *) av_malloc (Bytes * 2); pFrame = av_frame_alloc (); // FIX: Some Codec's Context Information is needed = require (pCodecCtx-> channels); // Swrau_convert_ctx = swr_alloc (); au_convert_ctx = convert (au_convert_ctx, out_channel_layout, release, out_sample_rate, percent, pCodecCtx-> sample_fmt, pCodecCtx-> sample_rate, 0, NULL); swr_init (au_convert_ctx); while (av_read_frame (pFormatCtx, packet)> = 0) {if (packet-> stream_index = audioStream) {ret = avcodec_decode_audio4 (pCodecCtx, pFrame, & got_picture, packet); if (ret <0) {printf ("Error in decoding audio frame. \ n "); return-1;} if (got_picture> 0) {swr_convert (au_convert_ctx, & out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **) pFrame-> data, pFrame-> nb_samples); printf ("index: % 5d \ t pts: % lld \ t packet size: % d \ n", index, packet-> pts, packet-> size); // Write PCMfwrite (out_buffer, 1, out_buffer_size, pFile); index ++ ;}} av_free_packet (packet);} swr_free (& au_convert_ctx ); fclose (pFile); av_free (out_buffer); // Close the codecavcodec_close (pCodecCtx); // Close the video fileavformat_close_input (& pFormatCtx); return 0 ;}

After the running result program is run, the following audio files are decoded.
The decoded PCM sample data is saved as a file. You can use Adobe Audition to set the sampling rate and other information to view the PCM content.
 
Source code of the SDL audio sampling data player
/*** Simplest example of SDL2 playing Audio (SDL2 playing PCM) * Simplest Audio Play SDL2 (SDL2 play PCM) ** leixiao Li Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV Technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program uses SDL2 to play PCM audio sample data. SDL is actually an encapsulation of the underlying graph * API (Direct3D, OpenGL). It is obviously easy to use to directly call the underlying * API. ** Function call procedure: ** [initialization] * SDL_Init (): Initialize SDL. * SDL_OpenAudio (): Enable the audio device based on the parameter (stored in SDL_AudioSpec. ** [Loop playback data] * SDL_PauseAudio (): play audio data. * SDL_Delay (): latency: waiting for the playback to complete. ** This software plays PCM raw audio data using SDL2. * SDL is a wrapper of low-level API (DirectSound ). * Use SDL is much easier than directly call these low-level API. ** The process is shown as follows: ** [Init] * SDL_Init (): Init SDL. * SDL_OpenAudio (): Opens the audio device with the desired * parameters (In SDL_AudioSpec ). ** [Loop to play data] * SDL_PauseAudio (): Play Audio. * SDL_Dela Y (): Wait for completetion of playback. */# include <stdio. h> # include <tchar. h> extern "C" {# include "sdl/SDL. h "}; // Buffer: // | ------------- | // chunk ------- pos --- len ----- | static Uint8 * audio_chunk; static Uint32 audio_len; static Uint8 * audio_pos; /* Audio Callback * The audio function callback takes the following parameters: * stream: A pointer to the audio buffer to be filled * len: The l Ength (in bytes) of the audio buffer **/void fill_audio (void * udata, Uint8 * stream, int len) {// SDL 2.0SDL _ memset (stream, 0, len); if (audio_len = 0)/* Only play if we have data left */return; len = (len> audio_len? Audio_len: len);/* Mix as much data as possible */SDL_MixAudio (stream, audio_pos, len, SDL_MIX_MAXVOLUME); audio_pos + = len; audio_len-= len ;} int main (int argc, char * argv []) {// Initif (SDL_Init (SDL_INIT_AUDIO | SDL_INIT_TIMER) {printf ("cocould not initialize SDL-% s \ n ", SDL_GetError (); return-1;} // SDL_AudioSpecSDL_AudioSpec wanted_spec; wanted_spec.freq = 44100; wanted_spec.format = AUDIO_S16SYS; Wanted_spec.channels = 2; wanted_spec.silence = 0; channels = 1024; wanted_spec.callback = fill_audio; if (SDL_OpenAudio (& wanted_spec, NULL) <0) {printf ("can't open audio. \ n "); return-1;} FILE * fp = fopen (" NocturneNo2inEflat_44.1k_s16le.pcm "," rb + "); if (fp = NULL) {printf ("cannot open this file \ n"); return-1 ;}// For YUV420Pint pcm_buffer_size = 4096; char * pcm_buffer = (char *) malloc (pcm_buffer_si Ze); int data_count = 0; while (1) {if (fread (pcm_buffer, 1, pcm_buffer_size, fp )! = Pcm_buffer_size) {// Loopfseek (fp, 0, SEEK_SET); fread (pcm_buffer, 1, pcm_buffer_size, fp); data_count = 0 ;} printf ("Now Playing % 10d Bytes data. \ n ", data_count); data_count + = pcm_buffer_size; // Set audio buffer (PCM data) audio_chunk = (Uint8 *) pcm_buffer; // Audio buffer lengthaudio_len = pcm_buffer_size; audio_pos = audio_chunk; // PlaySDL_PauseAudio (0); while (audio_len> 0) // Wait until finishSDL_Delay (1);} free (pcm_buffer); SDL_Quit (); return 0 ;}

After running the result program, a PCM sample data file in the program folder is read as follows.
Next, the PCM data will be output to the system's audio devices (audios and headphones ).

Download
Simplest FFmpeg Audio Player

SourceForge: https://sourceforge.net/projects/simplestffmpegaudioplayer/

Github: https://github.com/leixiaohua1020/simplest_ffmpeg_audio_player

Open source China: http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_audio_player


This program implements audio decoding and playback. It is the simplest tutorial on FFmpeg audio decoding.
By studying this example, we can understand the decoding process of FFmpeg.
The project contains three projects:
Simplest_ffmpeg_audio_player: audio decoder based on FFmpeg + SDL
Simplest_ffmpeg_audio_decoder: Specifies the audio decoder. Libavcodec and libavformat are used.
Simplest_audio_play_sdl2: An example of using SDL2 to play PCM sample data.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.