Help needed with sound decoding from ffmpeg lib

2 posts / 0 new
Last post
TSK
TSK's picture
Offline
Last seen: 1 year 1 month ago
Joined: 2011-06-28 02:06
Help needed with sound decoding from ffmpeg lib

If I use my routine to play an 8svx file from disk it works but I can't get it working with audio decoding of ffmpeg lib. I found some routine to convert float to int16 but it doesn't help.

  1. if (packet.stream_index==AudStreamNum)
  2. {
  3. totalDecoded=0;
  4. AudBuffI=0;
  5. lastDecoded=avcodec_decode_audio4(AudCodecCtx,VidFrame,(int *)&got_sndframe_ptr,&packet);
  6. totalDecoded+=lastDecoded;
  7. if ((VidFrame!=NULL) && (VidFrame->data[0]!=NULL) && (got_sndframe_ptr!=0))
  8. {
  9. if (lastDecoded==VidFrame->nb_samples) { AudBuff16=(int16 *)VidFrame->nb_samples; }
  10. else
  11. {
  12. if (AudBuff16==0)
  13. {
  14. AudBuffSize=VidFrame->nb_samples;
  15. AudBuff16=(int16 *)IExec->AllocVecTags(AudBuffSize*sizeof(int16),
  16. AVT_Alignment,4, //4
  17. AVT_ClearWithValue,0,
  18. TAG_DONE);
  19. }
  20. else
  21. if (AudBuffSize!=VidFrame->nb_samples)
  22. {
  23. IExec->FreeVec(AudBuff16);
  24. AudBuffSize=VidFrame->nb_samples;
  25. AudBuff16=(int16 *)IExec->AllocVecTags(AudBuffSize*sizeof(int16),
  26. AVT_Alignment,4, //4
  27. AVT_ClearWithValue,0,
  28. TAG_DONE);
  29. }
  30. if (AudBuff16!=0)
  31. {
  32. if ((AudCodecCtx->sample_fmt==AV_SAMPLE_FMT_FLT) || (AudCodecCtx->sample_fmt==AV_SAMPLE_FMT_FLTP))
  33. {
  34. for (r=0; r<lastDecoded; r++) { AudBuff16[AudBuffI+r]=(int8)((float32)VidFrame->data[0][r]*(32767.0f)); }
  35. }
  36. AudBuffI+=lastDecoded;
  37.  
  38. while (totalDecoded<AudBuffSize)
  39. {
  40. lastDecoded=avcodec_decode_audio4(AudCodecCtx,VidFrame,(int *)&got_sndframe_ptr,&packet);
  41. totalDecoded+=lastDecoded;
  42. if ((VidFrame!=NULL) && (VidFrame->data[0]!=NULL) && (got_sndframe_ptr!=0))
  43. {
  44. if (totalDecoded<=AudBuffSize)
  45. {
  46. if ((AudCodecCtx->sample_fmt==AV_SAMPLE_FMT_FLT) || (AudCodecCtx->sample_fmt==AV_SAMPLE_FMT_FLTP))
  47. {
  48. for (r=0; r<lastDecoded; r++) { AudBuff16[AudBuffI+r]=(int8)((float32)VidFrame->data[0][r]*(32767.0f)); }
  49. }
  50. AudBuffI+=lastDecoded;
  51. }
  52. else
  53. {
  54. if ((AudCodecCtx->sample_fmt==AV_SAMPLE_FMT_FLT) || (AudCodecCtx->sample_fmt==AV_SAMPLE_FMT_FLTP))
  55. {
  56. for (r=0; r<(AudBuffSize-(totalDecoded-lastDecoded)); r++) { AudBuff16[AudBuffI+r]=(int16)((float32)VidFrame->data[0][r]*(32767.0f)); }
  57. }
  58. AudBuffI+=lastDecoded;
  59. }
  60. }
  61. } // while (totalDecoded<AudBuffSize)
  62.  
  63. if ((AHIAudType!=-1) && (AudBuff16!=0))
  64. {
  65. AHIio->ahir_Std.io_Command=CMD_WRITE;
  66. AHIio->ahir_Std.io_Data=AudBuff16;
  67. AHIio->ahir_Std.io_Length=AudBuffSize;
  68. AHIio->ahir_Std.io_Offset=0;
  69. AHIio->ahir_Type=AHIST_M16S; //AHIAudType;
  70. AHIio->ahir_Frequency=AudCodecCtx->sample_rate;
  71. AHIio->ahir_Volume=0x10000; // full volume = 0x10000
  72. AHIio->ahir_Position=0x8000; //stereopos
  73. AHIio->ahir_Link=NULL;
  74.  
  75. IExec->DoIO((struct IORequest*)AHIio);
  76. ioreq_sent=TRUE;
  77. }
  78. else
  79. {
  80. ioreq_sent=FALSE;
  81. if (AHIAudType==-1) { IExec->DebugPrintF("Audio type not recognized, type=%ld, buff_ptr=%lu\n",AHIAudType,(uint32)AudBuff16); }
  82. }
  83. }
  84. }
  85. }
  86. } // audio frame

Any ideas what I'm doing wrong ?

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
AVFrame.data is an uint8

AVFrame.data is an uint8 pointer. So what you are doing is to read a byte from the ffmpeg buffer, convert it to float, convert it back to int (wrongly casted to int8) and store it in your AHI buffer. Effectively you are converting uint8 to int8. That's not what you want.

Try this instead:

  1. {
  2. float32 *floatdata = (float32 *)VidFrame->data[0];
  3. for (r=0; r<lastDecoded; r++)
  4. {
  5. AudBuff16[AudBuffI+r] = (floatdata[r]*(32767.0f));
  6. }
  7. }
Log in or register to post comments