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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include <stdio.h> #include <SDL.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h>
int WinMain(int argc, char *argv[]) { int ret = -1; char *file = "C:\\Users\\lenovo\\Desktop\\fengjing.mp4"; AVFormatContext *pFormatCtx = NULL; int i, videoStream; AVCodecParameters *pCodecParameters = NULL; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet;
SDL_Rect rect; Uint32 pixformat; SDL_Window *win = NULL; SDL_Renderer *renderer = NULL; SDL_Texture *texture = NULL; int w_width = 640; int w_height = 480; 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; }
if (avformat_open_input(&pFormatCtx, file, NULL, NULL) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open video file!"); goto __FAIL; }
videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (videoStream == -1) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Din't find a video stream!"); goto __FAIL; }
pCodecParameters = pFormatCtx->streams[videoStream]->codecpar;
pCodec = avcodec_find_decoder(pCodecParameters->codec_id); if (pCodec == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported codec!\n"); goto __FAIL; }
pCodecCtx = avcodec_alloc_context3(pCodec); if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't copy codec context"); goto __FAIL; }
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open decoder!\n"); goto __FAIL; }
pFrame = av_frame_alloc();
w_width = pCodecCtx->width; w_height = pCodecCtx->height;
win = SDL_CreateWindow("Media Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w_width, w_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (!win) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create window by SDL"); goto __FAIL; }
renderer = SDL_CreateRenderer(win, -1, 0); if (!renderer) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create Renderer by SDL"); goto __FAIL; }
pixformat = SDL_PIXELFORMAT_IYUV; texture = SDL_CreateTexture(renderer, pixformat, SDL_TEXTUREACCESS_STREAMING, w_width, w_height);
while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { avcodec_send_packet(pCodecCtx, &packet); while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
SDL_UpdateYUVTexture(texture, NULL, pFrame->data[0], pFrame->linesize[0], pFrame->data[1], pFrame->linesize[1], pFrame->data[2], pFrame->linesize[2]);
rect.x = 0; rect.y = 0; rect.w = pCodecCtx->width; rect.h = pCodecCtx->height;
SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, &rect); SDL_RenderPresent(renderer); } }
av_packet_unref(&packet);
SDL_Event event; SDL_PollEvent(&event); switch (event.type) { case SDL_QUIT: goto __QUIT; default: break; }
}
__QUIT: ret = 0;
__FAIL: if (pFrame) { av_frame_free(&pFrame); }
if (pCodecCtx) { avcodec_close(pCodecCtx); }
if (pCodecParameters) { avcodec_parameters_free(&pCodecParameters); }
if (pFormatCtx) { avformat_close_input(&pFormatCtx); }
if (win) { SDL_DestroyWindow(win); }
if (renderer) { SDL_DestroyRenderer(renderer); }
if (texture) { SDL_DestroyTexture(texture); }
SDL_Quit();
return ret; }
|