Outputting to the Screen
发现官网的教程示例比较老,很多API都变了,重新发一份比较新的版本。
对应的教程地址:这里
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <SDL.h>
#include <SDL_thread.h>
#include <stdio.h>
#include <string.h>
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame);
int main(int argc, char **argv)
{
AVFormatContext *pFormateCtx = NULL;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVFrame *pFrameRGB = NULL;
uint8_t *buffer = NULL;
int numBytes;
int videoStream = -1;
struct SwsContext *sws_cts = NULL;
int i;
int ret;
AVCodecParameters codeParameters;
SDL_Window *screen;
SDL_Surface *surface;
SDL_Renderer* renderer;
SDL_Texture* testexture;
SDL_Event event;
SDL_Rect rect;
char filePath[256] = { 0 };
if (argc < 2)
{
printf("Please input video name:");
gets_s(filePath, 256);
}
else
{
strcpy_s(filePath, 256, argv[1]);
}
av_register_all();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
printf_s("SDL_Init failed:%s\n", SDL_GetError());
return -1;
}
if (avformat_open_input(&pFormateCtx, filePath, NULL, 0) != 0)
{
printf_s("avformat_open_input failed\n");
return -1;
}
if (avformat_find_stream_info(pFormateCtx, NULL) < 0)
{
printf_s("avformat_find_stream_info failed\n");
return -1;
}
av_dump_format(pFormateCtx, 0, filePath, 0);
for (unsigned i = 0; i < pFormateCtx->nb_streams; ++i)
{
if (AVMEDIA_TYPE_VIDEO == pFormateCtx->streams[i]->codec->codec_type)
{
videoStream = i;
break;
}
}
if (-1 == videoStream)
{
printf_s("Can't find video stream\n");
return -1;
}
pCodecCtx = pFormateCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (NULL == pCodec)
{
printf_s("avcodec_find_decoder failed\n");
return -1;
}
pCodecCtxOrig = avcodec_alloc_context3(pCodec);
if (NULL == pCodecCtxOrig)
{
printf_s("avcodec_alloc_context3 failed\n");
return -1;
}
memset(&codeParameters, 0, sizeof(codeParameters));
if (avcodec_parameters_from_context(&codeParameters, pCodecCtx) < 0)
{
printf_s("avcodec_parameters_from_context failed\n");
return -1;
}
if (avcodec_parameters_to_context(pCodecCtxOrig, &codeParameters) < 0)
{
printf_s("avcodec_parameters_to_context failed\n");
return -1;
}
if (avcodec_open2(pCodecCtxOrig, pCodec, NULL) < 0)
{
printf_s("avcodec_open2 failed\n");
return -1;
}
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
numBytes = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtxOrig->width, pCodecCtxOrig->height, 1);
buffer = (uint8_t *)av_mallocz(numBytes * sizeof(uint8_t));
memset(buffer, 0, numBytes * sizeof(uint8_t));
ret = av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_YUV420P, pCodecCtxOrig->width, pCodecCtxOrig->height, 1);
AVPacket packet;
sws_cts = sws_getContext(
pCodecCtxOrig->width,
pCodecCtxOrig->height,
pCodecCtxOrig->pix_fmt,
pCodecCtxOrig->width,
pCodecCtxOrig->height,
AV_PIX_FMT_YUV420P,
SWS_BILINEAR,
NULL, NULL, NULL);
screen = SDL_CreateWindow("Hell's Player",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
pCodecCtxOrig->width, pCodecCtxOrig->height, SDL_WINDOW_SHOWN| SDL_WINDOW_ALLOW_HIGHDPI);
surface = SDL_GetWindowSurface(screen);
renderer = SDL_CreateRenderer(screen, -1, 0);
testexture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
pCodecCtxOrig->width, pCodecCtxOrig->height);
rect.x = 0;
rect.y = 0;
rect.h = pCodecCtxOrig->height;
rect.w = pCodecCtxOrig->width;
i = 0;
while (av_read_frame(pFormateCtx, &packet) >= 0)
{
if (packet.stream_index != videoStream)
{
continue;
}
ret = avcodec_send_packet(pCodecCtxOrig, &packet);
switch (ret)
{
case 0:
break;
case AVERROR(EAGAIN):
return -1;
case AVERROR_EOF:
return -1;
case AVERROR(EINVAL):
return -1;
case AVERROR(ENOMEM):
return -1;
}
ret = avcodec_receive_frame(pCodecCtxOrig, pFrame);
switch (ret)
{
case 0:
break;
case AVERROR(EAGAIN):
printf_s("input is not accepted right now - the packet must be resent after trying to read output\n");
continue;
case AVERROR_EOF:
return -1;
case AVERROR(EINVAL):
return -1;
}
ret = sws_scale(sws_cts, (uint8_t const* const *)pFrame->data,
pFrame->linesize, 0, pCodecCtxOrig->height,
pFrameRGB->data, pFrameRGB->linesize);
SDL_UpdateTexture(testexture, &rect, pFrameRGB->data[0], pFrameRGB->linesize[0]);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, testexture, &rect, &rect);
SDL_RenderPresent(renderer);
SDL_PollEvent(&event);
if (SDL_QUIT == event.type)
{
break;
}
}
av_packet_unref(&packet);
av_free(buffer);
av_free(pFrameRGB);
av_free(pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormateCtx);
return 0;
}
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFileName[32];
int y;
sprintf_s(szFileName, 32, "frame%d.ppm", iFrame);
fopen_s(&pFile, szFileName, "wb");
if (NULL == pFile)
{
return;
}
fprintf_s(pFile, "P6\n%d %d\n255\n", width, height);
for (y = 0; y < height; ++y)
{
fwrite(pFrame->data[0] + y*pFrame->linesize[0], 1, width * 3, pFile);
}
fclose(pFile);
}