macosforge / alac

The Apple Lossless Audio Codec (ALAC) is a lossless audio codec developed by Apple and deployed on all of its platforms and devices.
https://macosforge.github.io/alac/
Apache License 2.0
349 stars 63 forks source link

header building seems to be potential broken #13

Open macosforgebot opened 10 years ago

macosforgebot commented 10 years ago

nt@… originally submitted this as ticket:19


Hello folks,

I guess I found a bug in alacconvert. When building the packet table header of CAFF file, and any case of "mReminderFrames" should be zero, it goes to be the value of "kALACDefaultFramesPerPacket" due to the lack of a condition. As a result, mNumberPackets will be increased, against the code will. With this patch, we've confirmed better results come in various music source. The patch follows:

Thanks, nt

$ diff -c CAFFileALAC.cpp~ CAFFileALAC.cpp
*** CAFFileALAC.cpp~    Sun Apr 13 22:21:11 2014
--- CAFFileALAC.cpp Wed Apr 23 21:27:03 2014
***************
*** 266,274 ****
      thePacketTableHeader->mNumberPackets = thePacketTableHeader->mNumberValidFrames/kALACDefaultFramesPerPacket;
      thePacketTableHeader->mPrimingFrames = 0;
      thePacketTableHeader->mRemainderFrames = thePacketTableHeader->mNumberValidFrames - thePacketTableHeader->mNumberPackets * kALACDefaultFramesPerPacket;
!     thePacketTableHeader->mRemainderFrames = kALACDefaultFramesPerPacket - thePacketTableHeader->mRemainderFrames;
!     if (thePacketTableHeader->mRemainderFrames) thePacketTableHeader->mNumberPackets += 1;
!     
      // Ok, we have to assume the worst case scenario for packet sizes
      theMaxPacketSize = (theInputFormat.mBitsPerChannel >> 3) * theInputFormat.mChannelsPerFrame * kALACDefaultFramesPerPacket + kALACMaxEscapeHeaderBytes;

--- 266,280 ----
      thePacketTableHeader->mNumberPackets = thePacketTableHeader->mNumberValidFrames/kALACDefaultFramesPerPacket;
      thePacketTableHeader->mPrimingFrames = 0;
      thePacketTableHeader->mRemainderFrames = thePacketTableHeader->mNumberValidFrames - thePacketTableHeader->mNumberPackets * kALACDefaultFramesPerPacket;
!     if (0) {
!       thePacketTableHeader->mRemainderFrames = kALACDefaultFramesPerPacket - thePacketTableHeader->mRemainderFrames;
!       if (thePacketTableHeader->mRemainderFrames) thePacketTableHeader->mNumberPackets += 1;
!     } else { /* fixed by nt@ototoy.jp 20140423 */
!       if (thePacketTableHeader->mRemainderFrames) {
!   thePacketTableHeader->mRemainderFrames = kALACDefaultFramesPerPacket - thePacketTableHeader->mRemainderFrames;
!   thePacketTableHeader->mNumberPackets += 1;
!       }
!     }
      // Ok, we have to assume the worst case scenario for packet sizes
      theMaxPacketSize = (theInputFormat.mBitsPerChannel >> 3) * theInputFormat.mChannelsPerFrame * kALACDefaultFramesPerPacket + kALACMaxEscapeHeaderBytes;
macosforgebot commented 8 years ago

@ryandesign originally submitted this as comment:1:⁠ticket:19