Makes multi-stream encoding code use the repacketizer

This commit is contained in:
Jean-Marc Valin 2011-09-09 13:56:09 -04:00
parent 0ca076d527
commit 131d888234
3 changed files with 34 additions and 18 deletions

View file

@ -235,10 +235,15 @@ int opus_multistream_encode_float(
char *ptr; char *ptr;
int tot_size; int tot_size;
VARDECL(opus_val16, buf); VARDECL(opus_val16, buf);
unsigned char tmp_data[1276]; /* Max size in case the encoder decides to return three frames */
unsigned char tmp_data[3*1275+7];
VARDECL(unsigned char, rp_);
OpusRepacketizer *rp;
ALLOC_STACK; ALLOC_STACK;
ALLOC(buf, 2*frame_size, opus_val16); ALLOC(buf, 2*frame_size, opus_val16);
ALLOC(rp_, opus_repacketizer_get_size(), unsigned char);
rp = (OpusRepacketizer*)rp_;
ptr = (char*)st + align(sizeof(OpusMSEncoder)); ptr = (char*)st + align(sizeof(OpusMSEncoder));
coupled_size = opus_encoder_get_size(2); coupled_size = opus_encoder_get_size(2);
mono_size = opus_encoder_get_size(1); mono_size = opus_encoder_get_size(1);
@ -256,6 +261,7 @@ int opus_multistream_encode_float(
int len; int len;
int curr_max; int curr_max;
opus_repacketizer_init(rp);
enc = (OpusEncoder*)ptr; enc = (OpusEncoder*)ptr;
if (s < st->layout.nb_coupled_streams) if (s < st->layout.nb_coupled_streams)
{ {
@ -284,17 +290,13 @@ int opus_multistream_encode_float(
RESTORE_STACK; RESTORE_STACK;
return len; return len;
} }
/* ToC first */ /* We need to use the repacketizer to add the self-delimiting lengths
*data++ = tmp_data[0]; while taking into account the fact that the encoder can now return
if (s != st->layout.nb_streams-1) more than one frame at a time (e.g. 60 ms CELT-only) */
{ opus_repacketizer_cat(rp, tmp_data, len);
int tmp = encode_size(len-1, data); len = opus_repacketizer_out_range_impl(rp, 0, opus_repacketizer_get_nb_frames(rp), data, max_data_bytes-tot_size, s != st->layout.nb_streams-1);
data += tmp; data += len;
tot_size += tmp;
}
/* IMPORTANT: Here we assume that the encoder only returned one frame */
tot_size += len; tot_size += len;
OPUS_COPY(data, &tmp_data[1], len-1);
} }
RESTORE_STACK; RESTORE_STACK;
return tot_size; return tot_size;

View file

@ -52,4 +52,6 @@ static inline int align(int i)
return (i+3)&-4; return (i+3)&-4;
} }
int opus_repacketizer_out_range_impl(OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen, int self_delimited);
#endif /* OPUS_PRIVATE_H_ */ #endif /* OPUS_PRIVATE_H_ */

View file

@ -97,7 +97,7 @@ int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp)
return rp->nb_frames; return rp->nb_frames;
} }
int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen) int opus_repacketizer_out_range_impl(OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen, int self_delimited)
{ {
int i, count, tot_size; int i, count, tot_size;
short *len; short *len;
@ -112,12 +112,17 @@ int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsign
len = rp->len+begin; len = rp->len+begin;
frames = rp->frames+begin; frames = rp->frames+begin;
if (self_delimited)
tot_size = 1 + len[count-1]>=252;
else
tot_size = 0;
switch (count) switch (count)
{ {
case 1: case 1:
{ {
/* Code 0 */ /* Code 0 */
tot_size = len[0]+1; tot_size += len[0]+1;
if (tot_size > maxlen) if (tot_size > maxlen)
return OPUS_BUFFER_TOO_SMALL; return OPUS_BUFFER_TOO_SMALL;
*data++ = rp->toc&0xFC; *data++ = rp->toc&0xFC;
@ -128,13 +133,13 @@ int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsign
if (len[1] == len[0]) if (len[1] == len[0])
{ {
/* Code 1 */ /* Code 1 */
tot_size = 2*len[0]+1; tot_size += 2*len[0]+1;
if (tot_size > maxlen) if (tot_size > maxlen)
return OPUS_BUFFER_TOO_SMALL; return OPUS_BUFFER_TOO_SMALL;
*data++ = (rp->toc&0xFC) | 0x1; *data++ = (rp->toc&0xFC) | 0x1;
} else { } else {
/* Code 2 */ /* Code 2 */
tot_size = len[0]+len[1]+2+(len[0]>=252); tot_size += len[0]+len[1]+2+(len[0]>=252);
if (tot_size > maxlen) if (tot_size > maxlen)
return OPUS_BUFFER_TOO_SMALL; return OPUS_BUFFER_TOO_SMALL;
*data++ = (rp->toc&0xFC) | 0x2; *data++ = (rp->toc&0xFC) | 0x2;
@ -158,7 +163,7 @@ int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsign
} }
if (vbr) if (vbr)
{ {
tot_size = 2; tot_size += 2;
for (i=0;i<count-1;i++) for (i=0;i<count-1;i++)
tot_size += 1 + (len[i]>=252) + len[i]; tot_size += 1 + (len[i]>=252) + len[i];
tot_size += len[count-1]; tot_size += len[count-1];
@ -170,7 +175,7 @@ int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsign
for (i=0;i<count-1;i++) for (i=0;i<count-1;i++)
data += encode_size(len[i], data); data += encode_size(len[i], data);
} else { } else {
tot_size = count*len[0]+2; tot_size += count*len[0]+2;
if (tot_size > maxlen) if (tot_size > maxlen)
return OPUS_BUFFER_TOO_SMALL; return OPUS_BUFFER_TOO_SMALL;
*data++ = (rp->toc&0xFC) | 0x3; *data++ = (rp->toc&0xFC) | 0x3;
@ -179,6 +184,8 @@ int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsign
} }
break; break;
} }
if (self_delimited)
data += encode_size(len[count-1], data);
/* Copy the actual data */ /* Copy the actual data */
for (i=0;i<count;i++) for (i=0;i<count;i++)
{ {
@ -188,9 +195,14 @@ int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsign
return tot_size; return tot_size;
} }
int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen)
{
return opus_repacketizer_out_range_impl(rp, begin, end, data, maxlen, 0);
}
int opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, int maxlen) int opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, int maxlen)
{ {
return opus_repacketizer_out_range(rp, 0, rp->nb_frames, data, maxlen); return opus_repacketizer_out_range_impl(rp, 0, rp->nb_frames, data, maxlen, 0);
} }