ffmpeg sdl 播放器实现(非同步2)
作者:互联网
前面的文章虽然实现了播放器,但是架构是混乱的,这一篇我们做一个结构清晰的播放器。
播放器架构图
解复用、视频解码、音频解码都是很耗时的
所以我们都要新建线程来完成。
由于音频的播放是由音频设备来来拉取的,所以音频的线程不需要创建。
解复用后的数据要放在队列中,所以我们新建了一个队列,用来存放解复用后的AVPacket,音频放在音频队列,视频放在视频队列。
由于解复用、视频解码、音频解码在不同的线程。所以对队列的操作也是多线程对于数据的操作,要用到SDL锁。
typedef struct PacketQueue
{
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
int total;
int end;
int useCout;
int flash;
} PacketQueue;
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 *srcpkt)
{
AVPacket *pkt = av_packet_alloc();
AVPacketList *pkt1;
if (av_packet_ref(pkt, srcpkt) < 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;
//fprintf(stderr, "enqueue, packets:%d, send cond signal\n", q->nb_packets);
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 (global_video_state->quit)
{
fprintf(stderr, "quit from queue_get\n");
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 if(!(q->end))
{
fprintf(stderr, "queue is empty, so wait a moment and wait a cond signal\n");
SDL_CondWait(q->cond, q->mutex);
}else{
ret = -1;
break;
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
从代码总我们可以看到,取元素过程中,如果没有元素了,会通过信号等待函数,等待输入。
else if(!(q->end))
{
fprintf(stderr, "queue is empty, so wait a moment and wait a cond signal\n");
SDL_CondWait(q->cond, q->mutex);
}
对于音频来说最重要的参数是采样大小(位深),采样率,声道数。在重采样的时候,采样个数非常重要,我一般情况下重采样后的采样个数和输入的采样个数要相等。
下面是音频解码的操作:
if (codecCtx->codec_type == AVMEDIA_TYPE_AUDIO)
{
// Set audio settings from codec info
wanted_spec.freq = codecCtx->sample_rate;
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = out_channel;
wanted_spec.silence = 0;
wanted_spec.samples = out_nb_samples;
wanted_spec.callback = audio_callback;
wanted_spec.userdata = is;
if (SDL_OpenAudio(&wanted_spec, &spec) < 0)
{
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}
}
我们通过代码可以知道,音频设备播放会从audio_callback获取播放数据。
void audio_callback(void *userdata, Uint8 *stream, int len)
{
VideoState *is = (VideoState *)userdata;
int len1, audio_size;
SDL_memset(stream, 0, len);
while (len > 0)
{
if (is->audio_buf_index >= is->audio_buf_size)
{
/* We have already sent all our data; get more */
audio_size = audio_decode_frame(is, is->audio_buf, sizeof(is->audio_buf));
if (audio_size < 0)
{
/* If error, output silence */
is->audio_buf_size = 1024 * 2 * 2;
memset(is->audio_buf, 0, is->audio_buf_size);
}
else
{
is->audio_buf_size = audio_size;
}
is->audio_buf_index = 0;
}
len1 = is->audio_buf_size - is->audio_buf_index;
fprintf(stderr, "stream addr:%p, audio_buf_index:%d, audio_buf_size:%d, len1:%d, len:%d\n",
stream,
is->audio_buf_index,
is->audio_buf_size,
len1,
len);
if (len1 > len)
len1 = len;
SDL_MixAudio(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1, SDL_MIX_MAXVOLUME);
len -= len1;
stream += len1;
is->audio_buf_index += len1;
}
}
我们会从audio_decode_frame获取解码后的数据
ffmpeg 解码是通过avcodec_send_packet和avcodec_receive_frame来完成的。他们不是一一对应出现的。一个AVPacket中可能包含多个frame。所以一般写代码是这样的
avcodec_send_packet
while(avcodec_receive_frame == 0){
}
我们audio_decode_frame的逻辑是这样的
伪代码
if(avcodec_receive_frame == 0){
return size;
}
从队列中获取AVPacket
avcodec_send_packet
if(avcodec_receive_frame == 0){
return size;
}
return -1;
真正代码
int audio_decode_frame(VideoState *is, 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 data_size = 0;
int ret = 0;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int index = 0;
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;
frame.channels = is->audio_ctx->channels;
frame.format = is->audio_ctx->sample_fmt;
frame.nb_samples = is->audio_ctx->frame_size;
av_frame_get_buffer(&frame, 0);
for (;;)
{
if (pkt.data)
av_packet_unref(&pkt);
if (is->quit)
{
return -1;
}
ret = avcodec_receive_frame(is->audio_ctx, &frame);
if (ret == 0)
{
goto __SWR_DATA;
}
if (packet_queue_get(&(is->audioq), &pkt, 1) < 0 )
{
if(is->audioq.flash){
goto __RECEIVE;
}
av_log(NULL, AV_LOG_ERROR, "flash audio\n");
is->audioq.flash = 1;
ret = avcodec_send_packet(is->audio_ctx, NULL);
if (ret < 0)
{
return -1;
}
goto __RECEIVE;
}
++(is->audioq.useCout);
ret = avcodec_send_packet(is->audio_ctx, &pkt);
if (ret < 0)
{
ret = -1;
printf("decode error");
av_packet_unref(&pkt);
return -1;
}
if (pkt.data)
{
av_packet_unref(&pkt);
}
__RECEIVE:
index = 0;
ret = avcodec_receive_frame(is->audio_ctx, &frame);
if (ret < 0)
{
return ret;
}
__SWR_DATA:
data_size = av_get_bytes_per_sample(out_format) * out_channel * out_nb_samples;
swr_convert(is->audio_swr_ctx,
&audio_buf,
out_nb_samples,
(const uint8_t **)frame.data,
frame.nb_samples);
return data_size;
}
}
当队列中没有AVPacket时,并且是已经把多媒体文件读完了,我们就要avcodec_send_packet(NULL)用来刷新缓存,告知解码器文件已经读完了。
if (packet_queue_get(&(is->audioq), &pkt, 1) < 0 )
{
if(is->audioq.flash){
goto __RECEIVE;
}
av_log(NULL, AV_LOG_ERROR, "flash audio\n");
is->audioq.flash = 1;
ret = avcodec_send_packet(is->audio_ctx, NULL);
if (ret < 0)
{
return -1;
}
goto __RECEIVE;
}
视频解码与渲染
在主线程中,我们启动一个线程,用来进行视频渲染,如果视频的帧率是25帧,那么我们就每隔1000/25=40ms执行一次渲染。视频解码是很耗时的,所以我们要启动一个线程来对视频进行解码。解码后我们要根据需求缩放为AvFrame为yuv的数据。解码流程同音频解码流程。解码后的数据放在解码视频队列中。渲染函数每隔40ms就从渲染队列中取一条数据进行渲染。
渲染代码
static void schedule_refresh(VideoState *is, int delay)
{
SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
}
static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
{
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = opaque;
SDL_PushEvent(&event);
return 0; /* 0 means stop timer */
}
void video_refresh_timer(void *userdata)
{
VideoState *is = (VideoState *)userdata;
VideoPicture *vp;
if (is->video_st)
{
if (is->pictq_size == 0)
{
schedule_refresh(is, 1); //if the queue is empty, so we shoud be as fast as checking queue of picture
}
else
{
vp = &is->pictq[is->pictq_rindex];
/* Now, normally here goes a ton of code
about timing, etc. we're just going to
guess at a delay for now. You can
increase and decrease this value and hard code
the timing - but I don't suggest that ;)
We'll learn how to do it for real later.
*/
schedule_refresh(is, REFRESH_TIME);
/* show the picture! */
video_display(is);
/* update queue for next picture! */
if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
{
is->pictq_rindex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size--;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
}
}
else
{
schedule_refresh(is, 100);
}
}
视频解码代码同音频解码类似,视频解码后会放在frame队列中,等待渲染函数去获取。
int video_thread(void *arg)
{
VideoState *is = (VideoState *)arg;
static AVPacket pkt;
static AVFrame pFrame;
int ret = 0;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
for (;;)
{
if (pkt.data)
av_packet_unref(&pkt);
if (packet_queue_get(&is->videoq, &pkt, 1) < 0)
{
if(is->videoq.flash){
goto __RECEIVE;
}
av_log(NULL, AV_LOG_ERROR, "flash audio\n");
is->videoq.flash = 1;
ret = avcodec_send_packet(is->video_ctx, NULL);
if (ret < 0)
{
goto __ERROR;
}
goto __RECEIVE;
}
ret = avcodec_send_packet(is->video_ctx, &pkt);
if (ret != 0)
{
printf("decode error");
goto __ERROR;
}
__RECEIVE:
ret = avcodec_receive_frame(is->video_ctx, &pFrame);
if (ret != 0)
{
continue;
}
ret = queue_picture(is, &pFrame);
if (ret < 0)
{
goto __ERROR;
}
}
__ERROR:
if (pkt.data)
{
av_packet_unref(&pkt);
}
return ret;
}
int queue_picture(VideoState *is, AVFrame *pFrame)
{
VideoPicture *vp;
int dst_pix_fmt;
AVPicture pict;
/* wait until we have space for a new pic */
SDL_LockMutex(is->pictq_mutex);
while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
!is->quit)
{
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
}
SDL_UnlockMutex(is->pictq_mutex);
if (is->quit)
{
fprintf(stderr, "quit from queue_picture....\n");
return -1;
}
// windex is set to 0 initially
vp = &is->pictq[is->pictq_windex];
/*
fprintf(stderr, "vp.width=%d, vp.height=%d, video_ctx.width=%d, video_ctx.height=%d\n",
vp->width,
vp->height,
is->video_ctx->width,
is->video_ctx->height);
*/
/* allocate or resize the buffer! */
if (!vp->yuv_frame ||
vp->width != is->video_ctx->width ||
vp->height != is->video_ctx->height)
{
vp->allocated = 0;
alloc_picture(is);
if (is->quit)
{
fprintf(stderr, "quit from queue_picture2....\n");
return -1;
}
}
/* We have a place to put our picture on the queue */
if (vp->yuv_frame)
{
// Convert the image into YUV format that SDL uses
sws_scale(is->sws_ctx,
(uint8_t const *const *)pFrame->data,
pFrame->linesize,
0,
is->video_ctx->height,
vp->yuv_frame->data,
vp->yuv_frame->linesize);
/* now we inform our display thread that we have a pic ready */
if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
{
is->pictq_windex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size++;
SDL_UnlockMutex(is->pictq_mutex);
}
return 0;
}
我们要在播放过程中能够关闭视频播放器,所以我们要在main函数中放入等待事件的函数。
for (;;)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case FF_QUIT_EVENT:
case SDL_QUIT:
fprintf(stderr, "receive a QUIT event: %d\n", event.type);
is->quit = 1;
SDL_CondSignal(is->audioq.cond);
SDL_CondSignal(is->pictq_cond);
goto __QUIT;
break;
case FF_REFRESH_EVENT:
//fprintf(stderr, "receive a refresh event: %d\n", event.type);
video_refresh_timer(event.user.data1);
break;
default:
break;
}
}
如何关闭所有的线程呢?
我们收到关闭事件后,重置一个全局变量quit,其他线程能够尽快检测到这个变量quit,如果quit==1,就退出当前执行的线程。
case SDL_QUIT:
fprintf(stderr, "receive a QUIT event: %d\n", event.type);
is->quit = 1;
SDL_CondSignal(is->audioq.cond);
SDL_CondSignal(is->pictq_cond);
goto __QUIT;
break;
播放后,我们一定不要忘了释放资源。
下面是完整代码:
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/samplefmt.h>
#include <libavutil/imgutils.h>
#include <libavutil/mem.h>
#define MAX_AUDIO_FRAME_SIZE 192000
#define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
#define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
#define FF_REFRESH_EVENT SDL_USEREVENT
#define FF_QUIT_EVENT SDL_USEREVENT + 1
#define REFRESH_TIME 45
#define VIDEO_PICTURE_QUEUE_SIZE 1
static enum AVPixelFormat out_yuv_foramt = AV_PIX_FMT_YUV420P;
typedef struct PacketQueue
{
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
int total;
int end;
int useCout;
int flash;
} PacketQueue;
typedef struct VideoPicture
{
AVFrame *yuv_frame;
int width, height;
int allocated;
} VideoPicture;
typedef struct VideoState
{
char filename[1024];
AVFormatContext *pFormatCtx;
int videoStream, audioStream;
//audio
AVStream *audio_st;
AVCodecContext *audio_ctx;
PacketQueue audioq;
uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
unsigned int audio_buf_size;
unsigned int audio_buf_index;
struct SwrContext *audio_swr_ctx;
//video
AVStream *video_st;
AVCodecContext *video_ctx;
PacketQueue videoq;
struct SwsContext *sws_ctx;
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
//for thread
SDL_mutex *pictq_mutex;
SDL_cond *pictq_cond;
SDL_Thread *parse_tid;
SDL_Thread *video_tid;
int quit;
} VideoState;
//SDL_mutex *texture_mutex;
SDL_Window *win;
SDL_Renderer *renderer;
SDL_Texture *texture;
VideoState *global_video_state;
static Uint8 out_channel = 2;
static enum AVSampleFormat out_format = AV_SAMPLE_FMT_S16;
static int out_nb_samples = 0; //一般情况下输入音频的采样个数要等于输出音频的采样个数
static int out_sample_rate = 0;
static enum AVPixelFormat out_pix_foramt = AV_PIX_FMT_YUV420P;
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 *srcpkt)
{
AVPacket *pkt = av_packet_alloc();
AVPacketList *pkt1;
if (av_packet_ref(pkt, srcpkt) < 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;
//fprintf(stderr, "enqueue, packets:%d, send cond signal\n", q->nb_packets);
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 (global_video_state->quit)
{
fprintf(stderr, "quit from queue_get\n");
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 if(!(q->end))
{
fprintf(stderr, "queue is empty, so wait a moment and wait a cond signal\n");
SDL_CondWait(q->cond, q->mutex);
}else{
ret = -1;
break;
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
int audio_decode_frame(VideoState *is, 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 data_size = 0;
int ret = 0;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int index = 0;
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;
frame.channels = is->audio_ctx->channels;
frame.format = is->audio_ctx->sample_fmt;
frame.nb_samples = is->audio_ctx->frame_size;
av_frame_get_buffer(&frame, 0);
for (;;)
{
if (pkt.data)
av_packet_unref(&pkt);
if (is->quit)
{
return -1;
}
ret = avcodec_receive_frame(is->audio_ctx, &frame);
if (ret == 0)
{
goto __SWR_DATA;
}
if (packet_queue_get(&(is->audioq), &pkt, 1) < 0 )
{
if(is->audioq.flash){
goto __RECEIVE;
}
av_log(NULL, AV_LOG_ERROR, "flash audio\n");
is->audioq.flash = 1;
ret = avcodec_send_packet(is->audio_ctx, NULL);
if (ret < 0)
{
return -1;
}
goto __RECEIVE;
}
++(is->audioq.useCout);
ret = avcodec_send_packet(is->audio_ctx, &pkt);
if (ret < 0)
{
ret = -1;
printf("decode error");
av_packet_unref(&pkt);
return -1;
}
if (pkt.data)
{
av_packet_unref(&pkt);
}
__RECEIVE:
index = 0;
ret = avcodec_receive_frame(is->audio_ctx, &frame);
if (ret < 0)
{
return ret;
}
__SWR_DATA:
data_size = av_get_bytes_per_sample(out_format) * out_channel * out_nb_samples;
swr_convert(is->audio_swr_ctx,
&audio_buf,
out_nb_samples,
(const uint8_t **)frame.data,
frame.nb_samples);
return data_size;
}
}
void audio_callback(void *userdata, Uint8 *stream, int len)
{
VideoState *is = (VideoState *)userdata;
int len1, audio_size;
SDL_memset(stream, 0, len);
while (len > 0)
{
if (is->audio_buf_index >= is->audio_buf_size)
{
/* We have already sent all our data; get more */
audio_size = audio_decode_frame(is, is->audio_buf, sizeof(is->audio_buf));
if (audio_size < 0)
{
/* If error, output silence */
is->audio_buf_size = 1024 * 2 * 2;
memset(is->audio_buf, 0, is->audio_buf_size);
}
else
{
is->audio_buf_size = audio_size;
}
is->audio_buf_index = 0;
}
len1 = is->audio_buf_size - is->audio_buf_index;
fprintf(stderr, "stream addr:%p, audio_buf_index:%d, audio_buf_size:%d, len1:%d, len:%d\n",
stream,
is->audio_buf_index,
is->audio_buf_size,
len1,
len);
if (len1 > len)
len1 = len;
SDL_MixAudio(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1, SDL_MIX_MAXVOLUME);
len -= len1;
stream += len1;
is->audio_buf_index += len1;
}
}
static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
{
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = opaque;
SDL_PushEvent(&event);
return 0; /* 0 means stop timer */
}
static void schedule_refresh(VideoState *is, int delay)
{
SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
}
void video_display(VideoState *is)
{
SDL_Rect rect;
VideoPicture *vp;
float aspect_ratio;
int w, h, x, y;
int i;
vp = &is->pictq[is->pictq_rindex];
if (vp->yuv_frame)
{
if (is->video_ctx->sample_aspect_ratio.num == 0)
{
aspect_ratio = 0;
}
else
{
aspect_ratio = av_q2d(is->video_ctx->sample_aspect_ratio) *
is->video_ctx->width / is->video_ctx->height;
}
if (aspect_ratio <= 0.0)
{
aspect_ratio = (float)is->video_ctx->width /
(float)is->video_ctx->height;
}
// size_t buffer_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 960, 540, 32);
// uint8_t *buffer = malloc(buffer_size);
// int y_size = 960 * 540;
// memcpy(buffer, vp->yuv_frame->data[0], y_size);
// memcpy(buffer + y_size, vp->yuv_frame->data[1], y_size / 4);
// memcpy(buffer + y_size + y_size / 4, vp->yuv_frame->data[2], y_size / 4);
SDL_UpdateYUVTexture(texture, NULL,
vp->yuv_frame->data[0], vp->yuv_frame->linesize[0],
vp->yuv_frame->data[1], vp->yuv_frame->linesize[1],
vp->yuv_frame->data[2], vp->yuv_frame->linesize[2]);
// SDL_UpdateTexture(texture, NULL, buffer, 960);
rect.x = 0;
rect.y = 0;
rect.w = is->video_ctx->width;
rect.h = is->video_ctx->height;
//SDL_LockMutex(texture_mutex);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_RenderPresent(renderer);
//SDL_UnlockMutex(texture_mutex);
}
}
void video_refresh_timer(void *userdata)
{
VideoState *is = (VideoState *)userdata;
VideoPicture *vp;
if (is->video_st)
{
if (is->pictq_size == 0)
{
schedule_refresh(is, 1); //if the queue is empty, so we shoud be as fast as checking queue of picture
}
else
{
vp = &is->pictq[is->pictq_rindex];
/* Now, normally here goes a ton of code
about timing, etc. we're just going to
guess at a delay for now. You can
increase and decrease this value and hard code
the timing - but I don't suggest that ;)
We'll learn how to do it for real later.
*/
schedule_refresh(is, REFRESH_TIME);
/* show the picture! */
video_display(is);
/* update queue for next picture! */
if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
{
is->pictq_rindex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size--;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
}
}
else
{
schedule_refresh(is, 100);
}
}
void alloc_picture(void *userdata)
{
VideoState *is = (VideoState *)userdata;
VideoPicture *vp;
vp = &is->pictq[is->pictq_windex];
if (vp->yuv_frame)
{ //free space if vp->pict is not NULL
av_frame_free(&(vp->yuv_frame));
free(vp->yuv_frame);
}
// Allocate a place to put our YUV image on that screen
//SDL_LockMutex(texture_mutex);
vp->yuv_frame = av_frame_alloc();
vp->yuv_frame->width = is->video_ctx->width;
vp->yuv_frame->height = is->video_ctx->height;
vp->yuv_frame->format = out_yuv_foramt;
av_frame_get_buffer(vp->yuv_frame, 32);
vp->width = is->video_ctx->width;
vp->height = is->video_ctx->height;
vp->allocated = 1;
}
int queue_picture(VideoState *is, AVFrame *pFrame)
{
VideoPicture *vp;
int dst_pix_fmt;
AVPicture pict;
/* wait until we have space for a new pic */
SDL_LockMutex(is->pictq_mutex);
while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
!is->quit)
{
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
}
SDL_UnlockMutex(is->pictq_mutex);
if (is->quit)
{
fprintf(stderr, "quit from queue_picture....\n");
return -1;
}
// windex is set to 0 initially
vp = &is->pictq[is->pictq_windex];
/*
fprintf(stderr, "vp.width=%d, vp.height=%d, video_ctx.width=%d, video_ctx.height=%d\n",
vp->width,
vp->height,
is->video_ctx->width,
is->video_ctx->height);
*/
/* allocate or resize the buffer! */
if (!vp->yuv_frame ||
vp->width != is->video_ctx->width ||
vp->height != is->video_ctx->height)
{
vp->allocated = 0;
alloc_picture(is);
if (is->quit)
{
fprintf(stderr, "quit from queue_picture2....\n");
return -1;
}
}
/* We have a place to put our picture on the queue */
if (vp->yuv_frame)
{
// Convert the image into YUV format that SDL uses
sws_scale(is->sws_ctx,
(uint8_t const *const *)pFrame->data,
pFrame->linesize,
0,
is->video_ctx->height,
vp->yuv_frame->data,
vp->yuv_frame->linesize);
/* now we inform our display thread that we have a pic ready */
if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
{
is->pictq_windex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size++;
SDL_UnlockMutex(is->pictq_mutex);
}
return 0;
}
int video_thread(void *arg)
{
VideoState *is = (VideoState *)arg;
static AVPacket pkt;
static AVFrame pFrame;
int ret = 0;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
for (;;)
{
if (pkt.data)
av_packet_unref(&pkt);
if (packet_queue_get(&is->videoq, &pkt, 1) < 0)
{
if(is->videoq.flash){
goto __RECEIVE;
}
av_log(NULL, AV_LOG_ERROR, "flash audio\n");
is->videoq.flash = 1;
ret = avcodec_send_packet(is->video_ctx, NULL);
if (ret < 0)
{
goto __ERROR;
}
goto __RECEIVE;
}
ret = avcodec_send_packet(is->video_ctx, &pkt);
if (ret != 0)
{
printf("decode error");
goto __ERROR;
}
__RECEIVE:
ret = avcodec_receive_frame(is->video_ctx, &pFrame);
if (ret != 0)
{
continue;
}
ret = queue_picture(is, &pFrame);
if (ret < 0)
{
goto __ERROR;
}
}
__ERROR:
if (pkt.data)
{
av_packet_unref(&pkt);
}
return ret;
}
int stream_component_open(VideoState *is, int stream_index)
{
int64_t in_channel_layout, out_channel_layout;
AVFormatContext *pFormatCtx = is->pFormatCtx;
AVCodecContext *codecCtx = NULL;
AVCodec *codec = NULL;
SDL_AudioSpec wanted_spec, spec;
AVCodecParameters *codec_par = NULL;
if (stream_index < 0 || stream_index >= pFormatCtx->nb_streams)
{
return -1;
}
codec_par = pFormatCtx->streams[stream_index]->codecpar;
if(stream_index == is->audioStream){
out_nb_samples = codec_par->frame_size;
out_sample_rate = codec_par->sample_rate;
}
codec = avcodec_find_decoder(codec_par->codec_id);
codecCtx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codecCtx, codec_par);
if (!codec)
{
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
if (codecCtx->codec_type == AVMEDIA_TYPE_AUDIO)
{
// Set audio settings from codec info
wanted_spec.freq = codecCtx->sample_rate;
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = out_channel;
wanted_spec.silence = 0;
wanted_spec.samples = out_nb_samples;
wanted_spec.callback = audio_callback;
wanted_spec.userdata = is;
if (SDL_OpenAudio(&wanted_spec, &spec) < 0)
{
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}
}
if (avcodec_open2(codecCtx, codec, NULL) < 0)
{
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
switch (codecCtx->codec_type)
{
case AVMEDIA_TYPE_AUDIO:
is->audio_st = pFormatCtx->streams[stream_index];
is->audio_ctx = codecCtx;
is->audio_buf_size = 0;
is->audio_buf_index = 0;
packet_queue_init(&is->audioq);
SDL_PauseAudio(0);
//Out Audio Param
uint64_t out_channel_layout = av_get_default_channel_layout(out_channel);
//uint8_t *out_buffer=(uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
int64_t in_channel_layout = av_get_default_channel_layout(is->audio_ctx->channels);
struct SwrContext *audio_convert_ctx = NULL;
audio_convert_ctx = swr_alloc();
if (!audio_convert_ctx)
{
printf("Failed to swr_alloc\n");
return -1;
}
swr_alloc_set_opts(audio_convert_ctx,
out_channel_layout,
out_format,
out_sample_rate,
in_channel_layout,
is->audio_ctx->sample_fmt,
is->audio_ctx->sample_rate,
0,
NULL);
fprintf(stderr, "swr opts: out_channel_layout:%lld, out_sample_fmt:%d, out_sample_rate:%d, in_channel_layout:%lld, in_sample_fmt:%d, in_sample_rate:%d\n",
out_channel_layout,
out_format,
out_sample_rate,
in_channel_layout,
is->audio_ctx->sample_fmt,
is->audio_ctx->sample_rate);
swr_init(audio_convert_ctx);
is->audio_swr_ctx = audio_convert_ctx;
break;
case AVMEDIA_TYPE_VIDEO:
is->video_st = pFormatCtx->streams[stream_index];
is->video_ctx = codecCtx;
packet_queue_init(&is->videoq);
is->video_tid = SDL_CreateThread(video_thread, "video_thread", is);
is->sws_ctx = sws_getContext(is->video_ctx->width,
is->video_ctx->height,
is->video_ctx->pix_fmt,
is->video_ctx->width,
is->video_ctx->height,
out_pix_foramt,
SWS_BILINEAR,
NULL, NULL, NULL);
break;
default:
break;
}
return 0;
}
int decode_thread(void *arg)
{
VideoState *is = arg;
AVPacket packet;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
if (is->audioStream >= 0)
{
stream_component_open(is, is->audioStream);
}
if (is->videoStream >= 0)
{
stream_component_open(is, is->videoStream);
}
fprintf(stderr, "video context: width=%d, height=%d\n", is->video_ctx->width, is->video_ctx->height);
// main decode loop
for (;;)
{
if (is->quit)
{
SDL_CondSignal(is->videoq.cond);
SDL_CondSignal(is->audioq.cond);
break;
}
// seek stuff goes here
if (is->audioq.size > MAX_AUDIOQ_SIZE ||
is->videoq.size > MAX_VIDEOQ_SIZE)
{
SDL_Delay(10);
continue;
}
int ret = av_read_frame(is->pFormatCtx, &packet);
fprintf(stderr, "av_read_frame, ret :%s\n", av_err2str(ret));
if (ret < 0)
{
break;
}
// Is this a packet from the video stream?
if (packet.stream_index == is->videoStream)
{
packet_queue_put(&is->videoq, &packet);
++(is->videoq.total);
fprintf(stderr, "put video queue, size :%d\n", is->videoq.total);
}
else if (packet.stream_index == is->audioStream)
{
packet_queue_put(&is->audioq, &packet);
++(is->audioq.total);
fprintf(stderr, "put audio queue, size :%d\n", is->audioq.total);
}
av_packet_unref(&packet);
}
is->audioq.end = 1;
is->videoq.end = 1;
/* all done - wait for it */
while (!is->quit)
{
SDL_Delay(100);
}
fail:
if (1)
{
SDL_Event event;
event.type = FF_QUIT_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event);
}
return 0;
}
int init_VideoState(VideoState *is){
Uint32 pixformat;
AVFormatContext *pFormatCtx = NULL;
AVPacket pkt1, *packet = &pkt1;
int i;
is->videoStream = -1;
is->audioStream = -1;
global_video_state = is;
// Open video file
if (avformat_open_input(&pFormatCtx, is->filename, NULL, NULL) != 0)
return -1; // Couldn't open file
is->pFormatCtx = pFormatCtx;
// Retrieve stream information
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, is->filename, 0);
// Find the first video stream
is->videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, -1);
is->audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, -1);
if (is->videoStream < 0 || is->audioStream < 0)
{
av_log(NULL, AV_LOG_ERROR, "%s: could not open codecs\n", is->filename);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int ret = -1;
SDL_Event event;
VideoState *is;
if (argc < 2)
{
fprintf(stderr, "Usage: test <file>\n");
exit(1);
}
av_log_set_level(AV_LOG_INFO);
//big struct, it's core
is = av_mallocz(sizeof(VideoState));
// Register all formats and codecs
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}
//texture_mutex = SDL_CreateMutex();
memcpy(is->filename, argv[1], sizeof(is->filename));
is->pictq_mutex = SDL_CreateMutex();
is->pictq_cond = SDL_CreateCond();
ret = init_VideoState(is);
if(ret < 0){
goto __FAIL;
}
AVCodecParameters* video_paramters = is->pFormatCtx->streams[is->videoStream]->codecpar;
win = SDL_CreateWindow("Media Player",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
video_paramters->width,
video_paramters->height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(win, -1, 0);
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
video_paramters->width,
video_paramters->height);
//set timer
schedule_refresh(is, 40);
is->parse_tid = SDL_CreateThread(decode_thread, "decode_thread", is);
if (!is->parse_tid)
{
av_free(is);
goto __FAIL;
}
for (;;)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case FF_QUIT_EVENT:
case SDL_QUIT:
fprintf(stderr, "receive a QUIT event: %d\n", event.type);
is->quit = 1;
SDL_CondSignal(is->audioq.cond);
SDL_CondSignal(is->pictq_cond);
goto __QUIT;
break;
case FF_REFRESH_EVENT:
//fprintf(stderr, "receive a refresh event: %d\n", event.type);
video_refresh_timer(event.user.data1);
break;
default:
break;
}
}
__QUIT:
ret = 0;
__FAIL:
SDL_Delay(20);
SDL_Quit();
if(is){
if(is->audio_swr_ctx){
swr_close(is->audio_swr_ctx);
swr_free(&(is->audio_swr_ctx));
}
if(is->sws_ctx){
sws_freeContext(is->sws_ctx);
}
if(is->audio_ctx){
avcodec_close(is->audio_ctx);
avcodec_free_context(&is->audio_ctx);
}
if(is->video_ctx){
avcodec_close(is->video_ctx);
avcodec_free_context(&is->video_ctx);
}
if(is->pFormatCtx){
avformat_close_input(&(is->pFormatCtx));
avformat_free_context(is->pFormatCtx);
}
}
return ret;
}
标签:播放器,ffmpeg,pkt,ctx,SDL,video,sdl,audio,size 来源: https://blog.csdn.net/qq_15255121/article/details/117327999