1
/* $Id: vid_codec_util.c 4362 2013-02-21 14:51:56Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjmedia/vid_codec_util.h>
21
#include <pjmedia/errno.h>
22
#include <pjmedia/stream_common.h>
23
#include <pjlib-util/base64.h>
28
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
31
#define THIS_FILE "vid_codec_util.c"
33
/* If this is set to non-zero, H.264 custom negotiation will require
34
* "profile-level-id" and "packetization-mode" to be exact match to
35
* get a successful negotiation. Note that flexible answer (updating
36
* SDP answer to match remote offer) is always active regardless the
37
* value of this macro.
39
#define H264_STRICT_SDP_NEGO 0
41
/* Default frame rate, if not specified */
42
#define DEFAULT_H264_FPS_NUM 15
43
#define DEFAULT_H264_FPS_DENUM 1
45
/* Default aspect ratio, if not specified */
46
#define DEFAULT_H264_RATIO_NUM 16
47
#define DEFAULT_H264_RATIO_DENUM 9
50
/* ITU resolution definition */
51
struct mpi_resolution_t
54
pjmedia_rect_size size;
58
{{"CIF",3}, {352,288}},
59
{{"QCIF",4}, {176,144}},
60
{{"SQCIF",5}, {88,72}},
61
{{"CIF4",4}, {704,576}},
62
{{"CIF16",5}, {1408,1142}},
66
#define CALC_H264_MB_NUM(size) (((size.w+15)/16)*((size.h+15)/16))
67
#define CALC_H264_MBPS(size,fps) CALC_H264_MB_NUM(size)*fps.num/fps.denum
70
/* Parse fmtp value for custom resolution, e.g: "CUSTOM=800,600,2" */
71
static pj_status_t parse_custom_res_fmtp(const pj_str_t *fmtp_val,
72
pjmedia_rect_size *size,
75
const char *p, *p_end;
77
unsigned long val[3] = {0};
80
p = token.ptr = fmtp_val->ptr;
81
p_end = p + fmtp_val->slen;
83
while (p<=p_end && i<PJ_ARRAY_SIZE(val)) {
84
if (*p==',' || p==p_end) {
85
token.slen = (char*)p - token.ptr;
86
val[i++] = pj_strtoul(&token);
87
token.ptr = (char*)p+1;
92
if (!val[0] || !val[1])
95
if (val[2]<1 || val[2]>32)
105
/* H263 fmtp parser */
106
PJ_DEF(pj_status_t) pjmedia_vid_codec_parse_h263_fmtp(
107
const pjmedia_codec_fmtp *fmtp,
108
pjmedia_vid_codec_h263_fmtp *h263_fmtp)
110
const pj_str_t CUSTOM = {"CUSTOM", 6};
113
pj_bzero(h263_fmtp, sizeof(*h263_fmtp));
115
for (i=0; i<fmtp->cnt; ++i) {
117
pj_bool_t parsed = PJ_FALSE;
119
if (h263_fmtp->mpi_cnt >= PJ_ARRAY_SIZE(h263_fmtp->mpi)) {
120
pj_assert(!"Too small MPI array in H263 fmtp");
124
/* Standard size MPIs */
125
for (j=0; j<PJ_ARRAY_SIZE(mpi_resolutions) && !parsed; ++j) {
126
if (pj_stricmp(&fmtp->param[i].name, &mpi_resolutions[j].name)==0)
130
mpi = pj_strtoul(&fmtp->param[i].val);
132
return PJMEDIA_SDP_EINFMTP;
134
h263_fmtp->mpi[h263_fmtp->mpi_cnt].size =
135
mpi_resolutions[j].size;
136
h263_fmtp->mpi[h263_fmtp->mpi_cnt].val = mpi;
137
++h263_fmtp->mpi_cnt;
144
/* Custom size MPIs */
145
if (pj_stricmp(&fmtp->param[i].name, &CUSTOM)==0) {
146
pjmedia_rect_size size;
150
status = parse_custom_res_fmtp(&fmtp->param[i].val, &size, &mpi);
151
if (status != PJ_SUCCESS)
152
return PJMEDIA_SDP_EINFMTP;
154
h263_fmtp->mpi[h263_fmtp->mpi_cnt].size = size;
155
h263_fmtp->mpi[h263_fmtp->mpi_cnt].val = mpi;
156
++h263_fmtp->mpi_cnt;
164
static unsigned fps_to_mpi(const pjmedia_ratio *fps)
168
/* Original formula = (fps->denum * 30000) / (fps->num * 1001) */
169
mpi = (fps->denum*30000 + fps->num*1001/2) / (fps->num*1001);
171
/* Normalize, should be in the range of 1-32 */
172
if (mpi > 32) mpi = 32;
173
if (mpi < 1) mpi = 1;
178
PJ_DEF(pj_status_t) pjmedia_vid_codec_h263_apply_fmtp(
179
pjmedia_vid_codec_param *param)
181
if (param->dir & PJMEDIA_DIR_ENCODING) {
182
pjmedia_vid_codec_h263_fmtp fmtp_loc, fmtp_rem;
183
pjmedia_rect_size size = {0};
185
pjmedia_video_format_detail *vfd;
188
vfd = pjmedia_format_get_video_format_detail(¶m->enc_fmt,
191
/* Get local param */
192
// Local param should be fetched from "param->enc_fmt" instead of
193
// "param->dec_fmtp".
194
//status = pjmedia_vid_codec_parse_h263_fmtp(¶m->dec_fmtp,
196
//if (status != PJ_SUCCESS)
198
fmtp_loc.mpi_cnt = 1;
199
fmtp_loc.mpi[0].size = vfd->size;
200
fmtp_loc.mpi[0].val = fps_to_mpi(&vfd->fps);
202
/* Get remote param */
203
status = pjmedia_vid_codec_parse_h263_fmtp(¶m->enc_fmtp,
205
if (status != PJ_SUCCESS)
208
/* Negotiate size & MPI setting */
209
if (fmtp_rem.mpi_cnt == 0) {
210
/* Remote doesn't specify MPI setting, send QCIF=1 */
214
//} else if (fmtp_loc.mpi_cnt == 0) {
215
// /* Local MPI setting not set, just use remote preference. */
216
// size = fmtp_rem.mpi[0].size;
217
// mpi = fmtp_rem.mpi[0].val;
219
/* Both have preferences, let's try to match them */
221
pj_bool_t matched = PJ_FALSE;
222
pj_uint32_t min_diff = 0xFFFFFFFF;
223
pj_uint32_t loc_sq, rem_sq, diff;
225
/* Find the exact size match or the closest size, then choose
226
* the highest MPI among the match/closest pair.
228
for (i = 0; i < fmtp_rem.mpi_cnt && !matched; ++i) {
229
rem_sq = fmtp_rem.mpi[i].size.w * fmtp_rem.mpi[i].size.h;
230
for (j = 0; j < fmtp_loc.mpi_cnt; ++j) {
231
/* See if we got exact match */
232
if (fmtp_rem.mpi[i].size.w == fmtp_loc.mpi[j].size.w &&
233
fmtp_rem.mpi[i].size.h == fmtp_loc.mpi[j].size.h)
235
size = fmtp_rem.mpi[i].size;
236
mpi = PJ_MAX(fmtp_rem.mpi[i].val,
237
fmtp_loc.mpi[j].val);
242
/* Otherwise keep looking for the closest match */
243
loc_sq = fmtp_loc.mpi[j].size.w * fmtp_loc.mpi[j].size.h;
244
diff = loc_sq>rem_sq? (loc_sq-rem_sq):(rem_sq-loc_sq);
245
if (diff < min_diff) {
246
size = rem_sq<loc_sq? fmtp_rem.mpi[i].size :
247
fmtp_loc.mpi[j].size;
248
mpi = PJ_MAX(fmtp_rem.mpi[i].val,
249
fmtp_loc.mpi[j].val);
255
/* Apply the negotiation result */
257
vfd->fps.num = 30000;
258
vfd->fps.denum = 1001 * mpi;
261
if (param->dir & PJMEDIA_DIR_DECODING) {
262
/* Here we just want to find the highest resolution and the lowest MPI
263
* we support and set it as the decoder param.
265
pjmedia_vid_codec_h263_fmtp fmtp;
266
pjmedia_video_format_detail *vfd;
269
status = pjmedia_vid_codec_parse_h263_fmtp(¶m->dec_fmtp,
271
if (status != PJ_SUCCESS)
274
vfd = pjmedia_format_get_video_format_detail(¶m->dec_fmt,
277
if (fmtp.mpi_cnt == 0) {
278
/* No resolution specified, lets just assume 4CIF=1! */
281
vfd->fps.num = 30000;
282
vfd->fps.denum = 1001;
284
unsigned i, max_size = 0, max_size_idx = 0, min_mpi = 32;
286
/* Get the largest size and the lowest MPI */
287
for (i = 0; i < fmtp.mpi_cnt; ++i) {
288
if (fmtp.mpi[i].size.w * fmtp.mpi[i].size.h > max_size) {
289
max_size = fmtp.mpi[i].size.w * fmtp.mpi[i].size.h;
292
if (fmtp.mpi[i].val < min_mpi)
293
min_mpi = fmtp.mpi[i].val;
296
vfd->size = fmtp.mpi[max_size_idx].size;
297
vfd->fps.num = 30000;
298
vfd->fps.denum = 1001 * min_mpi;
306
/* Declaration of H.264 level info */
307
typedef struct h264_level_info_t
309
unsigned id; /* Level id. */
310
unsigned max_mbps; /* Max macroblocks per second. */
311
unsigned max_mb; /* Max macroblocks. */
312
unsigned max_br; /* Max bitrate (kbps). */
316
/* Init H264 parameters based on profile-level-id */
317
static pj_status_t init_h264_profile(const pj_str_t *profile,
318
pjmedia_vid_codec_h264_fmtp *fmtp)
320
const h264_level_info_t level_info[] =
322
{ 10, 1485, 99, 64 },
323
{ 9, 1485, 99, 128 }, /*< level 1b */
324
{ 11, 3000, 396, 192 },
325
{ 12, 6000, 396, 384 },
326
{ 13, 11880, 396, 768 },
327
{ 20, 11880, 396, 2000 },
328
{ 21, 19800, 792, 4000 },
329
{ 22, 20250, 1620, 4000 },
330
{ 30, 40500, 1620, 10000 },
331
{ 31, 108000, 3600, 14000 },
332
{ 32, 216000, 5120, 20000 },
333
{ 40, 245760, 8192, 20000 },
334
{ 41, 245760, 8192, 50000 },
335
{ 42, 522240, 8704, 50000 },
336
{ 50, 589824, 22080, 135000 },
337
{ 51, 983040, 36864, 240000 },
341
const h264_level_info_t *li = NULL;
343
if (profile->slen != 6)
344
return PJMEDIA_SDP_EINFMTP;
346
tmp = pj_strtoul2(profile, &endst, 16);
348
return PJMEDIA_SDP_EINFMTP;
350
fmtp->profile_idc = (pj_uint8_t)((tmp >> 16) & 0xFF);
351
fmtp->profile_iop = (pj_uint8_t)((tmp >> 8) & 0xFF);
352
fmtp->level = (pj_uint8_t)(tmp & 0xFF);
354
for (i = 0; i < PJ_ARRAY_SIZE(level_info); ++i) {
355
if (level_info[i].id == fmtp->level) {
361
return PJMEDIA_SDP_EINFMTP;
363
/* Init profile level spec */
364
if (fmtp->max_br == 0)
365
fmtp->max_br = li->max_br;
366
if (fmtp->max_mbps == 0)
367
fmtp->max_mbps = li->max_mbps;
368
if (fmtp->max_fs == 0)
369
fmtp->max_fs = li->max_mb;
375
/* H264 fmtp parser */
376
PJ_DEF(pj_status_t) pjmedia_vid_codec_h264_parse_fmtp(
377
const pjmedia_codec_fmtp *fmtp,
378
pjmedia_vid_codec_h264_fmtp *h264_fmtp)
380
const pj_str_t PROFILE_LEVEL_ID = {"profile-level-id", 16};
381
const pj_str_t MAX_MBPS = {"max-mbps", 8};
382
const pj_str_t MAX_FS = {"max-fs", 6};
383
const pj_str_t MAX_CPB = {"max-cpb", 7};
384
const pj_str_t MAX_DPB = {"max-dpb", 7};
385
const pj_str_t MAX_BR = {"max-br", 6};
386
const pj_str_t PACKETIZATION_MODE = {"packetization-mode", 18};
387
const pj_str_t SPROP_PARAMETER_SETS = {"sprop-parameter-sets", 20};
391
pj_bzero(h264_fmtp, sizeof(*h264_fmtp));
393
for (i=0; i<fmtp->cnt; ++i) {
395
if (pj_stricmp(&fmtp->param[i].name, &PROFILE_LEVEL_ID)==0) {
396
/* Init H264 parameters based on level, if not set yet */
397
status = init_h264_profile(&fmtp->param[i].val, h264_fmtp);
398
if (status != PJ_SUCCESS)
400
} else if (pj_stricmp(&fmtp->param[i].name, &PACKETIZATION_MODE)==0) {
401
tmp = pj_strtoul(&fmtp->param[i].val);
402
if (tmp >= 0 && tmp <= 2)
403
h264_fmtp->packetization_mode = (pj_uint8_t)tmp;
405
return PJMEDIA_SDP_EINFMTP;
406
} else if (pj_stricmp(&fmtp->param[i].name, &MAX_MBPS)==0) {
407
tmp = pj_strtoul(&fmtp->param[i].val);
408
h264_fmtp->max_mbps = PJ_MAX(tmp, h264_fmtp->max_mbps);
409
} else if (pj_stricmp(&fmtp->param[i].name, &MAX_FS)==0) {
410
tmp = pj_strtoul(&fmtp->param[i].val);
411
h264_fmtp->max_fs = PJ_MAX(tmp, h264_fmtp->max_fs);
412
} else if (pj_stricmp(&fmtp->param[i].name, &MAX_CPB)==0) {
413
tmp = pj_strtoul(&fmtp->param[i].val);
414
h264_fmtp->max_cpb = PJ_MAX(tmp, h264_fmtp->max_cpb);
415
} else if (pj_stricmp(&fmtp->param[i].name, &MAX_DPB)==0) {
416
tmp = pj_strtoul(&fmtp->param[i].val);
417
h264_fmtp->max_dpb = PJ_MAX(tmp, h264_fmtp->max_dpb);
418
} else if (pj_stricmp(&fmtp->param[i].name, &MAX_BR)==0) {
419
tmp = pj_strtoul(&fmtp->param[i].val);
420
h264_fmtp->max_br = PJ_MAX(tmp, h264_fmtp->max_br);
421
} else if (pj_stricmp(&fmtp->param[i].name, &SPROP_PARAMETER_SETS)==0)
425
sps_st = fmtp->param[i].val;
426
while (sps_st.slen) {
429
const pj_uint8_t start_code[3] = {0, 0, 1};
434
/* Find field separator ',' */
436
p = pj_strchr(&sps_st, ',');
438
tmp_st.slen = p - sps_st.ptr;
440
sps_st.slen -= (tmp_st.slen+1);
445
/* Decode field and build NAL unit for this param */
446
nal = &h264_fmtp->sprop_param_sets[
447
h264_fmtp->sprop_param_sets_len];
448
tmp_len = PJ_ARRAY_SIZE(h264_fmtp->sprop_param_sets) -
449
h264_fmtp->sprop_param_sets_len -
450
PJ_ARRAY_SIZE(start_code);
451
status = pj_base64_decode(&tmp_st,
452
nal + PJ_ARRAY_SIZE(start_code),
454
if (status != PJ_SUCCESS)
455
return PJMEDIA_SDP_EINFMTP;
457
tmp_len += PJ_ARRAY_SIZE(start_code);
458
pj_memcpy(nal, start_code, PJ_ARRAY_SIZE(start_code));
459
h264_fmtp->sprop_param_sets_len += tmp_len;
464
/* When profile-level-id is not specified, use default value "42000A" */
465
if (h264_fmtp->profile_idc == 0) {
466
h264_fmtp->profile_idc = 0x42;
467
h264_fmtp->profile_iop = 0x00;
468
h264_fmtp->level = 0x0A;
474
PJ_DEF(pj_status_t) pjmedia_vid_codec_h264_match_sdp(pj_pool_t *pool,
475
pjmedia_sdp_media *offer,
477
pjmedia_sdp_media *answer,
481
const pj_str_t PROFILE_LEVEL_ID = {"profile-level-id", 16};
482
const pj_str_t PACKETIZATION_MODE = {"packetization-mode", 18};
483
pjmedia_codec_fmtp o_fmtp_raw, a_fmtp_raw;
484
pjmedia_vid_codec_h264_fmtp o_fmtp, a_fmtp;
490
status = pjmedia_stream_info_parse_fmtp(
492
pj_strtoul(&offer->desc.fmt[o_fmt_idx]),
494
if (status != PJ_SUCCESS)
497
status = pjmedia_vid_codec_h264_parse_fmtp(&o_fmtp_raw, &o_fmtp);
498
if (status != PJ_SUCCESS)
502
status = pjmedia_stream_info_parse_fmtp(
504
pj_strtoul(&answer->desc.fmt[a_fmt_idx]),
506
if (status != PJ_SUCCESS)
509
status = pjmedia_vid_codec_h264_parse_fmtp(&a_fmtp_raw, &a_fmtp);
510
if (status != PJ_SUCCESS)
513
if (option & PJMEDIA_SDP_NEG_FMT_MATCH_ALLOW_MODIFY_ANSWER) {
516
/* Flexible negotiation, if the answer has higher capability than
517
* the offer, adjust the answer capability to be match to the offer.
519
if (a_fmtp.profile_idc >= o_fmtp.profile_idc)
520
a_fmtp.profile_idc = o_fmtp.profile_idc;
521
if (a_fmtp.profile_iop != o_fmtp.profile_iop)
522
a_fmtp.profile_iop = o_fmtp.profile_iop;
523
if (a_fmtp.level >= o_fmtp.level)
524
a_fmtp.level = o_fmtp.level;
525
if (a_fmtp.packetization_mode >= o_fmtp.packetization_mode)
526
a_fmtp.packetization_mode = o_fmtp.packetization_mode;
529
#if H264_STRICT_SDP_NEGO
530
if (a_fmtp.profile_idc != o_fmtp.profile_idc ||
531
a_fmtp.profile_iop != o_fmtp.profile_iop ||
532
a_fmtp.level != o_fmtp.level ||
533
a_fmtp.packetization_mode != o_fmtp.packetization_mode)
535
return PJMEDIA_SDP_EFORMATNOTEQUAL;
538
if (a_fmtp.profile_idc != o_fmtp.profile_idc)
540
return PJMEDIA_SDP_EFORMATNOTEQUAL;
544
/* Update the answer */
545
for (i = 0; i < a_fmtp_raw.cnt; ++i) {
546
if (pj_stricmp(&a_fmtp_raw.param[i].name, &PROFILE_LEVEL_ID) == 0)
548
char *p = a_fmtp_raw.param[i].val.ptr;
549
pj_val_to_hex_digit(a_fmtp.profile_idc, p);
551
pj_val_to_hex_digit(a_fmtp.profile_iop, p);
553
pj_val_to_hex_digit(a_fmtp.level, p);
555
else if (pj_stricmp(&a_fmtp_raw.param[i].name, &PACKETIZATION_MODE) == 0)
557
char *p = a_fmtp_raw.param[i].val.ptr;
558
*p = '0' + a_fmtp.packetization_mode;
562
#if H264_STRICT_SDP_NEGO
563
/* Strict negotiation */
564
if (a_fmtp.profile_idc != o_fmtp.profile_idc ||
565
a_fmtp.profile_iop != o_fmtp.profile_iop ||
566
a_fmtp.level != o_fmtp.level ||
567
a_fmtp.packetization_mode != o_fmtp.packetization_mode)
569
return PJMEDIA_SDP_EFORMATNOTEQUAL;
572
/* Permissive negotiation */
573
if (a_fmtp.profile_idc != o_fmtp.profile_idc)
575
return PJMEDIA_SDP_EFORMATNOTEQUAL;
584
/* Find greatest common divisor (GCD) */
585
static unsigned gcd (unsigned a, unsigned b) {
595
/* Find highest resolution possible for the specified H264 fmtp and
598
static pj_status_t find_highest_res(pjmedia_vid_codec_h264_fmtp *fmtp,
599
const pjmedia_ratio *fps,
600
const pjmedia_ratio *ratio,
601
pjmedia_rect_size *size)
603
pjmedia_ratio def_ratio = { DEFAULT_H264_RATIO_NUM,
604
DEFAULT_H264_RATIO_DENUM };
605
pjmedia_ratio def_fps = { DEFAULT_H264_FPS_NUM,
606
DEFAULT_H264_FPS_DENUM };
607
pjmedia_ratio asp_ratio, the_fps;
608
unsigned max_fs, g, scale;
612
/* Get the ratio, or just use default if not provided. */
613
if (ratio && ratio->num && ratio->denum) {
616
asp_ratio = def_ratio;
619
/* Normalize the aspect ratio */
620
g = gcd(asp_ratio.num, asp_ratio.denum);
622
asp_ratio.denum /= g;
624
/* Get the frame rate, or just use default if not provided. */
625
if (fps && fps->num && fps->denum) {
631
/* Calculate maximum size (in macroblocks) */
632
max_fs = fmtp->max_mbps * fps->denum / fps->num;
633
max_fs = PJ_MIN(max_fs, fmtp->max_fs);
635
/* Check if the specified ratio is using big numbers
636
* (not normalizable), override it with default ratio!
638
if ((int)max_fs < asp_ratio.num * asp_ratio.denum)
639
asp_ratio = def_ratio;
641
/* Calculate the scale factor for size */
642
scale = pj_isqrt(max_fs / asp_ratio.denum / asp_ratio.num);
644
/* Calculate the size, note that the frame size is in macroblock units */
645
size->w = asp_ratio.num * scale * 16;
646
size->h = asp_ratio.denum * scale * 16;
652
PJ_DEF(pj_status_t) pjmedia_vid_codec_h264_apply_fmtp(
653
pjmedia_vid_codec_param *param)
655
if (param->dir & PJMEDIA_DIR_ENCODING) {
656
pjmedia_vid_codec_h264_fmtp fmtp;
657
pjmedia_video_format_detail *vfd;
660
/* Get remote param */
661
status = pjmedia_vid_codec_h264_parse_fmtp(¶m->enc_fmtp,
663
if (status != PJ_SUCCESS)
666
/* Adjust fps, size, and bitrate to conform to H.264 level
667
* specified by remote SDP fmtp.
669
vfd = pjmedia_format_get_video_format_detail(¶m->enc_fmt,
672
if (vfd->fps.num == 0 || vfd->fps.denum == 0) {
673
vfd->fps.num = DEFAULT_H264_FPS_NUM;
674
vfd->fps.denum = DEFAULT_H264_FPS_DENUM;
677
if (vfd->size.w && vfd->size.h) {
680
/* Scale down the resolution if it exceeds profile spec */
681
mb = CALC_H264_MB_NUM(vfd->size);
682
mbps = CALC_H264_MBPS(vfd->size, vfd->fps);
683
if (mb > fmtp.max_fs || mbps > fmtp.max_mbps) {
686
r.denum = vfd->size.h;
687
find_highest_res(&fmtp, &vfd->fps, &r, &vfd->size);
690
/* When not specified, just use the highest res possible*/
693
r.denum = vfd->size.h;
694
find_highest_res(&fmtp, &vfd->fps, &r, &vfd->size);
697
/* Encoding bitrate must not be higher than H264 level spec */
698
if (vfd->avg_bps > fmtp.max_br * 1000)
699
vfd->avg_bps = fmtp.max_br * 1000;
700
if (vfd->max_bps > fmtp.max_br * 1000)
701
vfd->max_bps = fmtp.max_br * 1000;
704
if (param->dir & PJMEDIA_DIR_DECODING) {
705
/* Here we just want to find the highest resolution possible from the
706
* fmtp and set it as the decoder param.
708
pjmedia_vid_codec_h264_fmtp fmtp;
709
pjmedia_video_format_detail *vfd;
711
pjmedia_rect_size highest_size;
714
status = pjmedia_vid_codec_h264_parse_fmtp(¶m->dec_fmtp,
716
if (status != PJ_SUCCESS)
719
vfd = pjmedia_format_get_video_format_detail(¶m->dec_fmt,
722
if (vfd->fps.num == 0 || vfd->fps.denum == 0) {
723
vfd->fps.num = DEFAULT_H264_FPS_NUM;
724
vfd->fps.denum = DEFAULT_H264_FPS_DENUM;
727
/* Normalize decoding resolution, i.e: it must not be lower than
728
* the H264 profile level setting used, as this may be used by
729
* app to allocate buffer.
732
r.denum = vfd->size.h;
733
find_highest_res(&fmtp, &vfd->fps, &r, &highest_size);
734
if (vfd->size.w * vfd->size.h < highest_size.w * highest_size.h)
735
vfd->size = highest_size;
737
/* Normalize decoding bitrate based on H264 level spec */
738
if (vfd->avg_bps < fmtp.max_br * 1000)
739
vfd->avg_bps = fmtp.max_br * 1000;
740
if (vfd->max_bps < fmtp.max_br * 1000)
741
vfd->max_bps = fmtp.max_br * 1000;
748
#endif /* PJMEDIA_HAS_VIDEO */