By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

DVI/RTP packet decode

P: 1
Hi,

I need to stream audio and/or video to a PDA device. There is a trick here which is:
The PDA must receive the stream from a multicast address. For this I have implemented a Bridge application which joins the multicast group on behalf of the PDA and receives the Multicast RTP packets (which are sent from JMStudio) and Unicasts them to the PDA.(HP iPAQ) I had no problem implementing this. The streaming is done using JMStudio player which encodes the streaming audio data into a number of encodings (DVI/RTP in my case). I choose DVI/RTP and stream a .wav audio file.
Now I have to accept the packets and play the stream on the PDA.
The j2me application receives all the RTP packets successfully and I can extract usefull information from the packets such as: Timestamp, sequence number, payload type. The payload type is 5 which means it is a DVI4 encoding.
I use the following method to decode the samples:

Expand|Select|Wrap|Line Numbers
  1.  public int decode(Object state, byte[] input, int inp, int len, short[] output, int outp) {
  2. int sign;
  3. int delta;
  4. int vpdiff;
  5. //int valprev = audio.Convert.byte2short(input, inp);
  6.  
  7. //int index = input[inp + 2];
  8. int valprev=0,index=0; 
  9. int inputbuffer = 0;
  10. int bufferstep = 0;
  11. ////////////////////////////////////
  12. valprev = input[0] <<8;
  13. valprev |= input[1] &0xff;
  14.  
  15. index = input[2] &0xff;
  16. //////////////////////////////////////////
  17.  
  18. if ( index < 0 ) index = 0;
  19. else if ( index > 88 ) index = 88;
  20.  
  21. int step = stepsizeTable[index];
  22.  
  23. inp += 4;
  24.  
  25. len = (len - 4) * 2;
  26.  
  27. int count = len;
  28. while(count-- > 0) {
  29.  
  30. if ( 0 == bufferstep ) {
  31. inputbuffer = input[inp++];
  32. delta = (inputbuffer >> 4) & 0xf;
  33. bufferstep = 1;
  34. } else {
  35. delta = inputbuffer & 0xf;
  36. bufferstep = 0;
  37. }
  38.  
  39. index += indexTable[delta];
  40. if ( index < 0 ) index = 0;
  41. else if ( index > 88 ) index = 88;
  42.  
  43. sign = delta & 8;
  44. delta = delta & 7;
  45.  
  46. vpdiff = step >> 1;
  47. if ( (delta & 4) == 4 ) vpdiff += (step << 2);
  48. if ( (delta & 2) == 2 ) vpdiff += (step << 1);
  49. if ( (delta & 1) == 1 ) vpdiff += step;
  50. vpdiff >>= 2;
  51.  
  52. if ( 0 != sign )
  53. valprev -= vpdiff;
  54. else
  55. valprev += vpdiff;
  56.  
  57. if ( valprev > 32767 )
  58. valprev = 32767;
  59. else if ( valprev < -32768 )
  60. valprev = -32768;
  61.  
  62. step = stepsizeTable[index];
  63. output[outp++] = (short) valprev;
  64. }
  65.  
  66. ((AdpcmState)state).valprev = valprev;
  67. ((AdpcmState)state).index = index;
  68. return len;
  69. }
which stores the result into a short[] array.

I then convert this short[] array into a byte[] array with the following way:

s is the short[] array
adp is the byte array

Expand|Select|Wrap|Line Numbers
  1.  for(int g=0,k=0;g<s.length;g++,k=k+2){
  2. audio.Convert.short2byte(s[g],adp,k);
  3. }
  4.  
  5. public static void short2byte(short ival, byte b[], int offset) {
  6. int i;
  7. int bits = 16;
  8.  
  9. for(i = 0; i >< 2; i++) {
  10. bits -= 8;
  11. b[offset + i] = (byte) ((ival >> bits) & 0xff);
  12. }
  13. }
The final result is loaded to the player as follows:

Expand|Select|Wrap|Line Numbers
  1.  ByteArrayInputStream input1 = new ByteArrayInputStream(adp);
  2. player = Manager.createPlayer(input1, "audio/x-wav");//create new player
  3. player.addPlayerListener(this);
  4. player.prefetch();
  5. player.realize();
  6.  
  7. player.start();
  8.  
The player begins to play but I only get horrible sounds instead of the original wave file

The player now initializes ok without any problem but I can only hear a meesed up sound rather than the original. So now I strongly believe that the problem is in the decoding of the samples of the DVI/RTP codec.
Jul 5 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.