stereoplay

annotate src/vid.c @ 4:acf3d25f23cb

added license headings
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 03 Nov 2011 21:57:12 +0200
parents fd671d488cfd
children 8fc045d33d62
rev   line source
nuclear@4 1 /*
nuclear@4 2 Stereoplay - an OpenGL stereoscopic video player.
nuclear@4 3 Copyright (C) 2011 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@4 18
nuclear@0 19 #include <stdio.h>
nuclear@0 20 #include <libavcodec/avcodec.h>
nuclear@0 21 #include <libavformat/avformat.h>
nuclear@0 22 #include <libswscale/swscale.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@0 52 if(av_open_input_file(&vf->avctx, fname, 0, 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@0 58 if(av_find_stream_info(vf->avctx) < 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@0 84 if(avcodec_open(vf->cctx, vf->codec) < 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@0 90 if(!(vf->frm = avcodec_alloc_frame()) || !(vf->rgbfrm = avcodec_alloc_frame())) {
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@0 97 vf->cctx->width, vf->cctx->height, 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@0 120 if(vf->avctx) av_close_input_file(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 AVPicture *rgbfrm = (AVPicture*)vf->rgbfrm;
nuclear@0 157 AVPicture *frm = (AVPicture*)vf->frm;
nuclear@0 158
nuclear@0 159 avpicture_fill((AVPicture*)vf->rgbfrm, (uint8_t*)img, PIX_FMT_RGB32, vf->cctx->width, vf->cctx->height);
nuclear@0 160
nuclear@0 161 while(av_read_frame(vf->avctx, &packet) >= 0) {
nuclear@0 162 if(packet.stream_index == vf->vstream) {
nuclear@0 163 avcodec_decode_video2(vf->cctx, vf->frm, &frame_done, &packet);
nuclear@0 164
nuclear@0 165 if(frame_done) {
nuclear@0 166 sws_scale(vf->sws, frm->data, frm->linesize, 0, vf->cctx->height, rgbfrm->data, rgbfrm->linesize);
nuclear@0 167 av_free_packet(&packet);
nuclear@0 168 return 0;
nuclear@0 169 }
nuclear@0 170 }
nuclear@0 171 av_free_packet(&packet);
nuclear@0 172 }
nuclear@0 173
nuclear@0 174 return -1;
nuclear@0 175 }