SDL音频播放

gcc -o tutorial03 tutorial03.c -lavutil -lavformat -lavcodec -lz -lm \
`sdl-config --cflags --libs`

AUDIO名词解释:
samples:采样,通过PCM来采样,通常样本16bit,PCM的采样精度从14-bit发展到16-bit、18-bit、20-bit直到24-bit
Samples rate:采样率,22.05KHz and 44.1KHz,每秒从连续信号中提取并组成离散信号的采样个数
位速:采样率*样本bit*通道数,CD上未经压缩的音频位速是1411.2 kbit/s(16 位/采样点 × 44100 采样点/秒 × 2 通道)

pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO , 在for循环找找到audiostream的索引i
AVCodecContext *aCodecCtx=pFormatCtx->streams[audioStream]->codec; 得到音频编码的信息

SDL_AudioSpec  wanted_spec,spec;
wanted_spec.freq = aCodecCtx->sample_rate;   //采样率
wanted_spec.format = AUDIO_S16SYS;
//告诉SDL使用什么格式,S指代signed,16为样本16bit,SYS指代大小端由系统决定
wanted_spec.channels = aCodecCtx->channels; //有多少个通道
wanted_spec.silence = 0; //silence值,由于为signed,故为0
wanted_spec.samples =1024; //缓存大小
wanted_spec.callback = audio_callback; //音频的回调函数

wanted_spec.userdata = aCodecCtx; //给回调函数处理的数据
SDL_OpenAudio(&wanted_spec, &spec)
返回-1则打开失败,spec为NULL则以wanted_spec指定的方式播放,若spec不为NULL,则使用根据硬件改变的spec指定的方式播放,而wanted_spec可以删除

VCodec         *aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
avcodec_open(aCodecCtx, aCodec);
找到解码器,并进行解码

typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;        //为包的总数
int size;    //为所有包的大小
SDL_mutex *mutex; //互斥锁
SDL_cond *cond; //条件变量
} PacketQueue;
我们自己创建的用于构建Packet队列的数据结构

AVPacketList
A simple linked list for packets.
AVPacket pkt
AVPacketList * next

void packet_queue_init(PacketQueue *q)
{

memset(q,0,sizeof(PacketQueue));
    q->mutex = SDL_CreateMutex();
    q->cond=SDL_CreateCond();
}

对PacketQueue数据结构进行初始化

用于给PacketQueue数据结构中填入包的函数
int    packet_queue_put(PacketQueue    *q,AVPacket    *pkt)
{
    AVPacketList    *pkt1;
    if(av_dup_packet(pkt)<0)
    {
        return    -1;    //检查是否为NULL,为NULL则自己填充,否则返回-1
    }
    pkt1    =    av_malloc(sizeof(AVPacketList));//给AVPacketList分配空间


if (!pkt1)
        return -1;
    pkt1->pkt = *pkt;
    pkt1->next = NULL;
    SDL_LockMutex(q->mutex); //对PacketQueue进行操作,先锁定互斥变量
    if (!q->last_pkt)
        q->first_pkt = pkt1;
    else
        q->last_pkt->next = pkt1;
    q->last_pkt = pkt1;
    q->nb_packets++;
    q->size += pkt1->pkt.size;
    SDL_CondSignal(q->cond); //发送条件信号,方便等待数据的地方唤醒
    SDL_UnlockMutex(q->mutex); //解锁
    return 0;
}

接收数据
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
{
    AVPacketList    *pkt1;
    int    ret;
    SDL_LockMutex(q->mutex);//锁定mutex
    for(;;)
    {
        if(quit)
        {
            ret    =-1;
            break;
        }
        pkt1=q->first_pkt;
        if (pkt1) {
            q->first_pkt = pkt1->next;
            if (!q->first_pkt)
                q->last_pkt = NULL;
            q->nb_packets--;
            q->size -= pkt1->pkt.size;
            *pkt = pkt1->pkt;
            av_free(pkt1);
            ret = 1;
            break;
        }
        else if (!block) {
            ret = 0;
            break;
        }
        else {
            SDL_CondWait(q->cond, q->mutex);
        }
    }
    SDL_UnlockMutex(q->mutex);
    return ret;
}
SDL_CondWait先检测是否满足条件,若不满足,解锁mutex,wait,直至被SDL_CondSignal()函数或者SDL_CondBroadcast()函数通知,则锁定mutex并返回

