| 1 |
/* |
| 2 |
* Copyright (C)2020 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 |
|
| 19 |
#include <pjmedia-codec/and_vid_mediacodec.h> |
| 20 |
#include <pjmedia-codec/h264_packetizer.h> |
| 21 |
#include <pjmedia-codec/vpx_packetizer.h> |
| 22 |
#include <pjmedia/vid_codec_util.h> |
| 23 |
#include <pjmedia/errno.h> |
| 24 |
#include <pj/atomic_queue.h> |
| 25 |
#include <pj/log.h> |
| 26 |
|
| 27 |
#if defined(PJMEDIA_HAS_ANDROID_MEDIACODEC) && \ |
| 28 |
PJMEDIA_HAS_ANDROID_MEDIACODEC != 0 && \ |
| 29 |
defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 30 |
|
| 31 |
#include <android/log.h> |
| 32 |
|
| 33 |
/* Android AMediaCodec: */ |
| 34 |
#include "media/NdkMediaCodec.h" |
| 35 |
|
| 36 |
/* |
| 37 |
* Constants |
| 38 |
*/ |
| 39 |
#define THIS_FILE "and_vid_mediacodec.cpp" |
| 40 |
#define AND_MEDIA_KEY_COLOR_FMT "color-format" |
| 41 |
#define AND_MEDIA_KEY_WIDTH "width" |
| 42 |
#define AND_MEDIA_KEY_HEIGHT "height" |
| 43 |
#define AND_MEDIA_KEY_BIT_RATE "bitrate" |
| 44 |
#define AND_MEDIA_KEY_PROFILE "profile" |
| 45 |
#define AND_MEDIA_KEY_FRAME_RATE "frame-rate" |
| 46 |
#define AND_MEDIA_KEY_IFR_INTERVAL "i-frame-interval" |
| 47 |
#define AND_MEDIA_KEY_MIME "mime" |
| 48 |
#define AND_MEDIA_KEY_REQUEST_SYNCF "request-sync" |
| 49 |
#define AND_MEDIA_KEY_CSD0 "csd-0" |
| 50 |
#define AND_MEDIA_KEY_CSD1 "csd-1" |
| 51 |
#define AND_MEDIA_KEY_MAX_INPUT_SZ "max-input-size" |
| 52 |
#define AND_MEDIA_KEY_ENCODER "encoder" |
| 53 |
#define AND_MEDIA_KEY_PRIORITY "priority" |
| 54 |
#define AND_MEDIA_KEY_STRIDE "stride" |
| 55 |
#define AND_MEDIA_KEY_LATENCY "latency" |
| 56 |
#define AND_MEDIA_KEY_LOW_LATENCY "low-latency" |
| 57 |
#define AND_MEDIA_I420_PLANAR_FMT 0x13 |
| 58 |
#define AND_MEDIA_QUEUE_TIMEOUT 2000*100 |
| 59 |
|
| 60 |
#define DEFAULT_WIDTH 352 |
| 61 |
#define DEFAULT_HEIGHT 288 |
| 62 |
|
| 63 |
#define DEFAULT_FPS 15 |
| 64 |
#define DEFAULT_AVG_BITRATE 256000 |
| 65 |
#define DEFAULT_MAX_BITRATE 256000 |
| 66 |
|
| 67 |
#define SPS_PPS_BUF_SIZE 64 |
| 68 |
|
| 69 |
#define MAX_RX_WIDTH 1280 |
| 70 |
#define MAX_RX_HEIGHT 800 |
| 71 |
|
| 72 |
/* Maximum duration from one key frame to the next (in seconds). */ |
| 73 |
#define KEYFRAME_INTERVAL 1 |
| 74 |
|
| 75 |
#define AND_MED_H264_PT PJMEDIA_RTP_PT_H264_RSV2 |
| 76 |
#define AND_MED_VP8_PT PJMEDIA_RTP_PT_VP8_RSV1 |
| 77 |
#define AND_MED_VP9_PT PJMEDIA_RTP_PT_VP9_RSV1 |
| 78 |
|
| 79 |
#define BUFFER_MAX_ITEM 16 |
| 80 |
#define API_AT_LEAST(x) __builtin_available(android x, *) |
| 81 |
|
| 82 |
typedef struct and_med_buf_info { |
| 83 |
pj_int32_t index; |
| 84 |
pj_int32_t size; |
| 85 |
pj_uint32_t flags; |
| 86 |
} and_med_buf_info; |
| 87 |
|
| 88 |
/* |
| 89 |
* Factory operations. |
| 90 |
*/ |
| 91 |
static pj_status_t and_media_test_alloc(pjmedia_vid_codec_factory *factory, |
| 92 |
const pjmedia_vid_codec_info *info ); |
| 93 |
static pj_status_t and_media_default_attr(pjmedia_vid_codec_factory *factory, |
| 94 |
const pjmedia_vid_codec_info *info, |
| 95 |
pjmedia_vid_codec_param *attr ); |
| 96 |
static pj_status_t and_media_enum_info(pjmedia_vid_codec_factory *factory, |
| 97 |
unsigned *count, |
| 98 |
pjmedia_vid_codec_info codecs[]); |
| 99 |
static pj_status_t and_media_alloc_codec(pjmedia_vid_codec_factory *factory, |
| 100 |
const pjmedia_vid_codec_info *info, |
| 101 |
pjmedia_vid_codec **p_codec); |
| 102 |
static pj_status_t and_media_dealloc_codec(pjmedia_vid_codec_factory *factory, |
| 103 |
pjmedia_vid_codec *codec ); |
| 104 |
|
| 105 |
|
| 106 |
/* |
| 107 |
* Codec operations |
| 108 |
*/ |
| 109 |
static pj_status_t and_media_codec_init(pjmedia_vid_codec *codec, |
| 110 |
pj_pool_t *pool ); |
| 111 |
static pj_status_t and_media_codec_open(pjmedia_vid_codec *codec, |
| 112 |
pjmedia_vid_codec_param *param ); |
| 113 |
static pj_status_t and_media_codec_close(pjmedia_vid_codec *codec); |
| 114 |
static pj_status_t and_media_codec_modify(pjmedia_vid_codec *codec, |
| 115 |
const pjmedia_vid_codec_param *param); |
| 116 |
static pj_status_t and_media_codec_get_param(pjmedia_vid_codec *codec, |
| 117 |
pjmedia_vid_codec_param *param); |
| 118 |
static pj_status_t and_media_codec_encode_begin(pjmedia_vid_codec *codec, |
| 119 |
const pjmedia_vid_encode_opt *opt, |
| 120 |
const pjmedia_frame *input, |
| 121 |
unsigned out_size, |
| 122 |
pjmedia_frame *output, |
| 123 |
pj_bool_t *has_more); |
| 124 |
static pj_status_t and_media_codec_encode_more(pjmedia_vid_codec *codec, |
| 125 |
unsigned out_size, |
| 126 |
pjmedia_frame *output, |
| 127 |
pj_bool_t *has_more); |
| 128 |
static pj_status_t and_media_codec_decode(pjmedia_vid_codec *codec, |
| 129 |
pj_size_t count, |
| 130 |
pjmedia_frame packets[], |
| 131 |
unsigned out_size, |
| 132 |
pjmedia_frame *output); |
| 133 |
|
| 134 |
/* Definition for Android AMediaCodec operations. */ |
| 135 |
static pjmedia_vid_codec_op and_media_codec_op = |
| 136 |
{ |
| 137 |
&and_media_codec_init, |
| 138 |
&and_media_codec_open, |
| 139 |
&and_media_codec_close, |
| 140 |
&and_media_codec_modify, |
| 141 |
&and_media_codec_get_param, |
| 142 |
&and_media_codec_encode_begin, |
| 143 |
&and_media_codec_encode_more, |
| 144 |
&and_media_codec_decode, |
| 145 |
NULL |
| 146 |
}; |
| 147 |
|
| 148 |
/* Definition for Android AMediaCodec factory operations. */ |
| 149 |
static pjmedia_vid_codec_factory_op and_media_factory_op = |
| 150 |
{ |
| 151 |
&and_media_test_alloc, |
| 152 |
&and_media_default_attr, |
| 153 |
&and_media_enum_info, |
| 154 |
&and_media_alloc_codec, |
| 155 |
&and_media_dealloc_codec |
| 156 |
}; |
| 157 |
|
| 158 |
static struct and_media_factory |
| 159 |
{ |
| 160 |
pjmedia_vid_codec_factory base; |
| 161 |
pjmedia_vid_codec_mgr *mgr; |
| 162 |
pj_pool_factory *pf; |
| 163 |
pj_pool_t *pool; |
| 164 |
} and_media_factory; |
| 165 |
|
| 166 |
enum and_media_frm_type { |
| 167 |
AND_MEDIA_FRM_TYPE_DEFAULT = 0, |
| 168 |
AND_MEDIA_FRM_TYPE_KEYFRAME = 1, |
| 169 |
AND_MEDIA_FRM_TYPE_CONFIG = 2 |
| 170 |
}; |
| 171 |
|
| 172 |
typedef struct h264_codec_data { |
| 173 |
pjmedia_h264_packetizer *pktz; |
| 174 |
|
| 175 |
pj_uint8_t enc_sps_pps_buf[SPS_PPS_BUF_SIZE]; |
| 176 |
unsigned enc_sps_pps_len; |
| 177 |
pj_bool_t enc_sps_pps_ex; |
| 178 |
|
| 179 |
pj_uint8_t *dec_sps_buf; |
| 180 |
unsigned dec_sps_len; |
| 181 |
pj_uint8_t *dec_pps_buf; |
| 182 |
unsigned dec_pps_len; |
| 183 |
} h264_codec_data; |
| 184 |
|
| 185 |
typedef struct vpx_codec_data { |
| 186 |
pjmedia_vpx_packetizer *pktz; |
| 187 |
} vpx_codec_data; |
| 188 |
|
| 189 |
typedef struct and_media_codec_data |
| 190 |
{ |
| 191 |
pj_pool_t *pool; |
| 192 |
pj_uint8_t codec_idx; |
| 193 |
pjmedia_vid_codec_param *prm; |
| 194 |
pj_bool_t whole; |
| 195 |
void *ex_data; |
| 196 |
|
| 197 |
/* Encoder state */ |
| 198 |
AMediaCodec *enc; |
| 199 |
unsigned enc_input_size; |
| 200 |
pj_uint8_t *enc_frame_whole; |
| 201 |
unsigned enc_frame_size; |
| 202 |
unsigned enc_processed; |
| 203 |
AMediaCodecBufferInfo enc_buf_info; |
| 204 |
int enc_output_buf_idx; |
| 205 |
|
| 206 |
/* Decoder state */ |
| 207 |
AMediaCodec *dec; |
| 208 |
pj_uint8_t *dec_buf; |
| 209 |
pj_uint8_t *dec_input_buf; |
| 210 |
unsigned dec_input_buf_len; |
| 211 |
pj_size_t dec_input_buf_max_size; |
| 212 |
pj_ssize_t dec_input_buf_idx; |
| 213 |
unsigned dec_has_output_frame; |
| 214 |
unsigned dec_stride_len; |
| 215 |
unsigned dec_buf_size; |
| 216 |
AMediaCodecBufferInfo dec_buf_info; |
| 217 |
|
| 218 |
pj_atomic_queue_t *enc_avail_input_buf; |
| 219 |
pj_atomic_queue_t *enc_avail_output_buf; |
| 220 |
pj_atomic_queue_t *dec_avail_input_buf; |
| 221 |
pj_atomic_queue_t *dec_avail_output_buf; |
| 222 |
|
| 223 |
pj_bool_t format_changed; |
| 224 |
pjmedia_rect_size new_size; |
| 225 |
int new_stride; |
| 226 |
} and_media_codec_data; |
| 227 |
|
| 228 |
/* Custom callbacks. */ |
| 229 |
|
| 230 |
/* This callback is useful when specific method is needed when opening |
| 231 |
* the codec (e.g: applying fmtp or setting up the packetizer) |
| 232 |
*/ |
| 233 |
typedef pj_status_t (*open_cb)(and_media_codec_data *and_media_data); |
| 234 |
|
| 235 |
/* This callback is useful for handling configure frame produced by encoder. |
| 236 |
* Output frame might want to be stored the configuration frame and append it |
| 237 |
* to a keyframe for sending later (e.g: on H264 codec). The default behavior |
| 238 |
* is to send the configuration frame regardless. |
| 239 |
*/ |
| 240 |
typedef pj_status_t (*process_encode_cb)(and_media_codec_data *and_media_data); |
| 241 |
|
| 242 |
/* This callback is to process more encoded packets/payloads from the codec.*/ |
| 243 |
typedef pj_status_t(*encode_more_cb)(and_media_codec_data *and_media_data, |
| 244 |
unsigned out_size, |
| 245 |
pjmedia_frame *output, |
| 246 |
pj_bool_t *has_more); |
| 247 |
|
| 248 |
/* This callback is to decode packets. */ |
| 249 |
typedef pj_status_t(*decode_cb)(pjmedia_vid_codec *codec, |
| 250 |
pj_size_t count, |
| 251 |
pjmedia_frame packets[], |
| 252 |
unsigned out_size, |
| 253 |
pjmedia_frame *output); |
| 254 |
|
| 255 |
/** |
| 256 |
* Called when an input buffer becomes available. |
| 257 |
* The specified index is the index of the available input buffer. |
| 258 |
*/ |
| 259 |
static void and_med_on_input_avail(AMediaCodec *codec, |
| 260 |
void *userdata, |
| 261 |
int32_t index) |
| 262 |
{ |
| 263 |
and_media_codec_data *and_media_data = (and_media_codec_data *) userdata; |
| 264 |
and_med_buf_info buf_info; |
| 265 |
pj_atomic_queue_t *buf_queue; |
| 266 |
|
| 267 |
pj_bzero(&buf_info, sizeof(buf_info)); |
| 268 |
if (codec == and_media_data->enc) { |
| 269 |
buf_queue = and_media_data->enc_avail_input_buf; |
| 270 |
} else { |
| 271 |
buf_queue = and_media_data->dec_avail_input_buf; |
| 272 |
} |
| 273 |
buf_info.index = index; |
| 274 |
pj_atomic_queue_put(buf_queue, &buf_info); |
| 275 |
} |
| 276 |
/** |
| 277 |
* Called when an output buffer becomes available. |
| 278 |
* The specified index is the index of the available output buffer. |
| 279 |
*/ |
| 280 |
static void and_med_on_output_avail(AMediaCodec *codec, |
| 281 |
void *userdata, |
| 282 |
int32_t index, |
| 283 |
AMediaCodecBufferInfo *bufferInfo) |
| 284 |
{ |
| 285 |
and_media_codec_data *and_media_data = (and_media_codec_data *) userdata; |
| 286 |
and_med_buf_info buf_info; |
| 287 |
pj_atomic_queue_t *buf_queue; |
| 288 |
|
| 289 |
pj_bzero(&buf_info, sizeof(buf_info)); |
| 290 |
if (codec == and_media_data->enc) { |
| 291 |
buf_queue = and_media_data->enc_avail_output_buf; |
| 292 |
} else { |
| 293 |
buf_queue = and_media_data->dec_avail_output_buf; |
| 294 |
} |
| 295 |
buf_info.index = index; |
| 296 |
buf_info.size = bufferInfo->size; |
| 297 |
buf_info.flags = bufferInfo->flags; |
| 298 |
pj_atomic_queue_put(buf_queue, &buf_info); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Called when the output format has changed. |
| 303 |
* The specified format contains the new output format. |
| 304 |
*/ |
| 305 |
static void and_med_on_format_changed(AMediaCodec *codec, |
| 306 |
void *userdata, |
| 307 |
AMediaFormat *format) |
| 308 |
{ |
| 309 |
int width, height, stride; |
| 310 |
and_media_codec_data *and_media_data = (and_media_codec_data *) userdata; |
| 311 |
|
| 312 |
AMediaFormat_getInt32(format, AND_MEDIA_KEY_WIDTH, &width); |
| 313 |
AMediaFormat_getInt32(format, AND_MEDIA_KEY_HEIGHT, &height); |
| 314 |
AMediaFormat_getInt32(format, AND_MEDIA_KEY_STRIDE, &stride); |
| 315 |
if(codec==and_media_data->dec){ |
| 316 |
and_media_data->format_changed = PJ_TRUE; |
| 317 |
and_media_data->new_size.w = width; |
| 318 |
and_media_data->new_size.h = height; |
| 319 |
and_media_data->new_stride = stride; |
| 320 |
} |
| 321 |
__android_log_print(ANDROID_LOG_INFO, THIS_FILE, |
| 322 |
"[%s] On format changed w:%d h:%d stride:%d\r\n", |
| 323 |
(codec==and_media_data->enc)?"encoder":"decoder", |
| 324 |
width, height, stride); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Called when the MediaCodec encountered an error. |
| 329 |
*/ |
| 330 |
static void and_med_on_error(AMediaCodec *codec, |
| 331 |
void *userdata, |
| 332 |
media_status_t error, |
| 333 |
int32_t actionCode, |
| 334 |
const char *detail) |
| 335 |
{ |
| 336 |
and_media_codec_data *and_media_data = (and_media_codec_data *) userdata; |
| 337 |
__android_log_print(ANDROID_LOG_INFO, THIS_FILE, |
| 338 |
"[%s] On Media error : err[%d] code[%d] msg[%s]\r\n", |
| 339 |
(codec==and_media_data->enc)?"encoder":"decoder", error, |
| 340 |
actionCode, detail); |
| 341 |
} |
| 342 |
|
| 343 |
/* Custom callback implementation. */ |
| 344 |
#if PJMEDIA_HAS_AND_MEDIA_H264 |
| 345 |
static pj_status_t open_h264(and_media_codec_data *and_media_data); |
| 346 |
static pj_status_t process_encode_h264(and_media_codec_data *and_media_data); |
| 347 |
static pj_status_t encode_more_h264(and_media_codec_data *and_media_data, |
| 348 |
unsigned out_size, |
| 349 |
pjmedia_frame *output, |
| 350 |
pj_bool_t *has_more); |
| 351 |
static pj_status_t decode_h264(pjmedia_vid_codec *codec, |
| 352 |
pj_size_t count, |
| 353 |
pjmedia_frame packets[], |
| 354 |
unsigned out_size, |
| 355 |
pjmedia_frame *output); |
| 356 |
#endif |
| 357 |
|
| 358 |
#if PJMEDIA_HAS_AND_MEDIA_VP8 || PJMEDIA_HAS_AND_MEDIA_VP9 |
| 359 |
static pj_status_t open_vpx(and_media_codec_data *and_media_data); |
| 360 |
static pj_status_t encode_more_vpx(and_media_codec_data *and_media_data, |
| 361 |
unsigned out_size, |
| 362 |
pjmedia_frame *output, |
| 363 |
pj_bool_t *has_more); |
| 364 |
static pj_status_t decode_vpx(pjmedia_vid_codec *codec, |
| 365 |
pj_size_t count, |
| 366 |
pjmedia_frame packets[], |
| 367 |
unsigned out_size, |
| 368 |
pjmedia_frame *output); |
| 369 |
#endif |
| 370 |
|
| 371 |
|
| 372 |
#if PJMEDIA_HAS_AND_MEDIA_H264 |
| 373 |
static pj_str_t H264_sw_encoder[] = {{(char *)"OMX.google.h264.encoder\0", |
| 374 |
23}}; |
| 375 |
static pj_str_t H264_hw_encoder[] = |
| 376 |
{{(char *)"OMX.qcom.video.encoder.avc\0", 26}, |
| 377 |
{(char *)"OMX.Exynos.avc.Encoder\0", 22}}; |
| 378 |
static pj_str_t H264_sw_decoder[] = {{(char *)"OMX.google.h264.decoder\0", |
| 379 |
23}}; |
| 380 |
static pj_str_t H264_hw_decoder[] = |
| 381 |
{{(char *)"OMX.qcom.video.decoder.avc\0", 26}, |
| 382 |
{(char *)"OMX.Exynos.avc.dec\0", 18}}; |
| 383 |
#endif |
| 384 |
|
| 385 |
#if PJMEDIA_HAS_AND_MEDIA_VP8 |
| 386 |
static pj_str_t VP8_sw_encoder[] = {{(char *)"OMX.google.vp8.encoder\0", 23}}; |
| 387 |
static pj_str_t VP8_hw_encoder[] = |
| 388 |
{{(char *)"OMX.qcom.video.encoder.vp8\0", 26}, |
| 389 |
{(char *)"OMX.Exynos.vp8.Encoder\0", 22}}; |
| 390 |
static pj_str_t VP8_sw_decoder[] = {{(char *)"OMX.google.vp8.decoder\0", 23}}; |
| 391 |
static pj_str_t VP8_hw_decoder[] = |
| 392 |
{{(char *)"OMX.qcom.video.decoder.vp8\0", 26}, |
| 393 |
{(char *)"OMX.Exynos.vp8.dec\0", 18}}; |
| 394 |
#endif |
| 395 |
|
| 396 |
#if PJMEDIA_HAS_AND_MEDIA_VP9 |
| 397 |
static pj_str_t VP9_sw_encoder[] = {{(char *)"OMX.google.vp9.encoder\0", 23}}; |
| 398 |
static pj_str_t VP9_hw_encoder[] = |
| 399 |
{{(char *)"OMX.qcom.video.encoder.vp9\0", 26}, |
| 400 |
{(char *)"OMX.Exynos.vp9.Encoder\0", 22}}; |
| 401 |
static pj_str_t VP9_sw_decoder[] = {{(char *)"OMX.google.vp9.decoder\0", 23}}; |
| 402 |
static pj_str_t VP9_hw_decoder[] = |
| 403 |
{{(char *)"OMX.qcom.video.decoder.vp9\0", 26}, |
| 404 |
{(char *)"OMX.Exynos.vp9.dec\0", 18}}; |
| 405 |
#endif |
| 406 |
|
| 407 |
static struct and_media_codec { |
| 408 |
int enabled; /* Is this codec enabled? */ |
| 409 |
const char *name; /* Codec name. */ |
| 410 |
const char *description; /* Codec description. */ |
| 411 |
const char *mime_type; /* Mime type. */ |
| 412 |
pj_str_t *encoder_name; /* Encoder name. */ |
| 413 |
pj_str_t *decoder_name; /* Decoder name. */ |
| 414 |
pj_uint8_t pt; /* Payload type. */ |
| 415 |
pjmedia_format_id fmt_id; /* Format id. */ |
| 416 |
pj_uint8_t keyframe_interval; /* Keyframe interval. */ |
| 417 |
|
| 418 |
open_cb open_codec; |
| 419 |
process_encode_cb process_encode; |
| 420 |
encode_more_cb encode_more; |
| 421 |
decode_cb decode; |
| 422 |
|
| 423 |
pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */ |
| 424 |
} |
| 425 |
and_media_codec[] = { |
| 426 |
#if PJMEDIA_HAS_AND_MEDIA_H264 |
| 427 |
{0, "H264", "Android MediaCodec H264 codec", "video/avc", |
| 428 |
NULL, NULL, |
| 429 |
AND_MED_H264_PT, PJMEDIA_FORMAT_H264, KEYFRAME_INTERVAL, |
| 430 |
&open_h264, &process_encode_h264, &encode_more_h264, &decode_h264, |
| 431 |
{2, {{{(char *)"profile-level-id", 16}, {(char *)"42e01e", 6}}, |
| 432 |
{{(char *)" packetization-mode", 19}, {(char *)"1", 1}}} |
| 433 |
} |
| 434 |
}, |
| 435 |
#endif |
| 436 |
#if PJMEDIA_HAS_AND_MEDIA_VP8 |
| 437 |
{0, "VP8", "Android MediaCodec VP8 codec", "video/x-vnd.on2.vp8", |
| 438 |
NULL, NULL, |
| 439 |
AND_MED_VP8_PT, PJMEDIA_FORMAT_VP8, KEYFRAME_INTERVAL, |
| 440 |
&open_vpx, NULL, &encode_more_vpx, &decode_vpx, |
| 441 |
{2, {{{(char *)"max-fr", 6}, {(char *)"30", 2}}, |
| 442 |
{{(char *)" max-fs", 7}, {(char *)"580", 3}}} |
| 443 |
} |
| 444 |
}, |
| 445 |
#endif |
| 446 |
#if PJMEDIA_HAS_AND_MEDIA_VP9 |
| 447 |
{0, "VP9", "Android MediaCodec VP9 codec", "video/x-vnd.on2.vp9", |
| 448 |
NULL, NULL, |
| 449 |
AND_MED_VP9_PT, PJMEDIA_FORMAT_VP9, KEYFRAME_INTERVAL, |
| 450 |
&open_vpx, NULL, &encode_more_vpx, &decode_vpx, |
| 451 |
{2, {{{(char *)"max-fr", 6}, {(char *)"30", 2}}, |
| 452 |
{{(char *)" max-fs", 7}, {(char *)"580", 3}}} |
| 453 |
} |
| 454 |
} |
| 455 |
#endif |
| 456 |
}; |
| 457 |
|
| 458 |
static pj_status_t configure_encoder(and_media_codec_data *and_media_data) |
| 459 |
{ |
| 460 |
media_status_t am_status; |
| 461 |
AMediaFormat *vid_fmt; |
| 462 |
pjmedia_vid_codec_param *param = and_media_data->prm; |
| 463 |
|
| 464 |
vid_fmt = AMediaFormat_new(); |
| 465 |
if (!vid_fmt) { |
| 466 |
PJ_LOG(4, (THIS_FILE, "Encoder failed creating media format")); |
| 467 |
return PJ_ENOMEM; |
| 468 |
} |
| 469 |
|
| 470 |
AMediaFormat_setString(vid_fmt, AND_MEDIA_KEY_MIME, |
| 471 |
and_media_codec[and_media_data->codec_idx].mime_type); |
| 472 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_COLOR_FMT, |
| 473 |
AND_MEDIA_I420_PLANAR_FMT); |
| 474 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_HEIGHT, |
| 475 |
param->enc_fmt.det.vid.size.h); |
| 476 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_WIDTH, |
| 477 |
param->enc_fmt.det.vid.size.w); |
| 478 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_BIT_RATE, |
| 479 |
param->enc_fmt.det.vid.avg_bps); |
| 480 |
//AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_PROFILE, 1); |
| 481 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_IFR_INTERVAL, |
| 482 |
KEYFRAME_INTERVAL); |
| 483 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_FRAME_RATE, |
| 484 |
(param->enc_fmt.det.vid.fps.num / |
| 485 |
param->enc_fmt.det.vid.fps.denum)); |
| 486 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_PRIORITY, 0); |
| 487 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_LATENCY, 1); |
| 488 |
|
| 489 |
/* Configure and start encoder. */ |
| 490 |
am_status = AMediaCodec_configure(and_media_data->enc, vid_fmt, NULL, NULL, |
| 491 |
AMEDIACODEC_CONFIGURE_FLAG_ENCODE); |
| 492 |
AMediaFormat_delete(vid_fmt); |
| 493 |
if (am_status != AMEDIA_OK) { |
| 494 |
PJ_LOG(4, (THIS_FILE, "Encoder configure failed, status=%d", |
| 495 |
am_status)); |
| 496 |
return PJMEDIA_CODEC_EFAILED; |
| 497 |
} |
| 498 |
am_status = AMediaCodec_start(and_media_data->enc); |
| 499 |
if (am_status != AMEDIA_OK) { |
| 500 |
PJ_LOG(4, (THIS_FILE, "Encoder start failed, status=%d", |
| 501 |
am_status)); |
| 502 |
return PJMEDIA_CODEC_EFAILED; |
| 503 |
} |
| 504 |
return PJ_SUCCESS; |
| 505 |
} |
| 506 |
|
| 507 |
static pj_status_t configure_decoder(and_media_codec_data *and_media_data) { |
| 508 |
media_status_t am_status; |
| 509 |
AMediaFormat *vid_fmt; |
| 510 |
|
| 511 |
vid_fmt = AMediaFormat_new(); |
| 512 |
if (!vid_fmt) { |
| 513 |
PJ_LOG(4, (THIS_FILE, "Decoder failed creating media format")); |
| 514 |
return PJ_ENOMEM; |
| 515 |
} |
| 516 |
AMediaFormat_setString(vid_fmt, AND_MEDIA_KEY_MIME, |
| 517 |
and_media_codec[and_media_data->codec_idx].mime_type); |
| 518 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_COLOR_FMT, |
| 519 |
AND_MEDIA_I420_PLANAR_FMT); |
| 520 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_HEIGHT, |
| 521 |
and_media_data->prm->dec_fmt.det.vid.size.h); |
| 522 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_WIDTH, |
| 523 |
and_media_data->prm->dec_fmt.det.vid.size.w); |
| 524 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_MAX_INPUT_SZ, 0); |
| 525 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_ENCODER, 0); |
| 526 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_PRIORITY, 0); |
| 527 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_LOW_LATENCY, 1); |
| 528 |
|
| 529 |
if (and_media_codec[and_media_data->codec_idx].fmt_id == |
| 530 |
PJMEDIA_FORMAT_H264) |
| 531 |
{ |
| 532 |
h264_codec_data *h264_data = (h264_codec_data *)and_media_data->ex_data; |
| 533 |
|
| 534 |
if (h264_data->dec_sps_len) { |
| 535 |
AMediaFormat_setBuffer(vid_fmt, AND_MEDIA_KEY_CSD0, |
| 536 |
h264_data->dec_sps_buf, |
| 537 |
h264_data->dec_sps_len); |
| 538 |
} |
| 539 |
if (h264_data->dec_pps_len) { |
| 540 |
AMediaFormat_setBuffer(vid_fmt, AND_MEDIA_KEY_CSD1, |
| 541 |
h264_data->dec_pps_buf, |
| 542 |
h264_data->dec_pps_len); |
| 543 |
} |
| 544 |
} |
| 545 |
am_status = AMediaCodec_configure(and_media_data->dec, vid_fmt, NULL, |
| 546 |
NULL, 0); |
| 547 |
|
| 548 |
AMediaFormat_delete(vid_fmt); |
| 549 |
if (am_status != AMEDIA_OK) { |
| 550 |
PJ_LOG(4, (THIS_FILE, "Decoder configure failed, status=%d, fmt_id=%d", |
| 551 |
am_status, and_media_data->prm->dec_fmt.id)); |
| 552 |
return PJMEDIA_CODEC_EFAILED; |
| 553 |
} |
| 554 |
|
| 555 |
am_status = AMediaCodec_start(and_media_data->dec); |
| 556 |
if (am_status != AMEDIA_OK) { |
| 557 |
PJ_LOG(4, (THIS_FILE, "Decoder start failed, status=%d", |
| 558 |
am_status)); |
| 559 |
return PJMEDIA_CODEC_EFAILED; |
| 560 |
} |
| 561 |
return PJ_SUCCESS; |
| 562 |
} |
| 563 |
|
| 564 |
PJ_DEF(pj_status_t) pjmedia_codec_and_media_vid_init( |
| 565 |
pjmedia_vid_codec_mgr *mgr, |
| 566 |
pj_pool_factory *pf) |
| 567 |
{ |
| 568 |
const pj_str_t h264_name = { (char*)"H264", 4}; |
| 569 |
pj_status_t status; |
| 570 |
int api_level = android_get_device_api_level(); |
| 571 |
|
| 572 |
if (api_level < 28) { |
| 573 |
PJ_LOG(4,(THIS_FILE, "Minimum API level 28," |
| 574 |
"Android MediaCodec cannot work with API level %d", |
| 575 |
api_level)); |
| 576 |
|
| 577 |
return PJ_SUCCESS; |
| 578 |
} |
| 579 |
|
| 580 |
if (and_media_factory.pool != NULL) { |
| 581 |
/* Already initialized. */ |
| 582 |
return PJ_SUCCESS; |
| 583 |
} |
| 584 |
|
| 585 |
if (!mgr) mgr = pjmedia_vid_codec_mgr_instance(); |
| 586 |
PJ_ASSERT_RETURN(mgr, PJ_EINVAL); |
| 587 |
|
| 588 |
/* Create Android AMediaCodec codec factory. */ |
| 589 |
and_media_factory.base.op = &and_media_factory_op; |
| 590 |
and_media_factory.base.factory_data = NULL; |
| 591 |
and_media_factory.mgr = mgr; |
| 592 |
and_media_factory.pf = pf; |
| 593 |
and_media_factory.pool = pj_pool_create(pf, "and_media_vid_factory", |
| 594 |
256, 256, NULL); |
| 595 |
if (!and_media_factory.pool) |
| 596 |
return PJ_ENOMEM; |
| 597 |
|
| 598 |
#if PJMEDIA_HAS_AND_MEDIA_H264 |
| 599 |
/* Registering format match for SDP negotiation */ |
| 600 |
status = pjmedia_sdp_neg_register_fmt_match_cb( |
| 601 |
&h264_name, |
| 602 |
&pjmedia_vid_codec_h264_match_sdp); |
| 603 |
if (status != PJ_SUCCESS) |
| 604 |
goto on_error; |
| 605 |
#endif |
| 606 |
|
| 607 |
/* Register codec factory to codec manager. */ |
| 608 |
status = pjmedia_vid_codec_mgr_register_factory(mgr, |
| 609 |
&and_media_factory.base); |
| 610 |
if (status != PJ_SUCCESS) |
| 611 |
goto on_error; |
| 612 |
|
| 613 |
PJ_LOG(4,(THIS_FILE, "Android AMediaCodec initialized")); |
| 614 |
|
| 615 |
/* Done. */ |
| 616 |
return PJ_SUCCESS; |
| 617 |
|
| 618 |
on_error: |
| 619 |
pj_pool_release(and_media_factory.pool); |
| 620 |
and_media_factory.pool = NULL; |
| 621 |
return status; |
| 622 |
} |
| 623 |
|
| 624 |
/* |
| 625 |
* Unregister Android AMediaCodec factory from pjmedia endpoint. |
| 626 |
*/ |
| 627 |
PJ_DEF(pj_status_t) pjmedia_codec_and_media_vid_deinit(void) |
| 628 |
{ |
| 629 |
pj_status_t status = PJ_SUCCESS; |
| 630 |
|
| 631 |
if (and_media_factory.pool == NULL) { |
| 632 |
/* Already deinitialized */ |
| 633 |
return PJ_SUCCESS; |
| 634 |
} |
| 635 |
|
| 636 |
/* Unregister Android AMediaCodec factory. */ |
| 637 |
status = pjmedia_vid_codec_mgr_unregister_factory(and_media_factory.mgr, |
| 638 |
&and_media_factory.base); |
| 639 |
|
| 640 |
/* Destroy pool. */ |
| 641 |
pj_pool_release(and_media_factory.pool); |
| 642 |
and_media_factory.pool = NULL; |
| 643 |
|
| 644 |
return status; |
| 645 |
} |
| 646 |
|
| 647 |
static pj_status_t and_media_test_alloc(pjmedia_vid_codec_factory *factory, |
| 648 |
const pjmedia_vid_codec_info *info ) |
| 649 |
{ |
| 650 |
unsigned i; |
| 651 |
|
| 652 |
PJ_ASSERT_RETURN(factory == &and_media_factory.base, PJ_EINVAL); |
| 653 |
|
| 654 |
for (i = 0; i < PJ_ARRAY_SIZE(and_media_codec); ++i) { |
| 655 |
if (and_media_codec[i].enabled && info->pt == and_media_codec[i].pt && |
| 656 |
(info->fmt_id == and_media_codec[i].fmt_id)) |
| 657 |
{ |
| 658 |
return PJ_SUCCESS; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
return PJMEDIA_CODEC_EUNSUP; |
| 663 |
} |
| 664 |
|
| 665 |
static pj_status_t and_media_default_attr(pjmedia_vid_codec_factory *factory, |
| 666 |
const pjmedia_vid_codec_info *info, |
| 667 |
pjmedia_vid_codec_param *attr ) |
| 668 |
{ |
| 669 |
unsigned i; |
| 670 |
|
| 671 |
PJ_ASSERT_RETURN(factory == &and_media_factory.base, PJ_EINVAL); |
| 672 |
PJ_ASSERT_RETURN(info && attr, PJ_EINVAL); |
| 673 |
|
| 674 |
for (i = 0; i < PJ_ARRAY_SIZE(and_media_codec); ++i) { |
| 675 |
if (and_media_codec[i].enabled && info->pt != 0 && |
| 676 |
(info->fmt_id == and_media_codec[i].fmt_id)) |
| 677 |
{ |
| 678 |
break; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
if (i == PJ_ARRAY_SIZE(and_media_codec)) |
| 683 |
return PJ_EINVAL; |
| 684 |
|
| 685 |
pj_bzero(attr, sizeof(pjmedia_vid_codec_param)); |
| 686 |
|
| 687 |
attr->dir = PJMEDIA_DIR_ENCODING_DECODING; |
| 688 |
attr->packing = PJMEDIA_VID_PACKING_PACKETS; |
| 689 |
|
| 690 |
/* Encoded format */ |
| 691 |
pjmedia_format_init_video(&attr->enc_fmt, info->fmt_id, |
| 692 |
DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS, 1); |
| 693 |
|
| 694 |
/* Decoded format */ |
| 695 |
pjmedia_format_init_video(&attr->dec_fmt, PJMEDIA_FORMAT_I420, |
| 696 |
DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS, 1); |
| 697 |
|
| 698 |
attr->dec_fmtp = and_media_codec[i].dec_fmtp; |
| 699 |
|
| 700 |
/* Bitrate */ |
| 701 |
attr->enc_fmt.det.vid.avg_bps = DEFAULT_AVG_BITRATE; |
| 702 |
attr->enc_fmt.det.vid.max_bps = DEFAULT_MAX_BITRATE; |
| 703 |
|
| 704 |
/* Encoding MTU */ |
| 705 |
attr->enc_mtu = PJMEDIA_MAX_VID_PAYLOAD_SIZE; |
| 706 |
|
| 707 |
return PJ_SUCCESS; |
| 708 |
} |
| 709 |
|
| 710 |
static pj_bool_t codec_exists(const pj_str_t *codec_name) |
| 711 |
{ |
| 712 |
AMediaCodec *codec; |
| 713 |
char *codec_txt; |
| 714 |
|
| 715 |
codec_txt = codec_name->ptr; |
| 716 |
|
| 717 |
codec = AMediaCodec_createCodecByName(codec_txt); |
| 718 |
if (!codec) { |
| 719 |
PJ_LOG(4, (THIS_FILE, "Failed creating codec : %.*s", |
| 720 |
(int)codec_name->slen, codec_name->ptr)); |
| 721 |
return PJ_FALSE; |
| 722 |
} |
| 723 |
AMediaCodec_delete(codec); |
| 724 |
|
| 725 |
return PJ_TRUE; |
| 726 |
} |
| 727 |
|
| 728 |
void add_codec(struct and_media_codec *codec, |
| 729 |
unsigned *count, pjmedia_vid_codec_info *info) |
| 730 |
{ |
| 731 |
info[*count].fmt_id = codec->fmt_id; |
| 732 |
info[*count].pt = codec->pt; |
| 733 |
info[*count].encoding_name = pj_str((char *)codec->name); |
| 734 |
info[*count].encoding_desc = pj_str((char *)codec->description); |
| 735 |
|
| 736 |
info[*count].clock_rate = 90000; |
| 737 |
info[*count].dir = PJMEDIA_DIR_ENCODING_DECODING; |
| 738 |
info[*count].dec_fmt_id_cnt = 1; |
| 739 |
info[*count].dec_fmt_id[0] = PJMEDIA_FORMAT_I420; |
| 740 |
info[*count].packings = PJMEDIA_VID_PACKING_PACKETS; |
| 741 |
info[*count].fps_cnt = 3; |
| 742 |
info[*count].fps[0].num = 15; |
| 743 |
info[*count].fps[0].denum = 1; |
| 744 |
info[*count].fps[1].num = 25; |
| 745 |
info[*count].fps[1].denum = 1; |
| 746 |
info[*count].fps[2].num = 30; |
| 747 |
info[*count].fps[2].denum = 1; |
| 748 |
++*count; |
| 749 |
} |
| 750 |
|
| 751 |
static void get_codec_name(pj_bool_t is_enc, |
| 752 |
pj_bool_t prio, |
| 753 |
pjmedia_format_id fmt_id, |
| 754 |
pj_str_t **codec_name, |
| 755 |
unsigned *codec_num) |
| 756 |
{ |
| 757 |
pj_bool_t use_sw_enc = PJMEDIA_AND_MEDIA_PRIO_SW_VID_ENC; |
| 758 |
pj_bool_t use_sw_dec = PJMEDIA_AND_MEDIA_PRIO_SW_VID_DEC; |
| 759 |
|
| 760 |
*codec_num = 0; |
| 761 |
|
| 762 |
switch (fmt_id) { |
| 763 |
|
| 764 |
#if PJMEDIA_HAS_AND_MEDIA_H264 |
| 765 |
case PJMEDIA_FORMAT_H264: |
| 766 |
if (is_enc) { |
| 767 |
if ((prio && use_sw_enc) || (!prio && !use_sw_enc)) { |
| 768 |
*codec_name = &H264_sw_encoder[0]; |
| 769 |
*codec_num = PJ_ARRAY_SIZE(H264_sw_encoder); |
| 770 |
} else { |
| 771 |
*codec_name = &H264_hw_encoder[0]; |
| 772 |
*codec_num = PJ_ARRAY_SIZE(H264_hw_encoder); |
| 773 |
} |
| 774 |
} else { |
| 775 |
if ((prio && use_sw_dec) || (!prio && !use_sw_dec)) { |
| 776 |
*codec_name = &H264_sw_decoder[0]; |
| 777 |
*codec_num = PJ_ARRAY_SIZE(H264_sw_decoder); |
| 778 |
} else { |
| 779 |
*codec_name = &H264_hw_decoder[0]; |
| 780 |
*codec_num = PJ_ARRAY_SIZE(H264_hw_decoder); |
| 781 |
} |
| 782 |
} |
| 783 |
break; |
| 784 |
#endif |
| 785 |
#if PJMEDIA_HAS_AND_MEDIA_VP8 |
| 786 |
case PJMEDIA_FORMAT_VP8: |
| 787 |
if (is_enc) { |
| 788 |
if ((prio && use_sw_enc) || (!prio && !use_sw_enc)) { |
| 789 |
*codec_name = &VP8_sw_encoder[0]; |
| 790 |
*codec_num = PJ_ARRAY_SIZE(VP8_sw_encoder); |
| 791 |
} else { |
| 792 |
*codec_name = &VP8_hw_encoder[0]; |
| 793 |
*codec_num = PJ_ARRAY_SIZE(VP8_hw_encoder); |
| 794 |
} |
| 795 |
} else { |
| 796 |
if ((prio && use_sw_dec) || (!prio && !use_sw_dec)) { |
| 797 |
*codec_name = &VP8_sw_decoder[0]; |
| 798 |
*codec_num = PJ_ARRAY_SIZE(VP8_sw_decoder); |
| 799 |
} else { |
| 800 |
*codec_name = &VP8_hw_decoder[0]; |
| 801 |
*codec_num = PJ_ARRAY_SIZE(VP8_hw_decoder); |
| 802 |
} |
| 803 |
} |
| 804 |
break; |
| 805 |
#endif |
| 806 |
#if PJMEDIA_HAS_AND_MEDIA_VP9 |
| 807 |
case PJMEDIA_FORMAT_VP9: |
| 808 |
if (is_enc) { |
| 809 |
if ((prio && use_sw_enc) || (!prio && !use_sw_enc)) { |
| 810 |
*codec_name = &VP9_sw_encoder[0]; |
| 811 |
*codec_num = PJ_ARRAY_SIZE(VP9_sw_encoder); |
| 812 |
} else { |
| 813 |
*codec_name = &VP9_hw_encoder[0]; |
| 814 |
*codec_num = PJ_ARRAY_SIZE(VP9_hw_encoder); |
| 815 |
} |
| 816 |
} else { |
| 817 |
if ((prio && use_sw_dec) || (!prio && !use_sw_dec)) { |
| 818 |
*codec_name = &VP9_sw_decoder[0]; |
| 819 |
*codec_num = PJ_ARRAY_SIZE(VP9_sw_decoder); |
| 820 |
} else { |
| 821 |
*codec_name = &VP9_hw_decoder[0]; |
| 822 |
*codec_num = PJ_ARRAY_SIZE(VP9_hw_decoder); |
| 823 |
} |
| 824 |
} |
| 825 |
break; |
| 826 |
#endif |
| 827 |
default: |
| 828 |
break; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
static pj_status_t and_media_enum_info(pjmedia_vid_codec_factory *factory, |
| 833 |
unsigned *count, |
| 834 |
pjmedia_vid_codec_info info[]) |
| 835 |
{ |
| 836 |
unsigned i, max; |
| 837 |
|
| 838 |
PJ_ASSERT_RETURN(info && *count > 0, PJ_EINVAL); |
| 839 |
PJ_ASSERT_RETURN(factory == &and_media_factory.base, PJ_EINVAL); |
| 840 |
|
| 841 |
max = *count; |
| 842 |
|
| 843 |
for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(and_media_codec) && *count < max; |
| 844 |
++i) |
| 845 |
{ |
| 846 |
unsigned enc_idx = 0; |
| 847 |
unsigned dec_idx = 0; |
| 848 |
pj_str_t *enc_name = NULL; |
| 849 |
unsigned num_enc; |
| 850 |
pj_str_t *dec_name = NULL; |
| 851 |
unsigned num_dec; |
| 852 |
|
| 853 |
get_codec_name(PJ_TRUE, PJ_TRUE, and_media_codec[i].fmt_id, |
| 854 |
&enc_name, &num_enc); |
| 855 |
|
| 856 |
for (enc_idx = 0; enc_idx < num_enc ;++enc_idx, ++enc_name) { |
| 857 |
if (codec_exists(enc_name)) { |
| 858 |
break; |
| 859 |
} |
| 860 |
} |
| 861 |
if (enc_idx == num_enc) { |
| 862 |
get_codec_name(PJ_TRUE, PJ_FALSE, and_media_codec[i].fmt_id, |
| 863 |
&enc_name, &num_enc); |
| 864 |
|
| 865 |
for (enc_idx = 0; enc_idx < num_enc ;++enc_idx, ++enc_name) { |
| 866 |
if (codec_exists(enc_name)) { |
| 867 |
break; |
| 868 |
} |
| 869 |
} |
| 870 |
if (enc_idx == num_enc) |
| 871 |
continue; |
| 872 |
} |
| 873 |
|
| 874 |
get_codec_name(PJ_FALSE, PJ_TRUE, and_media_codec[i].fmt_id, |
| 875 |
&dec_name, &num_dec); |
| 876 |
for (dec_idx = 0; dec_idx < num_dec ;++dec_idx, ++dec_name) { |
| 877 |
if (codec_exists(dec_name)) { |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
if (dec_idx == num_dec) { |
| 882 |
get_codec_name(PJ_FALSE, PJ_FALSE, and_media_codec[i].fmt_id, |
| 883 |
&dec_name, &num_dec); |
| 884 |
for (enc_idx = 0; enc_idx < num_enc ;++enc_idx, ++enc_name) { |
| 885 |
if (codec_exists(enc_name)) { |
| 886 |
break; |
| 887 |
} |
| 888 |
} |
| 889 |
if (dec_idx == num_dec) |
| 890 |
continue; |
| 891 |
} |
| 892 |
|
| 893 |
and_media_codec[i].encoder_name = enc_name; |
| 894 |
and_media_codec[i].decoder_name = dec_name; |
| 895 |
PJ_LOG(4, (THIS_FILE, "Found encoder [%d]: %.*s and decoder: %.*s ", |
| 896 |
*count, (int)enc_name->slen, enc_name->ptr, |
| 897 |
(int)dec_name->slen, dec_name->ptr)); |
| 898 |
add_codec(&and_media_codec[*count], count, info); |
| 899 |
and_media_codec[i].enabled = PJ_TRUE; |
| 900 |
} |
| 901 |
|
| 902 |
return PJ_SUCCESS; |
| 903 |
} |
| 904 |
|
| 905 |
static void create_codec(struct and_media_codec_data *and_media_data) |
| 906 |
{ |
| 907 |
char *enc_name; |
| 908 |
char *dec_name; |
| 909 |
|
| 910 |
if (!and_media_codec[and_media_data->codec_idx].encoder_name || |
| 911 |
!and_media_codec[and_media_data->codec_idx].decoder_name) |
| 912 |
{ |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
enc_name = and_media_codec[and_media_data->codec_idx].encoder_name->ptr; |
| 917 |
dec_name = and_media_codec[and_media_data->codec_idx].decoder_name->ptr; |
| 918 |
|
| 919 |
if (!and_media_data->enc) { |
| 920 |
and_media_data->enc = AMediaCodec_createCodecByName(enc_name); |
| 921 |
if (!and_media_data->enc) { |
| 922 |
PJ_LOG(4, (THIS_FILE, "Failed creating encoder: %s", enc_name)); |
| 923 |
} |
| 924 |
pj_atomic_queue_create(and_media_data->pool, |
| 925 |
BUFFER_MAX_ITEM, |
| 926 |
sizeof(and_med_buf_info), |
| 927 |
"enc_input_buf", |
| 928 |
&and_media_data->enc_avail_input_buf); |
| 929 |
pj_atomic_queue_create(and_media_data->pool, |
| 930 |
BUFFER_MAX_ITEM, |
| 931 |
sizeof(and_med_buf_info), |
| 932 |
"enc_output_buf", |
| 933 |
&and_media_data->enc_avail_output_buf); |
| 934 |
} |
| 935 |
|
| 936 |
if (!and_media_data->dec) { |
| 937 |
and_media_data->dec = AMediaCodec_createCodecByName(dec_name); |
| 938 |
if (!and_media_data->dec) { |
| 939 |
PJ_LOG(4, (THIS_FILE, "Failed creating decoder: %s", dec_name)); |
| 940 |
} |
| 941 |
pj_atomic_queue_create(and_media_data->pool, |
| 942 |
BUFFER_MAX_ITEM, |
| 943 |
sizeof(and_med_buf_info), |
| 944 |
"dec_input_buf", |
| 945 |
&and_media_data->dec_avail_input_buf); |
| 946 |
pj_atomic_queue_create(and_media_data->pool, |
| 947 |
BUFFER_MAX_ITEM, |
| 948 |
sizeof(and_med_buf_info), |
| 949 |
"dec_output_buf", |
| 950 |
&and_media_data->dec_avail_output_buf); |
| 951 |
} |
| 952 |
|
| 953 |
PJ_LOG(4, (THIS_FILE, "Created encoder: %s, decoder: %s", enc_name, |
| 954 |
dec_name)); |
| 955 |
} |
| 956 |
|
| 957 |
static pj_status_t and_media_alloc_codec(pjmedia_vid_codec_factory *factory, |
| 958 |
const pjmedia_vid_codec_info *info, |
| 959 |
pjmedia_vid_codec **p_codec) |
| 960 |
{ |
| 961 |
pj_pool_t *pool; |
| 962 |
pjmedia_vid_codec *codec; |
| 963 |
and_media_codec_data *and_media_data; |
| 964 |
int i, idx; |
| 965 |
|
| 966 |
PJ_ASSERT_RETURN(factory == &and_media_factory.base && info && p_codec, |
| 967 |
PJ_EINVAL); |
| 968 |
|
| 969 |
idx = -1; |
| 970 |
for (i = 0; i < PJ_ARRAY_SIZE(and_media_codec); ++i) { |
| 971 |
if ((info->fmt_id == and_media_codec[i].fmt_id) && |
| 972 |
(and_media_codec[i].enabled)) |
| 973 |
{ |
| 974 |
idx = i; |
| 975 |
break; |
| 976 |
} |
| 977 |
} |
| 978 |
if (idx == -1) { |
| 979 |
*p_codec = NULL; |
| 980 |
return PJMEDIA_CODEC_EFAILED; |
| 981 |
} |
| 982 |
|
| 983 |
*p_codec = NULL; |
| 984 |
pool = pj_pool_create(and_media_factory.pf, "anmedvid%p", 512, 512, NULL); |
| 985 |
if (!pool) |
| 986 |
return PJ_ENOMEM; |
| 987 |
|
| 988 |
/* codec instance */ |
| 989 |
codec = PJ_POOL_ZALLOC_T(pool, pjmedia_vid_codec); |
| 990 |
codec->factory = factory; |
| 991 |
codec->op = &and_media_codec_op; |
| 992 |
|
| 993 |
/* codec data */ |
| 994 |
and_media_data = PJ_POOL_ZALLOC_T(pool, and_media_codec_data); |
| 995 |
and_media_data->pool = pool; |
| 996 |
and_media_data->codec_idx = idx; |
| 997 |
codec->codec_data = and_media_data; |
| 998 |
|
| 999 |
create_codec(and_media_data); |
| 1000 |
if (!and_media_data->enc || !and_media_data->dec) { |
| 1001 |
goto on_error; |
| 1002 |
} |
| 1003 |
|
| 1004 |
*p_codec = codec; |
| 1005 |
return PJ_SUCCESS; |
| 1006 |
|
| 1007 |
on_error: |
| 1008 |
and_media_dealloc_codec(factory, codec); |
| 1009 |
return PJMEDIA_CODEC_EFAILED; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static pj_status_t and_media_dealloc_codec(pjmedia_vid_codec_factory *factory, |
| 1013 |
pjmedia_vid_codec *codec ) |
| 1014 |
{ |
| 1015 |
and_media_codec_data *and_media_data; |
| 1016 |
|
| 1017 |
PJ_ASSERT_RETURN(codec, PJ_EINVAL); |
| 1018 |
|
| 1019 |
PJ_UNUSED_ARG(factory); |
| 1020 |
|
| 1021 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1022 |
if (and_media_data->enc) { |
| 1023 |
AMediaCodec_stop(and_media_data->enc); |
| 1024 |
AMediaCodec_delete(and_media_data->enc); |
| 1025 |
and_media_data->enc = NULL; |
| 1026 |
pj_atomic_queue_destroy(and_media_data->enc_avail_input_buf); |
| 1027 |
and_media_data->enc_avail_input_buf = NULL; |
| 1028 |
pj_atomic_queue_destroy(and_media_data->enc_avail_output_buf); |
| 1029 |
and_media_data->enc_avail_output_buf = NULL; |
| 1030 |
} |
| 1031 |
|
| 1032 |
if (and_media_data->dec) { |
| 1033 |
AMediaCodec_stop(and_media_data->dec); |
| 1034 |
AMediaCodec_delete(and_media_data->dec); |
| 1035 |
and_media_data->dec = NULL; |
| 1036 |
pj_atomic_queue_destroy(and_media_data->dec_avail_input_buf); |
| 1037 |
and_media_data->dec_avail_input_buf = NULL; |
| 1038 |
pj_atomic_queue_destroy(and_media_data->dec_avail_output_buf); |
| 1039 |
and_media_data->dec_avail_output_buf = NULL; |
| 1040 |
} |
| 1041 |
pj_pool_release(and_media_data->pool); |
| 1042 |
return PJ_SUCCESS; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static pj_status_t and_media_codec_init(pjmedia_vid_codec *codec, |
| 1046 |
pj_pool_t *pool ) |
| 1047 |
{ |
| 1048 |
PJ_ASSERT_RETURN(codec && pool, PJ_EINVAL); |
| 1049 |
PJ_UNUSED_ARG(codec); |
| 1050 |
PJ_UNUSED_ARG(pool); |
| 1051 |
return PJ_SUCCESS; |
| 1052 |
} |
| 1053 |
|
| 1054 |
static pj_status_t and_media_codec_open(pjmedia_vid_codec *codec, |
| 1055 |
pjmedia_vid_codec_param *codec_param) |
| 1056 |
{ |
| 1057 |
and_media_codec_data *and_media_data; |
| 1058 |
pjmedia_vid_codec_param *param; |
| 1059 |
pj_status_t status = PJ_SUCCESS; |
| 1060 |
|
| 1061 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1062 |
and_media_data->prm = pjmedia_vid_codec_param_clone( and_media_data->pool, |
| 1063 |
codec_param); |
| 1064 |
param = and_media_data->prm; |
| 1065 |
if (and_media_codec[and_media_data->codec_idx].open_codec) { |
| 1066 |
status = and_media_codec[and_media_data->codec_idx].open_codec( |
| 1067 |
and_media_data); |
| 1068 |
if (status != PJ_SUCCESS) |
| 1069 |
return status; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (API_AT_LEAST(28)) { |
| 1073 |
AMediaCodecOnAsyncNotifyCallback async_cb = {&and_med_on_input_avail, |
| 1074 |
&and_med_on_output_avail, |
| 1075 |
&and_med_on_format_changed, |
| 1076 |
&and_med_on_error}; |
| 1077 |
|
| 1078 |
AMediaCodec_setAsyncNotifyCallback(and_media_data->enc, async_cb, |
| 1079 |
and_media_data); |
| 1080 |
AMediaCodec_setAsyncNotifyCallback(and_media_data->dec, async_cb, |
| 1081 |
and_media_data); |
| 1082 |
} |
| 1083 |
and_media_data->whole = (param->packing == PJMEDIA_VID_PACKING_WHOLE); |
| 1084 |
status = configure_encoder(and_media_data); |
| 1085 |
if (status != PJ_SUCCESS) { |
| 1086 |
return PJMEDIA_CODEC_EFAILED; |
| 1087 |
} |
| 1088 |
status = configure_decoder(and_media_data); |
| 1089 |
if (status != PJ_SUCCESS) { |
| 1090 |
return PJMEDIA_CODEC_EFAILED; |
| 1091 |
} |
| 1092 |
if (and_media_data->dec_buf_size == 0) { |
| 1093 |
and_media_data->dec_buf_size = (MAX_RX_WIDTH * MAX_RX_HEIGHT * 3 >> 1) + |
| 1094 |
(MAX_RX_WIDTH); |
| 1095 |
} |
| 1096 |
and_media_data->dec_buf = (pj_uint8_t*)pj_pool_alloc(and_media_data->pool, |
| 1097 |
and_media_data->dec_buf_size); |
| 1098 |
/* Need to update param back after values are negotiated */ |
| 1099 |
pj_memcpy(codec_param, param, sizeof(*codec_param)); |
| 1100 |
|
| 1101 |
return PJ_SUCCESS; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static pj_status_t and_media_codec_close(pjmedia_vid_codec *codec) |
| 1105 |
{ |
| 1106 |
PJ_ASSERT_RETURN(codec, PJ_EINVAL); |
| 1107 |
PJ_UNUSED_ARG(codec); |
| 1108 |
return PJ_SUCCESS; |
| 1109 |
} |
| 1110 |
|
| 1111 |
static pj_status_t and_media_codec_modify(pjmedia_vid_codec *codec, |
| 1112 |
const pjmedia_vid_codec_param *param) |
| 1113 |
{ |
| 1114 |
PJ_ASSERT_RETURN(codec && param, PJ_EINVAL); |
| 1115 |
PJ_UNUSED_ARG(codec); |
| 1116 |
PJ_UNUSED_ARG(param); |
| 1117 |
return PJ_EINVALIDOP; |
| 1118 |
} |
| 1119 |
|
| 1120 |
static pj_status_t and_media_codec_get_param(pjmedia_vid_codec *codec, |
| 1121 |
pjmedia_vid_codec_param *param) |
| 1122 |
{ |
| 1123 |
struct and_media_codec_data *and_media_data; |
| 1124 |
|
| 1125 |
PJ_ASSERT_RETURN(codec && param, PJ_EINVAL); |
| 1126 |
|
| 1127 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1128 |
pj_memcpy(param, and_media_data->prm, sizeof(*param)); |
| 1129 |
|
| 1130 |
return PJ_SUCCESS; |
| 1131 |
} |
| 1132 |
|
| 1133 |
static pj_status_t and_media_codec_encode_begin(pjmedia_vid_codec *codec, |
| 1134 |
const pjmedia_vid_encode_opt *opt, |
| 1135 |
const pjmedia_frame *input, |
| 1136 |
unsigned out_size, |
| 1137 |
pjmedia_frame *output, |
| 1138 |
pj_bool_t *has_more) |
| 1139 |
{ |
| 1140 |
struct and_media_codec_data *and_media_data; |
| 1141 |
and_med_buf_info buf_info; |
| 1142 |
media_status_t am_status; |
| 1143 |
pj_size_t output_size; |
| 1144 |
pj_uint8_t *input_buf = NULL; |
| 1145 |
pj_uint8_t *output_buf; |
| 1146 |
pj_atomic_queue_t *queue; |
| 1147 |
|
| 1148 |
PJ_ASSERT_RETURN(codec && input && out_size && output && has_more, |
| 1149 |
PJ_EINVAL); |
| 1150 |
|
| 1151 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1152 |
pj_bzero(&buf_info, sizeof(buf_info)); |
| 1153 |
|
| 1154 |
if (opt && opt->force_keyframe) { |
| 1155 |
#if __ANDROID_API__ >=26 |
| 1156 |
AMediaFormat *vid_fmt = NULL; |
| 1157 |
media_status_t am_status; |
| 1158 |
|
| 1159 |
vid_fmt = AMediaFormat_new(); |
| 1160 |
if (!vid_fmt) { |
| 1161 |
return PJMEDIA_CODEC_EFAILED; |
| 1162 |
} |
| 1163 |
AMediaFormat_setInt32(vid_fmt, AND_MEDIA_KEY_REQUEST_SYNCF, 0); |
| 1164 |
am_status = AMediaCodec_setParameters(and_media_data->enc, vid_fmt); |
| 1165 |
|
| 1166 |
if (am_status != AMEDIA_OK) |
| 1167 |
PJ_LOG(4,(THIS_FILE, "Encoder setParameters failed %d", am_status)); |
| 1168 |
|
| 1169 |
AMediaFormat_delete(vid_fmt); |
| 1170 |
#else |
| 1171 |
PJ_LOG(5, (THIS_FILE, "Encoder cannot be forced to send keyframe")); |
| 1172 |
#endif |
| 1173 |
} |
| 1174 |
|
| 1175 |
queue = and_media_data->enc_avail_input_buf; |
| 1176 |
if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS || |
| 1177 |
buf_info.index < 0) |
| 1178 |
{ |
| 1179 |
PJ_LOG(4,(THIS_FILE, "Encoder failed to get input Buffer[%d]", |
| 1180 |
buf_info.index)); |
| 1181 |
goto on_return; |
| 1182 |
} |
| 1183 |
|
| 1184 |
input_buf = AMediaCodec_getInputBuffer(and_media_data->enc, |
| 1185 |
buf_info.index, &output_size); |
| 1186 |
if (input_buf && output_size >= input->size) { |
| 1187 |
pj_memcpy(input_buf, input->buf, input->size); |
| 1188 |
am_status = AMediaCodec_queueInputBuffer(and_media_data->enc, |
| 1189 |
buf_info.index, 0, input->size, 0, 0); |
| 1190 |
if (am_status != AMEDIA_OK) { |
| 1191 |
PJ_LOG(4, (THIS_FILE, "Encoder queueInputBuffer return %d", |
| 1192 |
am_status)); |
| 1193 |
goto on_return; |
| 1194 |
} |
| 1195 |
} else { |
| 1196 |
if (!input_buf) { |
| 1197 |
PJ_LOG(4,(THIS_FILE, "Encoder getInputBuffer " |
| 1198 |
"returns no input buff")); |
| 1199 |
} else { |
| 1200 |
PJ_LOG(4,(THIS_FILE, "Encoder getInputBuffer " |
| 1201 |
"size: %lu, expecting %lu.", |
| 1202 |
(unsigned long)output_size, |
| 1203 |
(unsigned long)input->size)); |
| 1204 |
} |
| 1205 |
goto on_return; |
| 1206 |
} |
| 1207 |
|
| 1208 |
queue = and_media_data->enc_avail_output_buf; |
| 1209 |
if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS || |
| 1210 |
buf_info.index < 0) |
| 1211 |
{ |
| 1212 |
PJ_LOG(4, (THIS_FILE, "Encoder failed to get output Buffer[%d]", |
| 1213 |
buf_info.index)); |
| 1214 |
goto on_return; |
| 1215 |
} |
| 1216 |
and_media_data->enc_output_buf_idx = buf_info.index; |
| 1217 |
and_media_data->enc_buf_info.size = buf_info.size; |
| 1218 |
and_media_data->enc_buf_info.flags = buf_info.flags; |
| 1219 |
output_buf = AMediaCodec_getOutputBuffer(and_media_data->enc, |
| 1220 |
buf_info.index, |
| 1221 |
&output_size); |
| 1222 |
if (!output_buf) { |
| 1223 |
PJ_LOG(4, (THIS_FILE, "Encoder failed getting output buffer, " |
| 1224 |
"buffer size %d, offset %d, flags %d", |
| 1225 |
and_media_data->enc_buf_info.size, |
| 1226 |
and_media_data->enc_buf_info.offset, |
| 1227 |
and_media_data->enc_buf_info.flags)); |
| 1228 |
goto on_return; |
| 1229 |
} |
| 1230 |
and_media_data->enc_processed = 0; |
| 1231 |
and_media_data->enc_frame_whole = output_buf; |
| 1232 |
and_media_data->enc_output_buf_idx = buf_info.index; |
| 1233 |
and_media_data->enc_frame_size = and_media_data->enc_buf_info.size; |
| 1234 |
|
| 1235 |
if (and_media_codec[and_media_data->codec_idx].process_encode) { |
| 1236 |
pj_status_t status; |
| 1237 |
|
| 1238 |
status = and_media_codec[and_media_data->codec_idx].process_encode( |
| 1239 |
and_media_data); |
| 1240 |
|
| 1241 |
if (status != PJ_SUCCESS) |
| 1242 |
goto on_return; |
| 1243 |
} |
| 1244 |
|
| 1245 |
if(and_media_data->enc_buf_info.flags & AND_MEDIA_FRM_TYPE_KEYFRAME) { |
| 1246 |
output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME; |
| 1247 |
} |
| 1248 |
|
| 1249 |
if (and_media_data->whole) { |
| 1250 |
unsigned payload_size = 0; |
| 1251 |
unsigned start_data = 0; |
| 1252 |
|
| 1253 |
*has_more = PJ_FALSE; |
| 1254 |
|
| 1255 |
if ((and_media_data->prm->enc_fmt.id == PJMEDIA_FORMAT_H264) && |
| 1256 |
(and_media_data->enc_buf_info.flags & |
| 1257 |
AND_MEDIA_FRM_TYPE_KEYFRAME)) |
| 1258 |
{ |
| 1259 |
h264_codec_data *h264_data = |
| 1260 |
(h264_codec_data *)and_media_data->ex_data; |
| 1261 |
start_data = h264_data->enc_sps_pps_len; |
| 1262 |
pj_memcpy(output->buf, h264_data->enc_sps_pps_buf, |
| 1263 |
h264_data->enc_sps_pps_len); |
| 1264 |
} |
| 1265 |
|
| 1266 |
payload_size = and_media_data->enc_buf_info.size + start_data; |
| 1267 |
|
| 1268 |
if (payload_size > out_size) |
| 1269 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 1270 |
|
| 1271 |
output->type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 1272 |
output->size = payload_size; |
| 1273 |
output->timestamp = input->timestamp; |
| 1274 |
pj_memcpy((pj_uint8_t*)output->buf+start_data, |
| 1275 |
and_media_data->enc_frame_whole, |
| 1276 |
and_media_data->enc_buf_info.size); |
| 1277 |
|
| 1278 |
AMediaCodec_releaseOutputBuffer(and_media_data->enc, |
| 1279 |
buf_info.index, |
| 1280 |
0); |
| 1281 |
|
| 1282 |
return PJ_SUCCESS; |
| 1283 |
} |
| 1284 |
|
| 1285 |
return and_media_codec_encode_more(codec, out_size, output, has_more); |
| 1286 |
|
| 1287 |
on_return: |
| 1288 |
output->size = 0; |
| 1289 |
output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 1290 |
*has_more = PJ_FALSE; |
| 1291 |
return PJ_SUCCESS; |
| 1292 |
} |
| 1293 |
|
| 1294 |
static pj_status_t and_media_codec_encode_more(pjmedia_vid_codec *codec, |
| 1295 |
unsigned out_size, |
| 1296 |
pjmedia_frame *output, |
| 1297 |
pj_bool_t *has_more) |
| 1298 |
{ |
| 1299 |
struct and_media_codec_data *and_media_data; |
| 1300 |
pj_status_t status = PJ_SUCCESS; |
| 1301 |
|
| 1302 |
PJ_ASSERT_RETURN(codec && out_size && output && has_more, PJ_EINVAL); |
| 1303 |
|
| 1304 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1305 |
|
| 1306 |
status = and_media_codec[and_media_data->codec_idx].encode_more( |
| 1307 |
and_media_data, |
| 1308 |
out_size, output, |
| 1309 |
has_more); |
| 1310 |
if (!(*has_more)) { |
| 1311 |
AMediaCodec_releaseOutputBuffer(and_media_data->enc, |
| 1312 |
and_media_data->enc_output_buf_idx, |
| 1313 |
0); |
| 1314 |
} |
| 1315 |
|
| 1316 |
return status; |
| 1317 |
} |
| 1318 |
|
| 1319 |
static int write_yuv(pj_uint8_t *buf, |
| 1320 |
pj_int32_t buf_len, // from "and_med_buf_info.size", it is "pj_int32_t" |
| 1321 |
pj_uint8_t *input, |
| 1322 |
pj_size_t input_len, |
| 1323 |
unsigned stride_len, |
| 1324 |
unsigned input_width, |
| 1325 |
unsigned input_height) |
| 1326 |
{ |
| 1327 |
pj_uint8_t *dst = buf; |
| 1328 |
pj_uint8_t *input_ptr = input; |
| 1329 |
pj_size_t req_buf_size = input_width * input_height * 3 / 2; |
| 1330 |
pj_size_t req_input_size = stride_len * input_height * 3 / 2; |
| 1331 |
unsigned half_stride = stride_len / 2; |
| 1332 |
unsigned half_width = input_width / 2; |
| 1333 |
unsigned half_height = input_height / 2; |
| 1334 |
unsigned i; |
| 1335 |
|
| 1336 |
if (buf_len < req_buf_size || input_len < req_input_size) |
| 1337 |
return -1; |
| 1338 |
|
| 1339 |
for (i = 0; i < input_height; i++) { |
| 1340 |
pj_memcpy(dst, input_ptr, input_width); |
| 1341 |
input_ptr += stride_len; |
| 1342 |
dst += input_width; |
| 1343 |
} |
| 1344 |
|
| 1345 |
for (i = 0; i < half_height; i++) { |
| 1346 |
pj_memcpy(dst, input_ptr, half_width); |
| 1347 |
input_ptr += half_stride; |
| 1348 |
dst += half_width; |
| 1349 |
} |
| 1350 |
|
| 1351 |
for (i = 0; i < half_height; i++) { |
| 1352 |
pj_memcpy(dst, input_ptr, half_width); |
| 1353 |
input_ptr += half_stride; |
| 1354 |
dst += half_width; |
| 1355 |
} |
| 1356 |
|
| 1357 |
return dst - buf; |
| 1358 |
} |
| 1359 |
|
| 1360 |
static pj_bool_t and_media_get_input_buffer( |
| 1361 |
struct and_media_codec_data *and_media_data) |
| 1362 |
{ |
| 1363 |
and_med_buf_info buf_info; |
| 1364 |
pj_atomic_queue_t *queue = and_media_data->dec_avail_input_buf; |
| 1365 |
|
| 1366 |
pj_bzero(&buf_info, sizeof(buf_info)); |
| 1367 |
if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS || |
| 1368 |
buf_info.index < 0) |
| 1369 |
{ |
| 1370 |
PJ_LOG(4,(THIS_FILE, "Decoder failed to get input Buffer [%d]", |
| 1371 |
buf_info.index)); |
| 1372 |
return PJ_FALSE; |
| 1373 |
} |
| 1374 |
|
| 1375 |
and_media_data->dec_input_buf_len = 0; |
| 1376 |
and_media_data->dec_input_buf_idx = buf_info.index; |
| 1377 |
and_media_data->dec_input_buf = AMediaCodec_getInputBuffer( |
| 1378 |
and_media_data->dec, |
| 1379 |
buf_info.index, |
| 1380 |
&and_media_data->dec_input_buf_max_size); |
| 1381 |
return PJ_TRUE; |
| 1382 |
} |
| 1383 |
|
| 1384 |
static pj_status_t and_media_decode(pjmedia_vid_codec *codec, |
| 1385 |
struct and_media_codec_data *and_media_data, |
| 1386 |
pj_uint8_t *input_buf, unsigned buf_size, |
| 1387 |
int buf_flag, pj_timestamp *input_ts, |
| 1388 |
pj_bool_t write_output, pjmedia_frame *output) |
| 1389 |
{ |
| 1390 |
pj_status_t status = PJ_SUCCESS; |
| 1391 |
pj_size_t output_size; |
| 1392 |
int len = 0; |
| 1393 |
media_status_t am_status; |
| 1394 |
and_med_buf_info buf_info; |
| 1395 |
pj_uint8_t *output_buf; |
| 1396 |
pj_atomic_queue_t *queue; |
| 1397 |
|
| 1398 |
pj_bzero(&buf_info, sizeof(buf_info)); |
| 1399 |
if (and_media_data->format_changed) { |
| 1400 |
unsigned new_width = and_media_data->new_size.w; |
| 1401 |
unsigned new_height = and_media_data->new_size.h; |
| 1402 |
|
| 1403 |
and_media_data->dec_stride_len = and_media_data->new_stride; |
| 1404 |
if (new_width != and_media_data->prm->dec_fmt.det.vid.size.w || |
| 1405 |
new_height != and_media_data->prm->dec_fmt.det.vid.size.h) |
| 1406 |
{ |
| 1407 |
pjmedia_event event; |
| 1408 |
|
| 1409 |
and_media_data->prm->dec_fmt.det.vid.size.w = new_width; |
| 1410 |
and_media_data->prm->dec_fmt.det.vid.size.h = new_height; |
| 1411 |
|
| 1412 |
PJ_LOG(4,(THIS_FILE, "Frame size changed to %dx%d", |
| 1413 |
and_media_data->prm->dec_fmt.det.vid.size.w, |
| 1414 |
and_media_data->prm->dec_fmt.det.vid.size.h)); |
| 1415 |
|
| 1416 |
/* Broadcast format changed event */ |
| 1417 |
pjmedia_event_init(&event, PJMEDIA_EVENT_FMT_CHANGED, |
| 1418 |
NULL, codec); |
| 1419 |
event.data.fmt_changed.dir = PJMEDIA_DIR_DECODING; |
| 1420 |
pjmedia_format_copy(&event.data.fmt_changed.new_fmt, |
| 1421 |
&and_media_data->prm->dec_fmt); |
| 1422 |
pjmedia_event_publish(NULL, codec, &event, |
| 1423 |
PJMEDIA_EVENT_PUBLISH_DEFAULT); |
| 1424 |
} |
| 1425 |
|
| 1426 |
and_media_data->format_changed = PJ_FALSE; |
| 1427 |
} |
| 1428 |
|
| 1429 |
if ((and_media_data->dec_input_buf_max_size > 0) && |
| 1430 |
(and_media_data->dec_input_buf_len + buf_size > |
| 1431 |
and_media_data->dec_input_buf_max_size)) |
| 1432 |
{ |
| 1433 |
am_status = AMediaCodec_queueInputBuffer(and_media_data->dec, |
| 1434 |
and_media_data->dec_input_buf_idx, |
| 1435 |
0, |
| 1436 |
and_media_data->dec_input_buf_len, |
| 1437 |
input_ts->u32.lo, |
| 1438 |
buf_flag); |
| 1439 |
if (am_status != AMEDIA_OK) { |
| 1440 |
PJ_LOG(4,(THIS_FILE, "Decoder queueInputBuffer idx[%ld] return %d", |
| 1441 |
and_media_data->dec_input_buf_idx, am_status)); |
| 1442 |
return status; |
| 1443 |
} |
| 1444 |
and_media_data->dec_input_buf = NULL; |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (and_media_data->dec_input_buf == NULL) { |
| 1448 |
and_media_get_input_buffer(and_media_data); |
| 1449 |
|
| 1450 |
if (and_media_data->dec_input_buf == NULL) { |
| 1451 |
PJ_LOG(4,(THIS_FILE, "Decoder failed getting input buffer")); |
| 1452 |
return status; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
pj_memcpy(and_media_data->dec_input_buf + and_media_data->dec_input_buf_len, |
| 1456 |
input_buf, buf_size); |
| 1457 |
|
| 1458 |
and_media_data->dec_input_buf_len += buf_size; |
| 1459 |
|
| 1460 |
if (!write_output) |
| 1461 |
return status; |
| 1462 |
|
| 1463 |
am_status = AMediaCodec_queueInputBuffer(and_media_data->dec, |
| 1464 |
and_media_data->dec_input_buf_idx, |
| 1465 |
0, |
| 1466 |
and_media_data->dec_input_buf_len, |
| 1467 |
input_ts->u32.lo, |
| 1468 |
buf_flag); |
| 1469 |
if (am_status != AMEDIA_OK) { |
| 1470 |
PJ_LOG(4,(THIS_FILE, "Decoder queueInputBuffer failed return %d", |
| 1471 |
am_status)); |
| 1472 |
and_media_data->dec_input_buf = NULL; |
| 1473 |
return status; |
| 1474 |
} |
| 1475 |
and_media_data->dec_input_buf_len += buf_size; |
| 1476 |
|
| 1477 |
pj_bzero(&buf_info, sizeof(buf_info)); |
| 1478 |
queue = and_media_data->dec_avail_output_buf; |
| 1479 |
if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS || |
| 1480 |
buf_info.index < 0) |
| 1481 |
{ |
| 1482 |
PJ_LOG(4,(THIS_FILE, "Decoder failed to get output Buffer[%d]", |
| 1483 |
buf_info.index)); |
| 1484 |
return status; |
| 1485 |
} |
| 1486 |
|
| 1487 |
output_buf = AMediaCodec_getOutputBuffer(and_media_data->dec, |
| 1488 |
buf_info.index, |
| 1489 |
&output_size); |
| 1490 |
if (output_buf == NULL) { |
| 1491 |
am_status = AMediaCodec_releaseOutputBuffer(and_media_data->dec, |
| 1492 |
buf_info.index, 0); |
| 1493 |
PJ_LOG(4,(THIS_FILE, "Decoder getOutputBuffer failed")); |
| 1494 |
return status; |
| 1495 |
} |
| 1496 |
|
| 1497 |
len = write_yuv((pj_uint8_t *)output->buf, |
| 1498 |
output->size, |
| 1499 |
output_buf, // android media output buffer, here it is used as input for our "output" buffer |
| 1500 |
output_size, // size of android media output buffer |
| 1501 |
and_media_data->dec_stride_len, |
| 1502 |
and_media_data->prm->dec_fmt.det.vid.size.w, |
| 1503 |
and_media_data->prm->dec_fmt.det.vid.size.h); |
| 1504 |
|
| 1505 |
am_status = AMediaCodec_releaseOutputBuffer(and_media_data->dec, |
| 1506 |
buf_info.index, 0); |
| 1507 |
|
| 1508 |
if (len > 0) { |
| 1509 |
if (!and_media_data->dec_has_output_frame) { |
| 1510 |
output->type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 1511 |
output->size = len; |
| 1512 |
output->timestamp = *input_ts; |
| 1513 |
|
| 1514 |
and_media_data->dec_has_output_frame = PJ_TRUE; |
| 1515 |
} |
| 1516 |
} else { |
| 1517 |
status = PJMEDIA_CODEC_EFRMTOOSHORT; |
| 1518 |
} |
| 1519 |
return status; |
| 1520 |
} |
| 1521 |
|
| 1522 |
static pj_status_t and_media_codec_decode(pjmedia_vid_codec *codec, |
| 1523 |
pj_size_t count, |
| 1524 |
pjmedia_frame packets[], |
| 1525 |
unsigned out_size, |
| 1526 |
pjmedia_frame *output) |
| 1527 |
{ |
| 1528 |
struct and_media_codec_data *and_media_data; |
| 1529 |
pj_status_t status = PJ_EINVAL; |
| 1530 |
|
| 1531 |
PJ_ASSERT_RETURN(codec && count && packets && out_size && output, |
| 1532 |
PJ_EINVAL); |
| 1533 |
PJ_ASSERT_RETURN(output->buf, PJ_EINVAL); |
| 1534 |
|
| 1535 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1536 |
and_media_data->dec_has_output_frame = PJ_FALSE; |
| 1537 |
and_media_data->dec_input_buf = NULL; |
| 1538 |
and_media_data->dec_input_buf_len = 0; |
| 1539 |
|
| 1540 |
if (and_media_codec[and_media_data->codec_idx].decode) { |
| 1541 |
status = and_media_codec[and_media_data->codec_idx].decode(codec, count, |
| 1542 |
packets, out_size, |
| 1543 |
output); |
| 1544 |
} |
| 1545 |
if (status != PJ_SUCCESS) { |
| 1546 |
return status; |
| 1547 |
} |
| 1548 |
if (!and_media_data->dec_has_output_frame) { |
| 1549 |
pjmedia_event event; |
| 1550 |
|
| 1551 |
/* Broadcast missing keyframe event */ |
| 1552 |
pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING, |
| 1553 |
&packets[0].timestamp, codec); |
| 1554 |
pjmedia_event_publish(NULL, codec, &event, |
| 1555 |
PJMEDIA_EVENT_PUBLISH_DEFAULT); |
| 1556 |
|
| 1557 |
PJ_LOG(4,(THIS_FILE, "Decoder couldn't produce output frame")); |
| 1558 |
|
| 1559 |
output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 1560 |
output->size = 0; |
| 1561 |
output->timestamp = packets[0].timestamp; |
| 1562 |
} |
| 1563 |
return PJ_SUCCESS; |
| 1564 |
} |
| 1565 |
|
| 1566 |
#if PJMEDIA_HAS_AND_MEDIA_H264 |
| 1567 |
|
| 1568 |
static pj_status_t open_h264(and_media_codec_data *and_media_data) |
| 1569 |
{ |
| 1570 |
pj_status_t status; |
| 1571 |
pjmedia_vid_codec_param *param = and_media_data->prm; |
| 1572 |
pjmedia_h264_packetizer_cfg pktz_cfg; |
| 1573 |
pjmedia_vid_codec_h264_fmtp h264_fmtp; |
| 1574 |
h264_codec_data *h264_data; |
| 1575 |
|
| 1576 |
/* Parse remote fmtp */ |
| 1577 |
pj_bzero(&h264_fmtp, sizeof(h264_fmtp)); |
| 1578 |
status = pjmedia_vid_codec_h264_parse_fmtp(¶m->enc_fmtp, |
| 1579 |
&h264_fmtp); |
| 1580 |
if (status != PJ_SUCCESS) |
| 1581 |
return status; |
| 1582 |
|
| 1583 |
/* Apply SDP fmtp to format in codec param */ |
| 1584 |
if (!param->ignore_fmtp) { |
| 1585 |
status = pjmedia_vid_codec_h264_apply_fmtp(param); |
| 1586 |
if (status != PJ_SUCCESS) |
| 1587 |
return status; |
| 1588 |
} |
| 1589 |
h264_data = PJ_POOL_ZALLOC_T(and_media_data->pool, h264_codec_data); |
| 1590 |
if (!h264_data) |
| 1591 |
return PJ_ENOMEM; |
| 1592 |
|
| 1593 |
pj_bzero(&pktz_cfg, sizeof(pktz_cfg)); |
| 1594 |
pktz_cfg.mtu = param->enc_mtu; |
| 1595 |
pktz_cfg.unpack_nal_start = 4; |
| 1596 |
/* Packetization mode */ |
| 1597 |
if (h264_fmtp.packetization_mode == 0) |
| 1598 |
pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_SINGLE_NAL; |
| 1599 |
else if (h264_fmtp.packetization_mode == 1) |
| 1600 |
pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED; |
| 1601 |
else |
| 1602 |
return PJ_ENOTSUP; |
| 1603 |
|
| 1604 |
/* Android H264 only supports Non Interleaved mode. */ |
| 1605 |
pktz_cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED; |
| 1606 |
status = pjmedia_h264_packetizer_create(and_media_data->pool, &pktz_cfg, |
| 1607 |
&h264_data->pktz); |
| 1608 |
if (status != PJ_SUCCESS) |
| 1609 |
return status; |
| 1610 |
|
| 1611 |
and_media_data->ex_data = h264_data; |
| 1612 |
and_media_data->dec_buf_size = (MAX_RX_WIDTH * MAX_RX_HEIGHT * 3 >> 1) + |
| 1613 |
(MAX_RX_WIDTH); |
| 1614 |
|
| 1615 |
/* If available, use the "sprop-parameter-sets" fmtp from remote SDP |
| 1616 |
* to create the decoder. |
| 1617 |
*/ |
| 1618 |
if (h264_fmtp.sprop_param_sets_len) { |
| 1619 |
const pj_uint8_t start_code[3] = { 0, 0, 1 }; |
| 1620 |
const int code_size = PJ_ARRAY_SIZE(start_code); |
| 1621 |
const pj_uint8_t med_start_code[4] = { 0, 0, 0, 1 }; |
| 1622 |
const int med_code_size = PJ_ARRAY_SIZE(med_start_code); |
| 1623 |
unsigned i, j; |
| 1624 |
|
| 1625 |
for (i = h264_fmtp.sprop_param_sets_len - code_size; |
| 1626 |
i >= code_size; i--) |
| 1627 |
{ |
| 1628 |
for (j = 0; j < code_size; j++) { |
| 1629 |
if (h264_fmtp.sprop_param_sets[i + j] != start_code[j]) { |
| 1630 |
break; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (i >= code_size) { |
| 1636 |
h264_data->dec_sps_len = i + med_code_size - code_size; |
| 1637 |
h264_data->dec_pps_len = h264_fmtp.sprop_param_sets_len + |
| 1638 |
med_code_size - code_size - i; |
| 1639 |
|
| 1640 |
h264_data->dec_sps_buf = (pj_uint8_t *)pj_pool_alloc( |
| 1641 |
and_media_data->pool, h264_data->dec_sps_len); |
| 1642 |
h264_data->dec_pps_buf = (pj_uint8_t *)pj_pool_alloc( |
| 1643 |
and_media_data->pool, h264_data->dec_pps_len); |
| 1644 |
|
| 1645 |
pj_memcpy(h264_data->dec_sps_buf, med_start_code, |
| 1646 |
med_code_size); |
| 1647 |
pj_memcpy(h264_data->dec_sps_buf + med_code_size, |
| 1648 |
&h264_fmtp.sprop_param_sets[code_size], |
| 1649 |
h264_data->dec_sps_len - med_code_size); |
| 1650 |
pj_memcpy(h264_data->dec_pps_buf, med_start_code, |
| 1651 |
med_code_size); |
| 1652 |
pj_memcpy(h264_data->dec_pps_buf + med_code_size, |
| 1653 |
&h264_fmtp.sprop_param_sets[i + code_size], |
| 1654 |
h264_data->dec_pps_len - med_code_size); |
| 1655 |
} |
| 1656 |
} |
| 1657 |
return status; |
| 1658 |
} |
| 1659 |
|
| 1660 |
static pj_status_t process_encode_h264(and_media_codec_data *and_media_data) |
| 1661 |
{ |
| 1662 |
pj_status_t status = PJ_SUCCESS; |
| 1663 |
h264_codec_data *h264_data; |
| 1664 |
|
| 1665 |
h264_data = (h264_codec_data *)and_media_data->ex_data; |
| 1666 |
if (and_media_data->enc_buf_info.flags & AND_MEDIA_FRM_TYPE_CONFIG) { |
| 1667 |
|
| 1668 |
/* |
| 1669 |
* Config data or SPS+PPS. Update the SPS and PPS buffer, |
| 1670 |
* this will be sent later when sending Keyframe. |
| 1671 |
*/ |
| 1672 |
h264_data->enc_sps_pps_len = PJ_MIN(and_media_data->enc_buf_info.size, |
| 1673 |
sizeof(h264_data->enc_sps_pps_buf)); |
| 1674 |
pj_memcpy(h264_data->enc_sps_pps_buf, and_media_data->enc_frame_whole, |
| 1675 |
h264_data->enc_sps_pps_len); |
| 1676 |
|
| 1677 |
AMediaCodec_releaseOutputBuffer(and_media_data->enc, |
| 1678 |
and_media_data->enc_output_buf_idx, |
| 1679 |
0); |
| 1680 |
|
| 1681 |
return PJ_EIGNORED; |
| 1682 |
} |
| 1683 |
if (and_media_data->enc_buf_info.flags & AND_MEDIA_FRM_TYPE_KEYFRAME) { |
| 1684 |
h264_data->enc_sps_pps_ex = PJ_TRUE; |
| 1685 |
and_media_data->enc_frame_size = h264_data->enc_sps_pps_len; |
| 1686 |
} else { |
| 1687 |
h264_data->enc_sps_pps_ex = PJ_FALSE; |
| 1688 |
} |
| 1689 |
|
| 1690 |
return status; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static pj_status_t encode_more_h264(and_media_codec_data *and_media_data, |
| 1694 |
unsigned out_size, |
| 1695 |
pjmedia_frame *output, |
| 1696 |
pj_bool_t *has_more) |
| 1697 |
{ |
| 1698 |
const pj_uint8_t *payload; |
| 1699 |
pj_size_t payload_len; |
| 1700 |
pj_status_t status; |
| 1701 |
pj_uint8_t *data_buf = NULL; |
| 1702 |
h264_codec_data *h264_data; |
| 1703 |
|
| 1704 |
h264_data = (h264_codec_data *)and_media_data->ex_data; |
| 1705 |
if (h264_data->enc_sps_pps_ex) { |
| 1706 |
data_buf = h264_data->enc_sps_pps_buf; |
| 1707 |
} else { |
| 1708 |
data_buf = and_media_data->enc_frame_whole; |
| 1709 |
} |
| 1710 |
/* We have outstanding frame in packetizer */ |
| 1711 |
status = pjmedia_h264_packetize(h264_data->pktz, |
| 1712 |
data_buf, |
| 1713 |
and_media_data->enc_frame_size, |
| 1714 |
&and_media_data->enc_processed, |
| 1715 |
&payload, &payload_len); |
| 1716 |
if (status != PJ_SUCCESS) { |
| 1717 |
/* Reset */ |
| 1718 |
and_media_data->enc_frame_size = and_media_data->enc_processed = 0; |
| 1719 |
*has_more = (and_media_data->enc_processed < |
| 1720 |
and_media_data->enc_frame_size); |
| 1721 |
|
| 1722 |
PJ_PERROR(3,(THIS_FILE, status, "pjmedia_h264_packetize() error")); |
| 1723 |
return status; |
| 1724 |
} |
| 1725 |
|
| 1726 |
PJ_ASSERT_RETURN(payload_len <= out_size, PJMEDIA_CODEC_EFRMTOOSHORT); |
| 1727 |
|
| 1728 |
output->type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 1729 |
pj_memcpy(output->buf, payload, payload_len); |
| 1730 |
output->size = payload_len; |
| 1731 |
|
| 1732 |
if (and_media_data->enc_processed >= and_media_data->enc_frame_size) { |
| 1733 |
h264_codec_data *h264_data = (h264_codec_data *)and_media_data->ex_data; |
| 1734 |
|
| 1735 |
if (h264_data->enc_sps_pps_ex) { |
| 1736 |
*has_more = PJ_TRUE; |
| 1737 |
h264_data->enc_sps_pps_ex = PJ_FALSE; |
| 1738 |
and_media_data->enc_processed = 0; |
| 1739 |
and_media_data->enc_frame_size = and_media_data->enc_buf_info.size; |
| 1740 |
} else { |
| 1741 |
*has_more = PJ_FALSE; |
| 1742 |
} |
| 1743 |
} else { |
| 1744 |
*has_more = PJ_TRUE; |
| 1745 |
} |
| 1746 |
|
| 1747 |
return PJ_SUCCESS; |
| 1748 |
} |
| 1749 |
|
| 1750 |
static pj_status_t decode_h264(pjmedia_vid_codec *codec, |
| 1751 |
pj_size_t count, |
| 1752 |
pjmedia_frame packets[], |
| 1753 |
unsigned out_size, |
| 1754 |
pjmedia_frame *output) |
| 1755 |
{ |
| 1756 |
struct and_media_codec_data *and_media_data; |
| 1757 |
const pj_uint8_t start_code[] = { 0, 0, 0, 1 }; |
| 1758 |
const int code_size = PJ_ARRAY_SIZE(start_code); |
| 1759 |
unsigned buf_pos, whole_len = 0; |
| 1760 |
unsigned i, frm_cnt; |
| 1761 |
pj_status_t status; |
| 1762 |
|
| 1763 |
PJ_ASSERT_RETURN(codec && count && packets && out_size && output, |
| 1764 |
PJ_EINVAL); |
| 1765 |
PJ_ASSERT_RETURN(output->buf, PJ_EINVAL); |
| 1766 |
|
| 1767 |
and_media_data = (and_media_codec_data*) codec->codec_data; |
| 1768 |
|
| 1769 |
/* |
| 1770 |
* Step 1: unpacketize the packets/frames |
| 1771 |
*/ |
| 1772 |
whole_len = 0; |
| 1773 |
if (and_media_data->whole) { |
| 1774 |
for (i=0; i<count; ++i) { |
| 1775 |
if (whole_len + packets[i].size > and_media_data->dec_buf_size) { |
| 1776 |
PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [1]")); |
| 1777 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 1778 |
} |
| 1779 |
|
| 1780 |
pj_memcpy( and_media_data->dec_buf + whole_len, |
| 1781 |
(pj_uint8_t*)packets[i].buf, |
| 1782 |
packets[i].size); |
| 1783 |
whole_len += packets[i].size; |
| 1784 |
} |
| 1785 |
|
| 1786 |
} else { |
| 1787 |
h264_codec_data *h264_data = (h264_codec_data *)and_media_data->ex_data; |
| 1788 |
|
| 1789 |
for (i=0; i<count; ++i) { |
| 1790 |
|
| 1791 |
if (whole_len + packets[i].size + code_size > |
| 1792 |
and_media_data->dec_buf_size) |
| 1793 |
{ |
| 1794 |
PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [2]")); |
| 1795 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 1796 |
} |
| 1797 |
|
| 1798 |
status = pjmedia_h264_unpacketize( h264_data->pktz, |
| 1799 |
(pj_uint8_t*)packets[i].buf, |
| 1800 |
packets[i].size, |
| 1801 |
and_media_data->dec_buf, |
| 1802 |
and_media_data->dec_buf_size, |
| 1803 |
&whole_len); |
| 1804 |
if (status != PJ_SUCCESS) { |
| 1805 |
PJ_PERROR(4,(THIS_FILE, status, "Unpacketize error")); |
| 1806 |
continue; |
| 1807 |
} |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
if (whole_len + code_size > and_media_data->dec_buf_size || |
| 1812 |
whole_len <= code_size + 1) |
| 1813 |
{ |
| 1814 |
PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow or unpacketize error " |
| 1815 |
"size: %d, buffer: %d", whole_len, |
| 1816 |
and_media_data->dec_buf_size)); |
| 1817 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 1818 |
} |
| 1819 |
|
| 1820 |
/* Dummy NAL sentinel */ |
| 1821 |
pj_memcpy(and_media_data->dec_buf + whole_len, start_code, code_size); |
| 1822 |
|
| 1823 |
/* |
| 1824 |
* Step 2: parse the individual NAL and give to decoder |
| 1825 |
*/ |
| 1826 |
buf_pos = 0; |
| 1827 |
for ( frm_cnt=0; ; ++frm_cnt) { |
| 1828 |
pj_uint32_t frm_size; |
| 1829 |
pj_bool_t write_output = PJ_FALSE; |
| 1830 |
unsigned char *start; |
| 1831 |
|
| 1832 |
for (i = code_size - 1; buf_pos + i < whole_len; i++) { |
| 1833 |
if (and_media_data->dec_buf[buf_pos + i] == 0 && |
| 1834 |
and_media_data->dec_buf[buf_pos + i + 1] == 0 && |
| 1835 |
and_media_data->dec_buf[buf_pos + i + 2] == 0 && |
| 1836 |
and_media_data->dec_buf[buf_pos + i + 3] == 1) |
| 1837 |
{ |
| 1838 |
break; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
frm_size = i; |
| 1843 |
start = and_media_data->dec_buf + buf_pos; |
| 1844 |
write_output = (buf_pos + frm_size >= whole_len); |
| 1845 |
|
| 1846 |
status = and_media_decode(codec, and_media_data, start, frm_size, 0, |
| 1847 |
&packets[0].timestamp, write_output, output); |
| 1848 |
if (status != PJ_SUCCESS) |
| 1849 |
return status; |
| 1850 |
|
| 1851 |
if (write_output) |
| 1852 |
break; |
| 1853 |
|
| 1854 |
buf_pos += frm_size; |
| 1855 |
} |
| 1856 |
|
| 1857 |
PJ_UNUSED_ARG(frm_cnt); |
| 1858 |
|
| 1859 |
return PJ_SUCCESS; |
| 1860 |
} |
| 1861 |
|
| 1862 |
#endif |
| 1863 |
|
| 1864 |
#if PJMEDIA_HAS_AND_MEDIA_VP8 || PJMEDIA_HAS_AND_MEDIA_VP9 |
| 1865 |
|
| 1866 |
static pj_status_t open_vpx(and_media_codec_data *and_media_data) |
| 1867 |
{ |
| 1868 |
vpx_codec_data *vpx_data; |
| 1869 |
pjmedia_vid_codec_vpx_fmtp vpx_fmtp; |
| 1870 |
pjmedia_vpx_packetizer_cfg pktz_cfg; |
| 1871 |
pj_status_t status = PJ_SUCCESS; |
| 1872 |
unsigned max_res = MAX_RX_WIDTH; |
| 1873 |
|
| 1874 |
if (!and_media_data->prm->ignore_fmtp) { |
| 1875 |
status = pjmedia_vid_codec_vpx_apply_fmtp(and_media_data->prm); |
| 1876 |
if (status != PJ_SUCCESS) |
| 1877 |
return status; |
| 1878 |
} |
| 1879 |
|
| 1880 |
vpx_data = PJ_POOL_ZALLOC_T(and_media_data->pool, vpx_codec_data); |
| 1881 |
if (!vpx_data) |
| 1882 |
return PJ_ENOMEM; |
| 1883 |
|
| 1884 |
/* Parse local fmtp */ |
| 1885 |
status = pjmedia_vid_codec_vpx_parse_fmtp(&and_media_data->prm->dec_fmtp, |
| 1886 |
&vpx_fmtp); |
| 1887 |
if (status != PJ_SUCCESS) |
| 1888 |
return status; |
| 1889 |
|
| 1890 |
if (vpx_fmtp.max_fs > 0) { |
| 1891 |
max_res = ((int)pj_isqrt(vpx_fmtp.max_fs * 8)) * 16; |
| 1892 |
} |
| 1893 |
and_media_data->dec_buf_size = (max_res * max_res * 3 >> 1) + (max_res); |
| 1894 |
|
| 1895 |
pj_bzero(&pktz_cfg, sizeof(pktz_cfg)); |
| 1896 |
pktz_cfg.mtu = and_media_data->prm->enc_mtu; |
| 1897 |
pktz_cfg.fmt_id = and_media_data->prm->enc_fmt.id; |
| 1898 |
|
| 1899 |
status = pjmedia_vpx_packetizer_create(and_media_data->pool, &pktz_cfg, |
| 1900 |
&vpx_data->pktz); |
| 1901 |
if (status != PJ_SUCCESS) |
| 1902 |
return status; |
| 1903 |
|
| 1904 |
and_media_data->ex_data = vpx_data; |
| 1905 |
|
| 1906 |
return status; |
| 1907 |
} |
| 1908 |
|
| 1909 |
static pj_status_t encode_more_vpx(and_media_codec_data *and_media_data, |
| 1910 |
unsigned out_size, |
| 1911 |
pjmedia_frame *output, |
| 1912 |
pj_bool_t *has_more) |
| 1913 |
{ |
| 1914 |
pj_status_t status = PJ_SUCCESS; |
| 1915 |
struct vpx_codec_data *vpx_data = (vpx_codec_data *)and_media_data->ex_data; |
| 1916 |
|
| 1917 |
PJ_ASSERT_RETURN(and_media_data && out_size && output && has_more, |
| 1918 |
PJ_EINVAL); |
| 1919 |
|
| 1920 |
if ((and_media_data->prm->enc_fmt.id != PJMEDIA_FORMAT_VP8) && |
| 1921 |
(and_media_data->prm->enc_fmt.id != PJMEDIA_FORMAT_VP9)) |
| 1922 |
{ |
| 1923 |
*has_more = PJ_FALSE; |
| 1924 |
output->size = 0; |
| 1925 |
output->type = PJMEDIA_FRAME_TYPE_NONE; |
| 1926 |
|
| 1927 |
return PJ_SUCCESS; |
| 1928 |
} |
| 1929 |
|
| 1930 |
if (and_media_data->enc_processed < and_media_data->enc_frame_size) { |
| 1931 |
unsigned payload_desc_size = 1; |
| 1932 |
pj_size_t payload_len = out_size; |
| 1933 |
pj_uint8_t *p = (pj_uint8_t *)output->buf; |
| 1934 |
pj_bool_t is_keyframe = and_media_data->enc_buf_info.flags & |
| 1935 |
AND_MEDIA_FRM_TYPE_KEYFRAME; |
| 1936 |
|
| 1937 |
status = pjmedia_vpx_packetize(vpx_data->pktz, |
| 1938 |
and_media_data->enc_frame_size, |
| 1939 |
&and_media_data->enc_processed, |
| 1940 |
is_keyframe, |
| 1941 |
&p, |
| 1942 |
&payload_len); |
| 1943 |
if (status != PJ_SUCCESS) { |
| 1944 |
return status; |
| 1945 |
} |
| 1946 |
pj_memcpy(p + payload_desc_size, |
| 1947 |
(and_media_data->enc_frame_whole + and_media_data->enc_processed), |
| 1948 |
payload_len); |
| 1949 |
output->size = payload_len + payload_desc_size; |
| 1950 |
if (is_keyframe) { |
| 1951 |
output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME; |
| 1952 |
} |
| 1953 |
and_media_data->enc_processed += payload_len; |
| 1954 |
*has_more = (and_media_data->enc_processed < |
| 1955 |
and_media_data->enc_frame_size); |
| 1956 |
} |
| 1957 |
|
| 1958 |
return status; |
| 1959 |
} |
| 1960 |
|
| 1961 |
static pj_status_t decode_vpx(pjmedia_vid_codec *codec, |
| 1962 |
pj_size_t count, |
| 1963 |
pjmedia_frame packets[], |
| 1964 |
unsigned out_size, |
| 1965 |
pjmedia_frame *output) |
| 1966 |
{ |
| 1967 |
unsigned i, whole_len = 0; |
| 1968 |
pj_status_t status; |
| 1969 |
and_media_codec_data *and_media_data = |
| 1970 |
(and_media_codec_data*) codec->codec_data; |
| 1971 |
struct vpx_codec_data *vpx_data = (vpx_codec_data *)and_media_data->ex_data; |
| 1972 |
|
| 1973 |
PJ_ASSERT_RETURN(codec && count && packets && out_size && output, |
| 1974 |
PJ_EINVAL); |
| 1975 |
PJ_ASSERT_RETURN(output->buf, PJ_EINVAL); |
| 1976 |
|
| 1977 |
whole_len = 0; |
| 1978 |
if (and_media_data->whole) { |
| 1979 |
for (i = 0; i < count; ++i) { |
| 1980 |
if (whole_len + packets[i].size > and_media_data->dec_buf_size) { |
| 1981 |
PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [1]")); |
| 1982 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 1983 |
} |
| 1984 |
|
| 1985 |
pj_memcpy( and_media_data->dec_buf + whole_len, |
| 1986 |
(pj_uint8_t*)packets[i].buf, |
| 1987 |
packets[i].size); |
| 1988 |
whole_len += packets[i].size; |
| 1989 |
} |
| 1990 |
status = and_media_decode(codec, and_media_data, |
| 1991 |
and_media_data->dec_buf, whole_len, 0, |
| 1992 |
&packets[0].timestamp, PJ_TRUE, output); |
| 1993 |
|
| 1994 |
if (status != PJ_SUCCESS) |
| 1995 |
return status; |
| 1996 |
|
| 1997 |
} else { |
| 1998 |
for (i = 0; i < count; ++i) { |
| 1999 |
unsigned desc_len; |
| 2000 |
unsigned packet_size = packets[i].size; |
| 2001 |
pj_status_t status; |
| 2002 |
pj_bool_t write_output; |
| 2003 |
|
| 2004 |
status = pjmedia_vpx_unpacketize(vpx_data->pktz, |
| 2005 |
(pj_uint8_t *)packets[i].buf, |
| 2006 |
packet_size, |
| 2007 |
&desc_len); |
| 2008 |
if (status != PJ_SUCCESS) { |
| 2009 |
PJ_LOG(4,(THIS_FILE, "Unpacketize error packet size[%d]", |
| 2010 |
packet_size)); |
| 2011 |
return status; |
| 2012 |
} |
| 2013 |
|
| 2014 |
packet_size -= desc_len; |
| 2015 |
if (whole_len + packet_size > and_media_data->dec_buf_size) { |
| 2016 |
PJ_LOG(4,(THIS_FILE, "Decoding buffer overflow [2]")); |
| 2017 |
return PJMEDIA_CODEC_EFRMTOOSHORT; |
| 2018 |
} |
| 2019 |
|
| 2020 |
write_output = (i == count - 1); |
| 2021 |
|
| 2022 |
status = and_media_decode(codec, and_media_data, |
| 2023 |
(pj_uint8_t *)packets[i].buf + desc_len, |
| 2024 |
packet_size, 0, &packets[0].timestamp, |
| 2025 |
write_output, output); |
| 2026 |
if (status != PJ_SUCCESS) |
| 2027 |
return status; |
| 2028 |
|
| 2029 |
whole_len += packet_size; |
| 2030 |
} |
| 2031 |
} |
| 2032 |
return PJ_SUCCESS; |
| 2033 |
} |
| 2034 |
|
| 2035 |
#endif |
| 2036 |
|
| 2037 |
#endif /* PJMEDIA_HAS_ANDROID_MEDIACODEC */ |