~ubuntu-branches/ubuntu/vivid/linphone/vivid

« back to all changes in this revision

Viewing changes to mediastreamer2/src/sizeconv.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-11-15 10:34:50 UTC
  • mfrom: (1.2.1 upstream) (2.1.8 feisty)
  • Revision ID: james.westby@ubuntu.com-20061115103450-qgafwcks2lkhctlj
* New upstream release.
* Enable video support.
* Fix mismatched #endif in mscommon.h, closes: #398307.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
mediastreamer2 library - modular sound and video processing and streaming
 
3
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)
 
4
 
 
5
This program is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU General Public License
 
7
as published by the Free Software Foundation; either version 2
 
8
of the License, or (at your option) any later version.
 
9
 
 
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.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
*/
 
19
 
 
20
#include "mediastreamer2/msfilter.h"
 
21
#include "mediastreamer2/msvideo.h"
 
22
 
 
23
#include <ffmpeg/avcodec.h>
 
24
 
 
25
typedef struct SizeConvState{
 
26
        MSVideoSize target_vsize;
 
27
        MSVideoSize input_vsize;
 
28
        ImgReSampleContext *rsctx;
 
29
        mblk_t *om;
 
30
        int in_size;
 
31
        int out_size;
 
32
} SizeConvState;
 
33
 
 
34
static int cif_yuv_size=0;
 
35
static int qcif_yuv_size=0;
 
36
static int qvga_yuv_size=0;
 
37
 
 
38
/*this MSFilter will do on the fly picture size conversion. It attempts to guess the picture size from the yuv buffer size. YUV420P is assumed on input.
 
39
For now it only supports QCIF->CIF, QVGA->CIF and CIF->CIF (does nothing in this case)*/
 
40
 
 
41
static void size_conv_init(MSFilter *f){
 
42
        SizeConvState *s=ms_new(SizeConvState,1);
 
43
        s->target_vsize.width = MS_VIDEO_SIZE_CIF_W;
 
44
        s->target_vsize.height = MS_VIDEO_SIZE_CIF_H;
 
45
        s->input_vsize.width = MS_VIDEO_SIZE_CIF_W;
 
46
        s->input_vsize.height = MS_VIDEO_SIZE_CIF_H;
 
47
        s->rsctx=NULL;
 
48
        s->om=NULL;
 
49
        s->in_size=0;
 
50
        s->out_size=avpicture_get_size(PIX_FMT_YUV420P,s->target_vsize.width,s->target_vsize.height);
 
51
        f->data=s;
 
52
        if (cif_yuv_size==0){
 
53
                cif_yuv_size=avpicture_get_size(PIX_FMT_YUV420P,MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H);
 
54
                qcif_yuv_size=avpicture_get_size(PIX_FMT_YUV420P,MS_VIDEO_SIZE_QCIF_W,MS_VIDEO_SIZE_QCIF_H);
 
55
                qvga_yuv_size=avpicture_get_size(PIX_FMT_YUV420P,MS_VIDEO_SIZE_QVGA_W,MS_VIDEO_SIZE_QVGA_H);
 
56
        }
 
57
}
 
58
 
 
59
static void size_conv_uninit(MSFilter *f){
 
60
        SizeConvState *s=(SizeConvState*)f->data;
 
61
        freemsg(s->om);
 
62
        ms_free(s);
 
63
}
 
64
 
 
65
static void size_conv_postprocess(MSFilter *f){
 
66
        SizeConvState *s=(SizeConvState*)f->data;
 
67
        if (s->rsctx!=NULL) {
 
68
                img_resample_close(s->rsctx);
 
69
                s->rsctx=NULL;
 
70
        }
 
71
}
 
72
 
 
73
static mblk_t * size_conv_alloc_mblk(SizeConvState *s){
 
74
        if (s->om!=NULL){
 
75
                int ref=s->om->b_datap->db_ref;
 
76
                if (ref==1){
 
77
                        return dupmsg(s->om);
 
78
                }else{
 
79
                        /*the last msg is still referenced by somebody else*/
 
80
                        ms_message("size_conv_alloc_mblk: Somebody still retaining yuv buffer (ref=%i)",ref);
 
81
                        freemsg(s->om);
 
82
                        s->om=NULL;
 
83
                }
 
84
        }
 
85
        s->om=allocb(s->out_size,0);
 
86
        s->om->b_wptr=s->om->b_datap->db_lim;
 
87
        return dupmsg(s->om);
 
88
}
 
