summaryrefslogtreecommitdiff
path: root/modules/avcodec/avcodec.c
blob: 307b774d26734fe08ab0979b921e22079af500fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
 * @file avcodec.c  Video codecs using libavcodec
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include <libavcodec/avcodec.h>
#ifdef USE_X264
#include <x264.h>
#endif
#include "h26x.h"
#include "avcodec.h"


/**
 * @defgroup avcodec avcodec
 *
 * Video codecs using libavcodec
 *
 * This module implements H.263, H.264 and MPEG4 video codecs
 * using libavcodec from FFmpeg or libav projects, and libx264.
 *
 * Build options:
 *
 \verbatim
      $ make USE_X264=1    ; enable direct usage of libx264
      $ make USE_X264=     ; use H.264 encoder from libavcodec
 \endverbatim
 *
 * Config options:
 *
 \verbatim
      avcodec_h264enc  <NAME>  ; e.g. h264_nvenc, h264_videotoolbox
      avcodec_h264dec  <NAME>  ; e.g. h264_cuvid, h264_vda, h264_qsv
 \endverbatim
 *
 * References:
 *
 *     http://ffmpeg.org
 *
 *     https://libav.org
 *
 *     RTP Payload Format for H.264 Video
 *     https://tools.ietf.org/html/rfc6184
 */


const uint8_t h264_level_idc = 0x0c;
AVCodec *avcodec_h264enc;             /* optinal; specified H.264 encoder */
AVCodec *avcodec_h264dec;             /* optinal; specified H.264 decoder */


int avcodec_resolve_codecid(const char *s)
{
	if (0 == str_casecmp(s, "H263"))
		return AV_CODEC_ID_H263;
	else if (0 == str_casecmp(s, "H264"))
		return AV_CODEC_ID_H264;
	else if (0 == str_casecmp(s, "MP4V-ES"))
		return AV_CODEC_ID_MPEG4;
	else
		return AV_CODEC_ID_NONE;
}


static uint32_t packetization_mode(const char *fmtp)
{
	struct pl pl, mode;

	if (!fmtp)
		return 0;

	pl_set_str(&pl, fmtp);

	if (fmt_param_get(&pl, "packetization-mode", &mode))
		return pl_u32(&mode);

	return 0;
}


static int h264_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt,
			 bool offer, void *arg)
{
	struct vidcodec *vc = arg;
	const uint8_t profile_idc = 0x42; /* baseline profile */
	const uint8_t profile_iop = 0x80;
	(void)offer;

	if (!mb || !fmt || !vc)
		return 0;

	return mbuf_printf(mb, "a=fmtp:%s"
			   " packetization-mode=0"
			   ";profile-level-id=%02x%02x%02x"
			   "\r\n",
			   fmt->id, profile_idc, profile_iop, h264_level_idc);
}


static bool h264_fmtp_cmp(const char *fmtp1, const char *fmtp2, void *data)
{
	(void)data;

	return packetization_mode(fmtp1) == packetization_mode(fmtp2);
}


static int h263_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt,
			 bool offer, void *arg)
{
	(void)offer;
	(void)arg;

	if (!mb || !fmt)
		return 0;

	return mbuf_printf(mb, "a=fmtp:%s CIF=1;CIF4=1\r\n", fmt->id);
}


static int mpg4_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt,
			 bool offer, void *arg)
{
	(void)offer;
	(void)arg;

	if (!mb || !fmt)
		return 0;

	return mbuf_printf(mb, "a=fmtp:%s profile-level-id=3\r\n", fmt->id);
}


static struct vidcodec h264 = {
	LE_INIT,
	NULL,
	"H264",
	"packetization-mode=0",
	NULL,
	encode_update,
#ifdef USE_X264
	encode_x264,
#else
	encode,
#endif
	decode_update,
	decode_h264,
	h264_fmtp_enc,
	h264_fmtp_cmp,
};

static struct vidcodec h263 = {
	LE_INIT,
	"34",
	"H263",
	NULL,
	NULL,
	encode_update,
	encode,
	decode_update,
	decode_h263,
	h263_fmtp_enc,
	NULL,
};

static struct vidcodec mpg4 = {
	LE_INIT,
	NULL,
	"MP4V-ES",
	NULL,
	NULL,
	encode_update,
	encode,
	decode_update,
	decode_mpeg4,
	mpg4_fmtp_enc,
	NULL,
};


static int module_init(void)
{
	char h264enc[64];
	char h264dec[64];

#ifdef USE_X264
	debug("avcodec: x264 build %d\n", X264_BUILD);
#else
	debug("avcodec: using libavcodec H.264 encoder\n");
#endif

#if LIBAVCODEC_VERSION_INT < ((53<<16)+(10<<8)+0)
	avcodec_init();
#endif

	avcodec_register_all();

	if (0 == conf_get_str(conf_cur(), "avcodec_h264dec",
			      h264dec, sizeof(h264dec))) {

		info("avcodec: using h264 decoder by name (%s)\n", h264dec);

		avcodec_h264dec = avcodec_find_decoder_by_name(h264dec);
		if (!avcodec_h264dec) {
			warning("avcodec: h264 decoder not found (%s)\n",
				h264dec);
			return ENOENT;
		}
		vidcodec_register(&h264);
	}
	else {
		if (avcodec_find_decoder(AV_CODEC_ID_H264))
			vidcodec_register(&h264);
	}

	if (avcodec_find_decoder(AV_CODEC_ID_H263))
		vidcodec_register(&h263);

	if (avcodec_find_decoder(AV_CODEC_ID_MPEG4))
		vidcodec_register(&mpg4);

	if (0 == conf_get_str(conf_cur(), "avcodec_h264enc",
			      h264enc, sizeof(h264enc))) {

		info("avcodec: using h264 encoder by name (%s)\n", h264enc);

		avcodec_h264enc = avcodec_find_encoder_by_name(h264enc);
		if (!avcodec_h264enc) {
			warning("avcodec: h264 encoder not found (%s)\n",
				h264enc);
			return ENOENT;
		}
	}

	return 0;
}


static int module_close(void)
{
	vidcodec_unregister(&mpg4);
	vidcodec_unregister(&h263);
	vidcodec_unregister(&h264);

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(avcodec) = {
	"avcodec",
	"codec",
	module_init,
	module_close
};