#if 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "SDL2/SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endif
//#define debug_msg(fmt, args...) printf("--->[%s,%d] " fmt "\n\n", __FUNCTION__, __LINE__, ##args)
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
//48000 * (32/8)
unsigned int audioLen = 0;
unsigned char *audioChunk = NULL;
unsigned char *audioPos = NULL;
void fill_audio(void * udata, Uint8 * stream, int len)
{
SDL_memset(stream, 0, len);
if (audioLen == 0)
return;
len = (len>audioLen?audioLen:len);
SDL_MixAudio(stream,audioPos,len,SDL_MIX_MAXVOLUME);
audioPos += len;
audioLen -= len;
}
int test_audio_2_play()
{
AVFormatContext *pFortCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVPacket *pPkt = NULL;
AVFrame*pFrame = NULL;
struct SwrContext *pSwrCtx = NULL;
SDL_AudioSpec wantSpec;
//FILE* outFile = fopen("output.pcm", "wb");
char inFile[] = "skycity1.mp3";
int ret = -1;
int audioIndex = -1;
int i = 0;
int got_picture = -1;
uint64_t out_chn_layout = AV_CH_LAYOUT_STEREO; //通道布局 输出双声道
enum AVSampleFormat out_sample_fmt=AV_SAMPLE_FMT_S16; //声音格式
int out_sample_rate=44100; //采样率
int out_nb_samples = -1;
int out_channels = -1; //通道数
int out_buffer_size = -1; //输出buff
unsigned char *outBuff = NULL;
uint64_t in_chn_layout = -1; //通道布局
struct SwrContext *au_convert_ctx;
av_register_all();
pFortCtx = avformat_alloc_context();
if (avformat_open_input(&pFortCtx, inFile, NULL, NULL) != 0) //open input file and read data into buf
{
printf("avformat_open_input error!\n");
ret = -1;
goto ERR_1;
}
if (avformat_find_stream_info(pFortCtx, NULL) < 0) //find stream some info
{
printf("avformat_find_stream_info error!\n");
ret = -1;
goto ERR_1;
}
/* find audio index */
for (i = 0; i < pFortCtx->nb_streams; i++)
{
if (pFortCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioIndex = i;
break;
}
}
if (-1 == audioIndex)
{
printf("can not find audio index!\n");
ret = -1;
goto ERR_1;
}
printf("------>audioIndex is %d\n", audioIndex);
pCodecCtx = pFortCtx->streams[audioIndex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (NULL == pCodec)
{
printf("can not find decoder!\n");
ret = -1;
goto ERR_1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
ret = -1;
goto ERR_1;
}
if (NULL == (pPkt = (AVPacket *)av_malloc(sizeof(AVPacket))))
{
printf("AV malloc failure.\n");
ret = -1;
goto ERR_2;
}
//out parameter
out_nb_samples = pCodecCtx->frame_size;
out_channels = av_get_channel_layout_nb_channels(out_chn_layout);
out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);
outBuff = (unsigned char *)av_malloc(MAX_AUDIO_FRAME_SIZE*2); //双声道
printf("-------->out_buffer_size is %d\n",out_buffer_size);
in_chn_layout = av_get_default_channel_layout(pCodecCtx->channels);
pFrame = av_frame_alloc();
//SDL
wantSpec.freq = out_sample_rate;
wantSpec.format = AUDIO_S16SYS;
wantSpec.channels = out_channels;
wantSpec.silence = 0;
wantSpec.samples = out_nb_samples;
wantSpec.callback = fill_audio;
wantSpec.userdata = pCodecCtx;
if (SDL_OpenAudio(&wantSpec, NULL) < 0)
{
printf("can not open SDL!\n");
ret = -1;
goto ERR_3;
}
//Swr
au_convert_ctx=swr_alloc_set_opts(NULL,
out_chn_layout, /*out*/
out_sample_fmt, /*out*/
out_sample_rate, /*out*/
in_chn_layout, /*in*/
pCodecCtx->sample_fmt , /*in*/
pCodecCtx->sample_rate, /*in*/
0,
NULL);
swr_init(au_convert_ctx);
SDL_PauseAudio(0);
while(av_read_frame(pFortCtx, pPkt) >= 0)
{
if (pPkt->stream_index == audioIndex)
{
if (avcodec_decode_audio4(pCodecCtx, pFrame, &got_picture, pPkt) < 0)
{
printf("Error in decoding audio frame.\n");
ret = -1;
break;
}
if (got_picture > 0)
{
swr_convert(au_convert_ctx,&outBuff, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);
//fwrite(outBuff, 1, out_buffer_size, outFile);
while(audioLen > 0)
SDL_Delay(1);
audioChunk = (unsigned char *)outBuff;
audioPos = audioChunk;
audioLen = out_buffer_size;
}
}
av_free_packet(pPkt);
}
SDL_CloseAudio();
SDL_Quit();
swr_free(&au_convert_ctx);
ERR_3:
av_free(outBuff);
ERR_2:
avcodec_close(pCodecCtx);
avformat_close_input(&pFortCtx);
ERR_1:
avformat_free_context(pFortCtx);
//fclose(outFile);
return ret;
}
int main(int argc, char *argv[])
{
//test_audio_2_PCM();
test_audio_2_play();
return 0;
}
#endif
#if 1
//采用队列的方式
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "SDL2/SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endif
//#define debug_msg(fmt, args...) printf("--->[%s,%d] " fmt "\n\n", __FUNCTION__, __LINE__, ##args)
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
//48000 * (32/8)
#define SDL_AUDIO_BUFFER_SIZE 1024
struct SwrContext *au_convert_ctx;
int out_buffer_size = -1; //输出buff
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
PacketQueue audioq;
void packet_queue_init(PacketQueue *q) {
memset(q, 0, sizeof(PacketQueue));
q->first_pkt = NULL;
q->last_pkt = NULL;
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
int packet_queue_put(PacketQueue *q, AVPacket *pkt) {
AVPacketList *pkt1;
if(av_dup_packet(pkt) < 0) {
return -1;
}
pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
SDL_LockMutex(q->mutex);
if (!q->last_pkt) //队列为空
q->first_pkt = pkt1;
else
q->last_pkt->next = pkt1; //将当前链表的最后一个结点的next指向pkt1
q->last_pkt = pkt1; //将last_pkt指向最后一个结点,即pkt1
q->nb_packets++;
q->size += pkt1->pkt.size;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return 0;
}
int quit = 0;
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) {
AVPacketList *pkt1;
int ret;
SDL_LockMutex(q->mutex);
for(;;)
{
if(quit)
{
ret = -1;
break;
}
pkt1 = q->first_pkt; //指向第一个结点,即取出第一个结点
if (pkt1)
{
q->first_pkt = pkt1->next; //即q->first_pkt = q->first_pkt->next 将第一个结点指向的下一个结点设置为first_pkt,可以理解为取出当前第一个结点
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-
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于FFMPEG+SDL2播放音频 (231个子文件)
WavinFlag.aac 3.37MB
compile_cl.bat 392B
ffmpeg_sdl_audio_player.cpp 15KB
avcodec-55.dll 18.06MB
avformat-55.dll 5.1MB
avfilter-4.dll 1.94MB
avdevice-55.dll 1.28MB
SDL2.dll 984KB
swscale-2.dll 424KB
avutil-52.dll 409KB
swresample-0.dll 270KB
ffmpeg_sdl_audio_player.exe 36KB
simplest_ffmpeg_audio_player.vcxproj.filters 961B
.gitignore 67B
SDL_opengl.h 622KB
avcodec.h 171KB
SDL_opengles2.h 129KB
avformat.h 97KB
avfilter.h 56KB
SDL_haptic.h 38KB
opt.h 34KB
SDL_video.h 33KB
SDL_render.h 32KB
pixfmt.h 28KB
SDL_events.h 26KB
frame.h 22KB
SDL_audio.h 20KB
SDL_hints.h 19KB
SDL_surface.h 18KB
avio.h 18KB
intreadwrite.h 18KB
avdevice.h 16KB
SDL_pixels.h 15KB
SDL_scancode.h 15KB
SDL_keycode.h 14KB
old_pix_fmts.h 14KB
mem.h 14KB
SDL_stdinc.h 14KB
common.h 14KB
pixdesc.h 13KB
SDL_test_fuzzer.h 13KB
avstring.h 12KB
swscale.h 12KB
swresample.h 12KB
SDL_assert.h 10KB
old_codec_ids.h 10KB
SDL_gamecontroller.h 10KB
SDL_thread.h 10KB
samplefmt.h 10KB
log.h 10KB
buffer.h 10KB
channel_layout.h 9KB
SDL_atomic.h 9KB
avutil.h 8KB
SDL_joystick.h 8KB
_mingw.h 8KB
imgutils.h 8KB
bprint.h 8KB
buffersink.h 7KB
SDL_mouse.h 7KB
parseutils.h 7KB
SDL_rwops.h 7KB
SDL_syswm.h 7KB
SDL_system.h 7KB
SDL_mutex.h 7KB
dict.h 6KB
SDL_keyboard.h 6KB
vdpau.h 6KB
SDL_log.h 6KB
xvmc.h 6KB
SDL_endian.h 6KB
stdint.h 6KB
inttypes.h 6KB
SDL_shape.h 6KB
version.h 5KB
SDL_config.h 5KB
SDL.h 5KB
timecode.h 5KB
eval.h 5KB
SDL_filesystem.h 5KB
mathematics.h 5KB
SDL_version.h 5KB
SDL_test_common.h 5KB
buffersrc.h 5KB
error.h 5KB
cpu.h 5KB
fifo.h 5KB
SDL_test_md5.h 5KB
SDL_messagebox.h 5KB
version.h 4KB
SDL_main.h 4KB
SDL_platform.h 4KB
audio_fifo.h 4KB
SDL_test_harness.h 4KB
attributes.h 4KB
rational.h 4KB
begin_code.h 4KB
SDL_rect.h 4KB
vda.h 4KB
SDL_cpuinfo.h 4KB
共 231 条
- 1
- 2
- 3
资源评论
Stoneshen1211
- 粉丝: 312
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功