~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.2.1/pjmedia/src/pjmedia/format.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: format.c 4785 2014-03-10 09:01:18Z nanang $ */
 
2
/*
 
3
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 
4
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
#include <pjmedia/format.h>
 
21
#include <pj/assert.h>
 
22
#include <pj/errno.h>
 
23
#include <pj/pool.h>
 
24
#include <pj/string.h>
 
25
 
 
26
 
 
27
PJ_DEF(pjmedia_audio_format_detail*)
 
28
pjmedia_format_get_audio_format_detail(const pjmedia_format *fmt,
 
29
                                       pj_bool_t assert_valid)
 
30
{
 
31
    if (fmt->detail_type==PJMEDIA_FORMAT_DETAIL_AUDIO) {
 
32
        return (pjmedia_audio_format_detail*) &fmt->det.aud;
 
33
    } else {
 
34
        /* Get rid of unused var compiler warning if pj_assert()
 
35
         * macro does not do anything
 
36
         */
 
37
        PJ_UNUSED_ARG(assert_valid);
 
38
        pj_assert(!assert_valid || !"Invalid audio format detail");
 
39
        return NULL;
 
40
    }
 
41
}
 
42
 
 
43
 
 
44
PJ_DEF(pjmedia_format*) pjmedia_format_copy(pjmedia_format *dst,
 
45
                                            const pjmedia_format *src)
 
46
{
 
47
    return (pjmedia_format*)pj_memcpy(dst, src, sizeof(*src));
 
48
}
 
49
 
 
50
 
 
51
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
 
52
 
 
53
 
 
54
static pj_status_t apply_packed_fmt(const pjmedia_video_format_info *fi,
 
55
                                    pjmedia_video_apply_fmt_param *aparam);
 
56
 
 
57
static pj_status_t apply_planar_420(const pjmedia_video_format_info *fi,
 
58
                                    pjmedia_video_apply_fmt_param *aparam);
 
59
 
 
60
static pj_status_t apply_planar_422(const pjmedia_video_format_info *fi,
 
61
                                    pjmedia_video_apply_fmt_param *aparam);
 
62
 
 
63
static pj_status_t apply_planar_444(const pjmedia_video_format_info *fi,
 
64
                                    pjmedia_video_apply_fmt_param *aparam);
 
65
 
 
66
struct pjmedia_video_format_mgr
 
67
{
 
68
    unsigned                    max_info;
 
69
    unsigned                    info_cnt;
 
70
    pjmedia_video_format_info **infos;
 
71
};
 
72
 
 
73
static pjmedia_video_format_mgr *video_format_mgr_instance;
 
74
static pjmedia_video_format_info built_in_vid_fmt_info[] =
 
75
{
 
76
    {PJMEDIA_FORMAT_RGB24, "RGB24", PJMEDIA_COLOR_MODEL_RGB, 24, 1, &apply_packed_fmt},
 
77
    {PJMEDIA_FORMAT_RGBA,  "RGBA", PJMEDIA_COLOR_MODEL_RGB, 32, 1, &apply_packed_fmt},
 
78
    {PJMEDIA_FORMAT_BGRA,  "BGRA", PJMEDIA_COLOR_MODEL_RGB, 32, 1, &apply_packed_fmt},
 
79
    {PJMEDIA_FORMAT_DIB ,  "DIB ", PJMEDIA_COLOR_MODEL_RGB, 24, 1, &apply_packed_fmt},
 
80
    {PJMEDIA_FORMAT_GBRP,  "GBRP", PJMEDIA_COLOR_MODEL_RGB, 24, 3, &apply_planar_444},
 
81
    {PJMEDIA_FORMAT_AYUV,  "AYUV", PJMEDIA_COLOR_MODEL_YUV, 32, 1, &apply_packed_fmt},
 
82
    {PJMEDIA_FORMAT_YUY2,  "YUY2", PJMEDIA_COLOR_MODEL_YUV, 16, 1, &apply_packed_fmt},
 
83
    {PJMEDIA_FORMAT_UYVY,  "UYVY", PJMEDIA_COLOR_MODEL_YUV, 16, 1, &apply_packed_fmt},
 
84
    {PJMEDIA_FORMAT_YVYU,  "YVYU", PJMEDIA_COLOR_MODEL_YUV, 16, 1, &apply_packed_fmt},
 
85
    {PJMEDIA_FORMAT_I420,  "I420", PJMEDIA_COLOR_MODEL_YUV, 12, 3, &apply_planar_420},
 
86
    {PJMEDIA_FORMAT_YV12,  "YV12", PJMEDIA_COLOR_MODEL_YUV, 12, 3, &apply_planar_420},
 
87
    {PJMEDIA_FORMAT_I422,  "I422", PJMEDIA_COLOR_MODEL_YUV, 16, 3, &apply_planar_422},
 
88
    {PJMEDIA_FORMAT_I420JPEG, "I420JPG", PJMEDIA_COLOR_MODEL_YUV, 12, 3, &apply_planar_420},
 
89
    {PJMEDIA_FORMAT_I422JPEG, "I422JPG", PJMEDIA_COLOR_MODEL_YUV, 16, 3, &apply_planar_422},
 
90
};
 
