Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Almost) Add support for G.729 Annex B #153

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/audio/audio_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,30 +572,45 @@ t_g729a_audio_decoder::~t_g729a_audio_decoder()

uint16 t_g729a_audio_decoder::get_ptime(uint16 payload_size) const
{
return (payload_size * 8) / (audio_sample_rate(_codec) / 1000);
// Account for a possible 2-byte SID frame that will expand to 80
// samples juste like any other frame. The (size+8)/10 expression,
// rounded down, results in the total number of frames, SID or not.
return (((payload_size + 8) / 10) * 80) / (audio_sample_rate(_codec) / 1000);
}

uint16 t_g729a_audio_decoder::decode(uint8 *payload, uint16 payload_size,
int16 *pcm_buf, uint16 pcm_buf_size)
{
assert((payload_size % 10) == 0);
assert(pcm_buf_size >= payload_size*8);
// Allow for a 2-byte SID frame at the end of the RTP packet
assert(((payload_size % 10) == 0) || ((payload_size % 10) == 2));
// See get_ptime() above for an explanation of this expression
assert(pcm_buf_size >= (((payload_size + 8) / 10) * 80));

for (uint16 done = 0; done < payload_size; done += 10)
uint8 frame_size = 10;
uint16 result_size = 0;

for (uint16 done = 0; done < payload_size; done += frame_size)
{
// RFC 3551 mandates that a SID frame be the last in the packet;
// its presence can thus be deduced from the payload size.
bool is_sid = ((payload_size - done) == 2);
frame_size = (is_sid ? 2 : 10);
#ifdef HAVE_BCG729_ANNEX_B
bcg729Decoder(_context, &payload[done], 0, false, false, false, &pcm_buf[done * 8]);
bcg729Decoder(_context, &payload[done], 0, false, is_sid, false, &pcm_buf[result_size]);
#else
bcg729Decoder(_context, &payload[done], false, &pcm_buf[done * 8]);
bcg729Decoder(_context, &payload[done], false, &pcm_buf[result_size]);
#endif
result_size += 80;
}

return payload_size * 8;
return result_size;
}

bool t_g729a_audio_decoder::valid_payload_size(uint16 payload_size, uint16 sample_buf_size) const
{
return payload_size > 0 && (payload_size % 10) == 0;
// Allow for a 2-byte SID frame at the end of the RTP packet
return (payload_size > 0) &&
(((payload_size % 10) == 0) || ((payload_size % 10) == 2));
}

uint16 t_g729a_audio_decoder::conceal(int16 *pcm_buf, uint16 pcm_buf_size)
Expand Down
25 changes: 18 additions & 7 deletions src/audio/audio_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ t_g729a_audio_encoder::t_g729a_audio_encoder(uint16 payload_id, uint16 ptime, t_
: t_audio_encoder(payload_id, ptime, user_config)
{
#ifdef HAVE_BCG729_ANNEX_B
// Disable G.729B features for the time being
_context = initBcg729EncoderChannel(false);
#else
_context = initBcg729EncoderChannel();
Expand All @@ -491,22 +492,32 @@ uint16 t_g729a_audio_encoder::encode(int16 *sample_buf, uint16 nsamples,
uint8 *payload, uint16 payload_size, bool &silence)
{
assert ((nsamples % 80) == 0);
assert (payload_size >= (nsamples/8));
assert (payload_size >= ((nsamples / 80) * 10));

silence = false;
uint8 frame_size = 10;
uint16 result_size = 0;

// G.729B TODO: RFC 3551 mandates that a SID frame be the last in the
// packet; this could require us to only encode a portion of the
// samples, which the current interface does not allow for.
for (uint16 done = 0; done < nsamples; done += 80)
{
#ifdef HAVE_BCG729_ANNEX_B
uint8 frame_size = 10;
bcg729Encoder(_context, &sample_buf[done], &payload[done / 8], &frame_size);
assert(frame_size == 10);
bcg729Encoder(_context, &sample_buf[done], &payload[result_size], &frame_size);
#else
bcg729Encoder(_context, &sample_buf[done], &payload[done / 8]);
bcg729Encoder(_context, &sample_buf[done], &payload[result_size]);
#endif
result_size += frame_size;
}

return nsamples / 8;
silence = (result_size == 0);

// G.729B TODO: In the event that the encoding only resulted in
// untransmitted frames, we would return 0 bytes; the caller should be
// ready for that outcome. (At the moment, when sending a keepalive
// packet, it would call putData() with len = 0, which is interpreted as
// "a default by payload type".)
return result_size;
}

#endif