In fact, there is something else must be pay attention.
you bytesdecoded is the length of the decoded stream, but what you need return in this function is the pcm output size. You forget that the decoded stream have been resampled this after, so you must compute the exact length of the resampled stream. Even you could get the same result in some cases but you must did in a right way.