ffmpeg[学习(四)](代码实现) 实现音频数据解码并且用SDL播放

news/2024/6/3 16:52:09 标签: ffmpeg, 学习, 音视频, 视频编解码

0、作者杂谈

CSDN大多数都是落后的,要么是到处复制粘贴的,对于初学者我来说困惑了很久,大多数CSDN文章都是使用旧的API ,已经被否决了,于是我读一些官方文档,和一些开源项目音视频的输出过程,写出这篇文章希望能帮助到入门音视频的人。
感觉这个专栏没多少人看呃,哎~

一、流程导图

其实与视频解码播放流程差不了太多,前面部分和专栏(一)一样
ffmpeg学习(一)
后面的话是添加了回调函数用于声卡通过回调函数拉数据到声卡缓冲区
在这里插入图片描述

二、实现过程

在这里插入图片描述
这中间省略了很多步骤 其实和ffmpeg学习(三)类似

SDL参数

在这里插入图片描述

转码参数和一开始的参数

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/2d471a44ad4f45d99eac2af9ae05b400.pn
这里新API中将AVChannelLayout分离出来了,我们需要自己创建一个AVChannelLayout来获得声道布局为后面转码参数做铺垫

转码器

在这里插入图片描述

数据转换格式

在这里插入图片描述
这里SDL_Delay主要是防止声音播放过快。

回调函数

在这里插入图片描述

播放过程

在这里插入图片描述
😔 这里播放的是瓦罗兰特的die for you 可惜你们听不到 😄 希望这篇文章对读者有收获!

源代码

#include<iostream>
#include "vp_test.h"
 static uint8_t* audio_buf = new uint8_t[4096];
 static int audio_size;

void read_audio_data(void* userdata, Uint8* stream,int len)
{
	if (audio_size == 0)
		return;
	int audio_buf_index = 0;
	int len1 = 0; 
	while (len > 0){
	len1 = audio_size - audio_buf_index;
	if (len1 > len)
		len1 = len;
	memcpy(stream, audio_buf+audio_buf_index, len1);
	audio_buf_index += len1;
	stream += len1;
	len -= len1;
	}
	SDL_Delay(1);

}

int vp_audio(const char * filepath) {
	int ret = 0;

	AVFormatContext* is = NULL;
	AVCodecContext* ic = NULL;
	const AVCodec* codec = NULL;
	AVPacket* pkt = NULL;
	AVFrame* frame = NULL;
	int audio_index;

	//init ffmpeg
	is = avformat_alloc_context();
	pkt = av_packet_alloc();
    frame = av_frame_alloc();

	//初始化网络库
	avformat_network_init();
     
	if (avformat_open_input(&is, filepath, NULL, NULL) != 0) {
		return -1;
	}

	if (avformat_find_stream_info(is, NULL) < 0) {
		return -1;
	}
	//查找音频解码器
	for (int i = 0; i < is->nb_streams; i++) {
		AVStream *stream = NULL;
		stream = is->streams[i];
		if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
			codec = avcodec_find_decoder(stream->codecpar->codec_id);
			ic = avcodec_alloc_context3(codec);
			avcodec_parameters_to_context(ic,stream->codecpar);
			audio_index = i;
		}
	}
	//打开解码器
	if (avcodec_open2(ic, codec, NULL) != 0)
		return -1;


	//SDL 初始化音频模块
	SDL_Init(SDL_INIT_AUDIO | SDL_INIT_AUDIO);

	//初始化SDL中自己想设置的参数
	SDL_AudioSpec wanted_spec ;
	wanted_spec.freq = 44100;
	wanted_spec.format = AUDIO_S16SYS;
	wanted_spec.channels = 2;
	wanted_spec.samples = 1024;
	wanted_spec.callback = read_audio_data;
	wanted_spec.userdata = ic;
	
	//设置转码参数(转码成我们SDL播放的音频参数格式)
	AVChannelLayout out_ch;
	av_channel_layout_default(&out_ch, 2);
	int out_nb_samples = 1024;
	enum AVSampleFormat sample_fmt = AV_SAMPLE_FMT_S16;
	int out_sample_rate = 44100;

	// 解码前的格式参数
	AVChannelLayout in_ch ;
	av_channel_layout_default(&in_ch, 2);
	enum AVSampleFormat in_sample_fmt=ic->sample_fmt;
	int in_sample_rate=ic->sample_rate;

	//转码器
	SwrContext* swr_ctx = NULL;
	swr_alloc_set_opts2(
		&swr_ctx,
		&out_ch,
		sample_fmt,
		out_sample_rate,
		&in_ch,
		in_sample_fmt,
		in_sample_rate,
		0, NULL);

	swr_init(swr_ctx);
	//打开音频播放设备
	if (SDL_OpenAudio(&wanted_spec, NULL) < 0)
		return -1;
	//开始或暂停播放
	SDL_PauseAudio(0);//开始调用回调函数填充缓冲区
	while (true) {
		while (true) {
			if (av_read_frame(is, pkt))
				goto end;//读取完毕
		if (pkt->stream_index == audio_index)
			break;
		}
		//发送编码包
		avcodec_send_packet(ic, pkt);
		av_frame_unref(frame);
		if (avcodec_receive_frame(ic, frame) == 0) {
			//数据转换
			int upper_bound_samples = swr_get_out_samples(swr_ctx, frame->nb_samples);
			uint8_t* out[4] = { 0 };
			out[0] = (uint8_t*)av_malloc(upper_bound_samples * 2 * 2);
			int samples = swr_convert(
				swr_ctx,
				out,
				upper_bound_samples,
				(const uint8_t**)frame->data,
				frame->nb_samples);
			//将数据写入buffer区
			memcpy(audio_buf, out[0], samples * 4);
			audio_size = samples * 4;
			SDL_Delay(19);
		}
	}