89
 
 
90
static void init_resampler(SizeConvState *s, int size){
 
91
        if (s->rsctx!=NULL){
 
92
                img_resample_close(s->rsctx);
 
93
                s->rsctx=NULL;
 
94
        }
 
95
        s->in_size=size;
 
96
        if (size!=s->out_size){
 
97
                if (size==cif_yuv_size){
 
98
                        s->input_vsize.width=MS_VIDEO_SIZE_CIF_W;
 
99
                        s->input_vsize.height=MS_VIDEO_SIZE_CIF_H;
 
100
                }else if (size==qcif_yuv_size){
 
101
                        s->input_vsize.width=MS_VIDEO_SIZE_QCIF_W;
 
102
                        s->input_vsize.height=MS_VIDEO_SIZE_QCIF_H;
 
103
                }else if (size==qvga_yuv_size){
 
104
                        s->input_vsize.width=MS_VIDEO_SIZE_QVGA_W;
 
105
                        s->input_vsize.height=MS_VIDEO_SIZE_QVGA_H;
 
106
                }else{
 
107
                        ms_error("Unsupported image size !");
 
108
                        return;
 
109
                }
 
110
                s->rsctx=img_resample_init(s->target_vsize.width,s->target_vsize.height,s->input_vsize.width,s->input_vsize.height);
 
111
        }
 
112
}
 
113
 
 
114
static void size_conv_process(MSFilter *f){
 
115
        SizeConvState *s=(SizeConvState*)f->data;
 
116
        mblk_t *im,*om;
 
117
        int sz;
 
118
        AVPicture orig,dest;
 
119
        while((im=ms_queue_get(f->inputs[0]))!=NULL ){
 
120
                sz=msgdsize(im);
 
121
                if (sz!=s->in_size){
 
122
                        init_resampler(s,sz);
 
123
                }
 
124
                if (s->rsctx!=NULL){
 
125
                        avpicture_fill(&orig,(unsigned char *)im->b_rptr,PIX_FMT_YUV420P,s->input_vsize.width,s->input_vsize.height);
 
126
                        om=size_conv_alloc_mblk(s);
 
127
                        avpicture_fill(&dest,(uint8_t*)om->b_rptr,PIX_FMT_YUV420P,s->target_vsize.width,s->target_vsize.height);
 
128
                        img_resample(s->rsctx,&dest,&orig);
 
129
                        freemsg(im);
 
130
                        ms_queue_put(f->outputs[0],om);
 
131
                }else{
 
132
                        ms_queue_put(f->outputs[0],im);
 
133
                }
 
134
        }
 
135
}
 
136
 
 
137
#ifdef _MSC_VER
 
138
 
 
139
MSFilterDesc ms_size_conv_desc={
 
140
        MS_SIZE_CONV_ID,
 
141
        "MSSizeConv",
 
142
        "a small video size converter",
 
143
        MS_FILTER_OTHER,
 
144
        NULL,
 
145
        1,
 
146
        1,
 
147
        size_conv_init,
 
148
        NULL,
 
149
        size_conv_process,
 
150
        size_conv_postprocess,
 
151
        size_conv_uninit,
 
152
        NULL
 
153
};
 
154
 
 
155
#else
 
156
 
 
157
MSFilterDesc ms_size_conv_desc={
 
158
        .id=MS_SIZE_CONV_ID,
 
159
        .name="MSSizeConv",
 
160
        .text="a small video size converter",
 
161
        .ninputs=1,
 
162
        .noutputs=1,
 
163
        .init=size_conv_init,
 
164
        .process=size_conv_process,
 
165
        .postprocess=size_conv_postprocess,
 
166
        .uninit=size_conv_uninit
 
167
};
 
168
 
 
169
#endif
 
170
 
 
171
MS_FILTER_DESC_EXPORT(ms_size_conv_desc)
 
172