91
 
 
92
PJ_DEF(void) pjmedia_format_init_video( pjmedia_format *fmt,
 
93
                                        pj_uint32_t fmt_id,
 
94
                                        unsigned width,
 
95
                                        unsigned height,
 
96
                                        unsigned fps_num,
 
97
                                        unsigned fps_denum)
 
98
{
 
99
    pj_assert(fps_denum);
 
100
    fmt->id = fmt_id;
 
101
    fmt->type = PJMEDIA_TYPE_VIDEO;
 
102
    fmt->detail_type = PJMEDIA_FORMAT_DETAIL_VIDEO;
 
103
 
 
104
    fmt->det.vid.size.w = width;
 
105
    fmt->det.vid.size.h = height;
 
106
    fmt->det.vid.fps.num = fps_num;
 
107
    fmt->det.vid.fps.denum = fps_denum;
 
108
    fmt->det.vid.avg_bps = fmt->det.vid.max_bps = 0;
 
109
 
 
110
    if (pjmedia_video_format_mgr_instance()) {
 
111
        const pjmedia_video_format_info *vfi;
 
112
        pjmedia_video_apply_fmt_param vafp;
 
113
        pj_uint32_t bps;
 
114
 
 
115
        vfi = pjmedia_get_video_format_info(NULL, fmt->id);
 
116
        if (vfi) {
 
117
            pj_bzero(&vafp, sizeof(vafp));
 
118
            vafp.size = fmt->det.vid.size;
 
119
            vfi->apply_fmt(vfi, &vafp);
 
120
 
 
121
            bps = (pj_uint32_t)vafp.framebytes * fps_num * (pj_size_t)8 / fps_denum;
 
122
            fmt->det.vid.avg_bps = fmt->det.vid.max_bps = bps;
 
123
        }
 
124
    }
 
125
}
 
126
 
 
127
PJ_DEF(pjmedia_video_format_detail*)
 
128
pjmedia_format_get_video_format_detail(const pjmedia_format *fmt,
 
129
                                       pj_bool_t assert_valid)
 
130
{
 
131
    if (fmt->detail_type==PJMEDIA_FORMAT_DETAIL_VIDEO) {
 
132
        return (pjmedia_video_format_detail*)&fmt->det.vid;
 
133
    } else {
 
134
        pj_assert(!assert_valid || !"Invalid video format detail");
 
135
        return NULL;
 
136
    }
 
137
}
 
138
 
 
139
 
 
140
static pj_status_t apply_packed_fmt(const pjmedia_video_format_info *fi,
 
141
                                    pjmedia_video_apply_fmt_param *aparam)
 
142
{
 
143
    unsigned i;
 
144
    pj_size_t stride;
 
145
 
 
146
    stride = (pj_size_t)((aparam->size.w*fi->bpp) >> 3);
 
147
 
 
148
    /* Calculate memsize */
 
149
    aparam->framebytes = stride * aparam->size.h;
 
150
 
 
151
    /* Packed formats only use 1 plane */
 
152
    aparam->planes[0] = aparam->buffer;
 
153
    aparam->strides[0] = (int)stride;
 
154
    aparam->plane_bytes[0] = aparam->framebytes;
 
155
 
 
156
    /* Zero unused planes */
 
157
    for (i=1; i<PJMEDIA_MAX_VIDEO_PLANES; ++i) {
 
158
        aparam->strides[i] = 0;
 
159
        aparam->planes[i] = NULL;
 
160
    }
 
161
 
 
162
    return PJ_SUCCESS;
 
163
}
 
164
 
 
165
static pj_status_t apply_planar_420(const pjmedia_video_format_info *fi,
 
166
                                     pjmedia_video_apply_fmt_param *aparam)
 
