source: mediastreamer2/linphone/mediastreamer2/src/nowebcam.c @ 55:76c9c25985d2

Last change on this file since 55:76c9c25985d2 was 55:76c9c25985d2, checked in by smorlat <smorlat@…>, 5 years ago

prevent mirroring of no webcam picture.

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@58 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

File size: 5.5 KB
Line 
1/*
2mediastreamer2 library - modular sound and video processing and streaming
3Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*/
19
20#ifdef HAVE_CONFIG_H
21#include "mediastreamer-config.h"
22#endif
23
24#include "mediastreamer2/mscommon.h"
25#include "mediastreamer2/msvideo.h"
26#include "mediastreamer2/msfilter.h"
27#include "mediastreamer2/msticker.h"
28#include "mediastreamer2/mswebcam.h"
29
30#include "ffmpeg-priv.h"
31
32#include <sys/stat.h>
33
34#ifdef WIN32
35#include <fcntl.h>
36#include <sys/types.h>
37#include <io.h>
38#include <stdio.h>
39#include <malloc.h>
40#endif
41
42static mblk_t *jpeg2yuv(uint8_t *jpgbuf, int bufsize, MSVideoSize *reqsize){
43        AVCodecContext av_context;
44        int got_picture=0;
45        AVFrame orig;
46        AVPicture dest;
47        mblk_t *ret;
48        struct SwsContext *sws_ctx;
49
50        avcodec_get_context_defaults(&av_context);
51        if (avcodec_open(&av_context,avcodec_find_decoder(CODEC_ID_MJPEG))<0){
52                ms_error("jpeg2yuv: avcodec_open failed");
53                return NULL;
54        }
55        if (avcodec_decode_video(&av_context,&orig,&got_picture,jpgbuf,bufsize)<0){
56                ms_error("jpeg2yuv: avcodec_decode_video failed");
57                avcodec_close(&av_context);
58                return NULL;
59        }
60        ret=allocb(avpicture_get_size(PIX_FMT_YUV420P,reqsize->width,reqsize->height),0);
61        ret->b_wptr=ret->b_datap->db_lim;
62        avpicture_fill(&dest,ret->b_rptr,PIX_FMT_YUV420P,reqsize->width,reqsize->height);
63       
64        sws_ctx=sws_getContext(av_context.width,av_context.height,PIX_FMT_YUV420P,
65                reqsize->width,reqsize->height,PIX_FMT_YUV420P,SWS_FAST_BILINEAR,
66                NULL, NULL, NULL);
67        if (sws_scale(sws_ctx,orig.data,orig.linesize,0,av_context.height,dest.data,dest.linesize)<0){
68                ms_error("jpeg2yuv: sws_scale() failed.");
69        }
70        sws_freeContext(sws_ctx);
71        avcodec_close(&av_context);
72        return ret;
73}
74
75mblk_t *ms_load_jpeg_as_yuv(const char *jpgpath, MSVideoSize *reqsize){
76        mblk_t *m=NULL;
77        struct stat statbuf;
78        uint8_t *jpgbuf;
79#if !defined(_MSC_VER)
80        int fd=open(jpgpath,O_RDONLY);
81#else
82        int fd=_open(jpgpath,O_RDONLY);
83#endif
84        if (fd!=-1){
85                fstat(fd,&statbuf);
86                jpgbuf=(uint8_t*)alloca(statbuf.st_size);
87#if !defined(_MSC_VER)
88                read(fd,jpgbuf,statbuf.st_size);
89#else
90                _read(fd,jpgbuf,statbuf.st_size);
91#endif
92                m=jpeg2yuv(jpgbuf,statbuf.st_size,reqsize);
93        }else{
94                ms_error("Cannot load %s",jpgpath);
95        }
96        return m;
97}
98
99#ifndef PACKAGE_DATA_DIR
100#define PACKAGE_DATA_DIR "."
101#endif
102
103#ifndef NOWEBCAM_JPG
104#define NOWEBCAM_JPG "nowebcamCIF"
105#endif
106
107mblk_t *ms_load_nowebcam(MSVideoSize *reqsize, int idx){
108        char tmp[256];
109        if (idx<0)
110                snprintf(tmp, sizeof(tmp), "%s/images/%s.jpg", PACKAGE_DATA_DIR, NOWEBCAM_JPG);
111        else
112                snprintf(tmp, sizeof(tmp), "%s/images/%s%i.jpg", PACKAGE_DATA_DIR, NOWEBCAM_JPG, idx);
113        return ms_load_jpeg_as_yuv(tmp,reqsize);
114}
115
116typedef struct _SIData{
117        MSVideoSize vsize;
118        int index;
119        uint64_t lasttime;
120        mblk_t *pic;
121}SIData;
122
123void static_image_init(MSFilter *f){
124        SIData *d=(SIData*)ms_new(SIData,1);
125        d->vsize.width=MS_VIDEO_SIZE_CIF_W;
126        d->vsize.height=MS_VIDEO_SIZE_CIF_H;
127        d->index=-1;
128        d->lasttime=0;
129        d->pic=NULL;
130        f->data=d;
131}
132
133void static_image_uninit(MSFilter *f){
134        ms_free(f->data);
135}
136
137void static_image_preprocess(MSFilter *f){
138        SIData *d=(SIData*)f->data;
139        d->pic=ms_load_nowebcam(&d->vsize,d->index);
140}
141
142void static_image_process(MSFilter *f){
143        SIData *d=(SIData*)f->data;
144        /*output a frame every second*/
145        if ((f->ticker->time - d->lasttime>1000) || d->lasttime==0){
146                if (d->pic) {
147                        mblk_t *o=dupb(d->pic);
148                        /*prevent mirroring at the output*/
149                        mblk_set_precious_flag(o,1);
150                        ms_queue_put(f->outputs[0],o);
151                }
152                d->lasttime=f->ticker->time;
153        }
154}
155
156void static_image_postprocess(MSFilter *f){
157        SIData *d=(SIData*)f->data;
158        if (d->pic) {
159                freemsg(d->pic);
160                d->pic=NULL;
161        }
162}
163
164int static_image_set_vsize(MSFilter *f, void* data){
165        SIData *d=(SIData*)f->data;
166        d->vsize=*(MSVideoSize*)data;
167        return 0;
168}
169
170int static_image_get_pix_fmt(MSFilter *f, void *data){
171        *(MSPixFmt*)data=MS_YUV420P;
172        return 0;
173}
174
175MSFilterMethod static_image_methods[]={
176        {       MS_FILTER_SET_VIDEO_SIZE, static_image_set_vsize },
177        {       MS_FILTER_GET_PIX_FMT, static_image_get_pix_fmt },
178        {       0,0 }
179};
180
181MSFilterDesc ms_static_image_desc={
182        MS_STATIC_IMAGE_ID,
183        "MSStaticImage",
184        "A filter that outputs a static image.",
185        MS_FILTER_OTHER,
186        NULL,
187        0,
188        1,
189        static_image_init,
190        static_image_preprocess,
191        static_image_process,
192        static_image_postprocess,
193        static_image_uninit,
194        static_image_methods
195};
196
197MS_FILTER_DESC_EXPORT(ms_static_image_desc)
198
199static void static_image_detect(MSWebCamManager *obj);
200
201static void static_image_cam_init(MSWebCam *cam){
202        cam->name=ms_strdup("Static picture");
203}
204
205
206static MSFilter *static_image_create_reader(MSWebCam *obj){
207        return ms_filter_new_from_desc(&ms_static_image_desc);
208}
209
210MSWebCamDesc static_image_desc={
211        "StaticImage",
212        &static_image_detect,
213        &static_image_cam_init,
214        &static_image_create_reader,
215        NULL
216};
217
218static void static_image_detect(MSWebCamManager *obj){
219        MSWebCam *cam=ms_web_cam_new(&static_image_desc);
220        ms_web_cam_manager_add_cam(obj,cam);
221}
222
Note: See TracBrowser for help on using the repository browser.