void SDL_PauseAudio(int pause_on)
控制播放与暂停,当pause_on为0时,播放数据,若数据未准备,则播放静音

播放的回调函数,格式必须为void callback(void *userdata, Uint8 *stream, int len),这里的userdata就是我们给到SDL的指针,stream是我们要把声音数据写入的缓冲区指针,len是缓冲区的大小。
void audio_callback(void *userdata, Uint8 *stream, int len) {

struct    mydata    *data=(struct    mydata*)userdata;
AVCodecContext *aCodecCtx = (AVCodecContext *)data->pFormatCtx;
PacketQueue *audioq=data->audioq;
int len1, audio_size;
//静态的数据为了可以多次调用回调函数,而每次不一定处理完了数据
static uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
//audio_buf的大小为1.5倍的声音帧的大小以便于有一个比较好的缓冲
static unsigned int audio_buf_size = 0;
static unsigned int audio_buf_index = 0;

while(len > 0) {
    if(audio_buf_index >= audio_buf_size) {
     
      audio_size = audio_decode_frame(aCodecCtx, audio_buf,
                                      sizeof(audio_buf));
      if(audio_size < 0) {
   
    audio_buf_size = 1024;
    memset(audio_buf, 0, audio_buf_size);
      } else {
    audio_buf_size = audio_size;
      }
      audio_buf_index = 0;
    }
    len1 = audio_buf_size - audio_buf_index;
    if(len1 > len)
      len1 = len;
    memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);
    len -= len1;
    stream += len1;
    audio_buf_index += len1;
}
}

对音频数据进行解码,被解码的数据存在audio_buf中,buf_size告诉函数audio_buf缓冲区多大,返回值为解码的数据数量,结束时返回-1,否则返回被解码的bytes数。
int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf,
                       int buf_size,PacketQueue *audioq) {

static AVPacket pkt;
static uint8_t *audio_pkt_data = NULL;
static int audio_pkt_size = 0;
int len1, data_size;
for(;;) {
    while(audio_pkt_size > 0) {    //该循环从末尾开始
      data_size = buf_size;    //avcodec_decode_audio2必须先指定输出缓冲大小
      len1 = avcodec_decode_audio2(aCodecCtx, (int16_t *)audio_buf, &data_size, audio_pkt_data, audio_pkt_size);

//对数据进行解码
      if(len1 < 0) {
   
    audio_pkt_size = 0;
    break;
      }
      audio_pkt_data += len1;
      audio_pkt_size -= len1; //静态数据,可能包里面有多个帧,故下次调用继续
      if(data_size <= 0) {
   
    continue;
      }
     
      return data_size;
    }
    if(pkt.data)
      av_free_packet(&pkt);
    if(quit) {
      return -1;
    }
    if(packet_queue_get(&audioq, &pkt, 1) < 0) {
      return -1;
    }
    audio_pkt_data = pkt.data;
    audio_pkt_size = pkt.size;
}
}

int avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples, int *frame_size_ptr, uint8_t *buf, int buf_size)
输出为samples,如果没有可以解码的frame_size_ptr返回0,否则其为被解码的大小。你必须分配frame_size_ptr为samples的缓存大小在你调用该函数时。
返回负数为错误,返回解码的字节数或0指示没有被解码的。
The input buffer must be FF_INPUT_BUFFER_PADDING_SIZE larger than the actual read bytes because some optimized bitstream readers read 32 or 64 bits at once and could read over the end. The end of the input buffer buf should be set to 0 to ensure that no overreading happens for damaged MPEG streams.
Note:You might have to align the input buffer buf and output buffer samples. The alignment requirements depend on the CPU: on some CPUs it isn‘t necessary at all, on others it won‘t work at all if not aligned and on others it will work but it will have an impact on performance. In practice, the bitstream should have 4 byte alignment at minimum and all sample data should be 16 byte aligned unless the CPU doesn‘t need it (AltiVec and SSE do). If the linesize is not a multiple of 16 then there‘s no sense in aligning the start of the buffer to 16.

时间: 2024-08-28 15:27:02

SDL音频播放的相关文章

FFMPEG + SDL音频播放分析

目录 [hide] 1 抽象流程: 2 关键实现: 2.1 main()函数 2.2 decode_thread()读取文件信息和音频包 2.3 stream_component_open():设置音频参数和打开设备 2.4 audio_callback(): 回调函数,向SDL缓冲区填充数据 2.5 audio_decode_frame():解码音频 3 FFMPEG结构体 3.1 channel_layout_map 4 FFMPEG宏定义 4.1 Audio channel conveni