167
{
 
168
    unsigned i;
 
169
    pj_size_t Y_bytes;
 
170
 
 
171
    PJ_UNUSED_ARG(fi);
 
172
 
 
173
    /* Calculate memsize */
 
174
    Y_bytes = (pj_size_t)(aparam->size.w * aparam->size.h);
 
175
    aparam->framebytes = Y_bytes + (Y_bytes>>1);
 
176
 
 
177
    /* Planar formats use 3 plane */
 
178
    aparam->strides[0] = aparam->size.w;
 
179
    aparam->strides[1] = aparam->strides[2] = (aparam->size.w>>1);
 
180
 
 
181
    aparam->planes[0] = aparam->buffer;
 
182
    aparam->planes[1] = aparam->planes[0] + Y_bytes;
 
183
    aparam->planes[2] = aparam->planes[1] + (Y_bytes>>2);
 
184
 
 
185
    aparam->plane_bytes[0] = Y_bytes;
 
186
    aparam->plane_bytes[1] = aparam->plane_bytes[2] = (Y_bytes>>2);
 
187
 
 
188
    /* Zero unused planes */
 
189
    for (i=3; i<PJMEDIA_MAX_VIDEO_PLANES; ++i) {
 
190
        aparam->strides[i] = 0;
 
191
        aparam->planes[i] = NULL;
 
192
        aparam->plane_bytes[i] = 0;
 
193
    }
 
194
 
 
195
    return PJ_SUCCESS;
 
196
}
 
197
 
 
198
static pj_status_t apply_planar_422(const pjmedia_video_format_info *fi,
 
199
                                     pjmedia_video_apply_fmt_param *aparam)
 
200
{
 
201
    unsigned i;
 
202
    pj_size_t Y_bytes;
 
203
 
 
204
    PJ_UNUSED_ARG(fi);
 
205
 
 
206
    /* Calculate memsize */
 
207
    Y_bytes = (pj_size_t)(aparam->size.w * aparam->size.h);
 
208
    aparam->framebytes = (Y_bytes << 1);
 
209
 
 
210
    /* Planar formats use 3 plane */
 
211
    aparam->strides[0] = aparam->size.w;
 
212
    aparam->strides[1] = aparam->strides[2] = (aparam->size.w>>1);
 
213
 
 
214
    aparam->planes[0] = aparam->buffer;
 
215
    aparam->planes[1] = aparam->planes[0] + Y_bytes;
 
216
    aparam->planes[2] = aparam->planes[1] + (Y_bytes>>1);
 
217
 
 
218
    aparam->plane_bytes[0] = Y_bytes;
 
219
    aparam->plane_bytes[1] = aparam->plane_bytes[2] = (Y_bytes>>1);
 
220
 
 
221
    /* Zero unused planes */
 
222
    for (i=3; i<PJMEDIA_MAX_VIDEO_PLANES; ++i) {
 
223
        aparam->strides[i] = 0;
 
224
        aparam->planes[i] = NULL;
 
225
        aparam->plane_bytes[i] = 0;
 
226
    }
 
227
 
 
228
    return PJ_SUCCESS;
 
229
}
 
230
 
 
231
static pj_status_t apply_planar_444(const pjmedia_video_format_info *fi,
 
232
                                    pjmedia_video_apply_fmt_param *aparam)
 
233
{
 
234
    unsigned i;
 
235
    pj_size_t Y_bytes;
 
236
 
 
237
    PJ_UNUSED_ARG(fi);
 
238
 
 
239
    /* Calculate memsize */
 
240
    Y_bytes = (pj_size_t)(aparam->size.w * aparam->size.h);
 
241
    aparam->framebytes = (Y_bytes * 3);
 
242
 
 
243
    /* Planar formats use 3 plane */
 
244
    aparam->strides[0] = aparam->strides[1] = 
 
245
                         aparam->strides[2] = aparam->size.w;
 
246
 
 
247
    aparam->planes[0] = aparam->buffer;
 
248
    aparam->planes[1] = aparam->planes[0] + Y_bytes;
 
249
    aparam->planes[2] = aparam->planes[1] + Y_bytes;
 
250
 
 
251
    aparam->plane_bytes[0] = aparam->plane_bytes[1] =
 
252
                             aparam->plane_bytes[2] = Y_bytes;
 
253
 
 
254
    /* Zero unused planes */
 
255
    for (i=3; i<PJMEDIA_MAX_VIDEO_PLANES; ++i) {
 
256
        aparam->strides[i] = 0;
 
257
        aparam->planes[i] = NULL;
 
258
        aparam->plane_bytes[i] = 0;
 
259
    }
 
260
 
 
261
    return PJ_SUCCESS;
 
262
}
 
263
 
 
264
PJ_DEF(pj_status_t)
 
265
pjmedia_video_format_mgr_create(pj_pool_t *pool,
 
266
                                unsigned max_fmt,
 
267
                                unsigned options,
 
268
                                pjmedia_video_format_mgr **p_mgr)
 
