1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <stdio.h> #include <SDL_types.h> #include "SDL.h"
#include "libswresample/swresample.h" #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h>
static Uint8 *audio_chunk; static Uint32 audio_len; static Uint8 *audio_pos;
#define MAX_AUDIO_FRAME_SIZE 19200
void read_audio_data(void *udata, Uint8 *stream, int len) {
SDL_memset(stream, 0, len); if (audio_len == 0) return; len = (len > audio_len ? audio_len : len);
SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME); audio_pos += len; audio_len -= len; }
int WinMain(int argc, char *argv[]) { char *file = "C:\\Users\\lenovo\\Desktop\\1080p.mov";
AVFormatContext *pFormatCtx = NULL;
int i, audioStream = -1;
AVCodecParameters *pCodecParameters = NULL; AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket *packet; uint8_t *out_buffer;
int64_t in_channel_layout; struct SwrContext *au_convert_ctx;
if (avformat_open_input(&pFormatCtx, file, NULL, NULL) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open video file!"); return -1; }
audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (audioStream == -1) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Din't find a video stream!"); return -1; }
pCodecParameters = pFormatCtx->streams[audioStream]->codecpar;
pCodec = avcodec_find_decoder(pCodecParameters->codec_id); if (pCodec == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported codec!\n"); return -1; }
pCodecCtx = avcodec_alloc_context3(pCodec); if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't copy codec context"); return -1; }
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open decoder!\n"); return -1; } packet = (AVPacket *) av_malloc(sizeof(AVPacket)); av_init_packet(packet); pFrame = av_frame_alloc();
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO; int out_nb_samples = 1024; enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16; int out_sample_rate = 44100; int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1); out_buffer = (uint8_t *) av_malloc(MAX_AUDIO_FRAME_SIZE * 2);
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) { printf("Could not initialize SDL - %s\n", SDL_GetError()); return -1; }
SDL_AudioSpec spec; spec.freq = out_sample_rate; spec.format = AUDIO_S16SYS; spec.channels = out_channels; spec.silence = 0; spec.samples = out_nb_samples; spec.callback = read_audio_data; spec.userdata = pCodecCtx;
if (SDL_OpenAudio(&spec, NULL) < 0) { printf("can't open audio.\n"); return -1; }
in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels); printf("in_channel_layout --->%d\n", in_channel_layout); au_convert_ctx = swr_alloc(); au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate,in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL); swr_init(au_convert_ctx);
SDL_PauseAudio(0);
while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == audioStream) { avcodec_send_packet(pCodecCtx, packet); while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **) pFrame->data,pFrame->nb_samples); }
audio_chunk = (Uint8 *) out_buffer; audio_len = out_buffer_size; audio_pos = audio_chunk;
while (audio_len > 0) { SDL_Delay(1); } } av_packet_unref(packet); } swr_free(&au_convert_ctx); SDL_Quit();
return 0; }
|