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

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.0.1/pjmedia/src/pjmedia/ffmpeg_util.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ffmpeg_util.c 4158 2012-06-06 09:56:14Z nanang $ */
2
 
/*
3
 
 * Copyright (C) 2010-2011 Teluu Inc. (http://www.teluu.com)
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (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
 
#include <pjmedia/types.h>
20
 
#include <pj/errno.h>
21
 
#include <pj/log.h>
22
 
#include <pj/string.h>
23
 
 
24
 
#if PJMEDIA_HAS_LIBAVFORMAT && PJMEDIA_HAS_LIBAVUTIL
25
 
 
26
 
#include "ffmpeg_util.h"
27
 
#include <libavformat/avformat.h>
28
 
 
29
 
#define MAKE_VER(mj,mn,mi)      ((mj << 16) | (mn << 8) | (mi << 0))
30
 
#define VER_AT_LEAST(mj,mn,mi)  (MAKE_VER(LIBAVUTIL_VERSION_MAJOR, \
31
 
                                          LIBAVUTIL_VERSION_MINOR, \
32
 
                                          LIBAVUTIL_VERSION_MICRO) >= \
33
 
                                 MAKE_VER(mj,mn,mi))
34
 
 
35
 
 
36
 
 
37
 
/* Conversion table between pjmedia_format_id and PixelFormat */
38
 
static const struct ffmpeg_fmt_table_t
39
 
{
40
 
    pjmedia_format_id   id;
41
 
    enum PixelFormat    pf;
42
 
} ffmpeg_fmt_table[] =
43
 
{
44
 
    { PJMEDIA_FORMAT_RGBA, PIX_FMT_RGBA},
45
 
    { PJMEDIA_FORMAT_RGB24,PIX_FMT_BGR24},
46
 
    { PJMEDIA_FORMAT_BGRA, PIX_FMT_BGRA},
47
 
#if VER_AT_LEAST(51,20,1)
48
 
    { PJMEDIA_FORMAT_GBRP, PIX_FMT_GBR24P},
49
 
#endif
50
 
 
51
 
    { PJMEDIA_FORMAT_AYUV, PIX_FMT_NONE},
52
 
    { PJMEDIA_FORMAT_YUY2, PIX_FMT_YUYV422},
53
 
    { PJMEDIA_FORMAT_UYVY, PIX_FMT_UYVY422},
54
 
    { PJMEDIA_FORMAT_I420, PIX_FMT_YUV420P},
55
 
    //{ PJMEDIA_FORMAT_YV12, PIX_FMT_YUV420P},
56
 
    { PJMEDIA_FORMAT_I422, PIX_FMT_YUV422P},
57
 
    { PJMEDIA_FORMAT_I420JPEG, PIX_FMT_YUVJ420P},
58
 
    { PJMEDIA_FORMAT_I422JPEG, PIX_FMT_YUVJ422P},
59
 
};
60
 
 
61
 
/* Conversion table between pjmedia_format_id and CodecID */
62
 
static const struct ffmpeg_codec_table_t
63
 
{
64
 
    pjmedia_format_id   id;
65
 
    enum CodecID        codec_id;
66
 
} ffmpeg_codec_table[] =
67
 
{
68
 
    {PJMEDIA_FORMAT_H261,       CODEC_ID_H261},
69
 
    {PJMEDIA_FORMAT_H263,       CODEC_ID_H263},
70
 
    {PJMEDIA_FORMAT_H263P,      CODEC_ID_H263P},
71
 
    {PJMEDIA_FORMAT_H264,       CODEC_ID_H264},
72
 
    {PJMEDIA_FORMAT_MPEG1VIDEO, CODEC_ID_MPEG1VIDEO},
73
 
    {PJMEDIA_FORMAT_MPEG2VIDEO, CODEC_ID_MPEG2VIDEO},
74
 
    {PJMEDIA_FORMAT_MPEG4,      CODEC_ID_MPEG4},
75
 
    {PJMEDIA_FORMAT_MJPEG,      CODEC_ID_MJPEG}
76
 
};
77
 
 
78
 
static int pjmedia_ffmpeg_ref_cnt;
79
 
 
80
 
static void ffmpeg_log_cb(void* ptr, int level, const char* fmt, va_list vl);
81
 
 
82
 
void pjmedia_ffmpeg_add_ref()
83
 
{
84
 
    if (pjmedia_ffmpeg_ref_cnt++ == 0) {
85
 
        av_log_set_level(AV_LOG_ERROR);
86
 
        av_log_set_callback(&ffmpeg_log_cb);
87
 
        av_register_all();
88
 
    }
89
 
}
90
 
 
91
 
void pjmedia_ffmpeg_dec_ref()
92
 