269
{
 
270
    pjmedia_video_format_mgr *mgr;
 
271
    unsigned i;
 
272
 
 
273
    PJ_ASSERT_RETURN(pool && options==0, PJ_EINVAL);
 
274
 
 
275
    PJ_UNUSED_ARG(options);
 
276
 
 
277
    mgr = PJ_POOL_ALLOC_T(pool, pjmedia_video_format_mgr);
 
278
    mgr->max_info = max_fmt;
 
279
    mgr->info_cnt = 0;
 
280
    mgr->infos = pj_pool_calloc(pool, max_fmt, sizeof(pjmedia_video_format_info *));
 
281
 
 
282
    if (video_format_mgr_instance == NULL)
 
283
        video_format_mgr_instance = mgr;
 
284
 
 
285
    for (i=0; i<PJ_ARRAY_SIZE(built_in_vid_fmt_info); ++i) {
 
286
        pjmedia_register_video_format_info(mgr,
 
287
                                           &built_in_vid_fmt_info[i]);
 
288
    }
 
289
 
 
290
    if (p_mgr)
 
291
        *p_mgr = mgr;
 
292
 
 
293
    return PJ_SUCCESS;
 
294
}
 
295
 
 
296
 
 
297
PJ_DEF(const pjmedia_video_format_info*)
 
298
pjmedia_get_video_format_info(pjmedia_video_format_mgr *mgr,
 
299
                              pj_uint32_t id)
 
300
{
 
301
    pjmedia_video_format_info **first;
 
302
    unsigned     n;
 
303
 
 
304
    if (!mgr)
 
305
        mgr = pjmedia_video_format_mgr_instance();
 
306
 
 
307
    PJ_ASSERT_RETURN(mgr != NULL, NULL);
 
308
 
 
309
    /* Binary search for the appropriate format id */
 
310
    first = &mgr->infos[0];
 
311
    n = mgr->info_cnt;
 
312
    for (; n > 0; ) {
 
313
        unsigned half = n / 2;
 
314
        pjmedia_video_format_info **mid = first + half;
 
315
 
 
316
        if ((*mid)->id < id) {
 
317
            first = ++mid;
 
318
            n -= half + 1;
 
319
        } else if ((*mid)->id==id) {
 
320
            return *mid;
 
321
        } else {
 
322
            n = half;
 
323
        }
 
324
    }
 
325
 
 
326
    return NULL;
 
327
}
 
328
 
 
329
 
 
330
PJ_DEF(pj_status_t)
 
331
pjmedia_register_video_format_info(pjmedia_video_format_mgr *mgr,
 
332
                                   pjmedia_video_format_info *info)
 
333
{
 
334
    unsigned i;
 
335
 
 
336
    if (!mgr)
 
337
        mgr = pjmedia_video_format_mgr_instance();
 
338
 
 
339
    PJ_ASSERT_RETURN(mgr != NULL, PJ_EINVALIDOP);
 
340
 
 
341
    if (mgr->info_cnt >= mgr->max_info)
 
342
        return PJ_ETOOMANY;
 
343
 
 
344
    /* Insert to the array, sorted */
 
345
    for (i=0; i<mgr->info_cnt; ++i) {
 
346
        if (mgr->infos[i]->id >= info->id)
 
347
            break;
 
348
    }
 
349
 
 
350
    if (i < mgr->info_cnt) {
 
351
        if (mgr->infos[i]->id == info->id) {
 
352
            /* just overwrite */
 
353
            mgr->infos[i] = info;
 
354
            return PJ_SUCCESS;
 
355
        }
 
356
 
 
357
        pj_memmove(&mgr->infos[i+1], &mgr->infos[i],
 
358
                   (mgr->info_cnt - i) * sizeof(pjmedia_video_format_info*));
 
359
    }
 
360
 
 
361
    mgr->infos[i] = info;
 
362
    mgr->info_cnt++;
 
363
 
 
364
    return PJ_SUCCESS;
 
365
}
 
366
 
 
367
PJ_DEF(pjmedia_video_format_mgr*) pjmedia_video_format_mgr_instance(void)
 
368
{
 
369
    pj_assert(video_format_mgr_instance != NULL);
 
370
    return video_format_mgr_instance;
 
371
}
 
372
 
 
373
PJ_DEF(void)
 
374
pjmedia_video_format_mgr_set_instance(pjmedia_video_format_mgr *mgr)
 
375
{
 
376
    video_format_mgr_instance = mgr;
 
377
}
 
378
 
 
379
 
 
380
PJ_DEF(void) pjmedia_video_format_mgr_destroy(pjmedia_video_format_mgr *mgr)
 
381
{
 
382
    if (!mgr)
 
383
        mgr = pjmedia_video_format_mgr_instance();
 
384
 
 
385
    PJ_ASSERT_ON_FAIL(mgr != NULL, return);
 
386
 
 
387
    mgr->info_cnt = 0;
 
388
    if (video_format_mgr_instance == mgr)
 
389
        video_format_mgr_instance = NULL;
 
390
}
 
391
 
 
392
#endif /* PJMEDIA_HAS_VIDEO */