end:
	if (is)
		avformat_free_context(is);
	if (ic)
		avcodec_free_context(&ic);
	if (pkt)
		av_packet_free(&pkt);
	if (frame)
		av_frame_free(&frame);
	if (swr_ctx)
		swr_free(&swr_ctx);
	SDL_CloseAudio();
	SDL_Quit();
	return 0;
}

http://www.niftyadmin.cn/n/5314479.html

相关文章

.NET国产化改造探索(五)、结合Nginx并确保.NET应用程序自动启动

随着时代的发展以及近年来信创工作和…废话就不多说了&#xff0c;这个系列就是为.NET遇到国产化需求的一个闭坑系列。接下来&#xff0c;看操作。 上一篇介绍了如何在银河麒麟操作系统上安装Nginx&#xff0c;这篇文章详细介绍下在银河麒麟操作系统上&#xff0c;使用Nginx.N…

2024-01-09 Android.mk 根据c文件名插入特定的宏定义,我这里用于定义log LOG_TAG 标签

一、在Android的构建系统中&#xff0c;使用Android.mk构建脚本可以根据特定需求来定义宏。如果你想根据C文件的名称来插入特定的宏定义&#xff0c;可以使用条件语句检查文件名&#xff0c;并相应地设置宏。 在Android的构建系统中&#xff0c;使用Android.mk构建脚本可以根据…

Fiddler -- https配置

首先&#xff0c;我们先在官网&#xff08;https://www.telerik.com/fiddler&#xff09;下载fiddler 下载好后双击 “.exe” 文件即完成安装 配置HTTPS 打开fiddler&#xff0c;在tools --> options --> https – 一次性勾选所有内容&#xff0c;点击 OK&#xff0c;…

Flask类视图的基本用法及高级技巧详解

概要 当我们谈论Web开发时&#xff0c;Flask是Python世界中最受欢迎的微框架之一。简洁灵活的设计让它在开发小型到中型的Web应用程序时尤其受欢迎。在Flask中处理URL路由时&#xff0c;我们常常会使用基于函数的视图。尽管这很简单直接&#xff0c;但是随着应用的增长&#x…

关于C++中排序和建堆的比较规则:std::greater()、std::less()、自定义比较规则

排序和建堆的使用方式&#xff08;自定义为例&#xff09; 在C中&#xff0c;排序和建堆的比较规则是通过比较函数或者比较对象来定义的。这通常涉及到使用函数对象&#xff08;Functor&#xff09;或者函数指针&#xff0c;以决定元素之间的大小关系。 举例&#xff1a; 利用…

vue3.2引用unplugin-auto-import插入,解放开发中import组件

目录 前言引用unplugin-auto-import插件的优缺点优点缺点 unplugin-auto-import插件引入安装插件配置vite配置更新TypeScript配置使用代码位置 总结 前言 是否添加unplugin-auto-import取决于项目需求和团队习惯。如果项目中频繁使用Vue相关API&#xff0c;并且团队成员都熟悉这…

机器学习指南:如何学习机器学习?

机器学习 一、介绍 你有没有想过计算机是如何从数据中学习和变得更聪明的&#xff1f;这就是机器学习 &#xff08;ML&#xff09; 的魔力&#xff01;这就像计算机科学和统计学的酷炫组合&#xff0c;计算机从大量信息中学习以解决问题并做出预测&#xff0c;就像人类一样。 …

Flink standalone集群部署配置

文章目录 简介软件依赖部署方案二、安装1.下载并解压2.ssh免密登录3.修改配置文件3.启动集群4.访问 Web UI 简介 Flink独立模式&#xff08;Standalone&#xff09;是部署 Flink 最基本也是最简单的方式&#xff1a;所需要的所有 Flink 组件&#xff0c; 都只是操作系统上运行…