source: mediastreamer2/linphone/mediastreamer2/src/nowebcam.c @ 143:00dddccb0111

Last change on this file since 143:00dddccb0111 was 143:00dddccb0111, checked in by aymeric <aymeric@…>, 5 years ago

make SET_IMAGE usable by all video source filter

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

File size: 6.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        char nowebcamimage[256];
119        int index;
120        uint64_t lasttime;
121        mblk_t *pic;
122}SIData;
123
124void static_image_init(MSFilter *f){
125        SIData *d=(SIData*)ms_new(SIData,1);
126        d->vsize.width=MS_VIDEO_SIZE_CIF_W;
127        d->vsize.height=MS_VIDEO_SIZE_CIF_H;
128        memset(d->nowebcamimage, 0, sizeof(d->nowebcamimage));
129        d->index=-1;
130        d->lasttime=0;
131        d->pic=NULL;
132        f->data=d;
133}
134
135void static_image_uninit(MSFilter *f){
136        ms_free(f->data);
137}
138
139void static_image_preprocess(MSFilter *f){
140        SIData *d=(SIData*)f->data;
141  if (d->pic==NULL)
142  {
143    if (d->nowebcamimage[0] != '\0')
144          d->pic=ms_load_jpeg_as_yuv(d->nowebcamimage,&d->vsize);
145    else
146          d->pic=ms_load_nowebcam(&d->vsize,d->index);
147  }
148}
149
150void static_image_process(MSFilter *f){
151        SIData *d=(SIData*)f->data;
152        /*output a frame every second*/
153        if ((f->ticker->time - d->lasttime>1000) || d->lasttime==0){
154    ms_mutex_lock(&f->lock);
155                if (d->pic) {
156                        mblk_t *o=dupb(d->pic);
157                        /*prevent mirroring at the output*/
158                        mblk_set_precious_flag(o,1);
159                        ms_queue_put(f->outputs[0],o);
160                }
161    ms_mutex_unlock(&f->lock);
162                d->lasttime=f->ticker->time;
163        }
164}
165
166void static_image_postprocess(MSFilter *f){
167        SIData *d=(SIData*)f->data;
168        if (d->pic) {
169                freemsg(d->pic);
170                d->pic=NULL;
171        }
172}
173
174int static_image_set_vsize(MSFilter *f, void* data){
175        SIData *d=(SIData*)f->data;
176        d->vsize=*(MSVideoSize*)data;
177        return 0;
178}
179
180int static_image_get_vsize(MSFilter *f, void* data){
181        SIData *d=(SIData*)f->data;
182        *(MSVideoSize*)data=d->vsize;
183        return 0;
184}
185
186int static_image_get_pix_fmt(MSFilter *f, void *data){
187        *(MSPixFmt*)data=MS_YUV420P;
188        return 0;
189}
190
191static int static_image_set_image(MSFilter *f, void *arg){
192        SIData *d=(SIData*)f->data;
193        char *image = (char *)arg;
194  ms_mutex_lock(&f->lock);
195        if (image!=NULL && image[0]!='\0')
196          snprintf(d->nowebcamimage, sizeof(d->nowebcamimage), "%s", image);
197        else
198          d->nowebcamimage[0] = '\0';
199
200  if (d->pic!=NULL)
201                freemsg(d->pic);
202
203  //if (d->nowebcamimage[0] != '\0')
204         // d->pic=ms_load_jpeg_as_yuv(d->nowebcamimage,&d->vsize);
205  //else
206         // d->pic=ms_load_nowebcam(&d->vsize,d->index);
207  ms_mutex_unlock(&f->lock);
208        return 0;
209}
210
211MSFilterMethod static_image_methods[]={
212        {       MS_FILTER_SET_VIDEO_SIZE, static_image_set_vsize },
213        {       MS_FILTER_GET_VIDEO_SIZE, static_image_get_vsize },
214        {       MS_FILTER_GET_PIX_FMT, static_image_get_pix_fmt },
215        {       MS_FILTER_SET_IMAGE, static_image_set_image },
216        {       0,0 }
217};
218
219MSFilterDesc ms_static_image_desc={
220        MS_STATIC_IMAGE_ID,
221        "MSStaticImage",
222        "A filter that outputs a static image.",
223        MS_FILTER_OTHER,
224        NULL,
225        0,
226        1,
227        static_image_init,
228        static_image_preprocess,
229        static_image_process,
230        static_image_postprocess,
231        static_image_uninit,
232        static_image_methods
233};
234
235MS_FILTER_DESC_EXPORT(ms_static_image_desc)
236
237static void static_image_detect(MSWebCamManager *obj);
238
239static void static_image_cam_init(MSWebCam *cam){
240        cam->name=ms_strdup("Static picture");
241}
242
243
244static MSFilter *static_image_create_reader(MSWebCam *obj){
245        return ms_filter_new_from_desc(&ms_static_image_desc);
246}
247
248MSWebCamDesc static_image_desc={
249        "StaticImage",
250        &static_image_detect,
251        &static_image_cam_init,
252        &static_image_create_reader,
253        NULL
254};
255
256static void static_image_detect(MSWebCamManager *obj){
257        MSWebCam *cam=ms_web_cam_new(&static_image_desc);
258        ms_web_cam_manager_add_cam(obj,cam);
259}
260
Note: See TracBrowser for help on using the repository browser.