最简单的基于FFMPEG+SDL的音频播放器:拆分-解码器和播放器

本文补充记录<最简单的基于FFMPEG+SDL的音频播放器>中的两个例子:FFmpeg音频解码器和SDL音频采样数据播放器.这两个部分是从音频播放器中拆分出来的两个例子.FFmpeg音频解码器实现了视频数据到PCM采样数据的解码,而SDL音频采样数据播放器实现了PCM数据到音频设备的播放.简而言之,原先的FFmpeg+SDL音频播放器实现了: 音频数据->PCM->音频设备 FFmpeg音频解码器实现了: 音频数据->PCM SDL音频采样数据播放器实现了: PCM->

最简单的基于FFMPEG+SDL的音频播放器 ver2 (采用SDL2.0)

简介 之前做过一个简单的音频播放器:<最简单的基于FFMPEG+SDL的音频播放器>,采用的是SDL1.2.前两天刚把原先做的<最简单的基于FFMPEG+SDL的视频播放器>更新采用了SDL2.0,于是顺手也把音频播放器更新成为SDL2.0. SourceForge项目主页:https://sourceforge.net/projects/simplestffmpegaudioplayer/ 完整工程下载地址:http://download.csdn.net/detail/leix

最简单的视音频播放示例9:SDL2播放PCM

本文记录SDL播放音频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API用于播放视频:封装了DirectSound这类的API用于播放音频.因为SDL的编写目的就是简化视音频播放的开发难度,所以使用SDL播放视频(YUV/RGB)和音频(PCM)数据非常的容易. SDL简介 SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,

最简单的视音频播放示例1:总述

前言 最近研究了一下Windows平台下的视音频播放的技术.在Windows平台下的视频播放技术主要有以下三种:GDI,Direct3D和OpenGL:音频播放技术主要是DirectSound.这些技术属于比较底层的技术,因此使用起来相对来说还是比较复杂的.我在学习的过程中也发现这一领域一直缺少比较简单直观的示例程序,因此打算做些示例程序,同时写一些相关的文章,方便这一领域的人学习相关的知识. 打算重点记录一下视频播放的技术.之前的研究一直集中于视频的编解码方面的技术,而很少接触到视频播放显示方

最简单的视音频播放演示样例7:SDL2播放RGB/YUV

===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频播放演示样例2:GDI播放YUV, RGB 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface) 最简单的视音频播放演示样例4:Direct3D播放RGB(通过Texture) 最简单的视音频播放演示样例5:OpenGL播放RGB/YUV 最简单的视音频播放演示样例

算法系列之二十三:离散傅立叶变换之音频播放与频谱显示

算法系列之二十三:离散傅立叶变换之音频播放与频谱显示 算法系列之二十三离散傅立叶变换之音频播放与频谱显示 导语 什么是频谱 1 频谱的原理 2 频谱的选择 3 频谱的计算 显示动态频谱 1 实现方法 2 杂项说明 结果展示 导语 频谱和均衡器,几乎是媒体播放程序的必备物件,没有这两个功能的媒体播放程序会被认为不够专业,现在主流的播放器都具备这两个功能,foobar 2000的十八段均衡器就曾经让很多人着迷.在上一篇对离散傅立叶变换介绍的基础上,本篇就进一步介绍一下频谱是怎么回事儿,下一篇继续介绍

Android通过意图使用内置的音频播放器

如果实现一个音频文件的播放,那么在应用程序中提供播放音频文件功能的最简单的方式是利用内置的"Music(音乐)"应用程序的功能--即使用系统自带的或已安装好的音乐播放器来播放指定的音频文件. 本例比较简单,下面直接给出源代码: 布局文件activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http:/

IOS 音频播放

iOS音频播放 (一):概述 Audio Playback in iOS (Part 1) : Introduction 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改我也因此对于iOS下的音频播放实现有了一定的研究.写这个系列的博客目的一方面希望能够抛砖引玉,另一方面也是希望能帮助国内其他的iOS开发者和爱好者少走弯路(我自己就遇到了不少的坑=.=). 本篇为<iOS音频播放>系列的第一篇,主要将对iOS下实现音频播放的方法进行概述. 基础 先来简单了解一