History

avcodec/adpcm: Mono ADPCM for EA WVE Files

aaron committed 2 years ago

Commit f44353c1 parent 0e321921 file · +32 −12Browse files

Unified · Split

libavcodec/adpcm.c+32−12
@@ -262,7 +262,7 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx) · 261 unmodified lines
262262 break;
263263 case AV_CODEC_ID_ADPCM_DTK:
264264 case AV_CODEC_ID_ADPCM_EA:
265− min_channels = 2;
265+ min_channels = 1;
266266 break;
267267 case AV_CODEC_ID_ADPCM_AFC:
268268 case AV_CODEC_ID_ADPCM_EA_R1:
@@ -914,10 +914,12 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb, · 645 unmodified lines
914914 bytestream2_seek(gb, -8, SEEK_CUR);
915915 break;
916916 case AV_CODEC_ID_ADPCM_EA:
917+ /* Stereo is 30 bytes per block */
918+ /* Mono is 15 bytes per block */
917919 has_coded_samples = 1;
918920 *coded_samples = bytestream2_get_le32(gb);
919921 *coded_samples -= *coded_samples % 28;
920− nb_samples = (buf_size - 12) / 30 * 28;
922+ nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
921923 break;
922924 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
923925 has_coded_samples = 1;
@@ -1652,10 +1654,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 728 unmodified lines
16521654 int coeff1l, coeff2l, coeff1r, coeff2r;
16531655 int shift_left, shift_right;
16541656
1655− /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1656− each coding 28 stereo samples. */
1657+ /* Each EA ADPCM frame has a 12-byte header followed by 30-byte (stereo) or 15-byte (mono) pieces,
1658+ each coding 28 stereo/mono samples. */
16571659
1658− if (channels != 2)
1660+ if (channels != 2 && channels != 1)
16591661 return AVERROR_INVALIDDATA;
16601662
16611663 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
@@ -1670,11 +1672,16 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 8 unmodified lines
16701672 coeff1r = ea_adpcm_table[ byte & 0x0F];
16711673 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
16721674
1673− byte = bytestream2_get_byteu(&gb);
1674− shift_left = 20 - (byte >> 4);
1675− shift_right = 20 - (byte & 0x0F);
1675+ if (channels == 2){
1676+ byte = bytestream2_get_byteu(&gb);
1677+ shift_left = 20 - (byte >> 4);
1678+ shift_right = 20 - (byte & 0x0F);
1679+ } else{
1680+ /* Mono packs the shift into the coefficient byte's lower nibble instead */
1681+ shift_left = 20 - (byte & 0x0F);
1682+ }
16761683
1677− for (int count2 = 0; count2 < 28; count2++) {
1684+ for (int count2 = 0; count2 < (channels == 2 ? 28 : 14); count2++) {
16781685 byte = bytestream2_get_byteu(&gb);
16791686 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
16801687 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
@@ -1691,11 +1698,24 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 10 unmodified lines
16911698 previous_right_sample = current_right_sample;
16921699 current_right_sample = av_clip_int16(next_right_sample);
16931700 *samples++ = current_left_sample;
1694− *samples++ = current_right_sample;
1701+
1702+ if (channels == 2){
1703+ *samples++ = current_right_sample;
1704+ } else {
1705+ next_left_sample = sign_extend(byte, 4) * (1 << shift_left);
1706+
1707+ next_left_sample = (next_left_sample +
1708+ (current_left_sample * coeff1l) +
1709+ (previous_left_sample * coeff2l) + 0x80) >> 8;
1710+
1711+ previous_left_sample = current_left_sample;
1712+ current_left_sample = av_clip_int16(next_left_sample);
1713+
1714+ *samples++ = current_left_sample;
1715+ }
16951716 }
16961717 }
1697−
1698− bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1718+ bytestream2_skip(&gb, channels == 2 ? 2 : 3); // Skip terminating NULs
16991719 ) /* End of CASE */
17001720 CASE(ADPCM_EA_MAXIS_XA,
17011721 int coeff[2][2], shift[2];

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t