rev |
line source |
nuclear@4
|
1 /*
|
nuclear@4
|
2 Stereoplay - an OpenGL stereoscopic video player.
|
nuclear@6
|
3 Copyright (C) 2011-2020 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@4
|
4
|
nuclear@4
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@4
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@4
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@4
|
8 (at your option) any later version.
|
nuclear@4
|
9
|
nuclear@4
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@4
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@4
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@4
|
13 GNU General Public License for more details.
|
nuclear@4
|
14
|
nuclear@4
|
15 You should have received a copy of the GNU General Public License
|
nuclear@4
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@4
|
17 */
|
nuclear@0
|
18 #include <stdio.h>
|
nuclear@0
|
19 #include <libavcodec/avcodec.h>
|
nuclear@0
|
20 #include <libavformat/avformat.h>
|
nuclear@0
|
21 #include <libswscale/swscale.h>
|
nuclear@6
|
22 #include <libavutil/imgutils.h>
|
nuclear@0
|
23 #include "vid.h"
|
nuclear@0
|
24
|
nuclear@0
|
25 struct video_file {
|
nuclear@0
|
26 AVFormatContext *avctx;
|
nuclear@0
|
27 AVCodecContext *cctx;
|
nuclear@0
|
28 AVCodec *codec;
|
nuclear@0
|
29 int vstream, audio_stream;
|
nuclear@0
|
30 struct SwsContext *sws;
|
nuclear@0
|
31
|
nuclear@0
|
32 AVFrame *frm, *rgbfrm;
|
nuclear@0
|
33 };
|
nuclear@0
|
34
|
nuclear@0
|
35 struct video_file *vid_open(const char *fname)
|
nuclear@0
|
36 {
|
nuclear@0
|
37 static int initialized;
|
nuclear@0
|
38 struct video_file *vf;
|
nuclear@0
|
39 int i;
|
nuclear@0
|
40
|
nuclear@0
|
41 if(!initialized) {
|
nuclear@0
|
42 av_register_all();
|
nuclear@0
|
43 initialized = 1;
|
nuclear@0
|
44 }
|
nuclear@0
|
45
|
nuclear@0
|
46 if(!(vf = malloc(sizeof *vf))) {
|
nuclear@0
|
47 fprintf(stderr, "open_video(%s): failed to allocate memory: %s\n", fname, strerror(errno));
|
nuclear@0
|
48 return 0;
|
nuclear@0
|
49 }
|
nuclear@0
|
50 memset(vf, 0, sizeof *vf);
|
nuclear@0
|
51
|
nuclear@6
|
52 if(avformat_open_input(&vf->avctx, fname, 0, 0) != 0) {
|
nuclear@0
|
53 fprintf(stderr, "open_video(%s): failed to open file\n", fname);
|
nuclear@0
|
54 vid_close(vf);
|
nuclear@0
|
55 return 0;
|
nuclear@0
|
56 }
|
nuclear@0
|
57
|
nuclear@6
|
58 if(avformat_find_stream_info(vf->avctx, 0) < 0) {
|
nuclear@0
|
59 fprintf(stderr, "open_video(%s): failed to find stream info\n", fname);
|
nuclear@0
|
60 vid_close(vf);
|
nuclear@0
|
61 return 0;
|
nuclear@0
|
62 }
|
nuclear@0
|
63
|
nuclear@0
|
64 vf->vstream = -1;
|
nuclear@0
|
65 for(i=0; i<vf->avctx->nb_streams; i++) {
|
nuclear@2
|
66 if(vf->avctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
nuclear@0
|
67 vf->vstream = i;
|
nuclear@0
|
68 break;
|
nuclear@0
|
69 }
|
nuclear@0
|
70 }
|
nuclear@0
|
71 if(vf->vstream == -1) {
|
nuclear@0
|
72 fprintf(stderr, "open_video(%s): didn't find a video stream\n", fname);
|
nuclear@0
|
73 vid_close(vf);
|
nuclear@0
|
74 return 0;
|
nuclear@0
|
75 }
|
nuclear@0
|
76 vf->cctx = vf->avctx->streams[vf->vstream]->codec;
|
nuclear@0
|
77
|
nuclear@0
|
78 if(!(vf->codec = avcodec_find_decoder(vf->cctx->codec_id))) {
|
nuclear@0
|
79 fprintf(stderr, "open_video(%s): unsupported codec\n", fname);
|
nuclear@0
|
80 vid_close(vf);
|
nuclear@0
|
81 return 0;
|
nuclear@0
|
82 }
|
nuclear@0
|
83
|
nuclear@6
|
84 if(avcodec_open2(vf->cctx, vf->codec, 0) < 0) {
|
nuclear@0
|
85 fprintf(stderr, "open_video(%s): failed to open codec\n", fname);
|
nuclear@0
|
86 vid_close(vf);
|
nuclear@0
|
87 return 0;
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@6
|
90 if(!(vf->frm = av_frame_alloc()) || !(vf->rgbfrm = av_frame_alloc())) {
|
nuclear@0
|
91 fprintf(stderr, "open_video(%s): failed to allocate frame\n", fname);
|
nuclear@0
|
92 vid_close(vf);
|
nuclear@0
|
93 return 0;
|
nuclear@0
|
94 }
|
nuclear@0
|
95
|
nuclear@0
|
96 vf->sws = sws_getContext(vf->cctx->width, vf->cctx->height, vf->cctx->pix_fmt,
|
nuclear@6
|
97 vf->cctx->width, vf->cctx->height, AV_PIX_FMT_RGB32, SWS_POINT, 0, 0, 0);
|
nuclear@0
|
98 if(!vf->sws) {
|
nuclear@0
|
99 fprintf(stderr, "open_video(%s): failed to allocate sws context\n", fname);
|
nuclear@0
|
100 vid_close(vf);
|
nuclear@0
|
101 return 0;
|
nuclear@0
|
102 }
|
nuclear@0
|
103
|
nuclear@0
|
104
|
nuclear@0
|
105 printf("using codec: %s\n", vf->codec->name);
|
nuclear@0
|
106 printf("fps: %f (%u usec frame interval)\n", vid_fps(vf), vid_frame_interval(vf));
|
nuclear@0
|
107 printf("size: %dx%d\n", vid_frame_width(vf), vid_frame_height(vf));
|
nuclear@0
|
108
|
nuclear@0
|
109 return vf;
|
nuclear@0
|
110 }
|
nuclear@0
|
111
|
nuclear@0
|
112 void vid_close(struct video_file *vf)
|
nuclear@0
|
113 {
|
nuclear@0
|
114 if(!vf) return;
|
nuclear@0
|
115
|
nuclear@0
|
116 /* TODO how do we deallocate sws contexts? */
|
nuclear@0
|
117 if(vf->rgbfrm) av_free(vf->rgbfrm);
|
nuclear@0
|
118 if(vf->frm) av_free(vf->frm);
|
nuclear@0
|
119 if(vf->cctx) avcodec_close(vf->cctx);
|
nuclear@6
|
120 if(vf->avctx) avformat_close_input(&vf->avctx);
|
nuclear@0
|
121 free(vf);
|
nuclear@0
|
122 }
|
nuclear@0
|
123
|
nuclear@0
|
124
|
nuclear@0
|
125 float vid_fps(struct video_file *vf)
|
nuclear@0
|
126 {
|
nuclear@0
|
127 float inv_tb = (float)vf->cctx->time_base.den / (float)vf->cctx->time_base.num;
|
nuclear@0
|
128 return inv_tb / (float)vf->cctx->ticks_per_frame;
|
nuclear@0
|
129 }
|
nuclear@0
|
130
|
nuclear@0
|
131 unsigned int vid_frame_interval(struct video_file *vf)
|
nuclear@0
|
132 {
|
nuclear@0
|
133 float fps = vid_fps(vf);
|
nuclear@0
|
134 return 1000000 / fps;
|
nuclear@0
|
135 }
|
nuclear@0
|
136
|
nuclear@0
|
137 int vid_frame_width(struct video_file *vf)
|
nuclear@0
|
138 {
|
nuclear@0
|
139 return vf->cctx->width;
|
nuclear@0
|
140 }
|
nuclear@0
|
141
|
nuclear@0
|
142 int vid_frame_height(struct video_file *vf)
|
nuclear@0
|
143 {
|
nuclear@0
|
144 return vf->cctx->height;
|
nuclear@0
|
145 }
|
nuclear@0
|
146
|
nuclear@0
|
147 size_t vid_frame_size(struct video_file *vf)
|
nuclear@0
|
148 {
|
nuclear@0
|
149 return vf->cctx->width * vf->cctx->height * 4;
|
nuclear@0
|
150 }
|
nuclear@0
|
151
|
nuclear@0
|
152 int vid_get_frame(struct video_file *vf, uint32_t *img)
|
nuclear@0
|
153 {
|
nuclear@0
|
154 AVPacket packet;
|
nuclear@0
|
155 int frame_done = 0;
|
nuclear@0
|
156
|
nuclear@6
|
157 av_image_fill_arrays(vf->rgbfrm->data, vf->rgbfrm->linesize, (unsigned char*)img,
|
nuclear@6
|
158 AV_PIX_FMT_RGB32, vf->cctx->width, vf->cctx->height, 1);
|
nuclear@0
|
159
|
nuclear@0
|
160 while(av_read_frame(vf->avctx, &packet) >= 0) {
|
nuclear@0
|
161 if(packet.stream_index == vf->vstream) {
|
nuclear@0
|
162 avcodec_decode_video2(vf->cctx, vf->frm, &frame_done, &packet);
|
nuclear@0
|
163
|
nuclear@0
|
164 if(frame_done) {
|
nuclear@6
|
165 sws_scale(vf->sws, vf->frm->data, vf->frm->linesize, 0, vf->cctx->height,
|
nuclear@6
|
166 vf->rgbfrm->data, vf->rgbfrm->linesize);
|
nuclear@6
|
167 av_packet_unref(&packet);
|
nuclear@0
|
168 return 0;
|
nuclear@0
|
169 }
|
nuclear@0
|
170 }
|
nuclear@6
|
171 av_packet_unref(&packet);
|
nuclear@0
|
172 }
|
nuclear@0
|
173
|
nuclear@0
|
174 return -1;
|
nuclear@0
|
175 }
|