ref: da413b17cb8836b94042fc852cbdaccbf3892cb5
parent: 7ff162d8b3dc3bd2f8cc6231d05a6ca4e93d7ff7
author: Jean-Marc Valin <[email protected]>
date: Thu May 11 13:18:30 EDT 2017
Add explicit user data for packet callback, add OPUS_GET_LOOKAHEAD, fix error code
--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -82,8 +82,8 @@
#define OPE_SET_SERIALNO(x) OPE_SET_SERIALNO_REQUEST, __opus_check_int(x)
#define OPE_GET_SERIALNO(x) OPE_GET_SERIALNO_REQUEST, __opus_check_int_ptr(x)
/* FIXME: Add type-checking macros to these. */
-#define OPE_SET_PACKET_CALLBACK(x) OPE_SET_PACKET_CALLBACK_REQUEST, (x)
-#define OPE_GET_PACKET_CALLBACK(x) OPE_GET_PACKET_CALLBACK_REQUEST, (x)
+#define OPE_SET_PACKET_CALLBACK(x,u) OPE_SET_PACKET_CALLBACK_REQUEST, (x), (u)
+#define OPE_GET_PACKET_CALLBACK(x,u) OPE_GET_PACKET_CALLBACK_REQUEST, (x), (u)
typedef int (*ope_write_func)(void *user_data, const unsigned char *ptr, int len);
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -192,6 +192,7 @@
int chaining_keyframe_length;
OpusEncCallbacks callbacks;
ope_packet_func packet_callback;
+ void *packet_callback_data;
OpusHeader header;
int comment_padding;
EncStream *streams;
@@ -431,12 +432,12 @@
p = oggp_get_packet_buffer(enc->oggp, 276);
int packet_size = opus_header_to_packet(&enc->header, p, 276);
oggp_commit_packet(enc->oggp, packet_size, 0, 0);
- if (enc->packet_callback) enc->packet_callback(enc->streams->user_data, p, packet_size, 0);
+ if (enc->packet_callback) enc->packet_callback(enc->packet_callback_data, p, packet_size, 0);
oe_flush_page(enc);
p = oggp_get_packet_buffer(enc->oggp, enc->streams->comment_length);
memcpy(p, enc->streams->comment, enc->streams->comment_length);
oggp_commit_packet(enc->oggp, enc->streams->comment_length, 0, 0);
- if (enc->packet_callback) enc->packet_callback(enc->streams->user_data, p, enc->streams->comment_length, 0);
+ if (enc->packet_callback) enc->packet_callback(enc->packet_callback_data, p, enc->streams->comment_length, 0);
oe_flush_page(enc);
#else
@@ -455,7 +456,7 @@
op.granulepos=0;
op.packetno=0;
ogg_stream_packetin(&enc->streams->os, &op);
- if (enc->packet_callback) enc->packet_callback(enc->streams->user_data, op.packet, op.bytes, 0);
+ if (enc->packet_callback) enc->packet_callback(enc->packet_callback_data, op.packet, op.bytes, 0);
oe_flush_page(enc);
op.packet = (unsigned char *)enc->streams->comment;
@@ -465,7 +466,7 @@
op.granulepos = 0;
op.packetno = 1;
ogg_stream_packetin(&enc->streams->os, &op);
- if (enc->packet_callback) enc->packet_callback(enc->streams->user_data, op.packet, op.bytes, 0);
+ if (enc->packet_callback) enc->packet_callback(enc->packet_callback_data, op.packet, op.bytes, 0);
oe_flush_page(enc);
#endif
}
@@ -533,7 +534,7 @@
#else
ogg_stream_packetin(&enc->streams->os, &op);
#endif
- if (enc->packet_callback) enc->packet_callback(enc->streams->user_data, op.packet, op.bytes, 0);
+ if (enc->packet_callback) enc->packet_callback(enc->packet_callback_data, op.packet, op.bytes, 0);
/* FIXME: Also flush on too many segments. */
#ifdef USE_OGGP
flush_needed = op.e_o_s;
@@ -586,7 +587,7 @@
#else
ogg_stream_packetin(&enc->streams->os, &op2);
#endif
- if (enc->packet_callback) enc->packet_callback(enc->streams->user_data, op2.packet, op2.bytes, 0);
+ if (enc->packet_callback) enc->packet_callback(enc->packet_callback_data, op2.packet, op2.bytes, 0);
}
end_granule48k = (enc->streams->end_granule*48000 + enc->rate - 1)/enc->rate + enc->global_granule_offset;
cont = 1;
@@ -811,6 +812,12 @@
ret = opus_multistream_encoder_ctl(enc->st, request, value);
}
break;
+ case OPUS_GET_LOOKAHEAD_REQUEST:
+ {
+ opus_int32 *value = va_arg(ap, opus_int32*);
+ ret = opus_multistream_encoder_ctl(enc->st, request, value);
+ }
+ break;
case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
{
opus_int32 value = va_arg(ap, opus_int32);
@@ -890,7 +897,9 @@
case OPE_SET_PACKET_CALLBACK_REQUEST:
{
ope_packet_func value = va_arg(ap, ope_packet_func);
+ void *data = va_arg(ap, void *);
enc->packet_callback = value;
+ enc->packet_callback_data = data;
ret = OPE_OK;
}
break;
@@ -898,7 +907,7 @@
return OPE_UNIMPLEMENTED;
}
va_end(ap);
- translate = request < 14000 || (ret < 0 && ret >= -10);
+ translate = ret != 0 && (request < 14000 || (ret < 0 && ret >= -10));
if (translate) {
if (ret == OPUS_BAD_ARG) ret = OPE_BAD_ARG;
else if (ret == OPUS_INTERNAL_ERROR) ret = OPE_INTERNAL_ERROR;