| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <pjmedia-codec/ffmpeg_vid_codecs.h> |
| 19 |
#include <pjmedia-codec/h263_packetizer.h> |
| 20 |
#include <pjmedia-codec/h264_packetizer.h> |
| 21 |
#include <pjmedia-codec/vpx_packetizer.h> |
| 22 |
#include <pjmedia/errno.h> |
| 23 |
#include <pjmedia/vid_codec_util.h> |
| 24 |
#include <pj/assert.h> |
| 25 |
#include <pj/list.h> |
| 26 |
#include <pj/log.h> |
| 27 |
#include <pj/math.h> |
| 28 |
#include <pj/pool.h> |
| 29 |
#include <pj/string.h> |
| 30 |
#include <pj/os.h> |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && \ |
| 38 |
PJMEDIA_HAS_FFMPEG_VID_CODEC != 0 && \ |
| 39 |
defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 40 |
|
| 41 |
#define THIS_FILE "ffmpeg_vid_codecs.c" |
| 42 |
|
| 43 |
#include "../pjmedia/ffmpeg_util.h" |
| 44 |
#include <libavcodec/avcodec.h> |
| 45 |
#include <libavformat/avformat.h> |
| 46 |
#if LIBAVCODEC_VER_AT_LEAST(53,20) |
| 47 |
|
| 48 |
# include <libavutil/opt.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#if LIBAVCODEC_VER_AT_LEAST(53,20) |
| 55 |
# define AVCODEC_OPEN(ctx,c) avcodec_open2(ctx,c,NULL) |
| 56 |
#else |
| 57 |
# define AVCODEC_OPEN(ctx,c) avcodec_open(ctx,c) |
| 58 |
#endif |
| 59 |
|
| 60 |
#if LIBAVCODEC_VER_AT_LEAST(53,61) |
| 61 |
# if LIBAVCODEC_VER_AT_LEAST(54,59) |
| 62 |
|
| 63 |
# define AVCODEC_HAS_ENCODE(c) (c->encode2 != (void*)0) |
| 64 |
# else |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
# define AVCODEC_HAS_ENCODE(c) (c->encode != (void*)0 || \ |
| 70 |
c->encode2 != (void*)0) |
| 71 |
# endif |
| 72 |
# define AV_OPT_SET(obj,name,val,opt) (av_opt_set(obj,name,val,opt)==0) |
| 73 |
# define AV_OPT_SET_INT(obj,name,val) (av_opt_set_int(obj,name,val,0)==0) |
| 74 |
#else |
| 75 |
# define AVCODEC_HAS_ENCODE(c) (c->encode != (void*)0) |
| 76 |
# define AV_OPT_SET(obj,name,val,opt) (av_set_string3(obj,name,val,opt,NULL)==0) |
| 77 |
# define AV_OPT_SET_INT(obj,name,val) (av_set_int(obj,name,val)!=NULL) |
| 78 |
#endif |
| 79 |
#define AVCODEC_HAS_DECODE(c) (c->decode != (void*)0) |
| 80 |
|
| 81 |
|
| 82 |
#define AVC_H264_PT PJMEDIA_RTP_PT_H264_RSV3 |
| 83 |
|
| 84 |
|
| 85 |
static pj_status_t ffmpeg_test_alloc( pjmedia_vid_codec_factory *factory, |
| 86 |
const pjmedia_vid_codec_info *id ); |
| 87 |
static pj_status_t ffmpeg_default_attr( pjmedia_vid_codec_factory *factory, |
| 88 |
const pjmedia_vid_codec_info *info, |
| 89 |
pjmedia_vid_codec_param *attr ); |
| 90 |
static pj_status_t ffmpeg_enum_codecs( pjmedia_vid_codec_factory *factory, |
| 91 |
unsigned *count, |
| 92 |
pjmedia_vid_codec_info codecs[]); |
| 93 |
static pj_status_t ffmpeg_alloc_codec( pjmedia_vid_codec_factory *factory, |
| 94 |
const pjmedia_vid_codec_info *info, |
| 95 |
pjmedia_vid_codec **p_codec); |
| 96 |
static pj_status_t ffmpeg_dealloc_codec( pjmedia_vid_codec_factory *factory, |
| 97 |
pjmedia_vid_codec *codec ); |
| 98 |
|
| 99 |
|
| 100 |
static pj_status_t ffmpeg_codec_init( pjmedia_vid_codec *codec, |
| 101 |
pj_pool_t *pool ); |
| 102 |
static pj_status_t ffmpeg_codec_open( pjmedia_vid_codec *codec, |
| 103 |
pjmedia_vid_codec_param *attr ); |
| 104 |
static pj_status_t ffmpeg_codec_close( pjmedia_vid_codec *codec ); |
| 105 |
static pj_status_t ffmpeg_codec_modify(pjmedia_vid_codec *codec, |
| 106 |
const pjmedia_vid_codec_param *attr ); |
| 107 |
static pj_status_t ffmpeg_codec_get_param(pjmedia_vid_codec *codec, |
| 108 |
pjmedia_vid_codec_param *param); |
| 109 |
static pj_status_t ffmpeg_codec_encode_begin(pjmedia_vid_codec *codec, |
| 110 |
const pjmedia_vid_encode_opt *opt, |
| 111 |
const pjmedia_frame *input, |
| 112 |
unsigned out_size, |
| 113 |
pjmedia_frame *output, |
| 114 |
pj_bool_t *has_more); |
| 115 |
static pj_status_t ffmpeg_codec_encode_more(pjmedia_vid_codec *codec, |
| 116 |
unsigned out_size, |
| 117 |
pjmedia_frame *output, |
| 118 |
pj_bool_t *has_more); |
| 119 |
static pj_status_t ffmpeg_codec_decode( pjmedia_vid_codec *codec, |
| 120 |
pj_size_t pkt_count, |
| 121 |
pjmedia_frame packets[], |
| 122 |
unsigned out_size, |
| 123 |
pjmedia_frame *output); |
| 124 |
|
| 125 |
|
| 126 |
static pjmedia_vid_codec_op ffmpeg_op = |
| 127 |
{ |
| 128 |
&ffmpeg_codec_init, |
| 129 |
&ffmpeg_codec_open, |
| 130 |
&ffmpeg_codec_close, |
| 131 |
&ffmpeg_codec_modify, |
| 132 |
&ffmpeg_codec_get_param, |
| 133 |
&ffmpeg_codec_encode_begin, |
| 134 |
&ffmpeg_codec_encode_more, |
| 135 |
&ffmpeg_codec_decode, |
| 136 |
NULL |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
static pjmedia_vid_codec_factory_op ffmpeg_factory_op = |
| 141 |
{ |
| 142 |
&ffmpeg_test_alloc, |
| 143 |
&ffmpeg_default_attr, |
| 144 |
&ffmpeg_enum_codecs, |
| 145 |
&ffmpeg_alloc_codec, |
| 146 |
&ffmpeg_dealloc_codec |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static struct ffmpeg_factory { |
| 152 |
pjmedia_vid_codec_factory base; |
| 153 |
pjmedia_vid_codec_mgr *mgr; |
| 154 |
pj_pool_factory *pf; |
| 155 |
pj_pool_t *pool; |
| 156 |
pj_mutex_t *mutex; |
| 157 |
} ffmpeg_factory; |
| 158 |
|
| 159 |
|
| 160 |
typedef struct ffmpeg_codec_desc ffmpeg_codec_desc; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
typedef struct ffmpeg_private |
| 165 |
{ |
| 166 |
const ffmpeg_codec_desc *desc; |
| 167 |
pjmedia_vid_codec_param *param; |
| 168 |
pj_pool_t *pool; |
| 169 |
|
| 170 |
|
| 171 |
const pjmedia_video_format_info *enc_vfi; |
| 172 |
pjmedia_video_apply_fmt_param enc_vafp; |
| 173 |
const pjmedia_video_format_info *dec_vfi; |
| 174 |
pjmedia_video_apply_fmt_param dec_vafp; |
| 175 |
|
| 176 |
|
| 177 |
pj_bool_t whole; |
| 178 |
void *enc_buf; |
| 179 |
unsigned enc_buf_size; |
| 180 |
pj_bool_t enc_buf_is_keyframe; |
| 181 |
unsigned enc_frame_len; |
| 182 |
unsigned enc_processed; |
| 183 |
void *dec_buf; |
| 184 |
unsigned dec_buf_size; |
| 185 |
pj_timestamp last_dec_keyframe_ts; |
| 186 |
|
| 187 |
|
| 188 |
const AVCodec *enc; |
| 189 |
const AVCodec *dec; |
| 190 |
AVCodecContext *enc_ctx; |
| 191 |
AVCodecContext *dec_ctx; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
enum AVPixelFormat expected_dec_fmt; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
void *data; |
| 201 |
} ffmpeg_private; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
#define FUNC_PACKETIZE(name) \ |
| 208 |
pj_status_t(name)(ffmpeg_private *ff, pj_uint8_t *bits, \ |
| 209 |
pj_size_t bits_len, unsigned *bits_pos, \ |
| 210 |
pj_uint8_t *payload, pj_size_t *payload_len, \ |
| 211 |
pj_bool_t is_keyframe) |
| 212 |
|
| 213 |
#define FUNC_UNPACKETIZE(name) \ |
| 214 |
pj_status_t(name)(ffmpeg_private *ff, const pj_uint8_t *payload, \ |
| 215 |
pj_size_t payload_len, pj_uint8_t *bits, \ |
| 216 |
pj_size_t bits_len, unsigned *bits_pos) |
| 217 |
|
| 218 |
#define FUNC_FMT_MATCH(name) \ |
| 219 |
pj_status_t(name)(pj_pool_t *pool, \ |
| 220 |
pjmedia_sdp_media *offer, unsigned o_fmt_idx, \ |
| 221 |
pjmedia_sdp_media *answer, unsigned a_fmt_idx, \ |
| 222 |
unsigned option) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
typedef FUNC_PACKETIZE(*func_packetize); |
| 227 |
typedef FUNC_UNPACKETIZE(*func_unpacketize); |
| 228 |
typedef pj_status_t (*func_preopen) (ffmpeg_private *ff); |
| 229 |
typedef pj_status_t (*func_postopen) (ffmpeg_private *ff); |
| 230 |
typedef FUNC_FMT_MATCH(*func_sdp_fmt_match); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
struct ffmpeg_codec_desc |
| 235 |
{ |
| 236 |
|
| 237 |
pjmedia_vid_codec_info info; |
| 238 |
pjmedia_format_id base_fmt_id; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
pjmedia_rect_size size; |
| 245 |
pjmedia_ratio fps; |
| 246 |
pj_uint32_t avg_bps; |
| 247 |
pj_uint32_t max_bps; |
| 248 |
func_packetize packetize; |
| 249 |
func_unpacketize unpacketize; |
| 250 |
func_preopen preopen; |
| 251 |
func_preopen postopen; |
| 252 |
func_sdp_fmt_match sdp_fmt_match; |
| 253 |
pjmedia_codec_fmtp dec_fmtp; |
| 254 |
|
| 255 |
|
| 256 |
pj_bool_t enabled; |
| 257 |
const AVCodec *enc; |
| 258 |
const AVCodec *dec; |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
#if PJMEDIA_HAS_FFMPEG_CODEC_H264 && !LIBAVCODEC_VER_AT_LEAST(53,20) |
| 263 |
# error "Must use libavcodec version 53.20 or later to enable FFMPEG H264" |
| 264 |
#endif |
| 265 |
|
| 266 |
|
| 267 |
#define PROFILE_H264_BASELINE 66 |
| 268 |
#define PROFILE_H264_MAIN 77 |
| 269 |
|
| 270 |
|
| 271 |
#if PJMEDIA_HAS_FFMPEG_CODEC_VP8 || PJMEDIA_HAS_FFMPEG_CODEC_VP9 |
| 272 |
static pj_status_t vpx_preopen(ffmpeg_private *ff); |
| 273 |
static pj_status_t vpx_postopen(ffmpeg_private *ff); |
| 274 |
static FUNC_PACKETIZE(vpx_packetize); |
| 275 |
static FUNC_UNPACKETIZE(vpx_unpacketize); |
| 276 |
#endif |
| 277 |
|
| 278 |
#if PJMEDIA_HAS_FFMPEG_CODEC_H264 |
| 279 |
static pj_status_t h264_preopen(ffmpeg_private *ff); |
| 280 |
static pj_status_t h264_postopen(ffmpeg_private *ff); |
| 281 |
static FUNC_PACKETIZE(h264_packetize); |
| 282 |
static FUNC_UNPACKETIZE(h264_unpacketize); |
| 283 |
#endif |
| 284 |
|
| 285 |
static pj_status_t h263_preopen(ffmpeg_private *ff); |
| 286 |
static FUNC_PACKETIZE(h263_packetize); |
| 287 |
static FUNC_UNPACKETIZE(h263_unpacketize); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static ffmpeg_codec_desc codec_desc[] = |
| 292 |
{ |
| 293 |
#if PJMEDIA_HAS_FFMPEG_CODEC_H264 |
| 294 |
{ |
| 295 |
{PJMEDIA_FORMAT_H264, AVC_H264_PT, {"H264",4}, |
| 296 |
{"Constrained Baseline (level=30, pack=1)", 39}}, |
| 297 |
0, |
| 298 |
{720, 480}, {15, 1}, 256000, 256000, |
| 299 |
&h264_packetize, &h264_unpacketize, &h264_preopen, &h264_postopen, |
| 300 |
&pjmedia_vid_codec_h264_match_sdp, |
| 301 |
|
| 302 |
{2, { {{"profile-level-id",16}, {"42e01e",6}}, |
| 303 |
{{" packetization-mode",19}, {"1",1}}, } }, |
| 304 |
}, |
| 305 |
#endif |
| 306 |
|
| 307 |
#if PJMEDIA_HAS_FFMPEG_CODEC_VP8 |
| 308 |
{ |
| 309 |
{PJMEDIA_FORMAT_VP8, PJMEDIA_RTP_PT_VP8_RSV2, {"VP8",3}, |
| 310 |
{"FF VP8", 6}}, |
| 311 |
0, |
| 312 |
{720, 480}, {15, 1}, 256000, 256000, |
| 313 |
&vpx_packetize, &vpx_unpacketize, &vpx_preopen, &vpx_postopen, NULL, |
| 314 |
{2, { {{"max-fr",6}, {"30",2}}, |
| 315 |
{{" max-fs",7}, {"580",3}}, } }, |
| 316 |
}, |
| 317 |
#endif |
| 318 |
|
| 319 |
#if PJMEDIA_HAS_FFMPEG_CODEC_VP9 |
| 320 |
{ |
| 321 |
{PJMEDIA_FORMAT_VP9, PJMEDIA_RTP_PT_VP9_RSV2, {"VP9",3}, |
| 322 |
{"FF VP9", 6}}, |
| 323 |
0, |
| 324 |
{720, 480}, {15, 1}, 256000, 256000, |
| 325 |
&vpx_packetize, &vpx_unpacketize, &vpx_preopen, &vpx_postopen, NULL, |
| 326 |
{2, { {{"max-fr",6}, {"30",2}}, |
| 327 |
{{" max-fs",7}, {"580",3}}, } }, |
| 328 |
}, |
| 329 |
#endif |
| 330 |
|
| 331 |
#if PJMEDIA_HAS_FFMPEG_CODEC_H263P |
| 332 |
{ |
| 333 |
{PJMEDIA_FORMAT_H263P, PJMEDIA_RTP_PT_H263P, {"H263-1998",9}}, |
| 334 |
PJMEDIA_FORMAT_H263, |
| 335 |
{352, 288}, {15, 1}, 256000, 256000, |
| 336 |
&h263_packetize, &h263_unpacketize, &h263_preopen, NULL, NULL, |
| 337 |
{2, { {{"CIF",3}, {"1",1}}, |
| 338 |
{{"QCIF",4}, {"1",1}}, } }, |
| 339 |
}, |
| 340 |
#endif |
| 341 |
|
| 342 |
{ |
| 343 |
{PJMEDIA_FORMAT_H263, PJMEDIA_RTP_PT_H263, {"H263",4}}, |
| 344 |
}, |
| 345 |
{ |
| 346 |
{PJMEDIA_FORMAT_H261, PJMEDIA_RTP_PT_H261, {"H261",4}}, |
| 347 |
}, |
| 348 |
{ |
| 349 |
{PJMEDIA_FORMAT_MJPEG, PJMEDIA_RTP_PT_JPEG, {"JPEG",4}}, |
| 350 |
PJMEDIA_FORMAT_MJPEG, {640, 480}, {25, 1}, |
| 351 |
}, |
| 352 |
{ |
| 353 |
{PJMEDIA_FORMAT_MPEG4, 0, {"MP4V",4}}, |
| 354 |
PJMEDIA_FORMAT_MPEG4, {640, 480}, {25, 1}, |
| 355 |
}, |
| 356 |
}; |
| 357 |
|
| 358 |
#if PJMEDIA_HAS_FFMPEG_CODEC_VP8 || PJMEDIA_HAS_FFMPEG_CODEC_VP9 |
| 359 |
typedef struct vpx_data |
| 360 |
{ |
| 361 |
pjmedia_vpx_packetizer *pktz; |
| 362 |
} vpx_data; |
| 363 |
|
| 364 |
static pj_status_t vpx_preopen(ffmpeg_private *ff) |
| 365 |
{ |
| 366 |
vpx_data *data; |
| 367 |
pjmedia_vpx_packetizer_cfg pktz_cfg; |
| 368 |
pj_status_t status; |
| 369 |
|
| 370 |
data = PJ_POOL_ZALLOC_T(ff->pool, vpx_data); |
| 371 |
ff->data = data; |
| 372 |
|
| 373 |
|
| 374 |
if (!ff->param->ignore_fmtp) |
| 375 |
{ |
| 376 |
pjmedia_vid_codec_vpx_fmtp vpx_fmtp; |
| 377 |
const unsigned MAX_RX_RES = 1200; |
| 378 |
unsigned max_res = MAX_RX_RES; |
| 379 |
|
| 380 |
status = pjmedia_vid_codec_vpx_parse_fmtp(&ff->param->dec_fmtp, &vpx_fmtp); |
| 381 |
if (status != PJ_SUCCESS) |
| 382 |
{ |
| 383 |
PJ_LOG(2, (THIS_FILE, "Parse vpx fmtp fail, status:%d", status)); |
| 384 |
return status; |
| 385 |
} |
| 386 |
if (vpx_fmtp.max_fs > 0) |
| 387 |
{ |
| 388 |
max_res = ((int)pj_isqrt(vpx_fmtp.max_fs * 8)) * 16; |
| 389 |
} |
| 390 |
ff->dec_buf_size = (max_res * max_res * 3 >> 1) + (max_res); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
pktz_cfg.fmt_id = ff->desc->info.fmt_id; |
| 395 |
pktz_cfg.mtu = ff->param->enc_mtu; |
| 396 |
status = pjmedia_vpx_packetizer_create(ff->pool, &pktz_cfg, &data->pktz); |
| 397 |
if (status != PJ_SUCCESS) |
| 398 |
{ |
| 399 |
return status; |
| 400 |
} |
| 401 |
|
| 402 |
return PJ_SUCCESS; |
| 403 |
} |
| 404 |
|
| 405 |
static pj_status_t vpx_postopen(ffmpeg_private *ff) |
| 406 |
{ |
| 407 |
vpx_data *data = (vpx_data *)ff->data; |
| 408 |
PJ_UNUSED_ARG(data); |
| 409 |
return PJ_SUCCESS; |
| 410 |
} |
| 411 |
|
| 412 |
static FUNC_PACKETIZE(vpx_packetize) |
| 413 |
{ |
| 414 |
vpx_data *data = (vpx_data *)ff->data; |
| 415 |
pj_status_t status; |
| 416 |
unsigned payload_desc_size = 1; |
| 417 |
pj_uint8_t *outbuf = payload; |
| 418 |
pj_size_t out_size = *payload_len; |
| 419 |
out_size -= payload_desc_size; |
| 420 |
|
| 421 |
status = pjmedia_vpx_packetize(data->pktz, bits_len, bits_pos, is_keyframe, |
| 422 |
&outbuf, &out_size); |
| 423 |
if (status != PJ_SUCCESS) |
| 424 |
return status; |
| 425 |
pj_memcpy(outbuf + payload_desc_size, bits + *bits_pos, out_size); |
| 426 |
*payload_len = out_size + payload_desc_size; |
| 427 |
*bits_pos += out_size; |
| 428 |
return PJ_SUCCESS; |
| 429 |
} |
| 430 |
|
| 431 |
static FUNC_UNPACKETIZE(vpx_unpacketize) |
| 432 |
{ |
| 433 |
vpx_data *data = (vpx_data *)ff->data; |
| 434 |
unsigned desc_len = 0; |
| 435 |
pj_status_t status; |
| 436 |
unsigned pos = *bits_pos; |
| 437 |
status = pjmedia_vpx_unpacketize(data->pktz, payload, payload_len, &desc_len); |
| 438 |
if (status != PJ_SUCCESS) |
| 439 |
return status; |
| 440 |
payload_len -= desc_len; |
| 441 |
if (pos + payload_len > bits_len) |
| 442 |
{ |
| 443 |
PJ_LOG(2, (THIS_FILE, "Vpx unpacketize buffer overflow")); |
| 444 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 445 |
} |
| 446 |
|
| 447 |
pj_memcpy(bits + pos, (char *)payload + desc_len, payload_len); |
| 448 |
pos += payload_len; |
| 449 |
*bits_pos = pos; |
| 450 |
return PJ_SUCCESS; |
| 451 |
} |
| 452 |
#endif |
| 453 |
|
| 454 |
#if PJMEDIA_HAS_FFMPEG_CODEC_H264 |
| 455 |
|
| 456 |
typedef struct h264_data |
| 457 |
{ |
| 458 |
pjmedia_vid_codec_h264_fmtp fmtp; |
| 459 |
pjmedia_h264_packetizer *pktz; |
| 460 |
} h264_data; |
| 461 |
|
| 462 |
|
| 463 |
static pj_status_t h264_preopen(ffmpeg_private *ff) |
| 464 |
{ |
| 465 |
h264_data *data; |
| 466 |
pjmedia_h264_packetizer_cfg pktz_cfg; |
| 467 |
pj_status_t status; |
| 468 |
|
| 469 |
data = PJ_POOL_ZALLOC_T(ff->pool, h264_data); |
| 470 |
ff->data = data; |
| 471 |
|
| 472 |
|
| 473 |
status = pjmedia_vid_codec_h264_parse_fmtp(&ff->param->enc_fmtp, |
| 474 |
&data->fmtp); |
| 475 |
if (status != PJ_SUCCESS) |
| 476 |
return status; |
| 477 |
|
| 478 |
|
| 479 |
pktz_cfg.mtu = ff->param->enc_mtu; |
| 480 |
pktz_cfg.unpack_nal_start = 0; |
| 481 |
#if 0 |
| 482 |
if (data->fmtp.packetization_mode == 0) |
| 483 |
pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL; |
| 484 |
else if (data->fmtp.packetization_mode == 1) |
| 485 |
pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED; |
| 486 |
else |
| 487 |
return PJ_ENOTSUP; |
| 488 |
#else |
| 489 |
if (data->fmtp.packetization_mode!= |
| 490 |
PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL && |
| 491 |
data->fmtp.packetization_mode!= |
| 492 |
PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED) |
| 493 |
{ |
| 494 |
return PJ_ENOTSUP; |
| 495 |
} |
| 496 |
|
| 497 |
pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL; |
| 498 |
#endif |
| 499 |
|
| 500 |
status = pjmedia_h264_packetizer_create(ff->pool, &pktz_cfg, &data->pktz); |
| 501 |
if (status != PJ_SUCCESS) |
| 502 |
return status; |
| 503 |
|
| 504 |
|
| 505 |
if (!ff->param->ignore_fmtp) { |
| 506 |
status = pjmedia_vid_codec_h264_apply_fmtp(ff->param); |
| 507 |
if (status != PJ_SUCCESS) |
| 508 |
return status; |
| 509 |
} |
| 510 |
|
| 511 |
if (ff->param->dir & PJMEDIA_DIR_ENCODING) { |
| 512 |
pjmedia_video_format_detail *vfd; |
| 513 |
AVCodecContext *ctx = ff->enc_ctx; |
| 514 |
const char *profile = NULL; |
| 515 |
|
| 516 |
vfd = pjmedia_format_get_video_format_detail(&ff->param->enc_fmt, |
| 517 |
PJ_TRUE); |
| 518 |
|
| 519 |
|
| 520 |
ctx->width = vfd->size.w; |
| 521 |
ctx->height = vfd->size.h; |
| 522 |
ctx->time_base.num = vfd->fps.denum; |
| 523 |
ctx->time_base.den = vfd->fps.num; |
| 524 |
|
| 525 |
|
| 526 |
ctx->profile = data->fmtp.profile_idc; |
| 527 |
switch (ctx->profile) { |
| 528 |
case PROFILE_H264_BASELINE: |
| 529 |
profile = "baseline"; |
| 530 |
break; |
| 531 |
case PROFILE_H264_MAIN: |
| 532 |
profile = "main"; |
| 533 |
break; |
| 534 |
default: |
| 535 |
break; |
| 536 |
} |
| 537 |
if (profile && !AV_OPT_SET(ctx->priv_data, "profile", profile, 0)) |
| 538 |
{ |
| 539 |
PJ_LOG(3, (THIS_FILE, "Failed to set H264 profile to '%s'", |
| 540 |
profile)); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
if (data->fmtp.profile_iop) { |
| 546 |
#if defined(FF_PROFILE_H264_CONSTRAINED) |
| 547 |
ctx->profile |= FF_PROFILE_H264_CONSTRAINED; |
| 548 |
#endif |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
ctx->level = data->fmtp.level; |
| 553 |
|
| 554 |
|
| 555 |
if (!AV_OPT_SET_INT(ctx->priv_data, "slice-max-size", ff->param->enc_mtu)) |
| 556 |
{ |
| 557 |
PJ_LOG(3, (THIS_FILE, "Failed to set H264 max NAL size to %d", |
| 558 |
ff->param->enc_mtu)); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
if (!AV_OPT_SET_INT(ctx->priv_data, "intra-refresh", 1)) |
| 563 |
{ |
| 564 |
PJ_LOG(3, (THIS_FILE, "Failed to set x264 intra-refresh")); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
if (!AV_OPT_SET(ctx->priv_data, "preset", "veryfast", 0)) { |
| 571 |
PJ_LOG(3, (THIS_FILE, "Failed to set x264 preset 'veryfast'")); |
| 572 |
} |
| 573 |
if (!AV_OPT_SET(ctx->priv_data, "tune", "animation+zerolatency", 0)) { |
| 574 |
PJ_LOG(3, (THIS_FILE, "Failed to set x264 tune 'zerolatency'")); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (ff->param->dir & PJMEDIA_DIR_DECODING) { |
| 579 |
AVCodecContext *ctx = ff->dec_ctx; |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
if (data->fmtp.sprop_param_sets_len) { |
| 585 |
ctx->extradata_size = (int)data->fmtp.sprop_param_sets_len; |
| 586 |
ctx->extradata = data->fmtp.sprop_param_sets; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
return PJ_SUCCESS; |
| 591 |
} |
| 592 |
|
| 593 |
static pj_status_t h264_postopen(ffmpeg_private *ff) |
| 594 |
{ |
| 595 |
h264_data *data = (h264_data*)ff->data; |
| 596 |
PJ_UNUSED_ARG(data); |
| 597 |
return PJ_SUCCESS; |
| 598 |
} |
| 599 |
|
| 600 |
static FUNC_PACKETIZE(h264_packetize) |
| 601 |
{ |
| 602 |
PJ_UNUSED_ARG(is_keyframe); |
| 603 |
h264_data *data = (h264_data*)ff->data; |
| 604 |
pj_status_t status; |
| 605 |
pj_uint8_t *outbuf = payload; |
| 606 |
pj_size_t out_size = *payload_len; |
| 607 |
status = pjmedia_h264_packetize(data->pktz, bits, bits_len, bits_pos, |
| 608 |
(const pj_uint8_t **)&payload, payload_len); |
| 609 |
if (status != PJ_SUCCESS) |
| 610 |
return status; |
| 611 |
if (out_size < *payload_len) |
| 612 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 613 |
pj_memcpy(outbuf, payload, *payload_len); |
| 614 |
return PJ_SUCCESS; |
| 615 |
} |
| 616 |
|
| 617 |
static FUNC_UNPACKETIZE(h264_unpacketize) |
| 618 |
{ |
| 619 |
h264_data *data = (h264_data*)ff->data; |
| 620 |
return pjmedia_h264_unpacketize(data->pktz, payload, payload_len, |
| 621 |
bits, bits_len, bits_pos); |
| 622 |
} |
| 623 |
|
| 624 |
#endif |
| 625 |
|
| 626 |
|
| 627 |
#if PJMEDIA_HAS_FFMPEG_CODEC_H263P |
| 628 |
|
| 629 |
typedef struct h263_data |
| 630 |
{ |
| 631 |
pjmedia_h263_packetizer *pktz; |
| 632 |
} h263_data; |
| 633 |
|
| 634 |
|
| 635 |
static pj_status_t h263_preopen(ffmpeg_private *ff) |
| 636 |
{ |
| 637 |
h263_data *data; |
| 638 |
pjmedia_h263_packetizer_cfg pktz_cfg; |
| 639 |
pj_status_t status; |
| 640 |
|
| 641 |
data = PJ_POOL_ZALLOC_T(ff->pool, h263_data); |
| 642 |
ff->data = data; |
| 643 |
|
| 644 |
|
| 645 |
pktz_cfg.mtu = ff->param->enc_mtu; |
| 646 |
pktz_cfg.mode = PJMEDIA_H263_PACKETIZER_MODE_RFC4629; |
| 647 |
status = pjmedia_h263_packetizer_create(ff->pool, &pktz_cfg, &data->pktz); |
| 648 |
if (status != PJ_SUCCESS) |
| 649 |
return status; |
| 650 |
|
| 651 |
|
| 652 |
if (!ff->param->ignore_fmtp) { |
| 653 |
status = pjmedia_vid_codec_h263_apply_fmtp(ff->param); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
if (ff->param->dir & PJMEDIA_DIR_ENCODING) { |
| 658 |
pjmedia_video_format_detail *vfd; |
| 659 |
AVCodecContext *ctx = ff->enc_ctx; |
| 660 |
|
| 661 |
vfd = pjmedia_format_get_video_format_detail(&ff->param->enc_fmt, |
| 662 |
PJ_TRUE); |
| 663 |
|
| 664 |
|
| 665 |
ctx->width = vfd->size.w; |
| 666 |
ctx->height = vfd->size.h; |
| 667 |
ctx->time_base.num = vfd->fps.denum; |
| 668 |
ctx->time_base.den = vfd->fps.num; |
| 669 |
} |
| 670 |
|
| 671 |
return status; |
| 672 |
} |
| 673 |
|
| 674 |
static FUNC_PACKETIZE(h263_packetize) |
| 675 |
{ |
| 676 |
PJ_UNUSED_ARG(is_keyframe); |
| 677 |
h263_data *data = (h263_data*)ff->data; |
| 678 |
pj_status_t status; |
| 679 |
pj_uint8_t *outbuf = payload; |
| 680 |
pj_size_t out_size = *payload_len; |
| 681 |
status = pjmedia_h263_packetize(data->pktz, bits, bits_len, bits_pos, |
| 682 |
&payload, payload_len); |
| 683 |
if (status != PJ_SUCCESS) |
| 684 |
return status; |
| 685 |
if (out_size < *payload_len) |
| 686 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 687 |
pj_memcpy(outbuf, payload, *payload_len); |
| 688 |
return PJ_SUCCESS; |
| 689 |
} |
| 690 |
|
| 691 |
static FUNC_UNPACKETIZE(h263_unpacketize) |
| 692 |
{ |
| 693 |
h263_data *data = (h263_data*)ff->data; |
| 694 |
return pjmedia_h263_unpacketize(data->pktz, payload, payload_len, |
| 695 |
bits, bits_len, bits_pos); |
| 696 |
} |
| 697 |
|
| 698 |
#endif |
| 699 |
|
| 700 |
|
| 701 |
static const ffmpeg_codec_desc* find_codec_desc_by_info( |
| 702 |
const pjmedia_vid_codec_info *info) |
| 703 |
{ |
| 704 |
unsigned i; |
| 705 |
|
| 706 |
for (i=0; i<PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 707 |
ffmpeg_codec_desc *desc = &codec_desc[i]; |
| 708 |
|
| 709 |
if (desc->enabled && |
| 710 |
(desc->info.fmt_id == info->fmt_id) && |
| 711 |
((desc->info.dir & info->dir) == info->dir) && |
| 712 |
(desc->info.pt == info->pt) && |
| 713 |
(desc->info.packings & info->packings)) |
| 714 |
{ |
| 715 |
return desc; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
return NULL; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
static int find_codec_idx_by_fmt_id(pjmedia_format_id fmt_id) |
| 724 |
{ |
| 725 |
unsigned i; |
| 726 |
for (i=0; i<PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 727 |
if (codec_desc[i].info.fmt_id == fmt_id) |
| 728 |
return i; |
| 729 |
} |
| 730 |
|
| 731 |
return -1; |
| 732 |
} |
| 733 |
|
| 734 |
static void init_codec(const AVCodec *c, pj_bool_t is_encoder, |
| 735 |
pj_bool_t is_decoder) |
| 736 |
{ |
| 737 |
pj_status_t status; |
| 738 |
ffmpeg_codec_desc *desc; |
| 739 |
pjmedia_format_id fmt_id; |
| 740 |
int codec_info_idx; |
| 741 |
|
| 742 |
#if LIBAVCODEC_VERSION_MAJOR <= 52 |
| 743 |
# define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO |
| 744 |
#endif |
| 745 |
if (c->type != AVMEDIA_TYPE_VIDEO) |
| 746 |
return; |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
status = CodecID_to_pjmedia_format_id(c->id, &fmt_id); |
| 754 |
|
| 755 |
if (status != PJ_SUCCESS) |
| 756 |
return; |
| 757 |
|
| 758 |
codec_info_idx = find_codec_idx_by_fmt_id(fmt_id); |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
if (codec_info_idx < 0) |
| 763 |
return; |
| 764 |
|
| 765 |
desc = &codec_desc[codec_info_idx]; |
| 766 |
|
| 767 |
|
| 768 |
if ((is_encoder && (desc->info.dir & PJMEDIA_DIR_ENCODING)) |
| 769 |
|| |
| 770 |
(is_decoder && (desc->info.dir & PJMEDIA_DIR_DECODING))) |
| 771 |
{ |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
if (c->pix_fmts && is_encoder) { |
| 777 |
pjmedia_format_id raw_fmt[PJMEDIA_VID_CODEC_MAX_DEC_FMT_CNT]; |
| 778 |
unsigned raw_fmt_cnt = 0; |
| 779 |
unsigned raw_fmt_cnt_should_be = 0; |
| 780 |
const enum AVPixelFormat *p = c->pix_fmts; |
| 781 |
|
| 782 |
for(;(p && *p != -1) && |
| 783 |
(raw_fmt_cnt < PJMEDIA_VID_CODEC_MAX_DEC_FMT_CNT); |
| 784 |
++p) |
| 785 |
{ |
| 786 |
pjmedia_format_id pix_fmt_id; |
| 787 |
|
| 788 |
raw_fmt_cnt_should_be++; |
| 789 |
status = PixelFormat_to_pjmedia_format_id(*p, &pix_fmt_id); |
| 790 |
if (status != PJ_SUCCESS) { |
| 791 |
PJ_PERROR(6, (THIS_FILE, status, |
| 792 |
"Unrecognized ffmpeg pixel format %d", *p)); |
| 793 |
continue; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
if (desc->info.pt != PJMEDIA_RTP_PT_H264 || |
| 801 |
pix_fmt_id != PJMEDIA_FORMAT_RGB24) |
| 802 |
{ |
| 803 |
raw_fmt[raw_fmt_cnt++] = pix_fmt_id; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
if (raw_fmt_cnt == 0) { |
| 808 |
PJ_LOG(5, (THIS_FILE, "No recognized raw format " |
| 809 |
"for codec [%s/%s], codec ignored", |
| 810 |
c->name, c->long_name)); |
| 811 |
|
| 812 |
return; |
| 813 |
} |
| 814 |
|
| 815 |
if (raw_fmt_cnt < raw_fmt_cnt_should_be) { |
| 816 |
PJ_LOG(6, (THIS_FILE, "Codec [%s/%s] have %d raw formats, " |
| 817 |
"recognized only %d raw formats", |
| 818 |
c->name, c->long_name, |
| 819 |
raw_fmt_cnt_should_be, raw_fmt_cnt)); |
| 820 |
} |
| 821 |
|
| 822 |
desc->info.dec_fmt_id_cnt = raw_fmt_cnt; |
| 823 |
pj_memcpy(desc->info.dec_fmt_id, raw_fmt, |
| 824 |
sizeof(raw_fmt[0])*raw_fmt_cnt); |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
if (c->supported_framerates) { |
| 829 |
const AVRational *fr = c->supported_framerates; |
| 830 |
while ((fr->num != 0 || fr->den != 0) && |
| 831 |
desc->info.fps_cnt < PJMEDIA_VID_CODEC_MAX_FPS_CNT) |
| 832 |
{ |
| 833 |
desc->info.fps[desc->info.fps_cnt].num = fr->num; |
| 834 |
desc->info.fps[desc->info.fps_cnt].denum = fr->den; |
| 835 |
++desc->info.fps_cnt; |
| 836 |
++fr; |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
if (is_encoder && !desc->enc) { |
| 842 |
desc->info.dir |= PJMEDIA_DIR_ENCODING; |
| 843 |
desc->enc = c; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
if (is_decoder && !desc->dec) { |
| 848 |
desc->info.dir |= PJMEDIA_DIR_DECODING; |
| 849 |
desc->dec = c; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
if ((desc->dec || desc->enc) && desc->info.dec_fmt_id_cnt) |
| 856 |
{ |
| 857 |
desc->enabled = PJ_TRUE; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
if (desc->info.clock_rate == 0) |
| 862 |
desc->info.clock_rate = 90000; |
| 863 |
|
| 864 |
|
| 865 |
desc->info.packings |= PJMEDIA_VID_PACKING_WHOLE; |
| 866 |
if (desc->packetize && desc->unpacketize) |
| 867 |
desc->info.packings |= PJMEDIA_VID_PACKING_PACKETS; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
PJ_DEF(pj_status_t) pjmedia_codec_ffmpeg_vid_init(pjmedia_vid_codec_mgr *mgr, |
| 874 |
pj_pool_factory *pf) |
| 875 |
{ |
| 876 |
pj_pool_t *pool; |
| 877 |
const AVCodec *c; |
| 878 |
pj_status_t status; |
| 879 |
unsigned i; |
| 880 |
|
| 881 |
if (ffmpeg_factory.pool != NULL) { |
| 882 |
|
| 883 |
return PJ_SUCCESS; |
| 884 |
} |
| 885 |
|
| 886 |
if (!mgr) mgr = pjmedia_vid_codec_mgr_instance(); |
| 887 |
PJ_ASSERT_RETURN(mgr, PJ_EINVAL); |
| 888 |
|
| 889 |
|
| 890 |
ffmpeg_factory.base.op = &ffmpeg_factory_op; |
| 891 |
ffmpeg_factory.base.factory_data = NULL; |
| 892 |
ffmpeg_factory.mgr = mgr; |
| 893 |
ffmpeg_factory.pf = pf; |
| 894 |
|
| 895 |
pool = pj_pool_create(pf, "ffmpeg codec factory", 256, 256, NULL); |
| 896 |
if (!pool) |
| 897 |
return PJ_ENOMEM; |
| 898 |
|
| 899 |
|
| 900 |
status = pj_mutex_create_simple(pool, "ffmpeg codec factory", |
| 901 |
&ffmpeg_factory.mutex); |
| 902 |
if (status != PJ_SUCCESS) |
| 903 |
goto on_error; |
| 904 |
|
| 905 |
pjmedia_ffmpeg_add_ref(); |
| 906 |
#if !LIBAVCODEC_VER_AT_LEAST(53,20) |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
avcodec_init(); |
| 911 |
#endif |
| 912 |
|
| 913 |
#if LIBAVCODEC_VER_AT_LEAST(58,137) |
| 914 |
|
| 915 |
for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 916 |
unsigned codec_id; |
| 917 |
|
| 918 |
pjmedia_format_id_to_CodecID(codec_desc[i].info.fmt_id, &codec_id); |
| 919 |
|
| 920 |
c = avcodec_find_encoder(codec_id); |
| 921 |
if (c) |
| 922 |
init_codec(c, PJ_TRUE, PJ_FALSE); |
| 923 |
|
| 924 |
c = avcodec_find_decoder(codec_id); |
| 925 |
if (c) |
| 926 |
init_codec(c, PJ_FALSE, PJ_TRUE); |
| 927 |
} |
| 928 |
#else |
| 929 |
|
| 930 |
avcodec_register_all(); |
| 931 |
|
| 932 |
|
| 933 |
for (c=av_codec_next(NULL); c; c=av_codec_next(c)) { |
| 934 |
init_codec(c, AVCODEC_HAS_ENCODE(c), AVCODEC_HAS_DECODE(c)); |
| 935 |
} |
| 936 |
|
| 937 |
#endif |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) { |
| 943 |
ffmpeg_codec_desc *desc = &codec_desc[i]; |
| 944 |
|
| 945 |
|
| 946 |
if (desc->base_fmt_id && (!desc->dec || !desc->enc)) { |
| 947 |
ffmpeg_codec_desc *base_desc = NULL; |
| 948 |
int base_desc_idx; |
| 949 |
pjmedia_dir copied_dir = PJMEDIA_DIR_NONE; |
| 950 |
|
| 951 |
base_desc_idx = find_codec_idx_by_fmt_id(desc->base_fmt_id); |
| 952 |
if (base_desc_idx != -1) |
| 953 |
base_desc = &codec_desc[base_desc_idx]; |
| 954 |
if (!base_desc || !base_desc->enabled) |
| 955 |
continue; |
| 956 |
|
| 957 |
|
| 958 |
if (!desc->info.dec_fmt_id_cnt) { |
| 959 |
desc->info.dec_fmt_id_cnt = base_desc->info.dec_fmt_id_cnt; |
| 960 |
pj_memcpy(desc->info.dec_fmt_id, base_desc->info.dec_fmt_id, |
| 961 |
sizeof(pjmedia_format_id)*desc->info.dec_fmt_id_cnt); |
| 962 |
} |
| 963 |
if (!desc->info.fps_cnt) { |
| 964 |
desc->info.fps_cnt = base_desc->info.fps_cnt; |
| 965 |
pj_memcpy(desc->info.fps, base_desc->info.fps, |
| 966 |
sizeof(desc->info.fps[0])*desc->info.fps_cnt); |
| 967 |
} |
| 968 |
if (!desc->info.clock_rate) { |
| 969 |
desc->info.clock_rate = base_desc->info.clock_rate; |
| 970 |
} |
| 971 |
if (!desc->dec && base_desc->dec) { |
| 972 |
copied_dir |= PJMEDIA_DIR_DECODING; |
| 973 |
desc->dec = base_desc->dec; |
| 974 |
} |
| 975 |
if (!desc->enc && base_desc->enc) { |
| 976 |
copied_dir |= PJMEDIA_DIR_ENCODING; |
| 977 |
desc->enc = base_desc->enc; |
| 978 |
} |
| 979 |
|
| 980 |
desc->info.dir |= copied_dir; |
| 981 |
desc->enabled = (desc->info.dir != PJMEDIA_DIR_NONE); |
| 982 |
|
| 983 |
|
| 984 |
desc->info.packings |= PJMEDIA_VID_PACKING_WHOLE; |
| 985 |
if (desc->packetize && desc->unpacketize) |
| 986 |
desc->info.packings |= PJMEDIA_VID_PACKING_PACKETS; |
| 987 |
|
| 988 |
if (copied_dir != PJMEDIA_DIR_NONE) { |
| 989 |
const char *dir_name[] = {NULL, "encoder", "decoder", "codec"}; |
| 990 |
PJ_LOG(5, (THIS_FILE, "The %.*s %s is using base codec (%.*s)", |
| 991 |
(int)desc->info.encoding_name.slen, |
| 992 |
desc->info.encoding_name.ptr, |
| 993 |
dir_name[copied_dir], |
| 994 |
(int)base_desc->info.encoding_name.slen, |
| 995 |
base_desc->info.encoding_name.ptr)); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
if (desc->sdp_fmt_match) { |
| 1001 |
status = pjmedia_sdp_neg_register_fmt_match_cb( |
| 1002 |
&desc->info.encoding_name, |
| 1003 |
desc->sdp_fmt_match); |
| 1004 |
pj_assert(status == PJ_SUCCESS); |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
if (!desc->enc) { |
| 1009 |
PJ_LOG(4, (THIS_FILE, "Cannot find %.*s encoder in ffmpeg library", |
| 1010 |
(int)desc->info.encoding_name.slen, |
| 1011 |
desc->info.encoding_name.ptr)); |
| 1012 |
} |
| 1013 |
if (!desc->dec) { |
| 1014 |
PJ_LOG(4, (THIS_FILE, "Cannot find %.*s decoder in ffmpeg library", |
| 1015 |
(int)desc->info.encoding_name.slen, |
| 1016 |
desc->info.encoding_name.ptr)); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
status = pjmedia_vid_codec_mgr_register_factory(mgr, |
| 1022 |
&ffmpeg_factory.base); |
| 1023 |
if (status != PJ_SUCCESS) |
| 1024 |
goto on_error; |
| 1025 |
|
| 1026 |
ffmpeg_factory.pool = pool; |
| 1027 |
|
| 1028 |
|
| 1029 |
return PJ_SUCCESS; |
| 1030 |
|
| 1031 |
on_error: |
| 1032 |
pj_pool_release(pool); |
| 1033 |
return status; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
PJ_DEF(pj_status_t) pjmedia_codec_ffmpeg_vid_deinit(void) |
| 1040 |
{ |
| 1041 |
pj_status_t status = PJ_SUCCESS; |
| 1042 |
|
| 1043 |
if (ffmpeg_factory.pool == NULL) { |
| 1044 |
|
| 1045 |
return PJ_SUCCESS; |
| 1046 |
} |
| 1047 |
|
| 1048 |
pj_mutex_lock(ffmpeg_factory.mutex); |
| 1049 |
|
| 1050 |
|
| 1051 |
status = pjmedia_vid_codec_mgr_unregister_factory(ffmpeg_factory.mgr, |
| 1052 |
&ffmpeg_factory.base); |
| 1053 |
|
| 1054 |
|
| 1055 |
pj_mutex_unlock(ffmpeg_factory.mutex); |
| 1056 |
pj_mutex_destroy(ffmpeg_factory.mutex); |
| 1057 |
ffmpeg_factory.mutex = NULL; |
| 1058 |
|
| 1059 |
|
| 1060 |
pj_pool_release(ffmpeg_factory.pool); |
| 1061 |
ffmpeg_factory.pool = NULL; |
| 1062 |
|
| 1063 |
pjmedia_ffmpeg_dec_ref(); |
| 1064 |
|
| 1065 |
return status; |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
static pj_status_t ffmpeg_test_alloc( pjmedia_vid_codec_factory *factory, |
| 1073 |
const pjmedia_vid_codec_info *info ) |
| 1074 |
{ |
| 1075 |
const ffmpeg_codec_desc *desc; |
| 1076 |
|
| 1077 |
PJ_ASSERT_RETURN(factory==&ffmpeg_factory.base, PJ_EINVAL); |
| 1078 |
PJ_ASSERT_RETURN(info, PJ_EINVAL); |
| 1079 |
|
| 1080 |
desc = find_codec_desc_by_info(info); |
| 1081 |
if (!desc) { |
| 1082 |
return PJMEDIA_CODEC_EUNSUP; |
| 1083 |
} |
| 1084 |
|
| 1085 |
return PJ_SUCCESS; |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
static pj_status_t ffmpeg_default_attr( pjmedia_vid_codec_factory *factory, |
| 1092 |
const pjmedia_vid_codec_info *info, |
| 1093 |
pjmedia_vid_codec_param *attr ) |
| 1094 |
{ |
| 1095 |
const ffmpeg_codec_desc *desc; |
| 1096 |
unsigned i; |
| 1097 |
|
| 1098 |
PJ_ASSERT_RETURN(factory==&ffmpeg_factory.base, PJ_EINVAL); |
| 1099 |
PJ_ASSERT_RETURN(info && attr, PJ_EINVAL); |
| 1100 |
|
| 1101 |
desc = find_codec_desc_by_info(info); |
| 1102 |
if (!desc) { |
| 1103 |
return PJMEDIA_CODEC_EUNSUP; |
| 1104 |
} |
| 1105 |
|
| 1106 |
pj_bzero(attr, sizeof(pjmedia_vid_codec_param)); |
| 1107 |
|
| 1108 |
|
| 1109 |
attr->packing = 0; |
| 1110 |
for (i=0; i<15; ++i) { |
| 1111 |
unsigned packing = (1 << i); |
| 1112 |
if ((desc->info.packings & info->packings) & packing) { |
| 1113 |
attr->packing = (pjmedia_vid_packing)packing; |
| 1114 |
break; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
if (attr->packing == 0) { |
| 1118 |
|
| 1119 |
return PJMEDIA_CODEC_EUNSUP; |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
attr->dir = desc->info.dir; |
| 1124 |
|
| 1125 |
|
| 1126 |
pjmedia_format_init_video(&attr->enc_fmt, desc->info.fmt_id, |
| 1127 |
desc->size.w, desc->size.h, |
| 1128 |
desc->fps.num, desc->fps.denum); |
| 1129 |
|
| 1130 |
|
| 1131 |
pjmedia_format_init_video(&attr->dec_fmt, desc->info.dec_fmt_id[0], |
| 1132 |
desc->size.w, desc->size.h, |
| 1133 |
desc->fps.num, desc->fps.denum); |
| 1134 |
|
| 1135 |
|
| 1136 |
attr->dec_fmtp = desc->dec_fmtp; |
| 1137 |
|
| 1138 |
|
| 1139 |
attr->enc_fmt.det.vid.avg_bps = desc->avg_bps; |
| 1140 |
attr->enc_fmt.det.vid.max_bps = desc->max_bps; |
| 1141 |
|
| 1142 |
|
| 1143 |
attr->enc_mtu = PJMEDIA_MAX_VID_PAYLOAD_SIZE; |
| 1144 |
|
| 1145 |
return PJ_SUCCESS; |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
static pj_status_t ffmpeg_enum_codecs( pjmedia_vid_codec_factory *factory, |
| 1152 |
unsigned *count, |
| 1153 |
pjmedia_vid_codec_info codecs[]) |
| 1154 |
{ |
| 1155 |
unsigned i, max_cnt; |
| 1156 |
|
| 1157 |
PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); |
| 1158 |
PJ_ASSERT_RETURN(factory == &ffmpeg_factory.base, PJ_EINVAL); |
| 1159 |
|
| 1160 |
max_cnt = PJ_MIN(*count, PJ_ARRAY_SIZE(codec_desc)); |
| 1161 |
*count = 0; |
| 1162 |
|
| 1163 |
for (i=0; i<max_cnt; ++i) { |
| 1164 |
if (codec_desc[i].enabled) { |
| 1165 |
pj_memcpy(&codecs[*count], &codec_desc[i].info, |
| 1166 |
sizeof(pjmedia_vid_codec_info)); |
| 1167 |
(*count)++; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
return PJ_SUCCESS; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
static pj_status_t ffmpeg_alloc_codec( pjmedia_vid_codec_factory *factory, |
| 1178 |
const pjmedia_vid_codec_info *info, |
| 1179 |
pjmedia_vid_codec **p_codec) |
| 1180 |
{ |
| 1181 |
ffmpeg_private *ff; |
| 1182 |
const ffmpeg_codec_desc *desc; |
| 1183 |
pjmedia_vid_codec *codec; |
| 1184 |
pj_pool_t *pool = NULL; |
| 1185 |
pj_status_t status = PJ_SUCCESS; |
| 1186 |
|
| 1187 |
PJ_ASSERT_RETURN(factory && info && p_codec, PJ_EINVAL); |
| 1188 |
PJ_ASSERT_RETURN(factory == &ffmpeg_factory.base, PJ_EINVAL); |
| 1189 |
|
| 1190 |
desc = find_codec_desc_by_info(info); |
| 1191 |
if (!desc) { |
| 1192 |
return PJMEDIA_CODEC_EUNSUP; |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
pool = pj_pool_create(ffmpeg_factory.pf, "ffmpeg codec", 512, 512, NULL); |
| 1197 |
if (!pool) { |
| 1198 |
status = PJ_ENOMEM; |
| 1199 |
goto on_error; |
| 1200 |
} |
| 1201 |
codec = PJ_POOL_ZALLOC_T(pool, pjmedia_vid_codec); |
| 1202 |
if (!codec) { |
| 1203 |
status = PJ_ENOMEM; |
| 1204 |
goto on_error; |
| 1205 |
} |
| 1206 |
codec->op = &ffmpeg_op; |
| 1207 |
codec->factory = factory; |
| 1208 |
ff = PJ_POOL_ZALLOC_T(pool, ffmpeg_private); |
| 1209 |
if (!ff) { |
| 1210 |
status = PJ_ENOMEM; |
| 1211 |
goto on_error; |
| 1212 |
} |
| 1213 |
codec->codec_data = ff; |
| 1214 |
ff->pool = pool; |
| 1215 |
ff->enc = desc->enc; |
| 1216 |
ff->dec = desc->dec; |
| 1217 |
ff->desc = desc; |
| 1218 |
|
| 1219 |
*p_codec = codec; |
| 1220 |
return PJ_SUCCESS; |
| 1221 |
|
| 1222 |
on_error: |
| 1223 |
if (pool) |
| 1224 |
pj_pool_release(pool); |
| 1225 |
return status; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
static pj_status_t ffmpeg_dealloc_codec( pjmedia_vid_codec_factory *factory, |
| 1232 |
pjmedia_vid_codec *codec ) |
| 1233 |
{ |
| 1234 |
ffmpeg_private *ff; |
| 1235 |
pj_pool_t *pool; |
| 1236 |
|
| 1237 |
PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL); |
| 1238 |
PJ_ASSERT_RETURN(factory == &ffmpeg_factory.base, PJ_EINVAL); |
| 1239 |
|
| 1240 |
|
| 1241 |
ff = (ffmpeg_private*) codec->codec_data; |
| 1242 |
pool = ff->pool; |
| 1243 |
codec->codec_data = NULL; |
| 1244 |
pj_pool_release(pool); |
| 1245 |
|
| 1246 |
return PJ_SUCCESS; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
static pj_status_t ffmpeg_codec_init( pjmedia_vid_codec *codec, |
| 1253 |
pj_pool_t *pool ) |
| 1254 |
{ |
| 1255 |
PJ_UNUSED_ARG(codec); |
| 1256 |
PJ_UNUSED_ARG(pool); |
| 1257 |
return PJ_SUCCESS; |
| 1258 |
} |
| 1259 |
|
| 1260 |
static void print_ffmpeg_err(int err) |
| 1261 |
{ |
| 1262 |
#if LIBAVCODEC_VER_AT_LEAST(52,72) |
| 1263 |
char errbuf[512]; |
| 1264 |
if (av_strerror(err, errbuf, sizeof(errbuf)) >= 0) |
| 1265 |
PJ_LOG(5, (THIS_FILE, "ffmpeg err %d: %s", err, errbuf)); |
| 1266 |
#else |
| 1267 |
PJ_LOG(5, (THIS_FILE, "ffmpeg err %d", err)); |
| 1268 |
#endif |
| 1269 |
|
| 1270 |
} |
| 1271 |
|
| 1272 |
static pj_status_t open_ffmpeg_codec(ffmpeg_private *ff, |
| 1273 |
pj_mutex_t *ff_mutex) |
| 1274 |
{ |
| 1275 |
enum AVPixelFormat pix_fmt; |
| 1276 |
pjmedia_video_format_detail *vfd; |
| 1277 |
pj_bool_t enc_opened = PJ_FALSE, dec_opened = PJ_FALSE; |
| 1278 |
pj_status_t status; |
| 1279 |
|
| 1280 |
|
| 1281 |
status = pjmedia_format_id_to_PixelFormat(ff->param->dec_fmt.id, |
| 1282 |
&pix_fmt); |
| 1283 |
if (status != PJ_SUCCESS) |
| 1284 |
return status; |
| 1285 |
ff->expected_dec_fmt = pix_fmt; |
| 1286 |
|
| 1287 |
|
| 1288 |
vfd = pjmedia_format_get_video_format_detail(&ff->param->enc_fmt, |
| 1289 |
PJ_TRUE); |
| 1290 |
|
| 1291 |
|
| 1292 |
if (ff->param->dir & PJMEDIA_DIR_ENCODING) { |
| 1293 |
#if LIBAVCODEC_VER_AT_LEAST(53,20) |
| 1294 |
ff->enc_ctx = avcodec_alloc_context3(ff->enc); |
| 1295 |
#else |
| 1296 |
ff->enc_ctx = avcodec_alloc_context(); |
| 1297 |
#endif |
| 1298 |
if (ff->enc_ctx == NULL) |
| 1299 |
goto on_error; |
| 1300 |
} |
| 1301 |
if (ff->param->dir & PJMEDIA_DIR_DECODING) { |
| 1302 |
#if LIBAVCODEC_VER_AT_LEAST(53,20) |
| 1303 |
ff->dec_ctx = avcodec_alloc_context3(ff->dec); |
| 1304 |
#else |
| 1305 |
ff->dec_ctx = avcodec_alloc_context(); |
| 1306 |
#endif |
| 1307 |
if (ff->dec_ctx == NULL) |
| 1308 |
goto on_error; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
if (ff->param->dir & PJMEDIA_DIR_ENCODING) { |
| 1313 |
AVCodecContext *ctx = ff->enc_ctx; |
| 1314 |
|
| 1315 |
ctx->pix_fmt = pix_fmt; |
| 1316 |
ctx->width = vfd->size.w; |
| 1317 |
ctx->height = vfd->size.h; |
| 1318 |
ctx->time_base.num = vfd->fps.denum; |
| 1319 |
ctx->time_base.den = vfd->fps.num; |
| 1320 |
if (vfd->avg_bps) { |
| 1321 |
ctx->bit_rate = vfd->avg_bps; |
| 1322 |
if (vfd->max_bps > vfd->avg_bps) |
| 1323 |
ctx->bit_rate_tolerance = vfd->max_bps - vfd->avg_bps; |
| 1324 |
} |
| 1325 |
ctx->strict_std_compliance = FF_COMPLIANCE_STRICT; |
| 1326 |
ctx->workaround_bugs = FF_BUG_AUTODETECT; |
| 1327 |
ctx->opaque = ff; |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
#if LIBAVCODEC_VER_AT_LEAST(52,113) && !LIBAVCODEC_VER_AT_LEAST(53,20) |
| 1333 |
ctx->rc_lookahead = 0; |
| 1334 |
#endif |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
if (ff->param->dir & PJMEDIA_DIR_DECODING) { |
| 1339 |
AVCodecContext *ctx = ff->dec_ctx; |
| 1340 |
|
| 1341 |
|
| 1342 |
ctx->width = ctx->coded_width = ff->param->dec_fmt.det.vid.size.w; |
| 1343 |
ctx->height = ctx->coded_height = ff->param->dec_fmt.det.vid.size.h; |
| 1344 |
ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; |
| 1345 |
ctx->workaround_bugs = FF_BUG_AUTODETECT; |
| 1346 |
ctx->opaque = ff; |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
if (ff->desc->preopen) { |
| 1353 |
status = (*ff->desc->preopen)(ff); |
| 1354 |
if (status != PJ_SUCCESS) |
| 1355 |
goto on_error; |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
if (ff->param->dir & PJMEDIA_DIR_ENCODING) { |
| 1360 |
int err; |
| 1361 |
|
| 1362 |
pj_mutex_lock(ff_mutex); |
| 1363 |
err = AVCODEC_OPEN(ff->enc_ctx, ff->enc); |
| 1364 |
pj_mutex_unlock(ff_mutex); |
| 1365 |
if (err < 0) { |
| 1366 |
print_ffmpeg_err(err); |
| 1367 |
status = PJMEDIA_CODEC_EFAILED; |
| 1368 |
goto on_error; |
| 1369 |
} |
| 1370 |
enc_opened = PJ_TRUE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
if (ff->param->dir & PJMEDIA_DIR_DECODING) { |
| 1375 |
int err; |
| 1376 |
|
| 1377 |
pj_mutex_lock(ff_mutex); |
| 1378 |
err = AVCODEC_OPEN(ff->dec_ctx, ff->dec); |
| 1379 |
pj_mutex_unlock(ff_mutex); |
| 1380 |
if (err < 0) { |
| 1381 |
print_ffmpeg_err(err); |
| 1382 |
status = PJMEDIA_CODEC_EFAILED; |
| 1383 |
goto on_error; |
| 1384 |
} |
| 1385 |
dec_opened = PJ_TRUE; |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
if (ff->desc->postopen) { |
| 1390 |
status = (*ff->desc->postopen)(ff); |
| 1391 |
if (status != PJ_SUCCESS) |
| 1392 |
goto on_error; |
| 1393 |
} |
| 1394 |
|
| 1395 |
return PJ_SUCCESS; |
| 1396 |
|
| 1397 |
on_error: |
| 1398 |
if (ff->enc_ctx) { |
| 1399 |
if (enc_opened) |
| 1400 |
avcodec_close(ff->enc_ctx); |
| 1401 |
av_free(ff->enc_ctx); |
| 1402 |
ff->enc_ctx = NULL; |
| 1403 |
} |
| 1404 |
if (ff->dec_ctx) { |
| 1405 |
if (dec_opened) |
| 1406 |
avcodec_close(ff->dec_ctx); |
| 1407 |
av_free(ff->dec_ctx); |
| 1408 |
ff->dec_ctx = NULL; |
| 1409 |
} |
| 1410 |
return status; |
| 1411 |
} |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
static pj_status_t ffmpeg_codec_open( pjmedia_vid_codec *codec, |
| 1417 |
pjmedia_vid_codec_param *attr ) |
| 1418 |
{ |
| 1419 |
ffmpeg_private *ff; |
| 1420 |
pj_status_t status; |
| 1421 |
pj_mutex_t *ff_mutex; |
| 1422 |
|
| 1423 |
PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL); |
| 1424 |
ff = (ffmpeg_private*)codec->codec_data; |
| 1425 |
|
| 1426 |
ff->param = pjmedia_vid_codec_param_clone(ff->pool, attr); |
| 1427 |
|
| 1428 |
|
| 1429 |
if (ff->param->enc_mtu > PJMEDIA_MAX_VID_PAYLOAD_SIZE) |
| 1430 |
ff->param->enc_mtu = PJMEDIA_MAX_VID_PAYLOAD_SIZE; |
| 1431 |
|
| 1432 |
|
| 1433 |
ff_mutex = ((struct ffmpeg_factory*)codec->factory)->mutex; |
| 1434 |
status = open_ffmpeg_codec(ff, ff_mutex); |
| 1435 |
if (status != PJ_SUCCESS) |
| 1436 |
goto on_error; |
| 1437 |
|
| 1438 |
|
| 1439 |
ff->dec_vfi = pjmedia_get_video_format_info(NULL, ff->param->dec_fmt.id); |
| 1440 |
if (!ff->dec_vfi) { |
| 1441 |
status = PJ_EINVAL; |
| 1442 |
goto on_error; |
| 1443 |
} |
| 1444 |
pj_bzero(&ff->dec_vafp, sizeof(ff->dec_vafp)); |
| 1445 |
ff->dec_vafp.size = ff->param->dec_fmt.det.vid.size; |
| 1446 |
ff->dec_vafp.buffer = NULL; |
| 1447 |
status = (*ff->dec_vfi->apply_fmt)(ff->dec_vfi, &ff->dec_vafp); |
| 1448 |
if (status != PJ_SUCCESS) { |
| 1449 |
goto on_error; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
ff->enc_vfi = pjmedia_get_video_format_info(NULL, ff->param->dec_fmt.id); |
| 1454 |
if (!ff->enc_vfi) { |
| 1455 |
status = PJ_EINVAL; |
| 1456 |
goto on_error; |
| 1457 |
} |
| 1458 |
pj_bzero(&ff->enc_vafp, sizeof(ff->enc_vafp)); |
| 1459 |
ff->enc_vafp.size = ff->param->enc_fmt.det.vid.size; |
| 1460 |
ff->enc_vafp.buffer = NULL; |
| 1461 |
status = (*ff->enc_vfi->apply_fmt)(ff->enc_vfi, &ff->enc_vafp); |
| 1462 |
if (status != PJ_SUCCESS) { |
| 1463 |
goto on_error; |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
ff->whole = (ff->param->packing == PJMEDIA_VID_PACKING_WHOLE); |
| 1468 |
if (!ff->whole) { |
| 1469 |
ff->enc_buf_size = (unsigned)ff->enc_vafp.framebytes; |
| 1470 |
ff->enc_buf = pj_pool_alloc(ff->pool, ff->enc_buf_size); |
| 1471 |
|
| 1472 |
ff->dec_buf_size = PJ_MAX((unsigned)ff->dec_vafp.framebytes, ff->dec_buf_size); |
| 1473 |
ff->dec_buf = pj_pool_alloc(ff->pool, ff->dec_buf_size); |
| 1474 |
} |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
pj_memcpy(attr, ff->param, sizeof(*attr)); |
| 1480 |
|
| 1481 |
return PJ_SUCCESS; |
| 1482 |
|
| 1483 |
on_error: |
| 1484 |
ffmpeg_codec_close(codec); |
| 1485 |
return status; |
| 1486 |
} |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
static pj_status_t ffmpeg_codec_close( pjmedia_vid_codec *codec ) |
| 1492 |
{ |
| 1493 |
ffmpeg_private *ff; |
| 1494 |
pj_mutex_t *ff_mutex; |
| 1495 |
|
| 1496 |
PJ_ASSERT_RETURN(codec, PJ_EINVAL); |
| 1497 |
ff = (ffmpeg_private*)codec->codec_data; |
| 1498 |
ff_mutex = ((struct ffmpeg_factory*)codec->factory)->mutex; |
| 1499 |
|
| 1500 |
pj_mutex_lock(ff_mutex); |
| 1501 |
if (ff->enc_ctx) { |
| 1502 |
avcodec_close(ff->enc_ctx); |
| 1503 |
av_free(ff->enc_ctx); |
| 1504 |
} |
| 1505 |
if (ff->dec_ctx && ff->dec_ctx!=ff->enc_ctx) { |
| 1506 |
avcodec_close(ff->dec_ctx); |
| 1507 |
av_free(ff->dec_ctx); |
| 1508 |
} |
| 1509 |
ff->enc_ctx = NULL; |
| 1510 |
ff->dec_ctx = NULL; |
| 1511 |
pj_mutex_unlock(ff_mutex); |
| 1512 |
|
| 1513 |
return PJ_SUCCESS; |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
static pj_status_t ffmpeg_codec_modify( pjmedia_vid_codec *codec, |
| 1521 |
const pjmedia_vid_codec_param *attr) |
| 1522 |
{ |
| 1523 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1524 |
|
| 1525 |
PJ_UNUSED_ARG(attr); |
| 1526 |
PJ_UNUSED_ARG(ff); |
| 1527 |
|
| 1528 |
return PJ_ENOTSUP; |
| 1529 |
} |
| 1530 |
|
| 1531 |
static pj_status_t ffmpeg_codec_get_param(pjmedia_vid_codec *codec, |
| 1532 |
pjmedia_vid_codec_param *param) |
| 1533 |
{ |
| 1534 |
ffmpeg_private *ff; |
| 1535 |
|
| 1536 |
PJ_ASSERT_RETURN(codec && param, PJ_EINVAL); |
| 1537 |
|
| 1538 |
ff = (ffmpeg_private*)codec->codec_data; |
| 1539 |
pj_memcpy(param, ff->param, sizeof(*param)); |
| 1540 |
|
| 1541 |
return PJ_SUCCESS; |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
static pj_status_t ffmpeg_packetize ( pjmedia_vid_codec *codec, |
| 1546 |
pj_uint8_t *bits, |
| 1547 |
pj_size_t bits_len, |
| 1548 |
unsigned *bits_pos, |
| 1549 |
pj_uint8_t *payload, |
| 1550 |
pj_size_t *payload_len, |
| 1551 |
pj_bool_t is_keyframe) |
| 1552 |
{ |
| 1553 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1554 |
|
| 1555 |
if (ff->desc->packetize) { |
| 1556 |
return (*ff->desc->packetize)(ff, bits, bits_len, bits_pos, |
| 1557 |
payload, payload_len, is_keyframe); |
| 1558 |
} |
| 1559 |
|
| 1560 |
return PJ_ENOTSUP; |
| 1561 |
} |
| 1562 |
|
| 1563 |
static pj_status_t ffmpeg_unpacketize(pjmedia_vid_codec *codec, |
| 1564 |
const pj_uint8_t *payload, |
| 1565 |
pj_size_t payload_len, |
| 1566 |
pj_uint8_t *bits, |
| 1567 |
pj_size_t bits_len, |
| 1568 |
unsigned *bits_pos) |
| 1569 |
{ |
| 1570 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1571 |
|
| 1572 |
if (ff->desc->unpacketize) { |
| 1573 |
return (*ff->desc->unpacketize)(ff, payload, payload_len, |
| 1574 |
bits, bits_len, bits_pos); |
| 1575 |
} |
| 1576 |
|
| 1577 |
return PJ_ENOTSUP; |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
static pj_status_t ffmpeg_codec_encode_whole(pjmedia_vid_codec *codec, |
| 1585 |
const pjmedia_vid_encode_opt *opt, |
| 1586 |
const pjmedia_frame *input, |
| 1587 |
unsigned output_buf_len, |
| 1588 |
pjmedia_frame *output) |
| 1589 |
{ |
| 1590 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1591 |
pj_uint8_t *p = (pj_uint8_t*)input->buf; |
| 1592 |
AVFrame avframe; |
| 1593 |
AVPacket avpacket; |
| 1594 |
int err, got_packet; |
| 1595 |
pj_bool_t has_key_frame = PJ_FALSE; |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
PJ_ALIGN_DATA(pj_uint32_t i[4], 16); |
| 1602 |
|
| 1603 |
if ((long)(pj_ssize_t)i & 0xF) { |
| 1604 |
PJ_LOG(2,(THIS_FILE, "Stack alignment fails")); |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
PJ_ASSERT_RETURN(ff->enc_ctx, PJ_EINVALIDOP); |
| 1609 |
|
| 1610 |
#ifdef PJMEDIA_USE_OLD_FFMPEG |
| 1611 |
avcodec_get_frame_defaults(&avframe); |
| 1612 |
#else |
| 1613 |
pj_bzero(&avframe, sizeof(avframe)); |
| 1614 |
av_frame_unref(&avframe); |
| 1615 |
#endif |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
for (i[0] = 0; i[0] < ff->enc_vfi->plane_cnt; ++i[0]) { |
| 1626 |
avframe.data[i[0]] = p; |
| 1627 |
avframe.linesize[i[0]] = ff->enc_vafp.strides[i[0]]; |
| 1628 |
p += ff->enc_vafp.plane_bytes[i[0]]; |
| 1629 |
} |
| 1630 |
|
| 1631 |
#if LIBAVCODEC_VER_AT_LEAST(58,134) |
| 1632 |
avframe.height = ff->enc_ctx->height; |
| 1633 |
avframe.width = ff->enc_ctx->width; |
| 1634 |
avframe.format = ff->enc_ctx->pix_fmt; |
| 1635 |
#endif |
| 1636 |
|
| 1637 |
|
| 1638 |
if (opt && opt->force_keyframe) { |
| 1639 |
#if LIBAVCODEC_VER_AT_LEAST(53,20) |
| 1640 |
avframe.pict_type = AV_PICTURE_TYPE_I; |
| 1641 |
#else |
| 1642 |
avframe.pict_type = FF_I_TYPE; |
| 1643 |
#endif |
| 1644 |
} |
| 1645 |
|
| 1646 |
av_init_packet(&avpacket); |
| 1647 |
avpacket.data = (pj_uint8_t*)output->buf; |
| 1648 |
avpacket.size = output_buf_len; |
| 1649 |
|
| 1650 |
#if LIBAVCODEC_VER_AT_LEAST(58,137) |
| 1651 |
PJ_UNUSED_ARG(got_packet); |
| 1652 |
err = avcodec_send_frame(ff->enc_ctx, &avframe); |
| 1653 |
if (err >= 0) { |
| 1654 |
AVPacket *pkt = NULL; |
| 1655 |
pj_uint8_t *bits_out = (pj_uint8_t*) output->buf; |
| 1656 |
unsigned out_size = 0; |
| 1657 |
pkt = av_packet_alloc(); |
| 1658 |
if (pkt) { |
| 1659 |
while (err >= 0) { |
| 1660 |
err = avcodec_receive_packet(ff->enc_ctx, pkt); |
| 1661 |
if (err == AVERROR(EAGAIN) || err == AVERROR_EOF) { |
| 1662 |
err = out_size; |
| 1663 |
break; |
| 1664 |
} |
| 1665 |
if (err >= 0) { |
| 1666 |
out_size += pkt->size; |
| 1667 |
if (out_size <= output_buf_len) { |
| 1668 |
pj_memcpy(bits_out, pkt->data, pkt->size); |
| 1669 |
bits_out += pkt->size; |
| 1670 |
} |
| 1671 |
if (pkt->flags & AV_PKT_FLAG_KEY) |
| 1672 |
has_key_frame = PJ_TRUE; |
| 1673 |
av_packet_unref(pkt); |
| 1674 |
} |
| 1675 |
} |
| 1676 |
av_packet_free(&pkt); |
| 1677 |
if (out_size > output_buf_len) { |
| 1678 |
PJ_LOG(2, (THIS_FILE, |
| 1679 |
"Output frame size (%u) exceeds buffer size (%u)", |
| 1680 |
out_size, output_buf_len)); |
| 1681 |
return PJ_ETOOSMALL; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
} |
| 1685 |
|
| 1686 |
#elif LIBAVCODEC_VER_AT_LEAST(54,15) |
| 1687 |
|
| 1688 |
err = avcodec_encode_video2(ff->enc_ctx, &avpacket, &avframe, &got_packet); |
| 1689 |
if (!err && got_packet) |
| 1690 |
err = avpacket.size; |
| 1691 |
has_key_frame = avpacket.flags & AV_PKT_FLAG_KEY; |
| 1692 |
#else |
| 1693 |
PJ_UNUSED_ARG(got_packet); |
| 1694 |
err = avcodec_encode_video(ff->enc_ctx, avpacket.data, avpacket.size, &avframe); |
| 1695 |
has_key_frame = ff->enc_ctx->coded_frame->key_frame; |
| 1696 |
#endif |
| 1697 |
|
| 1698 |
if (err < 0) { |
| 1699 |
print_ffmpeg_err(err); |
| 1700 |
return PJMEDIA_CODEC_EFAILED; |
| 1701 |
} else { |
| 1702 |
output->size = err; |
| 1703 |
output->bit_info = 0; |
| 1704 |
|
| 1705 |
if (has_key_frame) |
| 1706 |
output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME; |
| 1707 |
} |
| 1708 |
|
| 1709 |
return PJ_SUCCESS; |
| 1710 |
} |
| 1711 |
|
| 1712 |
static pj_status_t ffmpeg_codec_encode_begin(pjmedia_vid_codec *codec, |
| 1713 |
const pjmedia_vid_encode_opt *opt, |
| 1714 |
const pjmedia_frame *input, |
| 1715 |
unsigned out_size, |
| 1716 |
pjmedia_frame *output, |
| 1717 |
pj_bool_t *has_more) |
| 1718 |
{ |
| 1719 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1720 |
pj_status_t status; |
| 1721 |
|
| 1722 |
*has_more = PJ_FALSE; |
| 1723 |
|
| 1724 |
if (ff->whole) { |
| 1725 |
status = ffmpeg_codec_encode_whole(codec, opt, input, out_size, output); |
| 1726 |
} else { |
| 1727 |
pjmedia_frame whole_frm; |
| 1728 |
pj_bzero(&whole_frm, sizeof(whole_frm)); |
| 1729 |
whole_frm.buf = ff->enc_buf; |
| 1730 |
whole_frm.size = ff->enc_buf_size; |
| 1731 |
status = ffmpeg_codec_encode_whole(codec, opt, input, |
| 1732 |
(unsigned)whole_frm.size, |
| 1733 |
&whole_frm); |
| 1734 |
if (status != PJ_SUCCESS) |
| 1735 |
return status; |
| 1736 |
|
| 1737 |
ff->enc_buf_is_keyframe = (whole_frm.bit_info & |
| 1738 |
PJMEDIA_VID_FRM_KEYFRAME); |
| 1739 |
ff->enc_frame_len = (unsigned)whole_frm.size; |
| 1740 |
ff->enc_processed = 0; |
| 1741 |
status = ffmpeg_codec_encode_more(codec, out_size, output, has_more); |
| 1742 |
} |
| 1743 |
|
| 1744 |
return status; |
| 1745 |
} |
| 1746 |
|
| 1747 |
static pj_status_t ffmpeg_codec_encode_more(pjmedia_vid_codec *codec, |
| 1748 |
unsigned out_size, |
| 1749 |
pjmedia_frame *output, |
| 1750 |
pj_bool_t *has_more) |
| 1751 |
{ |
| 1752 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1753 |
pj_uint8_t *payload = (pj_uint8_t *)output->buf; |
| 1754 |
pj_size_t payload_len = (pj_size_t) out_size; |
| 1755 |
pj_status_t status; |
| 1756 |
|
| 1757 |
*has_more = PJ_FALSE; |
| 1758 |
|
| 1759 |
if (ff->enc_processed >= ff->enc_frame_len) { |
| 1760 |
|
| 1761 |
return PJ_EEOF; |
| 1762 |
} |
| 1763 |
|
| 1764 |
status = ffmpeg_packetize(codec, (pj_uint8_t*)ff->enc_buf, |
| 1765 |
ff->enc_frame_len, &ff->enc_processed, |
| 1766 |
payload, &payload_len, ff->enc_buf_is_keyframe); |
| 1767 |
if (status != PJ_SUCCESS) |
| 1768 |
return status; |
| 1769 |
|
| 1770 |
output->type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 1771 |
output->size = payload_len; |
| 1772 |
|
| 1773 |
if (ff->enc_buf_is_keyframe) |
| 1774 |
output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME; |
| 1775 |
|
| 1776 |
*has_more = (ff->enc_processed < ff->enc_frame_len); |
| 1777 |
|
| 1778 |
return PJ_SUCCESS; |
| 1779 |
} |
| 1780 |
|
| 1781 |
|
| 1782 |
static pj_status_t check_decode_result(pjmedia_vid_codec *codec, |
| 1783 |
const pj_timestamp *ts, |
| 1784 |
pj_bool_t got_keyframe) |
| 1785 |
{ |
| 1786 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1787 |
pjmedia_video_apply_fmt_param *vafp = &ff->dec_vafp; |
| 1788 |
pjmedia_event event; |
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
if (ff->dec_ctx->pix_fmt != ff->expected_dec_fmt || |
| 1795 |
ff->dec_ctx->width != (int)vafp->size.w || |
| 1796 |
ff->dec_ctx->height != (int)vafp->size.h) |
| 1797 |
{ |
| 1798 |
pjmedia_format_id new_fmt_id; |
| 1799 |
pj_status_t status; |
| 1800 |
|
| 1801 |
|
| 1802 |
status = PixelFormat_to_pjmedia_format_id(ff->dec_ctx->pix_fmt, |
| 1803 |
&new_fmt_id); |
| 1804 |
if (status != PJ_SUCCESS) |
| 1805 |
return status; |
| 1806 |
|
| 1807 |
|
| 1808 |
ff->param->dec_fmt.id = new_fmt_id; |
| 1809 |
ff->param->dec_fmt.det.vid.size.w = ff->dec_ctx->width; |
| 1810 |
ff->param->dec_fmt.det.vid.size.h = ff->dec_ctx->height; |
| 1811 |
ff->expected_dec_fmt = ff->dec_ctx->pix_fmt; |
| 1812 |
|
| 1813 |
|
| 1814 |
ff->dec_vfi = pjmedia_get_video_format_info(NULL, ff->param->dec_fmt.id); |
| 1815 |
if (!ff->dec_vfi) |
| 1816 |
return PJ_ENOTSUP; |
| 1817 |
pj_bzero(&ff->dec_vafp, sizeof(ff->dec_vafp)); |
| 1818 |
ff->dec_vafp.size = ff->param->dec_fmt.det.vid.size; |
| 1819 |
ff->dec_vafp.buffer = NULL; |
| 1820 |
status = (*ff->dec_vfi->apply_fmt)(ff->dec_vfi, &ff->dec_vafp); |
| 1821 |
if (status != PJ_SUCCESS) |
| 1822 |
return status; |
| 1823 |
|
| 1824 |
|
| 1825 |
if (ff->dec_vafp.framebytes > ff->dec_buf_size) { |
| 1826 |
PJ_LOG(5,(THIS_FILE, "Reallocating decoding buffer %u --> %u", |
| 1827 |
(unsigned)ff->dec_buf_size, |
| 1828 |
(unsigned)ff->dec_vafp.framebytes)); |
| 1829 |
ff->dec_buf_size = (unsigned)ff->dec_vafp.framebytes; |
| 1830 |
ff->dec_buf = pj_pool_alloc(ff->pool, ff->dec_buf_size); |
| 1831 |
} |
| 1832 |
|
| 1833 |
|
| 1834 |
pjmedia_event_init(&event, PJMEDIA_EVENT_FMT_CHANGED, ts, codec); |
| 1835 |
event.data.fmt_changed.dir = PJMEDIA_DIR_DECODING; |
| 1836 |
pj_memcpy(&event.data.fmt_changed.new_fmt, &ff->param->dec_fmt, |
| 1837 |
sizeof(ff->param->dec_fmt)); |
| 1838 |
pjmedia_event_publish(NULL, codec, &event, 0); |
| 1839 |
} |
| 1840 |
|
| 1841 |
|
| 1842 |
if (got_keyframe) { |
| 1843 |
pj_get_timestamp(&ff->last_dec_keyframe_ts); |
| 1844 |
|
| 1845 |
|
| 1846 |
pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_FOUND, ts, codec); |
| 1847 |
pjmedia_event_publish(NULL, codec, &event, 0); |
| 1848 |
} else if (ff->last_dec_keyframe_ts.u64 == 0) { |
| 1849 |
|
| 1850 |
pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING, ts, codec); |
| 1851 |
pjmedia_event_publish(NULL, codec, &event, 0); |
| 1852 |
} |
| 1853 |
|
| 1854 |
return PJ_SUCCESS; |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
static void ffmpeg_frame_unref(AVFrame *frame) |
| 1861 |
{ |
| 1862 |
#ifdef PJMEDIA_USE_OLD_FFMPEG |
| 1863 |
(void)frame; |
| 1864 |
#else |
| 1865 |
av_frame_unref(frame); |
| 1866 |
#endif |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
static pj_status_t ffmpeg_codec_decode_whole(pjmedia_vid_codec *codec, |
| 1873 |
const pjmedia_frame *input, |
| 1874 |
unsigned output_buf_len, |
| 1875 |
pjmedia_frame *output) |
| 1876 |
{ |
| 1877 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 1878 |
AVFrame avframe; |
| 1879 |
AVPacket avpacket; |
| 1880 |
int err, got_picture = 0; |
| 1881 |
|
| 1882 |
|
| 1883 |
PJ_ASSERT_RETURN(ff->dec_ctx, PJ_EINVALIDOP); |
| 1884 |
|
| 1885 |
|
| 1886 |
output->bit_info = 0; |
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
#ifdef PJMEDIA_USE_OLD_FFMPEG |
| 1900 |
avcodec_get_frame_defaults(&avframe); |
| 1901 |
#else |
| 1902 |
pj_bzero(&avframe, sizeof(avframe)); |
| 1903 |
av_frame_unref(&avframe); |
| 1904 |
#endif |
| 1905 |
|
| 1906 |
|
| 1907 |
av_init_packet(&avpacket); |
| 1908 |
avpacket.data = (pj_uint8_t*)input->buf; |
| 1909 |
avpacket.size = (int)input->size; |
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
#if LIBAVCODEC_VER_AT_LEAST(56,35) |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
pj_bzero(avpacket.data+avpacket.size, AV_INPUT_BUFFER_PADDING_SIZE); |
| 1924 |
#else |
| 1925 |
pj_bzero(avpacket.data+avpacket.size, FF_INPUT_BUFFER_PADDING_SIZE); |
| 1926 |
#endif |
| 1927 |
|
| 1928 |
output->bit_info = 0; |
| 1929 |
output->timestamp = input->timestamp; |
| 1930 |
|
| 1931 |
#if LIBAVCODEC_VER_AT_LEAST(52,72) |
| 1932 |
|
| 1933 |
#else |
| 1934 |
avpacket.flags = 0; |
| 1935 |
#endif |
| 1936 |
|
| 1937 |
#if LIBAVCODEC_VER_AT_LEAST(58,137) |
| 1938 |
err = avcodec_send_packet(ff->dec_ctx, &avpacket); |
| 1939 |
if (err >= 0) { |
| 1940 |
err = avcodec_receive_frame(ff->dec_ctx, &avframe); |
| 1941 |
if (err == AVERROR_EOF) |
| 1942 |
err = 0; |
| 1943 |
|
| 1944 |
if (err >= 0) { |
| 1945 |
got_picture = PJ_TRUE; |
| 1946 |
} |
| 1947 |
} |
| 1948 |
#elif LIBAVCODEC_VER_AT_LEAST(52,72) |
| 1949 |
err = avcodec_decode_video2(ff->dec_ctx, &avframe, |
| 1950 |
&got_picture, &avpacket); |
| 1951 |
#else |
| 1952 |
err = avcodec_decode_video(ff->dec_ctx, &avframe, |
| 1953 |
&got_picture, avpacket.data, avpacket.size); |
| 1954 |
#endif |
| 1955 |
if (err < 0) { |
| 1956 |
pjmedia_event event; |
| 1957 |
|
| 1958 |
output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 1959 |
output->size = 0; |
| 1960 |
print_ffmpeg_err(err); |
| 1961 |
|
| 1962 |
|
| 1963 |
pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING, |
| 1964 |
&input->timestamp, codec); |
| 1965 |
pjmedia_event_publish(NULL, codec, &event, 0); |
| 1966 |
|
| 1967 |
return PJMEDIA_CODEC_EBADBITSTREAM; |
| 1968 |
} else if (got_picture) { |
| 1969 |
pjmedia_video_apply_fmt_param *vafp = &ff->dec_vafp; |
| 1970 |
pj_uint8_t *q = (pj_uint8_t*)output->buf; |
| 1971 |
unsigned i; |
| 1972 |
pj_status_t status; |
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
#if LIBAVUTIL_VER_AT_LEAST(58,7) |
| 1978 |
status = check_decode_result(codec, &input->timestamp, |
| 1979 |
!!(avframe.flags & AV_FRAME_FLAG_KEY)); |
| 1980 |
#else |
| 1981 |
status = check_decode_result(codec, &input->timestamp, |
| 1982 |
avframe.key_frame); |
| 1983 |
#endif |
| 1984 |
|
| 1985 |
if (status != PJ_SUCCESS) { |
| 1986 |
ffmpeg_frame_unref(&avframe); |
| 1987 |
return status; |
| 1988 |
} |
| 1989 |
|
| 1990 |
|
| 1991 |
if (vafp->framebytes > output_buf_len) { |
| 1992 |
ffmpeg_frame_unref(&avframe); |
| 1993 |
return PJ_ETOOSMALL; |
| 1994 |
} |
| 1995 |
|
| 1996 |
|
| 1997 |
for (i = 0; i < ff->dec_vfi->plane_cnt; ++i) { |
| 1998 |
pj_uint8_t *p = avframe.data[i]; |
| 1999 |
|
| 2000 |
|
| 2001 |
if (avframe.linesize[i]!=vafp->strides[i]) { |
| 2002 |
|
| 2003 |
pj_uint8_t *q_end; |
| 2004 |
|
| 2005 |
q_end = q+vafp->plane_bytes[i]; |
| 2006 |
while(q < q_end) { |
| 2007 |
pj_memcpy(q, p, vafp->strides[i]); |
| 2008 |
q += vafp->strides[i]; |
| 2009 |
p += avframe.linesize[i]; |
| 2010 |
} |
| 2011 |
} else { |
| 2012 |
|
| 2013 |
pj_memcpy(q, p, vafp->plane_bytes[i]); |
| 2014 |
q += vafp->plane_bytes[i]; |
| 2015 |
} |
| 2016 |
} |
| 2017 |
|
| 2018 |
output->type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 2019 |
output->size = vafp->framebytes; |
| 2020 |
ffmpeg_frame_unref(&avframe); |
| 2021 |
} else { |
| 2022 |
output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 2023 |
output->size = 0; |
| 2024 |
} |
| 2025 |
|
| 2026 |
return PJ_SUCCESS; |
| 2027 |
} |
| 2028 |
|
| 2029 |
static pj_status_t ffmpeg_codec_decode( pjmedia_vid_codec *codec, |
| 2030 |
pj_size_t pkt_count, |
| 2031 |
pjmedia_frame packets[], |
| 2032 |
unsigned out_size, |
| 2033 |
pjmedia_frame *output) |
| 2034 |
{ |
| 2035 |
ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data; |
| 2036 |
pj_status_t status; |
| 2037 |
|
| 2038 |
PJ_ASSERT_RETURN(codec && pkt_count > 0 && packets && output, |
| 2039 |
PJ_EINVAL); |
| 2040 |
|
| 2041 |
if (ff->whole) { |
| 2042 |
pj_assert(pkt_count==1); |
| 2043 |
return ffmpeg_codec_decode_whole(codec, &packets[0], out_size, output); |
| 2044 |
} else { |
| 2045 |
pjmedia_frame whole_frm; |
| 2046 |
unsigned whole_len = 0; |
| 2047 |
unsigned i; |
| 2048 |
|
| 2049 |
for (i=0; i<pkt_count; ++i) { |
| 2050 |
if (whole_len + packets[i].size > ff->dec_buf_size) { |
| 2051 |
PJ_LOG(5,(THIS_FILE, "Decoding buffer overflow")); |
| 2052 |
break; |
| 2053 |
} |
| 2054 |
|
| 2055 |
status = ffmpeg_unpacketize(codec, packets[i].buf, packets[i].size, |
| 2056 |
ff->dec_buf, ff->dec_buf_size, |
| 2057 |
&whole_len); |
| 2058 |
if (status != PJ_SUCCESS) { |
| 2059 |
PJ_PERROR(5,(THIS_FILE, status, "Unpacketize error")); |
| 2060 |
continue; |
| 2061 |
} |
| 2062 |
} |
| 2063 |
|
| 2064 |
whole_frm.buf = ff->dec_buf; |
| 2065 |
whole_frm.size = whole_len; |
| 2066 |
whole_frm.timestamp = output->timestamp = packets[i].timestamp; |
| 2067 |
whole_frm.bit_info = 0; |
| 2068 |
|
| 2069 |
return ffmpeg_codec_decode_whole(codec, &whole_frm, out_size, output); |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
|
| 2074 |
#ifdef _MSC_VER |
| 2075 |
# pragma comment( lib, "avcodec.lib") |
| 2076 |
#endif |
| 2077 |
|
| 2078 |
#endif |
| 2079 |
|