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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <SDL2/SDL.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h>
#define REFRESH_EVENT (SDL_USEREVENT + 1)
SDL_Window* window; SDL_Renderer* renderer; SDL_Texture* texture; AVFormatContext* fmt_ctx; AVCodecContext* codec_ctx; int video_stream_index;
int video_play_thread(void* arg) { AVPacket* pkt = av_packet_alloc(); AVFrame* frame = av_frame_alloc(); struct SwsContext* sws_ctx = NULL;
while (av_read_frame(fmt_ctx, pkt) >= 0) { if (pkt->stream_index == video_stream_index) { if (avcodec_send_packet(codec_ctx, pkt) == 0) { while (avcodec_receive_frame(codec_ctx, frame) == 0) { if (!sws_ctx) { sws_ctx = sws_getContext( codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL ); }
AVFrame* rgb_frame = av_frame_alloc(); av_image_alloc(rgb_frame->data, rgb_frame->linesize, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, 1); sws_scale(sws_ctx, (const uint8_t* const*)frame->data, frame->linesize, 0, frame->height, rgb_frame->data, rgb_frame->linesize);
SDL_UpdateTexture(texture, NULL, rgb_frame->data[0], rgb_frame->linesize[0]);
SDL_Event event; event.type = REFRESH_EVENT; SDL_PushEvent(&event);
int delay = (codec_ctx->time_base.num * 1000) / codec_ctx->time_base.den; SDL_Delay(delay); av_freep(&rgb_frame->data[0]); av_frame_free(&rgb_frame); } } } av_packet_unref(pkt); } return 0; } #undef main int main(int argc, char* argv[]) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError()); return -1; } window = SDL_CreateWindow("Video Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN); if (!window) { fprintf(stderr, "SDL: could not create window - exiting:%s\n", SDL_GetError()); return -1; } renderer = SDL_CreateRenderer(window, -1, 0); if (!renderer) { fprintf(stderr, "SDL: could not create renderer - exiting:%s\n", SDL_GetError()); return -1; }
if (avformat_open_input(&fmt_ctx, "test.mp4", NULL, NULL) < 0) { fprintf(stderr, "Could not open source file %s\n", "test.mp4"); return -1; } if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { fprintf(stderr, "Could not find stream information\n"); return -1; }
for (int i = 0; i < fmt_ctx->nb_streams; i++) { if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } }
AVCodecParameters* codecpar = fmt_ctx->streams[video_stream_index]->codecpar; const AVCodec* codec = avcodec_find_decoder(codecpar->codec_id); if (!codec) { fprintf(stderr, "Codec not found\n"); return -1; } codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate video codec context\n"); return -1; } if (avcodec_parameters_to_context(codec_ctx, codecpar) < 0) { fprintf(stderr, "Could not copy codec parameters to codec context\n"); return -1; } if (avcodec_open2(codec_ctx, codec, NULL) < 0) { fprintf(stderr, "Could not open codec\n"); return -1; }
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, codec_ctx->width, codec_ctx->height);
SDL_CreateThread(video_play_thread, "video_thread", NULL);
SDL_Event event; while (1) { SDL_WaitEvent(&event); if (event.type == REFRESH_EVENT) { SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); } else if (event.type == SDL_QUIT) { break; } }
avformat_close_input(&fmt_ctx); avcodec_free_context(&codec_ctx); SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit();
return 0; }
|