player.zip

  • haobin
    了解作者
  • C/C++
    开发工具
  • 6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-03-16 22:16
    上传日期
配合ffmpeg做的音视频解码程序,可解码出PCM、YUV等数据
player.zip
  • player2.c
    6.1KB
  • player3.c
    12.6KB
内容介绍
#include <stdio.h> #include <assert.h rel='nofollow' onclick='return false;'> #include <SDL.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <libswresample/swresample.h> // compatibility with newer API #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1) #define av_frame_alloc avcodec_alloc_frame #define av_frame_free avcodec_free_frame #endif #define SDL_AUDIO_BUFFER_SIZE 1024 #define MAX_AUDIO_FRAME_SIZE 192000 struct SwrContext *audio_convert_ctx = NULL; typedef struct PacketQueue { AVPacketList *first_pkt, *last_pkt; int nb_packets; int size; SDL_mutex *mutex; SDL_cond *cond; } PacketQueue; PacketQueue audioq; int quit = 0; void packet_queue_init(PacketQueue *q) { memset(q, 0, sizeof(PacketQueue)); 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 = 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; } q->last_pkt = pkt1; q->nb_packets++; q->size += pkt1->pkt.size; SDL_CondSignal(q->cond); SDL_UnlockMutex(q->mutex); return 0; } 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; 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; } int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size) { static AVPacket pkt; static uint8_t *audio_pkt_data = NULL; static int audio_pkt_size = 0; static AVFrame frame; int len1, data_size = 0; for(;;) { while(audio_pkt_size > 0) { int got_frame = 0; len1 = avcodec_decode_audio4(aCodecCtx, &frame, &got_frame, &pkt); if(len1 < 0) { /* if error, skip frame */ audio_pkt_size = 0; break; } audio_pkt_data += len1; audio_pkt_size -= len1; data_size = 0; if(got_frame) { //fprintf(stderr, "channels:%d, nb_samples:%d, sample_fmt:%d \n", aCodecCtx->channels, frame.nb_samples, aCodecCtx->sample_fmt); /* data_size = av_samples_get_buffer_size(NULL, aCodecCtx->channels, frame.nb_samples, aCodecCtx->sample_fmt, 1); */ data_size = 2 * 2 * frame.nb_samples; assert(data_size <= buf_size); swr_convert(audio_convert_ctx, &audio_buf, MAX_AUDIO_FRAME_SIZE*3/2, (const uint8_t **)frame.data, frame.nb_samples); //memcpy(audio_buf, frame.data[0], data_size); } if(data_size <= 0) { /* No data yet, get more frames */ continue; } /* We have data, return it and come back for more later */ 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; } } void audio_callback(void *userdata, Uint8 *stream, int len) { AVCodecContext *aCodecCtx = (AVCodecContext *)userdata; int len1, audio_size; static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2]; static unsigned int audio_buf_size = 0; static unsigned int audio_buf_index = 0; while(len > 0) { if(audio_buf_index >= audio_buf_size) { /* We have already sent all our data; get more */ audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf)); if(audio_size < 0) { /* If error, output silence */ audio_buf_size = 1024; // arbitrary? 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; fprintf(stderr, "index=%d, len1=%d, len=%d\n", audio_buf_index, len, len1); memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1); len -= len1; stream += len1; audio_buf_index += len1; } } int main(int argc, char *argv[]) { int ret = -1; int i, videoStream, audioStream; AVFormatContext *pFormatCtx = NULL; //for video decode AVCodecContext *pCodecCtxOrig = NULL; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; struct SwsContext *sws_ctx = NULL; AVPicture *pict = NULL; AVFrame *pFrame = NULL; AVPacket packet; int frameFinished; //for audio decode AVCodecContext *aCodecCtxOrig = NULL; AVCodecContext *aCodecCtx = NULL; AVCodec *aCodec = NULL; int64_t in_channel_layout; int64_t out_channel_layout; //for video render int w_width = 640; int w_height = 480; int pixformat; SDL_Rect rect; SDL_Window *win; SDL_Renderer *renderer; SDL_Texture *texture; //for event SDL_Event event; //for audio SDL_AudioSpec wanted_spec, spec; if(argc < 2) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: command <file>"); return ret; } // Register all formats and codecs av_register_all(); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL - %s\n", SDL_GetError()); return ret; } // Open video file if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open multi-media file"); goto __FAIL; // Couldn't open file } // Retrieve stream information if(avformat_find_stream_info(pFormatCtx, NULL)<0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find stream information "); goto __FAIL; } // Dump information about file onto standard error av_dump_format(pFormatCtx, 0, argv[1], 0); // Find the first video stream videoStream=-1; audioStream=-1; for(i=0; i<pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO && videoStream < 0) { videoStream=i; } if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audioStream < 0) { audioStream=i; } } if(videoStream==-1) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " Didn't find a video stream "); goto __FAIL; // Didn't find a video stream } if(audioStream==-1) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " Didn't find a audio stream "); goto __FAIL; // Didn't find a video stream } aCodecCtxOrig=pFormatCtx->streams[audioStream]->codec; aCodec = avcodec_find_decoder(aCodecCtxOrig->codec_id); if(!aCodec) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported codec! "); goto __FAIL; // Didn't find a video stream } // Copy context aCodecCtx = avcodec_alloc_context3(aCodec); if(avcodec_copy_context(aCodecCtx, aCodecCtxOrig) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't copy codec context! "); goto __FAIL; // Didn't find a video stream } // Set audio settings from codec info wanted_spec.freq = aCodecCtx->sample_rate; wanted_spec.format = AUDIO_S16SYS; wanted_spec.channels = aCodecCtx->channels; wanted_spec.silence = 0; wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; wanted_spec.callback = audio_callback; wanted_spec.userdata = aCodecCtx; if(SDL_OpenAudio(&wanted_spec, &spec) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open audio device - %s!
评论
    相关推荐
    • FFmpeg安装包
      FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了...
    • ffmpeg all
      ffmpeg在win下的编译结果 使用mingw 包括头文件 dll 和lib dll包含所有第三方依赖的dll 经测试可用。生成的是标准dll vc mfc等亦可用 注意dll加载时有依赖。
    • ffmpeg2.6.2
      ffmpeg2.6.2 已经编译好了,打包源码和编译好的库及头文件 我的编译环境为: cat /proc/version Linux version 2.6.18-238.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48))...
    • FFmpeg工具
      FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了...
    • ffmpeg文件
      Fmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了...
    • ffmpeg
      3ae76b105113d944984b2351c61e21c6-opencv_ffmpeg.dll cf3bb5bc9d393b022ea7a42eb63e794d-opencv_ffmpeg_64.dll ec59008da403fb18ab3c1ed66aed583b-ffmpeg_version.cmake 另外:0421e642bc7ad741a2236d3ec4190bdd-...
    • FFMpeg源码
      这是FFmpeg视音频编解码教程的合集,包含了FFmpeg各方面的例程代码。 包含: simplest_ffmpeg_player:简单的视频播放器。 simplest_ffmpeg_audio_player:简单的音频播放器。 simplest_ffmpeg_pic_encoder:...
    • ffmpeg0.7.15
      ffmpeg音视频编解码,ffmpeg is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly ...
    • ffmpeg头文件
      按照ffmpeg的头文件逐行翻译,基本上保持了原貌。 个别地方由于循环引用的关系,用了Pointer指针代替。有注释。 翻译的ffmpeg版本号:2014-05-13 git-c683e6a DelphiXE翻译。
    • ffmpeg视频
      FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了...