Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/openh264.cpp

raw
1 /*
2 * Copyright (C)2014 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjmedia-codec/openh264.h>
19 #include <pjmedia-codec/h264_packetizer.h>
20 #include <pjmedia/vid_codec_util.h>
21 #include <pjmedia/errno.h>
22 #include <pj/log.h>
23
24 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && \
25 PJMEDIA_HAS_OPENH264_CODEC != 0 && \
26 defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
27
28 #ifdef _MSC_VER
29 # include <stdint.h>
30 # pragma comment( lib, "openh264.lib")
31 #endif
32
33 /* OpenH264: */
34 #include <wels/codec_api.h>
35 #include <wels/codec_app_def.h>
36
37 /*
38 * Constants
39 */
40 #define THIS_FILE "openh264.cpp"
41
42 #if (defined(PJ_DARWINOS) && PJ_DARWINOS != 0 && TARGET_OS_IPHONE) || \
43 defined(__ANDROID__)
44 # define DEFAULT_WIDTH 352
45 # define DEFAULT_HEIGHT 288
46 #else
47 # define DEFAULT_WIDTH 720
48 # define DEFAULT_HEIGHT 480
49 #endif
50
51 #define DEFAULT_FPS 15
52 #define DEFAULT_AVG_BITRATE 256000
53 #define DEFAULT_MAX_BITRATE 256000
54
55 #define MAX_RX_WIDTH 1200
56 #define MAX_RX_HEIGHT 800
57
58 /* OpenH264 default PT */
59 #define OH264_PT PJMEDIA_RTP_PT_H264
60
61 /* Minimum interval (in msec) between generating two missing keyframe events.
62 * This is to avoid sending too many events during consecutive decode
63 * failures.
64 */
65 #define MISSING_KEYFRAME_EV_MIN_INTERVAL 1000
66
67 /*
68 * Factory operations.
69 */
70 static pj_status_t oh264_test_alloc(pjmedia_vid_codec_factory *factory,
71 const pjmedia_vid_codec_info *info );
72 static pj_status_t oh264_default_attr(pjmedia_vid_codec_factory *factory,
73 const pjmedia_vid_codec_info *info,
74 pjmedia_vid_codec_param *attr );
75 static pj_status_t oh264_enum_info(pjmedia_vid_codec_factory *factory,
76 unsigned *count,
77 pjmedia_vid_codec_info codecs[]);
78 static pj_status_t oh264_alloc_codec(pjmedia_vid_codec_factory *factory,
79 const pjmedia_vid_codec_info *info,
80 pjmedia_vid_codec **p_codec);
81 static pj_status_t oh264_dealloc_codec(pjmedia_vid_codec_factory *factory,
82 pjmedia_vid_codec *codec );
83
84
85 /*
86 * Codec operations
87 */
88 static pj_status_t oh264_codec_init(pjmedia_vid_codec *codec,
89 pj_pool_t *pool );
90 static pj_status_t oh264_codec_open(pjmedia_vid_codec *codec,
91 pjmedia_vid_codec_param *param );
92 static pj_status_t oh264_codec_close(pjmedia_vid_codec *codec);
93 static pj_status_t oh264_codec_modify(pjmedia_vid_codec *codec,
94 const pjmedia_vid_codec_param *param);
95 static pj_status_t oh264_codec_get_param(pjmedia_vid_codec *codec,
96 pjmedia_vid_codec_param *param);
97 static pj_status_t oh264_codec_encode_begin(pjmedia_vid_codec *codec,
98 const pjmedia_vid_encode_opt *opt,
99 const pjmedia_frame *input,
100 unsigned out_size,
101 pjmedia_frame *output,
102 pj_bool_t *has_more);
103 static pj_status_t oh264_codec_encode_more(pjmedia_vid_codec *codec,
104 unsigned out_size,
105 pjmedia_frame *output,
106 pj_bool_t *has_more);
107 static pj_status_t oh264_codec_decode(pjmedia_vid_codec *codec,
108 pj_size_t count,
109 pjmedia_frame packets[],
110 unsigned out_size,
111 pjmedia_frame *output);
112
113 /* Definition for OpenH264 codecs operations. */
114 static pjmedia_vid_codec_op oh264_codec_op =
115 {
116 &oh264_codec_init,
117 &oh264_codec_open,
118 &oh264_codec_close,
119 &oh264_codec_modify,
120 &oh264_codec_get_param,
121 &oh264_codec_encode_begin,
122 &oh264_codec_encode_more,
123 &oh264_codec_decode,
124 NULL
125 };
126
127 /* Definition for OpenH264 codecs factory operations. */
128 static pjmedia_vid_codec_factory_op oh264_factory_op =
129 {
130 &oh264_test_alloc,
131 &oh264_default_attr,
132 &oh264_enum_info,
133 &oh264_alloc_codec,
134 &oh264_dealloc_codec
135 };
136
137
138 static struct oh264_factory
139 {
140 pjmedia_vid_codec_factory base;
141 pjmedia_vid_codec_mgr *mgr;
142 pj_pool_factory *pf;
143 pj_pool_t *pool;
144 } oh264_factory;
145
146
147 typedef struct oh264_codec_data
148 {
149 pj_pool_t *pool;
150 pjmedia_vid_codec_param *prm;
151 pj_bool_t whole;
152 pjmedia_h264_packetizer *pktz;
153
154 /* Encoder state */
155 ISVCEncoder *enc;
156 SSourcePicture *esrc_pic;
157 unsigned enc_input_size;
158 pj_uint8_t *enc_frame_whole;
159 unsigned enc_frame_size;
160 unsigned enc_processed;
161 pj_timestamp ets;
162 SFrameBSInfo bsi;
163 int ilayer;
164
165 /* Decoder state */
166 ISVCDecoder *dec;
167 pj_uint8_t *dec_buf;
168 unsigned dec_buf_size;
169 unsigned missing_kf_interval;
170 unsigned last_missing_kf_event;
171 } oh264_codec_data;
172
173 struct SLayerPEncCtx
174 {
175 pj_int32_t iDLayerQp;
176 SSliceArgument sSliceArgument;
177 };
178
179 static void log_print(void* ctx, int level, const char* string) {
180 PJ_UNUSED_ARG(ctx);
181 PJ_LOG(4,("[OPENH264_LOG]", "[L%d] %s", level, string));
182 }
183
184 PJ_DEF(pj_status_t) pjmedia_codec_openh264_vid_init(pjmedia_vid_codec_mgr *mgr,
185 pj_pool_factory *pf)
186 {
187 const pj_str_t h264_name = { (char*)"H264", 4};
188 pj_status_t status;
189
190 if (oh264_factory.pool != NULL) {
191 /* Already initialized. */
192 return PJ_SUCCESS;
193 }
194
195 if (!mgr) mgr = pjmedia_vid_codec_mgr_instance();
196 PJ_ASSERT_RETURN(mgr, PJ_EINVAL);
197
198 /* Create OpenH264 codec factory. */
199 oh264_factory.base.op = &oh264_factory_op;
200 oh264_factory.base.factory_data = NULL;
201 oh264_factory.mgr = mgr;
202 oh264_factory.pf = pf;
203 oh264_factory.pool = pj_pool_create(pf, "oh264factory", 256, 256, NULL);
204 if (!oh264_factory.pool)
205 return PJ_ENOMEM;
206
207 /* Registering format match for SDP negotiation */
208 status = pjmedia_sdp_neg_register_fmt_match_cb(
209 &h264_name,
210 &pjmedia_vid_codec_h264_match_sdp);
211 if (status != PJ_SUCCESS)
212 goto on_error;
213
214 /* Register codec factory to codec manager. */
215 status = pjmedia_vid_codec_mgr_register_factory(mgr,
216 &oh264_factory.base);
217 if (status != PJ_SUCCESS)
218 goto on_error;
219
220 PJ_LOG(4,(THIS_FILE, "OpenH264 codec initialized"));
221
222 /* Done. */
223 return PJ_SUCCESS;
224
225 on_error:
226 pj_pool_release(oh264_factory.pool);
227 oh264_factory.pool = NULL;
228 return status;
229 }
230
231 /*
232 * Unregister OpenH264 codecs factory from pjmedia endpoint.
233 */
234 PJ_DEF(pj_status_t) pjmedia_codec_openh264_vid_deinit(void)
235 {
236 pj_status_t status = PJ_SUCCESS;
237
238 if (oh264_factory.pool == NULL) {
239 /* Already deinitialized */
240 return PJ_SUCCESS;
241 }
242
243 /* Unregister OpenH264 codecs factory. */
244 status = pjmedia_vid_codec_mgr_unregister_factory(oh264_factory.mgr,
245 &oh264_factory.base);
246
247 /* Destroy pool. */
248 pj_pool_release(oh264_factory.pool);
249 oh264_factory.pool = NULL;
250
251 return status;
252 }
253
254 static pj_status_t oh264_test_alloc(pjmedia_vid_codec_factory *factory,
255 const pjmedia_vid_codec_info *info )
256 {
257 PJ_ASSERT_RETURN(factory == &oh264_factory.base, PJ_EINVAL);
258
259 if (info->fmt_id == PJMEDIA_FORMAT_H264 &&
260 info->pt == OH264_PT)
261 {
262 return PJ_SUCCESS;
263 }
264
265 return PJMEDIA_CODEC_EUNSUP;
266 }
267
268 static pj_status_t oh264_default_attr(pjmedia_vid_codec_factory *factory,
269 const pjmedia_vid_codec_info *info,
270 pjmedia_vid_codec_param *attr )
271 {
272 PJ_ASSERT_RETURN(factory == &oh264_factory.base, PJ_EINVAL);
273 PJ_ASSERT_RETURN(info && attr, PJ_EINVAL);
274
275 pj_bzero(attr, sizeof(pjmedia_vid_codec_param));
276
277 attr->dir = PJMEDIA_DIR_ENCODING_DECODING;
278 attr->packing = PJMEDIA_VID_PACKING_PACKETS;
279
280 /* Encoded format */
281 pjmedia_format_init_video(&attr->enc_fmt, PJMEDIA_FORMAT_H264,
282 DEFAULT_WIDTH, DEFAULT_HEIGHT,
283 DEFAULT_FPS, 1);
284
285 /* Decoded format */
286 pjmedia_format_init_video(&attr->dec_fmt, PJMEDIA_FORMAT_I420,
287 DEFAULT_WIDTH, DEFAULT_HEIGHT,
288 DEFAULT_FPS, 1);
289
290 /* Decoding fmtp */
291 attr->dec_fmtp.cnt = 2;
292 attr->dec_fmtp.param[0].name = pj_str((char*)"profile-level-id");
293 attr->dec_fmtp.param[0].val = pj_str((char*)"42e01e");
294 attr->dec_fmtp.param[1].name = pj_str((char*)" packetization-mode");
295 attr->dec_fmtp.param[1].val = pj_str((char*)"1");
296
297 /* Bitrate */
298 attr->enc_fmt.det.vid.avg_bps = DEFAULT_AVG_BITRATE;
299 attr->enc_fmt.det.vid.max_bps = DEFAULT_MAX_BITRATE;
300
301 /* Encoding MTU */
302 attr->enc_mtu = PJMEDIA_MAX_VID_PAYLOAD_SIZE;
303
304 return PJ_SUCCESS;
305 }
306
307 static pj_status_t oh264_enum_info(pjmedia_vid_codec_factory *factory,
308 unsigned *count,
309 pjmedia_vid_codec_info info[])
310 {
311 PJ_ASSERT_RETURN(info && *count > 0, PJ_EINVAL);
312 PJ_ASSERT_RETURN(factory == &oh264_factory.base, PJ_EINVAL);
313
314 *count = 1;
315 info->fmt_id = PJMEDIA_FORMAT_H264;
316 info->pt = OH264_PT;
317 info->encoding_name = pj_str((char*)"H264");
318 info->encoding_desc = pj_str((char*)"OpenH264 codec");
319 info->clock_rate = 90000;
320 info->dir = PJMEDIA_DIR_ENCODING_DECODING;
321 info->dec_fmt_id_cnt = 1;
322 info->dec_fmt_id[0] = PJMEDIA_FORMAT_I420;
323 info->packings = PJMEDIA_VID_PACKING_PACKETS |
324 PJMEDIA_VID_PACKING_WHOLE;
325 info->fps_cnt = 3;
326 info->fps[0].num = 15;
327 info->fps[0].denum = 1;
328 info->fps[1].num = 25;
329 info->fps[1].denum = 1;
330 info->fps[2].num = 30;
331 info->fps[2].denum = 1;
332
333 return PJ_SUCCESS;
334
335 }
336
337 static pj_status_t oh264_alloc_codec(pjmedia_vid_codec_factory *factory,
338 const pjmedia_vid_codec_info *info,
339 pjmedia_vid_codec **p_codec)
340 {
341 pj_pool_t *pool;
342 pjmedia_vid_codec *codec;
343 oh264_codec_data *oh264_data;
344 int rc;
345 WelsTraceCallback log_cb = &log_print;
346 int log_level = PJMEDIA_CODEC_OPENH264_LOG_LEVEL;
347
348 PJ_ASSERT_RETURN(factory == &oh264_factory.base && info && p_codec,
349 PJ_EINVAL);
350
351 *p_codec = NULL;
352
353 pool = pj_pool_create(oh264_factory.pf, "oh264%p", 512, 512, NULL);
354 if (!pool)
355 return PJ_ENOMEM;
356
357 /* codec instance */
358 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_vid_codec);
359 codec->factory = factory;
360 codec->op = &oh264_codec_op;
361
362 /* codec data */
363 oh264_data = PJ_POOL_ZALLOC_T(pool, oh264_codec_data);
364 oh264_data->pool = pool;
365 codec->codec_data = oh264_data;
366
367 /* encoder allocation */
368 rc = WelsCreateSVCEncoder(&oh264_data->enc);
369 if (rc != 0)
370 goto on_error;
371
372 oh264_data->esrc_pic = PJ_POOL_ZALLOC_T(pool, SSourcePicture);
373
374 /* decoder allocation */
375 rc = WelsCreateDecoder(&oh264_data->dec);
376 if (rc != 0)
377 goto on_error;
378
379 oh264_data->enc->SetOption(ENCODER_OPTION_TRACE_LEVEL, &log_level);
380 oh264_data->enc->SetOption(ENCODER_OPTION_TRACE_CALLBACK, &log_cb);
381 oh264_data->dec->SetOption(DECODER_OPTION_TRACE_LEVEL, &log_level);
382 oh264_data->dec->SetOption(DECODER_OPTION_TRACE_CALLBACK, &log_cb);
383
384 *p_codec = codec;
385 return PJ_SUCCESS;
386
387 on_error:
388 oh264_dealloc_codec(factory, codec);
389 return PJMEDIA_CODEC_EFAILED;
390 }
391
392 static pj_status_t oh264_dealloc_codec(pjmedia_vid_codec_factory *factory,
393 pjmedia_vid_codec *codec )
394 {
395 oh264_codec_data *oh264_data;
396
397 PJ_ASSERT_RETURN(codec, PJ_EINVAL);
398
399 PJ_UNUSED_ARG(factory);
400
401 oh264_data = (oh264_codec_data*) codec->codec_data;
402 if (oh264_data->enc) {
403 WelsDestroySVCEncoder(oh264_data->enc);
404 oh264_data->enc = NULL;
405 }
406 if (oh264_data->dec) {
407 oh264_data->dec->Uninitialize();
408 WelsDestroyDecoder(oh264_data->dec);
409 oh264_data->dec = NULL;
410 }
411 pj_pool_release(oh264_data->pool);
412 return PJ_SUCCESS;
413 }
414
415 static pj_status_t oh264_codec_init(pjmedia_vid_codec *codec,
416 pj_pool_t *pool )
417 {
418 PJ_ASSERT_RETURN(codec && pool, PJ_EINVAL);
419 PJ_UNUSED_ARG(codec);
420 PJ_UNUSED_ARG(pool);
421 return PJ_SUCCESS;
422 }
423
424 static pj_status_t oh264_codec_open(pjmedia_vid_codec *codec,
425 pjmedia_vid_codec_param *codec_param )
426 {
427 oh264_codec_data *oh264_data;
428 pjmedia_vid_codec_param *param;
429 pjmedia_h264_packetizer_cfg pktz_cfg;
430 pjmedia_vid_codec_h264_fmtp h264_fmtp;
431 SEncParamExt eprm;
432 SSpatialLayerConfig *elayer = &eprm.sSpatialLayers[0];
433 SLayerPEncCtx elayer_ctx;
434 SDecodingParam sDecParam = {0};
435 int rc;
436 pj_status_t status;
437
438 PJ_ASSERT_RETURN(codec && codec_param, PJ_EINVAL);
439
440 PJ_LOG(5,(THIS_FILE, "Opening codec.."));
441
442 oh264_data = (oh264_codec_data*) codec->codec_data;
443 oh264_data->prm = pjmedia_vid_codec_param_clone( oh264_data->pool,
444 codec_param);
445 param = oh264_data->prm;
446
447 /* Parse remote fmtp */
448 pj_bzero(&h264_fmtp, sizeof(h264_fmtp));
449 status = pjmedia_vid_codec_h264_parse_fmtp(&param->enc_fmtp, &h264_fmtp);
450 if (status != PJ_SUCCESS)
451 return status;
452
453 /* Apply SDP fmtp to format in codec param */
454 if (!param->ignore_fmtp) {
455 status = pjmedia_vid_codec_h264_apply_fmtp(param);
456 if (status != PJ_SUCCESS)
457 return status;
458 }
459
460 pj_bzero(&pktz_cfg, sizeof(pktz_cfg));
461 pktz_cfg.mtu = param->enc_mtu;
462 /* Packetization mode */
463 #if 0
464 if (h264_fmtp.packetization_mode == 0)
465 pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL;
466 else if (h264_fmtp.packetization_mode == 1)
467 pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED;
468 else
469 return PJ_ENOTSUP;
470 #else
471 if (h264_fmtp.packetization_mode!=
472 PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL &&
473 h264_fmtp.packetization_mode!=
474 PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED)
475 {
476 return PJ_ENOTSUP;
477 }
478 /* Better always send in single NAL mode for better compatibility */
479 pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL;
480 #endif
481
482 status = pjmedia_h264_packetizer_create(oh264_data->pool, &pktz_cfg,
483 &oh264_data->pktz);
484 if (status != PJ_SUCCESS)
485 return status;
486
487 oh264_data->whole = (param->packing == PJMEDIA_VID_PACKING_WHOLE);
488
489 /*
490 * Encoder
491 */
492
493 /* Init encoder parameters */
494 oh264_data->enc->GetDefaultParams (&eprm);
495 eprm.iComplexityMode = MEDIUM_COMPLEXITY;
496 eprm.sSpatialLayers[0].uiProfileIdc = PRO_BASELINE;
497 eprm.iPicWidth = param->enc_fmt.det.vid.size.w;
498 eprm.iUsageType = CAMERA_VIDEO_REAL_TIME;
499 eprm.iPicHeight = param->enc_fmt.det.vid.size.h;
500 eprm.fMaxFrameRate = (param->enc_fmt.det.vid.fps.num *
501 1.0f /
502 param->enc_fmt.det.vid.fps.denum);
503 eprm.iTemporalLayerNum = 1;
504 eprm.uiIntraPeriod = 0; /* I-Frame interval in frames */
505 eprm.eSpsPpsIdStrategy = (oh264_data->whole ? CONSTANT_ID :
506 INCREASING_ID);
507 eprm.bEnableFrameCroppingFlag = true;
508 eprm.iLoopFilterDisableIdc = 0;
509 eprm.iLoopFilterAlphaC0Offset = 0;
510 eprm.iLoopFilterBetaOffset = 0;
511 eprm.iMultipleThreadIdc = 1;
512 //eprm.bEnableRc = 1;
513 eprm.iTargetBitrate = param->enc_fmt.det.vid.avg_bps;
514 eprm.bEnableFrameSkip = 1;
515 eprm.bEnableDenoise = 0;
516 eprm.bEnableSceneChangeDetect = 1;
517 eprm.bEnableBackgroundDetection = 1;
518 eprm.bEnableAdaptiveQuant = 1;
519 eprm.bEnableLongTermReference = 0;
520 eprm.iLtrMarkPeriod = 30;
521 eprm.bPrefixNalAddingCtrl = false;
522 eprm.iSpatialLayerNum = 1;
523 if (!oh264_data->whole) {
524 eprm.uiMaxNalSize = param->enc_mtu;
525 }
526
527 pj_bzero(&elayer_ctx, sizeof (SLayerPEncCtx));
528 elayer_ctx.iDLayerQp = 24;
529 elayer_ctx.sSliceArgument.uiSliceMode = (oh264_data->whole ?
530 SM_SINGLE_SLICE :
531 SM_SIZELIMITED_SLICE);
532
533 /* uiSliceSizeConstraint = uiMaxNalSize - NAL_HEADER_ADD_0X30BYTES */
534 elayer_ctx.sSliceArgument.uiSliceSizeConstraint = param->enc_mtu - 50;
535 elayer_ctx.sSliceArgument.uiSliceNum = 1;
536 elayer_ctx.sSliceArgument.uiSliceMbNum[0] = 960;
537 elayer_ctx.sSliceArgument.uiSliceMbNum[1] = 0;
538 elayer_ctx.sSliceArgument.uiSliceMbNum[2] = 0;
539 elayer_ctx.sSliceArgument.uiSliceMbNum[3] = 0;
540 elayer_ctx.sSliceArgument.uiSliceMbNum[4] = 0;
541 elayer_ctx.sSliceArgument.uiSliceMbNum[5] = 0;
542 elayer_ctx.sSliceArgument.uiSliceMbNum[6] = 0;
543 elayer_ctx.sSliceArgument.uiSliceMbNum[7] = 0;
544
545 elayer->iVideoWidth = eprm.iPicWidth;
546 elayer->iVideoHeight = eprm.iPicHeight;
547 elayer->fFrameRate = eprm.fMaxFrameRate;
548 elayer->uiProfileIdc = eprm.sSpatialLayers[0].uiProfileIdc;
549 elayer->iSpatialBitrate = eprm.iTargetBitrate;
550 elayer->iDLayerQp = elayer_ctx.iDLayerQp;
551 elayer->sSliceArgument.uiSliceMode = elayer_ctx.sSliceArgument.uiSliceMode;
552
553 memcpy ( &elayer->sSliceArgument,
554 &elayer_ctx.sSliceArgument,
555 sizeof (SSliceArgument));
556 memcpy ( &elayer->sSliceArgument.uiSliceMbNum[0],
557 &elayer_ctx.sSliceArgument.uiSliceMbNum[0],
558 sizeof (elayer_ctx.sSliceArgument.uiSliceMbNum));
559
560 /* Init input picture */
561 oh264_data->esrc_pic->iColorFormat = videoFormatI420;
562 oh264_data->esrc_pic->uiTimeStamp = 0;
563 oh264_data->esrc_pic->iPicWidth = eprm.iPicWidth;
564 oh264_data->esrc_pic->iPicHeight = eprm.iPicHeight;
565 oh264_data->esrc_pic->iStride[0] = oh264_data->esrc_pic->iPicWidth;
566 oh264_data->esrc_pic->iStride[1] =
567 oh264_data->esrc_pic->iStride[2] =
568 oh264_data->esrc_pic->iStride[0]>>1;
569
570 oh264_data->enc_input_size = oh264_data->esrc_pic->iPicWidth *
571 oh264_data->esrc_pic->iPicHeight * 3 >> 1;
572
573 /* Initialize encoder */
574 rc = oh264_data->enc->InitializeExt (&eprm);
575 if (rc != cmResultSuccess) {
576 PJ_LOG(4,(THIS_FILE, "SVC encoder Initialize failed, rc=%d", rc));
577 return PJMEDIA_CODEC_EFAILED;
578 }
579
580 int videoFormat = videoFormatI420;
581 rc = oh264_data->enc->SetOption (ENCODER_OPTION_DATAFORMAT, &videoFormat);
582 if (rc != cmResultSuccess) {
583 PJ_LOG(4,(THIS_FILE, "SVC encoder SetOption videoFormatI420 failed, "
584 "rc=%d", rc));
585 return PJMEDIA_CODEC_EFAILED;
586 }
587
588 /*
589 * Decoder
590 */
591 sDecParam.sVideoProperty.size = sizeof (sDecParam.sVideoProperty);
592 sDecParam.uiTargetDqLayer = (pj_uint8_t) - 1;
593 sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
594 sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
595
596 /* Calculate minimum missing keyframe event interval in frames. */
597 oh264_data->missing_kf_interval =
598 (unsigned)((1.0f * param->dec_fmt.det.vid.fps.num /
599 param->dec_fmt.det.vid.fps.denum) *
600 MISSING_KEYFRAME_EV_MIN_INTERVAL/1000);
601 oh264_data->last_missing_kf_event = oh264_data->missing_kf_interval;
602
603 //TODO:
604 // Apply "sprop-parameter-sets" here
605
606 /* Initialize decoder */
607 rc = oh264_data->dec->Initialize (&sDecParam);
608 if (rc) {
609 PJ_LOG(4,(THIS_FILE, "Decoder initialization failed, rc=%d", rc));
610 return PJMEDIA_CODEC_EFAILED;
611 }
612
613 oh264_data->dec_buf_size = (MAX_RX_WIDTH * MAX_RX_HEIGHT * 3 >> 1) +
614 (MAX_RX_WIDTH);
615 oh264_data->dec_buf = (pj_uint8_t*)pj_pool_alloc(oh264_data->pool,
616 oh264_data->dec_buf_size);
617
618 /* Need to update param back after values are negotiated */
619 pj_memcpy(codec_param, param, sizeof(*codec_param));
620
621 return PJ_SUCCESS;
622 }
623
624 static pj_status_t oh264_codec_close(pjmedia_vid_codec *codec)
625 {
626 PJ_ASSERT_RETURN(codec, PJ_EINVAL);
627 PJ_UNUSED_ARG(codec);
628 return PJ_SUCCESS;
629 }
630
631 static pj_status_t oh264_codec_modify(pjmedia_vid_codec *codec,
632 const pjmedia_vid_codec_param *param)
633 {
634 struct oh264_codec_data *oh264_data;
635 int rc;
636 SBitrateInfo bitrate;
637
638 PJ_ASSERT_RETURN(codec && param, PJ_EINVAL);
639
640 oh264_data = (oh264_codec_data*) codec->codec_data;
641
642 bitrate.iLayer = SPATIAL_LAYER_ALL;
643 bitrate.iBitrate = param->enc_fmt.det.vid.avg_bps;
644 rc = oh264_data->enc->SetOption (ENCODER_OPTION_BITRATE, &bitrate);
645 if (rc != cmResultSuccess) {
646 PJ_LOG(4,(THIS_FILE, "OpenH264 encoder SetOption bitrate failed, "
647 "rc=%d", rc));
648 return PJMEDIA_CODEC_EUNSUP;
649 }
650
651 oh264_data->prm->enc_fmt.det.vid.avg_bps = param->enc_fmt.det.vid.avg_bps;
652
653 bitrate.iBitrate = param->enc_fmt.det.vid.max_bps;
654 rc = oh264_data->enc->SetOption (ENCODER_OPTION_MAX_BITRATE, &bitrate);
655 if (rc != cmResultSuccess) {
656 PJ_LOG(4,(THIS_FILE, "OpenH264 encoder SetOption max bitrate failed, "
657 "rc=%d", rc));
658 } else {
659 oh264_data->prm->enc_fmt.det.vid.max_bps =
660 param->enc_fmt.det.vid.max_bps;
661
662 PJ_LOG(4, (THIS_FILE, "OpenH264 encoder bitrate is modified to "
663 "%d avg bps and %d max bps",
664 param->enc_fmt.det.vid.avg_bps,
665 param->enc_fmt.det.vid.max_bps));
666 }
667
668 return PJ_SUCCESS;
669 }
670
671 static pj_status_t oh264_codec_get_param(pjmedia_vid_codec *codec,
672 pjmedia_vid_codec_param *param)
673 {
674 struct oh264_codec_data *oh264_data;
675
676 PJ_ASSERT_RETURN(codec && param, PJ_EINVAL);
677
678 oh264_data = (oh264_codec_data*) codec->codec_data;
679 pj_memcpy(param, oh264_data->prm, sizeof(*param));
680
681 return PJ_SUCCESS;
682 }
683
684 static pj_status_t oh264_codec_encode_begin(pjmedia_vid_codec *codec,
685 const pjmedia_vid_encode_opt *opt,
686 const pjmedia_frame *input,
687 unsigned out_size,
688 pjmedia_frame *output,
689 pj_bool_t *has_more)
690 {
691 struct oh264_codec_data *oh264_data;
692 int rc;
693
694 PJ_ASSERT_RETURN(codec && input && out_size && output && has_more,
695 PJ_EINVAL);
696
697 oh264_data = (oh264_codec_data*) codec->codec_data;
698
699 PJ_ASSERT_RETURN(input->size == oh264_data->enc_input_size,
700 PJMEDIA_CODEC_EFRMINLEN);
701
702 if (opt && opt->force_keyframe) {
703 oh264_data->enc->ForceIntraFrame(true);
704 }
705
706 oh264_data->esrc_pic->pData[0] = (pj_uint8_t*)input->buf;
707 oh264_data->esrc_pic->pData[1] = oh264_data->esrc_pic->pData[0] +
708 (oh264_data->esrc_pic->iPicWidth *
709 oh264_data->esrc_pic->iPicHeight);
710 oh264_data->esrc_pic->pData[2] = oh264_data->esrc_pic->pData[1] +
711 (oh264_data->esrc_pic->iPicWidth *
712 oh264_data->esrc_pic->iPicHeight >>2);
713
714 pj_memset (&oh264_data->bsi, 0, sizeof (SFrameBSInfo));
715 rc = oh264_data->enc->EncodeFrame( oh264_data->esrc_pic, &oh264_data->bsi);
716 if (rc != cmResultSuccess) {
717 PJ_LOG(5,(THIS_FILE, "EncodeFrame() error, ret: %d", rc));
718 return PJMEDIA_CODEC_EFAILED;
719 }
720
721 if (oh264_data->bsi.eFrameType == videoFrameTypeSkip) {
722 output->size = 0;
723 output->type = PJMEDIA_FRAME_TYPE_NONE;
724 output->timestamp = input->timestamp;
725 return PJ_SUCCESS;
726 }
727
728 oh264_data->ets = input->timestamp;
729 oh264_data->ilayer = 0;
730 oh264_data->enc_frame_size = oh264_data->enc_processed = 0;
731
732 if (oh264_data->whole) {
733 SLayerBSInfo* pLayerBsInfo;
734 pj_uint8_t *payload;
735 unsigned i, payload_size = 0;
736
737 *has_more = PJ_FALSE;
738
739 /* Find which layer with biggest payload */
740 oh264_data->ilayer = 0;
741 payload_size = oh264_data->bsi.sLayerInfo[0].pNalLengthInByte[0];
742 for (i=0; i < (unsigned)oh264_data->bsi.iLayerNum; ++i) {
743 unsigned j;
744 pLayerBsInfo = &oh264_data->bsi.sLayerInfo[i];
745 for (j=0; j < (unsigned)pLayerBsInfo->iNalCount; ++j) {
746 if (pLayerBsInfo->pNalLengthInByte[j] > (int)payload_size) {
747 payload_size = pLayerBsInfo->pNalLengthInByte[j];
748 oh264_data->ilayer = i;
749 }
750 }
751 }
752
753 pLayerBsInfo = &oh264_data->bsi.sLayerInfo[oh264_data->ilayer];
754 if (pLayerBsInfo == NULL) {
755 output->size = 0;
756 output->type = PJMEDIA_FRAME_TYPE_NONE;
757 return PJ_SUCCESS;
758 }
759
760 payload = pLayerBsInfo->pBsBuf;
761 payload_size = 0;
762 for (int inal = pLayerBsInfo->iNalCount - 1; inal >= 0; --inal) {
763 payload_size += pLayerBsInfo->pNalLengthInByte[inal];
764 }
765
766 if (payload_size > out_size)
767 return PJMEDIA_CODEC_EFRMTOOSHORT;
768
769 output->type = PJMEDIA_FRAME_TYPE_VIDEO;
770 output->size = payload_size;
771 output->timestamp = input->timestamp;
772 pj_memcpy(output->buf, payload, payload_size);
773
774 return PJ_SUCCESS;
775 }
776
777 return oh264_codec_encode_more(codec, out_size, output, has_more);
778 }
779
780
781 static pj_status_t oh264_codec_encode_more(pjmedia_vid_codec *codec,
782 unsigned out_size,
783 pjmedia_frame *output,
784 pj_bool_t *has_more)
785 {
786 struct oh264_codec_data *oh264_data;
787 const pj_uint8_t *payload;
788 pj_size_t payload_len;
789 pj_status_t status;
790
791 PJ_ASSERT_RETURN(codec && out_size && output && has_more,
792 PJ_EINVAL);
793
794 oh264_data = (oh264_codec_data*) codec->codec_data;
795
796 if (oh264_data->enc_processed < oh264_data->enc_frame_size) {
797 /* We have outstanding frame in packetizer */
798 status = pjmedia_h264_packetize(oh264_data->pktz,
799 oh264_data->enc_frame_whole,
800 oh264_data->enc_frame_size,
801 &oh264_data->enc_processed,
802 &payload, &payload_len);
803 if (status != PJ_SUCCESS) {
804 /* Reset */
805 oh264_data->enc_frame_size = oh264_data->enc_processed = 0;
806 *has_more = (oh264_data->enc_processed <
807 oh264_data->enc_frame_size) ||
808 (oh264_data->ilayer < oh264_data->bsi.iLayerNum);
809
810 PJ_PERROR(3,(THIS_FILE, status, "pjmedia_h264_packetize() error"));
811 return status;
812 }
813
814 PJ_ASSERT_RETURN(payload_len <= out_size, PJMEDIA_CODEC_EFRMTOOSHORT);
815
816 output->type = PJMEDIA_FRAME_TYPE_VIDEO;
817 pj_memcpy(output->buf, payload, payload_len);
818 output->size = payload_len;
819
820 if (oh264_data->bsi.eFrameType == videoFrameTypeIDR) {
821 output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME;
822 }
823
824 *has_more = (oh264_data->enc_processed < oh264_data->enc_frame_size) ||
825 (oh264_data->ilayer < oh264_data->bsi.iLayerNum);
826 return PJ_SUCCESS;
827 }
828
829 if (oh264_data->ilayer >= oh264_data->bsi.iLayerNum) {
830 /* No more unretrieved frame */
831 goto no_frame;
832 }
833
834 SLayerBSInfo* pLayerBsInfo;
835 pLayerBsInfo = &oh264_data->bsi.sLayerInfo[oh264_data->ilayer++];
836 if (pLayerBsInfo == NULL) {
837 goto no_frame;
838 }
839
840 oh264_data->enc_frame_size = 0;
841 for (int inal = pLayerBsInfo->iNalCount - 1; inal >= 0; --inal) {
842 oh264_data->enc_frame_size += pLayerBsInfo->pNalLengthInByte[inal];
843 }
844
845 oh264_data->enc_frame_whole = pLayerBsInfo->pBsBuf;
846 oh264_data->enc_processed = 0;
847
848
849 status = pjmedia_h264_packetize(oh264_data->pktz,
850 oh264_data->enc_frame_whole,
851 oh264_data->enc_frame_size,
852 &oh264_data->enc_processed,
853 &payload, &payload_len);
854 if (status != PJ_SUCCESS) {
855 /* Reset */
856 oh264_data->enc_frame_size = oh264_data->enc_processed = 0;
857 *has_more = (oh264_data->ilayer < oh264_data->bsi.iLayerNum);
858
859 PJ_PERROR(3,(THIS_FILE, status, "pjmedia_h264_packetize() error [2]"));
860 return status;
861 }
862
863 PJ_ASSERT_RETURN(payload_len <= out_size, PJMEDIA_CODEC_EFRMTOOSHORT);
864
865 output->type = PJMEDIA_FRAME_TYPE_VIDEO;
866 pj_memcpy(output->buf, payload, payload_len);
867 output->size = payload_len;
868
869 if (oh264_data->bsi.eFrameType == videoFrameTypeIDR) {
870 output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME;
871 }
872
873 *has_more = (oh264_data->enc_processed < oh264_data->enc_frame_size) ||
874 (oh264_data->ilayer < oh264_data->bsi.iLayerNum);
875
876 return PJ_SUCCESS;
877
878 no_frame:
879 *has_more = PJ_FALSE;
880 output->size = 0;
881 output->type = PJMEDIA_FRAME_TYPE_NONE;
882 return PJ_SUCCESS;
883 }
884
885 static int write_yuv(pj_uint8_t *buf,
886 unsigned dst_len,
887 unsigned char* pData[3],
888 int iStride[2],
889 int iWidth,
890 int iHeight)
891 {
892 unsigned req_size;
893 pj_uint8_t *dst = buf;
894 pj_uint8_t *max = dst + dst_len;
895 int i;
896 unsigned char* pPtr = NULL;
897
898 req_size = (iWidth * iHeight) + (iWidth / 2 * iHeight / 2) +
899 (iWidth / 2 * iHeight / 2);
900 if (dst_len < req_size)
901 return -1;
902
903 pPtr = pData[0];
904 for (i = 0; i < iHeight && (dst + iWidth < max); i++) {
905 pj_memcpy(dst, pPtr, iWidth);
906 pPtr += iStride[0];
907 dst += iWidth;
908 }
909
910 if (i < iHeight)
911 return -1;
912
913 iHeight = iHeight / 2;
914 iWidth = iWidth / 2;
915 pPtr = pData[1];
916 for (i = 0; i < iHeight && (dst + iWidth <= max); i++) {
917 pj_memcpy(dst, pPtr, iWidth);
918 pPtr += iStride[1];
919 dst += iWidth;
920 }
921
922 if (i < iHeight)
923 return -1;
924
925 pPtr = pData[2];
926 for (i = 0; i < iHeight && (dst + iWidth <= max); i++) {
927 pj_memcpy(dst, pPtr, iWidth);
928 pPtr += iStride[1];
929 dst += iWidth;
930 }
931
932 if (i < iHeight)
933 return -1;
934
935 return (int)(dst - buf);
936 }
937
938 static pj_status_t oh264_got_decoded_frame(pjmedia_vid_codec *codec,
939 struct oh264_codec_data *oh264_data,
940 unsigned char *pData[3],
941 SBufferInfo *sDstBufInfo,
942 pj_timestamp *timestamp,
943 unsigned out_size,
944 pjmedia_frame *output)
945 {
946 pj_uint8_t* pDst[3] = {NULL};
947
948 pDst[0] = (pj_uint8_t*)pData[0];
949 pDst[1] = (pj_uint8_t*)pData[1];
950 pDst[2] = (pj_uint8_t*)pData[2];
951
952 /* Do not reset size as it may already contain frame
953 output->size = 0;
954 */
955
956 if (!pDst[0] || !pDst[1] || !pDst[2]) {
957 return PJ_SUCCESS;
958 }
959
960 int iStride[2];
961 int iWidth = sDstBufInfo->UsrData.sSystemBuffer.iWidth;
962 int iHeight = sDstBufInfo->UsrData.sSystemBuffer.iHeight;
963
964 iStride[0] = sDstBufInfo->UsrData.sSystemBuffer.iStride[0];
965 iStride[1] = sDstBufInfo->UsrData.sSystemBuffer.iStride[1];
966
967 int len = write_yuv((pj_uint8_t *)output->buf, out_size,
968 pDst, iStride, iWidth, iHeight);
969 if (len > 0) {
970 output->timestamp = *timestamp;
971 output->size = len;
972 output->type = PJMEDIA_FRAME_TYPE_VIDEO;
973 } else {
974 /* buffer is damaged, reset size */
975 output->size = 0;
976 return PJMEDIA_CODEC_EFRMTOOSHORT;
977 }
978
979 /* Detect format change */
980 if (iWidth != (int)oh264_data->prm->dec_fmt.det.vid.size.w ||
981 iHeight != (int)oh264_data->prm->dec_fmt.det.vid.size.h)
982 {
983 pjmedia_event event;
984
985 PJ_LOG(4,(THIS_FILE, "Frame size changed: %dx%d --> %dx%d",
986 oh264_data->prm->dec_fmt.det.vid.size.w,
987 oh264_data->prm->dec_fmt.det.vid.size.h,
988 iWidth, iHeight));
989
990 oh264_data->prm->dec_fmt.det.vid.size.w = iWidth;
991 oh264_data->prm->dec_fmt.det.vid.size.h = iHeight;
992
993 /* Broadcast format changed event */
994 pjmedia_event_init(&event, PJMEDIA_EVENT_FMT_CHANGED,
995 timestamp, codec);
996 event.data.fmt_changed.dir = PJMEDIA_DIR_DECODING;
997 pjmedia_format_copy(&event.data.fmt_changed.new_fmt,
998 &oh264_data->prm->dec_fmt);
999 pjmedia_event_publish(NULL, codec, &event,
1000 PJMEDIA_EVENT_PUBLISH_DEFAULT);
1001 }
1002
1003 return PJ_SUCCESS;
1004 }
1005
1006 static pj_status_t oh264_codec_decode(pjmedia_vid_codec *codec,
1007 pj_size_t count,
1008 pjmedia_frame packets[],
1009 unsigned out_size,
1010 pjmedia_frame *output)
1011 {
1012 struct oh264_codec_data *oh264_data;
1013 unsigned char* pData[3] = {NULL};
1014 const pj_uint8_t nal_start[] = { 0, 0, 1 };
1015 SBufferInfo sDstBufInfo;
1016 pj_bool_t has_frame = PJ_FALSE;
1017 pj_bool_t kf_requested = PJ_FALSE;
1018 unsigned buf_pos, whole_len = 0;
1019 unsigned i;
1020 pj_status_t status = PJ_SUCCESS;
1021 DECODING_STATE ret;
1022
1023 PJ_ASSERT_RETURN(codec && count && packets && out_size && output,
1024 PJ_EINVAL);
1025 PJ_ASSERT_RETURN(output->buf, PJ_EINVAL);
1026
1027 oh264_data = (oh264_codec_data*) codec->codec_data;
1028 oh264_data->last_missing_kf_event++;
1029 /* Check if we have recently generated missing keyframe event. */
1030 if (oh264_data->last_missing_kf_event < oh264_data->missing_kf_interval)
1031 kf_requested = PJ_TRUE;
1032
1033 /*
1034 * Step 1: unpacketize the packets/frames
1035 */
1036 whole_len = 0;
1037 if (oh264_data->whole) {
1038 for (i=0; i<count; ++i) {
1039 if (whole_len + packets[i].size > oh264_data->dec_buf_size) {
1040 PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [1]"));
1041 return PJMEDIA_CODEC_EFRMTOOSHORT;
1042 }
1043
1044 pj_memcpy( oh264_data->dec_buf + whole_len,
1045 (pj_uint8_t*)packets[i].buf,
1046 packets[i].size);
1047 whole_len += (unsigned)packets[i].size;
1048 }
1049
1050 } else {
1051 for (i=0; i<count; ++i) {
1052
1053 if (whole_len + packets[i].size + sizeof(nal_start) >
1054 oh264_data->dec_buf_size)
1055 {
1056 PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [1]"));
1057 return PJMEDIA_CODEC_EFRMTOOSHORT;
1058 }
1059
1060 status = pjmedia_h264_unpacketize( oh264_data->pktz,
1061 (pj_uint8_t*)packets[i].buf,
1062 packets[i].size,
1063 oh264_data->dec_buf,
1064 oh264_data->dec_buf_size,
1065 &whole_len);
1066 if (status != PJ_SUCCESS) {
1067 PJ_PERROR(4,(THIS_FILE, status, "Unpacketize error"));
1068 continue;
1069 }
1070 }
1071 }
1072
1073 if (whole_len + sizeof(nal_start) > oh264_data->dec_buf_size) {
1074 PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [2]"));
1075 return PJMEDIA_CODEC_EFRMTOOSHORT;
1076 }
1077
1078 /* Dummy NAL sentinel */
1079 pj_memcpy( oh264_data->dec_buf + whole_len, nal_start, sizeof(nal_start));
1080
1081 /*
1082 * Step 2: parse the individual NAL and give to decoder
1083 */
1084 buf_pos = 0;
1085 while (1) {
1086 unsigned frm_size;
1087 unsigned char *start;
1088
1089 for (i = 0; buf_pos + i < whole_len; i++) {
1090 if (oh264_data->dec_buf[buf_pos + i] == 0 &&
1091 oh264_data->dec_buf[buf_pos + i + 1] == 0 &&
1092 oh264_data->dec_buf[buf_pos + i + 2] == 1 &&
1093 i > 1)
1094 {
1095 break;
1096 }
1097 }
1098 frm_size = i;
1099
1100 pj_bzero( pData, sizeof(pData));
1101 pj_bzero( &sDstBufInfo, sizeof (SBufferInfo));
1102
1103 start = oh264_data->dec_buf + buf_pos;
1104
1105 /* Decode */
1106 ret = oh264_data->dec->DecodeFrame2( start, frm_size, pData,
1107 &sDstBufInfo);
1108
1109 if (ret != dsErrorFree && !kf_requested) {
1110 /* Broadcast missing keyframe event */
1111 pjmedia_event event;
1112 pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING,
1113 &packets[0].timestamp, codec);
1114 pjmedia_event_publish(NULL, codec, &event,
1115 PJMEDIA_EVENT_PUBLISH_DEFAULT);
1116 kf_requested = PJ_TRUE;
1117 oh264_data->last_missing_kf_event = 0;
1118 }
1119
1120 if (0 && sDstBufInfo.iBufferStatus == 1) {
1121 // Better to just get the frame later after all NALs are consumed
1122 // by the decoder, it should have the best quality and save some
1123 // CPU load.
1124 /* May overwrite existing frame but that's ok. */
1125 status = oh264_got_decoded_frame(codec, oh264_data, pData,
1126 &sDstBufInfo,
1127 &packets[0].timestamp, out_size,
1128 output);
1129 has_frame = (status==PJ_SUCCESS && output->size != 0);
1130 }
1131
1132 if (buf_pos + frm_size >= whole_len)
1133 break;
1134
1135 buf_pos += frm_size;
1136 }
1137
1138 /* Signal that we have no more frames */
1139 pj_int32_t iEndOfStreamFlag = true;
1140 oh264_data->dec->SetOption( DECODER_OPTION_END_OF_STREAM,
1141 (void*)&iEndOfStreamFlag);
1142
1143 /* Retrieve the decoded frame */
1144 pj_bzero(pData, sizeof(pData));
1145 pj_bzero(&sDstBufInfo, sizeof (SBufferInfo));
1146 ret = oh264_data->dec->DecodeFrame2 (NULL, 0, pData, &sDstBufInfo);
1147
1148 if (sDstBufInfo.iBufferStatus == 1 &&
1149 !(ret & dsRefLost) && !(ret & dsNoParamSets) &&
1150 !(ret & dsDepLayerLost))
1151 {
1152 /* Overwrite existing output frame and that's ok, because we assume
1153 * newer frame have better quality because it has more NALs
1154 */
1155 status = oh264_got_decoded_frame(codec, oh264_data, pData,
1156 &sDstBufInfo, &packets[0].timestamp,
1157 out_size, output);
1158 has_frame = (status==PJ_SUCCESS && output->size != 0);
1159 }
1160
1161 if (ret != dsErrorFree) {
1162 if (!kf_requested) {
1163 /* Broadcast missing keyframe event */
1164 pjmedia_event event;
1165 pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING,
1166 &packets[0].timestamp, codec);
1167 pjmedia_event_publish(NULL, codec, &event,
1168 PJMEDIA_EVENT_PUBLISH_DEFAULT);
1169 oh264_data->last_missing_kf_event = 0;
1170 }
1171
1172 if (has_frame) {
1173 PJ_LOG(5,(oh264_data->pool->obj_name,
1174 "Decoder returned non error free frame, ret=%d", ret));
1175 }
1176 }
1177
1178 if (!has_frame) {
1179 output->type = PJMEDIA_FRAME_TYPE_NONE;
1180 output->size = 0;
1181 output->timestamp = packets[0].timestamp;
1182
1183 PJ_LOG(5,(THIS_FILE, "Decode couldn't produce picture, "
1184 "input nframes=%lu, concatenated size=%d bytes, ret=%d",
1185 (unsigned long)count, whole_len, ret));
1186 }
1187
1188 return status;
1189 }
1190
1191 #endif /* PJMEDIA_HAS_OPENH264_CODEC */