Ticket #3338: video.cpp

File video.cpp, 3.0 KB (added by romain145, 12 years ago)

Updated render

Line 
1#include "video.h"
2#include <string.h>
3#include "globals.h"
4extern "C"
5{
6 #include <libavcodec/avcodec.h>
7 #include <libavformat/avformat.h>
8 #include <libavutil/frame.h>
9}
10#define CODEC_TYPE_VIDEO AVMEDIA_TYPE_VIDEO
11video::video(std::string filename,int x,int y,int width,int height,int zindex)
12{
13 av_register_all();
14 avcodec_register_all();
15 this->filename = filename;
16 running = false;
17 playb = false;
18 playonceb = false;
19 stopb = false;
20 pos.x = x;
21 pos.y = y;
22 pos.w = width;
23 pos.h = height;
24 this->type = 3;
25 this->zindex = zindex;
26
27
28}
29
30video::~video()
31{
32
33}
34
35void video::update()
36
37{
38 if ((playb==false & playonceb==false) & running)
39 {
40 clean();
41 }
42 if(visible){
43 if (playb | playonceb)
44 {
45 if (!running)
46 {
47 pFormatCtx = NULL;
48 pCodec = NULL;
49 pCodecCtx = NULL;
50 if (avformat_open_input(&pFormatCtx, filename.c_str(), NULL, NULL) != 0)
51 {
52 playb = false;
53 printf("file name error: %s\n",filename.c_str());
54 return;
55 }
56 avformat_find_stream_info(pFormatCtx, NULL);
57 av_dump_format(pFormatCtx, 0, filename.c_str(), 0);
58
59 for (unsigned int i = 0; i < pFormatCtx->nb_streams; ++i)
60 {
61 if (!pCodecCtx && pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
62 {
63 pCodecCtx = pFormatCtx->streams[i]->codec;
64 videostream = i;
65 }
66 }
67 pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
68 avcodec_open2(pCodecCtx, pCodec, NULL);
69 running = true;
70 }
71
72 AVPacket packet;
73
74 if(av_read_frame(pFormatCtx, &packet)<0)
75 {
76 av_free_packet(&packet);
77 printf("Could not read frame!\n");
78 if (playb) { avformat_seek_file(pFormatCtx,videostream,0,0,0,0); }
79 if (playonceb) {printf("PLAYONCEB stop request\n"); playonceb = false; clean();return;}
80 } //readframe
81
82 AVFrame* frame = avcodec_alloc_frame();
83 int frame_finished;
84 avcodec_decode_video2(pCodecCtx, frame, &frame_finished, &packet);
85
86 if (frame_finished)
87 {
88
89 SDL_Texture* bmp = SDL_CreateTexture(RENDER, SDL_PIXELFORMAT_YV12,SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width, pCodecCtx->height);
90 SDL_UpdateYUVTexture(bmp, NULL, frame->data[0], frame->linesize[0],frame->data[1], frame->linesize[1],frame->data[2], frame->linesize[2]);
91 SDL_RenderCopyEx(RENDER, bmp, NULL, &pos,0,NULL,SDL_FLIP_NONE);
92 SDL_DestroyTexture(bmp);
93
94 }//frame finished
95 av_free_packet(&packet);
96 av_free(frame);
97
98 }//if playb
99} //visible
100}
101
102
103void video::clean()
104{
105 printf("clean\n");
106 avcodec_close(pCodecCtx);
107 avcodec_close(pFormatCtx->streams[videostream]->codec);
108 if (pCodecCtx==NULL)
109 {
110 printf("(pCodecCtx) = NULL\n");
111 }
112 avformat_close_input(&pFormatCtx);
113 if (pFormatCtx==NULL)
114 {
115 printf("pFormatCtx = NULL\n");
116 }
117 running = false;
118}
119
120void video::playloop()
121{
122 playonceb = false;
123 playb = true;
124 visible = 1;
125}
126void video::playonce()
127{
128 playonceb = true;
129 playb = false;
130 visible = 1;
131}
132void video::stop()
133{
134 playonceb = false;
135 playb = false;
136 visible = 0;
137}
138bool video::getrunning()
139{
140 return running;
141}