2014-11-11 03:38:56 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2015-02-26 00:58:04 +00:00
|
|
|
* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
|
2014-11-11 03:38:56 +00:00
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Seven Du <dujinfang@gmail.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
|
|
|
* Seven Du <dujinfang@gmail.com>
|
|
|
|
* Sam Russell <sam.h.russell@gmail.com>
|
|
|
|
*
|
|
|
|
* mod_vpx.c -- VP8/9 Video Codec, with transcoding
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <switch.h>
|
2016-02-29 17:39:51 +00:00
|
|
|
#ifdef SWITCH_HAVE_YUV
|
|
|
|
#ifdef SWITCH_HAVE_VPX
|
2014-11-11 03:38:56 +00:00
|
|
|
#include <vpx/vpx_encoder.h>
|
|
|
|
#include <vpx/vpx_decoder.h>
|
|
|
|
#include <vpx/vp8cx.h>
|
|
|
|
#include <vpx/vp8dx.h>
|
|
|
|
#include <vpx/vp8.h>
|
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
// #define DEBUG_VP9
|
|
|
|
|
2014-11-20 14:40:27 +00:00
|
|
|
#define SLICE_SIZE SWITCH_DEFAULT_VIDEO_SIZE
|
2015-05-06 19:02:44 +00:00
|
|
|
#define KEY_FRAME_MIN_FREQ 250000
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
/* http://tools.ietf.org/html/draft-ietf-payload-vp8-10
|
|
|
|
|
|
|
|
The first octets after the RTP header are the VP8 payload descriptor, with the following structure.
|
2016-03-28 06:09:07 +00:00
|
|
|
#endif
|
2015-03-26 20:00:55 +00:00
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
|X|R|N|S|R| PID | (REQUIRED)
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
X: |I|L|T|K| RSV | (OPTIONAL)
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
I: |M| PictureID | (OPTIONAL)
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
L: | TL0PICIDX | (OPTIONAL)
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
T/K:|TID|Y| KEYIDX | (OPTIONAL)
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
|
|
|
|
|
|
|
|
VP8 Payload Header
|
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
|Size0|H| VER |P|
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
| Size1 |
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
| Size2 |
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
| Bytes 4..N of |
|
|
|
|
| VP8 payload |
|
|
|
|
: :
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
| OPTIONAL RTP |
|
|
|
|
| padding |
|
|
|
|
: :
|
|
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(push, r1, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned extended:1;
|
|
|
|
unsigned reserved1:1;
|
|
|
|
unsigned non_referenced:1;
|
|
|
|
unsigned start:1;
|
|
|
|
unsigned reserved2:1;
|
|
|
|
unsigned pid:3;
|
|
|
|
} vp8_payload_descriptor_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned have_pid:1;
|
2016-02-04 00:36:17 +00:00
|
|
|
unsigned have_p_layer:1;
|
2015-03-26 20:00:55 +00:00
|
|
|
unsigned have_layer_ind:1;
|
2016-02-04 00:36:17 +00:00
|
|
|
unsigned is_flexible:1;
|
2015-03-26 20:00:55 +00:00
|
|
|
unsigned start:1;
|
|
|
|
unsigned end:1;
|
|
|
|
unsigned have_ss:1;
|
|
|
|
unsigned zero:1;
|
|
|
|
} vp9_payload_descriptor_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2016-02-04 00:36:17 +00:00
|
|
|
unsigned n_s:3;
|
|
|
|
unsigned y:1;
|
|
|
|
unsigned g:1;
|
2016-03-28 06:09:07 +00:00
|
|
|
unsigned zero:3;
|
2016-02-04 00:36:17 +00:00
|
|
|
} vp9_ss_t;
|
2015-03-26 20:00:55 +00:00
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
typedef struct {
|
|
|
|
unsigned t:3;
|
|
|
|
unsigned u:1;
|
|
|
|
unsigned r:2;
|
|
|
|
unsigned zero:2;
|
|
|
|
} vp9_n_g_t;
|
2015-03-26 20:00:55 +00:00
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
typedef struct {
|
|
|
|
unsigned temporal_id:3;
|
|
|
|
unsigned temporal_up_switch:1;
|
|
|
|
unsigned spatial_id:3;
|
|
|
|
unsigned inter_layer_predicted:1;
|
|
|
|
} vp9_p_layer_t;
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
#else /* ELSE LITTLE */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned pid:3;
|
|
|
|
unsigned reserved2:1;
|
|
|
|
unsigned start:1;
|
|
|
|
unsigned non_referenced:1;
|
|
|
|
unsigned reserved1:1;
|
|
|
|
unsigned extended:1;
|
|
|
|
} vp8_payload_descriptor_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned zero:1;
|
|
|
|
unsigned have_ss:1;
|
|
|
|
unsigned end:1;
|
|
|
|
unsigned start:1;
|
2016-02-04 00:36:17 +00:00
|
|
|
unsigned is_flexible:1;
|
2015-03-26 20:00:55 +00:00
|
|
|
unsigned have_layer_ind:1;
|
2016-02-04 00:36:17 +00:00
|
|
|
unsigned have_p_layer:1;
|
2015-03-26 20:00:55 +00:00
|
|
|
unsigned have_pid:1;
|
|
|
|
} vp9_payload_descriptor_t;
|
2016-02-04 00:36:17 +00:00
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
typedef struct {
|
2016-03-28 06:09:07 +00:00
|
|
|
unsigned zero:3;
|
2016-02-04 00:36:17 +00:00
|
|
|
unsigned g:1;
|
|
|
|
unsigned y:1;
|
|
|
|
unsigned n_s:3;
|
|
|
|
} vp9_ss_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned zero:2;
|
|
|
|
unsigned r:2;
|
|
|
|
unsigned u:1;
|
|
|
|
unsigned t:3;
|
|
|
|
} vp9_n_g_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2016-03-28 06:09:07 +00:00
|
|
|
unsigned inter_layer_predicted:1;
|
|
|
|
unsigned spatial_id:3;
|
|
|
|
unsigned temporal_up_switch:1;
|
|
|
|
unsigned temporal_id:3;
|
|
|
|
} vp9_p_layer_t;
|
2015-03-26 20:00:55 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
vp8_payload_descriptor_t vp8;
|
|
|
|
vp9_payload_descriptor_t vp9;
|
|
|
|
} vpx_payload_descriptor_t;
|
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
#define kMaxVp9NumberOfSpatialLayers 16
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
switch_bool_t has_received_sli;
|
|
|
|
uint8_t picture_id_sli;
|
|
|
|
switch_bool_t has_received_rpsi;
|
|
|
|
uint64_t picture_id_rpsi;
|
|
|
|
int16_t picture_id; // Negative value to skip pictureId.
|
|
|
|
|
|
|
|
switch_bool_t inter_pic_predicted; // This layer frame is dependent on previously
|
|
|
|
// coded frame(s).
|
|
|
|
switch_bool_t flexible_mode;
|
|
|
|
switch_bool_t ss_data_available;
|
|
|
|
|
|
|
|
int tl0_pic_idx; // Negative value to skip tl0PicIdx.
|
|
|
|
uint8_t temporal_idx;
|
|
|
|
uint8_t spatial_idx;
|
|
|
|
switch_bool_t temporal_up_switch;
|
|
|
|
switch_bool_t inter_layer_predicted; // Frame is dependent on directly lower spatial
|
|
|
|
// layer frame.
|
|
|
|
uint8_t gof_idx;
|
|
|
|
|
|
|
|
// SS data.
|
|
|
|
size_t num_spatial_layers;
|
|
|
|
switch_bool_t spatial_layer_resolution_present;
|
|
|
|
uint16_t width[kMaxVp9NumberOfSpatialLayers];
|
|
|
|
uint16_t height[kMaxVp9NumberOfSpatialLayers];
|
|
|
|
// GofInfoVP9 gof;
|
|
|
|
} vp9_info_t;
|
|
|
|
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(pop, r1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-05-23 03:08:27 +00:00
|
|
|
#define __IS_VP8_KEY_FRAME(byte) !(((byte) & 0x01))
|
2015-03-26 20:00:55 +00:00
|
|
|
static inline int IS_VP8_KEY_FRAME(uint8_t *data)
|
|
|
|
{
|
|
|
|
uint8_t S;
|
|
|
|
uint8_t DES;
|
|
|
|
uint8_t PID;
|
|
|
|
|
|
|
|
DES = *data;
|
|
|
|
data++;
|
|
|
|
S = DES & 0x10;
|
|
|
|
PID = DES & 0x07;
|
|
|
|
|
|
|
|
if (DES & 0x80) { // X
|
|
|
|
uint8_t X = *data;
|
|
|
|
data++;
|
|
|
|
if (X & 0x80) { // I
|
|
|
|
uint8_t M = (*data) & 0x80;
|
|
|
|
data++;
|
|
|
|
if (M) data++;
|
|
|
|
}
|
|
|
|
if (X & 0x40) data++; // L
|
|
|
|
if (X & 0x30) data++; // T/K
|
|
|
|
}
|
|
|
|
|
2015-03-29 12:32:12 +00:00
|
|
|
if (S && (PID == 0)) {
|
2015-03-26 20:00:55 +00:00
|
|
|
return __IS_VP8_KEY_FRAME(*data);
|
|
|
|
} else {
|
2016-01-25 00:11:11 +00:00
|
|
|
// if (PID > 0) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "PID: %d\n", PID);
|
2015-03-26 20:00:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
#define IS_VP9_KEY_FRAME(byte) ((((byte) & 0x40) == 0) && ((byte) & 0x0A))
|
|
|
|
#define IS_VP9_START_PKT(byte) ((byte) & 0x08)
|
2015-03-12 02:26:14 +00:00
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_vpx_load);
|
2016-02-24 23:43:21 +00:00
|
|
|
SWITCH_MODULE_DEFINITION(CORE_VPX_MODULE, mod_vpx_load, NULL, NULL);
|
2014-11-11 03:38:56 +00:00
|
|
|
|
|
|
|
struct vpx_context {
|
|
|
|
switch_codec_t *codec;
|
2015-03-11 23:33:20 +00:00
|
|
|
int is_vp9;
|
2016-02-04 00:36:17 +00:00
|
|
|
vp9_info_t vp9;
|
2015-03-12 16:17:35 +00:00
|
|
|
int lossless;
|
2015-03-11 23:33:20 +00:00
|
|
|
vpx_codec_iface_t *encoder_interface;
|
|
|
|
vpx_codec_iface_t *decoder_interface;
|
2014-11-11 03:38:56 +00:00
|
|
|
unsigned int flags;
|
2014-11-15 01:01:56 +00:00
|
|
|
switch_codec_settings_t codec_settings;
|
|
|
|
unsigned int bandwidth;
|
2014-11-11 03:38:56 +00:00
|
|
|
vpx_codec_enc_cfg_t config;
|
2014-11-20 00:01:32 +00:00
|
|
|
switch_time_t last_key_frame;
|
2014-11-11 03:38:56 +00:00
|
|
|
|
|
|
|
vpx_codec_ctx_t encoder;
|
2014-11-15 01:01:56 +00:00
|
|
|
uint8_t encoder_init;
|
2014-11-11 03:38:56 +00:00
|
|
|
vpx_image_t *pic;
|
|
|
|
switch_bool_t force_key_frame;
|
|
|
|
int fps;
|
|
|
|
int format;
|
|
|
|
int intra_period;
|
|
|
|
int num;
|
|
|
|
int partition_index;
|
|
|
|
const vpx_codec_cx_pkt_t *pkt;
|
2015-05-06 19:02:44 +00:00
|
|
|
vpx_codec_iter_t enc_iter;
|
|
|
|
vpx_codec_iter_t dec_iter;
|
2015-01-29 22:37:29 +00:00
|
|
|
uint32_t last_ts;
|
2015-03-13 15:41:47 +00:00
|
|
|
switch_time_t last_ms;
|
2014-11-11 03:38:56 +00:00
|
|
|
vpx_codec_ctx_t decoder;
|
2014-11-15 01:01:56 +00:00
|
|
|
uint8_t decoder_init;
|
2014-11-11 03:38:56 +00:00
|
|
|
switch_buffer_t *vpx_packet_buffer;
|
|
|
|
int got_key_frame;
|
2015-11-18 16:31:30 +00:00
|
|
|
int no_key_frame;
|
2015-05-23 03:08:27 +00:00
|
|
|
int got_start_frame;
|
2015-03-29 12:32:12 +00:00
|
|
|
uint32_t last_received_timestamp;
|
2014-11-11 03:38:56 +00:00
|
|
|
switch_bool_t last_received_complete_picture;
|
|
|
|
int need_key_frame;
|
2015-02-13 20:10:42 +00:00
|
|
|
int need_encoder_reset;
|
|
|
|
int need_decoder_reset;
|
2015-02-06 22:13:32 +00:00
|
|
|
int32_t change_bandwidth;
|
2015-02-03 04:58:39 +00:00
|
|
|
uint64_t framecount;
|
2015-03-11 23:33:20 +00:00
|
|
|
switch_memory_pool_t *pool;
|
|
|
|
switch_buffer_t *pbuffer;
|
2015-03-12 16:17:35 +00:00
|
|
|
switch_time_t start_time;
|
2016-06-16 02:08:46 +00:00
|
|
|
switch_image_t *patch_img;
|
2014-11-11 03:38:56 +00:00
|
|
|
};
|
|
|
|
typedef struct vpx_context vpx_context_t;
|
|
|
|
|
|
|
|
|
2015-02-17 21:55:23 +00:00
|
|
|
static switch_status_t init_decoder(switch_codec_t *codec)
|
|
|
|
{
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
2015-03-11 23:33:20 +00:00
|
|
|
vpx_codec_dec_cfg_t cfg = {0, 0, 0};
|
|
|
|
vpx_codec_flags_t dec_flags = 0;
|
|
|
|
|
2015-02-17 21:55:23 +00:00
|
|
|
if (context->flags & SWITCH_CODEC_FLAG_DECODE && !context->decoder_init) {
|
|
|
|
vp8_postproc_cfg_t ppcfg;
|
2016-01-25 00:11:11 +00:00
|
|
|
|
2015-02-17 21:55:23 +00:00
|
|
|
//if (context->decoder_init) {
|
|
|
|
// vpx_codec_destroy(&context->decoder);
|
|
|
|
// context->decoder_init = 0;
|
|
|
|
//}
|
|
|
|
|
2016-02-25 22:11:28 +00:00
|
|
|
cfg.threads = 1;//(switch_core_cpu_count() > 1) ? 2 : 1;
|
2015-03-11 23:33:20 +00:00
|
|
|
|
|
|
|
if (!context->is_vp9) { // vp8 only
|
2016-01-25 00:11:11 +00:00
|
|
|
// dec_flags = VPX_CODEC_USE_POSTPROC;
|
2015-03-11 23:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vpx_codec_dec_init(&context->decoder, context->decoder_interface, &cfg, dec_flags) != VPX_CODEC_OK) {
|
2016-01-25 00:11:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec %s init error: [%d:%s]\n", vpx_codec_iface_name(context->decoder_interface), context->encoder.err, context->encoder.err_detail);
|
2015-02-17 21:55:23 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
|
|
|
|
context->last_ts = 0;
|
|
|
|
context->last_received_timestamp = 0;
|
|
|
|
context->last_received_complete_picture = 0;
|
2015-02-17 21:55:23 +00:00
|
|
|
context->decoder_init = 1;
|
2015-03-26 20:00:55 +00:00
|
|
|
context->got_key_frame = 0;
|
2015-11-18 16:31:30 +00:00
|
|
|
context->no_key_frame = 0;
|
2015-05-23 03:08:27 +00:00
|
|
|
context->got_start_frame = 0;
|
2015-02-17 21:55:23 +00:00
|
|
|
// the types of post processing to be done, should be combination of "vp8_postproc_level"
|
2015-03-26 20:00:55 +00:00
|
|
|
ppcfg.post_proc_flag = VP8_DEBLOCK;//VP8_DEMACROBLOCK | VP8_DEBLOCK;
|
2015-02-17 21:55:23 +00:00
|
|
|
// the strength of deblocking, valid range [0, 16]
|
2015-03-26 20:00:55 +00:00
|
|
|
ppcfg.deblocking_level = 1;
|
2015-02-17 21:55:23 +00:00
|
|
|
// Set deblocking settings
|
|
|
|
vpx_codec_control(&context->decoder, VP8_SET_POSTPROC, &ppcfg);
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
if (context->vpx_packet_buffer) {
|
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
|
|
|
} else {
|
|
|
|
switch_buffer_create_dynamic(&context->vpx_packet_buffer, 512, 512, 0);
|
|
|
|
}
|
2015-02-17 21:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static switch_status_t init_encoder(switch_codec_t *codec)
|
2014-11-15 01:01:56 +00:00
|
|
|
{
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
|
|
|
vpx_codec_enc_cfg_t *config = &context->config;
|
2014-11-20 00:01:32 +00:00
|
|
|
int token_parts = 1;
|
2014-11-19 20:48:14 +00:00
|
|
|
int cpus = switch_core_cpu_count();
|
2016-05-25 15:59:15 +00:00
|
|
|
int sane, threads = 1;
|
2015-11-19 03:36:41 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
if (!context->codec_settings.video.width) {
|
|
|
|
context->codec_settings.video.width = 1280;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context->codec_settings.video.height) {
|
|
|
|
context->codec_settings.video.height = 720;
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:13:32 +00:00
|
|
|
if (context->codec_settings.video.bandwidth == -1) {
|
|
|
|
context->codec_settings.video.bandwidth = 0;
|
|
|
|
}
|
2015-11-19 03:36:41 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
if (context->codec_settings.video.bandwidth) {
|
2015-04-20 18:56:58 +00:00
|
|
|
context->bandwidth = context->codec_settings.video.bandwidth;
|
2014-11-18 22:39:32 +00:00
|
|
|
} else {
|
2015-12-09 21:33:18 +00:00
|
|
|
context->bandwidth = switch_calc_bitrate(context->codec_settings.video.width, context->codec_settings.video.height, 1, 15);
|
2015-11-19 03:36:41 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 21:42:51 +00:00
|
|
|
sane = switch_calc_bitrate(1920, 1080, 2, 30);
|
2015-11-19 03:36:41 +00:00
|
|
|
|
|
|
|
if (context->bandwidth > sane) {
|
2015-11-20 22:24:35 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(codec->session), SWITCH_LOG_WARNING, "BITRATE TRUNCATED FROM %d TO %d\n", context->bandwidth, sane);
|
2015-11-19 03:36:41 +00:00
|
|
|
context->bandwidth = sane;
|
2014-11-18 22:39:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 17:44:38 +00:00
|
|
|
context->pkt = NULL;
|
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(codec->session), SWITCH_LOG_NOTICE,
|
2014-11-20 00:01:32 +00:00
|
|
|
"VPX reset encoder picture from %dx%d to %dx%d %u BW\n",
|
|
|
|
config->g_w, config->g_h, context->codec_settings.video.width, context->codec_settings.video.height, context->bandwidth);
|
|
|
|
|
2015-03-12 16:17:35 +00:00
|
|
|
context->start_time = switch_micro_time_now();
|
|
|
|
|
|
|
|
config->g_timebase.num = 1;
|
|
|
|
config->g_timebase.den = 1000;
|
|
|
|
config->g_pass = VPX_RC_ONE_PASS;
|
|
|
|
config->g_w = context->codec_settings.video.width;
|
|
|
|
config->g_h = context->codec_settings.video.height;
|
|
|
|
config->rc_target_bitrate = context->bandwidth;
|
|
|
|
config->g_lag_in_frames = 0;
|
2016-04-27 04:05:08 +00:00
|
|
|
config->kf_max_dist = 360;//2000;
|
2016-05-25 15:59:15 +00:00
|
|
|
threads = cpus / 4;
|
|
|
|
if (threads < 0) threads = 1;
|
|
|
|
config->g_threads = threads;
|
2015-03-12 16:17:35 +00:00
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (context->is_vp9) {
|
2015-03-12 16:17:35 +00:00
|
|
|
//config->rc_dropframe_thresh = 2;
|
2015-03-11 23:33:20 +00:00
|
|
|
token_parts = (cpus > 1) ? 3 : 0;
|
|
|
|
|
2015-03-12 16:17:35 +00:00
|
|
|
if (context->lossless) {
|
|
|
|
config->rc_min_quantizer = 0;
|
|
|
|
config->rc_max_quantizer = 0;
|
|
|
|
} else {
|
|
|
|
config->rc_min_quantizer = 0;
|
|
|
|
config->rc_max_quantizer = 63;
|
|
|
|
}
|
2014-11-15 01:01:56 +00:00
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
config->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING;
|
|
|
|
config->ts_number_layers = 1;
|
|
|
|
config->ts_rate_decimator[0] = 1;
|
|
|
|
config->ts_periodicity = 1;
|
|
|
|
config->ts_layer_id[0] = 0;
|
2015-03-11 23:33:20 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// settings
|
2015-03-12 16:17:35 +00:00
|
|
|
config->g_profile = 2;
|
2015-03-11 23:33:20 +00:00
|
|
|
config->g_error_resilient = VPX_ERROR_RESILIENT_PARTITIONS;
|
|
|
|
token_parts = (cpus > 1) ? 3 : 0;
|
|
|
|
|
|
|
|
// rate control settings
|
|
|
|
config->rc_dropframe_thresh = 0;
|
|
|
|
config->rc_end_usage = VPX_CBR;
|
2015-03-12 16:17:35 +00:00
|
|
|
//config->g_pass = VPX_RC_ONE_PASS;
|
2015-03-11 23:33:20 +00:00
|
|
|
config->kf_mode = VPX_KF_AUTO;
|
2016-04-27 04:05:08 +00:00
|
|
|
config->kf_max_dist = 360;//1000;
|
2015-03-11 23:33:20 +00:00
|
|
|
|
|
|
|
//config->kf_mode = VPX_KF_DISABLED;
|
|
|
|
config->rc_resize_allowed = 1;
|
2015-03-12 16:17:35 +00:00
|
|
|
//config->rc_min_quantizer = 0;
|
|
|
|
//config->rc_max_quantizer = 63;
|
2015-03-11 23:33:20 +00:00
|
|
|
config->rc_min_quantizer = 0;
|
|
|
|
config->rc_max_quantizer = 63;
|
|
|
|
//Rate control adaptation undershoot control.
|
|
|
|
// This value, expressed as a percentage of the target bitrate,
|
|
|
|
// controls the maximum allowed adaptation speed of the codec.
|
|
|
|
// This factor controls the maximum amount of bits that can be
|
|
|
|
// subtracted from the target bitrate in order to compensate for
|
|
|
|
// prior overshoot.
|
|
|
|
// Valid values in the range 0-1000.
|
|
|
|
config->rc_undershoot_pct = 100;
|
|
|
|
//Rate control adaptation overshoot control.
|
|
|
|
// This value, expressed as a percentage of the target bitrate,
|
|
|
|
// controls the maximum allowed adaptation speed of the codec.
|
|
|
|
// This factor controls the maximum amount of bits that can be
|
|
|
|
// added to the target bitrate in order to compensate for prior
|
|
|
|
// undershoot.
|
|
|
|
// Valid values in the range 0-1000.
|
|
|
|
config->rc_overshoot_pct = 15;
|
|
|
|
//Decoder Buffer Size.
|
|
|
|
// This value indicates the amount of data that may be buffered
|
|
|
|
// by the decoding application. Note that this value is expressed
|
|
|
|
// in units of time (milliseconds). For example, a value of 5000
|
|
|
|
// indicates that the client will buffer (at least) 5000ms worth
|
|
|
|
// of encoded data. Use the target bitrate (rc_target_bitrate) to
|
|
|
|
// convert to bits/bytes, if necessary.
|
|
|
|
config->rc_buf_sz = 5000;
|
|
|
|
//Decoder Buffer Initial Size.
|
|
|
|
// This value indicates the amount of data that will be buffered
|
|
|
|
// by the decoding application prior to beginning playback.
|
|
|
|
// This value is expressed in units of time (milliseconds).
|
|
|
|
// Use the target bitrate (rc_target_bitrate) to convert to
|
|
|
|
// bits/bytes, if necessary.
|
|
|
|
config->rc_buf_initial_sz = 1000;
|
|
|
|
//Decoder Buffer Optimal Size.
|
|
|
|
// This value indicates the amount of data that the encoder should
|
|
|
|
// try to maintain in the decoder's buffer. This value is expressed
|
|
|
|
// in units of time (milliseconds).
|
|
|
|
// Use the target bitrate (rc_target_bitrate) to convert to
|
|
|
|
// bits/bytes, if necessary.
|
|
|
|
config->rc_buf_optimal_sz = 1000;
|
|
|
|
}
|
2014-11-15 01:01:56 +00:00
|
|
|
|
2014-11-20 00:01:32 +00:00
|
|
|
if (context->encoder_init) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "VPX ENCODER RESET\n");
|
|
|
|
if (vpx_codec_enc_config_set(&context->encoder, config) != VPX_CODEC_OK) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec init error: [%d:%s]\n", context->encoder.err, context->encoder.err_detail);
|
2014-11-15 01:01:56 +00:00
|
|
|
}
|
2014-11-20 00:01:32 +00:00
|
|
|
} else if (context->flags & SWITCH_CODEC_FLAG_ENCODE) {
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
// #define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, config->field);
|
2016-01-15 07:38:13 +00:00
|
|
|
|
|
|
|
#ifdef SHOW
|
|
|
|
fprintf(stderr, "Codec: %s\n", vpx_codec_iface_name(context->encoder_interface));
|
|
|
|
|
|
|
|
SHOW(g_usage);
|
|
|
|
SHOW(g_threads);
|
|
|
|
SHOW(g_profile);
|
|
|
|
SHOW(g_w);
|
|
|
|
SHOW(g_h);
|
|
|
|
SHOW(g_bit_depth);
|
|
|
|
SHOW(g_input_bit_depth);
|
|
|
|
SHOW(g_timebase.num);
|
|
|
|
SHOW(g_timebase.den);
|
|
|
|
SHOW(g_error_resilient);
|
|
|
|
SHOW(g_pass);
|
|
|
|
SHOW(g_lag_in_frames);
|
|
|
|
SHOW(rc_dropframe_thresh);
|
|
|
|
SHOW(rc_resize_allowed);
|
|
|
|
SHOW(rc_scaled_width);
|
|
|
|
SHOW(rc_scaled_height);
|
|
|
|
SHOW(rc_resize_up_thresh);
|
|
|
|
SHOW(rc_resize_down_thresh);
|
|
|
|
SHOW(rc_end_usage);
|
|
|
|
SHOW(rc_target_bitrate);
|
|
|
|
SHOW(rc_min_quantizer);
|
|
|
|
SHOW(rc_max_quantizer);
|
|
|
|
SHOW(rc_undershoot_pct);
|
|
|
|
SHOW(rc_overshoot_pct);
|
|
|
|
SHOW(rc_buf_sz);
|
|
|
|
SHOW(rc_buf_initial_sz);
|
|
|
|
SHOW(rc_buf_optimal_sz);
|
|
|
|
SHOW(rc_2pass_vbr_bias_pct);
|
|
|
|
SHOW(rc_2pass_vbr_minsection_pct);
|
|
|
|
SHOW(rc_2pass_vbr_maxsection_pct);
|
|
|
|
SHOW(kf_mode);
|
|
|
|
SHOW(kf_min_dist);
|
|
|
|
SHOW(kf_max_dist);
|
|
|
|
#endif
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (vpx_codec_enc_init(&context->encoder, context->encoder_interface, config, 0 & VPX_CODEC_USE_OUTPUT_PARTITION) != VPX_CODEC_OK) {
|
2016-01-25 00:11:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec %s init error: [%d:%s]\n", vpx_codec_iface_name(context->encoder_interface), context->encoder.err, context->encoder.err_detail);
|
2014-11-11 03:38:56 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2015-03-12 16:17:35 +00:00
|
|
|
|
2014-11-15 01:01:56 +00:00
|
|
|
context->encoder_init = 1;
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (context->is_vp9) {
|
2015-03-12 16:17:35 +00:00
|
|
|
if (context->lossless) {
|
|
|
|
vpx_codec_control(&context->encoder, VP9E_SET_LOSSLESS, 1);
|
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_CPUUSED, -6);
|
|
|
|
} else {
|
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_CPUUSED, -8);
|
|
|
|
}
|
|
|
|
|
2016-04-27 04:05:08 +00:00
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_STATIC_THRESHOLD, 1000);
|
2015-03-11 23:33:20 +00:00
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_TOKEN_PARTITIONS, token_parts);
|
2015-03-12 16:17:35 +00:00
|
|
|
vpx_codec_control(&context->encoder, VP9E_SET_TUNE_CONTENT, VP9E_CONTENT_SCREEN);
|
2014-11-19 20:48:14 +00:00
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
} else {
|
|
|
|
// The static threshold imposes a change threshold on blocks below which they will be skipped by the encoder.
|
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_STATIC_THRESHOLD, 100);
|
|
|
|
//Set cpu usage, a bit lower than normal (-6) but higher than android (-12)
|
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_CPUUSED, -6);
|
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_TOKEN_PARTITIONS, token_parts);
|
|
|
|
|
|
|
|
// Enable noise reduction
|
|
|
|
vpx_codec_control(&context->encoder, VP8E_SET_NOISE_SENSITIVITY, 1);
|
|
|
|
//Set max data rate for Intra frames.
|
|
|
|
// This value controls additional clamping on the maximum size of a keyframe.
|
|
|
|
// It is expressed as a percentage of the average per-frame bitrate, with the
|
|
|
|
// special (and default) value 0 meaning unlimited, or no additional clamping
|
|
|
|
// beyond the codec's built-in algorithm.
|
|
|
|
// For example, to allocate no more than 4.5 frames worth of bitrate to a keyframe, set this to 450.
|
|
|
|
//vpx_codec_control(&context->encoder, VP8E_SET_MAX_INTRA_BITRATE_PCT, 0);
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-11-15 01:01:56 +00:00
|
|
|
static switch_status_t switch_vpx_init(switch_codec_t *codec, switch_codec_flag_t flags, const switch_codec_settings_t *codec_settings)
|
|
|
|
{
|
|
|
|
vpx_context_t *context = NULL;
|
|
|
|
int encoding, decoding;
|
|
|
|
|
|
|
|
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
|
|
|
|
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
|
|
|
|
|
|
|
|
if (!(encoding || decoding) || ((context = switch_core_alloc(codec->memory_pool, sizeof(*context))) == 0)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(context, 0, sizeof(*context));
|
|
|
|
context->flags = flags;
|
|
|
|
codec->private_info = context;
|
2015-03-11 23:33:20 +00:00
|
|
|
context->pool = codec->memory_pool;
|
2014-11-15 01:01:56 +00:00
|
|
|
|
|
|
|
if (codec_settings) {
|
|
|
|
context->codec_settings = *codec_settings;
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (!strcmp(codec->implementation->iananame, "VP9")) {
|
|
|
|
context->is_vp9 = 1;
|
|
|
|
context->encoder_interface = vpx_codec_vp9_cx();
|
|
|
|
context->decoder_interface = vpx_codec_vp9_dx();
|
|
|
|
} else {
|
|
|
|
context->encoder_interface = vpx_codec_vp8_cx();
|
|
|
|
context->decoder_interface = vpx_codec_vp8_dx();
|
|
|
|
}
|
|
|
|
|
2014-11-15 01:01:56 +00:00
|
|
|
if (codec->fmtp_in) {
|
|
|
|
codec->fmtp_out = switch_core_strdup(codec->memory_pool, codec->fmtp_in);
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (vpx_codec_enc_config_default(context->encoder_interface, &context->config, 0) != VPX_CODEC_OK) {
|
2014-11-15 01:01:56 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Encoder config Error\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:55:23 +00:00
|
|
|
context->codec_settings.video.width = 320;
|
|
|
|
context->codec_settings.video.height = 240;
|
2014-11-20 00:01:32 +00:00
|
|
|
|
2015-04-21 09:27:49 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "VPX VER:%s VPX_IMAGE_ABI_VERSION:%d VPX_CODEC_ABI_VERSION:%d\n",
|
|
|
|
vpx_codec_version_str(), VPX_IMAGE_ABI_VERSION, VPX_CODEC_ABI_VERSION);
|
|
|
|
|
2014-11-15 01:01:56 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
static switch_status_t consume_partition(vpx_context_t *context, switch_frame_t *frame)
|
2014-11-11 03:38:56 +00:00
|
|
|
{
|
2015-03-11 23:33:20 +00:00
|
|
|
vpx_payload_descriptor_t *payload_descriptor;
|
|
|
|
uint8_t *body;
|
|
|
|
uint32_t hdrlen = 0, payload_size = 0, packet_size = 0, start = 0, key = 0;
|
|
|
|
switch_size_t remaining_bytes = 0;
|
|
|
|
|
2014-11-20 22:30:19 +00:00
|
|
|
if (!context->pkt) {
|
2015-05-06 19:02:44 +00:00
|
|
|
if ((context->pkt = vpx_codec_get_cx_data(&context->encoder, &context->enc_iter))) {
|
2015-03-11 23:33:20 +00:00
|
|
|
start = 1;
|
|
|
|
if (!context->pbuffer) {
|
|
|
|
switch_buffer_create_partition(context->pool, &context->pbuffer, context->pkt->data.frame.buf, context->pkt->data.frame.sz);
|
|
|
|
} else {
|
|
|
|
switch_buffer_set_partition_data(context->pbuffer, context->pkt->data.frame.buf, context->pkt->data.frame.sz);
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 22:30:19 +00:00
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (context->pbuffer) {
|
|
|
|
remaining_bytes = switch_buffer_inuse(context->pbuffer);
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (!context->pkt || context->pkt->kind != VPX_CODEC_CX_FRAME_PKT || !remaining_bytes) {
|
2014-11-18 22:39:32 +00:00
|
|
|
frame->datalen = 0;
|
|
|
|
frame->m = 1;
|
2014-11-11 03:38:56 +00:00
|
|
|
context->pkt = NULL;
|
2015-03-29 14:13:28 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "writing 0 bytes\n");
|
2014-11-11 03:38:56 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
key = (context->pkt->data.frame.flags & VPX_FRAME_IS_KEY);
|
2015-03-29 14:13:28 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "flags: %x pts: %lld duration:%lu partition_id: %d\n",
|
|
|
|
context->pkt->data.frame.flags, context->pkt->data.frame.pts, context->pkt->data.frame.duration, context->pkt->data.frame.partition_id);
|
|
|
|
#endif
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
/* reset header */
|
|
|
|
*(uint8_t *)frame->data = 0;
|
|
|
|
payload_descriptor = (vpx_payload_descriptor_t *) frame->data;
|
|
|
|
|
|
|
|
// if !extended
|
|
|
|
hdrlen = 1;
|
|
|
|
body = ((uint8_t *)frame->data) + hdrlen;
|
|
|
|
packet_size = SLICE_SIZE;
|
|
|
|
payload_size = packet_size - hdrlen;
|
|
|
|
// else add extended TBD
|
|
|
|
|
|
|
|
frame->datalen = hdrlen;
|
|
|
|
|
|
|
|
if (context->is_vp9) {
|
|
|
|
payload_descriptor->vp9.start = start;
|
2016-02-04 00:36:17 +00:00
|
|
|
|
|
|
|
if (1) {
|
|
|
|
// payload_descriptor->vp9.have_p_layer = key; // key?
|
2016-03-28 06:09:07 +00:00
|
|
|
payload_descriptor->vp9.have_pid = 1;
|
|
|
|
|
|
|
|
if (payload_descriptor->vp9.have_pid) {
|
|
|
|
|
|
|
|
if (context->vp9.picture_id < 0) context->vp9.picture_id = 0;
|
|
|
|
|
|
|
|
if (context->vp9.picture_id > 0x7f) {
|
|
|
|
*body++ = (context->vp9.picture_id >> 8) | 0x80;
|
|
|
|
*body++ = context->vp9.picture_id & 0xff;
|
|
|
|
payload_size--;
|
|
|
|
frame->datalen++;
|
|
|
|
} else {
|
|
|
|
*body++ = context->vp9.picture_id;
|
|
|
|
}
|
2016-02-04 00:36:17 +00:00
|
|
|
|
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
payload_size--;
|
|
|
|
frame->datalen++;
|
|
|
|
}
|
2016-02-04 00:36:17 +00:00
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
if (start) {
|
|
|
|
context->vp9.picture_id++;
|
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "sending pid: %d\n", context->vp9.picture_id);
|
|
|
|
#endif
|
|
|
|
}
|
2016-02-04 00:36:17 +00:00
|
|
|
|
|
|
|
if (key) {
|
|
|
|
vp9_ss_t *ss = (vp9_ss_t *)body;
|
|
|
|
|
|
|
|
payload_descriptor->vp9.have_ss = 1;
|
2016-03-28 06:09:07 +00:00
|
|
|
payload_descriptor->vp9.have_p_layer = 0;
|
2016-02-04 00:36:17 +00:00
|
|
|
ss->n_s = 0;
|
|
|
|
ss->g = 0;
|
|
|
|
ss->y = 0;
|
|
|
|
ss->zero = 0;
|
|
|
|
body++;
|
|
|
|
payload_size--;
|
|
|
|
frame->datalen++;
|
|
|
|
|
|
|
|
if (0) { // y ?
|
|
|
|
uint16_t *w;
|
|
|
|
uint16_t *h;
|
|
|
|
|
|
|
|
ss->y = 1;
|
|
|
|
|
|
|
|
w = (uint16_t *)body;
|
|
|
|
body+=2;
|
|
|
|
h = (uint16_t *)body;
|
|
|
|
body+=2;
|
|
|
|
|
|
|
|
*w = (uint16_t)context->codec_settings.video.width;
|
|
|
|
*h = (uint16_t)context->codec_settings.video.height;
|
|
|
|
|
|
|
|
payload_size-= (ss->n_s + 1) * 4;
|
|
|
|
frame->datalen+= (ss->n_s + 1) * 4;
|
|
|
|
}
|
2016-03-28 06:09:07 +00:00
|
|
|
} else {
|
|
|
|
payload_descriptor->vp9.have_p_layer = 1;
|
2016-02-04 00:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
} else {
|
|
|
|
payload_descriptor->vp8.start = start;
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
if (remaining_bytes <= payload_size) {
|
|
|
|
switch_buffer_read(context->pbuffer, body, remaining_bytes);
|
2014-11-11 03:38:56 +00:00
|
|
|
context->pkt = NULL;
|
2015-03-11 23:33:20 +00:00
|
|
|
frame->datalen += remaining_bytes;
|
|
|
|
frame->m = 1;
|
2014-11-11 03:38:56 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
} else {
|
2015-03-11 23:33:20 +00:00
|
|
|
switch_buffer_read(context->pbuffer, body, payload_size);
|
|
|
|
frame->datalen += payload_size;
|
|
|
|
frame->m = 0;
|
|
|
|
return SWITCH_STATUS_MORE_DATA;
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
2016-02-04 00:36:17 +00:00
|
|
|
|
|
|
|
if (frame->m && context->is_vp9) {
|
|
|
|
payload_descriptor->vp9.end = 1;
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
static switch_status_t reset_codec_encoder(switch_codec_t *codec)
|
2015-02-17 21:55:23 +00:00
|
|
|
{
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
|
|
|
|
|
|
|
if (context->encoder_init) {
|
|
|
|
vpx_codec_destroy(&context->encoder);
|
|
|
|
}
|
2015-03-13 15:41:47 +00:00
|
|
|
context->last_ts = 0;
|
|
|
|
context->last_ms = 0;
|
2015-02-17 21:55:23 +00:00
|
|
|
context->framecount = 0;
|
|
|
|
context->encoder_init = 0;
|
2015-03-03 17:44:38 +00:00
|
|
|
context->pkt = NULL;
|
2016-02-04 00:36:17 +00:00
|
|
|
return init_encoder(codec);
|
2015-02-17 21:55:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
static switch_status_t switch_vpx_encode(switch_codec_t *codec, switch_frame_t *frame)
|
2014-11-11 03:38:56 +00:00
|
|
|
{
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2015-03-13 15:41:47 +00:00
|
|
|
uint32_t dur;
|
|
|
|
int64_t pts;
|
2014-11-11 03:38:56 +00:00
|
|
|
vpx_enc_frame_flags_t vpx_flags = 0;
|
2015-03-13 15:41:47 +00:00
|
|
|
switch_time_t now;
|
2015-04-07 03:03:39 +00:00
|
|
|
int err;
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
if (frame->flags & SFF_SAME_IMAGE) {
|
|
|
|
return consume_partition(context, frame);
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 20:10:42 +00:00
|
|
|
if (context->need_encoder_reset != 0) {
|
2016-02-04 00:36:17 +00:00
|
|
|
if (reset_codec_encoder(codec) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2015-02-13 20:10:42 +00:00
|
|
|
context->need_encoder_reset = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
if (frame->img->d_h > 1) {
|
|
|
|
width = frame->img->d_w;
|
|
|
|
height = frame->img->d_h;
|
|
|
|
} else {
|
|
|
|
width = frame->img->w;
|
|
|
|
height = frame->img->h;
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
|
|
|
if (context->config.g_w != width || context->config.g_h != height) {
|
2014-11-15 01:01:56 +00:00
|
|
|
context->codec_settings.video.width = width;
|
|
|
|
context->codec_settings.video.height = height;
|
2015-02-17 21:55:23 +00:00
|
|
|
reset_codec_encoder(codec);
|
2014-11-18 22:39:32 +00:00
|
|
|
frame->flags |= SFF_PICTURE_RESET;
|
2016-02-24 04:01:38 +00:00
|
|
|
context->need_key_frame = 3;
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
|
|
|
|
if (!context->encoder_init) {
|
2016-02-04 00:36:17 +00:00
|
|
|
if (init_encoder(codec) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2014-11-18 22:39:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 22:13:32 +00:00
|
|
|
if (context->change_bandwidth) {
|
|
|
|
context->codec_settings.video.bandwidth = context->change_bandwidth;
|
|
|
|
context->change_bandwidth = 0;
|
2016-02-04 00:36:17 +00:00
|
|
|
if (init_encoder(codec) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2015-02-06 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 15:41:47 +00:00
|
|
|
now = switch_time_now();
|
|
|
|
|
2016-02-24 04:01:38 +00:00
|
|
|
if (context->need_key_frame > 0) {
|
2014-11-11 03:38:56 +00:00
|
|
|
// force generate a key frame
|
2014-11-20 00:01:32 +00:00
|
|
|
|
2015-03-12 16:17:35 +00:00
|
|
|
if (!context->last_key_frame || (now - context->last_key_frame) > KEY_FRAME_MIN_FREQ) {
|
2015-04-08 19:16:10 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "VPX KEYFRAME GENERATED\n");
|
2014-11-20 00:01:32 +00:00
|
|
|
vpx_flags |= VPX_EFLAG_FORCE_KF;
|
|
|
|
context->need_key_frame = 0;
|
|
|
|
context->last_key_frame = now;
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 04:58:39 +00:00
|
|
|
context->framecount++;
|
|
|
|
|
2015-03-13 15:41:47 +00:00
|
|
|
pts = (now - context->start_time) / 1000;
|
|
|
|
|
|
|
|
dur = context->last_ms ? (now - context->last_ms) / 1000 : pts;
|
|
|
|
|
2015-04-07 03:03:39 +00:00
|
|
|
if ((err = vpx_codec_encode(&context->encoder,
|
2015-03-12 16:17:35 +00:00
|
|
|
(vpx_image_t *) frame->img,
|
2015-03-13 15:41:47 +00:00
|
|
|
pts,
|
2016-02-04 00:36:17 +00:00
|
|
|
dur,
|
2015-03-12 16:17:35 +00:00
|
|
|
vpx_flags,
|
2015-04-07 03:03:39 +00:00
|
|
|
VPX_DL_REALTIME)) != VPX_CODEC_OK) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "VPX encode error %d:%s:%s\n",
|
|
|
|
err, vpx_codec_error(&context->encoder), vpx_codec_error_detail(&context->encoder));
|
2014-11-18 22:39:32 +00:00
|
|
|
frame->datalen = 0;
|
2014-11-11 03:38:56 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-05-06 19:02:44 +00:00
|
|
|
context->enc_iter = NULL;
|
2015-01-29 22:37:29 +00:00
|
|
|
context->last_ts = frame->timestamp;
|
2015-03-13 15:41:47 +00:00
|
|
|
context->last_ms = now;
|
2015-02-03 04:58:39 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
return consume_partition(context, frame);
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
static switch_status_t buffer_vp8_packets(vpx_context_t *context, switch_frame_t *frame)
|
2014-11-11 03:38:56 +00:00
|
|
|
{
|
|
|
|
uint8_t *data = frame->data;
|
|
|
|
uint8_t S;
|
|
|
|
uint8_t DES;
|
2015-06-22 20:51:12 +00:00
|
|
|
// uint8_t PID;
|
2014-11-11 03:38:56 +00:00
|
|
|
int len;
|
2015-06-22 20:51:12 +00:00
|
|
|
#if 0
|
2015-05-23 03:08:27 +00:00
|
|
|
int key = 0;
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
|
|
|
|
"VIDEO VPX: seq: %d ts: %u len: %ld %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x mark: %d\n",
|
|
|
|
frame->seq, frame->timestamp, frame->datalen,
|
|
|
|
*((uint8_t *)data), *((uint8_t *)data + 1),
|
|
|
|
*((uint8_t *)data + 2), *((uint8_t *)data + 3),
|
|
|
|
*((uint8_t *)data + 4), *((uint8_t *)data + 5),
|
|
|
|
*((uint8_t *)data + 6), *((uint8_t *)data + 7),
|
|
|
|
*((uint8_t *)data + 8), *((uint8_t *)data + 9),
|
|
|
|
*((uint8_t *)data + 10), frame->m);
|
|
|
|
#endif
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-05-23 03:08:27 +00:00
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
DES = *data;
|
|
|
|
data++;
|
2015-05-23 03:08:27 +00:00
|
|
|
S = (DES & 0x10);
|
2015-06-22 20:51:12 +00:00
|
|
|
// PID = DES & 0x07;
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-05-23 03:08:27 +00:00
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DATA LEN %d S BIT %d PID: %d\n", frame->datalen, S, PID);
|
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
if (DES & 0x80) { // X
|
|
|
|
uint8_t X = *data;
|
2015-05-23 03:08:27 +00:00
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "X BIT SET\n");
|
2014-11-11 03:38:56 +00:00
|
|
|
data++;
|
|
|
|
if (X & 0x80) { // I
|
|
|
|
uint8_t M = (*data) & 0x80;
|
2015-05-23 03:08:27 +00:00
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "I BIT SET\n");
|
2014-11-11 03:38:56 +00:00
|
|
|
data++;
|
2015-05-23 03:08:27 +00:00
|
|
|
if (M) {
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "M BIT SET\n");
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (X & 0x40) {
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "L BIT SET\n");
|
|
|
|
data++; // L
|
|
|
|
}
|
|
|
|
if (X & 0x30) {
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "T/K BIT SET\n");
|
|
|
|
data++; // T/K
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
if (!switch_buffer_inuse(context->vpx_packet_buffer) && !S) {
|
|
|
|
if (context->got_key_frame > 0) {
|
|
|
|
context->got_key_frame = 0;
|
2015-05-23 03:08:27 +00:00
|
|
|
context->got_start_frame = 0;
|
2015-04-08 19:16:10 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "packet loss?\n");
|
2015-03-26 20:00:55 +00:00
|
|
|
}
|
|
|
|
return SWITCH_STATUS_MORE_DATA;
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
if (S) {
|
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
2015-03-29 12:32:12 +00:00
|
|
|
context->last_received_timestamp = frame->timestamp;
|
2015-06-22 20:51:12 +00:00
|
|
|
#if 0
|
2015-05-23 03:08:27 +00:00
|
|
|
if (PID == 0) {
|
|
|
|
key = __IS_VP8_KEY_FRAME(*data);
|
|
|
|
}
|
2015-06-22 20:51:12 +00:00
|
|
|
#endif
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
len = frame->datalen - (data - (uint8_t *)frame->data);
|
2015-05-23 03:08:27 +00:00
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "POST PARSE: DATA LEN %d KEY %d KEYBYTE = %0x\n", len, key, *data);
|
|
|
|
|
2015-03-26 20:00:55 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid packet %d\n", len);
|
2014-11-18 22:39:32 +00:00
|
|
|
return SWITCH_STATUS_RESTART;
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 12:32:12 +00:00
|
|
|
if (context->last_received_timestamp != frame->timestamp) {
|
2015-04-08 19:16:10 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "wrong timestamp %u, expect %u, packet loss?\n", frame->timestamp, context->last_received_timestamp);
|
2015-03-29 12:32:12 +00:00
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
|
|
|
return SWITCH_STATUS_RESTART;
|
|
|
|
}
|
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
switch_buffer_write(context->vpx_packet_buffer, data, len);
|
2014-11-18 22:39:32 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
// https://tools.ietf.org/id/draft-ietf-payload-vp9-01.txt
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
static switch_status_t buffer_vp9_packets(vpx_context_t *context, switch_frame_t *frame)
|
|
|
|
{
|
2015-03-12 02:26:14 +00:00
|
|
|
uint8_t *data = (uint8_t *)frame->data;
|
|
|
|
uint8_t *vp9 = (uint8_t *)frame->data;
|
2016-02-04 00:36:17 +00:00
|
|
|
vp9_payload_descriptor_t *desc = (vp9_payload_descriptor_t *)vp9;
|
2015-03-11 23:33:20 +00:00
|
|
|
int len = 0;
|
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%02x %02x %02x %02x m=%d len=%4d "
|
|
|
|
"have_pid=%d "
|
|
|
|
"have_p_layer=%d "
|
|
|
|
"have_layer_ind=%d "
|
|
|
|
"is_flexible=%d "
|
|
|
|
"start=%d "
|
|
|
|
"end=%d "
|
|
|
|
"have_ss=%d "
|
|
|
|
"zero=%d\n",
|
|
|
|
*data, *(data+1), *(data+2), *(data+3), frame->m, frame->datalen,
|
|
|
|
desc->have_pid,
|
|
|
|
desc->have_p_layer,
|
|
|
|
desc->have_layer_ind,
|
|
|
|
desc->is_flexible,
|
|
|
|
desc->start,
|
|
|
|
desc->end,
|
|
|
|
desc->have_ss,
|
|
|
|
desc->zero);
|
|
|
|
#endif
|
2016-02-04 00:36:17 +00:00
|
|
|
|
|
|
|
vp9++;
|
|
|
|
|
|
|
|
if (desc->have_pid) {
|
|
|
|
uint16_t pid = 0;
|
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
pid = *vp9 & 0x7f; //0 bit is M , 1-7 bit is pid.
|
2016-02-04 00:36:17 +00:00
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
if (*vp9 & 0x80) { //if (M==1)
|
2016-02-04 00:36:17 +00:00
|
|
|
vp9++;
|
|
|
|
pid = (pid << 8) + *vp9;
|
|
|
|
}
|
|
|
|
|
|
|
|
vp9++;
|
2016-03-28 06:09:07 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "have pid: %d start=%d end=%d\n", pid, desc->start, desc->end);
|
|
|
|
#endif
|
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->have_layer_ind) {
|
2016-03-28 06:09:07 +00:00
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
vp9_p_layer_t *layer = (vp9_p_layer_t *)vp9;
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "temporal_id=%d temporal_up_switch=%d spatial_id=%d inter_layer_predicted=%d\n",
|
|
|
|
layer->temporal_id, layer->temporal_up_switch, layer->spatial_id, layer->inter_layer_predicted);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vp9++;
|
|
|
|
if (!desc->is_flexible) {
|
|
|
|
vp9++; // TL0PICIDX
|
|
|
|
}
|
|
|
|
}
|
2016-02-04 00:36:17 +00:00
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
//When P and F are both set to one, indicating a non-key frame in flexible mode
|
|
|
|
if (desc->have_p_layer && desc->is_flexible) { // P & F set, P_DIFF
|
|
|
|
if (*vp9 & 1) { // N
|
|
|
|
vp9++;
|
|
|
|
if (*vp9 & 1) { // N
|
|
|
|
vp9++;
|
|
|
|
if (*vp9 & 1) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid VP9 packet!");
|
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vp9++;
|
2016-02-04 00:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->have_ss) {
|
|
|
|
vp9_ss_t *ss = (vp9_ss_t *)(vp9++);
|
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "have ss: %02x n_s: %d y:%d g:%d\n", *(uint8_t *)ss, ss->n_s, ss->y, ss->g);
|
|
|
|
#endif
|
2016-02-04 00:36:17 +00:00
|
|
|
if (ss->y) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<=ss->n_s; i++) {
|
2016-03-28 06:09:07 +00:00
|
|
|
#ifdef DEBUG_VP9
|
2016-02-04 00:36:17 +00:00
|
|
|
int width = ntohs(*(uint16_t *)vp9);
|
|
|
|
int height = ntohs(*(uint16_t *)(vp9 + 2));
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SS: %d %dx%d\n", i, width, height);
|
2016-03-28 06:09:07 +00:00
|
|
|
#endif
|
|
|
|
vp9 += 4;
|
2016-02-04 00:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ss->g) {
|
|
|
|
int i;
|
2016-03-28 06:09:07 +00:00
|
|
|
uint8_t ng = *vp9++; //N_G indicates the number of frames in a GOF
|
2016-02-04 00:36:17 +00:00
|
|
|
|
|
|
|
for (i = 0; ng > 0 && i < ng; i++) {
|
|
|
|
vp9_n_g_t *n_g = (vp9_n_g_t *)(vp9++);
|
|
|
|
vp9 += n_g->r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vp9 - data >= frame->datalen) {
|
2016-03-28 06:09:07 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Invalid VP9 Packet %" SWITCH_SSIZE_T_FMT " > %d\n", vp9 - data, frame->datalen);
|
2016-02-04 00:36:17 +00:00
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
|
|
|
goto end;
|
|
|
|
}
|
2015-03-11 23:33:20 +00:00
|
|
|
|
2016-07-19 21:25:22 +00:00
|
|
|
if (!switch_buffer_inuse(context->vpx_packet_buffer)) { // middle packet
|
|
|
|
//if (desc->start) {
|
|
|
|
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "got invalid vp9 packet, packet loss? resetting buffer\n");
|
|
|
|
// switch_buffer_zero(context->vpx_packet_buffer);
|
|
|
|
//}
|
|
|
|
//} else { // start packet
|
2016-02-04 00:36:17 +00:00
|
|
|
if (!desc->start) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "got invalid vp9 packet, packet loss? waiting for a start packet\n");
|
2015-03-13 01:03:52 +00:00
|
|
|
goto end;
|
2015-03-12 02:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
len = frame->datalen - (vp9 - data);
|
|
|
|
switch_buffer_write(context->vpx_packet_buffer, vp9, len);
|
|
|
|
|
2015-03-13 01:03:52 +00:00
|
|
|
end:
|
2016-03-28 06:09:07 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "buffered %d bytes, buffer size: %" SWITCH_SIZE_T_FMT "\n", len, switch_buffer_inuse(context->vpx_packet_buffer));
|
|
|
|
#endif
|
2015-03-11 23:33:20 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
static switch_status_t switch_vpx_decode(switch_codec_t *codec, switch_frame_t *frame)
|
2014-11-11 03:38:56 +00:00
|
|
|
{
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
|
|
|
switch_size_t len;
|
2014-11-18 22:39:32 +00:00
|
|
|
vpx_codec_ctx_t *decoder = NULL;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2015-05-06 19:02:44 +00:00
|
|
|
int is_start = 0, is_keyframe = 0, get_refresh = 0;
|
2015-02-13 20:10:42 +00:00
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
#if 0
|
|
|
|
vp9_payload_descriptor_t *desc = (vp9_payload_descriptor_t *)frame->data;
|
|
|
|
uint8_t *data = frame->data;
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%02x %02x %02x %02x m=%d start=%d end=%d m=%d len=%d\n",
|
|
|
|
*data, *(data+1), *(data+2), *(data+3), frame->m, desc->start, desc->end, frame->m, frame->datalen);
|
|
|
|
#endif
|
|
|
|
|
2015-03-12 02:26:14 +00:00
|
|
|
if (context->is_vp9) {
|
2016-03-28 06:09:07 +00:00
|
|
|
is_keyframe = IS_VP9_KEY_FRAME(*(unsigned char *)frame->data);
|
|
|
|
is_start = IS_VP9_START_PKT(*(unsigned char *)frame->data);
|
|
|
|
|
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
if (is_keyframe) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "================Got a key frame!!!!========================\n");
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-12 02:26:14 +00:00
|
|
|
} else { // vp8
|
2015-05-06 19:02:44 +00:00
|
|
|
is_start = (*(unsigned char *)frame->data & 0x10);
|
2015-05-05 18:27:52 +00:00
|
|
|
is_keyframe = IS_VP8_KEY_FRAME((uint8_t *)frame->data);
|
2016-03-28 06:09:07 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "VP8\n");
|
|
|
|
#endif
|
2015-03-12 02:26:14 +00:00
|
|
|
}
|
2016-03-28 06:09:07 +00:00
|
|
|
|
|
|
|
if (!is_keyframe && context->got_key_frame <= 0) {
|
2016-03-17 13:55:00 +00:00
|
|
|
context->no_key_frame++;
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no keyframe, %d\n", context->no_key_frame);
|
|
|
|
if (context->no_key_frame > 50) {
|
|
|
|
if ((is_keyframe = is_start)) {
|
2016-03-28 06:09:07 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no keyframe, treating start as key. frames=%d\n", context->no_key_frame);
|
2016-03-17 13:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 06:09:07 +00:00
|
|
|
}
|
2016-02-04 00:36:17 +00:00
|
|
|
|
2016-03-28 06:09:07 +00:00
|
|
|
// if (is_keyframe) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "got key %d\n", context->got_key_frame);
|
2015-03-29 12:32:12 +00:00
|
|
|
|
2015-02-13 20:10:42 +00:00
|
|
|
if (context->need_decoder_reset != 0) {
|
|
|
|
vpx_codec_destroy(&context->decoder);
|
|
|
|
context->decoder_init = 0;
|
2015-02-17 21:55:23 +00:00
|
|
|
init_decoder(codec);
|
2015-02-13 20:10:42 +00:00
|
|
|
context->need_decoder_reset = 0;
|
|
|
|
}
|
2015-01-26 16:48:59 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
if (!context->decoder_init) {
|
2015-02-17 21:55:23 +00:00
|
|
|
init_decoder(codec);
|
2014-11-18 22:39:32 +00:00
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
if (!context->decoder_init) {
|
2014-11-11 03:38:56 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "VPX decoder is not initialized!\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
decoder = &context->decoder;
|
2015-03-26 20:00:55 +00:00
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "len: %d ts: %u mark:%d\n", frame->datalen, frame->timestamp, frame->m);
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-03-29 12:32:12 +00:00
|
|
|
// context->last_received_timestamp = frame->timestamp;
|
2014-11-11 03:38:56 +00:00
|
|
|
context->last_received_complete_picture = frame->m ? SWITCH_TRUE : SWITCH_FALSE;
|
|
|
|
|
2015-05-23 03:08:27 +00:00
|
|
|
if (is_start) {
|
|
|
|
context->got_start_frame = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_keyframe) {
|
2015-03-26 20:00:55 +00:00
|
|
|
if (context->got_key_frame <= 0) {
|
|
|
|
context->got_key_frame = 1;
|
2015-11-18 16:31:30 +00:00
|
|
|
context->no_key_frame = 0;
|
2015-03-26 20:00:55 +00:00
|
|
|
} else {
|
|
|
|
context->got_key_frame++;
|
|
|
|
}
|
|
|
|
} else if (context->got_key_frame <= 0) {
|
|
|
|
if ((--context->got_key_frame % 200) == 0) {
|
2015-04-08 19:16:10 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Waiting for key frame %d\n", context->got_key_frame);
|
2015-03-26 20:00:55 +00:00
|
|
|
}
|
2015-11-18 16:31:30 +00:00
|
|
|
|
|
|
|
get_refresh = 1;
|
2016-03-28 06:09:07 +00:00
|
|
|
|
2015-05-23 03:08:27 +00:00
|
|
|
if (!context->got_start_frame) {
|
|
|
|
switch_goto_status(SWITCH_STATUS_MORE_DATA, end);
|
|
|
|
}
|
2015-03-26 20:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-11 23:33:20 +00:00
|
|
|
status = context->is_vp9 ? buffer_vp9_packets(context, frame) : buffer_vp8_packets(context, frame);
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-05-06 19:02:44 +00:00
|
|
|
|
|
|
|
if (context->dec_iter && (frame->img = (switch_image_t *) vpx_codec_get_frame(decoder, &context->dec_iter))) {
|
|
|
|
switch_goto_status(SWITCH_STATUS_SUCCESS, end);
|
|
|
|
}
|
|
|
|
|
2016-02-04 00:36:17 +00:00
|
|
|
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "====READ buf:%ld got_key:%d st:%d m:%d\n", switch_buffer_inuse(context->vpx_packet_buffer), context->got_key_frame, status, frame->m);
|
2014-11-20 00:01:32 +00:00
|
|
|
|
|
|
|
len = switch_buffer_inuse(context->vpx_packet_buffer);
|
|
|
|
|
2014-12-02 16:51:21 +00:00
|
|
|
//if (frame->m && (status != SWITCH_STATUS_SUCCESS || !len)) {
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WTF????? %d %ld\n", status, len);
|
|
|
|
//}
|
2014-11-20 00:01:32 +00:00
|
|
|
|
|
|
|
if (status == SWITCH_STATUS_SUCCESS && frame->m && len) {
|
2014-11-11 03:38:56 +00:00
|
|
|
uint8_t *data;
|
|
|
|
int corrupted = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
switch_buffer_peek_zerocopy(context->vpx_packet_buffer, (void *)&data);
|
|
|
|
|
2015-05-06 19:02:44 +00:00
|
|
|
context->dec_iter = NULL;
|
2014-11-11 03:38:56 +00:00
|
|
|
err = vpx_codec_decode(decoder, data, (unsigned int)len, NULL, 0);
|
|
|
|
|
|
|
|
if (err != VPX_CODEC_OK) {
|
2015-05-01 20:40:50 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Error decoding %" SWITCH_SIZE_T_FMT " bytes, [%d:%s:%s]\n",
|
2015-03-26 20:00:55 +00:00
|
|
|
len, err, vpx_codec_error(decoder), vpx_codec_error_detail(decoder));
|
2014-11-24 16:18:19 +00:00
|
|
|
switch_goto_status(SWITCH_STATUS_RESTART, end);
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vpx_codec_control(decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted) != VPX_CODEC_OK) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "VPX control error!\n");
|
2014-11-18 22:39:32 +00:00
|
|
|
switch_goto_status(SWITCH_STATUS_RESTART, end);
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
2015-03-26 20:00:55 +00:00
|
|
|
|
|
|
|
if (corrupted) {
|
|
|
|
frame->img = NULL;
|
2016-03-28 06:09:07 +00:00
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "corrupted!!\n");
|
|
|
|
#endif
|
2015-03-26 20:00:55 +00:00
|
|
|
} else {
|
2015-05-06 19:02:44 +00:00
|
|
|
frame->img = (switch_image_t *) vpx_codec_get_frame(decoder, &context->dec_iter);
|
2016-03-28 06:09:07 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VP9
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "decoded: %dx%d\n", frame->img->d_w, frame->img->d_h);
|
|
|
|
#endif
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
2015-03-26 20:00:55 +00:00
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
2015-03-26 20:00:55 +00:00
|
|
|
|
|
|
|
if (!frame->img) {
|
2015-05-23 03:08:27 +00:00
|
|
|
//context->need_decoder_reset = 1;
|
2015-05-06 19:02:44 +00:00
|
|
|
context->got_key_frame = 0;
|
2015-05-23 03:08:27 +00:00
|
|
|
context->got_start_frame = 0;
|
2015-03-26 20:00:55 +00:00
|
|
|
status = SWITCH_STATUS_RESTART;
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
end:
|
|
|
|
|
2015-05-23 03:08:27 +00:00
|
|
|
if (status == SWITCH_STATUS_RESTART) {
|
|
|
|
switch_buffer_zero(context->vpx_packet_buffer);
|
|
|
|
//context->need_decoder_reset = 1;
|
|
|
|
context->got_key_frame = 0;
|
|
|
|
context->got_start_frame = 0;
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "RESET VPX\n");
|
|
|
|
}
|
2014-11-18 22:39:32 +00:00
|
|
|
|
2015-01-24 07:54:38 +00:00
|
|
|
if (!frame->img || status == SWITCH_STATUS_RESTART) {
|
2014-11-18 22:39:32 +00:00
|
|
|
status = SWITCH_STATUS_MORE_DATA;
|
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2015-05-06 19:02:44 +00:00
|
|
|
if (context->got_key_frame <= 0 || get_refresh) {
|
2014-12-03 00:39:53 +00:00
|
|
|
switch_set_flag(frame, SFF_WAIT_KEY_FRAME);
|
2014-11-18 22:39:32 +00:00
|
|
|
}
|
2014-11-11 03:38:56 +00:00
|
|
|
|
2016-06-16 02:08:46 +00:00
|
|
|
if (frame->img && (codec->flags & SWITCH_CODEC_FLAG_VIDEO_PATCHING)) {
|
|
|
|
switch_img_free(&context->patch_img);
|
|
|
|
switch_img_copy(frame->img, &context->patch_img);
|
|
|
|
frame->img = context->patch_img;
|
|
|
|
}
|
|
|
|
|
2014-11-18 22:39:32 +00:00
|
|
|
return status;
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:30:39 +00:00
|
|
|
|
|
|
|
static switch_status_t switch_vpx_control(switch_codec_t *codec,
|
|
|
|
switch_codec_control_command_t cmd,
|
|
|
|
switch_codec_control_type_t ctype,
|
|
|
|
void *cmd_data,
|
2015-09-11 16:37:01 +00:00
|
|
|
switch_codec_control_type_t atype,
|
|
|
|
void *cmd_arg,
|
2014-11-13 03:30:39 +00:00
|
|
|
switch_codec_control_type_t *rtype,
|
|
|
|
void **ret_data)
|
|
|
|
{
|
|
|
|
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
|
|
|
|
|
|
|
switch(cmd) {
|
2015-02-13 20:10:42 +00:00
|
|
|
case SCC_VIDEO_RESET:
|
|
|
|
{
|
|
|
|
int mask = *((int *) cmd_data);
|
|
|
|
if (mask & 1) {
|
|
|
|
context->need_encoder_reset = 1;
|
|
|
|
}
|
|
|
|
if (mask & 2) {
|
|
|
|
context->need_decoder_reset = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-09-21 19:08:32 +00:00
|
|
|
case SCC_VIDEO_GEN_KEYFRAME:
|
2014-11-13 03:30:39 +00:00
|
|
|
context->need_key_frame = 1;
|
|
|
|
break;
|
2015-02-06 22:13:32 +00:00
|
|
|
case SCC_VIDEO_BANDWIDTH:
|
|
|
|
{
|
|
|
|
switch(ctype) {
|
|
|
|
case SCCT_INT:
|
|
|
|
context->change_bandwidth = *((int *) cmd_data);
|
|
|
|
break;
|
|
|
|
case SCCT_STRING:
|
|
|
|
{
|
|
|
|
char *bwv = (char *) cmd_data;
|
|
|
|
context->change_bandwidth = switch_parse_bandwidth_string(bwv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-11-13 03:30:39 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
static switch_status_t switch_vpx_destroy(switch_codec_t *codec)
|
|
|
|
{
|
|
|
|
vpx_context_t *context = (vpx_context_t *)codec->private_info;
|
|
|
|
|
|
|
|
if (context) {
|
2016-06-16 02:08:46 +00:00
|
|
|
|
|
|
|
switch_img_free(&context->patch_img);
|
|
|
|
|
2014-11-11 03:38:56 +00:00
|
|
|
if ((codec->flags & SWITCH_CODEC_FLAG_ENCODE)) {
|
2014-11-15 01:01:56 +00:00
|
|
|
vpx_codec_destroy(&context->encoder);
|
2014-11-11 03:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((codec->flags & SWITCH_CODEC_FLAG_DECODE)) {
|
|
|
|
vpx_codec_destroy(&context->decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context->pic) {
|
|
|
|
vpx_img_free(context->pic);
|
|
|
|
context->pic = NULL;
|
|
|
|
}
|
|
|
|
if (context->vpx_packet_buffer) {
|
|
|
|
switch_buffer_destroy(&context->vpx_packet_buffer);
|
|
|
|
context->vpx_packet_buffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_vpx_load)
|
|
|
|
{
|
|
|
|
switch_codec_interface_t *codec_interface;
|
|
|
|
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
|
|
|
SWITCH_ADD_CODEC(codec_interface, "VP8 Video");
|
|
|
|
switch_core_codec_add_video_implementation(pool, codec_interface, 99, "VP8", NULL,
|
2014-11-13 03:30:39 +00:00
|
|
|
switch_vpx_init, switch_vpx_encode, switch_vpx_decode, switch_vpx_control, switch_vpx_destroy);
|
2015-03-11 23:33:20 +00:00
|
|
|
SWITCH_ADD_CODEC(codec_interface, "VP9 Video");
|
|
|
|
switch_core_codec_add_video_implementation(pool, codec_interface, 99, "VP9", NULL,
|
|
|
|
switch_vpx_init, switch_vpx_encode, switch_vpx_decode, switch_vpx_control, switch_vpx_destroy);
|
2014-11-11 03:38:56 +00:00
|
|
|
|
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-02-29 17:39:51 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2014-11-11 03:38:56 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
|
|
|
*/
|