{
93
 
    if (pjmedia_ffmpeg_ref_cnt-- == 1) {
94
 
        /* How to shutdown ffmpeg? */
95
 
    }
96
 
 
97
 
    if (pjmedia_ffmpeg_ref_cnt < 0) pjmedia_ffmpeg_ref_cnt = 0;
98
 
}
99
 
 
100
 
 
101
 
static void ffmpeg_log_cb(void* ptr, int level, const char* fmt, va_list vl)
102
 
{
103
 
    const char *LOG_SENDER = "ffmpeg";
104
 
    enum { LOG_LEVEL = 5 };
105
 
    char buf[100];
106
 
    int bufsize = sizeof(buf), len;
107
 
    pj_str_t fmt_st;
108
 
 
109
 
    /* Custom callback needs to filter log level by itself */
110
 
    if (level > av_log_get_level())
111
 
        return;
112
 
 
113
 
    /* Add original ffmpeg sender to log format */
114
 
    if (ptr) {
115
 
        AVClass* avc = *(AVClass**)ptr;
116
 
        len = pj_ansi_snprintf(buf, bufsize, "%s: ", avc->item_name(ptr));
117
 
        bufsize -= len;
118
 
    }
119
 
 
120
 
    /* Copy original log format */
121
 
    len = pj_ansi_strlen(fmt);
122
 
    if (len > bufsize-1)
123
 
        len = bufsize-1;
124
 
    pj_memcpy(buf+sizeof(buf)-bufsize, fmt, len);
125
 
    bufsize -= len;
126
 
 
127
 
    /* Trim log format */
128
 
    pj_strset(&fmt_st, buf, sizeof(buf)-bufsize);
129
 
    pj_strrtrim(&fmt_st);
130
 
    buf[fmt_st.slen] = '\0';
131
 
 
132
 
    pj_log(LOG_SENDER, LOG_LEVEL, buf, vl);
133
 
}
134
 
 
135
 
 
136
 
pj_status_t pjmedia_format_id_to_PixelFormat(pjmedia_format_id fmt_id,
137
 
                                             enum PixelFormat *pixel_format)
138
 
{
139
 
    unsigned i;
140
 
    for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_fmt_table); ++i) {
141
 
        const struct ffmpeg_fmt_table_t *t = &ffmpeg_fmt_table[i];
142
 
        if (t->id==fmt_id && t->pf != PIX_FMT_NONE) {
143
 
            *pixel_format = t->pf;
144
 
            return PJ_SUCCESS;
145
 
        }
146
 
    }
147
 
 
148
 
    *pixel_format = PIX_FMT_NONE;
149
 
    return PJ_ENOTFOUND;
150
 
}
151
 
 
152
 
pj_status_t PixelFormat_to_pjmedia_format_id(enum PixelFormat pf,
153
 
                                             pjmedia_format_id *fmt_id)
154
 
{
155
 
    unsigned i;
156
 
    for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_fmt_table); ++i) {
157
 
        const struct ffmpeg_fmt_table_t *t = &ffmpeg_fmt_table[i];
158
 
        if (t->pf == pf) {
159
 
            if (fmt_id) *fmt_id = t->id;
160
 
            return PJ_SUCCESS;
161
 
        }
162
 
    }
163
 
 
164
 
    return PJ_ENOTFOUND;
165
 
}
166
 
 
167
 
pj_status_t pjmedia_format_id_to_CodecID(pjmedia_format_id fmt_id,
168
 
                                         enum CodecID *codec_id)
169
 
{
170
 
    unsigned i;
171
 
    for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_codec_table); ++i) {
172
 
        const struct ffmpeg_codec_table_t *t = &ffmpeg_codec_table[i];
173
 
        if (t->id==fmt_id && t->codec_id != PIX_FMT_NONE) {
174
 
            *codec_id = t->codec_id;
175
 
            return PJ_SUCCESS;
176
 
        }
177
 
    }
178
 
 
179
 
    *codec_id = PIX_FMT_NONE;
180
 
    return PJ_ENOTFOUND;
181
 
}
182
 
 
183
 
pj_status_t CodecID_to_pjmedia_format_id(enum CodecID codec_id,
184
 
                                         pjmedia_format_id *fmt_id)
185
 
{
186
 
    unsigned i;
187
 
    for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_codec_table); ++i) {
188
 
        const struct ffmpeg_codec_table_t *t = &ffmpeg_codec_table[i];
189
 
        if (t->codec_id == codec_id) {
190
 
            if (fmt_id) *fmt_id = t->id;
191
 
            return PJ_SUCCESS;
192
 
        }
193
 
    }
194
 
 
195
 
    return PJ_ENOTFOUND;
196
 
}
197
 
 
198
 
 
199
 
#ifdef _MSC_VER
200
 
#   pragma comment( lib, "avformat.lib")
201
 
#   pragma comment( lib, "avutil.lib")
202
 
#endif
203
 
 
204
 
#endif  /* #if PJMEDIA_HAS_LIBAVFORMAT && PJMEDIA_HAS_LIBAVUTIL */