#2
forever742023-04-22 08:51
|
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
知道,这是因为FFmpeg库版本引起的问题。
可现在使用的已经是,ffmpeg-4.4.1-full_build-shared版本了。。。
如果使用,ffmpeg-5.0.1-full_build-shared版本,有几十个错误。。。
什么地方可以下载更早的版本呀。。。
FFmpeg官网上全是英文,看不懂。。。
程序代码:
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <SDL.h>
#include <libavutil/log.h>
#include <libavutil/time.h>
#include <libavutil/avstring.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#define SDL_AUDIO_BUFFER_SIZE 1024
#define MAX_AUDIO_FRAME_SIZE 192000
#define AV_SYNC_THRESHOLD 0.01
#define AV_NOSYNC_THRESHOLD 10.0
#define MAX_AUDIOQ_SIZE (5*6*1024)
#define MAX_VEDIOQ_SIZE (5*256*1024)
#define FF_REFRESH_EVENT (SDL_USEREVENT)
#define FF_QUIT_EVENT (SDL_USEREVENT + 1)
#define VIDEO_PICTURE_QUEUE_SIZE 1
typedef struct PacketQueue {
AVPacketList* first_pkt, * last_pkt;
int nb_packets;
int size;
SDL_mutex* mutex;
SDL_cond* cond;
}PacketQueue;
typedef struct VideoPicture {
AVPicture* pict;
int width, height;
int allocated;
double pts;
}VideoPicture;
typedef struct VideoState {
char filename[1024];
AVFormatContext* pFormatCtx;
int videoStream, audioStream;
AVStream* audio_st;
AVCodecContext* audio_ctx;
struct SwrContext* audio_swr_cxt;
uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
unsigned int audio_buf_size;
unsigned int audio_buf_index;
PacketQueue audioq;
AVFrame audio_frame;
AVPacket audio_pkt;
uint8_t* audio_pkt_data;
int audio_pkt_size;
AVStream* video_st;
AVCodecContext* video_ctx;
struct SwsContext* sws_ctx;
PacketQueue videoq;
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size;
int pictq_rindex;
int pictq_windex;
SDL_mutex* pictq_mutex;
SDL_cond* pictq_cond;
SDL_Thread* parse_tid;
SDL_Thread* video_tid;
double audio_clock;
double video_clock;
double frame_timer;
double frame_last_pts;
double frame_last_delay;
int quit;
}VideoState;
SDL_mutex* texture_mutex;
SDL_Window* win;
SDL_Renderer* renderer;
SDL_Texture* texture;
VideoState* global_video_state;
void packet_queue_init(PacketQueue* q) {
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
int packet_queue_get(PacketQueue* q, AVPacket* pkt, int block) {
AVPacketList* pktl;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
if (global_video_state->quit) {
fprintf(stderr, "quit from queue_get!\n");
ret = -1;
break;
}
pktl = q->first_pkt;
if (pktl) {
q->first_pkt = pktl->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pktl->pkt.size;
*pkt = pktl->pkt;
av_free(pktl);
ret = 1;
break;
}
else if (!block) {
ret = 0;
break;
}
else {
fprintf(stderr, "queue is empty, so wait a moment and wait a cond signal!\n");
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
int packet_queue_put(PacketQueue* q, AVPacket* pkt) {
AVPacketList* pktl;
if (av_dup_packet(pkt) < 0) {
return -1;
}
pktl = av_malloc(sizeof(AVPacketList));
if (!pktl)
{
return -1;
}
pktl->pkt = *pkt;
pktl->next = NULL;
SDL_LockMutex(q->mutex);
if (!q->last_pkt)
q->first_pkt = pktl;
else
q->last_pkt->next = pktl;
q->last_pkt = pktl;
q->nb_packets++;
q->size += pktl->pkt.size;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return 0;
}
double get_audio_clock(VideoState* is) {
double pts;
int hw_buf_size, bytes_per_sec, n;
pts = is->audio_clock;
hw_buf_size = is->audio_buf_size - is->audio_buf_index;
bytes_per_sec = 0;
n = is->audio_ctx->channels * 2;
if (is->audio_st) {
bytes_per_sec = is->audio_ctx->sample_rate * n;
}
if (bytes_per_sec) {
pts -= (double)hw_buf_size / bytes_per_sec;
}
return pts;
}
int audio_decode_frame(VideoState* is, uint8_t* audio_buf, int buf_size, double* pts_ptr) {
int len1, data_size = 0, n;
AVPacket* pkt = &is->audio_pkt;
double pts;
for (;;) {
while (is->audio_pkt_size > 0) {
int got_frame = 0;
len1 = avcodec_decode_audio4(is->audio_ctx, &is->audio_frame, &got_frame, pkt);
if (len1 < 0) {
printf("Failed to decode audio...\n");
is->audio_pkt_size = 0;
break;
}
data_size = 0;
if (got_frame) {
data_size = 2 * 2 * is->audio_frame.nb_samples;
assert(data_size <= buf_size);
swr_convert(is->audio_swr_cxt,
&audio_buf,
MAX_AUDIO_FRAME_SIZE * 3 / 2,
(const uint8_t**)is->audio_frame.data,
is->audio_frame.nb_samples);
is->audio_pkt_size -= len1;
is->audio_pkt_data += len1;
}
if (data_size == 0)
continue;
pts = is->audio_clock;
*pts_ptr = pts;
n = 2 * is->audio_ctx->channels;
is->audio_clock += (double)data_size / (double)(n * is->audio_ctx->sample_rate);
return data_size;
}
if (pkt->data)
av_free_packet(pkt);
if (is->quit) {
printf("Will quit program...\n");
return -1;
}
if (packet_queue_get(&is->audioq, pkt, 0) <= 0) {
return -1;
}
is->audio_pkt_data = pkt->data;
is->audio_pkt_size = pkt->size;
if (pkt->pts != AV_NOPTS_VALUE) {
is->audio_clock = av_q2d(is->audio_st->time_base) * pkt->pts;
}
}
}
void audio_callback(void* userdata, uint8_t* stream, int len) {
VideoState* is = (VideoState*)userdata;
int len1, audio_size;
double pts;
SDL_memset(stream, 0, len);
while (len > 0) {
if (is->audio_buf_index >= is->audio_buf_size) {
audio_size = audio_decode_frame(is, is->audio_buf, sizeof(is->audio_buf), &pts);
if (audio_size < 0) {
is->audio_buf_size = 1024 * 2 * 2;
printf("--------------no audio packet-------------------\n");
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;
if (len1 > len)
len1 = len;
//memcpy(stream,(uint8_t*)is->audio_buf+is->audio_buf_index,len1);
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_t sdl_refresh_timer_cb(uint32_t interval, void* dat) {
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = dat;
SDL_PushEvent(&event);
return 0;
}
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;
vp = &is->pictq[is->pictq_rindex];
if (vp->pict) {
SDL_UpdateYUVTexture(texture, NULL,
vp->pict->data[0], vp->pict->linesize[0],
vp->pict->data[1], vp->pict->linesize[1],
vp->pict->data[2], vp->pict->linesize[2]);
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;
/*
actual_delay:
delay:
sync_threshold
ref_clock:
diff:
*/
double actual_delay, delay, sync_threshold, ref_clock, diff;
if (is->video_st) {
if (is->pictq_size == 0) {
schedule_refresh(is, 1);
}
else {
vp = &is->pictq[is->pictq_rindex];
delay = vp->pts - is->frame_last_pts;
if (delay <= 0 || delay >= 1.0) {
delay = is->frame_last_delay;
}
is->frame_last_delay = delay;
is->frame_last_pts = vp->pts;
ref_clock = get_audio_clock(is);
diff = vp->pts - ref_clock;
sync_threshold = (delay > AV_SYNC_THRESHOLD) ? delay : AV_SYNC_THRESHOLD;
if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
if (diff <= -sync_threshold) {
delay = 0;
}
else if (diff >= sync_threshold) {
delay *= 2;
}
}
is->frame_timer += delay;
actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
if (actual_delay < AV_SYNC_THRESHOLD) {
actual_delay = AV_SYNC_THRESHOLD;
}
schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
video_display(is);
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 = &is->pictq[is->pictq_windex];
if (vp->pict) {
avpicture_free(vp->pict);
free(vp->pict);
}
SDL_LockMutex(texture_mutex);
vp->pict = (AVPicture*)malloc(sizeof(AVPicture));
if (vp->pict) {
avpicture_alloc(vp->pict,
AV_PIX_FMT_YUV420P,
is->video_ctx->width,
is->video_ctx->height);
}
SDL_UnlockMutex(texture_mutex);
vp->width = is->video_ctx->width;
vp->height = is->video_ctx->height;
vp->allocated = 1;
}
int queue_picture(VideoState* is, AVFrame* pFrame, double pts) {
VideoPicture* vp;
int dst_pix_fmt;
AVPicture pict;
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) {
printf("quit from queue picture...\n");
return -1;
}
vp = &is->pictq[is->pictq_windex];
if (!vp->pict ||
vp->width != is->video_ctx->width ||
vp->height != is->video_ctx->height) {
vp->allocated = 0;
alloc_picture(is);
if (is->quit) {
printf("quit from queue picture 2...\n");
return -1;
}
}
if (vp->pict) {
vp->pts = pts;
sws_scale(is->sws_ctx, (uint8_t const* const*)pFrame->data,
pFrame->linesize, 0, is->video_ctx->height,
vp->pict->data, vp->pict->linesize);
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;
}
double synchronize_video(VideoState* is, AVFrame* src_frame, double pts) {
double frame_delay;
if (pts != 0) {
is->video_clock = pts;
}
else {
pts = is->video_clock;
}
frame_delay = av_q2d(is->video_ctx->time_base);
frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
is->video_clock += frame_delay;
return pts;
}
int video_thread(void* arg) {
VideoState* is = (VideoState*)arg;
AVPacket packet, * pkt = &packet;
int frameFinished;
AVFrame* pFrame;
double pts;
pFrame = av_frame_alloc();
for (;;) {
if (packet_queue_get(&is->videoq, pkt, 1) < 0) {
break;
}
avcodec_decode_video2(is->video_ctx, pFrame, &frameFinished, pkt);
if ((pts = av_frame_get_best_effort_timestamp(pFrame)) == AV_NOPTS_VALUE) {
pts = 0;
}
pts *= av_q2d(is->video_st->time_base);
if (frameFinished) {
pts = synchronize_video(is, pFrame, pts);
if (queue_picture(is, pFrame, pts) < 0) {
break;
}
}
av_free_packet(pkt);
}
av_frame_free(&pFrame);
return 0;
}
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 new_spec, old_spec;
codec = avcodec_find_decoder(pFormatCtx->streams[stream_index]->codec->codec_id);
if (!codec) {
printf("UnSupport codec!\n");
return -1;
}
codecCtx = avcodec_alloc_context3(codec);
if (!codecCtx) {
printf("Failed to alloc codec context\n");
return -1;
}
if (avcodec_copy_context(codecCtx, pFormatCtx->streams[stream_index]->codec) < 0) {
printf("Failed to copy codec context\n");
return -1;
}
if (codecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
new_spec.freq = codecCtx->sample_rate;
new_spec.format = AUDIO_S16SYS;
new_spec.channels = codecCtx->channels;
new_spec.silence = 0;
new_spec.samples = SDL_AUDIO_BUFFER_SIZE;
new_spec.callback = audio_callback;
new_spec.userdata = is;
if (SDL_OpenAudio(&new_spec, &old_spec) < 0) {
printf("SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}
}
if (avcodec_open2(codecCtx, codec, NULL) < 0) {
printf("Failed to open codec\n");
return -1;
}
switch (codecCtx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
is->audioStream = stream_index;
is->audio_st = pFormatCtx->streams[stream_index];
is->audio_ctx = codecCtx;
is->audio_buf_size = 0;
is->audio_buf_index = 0;
memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
packet_queue_init(&is->audioq);
SDL_PauseAudio(0);
in_channel_layout = av_get_default_channel_layout(is->audio_ctx->channels);
out_channel_layout = in_channel_layout;
is->audio_swr_cxt = swr_alloc();
swr_alloc_set_opts(is->audio_swr_cxt,
out_channel_layout,
AV_SAMPLE_FMT_S16,
is->audio_ctx->sample_rate,
in_channel_layout,
is->audio_ctx->sample_fmt,
is->audio_ctx->sample_rate,
0,
NULL);
swr_init(is->audio_swr_cxt);
break;
case AVMEDIA_TYPE_VIDEO:
is->videoStream = stream_index;
is->video_st = pFormatCtx->streams[stream_index];
is->video_ctx = codecCtx;
is->frame_timer = (double)av_gettime() / 1000000.0;
is->frame_last_delay = 40e-3;
packet_queue_init(&is->videoq);
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,
AV_PIX_FMT_YUV420P,
SWS_BILINEAR,
NULL, NULL, NULL);
is->video_tid = SDL_CreateThread(video_thread, "video_thread", is);
break;
default:
break;
}
return 0;
}
int decode_thread(void* arg) {
VideoState* is = (VideoState*)arg;
AVFormatContext* pFormatCtx = NULL;
AVPacket packet, * ptk = &packet;
SDL_Event event;
int i, ret = 0;
int video_index = -1;
int audio_index = -1;
is->videoStream = -1;
is->audioStream = -1;
global_video_state = is;
if (avformat_open_input(&pFormatCtx, is->filename, NULL, NULL) < 0) {
printf("Failed to open file[%s] context\n", is->filename);
return -1;
}
is->pFormatCtx = pFormatCtx;
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("Failed to get detail stream infomation\n");
return -1;
}
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && video_index < 0)
video_index = i;
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && audio_index < 0)
audio_index = i;
}
if (video_index < 0 || audio_index < 0) {
printf("Failed to find stream index for audio/video\n");
return -1;
}
av_dump_format(pFormatCtx, audio_index, is->filename, 0);
av_dump_format(pFormatCtx, video_index, is->filename, 0);
ret = stream_component_open(is, audio_index);
if (ret < 0) {
printf("Failed to initialize audio data\n");
goto fail;
}
ret = stream_component_open(is, video_index);
if (ret < 0) {
printf("Failed to initialize video data\n");
goto fail;
}
win = SDL_CreateWindow("Media Player",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
is->video_ctx->width, is->video_ctx->height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
renderer = SDL_CreateRenderer(win, -1, 0);
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
is->video_ctx->width,
is->video_ctx->height);
for (;;) {
if (is->quit) {
SDL_CondSignal(is->videoq.cond);
SDL_CondSignal(is->audioq.cond);
break;
}
if (is->audioq.size > MAX_AUDIOQ_SIZE ||
is->videoq.size > MAX_VEDIOQ_SIZE) {
SDL_Delay(10);
continue;
}
if (av_read_frame(is->pFormatCtx, ptk) < 0) {
if (is->pFormatCtx->pb->error == 0) {
SDL_Delay(100);
continue;
}
else {
break;
}
}
if (ptk->stream_index == is->videoStream) {
packet_queue_put(&is->videoq, ptk);
printf("put video queue, size:%d\n", is->videoq.nb_packets);
}
else if (ptk->stream_index == is->audioStream) {
packet_queue_put(&is->audioq, ptk);
printf("put audio queue, size:%d\n", is->audioq.nb_packets);
}
else {
av_free_packet(ptk);
}
}
while (!is->quit) {
SDL_Delay(100);
}
fail:
event.type = FF_QUIT_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event);
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");
return ret;
}
is = av_malloc(sizeof(VideoState));
av_register_all();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
printf("Failed to initialize SDL - %s\n", SDL_GetError());
return ret;
}
texture_mutex = SDL_CreateMutex();
av_strlcpy(is->filename, argv[1], sizeof(is->filename));
is->pictq_mutex = SDL_CreateMutex();
is->pictq_cond = SDL_CreateCond();
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:
printf("receive a QUIT event:%d\n", event.type);
is->quit = 1;
goto __QUIT;
break;
case FF_REFRESH_EVENT:
video_refresh_timer(event.user.data1);
break;
default:
break;
}
}
__QUIT:
ret = 0;
__FAIL:
SDL_Quit();
return ret;
}
#include <stdio.h>
#include <math.h>
#include <SDL.h>
#include <libavutil/log.h>
#include <libavutil/time.h>
#include <libavutil/avstring.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#define SDL_AUDIO_BUFFER_SIZE 1024
#define MAX_AUDIO_FRAME_SIZE 192000
#define AV_SYNC_THRESHOLD 0.01
#define AV_NOSYNC_THRESHOLD 10.0
#define MAX_AUDIOQ_SIZE (5*6*1024)
#define MAX_VEDIOQ_SIZE (5*256*1024)
#define FF_REFRESH_EVENT (SDL_USEREVENT)
#define FF_QUIT_EVENT (SDL_USEREVENT + 1)
#define VIDEO_PICTURE_QUEUE_SIZE 1
typedef struct PacketQueue {
AVPacketList* first_pkt, * last_pkt;
int nb_packets;
int size;
SDL_mutex* mutex;
SDL_cond* cond;
}PacketQueue;
typedef struct VideoPicture {
AVPicture* pict;
int width, height;
int allocated;
double pts;
}VideoPicture;
typedef struct VideoState {
char filename[1024];
AVFormatContext* pFormatCtx;
int videoStream, audioStream;
AVStream* audio_st;
AVCodecContext* audio_ctx;
struct SwrContext* audio_swr_cxt;
uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
unsigned int audio_buf_size;
unsigned int audio_buf_index;
PacketQueue audioq;
AVFrame audio_frame;
AVPacket audio_pkt;
uint8_t* audio_pkt_data;
int audio_pkt_size;
AVStream* video_st;
AVCodecContext* video_ctx;
struct SwsContext* sws_ctx;
PacketQueue videoq;
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size;
int pictq_rindex;
int pictq_windex;
SDL_mutex* pictq_mutex;
SDL_cond* pictq_cond;
SDL_Thread* parse_tid;
SDL_Thread* video_tid;
double audio_clock;
double video_clock;
double frame_timer;
double frame_last_pts;
double frame_last_delay;
int quit;
}VideoState;
SDL_mutex* texture_mutex;
SDL_Window* win;
SDL_Renderer* renderer;
SDL_Texture* texture;
VideoState* global_video_state;
void packet_queue_init(PacketQueue* q) {
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
int packet_queue_get(PacketQueue* q, AVPacket* pkt, int block) {
AVPacketList* pktl;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
if (global_video_state->quit) {
fprintf(stderr, "quit from queue_get!\n");
ret = -1;
break;
}
pktl = q->first_pkt;
if (pktl) {
q->first_pkt = pktl->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pktl->pkt.size;
*pkt = pktl->pkt;
av_free(pktl);
ret = 1;
break;
}
else if (!block) {
ret = 0;
break;
}
else {
fprintf(stderr, "queue is empty, so wait a moment and wait a cond signal!\n");
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
int packet_queue_put(PacketQueue* q, AVPacket* pkt) {
AVPacketList* pktl;
if (av_dup_packet(pkt) < 0) {
return -1;
}
pktl = av_malloc(sizeof(AVPacketList));
if (!pktl)
{
return -1;
}
pktl->pkt = *pkt;
pktl->next = NULL;
SDL_LockMutex(q->mutex);
if (!q->last_pkt)
q->first_pkt = pktl;
else
q->last_pkt->next = pktl;
q->last_pkt = pktl;
q->nb_packets++;
q->size += pktl->pkt.size;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return 0;
}
double get_audio_clock(VideoState* is) {
double pts;
int hw_buf_size, bytes_per_sec, n;
pts = is->audio_clock;
hw_buf_size = is->audio_buf_size - is->audio_buf_index;
bytes_per_sec = 0;
n = is->audio_ctx->channels * 2;
if (is->audio_st) {
bytes_per_sec = is->audio_ctx->sample_rate * n;
}
if (bytes_per_sec) {
pts -= (double)hw_buf_size / bytes_per_sec;
}
return pts;
}
int audio_decode_frame(VideoState* is, uint8_t* audio_buf, int buf_size, double* pts_ptr) {
int len1, data_size = 0, n;
AVPacket* pkt = &is->audio_pkt;
double pts;
for (;;) {
while (is->audio_pkt_size > 0) {
int got_frame = 0;
len1 = avcodec_decode_audio4(is->audio_ctx, &is->audio_frame, &got_frame, pkt);
if (len1 < 0) {
printf("Failed to decode audio...\n");
is->audio_pkt_size = 0;
break;
}
data_size = 0;
if (got_frame) {
data_size = 2 * 2 * is->audio_frame.nb_samples;
assert(data_size <= buf_size);
swr_convert(is->audio_swr_cxt,
&audio_buf,
MAX_AUDIO_FRAME_SIZE * 3 / 2,
(const uint8_t**)is->audio_frame.data,
is->audio_frame.nb_samples);
is->audio_pkt_size -= len1;
is->audio_pkt_data += len1;
}
if (data_size == 0)
continue;
pts = is->audio_clock;
*pts_ptr = pts;
n = 2 * is->audio_ctx->channels;
is->audio_clock += (double)data_size / (double)(n * is->audio_ctx->sample_rate);
return data_size;
}
if (pkt->data)
av_free_packet(pkt);
if (is->quit) {
printf("Will quit program...\n");
return -1;
}
if (packet_queue_get(&is->audioq, pkt, 0) <= 0) {
return -1;
}
is->audio_pkt_data = pkt->data;
is->audio_pkt_size = pkt->size;
if (pkt->pts != AV_NOPTS_VALUE) {
is->audio_clock = av_q2d(is->audio_st->time_base) * pkt->pts;
}
}
}
void audio_callback(void* userdata, uint8_t* stream, int len) {
VideoState* is = (VideoState*)userdata;
int len1, audio_size;
double pts;
SDL_memset(stream, 0, len);
while (len > 0) {
if (is->audio_buf_index >= is->audio_buf_size) {
audio_size = audio_decode_frame(is, is->audio_buf, sizeof(is->audio_buf), &pts);
if (audio_size < 0) {
is->audio_buf_size = 1024 * 2 * 2;
printf("--------------no audio packet-------------------\n");
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;
if (len1 > len)
len1 = len;
//memcpy(stream,(uint8_t*)is->audio_buf+is->audio_buf_index,len1);
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_t sdl_refresh_timer_cb(uint32_t interval, void* dat) {
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = dat;
SDL_PushEvent(&event);
return 0;
}
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;
vp = &is->pictq[is->pictq_rindex];
if (vp->pict) {
SDL_UpdateYUVTexture(texture, NULL,
vp->pict->data[0], vp->pict->linesize[0],
vp->pict->data[1], vp->pict->linesize[1],
vp->pict->data[2], vp->pict->linesize[2]);
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;
/*
actual_delay:
delay:
sync_threshold
ref_clock:
diff:
*/
double actual_delay, delay, sync_threshold, ref_clock, diff;
if (is->video_st) {
if (is->pictq_size == 0) {
schedule_refresh(is, 1);
}
else {
vp = &is->pictq[is->pictq_rindex];
delay = vp->pts - is->frame_last_pts;
if (delay <= 0 || delay >= 1.0) {
delay = is->frame_last_delay;
}
is->frame_last_delay = delay;
is->frame_last_pts = vp->pts;
ref_clock = get_audio_clock(is);
diff = vp->pts - ref_clock;
sync_threshold = (delay > AV_SYNC_THRESHOLD) ? delay : AV_SYNC_THRESHOLD;
if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
if (diff <= -sync_threshold) {
delay = 0;
}
else if (diff >= sync_threshold) {
delay *= 2;
}
}
is->frame_timer += delay;
actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
if (actual_delay < AV_SYNC_THRESHOLD) {
actual_delay = AV_SYNC_THRESHOLD;
}
schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
video_display(is);
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 = &is->pictq[is->pictq_windex];
if (vp->pict) {
avpicture_free(vp->pict);
free(vp->pict);
}
SDL_LockMutex(texture_mutex);
vp->pict = (AVPicture*)malloc(sizeof(AVPicture));
if (vp->pict) {
avpicture_alloc(vp->pict,
AV_PIX_FMT_YUV420P,
is->video_ctx->width,
is->video_ctx->height);
}
SDL_UnlockMutex(texture_mutex);
vp->width = is->video_ctx->width;
vp->height = is->video_ctx->height;
vp->allocated = 1;
}
int queue_picture(VideoState* is, AVFrame* pFrame, double pts) {
VideoPicture* vp;
int dst_pix_fmt;
AVPicture pict;
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) {
printf("quit from queue picture...\n");
return -1;
}
vp = &is->pictq[is->pictq_windex];
if (!vp->pict ||
vp->width != is->video_ctx->width ||
vp->height != is->video_ctx->height) {
vp->allocated = 0;
alloc_picture(is);
if (is->quit) {
printf("quit from queue picture 2...\n");
return -1;
}
}
if (vp->pict) {
vp->pts = pts;
sws_scale(is->sws_ctx, (uint8_t const* const*)pFrame->data,
pFrame->linesize, 0, is->video_ctx->height,
vp->pict->data, vp->pict->linesize);
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;
}
double synchronize_video(VideoState* is, AVFrame* src_frame, double pts) {
double frame_delay;
if (pts != 0) {
is->video_clock = pts;
}
else {
pts = is->video_clock;
}
frame_delay = av_q2d(is->video_ctx->time_base);
frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
is->video_clock += frame_delay;
return pts;
}
int video_thread(void* arg) {
VideoState* is = (VideoState*)arg;
AVPacket packet, * pkt = &packet;
int frameFinished;
AVFrame* pFrame;
double pts;
pFrame = av_frame_alloc();
for (;;) {
if (packet_queue_get(&is->videoq, pkt, 1) < 0) {
break;
}
avcodec_decode_video2(is->video_ctx, pFrame, &frameFinished, pkt);
if ((pts = av_frame_get_best_effort_timestamp(pFrame)) == AV_NOPTS_VALUE) {
pts = 0;
}
pts *= av_q2d(is->video_st->time_base);
if (frameFinished) {
pts = synchronize_video(is, pFrame, pts);
if (queue_picture(is, pFrame, pts) < 0) {
break;
}
}
av_free_packet(pkt);
}
av_frame_free(&pFrame);
return 0;
}
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 new_spec, old_spec;
codec = avcodec_find_decoder(pFormatCtx->streams[stream_index]->codec->codec_id);
if (!codec) {
printf("UnSupport codec!\n");
return -1;
}
codecCtx = avcodec_alloc_context3(codec);
if (!codecCtx) {
printf("Failed to alloc codec context\n");
return -1;
}
if (avcodec_copy_context(codecCtx, pFormatCtx->streams[stream_index]->codec) < 0) {
printf("Failed to copy codec context\n");
return -1;
}
if (codecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
new_spec.freq = codecCtx->sample_rate;
new_spec.format = AUDIO_S16SYS;
new_spec.channels = codecCtx->channels;
new_spec.silence = 0;
new_spec.samples = SDL_AUDIO_BUFFER_SIZE;
new_spec.callback = audio_callback;
new_spec.userdata = is;
if (SDL_OpenAudio(&new_spec, &old_spec) < 0) {
printf("SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}
}
if (avcodec_open2(codecCtx, codec, NULL) < 0) {
printf("Failed to open codec\n");
return -1;
}
switch (codecCtx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
is->audioStream = stream_index;
is->audio_st = pFormatCtx->streams[stream_index];
is->audio_ctx = codecCtx;
is->audio_buf_size = 0;
is->audio_buf_index = 0;
memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
packet_queue_init(&is->audioq);
SDL_PauseAudio(0);
in_channel_layout = av_get_default_channel_layout(is->audio_ctx->channels);
out_channel_layout = in_channel_layout;
is->audio_swr_cxt = swr_alloc();
swr_alloc_set_opts(is->audio_swr_cxt,
out_channel_layout,
AV_SAMPLE_FMT_S16,
is->audio_ctx->sample_rate,
in_channel_layout,
is->audio_ctx->sample_fmt,
is->audio_ctx->sample_rate,
0,
NULL);
swr_init(is->audio_swr_cxt);
break;
case AVMEDIA_TYPE_VIDEO:
is->videoStream = stream_index;
is->video_st = pFormatCtx->streams[stream_index];
is->video_ctx = codecCtx;
is->frame_timer = (double)av_gettime() / 1000000.0;
is->frame_last_delay = 40e-3;
packet_queue_init(&is->videoq);
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,
AV_PIX_FMT_YUV420P,
SWS_BILINEAR,
NULL, NULL, NULL);
is->video_tid = SDL_CreateThread(video_thread, "video_thread", is);
break;
default:
break;
}
return 0;
}
int decode_thread(void* arg) {
VideoState* is = (VideoState*)arg;
AVFormatContext* pFormatCtx = NULL;
AVPacket packet, * ptk = &packet;
SDL_Event event;
int i, ret = 0;
int video_index = -1;
int audio_index = -1;
is->videoStream = -1;
is->audioStream = -1;
global_video_state = is;
if (avformat_open_input(&pFormatCtx, is->filename, NULL, NULL) < 0) {
printf("Failed to open file[%s] context\n", is->filename);
return -1;
}
is->pFormatCtx = pFormatCtx;
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("Failed to get detail stream infomation\n");
return -1;
}
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && video_index < 0)
video_index = i;
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && audio_index < 0)
audio_index = i;
}
if (video_index < 0 || audio_index < 0) {
printf("Failed to find stream index for audio/video\n");
return -1;
}
av_dump_format(pFormatCtx, audio_index, is->filename, 0);
av_dump_format(pFormatCtx, video_index, is->filename, 0);
ret = stream_component_open(is, audio_index);
if (ret < 0) {
printf("Failed to initialize audio data\n");
goto fail;
}
ret = stream_component_open(is, video_index);
if (ret < 0) {
printf("Failed to initialize video data\n");
goto fail;
}
win = SDL_CreateWindow("Media Player",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
is->video_ctx->width, is->video_ctx->height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
renderer = SDL_CreateRenderer(win, -1, 0);
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
is->video_ctx->width,
is->video_ctx->height);
for (;;) {
if (is->quit) {
SDL_CondSignal(is->videoq.cond);
SDL_CondSignal(is->audioq.cond);
break;
}
if (is->audioq.size > MAX_AUDIOQ_SIZE ||
is->videoq.size > MAX_VEDIOQ_SIZE) {
SDL_Delay(10);
continue;
}
if (av_read_frame(is->pFormatCtx, ptk) < 0) {
if (is->pFormatCtx->pb->error == 0) {
SDL_Delay(100);
continue;
}
else {
break;
}
}
if (ptk->stream_index == is->videoStream) {
packet_queue_put(&is->videoq, ptk);
printf("put video queue, size:%d\n", is->videoq.nb_packets);
}
else if (ptk->stream_index == is->audioStream) {
packet_queue_put(&is->audioq, ptk);
printf("put audio queue, size:%d\n", is->audioq.nb_packets);
}
else {
av_free_packet(ptk);
}
}
while (!is->quit) {
SDL_Delay(100);
}
fail:
event.type = FF_QUIT_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event);
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");
return ret;
}
is = av_malloc(sizeof(VideoState));
av_register_all();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
printf("Failed to initialize SDL - %s\n", SDL_GetError());
return ret;
}
texture_mutex = SDL_CreateMutex();
av_strlcpy(is->filename, argv[1], sizeof(is->filename));
is->pictq_mutex = SDL_CreateMutex();
is->pictq_cond = SDL_CreateCond();
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:
printf("receive a QUIT event:%d\n", event.type);
is->quit = 1;
goto __QUIT;
break;
case FF_REFRESH_EVENT:
video_refresh_timer(event.user.data1);
break;
default:
break;
}
}
__QUIT:
ret = 0;
__FAIL:
SDL_Quit();
return ret;
}
[此贴子已经被作者于2023-4-22 01:15编辑过]