stereoplay

view src/vid.c @ 6:8fc045d33d62

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