Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2147810
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-09-11 18:17:10

一.
1. 己经学会了用alsa播放pcm并且ffmpeg可以解码mp3为pcm,那么把这两者合并起来
就是ffmpeg解码mp3后用alsa播放
2. 代码
  1. #include "utils.h"
  2. #include <libavutil/avutil.h>
  3. #include <libavutil/attributes.h>
  4. #include <libavutil/opt.h>
  5. #include <libavutil/mathematics.h>
  6. #include <libavutil/imgutils.h>
  7. #include <libavutil/samplefmt.h>
  8. #include <libavutil/timestamp.h>
  9. #include <libavformat/avformat.h>
  10. #include <libavcodec/avcodec.h>
  11. #include <libswscale/swscale.h>
  12. #include <libavutil/mathematics.h>
  13. #include <libswresample/swresample.h>
  14. #include <libavutil/channel_layout.h>
  15. #include <libavutil/common.h>
  16. #include <libavformat/avio.h>
  17. #include <libavutil/file.h>
  18. #include <libswresample/swresample.h>
  19. #include <alsa/asoundlib.h>
  20. #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000

  21. typedef struct {
  22.     int videoindex;
  23.     int sndindex;
  24.     AVFormatContext* pFormatCtx;
  25.     AVCodecContext* sndCodecCtx;
  26.     AVCodec* sndCodec;
  27.     SwrContext *swr_ctx;
  28.     snd_pcm_t *pcm;
  29.     DECLARE_ALIGNED(16,uint8_t,audio_buf) [AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
  30. }AudioState;

  31. void ffmpeg_fmt_to_alsa_fmt(AVCodecContext *pCodecCtx, snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
  32. {
  33.     switch(pCodecCtx->sample_fmt) {
  34.         case AV_SAMPLE_FMT_U8: //unsigned 8 bits
  35.             snd_pcm_hw_params_set_format(pcm, params, SND_PCM_FORMAT_S8);
  36.             break;
  37.         case AV_SAMPLE_FMT_S16: //signed 16 bits
  38.             dbmsg("AV_SAMPLE_FMT_S16");
  39.             //SND_PCM_FORMAT_S16 is ok, not care SND_PCM_FORMAT_S16_LE or SND_PCM_FORMAT_S16_BE
  40.             snd_pcm_hw_params_set_format(pcm, params, SND_PCM_FORMAT_S16); //SND_PCM_FORMAT_S16_LE
  41.             break;
  42.         case AV_SAMPLE_FMT_S16P: //signed 16 bits, planar
  43.             dbmsg("AV_SAMPLE_FMT_S16P");
  44.             //SND_PCM_FORMAT_S16 is ok, not care SND_PCM_FORMAT_S16_LE or SND_PCM_FORMAT_S16_BE
  45.             snd_pcm_hw_params_set_format(pcm, params, SND_PCM_FORMAT_S16);
  46.             break;
  47.         default:
  48.             break;
  49.     }
  50. }

  51. int init_ffmpeg_alsa(AudioState* is, char* filepath)
  52. {
  53.     int i=0;
  54.     int ret;
  55.     //alsa    
  56.     snd_pcm_hw_params_t *params;
  57.     unsigned int val;
  58.     int dir;
  59.     snd_pcm_uframes_t buffer_size;

  60.     is->sndindex = -1;
  61.     if(NULL == filepath)
  62.     {
  63.         dbmsg("input file is NULL");
  64.         return -1;
  65.     }
  66.     avcodec_register_all();
  67.     avfilter_register_all();
  68.     av_register_all();

  69.     is->pFormatCtx = avformat_alloc_context();

  70.     if(avformat_open_input(&is->pFormatCtx, filepath, NULL, NULL)!=0)
  71.         return -1;

  72.     if(avformat_find_stream_info(is->pFormatCtx, NULL)<0)
  73.         return -1;
  74.     av_dump_format(is->pFormatCtx,0, 0, 0);
  75.     is->videoindex = av_find_best_stream(is->pFormatCtx, AVMEDIA_TYPE_VIDEO, is->videoindex, -1, NULL, 0);
  76.     is->sndindex = av_find_best_stream(is->pFormatCtx, AVMEDIA_TYPE_AUDIO,is->sndindex, is->videoindex, NULL, 0);
  77.     dbmsg("videoindex=%d, sndindex=%d", is->videoindex, is->sndindex);
  78.     if(is->sndindex != -1)
  79.     {
  80.         is->sndCodecCtx = is->pFormatCtx->streams[is->sndindex]->codec;
  81.         is->sndCodec = avcodec_find_decoder(is->sndCodecCtx->codec_id);
  82.         if(is->sndCodec == NULL)
  83.         {
  84.             dbmsg("Codec not found");
  85.             return -1;
  86.         }
  87.         if(avcodec_open2(is->sndCodecCtx, is->sndCodec, NULL) < 0)
  88.             return -1;
  89.         snd_pcm_open(&is->pcm, "default", SND_PCM_STREAM_PLAYBACK, 0);
  90.         snd_pcm_hw_params_alloca(&params);
  91.         snd_pcm_hw_params_any(is->pcm, params);
  92.         snd_pcm_hw_params_set_access(is->pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED);
  93.         ffmpeg_fmt_to_alsa_fmt(is->sndCodecCtx, is->pcm, params);
  94.         snd_pcm_hw_params_set_channels(is->pcm, params, is->sndCodecCtx->channels);
  95.         val = is->sndCodecCtx->sample_rate;
  96.         dbmsg("is->sndCodecCtx->sample_rate=%d", is->sndCodecCtx->sample_rate);
  97.         snd_pcm_hw_params_set_rate_near(is->pcm, params, &val, &dir);    
  98.         snd_pcm_hw_params(is->pcm, params);
  99.     }
  100.     return 0;
  101. }

  102. int main(int argc, char **argv)
  103. {
  104.     int ret;
  105.     FILE* fp;
  106.     int file_data_size = 0;
  107.     int len2,len3, data_size, got_frame;
  108.     AVPacket *packet = av_mallocz(sizeof(AVPacket));
  109.     AVFrame *frame = av_frame_alloc();
  110.     AudioState* is = (AudioState*) av_mallocz(sizeof(AudioState));
  111.     uint8_t *out[] = { is->audio_buf };
  112.     if( (ret=init_ffmpeg_alsa(is, argv[1])) != 0)
  113.     {
  114.         dbmsg("init_ffmpeg error");
  115.         return -1;
  116.     }
  117.     while( (av_read_frame(is->pFormatCtx, packet)>=0) )
  118.     {
  119.         if(packet->stream_index != is->sndindex)
  120.             continue;
  121.         if((ret=avcodec_decode_audio4(is->sndCodecCtx, frame, &got_frame, packet)) < 0) //decode data is store in frame
  122.         {
  123.             dbmsg("file eof");
  124.             break;
  125.         }

  126.         if(got_frame <= 0) /* No data yet, get more frames */
  127.             continue;
  128.         if(NULL==is->swr_ctx)
  129.         {
  130.             if(is->swr_ctx != NULL)
  131.                 swr_free(&is->swr_ctx);
  132.             dbmsg("AV_CH_LAYOUT_STEREO=%d, AV_SAMPLE_FMT_S16=%d, freq=44100", AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16);
  133.             dbmsg("frame: channnels=%d, default_layout=%d, format=%d, sample_rate=%d", frame->channels,av_get_default_channel_layout(frame->channels), frame->format, frame->sample_rate);
  134.             is->swr_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, is->sndCodecCtx->sample_rate, av_get_default_channel_layout(frame->channels), frame->format, frame->sample_rate, 0, NULL);
  135.             if(is->swr_ctx == NULL)
  136.             {
  137.                 dbmsg("swr_ctx == NULL");
  138.             }
  139.             swr_init(is->swr_ctx);
  140.         }
  141.         //dbmsg("next swr_convert");
  142.         len2 = swr_convert(is->swr_ctx, out, is->sndCodecCtx->sample_rate,(const uint8_t **)frame->extended_data, frame->nb_samples);
  143.         data_size = av_samples_get_buffer_size(NULL, is->sndCodecCtx->channels, frame->nb_samples, is->sndCodecCtx->sample_fmt, 1);
  144.         //dbmsg("data_size 111 = %d", data_size);
  145.         //data_size = len2 * 2 * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);         -->通过av_sample_get_buffer_size的值与这儿计算的值是一样的
  146.         //dbmsg("data_size 222 = %d", data_size);
  147.         //dbmsg("next pcm write, data_size=%d", data_size);
  148.         ret = snd_pcm_writei(is->pcm, is->audio_buf, data_size/4);                   -->最后面的4应该通过计算获得: 采样位数/8*声道数
  149.         if (ret < 0)
  150.             ret = snd_pcm_recover(is->pcm, ret, 0); //ALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred
  151.         //dbmsg("pcm write over, data_size=%d", data_size/4);
  152.     }
  153.     av_free_packet(packet);
  154.     av_free(frame);
  155.     avcodec_close(is->sndCodecCtx);
  156.     avformat_close_input(&is->pFormatCtx);
  157.     snd_pcm_close(is->pcm);
  158.     fclose(fp);
  159.     return 0;
  160. }
关于snd_pcm_writei --> 最后的i代表是SND_PCM_ACCESS_RW_INTERLEAVED类型的接口
即:SND_PCM_ACCESS_RW_INTERLEAVED类型的读写接口是 snd_pcm_readi/snd_pcm_writei
snd_pcm_sframes_t snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
它的最后一个参数是snd_pcm_uframes_t,不像系统read write都是字节数,两者是不一样的。
这个size=buffer_size/(采样位数/8*声道数)
3.运行结果
  1. cong@msi:/work/ffmpeg/test/alsa/testalsa/6ffmpeg_alsa$ make run
  2. export LD_LIBRARY_PATH=/work/ffmpeg/out/lib/ \
  3.     && ./ffmpeg_alsa /work/ffmpeg/test/resource//yeye.mp3
  4. libavutil/crc.c:av_crc_init[313]:
  5. [mp3 @ 0xe9d3e0] Skipping 0 bytes of junk at 2169.
  6. Input #0, mp3, from '(null)':
  7.   Metadata:
  8.     album : 八度空间
  9.     artist : 周杰伦
  10.     title : 爷爷泡的茶
  11.   Duration: 00:03:58.55, start: 0.025057, bitrate: 320 kb/s
  12.     Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
  13.     Metadata:
  14.       encoder : LAME3.98r
  15.     Side data:
  16.       replaygain: track gain - -10.100000, track peak - unknown, album gain - unknown, album peak - unknown,
  17. ffmpeg_alsa.c:init_ffmpeg_alsa[84]: videoindex=-1381258232, sndindex=0
  18. ffmpeg_alsa.c:ffmpeg_fmt_to_alsa_fmt[45]: AV_SAMPLE_FMT_S16P
  19. ffmpeg_alsa.c:init_ffmpeg_alsa[103]: is->sndCodecCtx->sample_rate=44100
  20. ffmpeg_alsa.c:main[142]: AV_CH_LAYOUT_STEREO=3, AV_SAMPLE_FMT_S16=1, freq=44100
  21. ffmpeg_alsa.c:main[143]: frame: channnels=2, default_layout=3, format=6, sample_rate=44100
  22. ALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred              --> 这就是为什么要加上snd_pcm_recover

4.代码打包
6ffmpeg_alsa.rar(下载后改名为6ffmpeg_alsa.tar.gz)


阅读(4067) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~